Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Analysis/ValueTracking.h - Walk computations --------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains routines that help analyze properties that chains of |
| 10 | // computations have. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_ANALYSIS_VALUETRACKING_H |
| 15 | #define LLVM_ANALYSIS_VALUETRACKING_H |
| 16 | |
| 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/Optional.h" |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 19 | #include "llvm/ADT/SmallSet.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | #include "llvm/IR/CallSite.h" |
| 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/Instruction.h" |
| 23 | #include "llvm/IR/Intrinsics.h" |
| 24 | #include <cassert> |
| 25 | #include <cstdint> |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | class AddOperator; |
| 30 | class APInt; |
| 31 | class AssumptionCache; |
| 32 | class DataLayout; |
| 33 | class DominatorTree; |
| 34 | class GEPOperator; |
| 35 | class IntrinsicInst; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 36 | class WithOverflowInst; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | struct KnownBits; |
| 38 | class Loop; |
| 39 | class LoopInfo; |
| 40 | class MDNode; |
| 41 | class OptimizationRemarkEmitter; |
| 42 | class StringRef; |
| 43 | class TargetLibraryInfo; |
| 44 | class Value; |
| 45 | |
| 46 | /// Determine which bits of V are known to be either zero or one and return |
| 47 | /// them in the KnownZero/KnownOne bit sets. |
| 48 | /// |
| 49 | /// This function is defined on values with integer type, values with pointer |
| 50 | /// type, and vectors of integers. In the case |
| 51 | /// where V is a vector, the known zero and known one values are the |
| 52 | /// same width as the vector element, and the bit is set only if it is true |
| 53 | /// for all of the elements in the vector. |
| 54 | void computeKnownBits(const Value *V, KnownBits &Known, |
| 55 | const DataLayout &DL, unsigned Depth = 0, |
| 56 | AssumptionCache *AC = nullptr, |
| 57 | const Instruction *CxtI = nullptr, |
| 58 | const DominatorTree *DT = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 59 | OptimizationRemarkEmitter *ORE = nullptr, |
| 60 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | |
| 62 | /// Returns the known bits rather than passing by reference. |
| 63 | KnownBits computeKnownBits(const Value *V, const DataLayout &DL, |
| 64 | unsigned Depth = 0, AssumptionCache *AC = nullptr, |
| 65 | const Instruction *CxtI = nullptr, |
| 66 | const DominatorTree *DT = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 67 | OptimizationRemarkEmitter *ORE = nullptr, |
| 68 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 69 | |
| 70 | /// Compute known bits from the range metadata. |
| 71 | /// \p KnownZero the set of bits that are known to be zero |
| 72 | /// \p KnownOne the set of bits that are known to be one |
| 73 | void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
| 74 | KnownBits &Known); |
| 75 | |
| 76 | /// Return true if LHS and RHS have no common bits set. |
| 77 | bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS, |
| 78 | const DataLayout &DL, |
| 79 | AssumptionCache *AC = nullptr, |
| 80 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 81 | const DominatorTree *DT = nullptr, |
| 82 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | |
| 84 | /// Return true if the given value is known to have exactly one bit set when |
| 85 | /// defined. For vectors return true if every element is known to be a power |
| 86 | /// of two when defined. Supports values with integer or pointer type and |
| 87 | /// vectors of integers. If 'OrZero' is set, then return true if the given |
| 88 | /// value is either a power of two or zero. |
| 89 | bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, |
| 90 | bool OrZero = false, unsigned Depth = 0, |
| 91 | AssumptionCache *AC = nullptr, |
| 92 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 93 | const DominatorTree *DT = nullptr, |
| 94 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 95 | |
| 96 | bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI); |
| 97 | |
| 98 | /// Return true if the given value is known to be non-zero when defined. For |
| 99 | /// vectors, return true if every element is known to be non-zero when |
| 100 | /// defined. For pointers, if the context instruction and dominator tree are |
| 101 | /// specified, perform context-sensitive analysis and return true if the |
| 102 | /// pointer couldn't possibly be null at the specified instruction. |
| 103 | /// Supports values with integer or pointer type and vectors of integers. |
| 104 | bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0, |
| 105 | AssumptionCache *AC = nullptr, |
| 106 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 107 | const DominatorTree *DT = nullptr, |
| 108 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 110 | /// Return true if the two given values are negation. |
| 111 | /// Currently can recoginze Value pair: |
| 112 | /// 1: <X, Y> if X = sub (0, Y) or Y = sub (0, X) |
| 113 | /// 2: <X, Y> if X = sub (A, B) and Y = sub (B, A) |
| 114 | bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false); |
| 115 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 116 | /// Returns true if the give value is known to be non-negative. |
| 117 | bool isKnownNonNegative(const Value *V, const DataLayout &DL, |
| 118 | unsigned Depth = 0, |
| 119 | AssumptionCache *AC = nullptr, |
| 120 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 121 | const DominatorTree *DT = nullptr, |
| 122 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 123 | |
| 124 | /// Returns true if the given value is known be positive (i.e. non-negative |
| 125 | /// and non-zero). |
| 126 | bool isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth = 0, |
| 127 | AssumptionCache *AC = nullptr, |
| 128 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 129 | const DominatorTree *DT = nullptr, |
| 130 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 131 | |
| 132 | /// Returns true if the given value is known be negative (i.e. non-positive |
| 133 | /// and non-zero). |
| 134 | bool isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth = 0, |
| 135 | AssumptionCache *AC = nullptr, |
| 136 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 137 | const DominatorTree *DT = nullptr, |
| 138 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 139 | |
| 140 | /// Return true if the given values are known to be non-equal when defined. |
| 141 | /// Supports scalar integer types only. |
| 142 | bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 143 | AssumptionCache *AC = nullptr, |
| 144 | const Instruction *CxtI = nullptr, |
| 145 | const DominatorTree *DT = nullptr, |
| 146 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 147 | |
| 148 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 149 | /// simplify operations downstream. Mask is known to be zero for bits that V |
| 150 | /// cannot have. |
| 151 | /// |
| 152 | /// This function is defined on values with integer type, values with pointer |
| 153 | /// type, and vectors of integers. In the case |
| 154 | /// where V is a vector, the mask, known zero, and known one values are the |
| 155 | /// same width as the vector element, and the bit is set only if it is true |
| 156 | /// for all of the elements in the vector. |
| 157 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, |
| 158 | const DataLayout &DL, |
| 159 | unsigned Depth = 0, AssumptionCache *AC = nullptr, |
| 160 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 161 | const DominatorTree *DT = nullptr, |
| 162 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 163 | |
| 164 | /// Return the number of times the sign bit of the register is replicated into |
| 165 | /// the other bits. We know that at least 1 bit is always equal to the sign |
| 166 | /// bit (itself), but other cases can give us information. For example, |
| 167 | /// immediately after an "ashr X, 2", we know that the top 3 bits are all |
| 168 | /// equal to each other, so we return 3. For vectors, return the number of |
| 169 | /// sign bits for the vector element with the mininum number of known sign |
| 170 | /// bits. |
| 171 | unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, |
| 172 | unsigned Depth = 0, AssumptionCache *AC = nullptr, |
| 173 | const Instruction *CxtI = nullptr, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 174 | const DominatorTree *DT = nullptr, |
| 175 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 176 | |
| 177 | /// This function computes the integer multiple of Base that equals V. If |
| 178 | /// successful, it returns true and returns the multiple in Multiple. If |
| 179 | /// unsuccessful, it returns false. Also, if V can be simplified to an |
| 180 | /// integer, then the simplified V is returned in Val. Look through sext only |
| 181 | /// if LookThroughSExt=true. |
| 182 | bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
| 183 | bool LookThroughSExt = false, |
| 184 | unsigned Depth = 0); |
| 185 | |
| 186 | /// Map a call instruction to an intrinsic ID. Libcalls which have equivalent |
| 187 | /// intrinsics are treated as-if they were intrinsics. |
| 188 | Intrinsic::ID getIntrinsicForCallSite(ImmutableCallSite ICS, |
| 189 | const TargetLibraryInfo *TLI); |
| 190 | |
| 191 | /// Return true if we can prove that the specified FP value is never equal to |
| 192 | /// -0.0. |
| 193 | bool CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, |
| 194 | unsigned Depth = 0); |
| 195 | |
| 196 | /// Return true if we can prove that the specified FP value is either NaN or |
| 197 | /// never less than -0.0. |
| 198 | /// |
| 199 | /// NaN --> true |
| 200 | /// +0 --> true |
| 201 | /// -0 --> true |
| 202 | /// x > +0 --> true |
| 203 | /// x < -0 --> false |
| 204 | bool CannotBeOrderedLessThanZero(const Value *V, const TargetLibraryInfo *TLI); |
| 205 | |
| 206 | /// Return true if the floating-point scalar value is not a NaN or if the |
| 207 | /// floating-point vector value has no NaN elements. Return false if a value |
| 208 | /// could ever be NaN. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 209 | bool isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, |
| 210 | unsigned Depth = 0); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 211 | |
| 212 | /// Return true if we can prove that the specified FP value's sign bit is 0. |
| 213 | /// |
| 214 | /// NaN --> true/false (depending on the NaN's sign bit) |
| 215 | /// +0 --> true |
| 216 | /// -0 --> false |
| 217 | /// x > +0 --> true |
| 218 | /// x < -0 --> false |
| 219 | bool SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI); |
| 220 | |
| 221 | /// If the specified value can be set by repeating the same byte in memory, |
| 222 | /// return the i8 value that it is represented with. This is true for all i8 |
| 223 | /// values obviously, but is also true for i32 0, i32 -1, i16 0xF0F0, double |
| 224 | /// 0.0 etc. If the value can't be handled with a repeated byte store (e.g. |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 225 | /// i16 0x1234), return null. If the value is entirely undef and padding, |
| 226 | /// return undef. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 227 | Value *isBytewiseValue(Value *V); |
| 228 | |
| 229 | /// Given an aggregrate and an sequence of indices, see if the scalar value |
| 230 | /// indexed is already around as a register, for example if it were inserted |
| 231 | /// directly into the aggregrate. |
| 232 | /// |
| 233 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 234 | /// insertvalues when a part of a nested struct is extracted. |
| 235 | Value *FindInsertedValue(Value *V, |
| 236 | ArrayRef<unsigned> idx_range, |
| 237 | Instruction *InsertBefore = nullptr); |
| 238 | |
| 239 | /// Analyze the specified pointer to see if it can be expressed as a base |
| 240 | /// pointer plus a constant offset. Return the base and offset to the caller. |
| 241 | Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
| 242 | const DataLayout &DL); |
| 243 | inline const Value *GetPointerBaseWithConstantOffset(const Value *Ptr, |
| 244 | int64_t &Offset, |
| 245 | const DataLayout &DL) { |
| 246 | return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset, |
| 247 | DL); |
| 248 | } |
| 249 | |
| 250 | /// Returns true if the GEP is based on a pointer to a string (array of |
| 251 | // \p CharSize integers) and is indexing into this string. |
| 252 | bool isGEPBasedOnPointerToString(const GEPOperator *GEP, |
| 253 | unsigned CharSize = 8); |
| 254 | |
| 255 | /// Represents offset+length into a ConstantDataArray. |
| 256 | struct ConstantDataArraySlice { |
| 257 | /// ConstantDataArray pointer. nullptr indicates a zeroinitializer (a valid |
| 258 | /// initializer, it just doesn't fit the ConstantDataArray interface). |
| 259 | const ConstantDataArray *Array; |
| 260 | |
| 261 | /// Slice starts at this Offset. |
| 262 | uint64_t Offset; |
| 263 | |
| 264 | /// Length of the slice. |
| 265 | uint64_t Length; |
| 266 | |
| 267 | /// Moves the Offset and adjusts Length accordingly. |
| 268 | void move(uint64_t Delta) { |
| 269 | assert(Delta < Length); |
| 270 | Offset += Delta; |
| 271 | Length -= Delta; |
| 272 | } |
| 273 | |
| 274 | /// Convenience accessor for elements in the slice. |
| 275 | uint64_t operator[](unsigned I) const { |
| 276 | return Array==nullptr ? 0 : Array->getElementAsInteger(I + Offset); |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | /// Returns true if the value \p V is a pointer into a ConstantDataArray. |
| 281 | /// If successful \p Slice will point to a ConstantDataArray info object |
| 282 | /// with an appropriate offset. |
| 283 | bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, |
| 284 | unsigned ElementSize, uint64_t Offset = 0); |
| 285 | |
| 286 | /// This function computes the length of a null-terminated C string pointed to |
| 287 | /// by V. If successful, it returns true and returns the string in Str. If |
| 288 | /// unsuccessful, it returns false. This does not include the trailing null |
| 289 | /// character by default. If TrimAtNul is set to false, then this returns any |
| 290 | /// trailing null characters as well as any other characters that come after |
| 291 | /// it. |
| 292 | bool getConstantStringInfo(const Value *V, StringRef &Str, |
| 293 | uint64_t Offset = 0, bool TrimAtNul = true); |
| 294 | |
| 295 | /// If we can compute the length of the string pointed to by the specified |
| 296 | /// pointer, return 'len+1'. If we can't, return 0. |
| 297 | uint64_t GetStringLength(const Value *V, unsigned CharSize = 8); |
| 298 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 299 | /// This function returns call pointer argument that is considered the same by |
| 300 | /// aliasing rules. You CAN'T use it to replace one value with another. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 301 | const Value *getArgumentAliasingToReturnedPointer(const CallBase *Call); |
| 302 | inline Value *getArgumentAliasingToReturnedPointer(CallBase *Call) { |
| 303 | return const_cast<Value *>(getArgumentAliasingToReturnedPointer( |
| 304 | const_cast<const CallBase *>(Call))); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // {launder,strip}.invariant.group returns pointer that aliases its argument, |
| 308 | // and it only captures pointer by returning it. |
| 309 | // These intrinsics are not marked as nocapture, because returning is |
| 310 | // considered as capture. The arguments are not marked as returned neither, |
| 311 | // because it would make it useless. |
| 312 | bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 313 | const CallBase *Call); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 314 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 315 | /// This method strips off any GEP address adjustments and pointer casts from |
| 316 | /// the specified value, returning the original object being addressed. Note |
| 317 | /// that the returned value has pointer type if the specified value does. If |
| 318 | /// the MaxLookup value is non-zero, it limits the number of instructions to |
| 319 | /// be stripped off. |
| 320 | Value *GetUnderlyingObject(Value *V, const DataLayout &DL, |
| 321 | unsigned MaxLookup = 6); |
| 322 | inline const Value *GetUnderlyingObject(const Value *V, const DataLayout &DL, |
| 323 | unsigned MaxLookup = 6) { |
| 324 | return GetUnderlyingObject(const_cast<Value *>(V), DL, MaxLookup); |
| 325 | } |
| 326 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 327 | /// This method is similar to GetUnderlyingObject except that it can |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 328 | /// look through phi and select instructions and return multiple objects. |
| 329 | /// |
| 330 | /// If LoopInfo is passed, loop phis are further analyzed. If a pointer |
| 331 | /// accesses different objects in each iteration, we don't look through the |
| 332 | /// phi node. E.g. consider this loop nest: |
| 333 | /// |
| 334 | /// int **A; |
| 335 | /// for (i) |
| 336 | /// for (j) { |
| 337 | /// A[i][j] = A[i-1][j] * B[j] |
| 338 | /// } |
| 339 | /// |
| 340 | /// This is transformed by Load-PRE to stash away A[i] for the next iteration |
| 341 | /// of the outer loop: |
| 342 | /// |
| 343 | /// Curr = A[0]; // Prev_0 |
| 344 | /// for (i: 1..N) { |
| 345 | /// Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 346 | /// Curr = A[i]; |
| 347 | /// for (j: 0..N) { |
| 348 | /// Curr[j] = Prev[j] * B[j] |
| 349 | /// } |
| 350 | /// } |
| 351 | /// |
| 352 | /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects |
| 353 | /// should not assume that Curr and Prev share the same underlying object thus |
| 354 | /// it shouldn't look through the phi above. |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 355 | void GetUnderlyingObjects(const Value *V, |
| 356 | SmallVectorImpl<const Value *> &Objects, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 357 | const DataLayout &DL, LoopInfo *LI = nullptr, |
| 358 | unsigned MaxLookup = 6); |
| 359 | |
| 360 | /// This is a wrapper around GetUnderlyingObjects and adds support for basic |
| 361 | /// ptrtoint+arithmetic+inttoptr sequences. |
| 362 | bool getUnderlyingObjectsForCodeGen(const Value *V, |
| 363 | SmallVectorImpl<Value *> &Objects, |
| 364 | const DataLayout &DL); |
| 365 | |
| 366 | /// Return true if the only users of this pointer are lifetime markers. |
| 367 | bool onlyUsedByLifetimeMarkers(const Value *V); |
| 368 | |
| 369 | /// Return true if the instruction does not have any effects besides |
| 370 | /// calculating the result and does not have undefined behavior. |
| 371 | /// |
| 372 | /// This method never returns true for an instruction that returns true for |
| 373 | /// mayHaveSideEffects; however, this method also does some other checks in |
| 374 | /// addition. It checks for undefined behavior, like dividing by zero or |
| 375 | /// loading from an invalid pointer (but not for undefined results, like a |
| 376 | /// shift with a shift amount larger than the width of the result). It checks |
| 377 | /// for malloc and alloca because speculatively executing them might cause a |
| 378 | /// memory leak. It also returns false for instructions related to control |
| 379 | /// flow, specifically terminators and PHI nodes. |
| 380 | /// |
| 381 | /// If the CtxI is specified this method performs context-sensitive analysis |
| 382 | /// and returns true if it is safe to execute the instruction immediately |
| 383 | /// before the CtxI. |
| 384 | /// |
| 385 | /// If the CtxI is NOT specified this method only looks at the instruction |
| 386 | /// itself and its operands, so if this method returns true, it is safe to |
| 387 | /// move the instruction as long as the correct dominance relationships for |
| 388 | /// the operands and users hold. |
| 389 | /// |
| 390 | /// This method can return true for instructions that read memory; |
| 391 | /// for such instructions, moving them may change the resulting value. |
| 392 | bool isSafeToSpeculativelyExecute(const Value *V, |
| 393 | const Instruction *CtxI = nullptr, |
| 394 | const DominatorTree *DT = nullptr); |
| 395 | |
| 396 | /// Returns true if the result or effects of the given instructions \p I |
| 397 | /// depend on or influence global memory. |
| 398 | /// Memory dependence arises for example if the instruction reads from |
| 399 | /// memory or may produce effects or undefined behaviour. Memory dependent |
| 400 | /// instructions generally cannot be reorderd with respect to other memory |
| 401 | /// dependent instructions or moved into non-dominated basic blocks. |
| 402 | /// Instructions which just compute a value based on the values of their |
| 403 | /// operands are not memory dependent. |
| 404 | bool mayBeMemoryDependent(const Instruction &I); |
| 405 | |
| 406 | /// Return true if it is an intrinsic that cannot be speculated but also |
| 407 | /// cannot trap. |
| 408 | bool isAssumeLikeIntrinsic(const Instruction *I); |
| 409 | |
| 410 | /// Return true if it is valid to use the assumptions provided by an |
| 411 | /// assume intrinsic, I, at the point in the control-flow identified by the |
| 412 | /// context instruction, CxtI. |
| 413 | bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, |
| 414 | const DominatorTree *DT = nullptr); |
| 415 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 416 | enum class OverflowResult { |
| 417 | /// Always overflows in the direction of signed/unsigned min value. |
| 418 | AlwaysOverflowsLow, |
| 419 | /// Always overflows in the direction of signed/unsigned max value. |
| 420 | AlwaysOverflowsHigh, |
| 421 | /// May or may not overflow. |
| 422 | MayOverflow, |
| 423 | /// Never overflows. |
| 424 | NeverOverflows, |
| 425 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 426 | |
| 427 | OverflowResult computeOverflowForUnsignedMul(const Value *LHS, |
| 428 | const Value *RHS, |
| 429 | const DataLayout &DL, |
| 430 | AssumptionCache *AC, |
| 431 | const Instruction *CxtI, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 432 | const DominatorTree *DT, |
| 433 | bool UseInstrInfo = true); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 434 | OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, |
| 435 | const DataLayout &DL, |
| 436 | AssumptionCache *AC, |
| 437 | const Instruction *CxtI, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 438 | const DominatorTree *DT, |
| 439 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 440 | OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, |
| 441 | const Value *RHS, |
| 442 | const DataLayout &DL, |
| 443 | AssumptionCache *AC, |
| 444 | const Instruction *CxtI, |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 445 | const DominatorTree *DT, |
| 446 | bool UseInstrInfo = true); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 447 | OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS, |
| 448 | const DataLayout &DL, |
| 449 | AssumptionCache *AC = nullptr, |
| 450 | const Instruction *CxtI = nullptr, |
| 451 | const DominatorTree *DT = nullptr); |
| 452 | /// This version also leverages the sign bit of Add if known. |
| 453 | OverflowResult computeOverflowForSignedAdd(const AddOperator *Add, |
| 454 | const DataLayout &DL, |
| 455 | AssumptionCache *AC = nullptr, |
| 456 | const Instruction *CxtI = nullptr, |
| 457 | const DominatorTree *DT = nullptr); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 458 | OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, |
| 459 | const DataLayout &DL, |
| 460 | AssumptionCache *AC, |
| 461 | const Instruction *CxtI, |
| 462 | const DominatorTree *DT); |
| 463 | OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, |
| 464 | const DataLayout &DL, |
| 465 | AssumptionCache *AC, |
| 466 | const Instruction *CxtI, |
| 467 | const DominatorTree *DT); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 468 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 469 | /// Returns true if the arithmetic part of the \p WO 's result is |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 470 | /// used only along the paths control dependent on the computation |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 471 | /// not overflowing, \p WO being an <op>.with.overflow intrinsic. |
| 472 | bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 473 | const DominatorTree &DT); |
| 474 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 475 | |
| 476 | /// Determine the possible constant range of an integer or vector of integer |
| 477 | /// value. This is intended as a cheap, non-recursive check. |
| 478 | ConstantRange computeConstantRange(const Value *V, bool UseInstrInfo = true); |
| 479 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 480 | /// Return true if this function can prove that the instruction I will |
| 481 | /// always transfer execution to one of its successors (including the next |
| 482 | /// instruction that follows within a basic block). E.g. this is not |
| 483 | /// guaranteed for function calls that could loop infinitely. |
| 484 | /// |
| 485 | /// In other words, this function returns false for instructions that may |
| 486 | /// transfer execution or fail to transfer execution in a way that is not |
| 487 | /// captured in the CFG nor in the sequence of instructions within a basic |
| 488 | /// block. |
| 489 | /// |
| 490 | /// Undefined behavior is assumed not to happen, so e.g. division is |
| 491 | /// guaranteed to transfer execution to the following instruction even |
| 492 | /// though division by zero might cause undefined behavior. |
| 493 | bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I); |
| 494 | |
| 495 | /// Returns true if this block does not contain a potential implicit exit. |
| 496 | /// This is equivelent to saying that all instructions within the basic block |
| 497 | /// are guaranteed to transfer execution to their successor within the basic |
| 498 | /// block. This has the same assumptions w.r.t. undefined behavior as the |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 499 | /// instruction variant of this function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 500 | bool isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB); |
| 501 | |
| 502 | /// Return true if this function can prove that the instruction I |
| 503 | /// is executed for every iteration of the loop L. |
| 504 | /// |
| 505 | /// Note that this currently only considers the loop header. |
| 506 | bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
| 507 | const Loop *L); |
| 508 | |
| 509 | /// Return true if this function can prove that I is guaranteed to yield |
| 510 | /// full-poison (all bits poison) if at least one of its operands are |
| 511 | /// full-poison (all bits poison). |
| 512 | /// |
| 513 | /// The exact rules for how poison propagates through instructions have |
| 514 | /// not been settled as of 2015-07-10, so this function is conservative |
| 515 | /// and only considers poison to be propagated in uncontroversial |
| 516 | /// cases. There is no attempt to track values that may be only partially |
| 517 | /// poison. |
| 518 | bool propagatesFullPoison(const Instruction *I); |
| 519 | |
| 520 | /// Return either nullptr or an operand of I such that I will trigger |
| 521 | /// undefined behavior if I is executed and that operand has a full-poison |
| 522 | /// value (all bits poison). |
| 523 | const Value *getGuaranteedNonFullPoisonOp(const Instruction *I); |
| 524 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 525 | /// Return true if the given instruction must trigger undefined behavior. |
| 526 | /// when I is executed with any operands which appear in KnownPoison holding |
| 527 | /// a full-poison value at the point of execution. |
| 528 | bool mustTriggerUB(const Instruction *I, |
| 529 | const SmallSet<const Value *, 16>& KnownPoison); |
| 530 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 531 | /// Return true if this function can prove that if PoisonI is executed |
| 532 | /// and yields a full-poison value (all bits poison), then that will |
| 533 | /// trigger undefined behavior. |
| 534 | /// |
| 535 | /// Note that this currently only considers the basic block that is |
| 536 | /// the parent of I. |
| 537 | bool programUndefinedIfFullPoison(const Instruction *PoisonI); |
| 538 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 539 | /// Specific patterns of select instructions we can match. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 540 | enum SelectPatternFlavor { |
| 541 | SPF_UNKNOWN = 0, |
| 542 | SPF_SMIN, /// Signed minimum |
| 543 | SPF_UMIN, /// Unsigned minimum |
| 544 | SPF_SMAX, /// Signed maximum |
| 545 | SPF_UMAX, /// Unsigned maximum |
| 546 | SPF_FMINNUM, /// Floating point minnum |
| 547 | SPF_FMAXNUM, /// Floating point maxnum |
| 548 | SPF_ABS, /// Absolute value |
| 549 | SPF_NABS /// Negated absolute value |
| 550 | }; |
| 551 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 552 | /// Behavior when a floating point min/max is given one NaN and one |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 553 | /// non-NaN as input. |
| 554 | enum SelectPatternNaNBehavior { |
| 555 | SPNB_NA = 0, /// NaN behavior not applicable. |
| 556 | SPNB_RETURNS_NAN, /// Given one NaN input, returns the NaN. |
| 557 | SPNB_RETURNS_OTHER, /// Given one NaN input, returns the non-NaN. |
| 558 | SPNB_RETURNS_ANY /// Given one NaN input, can return either (or |
| 559 | /// it has been determined that no operands can |
| 560 | /// be NaN). |
| 561 | }; |
| 562 | |
| 563 | struct SelectPatternResult { |
| 564 | SelectPatternFlavor Flavor; |
| 565 | SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is |
| 566 | /// SPF_FMINNUM or SPF_FMAXNUM. |
| 567 | bool Ordered; /// When implementing this min/max pattern as |
| 568 | /// fcmp; select, does the fcmp have to be |
| 569 | /// ordered? |
| 570 | |
| 571 | /// Return true if \p SPF is a min or a max pattern. |
| 572 | static bool isMinOrMax(SelectPatternFlavor SPF) { |
| 573 | return SPF != SPF_UNKNOWN && SPF != SPF_ABS && SPF != SPF_NABS; |
| 574 | } |
| 575 | }; |
| 576 | |
| 577 | /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind |
| 578 | /// and providing the out parameter results if we successfully match. |
| 579 | /// |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 580 | /// For ABS/NABS, LHS will be set to the input to the abs idiom. RHS will be |
| 581 | /// the negation instruction from the idiom. |
| 582 | /// |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 583 | /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does |
| 584 | /// not match that of the original select. If this is the case, the cast |
| 585 | /// operation (one of Trunc,SExt,Zext) that must be done to transform the |
| 586 | /// type of LHS and RHS into the type of V is returned in CastOp. |
| 587 | /// |
| 588 | /// For example: |
| 589 | /// %1 = icmp slt i32 %a, i32 4 |
| 590 | /// %2 = sext i32 %a to i64 |
| 591 | /// %3 = select i1 %1, i64 %2, i64 4 |
| 592 | /// |
| 593 | /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt |
| 594 | /// |
| 595 | SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, |
| 596 | Instruction::CastOps *CastOp = nullptr, |
| 597 | unsigned Depth = 0); |
| 598 | inline SelectPatternResult |
| 599 | matchSelectPattern(const Value *V, const Value *&LHS, const Value *&RHS, |
| 600 | Instruction::CastOps *CastOp = nullptr) { |
| 601 | Value *L = const_cast<Value*>(LHS); |
| 602 | Value *R = const_cast<Value*>(RHS); |
| 603 | auto Result = matchSelectPattern(const_cast<Value*>(V), L, R); |
| 604 | LHS = L; |
| 605 | RHS = R; |
| 606 | return Result; |
| 607 | } |
| 608 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame^] | 609 | /// Determine the pattern that a select with the given compare as its |
| 610 | /// predicate and given values as its true/false operands would match. |
| 611 | SelectPatternResult matchDecomposedSelectPattern( |
| 612 | CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, |
| 613 | Instruction::CastOps *CastOp = nullptr, unsigned Depth = 0); |
| 614 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 615 | /// Return the canonical comparison predicate for the specified |
| 616 | /// minimum/maximum flavor. |
| 617 | CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, |
| 618 | bool Ordered = false); |
| 619 | |
| 620 | /// Return the inverse minimum/maximum flavor of the specified flavor. |
| 621 | /// For example, signed minimum is the inverse of signed maximum. |
| 622 | SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF); |
| 623 | |
| 624 | /// Return the canonical inverse comparison predicate for the specified |
| 625 | /// minimum/maximum flavor. |
| 626 | CmpInst::Predicate getInverseMinMaxPred(SelectPatternFlavor SPF); |
| 627 | |
| 628 | /// Return true if RHS is known to be implied true by LHS. Return false if |
| 629 | /// RHS is known to be implied false by LHS. Otherwise, return None if no |
| 630 | /// implication can be made. |
| 631 | /// A & B must be i1 (boolean) values or a vector of such values. Note that |
| 632 | /// the truth table for implication is the same as <=u on i1 values (but not |
| 633 | /// <=s!). The truth table for both is: |
| 634 | /// | T | F (B) |
| 635 | /// T | T | F |
| 636 | /// F | T | T |
| 637 | /// (A) |
| 638 | Optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS, |
| 639 | const DataLayout &DL, bool LHSIsTrue = true, |
| 640 | unsigned Depth = 0); |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 641 | |
| 642 | /// Return the boolean condition value in the context of the given instruction |
| 643 | /// if it is known based on dominating conditions. |
| 644 | Optional<bool> isImpliedByDomCondition(const Value *Cond, |
| 645 | const Instruction *ContextI, |
| 646 | const DataLayout &DL); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 647 | } // end namespace llvm |
| 648 | |
| 649 | #endif // LLVM_ANALYSIS_VALUETRACKING_H |