]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/Analysis/InstructionSimplify.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / Analysis / InstructionSimplify.h
1 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- 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 declares routines for folding instructions into simpler forms
10 // that do not require creating new instructions.  This does constant folding
11 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
12 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
13 // ("and i32 %x, %x" -> "%x").  If the simplification is also an instruction
14 // then it dominates the original instruction.
15 //
16 // These routines implicitly resolve undef uses. The easiest way to be safe when
17 // using these routines to obtain simplified values for existing instructions is
18 // to always replace all uses of the instructions with the resulting simplified
19 // values. This will prevent other code from seeing the same undef uses and
20 // resolving them to different values.
21 //
22 // These routines are designed to tolerate moderately incomplete IR, such as
23 // instructions that are not connected to basic blocks yet. However, they do
24 // require that all the IR that they encounter be valid. In particular, they
25 // require that all non-constant values be defined in the same function, and the
26 // same call context of that function (and not split between caller and callee
27 // contexts of a directly recursive call, for example).
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
32 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/IR/Instruction.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/IR/User.h"
38
39 namespace llvm {
40 class Function;
41 template <typename T, typename... TArgs> class AnalysisManager;
42 template <class T> class ArrayRef;
43 class AssumptionCache;
44 class CallBase;
45 class DominatorTree;
46 class DataLayout;
47 class FastMathFlags;
48 struct LoopStandardAnalysisResults;
49 class OptimizationRemarkEmitter;
50 class Pass;
51 class TargetLibraryInfo;
52 class Type;
53 class Value;
54 class MDNode;
55 class BinaryOperator;
56
57 /// InstrInfoQuery provides an interface to query additional information for
58 /// instructions like metadata or keywords like nsw, which provides conservative
59 /// results if the users specified it is safe to use.
60 struct InstrInfoQuery {
61   InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
62   InstrInfoQuery() : UseInstrInfo(true) {}
63   bool UseInstrInfo = true;
64
65   MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
66     if (UseInstrInfo)
67       return I->getMetadata(KindID);
68     return nullptr;
69   }
70
71   template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
72     if (UseInstrInfo)
73       return Op->hasNoUnsignedWrap();
74     return false;
75   }
76
77   template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
78     if (UseInstrInfo)
79       return Op->hasNoSignedWrap();
80     return false;
81   }
82
83   bool isExact(const BinaryOperator *Op) const {
84     if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
85       return cast<PossiblyExactOperator>(Op)->isExact();
86     return false;
87   }
88 };
89
90 struct SimplifyQuery {
91   const DataLayout &DL;
92   const TargetLibraryInfo *TLI = nullptr;
93   const DominatorTree *DT = nullptr;
94   AssumptionCache *AC = nullptr;
95   const Instruction *CxtI = nullptr;
96
97   // Wrapper to query additional information for instructions like metadata or
98   // keywords like nsw, which provides conservative results if those cannot
99   // be safely used.
100   const InstrInfoQuery IIQ;
101
102   SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
103       : DL(DL), CxtI(CXTI) {}
104
105   SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
106                 const DominatorTree *DT = nullptr,
107                 AssumptionCache *AC = nullptr,
108                 const Instruction *CXTI = nullptr, bool UseInstrInfo = true)
109       : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo) {}
110   SimplifyQuery getWithInstruction(Instruction *I) const {
111     SimplifyQuery Copy(*this);
112     Copy.CxtI = I;
113     return Copy;
114   }
115 };
116
117 // NOTE: the explicit multiple argument versions of these functions are
118 // deprecated.
119 // Please use the SimplifyQuery versions in new code.
120
121 /// Given operand for an FNeg, fold the result or return null.
122 Value *SimplifyFNegInst(Value *Op, FastMathFlags FMF,
123                         const SimplifyQuery &Q);
124
125 /// Given operands for an Add, fold the result or return null.
126 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
127                        const SimplifyQuery &Q);
128
129 /// Given operands for a Sub, fold the result or return null.
130 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
131                        const SimplifyQuery &Q);
132
133 /// Given operands for an FAdd, fold the result or return null.
134 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
135                         const SimplifyQuery &Q);
136
137 /// Given operands for an FSub, fold the result or return null.
138 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
139                         const SimplifyQuery &Q);
140
141 /// Given operands for an FMul, fold the result or return null.
142 Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
143                         const SimplifyQuery &Q);
144
145 /// Given operands for a Mul, fold the result or return null.
146 Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
147
148 /// Given operands for an SDiv, fold the result or return null.
149 Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
150
151 /// Given operands for a UDiv, fold the result or return null.
152 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
153
154 /// Given operands for an FDiv, fold the result or return null.
155 Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
156                         const SimplifyQuery &Q);
157
158 /// Given operands for an SRem, fold the result or return null.
159 Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
160
161 /// Given operands for a URem, fold the result or return null.
162 Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
163
164 /// Given operands for an FRem, fold the result or return null.
165 Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
166                         const SimplifyQuery &Q);
167
168 /// Given operands for a Shl, fold the result or return null.
169 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
170                        const SimplifyQuery &Q);
171
172 /// Given operands for a LShr, fold the result or return null.
173 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
174                         const SimplifyQuery &Q);
175
176 /// Given operands for a AShr, fold the result or return nulll.
177 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
178                         const SimplifyQuery &Q);
179
180 /// Given operands for an And, fold the result or return null.
181 Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
182
183 /// Given operands for an Or, fold the result or return null.
184 Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
185
186 /// Given operands for an Xor, fold the result or return null.
187 Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
188
189 /// Given operands for an ICmpInst, fold the result or return null.
190 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
191                         const SimplifyQuery &Q);
192
193 /// Given operands for an FCmpInst, fold the result or return null.
194 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
195                         FastMathFlags FMF, const SimplifyQuery &Q);
196
197 /// Given operands for a SelectInst, fold the result or return null.
198 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
199                           const SimplifyQuery &Q);
200
201 /// Given operands for a GetElementPtrInst, fold the result or return null.
202 Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
203                        const SimplifyQuery &Q);
204
205 /// Given operands for an InsertValueInst, fold the result or return null.
206 Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
207                                const SimplifyQuery &Q);
208
209 /// Given operands for an InsertElement, fold the result or return null.
210 Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
211                                  const SimplifyQuery &Q);
212
213 /// Given operands for an ExtractValueInst, fold the result or return null.
214 Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
215                                 const SimplifyQuery &Q);
216
217 /// Given operands for an ExtractElementInst, fold the result or return null.
218 Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
219                                   const SimplifyQuery &Q);
220
221 /// Given operands for a CastInst, fold the result or return null.
222 Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
223                         const SimplifyQuery &Q);
224
225 /// Given operands for a ShuffleVectorInst, fold the result or return null.
226 Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask,
227                                  Type *RetTy, const SimplifyQuery &Q);
228
229 //=== Helper functions for higher up the class hierarchy.
230
231 /// Given operands for a CmpInst, fold the result or return null.
232 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
233                        const SimplifyQuery &Q);
234
235 /// Given operand for a UnaryOperator, fold the result or return null.
236 Value *SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q);
237
238 /// Given operand for an FP UnaryOperator, fold the result or return null.
239 /// In contrast to SimplifyUnOp, try to use FastMathFlag when folding the
240 /// result. In case we don't need FastMathFlags, simply fall to SimplifyUnOp.
241 Value *SimplifyFPUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
242                       const SimplifyQuery &Q);
243
244 /// Given operands for a BinaryOperator, fold the result or return null.
245 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
246                      const SimplifyQuery &Q);
247
248 /// Given operands for an FP BinaryOperator, fold the result or return null.
249 /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
250 /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
251 Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
252                        FastMathFlags FMF, const SimplifyQuery &Q);
253
254 /// Given a callsite, fold the result or return null.
255 Value *SimplifyCall(CallBase *Call, const SimplifyQuery &Q);
256
257 /// See if we can compute a simplified version of this instruction. If not,
258 /// return null.
259 Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
260                            OptimizationRemarkEmitter *ORE = nullptr);
261
262 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
263 ///
264 /// This first performs a normal RAUW of I with SimpleV. It then recursively
265 /// attempts to simplify those users updated by the operation. The 'I'
266 /// instruction must not be equal to the simplified value 'SimpleV'.
267 /// If UnsimplifiedUsers is provided, instructions that could not be simplified
268 /// are added to it.
269 ///
270 /// The function returns true if any simplifications were performed.
271 bool replaceAndRecursivelySimplify(
272     Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
273     const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
274     SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
275
276 /// Recursively attempt to simplify an instruction.
277 ///
278 /// This routine uses SimplifyInstruction to simplify 'I', and if successful
279 /// replaces uses of 'I' with the simplified value. It then recurses on each
280 /// of the users impacted. It returns true if any simplifications were
281 /// performed.
282 bool recursivelySimplifyInstruction(Instruction *I,
283                                     const TargetLibraryInfo *TLI = nullptr,
284                                     const DominatorTree *DT = nullptr,
285                                     AssumptionCache *AC = nullptr);
286
287 // These helper functions return a SimplifyQuery structure that contains as
288 // many of the optional analysis we use as are currently valid.  This is the
289 // strongly preferred way of constructing SimplifyQuery in passes.
290 const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
291 template <class T, class... TArgs>
292 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
293                                          Function &);
294 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
295                                          const DataLayout &);
296 } // end namespace llvm
297
298 #endif
299