LLVM  6.0.0svn
NoFolder.h
Go to the documentation of this file.
1 //===- NoFolder.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 NoFolder class, a helper for IRBuilder. It provides
11 // IRBuilder with a set of methods for creating unfolded constants. This is
12 // useful for learners trying to understand how LLVM IR works, and who don't
13 // want details to be hidden by the constant folder. For general constant
14 // creation and folding, use ConstantExpr and the routines in
15 // llvm/Analysis/ConstantFolding.h.
16 //
17 // Note: since it is not actually possible to create unfolded constants, this
18 // class returns instructions rather than constants.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #ifndef LLVM_IR_NOFOLDER_H
23 #define LLVM_IR_NOFOLDER_H
24 
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h"
30 
31 namespace llvm {
32 
33 /// NoFolder - Create "constants" (actually, instructions) with no folding.
34 class NoFolder {
35 public:
36  explicit NoFolder() = default;
37 
38  //===--------------------------------------------------------------------===//
39  // Binary Operators
40  //===--------------------------------------------------------------------===//
41 
43  bool HasNUW = false, bool HasNSW = false) const {
45  if (HasNUW) BO->setHasNoUnsignedWrap();
46  if (HasNSW) BO->setHasNoSignedWrap();
47  return BO;
48  }
49 
51  return BinaryOperator::CreateNSWAdd(LHS, RHS);
52  }
53 
55  return BinaryOperator::CreateNUWAdd(LHS, RHS);
56  }
57 
58  Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
59  return BinaryOperator::CreateFAdd(LHS, RHS);
60  }
61 
63  bool HasNUW = false, bool HasNSW = false) const {
64  BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
65  if (HasNUW) BO->setHasNoUnsignedWrap();
66  if (HasNSW) BO->setHasNoSignedWrap();
67  return BO;
68  }
69 
71  return BinaryOperator::CreateNSWSub(LHS, RHS);
72  }
73 
75  return BinaryOperator::CreateNUWSub(LHS, RHS);
76  }
77 
78  Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
79  return BinaryOperator::CreateFSub(LHS, RHS);
80  }
81 
83  bool HasNUW = false, bool HasNSW = false) const {
85  if (HasNUW) BO->setHasNoUnsignedWrap();
86  if (HasNSW) BO->setHasNoSignedWrap();
87  return BO;
88  }
89 
91  return BinaryOperator::CreateNSWMul(LHS, RHS);
92  }
93 
95  return BinaryOperator::CreateNUWMul(LHS, RHS);
96  }
97 
98  Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
99  return BinaryOperator::CreateFMul(LHS, RHS);
100  }
101 
103  bool isExact = false) const {
104  if (!isExact)
105  return BinaryOperator::CreateUDiv(LHS, RHS);
106  return BinaryOperator::CreateExactUDiv(LHS, RHS);
107  }
108 
110  return BinaryOperator::CreateExactUDiv(LHS, RHS);
111  }
112 
114  bool isExact = false) const {
115  if (!isExact)
116  return BinaryOperator::CreateSDiv(LHS, RHS);
117  return BinaryOperator::CreateExactSDiv(LHS, RHS);
118  }
119 
121  return BinaryOperator::CreateExactSDiv(LHS, RHS);
122  }
123 
125  return BinaryOperator::CreateFDiv(LHS, RHS);
126  }
127 
129  return BinaryOperator::CreateURem(LHS, RHS);
130  }
131 
133  return BinaryOperator::CreateSRem(LHS, RHS);
134  }
135 
137  return BinaryOperator::CreateFRem(LHS, RHS);
138  }
139 
140  Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
141  bool HasNSW = false) const {
142  BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
143  if (HasNUW) BO->setHasNoUnsignedWrap();
144  if (HasNSW) BO->setHasNoSignedWrap();
145  return BO;
146  }
147 
149  bool isExact = false) const {
150  if (!isExact)
151  return BinaryOperator::CreateLShr(LHS, RHS);
152  return BinaryOperator::CreateExactLShr(LHS, RHS);
153  }
154 
156  bool isExact = false) const {
157  if (!isExact)
158  return BinaryOperator::CreateAShr(LHS, RHS);
159  return BinaryOperator::CreateExactAShr(LHS, RHS);
160  }
161 
162  Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
163  return BinaryOperator::CreateAnd(LHS, RHS);
164  }
165 
166  Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
167  return BinaryOperator::CreateOr(LHS, RHS);
168  }
169 
170  Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
171  return BinaryOperator::CreateXor(LHS, RHS);
172  }
173 
175  Constant *LHS, Constant *RHS) const {
176  return BinaryOperator::Create(Opc, LHS, RHS);
177  }
178 
179  //===--------------------------------------------------------------------===//
180  // Unary Operators
181  //===--------------------------------------------------------------------===//
182 
184  bool HasNUW = false, bool HasNSW = false) const {
186  if (HasNUW) BO->setHasNoUnsignedWrap();
187  if (HasNSW) BO->setHasNoSignedWrap();
188  return BO;
189  }
190 
193  }
194 
197  }
198 
200  return BinaryOperator::CreateFNeg(C);
201  }
202 
204  return BinaryOperator::CreateNot(C);
205  }
206 
207  //===--------------------------------------------------------------------===//
208  // Memory Instructions
209  //===--------------------------------------------------------------------===//
210 
212  ArrayRef<Constant *> IdxList) const {
213  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
214  }
215 
217  // This form of the function only exists to avoid ambiguous overload
218  // warnings about whether to convert Idx to ArrayRef<Constant *> or
219  // ArrayRef<Value *>.
220  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
221  }
222 
224  ArrayRef<Value *> IdxList) const {
225  return GetElementPtrInst::Create(Ty, C, IdxList);
226  }
227 
229  ArrayRef<Constant *> IdxList) const {
230  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
231  }
232 
234  Constant *Idx) const {
235  // This form of the function only exists to avoid ambiguous overload
236  // warnings about whether to convert Idx to ArrayRef<Constant *> or
237  // ArrayRef<Value *>.
238  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
239  }
240 
242  ArrayRef<Value *> IdxList) const {
243  return GetElementPtrInst::CreateInBounds(Ty, C, IdxList);
244  }
245 
246  //===--------------------------------------------------------------------===//
247  // Cast/Conversion Operators
248  //===--------------------------------------------------------------------===//
249 
251  Type *DestTy) const {
252  return CastInst::Create(Op, C, DestTy);
253  }
254 
256  return CastInst::CreatePointerCast(C, DestTy);
257  }
258 
260  bool isSigned) const {
261  return CastInst::CreateIntegerCast(C, DestTy, isSigned);
262  }
263 
264  Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
265  return CastInst::CreateFPCast(C, DestTy);
266  }
267 
269  return CreateCast(Instruction::BitCast, C, DestTy);
270  }
271 
273  return CreateCast(Instruction::IntToPtr, C, DestTy);
274  }
275 
277  return CreateCast(Instruction::PtrToInt, C, DestTy);
278  }
279 
281  return CastInst::CreateZExtOrBitCast(C, DestTy);
282  }
283 
285  return CastInst::CreateSExtOrBitCast(C, DestTy);
286  }
287 
289  return CastInst::CreateTruncOrBitCast(C, DestTy);
290  }
291 
292  //===--------------------------------------------------------------------===//
293  // Compare Instructions
294  //===--------------------------------------------------------------------===//
295 
297  Constant *LHS, Constant *RHS) const {
298  return new ICmpInst(P, LHS, RHS);
299  }
300 
302  Constant *LHS, Constant *RHS) const {
303  return new FCmpInst(P, LHS, RHS);
304  }
305 
306  //===--------------------------------------------------------------------===//
307  // Other Instructions
308  //===--------------------------------------------------------------------===//
309 
311  Constant *True, Constant *False) const {
312  return SelectInst::Create(C, True, False);
313  }
314 
316  return ExtractElementInst::Create(Vec, Idx);
317  }
318 
320  Constant *Idx) const {
321  return InsertElementInst::Create(Vec, NewElt, Idx);
322  }
323 
325  Constant *Mask) const {
326  return new ShuffleVectorInst(V1, V2, Mask);
327  }
328 
330  ArrayRef<unsigned> IdxList) const {
331  return ExtractValueInst::Create(Agg, IdxList);
332  }
333 
335  ArrayRef<unsigned> IdxList) const {
336  return InsertValueInst::Create(Agg, Val, IdxList);
337  }
338 };
339 
340 } // end namespace llvm
341 
342 #endif // LLVM_IR_NOFOLDER_H
uint64_t CallInst * C
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: NoFolder.h:228
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateOr(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:166
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Instruction * CreateFRem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:136
Instruction * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Definition: NoFolder.h:324
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
Instruction * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:155
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:863
Instruction * CreateNUWAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:54
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Instruction * CreateNUWSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:74
Instruction * CreateFAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:58
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreateNSWAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:50
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList) const
Definition: NoFolder.h:211
Instruction * CreateXor(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:170
Instruction * CreateNSWSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:70
Instruction * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: NoFolder.h:223
Instruction * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value *> IdxList) const
Definition: NoFolder.h:241
Instruction * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Definition: NoFolder.h:250
Instruction * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Definition: NoFolder.h:319
Instruction * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:102
Instruction * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:284
Instruction * CreateExactUDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:109
This instruction compares its operands according to the predicate given to the constructor.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Instruction * CreateURem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:128
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
NoFolder - Create "constants" (actually, instructions) with no folding.
Definition: NoFolder.h:34
Instruction * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:148
Instruction * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:174
#define P(N)
static BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateNot(Constant *C) const
Definition: NoFolder.h:203
Instruction * CreateExtractElement(Constant *Vec, Constant *Idx) const
Definition: NoFolder.h:315
Instruction * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
Definition: NoFolder.h:334
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is an important base class in LLVM.
Definition: Constant.h:42
NoFolder()=default
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Instruction * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Definition: NoFolder.h:259
Instruction * CreatePtrToInt(Constant *C, Type *DestTy) const
Definition: NoFolder.h:276
Instruction * CreateExactSDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:120
Instruction * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:113
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:860
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
NUW NUW NUW NUW Exact static Exact BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
Instruction * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:296
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: NoFolder.h:233
Instruction * CreateFPCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:264
static BinaryOperator * CreateNUWNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateFMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:98
Instruction * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:280
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
Instruction * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
Definition: NoFolder.h:329
Instruction * CreateNSWNeg(Constant *C) const
Definition: NoFolder.h:191
Instruction * CreatePointerCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:255
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: NoFolder.h:216
Instruction * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:140
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Instruction * CreateIntToPtr(Constant *C, Type *DestTy) const
Definition: NoFolder.h:272
Instruction * CreateSRem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:132
Instruction * CreateBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:268
Instruction * CreateFSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:78
Instruction * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:62
Instruction * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:82
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Instruction * CreateSelect(Constant *C, Constant *True, Constant *False) const
Definition: NoFolder.h:310
Instruction * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:301
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass&#39;s ...
Instruction * CreateNUWMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:94
Instruction * CreateFDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:124
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
Instruction * CreateNSWMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:90
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1142
Instruction * CreateNUWNeg(Constant *C) const
Definition: NoFolder.h:195
Instruction * CreateAnd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:162
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Definition: Instruction.cpp:98
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:897
Instruction * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:288
Instruction * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:183
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
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Instruction * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:42
Instruction * CreateFNeg(Constant *C) const
Definition: NoFolder.h:199