LLVM API Documentation

InstructionSimplify.h
Go to the documentation of this file.
00001 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- C++ -*-===//
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 = nullptr,
00052                          const TargetLibraryInfo *TLI = nullptr,
00053                          const DominatorTree *DT = nullptr);
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 = nullptr,
00059                          const TargetLibraryInfo *TLI = nullptr,
00060                          const DominatorTree *DT = nullptr);
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 = nullptr,
00066                          const TargetLibraryInfo *TLI = nullptr,
00067                          const DominatorTree *DT = nullptr);
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 = nullptr,
00073                          const TargetLibraryInfo *TLI = nullptr,
00074                          const DominatorTree *DT = nullptr);
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 = nullptr,
00081                           const TargetLibraryInfo *TLI = nullptr,
00082                           const DominatorTree *DT = nullptr);
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 = nullptr,
00087                          const TargetLibraryInfo *TLI = nullptr,
00088                          const DominatorTree *DT = nullptr);
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,
00093                           const DataLayout *TD = nullptr,
00094                           const TargetLibraryInfo *TLI = nullptr,
00095                           const DominatorTree *DT = nullptr);
00096 
00097   /// SimplifyUDivInst - Given operands for a UDiv, see if we can
00098   /// fold the result.  If not, this returns null.
00099   Value *SimplifyUDivInst(Value *LHS, Value *RHS,
00100                           const DataLayout *TD = nullptr,
00101                           const TargetLibraryInfo *TLI = nullptr,
00102                           const DominatorTree *DT = nullptr);
00103 
00104   /// SimplifyFDivInst - Given operands for an FDiv, see if we can
00105   /// fold the result.  If not, this returns null.
00106   Value *SimplifyFDivInst(Value *LHS, Value *RHS,
00107                           const DataLayout *TD = nullptr,
00108                           const TargetLibraryInfo *TLI = nullptr,
00109                           const DominatorTree *DT = nullptr);
00110 
00111   /// SimplifySRemInst - Given operands for an SRem, see if we can
00112   /// fold the result.  If not, this returns null.
00113   Value *SimplifySRemInst(Value *LHS, Value *RHS,
00114                           const DataLayout *TD = nullptr,
00115                           const TargetLibraryInfo *TLI = nullptr,
00116                           const DominatorTree *DT = nullptr);
00117 
00118   /// SimplifyURemInst - Given operands for a URem, see if we can
00119   /// fold the result.  If not, this returns null.
00120   Value *SimplifyURemInst(Value *LHS, Value *RHS,
00121                           const DataLayout *TD = nullptr,
00122                           const TargetLibraryInfo *TLI = nullptr,
00123                           const DominatorTree *DT = nullptr);
00124 
00125   /// SimplifyFRemInst - Given operands for an FRem, see if we can
00126   /// fold the result.  If not, this returns null.
00127   Value *SimplifyFRemInst(Value *LHS, Value *RHS,
00128                           const DataLayout *TD = nullptr,
00129                           const TargetLibraryInfo *TLI = nullptr,
00130                           const DominatorTree *DT = nullptr);
00131 
00132   /// SimplifyShlInst - Given operands for a Shl, see if we can
00133   /// fold the result.  If not, this returns null.
00134   Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
00135                          const DataLayout *TD = nullptr,
00136                          const TargetLibraryInfo *TLI = nullptr,
00137                          const DominatorTree *DT = nullptr);
00138 
00139   /// SimplifyLShrInst - Given operands for a LShr, see if we can
00140   /// fold the result.  If not, this returns null.
00141   Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
00142                           const DataLayout *TD = nullptr,
00143                           const TargetLibraryInfo *TLI = nullptr,
00144                           const DominatorTree *DT = nullptr);
00145 
00146   /// SimplifyAShrInst - Given operands for a AShr, see if we can
00147   /// fold the result.  If not, this returns null.
00148   Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
00149                           const DataLayout *TD = nullptr,
00150                           const TargetLibraryInfo *TLI = nullptr,
00151                           const DominatorTree *DT = nullptr);
00152 
00153   /// SimplifyAndInst - Given operands for an And, see if we can
00154   /// fold the result.  If not, this returns null.
00155   Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
00156                          const TargetLibraryInfo *TLI = nullptr,
00157                          const DominatorTree *DT = nullptr);
00158 
00159   /// SimplifyOrInst - Given operands for an Or, see if we can
00160   /// fold the result.  If not, this returns null.
00161   Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
00162                         const TargetLibraryInfo *TLI = nullptr,
00163                         const DominatorTree *DT = nullptr);
00164 
00165   /// SimplifyXorInst - Given operands for a Xor, see if we can
00166   /// fold the result.  If not, this returns null.
00167   Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout *TD = nullptr,
00168                          const TargetLibraryInfo *TLI = nullptr,
00169                          const DominatorTree *DT = nullptr);
00170 
00171   /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
00172   /// fold the result.  If not, this returns null.
00173   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00174                           const DataLayout *TD = nullptr,
00175                           const TargetLibraryInfo *TLI = nullptr,
00176                           const DominatorTree *DT = nullptr);
00177 
00178   /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
00179   /// fold the result.  If not, this returns null.
00180   Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00181                           const DataLayout *TD = nullptr,
00182                           const TargetLibraryInfo *TLI = nullptr,
00183                           const DominatorTree *DT = nullptr);
00184 
00185   /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
00186   /// the result.  If not, this returns null.
00187   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
00188                             const DataLayout *TD = nullptr,
00189                             const TargetLibraryInfo *TLI = nullptr,
00190                             const DominatorTree *DT = nullptr);
00191 
00192   /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
00193   /// fold the result.  If not, this returns null.
00194   Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout *TD = nullptr,
00195                          const TargetLibraryInfo *TLI = nullptr,
00196                          const DominatorTree *DT = nullptr);
00197 
00198   /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
00199   /// can fold the result.  If not, this returns null.
00200   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
00201                                  ArrayRef<unsigned> Idxs,
00202                                  const DataLayout *TD = nullptr,
00203                                  const TargetLibraryInfo *TLI = nullptr,
00204                                  const DominatorTree *DT = nullptr);
00205 
00206   /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
00207   /// the result.  If not, this returns null.
00208   Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout *TD = nullptr,
00209                            const TargetLibraryInfo *TLI = nullptr,
00210                            const DominatorTree *DT = nullptr);
00211 
00212   //=== Helper functions for higher up the class hierarchy.
00213 
00214 
00215   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
00216   /// fold the result.  If not, this returns null.
00217   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00218                          const DataLayout *TD = nullptr,
00219                          const TargetLibraryInfo *TLI = nullptr,
00220                          const DominatorTree *DT = nullptr);
00221 
00222   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
00223   /// fold the result.  If not, this returns null.
00224   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
00225                        const DataLayout *TD = nullptr,
00226                        const TargetLibraryInfo *TLI = nullptr,
00227                        const DominatorTree *DT = nullptr);
00228 
00229   /// \brief Given a function and iterators over arguments, see if we can fold
00230   /// the result.
00231   ///
00232   /// If this call could not be simplified returns null.
00233   Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
00234                       User::op_iterator ArgEnd, const DataLayout *TD = nullptr,
00235                       const TargetLibraryInfo *TLI = nullptr,
00236                       const DominatorTree *DT = nullptr);
00237 
00238   /// \brief Given a function and set of arguments, see if we can fold the
00239   /// result.
00240   ///
00241   /// If this call could not be simplified returns null.
00242   Value *SimplifyCall(Value *V, ArrayRef<Value *> Args,
00243                       const DataLayout *TD = nullptr,
00244                       const TargetLibraryInfo *TLI = nullptr,
00245                       const DominatorTree *DT = nullptr);
00246 
00247   /// SimplifyInstruction - See if we can compute a simplified version of this
00248   /// instruction.  If not, this returns null.
00249   Value *SimplifyInstruction(Instruction *I, const DataLayout *TD = nullptr,
00250                              const TargetLibraryInfo *TLI = nullptr,
00251                              const DominatorTree *DT = nullptr);
00252 
00253 
00254   /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
00255   /// recursively.
00256   ///
00257   /// This first performs a normal RAUW of I with SimpleV. It then recursively
00258   /// attempts to simplify those users updated by the operation. The 'I'
00259   /// instruction must not be equal to the simplified value 'SimpleV'.
00260   ///
00261   /// The function returns true if any simplifications were performed.
00262   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
00263                                      const DataLayout *TD = nullptr,
00264                                      const TargetLibraryInfo *TLI = nullptr,
00265                                      const DominatorTree *DT = nullptr);
00266 
00267   /// \brief Recursively attempt to simplify an instruction.
00268   ///
00269   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
00270   /// replaces uses of 'I' with the simplified value. It then recurses on each
00271   /// of the users impacted. It returns true if any simplifications were
00272   /// performed.
00273   bool recursivelySimplifyInstruction(Instruction *I,
00274                                       const DataLayout *TD = nullptr,
00275                                       const TargetLibraryInfo *TLI = nullptr,
00276                                       const DominatorTree *DT = nullptr);
00277 } // end namespace llvm
00278 
00279 #endif
00280