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 AssumptionTracker;
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                         AssumptionTracker *AT = 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                       AssumptionTracker *AT = 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                               AssumptionTracker *AT = 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, AssumptionTracker *AT = 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                          AssumptionTracker *AT = nullptr,
00089                          const Instruction *CxtI = nullptr,
00090                          const DominatorTree *DT = nullptr);
00091 
00092   
00093   /// ComputeNumSignBits - Return the number of times the sign bit of the
00094   /// register is replicated into the other bits.  We know that at least 1 bit
00095   /// is always equal to the sign bit (itself), but other cases can give us
00096   /// information.  For example, immediately after an "ashr X, 2", we know that
00097   /// the top 3 bits are all equal to each other, so we return 3.
00098   ///
00099   /// 'Op' must have a scalar integer type.
00100   ///
00101   unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD = nullptr,
00102                               unsigned Depth = 0,
00103                               AssumptionTracker *AT = nullptr,
00104                               const Instruction *CxtI = nullptr,
00105                               const DominatorTree *DT = nullptr);
00106 
00107   /// ComputeMultiple - This function computes the integer multiple of Base that
00108   /// equals V.  If successful, it returns true and returns the multiple in
00109   /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
00110   /// simplified to an integer, then the simplified V is returned in Val.  Look
00111   /// through sext only if LookThroughSExt=true.
00112   bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
00113                        bool LookThroughSExt = false,
00114                        unsigned Depth = 0);
00115 
00116   /// CannotBeNegativeZero - Return true if we can prove that the specified FP 
00117   /// value is never equal to -0.0.
00118   ///
00119   bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
00120 
00121   /// isBytewiseValue - If the specified value can be set by repeating the same
00122   /// byte in memory, return the i8 value that it is represented with.  This is
00123   /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
00124   /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
00125   /// byte store (e.g. i16 0x1234), return null.
00126   Value *isBytewiseValue(Value *V);
00127     
00128   /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
00129   /// the scalar value indexed is already around as a register, for example if
00130   /// it were inserted directly into the aggregrate.
00131   ///
00132   /// If InsertBefore is not null, this function will duplicate (modified)
00133   /// insertvalues when a part of a nested struct is extracted.
00134   Value *FindInsertedValue(Value *V,
00135                            ArrayRef<unsigned> idx_range,
00136                            Instruction *InsertBefore = nullptr);
00137 
00138   /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
00139   /// it can be expressed as a base pointer plus a constant offset.  Return the
00140   /// base and offset to the caller.
00141   Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
00142                                           const DataLayout *TD);
00143   static inline const Value *
00144   GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
00145                                    const DataLayout *TD) {
00146     return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD);
00147   }
00148   
00149   /// getConstantStringInfo - This function computes the length of a
00150   /// null-terminated C string pointed to by V.  If successful, it returns true
00151   /// and returns the string in Str.  If unsuccessful, it returns false.  This
00152   /// does not include the trailing nul character by default.  If TrimAtNul is
00153   /// set to false, then this returns any trailing nul characters as well as any
00154   /// other characters that come after it.
00155   bool getConstantStringInfo(const Value *V, StringRef &Str,
00156                              uint64_t Offset = 0, bool TrimAtNul = true);
00157 
00158   /// GetStringLength - If we can compute the length of the string pointed to by
00159   /// the specified pointer, return 'len+1'.  If we can't, return 0.
00160   uint64_t GetStringLength(Value *V);
00161 
00162   /// GetUnderlyingObject - This method strips off any GEP address adjustments
00163   /// and pointer casts from the specified value, returning the original object
00164   /// being addressed.  Note that the returned value has pointer type if the
00165   /// specified value does.  If the MaxLookup value is non-zero, it limits the
00166   /// number of instructions to be stripped off.
00167   Value *GetUnderlyingObject(Value *V, const DataLayout *TD = nullptr,
00168                              unsigned MaxLookup = 6);
00169   static inline const Value *
00170   GetUnderlyingObject(const Value *V, const DataLayout *TD = nullptr,
00171                       unsigned MaxLookup = 6) {
00172     return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup);
00173   }
00174 
00175   /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
00176   /// except that it can look through phi and select instructions and return
00177   /// multiple objects.
00178   void GetUnderlyingObjects(Value *V,
00179                             SmallVectorImpl<Value *> &Objects,
00180                             const DataLayout *TD = nullptr,
00181                             unsigned MaxLookup = 6);
00182 
00183   /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
00184   /// are lifetime markers.
00185   bool onlyUsedByLifetimeMarkers(const Value *V);
00186 
00187   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00188   /// have any effects besides calculating the result and does not have
00189   /// undefined behavior.
00190   ///
00191   /// This method never returns true for an instruction that returns true for
00192   /// mayHaveSideEffects; however, this method also does some other checks in
00193   /// addition. It checks for undefined behavior, like dividing by zero or
00194   /// loading from an invalid pointer (but not for undefined results, like a
00195   /// shift with a shift amount larger than the width of the result). It checks
00196   /// for malloc and alloca because speculatively executing them might cause a
00197   /// memory leak. It also returns false for instructions related to control
00198   /// flow, specifically terminators and PHI nodes.
00199   ///
00200   /// This method only looks at the instruction itself and its operands, so if
00201   /// this method returns true, it is safe to move the instruction as long as
00202   /// the correct dominance relationships for the operands and users hold.
00203   /// However, this method can return true for instructions that read memory;
00204   /// for such instructions, moving them may change the resulting value.
00205   bool isSafeToSpeculativelyExecute(const Value *V,
00206                                     const DataLayout *TD = nullptr);
00207 
00208   /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
00209   /// its definition.  This returns true for allocas, non-extern-weak globals
00210   /// and byval arguments.
00211   bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr);
00212 
00213   /// Return true if it is valid to use the assumptions provided by an
00214   /// assume intrinsic, I, at the point in the control-flow identified by the
00215   /// context instruction, CxtI.
00216   bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
00217                                const DataLayout *DL = nullptr,
00218                                const DominatorTree *DT = nullptr);
00219 
00220 } // end namespace llvm
00221 
00222 #endif