1//===- ValueTracking.cpp - Walk computations to compute properties --------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This file contains routines that help analyze properties that chains of 12//===----------------------------------------------------------------------===// 57#include "llvm/IR/IntrinsicsAArch64.h" 58#include "llvm/IR/IntrinsicsAMDGPU.h" 59#include "llvm/IR/IntrinsicsRISCV.h" 60#include "llvm/IR/IntrinsicsX86.h" 85// Controls the number of uses of the value searched for possible 86// dominating comparisons. 91/// Returns the bitwidth of the given scalar or pointer type. For vector types, 92/// returns the element type's bitwidth. 97returnDL.getPointerTypeSizeInBits(Ty);
100// Given the provided Value and, potentially, a context instruction, return 101// the preferred context instruction (if any). 103// If we've been provided with a context instruction, then use that (provided 104// it has been inserted). 108// If the value is really an already-inserted instruction, then use that. 109 CxtI = dyn_cast<Instruction>(V);
117// If we've been provided with a context instruction, then use that (provided 118// it has been inserted). 122// If the value is really an already-inserted instruction, then use that. 123 CxtI = dyn_cast<Instruction>(V1);
127 CxtI = dyn_cast<Instruction>(V2);
135constAPInt &DemandedElts,
137if (isa<ScalableVectorType>(Shuf->
getType())) {
139 DemandedLHS = DemandedRHS = DemandedElts;
146 DemandedElts, DemandedLHS, DemandedRHS);
155// Since the number of lanes in a scalable vector is unknown at compile time, 156// we track one bit which is implicitly broadcast to all lanes. This means 157// that all lanes in a scalable vector are considered demanded. 158auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
186 V, DemandedElts,
Depth,
192// Look for an inverted mask: (X & ~M) op (Y & M). 206// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern 215// Peek through extends to find a 'not' of the other side: 222// Look for: (A & B) op ~(A | B) 232// Look for: (X << V) op (Y >> (BitWidth - V)) 233// or (X >> V) op (Y << (BitWidth - V)) 255"LHS and RHS should have the same type");
257"LHS and RHS should be integers");
268return !
I->user_empty() &&
all_of(
I->users(), [](
constUser *U) {
269 return match(U, m_ICmp(m_Value(), m_Zero()));
274return !
I->user_empty() &&
all_of(
I->users(), [](
constUser *U) {
276 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
281bool OrZero,
unsignedDepth,
284 return ::isKnownToBeAPowerOfTwo(
299if (
auto *CI = dyn_cast<ConstantInt>(V))
300return CI->getValue().isStrictlyPositive();
302// If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep 322// We don't support looking through casts. 323if (V1 == V2 || V1->
getType() != V2->getType())
325auto *FVTy = dyn_cast<FixedVectorType>(V1->
getType());
328 return ::isKnownNonEqual(
329 V1, V2, DemandedElts, 0,
337return Mask.isSubsetOf(Known.
Zero);
345auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
355 return ::ComputeNumSignBits(
364return V->getType()->getScalarSizeInBits() - SignBits + 1;
369constAPInt &DemandedElts,
374// If one operand is unknown and we have no nowrap information, 375// the result will be unknown independently of the second operand. 384bool NUW,
constAPInt &DemandedElts,
392// If the multiplication is known not to overflow, compute the sign bit. 395// The product of a number with itself is non-negative. 402// The product of two numbers with the same sign is non-negative. 404 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
406// mul nuw nsw with a factor > 1 is non-negative. 412// The product of a negative number and a non-negative number is either 416 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
418 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.
isNonZero());
422bool SelfMultiply = Op0 == Op1;
428// Only make use of no-wrap flags if we failed to compute the sign bit 429// directly. This matters if the multiplication always overflows, in 430// which case we prefer to follow the result of the direct computation, 431// though as the program is invoking undefined behaviour we can choose 432// whatever we like here. 442unsigned NumRanges = Ranges.getNumOperands() / 2;
448for (
unsigned i = 0; i < NumRanges; ++i) {
450 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
452 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
455// The first CommonPrefixBits of all values in Range are equal. 456unsigned CommonPrefixBits =
460 Known.
One &= UnsignedMax & Mask;
461 Known.
Zero &= ~UnsignedMax & Mask;
470// The instruction defining an assumption's condition itself is always 471// considered ephemeral to that assumption (even if it has other 472// non-ephemeral users). See r246696's test case for an example. 476while (!WorkSet.
empty()) {
478if (!Visited.
insert(V).second)
481// If all uses of this value are ephemeral, then so is this value. 483 return EphValues.count(U);
488if (V ==
I || (isa<Instruction>(V) &&
490 !cast<Instruction>(V)->isTerminator())) {
492if (
constUser *U = dyn_cast<User>(V))
501// Is this an intrinsic that cannot be speculated but also cannot trap? 504return CI->isAssumeLikeIntrinsic();
512bool AllowEphemerals) {
513// There are two restrictions on the use of an assume: 514// 1. The assume must dominate the context (or the control flow must 515// reach the assume whenever it reaches the context). 516// 2. The context must not be in the assume's set of ephemeral values 517// (otherwise we will use the assume to prove that the condition 518// feeding the assume is trivially true, thus causing the removal of 522// If Inv and CtxI are in the same block, check if the assume (Inv) is first 527// Don't let an assume affect itself - this would cause the problems 528// `isEphemeralValueOf` is trying to prevent, and it would also make 529// the loop below go out of bounds. 530if (!AllowEphemerals && Inv == CxtI)
533// The context comes first, but they're both in the same block. 534// Make sure there is nothing in between that might interrupt 535// the control flow, not even CxtI itself. 536// We limit the scan distance between the assume and its context instruction 537// to avoid a compile-time explosion. This limit is chosen arbitrarily, so 538// it can be adjusted if needed (could be turned into a cl::opt). 546// Inv and CxtI are in different blocks. 552// We don't have a DT, but this trivially dominates. 559// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but 560// we still have enough information about `RHS` to conclude non-zero. For 561// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops 562// so the extra compile time may not be worth it, but possibly a second API 563// should be created for use outside of loops. 565// v u> y implies v != 0. 566if (Pred == ICmpInst::ICMP_UGT)
569// Special-case v != 0 to also handle v != null. 570if (Pred == ICmpInst::ICMP_NE)
573// All other predicates - rely on generic ConstantRange handling. 581auto *VC = dyn_cast<ConstantDataVector>(
RHS);
585for (
unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
588 Pred, VC->getElementAsAPInt(ElemIdx));
597constPHINode **PhiOut =
nullptr) {
601 CtxIOut =
PHI->getIncomingBlock(*U)->getTerminator();
605// If the Use is a select of this phi, compute analysis on other arm to break 612// Same for select, if this phi is 2-operand phi, compute analysis on other 613// incoming value to break recursion. 614// TODO: We could handle any number of incoming edges as long as we only have 616if (
auto *IncPhi = dyn_cast<PHINode>(ValOut);
617 IncPhi && IncPhi->getNumIncomingValues() == 2) {
619if (IncPhi->getIncomingValue(
Idx) ==
PHI) {
620 ValOut = IncPhi->getIncomingValue(1 -
Idx);
623 CtxIOut = IncPhi->getIncomingBlock(1 -
Idx)->getTerminator();
631// Use of assumptions is context-sensitive. If we don't have a context, we 642"Got assumption for the wrong function!");
645if (!V->getType()->isPointerTy())
648 *
I,
I->bundle_op_info_begin()[Elem.Index])) {
650 (RK.AttrKind == Attribute::NonNull ||
651 (RK.AttrKind == Attribute::Dereferenceable &&
653 V->getType()->getPointerAddressSpace()))) &&
660// Warning: This loop can end up being somewhat performance sensitive. 661// We're running this loop for once for each value queried resulting in a 662// runtime of ~O(#assumes * #values). 681// Handle comparison of pointer to null explicitly, as it will not be 682// covered by the m_APInt() logic below. 685case ICmpInst::ICMP_EQ:
688case ICmpInst::ICMP_SGE:
689case ICmpInst::ICMP_SGT:
692case ICmpInst::ICMP_SLT:
710case ICmpInst::ICMP_EQ:
714// assume(V & Mask = C) 717// For one bits in Mask, we can propagate bits from C to V. 720 Known.
Zero |= ~*
C & *Mask;
721// assume(V | Mask = C) 723// For zero bits in Mask, we can propagate bits from C to V. 726 Known.
One |= *
C & ~*Mask;
727// assume(V ^ Mask = C) 730// Equivalent to assume(V == Mask ^ C) 732// assume(V << ShAmt = C) 735// For those bits in C that are known, we can propagate them to known 736// bits in V shifted to the right by ShAmt. 741// assume(V >> ShAmt = C) 745// For those bits in RHS that are known, we can propagate them to known 746// bits in V shifted to the right by C. 747 Known.
Zero |= RHSKnown.
Zero << ShAmt;
748 Known.
One |= RHSKnown.
One << ShAmt;
751case ICmpInst::ICMP_NE: {
752// assume (V & B != 0) where B is a power of 2 767if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
768// X & Y u> C -> X u> C && Y u> C 769// X nuw- Y u> C -> X u> C 773 (*
C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
775if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
776// X | Y u< C -> X u< C && Y u< C 777// X nuw+ Y u< C -> X u< C && Y u< C 781 (*
C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
793 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
797// Handle icmp pred (trunc V), C 826if (
auto *Cmp = dyn_cast<ICmpInst>(
Cond))
832// Handle injected condition. 840// Handle dominating conditions. 862// Note that the patterns below need to be kept in sync with the code 863// in AssumptionCache::updateAffectedValues. 871"Got assumption for the wrong function!");
874if (!V->getType()->isPointerTy())
877 *
I,
I->bundle_op_info_begin()[Elem.Index])) {
878// Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might 879// be the producer of the pointer in the bundle. At the moment, align 880// assumptions aren't optimized away. 881if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
889// Warning: This loop can end up being somewhat performance sensitive. 890// We're running this loop for once for each value queried resulting in a 891// runtime of ~O(#assumes * #values). 893Value *Arg =
I->getArgOperand(0);
909// The remaining tests are all recursive, so bail out if we hit the limit. 913ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
923// Conflicting assumption: Undefined behavior will occur on this execution 929/// Compute known bits from a shift operator, including those with a 930/// non-constant shift amount. Known is the output of this function. Known2 is a 931/// pre-allocated temporary with the same bit width as Known and on return 932/// contains the known bit of the shift value source. KF is an 933/// operator-specific function that, given the known-bits and a shift amount, 934/// compute the implied known-bits of the shift operator's result respectively 935/// for that shift amount. The results from calling KF are conservatively 936/// combined for all permitted shift amounts. 943// To limit compile-time impact, only query isKnownNonZero() if we know at 944// least something about the shift amount. 949 Known = KF(Known2, Known, ShAmtNonZero);
962switch (
I->getOpcode()) {
963case Instruction::And:
964 KnownOut = KnownLHS & KnownRHS;
966// and(x, -x) is common idioms that will clear all but lowest set 967// bit. If we have a single known bit in x, we can clear all bits 969// TODO: instcombine often reassociates independent `and` which can hide 970// this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x). 972// -(-x) == x so using whichever (LHS/RHS) gets us a better result. 974 KnownOut = KnownLHS.
blsi();
976 KnownOut = KnownRHS.
blsi();
980 KnownOut = KnownLHS | KnownRHS;
982case Instruction::Xor:
983 KnownOut = KnownLHS ^ KnownRHS;
984// xor(x, x-1) is common idioms that will clear all but lowest set 985// bit. If we have a single known bit in x, we can clear all bits 987// TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C != 988// -1 but for the purpose of demanded bits (xor(x, x-C) & 989// Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern 990// to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1). 993constKnownBits &XBits =
I->getOperand(0) ==
X ? KnownLHS : KnownRHS;
994 KnownOut = XBits.
blsmsk();
1001// and(x, add (x, -1)) is a common idiom that always clears the low bit; 1002// xor/or(x, add (x, -1)) is an idiom that will always set the low bit. 1003// here we handle the more general case of adding any odd number by 1004// matching the form and/xor/or(x, add(x, y)) where y is odd. 1005// TODO: This could be generalized to clearing any bit set in y where the 1006// following bit is known to be unset in y. 1007if (!KnownOut.
Zero[0] && !KnownOut.
One[0] &&
1028APInt DemandedEltsLHS, DemandedEltsRHS;
1030 DemandedElts, DemandedEltsLHS,
1033constauto ComputeForSingleOpFunc =
1035return KnownBitsFunc(
1040if (DemandedEltsRHS.
isZero())
1041return ComputeForSingleOpFunc(
I->getOperand(0), DemandedEltsLHS);
1042if (DemandedEltsLHS.
isZero())
1043return ComputeForSingleOpFunc(
I->getOperand(1), DemandedEltsRHS);
1045return ComputeForSingleOpFunc(
I->getOperand(0), DemandedEltsLHS)
1046 .intersectWith(ComputeForSingleOpFunc(
I->getOperand(1), DemandedEltsRHS));
1049// Public so this can be used in `SimplifyDemandedUseBits`. 1055auto *FVTy = dyn_cast<FixedVectorType>(
I->getType());
1064Attribute Attr =
F->getFnAttribute(Attribute::VScaleRange);
1065// Without vscale_range, we only know that vscale is non-zero. 1070// Minimum is larger than vscale width, result is always poison. 1072return ConstantRange::getEmpty(
BitWidth);
1085// If we have a constant arm, we are done. 1089// See what condition implies about the bits of the select arm. 1092// If we don't get any information from the condition, no reason to 1097// We can have conflict if the condition is dead. I.e if we have 1098// (x | 64) < 32 ? (x | 64) : y 1099// we will have conflict at bit 6 from the condition/the `or`. 1100// In that case just return. Its not particularly important 1101// what we do, as this select is going to be simplified soon. 1106// Finally make sure the information we found is valid. This is relatively 1107// expensive so it's left for the very end. 1111// Finally, we know we get information from the condition and its valid, 1116// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). 1117// Returns the input and lower/upper bounds. 1122"Input should be a Select!");
1132constValue *LHS2 =
nullptr, *RHS2 =
nullptr;
1144return CLow->
sle(*CHigh);
1149constAPInt *&CHigh) {
1150assert((
II->getIntrinsicID() == Intrinsic::smin ||
1151II->getIntrinsicID() == Intrinsic::smax) &&
1152"Must be smin/smax");
1155auto *InnerII = dyn_cast<IntrinsicInst>(
II->getArgOperand(0));
1156if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1161if (
II->getIntrinsicID() == Intrinsic::smin)
1163return CLow->
sle(*CHigh);
1168constAPInt *CLow, *CHigh;
1175constAPInt &DemandedElts,
1181switch (
I->getOpcode()) {
1183case Instruction::Load:
1188case Instruction::And:
1194case Instruction::Or:
1200case Instruction::Xor:
1206case Instruction::Mul: {
1210 DemandedElts, Known, Known2,
Depth, Q);
1213case Instruction::UDiv: {
1220case Instruction::SDiv: {
1227case Instruction::Select: {
1228auto ComputeForArm = [&](
Value *Arm,
bool Invert) {
1234// Only known if known in both the LHS and RHS. 1236 ComputeForArm(
I->getOperand(1),
/*Invert=*/false)
1240case Instruction::FPTrunc:
1241case Instruction::FPExt:
1242case Instruction::FPToUI:
1243case Instruction::FPToSI:
1244case Instruction::SIToFP:
1245case Instruction::UIToFP:
1246break;
// Can't work with floating point. 1247case Instruction::PtrToInt:
1248case Instruction::IntToPtr:
1249// Fall through and handle them the same as zext/trunc. 1251case Instruction::ZExt:
1252case Instruction::Trunc: {
1253Type *SrcTy =
I->getOperand(0)->getType();
1255unsigned SrcBitWidth;
1256// Note that we handle pointer operands here because of inttoptr/ptrtoint 1257// which fall through here. 1263assert(SrcBitWidth &&
"SrcBitWidth can't be zero");
1266if (
auto *Inst = dyn_cast<PossiblyNonNegInst>(
I);
1267 Inst && Inst->hasNonNeg() && !Known.
isNegative())
1272case Instruction::BitCast: {
1273Type *SrcTy =
I->getOperand(0)->getType();
1275// TODO: For now, not handling conversions like: 1276// (bitcast i64 %x to <2 x i32>) 1277 !
I->getType()->isVectorTy()) {
1283// Handle bitcast from floating point to integer. 1285 V->getType()->isFPOrFPVectorTy()) {
1286Type *FPType = V->getType()->getScalarType();
1291// TODO: Treat it as zero/poison if the use of I is unreachable. 1299if (FPClasses &
fcInf)
1311if (Result.SignBit) {
1321// Handle cast from vector integer type to scalar or vector integer. 1322auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1323if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1324 !
I->getType()->isIntOrIntVectorTy() ||
1325 isa<ScalableVectorType>(
I->getType()))
1328// Look through a cast from narrow vector elements to wider type. 1329// Examples: v4i32 -> v2i64, v3i8 -> v24 1330unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1332// Known bits are automatically intersected across demanded elements of a 1333// vector. So for example, if a bit is computed as known zero, it must be 1334// zero across all demanded elements of the vector. 1336// For this bitcast, each demanded element of the output is sub-divided 1337// across a set of smaller vector elements in the source vector. To get 1338// the known bits for an entire element of the output, compute the known 1339// bits for each sub-element sequentially. This is done by shifting the 1340// one-set-bit demanded elements parameter across the sub-elements for 1341// consecutive calls to computeKnownBits. We are using the demanded 1342// elements parameter as a mask operator. 1344// The known bits of each sub-element are then inserted into place 1345// (dependent on endian) to form the full result of known bits. 1347unsigned SubScale =
BitWidth / SubBitWidth;
1349for (
unsigned i = 0; i != NumElts; ++i) {
1351 SubDemandedElts.
setBit(i * SubScale);
1355for (
unsigned i = 0; i != SubScale; ++i) {
1359 Known.
insertBits(KnownSrc, ShiftElt * SubBitWidth);
1364case Instruction::SExt: {
1365// Compute the bits in the result that are not present in the input. 1366unsigned SrcBitWidth =
I->getOperand(0)->getType()->getScalarSizeInBits();
1368 Known = Known.
trunc(SrcBitWidth);
1370// If the sign bit of the input is known set or clear, then we know the 1371// top bits of the result. 1375case Instruction::Shl: {
1384// Trailing zeros of a right-shifted constant never decrease. 1390case Instruction::LShr: {
1391bool Exact = Q.
IIQ.
isExact(cast<BinaryOperator>(
I));
1398// Leading zeros of a left-shifted constant never decrease. 1404case Instruction::AShr: {
1405bool Exact = Q.
IIQ.
isExact(cast<BinaryOperator>(
I));
1414case Instruction::Sub: {
1418 DemandedElts, Known, Known2,
Depth, Q);
1421case Instruction::Add: {
1425 DemandedElts, Known, Known2,
Depth, Q);
1428case Instruction::SRem:
1434case Instruction::URem:
1439case Instruction::Alloca:
1442case Instruction::GetElementPtr: {
1443// Analyze all of the subscripts of this getelementptr instruction 1444// to determine if we can prove known low zero bits. 1446// Accumulate the constant indices in a separate variable 1447// to minimize the number of calls to computeForAddSub. 1451for (
unsigned i = 1, e =
I->getNumOperands(); i != e; ++i, ++GTI) {
1452// TrailZ can only become smaller, short-circuit if we hit zero. 1456Value *Index =
I->getOperand(i);
1458// Handle case when index is zero. 1459Constant *CIndex = dyn_cast<Constant>(Index);
1464// Handle struct member offset arithmetic. 1467"Access to structure field must be known at compile time");
1472unsignedIdx = cast<ConstantInt>(Index)->getZExtValue();
1475 AccConstIndices +=
Offset;
1479// Handle array index arithmetic. 1486unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1492// Multiply by current sizeof type. 1493// &A[i] == A + i * sizeof(*A[i]). 1495// For scalable types the only thing we know about sizeof is 1496// that this is a multiple of the minimum size. 1500APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1501 IndexConst *= ScalingFactor;
1510// If the offsets have a different width from the pointer, according 1511// to the language reference we need to sign-extend or truncate them 1512// to the width of the pointer. 1515// Note that inbounds does *not* guarantee nsw for the addition, as only 1516// the offset is signed, while the base address is unsigned. 1525case Instruction::PHI: {
1528Value *R =
nullptr, *L =
nullptr;
1530// Handle the case of a simple two-predecessor recurrence PHI. 1531// There's a lot more that could theoretically be done here, but 1532// this is sufficient to catch some interesting cases. 1536// If this is a shift recurrence, we know the bits being shifted in. We 1537// can combine that with information about the start value of the 1538// recurrence to conclude facts about the result. If this is a udiv 1539// recurrence, we know that the result can never exceed either the 1540// numerator or the start value, whichever is greater. 1541case Instruction::LShr:
1542case Instruction::AShr:
1543case Instruction::Shl:
1544case Instruction::UDiv:
1549// For a urem recurrence, the result can never exceed the start value. The 1550// phi could either be the numerator or the denominator. 1551case Instruction::URem: {
1552// We have matched a recurrence of the form: 1553// %iv = [R, %entry], [%iv.next, %backedge] 1554// %iv.next = shift_op %iv, L 1556// Recurse with the phi context to avoid concern about whether facts 1557// inferred hold at original context instruction. TODO: It may be 1558// correct to use the original context. IF warranted, explore and 1559// add sufficient tests to cover. 1564case Instruction::Shl:
1565// A shl recurrence will only increase the tailing zeros 1568case Instruction::LShr:
1569case Instruction::UDiv:
1570case Instruction::URem:
1571// lshr, udiv, and urem recurrences will preserve the leading zeros of 1575case Instruction::AShr:
1576// An ashr recurrence will extend the initial sign bit 1584// Check for operations that have the property that if 1585// both their operands have low zero bits, the result 1586// will have low zero bits. 1587case Instruction::Add:
1588case Instruction::Sub:
1589case Instruction::And:
1590case Instruction::Or:
1591case Instruction::Mul: {
1592// Change the context instruction to the "edge" that flows into the 1593// phi. This is important because that is where the value is actually 1594// "evaluated" even though it is used later somewhere else. (see also 1598unsigned OpNum =
P->getOperand(0) == R ? 0 : 1;
1599Instruction *RInst =
P->getIncomingBlock(OpNum)->getTerminator();
1600Instruction *LInst =
P->getIncomingBlock(1 - OpNum)->getTerminator();
1602// Ok, we have a PHI of the form L op= R. Check for low 1607// We need to take the minimum number of known bits 1615auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1620// If initial value of recurrence is nonnegative, and we are adding 1621// a nonnegative number with nsw, the result can only be nonnegative 1622// or poison value regardless of the number of times we execute the 1623// add in phi recurrence. If initial value is negative and we are 1624// adding a negative number with nsw, the result can only be 1625// negative or poison value. Similar arguments apply to sub and mul. 1627// (add non-negative, non-negative) --> non-negative 1628// (add negative, negative) --> negative 1629case Instruction::Add: {
1637// (sub nsw non-negative, negative) --> non-negative 1638// (sub nsw negative, non-negative) --> negative 1639case Instruction::Sub: {
1649// (mul nsw non-negative, non-negative) --> non-negative 1650case Instruction::Mul:
1666// Unreachable blocks may have zero-operand PHI nodes. 1667if (
P->getNumIncomingValues() == 0)
1670// Otherwise take the unions of the known bit sets of the operands, 1671// taking conservative care to avoid excessive recursion. 1673// Skip if every incoming value references to ourself. 1674if (isa_and_nonnull<UndefValue>(
P->hasConstantValue()))
1679for (
constUse &U :
P->operands()) {
1684// Skip direct self references. 1688// Change the context instruction to the "edge" that flows into the 1689// phi. This is important because that is where the value is actually 1690// "evaluated" even though it is used later somewhere else. (see also 1696// Recurse, but cap the recursion to one level, because we don't 1697// want to waste time spinning around in loops. 1698// TODO: See if we can base recursion limiter on number of incoming phi 1699// edges so we don't overly clamp analysis. 1703// See if we can further use a conditional branch into the phi 1704// to help us determine the range of the value. 1709// TODO: Use RHS Value and compute range from its known bits. 1713// Check for cases of duplicate successors. 1716// If we're using the false successor, invert the predicate. 1719// Get the knownbits implied by the incoming phi condition. 1722// We can have conflicts here if we are analyzing deadcode (its 1723// impossible for us reach this BB based the icmp). 1725// No reason to continue analyzing in a known dead region, so 1726// just resetAll and break. This will cause us to also exit the 1731 Known2 = KnownUnion;
1737// If all bits have been ruled out, there's no need to check 1745case Instruction::Call:
1746case Instruction::Invoke: {
1747// If range metadata is attached to this call, set known bits from that, 1748// and then intersect with known bits based on other properties of the 1754constauto *CB = cast<CallBase>(
I);
1756if (std::optional<ConstantRange>
Range = CB->getRange())
1759if (
constValue *RV = CB->getReturnedArgOperand()) {
1760if (RV->getType() ==
I->getType()) {
1763// If the function doesn't return properly for all input values 1764// (e.g. unreachable exits) then there might be conflicts between the 1765// argument value and the range metadata. Simply discard the known bits 1766// in case of conflicts. 1772switch (
II->getIntrinsicID()) {
1775case Intrinsic::abs: {
1777bool IntMinIsPoison =
match(
II->getArgOperand(1),
m_One());
1778 Known = Known2.
abs(IntMinIsPoison);
1781case Intrinsic::bitreverse:
1786case Intrinsic::bswap:
1791case Intrinsic::ctlz: {
1793// If we have a known 1, its position is our upper bound. 1795// If this call is poison for 0 input, the result will be less than 2^n. 1797 PossibleLZ = std::min(PossibleLZ,
BitWidth - 1);
1802case Intrinsic::cttz: {
1804// If we have a known 1, its position is our upper bound. 1806// If this call is poison for 0 input, the result will be less than 2^n. 1808 PossibleTZ = std::min(PossibleTZ,
BitWidth - 1);
1813case Intrinsic::ctpop: {
1815// We can bound the space the count needs. Also, bits known to be zero 1816// can't contribute to the population. 1820// TODO: we could bound KnownOne using the lower bound on the number 1821// of bits which might be set provided by popcnt KnownOne2. 1824case Intrinsic::fshr:
1825case Intrinsic::fshl: {
1830// Normalize to funnel shift left. 1832if (
II->getIntrinsicID() == Intrinsic::fshr)
1845case Intrinsic::uadd_sat:
1850case Intrinsic::usub_sat:
1855case Intrinsic::sadd_sat:
1860case Intrinsic::ssub_sat:
1865// Vec reverse preserves bits from input vec. 1866case Intrinsic::vector_reverse:
1870// for min/max/and/or reduce, any bit common to each element in the 1871// input vec is set in the output. 1872case Intrinsic::vector_reduce_and:
1873case Intrinsic::vector_reduce_or:
1874case Intrinsic::vector_reduce_umax:
1875case Intrinsic::vector_reduce_umin:
1876case Intrinsic::vector_reduce_smax:
1877case Intrinsic::vector_reduce_smin:
1880case Intrinsic::vector_reduce_xor: {
1882// The zeros common to all vecs are zero in the output. 1883// If the number of elements is odd, then the common ones remain. If the 1884// number of elements is even, then the common ones becomes zeros. 1885auto *VecTy = cast<VectorType>(
I->getOperand(0)->getType());
1886// Even, so the ones become zeros. 1887bool EvenCnt = VecTy->getElementCount().isKnownEven();
1890// Maybe even element count so need to clear ones. 1891if (VecTy->isScalableTy() || EvenCnt)
1895case Intrinsic::umin:
1900case Intrinsic::umax:
1905case Intrinsic::smin:
1911case Intrinsic::smax:
1917case Intrinsic::ptrmask: {
1920constValue *Mask =
I->getOperand(1);
1921 Known2 =
KnownBits(Mask->getType()->getScalarSizeInBits());
1923// TODO: 1-extend would be more precise. 1927case Intrinsic::x86_sse2_pmulh_w:
1928case Intrinsic::x86_avx2_pmulh_w:
1929case Intrinsic::x86_avx512_pmulh_w_512:
1934case Intrinsic::x86_sse2_pmulhu_w:
1935case Intrinsic::x86_avx2_pmulhu_w:
1936case Intrinsic::x86_avx512_pmulhu_w_512:
1941case Intrinsic::x86_sse42_crc32_64_64:
1944case Intrinsic::x86_ssse3_phadd_d_128:
1945case Intrinsic::x86_ssse3_phadd_w_128:
1946case Intrinsic::x86_avx2_phadd_d:
1947case Intrinsic::x86_avx2_phadd_w: {
1955case Intrinsic::x86_ssse3_phadd_sw_128:
1956case Intrinsic::x86_avx2_phadd_sw: {
1961case Intrinsic::x86_ssse3_phsub_d_128:
1962case Intrinsic::x86_ssse3_phsub_w_128:
1963case Intrinsic::x86_avx2_phsub_d:
1964case Intrinsic::x86_avx2_phsub_w: {
1972case Intrinsic::x86_ssse3_phsub_sw_128:
1973case Intrinsic::x86_avx2_phsub_sw: {
1978case Intrinsic::riscv_vsetvli:
1979case Intrinsic::riscv_vsetvlimax: {
1980bool HasAVL =
II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1983 cast<ConstantInt>(
II->getArgOperand(HasAVL))->getZExtValue());
1985 cast<ConstantInt>(
II->getArgOperand(1 + HasAVL))->getZExtValue());
1990// Result of vsetvli must be not larger than AVL. 1992if (
auto *CI = dyn_cast<ConstantInt>(
II->getArgOperand(0)))
1993 MaxVL = std::min(MaxVL, CI->getZExtValue());
1995unsigned KnownZeroFirstBit =
Log2_32(MaxVL) + 1;
2000case Intrinsic::vscale: {
2001if (!
II->getParent() || !
II->getFunction())
2011case Instruction::ShuffleVector: {
2012auto *Shuf = dyn_cast<ShuffleVectorInst>(
I);
2013// FIXME: Do we need to handle ConstantExpr involving shufflevectors? 2018// For undef elements, we don't know anything about the common state of 2019// the shuffle result. 2020APInt DemandedLHS, DemandedRHS;
2028constValue *
LHS = Shuf->getOperand(0);
2030// If we don't know any bits, early out. 2035constValue *
RHS = Shuf->getOperand(1);
2041case Instruction::InsertElement: {
2042if (isa<ScalableVectorType>(
I->getType())) {
2046constValue *Vec =
I->getOperand(0);
2047constValue *Elt =
I->getOperand(1);
2048auto *CIdx = dyn_cast<ConstantInt>(
I->getOperand(2));
2050APInt DemandedVecElts = DemandedElts;
2052// If we know the index we are inserting too, clear it from Vec check. 2053if (CIdx && CIdx->getValue().ult(NumElts)) {
2054 DemandedVecElts.
clearBit(CIdx->getZExtValue());
2055 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2062// If we don't know any bits, early out. 2067if (!DemandedVecElts.
isZero()) {
2073case Instruction::ExtractElement: {
2074// Look through extract element. If the index is non-constant or 2075// out-of-range demand all elements, otherwise just the extracted element. 2076constValue *Vec =
I->getOperand(0);
2078auto *CIdx = dyn_cast<ConstantInt>(
Idx);
2079if (isa<ScalableVectorType>(Vec->
getType())) {
2080// FIXME: there's probably *something* we can do with scalable vectors 2084unsigned NumElts = cast<FixedVectorType>(Vec->
getType())->getNumElements();
2086if (CIdx && CIdx->getValue().ult(NumElts))
2091case Instruction::ExtractValue:
2096switch (
II->getIntrinsicID()) {
2098case Intrinsic::uadd_with_overflow:
2099case Intrinsic::sadd_with_overflow:
2101true,
II->getArgOperand(0),
II->getArgOperand(1),
/*NSW=*/false,
2102/* NUW=*/false, DemandedElts, Known, Known2,
Depth, Q);
2104case Intrinsic::usub_with_overflow:
2105case Intrinsic::ssub_with_overflow:
2107false,
II->getArgOperand(0),
II->getArgOperand(1),
/*NSW=*/false,
2108/* NUW=*/false, DemandedElts, Known, Known2,
Depth, Q);
2110case Intrinsic::umul_with_overflow:
2111case Intrinsic::smul_with_overflow:
2113false, DemandedElts, Known, Known2,
Depth, Q);
2119case Instruction::Freeze:
2127/// Determine which bits of V are known to be either zero or one and return 2136/// Determine which bits of V are known to be either zero or one and return 2145/// Determine which bits of V are known to be either zero or one and return 2146/// them in the Known bit set. 2148/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that 2149/// we cannot optimize based on the assumption that it is zero without changing 2150/// it to be an explicit zero. If we don't change it to zero, other code could 2151/// optimized based on the contradictory assumption that it is non-zero. 2152/// Because instcombine aggressively folds operations with undef args anyway, 2153/// this won't lose us code quality. 2155/// This function is defined on values with integer type, values with pointer 2156/// type, and vectors of integers. In the case 2157/// where V is a vector, known zero, and known one values are the 2158/// same width as the vector element, and the bit is set only if it is true 2159/// for all of the demanded elements in the vector specified by DemandedElts. 2164// No demanded elts, better to assume we don't know anything. 2173Type *Ty = V->getType();
2177"Not integer or pointer type!");
2179if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2181 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
2182"DemandedElt width should equal the fixed vector number of elements");
2185"DemandedElt width should be 1 for scalars or scalable vectors");
2191"V and Known should have same BitWidth");
2194"V and Known should have same BitWidth");
2200// We know all of the bits for a scalar constant or a splat vector constant! 2204// Null and aggregate-zero are all-zeros. 2205if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2209// Handle a constant vector by taking the intersection of the known bits of 2212assert(!isa<ScalableVectorType>(V->getType()));
2213// We know that CDV must be a vector of integers. Take the intersection of 2216for (
unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2217if (!DemandedElts[i])
2219APInt Elt = CDV->getElementAsAPInt(i);
2228if (
constauto *CV = dyn_cast<ConstantVector>(V)) {
2229assert(!isa<ScalableVectorType>(V->getType()));
2230// We know that CV must be a vector of integers. Take the intersection of 2233for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2234if (!DemandedElts[i])
2237if (isa<PoisonValue>(Element))
2239auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2244constAPInt &Elt = ElementCI->getValue();
2253// Start out not knowing anything. 2256// We can't imply anything about undefs. 2257if (isa<UndefValue>(V))
2260// There's no point in looking through other users of ConstantData for 2261// assumptions. Confirm that we've handled them all. 2262assert(!isa<ConstantData>(V) &&
"Unhandled constant data!");
2264if (
constauto *
A = dyn_cast<Argument>(V))
2265if (std::optional<ConstantRange>
Range =
A->getRange())
2268// All recursive calls that increase depth must come after this. 2272// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has 2273// the bits of its aliasee. 2274if (
constGlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2275if (!GA->isInterposable())
2280if (
constOperator *
I = dyn_cast<Operator>(V))
2282elseif (
constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2283if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2284 Known = CR->toKnownBits();
2287// Aligned pointers have trailing zeros - refine Known.Zero set 2288if (isa<PointerType>(V->getType())) {
2289Align Alignment = V->getPointerAlignment(Q.
DL);
2293// computeKnownBitsFromContext strictly refines Known. 2294// Therefore, we run them after computeKnownBitsFromOperator. 2296// Check whether we can determine known bits from context such as assumes. 2300/// Try to detect a recurrence that the value of the induction variable is 2301/// always a power of two (or zero). 2305Value *Start =
nullptr, *Step =
nullptr;
2309// Initial value must be a power of two. 2311if (U.get() == Start) {
2312// Initial value comes from a different BB, need to adjust context 2313// instruction for analysis. 2320// Except for Mul, the induction variable must be on the left side of the 2321// increment expression, otherwise its value can be arbitrary. 2327case Instruction::Mul:
2328// Power of two is closed under multiplication. 2332case Instruction::SDiv:
2333// Start value must not be signmask for signed division, so simply being a 2334// power of two is not sufficient, and it has to be a constant. 2338case Instruction::UDiv:
2339// Divisor must be a power of two. 2340// If OrZero is false, cannot guarantee induction variable is non-zero after 2341// division, same for Shr, unless it is exact division. 2344case Instruction::Shl:
2346case Instruction::AShr:
2350case Instruction::LShr:
2357/// Return true if we can infer that \p V is known to be a power of 2 from 2358/// dominating condition \p Cond (e.g., ctpop(V) == 1). 2368 Pred = ICmpInst::getInversePredicate(Pred);
2370if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2373return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2376/// Return true if the given value is known to have exactly one 2377/// bit set when defined. For vectors return true if every element is known to 2378/// be a power of two when defined. Supports values with integer or pointer 2379/// types and vectors of integers. 2384if (isa<Constant>(V))
2387// i1 is by definition a power of 2 or zero. 2388if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2391// Try to infer from assumptions. 2398/*CondIsTrue=*/true) &&
2404// Handle dominating conditions. 2411/*CondIsTrue=*/true) &&
2417/*CondIsTrue=*/false) &&
2423auto *
I = dyn_cast<Instruction>(V);
2429// The vscale_range indicates vscale is a power-of-two. 2430returnF->hasFnAttribute(Attribute::VScaleRange);
2433// 1 << X is clearly a power of two if the one is not shifted off the end. If 2434// it is shifted off the end then the result is undefined. 2438// (signmask) >>l X is clearly a power of two if the one is not shifted off 2439// the bottom. If it is shifted off the bottom then the result is undefined. 2443// The remaining tests are all recursive, so bail out if we hit the limit. 2447switch (
I->getOpcode()) {
2448case Instruction::ZExt:
2450case Instruction::Trunc:
2452case Instruction::Shl:
2456case Instruction::LShr:
2457if (OrZero || Q.
IIQ.
isExact(cast<BinaryOperator>(
I)))
2460case Instruction::UDiv:
2464case Instruction::Mul:
2468case Instruction::And:
2469// A power of two and'd with anything is a power of two or zero. 2474// X & (-X) is always a power of two or zero. 2479case Instruction::Add: {
2480// Adding a power-of-two or zero to the same power-of-two or zero yields 2481// either the original power-of-two, a larger power-of-two or zero. 2485if (
match(
I->getOperand(0),
2489if (
match(
I->getOperand(1),
2494unsignedBitWidth = V->getType()->getScalarSizeInBits();
2500// If i8 V is a power of two or zero: 2501// ZeroBits: 1 1 1 0 1 1 1 1 2502// ~ZeroBits: 0 0 0 1 0 0 0 0 2503if ((~(LHSBits.
Zero & RHSBits.
Zero)).isPowerOf2())
2504// If OrZero isn't set, we cannot give back a zero result. 2505// Make sure either the LHS or RHS has a bit set. 2510// LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero. 2516case Instruction::Select:
2519case Instruction::PHI: {
2520// A PHI node is power of two if all incoming values are power of two, or if 2521// it is an induction variable where in each step its value is a power of 2523auto *PN = cast<PHINode>(
I);
2526// Check if it is an induction variable and always power of two. 2530// Recursively check all incoming values. Limit recursion to 2 levels, so 2531// that search complexity is limited to number of operands^2. 2534// Value is power of 2 if it is coming from PHI node itself by induction. 2538// Change the context instruction to the incoming block where it is 2540 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2541 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2544case Instruction::Invoke:
2545case Instruction::Call: {
2546if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
2547switch (
II->getIntrinsicID()) {
2548case Intrinsic::umax:
2549case Intrinsic::smax:
2550case Intrinsic::umin:
2551case Intrinsic::smin:
2554// bswap/bitreverse just move around bits, but don't change any 1s/0s 2555// thus dont change pow2/non-pow2 status. 2556case Intrinsic::bitreverse:
2557case Intrinsic::bswap:
2559case Intrinsic::fshr:
2560case Intrinsic::fshl:
2561// If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x) 2562if (
II->getArgOperand(0) ==
II->getArgOperand(1))
2576/// Test whether a GEP's result is known to be non-null. 2578/// Uses properties inherent in a GEP to try to determine whether it is known 2581/// Currently this routine does not support vector GEPs. 2588// If the gep is nuw or inbounds with invalid null pointer, then the GEP 2589// may be null iff the base pointer is null and the offset is zero. 2590if (!
GEP->hasNoUnsignedWrap() &&
2591 !(
GEP->isInBounds() &&
2595// FIXME: Support vector-GEPs. 2596assert(
GEP->getType()->isPointerTy() &&
"We only support plain pointer GEP");
2598// If the base pointer is non-null, we cannot walk to a null address with an 2599// inbounds GEP in address space zero. 2603// Walk the GEP operands and see if any operand introduces a non-zero offset. 2604// If so, then the GEP cannot produce a null pointer, as doing so would 2605// inherently violate the inbounds contract within address space zero. 2607 GTI != GTE; ++GTI) {
2608// Struct types are easy -- they must always be indexed by a constant. 2609if (
StructType *STy = GTI.getStructTypeOrNull()) {
2610ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2614if (ElementOffset > 0)
2619// If we have a zero-sized type, the index doesn't matter. Keep looping. 2620if (GTI.getSequentialElementStride(Q.
DL).isZero())
2623// Fast path the constant operand case both for efficiency and so we don't 2624// increment Depth when just zipping down an all-constant GEP. 2625if (
ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2631// We post-increment Depth here because while isKnownNonZero increments it 2632// as well, when we pop back up that increment won't persist. We don't want 2633// to recurse 10k times just because we have 10k GEP operands. We don't 2634// bail completely out because we want to handle constant GEPs regardless 2649assert(!isa<Constant>(V) &&
"Called for constant?");
2654unsigned NumUsesExplored = 0;
2655for (
constauto *U : V->users()) {
2656// Avoid massive lists 2661// If the value is used as an argument to a call or invoke, then argument 2662// attributes may provide an answer about null-ness. 2663if (
constauto *CB = dyn_cast<CallBase>(U))
2664if (
auto *CalledFunc = CB->getCalledFunction())
2665for (
constArgument &Arg : CalledFunc->args())
2666if (CB->getArgOperand(Arg.getArgNo()) == V &&
2667 Arg.hasNonNullAttr(
/* AllowUndefOrPoison */false) &&
2671// If the value is used as a load/store, then the pointer must be non null. 2675 V->getType()->getPointerAddressSpace()) &&
2685// Consider only compare instructions uniquely controlling a branch 2693 NonNullIfTrue =
true;
2695 NonNullIfTrue =
false;
2701for (
constauto *CmpU : U->users()) {
2703if (Visited.
insert(CmpU).second)
2706while (!WorkList.
empty()) {
2709// If a user is an AND, add all its users to the work list. We only 2710// propagate "pred != null" condition through AND because it is only 2711// correct to assume that all conditions of AND are met in true branch. 2712// TODO: Support similar logic of OR and EQ predicate? 2715for (
constauto *CurrU : Curr->users())
2716if (Visited.
insert(CurrU).second)
2721if (
constBranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2722assert(BI->isConditional() &&
"uses a comparison!");
2725 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2729 }
elseif (NonNullIfTrue &&
isGuard(Curr) &&
2730 DT->
dominates(cast<Instruction>(Curr), CtxI)) {
2740/// Does the 'Range' metadata (which must be a valid MD_range operand list) 2741/// ensure that the value it's attached to is never Value? 'RangeType' is 2742/// is the type of the value described by the range. 2744constunsigned NumRanges = Ranges->getNumOperands() / 2;
2746for (
unsigned i = 0; i < NumRanges; ++i) {
2748 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2750 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2758/// Try to detect a recurrence that monotonically increases/decreases from a 2759/// non-zero starting value. These are common as induction variables. 2762Value *Start =
nullptr, *Step =
nullptr;
2763constAPInt *StartC, *StepC;
2769case Instruction::Add:
2770// Starting from non-zero and stepping away from zero can never wrap back 2775case Instruction::Mul:
2778case Instruction::Shl:
2780case Instruction::AShr:
2781case Instruction::LShr:
2797Value *
Y,
bool NSW,
bool NUW) {
2798// (X + (X != 0)) is non zero 2809// If X and Y are both non-negative (as signed values) then their sum is not 2810// zero unless both X and Y are zero. 2816// If X and Y are both negative (as signed values) then their sum is not 2817// zero unless both X and Y equal INT_MIN. 2820// The sign bit of X is set. If some other bit is set then X is not equal 2824// The sign bit of Y is set. If some other bit is set then Y is not equal 2830// The sum of a non-negative number and a power of two is not zero. 2844// (X - (X != 0)) is non zero 2845// ((X != 0) - X) is non zero 2849// TODO: Move this case into isKnownNonEqual(). 2850if (
auto *
C = dyn_cast<Constant>(
X))
2854 return ::isKnownNonEqual(
X,
Y, DemandedElts,
Depth, Q);
2859Value *
Y,
bool NSW,
bool NUW) {
2860// If X and Y are non-zero then so is X * Y as long as the multiplication 2861// does not overflow. 2866// If either X or Y is odd, then if the other is non-zero the result can't 2876// If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is 2877// non-zero, then X * Y is non-zero. We can find sX and sY by just taking 2878// the lowest known One of X and Y. If they are non-zero, the result 2879// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing 2880// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth. 2888auto ShiftOp = [&](
constAPInt &Lhs,
constAPInt &Rhs) {
2889switch (
I->getOpcode()) {
2890case Instruction::Shl:
2892case Instruction::LShr:
2893return Lhs.
lshr(Rhs);
2894case Instruction::AShr:
2895return Lhs.
ashr(Rhs);
2901auto InvShiftOp = [&](
constAPInt &Lhs,
constAPInt &Rhs) {
2902switch (
I->getOpcode()) {
2903case Instruction::Shl:
2904return Lhs.
lshr(Rhs);
2905case Instruction::LShr:
2906case Instruction::AShr:
2920if (MaxShift.
uge(NumBits))
2923if (!ShiftOp(KnownVal.
One, MaxShift).isZero())
2926// If all of the bits shifted out are known to be zero, and Val is known 2927// non-zero then at least one non-zero bit must remain. 2928if (InvShiftOp(KnownVal.
Zero, NumBits - MaxShift)
2937constAPInt &DemandedElts,
2940switch (
I->getOpcode()) {
2941case Instruction::Alloca:
2942// Alloca never returns null, malloc might. 2943returnI->getType()->getPointerAddressSpace() == 0;
2944case Instruction::GetElementPtr:
2945if (
I->getType()->isPointerTy())
2948case Instruction::BitCast: {
2949// We need to be a bit careful here. We can only peek through the bitcast 2950// if the scalar size of elements in the operand are smaller than and a 2951// multiple of the size they are casting too. Take three cases: 2954// bitcast <2 x i16> %NonZero to <4 x i8> 2956// %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a 2957// <4 x i8> requires that all 4 i8 elements be non-zero which isn't 2958// guranteed (imagine just sign bit set in the 2 i16 elements). 2961// bitcast <4 x i3> %NonZero to <3 x i4> 2963// Even though the scalar size of the src (`i3`) is smaller than the 2964// scalar size of the dst `i4`, because `i3` is not a multiple of `i4` 2965// its possible for the `3 x i4` elements to be zero because there are 2966// some elements in the destination that don't contain any full src 2970// bitcast <4 x i8> %NonZero to <2 x i16> 2972// This is always safe as non-zero in the 4 i8 elements implies 2973// non-zero in the combination of any two adjacent ones. Since i8 is a 2974// multiple of i16, each i16 is guranteed to have 2 full i8 elements. 2975// This all implies the 2 i16 elements are non-zero. 2976Type *FromTy =
I->getOperand(0)->getType();
2981case Instruction::IntToPtr:
2982// Note that we have to take special care to avoid looking through 2983// truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well 2984// as casts that can alter the value, e.g., AddrSpaceCasts. 2985if (!isa<ScalableVectorType>(
I->getType()) &&
2990case Instruction::PtrToInt:
2991// Similar to int2ptr above, we can look through ptr2int here if the cast 2992// is a no-op or an extend and not a truncate. 2993if (!isa<ScalableVectorType>(
I->getType()) &&
2998case Instruction::Trunc:
2999// nuw/nsw trunc preserves zero/non-zero status of input. 3000if (
auto *TI = dyn_cast<TruncInst>(
I))
3001if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3005case Instruction::Sub:
3008case Instruction::Xor:
3009// (X ^ (X != 0)) is non zero 3013case Instruction::Or:
3014// (X | (X != 0)) is non zero 3017// X | Y != 0 if X != 0 or Y != 0. 3020case Instruction::SExt:
3021case Instruction::ZExt:
3022// ext X != 0 if X != 0. 3025case Instruction::Shl: {
3026// shl nsw/nuw can't remove any non-zero bits. 3031// shl X, Y != 0 if X is odd. Note that the value of the shift is undefined 3032// if the lowest bit is shifted off the end. 3040case Instruction::LShr:
3041case Instruction::AShr: {
3042// shr exact can only shift out zero bits. 3047// shr X, Y != 0 if X is negative. Note that the value of the shift is not 3048// defined if the sign bit is shifted off the end. 3056case Instruction::UDiv:
3057case Instruction::SDiv: {
3059// div exact can only produce a zero if the dividend is zero. 3060if (cast<PossiblyExactOperator>(
I)->isExact())
3065// If X is fully unknown we won't be able to figure anything out so don't 3066// both computing knownbits for Y. 3072if (
I->getOpcode() == Instruction::SDiv) {
3073// For signed division need to compare abs value of the operands. 3074 XKnown = XKnown.
abs(
/*IntMinIsPoison*/false);
3075 YKnown = YKnown.
abs(
/*IntMinIsPoison*/false);
3077// If X u>= Y then div is non zero (0/0 is UB). 3079// If X is total unknown or X u< Y we won't be able to prove non-zero 3080// with compute known bits so just return early. 3081return XUgeY && *XUgeY;
3083case Instruction::Add: {
3086// If Add has nuw wrap flag, then if either X or Y is non-zero the result is 3088auto *BO = cast<OverflowingBinaryOperator>(
I);
3093case Instruction::Mul: {
3099case Instruction::Select: {
3100// (C ? X : Y) != 0 if X != 0 and Y != 0. 3102// First check if the arm is non-zero using `isKnownNonZero`. If that fails, 3103// then see if the select condition implies the arm is non-zero. For example 3104// (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is 3105// dominated by `X != 0`. 3106auto SelectArmIsNonZero = [&](
bool IsTrueArm) {
3108Op = IsTrueArm ?
I->getOperand(1) :
I->getOperand(2);
3109// Op is trivially non-zero. 3113// The condition of the select dominates the true/false arm. Check if the 3114// condition implies that a given arm is non-zero. 3121 Pred = ICmpInst::getInversePredicate(Pred);
3126if (SelectArmIsNonZero(
/* IsTrueArm */true) &&
3127 SelectArmIsNonZero(
/* IsTrueArm */false))
3131case Instruction::PHI: {
3132auto *PN = cast<PHINode>(
I);
3136// Check if all incoming values are non-zero using recursion. 3142 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3143// Check if the branch on the phi excludes zero. 3146 BasicBlock *TrueSucc, *FalseSucc;
3147 if (match(RecQ.CxtI,
3148 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3149 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3150// Check for cases of duplicate successors. 3151 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3152// If we're using the false successor, invert the predicate. 3153 if (FalseSucc == PN->getParent())
3154 Pred = CmpInst::getInversePredicate(Pred);
3155 if (cmpExcludesZero(Pred, X))
3159// Finally recurse on the edge and check it directly. 3163case Instruction::InsertElement: {
3164if (isa<ScalableVectorType>(
I->getType()))
3167constValue *Vec =
I->getOperand(0);
3168constValue *Elt =
I->getOperand(1);
3169auto *CIdx = dyn_cast<ConstantInt>(
I->getOperand(2));
3172APInt DemandedVecElts = DemandedElts;
3174// If we know the index we are inserting too, clear it from Vec check. 3175if (CIdx && CIdx->getValue().ult(NumElts)) {
3176 DemandedVecElts.
clearBit(CIdx->getZExtValue());
3177 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3180// Result is zero if Elt is non-zero and rest of the demanded elts in Vec 3183 (DemandedVecElts.
isZero() ||
3186case Instruction::ExtractElement:
3187if (
constauto *EEI = dyn_cast<ExtractElementInst>(
I)) {
3188constValue *Vec = EEI->getVectorOperand();
3189constValue *
Idx = EEI->getIndexOperand();
3190auto *CIdx = dyn_cast<ConstantInt>(
Idx);
3191if (
auto *VecTy = dyn_cast<FixedVectorType>(Vec->
getType())) {
3192unsigned NumElts = VecTy->getNumElements();
3194if (CIdx && CIdx->getValue().ult(NumElts))
3200case Instruction::ShuffleVector: {
3201auto *Shuf = dyn_cast<ShuffleVectorInst>(
I);
3204APInt DemandedLHS, DemandedRHS;
3205// For undef elements, we don't know anything about the common state of 3206// the shuffle result. 3209// If demanded elements for both vecs are non-zero, the shuffle is non-zero. 3210return (DemandedRHS.
isZero() ||
3215case Instruction::Freeze:
3219case Instruction::Load: {
3220auto *LI = cast<LoadInst>(
I);
3221// A Load tagged with nonnull or dereferenceable with null pointer undefined 3223if (
auto *PtrT = dyn_cast<PointerType>(
I->getType())) {
3232// No need to fall through to computeKnownBits as range metadata is already 3233// handled in isKnownNonZero. 3236case Instruction::ExtractValue: {
3242case Instruction::Add:
3247case Instruction::Sub:
3250case Instruction::Mul:
3253/*NSW=*/false,
/*NUW=*/false);
3259case Instruction::Call:
3260case Instruction::Invoke: {
3261constauto *Call = cast<CallBase>(
I);
3262if (
I->getType()->isPointerTy()) {
3263if (Call->isReturnNonNull())
3270if (std::optional<ConstantRange>
Range = Call->getRange()) {
3275if (
constValue *RV = Call->getReturnedArgOperand())
3280if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
3281switch (
II->getIntrinsicID()) {
3282case Intrinsic::sshl_sat:
3283case Intrinsic::ushl_sat:
3285case Intrinsic::bitreverse:
3286case Intrinsic::bswap:
3287case Intrinsic::ctpop:
3289// NB: We don't do usub_sat here as in any case we can prove its 3290// non-zero, we will fold it to `sub nuw` in InstCombine. 3291case Intrinsic::ssub_sat:
3293II->getArgOperand(0),
II->getArgOperand(1));
3294case Intrinsic::sadd_sat:
3296II->getArgOperand(0),
II->getArgOperand(1),
3297/*NSW=*/true,
/* NUW=*/false);
3298// Vec reverse preserves zero/non-zero status from input vec. 3299case Intrinsic::vector_reverse:
3302// umin/smin/smax/smin/or of all non-zero elements is always non-zero. 3303case Intrinsic::vector_reduce_or:
3304case Intrinsic::vector_reduce_umax:
3305case Intrinsic::vector_reduce_umin:
3306case Intrinsic::vector_reduce_smax:
3307case Intrinsic::vector_reduce_smin:
3309case Intrinsic::umax:
3310case Intrinsic::uadd_sat:
3311// umax(X, (X != 0)) is non zero 3312// X +usat (X != 0) is non zero 3318case Intrinsic::smax: {
3319// If either arg is strictly positive the result is non-zero. Otherwise 3320// the result is non-zero if both ops are non-zero. 3321auto IsNonZero = [&](
Value *
Op, std::optional<bool> &OpNonZero,
3323if (!OpNonZero.has_value())
3324 OpNonZero = OpKnown.isNonZero() ||
3328// Avoid re-computing isKnownNonZero. 3329 std::optional<bool> Op0NonZero, Op1NonZero;
3333 IsNonZero(
II->getArgOperand(1), Op1NonZero, Op1Known))
3338 IsNonZero(
II->getArgOperand(0), Op0NonZero, Op0Known))
3340return IsNonZero(
II->getArgOperand(1), Op1NonZero, Op1Known) &&
3341 IsNonZero(
II->getArgOperand(0), Op0NonZero, Op0Known);
3343case Intrinsic::smin: {
3344// If either arg is negative the result is non-zero. Otherwise 3345// the result is non-zero if both ops are non-zero. 3359case Intrinsic::umin:
3362case Intrinsic::cttz:
3365case Intrinsic::ctlz:
3368case Intrinsic::fshr:
3369case Intrinsic::fshl:
3370// If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0. 3371if (
II->getArgOperand(0) ==
II->getArgOperand(1))
3374case Intrinsic::vscale:
3376case Intrinsic::experimental_get_vector_length:
3390return Known.
One != 0;
3393/// Return true if the given value is known to be non-zero when defined. For 3394/// vectors, return true if every demanded element is known to be non-zero when 3395/// defined. For pointers, if the context instruction and dominator tree are 3396/// specified, perform context-sensitive analysis and return true if the 3397/// pointer couldn't possibly be null at the specified instruction. 3398/// Supports values with integer or pointer type and vectors of integers. 3401Type *Ty = V->getType();
3406if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3408 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
3409"DemandedElt width should equal the fixed vector number of elements");
3412"DemandedElt width should be 1 for scalars");
3416if (
auto *
C = dyn_cast<Constant>(V)) {
3417if (
C->isNullValue())
3419if (isa<ConstantInt>(
C))
3420// Must be non-zero due to null test above. 3423// For constant vectors, check that all elements are poison or known 3424// non-zero to determine that the whole vector is known non-zero. 3425if (
auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3426for (
unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3427if (!DemandedElts[i])
3429Constant *Elt =
C->getAggregateElement(i);
3432if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3438// Constant ptrauth can be null, iff the base pointer can be. 3439if (
auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3442// A global variable in address space 0 is non null unless extern weak 3443// or an absolute symbol reference. Other address spaces may have null as a 3444// valid address for a global, so we can't assume anything. 3445if (
constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3446if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3447 GV->getType()->getAddressSpace() == 0)
3451// For constant expressions, fall through to the Operator code below. 3452if (!isa<ConstantExpr>(V))
3456if (
constauto *
A = dyn_cast<Argument>(V))
3457if (std::optional<ConstantRange>
Range =
A->getRange()) {
3466// Some of the tests below are recursive, so bail out if we hit the limit. 3470// Check for pointer simplifications. 3472if (
PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3473// A byval, inalloca may not be null in a non-default addres space. A 3474// nonnull argument is assumed never 0. 3475if (
constArgument *
A = dyn_cast<Argument>(V)) {
3476if (((
A->hasPassPointeeByValueCopyAttr() &&
3478A->hasNonNullAttr()))
3483if (
constauto *
I = dyn_cast<Operator>(V))
3487if (!isa<Constant>(V) &&
3496auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3499 return ::isKnownNonZero(V, DemandedElts, Q,
Depth);
3502/// If the pair of operators are the same invertible function, return the 3503/// the operands of the function corresponding to each input. Otherwise, 3504/// return std::nullopt. An invertible function is one that is 1-to-1 and maps 3505/// every input value to exactly one output value. This is equivalent to 3506/// saying that Op1 and Op2 are equal exactly when the specified pair of 3507/// operands are equal, (except that Op1 and Op2 may be poison more often.) 3508static std::optional<std::pair<Value*, Value*>>
3521case Instruction::Or:
3522if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3523 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3526case Instruction::Xor:
3527case Instruction::Add: {
3535case Instruction::Sub:
3541case Instruction::Mul: {
3542// invertible if A * B == (A * B) mod 2^N where A, and B are integers 3543// and N is the bitwdith. The nsw case is non-obvious, but proven by 3544// alive2: https://alive2.llvm.org/ce/z/Z6D5qK 3545auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3546auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3547if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3548 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3551// Assume operand order has been canonicalized 3554 !cast<ConstantInt>(Op1->
getOperand(1))->isZero())
3558case Instruction::Shl: {
3559// Same as multiplies, with the difference that we don't need to check 3560// for a non-zero multiply. Shifts always multiply by non-zero. 3561auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3562auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3563if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3564 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3571case Instruction::AShr:
3572case Instruction::LShr: {
3573auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3574auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3575if (!PEO1->isExact() || !PEO2->isExact())
3582case Instruction::SExt:
3583case Instruction::ZExt:
3587case Instruction::PHI: {
3588constPHINode *PN1 = cast<PHINode>(Op1);
3589constPHINode *PN2 = cast<PHINode>(Op2);
3591// If PN1 and PN2 are both recurrences, can we prove the entire recurrences 3592// are a single invertible function of the start values? Note that repeated 3593// application of an invertible function is also invertible 3595Value *Start1 =
nullptr, *Step1 =
nullptr;
3597Value *Start2 =
nullptr, *Step2 =
nullptr;
3604 cast<Operator>(BO2));
3608// We have to be careful of mutually defined recurrences here. Ex: 3609// * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V 3610// * X_i = Y_i = X_(i-1) OP Y_(i-1) 3611// The invertibility of these is complicated, and not worth reasoning 3613if (Values->first != PN1 || Values->second != PN2)
3616return std::make_pair(Start1, Start2);
3622/// Return true if V1 == (binop V2, X), where X is known non-zero. 3623/// Only handle a small subset of binops where (binop V2, X) with non-zero X 3624/// implies V2 != V1. 3634case Instruction::Or:
3635if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3638case Instruction::Xor:
3639case Instruction::Add:
3652/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and 3653/// the multiplication is nuw or nsw. 3657if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3660 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3661 !
C->isZero() && !
C->isOne() &&
3667/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and 3668/// the shift is nuw or nsw. 3672if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3675 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3684// Check two PHIs are in same block. 3689bool UsedFullRecursion =
false;
3691if (!VisitedBBs.
insert(IncomBB).second)
3692continue;
// Don't reprocess blocks that we have dealt with already. 3699// Only one pair of phi operands is allowed for full recursion. 3700if (UsedFullRecursion)
3704 RecQ.
CxtI = IncomBB->getTerminator();
3707 UsedFullRecursion =
true;
3715constSelectInst *SI1 = dyn_cast<SelectInst>(V1);
3719if (
constSelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3721constValue *Cond2 = SI2->getCondition();
3724 DemandedElts,
Depth + 1, Q) &&
3726 DemandedElts,
Depth + 1, Q);
3732// Check to see if A is both a GEP and is the incoming value for a PHI in the 3733// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values, 3734// one of them being the recursive GEP A and the other a ptr at same base and at 3735// the same/higher offset than B we are only incrementing the pointer further in 3736// loop if offset of recursive GEP is greater than 0. 3739if (!
A->getType()->isPointerTy() || !
B->getType()->isPointerTy())
3742auto *GEPA = dyn_cast<GEPOperator>(
A);
3743if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3746// Handle 2 incoming PHI values with one being a recursive GEP. 3747auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3748if (!PN || PN->getNumIncomingValues() != 2)
3751// Search for the recursive GEP as an incoming operand, and record that as 3753Value *Start =
nullptr;
3755if (PN->getIncomingValue(0) == Step)
3756 Start = PN->getIncomingValue(1);
3757elseif (PN->getIncomingValue(1) == Step)
3758 Start = PN->getIncomingValue(0);
3762// Other incoming node base should match the B base. 3763// StartOffset >= OffsetB && StepOffset > 0? 3764// StartOffset <= OffsetB && StepOffset < 0? 3765// Is non-equal if above are true. 3766// We use stripAndAccumulateInBoundsConstantOffsets to restrict the 3767// optimisation to inbounds GEPs only. 3769APInt StartOffset(IndexWidth, 0);
3770 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.
DL, StartOffset);
3771APInt StepOffset(IndexWidth, 0);
3774// Check if Base Pointer of Step matches the PHI. 3777APInt OffsetB(IndexWidth, 0);
3778B =
B->stripAndAccumulateInBoundsConstantOffsets(Q.
DL, OffsetB);
3784/// Return true if it is known that V1 != V2. 3790if (V1->
getType() != V2->getType())
3791// We can't look through casts yet. 3797// See if we can recurse through (exactly one of) our operands. This 3798// requires our operation be 1-to-1 and map every input value to exactly 3799// one output value. Such an operation is invertible. 3800auto *O1 = dyn_cast<Operator>(V1);
3801auto *O2 = dyn_cast<Operator>(V2);
3802if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3807if (
constPHINode *PN1 = dyn_cast<PHINode>(V1)) {
3808constPHINode *PN2 = cast<PHINode>(V2);
3809// FIXME: This is missing a generalization to handle the case where one is 3810// a PHI and another one isn't. 3829// Are any known bits in V1 contradictory to known bits in V2? If V1 3830// has a known zero where V2 has a known one, they must not be equal. 3849// PtrToInts are NonEqual if their Ptrs are NonEqual. 3850// Check PtrToInt type matches the pointer size. 3858/// For vector constants, loop over the elements and find the constant with the 3859/// minimum number of sign bits. Return 0 if the value is not a vector constant 3860/// or if any element was not analyzed; otherwise, return the count for the 3861/// element with the minimum number of sign bits. 3863constAPInt &DemandedElts,
3865constauto *CV = dyn_cast<Constant>(V);
3866if (!CV || !isa<FixedVectorType>(CV->getType()))
3869unsigned MinSignBits = TyBits;
3870unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3871for (
unsigned i = 0; i != NumElts; ++i) {
3872if (!DemandedElts[i])
3874// If we find a non-ConstantInt, bail out. 3875auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3879 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3886constAPInt &DemandedElts,
3892assert(Result > 0 &&
"At least one sign bit needs to be present!");
3896/// Return the number of times the sign bit of the register is replicated into 3897/// the other bits. We know that at least 1 bit is always equal to the sign bit 3898/// (itself), but other cases can give us information. For example, immediately 3899/// after an "ashr X, 2", we know that the top 3 bits are all equal to each 3900/// other, so we return 3. For vectors, return the number of sign bits for the 3901/// vector element with the minimum number of known sign bits of the demanded 3902/// elements in the vector specified by DemandedElts. 3904constAPInt &DemandedElts,
3906Type *Ty = V->getType();
3910if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3912 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
3913"DemandedElt width should equal the fixed vector number of elements");
3916"DemandedElt width should be 1 for scalars");
3920// We return the minimum number of sign bits that are guaranteed to be present 3921// in V, so for undef we have to conservatively return 1. We don't have the 3922// same behavior for poison though -- that's a FIXME today. 3930unsigned FirstAnswer = 1;
3932// Note that ConstantInt is handled by the general computeKnownBits case 3938if (
auto *U = dyn_cast<Operator>(V)) {
3941case Instruction::SExt:
3942 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3946case Instruction::SDiv: {
3947constAPInt *Denominator;
3948// sdiv X, C -> adds log(C) sign bits. 3951// Ignore non-positive denominator. 3955// Calculate the incoming numerator bits. 3959// Add floor(log(C)) bits to the numerator bits. 3960return std::min(TyBits, NumBits + Denominator->
logBase2());
3965case Instruction::SRem: {
3968constAPInt *Denominator;
3969// srem X, C -> we know that the result is within [-C+1,C) when C is a 3970// positive constant. This let us put a lower bound on the number of sign 3974// Ignore non-positive denominator. 3976// Calculate the leading sign bit constraints by examining the 3977// denominator. Given that the denominator is positive, there are two 3980// 1. The numerator is positive. The result range is [0,C) and 3981// [0,C) u< (1 << ceilLogBase2(C)). 3983// 2. The numerator is negative. Then the result range is (-C,0] and 3984// integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). 3986// Thus a lower bound on the number of sign bits is `TyBits - 3989unsigned ResBits = TyBits - Denominator->
ceilLogBase2();
3990 Tmp = std::max(Tmp, ResBits);
3996case Instruction::AShr: {
3998// ashr X, C -> adds C sign bits. Vectors too. 4001if (ShAmt->
uge(TyBits))
4004 Tmp += ShAmtLimited;
4005if (Tmp > TyBits) Tmp = TyBits;
4009case Instruction::Shl: {
4013// shl destroys sign bits. 4014if (ShAmt->
uge(TyBits))
4016// We can look through a zext (more or less treating it as a sext) if 4017// all extended bits are shifted out. 4019 ShAmt->
uge(TyBits -
X->getType()->getScalarSizeInBits())) {
4021 Tmp += TyBits -
X->getType()->getScalarSizeInBits();
4026break;
// Shifted all sign bits out. 4032case Instruction::And:
4033case Instruction::Or:
4034case Instruction::Xor:
// NOT is handled here. 4035// Logical binary ops preserve the number of sign bits at the worst. 4039 FirstAnswer = std::min(Tmp, Tmp2);
4040// We computed what we know about the sign bits as our first 4041// answer. Now proceed to the generic code that uses 4042// computeKnownBits, and pick whichever answer is better. 4046case Instruction::Select: {
4047// If we have a clamp pattern, we know that the number of sign bits will 4048// be the minimum of the clamp min/max range. 4050constAPInt *CLow, *CHigh;
4058return std::min(Tmp, Tmp2);
4061case Instruction::Add:
4062// Add can have at most one carry bit. Thus we know that the output 4063// is, at worst, one more bit than the inputs. 4067// Special case decrementing a value (ADD X, -1): 4068if (
constauto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4069if (CRHS->isAllOnesValue()) {
4073// If the input is known to be 0 or 1, the output is 0/-1, which is 4074// all sign bits set. 4075if ((Known.
Zero | 1).isAllOnes())
4078// If we are subtracting one from a positive number, there is no carry 4079// out of the result. 4087return std::min(Tmp, Tmp2) - 1;
4089case Instruction::Sub:
4095if (
constauto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4096if (CLHS->isNullValue()) {
4099// If the input is known to be 0 or 1, the output is 0/-1, which is 4100// all sign bits set. 4101if ((Known.
Zero | 1).isAllOnes())
4104// If the input is known to be positive (the sign bit is known clear), 4105// the output of the NEG has the same number of sign bits as the 4110// Otherwise, we treat this like a SUB. 4113// Sub can have at most one carry bit. Thus we know that the output 4114// is, at worst, one more bit than the inputs. 4118return std::min(Tmp, Tmp2) - 1;
4120case Instruction::Mul: {
4121// The output of the Mul can be at most twice the valid bits in the 4123unsigned SignBitsOp0 =
4125if (SignBitsOp0 == 1)
4127unsigned SignBitsOp1 =
4129if (SignBitsOp1 == 1)
4131unsigned OutValidBits =
4132 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4133return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4136case Instruction::PHI: {
4137constPHINode *PN = cast<PHINode>(U);
4139// Don't analyze large in-degree PHIs. 4140if (NumIncomingValues > 4)
break;
4141// Unreachable blocks may have zero-operand PHI nodes. 4142if (NumIncomingValues == 0)
break;
4144// Take the minimum of all incoming values. This can't infinitely loop 4145// because of our depth threshold. 4148for (
unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4149if (Tmp == 1)
return Tmp;
4152 DemandedElts,
Depth + 1, RecQ));
4157case Instruction::Trunc: {
4158// If the input contained enough sign bits that some remain after the 4159// truncation, then we can make use of that. Otherwise we don't know 4162unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4163if (Tmp > (OperandTyBits - TyBits))
4164return Tmp - (OperandTyBits - TyBits);
4169case Instruction::ExtractElement:
4170// Look through extract element. At the moment we keep this simple and 4171// skip tracking the specific element. But at least we might find 4172// information valid for all elements of the vector (for example if vector 4173// is sign extended, shifted, etc). 4176case Instruction::ShuffleVector: {
4177// Collect the minimum number of sign bits that are shared by every vector 4178// element referenced by the shuffle. 4179auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4181// FIXME: Add support for shufflevector constant expressions. 4184APInt DemandedLHS, DemandedRHS;
4185// For undef elements, we don't know anything about the common state of 4186// the shuffle result. 4189 Tmp = std::numeric_limits<unsigned>::max();
4191constValue *
LHS = Shuf->getOperand(0);
4194// If we don't know anything, early out and try computeKnownBits 4199constValue *
RHS = Shuf->getOperand(1);
4201 Tmp = std::min(Tmp, Tmp2);
4203// If we don't know anything, early out and try computeKnownBits 4207assert(Tmp <= TyBits &&
"Failed to determine minimum sign bits");
4210case Instruction::Call: {
4211if (
constauto *
II = dyn_cast<IntrinsicInst>(U)) {
4212switch (
II->getIntrinsicID()) {
4221// Absolute value reduces number of sign bits by at most 1. 4223case Intrinsic::smin:
4224case Intrinsic::smax: {
4225constAPInt *CLow, *CHigh;
4235// Finally, if we can prove that the top bits of the result are 0's or 1's, 4236// use this information. 4238// If we can examine all elements of a vector constant successfully, we're 4239// done (we can't do any better than that). If not, keep trying. 4240if (
unsigned VecSignBits =
4247// If we know that the sign bit is either zero or one, determine the number of 4248// identical bits in the top of the input value. 4258if (
F->isIntrinsic())
4259returnF->getIntrinsicID();
4261// We are going to infer semantics of a library function based on mapping it 4262// to an LLVM intrinsic. Check that the library function is available from 4263// this callbase and in this environment. 4265if (
F->hasLocalLinkage() || !TLI || !TLI->
getLibFunc(CB, Func) ||
4275return Intrinsic::sin;
4279return Intrinsic::cos;
4283return Intrinsic::tan;
4287return Intrinsic::asin;
4291return Intrinsic::acos;
4295return Intrinsic::atan;
4299return Intrinsic::atan2;
4303return Intrinsic::sinh;
4307return Intrinsic::cosh;
4311return Intrinsic::tanh;
4315return Intrinsic::exp;
4319return Intrinsic::exp2;
4323return Intrinsic::exp10;
4327return Intrinsic::log;
4331return Intrinsic::log10;
4335return Intrinsic::log2;
4339return Intrinsic::fabs;
4343return Intrinsic::minnum;
4347return Intrinsic::maxnum;
4348case LibFunc_copysign:
4349case LibFunc_copysignf:
4350case LibFunc_copysignl:
4351return Intrinsic::copysign;
4355return Intrinsic::floor;
4359return Intrinsic::ceil;
4363return Intrinsic::trunc;
4367return Intrinsic::rint;
4368case LibFunc_nearbyint:
4369case LibFunc_nearbyintf:
4370case LibFunc_nearbyintl:
4371return Intrinsic::nearbyint;
4375return Intrinsic::round;
4376case LibFunc_roundeven:
4377case LibFunc_roundevenf:
4378case LibFunc_roundevenl:
4379return Intrinsic::roundeven;
4383return Intrinsic::pow;
4387return Intrinsic::sqrt;
4393/// Return true if it's possible to assume IEEE treatment of input denormals in 4430// If we know there are no denormals, nothing can be flushed to zero. 4435switch (Mode.Input) {
4439// Negative subnormal won't flush to +0 4443// Both positive and negative subnormal could flush to +0 4453// If we aren't assuming the source can't be a zero, we don't have to check if 4454// a denormal input could be flushed. 4455if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4458// If we know the input can't be a denormal, it can't be flushed to 0. 4459if (Src.isKnownNeverSubnormal())
4485/// Given an exploded icmp instruction, return true if the comparison only 4486/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if 4487/// the result of the comparison is true when the input value is signed. 4489bool &TrueIfSigned) {
4496returnRHS.isAllOnes();
4498 TrueIfSigned =
false;
4499returnRHS.isAllOnes();
4501 TrueIfSigned =
false;
4504// True if LHS u> RHS and RHS == sign-bit-mask - 1 4506returnRHS.isMaxSignedValue();
4508// True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) 4510returnRHS.isMinSignedValue();
4512// True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) 4513 TrueIfSigned =
false;
4514returnRHS.isMinSignedValue();
4516// True if LHS u<= RHS and RHS == sign-bit-mask - 1 4517 TrueIfSigned =
false;
4518returnRHS.isMaxSignedValue();
4524/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the 4525/// same result as an fcmp with the given operands. 4529bool LookThroughSrc) {
4537std::pair<Value *, FPClassTest>
4539constAPFloat *ConstRHS,
bool LookThroughSrc) {
4541auto [Src, ClassIfTrue, ClassIfFalse] =
4543if (Src && ClassIfTrue == ~ClassIfFalse)
4544return {Src, ClassIfTrue};
4548/// Return the return value for fcmpImpliesClass for a compare that produces an 4549/// exact class test. 4555std::tuple<Value *, FPClassTest, FPClassTest>
4569constbool IsNegativeRHS = (RHSClass &
fcNegative) == RHSClass;
4570constbool IsPositiveRHS = (RHSClass &
fcPositive) == RHSClass;
4571constbool IsNaN = (RHSClass & ~fcNan) ==
fcNone;
4574// fcmp o__ x, nan -> false 4575// fcmp u__ x, nan -> true 4579// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan 4583// fcmp uno x, zero|normal|subnormal|inf -> fcNan 4591constbool IsZero = (OrigClass &
fcZero) == OrigClass;
4594// Compares with fcNone are only exactly equal to fcZero if input denormals 4596// TODO: Handle DAZ by expanding masks to cover subnormal cases. 4610// Canonical form of ord/uno is with a zero. We could also handle 4611// non-canonical other non-NaN constants or LHS == RHS. 4638constbool IsDenormalRHS = (OrigClass &
fcSubnormal) == OrigClass;
4640constbool IsInf = (OrigClass &
fcInf) == OrigClass;
4647// Match __builtin_isinf patterns 4649// fcmp oeq x, +inf -> is_fpclass x, fcPosInf 4650// fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf 4651// fcmp oeq x, -inf -> is_fpclass x, fcNegInf 4652// fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false 4654// fcmp une x, +inf -> is_fpclass x, ~fcPosInf 4655// fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf 4656// fcmp une x, -inf -> is_fpclass x, ~fcNegInf 4657// fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true 4671// Match __builtin_isinf patterns 4672// fcmp one x, -inf -> is_fpclass x, fcNegInf 4673// fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan 4674// fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan 4675// fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan 4677// fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan 4678// fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan 4679// fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan 4680// fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan 4682 Mask = ~fcNegInf & ~fcNan;
4686 Mask = ~fcPosInf & ~fcNan;
4696// No value is ordered and less than negative infinity. 4697// All values are unordered with or at least negative infinity. 4698// fcmp olt x, -inf -> false 4699// fcmp uge x, -inf -> true 4704// fcmp olt fabs(x), +inf -> fcFinite 4705// fcmp uge fabs(x), +inf -> ~fcFinite 4706// fcmp olt x, +inf -> fcFinite|fcNegInf 4707// fcmp uge x, +inf -> ~(fcFinite|fcNegInf) 4716// fcmp oge x, -inf -> ~fcNan 4717// fcmp oge fabs(x), -inf -> ~fcNan 4718// fcmp ult x, -inf -> fcNan 4719// fcmp ult fabs(x), -inf -> fcNan 4724// fcmp oge fabs(x), +inf -> fcInf 4725// fcmp oge x, +inf -> fcPosInf 4726// fcmp ult fabs(x), +inf -> ~fcInf 4727// fcmp ult x, +inf -> ~fcPosInf 4736// fcmp ogt x, -inf -> fcmp one x, -inf 4737// fcmp ogt fabs(x), -inf -> fcmp ord x, x 4738// fcmp ule x, -inf -> fcmp ueq x, -inf 4739// fcmp ule fabs(x), -inf -> fcmp uno x, x 4744// No value is ordered and greater than infinity. 4755// fcmp ole x, +inf -> fcmp ord x, x 4756// fcmp ole fabs(x), +inf -> fcmp ord x, x 4757// fcmp ole x, -inf -> fcmp oeq x, -inf 4758// fcmp ole fabs(x), -inf -> false 4766// Invert the comparison for the unordered cases. 4778return {Src, Class, ~fcNan};
4782return {Src, ~fcNan, RHSClass |
fcNan};
4791"should have been recognized as an exact class test");
4794// TODO: Handle fneg(fabs) 4796// fabs(x) o> -k -> fcmp ord x, x 4797// fabs(x) u> -k -> true 4798// fabs(x) o< -k -> false 4799// fabs(x) u< -k -> fcmp uno x, x 4803return {Src, ~fcNan,
fcNan};
4812return {Src,
fcNan, ~fcNan};
4831return {Src, ClassesGE, ~ClassesGE | RHSClass};
4834return {Src, ClassesGE |
fcNan, ~(ClassesGE |
fcNan) | RHSClass};
4837return {Src, ClassesLE, ~ClassesLE | RHSClass};
4840return {Src, ClassesLE |
fcNan, ~(ClassesLE |
fcNan) | RHSClass};
4844 }
elseif (IsPositiveRHS) {
4860return {Src, ClassesGE, ~ClassesGE | RHSClass};
4863return {Src, ClassesGE |
fcNan, ~(ClassesGE |
fcNan) | RHSClass};
4866return {Src, ClassesLE, ~ClassesLE | RHSClass};
4869return {Src, ClassesLE |
fcNan, ~(ClassesLE |
fcNan) | RHSClass};
4878std::tuple<Value *, FPClassTest, FPClassTest>
4880constAPFloat &ConstRHS,
bool LookThroughSrc) {
4881// We can refine checks against smallest normal / largest denormal to an 4888// Match pattern that's used in __builtin_isnormal. 4892// fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero 4893// fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero 4894// fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf 4895// fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero) 4904// fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf 4905// fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal 4906// fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf) 4907// fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal) 4918// Invert the comparison for the unordered cases. 4928std::tuple<Value *, FPClassTest, FPClassTest>
4930Value *RHS,
bool LookThroughSrc) {
4935// TODO: Just call computeKnownFPClass for RHS to handle non-constants. 4940unsignedDepth,
bool CondIsTrue,
4962 KnownFromContext.
knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4963 }
elseif (
match(
Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4966 KnownFromContext.
knownNot(CondIsTrue ? ~Mask : Mask);
4972if (TrueIfSigned == CondIsTrue)
4984return KnownFromContext;
4987// Handle dominating conditions. 4994 Q.
CxtI, KnownFromContext);
4999 Q.
CxtI, KnownFromContext);
5004return KnownFromContext;
5006// Try to restrict the floating-point classes based on information from 5014"Got assumption for the wrong function!");
5015assert(
I->getIntrinsicID() == Intrinsic::assume &&
5016"must be an assume intrinsic");
5022/*CondIsTrue=*/true, Q.
CxtI, KnownFromContext);
5025return KnownFromContext;
5035auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5042constAPInt &DemandedElts,
5046if ((InterestedClasses &
5052 KnownSrc,
Depth + 1, Q);
5054// Sign should be preserved 5055// TODO: Handle cannot be ordered greater than zero 5061// Infinity needs a range check. 5067assert(Known.
isUnknown() &&
"should not be called with known information");
5070// No demanded elts, better to assume we don't know anything. 5077if (
auto *CFP = dyn_cast<ConstantFP>(V)) {
5079 Known.
SignBit = CFP->isNegative();
5083if (isa<ConstantAggregateZero>(V)) {
5089if (isa<PoisonValue>(V)) {
5095// Try to handle fixed width vector constants 5096auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5097constConstant *CV = dyn_cast<Constant>(V);
5100bool SignBitAllZero =
true;
5101bool SignBitAllOne =
true;
5103// For vectors, verify that each element is not NaN. 5104unsigned NumElts = VFVTy->getNumElements();
5105for (
unsigned i = 0; i != NumElts; ++i) {
5106if (!DemandedElts[i])
5114if (isa<PoisonValue>(Elt))
5116auto *CElt = dyn_cast<ConstantFP>(Elt);
5122constAPFloat &
C = CElt->getValueAPF();
5125 SignBitAllZero =
false;
5127 SignBitAllOne =
false;
5129if (SignBitAllOne != SignBitAllZero)
5130 Known.
SignBit = SignBitAllOne;
5135if (
constauto *CB = dyn_cast<CallBase>(V))
5136 KnownNotFromFlags |= CB->getRetNoFPClass();
5137elseif (
constauto *Arg = dyn_cast<Argument>(V))
5138 KnownNotFromFlags |= Arg->getNoFPClass();
5142if (FPOp->hasNoNaNs())
5143 KnownNotFromFlags |=
fcNan;
5144if (FPOp->hasNoInfs())
5145 KnownNotFromFlags |=
fcInf;
5149 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5151// We no longer need to find out about these bits from inputs if we can 5152// assume this from flags/attributes. 5153 InterestedClasses &= ~KnownNotFromFlags;
5158 if (*AssumedClasses.SignBit)
5159 Known.signBitMustBeOne();
5161 Known.signBitMustBeZero();
5168// All recursive calls that increase depth must come after this. 5172constunsigned Opc =
Op->getOpcode();
5174case Instruction::FNeg: {
5176 Known,
Depth + 1, Q);
5180case Instruction::Select: {
5188Value *TestedValue =
nullptr;
5192constFunction *
F = cast<Instruction>(
Op)->getFunction();
5194Value *CmpLHS, *CmpRHS;
5196// If the select filters out a value based on the class, it no longer 5197// participates in the class of the result 5199// TODO: In some degenerate cases we can infer something if we try again 5200// without looking through sign operations. 5201bool LookThroughFAbsFNeg = CmpLHS !=
LHS && CmpLHS !=
RHS;
5202 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5205 m_Intrinsic<Intrinsic::is_fpclass>(
5208 MaskIfTrue = TestedMask;
5209 MaskIfFalse = ~TestedMask;
5212if (TestedValue ==
LHS) {
5213// match !isnan(x) ? x : y 5214 FilterLHS = MaskIfTrue;
5215 }
elseif (TestedValue ==
RHS) {
// && IsExactClass 5216// match !isnan(x) ? y : x 5217 FilterRHS = MaskIfFalse;
5226 Known2,
Depth + 1, Q);
5232case Instruction::Call: {
5236case Intrinsic::fabs: {
5238// If we only care about the sign bit we don't need to inspect the 5241 InterestedClasses, Known,
Depth + 1, Q);
5247case Intrinsic::copysign: {
5251 Known,
Depth + 1, Q);
5253 KnownSign,
Depth + 1, Q);
5258case Intrinsic::fmuladd: {
5262if (
II->getArgOperand(0) !=
II->getArgOperand(1))
5265// The multiply cannot be -0 and therefore the add can't be -0 5268// x * x + y is non-negative if y is non-negative. 5271 KnownAddend,
Depth + 1, Q);
5277case Intrinsic::sqrt:
5278case Intrinsic::experimental_constrained_sqrt: {
5281if (InterestedClasses &
fcNan)
5285 KnownSrc,
Depth + 1, Q);
5292// Any negative value besides -0 returns a nan. 5296// The only negative value that can be returned is -0 for -0 inputs. 5299// If the input denormal mode could be PreserveSign, a negative 5300// subnormal input could produce a negative zero output. 5309case Intrinsic::cos: {
5310// Return NaN on infinite inputs. 5313 KnownSrc,
Depth + 1, Q);
5319case Intrinsic::maxnum:
5320case Intrinsic::minnum:
5321case Intrinsic::minimum:
5322case Intrinsic::maximum: {
5325 KnownLHS,
Depth + 1, Q);
5327 KnownRHS,
Depth + 1, Q);
5330 Known = KnownLHS | KnownRHS;
5332// If either operand is not NaN, the result is not NaN. 5333if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5336if (IID == Intrinsic::maxnum) {
5337// If at least one operand is known to be positive, the result must be 5344 }
elseif (IID == Intrinsic::maximum) {
5345// If at least one operand is known to be positive, the result must be 5350 }
elseif (IID == Intrinsic::minnum) {
5351// If at least one operand is known to be negative, the result must be 5359// If at least one operand is known to be negative, the result must be 5366// Fixup zero handling if denormals could be returned as a zero. 5368// As there's no spec for denormal flushing, be conservative with the 5369// treatment of denormals that could be flushed to zero. For older 5370// subtargets on AMDGPU the min/max instructions would not flush the 5371// output and return the original value. 5380II->getType()->getScalarType()->getFltSemantics());
5392 }
elseif ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5397if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5400elseif ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5407case Intrinsic::canonicalize: {
5410 KnownSrc,
Depth + 1, Q);
5412// This is essentially a stronger form of 5413// propagateCanonicalizingSrc. Other "canonicalizing" operations don't 5414// actually have an IR canonicalization guarantee. 5416// Canonicalize may flush denormals to zero, so we have to consider the 5417// denormal mode to preserve known-not-0 knowledge. 5420// Stronger version of propagateNaN 5421// Canonicalize is guaranteed to quiet signaling nans. 5431// If the parent function flushes denormals, the canonical output cannot 5434II->getType()->getScalarType()->getFltSemantics();
5454case Intrinsic::vector_reduce_fmax:
5455case Intrinsic::vector_reduce_fmin:
5456case Intrinsic::vector_reduce_fmaximum:
5457case Intrinsic::vector_reduce_fminimum: {
5458// reduce min/max will choose an element from one of the vector elements, 5459// so we can infer and class information that is common to all elements. 5461 InterestedClasses,
Depth + 1, Q);
5462// Can only propagate sign if output is never NaN. 5467// reverse preserves all characteristics of the input vec's element. 5468case Intrinsic::vector_reverse:
5471II->getFastMathFlags(), InterestedClasses,
Depth + 1, Q);
5473case Intrinsic::trunc:
5474case Intrinsic::floor:
5475case Intrinsic::ceil:
5476case Intrinsic::rint:
5477case Intrinsic::nearbyint:
5478case Intrinsic::round:
5479case Intrinsic::roundeven: {
5487 KnownSrc,
Depth + 1, Q);
5489// Integer results cannot be subnormal. 5494// Pass through infinities, except PPC_FP128 is a special case for 5495// intrinsics other than trunc. 5496if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5503// Negative round ups to 0 produce -0 5512case Intrinsic::exp2:
5513case Intrinsic::exp10: {
5520 KnownSrc,
Depth + 1, Q);
5528case Intrinsic::fptrunc_round: {
5534case Intrinsic::log10:
5535case Intrinsic::log2:
5536case Intrinsic::experimental_constrained_log:
5537case Intrinsic::experimental_constrained_log10:
5538case Intrinsic::experimental_constrained_log2: {
5540// log([+-]0.0) -> -inf 5554 KnownSrc,
Depth + 1, Q);
5568case Intrinsic::powi: {
5572constValue *Exp =
II->getArgOperand(1);
5573Type *ExpTy = Exp->getType();
5577 ExponentKnownBits,
Depth + 1, Q);
5579if (ExponentKnownBits.
Zero[0]) {
// Is even 5584// Given that exp is an integer, here are the 5585// ways that pow can return a negative value: 5587// pow(-x, exp) --> negative if exp is odd and x is negative. 5588// pow(-0, exp) --> -inf if exp is negative odd. 5589// pow(-0, exp) --> -0 if exp is positive odd. 5590// pow(-inf, exp) --> -0 if exp is negative odd. 5591// pow(-inf, exp) --> -inf if exp is positive odd. 5594 KnownSrc,
Depth + 1, Q);
5599case Intrinsic::ldexp: {
5602 KnownSrc,
Depth + 1, Q);
5605// Sign is preserved, but underflows may produce zeroes. 5616// Can refine inf/zero handling based on the exponent operand. 5618if ((InterestedClasses & ExpInfoMask) ==
fcNone)
5624II->getType()->getScalarType()->getFltSemantics();
5626constValue *ExpArg =
II->getArgOperand(1);
5630constint MantissaBits = Precision - 1;
5636if (ConstVal && ConstVal->
isZero()) {
5637// ldexp(x, 0) -> x, so propagate everything. 5640// If we know the power is <= 0, can't introduce inf 5646// If we know the power is >= 0, can't introduce subnormal or zero 5659case Intrinsic::arithmetic_fence: {
5661 Known,
Depth + 1, Q);
5664case Intrinsic::experimental_constrained_sitofp:
5665case Intrinsic::experimental_constrained_uitofp:
5666// Cannot produce nan 5669// sitofp and uitofp turn into +0.0 for zero. 5672// Integers cannot be subnormal 5675if (IID == Intrinsic::experimental_constrained_uitofp)
5678// TODO: Copy inf handling from instructions 5686case Instruction::FAdd:
5687case Instruction::FSub: {
5690Op->getOpcode() == Instruction::FAdd &&
5692bool WantNaN = (InterestedClasses &
fcNan) !=
fcNone;
5695if (!WantNaN && !WantNegative && !WantNegZero)
5701if (InterestedClasses &
fcNan)
5702 InterestedSrcs |=
fcInf;
5704 KnownRHS,
Depth + 1, Q);
5708 WantNegZero || Opc == Instruction::FSub) {
5710// RHS is canonically cheaper to compute. Skip inspecting the LHS if 5713 KnownLHS,
Depth + 1, Q);
5714// Adding positive and negative infinity produces NaN. 5715// TODO: Check sign of infinities. 5720// FIXME: Context function should always be passed in separately 5721constFunction *
F = cast<Instruction>(
Op)->getFunction();
5723if (
Op->getOpcode() == Instruction::FAdd) {
5730// (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. 5733// Make sure output negative denormal can't flush to -0 5740// Only fsub -0, +0 can return -0 5743// Make sure output negative denormal can't flush to -0 5751case Instruction::FMul: {
5752// X * X is always non-negative or a NaN. 5753if (
Op->getOperand(0) ==
Op->getOperand(1))
5759// fcSubnormal is only needed in case of DAZ. 5780// If 0 * +/-inf produces NaN. 5786constFunction *
F = cast<Instruction>(
Op)->getFunction();
5798case Instruction::FDiv:
5799case Instruction::FRem: {
5800if (
Op->getOperand(0) ==
Op->getOperand(1)) {
5801// TODO: Could filter out snan if we inspect the operand 5802if (
Op->getOpcode() == Instruction::FDiv) {
5803// X / X is always exactly 1.0 or a NaN. 5806// X % X is always exactly [+-]0.0 or a NaN. 5813constbool WantNan = (InterestedClasses &
fcNan) !=
fcNone;
5815constbool WantPositive =
5817if (!WantNan && !WantNegative && !WantPositive)
5826bool KnowSomethingUseful =
5829if (KnowSomethingUseful || WantPositive) {
5835 InterestedClasses & InterestedLHS, KnownLHS,
5839constFunction *
F = cast<Instruction>(
Op)->getFunction();
5841if (
Op->getOpcode() == Instruction::FDiv) {
5842// Only 0/0, Inf/Inf produce NaN. 5851// X / -0.0 is -Inf (or NaN). 5856// Inf REM x and x REM 0 produce NaN. 5863// The sign for frem is the same as the first operand. 5869// See if we can be more aggressive about the sign of 0. 5878case Instruction::FPExt: {
5879// Infinity, nan and zero propagate from source. 5881 Known,
Depth + 1, Q);
5884Op->getType()->getScalarType()->getFltSemantics();
5886Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5888// All subnormal inputs should be in the normal range in the result type. 5897// Sign bit of a nan isn't guaranteed. 5902case Instruction::FPTrunc: {
5907case Instruction::SIToFP:
5908case Instruction::UIToFP: {
5909// Cannot produce nan 5912// Integers cannot be subnormal 5915// sitofp and uitofp turn into +0.0 for zero. 5917if (
Op->getOpcode() == Instruction::UIToFP)
5920if (InterestedClasses &
fcInf) {
5921// Get width of largest magnitude integer (remove a bit if signed). 5922// This still works for a signed minimum value because the largest FP 5923// value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx). 5924int IntSize =
Op->getOperand(0)->getType()->getScalarSizeInBits();
5925if (
Op->getOpcode() == Instruction::SIToFP)
5928// If the exponent of the largest finite FP value can hold the largest 5929// integer, the result of the cast must be finite. 5930Type *FPTy =
Op->getType()->getScalarType();
5937case Instruction::ExtractElement: {
5938// Look through extract element. If the index is non-constant or 5939// out-of-range demand all elements, otherwise just the extracted element. 5940constValue *Vec =
Op->getOperand(0);
5942auto *CIdx = dyn_cast<ConstantInt>(
Idx);
5944if (
auto *VecTy = dyn_cast<FixedVectorType>(Vec->
getType())) {
5945unsigned NumElts = VecTy->getNumElements();
5947if (CIdx && CIdx->getValue().ult(NumElts))
5955case Instruction::InsertElement: {
5956if (isa<ScalableVectorType>(
Op->getType()))
5959constValue *Vec =
Op->getOperand(0);
5960constValue *Elt =
Op->getOperand(1);
5961auto *CIdx = dyn_cast<ConstantInt>(
Op->getOperand(2));
5963APInt DemandedVecElts = DemandedElts;
5965// If we know the index we are inserting to, clear it from Vec check. 5966if (CIdx && CIdx->getValue().ult(NumElts)) {
5967 DemandedVecElts.
clearBit(CIdx->getZExtValue());
5968 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5971// Do we demand the inserted element? 5974// If we don't know any bits, early out. 5981// Do we need anymore elements from Vec? 5982if (!DemandedVecElts.
isZero()) {
5991case Instruction::ShuffleVector: {
5992// For undef elements, we don't know anything about the common state of 5993// the shuffle result. 5994APInt DemandedLHS, DemandedRHS;
5995auto *Shuf = dyn_cast<ShuffleVectorInst>(
Op);
6000constValue *
LHS = Shuf->getOperand(0);
6004// If we don't know any bits, early out. 6013constValue *
RHS = Shuf->getOperand(1);
6021case Instruction::ExtractValue: {
6025if (isa<StructType>(Src->getType()) && Indices.
size() == 1 &&
6027if (
constauto *
II = dyn_cast<IntrinsicInst>(Src)) {
6028switch (
II->getIntrinsicID()) {
6029case Intrinsic::frexp: {
6034 InterestedClasses, KnownSrc,
Depth + 1, Q);
6036constFunction *
F = cast<Instruction>(
Op)->getFunction();
6069case Instruction::PHI: {
6071// Unreachable blocks may have zero-operand PHI nodes. 6072if (
P->getNumIncomingValues() == 0)
6075// Otherwise take the unions of the known bit sets of the operands, 6076// taking conservative care to avoid excessive recursion. 6079if (
Depth < PhiRecursionLimit) {
6080// Skip if every incoming value references to ourself. 6081if (isa_and_nonnull<UndefValue>(
P->hasConstantValue()))
6086for (
constUse &U :
P->operands()) {
6090// Skip direct self references. 6095// Recurse, but cap the recursion to two levels, because we don't want 6096// to waste time spinning around in loops. We need at least depth 2 to 6097// detect known sign bits. 6116case Instruction::BitCast: {
6119 !Src->getType()->isIntOrIntVectorTy())
6122constType *Ty =
Op->getType()->getScalarType();
6126// Transfer information from the sign bit. 6127if (Bits.isNonNegative())
6129elseif (Bits.isNegative())
6133// IEEE floats are NaN when all bits of the exponent plus at least one of 6134// the fraction bits are 1. This means: 6135// - If we assume unknown bits are 0 and the value is NaN, it will 6137// - If we assume unknown bits are 1 and the value is not NaN, it can 6144// Build KnownBits representing Inf and check if it must be equal or 6145// unequal to this value. 6148 InfKB.Zero.clearSignBit();
6150assert(!InfResult.value());
6152 }
elseif (Bits == InfKB) {
6156// Build KnownBits representing Zero and check if it must be equal or 6157// unequal to this value. 6160 ZeroKB.Zero.clearSignBit();
6162assert(!ZeroResult.value());
6164 }
elseif (Bits == ZeroKB) {
6177constAPInt &DemandedElts,
6198// All byte-wide stores are splatable, even of arbitrary variables. 6199if (V->getType()->isIntegerTy(8))
6206if (isa<UndefValue>(V))
6209// Return poison for zero-sized type. 6210if (
DL.getTypeStoreSize(V->getType()).isZero())
6215// Conceptually, we could handle things like: 6216// %a = zext i8 %X to i16 6217// %b = shl i16 %a, 8 6218// %c = or i16 %a, %b 6219// but until there is an example that actually needs this, it doesn't seem 6220// worth worrying about. 6224// Handle 'null' ConstantArrayZero etc. 6225if (
C->isNullValue())
6228// Constant floating-point values can be handled as integer values if the 6229// corresponding integer value is "byteable". An important case is 0.0. 6232if (CFP->getType()->isHalfTy())
6234elseif (CFP->getType()->isFloatTy())
6236elseif (CFP->getType()->isDoubleTy())
6238// Don't handle long double formats, which have strange constraints. 6243// We can handle constant integers that are multiple of 8 bits. 6245if (CI->getBitWidth() % 8 == 0) {
6246assert(CI->getBitWidth() > 8 &&
"8 bits should be handled above!");
6247if (!CI->getValue().isSplat(8))
6249return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6253if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
6254if (CE->getOpcode() == Instruction::IntToPtr) {
6255if (
auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6256unsignedBitWidth =
DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6269if (
LHS == UndefInt8)
6271if (
RHS == UndefInt8)
6277Value *Val = UndefInt8;
6278for (
unsignedI = 0, E = CA->getNumElements();
I != E; ++
I)
6284if (isa<ConstantAggregate>(
C)) {
6285Value *Val = UndefInt8;
6292// Don't try to handle the handful of other constants. 6296// This is the recursive version of BuildSubAggregate. It takes a few different 6297// arguments. Idxs is the index within the nested struct From that we are 6298// looking at now (which is of type IndexedType). IdxSkip is the number of 6299// indices from Idxs that should be left out when inserting into the resulting 6300// struct. To is the result struct built so far, new insertvalue instructions 6306StructType *STy = dyn_cast<StructType>(IndexedType);
6308// Save the original To argument so we can modify it 6310// General case, the type indexed by Idxs is a struct 6312// Process each struct element recursively 6319// Couldn't find any inserted value for this index? Cleanup 6320while (PrevTo != OrigTo) {
6325// Stop processing elements 6329// If we successfully found a value for each of our subaggregates 6333// Base case, the type indexed by SourceIdxs is not a struct, or not all of 6334// the struct's elements had a value that was inserted directly. In the latter 6335// case, perhaps we can't determine each of the subelements individually, but 6336// we might be able to find the complete struct somewhere. 6338// Find the value that is at that particular spot 6344// Insert the value in the new (sub) aggregate 6349// This helper takes a nested struct and extracts a part of it (which is again a 6350// struct) into a new value. For example, given the struct: 6351// { a, { b, { c, d }, e } } 6352// and the indices "1, 1" this returns 6355// It does this by inserting an insertvalue for each element in the resulting 6356// struct, as opposed to just inserting a single struct. This will only work if 6357// each of the elements of the substruct are known (ie, inserted into From by an 6358// insertvalue instruction somewhere). 6360// All inserted insertvalue instructions are inserted before InsertBefore 6367unsigned IdxSkip = Idxs.
size();
6372/// Given an aggregate and a sequence of indices, see if the scalar value 6373/// indexed is already around as a register, for example if it was inserted 6374/// directly into the aggregate. 6376/// If InsertBefore is not null, this function will duplicate (modified) 6377/// insertvalues when a part of a nested struct is extracted. 6380 std::optional<BasicBlock::iterator> InsertBefore) {
6381// Nothing to index? Just return V then (this is useful at the end of our 6383if (idx_range.
empty())
6385// We have indices, so V should have an indexable type. 6386assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6387"Not looking at a struct or array?");
6389"Invalid indices for type?");
6391if (
Constant *
C = dyn_cast<Constant>(V)) {
6392C =
C->getAggregateElement(idx_range[0]);
6393if (!
C)
returnnullptr;
6398// Loop the indices for the insertvalue instruction in parallel with the 6400constunsigned *req_idx = idx_range.
begin();
6401for (
constunsigned *i =
I->idx_begin(), *e =
I->idx_end();
6402 i != e; ++i, ++req_idx) {
6403if (req_idx == idx_range.
end()) {
6404// We can't handle this without inserting insertvalues 6408// The requested index identifies a part of a nested aggregate. Handle 6409// this specially. For example, 6410// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 6411// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 6412// %C = extractvalue {i32, { i32, i32 } } %B, 1 6413// This can be changed into 6414// %A = insertvalue {i32, i32 } undef, i32 10, 0 6415// %C = insertvalue {i32, i32 } %A, i32 11, 1 6416// which allows the unused 0,0 element from the nested struct to be 6422// This insert value inserts something else than what we are looking for. 6423// See if the (aggregate) value inserted into has the value we are 6424// looking for, then. 6429// If we end up here, the indices of the insertvalue match with those 6430// requested (though possibly only partially). Now we recursively look at 6431// the inserted value, passing any remaining indices. 6437// If we're extracting a value from an aggregate that was extracted from 6438// something else, we can extract from that something else directly instead. 6439// However, we will need to chain I's indices with the requested indices. 6441// Calculate the number of indices required 6442unsignedsize =
I->getNumIndices() + idx_range.
size();
6443// Allocate some space to put the new indices in 6446// Add indices from the extract value instruction 6447 Idxs.
append(
I->idx_begin(),
I->idx_end());
6449// Add requested indices 6453 &&
"Number of indices added not correct?");
6457// Otherwise, we don't know (such as, extracting from a function return value 6458// or load instruction) 6464// Make sure the GEP has exactly three arguments. 6465if (
GEP->getNumOperands() != 3)
6468// Make sure the index-ee is a pointer to array of \p CharSize integers. 6470ArrayType *AT = dyn_cast<ArrayType>(
GEP->getSourceElementType());
6474// Check to make sure that the first operand of the GEP is an integer and 6475// has value 0 so that we are sure we're indexing into the initializer. 6476constConstantInt *FirstIdx = dyn_cast<ConstantInt>(
GEP->getOperand(1));
6477if (!FirstIdx || !FirstIdx->
isZero())
6483// If V refers to an initialized global constant, set Slice either to 6484// its initializer if the size of its elements equals ElementSize, or, 6485// for ElementSize == 8, to its representation as an array of unsiged 6486// char. Return true on success. 6487// Offset is in the unit "nr of ElementSize sized elements". 6491assert(V &&
"V should not be null.");
6492assert((ElementSize % 8) == 0 &&
6493"ElementSize expected to be a multiple of the size of a byte.");
6494unsigned ElementSizeInBytes = ElementSize / 8;
6496// Drill down into the pointer expression V, ignoring any intervening 6497// casts, and determine the identity of the object it references along 6498// with the cumulative byte offset into it. 6502// Fail if V is not based on constant global object. 6506APInt Off(
DL.getIndexTypeSizeInBits(V->getType()), 0);
6508if (GV != V->stripAndAccumulateConstantOffsets(
DL, Off,
6509/*AllowNonInbounds*/true))
6510// Fail if a constant offset could not be determined. 6513uint64_t StartIdx = Off.getLimitedValue();
6515// Fail if the constant offset is excessive. 6518// Off/StartIdx is in the unit of bytes. So we need to convert to number of 6519// elements. Simply bail out if that isn't possible. 6520if ((StartIdx % ElementSizeInBytes) != 0)
6523Offset += StartIdx / ElementSizeInBytes;
6529uint64_t SizeInBytes =
DL.getTypeStoreSize(GVTy).getFixedValue();
6532 Slice.
Array =
nullptr;
6534// Return an empty Slice for undersized constants to let callers 6535// transform even undefined library calls into simpler, well-defined 6536// expressions. This is preferable to making the calls although it 6537// prevents sanitizers from detecting such calls. 6543if (
auto *ArrayInit = dyn_cast<ConstantDataArray>(
Init)) {
6544Type *InitElTy = ArrayInit->getElementType();
6546// If Init is an initializer for an array of the expected type 6547// and size, use it as is. 6549 ArrayTy = ArrayInit->getType();
6554if (ElementSize != 8)
6555// TODO: Handle conversions to larger integral types. 6558// Otherwise extract the portion of the initializer starting 6559// at Offset as an array of bytes, and reset Offset. 6565 Array = dyn_cast<ConstantDataArray>(
Init);
6566 ArrayTy = dyn_cast<ArrayType>(
Init->getType());
6573 Slice.
Array = Array;
6579/// Extract bytes from the initializer of the constant array V, which need 6580/// not be a nul-terminated string. On success, store the bytes in Str and 6581/// return true. When TrimAtNul is set, Str will contain only the bytes up 6582/// to but not including the first nul. Return false on failure. 6589if (Slice.
Array ==
nullptr) {
6591// Return a nul-terminated string even for an empty Slice. This is 6592// safe because all existing SimplifyLibcalls callers require string 6593// arguments and the behavior of the functions they fold is undefined 6594// otherwise. Folding the calls this way is preferable to making 6595// the undefined library calls, even though it prevents sanitizers 6596// from reporting such calls. 6604// We cannot instantiate a StringRef as we do not have an appropriate string 6609// Start out with the entire array in the StringRef. 6611// Skip over 'offset' bytes. 6612 Str = Str.substr(Slice.
Offset);
6615// Trim off the \0 and anything after it. If the array is not nul 6616// terminated, we just return the whole end of string. The client may know 6617// some other way that the string is length-bound. 6618 Str = Str.substr(0, Str.find(
'\0'));
6623// These next two are very similar to the above, but also look through PHI 6625// TODO: See if we can integrate these two together. 6627/// If we can compute the length of the string pointed to by 6628/// the specified pointer, return 'len+1'. If we can't, return 0. 6632// Look through noop bitcast instructions. 6633 V = V->stripPointerCasts();
6635// If this is a PHI node, there are two cases: either we have already seen it 6637if (
constPHINode *PN = dyn_cast<PHINode>(V)) {
6638if (!PHIs.
insert(PN).second)
6639return ~0ULL;
// already in the set. 6641// If it was new, see if all the input strings are the same length. 6643for (
Value *IncValue : PN->incoming_values()) {
6645if (Len == 0)
return 0;
// Unknown length -> unknown. 6647if (Len == ~0ULL)
continue;
6649if (Len != LenSoFar && LenSoFar != ~0ULL)
6650return 0;
// Disagree -> unknown. 6654// Success, all agree. 6658// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) 6659if (
constSelectInst *SI = dyn_cast<SelectInst>(V)) {
6661if (Len1 == 0)
return 0;
6663if (Len2 == 0)
return 0;
6664if (Len1 == ~0ULL)
return Len2;
6665if (Len2 == ~0ULL)
return Len1;
6666if (Len1 != Len2)
return 0;
6670// Otherwise, see if we can read the string. 6675if (Slice.
Array ==
nullptr)
6676// Zeroinitializer (including an empty one). 6679// Search for the first nul character. Return a conservative result even 6680// when there is no nul. This is safe since otherwise the string function 6681// being folded such as strlen is undefined, and can be preferable to 6682// making the undefined library call. 6683unsigned NullIndex = 0;
6684for (
unsigned E = Slice.
Length; NullIndex < E; ++NullIndex) {
6689return NullIndex + 1;
6692/// If we can compute the length of the string pointed to by 6693/// the specified pointer, return 'len+1'. If we can't, return 0. 6695if (!V->getType()->isPointerTy())
6700// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return 6701// an empty string as a length. 6702return Len == ~0ULL ? 1 : Len;
6707bool MustPreserveNullness) {
6709"getArgumentAliasingToReturnedPointer only works on nonnull calls");
6710if (
constValue *RV = Call->getReturnedArgOperand())
6712// This can be used only as a aliasing property. 6714 Call, MustPreserveNullness))
6715return Call->getArgOperand(0);
6720constCallBase *Call,
bool MustPreserveNullness) {
6721switch (Call->getIntrinsicID()) {
6722case Intrinsic::launder_invariant_group:
6723case Intrinsic::strip_invariant_group:
6724case Intrinsic::aarch64_irg:
6725case Intrinsic::aarch64_tagp:
6726// The amdgcn_make_buffer_rsrc function does not alter the address of the 6727// input pointer (and thus preserve null-ness for the purposes of escape 6728// analysis, which is where the MustPreserveNullness flag comes in to play). 6729// However, it will not necessarily map ptr addrspace(N) null to ptr 6730// addrspace(8) null, aka the "null descriptor", which has "all loads return 6731// 0, all stores are dropped" semantics. Given the context of this intrinsic 6732// list, no one should be relying on such a strict interpretation of 6733// MustPreserveNullness (and, at time of writing, they are not), but we 6734// document this fact out of an abundance of caution. 6735case Intrinsic::amdgcn_make_buffer_rsrc:
6737case Intrinsic::ptrmask:
6738return !MustPreserveNullness;
6739case Intrinsic::threadlocal_address:
6740// The underlying variable changes with thread ID. The Thread ID may change 6741// at coroutine suspend points. 6742return !Call->getParent()->getParent()->isPresplitCoroutine();
6748/// \p PN defines a loop-variant pointer to an object. Check if the 6749/// previous iteration of the loop was referring to the same object as \p PN. 6752// Find the loop-defined value. 6757// Find the value from previous iteration. 6759if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
6761if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
6764// If a new pointer is loaded in the loop, the pointer references a different 6765// object in every iteration. E.g.: 6769if (
auto *Load = dyn_cast<LoadInst>(PrevValue))
6770if (!L->isLoopInvariant(Load->getPointerOperand()))
6776for (
unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6777if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
6778constValue *PtrOp =
GEP->getPointerOperand();
6784Value *NewV = cast<Operator>(V)->getOperand(0);
6788 }
elseif (
auto *GA = dyn_cast<GlobalAlias>(V)) {
6789if (GA->isInterposable())
6791 V = GA->getAliasee();
6793if (
auto *
PHI = dyn_cast<PHINode>(V)) {
6794// Look through single-arg phi nodes created by LCSSA. 6795if (
PHI->getNumIncomingValues() == 1) {
6796 V =
PHI->getIncomingValue(0);
6799 }
elseif (
auto *Call = dyn_cast<CallBase>(V)) {
6800// CaptureTracking can know about special capturing properties of some 6801// intrinsics like launder.invariant.group, that can't be expressed with 6802// the attributes, but have properties like returning aliasing pointer. 6803// Because some analysis may assume that nocaptured pointer is not 6804// returned from some special intrinsic (because function would have to 6805// be marked with returns attribute), it is crucial to use this function 6806// because it should be in sync with CaptureTracking. Not using it may 6807// cause weird miscompilations where 2 aliasing pointers are assumed to 6817assert(V->getType()->isPointerTy() &&
"Unexpected operand type!");
6824constLoopInfo *LI,
unsigned MaxLookup) {
6835if (
auto *SI = dyn_cast<SelectInst>(
P)) {
6837 Worklist.
push_back(SI->getFalseValue());
6841if (
auto *PN = dyn_cast<PHINode>(
P)) {
6842// If this PHI changes the underlying object in every iteration of the 6843// loop, don't look through it. Consider: 6846// Prev = Curr; // Prev = PHI (Prev_0, Curr) 6850// Prev is tracking Curr one iteration behind so they refer to different 6851// underlying objects. 6861 }
while (!Worklist.
empty());
6865constunsigned MaxVisited = 8;
6870constValue *Object =
nullptr;
6871// Used as fallback if we can't find a common underlying object through 6883if (Visited.
size() == MaxVisited)
6886if (
auto *SI = dyn_cast<SelectInst>(
P)) {
6888 Worklist.
push_back(SI->getFalseValue());
6892if (
auto *PN = dyn_cast<PHINode>(
P)) {
6901 }
while (!Worklist.
empty());
6903return Object ? Object : FirstObject;
6906/// This is the function that does the work of looking through basic 6907/// ptrtoint+arithmetic+inttoptr sequences. 6910if (
constOperator *U = dyn_cast<Operator>(V)) {
6911// If we find a ptrtoint, we can transfer control back to the 6912// regular getUnderlyingObjectFromInt. 6913if (U->getOpcode() == Instruction::PtrToInt)
6914return U->getOperand(0);
6915// If we find an add of a constant, a multiplied value, or a phi, it's 6916// likely that the other operand will lead us to the base 6917// object. We don't have to worry about the case where the 6918// object address is somehow being computed by the multiply, 6919// because our callers only care when the result is an 6920// identifiable object. 6921if (U->getOpcode() != Instruction::Add ||
6922 (!isa<ConstantInt>(U->getOperand(1)) &&
6924 !isa<PHINode>(U->getOperand(1))))
6926 V = U->getOperand(0);
6930assert(V->getType()->isIntegerTy() &&
"Unexpected operand type!");
6934/// This is a wrapper around getUnderlyingObjects and adds support for basic 6935/// ptrtoint+arithmetic+inttoptr sequences. 6936/// It returns false if unidentified object is found in getUnderlyingObjects. 6947for (
constValue *V : Objs) {
6948if (!Visited.
insert(V).second)
6953if (O->getType()->isPointerTy()) {
6958// If getUnderlyingObjects fails to find an identifiable object, 6959// getUnderlyingObjectsForCodeGen also fails for safety. 6966 }
while (!Working.
empty());
6975auto AddWork = [&](
Value *V) {
6976if (Visited.
insert(V).second)
6985if (
AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6986if (Result && Result != AI)
6989 }
elseif (
CastInst *CI = dyn_cast<CastInst>(V)) {
6990 AddWork(CI->getOperand(0));
6991 }
elseif (
PHINode *PN = dyn_cast<PHINode>(V)) {
6992for (
Value *IncValue : PN->incoming_values())
6994 }
elseif (
auto *SI = dyn_cast<SelectInst>(V)) {
6995 AddWork(SI->getTrueValue());
6996 AddWork(SI->getFalseValue());
6998if (OffsetZero && !
GEP->hasAllZeroIndices())
7000 AddWork(
GEP->getPointerOperand());
7001 }
elseif (
CallBase *CB = dyn_cast<CallBase>(V)) {
7002Value *Returned = CB->getReturnedArgOperand();
7010 }
while (!Worklist.
empty());
7016constValue *V,
bool AllowLifetime,
bool AllowDroppable) {
7017for (
constUser *U : V->users()) {
7022if (AllowLifetime &&
II->isLifetimeStartOrEnd())
7025if (AllowDroppable &&
II->isDroppable())
7035 V,
/* AllowLifetime */true,
/* AllowDroppable */false);
7039 V,
/* AllowLifetime */true,
/* AllowDroppable */true);
7043if (
auto *
II = dyn_cast<IntrinsicInst>(
I))
7045auto *Shuffle = dyn_cast<ShuffleVectorInst>(
I);
7046return (!Shuffle || Shuffle->isSelect()) &&
7047 !isa<CallBase, BitCastInst, ExtractElementInst>(
I);
7055bool UseVariableInfo) {
7057 AC, DT, TLI, UseVariableInfo);
7063bool UseVariableInfo) {
7066// Check that the operands are actually compatible with the Opcode override. 7067auto hasEqualReturnAndLeadingOperandTypes =
7068 [](
constInstruction *Inst,
unsigned NumLeadingOperands) {
7072for (
unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7078 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7080 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7087case Instruction::UDiv:
7088case Instruction::URem: {
7089// x / y is undefined if y == 0. 7095case Instruction::SDiv:
7096case Instruction::SRem: {
7097// x / y is undefined if y == 0 or x == INT_MIN and y == -1 7098constAPInt *Numerator, *Denominator;
7101// We cannot hoist this division if the denominator is 0. 7102if (*Denominator == 0)
7104// It's safe to hoist if the denominator is not 0 or -1. 7107// At this point we know that the denominator is -1. It is safe to hoist as 7108// long we know that the numerator is not INT_MIN. 7111// The numerator *might* be MinSignedValue. 7114case Instruction::Load: {
7115if (!UseVariableInfo)
7118constLoadInst *LI = dyn_cast<LoadInst>(Inst);
7128case Instruction::Call: {
7129auto *CI = dyn_cast<const CallInst>(Inst);
7132constFunction *Callee = CI->getCalledFunction();
7134// The called function could have undefined behavior or side-effects, even 7135// if marked readnone nounwind. 7136return Callee && Callee->isSpeculatable();
7138case Instruction::VAArg:
7139case Instruction::Alloca:
7140case Instruction::Invoke:
7141case Instruction::CallBr:
7142case Instruction::PHI:
7143case Instruction::Store:
7144case Instruction::Ret:
7145case Instruction::Br:
7146case Instruction::IndirectBr:
7147case Instruction::Switch:
7148case Instruction::Unreachable:
7149case Instruction::Fence:
7150case Instruction::AtomicRMW:
7151case Instruction::AtomicCmpXchg:
7152case Instruction::LandingPad:
7153case Instruction::Resume:
7154case Instruction::CatchSwitch:
7155case Instruction::CatchPad:
7156case Instruction::CatchRet:
7157case Instruction::CleanupPad:
7158case Instruction::CleanupRet:
7159returnfalse;
// Misc instructions which have effects 7164if (
I.mayReadOrWriteMemory())
7165// Memory dependency possible 7168// Can't move above a maythrow call or infinite loop. Or if an 7169// inalloca alloca, above a stacksave call. 7172// 1) Can't reorder two inf-loop calls, even if readonly 7173// 2) Also can't reorder an inf-loop call below a instruction which isn't 7174// safe to speculative execute. (Inverse of above) 7179/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. 7194/// Combine constant ranges from computeConstantRange() and computeKnownBits(). 7214// mul nsw of two non-negative numbers is also nuw. 7226// Multiplying n * m significant bits yields a result of n + m significant 7227// bits. If the total number of significant bits does not exceed the 7228// result bit width (minus 1), there is no overflow. 7229// This means if we have enough leading sign bits in the operands 7230// we can guarantee that the result does not overflow. 7231// Ref: "Hacker's Delight" by Henry Warren 7234// Note that underestimating the number of sign bits gives a more 7235// conservative answer. 7239// First handle the easy case: if we have enough sign bits there's 7240// definitely no overflow. 7244// There are two ambiguous cases where there can be no overflow: 7245// SignBits == BitWidth + 1 and 7246// SignBits == BitWidth 7247// The second case is difficult to check, therefore we only handle the 7250// It overflows only when both arguments are negative and the true 7251// product is exactly the minimum negative number. 7252// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 7253// For simplicity we just check if at least one side is not negative. 7277if (
Add &&
Add->hasNoSignedWrap()) {
7281// If LHS and RHS each have at least two sign bits, the addition will look 7287// If the carry into the most significant position is 0, X and Y can't both 7288// be 1 and therefore the carry out of the addition is also 0. 7290// If the carry into the most significant position is 1, X and Y can't both 7291// be 0 and therefore the carry out of the addition is also 1. 7293// Since the carry into the most significant position is always equal to 7294// the carry out of the addition, there is no signed overflow. 7308// The remaining code needs Add to be available. Early returns if not so. 7312// If the sign of Add is the same as at least one of the operands, this add 7313// CANNOT overflow. If this can be determined from the known bits of the 7314// operands the above signedAddMayOverflow() check will have already done so. 7315// The only other way to improve on the known bits is from an assumption, so 7316// call computeKnownBitsFromContext() directly. 7317bool LHSOrRHSKnownNonNegative =
7319bool LHSOrRHSKnownNegative =
7321if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7324if ((AddKnown.
isNonNegative() && LHSOrRHSKnownNonNegative) ||
7325 (AddKnown.
isNegative() && LHSOrRHSKnownNegative))
7336// The remainder of a value can't have greater magnitude than itself, 7337// so the subtraction can't overflow. 7340// In the minimal case, this would simplify to "?", so there's no subtract 7341// at all. But if this analysis is used to peek through casts, for example, 7342// then determining no-overflow may allow other transforms. 7344// TODO: There are other patterns like this. 7345// See simplifyICmpWithBinOpOnLHS() for candidates. 7369// The remainder of a value can't have greater magnitude than itself, 7370// so the subtraction can't overflow. 7373// In the minimal case, this would simplify to "?", so there's no subtract 7374// at all. But if this analysis is used to peek through casts, for example, 7375// then determining no-overflow may allow other transforms. 7381// If LHS and RHS each have at least two sign bits, the subtraction 7400if (
constauto *EVI = dyn_cast<ExtractValueInst>(U)) {
7401assert(EVI->getNumIndices() == 1 &&
"Obvious from CI's type");
7403if (EVI->getIndices()[0] == 0)
7406assert(EVI->getIndices()[0] == 1 &&
"Obvious from CI's type");
7408for (
constauto *U : EVI->users())
7409if (
constauto *
B = dyn_cast<BranchInst>(U)) {
7410assert(
B->isConditional() &&
"How else is it using an i1?");
7415// We are using the aggregate directly in a way we don't want to analyze 7416// here (storing it to a global, say). 7421auto AllUsesGuardedByBranch = [&](
constBranchInst *BI) {
7426// Check if all users of the add are provably no-wrap. 7427for (
constauto *Result :
Results) {
7428// If the extractvalue itself is not executed on overflow, the we don't 7429// need to check each use separately, since domination is transitive. 7430if (DT.
dominates(NoWrapEdge, Result->getParent()))
7433for (
constauto &RU : Result->uses())
7441returnllvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7444/// Shifts return poison if shiftwidth is larger than the bitwidth. 7446auto *
C = dyn_cast<Constant>(ShiftAmount);
7450// Shifts return poison if shiftwidth is larger than the bitwidth. 7452if (
auto *FVTy = dyn_cast<FixedVectorType>(
C->getType())) {
7453unsigned NumElts = FVTy->getNumElements();
7454for (
unsigned i = 0; i < NumElts; ++i)
7455 ShiftAmounts.
push_back(
C->getAggregateElement(i));
7456 }
elseif (isa<ScalableVectorType>(
C->getType()))
7457returnfalse;
// Can't tell, just return false to be safe 7462auto *CI = dyn_cast_or_null<ConstantInt>(
C);
7463return CI && CI->getValue().ult(
C->getType()->getIntegerBitWidth());
7476return (
unsigned(Kind) &
unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7480return (
unsigned(Kind) &
unsigned(UndefPoisonKind::UndefOnly)) != 0;
7484bool ConsiderFlagsAndMetadata) {
7487Op->hasPoisonGeneratingAnnotations())
7490unsigned Opcode =
Op->getOpcode();
7492// Check whether opcode is a poison/undef-generating operation 7494case Instruction::Shl:
7495case Instruction::AShr:
7496case Instruction::LShr:
7498case Instruction::FPToSI:
7499case Instruction::FPToUI:
7500// fptosi/ui yields poison if the resulting value does not fit in the 7503case Instruction::Call:
7504if (
auto *
II = dyn_cast<IntrinsicInst>(
Op)) {
7505switch (
II->getIntrinsicID()) {
7506// TODO: Add more intrinsics. 7507case Intrinsic::ctlz:
7508case Intrinsic::cttz:
7510if (cast<ConstantInt>(
II->getArgOperand(1))->isNullValue())
7513case Intrinsic::ctpop:
7514case Intrinsic::bswap:
7515case Intrinsic::bitreverse:
7516case Intrinsic::fshl:
7517case Intrinsic::fshr:
7518case Intrinsic::smax:
7519case Intrinsic::smin:
7520case Intrinsic::umax:
7521case Intrinsic::umin:
7522case Intrinsic::ptrmask:
7523case Intrinsic::fptoui_sat:
7524case Intrinsic::fptosi_sat:
7525case Intrinsic::sadd_with_overflow:
7526case Intrinsic::ssub_with_overflow:
7527case Intrinsic::smul_with_overflow:
7528case Intrinsic::uadd_with_overflow:
7529case Intrinsic::usub_with_overflow:
7530case Intrinsic::umul_with_overflow:
7531case Intrinsic::sadd_sat:
7532case Intrinsic::uadd_sat:
7533case Intrinsic::ssub_sat:
7534case Intrinsic::usub_sat:
7536case Intrinsic::sshl_sat:
7537case Intrinsic::ushl_sat:
7541case Intrinsic::fmuladd:
7542case Intrinsic::sqrt:
7543case Intrinsic::powi:
7548case Intrinsic::log10:
7549case Intrinsic::log2:
7551case Intrinsic::exp2:
7552case Intrinsic::exp10:
7553case Intrinsic::fabs:
7554case Intrinsic::copysign:
7555case Intrinsic::floor:
7556case Intrinsic::ceil:
7557case Intrinsic::trunc:
7558case Intrinsic::rint:
7559case Intrinsic::nearbyint:
7560case Intrinsic::round:
7561case Intrinsic::roundeven:
7562case Intrinsic::fptrunc_round:
7563case Intrinsic::canonicalize:
7564case Intrinsic::arithmetic_fence:
7565case Intrinsic::minnum:
7566case Intrinsic::maxnum:
7567case Intrinsic::minimum:
7568case Intrinsic::maximum:
7569case Intrinsic::is_fpclass:
7570case Intrinsic::ldexp:
7571case Intrinsic::frexp:
7573case Intrinsic::lround:
7574case Intrinsic::llround:
7575case Intrinsic::lrint:
7576case Intrinsic::llrint:
7577// If the value doesn't fit an unspecified value is returned (but this 7583case Instruction::CallBr:
7584case Instruction::Invoke: {
7585constauto *CB = cast<CallBase>(
Op);
7586return !CB->hasRetAttr(Attribute::NoUndef);
7588case Instruction::InsertElement:
7589case Instruction::ExtractElement: {
7590// If index exceeds the length of the vector, it returns poison 7591auto *VTy = cast<VectorType>(
Op->getOperand(0)->getType());
7592unsigned IdxOp =
Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7593auto *
Idx = dyn_cast<ConstantInt>(
Op->getOperand(IdxOp));
7596Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7599case Instruction::ShuffleVector: {
7601 ? cast<ConstantExpr>(
Op)->getShuffleMask()
7602 : cast<ShuffleVectorInst>(
Op)->getShuffleMask();
7605case Instruction::FNeg:
7606case Instruction::PHI:
7607case Instruction::Select:
7608case Instruction::URem:
7609case Instruction::SRem:
7610case Instruction::ExtractValue:
7611case Instruction::InsertValue:
7612case Instruction::Freeze:
7613case Instruction::ICmp:
7614case Instruction::FCmp:
7615case Instruction::FAdd:
7616case Instruction::FSub:
7617case Instruction::FMul:
7618case Instruction::FDiv:
7619case Instruction::FRem:
7621case Instruction::GetElementPtr:
7622// inbounds is handled above 7623// TODO: what about inrange on constexpr? 7626constauto *CE = dyn_cast<ConstantExpr>(
Op);
7627if (isa<CastInst>(
Op) || (CE && CE->isCast()))
7631// Be conservative and return true. 7638bool ConsiderFlagsAndMetadata) {
7639 return ::canCreateUndefOrPoison(
Op, UndefPoisonKind::UndefOrPoison,
7640 ConsiderFlagsAndMetadata);
7644 return ::canCreateUndefOrPoison(
Op, UndefPoisonKind::PoisonOnly,
7645 ConsiderFlagsAndMetadata);
7650if (ValAssumedPoison == V)
7653constunsigned MaxDepth = 2;
7654if (
Depth >= MaxDepth)
7657if (
constauto *
I = dyn_cast<Instruction>(V)) {
7659 return propagatesPoison(Op) &&
7660 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7664// V = extractvalue V0, idx 7665// V2 = extractvalue V0, idx2 7666// V0's elements are all poison or not. (e.g., add_with_overflow) 7684constunsigned MaxDepth = 2;
7685if (
Depth >= MaxDepth)
7688constauto *
I = dyn_cast<Instruction>(ValAssumedPoison);
7691 return impliesPoison(Op, V, Depth + 1);
7698 return ::impliesPoison(ValAssumedPoison, V,
/* Depth */ 0);
7709if (isa<MetadataAsValue>(V))
7712if (
constauto *
A = dyn_cast<Argument>(V)) {
7713if (
A->hasAttribute(Attribute::NoUndef) ||
7714A->hasAttribute(Attribute::Dereferenceable) ||
7715A->hasAttribute(Attribute::DereferenceableOrNull))
7719if (
auto *
C = dyn_cast<Constant>(V)) {
7720if (isa<PoisonValue>(
C))
7723if (isa<UndefValue>(
C))
7726if (isa<ConstantInt>(
C) || isa<GlobalVariable>(
C) || isa<ConstantFP>(V) ||
7727 isa<ConstantPointerNull>(
C) || isa<Function>(
C))
7730if (
C->getType()->isVectorTy() && !isa<ConstantExpr>(
C)) {
7735return !
C->containsConstantExpression();
7739// Strip cast operations from a pointer value. 7740// Note that stripPointerCastsSameRepresentation can strip off getelementptr 7741// inbounds with zero offset. To guarantee that the result isn't poison, the 7742// stripped pointer is checked as it has to be pointing into an allocated 7743// object or be null `null` to ensure `inbounds` getelement pointers with a 7744// zero offset could not produce poison. 7745// It can strip off addrspacecast that do not change bit representation as 7746// well. We believe that such addrspacecast is equivalent to no-op. 7747auto *StrippedV = V->stripPointerCastsSameRepresentation();
7748if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7749 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7752auto OpCheck = [&](
constValue *V) {
7756if (
auto *Opr = dyn_cast<Operator>(V)) {
7757// If the value is a freeze instruction, then it can never 7758// be undef or poison. 7759if (isa<FreezeInst>(V))
7762if (
constauto *CB = dyn_cast<CallBase>(V)) {
7763if (CB->hasRetAttr(Attribute::NoUndef) ||
7764 CB->hasRetAttr(Attribute::Dereferenceable) ||
7765 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7769if (
constauto *PN = dyn_cast<PHINode>(V)) {
7770unsigned Num = PN->getNumIncomingValues();
7771bool IsWellDefined =
true;
7772for (
unsigned i = 0; i < Num; ++i) {
7773auto *TI = PN->getIncomingBlock(i)->getTerminator();
7775 DT,
Depth + 1, Kind)) {
7776 IsWellDefined =
false;
7783/*ConsiderFlagsAndMetadata*/true) &&
7784all_of(Opr->operands(), OpCheck))
7788if (
auto *
I = dyn_cast<LoadInst>(V))
7789if (
I->hasMetadata(LLVMContext::MD_noundef) ||
7790I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7791I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7797// CxtI may be null or a cloned instruction. 7806// If V is used as a branch condition before reaching CtxI, V cannot be 7810// CtxI ; V cannot be undef or poison here 7811auto *Dominator = DNode->
getIDom();
7812// This check is purely for compile time reasons: we can skip the IDom walk 7813// if what we are checking for includes undef and the value is not an integer. 7816auto *TI = Dominator->
getBlock()->getTerminator();
7819if (
auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7820if (BI->isConditional())
7821Cond = BI->getCondition();
7822 }
elseif (
auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7823Cond = SI->getCondition();
7830// For poison, we can analyze further 7831auto *Opr = cast<Operator>(
Cond);
7832if (
any_of(Opr->operands(), [V](
constUse &U) {
7833 return V == U && propagatesPoison(U);
7839 Dominator = Dominator->getIDom();
7852 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7853 UndefPoisonKind::UndefOrPoison);
7859 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7860 UndefPoisonKind::PoisonOnly);
7866 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7867 UndefPoisonKind::UndefOnly);
7870/// Return true if undefined behavior would provably be executed on the path to 7871/// OnPathTo if Root produced a posion result. Note that this doesn't say 7872/// anything about whether OnPathTo is actually executed or whether Root is 7873/// actually poison. This can be used to assess whether a new use of Root can 7874/// be added at a location which is control equivalent with OnPathTo (such as 7875/// immediately before it) without introducing UB which didn't previously 7876/// exist. Note that a false result conveys no information. 7880// Basic approach is to assume Root is poison, propagate poison forward 7881// through all users we can easily track, and then check whether any of those 7882// users are provable UB and must execute before out exiting block might 7885// The set of all recursive users we've visited (which are assumed to all be 7886// poison because of said visit) 7890while (!Worklist.
empty()) {
7893// If we know this must trigger UB on a path leading our target. 7897// If we can't analyze propagation through this instruction, just skip it 7898// and transitive users. Safe as false is a conservative result. 7899if (
I != Root && !
any_of(
I->operands(), [&KnownPoison](
constUse &U) {
7900 return KnownPoison.contains(U) && propagatesPoison(U);
7904if (KnownPoison.
insert(
I).second)
7909// Might be non-UB, or might have a path we couldn't prove must execute on 7910// way to exiting bb. 7916 return ::computeOverflowForSignedAdd(
Add->getOperand(0),
Add->getOperand(1),
7924 return ::computeOverflowForSignedAdd(
LHS,
RHS,
nullptr, SQ);
7928// Note: An atomic operation isn't guaranteed to return in a reasonable amount 7929// of time because it's possible for another thread to interfere with it for an 7930// arbitrary length of time, but programs aren't allowed to rely on that. 7932// If there is no successor, then execution can't transfer to it. 7933if (isa<ReturnInst>(
I))
7935if (isa<UnreachableInst>(
I))
7938// Note: Do not add new checks here; instead, change Instruction::mayThrow or 7939// Instruction::willReturn. 7941// FIXME: Move this check into Instruction::willReturn. 7942if (isa<CatchPadInst>(
I)) {
7945// A catchpad may invoke exception object constructors and such, which 7946// in some languages can be arbitrary code, so be conservative by default. 7949// For CoreCLR, it just involves a type test. 7954// An instruction that returns without throwing must transfer control flow 7956return !
I->mayThrow() &&
I->willReturn();
7960// TODO: This is slightly conservative for invoke instruction since exiting 7961// via an exception *is* normal control for them. 7970unsigned ScanLimit) {
7977assert(ScanLimit &&
"scan limit must be non-zero");
7979if (isa<DbgInfoIntrinsic>(
I))
7981if (--ScanLimit == 0)
7991// The loop header is guaranteed to be executed for every iteration. 7993// FIXME: Relax this constraint to cover all basic blocks that are 7994// guaranteed to be executed at every iteration. 7995if (
I->getParent() != L->getHeader())
returnfalse;
7998if (&LI ==
I)
returntrue;
8001llvm_unreachable(
"Instruction not contained in its own parent basic block.");
8006switch (
I->getOpcode()) {
8007case Instruction::Freeze:
8008case Instruction::PHI:
8009case Instruction::Invoke:
8011case Instruction::Select:
8013case Instruction::Call:
8014if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
8015switch (
II->getIntrinsicID()) {
8016// TODO: Add more intrinsics. 8017case Intrinsic::sadd_with_overflow:
8018case Intrinsic::ssub_with_overflow:
8019case Intrinsic::smul_with_overflow:
8020case Intrinsic::uadd_with_overflow:
8021case Intrinsic::usub_with_overflow:
8022case Intrinsic::umul_with_overflow:
8023// If an input is a vector containing a poison element, the 8024// two output vectors (calculated results, overflow bits)' 8025// corresponding lanes are poison. 8027case Intrinsic::ctpop:
8028case Intrinsic::ctlz:
8029case Intrinsic::cttz:
8031case Intrinsic::smax:
8032case Intrinsic::smin:
8033case Intrinsic::umax:
8034case Intrinsic::umin:
8035case Intrinsic::bitreverse:
8036case Intrinsic::bswap:
8037case Intrinsic::sadd_sat:
8038case Intrinsic::ssub_sat:
8039case Intrinsic::sshl_sat:
8040case Intrinsic::uadd_sat:
8041case Intrinsic::usub_sat:
8042case Intrinsic::ushl_sat:
8047case Instruction::ICmp:
8048case Instruction::FCmp:
8049case Instruction::GetElementPtr:
8052if (isa<BinaryOperator>(
I) || isa<UnaryOperator>(
I) || isa<CastInst>(
I))
8055// Be conservative and return false. 8060/// Enumerates all operands of \p I that are guaranteed to not be undef or 8061/// poison. If the callback \p Handle returns true, stop processing and return 8062/// true. Otherwise, return false. 8063template <
typename CallableT>
8065const CallableT &Handle) {
8066switch (
I->getOpcode()) {
8067case Instruction::Store:
8072case Instruction::Load:
8077// Since dereferenceable attribute imply noundef, atomic operations 8078// also implicitly have noundef pointers too 8079case Instruction::AtomicCmpXchg:
8084case Instruction::AtomicRMW:
8089case Instruction::Call:
8090case Instruction::Invoke: {
8094for (
unsigned i = 0; i < CB->
arg_size(); ++i)
8097 CB->
paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8102case Instruction::Ret:
8103if (
I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8104 Handle(
I->getOperand(0)))
8107case Instruction::Switch:
8108if (Handle(cast<SwitchInst>(
I)->getCondition()))
8111case Instruction::Br: {
8112auto *BR = cast<BranchInst>(
I);
8113if (BR->isConditional() && Handle(BR->getCondition()))
8132/// Enumerates all operands of \p I that are guaranteed to not be poison. 8133template <
typename CallableT>
8135const CallableT &Handle) {
8138switch (
I->getOpcode()) {
8139// Divisors of these operations are allowed to be partially undef. 8140case Instruction::UDiv:
8141case Instruction::SDiv:
8142case Instruction::URem:
8143case Instruction::SRem:
8144return Handle(
I->getOperand(1));
8161I, [&](
constValue *V) {
return KnownPoison.
count(V); });
8166// We currently only look for uses of values within the same basic 8167// block, as that makes it easier to guarantee that the uses will be 8168// executed given that Inst is executed. 8170// FIXME: Expand this to consider uses beyond the same basic block. To do 8171// this, look out for the distinction between post-dominance and strong 8175if (
constauto *Inst = dyn_cast<Instruction>(V)) {
8179 }
elseif (
constauto *Arg = dyn_cast<Argument>(V)) {
8180if (Arg->getParent()->isDeclaration())
8183 Begin = BB->
begin();
8188// Limit number of instructions we look at, to avoid scanning through large 8189// blocks. The current limit is chosen arbitrarily. 8190unsigned ScanLimit = 32;
8194// Since undef does not propagate eagerly, be conservative & just check 8195// whether a value is directly passed to an instruction that must take 8196// well-defined operands. 8199if (isa<DbgInfoIntrinsic>(
I))
8201if (--ScanLimit == 0)
8205return WellDefinedOp == V;
8215// Set of instructions that we have proved will yield poison if Inst 8225if (isa<DbgInfoIntrinsic>(
I))
8227if (--ScanLimit == 0)
8234// If an operand is poison and propagates it, mark I as yielding poison. 8235for (
constUse &
Op :
I.operands()) {
8242// Special handling for select, which returns poison if its operand 0 is 8243// poison (handled in the loop above) *or* if both its true/false operands 8244// are poison (handled here). 8245if (
I.getOpcode() == Instruction::Select &&
8246 YieldsPoison.
count(
I.getOperand(1)) &&
8247 YieldsPoison.
count(
I.getOperand(2))) {
8253if (!BB || !Visited.
insert(BB).second)
8263 return ::programUndefinedIfUndefOrPoison(Inst,
false);
8267 return ::programUndefinedIfUndefOrPoison(Inst,
true);
8274if (
auto *
C = dyn_cast<ConstantFP>(V))
8277if (
auto *
C = dyn_cast<ConstantDataVector>(V)) {
8278if (!
C->getElementType()->isFloatingPointTy())
8280for (
unsignedI = 0, E =
C->getNumElements();
I < E; ++
I) {
8281if (
C->getElementAsAPFloat(
I).isNaN())
8287if (isa<ConstantAggregateZero>(V))
8294if (
auto *
C = dyn_cast<ConstantFP>(V))
8297if (
auto *
C = dyn_cast<ConstantDataVector>(V)) {
8298if (!
C->getElementType()->isFloatingPointTy())
8300for (
unsignedI = 0, E =
C->getNumElements();
I < E; ++
I) {
8301if (
C->getElementAsAPFloat(
I).isZero())
8310/// Match clamp pattern for float types without care about NaNs or signed zeros. 8311/// Given non-min/max outer cmp/select from the clamp pattern this 8312/// function recognizes if it can be substitued by a "canonical" min/max 8319// X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) 8320// X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) 8321// and return description of the outer Max/Min. 8323// First, check if select has inverse order: 8324if (CmpRHS == FalseVal) {
8329// Assume success now. If there's no match, callers should not use these anyway. 8362/// Recognize variations of: 8363/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) 8367// Swap the select operands and predicate to match the patterns below. 8368if (CmpRHS != TrueVal) {
8375// (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) 8380// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) 8385// (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) 8390// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) 8398/// Recognize variations of: 8399/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) 8404// TODO: Allow FP min/max with nnan/nsz. 8407Value *
A =
nullptr, *
B =
nullptr;
8412Value *
C =
nullptr, *
D =
nullptr;
8414if (L.Flavor != R.Flavor)
8417// We have something like: x Pred y ? min(a, b) : min(c, d). 8418// Try to match the compare to the min/max operations of the select operands. 8419// First, make sure we have the right compare predicate. 8457// If there is a common operand in the already matched min/max and the other 8458// min/max operands match the compare operands (either directly or inverted), 8459// then this is min/max of the same flavor. 8461// a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) 8462// ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) 8466return {L.Flavor,
SPNB_NA,
false};
8468// a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) 8469// ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) 8473return {L.Flavor,
SPNB_NA,
false};
8475// b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) 8476// ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) 8480return {L.Flavor,
SPNB_NA,
false};
8482// b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) 8483// ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) 8487return {L.Flavor,
SPNB_NA,
false};
8493/// If the input value is the result of a 'not' op, constant integer, or vector 8494/// splat of a constant integer, return the bitwise-not source value. 8495/// TODO: This could be extended to handle non-splat vector integer constants. 8503return ConstantInt::get(V->getType(), ~(*
C));
8508/// Match non-obvious integer minimum and maximum sequences. 8514// Assume success. If there's no match, callers should not use these anyway. 8526// Look through 'not' ops to find disguised min/max. 8527// (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y) 8528// (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y) 8539// (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X) 8540// (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X) 8558// An unsigned min/max can be written with a signed compare. 8560if ((CmpLHS == TrueVal &&
match(FalseVal,
m_APInt(C2))) ||
8562// Is the sign bit set? 8563// (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX 8564// (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN 8568// Is the sign bit clear? 8569// (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX 8570// (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN 8586auto *BO = cast<BinaryOperator>(
X);
8587if (NeedNSW && !BO->hasNoSignedWrap())
8590auto *Zero = cast<Constant>(BO->getOperand(0));
8591if (!AllowPoison && !Zero->isNullValue())
8598if (IsNegationOf(
X,
Y) || IsNegationOf(
Y,
X))
8601// X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) 8610// Handle X = icmp pred A, B, Y = icmp pred A, C. 8617// They must both have samesign flag or not. 8618if (cast<ICmpInst>(
X)->hasSameSign() != cast<ICmpInst>(
Y)->hasSameSign())
8624// Try to infer the relationship from constant ranges. 8625constAPInt *RHSC1, *RHSC2;
8629// Sign bits of two RHSCs should match. 8630if (cast<ICmpInst>(
X)->hasSameSign() &&
8637return CR1.inverse() == CR2;
8671std::optional<std::pair<CmpPredicate, Constant *>>
8674"Only for relational integer predicates.");
8675if (isa<UndefValue>(
C))
8685// Check if the constant operand can be safely incremented/decremented 8686// without overflowing/underflowing. 8687auto ConstantIsOk = [WillIncrement, IsSigned](
ConstantInt *
C) {
8688return WillIncrement ? !
C->isMaxValue(IsSigned) : !
C->isMinValue(IsSigned);
8691Constant *SafeReplacementConstant =
nullptr;
8692if (
auto *CI = dyn_cast<ConstantInt>(
C)) {
8693// Bail out if the constant can't be safely incremented/decremented. 8694if (!ConstantIsOk(CI))
8696 }
elseif (
auto *FVTy = dyn_cast<FixedVectorType>(
Type)) {
8697unsigned NumElts = FVTy->getNumElements();
8698for (
unsigned i = 0; i != NumElts; ++i) {
8699Constant *Elt =
C->getAggregateElement(i);
8703if (isa<UndefValue>(Elt))
8706// Bail out if we can't determine if this constant is min/max or if we 8707// know that this constant is min/max. 8708auto *CI = dyn_cast<ConstantInt>(Elt);
8709if (!CI || !ConstantIsOk(CI))
8712if (!SafeReplacementConstant)
8713 SafeReplacementConstant = CI;
8715 }
elseif (isa<VectorType>(
C->getType())) {
8716// Handle scalable splat 8717Value *SplatC =
C->getSplatValue();
8718auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8719// Bail out if the constant can't be safely incremented/decremented. 8720if (!CI || !ConstantIsOk(CI))
8727// It may not be safe to change a compare predicate in the presence of 8728// undefined elements, so replace those elements with the first safe constant 8730// TODO: in case of poison, it is safe; let's replace undefs only. 8731if (
C->containsUndefOrPoisonElement()) {
8732assert(SafeReplacementConstant &&
"Replacement constant not set");
8738// Increment or decrement the constant. 8739Constant *OneOrNegOne = ConstantInt::get(
Type, WillIncrement ? 1 : -1,
true);
8742return std::make_pair(NewPred, NewC);
8751bool HasMismatchedZeros =
false;
8753// IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one 8754// 0.0 operand, set the compare's 0.0 operands to that same value for the 8755// purpose of identifying min/max. Disregard vector constants with undefined 8756// elements because those can not be back-propagated for analysis. 8757Value *OutputZeroVal =
nullptr;
8759 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8760 OutputZeroVal = TrueVal;
8762 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8763 OutputZeroVal = FalseVal;
8767 HasMismatchedZeros =
true;
8768 CmpLHS = OutputZeroVal;
8771 HasMismatchedZeros =
true;
8772 CmpRHS = OutputZeroVal;
8780// Signed zero may return inconsistent results between implementations. 8781// (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 8782// minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) 8783// Therefore, we behave conservatively and only proceed if at least one of the 8784// operands is known to not be zero or if we don't care about signed zero. 8789if (!HasMismatchedZeros)
8802// When given one NaN and one non-NaN input: 8803// - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. 8804// - A simple C99 (a < b ? a : b) construction will return 'b' (as the 8805// ordered comparison fails), which could be NaN or non-NaN. 8806// so here we discover exactly what NaN behavior is required/accepted. 8811if (LHSSafe && RHSSafe) {
8812// Both operands are known non-NaN. 8815// An ordered comparison will return false when given a NaN, so it 8819// LHS is non-NaN, so if RHS is NaN then NaN will be returned. 8824// Completely unsafe. 8828// An unordered comparison will return true when given a NaN, so it 8831// LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. 8836// Completely unsafe. 8841if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8851// ([if]cmp X, Y) ? X : Y 8852if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8856// Sign-extending LHS does not change its sign, so TrueVal/FalseVal can 8857// match against either LHS or sext(LHS). 8858auto MaybeSExtCmpLHS =
8862if (
match(TrueVal, MaybeSExtCmpLHS)) {
8863// Set the return values. If the compare uses the negated value (-X >s 0), 8864// swap the return values because the negated value is always 'RHS'. 8870// (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) 8871// (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) 8875// (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) 8879// (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) 8880// (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) 8884elseif (
match(FalseVal, MaybeSExtCmpLHS)) {
8885// Set the return values. If the compare uses the negated value (-X >s 0), 8886// swap the return values because the negated value is always 'RHS'. 8892// (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) 8893// (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) 8897// (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) 8898// (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) 8907// According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar 8908// may return either -0.0 or 0.0, so fcmp/select pair has stricter 8909// semantics than minNum. Be conservative in such case. 8924case Instruction::ZExt:
8928case Instruction::SExt:
8932case Instruction::Trunc:
8935 CmpConst->
getType() == SrcTy) {
8936// Here we have the following case: 8938// %cond = cmp iN %x, CmpConst 8939// %tr = trunc iN %x to iK 8940// %narrowsel = select i1 %cond, iK %t, iK C 8942// We can always move trunc after select operation: 8944// %cond = cmp iN %x, CmpConst 8945// %widesel = select i1 %cond, iN %x, iN CmpConst 8946// %tr = trunc iN %widesel to iK 8948// Note that C could be extended in any way because we don't care about 8949// upper bits after truncation. It can't be abs pattern, because it would 8952// select i1 %cond, x, -x. 8954// So only min/max pattern could be matched. Such match requires widened C 8955// == CmpConst. That is why set widened C = CmpConst, condition trunc 8956// CmpConst == C is checked below. 8957 CastedTo = CmpConst;
8959unsigned ExtOp = CmpI->
isSigned() ? Instruction::SExt : Instruction::ZExt;
8963case Instruction::FPTrunc:
8966case Instruction::FPExt:
8969case Instruction::FPToUI:
8972case Instruction::FPToSI:
8975case Instruction::UIToFP:
8978case Instruction::SIToFP:
8988// Make sure the cast doesn't lose any information. 8991if (CastedBack && CastedBack !=
C)
8997/// Helps to match a select pattern in case of a type mismatch. 8999/// The function processes the case when type of true and false values of a 9000/// select instruction differs from type of the cmp instruction operands because 9001/// of a cast instruction. The function checks if it is legal to move the cast 9002/// operation after "select". If yes, it returns the new second value of 9003/// "select" (with the assumption that cast is moved): 9004/// 1. As operand of cast instruction when both values of "select" are same cast 9006/// 2. As restored constant (by applying reverse cast operation) when the first 9007/// value of the "select" is a cast operation and the second value is a 9008/// constant. It is implemented in lookThroughCastConst(). 9009/// 3. As one operand is cast instruction and the other is not. The operands in 9010/// sel(cmp) are in different type integer. 9011/// NOTE: We return only the new second value because the first value could be 9012/// accessed as operand of cast instruction. 9015auto *Cast1 = dyn_cast<CastInst>(V1);
9019 *CastOp = Cast1->getOpcode();
9020Type *SrcTy = Cast1->getSrcTy();
9021if (
auto *Cast2 = dyn_cast<CastInst>(V2)) {
9022// If V1 and V2 are both the same cast from the same type, look through V1. 9023if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9024return Cast2->getOperand(0);
9028auto *
C = dyn_cast<Constant>(V2);
9032Value *CastedTo =
nullptr;
9033if (*CastOp == Instruction::Trunc) {
9035// Here we have the following case: 9036// %y_ext = sext iK %y to iN 9037// %cond = cmp iN %x, %y_ext 9038// %tr = trunc iN %x to iK 9039// %narrowsel = select i1 %cond, iK %tr, iK %y 9041// We can always move trunc after select operation: 9042// %y_ext = sext iK %y to iN 9043// %cond = cmp iN %x, %y_ext 9044// %widesel = select i1 %cond, iN %x, iN %y_ext 9045// %tr = trunc iN %widesel to iK 9046assert(V2->getType() == Cast1->getType() &&
9047"V2 and Cast1 should be the same type.");
9063CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9066Value *TrueVal = SI->getTrueValue();
9067Value *FalseVal = SI->getFalseValue();
9080if (isa<FPMathOperator>(CmpI))
9087// Deal with type mismatches. 9088if (CastOp && CmpLHS->
getType() != TrueVal->getType()) {
9090// If this is a potential fmin/fmax with a cast to integer, then ignore 9091// -0.0 because there is no corresponding integer value. 9092if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9094 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9095 cast<CastInst>(TrueVal)->getOperand(0),
C,
9099// If this is a potential fmin/fmax with a cast to integer, then ignore 9100// -0.0 because there is no corresponding integer value. 9101if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9103 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9104C, cast<CastInst>(FalseVal)->getOperand(0),
9108 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9127return Intrinsic::umin;
9129return Intrinsic::umax;
9131return Intrinsic::smin;
9133return Intrinsic::smax;
9149case Intrinsic::smax:
return Intrinsic::smin;
9150case Intrinsic::smin:
return Intrinsic::smax;
9151case Intrinsic::umax:
return Intrinsic::umin;
9152case Intrinsic::umin:
return Intrinsic::umax;
9153// Please note that next four intrinsics may produce the same result for 9154// original and inverted case even if X != Y due to NaN is handled specially. 9155case Intrinsic::maximum:
return Intrinsic::minimum;
9156case Intrinsic::minimum:
return Intrinsic::maximum;
9157case Intrinsic::maxnum:
return Intrinsic::minnum;
9158case Intrinsic::minnum:
return Intrinsic::maxnum;
9173std::pair<Intrinsic::ID, bool>
9175// Check if VL contains select instructions that can be folded into a min/max 9176// vector intrinsic and return the intrinsic if it is possible. 9177// TODO: Support floating point min/max. 9178bool AllCmpSingleUse =
true;
9181if (
all_of(VL, [&SelectPattern, &AllCmpSingleUse](
Value *
I) {
9187 SelectPattern.
Flavor != CurrentPattern.Flavor)
9189 SelectPattern = CurrentPattern;
9194switch (SelectPattern.
Flavor) {
9196return {Intrinsic::smin, AllCmpSingleUse};
9198return {Intrinsic::umin, AllCmpSingleUse};
9200return {Intrinsic::smax, AllCmpSingleUse};
9202return {Intrinsic::umax, AllCmpSingleUse};
9204return {Intrinsic::maxnum, AllCmpSingleUse};
9206return {Intrinsic::minnum, AllCmpSingleUse};
9216// Handle the case of a simple two-predecessor recurrence PHI. 9217// There's a lot more that could theoretically be done here, but 9218// this is sufficient to catch some interesting cases. 9219if (
P->getNumIncomingValues() != 2)
9222for (
unsigned i = 0; i != 2; ++i) {
9223Value *L =
P->getIncomingValue(i);
9224Value *R =
P->getIncomingValue(!i);
9225auto *LU = dyn_cast<BinaryOperator>(L);
9228unsigned Opcode = LU->getOpcode();
9233// TODO: Expand list -- xor, gep, uadd.sat etc. 9234case Instruction::LShr:
9235case Instruction::AShr:
9236case Instruction::Shl:
9237case Instruction::Add:
9238case Instruction::Sub:
9239case Instruction::UDiv:
9240case Instruction::URem:
9241case Instruction::And:
9242case Instruction::Or:
9243case Instruction::Mul:
9244case Instruction::FMul: {
9245Value *LL = LU->getOperand(0);
9246Value *LR = LU->getOperand(1);
9247// Find a recurrence. 9253continue;
// Check for recurrence with L and R flipped. 9259// We have matched a recurrence of the form: 9260// %iv = [R, %entry], [%iv.next, %backedge] 9261// %iv.next = binop %iv, L 9263// %iv = [R, %entry], [%iv.next, %backedge] 9264// %iv.next = binop L, %iv 9276P = dyn_cast<PHINode>(
I->getOperand(0));
9278P = dyn_cast<PHINode>(
I->getOperand(1));
9282/// Return true if "icmp Pred LHS RHS" is always true. 9295// LHS s<= LHS +_{nsw} C if C >= 0 9296// LHS s<= LHS | C if C >= 0 9299return !
C->isNegative();
9301// LHS s<= smax(LHS, V) for any V 9305// smin(RHS, V) s<= RHS for any V 9309// Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB) 9311constAPInt *CLHS, *CRHS;
9314return CLHS->
sle(*CRHS);
9320// LHS u<= LHS +_{nuw} V for any V 9325// LHS u<= LHS | V for any V 9329// LHS u<= umax(LHS, V) for any V 9333// RHS >> V u<= RHS for any V 9337// RHS u/ C_ugt_1 u<= RHS 9342// RHS & V u<= RHS for any V 9346// umin(RHS, V) u<= RHS for any V 9350// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) 9352constAPInt *CLHS, *CRHS;
9355return CLHS->
ule(*CRHS);
9362/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred 9363/// ALHS ARHS" is true. Otherwise, return std::nullopt. 9364static std::optional<bool>
9401/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true. 9402/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false. 9403/// Otherwise, return std::nullopt if we can't infer anything. 9404static std::optional<bool>
9409// If all true values for lhs and true for rhs, lhs implies rhs 9410if (CR.
icmp(Pred, RCR))
9413// If there is no overlap, lhs implies not rhs 9433/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") 9434/// is true. Return false if LHS implies RHS is false. Otherwise, return 9435/// std::nullopt if we can't infer anything. 9436static std::optional<bool>
9442// The rest of the logic assumes the LHS condition is true. If that's not the 9443// case, invert the predicate to make it so. 9445 LHSIsTrue ?
LHS->getCmpPredicate() :
LHS->getInverseCmpPredicate();
9447// We can have non-canonical operands, so try to normalize any common operand 9458// If we have L0 == R0 and L1 == R1, then make L1/R1 the constants. 9467// See if we can infer anything if operand-0 matches and we have at least one 9471// Potential TODO: We could also further use the constant range of L0/R0 to 9472// further constraint the constant ranges. At the moment this leads to 9473// several regressions related to not transforming `multi_use(A + C0) eq/ne 9474// C1` (see discussion: D58633). 9481// Even if L1/R1 are not both constant, we can still sometimes deduce 9482// relationship from a single constant. For example X u> Y implies X != 0. 9485// If both L1/R1 were exact constant ranges and we didn't get anything 9486// here, we won't be able to deduce this. 9491// Can we infer anything when the two compares have matching operands? 9492if (L0 == R0 && L1 == R1)
9495// It only really makes sense in the context of signed comparison for "X - Y 9496// must be positive if X >= Y and no overflow". 9497// Take SGT as an example: L0:x > L1:y and C >= 0 9498// ==> R0:(x -nsw y) < R1:(-C) is false 9508// Take SLT as an example: L0:x < L1:y and C <= 0 9509// ==> R0:(x -nsw y) < R1:(-C) is true 9518// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1 9531/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is 9532/// false. Otherwise, return std::nullopt if we can't infer anything. We 9533/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select' 9535static std::optional<bool>
9539// The LHS must be an 'or', 'and', or a 'select' instruction. 9540assert((
LHS->getOpcode() == Instruction::And ||
9541LHS->getOpcode() == Instruction::Or ||
9542LHS->getOpcode() == Instruction::Select) &&
9543"Expected LHS to be 'and', 'or', or 'select'.");
9547// If the result of an 'or' is false, then we know both legs of the 'or' are 9548// false. Similarly, if the result of an 'and' is true, then we know both 9549// legs of the 'and' are true. 9550constValue *ALHS, *ARHS;
9553// FIXME: Make this non-recursion. 9555 ALHS, RHSPred, RHSOp0, RHSOp1,
DL, LHSIsTrue,
Depth + 1))
9558 ARHS, RHSPred, RHSOp0, RHSOp1,
DL, LHSIsTrue,
Depth + 1))
9569// Bail out when we hit the limit. 9573// A mismatch occurs when we compare a scalar cmp to a vector cmp, for 9579"Expected integer type only!");
9583 LHSIsTrue = !LHSIsTrue;
9585// Both LHS and RHS are icmps. 9590 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect 9591 /// the RHS to be an icmp. 9592 /// FIXME: Add support for and/or/select on the RHS. 9594if ((LHSI->getOpcode() == Instruction::And ||
9595 LHSI->getOpcode() == Instruction::Or ||
9596 LHSI->getOpcode() == Instruction::Select))
9605bool LHSIsTrue,
unsignedDepth) {
9606// LHS ==> RHS by definition 9611bool InvertRHS =
false;
9618if (
constICmpInst *RHSCmp = dyn_cast<ICmpInst>(
RHS)) {
9620LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9621 RHSCmp->getOperand(1),
DL, LHSIsTrue,
Depth))
9622return InvertRHS ? !*Implied : *Implied;
9629// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2 9630// LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2 9631constValue *RHS1, *RHS2;
9633if (std::optional<bool> Imp =
9637if (std::optional<bool> Imp =
9643if (std::optional<bool> Imp =
9647if (std::optional<bool> Imp =
9656// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch 9657// condition dominating ContextI or nullptr, if no condition is found. 9658static std::pair<Value *, bool>
9661return {
nullptr,
false};
9663// TODO: This is a poor/cheap way to determine dominance. Should we use a 9664// dominator tree (eg, from a SimplifyQuery) instead? 9668return {
nullptr,
false};
9670// We need a conditional branch in the predecessor. 9674return {
nullptr,
false};
9676// The branch should get simplified. Don't bother simplifying this condition. 9677if (TrueBB == FalseBB)
9678return {
nullptr,
false};
9680assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9681"Predecessor block does not point to successor?");
9683// Is this condition implied by the predecessor condition? 9684return {PredCond, TrueBB == ContextBB};
9690assert(
Cond->getType()->isIntOrIntVectorTy(1) &&
"Condition must be bool");
9711bool PreferSignedRange) {
9712unsigned Width =
Lower.getBitWidth();
9715case Instruction::Add:
9720// If the caller expects a signed compare, then try to use a signed range. 9721// Otherwise if both no-wraps are set, use the unsigned range because it 9722// is never larger than the signed range. Example: 9723// "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125]. 9724if (PreferSignedRange && HasNSW && HasNUW)
9728// 'add nuw x, C' produces [C, UINT_MAX]. 9731if (
C->isNegative()) {
9732// 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. 9736// 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. 9744case Instruction::And:
9746// 'and x, C' produces [0, C]. 9748// X & -X is a power of two or zero. So we can cap the value at max power of 9755case Instruction::Or:
9757// 'or x, C' produces [C, UINT_MAX]. 9761case Instruction::AShr:
9763// 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. 9767unsigned ShiftAmount = Width - 1;
9768if (!
C->isZero() && IIQ.
isExact(&BO))
9769 ShiftAmount =
C->countr_zero();
9770if (
C->isNegative()) {
9771// 'ashr C, x' produces [C, C >> (Width-1)] 9773Upper =
C->ashr(ShiftAmount) + 1;
9775// 'ashr C, x' produces [C >> (Width-1), C] 9776Lower =
C->ashr(ShiftAmount);
9782case Instruction::LShr:
9784// 'lshr x, C' produces [0, UINT_MAX >> C]. 9787// 'lshr C, x' produces [C >> (Width-1), C]. 9788unsigned ShiftAmount = Width - 1;
9789if (!
C->isZero() && IIQ.
isExact(&BO))
9790 ShiftAmount =
C->countr_zero();
9791Lower =
C->lshr(ShiftAmount);
9796case Instruction::Shl:
9799// 'shl nuw C, x' produces [C, C << CLZ(C)] 9803if (
C->isNegative()) {
9804// 'shl nsw C, x' produces [C << CLO(C)-1, C] 9805unsigned ShiftAmount =
C->countl_one() - 1;
9806Lower =
C->shl(ShiftAmount);
9809// 'shl nsw C, x' produces [C, C << CLZ(C)-1] 9810unsigned ShiftAmount =
C->countl_zero() - 1;
9812Upper =
C->shl(ShiftAmount) + 1;
9815// If lowbit is set, value can never be zero. 9818// If we are shifting a constant the largest it can be is if the longest 9819// sequence of consecutive ones is shifted to the highbits (breaking 9820// ties for which sequence is higher). At the moment we take a liberal 9821// upper bound on this by just popcounting the constant. 9822// TODO: There may be a bitwise trick for it longest/highest 9823// consecutative sequence of ones (naive method is O(Width) loop). 9831case Instruction::SDiv:
9835if (
C->isAllOnes()) {
9836// 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] 9837// where C != -1 and C != 0 and C != 1 9840 }
elseif (
C->countl_zero() < Width - 1) {
9841// 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] 9842// where C != -1 and C != 0 and C != 1 9851if (
C->isMinSignedValue()) {
9852// 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. 9856// 'sdiv C, x' produces [-|C|, |C|]. 9863case Instruction::UDiv:
9865// 'udiv x, C' produces [0, UINT_MAX / C]. 9868// 'udiv C, x' produces [0, C]. 9873case Instruction::SRem:
9875// 'srem x, C' produces (-|C|, |C|). 9879if (
C->isNegative()) {
9880// 'srem -|C|, x' produces [-|C|, 0]. 9884// 'srem |C|, x' produces [0, |C|]. 9890case Instruction::URem:
9892// 'urem x, C' produces [0, C). 9895// 'urem C, x' produces [0, C]. 9906unsigned Width =
II.getType()->getScalarSizeInBits();
9908switch (
II.getIntrinsicID()) {
9909case Intrinsic::ctlz:
9910case Intrinsic::cttz: {
9912if (!UseInstrInfo || !
match(
II.getArgOperand(1),
m_One()))
9914// Maximum of set/clear bits is the bit width. 9917case Intrinsic::ctpop:
9918// Maximum of set/clear bits is the bit width. 9920APInt(Width, Width) + 1);
9921case Intrinsic::uadd_sat:
9922// uadd.sat(x, C) produces [C, UINT_MAX]. 9927case Intrinsic::sadd_sat:
9931// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. 9936// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. 9941case Intrinsic::usub_sat:
9942// usub.sat(C, x) produces [0, C]. 9946// usub.sat(x, C) produces [0, UINT_MAX - C]. 9951case Intrinsic::ssub_sat:
9954// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. 9959// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. 9964// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: 9968// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. 9974case Intrinsic::umin:
9975case Intrinsic::umax:
9976case Intrinsic::smin:
9977case Intrinsic::smax:
9982switch (
II.getIntrinsicID()) {
9983case Intrinsic::umin:
9985case Intrinsic::umax:
9987case Intrinsic::smin:
9990case Intrinsic::smax:
9998// If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX], 9999// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. 10006case Intrinsic::vscale:
10007if (!
II.getParent() || !
II.getFunction())
10010case Intrinsic::scmp:
10011case Intrinsic::ucmp:
10018return ConstantRange::getFull(Width);
10023unsignedBitWidth = SI.getType()->getScalarSizeInBits();
10027return ConstantRange::getFull(
BitWidth);
10030// If the negation part of the abs (in RHS) has the NSW flag, 10031// then the result of abs(X) is [0..SIGNED_MAX], 10032// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. 10043// The result of -abs(X) is <= 0. 10050return ConstantRange::getFull(
BitWidth);
10064return ConstantRange::getFull(
BitWidth);
10069// The maximum representable value of a half is 65504. For floats the maximum 10070// value is 3.4e38 which requires roughly 129 bits. 10071unsignedBitWidth =
I->getType()->getScalarSizeInBits();
10072if (!
I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10074if (isa<FPToSIInst>(
I) &&
BitWidth >= 17) {
10079if (isa<FPToUIInst>(
I) &&
BitWidth >= 16) {
10080// For a fptoui the lower limit is left as 0. 10090assert(V->getType()->isIntOrIntVectorTy() &&
"Expected integer instruction");
10093return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10095if (
auto *
C = dyn_cast<Constant>(V))
10096returnC->toConstantRange();
10098unsignedBitWidth = V->getType()->getScalarSizeInBits();
10101if (
auto *BO = dyn_cast<BinaryOperator>(V)) {
10104// TODO: Return ConstantRange. 10107 }
elseif (
auto *
II = dyn_cast<IntrinsicInst>(V))
10109elseif (
auto *SI = dyn_cast<SelectInst>(V)) {
10111 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,
Depth + 1);
10113 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,
Depth + 1);
10116 }
elseif (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10119// TODO: Return ConstantRange. 10122 }
elseif (
constauto *
A = dyn_cast<Argument>(V))
10123if (std::optional<ConstantRange>
Range =
A->getRange())
10126if (
auto *
I = dyn_cast<Instruction>(V)) {
10130if (
constauto *CB = dyn_cast<CallBase>(V))
10131if (std::optional<ConstantRange>
Range = CB->getRange())
10136// Try to restrict the range based on information from assumptions. 10142"Got assumption for the wrong function!");
10143assert(
I->getIntrinsicID() == Intrinsic::assume &&
10144"must be an assume intrinsic");
10148Value *Arg =
I->getArgOperand(0);
10149ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10150// Currently we just use information from comparisons. 10151if (!Cmp || Cmp->getOperand(0) != V)
10153// TODO: Set "ForSigned" parameter via Cmp->isSigned()? 10156 UseInstrInfo, AC,
I, DT,
Depth + 1);
10169if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10171 }
elseif (
auto *
I = dyn_cast<Instruction>(V)) {
10174// Peek through unary operators to find the source of the condition. 10177if (isa<Instruction>(
Op) || isa<Argument>(
Op))
10178 InsertAffected(
Op);
10185auto AddAffected = [&InsertAffected](
Value *V) {
10200while (!Worklist.
empty()) {
10202if (!Visited.
insert(V).second)
10215// assume(A && B) is split to -> assume(A); assume(B); 10216// assume(!(A || B)) is split to -> assume(!A); assume(!B); 10217// Finally, assume(A || B) / assume(!(A && B)) generally don't provide 10218// enough information to be worth handling (intersection of information as 10219// opposed to union). 10225 AddCmpOperands(
A,
B);
10231// (X & C) or (X | C) or (X ^ C). 10232// (X << C) or (X >>_s C) or (X >>_u C). 10244// Handle (A + C1) u< C2, which is the canonical form of 10245// A > C3 && A < C4. 10251// X & Y u> C -> X >u C && Y >u C 10252// X | Y u< C -> X u< C && Y u< C 10253// X nuw+ Y u< C -> X u< C && Y u< C 10260// X nuw- Y u> C -> X u> C 10266// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported 10267// by computeKnownFPClass(). 10276if (HasRHSC &&
match(
A, m_Intrinsic<Intrinsic::ctpop>(
m_Value(
X))))
10279 AddCmpOperands(
A,
B);
10283// fcmp fneg(fabs(x)), y 10289 }
elseif (
match(V, m_Intrinsic<Intrinsic::is_fpclass>(
m_Value(
A),
10291// Handle patterns that computeKnownFPClass() support. AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::optional< std::vector< StOtherPiece > > Other
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static MaybeAlign getAlign(Value *Ptr)
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
mir Rename Register Operands
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const SmallVectorImpl< MachineOperand > & Cond
static bool mayHaveSideEffects(MachineInstr &MI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const KnownBits &KnownVal)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool inputDenormalIsIEEE(const Function &F, const Type *Ty)
Return true if it's possible to assume IEEE treatment of input denormals in F for Val.
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static std::tuple< Value *, FPClassTest, FPClassTest > exactClass(Value *V, FPClassTest M)
Return the return value for fcmpImpliesClass for a compare that produces an exact class test.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static bool isKnownNonEqual(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if it is known that V1 != V2.
static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if the given value is known to be non-zero when defined.
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool includesPoison(UndefPoisonKind Kind)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, unsigned Depth, SimplifyQuery &Q)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V1 == (binop V2, X), where X is known non-zero.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, unsigned Depth, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
static std::optional< bool > isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Determine which bits of V are known to be either zero or one and return them in the Known bit set.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, unsigned Depth, const SimplifyQuery &SQ, bool Invert)
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, const SimplifyQuery &Q)
Test whether a GEP's result is known to be non-null.
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y)
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &Q)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred ALHS ARHS" is true.
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return the number of times the sign bit of the register is replicated into the other bits.
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
APInt bitcastToAPInt() const
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
bool isSmallestNormalized() const
Class for arbitrary precision integers.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
void clearBit(unsigned BitPosition)
Set a given bit to 0.
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
unsigned ceilLogBase2() const
bool sgt(const APInt &RHS) const
Signed greater than comparison.
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
bool isNegative() const
Determine sign of this APInt.
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
void clearAllBits()
Set every bit to 0.
APInt reverseBits() const
bool sle(const APInt &RHS) const
Signed less or equal comparison.
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
unsigned logBase2() const
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
void setAllBits()
Set every bit to 1.
bool getBoolValue() const
Convert APInt to a boolean value.
bool isMaxSignedValue() const
Determine if this is the largest signed value.
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
APInt shl(unsigned shiftAmt) const
Left-shift function.
bool slt(const APInt &RHS) const
Signed less than comparison.
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
void clearSignBit()
Set the sign bit to 0.
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Class to represent array types.
Type * getElementType() const
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
This class is the base class for the comparison instructions.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ FCMP_OLT
0 1 0 0 True if ordered and less than
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ FCMP_ULT
1 1 0 0 True if unordered or less than
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
@ ICMP_ULT
unsigned less than
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
@ ICMP_SGE
signed greater or equal
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
@ ICMP_ULE
unsigned less or equal
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
static bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
bool isTrueWhenEqual() const
This is just a convenience.
bool isFPPredicate() const
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Predicate getPredicate() const
Return the predicate for this instruction.
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
bool isIntPredicate() const
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
bool isAllNegative() const
Return true if all values in this range are negative.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isZeroValue() const
Return true if the value is negative zero or null value.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
bool isLittleEndian() const
Layout endianness...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
Value * getAggregateOperand()
Utility class for floating point operations which can have information about relaxed accuracy require...
Convenience struct for specifying and reasoning about fast-math flags.
bool noSignedZeros() const
void setNoSignedZeros(bool B=true)
const BasicBlock & getEntryBlock() const
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
Value * getAggregateOperand()
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
const Function * getFunction() const
Return the function this instruction belongs to.
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
This is a utility class that provides an abstraction for the common functionality between Instruction...
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
TypeSize getElementOffset(unsigned Idx) const
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool isPointerTy() const
True if this is an instance of PointerType.
uint64_t getArrayNumElements() const
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
User * getUser() const
Returns the User that contains this Use.
unsigned getOperandNo() const
Return the operand # of this use in its User.
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
iterator_range< user_iterator > users()
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
PointerType getValue() const
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
An efficient, type-erasing, non-owning reference to a callable.
StructType * getStructTypeOrNull() const
TypeSize getSequentialElementStride(const DataLayout &DL) const
Type * getIndexedType() const
const ParentTy * getParent() const
self_iterator getIterator()
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
OneUse_match< T > m_OneUse(const T &SubPattern)
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
VScaleVal_match m_VScale()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
void getGuaranteedNonPoisonOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
gep_type_iterator gep_type_end(const User *GEP)
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
void computeKnownBitsFromContext(const Value *V, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Merge bits known from context-dependent facts into Known.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, const Instruction *CtxI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &SQ)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
void getGuaranteedWellDefinedOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Compute the possible floating-point classes that LHS could be based on fcmp \Pred LHS,...
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_FMINNUM
Unsigned maximum.
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, unsigned Depth, const SimplifyQuery &Q)
Adjust Known for the given select Arm to include information from the select Cond.
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool programUndefinedIfPoison(const Instruction *Inst)
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given values are known to be non-equal when defined.
ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
gep_type_iterator gep_type_begin(const User *GEP)
std::pair< Value *, FPClassTest > fcmpToClassTest(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static unsigned int semanticsPrecision(const fltSemantics &)
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
This struct is a compact representation of a valid (non-zero power of two) alignment.
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
bool hasConflict() const
Returns true if there is conflicting information.
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
void setAllZero()
Make all bits known to be zero and discard any previous information.
unsigned getBitWidth() const
Get the bit width of this value.
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
bool isConstant() const
Returns true if we know the value of all bits.
void resetAll()
Resets the known state of all bits.
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
bool isNegative() const
Returns true if this value is known to be negative.
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
void setAllOnes()
Make all bits known to be one and discard any previous information.
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
const APInt & getConstant() const
Returns the value when all bits have a known value.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a positive zero.
void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, Type *Ty)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty)
Propagate knowledge from a source value that could be a denormal or zero.
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
SimplifyQuery getWithoutCondContext() const
SimplifyQuery getWithInstruction(const Instruction *I) const
const DomConditionCache * DC