Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
9.5 KiB

  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. #ifndef LLVM_ANALYSIS_VALUETRACKING_H
  15. #define LLVM_ANALYSIS_VALUETRACKING_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/Support/DataTypes.h"
  18. namespace llvm {
  19. class Value;
  20. class Instruction;
  21. class APInt;
  22. class DataLayout;
  23. class StringRef;
  24. class MDNode;
  25. /// ComputeMaskedBits - Determine which of the bits specified in Mask are
  26. /// known to be either zero or one and return them in the KnownZero/KnownOne
  27. /// bit sets. This code only analyzes bits in Mask, in order to short-circuit
  28. /// processing.
  29. ///
  30. /// This function is defined on values with integer type, values with pointer
  31. /// type (but only if TD is non-null), and vectors of integers. In the case
  32. /// where V is a vector, the mask, known zero, and known one values are the
  33. /// same width as the vector element, and the bit is set only if it is true
  34. /// for all of the elements in the vector.
  35. void ComputeMaskedBits(Value *V, APInt &KnownZero, APInt &KnownOne,
  36. const DataLayout *TD = 0, unsigned Depth = 0);
  37. void computeMaskedBitsLoad(const MDNode &Ranges, APInt &KnownZero);
  38. /// ComputeSignBit - Determine whether the sign bit is known to be zero or
  39. /// one. Convenience wrapper around ComputeMaskedBits.
  40. void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
  41. const DataLayout *TD = 0, unsigned Depth = 0);
  42. /// isKnownToBeAPowerOfTwo - Return true if the given value is known to have
  43. /// exactly one bit set when defined. For vectors return true if every
  44. /// element is known to be a power of two when defined. Supports values with
  45. /// integer or pointer type and vectors of integers. If 'OrZero' is set then
  46. /// returns true if the given value is either a power of two or zero.
  47. bool isKnownToBeAPowerOfTwo(Value *V, bool OrZero = false, unsigned Depth = 0);
  48. /// isKnownNonZero - Return true if the given value is known to be non-zero
  49. /// when defined. For vectors return true if every element is known to be
  50. /// non-zero when defined. Supports values with integer or pointer type and
  51. /// vectors of integers.
  52. bool isKnownNonZero(Value *V, const DataLayout *TD = 0, unsigned Depth = 0);
  53. /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
  54. /// this predicate to simplify operations downstream. Mask is known to be
  55. /// zero for bits that V cannot have.
  56. ///
  57. /// This function is defined on values with integer type, values with pointer
  58. /// type (but only if TD is non-null), and vectors of integers. In the case
  59. /// where V is a vector, the mask, known zero, and known one values are the
  60. /// same width as the vector element, and the bit is set only if it is true
  61. /// for all of the elements in the vector.
  62. bool MaskedValueIsZero(Value *V, const APInt &Mask,
  63. const DataLayout *TD = 0, unsigned Depth = 0);
  64. /// ComputeNumSignBits - Return the number of times the sign bit of the
  65. /// register is replicated into the other bits. We know that at least 1 bit
  66. /// is always equal to the sign bit (itself), but other cases can give us
  67. /// information. For example, immediately after an "ashr X, 2", we know that
  68. /// the top 3 bits are all equal to each other, so we return 3.
  69. ///
  70. /// 'Op' must have a scalar integer type.
  71. ///
  72. unsigned ComputeNumSignBits(Value *Op, const DataLayout *TD = 0,
  73. unsigned Depth = 0);
  74. /// ComputeMultiple - This function computes the integer multiple of Base that
  75. /// equals V. If successful, it returns true and returns the multiple in
  76. /// Multiple. If unsuccessful, it returns false. Also, if V can be
  77. /// simplified to an integer, then the simplified V is returned in Val. Look
  78. /// through sext only if LookThroughSExt=true.
  79. bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
  80. bool LookThroughSExt = false,
  81. unsigned Depth = 0);
  82. /// CannotBeNegativeZero - Return true if we can prove that the specified FP
  83. /// value is never equal to -0.0.
  84. ///
  85. bool CannotBeNegativeZero(const Value *V, unsigned Depth = 0);
  86. /// isBytewiseValue - If the specified value can be set by repeating the same
  87. /// byte in memory, return the i8 value that it is represented with. This is
  88. /// true for all i8 values obviously, but is also true for i32 0, i32 -1,
  89. /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
  90. /// byte store (e.g. i16 0x1234), return null.
  91. Value *isBytewiseValue(Value *V);
  92. /// FindInsertedValue - Given an aggregrate and an sequence of indices, see if
  93. /// the scalar value indexed is already around as a register, for example if
  94. /// it were inserted directly into the aggregrate.
  95. ///
  96. /// If InsertBefore is not null, this function will duplicate (modified)
  97. /// insertvalues when a part of a nested struct is extracted.
  98. Value *FindInsertedValue(Value *V,
  99. ArrayRef<unsigned> idx_range,
  100. Instruction *InsertBefore = 0);
  101. /// GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if
  102. /// it can be expressed as a base pointer plus a constant offset. Return the
  103. /// base and offset to the caller.
  104. Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
  105. const DataLayout *TD);
  106. static inline const Value *
  107. GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
  108. const DataLayout *TD) {
  109. return GetPointerBaseWithConstantOffset(const_cast<Value*>(Ptr), Offset,TD);
  110. }
  111. /// getConstantStringInfo - This function computes the length of a
  112. /// null-terminated C string pointed to by V. If successful, it returns true
  113. /// and returns the string in Str. If unsuccessful, it returns false. This
  114. /// does not include the trailing nul character by default. If TrimAtNul is
  115. /// set to false, then this returns any trailing nul characters as well as any
  116. /// other characters that come after it.
  117. bool getConstantStringInfo(const Value *V, StringRef &Str,
  118. uint64_t Offset = 0, bool TrimAtNul = true);
  119. /// GetStringLength - If we can compute the length of the string pointed to by
  120. /// the specified pointer, return 'len+1'. If we can't, return 0.
  121. uint64_t GetStringLength(Value *V);
  122. /// GetUnderlyingObject - This method strips off any GEP address adjustments
  123. /// and pointer casts from the specified value, returning the original object
  124. /// being addressed. Note that the returned value has pointer type if the
  125. /// specified value does. If the MaxLookup value is non-zero, it limits the
  126. /// number of instructions to be stripped off.
  127. Value *GetUnderlyingObject(Value *V, const DataLayout *TD = 0,
  128. unsigned MaxLookup = 6);
  129. static inline const Value *
  130. GetUnderlyingObject(const Value *V, const DataLayout *TD = 0,
  131. unsigned MaxLookup = 6) {
  132. return GetUnderlyingObject(const_cast<Value *>(V), TD, MaxLookup);
  133. }
  134. /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
  135. /// except that it can look through phi and select instructions and return
  136. /// multiple objects.
  137. void GetUnderlyingObjects(Value *V,
  138. SmallVectorImpl<Value *> &Objects,
  139. const DataLayout *TD = 0,
  140. unsigned MaxLookup = 6);
  141. /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
  142. /// are lifetime markers.
  143. bool onlyUsedByLifetimeMarkers(const Value *V);
  144. /// isSafeToSpeculativelyExecute - Return true if the instruction does not
  145. /// have any effects besides calculating the result and does not have
  146. /// undefined behavior.
  147. ///
  148. /// This method never returns true for an instruction that returns true for
  149. /// mayHaveSideEffects; however, this method also does some other checks in
  150. /// addition. It checks for undefined behavior, like dividing by zero or
  151. /// loading from an invalid pointer (but not for undefined results, like a
  152. /// shift with a shift amount larger than the width of the result). It checks
  153. /// for malloc and alloca because speculatively executing them might cause a
  154. /// memory leak. It also returns false for instructions related to control
  155. /// flow, specifically terminators and PHI nodes.
  156. ///
  157. /// This method only looks at the instruction itself and its operands, so if
  158. /// this method returns true, it is safe to move the instruction as long as
  159. /// the correct dominance relationships for the operands and users hold.
  160. /// However, this method can return true for instructions that read memory;
  161. /// for such instructions, moving them may change the resulting value.
  162. bool isSafeToSpeculativelyExecute(const Value *V,
  163. const DataLayout *TD = 0);
  164. /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
  165. /// its definition. This returns true for allocas, non-extern-weak globals
  166. /// and byval arguments.
  167. bool isKnownNonNull(const Value *V);
  168. } // end namespace llvm
  169. #endif