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