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