LLVM  mainline
ValueTracking.h
Go to the documentation of this file.
00001 //===- llvm/Analysis/ValueTracking.h - Walk computations --------*- 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 contains routines that help analyze properties that chains of
00011 // computations have.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ANALYSIS_VALUETRACKING_H
00016 #define LLVM_ANALYSIS_VALUETRACKING_H
00017 
00018 #include "llvm/ADT/ArrayRef.h"
00019 #include "llvm/IR/Instruction.h"
00020 #include "llvm/Support/DataTypes.h"
00021 
00022 namespace llvm {
00023   class Value;
00024   class Instruction;
00025   class APInt;
00026   class DataLayout;
00027   class StringRef;
00028   class MDNode;
00029   class AssumptionCache;
00030   class DominatorTree;
00031   class TargetLibraryInfo;
00032   class LoopInfo;
00033 
00034   /// Determine which bits of V are known to be either zero or one and return
00035   /// them in the KnownZero/KnownOne bit sets.
00036   ///
00037   /// This function is defined on values with integer type, values with pointer
00038   /// type, and vectors of integers.  In the case
00039   /// where V is a vector, the known zero and known one values are the
00040   /// same width as the vector element, and the bit is set only if it is true
00041   /// for all of the elements in the vector.
00042   void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
00043                         const DataLayout &DL, unsigned Depth = 0,
00044                         AssumptionCache *AC = nullptr,
00045                         const Instruction *CxtI = nullptr,
00046                         const DominatorTree *DT = nullptr);
00047   /// Compute known bits from the range metadata.
00048   /// \p KnownZero the set of bits that are known to be zero
00049   void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
00050                                          APInt &KnownZero);
00051   /// Returns true if LHS and RHS have no common bits set.
00052   bool haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL,
00053                            AssumptionCache *AC = nullptr,
00054                            const Instruction *CxtI = nullptr,
00055                            const DominatorTree *DT = nullptr);
00056 
00057   /// ComputeSignBit - Determine whether the sign bit is known to be zero or
00058   /// one.  Convenience wrapper around computeKnownBits.
00059   void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
00060                       const DataLayout &DL, unsigned Depth = 0,
00061                       AssumptionCache *AC = nullptr,
00062                       const Instruction *CxtI = nullptr,
00063                       const DominatorTree *DT = nullptr);
00064 
00065   /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have
00066   /// exactly one bit set when defined. For vectors return true if every
00067   /// element is known to be a power of two when defined.  Supports values with
00068   /// integer or pointer type and vectors of integers.  If 'OrZero' is set then
00069   /// returns true if the given value is either a power of two or zero.
00070   bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL,
00071                               bool OrZero = false, unsigned Depth = 0,
00072                               AssumptionCache *AC = nullptr,
00073                               const Instruction *CxtI = nullptr,
00074                               const DominatorTree *DT = nullptr);
00075 
00076   /// isKnownNonZero - Return true if the given value is known to be non-zero
00077   /// when defined.  For vectors return true if every element is known to be
00078   /// non-zero when defined.  Supports values with integer or pointer type and
00079   /// vectors of integers.
00080   bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth = 0,
00081                       AssumptionCache *AC = nullptr,
00082                       const Instruction *CxtI = nullptr,
00083                       const DominatorTree *DT = nullptr);
00084 
00085   /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00086   /// this predicate to simplify operations downstream.  Mask is known to be
00087   /// zero for bits that V cannot have.
00088   ///
00089   /// This function is defined on values with integer type, values with pointer
00090   /// type, and vectors of integers.  In the case
00091   /// where V is a vector, the mask, known zero, and known one values are the
00092   /// same width as the vector element, and the bit is set only if it is true
00093   /// for all of the elements in the vector.
00094   bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
00095                          unsigned Depth = 0, AssumptionCache *AC = nullptr,
00096                          const Instruction *CxtI = nullptr,
00097                          const DominatorTree *DT = nullptr);
00098 
00099   /// ComputeNumSignBits - Return the number of times the sign bit of the
00100   /// register is replicated into the other bits.  We know that at least 1 bit
00101   /// is always equal to the sign bit (itself), but other cases can give us
00102   /// information.  For example, immediately after an "ashr X, 2", we know that
00103   /// the top 3 bits are all equal to each other, so we return 3.
00104   ///
00105   /// 'Op' must have a scalar integer type.
00106   ///
00107   unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL,
00108                               unsigned Depth = 0, AssumptionCache *AC = nullptr,
00109                               const Instruction *CxtI = nullptr,
00110                               const DominatorTree *DT = nullptr);
00111 
00112   /// ComputeMultiple - This function computes the integer multiple of Base that
00113   /// equals V.  If successful, it returns true and returns the multiple in
00114   /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
00115   /// simplified to an integer, then the simplified V is returned in Val.  Look
00116   /// through sext only if LookThroughSExt=true.
00117   bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
00118                        bool LookThroughSExt = false,
00119                        unsigned Depth = 0);
00120 
00121   /// CannotBeNegativeZero - Return true if we can prove that the specified FP 
00122   /// value is never equal to -0.0.
00123   ///
00124   bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
00125 
00126   /// CannotBeOrderedLessThanZero - Return true if we can prove that the 
00127   /// specified FP value is either a NaN or never less than 0.0.
00128   ///
00129   bool CannotBeOrderedLessThanZero(const Value *V, unsigned Depth = 0);
00130 
00131   /// isBytewiseValue - If the specified value can be set by repeating the same
00132   /// byte in memory, return the i8 value that it is represented with.  This is
00133   /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
00134   /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
00135   /// byte store (e.g. i16 0x1234), return null.
00136   Value *isBytewiseValue(Value *V);
00137     
00138   /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
00139   /// the scalar value indexed is already around as a register, for example if
00140   /// it were inserted directly into the aggregrate.
00141   ///
00142   /// If InsertBefore is not null, this function will duplicate (modified)
00143   /// insertvalues when a part of a nested struct is extracted.
00144   Value *FindInsertedValue(Value *V,
00145                            ArrayRef<unsigned> idx_range,
00146                            Instruction *InsertBefore = nullptr);
00147 
00148   /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
00149   /// it can be expressed as a base pointer plus a constant offset.  Return the
00150   /// base and offset to the caller.
00151   Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
00152                                           const DataLayout &DL);
00153   static inline const Value *
00154   GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
00155                                    const DataLayout &DL) {
00156     return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset,
00157                                             DL);
00158   }
00159   
00160   /// getConstantStringInfo - This function computes the length of a
00161   /// null-terminated C string pointed to by V.  If successful, it returns true
00162   /// and returns the string in Str.  If unsuccessful, it returns false.  This
00163   /// does not include the trailing nul character by default.  If TrimAtNul is
00164   /// set to false, then this returns any trailing nul characters as well as any
00165   /// other characters that come after it.
00166   bool getConstantStringInfo(const Value *V, StringRef &Str,
00167                              uint64_t Offset = 0, bool TrimAtNul = true);
00168 
00169   /// GetStringLength - If we can compute the length of the string pointed to by
00170   /// the specified pointer, return 'len+1'.  If we can't, return 0.
00171   uint64_t GetStringLength(Value *V);
00172 
00173   /// GetUnderlyingObject - This method strips off any GEP address adjustments
00174   /// and pointer casts from the specified value, returning the original object
00175   /// being addressed.  Note that the returned value has pointer type if the
00176   /// specified value does.  If the MaxLookup value is non-zero, it limits the
00177   /// number of instructions to be stripped off.
00178   Value *GetUnderlyingObject(Value *V, const DataLayout &DL,
00179                              unsigned MaxLookup = 6);
00180   static inline const Value *GetUnderlyingObject(const Value *V,
00181                                                  const DataLayout &DL,
00182                                                  unsigned MaxLookup = 6) {
00183     return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup);
00184   }
00185 
00186   /// \brief This method is similar to GetUnderlyingObject except that it can
00187   /// look through phi and select instructions and return multiple objects.
00188   ///
00189   /// If LoopInfo is passed, loop phis are further analyzed.  If a pointer
00190   /// accesses different objects in each iteration, we don't look through the
00191   /// phi node. E.g. consider this loop nest:
00192   ///
00193   ///   int **A;
00194   ///   for (i)
00195   ///     for (j) {
00196   ///        A[i][j] = A[i-1][j] * B[j]
00197   ///     }
00198   ///
00199   /// This is transformed by Load-PRE to stash away A[i] for the next iteration
00200   /// of the outer loop:
00201   ///
00202   ///   Curr = A[0];          // Prev_0
00203   ///   for (i: 1..N) {
00204   ///     Prev = Curr;        // Prev = PHI (Prev_0, Curr)
00205   ///     Curr = A[i];
00206   ///     for (j: 0..N) {
00207   ///        Curr[j] = Prev[j] * B[j]
00208   ///     }
00209   ///   }
00210   ///
00211   /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
00212   /// should not assume that Curr and Prev share the same underlying object thus
00213   /// it shouldn't look through the phi above.
00214   void GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
00215                             const DataLayout &DL, LoopInfo *LI = nullptr,
00216                             unsigned MaxLookup = 6);
00217 
00218   /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
00219   /// are lifetime markers.
00220   bool onlyUsedByLifetimeMarkers(const Value *V);
00221 
00222   /// isDereferenceablePointer - Return true if this is always a dereferenceable
00223   /// pointer. If the context instruction is specified perform context-sensitive
00224   /// analysis and return true if the pointer is dereferenceable at the
00225   /// specified instruction.
00226   bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
00227                                 const Instruction *CtxI = nullptr,
00228                                 const DominatorTree *DT = nullptr,
00229                                 const TargetLibraryInfo *TLI = nullptr);
00230   
00231   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00232   /// have any effects besides calculating the result and does not have
00233   /// undefined behavior.
00234   ///
00235   /// This method never returns true for an instruction that returns true for
00236   /// mayHaveSideEffects; however, this method also does some other checks in
00237   /// addition. It checks for undefined behavior, like dividing by zero or
00238   /// loading from an invalid pointer (but not for undefined results, like a
00239   /// shift with a shift amount larger than the width of the result). It checks
00240   /// for malloc and alloca because speculatively executing them might cause a
00241   /// memory leak. It also returns false for instructions related to control
00242   /// flow, specifically terminators and PHI nodes.
00243   ///
00244   /// If the CtxI is specified this method performs context-sensitive analysis
00245   /// and returns true if it is safe to execute the instruction immediately
00246   /// before the CtxI.
00247   ///
00248   /// If the CtxI is NOT specified this method only looks at the instruction
00249   /// itself and its operands, so if this method returns true, it is safe to
00250   /// move the instruction as long as the correct dominance relationships for
00251   /// the operands and users hold.
00252   ///
00253   /// This method can return true for instructions that read memory;
00254   /// for such instructions, moving them may change the resulting value.
00255   bool isSafeToSpeculativelyExecute(const Value *V,
00256                                     const Instruction *CtxI = nullptr,
00257                                     const DominatorTree *DT = nullptr,
00258                                     const TargetLibraryInfo *TLI = nullptr);
00259 
00260   /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
00261   /// its definition.  This returns true for allocas, non-extern-weak globals
00262   /// and byval arguments.
00263   bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr);
00264 
00265   /// isKnownNonNullAt - Return true if this pointer couldn't possibly be null.
00266   /// If the context instruction is specified perform context-sensitive analysis
00267   /// and return true if the pointer couldn't possibly be null at the specified
00268   /// instruction.
00269   bool isKnownNonNullAt(const Value *V,
00270                         const Instruction *CtxI = nullptr,
00271                         const DominatorTree *DT  = nullptr,
00272                         const TargetLibraryInfo *TLI = nullptr);
00273 
00274   /// Return true if it is valid to use the assumptions provided by an
00275   /// assume intrinsic, I, at the point in the control-flow identified by the
00276   /// context instruction, CxtI.
00277   bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
00278                                const DominatorTree *DT = nullptr);
00279 
00280   enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
00281   OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
00282                                                const DataLayout &DL,
00283                                                AssumptionCache *AC,
00284                                                const Instruction *CxtI,
00285                                                const DominatorTree *DT);
00286   OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
00287                                                const DataLayout &DL,
00288                                                AssumptionCache *AC,
00289                                                const Instruction *CxtI,
00290                                                const DominatorTree *DT);
00291   
00292   /// \brief Specific patterns of select instructions we can match.
00293   enum SelectPatternFlavor {
00294     SPF_UNKNOWN = 0,
00295     SPF_SMIN,                   // Signed minimum
00296     SPF_UMIN,                   // Unsigned minimum
00297     SPF_SMAX,                   // Signed maximum
00298     SPF_UMAX,                   // Unsigned maximum
00299     SPF_ABS,                    // Absolute value
00300     SPF_NABS                    // Negated absolute value
00301   };
00302   /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind
00303   /// and providing the out parameter results if we successfully match.
00304   ///
00305   /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does
00306   /// not match that of the original select. If this is the case, the cast
00307   /// operation (one of Trunc,SExt,Zext) that must be done to transform the
00308   /// type of LHS and RHS into the type of V is returned in CastOp.
00309   ///
00310   /// For example:
00311   ///   %1 = icmp slt i32 %a, i32 4
00312   ///   %2 = sext i32 %a to i64
00313   ///   %3 = select i1 %1, i64 %2, i64 4
00314   ///
00315   /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
00316   ///
00317   SelectPatternFlavor matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
00318                                          Instruction::CastOps *CastOp = nullptr);
00319 
00320 } // end namespace llvm
00321 
00322 #endif