LLVM  mainline
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 AssumptionCache;
00041   class DominatorTree;
00042   class Instruction;
00043   class DataLayout;
00044   class FastMathFlags;
00045   class TargetLibraryInfo;
00046   class Type;
00047   class Value;
00048 
00049   /// SimplifyAddInst - Given operands for an Add, see if we can
00050   /// fold the result.  If not, this returns null.
00051   Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
00052                          const DataLayout &DL,
00053                          const TargetLibraryInfo *TLI = nullptr,
00054                          const DominatorTree *DT = nullptr,
00055                          AssumptionCache *AC = nullptr,
00056                          const Instruction *CxtI = nullptr);
00057 
00058   /// SimplifySubInst - Given operands for a Sub, see if we can
00059   /// fold the result.  If not, this returns null.
00060   Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
00061                          const DataLayout &DL,
00062                          const TargetLibraryInfo *TLI = nullptr,
00063                          const DominatorTree *DT = nullptr,
00064                          AssumptionCache *AC = nullptr,
00065                          const Instruction *CxtI = nullptr);
00066 
00067   /// Given operands for an FAdd, see if we can fold the result.  If not, this
00068   /// returns null.
00069   Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
00070                           const DataLayout &DL,
00071                           const TargetLibraryInfo *TLI = nullptr,
00072                           const DominatorTree *DT = nullptr,
00073                           AssumptionCache *AC = nullptr,
00074                           const Instruction *CxtI = nullptr);
00075 
00076   /// Given operands for an FSub, see if we can fold the result.  If not, this
00077   /// returns null.
00078   Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
00079                           const DataLayout &DL,
00080                           const TargetLibraryInfo *TLI = nullptr,
00081                           const DominatorTree *DT = nullptr,
00082                           AssumptionCache *AC = nullptr,
00083                           const Instruction *CxtI = nullptr);
00084 
00085   /// Given operands for an FMul, see if we can fold the result.  If not, this
00086   /// returns null.
00087   Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
00088                           const DataLayout &DL,
00089                           const TargetLibraryInfo *TLI = nullptr,
00090                           const DominatorTree *DT = nullptr,
00091                           AssumptionCache *AC = nullptr,
00092                           const Instruction *CxtI = nullptr);
00093 
00094   /// SimplifyMulInst - Given operands for a Mul, see if we can
00095   /// fold the result.  If not, this returns null.
00096   Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL,
00097                          const TargetLibraryInfo *TLI = nullptr,
00098                          const DominatorTree *DT = nullptr,
00099                          AssumptionCache *AC = nullptr,
00100                          const Instruction *CxtI = nullptr);
00101 
00102   /// SimplifySDivInst - Given operands for an SDiv, see if we can
00103   /// fold the result.  If not, this returns null.
00104   Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
00105                           const TargetLibraryInfo *TLI = nullptr,
00106                           const DominatorTree *DT = nullptr,
00107                           AssumptionCache *AC = nullptr,
00108                           const Instruction *CxtI = nullptr);
00109 
00110   /// SimplifyUDivInst - Given operands for a UDiv, see if we can
00111   /// fold the result.  If not, this returns null.
00112   Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
00113                           const TargetLibraryInfo *TLI = nullptr,
00114                           const DominatorTree *DT = nullptr,
00115                           AssumptionCache *AC = nullptr,
00116                           const Instruction *CxtI = nullptr);
00117 
00118   /// SimplifyFDivInst - Given operands for an FDiv, see if we can
00119   /// fold the result.  If not, this returns null.
00120   Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
00121                           const DataLayout &DL,
00122                           const TargetLibraryInfo *TLI = nullptr,
00123                           const DominatorTree *DT = nullptr,
00124                           AssumptionCache *AC = nullptr,
00125                           const Instruction *CxtI = nullptr);
00126 
00127   /// SimplifySRemInst - Given operands for an SRem, see if we can
00128   /// fold the result.  If not, this returns null.
00129   Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL,
00130                           const TargetLibraryInfo *TLI = nullptr,
00131                           const DominatorTree *DT = nullptr,
00132                           AssumptionCache *AC = nullptr,
00133                           const Instruction *CxtI = nullptr);
00134 
00135   /// SimplifyURemInst - Given operands for a URem, see if we can
00136   /// fold the result.  If not, this returns null.
00137   Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL,
00138                           const TargetLibraryInfo *TLI = nullptr,
00139                           const DominatorTree *DT = nullptr,
00140                           AssumptionCache *AC = nullptr,
00141                           const Instruction *CxtI = nullptr);
00142 
00143   /// SimplifyFRemInst - Given operands for an FRem, see if we can
00144   /// fold the result.  If not, this returns null.
00145   Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
00146                           const DataLayout &DL,
00147                           const TargetLibraryInfo *TLI = nullptr,
00148                           const DominatorTree *DT = nullptr,
00149                           AssumptionCache *AC = nullptr,
00150                           const Instruction *CxtI = nullptr);
00151 
00152   /// SimplifyShlInst - Given operands for a Shl, see if we can
00153   /// fold the result.  If not, this returns null.
00154   Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
00155                          const DataLayout &DL,
00156                          const TargetLibraryInfo *TLI = nullptr,
00157                          const DominatorTree *DT = nullptr,
00158                          AssumptionCache *AC = nullptr,
00159                          const Instruction *CxtI = nullptr);
00160 
00161   /// SimplifyLShrInst - Given operands for a LShr, see if we can
00162   /// fold the result.  If not, this returns null.
00163   Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
00164                           const DataLayout &DL,
00165                           const TargetLibraryInfo *TLI = nullptr,
00166                           const DominatorTree *DT = nullptr,
00167                           AssumptionCache *AC = nullptr,
00168                           const Instruction *CxtI = nullptr);
00169 
00170   /// SimplifyAShrInst - Given operands for a AShr, see if we can
00171   /// fold the result.  If not, this returns null.
00172   Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
00173                           const DataLayout &DL,
00174                           const TargetLibraryInfo *TLI = nullptr,
00175                           const DominatorTree *DT = nullptr,
00176                           AssumptionCache *AC = nullptr,
00177                           const Instruction *CxtI = nullptr);
00178 
00179   /// SimplifyAndInst - Given operands for an And, see if we can
00180   /// fold the result.  If not, this returns null.
00181   Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout &DL,
00182                          const TargetLibraryInfo *TLI = nullptr,
00183                          const DominatorTree *DT = nullptr,
00184                          AssumptionCache *AC = nullptr,
00185                          const Instruction *CxtI = nullptr);
00186 
00187   /// SimplifyOrInst - Given operands for an Or, see if we can
00188   /// fold the result.  If not, this returns null.
00189   Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout &DL,
00190                         const TargetLibraryInfo *TLI = nullptr,
00191                         const DominatorTree *DT = nullptr,
00192                         AssumptionCache *AC = nullptr,
00193                         const Instruction *CxtI = nullptr);
00194 
00195   /// SimplifyXorInst - Given operands for a Xor, see if we can
00196   /// fold the result.  If not, this returns null.
00197   Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout &DL,
00198                          const TargetLibraryInfo *TLI = nullptr,
00199                          const DominatorTree *DT = nullptr,
00200                          AssumptionCache *AC = nullptr,
00201                          const Instruction *CxtI = nullptr);
00202 
00203   /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
00204   /// fold the result.  If not, this returns null.
00205   Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00206                           const DataLayout &DL,
00207                           const TargetLibraryInfo *TLI = nullptr,
00208                           const DominatorTree *DT = nullptr,
00209                           AssumptionCache *AC = nullptr,
00210                           Instruction *CxtI = nullptr);
00211 
00212   /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
00213   /// fold the result.  If not, this returns null.
00214   Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00215                           const DataLayout &DL,
00216                           const TargetLibraryInfo *TLI = nullptr,
00217                           const DominatorTree *DT = nullptr,
00218                           AssumptionCache *AC = nullptr,
00219                           const Instruction *CxtI = nullptr);
00220 
00221   /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
00222   /// the result.  If not, this returns null.
00223   Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
00224                             const DataLayout &DL,
00225                             const TargetLibraryInfo *TLI = nullptr,
00226                             const DominatorTree *DT = nullptr,
00227                             AssumptionCache *AC = nullptr,
00228                             const Instruction *CxtI = nullptr);
00229 
00230   /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
00231   /// fold the result.  If not, this returns null.
00232   Value *SimplifyGEPInst(ArrayRef<Value *> Ops, const DataLayout &DL,
00233                          const TargetLibraryInfo *TLI = nullptr,
00234                          const DominatorTree *DT = nullptr,
00235                          AssumptionCache *AC = nullptr,
00236                          const Instruction *CxtI = nullptr);
00237 
00238   /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
00239   /// can fold the result.  If not, this returns null.
00240   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
00241                                  ArrayRef<unsigned> Idxs, const DataLayout &DL,
00242                                  const TargetLibraryInfo *TLI = nullptr,
00243                                  const DominatorTree *DT = nullptr,
00244                                  AssumptionCache *AC = nullptr,
00245                                  const Instruction *CxtI = nullptr);
00246 
00247   /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
00248   /// the result.  If not, this returns null.
00249   Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout &DL,
00250                            const TargetLibraryInfo *TLI = nullptr,
00251                            const DominatorTree *DT = nullptr,
00252                            AssumptionCache *AC = nullptr,
00253                            const Instruction *CxtI = nullptr);
00254 
00255   //=== Helper functions for higher up the class hierarchy.
00256 
00257 
00258   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
00259   /// fold the result.  If not, this returns null.
00260   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00261                          const DataLayout &DL,
00262                          const TargetLibraryInfo *TLI = nullptr,
00263                          const DominatorTree *DT = nullptr,
00264                          AssumptionCache *AC = nullptr,
00265                          const Instruction *CxtI = nullptr);
00266 
00267   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
00268   /// fold the result.  If not, this returns null.
00269   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
00270                        const DataLayout &DL,
00271                        const TargetLibraryInfo *TLI = nullptr,
00272                        const DominatorTree *DT = nullptr,
00273                        AssumptionCache *AC = nullptr,
00274                        const Instruction *CxtI = nullptr);
00275   /// SimplifyFPBinOp - Given operands for a BinaryOperator, see if we can
00276   /// fold the result.  If not, this returns null.
00277   /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
00278   /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
00279   Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
00280                          const FastMathFlags &FMF, const DataLayout &DL,
00281                          const TargetLibraryInfo *TLI = nullptr,
00282                          const DominatorTree *DT = nullptr,
00283                          AssumptionCache *AC = nullptr,
00284                          const Instruction *CxtI = nullptr);
00285 
00286   /// \brief Given a function and iterators over arguments, see if we can fold
00287   /// the result.
00288   ///
00289   /// If this call could not be simplified returns null.
00290   Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
00291                       User::op_iterator ArgEnd, const DataLayout &DL,
00292                       const TargetLibraryInfo *TLI = nullptr,
00293                       const DominatorTree *DT = nullptr,
00294                       AssumptionCache *AC = nullptr,
00295                       const Instruction *CxtI = nullptr);
00296 
00297   /// \brief Given a function and set of arguments, see if we can fold the
00298   /// result.
00299   ///
00300   /// If this call could not be simplified returns null.
00301   Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const DataLayout &DL,
00302                       const TargetLibraryInfo *TLI = nullptr,
00303                       const DominatorTree *DT = nullptr,
00304                       AssumptionCache *AC = nullptr,
00305                       const Instruction *CxtI = nullptr);
00306 
00307   /// SimplifyInstruction - See if we can compute a simplified version of this
00308   /// instruction.  If not, this returns null.
00309   Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
00310                              const TargetLibraryInfo *TLI = nullptr,
00311                              const DominatorTree *DT = nullptr,
00312                              AssumptionCache *AC = nullptr);
00313 
00314   /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
00315   /// recursively.
00316   ///
00317   /// This first performs a normal RAUW of I with SimpleV. It then recursively
00318   /// attempts to simplify those users updated by the operation. The 'I'
00319   /// instruction must not be equal to the simplified value 'SimpleV'.
00320   ///
00321   /// The function returns true if any simplifications were performed.
00322   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
00323                                      const TargetLibraryInfo *TLI = nullptr,
00324                                      const DominatorTree *DT = nullptr,
00325                                      AssumptionCache *AC = nullptr);
00326 
00327   /// \brief Recursively attempt to simplify an instruction.
00328   ///
00329   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
00330   /// replaces uses of 'I' with the simplified value. It then recurses on each
00331   /// of the users impacted. It returns true if any simplifications were
00332   /// performed.
00333   bool recursivelySimplifyInstruction(Instruction *I,
00334                                       const TargetLibraryInfo *TLI = nullptr,
00335                                       const DominatorTree *DT = nullptr,
00336                                       AssumptionCache *AC = nullptr);
00337 } // end namespace llvm
00338 
00339 #endif
00340