LLVM  12.0.0git
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"
30 
31 namespace llvm {
32 
33 /// NoFolder - Create "constants" (actually, instructions) with no folding.
34 class NoFolder final : public IRBuilderFolder {
35  virtual void anchor();
36 
37 public:
38  explicit NoFolder() = default;
39 
40  //===--------------------------------------------------------------------===//
41  // Binary Operators
42  //===--------------------------------------------------------------------===//
43 
45  bool HasNUW = false,
46  bool HasNSW = false) const override {
48  if (HasNUW) BO->setHasNoUnsignedWrap();
49  if (HasNSW) BO->setHasNoSignedWrap();
50  return BO;
51  }
52 
53  Instruction *CreateFAdd(Constant *LHS, Constant *RHS) const override {
54  return BinaryOperator::CreateFAdd(LHS, RHS);
55  }
56 
58  bool HasNUW = false,
59  bool HasNSW = false) const override {
60  BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
61  if (HasNUW) BO->setHasNoUnsignedWrap();
62  if (HasNSW) BO->setHasNoSignedWrap();
63  return BO;
64  }
65 
66  Instruction *CreateFSub(Constant *LHS, Constant *RHS) const override {
67  return BinaryOperator::CreateFSub(LHS, RHS);
68  }
69 
71  bool HasNUW = false,
72  bool HasNSW = false) const override {
74  if (HasNUW) BO->setHasNoUnsignedWrap();
75  if (HasNSW) BO->setHasNoSignedWrap();
76  return BO;
77  }
78 
79  Instruction *CreateFMul(Constant *LHS, Constant *RHS) const override {
80  return BinaryOperator::CreateFMul(LHS, RHS);
81  }
82 
84  bool isExact = false) const override {
85  if (!isExact)
86  return BinaryOperator::CreateUDiv(LHS, RHS);
87  return BinaryOperator::CreateExactUDiv(LHS, RHS);
88  }
89 
91  bool isExact = false) const override {
92  if (!isExact)
93  return BinaryOperator::CreateSDiv(LHS, RHS);
94  return BinaryOperator::CreateExactSDiv(LHS, RHS);
95  }
96 
97  Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const override {
98  return BinaryOperator::CreateFDiv(LHS, RHS);
99  }
100 
101  Instruction *CreateURem(Constant *LHS, Constant *RHS) const override {
102  return BinaryOperator::CreateURem(LHS, RHS);
103  }
104 
105  Instruction *CreateSRem(Constant *LHS, Constant *RHS) const override {
106  return BinaryOperator::CreateSRem(LHS, RHS);
107  }
108 
109  Instruction *CreateFRem(Constant *LHS, Constant *RHS) const override {
110  return BinaryOperator::CreateFRem(LHS, RHS);
111  }
112 
113  Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
114  bool HasNSW = false) const override {
115  BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
116  if (HasNUW) BO->setHasNoUnsignedWrap();
117  if (HasNSW) BO->setHasNoSignedWrap();
118  return BO;
119  }
120 
122  bool isExact = false) const override {
123  if (!isExact)
124  return BinaryOperator::CreateLShr(LHS, RHS);
125  return BinaryOperator::CreateExactLShr(LHS, RHS);
126  }
127 
129  bool isExact = false) const override {
130  if (!isExact)
131  return BinaryOperator::CreateAShr(LHS, RHS);
132  return BinaryOperator::CreateExactAShr(LHS, RHS);
133  }
134 
135  Instruction *CreateAnd(Constant *LHS, Constant *RHS) const override {
136  return BinaryOperator::CreateAnd(LHS, RHS);
137  }
138 
139  Instruction *CreateOr(Constant *LHS, Constant *RHS) const override {
140  return BinaryOperator::CreateOr(LHS, RHS);
141  }
142 
143  Instruction *CreateXor(Constant *LHS, Constant *RHS) const override {
144  return BinaryOperator::CreateXor(LHS, RHS);
145  }
146 
148  Constant *LHS, Constant *RHS) const override {
149  return BinaryOperator::Create(Opc, LHS, RHS);
150  }
151 
152  //===--------------------------------------------------------------------===//
153  // Unary Operators
154  //===--------------------------------------------------------------------===//
155 
157  bool HasNUW = false,
158  bool HasNSW = false) const override {
160  if (HasNUW) BO->setHasNoUnsignedWrap();
161  if (HasNSW) BO->setHasNoSignedWrap();
162  return BO;
163  }
164 
165  Instruction *CreateFNeg(Constant *C) const override {
166  return UnaryOperator::CreateFNeg(C);
167  }
168 
169  Instruction *CreateNot(Constant *C) const override {
171  }
172 
174  Constant *C) const override {
175  return UnaryOperator::Create(Opc, C);
176  }
177 
178  //===--------------------------------------------------------------------===//
179  // Memory Instructions
180  //===--------------------------------------------------------------------===//
181 
183  ArrayRef<Constant *> IdxList) const override {
184  return ConstantExpr::getGetElementPtr(Ty, C, IdxList);
185  }
186 
188  Constant *Idx) const override {
189  // This form of the function only exists to avoid ambiguous overload
190  // warnings about whether to convert Idx to ArrayRef<Constant *> or
191  // ArrayRef<Value *>.
192  return ConstantExpr::getGetElementPtr(Ty, C, Idx);
193  }
194 
196  ArrayRef<Value *> IdxList) const override {
197  return GetElementPtrInst::Create(Ty, C, IdxList);
198  }
199 
201  Type *Ty, Constant *C, ArrayRef<Constant *> IdxList) const override {
202  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, IdxList);
203  }
204 
206  Constant *Idx) const override {
207  // This form of the function only exists to avoid ambiguous overload
208  // warnings about whether to convert Idx to ArrayRef<Constant *> or
209  // ArrayRef<Value *>.
210  return ConstantExpr::getInBoundsGetElementPtr(Ty, C, Idx);
211  }
212 
214  Type *Ty, Constant *C, ArrayRef<Value *> IdxList) const override {
215  return GetElementPtrInst::CreateInBounds(Ty, C, IdxList);
216  }
217 
218  //===--------------------------------------------------------------------===//
219  // Cast/Conversion Operators
220  //===--------------------------------------------------------------------===//
221 
223  Type *DestTy) const override {
224  return CastInst::Create(Op, C, DestTy);
225  }
226 
227  Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
228  return CastInst::CreatePointerCast(C, DestTy);
229  }
230 
232  Constant *C, Type *DestTy) const override {
234  }
235 
237  bool isSigned) const override {
238  return CastInst::CreateIntegerCast(C, DestTy, isSigned);
239  }
240 
241  Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
242  return CastInst::CreateFPCast(C, DestTy);
243  }
244 
245  Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
246  return CreateCast(Instruction::BitCast, C, DestTy);
247  }
248 
249  Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
250  return CreateCast(Instruction::IntToPtr, C, DestTy);
251  }
252 
253  Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
254  return CreateCast(Instruction::PtrToInt, C, DestTy);
255  }
256 
257  Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
258  return CastInst::CreateZExtOrBitCast(C, DestTy);
259  }
260 
261  Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
262  return CastInst::CreateSExtOrBitCast(C, DestTy);
263  }
264 
265  Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
266  return CastInst::CreateTruncOrBitCast(C, DestTy);
267  }
268 
269  //===--------------------------------------------------------------------===//
270  // Compare Instructions
271  //===--------------------------------------------------------------------===//
272 
274  Constant *LHS, Constant *RHS) const override {
275  return new ICmpInst(P, LHS, RHS);
276  }
277 
279  Constant *LHS, Constant *RHS) const override {
280  return new FCmpInst(P, LHS, RHS);
281  }
282 
283  //===--------------------------------------------------------------------===//
284  // Other Instructions
285  //===--------------------------------------------------------------------===//
286 
288  Constant *True, Constant *False) const override {
289  return SelectInst::Create(C, True, False);
290  }
291 
293  Constant *Idx) const override {
294  return ExtractElementInst::Create(Vec, Idx);
295  }
296 
298  Constant *Idx) const override {
299  return InsertElementInst::Create(Vec, NewElt, Idx);
300  }
301 
303  ArrayRef<int> Mask) const override {
304  return new ShuffleVectorInst(V1, V2, Mask);
305  }
306 
308  ArrayRef<unsigned> IdxList) const override {
309  return ExtractValueInst::Create(Agg, IdxList);
310  }
311 
313  ArrayRef<unsigned> IdxList) const override {
314  return InsertValueInst::Create(Agg, Val, IdxList);
315  }
316 };
317 
318 } // end namespace llvm
319 
320 #endif // LLVM_IR_NOFOLDER_H
uint64_t CallInst * C
Instruction * CreateExtractValue(Constant *Agg, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:307
Instruction * CreateICmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:273
Instruction * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:90
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const override
Definition: NoFolder.h:200
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:1210
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 * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:83
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:722
Instruction * CreateFAdd(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:53
Instruction * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:249
Instruction * CreateXor(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:143
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreateShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask) const override
Definition: NoFolder.h:302
Instruction * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override
Definition: NoFolder.h:173
Instruction * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:227
Instruction * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:70
Instruction * CreateAnd(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:135
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:80
static GetElementPtrInst * CreateInBounds(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:965
Instruction * CreateFDiv(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:97
This instruction compares its operands according to the predicate given to the constructor.
Instruction * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:253
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
Instruction * CreateOr(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:139
Instruction * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: NoFolder.h:222
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Instruction * CreateNot(Constant *C) const override
Definition: NoFolder.h:169
NoFolder - Create "constants" (actually, instructions) with no folding.
Definition: NoFolder.h:34
Instruction * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:121
Instruction * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:147
#define P(N)
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1237
Instruction * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:113
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
Constant * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList) const override
Definition: NoFolder.h:182
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
Constant * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const override
Definition: NoFolder.h:205
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Instruction * CreateFNeg(Constant *C) const override
Definition: NoFolder.h:165
This instruction compares its operands according to the predicate given to the constructor.
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
Instruction * CreateInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:312
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 * CreateFRem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:109
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 * CreateFMul(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:79
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:931
IRBuilderFolder - Interface for constant folding in IRBuilder.
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 * CreateFSub(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:66
Instruction * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const override
Definition: NoFolder.h:297
Instruction * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: NoFolder.h:236
Instruction * CreateInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const override
Definition: NoFolder.h:213
Instruction * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:57
Instruction * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:156
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Instruction * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:245
Constant * CreateGetElementPtr(Type *Ty, Constant *C, Constant *Idx) const override
Definition: NoFolder.h:187
Instruction * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:128
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 * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:265
Instruction * CreateSelect(Constant *C, Constant *True, Constant *False) const override
Definition: NoFolder.h:287
Instruction * CreateURem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:101
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 ...
Instruction * CreateExtractElement(Constant *Vec, Constant *Idx) const override
Definition: NoFolder.h:292
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
Instruction * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:261
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Instruction * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:231
Instruction * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:241
Instruction * CreateSRem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:105
Instruction * CreateAdd(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:44
Instruction * CreateGetElementPtr(Type *Ty, Constant *C, ArrayRef< Value * > IdxList) const override
Definition: NoFolder.h:195
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.
Instruction * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:257
Instruction * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:278
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)