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