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