LLVM  8.0.1
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 (Constant *CF = ConstantFoldConstant(C, DL))
38  return CF;
39  return C;
40  }
41 
42 public:
43  explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
44 
45  //===--------------------------------------------------------------------===//
46  // Binary Operators
47  //===--------------------------------------------------------------------===//
48 
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  }
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  }
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  }
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 
109  Constant *LHS, Constant *RHS) const {
110  return Fold(ConstantExpr::get(Opc, LHS, RHS));
111  }
112 
113  //===--------------------------------------------------------------------===//
114  // Unary Operators
115  //===--------------------------------------------------------------------===//
116 
118  bool HasNUW = false, bool HasNSW = false) const {
119  return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
120  }
122  return Fold(ConstantExpr::getFNeg(C));
123  }
125  return Fold(ConstantExpr::getNot(C));
126  }
127 
128  //===--------------------------------------------------------------------===//
129  // Memory Instructions
130  //===--------------------------------------------------------------------===//
131 
133  ArrayRef<Constant *> IdxList) const {
134  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
135  }
137  // This form of the function only exists to avoid ambiguous overload
138  // warnings about whether to convert Idx to ArrayRef<Constant *> or
139  // ArrayRef<Value *>.
140  return Fold(ConstantExpr::getGetElementPtr(Ty, C, Idx));
141  }
143  ArrayRef<Value *> IdxList) const {
144  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
145  }
146 
148  ArrayRef<Constant *> IdxList) const {
149  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
150  }
152  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(Ty, C, Idx));
157  }
159  ArrayRef<Value *> IdxList) const {
160  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
161  }
162 
163  //===--------------------------------------------------------------------===//
164  // Cast/Conversion Operators
165  //===--------------------------------------------------------------------===//
166 
168  Type *DestTy) const {
169  if (C->getType() == DestTy)
170  return C; // avoid calling Fold
171  return Fold(ConstantExpr::getCast(Op, C, DestTy));
172  }
174  bool isSigned) const {
175  if (C->getType() == DestTy)
176  return C; // avoid calling Fold
177  return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
178  }
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  }
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  }
189  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
190  return CreateCast(Instruction::BitCast, C, DestTy);
191  }
192  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
193  return CreateCast(Instruction::IntToPtr, C, DestTy);
194  }
195  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
196  return CreateCast(Instruction::PtrToInt, C, DestTy);
197  }
199  if (C->getType() == DestTy)
200  return C; // avoid calling Fold
201  return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
202  }
204  if (C->getType() == DestTy)
205  return C; // avoid calling Fold
206  return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
207  }
209  if (C->getType() == DestTy)
210  return C; // avoid calling Fold
211  return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
212  }
213 
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 
226  Constant *RHS) const {
227  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
228  }
230  Constant *RHS) const {
231  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
232  }
233 
234  //===--------------------------------------------------------------------===//
235  // Other Instructions
236  //===--------------------------------------------------------------------===//
237 
238  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
239  return Fold(ConstantExpr::getSelect(C, True, False));
240  }
241 
243  return Fold(ConstantExpr::getExtractElement(Vec, Idx));
244  }
245 
247  Constant *Idx) const {
248  return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
249  }
250 
252  Constant *Mask) const {
253  return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
254  }
255 
257  ArrayRef<unsigned> IdxList) const {
258  return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
259  }
260 
262  ArrayRef<unsigned> IdxList) const {
263  return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
264  }
265 };
266 
267 }
268 
269 #endif
uint64_t CallInst * C
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Definition: TargetFolder.h:167
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:256
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2245
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1602
This class represents lattice values for constants.
Definition: AllocatorList.h:24
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1154
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:108
Constant * CreateFNeg(Constant *C) const
Definition: TargetFolder.h:121
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:49
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:88
Constant * CreateNot(Constant *C) const
Definition: TargetFolder.h:124
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:195
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2103
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:1956
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2249
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:198
Constant * CreateFAdd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:53
Constant * CreatePointerCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:179
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Definition: TargetFolder.h:251
Constant * CreateXor(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:104
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2125
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2238
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2267
Constant * CreateFRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:85
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1613
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:203
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2316
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:56
Constant * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:63
Constant * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:70
Constant * CreateURem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:79
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:117
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Attempt to fold the constant using the specified DataLayout.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1625
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2321
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:208
Constant * CreateFMul(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:67
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:1978
Constant * CreateFSub(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:60
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:76
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2271
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2281
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Definition: TargetFolder.h:173
Constant * CreateAnd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:98
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2171
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2226
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2293
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important base class in LLVM.
Definition: Constant.h:42
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: TargetFolder.h:158
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Constant * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:73
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2297
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1575
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2148
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:646
Constant * CreateSelect(Constant *C, Constant *True, Constant *False) const
Definition: TargetFolder.h:238
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:136
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2232
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const
Definition: TargetFolder.h:242
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:225
Constant * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:92
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1587
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2276
Constant * CreateBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:189
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:214
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:261
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1530
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1569
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: TargetFolder.h:132
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2256
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1581
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2219
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:229
Constant * CreateOr(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:101
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1181
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:192
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2301
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: TargetFolder.h:142
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2309
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:95
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:151
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:82
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2289
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: TargetFolder.h:147
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2285
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Definition: TargetFolder.h:246
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2195
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2260
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:43
Constant * CreateFPCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:184
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2305
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
Definition: Constants.cpp:1806