LLVM API Documentation
00001 //===-- InstructionSimplify.h - Fold instructions into simpler forms ------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares routines for folding instructions into simpler forms 00011 // that do not require creating new instructions. This does constant folding 00012 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either 00013 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value 00014 // ("and i32 %x, %x" -> "%x"). If the simplification is also an instruction 00015 // then it dominates the original instruction. 00016 // 00017 // These routines implicitly resolve undef uses. The easiest way to be safe when 00018 // using these routines to obtain simplified values for existing instructions is 00019 // to always replace all uses of the instructions with the resulting simplified 00020 // values. This will prevent other code from seeing the same undef uses and 00021 // resolving them to different values. 00022 // 00023 // These routines are designed to tolerate moderately incomplete IR, such as 00024 // instructions that are not connected to basic blocks yet. However, they do 00025 // require that all the IR that they encounter be valid. In particular, they 00026 // require that all non-constant values be defined in the same function, and the 00027 // same call context of that function (and not split between caller and callee 00028 // contexts of a directly recursive call, for example). 00029 // 00030 //===----------------------------------------------------------------------===// 00031 00032 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H 00033 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H 00034 00035 #include "llvm/IR/User.h" 00036 00037 namespace llvm { 00038 template<typename T> 00039 class ArrayRef; 00040 class DominatorTree; 00041 class Instruction; 00042 class DataLayout; 00043 class FastMathFlags; 00044 class TargetLibraryInfo; 00045 class Type; 00046 class Value; 00047 00048 /// SimplifyAddInst - Given operands for an Add, see if we can 00049 /// fold the result. If not, this returns null. 00050 Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, 00051 const DataLayout *TD = 0, 00052 const TargetLibraryInfo *TLI = 0, 00053 const DominatorTree *DT = 0); 00054 00055 /// SimplifySubInst - Given operands for a Sub, see if we can 00056 /// fold the result. If not, this returns null. 00057 Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, 00058 const DataLayout *TD = 0, 00059 const TargetLibraryInfo *TLI = 0, 00060 const DominatorTree *DT = 0); 00061 00062 /// Given operands for an FAdd, see if we can fold the result. If not, this 00063 /// returns null. 00064 Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, 00065 const DataLayout *TD = 0, 00066 const TargetLibraryInfo *TLI = 0, 00067 const DominatorTree *DT = 0); 00068 00069 /// Given operands for an FSub, see if we can fold the result. If not, this 00070 /// returns null. 00071 Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, 00072 const DataLayout *TD = 0, 00073 const TargetLibraryInfo *TLI = 0, 00074 const DominatorTree *DT = 0); 00075 00076 /// Given operands for an FMul, see if we can fold the result. If not, this 00077 /// returns null. 00078 Value *SimplifyFMulInst(Value *LHS, Value *RHS, 00079 FastMathFlags FMF, 00080 const DataLayout *TD = 0, 00081 const TargetLibraryInfo *TLI = 0, 00082 const DominatorTree *DT = 0); 00083 00084 /// SimplifyMulInst - Given operands for a Mul, see if we can 00085 /// fold the result. If not, this returns null. 00086 Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00087 const TargetLibraryInfo *TLI = 0, 00088 const DominatorTree *DT = 0); 00089 00090 /// SimplifySDivInst - Given operands for an SDiv, see if we can 00091 /// fold the result. If not, this returns null. 00092 Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00093 const TargetLibraryInfo *TLI = 0, 00094 const DominatorTree *DT = 0); 00095 00096 /// SimplifyUDivInst - Given operands for a UDiv, see if we can 00097 /// fold the result. If not, this returns null. 00098 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00099 const TargetLibraryInfo *TLI = 0, 00100 const DominatorTree *DT = 0); 00101 00102 /// SimplifyFDivInst - Given operands for an FDiv, see if we can 00103 /// fold the result. If not, this returns null. 00104 Value *SimplifyFDivInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00105 const TargetLibraryInfo *TLI = 0, 00106 const DominatorTree *DT = 0); 00107 00108 /// SimplifySRemInst - Given operands for an SRem, see if we can 00109 /// fold the result. If not, this returns null. 00110 Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00111 const TargetLibraryInfo *TLI = 0, 00112 const DominatorTree *DT = 0); 00113 00114 /// SimplifyURemInst - Given operands for a URem, see if we can 00115 /// fold the result. If not, this returns null. 00116 Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00117 const TargetLibraryInfo *TLI = 0, 00118 const DominatorTree *DT = 0); 00119 00120 /// SimplifyFRemInst - Given operands for an FRem, see if we can 00121 /// fold the result. If not, this returns null. 00122 Value *SimplifyFRemInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00123 const TargetLibraryInfo *TLI = 0, 00124 const DominatorTree *DT = 0); 00125 00126 /// SimplifyShlInst - Given operands for a Shl, see if we can 00127 /// fold the result. If not, this returns null. 00128 Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, 00129 const DataLayout *TD = 0, 00130 const TargetLibraryInfo *TLI = 0, 00131 const DominatorTree *DT = 0); 00132 00133 /// SimplifyLShrInst - Given operands for a LShr, see if we can 00134 /// fold the result. If not, this returns null. 00135 Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact, 00136 const DataLayout *TD = 0, 00137 const TargetLibraryInfo *TLI = 0, 00138 const DominatorTree *DT = 0); 00139 00140 /// SimplifyAShrInst - Given operands for a AShr, see if we can 00141 /// fold the result. If not, this returns null. 00142 Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact, 00143 const DataLayout *TD = 0, 00144 const TargetLibraryInfo *TLI = 0, 00145 const DominatorTree *DT = 0); 00146 00147 /// SimplifyAndInst - Given operands for an And, see if we can 00148 /// fold the result. If not, this returns null. 00149 Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00150 const TargetLibraryInfo *TLI = 0, 00151 const DominatorTree *DT = 0); 00152 00153 /// SimplifyOrInst - Given operands for an Or, see if we can 00154 /// fold the result. If not, this returns null. 00155 Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00156 const TargetLibraryInfo *TLI = 0, 00157 const DominatorTree *DT = 0); 00158 00159 /// SimplifyXorInst - Given operands for a Xor, see if we can 00160 /// fold the result. If not, this returns null. 00161 Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = 0, 00162 const TargetLibraryInfo *TLI = 0, 00163 const DominatorTree *DT = 0); 00164 00165 /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can 00166 /// fold the result. If not, this returns null. 00167 Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, 00168 const DataLayout *TD = 0, 00169 const TargetLibraryInfo *TLI = 0, 00170 const DominatorTree *DT = 0); 00171 00172 /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can 00173 /// fold the result. If not, this returns null. 00174 Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, 00175 const DataLayout *TD = 0, 00176 const TargetLibraryInfo *TLI = 0, 00177 const DominatorTree *DT = 0); 00178 00179 /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold 00180 /// the result. If not, this returns null. 00181 Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, 00182 const DataLayout *TD = 0, 00183 const TargetLibraryInfo *TLI = 0, 00184 const DominatorTree *DT = 0); 00185 00186 /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can 00187 /// fold the result. If not, this returns null. 00188 Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = 0, 00189 const TargetLibraryInfo *TLI = 0, 00190 const DominatorTree *DT = 0); 00191 00192 /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we 00193 /// can fold the result. If not, this returns null. 00194 Value *SimplifyInsertValueInst(Value *Agg, Value *Val, 00195 ArrayRef<unsigned> Idxs, 00196 const DataLayout *TD = 0, 00197 const TargetLibraryInfo *TLI = 0, 00198 const DominatorTree *DT = 0); 00199 00200 /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold 00201 /// the result. If not, this returns null. 00202 Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = 0, 00203 const TargetLibraryInfo *TLI = 0, 00204 const DominatorTree *DT = 0); 00205 00206 //=== Helper functions for higher up the class hierarchy. 00207 00208 00209 /// SimplifyCmpInst - Given operands for a CmpInst, see if we can 00210 /// fold the result. If not, this returns null. 00211 Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, 00212 const DataLayout *TD = 0, 00213 const TargetLibraryInfo *TLI = 0, 00214 const DominatorTree *DT = 0); 00215 00216 /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can 00217 /// fold the result. If not, this returns null. 00218 Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, 00219 const DataLayout *TD = 0, 00220 const TargetLibraryInfo *TLI = 0, 00221 const DominatorTree *DT = 0); 00222 00223 /// \brief Given a function and iterators over arguments, see if we can fold 00224 /// the result. 00225 /// 00226 /// If this call could not be simplified returns null. 00227 Value *SimplifyCall(Value *V, User::op_iterator ArgBegin, 00228 User::op_iterator ArgEnd, const DataLayout *TD = 0, 00229 const TargetLibraryInfo *TLI = 0, 00230 const DominatorTree *DT = 0); 00231 00232 /// \brief Given a function and set of arguments, see if we can fold the 00233 /// result. 00234 /// 00235 /// If this call could not be simplified returns null. 00236 Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, 00237 const DataLayout *TD = 0, 00238 const TargetLibraryInfo *TLI = 0, 00239 const DominatorTree *DT = 0); 00240 00241 /// SimplifyInstruction - See if we can compute a simplified version of this 00242 /// instruction. If not, this returns null. 00243 Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = 0, 00244 const TargetLibraryInfo *TLI = 0, 00245 const DominatorTree *DT = 0); 00246 00247 00248 /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses 00249 /// recursively. 00250 /// 00251 /// This first performs a normal RAUW of I with SimpleV. It then recursively 00252 /// attempts to simplify those users updated by the operation. The 'I' 00253 /// instruction must not be equal to the simplified value 'SimpleV'. 00254 /// 00255 /// The function returns true if any simplifications were performed. 00256 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, 00257 const DataLayout *TD = 0, 00258 const TargetLibraryInfo *TLI = 0, 00259 const DominatorTree *DT = 0); 00260 00261 /// \brief Recursively attempt to simplify an instruction. 00262 /// 00263 /// This routine uses SimplifyInstruction to simplify 'I', and if successful 00264 /// replaces uses of 'I' with the simplified value. It then recurses on each 00265 /// of the users impacted. It returns true if any simplifications were 00266 /// performed. 00267 bool recursivelySimplifyInstruction(Instruction *I, 00268 const DataLayout *TD = 0, 00269 const TargetLibraryInfo *TLI = 0, 00270 const DominatorTree *DT = 0); 00271 } // end namespace llvm 00272 00273 #endif 00274