LLVM  3.7.0
TargetFolder.h
Go to the documentation of this file.
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"
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.
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:
44  explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
45 
46  //===--------------------------------------------------------------------===//
47  // Binary Operators
48  //===--------------------------------------------------------------------===//
49 
51  bool HasNUW = false, bool HasNSW = false) const {
52  return Fold(ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW));
53  }
54  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
55  return Fold(ConstantExpr::getFAdd(LHS, RHS));
56  }
58  bool HasNUW = false, bool HasNSW = false) const {
59  return Fold(ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW));
60  }
61  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
62  return Fold(ConstantExpr::getFSub(LHS, RHS));
63  }
65  bool HasNUW = false, bool HasNSW = false) const {
66  return Fold(ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW));
67  }
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  }
77  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
78  return Fold(ConstantExpr::getFDiv(LHS, RHS));
79  }
80  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
81  return Fold(ConstantExpr::getURem(LHS, RHS));
82  }
83  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
84  return Fold(ConstantExpr::getSRem(LHS, RHS));
85  }
86  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
87  return Fold(ConstantExpr::getFRem(LHS, RHS));
88  }
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  }
99  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
100  return Fold(ConstantExpr::getAnd(LHS, RHS));
101  }
102  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
103  return Fold(ConstantExpr::getOr(LHS, RHS));
104  }
105  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
106  return Fold(ConstantExpr::getXor(LHS, RHS));
107  }
108 
110  Constant *LHS, Constant *RHS) const {
111  return Fold(ConstantExpr::get(Opc, LHS, RHS));
112  }
113 
114  //===--------------------------------------------------------------------===//
115  // Unary Operators
116  //===--------------------------------------------------------------------===//
117 
119  bool HasNUW = false, bool HasNSW = false) const {
120  return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
121  }
123  return Fold(ConstantExpr::getFNeg(C));
124  }
126  return Fold(ConstantExpr::getNot(C));
127  }
128 
129  //===--------------------------------------------------------------------===//
130  // Memory Instructions
131  //===--------------------------------------------------------------------===//
132 
134  ArrayRef<Constant *> IdxList) const {
135  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
136  }
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(Ty, C, Idx));
142  }
144  ArrayRef<Value *> IdxList) const {
145  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
146  }
147 
149  ArrayRef<Constant *> IdxList) const {
150  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
151  }
153  Constant *Idx) const {
154  // This form of the function only exists to avoid ambiguous overload
155  // warnings about whether to convert Idx to ArrayRef<Constant *> or
156  // ArrayRef<Value *>.
157  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx));
158  }
160  ArrayRef<Value *> IdxList) const {
161  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
162  }
163 
164  //===--------------------------------------------------------------------===//
165  // Cast/Conversion Operators
166  //===--------------------------------------------------------------------===//
167 
169  Type *DestTy) const {
170  if (C->getType() == DestTy)
171  return C; // avoid calling Fold
172  return Fold(ConstantExpr::getCast(Op, C, DestTy));
173  }
175  bool isSigned) const {
176  if (C->getType() == DestTy)
177  return C; // avoid calling Fold
178  return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
179  }
180  Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
181  if (C->getType() == DestTy)
182  return C; // avoid calling Fold
183  return Fold(ConstantExpr::getPointerCast(C, DestTy));
184  }
185  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
186  if (C->getType() == DestTy)
187  return C; // avoid calling Fold
188  return Fold(ConstantExpr::getFPCast(C, DestTy));
189  }
190  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
191  return CreateCast(Instruction::BitCast, C, DestTy);
192  }
193  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
194  return CreateCast(Instruction::IntToPtr, C, DestTy);
195  }
196  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
197  return CreateCast(Instruction::PtrToInt, C, DestTy);
198  }
200  if (C->getType() == DestTy)
201  return C; // avoid calling Fold
202  return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
203  }
205  if (C->getType() == DestTy)
206  return C; // avoid calling Fold
207  return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
208  }
210  if (C->getType() == DestTy)
211  return C; // avoid calling Fold
212  return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
213  }
214 
216  Type *DestTy) const {
217  if (C->getType() == DestTy)
218  return C; // avoid calling Fold
219  return Fold(ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy));
220  }
221 
222  //===--------------------------------------------------------------------===//
223  // Compare Instructions
224  //===--------------------------------------------------------------------===//
225 
227  Constant *RHS) const {
228  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
229  }
231  Constant *RHS) const {
232  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
233  }
234 
235  //===--------------------------------------------------------------------===//
236  // Other Instructions
237  //===--------------------------------------------------------------------===//
238 
239  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
240  return Fold(ConstantExpr::getSelect(C, True, False));
241  }
242 
244  return Fold(ConstantExpr::getExtractElement(Vec, Idx));
245  }
246 
248  Constant *Idx) const {
249  return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
250  }
251 
253  Constant *Mask) const {
254  return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
255  }
256 
258  ArrayRef<unsigned> IdxList) const {
259  return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
260  }
261 
263  ArrayRef<unsigned> IdxList) const {
264  return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
265  }
266 };
267 
268 }
269 
270 #endif
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Definition: TargetFolder.h:168
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
Definition: TargetFolder.h:133
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:152
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2265
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Definition: TargetFolder.h:252
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1663
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:226
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
Definition: TargetFolder.h:143
Constant * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:71
Constant * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:64
Constant * CreateSelect(Constant *C, Constant *True, Constant *False) const
Definition: TargetFolder.h:239
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1092
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2123
Constant * CreateFRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:86
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:109
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1990
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstantExpression - Attempt to fold the constant expression using the specified DataLayo...
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:193
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2145
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:230
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:209
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2258
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2287
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:83
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1674
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
Definition: TargetFolder.h:159
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2336
Constant * CreateFAdd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:54
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:262
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
Definition: Constants.cpp:1868
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1687
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:89
Constant * CreateFNeg(Constant *C) const
Definition: TargetFolder.h:122
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2341
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2012
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:96
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2291
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2301
Constant * CreateXor(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:105
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2191
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2246
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2313
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1115
Constant * CreateOr(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:102
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2317
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const
Definition: TargetFolder.h:243
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1636
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2168
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
Constant * CreateBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:190
Constant * CreatePointerCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:180
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2252
Constant * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:74
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:215
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:77
Constant * CreateFMul(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:68
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:118
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1648
Constant * CreateFPCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:185
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2296
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:204
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1591
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1630
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2276
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1642
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2239
Constant * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:93
Constant * CreateAnd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:99
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:137
Constant * CreateNot(Constant *C) const
Definition: TargetFolder.h:125
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2321
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2329
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:57
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Definition: TargetFolder.h:247
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:199
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:50
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2309
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2305
Constant * CreateURem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:80
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
Definition: TargetFolder.h:148
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2215
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:257
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2280
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:44
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Definition: TargetFolder.h:174
Constant * CreateFSub(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:61
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2325
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:196