LLVM  15.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  Value *FoldAdd(Value *LHS, Value *RHS, bool HasNUW = false,
50  bool HasNSW = false) const override {
51  return SimplifyAddInst(LHS, RHS, HasNUW, HasNSW, SQ);
52  }
53 
54  Value *FoldAnd(Value *LHS, Value *RHS) const override {
55  return SimplifyAndInst(LHS, RHS, SQ);
56  }
57 
58  Value *FoldOr(Value *LHS, Value *RHS) const override {
59  return SimplifyOrInst(LHS, RHS, SQ);
60  }
61 
63  return SimplifyICmpInst(P, LHS, RHS, SQ);
64  }
65 
66  Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
67  bool IsInBounds = false) const override {
68  return SimplifyGEPInst(Ty, Ptr, IdxList, IsInBounds, SQ);
69  }
70 
71  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
72  return SimplifySelectInst(C, True, False, SQ);
73  }
74 
76  ArrayRef<unsigned> IdxList) const override {
77  return SimplifyExtractValueInst(Agg, IdxList, SQ);
78  };
79 
81  ArrayRef<unsigned> IdxList) const override {
82  return SimplifyInsertValueInst(Agg, Val, IdxList, SQ);
83  }
84 
85  //===--------------------------------------------------------------------===//
86  // Binary Operators
87  //===--------------------------------------------------------------------===//
88 
89  Value *CreateFAdd(Constant *LHS, Constant *RHS) const override {
90  return ConstFolder.CreateFAdd(LHS, RHS);
91  }
92  Value *CreateSub(Constant *LHS, Constant *RHS, bool HasNUW = false,
93  bool HasNSW = false) const override {
94  return ConstFolder.CreateSub(LHS, RHS, HasNUW, HasNSW);
95  }
96  Value *CreateFSub(Constant *LHS, Constant *RHS) const override {
97  return ConstFolder.CreateFSub(LHS, RHS);
98  }
99  Value *CreateMul(Constant *LHS, Constant *RHS, bool HasNUW = false,
100  bool HasNSW = false) const override {
101  return ConstFolder.CreateMul(LHS, RHS, HasNUW, HasNSW);
102  }
103  Value *CreateFMul(Constant *LHS, Constant *RHS) const override {
104  return ConstFolder.CreateFMul(LHS, RHS);
105  }
107  bool isExact = false) const override {
108  return ConstFolder.CreateUDiv(LHS, RHS, isExact);
109  }
111  bool isExact = false) const override {
112  return ConstFolder.CreateSDiv(LHS, RHS, isExact);
113  }
114  Value *CreateFDiv(Constant *LHS, Constant *RHS) const override {
115  return ConstFolder.CreateFDiv(LHS, RHS);
116  }
117  Value *CreateURem(Constant *LHS, Constant *RHS) const override {
118  return ConstFolder.CreateURem(LHS, RHS);
119  }
120  Value *CreateSRem(Constant *LHS, Constant *RHS) const override {
121  return ConstFolder.CreateSRem(LHS, RHS);
122  }
123  Value *CreateFRem(Constant *LHS, Constant *RHS) const override {
124  return ConstFolder.CreateFRem(LHS, RHS);
125  }
126  Value *CreateShl(Constant *LHS, Constant *RHS, bool HasNUW = false,
127  bool HasNSW = false) const override {
128  return ConstFolder.CreateShl(LHS, RHS, HasNUW, HasNSW);
129  }
131  bool isExact = false) const override {
132  return ConstFolder.CreateLShr(LHS, RHS, isExact);
133  }
135  bool isExact = false) const override {
136  return ConstFolder.CreateAShr(LHS, RHS, isExact);
137  }
138  Value *CreateXor(Constant *LHS, Constant *RHS) const override {
139  return ConstFolder.CreateXor(LHS, RHS);
140  }
141 
143  Constant *RHS) const override {
144  return ConstFolder.CreateBinOp(Opc, LHS, RHS);
145  }
146 
147  //===--------------------------------------------------------------------===//
148  // Unary Operators
149  //===--------------------------------------------------------------------===//
150 
151  Value *CreateNeg(Constant *C, bool HasNUW = false,
152  bool HasNSW = false) const override {
153  return ConstFolder.CreateNeg(C, HasNUW, HasNSW);
154  }
155  Value *CreateFNeg(Constant *C) const override {
156  return ConstFolder.CreateFNeg(C);
157  }
158  Value *CreateNot(Constant *C) const override {
159  return ConstFolder.CreateNot(C);
160  }
161 
163  return ConstFolder.CreateUnOp(Opc, C);
164  }
165 
166  //===--------------------------------------------------------------------===//
167  // Cast/Conversion Operators
168  //===--------------------------------------------------------------------===//
169 
171  Type *DestTy) const override {
172  if (C->getType() == DestTy)
173  return C; // avoid calling Fold
174  return ConstFolder.CreateCast(Op, C, DestTy);
175  }
177  bool isSigned) const override {
178  if (C->getType() == DestTy)
179  return C; // avoid calling Fold
180  return ConstFolder.CreateIntCast(C, DestTy, isSigned);
181  }
182  Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
183  if (C->getType() == DestTy)
184  return C; // avoid calling Fold
185  return ConstFolder.CreatePointerCast(C, DestTy);
186  }
187  Value *CreateFPCast(Constant *C, Type *DestTy) const override {
188  if (C->getType() == DestTy)
189  return C; // avoid calling Fold
190  return ConstFolder.CreateFPCast(C, DestTy);
191  }
192  Value *CreateBitCast(Constant *C, Type *DestTy) const override {
193  return ConstFolder.CreateBitCast(C, DestTy);
194  }
195  Value *CreateIntToPtr(Constant *C, Type *DestTy) const override {
196  return ConstFolder.CreateIntToPtr(C, DestTy);
197  }
198  Value *CreatePtrToInt(Constant *C, Type *DestTy) const override {
199  return ConstFolder.CreatePtrToInt(C, DestTy);
200  }
201  Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
202  if (C->getType() == DestTy)
203  return C; // avoid calling Fold
204  return ConstFolder.CreateZExtOrBitCast(C, DestTy);
205  }
206  Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
207  if (C->getType() == DestTy)
208  return C; // avoid calling Fold
209  return ConstFolder.CreateSExtOrBitCast(C, DestTy);
210  }
211  Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
212  if (C->getType() == DestTy)
213  return C; // avoid calling Fold
214  return ConstFolder.CreateTruncOrBitCast(C, DestTy);
215  }
216 
218  Type *DestTy) const override {
219  if (C->getType() == DestTy)
220  return C; // avoid calling Fold
221  return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
222  }
223 
224  //===--------------------------------------------------------------------===//
225  // Compare Instructions
226  //===--------------------------------------------------------------------===//
227 
229  Constant *RHS) const override {
230  return ConstFolder.CreateFCmp(P, LHS, RHS);
231  }
232 
233  //===--------------------------------------------------------------------===//
234  // Other Instructions
235  //===--------------------------------------------------------------------===//
236 
237  Value *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
238  return ConstFolder.CreateExtractElement(Vec, Idx);
239  }
240 
242  Constant *Idx) const override {
243  return ConstFolder.CreateInsertElement(Vec, NewElt, Idx);
244  }
245 
247  ArrayRef<int> Mask) const override {
248  return ConstFolder.CreateShuffleVector(V1, V2, Mask);
249  }
250 };
251 
252 } // end namespace llvm
253 
254 #endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
llvm::InstSimplifyFolder::CreateLShr
Value * CreateLShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: InstSimplifyFolder.h:130
llvm::InstSimplifyFolder::CreateFDiv
Value * CreateFDiv(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:114
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::InstSimplifyFolder::CreateShuffleVector
Value * CreateShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask) const override
Definition: InstSimplifyFolder.h:246
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:779
llvm::InstSimplifyFolder::CreateCast
Value * CreateCast(Instruction::CastOps Op, Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:170
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:719
llvm::SimplifyQuery
Definition: InstructionSimplify.h:93
llvm::InstSimplifyFolder::CreateFRem
Value * CreateFRem(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:123
llvm::InstSimplifyFolder::CreateUDiv
Value * CreateUDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: InstSimplifyFolder.h:106
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::SimplifyAndInst
Value * SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
Definition: InstructionSimplify.cpp:2234
llvm::InstSimplifyFolder::FoldInsertValue
Value * FoldInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > IdxList) const override
Definition: InstSimplifyFolder.h:80
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:4517
llvm::IRBuilderFolder
IRBuilderFolder - Interface for constant folding in IRBuilder.
Definition: IRBuilderFolder.h:24
llvm::InstSimplifyFolder::CreateExtractElement
Value * CreateExtractElement(Constant *Vec, Constant *Idx) const override
Definition: InstSimplifyFolder.h:237
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::InstSimplifyFolder::CreateBinOp
Value * CreateBinOp(Instruction::BinaryOps Opc, Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:142
llvm::InstSimplifyFolder::CreateIntCast
Value * CreateIntCast(Constant *C, Type *DestTy, bool isSigned) const override
Definition: InstSimplifyFolder.h:176
llvm::InstSimplifyFolder::CreateUnOp
Value * CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override
Definition: InstSimplifyFolder.h:162
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:4662
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::InstSimplifyFolder::CreateSRem
Value * CreateSRem(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:120
llvm::InstSimplifyFolder::CreateSExtOrBitCast
Value * CreateSExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:206
llvm::InstSimplifyFolder::FoldOr
Value * FoldOr(Value *LHS, Value *RHS) const override
Definition: InstSimplifyFolder.h:58
llvm::InstSimplifyFolder::CreateMul
Value * CreateMul(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: InstSimplifyFolder.h:99
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::FoldGEP
Value * FoldGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, bool IsInBounds=false) const override
Definition: InstSimplifyFolder.h:66
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::InstSimplifyFolder::CreateFNeg
Value * CreateFNeg(Constant *C) const override
Definition: InstSimplifyFolder.h:155
llvm::InstSimplifyFolder::FoldSelect
Value * FoldSelect(Value *C, Value *True, Value *False) const override
Definition: InstSimplifyFolder.h:71
llvm::InstSimplifyFolder::CreatePtrToInt
Value * CreatePtrToInt(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:198
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
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:3878
llvm::InstSimplifyFolder::CreateSDiv
Value * CreateSDiv(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: InstSimplifyFolder.h:110
llvm::InstSimplifyFolder::CreateBitCast
Value * CreateBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:192
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:800
llvm::InstSimplifyFolder::CreateShl
Value * CreateShl(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: InstSimplifyFolder.h:126
llvm::InstSimplifyFolder::CreateNot
Value * CreateNot(Constant *C) const override
Definition: InstSimplifyFolder.h:158
llvm::InstSimplifyFolder::CreateFAdd
Value * CreateFAdd(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:89
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::InstSimplifyFolder::CreateInsertElement
Value * CreateInsertElement(Constant *Vec, Constant *NewElt, Constant *Idx) const override
Definition: InstSimplifyFolder.h:241
llvm::InstSimplifyFolder::CreateZExtOrBitCast
Value * CreateZExtOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:201
llvm::InstSimplifyFolder::CreateTruncOrBitCast
Value * CreateTruncOrBitCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:211
llvm::InstSimplifyFolder::CreatePointerCast
Value * CreatePointerCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:182
llvm::InstSimplifyFolder::CreateFSub
Value * CreateFSub(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:96
ArrayRef.h
IRBuilderFolder.h
llvm::InstSimplifyFolder::CreateNeg
Value * CreateNeg(Constant *C, bool HasNUW=false, bool HasNSW=false) const override
Definition: InstSimplifyFolder.h:151
llvm::InstSimplifyFolder::FoldExtractValue
Value * FoldExtractValue(Value *Agg, ArrayRef< unsigned > IdxList) const override
Definition: InstSimplifyFolder.h:75
llvm::InstSimplifyFolder::FoldICmp
Value * FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override
Definition: InstSimplifyFolder.h:62
llvm::InstSimplifyFolder::InstSimplifyFolder
InstSimplifyFolder(const DataLayout &DL)
Definition: InstSimplifyFolder.h:41
llvm::InstSimplifyFolder::CreateAShr
Value * CreateAShr(Constant *LHS, Constant *RHS, bool isExact=false) const override
Definition: InstSimplifyFolder.h:134
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::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:4762
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
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:4696
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::InstSimplifyFolder::CreatePointerBitCastOrAddrSpaceCast
Value * CreatePointerBitCastOrAddrSpaceCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:217
llvm::TargetFolder
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:33
llvm::InstSimplifyFolder::CreateSub
Value * CreateSub(Constant *LHS, Constant *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: InstSimplifyFolder.h:92
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:341
llvm::InstSimplifyFolder::CreateXor
Value * CreateXor(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:138
llvm::InstSimplifyFolder::CreateFCmp
Value * CreateFCmp(CmpInst::Predicate P, Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:228
llvm::InstSimplifyFolder::CreateURem
Value * CreateURem(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:117
llvm::Instruction::BinaryOps
BinaryOps
Definition: Instruction.h:786
llvm::InstSimplifyFolder::CreateFMul
Value * CreateFMul(Constant *LHS, Constant *RHS) const override
Definition: InstSimplifyFolder.h:103
InstructionSimplify.h
llvm::SimplifyOrInst
Value * SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
Definition: InstructionSimplify.cpp:2475
llvm::InstSimplifyFolder::FoldAnd
Value * FoldAnd(Value *LHS, Value *RHS) const override
Definition: InstSimplifyFolder.h:54
llvm::InstSimplifyFolder::FoldAdd
Value * FoldAdd(Value *LHS, Value *RHS, bool HasNUW=false, bool HasNSW=false) const override
Definition: InstSimplifyFolder.h:49
llvm::InstSimplifyFolder::CreateIntToPtr
Value * CreateIntToPtr(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:195
llvm::InstSimplifyFolder::CreateFPCast
Value * CreateFPCast(Constant *C, Type *DestTy) const override
Definition: InstSimplifyFolder.h:187
llvm::SimplifyAddInst
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
Definition: InstructionSimplify.cpp:683
llvm::Value
LLVM Value Representation.
Definition: Value.h:74