LLVM  16.0.0git
TargetFolder.h
Go to the documentation of this file.
1 //====- TargetFolder.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 TargetFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants with
11 // target dependent folding, in addition to the same target-independent
12 // folding that the ConstantFolder class provides. For general constant
13 // creation and folding, use ConstantExpr and the routines in
14 // llvm/Analysis/ConstantFolding.h.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_ANALYSIS_TARGETFOLDER_H
19 #define LLVM_ANALYSIS_TARGETFOLDER_H
20 
21 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Operator.h"
26 
27 namespace llvm {
28 
29 class Constant;
30 class DataLayout;
31 class Type;
32 
33 /// TargetFolder - Create constants with target dependent folding.
34 class TargetFolder final : public IRBuilderFolder {
35  const DataLayout &DL;
36 
37  /// Fold - Fold the constant using target specific information.
38  Constant *Fold(Constant *C) const {
39  return ConstantFoldConstant(C, DL);
40  }
41 
42  virtual void anchor();
43 
44 public:
45  explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
46 
47  //===--------------------------------------------------------------------===//
48  // Value-based folders.
49  //
50  // Return an existing value or a constant if the operation can be simplified.
51  // Otherwise return nullptr.
52  //===--------------------------------------------------------------------===//
53 
55  Value *RHS) const override {
56  auto *LC = dyn_cast<Constant>(LHS);
57  auto *RC = dyn_cast<Constant>(RHS);
58  if (LC && RC) {
60  return Fold(ConstantExpr::get(Opc, LC, RC));
61  return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
62  }
63  return nullptr;
64  }
65 
67  bool IsExact) const override {
68  auto *LC = dyn_cast<Constant>(LHS);
69  auto *RC = dyn_cast<Constant>(RHS);
70  if (LC && RC) {
72  return Fold(ConstantExpr::get(
73  Opc, LC, RC, IsExact ? PossiblyExactOperator::IsExact : 0));
74  return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
75  }
76  return nullptr;
77  }
78 
80  bool HasNUW, bool HasNSW) const override {
81  auto *LC = dyn_cast<Constant>(LHS);
82  auto *RC = dyn_cast<Constant>(RHS);
83  if (LC && RC) {
85  unsigned Flags = 0;
86  if (HasNUW)
88  if (HasNSW)
90  return Fold(ConstantExpr::get(Opc, LC, RC, Flags));
91  }
92  return ConstantFoldBinaryOpOperands(Opc, LC, RC, DL);
93  }
94  return nullptr;
95  }
96 
98  FastMathFlags FMF) const override {
99  return FoldBinOp(Opc, LHS, RHS);
100  }
101 
103  auto *LC = dyn_cast<Constant>(LHS);
104  auto *RC = dyn_cast<Constant>(RHS);
105  if (LC && RC)
106  return Fold(ConstantExpr::getCompare(P, LC, RC));
107  return nullptr;
108  }
109 
111  FastMathFlags FMF) const override {
112  if (Constant *C = dyn_cast<Constant>(V))
113  return ConstantFoldUnaryOpOperand(Opc, C, DL);
114  return nullptr;
115  }
116 
118  bool IsInBounds = false) const override {
119  if (auto *PC = dyn_cast<Constant>(Ptr)) {
120  // Every index must be constant.
121  if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
122  return nullptr;
123  if (IsInBounds)
124  return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList));
125  else
126  return Fold(ConstantExpr::getGetElementPtr(Ty, PC, IdxList));
127  }
128  return nullptr;
129  }
130 
131  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
132  auto *CC = dyn_cast<Constant>(C);
133  auto *TC = dyn_cast<Constant>(True);
134  auto *FC = dyn_cast<Constant>(False);
135  if (CC && TC && FC)
136  return Fold(ConstantExpr::getSelect(CC, TC, FC));
137 
138  return nullptr;
139  }
140 
142  ArrayRef<unsigned> IdxList) const override {
143  if (auto *CAgg = dyn_cast<Constant>(Agg))
144  return ConstantFoldExtractValueInstruction(CAgg, IdxList);
145  return nullptr;
146  };
147 
149  ArrayRef<unsigned> IdxList) const override {
150  auto *CAgg = dyn_cast<Constant>(Agg);
151  auto *CVal = dyn_cast<Constant>(Val);
152  if (CAgg && CVal)
153  return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
154  return nullptr;
155  }
156 
157  Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
158  auto *CVec = dyn_cast<Constant>(Vec);
159  auto *CIdx = dyn_cast<Constant>(Idx);
160  if (CVec && CIdx)
161  return Fold(ConstantExpr::getExtractElement(CVec, CIdx));
162  return nullptr;
163  }
164 
166  Value *Idx) const override {
167  auto *CVec = dyn_cast<Constant>(Vec);
168  auto *CNewElt = dyn_cast<Constant>(NewElt);
169  auto *CIdx = dyn_cast<Constant>(Idx);
170  if (CVec && CNewElt && CIdx)
171  return Fold(ConstantExpr::getInsertElement(CVec, CNewElt, CIdx));
172  return nullptr;
173  }
174 
176  ArrayRef<int> Mask) const override {
177  auto *C1 = dyn_cast<Constant>(V1);
178  auto *C2 = dyn_cast<Constant>(V2);
179  if (C1 && C2)
180  return Fold(ConstantExpr::getShuffleVector(C1, C2, Mask));
181  return nullptr;
182  }
183 
184  //===--------------------------------------------------------------------===//
185  // Cast/Conversion Operators
186  //===--------------------------------------------------------------------===//
187 
189  Type *DestTy) const override {
190  if (C->getType() == DestTy)
191  return C; // avoid calling Fold
192  return Fold(ConstantExpr::getCast(Op, C, DestTy));
193  }
195  bool isSigned) const override {
196  if (C->getType() == DestTy)
197  return C; // avoid calling Fold
198  return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
199  }
200  Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
201  if (C->getType() == DestTy)
202  return C; // avoid calling Fold
203  return Fold(ConstantExpr::getPointerCast(C, DestTy));
204  }
205  Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
206  if (C->getType() == DestTy)
207  return C; // avoid calling Fold
208  return Fold(ConstantExpr::getFPCast(C, DestTy));
209  }
210  Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
211  return CreateCast(Instruction::BitCast, C, DestTy);
212  }
213  Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
214  return CreateCast(Instruction::IntToPtr, C, DestTy);
215  }
216  Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
217  return CreateCast(Instruction::PtrToInt, C, DestTy);
218  }
219  Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
220  if (C->getType() == DestTy)
221  return C; // avoid calling Fold
222  return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
223  }
224  Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
225  if (C->getType() == DestTy)
226  return C; // avoid calling Fold
227  return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
228  }
229  Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
230  if (C->getType() == DestTy)
231  return C; // avoid calling Fold
232  return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
233  }
234 
236  Type *DestTy) const override {
237  if (C->getType() == DestTy)
238  return C; // avoid calling Fold
240  }
241 
242  //===--------------------------------------------------------------------===//
243  // Compare Instructions
244  //===--------------------------------------------------------------------===//
245 
247  Constant *RHS) const override {
248  return Fold(ConstantExpr::getCompare(P, LHS, RHS));
249  }
250 };
251 
252 }
253 
254 #endif
llvm::TargetFolder::FoldExtractValue
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:141
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::ConstantExpr::getExtractElement
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2546
llvm::ConstantFoldUnaryOpOperand
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Definition: ConstantFolding.cpp:1332
llvm::ConstantExpr::getZExtOrBitCast
static Constant * getZExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1985
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::Instruction::UnaryOps
UnaryOps
Definition: Instruction.h:786
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:720
llvm::TargetFolder::FoldGEP
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: TargetFolder.h:117
llvm::TargetFolder::FoldICmp
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: TargetFolder.h:102
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:2052
C1
instcombine should handle this C2 when C1
Definition: README.txt:263
llvm::TargetFolder::FoldExtractElement
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Definition: TargetFolder.h:157
llvm::IRBuilderFolder
IRBuilderFolder - Interface for constant folding in IRBuilder.
Definition: IRBuilderFolder.h:24
llvm::TargetFolder::CreateFCmp
Constant * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: TargetFolder.h:246
llvm::TargetFolder::CreatePtrToInt
Constant * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:216
R600_InstFlag::FC
@ FC
Definition: R600Defines.h:32
llvm::ConstantExpr::getSelect
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2414
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Operator.h
llvm::PossiblyExactOperator::IsExact
@ IsExact
Definition: Operator.h:129
llvm::ConstantExpr::getInBoundsGetElementPtr
static Constant * getInBoundsGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList)
Create an "inbounds" getelementptr.
Definition: Constants.h:1245
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
ConstantFolding.h
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:2014
llvm::TargetFolder::FoldInsertElement
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Definition: TargetFolder.h:165
llvm::ConstantExpr::get
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
Definition: Constants.cpp:2236
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
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
Constants.h
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::ConstantFoldBinaryOpOperands
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Definition: ConstantFolding.cpp:1339
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::TargetFolder::FoldBinOpFMF
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Definition: TargetFolder.h:97
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:807
llvm::TargetFolder::FoldUnOpFMF
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Definition: TargetFolder.h:110
llvm::TargetFolder::CreatePointerCast
Constant * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:200
llvm::TargetFolder::CreateBitCast
Constant * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:210
llvm::TargetFolder::FoldNoWrapBinOp
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Definition: TargetFolder.h:79
llvm::ConstantExpr::getShuffleVector
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2591
llvm::TargetFolder::FoldBinOp
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Definition: TargetFolder.h:54
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:2392
llvm::TargetFolder::CreateFPCast
Constant * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:205
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::TargetFolder::FoldExactBinOp
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Definition: TargetFolder.h:66
ArrayRef.h
IRBuilderFolder.h
TemplateParamKind::Type
@ Type
llvm::TargetFolder::FoldInsertValue
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: TargetFolder.h:148
llvm::TargetFolder::FoldSelect
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: TargetFolder.h:131
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:1946
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::ConstantExpr::getTruncOrBitCast
static Constant * getTruncOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1997
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:1741
llvm::TargetFolder::CreatePointerBitCastOrAddrSpaceCast
Constant * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:235
llvm::ConstantExpr::getSExtOrBitCast
static Constant * getSExtOrBitCast(Constant *C, Type *Ty)
Definition: Constants.cpp:1991
llvm::TargetFolder::FoldShuffleVector
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Definition: TargetFolder.h:175
llvm::TargetFolder::CreateIntCast
Constant * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: TargetFolder.h:194
llvm::TargetFolder::CreateZExtOrBitCast
Constant * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:219
llvm::ConstantExpr::getInsertElement
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2568
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
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:2040
CC
auto CC
Definition: RISCVRedundantCopyElimination.cpp:79
llvm::TargetFolder::CreateCast
Constant * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:188
llvm::TargetFolder::CreateSExtOrBitCast
Constant * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:224
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::TargetFolder
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:34
llvm::TargetFolder::TargetFolder
TargetFolder(const DataLayout &DL)
Definition: TargetFolder.h:45
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:348
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:1218
llvm::TargetFolder::CreateIntToPtr
Constant * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:213
llvm::OverflowingBinaryOperator::NoSignedWrap
@ NoSignedWrap
Definition: Operator.h:77
llvm::OverflowingBinaryOperator::NoUnsignedWrap
@ NoUnsignedWrap
Definition: Operator.h:76
llvm::ConstantExpr::isDesirableBinOp
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
Definition: Constants.cpp:2297
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:793
isSigned
static bool isSigned(unsigned int Opcode)
Definition: ExpandLargeDivRem.cpp:52
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:2029
llvm::ConstantFoldConstant
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Definition: ConstantFolding.cpp:1207
llvm::TargetFolder::CreateTruncOrBitCast
Constant * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: TargetFolder.h:229
llvm::Value
LLVM Value Representation.
Definition: Value.h:74