LLVM  3.7.0
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/IR/Constants.h"
21 #include "llvm/IR/InstrTypes.h"
22 
23 namespace llvm {
24 
25 /// ConstantFolder - Create constants with minimum, target independent, folding.
27 public:
28  explicit ConstantFolder() {}
29 
30  //===--------------------------------------------------------------------===//
31  // Binary Operators
32  //===--------------------------------------------------------------------===//
33 
35  bool HasNUW = false, bool HasNSW = false) const {
36  return ConstantExpr::getAdd(LHS, RHS, HasNUW, HasNSW);
37  }
38  Constant *CreateFAdd(Constant *LHS, Constant *RHS) const {
39  return ConstantExpr::getFAdd(LHS, RHS);
40  }
42  bool HasNUW = false, bool HasNSW = false) const {
43  return ConstantExpr::getSub(LHS, RHS, HasNUW, HasNSW);
44  }
45  Constant *CreateFSub(Constant *LHS, Constant *RHS) const {
46  return ConstantExpr::getFSub(LHS, RHS);
47  }
49  bool HasNUW = false, bool HasNSW = false) const {
50  return ConstantExpr::getMul(LHS, RHS, HasNUW, HasNSW);
51  }
52  Constant *CreateFMul(Constant *LHS, Constant *RHS) const {
53  return ConstantExpr::getFMul(LHS, RHS);
54  }
56  bool isExact = false) const {
57  return ConstantExpr::getUDiv(LHS, RHS, isExact);
58  }
60  bool isExact = false) const {
61  return ConstantExpr::getSDiv(LHS, RHS, isExact);
62  }
63  Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
64  return ConstantExpr::getFDiv(LHS, RHS);
65  }
66  Constant *CreateURem(Constant *LHS, Constant *RHS) const {
67  return ConstantExpr::getURem(LHS, RHS);
68  }
69  Constant *CreateSRem(Constant *LHS, Constant *RHS) const {
70  return ConstantExpr::getSRem(LHS, RHS);
71  }
72  Constant *CreateFRem(Constant *LHS, Constant *RHS) const {
73  return ConstantExpr::getFRem(LHS, RHS);
74  }
76  bool HasNUW = false, bool HasNSW = false) const {
77  return ConstantExpr::getShl(LHS, RHS, HasNUW, HasNSW);
78  }
80  bool isExact = false) const {
81  return ConstantExpr::getLShr(LHS, RHS, isExact);
82  }
84  bool isExact = false) const {
85  return ConstantExpr::getAShr(LHS, RHS, isExact);
86  }
87  Constant *CreateAnd(Constant *LHS, Constant *RHS) const {
88  return ConstantExpr::getAnd(LHS, RHS);
89  }
90  Constant *CreateOr(Constant *LHS, Constant *RHS) const {
91  return ConstantExpr::getOr(LHS, RHS);
92  }
93  Constant *CreateXor(Constant *LHS, Constant *RHS) const {
94  return ConstantExpr::getXor(LHS, RHS);
95  }
96 
98  Constant *LHS, Constant *RHS) const {
99  return ConstantExpr::get(Opc, LHS, RHS);
100  }
101 
102  //===--------------------------------------------------------------------===//
103  // Unary Operators
104  //===--------------------------------------------------------------------===//
105 
107  bool HasNUW = false, bool HasNSW = false) const {
108  return ConstantExpr::getNeg(C, HasNUW, HasNSW);
109  }
111  return ConstantExpr::getFNeg(C);
112  }
114  return ConstantExpr::getNot(C);
115  }
116 
117  //===--------------------------------------------------------------------===//
118  // Memory Instructions
119  //===--------------------------------------------------------------------===//
120 
122  ArrayRef<Constant *> IdxList) const {
123  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
124  }
126  // This form of the function only exists to avoid ambiguous overload
127  // warnings about whether to convert Idx to ArrayRef<Constant *> or
128  // ArrayRef<Value *>.
129  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
130  }
132  ArrayRef<Value *> IdxList) const {
133  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
134  }
135 
137  ArrayRef<Constant *> IdxList) const {
138  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
139  }
141  Constant *Idx) const {
142  // This form of the function only exists to avoid ambiguous overload
143  // warnings about whether to convert Idx to ArrayRef<Constant *> or
144  // ArrayRef<Value *>.
145  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
146  }
148  ArrayRef<Value *> IdxList) const {
149  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
150  }
151 
152  //===--------------------------------------------------------------------===//
153  // Cast/Conversion Operators
154  //===--------------------------------------------------------------------===//
155 
157  Type *DestTy) const {
158  return ConstantExpr::getCast(Op, C, DestTy);
159  }
161  return ConstantExpr::getPointerCast(C, DestTy);
162  }
163 
165  Type *DestTy) const {
167  }
168 
170  bool isSigned) const {
171  return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
172  }
173  Constant *CreateFPCast(Constant *C, Type *DestTy) const {
174  return ConstantExpr::getFPCast(C, DestTy);
175  }
176 
177  Constant *CreateBitCast(Constant *C, Type *DestTy) const {
178  return CreateCast(Instruction::BitCast, C, DestTy);
179  }
180  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const {
181  return CreateCast(Instruction::IntToPtr, C, DestTy);
182  }
183  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const {
184  return CreateCast(Instruction::PtrToInt, C, DestTy);
185  }
187  return ConstantExpr::getZExtOrBitCast(C, DestTy);
188  }
190  return ConstantExpr::getSExtOrBitCast(C, DestTy);
191  }
192 
194  return ConstantExpr::getTruncOrBitCast(C, DestTy);
195  }
196 
197  //===--------------------------------------------------------------------===//
198  // Compare Instructions
199  //===--------------------------------------------------------------------===//
200 
202  Constant *RHS) const {
203  return ConstantExpr::getCompare(P, LHS, RHS);
204  }
206  Constant *RHS) const {
207  return ConstantExpr::getCompare(P, LHS, RHS);
208  }
209 
210  //===--------------------------------------------------------------------===//
211  // Other Instructions
212  //===--------------------------------------------------------------------===//
213 
214  Constant *CreateSelect(Constant *C, Constant *True, Constant *False) const {
215  return ConstantExpr::getSelect(C, True, False);
216  }
217 
219  return ConstantExpr::getExtractElement(Vec, Idx);
220  }
221 
223  Constant *Idx) const {
224  return ConstantExpr::getInsertElement(Vec, NewElt, Idx);
225  }
226 
228  Constant *Mask) const {
229  return ConstantExpr::getShuffleVector(V1, V2, Mask);
230  }
231 
233  ArrayRef<unsigned> IdxList) const {
234  return ConstantExpr::getExtractValue(Agg, IdxList);
235  }
236 
238  ArrayRef<unsigned> IdxList) const {
239  return ConstantExpr::getInsertValue(Agg, Val, IdxList);
240  }
241 };
242 
243 }
244 
245 #endif
static Constant * getFAdd(Constant *C1, Constant *C2)
Definition: Constants.cpp:2265
Constant * CreateBitCast(Constant *C, Type *DestTy) 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:1663
Constant * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
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 * CreateFAdd(Constant *LHS, Constant *RHS) const
Constant * CreateSelect(Constant *C, Constant *True, Constant *False) 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:1990
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
Constant * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2145
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 * CreateOr(Constant *LHS, Constant *RHS) const
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Constant * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:1674
Constant * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
static Constant * getLShr(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2336
Constant * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Constant * CreateExtractElement(Constant *Vec, Constant *Idx) const
Constant * CreateFRem(Constant *LHS, Constant *RHS) 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:1868
Constant * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:1687
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
ConstantFolder - Create constants with minimum, target independent, folding.
static Constant * getUDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2291
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
static Constant * getFDiv(Constant *C1, Constant *C2)
Definition: Constants.cpp:2301
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
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 * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Constant * CreateNot(Constant *C) const
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
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1636
Constant * CreateSRem(Constant *LHS, Constant *RHS) const
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2168
Constant * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
static Constant * getNot(Constant *C)
Definition: Constants.cpp:2252
Constant * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1648
Constant * CreateFMul(Constant *LHS, Constant *RHS) const
Constant * CreateAnd(Constant *LHS, Constant *RHS) const
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Constant * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
static Constant * getSDiv(Constant *C1, Constant *C2, bool isExact=false)
Definition: Constants.cpp:2296
Constant * CreateURem(Constant *LHS, Constant *RHS) const
Constant * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
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
Constant * CreatePointerCast(Constant *C, Type *DestTy) const
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const
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 * CreateFPCast(Constant *C, Type *DestTy) const
Constant * CreateFNeg(Constant *C) const
static Constant * getOr(Constant *C1, Constant *C2)
Definition: Constants.cpp:2321
Constant * CreateFDiv(Constant *LHS, Constant *RHS) const
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2329
Constant * CreateXor(Constant *LHS, Constant *RHS) const
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
static Constant * getSRem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2309
Constant * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
static Constant * getURem(Constant *C1, Constant *C2)
Definition: Constants.cpp:2305
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2215
Constant * CreateFSub(Constant *LHS, Constant *RHS) const
Constant * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
static Constant * getMul(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2280
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const
Constant * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
Constant * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
static Constant * getXor(Constant *C1, Constant *C2)
Definition: Constants.cpp:2325