]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/InstructionSimplify.h
Merge ^/head r314178 through r314269.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / InstructionSimplify.h
1 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares routines for folding instructions into simpler forms
11 // that do not require creating new instructions.  This does constant folding
12 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
13 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
14 // ("and i32 %x, %x" -> "%x").  If the simplification is also an instruction
15 // then it dominates the original instruction.
16 //
17 // These routines implicitly resolve undef uses. The easiest way to be safe when
18 // using these routines to obtain simplified values for existing instructions is
19 // to always replace all uses of the instructions with the resulting simplified
20 // values. This will prevent other code from seeing the same undef uses and
21 // resolving them to different values.
22 //
23 // These routines are designed to tolerate moderately incomplete IR, such as
24 // instructions that are not connected to basic blocks yet. However, they do
25 // require that all the IR that they encounter be valid. In particular, they
26 // require that all non-constant values be defined in the same function, and the
27 // same call context of that function (and not split between caller and callee
28 // contexts of a directly recursive call, for example).
29 //
30 //===----------------------------------------------------------------------===//
31
32 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
33 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
34
35 #include "llvm/IR/User.h"
36
37 namespace llvm {
38   template<typename T>
39   class ArrayRef;
40   class AssumptionCache;
41   class DominatorTree;
42   class Instruction;
43   class DataLayout;
44   class FastMathFlags;
45   class TargetLibraryInfo;
46   class Type;
47   class Value;
48
49   /// Given operands for an Add, fold the result or return null.
50   Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
51                          const DataLayout &DL,
52                          const TargetLibraryInfo *TLI = nullptr,
53                          const DominatorTree *DT = nullptr,
54                          AssumptionCache *AC = nullptr,
55                          const Instruction *CxtI = nullptr);
56
57   /// Given operands for a Sub, fold the result or return null.
58   Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
59                          const DataLayout &DL,
60                          const TargetLibraryInfo *TLI = nullptr,
61                          const DominatorTree *DT = nullptr,
62                          AssumptionCache *AC = nullptr,
63                          const Instruction *CxtI = nullptr);
64
65   /// Given operands for an FAdd, fold the result or return null.
66   Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
67                           const DataLayout &DL,
68                           const TargetLibraryInfo *TLI = nullptr,
69                           const DominatorTree *DT = nullptr,
70                           AssumptionCache *AC = nullptr,
71                           const Instruction *CxtI = nullptr);
72
73   /// Given operands for an FSub, fold the result or return null.
74   Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
75                           const DataLayout &DL,
76                           const TargetLibraryInfo *TLI = nullptr,
77                           const DominatorTree *DT = nullptr,
78                           AssumptionCache *AC = nullptr,
79                           const Instruction *CxtI = nullptr);
80
81   /// Given operands for an FMul, fold the result or return null.
82   Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
83                           const DataLayout &DL,
84                           const TargetLibraryInfo *TLI = nullptr,
85                           const DominatorTree *DT = nullptr,
86                           AssumptionCache *AC = nullptr,
87                           const Instruction *CxtI = nullptr);
88
89   /// Given operands for a Mul, fold the result or return null.
90   Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL,
91                          const TargetLibraryInfo *TLI = nullptr,
92                          const DominatorTree *DT = nullptr,
93                          AssumptionCache *AC = nullptr,
94                          const Instruction *CxtI = nullptr);
95
96   /// Given operands for an SDiv, fold the result or return null.
97   Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
98                           const TargetLibraryInfo *TLI = nullptr,
99                           const DominatorTree *DT = nullptr,
100                           AssumptionCache *AC = nullptr,
101                           const Instruction *CxtI = nullptr);
102
103   /// Given operands for a UDiv, fold the result or return null.
104   Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
105                           const TargetLibraryInfo *TLI = nullptr,
106                           const DominatorTree *DT = nullptr,
107                           AssumptionCache *AC = nullptr,
108                           const Instruction *CxtI = nullptr);
109
110   /// Given operands for an FDiv, fold the result or return null.
111   Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
112                           const DataLayout &DL,
113                           const TargetLibraryInfo *TLI = nullptr,
114                           const DominatorTree *DT = nullptr,
115                           AssumptionCache *AC = nullptr,
116                           const Instruction *CxtI = nullptr);
117
118   /// Given operands for an SRem, fold the result or return null.
119   Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL,
120                           const TargetLibraryInfo *TLI = nullptr,
121                           const DominatorTree *DT = nullptr,
122                           AssumptionCache *AC = nullptr,
123                           const Instruction *CxtI = nullptr);
124
125   /// Given operands for a URem, fold the result or return null.
126   Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL,
127                           const TargetLibraryInfo *TLI = nullptr,
128                           const DominatorTree *DT = nullptr,
129                           AssumptionCache *AC = nullptr,
130                           const Instruction *CxtI = nullptr);
131
132   /// Given operands for an FRem, fold the result or return null.
133   Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
134                           const DataLayout &DL,
135                           const TargetLibraryInfo *TLI = nullptr,
136                           const DominatorTree *DT = nullptr,
137                           AssumptionCache *AC = nullptr,
138                           const Instruction *CxtI = nullptr);
139
140   /// Given operands for a Shl, fold the result or return null.
141   Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
142                          const DataLayout &DL,
143                          const TargetLibraryInfo *TLI = nullptr,
144                          const DominatorTree *DT = nullptr,
145                          AssumptionCache *AC = nullptr,
146                          const Instruction *CxtI = nullptr);
147
148   /// Given operands for a LShr, fold the result or return null.
149   Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
150                           const DataLayout &DL,
151                           const TargetLibraryInfo *TLI = nullptr,
152                           const DominatorTree *DT = nullptr,
153                           AssumptionCache *AC = nullptr,
154                           const Instruction *CxtI = nullptr);
155
156   /// Given operands for a AShr, fold the result or return nulll.
157   Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
158                           const DataLayout &DL,
159                           const TargetLibraryInfo *TLI = nullptr,
160                           const DominatorTree *DT = nullptr,
161                           AssumptionCache *AC = nullptr,
162                           const Instruction *CxtI = nullptr);
163
164   /// Given operands for an And, fold the result or return null.
165   Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout &DL,
166                          const TargetLibraryInfo *TLI = nullptr,
167                          const DominatorTree *DT = nullptr,
168                          AssumptionCache *AC = nullptr,
169                          const Instruction *CxtI = nullptr);
170
171   /// Given operands for an Or, fold the result or return null.
172   Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout &DL,
173                         const TargetLibraryInfo *TLI = nullptr,
174                         const DominatorTree *DT = nullptr,
175                         AssumptionCache *AC = nullptr,
176                         const Instruction *CxtI = nullptr);
177
178   /// Given operands for an Xor, fold the result or return null.
179   Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout &DL,
180                          const TargetLibraryInfo *TLI = nullptr,
181                          const DominatorTree *DT = nullptr,
182                          AssumptionCache *AC = nullptr,
183                          const Instruction *CxtI = nullptr);
184
185   /// Given operands for an ICmpInst, fold the result or return null.
186   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
187                           const DataLayout &DL,
188                           const TargetLibraryInfo *TLI = nullptr,
189                           const DominatorTree *DT = nullptr,
190                           AssumptionCache *AC = nullptr,
191                           const Instruction *CxtI = nullptr);
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 DataLayout &DL,
196                           const TargetLibraryInfo *TLI = nullptr,
197                           const DominatorTree *DT = nullptr,
198                           AssumptionCache *AC = nullptr,
199                           const Instruction *CxtI = nullptr);
200
201   /// Given operands for a SelectInst, fold the result or return null.
202   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
203                             const DataLayout &DL,
204                             const TargetLibraryInfo *TLI = nullptr,
205                             const DominatorTree *DT = nullptr,
206                             AssumptionCache *AC = nullptr,
207                             const Instruction *CxtI = nullptr);
208
209   /// Given operands for a GetElementPtrInst, fold the result or return null.
210   Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
211                          const DataLayout &DL,
212                          const TargetLibraryInfo *TLI = nullptr,
213                          const DominatorTree *DT = nullptr,
214                          AssumptionCache *AC = nullptr,
215                          const Instruction *CxtI = nullptr);
216
217   /// Given operands for an InsertValueInst, fold the result or return null.
218   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
219                                  ArrayRef<unsigned> Idxs, const DataLayout &DL,
220                                  const TargetLibraryInfo *TLI = nullptr,
221                                  const DominatorTree *DT = nullptr,
222                                  AssumptionCache *AC = nullptr,
223                                  const Instruction *CxtI = nullptr);
224
225   /// Given operands for an ExtractValueInst, fold the result or return null.
226   Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
227                                   const DataLayout &DL,
228                                   const TargetLibraryInfo *TLI = nullptr,
229                                   const DominatorTree *DT = nullptr,
230                                   AssumptionCache *AC = nullptr,
231                                   const Instruction *CxtI = nullptr);
232
233   /// Given operands for an ExtractElementInst, fold the result or return null.
234   Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
235                                     const DataLayout &DL,
236                                     const TargetLibraryInfo *TLI = nullptr,
237                                     const DominatorTree *DT = nullptr,
238                                     AssumptionCache *AC = nullptr,
239                                     const Instruction *CxtI = nullptr);
240
241   /// Given operands for a CastInst, fold the result or return null.
242   Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
243                           const DataLayout &DL,
244                           const TargetLibraryInfo *TLI = nullptr,
245                           const DominatorTree *DT = nullptr,
246                           AssumptionCache *AC = nullptr,
247                           const Instruction *CxtI = nullptr);
248
249   //=== Helper functions for higher up the class hierarchy.
250
251
252   /// Given operands for a CmpInst, fold the result or return null.
253   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
254                          const DataLayout &DL,
255                          const TargetLibraryInfo *TLI = nullptr,
256                          const DominatorTree *DT = nullptr,
257                          AssumptionCache *AC = nullptr,
258                          const Instruction *CxtI = nullptr);
259
260   /// Given operands for a BinaryOperator, fold the result or return null.
261   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
262                        const DataLayout &DL,
263                        const TargetLibraryInfo *TLI = nullptr,
264                        const DominatorTree *DT = nullptr,
265                        AssumptionCache *AC = nullptr,
266                        const Instruction *CxtI = nullptr);
267
268   /// Given operands for an FP BinaryOperator, fold the result or return null.
269   /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
270   /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
271   Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
272                          const FastMathFlags &FMF, const DataLayout &DL,
273                          const TargetLibraryInfo *TLI = nullptr,
274                          const DominatorTree *DT = nullptr,
275                          AssumptionCache *AC = nullptr,
276                          const Instruction *CxtI = nullptr);
277
278   /// Given a function and iterators over arguments, fold the result or return
279   /// null.
280   Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
281                       User::op_iterator ArgEnd, const DataLayout &DL,
282                       const TargetLibraryInfo *TLI = nullptr,
283                       const DominatorTree *DT = nullptr,
284                       AssumptionCache *AC = nullptr,
285                       const Instruction *CxtI = nullptr);
286
287   /// Given a function and set of arguments, fold the result or return null.
288   Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const DataLayout &DL,
289                       const TargetLibraryInfo *TLI = nullptr,
290                       const DominatorTree *DT = nullptr,
291                       AssumptionCache *AC = nullptr,
292                       const Instruction *CxtI = nullptr);
293
294   /// See if we can compute a simplified version of this instruction. If not,
295   /// return null.
296   Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
297                              const TargetLibraryInfo *TLI = nullptr,
298                              const DominatorTree *DT = nullptr,
299                              AssumptionCache *AC = nullptr);
300
301   /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
302   ///
303   /// This first performs a normal RAUW of I with SimpleV. It then recursively
304   /// attempts to simplify those users updated by the operation. The 'I'
305   /// instruction must not be equal to the simplified value 'SimpleV'.
306   ///
307   /// The function returns true if any simplifications were performed.
308   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
309                                      const TargetLibraryInfo *TLI = nullptr,
310                                      const DominatorTree *DT = nullptr,
311                                      AssumptionCache *AC = nullptr);
312
313   /// Recursively attempt to simplify an instruction.
314   ///
315   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
316   /// replaces uses of 'I' with the simplified value. It then recurses on each
317   /// of the users impacted. It returns true if any simplifications were
318   /// performed.
319   bool recursivelySimplifyInstruction(Instruction *I,
320                                       const TargetLibraryInfo *TLI = nullptr,
321                                       const DominatorTree *DT = nullptr,
322                                       AssumptionCache *AC = nullptr);
323 } // end namespace llvm
324
325 #endif
326