Make two more LLVM headers standalone
[oota-llvm.git] / include / llvm / Support / TargetFolder.h
1 //====-- llvm/Support/TargetFolder.h - Constant folding helper -*- C++ -*-====//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the TargetFolder class, a helper for IRBuilder.
11 // It provides IRBuilder with a set of methods for creating constants with
12 // target dependent folding, in addition to the same target-independent
13 // folding that the ConstantFolder class provides.  For general constant
14 // creation and folding, use ConstantExpr and the routines in
15 // llvm/Analysis/ConstantFolding.h.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_SUPPORT_TARGETFOLDER_H
20 #define LLVM_SUPPORT_TARGETFOLDER_H
21
22 #include "llvm/Constants.h"
23 #include "llvm/Instruction.h"
24 #include "llvm/InstrTypes.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26
27 namespace llvm {
28
29 class TargetData;
30 class LLVMContext;
31
32 /// TargetFolder - Create constants with target dependent folding.
33 class TargetFolder {
34   const TargetData *TD;
35   LLVMContext &Context;
36
37   /// Fold - Fold the constant using target specific information.
38   Constant *Fold(Constant *C) const {
39     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
40       if (Constant *CF = ConstantFoldConstantExpression(CE, Context, TD))
41         return CF;
42     return C;
43   }
44
45 public:
46   explicit TargetFolder(const TargetData *TheTD, LLVMContext &C) :
47     TD(TheTD), Context(C) {}
48
49   //===--------------------------------------------------------------------===//
50   // Binary Operators
51   //===--------------------------------------------------------------------===//
52
53   Constant *CreateAdd(Constant *LHS, Constant *RHS) const {
54     return Fold(ConstantExpr::getAdd(LHS, RHS));
55   }
56   Constant *CreateNSWAdd(Constant *LHS, Constant *RHS) const {
57     return Fold(ConstantExpr::getNSWAdd(LHS, RHS));
58   }
59   Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
60     return Fold(ConstantExpr::getFAdd(LHS, RHS));
61   }
62   Constant *CreateSub(Constant *LHS, Constant *RHS) const {
63     return Fold(ConstantExpr::getSub(LHS, RHS));
64   }
65   Constant *CreateNSWSub(Constant *LHS, Constant *RHS) const {
66     return Fold(ConstantExpr::getNSWSub(LHS, RHS));
67   }
68   Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
69     return Fold(ConstantExpr::getFSub(LHS, RHS));
70   }
71   Constant *CreateMul(Constant *LHS, Constant *RHS) const {
72     return Fold(ConstantExpr::getMul(LHS, RHS));
73   }
74   Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
75     return Fold(ConstantExpr::getFMul(LHS, RHS));
76   }
77   Constant *CreateUDiv(Constant *LHS, Constant *RHS) const {
78     return Fold(ConstantExpr::getUDiv(LHS, RHS));
79   }
80   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
81     return Fold(ConstantExpr::getSDiv(LHS, RHS));
82   }
83   Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
84     return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
85   }
86   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
87     return Fold(ConstantExpr::getFDiv(LHS, RHS));
88   }
89   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
90     return Fold(ConstantExpr::getURem(LHS, RHS));
91   }
92   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
93     return Fold(ConstantExpr::getSRem(LHS, RHS));
94   }
95   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
96     return Fold(ConstantExpr::getFRem(LHS, RHS));
97   }
98   Constant *CreateShl(Constant *LHS, Constant *RHS) const {
99     return Fold(ConstantExpr::getShl(LHS, RHS));
100   }
101   Constant *CreateLShr(Constant *LHS, Constant *RHS) const {
102     return Fold(ConstantExpr::getLShr(LHS, RHS));
103   }
104   Constant *CreateAShr(Constant *LHS, Constant *RHS) const {
105     return Fold(ConstantExpr::getAShr(LHS, RHS));
106   }
107   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
108     return Fold(ConstantExpr::getAnd(LHS, RHS));
109   }
110   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
111     return Fold(ConstantExpr::getOr(LHS, RHS));
112   }
113   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
114     return Fold(ConstantExpr::getXor(LHS, RHS));
115   }
116
117   Constant *CreateBinOp(Instruction::BinaryOps Opc,
118                         Constant *LHS, Constant *RHS) const {
119     return Fold(ConstantExpr::get(Opc, LHS, RHS));
120   }
121
122   //===--------------------------------------------------------------------===//
123   // Unary Operators
124   //===--------------------------------------------------------------------===//
125
126   Constant *CreateNeg(Constant *C) const {
127     return Fold(ConstantExpr::getNeg(C));
128   }
129   Constant *CreateFNeg(Constant *C) const {
130     return Fold(ConstantExpr::getFNeg(C));
131   }
132   Constant *CreateNot(Constant *C) const {
133     return Fold(ConstantExpr::getNot(C));
134   }
135
136   //===--------------------------------------------------------------------===//
137   // Memory Instructions
138   //===--------------------------------------------------------------------===//
139
140   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
141                                 unsigned NumIdx) const {
142     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
143   }
144   Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
145                                 unsigned NumIdx) const {
146     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
147   }
148
149   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
150                                         unsigned NumIdx) const {
151     return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
152   }
153   Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
154                                         unsigned NumIdx) const {
155     return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
156   }
157
158   //===--------------------------------------------------------------------===//
159   // Cast/Conversion Operators
160   //===--------------------------------------------------------------------===//
161
162   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
163                        const Type *DestTy) const {
164     if (C->getType() == DestTy)
165       return C; // avoid calling Fold
166     return Fold(ConstantExpr::getCast(Op, C, DestTy));
167   }
168   Constant *CreateIntCast(Constant *C, const Type *DestTy,
169                           bool isSigned) const {
170     if (C->getType() == DestTy)
171       return C; // avoid calling Fold
172     return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
173   }
174
175   Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
176     return CreateCast(Instruction::BitCast, C, DestTy);
177   }
178   Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
179     return CreateCast(Instruction::IntToPtr, C, DestTy);
180   }
181   Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
182     return CreateCast(Instruction::PtrToInt, C, DestTy);
183   }
184   Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
185     if (C->getType() == DestTy)
186       return C; // avoid calling Fold
187     return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
188   }
189   Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
190     if (C->getType() == DestTy)
191       return C; // avoid calling Fold
192     return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
193   }
194   Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
195     if (C->getType() == DestTy)
196       return C; // avoid calling Fold
197     return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
198   }
199
200   //===--------------------------------------------------------------------===//
201   // Compare Instructions
202   //===--------------------------------------------------------------------===//
203
204   Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
205                        Constant *RHS) const {
206     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
207   }
208   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
209                        Constant *RHS) const {
210     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
211   }
212
213   //===--------------------------------------------------------------------===//
214   // Other Instructions
215   //===--------------------------------------------------------------------===//
216
217   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
218     return Fold(ConstantExpr::getSelect(C, True, False));
219   }
220
221   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
222     return Fold(ConstantExpr::getExtractElement(Vec, Idx));
223   }
224
225   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
226                                 Constant *Idx) const {
227     return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
228   }
229
230   Constant *CreateShuffleVector(Constant *V1, Constant *V2,
231                                 Constant *Mask) const {
232     return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
233   }
234
235   Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
236                                unsigned NumIdx) const {
237     return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
238   }
239
240   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
241                               const unsigned *IdxList, unsigned NumIdx) const {
242     return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));
243   }
244 };
245
246 }
247
248 #endif