move DecomposeGEPExpression out into ValueTracking.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/System/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   class LLVMContext;
28   
29   /// ComputeMaskedBits - Determine which of the bits specified in Mask are
30   /// known to be either zero or one and return them in the KnownZero/KnownOne
31   /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit
32   /// processing.
33   ///
34   /// This function is defined on values with integer type, values with pointer
35   /// type (but only if TD is non-null), and vectors of integers.  In the case
36   /// where V is a vector, the mask, known zero, and known one values are the
37   /// same width as the vector element, and the bit is set only if it is true
38   /// for all of the elements in the vector.
39   void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero,
40                          APInt &KnownOne, const TargetData *TD = 0,
41                          unsigned Depth = 0);
42   
43   /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
44   /// this predicate to simplify operations downstream.  Mask is known to be
45   /// zero for bits that V cannot have.
46   ///
47   /// This function is defined on values with integer type, values with pointer
48   /// type (but only if TD is non-null), and vectors of integers.  In the case
49   /// where V is a vector, the mask, known zero, and known one values are the
50   /// same width as the vector element, and the bit is set only if it is true
51   /// for all of the elements in the vector.
52   bool MaskedValueIsZero(Value *V, const APInt &Mask, 
53                          const TargetData *TD = 0, unsigned Depth = 0);
54
55   
56   /// ComputeNumSignBits - Return the number of times the sign bit of the
57   /// register is replicated into the other bits.  We know that at least 1 bit
58   /// is always equal to the sign bit (itself), but other cases can give us
59   /// information.  For example, immediately after an "ashr X, 2", we know that
60   /// the top 3 bits are all equal to each other, so we return 3.
61   ///
62   /// 'Op' must have a scalar integer type.
63   ///
64   unsigned ComputeNumSignBits(Value *Op, const TargetData *TD = 0,
65                               unsigned Depth = 0);
66
67   /// ComputeMultiple - This function computes the integer multiple of Base that
68   /// equals V.  If successful, it returns true and returns the multiple in
69   /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
70   /// simplified to an integer, then the simplified V is returned in Val.  Look
71   /// through sext only if LookThroughSExt=true.
72   bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
73                        bool LookThroughSExt = false,
74                        unsigned Depth = 0);
75
76   /// CannotBeNegativeZero - Return true if we can prove that the specified FP 
77   /// value is never equal to -0.0.
78   ///
79   bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
80
81   /// DecomposeGEPExpression - If V is a symbolic pointer expression, decompose
82   /// it into a base pointer with a constant offset and a number of scaled
83   /// symbolic offsets.
84   ///
85   /// When TargetData is around, this function is capable of analyzing
86   /// everything that Value::getUnderlyingObject() can look through.  When not,
87   /// it just looks through pointer casts.
88   ///
89   const Value *DecomposeGEPExpression(const Value *V, int64_t &BaseOffs,
90                  SmallVectorImpl<std::pair<const Value*, int64_t> > &VarIndices,
91                                       const TargetData *TD);
92     
93   
94   
95   /// FindScalarValue - Given an aggregrate and an sequence of indices, see if
96   /// the scalar value indexed is already around as a register, for example if
97   /// it were inserted directly into the aggregrate.
98   ///
99   /// If InsertBefore is not null, this function will duplicate (modified)
100   /// insertvalues when a part of a nested struct is extracted.
101   Value *FindInsertedValue(Value *V,
102                            const unsigned *idx_begin,
103                            const unsigned *idx_end,
104                            Instruction *InsertBefore = 0);
105
106   /// This is a convenience wrapper for finding values indexed by a single index
107   /// only.
108   inline Value *FindInsertedValue(Value *V, const unsigned Idx,
109                                   Instruction *InsertBefore = 0) {
110     const unsigned Idxs[1] = { Idx };
111     return FindInsertedValue(V, &Idxs[0], &Idxs[1], InsertBefore);
112   }
113   
114   /// GetConstantStringInfo - This function computes the length of a
115   /// null-terminated C string pointed to by V.  If successful, it returns true
116   /// and returns the string in Str.  If unsuccessful, it returns false.  If
117   /// StopAtNul is set to true (the default), the returned string is truncated
118   /// by a nul character in the global.  If StopAtNul is false, the nul
119   /// character is included in the result string.
120   bool GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset = 0,
121                              bool StopAtNul = true);
122 } // end namespace llvm
123
124 #endif