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