LLVM  6.0.0svn
ConstantFolder.h
Go to the documentation of this file.
1 //===- ConstantFolder.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 ConstantFolder class, a helper for IRBuilder.
11 // It provides IRBuilder with a set of methods for creating constants
12 // with minimal folding. For general constant creation and folding,
13 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_IR_CONSTANTFOLDER_H
18 #define LLVM_IR_CONSTANTFOLDER_H
19 
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/InstrTypes.h"
23 #include "llvm/IR/Instruction.h"
24 
25 namespace llvm {
26 
27 /// ConstantFolder - Create constants with minimum, target independent, folding.
29 public:
30  explicit ConstantFolder() = default;
31 
32  //===--------------------------------------------------------------------===//
33  // Binary Operators
34  //===--------------------------------------------------------------------===//
35 
37  bool HasNUW = false, bool HasNSW = false) const {
38  return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
39  }
40 
41  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
42  return ConstantExpr::getFAdd(LHS, RHS);
43  }
44 
46  bool HasNUW = false, bool HasNSW = false) const {
47  return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
48  }
49 
50  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
51  return ConstantExpr::getFSub(LHS, RHS);
52  }
53 
55  bool HasNUW = false, bool HasNSW = false) const {
56  return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
57  }
58 
59  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
60  return ConstantExpr::getFMul(LHS, RHS);
61  }
62 
64  bool isExact = false) const {
65  return ConstantExpr::getUDiv(LHS, RHS, isExact);
66  }
67 
69  bool isExact = false) const {
70  return ConstantExpr::getSDiv(LHS, RHS, isExact);
71  }
72 
73  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
74  return ConstantExpr::getFDiv(LHS, RHS);
75  }
76 
77  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
78  return ConstantExpr::getURem(LHS, RHS);
79  }
80 
81  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
82  return ConstantExpr::getSRem(LHS, RHS);
83  }
84 
85  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
86  return ConstantExpr::getFRem(LHS, RHS);
87  }
88 
90  bool HasNUW = false, bool HasNSW = false) const {
91  return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
92  }
93 
95  bool isExact = false) const {
96  return ConstantExpr::getLShr(LHS, RHS, isExact);
97  }
98 
100  bool isExact = false) const {
101  return ConstantExpr::getAShr(LHS, RHS, isExact);
102  }
103 
104  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
105  return ConstantExpr::getAnd(LHS, RHS);
106  }
107 
108  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
109  return ConstantExpr::getOr(LHS, RHS);
110  }
111 
112  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
113  return ConstantExpr::getXor(LHS, RHS);
114  }
115 
117  Constant *LHS, Constant *RHS) const {
118  return ConstantExpr::get(Opc, LHS, RHS);
119  }
120 
121  //===--------------------------------------------------------------------===//
122  // Unary Operators
123  //===--------------------------------------------------------------------===//
124 
126  bool HasNUW = false, bool HasNSW = false) const {
127  return ConstantExpr::getNeg(C, HasNUW, HasNSW);
128  }
129 
131  return ConstantExpr::getFNeg(C);
132  }
133 
135  return ConstantExpr::getNot(C);
136  }
137 
138  //===--------------------------------------------------------------------===//
139  // Memory Instructions
140  //===--------------------------------------------------------------------===//
141 
143  ArrayRef<Constant *> IdxList) const {
144  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
145  }
146 
148  // This form of the function only exists to avoid ambiguous overload
149  // warnings about whether to convert Idx to ArrayRef<Constant *> or
150  // ArrayRef<Value *>.
151  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
152  }
153 
155  ArrayRef<Value *> IdxList) const {
156  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
157  }
158 
160  ArrayRef<Constant *> IdxList) const {
161  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
162  }
163 
165  Constant *Idx) const {
166  // This form of the function only exists to avoid ambiguous overload
167  // warnings about whether to convert Idx to ArrayRef<Constant *> or
168  // ArrayRef<Value *>.
169  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
170  }
171 
173  ArrayRef<Value *> IdxList) const {
174  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
175  }
176 
177  //===--------------------------------------------------------------------===//
178  // Cast/Conversion Operators
179  //===--------------------------------------------------------------------===//
180 
182  Type *DestTy) const {
183  return ConstantExpr::getCast(Op, C, DestTy);
184  }
185 
187  return ConstantExpr::getPointerCast(C, DestTy);
188  }
189 
191  Type *DestTy) const {
193  }
194 
196  bool isSigned) const {
197  return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
198  }
199 
200  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
201  return ConstantExpr::getFPCast(C, DestTy);
202  }
203 
204  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
205  return CreateCast(Instruction::BitCast, C, DestTy);
206  }
207 
208  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
209  return CreateCast(Instruction::IntToPtr, C, DestTy);
210  }
211 
212  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
213  return CreateCast(Instruction::PtrToInt, C, DestTy);
214  }
215 
217  return ConstantExpr::getZExtOrBitCast(C, DestTy);
218  }
219 
221  return ConstantExpr::getSExtOrBitCast(C, DestTy);
222  }
223 
225  return ConstantExpr::getTruncOrBitCast(C, DestTy);
226  }
227 
228  //===--------------------------------------------------------------------===//
229  // Compare Instructions
230  //===--------------------------------------------------------------------===//
231 
233  Constant *RHS) const {
234  return ConstantExpr::getCompare(P, LHS, RHS);
235  }
236 
238  Constant *RHS) const {
239  return ConstantExpr::getCompare(P, LHS, RHS);
240  }
241 
242  //===--------------------------------------------------------------------===//
243  // Other Instructions
244  //===--------------------------------------------------------------------===//
245 
246  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
247  return ConstantExpr::getSelect(C, True, False);
248  }
249 
251  return ConstantExpr::getExtractElement(Vec, Idx);
252  }
253 
255  Constant *Idx) const {
256  return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
257  }
258 
260  Constant *Mask) const {
261  return ConstantExpr::getShuffleVector(V1, V2, Mask);
262  }
263 
265  ArrayRef<unsigned> IdxList) const {
266  return ConstantExpr::getExtractValue(Agg, IdxList);
267  }
268 
270  ArrayRef<unsigned> IdxList) const {
271  return ConstantExpr::getInsertValue(Agg, Val, IdxList);
272  }
273 };
274 
275 } // end namespace llvm
276 
277 #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:2122
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:1507
Compute iterated dominance frontiers using a linear time algorithm.
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:1115
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 * 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:1980
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:1832
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2126
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:2002
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2115
static Constant * getFMul(Constant *C1, Constant *C2)
Definition: Constants.cpp:2144
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1518
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:2193
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
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:1711
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1530
static Constant * getAShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2198
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:1854
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:33
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:2148
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2158
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:2048
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
#define P(N)
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2103
static Constant * getFRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2170
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
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:2174
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1480
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2025
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:853
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2109
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:1492
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:2153
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:1435
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1474
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
static Constant * getFSub(Constant *C1, Constant *C2)
Definition: Constants.cpp:2133
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1486
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2096
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:1142
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2178
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:2186
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:2166
Constant * CreateURem(Constant *LHS, Constant *RHS) const
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2162
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 * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2072
Constant * CreateXor(Constant *LHS, Constant *RHS) const
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2137
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2182