LLVM  16.0.0git
InstSimplifyFolder.h
Go to the documentation of this file.
1 //===- InstSimplifyFolder.h - InstSimplify 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 InstSimplifyFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for folding operations to
11 // existing values using InstructionSimplify. At the moment, only a subset of
12 // the implementation uses InstructionSimplify. The rest of the implementation
13 // only folds constants.
14 //
15 // The folder also applies target-specific constant folding.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
20 #define LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
21 
22 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/IR/Instruction.h"
27 
28 namespace llvm {
29 class Constant;
30 
31 /// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
32 /// values. Also applies target-specific constant folding when not using
33 /// InstructionSimplify.
34 class InstSimplifyFolder final : public IRBuilderFolder {
35  TargetFolder ConstFolder;
36  SimplifyQuery SQ;
37 
38  virtual void anchor();
39 
40 public:
41  InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
42 
43  //===--------------------------------------------------------------------===//
44  // Value-based folders.
45  //
46  // Return an existing value or a constant if the operation can be simplified.
47  // Otherwise return nullptr.
48  //===--------------------------------------------------------------------===//
49 
51  Value *RHS) const override {
52  return simplifyBinOp(Opc, LHS, RHS, SQ);
53  }
54 
56  bool IsExact) const override {
57  return simplifyBinOp(Opc, LHS, RHS, SQ);
58  }
59 
61  bool HasNUW, bool HasNSW) const override {
62  return simplifyBinOp(Opc, LHS, RHS, SQ);
63  }
64 
66  FastMathFlags FMF) const override {
67  return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
68  }
69 
71  FastMathFlags FMF) const override {
72  return simplifyUnOp(Opc, V, FMF, SQ);
73  }
74 
76  return simplifyICmpInst(P, LHS, RHS, SQ);
77  }
78 
80  bool IsInBounds = false) const override {
81  return simplifyGEPInst(Ty, Ptr, IdxList, IsInBounds, SQ);
82  }
83 
84  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
85  return simplifySelectInst(C, True, False, SQ);
86  }
87 
89  ArrayRef<unsigned> IdxList) const override {
90  return simplifyExtractValueInst(Agg, IdxList, SQ);
91  };
92 
94  ArrayRef<unsigned> IdxList) const override {
95  return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
96  }
97 
98  Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
99  return simplifyExtractElementInst(Vec, Idx, SQ);
100  }
101 
103  Value *Idx) const override {
104  return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
105  }
106 
108  ArrayRef<int> Mask) const override {
109  Type *RetTy = VectorType::get(
110  cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
111  isa<ScalableVectorType>(V1->getType()));
112  return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
113  }
114 
115  //===--------------------------------------------------------------------===//
116  // Cast/Conversion Operators
117  //===--------------------------------------------------------------------===//
118 
120  Type *DestTy) const override {
121  if (C->getType() == DestTy)
122  return C; // avoid calling Fold
123  return ConstFolder.CreateCast(Op, C, DestTy);
124  }
126  bool isSigned) const override {
127  if (C->getType() == DestTy)
128  return C; // avoid calling Fold
129  return ConstFolder.CreateIntCast(C, DestTy, isSigned);
130  }
131  Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
132  if (C->getType() == DestTy)
133  return C; // avoid calling Fold
134  return ConstFolder.CreatePointerCast(C, DestTy);
135  }
136  Value *CreateFPCast(Constant *C, Type *DestTy) const override {
137  if (C->getType() == DestTy)
138  return C; // avoid calling Fold
139  return ConstFolder.CreateFPCast(C, DestTy);
140  }
141  Value *CreateBitCast(Constant *C, Type *DestTy) const override {
142  return ConstFolder.CreateBitCast(C, DestTy);
143  }
144  Value *CreateIntToPtr(Constant *C, Type *DestTy) const override {
145  return ConstFolder.CreateIntToPtr(C, DestTy);
146  }
147  Value *CreatePtrToInt(Constant *C, Type *DestTy) const override {
148  return ConstFolder.CreatePtrToInt(C, DestTy);
149  }
150  Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
151  if (C->getType() == DestTy)
152  return C; // avoid calling Fold
153  return ConstFolder.CreateZExtOrBitCast(C, DestTy);
154  }
155  Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
156  if (C->getType() == DestTy)
157  return C; // avoid calling Fold
158  return ConstFolder.CreateSExtOrBitCast(C, DestTy);
159  }
160  Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
161  if (C->getType() == DestTy)
162  return C; // avoid calling Fold
163  return ConstFolder.CreateTruncOrBitCast(C, DestTy);
164  }
165 
167  Type *DestTy) const override {
168  if (C->getType() == DestTy)
169  return C; // avoid calling Fold
170  return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
171  }
172 
173  //===--------------------------------------------------------------------===//
174  // Compare Instructions
175  //===--------------------------------------------------------------------===//
176 
178  Constant *RHS) const override {
179  return ConstFolder.CreateFCmp(P, LHS, RHS);
180  }
181 };
182 
183 } // end namespace llvm
184 
185 #endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
llvm::simplifyInsertElementInst
Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
Definition: InstructionSimplify.cpp:4772
llvm::simplifyGEPInst
Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, bool InBounds, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
Definition: InstructionSimplify.cpp:4732
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:781
llvm::InstSimplifyFolder::CreateCast
Value * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:119
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:719
llvm::SimplifyQuery
Definition: InstructionSimplify.h:93
TargetFolder.h
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::InstSimplifyFolder::FoldInsertValue
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: InstSimplifyFolder.h:93
llvm::IRBuilderFolder
IRBuilderFolder - Interface for constant folding in IRBuilder.
Definition: IRBuilderFolder.h:24
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::InstSimplifyFolder::CreateIntCast
Value * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: InstSimplifyFolder.h:125
llvm::simplifyInsertValueInst
Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
Definition: InstructionSimplify.cpp:4766
llvm::InstSimplifyFolder::FoldBinOpFMF
Value * FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const override
Definition: InstSimplifyFolder.h:65
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
llvm::InstSimplifyFolder::CreateSExtOrBitCast
Value * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:155
llvm::simplifyExtractValueInst
Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
Definition: InstructionSimplify.cpp:4832
llvm::simplifyExtractElementInst
Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
Definition: InstructionSimplify.cpp:4884
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::InstSimplifyFolder::FoldInsertElement
Value * FoldInsertElement(Value *Vec, Value *NewElt, Value *Idx) const override
Definition: InstSimplifyFolder.h:102
llvm::InstSimplifyFolder::FoldGEP
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: InstSimplifyFolder.h:79
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::InstSimplifyFolder::FoldSelect
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: InstSimplifyFolder.h:84
llvm::InstSimplifyFolder::CreatePtrToInt
Value * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:147
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::InstSimplifyFolder::CreateBitCast
Value * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:141
llvm::simplifyBinOp
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
Definition: InstructionSimplify.cpp:5636
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:802
llvm::simplifyUnOp
Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
Definition: InstructionSimplify.cpp:5561
llvm::InstSimplifyFolder::FoldExactBinOp
Value * FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool IsExact) const override
Definition: InstSimplifyFolder.h:55
llvm::simplifyICmpInst
Value * simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
Definition: InstructionSimplify.cpp:3869
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::InstSimplifyFolder::FoldNoWrapBinOp
Value * FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, bool HasNUW, bool HasNSW) const override
Definition: InstSimplifyFolder.h:60
llvm::InstSimplifyFolder::CreateZExtOrBitCast
Value * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:150
llvm::simplifySelectInst
Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
Definition: InstructionSimplify.cpp:4587
llvm::InstSimplifyFolder::FoldExtractElement
Value * FoldExtractElement(Value *Vec, Value *Idx) const override
Definition: InstSimplifyFolder.h:98
llvm::InstSimplifyFolder::CreateTruncOrBitCast
Value * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:160
llvm::InstSimplifyFolder::CreatePointerCast
Value * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:131
ArrayRef.h
IRBuilderFolder.h
llvm::InstSimplifyFolder::FoldExtractValue
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: InstSimplifyFolder.h:88
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::InstSimplifyFolder::FoldICmp
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: InstSimplifyFolder.h:75
llvm::InstSimplifyFolder::InstSimplifyFolder
InstSimplifyFolder(const DataLayout &DL)
Definition: InstSimplifyFolder.h:41
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::InstSimplifyFolder
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
Definition: InstSimplifyFolder.h:34
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::InstSimplifyFolder::CreatePointerBitCastOrAddrSpaceCast
Value * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:166
llvm::TargetFolder
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:34
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:348
llvm::InstSimplifyFolder::FoldBinOp
Value * FoldBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS) const override
Definition: InstSimplifyFolder.h:50
llvm::InstSimplifyFolder::CreateFCmp
Value * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:177
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:788
isSigned
static bool isSigned(unsigned int Opcode)
Definition: ExpandLargeDivRem.cpp:52
llvm::InstSimplifyFolder::FoldShuffleVector
Value * FoldShuffleVector(Value *V1, Value *V2, ArrayRef< int > Mask) const override
Definition: InstSimplifyFolder.h:107
InstructionSimplify.h
llvm::InstSimplifyFolder::CreateIntToPtr
Value * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:144
llvm::simplifyShuffleVectorInst
Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
Definition: InstructionSimplify.cpp:5135
llvm::InstSimplifyFolder::CreateFPCast
Value * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:136
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::VectorType::get
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:668
llvm::InstSimplifyFolder::FoldUnOpFMF
Value * FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override
Definition: InstSimplifyFolder.h:70