LLVM  15.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  // Value-based folders.
42  //
43  // Return an existing value or a constant if the operation can be simplified.
44  // Otherwise return nullptr.
45  //===--------------------------------------------------------------------===//
46  Value *FoldAdd(Value *LHS, Value *RHS, bool HasNUW = false,
47  bool HasNSW = false) const override {
48  return nullptr;
49  }
50 
51  Value *FoldAnd(Value *LHS, Value *RHS) const override { return nullptr; }
52 
53  Value *FoldOr(Value *LHS, Value *RHS) const override { return nullptr; }
54 
56  return nullptr;
57  }
58 
59  Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
60  bool IsInBounds = false) const override {
61  return nullptr;
62  }
63 
64  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
65  return nullptr;
66  }
67 
69  ArrayRef<unsigned> IdxList) const override {
70  return nullptr;
71  }
72 
74  ArrayRef<unsigned> IdxList) const override {
75  return nullptr;
76  }
77 
78  //===--------------------------------------------------------------------===//
79  // Binary Operators
80  //===--------------------------------------------------------------------===//
81 
83  return BinaryOperator::CreateFAdd(LHS, RHS);
84  }
85 
87  bool HasNUW = false,
88  bool HasNSW = false) const override {
89  BinaryOperator *BO = BinaryOperator::CreateSub(LHS, RHS);
90  if (HasNUW) BO->setHasNoUnsignedWrap();
91  if (HasNSW) BO->setHasNoSignedWrap();
92  return BO;
93  }
94 
96  return BinaryOperator::CreateFSub(LHS, RHS);
97  }
98 
100  bool HasNUW = false,
101  bool HasNSW = false) const override {
103  if (HasNUW) BO->setHasNoUnsignedWrap();
104  if (HasNSW) BO->setHasNoSignedWrap();
105  return BO;
106  }
107 
109  return BinaryOperator::CreateFMul(LHS, RHS);
110  }
111 
113  bool isExact = false) const override {
114  if (!isExact)
115  return BinaryOperator::CreateUDiv(LHS, RHS);
116  return BinaryOperator::CreateExactUDiv(LHS, RHS);
117  }
118 
120  bool isExact = false) const override {
121  if (!isExact)
122  return BinaryOperator::CreateSDiv(LHS, RHS);
123  return BinaryOperator::CreateExactSDiv(LHS, RHS);
124  }
125 
127  return BinaryOperator::CreateFDiv(LHS, RHS);
128  }
129 
131  return BinaryOperator::CreateURem(LHS, RHS);
132  }
133 
135  return BinaryOperator::CreateSRem(LHS, RHS);
136  }
137 
139  return BinaryOperator::CreateFRem(LHS, RHS);
140  }
141 
142  Instruction *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
143  bool HasNSW = false) const override {
144  BinaryOperator *BO = BinaryOperator::CreateShl(LHS, RHS);
145  if (HasNUW) BO->setHasNoUnsignedWrap();
146  if (HasNSW) BO->setHasNoSignedWrap();
147  return BO;
148  }
149 
151  bool isExact = false) const override {
152  if (!isExact)
153  return BinaryOperator::CreateLShr(LHS, RHS);
154  return BinaryOperator::CreateExactLShr(LHS, RHS);
155  }
156 
158  bool isExact = false) const override {
159  if (!isExact)
160  return BinaryOperator::CreateAShr(LHS, RHS);
161  return BinaryOperator::CreateExactAShr(LHS, RHS);
162  }
163 
165  return BinaryOperator::CreateXor(LHS, RHS);
166  }
167 
169  Constant *LHS, Constant *RHS) const override {
170  return BinaryOperator::Create(Opc, LHS, RHS);
171  }
172 
173  //===--------------------------------------------------------------------===//
174  // Unary Operators
175  //===--------------------------------------------------------------------===//
176 
178  bool HasNUW = false,
179  bool HasNSW = false) const override {
181  if (HasNUW) BO->setHasNoUnsignedWrap();
182  if (HasNSW) BO->setHasNoSignedWrap();
183  return BO;
184  }
185 
186  Instruction *CreateFNeg(Constant *C) const override {
187  return UnaryOperator::CreateFNeg(C);
188  }
189 
190  Instruction *CreateNot(Constant *C) const override {
192  }
193 
195  Constant *C) const override {
196  return UnaryOperator::Create(Opc, C);
197  }
198 
199  //===--------------------------------------------------------------------===//
200  // Cast/Conversion Operators
201  //===--------------------------------------------------------------------===//
202 
204  Type *DestTy) const override {
205  return CastInst::Create(Op, C, DestTy);
206  }
207 
208  Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
209  return CastInst::CreatePointerCast(C, DestTy);
210  }
211 
213  Constant *C, Type *DestTy) const override {
215  }
216 
218  bool isSigned) const override {
219  return CastInst::CreateIntegerCast(C, DestTy, isSigned);
220  }
221 
222  Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
223  return CastInst::CreateFPCast(C, DestTy);
224  }
225 
226  Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
227  return CreateCast(Instruction::BitCast, C, DestTy);
228  }
229 
230  Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
231  return CreateCast(Instruction::IntToPtr, C, DestTy);
232  }
233 
234  Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
235  return CreateCast(Instruction::PtrToInt, C, DestTy);
236  }
237 
238  Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
239  return CastInst::CreateZExtOrBitCast(C, DestTy);
240  }
241 
242  Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
243  return CastInst::CreateSExtOrBitCast(C, DestTy);
244  }
245 
246  Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
247  return CastInst::CreateTruncOrBitCast(C, DestTy);
248  }
249 
250  //===--------------------------------------------------------------------===//
251  // Compare Instructions
252  //===--------------------------------------------------------------------===//
253 
255  Constant *LHS, Constant *RHS) const override {
256  return new FCmpInst(P, LHS, RHS);
257  }
258 
259  //===--------------------------------------------------------------------===//
260  // Other Instructions
261  //===--------------------------------------------------------------------===//
262 
264  Constant *Idx) const override {
265  return ExtractElementInst::Create(Vec, Idx);
266  }
267 
269  Constant *Idx) const override {
270  return InsertElementInst::Create(Vec, NewElt, Idx);
271  }
272 
274  ArrayRef<int> Mask) const override {
275  return new ShuffleVectorInst(V1, V2, Mask);
276  }
277 };
278 
279 } // end namespace llvm
280 
281 #endif // LLVM_IR_NOFOLDER_H
llvm::NoFolder::FoldExtractValue
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:68
llvm::NoFolder::CreateIntCast
Instruction * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: NoFolder.h:217
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::CastInst::CreatePointerBitCastOrAddrSpaceCast
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast or an AddrSpaceCast cast instruction.
Definition: Instructions.cpp:3309
llvm::NoFolder::FoldAnd
Value * FoldAnd(Value *LHS, Value *RHS) const override
Definition: NoFolder.h:51
llvm::Instruction::UnaryOps
UnaryOps
Definition: Instruction.h:779
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:719
llvm::NoFolder::CreateSExtOrBitCast
Instruction * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:242
llvm::NoFolder::CreateSDiv
Instruction * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:119
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::BinaryOperator::CreateNot
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Definition: Instructions.cpp:2834
llvm::NoFolder::CreateSRem
Instruction * CreateSRem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:134
llvm::NoFolder::FoldGEP
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: NoFolder.h:59
llvm::IRBuilderFolder
IRBuilderFolder - Interface for constant folding in IRBuilder.
Definition: IRBuilderFolder.h:24
llvm::NoFolder::CreateTruncOrBitCast
Instruction * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:246
llvm::CastInst::Create
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 ...
Definition: Instructions.cpp:3180
llvm::NoFolder::CreateCast
Instruction * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: NoFolder.h:203
llvm::InsertElementInst::Create
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1950
llvm::ExtractElementInst::Create
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1885
llvm::NoFolder::CreateFPCast
Instruction * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:222
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Instruction::setHasNoUnsignedWrap
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag.
Definition: Instruction.cpp:123
llvm::NoFolder::CreateFRem
Instruction * CreateFRem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:138
llvm::NoFolder::CreateBinOp
Instruction * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:168
llvm::NoFolder::FoldICmp
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: NoFolder.h:55
llvm::CastInst::CreateIntegerCast
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.
Definition: Instructions.cpp:3346
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::NoFolder::CreatePtrToInt
Instruction * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:234
llvm::NoFolder::CreateIntToPtr
Instruction * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:230
llvm::BinaryOperator::CreateNeg
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...
Definition: Instructions.cpp:2794
llvm::NoFolder::FoldSelect
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: NoFolder.h:64
llvm::NoFolder::CreateZExtOrBitCast
Instruction * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:238
llvm::NoFolder::CreateMul
Instruction * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:99
llvm::NoFolder::CreateLShr
Instruction * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:150
llvm::NoFolder::CreateFSub
Instruction * CreateFSub(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:95
llvm::BitmaskEnumDetail::Mask
constexpr 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
Instruction.h
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::NoFolder::CreateFNeg
Instruction * CreateFNeg(Constant *C) const override
Definition: NoFolder.h:186
Constants.h
llvm::NoFolder::CreateFCmp
Instruction * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:254
llvm::Instruction::setHasNoSignedWrap
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag.
Definition: Instruction.cpp:127
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::NoFolder::CreateSub
Instruction * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:86
InstrTypes.h
llvm::FCmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition: Instructions.h:1360
llvm::CastInst::CreateFPCast
static CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
Definition: Instructions.cpp:3374
llvm::NoFolder::CreateBitCast
Instruction * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:226
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:800
llvm::NoFolder::CreateFDiv
Instruction * CreateFDiv(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:126
llvm::Instruction
Definition: Instruction.h:42
llvm::CastInst::CreateSExtOrBitCast
static CastInst * CreateSExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a SExt or BitCast cast instruction.
Definition: Instructions.cpp:3240
llvm::CastInst::CreateTruncOrBitCast
static CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
Definition: Instructions.cpp:3256
llvm::NoFolder::CreateNeg
Instruction * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:177
llvm::NoFolder
NoFolder - Create "constants" (actually, instructions) with no folding.
Definition: NoFolder.h:34
llvm::NoFolder::CreateShl
Instruction * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:142
llvm::NoFolder::CreateXor
Instruction * CreateXor(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:164
llvm::NoFolder::CreateUDiv
Instruction * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:112
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::NoFolder::CreatePointerBitCastOrAddrSpaceCast
Instruction * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:212
ArrayRef.h
IRBuilderFolder.h
llvm::NoFolder::CreatePointerCast
Instruction * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: NoFolder.h:208
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::BinaryOperator
Definition: InstrTypes.h:188
llvm::NoFolder::CreateFMul
Instruction * CreateFMul(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:108
llvm::NoFolder::CreateAShr
Instruction * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: NoFolder.h:157
llvm::NoFolder::NoFolder
NoFolder()=default
llvm::NoFolder::FoldOr
Value * FoldOr(Value *LHS, Value *RHS) const override
Definition: NoFolder.h:53
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::NoFolder::FoldAdd
Value * FoldAdd(Value *LHS, Value *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: NoFolder.h:46
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:341
llvm::NoFolder::CreateFAdd
Instruction * CreateFAdd(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:82
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:786
llvm::ShuffleVectorInst
This instruction constructs a fixed permutation of two input vectors.
Definition: Instructions.h:2008
llvm::NoFolder::CreateExtractElement
Instruction * CreateExtractElement(Constant *Vec, Constant *Idx) const override
Definition: NoFolder.h:263
llvm::UnaryOperator::Create
static UnaryOperator * Create(UnaryOps Op, Value *S, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a unary instruction, given the opcode and an operand.
Definition: Instructions.cpp:2649
Instructions.h
CreateMul
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Definition: Reassociate.cpp:243
llvm::NoFolder::CreateShuffleVector
Instruction * CreateShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask) const override
Definition: NoFolder.h:273
llvm::CastInst::CreatePointerCast
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
Create a BitCast AddrSpaceCast, or a PtrToInt cast instruction.
Definition: Instructions.cpp:3272
llvm::CastInst::CreateZExtOrBitCast
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
Definition: Instructions.cpp:3224
llvm::NoFolder::FoldInsertValue
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: NoFolder.h:73
llvm::NoFolder::CreateInsertElement
Instruction * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const override
Definition: NoFolder.h:268
llvm::BinaryOperator::Create
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.
Definition: Instructions.cpp:2778
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::NoFolder::CreateUnOp
Instruction * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override
Definition: NoFolder.h:194
llvm::NoFolder::CreateURem
Instruction * CreateURem(Constant *LHS, Constant *RHS) const override
Definition: NoFolder.h:130
llvm::NoFolder::CreateNot
Instruction * CreateNot(Constant *C) const override
Definition: NoFolder.h:190