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