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