LLVM  9.0.0svn
TargetFolder.h
Go to the documentation of this file.
1 //====- TargetFolder.h - Constant folding helper ---------------*- C++ -*-====//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the TargetFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants with
11 // target dependent folding, in addition to the same target-independent
12 // folding that the ConstantFolder class provides. For general constant
13 // creation and folding, use ConstantExpr and the routines in
14 // llvm/Analysis/ConstantFolding.h.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_ANALYSIS_TARGETFOLDER_H
19 #define LLVM_ANALYSIS_TARGETFOLDER_H
20 
21 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/InstrTypes.h"
25 
26 namespace llvm {
27 
28 class DataLayout;
29 
30 /// TargetFolder - Create constants with target dependent folding.
31 class TargetFolder {
32  const DataLayout &DL;
33 
34  /// Fold - Fold the constant using target specific information.
35  Constant *Fold(Constant *C) const {
36  if (Constant *CF = ConstantFoldConstant(C, DL))
37  return CF;
38  return C;
39  }
40 
41 public:
42  explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
43 
44  //===--------------------------------------------------------------------===//
45  // Binary Operators
46  //===--------------------------------------------------------------------===//
47 
49  bool HasNUW = false, bool HasNSW = false) const {
50  return Fold(ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW));
51  }
52  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
53  return Fold(ConstantExpr::getFAdd(LHS, RHS));
54  }
56  bool HasNUW = false, bool HasNSW = false) const {
57  return Fold(ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW));
58  }
59  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
60  return Fold(ConstantExpr::getFSub(LHS, RHS));
61  }
63  bool HasNUW = false, bool HasNSW = false) const {
64  return Fold(ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW));
65  }
66  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
67  return Fold(ConstantExpr::getFMul(LHS, RHS));
68  }
69  Constant *CreateUDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
70  return Fold(ConstantExpr::getUDiv(LHS, RHS, isExact));
71  }
72  Constant *CreateSDiv(Constant *LHS, Constant *RHS, bool isExact = false)const{
73  return Fold(ConstantExpr::getSDiv(LHS, RHS, isExact));
74  }
75  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
76  return Fold(ConstantExpr::getFDiv(LHS, RHS));
77  }
78  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
79  return Fold(ConstantExpr::getURem(LHS, RHS));
80  }
81  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
82  return Fold(ConstantExpr::getSRem(LHS, RHS));
83  }
84  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
85  return Fold(ConstantExpr::getFRem(LHS, RHS));
86  }
88  bool HasNUW = false, bool HasNSW = false) const {
89  return Fold(ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW));
90  }
91  Constant *CreateLShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
92  return Fold(ConstantExpr::getLShr(LHS, RHS, isExact));
93  }
94  Constant *CreateAShr(Constant *LHS, Constant *RHS, bool isExact = false)const{
95  return Fold(ConstantExpr::getAShr(LHS, RHS, isExact));
96  }
97  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
98  return Fold(ConstantExpr::getAnd(LHS, RHS));
99  }
100  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
101  return Fold(ConstantExpr::getOr(LHS, RHS));
102  }
103  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
104  return Fold(ConstantExpr::getXor(LHS, RHS));
105  }
106 
108  Constant *LHS, Constant *RHS) const {
109  return Fold(ConstantExpr::get(Opc, LHS, RHS));
110  }
111 
112  //===--------------------------------------------------------------------===//
113  // Unary Operators
114  //===--------------------------------------------------------------------===//
115 
117  bool HasNUW = false, bool HasNSW = false) const {
118  return Fold(ConstantExpr::getNeg(C, HasNUW, HasNSW));
119  }
121  return Fold(ConstantExpr::getFNeg(C));
122  }
124  return Fold(ConstantExpr::getNot(C));
125  }
126 
127  //===--------------------------------------------------------------------===//
128  // Memory Instructions
129  //===--------------------------------------------------------------------===//
130 
132  ArrayRef<Constant *> IdxList) const {
133  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
134  }
136  // This form of the function only exists to avoid ambiguous overload
137  // warnings about whether to convert Idx to ArrayRef<Constant *> or
138  // ArrayRef<Value *>.
139  return Fold(ConstantExpr::getGetElementPtr(Ty, C, Idx));
140  }
142  ArrayRef<Value *> IdxList) const {
143  return Fold(ConstantExpr::getGetElementPtr(Ty, C, IdxList));
144  }
145 
147  ArrayRef<Constant *> IdxList) const {
148  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
149  }
151  Constant *Idx) const {
152  // This form of the function only exists to avoid ambiguous overload
153  // warnings about whether to convert Idx to ArrayRef<Constant *> or
154  // ArrayRef<Value *>.
155  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx));
156  }
158  ArrayRef<Value *> IdxList) const {
159  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList));
160  }
161 
162  //===--------------------------------------------------------------------===//
163  // Cast/Conversion Operators
164  //===--------------------------------------------------------------------===//
165 
167  Type *DestTy) const {
168  if (C->getType() == DestTy)
169  return C; // avoid calling Fold
170  return Fold(ConstantExpr::getCast(Op, C, DestTy));
171  }
173  bool isSigned) const {
174  if (C->getType() == DestTy)
175  return C; // avoid calling Fold
176  return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
177  }
178  Constant *CreatePointerCast(Constant *C, Type *DestTy) const {
179  if (C->getType() == DestTy)
180  return C; // avoid calling Fold
181  return Fold(ConstantExpr::getPointerCast(C, DestTy));
182  }
183  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
184  if (C->getType() == DestTy)
185  return C; // avoid calling Fold
186  return Fold(ConstantExpr::getFPCast(C, DestTy));
187  }
188  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
189  return CreateCast(Instruction::BitCast, C, DestTy);
190  }
191  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
192  return CreateCast(Instruction::IntToPtr, C, DestTy);
193  }
194  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
195  return CreateCast(Instruction::PtrToInt, C, DestTy);
196  }
198  if (C->getType() == DestTy)
199  return C; // avoid calling Fold
200  return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
201  }
203  if (C->getType() == DestTy)
204  return C; // avoid calling Fold
205  return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
206  }
208  if (C->getType() == DestTy)
209  return C; // avoid calling Fold
210  return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
211  }
212 
214  Type *DestTy) const {
215  if (C->getType() == DestTy)
216  return C; // avoid calling Fold
217  return Fold(ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy));
218  }
219 
220  //===--------------------------------------------------------------------===//
221  // Compare Instructions
222  //===--------------------------------------------------------------------===//
223 
225  Constant *RHS) const {
226  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
227  }
229  Constant *RHS) const {
230  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
231  }
232 
233  //===--------------------------------------------------------------------===//
234  // Other Instructions
235  //===--------------------------------------------------------------------===//
236 
237  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
238  return Fold(ConstantExpr::getSelect(C, True, False));
239  }
240 
242  return Fold(ConstantExpr::getExtractElement(Vec, Idx));
243  }
244 
246  Constant *Idx) const {
247  return Fold(ConstantExpr::getInsertElement(Vec, NewElt, Idx));
248  }
249 
251  Constant *Mask) const {
252  return Fold(ConstantExpr::getShuffleVector(V1, V2, Mask));
253  }
254 
256  ArrayRef<unsigned> IdxList) const {
257  return Fold(ConstantExpr::getExtractValue(Agg, IdxList));
258  }
259 
261  ArrayRef<unsigned> IdxList) const {
262  return Fold(ConstantExpr::getInsertValue(Agg, Val, IdxList));
263  }
264 };
265 
266 }
267 
268 #endif
uint64_t CallInst * C
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Definition: TargetFolder.h:166
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:255
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2254
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1611
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:1153
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:107
Constant * CreateFNeg(Constant *C) const
Definition: TargetFolder.h:120
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:48
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:87
Constant * CreateNot(Constant *C) const
Definition: TargetFolder.h:123
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:194
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2112
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:1965
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2258
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:197
Constant * CreateFAdd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:52
Constant * CreatePointerCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:178
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Definition: TargetFolder.h:250
Constant * CreateXor(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:103
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2134
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2247
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2276
Constant * CreateFRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:84
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1622
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:202
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2325
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:55
Constant * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:62
Constant * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:69
Constant * CreateURem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:78
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Definition: TargetFolder.h:116
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:244
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1634
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2330
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:207
Constant * CreateFMul(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:66
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:1987
Constant * CreateFSub(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:59
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:31
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:75
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2280
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2290
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Definition: TargetFolder.h:172
Constant * CreateAnd(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:97
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2180
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2235
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2302
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
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: TargetFolder.h:157
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:72
static Constant * getAnd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2306
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1584
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2157
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:237
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:135
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2241
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const
Definition: TargetFolder.h:241
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:224
Constant * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:91
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1596
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2285
Constant * CreateBitCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:188
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:213
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
Definition: TargetFolder.h:260
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1539
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1578
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: TargetFolder.h:131
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2265
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1590
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2228
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:228
Constant * CreateOr(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:100
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1180
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:191
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2310
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: TargetFolder.h:141
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2318
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: TargetFolder.h:94
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: TargetFolder.h:150
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
Definition: TargetFolder.h:81
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2298
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: TargetFolder.h:146
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2294
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:80
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Definition: TargetFolder.h:245
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2204
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:42
Constant * CreateFPCast(Constant *C, Type *DestTy) const
Definition: TargetFolder.h:183
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2314
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:1815