LLVM  3.7.0
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/Instructions.h"
28 
29 namespace llvm {
30 
31 /// NoFolder - Create "constants" (actually, instructions) with no folding.
32 class NoFolder {
33 public:
34  explicit NoFolder() {}
35 
36  //===--------------------------------------------------------------------===//
37  // Binary Operators
38  //===--------------------------------------------------------------------===//
39 
41  bool HasNUW = false, bool HasNSW = false) const {
43  if (HasNUW) BO->setHasNoUnsignedWrap();
44  if (HasNSW) BO->setHasNoSignedWrap();
45  return BO;
46  }
48  return BinaryOperator::CreateNSWAdd(LHS, RHS);
49  }
51  return BinaryOperator::CreateNUWAdd(LHS, RHS);
52  }
53  Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const {
54  return BinaryOperator::CreateFAdd(LHS, RHS);
55  }
57  bool HasNUW = false, bool HasNSW = false) const {
58  BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
59  if (HasNUW) BO->setHasNoUnsignedWrap();
60  if (HasNSW) BO->setHasNoSignedWrap();
61  return BO;
62  }
64  return BinaryOperator::CreateNSWSub(LHS, RHS);
65  }
67  return BinaryOperator::CreateNUWSub(LHS, RHS);
68  }
69  Instruction *CreateFSub(Constant *LHS, Constant *RHS) const {
70  return BinaryOperator::CreateFSub(LHS, RHS);
71  }
73  bool HasNUW = false, bool HasNSW = false) const {
75  if (HasNUW) BO->setHasNoUnsignedWrap();
76  if (HasNSW) BO->setHasNoSignedWrap();
77  return BO;
78  }
80  return BinaryOperator::CreateNSWMul(LHS, RHS);
81  }
83  return BinaryOperator::CreateNUWMul(LHS, RHS);
84  }
85  Instruction *CreateFMul(Constant *LHS, Constant *RHS) const {
86  return BinaryOperator::CreateFMul(LHS, RHS);
87  }
89  bool isExact = false) const {
90  if (!isExact)
91  return BinaryOperator::CreateUDiv(LHS, RHS);
92  return BinaryOperator::CreateExactUDiv(LHS, RHS);
93  }
95  return BinaryOperator::CreateExactUDiv(LHS, RHS);
96  }
98  bool isExact = false) const {
99  if (!isExact)
100  return BinaryOperator::CreateSDiv(LHS, RHS);
101  return BinaryOperator::CreateExactSDiv(LHS, RHS);
102  }
104  return BinaryOperator::CreateExactSDiv(LHS, RHS);
105  }
107  return BinaryOperator::CreateFDiv(LHS, RHS);
108  }
110  return BinaryOperator::CreateURem(LHS, RHS);
111  }
113  return BinaryOperator::CreateSRem(LHS, RHS);
114  }
116  return BinaryOperator::CreateFRem(LHS, RHS);
117  }
118  Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
119  bool HasNSW = false) const {
120  BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
121  if (HasNUW) BO->setHasNoUnsignedWrap();
122  if (HasNSW) BO->setHasNoSignedWrap();
123  return BO;
124  }
126  bool isExact = false) const {
127  if (!isExact)
128  return BinaryOperator::CreateLShr(LHS, RHS);
129  return BinaryOperator::CreateExactLShr(LHS, RHS);
130  }
132  bool isExact = false) const {
133  if (!isExact)
134  return BinaryOperator::CreateAShr(LHS, RHS);
135  return BinaryOperator::CreateExactAShr(LHS, RHS);
136  }
137  Instruction *CreateAnd(Constant *LHS, Constant *RHS) const {
138  return BinaryOperator::CreateAnd(LHS, RHS);
139  }
140  Instruction *CreateOr(Constant *LHS, Constant *RHS) const {
141  return BinaryOperator::CreateOr(LHS, RHS);
142  }
143  Instruction *CreateXor(Constant *LHS, Constant *RHS) const {
144  return BinaryOperator::CreateXor(LHS, RHS);
145  }
146 
148  Constant *LHS, Constant *RHS) const {
149  return BinaryOperator::Create(Opc, LHS, RHS);
150  }
151 
152  //===--------------------------------------------------------------------===//
153  // Unary Operators
154  //===--------------------------------------------------------------------===//
155 
157  bool HasNUW = false, bool HasNSW = false) const {
159  if (HasNUW) BO->setHasNoUnsignedWrap();
160  if (HasNSW) BO->setHasNoSignedWrap();
161  return BO;
162  }
165  }
168  }
170  return BinaryOperator::CreateFNeg(C);
171  }
173  return BinaryOperator::CreateNot(C);
174  }
175 
176  //===--------------------------------------------------------------------===//
177  // Memory Instructions
178  //===--------------------------------------------------------------------===//
179 
181  ArrayRef<Constant *> IdxList) const {
182  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
183  }
185  // This form of the function only exists to avoid ambiguous overload
186  // warnings about whether to convert Idx to ArrayRef<Constant *> or
187  // ArrayRef<Value *>.
188  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
189  }
191  ArrayRef<Value *> IdxList) const {
192  return GetElementPtrInst::Create(Ty, C, IdxList);
193  }
194 
196  ArrayRef<Constant *> IdxList) const {
197  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
198  }
200  Constant *Idx) const {
201  // This form of the function only exists to avoid ambiguous overload
202  // warnings about whether to convert Idx to ArrayRef<Constant *> or
203  // ArrayRef<Value *>.
204  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
205  }
207  ArrayRef<Value *> IdxList) const {
208  return GetElementPtrInst::CreateInBounds(Ty, C, IdxList);
209  }
210 
211  //===--------------------------------------------------------------------===//
212  // Cast/Conversion Operators
213  //===--------------------------------------------------------------------===//
214 
216  Type *DestTy) const {
217  return CastInst::Create(Op, C, DestTy);
218  }
220  return CastInst::CreatePointerCast(C, DestTy);
221  }
223  bool isSigned) const {
224  return CastInst::CreateIntegerCast(C, DestTy, isSigned);
225  }
226  Instruction *CreateFPCast(Constant *C, Type *DestTy) const {
227  return CastInst::CreateFPCast(C, DestTy);
228  }
229 
231  return CreateCast(Instruction::BitCast, C, DestTy);
232  }
234  return CreateCast(Instruction::IntToPtr, C, DestTy);
235  }
237  return CreateCast(Instruction::PtrToInt, C, DestTy);
238  }
240  return CastInst::CreateZExtOrBitCast(C, DestTy);
241  }
243  return CastInst::CreateSExtOrBitCast(C, DestTy);
244  }
245 
247  return CastInst::CreateTruncOrBitCast(C, DestTy);
248  }
249 
250  //===--------------------------------------------------------------------===//
251  // Compare Instructions
252  //===--------------------------------------------------------------------===//
253 
255  Constant *LHS, Constant *RHS) const {
256  return new ICmpInst(P, LHS, RHS);
257  }
259  Constant *LHS, Constant *RHS) const {
260  return new FCmpInst(P, LHS, RHS);
261  }
262 
263  //===--------------------------------------------------------------------===//
264  // Other Instructions
265  //===--------------------------------------------------------------------===//
266 
268  Constant *True, Constant *False) const {
269  return SelectInst::Create(C, True, False);
270  }
271 
273  return ExtractElementInst::Create(Vec, Idx);
274  }
275 
277  Constant *Idx) const {
278  return InsertElementInst::Create(Vec, NewElt, Idx);
279  }
280 
282  Constant *Mask) const {
283  return new ShuffleVectorInst(V1, V2, Mask);
284  }
285 
287  ArrayRef<unsigned> IdxList) const {
288  return ExtractValueInst::Create(Agg, IdxList);
289  }
290 
292  ArrayRef<unsigned> IdxList) const {
293  return InsertValueInst::Create(Agg, Val, IdxList);
294  }
295 };
296 
297 }
298 
299 #endif
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Instruction * CreateNUWMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:82
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateNUWSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:66
Instruction * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:254
Instruction * CreateIntToPtr(Constant *C, Type *DestTy) const
Definition: NoFolder.h:233
Instruction * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:97
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1092
ShuffleVectorInst - This instruction constructs a fixed permutation of two input vectors.
Instruction * CreateNSWSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:63
Instruction * CreateBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:230
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
Definition: NoFolder.h:206
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: NoFolder.h:184
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Instruction * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const
Definition: NoFolder.h:276
Instruction * CreateNSWNeg(Constant *C) const
Definition: NoFolder.h:163
Instruction * CreateFRem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:115
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:887
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
Definition: NoFolder.h:195
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const
Definition: NoFolder.h:215
Instruction * CreateExactUDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:94
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: ArrayRef.h:31
Instruction * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:131
Instruction * CreateFSub(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:69
Instruction * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:258
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:32
Instruction * CreateFPCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:226
Instruction * CreateNUWNeg(Constant *C) const
Definition: NoFolder.h:166
Instruction * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const
Definition: NoFolder.h:291
#define P(N)
static BinaryOperator * CreateNSWNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateNSWMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:79
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1115
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Instruction * CreateNot(Constant *C) const
Definition: NoFolder.h:172
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:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Instruction * CreateOr(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:140
Instruction * CreateNSWAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:47
Instruction * CreateAnd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:137
Instruction * CreatePointerCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:219
Instruction * CreateShuffleVector(Constant *V1, Constant *V2, Constant *Mask) const
Definition: NoFolder.h:281
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:697
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...
Instruction * CreateSRem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:112
Instruction * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:40
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:854
static BinaryOperator * CreateNUWNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
static BinaryOperator * CreateFNeg(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Instruction * CreateTruncOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:246
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 * CreateExactSDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:103
Instruction * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:88
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Instruction * CreatePtrToInt(Constant *C, Type *DestTy) const
Definition: NoFolder.h:236
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const
Definition: NoFolder.h:199
Instruction * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const
Definition: NoFolder.h:222
Instruction * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const
Definition: NoFolder.h:125
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 * CreateZExtOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:239
Instruction * CreateNUWAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:50
Instruction * CreateFAdd(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:53
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's ...
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const
Definition: NoFolder.h:180
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
Instruction * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:147
Instruction * CreateFNeg(Constant *C) const
Definition: NoFolder.h:169
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const
Definition: NoFolder.h:190
Instruction * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:72
Instruction * CreateXor(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:143
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
Instruction * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:156
Instruction * CreateURem(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:109
Instruction * CreateFMul(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:85
Instruction * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:118
Instruction * CreateSExtOrBitCast(Constant *C, Type *DestTy) const
Definition: NoFolder.h:242
Instruction * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const
Definition: NoFolder.h:56
Instruction * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const
Definition: NoFolder.h:286
Instruction * CreateFDiv(Constant *LHS, Constant *RHS) const
Definition: NoFolder.h:106
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 * CreateSelect(Constant *C, Constant *True, Constant *False) const
Definition: NoFolder.h:267
Instruction * CreateExtractElement(Constant *Vec, Constant *Idx) const
Definition: NoFolder.h:272