LLVM  15.0.0git
ConstantFolder.h
Go to the documentation of this file.
1 //===- ConstantFolder.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 ConstantFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants
11 // with minimal folding. For general constant creation and folding,
12 // use ConstantExpr and the routines in llvm/Analysis/ConstantFolding.h.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_IR_CONSTANTFOLDER_H
17 #define LLVM_IR_CONSTANTFOLDER_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/ConstantFold.h"
24 #include "llvm/IR/Instruction.h"
25 #include "llvm/IR/Operator.h"
26 
27 namespace llvm {
28 
29 /// ConstantFolder - Create constants with minimum, target independent, folding.
30 class ConstantFolder final : public IRBuilderFolder {
31  virtual void anchor();
32 
33 public:
34  explicit ConstantFolder() = default;
35 
36  //===--------------------------------------------------------------------===//
37  // Value-based folders.
38  //
39  // Return an existing value or a constant if the operation can be simplified.
40  // Otherwise return nullptr.
41  //===--------------------------------------------------------------------===//
42 
44  Value *RHS) const override {
45  auto *LC = dyn_cast<Constant>(LHS);
46  auto *RC = dyn_cast<Constant>(RHS);
47  if (LC && RC) {
49  return ConstantExpr::get(Opc, LC, RC);
50  return ConstantFoldBinaryInstruction(Opc, LC, RC);
51  }
52  return nullptr;
53  }
54 
56  bool IsExact) const override {
57  auto *LC = dyn_cast<Constant>(LHS);
58  auto *RC = dyn_cast<Constant>(RHS);
59  if (LC && RC) {
61  return ConstantExpr::get(Opc, LC, RC,
62  IsExact ? PossiblyExactOperator::IsExact : 0);
63  return ConstantFoldBinaryInstruction(Opc, LC, RC);
64  }
65  return nullptr;
66  }
67 
69  bool HasNUW, bool HasNSW) const override {
70  auto *LC = dyn_cast<Constant>(LHS);
71  auto *RC = dyn_cast<Constant>(RHS);
72  if (LC && RC) {
74  unsigned Flags = 0;
75  if (HasNUW)
77  if (HasNSW)
79  return ConstantExpr::get(Opc, LC, RC, Flags);
80  }
81  return ConstantFoldBinaryInstruction(Opc, LC, RC);
82  }
83  return nullptr;
84  }
85 
87  FastMathFlags FMF) const override {
88  return FoldBinOp(Opc, LHS, RHS);
89  }
90 
92  auto *LC = dyn_cast<Constant>(LHS);
93  auto *RC = dyn_cast<Constant>(RHS);
94  if (LC && RC)
95  return ConstantExpr::getCompare(P, LC, RC);
96  return nullptr;
97  }
98 
99  Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
100  bool IsInBounds = false) const override {
101  if (auto *PC = dyn_cast<Constant>(Ptr)) {
102  // Every index must be constant.
103  if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
104  return nullptr;
105 
106  if (IsInBounds)
107  return ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList);
108  else
109  return ConstantExpr::getGetElementPtr(Ty, PC, IdxList);
110  }
111  return nullptr;
112  }
113 
114  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
115  auto *CC = dyn_cast<Constant>(C);
116  auto *TC = dyn_cast<Constant>(True);
117  auto *FC = dyn_cast<Constant>(False);
118  if (CC && TC && FC)
119  return ConstantExpr::getSelect(CC, TC, FC);
120  return nullptr;
121  }
122 
124  ArrayRef<unsigned> IdxList) const override {
125  if (auto *CAgg = dyn_cast<Constant>(Agg))
126  return ConstantFoldExtractValueInstruction(CAgg, IdxList);
127  return nullptr;
128  };
129 
131  ArrayRef<unsigned> IdxList) const override {
132  auto *CAgg = dyn_cast<Constant>(Agg);
133  auto *CVal = dyn_cast<Constant>(Val);
134  if (CAgg && CVal)
135  return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
136  return nullptr;
137  }
138 
139  Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
140  auto *CVec = dyn_cast<Constant>(Vec);
141  auto *CIdx = dyn_cast<Constant>(Idx);
142  if (CVec && CIdx)
143  return ConstantExpr::getExtractElement(CVec, CIdx);
144  return nullptr;
145  }
146 
148  Value *Idx) const override {
149  auto *CVec = dyn_cast<Constant>(Vec);
150  auto *CNewElt = dyn_cast<Constant>(NewElt);
151  auto *CIdx = dyn_cast<Constant>(Idx);
152  if (CVec && CNewElt && CIdx)
153  return ConstantExpr::getInsertElement(CVec, CNewElt, CIdx);
154  return nullptr;
155  }
156 
158  ArrayRef<int> Mask) const override {
159  auto *C1 = dyn_cast<Constant>(V1);
160  auto *C2 = dyn_cast<Constant>(V2);
161  if (C1 && C2)
163  return nullptr;
164  }
165 
166  //===--------------------------------------------------------------------===//
167  // Unary Operators
168  //===--------------------------------------------------------------------===//
169 
170  Constant *CreateFNeg(Constant *C) const override {
171  return ConstantExpr::getFNeg(C);
172  }
173 
175  return ConstantExpr::get(Opc, C);
176  }
177 
178  //===--------------------------------------------------------------------===//
179  // Cast/Conversion Operators
180  //===--------------------------------------------------------------------===//
181 
183  Type *DestTy) const override {
184  return ConstantExpr::getCast(Op, C, DestTy);
185  }
186 
187  Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
188  return ConstantExpr::getPointerCast(C, DestTy);
189  }
190 
192  Type *DestTy) const override {
194  }
195 
197  bool isSigned) const override {
198  return ConstantExpr::getIntegerCast(C, DestTy, isSigned);
199  }
200 
201  Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
202  return ConstantExpr::getFPCast(C, DestTy);
203  }
204 
205  Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
206  return CreateCast(Instruction::BitCast, C, DestTy);
207  }
208 
209  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
210  return CreateCast(Instruction::IntToPtr, C, DestTy);
211  }
212 
213  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
214  return CreateCast(Instruction::PtrToInt, C, DestTy);
215  }
216 
217  Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
218  return ConstantExpr::getZExtOrBitCast(C, DestTy);
219  }
220 
221  Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
222  return ConstantExpr::getSExtOrBitCast(C, DestTy);
223  }
224 
225  Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
226  return ConstantExpr::getTruncOrBitCast(C, DestTy);
227  }
228 
229  //===--------------------------------------------------------------------===//
230  // Compare Instructions
231  //===--------------------------------------------------------------------===//
232 
234  Constant *RHS) const override {
235  return ConstantExpr::getCompare(P, LHS, RHS);
236  }
237 };
238 
239 } // end namespace llvm
240 
241 #endif // LLVM_IR_CONSTANTFOLDER_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::ConstantExpr::getExtractElement
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2590
llvm::ConstantFolder::CreateSExtOrBitCast
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:221
llvm::ConstantExpr::getZExtOrBitCast
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2027
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
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::ConstantExpr::getFPCast
static Constant * getFPCast(Constant *C, Type *Ty)
Create a FPExt, Bitcast or FPTrunc for fp -> fp casts.
Definition: Constants.cpp:2094
C1
instcombine should handle this C2 when C1
Definition: README.txt:263
llvm::IRBuilderFolder
IRBuilderFolder - Interface for constant folding in IRBuilder.
Definition: IRBuilderFolder.h:24
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::ConstantFolder::FoldExtractElement
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Definition: ConstantFolder.h:139
llvm::ConstantExpr::getSelect
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2458
llvm::ConstantFolder::FoldExactBinOp
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Definition: ConstantFolder.h:55
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ConstantFolder::CreateFNeg
Constant * CreateFNeg(Constant *C) const override
Definition: ConstantFolder.h:170
Operator.h
llvm::ConstantExpr::getInBoundsGetElementPtr
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1270
STLExtras.h
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::ConstantFolder::FoldICmp
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: ConstantFolder.h:91
llvm::ConstantFolder::CreatePointerBitCastOrAddrSpaceCast
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:191
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
llvm::ConstantFoldExtractValueInstruction
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Definition: ConstantFold.cpp:775
llvm::ConstantExpr::getPointerCast
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:2056
llvm::ConstantFolder::FoldSelect
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: ConstantFolder.h:114
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::ConstantFolder
ConstantFolder - Create constants with minimum, target independent, folding.
Definition: ConstantFolder.h:30
Constants.h
llvm::ConstantFolder::CreateZExtOrBitCast
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:217
llvm::ConstantFolder::FoldBinOpFMF
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Definition: ConstantFolder.h:86
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ConstantFolder::CreateUnOp
Constant * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override
Definition: ConstantFolder.h:174
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:800
llvm::OverflowingBinaryOperator::NoSignedWrap
@ NoSignedWrap
Definition: Operator.h:77
llvm::OverflowingBinaryOperator::NoUnsignedWrap
@ NoUnsignedWrap
Definition: Operator.h:76
llvm::ConstantFolder::ConstantFolder
ConstantFolder()=default
llvm::ConstantExpr::getFNeg
static Constant * getFNeg(Constant *C)
Definition: Constants.cpp:2668
llvm::ConstantFoldBinaryInstruction
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
Definition: ConstantFold.cpp:872
llvm::ConstantExpr::getShuffleVector
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2635
llvm::ConstantFolder::CreatePtrToInt
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:213
llvm::ConstantExpr::getCompare
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:2436
llvm::ConstantFolder::FoldGEP
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: ConstantFolder.h:99
llvm::ConstantFolder::FoldShuffleVector
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Definition: ConstantFolder.h:157
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::ConstantFoldInsertValueInstruction
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Definition: ConstantFold.cpp:787
llvm::ConstantExpr::get
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.
Definition: Constants.cpp:2278
llvm::ConstantFolder::CreatePointerCast
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:187
ArrayRef.h
IRBuilderFolder.h
llvm::ConstantFolder::FoldNoWrapBinOp
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Definition: ConstantFolder.h:68
llvm::ConstantExpr::getCast
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1988
llvm::ConstantExpr::getTruncOrBitCast
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2039
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1624
llvm::ConstantExpr::getSExtOrBitCast
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:2033
llvm::ConstantExpr::getInsertElement
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2612
llvm::ConstantFolder::CreateFCmp
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: ConstantFolder.h:233
llvm::ConstantFolder::FoldInsertElement
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Definition: ConstantFolder.h:147
llvm::ConstantFolder::CreateIntToPtr
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:209
llvm::ConstantExpr::getIntegerCast
static Constant * getIntegerCast(Constant *C, Type *Ty, bool IsSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Definition: Constants.cpp:2082
ConstantFold.h
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::ConstantFolder::CreateFPCast
Constant * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:201
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:348
llvm::ConstantFolder::FoldExtractValue
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: ConstantFolder.h:123
llvm::ConstantFolder::CreateBitCast
Constant * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:205
llvm::ConstantExpr::getGetElementPtr
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:1243
llvm::ConstantFolder::CreateTruncOrBitCast
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:225
llvm::ConstantExpr::isDesirableBinOp
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2368
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:786
llvm::ConstantFolder::FoldBinOp
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Definition: ConstantFolder.h:43
llvm::ConstantFolder::CreateIntCast
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: ConstantFolder.h:196
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2071
llvm::ConstantFolder::FoldInsertValue
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: ConstantFolder.h:130
llvm::PossiblyExactOperator::IsExact
@ IsExact
Definition: Operator.h:129
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::ConstantFolder::CreateCast
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: ConstantFolder.h:182