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 #include <string> 21 22 namespace llvm { 23 template <typename T> class SmallVectorImpl; 24 class Value; 25 class Instruction; 26 class APInt; 27 class TargetData; 28 29 /// ComputeMaskedBits - Determine which of the bits specified in Mask are 30 /// known to be either zero or one and return them in the KnownZero/KnownOne 31 /// bit sets. This code only analyzes bits in Mask, in order to short-circuit 32 /// processing. 33 /// 34 /// This function is defined on values with integer type, values with pointer 35 /// type (but only if TD is non-null), and vectors of integers. In the case 36 /// where V is a vector, the mask, known zero, and known one values are the 37 /// same width as the vector element, and the bit is set only if it is true 38 /// for all of the elements in the vector. 39 void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, 40 APInt &KnownOne, const TargetData *TD = 0, 41 unsigned Depth = 0); 42 43 /// ComputeSignBit - Determine whether the sign bit is known to be zero or 44 /// one. Convenience wrapper around ComputeMaskedBits. 45 void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, 46 const TargetData *TD = 0, unsigned Depth = 0); 47 48 /// isPowerOfTwo - Return true if the given value is known to have exactly one 49 /// bit set when defined. For vectors return true if every element is known to 50 /// be a power of two when defined. Supports values with integer or pointer 51 /// type and vectors of integers. 52 bool isPowerOfTwo(Value *V, const TargetData *TD = 0, unsigned Depth = 0); 53 54 /// isKnownNonZero - Return true if the given value is known to be non-zero 55 /// when defined. For vectors return true if every element is known to be 56 /// non-zero when defined. Supports values with integer or pointer type and 57 /// vectors of integers. 58 bool isKnownNonZero(Value *V, const TargetData *TD = 0, unsigned Depth = 0); 59 60 /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use 61 /// this predicate to simplify operations downstream. Mask is known to be 62 /// zero for bits that V cannot have. 63 /// 64 /// This function is defined on values with integer type, values with pointer 65 /// type (but only if TD is non-null), and vectors of integers. In the case 66 /// where V is a vector, the mask, known zero, and known one values are the 67 /// same width as the vector element, and the bit is set only if it is true 68 /// for all of the elements in the vector. 69 bool MaskedValueIsZero(Value *V, const APInt &Mask, 70 const TargetData *TD = 0, unsigned Depth = 0); 71 72 73 /// ComputeNumSignBits - Return the number of times the sign bit of the 74 /// register is replicated into the other bits. We know that at least 1 bit 75 /// is always equal to the sign bit (itself), but other cases can give us 76 /// information. For example, immediately after an "ashr X, 2", we know that 77 /// the top 3 bits are all equal to each other, so we return 3. 78 /// 79 /// 'Op' must have a scalar integer type. 80 /// 81 unsigned ComputeNumSignBits(Value *Op, const TargetData *TD = 0, 82 unsigned Depth = 0); 83 84 /// ComputeMultiple - This function computes the integer multiple of Base that 85 /// equals V. If successful, it returns true and returns the multiple in 86 /// Multiple. If unsuccessful, it returns false. Also, if V can be 87 /// simplified to an integer, then the simplified V is returned in Val. Look 88 /// through sext only if LookThroughSExt=true. 89 bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, 90 bool LookThroughSExt = false, 91 unsigned Depth = 0); 92 93 /// CannotBeNegativeZero - Return true if we can prove that the specified FP 94 /// value is never equal to -0.0. 95 /// 96 bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0); 97 98 /// isBytewiseValue - If the specified value can be set by repeating the same 99 /// byte in memory, return the i8 value that it is represented with. This is 100 /// true for all i8 values obviously, but is also true for i32 0, i32 -1, 101 /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated 102 /// byte store (e.g. i16 0x1234), return null. 103 Value *isBytewiseValue(Value *V); 104 105 /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if 106 /// the scalar value indexed is already around as a register, for example if 107 /// it were inserted directly into the aggregrate. 108 /// 109 /// If InsertBefore is not null, this function will duplicate (modified) 110 /// insertvalues when a part of a nested struct is extracted. 111 Value *FindInsertedValue(Value *V, 112 ArrayRef<unsigned> idx_range, 113 Instruction *InsertBefore = 0); 114 115 /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if 116 /// it can be expressed as a base pointer plus a constant offset. Return the 117 /// base and offset to the caller. 118 Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, 119 const TargetData &TD); 120 static inline const Value * GetPointerBaseWithConstantOffset(const Value * Ptr,int64_t & Offset,const TargetData & TD)121 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset, 122 const TargetData &TD) { 123 return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD); 124 } 125 126 /// GetConstantStringInfo - This function computes the length of a 127 /// null-terminated C string pointed to by V. If successful, it returns true 128 /// and returns the string in Str. If unsuccessful, it returns false. If 129 /// StopAtNul is set to true (the default), the returned string is truncated 130 /// by a nul character in the global. If StopAtNul is false, the nul 131 /// character is included in the result string. 132 bool GetConstantStringInfo(const Value *V, std::string &Str, 133 uint64_t Offset = 0, 134 bool StopAtNul = true); 135 136 /// GetStringLength - If we can compute the length of the string pointed to by 137 /// the specified pointer, return 'len+1'. If we can't, return 0. 138 uint64_t GetStringLength(Value *V); 139 140 /// GetUnderlyingObject - This method strips off any GEP address adjustments 141 /// and pointer casts from the specified value, returning the original object 142 /// being addressed. Note that the returned value has pointer type if the 143 /// specified value does. If the MaxLookup value is non-zero, it limits the 144 /// number of instructions to be stripped off. 145 Value *GetUnderlyingObject(Value *V, const TargetData *TD = 0, 146 unsigned MaxLookup = 6); 147 static inline const Value * 148 GetUnderlyingObject(const Value *V, const TargetData *TD = 0, 149 unsigned MaxLookup = 6) { 150 return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup); 151 } 152 153 /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer 154 /// are lifetime markers. 155 bool onlyUsedByLifetimeMarkers(const Value *V); 156 157 } // end namespace llvm 158 159 #endif 160