LLVM API Documentation

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 (but only if TD is non-null), 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 *TD = nullptr, 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 *TD = nullptr, 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, bool OrZero = false, unsigned Depth = 0,
00064                               AssumptionCache *AC = nullptr,
00065                               const Instruction *CxtI = nullptr,
00066                               const DominatorTree *DT = nullptr);
00067 
00068   /// isKnownNonZero - Return true if the given value is known to be non-zero
00069   /// when defined.  For vectors return true if every element is known to be
00070   /// non-zero when defined.  Supports values with integer or pointer type and
00071   /// vectors of integers.
00072   bool isKnownNonZero(Value *V, const DataLayout *TD = nullptr,
00073                       unsigned Depth = 0, AssumptionCache *AC = nullptr,
00074                       const Instruction *CxtI = nullptr,
00075                       const DominatorTree *DT = nullptr);
00076 
00077   /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00078   /// this predicate to simplify operations downstream.  Mask is known to be
00079   /// zero for bits that V cannot have.
00080   ///
00081   /// This function is defined on values with integer type, values with pointer
00082   /// type (but only if TD is non-null), and vectors of integers.  In the case
00083   /// where V is a vector, the mask, known zero, and known one values are the
00084   /// same width as the vector element, and the bit is set only if it is true
00085   /// for all of the elements in the vector.
00086   bool MaskedValueIsZero(Value *V, const APInt &Mask,
00087                          const DataLayout *TD = nullptr, unsigned Depth = 0,
00088                          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 *TD = nullptr,
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 *TD);
00146   static inline const Value *
00147   GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
00148                                    const DataLayout *TD) {
00149     return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD);
00150   }
00151   
00152   /// getConstantStringInfo - This function computes the length of a
00153   /// null-terminated C string pointed to by V.  If successful, it returns true
00154   /// and returns the string in Str.  If unsuccessful, it returns false.  This
00155   /// does not include the trailing nul character by default.  If TrimAtNul is
00156   /// set to false, then this returns any trailing nul characters as well as any
00157   /// other characters that come after it.
00158   bool getConstantStringInfo(const Value *V, StringRef &Str,
00159                              uint64_t Offset = 0, bool TrimAtNul = true);
00160 
00161   /// GetStringLength - If we can compute the length of the string pointed to by
00162   /// the specified pointer, return 'len+1'.  If we can't, return 0.
00163   uint64_t GetStringLength(Value *V);
00164 
00165   /// GetUnderlyingObject - This method strips off any GEP address adjustments
00166   /// and pointer casts from the specified value, returning the original object
00167   /// being addressed.  Note that the returned value has pointer type if the
00168   /// specified value does.  If the MaxLookup value is non-zero, it limits the
00169   /// number of instructions to be stripped off.
00170   Value *GetUnderlyingObject(Value *V, const DataLayout *TD = nullptr,
00171                              unsigned MaxLookup = 6);
00172   static inline const Value *
00173   GetUnderlyingObject(const Value *V, const DataLayout *TD = nullptr,
00174                       unsigned MaxLookup = 6) {
00175     return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup);
00176   }
00177 
00178   /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
00179   /// except that it can look through phi and select instructions and return
00180   /// multiple objects.
00181   void GetUnderlyingObjects(Value *V,
00182                             SmallVectorImpl<Value *> &Objects,
00183                             const DataLayout *TD = nullptr,
00184                             unsigned MaxLookup = 6);
00185 
00186   /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
00187   /// are lifetime markers.
00188   bool onlyUsedByLifetimeMarkers(const Value *V);
00189 
00190   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00191   /// have any effects besides calculating the result and does not have
00192   /// undefined behavior.
00193   ///
00194   /// This method never returns true for an instruction that returns true for
00195   /// mayHaveSideEffects; however, this method also does some other checks in
00196   /// addition. It checks for undefined behavior, like dividing by zero or
00197   /// loading from an invalid pointer (but not for undefined results, like a
00198   /// shift with a shift amount larger than the width of the result). It checks
00199   /// for malloc and alloca because speculatively executing them might cause a
00200   /// memory leak. It also returns false for instructions related to control
00201   /// flow, specifically terminators and PHI nodes.
00202   ///
00203   /// This method only looks at the instruction itself and its operands, so if
00204   /// this method returns true, it is safe to move the instruction as long as
00205   /// the correct dominance relationships for the operands and users hold.
00206   /// However, this method can return true for instructions that read memory;
00207   /// for such instructions, moving them may change the resulting value.
00208   bool isSafeToSpeculativelyExecute(const Value *V,
00209                                     const DataLayout *TD = nullptr);
00210 
00211   /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
00212   /// its definition.  This returns true for allocas, non-extern-weak globals
00213   /// and byval arguments.
00214   bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr);
00215 
00216   /// Return true if it is valid to use the assumptions provided by an
00217   /// assume intrinsic, I, at the point in the control-flow identified by the
00218   /// context instruction, CxtI.
00219   bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
00220                                const DataLayout *DL = nullptr,
00221                                const DominatorTree *DT = nullptr);
00222 
00223   enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
00224   OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
00225                                                const DataLayout *DL,
00226                                                AssumptionCache *AC,
00227                                                const Instruction *CxtI,
00228                                                const DominatorTree *DT);
00229   OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
00230                                                const DataLayout *DL,
00231                                                AssumptionCache *AC,
00232                                                const Instruction *CxtI,
00233                                                const DominatorTree *DT);
00234 } // end namespace llvm
00235 
00236 #endif