InstructionSimplify.h revision eb61c920f12cacee38815bf10821d2f294b66f3a
1//===-- InstructionSimplify.h - Fold instructions into simpler forms ------===// 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//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H 20#define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H 21 22namespace llvm { 23 template<typename T> 24 class ArrayRef; 25 class DominatorTree; 26 class Instruction; 27 class DataLayout; 28 struct FastMathFlags; 29 class TargetLibraryInfo; 30 class Type; 31 class Value; 32 33 /// SimplifyAddInst - Given operands for an Add, see if we can 34 /// fold the result. If not, this returns null. 35 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, 36 const DataLayout *TD = 0, 37 const TargetLibraryInfo *TLI = 0, 38 const DominatorTree *DT = 0); 39 40 /// SimplifySubInst - Given operands for a Sub, see if we can 41 /// fold the result. If not, this returns null. 42 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, 43 const DataLayout *TD = 0, 44 const TargetLibraryInfo *TLI = 0, 45 const DominatorTree *DT = 0); 46 47 /// Given operands for an FMul, see if we can fold the result. If not, this 48 /// returns null. 49 Value *SimplifyFMulInst(Value *LHS, Value *RHS, 50 FastMathFlags FMF, 51 const DataLayout *TD = 0, 52 const TargetLibraryInfo *TLI = 0, 53 const DominatorTree *DT = 0); 54 55 /// SimplifyMulInst - Given operands for a Mul, see if we can 56 /// fold the result. If not, this returns null. 57 Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 58 const TargetLibraryInfo *TLI = 0, 59 const DominatorTree *DT = 0); 60 61 /// SimplifySDivInst - Given operands for an SDiv, see if we can 62 /// fold the result. If not, this returns null. 63 Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 64 const TargetLibraryInfo *TLI = 0, 65 const DominatorTree *DT = 0); 66 67 /// SimplifyUDivInst - Given operands for a UDiv, see if we can 68 /// fold the result. If not, this returns null. 69 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 70 const TargetLibraryInfo *TLI = 0, 71 const DominatorTree *DT = 0); 72 73 /// SimplifyFDivInst - Given operands for an FDiv, see if we can 74 /// fold the result. If not, this returns null. 75 Value *SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 76 const TargetLibraryInfo *TLI = 0, 77 const DominatorTree *DT = 0); 78 79 /// SimplifySRemInst - Given operands for an SRem, see if we can 80 /// fold the result. If not, this returns null. 81 Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 82 const TargetLibraryInfo *TLI = 0, 83 const DominatorTree *DT = 0); 84 85 /// SimplifyURemInst - Given operands for a URem, see if we can 86 /// fold the result. If not, this returns null. 87 Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 88 const TargetLibraryInfo *TLI = 0, 89 const DominatorTree *DT = 0); 90 91 /// SimplifyFRemInst - Given operands for an FRem, see if we can 92 /// fold the result. If not, this returns null. 93 Value *SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 94 const TargetLibraryInfo *TLI = 0, 95 const DominatorTree *DT = 0); 96 97 /// SimplifyShlInst - Given operands for a Shl, see if we can 98 /// fold the result. If not, this returns null. 99 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, 100 const DataLayout *TD = 0, 101 const TargetLibraryInfo *TLI = 0, 102 const DominatorTree *DT = 0); 103 104 /// SimplifyLShrInst - Given operands for a LShr, see if we can 105 /// fold the result. If not, this returns null. 106 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, 107 const DataLayout *TD = 0, 108 const TargetLibraryInfo *TLI = 0, 109 const DominatorTree *DT = 0); 110 111 /// SimplifyAShrInst - Given operands for a AShr, see if we can 112 /// fold the result. If not, this returns null. 113 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, 114 const DataLayout *TD = 0, 115 const TargetLibraryInfo *TLI = 0, 116 const DominatorTree *DT = 0); 117 118 /// SimplifyAndInst - Given operands for an And, see if we can 119 /// fold the result. If not, this returns null. 120 Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 121 const TargetLibraryInfo *TLI = 0, 122 const DominatorTree *DT = 0); 123 124 /// SimplifyOrInst - Given operands for an Or, see if we can 125 /// fold the result. If not, this returns null. 126 Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 127 const TargetLibraryInfo *TLI = 0, 128 const DominatorTree *DT = 0); 129 130 /// SimplifyXorInst - Given operands for a Xor, see if we can 131 /// fold the result. If not, this returns null. 132 Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 133 const TargetLibraryInfo *TLI = 0, 134 const DominatorTree *DT = 0); 135 136 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can 137 /// fold the result. If not, this returns null. 138 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, 139 const DataLayout *TD = 0, 140 const TargetLibraryInfo *TLI = 0, 141 const DominatorTree *DT = 0); 142 143 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can 144 /// fold the result. If not, this returns null. 145 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, 146 const DataLayout *TD = 0, 147 const TargetLibraryInfo *TLI = 0, 148 const DominatorTree *DT = 0); 149 150 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold 151 /// the result. If not, this returns null. 152 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, 153 const DataLayout *TD = 0, 154 const TargetLibraryInfo *TLI = 0, 155 const DominatorTree *DT = 0); 156 157 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can 158 /// fold the result. If not, this returns null. 159 Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = 0, 160 const TargetLibraryInfo *TLI = 0, 161 const DominatorTree *DT = 0); 162 163 /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we 164 /// can fold the result. If not, this returns null. 165 Value *SimplifyInsertValueInst(Value *Agg, Value *Val, 166 ArrayRef<unsigned> Idxs, 167 const DataLayout *TD = 0, 168 const TargetLibraryInfo *TLI = 0, 169 const DominatorTree *DT = 0); 170 171 /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold 172 /// the result. If not, this returns null. 173 Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = 0, 174 const TargetLibraryInfo *TLI = 0, 175 const DominatorTree *DT = 0); 176 177 //=== Helper functions for higher up the class hierarchy. 178 179 180 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can 181 /// fold the result. If not, this returns null. 182 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, 183 const DataLayout *TD = 0, 184 const TargetLibraryInfo *TLI = 0, 185 const DominatorTree *DT = 0); 186 187 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can 188 /// fold the result. If not, this returns null. 189 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, 190 const DataLayout *TD = 0, 191 const TargetLibraryInfo *TLI = 0, 192 const DominatorTree *DT = 0); 193 194 /// SimplifyInstruction - See if we can compute a simplified version of this 195 /// instruction. If not, this returns null. 196 Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = 0, 197 const TargetLibraryInfo *TLI = 0, 198 const DominatorTree *DT = 0); 199 200 201 /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses 202 /// recursively. 203 /// 204 /// This first performs a normal RAUW of I with SimpleV. It then recursively 205 /// attempts to simplify those users updated by the operation. The 'I' 206 /// instruction must not be equal to the simplified value 'SimpleV'. 207 /// 208 /// The function returns true if any simplifications were performed. 209 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, 210 const DataLayout *TD = 0, 211 const TargetLibraryInfo *TLI = 0, 212 const DominatorTree *DT = 0); 213 214 /// \brief Recursively attempt to simplify an instruction. 215 /// 216 /// This routine uses SimplifyInstruction to simplify 'I', and if successful 217 /// replaces uses of 'I' with the simplified value. It then recurses on each 218 /// of the users impacted. It returns true if any simplifications were 219 /// performed. 220 bool recursivelySimplifyInstruction(Instruction *I, 221 const DataLayout *TD = 0, 222 const TargetLibraryInfo *TLI = 0, 223 const DominatorTree *DT = 0); 224} // end namespace llvm 225 226#endif 227 228