1 //===- llvm/Analysis/ValueTracking.h - Walk computations --------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains routines that help analyze properties that chains of 11 // computations have. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_ANALYSIS_VALUETRACKING_H 16 #define LLVM_ANALYSIS_VALUETRACKING_H 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/Support/DataTypes.h" 20 21 namespace llvm { 22 class Value; 23 class Instruction; 24 class APInt; 25 class DataLayout; 26 class StringRef; 27 class MDNode; 28 class TargetLibraryInfo; 29 30 /// Determine which bits of V are known to be either zero or one and return 31 /// them in the KnownZero/KnownOne bit sets. 32 /// 33 /// This function is defined on values with integer type, values with pointer 34 /// type (but only if TD is non-null), and vectors of integers. In the case 35 /// where V is a vector, the known zero and known one values are the 36 /// same width as the vector element, and the bit is set only if it is true 37 /// for all of the elements in the vector. 38 void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, 39 const DataLayout *TD = nullptr, unsigned Depth = 0); 40 /// Compute known bits from the range metadata. 41 /// \p KnownZero the set of bits that are known to be zero 42 void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, 43 APInt &KnownZero); 44 45 /// ComputeSignBit - Determine whether the sign bit is known to be zero or 46 /// one. Convenience wrapper around computeKnownBits. 47 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, 48 const DataLayout *TD = nullptr, unsigned Depth = 0); 49 50 /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have 51 /// exactly one bit set when defined. For vectors return true if every 52 /// element is known to be a power of two when defined. Supports values with 53 /// integer or pointer type and vectors of integers. If 'OrZero' is set then 54 /// returns true if the given value is either a power of two or zero. 55 bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero = false, unsigned Depth = 0); 56 57 /// isKnownNonZero - Return true if the given value is known to be non-zero 58 /// when defined. For vectors return true if every element is known to be 59 /// non-zero when defined. Supports values with integer or pointer type and 60 /// vectors of integers. 61 bool isKnownNonZero(Value *V, const DataLayout *TD = nullptr, 62 unsigned Depth = 0); 63 64 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 65 /// this predicate to simplify operations downstream. Mask is known to be 66 /// zero for bits that V cannot have. 67 /// 68 /// This function is defined on values with integer type, values with pointer 69 /// type (but only if TD is non-null), and vectors of integers. In the case 70 /// where V is a vector, the mask, known zero, and known one values are the 71 /// same width as the vector element, and the bit is set only if it is true 72 /// for all of the elements in the vector. 73 bool MaskedValueIsZero(Value *V, const APInt &Mask, 74 const DataLayout *TD = nullptr, unsigned Depth = 0); 75 76 77 /// ComputeNumSignBits - Return the number of times the sign bit of the 78 /// register is replicated into the other bits. We know that at least 1 bit 79 /// is always equal to the sign bit (itself), but other cases can give us 80 /// information. For example, immediately after an "ashr X, 2", we know that 81 /// the top 3 bits are all equal to each other, so we return 3. 82 /// 83 /// 'Op' must have a scalar integer type. 84 /// 85 unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD = nullptr, 86 unsigned Depth = 0); 87 88 /// ComputeMultiple - This function computes the integer multiple of Base that 89 /// equals V. If successful, it returns true and returns the multiple in 90 /// Multiple. If unsuccessful, it returns false. Also, if V can be 91 /// simplified to an integer, then the simplified V is returned in Val. Look 92 /// through sext only if LookThroughSExt=true. 93 bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, 94 bool LookThroughSExt = false, 95 unsigned Depth = 0); 96 97 /// CannotBeNegativeZero - Return true if we can prove that the specified FP 98 /// value is never equal to -0.0. 99 /// 100 bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0); 101 102 /// isBytewiseValue - If the specified value can be set by repeating the same 103 /// byte in memory, return the i8 value that it is represented with. This is 104 /// true for all i8 values obviously, but is also true for i32 0, i32 -1, 105 /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated 106 /// byte store (e.g. i16 0x1234), return null. 107 Value *isBytewiseValue(Value *V); 108 109 /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if 110 /// the scalar value indexed is already around as a register, for example if 111 /// it were inserted directly into the aggregrate. 112 /// 113 /// If InsertBefore is not null, this function will duplicate (modified) 114 /// insertvalues when a part of a nested struct is extracted. 115 Value *FindInsertedValue(Value *V, 116 ArrayRef<unsigned> idx_range, 117 Instruction *InsertBefore = nullptr); 118 119 /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if 120 /// it can be expressed as a base pointer plus a constant offset. Return the 121 /// base and offset to the caller. 122 Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, 123 const DataLayout *TD); 124 static inline const Value * GetPointerBaseWithConstantOffset(const Value * Ptr,int64_t & Offset,const DataLayout * TD)125 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset, 126 const DataLayout *TD) { 127 return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD); 128 } 129 130 /// getConstantStringInfo - This function computes the length of a 131 /// null-terminated C string pointed to by V. If successful, it returns true 132 /// and returns the string in Str. If unsuccessful, it returns false. This 133 /// does not include the trailing nul character by default. If TrimAtNul is 134 /// set to false, then this returns any trailing nul characters as well as any 135 /// other characters that come after it. 136 bool getConstantStringInfo(const Value *V, StringRef &Str, 137 uint64_t Offset = 0, bool TrimAtNul = true); 138 139 /// GetStringLength - If we can compute the length of the string pointed to by 140 /// the specified pointer, return 'len+1'. If we can't, return 0. 141 uint64_t GetStringLength(Value *V); 142 143 /// GetUnderlyingObject - This method strips off any GEP address adjustments 144 /// and pointer casts from the specified value, returning the original object 145 /// being addressed. Note that the returned value has pointer type if the 146 /// specified value does. If the MaxLookup value is non-zero, it limits the 147 /// number of instructions to be stripped off. 148 Value *GetUnderlyingObject(Value *V, const DataLayout *TD = nullptr, 149 unsigned MaxLookup = 6); 150 static inline const Value * 151 GetUnderlyingObject(const Value *V, const DataLayout *TD = nullptr, 152 unsigned MaxLookup = 6) { 153 return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup); 154 } 155 156 /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject 157 /// except that it can look through phi and select instructions and return 158 /// multiple objects. 159 void GetUnderlyingObjects(Value *V, 160 SmallVectorImpl<Value *> &Objects, 161 const DataLayout *TD = nullptr, 162 unsigned MaxLookup = 6); 163 164 /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer 165 /// are lifetime markers. 166 bool onlyUsedByLifetimeMarkers(const Value *V); 167 168 /// isSafeToSpeculativelyExecute - Return true if the instruction does not 169 /// have any effects besides calculating the result and does not have 170 /// undefined behavior. 171 /// 172 /// This method never returns true for an instruction that returns true for 173 /// mayHaveSideEffects; however, this method also does some other checks in 174 /// addition. It checks for undefined behavior, like dividing by zero or 175 /// loading from an invalid pointer (but not for undefined results, like a 176 /// shift with a shift amount larger than the width of the result). It checks 177 /// for malloc and alloca because speculatively executing them might cause a 178 /// memory leak. It also returns false for instructions related to control 179 /// flow, specifically terminators and PHI nodes. 180 /// 181 /// This method only looks at the instruction itself and its operands, so if 182 /// this method returns true, it is safe to move the instruction as long as 183 /// the correct dominance relationships for the operands and users hold. 184 /// However, this method can return true for instructions that read memory; 185 /// for such instructions, moving them may change the resulting value. 186 bool isSafeToSpeculativelyExecute(const Value *V, 187 const DataLayout *TD = nullptr); 188 189 /// isKnownNonNull - Return true if this pointer couldn't possibly be null by 190 /// its definition. This returns true for allocas, non-extern-weak globals 191 /// and byval arguments. 192 bool isKnownNonNull(const Value *V, const TargetLibraryInfo *TLI = nullptr); 193 194 } // end namespace llvm 195 196 #endif 197