1 //===- llvm/Analysis/ValueTracking.h - Walk computations --------*- C++ -*-===// 2 // 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 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/Analysis/SimplifyQuery.h" 19 #include "llvm/Analysis/WithCache.h" 20 #include "llvm/IR/Constants.h" 21 #include "llvm/IR/DataLayout.h" 22 #include "llvm/IR/FMF.h" 23 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/InstrTypes.h" 25 #include "llvm/IR/Intrinsics.h" 26 #include <cassert> 27 #include <cstdint> 28 29 namespace llvm { 30 31 class Operator; 32 class AddOperator; 33 class AllocaInst; 34 class APInt; 35 class AssumptionCache; 36 class DominatorTree; 37 class GEPOperator; 38 class LoadInst; 39 class WithOverflowInst; 40 struct KnownBits; 41 class Loop; 42 class LoopInfo; 43 class MDNode; 44 class StringRef; 45 class TargetLibraryInfo; 46 class Value; 47 48 constexpr unsigned MaxAnalysisRecursionDepth = 6; 49 50 /// Determine which bits of V are known to be either zero or one and return 51 /// them in the KnownZero/KnownOne bit sets. 52 /// 53 /// This function is defined on values with integer type, values with pointer 54 /// type, and vectors of integers. In the case 55 /// where V is a vector, the known zero and known one values are the 56 /// same width as the vector element, and the bit is set only if it is true 57 /// for all of the elements in the vector. 58 void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, 59 unsigned Depth = 0, AssumptionCache *AC = nullptr, 60 const Instruction *CxtI = nullptr, 61 const DominatorTree *DT = nullptr, 62 bool UseInstrInfo = true); 63 64 /// Returns the known bits rather than passing by reference. 65 KnownBits computeKnownBits(const Value *V, const DataLayout &DL, 66 unsigned Depth = 0, AssumptionCache *AC = nullptr, 67 const Instruction *CxtI = nullptr, 68 const DominatorTree *DT = nullptr, 69 bool UseInstrInfo = true); 70 71 /// Returns the known bits rather than passing by reference. 72 KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, 73 const DataLayout &DL, unsigned Depth = 0, 74 AssumptionCache *AC = nullptr, 75 const Instruction *CxtI = nullptr, 76 const DominatorTree *DT = nullptr, 77 bool UseInstrInfo = true); 78 79 KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts, 80 unsigned Depth, const SimplifyQuery &Q); 81 82 KnownBits computeKnownBits(const Value *V, unsigned Depth, 83 const SimplifyQuery &Q); 84 85 void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, 86 const SimplifyQuery &Q); 87 88 /// Compute known bits from the range metadata. 89 /// \p KnownZero the set of bits that are known to be zero 90 /// \p KnownOne the set of bits that are known to be one 91 void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known); 92 93 /// Merge bits known from context-dependent facts into Known. 94 void computeKnownBitsFromContext(const Value *V, KnownBits &Known, 95 unsigned Depth, const SimplifyQuery &Q); 96 97 /// Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or). 98 KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, 99 const KnownBits &KnownLHS, 100 const KnownBits &KnownRHS, 101 unsigned Depth, const SimplifyQuery &SQ); 102 103 /// Adjust \p Known for the given select \p Arm to include information from the 104 /// select \p Cond. 105 void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, 106 bool Invert, unsigned Depth, 107 const SimplifyQuery &Q); 108 109 /// Return true if LHS and RHS have no common bits set. 110 bool haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache, 111 const WithCache<const Value *> &RHSCache, 112 const SimplifyQuery &SQ); 113 114 /// Return true if the given value is known to have exactly one bit set when 115 /// defined. For vectors return true if every element is known to be a power 116 /// of two when defined. Supports values with integer or pointer type and 117 /// vectors of integers. If 'OrZero' is set, then return true if the given 118 /// value is either a power of two or zero. 119 bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, 120 bool OrZero = false, unsigned Depth = 0, 121 AssumptionCache *AC = nullptr, 122 const Instruction *CxtI = nullptr, 123 const DominatorTree *DT = nullptr, 124 bool UseInstrInfo = true); 125 126 bool isOnlyUsedInZeroComparison(const Instruction *CxtI); 127 128 bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI); 129 130 /// Return true if the given value is known to be non-zero when defined. For 131 /// vectors, return true if every element is known to be non-zero when 132 /// defined. For pointers, if the context instruction and dominator tree are 133 /// specified, perform context-sensitive analysis and return true if the 134 /// pointer couldn't possibly be null at the specified instruction. 135 /// Supports values with integer or pointer type and vectors of integers. 136 bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth = 0); 137 138 /// Return true if the two given values are negation. 139 /// Currently can recoginze Value pair: 140 /// 1: <X, Y> if X = sub (0, Y) or Y = sub (0, X) 141 /// 2: <X, Y> if X = sub (A, B) and Y = sub (B, A) 142 bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW = false, 143 bool AllowPoison = true); 144 145 /// Return true iff: 146 /// 1. X is poison implies Y is poison. 147 /// 2. X is true implies Y is false. 148 /// 3. X is false implies Y is true. 149 /// Otherwise, return false. 150 bool isKnownInversion(const Value *X, const Value *Y); 151 152 /// Returns true if the give value is known to be non-negative. 153 bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, 154 unsigned Depth = 0); 155 156 /// Returns true if the given value is known be positive (i.e. non-negative 157 /// and non-zero). 158 bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, 159 unsigned Depth = 0); 160 161 /// Returns true if the given value is known be negative (i.e. non-positive 162 /// and non-zero). 163 bool isKnownNegative(const Value *V, const SimplifyQuery &DL, 164 unsigned Depth = 0); 165 166 /// Return true if the given values are known to be non-equal when defined. 167 /// Supports scalar integer types only. 168 bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, 169 AssumptionCache *AC = nullptr, 170 const Instruction *CxtI = nullptr, 171 const DominatorTree *DT = nullptr, 172 bool UseInstrInfo = true); 173 174 /// Return true if 'V & Mask' is known to be zero. We use this predicate to 175 /// simplify operations downstream. Mask is known to be zero for bits that V 176 /// cannot have. 177 /// 178 /// This function is defined on values with integer type, values with pointer 179 /// type, and vectors of integers. In the case 180 /// where V is a vector, the mask, known zero, and known one values are the 181 /// same width as the vector element, and the bit is set only if it is true 182 /// for all of the elements in the vector. 183 bool MaskedValueIsZero(const Value *V, const APInt &Mask, 184 const SimplifyQuery &DL, unsigned Depth = 0); 185 186 /// Return the number of times the sign bit of the register is replicated into 187 /// the other bits. We know that at least 1 bit is always equal to the sign 188 /// bit (itself), but other cases can give us information. For example, 189 /// immediately after an "ashr X, 2", we know that the top 3 bits are all 190 /// equal to each other, so we return 3. For vectors, return the number of 191 /// sign bits for the vector element with the mininum number of known sign 192 /// bits. 193 unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, 194 unsigned Depth = 0, AssumptionCache *AC = nullptr, 195 const Instruction *CxtI = nullptr, 196 const DominatorTree *DT = nullptr, 197 bool UseInstrInfo = true); 198 199 /// Get the upper bound on bit size for this Value \p Op as a signed integer. 200 /// i.e. x == sext(trunc(x to MaxSignificantBits) to bitwidth(x)). 201 /// Similar to the APInt::getSignificantBits function. 202 unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, 203 unsigned Depth = 0, 204 AssumptionCache *AC = nullptr, 205 const Instruction *CxtI = nullptr, 206 const DominatorTree *DT = nullptr); 207 208 /// Map a call instruction to an intrinsic ID. Libcalls which have equivalent 209 /// intrinsics are treated as-if they were intrinsics. 210 Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, 211 const TargetLibraryInfo *TLI); 212 213 /// Given an exploded icmp instruction, return true if the comparison only 214 /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if 215 /// the result of the comparison is true when the input value is signed. 216 bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, 217 bool &TrueIfSigned); 218 219 /// Returns a pair of values, which if passed to llvm.is.fpclass, returns the 220 /// same result as an fcmp with the given operands. 221 /// 222 /// If \p LookThroughSrc is true, consider the input value when computing the 223 /// mask. 224 /// 225 /// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair 226 /// element will always be LHS. 227 std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred, 228 const Function &F, Value *LHS, 229 Value *RHS, 230 bool LookThroughSrc = true); 231 std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred, 232 const Function &F, Value *LHS, 233 const APFloat *ConstRHS, 234 bool LookThroughSrc = true); 235 236 /// Compute the possible floating-point classes that \p LHS could be based on 237 /// fcmp \Pred \p LHS, \p RHS. 238 /// 239 /// \returns { TestedValue, ClassesIfTrue, ClassesIfFalse } 240 /// 241 /// If the compare returns an exact class test, ClassesIfTrue == ~ClassesIfFalse 242 /// 243 /// This is a less exact version of fcmpToClassTest (e.g. fcmpToClassTest will 244 /// only succeed for a test of x > 0 implies positive, but not x > 1). 245 /// 246 /// If \p LookThroughSrc is true, consider the input value when computing the 247 /// mask. This may look through sign bit operations. 248 /// 249 /// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair 250 /// element will always be LHS. 251 /// 252 std::tuple<Value *, FPClassTest, FPClassTest> 253 fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, 254 Value *RHS, bool LookThroughSrc = true); 255 std::tuple<Value *, FPClassTest, FPClassTest> 256 fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, 257 FPClassTest RHS, bool LookThroughSrc = true); 258 std::tuple<Value *, FPClassTest, FPClassTest> 259 fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, 260 const APFloat &RHS, bool LookThroughSrc = true); 261 262 struct KnownFPClass { 263 /// Floating-point classes the value could be one of. 264 FPClassTest KnownFPClasses = fcAllFlags; 265 266 /// std::nullopt if the sign bit is unknown, true if the sign bit is 267 /// definitely set or false if the sign bit is definitely unset. 268 std::optional<bool> SignBit; 269 270 bool operator==(KnownFPClass Other) const { 271 return KnownFPClasses == Other.KnownFPClasses && SignBit == Other.SignBit; 272 } 273 274 /// Return true if it's known this can never be one of the mask entries. 275 bool isKnownNever(FPClassTest Mask) const { 276 return (KnownFPClasses & Mask) == fcNone; 277 } 278 279 bool isKnownAlways(FPClassTest Mask) const { return isKnownNever(~Mask); } 280 281 bool isUnknown() const { 282 return KnownFPClasses == fcAllFlags && !SignBit; 283 } 284 285 /// Return true if it's known this can never be a nan. 286 bool isKnownNeverNaN() const { 287 return isKnownNever(fcNan); 288 } 289 290 /// Return true if it's known this must always be a nan. 291 bool isKnownAlwaysNaN() const { return isKnownAlways(fcNan); } 292 293 /// Return true if it's known this can never be an infinity. 294 bool isKnownNeverInfinity() const { 295 return isKnownNever(fcInf); 296 } 297 298 /// Return true if it's known this can never be +infinity. 299 bool isKnownNeverPosInfinity() const { 300 return isKnownNever(fcPosInf); 301 } 302 303 /// Return true if it's known this can never be -infinity. 304 bool isKnownNeverNegInfinity() const { 305 return isKnownNever(fcNegInf); 306 } 307 308 /// Return true if it's known this can never be a subnormal 309 bool isKnownNeverSubnormal() const { 310 return isKnownNever(fcSubnormal); 311 } 312 313 /// Return true if it's known this can never be a positive subnormal 314 bool isKnownNeverPosSubnormal() const { 315 return isKnownNever(fcPosSubnormal); 316 } 317 318 /// Return true if it's known this can never be a negative subnormal 319 bool isKnownNeverNegSubnormal() const { 320 return isKnownNever(fcNegSubnormal); 321 } 322 323 /// Return true if it's known this can never be a zero. This means a literal 324 /// [+-]0, and does not include denormal inputs implicitly treated as [+-]0. 325 bool isKnownNeverZero() const { 326 return isKnownNever(fcZero); 327 } 328 329 /// Return true if it's known this can never be a literal positive zero. 330 bool isKnownNeverPosZero() const { 331 return isKnownNever(fcPosZero); 332 } 333 334 /// Return true if it's known this can never be a negative zero. This means a 335 /// literal -0 and does not include denormal inputs implicitly treated as -0. 336 bool isKnownNeverNegZero() const { 337 return isKnownNever(fcNegZero); 338 } 339 340 /// Return true if it's know this can never be interpreted as a zero. This 341 /// extends isKnownNeverZero to cover the case where the assumed 342 /// floating-point mode for the function interprets denormals as zero. 343 bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const; 344 345 /// Return true if it's know this can never be interpreted as a negative zero. 346 bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const; 347 348 /// Return true if it's know this can never be interpreted as a positive zero. 349 bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const; 350 351 static constexpr FPClassTest OrderedLessThanZeroMask = 352 fcNegSubnormal | fcNegNormal | fcNegInf; 353 static constexpr FPClassTest OrderedGreaterThanZeroMask = 354 fcPosSubnormal | fcPosNormal | fcPosInf; 355 356 /// Return true if we can prove that the analyzed floating-point value is 357 /// either NaN or never less than -0.0. 358 /// 359 /// NaN --> true 360 /// +0 --> true 361 /// -0 --> true 362 /// x > +0 --> true 363 /// x < -0 --> false 364 bool cannotBeOrderedLessThanZero() const { 365 return isKnownNever(OrderedLessThanZeroMask); 366 } 367 368 /// Return true if we can prove that the analyzed floating-point value is 369 /// either NaN or never greater than -0.0. 370 /// NaN --> true 371 /// +0 --> true 372 /// -0 --> true 373 /// x > +0 --> false 374 /// x < -0 --> true 375 bool cannotBeOrderedGreaterThanZero() const { 376 return isKnownNever(OrderedGreaterThanZeroMask); 377 } 378 379 KnownFPClass &operator|=(const KnownFPClass &RHS) { 380 KnownFPClasses = KnownFPClasses | RHS.KnownFPClasses; 381 382 if (SignBit != RHS.SignBit) 383 SignBit = std::nullopt; 384 return *this; 385 } 386 387 void knownNot(FPClassTest RuleOut) { 388 KnownFPClasses = KnownFPClasses & ~RuleOut; 389 if (isKnownNever(fcNan) && !SignBit) { 390 if (isKnownNever(fcNegative)) 391 SignBit = false; 392 else if (isKnownNever(fcPositive)) 393 SignBit = true; 394 } 395 } 396 397 void fneg() { 398 KnownFPClasses = llvm::fneg(KnownFPClasses); 399 if (SignBit) 400 SignBit = !*SignBit; 401 } 402 403 void fabs() { 404 if (KnownFPClasses & fcNegZero) 405 KnownFPClasses |= fcPosZero; 406 407 if (KnownFPClasses & fcNegInf) 408 KnownFPClasses |= fcPosInf; 409 410 if (KnownFPClasses & fcNegSubnormal) 411 KnownFPClasses |= fcPosSubnormal; 412 413 if (KnownFPClasses & fcNegNormal) 414 KnownFPClasses |= fcPosNormal; 415 416 signBitMustBeZero(); 417 } 418 419 /// Return true if the sign bit must be 0, ignoring the sign of nans. 420 bool signBitIsZeroOrNaN() const { 421 return isKnownNever(fcNegative); 422 } 423 424 /// Assume the sign bit is zero. 425 void signBitMustBeZero() { 426 KnownFPClasses &= (fcPositive | fcNan); 427 SignBit = false; 428 } 429 430 /// Assume the sign bit is one. 431 void signBitMustBeOne() { 432 KnownFPClasses &= (fcNegative | fcNan); 433 SignBit = true; 434 } 435 436 void copysign(const KnownFPClass &Sign) { 437 // Don't know anything about the sign of the source. Expand the possible set 438 // to its opposite sign pair. 439 if (KnownFPClasses & fcZero) 440 KnownFPClasses |= fcZero; 441 if (KnownFPClasses & fcSubnormal) 442 KnownFPClasses |= fcSubnormal; 443 if (KnownFPClasses & fcNormal) 444 KnownFPClasses |= fcNormal; 445 if (KnownFPClasses & fcInf) 446 KnownFPClasses |= fcInf; 447 448 // Sign bit is exactly preserved even for nans. 449 SignBit = Sign.SignBit; 450 451 // Clear sign bits based on the input sign mask. 452 if (Sign.isKnownNever(fcPositive | fcNan) || (SignBit && *SignBit)) 453 KnownFPClasses &= (fcNegative | fcNan); 454 if (Sign.isKnownNever(fcNegative | fcNan) || (SignBit && !*SignBit)) 455 KnownFPClasses &= (fcPositive | fcNan); 456 } 457 458 // Propagate knowledge that a non-NaN source implies the result can also not 459 // be a NaN. For unconstrained operations, signaling nans are not guaranteed 460 // to be quieted but cannot be introduced. 461 void propagateNaN(const KnownFPClass &Src, bool PreserveSign = false) { 462 if (Src.isKnownNever(fcNan)) { 463 knownNot(fcNan); 464 if (PreserveSign) 465 SignBit = Src.SignBit; 466 } else if (Src.isKnownNever(fcSNan)) 467 knownNot(fcSNan); 468 } 469 470 /// Propagate knowledge from a source value that could be a denormal or 471 /// zero. We have to be conservative since output flushing is not guaranteed, 472 /// so known-never-zero may not hold. 473 /// 474 /// This assumes a copy-like operation and will replace any currently known 475 /// information. 476 void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty); 477 478 /// Report known classes if \p Src is evaluated through a potentially 479 /// canonicalizing operation. We can assume signaling nans will not be 480 /// introduced, but cannot assume a denormal will be flushed under FTZ/DAZ. 481 /// 482 /// This assumes a copy-like operation and will replace any currently known 483 /// information. 484 void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, 485 Type *Ty); 486 487 void resetAll() { *this = KnownFPClass(); } 488 }; 489 490 inline KnownFPClass operator|(KnownFPClass LHS, const KnownFPClass &RHS) { 491 LHS |= RHS; 492 return LHS; 493 } 494 495 inline KnownFPClass operator|(const KnownFPClass &LHS, KnownFPClass &&RHS) { 496 RHS |= LHS; 497 return std::move(RHS); 498 } 499 500 /// Determine which floating-point classes are valid for \p V, and return them 501 /// in KnownFPClass bit sets. 502 /// 503 /// This function is defined on values with floating-point type, values vectors 504 /// of floating-point type, and arrays of floating-point type. 505 506 /// \p InterestedClasses is a compile time optimization hint for which floating 507 /// point classes should be queried. Queries not specified in \p 508 /// InterestedClasses should be reliable if they are determined during the 509 /// query. 510 KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, 511 FPClassTest InterestedClasses, unsigned Depth, 512 const SimplifyQuery &SQ); 513 514 KnownFPClass computeKnownFPClass(const Value *V, FPClassTest InterestedClasses, 515 unsigned Depth, const SimplifyQuery &SQ); 516 517 inline KnownFPClass computeKnownFPClass( 518 const Value *V, const DataLayout &DL, 519 FPClassTest InterestedClasses = fcAllFlags, unsigned Depth = 0, 520 const TargetLibraryInfo *TLI = nullptr, AssumptionCache *AC = nullptr, 521 const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr, 522 bool UseInstrInfo = true) { 523 return computeKnownFPClass( 524 V, InterestedClasses, Depth, 525 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo)); 526 } 527 528 /// Wrapper to account for known fast math flags at the use instruction. 529 inline KnownFPClass 530 computeKnownFPClass(const Value *V, const APInt &DemandedElts, 531 FastMathFlags FMF, FPClassTest InterestedClasses, 532 unsigned Depth, const SimplifyQuery &SQ) { 533 if (FMF.noNaNs()) 534 InterestedClasses &= ~fcNan; 535 if (FMF.noInfs()) 536 InterestedClasses &= ~fcInf; 537 538 KnownFPClass Result = 539 computeKnownFPClass(V, DemandedElts, InterestedClasses, Depth, SQ); 540 541 if (FMF.noNaNs()) 542 Result.KnownFPClasses &= ~fcNan; 543 if (FMF.noInfs()) 544 Result.KnownFPClasses &= ~fcInf; 545 return Result; 546 } 547 548 inline KnownFPClass computeKnownFPClass(const Value *V, FastMathFlags FMF, 549 FPClassTest InterestedClasses, 550 unsigned Depth, 551 const SimplifyQuery &SQ) { 552 auto *FVTy = dyn_cast<FixedVectorType>(V->getType()); 553 APInt DemandedElts = 554 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1); 555 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, Depth, 556 SQ); 557 } 558 559 /// Return true if we can prove that the specified FP value is never equal to 560 /// -0.0. Users should use caution when considering PreserveSign 561 /// denormal-fp-math. 562 inline bool cannotBeNegativeZero(const Value *V, unsigned Depth, 563 const SimplifyQuery &SQ) { 564 KnownFPClass Known = computeKnownFPClass(V, fcNegZero, Depth, SQ); 565 return Known.isKnownNeverNegZero(); 566 } 567 568 /// Return true if we can prove that the specified FP value is either NaN or 569 /// never less than -0.0. 570 /// 571 /// NaN --> true 572 /// +0 --> true 573 /// -0 --> true 574 /// x > +0 --> true 575 /// x < -0 --> false 576 inline bool cannotBeOrderedLessThanZero(const Value *V, unsigned Depth, 577 const SimplifyQuery &SQ) { 578 KnownFPClass Known = 579 computeKnownFPClass(V, KnownFPClass::OrderedLessThanZeroMask, Depth, SQ); 580 return Known.cannotBeOrderedLessThanZero(); 581 } 582 583 /// Return true if the floating-point scalar value is not an infinity or if 584 /// the floating-point vector value has no infinities. Return false if a value 585 /// could ever be infinity. 586 inline bool isKnownNeverInfinity(const Value *V, unsigned Depth, 587 const SimplifyQuery &SQ) { 588 KnownFPClass Known = computeKnownFPClass(V, fcInf, Depth, SQ); 589 return Known.isKnownNeverInfinity(); 590 } 591 592 /// Return true if the floating-point value can never contain a NaN or infinity. 593 inline bool isKnownNeverInfOrNaN(const Value *V, unsigned Depth, 594 const SimplifyQuery &SQ) { 595 KnownFPClass Known = computeKnownFPClass(V, fcInf | fcNan, Depth, SQ); 596 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity(); 597 } 598 599 /// Return true if the floating-point scalar value is not a NaN or if the 600 /// floating-point vector value has no NaN elements. Return false if a value 601 /// could ever be NaN. 602 inline bool isKnownNeverNaN(const Value *V, unsigned Depth, 603 const SimplifyQuery &SQ) { 604 KnownFPClass Known = computeKnownFPClass(V, fcNan, Depth, SQ); 605 return Known.isKnownNeverNaN(); 606 } 607 608 /// Return false if we can prove that the specified FP value's sign bit is 0. 609 /// Return true if we can prove that the specified FP value's sign bit is 1. 610 /// Otherwise return std::nullopt. 611 inline std::optional<bool> computeKnownFPSignBit(const Value *V, unsigned Depth, 612 const SimplifyQuery &SQ) { 613 KnownFPClass Known = computeKnownFPClass(V, fcAllFlags, Depth, SQ); 614 return Known.SignBit; 615 } 616 617 /// If the specified value can be set by repeating the same byte in memory, 618 /// return the i8 value that it is represented with. This is true for all i8 619 /// values obviously, but is also true for i32 0, i32 -1, i16 0xF0F0, double 620 /// 0.0 etc. If the value can't be handled with a repeated byte store (e.g. 621 /// i16 0x1234), return null. If the value is entirely undef and padding, 622 /// return undef. 623 Value *isBytewiseValue(Value *V, const DataLayout &DL); 624 625 /// Given an aggregate and an sequence of indices, see if the scalar value 626 /// indexed is already around as a register, for example if it were inserted 627 /// directly into the aggregate. 628 /// 629 /// If InsertBefore is not empty, this function will duplicate (modified) 630 /// insertvalues when a part of a nested struct is extracted. 631 Value *FindInsertedValue( 632 Value *V, ArrayRef<unsigned> idx_range, 633 std::optional<BasicBlock::iterator> InsertBefore = std::nullopt); 634 635 /// Analyze the specified pointer to see if it can be expressed as a base 636 /// pointer plus a constant offset. Return the base and offset to the caller. 637 /// 638 /// This is a wrapper around Value::stripAndAccumulateConstantOffsets that 639 /// creates and later unpacks the required APInt. 640 inline Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, 641 const DataLayout &DL, 642 bool AllowNonInbounds = true) { 643 APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); 644 Value *Base = 645 Ptr->stripAndAccumulateConstantOffsets(DL, OffsetAPInt, AllowNonInbounds); 646 647 Offset = OffsetAPInt.getSExtValue(); 648 return Base; 649 } 650 inline const Value * 651 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset, 652 const DataLayout &DL, 653 bool AllowNonInbounds = true) { 654 return GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset, DL, 655 AllowNonInbounds); 656 } 657 658 /// Returns true if the GEP is based on a pointer to a string (array of 659 // \p CharSize integers) and is indexing into this string. 660 bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize = 8); 661 662 /// Represents offset+length into a ConstantDataArray. 663 struct ConstantDataArraySlice { 664 /// ConstantDataArray pointer. nullptr indicates a zeroinitializer (a valid 665 /// initializer, it just doesn't fit the ConstantDataArray interface). 666 const ConstantDataArray *Array; 667 668 /// Slice starts at this Offset. 669 uint64_t Offset; 670 671 /// Length of the slice. 672 uint64_t Length; 673 674 /// Moves the Offset and adjusts Length accordingly. 675 void move(uint64_t Delta) { 676 assert(Delta < Length); 677 Offset += Delta; 678 Length -= Delta; 679 } 680 681 /// Convenience accessor for elements in the slice. 682 uint64_t operator[](unsigned I) const { 683 return Array == nullptr ? 0 : Array->getElementAsInteger(I + Offset); 684 } 685 }; 686 687 /// Returns true if the value \p V is a pointer into a ConstantDataArray. 688 /// If successful \p Slice will point to a ConstantDataArray info object 689 /// with an appropriate offset. 690 bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, 691 unsigned ElementSize, uint64_t Offset = 0); 692 693 /// This function computes the length of a null-terminated C string pointed to 694 /// by V. If successful, it returns true and returns the string in Str. If 695 /// unsuccessful, it returns false. This does not include the trailing null 696 /// character by default. If TrimAtNul is set to false, then this returns any 697 /// trailing null characters as well as any other characters that come after 698 /// it. 699 bool getConstantStringInfo(const Value *V, StringRef &Str, 700 bool TrimAtNul = true); 701 702 /// If we can compute the length of the string pointed to by the specified 703 /// pointer, return 'len+1'. If we can't, return 0. 704 uint64_t GetStringLength(const Value *V, unsigned CharSize = 8); 705 706 /// This function returns call pointer argument that is considered the same by 707 /// aliasing rules. You CAN'T use it to replace one value with another. If 708 /// \p MustPreserveNullness is true, the call must preserve the nullness of 709 /// the pointer. 710 const Value *getArgumentAliasingToReturnedPointer(const CallBase *Call, 711 bool MustPreserveNullness); 712 inline Value *getArgumentAliasingToReturnedPointer(CallBase *Call, 713 bool MustPreserveNullness) { 714 return const_cast<Value *>(getArgumentAliasingToReturnedPointer( 715 const_cast<const CallBase *>(Call), MustPreserveNullness)); 716 } 717 718 /// {launder,strip}.invariant.group returns pointer that aliases its argument, 719 /// and it only captures pointer by returning it. 720 /// These intrinsics are not marked as nocapture, because returning is 721 /// considered as capture. The arguments are not marked as returned neither, 722 /// because it would make it useless. If \p MustPreserveNullness is true, 723 /// the intrinsic must preserve the nullness of the pointer. 724 bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( 725 const CallBase *Call, bool MustPreserveNullness); 726 727 /// This method strips off any GEP address adjustments, pointer casts 728 /// or `llvm.threadlocal.address` from the specified value \p V, returning the 729 /// original object being addressed. Note that the returned value has pointer 730 /// type if the specified value does. If the \p MaxLookup value is non-zero, it 731 /// limits the number of instructions to be stripped off. 732 const Value *getUnderlyingObject(const Value *V, unsigned MaxLookup = 6); 733 inline Value *getUnderlyingObject(Value *V, unsigned MaxLookup = 6) { 734 // Force const to avoid infinite recursion. 735 const Value *VConst = V; 736 return const_cast<Value *>(getUnderlyingObject(VConst, MaxLookup)); 737 } 738 739 /// Like getUnderlyingObject(), but will try harder to find a single underlying 740 /// object. In particular, this function also looks through selects and phis. 741 const Value *getUnderlyingObjectAggressive(const Value *V); 742 743 /// This method is similar to getUnderlyingObject except that it can 744 /// look through phi and select instructions and return multiple objects. 745 /// 746 /// If LoopInfo is passed, loop phis are further analyzed. If a pointer 747 /// accesses different objects in each iteration, we don't look through the 748 /// phi node. E.g. consider this loop nest: 749 /// 750 /// int **A; 751 /// for (i) 752 /// for (j) { 753 /// A[i][j] = A[i-1][j] * B[j] 754 /// } 755 /// 756 /// This is transformed by Load-PRE to stash away A[i] for the next iteration 757 /// of the outer loop: 758 /// 759 /// Curr = A[0]; // Prev_0 760 /// for (i: 1..N) { 761 /// Prev = Curr; // Prev = PHI (Prev_0, Curr) 762 /// Curr = A[i]; 763 /// for (j: 0..N) { 764 /// Curr[j] = Prev[j] * B[j] 765 /// } 766 /// } 767 /// 768 /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects 769 /// should not assume that Curr and Prev share the same underlying object thus 770 /// it shouldn't look through the phi above. 771 void getUnderlyingObjects(const Value *V, 772 SmallVectorImpl<const Value *> &Objects, 773 LoopInfo *LI = nullptr, unsigned MaxLookup = 6); 774 775 /// This is a wrapper around getUnderlyingObjects and adds support for basic 776 /// ptrtoint+arithmetic+inttoptr sequences. 777 bool getUnderlyingObjectsForCodeGen(const Value *V, 778 SmallVectorImpl<Value *> &Objects); 779 780 /// Returns unique alloca where the value comes from, or nullptr. 781 /// If OffsetZero is true check that V points to the begining of the alloca. 782 AllocaInst *findAllocaForValue(Value *V, bool OffsetZero = false); 783 inline const AllocaInst *findAllocaForValue(const Value *V, 784 bool OffsetZero = false) { 785 return findAllocaForValue(const_cast<Value *>(V), OffsetZero); 786 } 787 788 /// Return true if the only users of this pointer are lifetime markers. 789 bool onlyUsedByLifetimeMarkers(const Value *V); 790 791 /// Return true if the only users of this pointer are lifetime markers or 792 /// droppable instructions. 793 bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V); 794 795 /// Return true if speculation of the given load must be suppressed to avoid 796 /// ordering or interfering with an active sanitizer. If not suppressed, 797 /// dereferenceability and alignment must be proven separately. Note: This 798 /// is only needed for raw reasoning; if you use the interface below 799 /// (isSafeToSpeculativelyExecute), this is handled internally. 800 bool mustSuppressSpeculation(const LoadInst &LI); 801 802 /// Return true if the instruction does not have any effects besides 803 /// calculating the result and does not have undefined behavior. 804 /// 805 /// This method never returns true for an instruction that returns true for 806 /// mayHaveSideEffects; however, this method also does some other checks in 807 /// addition. It checks for undefined behavior, like dividing by zero or 808 /// loading from an invalid pointer (but not for undefined results, like a 809 /// shift with a shift amount larger than the width of the result). It checks 810 /// for malloc and alloca because speculatively executing them might cause a 811 /// memory leak. It also returns false for instructions related to control 812 /// flow, specifically terminators and PHI nodes. 813 /// 814 /// If the CtxI is specified this method performs context-sensitive analysis 815 /// and returns true if it is safe to execute the instruction immediately 816 /// before the CtxI. 817 /// 818 /// If the CtxI is NOT specified this method only looks at the instruction 819 /// itself and its operands, so if this method returns true, it is safe to 820 /// move the instruction as long as the correct dominance relationships for 821 /// the operands and users hold. 822 /// 823 /// This method can return true for instructions that read memory; 824 /// for such instructions, moving them may change the resulting value. 825 bool isSafeToSpeculativelyExecute(const Instruction *I, 826 const Instruction *CtxI = nullptr, 827 AssumptionCache *AC = nullptr, 828 const DominatorTree *DT = nullptr, 829 const TargetLibraryInfo *TLI = nullptr, 830 bool UseVariableInfo = true); 831 832 inline bool isSafeToSpeculativelyExecute(const Instruction *I, 833 BasicBlock::iterator CtxI, 834 AssumptionCache *AC = nullptr, 835 const DominatorTree *DT = nullptr, 836 const TargetLibraryInfo *TLI = nullptr, 837 bool UseVariableInfo = true) { 838 // Take an iterator, and unwrap it into an Instruction *. 839 return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo); 840 } 841 842 /// Don't use information from its non-constant operands. This helper is used 843 /// when its operands are going to be replaced. 844 inline bool 845 isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) { 846 return isSafeToSpeculativelyExecute(I, nullptr, nullptr, nullptr, nullptr, 847 /*UseVariableInfo=*/false); 848 } 849 850 /// This returns the same result as isSafeToSpeculativelyExecute if Opcode is 851 /// the actual opcode of Inst. If the provided and actual opcode differ, the 852 /// function (virtually) overrides the opcode of Inst with the provided 853 /// Opcode. There are come constraints in this case: 854 /// * If Opcode has a fixed number of operands (eg, as binary operators do), 855 /// then Inst has to have at least as many leading operands. The function 856 /// will ignore all trailing operands beyond that number. 857 /// * If Opcode allows for an arbitrary number of operands (eg, as CallInsts 858 /// do), then all operands are considered. 859 /// * The virtual instruction has to satisfy all typing rules of the provided 860 /// Opcode. 861 /// * This function is pessimistic in the following sense: If one actually 862 /// materialized the virtual instruction, then isSafeToSpeculativelyExecute 863 /// may say that the materialized instruction is speculatable whereas this 864 /// function may have said that the instruction wouldn't be speculatable. 865 /// This behavior is a shortcoming in the current implementation and not 866 /// intentional. 867 bool isSafeToSpeculativelyExecuteWithOpcode( 868 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI = nullptr, 869 AssumptionCache *AC = nullptr, const DominatorTree *DT = nullptr, 870 const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true); 871 872 /// Returns true if the result or effects of the given instructions \p I 873 /// depend values not reachable through the def use graph. 874 /// * Memory dependence arises for example if the instruction reads from 875 /// memory or may produce effects or undefined behaviour. Memory dependent 876 /// instructions generally cannot be reorderd with respect to other memory 877 /// dependent instructions. 878 /// * Control dependence arises for example if the instruction may fault 879 /// if lifted above a throwing call or infinite loop. 880 bool mayHaveNonDefUseDependency(const Instruction &I); 881 882 /// Return true if it is an intrinsic that cannot be speculated but also 883 /// cannot trap. 884 bool isAssumeLikeIntrinsic(const Instruction *I); 885 886 /// Return true if it is valid to use the assumptions provided by an 887 /// assume intrinsic, I, at the point in the control-flow identified by the 888 /// context instruction, CxtI. By default, ephemeral values of the assumption 889 /// are treated as an invalid context, to prevent the assumption from being used 890 /// to optimize away its argument. If the caller can ensure that this won't 891 /// happen, it can call with AllowEphemerals set to true to get more valid 892 /// assumptions. 893 bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, 894 const DominatorTree *DT = nullptr, 895 bool AllowEphemerals = false); 896 897 enum class OverflowResult { 898 /// Always overflows in the direction of signed/unsigned min value. 899 AlwaysOverflowsLow, 900 /// Always overflows in the direction of signed/unsigned max value. 901 AlwaysOverflowsHigh, 902 /// May or may not overflow. 903 MayOverflow, 904 /// Never overflows. 905 NeverOverflows, 906 }; 907 908 OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, 909 const SimplifyQuery &SQ, 910 bool IsNSW = false); 911 OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, 912 const SimplifyQuery &SQ); 913 OverflowResult 914 computeOverflowForUnsignedAdd(const WithCache<const Value *> &LHS, 915 const WithCache<const Value *> &RHS, 916 const SimplifyQuery &SQ); 917 OverflowResult computeOverflowForSignedAdd(const WithCache<const Value *> &LHS, 918 const WithCache<const Value *> &RHS, 919 const SimplifyQuery &SQ); 920 /// This version also leverages the sign bit of Add if known. 921 OverflowResult computeOverflowForSignedAdd(const AddOperator *Add, 922 const SimplifyQuery &SQ); 923 OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, 924 const SimplifyQuery &SQ); 925 OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, 926 const SimplifyQuery &SQ); 927 928 /// Returns true if the arithmetic part of the \p WO 's result is 929 /// used only along the paths control dependent on the computation 930 /// not overflowing, \p WO being an <op>.with.overflow intrinsic. 931 bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, 932 const DominatorTree &DT); 933 934 /// Determine the possible constant range of vscale with the given bit width, 935 /// based on the vscale_range function attribute. 936 ConstantRange getVScaleRange(const Function *F, unsigned BitWidth); 937 938 /// Determine the possible constant range of an integer or vector of integer 939 /// value. This is intended as a cheap, non-recursive check. 940 ConstantRange computeConstantRange(const Value *V, bool ForSigned, 941 bool UseInstrInfo = true, 942 AssumptionCache *AC = nullptr, 943 const Instruction *CtxI = nullptr, 944 const DominatorTree *DT = nullptr, 945 unsigned Depth = 0); 946 947 /// Combine constant ranges from computeConstantRange() and computeKnownBits(). 948 ConstantRange 949 computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V, 950 bool ForSigned, const SimplifyQuery &SQ); 951 952 /// Return true if this function can prove that the instruction I will 953 /// always transfer execution to one of its successors (including the next 954 /// instruction that follows within a basic block). E.g. this is not 955 /// guaranteed for function calls that could loop infinitely. 956 /// 957 /// In other words, this function returns false for instructions that may 958 /// transfer execution or fail to transfer execution in a way that is not 959 /// captured in the CFG nor in the sequence of instructions within a basic 960 /// block. 961 /// 962 /// Undefined behavior is assumed not to happen, so e.g. division is 963 /// guaranteed to transfer execution to the following instruction even 964 /// though division by zero might cause undefined behavior. 965 bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I); 966 967 /// Returns true if this block does not contain a potential implicit exit. 968 /// This is equivelent to saying that all instructions within the basic block 969 /// are guaranteed to transfer execution to their successor within the basic 970 /// block. This has the same assumptions w.r.t. undefined behavior as the 971 /// instruction variant of this function. 972 bool isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB); 973 974 /// Return true if every instruction in the range (Begin, End) is 975 /// guaranteed to transfer execution to its static successor. \p ScanLimit 976 /// bounds the search to avoid scanning huge blocks. 977 bool isGuaranteedToTransferExecutionToSuccessor( 978 BasicBlock::const_iterator Begin, BasicBlock::const_iterator End, 979 unsigned ScanLimit = 32); 980 981 /// Same as previous, but with range expressed via iterator_range. 982 bool isGuaranteedToTransferExecutionToSuccessor( 983 iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit = 32); 984 985 /// Return true if this function can prove that the instruction I 986 /// is executed for every iteration of the loop L. 987 /// 988 /// Note that this currently only considers the loop header. 989 bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, 990 const Loop *L); 991 992 /// Return true if \p PoisonOp's user yields poison or raises UB if its 993 /// operand \p PoisonOp is poison. 994 /// 995 /// If \p PoisonOp is a vector or an aggregate and the operation's result is a 996 /// single value, any poison element in /p PoisonOp should make the result 997 /// poison or raise UB. 998 /// 999 /// To filter out operands that raise UB on poison, you can use 1000 /// getGuaranteedNonPoisonOp. 1001 bool propagatesPoison(const Use &PoisonOp); 1002 1003 /// Insert operands of I into Ops such that I will trigger undefined behavior 1004 /// if I is executed and that operand has a poison value. 1005 void getGuaranteedNonPoisonOps(const Instruction *I, 1006 SmallVectorImpl<const Value *> &Ops); 1007 1008 /// Insert operands of I into Ops such that I will trigger undefined behavior 1009 /// if I is executed and that operand is not a well-defined value 1010 /// (i.e. has undef bits or poison). 1011 void getGuaranteedWellDefinedOps(const Instruction *I, 1012 SmallVectorImpl<const Value *> &Ops); 1013 1014 /// Return true if the given instruction must trigger undefined behavior 1015 /// when I is executed with any operands which appear in KnownPoison holding 1016 /// a poison value at the point of execution. 1017 bool mustTriggerUB(const Instruction *I, 1018 const SmallPtrSetImpl<const Value *> &KnownPoison); 1019 1020 /// Return true if this function can prove that if Inst is executed 1021 /// and yields a poison value or undef bits, then that will trigger 1022 /// undefined behavior. 1023 /// 1024 /// Note that this currently only considers the basic block that is 1025 /// the parent of Inst. 1026 bool programUndefinedIfUndefOrPoison(const Instruction *Inst); 1027 bool programUndefinedIfPoison(const Instruction *Inst); 1028 1029 /// canCreateUndefOrPoison returns true if Op can create undef or poison from 1030 /// non-undef & non-poison operands. 1031 /// For vectors, canCreateUndefOrPoison returns true if there is potential 1032 /// poison or undef in any element of the result when vectors without 1033 /// undef/poison poison are given as operands. 1034 /// For example, given `Op = shl <2 x i32> %x, <0, 32>`, this function returns 1035 /// true. If Op raises immediate UB but never creates poison or undef 1036 /// (e.g. sdiv I, 0), canCreatePoison returns false. 1037 /// 1038 /// \p ConsiderFlagsAndMetadata controls whether poison producing flags and 1039 /// metadata on the instruction are considered. This can be used to see if the 1040 /// instruction could still introduce undef or poison even without poison 1041 /// generating flags and metadata which might be on the instruction. 1042 /// (i.e. could the result of Op->dropPoisonGeneratingFlags() still create 1043 /// poison or undef) 1044 /// 1045 /// canCreatePoison returns true if Op can create poison from non-poison 1046 /// operands. 1047 bool canCreateUndefOrPoison(const Operator *Op, 1048 bool ConsiderFlagsAndMetadata = true); 1049 bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata = true); 1050 1051 /// Return true if V is poison given that ValAssumedPoison is already poison. 1052 /// For example, if ValAssumedPoison is `icmp X, 10` and V is `icmp X, 5`, 1053 /// impliesPoison returns true. 1054 bool impliesPoison(const Value *ValAssumedPoison, const Value *V); 1055 1056 /// Return true if this function can prove that V does not have undef bits 1057 /// and is never poison. If V is an aggregate value or vector, check whether 1058 /// all elements (except padding) are not undef or poison. 1059 /// Note that this is different from canCreateUndefOrPoison because the 1060 /// function assumes Op's operands are not poison/undef. 1061 /// 1062 /// If CtxI and DT are specified this method performs flow-sensitive analysis 1063 /// and returns true if it is guaranteed to be never undef or poison 1064 /// immediately before the CtxI. 1065 bool isGuaranteedNotToBeUndefOrPoison(const Value *V, 1066 AssumptionCache *AC = nullptr, 1067 const Instruction *CtxI = nullptr, 1068 const DominatorTree *DT = nullptr, 1069 unsigned Depth = 0); 1070 1071 /// Returns true if V cannot be poison, but may be undef. 1072 bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC = nullptr, 1073 const Instruction *CtxI = nullptr, 1074 const DominatorTree *DT = nullptr, 1075 unsigned Depth = 0); 1076 1077 inline bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC, 1078 BasicBlock::iterator CtxI, 1079 const DominatorTree *DT = nullptr, 1080 unsigned Depth = 0) { 1081 // Takes an iterator as a position, passes down to Instruction * 1082 // implementation. 1083 return isGuaranteedNotToBePoison(V, AC, &*CtxI, DT, Depth); 1084 } 1085 1086 /// Returns true if V cannot be undef, but may be poison. 1087 bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC = nullptr, 1088 const Instruction *CtxI = nullptr, 1089 const DominatorTree *DT = nullptr, 1090 unsigned Depth = 0); 1091 1092 /// Return true if undefined behavior would provable be executed on the path to 1093 /// OnPathTo if Root produced a posion result. Note that this doesn't say 1094 /// anything about whether OnPathTo is actually executed or whether Root is 1095 /// actually poison. This can be used to assess whether a new use of Root can 1096 /// be added at a location which is control equivalent with OnPathTo (such as 1097 /// immediately before it) without introducing UB which didn't previously 1098 /// exist. Note that a false result conveys no information. 1099 bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, 1100 Instruction *OnPathTo, 1101 DominatorTree *DT); 1102 1103 /// Specific patterns of select instructions we can match. 1104 enum SelectPatternFlavor { 1105 SPF_UNKNOWN = 0, 1106 SPF_SMIN, /// Signed minimum 1107 SPF_UMIN, /// Unsigned minimum 1108 SPF_SMAX, /// Signed maximum 1109 SPF_UMAX, /// Unsigned maximum 1110 SPF_FMINNUM, /// Floating point minnum 1111 SPF_FMAXNUM, /// Floating point maxnum 1112 SPF_ABS, /// Absolute value 1113 SPF_NABS /// Negated absolute value 1114 }; 1115 1116 /// Behavior when a floating point min/max is given one NaN and one 1117 /// non-NaN as input. 1118 enum SelectPatternNaNBehavior { 1119 SPNB_NA = 0, /// NaN behavior not applicable. 1120 SPNB_RETURNS_NAN, /// Given one NaN input, returns the NaN. 1121 SPNB_RETURNS_OTHER, /// Given one NaN input, returns the non-NaN. 1122 SPNB_RETURNS_ANY /// Given one NaN input, can return either (or 1123 /// it has been determined that no operands can 1124 /// be NaN). 1125 }; 1126 1127 struct SelectPatternResult { 1128 SelectPatternFlavor Flavor; 1129 SelectPatternNaNBehavior NaNBehavior; /// Only applicable if Flavor is 1130 /// SPF_FMINNUM or SPF_FMAXNUM. 1131 bool Ordered; /// When implementing this min/max pattern as 1132 /// fcmp; select, does the fcmp have to be 1133 /// ordered? 1134 1135 /// Return true if \p SPF is a min or a max pattern. 1136 static bool isMinOrMax(SelectPatternFlavor SPF) { 1137 return SPF != SPF_UNKNOWN && SPF != SPF_ABS && SPF != SPF_NABS; 1138 } 1139 }; 1140 1141 /// Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind 1142 /// and providing the out parameter results if we successfully match. 1143 /// 1144 /// For ABS/NABS, LHS will be set to the input to the abs idiom. RHS will be 1145 /// the negation instruction from the idiom. 1146 /// 1147 /// If CastOp is not nullptr, also match MIN/MAX idioms where the type does 1148 /// not match that of the original select. If this is the case, the cast 1149 /// operation (one of Trunc,SExt,Zext) that must be done to transform the 1150 /// type of LHS and RHS into the type of V is returned in CastOp. 1151 /// 1152 /// For example: 1153 /// %1 = icmp slt i32 %a, i32 4 1154 /// %2 = sext i32 %a to i64 1155 /// %3 = select i1 %1, i64 %2, i64 4 1156 /// 1157 /// -> LHS = %a, RHS = i32 4, *CastOp = Instruction::SExt 1158 /// 1159 SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, 1160 Instruction::CastOps *CastOp = nullptr, 1161 unsigned Depth = 0); 1162 1163 inline SelectPatternResult matchSelectPattern(const Value *V, const Value *&LHS, 1164 const Value *&RHS) { 1165 Value *L = const_cast<Value *>(LHS); 1166 Value *R = const_cast<Value *>(RHS); 1167 auto Result = matchSelectPattern(const_cast<Value *>(V), L, R); 1168 LHS = L; 1169 RHS = R; 1170 return Result; 1171 } 1172 1173 /// Determine the pattern that a select with the given compare as its 1174 /// predicate and given values as its true/false operands would match. 1175 SelectPatternResult matchDecomposedSelectPattern( 1176 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, 1177 Instruction::CastOps *CastOp = nullptr, unsigned Depth = 0); 1178 1179 /// Return the canonical comparison predicate for the specified 1180 /// minimum/maximum flavor. 1181 CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered = false); 1182 1183 /// Return the inverse minimum/maximum flavor of the specified flavor. 1184 /// For example, signed minimum is the inverse of signed maximum. 1185 SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF); 1186 1187 Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID); 1188 1189 /// Return the minimum or maximum constant value for the specified integer 1190 /// min/max flavor and type. 1191 APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth); 1192 1193 /// Check if the values in \p VL are select instructions that can be converted 1194 /// to a min or max (vector) intrinsic. Returns the intrinsic ID, if such a 1195 /// conversion is possible, together with a bool indicating whether all select 1196 /// conditions are only used by the selects. Otherwise return 1197 /// Intrinsic::not_intrinsic. 1198 std::pair<Intrinsic::ID, bool> 1199 canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL); 1200 1201 /// Attempt to match a simple first order recurrence cycle of the form: 1202 /// %iv = phi Ty [%Start, %Entry], [%Inc, %backedge] 1203 /// %inc = binop %iv, %step 1204 /// OR 1205 /// %iv = phi Ty [%Start, %Entry], [%Inc, %backedge] 1206 /// %inc = binop %step, %iv 1207 /// 1208 /// A first order recurrence is a formula with the form: X_n = f(X_(n-1)) 1209 /// 1210 /// A couple of notes on subtleties in that definition: 1211 /// * The Step does not have to be loop invariant. In math terms, it can 1212 /// be a free variable. We allow recurrences with both constant and 1213 /// variable coefficients. Callers may wish to filter cases where Step 1214 /// does not dominate P. 1215 /// * For non-commutative operators, we will match both forms. This 1216 /// results in some odd recurrence structures. Callers may wish to filter 1217 /// out recurrences where the phi is not the LHS of the returned operator. 1218 /// * Because of the structure matched, the caller can assume as a post 1219 /// condition of the match the presence of a Loop with P's parent as it's 1220 /// header *except* in unreachable code. (Dominance decays in unreachable 1221 /// code.) 1222 /// 1223 /// NOTE: This is intentional simple. If you want the ability to analyze 1224 /// non-trivial loop conditons, see ScalarEvolution instead. 1225 bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, 1226 Value *&Step); 1227 1228 /// Analogous to the above, but starting from the binary operator 1229 bool matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P, Value *&Start, 1230 Value *&Step); 1231 1232 /// Return true if RHS is known to be implied true by LHS. Return false if 1233 /// RHS is known to be implied false by LHS. Otherwise, return std::nullopt if 1234 /// no implication can be made. A & B must be i1 (boolean) values or a vector of 1235 /// such values. Note that the truth table for implication is the same as <=u on 1236 /// i1 values (but not 1237 /// <=s!). The truth table for both is: 1238 /// | T | F (B) 1239 /// T | T | F 1240 /// F | T | T 1241 /// (A) 1242 std::optional<bool> isImpliedCondition(const Value *LHS, const Value *RHS, 1243 const DataLayout &DL, 1244 bool LHSIsTrue = true, 1245 unsigned Depth = 0); 1246 std::optional<bool> isImpliedCondition(const Value *LHS, 1247 CmpInst::Predicate RHSPred, 1248 const Value *RHSOp0, const Value *RHSOp1, 1249 const DataLayout &DL, 1250 bool LHSIsTrue = true, 1251 unsigned Depth = 0); 1252 1253 /// Return the boolean condition value in the context of the given instruction 1254 /// if it is known based on dominating conditions. 1255 std::optional<bool> isImpliedByDomCondition(const Value *Cond, 1256 const Instruction *ContextI, 1257 const DataLayout &DL); 1258 std::optional<bool> isImpliedByDomCondition(CmpInst::Predicate Pred, 1259 const Value *LHS, const Value *RHS, 1260 const Instruction *ContextI, 1261 const DataLayout &DL); 1262 1263 /// Call \p InsertAffected on all Values whose known bits / value may be 1264 /// affected by the condition \p Cond. Used by AssumptionCache and 1265 /// DomConditionCache. 1266 void findValuesAffectedByCondition(Value *Cond, bool IsAssume, 1267 function_ref<void(Value *)> InsertAffected); 1268 1269 } // end namespace llvm 1270 1271 #endif // LLVM_ANALYSIS_VALUETRACKING_H 1272