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