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));
600 CtxIOut =
PHI->getIncomingBlock(*U)->getTerminator();
602// If the Use is a select of this phi, compute analysis on other arm to break 609// Same for select, if this phi is 2-operand phi, compute analysis on other 610// incoming value to break recursion. 611// TODO: We could handle any number of incoming edges as long as we only have 613elseif (
auto *IncPhi = dyn_cast<PHINode>(ValOut);
614 IncPhi && IncPhi->getNumIncomingValues() == 2) {
616if (IncPhi->getIncomingValue(
Idx) ==
PHI) {
617 ValOut = IncPhi->getIncomingValue(1 -
Idx);
618 CtxIOut = IncPhi->getIncomingBlock(1 -
Idx)->getTerminator();
626// Use of assumptions is context-sensitive. If we don't have a context, we 637"Got assumption for the wrong function!");
640if (!V->getType()->isPointerTy())
643 *
I,
I->bundle_op_info_begin()[Elem.Index])) {
645 (RK.AttrKind == Attribute::NonNull ||
646 (RK.AttrKind == Attribute::Dereferenceable &&
648 V->getType()->getPointerAddressSpace()))) &&
655// Warning: This loop can end up being somewhat performance sensitive. 656// We're running this loop for once for each value queried resulting in a 657// runtime of ~O(#assumes * #values). 676// Handle comparison of pointer to null explicitly, as it will not be 677// covered by the m_APInt() logic below. 680case ICmpInst::ICMP_EQ:
683case ICmpInst::ICMP_SGE:
684case ICmpInst::ICMP_SGT:
687case ICmpInst::ICMP_SLT:
705case ICmpInst::ICMP_EQ:
709// assume(V & Mask = C) 712// For one bits in Mask, we can propagate bits from C to V. 715 Known.
Zero |= ~*
C & *Mask;
716// assume(V | Mask = C) 718// For zero bits in Mask, we can propagate bits from C to V. 721 Known.
One |= *
C & ~*Mask;
722// assume(V ^ Mask = C) 725// Equivalent to assume(V == Mask ^ C) 727// assume(V << ShAmt = C) 730// For those bits in C that are known, we can propagate them to known 731// bits in V shifted to the right by ShAmt. 736// assume(V >> ShAmt = C) 740// For those bits in RHS that are known, we can propagate them to known 741// bits in V shifted to the right by C. 742 Known.
Zero |= RHSKnown.
Zero << ShAmt;
743 Known.
One |= RHSKnown.
One << ShAmt;
746case ICmpInst::ICMP_NE: {
747// assume (V & B != 0) where B is a power of 2 762if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
763// X & Y u> C -> X u> C && Y u> C 764// X nuw- Y u> C -> X u> C 768 (*
C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
770if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
771// X | Y u< C -> X u< C && Y u< C 772// X nuw+ Y u< C -> X u< C && Y u< C 776 (*
C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
788 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
792// Handle icmp pred (trunc V), C 821if (
auto *Cmp = dyn_cast<ICmpInst>(
Cond))
827// Handle injected condition. 835// Handle dominating conditions. 857// Note that the patterns below need to be kept in sync with the code 858// in AssumptionCache::updateAffectedValues. 866"Got assumption for the wrong function!");
869if (!V->getType()->isPointerTy())
872 *
I,
I->bundle_op_info_begin()[Elem.Index])) {
873// Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might 874// be the producer of the pointer in the bundle. At the moment, align 875// assumptions aren't optimized away. 876if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
884// Warning: This loop can end up being somewhat performance sensitive. 885// We're running this loop for once for each value queried resulting in a 886// runtime of ~O(#assumes * #values). 888Value *Arg =
I->getArgOperand(0);
904// The remaining tests are all recursive, so bail out if we hit the limit. 908ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
918// Conflicting assumption: Undefined behavior will occur on this execution 924/// Compute known bits from a shift operator, including those with a 925/// non-constant shift amount. Known is the output of this function. Known2 is a 926/// pre-allocated temporary with the same bit width as Known and on return 927/// contains the known bit of the shift value source. KF is an 928/// operator-specific function that, given the known-bits and a shift amount, 929/// compute the implied known-bits of the shift operator's result respectively 930/// for that shift amount. The results from calling KF are conservatively 931/// combined for all permitted shift amounts. 938// To limit compile-time impact, only query isKnownNonZero() if we know at 939// least something about the shift amount. 944 Known = KF(Known2, Known, ShAmtNonZero);
957switch (
I->getOpcode()) {
958case Instruction::And:
959 KnownOut = KnownLHS & KnownRHS;
961// and(x, -x) is common idioms that will clear all but lowest set 962// bit. If we have a single known bit in x, we can clear all bits 964// TODO: instcombine often reassociates independent `and` which can hide 965// this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x). 967// -(-x) == x so using whichever (LHS/RHS) gets us a better result. 969 KnownOut = KnownLHS.
blsi();
971 KnownOut = KnownRHS.
blsi();
975 KnownOut = KnownLHS | KnownRHS;
977case Instruction::Xor:
978 KnownOut = KnownLHS ^ KnownRHS;
979// xor(x, x-1) is common idioms that will clear all but lowest set 980// bit. If we have a single known bit in x, we can clear all bits 982// TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C != 983// -1 but for the purpose of demanded bits (xor(x, x-C) & 984// Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern 985// to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1). 988constKnownBits &XBits =
I->getOperand(0) ==
X ? KnownLHS : KnownRHS;
989 KnownOut = XBits.
blsmsk();
996// and(x, add (x, -1)) is a common idiom that always clears the low bit; 997// xor/or(x, add (x, -1)) is an idiom that will always set the low bit. 998// here we handle the more general case of adding any odd number by 999// matching the form and/xor/or(x, add(x, y)) where y is odd. 1000// TODO: This could be generalized to clearing any bit set in y where the 1001// following bit is known to be unset in y. 1002if (!KnownOut.
Zero[0] && !KnownOut.
One[0] &&
1023APInt DemandedEltsLHS, DemandedEltsRHS;
1025 DemandedElts, DemandedEltsLHS,
1028constauto ComputeForSingleOpFunc =
1030return KnownBitsFunc(
1035if (DemandedEltsRHS.
isZero())
1036return ComputeForSingleOpFunc(
I->getOperand(0), DemandedEltsLHS);
1037if (DemandedEltsLHS.
isZero())
1038return ComputeForSingleOpFunc(
I->getOperand(1), DemandedEltsRHS);
1040return ComputeForSingleOpFunc(
I->getOperand(0), DemandedEltsLHS)
1041 .intersectWith(ComputeForSingleOpFunc(
I->getOperand(1), DemandedEltsRHS));
1044// Public so this can be used in `SimplifyDemandedUseBits`. 1050auto *FVTy = dyn_cast<FixedVectorType>(
I->getType());
1059Attribute Attr =
F->getFnAttribute(Attribute::VScaleRange);
1060// Without vscale_range, we only know that vscale is non-zero. 1065// Minimum is larger than vscale width, result is always poison. 1067return ConstantRange::getEmpty(
BitWidth);
1080// If we have a constant arm, we are done. 1084// See what condition implies about the bits of the select arm. 1087// If we don't get any information from the condition, no reason to 1092// We can have conflict if the condition is dead. I.e if we have 1093// (x | 64) < 32 ? (x | 64) : y 1094// we will have conflict at bit 6 from the condition/the `or`. 1095// In that case just return. Its not particularly important 1096// what we do, as this select is going to be simplified soon. 1101// Finally make sure the information we found is valid. This is relatively 1102// expensive so it's left for the very end. 1106// Finally, we know we get information from the condition and its valid, 1111// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). 1112// Returns the input and lower/upper bounds. 1117"Input should be a Select!");
1127constValue *LHS2 =
nullptr, *RHS2 =
nullptr;
1139return CLow->
sle(*CHigh);
1144constAPInt *&CHigh) {
1145assert((
II->getIntrinsicID() == Intrinsic::smin ||
1146II->getIntrinsicID() == Intrinsic::smax) &&
1147"Must be smin/smax");
1150auto *InnerII = dyn_cast<IntrinsicInst>(
II->getArgOperand(0));
1151if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1156if (
II->getIntrinsicID() == Intrinsic::smin)
1158return CLow->
sle(*CHigh);
1163constAPInt *CLow, *CHigh;
1170constAPInt &DemandedElts,
1176switch (
I->getOpcode()) {
1178case Instruction::Load:
1183case Instruction::And:
1189case Instruction::Or:
1195case Instruction::Xor:
1201case Instruction::Mul: {
1205 DemandedElts, Known, Known2,
Depth, Q);
1208case Instruction::UDiv: {
1215case Instruction::SDiv: {
1222case Instruction::Select: {
1223auto ComputeForArm = [&](
Value *Arm,
bool Invert) {
1229// Only known if known in both the LHS and RHS. 1231 ComputeForArm(
I->getOperand(1),
/*Invert=*/false)
1235case Instruction::FPTrunc:
1236case Instruction::FPExt:
1237case Instruction::FPToUI:
1238case Instruction::FPToSI:
1239case Instruction::SIToFP:
1240case Instruction::UIToFP:
1241break;
// Can't work with floating point. 1242case Instruction::PtrToInt:
1243case Instruction::IntToPtr:
1244// Fall through and handle them the same as zext/trunc. 1246case Instruction::ZExt:
1247case Instruction::Trunc: {
1248Type *SrcTy =
I->getOperand(0)->getType();
1250unsigned SrcBitWidth;
1251// Note that we handle pointer operands here because of inttoptr/ptrtoint 1252// which fall through here. 1258assert(SrcBitWidth &&
"SrcBitWidth can't be zero");
1261if (
auto *Inst = dyn_cast<PossiblyNonNegInst>(
I);
1262 Inst && Inst->hasNonNeg() && !Known.
isNegative())
1267case Instruction::BitCast: {
1268Type *SrcTy =
I->getOperand(0)->getType();
1270// TODO: For now, not handling conversions like: 1271// (bitcast i64 %x to <2 x i32>) 1272 !
I->getType()->isVectorTy()) {
1278// Handle bitcast from floating point to integer. 1280 V->getType()->isFPOrFPVectorTy()) {
1281Type *FPType = V->getType()->getScalarType();
1286// TODO: Treat it as zero/poison if the use of I is unreachable. 1294if (FPClasses &
fcInf)
1306if (Result.SignBit) {
1316// Handle cast from vector integer type to scalar or vector integer. 1317auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1318if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1319 !
I->getType()->isIntOrIntVectorTy() ||
1320 isa<ScalableVectorType>(
I->getType()))
1323// Look through a cast from narrow vector elements to wider type. 1324// Examples: v4i32 -> v2i64, v3i8 -> v24 1325unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1327// Known bits are automatically intersected across demanded elements of a 1328// vector. So for example, if a bit is computed as known zero, it must be 1329// zero across all demanded elements of the vector. 1331// For this bitcast, each demanded element of the output is sub-divided 1332// across a set of smaller vector elements in the source vector. To get 1333// the known bits for an entire element of the output, compute the known 1334// bits for each sub-element sequentially. This is done by shifting the 1335// one-set-bit demanded elements parameter across the sub-elements for 1336// consecutive calls to computeKnownBits. We are using the demanded 1337// elements parameter as a mask operator. 1339// The known bits of each sub-element are then inserted into place 1340// (dependent on endian) to form the full result of known bits. 1342unsigned SubScale =
BitWidth / SubBitWidth;
1344for (
unsigned i = 0; i != NumElts; ++i) {
1346 SubDemandedElts.
setBit(i * SubScale);
1350for (
unsigned i = 0; i != SubScale; ++i) {
1354 Known.
insertBits(KnownSrc, ShiftElt * SubBitWidth);
1359case Instruction::SExt: {
1360// Compute the bits in the result that are not present in the input. 1361unsigned SrcBitWidth =
I->getOperand(0)->getType()->getScalarSizeInBits();
1363 Known = Known.
trunc(SrcBitWidth);
1365// If the sign bit of the input is known set or clear, then we know the 1366// top bits of the result. 1370case Instruction::Shl: {
1379// Trailing zeros of a right-shifted constant never decrease. 1385case Instruction::LShr: {
1386bool Exact = Q.
IIQ.
isExact(cast<BinaryOperator>(
I));
1393// Leading zeros of a left-shifted constant never decrease. 1399case Instruction::AShr: {
1400bool Exact = Q.
IIQ.
isExact(cast<BinaryOperator>(
I));
1409case Instruction::Sub: {
1413 DemandedElts, Known, Known2,
Depth, Q);
1416case Instruction::Add: {
1420 DemandedElts, Known, Known2,
Depth, Q);
1423case Instruction::SRem:
1429case Instruction::URem:
1434case Instruction::Alloca:
1437case Instruction::GetElementPtr: {
1438// Analyze all of the subscripts of this getelementptr instruction 1439// to determine if we can prove known low zero bits. 1441// Accumulate the constant indices in a separate variable 1442// to minimize the number of calls to computeForAddSub. 1446for (
unsigned i = 1, e =
I->getNumOperands(); i != e; ++i, ++GTI) {
1447// TrailZ can only become smaller, short-circuit if we hit zero. 1451Value *Index =
I->getOperand(i);
1453// Handle case when index is zero. 1454Constant *CIndex = dyn_cast<Constant>(Index);
1459// Handle struct member offset arithmetic. 1462"Access to structure field must be known at compile time");
1467unsignedIdx = cast<ConstantInt>(Index)->getZExtValue();
1470 AccConstIndices +=
Offset;
1474// Handle array index arithmetic. 1481unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1487// Multiply by current sizeof type. 1488// &A[i] == A + i * sizeof(*A[i]). 1490// For scalable types the only thing we know about sizeof is 1491// that this is a multiple of the minimum size. 1495APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1496 IndexConst *= ScalingFactor;
1505// If the offsets have a different width from the pointer, according 1506// to the language reference we need to sign-extend or truncate them 1507// to the width of the pointer. 1510// Note that inbounds does *not* guarantee nsw for the addition, as only 1511// the offset is signed, while the base address is unsigned. 1520case Instruction::PHI: {
1523Value *R =
nullptr, *L =
nullptr;
1525// Handle the case of a simple two-predecessor recurrence PHI. 1526// There's a lot more that could theoretically be done here, but 1527// this is sufficient to catch some interesting cases. 1531// If this is a shift recurrence, we know the bits being shifted in. We 1532// can combine that with information about the start value of the 1533// recurrence to conclude facts about the result. If this is a udiv 1534// recurrence, we know that the result can never exceed either the 1535// numerator or the start value, whichever is greater. 1536case Instruction::LShr:
1537case Instruction::AShr:
1538case Instruction::Shl:
1539case Instruction::UDiv:
1544// For a urem recurrence, the result can never exceed the start value. The 1545// phi could either be the numerator or the denominator. 1546case Instruction::URem: {
1547// We have matched a recurrence of the form: 1548// %iv = [R, %entry], [%iv.next, %backedge] 1549// %iv.next = shift_op %iv, L 1551// Recurse with the phi context to avoid concern about whether facts 1552// inferred hold at original context instruction. TODO: It may be 1553// correct to use the original context. IF warranted, explore and 1554// add sufficient tests to cover. 1559case Instruction::Shl:
1560// A shl recurrence will only increase the tailing zeros 1563case Instruction::LShr:
1564case Instruction::UDiv:
1565case Instruction::URem:
1566// lshr, udiv, and urem recurrences will preserve the leading zeros of 1570case Instruction::AShr:
1571// An ashr recurrence will extend the initial sign bit 1579// Check for operations that have the property that if 1580// both their operands have low zero bits, the result 1581// will have low zero bits. 1582case Instruction::Add:
1583case Instruction::Sub:
1584case Instruction::And:
1585case Instruction::Or:
1586case Instruction::Mul: {
1587// Change the context instruction to the "edge" that flows into the 1588// phi. This is important because that is where the value is actually 1589// "evaluated" even though it is used later somewhere else. (see also 1593unsigned OpNum =
P->getOperand(0) == R ? 0 : 1;
1594Instruction *RInst =
P->getIncomingBlock(OpNum)->getTerminator();
1595Instruction *LInst =
P->getIncomingBlock(1 - OpNum)->getTerminator();
1597// Ok, we have a PHI of the form L op= R. Check for low 1602// We need to take the minimum number of known bits 1610auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1615// If initial value of recurrence is nonnegative, and we are adding 1616// a nonnegative number with nsw, the result can only be nonnegative 1617// or poison value regardless of the number of times we execute the 1618// add in phi recurrence. If initial value is negative and we are 1619// adding a negative number with nsw, the result can only be 1620// negative or poison value. Similar arguments apply to sub and mul. 1622// (add non-negative, non-negative) --> non-negative 1623// (add negative, negative) --> negative 1624case Instruction::Add: {
1632// (sub nsw non-negative, negative) --> non-negative 1633// (sub nsw negative, non-negative) --> negative 1634case Instruction::Sub: {
1644// (mul nsw non-negative, non-negative) --> non-negative 1645case Instruction::Mul:
1661// Unreachable blocks may have zero-operand PHI nodes. 1662if (
P->getNumIncomingValues() == 0)
1665// Otherwise take the unions of the known bit sets of the operands, 1666// taking conservative care to avoid excessive recursion. 1668// Skip if every incoming value references to ourself. 1669if (isa_and_nonnull<UndefValue>(
P->hasConstantValue()))
1674for (
constUse &U :
P->operands()) {
1678// Skip direct self references. 1682// Change the context instruction to the "edge" that flows into the 1683// phi. This is important because that is where the value is actually 1684// "evaluated" even though it is used later somewhere else. (see also 1690// Recurse, but cap the recursion to one level, because we don't 1691// want to waste time spinning around in loops. 1692// TODO: See if we can base recursion limiter on number of incoming phi 1693// edges so we don't overly clamp analysis. 1697// See if we can further use a conditional branch into the phi 1698// to help us determine the range of the value. 1703// TODO: Use RHS Value and compute range from its known bits. 1707// Check for cases of duplicate successors. 1708if ((TrueSucc ==
P->getParent()) != (FalseSucc ==
P->getParent())) {
1709// If we're using the false successor, invert the predicate. 1710if (FalseSucc ==
P->getParent())
1712// Get the knownbits implied by the incoming phi condition. 1715// We can have conflicts here if we are analyzing deadcode (its 1716// impossible for us reach this BB based the icmp). 1718// No reason to continue analyzing in a known dead region, so 1719// just resetAll and break. This will cause us to also exit the 1724 Known2 = KnownUnion;
1730// If all bits have been ruled out, there's no need to check 1738case Instruction::Call:
1739case Instruction::Invoke: {
1740// If range metadata is attached to this call, set known bits from that, 1741// and then intersect with known bits based on other properties of the 1747constauto *CB = cast<CallBase>(
I);
1749if (std::optional<ConstantRange>
Range = CB->getRange())
1752if (
constValue *RV = CB->getReturnedArgOperand()) {
1753if (RV->getType() ==
I->getType()) {
1756// If the function doesn't return properly for all input values 1757// (e.g. unreachable exits) then there might be conflicts between the 1758// argument value and the range metadata. Simply discard the known bits 1759// in case of conflicts. 1765switch (
II->getIntrinsicID()) {
1768case Intrinsic::abs: {
1770bool IntMinIsPoison =
match(
II->getArgOperand(1),
m_One());
1771 Known = Known2.
abs(IntMinIsPoison);
1774case Intrinsic::bitreverse:
1779case Intrinsic::bswap:
1784case Intrinsic::ctlz: {
1786// If we have a known 1, its position is our upper bound. 1788// If this call is poison for 0 input, the result will be less than 2^n. 1790 PossibleLZ = std::min(PossibleLZ,
BitWidth - 1);
1795case Intrinsic::cttz: {
1797// If we have a known 1, its position is our upper bound. 1799// If this call is poison for 0 input, the result will be less than 2^n. 1801 PossibleTZ = std::min(PossibleTZ,
BitWidth - 1);
1806case Intrinsic::ctpop: {
1808// We can bound the space the count needs. Also, bits known to be zero 1809// can't contribute to the population. 1813// TODO: we could bound KnownOne using the lower bound on the number 1814// of bits which might be set provided by popcnt KnownOne2. 1817case Intrinsic::fshr:
1818case Intrinsic::fshl: {
1823// Normalize to funnel shift left. 1825if (
II->getIntrinsicID() == Intrinsic::fshr)
1838case Intrinsic::uadd_sat:
1843case Intrinsic::usub_sat:
1848case Intrinsic::sadd_sat:
1853case Intrinsic::ssub_sat:
1858// Vec reverse preserves bits from input vec. 1859case Intrinsic::vector_reverse:
1863// for min/max/and/or reduce, any bit common to each element in the 1864// input vec is set in the output. 1865case Intrinsic::vector_reduce_and:
1866case Intrinsic::vector_reduce_or:
1867case Intrinsic::vector_reduce_umax:
1868case Intrinsic::vector_reduce_umin:
1869case Intrinsic::vector_reduce_smax:
1870case Intrinsic::vector_reduce_smin:
1873case Intrinsic::vector_reduce_xor: {
1875// The zeros common to all vecs are zero in the output. 1876// If the number of elements is odd, then the common ones remain. If the 1877// number of elements is even, then the common ones becomes zeros. 1878auto *VecTy = cast<VectorType>(
I->getOperand(0)->getType());
1879// Even, so the ones become zeros. 1880bool EvenCnt = VecTy->getElementCount().isKnownEven();
1883// Maybe even element count so need to clear ones. 1884if (VecTy->isScalableTy() || EvenCnt)
1888case Intrinsic::umin:
1893case Intrinsic::umax:
1898case Intrinsic::smin:
1904case Intrinsic::smax:
1910case Intrinsic::ptrmask: {
1913constValue *Mask =
I->getOperand(1);
1914 Known2 =
KnownBits(Mask->getType()->getScalarSizeInBits());
1916// TODO: 1-extend would be more precise. 1920case Intrinsic::x86_sse2_pmulh_w:
1921case Intrinsic::x86_avx2_pmulh_w:
1922case Intrinsic::x86_avx512_pmulh_w_512:
1927case Intrinsic::x86_sse2_pmulhu_w:
1928case Intrinsic::x86_avx2_pmulhu_w:
1929case Intrinsic::x86_avx512_pmulhu_w_512:
1934case Intrinsic::x86_sse42_crc32_64_64:
1937case Intrinsic::x86_ssse3_phadd_d_128:
1938case Intrinsic::x86_ssse3_phadd_w_128:
1939case Intrinsic::x86_avx2_phadd_d:
1940case Intrinsic::x86_avx2_phadd_w: {
1948case Intrinsic::x86_ssse3_phadd_sw_128:
1949case Intrinsic::x86_avx2_phadd_sw: {
1954case Intrinsic::x86_ssse3_phsub_d_128:
1955case Intrinsic::x86_ssse3_phsub_w_128:
1956case Intrinsic::x86_avx2_phsub_d:
1957case Intrinsic::x86_avx2_phsub_w: {
1965case Intrinsic::x86_ssse3_phsub_sw_128:
1966case Intrinsic::x86_avx2_phsub_sw: {
1971case Intrinsic::riscv_vsetvli:
1972case Intrinsic::riscv_vsetvlimax: {
1973bool HasAVL =
II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1976 cast<ConstantInt>(
II->getArgOperand(HasAVL))->getZExtValue());
1978 cast<ConstantInt>(
II->getArgOperand(1 + HasAVL))->getZExtValue());
1983// Result of vsetvli must be not larger than AVL. 1985if (
auto *CI = dyn_cast<ConstantInt>(
II->getArgOperand(0)))
1986 MaxVL = std::min(MaxVL, CI->getZExtValue());
1988unsigned KnownZeroFirstBit =
Log2_32(MaxVL) + 1;
1993case Intrinsic::vscale: {
1994if (!
II->getParent() || !
II->getFunction())
2004case Instruction::ShuffleVector: {
2005auto *Shuf = dyn_cast<ShuffleVectorInst>(
I);
2006// FIXME: Do we need to handle ConstantExpr involving shufflevectors? 2011// For undef elements, we don't know anything about the common state of 2012// the shuffle result. 2013APInt DemandedLHS, DemandedRHS;
2021constValue *
LHS = Shuf->getOperand(0);
2023// If we don't know any bits, early out. 2028constValue *
RHS = Shuf->getOperand(1);
2034case Instruction::InsertElement: {
2035if (isa<ScalableVectorType>(
I->getType())) {
2039constValue *Vec =
I->getOperand(0);
2040constValue *Elt =
I->getOperand(1);
2041auto *CIdx = dyn_cast<ConstantInt>(
I->getOperand(2));
2043APInt DemandedVecElts = DemandedElts;
2045// If we know the index we are inserting too, clear it from Vec check. 2046if (CIdx && CIdx->getValue().ult(NumElts)) {
2047 DemandedVecElts.
clearBit(CIdx->getZExtValue());
2048 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2055// If we don't know any bits, early out. 2060if (!DemandedVecElts.
isZero()) {
2066case Instruction::ExtractElement: {
2067// Look through extract element. If the index is non-constant or 2068// out-of-range demand all elements, otherwise just the extracted element. 2069constValue *Vec =
I->getOperand(0);
2071auto *CIdx = dyn_cast<ConstantInt>(
Idx);
2072if (isa<ScalableVectorType>(Vec->
getType())) {
2073// FIXME: there's probably *something* we can do with scalable vectors 2077unsigned NumElts = cast<FixedVectorType>(Vec->
getType())->getNumElements();
2079if (CIdx && CIdx->getValue().ult(NumElts))
2084case Instruction::ExtractValue:
2089switch (
II->getIntrinsicID()) {
2091case Intrinsic::uadd_with_overflow:
2092case Intrinsic::sadd_with_overflow:
2094true,
II->getArgOperand(0),
II->getArgOperand(1),
/*NSW=*/false,
2095/* NUW=*/false, DemandedElts, Known, Known2,
Depth, Q);
2097case Intrinsic::usub_with_overflow:
2098case Intrinsic::ssub_with_overflow:
2100false,
II->getArgOperand(0),
II->getArgOperand(1),
/*NSW=*/false,
2101/* NUW=*/false, DemandedElts, Known, Known2,
Depth, Q);
2103case Intrinsic::umul_with_overflow:
2104case Intrinsic::smul_with_overflow:
2106false, DemandedElts, Known, Known2,
Depth, Q);
2112case Instruction::Freeze:
2120/// Determine which bits of V are known to be either zero or one and return 2129/// Determine which bits of V are known to be either zero or one and return 2138/// Determine which bits of V are known to be either zero or one and return 2139/// them in the Known bit set. 2141/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that 2142/// we cannot optimize based on the assumption that it is zero without changing 2143/// it to be an explicit zero. If we don't change it to zero, other code could 2144/// optimized based on the contradictory assumption that it is non-zero. 2145/// Because instcombine aggressively folds operations with undef args anyway, 2146/// this won't lose us code quality. 2148/// This function is defined on values with integer type, values with pointer 2149/// type, and vectors of integers. In the case 2150/// where V is a vector, known zero, and known one values are the 2151/// same width as the vector element, and the bit is set only if it is true 2152/// for all of the demanded elements in the vector specified by DemandedElts. 2157// No demanded elts, better to assume we don't know anything. 2166Type *Ty = V->getType();
2170"Not integer or pointer type!");
2172if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2174 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
2175"DemandedElt width should equal the fixed vector number of elements");
2178"DemandedElt width should be 1 for scalars or scalable vectors");
2184"V and Known should have same BitWidth");
2187"V and Known should have same BitWidth");
2193// We know all of the bits for a scalar constant or a splat vector constant! 2197// Null and aggregate-zero are all-zeros. 2198if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2202// Handle a constant vector by taking the intersection of the known bits of 2205assert(!isa<ScalableVectorType>(V->getType()));
2206// We know that CDV must be a vector of integers. Take the intersection of 2209for (
unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2210if (!DemandedElts[i])
2212APInt Elt = CDV->getElementAsAPInt(i);
2221if (
constauto *CV = dyn_cast<ConstantVector>(V)) {
2222assert(!isa<ScalableVectorType>(V->getType()));
2223// We know that CV must be a vector of integers. Take the intersection of 2226for (
unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2227if (!DemandedElts[i])
2230if (isa<PoisonValue>(Element))
2232auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2237constAPInt &Elt = ElementCI->getValue();
2246// Start out not knowing anything. 2249// We can't imply anything about undefs. 2250if (isa<UndefValue>(V))
2253// There's no point in looking through other users of ConstantData for 2254// assumptions. Confirm that we've handled them all. 2255assert(!isa<ConstantData>(V) &&
"Unhandled constant data!");
2257if (
constauto *
A = dyn_cast<Argument>(V))
2258if (std::optional<ConstantRange>
Range =
A->getRange())
2261// All recursive calls that increase depth must come after this. 2265// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has 2266// the bits of its aliasee. 2267if (
constGlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2268if (!GA->isInterposable())
2273if (
constOperator *
I = dyn_cast<Operator>(V))
2275elseif (
constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2276if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2277 Known = CR->toKnownBits();
2280// Aligned pointers have trailing zeros - refine Known.Zero set 2281if (isa<PointerType>(V->getType())) {
2282Align Alignment = V->getPointerAlignment(Q.
DL);
2286// computeKnownBitsFromContext strictly refines Known. 2287// Therefore, we run them after computeKnownBitsFromOperator. 2289// Check whether we can determine known bits from context such as assumes. 2293/// Try to detect a recurrence that the value of the induction variable is 2294/// always a power of two (or zero). 2298Value *Start =
nullptr, *Step =
nullptr;
2302// Initial value must be a power of two. 2304if (U.get() == Start) {
2305// Initial value comes from a different BB, need to adjust context 2306// instruction for analysis. 2313// Except for Mul, the induction variable must be on the left side of the 2314// increment expression, otherwise its value can be arbitrary. 2320case Instruction::Mul:
2321// Power of two is closed under multiplication. 2325case Instruction::SDiv:
2326// Start value must not be signmask for signed division, so simply being a 2327// power of two is not sufficient, and it has to be a constant. 2331case Instruction::UDiv:
2332// Divisor must be a power of two. 2333// If OrZero is false, cannot guarantee induction variable is non-zero after 2334// division, same for Shr, unless it is exact division. 2337case Instruction::Shl:
2339case Instruction::AShr:
2343case Instruction::LShr:
2350/// Return true if we can infer that \p V is known to be a power of 2 from 2351/// dominating condition \p Cond (e.g., ctpop(V) == 1). 2361 Pred = ICmpInst::getInversePredicate(Pred);
2363if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2366return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2369/// Return true if the given value is known to have exactly one 2370/// bit set when defined. For vectors return true if every element is known to 2371/// be a power of two when defined. Supports values with integer or pointer 2372/// types and vectors of integers. 2377if (isa<Constant>(V))
2380// i1 is by definition a power of 2 or zero. 2381if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2384// Try to infer from assumptions. 2391/*CondIsTrue=*/true) &&
2397// Handle dominating conditions. 2404/*CondIsTrue=*/true) &&
2410/*CondIsTrue=*/false) &&
2416auto *
I = dyn_cast<Instruction>(V);
2422// The vscale_range indicates vscale is a power-of-two. 2423returnF->hasFnAttribute(Attribute::VScaleRange);
2426// 1 << X is clearly a power of two if the one is not shifted off the end. If 2427// it is shifted off the end then the result is undefined. 2431// (signmask) >>l X is clearly a power of two if the one is not shifted off 2432// the bottom. If it is shifted off the bottom then the result is undefined. 2436// The remaining tests are all recursive, so bail out if we hit the limit. 2440switch (
I->getOpcode()) {
2441case Instruction::ZExt:
2443case Instruction::Trunc:
2445case Instruction::Shl:
2449case Instruction::LShr:
2450if (OrZero || Q.
IIQ.
isExact(cast<BinaryOperator>(
I)))
2453case Instruction::UDiv:
2457case Instruction::Mul:
2461case Instruction::And:
2462// A power of two and'd with anything is a power of two or zero. 2467// X & (-X) is always a power of two or zero. 2472case Instruction::Add: {
2473// Adding a power-of-two or zero to the same power-of-two or zero yields 2474// either the original power-of-two, a larger power-of-two or zero. 2478if (
match(
I->getOperand(0),
2482if (
match(
I->getOperand(1),
2487unsignedBitWidth = V->getType()->getScalarSizeInBits();
2493// If i8 V is a power of two or zero: 2494// ZeroBits: 1 1 1 0 1 1 1 1 2495// ~ZeroBits: 0 0 0 1 0 0 0 0 2496if ((~(LHSBits.
Zero & RHSBits.
Zero)).isPowerOf2())
2497// If OrZero isn't set, we cannot give back a zero result. 2498// Make sure either the LHS or RHS has a bit set. 2503// LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero. 2509case Instruction::Select:
2512case Instruction::PHI: {
2513// A PHI node is power of two if all incoming values are power of two, or if 2514// it is an induction variable where in each step its value is a power of 2516auto *PN = cast<PHINode>(
I);
2519// Check if it is an induction variable and always power of two. 2523// Recursively check all incoming values. Limit recursion to 2 levels, so 2524// that search complexity is limited to number of operands^2. 2527// Value is power of 2 if it is coming from PHI node itself by induction. 2531// Change the context instruction to the incoming block where it is 2533 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2534 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2537case Instruction::Invoke:
2538case Instruction::Call: {
2539if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
2540switch (
II->getIntrinsicID()) {
2541case Intrinsic::umax:
2542case Intrinsic::smax:
2543case Intrinsic::umin:
2544case Intrinsic::smin:
2547// bswap/bitreverse just move around bits, but don't change any 1s/0s 2548// thus dont change pow2/non-pow2 status. 2549case Intrinsic::bitreverse:
2550case Intrinsic::bswap:
2552case Intrinsic::fshr:
2553case Intrinsic::fshl:
2554// If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x) 2555if (
II->getArgOperand(0) ==
II->getArgOperand(1))
2569/// Test whether a GEP's result is known to be non-null. 2571/// Uses properties inherent in a GEP to try to determine whether it is known 2574/// Currently this routine does not support vector GEPs. 2581// If the gep is nuw or inbounds with invalid null pointer, then the GEP 2582// may be null iff the base pointer is null and the offset is zero. 2583if (!
GEP->hasNoUnsignedWrap() &&
2584 !(
GEP->isInBounds() &&
2588// FIXME: Support vector-GEPs. 2589assert(
GEP->getType()->isPointerTy() &&
"We only support plain pointer GEP");
2591// If the base pointer is non-null, we cannot walk to a null address with an 2592// inbounds GEP in address space zero. 2596// Walk the GEP operands and see if any operand introduces a non-zero offset. 2597// If so, then the GEP cannot produce a null pointer, as doing so would 2598// inherently violate the inbounds contract within address space zero. 2600 GTI != GTE; ++GTI) {
2601// Struct types are easy -- they must always be indexed by a constant. 2602if (
StructType *STy = GTI.getStructTypeOrNull()) {
2603ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2607if (ElementOffset > 0)
2612// If we have a zero-sized type, the index doesn't matter. Keep looping. 2613if (GTI.getSequentialElementStride(Q.
DL).isZero())
2616// Fast path the constant operand case both for efficiency and so we don't 2617// increment Depth when just zipping down an all-constant GEP. 2618if (
ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2624// We post-increment Depth here because while isKnownNonZero increments it 2625// as well, when we pop back up that increment won't persist. We don't want 2626// to recurse 10k times just because we have 10k GEP operands. We don't 2627// bail completely out because we want to handle constant GEPs regardless 2642assert(!isa<Constant>(V) &&
"Called for constant?");
2647unsigned NumUsesExplored = 0;
2648for (
constauto *U : V->users()) {
2649// Avoid massive lists 2654// If the value is used as an argument to a call or invoke, then argument 2655// attributes may provide an answer about null-ness. 2656if (
constauto *CB = dyn_cast<CallBase>(U))
2657if (
auto *CalledFunc = CB->getCalledFunction())
2658for (
constArgument &Arg : CalledFunc->args())
2659if (CB->getArgOperand(Arg.getArgNo()) == V &&
2660 Arg.hasNonNullAttr(
/* AllowUndefOrPoison */false) &&
2664// If the value is used as a load/store, then the pointer must be non null. 2668 V->getType()->getPointerAddressSpace()) &&
2678// Consider only compare instructions uniquely controlling a branch 2686 NonNullIfTrue =
true;
2688 NonNullIfTrue =
false;
2694for (
constauto *CmpU : U->users()) {
2696if (Visited.
insert(CmpU).second)
2699while (!WorkList.
empty()) {
2702// If a user is an AND, add all its users to the work list. We only 2703// propagate "pred != null" condition through AND because it is only 2704// correct to assume that all conditions of AND are met in true branch. 2705// TODO: Support similar logic of OR and EQ predicate? 2708for (
constauto *CurrU : Curr->users())
2709if (Visited.
insert(CurrU).second)
2714if (
constBranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2715assert(BI->isConditional() &&
"uses a comparison!");
2718 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2722 }
elseif (NonNullIfTrue &&
isGuard(Curr) &&
2723 DT->
dominates(cast<Instruction>(Curr), CtxI)) {
2733/// Does the 'Range' metadata (which must be a valid MD_range operand list) 2734/// ensure that the value it's attached to is never Value? 'RangeType' is 2735/// is the type of the value described by the range. 2737constunsigned NumRanges = Ranges->getNumOperands() / 2;
2739for (
unsigned i = 0; i < NumRanges; ++i) {
2741 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2743 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2751/// Try to detect a recurrence that monotonically increases/decreases from a 2752/// non-zero starting value. These are common as induction variables. 2755Value *Start =
nullptr, *Step =
nullptr;
2756constAPInt *StartC, *StepC;
2762case Instruction::Add:
2763// Starting from non-zero and stepping away from zero can never wrap back 2768case Instruction::Mul:
2771case Instruction::Shl:
2773case Instruction::AShr:
2774case Instruction::LShr:
2790Value *
Y,
bool NSW,
bool NUW) {
2791// (X + (X != 0)) is non zero 2802// If X and Y are both non-negative (as signed values) then their sum is not 2803// zero unless both X and Y are zero. 2809// If X and Y are both negative (as signed values) then their sum is not 2810// zero unless both X and Y equal INT_MIN. 2813// The sign bit of X is set. If some other bit is set then X is not equal 2817// The sign bit of Y is set. If some other bit is set then Y is not equal 2823// The sum of a non-negative number and a power of two is not zero. 2837// (X - (X != 0)) is non zero 2838// ((X != 0) - X) is non zero 2842// TODO: Move this case into isKnownNonEqual(). 2843if (
auto *
C = dyn_cast<Constant>(
X))
2847 return ::isKnownNonEqual(
X,
Y, DemandedElts,
Depth, Q);
2852Value *
Y,
bool NSW,
bool NUW) {
2853// If X and Y are non-zero then so is X * Y as long as the multiplication 2854// does not overflow. 2859// If either X or Y is odd, then if the other is non-zero the result can't 2869// If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is 2870// non-zero, then X * Y is non-zero. We can find sX and sY by just taking 2871// the lowest known One of X and Y. If they are non-zero, the result 2872// must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing 2873// X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth. 2881auto ShiftOp = [&](
constAPInt &Lhs,
constAPInt &Rhs) {
2882switch (
I->getOpcode()) {
2883case Instruction::Shl:
2885case Instruction::LShr:
2886return Lhs.
lshr(Rhs);
2887case Instruction::AShr:
2888return Lhs.
ashr(Rhs);
2894auto InvShiftOp = [&](
constAPInt &Lhs,
constAPInt &Rhs) {
2895switch (
I->getOpcode()) {
2896case Instruction::Shl:
2897return Lhs.
lshr(Rhs);
2898case Instruction::LShr:
2899case Instruction::AShr:
2913if (MaxShift.
uge(NumBits))
2916if (!ShiftOp(KnownVal.
One, MaxShift).isZero())
2919// If all of the bits shifted out are known to be zero, and Val is known 2920// non-zero then at least one non-zero bit must remain. 2921if (InvShiftOp(KnownVal.
Zero, NumBits - MaxShift)
2930constAPInt &DemandedElts,
2933switch (
I->getOpcode()) {
2934case Instruction::Alloca:
2935// Alloca never returns null, malloc might. 2936returnI->getType()->getPointerAddressSpace() == 0;
2937case Instruction::GetElementPtr:
2938if (
I->getType()->isPointerTy())
2941case Instruction::BitCast: {
2942// We need to be a bit careful here. We can only peek through the bitcast 2943// if the scalar size of elements in the operand are smaller than and a 2944// multiple of the size they are casting too. Take three cases: 2947// bitcast <2 x i16> %NonZero to <4 x i8> 2949// %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a 2950// <4 x i8> requires that all 4 i8 elements be non-zero which isn't 2951// guranteed (imagine just sign bit set in the 2 i16 elements). 2954// bitcast <4 x i3> %NonZero to <3 x i4> 2956// Even though the scalar size of the src (`i3`) is smaller than the 2957// scalar size of the dst `i4`, because `i3` is not a multiple of `i4` 2958// its possible for the `3 x i4` elements to be zero because there are 2959// some elements in the destination that don't contain any full src 2963// bitcast <4 x i8> %NonZero to <2 x i16> 2965// This is always safe as non-zero in the 4 i8 elements implies 2966// non-zero in the combination of any two adjacent ones. Since i8 is a 2967// multiple of i16, each i16 is guranteed to have 2 full i8 elements. 2968// This all implies the 2 i16 elements are non-zero. 2969Type *FromTy =
I->getOperand(0)->getType();
2974case Instruction::IntToPtr:
2975// Note that we have to take special care to avoid looking through 2976// truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well 2977// as casts that can alter the value, e.g., AddrSpaceCasts. 2978if (!isa<ScalableVectorType>(
I->getType()) &&
2983case Instruction::PtrToInt:
2984// Similar to int2ptr above, we can look through ptr2int here if the cast 2985// is a no-op or an extend and not a truncate. 2986if (!isa<ScalableVectorType>(
I->getType()) &&
2991case Instruction::Trunc:
2992// nuw/nsw trunc preserves zero/non-zero status of input. 2993if (
auto *TI = dyn_cast<TruncInst>(
I))
2994if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
2998case Instruction::Sub:
3001case Instruction::Xor:
3002// (X ^ (X != 0)) is non zero 3006case Instruction::Or:
3007// (X | (X != 0)) is non zero 3010// X | Y != 0 if X != 0 or Y != 0. 3013case Instruction::SExt:
3014case Instruction::ZExt:
3015// ext X != 0 if X != 0. 3018case Instruction::Shl: {
3019// shl nsw/nuw can't remove any non-zero bits. 3024// shl X, Y != 0 if X is odd. Note that the value of the shift is undefined 3025// if the lowest bit is shifted off the end. 3033case Instruction::LShr:
3034case Instruction::AShr: {
3035// shr exact can only shift out zero bits. 3040// shr X, Y != 0 if X is negative. Note that the value of the shift is not 3041// defined if the sign bit is shifted off the end. 3049case Instruction::UDiv:
3050case Instruction::SDiv: {
3052// div exact can only produce a zero if the dividend is zero. 3053if (cast<PossiblyExactOperator>(
I)->isExact())
3058// If X is fully unknown we won't be able to figure anything out so don't 3059// both computing knownbits for Y. 3065if (
I->getOpcode() == Instruction::SDiv) {
3066// For signed division need to compare abs value of the operands. 3067 XKnown = XKnown.
abs(
/*IntMinIsPoison*/false);
3068 YKnown = YKnown.
abs(
/*IntMinIsPoison*/false);
3070// If X u>= Y then div is non zero (0/0 is UB). 3072// If X is total unknown or X u< Y we won't be able to prove non-zero 3073// with compute known bits so just return early. 3074return XUgeY && *XUgeY;
3076case Instruction::Add: {
3079// If Add has nuw wrap flag, then if either X or Y is non-zero the result is 3081auto *BO = cast<OverflowingBinaryOperator>(
I);
3086case Instruction::Mul: {
3092case Instruction::Select: {
3093// (C ? X : Y) != 0 if X != 0 and Y != 0. 3095// First check if the arm is non-zero using `isKnownNonZero`. If that fails, 3096// then see if the select condition implies the arm is non-zero. For example 3097// (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is 3098// dominated by `X != 0`. 3099auto SelectArmIsNonZero = [&](
bool IsTrueArm) {
3101Op = IsTrueArm ?
I->getOperand(1) :
I->getOperand(2);
3102// Op is trivially non-zero. 3106// The condition of the select dominates the true/false arm. Check if the 3107// condition implies that a given arm is non-zero. 3114 Pred = ICmpInst::getInversePredicate(Pred);
3119if (SelectArmIsNonZero(
/* IsTrueArm */true) &&
3120 SelectArmIsNonZero(
/* IsTrueArm */false))
3124case Instruction::PHI: {
3125auto *PN = cast<PHINode>(
I);
3129// Check if all incoming values are non-zero using recursion. 3135 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3136// Check if the branch on the phi excludes zero. 3139 BasicBlock *TrueSucc, *FalseSucc;
3140 if (match(RecQ.CxtI,
3141 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3142 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3143// Check for cases of duplicate successors. 3144 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3145// If we're using the false successor, invert the predicate. 3146 if (FalseSucc == PN->getParent())
3147 Pred = CmpInst::getInversePredicate(Pred);
3148 if (cmpExcludesZero(Pred, X))
3152// Finally recurse on the edge and check it directly. 3156case Instruction::InsertElement: {
3157if (isa<ScalableVectorType>(
I->getType()))
3160constValue *Vec =
I->getOperand(0);
3161constValue *Elt =
I->getOperand(1);
3162auto *CIdx = dyn_cast<ConstantInt>(
I->getOperand(2));
3165APInt DemandedVecElts = DemandedElts;
3167// If we know the index we are inserting too, clear it from Vec check. 3168if (CIdx && CIdx->getValue().ult(NumElts)) {
3169 DemandedVecElts.
clearBit(CIdx->getZExtValue());
3170 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3173// Result is zero if Elt is non-zero and rest of the demanded elts in Vec 3176 (DemandedVecElts.
isZero() ||
3179case Instruction::ExtractElement:
3180if (
constauto *EEI = dyn_cast<ExtractElementInst>(
I)) {
3181constValue *Vec = EEI->getVectorOperand();
3182constValue *
Idx = EEI->getIndexOperand();
3183auto *CIdx = dyn_cast<ConstantInt>(
Idx);
3184if (
auto *VecTy = dyn_cast<FixedVectorType>(Vec->
getType())) {
3185unsigned NumElts = VecTy->getNumElements();
3187if (CIdx && CIdx->getValue().ult(NumElts))
3193case Instruction::ShuffleVector: {
3194auto *Shuf = dyn_cast<ShuffleVectorInst>(
I);
3197APInt DemandedLHS, DemandedRHS;
3198// For undef elements, we don't know anything about the common state of 3199// the shuffle result. 3202// If demanded elements for both vecs are non-zero, the shuffle is non-zero. 3203return (DemandedRHS.
isZero() ||
3208case Instruction::Freeze:
3212case Instruction::Load: {
3213auto *LI = cast<LoadInst>(
I);
3214// A Load tagged with nonnull or dereferenceable with null pointer undefined 3216if (
auto *PtrT = dyn_cast<PointerType>(
I->getType())) {
3225// No need to fall through to computeKnownBits as range metadata is already 3226// handled in isKnownNonZero. 3229case Instruction::ExtractValue: {
3235case Instruction::Add:
3240case Instruction::Sub:
3243case Instruction::Mul:
3246/*NSW=*/false,
/*NUW=*/false);
3252case Instruction::Call:
3253case Instruction::Invoke: {
3254constauto *Call = cast<CallBase>(
I);
3255if (
I->getType()->isPointerTy()) {
3256if (Call->isReturnNonNull())
3263if (std::optional<ConstantRange>
Range = Call->getRange()) {
3268if (
constValue *RV = Call->getReturnedArgOperand())
3273if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
3274switch (
II->getIntrinsicID()) {
3275case Intrinsic::sshl_sat:
3276case Intrinsic::ushl_sat:
3278case Intrinsic::bitreverse:
3279case Intrinsic::bswap:
3280case Intrinsic::ctpop:
3282// NB: We don't do usub_sat here as in any case we can prove its 3283// non-zero, we will fold it to `sub nuw` in InstCombine. 3284case Intrinsic::ssub_sat:
3286II->getArgOperand(0),
II->getArgOperand(1));
3287case Intrinsic::sadd_sat:
3289II->getArgOperand(0),
II->getArgOperand(1),
3290/*NSW=*/true,
/* NUW=*/false);
3291// Vec reverse preserves zero/non-zero status from input vec. 3292case Intrinsic::vector_reverse:
3295// umin/smin/smax/smin/or of all non-zero elements is always non-zero. 3296case Intrinsic::vector_reduce_or:
3297case Intrinsic::vector_reduce_umax:
3298case Intrinsic::vector_reduce_umin:
3299case Intrinsic::vector_reduce_smax:
3300case Intrinsic::vector_reduce_smin:
3302case Intrinsic::umax:
3303case Intrinsic::uadd_sat:
3304// umax(X, (X != 0)) is non zero 3305// X +usat (X != 0) is non zero 3311case Intrinsic::smax: {
3312// If either arg is strictly positive the result is non-zero. Otherwise 3313// the result is non-zero if both ops are non-zero. 3314auto IsNonZero = [&](
Value *
Op, std::optional<bool> &OpNonZero,
3316if (!OpNonZero.has_value())
3317 OpNonZero = OpKnown.isNonZero() ||
3321// Avoid re-computing isKnownNonZero. 3322 std::optional<bool> Op0NonZero, Op1NonZero;
3326 IsNonZero(
II->getArgOperand(1), Op1NonZero, Op1Known))
3331 IsNonZero(
II->getArgOperand(0), Op0NonZero, Op0Known))
3333return IsNonZero(
II->getArgOperand(1), Op1NonZero, Op1Known) &&
3334 IsNonZero(
II->getArgOperand(0), Op0NonZero, Op0Known);
3336case Intrinsic::smin: {
3337// If either arg is negative the result is non-zero. Otherwise 3338// the result is non-zero if both ops are non-zero. 3352case Intrinsic::umin:
3355case Intrinsic::cttz:
3358case Intrinsic::ctlz:
3361case Intrinsic::fshr:
3362case Intrinsic::fshl:
3363// If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0. 3364if (
II->getArgOperand(0) ==
II->getArgOperand(1))
3367case Intrinsic::vscale:
3369case Intrinsic::experimental_get_vector_length:
3383return Known.
One != 0;
3386/// Return true if the given value is known to be non-zero when defined. For 3387/// vectors, return true if every demanded element is known to be non-zero when 3388/// defined. For pointers, if the context instruction and dominator tree are 3389/// specified, perform context-sensitive analysis and return true if the 3390/// pointer couldn't possibly be null at the specified instruction. 3391/// Supports values with integer or pointer type and vectors of integers. 3394Type *Ty = V->getType();
3399if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3401 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
3402"DemandedElt width should equal the fixed vector number of elements");
3405"DemandedElt width should be 1 for scalars");
3409if (
auto *
C = dyn_cast<Constant>(V)) {
3410if (
C->isNullValue())
3412if (isa<ConstantInt>(
C))
3413// Must be non-zero due to null test above. 3416// For constant vectors, check that all elements are poison or known 3417// non-zero to determine that the whole vector is known non-zero. 3418if (
auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3419for (
unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3420if (!DemandedElts[i])
3422Constant *Elt =
C->getAggregateElement(i);
3425if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3431// Constant ptrauth can be null, iff the base pointer can be. 3432if (
auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3435// A global variable in address space 0 is non null unless extern weak 3436// or an absolute symbol reference. Other address spaces may have null as a 3437// valid address for a global, so we can't assume anything. 3438if (
constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3439if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3440 GV->getType()->getAddressSpace() == 0)
3444// For constant expressions, fall through to the Operator code below. 3445if (!isa<ConstantExpr>(V))
3449if (
constauto *
A = dyn_cast<Argument>(V))
3450if (std::optional<ConstantRange>
Range =
A->getRange()) {
3459// Some of the tests below are recursive, so bail out if we hit the limit. 3463// Check for pointer simplifications. 3465if (
PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3466// A byval, inalloca may not be null in a non-default addres space. A 3467// nonnull argument is assumed never 0. 3468if (
constArgument *
A = dyn_cast<Argument>(V)) {
3469if (((
A->hasPassPointeeByValueCopyAttr() &&
3471A->hasNonNullAttr()))
3476if (
constauto *
I = dyn_cast<Operator>(V))
3480if (!isa<Constant>(V) &&
3489auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3492 return ::isKnownNonZero(V, DemandedElts, Q,
Depth);
3495/// If the pair of operators are the same invertible function, return the 3496/// the operands of the function corresponding to each input. Otherwise, 3497/// return std::nullopt. An invertible function is one that is 1-to-1 and maps 3498/// every input value to exactly one output value. This is equivalent to 3499/// saying that Op1 and Op2 are equal exactly when the specified pair of 3500/// operands are equal, (except that Op1 and Op2 may be poison more often.) 3501static std::optional<std::pair<Value*, Value*>>
3514case Instruction::Or:
3515if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3516 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3519case Instruction::Xor:
3520case Instruction::Add: {
3528case Instruction::Sub:
3534case Instruction::Mul: {
3535// invertible if A * B == (A * B) mod 2^N where A, and B are integers 3536// and N is the bitwdith. The nsw case is non-obvious, but proven by 3537// alive2: https://alive2.llvm.org/ce/z/Z6D5qK 3538auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3539auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3540if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3541 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3544// Assume operand order has been canonicalized 3547 !cast<ConstantInt>(Op1->
getOperand(1))->isZero())
3551case Instruction::Shl: {
3552// Same as multiplies, with the difference that we don't need to check 3553// for a non-zero multiply. Shifts always multiply by non-zero. 3554auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3555auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3556if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3557 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3564case Instruction::AShr:
3565case Instruction::LShr: {
3566auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3567auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3568if (!PEO1->isExact() || !PEO2->isExact())
3575case Instruction::SExt:
3576case Instruction::ZExt:
3580case Instruction::PHI: {
3581constPHINode *PN1 = cast<PHINode>(Op1);
3582constPHINode *PN2 = cast<PHINode>(Op2);
3584// If PN1 and PN2 are both recurrences, can we prove the entire recurrences 3585// are a single invertible function of the start values? Note that repeated 3586// application of an invertible function is also invertible 3588Value *Start1 =
nullptr, *Step1 =
nullptr;
3590Value *Start2 =
nullptr, *Step2 =
nullptr;
3597 cast<Operator>(BO2));
3601// We have to be careful of mutually defined recurrences here. Ex: 3602// * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V 3603// * X_i = Y_i = X_(i-1) OP Y_(i-1) 3604// The invertibility of these is complicated, and not worth reasoning 3606if (Values->first != PN1 || Values->second != PN2)
3609return std::make_pair(Start1, Start2);
3615/// Return true if V1 == (binop V2, X), where X is known non-zero. 3616/// Only handle a small subset of binops where (binop V2, X) with non-zero X 3617/// implies V2 != V1. 3627case Instruction::Or:
3628if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3631case Instruction::Xor:
3632case Instruction::Add:
3645/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and 3646/// the multiplication is nuw or nsw. 3650if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3653 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3654 !
C->isZero() && !
C->isOne() &&
3660/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and 3661/// the shift is nuw or nsw. 3665if (
auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3668 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3677// Check two PHIs are in same block. 3682bool UsedFullRecursion =
false;
3684if (!VisitedBBs.
insert(IncomBB).second)
3685continue;
// Don't reprocess blocks that we have dealt with already. 3692// Only one pair of phi operands is allowed for full recursion. 3693if (UsedFullRecursion)
3697 RecQ.
CxtI = IncomBB->getTerminator();
3700 UsedFullRecursion =
true;
3708constSelectInst *SI1 = dyn_cast<SelectInst>(V1);
3712if (
constSelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3714constValue *Cond2 = SI2->getCondition();
3717 DemandedElts,
Depth + 1, Q) &&
3719 DemandedElts,
Depth + 1, Q);
3725// Check to see if A is both a GEP and is the incoming value for a PHI in the 3726// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values, 3727// one of them being the recursive GEP A and the other a ptr at same base and at 3728// the same/higher offset than B we are only incrementing the pointer further in 3729// loop if offset of recursive GEP is greater than 0. 3732if (!
A->getType()->isPointerTy() || !
B->getType()->isPointerTy())
3735auto *GEPA = dyn_cast<GEPOperator>(
A);
3736if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3739// Handle 2 incoming PHI values with one being a recursive GEP. 3740auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3741if (!PN || PN->getNumIncomingValues() != 2)
3744// Search for the recursive GEP as an incoming operand, and record that as 3746Value *Start =
nullptr;
3748if (PN->getIncomingValue(0) == Step)
3749 Start = PN->getIncomingValue(1);
3750elseif (PN->getIncomingValue(1) == Step)
3751 Start = PN->getIncomingValue(0);
3755// Other incoming node base should match the B base. 3756// StartOffset >= OffsetB && StepOffset > 0? 3757// StartOffset <= OffsetB && StepOffset < 0? 3758// Is non-equal if above are true. 3759// We use stripAndAccumulateInBoundsConstantOffsets to restrict the 3760// optimisation to inbounds GEPs only. 3762APInt StartOffset(IndexWidth, 0);
3763 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.
DL, StartOffset);
3764APInt StepOffset(IndexWidth, 0);
3767// Check if Base Pointer of Step matches the PHI. 3770APInt OffsetB(IndexWidth, 0);
3771B =
B->stripAndAccumulateInBoundsConstantOffsets(Q.
DL, OffsetB);
3777/// Return true if it is known that V1 != V2. 3783if (V1->
getType() != V2->getType())
3784// We can't look through casts yet. 3790// See if we can recurse through (exactly one of) our operands. This 3791// requires our operation be 1-to-1 and map every input value to exactly 3792// one output value. Such an operation is invertible. 3793auto *O1 = dyn_cast<Operator>(V1);
3794auto *O2 = dyn_cast<Operator>(V2);
3795if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3800if (
constPHINode *PN1 = dyn_cast<PHINode>(V1)) {
3801constPHINode *PN2 = cast<PHINode>(V2);
3802// FIXME: This is missing a generalization to handle the case where one is 3803// a PHI and another one isn't. 3822// Are any known bits in V1 contradictory to known bits in V2? If V1 3823// has a known zero where V2 has a known one, they must not be equal. 3842// PtrToInts are NonEqual if their Ptrs are NonEqual. 3843// Check PtrToInt type matches the pointer size. 3851/// For vector constants, loop over the elements and find the constant with the 3852/// minimum number of sign bits. Return 0 if the value is not a vector constant 3853/// or if any element was not analyzed; otherwise, return the count for the 3854/// element with the minimum number of sign bits. 3856constAPInt &DemandedElts,
3858constauto *CV = dyn_cast<Constant>(V);
3859if (!CV || !isa<FixedVectorType>(CV->getType()))
3862unsigned MinSignBits = TyBits;
3863unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3864for (
unsigned i = 0; i != NumElts; ++i) {
3865if (!DemandedElts[i])
3867// If we find a non-ConstantInt, bail out. 3868auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3872 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3879constAPInt &DemandedElts,
3885assert(Result > 0 &&
"At least one sign bit needs to be present!");
3889/// Return the number of times the sign bit of the register is replicated into 3890/// the other bits. We know that at least 1 bit is always equal to the sign bit 3891/// (itself), but other cases can give us information. For example, immediately 3892/// after an "ashr X, 2", we know that the top 3 bits are all equal to each 3893/// other, so we return 3. For vectors, return the number of sign bits for the 3894/// vector element with the minimum number of known sign bits of the demanded 3895/// elements in the vector specified by DemandedElts. 3897constAPInt &DemandedElts,
3899Type *Ty = V->getType();
3903if (
auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3905 FVTy->getNumElements() == DemandedElts.
getBitWidth() &&
3906"DemandedElt width should equal the fixed vector number of elements");
3909"DemandedElt width should be 1 for scalars");
3913// We return the minimum number of sign bits that are guaranteed to be present 3914// in V, so for undef we have to conservatively return 1. We don't have the 3915// same behavior for poison though -- that's a FIXME today. 3923unsigned FirstAnswer = 1;
3925// Note that ConstantInt is handled by the general computeKnownBits case 3931if (
auto *U = dyn_cast<Operator>(V)) {
3934case Instruction::SExt:
3935 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3939case Instruction::SDiv: {
3940constAPInt *Denominator;
3941// sdiv X, C -> adds log(C) sign bits. 3944// Ignore non-positive denominator. 3948// Calculate the incoming numerator bits. 3952// Add floor(log(C)) bits to the numerator bits. 3953return std::min(TyBits, NumBits + Denominator->
logBase2());
3958case Instruction::SRem: {
3961constAPInt *Denominator;
3962// srem X, C -> we know that the result is within [-C+1,C) when C is a 3963// positive constant. This let us put a lower bound on the number of sign 3967// Ignore non-positive denominator. 3969// Calculate the leading sign bit constraints by examining the 3970// denominator. Given that the denominator is positive, there are two 3973// 1. The numerator is positive. The result range is [0,C) and 3974// [0,C) u< (1 << ceilLogBase2(C)). 3976// 2. The numerator is negative. Then the result range is (-C,0] and 3977// integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). 3979// Thus a lower bound on the number of sign bits is `TyBits - 3982unsigned ResBits = TyBits - Denominator->
ceilLogBase2();
3983 Tmp = std::max(Tmp, ResBits);
3989case Instruction::AShr: {
3991// ashr X, C -> adds C sign bits. Vectors too. 3994if (ShAmt->
uge(TyBits))
3997 Tmp += ShAmtLimited;
3998if (Tmp > TyBits) Tmp = TyBits;
4002case Instruction::Shl: {
4006// shl destroys sign bits. 4007if (ShAmt->
uge(TyBits))
4009// We can look through a zext (more or less treating it as a sext) if 4010// all extended bits are shifted out. 4012 ShAmt->
uge(TyBits -
X->getType()->getScalarSizeInBits())) {
4014 Tmp += TyBits -
X->getType()->getScalarSizeInBits();
4019break;
// Shifted all sign bits out. 4025case Instruction::And:
4026case Instruction::Or:
4027case Instruction::Xor:
// NOT is handled here. 4028// Logical binary ops preserve the number of sign bits at the worst. 4032 FirstAnswer = std::min(Tmp, Tmp2);
4033// We computed what we know about the sign bits as our first 4034// answer. Now proceed to the generic code that uses 4035// computeKnownBits, and pick whichever answer is better. 4039case Instruction::Select: {
4040// If we have a clamp pattern, we know that the number of sign bits will 4041// be the minimum of the clamp min/max range. 4043constAPInt *CLow, *CHigh;
4051return std::min(Tmp, Tmp2);
4054case Instruction::Add:
4055// Add can have at most one carry bit. Thus we know that the output 4056// is, at worst, one more bit than the inputs. 4060// Special case decrementing a value (ADD X, -1): 4061if (
constauto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4062if (CRHS->isAllOnesValue()) {
4066// If the input is known to be 0 or 1, the output is 0/-1, which is 4067// all sign bits set. 4068if ((Known.
Zero | 1).isAllOnes())
4071// If we are subtracting one from a positive number, there is no carry 4072// out of the result. 4080return std::min(Tmp, Tmp2) - 1;
4082case Instruction::Sub:
4088if (
constauto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4089if (CLHS->isNullValue()) {
4092// If the input is known to be 0 or 1, the output is 0/-1, which is 4093// all sign bits set. 4094if ((Known.
Zero | 1).isAllOnes())
4097// If the input is known to be positive (the sign bit is known clear), 4098// the output of the NEG has the same number of sign bits as the 4103// Otherwise, we treat this like a SUB. 4106// Sub can have at most one carry bit. Thus we know that the output 4107// is, at worst, one more bit than the inputs. 4111return std::min(Tmp, Tmp2) - 1;
4113case Instruction::Mul: {
4114// The output of the Mul can be at most twice the valid bits in the 4116unsigned SignBitsOp0 =
4118if (SignBitsOp0 == 1)
4120unsigned SignBitsOp1 =
4122if (SignBitsOp1 == 1)
4124unsigned OutValidBits =
4125 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4126return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4129case Instruction::PHI: {
4130constPHINode *PN = cast<PHINode>(U);
4132// Don't analyze large in-degree PHIs. 4133if (NumIncomingValues > 4)
break;
4134// Unreachable blocks may have zero-operand PHI nodes. 4135if (NumIncomingValues == 0)
break;
4137// Take the minimum of all incoming values. This can't infinitely loop 4138// because of our depth threshold. 4141for (
unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4142if (Tmp == 1)
return Tmp;
4145 DemandedElts,
Depth + 1, RecQ));
4150case Instruction::Trunc: {
4151// If the input contained enough sign bits that some remain after the 4152// truncation, then we can make use of that. Otherwise we don't know 4155unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4156if (Tmp > (OperandTyBits - TyBits))
4157return Tmp - (OperandTyBits - TyBits);
4162case Instruction::ExtractElement:
4163// Look through extract element. At the moment we keep this simple and 4164// skip tracking the specific element. But at least we might find 4165// information valid for all elements of the vector (for example if vector 4166// is sign extended, shifted, etc). 4169case Instruction::ShuffleVector: {
4170// Collect the minimum number of sign bits that are shared by every vector 4171// element referenced by the shuffle. 4172auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4174// FIXME: Add support for shufflevector constant expressions. 4177APInt DemandedLHS, DemandedRHS;
4178// For undef elements, we don't know anything about the common state of 4179// the shuffle result. 4182 Tmp = std::numeric_limits<unsigned>::max();
4184constValue *
LHS = Shuf->getOperand(0);
4187// If we don't know anything, early out and try computeKnownBits 4192constValue *
RHS = Shuf->getOperand(1);
4194 Tmp = std::min(Tmp, Tmp2);
4196// If we don't know anything, early out and try computeKnownBits 4200assert(Tmp <= TyBits &&
"Failed to determine minimum sign bits");
4203case Instruction::Call: {
4204if (
constauto *
II = dyn_cast<IntrinsicInst>(U)) {
4205switch (
II->getIntrinsicID()) {
4214// Absolute value reduces number of sign bits by at most 1. 4216case Intrinsic::smin:
4217case Intrinsic::smax: {
4218constAPInt *CLow, *CHigh;
4228// Finally, if we can prove that the top bits of the result are 0's or 1's, 4229// use this information. 4231// If we can examine all elements of a vector constant successfully, we're 4232// done (we can't do any better than that). If not, keep trying. 4233if (
unsigned VecSignBits =
4240// If we know that the sign bit is either zero or one, determine the number of 4241// identical bits in the top of the input value. 4251if (
F->isIntrinsic())
4252returnF->getIntrinsicID();
4254// We are going to infer semantics of a library function based on mapping it 4255// to an LLVM intrinsic. Check that the library function is available from 4256// this callbase and in this environment. 4258if (
F->hasLocalLinkage() || !TLI || !TLI->
getLibFunc(CB, Func) ||
4268return Intrinsic::sin;
4272return Intrinsic::cos;
4276return Intrinsic::tan;
4280return Intrinsic::asin;
4284return Intrinsic::acos;
4288return Intrinsic::atan;
4292return Intrinsic::atan2;
4296return Intrinsic::sinh;
4300return Intrinsic::cosh;
4304return Intrinsic::tanh;
4308return Intrinsic::exp;
4312return Intrinsic::exp2;
4316return Intrinsic::exp10;
4320return Intrinsic::log;
4324return Intrinsic::log10;
4328return Intrinsic::log2;
4332return Intrinsic::fabs;
4336return Intrinsic::minnum;
4340return Intrinsic::maxnum;
4341case LibFunc_copysign:
4342case LibFunc_copysignf:
4343case LibFunc_copysignl:
4344return Intrinsic::copysign;
4348return Intrinsic::floor;
4352return Intrinsic::ceil;
4356return Intrinsic::trunc;
4360return Intrinsic::rint;
4361case LibFunc_nearbyint:
4362case LibFunc_nearbyintf:
4363case LibFunc_nearbyintl:
4364return Intrinsic::nearbyint;
4368return Intrinsic::round;
4369case LibFunc_roundeven:
4370case LibFunc_roundevenf:
4371case LibFunc_roundevenl:
4372return Intrinsic::roundeven;
4376return Intrinsic::pow;
4380return Intrinsic::sqrt;
4386/// Return true if it's possible to assume IEEE treatment of input denormals in 4423// If we know there are no denormals, nothing can be flushed to zero. 4428switch (Mode.Input) {
4432// Negative subnormal won't flush to +0 4436// Both positive and negative subnormal could flush to +0 4446// If we aren't assuming the source can't be a zero, we don't have to check if 4447// a denormal input could be flushed. 4448if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4451// If we know the input can't be a denormal, it can't be flushed to 0. 4452if (Src.isKnownNeverSubnormal())
4478/// Given an exploded icmp instruction, return true if the comparison only 4479/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if 4480/// the result of the comparison is true when the input value is signed. 4482bool &TrueIfSigned) {
4489returnRHS.isAllOnes();
4491 TrueIfSigned =
false;
4492returnRHS.isAllOnes();
4494 TrueIfSigned =
false;
4497// True if LHS u> RHS and RHS == sign-bit-mask - 1 4499returnRHS.isMaxSignedValue();
4501// True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) 4503returnRHS.isMinSignedValue();
4505// True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) 4506 TrueIfSigned =
false;
4507returnRHS.isMinSignedValue();
4509// True if LHS u<= RHS and RHS == sign-bit-mask - 1 4510 TrueIfSigned =
false;
4511returnRHS.isMaxSignedValue();
4517/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the 4518/// same result as an fcmp with the given operands. 4522bool LookThroughSrc) {
4530std::pair<Value *, FPClassTest>
4532constAPFloat *ConstRHS,
bool LookThroughSrc) {
4534auto [Src, ClassIfTrue, ClassIfFalse] =
4536if (Src && ClassIfTrue == ~ClassIfFalse)
4537return {Src, ClassIfTrue};
4541/// Return the return value for fcmpImpliesClass for a compare that produces an 4542/// exact class test. 4548std::tuple<Value *, FPClassTest, FPClassTest>
4562constbool IsNegativeRHS = (RHSClass &
fcNegative) == RHSClass;
4563constbool IsPositiveRHS = (RHSClass &
fcPositive) == RHSClass;
4564constbool IsNaN = (RHSClass & ~fcNan) ==
fcNone;
4567// fcmp o__ x, nan -> false 4568// fcmp u__ x, nan -> true 4572// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan 4576// fcmp uno x, zero|normal|subnormal|inf -> fcNan 4584constbool IsZero = (OrigClass &
fcZero) == OrigClass;
4587// Compares with fcNone are only exactly equal to fcZero if input denormals 4589// TODO: Handle DAZ by expanding masks to cover subnormal cases. 4603// Canonical form of ord/uno is with a zero. We could also handle 4604// non-canonical other non-NaN constants or LHS == RHS. 4631constbool IsDenormalRHS = (OrigClass &
fcSubnormal) == OrigClass;
4633constbool IsInf = (OrigClass &
fcInf) == OrigClass;
4640// Match __builtin_isinf patterns 4642// fcmp oeq x, +inf -> is_fpclass x, fcPosInf 4643// fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf 4644// fcmp oeq x, -inf -> is_fpclass x, fcNegInf 4645// fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false 4647// fcmp une x, +inf -> is_fpclass x, ~fcPosInf 4648// fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf 4649// fcmp une x, -inf -> is_fpclass x, ~fcNegInf 4650// fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true 4664// Match __builtin_isinf patterns 4665// fcmp one x, -inf -> is_fpclass x, fcNegInf 4666// fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan 4667// fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan 4668// fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan 4670// fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan 4671// fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan 4672// fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan 4673// fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan 4675 Mask = ~fcNegInf & ~fcNan;
4679 Mask = ~fcPosInf & ~fcNan;
4689// No value is ordered and less than negative infinity. 4690// All values are unordered with or at least negative infinity. 4691// fcmp olt x, -inf -> false 4692// fcmp uge x, -inf -> true 4697// fcmp olt fabs(x), +inf -> fcFinite 4698// fcmp uge fabs(x), +inf -> ~fcFinite 4699// fcmp olt x, +inf -> fcFinite|fcNegInf 4700// fcmp uge x, +inf -> ~(fcFinite|fcNegInf) 4709// fcmp oge x, -inf -> ~fcNan 4710// fcmp oge fabs(x), -inf -> ~fcNan 4711// fcmp ult x, -inf -> fcNan 4712// fcmp ult fabs(x), -inf -> fcNan 4717// fcmp oge fabs(x), +inf -> fcInf 4718// fcmp oge x, +inf -> fcPosInf 4719// fcmp ult fabs(x), +inf -> ~fcInf 4720// fcmp ult x, +inf -> ~fcPosInf 4729// fcmp ogt x, -inf -> fcmp one x, -inf 4730// fcmp ogt fabs(x), -inf -> fcmp ord x, x 4731// fcmp ule x, -inf -> fcmp ueq x, -inf 4732// fcmp ule fabs(x), -inf -> fcmp uno x, x 4737// No value is ordered and greater than infinity. 4748// fcmp ole x, +inf -> fcmp ord x, x 4749// fcmp ole fabs(x), +inf -> fcmp ord x, x 4750// fcmp ole x, -inf -> fcmp oeq x, -inf 4751// fcmp ole fabs(x), -inf -> false 4759// Invert the comparison for the unordered cases. 4771return {Src, Class, ~fcNan};
4775return {Src, ~fcNan, RHSClass |
fcNan};
4784"should have been recognized as an exact class test");
4787// TODO: Handle fneg(fabs) 4789// fabs(x) o> -k -> fcmp ord x, x 4790// fabs(x) u> -k -> true 4791// fabs(x) o< -k -> false 4792// fabs(x) u< -k -> fcmp uno x, x 4796return {Src, ~fcNan,
fcNan};
4805return {Src,
fcNan, ~fcNan};
4824return {Src, ClassesGE, ~ClassesGE | RHSClass};
4827return {Src, ClassesGE |
fcNan, ~(ClassesGE |
fcNan) | RHSClass};
4830return {Src, ClassesLE, ~ClassesLE | RHSClass};
4833return {Src, ClassesLE |
fcNan, ~(ClassesLE |
fcNan) | RHSClass};
4837 }
elseif (IsPositiveRHS) {
4853return {Src, ClassesGE, ~ClassesGE | RHSClass};
4856return {Src, ClassesGE |
fcNan, ~(ClassesGE |
fcNan) | RHSClass};
4859return {Src, ClassesLE, ~ClassesLE | RHSClass};
4862return {Src, ClassesLE |
fcNan, ~(ClassesLE |
fcNan) | RHSClass};
4871std::tuple<Value *, FPClassTest, FPClassTest>
4873constAPFloat &ConstRHS,
bool LookThroughSrc) {
4874// We can refine checks against smallest normal / largest denormal to an 4881// Match pattern that's used in __builtin_isnormal. 4885// fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero 4886// fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero 4887// fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf 4888// fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero) 4897// fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf 4898// fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal 4899// fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf) 4900// fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal) 4911// Invert the comparison for the unordered cases. 4921std::tuple<Value *, FPClassTest, FPClassTest>
4923Value *RHS,
bool LookThroughSrc) {
4928// TODO: Just call computeKnownFPClass for RHS to handle non-constants. 4933unsignedDepth,
bool CondIsTrue,
4955 KnownFromContext.
knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4956 }
elseif (
match(
Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4959 KnownFromContext.
knownNot(CondIsTrue ? ~Mask : Mask);
4965if (TrueIfSigned == CondIsTrue)
4977return KnownFromContext;
4980// Handle dominating conditions. 4987 Q.
CxtI, KnownFromContext);
4992 Q.
CxtI, KnownFromContext);
4997return KnownFromContext;
4999// Try to restrict the floating-point classes based on information from 5007"Got assumption for the wrong function!");
5008assert(
I->getIntrinsicID() == Intrinsic::assume &&
5009"must be an assume intrinsic");
5015/*CondIsTrue=*/true, Q.
CxtI, KnownFromContext);
5018return KnownFromContext;
5028auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5035constAPInt &DemandedElts,
5039if ((InterestedClasses &
5045 KnownSrc,
Depth + 1, Q);
5047// Sign should be preserved 5048// TODO: Handle cannot be ordered greater than zero 5054// Infinity needs a range check. 5060assert(Known.
isUnknown() &&
"should not be called with known information");
5063// No demanded elts, better to assume we don't know anything. 5070if (
auto *CFP = dyn_cast<ConstantFP>(V)) {
5072 Known.
SignBit = CFP->isNegative();
5076if (isa<ConstantAggregateZero>(V)) {
5082if (isa<PoisonValue>(V)) {
5088// Try to handle fixed width vector constants 5089auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5090constConstant *CV = dyn_cast<Constant>(V);
5093bool SignBitAllZero =
true;
5094bool SignBitAllOne =
true;
5096// For vectors, verify that each element is not NaN. 5097unsigned NumElts = VFVTy->getNumElements();
5098for (
unsigned i = 0; i != NumElts; ++i) {
5099if (!DemandedElts[i])
5107if (isa<PoisonValue>(Elt))
5109auto *CElt = dyn_cast<ConstantFP>(Elt);
5115constAPFloat &
C = CElt->getValueAPF();
5118 SignBitAllZero =
false;
5120 SignBitAllOne =
false;
5122if (SignBitAllOne != SignBitAllZero)
5123 Known.
SignBit = SignBitAllOne;
5128if (
constauto *CB = dyn_cast<CallBase>(V))
5129 KnownNotFromFlags |= CB->getRetNoFPClass();
5130elseif (
constauto *Arg = dyn_cast<Argument>(V))
5131 KnownNotFromFlags |= Arg->getNoFPClass();
5135if (FPOp->hasNoNaNs())
5136 KnownNotFromFlags |=
fcNan;
5137if (FPOp->hasNoInfs())
5138 KnownNotFromFlags |=
fcInf;
5142 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5144// We no longer need to find out about these bits from inputs if we can 5145// assume this from flags/attributes. 5146 InterestedClasses &= ~KnownNotFromFlags;
5151 if (*AssumedClasses.SignBit)
5152 Known.signBitMustBeOne();
5154 Known.signBitMustBeZero();
5161// All recursive calls that increase depth must come after this. 5165constunsigned Opc =
Op->getOpcode();
5167case Instruction::FNeg: {
5169 Known,
Depth + 1, Q);
5173case Instruction::Select: {
5181Value *TestedValue =
nullptr;
5185constFunction *
F = cast<Instruction>(
Op)->getFunction();
5187Value *CmpLHS, *CmpRHS;
5189// If the select filters out a value based on the class, it no longer 5190// participates in the class of the result 5192// TODO: In some degenerate cases we can infer something if we try again 5193// without looking through sign operations. 5194bool LookThroughFAbsFNeg = CmpLHS !=
LHS && CmpLHS !=
RHS;
5195 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5198 m_Intrinsic<Intrinsic::is_fpclass>(
5201 MaskIfTrue = TestedMask;
5202 MaskIfFalse = ~TestedMask;
5205if (TestedValue ==
LHS) {
5206// match !isnan(x) ? x : y 5207 FilterLHS = MaskIfTrue;
5208 }
elseif (TestedValue ==
RHS) {
// && IsExactClass 5209// match !isnan(x) ? y : x 5210 FilterRHS = MaskIfFalse;
5219 Known2,
Depth + 1, Q);
5225case Instruction::Call: {
5229case Intrinsic::fabs: {
5231// If we only care about the sign bit we don't need to inspect the 5234 InterestedClasses, Known,
Depth + 1, Q);
5240case Intrinsic::copysign: {
5244 Known,
Depth + 1, Q);
5246 KnownSign,
Depth + 1, Q);
5251case Intrinsic::fmuladd: {
5255if (
II->getArgOperand(0) !=
II->getArgOperand(1))
5258// The multiply cannot be -0 and therefore the add can't be -0 5261// x * x + y is non-negative if y is non-negative. 5264 KnownAddend,
Depth + 1, Q);
5270case Intrinsic::sqrt:
5271case Intrinsic::experimental_constrained_sqrt: {
5274if (InterestedClasses &
fcNan)
5278 KnownSrc,
Depth + 1, Q);
5285// Any negative value besides -0 returns a nan. 5289// The only negative value that can be returned is -0 for -0 inputs. 5292// If the input denormal mode could be PreserveSign, a negative 5293// subnormal input could produce a negative zero output. 5302case Intrinsic::cos: {
5303// Return NaN on infinite inputs. 5306 KnownSrc,
Depth + 1, Q);
5312case Intrinsic::maxnum:
5313case Intrinsic::minnum:
5314case Intrinsic::minimum:
5315case Intrinsic::maximum: {
5318 KnownLHS,
Depth + 1, Q);
5320 KnownRHS,
Depth + 1, Q);
5323 Known = KnownLHS | KnownRHS;
5325// If either operand is not NaN, the result is not NaN. 5326if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5329if (IID == Intrinsic::maxnum) {
5330// If at least one operand is known to be positive, the result must be 5337 }
elseif (IID == Intrinsic::maximum) {
5338// If at least one operand is known to be positive, the result must be 5343 }
elseif (IID == Intrinsic::minnum) {
5344// If at least one operand is known to be negative, the result must be 5352// If at least one operand is known to be negative, the result must be 5359// Fixup zero handling if denormals could be returned as a zero. 5361// As there's no spec for denormal flushing, be conservative with the 5362// treatment of denormals that could be flushed to zero. For older 5363// subtargets on AMDGPU the min/max instructions would not flush the 5364// output and return the original value. 5373II->getType()->getScalarType()->getFltSemantics());
5385 }
elseif ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5390if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5393elseif ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5400case Intrinsic::canonicalize: {
5403 KnownSrc,
Depth + 1, Q);
5405// This is essentially a stronger form of 5406// propagateCanonicalizingSrc. Other "canonicalizing" operations don't 5407// actually have an IR canonicalization guarantee. 5409// Canonicalize may flush denormals to zero, so we have to consider the 5410// denormal mode to preserve known-not-0 knowledge. 5413// Stronger version of propagateNaN 5414// Canonicalize is guaranteed to quiet signaling nans. 5424// If the parent function flushes denormals, the canonical output cannot 5427II->getType()->getScalarType()->getFltSemantics();
5447case Intrinsic::vector_reduce_fmax:
5448case Intrinsic::vector_reduce_fmin:
5449case Intrinsic::vector_reduce_fmaximum:
5450case Intrinsic::vector_reduce_fminimum: {
5451// reduce min/max will choose an element from one of the vector elements, 5452// so we can infer and class information that is common to all elements. 5454 InterestedClasses,
Depth + 1, Q);
5455// Can only propagate sign if output is never NaN. 5460// reverse preserves all characteristics of the input vec's element. 5461case Intrinsic::vector_reverse:
5464II->getFastMathFlags(), InterestedClasses,
Depth + 1, Q);
5466case Intrinsic::trunc:
5467case Intrinsic::floor:
5468case Intrinsic::ceil:
5469case Intrinsic::rint:
5470case Intrinsic::nearbyint:
5471case Intrinsic::round:
5472case Intrinsic::roundeven: {
5480 KnownSrc,
Depth + 1, Q);
5482// Integer results cannot be subnormal. 5487// Pass through infinities, except PPC_FP128 is a special case for 5488// intrinsics other than trunc. 5489if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5496// Negative round ups to 0 produce -0 5505case Intrinsic::exp2:
5506case Intrinsic::exp10: {
5513 KnownSrc,
Depth + 1, Q);
5521case Intrinsic::fptrunc_round: {
5527case Intrinsic::log10:
5528case Intrinsic::log2:
5529case Intrinsic::experimental_constrained_log:
5530case Intrinsic::experimental_constrained_log10:
5531case Intrinsic::experimental_constrained_log2: {
5533// log([+-]0.0) -> -inf 5547 KnownSrc,
Depth + 1, Q);
5561case Intrinsic::powi: {
5565constValue *Exp =
II->getArgOperand(1);
5566Type *ExpTy = Exp->getType();
5570 ExponentKnownBits,
Depth + 1, Q);
5572if (ExponentKnownBits.
Zero[0]) {
// Is even 5577// Given that exp is an integer, here are the 5578// ways that pow can return a negative value: 5580// pow(-x, exp) --> negative if exp is odd and x is negative. 5581// pow(-0, exp) --> -inf if exp is negative odd. 5582// pow(-0, exp) --> -0 if exp is positive odd. 5583// pow(-inf, exp) --> -0 if exp is negative odd. 5584// pow(-inf, exp) --> -inf if exp is positive odd. 5587 KnownSrc,
Depth + 1, Q);
5592case Intrinsic::ldexp: {
5595 KnownSrc,
Depth + 1, Q);
5598// Sign is preserved, but underflows may produce zeroes. 5609// Can refine inf/zero handling based on the exponent operand. 5611if ((InterestedClasses & ExpInfoMask) ==
fcNone)
5617II->getType()->getScalarType()->getFltSemantics();
5619constValue *ExpArg =
II->getArgOperand(1);
5623constint MantissaBits = Precision - 1;
5629if (ConstVal && ConstVal->
isZero()) {
5630// ldexp(x, 0) -> x, so propagate everything. 5633// If we know the power is <= 0, can't introduce inf 5639// If we know the power is >= 0, can't introduce subnormal or zero 5652case Intrinsic::arithmetic_fence: {
5654 Known,
Depth + 1, Q);
5657case Intrinsic::experimental_constrained_sitofp:
5658case Intrinsic::experimental_constrained_uitofp:
5659// Cannot produce nan 5662// sitofp and uitofp turn into +0.0 for zero. 5665// Integers cannot be subnormal 5668if (IID == Intrinsic::experimental_constrained_uitofp)
5671// TODO: Copy inf handling from instructions 5679case Instruction::FAdd:
5680case Instruction::FSub: {
5683Op->getOpcode() == Instruction::FAdd &&
5685bool WantNaN = (InterestedClasses &
fcNan) !=
fcNone;
5688if (!WantNaN && !WantNegative && !WantNegZero)
5694if (InterestedClasses &
fcNan)
5695 InterestedSrcs |=
fcInf;
5697 KnownRHS,
Depth + 1, Q);
5701 WantNegZero || Opc == Instruction::FSub) {
5703// RHS is canonically cheaper to compute. Skip inspecting the LHS if 5706 KnownLHS,
Depth + 1, Q);
5707// Adding positive and negative infinity produces NaN. 5708// TODO: Check sign of infinities. 5713// FIXME: Context function should always be passed in separately 5714constFunction *
F = cast<Instruction>(
Op)->getFunction();
5716if (
Op->getOpcode() == Instruction::FAdd) {
5723// (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. 5726// Make sure output negative denormal can't flush to -0 5733// Only fsub -0, +0 can return -0 5736// Make sure output negative denormal can't flush to -0 5744case Instruction::FMul: {
5745// X * X is always non-negative or a NaN. 5746if (
Op->getOperand(0) ==
Op->getOperand(1))
5752// fcSubnormal is only needed in case of DAZ. 5773// If 0 * +/-inf produces NaN. 5779constFunction *
F = cast<Instruction>(
Op)->getFunction();
5791case Instruction::FDiv:
5792case Instruction::FRem: {
5793if (
Op->getOperand(0) ==
Op->getOperand(1)) {
5794// TODO: Could filter out snan if we inspect the operand 5795if (
Op->getOpcode() == Instruction::FDiv) {
5796// X / X is always exactly 1.0 or a NaN. 5799// X % X is always exactly [+-]0.0 or a NaN. 5806constbool WantNan = (InterestedClasses &
fcNan) !=
fcNone;
5808constbool WantPositive =
5810if (!WantNan && !WantNegative && !WantPositive)
5819bool KnowSomethingUseful =
5822if (KnowSomethingUseful || WantPositive) {
5828 InterestedClasses & InterestedLHS, KnownLHS,
5832constFunction *
F = cast<Instruction>(
Op)->getFunction();
5834if (
Op->getOpcode() == Instruction::FDiv) {
5835// Only 0/0, Inf/Inf produce NaN. 5844// X / -0.0 is -Inf (or NaN). 5849// Inf REM x and x REM 0 produce NaN. 5856// The sign for frem is the same as the first operand. 5862// See if we can be more aggressive about the sign of 0. 5871case Instruction::FPExt: {
5872// Infinity, nan and zero propagate from source. 5874 Known,
Depth + 1, Q);
5877Op->getType()->getScalarType()->getFltSemantics();
5879Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5881// All subnormal inputs should be in the normal range in the result type. 5890// Sign bit of a nan isn't guaranteed. 5895case Instruction::FPTrunc: {
5900case Instruction::SIToFP:
5901case Instruction::UIToFP: {
5902// Cannot produce nan 5905// Integers cannot be subnormal 5908// sitofp and uitofp turn into +0.0 for zero. 5910if (
Op->getOpcode() == Instruction::UIToFP)
5913if (InterestedClasses &
fcInf) {
5914// Get width of largest magnitude integer (remove a bit if signed). 5915// This still works for a signed minimum value because the largest FP 5916// value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx). 5917int IntSize =
Op->getOperand(0)->getType()->getScalarSizeInBits();
5918if (
Op->getOpcode() == Instruction::SIToFP)
5921// If the exponent of the largest finite FP value can hold the largest 5922// integer, the result of the cast must be finite. 5923Type *FPTy =
Op->getType()->getScalarType();
5930case Instruction::ExtractElement: {
5931// Look through extract element. If the index is non-constant or 5932// out-of-range demand all elements, otherwise just the extracted element. 5933constValue *Vec =
Op->getOperand(0);
5935auto *CIdx = dyn_cast<ConstantInt>(
Idx);
5937if (
auto *VecTy = dyn_cast<FixedVectorType>(Vec->
getType())) {
5938unsigned NumElts = VecTy->getNumElements();
5940if (CIdx && CIdx->getValue().ult(NumElts))
5948case Instruction::InsertElement: {
5949if (isa<ScalableVectorType>(
Op->getType()))
5952constValue *Vec =
Op->getOperand(0);
5953constValue *Elt =
Op->getOperand(1);
5954auto *CIdx = dyn_cast<ConstantInt>(
Op->getOperand(2));
5956APInt DemandedVecElts = DemandedElts;
5958// If we know the index we are inserting to, clear it from Vec check. 5959if (CIdx && CIdx->getValue().ult(NumElts)) {
5960 DemandedVecElts.
clearBit(CIdx->getZExtValue());
5961 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5964// Do we demand the inserted element? 5967// If we don't know any bits, early out. 5974// Do we need anymore elements from Vec? 5975if (!DemandedVecElts.
isZero()) {
5984case Instruction::ShuffleVector: {
5985// For undef elements, we don't know anything about the common state of 5986// the shuffle result. 5987APInt DemandedLHS, DemandedRHS;
5988auto *Shuf = dyn_cast<ShuffleVectorInst>(
Op);
5993constValue *
LHS = Shuf->getOperand(0);
5997// If we don't know any bits, early out. 6006constValue *
RHS = Shuf->getOperand(1);
6014case Instruction::ExtractValue: {
6018if (isa<StructType>(Src->getType()) && Indices.
size() == 1 &&
6020if (
constauto *
II = dyn_cast<IntrinsicInst>(Src)) {
6021switch (
II->getIntrinsicID()) {
6022case Intrinsic::frexp: {
6027 InterestedClasses, KnownSrc,
Depth + 1, Q);
6029constFunction *
F = cast<Instruction>(
Op)->getFunction();
6062case Instruction::PHI: {
6064// Unreachable blocks may have zero-operand PHI nodes. 6065if (
P->getNumIncomingValues() == 0)
6068// Otherwise take the unions of the known bit sets of the operands, 6069// taking conservative care to avoid excessive recursion. 6072if (
Depth < PhiRecursionLimit) {
6073// Skip if every incoming value references to ourself. 6074if (isa_and_nonnull<UndefValue>(
P->hasConstantValue()))
6079for (
constUse &U :
P->operands()) {
6083// Skip direct self references. 6088// Recurse, but cap the recursion to two levels, because we don't want 6089// to waste time spinning around in loops. We need at least depth 2 to 6090// detect known sign bits. 6109case Instruction::BitCast: {
6112 !Src->getType()->isIntOrIntVectorTy())
6115constType *Ty =
Op->getType()->getScalarType();
6119// Transfer information from the sign bit. 6120if (Bits.isNonNegative())
6122elseif (Bits.isNegative())
6126// IEEE floats are NaN when all bits of the exponent plus at least one of 6127// the fraction bits are 1. This means: 6128// - If we assume unknown bits are 0 and the value is NaN, it will 6130// - If we assume unknown bits are 1 and the value is not NaN, it can 6137// Build KnownBits representing Inf and check if it must be equal or 6138// unequal to this value. 6141 InfKB.Zero.clearSignBit();
6143assert(!InfResult.value());
6145 }
elseif (Bits == InfKB) {
6149// Build KnownBits representing Zero and check if it must be equal or 6150// unequal to this value. 6153 ZeroKB.Zero.clearSignBit();
6155assert(!ZeroResult.value());
6157 }
elseif (Bits == ZeroKB) {
6170constAPInt &DemandedElts,
6191// All byte-wide stores are splatable, even of arbitrary variables. 6192if (V->getType()->isIntegerTy(8))
6199if (isa<UndefValue>(V))
6202// Return poison for zero-sized type. 6203if (
DL.getTypeStoreSize(V->getType()).isZero())
6208// Conceptually, we could handle things like: 6209// %a = zext i8 %X to i16 6210// %b = shl i16 %a, 8 6211// %c = or i16 %a, %b 6212// but until there is an example that actually needs this, it doesn't seem 6213// worth worrying about. 6217// Handle 'null' ConstantArrayZero etc. 6218if (
C->isNullValue())
6221// Constant floating-point values can be handled as integer values if the 6222// corresponding integer value is "byteable". An important case is 0.0. 6225if (CFP->getType()->isHalfTy())
6227elseif (CFP->getType()->isFloatTy())
6229elseif (CFP->getType()->isDoubleTy())
6231// Don't handle long double formats, which have strange constraints. 6236// We can handle constant integers that are multiple of 8 bits. 6238if (CI->getBitWidth() % 8 == 0) {
6239assert(CI->getBitWidth() > 8 &&
"8 bits should be handled above!");
6240if (!CI->getValue().isSplat(8))
6242return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6246if (
auto *CE = dyn_cast<ConstantExpr>(
C)) {
6247if (CE->getOpcode() == Instruction::IntToPtr) {
6248if (
auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6249unsignedBitWidth =
DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6262if (
LHS == UndefInt8)
6264if (
RHS == UndefInt8)
6270Value *Val = UndefInt8;
6271for (
unsignedI = 0, E = CA->getNumElements();
I != E; ++
I)
6277if (isa<ConstantAggregate>(
C)) {
6278Value *Val = UndefInt8;
6285// Don't try to handle the handful of other constants. 6289// This is the recursive version of BuildSubAggregate. It takes a few different 6290// arguments. Idxs is the index within the nested struct From that we are 6291// looking at now (which is of type IndexedType). IdxSkip is the number of 6292// indices from Idxs that should be left out when inserting into the resulting 6293// struct. To is the result struct built so far, new insertvalue instructions 6299StructType *STy = dyn_cast<StructType>(IndexedType);
6301// Save the original To argument so we can modify it 6303// General case, the type indexed by Idxs is a struct 6305// Process each struct element recursively 6312// Couldn't find any inserted value for this index? Cleanup 6313while (PrevTo != OrigTo) {
6318// Stop processing elements 6322// If we successfully found a value for each of our subaggregates 6326// Base case, the type indexed by SourceIdxs is not a struct, or not all of 6327// the struct's elements had a value that was inserted directly. In the latter 6328// case, perhaps we can't determine each of the subelements individually, but 6329// we might be able to find the complete struct somewhere. 6331// Find the value that is at that particular spot 6337// Insert the value in the new (sub) aggregate 6342// This helper takes a nested struct and extracts a part of it (which is again a 6343// struct) into a new value. For example, given the struct: 6344// { a, { b, { c, d }, e } } 6345// and the indices "1, 1" this returns 6348// It does this by inserting an insertvalue for each element in the resulting 6349// struct, as opposed to just inserting a single struct. This will only work if 6350// each of the elements of the substruct are known (ie, inserted into From by an 6351// insertvalue instruction somewhere). 6353// All inserted insertvalue instructions are inserted before InsertBefore 6360unsigned IdxSkip = Idxs.
size();
6365/// Given an aggregate and a sequence of indices, see if the scalar value 6366/// indexed is already around as a register, for example if it was inserted 6367/// directly into the aggregate. 6369/// If InsertBefore is not null, this function will duplicate (modified) 6370/// insertvalues when a part of a nested struct is extracted. 6373 std::optional<BasicBlock::iterator> InsertBefore) {
6374// Nothing to index? Just return V then (this is useful at the end of our 6376if (idx_range.
empty())
6378// We have indices, so V should have an indexable type. 6379assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6380"Not looking at a struct or array?");
6382"Invalid indices for type?");
6384if (
Constant *
C = dyn_cast<Constant>(V)) {
6385C =
C->getAggregateElement(idx_range[0]);
6386if (!
C)
returnnullptr;
6391// Loop the indices for the insertvalue instruction in parallel with the 6393constunsigned *req_idx = idx_range.
begin();
6394for (
constunsigned *i =
I->idx_begin(), *e =
I->idx_end();
6395 i != e; ++i, ++req_idx) {
6396if (req_idx == idx_range.
end()) {
6397// We can't handle this without inserting insertvalues 6401// The requested index identifies a part of a nested aggregate. Handle 6402// this specially. For example, 6403// %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 6404// %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 6405// %C = extractvalue {i32, { i32, i32 } } %B, 1 6406// This can be changed into 6407// %A = insertvalue {i32, i32 } undef, i32 10, 0 6408// %C = insertvalue {i32, i32 } %A, i32 11, 1 6409// which allows the unused 0,0 element from the nested struct to be 6415// This insert value inserts something else than what we are looking for. 6416// See if the (aggregate) value inserted into has the value we are 6417// looking for, then. 6422// If we end up here, the indices of the insertvalue match with those 6423// requested (though possibly only partially). Now we recursively look at 6424// the inserted value, passing any remaining indices. 6430// If we're extracting a value from an aggregate that was extracted from 6431// something else, we can extract from that something else directly instead. 6432// However, we will need to chain I's indices with the requested indices. 6434// Calculate the number of indices required 6435unsignedsize =
I->getNumIndices() + idx_range.
size();
6436// Allocate some space to put the new indices in 6439// Add indices from the extract value instruction 6440 Idxs.
append(
I->idx_begin(),
I->idx_end());
6442// Add requested indices 6446 &&
"Number of indices added not correct?");
6450// Otherwise, we don't know (such as, extracting from a function return value 6451// or load instruction) 6457// Make sure the GEP has exactly three arguments. 6458if (
GEP->getNumOperands() != 3)
6461// Make sure the index-ee is a pointer to array of \p CharSize integers. 6463ArrayType *AT = dyn_cast<ArrayType>(
GEP->getSourceElementType());
6467// Check to make sure that the first operand of the GEP is an integer and 6468// has value 0 so that we are sure we're indexing into the initializer. 6469constConstantInt *FirstIdx = dyn_cast<ConstantInt>(
GEP->getOperand(1));
6470if (!FirstIdx || !FirstIdx->
isZero())
6476// If V refers to an initialized global constant, set Slice either to 6477// its initializer if the size of its elements equals ElementSize, or, 6478// for ElementSize == 8, to its representation as an array of unsiged 6479// char. Return true on success. 6480// Offset is in the unit "nr of ElementSize sized elements". 6484assert(V &&
"V should not be null.");
6485assert((ElementSize % 8) == 0 &&
6486"ElementSize expected to be a multiple of the size of a byte.");
6487unsigned ElementSizeInBytes = ElementSize / 8;
6489// Drill down into the pointer expression V, ignoring any intervening 6490// casts, and determine the identity of the object it references along 6491// with the cumulative byte offset into it. 6495// Fail if V is not based on constant global object. 6499APInt Off(
DL.getIndexTypeSizeInBits(V->getType()), 0);
6501if (GV != V->stripAndAccumulateConstantOffsets(
DL, Off,
6502/*AllowNonInbounds*/true))
6503// Fail if a constant offset could not be determined. 6506uint64_t StartIdx = Off.getLimitedValue();
6508// Fail if the constant offset is excessive. 6511// Off/StartIdx is in the unit of bytes. So we need to convert to number of 6512// elements. Simply bail out if that isn't possible. 6513if ((StartIdx % ElementSizeInBytes) != 0)
6516Offset += StartIdx / ElementSizeInBytes;
6522uint64_t SizeInBytes =
DL.getTypeStoreSize(GVTy).getFixedValue();
6525 Slice.
Array =
nullptr;
6527// Return an empty Slice for undersized constants to let callers 6528// transform even undefined library calls into simpler, well-defined 6529// expressions. This is preferable to making the calls although it 6530// prevents sanitizers from detecting such calls. 6536if (
auto *ArrayInit = dyn_cast<ConstantDataArray>(
Init)) {
6537Type *InitElTy = ArrayInit->getElementType();
6539// If Init is an initializer for an array of the expected type 6540// and size, use it as is. 6542 ArrayTy = ArrayInit->getType();
6547if (ElementSize != 8)
6548// TODO: Handle conversions to larger integral types. 6551// Otherwise extract the portion of the initializer starting 6552// at Offset as an array of bytes, and reset Offset. 6558 Array = dyn_cast<ConstantDataArray>(
Init);
6559 ArrayTy = dyn_cast<ArrayType>(
Init->getType());
6566 Slice.
Array = Array;
6572/// Extract bytes from the initializer of the constant array V, which need 6573/// not be a nul-terminated string. On success, store the bytes in Str and 6574/// return true. When TrimAtNul is set, Str will contain only the bytes up 6575/// to but not including the first nul. Return false on failure. 6582if (Slice.
Array ==
nullptr) {
6584// Return a nul-terminated string even for an empty Slice. This is 6585// safe because all existing SimplifyLibcalls callers require string 6586// arguments and the behavior of the functions they fold is undefined 6587// otherwise. Folding the calls this way is preferable to making 6588// the undefined library calls, even though it prevents sanitizers 6589// from reporting such calls. 6597// We cannot instantiate a StringRef as we do not have an appropriate string 6602// Start out with the entire array in the StringRef. 6604// Skip over 'offset' bytes. 6605 Str = Str.substr(Slice.
Offset);
6608// Trim off the \0 and anything after it. If the array is not nul 6609// terminated, we just return the whole end of string. The client may know 6610// some other way that the string is length-bound. 6611 Str = Str.substr(0, Str.find(
'\0'));
6616// These next two are very similar to the above, but also look through PHI 6618// TODO: See if we can integrate these two together. 6620/// If we can compute the length of the string pointed to by 6621/// the specified pointer, return 'len+1'. If we can't, return 0. 6625// Look through noop bitcast instructions. 6626 V = V->stripPointerCasts();
6628// If this is a PHI node, there are two cases: either we have already seen it 6630if (
constPHINode *PN = dyn_cast<PHINode>(V)) {
6631if (!PHIs.
insert(PN).second)
6632return ~0ULL;
// already in the set. 6634// If it was new, see if all the input strings are the same length. 6636for (
Value *IncValue : PN->incoming_values()) {
6638if (Len == 0)
return 0;
// Unknown length -> unknown. 6640if (Len == ~0ULL)
continue;
6642if (Len != LenSoFar && LenSoFar != ~0ULL)
6643return 0;
// Disagree -> unknown. 6647// Success, all agree. 6651// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) 6652if (
constSelectInst *SI = dyn_cast<SelectInst>(V)) {
6654if (Len1 == 0)
return 0;
6656if (Len2 == 0)
return 0;
6657if (Len1 == ~0ULL)
return Len2;
6658if (Len2 == ~0ULL)
return Len1;
6659if (Len1 != Len2)
return 0;
6663// Otherwise, see if we can read the string. 6668if (Slice.
Array ==
nullptr)
6669// Zeroinitializer (including an empty one). 6672// Search for the first nul character. Return a conservative result even 6673// when there is no nul. This is safe since otherwise the string function 6674// being folded such as strlen is undefined, and can be preferable to 6675// making the undefined library call. 6676unsigned NullIndex = 0;
6677for (
unsigned E = Slice.
Length; NullIndex < E; ++NullIndex) {
6682return NullIndex + 1;
6685/// If we can compute the length of the string pointed to by 6686/// the specified pointer, return 'len+1'. If we can't, return 0. 6688if (!V->getType()->isPointerTy())
6693// If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return 6694// an empty string as a length. 6695return Len == ~0ULL ? 1 : Len;
6700bool MustPreserveNullness) {
6702"getArgumentAliasingToReturnedPointer only works on nonnull calls");
6703if (
constValue *RV = Call->getReturnedArgOperand())
6705// This can be used only as a aliasing property. 6707 Call, MustPreserveNullness))
6708return Call->getArgOperand(0);
6713constCallBase *Call,
bool MustPreserveNullness) {
6714switch (Call->getIntrinsicID()) {
6715case Intrinsic::launder_invariant_group:
6716case Intrinsic::strip_invariant_group:
6717case Intrinsic::aarch64_irg:
6718case Intrinsic::aarch64_tagp:
6719// The amdgcn_make_buffer_rsrc function does not alter the address of the 6720// input pointer (and thus preserve null-ness for the purposes of escape 6721// analysis, which is where the MustPreserveNullness flag comes in to play). 6722// However, it will not necessarily map ptr addrspace(N) null to ptr 6723// addrspace(8) null, aka the "null descriptor", which has "all loads return 6724// 0, all stores are dropped" semantics. Given the context of this intrinsic 6725// list, no one should be relying on such a strict interpretation of 6726// MustPreserveNullness (and, at time of writing, they are not), but we 6727// document this fact out of an abundance of caution. 6728case Intrinsic::amdgcn_make_buffer_rsrc:
6730case Intrinsic::ptrmask:
6731return !MustPreserveNullness;
6732case Intrinsic::threadlocal_address:
6733// The underlying variable changes with thread ID. The Thread ID may change 6734// at coroutine suspend points. 6735return !Call->getParent()->getParent()->isPresplitCoroutine();
6741/// \p PN defines a loop-variant pointer to an object. Check if the 6742/// previous iteration of the loop was referring to the same object as \p PN. 6745// Find the loop-defined value. 6750// Find the value from previous iteration. 6752if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
6754if (!PrevValue || LI->
getLoopFor(PrevValue->getParent()) != L)
6757// If a new pointer is loaded in the loop, the pointer references a different 6758// object in every iteration. E.g.: 6762if (
auto *Load = dyn_cast<LoadInst>(PrevValue))
6763if (!L->isLoopInvariant(Load->getPointerOperand()))
6769for (
unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6770if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
6771constValue *PtrOp =
GEP->getPointerOperand();
6777Value *NewV = cast<Operator>(V)->getOperand(0);
6781 }
elseif (
auto *GA = dyn_cast<GlobalAlias>(V)) {
6782if (GA->isInterposable())
6784 V = GA->getAliasee();
6786if (
auto *
PHI = dyn_cast<PHINode>(V)) {
6787// Look through single-arg phi nodes created by LCSSA. 6788if (
PHI->getNumIncomingValues() == 1) {
6789 V =
PHI->getIncomingValue(0);
6792 }
elseif (
auto *Call = dyn_cast<CallBase>(V)) {
6793// CaptureTracking can know about special capturing properties of some 6794// intrinsics like launder.invariant.group, that can't be expressed with 6795// the attributes, but have properties like returning aliasing pointer. 6796// Because some analysis may assume that nocaptured pointer is not 6797// returned from some special intrinsic (because function would have to 6798// be marked with returns attribute), it is crucial to use this function 6799// because it should be in sync with CaptureTracking. Not using it may 6800// cause weird miscompilations where 2 aliasing pointers are assumed to 6810assert(V->getType()->isPointerTy() &&
"Unexpected operand type!");
6817constLoopInfo *LI,
unsigned MaxLookup) {
6828if (
auto *SI = dyn_cast<SelectInst>(
P)) {
6830 Worklist.
push_back(SI->getFalseValue());
6834if (
auto *PN = dyn_cast<PHINode>(
P)) {
6835// If this PHI changes the underlying object in every iteration of the 6836// loop, don't look through it. Consider: 6839// Prev = Curr; // Prev = PHI (Prev_0, Curr) 6843// Prev is tracking Curr one iteration behind so they refer to different 6844// underlying objects. 6854 }
while (!Worklist.
empty());
6858constunsigned MaxVisited = 8;
6863constValue *Object =
nullptr;
6864// Used as fallback if we can't find a common underlying object through 6876if (Visited.
size() == MaxVisited)
6879if (
auto *SI = dyn_cast<SelectInst>(
P)) {
6881 Worklist.
push_back(SI->getFalseValue());
6885if (
auto *PN = dyn_cast<PHINode>(
P)) {
6894 }
while (!Worklist.
empty());
6896return Object ? Object : FirstObject;
6899/// This is the function that does the work of looking through basic 6900/// ptrtoint+arithmetic+inttoptr sequences. 6903if (
constOperator *U = dyn_cast<Operator>(V)) {
6904// If we find a ptrtoint, we can transfer control back to the 6905// regular getUnderlyingObjectFromInt. 6906if (U->getOpcode() == Instruction::PtrToInt)
6907return U->getOperand(0);
6908// If we find an add of a constant, a multiplied value, or a phi, it's 6909// likely that the other operand will lead us to the base 6910// object. We don't have to worry about the case where the 6911// object address is somehow being computed by the multiply, 6912// because our callers only care when the result is an 6913// identifiable object. 6914if (U->getOpcode() != Instruction::Add ||
6915 (!isa<ConstantInt>(U->getOperand(1)) &&
6917 !isa<PHINode>(U->getOperand(1))))
6919 V = U->getOperand(0);
6923assert(V->getType()->isIntegerTy() &&
"Unexpected operand type!");
6927/// This is a wrapper around getUnderlyingObjects and adds support for basic 6928/// ptrtoint+arithmetic+inttoptr sequences. 6929/// It returns false if unidentified object is found in getUnderlyingObjects. 6940for (
constValue *V : Objs) {
6941if (!Visited.
insert(V).second)
6946if (O->getType()->isPointerTy()) {
6951// If getUnderlyingObjects fails to find an identifiable object, 6952// getUnderlyingObjectsForCodeGen also fails for safety. 6959 }
while (!Working.
empty());
6968auto AddWork = [&](
Value *V) {
6969if (Visited.
insert(V).second)
6978if (
AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6979if (Result && Result != AI)
6982 }
elseif (
CastInst *CI = dyn_cast<CastInst>(V)) {
6983 AddWork(CI->getOperand(0));
6984 }
elseif (
PHINode *PN = dyn_cast<PHINode>(V)) {
6985for (
Value *IncValue : PN->incoming_values())
6987 }
elseif (
auto *SI = dyn_cast<SelectInst>(V)) {
6988 AddWork(SI->getTrueValue());
6989 AddWork(SI->getFalseValue());
6991if (OffsetZero && !
GEP->hasAllZeroIndices())
6993 AddWork(
GEP->getPointerOperand());
6994 }
elseif (
CallBase *CB = dyn_cast<CallBase>(V)) {
6995Value *Returned = CB->getReturnedArgOperand();
7003 }
while (!Worklist.
empty());
7009constValue *V,
bool AllowLifetime,
bool AllowDroppable) {
7010for (
constUser *U : V->users()) {
7015if (AllowLifetime &&
II->isLifetimeStartOrEnd())
7018if (AllowDroppable &&
II->isDroppable())
7028 V,
/* AllowLifetime */true,
/* AllowDroppable */false);
7032 V,
/* AllowLifetime */true,
/* AllowDroppable */true);
7036if (
auto *
II = dyn_cast<IntrinsicInst>(
I))
7038auto *Shuffle = dyn_cast<ShuffleVectorInst>(
I);
7039return (!Shuffle || Shuffle->isSelect()) &&
7040 !isa<CallBase, BitCastInst, ExtractElementInst>(
I);
7048bool UseVariableInfo) {
7050 AC, DT, TLI, UseVariableInfo);
7056bool UseVariableInfo) {
7059// Check that the operands are actually compatible with the Opcode override. 7060auto hasEqualReturnAndLeadingOperandTypes =
7061 [](
constInstruction *Inst,
unsigned NumLeadingOperands) {
7065for (
unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7071 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7073 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7080case Instruction::UDiv:
7081case Instruction::URem: {
7082// x / y is undefined if y == 0. 7088case Instruction::SDiv:
7089case Instruction::SRem: {
7090// x / y is undefined if y == 0 or x == INT_MIN and y == -1 7091constAPInt *Numerator, *Denominator;
7094// We cannot hoist this division if the denominator is 0. 7095if (*Denominator == 0)
7097// It's safe to hoist if the denominator is not 0 or -1. 7100// At this point we know that the denominator is -1. It is safe to hoist as 7101// long we know that the numerator is not INT_MIN. 7104// The numerator *might* be MinSignedValue. 7107case Instruction::Load: {
7108if (!UseVariableInfo)
7111constLoadInst *LI = dyn_cast<LoadInst>(Inst);
7121case Instruction::Call: {
7122auto *CI = dyn_cast<const CallInst>(Inst);
7125constFunction *Callee = CI->getCalledFunction();
7127// The called function could have undefined behavior or side-effects, even 7128// if marked readnone nounwind. 7129return Callee && Callee->isSpeculatable();
7131case Instruction::VAArg:
7132case Instruction::Alloca:
7133case Instruction::Invoke:
7134case Instruction::CallBr:
7135case Instruction::PHI:
7136case Instruction::Store:
7137case Instruction::Ret:
7138case Instruction::Br:
7139case Instruction::IndirectBr:
7140case Instruction::Switch:
7141case Instruction::Unreachable:
7142case Instruction::Fence:
7143case Instruction::AtomicRMW:
7144case Instruction::AtomicCmpXchg:
7145case Instruction::LandingPad:
7146case Instruction::Resume:
7147case Instruction::CatchSwitch:
7148case Instruction::CatchPad:
7149case Instruction::CatchRet:
7150case Instruction::CleanupPad:
7151case Instruction::CleanupRet:
7152returnfalse;
// Misc instructions which have effects 7157if (
I.mayReadOrWriteMemory())
7158// Memory dependency possible 7161// Can't move above a maythrow call or infinite loop. Or if an 7162// inalloca alloca, above a stacksave call. 7165// 1) Can't reorder two inf-loop calls, even if readonly 7166// 2) Also can't reorder an inf-loop call below a instruction which isn't 7167// safe to speculative execute. (Inverse of above) 7172/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult. 7187/// Combine constant ranges from computeConstantRange() and computeKnownBits(). 7207// mul nsw of two non-negative numbers is also nuw. 7219// Multiplying n * m significant bits yields a result of n + m significant 7220// bits. If the total number of significant bits does not exceed the 7221// result bit width (minus 1), there is no overflow. 7222// This means if we have enough leading sign bits in the operands 7223// we can guarantee that the result does not overflow. 7224// Ref: "Hacker's Delight" by Henry Warren 7227// Note that underestimating the number of sign bits gives a more 7228// conservative answer. 7232// First handle the easy case: if we have enough sign bits there's 7233// definitely no overflow. 7237// There are two ambiguous cases where there can be no overflow: 7238// SignBits == BitWidth + 1 and 7239// SignBits == BitWidth 7240// The second case is difficult to check, therefore we only handle the 7243// It overflows only when both arguments are negative and the true 7244// product is exactly the minimum negative number. 7245// E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 7246// For simplicity we just check if at least one side is not negative. 7270if (
Add &&
Add->hasNoSignedWrap()) {
7274// If LHS and RHS each have at least two sign bits, the addition will look 7280// If the carry into the most significant position is 0, X and Y can't both 7281// be 1 and therefore the carry out of the addition is also 0. 7283// If the carry into the most significant position is 1, X and Y can't both 7284// be 0 and therefore the carry out of the addition is also 1. 7286// Since the carry into the most significant position is always equal to 7287// the carry out of the addition, there is no signed overflow. 7301// The remaining code needs Add to be available. Early returns if not so. 7305// If the sign of Add is the same as at least one of the operands, this add 7306// CANNOT overflow. If this can be determined from the known bits of the 7307// operands the above signedAddMayOverflow() check will have already done so. 7308// The only other way to improve on the known bits is from an assumption, so 7309// call computeKnownBitsFromContext() directly. 7310bool LHSOrRHSKnownNonNegative =
7312bool LHSOrRHSKnownNegative =
7314if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7317if ((AddKnown.
isNonNegative() && LHSOrRHSKnownNonNegative) ||
7318 (AddKnown.
isNegative() && LHSOrRHSKnownNegative))
7329// The remainder of a value can't have greater magnitude than itself, 7330// so the subtraction can't overflow. 7333// In the minimal case, this would simplify to "?", so there's no subtract 7334// at all. But if this analysis is used to peek through casts, for example, 7335// then determining no-overflow may allow other transforms. 7337// TODO: There are other patterns like this. 7338// See simplifyICmpWithBinOpOnLHS() for candidates. 7362// The remainder of a value can't have greater magnitude than itself, 7363// so the subtraction can't overflow. 7366// In the minimal case, this would simplify to "?", so there's no subtract 7367// at all. But if this analysis is used to peek through casts, for example, 7368// then determining no-overflow may allow other transforms. 7374// If LHS and RHS each have at least two sign bits, the subtraction 7393if (
constauto *EVI = dyn_cast<ExtractValueInst>(U)) {
7394assert(EVI->getNumIndices() == 1 &&
"Obvious from CI's type");
7396if (EVI->getIndices()[0] == 0)
7399assert(EVI->getIndices()[0] == 1 &&
"Obvious from CI's type");
7401for (
constauto *U : EVI->users())
7402if (
constauto *
B = dyn_cast<BranchInst>(U)) {
7403assert(
B->isConditional() &&
"How else is it using an i1?");
7408// We are using the aggregate directly in a way we don't want to analyze 7409// here (storing it to a global, say). 7414auto AllUsesGuardedByBranch = [&](
constBranchInst *BI) {
7419// Check if all users of the add are provably no-wrap. 7420for (
constauto *Result :
Results) {
7421// If the extractvalue itself is not executed on overflow, the we don't 7422// need to check each use separately, since domination is transitive. 7423if (DT.
dominates(NoWrapEdge, Result->getParent()))
7426for (
constauto &RU : Result->uses())
7434returnllvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7437/// Shifts return poison if shiftwidth is larger than the bitwidth. 7439auto *
C = dyn_cast<Constant>(ShiftAmount);
7443// Shifts return poison if shiftwidth is larger than the bitwidth. 7445if (
auto *FVTy = dyn_cast<FixedVectorType>(
C->getType())) {
7446unsigned NumElts = FVTy->getNumElements();
7447for (
unsigned i = 0; i < NumElts; ++i)
7448 ShiftAmounts.
push_back(
C->getAggregateElement(i));
7449 }
elseif (isa<ScalableVectorType>(
C->getType()))
7450returnfalse;
// Can't tell, just return false to be safe 7455auto *CI = dyn_cast_or_null<ConstantInt>(
C);
7456return CI && CI->getValue().ult(
C->getType()->getIntegerBitWidth());
7469return (
unsigned(Kind) &
unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7473return (
unsigned(Kind) &
unsigned(UndefPoisonKind::UndefOnly)) != 0;
7477bool ConsiderFlagsAndMetadata) {
7480Op->hasPoisonGeneratingAnnotations())
7483unsigned Opcode =
Op->getOpcode();
7485// Check whether opcode is a poison/undef-generating operation 7487case Instruction::Shl:
7488case Instruction::AShr:
7489case Instruction::LShr:
7491case Instruction::FPToSI:
7492case Instruction::FPToUI:
7493// fptosi/ui yields poison if the resulting value does not fit in the 7496case Instruction::Call:
7497if (
auto *
II = dyn_cast<IntrinsicInst>(
Op)) {
7498switch (
II->getIntrinsicID()) {
7499// TODO: Add more intrinsics. 7500case Intrinsic::ctlz:
7501case Intrinsic::cttz:
7503if (cast<ConstantInt>(
II->getArgOperand(1))->isNullValue())
7506case Intrinsic::ctpop:
7507case Intrinsic::bswap:
7508case Intrinsic::bitreverse:
7509case Intrinsic::fshl:
7510case Intrinsic::fshr:
7511case Intrinsic::smax:
7512case Intrinsic::smin:
7513case Intrinsic::umax:
7514case Intrinsic::umin:
7515case Intrinsic::ptrmask:
7516case Intrinsic::fptoui_sat:
7517case Intrinsic::fptosi_sat:
7518case Intrinsic::sadd_with_overflow:
7519case Intrinsic::ssub_with_overflow:
7520case Intrinsic::smul_with_overflow:
7521case Intrinsic::uadd_with_overflow:
7522case Intrinsic::usub_with_overflow:
7523case Intrinsic::umul_with_overflow:
7524case Intrinsic::sadd_sat:
7525case Intrinsic::uadd_sat:
7526case Intrinsic::ssub_sat:
7527case Intrinsic::usub_sat:
7529case Intrinsic::sshl_sat:
7530case Intrinsic::ushl_sat:
7534case Intrinsic::fmuladd:
7535case Intrinsic::sqrt:
7536case Intrinsic::powi:
7541case Intrinsic::log10:
7542case Intrinsic::log2:
7544case Intrinsic::exp2:
7545case Intrinsic::exp10:
7546case Intrinsic::fabs:
7547case Intrinsic::copysign:
7548case Intrinsic::floor:
7549case Intrinsic::ceil:
7550case Intrinsic::trunc:
7551case Intrinsic::rint:
7552case Intrinsic::nearbyint:
7553case Intrinsic::round:
7554case Intrinsic::roundeven:
7555case Intrinsic::fptrunc_round:
7556case Intrinsic::canonicalize:
7557case Intrinsic::arithmetic_fence:
7558case Intrinsic::minnum:
7559case Intrinsic::maxnum:
7560case Intrinsic::minimum:
7561case Intrinsic::maximum:
7562case Intrinsic::is_fpclass:
7563case Intrinsic::ldexp:
7564case Intrinsic::frexp:
7566case Intrinsic::lround:
7567case Intrinsic::llround:
7568case Intrinsic::lrint:
7569case Intrinsic::llrint:
7570// If the value doesn't fit an unspecified value is returned (but this 7576case Instruction::CallBr:
7577case Instruction::Invoke: {
7578constauto *CB = cast<CallBase>(
Op);
7579return !CB->hasRetAttr(Attribute::NoUndef);
7581case Instruction::InsertElement:
7582case Instruction::ExtractElement: {
7583// If index exceeds the length of the vector, it returns poison 7584auto *VTy = cast<VectorType>(
Op->getOperand(0)->getType());
7585unsigned IdxOp =
Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7586auto *
Idx = dyn_cast<ConstantInt>(
Op->getOperand(IdxOp));
7589Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7592case Instruction::ShuffleVector: {
7594 ? cast<ConstantExpr>(
Op)->getShuffleMask()
7595 : cast<ShuffleVectorInst>(
Op)->getShuffleMask();
7598case Instruction::FNeg:
7599case Instruction::PHI:
7600case Instruction::Select:
7601case Instruction::URem:
7602case Instruction::SRem:
7603case Instruction::ExtractValue:
7604case Instruction::InsertValue:
7605case Instruction::Freeze:
7606case Instruction::ICmp:
7607case Instruction::FCmp:
7608case Instruction::FAdd:
7609case Instruction::FSub:
7610case Instruction::FMul:
7611case Instruction::FDiv:
7612case Instruction::FRem:
7614case Instruction::GetElementPtr:
7615// inbounds is handled above 7616// TODO: what about inrange on constexpr? 7619constauto *CE = dyn_cast<ConstantExpr>(
Op);
7620if (isa<CastInst>(
Op) || (CE && CE->isCast()))
7624// Be conservative and return true. 7631bool ConsiderFlagsAndMetadata) {
7632 return ::canCreateUndefOrPoison(
Op, UndefPoisonKind::UndefOrPoison,
7633 ConsiderFlagsAndMetadata);
7637 return ::canCreateUndefOrPoison(
Op, UndefPoisonKind::PoisonOnly,
7638 ConsiderFlagsAndMetadata);
7643if (ValAssumedPoison == V)
7646constunsigned MaxDepth = 2;
7647if (
Depth >= MaxDepth)
7650if (
constauto *
I = dyn_cast<Instruction>(V)) {
7652 return propagatesPoison(Op) &&
7653 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7657// V = extractvalue V0, idx 7658// V2 = extractvalue V0, idx2 7659// V0's elements are all poison or not. (e.g., add_with_overflow) 7677constunsigned MaxDepth = 2;
7678if (
Depth >= MaxDepth)
7681constauto *
I = dyn_cast<Instruction>(ValAssumedPoison);
7684 return impliesPoison(Op, V, Depth + 1);
7691 return ::impliesPoison(ValAssumedPoison, V,
/* Depth */ 0);
7702if (isa<MetadataAsValue>(V))
7705if (
constauto *
A = dyn_cast<Argument>(V)) {
7706if (
A->hasAttribute(Attribute::NoUndef) ||
7707A->hasAttribute(Attribute::Dereferenceable) ||
7708A->hasAttribute(Attribute::DereferenceableOrNull))
7712if (
auto *
C = dyn_cast<Constant>(V)) {
7713if (isa<PoisonValue>(
C))
7716if (isa<UndefValue>(
C))
7719if (isa<ConstantInt>(
C) || isa<GlobalVariable>(
C) || isa<ConstantFP>(V) ||
7720 isa<ConstantPointerNull>(
C) || isa<Function>(
C))
7723if (
C->getType()->isVectorTy() && !isa<ConstantExpr>(
C)) {
7728return !
C->containsConstantExpression();
7732// Strip cast operations from a pointer value. 7733// Note that stripPointerCastsSameRepresentation can strip off getelementptr 7734// inbounds with zero offset. To guarantee that the result isn't poison, the 7735// stripped pointer is checked as it has to be pointing into an allocated 7736// object or be null `null` to ensure `inbounds` getelement pointers with a 7737// zero offset could not produce poison. 7738// It can strip off addrspacecast that do not change bit representation as 7739// well. We believe that such addrspacecast is equivalent to no-op. 7740auto *StrippedV = V->stripPointerCastsSameRepresentation();
7741if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7742 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7745auto OpCheck = [&](
constValue *V) {
7749if (
auto *Opr = dyn_cast<Operator>(V)) {
7750// If the value is a freeze instruction, then it can never 7751// be undef or poison. 7752if (isa<FreezeInst>(V))
7755if (
constauto *CB = dyn_cast<CallBase>(V)) {
7756if (CB->hasRetAttr(Attribute::NoUndef) ||
7757 CB->hasRetAttr(Attribute::Dereferenceable) ||
7758 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7762if (
constauto *PN = dyn_cast<PHINode>(V)) {
7763unsigned Num = PN->getNumIncomingValues();
7764bool IsWellDefined =
true;
7765for (
unsigned i = 0; i < Num; ++i) {
7766auto *TI = PN->getIncomingBlock(i)->getTerminator();
7768 DT,
Depth + 1, Kind)) {
7769 IsWellDefined =
false;
7776/*ConsiderFlagsAndMetadata*/true) &&
7777all_of(Opr->operands(), OpCheck))
7781if (
auto *
I = dyn_cast<LoadInst>(V))
7782if (
I->hasMetadata(LLVMContext::MD_noundef) ||
7783I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7784I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7790// CxtI may be null or a cloned instruction. 7799// If V is used as a branch condition before reaching CtxI, V cannot be 7803// CtxI ; V cannot be undef or poison here 7804auto *Dominator = DNode->
getIDom();
7805// This check is purely for compile time reasons: we can skip the IDom walk 7806// if what we are checking for includes undef and the value is not an integer. 7809auto *TI = Dominator->
getBlock()->getTerminator();
7812if (
auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7813if (BI->isConditional())
7814Cond = BI->getCondition();
7815 }
elseif (
auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7816Cond = SI->getCondition();
7823// For poison, we can analyze further 7824auto *Opr = cast<Operator>(
Cond);
7825if (
any_of(Opr->operands(), [V](
constUse &U) {
7826 return V == U && propagatesPoison(U);
7832 Dominator = Dominator->getIDom();
7845 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7846 UndefPoisonKind::UndefOrPoison);
7852 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7853 UndefPoisonKind::PoisonOnly);
7859 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,
Depth,
7860 UndefPoisonKind::UndefOnly);
7863/// Return true if undefined behavior would provably be executed on the path to 7864/// OnPathTo if Root produced a posion result. Note that this doesn't say 7865/// anything about whether OnPathTo is actually executed or whether Root is 7866/// actually poison. This can be used to assess whether a new use of Root can 7867/// be added at a location which is control equivalent with OnPathTo (such as 7868/// immediately before it) without introducing UB which didn't previously 7869/// exist. Note that a false result conveys no information. 7873// Basic approach is to assume Root is poison, propagate poison forward 7874// through all users we can easily track, and then check whether any of those 7875// users are provable UB and must execute before out exiting block might 7878// The set of all recursive users we've visited (which are assumed to all be 7879// poison because of said visit) 7883while (!Worklist.
empty()) {
7886// If we know this must trigger UB on a path leading our target. 7890// If we can't analyze propagation through this instruction, just skip it 7891// and transitive users. Safe as false is a conservative result. 7892if (
I != Root && !
any_of(
I->operands(), [&KnownPoison](
constUse &U) {
7893 return KnownPoison.contains(U) && propagatesPoison(U);
7897if (KnownPoison.
insert(
I).second)
7902// Might be non-UB, or might have a path we couldn't prove must execute on 7903// way to exiting bb. 7909 return ::computeOverflowForSignedAdd(
Add->getOperand(0),
Add->getOperand(1),
7917 return ::computeOverflowForSignedAdd(
LHS,
RHS,
nullptr, SQ);
7921// Note: An atomic operation isn't guaranteed to return in a reasonable amount 7922// of time because it's possible for another thread to interfere with it for an 7923// arbitrary length of time, but programs aren't allowed to rely on that. 7925// If there is no successor, then execution can't transfer to it. 7926if (isa<ReturnInst>(
I))
7928if (isa<UnreachableInst>(
I))
7931// Note: Do not add new checks here; instead, change Instruction::mayThrow or 7932// Instruction::willReturn. 7934// FIXME: Move this check into Instruction::willReturn. 7935if (isa<CatchPadInst>(
I)) {
7938// A catchpad may invoke exception object constructors and such, which 7939// in some languages can be arbitrary code, so be conservative by default. 7942// For CoreCLR, it just involves a type test. 7947// An instruction that returns without throwing must transfer control flow 7949return !
I->mayThrow() &&
I->willReturn();
7953// TODO: This is slightly conservative for invoke instruction since exiting 7954// via an exception *is* normal control for them. 7963unsigned ScanLimit) {
7970assert(ScanLimit &&
"scan limit must be non-zero");
7972if (isa<DbgInfoIntrinsic>(
I))
7974if (--ScanLimit == 0)
7984// The loop header is guaranteed to be executed for every iteration. 7986// FIXME: Relax this constraint to cover all basic blocks that are 7987// guaranteed to be executed at every iteration. 7988if (
I->getParent() != L->getHeader())
returnfalse;
7991if (&LI ==
I)
returntrue;
7994llvm_unreachable(
"Instruction not contained in its own parent basic block.");
7999switch (
I->getOpcode()) {
8000case Instruction::Freeze:
8001case Instruction::PHI:
8002case Instruction::Invoke:
8004case Instruction::Select:
8006case Instruction::Call:
8007if (
auto *
II = dyn_cast<IntrinsicInst>(
I)) {
8008switch (
II->getIntrinsicID()) {
8009// TODO: Add more intrinsics. 8010case Intrinsic::sadd_with_overflow:
8011case Intrinsic::ssub_with_overflow:
8012case Intrinsic::smul_with_overflow:
8013case Intrinsic::uadd_with_overflow:
8014case Intrinsic::usub_with_overflow:
8015case Intrinsic::umul_with_overflow:
8016// If an input is a vector containing a poison element, the 8017// two output vectors (calculated results, overflow bits)' 8018// corresponding lanes are poison. 8020case Intrinsic::ctpop:
8021case Intrinsic::ctlz:
8022case Intrinsic::cttz:
8024case Intrinsic::smax:
8025case Intrinsic::smin:
8026case Intrinsic::umax:
8027case Intrinsic::umin:
8028case Intrinsic::bitreverse:
8029case Intrinsic::bswap:
8030case Intrinsic::sadd_sat:
8031case Intrinsic::ssub_sat:
8032case Intrinsic::sshl_sat:
8033case Intrinsic::uadd_sat:
8034case Intrinsic::usub_sat:
8035case Intrinsic::ushl_sat:
8040case Instruction::ICmp:
8041case Instruction::FCmp:
8042case Instruction::GetElementPtr:
8045if (isa<BinaryOperator>(
I) || isa<UnaryOperator>(
I) || isa<CastInst>(
I))
8048// Be conservative and return false. 8053/// Enumerates all operands of \p I that are guaranteed to not be undef or 8054/// poison. If the callback \p Handle returns true, stop processing and return 8055/// true. Otherwise, return false. 8056template <
typename CallableT>
8058const CallableT &Handle) {
8059switch (
I->getOpcode()) {
8060case Instruction::Store:
8065case Instruction::Load:
8070// Since dereferenceable attribute imply noundef, atomic operations 8071// also implicitly have noundef pointers too 8072case Instruction::AtomicCmpXchg:
8077case Instruction::AtomicRMW:
8082case Instruction::Call:
8083case Instruction::Invoke: {
8087for (
unsigned i = 0; i < CB->
arg_size(); ++i)
8090 CB->
paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8095case Instruction::Ret:
8096if (
I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8097 Handle(
I->getOperand(0)))
8100case Instruction::Switch:
8101if (Handle(cast<SwitchInst>(
I)->getCondition()))
8104case Instruction::Br: {
8105auto *BR = cast<BranchInst>(
I);
8106if (BR->isConditional() && Handle(BR->getCondition()))
8125/// Enumerates all operands of \p I that are guaranteed to not be poison. 8126template <
typename CallableT>
8128const CallableT &Handle) {
8131switch (
I->getOpcode()) {
8132// Divisors of these operations are allowed to be partially undef. 8133case Instruction::UDiv:
8134case Instruction::SDiv:
8135case Instruction::URem:
8136case Instruction::SRem:
8137return Handle(
I->getOperand(1));
8154I, [&](
constValue *V) {
return KnownPoison.
count(V); });
8159// We currently only look for uses of values within the same basic 8160// block, as that makes it easier to guarantee that the uses will be 8161// executed given that Inst is executed. 8163// FIXME: Expand this to consider uses beyond the same basic block. To do 8164// this, look out for the distinction between post-dominance and strong 8168if (
constauto *Inst = dyn_cast<Instruction>(V)) {
8172 }
elseif (
constauto *Arg = dyn_cast<Argument>(V)) {
8173if (Arg->getParent()->isDeclaration())
8176 Begin = BB->
begin();
8181// Limit number of instructions we look at, to avoid scanning through large 8182// blocks. The current limit is chosen arbitrarily. 8183unsigned ScanLimit = 32;
8187// Since undef does not propagate eagerly, be conservative & just check 8188// whether a value is directly passed to an instruction that must take 8189// well-defined operands. 8192if (isa<DbgInfoIntrinsic>(
I))
8194if (--ScanLimit == 0)
8198return WellDefinedOp == V;
8208// Set of instructions that we have proved will yield poison if Inst 8218if (isa<DbgInfoIntrinsic>(
I))
8220if (--ScanLimit == 0)
8227// If an operand is poison and propagates it, mark I as yielding poison. 8228for (
constUse &
Op :
I.operands()) {
8235// Special handling for select, which returns poison if its operand 0 is 8236// poison (handled in the loop above) *or* if both its true/false operands 8237// are poison (handled here). 8238if (
I.getOpcode() == Instruction::Select &&
8239 YieldsPoison.
count(
I.getOperand(1)) &&
8240 YieldsPoison.
count(
I.getOperand(2))) {
8246if (!BB || !Visited.
insert(BB).second)
8256 return ::programUndefinedIfUndefOrPoison(Inst,
false);
8260 return ::programUndefinedIfUndefOrPoison(Inst,
true);
8267if (
auto *
C = dyn_cast<ConstantFP>(V))
8270if (
auto *
C = dyn_cast<ConstantDataVector>(V)) {
8271if (!
C->getElementType()->isFloatingPointTy())
8273for (
unsignedI = 0, E =
C->getNumElements();
I < E; ++
I) {
8274if (
C->getElementAsAPFloat(
I).isNaN())
8280if (isa<ConstantAggregateZero>(V))
8287if (
auto *
C = dyn_cast<ConstantFP>(V))
8290if (
auto *
C = dyn_cast<ConstantDataVector>(V)) {
8291if (!
C->getElementType()->isFloatingPointTy())
8293for (
unsignedI = 0, E =
C->getNumElements();
I < E; ++
I) {
8294if (
C->getElementAsAPFloat(
I).isZero())
8303/// Match clamp pattern for float types without care about NaNs or signed zeros. 8304/// Given non-min/max outer cmp/select from the clamp pattern this 8305/// function recognizes if it can be substitued by a "canonical" min/max 8312// X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) 8313// X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) 8314// and return description of the outer Max/Min. 8316// First, check if select has inverse order: 8317if (CmpRHS == FalseVal) {
8322// Assume success now. If there's no match, callers should not use these anyway. 8355/// Recognize variations of: 8356/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) 8360// Swap the select operands and predicate to match the patterns below. 8361if (CmpRHS != TrueVal) {
8368// (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) 8373// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) 8378// (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) 8383// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) 8391/// Recognize variations of: 8392/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) 8397// TODO: Allow FP min/max with nnan/nsz. 8400Value *
A =
nullptr, *
B =
nullptr;
8405Value *
C =
nullptr, *
D =
nullptr;
8407if (L.Flavor != R.Flavor)
8410// We have something like: x Pred y ? min(a, b) : min(c, d). 8411// Try to match the compare to the min/max operations of the select operands. 8412// First, make sure we have the right compare predicate. 8450// If there is a common operand in the already matched min/max and the other 8451// min/max operands match the compare operands (either directly or inverted), 8452// then this is min/max of the same flavor. 8454// a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) 8455// ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) 8459return {L.Flavor,
SPNB_NA,
false};
8461// a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) 8462// ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) 8466return {L.Flavor,
SPNB_NA,
false};
8468// b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) 8469// ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) 8473return {L.Flavor,
SPNB_NA,
false};
8475// b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) 8476// ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) 8480return {L.Flavor,
SPNB_NA,
false};
8486/// If the input value is the result of a 'not' op, constant integer, or vector 8487/// splat of a constant integer, return the bitwise-not source value. 8488/// TODO: This could be extended to handle non-splat vector integer constants. 8496return ConstantInt::get(V->getType(), ~(*
C));
8501/// Match non-obvious integer minimum and maximum sequences. 8507// Assume success. If there's no match, callers should not use these anyway. 8519// Look through 'not' ops to find disguised min/max. 8520// (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y) 8521// (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y) 8532// (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X) 8533// (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X) 8551// An unsigned min/max can be written with a signed compare. 8553if ((CmpLHS == TrueVal &&
match(FalseVal,
m_APInt(C2))) ||
8555// Is the sign bit set? 8556// (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX 8557// (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN 8561// Is the sign bit clear? 8562// (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX 8563// (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN 8579auto *BO = cast<BinaryOperator>(
X);
8580if (NeedNSW && !BO->hasNoSignedWrap())
8583auto *Zero = cast<Constant>(BO->getOperand(0));
8584if (!AllowPoison && !Zero->isNullValue())
8591if (IsNegationOf(
X,
Y) || IsNegationOf(
Y,
X))
8594// X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) 8603// Handle X = icmp pred A, B, Y = icmp pred A, C. 8610// They must both have samesign flag or not. 8611if (cast<ICmpInst>(
X)->hasSameSign() != cast<ICmpInst>(
Y)->hasSameSign())
8617// Try to infer the relationship from constant ranges. 8618constAPInt *RHSC1, *RHSC2;
8622// Sign bits of two RHSCs should match. 8623if (cast<ICmpInst>(
X)->hasSameSign() &&
8630return CR1.inverse() == CR2;
8664std::optional<std::pair<CmpPredicate, Constant *>>
8667"Only for relational integer predicates.");
8668if (isa<UndefValue>(
C))
8678// Check if the constant operand can be safely incremented/decremented 8679// without overflowing/underflowing. 8680auto ConstantIsOk = [WillIncrement, IsSigned](
ConstantInt *
C) {
8681return WillIncrement ? !
C->isMaxValue(IsSigned) : !
C->isMinValue(IsSigned);
8684Constant *SafeReplacementConstant =
nullptr;
8685if (
auto *CI = dyn_cast<ConstantInt>(
C)) {
8686// Bail out if the constant can't be safely incremented/decremented. 8687if (!ConstantIsOk(CI))
8689 }
elseif (
auto *FVTy = dyn_cast<FixedVectorType>(
Type)) {
8690unsigned NumElts = FVTy->getNumElements();
8691for (
unsigned i = 0; i != NumElts; ++i) {
8692Constant *Elt =
C->getAggregateElement(i);
8696if (isa<UndefValue>(Elt))
8699// Bail out if we can't determine if this constant is min/max or if we 8700// know that this constant is min/max. 8701auto *CI = dyn_cast<ConstantInt>(Elt);
8702if (!CI || !ConstantIsOk(CI))
8705if (!SafeReplacementConstant)
8706 SafeReplacementConstant = CI;
8708 }
elseif (isa<VectorType>(
C->getType())) {
8709// Handle scalable splat 8710Value *SplatC =
C->getSplatValue();
8711auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8712// Bail out if the constant can't be safely incremented/decremented. 8713if (!CI || !ConstantIsOk(CI))
8720// It may not be safe to change a compare predicate in the presence of 8721// undefined elements, so replace those elements with the first safe constant 8723// TODO: in case of poison, it is safe; let's replace undefs only. 8724if (
C->containsUndefOrPoisonElement()) {
8725assert(SafeReplacementConstant &&
"Replacement constant not set");
8731// Increment or decrement the constant. 8732Constant *OneOrNegOne = ConstantInt::get(
Type, WillIncrement ? 1 : -1,
true);
8735return std::make_pair(NewPred, NewC);
8744bool HasMismatchedZeros =
false;
8746// IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one 8747// 0.0 operand, set the compare's 0.0 operands to that same value for the 8748// purpose of identifying min/max. Disregard vector constants with undefined 8749// elements because those can not be back-propagated for analysis. 8750Value *OutputZeroVal =
nullptr;
8752 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8753 OutputZeroVal = TrueVal;
8755 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8756 OutputZeroVal = FalseVal;
8760 HasMismatchedZeros =
true;
8761 CmpLHS = OutputZeroVal;
8764 HasMismatchedZeros =
true;
8765 CmpRHS = OutputZeroVal;
8773// Signed zero may return inconsistent results between implementations. 8774// (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 8775// minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) 8776// Therefore, we behave conservatively and only proceed if at least one of the 8777// operands is known to not be zero or if we don't care about signed zero. 8782if (!HasMismatchedZeros)
8795// When given one NaN and one non-NaN input: 8796// - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. 8797// - A simple C99 (a < b ? a : b) construction will return 'b' (as the 8798// ordered comparison fails), which could be NaN or non-NaN. 8799// so here we discover exactly what NaN behavior is required/accepted. 8804if (LHSSafe && RHSSafe) {
8805// Both operands are known non-NaN. 8808// An ordered comparison will return false when given a NaN, so it 8812// LHS is non-NaN, so if RHS is NaN then NaN will be returned. 8817// Completely unsafe. 8821// An unordered comparison will return true when given a NaN, so it 8824// LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. 8829// Completely unsafe. 8834if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8844// ([if]cmp X, Y) ? X : Y 8845if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8849// Sign-extending LHS does not change its sign, so TrueVal/FalseVal can 8850// match against either LHS or sext(LHS). 8851auto MaybeSExtCmpLHS =
8855if (
match(TrueVal, MaybeSExtCmpLHS)) {
8856// Set the return values. If the compare uses the negated value (-X >s 0), 8857// swap the return values because the negated value is always 'RHS'. 8863// (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) 8864// (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) 8868// (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X) 8872// (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) 8873// (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) 8877elseif (
match(FalseVal, MaybeSExtCmpLHS)) {
8878// Set the return values. If the compare uses the negated value (-X >s 0), 8879// swap the return values because the negated value is always 'RHS'. 8885// (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) 8886// (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) 8890// (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) 8891// (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) 8900// According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar 8901// may return either -0.0 or 0.0, so fcmp/select pair has stricter 8902// semantics than minNum. Be conservative in such case. 8917case Instruction::ZExt:
8921case Instruction::SExt:
8925case Instruction::Trunc:
8928 CmpConst->
getType() == SrcTy) {
8929// Here we have the following case: 8931// %cond = cmp iN %x, CmpConst 8932// %tr = trunc iN %x to iK 8933// %narrowsel = select i1 %cond, iK %t, iK C 8935// We can always move trunc after select operation: 8937// %cond = cmp iN %x, CmpConst 8938// %widesel = select i1 %cond, iN %x, iN CmpConst 8939// %tr = trunc iN %widesel to iK 8941// Note that C could be extended in any way because we don't care about 8942// upper bits after truncation. It can't be abs pattern, because it would 8945// select i1 %cond, x, -x. 8947// So only min/max pattern could be matched. Such match requires widened C 8948// == CmpConst. That is why set widened C = CmpConst, condition trunc 8949// CmpConst == C is checked below. 8950 CastedTo = CmpConst;
8952unsigned ExtOp = CmpI->
isSigned() ? Instruction::SExt : Instruction::ZExt;
8956case Instruction::FPTrunc:
8959case Instruction::FPExt:
8962case Instruction::FPToUI:
8965case Instruction::FPToSI:
8968case Instruction::UIToFP:
8971case Instruction::SIToFP:
8981// Make sure the cast doesn't lose any information. 8984if (CastedBack && CastedBack !=
C)
8990/// Helps to match a select pattern in case of a type mismatch. 8992/// The function processes the case when type of true and false values of a 8993/// select instruction differs from type of the cmp instruction operands because 8994/// of a cast instruction. The function checks if it is legal to move the cast 8995/// operation after "select". If yes, it returns the new second value of 8996/// "select" (with the assumption that cast is moved): 8997/// 1. As operand of cast instruction when both values of "select" are same cast 8999/// 2. As restored constant (by applying reverse cast operation) when the first 9000/// value of the "select" is a cast operation and the second value is a 9001/// constant. It is implemented in lookThroughCastConst(). 9002/// 3. As one operand is cast instruction and the other is not. The operands in 9003/// sel(cmp) are in different type integer. 9004/// NOTE: We return only the new second value because the first value could be 9005/// accessed as operand of cast instruction. 9008auto *Cast1 = dyn_cast<CastInst>(V1);
9012 *CastOp = Cast1->getOpcode();
9013Type *SrcTy = Cast1->getSrcTy();
9014if (
auto *Cast2 = dyn_cast<CastInst>(V2)) {
9015// If V1 and V2 are both the same cast from the same type, look through V1. 9016if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9017return Cast2->getOperand(0);
9021auto *
C = dyn_cast<Constant>(V2);
9025Value *CastedTo =
nullptr;
9026if (*CastOp == Instruction::Trunc) {
9028// Here we have the following case: 9029// %y_ext = sext iK %y to iN 9030// %cond = cmp iN %x, %y_ext 9031// %tr = trunc iN %x to iK 9032// %narrowsel = select i1 %cond, iK %tr, iK %y 9034// We can always move trunc after select operation: 9035// %y_ext = sext iK %y to iN 9036// %cond = cmp iN %x, %y_ext 9037// %widesel = select i1 %cond, iN %x, iN %y_ext 9038// %tr = trunc iN %widesel to iK 9039assert(V2->getType() == Cast1->getType() &&
9040"V2 and Cast1 should be the same type.");
9056CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9059Value *TrueVal = SI->getTrueValue();
9060Value *FalseVal = SI->getFalseValue();
9073if (isa<FPMathOperator>(CmpI))
9080// Deal with type mismatches. 9081if (CastOp && CmpLHS->
getType() != TrueVal->getType()) {
9083// If this is a potential fmin/fmax with a cast to integer, then ignore 9084// -0.0 because there is no corresponding integer value. 9085if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9087 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9088 cast<CastInst>(TrueVal)->getOperand(0),
C,
9092// If this is a potential fmin/fmax with a cast to integer, then ignore 9093// -0.0 because there is no corresponding integer value. 9094if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9096 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9097C, cast<CastInst>(FalseVal)->getOperand(0),
9101 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9120return Intrinsic::umin;
9122return Intrinsic::umax;
9124return Intrinsic::smin;
9126return Intrinsic::smax;
9142case Intrinsic::smax:
return Intrinsic::smin;
9143case Intrinsic::smin:
return Intrinsic::smax;
9144case Intrinsic::umax:
return Intrinsic::umin;
9145case Intrinsic::umin:
return Intrinsic::umax;
9146// Please note that next four intrinsics may produce the same result for 9147// original and inverted case even if X != Y due to NaN is handled specially. 9148case Intrinsic::maximum:
return Intrinsic::minimum;
9149case Intrinsic::minimum:
return Intrinsic::maximum;
9150case Intrinsic::maxnum:
return Intrinsic::minnum;
9151case Intrinsic::minnum:
return Intrinsic::maxnum;
9166std::pair<Intrinsic::ID, bool>
9168// Check if VL contains select instructions that can be folded into a min/max 9169// vector intrinsic and return the intrinsic if it is possible. 9170// TODO: Support floating point min/max. 9171bool AllCmpSingleUse =
true;
9174if (
all_of(VL, [&SelectPattern, &AllCmpSingleUse](
Value *
I) {
9180 SelectPattern.
Flavor != CurrentPattern.Flavor)
9182 SelectPattern = CurrentPattern;
9187switch (SelectPattern.
Flavor) {
9189return {Intrinsic::smin, AllCmpSingleUse};
9191return {Intrinsic::umin, AllCmpSingleUse};
9193return {Intrinsic::smax, AllCmpSingleUse};
9195return {Intrinsic::umax, AllCmpSingleUse};
9197return {Intrinsic::maxnum, AllCmpSingleUse};
9199return {Intrinsic::minnum, AllCmpSingleUse};
9209// Handle the case of a simple two-predecessor recurrence PHI. 9210// There's a lot more that could theoretically be done here, but 9211// this is sufficient to catch some interesting cases. 9212if (
P->getNumIncomingValues() != 2)
9215for (
unsigned i = 0; i != 2; ++i) {
9216Value *L =
P->getIncomingValue(i);
9217Value *R =
P->getIncomingValue(!i);
9218auto *LU = dyn_cast<BinaryOperator>(L);
9221unsigned Opcode = LU->getOpcode();
9226// TODO: Expand list -- xor, gep, uadd.sat etc. 9227case Instruction::LShr:
9228case Instruction::AShr:
9229case Instruction::Shl:
9230case Instruction::Add:
9231case Instruction::Sub:
9232case Instruction::UDiv:
9233case Instruction::URem:
9234case Instruction::And:
9235case Instruction::Or:
9236case Instruction::Mul:
9237case Instruction::FMul: {
9238Value *LL = LU->getOperand(0);
9239Value *LR = LU->getOperand(1);
9240// Find a recurrence. 9246continue;
// Check for recurrence with L and R flipped. 9252// We have matched a recurrence of the form: 9253// %iv = [R, %entry], [%iv.next, %backedge] 9254// %iv.next = binop %iv, L 9256// %iv = [R, %entry], [%iv.next, %backedge] 9257// %iv.next = binop L, %iv 9269P = dyn_cast<PHINode>(
I->getOperand(0));
9271P = dyn_cast<PHINode>(
I->getOperand(1));
9275/// Return true if "icmp Pred LHS RHS" is always true. 9288// LHS s<= LHS +_{nsw} C if C >= 0 9289// LHS s<= LHS | C if C >= 0 9292return !
C->isNegative();
9294// LHS s<= smax(LHS, V) for any V 9298// smin(RHS, V) s<= RHS for any V 9302// Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB) 9304constAPInt *CLHS, *CRHS;
9307return CLHS->
sle(*CRHS);
9313// LHS u<= LHS +_{nuw} V for any V 9318// LHS u<= LHS | V for any V 9322// LHS u<= umax(LHS, V) for any V 9326// RHS >> V u<= RHS for any V 9330// RHS u/ C_ugt_1 u<= RHS 9335// RHS & V u<= RHS for any V 9339// umin(RHS, V) u<= RHS for any V 9343// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) 9345constAPInt *CLHS, *CRHS;
9348return CLHS->
ule(*CRHS);
9355/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred 9356/// ALHS ARHS" is true. Otherwise, return std::nullopt. 9357static std::optional<bool>
9394/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true. 9395/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false. 9396/// Otherwise, return std::nullopt if we can't infer anything. 9397static std::optional<bool>
9402// If all true values for lhs and true for rhs, lhs implies rhs 9403if (CR.
icmp(Pred, RCR))
9406// If there is no overlap, lhs implies not rhs 9426/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") 9427/// is true. Return false if LHS implies RHS is false. Otherwise, return 9428/// std::nullopt if we can't infer anything. 9429static std::optional<bool>
9435// The rest of the logic assumes the LHS condition is true. If that's not the 9436// case, invert the predicate to make it so. 9438 LHSIsTrue ?
LHS->getCmpPredicate() :
LHS->getInverseCmpPredicate();
9440// We can have non-canonical operands, so try to normalize any common operand 9451// If we have L0 == R0 and L1 == R1, then make L1/R1 the constants. 9460// See if we can infer anything if operand-0 matches and we have at least one 9464// Potential TODO: We could also further use the constant range of L0/R0 to 9465// further constraint the constant ranges. At the moment this leads to 9466// several regressions related to not transforming `multi_use(A + C0) eq/ne 9467// C1` (see discussion: D58633). 9474// Even if L1/R1 are not both constant, we can still sometimes deduce 9475// relationship from a single constant. For example X u> Y implies X != 0. 9478// If both L1/R1 were exact constant ranges and we didn't get anything 9479// here, we won't be able to deduce this. 9484// Can we infer anything when the two compares have matching operands? 9485if (L0 == R0 && L1 == R1)
9488// It only really makes sense in the context of signed comparison for "X - Y 9489// must be positive if X >= Y and no overflow". 9490// Take SGT as an example: L0:x > L1:y and C >= 0 9491// ==> R0:(x -nsw y) < R1:(-C) is false 9501// Take SLT as an example: L0:x < L1:y and C <= 0 9502// ==> R0:(x -nsw y) < R1:(-C) is true 9511// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1 9524/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is 9525/// false. Otherwise, return std::nullopt if we can't infer anything. We 9526/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select' 9528static std::optional<bool>
9532// The LHS must be an 'or', 'and', or a 'select' instruction. 9533assert((
LHS->getOpcode() == Instruction::And ||
9534LHS->getOpcode() == Instruction::Or ||
9535LHS->getOpcode() == Instruction::Select) &&
9536"Expected LHS to be 'and', 'or', or 'select'.");
9540// If the result of an 'or' is false, then we know both legs of the 'or' are 9541// false. Similarly, if the result of an 'and' is true, then we know both 9542// legs of the 'and' are true. 9543constValue *ALHS, *ARHS;
9546// FIXME: Make this non-recursion. 9548 ALHS, RHSPred, RHSOp0, RHSOp1,
DL, LHSIsTrue,
Depth + 1))
9551 ARHS, RHSPred, RHSOp0, RHSOp1,
DL, LHSIsTrue,
Depth + 1))
9562// Bail out when we hit the limit. 9566// A mismatch occurs when we compare a scalar cmp to a vector cmp, for 9572"Expected integer type only!");
9576 LHSIsTrue = !LHSIsTrue;
9578// Both LHS and RHS are icmps. 9583 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect 9584 /// the RHS to be an icmp. 9585 /// FIXME: Add support for and/or/select on the RHS. 9587if ((LHSI->getOpcode() == Instruction::And ||
9588 LHSI->getOpcode() == Instruction::Or ||
9589 LHSI->getOpcode() == Instruction::Select))
9598bool LHSIsTrue,
unsignedDepth) {
9599// LHS ==> RHS by definition 9604bool InvertRHS =
false;
9611if (
constICmpInst *RHSCmp = dyn_cast<ICmpInst>(
RHS)) {
9613LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9614 RHSCmp->getOperand(1),
DL, LHSIsTrue,
Depth))
9615return InvertRHS ? !*Implied : *Implied;
9622// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2 9623// LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2 9624constValue *RHS1, *RHS2;
9626if (std::optional<bool> Imp =
9630if (std::optional<bool> Imp =
9636if (std::optional<bool> Imp =
9640if (std::optional<bool> Imp =
9649// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch 9650// condition dominating ContextI or nullptr, if no condition is found. 9651static std::pair<Value *, bool>
9654return {
nullptr,
false};
9656// TODO: This is a poor/cheap way to determine dominance. Should we use a 9657// dominator tree (eg, from a SimplifyQuery) instead? 9661return {
nullptr,
false};
9663// We need a conditional branch in the predecessor. 9667return {
nullptr,
false};
9669// The branch should get simplified. Don't bother simplifying this condition. 9670if (TrueBB == FalseBB)
9671return {
nullptr,
false};
9673assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9674"Predecessor block does not point to successor?");
9676// Is this condition implied by the predecessor condition? 9677return {PredCond, TrueBB == ContextBB};
9683assert(
Cond->getType()->isIntOrIntVectorTy(1) &&
"Condition must be bool");
9704bool PreferSignedRange) {
9705unsigned Width =
Lower.getBitWidth();
9708case Instruction::Add:
9713// If the caller expects a signed compare, then try to use a signed range. 9714// Otherwise if both no-wraps are set, use the unsigned range because it 9715// is never larger than the signed range. Example: 9716// "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125]. 9717if (PreferSignedRange && HasNSW && HasNUW)
9721// 'add nuw x, C' produces [C, UINT_MAX]. 9724if (
C->isNegative()) {
9725// 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C]. 9729// 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX]. 9737case Instruction::And:
9739// 'and x, C' produces [0, C]. 9741// X & -X is a power of two or zero. So we can cap the value at max power of 9748case Instruction::Or:
9750// 'or x, C' produces [C, UINT_MAX]. 9754case Instruction::AShr:
9756// 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C]. 9760unsigned ShiftAmount = Width - 1;
9761if (!
C->isZero() && IIQ.
isExact(&BO))
9762 ShiftAmount =
C->countr_zero();
9763if (
C->isNegative()) {
9764// 'ashr C, x' produces [C, C >> (Width-1)] 9766Upper =
C->ashr(ShiftAmount) + 1;
9768// 'ashr C, x' produces [C >> (Width-1), C] 9769Lower =
C->ashr(ShiftAmount);
9775case Instruction::LShr:
9777// 'lshr x, C' produces [0, UINT_MAX >> C]. 9780// 'lshr C, x' produces [C >> (Width-1), C]. 9781unsigned ShiftAmount = Width - 1;
9782if (!
C->isZero() && IIQ.
isExact(&BO))
9783 ShiftAmount =
C->countr_zero();
9784Lower =
C->lshr(ShiftAmount);
9789case Instruction::Shl:
9792// 'shl nuw C, x' produces [C, C << CLZ(C)] 9796if (
C->isNegative()) {
9797// 'shl nsw C, x' produces [C << CLO(C)-1, C] 9798unsigned ShiftAmount =
C->countl_one() - 1;
9799Lower =
C->shl(ShiftAmount);
9802// 'shl nsw C, x' produces [C, C << CLZ(C)-1] 9803unsigned ShiftAmount =
C->countl_zero() - 1;
9805Upper =
C->shl(ShiftAmount) + 1;
9808// If lowbit is set, value can never be zero. 9811// If we are shifting a constant the largest it can be is if the longest 9812// sequence of consecutive ones is shifted to the highbits (breaking 9813// ties for which sequence is higher). At the moment we take a liberal 9814// upper bound on this by just popcounting the constant. 9815// TODO: There may be a bitwise trick for it longest/highest 9816// consecutative sequence of ones (naive method is O(Width) loop). 9824case Instruction::SDiv:
9828if (
C->isAllOnes()) {
9829// 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX] 9830// where C != -1 and C != 0 and C != 1 9833 }
elseif (
C->countl_zero() < Width - 1) {
9834// 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C] 9835// where C != -1 and C != 0 and C != 1 9844if (
C->isMinSignedValue()) {
9845// 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2]. 9849// 'sdiv C, x' produces [-|C|, |C|]. 9856case Instruction::UDiv:
9858// 'udiv x, C' produces [0, UINT_MAX / C]. 9861// 'udiv C, x' produces [0, C]. 9866case Instruction::SRem:
9868// 'srem x, C' produces (-|C|, |C|). 9872if (
C->isNegative()) {
9873// 'srem -|C|, x' produces [-|C|, 0]. 9877// 'srem |C|, x' produces [0, |C|]. 9883case Instruction::URem:
9885// 'urem x, C' produces [0, C). 9888// 'urem C, x' produces [0, C]. 9899unsigned Width =
II.getType()->getScalarSizeInBits();
9901switch (
II.getIntrinsicID()) {
9902case Intrinsic::ctlz:
9903case Intrinsic::cttz: {
9905if (!UseInstrInfo || !
match(
II.getArgOperand(1),
m_One()))
9907// Maximum of set/clear bits is the bit width. 9910case Intrinsic::ctpop:
9911// Maximum of set/clear bits is the bit width. 9913APInt(Width, Width) + 1);
9914case Intrinsic::uadd_sat:
9915// uadd.sat(x, C) produces [C, UINT_MAX]. 9920case Intrinsic::sadd_sat:
9924// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)]. 9929// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX]. 9934case Intrinsic::usub_sat:
9935// usub.sat(C, x) produces [0, C]. 9939// usub.sat(x, C) produces [0, UINT_MAX - C]. 9944case Intrinsic::ssub_sat:
9947// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)]. 9952// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX]. 9957// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]: 9961// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C]. 9967case Intrinsic::umin:
9968case Intrinsic::umax:
9969case Intrinsic::smin:
9970case Intrinsic::smax:
9975switch (
II.getIntrinsicID()) {
9976case Intrinsic::umin:
9978case Intrinsic::umax:
9980case Intrinsic::smin:
9983case Intrinsic::smax:
9991// If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX], 9992// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. 9999case Intrinsic::vscale:
10000if (!
II.getParent() || !
II.getFunction())
10003case Intrinsic::scmp:
10004case Intrinsic::ucmp:
10011return ConstantRange::getFull(Width);
10016unsignedBitWidth = SI.getType()->getScalarSizeInBits();
10020return ConstantRange::getFull(
BitWidth);
10023// If the negation part of the abs (in RHS) has the NSW flag, 10024// then the result of abs(X) is [0..SIGNED_MAX], 10025// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN. 10036// The result of -abs(X) is <= 0. 10043return ConstantRange::getFull(
BitWidth);
10057return ConstantRange::getFull(
BitWidth);
10062// The maximum representable value of a half is 65504. For floats the maximum 10063// value is 3.4e38 which requires roughly 129 bits. 10064unsignedBitWidth =
I->getType()->getScalarSizeInBits();
10065if (!
I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10067if (isa<FPToSIInst>(
I) &&
BitWidth >= 17) {
10072if (isa<FPToUIInst>(
I) &&
BitWidth >= 16) {
10073// For a fptoui the lower limit is left as 0. 10083assert(V->getType()->isIntOrIntVectorTy() &&
"Expected integer instruction");
10086return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10088if (
auto *
C = dyn_cast<Constant>(V))
10089returnC->toConstantRange();
10091unsignedBitWidth = V->getType()->getScalarSizeInBits();
10094if (
auto *BO = dyn_cast<BinaryOperator>(V)) {
10097// TODO: Return ConstantRange. 10100 }
elseif (
auto *
II = dyn_cast<IntrinsicInst>(V))
10102elseif (
auto *SI = dyn_cast<SelectInst>(V)) {
10104 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,
Depth + 1);
10106 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,
Depth + 1);
10109 }
elseif (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10112// TODO: Return ConstantRange. 10115 }
elseif (
constauto *
A = dyn_cast<Argument>(V))
10116if (std::optional<ConstantRange>
Range =
A->getRange())
10119if (
auto *
I = dyn_cast<Instruction>(V)) {
10123if (
constauto *CB = dyn_cast<CallBase>(V))
10124if (std::optional<ConstantRange>
Range = CB->getRange())
10129// Try to restrict the range based on information from assumptions. 10135"Got assumption for the wrong function!");
10136assert(
I->getIntrinsicID() == Intrinsic::assume &&
10137"must be an assume intrinsic");
10141Value *Arg =
I->getArgOperand(0);
10142ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10143// Currently we just use information from comparisons. 10144if (!Cmp || Cmp->getOperand(0) != V)
10146// TODO: Set "ForSigned" parameter via Cmp->isSigned()? 10149 UseInstrInfo, AC,
I, DT,
Depth + 1);
10162if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10164 }
elseif (
auto *
I = dyn_cast<Instruction>(V)) {
10167// Peek through unary operators to find the source of the condition. 10170if (isa<Instruction>(
Op) || isa<Argument>(
Op))
10171 InsertAffected(
Op);
10178auto AddAffected = [&InsertAffected](
Value *V) {
10193while (!Worklist.
empty()) {
10195if (!Visited.
insert(V).second)
10208// assume(A && B) is split to -> assume(A); assume(B); 10209// assume(!(A || B)) is split to -> assume(!A); assume(!B); 10210// Finally, assume(A || B) / assume(!(A && B)) generally don't provide 10211// enough information to be worth handling (intersection of information as 10212// opposed to union). 10218 AddCmpOperands(
A,
B);
10224// (X & C) or (X | C) or (X ^ C). 10225// (X << C) or (X >>_s C) or (X >>_u C). 10237// Handle (A + C1) u< C2, which is the canonical form of 10238// A > C3 && A < C4. 10244// X & Y u> C -> X >u C && Y >u C 10245// X | Y u< C -> X u< C && Y u< C 10246// X nuw+ Y u< C -> X u< C && Y u< C 10253// X nuw- Y u> C -> X u> C 10259// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported 10260// by computeKnownFPClass(). 10269if (HasRHSC &&
match(
A, m_Intrinsic<Intrinsic::ctpop>(
m_Value(
X))))
10272 AddCmpOperands(
A,
B);
10276// fcmp fneg(fabs(x)), y 10282 }
elseif (
match(V, m_Intrinsic<Intrinsic::is_fpclass>(
m_Value(
A),
10284// 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 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 void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut)
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
Iterator returning form of getFirstNonPHI.
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