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
37namespace llvm {
38  template<typename T>
39  class ArrayRef;
40  class DominatorTree;
41  class Instruction;
42  class DataLayout;
43  class FastMathFlags;
44  class TargetLibraryInfo;
45  class Type;
46  class Value;
47
48  /// SimplifyAddInst - Given operands for an Add, see if we can
49  /// fold the result.  If not, this returns null.
50  Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
51                         const DataLayout *TD = nullptr,
52                         const TargetLibraryInfo *TLI = nullptr,
53                         const DominatorTree *DT = nullptr);
54
55  /// SimplifySubInst - Given operands for a Sub, see if we can
56  /// fold the result.  If not, this returns null.
57  Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
58                         const DataLayout *TD = nullptr,
59                         const TargetLibraryInfo *TLI = nullptr,
60                         const DominatorTree *DT = nullptr);
61
62  /// Given operands for an FAdd, see if we can fold the result.  If not, this
63  /// returns null.
64  Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
65                         const DataLayout *TD = nullptr,
66                         const TargetLibraryInfo *TLI = nullptr,
67                         const DominatorTree *DT = nullptr);
68
69  /// Given operands for an FSub, see if we can fold the result.  If not, this
70  /// returns null.
71  Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
72                         const DataLayout *TD = nullptr,
73                         const TargetLibraryInfo *TLI = nullptr,
74                         const DominatorTree *DT = nullptr);
75
76  /// Given operands for an FMul, see if we can fold the result.  If not, this
77  /// returns null.
78  Value *SimplifyFMulInst(Value *LHS, Value *RHS,
79                          FastMathFlags FMF,
80                          const DataLayout *TD = nullptr,
81                          const TargetLibraryInfo *TLI = nullptr,
82                          const DominatorTree *DT = nullptr);
83
84  /// SimplifyMulInst - Given operands for a Mul, see if we can
85  /// fold the result.  If not, this returns null.
86  Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
87                         const TargetLibraryInfo *TLI = nullptr,
88                         const DominatorTree *DT = nullptr);
89
90  /// SimplifySDivInst - Given operands for an SDiv, see if we can
91  /// fold the result.  If not, this returns null.
92  Value *SimplifySDivInst(Value *LHS, Value *RHS,
93                          const DataLayout *TD = nullptr,
94                          const TargetLibraryInfo *TLI = nullptr,
95                          const DominatorTree *DT = nullptr);
96
97  /// SimplifyUDivInst - Given operands for a UDiv, see if we can
98  /// fold the result.  If not, this returns null.
99  Value *SimplifyUDivInst(Value *LHS, Value *RHS,
100                          const DataLayout *TD = nullptr,
101                          const TargetLibraryInfo *TLI = nullptr,
102                          const DominatorTree *DT = nullptr);
103
104  /// SimplifyFDivInst - Given operands for an FDiv, see if we can
105  /// fold the result.  If not, this returns null.
106  Value *SimplifyFDivInst(Value *LHS, Value *RHS,
107                          const DataLayout *TD = nullptr,
108                          const TargetLibraryInfo *TLI = nullptr,
109                          const DominatorTree *DT = nullptr);
110
111  /// SimplifySRemInst - Given operands for an SRem, see if we can
112  /// fold the result.  If not, this returns null.
113  Value *SimplifySRemInst(Value *LHS, Value *RHS,
114                          const DataLayout *TD = nullptr,
115                          const TargetLibraryInfo *TLI = nullptr,
116                          const DominatorTree *DT = nullptr);
117
118  /// SimplifyURemInst - Given operands for a URem, see if we can
119  /// fold the result.  If not, this returns null.
120  Value *SimplifyURemInst(Value *LHS, Value *RHS,
121                          const DataLayout *TD = nullptr,
122                          const TargetLibraryInfo *TLI = nullptr,
123                          const DominatorTree *DT = nullptr);
124
125  /// SimplifyFRemInst - Given operands for an FRem, see if we can
126  /// fold the result.  If not, this returns null.
127  Value *SimplifyFRemInst(Value *LHS, Value *RHS,
128                          const DataLayout *TD = nullptr,
129                          const TargetLibraryInfo *TLI = nullptr,
130                          const DominatorTree *DT = nullptr);
131
132  /// SimplifyShlInst - Given operands for a Shl, see if we can
133  /// fold the result.  If not, this returns null.
134  Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
135                         const DataLayout *TD = nullptr,
136                         const TargetLibraryInfo *TLI = nullptr,
137                         const DominatorTree *DT = nullptr);
138
139  /// SimplifyLShrInst - Given operands for a LShr, see if we can
140  /// fold the result.  If not, this returns null.
141  Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
142                          const DataLayout *TD = nullptr,
143                          const TargetLibraryInfo *TLI = nullptr,
144                          const DominatorTree *DT = nullptr);
145
146  /// SimplifyAShrInst - Given operands for a AShr, see if we can
147  /// fold the result.  If not, this returns null.
148  Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
149                          const DataLayout *TD = nullptr,
150                          const TargetLibraryInfo *TLI = nullptr,
151                          const DominatorTree *DT = nullptr);
152
153  /// SimplifyAndInst - Given operands for an And, see if we can
154  /// fold the result.  If not, this returns null.
155  Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
156                         const TargetLibraryInfo *TLI = nullptr,
157                         const DominatorTree *DT = nullptr);
158
159  /// SimplifyOrInst - Given operands for an Or, see if we can
160  /// fold the result.  If not, this returns null.
161  Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
162                        const TargetLibraryInfo *TLI = nullptr,
163                        const DominatorTree *DT = nullptr);
164
165  /// SimplifyXorInst - Given operands for a Xor, see if we can
166  /// fold the result.  If not, this returns null.
167  Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
168                         const TargetLibraryInfo *TLI = nullptr,
169                         const DominatorTree *DT = nullptr);
170
171  /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
172  /// fold the result.  If not, this returns null.
173  Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
174                          const DataLayout *TD = nullptr,
175                          const TargetLibraryInfo *TLI = nullptr,
176                          const DominatorTree *DT = nullptr);
177
178  /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
179  /// fold the result.  If not, this returns null.
180  Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
181                          const DataLayout *TD = nullptr,
182                          const TargetLibraryInfo *TLI = nullptr,
183                          const DominatorTree *DT = nullptr);
184
185  /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
186  /// the result.  If not, this returns null.
187  Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
188                            const DataLayout *TD = nullptr,
189                            const TargetLibraryInfo *TLI = nullptr,
190                            const DominatorTree *DT = nullptr);
191
192  /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
193  /// fold the result.  If not, this returns null.
194  Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = nullptr,
195                         const TargetLibraryInfo *TLI = nullptr,
196                         const DominatorTree *DT = nullptr);
197
198  /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
199  /// can fold the result.  If not, this returns null.
200  Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
201                                 ArrayRef<unsigned> Idxs,
202                                 const DataLayout *TD = nullptr,
203                                 const TargetLibraryInfo *TLI = nullptr,
204                                 const DominatorTree *DT = nullptr);
205
206  /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
207  /// the result.  If not, this returns null.
208  Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = nullptr,
209                           const TargetLibraryInfo *TLI = nullptr,
210                           const DominatorTree *DT = nullptr);
211
212  //=== Helper functions for higher up the class hierarchy.
213
214
215  /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
216  /// fold the result.  If not, this returns null.
217  Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
218                         const DataLayout *TD = nullptr,
219                         const TargetLibraryInfo *TLI = nullptr,
220                         const DominatorTree *DT = nullptr);
221
222  /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
223  /// fold the result.  If not, this returns null.
224  Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
225                       const DataLayout *TD = nullptr,
226                       const TargetLibraryInfo *TLI = nullptr,
227                       const DominatorTree *DT = nullptr);
228
229  /// \brief Given a function and iterators over arguments, see if we can fold
230  /// the result.
231  ///
232  /// If this call could not be simplified returns null.
233  Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
234                      User::op_iterator ArgEnd, const DataLayout *TD = nullptr,
235                      const TargetLibraryInfo *TLI = nullptr,
236                      const DominatorTree *DT = nullptr);
237
238  /// \brief Given a function and set of arguments, see if we can fold the
239  /// result.
240  ///
241  /// If this call could not be simplified returns null.
242  Value *SimplifyCall(Value *V, ArrayRef<Value *> Args,
243                      const DataLayout *TD = nullptr,
244                      const TargetLibraryInfo *TLI = nullptr,
245                      const DominatorTree *DT = nullptr);
246
247  /// SimplifyInstruction - See if we can compute a simplified version of this
248  /// instruction.  If not, this returns null.
249  Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = nullptr,
250                             const TargetLibraryInfo *TLI = nullptr,
251                             const DominatorTree *DT = nullptr);
252
253
254  /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
255  /// recursively.
256  ///
257  /// This first performs a normal RAUW of I with SimpleV. It then recursively
258  /// attempts to simplify those users updated by the operation. The 'I'
259  /// instruction must not be equal to the simplified value 'SimpleV'.
260  ///
261  /// The function returns true if any simplifications were performed.
262  bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
263                                     const DataLayout *TD = nullptr,
264                                     const TargetLibraryInfo *TLI = nullptr,
265                                     const DominatorTree *DT = nullptr);
266
267  /// \brief Recursively attempt to simplify an instruction.
268  ///
269  /// This routine uses SimplifyInstruction to simplify 'I', and if successful
270  /// replaces uses of 'I' with the simplified value. It then recurses on each
271  /// of the users impacted. It returns true if any simplifications were
272  /// performed.
273  bool recursivelySimplifyInstruction(Instruction *I,
274                                      const DataLayout *TD = nullptr,
275                                      const TargetLibraryInfo *TLI = nullptr,
276                                      const DominatorTree *DT = nullptr);
277} // end namespace llvm
278
279#endif
280
281