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                           const 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                           FastMathFlags FMF, 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(Type *SrcTy, ArrayRef<Value *> Ops,
00233                          const DataLayout &DL,
00234                          const TargetLibraryInfo *TLI = nullptr,
00235                          const DominatorTree *DT = nullptr,
00236                          AssumptionCache *AC = nullptr,
00237                          const Instruction *CxtI = nullptr);
00238 
00239   /// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
00240   /// can fold the result.  If not, this returns null.
00241   Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
00242                                  ArrayRef<unsigned> Idxs, const DataLayout &DL,
00243                                  const TargetLibraryInfo *TLI = nullptr,
00244                                  const DominatorTree *DT = nullptr,
00245                                  AssumptionCache *AC = nullptr,
00246                                  const Instruction *CxtI = nullptr);
00247 
00248   /// \brief Given operands for an ExtractValueInst, see if we can fold the
00249   /// result.  If not, this returns null.
00250   Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
00251                                   const DataLayout &DL,
00252                                   const TargetLibraryInfo *TLI = nullptr,
00253                                   const DominatorTree *DT = nullptr,
00254                                   AssumptionCache *AC = nullptr,
00255                                   const Instruction *CxtI = nullptr);
00256 
00257   /// \brief Given operands for an ExtractElementInst, see if we can fold the
00258   /// result.  If not, this returns null.
00259   Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
00260                                     const DataLayout &DL,
00261                                     const TargetLibraryInfo *TLI = nullptr,
00262                                     const DominatorTree *DT = nullptr,
00263                                     AssumptionCache *AC = nullptr,
00264                                     const Instruction *CxtI = nullptr);
00265 
00266   /// SimplifyTruncInst - Given operands for an TruncInst, see if we can fold
00267   /// the result.  If not, this returns null.
00268   Value *SimplifyTruncInst(Value *Op, Type *Ty, const DataLayout &DL,
00269                            const TargetLibraryInfo *TLI = nullptr,
00270                            const DominatorTree *DT = nullptr,
00271                            AssumptionCache *AC = nullptr,
00272                            const Instruction *CxtI = nullptr);
00273 
00274   //=== Helper functions for higher up the class hierarchy.
00275 
00276 
00277   /// SimplifyCmpInst - Given operands for a CmpInst, see if we can
00278   /// fold the result.  If not, this returns null.
00279   Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
00280                          const DataLayout &DL,
00281                          const TargetLibraryInfo *TLI = nullptr,
00282                          const DominatorTree *DT = nullptr,
00283                          AssumptionCache *AC = nullptr,
00284                          const Instruction *CxtI = nullptr);
00285 
00286   /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
00287   /// fold the result.  If not, this returns null.
00288   Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
00289                        const DataLayout &DL,
00290                        const TargetLibraryInfo *TLI = nullptr,
00291                        const DominatorTree *DT = nullptr,
00292                        AssumptionCache *AC = nullptr,
00293                        const Instruction *CxtI = nullptr);
00294   /// SimplifyFPBinOp - Given operands for a BinaryOperator, see if we can
00295   /// fold the result.  If not, this returns null.
00296   /// In contrast to SimplifyBinOp, try to use FastMathFlag when folding the
00297   /// result. In case we don't need FastMathFlags, simply fall to SimplifyBinOp.
00298   Value *SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
00299                          const FastMathFlags &FMF, const DataLayout &DL,
00300                          const TargetLibraryInfo *TLI = nullptr,
00301                          const DominatorTree *DT = nullptr,
00302                          AssumptionCache *AC = nullptr,
00303                          const Instruction *CxtI = nullptr);
00304 
00305   /// \brief Given a function and iterators over arguments, see if we can fold
00306   /// the result.
00307   ///
00308   /// If this call could not be simplified returns null.
00309   Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
00310                       User::op_iterator ArgEnd, const DataLayout &DL,
00311                       const TargetLibraryInfo *TLI = nullptr,
00312                       const DominatorTree *DT = nullptr,
00313                       AssumptionCache *AC = nullptr,
00314                       const Instruction *CxtI = nullptr);
00315 
00316   /// \brief Given a function and set of arguments, see if we can fold the
00317   /// result.
00318   ///
00319   /// If this call could not be simplified returns null.
00320   Value *SimplifyCall(Value *V, ArrayRef<Value *> Args, const DataLayout &DL,
00321                       const TargetLibraryInfo *TLI = nullptr,
00322                       const DominatorTree *DT = nullptr,
00323                       AssumptionCache *AC = nullptr,
00324                       const Instruction *CxtI = nullptr);
00325 
00326   /// SimplifyInstruction - See if we can compute a simplified version of this
00327   /// instruction.  If not, this returns null.
00328   Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
00329                              const TargetLibraryInfo *TLI = nullptr,
00330                              const DominatorTree *DT = nullptr,
00331                              AssumptionCache *AC = nullptr);
00332 
00333   /// \brief Replace all uses of 'I' with 'SimpleV' and simplify the uses
00334   /// recursively.
00335   ///
00336   /// This first performs a normal RAUW of I with SimpleV. It then recursively
00337   /// attempts to simplify those users updated by the operation. The 'I'
00338   /// instruction must not be equal to the simplified value 'SimpleV'.
00339   ///
00340   /// The function returns true if any simplifications were performed.
00341   bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
00342                                      const TargetLibraryInfo *TLI = nullptr,
00343                                      const DominatorTree *DT = nullptr,
00344                                      AssumptionCache *AC = nullptr);
00345 
00346   /// \brief Recursively attempt to simplify an instruction.
00347   ///
00348   /// This routine uses SimplifyInstruction to simplify 'I', and if successful
00349   /// replaces uses of 'I' with the simplified value. It then recurses on each
00350   /// of the users impacted. It returns true if any simplifications were
00351   /// performed.
00352   bool recursivelySimplifyInstruction(Instruction *I,
00353                                       const TargetLibraryInfo *TLI = nullptr,
00354                                       const DominatorTree *DT = nullptr,
00355                                       AssumptionCache *AC = nullptr);
00356 } // end namespace llvm
00357 
00358 #endif
00359