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