1173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//===- llvm/Analysis/ValueTracking.h - Walk computations --------*- C++ -*-===//
2173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//
3173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//                     The LLVM Compiler Infrastructure
4173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//
5173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner// This file is distributed under the University of Illinois Open Source
6173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner// License. See LICENSE.TXT for details.
7173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//
8173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//===----------------------------------------------------------------------===//
9173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//
10173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner// This file contains routines that help analyze properties that chains of
11173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner// computations have.
12173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//
13173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner//===----------------------------------------------------------------------===//
14173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
15173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner#ifndef LLVM_ANALYSIS_VALUETRACKING_H
16173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner#define LLVM_ANALYSIS_VALUETRACKING_H
17173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
18fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad#include "llvm/ADT/ArrayRef.h"
191f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
200d2b0aba424bd3959bb5c807873def8f53e57a3cEric Christopher
21173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattnernamespace llvm {
22173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  class Value;
23b23d5adbc8230167e711070b9298985de4580f30Matthijs Kooijman  class Instruction;
24173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  class APInt;
253574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  class DataLayout;
2618c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  class StringRef;
2795d594cac3737ae1594a391276942a443cac426bRafael Espindola  class MDNode;
2895d594cac3737ae1594a391276942a443cac426bRafael Espindola
29173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// ComputeMaskedBits - Determine which of the bits specified in Mask are
30173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// known to be either zero or one and return them in the KnownZero/KnownOne
31173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// bit sets.  This code only analyzes bits in Mask, in order to short-circuit
32173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// processing.
33cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  ///
34cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// This function is defined on values with integer type, values with pointer
35cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// type (but only if TD is non-null), and vectors of integers.  In the case
36cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// where V is a vector, the mask, known zero, and known one values are the
37cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// same width as the vector element, and the bit is set only if it is true
38cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// for all of the elements in the vector.
3926c8dcc692fb2addd475446cfff24d6a4e958bcaRafael Espindola  void ComputeMaskedBits(Value *V,  APInt &KnownZero, APInt &KnownOne,
403574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow                         const DataLayout *TD = 0, unsigned Depth = 0);
4126c8dcc692fb2addd475446cfff24d6a4e958bcaRafael Espindola  void computeMaskedBitsLoad(const MDNode &Ranges, APInt &KnownZero);
4295d594cac3737ae1594a391276942a443cac426bRafael Espindola
43d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// ComputeSignBit - Determine whether the sign bit is known to be zero or
44d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// one.  Convenience wrapper around ComputeMaskedBits.
45d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
463574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow                      const DataLayout *TD = 0, unsigned Depth = 0);
47d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands
48dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have
49dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  /// exactly one bit set when defined. For vectors return true if every
50dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  /// element is known to be a power of two when defined.  Supports values with
51dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  /// integer or pointer type and vectors of integers.  If 'OrZero' is set then
52dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  /// returns true if the given value is either a power of two or zero.
53dbaa2376f7b3c027e895ff4bee3ae08351f3ea88Rafael Espindola  bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero = false, unsigned Depth = 0);
54d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands
55d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// isKnownNonZero - Return true if the given value is known to be non-zero
56d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// when defined.  For vectors return true if every element is known to be
57d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// non-zero when defined.  Supports values with integer or pointer type and
58d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands  /// vectors of integers.
593574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  bool isKnownNonZero(Value *V, const DataLayout *TD = 0, unsigned Depth = 0);
60d70d1a5c44609af091f6fc3e29193f9f4756a74fDuncan Sands
61833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.  We use
62833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  /// this predicate to simplify operations downstream.  Mask is known to be
63833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  /// zero for bits that V cannot have.
64cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  ///
65cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// This function is defined on values with integer type, values with pointer
66cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// type (but only if TD is non-null), and vectors of integers.  In the case
67cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// where V is a vector, the mask, known zero, and known one values are the
68cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// same width as the vector element, and the bit is set only if it is true
69cf5128ec01f45d2bf7eadc20b253cb44486e473fChris Lattner  /// for all of the elements in the vector.
70173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  bool MaskedValueIsZero(Value *V, const APInt &Mask,
713574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow                         const DataLayout *TD = 0, unsigned Depth = 0);
72173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
73173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
74173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// ComputeNumSignBits - Return the number of times the sign bit of the
75173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// register is replicated into the other bits.  We know that at least 1 bit
76173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// is always equal to the sign bit (itself), but other cases can give us
77173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// information.  For example, immediately after an "ashr X, 2", we know that
78173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// the top 3 bits are all equal to each other, so we return 3.
79173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  ///
80173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  /// 'Op' must have a scalar integer type.
81173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner  ///
823574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD = 0,
83173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner                              unsigned Depth = 0);
84173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
852b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez  /// ComputeMultiple - This function computes the integer multiple of Base that
862b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez  /// equals V.  If successful, it returns true and returns the multiple in
872b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez  /// Multiple.  If unsuccessful, it returns false.  Also, if V can be
882b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez  /// simplified to an integer, then the simplified V is returned in Val.  Look
892b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez  /// through sext only if LookThroughSExt=true.
903dbb9e64d6e9d1e8bf16f75ebe4fe59ffdf93dd3Dan Gohman  bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
913dbb9e64d6e9d1e8bf16f75ebe4fe59ffdf93dd3Dan Gohman                       bool LookThroughSExt = false,
922b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez                       unsigned Depth = 0);
932b6705f5e7c7624bd7fe486298c400f1afc15f6cVictor Hernandez
94833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  /// CannotBeNegativeZero - Return true if we can prove that the specified FP
95833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  /// value is never equal to -0.0.
96833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  ///
97833f25d79ee28f1049f9177c3d2f4c9fbad6f643Chris Lattner  bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
98b23d5adbc8230167e711070b9298985de4580f30Matthijs Kooijman
99bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  /// isBytewiseValue - If the specified value can be set by repeating the same
100bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  /// byte in memory, return the i8 value that it is represented with.  This is
101bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
102bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  /// i16 0xF0F0, double 0.0 etc.  If the value can't be handled with a repeated
103bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  /// byte store (e.g. i16 0x1234), return null.
104bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner  Value *isBytewiseValue(Value *V);
105bb89710dddf967199dfc56e8bf5d28b0003f2ee6Chris Lattner
106dffc308c0e34617fe1ee686429f2146ff6170325Dan Gohman  /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
10774fc4d968617a1652666bed8aba5e35da86373ceMatthijs Kooijman  /// the scalar value indexed is already around as a register, for example if
10874fc4d968617a1652666bed8aba5e35da86373ceMatthijs Kooijman  /// it were inserted directly into the aggregrate.
109977289121996f0afb781592f92a4aee1be3010feMatthijs Kooijman  ///
110977289121996f0afb781592f92a4aee1be3010feMatthijs Kooijman  /// If InsertBefore is not null, this function will duplicate (modified)
111977289121996f0afb781592f92a4aee1be3010feMatthijs Kooijman  /// insertvalues when a part of a nested struct is extracted.
112b23d5adbc8230167e711070b9298985de4580f30Matthijs Kooijman  Value *FindInsertedValue(Value *V,
113fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad                           ArrayRef<unsigned> idx_range,
114464bb782fb9f873414b4ec01698c88b411564430Nick Lewycky                           Instruction *InsertBefore = 0);
115de9256ad2e33a203e97328e285c3909f67aad4b0Matthijs Kooijman
116ed58a6f96f605901adc0df3ca76499d52b2d1a1aChris Lattner  /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
117ed58a6f96f605901adc0df3ca76499d52b2d1a1aChris Lattner  /// it can be expressed as a base pointer plus a constant offset.  Return the
118ed58a6f96f605901adc0df3ca76499d52b2d1a1aChris Lattner  /// base and offset to the caller.
119ed58a6f96f605901adc0df3ca76499d52b2d1a1aChris Lattner  Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
120a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman                                          const DataLayout *TD);
121a04096580a65b6cfbe12dabf6d695f7303750c0dChris Lattner  static inline const Value *
122a04096580a65b6cfbe12dabf6d695f7303750c0dChris Lattner  GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
123a070d2a0355c4993240b5206ebc1d517c151331dDan Gohman                                   const DataLayout *TD) {
124a04096580a65b6cfbe12dabf6d695f7303750c0dChris Lattner    return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD);
125a04096580a65b6cfbe12dabf6d695f7303750c0dChris Lattner  }
126ed58a6f96f605901adc0df3ca76499d52b2d1a1aChris Lattner
12718c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  /// getConstantStringInfo - This function computes the length of a
1280582ae99ba75a556d6ff63b254da327d32ba036fBill Wendling  /// null-terminated C string pointed to by V.  If successful, it returns true
12918c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  /// and returns the string in Str.  If unsuccessful, it returns false.  This
13018c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  /// does not include the trailing nul character by default.  If TrimAtNul is
13118c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  /// set to false, then this returns any trailing nul characters as well as any
13218c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  /// other characters that come after it.
13318c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  bool getConstantStringInfo(const Value *V, StringRef &Str,
13418c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner                             uint64_t Offset = 0, bool TrimAtNul = true);
13518c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner
13625ec483cfca8d3a3ba8728a4a126e04b92789069Eric Christopher  /// GetStringLength - If we can compute the length of the string pointed to by
13725ec483cfca8d3a3ba8728a4a126e04b92789069Eric Christopher  /// the specified pointer, return 'len+1'.  If we can't, return 0.
13825ec483cfca8d3a3ba8728a4a126e04b92789069Eric Christopher  uint64_t GetStringLength(Value *V);
1395034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman
1405034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  /// GetUnderlyingObject - This method strips off any GEP address adjustments
1415034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  /// and pointer casts from the specified value, returning the original object
1425034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  /// being addressed.  Note that the returned value has pointer type if the
1435034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  /// specified value does.  If the MaxLookup value is non-zero, it limits the
1445034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  /// number of instructions to be stripped off.
1453574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  Value *GetUnderlyingObject(Value *V, const DataLayout *TD = 0,
146bd1801b5553c8be3960255a92738464e0010b6f6Dan Gohman                             unsigned MaxLookup = 6);
1475034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  static inline const Value *
1483574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow  GetUnderlyingObject(const Value *V, const DataLayout *TD = 0,
149bd1801b5553c8be3960255a92738464e0010b6f6Dan Gohman                      unsigned MaxLookup = 6) {
150bd1801b5553c8be3960255a92738464e0010b6f6Dan Gohman    return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup);
1515034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman  }
1525034dd318a9dfa0dc45a3ac01e58e60f2aa2498dDan Gohman
153b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman  /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
154b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman  /// except that it can look through phi and select instructions and return
155b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman  /// multiple objects.
156b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman  void GetUnderlyingObjects(Value *V,
157b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman                            SmallVectorImpl<Value *> &Objects,
1583574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow                            const DataLayout *TD = 0,
159b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman                            unsigned MaxLookup = 6);
160b401e3bd16c3d648464606d5e5b496dd61d12afcDan Gohman
16199e0b2a8df7e3a49c0e1edd250d17604fe2fb21cNick Lewycky  /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
16299e0b2a8df7e3a49c0e1edd250d17604fe2fb21cNick Lewycky  /// are lifetime markers.
16399e0b2a8df7e3a49c0e1edd250d17604fe2fb21cNick Lewycky  bool onlyUsedByLifetimeMarkers(const Value *V);
16499e0b2a8df7e3a49c0e1edd250d17604fe2fb21cNick Lewycky
165f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// isSafeToSpeculativelyExecute - Return true if the instruction does not
166f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// have any effects besides calculating the result and does not have
167f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// undefined behavior.
168f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  ///
169f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// This method never returns true for an instruction that returns true for
170f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// mayHaveSideEffects; however, this method also does some other checks in
171f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// addition. It checks for undefined behavior, like dividing by zero or
172f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// loading from an invalid pointer (but not for undefined results, like a
173f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// shift with a shift amount larger than the width of the result). It checks
174f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// for malloc and alloca because speculatively executing them might cause a
175f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// memory leak. It also returns false for instructions related to control
176f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// flow, specifically terminators and PHI nodes.
177f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  ///
178f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// This method only looks at the instruction itself and its operands, so if
179f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// this method returns true, it is safe to move the instruction as long as
180f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// the correct dominance relationships for the operands and users hold.
181f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// However, this method can return true for instructions that read memory;
182f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman  /// for such instructions, moving them may change the resulting value.
183febaf8401779fedf8db7b02e499c5e39848fb9f5Dan Gohman  bool isSafeToSpeculativelyExecute(const Value *V,
1843574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow                                    const DataLayout *TD = 0);
185f0426601977c3e386d2d26c72a2cca691dc42072Dan Gohman
186de0eb19248f3053c07a5b1dad9c47b8435458337Dan Gohman  /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
187de0eb19248f3053c07a5b1dad9c47b8435458337Dan Gohman  /// its definition.  This returns true for allocas, non-extern-weak globals
188de0eb19248f3053c07a5b1dad9c47b8435458337Dan Gohman  /// and byval arguments.
189de0eb19248f3053c07a5b1dad9c47b8435458337Dan Gohman  bool isKnownNonNull(const Value *V);
190de0eb19248f3053c07a5b1dad9c47b8435458337Dan Gohman
191173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner} // end namespace llvm
192173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner
193173234a68fb6ece106e77da443d87f09d5906cb9Chris Lattner#endif
194