10553 lines · cpp
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file contains routines that help analyze properties that chains of10// computations have.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/Analysis/ValueTracking.h"15#include "llvm/ADT/APFloat.h"16#include "llvm/ADT/APInt.h"17#include "llvm/ADT/ArrayRef.h"18#include "llvm/ADT/FloatingPointMode.h"19#include "llvm/ADT/STLExtras.h"20#include "llvm/ADT/ScopeExit.h"21#include "llvm/ADT/SmallPtrSet.h"22#include "llvm/ADT/SmallVector.h"23#include "llvm/ADT/StringRef.h"24#include "llvm/ADT/iterator_range.h"25#include "llvm/Analysis/AliasAnalysis.h"26#include "llvm/Analysis/AssumeBundleQueries.h"27#include "llvm/Analysis/AssumptionCache.h"28#include "llvm/Analysis/ConstantFolding.h"29#include "llvm/Analysis/DomConditionCache.h"30#include "llvm/Analysis/FloatingPointPredicateUtils.h"31#include "llvm/Analysis/GuardUtils.h"32#include "llvm/Analysis/InstructionSimplify.h"33#include "llvm/Analysis/Loads.h"34#include "llvm/Analysis/LoopInfo.h"35#include "llvm/Analysis/TargetLibraryInfo.h"36#include "llvm/Analysis/VectorUtils.h"37#include "llvm/Analysis/WithCache.h"38#include "llvm/IR/Argument.h"39#include "llvm/IR/Attributes.h"40#include "llvm/IR/BasicBlock.h"41#include "llvm/IR/Constant.h"42#include "llvm/IR/ConstantFPRange.h"43#include "llvm/IR/ConstantRange.h"44#include "llvm/IR/Constants.h"45#include "llvm/IR/DerivedTypes.h"46#include "llvm/IR/DiagnosticInfo.h"47#include "llvm/IR/Dominators.h"48#include "llvm/IR/EHPersonalities.h"49#include "llvm/IR/Function.h"50#include "llvm/IR/GetElementPtrTypeIterator.h"51#include "llvm/IR/GlobalAlias.h"52#include "llvm/IR/GlobalValue.h"53#include "llvm/IR/GlobalVariable.h"54#include "llvm/IR/InstrTypes.h"55#include "llvm/IR/Instruction.h"56#include "llvm/IR/Instructions.h"57#include "llvm/IR/IntrinsicInst.h"58#include "llvm/IR/Intrinsics.h"59#include "llvm/IR/IntrinsicsAArch64.h"60#include "llvm/IR/IntrinsicsAMDGPU.h"61#include "llvm/IR/IntrinsicsRISCV.h"62#include "llvm/IR/IntrinsicsX86.h"63#include "llvm/IR/LLVMContext.h"64#include "llvm/IR/Metadata.h"65#include "llvm/IR/Module.h"66#include "llvm/IR/Operator.h"67#include "llvm/IR/PatternMatch.h"68#include "llvm/IR/Type.h"69#include "llvm/IR/User.h"70#include "llvm/IR/Value.h"71#include "llvm/Support/Casting.h"72#include "llvm/Support/CommandLine.h"73#include "llvm/Support/Compiler.h"74#include "llvm/Support/ErrorHandling.h"75#include "llvm/Support/KnownBits.h"76#include "llvm/Support/KnownFPClass.h"77#include "llvm/Support/MathExtras.h"78#include "llvm/TargetParser/RISCVTargetParser.h"79#include <algorithm>80#include <cassert>81#include <cstdint>82#include <optional>83#include <utility>84 85using namespace llvm;86using namespace llvm::PatternMatch;87 88// Controls the number of uses of the value searched for possible89// dominating comparisons.90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",91 cl::Hidden, cl::init(20));92 93/// Maximum number of instructions to check between assume and context94/// instruction.95static constexpr unsigned MaxInstrsToCheckForFree = 16;96 97/// Returns the bitwidth of the given scalar or pointer type. For vector types,98/// returns the element type's bitwidth.99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {100 if (unsigned BitWidth = Ty->getScalarSizeInBits())101 return BitWidth;102 103 return DL.getPointerTypeSizeInBits(Ty);104}105 106// Given the provided Value and, potentially, a context instruction, return107// the preferred context instruction (if any).108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {109 // If we've been provided with a context instruction, then use that (provided110 // it has been inserted).111 if (CxtI && CxtI->getParent())112 return CxtI;113 114 // If the value is really an already-inserted instruction, then use that.115 CxtI = dyn_cast<Instruction>(V);116 if (CxtI && CxtI->getParent())117 return CxtI;118 119 return nullptr;120}121 122static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf,123 const APInt &DemandedElts,124 APInt &DemandedLHS, APInt &DemandedRHS) {125 if (isa<ScalableVectorType>(Shuf->getType())) {126 assert(DemandedElts == APInt(1,1));127 DemandedLHS = DemandedRHS = DemandedElts;128 return true;129 }130 131 int NumElts =132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),134 DemandedElts, DemandedLHS, DemandedRHS);135}136 137static void computeKnownBits(const Value *V, const APInt &DemandedElts,138 KnownBits &Known, const SimplifyQuery &Q,139 unsigned Depth);140 141void llvm::computeKnownBits(const Value *V, KnownBits &Known,142 const SimplifyQuery &Q, unsigned Depth) {143 // Since the number of lanes in a scalable vector is unknown at compile time,144 // we track one bit which is implicitly broadcast to all lanes. This means145 // that all lanes in a scalable vector are considered demanded.146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());147 APInt DemandedElts =148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);150}151 152void llvm::computeKnownBits(const Value *V, KnownBits &Known,153 const DataLayout &DL, AssumptionCache *AC,154 const Instruction *CxtI, const DominatorTree *DT,155 bool UseInstrInfo, unsigned Depth) {156 computeKnownBits(V, Known,157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),158 Depth);159}160 161KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,162 AssumptionCache *AC, const Instruction *CxtI,163 const DominatorTree *DT, bool UseInstrInfo,164 unsigned Depth) {165 return computeKnownBits(166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);167}168 169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,170 const DataLayout &DL, AssumptionCache *AC,171 const Instruction *CxtI,172 const DominatorTree *DT, bool UseInstrInfo,173 unsigned Depth) {174 return computeKnownBits(175 V, DemandedElts,176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);177}178 179static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,180 const SimplifyQuery &SQ) {181 // Look for an inverted mask: (X & ~M) op (Y & M).182 {183 Value *M;184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&185 match(RHS, m_c_And(m_Specific(M), m_Value())) &&186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))187 return true;188 }189 190 // X op (Y & ~X)191 if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) &&192 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))193 return true;194 195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern196 // for constant Y.197 Value *Y;198 if (match(RHS,199 m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) &&200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))202 return true;203 204 // Peek through extends to find a 'not' of the other side:205 // (ext Y) op ext(~Y)206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&207 match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y)))) &&208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))209 return true;210 211 // Look for: (A & B) op ~(A | B)212 {213 Value *A, *B;214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&215 match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))) &&216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))218 return true;219 }220 221 // Look for: (X << V) op (Y >> (BitWidth - V))222 // or (X >> V) op (Y << (BitWidth - V))223 {224 const Value *V;225 const APInt *R;226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&230 R->uge(LHS->getType()->getScalarSizeInBits()))231 return true;232 }233 234 return false;235}236 237bool llvm::haveNoCommonBitsSet(const WithCache<const Value *> &LHSCache,238 const WithCache<const Value *> &RHSCache,239 const SimplifyQuery &SQ) {240 const Value *LHS = LHSCache.getValue();241 const Value *RHS = RHSCache.getValue();242 243 assert(LHS->getType() == RHS->getType() &&244 "LHS and RHS should have the same type");245 assert(LHS->getType()->isIntOrIntVectorTy() &&246 "LHS and RHS should be integers");247 248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||249 haveNoCommonBitsSetSpecialCases(RHS, LHS, SQ))250 return true;251 252 return KnownBits::haveNoCommonBitsSet(LHSCache.getKnownBits(SQ),253 RHSCache.getKnownBits(SQ));254}255 256bool llvm::isOnlyUsedInZeroComparison(const Instruction *I) {257 return !I->user_empty() &&258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));259}260 261bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *I) {262 return !I->user_empty() && all_of(I->users(), [](const User *U) {263 CmpPredicate P;264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);265 });266}267 268bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,269 bool OrZero, AssumptionCache *AC,270 const Instruction *CxtI,271 const DominatorTree *DT, bool UseInstrInfo,272 unsigned Depth) {273 return ::isKnownToBeAPowerOfTwo(274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),275 Depth);276}277 278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,279 const SimplifyQuery &Q, unsigned Depth);280 281bool llvm::isKnownNonNegative(const Value *V, const SimplifyQuery &SQ,282 unsigned Depth) {283 return computeKnownBits(V, SQ, Depth).isNonNegative();284}285 286bool llvm::isKnownPositive(const Value *V, const SimplifyQuery &SQ,287 unsigned Depth) {288 if (auto *CI = dyn_cast<ConstantInt>(V))289 return CI->getValue().isStrictlyPositive();290 291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep292 // this updated.293 KnownBits Known = computeKnownBits(V, SQ, Depth);294 return Known.isNonNegative() &&295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));296}297 298bool llvm::isKnownNegative(const Value *V, const SimplifyQuery &SQ,299 unsigned Depth) {300 return computeKnownBits(V, SQ, Depth).isNegative();301}302 303static bool isKnownNonEqual(const Value *V1, const Value *V2,304 const APInt &DemandedElts, const SimplifyQuery &Q,305 unsigned Depth);306 307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,308 const SimplifyQuery &Q, unsigned Depth) {309 // We don't support looking through casts.310 if (V1 == V2 || V1->getType() != V2->getType())311 return false;312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());313 APInt DemandedElts =314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);316}317 318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,319 const SimplifyQuery &SQ, unsigned Depth) {320 KnownBits Known(Mask.getBitWidth());321 computeKnownBits(V, Known, SQ, Depth);322 return Mask.isSubsetOf(Known.Zero);323}324 325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,326 const SimplifyQuery &Q, unsigned Depth);327 328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,329 unsigned Depth = 0) {330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());331 APInt DemandedElts =332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);334}335 336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,337 AssumptionCache *AC, const Instruction *CxtI,338 const DominatorTree *DT, bool UseInstrInfo,339 unsigned Depth) {340 return ::ComputeNumSignBits(341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);342}343 344unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL,345 AssumptionCache *AC,346 const Instruction *CxtI,347 const DominatorTree *DT,348 unsigned Depth) {349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);350 return V->getType()->getScalarSizeInBits() - SignBits + 1;351}352 353/// Try to detect the lerp pattern: a * (b - c) + c * d354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.355///356/// In that particular case, we can use the following chain of reasoning:357///358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)359///360/// Since that is true for arbitrary a, b, c and d within our constraints, we361/// can conclude that:362///363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U364///365/// Considering that any result of the lerp would be less or equal to U, it366/// would have at least the number of leading 0s as in U.367///368/// While being quite a specific situation, it is fairly common in computer369/// graphics in the shape of alpha blending.370///371/// Modifies given KnownOut in-place with the inferred information.372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,373 const APInt &DemandedElts,374 KnownBits &KnownOut,375 const SimplifyQuery &Q,376 unsigned Depth) {377 378 Type *Ty = Op0->getType();379 const unsigned BitWidth = Ty->getScalarSizeInBits();380 381 // Only handle scalar types for now382 if (Ty->isVectorTy())383 return;384 385 // Try to match: a * (b - c) + c * d.386 // When a == 1 => A == nullptr, the same applies to d/D as well.387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;388 const Instruction *SubBC = nullptr;389 390 const auto MatchSubBC = [&]() {391 // (b - c) can have two forms that interest us:392 //393 // 1. sub nuw %b, %c394 // 2. xor %c, %b395 //396 // For the first case, nuw flag guarantees our requirement b >= c.397 //398 // The second case might happen when the analysis can infer that b is a mask399 // for c and we can transform sub operation into xor (that is usually true400 // for constant b's). Even though xor is symmetrical, canonicalization401 // ensures that the constant will be the RHS. We have additional checks402 // later on to ensure that this xor operation is equivalent to subtraction.403 return m_Instruction(SubBC, m_CombineOr(m_NUWSub(m_Value(B), m_Value(C)),404 m_Xor(m_Value(C), m_Value(B))));405 };406 407 const auto MatchASubBC = [&]() {408 // Cases:409 // - a * (b - c)410 // - (b - c) * a411 // - (b - c) <- a implicitly equals 1412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());413 };414 415 const auto MatchCD = [&]() {416 // Cases:417 // - d * c418 // - c * d419 // - c <- d implicitly equals 1420 return m_CombineOr(m_c_Mul(m_Value(D), m_Specific(C)), m_Specific(C));421 };422 423 const auto Match = [&](const Value *LHS, const Value *RHS) {424 // We do use m_Specific(C) in MatchCD, so we have to make sure that425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely426 // has to evaluate first and return true.427 //428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());430 };431 432 if (!Match(Op0, Op1) && !Match(Op1, Op0))433 return;434 435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {436 // For some of the values we use the convention of leaving437 // it nullptr to signify an implicit constant 1.438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)439 : KnownBits::makeConstant(APInt(BitWidth, 1));440 };441 442 // Check that all operands are non-negative443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);444 if (!KnownA.isNonNegative())445 return;446 447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);448 if (!KnownD.isNonNegative())449 return;450 451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);452 if (!KnownB.isNonNegative())453 return;454 455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);456 if (!KnownC.isNonNegative())457 return;458 459 // If we matched subtraction as xor, we need to actually check that xor460 // is semantically equivalent to subtraction.461 //462 // For that to be true, b has to be a mask for c or that b's known463 // ones cover all known and possible ones of c.464 if (SubBC->getOpcode() == Instruction::Xor &&465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))466 return;467 468 const APInt MaxA = KnownA.getMaxValue();469 const APInt MaxD = KnownD.getMaxValue();470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);471 const APInt MaxB = KnownB.getMaxValue();472 473 // We can't infer leading zeros info if the upper-bound estimate wraps.474 bool Overflow;475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);476 477 if (Overflow)478 return;479 480 // If we know that x <= y and both are positive than x has at least the same481 // number of leading zeros as y.482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);484}485 486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,487 bool NSW, bool NUW,488 const APInt &DemandedElts,489 KnownBits &KnownOut, KnownBits &Known2,490 const SimplifyQuery &Q, unsigned Depth) {491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);492 493 // If one operand is unknown and we have no nowrap information,494 // the result will be unknown independently of the second operand.495 if (KnownOut.isUnknown() && !NSW && !NUW)496 return;497 498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);500 501 if (!Add && NSW && !KnownOut.isNonNegative() &&502 isImpliedByDomCondition(ICmpInst::ICMP_SLE, Op1, Op0, Q.CxtI, Q.DL)503 .value_or(false))504 KnownOut.makeNonNegative();505 506 if (Add)507 // Try to match lerp pattern and combine results508 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);509}510 511static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,512 bool NUW, const APInt &DemandedElts,513 KnownBits &Known, KnownBits &Known2,514 const SimplifyQuery &Q, unsigned Depth) {515 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);516 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);517 518 bool isKnownNegative = false;519 bool isKnownNonNegative = false;520 // If the multiplication is known not to overflow, compute the sign bit.521 if (NSW) {522 if (Op0 == Op1) {523 // The product of a number with itself is non-negative.524 isKnownNonNegative = true;525 } else {526 bool isKnownNonNegativeOp1 = Known.isNonNegative();527 bool isKnownNonNegativeOp0 = Known2.isNonNegative();528 bool isKnownNegativeOp1 = Known.isNegative();529 bool isKnownNegativeOp0 = Known2.isNegative();530 // The product of two numbers with the same sign is non-negative.531 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||532 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);533 if (!isKnownNonNegative && NUW) {534 // mul nuw nsw with a factor > 1 is non-negative.535 KnownBits One = KnownBits::makeConstant(APInt(Known.getBitWidth(), 1));536 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||537 KnownBits::sgt(Known2, One).value_or(false);538 }539 540 // The product of a negative number and a non-negative number is either541 // negative or zero.542 if (!isKnownNonNegative)543 isKnownNegative =544 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&545 Known2.isNonZero()) ||546 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());547 }548 }549 550 bool SelfMultiply = Op0 == Op1;551 if (SelfMultiply)552 SelfMultiply &=553 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);554 Known = KnownBits::mul(Known, Known2, SelfMultiply);555 556 if (SelfMultiply) {557 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);558 unsigned TyBits = Op0->getType()->getScalarSizeInBits();559 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);560 561 if (OutValidBits < TyBits) {562 APInt KnownZeroMask =563 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);564 Known.Zero |= KnownZeroMask;565 }566 }567 568 // Only make use of no-wrap flags if we failed to compute the sign bit569 // directly. This matters if the multiplication always overflows, in570 // which case we prefer to follow the result of the direct computation,571 // though as the program is invoking undefined behaviour we can choose572 // whatever we like here.573 if (isKnownNonNegative && !Known.isNegative())574 Known.makeNonNegative();575 else if (isKnownNegative && !Known.isNonNegative())576 Known.makeNegative();577}578 579void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,580 KnownBits &Known) {581 unsigned BitWidth = Known.getBitWidth();582 unsigned NumRanges = Ranges.getNumOperands() / 2;583 assert(NumRanges >= 1);584 585 Known.setAllConflict();586 587 for (unsigned i = 0; i < NumRanges; ++i) {588 ConstantInt *Lower =589 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));590 ConstantInt *Upper =591 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));592 ConstantRange Range(Lower->getValue(), Upper->getValue());593 // BitWidth must equal the Ranges BitWidth for the correct number of high594 // bits to be set.595 assert(BitWidth == Range.getBitWidth() &&596 "Known bit width must match range bit width!");597 598 // The first CommonPrefixBits of all values in Range are equal.599 unsigned CommonPrefixBits =600 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();601 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);602 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);603 Known.One &= UnsignedMax & Mask;604 Known.Zero &= ~UnsignedMax & Mask;605 }606}607 608static bool isEphemeralValueOf(const Instruction *I, const Value *E) {609 SmallVector<const Instruction *, 16> WorkSet(1, I);610 SmallPtrSet<const Instruction *, 32> Visited;611 SmallPtrSet<const Instruction *, 16> EphValues;612 613 // The instruction defining an assumption's condition itself is always614 // considered ephemeral to that assumption (even if it has other615 // non-ephemeral users). See r246696's test case for an example.616 if (is_contained(I->operands(), E))617 return true;618 619 while (!WorkSet.empty()) {620 const Instruction *V = WorkSet.pop_back_val();621 if (!Visited.insert(V).second)622 continue;623 624 // If all uses of this value are ephemeral, then so is this value.625 if (all_of(V->users(), [&](const User *U) {626 return EphValues.count(cast<Instruction>(U));627 })) {628 if (V == E)629 return true;630 631 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {632 EphValues.insert(V);633 634 if (const User *U = dyn_cast<User>(V)) {635 for (const Use &U : U->operands()) {636 if (const auto *I = dyn_cast<Instruction>(U.get()))637 WorkSet.push_back(I);638 }639 }640 }641 }642 }643 644 return false;645}646 647// Is this an intrinsic that cannot be speculated but also cannot trap?648bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {649 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))650 return CI->isAssumeLikeIntrinsic();651 652 return false;653}654 655bool llvm::isValidAssumeForContext(const Instruction *Inv,656 const Instruction *CxtI,657 const DominatorTree *DT,658 bool AllowEphemerals) {659 // There are two restrictions on the use of an assume:660 // 1. The assume must dominate the context (or the control flow must661 // reach the assume whenever it reaches the context).662 // 2. The context must not be in the assume's set of ephemeral values663 // (otherwise we will use the assume to prove that the condition664 // feeding the assume is trivially true, thus causing the removal of665 // the assume).666 667 if (Inv->getParent() == CxtI->getParent()) {668 // If Inv and CtxI are in the same block, check if the assume (Inv) is first669 // in the BB.670 if (Inv->comesBefore(CxtI))671 return true;672 673 // Don't let an assume affect itself - this would cause the problems674 // `isEphemeralValueOf` is trying to prevent, and it would also make675 // the loop below go out of bounds.676 if (!AllowEphemerals && Inv == CxtI)677 return false;678 679 // The context comes first, but they're both in the same block.680 // Make sure there is nothing in between that might interrupt681 // the control flow, not even CxtI itself.682 // We limit the scan distance between the assume and its context instruction683 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so684 // it can be adjusted if needed (could be turned into a cl::opt).685 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());686 if (!isGuaranteedToTransferExecutionToSuccessor(Range, 15))687 return false;688 689 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);690 }691 692 // Inv and CxtI are in different blocks.693 if (DT) {694 if (DT->dominates(Inv, CxtI))695 return true;696 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||697 Inv->getParent()->isEntryBlock()) {698 // We don't have a DT, but this trivially dominates.699 return true;700 }701 702 return false;703}704 705bool llvm::willNotFreeBetween(const Instruction *Assume,706 const Instruction *CtxI) {707 // Helper to check if there are any calls in the range that may free memory.708 auto hasNoFreeCalls = [](auto Range) {709 for (const auto &[Idx, I] : enumerate(Range)) {710 if (Idx > MaxInstrsToCheckForFree)711 return false;712 if (const auto *CB = dyn_cast<CallBase>(&I))713 if (!CB->hasFnAttr(Attribute::NoFree))714 return false;715 }716 return true;717 };718 719 // Make sure the current function cannot arrange for another thread to free on720 // its behalf.721 if (!CtxI->getFunction()->hasNoSync())722 return false;723 724 // Handle cross-block case: CtxI in a successor of Assume's block.725 const BasicBlock *CtxBB = CtxI->getParent();726 const BasicBlock *AssumeBB = Assume->getParent();727 BasicBlock::const_iterator CtxIter = CtxI->getIterator();728 if (CtxBB != AssumeBB) {729 if (CtxBB->getSinglePredecessor() != AssumeBB)730 return false;731 732 if (!hasNoFreeCalls(make_range(CtxBB->begin(), CtxIter)))733 return false;734 735 CtxIter = AssumeBB->end();736 } else {737 // Same block case: check that Assume comes before CtxI.738 if (!Assume->comesBefore(CtxI))739 return false;740 }741 742 // Check if there are any calls between Assume and CtxIter that may free743 // memory.744 return hasNoFreeCalls(make_range(Assume->getIterator(), CtxIter));745}746 747// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but748// we still have enough information about `RHS` to conclude non-zero. For749// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops750// so the extra compile time may not be worth it, but possibly a second API751// should be created for use outside of loops.752static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {753 // v u> y implies v != 0.754 if (Pred == ICmpInst::ICMP_UGT)755 return true;756 757 // Special-case v != 0 to also handle v != null.758 if (Pred == ICmpInst::ICMP_NE)759 return match(RHS, m_Zero());760 761 // All other predicates - rely on generic ConstantRange handling.762 const APInt *C;763 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());764 if (match(RHS, m_APInt(C))) {765 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);766 return !TrueValues.contains(Zero);767 }768 769 auto *VC = dyn_cast<ConstantDataVector>(RHS);770 if (VC == nullptr)771 return false;772 773 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;774 ++ElemIdx) {775 ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(776 Pred, VC->getElementAsAPInt(ElemIdx));777 if (TrueValues.contains(Zero))778 return false;779 }780 return true;781}782 783static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,784 Value *&ValOut, Instruction *&CtxIOut,785 const PHINode **PhiOut = nullptr) {786 ValOut = U->get();787 if (ValOut == PHI)788 return;789 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();790 if (PhiOut)791 *PhiOut = PHI;792 Value *V;793 // If the Use is a select of this phi, compute analysis on other arm to break794 // recursion.795 // TODO: Min/Max796 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||797 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))798 ValOut = V;799 800 // Same for select, if this phi is 2-operand phi, compute analysis on other801 // incoming value to break recursion.802 // TODO: We could handle any number of incoming edges as long as we only have803 // two unique values.804 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);805 IncPhi && IncPhi->getNumIncomingValues() == 2) {806 for (int Idx = 0; Idx < 2; ++Idx) {807 if (IncPhi->getIncomingValue(Idx) == PHI) {808 ValOut = IncPhi->getIncomingValue(1 - Idx);809 if (PhiOut)810 *PhiOut = IncPhi;811 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();812 break;813 }814 }815 }816}817 818static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {819 // Use of assumptions is context-sensitive. If we don't have a context, we820 // cannot use them!821 if (!Q.AC || !Q.CxtI)822 return false;823 824 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {825 if (!Elem.Assume)826 continue;827 828 AssumeInst *I = cast<AssumeInst>(Elem.Assume);829 assert(I->getFunction() == Q.CxtI->getFunction() &&830 "Got assumption for the wrong function!");831 832 if (Elem.Index != AssumptionCache::ExprResultIdx) {833 if (!V->getType()->isPointerTy())834 continue;835 if (RetainedKnowledge RK = getKnowledgeFromBundle(836 *I, I->bundle_op_info_begin()[Elem.Index])) {837 if (RK.WasOn == V &&838 (RK.AttrKind == Attribute::NonNull ||839 (RK.AttrKind == Attribute::Dereferenceable &&840 !NullPointerIsDefined(Q.CxtI->getFunction(),841 V->getType()->getPointerAddressSpace()))) &&842 isValidAssumeForContext(I, Q.CxtI, Q.DT))843 return true;844 }845 continue;846 }847 848 // Warning: This loop can end up being somewhat performance sensitive.849 // We're running this loop for once for each value queried resulting in a850 // runtime of ~O(#assumes * #values).851 852 Value *RHS;853 CmpPredicate Pred;854 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));855 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))856 continue;857 858 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))859 return true;860 }861 862 return false;863}864 865static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,866 Value *LHS, Value *RHS, KnownBits &Known,867 const SimplifyQuery &Q) {868 if (RHS->getType()->isPointerTy()) {869 // Handle comparison of pointer to null explicitly, as it will not be870 // covered by the m_APInt() logic below.871 if (LHS == V && match(RHS, m_Zero())) {872 switch (Pred) {873 case ICmpInst::ICMP_EQ:874 Known.setAllZero();875 break;876 case ICmpInst::ICMP_SGE:877 case ICmpInst::ICMP_SGT:878 Known.makeNonNegative();879 break;880 case ICmpInst::ICMP_SLT:881 Known.makeNegative();882 break;883 default:884 break;885 }886 }887 return;888 }889 890 unsigned BitWidth = Known.getBitWidth();891 auto m_V =892 m_CombineOr(m_Specific(V), m_PtrToIntSameSize(Q.DL, m_Specific(V)));893 894 Value *Y;895 const APInt *Mask, *C;896 if (!match(RHS, m_APInt(C)))897 return;898 899 uint64_t ShAmt;900 switch (Pred) {901 case ICmpInst::ICMP_EQ:902 // assume(V = C)903 if (match(LHS, m_V)) {904 Known = Known.unionWith(KnownBits::makeConstant(*C));905 // assume(V & Mask = C)906 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {907 // For one bits in Mask, we can propagate bits from C to V.908 Known.One |= *C;909 if (match(Y, m_APInt(Mask)))910 Known.Zero |= ~*C & *Mask;911 // assume(V | Mask = C)912 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {913 // For zero bits in Mask, we can propagate bits from C to V.914 Known.Zero |= ~*C;915 if (match(Y, m_APInt(Mask)))916 Known.One |= *C & ~*Mask;917 // assume(V << ShAmt = C)918 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&919 ShAmt < BitWidth) {920 // For those bits in C that are known, we can propagate them to known921 // bits in V shifted to the right by ShAmt.922 KnownBits RHSKnown = KnownBits::makeConstant(*C);923 RHSKnown >>= ShAmt;924 Known = Known.unionWith(RHSKnown);925 // assume(V >> ShAmt = C)926 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&927 ShAmt < BitWidth) {928 // For those bits in RHS that are known, we can propagate them to known929 // bits in V shifted to the right by C.930 KnownBits RHSKnown = KnownBits::makeConstant(*C);931 RHSKnown <<= ShAmt;932 Known = Known.unionWith(RHSKnown);933 }934 break;935 case ICmpInst::ICMP_NE: {936 // assume (V & B != 0) where B is a power of 2937 const APInt *BPow2;938 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))939 Known.One |= *BPow2;940 break;941 }942 default: {943 const APInt *Offset = nullptr;944 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {945 ConstantRange LHSRange = ConstantRange::makeAllowedICmpRegion(Pred, *C);946 if (Offset)947 LHSRange = LHSRange.sub(*Offset);948 Known = Known.unionWith(LHSRange.toKnownBits());949 }950 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {951 // X & Y u> C -> X u> C && Y u> C952 // X nuw- Y u> C -> X u> C953 if (match(LHS, m_c_And(m_V, m_Value())) ||954 match(LHS, m_NUWSub(m_V, m_Value())))955 Known.One.setHighBits(956 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());957 }958 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {959 // X | Y u< C -> X u< C && Y u< C960 // X nuw+ Y u< C -> X u< C && Y u< C961 if (match(LHS, m_c_Or(m_V, m_Value())) ||962 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {963 Known.Zero.setHighBits(964 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());965 }966 }967 } break;968 }969}970 971static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,972 KnownBits &Known,973 const SimplifyQuery &SQ, bool Invert) {974 ICmpInst::Predicate Pred =975 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();976 Value *LHS = Cmp->getOperand(0);977 Value *RHS = Cmp->getOperand(1);978 979 // Handle icmp pred (trunc V), C980 if (match(LHS, m_Trunc(m_Specific(V)))) {981 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());982 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);983 if (cast<TruncInst>(LHS)->hasNoUnsignedWrap())984 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));985 else986 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));987 return;988 }989 990 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);991}992 993static void computeKnownBitsFromCond(const Value *V, Value *Cond,994 KnownBits &Known, const SimplifyQuery &SQ,995 bool Invert, unsigned Depth) {996 Value *A, *B;997 if (Depth < MaxAnalysisRecursionDepth &&998 match(Cond, m_LogicalOp(m_Value(A), m_Value(B)))) {999 KnownBits Known2(Known.getBitWidth());1000 KnownBits Known3(Known.getBitWidth());1001 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);1002 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);1003 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))1004 : match(Cond, m_LogicalAnd(m_Value(), m_Value())))1005 Known2 = Known2.unionWith(Known3);1006 else1007 Known2 = Known2.intersectWith(Known3);1008 Known = Known.unionWith(Known2);1009 return;1010 }1011 1012 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {1013 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);1014 return;1015 }1016 1017 if (match(Cond, m_Trunc(m_Specific(V)))) {1018 KnownBits DstKnown(1);1019 if (Invert) {1020 DstKnown.setAllZero();1021 } else {1022 DstKnown.setAllOnes();1023 }1024 if (cast<TruncInst>(Cond)->hasNoUnsignedWrap()) {1025 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));1026 return;1027 }1028 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));1029 return;1030 }1031 1032 if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A))))1033 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);1034}1035 1036void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,1037 const SimplifyQuery &Q, unsigned Depth) {1038 // Handle injected condition.1039 if (Q.CC && Q.CC->AffectedValues.contains(V))1040 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);1041 1042 if (!Q.CxtI)1043 return;1044 1045 if (Q.DC && Q.DT) {1046 // Handle dominating conditions.1047 for (BranchInst *BI : Q.DC->conditionsFor(V)) {1048 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));1049 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))1050 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,1051 /*Invert*/ false, Depth);1052 1053 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));1054 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))1055 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,1056 /*Invert*/ true, Depth);1057 }1058 1059 if (Known.hasConflict())1060 Known.resetAll();1061 }1062 1063 if (!Q.AC)1064 return;1065 1066 unsigned BitWidth = Known.getBitWidth();1067 1068 // Note that the patterns below need to be kept in sync with the code1069 // in AssumptionCache::updateAffectedValues.1070 1071 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {1072 if (!Elem.Assume)1073 continue;1074 1075 AssumeInst *I = cast<AssumeInst>(Elem.Assume);1076 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&1077 "Got assumption for the wrong function!");1078 1079 if (Elem.Index != AssumptionCache::ExprResultIdx) {1080 if (!V->getType()->isPointerTy())1081 continue;1082 if (RetainedKnowledge RK = getKnowledgeFromBundle(1083 *I, I->bundle_op_info_begin()[Elem.Index])) {1084 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might1085 // be the producer of the pointer in the bundle. At the moment, align1086 // assumptions aren't optimized away.1087 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&1088 isPowerOf2_64(RK.ArgValue) &&1089 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))1090 Known.Zero.setLowBits(Log2_64(RK.ArgValue));1091 }1092 continue;1093 }1094 1095 // Warning: This loop can end up being somewhat performance sensitive.1096 // We're running this loop for once for each value queried resulting in a1097 // runtime of ~O(#assumes * #values).1098 1099 Value *Arg = I->getArgOperand(0);1100 1101 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {1102 assert(BitWidth == 1 && "assume operand is not i1?");1103 (void)BitWidth;1104 Known.setAllOnes();1105 return;1106 }1107 if (match(Arg, m_Not(m_Specific(V))) &&1108 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {1109 assert(BitWidth == 1 && "assume operand is not i1?");1110 (void)BitWidth;1111 Known.setAllZero();1112 return;1113 }1114 auto *Trunc = dyn_cast<TruncInst>(Arg);1115 if (Trunc && Trunc->getOperand(0) == V &&1116 isValidAssumeForContext(I, Q.CxtI, Q.DT)) {1117 if (Trunc->hasNoUnsignedWrap()) {1118 Known = KnownBits::makeConstant(APInt(BitWidth, 1));1119 return;1120 }1121 Known.One.setBit(0);1122 return;1123 }1124 1125 // The remaining tests are all recursive, so bail out if we hit the limit.1126 if (Depth == MaxAnalysisRecursionDepth)1127 continue;1128 1129 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);1130 if (!Cmp)1131 continue;1132 1133 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))1134 continue;1135 1136 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);1137 }1138 1139 // Conflicting assumption: Undefined behavior will occur on this execution1140 // path.1141 if (Known.hasConflict())1142 Known.resetAll();1143}1144 1145/// Compute known bits from a shift operator, including those with a1146/// non-constant shift amount. Known is the output of this function. Known2 is a1147/// pre-allocated temporary with the same bit width as Known and on return1148/// contains the known bit of the shift value source. KF is an1149/// operator-specific function that, given the known-bits and a shift amount,1150/// compute the implied known-bits of the shift operator's result respectively1151/// for that shift amount. The results from calling KF are conservatively1152/// combined for all permitted shift amounts.1153static void computeKnownBitsFromShiftOperator(1154 const Operator *I, const APInt &DemandedElts, KnownBits &Known,1155 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,1156 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {1157 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);1158 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);1159 // To limit compile-time impact, only query isKnownNonZero() if we know at1160 // least something about the shift amount.1161 bool ShAmtNonZero =1162 Known.isNonZero() ||1163 (Known.getMaxValue().ult(Known.getBitWidth()) &&1164 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));1165 Known = KF(Known2, Known, ShAmtNonZero);1166}1167 1168static KnownBits1169getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,1170 const KnownBits &KnownLHS, const KnownBits &KnownRHS,1171 const SimplifyQuery &Q, unsigned Depth) {1172 unsigned BitWidth = KnownLHS.getBitWidth();1173 KnownBits KnownOut(BitWidth);1174 bool IsAnd = false;1175 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();1176 Value *X = nullptr, *Y = nullptr;1177 1178 switch (I->getOpcode()) {1179 case Instruction::And:1180 KnownOut = KnownLHS & KnownRHS;1181 IsAnd = true;1182 // and(x, -x) is common idioms that will clear all but lowest set1183 // bit. If we have a single known bit in x, we can clear all bits1184 // above it.1185 // TODO: instcombine often reassociates independent `and` which can hide1186 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).1187 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {1188 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.1189 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())1190 KnownOut = KnownLHS.blsi();1191 else1192 KnownOut = KnownRHS.blsi();1193 }1194 break;1195 case Instruction::Or:1196 KnownOut = KnownLHS | KnownRHS;1197 break;1198 case Instruction::Xor:1199 KnownOut = KnownLHS ^ KnownRHS;1200 // xor(x, x-1) is common idioms that will clear all but lowest set1201 // bit. If we have a single known bit in x, we can clear all bits1202 // above it.1203 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=1204 // -1 but for the purpose of demanded bits (xor(x, x-C) &1205 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern1206 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).1207 if (HasKnownOne &&1208 match(I, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())))) {1209 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;1210 KnownOut = XBits.blsmsk();1211 }1212 break;1213 default:1214 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");1215 }1216 1217 // and(x, add (x, -1)) is a common idiom that always clears the low bit;1218 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.1219 // here we handle the more general case of adding any odd number by1220 // matching the form and/xor/or(x, add(x, y)) where y is odd.1221 // TODO: This could be generalized to clearing any bit set in y where the1222 // following bit is known to be unset in y.1223 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&1224 (match(I, m_c_BinOp(m_Value(X), m_c_Add(m_Deferred(X), m_Value(Y)))) ||1225 match(I, m_c_BinOp(m_Value(X), m_Sub(m_Deferred(X), m_Value(Y)))) ||1226 match(I, m_c_BinOp(m_Value(X), m_Sub(m_Value(Y), m_Deferred(X)))))) {1227 KnownBits KnownY(BitWidth);1228 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);1229 if (KnownY.countMinTrailingOnes() > 0) {1230 if (IsAnd)1231 KnownOut.Zero.setBit(0);1232 else1233 KnownOut.One.setBit(0);1234 }1235 }1236 return KnownOut;1237}1238 1239static KnownBits computeKnownBitsForHorizontalOperation(1240 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,1241 unsigned Depth,1242 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>1243 KnownBitsFunc) {1244 APInt DemandedEltsLHS, DemandedEltsRHS;1245 getHorizDemandedEltsForFirstOperand(Q.DL.getTypeSizeInBits(I->getType()),1246 DemandedElts, DemandedEltsLHS,1247 DemandedEltsRHS);1248 1249 const auto ComputeForSingleOpFunc =1250 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {1251 return KnownBitsFunc(1252 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),1253 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));1254 };1255 1256 if (DemandedEltsRHS.isZero())1257 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);1258 if (DemandedEltsLHS.isZero())1259 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);1260 1261 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)1262 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));1263}1264 1265// Public so this can be used in `SimplifyDemandedUseBits`.1266KnownBits llvm::analyzeKnownBitsFromAndXorOr(const Operator *I,1267 const KnownBits &KnownLHS,1268 const KnownBits &KnownRHS,1269 const SimplifyQuery &SQ,1270 unsigned Depth) {1271 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());1272 APInt DemandedElts =1273 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);1274 1275 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,1276 Depth);1277}1278 1279ConstantRange llvm::getVScaleRange(const Function *F, unsigned BitWidth) {1280 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);1281 // Without vscale_range, we only know that vscale is non-zero.1282 if (!Attr.isValid())1283 return ConstantRange(APInt(BitWidth, 1), APInt::getZero(BitWidth));1284 1285 unsigned AttrMin = Attr.getVScaleRangeMin();1286 // Minimum is larger than vscale width, result is always poison.1287 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)1288 return ConstantRange::getEmpty(BitWidth);1289 1290 APInt Min(BitWidth, AttrMin);1291 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();1292 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)1293 return ConstantRange(Min, APInt::getZero(BitWidth));1294 1295 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);1296}1297 1298void llvm::adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond,1299 Value *Arm, bool Invert,1300 const SimplifyQuery &Q, unsigned Depth) {1301 // If we have a constant arm, we are done.1302 if (Known.isConstant())1303 return;1304 1305 // See what condition implies about the bits of the select arm.1306 KnownBits CondRes(Known.getBitWidth());1307 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);1308 // If we don't get any information from the condition, no reason to1309 // proceed.1310 if (CondRes.isUnknown())1311 return;1312 1313 // We can have conflict if the condition is dead. I.e if we have1314 // (x | 64) < 32 ? (x | 64) : y1315 // we will have conflict at bit 6 from the condition/the `or`.1316 // In that case just return. Its not particularly important1317 // what we do, as this select is going to be simplified soon.1318 CondRes = CondRes.unionWith(Known);1319 if (CondRes.hasConflict())1320 return;1321 1322 // Finally make sure the information we found is valid. This is relatively1323 // expensive so it's left for the very end.1324 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))1325 return;1326 1327 // Finally, we know we get information from the condition and its valid,1328 // so return it.1329 Known = CondRes;1330}1331 1332// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).1333// Returns the input and lower/upper bounds.1334static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,1335 const APInt *&CLow, const APInt *&CHigh) {1336 assert(isa<Operator>(Select) &&1337 cast<Operator>(Select)->getOpcode() == Instruction::Select &&1338 "Input should be a Select!");1339 1340 const Value *LHS = nullptr, *RHS = nullptr;1341 SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;1342 if (SPF != SPF_SMAX && SPF != SPF_SMIN)1343 return false;1344 1345 if (!match(RHS, m_APInt(CLow)))1346 return false;1347 1348 const Value *LHS2 = nullptr, *RHS2 = nullptr;1349 SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;1350 if (getInverseMinMaxFlavor(SPF) != SPF2)1351 return false;1352 1353 if (!match(RHS2, m_APInt(CHigh)))1354 return false;1355 1356 if (SPF == SPF_SMIN)1357 std::swap(CLow, CHigh);1358 1359 In = LHS2;1360 return CLow->sle(*CHigh);1361}1362 1363static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II,1364 const APInt *&CLow,1365 const APInt *&CHigh) {1366 assert((II->getIntrinsicID() == Intrinsic::smin ||1367 II->getIntrinsicID() == Intrinsic::smax) &&1368 "Must be smin/smax");1369 1370 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());1371 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));1372 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||1373 !match(II->getArgOperand(1), m_APInt(CLow)) ||1374 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))1375 return false;1376 1377 if (II->getIntrinsicID() == Intrinsic::smin)1378 std::swap(CLow, CHigh);1379 return CLow->sle(*CHigh);1380}1381 1382static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II,1383 KnownBits &Known) {1384 const APInt *CLow, *CHigh;1385 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))1386 Known = Known.unionWith(1387 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());1388}1389 1390static void computeKnownBitsFromOperator(const Operator *I,1391 const APInt &DemandedElts,1392 KnownBits &Known,1393 const SimplifyQuery &Q,1394 unsigned Depth) {1395 unsigned BitWidth = Known.getBitWidth();1396 1397 KnownBits Known2(BitWidth);1398 switch (I->getOpcode()) {1399 default: break;1400 case Instruction::Load:1401 if (MDNode *MD =1402 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))1403 computeKnownBitsFromRangeMetadata(*MD, Known);1404 break;1405 case Instruction::And:1406 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);1407 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);1408 1409 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);1410 break;1411 case Instruction::Or:1412 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);1413 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);1414 1415 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);1416 break;1417 case Instruction::Xor:1418 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);1419 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);1420 1421 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);1422 break;1423 case Instruction::Mul: {1424 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));1425 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));1426 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,1427 DemandedElts, Known, Known2, Q, Depth);1428 break;1429 }1430 case Instruction::UDiv: {1431 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1432 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);1433 Known =1434 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));1435 break;1436 }1437 case Instruction::SDiv: {1438 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1439 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);1440 Known =1441 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));1442 break;1443 }1444 case Instruction::Select: {1445 auto ComputeForArm = [&](Value *Arm, bool Invert) {1446 KnownBits Res(Known.getBitWidth());1447 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);1448 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);1449 return Res;1450 };1451 // Only known if known in both the LHS and RHS.1452 Known =1453 ComputeForArm(I->getOperand(1), /*Invert=*/false)1454 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));1455 break;1456 }1457 case Instruction::FPTrunc:1458 case Instruction::FPExt:1459 case Instruction::FPToUI:1460 case Instruction::FPToSI:1461 case Instruction::SIToFP:1462 case Instruction::UIToFP:1463 break; // Can't work with floating point.1464 case Instruction::PtrToInt:1465 case Instruction::IntToPtr:1466 // Fall through and handle them the same as zext/trunc.1467 [[fallthrough]];1468 case Instruction::ZExt:1469 case Instruction::Trunc: {1470 Type *SrcTy = I->getOperand(0)->getType();1471 1472 unsigned SrcBitWidth;1473 // Note that we handle pointer operands here because of inttoptr/ptrtoint1474 // which fall through here.1475 Type *ScalarTy = SrcTy->getScalarType();1476 SrcBitWidth = ScalarTy->isPointerTy() ?1477 Q.DL.getPointerTypeSizeInBits(ScalarTy) :1478 Q.DL.getTypeSizeInBits(ScalarTy);1479 1480 assert(SrcBitWidth && "SrcBitWidth can't be zero");1481 Known = Known.anyextOrTrunc(SrcBitWidth);1482 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1483 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);1484 Inst && Inst->hasNonNeg() && !Known.isNegative())1485 Known.makeNonNegative();1486 Known = Known.zextOrTrunc(BitWidth);1487 break;1488 }1489 case Instruction::BitCast: {1490 Type *SrcTy = I->getOperand(0)->getType();1491 if (SrcTy->isIntOrPtrTy() &&1492 // TODO: For now, not handling conversions like:1493 // (bitcast i64 %x to <2 x i32>)1494 !I->getType()->isVectorTy()) {1495 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);1496 break;1497 }1498 1499 const Value *V;1500 // Handle bitcast from floating point to integer.1501 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&1502 V->getType()->isFPOrFPVectorTy()) {1503 Type *FPType = V->getType()->getScalarType();1504 KnownFPClass Result =1505 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);1506 FPClassTest FPClasses = Result.KnownFPClasses;1507 1508 // TODO: Treat it as zero/poison if the use of I is unreachable.1509 if (FPClasses == fcNone)1510 break;1511 1512 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {1513 Known.setAllConflict();1514 1515 if (FPClasses & fcInf)1516 Known = Known.intersectWith(KnownBits::makeConstant(1517 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));1518 1519 if (FPClasses & fcZero)1520 Known = Known.intersectWith(KnownBits::makeConstant(1521 APInt::getZero(FPType->getScalarSizeInBits())));1522 1523 Known.Zero.clearSignBit();1524 Known.One.clearSignBit();1525 }1526 1527 if (Result.SignBit) {1528 if (*Result.SignBit)1529 Known.makeNegative();1530 else1531 Known.makeNonNegative();1532 }1533 1534 break;1535 }1536 1537 // Handle cast from vector integer type to scalar or vector integer.1538 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);1539 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||1540 !I->getType()->isIntOrIntVectorTy() ||1541 isa<ScalableVectorType>(I->getType()))1542 break;1543 1544 unsigned NumElts = DemandedElts.getBitWidth();1545 bool IsLE = Q.DL.isLittleEndian();1546 // Look through a cast from narrow vector elements to wider type.1547 // Examples: v4i32 -> v2i64, v3i8 -> v241548 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();1549 if (BitWidth % SubBitWidth == 0) {1550 // Known bits are automatically intersected across demanded elements of a1551 // vector. So for example, if a bit is computed as known zero, it must be1552 // zero across all demanded elements of the vector.1553 //1554 // For this bitcast, each demanded element of the output is sub-divided1555 // across a set of smaller vector elements in the source vector. To get1556 // the known bits for an entire element of the output, compute the known1557 // bits for each sub-element sequentially. This is done by shifting the1558 // one-set-bit demanded elements parameter across the sub-elements for1559 // consecutive calls to computeKnownBits. We are using the demanded1560 // elements parameter as a mask operator.1561 //1562 // The known bits of each sub-element are then inserted into place1563 // (dependent on endian) to form the full result of known bits.1564 unsigned SubScale = BitWidth / SubBitWidth;1565 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);1566 for (unsigned i = 0; i != NumElts; ++i) {1567 if (DemandedElts[i])1568 SubDemandedElts.setBit(i * SubScale);1569 }1570 1571 KnownBits KnownSrc(SubBitWidth);1572 for (unsigned i = 0; i != SubScale; ++i) {1573 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,1574 Depth + 1);1575 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;1576 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);1577 }1578 }1579 // Look through a cast from wider vector elements to narrow type.1580 // Examples: v2i64 -> v4i321581 if (SubBitWidth % BitWidth == 0) {1582 unsigned SubScale = SubBitWidth / BitWidth;1583 KnownBits KnownSrc(SubBitWidth);1584 APInt SubDemandedElts =1585 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);1586 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,1587 Depth + 1);1588 1589 Known.setAllConflict();1590 for (unsigned i = 0; i != NumElts; ++i) {1591 if (DemandedElts[i]) {1592 unsigned Shifts = IsLE ? i : NumElts - 1 - i;1593 unsigned Offset = (Shifts % SubScale) * BitWidth;1594 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));1595 if (Known.isUnknown())1596 break;1597 }1598 }1599 }1600 break;1601 }1602 case Instruction::SExt: {1603 // Compute the bits in the result that are not present in the input.1604 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();1605 1606 Known = Known.trunc(SrcBitWidth);1607 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1608 // If the sign bit of the input is known set or clear, then we know the1609 // top bits of the result.1610 Known = Known.sext(BitWidth);1611 break;1612 }1613 case Instruction::Shl: {1614 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));1615 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));1616 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,1617 bool ShAmtNonZero) {1618 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);1619 };1620 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,1621 KF);1622 // Trailing zeros of a right-shifted constant never decrease.1623 const APInt *C;1624 if (match(I->getOperand(0), m_APInt(C)))1625 Known.Zero.setLowBits(C->countr_zero());1626 break;1627 }1628 case Instruction::LShr: {1629 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));1630 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,1631 bool ShAmtNonZero) {1632 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);1633 };1634 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,1635 KF);1636 // Leading zeros of a left-shifted constant never decrease.1637 const APInt *C;1638 if (match(I->getOperand(0), m_APInt(C)))1639 Known.Zero.setHighBits(C->countl_zero());1640 break;1641 }1642 case Instruction::AShr: {1643 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));1644 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,1645 bool ShAmtNonZero) {1646 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);1647 };1648 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,1649 KF);1650 break;1651 }1652 case Instruction::Sub: {1653 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));1654 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));1655 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,1656 DemandedElts, Known, Known2, Q, Depth);1657 break;1658 }1659 case Instruction::Add: {1660 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));1661 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));1662 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,1663 DemandedElts, Known, Known2, Q, Depth);1664 break;1665 }1666 case Instruction::SRem:1667 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1668 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);1669 Known = KnownBits::srem(Known, Known2);1670 break;1671 1672 case Instruction::URem:1673 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);1674 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);1675 Known = KnownBits::urem(Known, Known2);1676 break;1677 case Instruction::Alloca:1678 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));1679 break;1680 case Instruction::GetElementPtr: {1681 // Analyze all of the subscripts of this getelementptr instruction1682 // to determine if we can prove known low zero bits.1683 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);1684 // Accumulate the constant indices in a separate variable1685 // to minimize the number of calls to computeForAddSub.1686 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());1687 APInt AccConstIndices(IndexWidth, 0);1688 1689 auto AddIndexToKnown = [&](KnownBits IndexBits) {1690 if (IndexWidth == BitWidth) {1691 // Note that inbounds does *not* guarantee nsw for the addition, as only1692 // the offset is signed, while the base address is unsigned.1693 Known = KnownBits::add(Known, IndexBits);1694 } else {1695 // If the index width is smaller than the pointer width, only add the1696 // value to the low bits.1697 assert(IndexWidth < BitWidth &&1698 "Index width can't be larger than pointer width");1699 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);1700 }1701 };1702 1703 gep_type_iterator GTI = gep_type_begin(I);1704 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {1705 // TrailZ can only become smaller, short-circuit if we hit zero.1706 if (Known.isUnknown())1707 break;1708 1709 Value *Index = I->getOperand(i);1710 1711 // Handle case when index is zero.1712 Constant *CIndex = dyn_cast<Constant>(Index);1713 if (CIndex && CIndex->isZeroValue())1714 continue;1715 1716 if (StructType *STy = GTI.getStructTypeOrNull()) {1717 // Handle struct member offset arithmetic.1718 1719 assert(CIndex &&1720 "Access to structure field must be known at compile time");1721 1722 if (CIndex->getType()->isVectorTy())1723 Index = CIndex->getSplatValue();1724 1725 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();1726 const StructLayout *SL = Q.DL.getStructLayout(STy);1727 uint64_t Offset = SL->getElementOffset(Idx);1728 AccConstIndices += Offset;1729 continue;1730 }1731 1732 // Handle array index arithmetic.1733 Type *IndexedTy = GTI.getIndexedType();1734 if (!IndexedTy->isSized()) {1735 Known.resetAll();1736 break;1737 }1738 1739 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);1740 uint64_t StrideInBytes = Stride.getKnownMinValue();1741 if (!Stride.isScalable()) {1742 // Fast path for constant offset.1743 if (auto *CI = dyn_cast<ConstantInt>(Index)) {1744 AccConstIndices +=1745 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;1746 continue;1747 }1748 }1749 1750 KnownBits IndexBits =1751 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);1752 KnownBits ScalingFactor(IndexWidth);1753 // Multiply by current sizeof type.1754 // &A[i] == A + i * sizeof(*A[i]).1755 if (Stride.isScalable()) {1756 // For scalable types the only thing we know about sizeof is1757 // that this is a multiple of the minimum size.1758 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));1759 } else {1760 ScalingFactor =1761 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));1762 }1763 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));1764 }1765 if (!Known.isUnknown() && !AccConstIndices.isZero())1766 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));1767 break;1768 }1769 case Instruction::PHI: {1770 const PHINode *P = cast<PHINode>(I);1771 BinaryOperator *BO = nullptr;1772 Value *R = nullptr, *L = nullptr;1773 if (matchSimpleRecurrence(P, BO, R, L)) {1774 // Handle the case of a simple two-predecessor recurrence PHI.1775 // There's a lot more that could theoretically be done here, but1776 // this is sufficient to catch some interesting cases.1777 unsigned Opcode = BO->getOpcode();1778 1779 switch (Opcode) {1780 // If this is a shift recurrence, we know the bits being shifted in. We1781 // can combine that with information about the start value of the1782 // recurrence to conclude facts about the result. If this is a udiv1783 // recurrence, we know that the result can never exceed either the1784 // numerator or the start value, whichever is greater.1785 case Instruction::LShr:1786 case Instruction::AShr:1787 case Instruction::Shl:1788 case Instruction::UDiv:1789 if (BO->getOperand(0) != I)1790 break;1791 [[fallthrough]];1792 1793 // For a urem recurrence, the result can never exceed the start value. The1794 // phi could either be the numerator or the denominator.1795 case Instruction::URem: {1796 // We have matched a recurrence of the form:1797 // %iv = [R, %entry], [%iv.next, %backedge]1798 // %iv.next = shift_op %iv, L1799 1800 // Recurse with the phi context to avoid concern about whether facts1801 // inferred hold at original context instruction. TODO: It may be1802 // correct to use the original context. IF warranted, explore and1803 // add sufficient tests to cover.1804 SimplifyQuery RecQ = Q.getWithoutCondContext();1805 RecQ.CxtI = P;1806 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);1807 switch (Opcode) {1808 case Instruction::Shl:1809 // A shl recurrence will only increase the tailing zeros1810 Known.Zero.setLowBits(Known2.countMinTrailingZeros());1811 break;1812 case Instruction::LShr:1813 case Instruction::UDiv:1814 case Instruction::URem:1815 // lshr, udiv, and urem recurrences will preserve the leading zeros of1816 // the start value.1817 Known.Zero.setHighBits(Known2.countMinLeadingZeros());1818 break;1819 case Instruction::AShr:1820 // An ashr recurrence will extend the initial sign bit1821 Known.Zero.setHighBits(Known2.countMinLeadingZeros());1822 Known.One.setHighBits(Known2.countMinLeadingOnes());1823 break;1824 }1825 break;1826 }1827 1828 // Check for operations that have the property that if1829 // both their operands have low zero bits, the result1830 // will have low zero bits.1831 case Instruction::Add:1832 case Instruction::Sub:1833 case Instruction::And:1834 case Instruction::Or:1835 case Instruction::Mul: {1836 // Change the context instruction to the "edge" that flows into the1837 // phi. This is important because that is where the value is actually1838 // "evaluated" even though it is used later somewhere else. (see also1839 // D69571).1840 SimplifyQuery RecQ = Q.getWithoutCondContext();1841 1842 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;1843 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();1844 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();1845 1846 // Ok, we have a PHI of the form L op= R. Check for low1847 // zero bits.1848 RecQ.CxtI = RInst;1849 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);1850 1851 // We need to take the minimum number of known bits1852 KnownBits Known3(BitWidth);1853 RecQ.CxtI = LInst;1854 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);1855 1856 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),1857 Known3.countMinTrailingZeros()));1858 1859 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);1860 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))1861 break;1862 1863 switch (Opcode) {1864 // If initial value of recurrence is nonnegative, and we are adding1865 // a nonnegative number with nsw, the result can only be nonnegative1866 // or poison value regardless of the number of times we execute the1867 // add in phi recurrence. If initial value is negative and we are1868 // adding a negative number with nsw, the result can only be1869 // negative or poison value. Similar arguments apply to sub and mul.1870 //1871 // (add non-negative, non-negative) --> non-negative1872 // (add negative, negative) --> negative1873 case Instruction::Add: {1874 if (Known2.isNonNegative() && Known3.isNonNegative())1875 Known.makeNonNegative();1876 else if (Known2.isNegative() && Known3.isNegative())1877 Known.makeNegative();1878 break;1879 }1880 1881 // (sub nsw non-negative, negative) --> non-negative1882 // (sub nsw negative, non-negative) --> negative1883 case Instruction::Sub: {1884 if (BO->getOperand(0) != I)1885 break;1886 if (Known2.isNonNegative() && Known3.isNegative())1887 Known.makeNonNegative();1888 else if (Known2.isNegative() && Known3.isNonNegative())1889 Known.makeNegative();1890 break;1891 }1892 1893 // (mul nsw non-negative, non-negative) --> non-negative1894 case Instruction::Mul:1895 if (Known2.isNonNegative() && Known3.isNonNegative())1896 Known.makeNonNegative();1897 break;1898 1899 default:1900 break;1901 }1902 break;1903 }1904 1905 default:1906 break;1907 }1908 }1909 1910 // Unreachable blocks may have zero-operand PHI nodes.1911 if (P->getNumIncomingValues() == 0)1912 break;1913 1914 // Otherwise take the unions of the known bit sets of the operands,1915 // taking conservative care to avoid excessive recursion.1916 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {1917 // Skip if every incoming value references to ourself.1918 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))1919 break;1920 1921 Known.setAllConflict();1922 for (const Use &U : P->operands()) {1923 Value *IncValue;1924 const PHINode *CxtPhi;1925 Instruction *CxtI;1926 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);1927 // Skip direct self references.1928 if (IncValue == P)1929 continue;1930 1931 // Change the context instruction to the "edge" that flows into the1932 // phi. This is important because that is where the value is actually1933 // "evaluated" even though it is used later somewhere else. (see also1934 // D69571).1935 SimplifyQuery RecQ = Q.getWithoutCondContext().getWithInstruction(CxtI);1936 1937 Known2 = KnownBits(BitWidth);1938 1939 // Recurse, but cap the recursion to one level, because we don't1940 // want to waste time spinning around in loops.1941 // TODO: See if we can base recursion limiter on number of incoming phi1942 // edges so we don't overly clamp analysis.1943 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,1944 MaxAnalysisRecursionDepth - 1);1945 1946 // See if we can further use a conditional branch into the phi1947 // to help us determine the range of the value.1948 if (!Known2.isConstant()) {1949 CmpPredicate Pred;1950 const APInt *RHSC;1951 BasicBlock *TrueSucc, *FalseSucc;1952 // TODO: Use RHS Value and compute range from its known bits.1953 if (match(RecQ.CxtI,1954 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),1955 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {1956 // Check for cases of duplicate successors.1957 if ((TrueSucc == CxtPhi->getParent()) !=1958 (FalseSucc == CxtPhi->getParent())) {1959 // If we're using the false successor, invert the predicate.1960 if (FalseSucc == CxtPhi->getParent())1961 Pred = CmpInst::getInversePredicate(Pred);1962 // Get the knownbits implied by the incoming phi condition.1963 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);1964 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());1965 // We can have conflicts here if we are analyzing deadcode (its1966 // impossible for us reach this BB based the icmp).1967 if (KnownUnion.hasConflict()) {1968 // No reason to continue analyzing in a known dead region, so1969 // just resetAll and break. This will cause us to also exit the1970 // outer loop.1971 Known.resetAll();1972 break;1973 }1974 Known2 = KnownUnion;1975 }1976 }1977 }1978 1979 Known = Known.intersectWith(Known2);1980 // If all bits have been ruled out, there's no need to check1981 // more operands.1982 if (Known.isUnknown())1983 break;1984 }1985 }1986 break;1987 }1988 case Instruction::Call:1989 case Instruction::Invoke: {1990 // If range metadata is attached to this call, set known bits from that,1991 // and then intersect with known bits based on other properties of the1992 // function.1993 if (MDNode *MD =1994 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))1995 computeKnownBitsFromRangeMetadata(*MD, Known);1996 1997 const auto *CB = cast<CallBase>(I);1998 1999 if (std::optional<ConstantRange> Range = CB->getRange())2000 Known = Known.unionWith(Range->toKnownBits());2001 2002 if (const Value *RV = CB->getReturnedArgOperand()) {2003 if (RV->getType() == I->getType()) {2004 computeKnownBits(RV, Known2, Q, Depth + 1);2005 Known = Known.unionWith(Known2);2006 // If the function doesn't return properly for all input values2007 // (e.g. unreachable exits) then there might be conflicts between the2008 // argument value and the range metadata. Simply discard the known bits2009 // in case of conflicts.2010 if (Known.hasConflict())2011 Known.resetAll();2012 }2013 }2014 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {2015 switch (II->getIntrinsicID()) {2016 default:2017 break;2018 case Intrinsic::abs: {2019 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2020 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());2021 Known = Known.unionWith(Known2.abs(IntMinIsPoison));2022 break;2023 }2024 case Intrinsic::bitreverse:2025 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2026 Known = Known.unionWith(Known2.reverseBits());2027 break;2028 case Intrinsic::bswap:2029 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2030 Known = Known.unionWith(Known2.byteSwap());2031 break;2032 case Intrinsic::ctlz: {2033 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2034 // If we have a known 1, its position is our upper bound.2035 unsigned PossibleLZ = Known2.countMaxLeadingZeros();2036 // If this call is poison for 0 input, the result will be less than 2^n.2037 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))2038 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);2039 unsigned LowBits = llvm::bit_width(PossibleLZ);2040 Known.Zero.setBitsFrom(LowBits);2041 break;2042 }2043 case Intrinsic::cttz: {2044 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2045 // If we have a known 1, its position is our upper bound.2046 unsigned PossibleTZ = Known2.countMaxTrailingZeros();2047 // If this call is poison for 0 input, the result will be less than 2^n.2048 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))2049 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);2050 unsigned LowBits = llvm::bit_width(PossibleTZ);2051 Known.Zero.setBitsFrom(LowBits);2052 break;2053 }2054 case Intrinsic::ctpop: {2055 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2056 // We can bound the space the count needs. Also, bits known to be zero2057 // can't contribute to the population.2058 unsigned BitsPossiblySet = Known2.countMaxPopulation();2059 unsigned LowBits = llvm::bit_width(BitsPossiblySet);2060 Known.Zero.setBitsFrom(LowBits);2061 // TODO: we could bound KnownOne using the lower bound on the number2062 // of bits which might be set provided by popcnt KnownOne2.2063 break;2064 }2065 case Intrinsic::fshr:2066 case Intrinsic::fshl: {2067 const APInt *SA;2068 if (!match(I->getOperand(2), m_APInt(SA)))2069 break;2070 2071 // Normalize to funnel shift left.2072 uint64_t ShiftAmt = SA->urem(BitWidth);2073 if (II->getIntrinsicID() == Intrinsic::fshr)2074 ShiftAmt = BitWidth - ShiftAmt;2075 2076 KnownBits Known3(BitWidth);2077 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);2078 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);2079 2080 Known2 <<= ShiftAmt;2081 Known3 >>= BitWidth - ShiftAmt;2082 Known = Known2.unionWith(Known3);2083 break;2084 }2085 case Intrinsic::uadd_sat:2086 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2087 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2088 Known = KnownBits::uadd_sat(Known, Known2);2089 break;2090 case Intrinsic::usub_sat:2091 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2092 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2093 Known = KnownBits::usub_sat(Known, Known2);2094 break;2095 case Intrinsic::sadd_sat:2096 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2097 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2098 Known = KnownBits::sadd_sat(Known, Known2);2099 break;2100 case Intrinsic::ssub_sat:2101 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2102 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2103 Known = KnownBits::ssub_sat(Known, Known2);2104 break;2105 // Vec reverse preserves bits from input vec.2106 case Intrinsic::vector_reverse:2107 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,2108 Depth + 1);2109 break;2110 // for min/max/and/or reduce, any bit common to each element in the2111 // input vec is set in the output.2112 case Intrinsic::vector_reduce_and:2113 case Intrinsic::vector_reduce_or:2114 case Intrinsic::vector_reduce_umax:2115 case Intrinsic::vector_reduce_umin:2116 case Intrinsic::vector_reduce_smax:2117 case Intrinsic::vector_reduce_smin:2118 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);2119 break;2120 case Intrinsic::vector_reduce_xor: {2121 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);2122 // The zeros common to all vecs are zero in the output.2123 // If the number of elements is odd, then the common ones remain. If the2124 // number of elements is even, then the common ones becomes zeros.2125 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());2126 // Even, so the ones become zeros.2127 bool EvenCnt = VecTy->getElementCount().isKnownEven();2128 if (EvenCnt)2129 Known.Zero |= Known.One;2130 // Maybe even element count so need to clear ones.2131 if (VecTy->isScalableTy() || EvenCnt)2132 Known.One.clearAllBits();2133 break;2134 }2135 case Intrinsic::umin:2136 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2137 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2138 Known = KnownBits::umin(Known, Known2);2139 break;2140 case Intrinsic::umax:2141 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2142 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2143 Known = KnownBits::umax(Known, Known2);2144 break;2145 case Intrinsic::smin:2146 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2147 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2148 Known = KnownBits::smin(Known, Known2);2149 unionWithMinMaxIntrinsicClamp(II, Known);2150 break;2151 case Intrinsic::smax:2152 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2153 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2154 Known = KnownBits::smax(Known, Known2);2155 unionWithMinMaxIntrinsicClamp(II, Known);2156 break;2157 case Intrinsic::ptrmask: {2158 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2159 2160 const Value *Mask = I->getOperand(1);2161 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());2162 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);2163 // TODO: 1-extend would be more precise.2164 Known &= Known2.anyextOrTrunc(BitWidth);2165 break;2166 }2167 case Intrinsic::x86_sse2_pmulh_w:2168 case Intrinsic::x86_avx2_pmulh_w:2169 case Intrinsic::x86_avx512_pmulh_w_512:2170 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2171 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2172 Known = KnownBits::mulhs(Known, Known2);2173 break;2174 case Intrinsic::x86_sse2_pmulhu_w:2175 case Intrinsic::x86_avx2_pmulhu_w:2176 case Intrinsic::x86_avx512_pmulhu_w_512:2177 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);2178 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);2179 Known = KnownBits::mulhu(Known, Known2);2180 break;2181 case Intrinsic::x86_sse42_crc32_64_64:2182 Known.Zero.setBitsFrom(32);2183 break;2184 case Intrinsic::x86_ssse3_phadd_d_128:2185 case Intrinsic::x86_ssse3_phadd_w_128:2186 case Intrinsic::x86_avx2_phadd_d:2187 case Intrinsic::x86_avx2_phadd_w: {2188 Known = computeKnownBitsForHorizontalOperation(2189 I, DemandedElts, Q, Depth,2190 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {2191 return KnownBits::add(KnownLHS, KnownRHS);2192 });2193 break;2194 }2195 case Intrinsic::x86_ssse3_phadd_sw_128:2196 case Intrinsic::x86_avx2_phadd_sw: {2197 Known = computeKnownBitsForHorizontalOperation(2198 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);2199 break;2200 }2201 case Intrinsic::x86_ssse3_phsub_d_128:2202 case Intrinsic::x86_ssse3_phsub_w_128:2203 case Intrinsic::x86_avx2_phsub_d:2204 case Intrinsic::x86_avx2_phsub_w: {2205 Known = computeKnownBitsForHorizontalOperation(2206 I, DemandedElts, Q, Depth,2207 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {2208 return KnownBits::sub(KnownLHS, KnownRHS);2209 });2210 break;2211 }2212 case Intrinsic::x86_ssse3_phsub_sw_128:2213 case Intrinsic::x86_avx2_phsub_sw: {2214 Known = computeKnownBitsForHorizontalOperation(2215 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);2216 break;2217 }2218 case Intrinsic::riscv_vsetvli:2219 case Intrinsic::riscv_vsetvlimax: {2220 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;2221 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);2222 uint64_t SEW = RISCVVType::decodeVSEW(2223 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());2224 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(2225 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());2226 uint64_t MaxVLEN =2227 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;2228 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);2229 2230 // Result of vsetvli must be not larger than AVL.2231 if (HasAVL)2232 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))2233 MaxVL = std::min(MaxVL, CI->getZExtValue());2234 2235 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;2236 if (BitWidth > KnownZeroFirstBit)2237 Known.Zero.setBitsFrom(KnownZeroFirstBit);2238 break;2239 }2240 case Intrinsic::vscale: {2241 if (!II->getParent() || !II->getFunction())2242 break;2243 2244 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();2245 break;2246 }2247 }2248 }2249 break;2250 }2251 case Instruction::ShuffleVector: {2252 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);2253 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?2254 if (!Shuf) {2255 Known.resetAll();2256 return;2257 }2258 // For undef elements, we don't know anything about the common state of2259 // the shuffle result.2260 APInt DemandedLHS, DemandedRHS;2261 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {2262 Known.resetAll();2263 return;2264 }2265 Known.setAllConflict();2266 if (!!DemandedLHS) {2267 const Value *LHS = Shuf->getOperand(0);2268 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);2269 // If we don't know any bits, early out.2270 if (Known.isUnknown())2271 break;2272 }2273 if (!!DemandedRHS) {2274 const Value *RHS = Shuf->getOperand(1);2275 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);2276 Known = Known.intersectWith(Known2);2277 }2278 break;2279 }2280 case Instruction::InsertElement: {2281 if (isa<ScalableVectorType>(I->getType())) {2282 Known.resetAll();2283 return;2284 }2285 const Value *Vec = I->getOperand(0);2286 const Value *Elt = I->getOperand(1);2287 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));2288 unsigned NumElts = DemandedElts.getBitWidth();2289 APInt DemandedVecElts = DemandedElts;2290 bool NeedsElt = true;2291 // If we know the index we are inserting too, clear it from Vec check.2292 if (CIdx && CIdx->getValue().ult(NumElts)) {2293 DemandedVecElts.clearBit(CIdx->getZExtValue());2294 NeedsElt = DemandedElts[CIdx->getZExtValue()];2295 }2296 2297 Known.setAllConflict();2298 if (NeedsElt) {2299 computeKnownBits(Elt, Known, Q, Depth + 1);2300 // If we don't know any bits, early out.2301 if (Known.isUnknown())2302 break;2303 }2304 2305 if (!DemandedVecElts.isZero()) {2306 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);2307 Known = Known.intersectWith(Known2);2308 }2309 break;2310 }2311 case Instruction::ExtractElement: {2312 // Look through extract element. If the index is non-constant or2313 // out-of-range demand all elements, otherwise just the extracted element.2314 const Value *Vec = I->getOperand(0);2315 const Value *Idx = I->getOperand(1);2316 auto *CIdx = dyn_cast<ConstantInt>(Idx);2317 if (isa<ScalableVectorType>(Vec->getType())) {2318 // FIXME: there's probably *something* we can do with scalable vectors2319 Known.resetAll();2320 break;2321 }2322 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();2323 APInt DemandedVecElts = APInt::getAllOnes(NumElts);2324 if (CIdx && CIdx->getValue().ult(NumElts))2325 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());2326 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);2327 break;2328 }2329 case Instruction::ExtractValue:2330 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {2331 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);2332 if (EVI->getNumIndices() != 1) break;2333 if (EVI->getIndices()[0] == 0) {2334 switch (II->getIntrinsicID()) {2335 default: break;2336 case Intrinsic::uadd_with_overflow:2337 case Intrinsic::sadd_with_overflow:2338 computeKnownBitsAddSub(2339 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,2340 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);2341 break;2342 case Intrinsic::usub_with_overflow:2343 case Intrinsic::ssub_with_overflow:2344 computeKnownBitsAddSub(2345 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,2346 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);2347 break;2348 case Intrinsic::umul_with_overflow:2349 case Intrinsic::smul_with_overflow:2350 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,2351 false, DemandedElts, Known, Known2, Q, Depth);2352 break;2353 }2354 }2355 }2356 break;2357 case Instruction::Freeze:2358 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,2359 Depth + 1))2360 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);2361 break;2362 }2363}2364 2365/// Determine which bits of V are known to be either zero or one and return2366/// them.2367KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,2368 const SimplifyQuery &Q, unsigned Depth) {2369 KnownBits Known(getBitWidth(V->getType(), Q.DL));2370 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);2371 return Known;2372}2373 2374/// Determine which bits of V are known to be either zero or one and return2375/// them.2376KnownBits llvm::computeKnownBits(const Value *V, const SimplifyQuery &Q,2377 unsigned Depth) {2378 KnownBits Known(getBitWidth(V->getType(), Q.DL));2379 computeKnownBits(V, Known, Q, Depth);2380 return Known;2381}2382 2383/// Determine which bits of V are known to be either zero or one and return2384/// them in the Known bit set.2385///2386/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that2387/// we cannot optimize based on the assumption that it is zero without changing2388/// it to be an explicit zero. If we don't change it to zero, other code could2389/// optimized based on the contradictory assumption that it is non-zero.2390/// Because instcombine aggressively folds operations with undef args anyway,2391/// this won't lose us code quality.2392///2393/// This function is defined on values with integer type, values with pointer2394/// type, and vectors of integers. In the case2395/// where V is a vector, known zero, and known one values are the2396/// same width as the vector element, and the bit is set only if it is true2397/// for all of the demanded elements in the vector specified by DemandedElts.2398void computeKnownBits(const Value *V, const APInt &DemandedElts,2399 KnownBits &Known, const SimplifyQuery &Q,2400 unsigned Depth) {2401 if (!DemandedElts) {2402 // No demanded elts, better to assume we don't know anything.2403 Known.resetAll();2404 return;2405 }2406 2407 assert(V && "No Value?");2408 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");2409 2410#ifndef NDEBUG2411 Type *Ty = V->getType();2412 unsigned BitWidth = Known.getBitWidth();2413 2414 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&2415 "Not integer or pointer type!");2416 2417 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {2418 assert(2419 FVTy->getNumElements() == DemandedElts.getBitWidth() &&2420 "DemandedElt width should equal the fixed vector number of elements");2421 } else {2422 assert(DemandedElts == APInt(1, 1) &&2423 "DemandedElt width should be 1 for scalars or scalable vectors");2424 }2425 2426 Type *ScalarTy = Ty->getScalarType();2427 if (ScalarTy->isPointerTy()) {2428 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&2429 "V and Known should have same BitWidth");2430 } else {2431 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&2432 "V and Known should have same BitWidth");2433 }2434#endif2435 2436 const APInt *C;2437 if (match(V, m_APInt(C))) {2438 // We know all of the bits for a scalar constant or a splat vector constant!2439 Known = KnownBits::makeConstant(*C);2440 return;2441 }2442 // Null and aggregate-zero are all-zeros.2443 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {2444 Known.setAllZero();2445 return;2446 }2447 // Handle a constant vector by taking the intersection of the known bits of2448 // each element.2449 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {2450 assert(!isa<ScalableVectorType>(V->getType()));2451 // We know that CDV must be a vector of integers. Take the intersection of2452 // each element.2453 Known.setAllConflict();2454 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {2455 if (!DemandedElts[i])2456 continue;2457 APInt Elt = CDV->getElementAsAPInt(i);2458 Known.Zero &= ~Elt;2459 Known.One &= Elt;2460 }2461 if (Known.hasConflict())2462 Known.resetAll();2463 return;2464 }2465 2466 if (const auto *CV = dyn_cast<ConstantVector>(V)) {2467 assert(!isa<ScalableVectorType>(V->getType()));2468 // We know that CV must be a vector of integers. Take the intersection of2469 // each element.2470 Known.setAllConflict();2471 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {2472 if (!DemandedElts[i])2473 continue;2474 Constant *Element = CV->getAggregateElement(i);2475 if (isa<PoisonValue>(Element))2476 continue;2477 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);2478 if (!ElementCI) {2479 Known.resetAll();2480 return;2481 }2482 const APInt &Elt = ElementCI->getValue();2483 Known.Zero &= ~Elt;2484 Known.One &= Elt;2485 }2486 if (Known.hasConflict())2487 Known.resetAll();2488 return;2489 }2490 2491 // Start out not knowing anything.2492 Known.resetAll();2493 2494 // We can't imply anything about undefs.2495 if (isa<UndefValue>(V))2496 return;2497 2498 // There's no point in looking through other users of ConstantData for2499 // assumptions. Confirm that we've handled them all.2500 assert(!isa<ConstantData>(V) && "Unhandled constant data!");2501 2502 if (const auto *A = dyn_cast<Argument>(V))2503 if (std::optional<ConstantRange> Range = A->getRange())2504 Known = Range->toKnownBits();2505 2506 // All recursive calls that increase depth must come after this.2507 if (Depth == MaxAnalysisRecursionDepth)2508 return;2509 2510 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has2511 // the bits of its aliasee.2512 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {2513 if (!GA->isInterposable())2514 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);2515 return;2516 }2517 2518 if (const Operator *I = dyn_cast<Operator>(V))2519 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);2520 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {2521 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())2522 Known = CR->toKnownBits();2523 }2524 2525 // Aligned pointers have trailing zeros - refine Known.Zero set2526 if (isa<PointerType>(V->getType())) {2527 Align Alignment = V->getPointerAlignment(Q.DL);2528 Known.Zero.setLowBits(Log2(Alignment));2529 }2530 2531 // computeKnownBitsFromContext strictly refines Known.2532 // Therefore, we run them after computeKnownBitsFromOperator.2533 2534 // Check whether we can determine known bits from context such as assumes.2535 computeKnownBitsFromContext(V, Known, Q, Depth);2536}2537 2538/// Try to detect a recurrence that the value of the induction variable is2539/// always a power of two (or zero).2540static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,2541 SimplifyQuery &Q, unsigned Depth) {2542 BinaryOperator *BO = nullptr;2543 Value *Start = nullptr, *Step = nullptr;2544 if (!matchSimpleRecurrence(PN, BO, Start, Step))2545 return false;2546 2547 // Initial value must be a power of two.2548 for (const Use &U : PN->operands()) {2549 if (U.get() == Start) {2550 // Initial value comes from a different BB, need to adjust context2551 // instruction for analysis.2552 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();2553 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))2554 return false;2555 }2556 }2557 2558 // Except for Mul, the induction variable must be on the left side of the2559 // increment expression, otherwise its value can be arbitrary.2560 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)2561 return false;2562 2563 Q.CxtI = BO->getParent()->getTerminator();2564 switch (BO->getOpcode()) {2565 case Instruction::Mul:2566 // Power of two is closed under multiplication.2567 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||2568 Q.IIQ.hasNoSignedWrap(BO)) &&2569 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);2570 case Instruction::SDiv:2571 // Start value must not be signmask for signed division, so simply being a2572 // power of two is not sufficient, and it has to be a constant.2573 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))2574 return false;2575 [[fallthrough]];2576 case Instruction::UDiv:2577 // Divisor must be a power of two.2578 // If OrZero is false, cannot guarantee induction variable is non-zero after2579 // division, same for Shr, unless it is exact division.2580 return (OrZero || Q.IIQ.isExact(BO)) &&2581 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);2582 case Instruction::Shl:2583 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);2584 case Instruction::AShr:2585 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))2586 return false;2587 [[fallthrough]];2588 case Instruction::LShr:2589 return OrZero || Q.IIQ.isExact(BO);2590 default:2591 return false;2592 }2593}2594 2595/// Return true if we can infer that \p V is known to be a power of 2 from2596/// dominating condition \p Cond (e.g., ctpop(V) == 1).2597static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,2598 const Value *Cond,2599 bool CondIsTrue) {2600 CmpPredicate Pred;2601 const APInt *RHSC;2602 if (!match(Cond, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),2603 m_APInt(RHSC))))2604 return false;2605 if (!CondIsTrue)2606 Pred = ICmpInst::getInversePredicate(Pred);2607 // ctpop(V) u< 22608 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)2609 return true;2610 // ctpop(V) == 12611 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;2612}2613 2614/// Return true if the given value is known to have exactly one2615/// bit set when defined. For vectors return true if every element is known to2616/// be a power of two when defined. Supports values with integer or pointer2617/// types and vectors of integers.2618bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,2619 const SimplifyQuery &Q, unsigned Depth) {2620 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");2621 2622 if (isa<Constant>(V))2623 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());2624 2625 // i1 is by definition a power of 2 or zero.2626 if (OrZero && V->getType()->getScalarSizeInBits() == 1)2627 return true;2628 2629 // Try to infer from assumptions.2630 if (Q.AC && Q.CxtI) {2631 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {2632 if (!AssumeVH)2633 continue;2634 CallInst *I = cast<CallInst>(AssumeVH);2635 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),2636 /*CondIsTrue=*/true) &&2637 isValidAssumeForContext(I, Q.CxtI, Q.DT))2638 return true;2639 }2640 }2641 2642 // Handle dominating conditions.2643 if (Q.DC && Q.CxtI && Q.DT) {2644 for (BranchInst *BI : Q.DC->conditionsFor(V)) {2645 Value *Cond = BI->getCondition();2646 2647 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));2648 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, Cond,2649 /*CondIsTrue=*/true) &&2650 Q.DT->dominates(Edge0, Q.CxtI->getParent()))2651 return true;2652 2653 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));2654 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, Cond,2655 /*CondIsTrue=*/false) &&2656 Q.DT->dominates(Edge1, Q.CxtI->getParent()))2657 return true;2658 }2659 }2660 2661 auto *I = dyn_cast<Instruction>(V);2662 if (!I)2663 return false;2664 2665 if (Q.CxtI && match(V, m_VScale())) {2666 const Function *F = Q.CxtI->getFunction();2667 // The vscale_range indicates vscale is a power-of-two.2668 return F->hasFnAttribute(Attribute::VScaleRange);2669 }2670 2671 // 1 << X is clearly a power of two if the one is not shifted off the end. If2672 // it is shifted off the end then the result is undefined.2673 if (match(I, m_Shl(m_One(), m_Value())))2674 return true;2675 2676 // (signmask) >>l X is clearly a power of two if the one is not shifted off2677 // the bottom. If it is shifted off the bottom then the result is undefined.2678 if (match(I, m_LShr(m_SignMask(), m_Value())))2679 return true;2680 2681 // The remaining tests are all recursive, so bail out if we hit the limit.2682 if (Depth++ == MaxAnalysisRecursionDepth)2683 return false;2684 2685 switch (I->getOpcode()) {2686 case Instruction::ZExt:2687 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);2688 case Instruction::Trunc:2689 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);2690 case Instruction::Shl:2691 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))2692 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);2693 return false;2694 case Instruction::LShr:2695 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))2696 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);2697 return false;2698 case Instruction::UDiv:2699 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))2700 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);2701 return false;2702 case Instruction::Mul:2703 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&2704 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&2705 (OrZero || isKnownNonZero(I, Q, Depth));2706 case Instruction::And:2707 // A power of two and'd with anything is a power of two or zero.2708 if (OrZero &&2709 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||2710 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))2711 return true;2712 // X & (-X) is always a power of two or zero.2713 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||2714 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))2715 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);2716 return false;2717 case Instruction::Add: {2718 // Adding a power-of-two or zero to the same power-of-two or zero yields2719 // either the original power-of-two, a larger power-of-two or zero.2720 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);2721 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||2722 Q.IIQ.hasNoSignedWrap(VOBO)) {2723 if (match(I->getOperand(0),2724 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&2725 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))2726 return true;2727 if (match(I->getOperand(1),2728 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&2729 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))2730 return true;2731 2732 unsigned BitWidth = V->getType()->getScalarSizeInBits();2733 KnownBits LHSBits(BitWidth);2734 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);2735 2736 KnownBits RHSBits(BitWidth);2737 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);2738 // If i8 V is a power of two or zero:2739 // ZeroBits: 1 1 1 0 1 1 1 12740 // ~ZeroBits: 0 0 0 1 0 0 0 02741 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())2742 // If OrZero isn't set, we cannot give back a zero result.2743 // Make sure either the LHS or RHS has a bit set.2744 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())2745 return true;2746 }2747 2748 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.2749 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))2750 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))2751 return true;2752 return false;2753 }2754 case Instruction::Select:2755 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&2756 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);2757 case Instruction::PHI: {2758 // A PHI node is power of two if all incoming values are power of two, or if2759 // it is an induction variable where in each step its value is a power of2760 // two.2761 auto *PN = cast<PHINode>(I);2762 SimplifyQuery RecQ = Q.getWithoutCondContext();2763 2764 // Check if it is an induction variable and always power of two.2765 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))2766 return true;2767 2768 // Recursively check all incoming values. Limit recursion to 2 levels, so2769 // that search complexity is limited to number of operands^2.2770 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);2771 return llvm::all_of(PN->operands(), [&](const Use &U) {2772 // Value is power of 2 if it is coming from PHI node itself by induction.2773 if (U.get() == PN)2774 return true;2775 2776 // Change the context instruction to the incoming block where it is2777 // evaluated.2778 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();2779 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);2780 });2781 }2782 case Instruction::Invoke:2783 case Instruction::Call: {2784 if (auto *II = dyn_cast<IntrinsicInst>(I)) {2785 switch (II->getIntrinsicID()) {2786 case Intrinsic::umax:2787 case Intrinsic::smax:2788 case Intrinsic::umin:2789 case Intrinsic::smin:2790 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&2791 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);2792 // bswap/bitreverse just move around bits, but don't change any 1s/0s2793 // thus dont change pow2/non-pow2 status.2794 case Intrinsic::bitreverse:2795 case Intrinsic::bswap:2796 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);2797 case Intrinsic::fshr:2798 case Intrinsic::fshl:2799 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)2800 if (II->getArgOperand(0) == II->getArgOperand(1))2801 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);2802 break;2803 default:2804 break;2805 }2806 }2807 return false;2808 }2809 default:2810 return false;2811 }2812}2813 2814/// Test whether a GEP's result is known to be non-null.2815///2816/// Uses properties inherent in a GEP to try to determine whether it is known2817/// to be non-null.2818///2819/// Currently this routine does not support vector GEPs.2820static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,2821 unsigned Depth) {2822 const Function *F = nullptr;2823 if (const Instruction *I = dyn_cast<Instruction>(GEP))2824 F = I->getFunction();2825 2826 // If the gep is nuw or inbounds with invalid null pointer, then the GEP2827 // may be null iff the base pointer is null and the offset is zero.2828 if (!GEP->hasNoUnsignedWrap() &&2829 !(GEP->isInBounds() &&2830 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))2831 return false;2832 2833 // FIXME: Support vector-GEPs.2834 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");2835 2836 // If the base pointer is non-null, we cannot walk to a null address with an2837 // inbounds GEP in address space zero.2838 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))2839 return true;2840 2841 // Walk the GEP operands and see if any operand introduces a non-zero offset.2842 // If so, then the GEP cannot produce a null pointer, as doing so would2843 // inherently violate the inbounds contract within address space zero.2844 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);2845 GTI != GTE; ++GTI) {2846 // Struct types are easy -- they must always be indexed by a constant.2847 if (StructType *STy = GTI.getStructTypeOrNull()) {2848 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());2849 unsigned ElementIdx = OpC->getZExtValue();2850 const StructLayout *SL = Q.DL.getStructLayout(STy);2851 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);2852 if (ElementOffset > 0)2853 return true;2854 continue;2855 }2856 2857 // If we have a zero-sized type, the index doesn't matter. Keep looping.2858 if (GTI.getSequentialElementStride(Q.DL).isZero())2859 continue;2860 2861 // Fast path the constant operand case both for efficiency and so we don't2862 // increment Depth when just zipping down an all-constant GEP.2863 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {2864 if (!OpC->isZero())2865 return true;2866 continue;2867 }2868 2869 // We post-increment Depth here because while isKnownNonZero increments it2870 // as well, when we pop back up that increment won't persist. We don't want2871 // to recurse 10k times just because we have 10k GEP operands. We don't2872 // bail completely out because we want to handle constant GEPs regardless2873 // of depth.2874 if (Depth++ >= MaxAnalysisRecursionDepth)2875 continue;2876 2877 if (isKnownNonZero(GTI.getOperand(), Q, Depth))2878 return true;2879 }2880 2881 return false;2882}2883 2884static bool isKnownNonNullFromDominatingCondition(const Value *V,2885 const Instruction *CtxI,2886 const DominatorTree *DT) {2887 assert(!isa<Constant>(V) && "Called for constant?");2888 2889 if (!CtxI || !DT)2890 return false;2891 2892 unsigned NumUsesExplored = 0;2893 for (auto &U : V->uses()) {2894 // Avoid massive lists2895 if (NumUsesExplored >= DomConditionsMaxUses)2896 break;2897 NumUsesExplored++;2898 2899 const Instruction *UI = cast<Instruction>(U.getUser());2900 // If the value is used as an argument to a call or invoke, then argument2901 // attributes may provide an answer about null-ness.2902 if (V->getType()->isPointerTy()) {2903 if (const auto *CB = dyn_cast<CallBase>(UI)) {2904 if (CB->isArgOperand(&U) &&2905 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),2906 /*AllowUndefOrPoison=*/false) &&2907 DT->dominates(CB, CtxI))2908 return true;2909 }2910 }2911 2912 // If the value is used as a load/store, then the pointer must be non null.2913 if (V == getLoadStorePointerOperand(UI)) {2914 if (!NullPointerIsDefined(UI->getFunction(),2915 V->getType()->getPointerAddressSpace()) &&2916 DT->dominates(UI, CtxI))2917 return true;2918 }2919 2920 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||2921 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&2922 isValidAssumeForContext(UI, CtxI, DT))2923 return true;2924 2925 // Consider only compare instructions uniquely controlling a branch2926 Value *RHS;2927 CmpPredicate Pred;2928 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))2929 continue;2930 2931 bool NonNullIfTrue;2932 if (cmpExcludesZero(Pred, RHS))2933 NonNullIfTrue = true;2934 else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS))2935 NonNullIfTrue = false;2936 else2937 continue;2938 2939 SmallVector<const User *, 4> WorkList;2940 SmallPtrSet<const User *, 4> Visited;2941 for (const auto *CmpU : UI->users()) {2942 assert(WorkList.empty() && "Should be!");2943 if (Visited.insert(CmpU).second)2944 WorkList.push_back(CmpU);2945 2946 while (!WorkList.empty()) {2947 auto *Curr = WorkList.pop_back_val();2948 2949 // If a user is an AND, add all its users to the work list. We only2950 // propagate "pred != null" condition through AND because it is only2951 // correct to assume that all conditions of AND are met in true branch.2952 // TODO: Support similar logic of OR and EQ predicate?2953 if (NonNullIfTrue)2954 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {2955 for (const auto *CurrU : Curr->users())2956 if (Visited.insert(CurrU).second)2957 WorkList.push_back(CurrU);2958 continue;2959 }2960 2961 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {2962 assert(BI->isConditional() && "uses a comparison!");2963 2964 BasicBlock *NonNullSuccessor =2965 BI->getSuccessor(NonNullIfTrue ? 0 : 1);2966 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);2967 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))2968 return true;2969 } else if (NonNullIfTrue && isGuard(Curr) &&2970 DT->dominates(cast<Instruction>(Curr), CtxI)) {2971 return true;2972 }2973 }2974 }2975 }2976 2977 return false;2978}2979 2980/// Does the 'Range' metadata (which must be a valid MD_range operand list)2981/// ensure that the value it's attached to is never Value? 'RangeType' is2982/// is the type of the value described by the range.2983static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {2984 const unsigned NumRanges = Ranges->getNumOperands() / 2;2985 assert(NumRanges >= 1);2986 for (unsigned i = 0; i < NumRanges; ++i) {2987 ConstantInt *Lower =2988 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));2989 ConstantInt *Upper =2990 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));2991 ConstantRange Range(Lower->getValue(), Upper->getValue());2992 if (Range.contains(Value))2993 return false;2994 }2995 return true;2996}2997 2998/// Try to detect a recurrence that monotonically increases/decreases from a2999/// non-zero starting value. These are common as induction variables.3000static bool isNonZeroRecurrence(const PHINode *PN) {3001 BinaryOperator *BO = nullptr;3002 Value *Start = nullptr, *Step = nullptr;3003 const APInt *StartC, *StepC;3004 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||3005 !match(Start, m_APInt(StartC)) || StartC->isZero())3006 return false;3007 3008 switch (BO->getOpcode()) {3009 case Instruction::Add:3010 // Starting from non-zero and stepping away from zero can never wrap back3011 // to zero.3012 return BO->hasNoUnsignedWrap() ||3013 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&3014 StartC->isNegative() == StepC->isNegative());3015 case Instruction::Mul:3016 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&3017 match(Step, m_APInt(StepC)) && !StepC->isZero();3018 case Instruction::Shl:3019 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();3020 case Instruction::AShr:3021 case Instruction::LShr:3022 return BO->isExact();3023 default:3024 return false;3025 }3026}3027 3028static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {3029 return match(Op0, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,3030 m_Specific(Op1), m_Zero()))) ||3031 match(Op1, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,3032 m_Specific(Op0), m_Zero())));3033}3034 3035static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,3036 unsigned BitWidth, Value *X, Value *Y, bool NSW,3037 bool NUW, unsigned Depth) {3038 // (X + (X != 0)) is non zero3039 if (matchOpWithOpEqZero(X, Y))3040 return true;3041 3042 if (NUW)3043 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||3044 isKnownNonZero(X, DemandedElts, Q, Depth);3045 3046 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);3047 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);3048 3049 // If X and Y are both non-negative (as signed values) then their sum is not3050 // zero unless both X and Y are zero.3051 if (XKnown.isNonNegative() && YKnown.isNonNegative())3052 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||3053 isKnownNonZero(X, DemandedElts, Q, Depth))3054 return true;3055 3056 // If X and Y are both negative (as signed values) then their sum is not3057 // zero unless both X and Y equal INT_MIN.3058 if (XKnown.isNegative() && YKnown.isNegative()) {3059 APInt Mask = APInt::getSignedMaxValue(BitWidth);3060 // The sign bit of X is set. If some other bit is set then X is not equal3061 // to INT_MIN.3062 if (XKnown.One.intersects(Mask))3063 return true;3064 // The sign bit of Y is set. If some other bit is set then Y is not equal3065 // to INT_MIN.3066 if (YKnown.One.intersects(Mask))3067 return true;3068 }3069 3070 // The sum of a non-negative number and a power of two is not zero.3071 if (XKnown.isNonNegative() &&3072 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))3073 return true;3074 if (YKnown.isNonNegative() &&3075 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))3076 return true;3077 3078 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();3079}3080 3081static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,3082 unsigned BitWidth, Value *X, Value *Y,3083 unsigned Depth) {3084 // (X - (X != 0)) is non zero3085 // ((X != 0) - X) is non zero3086 if (matchOpWithOpEqZero(X, Y))3087 return true;3088 3089 // TODO: Move this case into isKnownNonEqual().3090 if (auto *C = dyn_cast<Constant>(X))3091 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))3092 return true;3093 3094 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);3095}3096 3097static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,3098 unsigned BitWidth, Value *X, Value *Y, bool NSW,3099 bool NUW, unsigned Depth) {3100 // If X and Y are non-zero then so is X * Y as long as the multiplication3101 // does not overflow.3102 if (NSW || NUW)3103 return isKnownNonZero(X, DemandedElts, Q, Depth) &&3104 isKnownNonZero(Y, DemandedElts, Q, Depth);3105 3106 // If either X or Y is odd, then if the other is non-zero the result can't3107 // be zero.3108 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);3109 if (XKnown.One[0])3110 return isKnownNonZero(Y, DemandedElts, Q, Depth);3111 3112 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);3113 if (YKnown.One[0])3114 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);3115 3116 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is3117 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking3118 // the lowest known One of X and Y. If they are non-zero, the result3119 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing3120 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.3121 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <3122 BitWidth;3123}3124 3125static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,3126 const SimplifyQuery &Q, const KnownBits &KnownVal,3127 unsigned Depth) {3128 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {3129 switch (I->getOpcode()) {3130 case Instruction::Shl:3131 return Lhs.shl(Rhs);3132 case Instruction::LShr:3133 return Lhs.lshr(Rhs);3134 case Instruction::AShr:3135 return Lhs.ashr(Rhs);3136 default:3137 llvm_unreachable("Unknown Shift Opcode");3138 }3139 };3140 3141 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {3142 switch (I->getOpcode()) {3143 case Instruction::Shl:3144 return Lhs.lshr(Rhs);3145 case Instruction::LShr:3146 case Instruction::AShr:3147 return Lhs.shl(Rhs);3148 default:3149 llvm_unreachable("Unknown Shift Opcode");3150 }3151 };3152 3153 if (KnownVal.isUnknown())3154 return false;3155 3156 KnownBits KnownCnt =3157 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);3158 APInt MaxShift = KnownCnt.getMaxValue();3159 unsigned NumBits = KnownVal.getBitWidth();3160 if (MaxShift.uge(NumBits))3161 return false;3162 3163 if (!ShiftOp(KnownVal.One, MaxShift).isZero())3164 return true;3165 3166 // If all of the bits shifted out are known to be zero, and Val is known3167 // non-zero then at least one non-zero bit must remain.3168 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)3169 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&3170 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))3171 return true;3172 3173 return false;3174}3175 3176static bool isKnownNonZeroFromOperator(const Operator *I,3177 const APInt &DemandedElts,3178 const SimplifyQuery &Q, unsigned Depth) {3179 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);3180 switch (I->getOpcode()) {3181 case Instruction::Alloca:3182 // Alloca never returns null, malloc might.3183 return I->getType()->getPointerAddressSpace() == 0;3184 case Instruction::GetElementPtr:3185 if (I->getType()->isPointerTy())3186 return isGEPKnownNonNull(cast<GEPOperator>(I), Q, Depth);3187 break;3188 case Instruction::BitCast: {3189 // We need to be a bit careful here. We can only peek through the bitcast3190 // if the scalar size of elements in the operand are smaller than and a3191 // multiple of the size they are casting too. Take three cases:3192 //3193 // 1) Unsafe:3194 // bitcast <2 x i16> %NonZero to <4 x i8>3195 //3196 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a3197 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't3198 // guranteed (imagine just sign bit set in the 2 i16 elements).3199 //3200 // 2) Unsafe:3201 // bitcast <4 x i3> %NonZero to <3 x i4>3202 //3203 // Even though the scalar size of the src (`i3`) is smaller than the3204 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`3205 // its possible for the `3 x i4` elements to be zero because there are3206 // some elements in the destination that don't contain any full src3207 // element.3208 //3209 // 3) Safe:3210 // bitcast <4 x i8> %NonZero to <2 x i16>3211 //3212 // This is always safe as non-zero in the 4 i8 elements implies3213 // non-zero in the combination of any two adjacent ones. Since i8 is a3214 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.3215 // This all implies the 2 i16 elements are non-zero.3216 Type *FromTy = I->getOperand(0)->getType();3217 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&3218 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)3219 return isKnownNonZero(I->getOperand(0), Q, Depth);3220 } break;3221 case Instruction::IntToPtr:3222 // Note that we have to take special care to avoid looking through3223 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well3224 // as casts that can alter the value, e.g., AddrSpaceCasts.3225 if (!isa<ScalableVectorType>(I->getType()) &&3226 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=3227 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())3228 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3229 break;3230 case Instruction::PtrToInt:3231 // Similar to int2ptr above, we can look through ptr2int here if the cast3232 // is a no-op or an extend and not a truncate.3233 if (!isa<ScalableVectorType>(I->getType()) &&3234 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=3235 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())3236 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3237 break;3238 case Instruction::Trunc:3239 // nuw/nsw trunc preserves zero/non-zero status of input.3240 if (auto *TI = dyn_cast<TruncInst>(I))3241 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())3242 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);3243 break;3244 3245 // Iff x - y != 0, then x ^ y != 03246 // Therefore we can do the same exact checks3247 case Instruction::Xor:3248 case Instruction::Sub:3249 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),3250 I->getOperand(1), Depth);3251 case Instruction::Or:3252 // (X | (X != 0)) is non zero3253 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))3254 return true;3255 // X | Y != 0 if X != Y.3256 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,3257 Depth))3258 return true;3259 // X | Y != 0 if X != 0 or Y != 0.3260 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||3261 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3262 case Instruction::SExt:3263 case Instruction::ZExt:3264 // ext X != 0 if X != 0.3265 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3266 3267 case Instruction::Shl: {3268 // shl nsw/nuw can't remove any non-zero bits.3269 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);3270 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))3271 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3272 3273 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined3274 // if the lowest bit is shifted off the end.3275 KnownBits Known(BitWidth);3276 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);3277 if (Known.One[0])3278 return true;3279 3280 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);3281 }3282 case Instruction::LShr:3283 case Instruction::AShr: {3284 // shr exact can only shift out zero bits.3285 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);3286 if (BO->isExact())3287 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3288 3289 // shr X, Y != 0 if X is negative. Note that the value of the shift is not3290 // defined if the sign bit is shifted off the end.3291 KnownBits Known =3292 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);3293 if (Known.isNegative())3294 return true;3295 3296 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);3297 }3298 case Instruction::UDiv:3299 case Instruction::SDiv: {3300 // X / Y3301 // div exact can only produce a zero if the dividend is zero.3302 if (cast<PossiblyExactOperator>(I)->isExact())3303 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);3304 3305 KnownBits XKnown =3306 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);3307 // If X is fully unknown we won't be able to figure anything out so don't3308 // both computing knownbits for Y.3309 if (XKnown.isUnknown())3310 return false;3311 3312 KnownBits YKnown =3313 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);3314 if (I->getOpcode() == Instruction::SDiv) {3315 // For signed division need to compare abs value of the operands.3316 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);3317 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);3318 }3319 // If X u>= Y then div is non zero (0/0 is UB).3320 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);3321 // If X is total unknown or X u< Y we won't be able to prove non-zero3322 // with compute known bits so just return early.3323 return XUgeY && *XUgeY;3324 }3325 case Instruction::Add: {3326 // X + Y.3327 3328 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is3329 // non-zero.3330 auto *BO = cast<OverflowingBinaryOperator>(I);3331 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),3332 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),3333 Q.IIQ.hasNoUnsignedWrap(BO), Depth);3334 }3335 case Instruction::Mul: {3336 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);3337 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),3338 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),3339 Q.IIQ.hasNoUnsignedWrap(BO), Depth);3340 }3341 case Instruction::Select: {3342 // (C ? X : Y) != 0 if X != 0 and Y != 0.3343 3344 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,3345 // then see if the select condition implies the arm is non-zero. For example3346 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is3347 // dominated by `X != 0`.3348 auto SelectArmIsNonZero = [&](bool IsTrueArm) {3349 Value *Op;3350 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);3351 // Op is trivially non-zero.3352 if (isKnownNonZero(Op, DemandedElts, Q, Depth))3353 return true;3354 3355 // The condition of the select dominates the true/false arm. Check if the3356 // condition implies that a given arm is non-zero.3357 Value *X;3358 CmpPredicate Pred;3359 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))3360 return false;3361 3362 if (!IsTrueArm)3363 Pred = ICmpInst::getInversePredicate(Pred);3364 3365 return cmpExcludesZero(Pred, X);3366 };3367 3368 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&3369 SelectArmIsNonZero(/* IsTrueArm */ false))3370 return true;3371 break;3372 }3373 case Instruction::PHI: {3374 auto *PN = cast<PHINode>(I);3375 if (Q.IIQ.UseInstrInfo && isNonZeroRecurrence(PN))3376 return true;3377 3378 // Check if all incoming values are non-zero using recursion.3379 SimplifyQuery RecQ = Q.getWithoutCondContext();3380 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);3381 return llvm::all_of(PN->operands(), [&](const Use &U) {3382 if (U.get() == PN)3383 return true;3384 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();3385 // Check if the branch on the phi excludes zero.3386 CmpPredicate Pred;3387 Value *X;3388 BasicBlock *TrueSucc, *FalseSucc;3389 if (match(RecQ.CxtI,3390 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),3391 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {3392 // Check for cases of duplicate successors.3393 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {3394 // If we're using the false successor, invert the predicate.3395 if (FalseSucc == PN->getParent())3396 Pred = CmpInst::getInversePredicate(Pred);3397 if (cmpExcludesZero(Pred, X))3398 return true;3399 }3400 }3401 // Finally recurse on the edge and check it directly.3402 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);3403 });3404 }3405 case Instruction::InsertElement: {3406 if (isa<ScalableVectorType>(I->getType()))3407 break;3408 3409 const Value *Vec = I->getOperand(0);3410 const Value *Elt = I->getOperand(1);3411 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));3412 3413 unsigned NumElts = DemandedElts.getBitWidth();3414 APInt DemandedVecElts = DemandedElts;3415 bool SkipElt = false;3416 // If we know the index we are inserting too, clear it from Vec check.3417 if (CIdx && CIdx->getValue().ult(NumElts)) {3418 DemandedVecElts.clearBit(CIdx->getZExtValue());3419 SkipElt = !DemandedElts[CIdx->getZExtValue()];3420 }3421 3422 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec3423 // are non-zero.3424 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&3425 (DemandedVecElts.isZero() ||3426 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));3427 }3428 case Instruction::ExtractElement:3429 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {3430 const Value *Vec = EEI->getVectorOperand();3431 const Value *Idx = EEI->getIndexOperand();3432 auto *CIdx = dyn_cast<ConstantInt>(Idx);3433 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {3434 unsigned NumElts = VecTy->getNumElements();3435 APInt DemandedVecElts = APInt::getAllOnes(NumElts);3436 if (CIdx && CIdx->getValue().ult(NumElts))3437 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());3438 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);3439 }3440 }3441 break;3442 case Instruction::ShuffleVector: {3443 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);3444 if (!Shuf)3445 break;3446 APInt DemandedLHS, DemandedRHS;3447 // For undef elements, we don't know anything about the common state of3448 // the shuffle result.3449 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))3450 break;3451 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.3452 return (DemandedRHS.isZero() ||3453 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&3454 (DemandedLHS.isZero() ||3455 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));3456 }3457 case Instruction::Freeze:3458 return isKnownNonZero(I->getOperand(0), Q, Depth) &&3459 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,3460 Depth);3461 case Instruction::Load: {3462 auto *LI = cast<LoadInst>(I);3463 // A Load tagged with nonnull or dereferenceable with null pointer undefined3464 // is never null.3465 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {3466 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||3467 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&3468 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))3469 return true;3470 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {3471 return rangeMetadataExcludesValue(Ranges, APInt::getZero(BitWidth));3472 }3473 3474 // No need to fall through to computeKnownBits as range metadata is already3475 // handled in isKnownNonZero.3476 return false;3477 }3478 case Instruction::ExtractValue: {3479 const WithOverflowInst *WO;3480 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {3481 switch (WO->getBinaryOp()) {3482 default:3483 break;3484 case Instruction::Add:3485 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),3486 WO->getArgOperand(1),3487 /*NSW=*/false,3488 /*NUW=*/false, Depth);3489 case Instruction::Sub:3490 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),3491 WO->getArgOperand(1), Depth);3492 case Instruction::Mul:3493 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),3494 WO->getArgOperand(1),3495 /*NSW=*/false, /*NUW=*/false, Depth);3496 break;3497 }3498 }3499 break;3500 }3501 case Instruction::Call:3502 case Instruction::Invoke: {3503 const auto *Call = cast<CallBase>(I);3504 if (I->getType()->isPointerTy()) {3505 if (Call->isReturnNonNull())3506 return true;3507 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))3508 return isKnownNonZero(RP, Q, Depth);3509 } else {3510 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))3511 return rangeMetadataExcludesValue(Ranges, APInt::getZero(BitWidth));3512 if (std::optional<ConstantRange> Range = Call->getRange()) {3513 const APInt ZeroValue(Range->getBitWidth(), 0);3514 if (!Range->contains(ZeroValue))3515 return true;3516 }3517 if (const Value *RV = Call->getReturnedArgOperand())3518 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))3519 return true;3520 }3521 3522 if (auto *II = dyn_cast<IntrinsicInst>(I)) {3523 switch (II->getIntrinsicID()) {3524 case Intrinsic::sshl_sat:3525 case Intrinsic::ushl_sat:3526 case Intrinsic::abs:3527 case Intrinsic::bitreverse:3528 case Intrinsic::bswap:3529 case Intrinsic::ctpop:3530 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);3531 // NB: We don't do usub_sat here as in any case we can prove its3532 // non-zero, we will fold it to `sub nuw` in InstCombine.3533 case Intrinsic::ssub_sat:3534 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),3535 II->getArgOperand(1), Depth);3536 case Intrinsic::sadd_sat:3537 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),3538 II->getArgOperand(1),3539 /*NSW=*/true, /* NUW=*/false, Depth);3540 // Vec reverse preserves zero/non-zero status from input vec.3541 case Intrinsic::vector_reverse:3542 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),3543 Q, Depth);3544 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.3545 case Intrinsic::vector_reduce_or:3546 case Intrinsic::vector_reduce_umax:3547 case Intrinsic::vector_reduce_umin:3548 case Intrinsic::vector_reduce_smax:3549 case Intrinsic::vector_reduce_smin:3550 return isKnownNonZero(II->getArgOperand(0), Q, Depth);3551 case Intrinsic::umax:3552 case Intrinsic::uadd_sat:3553 // umax(X, (X != 0)) is non zero3554 // X +usat (X != 0) is non zero3555 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))3556 return true;3557 3558 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||3559 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);3560 case Intrinsic::smax: {3561 // If either arg is strictly positive the result is non-zero. Otherwise3562 // the result is non-zero if both ops are non-zero.3563 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,3564 const KnownBits &OpKnown) {3565 if (!OpNonZero.has_value())3566 OpNonZero = OpKnown.isNonZero() ||3567 isKnownNonZero(Op, DemandedElts, Q, Depth);3568 return *OpNonZero;3569 };3570 // Avoid re-computing isKnownNonZero.3571 std::optional<bool> Op0NonZero, Op1NonZero;3572 KnownBits Op1Known =3573 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);3574 if (Op1Known.isNonNegative() &&3575 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))3576 return true;3577 KnownBits Op0Known =3578 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);3579 if (Op0Known.isNonNegative() &&3580 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))3581 return true;3582 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&3583 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);3584 }3585 case Intrinsic::smin: {3586 // If either arg is negative the result is non-zero. Otherwise3587 // the result is non-zero if both ops are non-zero.3588 KnownBits Op1Known =3589 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);3590 if (Op1Known.isNegative())3591 return true;3592 KnownBits Op0Known =3593 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);3594 if (Op0Known.isNegative())3595 return true;3596 3597 if (Op1Known.isNonZero() && Op0Known.isNonZero())3598 return true;3599 }3600 [[fallthrough]];3601 case Intrinsic::umin:3602 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&3603 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);3604 case Intrinsic::cttz:3605 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)3606 .Zero[0];3607 case Intrinsic::ctlz:3608 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)3609 .isNonNegative();3610 case Intrinsic::fshr:3611 case Intrinsic::fshl:3612 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.3613 if (II->getArgOperand(0) == II->getArgOperand(1))3614 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);3615 break;3616 case Intrinsic::vscale:3617 return true;3618 case Intrinsic::experimental_get_vector_length:3619 return isKnownNonZero(I->getOperand(0), Q, Depth);3620 default:3621 break;3622 }3623 break;3624 }3625 3626 return false;3627 }3628 }3629 3630 KnownBits Known(BitWidth);3631 computeKnownBits(I, DemandedElts, Known, Q, Depth);3632 return Known.One != 0;3633}3634 3635/// Return true if the given value is known to be non-zero when defined. For3636/// vectors, return true if every demanded element is known to be non-zero when3637/// defined. For pointers, if the context instruction and dominator tree are3638/// specified, perform context-sensitive analysis and return true if the3639/// pointer couldn't possibly be null at the specified instruction.3640/// Supports values with integer or pointer type and vectors of integers.3641bool isKnownNonZero(const Value *V, const APInt &DemandedElts,3642 const SimplifyQuery &Q, unsigned Depth) {3643 Type *Ty = V->getType();3644 3645#ifndef NDEBUG3646 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");3647 3648 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {3649 assert(3650 FVTy->getNumElements() == DemandedElts.getBitWidth() &&3651 "DemandedElt width should equal the fixed vector number of elements");3652 } else {3653 assert(DemandedElts == APInt(1, 1) &&3654 "DemandedElt width should be 1 for scalars");3655 }3656#endif3657 3658 if (auto *C = dyn_cast<Constant>(V)) {3659 if (C->isNullValue())3660 return false;3661 if (isa<ConstantInt>(C))3662 // Must be non-zero due to null test above.3663 return true;3664 3665 // For constant vectors, check that all elements are poison or known3666 // non-zero to determine that the whole vector is known non-zero.3667 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {3668 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {3669 if (!DemandedElts[i])3670 continue;3671 Constant *Elt = C->getAggregateElement(i);3672 if (!Elt || Elt->isNullValue())3673 return false;3674 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))3675 return false;3676 }3677 return true;3678 }3679 3680 // Constant ptrauth can be null, iff the base pointer can be.3681 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))3682 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);3683 3684 // A global variable in address space 0 is non null unless extern weak3685 // or an absolute symbol reference. Other address spaces may have null as a3686 // valid address for a global, so we can't assume anything.3687 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {3688 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&3689 GV->getType()->getAddressSpace() == 0)3690 return true;3691 }3692 3693 // For constant expressions, fall through to the Operator code below.3694 if (!isa<ConstantExpr>(V))3695 return false;3696 }3697 3698 if (const auto *A = dyn_cast<Argument>(V))3699 if (std::optional<ConstantRange> Range = A->getRange()) {3700 const APInt ZeroValue(Range->getBitWidth(), 0);3701 if (!Range->contains(ZeroValue))3702 return true;3703 }3704 3705 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))3706 return true;3707 3708 // Some of the tests below are recursive, so bail out if we hit the limit.3709 if (Depth++ >= MaxAnalysisRecursionDepth)3710 return false;3711 3712 // Check for pointer simplifications.3713 3714 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {3715 // A byval, inalloca may not be null in a non-default addres space. A3716 // nonnull argument is assumed never 0.3717 if (const Argument *A = dyn_cast<Argument>(V)) {3718 if (((A->hasPassPointeeByValueCopyAttr() &&3719 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||3720 A->hasNonNullAttr()))3721 return true;3722 }3723 }3724 3725 if (const auto *I = dyn_cast<Operator>(V))3726 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))3727 return true;3728 3729 if (!isa<Constant>(V) &&3730 isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))3731 return true;3732 3733 if (const Value *Stripped = stripNullTest(V))3734 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);3735 3736 return false;3737}3738 3739bool llvm::isKnownNonZero(const Value *V, const SimplifyQuery &Q,3740 unsigned Depth) {3741 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());3742 APInt DemandedElts =3743 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);3744 return ::isKnownNonZero(V, DemandedElts, Q, Depth);3745}3746 3747/// If the pair of operators are the same invertible function, return the3748/// the operands of the function corresponding to each input. Otherwise,3749/// return std::nullopt. An invertible function is one that is 1-to-1 and maps3750/// every input value to exactly one output value. This is equivalent to3751/// saying that Op1 and Op2 are equal exactly when the specified pair of3752/// operands are equal, (except that Op1 and Op2 may be poison more often.)3753static std::optional<std::pair<Value*, Value*>>3754getInvertibleOperands(const Operator *Op1,3755 const Operator *Op2) {3756 if (Op1->getOpcode() != Op2->getOpcode())3757 return std::nullopt;3758 3759 auto getOperands = [&](unsigned OpNum) -> auto {3760 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));3761 };3762 3763 switch (Op1->getOpcode()) {3764 default:3765 break;3766 case Instruction::Or:3767 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||3768 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())3769 break;3770 [[fallthrough]];3771 case Instruction::Xor:3772 case Instruction::Add: {3773 Value *Other;3774 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))3775 return std::make_pair(Op1->getOperand(1), Other);3776 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))3777 return std::make_pair(Op1->getOperand(0), Other);3778 break;3779 }3780 case Instruction::Sub:3781 if (Op1->getOperand(0) == Op2->getOperand(0))3782 return getOperands(1);3783 if (Op1->getOperand(1) == Op2->getOperand(1))3784 return getOperands(0);3785 break;3786 case Instruction::Mul: {3787 // invertible if A * B == (A * B) mod 2^N where A, and B are integers3788 // and N is the bitwdith. The nsw case is non-obvious, but proven by3789 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK3790 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);3791 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);3792 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&3793 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))3794 break;3795 3796 // Assume operand order has been canonicalized3797 if (Op1->getOperand(1) == Op2->getOperand(1) &&3798 isa<ConstantInt>(Op1->getOperand(1)) &&3799 !cast<ConstantInt>(Op1->getOperand(1))->isZero())3800 return getOperands(0);3801 break;3802 }3803 case Instruction::Shl: {3804 // Same as multiplies, with the difference that we don't need to check3805 // for a non-zero multiply. Shifts always multiply by non-zero.3806 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);3807 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);3808 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&3809 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))3810 break;3811 3812 if (Op1->getOperand(1) == Op2->getOperand(1))3813 return getOperands(0);3814 break;3815 }3816 case Instruction::AShr:3817 case Instruction::LShr: {3818 auto *PEO1 = cast<PossiblyExactOperator>(Op1);3819 auto *PEO2 = cast<PossiblyExactOperator>(Op2);3820 if (!PEO1->isExact() || !PEO2->isExact())3821 break;3822 3823 if (Op1->getOperand(1) == Op2->getOperand(1))3824 return getOperands(0);3825 break;3826 }3827 case Instruction::SExt:3828 case Instruction::ZExt:3829 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())3830 return getOperands(0);3831 break;3832 case Instruction::PHI: {3833 const PHINode *PN1 = cast<PHINode>(Op1);3834 const PHINode *PN2 = cast<PHINode>(Op2);3835 3836 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences3837 // are a single invertible function of the start values? Note that repeated3838 // application of an invertible function is also invertible3839 BinaryOperator *BO1 = nullptr;3840 Value *Start1 = nullptr, *Step1 = nullptr;3841 BinaryOperator *BO2 = nullptr;3842 Value *Start2 = nullptr, *Step2 = nullptr;3843 if (PN1->getParent() != PN2->getParent() ||3844 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||3845 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))3846 break;3847 3848 auto Values = getInvertibleOperands(cast<Operator>(BO1),3849 cast<Operator>(BO2));3850 if (!Values)3851 break;3852 3853 // We have to be careful of mutually defined recurrences here. Ex:3854 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V3855 // * X_i = Y_i = X_(i-1) OP Y_(i-1)3856 // The invertibility of these is complicated, and not worth reasoning3857 // about (yet?).3858 if (Values->first != PN1 || Values->second != PN2)3859 break;3860 3861 return std::make_pair(Start1, Start2);3862 }3863 }3864 return std::nullopt;3865}3866 3867/// Return true if V1 == (binop V2, X), where X is known non-zero.3868/// Only handle a small subset of binops where (binop V2, X) with non-zero X3869/// implies V2 != V1.3870static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,3871 const APInt &DemandedElts,3872 const SimplifyQuery &Q, unsigned Depth) {3873 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);3874 if (!BO)3875 return false;3876 switch (BO->getOpcode()) {3877 default:3878 break;3879 case Instruction::Or:3880 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())3881 break;3882 [[fallthrough]];3883 case Instruction::Xor:3884 case Instruction::Add:3885 Value *Op = nullptr;3886 if (V2 == BO->getOperand(0))3887 Op = BO->getOperand(1);3888 else if (V2 == BO->getOperand(1))3889 Op = BO->getOperand(0);3890 else3891 return false;3892 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);3893 }3894 return false;3895}3896 3897/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and3898/// the multiplication is nuw or nsw.3899static bool isNonEqualMul(const Value *V1, const Value *V2,3900 const APInt &DemandedElts, const SimplifyQuery &Q,3901 unsigned Depth) {3902 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {3903 const APInt *C;3904 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&3905 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&3906 !C->isZero() && !C->isOne() &&3907 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);3908 }3909 return false;3910}3911 3912/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and3913/// the shift is nuw or nsw.3914static bool isNonEqualShl(const Value *V1, const Value *V2,3915 const APInt &DemandedElts, const SimplifyQuery &Q,3916 unsigned Depth) {3917 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {3918 const APInt *C;3919 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&3920 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&3921 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);3922 }3923 return false;3924}3925 3926static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,3927 const APInt &DemandedElts, const SimplifyQuery &Q,3928 unsigned Depth) {3929 // Check two PHIs are in same block.3930 if (PN1->getParent() != PN2->getParent())3931 return false;3932 3933 SmallPtrSet<const BasicBlock *, 8> VisitedBBs;3934 bool UsedFullRecursion = false;3935 for (const BasicBlock *IncomBB : PN1->blocks()) {3936 if (!VisitedBBs.insert(IncomBB).second)3937 continue; // Don't reprocess blocks that we have dealt with already.3938 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);3939 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);3940 const APInt *C1, *C2;3941 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)3942 continue;3943 3944 // Only one pair of phi operands is allowed for full recursion.3945 if (UsedFullRecursion)3946 return false;3947 3948 SimplifyQuery RecQ = Q.getWithoutCondContext();3949 RecQ.CxtI = IncomBB->getTerminator();3950 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))3951 return false;3952 UsedFullRecursion = true;3953 }3954 return true;3955}3956 3957static bool isNonEqualSelect(const Value *V1, const Value *V2,3958 const APInt &DemandedElts, const SimplifyQuery &Q,3959 unsigned Depth) {3960 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);3961 if (!SI1)3962 return false;3963 3964 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {3965 const Value *Cond1 = SI1->getCondition();3966 const Value *Cond2 = SI2->getCondition();3967 if (Cond1 == Cond2)3968 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),3969 DemandedElts, Q, Depth + 1) &&3970 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),3971 DemandedElts, Q, Depth + 1);3972 }3973 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&3974 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);3975}3976 3977// Check to see if A is both a GEP and is the incoming value for a PHI in the3978// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,3979// one of them being the recursive GEP A and the other a ptr at same base and at3980// the same/higher offset than B we are only incrementing the pointer further in3981// loop if offset of recursive GEP is greater than 0.3982static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,3983 const SimplifyQuery &Q) {3984 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())3985 return false;3986 3987 auto *GEPA = dyn_cast<GEPOperator>(A);3988 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))3989 return false;3990 3991 // Handle 2 incoming PHI values with one being a recursive GEP.3992 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());3993 if (!PN || PN->getNumIncomingValues() != 2)3994 return false;3995 3996 // Search for the recursive GEP as an incoming operand, and record that as3997 // Step.3998 Value *Start = nullptr;3999 Value *Step = const_cast<Value *>(A);4000 if (PN->getIncomingValue(0) == Step)4001 Start = PN->getIncomingValue(1);4002 else if (PN->getIncomingValue(1) == Step)4003 Start = PN->getIncomingValue(0);4004 else4005 return false;4006 4007 // Other incoming node base should match the B base.4008 // StartOffset >= OffsetB && StepOffset > 0?4009 // StartOffset <= OffsetB && StepOffset < 0?4010 // Is non-equal if above are true.4011 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the4012 // optimisation to inbounds GEPs only.4013 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());4014 APInt StartOffset(IndexWidth, 0);4015 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);4016 APInt StepOffset(IndexWidth, 0);4017 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);4018 4019 // Check if Base Pointer of Step matches the PHI.4020 if (Step != PN)4021 return false;4022 APInt OffsetB(IndexWidth, 0);4023 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);4024 return Start == B &&4025 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||4026 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));4027}4028 4029static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,4030 const SimplifyQuery &Q, unsigned Depth) {4031 if (!Q.CxtI)4032 return false;4033 4034 // Try to infer NonEqual based on information from dominating conditions.4035 if (Q.DC && Q.DT) {4036 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {4037 for (BranchInst *BI : Q.DC->conditionsFor(V)) {4038 Value *Cond = BI->getCondition();4039 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));4040 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&4041 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,4042 /*LHSIsTrue=*/true, Depth)4043 .value_or(false))4044 return true;4045 4046 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));4047 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&4048 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,4049 /*LHSIsTrue=*/false, Depth)4050 .value_or(false))4051 return true;4052 }4053 4054 return false;4055 };4056 4057 if (IsKnownNonEqualFromDominatingCondition(V1) ||4058 IsKnownNonEqualFromDominatingCondition(V2))4059 return true;4060 }4061 4062 if (!Q.AC)4063 return false;4064 4065 // Try to infer NonEqual based on information from assumptions.4066 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {4067 if (!AssumeVH)4068 continue;4069 CallInst *I = cast<CallInst>(AssumeVH);4070 4071 assert(I->getFunction() == Q.CxtI->getFunction() &&4072 "Got assumption for the wrong function!");4073 assert(I->getIntrinsicID() == Intrinsic::assume &&4074 "must be an assume intrinsic");4075 4076 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,4077 /*LHSIsTrue=*/true, Depth)4078 .value_or(false) &&4079 isValidAssumeForContext(I, Q.CxtI, Q.DT))4080 return true;4081 }4082 4083 return false;4084}4085 4086/// Return true if it is known that V1 != V2.4087static bool isKnownNonEqual(const Value *V1, const Value *V2,4088 const APInt &DemandedElts, const SimplifyQuery &Q,4089 unsigned Depth) {4090 if (V1 == V2)4091 return false;4092 if (V1->getType() != V2->getType())4093 // We can't look through casts yet.4094 return false;4095 4096 if (Depth >= MaxAnalysisRecursionDepth)4097 return false;4098 4099 // See if we can recurse through (exactly one of) our operands. This4100 // requires our operation be 1-to-1 and map every input value to exactly4101 // one output value. Such an operation is invertible.4102 auto *O1 = dyn_cast<Operator>(V1);4103 auto *O2 = dyn_cast<Operator>(V2);4104 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {4105 if (auto Values = getInvertibleOperands(O1, O2))4106 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,4107 Depth + 1);4108 4109 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {4110 const PHINode *PN2 = cast<PHINode>(V2);4111 // FIXME: This is missing a generalization to handle the case where one is4112 // a PHI and another one isn't.4113 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))4114 return true;4115 };4116 }4117 4118 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||4119 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))4120 return true;4121 4122 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||4123 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))4124 return true;4125 4126 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||4127 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))4128 return true;4129 4130 if (V1->getType()->isIntOrIntVectorTy()) {4131 // Are any known bits in V1 contradictory to known bits in V2? If V14132 // has a known zero where V2 has a known one, they must not be equal.4133 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);4134 if (!Known1.isUnknown()) {4135 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);4136 if (Known1.Zero.intersects(Known2.One) ||4137 Known2.Zero.intersects(Known1.One))4138 return true;4139 }4140 }4141 4142 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||4143 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))4144 return true;4145 4146 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||4147 isNonEqualPointersWithRecursiveGEP(V2, V1, Q))4148 return true;4149 4150 Value *A, *B;4151 // PtrToInts are NonEqual if their Ptrs are NonEqual.4152 // Check PtrToInt type matches the pointer size.4153 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&4154 match(V2, m_PtrToIntSameSize(Q.DL, m_Value(B))))4155 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);4156 4157 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))4158 return true;4159 4160 return false;4161}4162 4163/// For vector constants, loop over the elements and find the constant with the4164/// minimum number of sign bits. Return 0 if the value is not a vector constant4165/// or if any element was not analyzed; otherwise, return the count for the4166/// element with the minimum number of sign bits.4167static unsigned computeNumSignBitsVectorConstant(const Value *V,4168 const APInt &DemandedElts,4169 unsigned TyBits) {4170 const auto *CV = dyn_cast<Constant>(V);4171 if (!CV || !isa<FixedVectorType>(CV->getType()))4172 return 0;4173 4174 unsigned MinSignBits = TyBits;4175 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();4176 for (unsigned i = 0; i != NumElts; ++i) {4177 if (!DemandedElts[i])4178 continue;4179 // If we find a non-ConstantInt, bail out.4180 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));4181 if (!Elt)4182 return 0;4183 4184 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());4185 }4186 4187 return MinSignBits;4188}4189 4190static unsigned ComputeNumSignBitsImpl(const Value *V,4191 const APInt &DemandedElts,4192 const SimplifyQuery &Q, unsigned Depth);4193 4194static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,4195 const SimplifyQuery &Q, unsigned Depth) {4196 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);4197 assert(Result > 0 && "At least one sign bit needs to be present!");4198 return Result;4199}4200 4201/// Return the number of times the sign bit of the register is replicated into4202/// the other bits. We know that at least 1 bit is always equal to the sign bit4203/// (itself), but other cases can give us information. For example, immediately4204/// after an "ashr X, 2", we know that the top 3 bits are all equal to each4205/// other, so we return 3. For vectors, return the number of sign bits for the4206/// vector element with the minimum number of known sign bits of the demanded4207/// elements in the vector specified by DemandedElts.4208static unsigned ComputeNumSignBitsImpl(const Value *V,4209 const APInt &DemandedElts,4210 const SimplifyQuery &Q, unsigned Depth) {4211 Type *Ty = V->getType();4212#ifndef NDEBUG4213 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");4214 4215 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {4216 assert(4217 FVTy->getNumElements() == DemandedElts.getBitWidth() &&4218 "DemandedElt width should equal the fixed vector number of elements");4219 } else {4220 assert(DemandedElts == APInt(1, 1) &&4221 "DemandedElt width should be 1 for scalars");4222 }4223#endif4224 4225 // We return the minimum number of sign bits that are guaranteed to be present4226 // in V, so for undef we have to conservatively return 1. We don't have the4227 // same behavior for poison though -- that's a FIXME today.4228 4229 Type *ScalarTy = Ty->getScalarType();4230 unsigned TyBits = ScalarTy->isPointerTy() ?4231 Q.DL.getPointerTypeSizeInBits(ScalarTy) :4232 Q.DL.getTypeSizeInBits(ScalarTy);4233 4234 unsigned Tmp, Tmp2;4235 unsigned FirstAnswer = 1;4236 4237 // Note that ConstantInt is handled by the general computeKnownBits case4238 // below.4239 4240 if (Depth == MaxAnalysisRecursionDepth)4241 return 1;4242 4243 if (auto *U = dyn_cast<Operator>(V)) {4244 switch (Operator::getOpcode(V)) {4245 default: break;4246 case Instruction::BitCast: {4247 Value *Src = U->getOperand(0);4248 Type *SrcTy = Src->getType();4249 4250 // Skip if the source type is not an integer or integer vector type4251 // This ensures we only process integer-like types4252 if (!SrcTy->isIntOrIntVectorTy())4253 break;4254 4255 unsigned SrcBits = SrcTy->getScalarSizeInBits();4256 4257 // Bitcast 'large element' scalar/vector to 'small element' vector.4258 if ((SrcBits % TyBits) != 0)4259 break;4260 4261 // Only proceed if the destination type is a fixed-size vector4262 if (isa<FixedVectorType>(Ty)) {4263 // Fast case - sign splat can be simply split across the small elements.4264 // This works for both vector and scalar sources4265 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);4266 if (Tmp == SrcBits)4267 return TyBits;4268 }4269 break;4270 }4271 case Instruction::SExt:4272 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();4273 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +4274 Tmp;4275 4276 case Instruction::SDiv: {4277 const APInt *Denominator;4278 // sdiv X, C -> adds log(C) sign bits.4279 if (match(U->getOperand(1), m_APInt(Denominator))) {4280 4281 // Ignore non-positive denominator.4282 if (!Denominator->isStrictlyPositive())4283 break;4284 4285 // Calculate the incoming numerator bits.4286 unsigned NumBits =4287 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4288 4289 // Add floor(log(C)) bits to the numerator bits.4290 return std::min(TyBits, NumBits + Denominator->logBase2());4291 }4292 break;4293 }4294 4295 case Instruction::SRem: {4296 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4297 4298 const APInt *Denominator;4299 // srem X, C -> we know that the result is within [-C+1,C) when C is a4300 // positive constant. This let us put a lower bound on the number of sign4301 // bits.4302 if (match(U->getOperand(1), m_APInt(Denominator))) {4303 4304 // Ignore non-positive denominator.4305 if (Denominator->isStrictlyPositive()) {4306 // Calculate the leading sign bit constraints by examining the4307 // denominator. Given that the denominator is positive, there are two4308 // cases:4309 //4310 // 1. The numerator is positive. The result range is [0,C) and4311 // [0,C) u< (1 << ceilLogBase2(C)).4312 //4313 // 2. The numerator is negative. Then the result range is (-C,0] and4314 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).4315 //4316 // Thus a lower bound on the number of sign bits is `TyBits -4317 // ceilLogBase2(C)`.4318 4319 unsigned ResBits = TyBits - Denominator->ceilLogBase2();4320 Tmp = std::max(Tmp, ResBits);4321 }4322 }4323 return Tmp;4324 }4325 4326 case Instruction::AShr: {4327 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4328 // ashr X, C -> adds C sign bits. Vectors too.4329 const APInt *ShAmt;4330 if (match(U->getOperand(1), m_APInt(ShAmt))) {4331 if (ShAmt->uge(TyBits))4332 break; // Bad shift.4333 unsigned ShAmtLimited = ShAmt->getZExtValue();4334 Tmp += ShAmtLimited;4335 if (Tmp > TyBits) Tmp = TyBits;4336 }4337 return Tmp;4338 }4339 case Instruction::Shl: {4340 const APInt *ShAmt;4341 Value *X = nullptr;4342 if (match(U->getOperand(1), m_APInt(ShAmt))) {4343 // shl destroys sign bits.4344 if (ShAmt->uge(TyBits))4345 break; // Bad shift.4346 // We can look through a zext (more or less treating it as a sext) if4347 // all extended bits are shifted out.4348 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&4349 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {4350 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);4351 Tmp += TyBits - X->getType()->getScalarSizeInBits();4352 } else4353 Tmp =4354 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4355 if (ShAmt->uge(Tmp))4356 break; // Shifted all sign bits out.4357 Tmp2 = ShAmt->getZExtValue();4358 return Tmp - Tmp2;4359 }4360 break;4361 }4362 case Instruction::And:4363 case Instruction::Or:4364 case Instruction::Xor: // NOT is handled here.4365 // Logical binary ops preserve the number of sign bits at the worst.4366 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4367 if (Tmp != 1) {4368 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);4369 FirstAnswer = std::min(Tmp, Tmp2);4370 // We computed what we know about the sign bits as our first4371 // answer. Now proceed to the generic code that uses4372 // computeKnownBits, and pick whichever answer is better.4373 }4374 break;4375 4376 case Instruction::Select: {4377 // If we have a clamp pattern, we know that the number of sign bits will4378 // be the minimum of the clamp min/max range.4379 const Value *X;4380 const APInt *CLow, *CHigh;4381 if (isSignedMinMaxClamp(U, X, CLow, CHigh))4382 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());4383 4384 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);4385 if (Tmp == 1)4386 break;4387 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);4388 return std::min(Tmp, Tmp2);4389 }4390 4391 case Instruction::Add:4392 // Add can have at most one carry bit. Thus we know that the output4393 // is, at worst, one more bit than the inputs.4394 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);4395 if (Tmp == 1) break;4396 4397 // Special case decrementing a value (ADD X, -1):4398 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))4399 if (CRHS->isAllOnesValue()) {4400 KnownBits Known(TyBits);4401 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);4402 4403 // If the input is known to be 0 or 1, the output is 0/-1, which is4404 // all sign bits set.4405 if ((Known.Zero | 1).isAllOnes())4406 return TyBits;4407 4408 // If we are subtracting one from a positive number, there is no carry4409 // out of the result.4410 if (Known.isNonNegative())4411 return Tmp;4412 }4413 4414 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);4415 if (Tmp2 == 1)4416 break;4417 return std::min(Tmp, Tmp2) - 1;4418 4419 case Instruction::Sub:4420 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);4421 if (Tmp2 == 1)4422 break;4423 4424 // Handle NEG.4425 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))4426 if (CLHS->isNullValue()) {4427 KnownBits Known(TyBits);4428 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);4429 // If the input is known to be 0 or 1, the output is 0/-1, which is4430 // all sign bits set.4431 if ((Known.Zero | 1).isAllOnes())4432 return TyBits;4433 4434 // If the input is known to be positive (the sign bit is known clear),4435 // the output of the NEG has the same number of sign bits as the4436 // input.4437 if (Known.isNonNegative())4438 return Tmp2;4439 4440 // Otherwise, we treat this like a SUB.4441 }4442 4443 // Sub can have at most one carry bit. Thus we know that the output4444 // is, at worst, one more bit than the inputs.4445 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4446 if (Tmp == 1)4447 break;4448 return std::min(Tmp, Tmp2) - 1;4449 4450 case Instruction::Mul: {4451 // The output of the Mul can be at most twice the valid bits in the4452 // inputs.4453 unsigned SignBitsOp0 =4454 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4455 if (SignBitsOp0 == 1)4456 break;4457 unsigned SignBitsOp1 =4458 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);4459 if (SignBitsOp1 == 1)4460 break;4461 unsigned OutValidBits =4462 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);4463 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;4464 }4465 4466 case Instruction::PHI: {4467 const PHINode *PN = cast<PHINode>(U);4468 unsigned NumIncomingValues = PN->getNumIncomingValues();4469 // Don't analyze large in-degree PHIs.4470 if (NumIncomingValues > 4) break;4471 // Unreachable blocks may have zero-operand PHI nodes.4472 if (NumIncomingValues == 0) break;4473 4474 // Take the minimum of all incoming values. This can't infinitely loop4475 // because of our depth threshold.4476 SimplifyQuery RecQ = Q.getWithoutCondContext();4477 Tmp = TyBits;4478 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {4479 if (Tmp == 1) return Tmp;4480 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();4481 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),4482 DemandedElts, RecQ, Depth + 1));4483 }4484 return Tmp;4485 }4486 4487 case Instruction::Trunc: {4488 // If the input contained enough sign bits that some remain after the4489 // truncation, then we can make use of that. Otherwise we don't know4490 // anything.4491 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);4492 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();4493 if (Tmp > (OperandTyBits - TyBits))4494 return Tmp - (OperandTyBits - TyBits);4495 4496 return 1;4497 }4498 4499 case Instruction::ExtractElement:4500 // Look through extract element. At the moment we keep this simple and4501 // skip tracking the specific element. But at least we might find4502 // information valid for all elements of the vector (for example if vector4503 // is sign extended, shifted, etc).4504 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);4505 4506 case Instruction::ShuffleVector: {4507 // Collect the minimum number of sign bits that are shared by every vector4508 // element referenced by the shuffle.4509 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);4510 if (!Shuf) {4511 // FIXME: Add support for shufflevector constant expressions.4512 return 1;4513 }4514 APInt DemandedLHS, DemandedRHS;4515 // For undef elements, we don't know anything about the common state of4516 // the shuffle result.4517 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))4518 return 1;4519 Tmp = std::numeric_limits<unsigned>::max();4520 if (!!DemandedLHS) {4521 const Value *LHS = Shuf->getOperand(0);4522 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);4523 }4524 // If we don't know anything, early out and try computeKnownBits4525 // fall-back.4526 if (Tmp == 1)4527 break;4528 if (!!DemandedRHS) {4529 const Value *RHS = Shuf->getOperand(1);4530 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);4531 Tmp = std::min(Tmp, Tmp2);4532 }4533 // If we don't know anything, early out and try computeKnownBits4534 // fall-back.4535 if (Tmp == 1)4536 break;4537 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");4538 return Tmp;4539 }4540 case Instruction::Call: {4541 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {4542 switch (II->getIntrinsicID()) {4543 default:4544 break;4545 case Intrinsic::abs:4546 Tmp =4547 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);4548 if (Tmp == 1)4549 break;4550 4551 // Absolute value reduces number of sign bits by at most 1.4552 return Tmp - 1;4553 case Intrinsic::smin:4554 case Intrinsic::smax: {4555 const APInt *CLow, *CHigh;4556 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))4557 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());4558 }4559 }4560 }4561 }4562 }4563 }4564 4565 // Finally, if we can prove that the top bits of the result are 0's or 1's,4566 // use this information.4567 4568 // If we can examine all elements of a vector constant successfully, we're4569 // done (we can't do any better than that). If not, keep trying.4570 if (unsigned VecSignBits =4571 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))4572 return VecSignBits;4573 4574 KnownBits Known(TyBits);4575 computeKnownBits(V, DemandedElts, Known, Q, Depth);4576 4577 // If we know that the sign bit is either zero or one, determine the number of4578 // identical bits in the top of the input value.4579 return std::max(FirstAnswer, Known.countMinSignBits());4580}4581 4582Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB,4583 const TargetLibraryInfo *TLI) {4584 const Function *F = CB.getCalledFunction();4585 if (!F)4586 return Intrinsic::not_intrinsic;4587 4588 if (F->isIntrinsic())4589 return F->getIntrinsicID();4590 4591 // We are going to infer semantics of a library function based on mapping it4592 // to an LLVM intrinsic. Check that the library function is available from4593 // this callbase and in this environment.4594 LibFunc Func;4595 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||4596 !CB.onlyReadsMemory())4597 return Intrinsic::not_intrinsic;4598 4599 switch (Func) {4600 default:4601 break;4602 case LibFunc_sin:4603 case LibFunc_sinf:4604 case LibFunc_sinl:4605 return Intrinsic::sin;4606 case LibFunc_cos:4607 case LibFunc_cosf:4608 case LibFunc_cosl:4609 return Intrinsic::cos;4610 case LibFunc_tan:4611 case LibFunc_tanf:4612 case LibFunc_tanl:4613 return Intrinsic::tan;4614 case LibFunc_asin:4615 case LibFunc_asinf:4616 case LibFunc_asinl:4617 return Intrinsic::asin;4618 case LibFunc_acos:4619 case LibFunc_acosf:4620 case LibFunc_acosl:4621 return Intrinsic::acos;4622 case LibFunc_atan:4623 case LibFunc_atanf:4624 case LibFunc_atanl:4625 return Intrinsic::atan;4626 case LibFunc_atan2:4627 case LibFunc_atan2f:4628 case LibFunc_atan2l:4629 return Intrinsic::atan2;4630 case LibFunc_sinh:4631 case LibFunc_sinhf:4632 case LibFunc_sinhl:4633 return Intrinsic::sinh;4634 case LibFunc_cosh:4635 case LibFunc_coshf:4636 case LibFunc_coshl:4637 return Intrinsic::cosh;4638 case LibFunc_tanh:4639 case LibFunc_tanhf:4640 case LibFunc_tanhl:4641 return Intrinsic::tanh;4642 case LibFunc_exp:4643 case LibFunc_expf:4644 case LibFunc_expl:4645 return Intrinsic::exp;4646 case LibFunc_exp2:4647 case LibFunc_exp2f:4648 case LibFunc_exp2l:4649 return Intrinsic::exp2;4650 case LibFunc_exp10:4651 case LibFunc_exp10f:4652 case LibFunc_exp10l:4653 return Intrinsic::exp10;4654 case LibFunc_log:4655 case LibFunc_logf:4656 case LibFunc_logl:4657 return Intrinsic::log;4658 case LibFunc_log10:4659 case LibFunc_log10f:4660 case LibFunc_log10l:4661 return Intrinsic::log10;4662 case LibFunc_log2:4663 case LibFunc_log2f:4664 case LibFunc_log2l:4665 return Intrinsic::log2;4666 case LibFunc_fabs:4667 case LibFunc_fabsf:4668 case LibFunc_fabsl:4669 return Intrinsic::fabs;4670 case LibFunc_fmin:4671 case LibFunc_fminf:4672 case LibFunc_fminl:4673 return Intrinsic::minnum;4674 case LibFunc_fmax:4675 case LibFunc_fmaxf:4676 case LibFunc_fmaxl:4677 return Intrinsic::maxnum;4678 case LibFunc_copysign:4679 case LibFunc_copysignf:4680 case LibFunc_copysignl:4681 return Intrinsic::copysign;4682 case LibFunc_floor:4683 case LibFunc_floorf:4684 case LibFunc_floorl:4685 return Intrinsic::floor;4686 case LibFunc_ceil:4687 case LibFunc_ceilf:4688 case LibFunc_ceill:4689 return Intrinsic::ceil;4690 case LibFunc_trunc:4691 case LibFunc_truncf:4692 case LibFunc_truncl:4693 return Intrinsic::trunc;4694 case LibFunc_rint:4695 case LibFunc_rintf:4696 case LibFunc_rintl:4697 return Intrinsic::rint;4698 case LibFunc_nearbyint:4699 case LibFunc_nearbyintf:4700 case LibFunc_nearbyintl:4701 return Intrinsic::nearbyint;4702 case LibFunc_round:4703 case LibFunc_roundf:4704 case LibFunc_roundl:4705 return Intrinsic::round;4706 case LibFunc_roundeven:4707 case LibFunc_roundevenf:4708 case LibFunc_roundevenl:4709 return Intrinsic::roundeven;4710 case LibFunc_pow:4711 case LibFunc_powf:4712 case LibFunc_powl:4713 return Intrinsic::pow;4714 case LibFunc_sqrt:4715 case LibFunc_sqrtf:4716 case LibFunc_sqrtl:4717 return Intrinsic::sqrt;4718 }4719 4720 return Intrinsic::not_intrinsic;4721}4722 4723static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {4724 Ty = Ty->getScalarType();4725 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());4726 return Mode.Output == DenormalMode::IEEE ||4727 Mode.Output == DenormalMode::PositiveZero;4728}4729/// Given an exploded icmp instruction, return true if the comparison only4730/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if4731/// the result of the comparison is true when the input value is signed.4732bool llvm::isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,4733 bool &TrueIfSigned) {4734 switch (Pred) {4735 case ICmpInst::ICMP_SLT: // True if LHS s< 04736 TrueIfSigned = true;4737 return RHS.isZero();4738 case ICmpInst::ICMP_SLE: // True if LHS s<= -14739 TrueIfSigned = true;4740 return RHS.isAllOnes();4741 case ICmpInst::ICMP_SGT: // True if LHS s> -14742 TrueIfSigned = false;4743 return RHS.isAllOnes();4744 case ICmpInst::ICMP_SGE: // True if LHS s>= 04745 TrueIfSigned = false;4746 return RHS.isZero();4747 case ICmpInst::ICMP_UGT:4748 // True if LHS u> RHS and RHS == sign-bit-mask - 14749 TrueIfSigned = true;4750 return RHS.isMaxSignedValue();4751 case ICmpInst::ICMP_UGE:4752 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)4753 TrueIfSigned = true;4754 return RHS.isMinSignedValue();4755 case ICmpInst::ICMP_ULT:4756 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)4757 TrueIfSigned = false;4758 return RHS.isMinSignedValue();4759 case ICmpInst::ICMP_ULE:4760 // True if LHS u<= RHS and RHS == sign-bit-mask - 14761 TrueIfSigned = false;4762 return RHS.isMaxSignedValue();4763 default:4764 return false;4765 }4766}4767 4768static void computeKnownFPClassFromCond(const Value *V, Value *Cond,4769 bool CondIsTrue,4770 const Instruction *CxtI,4771 KnownFPClass &KnownFromContext,4772 unsigned Depth = 0) {4773 Value *A, *B;4774 if (Depth < MaxAnalysisRecursionDepth &&4775 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))4776 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {4777 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,4778 Depth + 1);4779 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,4780 Depth + 1);4781 return;4782 }4783 if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A)))) {4784 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,4785 Depth + 1);4786 return;4787 }4788 CmpPredicate Pred;4789 Value *LHS;4790 uint64_t ClassVal = 0;4791 const APFloat *CRHS;4792 const APInt *RHS;4793 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {4794 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(4795 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);4796 if (CmpVal == V)4797 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));4798 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(4799 m_Specific(V), m_ConstantInt(ClassVal)))) {4800 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);4801 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);4802 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),4803 m_APInt(RHS)))) {4804 bool TrueIfSigned;4805 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))4806 return;4807 if (TrueIfSigned == CondIsTrue)4808 KnownFromContext.signBitMustBeOne();4809 else4810 KnownFromContext.signBitMustBeZero();4811 }4812}4813 4814static KnownFPClass computeKnownFPClassFromContext(const Value *V,4815 const SimplifyQuery &Q) {4816 KnownFPClass KnownFromContext;4817 4818 if (Q.CC && Q.CC->AffectedValues.contains(V))4819 computeKnownFPClassFromCond(V, Q.CC->Cond, !Q.CC->Invert, Q.CxtI,4820 KnownFromContext);4821 4822 if (!Q.CxtI)4823 return KnownFromContext;4824 4825 if (Q.DC && Q.DT) {4826 // Handle dominating conditions.4827 for (BranchInst *BI : Q.DC->conditionsFor(V)) {4828 Value *Cond = BI->getCondition();4829 4830 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));4831 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))4832 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,4833 KnownFromContext);4834 4835 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));4836 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))4837 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,4838 KnownFromContext);4839 }4840 }4841 4842 if (!Q.AC)4843 return KnownFromContext;4844 4845 // Try to restrict the floating-point classes based on information from4846 // assumptions.4847 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {4848 if (!AssumeVH)4849 continue;4850 CallInst *I = cast<CallInst>(AssumeVH);4851 4852 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&4853 "Got assumption for the wrong function!");4854 assert(I->getIntrinsicID() == Intrinsic::assume &&4855 "must be an assume intrinsic");4856 4857 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))4858 continue;4859 4860 computeKnownFPClassFromCond(V, I->getArgOperand(0),4861 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);4862 }4863 4864 return KnownFromContext;4865}4866 4867void computeKnownFPClass(const Value *V, const APInt &DemandedElts,4868 FPClassTest InterestedClasses, KnownFPClass &Known,4869 const SimplifyQuery &Q, unsigned Depth);4870 4871static void computeKnownFPClass(const Value *V, KnownFPClass &Known,4872 FPClassTest InterestedClasses,4873 const SimplifyQuery &Q, unsigned Depth) {4874 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());4875 APInt DemandedElts =4876 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);4877 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);4878}4879 4880static void computeKnownFPClassForFPTrunc(const Operator *Op,4881 const APInt &DemandedElts,4882 FPClassTest InterestedClasses,4883 KnownFPClass &Known,4884 const SimplifyQuery &Q,4885 unsigned Depth) {4886 if ((InterestedClasses &4887 (KnownFPClass::OrderedLessThanZeroMask | fcNan)) == fcNone)4888 return;4889 4890 KnownFPClass KnownSrc;4891 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,4892 KnownSrc, Q, Depth + 1);4893 4894 // Sign should be preserved4895 // TODO: Handle cannot be ordered greater than zero4896 if (KnownSrc.cannotBeOrderedLessThanZero())4897 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);4898 4899 Known.propagateNaN(KnownSrc, true);4900 4901 // Infinity needs a range check.4902}4903 4904void computeKnownFPClass(const Value *V, const APInt &DemandedElts,4905 FPClassTest InterestedClasses, KnownFPClass &Known,4906 const SimplifyQuery &Q, unsigned Depth) {4907 assert(Known.isUnknown() && "should not be called with known information");4908 4909 if (!DemandedElts) {4910 // No demanded elts, better to assume we don't know anything.4911 Known.resetAll();4912 return;4913 }4914 4915 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");4916 4917 if (auto *CFP = dyn_cast<ConstantFP>(V)) {4918 Known.KnownFPClasses = CFP->getValueAPF().classify();4919 Known.SignBit = CFP->isNegative();4920 return;4921 }4922 4923 if (isa<ConstantAggregateZero>(V)) {4924 Known.KnownFPClasses = fcPosZero;4925 Known.SignBit = false;4926 return;4927 }4928 4929 if (isa<PoisonValue>(V)) {4930 Known.KnownFPClasses = fcNone;4931 Known.SignBit = false;4932 return;4933 }4934 4935 // Try to handle fixed width vector constants4936 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());4937 const Constant *CV = dyn_cast<Constant>(V);4938 if (VFVTy && CV) {4939 Known.KnownFPClasses = fcNone;4940 bool SignBitAllZero = true;4941 bool SignBitAllOne = true;4942 4943 // For vectors, verify that each element is not NaN.4944 unsigned NumElts = VFVTy->getNumElements();4945 for (unsigned i = 0; i != NumElts; ++i) {4946 if (!DemandedElts[i])4947 continue;4948 4949 Constant *Elt = CV->getAggregateElement(i);4950 if (!Elt) {4951 Known = KnownFPClass();4952 return;4953 }4954 if (isa<PoisonValue>(Elt))4955 continue;4956 auto *CElt = dyn_cast<ConstantFP>(Elt);4957 if (!CElt) {4958 Known = KnownFPClass();4959 return;4960 }4961 4962 const APFloat &C = CElt->getValueAPF();4963 Known.KnownFPClasses |= C.classify();4964 if (C.isNegative())4965 SignBitAllZero = false;4966 else4967 SignBitAllOne = false;4968 }4969 if (SignBitAllOne != SignBitAllZero)4970 Known.SignBit = SignBitAllOne;4971 return;4972 }4973 4974 FPClassTest KnownNotFromFlags = fcNone;4975 if (const auto *CB = dyn_cast<CallBase>(V))4976 KnownNotFromFlags |= CB->getRetNoFPClass();4977 else if (const auto *Arg = dyn_cast<Argument>(V))4978 KnownNotFromFlags |= Arg->getNoFPClass();4979 4980 const Operator *Op = dyn_cast<Operator>(V);4981 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {4982 if (FPOp->hasNoNaNs())4983 KnownNotFromFlags |= fcNan;4984 if (FPOp->hasNoInfs())4985 KnownNotFromFlags |= fcInf;4986 }4987 4988 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);4989 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;4990 4991 // We no longer need to find out about these bits from inputs if we can4992 // assume this from flags/attributes.4993 InterestedClasses &= ~KnownNotFromFlags;4994 4995 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {4996 Known.knownNot(KnownNotFromFlags);4997 if (!Known.SignBit && AssumedClasses.SignBit) {4998 if (*AssumedClasses.SignBit)4999 Known.signBitMustBeOne();5000 else5001 Known.signBitMustBeZero();5002 }5003 });5004 5005 if (!Op)5006 return;5007 5008 // All recursive calls that increase depth must come after this.5009 if (Depth == MaxAnalysisRecursionDepth)5010 return;5011 5012 const unsigned Opc = Op->getOpcode();5013 switch (Opc) {5014 case Instruction::FNeg: {5015 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,5016 Known, Q, Depth + 1);5017 Known.fneg();5018 break;5019 }5020 case Instruction::Select: {5021 Value *Cond = Op->getOperand(0);5022 Value *LHS = Op->getOperand(1);5023 Value *RHS = Op->getOperand(2);5024 5025 FPClassTest FilterLHS = fcAllFlags;5026 FPClassTest FilterRHS = fcAllFlags;5027 5028 Value *TestedValue = nullptr;5029 FPClassTest MaskIfTrue = fcAllFlags;5030 FPClassTest MaskIfFalse = fcAllFlags;5031 uint64_t ClassVal = 0;5032 const Function *F = cast<Instruction>(Op)->getFunction();5033 CmpPredicate Pred;5034 Value *CmpLHS, *CmpRHS;5035 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {5036 // If the select filters out a value based on the class, it no longer5037 // participates in the class of the result5038 5039 // TODO: In some degenerate cases we can infer something if we try again5040 // without looking through sign operations.5041 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;5042 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =5043 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);5044 } else if (match(Cond,5045 m_Intrinsic<Intrinsic::is_fpclass>(5046 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {5047 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);5048 MaskIfTrue = TestedMask;5049 MaskIfFalse = ~TestedMask;5050 }5051 5052 if (TestedValue == LHS) {5053 // match !isnan(x) ? x : y5054 FilterLHS = MaskIfTrue;5055 } else if (TestedValue == RHS) { // && IsExactClass5056 // match !isnan(x) ? y : x5057 FilterRHS = MaskIfFalse;5058 }5059 5060 KnownFPClass Known2;5061 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,5062 Q, Depth + 1);5063 Known.KnownFPClasses &= FilterLHS;5064 5065 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,5066 Known2, Q, Depth + 1);5067 Known2.KnownFPClasses &= FilterRHS;5068 5069 Known |= Known2;5070 break;5071 }5072 case Instruction::Call: {5073 const CallInst *II = cast<CallInst>(Op);5074 const Intrinsic::ID IID = II->getIntrinsicID();5075 switch (IID) {5076 case Intrinsic::fabs: {5077 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {5078 // If we only care about the sign bit we don't need to inspect the5079 // operand.5080 computeKnownFPClass(II->getArgOperand(0), DemandedElts,5081 InterestedClasses, Known, Q, Depth + 1);5082 }5083 5084 Known.fabs();5085 break;5086 }5087 case Intrinsic::copysign: {5088 KnownFPClass KnownSign;5089 5090 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5091 Known, Q, Depth + 1);5092 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,5093 KnownSign, Q, Depth + 1);5094 Known.copysign(KnownSign);5095 break;5096 }5097 case Intrinsic::fma:5098 case Intrinsic::fmuladd: {5099 if ((InterestedClasses & fcNegative) == fcNone)5100 break;5101 5102 if (II->getArgOperand(0) != II->getArgOperand(1))5103 break;5104 5105 // The multiply cannot be -0 and therefore the add can't be -05106 Known.knownNot(fcNegZero);5107 5108 // x * x + y is non-negative if y is non-negative.5109 KnownFPClass KnownAddend;5110 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,5111 KnownAddend, Q, Depth + 1);5112 5113 if (KnownAddend.cannotBeOrderedLessThanZero())5114 Known.knownNot(fcNegative);5115 break;5116 }5117 case Intrinsic::sqrt:5118 case Intrinsic::experimental_constrained_sqrt: {5119 KnownFPClass KnownSrc;5120 FPClassTest InterestedSrcs = InterestedClasses;5121 if (InterestedClasses & fcNan)5122 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;5123 5124 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,5125 KnownSrc, Q, Depth + 1);5126 5127 if (KnownSrc.isKnownNeverPosInfinity())5128 Known.knownNot(fcPosInf);5129 if (KnownSrc.isKnownNever(fcSNan))5130 Known.knownNot(fcSNan);5131 5132 // Any negative value besides -0 returns a nan.5133 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())5134 Known.knownNot(fcNan);5135 5136 // The only negative value that can be returned is -0 for -0 inputs.5137 Known.knownNot(fcNegInf | fcNegSubnormal | fcNegNormal);5138 5139 // If the input denormal mode could be PreserveSign, a negative5140 // subnormal input could produce a negative zero output.5141 const Function *F = II->getFunction();5142 const fltSemantics &FltSem =5143 II->getType()->getScalarType()->getFltSemantics();5144 5145 if (Q.IIQ.hasNoSignedZeros(II) ||5146 (F &&5147 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))5148 Known.knownNot(fcNegZero);5149 5150 break;5151 }5152 case Intrinsic::sin:5153 case Intrinsic::cos: {5154 // Return NaN on infinite inputs.5155 KnownFPClass KnownSrc;5156 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5157 KnownSrc, Q, Depth + 1);5158 Known.knownNot(fcInf);5159 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())5160 Known.knownNot(fcNan);5161 break;5162 }5163 case Intrinsic::maxnum:5164 case Intrinsic::minnum:5165 case Intrinsic::minimum:5166 case Intrinsic::maximum:5167 case Intrinsic::minimumnum:5168 case Intrinsic::maximumnum: {5169 KnownFPClass KnownLHS, KnownRHS;5170 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5171 KnownLHS, Q, Depth + 1);5172 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,5173 KnownRHS, Q, Depth + 1);5174 5175 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();5176 Known = KnownLHS | KnownRHS;5177 5178 // If either operand is not NaN, the result is not NaN.5179 if (NeverNaN &&5180 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||5181 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))5182 Known.knownNot(fcNan);5183 5184 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {5185 // If at least one operand is known to be positive, the result must be5186 // positive.5187 if ((KnownLHS.cannotBeOrderedLessThanZero() &&5188 KnownLHS.isKnownNeverNaN()) ||5189 (KnownRHS.cannotBeOrderedLessThanZero() &&5190 KnownRHS.isKnownNeverNaN()))5191 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);5192 } else if (IID == Intrinsic::maximum) {5193 // If at least one operand is known to be positive, the result must be5194 // positive.5195 if (KnownLHS.cannotBeOrderedLessThanZero() ||5196 KnownRHS.cannotBeOrderedLessThanZero())5197 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);5198 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {5199 // If at least one operand is known to be negative, the result must be5200 // negative.5201 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&5202 KnownLHS.isKnownNeverNaN()) ||5203 (KnownRHS.cannotBeOrderedGreaterThanZero() &&5204 KnownRHS.isKnownNeverNaN()))5205 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);5206 } else if (IID == Intrinsic::minimum) {5207 // If at least one operand is known to be negative, the result must be5208 // negative.5209 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||5210 KnownRHS.cannotBeOrderedGreaterThanZero())5211 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);5212 } else5213 llvm_unreachable("unhandled intrinsic");5214 5215 // Fixup zero handling if denormals could be returned as a zero.5216 //5217 // As there's no spec for denormal flushing, be conservative with the5218 // treatment of denormals that could be flushed to zero. For older5219 // subtargets on AMDGPU the min/max instructions would not flush the5220 // output and return the original value.5221 //5222 if ((Known.KnownFPClasses & fcZero) != fcNone &&5223 !Known.isKnownNeverSubnormal()) {5224 const Function *Parent = II->getFunction();5225 if (!Parent)5226 break;5227 5228 DenormalMode Mode = Parent->getDenormalMode(5229 II->getType()->getScalarType()->getFltSemantics());5230 if (Mode != DenormalMode::getIEEE())5231 Known.KnownFPClasses |= fcZero;5232 }5233 5234 if (Known.isKnownNeverNaN()) {5235 if (KnownLHS.SignBit && KnownRHS.SignBit &&5236 *KnownLHS.SignBit == *KnownRHS.SignBit) {5237 if (*KnownLHS.SignBit)5238 Known.signBitMustBeOne();5239 else5240 Known.signBitMustBeZero();5241 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||5242 IID == Intrinsic::maximumnum ||5243 IID == Intrinsic::minimumnum) ||5244 // FIXME: Should be using logical zero versions5245 ((KnownLHS.isKnownNeverNegZero() ||5246 KnownRHS.isKnownNeverPosZero()) &&5247 (KnownLHS.isKnownNeverPosZero() ||5248 KnownRHS.isKnownNeverNegZero()))) {5249 // Don't take sign bit from NaN operands.5250 if (!KnownLHS.isKnownNeverNaN())5251 KnownLHS.SignBit = std::nullopt;5252 if (!KnownRHS.isKnownNeverNaN())5253 KnownRHS.SignBit = std::nullopt;5254 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||5255 IID == Intrinsic::maxnum) &&5256 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))5257 Known.signBitMustBeZero();5258 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||5259 IID == Intrinsic::minnum) &&5260 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))5261 Known.signBitMustBeOne();5262 }5263 }5264 break;5265 }5266 case Intrinsic::canonicalize: {5267 KnownFPClass KnownSrc;5268 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5269 KnownSrc, Q, Depth + 1);5270 5271 // This is essentially a stronger form of5272 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't5273 // actually have an IR canonicalization guarantee.5274 5275 // Canonicalize may flush denormals to zero, so we have to consider the5276 // denormal mode to preserve known-not-0 knowledge.5277 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;5278 5279 // Stronger version of propagateNaN5280 // Canonicalize is guaranteed to quiet signaling nans.5281 if (KnownSrc.isKnownNeverNaN())5282 Known.knownNot(fcNan);5283 else5284 Known.knownNot(fcSNan);5285 5286 const Function *F = II->getFunction();5287 if (!F)5288 break;5289 5290 // If the parent function flushes denormals, the canonical output cannot5291 // be a denormal.5292 const fltSemantics &FPType =5293 II->getType()->getScalarType()->getFltSemantics();5294 DenormalMode DenormMode = F->getDenormalMode(FPType);5295 if (DenormMode == DenormalMode::getIEEE()) {5296 if (KnownSrc.isKnownNever(fcPosZero))5297 Known.knownNot(fcPosZero);5298 if (KnownSrc.isKnownNever(fcNegZero))5299 Known.knownNot(fcNegZero);5300 break;5301 }5302 5303 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())5304 Known.knownNot(fcSubnormal);5305 5306 if (DenormMode.Input == DenormalMode::PositiveZero ||5307 (DenormMode.Output == DenormalMode::PositiveZero &&5308 DenormMode.Input == DenormalMode::IEEE))5309 Known.knownNot(fcNegZero);5310 5311 break;5312 }5313 case Intrinsic::vector_reduce_fmax:5314 case Intrinsic::vector_reduce_fmin:5315 case Intrinsic::vector_reduce_fmaximum:5316 case Intrinsic::vector_reduce_fminimum: {5317 // reduce min/max will choose an element from one of the vector elements,5318 // so we can infer and class information that is common to all elements.5319 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),5320 InterestedClasses, Q, Depth + 1);5321 // Can only propagate sign if output is never NaN.5322 if (!Known.isKnownNeverNaN())5323 Known.SignBit.reset();5324 break;5325 }5326 // reverse preserves all characteristics of the input vec's element.5327 case Intrinsic::vector_reverse:5328 Known = computeKnownFPClass(5329 II->getArgOperand(0), DemandedElts.reverseBits(),5330 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);5331 break;5332 case Intrinsic::trunc:5333 case Intrinsic::floor:5334 case Intrinsic::ceil:5335 case Intrinsic::rint:5336 case Intrinsic::nearbyint:5337 case Intrinsic::round:5338 case Intrinsic::roundeven: {5339 KnownFPClass KnownSrc;5340 FPClassTest InterestedSrcs = InterestedClasses;5341 if (InterestedSrcs & fcPosFinite)5342 InterestedSrcs |= fcPosFinite;5343 if (InterestedSrcs & fcNegFinite)5344 InterestedSrcs |= fcNegFinite;5345 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,5346 KnownSrc, Q, Depth + 1);5347 5348 // Integer results cannot be subnormal.5349 Known.knownNot(fcSubnormal);5350 5351 Known.propagateNaN(KnownSrc, true);5352 5353 // Pass through infinities, except PPC_FP128 is a special case for5354 // intrinsics other than trunc.5355 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {5356 if (KnownSrc.isKnownNeverPosInfinity())5357 Known.knownNot(fcPosInf);5358 if (KnownSrc.isKnownNeverNegInfinity())5359 Known.knownNot(fcNegInf);5360 }5361 5362 // Negative round ups to 0 produce -05363 if (KnownSrc.isKnownNever(fcPosFinite))5364 Known.knownNot(fcPosFinite);5365 if (KnownSrc.isKnownNever(fcNegFinite))5366 Known.knownNot(fcNegFinite);5367 5368 break;5369 }5370 case Intrinsic::exp:5371 case Intrinsic::exp2:5372 case Intrinsic::exp10: {5373 Known.knownNot(fcNegative);5374 if ((InterestedClasses & fcNan) == fcNone)5375 break;5376 5377 KnownFPClass KnownSrc;5378 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5379 KnownSrc, Q, Depth + 1);5380 if (KnownSrc.isKnownNeverNaN()) {5381 Known.knownNot(fcNan);5382 Known.signBitMustBeZero();5383 }5384 5385 break;5386 }5387 case Intrinsic::fptrunc_round: {5388 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,5389 Q, Depth);5390 break;5391 }5392 case Intrinsic::log:5393 case Intrinsic::log10:5394 case Intrinsic::log2:5395 case Intrinsic::experimental_constrained_log:5396 case Intrinsic::experimental_constrained_log10:5397 case Intrinsic::experimental_constrained_log2: {5398 // log(+inf) -> +inf5399 // log([+-]0.0) -> -inf5400 // log(-inf) -> nan5401 // log(-x) -> nan5402 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)5403 break;5404 5405 FPClassTest InterestedSrcs = InterestedClasses;5406 if ((InterestedClasses & fcNegInf) != fcNone)5407 InterestedSrcs |= fcZero | fcSubnormal;5408 if ((InterestedClasses & fcNan) != fcNone)5409 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);5410 5411 KnownFPClass KnownSrc;5412 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,5413 KnownSrc, Q, Depth + 1);5414 5415 if (KnownSrc.isKnownNeverPosInfinity())5416 Known.knownNot(fcPosInf);5417 5418 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())5419 Known.knownNot(fcNan);5420 5421 const Function *F = II->getFunction();5422 5423 if (!F)5424 break;5425 5426 const fltSemantics &FltSem =5427 II->getType()->getScalarType()->getFltSemantics();5428 DenormalMode Mode = F->getDenormalMode(FltSem);5429 5430 if (KnownSrc.isKnownNeverLogicalZero(Mode))5431 Known.knownNot(fcNegInf);5432 5433 break;5434 }5435 case Intrinsic::powi: {5436 if ((InterestedClasses & fcNegative) == fcNone)5437 break;5438 5439 const Value *Exp = II->getArgOperand(1);5440 Type *ExpTy = Exp->getType();5441 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();5442 KnownBits ExponentKnownBits(BitWidth);5443 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),5444 ExponentKnownBits, Q, Depth + 1);5445 5446 if (ExponentKnownBits.Zero[0]) { // Is even5447 Known.knownNot(fcNegative);5448 break;5449 }5450 5451 // Given that exp is an integer, here are the5452 // ways that pow can return a negative value:5453 //5454 // pow(-x, exp) --> negative if exp is odd and x is negative.5455 // pow(-0, exp) --> -inf if exp is negative odd.5456 // pow(-0, exp) --> -0 if exp is positive odd.5457 // pow(-inf, exp) --> -0 if exp is negative odd.5458 // pow(-inf, exp) --> -inf if exp is positive odd.5459 KnownFPClass KnownSrc;5460 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,5461 KnownSrc, Q, Depth + 1);5462 if (KnownSrc.isKnownNever(fcNegative))5463 Known.knownNot(fcNegative);5464 break;5465 }5466 case Intrinsic::ldexp: {5467 KnownFPClass KnownSrc;5468 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5469 KnownSrc, Q, Depth + 1);5470 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);5471 5472 // Sign is preserved, but underflows may produce zeroes.5473 if (KnownSrc.isKnownNever(fcNegative))5474 Known.knownNot(fcNegative);5475 else if (KnownSrc.cannotBeOrderedLessThanZero())5476 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);5477 5478 if (KnownSrc.isKnownNever(fcPositive))5479 Known.knownNot(fcPositive);5480 else if (KnownSrc.cannotBeOrderedGreaterThanZero())5481 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);5482 5483 // Can refine inf/zero handling based on the exponent operand.5484 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;5485 if ((InterestedClasses & ExpInfoMask) == fcNone)5486 break;5487 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)5488 break;5489 5490 const fltSemantics &Flt =5491 II->getType()->getScalarType()->getFltSemantics();5492 unsigned Precision = APFloat::semanticsPrecision(Flt);5493 const Value *ExpArg = II->getArgOperand(1);5494 ConstantRange ExpRange = computeConstantRange(5495 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);5496 5497 const int MantissaBits = Precision - 1;5498 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))5499 Known.knownNot(fcSubnormal);5500 5501 const Function *F = II->getFunction();5502 const APInt *ConstVal = ExpRange.getSingleElement();5503 const fltSemantics &FltSem =5504 II->getType()->getScalarType()->getFltSemantics();5505 if (ConstVal && ConstVal->isZero()) {5506 // ldexp(x, 0) -> x, so propagate everything.5507 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));5508 } else if (ExpRange.isAllNegative()) {5509 // If we know the power is <= 0, can't introduce inf5510 if (KnownSrc.isKnownNeverPosInfinity())5511 Known.knownNot(fcPosInf);5512 if (KnownSrc.isKnownNeverNegInfinity())5513 Known.knownNot(fcNegInf);5514 } else if (ExpRange.isAllNonNegative()) {5515 // If we know the power is >= 0, can't introduce subnormal or zero5516 if (KnownSrc.isKnownNeverPosSubnormal())5517 Known.knownNot(fcPosSubnormal);5518 if (KnownSrc.isKnownNeverNegSubnormal())5519 Known.knownNot(fcNegSubnormal);5520 if (F &&5521 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))5522 Known.knownNot(fcPosZero);5523 if (F &&5524 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))5525 Known.knownNot(fcNegZero);5526 }5527 5528 break;5529 }5530 case Intrinsic::arithmetic_fence: {5531 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,5532 Known, Q, Depth + 1);5533 break;5534 }5535 case Intrinsic::experimental_constrained_sitofp:5536 case Intrinsic::experimental_constrained_uitofp:5537 // Cannot produce nan5538 Known.knownNot(fcNan);5539 5540 // sitofp and uitofp turn into +0.0 for zero.5541 Known.knownNot(fcNegZero);5542 5543 // Integers cannot be subnormal5544 Known.knownNot(fcSubnormal);5545 5546 if (IID == Intrinsic::experimental_constrained_uitofp)5547 Known.signBitMustBeZero();5548 5549 // TODO: Copy inf handling from instructions5550 break;5551 default:5552 break;5553 }5554 5555 break;5556 }5557 case Instruction::FAdd:5558 case Instruction::FSub: {5559 KnownFPClass KnownLHS, KnownRHS;5560 bool WantNegative =5561 Op->getOpcode() == Instruction::FAdd &&5562 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;5563 bool WantNaN = (InterestedClasses & fcNan) != fcNone;5564 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;5565 5566 if (!WantNaN && !WantNegative && !WantNegZero)5567 break;5568 5569 FPClassTest InterestedSrcs = InterestedClasses;5570 if (WantNegative)5571 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;5572 if (InterestedClasses & fcNan)5573 InterestedSrcs |= fcInf;5574 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,5575 KnownRHS, Q, Depth + 1);5576 5577 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||5578 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||5579 WantNegZero || Opc == Instruction::FSub) {5580 5581 // RHS is canonically cheaper to compute. Skip inspecting the LHS if5582 // there's no point.5583 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,5584 KnownLHS, Q, Depth + 1);5585 // Adding positive and negative infinity produces NaN.5586 // TODO: Check sign of infinities.5587 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&5588 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))5589 Known.knownNot(fcNan);5590 5591 // FIXME: Context function should always be passed in separately5592 const Function *F = cast<Instruction>(Op)->getFunction();5593 5594 if (Op->getOpcode() == Instruction::FAdd) {5595 if (KnownLHS.cannotBeOrderedLessThanZero() &&5596 KnownRHS.cannotBeOrderedLessThanZero())5597 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);5598 if (!F)5599 break;5600 5601 const fltSemantics &FltSem =5602 Op->getType()->getScalarType()->getFltSemantics();5603 DenormalMode Mode = F->getDenormalMode(FltSem);5604 5605 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.5606 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||5607 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&5608 // Make sure output negative denormal can't flush to -05609 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))5610 Known.knownNot(fcNegZero);5611 } else {5612 if (!F)5613 break;5614 5615 const fltSemantics &FltSem =5616 Op->getType()->getScalarType()->getFltSemantics();5617 DenormalMode Mode = F->getDenormalMode(FltSem);5618 5619 // Only fsub -0, +0 can return -05620 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||5621 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&5622 // Make sure output negative denormal can't flush to -05623 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))5624 Known.knownNot(fcNegZero);5625 }5626 }5627 5628 break;5629 }5630 case Instruction::FMul: {5631 // X * X is always non-negative or a NaN.5632 if (Op->getOperand(0) == Op->getOperand(1))5633 Known.knownNot(fcNegative);5634 5635 if ((InterestedClasses & fcNan) != fcNan)5636 break;5637 5638 // fcSubnormal is only needed in case of DAZ.5639 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;5640 5641 KnownFPClass KnownLHS, KnownRHS;5642 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,5643 Q, Depth + 1);5644 if (!KnownRHS.isKnownNeverNaN())5645 break;5646 5647 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,5648 Q, Depth + 1);5649 if (!KnownLHS.isKnownNeverNaN())5650 break;5651 5652 if (KnownLHS.SignBit && KnownRHS.SignBit) {5653 if (*KnownLHS.SignBit == *KnownRHS.SignBit)5654 Known.signBitMustBeZero();5655 else5656 Known.signBitMustBeOne();5657 }5658 5659 // If 0 * +/-inf produces NaN.5660 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {5661 Known.knownNot(fcNan);5662 break;5663 }5664 5665 const Function *F = cast<Instruction>(Op)->getFunction();5666 if (!F)5667 break;5668 5669 Type *OpTy = Op->getType()->getScalarType();5670 const fltSemantics &FltSem = OpTy->getFltSemantics();5671 DenormalMode Mode = F->getDenormalMode(FltSem);5672 5673 if ((KnownRHS.isKnownNeverInfinity() ||5674 KnownLHS.isKnownNeverLogicalZero(Mode)) &&5675 (KnownLHS.isKnownNeverInfinity() ||5676 KnownRHS.isKnownNeverLogicalZero(Mode)))5677 Known.knownNot(fcNan);5678 5679 break;5680 }5681 case Instruction::FDiv:5682 case Instruction::FRem: {5683 if (Op->getOperand(0) == Op->getOperand(1)) {5684 // TODO: Could filter out snan if we inspect the operand5685 if (Op->getOpcode() == Instruction::FDiv) {5686 // X / X is always exactly 1.0 or a NaN.5687 Known.KnownFPClasses = fcNan | fcPosNormal;5688 } else {5689 // X % X is always exactly [+-]0.0 or a NaN.5690 Known.KnownFPClasses = fcNan | fcZero;5691 }5692 5693 break;5694 }5695 5696 const bool WantNan = (InterestedClasses & fcNan) != fcNone;5697 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;5698 const bool WantPositive =5699 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;5700 if (!WantNan && !WantNegative && !WantPositive)5701 break;5702 5703 KnownFPClass KnownLHS, KnownRHS;5704 5705 computeKnownFPClass(Op->getOperand(1), DemandedElts,5706 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,5707 Depth + 1);5708 5709 bool KnowSomethingUseful =5710 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);5711 5712 if (KnowSomethingUseful || WantPositive) {5713 const FPClassTest InterestedLHS =5714 WantPositive ? fcAllFlags5715 : fcNan | fcInf | fcZero | fcSubnormal | fcNegative;5716 5717 computeKnownFPClass(Op->getOperand(0), DemandedElts,5718 InterestedClasses & InterestedLHS, KnownLHS, Q,5719 Depth + 1);5720 }5721 5722 const Function *F = cast<Instruction>(Op)->getFunction();5723 const fltSemantics &FltSem =5724 Op->getType()->getScalarType()->getFltSemantics();5725 5726 if (Op->getOpcode() == Instruction::FDiv) {5727 // Only 0/0, Inf/Inf produce NaN.5728 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&5729 (KnownLHS.isKnownNeverInfinity() ||5730 KnownRHS.isKnownNeverInfinity()) &&5731 ((F &&5732 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||5733 (F &&5734 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {5735 Known.knownNot(fcNan);5736 }5737 5738 // X / -0.0 is -Inf (or NaN).5739 // +X / +X is +X5740 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))5741 Known.knownNot(fcNegative);5742 } else {5743 // Inf REM x and x REM 0 produce NaN.5744 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&5745 KnownLHS.isKnownNeverInfinity() && F &&5746 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {5747 Known.knownNot(fcNan);5748 }5749 5750 // The sign for frem is the same as the first operand.5751 if (KnownLHS.cannotBeOrderedLessThanZero())5752 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);5753 if (KnownLHS.cannotBeOrderedGreaterThanZero())5754 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);5755 5756 // See if we can be more aggressive about the sign of 0.5757 if (KnownLHS.isKnownNever(fcNegative))5758 Known.knownNot(fcNegative);5759 if (KnownLHS.isKnownNever(fcPositive))5760 Known.knownNot(fcPositive);5761 }5762 5763 break;5764 }5765 case Instruction::FPExt: {5766 // Infinity, nan and zero propagate from source.5767 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,5768 Known, Q, Depth + 1);5769 5770 const fltSemantics &DstTy =5771 Op->getType()->getScalarType()->getFltSemantics();5772 const fltSemantics &SrcTy =5773 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();5774 5775 // All subnormal inputs should be in the normal range in the result type.5776 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {5777 if (Known.KnownFPClasses & fcPosSubnormal)5778 Known.KnownFPClasses |= fcPosNormal;5779 if (Known.KnownFPClasses & fcNegSubnormal)5780 Known.KnownFPClasses |= fcNegNormal;5781 Known.knownNot(fcSubnormal);5782 }5783 5784 // Sign bit of a nan isn't guaranteed.5785 if (!Known.isKnownNeverNaN())5786 Known.SignBit = std::nullopt;5787 break;5788 }5789 case Instruction::FPTrunc: {5790 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,5791 Depth);5792 break;5793 }5794 case Instruction::SIToFP:5795 case Instruction::UIToFP: {5796 // Cannot produce nan5797 Known.knownNot(fcNan);5798 5799 // Integers cannot be subnormal5800 Known.knownNot(fcSubnormal);5801 5802 // sitofp and uitofp turn into +0.0 for zero.5803 Known.knownNot(fcNegZero);5804 if (Op->getOpcode() == Instruction::UIToFP)5805 Known.signBitMustBeZero();5806 5807 if (InterestedClasses & fcInf) {5808 // Get width of largest magnitude integer (remove a bit if signed).5809 // This still works for a signed minimum value because the largest FP5810 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).5811 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();5812 if (Op->getOpcode() == Instruction::SIToFP)5813 --IntSize;5814 5815 // If the exponent of the largest finite FP value can hold the largest5816 // integer, the result of the cast must be finite.5817 Type *FPTy = Op->getType()->getScalarType();5818 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)5819 Known.knownNot(fcInf);5820 }5821 5822 break;5823 }5824 case Instruction::ExtractElement: {5825 // Look through extract element. If the index is non-constant or5826 // out-of-range demand all elements, otherwise just the extracted element.5827 const Value *Vec = Op->getOperand(0);5828 5829 APInt DemandedVecElts;5830 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {5831 unsigned NumElts = VecTy->getNumElements();5832 DemandedVecElts = APInt::getAllOnes(NumElts);5833 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));5834 if (CIdx && CIdx->getValue().ult(NumElts))5835 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());5836 } else {5837 DemandedVecElts = APInt(1, 1);5838 }5839 5840 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,5841 Q, Depth + 1);5842 }5843 case Instruction::InsertElement: {5844 if (isa<ScalableVectorType>(Op->getType()))5845 return;5846 5847 const Value *Vec = Op->getOperand(0);5848 const Value *Elt = Op->getOperand(1);5849 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));5850 unsigned NumElts = DemandedElts.getBitWidth();5851 APInt DemandedVecElts = DemandedElts;5852 bool NeedsElt = true;5853 // If we know the index we are inserting to, clear it from Vec check.5854 if (CIdx && CIdx->getValue().ult(NumElts)) {5855 DemandedVecElts.clearBit(CIdx->getZExtValue());5856 NeedsElt = DemandedElts[CIdx->getZExtValue()];5857 }5858 5859 // Do we demand the inserted element?5860 if (NeedsElt) {5861 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);5862 // If we don't know any bits, early out.5863 if (Known.isUnknown())5864 break;5865 } else {5866 Known.KnownFPClasses = fcNone;5867 }5868 5869 // Do we need anymore elements from Vec?5870 if (!DemandedVecElts.isZero()) {5871 KnownFPClass Known2;5872 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,5873 Depth + 1);5874 Known |= Known2;5875 }5876 5877 break;5878 }5879 case Instruction::ShuffleVector: {5880 // For undef elements, we don't know anything about the common state of5881 // the shuffle result.5882 APInt DemandedLHS, DemandedRHS;5883 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);5884 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))5885 return;5886 5887 if (!!DemandedLHS) {5888 const Value *LHS = Shuf->getOperand(0);5889 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,5890 Depth + 1);5891 5892 // If we don't know any bits, early out.5893 if (Known.isUnknown())5894 break;5895 } else {5896 Known.KnownFPClasses = fcNone;5897 }5898 5899 if (!!DemandedRHS) {5900 KnownFPClass Known2;5901 const Value *RHS = Shuf->getOperand(1);5902 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,5903 Depth + 1);5904 Known |= Known2;5905 }5906 5907 break;5908 }5909 case Instruction::ExtractValue: {5910 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);5911 ArrayRef<unsigned> Indices = Extract->getIndices();5912 const Value *Src = Extract->getAggregateOperand();5913 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&5914 Indices[0] == 0) {5915 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {5916 switch (II->getIntrinsicID()) {5917 case Intrinsic::frexp: {5918 Known.knownNot(fcSubnormal);5919 5920 KnownFPClass KnownSrc;5921 computeKnownFPClass(II->getArgOperand(0), DemandedElts,5922 InterestedClasses, KnownSrc, Q, Depth + 1);5923 5924 const Function *F = cast<Instruction>(Op)->getFunction();5925 const fltSemantics &FltSem =5926 Op->getType()->getScalarType()->getFltSemantics();5927 5928 if (KnownSrc.isKnownNever(fcNegative))5929 Known.knownNot(fcNegative);5930 else {5931 if (F &&5932 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))5933 Known.knownNot(fcNegZero);5934 if (KnownSrc.isKnownNever(fcNegInf))5935 Known.knownNot(fcNegInf);5936 }5937 5938 if (KnownSrc.isKnownNever(fcPositive))5939 Known.knownNot(fcPositive);5940 else {5941 if (F &&5942 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))5943 Known.knownNot(fcPosZero);5944 if (KnownSrc.isKnownNever(fcPosInf))5945 Known.knownNot(fcPosInf);5946 }5947 5948 Known.propagateNaN(KnownSrc);5949 return;5950 }5951 default:5952 break;5953 }5954 }5955 }5956 5957 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,5958 Depth + 1);5959 break;5960 }5961 case Instruction::PHI: {5962 const PHINode *P = cast<PHINode>(Op);5963 // Unreachable blocks may have zero-operand PHI nodes.5964 if (P->getNumIncomingValues() == 0)5965 break;5966 5967 // Otherwise take the unions of the known bit sets of the operands,5968 // taking conservative care to avoid excessive recursion.5969 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;5970 5971 if (Depth < PhiRecursionLimit) {5972 // Skip if every incoming value references to ourself.5973 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))5974 break;5975 5976 bool First = true;5977 5978 for (const Use &U : P->operands()) {5979 Value *IncValue;5980 Instruction *CxtI;5981 breakSelfRecursivePHI(&U, P, IncValue, CxtI);5982 // Skip direct self references.5983 if (IncValue == P)5984 continue;5985 5986 KnownFPClass KnownSrc;5987 // Recurse, but cap the recursion to two levels, because we don't want5988 // to waste time spinning around in loops. We need at least depth 2 to5989 // detect known sign bits.5990 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,5991 Q.getWithoutCondContext().getWithInstruction(CxtI),5992 PhiRecursionLimit);5993 5994 if (First) {5995 Known = KnownSrc;5996 First = false;5997 } else {5998 Known |= KnownSrc;5999 }6000 6001 if (Known.KnownFPClasses == fcAllFlags)6002 break;6003 }6004 }6005 6006 break;6007 }6008 case Instruction::BitCast: {6009 const Value *Src;6010 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||6011 !Src->getType()->isIntOrIntVectorTy())6012 break;6013 6014 const Type *Ty = Op->getType()->getScalarType();6015 KnownBits Bits(Ty->getScalarSizeInBits());6016 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);6017 6018 // Transfer information from the sign bit.6019 if (Bits.isNonNegative())6020 Known.signBitMustBeZero();6021 else if (Bits.isNegative())6022 Known.signBitMustBeOne();6023 6024 if (Ty->isIEEELikeFPTy()) {6025 // IEEE floats are NaN when all bits of the exponent plus at least one of6026 // the fraction bits are 1. This means:6027 // - If we assume unknown bits are 0 and the value is NaN, it will6028 // always be NaN6029 // - If we assume unknown bits are 1 and the value is not NaN, it can6030 // never be NaN6031 // Note: They do not hold for x86_fp80 format.6032 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())6033 Known.KnownFPClasses = fcNan;6034 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())6035 Known.knownNot(fcNan);6036 6037 // Build KnownBits representing Inf and check if it must be equal or6038 // unequal to this value.6039 auto InfKB = KnownBits::makeConstant(6040 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());6041 InfKB.Zero.clearSignBit();6042 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {6043 assert(!InfResult.value());6044 Known.knownNot(fcInf);6045 } else if (Bits == InfKB) {6046 Known.KnownFPClasses = fcInf;6047 }6048 6049 // Build KnownBits representing Zero and check if it must be equal or6050 // unequal to this value.6051 auto ZeroKB = KnownBits::makeConstant(6052 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());6053 ZeroKB.Zero.clearSignBit();6054 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {6055 assert(!ZeroResult.value());6056 Known.knownNot(fcZero);6057 } else if (Bits == ZeroKB) {6058 Known.KnownFPClasses = fcZero;6059 }6060 }6061 6062 break;6063 }6064 default:6065 break;6066 }6067}6068 6069KnownFPClass llvm::computeKnownFPClass(const Value *V,6070 const APInt &DemandedElts,6071 FPClassTest InterestedClasses,6072 const SimplifyQuery &SQ,6073 unsigned Depth) {6074 KnownFPClass KnownClasses;6075 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,6076 Depth);6077 return KnownClasses;6078}6079 6080KnownFPClass llvm::computeKnownFPClass(const Value *V,6081 FPClassTest InterestedClasses,6082 const SimplifyQuery &SQ,6083 unsigned Depth) {6084 KnownFPClass Known;6085 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);6086 return Known;6087}6088 6089KnownFPClass llvm::computeKnownFPClass(6090 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,6091 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,6092 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {6093 return computeKnownFPClass(V, InterestedClasses,6094 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),6095 Depth);6096}6097 6098KnownFPClass6099llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,6100 FastMathFlags FMF, FPClassTest InterestedClasses,6101 const SimplifyQuery &SQ, unsigned Depth) {6102 if (FMF.noNaNs())6103 InterestedClasses &= ~fcNan;6104 if (FMF.noInfs())6105 InterestedClasses &= ~fcInf;6106 6107 KnownFPClass Result =6108 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);6109 6110 if (FMF.noNaNs())6111 Result.KnownFPClasses &= ~fcNan;6112 if (FMF.noInfs())6113 Result.KnownFPClasses &= ~fcInf;6114 return Result;6115}6116 6117KnownFPClass llvm::computeKnownFPClass(const Value *V, FastMathFlags FMF,6118 FPClassTest InterestedClasses,6119 const SimplifyQuery &SQ,6120 unsigned Depth) {6121 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());6122 APInt DemandedElts =6123 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);6124 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,6125 Depth);6126}6127 6128bool llvm::cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ,6129 unsigned Depth) {6130 KnownFPClass Known = computeKnownFPClass(V, fcNegZero, SQ, Depth);6131 return Known.isKnownNeverNegZero();6132}6133 6134bool llvm::cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ,6135 unsigned Depth) {6136 KnownFPClass Known =6137 computeKnownFPClass(V, KnownFPClass::OrderedLessThanZeroMask, SQ, Depth);6138 return Known.cannotBeOrderedLessThanZero();6139}6140 6141bool llvm::isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ,6142 unsigned Depth) {6143 KnownFPClass Known = computeKnownFPClass(V, fcInf, SQ, Depth);6144 return Known.isKnownNeverInfinity();6145}6146 6147/// Return true if the floating-point value can never contain a NaN or infinity.6148bool llvm::isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ,6149 unsigned Depth) {6150 KnownFPClass Known = computeKnownFPClass(V, fcInf | fcNan, SQ, Depth);6151 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();6152}6153 6154/// Return true if the floating-point scalar value is not a NaN or if the6155/// floating-point vector value has no NaN elements. Return false if a value6156/// could ever be NaN.6157bool llvm::isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ,6158 unsigned Depth) {6159 KnownFPClass Known = computeKnownFPClass(V, fcNan, SQ, Depth);6160 return Known.isKnownNeverNaN();6161}6162 6163/// Return false if we can prove that the specified FP value's sign bit is 0.6164/// Return true if we can prove that the specified FP value's sign bit is 1.6165/// Otherwise return std::nullopt.6166std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,6167 const SimplifyQuery &SQ,6168 unsigned Depth) {6169 KnownFPClass Known = computeKnownFPClass(V, fcAllFlags, SQ, Depth);6170 return Known.SignBit;6171}6172 6173bool llvm::canIgnoreSignBitOfZero(const Use &U) {6174 auto *User = cast<Instruction>(U.getUser());6175 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {6176 if (FPOp->hasNoSignedZeros())6177 return true;6178 }6179 6180 switch (User->getOpcode()) {6181 case Instruction::FPToSI:6182 case Instruction::FPToUI:6183 return true;6184 case Instruction::FCmp:6185 // fcmp treats both positive and negative zero as equal.6186 return true;6187 case Instruction::Call:6188 if (auto *II = dyn_cast<IntrinsicInst>(User)) {6189 switch (II->getIntrinsicID()) {6190 case Intrinsic::fabs:6191 return true;6192 case Intrinsic::copysign:6193 return U.getOperandNo() == 0;6194 case Intrinsic::is_fpclass:6195 case Intrinsic::vp_is_fpclass: {6196 auto Test =6197 static_cast<FPClassTest>(6198 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &6199 FPClassTest::fcZero;6200 return Test == FPClassTest::fcZero || Test == FPClassTest::fcNone;6201 }6202 default:6203 return false;6204 }6205 }6206 return false;6207 default:6208 return false;6209 }6210}6211 6212bool llvm::canIgnoreSignBitOfNaN(const Use &U) {6213 auto *User = cast<Instruction>(U.getUser());6214 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {6215 if (FPOp->hasNoNaNs())6216 return true;6217 }6218 6219 switch (User->getOpcode()) {6220 case Instruction::FPToSI:6221 case Instruction::FPToUI:6222 return true;6223 // Proper FP math operations ignore the sign bit of NaN.6224 case Instruction::FAdd:6225 case Instruction::FSub:6226 case Instruction::FMul:6227 case Instruction::FDiv:6228 case Instruction::FRem:6229 case Instruction::FPTrunc:6230 case Instruction::FPExt:6231 case Instruction::FCmp:6232 return true;6233 // Bitwise FP operations should preserve the sign bit of NaN.6234 case Instruction::FNeg:6235 case Instruction::Select:6236 case Instruction::PHI:6237 return false;6238 case Instruction::Ret:6239 return User->getFunction()->getAttributes().getRetNoFPClass() &6240 FPClassTest::fcNan;6241 case Instruction::Call:6242 case Instruction::Invoke: {6243 if (auto *II = dyn_cast<IntrinsicInst>(User)) {6244 switch (II->getIntrinsicID()) {6245 case Intrinsic::fabs:6246 return true;6247 case Intrinsic::copysign:6248 return U.getOperandNo() == 0;6249 // Other proper FP math intrinsics ignore the sign bit of NaN.6250 case Intrinsic::maxnum:6251 case Intrinsic::minnum:6252 case Intrinsic::maximum:6253 case Intrinsic::minimum:6254 case Intrinsic::maximumnum:6255 case Intrinsic::minimumnum:6256 case Intrinsic::canonicalize:6257 case Intrinsic::fma:6258 case Intrinsic::fmuladd:6259 case Intrinsic::sqrt:6260 case Intrinsic::pow:6261 case Intrinsic::powi:6262 case Intrinsic::fptoui_sat:6263 case Intrinsic::fptosi_sat:6264 case Intrinsic::is_fpclass:6265 case Intrinsic::vp_is_fpclass:6266 return true;6267 default:6268 return false;6269 }6270 }6271 6272 FPClassTest NoFPClass =6273 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());6274 return NoFPClass & FPClassTest::fcNan;6275 }6276 default:6277 return false;6278 }6279}6280 6281Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {6282 6283 // All byte-wide stores are splatable, even of arbitrary variables.6284 if (V->getType()->isIntegerTy(8))6285 return V;6286 6287 LLVMContext &Ctx = V->getContext();6288 6289 // Undef don't care.6290 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));6291 if (isa<UndefValue>(V))6292 return UndefInt8;6293 6294 // Return poison for zero-sized type.6295 if (DL.getTypeStoreSize(V->getType()).isZero())6296 return PoisonValue::get(Type::getInt8Ty(Ctx));6297 6298 Constant *C = dyn_cast<Constant>(V);6299 if (!C) {6300 // Conceptually, we could handle things like:6301 // %a = zext i8 %X to i166302 // %b = shl i16 %a, 86303 // %c = or i16 %a, %b6304 // but until there is an example that actually needs this, it doesn't seem6305 // worth worrying about.6306 return nullptr;6307 }6308 6309 // Handle 'null' ConstantArrayZero etc.6310 if (C->isNullValue())6311 return Constant::getNullValue(Type::getInt8Ty(Ctx));6312 6313 // Constant floating-point values can be handled as integer values if the6314 // corresponding integer value is "byteable". An important case is 0.0.6315 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {6316 Type *Ty = nullptr;6317 if (CFP->getType()->isHalfTy())6318 Ty = Type::getInt16Ty(Ctx);6319 else if (CFP->getType()->isFloatTy())6320 Ty = Type::getInt32Ty(Ctx);6321 else if (CFP->getType()->isDoubleTy())6322 Ty = Type::getInt64Ty(Ctx);6323 // Don't handle long double formats, which have strange constraints.6324 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)6325 : nullptr;6326 }6327 6328 // We can handle constant integers that are multiple of 8 bits.6329 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {6330 if (CI->getBitWidth() % 8 == 0) {6331 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");6332 if (!CI->getValue().isSplat(8))6333 return nullptr;6334 return ConstantInt::get(Ctx, CI->getValue().trunc(8));6335 }6336 }6337 6338 if (auto *CE = dyn_cast<ConstantExpr>(C)) {6339 if (CE->getOpcode() == Instruction::IntToPtr) {6340 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {6341 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());6342 if (Constant *Op = ConstantFoldIntegerCast(6343 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))6344 return isBytewiseValue(Op, DL);6345 }6346 }6347 }6348 6349 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {6350 if (LHS == RHS)6351 return LHS;6352 if (!LHS || !RHS)6353 return nullptr;6354 if (LHS == UndefInt8)6355 return RHS;6356 if (RHS == UndefInt8)6357 return LHS;6358 return nullptr;6359 };6360 6361 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {6362 Value *Val = UndefInt8;6363 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)6364 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))6365 return nullptr;6366 return Val;6367 }6368 6369 if (isa<ConstantAggregate>(C)) {6370 Value *Val = UndefInt8;6371 for (Value *Op : C->operands())6372 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))6373 return nullptr;6374 return Val;6375 }6376 6377 // Don't try to handle the handful of other constants.6378 return nullptr;6379}6380 6381// This is the recursive version of BuildSubAggregate. It takes a few different6382// arguments. Idxs is the index within the nested struct From that we are6383// looking at now (which is of type IndexedType). IdxSkip is the number of6384// indices from Idxs that should be left out when inserting into the resulting6385// struct. To is the result struct built so far, new insertvalue instructions6386// build on that.6387static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,6388 SmallVectorImpl<unsigned> &Idxs,6389 unsigned IdxSkip,6390 BasicBlock::iterator InsertBefore) {6391 StructType *STy = dyn_cast<StructType>(IndexedType);6392 if (STy) {6393 // Save the original To argument so we can modify it6394 Value *OrigTo = To;6395 // General case, the type indexed by Idxs is a struct6396 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {6397 // Process each struct element recursively6398 Idxs.push_back(i);6399 Value *PrevTo = To;6400 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,6401 InsertBefore);6402 Idxs.pop_back();6403 if (!To) {6404 // Couldn't find any inserted value for this index? Cleanup6405 while (PrevTo != OrigTo) {6406 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);6407 PrevTo = Del->getAggregateOperand();6408 Del->eraseFromParent();6409 }6410 // Stop processing elements6411 break;6412 }6413 }6414 // If we successfully found a value for each of our subaggregates6415 if (To)6416 return To;6417 }6418 // Base case, the type indexed by SourceIdxs is not a struct, or not all of6419 // the struct's elements had a value that was inserted directly. In the latter6420 // case, perhaps we can't determine each of the subelements individually, but6421 // we might be able to find the complete struct somewhere.6422 6423 // Find the value that is at that particular spot6424 Value *V = FindInsertedValue(From, Idxs);6425 6426 if (!V)6427 return nullptr;6428 6429 // Insert the value in the new (sub) aggregate6430 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",6431 InsertBefore);6432}6433 6434// This helper takes a nested struct and extracts a part of it (which is again a6435// struct) into a new value. For example, given the struct:6436// { a, { b, { c, d }, e } }6437// and the indices "1, 1" this returns6438// { c, d }.6439//6440// It does this by inserting an insertvalue for each element in the resulting6441// struct, as opposed to just inserting a single struct. This will only work if6442// each of the elements of the substruct are known (ie, inserted into From by an6443// insertvalue instruction somewhere).6444//6445// All inserted insertvalue instructions are inserted before InsertBefore6446static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,6447 BasicBlock::iterator InsertBefore) {6448 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),6449 idx_range);6450 Value *To = PoisonValue::get(IndexedType);6451 SmallVector<unsigned, 10> Idxs(idx_range);6452 unsigned IdxSkip = Idxs.size();6453 6454 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);6455}6456 6457/// Given an aggregate and a sequence of indices, see if the scalar value6458/// indexed is already around as a register, for example if it was inserted6459/// directly into the aggregate.6460///6461/// If InsertBefore is not null, this function will duplicate (modified)6462/// insertvalues when a part of a nested struct is extracted.6463Value *6464llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,6465 std::optional<BasicBlock::iterator> InsertBefore) {6466 // Nothing to index? Just return V then (this is useful at the end of our6467 // recursion).6468 if (idx_range.empty())6469 return V;6470 // We have indices, so V should have an indexable type.6471 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&6472 "Not looking at a struct or array?");6473 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&6474 "Invalid indices for type?");6475 6476 if (Constant *C = dyn_cast<Constant>(V)) {6477 C = C->getAggregateElement(idx_range[0]);6478 if (!C) return nullptr;6479 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);6480 }6481 6482 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {6483 // Loop the indices for the insertvalue instruction in parallel with the6484 // requested indices6485 const unsigned *req_idx = idx_range.begin();6486 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();6487 i != e; ++i, ++req_idx) {6488 if (req_idx == idx_range.end()) {6489 // We can't handle this without inserting insertvalues6490 if (!InsertBefore)6491 return nullptr;6492 6493 // The requested index identifies a part of a nested aggregate. Handle6494 // this specially. For example,6495 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 06496 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 16497 // %C = extractvalue {i32, { i32, i32 } } %B, 16498 // This can be changed into6499 // %A = insertvalue {i32, i32 } undef, i32 10, 06500 // %C = insertvalue {i32, i32 } %A, i32 11, 16501 // which allows the unused 0,0 element from the nested struct to be6502 // removed.6503 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),6504 *InsertBefore);6505 }6506 6507 // This insert value inserts something else than what we are looking for.6508 // See if the (aggregate) value inserted into has the value we are6509 // looking for, then.6510 if (*req_idx != *i)6511 return FindInsertedValue(I->getAggregateOperand(), idx_range,6512 InsertBefore);6513 }6514 // If we end up here, the indices of the insertvalue match with those6515 // requested (though possibly only partially). Now we recursively look at6516 // the inserted value, passing any remaining indices.6517 return FindInsertedValue(I->getInsertedValueOperand(),6518 ArrayRef(req_idx, idx_range.end()), InsertBefore);6519 }6520 6521 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {6522 // If we're extracting a value from an aggregate that was extracted from6523 // something else, we can extract from that something else directly instead.6524 // However, we will need to chain I's indices with the requested indices.6525 6526 // Calculate the number of indices required6527 unsigned size = I->getNumIndices() + idx_range.size();6528 // Allocate some space to put the new indices in6529 SmallVector<unsigned, 5> Idxs;6530 Idxs.reserve(size);6531 // Add indices from the extract value instruction6532 Idxs.append(I->idx_begin(), I->idx_end());6533 6534 // Add requested indices6535 Idxs.append(idx_range.begin(), idx_range.end());6536 6537 assert(Idxs.size() == size6538 && "Number of indices added not correct?");6539 6540 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);6541 }6542 // Otherwise, we don't know (such as, extracting from a function return value6543 // or load instruction)6544 return nullptr;6545}6546 6547// If V refers to an initialized global constant, set Slice either to6548// its initializer if the size of its elements equals ElementSize, or,6549// for ElementSize == 8, to its representation as an array of unsiged6550// char. Return true on success.6551// Offset is in the unit "nr of ElementSize sized elements".6552bool llvm::getConstantDataArrayInfo(const Value *V,6553 ConstantDataArraySlice &Slice,6554 unsigned ElementSize, uint64_t Offset) {6555 assert(V && "V should not be null.");6556 assert((ElementSize % 8) == 0 &&6557 "ElementSize expected to be a multiple of the size of a byte.");6558 unsigned ElementSizeInBytes = ElementSize / 8;6559 6560 // Drill down into the pointer expression V, ignoring any intervening6561 // casts, and determine the identity of the object it references along6562 // with the cumulative byte offset into it.6563 const GlobalVariable *GV =6564 dyn_cast<GlobalVariable>(getUnderlyingObject(V));6565 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())6566 // Fail if V is not based on constant global object.6567 return false;6568 6569 const DataLayout &DL = GV->getDataLayout();6570 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);6571 6572 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,6573 /*AllowNonInbounds*/ true))6574 // Fail if a constant offset could not be determined.6575 return false;6576 6577 uint64_t StartIdx = Off.getLimitedValue();6578 if (StartIdx == UINT64_MAX)6579 // Fail if the constant offset is excessive.6580 return false;6581 6582 // Off/StartIdx is in the unit of bytes. So we need to convert to number of6583 // elements. Simply bail out if that isn't possible.6584 if ((StartIdx % ElementSizeInBytes) != 0)6585 return false;6586 6587 Offset += StartIdx / ElementSizeInBytes;6588 ConstantDataArray *Array = nullptr;6589 ArrayType *ArrayTy = nullptr;6590 6591 if (GV->getInitializer()->isNullValue()) {6592 Type *GVTy = GV->getValueType();6593 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();6594 uint64_t Length = SizeInBytes / ElementSizeInBytes;6595 6596 Slice.Array = nullptr;6597 Slice.Offset = 0;6598 // Return an empty Slice for undersized constants to let callers6599 // transform even undefined library calls into simpler, well-defined6600 // expressions. This is preferable to making the calls although it6601 // prevents sanitizers from detecting such calls.6602 Slice.Length = Length < Offset ? 0 : Length - Offset;6603 return true;6604 }6605 6606 auto *Init = const_cast<Constant *>(GV->getInitializer());6607 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {6608 Type *InitElTy = ArrayInit->getElementType();6609 if (InitElTy->isIntegerTy(ElementSize)) {6610 // If Init is an initializer for an array of the expected type6611 // and size, use it as is.6612 Array = ArrayInit;6613 ArrayTy = ArrayInit->getType();6614 }6615 }6616 6617 if (!Array) {6618 if (ElementSize != 8)6619 // TODO: Handle conversions to larger integral types.6620 return false;6621 6622 // Otherwise extract the portion of the initializer starting6623 // at Offset as an array of bytes, and reset Offset.6624 Init = ReadByteArrayFromGlobal(GV, Offset);6625 if (!Init)6626 return false;6627 6628 Offset = 0;6629 Array = dyn_cast<ConstantDataArray>(Init);6630 ArrayTy = dyn_cast<ArrayType>(Init->getType());6631 }6632 6633 uint64_t NumElts = ArrayTy->getArrayNumElements();6634 if (Offset > NumElts)6635 return false;6636 6637 Slice.Array = Array;6638 Slice.Offset = Offset;6639 Slice.Length = NumElts - Offset;6640 return true;6641}6642 6643/// Extract bytes from the initializer of the constant array V, which need6644/// not be a nul-terminated string. On success, store the bytes in Str and6645/// return true. When TrimAtNul is set, Str will contain only the bytes up6646/// to but not including the first nul. Return false on failure.6647bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,6648 bool TrimAtNul) {6649 ConstantDataArraySlice Slice;6650 if (!getConstantDataArrayInfo(V, Slice, 8))6651 return false;6652 6653 if (Slice.Array == nullptr) {6654 if (TrimAtNul) {6655 // Return a nul-terminated string even for an empty Slice. This is6656 // safe because all existing SimplifyLibcalls callers require string6657 // arguments and the behavior of the functions they fold is undefined6658 // otherwise. Folding the calls this way is preferable to making6659 // the undefined library calls, even though it prevents sanitizers6660 // from reporting such calls.6661 Str = StringRef();6662 return true;6663 }6664 if (Slice.Length == 1) {6665 Str = StringRef("", 1);6666 return true;6667 }6668 // We cannot instantiate a StringRef as we do not have an appropriate string6669 // of 0s at hand.6670 return false;6671 }6672 6673 // Start out with the entire array in the StringRef.6674 Str = Slice.Array->getAsString();6675 // Skip over 'offset' bytes.6676 Str = Str.substr(Slice.Offset);6677 6678 if (TrimAtNul) {6679 // Trim off the \0 and anything after it. If the array is not nul6680 // terminated, we just return the whole end of string. The client may know6681 // some other way that the string is length-bound.6682 Str = Str.substr(0, Str.find('\0'));6683 }6684 return true;6685}6686 6687// These next two are very similar to the above, but also look through PHI6688// nodes.6689// TODO: See if we can integrate these two together.6690 6691/// If we can compute the length of the string pointed to by6692/// the specified pointer, return 'len+1'. If we can't, return 0.6693static uint64_t GetStringLengthH(const Value *V,6694 SmallPtrSetImpl<const PHINode*> &PHIs,6695 unsigned CharSize) {6696 // Look through noop bitcast instructions.6697 V = V->stripPointerCasts();6698 6699 // If this is a PHI node, there are two cases: either we have already seen it6700 // or we haven't.6701 if (const PHINode *PN = dyn_cast<PHINode>(V)) {6702 if (!PHIs.insert(PN).second)6703 return ~0ULL; // already in the set.6704 6705 // If it was new, see if all the input strings are the same length.6706 uint64_t LenSoFar = ~0ULL;6707 for (Value *IncValue : PN->incoming_values()) {6708 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);6709 if (Len == 0) return 0; // Unknown length -> unknown.6710 6711 if (Len == ~0ULL) continue;6712 6713 if (Len != LenSoFar && LenSoFar != ~0ULL)6714 return 0; // Disagree -> unknown.6715 LenSoFar = Len;6716 }6717 6718 // Success, all agree.6719 return LenSoFar;6720 }6721 6722 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)6723 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {6724 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);6725 if (Len1 == 0) return 0;6726 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);6727 if (Len2 == 0) return 0;6728 if (Len1 == ~0ULL) return Len2;6729 if (Len2 == ~0ULL) return Len1;6730 if (Len1 != Len2) return 0;6731 return Len1;6732 }6733 6734 // Otherwise, see if we can read the string.6735 ConstantDataArraySlice Slice;6736 if (!getConstantDataArrayInfo(V, Slice, CharSize))6737 return 0;6738 6739 if (Slice.Array == nullptr)6740 // Zeroinitializer (including an empty one).6741 return 1;6742 6743 // Search for the first nul character. Return a conservative result even6744 // when there is no nul. This is safe since otherwise the string function6745 // being folded such as strlen is undefined, and can be preferable to6746 // making the undefined library call.6747 unsigned NullIndex = 0;6748 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {6749 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)6750 break;6751 }6752 6753 return NullIndex + 1;6754}6755 6756/// If we can compute the length of the string pointed to by6757/// the specified pointer, return 'len+1'. If we can't, return 0.6758uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {6759 if (!V->getType()->isPointerTy())6760 return 0;6761 6762 SmallPtrSet<const PHINode*, 32> PHIs;6763 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);6764 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return6765 // an empty string as a length.6766 return Len == ~0ULL ? 1 : Len;6767}6768 6769const Value *6770llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call,6771 bool MustPreserveNullness) {6772 assert(Call &&6773 "getArgumentAliasingToReturnedPointer only works on nonnull calls");6774 if (const Value *RV = Call->getReturnedArgOperand())6775 return RV;6776 // This can be used only as a aliasing property.6777 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(6778 Call, MustPreserveNullness))6779 return Call->getArgOperand(0);6780 return nullptr;6781}6782 6783bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(6784 const CallBase *Call, bool MustPreserveNullness) {6785 switch (Call->getIntrinsicID()) {6786 case Intrinsic::launder_invariant_group:6787 case Intrinsic::strip_invariant_group:6788 case Intrinsic::aarch64_irg:6789 case Intrinsic::aarch64_tagp:6790 // The amdgcn_make_buffer_rsrc function does not alter the address of the6791 // input pointer (and thus preserve null-ness for the purposes of escape6792 // analysis, which is where the MustPreserveNullness flag comes in to play).6793 // However, it will not necessarily map ptr addrspace(N) null to ptr6794 // addrspace(8) null, aka the "null descriptor", which has "all loads return6795 // 0, all stores are dropped" semantics. Given the context of this intrinsic6796 // list, no one should be relying on such a strict interpretation of6797 // MustPreserveNullness (and, at time of writing, they are not), but we6798 // document this fact out of an abundance of caution.6799 case Intrinsic::amdgcn_make_buffer_rsrc:6800 return true;6801 case Intrinsic::ptrmask:6802 return !MustPreserveNullness;6803 case Intrinsic::threadlocal_address:6804 // The underlying variable changes with thread ID. The Thread ID may change6805 // at coroutine suspend points.6806 return !Call->getParent()->getParent()->isPresplitCoroutine();6807 default:6808 return false;6809 }6810}6811 6812/// \p PN defines a loop-variant pointer to an object. Check if the6813/// previous iteration of the loop was referring to the same object as \p PN.6814static bool isSameUnderlyingObjectInLoop(const PHINode *PN,6815 const LoopInfo *LI) {6816 // Find the loop-defined value.6817 Loop *L = LI->getLoopFor(PN->getParent());6818 if (PN->getNumIncomingValues() != 2)6819 return true;6820 6821 // Find the value from previous iteration.6822 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));6823 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)6824 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));6825 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)6826 return true;6827 6828 // If a new pointer is loaded in the loop, the pointer references a different6829 // object in every iteration. E.g.:6830 // for (i)6831 // int *p = a[i];6832 // ...6833 if (auto *Load = dyn_cast<LoadInst>(PrevValue))6834 if (!L->isLoopInvariant(Load->getPointerOperand()))6835 return false;6836 return true;6837}6838 6839const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {6840 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {6841 if (auto *GEP = dyn_cast<GEPOperator>(V)) {6842 const Value *PtrOp = GEP->getPointerOperand();6843 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.6844 return V;6845 V = PtrOp;6846 } else if (Operator::getOpcode(V) == Instruction::BitCast ||6847 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {6848 Value *NewV = cast<Operator>(V)->getOperand(0);6849 if (!NewV->getType()->isPointerTy())6850 return V;6851 V = NewV;6852 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {6853 if (GA->isInterposable())6854 return V;6855 V = GA->getAliasee();6856 } else {6857 if (auto *PHI = dyn_cast<PHINode>(V)) {6858 // Look through single-arg phi nodes created by LCSSA.6859 if (PHI->getNumIncomingValues() == 1) {6860 V = PHI->getIncomingValue(0);6861 continue;6862 }6863 } else if (auto *Call = dyn_cast<CallBase>(V)) {6864 // CaptureTracking can know about special capturing properties of some6865 // intrinsics like launder.invariant.group, that can't be expressed with6866 // the attributes, but have properties like returning aliasing pointer.6867 // Because some analysis may assume that nocaptured pointer is not6868 // returned from some special intrinsic (because function would have to6869 // be marked with returns attribute), it is crucial to use this function6870 // because it should be in sync with CaptureTracking. Not using it may6871 // cause weird miscompilations where 2 aliasing pointers are assumed to6872 // noalias.6873 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {6874 V = RP;6875 continue;6876 }6877 }6878 6879 return V;6880 }6881 assert(V->getType()->isPointerTy() && "Unexpected operand type!");6882 }6883 return V;6884}6885 6886void llvm::getUnderlyingObjects(const Value *V,6887 SmallVectorImpl<const Value *> &Objects,6888 const LoopInfo *LI, unsigned MaxLookup) {6889 SmallPtrSet<const Value *, 4> Visited;6890 SmallVector<const Value *, 4> Worklist;6891 Worklist.push_back(V);6892 do {6893 const Value *P = Worklist.pop_back_val();6894 P = getUnderlyingObject(P, MaxLookup);6895 6896 if (!Visited.insert(P).second)6897 continue;6898 6899 if (auto *SI = dyn_cast<SelectInst>(P)) {6900 Worklist.push_back(SI->getTrueValue());6901 Worklist.push_back(SI->getFalseValue());6902 continue;6903 }6904 6905 if (auto *PN = dyn_cast<PHINode>(P)) {6906 // If this PHI changes the underlying object in every iteration of the6907 // loop, don't look through it. Consider:6908 // int **A;6909 // for (i) {6910 // Prev = Curr; // Prev = PHI (Prev_0, Curr)6911 // Curr = A[i];6912 // *Prev, *Curr;6913 //6914 // Prev is tracking Curr one iteration behind so they refer to different6915 // underlying objects.6916 if (!LI || !LI->isLoopHeader(PN->getParent()) ||6917 isSameUnderlyingObjectInLoop(PN, LI))6918 append_range(Worklist, PN->incoming_values());6919 else6920 Objects.push_back(P);6921 continue;6922 }6923 6924 Objects.push_back(P);6925 } while (!Worklist.empty());6926}6927 6928const Value *llvm::getUnderlyingObjectAggressive(const Value *V) {6929 const unsigned MaxVisited = 8;6930 6931 SmallPtrSet<const Value *, 8> Visited;6932 SmallVector<const Value *, 8> Worklist;6933 Worklist.push_back(V);6934 const Value *Object = nullptr;6935 // Used as fallback if we can't find a common underlying object through6936 // recursion.6937 bool First = true;6938 const Value *FirstObject = getUnderlyingObject(V);6939 do {6940 const Value *P = Worklist.pop_back_val();6941 P = First ? FirstObject : getUnderlyingObject(P);6942 First = false;6943 6944 if (!Visited.insert(P).second)6945 continue;6946 6947 if (Visited.size() == MaxVisited)6948 return FirstObject;6949 6950 if (auto *SI = dyn_cast<SelectInst>(P)) {6951 Worklist.push_back(SI->getTrueValue());6952 Worklist.push_back(SI->getFalseValue());6953 continue;6954 }6955 6956 if (auto *PN = dyn_cast<PHINode>(P)) {6957 append_range(Worklist, PN->incoming_values());6958 continue;6959 }6960 6961 if (!Object)6962 Object = P;6963 else if (Object != P)6964 return FirstObject;6965 } while (!Worklist.empty());6966 6967 return Object ? Object : FirstObject;6968}6969 6970/// This is the function that does the work of looking through basic6971/// ptrtoint+arithmetic+inttoptr sequences.6972static const Value *getUnderlyingObjectFromInt(const Value *V) {6973 do {6974 if (const Operator *U = dyn_cast<Operator>(V)) {6975 // If we find a ptrtoint, we can transfer control back to the6976 // regular getUnderlyingObjectFromInt.6977 if (U->getOpcode() == Instruction::PtrToInt)6978 return U->getOperand(0);6979 // If we find an add of a constant, a multiplied value, or a phi, it's6980 // likely that the other operand will lead us to the base6981 // object. We don't have to worry about the case where the6982 // object address is somehow being computed by the multiply,6983 // because our callers only care when the result is an6984 // identifiable object.6985 if (U->getOpcode() != Instruction::Add ||6986 (!isa<ConstantInt>(U->getOperand(1)) &&6987 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&6988 !isa<PHINode>(U->getOperand(1))))6989 return V;6990 V = U->getOperand(0);6991 } else {6992 return V;6993 }6994 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");6995 } while (true);6996}6997 6998/// This is a wrapper around getUnderlyingObjects and adds support for basic6999/// ptrtoint+arithmetic+inttoptr sequences.7000/// It returns false if unidentified object is found in getUnderlyingObjects.7001bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,7002 SmallVectorImpl<Value *> &Objects) {7003 SmallPtrSet<const Value *, 16> Visited;7004 SmallVector<const Value *, 4> Working(1, V);7005 do {7006 V = Working.pop_back_val();7007 7008 SmallVector<const Value *, 4> Objs;7009 getUnderlyingObjects(V, Objs);7010 7011 for (const Value *V : Objs) {7012 if (!Visited.insert(V).second)7013 continue;7014 if (Operator::getOpcode(V) == Instruction::IntToPtr) {7015 const Value *O =7016 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));7017 if (O->getType()->isPointerTy()) {7018 Working.push_back(O);7019 continue;7020 }7021 }7022 // If getUnderlyingObjects fails to find an identifiable object,7023 // getUnderlyingObjectsForCodeGen also fails for safety.7024 if (!isIdentifiedObject(V)) {7025 Objects.clear();7026 return false;7027 }7028 Objects.push_back(const_cast<Value *>(V));7029 }7030 } while (!Working.empty());7031 return true;7032}7033 7034AllocaInst *llvm::findAllocaForValue(Value *V, bool OffsetZero) {7035 AllocaInst *Result = nullptr;7036 SmallPtrSet<Value *, 4> Visited;7037 SmallVector<Value *, 4> Worklist;7038 7039 auto AddWork = [&](Value *V) {7040 if (Visited.insert(V).second)7041 Worklist.push_back(V);7042 };7043 7044 AddWork(V);7045 do {7046 V = Worklist.pop_back_val();7047 assert(Visited.count(V));7048 7049 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {7050 if (Result && Result != AI)7051 return nullptr;7052 Result = AI;7053 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {7054 AddWork(CI->getOperand(0));7055 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {7056 for (Value *IncValue : PN->incoming_values())7057 AddWork(IncValue);7058 } else if (auto *SI = dyn_cast<SelectInst>(V)) {7059 AddWork(SI->getTrueValue());7060 AddWork(SI->getFalseValue());7061 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {7062 if (OffsetZero && !GEP->hasAllZeroIndices())7063 return nullptr;7064 AddWork(GEP->getPointerOperand());7065 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {7066 Value *Returned = CB->getReturnedArgOperand();7067 if (Returned)7068 AddWork(Returned);7069 else7070 return nullptr;7071 } else {7072 return nullptr;7073 }7074 } while (!Worklist.empty());7075 7076 return Result;7077}7078 7079static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(7080 const Value *V, bool AllowLifetime, bool AllowDroppable) {7081 for (const User *U : V->users()) {7082 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);7083 if (!II)7084 return false;7085 7086 if (AllowLifetime && II->isLifetimeStartOrEnd())7087 continue;7088 7089 if (AllowDroppable && II->isDroppable())7090 continue;7091 7092 return false;7093 }7094 return true;7095}7096 7097bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {7098 return onlyUsedByLifetimeMarkersOrDroppableInstsHelper(7099 V, /* AllowLifetime */ true, /* AllowDroppable */ false);7100}7101bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) {7102 return onlyUsedByLifetimeMarkersOrDroppableInstsHelper(7103 V, /* AllowLifetime */ true, /* AllowDroppable */ true);7104}7105 7106bool llvm::isNotCrossLaneOperation(const Instruction *I) {7107 if (auto *II = dyn_cast<IntrinsicInst>(I))7108 return isTriviallyVectorizable(II->getIntrinsicID());7109 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);7110 return (!Shuffle || Shuffle->isSelect()) &&7111 !isa<CallBase, BitCastInst, ExtractElementInst>(I);7112}7113 7114bool llvm::isSafeToSpeculativelyExecute(7115 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,7116 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,7117 bool IgnoreUBImplyingAttrs) {7118 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,7119 AC, DT, TLI, UseVariableInfo,7120 IgnoreUBImplyingAttrs);7121}7122 7123bool llvm::isSafeToSpeculativelyExecuteWithOpcode(7124 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,7125 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,7126 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {7127#ifndef NDEBUG7128 if (Inst->getOpcode() != Opcode) {7129 // Check that the operands are actually compatible with the Opcode override.7130 auto hasEqualReturnAndLeadingOperandTypes =7131 [](const Instruction *Inst, unsigned NumLeadingOperands) {7132 if (Inst->getNumOperands() < NumLeadingOperands)7133 return false;7134 const Type *ExpectedType = Inst->getType();7135 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)7136 if (Inst->getOperand(ItOp)->getType() != ExpectedType)7137 return false;7138 return true;7139 };7140 assert(!Instruction::isBinaryOp(Opcode) ||7141 hasEqualReturnAndLeadingOperandTypes(Inst, 2));7142 assert(!Instruction::isUnaryOp(Opcode) ||7143 hasEqualReturnAndLeadingOperandTypes(Inst, 1));7144 }7145#endif7146 7147 switch (Opcode) {7148 default:7149 return true;7150 case Instruction::UDiv:7151 case Instruction::URem: {7152 // x / y is undefined if y == 0.7153 const APInt *V;7154 if (match(Inst->getOperand(1), m_APInt(V)))7155 return *V != 0;7156 return false;7157 }7158 case Instruction::SDiv:7159 case Instruction::SRem: {7160 // x / y is undefined if y == 0 or x == INT_MIN and y == -17161 const APInt *Numerator, *Denominator;7162 if (!match(Inst->getOperand(1), m_APInt(Denominator)))7163 return false;7164 // We cannot hoist this division if the denominator is 0.7165 if (*Denominator == 0)7166 return false;7167 // It's safe to hoist if the denominator is not 0 or -1.7168 if (!Denominator->isAllOnes())7169 return true;7170 // At this point we know that the denominator is -1. It is safe to hoist as7171 // long we know that the numerator is not INT_MIN.7172 if (match(Inst->getOperand(0), m_APInt(Numerator)))7173 return !Numerator->isMinSignedValue();7174 // The numerator *might* be MinSignedValue.7175 return false;7176 }7177 case Instruction::Load: {7178 if (!UseVariableInfo)7179 return false;7180 7181 const LoadInst *LI = dyn_cast<LoadInst>(Inst);7182 if (!LI)7183 return false;7184 if (mustSuppressSpeculation(*LI))7185 return false;7186 const DataLayout &DL = LI->getDataLayout();7187 return isDereferenceableAndAlignedPointer(LI->getPointerOperand(),7188 LI->getType(), LI->getAlign(), DL,7189 CtxI, AC, DT, TLI);7190 }7191 case Instruction::Call: {7192 auto *CI = dyn_cast<const CallInst>(Inst);7193 if (!CI)7194 return false;7195 const Function *Callee = CI->getCalledFunction();7196 7197 // The called function could have undefined behavior or side-effects, even7198 // if marked readnone nounwind.7199 if (!Callee || !Callee->isSpeculatable())7200 return false;7201 // Since the operands may be changed after hoisting, undefined behavior may7202 // be triggered by some UB-implying attributes.7203 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();7204 }7205 case Instruction::VAArg:7206 case Instruction::Alloca:7207 case Instruction::Invoke:7208 case Instruction::CallBr:7209 case Instruction::PHI:7210 case Instruction::Store:7211 case Instruction::Ret:7212 case Instruction::Br:7213 case Instruction::IndirectBr:7214 case Instruction::Switch:7215 case Instruction::Unreachable:7216 case Instruction::Fence:7217 case Instruction::AtomicRMW:7218 case Instruction::AtomicCmpXchg:7219 case Instruction::LandingPad:7220 case Instruction::Resume:7221 case Instruction::CatchSwitch:7222 case Instruction::CatchPad:7223 case Instruction::CatchRet:7224 case Instruction::CleanupPad:7225 case Instruction::CleanupRet:7226 return false; // Misc instructions which have effects7227 }7228}7229 7230bool llvm::mayHaveNonDefUseDependency(const Instruction &I) {7231 if (I.mayReadOrWriteMemory())7232 // Memory dependency possible7233 return true;7234 if (!isSafeToSpeculativelyExecute(&I))7235 // Can't move above a maythrow call or infinite loop. Or if an7236 // inalloca alloca, above a stacksave call.7237 return true;7238 if (!isGuaranteedToTransferExecutionToSuccessor(&I))7239 // 1) Can't reorder two inf-loop calls, even if readonly7240 // 2) Also can't reorder an inf-loop call below a instruction which isn't7241 // safe to speculative execute. (Inverse of above)7242 return true;7243 return false;7244}7245 7246/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.7247static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {7248 switch (OR) {7249 case ConstantRange::OverflowResult::MayOverflow:7250 return OverflowResult::MayOverflow;7251 case ConstantRange::OverflowResult::AlwaysOverflowsLow:7252 return OverflowResult::AlwaysOverflowsLow;7253 case ConstantRange::OverflowResult::AlwaysOverflowsHigh:7254 return OverflowResult::AlwaysOverflowsHigh;7255 case ConstantRange::OverflowResult::NeverOverflows:7256 return OverflowResult::NeverOverflows;7257 }7258 llvm_unreachable("Unknown OverflowResult");7259}7260 7261/// Combine constant ranges from computeConstantRange() and computeKnownBits().7262ConstantRange7263llvm::computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,7264 bool ForSigned,7265 const SimplifyQuery &SQ) {7266 ConstantRange CR1 =7267 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);7268 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);7269 ConstantRange::PreferredRangeType RangeType =7270 ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;7271 return CR1.intersectWith(CR2, RangeType);7272}7273 7274OverflowResult llvm::computeOverflowForUnsignedMul(const Value *LHS,7275 const Value *RHS,7276 const SimplifyQuery &SQ,7277 bool IsNSW) {7278 KnownBits LHSKnown = computeKnownBits(LHS, SQ);7279 KnownBits RHSKnown = computeKnownBits(RHS, SQ);7280 7281 // mul nsw of two non-negative numbers is also nuw.7282 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())7283 return OverflowResult::NeverOverflows;7284 7285 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);7286 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);7287 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));7288}7289 7290OverflowResult llvm::computeOverflowForSignedMul(const Value *LHS,7291 const Value *RHS,7292 const SimplifyQuery &SQ) {7293 // Multiplying n * m significant bits yields a result of n + m significant7294 // bits. If the total number of significant bits does not exceed the7295 // result bit width (minus 1), there is no overflow.7296 // This means if we have enough leading sign bits in the operands7297 // we can guarantee that the result does not overflow.7298 // Ref: "Hacker's Delight" by Henry Warren7299 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();7300 7301 // Note that underestimating the number of sign bits gives a more7302 // conservative answer.7303 unsigned SignBits =7304 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);7305 7306 // First handle the easy case: if we have enough sign bits there's7307 // definitely no overflow.7308 if (SignBits > BitWidth + 1)7309 return OverflowResult::NeverOverflows;7310 7311 // There are two ambiguous cases where there can be no overflow:7312 // SignBits == BitWidth + 1 and7313 // SignBits == BitWidth7314 // The second case is difficult to check, therefore we only handle the7315 // first case.7316 if (SignBits == BitWidth + 1) {7317 // It overflows only when both arguments are negative and the true7318 // product is exactly the minimum negative number.7319 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x80007320 // For simplicity we just check if at least one side is not negative.7321 KnownBits LHSKnown = computeKnownBits(LHS, SQ);7322 KnownBits RHSKnown = computeKnownBits(RHS, SQ);7323 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())7324 return OverflowResult::NeverOverflows;7325 }7326 return OverflowResult::MayOverflow;7327}7328 7329OverflowResult7330llvm::computeOverflowForUnsignedAdd(const WithCache<const Value *> &LHS,7331 const WithCache<const Value *> &RHS,7332 const SimplifyQuery &SQ) {7333 ConstantRange LHSRange =7334 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);7335 ConstantRange RHSRange =7336 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);7337 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));7338}7339 7340static OverflowResult7341computeOverflowForSignedAdd(const WithCache<const Value *> &LHS,7342 const WithCache<const Value *> &RHS,7343 const AddOperator *Add, const SimplifyQuery &SQ) {7344 if (Add && Add->hasNoSignedWrap()) {7345 return OverflowResult::NeverOverflows;7346 }7347 7348 // If LHS and RHS each have at least two sign bits, the addition will look7349 // like7350 //7351 // XX..... +7352 // YY.....7353 //7354 // If the carry into the most significant position is 0, X and Y can't both7355 // be 1 and therefore the carry out of the addition is also 0.7356 //7357 // If the carry into the most significant position is 1, X and Y can't both7358 // be 0 and therefore the carry out of the addition is also 1.7359 //7360 // Since the carry into the most significant position is always equal to7361 // the carry out of the addition, there is no signed overflow.7362 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)7363 return OverflowResult::NeverOverflows;7364 7365 ConstantRange LHSRange =7366 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);7367 ConstantRange RHSRange =7368 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);7369 OverflowResult OR =7370 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));7371 if (OR != OverflowResult::MayOverflow)7372 return OR;7373 7374 // The remaining code needs Add to be available. Early returns if not so.7375 if (!Add)7376 return OverflowResult::MayOverflow;7377 7378 // If the sign of Add is the same as at least one of the operands, this add7379 // CANNOT overflow. If this can be determined from the known bits of the7380 // operands the above signedAddMayOverflow() check will have already done so.7381 // The only other way to improve on the known bits is from an assumption, so7382 // call computeKnownBitsFromContext() directly.7383 bool LHSOrRHSKnownNonNegative =7384 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());7385 bool LHSOrRHSKnownNegative =7386 (LHSRange.isAllNegative() || RHSRange.isAllNegative());7387 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {7388 KnownBits AddKnown(LHSRange.getBitWidth());7389 computeKnownBitsFromContext(Add, AddKnown, SQ);7390 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||7391 (AddKnown.isNegative() && LHSOrRHSKnownNegative))7392 return OverflowResult::NeverOverflows;7393 }7394 7395 return OverflowResult::MayOverflow;7396}7397 7398OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,7399 const Value *RHS,7400 const SimplifyQuery &SQ) {7401 // X - (X % ?)7402 // The remainder of a value can't have greater magnitude than itself,7403 // so the subtraction can't overflow.7404 7405 // X - (X -nuw ?)7406 // In the minimal case, this would simplify to "?", so there's no subtract7407 // at all. But if this analysis is used to peek through casts, for example,7408 // then determining no-overflow may allow other transforms.7409 7410 // TODO: There are other patterns like this.7411 // See simplifyICmpWithBinOpOnLHS() for candidates.7412 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||7413 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))7414 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))7415 return OverflowResult::NeverOverflows;7416 7417 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,7418 SQ.DL)) {7419 if (*C)7420 return OverflowResult::NeverOverflows;7421 return OverflowResult::AlwaysOverflowsLow;7422 }7423 7424 ConstantRange LHSRange =7425 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);7426 ConstantRange RHSRange =7427 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);7428 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));7429}7430 7431OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,7432 const Value *RHS,7433 const SimplifyQuery &SQ) {7434 // X - (X % ?)7435 // The remainder of a value can't have greater magnitude than itself,7436 // so the subtraction can't overflow.7437 7438 // X - (X -nsw ?)7439 // In the minimal case, this would simplify to "?", so there's no subtract7440 // at all. But if this analysis is used to peek through casts, for example,7441 // then determining no-overflow may allow other transforms.7442 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||7443 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))7444 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))7445 return OverflowResult::NeverOverflows;7446 7447 // If LHS and RHS each have at least two sign bits, the subtraction7448 // cannot overflow.7449 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)7450 return OverflowResult::NeverOverflows;7451 7452 ConstantRange LHSRange =7453 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);7454 ConstantRange RHSRange =7455 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);7456 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));7457}7458 7459bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,7460 const DominatorTree &DT) {7461 SmallVector<const BranchInst *, 2> GuardingBranches;7462 SmallVector<const ExtractValueInst *, 2> Results;7463 7464 for (const User *U : WO->users()) {7465 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {7466 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");7467 7468 if (EVI->getIndices()[0] == 0)7469 Results.push_back(EVI);7470 else {7471 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");7472 7473 for (const auto *U : EVI->users())7474 if (const auto *B = dyn_cast<BranchInst>(U)) {7475 assert(B->isConditional() && "How else is it using an i1?");7476 GuardingBranches.push_back(B);7477 }7478 }7479 } else {7480 // We are using the aggregate directly in a way we don't want to analyze7481 // here (storing it to a global, say).7482 return false;7483 }7484 }7485 7486 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {7487 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));7488 if (!NoWrapEdge.isSingleEdge())7489 return false;7490 7491 // Check if all users of the add are provably no-wrap.7492 for (const auto *Result : Results) {7493 // If the extractvalue itself is not executed on overflow, the we don't7494 // need to check each use separately, since domination is transitive.7495 if (DT.dominates(NoWrapEdge, Result->getParent()))7496 continue;7497 7498 for (const auto &RU : Result->uses())7499 if (!DT.dominates(NoWrapEdge, RU))7500 return false;7501 }7502 7503 return true;7504 };7505 7506 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);7507}7508 7509/// Shifts return poison if shiftwidth is larger than the bitwidth.7510static bool shiftAmountKnownInRange(const Value *ShiftAmount) {7511 auto *C = dyn_cast<Constant>(ShiftAmount);7512 if (!C)7513 return false;7514 7515 // Shifts return poison if shiftwidth is larger than the bitwidth.7516 SmallVector<const Constant *, 4> ShiftAmounts;7517 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {7518 unsigned NumElts = FVTy->getNumElements();7519 for (unsigned i = 0; i < NumElts; ++i)7520 ShiftAmounts.push_back(C->getAggregateElement(i));7521 } else if (isa<ScalableVectorType>(C->getType()))7522 return false; // Can't tell, just return false to be safe7523 else7524 ShiftAmounts.push_back(C);7525 7526 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {7527 auto *CI = dyn_cast_or_null<ConstantInt>(C);7528 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());7529 });7530 7531 return Safe;7532}7533 7534enum class UndefPoisonKind {7535 PoisonOnly = (1 << 0),7536 UndefOnly = (1 << 1),7537 UndefOrPoison = PoisonOnly | UndefOnly,7538};7539 7540static bool includesPoison(UndefPoisonKind Kind) {7541 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;7542}7543 7544static bool includesUndef(UndefPoisonKind Kind) {7545 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;7546}7547 7548static bool canCreateUndefOrPoison(const Operator *Op, UndefPoisonKind Kind,7549 bool ConsiderFlagsAndMetadata) {7550 7551 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&7552 Op->hasPoisonGeneratingAnnotations())7553 return true;7554 7555 unsigned Opcode = Op->getOpcode();7556 7557 // Check whether opcode is a poison/undef-generating operation7558 switch (Opcode) {7559 case Instruction::Shl:7560 case Instruction::AShr:7561 case Instruction::LShr:7562 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));7563 case Instruction::FPToSI:7564 case Instruction::FPToUI:7565 // fptosi/ui yields poison if the resulting value does not fit in the7566 // destination type.7567 return true;7568 case Instruction::Call:7569 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {7570 switch (II->getIntrinsicID()) {7571 // TODO: Add more intrinsics.7572 case Intrinsic::ctlz:7573 case Intrinsic::cttz:7574 case Intrinsic::abs:7575 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())7576 return false;7577 break;7578 case Intrinsic::sshl_sat:7579 case Intrinsic::ushl_sat:7580 if (!includesPoison(Kind) ||7581 shiftAmountKnownInRange(II->getArgOperand(1)))7582 return false;7583 break;7584 }7585 }7586 [[fallthrough]];7587 case Instruction::CallBr:7588 case Instruction::Invoke: {7589 const auto *CB = cast<CallBase>(Op);7590 return !CB->hasRetAttr(Attribute::NoUndef) &&7591 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);7592 }7593 case Instruction::InsertElement:7594 case Instruction::ExtractElement: {7595 // If index exceeds the length of the vector, it returns poison7596 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());7597 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;7598 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));7599 if (includesPoison(Kind))7600 return !Idx ||7601 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());7602 return false;7603 }7604 case Instruction::ShuffleVector: {7605 ArrayRef<int> Mask = isa<ConstantExpr>(Op)7606 ? cast<ConstantExpr>(Op)->getShuffleMask()7607 : cast<ShuffleVectorInst>(Op)->getShuffleMask();7608 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);7609 }7610 case Instruction::FNeg:7611 case Instruction::PHI:7612 case Instruction::Select:7613 case Instruction::ExtractValue:7614 case Instruction::InsertValue:7615 case Instruction::Freeze:7616 case Instruction::ICmp:7617 case Instruction::FCmp:7618 case Instruction::GetElementPtr:7619 return false;7620 case Instruction::AddrSpaceCast:7621 return true;7622 default: {7623 const auto *CE = dyn_cast<ConstantExpr>(Op);7624 if (isa<CastInst>(Op) || (CE && CE->isCast()))7625 return false;7626 else if (Instruction::isBinaryOp(Opcode))7627 return false;7628 // Be conservative and return true.7629 return true;7630 }7631 }7632}7633 7634bool llvm::canCreateUndefOrPoison(const Operator *Op,7635 bool ConsiderFlagsAndMetadata) {7636 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,7637 ConsiderFlagsAndMetadata);7638}7639 7640bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {7641 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,7642 ConsiderFlagsAndMetadata);7643}7644 7645static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,7646 unsigned Depth) {7647 if (ValAssumedPoison == V)7648 return true;7649 7650 const unsigned MaxDepth = 2;7651 if (Depth >= MaxDepth)7652 return false;7653 7654 if (const auto *I = dyn_cast<Instruction>(V)) {7655 if (any_of(I->operands(), [=](const Use &Op) {7656 return propagatesPoison(Op) &&7657 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);7658 }))7659 return true;7660 7661 // V = extractvalue V0, idx7662 // V2 = extractvalue V0, idx27663 // V0's elements are all poison or not. (e.g., add_with_overflow)7664 const WithOverflowInst *II;7665 if (match(I, m_ExtractValue(m_WithOverflowInst(II))) &&7666 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||7667 llvm::is_contained(II->args(), ValAssumedPoison)))7668 return true;7669 }7670 return false;7671}7672 7673static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,7674 unsigned Depth) {7675 if (isGuaranteedNotToBePoison(ValAssumedPoison))7676 return true;7677 7678 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))7679 return true;7680 7681 const unsigned MaxDepth = 2;7682 if (Depth >= MaxDepth)7683 return false;7684 7685 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);7686 if (I && !canCreatePoison(cast<Operator>(I))) {7687 return all_of(I->operands(), [=](const Value *Op) {7688 return impliesPoison(Op, V, Depth + 1);7689 });7690 }7691 return false;7692}7693 7694bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {7695 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);7696}7697 7698static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);7699 7700static bool isGuaranteedNotToBeUndefOrPoison(7701 const Value *V, AssumptionCache *AC, const Instruction *CtxI,7702 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {7703 if (Depth >= MaxAnalysisRecursionDepth)7704 return false;7705 7706 if (isa<MetadataAsValue>(V))7707 return false;7708 7709 if (const auto *A = dyn_cast<Argument>(V)) {7710 if (A->hasAttribute(Attribute::NoUndef) ||7711 A->hasAttribute(Attribute::Dereferenceable) ||7712 A->hasAttribute(Attribute::DereferenceableOrNull))7713 return true;7714 }7715 7716 if (auto *C = dyn_cast<Constant>(V)) {7717 if (isa<PoisonValue>(C))7718 return !includesPoison(Kind);7719 7720 if (isa<UndefValue>(C))7721 return !includesUndef(Kind);7722 7723 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||7724 isa<ConstantPointerNull>(C) || isa<Function>(C))7725 return true;7726 7727 if (C->getType()->isVectorTy()) {7728 if (isa<ConstantExpr>(C)) {7729 // Scalable vectors can use a ConstantExpr to build a splat.7730 if (Constant *SplatC = C->getSplatValue())7731 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))7732 return true;7733 } else {7734 if (includesUndef(Kind) && C->containsUndefElement())7735 return false;7736 if (includesPoison(Kind) && C->containsPoisonElement())7737 return false;7738 return !C->containsConstantExpression();7739 }7740 }7741 }7742 7743 // Strip cast operations from a pointer value.7744 // Note that stripPointerCastsSameRepresentation can strip off getelementptr7745 // inbounds with zero offset. To guarantee that the result isn't poison, the7746 // stripped pointer is checked as it has to be pointing into an allocated7747 // object or be null `null` to ensure `inbounds` getelement pointers with a7748 // zero offset could not produce poison.7749 // It can strip off addrspacecast that do not change bit representation as7750 // well. We believe that such addrspacecast is equivalent to no-op.7751 auto *StrippedV = V->stripPointerCastsSameRepresentation();7752 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||7753 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))7754 return true;7755 7756 auto OpCheck = [&](const Value *V) {7757 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);7758 };7759 7760 if (auto *Opr = dyn_cast<Operator>(V)) {7761 // If the value is a freeze instruction, then it can never7762 // be undef or poison.7763 if (isa<FreezeInst>(V))7764 return true;7765 7766 if (const auto *CB = dyn_cast<CallBase>(V)) {7767 if (CB->hasRetAttr(Attribute::NoUndef) ||7768 CB->hasRetAttr(Attribute::Dereferenceable) ||7769 CB->hasRetAttr(Attribute::DereferenceableOrNull))7770 return true;7771 }7772 7773 if (!::canCreateUndefOrPoison(Opr, Kind,7774 /*ConsiderFlagsAndMetadata=*/true)) {7775 if (const auto *PN = dyn_cast<PHINode>(V)) {7776 unsigned Num = PN->getNumIncomingValues();7777 bool IsWellDefined = true;7778 for (unsigned i = 0; i < Num; ++i) {7779 if (PN == PN->getIncomingValue(i))7780 continue;7781 auto *TI = PN->getIncomingBlock(i)->getTerminator();7782 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,7783 DT, Depth + 1, Kind)) {7784 IsWellDefined = false;7785 break;7786 }7787 }7788 if (IsWellDefined)7789 return true;7790 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)7791 : nullptr) {7792 // For splats we only need to check the value being splatted.7793 if (OpCheck(Splat))7794 return true;7795 } else if (all_of(Opr->operands(), OpCheck))7796 return true;7797 }7798 }7799 7800 if (auto *I = dyn_cast<LoadInst>(V))7801 if (I->hasMetadata(LLVMContext::MD_noundef) ||7802 I->hasMetadata(LLVMContext::MD_dereferenceable) ||7803 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))7804 return true;7805 7806 if (programUndefinedIfUndefOrPoison(V, !includesUndef(Kind)))7807 return true;7808 7809 // CxtI may be null or a cloned instruction.7810 if (!CtxI || !CtxI->getParent() || !DT)7811 return false;7812 7813 auto *DNode = DT->getNode(CtxI->getParent());7814 if (!DNode)7815 // Unreachable block7816 return false;7817 7818 // If V is used as a branch condition before reaching CtxI, V cannot be7819 // undef or poison.7820 // br V, BB1, BB27821 // BB1:7822 // CtxI ; V cannot be undef or poison here7823 auto *Dominator = DNode->getIDom();7824 // This check is purely for compile time reasons: we can skip the IDom walk7825 // if what we are checking for includes undef and the value is not an integer.7826 if (!includesUndef(Kind) || V->getType()->isIntegerTy())7827 while (Dominator) {7828 auto *TI = Dominator->getBlock()->getTerminator();7829 7830 Value *Cond = nullptr;7831 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {7832 if (BI->isConditional())7833 Cond = BI->getCondition();7834 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {7835 Cond = SI->getCondition();7836 }7837 7838 if (Cond) {7839 if (Cond == V)7840 return true;7841 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {7842 // For poison, we can analyze further7843 auto *Opr = cast<Operator>(Cond);7844 if (any_of(Opr->operands(), [V](const Use &U) {7845 return V == U && propagatesPoison(U);7846 }))7847 return true;7848 }7849 }7850 7851 Dominator = Dominator->getIDom();7852 }7853 7854 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))7855 return true;7856 7857 return false;7858}7859 7860bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC,7861 const Instruction *CtxI,7862 const DominatorTree *DT,7863 unsigned Depth) {7864 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,7865 UndefPoisonKind::UndefOrPoison);7866}7867 7868bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC,7869 const Instruction *CtxI,7870 const DominatorTree *DT, unsigned Depth) {7871 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,7872 UndefPoisonKind::PoisonOnly);7873}7874 7875bool llvm::isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC,7876 const Instruction *CtxI,7877 const DominatorTree *DT, unsigned Depth) {7878 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,7879 UndefPoisonKind::UndefOnly);7880}7881 7882/// Return true if undefined behavior would provably be executed on the path to7883/// OnPathTo if Root produced a posion result. Note that this doesn't say7884/// anything about whether OnPathTo is actually executed or whether Root is7885/// actually poison. This can be used to assess whether a new use of Root can7886/// be added at a location which is control equivalent with OnPathTo (such as7887/// immediately before it) without introducing UB which didn't previously7888/// exist. Note that a false result conveys no information.7889bool llvm::mustExecuteUBIfPoisonOnPathTo(Instruction *Root,7890 Instruction *OnPathTo,7891 DominatorTree *DT) {7892 // Basic approach is to assume Root is poison, propagate poison forward7893 // through all users we can easily track, and then check whether any of those7894 // users are provable UB and must execute before out exiting block might7895 // exit.7896 7897 // The set of all recursive users we've visited (which are assumed to all be7898 // poison because of said visit)7899 SmallPtrSet<const Value *, 16> KnownPoison;7900 SmallVector<const Instruction*, 16> Worklist;7901 Worklist.push_back(Root);7902 while (!Worklist.empty()) {7903 const Instruction *I = Worklist.pop_back_val();7904 7905 // If we know this must trigger UB on a path leading our target.7906 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))7907 return true;7908 7909 // If we can't analyze propagation through this instruction, just skip it7910 // and transitive users. Safe as false is a conservative result.7911 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {7912 return KnownPoison.contains(U) && propagatesPoison(U);7913 }))7914 continue;7915 7916 if (KnownPoison.insert(I).second)7917 for (const User *User : I->users())7918 Worklist.push_back(cast<Instruction>(User));7919 }7920 7921 // Might be non-UB, or might have a path we couldn't prove must execute on7922 // way to exiting bb.7923 return false;7924}7925 7926OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,7927 const SimplifyQuery &SQ) {7928 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),7929 Add, SQ);7930}7931 7932OverflowResult7933llvm::computeOverflowForSignedAdd(const WithCache<const Value *> &LHS,7934 const WithCache<const Value *> &RHS,7935 const SimplifyQuery &SQ) {7936 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);7937}7938 7939bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {7940 // Note: An atomic operation isn't guaranteed to return in a reasonable amount7941 // of time because it's possible for another thread to interfere with it for an7942 // arbitrary length of time, but programs aren't allowed to rely on that.7943 7944 // If there is no successor, then execution can't transfer to it.7945 if (isa<ReturnInst>(I))7946 return false;7947 if (isa<UnreachableInst>(I))7948 return false;7949 7950 // Note: Do not add new checks here; instead, change Instruction::mayThrow or7951 // Instruction::willReturn.7952 //7953 // FIXME: Move this check into Instruction::willReturn.7954 if (isa<CatchPadInst>(I)) {7955 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {7956 default:7957 // A catchpad may invoke exception object constructors and such, which7958 // in some languages can be arbitrary code, so be conservative by default.7959 return false;7960 case EHPersonality::CoreCLR:7961 // For CoreCLR, it just involves a type test.7962 return true;7963 }7964 }7965 7966 // An instruction that returns without throwing must transfer control flow7967 // to a successor.7968 return !I->mayThrow() && I->willReturn();7969}7970 7971bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {7972 // TODO: This is slightly conservative for invoke instruction since exiting7973 // via an exception *is* normal control for them.7974 for (const Instruction &I : *BB)7975 if (!isGuaranteedToTransferExecutionToSuccessor(&I))7976 return false;7977 return true;7978}7979 7980bool llvm::isGuaranteedToTransferExecutionToSuccessor(7981 BasicBlock::const_iterator Begin, BasicBlock::const_iterator End,7982 unsigned ScanLimit) {7983 return isGuaranteedToTransferExecutionToSuccessor(make_range(Begin, End),7984 ScanLimit);7985}7986 7987bool llvm::isGuaranteedToTransferExecutionToSuccessor(7988 iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit) {7989 assert(ScanLimit && "scan limit must be non-zero");7990 for (const Instruction &I : Range) {7991 if (--ScanLimit == 0)7992 return false;7993 if (!isGuaranteedToTransferExecutionToSuccessor(&I))7994 return false;7995 }7996 return true;7997}7998 7999bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,8000 const Loop *L) {8001 // The loop header is guaranteed to be executed for every iteration.8002 //8003 // FIXME: Relax this constraint to cover all basic blocks that are8004 // guaranteed to be executed at every iteration.8005 if (I->getParent() != L->getHeader()) return false;8006 8007 for (const Instruction &LI : *L->getHeader()) {8008 if (&LI == I) return true;8009 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;8010 }8011 llvm_unreachable("Instruction not contained in its own parent basic block.");8012}8013 8014bool llvm::intrinsicPropagatesPoison(Intrinsic::ID IID) {8015 switch (IID) {8016 // TODO: Add more intrinsics.8017 case Intrinsic::sadd_with_overflow:8018 case Intrinsic::ssub_with_overflow:8019 case Intrinsic::smul_with_overflow:8020 case Intrinsic::uadd_with_overflow:8021 case Intrinsic::usub_with_overflow:8022 case Intrinsic::umul_with_overflow:8023 // If an input is a vector containing a poison element, the8024 // two output vectors (calculated results, overflow bits)'8025 // corresponding lanes are poison.8026 return true;8027 case Intrinsic::ctpop:8028 case Intrinsic::ctlz:8029 case Intrinsic::cttz:8030 case Intrinsic::abs:8031 case Intrinsic::smax:8032 case Intrinsic::smin:8033 case Intrinsic::umax:8034 case Intrinsic::umin:8035 case Intrinsic::scmp:8036 case Intrinsic::is_fpclass:8037 case Intrinsic::ptrmask:8038 case Intrinsic::ucmp:8039 case Intrinsic::bitreverse:8040 case Intrinsic::bswap:8041 case Intrinsic::sadd_sat:8042 case Intrinsic::ssub_sat:8043 case Intrinsic::sshl_sat:8044 case Intrinsic::uadd_sat:8045 case Intrinsic::usub_sat:8046 case Intrinsic::ushl_sat:8047 case Intrinsic::smul_fix:8048 case Intrinsic::smul_fix_sat:8049 case Intrinsic::umul_fix:8050 case Intrinsic::umul_fix_sat:8051 case Intrinsic::pow:8052 case Intrinsic::powi:8053 case Intrinsic::sin:8054 case Intrinsic::sinh:8055 case Intrinsic::cos:8056 case Intrinsic::cosh:8057 case Intrinsic::sincos:8058 case Intrinsic::sincospi:8059 case Intrinsic::tan:8060 case Intrinsic::tanh:8061 case Intrinsic::asin:8062 case Intrinsic::acos:8063 case Intrinsic::atan:8064 case Intrinsic::atan2:8065 case Intrinsic::canonicalize:8066 case Intrinsic::sqrt:8067 case Intrinsic::exp:8068 case Intrinsic::exp2:8069 case Intrinsic::exp10:8070 case Intrinsic::log:8071 case Intrinsic::log2:8072 case Intrinsic::log10:8073 case Intrinsic::modf:8074 case Intrinsic::floor:8075 case Intrinsic::ceil:8076 case Intrinsic::trunc:8077 case Intrinsic::rint:8078 case Intrinsic::nearbyint:8079 case Intrinsic::round:8080 case Intrinsic::roundeven:8081 case Intrinsic::lrint:8082 case Intrinsic::llrint:8083 return true;8084 default:8085 return false;8086 }8087}8088 8089bool llvm::propagatesPoison(const Use &PoisonOp) {8090 const Operator *I = cast<Operator>(PoisonOp.getUser());8091 switch (I->getOpcode()) {8092 case Instruction::Freeze:8093 case Instruction::PHI:8094 case Instruction::Invoke:8095 return false;8096 case Instruction::Select:8097 return PoisonOp.getOperandNo() == 0;8098 case Instruction::Call:8099 if (auto *II = dyn_cast<IntrinsicInst>(I))8100 return intrinsicPropagatesPoison(II->getIntrinsicID());8101 return false;8102 case Instruction::ICmp:8103 case Instruction::FCmp:8104 case Instruction::GetElementPtr:8105 return true;8106 default:8107 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))8108 return true;8109 8110 // Be conservative and return false.8111 return false;8112 }8113}8114 8115/// Enumerates all operands of \p I that are guaranteed to not be undef or8116/// poison. If the callback \p Handle returns true, stop processing and return8117/// true. Otherwise, return false.8118template <typename CallableT>8119static bool handleGuaranteedWellDefinedOps(const Instruction *I,8120 const CallableT &Handle) {8121 switch (I->getOpcode()) {8122 case Instruction::Store:8123 if (Handle(cast<StoreInst>(I)->getPointerOperand()))8124 return true;8125 break;8126 8127 case Instruction::Load:8128 if (Handle(cast<LoadInst>(I)->getPointerOperand()))8129 return true;8130 break;8131 8132 // Since dereferenceable attribute imply noundef, atomic operations8133 // also implicitly have noundef pointers too8134 case Instruction::AtomicCmpXchg:8135 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))8136 return true;8137 break;8138 8139 case Instruction::AtomicRMW:8140 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))8141 return true;8142 break;8143 8144 case Instruction::Call:8145 case Instruction::Invoke: {8146 const CallBase *CB = cast<CallBase>(I);8147 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))8148 return true;8149 for (unsigned i = 0; i < CB->arg_size(); ++i)8150 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||8151 CB->paramHasAttr(i, Attribute::Dereferenceable) ||8152 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&8153 Handle(CB->getArgOperand(i)))8154 return true;8155 break;8156 }8157 case Instruction::Ret:8158 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&8159 Handle(I->getOperand(0)))8160 return true;8161 break;8162 case Instruction::Switch:8163 if (Handle(cast<SwitchInst>(I)->getCondition()))8164 return true;8165 break;8166 case Instruction::Br: {8167 auto *BR = cast<BranchInst>(I);8168 if (BR->isConditional() && Handle(BR->getCondition()))8169 return true;8170 break;8171 }8172 default:8173 break;8174 }8175 8176 return false;8177}8178 8179/// Enumerates all operands of \p I that are guaranteed to not be poison.8180template <typename CallableT>8181static bool handleGuaranteedNonPoisonOps(const Instruction *I,8182 const CallableT &Handle) {8183 if (handleGuaranteedWellDefinedOps(I, Handle))8184 return true;8185 switch (I->getOpcode()) {8186 // Divisors of these operations are allowed to be partially undef.8187 case Instruction::UDiv:8188 case Instruction::SDiv:8189 case Instruction::URem:8190 case Instruction::SRem:8191 return Handle(I->getOperand(1));8192 default:8193 return false;8194 }8195}8196 8197bool llvm::mustTriggerUB(const Instruction *I,8198 const SmallPtrSetImpl<const Value *> &KnownPoison) {8199 return handleGuaranteedNonPoisonOps(8200 I, [&](const Value *V) { return KnownPoison.count(V); });8201}8202 8203static bool programUndefinedIfUndefOrPoison(const Value *V,8204 bool PoisonOnly) {8205 // We currently only look for uses of values within the same basic8206 // block, as that makes it easier to guarantee that the uses will be8207 // executed given that Inst is executed.8208 //8209 // FIXME: Expand this to consider uses beyond the same basic block. To do8210 // this, look out for the distinction between post-dominance and strong8211 // post-dominance.8212 const BasicBlock *BB = nullptr;8213 BasicBlock::const_iterator Begin;8214 if (const auto *Inst = dyn_cast<Instruction>(V)) {8215 BB = Inst->getParent();8216 Begin = Inst->getIterator();8217 Begin++;8218 } else if (const auto *Arg = dyn_cast<Argument>(V)) {8219 if (Arg->getParent()->isDeclaration())8220 return false;8221 BB = &Arg->getParent()->getEntryBlock();8222 Begin = BB->begin();8223 } else {8224 return false;8225 }8226 8227 // Limit number of instructions we look at, to avoid scanning through large8228 // blocks. The current limit is chosen arbitrarily.8229 unsigned ScanLimit = 32;8230 BasicBlock::const_iterator End = BB->end();8231 8232 if (!PoisonOnly) {8233 // Since undef does not propagate eagerly, be conservative & just check8234 // whether a value is directly passed to an instruction that must take8235 // well-defined operands.8236 8237 for (const auto &I : make_range(Begin, End)) {8238 if (--ScanLimit == 0)8239 break;8240 8241 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {8242 return WellDefinedOp == V;8243 }))8244 return true;8245 8246 if (!isGuaranteedToTransferExecutionToSuccessor(&I))8247 break;8248 }8249 return false;8250 }8251 8252 // Set of instructions that we have proved will yield poison if Inst8253 // does.8254 SmallPtrSet<const Value *, 16> YieldsPoison;8255 SmallPtrSet<const BasicBlock *, 4> Visited;8256 8257 YieldsPoison.insert(V);8258 Visited.insert(BB);8259 8260 while (true) {8261 for (const auto &I : make_range(Begin, End)) {8262 if (--ScanLimit == 0)8263 return false;8264 if (mustTriggerUB(&I, YieldsPoison))8265 return true;8266 if (!isGuaranteedToTransferExecutionToSuccessor(&I))8267 return false;8268 8269 // If an operand is poison and propagates it, mark I as yielding poison.8270 for (const Use &Op : I.operands()) {8271 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {8272 YieldsPoison.insert(&I);8273 break;8274 }8275 }8276 8277 // Special handling for select, which returns poison if its operand 0 is8278 // poison (handled in the loop above) *or* if both its true/false operands8279 // are poison (handled here).8280 if (I.getOpcode() == Instruction::Select &&8281 YieldsPoison.count(I.getOperand(1)) &&8282 YieldsPoison.count(I.getOperand(2))) {8283 YieldsPoison.insert(&I);8284 }8285 }8286 8287 BB = BB->getSingleSuccessor();8288 if (!BB || !Visited.insert(BB).second)8289 break;8290 8291 Begin = BB->getFirstNonPHIIt();8292 End = BB->end();8293 }8294 return false;8295}8296 8297bool llvm::programUndefinedIfUndefOrPoison(const Instruction *Inst) {8298 return ::programUndefinedIfUndefOrPoison(Inst, false);8299}8300 8301bool llvm::programUndefinedIfPoison(const Instruction *Inst) {8302 return ::programUndefinedIfUndefOrPoison(Inst, true);8303}8304 8305static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {8306 if (FMF.noNaNs())8307 return true;8308 8309 if (auto *C = dyn_cast<ConstantFP>(V))8310 return !C->isNaN();8311 8312 if (auto *C = dyn_cast<ConstantDataVector>(V)) {8313 if (!C->getElementType()->isFloatingPointTy())8314 return false;8315 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {8316 if (C->getElementAsAPFloat(I).isNaN())8317 return false;8318 }8319 return true;8320 }8321 8322 if (isa<ConstantAggregateZero>(V))8323 return true;8324 8325 return false;8326}8327 8328static bool isKnownNonZero(const Value *V) {8329 if (auto *C = dyn_cast<ConstantFP>(V))8330 return !C->isZero();8331 8332 if (auto *C = dyn_cast<ConstantDataVector>(V)) {8333 if (!C->getElementType()->isFloatingPointTy())8334 return false;8335 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {8336 if (C->getElementAsAPFloat(I).isZero())8337 return false;8338 }8339 return true;8340 }8341 8342 return false;8343}8344 8345/// Match clamp pattern for float types without care about NaNs or signed zeros.8346/// Given non-min/max outer cmp/select from the clamp pattern this8347/// function recognizes if it can be substitued by a "canonical" min/max8348/// pattern.8349static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,8350 Value *CmpLHS, Value *CmpRHS,8351 Value *TrueVal, Value *FalseVal,8352 Value *&LHS, Value *&RHS) {8353 // Try to match8354 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))8355 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))8356 // and return description of the outer Max/Min.8357 8358 // First, check if select has inverse order:8359 if (CmpRHS == FalseVal) {8360 std::swap(TrueVal, FalseVal);8361 Pred = CmpInst::getInversePredicate(Pred);8362 }8363 8364 // Assume success now. If there's no match, callers should not use these anyway.8365 LHS = TrueVal;8366 RHS = FalseVal;8367 8368 const APFloat *FC1;8369 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())8370 return {SPF_UNKNOWN, SPNB_NA, false};8371 8372 const APFloat *FC2;8373 switch (Pred) {8374 case CmpInst::FCMP_OLT:8375 case CmpInst::FCMP_OLE:8376 case CmpInst::FCMP_ULT:8377 case CmpInst::FCMP_ULE:8378 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&8379 *FC1 < *FC2)8380 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};8381 break;8382 case CmpInst::FCMP_OGT:8383 case CmpInst::FCMP_OGE:8384 case CmpInst::FCMP_UGT:8385 case CmpInst::FCMP_UGE:8386 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&8387 *FC1 > *FC2)8388 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};8389 break;8390 default:8391 break;8392 }8393 8394 return {SPF_UNKNOWN, SPNB_NA, false};8395}8396 8397/// Recognize variations of:8398/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))8399static SelectPatternResult matchClamp(CmpInst::Predicate Pred,8400 Value *CmpLHS, Value *CmpRHS,8401 Value *TrueVal, Value *FalseVal) {8402 // Swap the select operands and predicate to match the patterns below.8403 if (CmpRHS != TrueVal) {8404 Pred = ICmpInst::getSwappedPredicate(Pred);8405 std::swap(TrueVal, FalseVal);8406 }8407 const APInt *C1;8408 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {8409 const APInt *C2;8410 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)8411 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&8412 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)8413 return {SPF_SMAX, SPNB_NA, false};8414 8415 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)8416 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&8417 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)8418 return {SPF_SMIN, SPNB_NA, false};8419 8420 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)8421 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&8422 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)8423 return {SPF_UMAX, SPNB_NA, false};8424 8425 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)8426 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&8427 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)8428 return {SPF_UMIN, SPNB_NA, false};8429 }8430 return {SPF_UNKNOWN, SPNB_NA, false};8431}8432 8433/// Recognize variations of:8434/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))8435static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,8436 Value *CmpLHS, Value *CmpRHS,8437 Value *TVal, Value *FVal,8438 unsigned Depth) {8439 // TODO: Allow FP min/max with nnan/nsz.8440 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");8441 8442 Value *A = nullptr, *B = nullptr;8443 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);8444 if (!SelectPatternResult::isMinOrMax(L.Flavor))8445 return {SPF_UNKNOWN, SPNB_NA, false};8446 8447 Value *C = nullptr, *D = nullptr;8448 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);8449 if (L.Flavor != R.Flavor)8450 return {SPF_UNKNOWN, SPNB_NA, false};8451 8452 // We have something like: x Pred y ? min(a, b) : min(c, d).8453 // Try to match the compare to the min/max operations of the select operands.8454 // First, make sure we have the right compare predicate.8455 switch (L.Flavor) {8456 case SPF_SMIN:8457 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {8458 Pred = ICmpInst::getSwappedPredicate(Pred);8459 std::swap(CmpLHS, CmpRHS);8460 }8461 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)8462 break;8463 return {SPF_UNKNOWN, SPNB_NA, false};8464 case SPF_SMAX:8465 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {8466 Pred = ICmpInst::getSwappedPredicate(Pred);8467 std::swap(CmpLHS, CmpRHS);8468 }8469 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)8470 break;8471 return {SPF_UNKNOWN, SPNB_NA, false};8472 case SPF_UMIN:8473 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {8474 Pred = ICmpInst::getSwappedPredicate(Pred);8475 std::swap(CmpLHS, CmpRHS);8476 }8477 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)8478 break;8479 return {SPF_UNKNOWN, SPNB_NA, false};8480 case SPF_UMAX:8481 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {8482 Pred = ICmpInst::getSwappedPredicate(Pred);8483 std::swap(CmpLHS, CmpRHS);8484 }8485 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)8486 break;8487 return {SPF_UNKNOWN, SPNB_NA, false};8488 default:8489 return {SPF_UNKNOWN, SPNB_NA, false};8490 }8491 8492 // If there is a common operand in the already matched min/max and the other8493 // min/max operands match the compare operands (either directly or inverted),8494 // then this is min/max of the same flavor.8495 8496 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))8497 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))8498 if (D == B) {8499 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&8500 match(A, m_Not(m_Specific(CmpRHS)))))8501 return {L.Flavor, SPNB_NA, false};8502 }8503 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))8504 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))8505 if (C == B) {8506 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&8507 match(A, m_Not(m_Specific(CmpRHS)))))8508 return {L.Flavor, SPNB_NA, false};8509 }8510 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))8511 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))8512 if (D == A) {8513 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&8514 match(B, m_Not(m_Specific(CmpRHS)))))8515 return {L.Flavor, SPNB_NA, false};8516 }8517 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))8518 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))8519 if (C == A) {8520 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&8521 match(B, m_Not(m_Specific(CmpRHS)))))8522 return {L.Flavor, SPNB_NA, false};8523 }8524 8525 return {SPF_UNKNOWN, SPNB_NA, false};8526}8527 8528/// If the input value is the result of a 'not' op, constant integer, or vector8529/// splat of a constant integer, return the bitwise-not source value.8530/// TODO: This could be extended to handle non-splat vector integer constants.8531static Value *getNotValue(Value *V) {8532 Value *NotV;8533 if (match(V, m_Not(m_Value(NotV))))8534 return NotV;8535 8536 const APInt *C;8537 if (match(V, m_APInt(C)))8538 return ConstantInt::get(V->getType(), ~(*C));8539 8540 return nullptr;8541}8542 8543/// Match non-obvious integer minimum and maximum sequences.8544static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,8545 Value *CmpLHS, Value *CmpRHS,8546 Value *TrueVal, Value *FalseVal,8547 Value *&LHS, Value *&RHS,8548 unsigned Depth) {8549 // Assume success. If there's no match, callers should not use these anyway.8550 LHS = TrueVal;8551 RHS = FalseVal;8552 8553 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);8554 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)8555 return SPR;8556 8557 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);8558 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)8559 return SPR;8560 8561 // Look through 'not' ops to find disguised min/max.8562 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)8563 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)8564 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {8565 switch (Pred) {8566 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};8567 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};8568 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};8569 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};8570 default: break;8571 }8572 }8573 8574 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)8575 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)8576 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {8577 switch (Pred) {8578 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};8579 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};8580 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};8581 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};8582 default: break;8583 }8584 }8585 8586 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)8587 return {SPF_UNKNOWN, SPNB_NA, false};8588 8589 const APInt *C1;8590 if (!match(CmpRHS, m_APInt(C1)))8591 return {SPF_UNKNOWN, SPNB_NA, false};8592 8593 // An unsigned min/max can be written with a signed compare.8594 const APInt *C2;8595 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||8596 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {8597 // Is the sign bit set?8598 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX8599 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN8600 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())8601 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};8602 8603 // Is the sign bit clear?8604 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX8605 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN8606 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())8607 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};8608 }8609 8610 return {SPF_UNKNOWN, SPNB_NA, false};8611}8612 8613bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,8614 bool AllowPoison) {8615 assert(X && Y && "Invalid operand");8616 8617 auto IsNegationOf = [&](const Value *X, const Value *Y) {8618 if (!match(X, m_Neg(m_Specific(Y))))8619 return false;8620 8621 auto *BO = cast<BinaryOperator>(X);8622 if (NeedNSW && !BO->hasNoSignedWrap())8623 return false;8624 8625 auto *Zero = cast<Constant>(BO->getOperand(0));8626 if (!AllowPoison && !Zero->isNullValue())8627 return false;8628 8629 return true;8630 };8631 8632 // X = -Y or Y = -X8633 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))8634 return true;8635 8636 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)8637 Value *A, *B;8638 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&8639 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||8640 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&8641 match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));8642}8643 8644bool llvm::isKnownInversion(const Value *X, const Value *Y) {8645 // Handle X = icmp pred A, B, Y = icmp pred A, C.8646 Value *A, *B, *C;8647 CmpPredicate Pred1, Pred2;8648 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||8649 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))8650 return false;8651 8652 // They must both have samesign flag or not.8653 if (Pred1.hasSameSign() != Pred2.hasSameSign())8654 return false;8655 8656 if (B == C)8657 return Pred1 == ICmpInst::getInversePredicate(Pred2);8658 8659 // Try to infer the relationship from constant ranges.8660 const APInt *RHSC1, *RHSC2;8661 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))8662 return false;8663 8664 // Sign bits of two RHSCs should match.8665 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())8666 return false;8667 8668 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);8669 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);8670 8671 return CR1.inverse() == CR2;8672}8673 8674SelectPatternResult llvm::getSelectPattern(CmpInst::Predicate Pred,8675 SelectPatternNaNBehavior NaNBehavior,8676 bool Ordered) {8677 switch (Pred) {8678 default:8679 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.8680 case ICmpInst::ICMP_UGT:8681 case ICmpInst::ICMP_UGE:8682 return {SPF_UMAX, SPNB_NA, false};8683 case ICmpInst::ICMP_SGT:8684 case ICmpInst::ICMP_SGE:8685 return {SPF_SMAX, SPNB_NA, false};8686 case ICmpInst::ICMP_ULT:8687 case ICmpInst::ICMP_ULE:8688 return {SPF_UMIN, SPNB_NA, false};8689 case ICmpInst::ICMP_SLT:8690 case ICmpInst::ICMP_SLE:8691 return {SPF_SMIN, SPNB_NA, false};8692 case FCmpInst::FCMP_UGT:8693 case FCmpInst::FCMP_UGE:8694 case FCmpInst::FCMP_OGT:8695 case FCmpInst::FCMP_OGE:8696 return {SPF_FMAXNUM, NaNBehavior, Ordered};8697 case FCmpInst::FCMP_ULT:8698 case FCmpInst::FCMP_ULE:8699 case FCmpInst::FCMP_OLT:8700 case FCmpInst::FCMP_OLE:8701 return {SPF_FMINNUM, NaNBehavior, Ordered};8702 }8703}8704 8705std::optional<std::pair<CmpPredicate, Constant *>>8706llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) {8707 assert(ICmpInst::isRelational(Pred) && ICmpInst::isIntPredicate(Pred) &&8708 "Only for relational integer predicates.");8709 if (isa<UndefValue>(C))8710 return std::nullopt;8711 8712 Type *Type = C->getType();8713 bool IsSigned = ICmpInst::isSigned(Pred);8714 8715 CmpInst::Predicate UnsignedPred = ICmpInst::getUnsignedPredicate(Pred);8716 bool WillIncrement =8717 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;8718 8719 // Check if the constant operand can be safely incremented/decremented8720 // without overflowing/underflowing.8721 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {8722 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);8723 };8724 8725 Constant *SafeReplacementConstant = nullptr;8726 if (auto *CI = dyn_cast<ConstantInt>(C)) {8727 // Bail out if the constant can't be safely incremented/decremented.8728 if (!ConstantIsOk(CI))8729 return std::nullopt;8730 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {8731 unsigned NumElts = FVTy->getNumElements();8732 for (unsigned i = 0; i != NumElts; ++i) {8733 Constant *Elt = C->getAggregateElement(i);8734 if (!Elt)8735 return std::nullopt;8736 8737 if (isa<UndefValue>(Elt))8738 continue;8739 8740 // Bail out if we can't determine if this constant is min/max or if we8741 // know that this constant is min/max.8742 auto *CI = dyn_cast<ConstantInt>(Elt);8743 if (!CI || !ConstantIsOk(CI))8744 return std::nullopt;8745 8746 if (!SafeReplacementConstant)8747 SafeReplacementConstant = CI;8748 }8749 } else if (isa<VectorType>(C->getType())) {8750 // Handle scalable splat8751 Value *SplatC = C->getSplatValue();8752 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);8753 // Bail out if the constant can't be safely incremented/decremented.8754 if (!CI || !ConstantIsOk(CI))8755 return std::nullopt;8756 } else {8757 // ConstantExpr?8758 return std::nullopt;8759 }8760 8761 // It may not be safe to change a compare predicate in the presence of8762 // undefined elements, so replace those elements with the first safe constant8763 // that we found.8764 // TODO: in case of poison, it is safe; let's replace undefs only.8765 if (C->containsUndefOrPoisonElement()) {8766 assert(SafeReplacementConstant && "Replacement constant not set");8767 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);8768 }8769 8770 CmpInst::Predicate NewPred = CmpInst::getFlippedStrictnessPredicate(Pred);8771 8772 // Increment or decrement the constant.8773 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);8774 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);8775 8776 return std::make_pair(NewPred, NewC);8777}8778 8779static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,8780 FastMathFlags FMF,8781 Value *CmpLHS, Value *CmpRHS,8782 Value *TrueVal, Value *FalseVal,8783 Value *&LHS, Value *&RHS,8784 unsigned Depth) {8785 bool HasMismatchedZeros = false;8786 if (CmpInst::isFPPredicate(Pred)) {8787 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one8788 // 0.0 operand, set the compare's 0.0 operands to that same value for the8789 // purpose of identifying min/max. Disregard vector constants with undefined8790 // elements because those can not be back-propagated for analysis.8791 Value *OutputZeroVal = nullptr;8792 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&8793 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())8794 OutputZeroVal = TrueVal;8795 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&8796 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())8797 OutputZeroVal = FalseVal;8798 8799 if (OutputZeroVal) {8800 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {8801 HasMismatchedZeros = true;8802 CmpLHS = OutputZeroVal;8803 }8804 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {8805 HasMismatchedZeros = true;8806 CmpRHS = OutputZeroVal;8807 }8808 }8809 }8810 8811 LHS = CmpLHS;8812 RHS = CmpRHS;8813 8814 // Signed zero may return inconsistent results between implementations.8815 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.08816 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)8817 // Therefore, we behave conservatively and only proceed if at least one of the8818 // operands is known to not be zero or if we don't care about signed zero.8819 switch (Pred) {8820 default: break;8821 case CmpInst::FCMP_OGT: case CmpInst::FCMP_OLT:8822 case CmpInst::FCMP_UGT: case CmpInst::FCMP_ULT:8823 if (!HasMismatchedZeros)8824 break;8825 [[fallthrough]];8826 case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:8827 case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:8828 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&8829 !isKnownNonZero(CmpRHS))8830 return {SPF_UNKNOWN, SPNB_NA, false};8831 }8832 8833 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;8834 bool Ordered = false;8835 8836 // When given one NaN and one non-NaN input:8837 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.8838 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the8839 // ordered comparison fails), which could be NaN or non-NaN.8840 // so here we discover exactly what NaN behavior is required/accepted.8841 if (CmpInst::isFPPredicate(Pred)) {8842 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);8843 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);8844 8845 if (LHSSafe && RHSSafe) {8846 // Both operands are known non-NaN.8847 NaNBehavior = SPNB_RETURNS_ANY;8848 Ordered = CmpInst::isOrdered(Pred);8849 } else if (CmpInst::isOrdered(Pred)) {8850 // An ordered comparison will return false when given a NaN, so it8851 // returns the RHS.8852 Ordered = true;8853 if (LHSSafe)8854 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.8855 NaNBehavior = SPNB_RETURNS_NAN;8856 else if (RHSSafe)8857 NaNBehavior = SPNB_RETURNS_OTHER;8858 else8859 // Completely unsafe.8860 return {SPF_UNKNOWN, SPNB_NA, false};8861 } else {8862 Ordered = false;8863 // An unordered comparison will return true when given a NaN, so it8864 // returns the LHS.8865 if (LHSSafe)8866 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.8867 NaNBehavior = SPNB_RETURNS_OTHER;8868 else if (RHSSafe)8869 NaNBehavior = SPNB_RETURNS_NAN;8870 else8871 // Completely unsafe.8872 return {SPF_UNKNOWN, SPNB_NA, false};8873 }8874 }8875 8876 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {8877 std::swap(CmpLHS, CmpRHS);8878 Pred = CmpInst::getSwappedPredicate(Pred);8879 if (NaNBehavior == SPNB_RETURNS_NAN)8880 NaNBehavior = SPNB_RETURNS_OTHER;8881 else if (NaNBehavior == SPNB_RETURNS_OTHER)8882 NaNBehavior = SPNB_RETURNS_NAN;8883 Ordered = !Ordered;8884 }8885 8886 // ([if]cmp X, Y) ? X : Y8887 if (TrueVal == CmpLHS && FalseVal == CmpRHS)8888 return getSelectPattern(Pred, NaNBehavior, Ordered);8889 8890 if (isKnownNegation(TrueVal, FalseVal)) {8891 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can8892 // match against either LHS or sext(LHS).8893 auto MaybeSExtCmpLHS =8894 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));8895 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());8896 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());8897 if (match(TrueVal, MaybeSExtCmpLHS)) {8898 // Set the return values. If the compare uses the negated value (-X >s 0),8899 // swap the return values because the negated value is always 'RHS'.8900 LHS = TrueVal;8901 RHS = FalseVal;8902 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))8903 std::swap(LHS, RHS);8904 8905 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)8906 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)8907 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))8908 return {SPF_ABS, SPNB_NA, false};8909 8910 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)8911 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))8912 return {SPF_ABS, SPNB_NA, false};8913 8914 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)8915 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)8916 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))8917 return {SPF_NABS, SPNB_NA, false};8918 }8919 else if (match(FalseVal, MaybeSExtCmpLHS)) {8920 // Set the return values. If the compare uses the negated value (-X >s 0),8921 // swap the return values because the negated value is always 'RHS'.8922 LHS = FalseVal;8923 RHS = TrueVal;8924 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))8925 std::swap(LHS, RHS);8926 8927 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)8928 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)8929 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))8930 return {SPF_NABS, SPNB_NA, false};8931 8932 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)8933 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)8934 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))8935 return {SPF_ABS, SPNB_NA, false};8936 }8937 }8938 8939 if (CmpInst::isIntPredicate(Pred))8940 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);8941 8942 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar8943 // may return either -0.0 or 0.0, so fcmp/select pair has stricter8944 // semantics than minNum. Be conservative in such case.8945 if (NaNBehavior != SPNB_RETURNS_ANY ||8946 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&8947 !isKnownNonZero(CmpRHS)))8948 return {SPF_UNKNOWN, SPNB_NA, false};8949 8950 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);8951}8952 8953static Value *lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C,8954 Instruction::CastOps *CastOp) {8955 const DataLayout &DL = CmpI->getDataLayout();8956 8957 Constant *CastedTo = nullptr;8958 switch (*CastOp) {8959 case Instruction::ZExt:8960 if (CmpI->isUnsigned())8961 CastedTo = ConstantExpr::getTrunc(C, SrcTy);8962 break;8963 case Instruction::SExt:8964 if (CmpI->isSigned())8965 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);8966 break;8967 case Instruction::Trunc:8968 Constant *CmpConst;8969 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&8970 CmpConst->getType() == SrcTy) {8971 // Here we have the following case:8972 //8973 // %cond = cmp iN %x, CmpConst8974 // %tr = trunc iN %x to iK8975 // %narrowsel = select i1 %cond, iK %t, iK C8976 //8977 // We can always move trunc after select operation:8978 //8979 // %cond = cmp iN %x, CmpConst8980 // %widesel = select i1 %cond, iN %x, iN CmpConst8981 // %tr = trunc iN %widesel to iK8982 //8983 // Note that C could be extended in any way because we don't care about8984 // upper bits after truncation. It can't be abs pattern, because it would8985 // look like:8986 //8987 // select i1 %cond, x, -x.8988 //8989 // So only min/max pattern could be matched. Such match requires widened C8990 // == CmpConst. That is why set widened C = CmpConst, condition trunc8991 // CmpConst == C is checked below.8992 CastedTo = CmpConst;8993 } else {8994 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;8995 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);8996 }8997 break;8998 case Instruction::FPTrunc:8999 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);9000 break;9001 case Instruction::FPExt:9002 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);9003 break;9004 case Instruction::FPToUI:9005 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);9006 break;9007 case Instruction::FPToSI:9008 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);9009 break;9010 case Instruction::UIToFP:9011 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);9012 break;9013 case Instruction::SIToFP:9014 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);9015 break;9016 default:9017 break;9018 }9019 9020 if (!CastedTo)9021 return nullptr;9022 9023 // Make sure the cast doesn't lose any information.9024 Constant *CastedBack =9025 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);9026 if (CastedBack && CastedBack != C)9027 return nullptr;9028 9029 return CastedTo;9030}9031 9032/// Helps to match a select pattern in case of a type mismatch.9033///9034/// The function processes the case when type of true and false values of a9035/// select instruction differs from type of the cmp instruction operands because9036/// of a cast instruction. The function checks if it is legal to move the cast9037/// operation after "select". If yes, it returns the new second value of9038/// "select" (with the assumption that cast is moved):9039/// 1. As operand of cast instruction when both values of "select" are same cast9040/// instructions.9041/// 2. As restored constant (by applying reverse cast operation) when the first9042/// value of the "select" is a cast operation and the second value is a9043/// constant. It is implemented in lookThroughCastConst().9044/// 3. As one operand is cast instruction and the other is not. The operands in9045/// sel(cmp) are in different type integer.9046/// NOTE: We return only the new second value because the first value could be9047/// accessed as operand of cast instruction.9048static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,9049 Instruction::CastOps *CastOp) {9050 auto *Cast1 = dyn_cast<CastInst>(V1);9051 if (!Cast1)9052 return nullptr;9053 9054 *CastOp = Cast1->getOpcode();9055 Type *SrcTy = Cast1->getSrcTy();9056 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {9057 // If V1 and V2 are both the same cast from the same type, look through V1.9058 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())9059 return Cast2->getOperand(0);9060 return nullptr;9061 }9062 9063 auto *C = dyn_cast<Constant>(V2);9064 if (C)9065 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);9066 9067 Value *CastedTo = nullptr;9068 if (*CastOp == Instruction::Trunc) {9069 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {9070 // Here we have the following case:9071 // %y_ext = sext iK %y to iN9072 // %cond = cmp iN %x, %y_ext9073 // %tr = trunc iN %x to iK9074 // %narrowsel = select i1 %cond, iK %tr, iK %y9075 //9076 // We can always move trunc after select operation:9077 // %y_ext = sext iK %y to iN9078 // %cond = cmp iN %x, %y_ext9079 // %widesel = select i1 %cond, iN %x, iN %y_ext9080 // %tr = trunc iN %widesel to iK9081 assert(V2->getType() == Cast1->getType() &&9082 "V2 and Cast1 should be the same type.");9083 CastedTo = CmpI->getOperand(1);9084 }9085 }9086 9087 return CastedTo;9088}9089SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,9090 Instruction::CastOps *CastOp,9091 unsigned Depth) {9092 if (Depth >= MaxAnalysisRecursionDepth)9093 return {SPF_UNKNOWN, SPNB_NA, false};9094 9095 SelectInst *SI = dyn_cast<SelectInst>(V);9096 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};9097 9098 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());9099 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};9100 9101 Value *TrueVal = SI->getTrueValue();9102 Value *FalseVal = SI->getFalseValue();9103 9104 return llvm::matchDecomposedSelectPattern(9105 CmpI, TrueVal, FalseVal, LHS, RHS,9106 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),9107 CastOp, Depth);9108}9109 9110SelectPatternResult llvm::matchDecomposedSelectPattern(9111 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,9112 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {9113 CmpInst::Predicate Pred = CmpI->getPredicate();9114 Value *CmpLHS = CmpI->getOperand(0);9115 Value *CmpRHS = CmpI->getOperand(1);9116 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())9117 FMF.setNoNaNs();9118 9119 // Bail out early.9120 if (CmpI->isEquality())9121 return {SPF_UNKNOWN, SPNB_NA, false};9122 9123 // Deal with type mismatches.9124 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {9125 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {9126 // If this is a potential fmin/fmax with a cast to integer, then ignore9127 // -0.0 because there is no corresponding integer value.9128 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)9129 FMF.setNoSignedZeros();9130 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,9131 cast<CastInst>(TrueVal)->getOperand(0), C,9132 LHS, RHS, Depth);9133 }9134 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {9135 // If this is a potential fmin/fmax with a cast to integer, then ignore9136 // -0.0 because there is no corresponding integer value.9137 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)9138 FMF.setNoSignedZeros();9139 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,9140 C, cast<CastInst>(FalseVal)->getOperand(0),9141 LHS, RHS, Depth);9142 }9143 }9144 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,9145 LHS, RHS, Depth);9146}9147 9148CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {9149 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;9150 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;9151 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;9152 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;9153 if (SPF == SPF_FMINNUM)9154 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;9155 if (SPF == SPF_FMAXNUM)9156 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;9157 llvm_unreachable("unhandled!");9158}9159 9160Intrinsic::ID llvm::getMinMaxIntrinsic(SelectPatternFlavor SPF) {9161 switch (SPF) {9162 case SelectPatternFlavor::SPF_UMIN:9163 return Intrinsic::umin;9164 case SelectPatternFlavor::SPF_UMAX:9165 return Intrinsic::umax;9166 case SelectPatternFlavor::SPF_SMIN:9167 return Intrinsic::smin;9168 case SelectPatternFlavor::SPF_SMAX:9169 return Intrinsic::smax;9170 default:9171 llvm_unreachable("Unexpected SPF");9172 }9173}9174 9175SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {9176 if (SPF == SPF_SMIN) return SPF_SMAX;9177 if (SPF == SPF_UMIN) return SPF_UMAX;9178 if (SPF == SPF_SMAX) return SPF_SMIN;9179 if (SPF == SPF_UMAX) return SPF_UMIN;9180 llvm_unreachable("unhandled!");9181}9182 9183Intrinsic::ID llvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) {9184 switch (MinMaxID) {9185 case Intrinsic::smax: return Intrinsic::smin;9186 case Intrinsic::smin: return Intrinsic::smax;9187 case Intrinsic::umax: return Intrinsic::umin;9188 case Intrinsic::umin: return Intrinsic::umax;9189 // Please note that next four intrinsics may produce the same result for9190 // original and inverted case even if X != Y due to NaN is handled specially.9191 case Intrinsic::maximum: return Intrinsic::minimum;9192 case Intrinsic::minimum: return Intrinsic::maximum;9193 case Intrinsic::maxnum: return Intrinsic::minnum;9194 case Intrinsic::minnum: return Intrinsic::maxnum;9195 case Intrinsic::maximumnum:9196 return Intrinsic::minimumnum;9197 case Intrinsic::minimumnum:9198 return Intrinsic::maximumnum;9199 default: llvm_unreachable("Unexpected intrinsic");9200 }9201}9202 9203APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) {9204 switch (SPF) {9205 case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth);9206 case SPF_SMIN: return APInt::getSignedMinValue(BitWidth);9207 case SPF_UMAX: return APInt::getMaxValue(BitWidth);9208 case SPF_UMIN: return APInt::getMinValue(BitWidth);9209 default: llvm_unreachable("Unexpected flavor");9210 }9211}9212 9213std::pair<Intrinsic::ID, bool>9214llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {9215 // Check if VL contains select instructions that can be folded into a min/max9216 // vector intrinsic and return the intrinsic if it is possible.9217 // TODO: Support floating point min/max.9218 bool AllCmpSingleUse = true;9219 SelectPatternResult SelectPattern;9220 SelectPattern.Flavor = SPF_UNKNOWN;9221 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {9222 Value *LHS, *RHS;9223 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);9224 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))9225 return false;9226 if (SelectPattern.Flavor != SPF_UNKNOWN &&9227 SelectPattern.Flavor != CurrentPattern.Flavor)9228 return false;9229 SelectPattern = CurrentPattern;9230 AllCmpSingleUse &=9231 match(I, m_Select(m_OneUse(m_Value()), m_Value(), m_Value()));9232 return true;9233 })) {9234 switch (SelectPattern.Flavor) {9235 case SPF_SMIN:9236 return {Intrinsic::smin, AllCmpSingleUse};9237 case SPF_UMIN:9238 return {Intrinsic::umin, AllCmpSingleUse};9239 case SPF_SMAX:9240 return {Intrinsic::smax, AllCmpSingleUse};9241 case SPF_UMAX:9242 return {Intrinsic::umax, AllCmpSingleUse};9243 case SPF_FMAXNUM:9244 return {Intrinsic::maxnum, AllCmpSingleUse};9245 case SPF_FMINNUM:9246 return {Intrinsic::minnum, AllCmpSingleUse};9247 default:9248 llvm_unreachable("unexpected select pattern flavor");9249 }9250 }9251 return {Intrinsic::not_intrinsic, false};9252}9253 9254template <typename InstTy>9255static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,9256 Value *&Init, Value *&OtherOp) {9257 // Handle the case of a simple two-predecessor recurrence PHI.9258 // There's a lot more that could theoretically be done here, but9259 // this is sufficient to catch some interesting cases.9260 // TODO: Expand list -- gep, uadd.sat etc.9261 if (PN->getNumIncomingValues() != 2)9262 return false;9263 9264 for (unsigned I = 0; I != 2; ++I) {9265 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));9266 Operation && Operation->getNumOperands() >= 2) {9267 Value *LHS = Operation->getOperand(0);9268 Value *RHS = Operation->getOperand(1);9269 if (LHS != PN && RHS != PN)9270 continue;9271 9272 Inst = Operation;9273 Init = PN->getIncomingValue(!I);9274 OtherOp = (LHS == PN) ? RHS : LHS;9275 return true;9276 }9277 }9278 return false;9279}9280 9281bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,9282 Value *&Start, Value *&Step) {9283 // We try to match a recurrence of the form:9284 // %iv = [Start, %entry], [%iv.next, %backedge]9285 // %iv.next = binop %iv, Step9286 // Or:9287 // %iv = [Start, %entry], [%iv.next, %backedge]9288 // %iv.next = binop Step, %iv9289 return matchTwoInputRecurrence(P, BO, Start, Step);9290}9291 9292bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,9293 Value *&Start, Value *&Step) {9294 BinaryOperator *BO = nullptr;9295 P = dyn_cast<PHINode>(I->getOperand(0));9296 if (!P)9297 P = dyn_cast<PHINode>(I->getOperand(1));9298 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;9299}9300 9301bool llvm::matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I,9302 PHINode *&P, Value *&Init,9303 Value *&OtherOp) {9304 // Binary intrinsics only supported for now.9305 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||9306 I->getType() != I->getArgOperand(1)->getType())9307 return false;9308 9309 IntrinsicInst *II = nullptr;9310 P = dyn_cast<PHINode>(I->getArgOperand(0));9311 if (!P)9312 P = dyn_cast<PHINode>(I->getArgOperand(1));9313 9314 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;9315}9316 9317/// Return true if "icmp Pred LHS RHS" is always true.9318static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,9319 const Value *RHS) {9320 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)9321 return true;9322 9323 switch (Pred) {9324 default:9325 return false;9326 9327 case CmpInst::ICMP_SLE: {9328 const APInt *C;9329 9330 // LHS s<= LHS +_{nsw} C if C >= 09331 // LHS s<= LHS | C if C >= 09332 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||9333 match(RHS, m_Or(m_Specific(LHS), m_APInt(C))))9334 return !C->isNegative();9335 9336 // LHS s<= smax(LHS, V) for any V9337 if (match(RHS, m_c_SMax(m_Specific(LHS), m_Value())))9338 return true;9339 9340 // smin(RHS, V) s<= RHS for any V9341 if (match(LHS, m_c_SMin(m_Specific(RHS), m_Value())))9342 return true;9343 9344 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)9345 const Value *X;9346 const APInt *CLHS, *CRHS;9347 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&9348 match(RHS, m_NSWAddLike(m_Specific(X), m_APInt(CRHS))))9349 return CLHS->sle(*CRHS);9350 9351 return false;9352 }9353 9354 case CmpInst::ICMP_ULE: {9355 // LHS u<= LHS +_{nuw} V for any V9356 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&9357 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())9358 return true;9359 9360 // LHS u<= LHS | V for any V9361 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))9362 return true;9363 9364 // LHS u<= umax(LHS, V) for any V9365 if (match(RHS, m_c_UMax(m_Specific(LHS), m_Value())))9366 return true;9367 9368 // RHS >> V u<= RHS for any V9369 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))9370 return true;9371 9372 // RHS u/ C_ugt_1 u<= RHS9373 const APInt *C;9374 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))9375 return true;9376 9377 // RHS & V u<= RHS for any V9378 if (match(LHS, m_c_And(m_Specific(RHS), m_Value())))9379 return true;9380 9381 // umin(RHS, V) u<= RHS for any V9382 if (match(LHS, m_c_UMin(m_Specific(RHS), m_Value())))9383 return true;9384 9385 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)9386 const Value *X;9387 const APInt *CLHS, *CRHS;9388 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&9389 match(RHS, m_NUWAddLike(m_Specific(X), m_APInt(CRHS))))9390 return CLHS->ule(*CRHS);9391 9392 return false;9393 }9394 }9395}9396 9397/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred9398/// ALHS ARHS" is true. Otherwise, return std::nullopt.9399static std::optional<bool>9400isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,9401 const Value *ARHS, const Value *BLHS, const Value *BRHS) {9402 switch (Pred) {9403 default:9404 return std::nullopt;9405 9406 case CmpInst::ICMP_SLT:9407 case CmpInst::ICMP_SLE:9408 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&9409 isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS))9410 return true;9411 return std::nullopt;9412 9413 case CmpInst::ICMP_SGT:9414 case CmpInst::ICMP_SGE:9415 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&9416 isTruePredicate(CmpInst::ICMP_SLE, BRHS, ARHS))9417 return true;9418 return std::nullopt;9419 9420 case CmpInst::ICMP_ULT:9421 case CmpInst::ICMP_ULE:9422 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&9423 isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS))9424 return true;9425 return std::nullopt;9426 9427 case CmpInst::ICMP_UGT:9428 case CmpInst::ICMP_UGE:9429 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&9430 isTruePredicate(CmpInst::ICMP_ULE, BRHS, ARHS))9431 return true;9432 return std::nullopt;9433 }9434}9435 9436/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.9437/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.9438/// Otherwise, return std::nullopt if we can't infer anything.9439static std::optional<bool>9440isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR,9441 CmpPredicate RPred, const ConstantRange &RCR) {9442 auto CRImpliesPred = [&](ConstantRange CR,9443 CmpInst::Predicate Pred) -> std::optional<bool> {9444 // If all true values for lhs and true for rhs, lhs implies rhs9445 if (CR.icmp(Pred, RCR))9446 return true;9447 9448 // If there is no overlap, lhs implies not rhs9449 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))9450 return false;9451 9452 return std::nullopt;9453 };9454 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),9455 RPred))9456 return Res;9457 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {9458 LPred = LPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(LPred)9459 : LPred.dropSameSign();9460 RPred = RPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(RPred)9461 : RPred.dropSameSign();9462 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),9463 RPred);9464 }9465 return std::nullopt;9466}9467 9468/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")9469/// is true. Return false if LHS implies RHS is false. Otherwise, return9470/// std::nullopt if we can't infer anything.9471static std::optional<bool>9472isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,9473 CmpPredicate RPred, const Value *R0, const Value *R1,9474 const DataLayout &DL, bool LHSIsTrue) {9475 // The rest of the logic assumes the LHS condition is true. If that's not the9476 // case, invert the predicate to make it so.9477 if (!LHSIsTrue)9478 LPred = ICmpInst::getInverseCmpPredicate(LPred);9479 9480 // We can have non-canonical operands, so try to normalize any common operand9481 // to L0/R0.9482 if (L0 == R1) {9483 std::swap(R0, R1);9484 RPred = ICmpInst::getSwappedCmpPredicate(RPred);9485 }9486 if (R0 == L1) {9487 std::swap(L0, L1);9488 LPred = ICmpInst::getSwappedCmpPredicate(LPred);9489 }9490 if (L1 == R1) {9491 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.9492 if (L0 != R0 || match(L0, m_ImmConstant())) {9493 std::swap(L0, L1);9494 LPred = ICmpInst::getSwappedCmpPredicate(LPred);9495 std::swap(R0, R1);9496 RPred = ICmpInst::getSwappedCmpPredicate(RPred);9497 }9498 }9499 9500 // See if we can infer anything if operand-0 matches and we have at least one9501 // constant.9502 const APInt *Unused;9503 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {9504 // Potential TODO: We could also further use the constant range of L0/R0 to9505 // further constraint the constant ranges. At the moment this leads to9506 // several regressions related to not transforming `multi_use(A + C0) eq/ne9507 // C1` (see discussion: D58633).9508 ConstantRange LCR = computeConstantRange(9509 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,9510 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);9511 ConstantRange RCR = computeConstantRange(9512 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,9513 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);9514 // Even if L1/R1 are not both constant, we can still sometimes deduce9515 // relationship from a single constant. For example X u> Y implies X != 0.9516 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))9517 return R;9518 // If both L1/R1 were exact constant ranges and we didn't get anything9519 // here, we won't be able to deduce this.9520 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))9521 return std::nullopt;9522 }9523 9524 // Can we infer anything when the two compares have matching operands?9525 if (L0 == R0 && L1 == R1)9526 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);9527 9528 // It only really makes sense in the context of signed comparison for "X - Y9529 // must be positive if X >= Y and no overflow".9530 // Take SGT as an example: L0:x > L1:y and C >= 09531 // ==> R0:(x -nsw y) < R1:(-C) is false9532 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();9533 if ((SignedLPred == ICmpInst::ICMP_SGT ||9534 SignedLPred == ICmpInst::ICMP_SGE) &&9535 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {9536 if (match(R1, m_NonPositive()) &&9537 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)9538 return false;9539 }9540 9541 // Take SLT as an example: L0:x < L1:y and C <= 09542 // ==> R0:(x -nsw y) < R1:(-C) is true9543 if ((SignedLPred == ICmpInst::ICMP_SLT ||9544 SignedLPred == ICmpInst::ICMP_SLE) &&9545 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {9546 if (match(R1, m_NonNegative()) &&9547 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)9548 return true;9549 }9550 9551 // a - b == NonZero -> a != b9552 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b9553 const APInt *L1C;9554 Value *A, *B;9555 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&9556 match(L1, m_APInt(L1C)) && !L1C->isZero() &&9557 match(L0, m_Sub(m_Value(A), m_Value(B))) &&9558 ((A == R0 && B == R1) || (A == R1 && B == R0) ||9559 (match(A, m_PtrToInt(m_Specific(R0))) &&9560 match(B, m_PtrToInt(m_Specific(R1)))) ||9561 (match(A, m_PtrToInt(m_Specific(R1))) &&9562 match(B, m_PtrToInt(m_Specific(R0)))))) {9563 return RPred.dropSameSign() == ICmpInst::ICMP_NE;9564 }9565 9566 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R19567 if (L0 == R0 &&9568 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&9569 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&9570 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))9571 return CmpPredicate::getMatching(LPred, RPred).has_value();9572 9573 if (auto P = CmpPredicate::getMatching(LPred, RPred))9574 return isImpliedCondOperands(*P, L0, L1, R0, R1);9575 9576 return std::nullopt;9577}9578 9579/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")9580/// is true. Return false if LHS implies RHS is false. Otherwise, return9581/// std::nullopt if we can't infer anything.9582static std::optional<bool>9583isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1,9584 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,9585 const DataLayout &DL, bool LHSIsTrue) {9586 // The rest of the logic assumes the LHS condition is true. If that's not the9587 // case, invert the predicate to make it so.9588 if (!LHSIsTrue)9589 LPred = FCmpInst::getInversePredicate(LPred);9590 9591 // We can have non-canonical operands, so try to normalize any common operand9592 // to L0/R0.9593 if (L0 == R1) {9594 std::swap(R0, R1);9595 RPred = FCmpInst::getSwappedPredicate(RPred);9596 }9597 if (R0 == L1) {9598 std::swap(L0, L1);9599 LPred = FCmpInst::getSwappedPredicate(LPred);9600 }9601 if (L1 == R1) {9602 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.9603 if (L0 != R0 || match(L0, m_ImmConstant())) {9604 std::swap(L0, L1);9605 LPred = ICmpInst::getSwappedCmpPredicate(LPred);9606 std::swap(R0, R1);9607 RPred = ICmpInst::getSwappedCmpPredicate(RPred);9608 }9609 }9610 9611 // Can we infer anything when the two compares have matching operands?9612 if (L0 == R0 && L1 == R1) {9613 if ((LPred & RPred) == LPred)9614 return true;9615 if ((LPred & ~RPred) == LPred)9616 return false;9617 }9618 9619 // See if we can infer anything if operand-0 matches and we have at least one9620 // constant.9621 const APFloat *L1C, *R1C;9622 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {9623 if (std::optional<ConstantFPRange> DomCR =9624 ConstantFPRange::makeExactFCmpRegion(LPred, *L1C)) {9625 if (std::optional<ConstantFPRange> ImpliedCR =9626 ConstantFPRange::makeExactFCmpRegion(RPred, *R1C)) {9627 if (ImpliedCR->contains(*DomCR))9628 return true;9629 }9630 if (std::optional<ConstantFPRange> ImpliedCR =9631 ConstantFPRange::makeExactFCmpRegion(9632 FCmpInst::getInversePredicate(RPred), *R1C)) {9633 if (ImpliedCR->contains(*DomCR))9634 return false;9635 }9636 }9637 }9638 9639 return std::nullopt;9640}9641 9642/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is9643/// false. Otherwise, return std::nullopt if we can't infer anything. We9644/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'9645/// instruction.9646static std::optional<bool>9647isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred,9648 const Value *RHSOp0, const Value *RHSOp1,9649 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {9650 // The LHS must be an 'or', 'and', or a 'select' instruction.9651 assert((LHS->getOpcode() == Instruction::And ||9652 LHS->getOpcode() == Instruction::Or ||9653 LHS->getOpcode() == Instruction::Select) &&9654 "Expected LHS to be 'and', 'or', or 'select'.");9655 9656 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");9657 9658 // If the result of an 'or' is false, then we know both legs of the 'or' are9659 // false. Similarly, if the result of an 'and' is true, then we know both9660 // legs of the 'and' are true.9661 const Value *ALHS, *ARHS;9662 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||9663 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {9664 // FIXME: Make this non-recursion.9665 if (std::optional<bool> Implication = isImpliedCondition(9666 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))9667 return Implication;9668 if (std::optional<bool> Implication = isImpliedCondition(9669 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))9670 return Implication;9671 return std::nullopt;9672 }9673 return std::nullopt;9674}9675 9676std::optional<bool>9677llvm::isImpliedCondition(const Value *LHS, CmpPredicate RHSPred,9678 const Value *RHSOp0, const Value *RHSOp1,9679 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {9680 // Bail out when we hit the limit.9681 if (Depth == MaxAnalysisRecursionDepth)9682 return std::nullopt;9683 9684 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for9685 // example.9686 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())9687 return std::nullopt;9688 9689 assert(LHS->getType()->isIntOrIntVectorTy(1) &&9690 "Expected integer type only!");9691 9692 // Match not9693 if (match(LHS, m_Not(m_Value(LHS))))9694 LHSIsTrue = !LHSIsTrue;9695 9696 // Both LHS and RHS are icmps.9697 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {9698 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))9699 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),9700 LHSCmp->getOperand(0), LHSCmp->getOperand(1),9701 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);9702 const Value *V;9703 if (match(LHS, m_NUWTrunc(m_Value(V))))9704 return isImpliedCondICmps(CmpInst::ICMP_NE, V,9705 ConstantInt::get(V->getType(), 0), RHSPred,9706 RHSOp0, RHSOp1, DL, LHSIsTrue);9707 } else {9708 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&9709 "Expected floating point type only!");9710 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))9711 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),9712 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,9713 DL, LHSIsTrue);9714 }9715 9716 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect9717 /// the RHS to be an icmp.9718 /// FIXME: Add support for and/or/select on the RHS.9719 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {9720 if ((LHSI->getOpcode() == Instruction::And ||9721 LHSI->getOpcode() == Instruction::Or ||9722 LHSI->getOpcode() == Instruction::Select))9723 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,9724 Depth);9725 }9726 return std::nullopt;9727}9728 9729std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,9730 const DataLayout &DL,9731 bool LHSIsTrue, unsigned Depth) {9732 // LHS ==> RHS by definition9733 if (LHS == RHS)9734 return LHSIsTrue;9735 9736 // Match not9737 bool InvertRHS = false;9738 if (match(RHS, m_Not(m_Value(RHS)))) {9739 if (LHS == RHS)9740 return !LHSIsTrue;9741 InvertRHS = true;9742 }9743 9744 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {9745 if (auto Implied = isImpliedCondition(9746 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),9747 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))9748 return InvertRHS ? !*Implied : *Implied;9749 return std::nullopt;9750 }9751 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {9752 if (auto Implied = isImpliedCondition(9753 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),9754 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))9755 return InvertRHS ? !*Implied : *Implied;9756 return std::nullopt;9757 }9758 9759 const Value *V;9760 if (match(RHS, m_NUWTrunc(m_Value(V)))) {9761 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,9762 ConstantInt::get(V->getType(), 0), DL,9763 LHSIsTrue, Depth))9764 return InvertRHS ? !*Implied : *Implied;9765 return std::nullopt;9766 }9767 9768 if (Depth == MaxAnalysisRecursionDepth)9769 return std::nullopt;9770 9771 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS29772 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS29773 const Value *RHS1, *RHS2;9774 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {9775 if (std::optional<bool> Imp =9776 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))9777 if (*Imp == true)9778 return !InvertRHS;9779 if (std::optional<bool> Imp =9780 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))9781 if (*Imp == true)9782 return !InvertRHS;9783 }9784 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {9785 if (std::optional<bool> Imp =9786 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))9787 if (*Imp == false)9788 return InvertRHS;9789 if (std::optional<bool> Imp =9790 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))9791 if (*Imp == false)9792 return InvertRHS;9793 }9794 9795 return std::nullopt;9796}9797 9798// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch9799// condition dominating ContextI or nullptr, if no condition is found.9800static std::pair<Value *, bool>9801getDomPredecessorCondition(const Instruction *ContextI) {9802 if (!ContextI || !ContextI->getParent())9803 return {nullptr, false};9804 9805 // TODO: This is a poor/cheap way to determine dominance. Should we use a9806 // dominator tree (eg, from a SimplifyQuery) instead?9807 const BasicBlock *ContextBB = ContextI->getParent();9808 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();9809 if (!PredBB)9810 return {nullptr, false};9811 9812 // We need a conditional branch in the predecessor.9813 Value *PredCond;9814 BasicBlock *TrueBB, *FalseBB;9815 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))9816 return {nullptr, false};9817 9818 // The branch should get simplified. Don't bother simplifying this condition.9819 if (TrueBB == FalseBB)9820 return {nullptr, false};9821 9822 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&9823 "Predecessor block does not point to successor?");9824 9825 // Is this condition implied by the predecessor condition?9826 return {PredCond, TrueBB == ContextBB};9827}9828 9829std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,9830 const Instruction *ContextI,9831 const DataLayout &DL) {9832 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");9833 auto PredCond = getDomPredecessorCondition(ContextI);9834 if (PredCond.first)9835 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);9836 return std::nullopt;9837}9838 9839std::optional<bool> llvm::isImpliedByDomCondition(CmpPredicate Pred,9840 const Value *LHS,9841 const Value *RHS,9842 const Instruction *ContextI,9843 const DataLayout &DL) {9844 auto PredCond = getDomPredecessorCondition(ContextI);9845 if (PredCond.first)9846 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,9847 PredCond.second);9848 return std::nullopt;9849}9850 9851static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,9852 APInt &Upper, const InstrInfoQuery &IIQ,9853 bool PreferSignedRange) {9854 unsigned Width = Lower.getBitWidth();9855 const APInt *C;9856 switch (BO.getOpcode()) {9857 case Instruction::Sub:9858 if (match(BO.getOperand(0), m_APInt(C))) {9859 bool HasNSW = IIQ.hasNoSignedWrap(&BO);9860 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);9861 9862 // If the caller expects a signed compare, then try to use a signed range.9863 // Otherwise if both no-wraps are set, use the unsigned range because it9864 // is never larger than the signed range. Example:9865 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].9866 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].9867 if (PreferSignedRange && HasNSW && HasNUW)9868 HasNUW = false;9869 9870 if (HasNUW) {9871 // 'sub nuw c, x' produces [0, C].9872 Upper = *C + 1;9873 } else if (HasNSW) {9874 if (C->isNegative()) {9875 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].9876 Lower = APInt::getSignedMinValue(Width);9877 Upper = *C - APInt::getSignedMaxValue(Width);9878 } else {9879 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap9880 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].9881 Lower = *C - APInt::getSignedMaxValue(Width);9882 Upper = APInt::getSignedMinValue(Width);9883 }9884 }9885 }9886 break;9887 case Instruction::Add:9888 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {9889 bool HasNSW = IIQ.hasNoSignedWrap(&BO);9890 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);9891 9892 // If the caller expects a signed compare, then try to use a signed9893 // range. Otherwise if both no-wraps are set, use the unsigned range9894 // because it is never larger than the signed range. Example: "add nuw9895 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].9896 if (PreferSignedRange && HasNSW && HasNUW)9897 HasNUW = false;9898 9899 if (HasNUW) {9900 // 'add nuw x, C' produces [C, UINT_MAX].9901 Lower = *C;9902 } else if (HasNSW) {9903 if (C->isNegative()) {9904 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].9905 Lower = APInt::getSignedMinValue(Width);9906 Upper = APInt::getSignedMaxValue(Width) + *C + 1;9907 } else {9908 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].9909 Lower = APInt::getSignedMinValue(Width) + *C;9910 Upper = APInt::getSignedMaxValue(Width) + 1;9911 }9912 }9913 }9914 break;9915 9916 case Instruction::And:9917 if (match(BO.getOperand(1), m_APInt(C)))9918 // 'and x, C' produces [0, C].9919 Upper = *C + 1;9920 // X & -X is a power of two or zero. So we can cap the value at max power of9921 // two.9922 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||9923 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))9924 Upper = APInt::getSignedMinValue(Width) + 1;9925 break;9926 9927 case Instruction::Or:9928 if (match(BO.getOperand(1), m_APInt(C)))9929 // 'or x, C' produces [C, UINT_MAX].9930 Lower = *C;9931 break;9932 9933 case Instruction::AShr:9934 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {9935 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].9936 Lower = APInt::getSignedMinValue(Width).ashr(*C);9937 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;9938 } else if (match(BO.getOperand(0), m_APInt(C))) {9939 unsigned ShiftAmount = Width - 1;9940 if (!C->isZero() && IIQ.isExact(&BO))9941 ShiftAmount = C->countr_zero();9942 if (C->isNegative()) {9943 // 'ashr C, x' produces [C, C >> (Width-1)]9944 Lower = *C;9945 Upper = C->ashr(ShiftAmount) + 1;9946 } else {9947 // 'ashr C, x' produces [C >> (Width-1), C]9948 Lower = C->ashr(ShiftAmount);9949 Upper = *C + 1;9950 }9951 }9952 break;9953 9954 case Instruction::LShr:9955 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {9956 // 'lshr x, C' produces [0, UINT_MAX >> C].9957 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;9958 } else if (match(BO.getOperand(0), m_APInt(C))) {9959 // 'lshr C, x' produces [C >> (Width-1), C].9960 unsigned ShiftAmount = Width - 1;9961 if (!C->isZero() && IIQ.isExact(&BO))9962 ShiftAmount = C->countr_zero();9963 Lower = C->lshr(ShiftAmount);9964 Upper = *C + 1;9965 }9966 break;9967 9968 case Instruction::Shl:9969 if (match(BO.getOperand(0), m_APInt(C))) {9970 if (IIQ.hasNoUnsignedWrap(&BO)) {9971 // 'shl nuw C, x' produces [C, C << CLZ(C)]9972 Lower = *C;9973 Upper = Lower.shl(Lower.countl_zero()) + 1;9974 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?9975 if (C->isNegative()) {9976 // 'shl nsw C, x' produces [C << CLO(C)-1, C]9977 unsigned ShiftAmount = C->countl_one() - 1;9978 Lower = C->shl(ShiftAmount);9979 Upper = *C + 1;9980 } else {9981 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]9982 unsigned ShiftAmount = C->countl_zero() - 1;9983 Lower = *C;9984 Upper = C->shl(ShiftAmount) + 1;9985 }9986 } else {9987 // If lowbit is set, value can never be zero.9988 if ((*C)[0])9989 Lower = APInt::getOneBitSet(Width, 0);9990 // If we are shifting a constant the largest it can be is if the longest9991 // sequence of consecutive ones is shifted to the highbits (breaking9992 // ties for which sequence is higher). At the moment we take a liberal9993 // upper bound on this by just popcounting the constant.9994 // TODO: There may be a bitwise trick for it longest/highest9995 // consecutative sequence of ones (naive method is O(Width) loop).9996 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;9997 }9998 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {9999 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;10000 }10001 break;10002 10003 case Instruction::SDiv:10004 if (match(BO.getOperand(1), m_APInt(C))) {10005 APInt IntMin = APInt::getSignedMinValue(Width);10006 APInt IntMax = APInt::getSignedMaxValue(Width);10007 if (C->isAllOnes()) {10008 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]10009 // where C != -1 and C != 0 and C != 110010 Lower = IntMin + 1;10011 Upper = IntMax + 1;10012 } else if (C->countl_zero() < Width - 1) {10013 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]10014 // where C != -1 and C != 0 and C != 110015 Lower = IntMin.sdiv(*C);10016 Upper = IntMax.sdiv(*C);10017 if (Lower.sgt(Upper))10018 std::swap(Lower, Upper);10019 Upper = Upper + 1;10020 assert(Upper != Lower && "Upper part of range has wrapped!");10021 }10022 } else if (match(BO.getOperand(0), m_APInt(C))) {10023 if (C->isMinSignedValue()) {10024 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].10025 Lower = *C;10026 Upper = Lower.lshr(1) + 1;10027 } else {10028 // 'sdiv C, x' produces [-|C|, |C|].10029 Upper = C->abs() + 1;10030 Lower = (-Upper) + 1;10031 }10032 }10033 break;10034 10035 case Instruction::UDiv:10036 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {10037 // 'udiv x, C' produces [0, UINT_MAX / C].10038 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;10039 } else if (match(BO.getOperand(0), m_APInt(C))) {10040 // 'udiv C, x' produces [0, C].10041 Upper = *C + 1;10042 }10043 break;10044 10045 case Instruction::SRem:10046 if (match(BO.getOperand(1), m_APInt(C))) {10047 // 'srem x, C' produces (-|C|, |C|).10048 Upper = C->abs();10049 Lower = (-Upper) + 1;10050 } else if (match(BO.getOperand(0), m_APInt(C))) {10051 if (C->isNegative()) {10052 // 'srem -|C|, x' produces [-|C|, 0].10053 Upper = 1;10054 Lower = *C;10055 } else {10056 // 'srem |C|, x' produces [0, |C|].10057 Upper = *C + 1;10058 }10059 }10060 break;10061 10062 case Instruction::URem:10063 if (match(BO.getOperand(1), m_APInt(C)))10064 // 'urem x, C' produces [0, C).10065 Upper = *C;10066 else if (match(BO.getOperand(0), m_APInt(C)))10067 // 'urem C, x' produces [0, C].10068 Upper = *C + 1;10069 break;10070 10071 default:10072 break;10073 }10074}10075 10076static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II,10077 bool UseInstrInfo) {10078 unsigned Width = II.getType()->getScalarSizeInBits();10079 const APInt *C;10080 switch (II.getIntrinsicID()) {10081 case Intrinsic::ctlz:10082 case Intrinsic::cttz: {10083 APInt Upper(Width, Width);10084 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))10085 Upper += 1;10086 // Maximum of set/clear bits is the bit width.10087 return ConstantRange::getNonEmpty(APInt::getZero(Width), Upper);10088 }10089 case Intrinsic::ctpop:10090 // Maximum of set/clear bits is the bit width.10091 return ConstantRange::getNonEmpty(APInt::getZero(Width),10092 APInt(Width, Width) + 1);10093 case Intrinsic::uadd_sat:10094 // uadd.sat(x, C) produces [C, UINT_MAX].10095 if (match(II.getOperand(0), m_APInt(C)) ||10096 match(II.getOperand(1), m_APInt(C)))10097 return ConstantRange::getNonEmpty(*C, APInt::getZero(Width));10098 break;10099 case Intrinsic::sadd_sat:10100 if (match(II.getOperand(0), m_APInt(C)) ||10101 match(II.getOperand(1), m_APInt(C))) {10102 if (C->isNegative())10103 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].10104 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),10105 APInt::getSignedMaxValue(Width) + *C +10106 1);10107 10108 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].10109 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) + *C,10110 APInt::getSignedMaxValue(Width) + 1);10111 }10112 break;10113 case Intrinsic::usub_sat:10114 // usub.sat(C, x) produces [0, C].10115 if (match(II.getOperand(0), m_APInt(C)))10116 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);10117 10118 // usub.sat(x, C) produces [0, UINT_MAX - C].10119 if (match(II.getOperand(1), m_APInt(C)))10120 return ConstantRange::getNonEmpty(APInt::getZero(Width),10121 APInt::getMaxValue(Width) - *C + 1);10122 break;10123 case Intrinsic::ssub_sat:10124 if (match(II.getOperand(0), m_APInt(C))) {10125 if (C->isNegative())10126 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].10127 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),10128 *C - APInt::getSignedMinValue(Width) +10129 1);10130 10131 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].10132 return ConstantRange::getNonEmpty(*C - APInt::getSignedMaxValue(Width),10133 APInt::getSignedMaxValue(Width) + 1);10134 } else if (match(II.getOperand(1), m_APInt(C))) {10135 if (C->isNegative())10136 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:10137 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) - *C,10138 APInt::getSignedMaxValue(Width) + 1);10139 10140 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].10141 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),10142 APInt::getSignedMaxValue(Width) - *C +10143 1);10144 }10145 break;10146 case Intrinsic::umin:10147 case Intrinsic::umax:10148 case Intrinsic::smin:10149 case Intrinsic::smax:10150 if (!match(II.getOperand(0), m_APInt(C)) &&10151 !match(II.getOperand(1), m_APInt(C)))10152 break;10153 10154 switch (II.getIntrinsicID()) {10155 case Intrinsic::umin:10156 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);10157 case Intrinsic::umax:10158 return ConstantRange::getNonEmpty(*C, APInt::getZero(Width));10159 case Intrinsic::smin:10160 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),10161 *C + 1);10162 case Intrinsic::smax:10163 return ConstantRange::getNonEmpty(*C,10164 APInt::getSignedMaxValue(Width) + 1);10165 default:10166 llvm_unreachable("Must be min/max intrinsic");10167 }10168 break;10169 case Intrinsic::abs:10170 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],10171 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.10172 if (match(II.getOperand(1), m_One()))10173 return ConstantRange::getNonEmpty(APInt::getZero(Width),10174 APInt::getSignedMaxValue(Width) + 1);10175 10176 return ConstantRange::getNonEmpty(APInt::getZero(Width),10177 APInt::getSignedMinValue(Width) + 1);10178 case Intrinsic::vscale:10179 if (!II.getParent() || !II.getFunction())10180 break;10181 return getVScaleRange(II.getFunction(), Width);10182 default:10183 break;10184 }10185 10186 return ConstantRange::getFull(Width);10187}10188 10189static ConstantRange getRangeForSelectPattern(const SelectInst &SI,10190 const InstrInfoQuery &IIQ) {10191 unsigned BitWidth = SI.getType()->getScalarSizeInBits();10192 const Value *LHS = nullptr, *RHS = nullptr;10193 SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);10194 if (R.Flavor == SPF_UNKNOWN)10195 return ConstantRange::getFull(BitWidth);10196 10197 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {10198 // If the negation part of the abs (in RHS) has the NSW flag,10199 // then the result of abs(X) is [0..SIGNED_MAX],10200 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.10201 if (match(RHS, m_Neg(m_Specific(LHS))) &&10202 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))10203 return ConstantRange::getNonEmpty(APInt::getZero(BitWidth),10204 APInt::getSignedMaxValue(BitWidth) + 1);10205 10206 return ConstantRange::getNonEmpty(APInt::getZero(BitWidth),10207 APInt::getSignedMinValue(BitWidth) + 1);10208 }10209 10210 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {10211 // The result of -abs(X) is <= 0.10212 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),10213 APInt(BitWidth, 1));10214 }10215 10216 const APInt *C;10217 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))10218 return ConstantRange::getFull(BitWidth);10219 10220 switch (R.Flavor) {10221 case SPF_UMIN:10222 return ConstantRange::getNonEmpty(APInt::getZero(BitWidth), *C + 1);10223 case SPF_UMAX:10224 return ConstantRange::getNonEmpty(*C, APInt::getZero(BitWidth));10225 case SPF_SMIN:10226 return ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),10227 *C + 1);10228 case SPF_SMAX:10229 return ConstantRange::getNonEmpty(*C,10230 APInt::getSignedMaxValue(BitWidth) + 1);10231 default:10232 return ConstantRange::getFull(BitWidth);10233 }10234}10235 10236static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {10237 // The maximum representable value of a half is 65504. For floats the maximum10238 // value is 3.4e38 which requires roughly 129 bits.10239 unsigned BitWidth = I->getType()->getScalarSizeInBits();10240 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())10241 return;10242 if (isa<FPToSIInst>(I) && BitWidth >= 17) {10243 Lower = APInt(BitWidth, -65504, true);10244 Upper = APInt(BitWidth, 65505);10245 }10246 10247 if (isa<FPToUIInst>(I) && BitWidth >= 16) {10248 // For a fptoui the lower limit is left as 0.10249 Upper = APInt(BitWidth, 65505);10250 }10251}10252 10253ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,10254 bool UseInstrInfo, AssumptionCache *AC,10255 const Instruction *CtxI,10256 const DominatorTree *DT,10257 unsigned Depth) {10258 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");10259 10260 if (Depth == MaxAnalysisRecursionDepth)10261 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());10262 10263 if (auto *C = dyn_cast<Constant>(V))10264 return C->toConstantRange();10265 10266 unsigned BitWidth = V->getType()->getScalarSizeInBits();10267 InstrInfoQuery IIQ(UseInstrInfo);10268 ConstantRange CR = ConstantRange::getFull(BitWidth);10269 if (auto *BO = dyn_cast<BinaryOperator>(V)) {10270 APInt Lower = APInt(BitWidth, 0);10271 APInt Upper = APInt(BitWidth, 0);10272 // TODO: Return ConstantRange.10273 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);10274 CR = ConstantRange::getNonEmpty(Lower, Upper);10275 } else if (auto *II = dyn_cast<IntrinsicInst>(V))10276 CR = getRangeForIntrinsic(*II, UseInstrInfo);10277 else if (auto *SI = dyn_cast<SelectInst>(V)) {10278 ConstantRange CRTrue = computeConstantRange(10279 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);10280 ConstantRange CRFalse = computeConstantRange(10281 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);10282 CR = CRTrue.unionWith(CRFalse);10283 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));10284 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {10285 APInt Lower = APInt(BitWidth, 0);10286 APInt Upper = APInt(BitWidth, 0);10287 // TODO: Return ConstantRange.10288 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);10289 CR = ConstantRange::getNonEmpty(Lower, Upper);10290 } else if (const auto *A = dyn_cast<Argument>(V))10291 if (std::optional<ConstantRange> Range = A->getRange())10292 CR = *Range;10293 10294 if (auto *I = dyn_cast<Instruction>(V)) {10295 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))10296 CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));10297 10298 if (const auto *CB = dyn_cast<CallBase>(V))10299 if (std::optional<ConstantRange> Range = CB->getRange())10300 CR = CR.intersectWith(*Range);10301 }10302 10303 if (CtxI && AC) {10304 // Try to restrict the range based on information from assumptions.10305 for (auto &AssumeVH : AC->assumptionsFor(V)) {10306 if (!AssumeVH)10307 continue;10308 CallInst *I = cast<CallInst>(AssumeVH);10309 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&10310 "Got assumption for the wrong function!");10311 assert(I->getIntrinsicID() == Intrinsic::assume &&10312 "must be an assume intrinsic");10313 10314 if (!isValidAssumeForContext(I, CtxI, DT))10315 continue;10316 Value *Arg = I->getArgOperand(0);10317 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);10318 // Currently we just use information from comparisons.10319 if (!Cmp || Cmp->getOperand(0) != V)10320 continue;10321 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?10322 ConstantRange RHS =10323 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,10324 UseInstrInfo, AC, I, DT, Depth + 1);10325 CR = CR.intersectWith(10326 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));10327 }10328 }10329 10330 return CR;10331}10332 10333static void10334addValueAffectedByCondition(Value *V,10335 function_ref<void(Value *)> InsertAffected) {10336 assert(V != nullptr);10337 if (isa<Argument>(V) || isa<GlobalValue>(V)) {10338 InsertAffected(V);10339 } else if (auto *I = dyn_cast<Instruction>(V)) {10340 InsertAffected(V);10341 10342 // Peek through unary operators to find the source of the condition.10343 Value *Op;10344 if (match(I, m_CombineOr(m_PtrToInt(m_Value(Op)), m_Trunc(m_Value(Op))))) {10345 if (isa<Instruction>(Op) || isa<Argument>(Op))10346 InsertAffected(Op);10347 }10348 }10349}10350 10351void llvm::findValuesAffectedByCondition(10352 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {10353 auto AddAffected = [&InsertAffected](Value *V) {10354 addValueAffectedByCondition(V, InsertAffected);10355 };10356 10357 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {10358 if (IsAssume) {10359 AddAffected(LHS);10360 AddAffected(RHS);10361 } else if (match(RHS, m_Constant()))10362 AddAffected(LHS);10363 };10364 10365 SmallVector<Value *, 8> Worklist;10366 SmallPtrSet<Value *, 8> Visited;10367 Worklist.push_back(Cond);10368 while (!Worklist.empty()) {10369 Value *V = Worklist.pop_back_val();10370 if (!Visited.insert(V).second)10371 continue;10372 10373 CmpPredicate Pred;10374 Value *A, *B, *X;10375 10376 if (IsAssume) {10377 AddAffected(V);10378 if (match(V, m_Not(m_Value(X))))10379 AddAffected(X);10380 }10381 10382 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {10383 // assume(A && B) is split to -> assume(A); assume(B);10384 // assume(!(A || B)) is split to -> assume(!A); assume(!B);10385 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide10386 // enough information to be worth handling (intersection of information as10387 // opposed to union).10388 if (!IsAssume) {10389 Worklist.push_back(A);10390 Worklist.push_back(B);10391 }10392 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {10393 bool HasRHSC = match(B, m_ConstantInt());10394 if (ICmpInst::isEquality(Pred)) {10395 AddAffected(A);10396 if (IsAssume)10397 AddAffected(B);10398 if (HasRHSC) {10399 Value *Y;10400 // (X << C) or (X >>_s C) or (X >>_u C).10401 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))10402 AddAffected(X);10403 // (X & C) or (X | C).10404 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||10405 match(A, m_Or(m_Value(X), m_Value(Y)))) {10406 AddAffected(X);10407 AddAffected(Y);10408 }10409 // X - Y10410 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {10411 AddAffected(X);10412 AddAffected(Y);10413 }10414 }10415 } else {10416 AddCmpOperands(A, B);10417 if (HasRHSC) {10418 // Handle (A + C1) u< C2, which is the canonical form of10419 // A > C3 && A < C4.10420 if (match(A, m_AddLike(m_Value(X), m_ConstantInt())))10421 AddAffected(X);10422 10423 if (ICmpInst::isUnsigned(Pred)) {10424 Value *Y;10425 // X & Y u> C -> X >u C && Y >u C10426 // X | Y u< C -> X u< C && Y u< C10427 // X nuw+ Y u< C -> X u< C && Y u< C10428 if (match(A, m_And(m_Value(X), m_Value(Y))) ||10429 match(A, m_Or(m_Value(X), m_Value(Y))) ||10430 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {10431 AddAffected(X);10432 AddAffected(Y);10433 }10434 // X nuw- Y u> C -> X u> C10435 if (match(A, m_NUWSub(m_Value(X), m_Value())))10436 AddAffected(X);10437 }10438 }10439 10440 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported10441 // by computeKnownFPClass().10442 if (match(A, m_ElementWiseBitCast(m_Value(X)))) {10443 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))10444 InsertAffected(X);10445 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))10446 InsertAffected(X);10447 }10448 }10449 10450 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))10451 AddAffected(X);10452 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {10453 AddCmpOperands(A, B);10454 10455 // fcmp fneg(x), y10456 // fcmp fabs(x), y10457 // fcmp fneg(fabs(x)), y10458 if (match(A, m_FNeg(m_Value(A))))10459 AddAffected(A);10460 if (match(A, m_FAbs(m_Value(A))))10461 AddAffected(A);10462 10463 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),10464 m_Value()))) {10465 // Handle patterns that computeKnownFPClass() support.10466 AddAffected(A);10467 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {10468 // Assume is checked here as X is already added above for assumes in10469 // addValueAffectedByCondition10470 AddAffected(X);10471 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {10472 // Assume is checked here to avoid issues with ephemeral values10473 Worklist.push_back(X);10474 }10475 }10476}10477 10478const Value *llvm::stripNullTest(const Value *V) {10479 // (X >> C) or/add (X & mask(C) != 0)10480 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {10481 if (BO->getOpcode() == Instruction::Add ||10482 BO->getOpcode() == Instruction::Or) {10483 const Value *X;10484 const APInt *C1, *C2;10485 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),10486 m_ZExt(m_SpecificICmp(10487 ICmpInst::ICMP_NE,10488 m_And(m_Deferred(X), m_LowBitMask(C2)),10489 m_Zero())))) &&10490 C2->popcount() == C1->getZExtValue())10491 return X;10492 }10493 }10494 return nullptr;10495}10496 10497Value *llvm::stripNullTest(Value *V) {10498 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));10499}10500 10501bool llvm::collectPossibleValues(const Value *V,10502 SmallPtrSetImpl<const Constant *> &Constants,10503 unsigned MaxCount, bool AllowUndefOrPoison) {10504 SmallPtrSet<const Instruction *, 8> Visited;10505 SmallVector<const Instruction *, 8> Worklist;10506 auto Push = [&](const Value *V) -> bool {10507 Constant *C;10508 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {10509 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))10510 return false;10511 // Check existence first to avoid unnecessary allocations.10512 if (Constants.contains(C))10513 return true;10514 if (Constants.size() == MaxCount)10515 return false;10516 Constants.insert(C);10517 return true;10518 }10519 10520 if (auto *Inst = dyn_cast<Instruction>(V)) {10521 if (Visited.insert(Inst).second)10522 Worklist.push_back(Inst);10523 return true;10524 }10525 return false;10526 };10527 if (!Push(V))10528 return false;10529 while (!Worklist.empty()) {10530 const Instruction *CurInst = Worklist.pop_back_val();10531 switch (CurInst->getOpcode()) {10532 case Instruction::Select:10533 if (!Push(CurInst->getOperand(1)))10534 return false;10535 if (!Push(CurInst->getOperand(2)))10536 return false;10537 break;10538 case Instruction::PHI:10539 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {10540 // Fast path for recurrence PHI.10541 if (IncomingValue == CurInst)10542 continue;10543 if (!Push(IncomingValue))10544 return false;10545 }10546 break;10547 default:10548 return false;10549 }10550 }10551 return true;10552}10553