LLVM API Documentation

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