switch the constantexpr, target folder, and IRBuilder interfaces
[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/InstrTypes.h"
24 #include "llvm/Analysis/ConstantFolding.h"
25
26 namespace llvm {
27
28 class TargetData;
29
30 /// TargetFolder - Create constants with target dependent folding.
31 class TargetFolder {
32   const TargetData *TD;
33
34   /// Fold - Fold the constant using target specific information.
35   Constant *Fold(Constant *C) const {
36     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
37       if (Constant *CF = ConstantFoldConstantExpression(CE, TD))
38         return CF;
39     return C;
40   }
41
42 public:
43   explicit TargetFolder(const TargetData *TheTD) : TD(TheTD) {}
44
45   //===--------------------------------------------------------------------===//
46   // Binary Operators
47   //===--------------------------------------------------------------------===//
48
49   Constant *CreateAdd(Constant *LHS, Constant *RHS,
50                       bool HasNUW = false, bool HasNSW = false) const {
51     return Fold(ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW));
52   }
53   Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
54     return Fold(ConstantExpr::getFAdd(LHS, RHS));
55   }
56   Constant *CreateSub(Constant *LHS, Constant *RHS,
57                       bool HasNUW = false, bool HasNSW = false) const {
58     return Fold(ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW));
59   }
60   Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
61     return Fold(ConstantExpr::getFSub(LHS, RHS));
62   }
63   Constant *CreateMul(Constant *LHS, Constant *RHS,
64                       bool HasNUW = false, bool HasNSW = false) const {
65     return Fold(ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW));
66   }
67   Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
68     return Fold(ConstantExpr::getFMul(LHS, RHS));
69   }
70   Constant *CreateUDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
71     return Fold(ConstantExpr::getUDiv(LHS, RHS, isExact));
72   }
73   Constant *CreateSDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
74     return Fold(ConstantExpr::getSDiv(LHS, RHS, isExact));
75   }
76   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
77     return Fold(ConstantExpr::getFDiv(LHS, RHS));
78   }
79   Constant *CreateURem(Constant *LHS, Constant *RHS) const {
80     return Fold(ConstantExpr::getURem(LHS, RHS));
81   }
82   Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
83     return Fold(ConstantExpr::getSRem(LHS, RHS));
84   }
85   Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
86     return Fold(ConstantExpr::getFRem(LHS, RHS));
87   }
88   Constant *CreateShl(Constant *LHS, Constant *RHS,
89                       bool HasNUW = false, bool HasNSW = false) const {
90     return Fold(ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW));
91   }
92   Constant *CreateLShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
93     return Fold(ConstantExpr::getLShr(LHS, RHS, isExact));
94   }
95   Constant *CreateAShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
96     return Fold(ConstantExpr::getAShr(LHS, RHS, isExact));
97   }
98   Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
99     return Fold(ConstantExpr::getAnd(LHS, RHS));
100   }
101   Constant *CreateOr(Constant *LHS, Constant *RHS) const {
102     return Fold(ConstantExpr::getOr(LHS, RHS));
103   }
104   Constant *CreateXor(Constant *LHS, Constant *RHS) const {
105     return Fold(ConstantExpr::getXor(LHS, RHS));
106   }
107
108   Constant *CreateBinOp(Instruction::BinaryOps Opc,
109                         Constant *LHS, Constant *RHS) const {
110     return Fold(ConstantExpr::get(Opc, LHS, RHS));
111   }
112
113   //===--------------------------------------------------------------------===//
114   // Unary Operators
115   //===--------------------------------------------------------------------===//
116
117   Constant *CreateNeg(Constant *C,
118                       bool HasNUW = false, bool HasNSW = false) const {
119     return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
120   }
121   Constant *CreateFNeg(Constant *C) const {
122     return Fold(ConstantExpr::getFNeg(C));
123   }
124   Constant *CreateNot(Constant *C) const {
125     return Fold(ConstantExpr::getNot(C));
126   }
127
128   //===--------------------------------------------------------------------===//
129   // Memory Instructions
130   //===--------------------------------------------------------------------===//
131
132   Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList,
133                                 unsigned NumIdx) const {
134     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
135   }
136   Constant *CreateGetElementPtr(Constant *C, Value* const *IdxList,
137                                 unsigned NumIdx) const {
138     return Fold(ConstantExpr::getGetElementPtr(C, IdxList, NumIdx));
139   }
140
141   Constant *CreateInBoundsGetElementPtr(Constant *C, Constant* const *IdxList,
142                                         unsigned NumIdx) const {
143     return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
144   }
145   Constant *CreateInBoundsGetElementPtr(Constant *C, Value* const *IdxList,
146                                         unsigned NumIdx) const {
147     return Fold(ConstantExpr::getInBoundsGetElementPtr(C, IdxList, NumIdx));
148   }
149
150   //===--------------------------------------------------------------------===//
151   // Cast/Conversion Operators
152   //===--------------------------------------------------------------------===//
153
154   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
155                        const Type *DestTy) const {
156     if (C->getType() == DestTy)
157       return C; // avoid calling Fold
158     return Fold(ConstantExpr::getCast(Op, C, DestTy));
159   }
160   Constant *CreateIntCast(Constant *C, const Type *DestTy,
161                           bool isSigned) const {
162     if (C->getType() == DestTy)
163       return C; // avoid calling Fold
164     return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
165   }
166   Constant *CreatePointerCast(Constant *C, const Type *DestTy) const {
167     return ConstantExpr::getPointerCast(C, DestTy);
168   }
169   Constant *CreateBitCast(Constant *C, const Type *DestTy) const {
170     return CreateCast(Instruction::BitCast, C, DestTy);
171   }
172   Constant *CreateIntToPtr(Constant *C, const Type *DestTy) const {
173     return CreateCast(Instruction::IntToPtr, C, DestTy);
174   }
175   Constant *CreatePtrToInt(Constant *C, const Type *DestTy) const {
176     return CreateCast(Instruction::PtrToInt, C, DestTy);
177   }
178   Constant *CreateZExtOrBitCast(Constant *C, const Type *DestTy) const {
179     if (C->getType() == DestTy)
180       return C; // avoid calling Fold
181     return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
182   }
183   Constant *CreateSExtOrBitCast(Constant *C, const Type *DestTy) const {
184     if (C->getType() == DestTy)
185       return C; // avoid calling Fold
186     return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
187   }
188   Constant *CreateTruncOrBitCast(Constant *C, const Type *DestTy) const {
189     if (C->getType() == DestTy)
190       return C; // avoid calling Fold
191     return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
192   }
193
194   //===--------------------------------------------------------------------===//
195   // Compare Instructions
196   //===--------------------------------------------------------------------===//
197
198   Constant *CreateICmp(CmpInst::Predicate P, Constant *LHS,
199                        Constant *RHS) const {
200     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
201   }
202   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
203                        Constant *RHS) const {
204     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
205   }
206
207   //===--------------------------------------------------------------------===//
208   // Other Instructions
209   //===--------------------------------------------------------------------===//
210
211   Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
212     return Fold(ConstantExpr::getSelect(C, True, False));
213   }
214
215   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const {
216     return Fold(ConstantExpr::getExtractElement(Vec, Idx));
217   }
218
219   Constant *CreateInsertElement(Constant *Vec, Constant *NewElt,
220                                 Constant *Idx) const {
221     return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
222   }
223
224   Constant *CreateShuffleVector(Constant *V1, Constant *V2,
225                                 Constant *Mask) const {
226     return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
227   }
228
229   Constant *CreateExtractValue(Constant *Agg, const unsigned *IdxList,
230                                unsigned NumIdx) const {
231     return Fold(ConstantExpr::getExtractValue(Agg, IdxList, NumIdx));
232   }
233
234   Constant *CreateInsertValue(Constant *Agg, Constant *Val,
235                               const unsigned *IdxList, unsigned NumIdx) const {
236     return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList, NumIdx));
237   }
238 };
239
240 }
241
242 #endif