InstructionSimplify.h revision 19f14dcf6af11b5520acfc5c2fd4100ec1972b44
11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//===-- InstructionSimplify.h - Fold instructions into simpler forms ------===//
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//                     The LLVM Compiler Infrastructure
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// This file is distributed under the University of Illinois Open Source
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// License. See LICENSE.TXT for details.
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//===----------------------------------------------------------------------===//
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// This file declares routines for folding instructions into simpler forms
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// that do not require creating new instructions.  This does constant folding
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// ("and i32 %x, %x" -> "%x").  If the simplification is also an instruction
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// then it dominates the original instruction.
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//===----------------------------------------------------------------------===//
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace llvm {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class DominatorTree;
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class Instruction;
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class Value;
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class TargetData;
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// SimplifyAddInst - Given operands for an Add, see if we can
29  /// fold the result.  If not, this returns null.
30  Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
31                         const TargetData *TD = 0, const DominatorTree *DT = 0);
32
33  /// SimplifySubInst - Given operands for a Sub, see if we can
34  /// fold the result.  If not, this returns null.
35  Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
36                         const TargetData *TD = 0, const DominatorTree *DT = 0);
37
38  /// SimplifyAndInst - Given operands for an And, see if we can
39  /// fold the result.  If not, this returns null.
40  Value *SimplifyAndInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
41                         const DominatorTree *DT = 0);
42
43  /// SimplifyMulInst - Given operands for a Mul, see if we can
44  /// fold the result.  If not, this returns null.
45  Value *SimplifyMulInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
46                         const DominatorTree *DT = 0);
47
48  /// SimplifyOrInst - Given operands for an Or, see if we can
49  /// fold the result.  If not, this returns null.
50  Value *SimplifyOrInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
51                        const DominatorTree *DT = 0);
52
53  /// SimplifyXorInst - Given operands for a Xor, see if we can
54  /// fold the result.  If not, this returns null.
55  Value *SimplifyXorInst(Value *LHS, Value *RHS, const TargetData *TD = 0,
56                         const DominatorTree *DT = 0);
57
58  /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
59  /// fold the result.  If not, this returns null.
60  Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
61                          const TargetData *TD = 0,
62                          const DominatorTree *DT = 0);
63
64  /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
65  /// fold the result.  If not, this returns null.
66  Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
67                          const TargetData *TD = 0,
68                          const DominatorTree *DT = 0);
69
70  /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
71  /// the result.  If not, this returns null.
72  Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
73                            const TargetData *TD = 0,
74                            const DominatorTree *DT = 0);
75
76  /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
77  /// fold the result.  If not, this returns null.
78  Value *SimplifyGEPInst(Value * const *Ops, unsigned NumOps,
79                         const TargetData *TD = 0, const DominatorTree *DT = 0);
80
81  //=== Helper functions for higher up the class hierarchy.
82
83
84  /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
85  /// fold the result.  If not, this returns null.
86  Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
87                         const TargetData *TD = 0, const DominatorTree *DT = 0);
88
89  /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
90  /// fold the result.  If not, this returns null.
91  Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
92                       const TargetData *TD = 0, const DominatorTree *DT = 0);
93
94  /// SimplifyInstruction - See if we can compute a simplified version of this
95  /// instruction.  If not, this returns null.
96  Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0,
97                             const DominatorTree *DT = 0);
98
99
100  /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
101  /// delete the From instruction.  In addition to a basic RAUW, this does a
102  /// recursive simplification of the updated instructions.  This catches
103  /// things where one simplification exposes other opportunities.  This only
104  /// simplifies and deletes scalar operations, it does not change the CFG.
105  ///
106  void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
107                                 const TargetData *TD = 0,
108                                 const DominatorTree *DT = 0);
109} // end namespace llvm
110
111#endif
112
113