LLVM  10.0.0svn
ConstantFolder.h
Go to the documentation of this file.
1 //===- ConstantFolder.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 ConstantFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants
11 // with minimal folding. For general constant creation and folding,
12 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_IR_CONSTANTFOLDER_H
17 #define LLVM_IR_CONSTANTFOLDER_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/InstrTypes.h"
22 #include "llvm/IR/Instruction.h"
23 
24 namespace llvm {
25 
26 /// ConstantFolder - Create constants with minimum, target independent, folding.
28 public:
29  explicit ConstantFolder() = default;
30 
31  //===--------------------------------------------------------------------===//
32  // Binary Operators
33  //===--------------------------------------------------------------------===//
34 
36  bool HasNUW = false, bool HasNSW = false) const {
37  return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
38  }
39 
40  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
41  return ConstantExpr::getFAdd(LHS, RHS);
42  }
43 
45  bool HasNUW = false, bool HasNSW = false) const {
46  return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
47  }
48 
49  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
50  return ConstantExpr::getFSub(LHS, RHS);
51  }
52 
54  bool HasNUW = false, bool HasNSW = false) const {
55  return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
56  }
57 
58  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
59  return ConstantExpr::getFMul(LHS, RHS);
60  }
61 
63  bool isExact = false) const {
64  return ConstantExpr::getUDiv(LHS, RHS, isExact);
65  }
66 
68  bool isExact = false) const {
69  return ConstantExpr::getSDiv(LHS, RHS, isExact);
70  }
71 
72  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
73  return ConstantExpr::getFDiv(LHS, RHS);
74  }
75 
76  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
77  return ConstantExpr::getURem(LHS, RHS);
78  }
79 
80  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
81  return ConstantExpr::getSRem(LHS, RHS);
82  }
83 
84  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
85  return ConstantExpr::getFRem(LHS, RHS);
86  }
87 
89  bool HasNUW = false, bool HasNSW = false) const {
90  return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
91  }
92 
94  bool isExact = false) const {
95  return ConstantExpr::getLShr(LHS, RHS, isExact);
96  }
97 
99  bool isExact = false) const {
100  return ConstantExpr::getAShr(LHS, RHS, isExact);
101  }
102 
103  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
104  return ConstantExpr::getAnd(LHS, RHS);
105  }
106 
107  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
108  return ConstantExpr::getOr(LHS, RHS);
109  }
110 
111  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
112  return ConstantExpr::getXor(LHS, RHS);
113  }
114 
116  Constant *LHS, Constant *RHS) const {
117  return ConstantExpr::get(Opc, LHS, RHS);
118  }
119 
120  //===--------------------------------------------------------------------===//
121  // Unary Operators
122  //===--------------------------------------------------------------------===//
123 
125  bool HasNUW = false, bool HasNSW = false) const {
126  return ConstantExpr::getNeg(C, HasNUW, HasNSW);
127  }
128 
130  return ConstantExpr::getFNeg(C);
131  }
132 
134  return ConstantExpr::getNot(C);
135  }
136 
138  return ConstantExpr::get(Opc, C);
139  }
140 
141  //===--------------------------------------------------------------------===//
142  // Memory Instructions
143  //===--------------------------------------------------------------------===//
144 
146  ArrayRef<Constant *> IdxList) const {
147  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
148  }
149 
151  // This form of the function only exists to avoid ambiguous overload
152  // warnings about whether to convert Idx to ArrayRef<Constant *> or
153  // ArrayRef<Value *>.
154  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
155  }
156 
158  ArrayRef<Value *> IdxList) const {
159  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
160  }
161 
163  ArrayRef<Constant *> IdxList) const {
164  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
165  }
166 
168  Constant *Idx) const {
169  // This form of the function only exists to avoid ambiguous overload
170  // warnings about whether to convert Idx to ArrayRef<Constant *> or
171  // ArrayRef<Value *>.
172  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
173  }
174 
176  ArrayRef<Value *> IdxList) const {
177  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
178  }
179 
180  //===--------------------------------------------------------------------===//
181  // Cast/Conversion Operators
182  //===--------------------------------------------------------------------===//
183 
185  Type *DestTy) const {
186  return ConstantExpr::getCast(Op, C, DestTy);
187  }
188 
190  return ConstantExpr::getPointerCast(C, DestTy);
191  }
192 
194  Type *DestTy) const {
196  }
197 
199  bool isSigned) const {
200  return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
201  }
202 
203  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
204  return ConstantExpr::getFPCast(C, DestTy);
205  }
206 
207  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
208  return CreateCast(Instruction::BitCast, C, DestTy);
209  }
210 
211  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
212  return CreateCast(Instruction::IntToPtr, C, DestTy);
213  }
214 
215  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
216  return CreateCast(Instruction::PtrToInt, C, DestTy);
217  }
218 
220  return ConstantExpr::getZExtOrBitCast(C, DestTy);
221  }
222 
224  return ConstantExpr::getSExtOrBitCast(C, DestTy);
225  }
226 
228  return ConstantExpr::getTruncOrBitCast(C, DestTy);
229  }
230 
231  //===--------------------------------------------------------------------===//
232  // Compare Instructions
233  //===--------------------------------------------------------------------===//
234 
236  Constant *RHS) const {
237  return ConstantExpr::getCompare(P, LHS, RHS);
238  }
239 
241  Constant *RHS) const {
242  return ConstantExpr::getCompare(P, LHS, RHS);
243  }
244 
245  //===--------------------------------------------------------------------===//
246  // Other Instructions
247  //===--------------------------------------------------------------------===//
248 
249  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
250  return ConstantExpr::getSelect(C, True, False);
251  }
252 
254  return ConstantExpr::getExtractElement(Vec, Idx);
255  }
256 
258  Constant *Idx) const {
259  return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
260  }
261 
263  Constant *Mask) const {
264  return ConstantExpr::getShuffleVector(V1, V2, Mask);
265  }
266 
268  ArrayRef<unsigned> IdxList) const {
269  return ConstantExpr::getExtractValue(Agg, IdxList);
270  }
271 
273  ArrayRef<unsigned> IdxList) const {
274  return ConstantExpr::getInsertValue(Agg, Val, IdxList);
275  }
276 };
277 
278 } // end namespace llvm
279 
280 #endif // LLVM_IR_CONSTANTFOLDER_H
uint64_t CallInst * C
Constant * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2257
Constant * CreateFNeg(Constant *C) const
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1633
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 * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Constant * CreateBitCast(Constant *C, Type *DestTy) const
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Constant * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2115
Constant * CreateNot(Constant *C) const
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const
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:1968
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2261
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const
Constant * CreateFRem(Constant *LHS, Constant *RHS) const
Constant * CreateSelect(Constant *C, Constant *True, Constant *False) const
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2137
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2250
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2279
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1644
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2328
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1656
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2333
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:1990
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
ConstantFolder - Create constants with minimum, target independent, folding.
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2283
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2293
Constant * CreateFAdd(Constant *LHS, Constant *RHS) const
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2183
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2238
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2305
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: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:2309
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1606
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2160
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:732
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2244
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Constant * CreateFSub(Constant *LHS, Constant *RHS) const
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1618
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Constant * CreateOr(Constant *LHS, Constant *RHS) const
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2288
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Constant * CreateFPCast(Constant *C, Type *DestTy) const
Constant * CreateFMul(Constant *LHS, Constant *RHS) const
Constant * CreateAnd(Constant *LHS, Constant *RHS) const
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1561
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1600
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2268
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1612
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2231
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Constant * CreatePointerCast(Constant *C, Type *DestTy) const
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1180
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2313
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2321
Constant * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
ConstantFolder()=default
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2301
Constant * CreateURem(Constant *LHS, Constant *RHS) const
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2297
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 * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2207
Constant * CreateXor(Constant *LHS, Constant *RHS) const
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2272
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2317
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:1837