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/ConstantRange.h"
00020 #include "llvm/IR/Instruction.h"
00021 #include "llvm/Support/DataTypes.h"
00022 
00023 namespace llvm {
00024   class APInt;
00025   class AddOperator;
00026   class AssumptionCache;
00027   class DataLayout;
00028   class DominatorTree;
00029   class Instruction;
00030   class Loop;
00031   class LoopInfo;
00032   class MDNode;
00033   class StringRef;
00034   class TargetLibraryInfo;
00035   class Value;
00036 
00037   /// Determine which bits of V are known to be either zero or one and return
00038   /// them in the KnownZero/KnownOne bit sets.
00039   ///
00040   /// This function is defined on values with integer type, values with pointer
00041   /// type, and vectors of integers.  In the case
00042   /// where V is a vector, the known zero and known one values are the
00043   /// same width as the vector element, and the bit is set only if it is true
00044   /// for all of the elements in the vector.
00045   void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
00046                         const DataLayout &DL, unsigned Depth = 0,
00047                         AssumptionCache *AC = nullptr,
00048                         const Instruction *CxtI = nullptr,
00049                         const DominatorTree *DT = nullptr);
00050   /// Compute known bits from the range metadata.
00051   /// \p KnownZero the set of bits that are known to be zero
00052   /// \p KnownOne the set of bits that are known to be one
00053   void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
00054                                          APInt &KnownZero, APInt &KnownOne);
00055   /// Return true if LHS and RHS have no common bits set.
00056   bool haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL,
00057                            AssumptionCache *AC = nullptr,
00058                            const Instruction *CxtI = nullptr,
00059                            const DominatorTree *DT = nullptr);
00060 
00061   /// ComputeSignBit - Determine whether the sign bit is known to be zero or
00062   /// one.  Convenience wrapper around computeKnownBits.
00063   void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
00064                       const DataLayout &DL, unsigned Depth = 0,
00065                       AssumptionCache *AC = nullptr,
00066                       const Instruction *CxtI = nullptr,
00067                       const DominatorTree *DT = nullptr);
00068 
00069   /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have
00070   /// exactly one bit set when defined. For vectors return true if every
00071   /// element is known to be a power of two when defined.  Supports values with
00072   /// integer or pointer type and vectors of integers.  If 'OrZero' is set then
00073   /// return true if the given value is either a power of two or zero.
00074   bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL,
00075                               bool OrZero = false, unsigned Depth = 0,
00076                               AssumptionCache *AC = nullptr,
00077                               const Instruction *CxtI = nullptr,
00078                               const DominatorTree *DT = nullptr);
00079 
00080   /// isKnownNonZero - Return true if the given value is known to be non-zero
00081   /// when defined.  For vectors return true if every element is known to be
00082   /// non-zero when defined.  Supports values with integer or pointer type and
00083   /// vectors of integers.
00084   bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth = 0,
00085                       AssumptionCache *AC = nullptr,
00086                       const Instruction *CxtI = nullptr,
00087                       const DominatorTree *DT = nullptr);
00088 
00089   /// Returns true if the give value is known to be non-negative.
00090   bool isKnownNonNegative(Value *V, const DataLayout &DL, unsigned Depth = 0,
00091                           AssumptionCache *AC = nullptr,
00092                           const Instruction *CxtI = nullptr,
00093                           const DominatorTree *DT = nullptr);
00094 
00095   /// isKnownNonEqual - Return true if the given values are known to be
00096   /// non-equal when defined. Supports scalar integer types only.
00097   bool isKnownNonEqual(Value *V1, Value *V2, const DataLayout &DL,
00098                       AssumptionCache *AC = nullptr,
00099                       const Instruction *CxtI = nullptr,
00100                       const DominatorTree *DT = nullptr);
00101 
00102   /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
00103   /// this predicate to simplify operations downstream.  Mask is known to be
00104   /// zero for bits that V cannot have.
00105   ///
00106   /// This function is defined on values with integer type, values with pointer
00107   /// type, and vectors of integers.  In the case
00108   /// where V is a vector, the mask, known zero, and known one values are the
00109   /// same width as the vector element, and the bit is set only if it is true
00110   /// for all of the elements in the vector.
00111   bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
00112                          unsigned Depth = 0, AssumptionCache *AC = nullptr,
00113                          const Instruction *CxtI = nullptr,
00114                          const DominatorTree *DT = nullptr);
00115 
00116   /// ComputeNumSignBits - Return the number of times the sign bit of the
00117   /// register is replicated into the other bits.  We know that at least 1 bit
00118   /// is always equal to the sign bit (itself), but other cases can give us
00119   /// information.  For example, immediately after an "ashr X, 2", we know that
00120   /// the top 3 bits are all equal to each other, so we return 3.
00121   ///
00122   /// 'Op' must have a scalar integer type.
00123   ///
00124   unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL,
00125                               unsigned Depth = 0, AssumptionCache *AC = nullptr,
00126                               const Instruction *CxtI = nullptr,
00127                               const DominatorTree *DT = nullptr);
00128 
00129   /// ComputeMultiple - This function computes the integer multiple of Base that
00130   /// equals V.  If successful, it returns true and returns the multiple in
00131   /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
00132   /// simplified to an integer, then the simplified V is returned in Val.  Look
00133   /// through sext only if LookThroughSExt=true.
00134   bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
00135                        bool LookThroughSExt = false,
00136                        unsigned Depth = 0);
00137 
00138   /// CannotBeNegativeZero - Return true if we can prove that the specified FP
00139   /// value is never equal to -0.0.
00140   ///
00141   bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
00142 
00143   /// CannotBeOrderedLessThanZero - Return true if we can prove that the
00144   /// specified FP value is either a NaN or never less than 0.0.
00145   ///
00146   bool CannotBeOrderedLessThanZero(const Value *V, unsigned Depth = 0);
00147 
00148   /// isBytewiseValue - If the specified value can be set by repeating the same
00149   /// byte in memory, return the i8 value that it is represented with.  This is
00150   /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
00151   /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
00152   /// byte store (e.g. i16 0x1234), return null.
00153   Value *isBytewiseValue(Value *V);
00154 
00155   /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
00156   /// the scalar value indexed is already around as a register, for example if
00157   /// it were inserted directly into the aggregrate.
00158   ///
00159   /// If InsertBefore is not null, this function will duplicate (modified)
00160   /// insertvalues when a part of a nested struct is extracted.
00161   Value *FindInsertedValue(Value *V,
00162                            ArrayRef<unsigned> idx_range,
00163                            Instruction *InsertBefore = nullptr);
00164 
00165   /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
00166   /// it can be expressed as a base pointer plus a constant offset.  Return the
00167   /// base and offset to the caller.
00168   Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
00169                                           const DataLayout &DL);
00170   static inline const Value *
00171   GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
00172                                    const DataLayout &DL) {
00173     return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset,
00174                                             DL);
00175   }
00176 
00177   /// getConstantStringInfo - This function computes the length of a
00178   /// null-terminated C string pointed to by V.  If successful, it returns true
00179   /// and returns the string in Str.  If unsuccessful, it returns false.  This
00180   /// does not include the trailing nul character by default.  If TrimAtNul is
00181   /// set to false, then this returns any trailing nul characters as well as any
00182   /// other characters that come after it.
00183   bool getConstantStringInfo(const Value *V, StringRef &Str,
00184                              uint64_t Offset = 0, bool TrimAtNul = true);
00185 
00186   /// GetStringLength - If we can compute the length of the string pointed to by
00187   /// the specified pointer, return 'len+1'.  If we can't, return 0.
00188   uint64_t GetStringLength(Value *V);
00189 
00190   /// GetUnderlyingObject - This method strips off any GEP address adjustments
00191   /// and pointer casts from the specified value, returning the original object
00192   /// being addressed.  Note that the returned value has pointer type if the
00193   /// specified value does.  If the MaxLookup value is non-zero, it limits the
00194   /// number of instructions to be stripped off.
00195   Value *GetUnderlyingObject(Value *V, const DataLayout &DL,
00196                              unsigned MaxLookup = 6);
00197   static inline const Value *GetUnderlyingObject(const Value *V,
00198                                                  const DataLayout &DL,
00199                                                  unsigned MaxLookup = 6) {
00200     return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup);
00201   }
00202 
00203   /// \brief This method is similar to GetUnderlyingObject except that it can
00204   /// look through phi and select instructions and return multiple objects.
00205   ///
00206   /// If LoopInfo is passed, loop phis are further analyzed.  If a pointer
00207   /// accesses different objects in each iteration, we don't look through the
00208   /// phi node. E.g. consider this loop nest:
00209   ///
00210   ///   int **A;
00211   ///   for (i)
00212   ///     for (j) {
00213   ///        A[i][j] = A[i-1][j] * B[j]
00214   ///     }
00215   ///
00216   /// This is transformed by Load-PRE to stash away A[i] for the next iteration
00217   /// of the outer loop:
00218   ///
00219   ///   Curr = A[0];          // Prev_0
00220   ///   for (i: 1..N) {
00221   ///     Prev = Curr;        // Prev = PHI (Prev_0, Curr)
00222   ///     Curr = A[i];
00223   ///     for (j: 0..N) {
00224   ///        Curr[j] = Prev[j] * B[j]
00225   ///     }
00226   ///   }
00227   ///
00228   /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
00229   /// should not assume that Curr and Prev share the same underlying object thus
00230   /// it shouldn't look through the phi above.
00231   void GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
00232                             const DataLayout &DL, LoopInfo *LI = nullptr,
00233                             unsigned MaxLookup = 6);
00234 
00235   /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
00236   /// are lifetime markers.
00237   bool onlyUsedByLifetimeMarkers(const Value *V);
00238 
00239   /// isDereferenceablePointer - Return true if this is always a dereferenceable
00240   /// pointer. If the context instruction is specified perform context-sensitive
00241   /// analysis and return true if the pointer is dereferenceable at the
00242   /// specified instruction.
00243   bool isDereferenceablePointer(const Value *V, const DataLayout &DL,
00244                                 const Instruction *CtxI = nullptr,
00245                                 const DominatorTree *DT = nullptr,
00246                                 const TargetLibraryInfo *TLI = nullptr);
00247 
00248   /// Returns true if V is always a dereferenceable pointer with alignment
00249   /// greater or equal than requested. If the context instruction is specified
00250   /// performs context-sensitive analysis and returns true if the pointer is
00251   /// dereferenceable at the specified instruction.
00252   bool isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
00253                                           const DataLayout &DL,
00254                                           const Instruction *CtxI = nullptr,
00255                                           const DominatorTree *DT = nullptr,
00256                                           const TargetLibraryInfo *TLI = nullptr);
00257 
00258   /// isSafeToSpeculativelyExecute - Return true if the instruction does not
00259   /// have any effects besides calculating the result and does not have
00260   /// undefined behavior.
00261   ///
00262   /// This method never returns true for an instruction that returns true for
00263   /// mayHaveSideEffects; however, this method also does some other checks in
00264   /// addition. It checks for undefined behavior, like dividing by zero or
00265   /// loading from an invalid pointer (but not for undefined results, like a
00266   /// shift with a shift amount larger than the width of the result). It checks
00267   /// for malloc and alloca because speculatively executing them might cause a
00268   /// memory leak. It also returns false for instructions related to control
00269   /// flow, specifically terminators and PHI nodes.
00270   ///
00271   /// If the CtxI is specified this method performs context-sensitive analysis
00272   /// and returns true if it is safe to execute the instruction immediately
00273   /// before the CtxI.
00274   ///
00275   /// If the CtxI is NOT specified this method only looks at the instruction
00276   /// itself and its operands, so if this method returns true, it is safe to
00277   /// move the instruction as long as the correct dominance relationships for
00278   /// the operands and users hold.
00279   ///
00280   /// This method can return true for instructions that read memory;
00281   /// for such instructions, moving them may change the resulting value.
00282   bool isSafeToSpeculativelyExecute(const Value *V,
00283                                     const Instruction *CtxI = nullptr,
00284                                     const DominatorTree *DT = nullptr,
00285                                     const TargetLibraryInfo *TLI = nullptr);
00286 
00287   /// Returns true if the result or effects of the given instructions \p I
00288   /// depend on or influence global memory.
00289   /// Memory dependence arises for example if the instruction reads from
00290   /// memory or may produce effects or undefined behaviour. Memory dependent
00291   /// instructions generally cannot be reorderd with respect to other memory
00292   /// dependent instructions or moved into non-dominated basic blocks.
00293   /// Instructions which just compute a value based on the values of their
00294   /// operands are not memory dependent.
00295   bool mayBeMemoryDependent(const Instruction &I);
00296 
00297   /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
00298   /// its definition.  This returns true for allocas, non-extern-weak globals
00299   /// and byval arguments.
00300   bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr);
00301 
00302   /// isKnownNonNullAt - Return true if this pointer couldn't possibly be null.
00303   /// If the context instruction is specified perform context-sensitive analysis
00304   /// and return true if the pointer couldn't possibly be null at the specified
00305   /// instruction.
00306   bool isKnownNonNullAt(const Value *V,
00307                         const Instruction *CtxI = nullptr,
00308                         const DominatorTree *DT  = nullptr,
00309                         const TargetLibraryInfo *TLI = nullptr);
00310 
00311   /// Return true if it is valid to use the assumptions provided by an
00312   /// assume intrinsic, I, at the point in the control-flow identified by the
00313   /// context instruction, CxtI.
00314   bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
00315                                const DominatorTree *DT = nullptr);
00316 
00317   enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
00318   OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
00319                                                const DataLayout &DL,
00320                                                AssumptionCache *AC,
00321                                                const Instruction *CxtI,
00322                                                const DominatorTree *DT);
00323   OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
00324                                                const DataLayout &DL,
00325                                                AssumptionCache *AC,
00326                                                const Instruction *CxtI,
00327                                                const DominatorTree *DT);
00328   OverflowResult computeOverflowForSignedAdd(Value *LHS, Value *RHS,
00329                                              const DataLayout &DL,
00330                                              AssumptionCache *AC = nullptr,
00331                                              const Instruction *CxtI = nullptr,
00332                                              const DominatorTree *DT = nullptr);
00333   /// This version also leverages the sign bit of Add if known.
00334   OverflowResult computeOverflowForSignedAdd(AddOperator *Add,
00335                                              const DataLayout &DL,
00336                                              AssumptionCache *AC = nullptr,
00337                                              const Instruction *CxtI = nullptr,
00338                                              const DominatorTree *DT = nullptr);
00339 
00340   /// Return true if this function can prove that the instruction I will
00341   /// always transfer execution to one of its successors (including the next
00342   /// instruction that follows within a basic block). E.g. this is not
00343   /// guaranteed for function calls that could loop infinitely.
00344   ///
00345   /// In other words, this function returns false for instructions that may
00346   /// transfer execution or fail to transfer execution in a way that is not
00347   /// captured in the CFG nor in the sequence of instructions within a basic
00348   /// block.
00349   ///
00350   /// Undefined behavior is assumed not to happen, so e.g. division is
00351   /// guaranteed to transfer execution to the following instruction even
00352   /// though division by zero might cause undefined behavior.
00353   bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I);
00354 
00355   /// Return true if this function can prove that the instruction I
00356   /// is executed for every iteration of the loop L.
00357   ///
00358   /// Note that this currently only considers the loop header.
00359   bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
00360                                               const Loop *L);
00361 
00362   /// Return true if this function can prove that I is guaranteed to yield
00363   /// full-poison (all bits poison) if at least one of its operands are
00364   /// full-poison (all bits poison).
00365   ///
00366   /// The exact rules for how poison propagates through instructions have
00367   /// not been settled as of 2015-07-10, so this function is conservative
00368   /// and only considers poison to be propagated in uncontroversial
00369   /// cases. There is no attempt to track values that may be only partially
00370   /// poison.
00371   bool propagatesFullPoison(const Instruction *I);
00372 
00373   /// Return either nullptr or an operand of I such that I will trigger
00374   /// undefined behavior if I is executed and that operand has a full-poison
00375   /// value (all bits poison).
00376   const Value *getGuaranteedNonFullPoisonOp(const Instruction *I);
00377 
00378   /// Return true if this function can prove that if PoisonI is executed
00379   /// and yields a full-poison value (all bits poison), then that will
00380   /// trigger undefined behavior.
00381   ///
00382   /// Note that this currently only considers the basic block that is
00383   /// the parent of I.
00384   bool isKnownNotFullPoison(const Instruction *PoisonI);
00385 
00386   /// \brief Specific patterns of select instructions we can match.
00387   enum SelectPatternFlavor {
00388     SPF_UNKNOWN = 0,
00389     SPF_SMIN,                   /// Signed minimum
00390     SPF_UMIN,                   /// Unsigned minimum
00391     SPF_SMAX,                   /// Signed maximum
00392     SPF_UMAX,                   /// Unsigned maximum
00393     SPF_FMINNUM,                /// Floating point minnum
00394     SPF_FMAXNUM,                /// Floating point maxnum
00395     SPF_ABS,                    /// Absolute value
00396     SPF_NABS                    /// Negated absolute value
00397   };
00398   /// \brief Behavior when a floating point min/max is given one NaN and one
00399   /// non-NaN as input.
00400   enum SelectPatternNaNBehavior {
00401     SPNB_NA = 0,                /// NaN behavior not applicable.
00402     SPNB_RETURNS_NAN,           /// Given one NaN input, returns the NaN.
00403     SPNB_RETURNS_OTHER,         /// Given one NaN input, returns the non-NaN.
00404     SPNB_RETURNS_ANY            /// Given one NaN input, can return either (or
00405                                 /// it has been determined that no operands can
00406                                 /// be NaN).
00407   };
00408   struct SelectPatternResult {
00409     SelectPatternFlavor Flavor;
00410     SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is
00411                                           /// SPF_FMINNUM or SPF_FMAXNUM.
00412     bool Ordered;               /// When implementing this min/max pattern as
00413                                 /// fcmp; select, does the fcmp have to be
00414                                 /// ordered?
00415 
00416     /// \brief Return true if \p SPF is a min or a max pattern.
00417     static bool isMinOrMax(SelectPatternFlavor SPF) {
00418       return !(SPF == SPF_UNKNOWN || SPF == SPF_ABS || SPF == SPF_NABS);
00419     }
00420   };
00421   /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind
00422   /// and providing the out parameter results if we successfully match.
00423   ///
00424   /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does
00425   /// not match that of the original select. If this is the case, the cast
00426   /// operation (one of Trunc,SExt,Zext) that must be done to transform the
00427   /// type of LHS and RHS into the type of V is returned in CastOp.
00428   ///
00429   /// For example:
00430   ///   %1 = icmp slt i32 %a, i32 4
00431   ///   %2 = sext i32 %a to i64
00432   ///   %3 = select i1 %1, i64 %2, i64 4
00433   ///
00434   /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
00435   ///
00436   SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
00437                                          Instruction::CastOps *CastOp = nullptr);
00438 
00439   /// Parse out a conservative ConstantRange from !range metadata.
00440   ///
00441   /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
00442   ConstantRange getConstantRangeFromMetadata(MDNode &RangeMD);
00443 
00444   /// Return true if RHS is known to be implied by LHS.  A & B must be i1
00445   /// (boolean) values or a vector of such values. Note that the truth table for
00446   /// implication is the same as <=u on i1 values (but not <=s!).  The truth
00447   /// table for both is:
00448   ///    | T | F (B)
00449   ///  T | T | F
00450   ///  F | T | T
00451   /// (A)
00452   bool isImpliedCondition(Value *LHS, Value *RHS, const DataLayout &DL,
00453                           unsigned Depth = 0, AssumptionCache *AC = nullptr,
00454                           const Instruction *CxtI = nullptr,
00455                           const DominatorTree *DT = nullptr);
00456 } // end namespace llvm
00457 
00458 #endif