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