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