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