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/Support/DataTypes.h"
00020 
00021 namespace llvm {
00022   class Value;
00023   class Instruction;
00024   class APInt;
00025   class DataLayout;
00026   class StringRef;
00027   class MDNode;
00028   class AssumptionCache;
00029   class DominatorTree;
00030   class TargetLibraryInfo;
00031 
00032   /// Determine which bits of V are known to be either zero or one and return
00033   /// them in the KnownZero/KnownOne bit sets.
00034   ///
00035   /// This function is defined on values with integer type, values with pointer
00036   /// type, and vectors of integers.  In the case
00037   /// where V is a vector, the known zero and known one values are the
00038   /// same width as the vector element, and the bit is set only if it is true
00039   /// for all of the elements in the vector.
00040   void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
00041                         const DataLayout &DL, unsigned Depth = 0,
00042                         AssumptionCache *AC = nullptr,
00043                         const Instruction *CxtI = nullptr,
00044                         const DominatorTree *DT = nullptr);
00045   /// Compute known bits from the range metadata.
00046   /// \p KnownZero the set of bits that are known to be zero
00047   void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
00048                                          APInt &KnownZero);
00049 
00050   /// ComputeSignBit - Determine whether the sign bit is known to be zero or
00051   /// one.  Convenience wrapper around computeKnownBits.
00052   void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
00053                       const DataLayout &DL, unsigned Depth = 0,
00054                       AssumptionCache *AC = nullptr,
00055                       const Instruction *CxtI = nullptr,
00056                       const DominatorTree *DT = nullptr);
00057 
00058   /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have
00059   /// exactly one bit set when defined. For vectors return true if every
00060   /// element is known to be a power of two when defined.  Supports values with
00061   /// integer or pointer type and vectors of integers.  If 'OrZero' is set then
00062   /// returns true if the given value is either a power of two or zero.
00063   bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL,
00064                               bool OrZero = false, unsigned Depth = 0,
00065                               AssumptionCache *AC = nullptr,
00066                               const Instruction *CxtI = nullptr,
00067                               const DominatorTree *DT = nullptr);
00068 
00069   /// isKnownNonZero - Return true if the given value is known to be non-zero
00070   /// when defined.  For vectors return true if every element is known to be
00071   /// non-zero when defined.  Supports values with integer or pointer type and
00072   /// vectors of integers.
00073   bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth = 0,
00074                       AssumptionCache *AC = nullptr,
00075                       const Instruction *CxtI = nullptr,
00076                       const DominatorTree *DT = nullptr);
00077 
00078   /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00079   /// this predicate to simplify operations downstream.  Mask is known to be
00080   /// zero for bits that V cannot have.
00081   ///
00082   /// This function is defined on values with integer type, values with pointer
00083   /// type, and vectors of integers.  In the case
00084   /// where V is a vector, the mask, known zero, and known one values are the
00085   /// same width as the vector element, and the bit is set only if it is true
00086   /// for all of the elements in the vector.
00087   bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
00088                          unsigned Depth = 0, AssumptionCache *AC = nullptr,
00089                          const Instruction *CxtI = nullptr,
00090                          const DominatorTree *DT = nullptr);
00091 
00092   /// ComputeNumSignBits - Return the number of times the sign bit of the
00093   /// register is replicated into the other bits.  We know that at least 1 bit
00094   /// is always equal to the sign bit (itself), but other cases can give us
00095   /// information.  For example, immediately after an "ashr X, 2", we know that
00096   /// the top 3 bits are all equal to each other, so we return 3.
00097   ///
00098   /// 'Op' must have a scalar integer type.
00099   ///
00100   unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL,
00101                               unsigned Depth = 0, AssumptionCache *AC = nullptr,
00102                               const Instruction *CxtI = nullptr,
00103                               const DominatorTree *DT = nullptr);
00104 
00105   /// ComputeMultiple - This function computes the integer multiple of Base that
00106   /// equals V.  If successful, it returns true and returns the multiple in
00107   /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
00108   /// simplified to an integer, then the simplified V is returned in Val.  Look
00109   /// through sext only if LookThroughSExt=true.
00110   bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
00111                        bool LookThroughSExt = false,
00112                        unsigned Depth = 0);
00113 
00114   /// CannotBeNegativeZero - Return true if we can prove that the specified FP 
00115   /// value is never equal to -0.0.
00116   ///
00117   bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
00118 
00119   /// CannotBeOrderedLessThanZero - Return true if we can prove that the 
00120   /// specified FP value is either a NaN or never less than 0.0.
00121   ///
00122   bool CannotBeOrderedLessThanZero(const Value *V, unsigned Depth = 0);
00123 
00124   /// isBytewiseValue - If the specified value can be set by repeating the same
00125   /// byte in memory, return the i8 value that it is represented with.  This is
00126   /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
00127   /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
00128   /// byte store (e.g. i16 0x1234), return null.
00129   Value *isBytewiseValue(Value *V);
00130     
00131   /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
00132   /// the scalar value indexed is already around as a register, for example if
00133   /// it were inserted directly into the aggregrate.
00134   ///
00135   /// If InsertBefore is not null, this function will duplicate (modified)
00136   /// insertvalues when a part of a nested struct is extracted.
00137   Value *FindInsertedValue(Value *V,
00138                            ArrayRef<unsigned> idx_range,
00139                            Instruction *InsertBefore = nullptr);
00140 
00141   /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
00142   /// it can be expressed as a base pointer plus a constant offset.  Return the
00143   /// base and offset to the caller.
00144   Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
00145                                           const DataLayout &DL);
00146   static inline const Value *
00147   GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
00148                                    const DataLayout &DL) {
00149     return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset,
00150                                             DL);
00151   }
00152   
00153   /// getConstantStringInfo - This function computes the length of a
00154   /// null-terminated C string pointed to by V.  If successful, it returns true
00155   /// and returns the string in Str.  If unsuccessful, it returns false.  This
00156   /// does not include the trailing nul character by default.  If TrimAtNul is
00157   /// set to false, then this returns any trailing nul characters as well as any
00158   /// other characters that come after it.
00159   bool getConstantStringInfo(const Value *V, StringRef &Str,
00160                              uint64_t Offset = 0, bool TrimAtNul = true);
00161 
00162   /// GetStringLength - If we can compute the length of the string pointed to by
00163   /// the specified pointer, return 'len+1'.  If we can't, return 0.
00164   uint64_t GetStringLength(Value *V);
00165 
00166   /// GetUnderlyingObject - This method strips off any GEP address adjustments
00167   /// and pointer casts from the specified value, returning the original object
00168   /// being addressed.  Note that the returned value has pointer type if the
00169   /// specified value does.  If the MaxLookup value is non-zero, it limits the
00170   /// number of instructions to be stripped off.
00171   Value *GetUnderlyingObject(Value *V, const DataLayout &DL,
00172                              unsigned MaxLookup = 6);
00173   static inline const Value *GetUnderlyingObject(const Value *V,
00174                                                  const DataLayout &DL,
00175                                                  unsigned MaxLookup = 6) {
00176     return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup);
00177   }
00178 
00179   /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
00180   /// except that it can look through phi and select instructions and return
00181   /// multiple objects.
00182   void GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
00183                             const DataLayout &DL, unsigned MaxLookup = 6);
00184 
00185   /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
00186   /// are lifetime markers.
00187   bool onlyUsedByLifetimeMarkers(const Value *V);
00188 
00189   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00190   /// have any effects besides calculating the result and does not have
00191   /// undefined behavior.
00192   ///
00193   /// This method never returns true for an instruction that returns true for
00194   /// mayHaveSideEffects; however, this method also does some other checks in
00195   /// addition. It checks for undefined behavior, like dividing by zero or
00196   /// loading from an invalid pointer (but not for undefined results, like a
00197   /// shift with a shift amount larger than the width of the result). It checks
00198   /// for malloc and alloca because speculatively executing them might cause a
00199   /// memory leak. It also returns false for instructions related to control
00200   /// flow, specifically terminators and PHI nodes.
00201   ///
00202   /// This method only looks at the instruction itself and its operands, so if
00203   /// this method returns true, it is safe to move the instruction as long as
00204   /// the correct dominance relationships for the operands and users hold.
00205   /// However, this method can return true for instructions that read memory;
00206   /// for such instructions, moving them may change the resulting value.
00207   bool isSafeToSpeculativelyExecute(const Value *V);
00208 
00209   /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
00210   /// its definition.  This returns true for allocas, non-extern-weak globals
00211   /// and byval arguments.
00212   bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr);
00213 
00214   /// Return true if it is valid to use the assumptions provided by an
00215   /// assume intrinsic, I, at the point in the control-flow identified by the
00216   /// context instruction, CxtI.
00217   bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
00218                                const DominatorTree *DT = nullptr);
00219 
00220   enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
00221   OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
00222                                                const DataLayout &DL,
00223                                                AssumptionCache *AC,
00224                                                const Instruction *CxtI,
00225                                                const DominatorTree *DT);
00226   OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
00227                                                const DataLayout &DL,
00228                                                AssumptionCache *AC,
00229                                                const Instruction *CxtI,
00230                                                const DominatorTree *DT);
00231 } // end namespace llvm
00232 
00233 #endif