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/IR/CallSite.h"
19#include "llvm/IR/ConstantRange.h"
20#include "llvm/IR/Instruction.h"
21#include "llvm/IR/IntrinsicInst.h"
22#include "llvm/Support/DataTypes.h"
23
24namespace llvm {
25template <typename T> class ArrayRef;
26  class APInt;
27  class AddOperator;
28  class AssumptionCache;
29  class DataLayout;
30  class DominatorTree;
31  class GEPOperator;
32  class Instruction;
33  class Loop;
34  class LoopInfo;
35  class MDNode;
36  class StringRef;
37  class TargetLibraryInfo;
38  class Value;
39
40  namespace Intrinsic {
41  enum ID : unsigned;
42  }
43
44  /// Determine which bits of V are known to be either zero or one and return
45  /// them in the KnownZero/KnownOne bit sets.
46  ///
47  /// This function is defined on values with integer type, values with pointer
48  /// type, and vectors of integers.  In the case
49  /// where V is a vector, the 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  void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
53                        const DataLayout &DL, unsigned Depth = 0,
54                        AssumptionCache *AC = nullptr,
55                        const Instruction *CxtI = nullptr,
56                        const DominatorTree *DT = nullptr);
57  /// Compute known bits from the range metadata.
58  /// \p KnownZero the set of bits that are known to be zero
59  /// \p KnownOne the set of bits that are known to be one
60  void computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
61                                         APInt &KnownZero, APInt &KnownOne);
62  /// Return true if LHS and RHS have no common bits set.
63  bool haveNoCommonBitsSet(Value *LHS, Value *RHS, const DataLayout &DL,
64                           AssumptionCache *AC = nullptr,
65                           const Instruction *CxtI = nullptr,
66                           const DominatorTree *DT = nullptr);
67
68  /// Determine whether the sign bit is known to be zero or one. Convenience
69  /// wrapper around computeKnownBits.
70  void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
71                      const DataLayout &DL, unsigned Depth = 0,
72                      AssumptionCache *AC = nullptr,
73                      const Instruction *CxtI = nullptr,
74                      const DominatorTree *DT = nullptr);
75
76  /// Return true if the given value is known to have exactly one bit set when
77  /// defined. For vectors return true if every element is known to be a power
78  /// of two when defined. Supports values with integer or pointer type and
79  /// vectors of integers. If 'OrZero' is set, then return true if the given
80  /// value is either a power of two or zero.
81  bool isKnownToBeAPowerOfTwo(Value *V, const DataLayout &DL,
82                              bool OrZero = false, unsigned Depth = 0,
83                              AssumptionCache *AC = nullptr,
84                              const Instruction *CxtI = nullptr,
85                              const DominatorTree *DT = nullptr);
86
87  /// Return true if the given value is known to be non-zero when defined. For
88  /// vectors, return true if every element is known to be non-zero when
89  /// defined. Supports values with integer or pointer type and vectors of
90  /// integers.
91  bool isKnownNonZero(Value *V, const DataLayout &DL, unsigned Depth = 0,
92                      AssumptionCache *AC = nullptr,
93                      const Instruction *CxtI = nullptr,
94                      const DominatorTree *DT = nullptr);
95
96  /// Returns true if the give value is known to be non-negative.
97  bool isKnownNonNegative(Value *V, const DataLayout &DL, unsigned Depth = 0,
98                          AssumptionCache *AC = nullptr,
99                          const Instruction *CxtI = nullptr,
100                          const DominatorTree *DT = nullptr);
101
102  /// Returns true if the given value is known be positive (i.e. non-negative
103  /// and non-zero).
104  bool isKnownPositive(Value *V, const DataLayout &DL, unsigned Depth = 0,
105                       AssumptionCache *AC = nullptr,
106                       const Instruction *CxtI = nullptr,
107                       const DominatorTree *DT = nullptr);
108
109  /// Returns true if the given value is known be negative (i.e. non-positive
110  /// and non-zero).
111  bool isKnownNegative(Value *V, const DataLayout &DL, unsigned Depth = 0,
112                       AssumptionCache *AC = nullptr,
113                       const Instruction *CxtI = nullptr,
114                       const DominatorTree *DT = nullptr);
115
116  /// Return true if the given values are known to be non-equal when defined.
117  /// Supports scalar integer types only.
118  bool isKnownNonEqual(Value *V1, Value *V2, const DataLayout &DL,
119                      AssumptionCache *AC = nullptr,
120                      const Instruction *CxtI = nullptr,
121                      const DominatorTree *DT = nullptr);
122
123  /// Return true if 'V & Mask' is known to be zero. We use this predicate to
124  /// simplify operations downstream. Mask is known to be zero for bits that V
125  /// cannot have.
126  ///
127  /// This function is defined on values with integer type, values with pointer
128  /// type, and vectors of integers.  In the case
129  /// where V is a vector, the mask, known zero, and known one values are the
130  /// same width as the vector element, and the bit is set only if it is true
131  /// for all of the elements in the vector.
132  bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL,
133                         unsigned Depth = 0, AssumptionCache *AC = nullptr,
134                         const Instruction *CxtI = nullptr,
135                         const DominatorTree *DT = nullptr);
136
137  /// Return the number of times the sign bit of the register is replicated into
138  /// the other bits. We know that at least 1 bit is always equal to the sign
139  /// bit (itself), but other cases can give us information. For example,
140  /// immediately after an "ashr X, 2", we know that the top 3 bits are all
141  /// equal to each other, so we return 3. For vectors, return the number of
142  /// sign bits for the vector element with the mininum number of known sign
143  /// bits.
144  unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL,
145                              unsigned Depth = 0, AssumptionCache *AC = nullptr,
146                              const Instruction *CxtI = nullptr,
147                              const DominatorTree *DT = nullptr);
148
149  /// This function computes the integer multiple of Base that equals V. If
150  /// successful, it returns true and returns the multiple in Multiple. If
151  /// unsuccessful, it returns false. Also, if V can be simplified to an
152  /// integer, then the simplified V is returned in Val. Look through sext only
153  /// if LookThroughSExt=true.
154  bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
155                       bool LookThroughSExt = false,
156                       unsigned Depth = 0);
157
158  /// Map a call instruction to an intrinsic ID.  Libcalls which have equivalent
159  /// intrinsics are treated as-if they were intrinsics.
160  Intrinsic::ID getIntrinsicForCallSite(ImmutableCallSite ICS,
161                                        const TargetLibraryInfo *TLI);
162
163  /// Return true if we can prove that the specified FP value is never equal to
164  /// -0.0.
165  bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
166                            unsigned Depth = 0);
167
168  /// Return true if we can prove that the specified FP value is either a NaN or
169  /// never less than 0.0.
170  bool CannotBeOrderedLessThanZero(const Value *V, const TargetLibraryInfo *TLI,
171                                   unsigned Depth = 0);
172
173  /// If the specified value can be set by repeating the same byte in memory,
174  /// return the i8 value that it is represented with. This is true for all i8
175  /// values obviously, but is also true for i32 0, i32 -1, i16 0xF0F0, double
176  /// 0.0 etc. If the value can't be handled with a repeated byte store (e.g.
177  /// i16 0x1234), return null.
178  Value *isBytewiseValue(Value *V);
179
180  /// Given an aggregrate and an sequence of indices, see if the scalar value
181  /// indexed is already around as a register, for example if it were inserted
182  /// directly into the aggregrate.
183  ///
184  /// If InsertBefore is not null, this function will duplicate (modified)
185  /// insertvalues when a part of a nested struct is extracted.
186  Value *FindInsertedValue(Value *V,
187                           ArrayRef<unsigned> idx_range,
188                           Instruction *InsertBefore = nullptr);
189
190  /// Analyze the specified pointer to see if it can be expressed as a base
191  /// pointer plus a constant offset. Return the base and offset to the caller.
192  Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
193                                          const DataLayout &DL);
194  static inline const Value *
195  GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
196                                   const DataLayout &DL) {
197    return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset,
198                                            DL);
199  }
200
201  /// Returns true if the GEP is based on a pointer to a string (array of i8),
202  /// and is indexing into this string.
203  bool isGEPBasedOnPointerToString(const GEPOperator *GEP);
204
205  /// This function computes the length of a null-terminated C string pointed to
206  /// by V. If successful, it returns true and returns the string in Str. If
207  /// unsuccessful, it returns false. This does not include the trailing null
208  /// character by default. If TrimAtNul is set to false, then this returns any
209  /// trailing null characters as well as any other characters that come after
210  /// it.
211  bool getConstantStringInfo(const Value *V, StringRef &Str,
212                             uint64_t Offset = 0, bool TrimAtNul = true);
213
214  /// If we can compute the length of the string pointed to by the specified
215  /// pointer, return 'len+1'.  If we can't, return 0.
216  uint64_t GetStringLength(Value *V);
217
218  /// This method strips off any GEP address adjustments and pointer casts from
219  /// the specified value, returning the original object being addressed. Note
220  /// that the returned value has pointer type if the specified value does. If
221  /// the MaxLookup value is non-zero, it limits the number of instructions to
222  /// be stripped off.
223  Value *GetUnderlyingObject(Value *V, const DataLayout &DL,
224                             unsigned MaxLookup = 6);
225  static inline const Value *GetUnderlyingObject(const Value *V,
226                                                 const DataLayout &DL,
227                                                 unsigned MaxLookup = 6) {
228    return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup);
229  }
230
231  /// \brief This method is similar to GetUnderlyingObject except that it can
232  /// look through phi and select instructions and return multiple objects.
233  ///
234  /// If LoopInfo is passed, loop phis are further analyzed.  If a pointer
235  /// accesses different objects in each iteration, we don't look through the
236  /// phi node. E.g. consider this loop nest:
237  ///
238  ///   int **A;
239  ///   for (i)
240  ///     for (j) {
241  ///        A[i][j] = A[i-1][j] * B[j]
242  ///     }
243  ///
244  /// This is transformed by Load-PRE to stash away A[i] for the next iteration
245  /// of the outer loop:
246  ///
247  ///   Curr = A[0];          // Prev_0
248  ///   for (i: 1..N) {
249  ///     Prev = Curr;        // Prev = PHI (Prev_0, Curr)
250  ///     Curr = A[i];
251  ///     for (j: 0..N) {
252  ///        Curr[j] = Prev[j] * B[j]
253  ///     }
254  ///   }
255  ///
256  /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
257  /// should not assume that Curr and Prev share the same underlying object thus
258  /// it shouldn't look through the phi above.
259  void GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
260                            const DataLayout &DL, LoopInfo *LI = nullptr,
261                            unsigned MaxLookup = 6);
262
263  /// Return true if the only users of this pointer are lifetime markers.
264  bool onlyUsedByLifetimeMarkers(const Value *V);
265
266  /// Return true if the instruction does not have any effects besides
267  /// calculating the result and does not have undefined behavior.
268  ///
269  /// This method never returns true for an instruction that returns true for
270  /// mayHaveSideEffects; however, this method also does some other checks in
271  /// addition. It checks for undefined behavior, like dividing by zero or
272  /// loading from an invalid pointer (but not for undefined results, like a
273  /// shift with a shift amount larger than the width of the result). It checks
274  /// for malloc and alloca because speculatively executing them might cause a
275  /// memory leak. It also returns false for instructions related to control
276  /// flow, specifically terminators and PHI nodes.
277  ///
278  /// If the CtxI is specified this method performs context-sensitive analysis
279  /// and returns true if it is safe to execute the instruction immediately
280  /// before the CtxI.
281  ///
282  /// If the CtxI is NOT specified this method only looks at the instruction
283  /// itself and its operands, so if this method returns true, it is safe to
284  /// move the instruction as long as the correct dominance relationships for
285  /// the operands and users hold.
286  ///
287  /// This method can return true for instructions that read memory;
288  /// for such instructions, moving them may change the resulting value.
289  bool isSafeToSpeculativelyExecute(const Value *V,
290                                    const Instruction *CtxI = nullptr,
291                                    const DominatorTree *DT = nullptr);
292
293  /// Returns true if the result or effects of the given instructions \p I
294  /// depend on or influence global memory.
295  /// Memory dependence arises for example if the instruction reads from
296  /// memory or may produce effects or undefined behaviour. Memory dependent
297  /// instructions generally cannot be reorderd with respect to other memory
298  /// dependent instructions or moved into non-dominated basic blocks.
299  /// Instructions which just compute a value based on the values of their
300  /// operands are not memory dependent.
301  bool mayBeMemoryDependent(const Instruction &I);
302
303  /// Return true if this pointer couldn't possibly be null by its definition.
304  /// This returns true for allocas, non-extern-weak globals, and byval
305  /// arguments.
306  bool isKnownNonNull(const Value *V);
307
308  /// Return true if this pointer couldn't possibly be null. If the context
309  /// instruction is specified, perform context-sensitive analysis and return
310  /// true if the pointer couldn't possibly be null at the specified
311  /// instruction.
312  bool isKnownNonNullAt(const Value *V,
313                        const Instruction *CtxI = nullptr,
314                        const DominatorTree *DT  = nullptr);
315
316  /// Return true if it is valid to use the assumptions provided by an
317  /// assume intrinsic, I, at the point in the control-flow identified by the
318  /// context instruction, CxtI.
319  bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI,
320                               const DominatorTree *DT = nullptr);
321
322  enum class OverflowResult { AlwaysOverflows, MayOverflow, NeverOverflows };
323  OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
324                                               const DataLayout &DL,
325                                               AssumptionCache *AC,
326                                               const Instruction *CxtI,
327                                               const DominatorTree *DT);
328  OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
329                                               const DataLayout &DL,
330                                               AssumptionCache *AC,
331                                               const Instruction *CxtI,
332                                               const DominatorTree *DT);
333  OverflowResult computeOverflowForSignedAdd(Value *LHS, Value *RHS,
334                                             const DataLayout &DL,
335                                             AssumptionCache *AC = nullptr,
336                                             const Instruction *CxtI = nullptr,
337                                             const DominatorTree *DT = nullptr);
338  /// This version also leverages the sign bit of Add if known.
339  OverflowResult computeOverflowForSignedAdd(AddOperator *Add,
340                                             const DataLayout &DL,
341                                             AssumptionCache *AC = nullptr,
342                                             const Instruction *CxtI = nullptr,
343                                             const DominatorTree *DT = nullptr);
344
345  /// Returns true if the arithmetic part of the \p II 's result is
346  /// used only along the paths control dependent on the computation
347  /// not overflowing, \p II being an <op>.with.overflow intrinsic.
348  bool isOverflowIntrinsicNoWrap(IntrinsicInst *II, DominatorTree &DT);
349
350  /// Return true if this function can prove that the instruction I will
351  /// always transfer execution to one of its successors (including the next
352  /// instruction that follows within a basic block). E.g. this is not
353  /// guaranteed for function calls that could loop infinitely.
354  ///
355  /// In other words, this function returns false for instructions that may
356  /// transfer execution or fail to transfer execution in a way that is not
357  /// captured in the CFG nor in the sequence of instructions within a basic
358  /// block.
359  ///
360  /// Undefined behavior is assumed not to happen, so e.g. division is
361  /// guaranteed to transfer execution to the following instruction even
362  /// though division by zero might cause undefined behavior.
363  bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I);
364
365  /// Return true if this function can prove that the instruction I
366  /// is executed for every iteration of the loop L.
367  ///
368  /// Note that this currently only considers the loop header.
369  bool isGuaranteedToExecuteForEveryIteration(const Instruction *I,
370                                              const Loop *L);
371
372  /// Return true if this function can prove that I is guaranteed to yield
373  /// full-poison (all bits poison) if at least one of its operands are
374  /// full-poison (all bits poison).
375  ///
376  /// The exact rules for how poison propagates through instructions have
377  /// not been settled as of 2015-07-10, so this function is conservative
378  /// and only considers poison to be propagated in uncontroversial
379  /// cases. There is no attempt to track values that may be only partially
380  /// poison.
381  bool propagatesFullPoison(const Instruction *I);
382
383  /// Return either nullptr or an operand of I such that I will trigger
384  /// undefined behavior if I is executed and that operand has a full-poison
385  /// value (all bits poison).
386  const Value *getGuaranteedNonFullPoisonOp(const Instruction *I);
387
388  /// Return true if this function can prove that if PoisonI is executed
389  /// and yields a full-poison value (all bits poison), then that will
390  /// trigger undefined behavior.
391  ///
392  /// Note that this currently only considers the basic block that is
393  /// the parent of I.
394  bool isKnownNotFullPoison(const Instruction *PoisonI);
395
396  /// \brief Specific patterns of select instructions we can match.
397  enum SelectPatternFlavor {
398    SPF_UNKNOWN = 0,
399    SPF_SMIN,                   /// Signed minimum
400    SPF_UMIN,                   /// Unsigned minimum
401    SPF_SMAX,                   /// Signed maximum
402    SPF_UMAX,                   /// Unsigned maximum
403    SPF_FMINNUM,                /// Floating point minnum
404    SPF_FMAXNUM,                /// Floating point maxnum
405    SPF_ABS,                    /// Absolute value
406    SPF_NABS                    /// Negated absolute value
407  };
408  /// \brief Behavior when a floating point min/max is given one NaN and one
409  /// non-NaN as input.
410  enum SelectPatternNaNBehavior {
411    SPNB_NA = 0,                /// NaN behavior not applicable.
412    SPNB_RETURNS_NAN,           /// Given one NaN input, returns the NaN.
413    SPNB_RETURNS_OTHER,         /// Given one NaN input, returns the non-NaN.
414    SPNB_RETURNS_ANY            /// Given one NaN input, can return either (or
415                                /// it has been determined that no operands can
416                                /// be NaN).
417  };
418  struct SelectPatternResult {
419    SelectPatternFlavor Flavor;
420    SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is
421                                          /// SPF_FMINNUM or SPF_FMAXNUM.
422    bool Ordered;               /// When implementing this min/max pattern as
423                                /// fcmp; select, does the fcmp have to be
424                                /// ordered?
425
426    /// \brief Return true if \p SPF is a min or a max pattern.
427    static bool isMinOrMax(SelectPatternFlavor SPF) {
428      return !(SPF == SPF_UNKNOWN || SPF == SPF_ABS || SPF == SPF_NABS);
429    }
430  };
431  /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind
432  /// and providing the out parameter results if we successfully match.
433  ///
434  /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does
435  /// not match that of the original select. If this is the case, the cast
436  /// operation (one of Trunc,SExt,Zext) that must be done to transform the
437  /// type of LHS and RHS into the type of V is returned in CastOp.
438  ///
439  /// For example:
440  ///   %1 = icmp slt i32 %a, i32 4
441  ///   %2 = sext i32 %a to i64
442  ///   %3 = select i1 %1, i64 %2, i64 4
443  ///
444  /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt
445  ///
446  SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
447                                         Instruction::CastOps *CastOp = nullptr);
448
449  /// Parse out a conservative ConstantRange from !range metadata.
450  ///
451  /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
452  ConstantRange getConstantRangeFromMetadata(MDNode &RangeMD);
453
454  /// Return true if RHS is known to be implied true by LHS.  Return false if
455  /// RHS is known to be implied false by LHS.  Otherwise, return None if no
456  /// implication can be made.
457  /// A & B must be i1 (boolean) values or a vector of such values. Note that
458  /// the truth table for implication is the same as <=u on i1 values (but not
459  /// <=s!).  The truth table for both is:
460  ///    | T | F (B)
461  ///  T | T | F
462  ///  F | T | T
463  /// (A)
464  Optional<bool> isImpliedCondition(
465      Value *LHS, Value *RHS, const DataLayout &DL, bool InvertAPred = false,
466      unsigned Depth = 0, AssumptionCache *AC = nullptr,
467      const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr);
468} // end namespace llvm
469
470#endif
471