1//===- InstructionSimplify.cpp - Fold instruction operands ----------------===// 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 implements routines for folding instructions into simpler forms 10// that do not require creating new instructions. This does constant folding 11// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either 12// returning a constant ("and i32 %x, 0" -> "0") or an already existing value 13// ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been 14// simplified: This is usually true and assuming it simplifies the logic (if 15// they have not been simplified then results are correct but maybe suboptimal). 17//===----------------------------------------------------------------------===// 50#define DEBUG_TYPE "instsimplify" 84/// For a boolean type or a vector of boolean type, return false or a vector 85/// with every element false. 88/// For a boolean type or a vector of boolean type, return true or a vector 89/// with every element true. 92/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? 94CmpInst *Cmp = dyn_cast<CmpInst>(V);
98Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
99if (CPred == Pred && CLHS ==
LHS && CRHS ==
RHS)
105/// Simplify comparison with true or false branch of select: 106/// %sel = select i1 %cond, i32 %tv, i32 %fv 107/// %cmp = icmp sle i32 %sel, %rhs 108/// Compose new comparison by substituting %sel with either %tv or %fv 109/// and see if it simplifies. 112unsigned MaxRecurse,
Constant *TrueOrFalse) {
114if (SimplifiedCmp ==
Cond) {
115// %cmp simplified to the select condition (%cond). 118// It didn't simplify. However, if composed comparison is equivalent 119// to the select condition (%cond) then we can replace it. 125/// Simplify comparison with true branch of select 128unsigned MaxRecurse) {
133/// Simplify comparison with false branch of select 136unsigned MaxRecurse) {
141/// We know comparison with both branches of select can be simplified, but they 142/// are not equal. This routine handles some logical simplifications. 146unsigned MaxRecurse) {
147// If the false value simplified to false, then the result of the compare 148// is equal to "Cond && TCmp". This also catches the case when the false 149// value simplified to false and the true value to true, returning "Cond". 150// Folding select to and/or isn't poison-safe in general; impliesPoison 151// checks whether folding it does not convert a well-defined value into 156// If the true value simplified to true, then the result of the compare 157// is equal to "Cond || FCmp". 161// Finally, if the false value simplified to true and the true value to 162// false, then the result of the compare is equal to "!Cond". 170/// Does the given value dominate the specified phi node? 174// Arguments and constants dominate all instructions. 177// If we have a DominatorTree then do a precise test. 181// Otherwise, if the instruction is in the entry block and is not an invoke, 182// then it obviously dominates all phi nodes. 183if (
I->getParent()->isEntryBlock() && !isa<InvokeInst>(
I) &&
190/// Try to simplify a binary operator of form "V op OtherOp" where V is 191/// "(B0 opex B1)" by distributing 'op' across 'opex' as 192/// "(B0 op OtherOp) opex (B1 op OtherOp)". 196auto *
B = dyn_cast<BinaryOperator>(V);
197if (!
B ||
B->getOpcode() != OpcodeToExpand)
199Value *B0 =
B->getOperand(0), *B1 =
B->getOperand(1);
209// Does the expanded pair of binops simplify to the existing binop? 210if ((L == B0 && R == B1) ||
216// Otherwise, return "L op' R" if it simplifies. 225/// Try to simplify binops of form "A op (B op' C)" or the commuted variant by 226/// distributing op over op'. 231unsigned MaxRecurse) {
232// Recursion is always used, so bail out at once if we already hit the limit. 243/// Generic simplifications for associative binary operations. 244/// Returns the simpler value, or null if none was found. 248unsigned MaxRecurse) {
251// Recursion is always used, so bail out at once if we already hit the limit. 258// Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. 264// Does "B op C" simplify? 266// It does! Return "A op V" if it simplifies or is already available. 267// If V equals B then "A op V" is just the LHS. 270// Otherwise return "A op V" if it simplifies. 278// Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. 284// Does "A op B" simplify? 286// It does! Return "V op C" if it simplifies or is already available. 287// If V equals B then "V op C" is just the RHS. 290// Otherwise return "V op C" if it simplifies. 298// The remaining transforms require commutativity as well as associativity. 302// Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. 308// Does "C op A" simplify? 310// It does! Return "V op B" if it simplifies or is already available. 311// If V equals A then "V op B" is just the LHS. 314// Otherwise return "V op B" if it simplifies. 322// Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. 328// Does "C op A" simplify? 330// It does! Return "B op V" if it simplifies or is already available. 331// If V equals C then "B op V" is just the RHS. 334// Otherwise return "B op V" if it simplifies. 345/// In the case of a binary operation with a select instruction as an operand, 346/// try to simplify the binop by seeing whether evaluating it on both branches 347/// of the select results in the same value. Returns the common value if so, 348/// otherwise returns null. 351unsigned MaxRecurse) {
352// Recursion is always used, so bail out at once if we already hit the limit. 357if (isa<SelectInst>(
LHS)) {
358 SI = cast<SelectInst>(
LHS);
360assert(isa<SelectInst>(
RHS) &&
"No select instruction operand!");
361 SI = cast<SelectInst>(
RHS);
364// Evaluate the BinOp on the true and false branches of the select. 375// If they simplified to the same value, then return the common value. 376// If they both failed to simplify then return null. 380// If one branch simplified to undef, return the other one. 386// If applying the operation did not change the true and false select values, 387// then the result of the binop is the select itself. 388if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
391// If one branch simplified and the other did not, and the simplified 392// value is equal to the unsimplified one, return the simplified value. 393// For example, select (cond, X, X & Z) & Z -> X & Z. 394if ((FV && !TV) || (TV && !FV)) {
395// Check that the simplified value has the form "X op Y" where "op" is the 396// same as the original operation. 397Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
398if (Simplified && Simplified->getOpcode() ==
unsigned(Opcode) &&
399 !Simplified->hasPoisonGeneratingFlags()) {
400// The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". 401// We already know that "op" is the same as for the simplified value. See 402// if the operands match too. If so, return the simplified value. 403Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
404Value *UnsimplifiedLHS = SI ==
LHS ? UnsimplifiedBranch :
LHS;
405Value *UnsimplifiedRHS = SI ==
LHS ?
RHS : UnsimplifiedBranch;
406if (Simplified->getOperand(0) == UnsimplifiedLHS &&
407 Simplified->getOperand(1) == UnsimplifiedRHS)
409if (Simplified->isCommutative() &&
410 Simplified->getOperand(1) == UnsimplifiedLHS &&
411 Simplified->getOperand(0) == UnsimplifiedRHS)
419/// In the case of a comparison with a select instruction, try to simplify the 420/// comparison by seeing whether both branches of the select result in the same 421/// value. Returns the common value if so, otherwise returns null. 422/// For example, if we have: 423/// %tmp = select i1 %cmp, i32 1, i32 2 424/// %cmp1 = icmp sle i32 %tmp, 3 425/// We can simplify %cmp1 to true, because both branches of select are 426/// less than 3. We compose new comparison by substituting %tmp with both 427/// branches of select and see if it can be simplified. 430// Recursion is always used, so bail out at once if we already hit the limit. 434// Make sure the select is on the LHS. 435if (!isa<SelectInst>(
LHS)) {
439assert(isa<SelectInst>(
LHS) &&
"Not comparing with a select instruction!");
442Value *TV = SI->getTrueValue();
443Value *FV = SI->getFalseValue();
445// Now that we have "cmp select(Cond, TV, FV), RHS", analyse it. 446// Does "cmp TV, RHS" simplify? 451// Does "cmp FV, RHS" simplify? 456// If both sides simplified to the same value, then use it as the result of 457// the original comparison. 461// The remaining cases only make sense if the select condition has the same 462// type as the result of the comparison, so bail out if this is not so. 469/// In the case of a binary operation with an operand that is a PHI instruction, 470/// try to simplify the binop by seeing whether evaluating it on the incoming 471/// phi values yields the same result for every value. If so returns the common 472/// value, otherwise returns null. 475unsigned MaxRecurse) {
476// Recursion is always used, so bail out at once if we already hit the limit. 481if (isa<PHINode>(
LHS)) {
482 PI = cast<PHINode>(
LHS);
483// Bail out if RHS and the phi may be mutually interdependent due to a loop. 487assert(isa<PHINode>(
RHS) &&
"No PHI instruction operand!");
488 PI = cast<PHINode>(
RHS);
489// Bail out if LHS and the phi may be mutually interdependent due to a loop. 494// Evaluate the BinOp on the incoming phi values. 495Value *CommonValue =
nullptr;
497// If the incoming value is the phi node itself, it can safely be skipped. 506// If the operation failed to simplify, or simplified to a different value 507// to previously, then give up. 508if (!V || (CommonValue && V != CommonValue))
516/// In the case of a comparison with a PHI instruction, try to simplify the 517/// comparison by seeing whether comparing with all of the incoming phi values 518/// yields the same result every time. If so returns the common result, 519/// otherwise returns null. 522// Recursion is always used, so bail out at once if we already hit the limit. 526// Make sure the phi is on the LHS. 527if (!isa<PHINode>(
LHS)) {
531assert(isa<PHINode>(
LHS) &&
"Not comparing with a phi instruction!");
534// Bail out if RHS and the phi may be mutually interdependent due to a loop. 538// Evaluate the BinOp on the incoming phi values. 539Value *CommonValue =
nullptr;
543// If the incoming value is the phi node itself, it can safely be skipped. 546// Change the context instruction to the "edge" that flows into the phi. 547// This is important because that is where incoming is actually "evaluated" 548// even though it is used later somewhere else. 551// If the operation failed to simplify, or simplified to a different value 552// to previously, then give up. 553if (!V || (CommonValue && V != CommonValue))
564if (
auto *CLHS = dyn_cast<Constant>(Op0)) {
565if (
auto *CRHS = dyn_cast<Constant>(Op1)) {
569case Instruction::FAdd:
570case Instruction::FSub:
571case Instruction::FMul:
572case Instruction::FDiv:
573case Instruction::FRem:
580// Canonicalize the constant to the RHS if this is a commutative operation. 587/// Given operands for an Add, see if we can fold the result. 588/// If not, this returns null. 594// X + poison -> poison 595if (isa<PoisonValue>(Op1))
606// If two operands are negative, return 0. 618// X + ~X -> -1 since ~X = -X-1 623// add nsw/nuw (xor Y, signmask), signmask --> Y 624// The no-wrapping add guarantees that the top bit will be set by the add. 625// Therefore, the xor must be clearing the already set sign bit of Y. 630// add nuw %x, -1 -> -1, because %x can only be 0. 632return Op1;
// Which is -1. 639// Try some generic simplifications for associative operations. 644// Threading Add over selects and phi nodes is pointless, so don't bother. 645// Threading over the select in "A + select(cond, B, C)" means evaluating 646// "A+B" and "A+C" and seeing if they are equal; but they are equal if and 647// only if B and C are equal. If B and C are equal then (since we assume 648// that operands have already been simplified) "select(cond, B, C)" should 649// have been simplified to the common value of B and C already. Analysing 650// "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly 651// for threading over phi nodes. 658 return ::simplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query,
RecursionLimit);
661/// Compute the base pointer and cumulative constant offsets for V. 663/// This strips all constant offsets off of V, leaving it the base pointer, and 664/// accumulates the total constant offset applied in the returned constant. 665/// It returns zero if there are no constant offsets applied. 667/// This is very similar to stripAndAccumulateConstantOffsets(), except it 668/// normalizes the offset bitwidth to the stripped pointer type, not the 669/// original pointer type. 671bool AllowNonInbounds =
false) {
672assert(V->getType()->isPtrOrPtrVectorTy());
675 V = V->stripAndAccumulateConstantOffsets(
DL,
Offset, AllowNonInbounds);
676// As that strip may trace through `addrspacecast`, need to sext or trunc 677// the offset calculated. 678returnOffset.sextOrTrunc(
DL.getIndexTypeSizeInBits(V->getType()));
681/// Compute the constant difference between two pointer values. 682/// If the difference is not a constant, returns zero. 688// If LHS and RHS are not related via constant offsets to the same base 689// value, there is nothing we can do here. 693// Otherwise, the difference of LHS - RHS can be computed as: 695// = (LHSOffset + Base) - (RHSOffset + Base) 696// = LHSOffset - RHSOffset 698if (
auto *VecTy = dyn_cast<VectorType>(
LHS->
getType()))
703/// Test if there is a dominating equivalence condition for the 704/// two operands. If there is, try to reduce the binary operation 705/// between the two operands. 706/// Example: Op0 - Op1 --> 0 when Op0 == Op1 709// Recursive run it can not get any benefit 713 std::optional<bool> Imp =
718case Instruction::Sub:
719case Instruction::Xor:
720case Instruction::URem:
721case Instruction::SRem:
724case Instruction::SDiv:
725case Instruction::UDiv:
726return ConstantInt::get(Ty, 1);
728case Instruction::And:
730// Could be either one - choose Op1 since that's more likely a constant. 739/// Given operands for a Sub, see if we can fold the result. 740/// If not, this returns null. 746// X - poison -> poison 747// poison - X -> poison 748if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
764// Is this a negation? 766// 0 - X -> 0 if the sub is NUW. 772// Op1 is either 0 or the minimum signed value. If the sub is NSW, then 773// Op1 must be 0 because negating the minimum signed value is undefined. 777// 0 - X -> X if X is 0 or the minimum signed value. 782// (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. 783// For example, (X + Y) - Y -> X; (Y + X) - Y -> X 784Value *
X =
nullptr, *
Y =
nullptr, *Z = Op1;
786// See if "V === Y - Z" simplifies. 788// It does! Now see if "X + V" simplifies. 790// It does, we successfully reassociated! 794// See if "V === X - Z" simplifies. 796// It does! Now see if "Y + V" simplifies. 798// It does, we successfully reassociated! 804// X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. 805// For example, X - (X + 1) -> -1 808// See if "V === X - Y" simplifies. 810// It does! Now see if "V - Z" simplifies. 812// It does, we successfully reassociated! 816// See if "V === X - Z" simplifies. 818// It does! Now see if "V - Y" simplifies. 820// It does, we successfully reassociated! 826// Z - (X - Y) -> (Z - X) + Y if everything simplifies. 827// For example, X - (X - Y) -> Y. 830// See if "V === Z - X" simplifies. 832// It does! Now see if "V + Y" simplifies. 834// It does, we successfully reassociated! 839// trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies. 842if (
X->getType() ==
Y->getType())
843// See if "V === X - Y" simplifies. 845// It does! Now see if "trunc V" simplifies. 848// It does, return the simplified "trunc V". 851// Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). 862// Threading Sub over selects and phi nodes is pointless, so don't bother. 863// Threading over the select in "A - select(cond, B, C)" means evaluating 864// "A-B" and "A-C" and seeing if they are equal; but they are equal if and 865// only if B and C are equal. If B and C are equal then (since we assume 866// that operands have already been simplified) "select(cond, B, C)" should 867// have been simplified to the common value of B and C already. Analysing 868// "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly 869// for threading over phi nodes. 874// (sub nuw C_Mask, (xor X, C_Mask)) -> X 887 return ::simplifySubInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
890/// Given operands for a Mul, see if we can fold the result. 891/// If not, this returns null. 897// X * poison -> poison 898if (isa<PoisonValue>(Op1))
910// (X / Y) * Y -> X if the division is exact. 919// mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not 920// representable). All other cases reduce to 0, so just return 0. 922return ConstantInt::getNullValue(Op0->
getType());
924// Treat "mul i1" as "and i1". 930// Try some generic simplifications for associative operations. 935// Mul distributes over Add. Try some generic simplifications based on this. 937 Instruction::Add, Q, MaxRecurse))
940// If the operation is with the result of a select instruction, check whether 941// operating on either branch of the select always yields the same value. 942if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
947// If the operation is with the result of a phi instruction, check whether 948// operating on all incoming values of the phi always yields the same value. 949if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
959 return ::simplifyMulInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
962/// Given a predicate and two operands, return true if the comparison is true. 963/// This is a helper for div/rem simplification where we return some other value 964/// when we can prove a relationship between the operands. 968Constant *
C = dyn_cast_or_null<Constant>(V);
969return (
C &&
C->isAllOnesValue());
972/// Return true if we can simplify X / Y to 0. Remainder can adapt that answer 973/// to simplify X % Y to X. 975unsigned MaxRecurse,
bool IsSigned) {
976// Recursion is always used, so bail out at once if we already hit the limit. 981// (X srem Y) sdiv Y --> 0 987// We require that 1 operand is a simple constant. That could be extended to 988// 2 variables if we computed the sign bit for each. 990// Make sure that a constant is not the minimum signed value because taking 991// the abs() of that is undefined. 992Type *Ty =
X->getType();
995// Is the variable divisor magnitude always greater than the constant 996// dividend magnitude? 997// |Y| > |C| --> Y < -abs(C) or Y > abs(C) 998Constant *PosDividendC = ConstantInt::get(Ty,
C->abs());
999Constant *NegDividendC = ConstantInt::get(Ty, -
C->abs());
1005// Special-case: we can't take the abs() of a minimum signed value. If 1006// that's the divisor, then all we have to do is prove that the dividend 1007// is also not the minimum signed value. 1008if (
C->isMinSignedValue())
1011// Is the variable dividend magnitude always less than the constant 1012// divisor magnitude? 1013// |X| < |C| --> X > -abs(C) and X < abs(C) 1014Constant *PosDivisorC = ConstantInt::get(Ty,
C->abs());
1015Constant *NegDivisorC = ConstantInt::get(Ty, -
C->abs());
1023// IsSigned == false. 1025// Is the unsigned dividend known to be less than a constant divisor? 1026// TODO: Convert this (and above) to range analysis 1027// ("computeConstantRangeIncludingKnownBits")? 1033// Try again for any divisor: 1034// Is the dividend unsigned less than the divisor? 1035returnisICmpTrue(ICmpInst::ICMP_ULT,
X,
Y, Q, MaxRecurse);
1038/// Check for common or similar folds of integer division or integer remainder. 1039/// This applies to all 4 opcodes (sdiv/udiv/srem/urem). 1042unsigned MaxRecurse) {
1043bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
1044bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
1048// X / undef -> poison 1049// X % undef -> poison 1055// We don't need to preserve faults! 1059// poison / X -> poison 1060// poison % X -> poison 1061if (isa<PoisonValue>(Op0))
1082// If the divisor is known to be zero, just return poison. This can happen in 1083// some cases where its provable indirectly the denominator is zero but it's 1084// not trivially simplifiable (i.e known zero through a phi node). 1090// If the divisor can only be zero or one, we can't have division-by-zero 1091// or remainder-by-zero, so assume the divisor is 1. 1092// e.g. 1, zext (i8 X), sdiv X (Y and 1) 1096// If X * Y does not overflow, then: 1101auto *
Mul = cast<OverflowingBinaryOperator>(Op0);
1102// The multiplication can't overflow if it is defined not to, or if 1103// X == A / Y for some A. 1112if (
isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
1118// If the operation is with the result of a select instruction, check whether 1119// operating on either branch of the select always yields the same value. 1120if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1124// If the operation is with the result of a phi instruction, check whether 1125// operating on all incoming values of the phi always yields the same value. 1126if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1133/// These are simplifications common to SDiv and UDiv. 1136unsigned MaxRecurse) {
1145// If this is an exact divide by a constant, then the dividend (Op0) must 1146// have at least as many trailing zeros as the divisor to divide evenly. If 1147// it has less trailing zeros, then the result must be poison. 1154// udiv exact (mul nsw X, C), C --> X 1155// sdiv exact (mul nuw X, C), C --> X 1156// where C is not a power of 2. 1159 (Opcode == Instruction::UDiv
1168/// These are simplifications common to SRem and URem. 1179if ((Opcode == Instruction::SRem &&
1181 (Opcode == Instruction::URem &&
1187// (srem (mul nsw X, C1), C0) -> 0 if C1 s% C0 == 0 1188// (urem (mul nuw X, C1), C0) -> 0 if C1 u% C0 == 0 1189if (Opcode == Instruction::SRem
1192returnC.srem(*C0).isZero();
1196returnC.urem(*C0).isZero();
1204/// Given operands for an SDiv, see if we can fold the result. 1205/// If not, this returns null. 1208// If two operands are negated and no signed overflow, return -1. 1212returnsimplifyDiv(Instruction::SDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1220/// Given operands for a UDiv, see if we can fold the result. 1221/// If not, this returns null. 1224returnsimplifyDiv(Instruction::UDiv, Op0, Op1, IsExact, Q, MaxRecurse);
1232/// Given operands for an SRem, see if we can fold the result. 1233/// If not, this returns null. 1235unsigned MaxRecurse) {
1236// If the divisor is 0, the result is undefined, so assume the divisor is -1. 1237// srem Op0, (sext i1 X) --> srem Op0, -1 --> 0 1240return ConstantInt::getNullValue(Op0->
getType());
1242// If the two operands are negated, return 0. 1244return ConstantInt::getNullValue(Op0->
getType());
1246returnsimplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1253/// Given operands for a URem, see if we can fold the result. 1254/// If not, this returns null. 1256unsigned MaxRecurse) {
1257returnsimplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
1264/// Returns true if a shift by \c Amount always yields poison. 1266Constant *
C = dyn_cast<Constant>(Amount);
1270// X shift by undef -> poison because it may shift by the bitwidth. 1274// Shifting by the bitwidth or more is poison. This covers scalars and 1275// fixed/scalable vectors with splat constants. 1280// Try harder for fixed-length vectors: 1281// If all lanes of a vector shift are poison, the whole shift is poison. 1282if (isa<ConstantVector>(
C) || isa<ConstantDataVector>(
C)) {
1284 E = cast<FixedVectorType>(
C->getType())->getNumElements();
1294/// Given operands for an Shl, LShr or AShr, see if we can fold the result. 1295/// If not, this returns null. 1298unsigned MaxRecurse) {
1302// poison shift by X -> poison 1303if (isa<PoisonValue>(Op0))
1311// Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones 1318// Fold undefined shifts. 1322// If the operation is with the result of a select instruction, check whether 1323// operating on either branch of the select always yields the same value. 1324if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
1328// If the operation is with the result of a phi instruction, check whether 1329// operating on all incoming values of the phi always yields the same value. 1330if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
1334// If any bits in the shift amount make that value greater than or equal to 1335// the number of bits in the type, the shift is undefined. 1340// If all valid bits in the shift amount are known zero, the first operand is 1346// Check for nsw shl leading to a poison value. 1348assert(Opcode == Instruction::Shl &&
"Expected shl for nsw instruction");
1364/// Given operands for an LShr or AShr, see if we can fold the result. If not, 1365/// this returns null. 1367Value *Op1,
bool IsExact,
1370simplifyShift(Opcode, Op0, Op1,
/*IsNSW*/false, Q, MaxRecurse))
1378// undef >> X -> undef (if it's exact) 1382// The low bit cannot be shifted out of an exact shift if it is set. 1383// TODO: Generalize by counting trailing zeros (see fold for exact division). 1393/// Given operands for an Shl, see if we can fold the result. 1394/// If not, this returns null. 1398simplifyShift(Instruction::Shl, Op0, Op1, IsNSW, Q, MaxRecurse))
1403// undef << X -> undef if (if it's NSW/NUW) 1407// (X >> A) << A -> X 1413// shl nuw i8 C, %x -> C iff C has sign bit set. 1416// NOTE: could use computeKnownBits() / LazyValueInfo, 1417// but the cost-benefit analysis suggests it isn't worth it. 1419// "nuw" guarantees that only zeros are shifted out, and "nsw" guarantees 1420// that the sign-bit does not change, so the only input that does not 1421// produce poison is 0, and "0 << (bitwidth-1) --> 0". 1422if (IsNSW && IsNUW &&
1431 return ::simplifyShlInst(Op0, Op1, IsNSW, IsNUW, Q,
RecursionLimit);
1434/// Given operands for an LShr, see if we can fold the result. 1435/// If not, this returns null. 1442// (X << A) >> A -> X 1447// ((X << A) | Y) >> A -> X if effective width of Y is not larger than A. 1448// We can return X as we do in the above case since OR alters no bits in X. 1449// SimplifyDemandedBits in InstCombine can do more general optimization for 1450// bit manipulation. This pattern aims to provide opportunities for other 1451// optimizers by supporting a simple but common case in InstSimplify. 1453constAPInt *ShRAmt, *ShLAmt;
1456 *ShRAmt == *ShLAmt) {
1459if (ShRAmt->
uge(EffWidthY))
1471/// Given operands for an AShr, see if we can fold the result. 1472/// If not, this returns null. 1480// (-1 << X) a>> X --> -1 1481// We could return the original -1 constant to preserve poison elements. 1486// (X << A) >> A -> X 1491// Arithmetic shifting an all-sign-bit value is a no-op. 1504/// Commuted variants are assumed to be handled by calling this function again 1505/// with the parameters swapped. 1521if (
match(UnsignedICmp,
1523 ICmpInst::isUnsigned(UnsignedPred)) {
1524// A >=/<= B || (A - B) != 0 <--> true 1525if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1526 UnsignedPred == ICmpInst::ICMP_ULE) &&
1527 EqPred == ICmpInst::ICMP_NE && !IsAnd)
1529// A </> B && (A - B) == 0 <--> false 1530if ((UnsignedPred == ICmpInst::ICMP_ULT ||
1531 UnsignedPred == ICmpInst::ICMP_UGT) &&
1532 EqPred == ICmpInst::ICMP_EQ && IsAnd)
1535// A </> B && (A - B) != 0 <--> A </> B 1536// A </> B || (A - B) != 0 <--> (A - B) != 0 1537if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
1538 UnsignedPred == ICmpInst::ICMP_UGT))
1539return IsAnd ? UnsignedICmp : ZeroICmp;
1541// A <=/>= B && (A - B) == 0 <--> (A - B) == 0 1542// A <=/>= B || (A - B) == 0 <--> A <=/>= B 1543if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
1544 UnsignedPred == ICmpInst::ICMP_UGE))
1545return IsAnd ? ZeroICmp : UnsignedICmp;
1549// Y >= A && Y != 0 --> Y >= A iff B != 0 1550// Y < A || Y == 0 --> Y < A iff B != 0 1551if (
match(UnsignedICmp,
1553if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
1556if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
1563 ICmpInst::isUnsigned(UnsignedPred))
1565elseif (
match(UnsignedICmp,
1567 ICmpInst::isUnsigned(UnsignedPred))
1568 UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
1572// X > Y && Y == 0 --> Y == 0 iff X != 0 1573// X > Y || Y == 0 --> X > Y iff X != 0 1574if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1576return IsAnd ? ZeroICmp : UnsignedICmp;
1578// X <= Y && Y != 0 --> X <= Y iff X != 0 1579// X <= Y || Y != 0 --> Y != 0 iff X != 0 1580if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1582return IsAnd ? UnsignedICmp : ZeroICmp;
1584// The transforms below here are expected to be handled more generally with 1585// simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's 1586// foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap, 1587// these are candidates for removal. 1589// X < Y && Y != 0 --> X < Y 1590// X < Y || Y != 0 --> Y != 0 1591if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
1592return IsAnd ? UnsignedICmp : ZeroICmp;
1594// X >= Y && Y == 0 --> Y == 0 1595// X >= Y || Y == 0 --> X >= Y 1596if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
1597return IsAnd ? ZeroICmp : UnsignedICmp;
1599// X < Y && Y == 0 --> false 1600if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
1604// X >= Y || Y != 0 --> true 1605if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
1612/// Test if a pair of compares with a shared operand and 2 constants has an 1613/// empty set intersection, full set union, or if one compare is a superset of 1617// Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)). 1629// For and-of-compares, check if the intersection is empty: 1630// (icmp X, C0) && (icmp X, C1) --> empty set --> false 1631if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
1634// For or-of-compares, check if the union is full: 1635// (icmp X, C0) || (icmp X, C1) --> full set --> true 1636if (!IsAnd && Range0.unionWith(Range1).isFullSet())
1639// Is one range a superset of the other? 1640// If this is and-of-compares, take the smaller set: 1641// (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42 1642// If this is or-of-compares, take the larger set: 1643// (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4 1644if (Range0.contains(Range1))
1645return IsAnd ? Cmp1 : Cmp0;
1646if (Range1.contains(Range0))
1647return IsAnd ? Cmp0 : Cmp1;
1654// (icmp (add V, C0), C1) & (icmp V, C0) 1664auto *AddInst = cast<OverflowingBinaryOperator>(Op0->
getOperand(0));
1665if (AddInst->getOperand(1) != Op1->
getOperand(1))
1672constAPInt Delta = *C1 - *C0;
1675if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
1677if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1681if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
1683if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && IsNSW)
1689if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
1692if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
1699/// Try to simplify and/or of icmp with ctpop intrinsic. 1710// (ctpop(X) == C) || (X != 0) --> X != 0 where C > 0 1711if (!IsAnd && Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_NE)
1713// (ctpop(X) != C) && (X == 0) --> X == 0 where C > 0 1714if (IsAnd && Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_EQ)
1745// (icmp (add V, C0), C1) | (icmp V, C0) 1755auto *AddInst = cast<BinaryOperator>(Op0->
getOperand(0));
1756if (AddInst->getOperand(1) != Op1->
getOperand(1))
1763constAPInt Delta = *C1 - *C0;
1766if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
1768if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1772if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
1774if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && IsNSW)
1780if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
1783if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
1815Value *LHS0 =
LHS->getOperand(0), *LHS1 =
LHS->getOperand(1);
1816Value *RHS0 =
RHS->getOperand(0), *RHS1 =
RHS->getOperand(1);
1822if ((PredL == FCmpInst::FCMP_ORD || PredL == FCmpInst::FCMP_UNO) &&
1823 ((FCmpInst::isOrdered(PredR) && IsAnd) ||
1824 (FCmpInst::isUnordered(PredR) && !IsAnd))) {
1825// (fcmp ord X, 0) & (fcmp o** X/abs(X), Y) --> fcmp o** X/abs(X), Y 1826// (fcmp uno X, 0) & (fcmp o** X/abs(X), Y) --> false 1827// (fcmp uno X, 0) | (fcmp u** X/abs(X), Y) --> fcmp u** X/abs(X), Y 1828// (fcmp ord X, 0) | (fcmp u** X/abs(X), Y) --> true 1829if ((
match(RHS0, AbsOrSelfLHS0) ||
match(RHS1, AbsOrSelfLHS0)) &&
1831return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1837if ((PredR == FCmpInst::FCMP_ORD || PredR == FCmpInst::FCMP_UNO) &&
1838 ((FCmpInst::isOrdered(PredL) && IsAnd) ||
1839 (FCmpInst::isUnordered(PredL) && !IsAnd))) {
1840// (fcmp o** X/abs(X), Y) & (fcmp ord X, 0) --> fcmp o** X/abs(X), Y 1841// (fcmp o** X/abs(X), Y) & (fcmp uno X, 0) --> false 1842// (fcmp u** X/abs(X), Y) | (fcmp uno X, 0) --> fcmp u** X/abs(X), Y 1843// (fcmp u** X/abs(X), Y) | (fcmp ord X, 0) --> true 1844if ((
match(LHS0, AbsOrSelfRHS0) ||
match(LHS1, AbsOrSelfRHS0)) &&
1846return FCmpInst::isOrdered(PredL) == FCmpInst::isOrdered(PredR)
1855Value *Op1,
bool IsAnd) {
1856// Look through casts of the 'and' operands to find compares. 1857auto *Cast0 = dyn_cast<CastInst>(Op0);
1858auto *Cast1 = dyn_cast<CastInst>(Op1);
1859if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
1860 Cast0->getSrcTy() == Cast1->getSrcTy()) {
1861 Op0 = Cast0->getOperand(0);
1862 Op1 = Cast1->getOperand(0);
1866auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
1867auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
1872auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
1873auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
1882// If we looked through casts, we can only handle a constant simplification 1883// because we are not allowed to create a cast instruction here. 1884if (
auto *
C = dyn_cast<Constant>(V))
1893bool AllowRefinement,
1895unsigned MaxRecurse);
1899unsigned MaxRecurse) {
1900assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&
1911// and (icmp eq a, b), x implies (a==b) inside x. 1912// or (icmp ne a, b), x implies (a==b) inside x. 1913// If x simplifies to true/false, we can simplify the and/or. 1915 (Opcode == Instruction::And ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
1923// If we have and (icmp ne a, b), x and for a==b we can simplify x to false, 1924// then we can drop the icmp, as x will already be false in the case where 1925// the icmp is false. Similar for or and true. 1931// In the final case (Res == Absorber with inverted predicate), it is safe to 1932// refine poison during simplification, but not undef. For simplicity always 1933// disable undef-based folds here. 1935/* AllowRefinement */true,
1936/* DropFlags */nullptr, MaxRecurse))
1937return Simplify(Res);
1939/* AllowRefinement */true,
1940/* DropFlags */nullptr, MaxRecurse))
1941return Simplify(Res);
1946/// Given a bitwise logic op, check if the operands are add/sub with a common 1947/// source value and inverted constant (identity: C - X -> ~(X + ~C)). 1951assert(BinaryOperator::isBitwiseLogicOp(Opcode) &&
"Expected logic op");
1959// (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0 1960// (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1 1961// (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1 1963return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
1964 : ConstantInt::getAllOnesValue(Ty);
1970// Commutative patterns for and that will be tried with both operand orders. 1973unsigned MaxRecurse) {
1982// (X | ~Y) & (X | Y) --> X 1988// If we have a multiplication overflow check that is being 'and'ed with a 1989// check that one of the multipliers is not zero, we can omit the 'and', and 1990// only keep the overflow check. 1994// -A & A = A if A is a power of two or zero. 1999// This is a similar pattern used for checking if a value is a power-of-2: 2000// (A - 1) & A --> 0 (if A is a power-of-2 or 0) 2005// (x << N) & ((x << M) - 1) --> 0, where x is known to be a power of 2 and 2007constAPInt *Shift1, *Shift2;
2012 Shift1->
uge(*Shift2))
2022/// Given operands for an And, see if we can fold the result. 2023/// If not, this returns null. 2025unsigned MaxRecurse) {
2029// X & poison -> poison 2030if (isa<PoisonValue>(Op1))
2057// A mask that only clears known zeros of a shifted value is a no-op. 2062// If all bits in the inverted and shifted mask are clear: 2063// and (shl X, ShAmt), Mask --> shl X, ShAmt 2065 (~(*Mask)).lshr(*ShAmt).isZero())
2068// If all bits in the inverted and shifted mask are clear: 2069// and (lshr X, ShAmt), Mask --> lshr X, ShAmt 2071 (~(*Mask)).shl(*ShAmt).isZero())
2075// and 2^x-1, 2^C --> 0 where x <= C. 2083// Use getActiveBits() to make use of the additional power of two knowledge 2085return ConstantInt::getNullValue(Op1->
getType());
2091// Try some generic simplifications for associative operations. 2096// And distributes over Or. Try some generic simplifications based on this. 2098 Instruction::Or, Q, MaxRecurse))
2101// And distributes over Xor. Try some generic simplifications based on this. 2103 Instruction::Xor, Q, MaxRecurse))
2106if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2108// A & (A && B) -> A && B 2114// If the operation is with the result of a select instruction, check 2115// whether operating on either branch of the select always yields the same 2122// If the operation is with the result of a phi instruction, check whether 2123// operating on all incoming values of the phi always yields the same value. 2124if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2129// Assuming the effective width of Y is not larger than A, i.e. all bits 2130// from X and Y are disjoint in (X << A) | Y, 2131// if the mask of this AND op covers all bits of X or Y, while it covers 2132// no bits from the other, we can bypass this AND op. E.g., 2133// ((X << A) | Y) & Mask -> Y, 2134// if Mask = ((1 << effective_width_of(Y)) - 1) 2135// ((X << A) | Y) & Mask -> X << A, 2136// if Mask = ((1 << effective_width_of(X)) - 1) << A 2137// SimplifyDemandedBits in InstCombine can optimize the general case. 2138// This pattern aims to help other passes for a common case. 2148if (EffWidthY <= ShftCnt) {
2153// If the mask is extracting all bits from X or Y as is, we can skip 2162// ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0 2163// ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0 2173// (A ^ C) & (A ^ ~C) -> 0 2180// If Op0 is true implies Op1 is true, then Op0 is a subset of Op1. 2183// If Op0 is true implies Op1 is false, then they are not true together. 2184if (*Implied ==
false)
2188// If Op1 is true implies Op0 is true, then Op1 is a subset of Op0. 2191// If Op1 is true implies Op0 is false, then they are not true together. 2207// TODO: Many of these folds could use LogicalAnd/LogicalOr. 2209assert(
X->getType() ==
Y->getType() &&
"Expected same type for 'or' ops");
2210Type *Ty =
X->getType();
2214return ConstantInt::getAllOnesValue(Ty);
2218return ConstantInt::getAllOnesValue(Ty);
2226// (A ^ B) | (A | B) --> A | B 2227// (A ^ B) | (B | A) --> B | A 2232// ~(A ^ B) | (A | B) --> -1 2233// ~(A ^ B) | (B | A) --> -1 2236return ConstantInt::getAllOnesValue(Ty);
2238// (A & ~B) | (A ^ B) --> A ^ B 2239// (~B & A) | (A ^ B) --> A ^ B 2240// (A & ~B) | (B ^ A) --> B ^ A 2241// (~B & A) | (B ^ A) --> B ^ A 2246// (~A ^ B) | (A & B) --> ~A ^ B 2247// (B ^ ~A) | (A & B) --> B ^ ~A 2248// (~A ^ B) | (B & A) --> ~A ^ B 2249// (B ^ ~A) | (B & A) --> B ^ ~A 2254// (~A | B) | (A ^ B) --> -1 2255// (~A | B) | (B ^ A) --> -1 2256// (B | ~A) | (A ^ B) --> -1 2257// (B | ~A) | (B ^ A) --> -1 2260return ConstantInt::getAllOnesValue(Ty);
2262// (~A & B) | ~(A | B) --> ~A 2263// (~A & B) | ~(B | A) --> ~A 2264// (B & ~A) | ~(A | B) --> ~A 2265// (B & ~A) | ~(B | A) --> ~A 2271// The same is true of Logical And 2272// TODO: This could share the logic of the version above if there was a 2273// version of LogicalAnd that allowed more than just i1 types. 2279// ~(A ^ B) | (A & B) --> ~(A ^ B) 2280// ~(A ^ B) | (B & A) --> ~(A ^ B) 2287// ~(A & B) | (A ^ B) --> ~(A & B) 2288// ~(A & B) | (B ^ A) --> ~(A & B) 2297/// Given operands for an Or, see if we can fold the result. 2298/// If not, this returns null. 2300unsigned MaxRecurse) {
2304// X | poison -> poison 2305if (isa<PoisonValue>(Op1))
2310// Do not return Op1 because it may contain undef elements if it's a vector. 2327// Rotated -1 is still -1: 2328// (-1 << X) | (-1 >> (C - X)) --> -1 2329// (-1 >> X) | (-1 << (C - X)) --> -1 2330// ...with C <= bitwidth (and commuted variants). 2339C->ule(
X->getType()->getScalarSizeInBits())) {
2340return ConstantInt::getAllOnesValue(
X->getType());
2344// A funnel shift (rotate) can be decomposed into simpler shifts. See if we 2345// are mixing in another shift that is redundant with the funnel shift. 2347// (fshl X, ?, Y) | (shl X, Y) --> fshl X, ?, Y 2348// (shl X, Y) | (fshl X, ?, Y) --> fshl X, ?, Y 2358// (fshr ?, X, Y) | (lshr X, Y) --> fshr ?, X, Y 2359// (lshr X, Y) | (fshr ?, X, Y) --> fshr ?, X, Y 2379// If we have a multiplication overflow check that is being 'and'ed with a 2380// check that one of the multipliers is not zero, we can omit the 'and', and 2381// only keep the overflow check. 2387// Try some generic simplifications for associative operations. 2392// Or distributes over And. Try some generic simplifications based on this. 2394 Instruction::And, Q, MaxRecurse))
2397if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
2399// A | (A || B) -> A || B 2405// If the operation is with the result of a select instruction, check 2406// whether operating on either branch of the select always yields the same 2420// If we have: ((V + N) & C1) | (V & C2) 2421// .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 2424if (C2->
isMask() &&
// C2 == 0+1+ 2426// Add commutes, try both ways. 2430// Or commutes, try both ways. 2432// Add commutes, try both ways. 2439// If the operation is with the result of a phi instruction, check whether 2440// operating on all incoming values of the phi always yields the same value. 2441if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
2445// (A ^ C) | (A ^ ~C) -> -1, i.e. all bits set to one. 2451if (std::optional<bool> Implied =
2453// If Op0 is false implies Op1 is false, then Op1 is a subset of Op0. 2454if (*Implied ==
false)
2456// If Op0 is false implies Op1 is true, then at least one is always true. 2460if (std::optional<bool> Implied =
2462// If Op1 is false implies Op0 is false, then Op0 is a subset of Op1. 2463if (*Implied ==
false)
2465// If Op1 is false implies Op0 is true, then at least one is always true. 2481/// Given operands for a Xor, see if we can fold the result. 2482/// If not, this returns null. 2484unsigned MaxRecurse) {
2488// X ^ poison -> poison 2489if (isa<PoisonValue>(Op1))
2492// A ^ undef -> undef 2504// A ^ ~A = ~A ^ A = -1 2510// (~A & B) ^ (A | B) --> A -- There are 8 commuted variants. 2515// (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants. 2516// The 'not' op must contain a complete -1 operand (no undef elements for 2517// vector) for the transform to be safe. 2526if (
Value *R = foldAndOrNot(Op0, Op1))
2528if (
Value *R = foldAndOrNot(Op1, Op0))
2534// Try some generic simplifications for associative operations. 2539// Threading Xor over selects and phi nodes is pointless, so don't bother. 2540// Threading over the select in "A ^ select(cond, B, C)" means evaluating 2541// "A^B" and "A^C" and seeing if they are equal; but they are equal if and 2542// only if B and C are equal. If B and C are equal then (since we assume 2543// that operands have already been simplified) "select(cond, B, C)" should 2544// have been simplified to the common value of B and C already. Analysing 2545// "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly 2546// for threading over phi nodes. 2551// (xor (sub nuw C_Mask, X), C_Mask) -> X 2570/// Rummage around inside V looking for something equivalent to the comparison 2571/// "LHS Pred RHS". Return such a value if found, otherwise return null. 2572/// Helper function for analyzing max/min idioms. 2578CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
2581Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
2582if (Pred == Cmp->getPredicate() &&
LHS == CmpLHS &&
RHS == CmpRHS)
2585LHS == CmpRHS &&
RHS == CmpLHS)
2590/// Return true if the underlying object (storage) must be disjoint from 2591/// storage returned by any noalias return call. 2593// For allocas, we consider only static ones (dynamic 2594// allocas might be transformed into calls to malloc not simultaneously 2595// live with the compared-to allocation). For globals, we exclude symbols 2596// that might be resolve lazily to symbols in another dynamically-loaded 2597// library (and, thus, could be malloc'ed by the implementation). 2598if (
constAllocaInst *AI = dyn_cast<AllocaInst>(V))
2599return AI->isStaticAlloca();
2600if (
constGlobalValue *GV = dyn_cast<GlobalValue>(V))
2601return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
2602 GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
2603 !GV->isThreadLocal();
2604if (
constArgument *
A = dyn_cast<Argument>(V))
2605returnA->hasByValAttr();
2609/// Return true if V1 and V2 are each the base of some distict storage region 2610/// [V, object_size(V)] which do not overlap. Note that zero sized regions 2611/// *are* possible, and that zero sized regions do not overlap with any other. 2613// Global variables always exist, so they always exist during the lifetime 2614// of each other and all allocas. Global variables themselves usually have 2615// non-overlapping storage, but since their addresses are constants, the 2616// case involving two globals does not reach here and is instead handled in 2619// Two different allocas usually have different addresses... 2621// However, if there's an @llvm.stackrestore dynamically in between two 2622// allocas, they may have the same address. It's tempting to reduce the 2623// scope of the problem by only looking at *static* allocas here. That would 2624// cover the majority of allocas while significantly reducing the likelihood 2625// of having an @llvm.stackrestore pop up in the middle. However, it's not 2626// actually impossible for an @llvm.stackrestore to pop up in the middle of 2627// an entry block. Also, if we have a block that's not attached to a 2628// function, we can't tell if it's "static" under the current definition. 2629// Theoretically, this problem could be fixed by creating a new kind of 2630// instruction kind specifically for static allocas. Such a new instruction 2631// could be required to be at the top of the entry block, thus preventing it 2632// from being subject to a @llvm.stackrestore. Instcombine could even 2633// convert regular allocas into these special allocas. It'd be nifty. 2634// However, until then, this problem remains open. 2636// So, we'll assume that two non-empty allocas have different addresses 2638auto isByValArg = [](
constValue *V) {
2639constArgument *
A = dyn_cast<Argument>(V);
2640returnA &&
A->hasByValAttr();
2643// Byval args are backed by store which does not overlap with each other, 2644// allocas, or globals. 2646return isa<AllocaInst>(V2) || isa<GlobalVariable>(V2) || isByValArg(V2);
2648return isa<AllocaInst>(V1) || isa<GlobalVariable>(V1) || isByValArg(V1);
2650return isa<AllocaInst>(V1) &&
2651 (isa<AllocaInst>(V2) || isa<GlobalVariable>(V2));
2654// A significant optimization not implemented here is assuming that alloca 2655// addresses are not equal to incoming argument values. They don't *alias*, 2656// as we say, but that doesn't mean they aren't equal, so we take a 2657// conservative approach. 2659// This is inspired in part by C++11 5.10p1: 2660// "Two pointers of the same type compare equal if and only if they are both 2661// null, both point to the same function, or both represent the same 2664// This is pretty permissive. 2666// It's also partly due to C11 6.5.9p6: 2667// "Two pointers compare equal if and only if both are null pointers, both are 2668// pointers to the same object (including a pointer to an object and a 2669// subobject at its beginning) or function, both are pointers to one past the 2670// last element of the same array object, or one is a pointer to one past the 2671// end of one array object and the other is a pointer to the start of a 2672// different array object that happens to immediately follow the first array 2673// object in the address space.) 2675// C11's version is more restrictive, however there's no reason why an argument 2676// couldn't be a one-past-the-end value for a stack object in the caller and be 2677// equal to the beginning of a stack object in the callee. 2679// If the C and C++ standards are ever made sufficiently restrictive in this 2680// area, it may be possible to update LLVM's semantics accordingly and reinstate 2681// this optimization. 2688// We can only fold certain predicates on pointer comparisons. 2693// Equality comparisons are easy to fold. 2698// We can only handle unsigned relational comparisons because 'inbounds' on 2699// a GEP only protects against unsigned wrapping. 2704// However, we have to switch them to their signed variants to handle 2705// negative indices from the base pointer. 2710// Strip off any constant offsets so that we can reason about them. 2711// It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets 2712// here and compare base addresses like AliasAnalysis does, however there are 2713// numerous hazards. AliasAnalysis and its utilities rely on special rules 2714// governing loads and stores which don't apply to icmps. Also, AliasAnalysis 2715// doesn't need to guarantee pointer inequality when it says NoAlias. 2717// Even if an non-inbounds GEP occurs along the path we can still optimize 2718// equality comparisons concerning the result. 2720unsigned IndexSize =
DL.getIndexTypeSizeInBits(
LHS->
getType());
2721APInt LHSOffset(IndexSize, 0), RHSOffset(IndexSize, 0);
2725// If LHS and RHS are related via constant offsets to the same base 2726// value, we can replace it with an icmp which just compares the offsets. 2731// Various optimizations for (in)equality comparisons. 2733// Different non-empty allocations that exist at the same time have 2734// different addresses (if the program can tell). If the offsets are 2735// within the bounds of their allocations (and not one-past-the-end! 2736// so we can't use inbounds!), and their allocations aren't the same, 2737// the pointers are not equal. 2741 Opts.
EvalMode = ObjectSizeOpts::Mode::Min;
2743if (
auto *
I = dyn_cast<Instruction>(V))
2744returnI->getFunction();
2745if (
auto *
A = dyn_cast<Argument>(V))
2746returnA->getParent();
2752APInt Dist = LHSOffset - RHSOffset;
2759// If one side of the equality comparison must come from a noalias call 2760// (meaning a system memory allocation function), and the other side must 2761// come from a pointer that cannot overlap with dynamically-allocated 2762// memory within the lifetime of the current function (allocas, byval 2763// arguments, globals), then determine the comparison result here. 2768// Is the set of underlying objects all noalias calls? 2773// Is the set of underlying objects all things which must be disjoint from 2774// noalias calls. We assume that indexing from such disjoint storage 2775// into the heap is undefined, and thus offsets can be safely ignored. 2780if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
2781 (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
2785// Fold comparisons for non-escaping pointer even if the allocation call 2786// cannot be elided. We cannot fold malloc comparison to null. Also, the 2787// dynamic allocation call could be either of the operands. Note that 2788// the other operand can not be based on the alloc - if it were, then 2789// the cmp itself would be a capture. 2796// FIXME: This is incorrect, see PR54002. While we can assume that the 2797// allocation is at an address that makes the comparison false, this 2798// requires that *all* comparisons to that address be false, which 2799// InstSimplify cannot guarantee. 2801bool Captured =
false;
2804if (
auto *ICmp = dyn_cast<ICmpInst>(U->getUser())) {
2805// Comparison against value stored in global variable. Given the 2806// pointer does not escape, its value cannot be guessed and stored 2807// separately in a global variable. 2808unsigned OtherIdx = 1 - U->getOperandNo();
2809auto *LI = dyn_cast<LoadInst>(ICmp->getOperand(OtherIdx));
2810if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
2818 CustomCaptureTracker Tracker;
2820if (!Tracker.Captured)
2830/// Fold an icmp when its operands have i1 scalar type. 2838// A boolean compared to true/false can be reduced in 14 out of the 20 2839// (10 predicates * 2 constants) possible combinations. The other 2840// 6 cases require a 'not' of the LHS. 2904case ICmpInst::ICMP_UGE:
2908case ICmpInst::ICMP_SGE:
2909 /// For signed comparison, the values for an i1 are 0 and -1 2910 /// respectively. This maps into a truth table of: 2911 /// LHS | RHS | LHS >=s RHS | LHS implies RHS 2912 /// 0 | 0 | 1 (0 >= 0) | 1 2913 /// 0 | 1 | 1 (0 >= -1) | 1 2914 /// 1 | 0 | 0 (-1 >= 0) | 0 2915 /// 1 | 1 | 1 (-1 >= -1) | 1 2919case ICmpInst::ICMP_ULE:
2923case ICmpInst::ICMP_SLE:
2924 /// SLE follows the same logic as SGE with the LHS and RHS swapped. 2933/// Try hard to fold icmp with zero RHS because this is a common case. 2943case ICmpInst::ICMP_ULT:
2945case ICmpInst::ICMP_UGE:
2947case ICmpInst::ICMP_EQ:
2948case ICmpInst::ICMP_ULE:
2952case ICmpInst::ICMP_NE:
2953case ICmpInst::ICMP_UGT:
2957case ICmpInst::ICMP_SLT: {
2965case ICmpInst::ICMP_SLE: {
2973case ICmpInst::ICMP_SGE: {
2981case ICmpInst::ICMP_SGT: {
3003// Sign-bit checks can be optimized to true/false after unsigned 3004// floating-point casts: 3005// icmp slt (bitcast (uitofp X)), 0 --> false 3006// icmp sgt (bitcast (uitofp X)), -1 --> true 3013// Rule out tautological comparisons (eg., ult 0 or uge 0). 3029// (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC) 3030// (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC) 3034 *MulC != 0 &&
C->urem(*MulC) != 0) ||
3036 *MulC != 0 &&
C->srem(*MulC) != 0)))
3037return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
3044/// Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq). 3047if (!Res.
insert(V).second)
3050// Can be increased if useful. 3054auto *
I = dyn_cast<Instruction>(V);
3067switch (
I->getOpcode()) {
3068case Instruction::And:
3072case Instruction::URem:
3073case Instruction::UDiv:
3074case Instruction::LShr:
3077case Instruction::Call:
3089if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_ULT)
3092// We have LHS uge GreaterValues and LowerValues uge RHS. If any of the 3093// GreaterValues and LowerValues are the same, it follows that LHS uge RHS. 3098for (
Value *GV : GreaterValues)
3101 Pred == ICmpInst::ICMP_UGE);
3107unsigned MaxRecurse) {
3111// icmp pred (or X, Y), X 3113if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
3123// icmp pred (urem X, Y), Y 3128case ICmpInst::ICMP_SGT:
3129case ICmpInst::ICMP_SGE: {
3135case ICmpInst::ICMP_EQ:
3136case ICmpInst::ICMP_UGT:
3137case ICmpInst::ICMP_UGE:
3139case ICmpInst::ICMP_SLT:
3140case ICmpInst::ICMP_SLE: {
3146case ICmpInst::ICMP_NE:
3147case ICmpInst::ICMP_ULT:
3148case ICmpInst::ICMP_ULE:
3154// x >>u C <u x --> true for C != 0. 3155// x >>u C != x --> true for C != 0. 3156// x >>u C >=u x --> false for C != 0. 3157// x >>u C == x --> false for C != 0. 3158// x udiv C <u x --> true for C != 1. 3159// x udiv C != x --> true for C != 1. 3160// x udiv C >=u x --> false for C != 1. 3161// x udiv C == x --> false for C != 1. 3162// TODO: allow non-constant shift amount/divisor 3170case ICmpInst::ICMP_EQ:
3171case ICmpInst::ICMP_UGE:
3172case ICmpInst::ICMP_UGT:
3174case ICmpInst::ICMP_NE:
3175case ICmpInst::ICMP_ULT:
3176case ICmpInst::ICMP_ULE:
3182// (x*C1)/C2 <= x for C1 <= C2. 3183// This holds even if the multiplication overflows: Assume that x != 0 and 3184// arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and 3185// thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x. 3187// Additionally, either the multiplication and division might be represented 3189// (x*C1)>>C2 <= x for C1 < 2**C2. 3190// (x<<C1)/C2 <= x for 2**C1 < C2. 3198if (Pred == ICmpInst::ICMP_UGT)
3200if (Pred == ICmpInst::ICMP_ULE)
3204// (sub C, X) == X, C is odd --> false 3205// (sub C, X) != X, C is odd --> true 3213// If only one of the icmp's operands has NSW flags, try to prove that: 3215// icmp slt (x + C1), (x +nsw C2) 3221// which is true if x + C2 has the NSW flags set and: 3222// *) C1 < C2 && C1 >= 0, or 3223// *) C2 < C1 && C1 <= 0. 3227// TODO: only support icmp slt for now. 3231// Canonicalize nsw add as RHS. 3247/// TODO: A large part of this logic is duplicated in InstCombine's 3248/// foldICmpBinOp(). We should be able to share that and avoid the code 3252unsigned MaxRecurse) {
3255if (MaxRecurse && (LBO || RBO)) {
3256// Analyze the case when either LHS or RHS is an add instruction. 3257Value *
A =
nullptr, *
B =
nullptr, *
C =
nullptr, *
D =
nullptr;
3258// LHS = A + B (or A and B are null); RHS = C + D (or C and D are null). 3259bool NoLHSWrapProblem =
false, NoRHSWrapProblem =
false;
3260if (LBO && LBO->
getOpcode() == Instruction::Add) {
3270if (RBO && RBO->
getOpcode() == Instruction::Add) {
3281// icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow. 3282if ((
A ==
RHS ||
B ==
RHS) && NoLHSWrapProblem)
3288// icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow. 3289if ((
C ==
LHS ||
D ==
LHS) && NoRHSWrapProblem)
3292C ==
LHS ?
D :
C, Q, MaxRecurse - 1))
3295// icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow. 3296bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
3298if (
A &&
C && (
A ==
C ||
A ==
D ||
B ==
C ||
B ==
D) && CanSimplify) {
3299// Determine Y and Z in the form icmp (X+Y), (X+Z). 3302// C + B == C + D -> B == D 3306// D + B == C + D -> B == C 3310// A + C == C + D -> A == D 3315// A + D == C + D -> A == C 3330 ICmpInst::getSwappedPredicate(Pred), RBO,
LHS, Q, MaxRecurse))
3333// 0 - (zext X) pred C 3337if (
C->isStrictlyPositive()) {
3338if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
3340if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
3343if (
C->isNonNegative()) {
3344if (Pred == ICmpInst::ICMP_SLE)
3346if (Pred == ICmpInst::ICMP_SGT)
3352// If C2 is a power-of-2 and C is not: 3353// (C2 << X) == C --> false 3354// (C2 << X) != C --> true 3358// C2 << X can equal zero in some circumstances. 3359// This simplification might be unsafe if C is zero. 3361// We know it is safe if: 3362// - The shift is nsw. We can't shift out the one bit. 3363// - The shift is nuw. We can't shift out the one bit. 3369if (Pred == ICmpInst::ICMP_EQ)
3371if (Pred == ICmpInst::ICMP_NE)
3376// If C is a power-of-2: 3377// (C << X) >u 0x8000 --> false 3378// (C << X) <=u 0x8000 --> true 3380if (Pred == ICmpInst::ICMP_UGT)
3382if (Pred == ICmpInst::ICMP_ULE)
3393case Instruction::Shl: {
3396if (!NUW || (ICmpInst::isSigned(Pred) && !NSW) ||
3404// If C1 & C2 == C1, A = X and/or C1, B = X and/or C2: 3405// icmp ule A, B -> true 3406// icmp ugt A, B -> false 3407// icmp sle A, B -> true (C1 and C2 are the same sign) 3408// icmp sgt A, B -> false (C1 and C2 are the same sign) 3409case Instruction::And:
3410case Instruction::Or: {
3417 Pred = ICmpInst::getSwappedPredicate(Pred);
3420if (Pred == ICmpInst::ICMP_ULE)
3422if (Pred == ICmpInst::ICMP_UGT)
3425if (Pred == ICmpInst::ICMP_SLE)
3427if (Pred == ICmpInst::ICMP_SGT)
3441case Instruction::UDiv:
3442case Instruction::LShr:
3443if (ICmpInst::isSigned(Pred) || !Q.
IIQ.
isExact(LBO) ||
3450case Instruction::SDiv:
3458case Instruction::AShr:
3465case Instruction::Shl: {
3470if (!NSW && ICmpInst::isSigned(Pred))
3482/// simplify integer comparisons where at least one operand of the compare 3483/// matches an integer min/max idiom. 3486unsigned MaxRecurse) {
3492// Signed variants on "max(a,b)>=a -> true". 3497// We analyze this as smax(A, B) pred A. 3504// We analyze this as smax(A, B) swapped-pred A. 3511// We analyze this as smax(-A, -B) swapped-pred -A. 3512// Note that we do not need to actually form -A or -B thanks to EqP. 3519// We analyze this as smax(-A, -B) pred -A. 3520// Note that we do not need to actually form -A or -B thanks to EqP. 3524// Cases correspond to "max(A, B) p A". 3530// Equivalent to "A EqP B". This may be the same as the condition tested 3531// in the max/min; if so, we can just return that. 3536// Otherwise, see if "A EqP B" simplifies. 3544// Equivalent to "A InvEqP B". This may be the same as the condition 3545// tested in the max/min; if so, we can just return that. 3550// Otherwise, see if "A InvEqP B" simplifies. 3565// Unsigned variants on "max(a,b)>=a -> true". 3571// We analyze this as umax(A, B) pred A. 3578// We analyze this as umax(A, B) swapped-pred A. 3585// We analyze this as umax(-A, -B) swapped-pred -A. 3586// Note that we do not need to actually form -A or -B thanks to EqP. 3593// We analyze this as umax(-A, -B) pred -A. 3594// Note that we do not need to actually form -A or -B thanks to EqP. 3598// Cases correspond to "max(A, B) p A". 3604// Equivalent to "A EqP B". This may be the same as the condition tested 3605// in the max/min; if so, we can just return that. 3610// Otherwise, see if "A EqP B" simplifies. 3618// Equivalent to "A InvEqP B". This may be the same as the condition 3619// tested in the max/min; if so, we can just return that. 3624// Otherwise, see if "A InvEqP B" simplifies. 3637// Comparing 1 each of min/max with a common operand? 3638// Canonicalize min operand to RHS. 3642 Pred = ICmpInst::getSwappedPredicate(Pred);
3649// smax(A, B) >=s smin(A, D) --> true 3652// smax(A, B) <s smin(A, D) --> false 3658// umax(A, B) >=u umin(A, D) --> true 3661// umax(A, B) <u umin(A, D) --> false 3672// Gracefully handle instructions that have not been inserted yet. 3681CallInst *Assume = cast<CallInst>(AssumeVH);
3694auto *
II = dyn_cast<IntrinsicInst>(
LHS);
3698switch (
II->getIntrinsicID()) {
3699case Intrinsic::uadd_sat:
3700// uadd.sat(X, Y) uge X + Y 3703if (Pred == ICmpInst::ICMP_UGE)
3705if (Pred == ICmpInst::ICMP_ULT)
3709case Intrinsic::usub_sat:
3710// usub.sat(X, Y) ule X - Y 3713if (Pred == ICmpInst::ICMP_ULE)
3715if (Pred == ICmpInst::ICMP_UGT)
3724/// Helper method to get range from metadata or attribute. 3731if (
constArgument *
A = dyn_cast<Argument>(V))
3733elseif (
constCallBase *CB = dyn_cast<CallBase>(V))
3734return CB->getRange();
3739/// Given operands for an ICmpInst, see if we can fold the result. 3740/// If not, this returns null. 3749// If we have a constant, make sure it is on the RHS. 3753assert(!isa<UndefValue>(
LHS) &&
"Unexpected icmp undef,%X");
3757// icmp poison, X -> poison 3758if (isa<PoisonValue>(
RHS))
3761// For EQ and NE, we can always pick a value for the undef to make the 3762// predicate pass or fail, so we can return undef. 3763// Matches behavior in llvm::ConstantFoldCompareInstruction. 3767// icmp X, X -> true/false 3768// icmp X, undef -> true/false because undef could be X. 3775// TODO: Sink/common this with other potentially expensive calls that use 3776// ValueTracking? See comment below for isKnownNonEqual(). 3783// If both operands have range metadata, use the metadata 3784// to simplify the comparison. 3787if (LhsCr->icmp(Pred, *RhsCr))
3794// Compare of cast, for example (zext X) != 0 -> X != 0 3795if (isa<CastInst>(
LHS) && (isa<Constant>(
RHS) || isa<CastInst>(
RHS))) {
3801// Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input 3802// if the integer type is the same size as the pointer type. 3803if (MaxRecurse && isa<PtrToIntInst>(LI) &&
3806// Transfer the cast to the constant. 3812if (RI->getOperand(0)->getType() == SrcTy)
3813// Compare without the cast. 3820if (isa<ZExtInst>(
LHS)) {
3821// Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the 3824if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3825// Compare X and Y. Note that signed predicates become unsigned. 3828 RI->getOperand(0), Q, MaxRecurse - 1))
3831// Fold (zext X) ule (sext X), (zext X) sge (sext X) to true. 3833if (
SrcOp == RI->getOperand(0)) {
3834if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
3836if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
3840// Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended 3841// too. If not, then try to deduce the result of the comparison. 3846// Compute the constant that would happen if we truncated to SrcTy then 3847// reextended to DstTy. 3850assert(Trunc &&
"Constant-fold of ImmConstant should not fail");
3853assert(RExt &&
"Constant-fold of ImmConstant should not fail");
3856assert(AnyEq &&
"Constant-fold of ImmConstant should not fail");
3858// If the re-extended constant didn't change any of the elements then 3859// this is effectively also a case of comparing two zero-extended 3863SrcOp, Trunc, Q, MaxRecurse - 1))
3866// Otherwise the upper bits of LHS are zero while RHS has a non-zero bit 3867// there. Use this to work out the result of the comparison. 3873case ICmpInst::ICMP_EQ:
3874case ICmpInst::ICMP_UGT:
3875case ICmpInst::ICMP_UGE:
3878case ICmpInst::ICMP_NE:
3879case ICmpInst::ICMP_ULT:
3880case ICmpInst::ICMP_ULE:
3883// LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS 3884// is non-negative then LHS <s RHS. 3885case ICmpInst::ICMP_SGT:
3886case ICmpInst::ICMP_SGE:
3890case ICmpInst::ICMP_SLT:
3891case ICmpInst::ICMP_SLE:
3900if (isa<SExtInst>(
LHS)) {
3901// Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the 3904if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
3905// Compare X and Y. Note that the predicate does not change. 3910// Fold (sext X) uge (zext X), (sext X) sle (zext X) to true. 3912if (
SrcOp == RI->getOperand(0)) {
3913if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
3915if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
3919// Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended 3920// too. If not, then try to deduce the result of the comparison. 3924// Compute the constant that would happen if we truncated to SrcTy then 3925// reextended to DstTy. 3928assert(Trunc &&
"Constant-fold of ImmConstant should not fail");
3931assert(RExt &&
"Constant-fold of ImmConstant should not fail");
3934assert(AnyEq &&
"Constant-fold of ImmConstant should not fail");
3936// If the re-extended constant didn't change then this is effectively 3937// also a case of comparing two sign-extended values. 3943// Otherwise the upper bits of LHS are all equal, while RHS has varying 3944// bits there. Use this to work out the result of the comparison. 3949case ICmpInst::ICMP_EQ:
3951case ICmpInst::ICMP_NE:
3954// If RHS is non-negative then LHS <s RHS. If RHS is negative then 3956case ICmpInst::ICMP_SGT:
3957case ICmpInst::ICMP_SGE:
3961case ICmpInst::ICMP_SLT:
3962case ICmpInst::ICMP_SLE:
3967// If LHS is non-negative then LHS <u RHS. If LHS is negative then 3969case ICmpInst::ICMP_UGT:
3970case ICmpInst::ICMP_UGE:
3971// Comparison is true iff the LHS <s 0. 3978case ICmpInst::ICMP_ULT:
3979case ICmpInst::ICMP_ULE:
3980// Comparison is true iff the LHS >=s 0. 3993// icmp eq|ne X, Y -> false|true if X != Y 3994// This is potentially expensive, and we have already computedKnownBits for 3995// compares with 0 above here, so only try this for a non-zero compare. 4010 ICmpInst::getSwappedPredicate(Pred),
RHS,
LHS))
4016 ICmpInst::getSwappedPredicate(Pred),
RHS,
LHS))
4022if (std::optional<bool> Res =
4026// Simplify comparisons of related pointers using a powerful, recursive 4027// GEP-walk when we have target data available.. 4031if (
auto *CLHS = dyn_cast<PtrToIntOperator>(
LHS))
4032if (
auto *CRHS = dyn_cast<PtrToIntOperator>(
RHS))
4033if (CLHS->getPointerOperandType() == CRHS->getPointerOperandType() &&
4037 CRHS->getPointerOperand(), Q))
4040// If the comparison is with the result of a select instruction, check whether 4041// comparing with either branch of the select always yields the same value. 4042if (isa<SelectInst>(
LHS) || isa<SelectInst>(
RHS))
4046// If the comparison is with the result of a phi instruction, check whether 4047// doing the compare with each incoming phi value yields a common result. 4048if (isa<PHINode>(
LHS) || isa<PHINode>(
RHS))
4060/// Given operands for an FCmpInst, see if we can fold the result. 4061/// If not, this returns null. 4064unsigned MaxRecurse) {
4072// If we have a constant, make sure it is on the RHS. 4077// Fold trivial predicates. 4079if (Pred == FCmpInst::FCMP_FALSE)
4081if (Pred == FCmpInst::FCMP_TRUE)
4084// fcmp pred x, poison and fcmp pred poison, x 4086if (isa<PoisonValue>(
LHS) || isa<PoisonValue>(
RHS))
4089// fcmp pred x, undef and fcmp pred undef, x 4090// fold to true if unordered, false if ordered 4092// Choosing NaN for the undef will always make unordered comparison succeed 4093// and ordered comparison fail. 4097// fcmp x,x -> true/false. Not all compares are foldable. 4105// Fold (un)ordered comparison if we can determine there are no NaNs. 4107// This catches the 2 variable input case, constants are handled below as a 4108// class-like compare. 4109if (Pred == FCmpInst::FCMP_ORD || Pred == FCmpInst::FCMP_UNO) {
4117return ConstantInt::get(
RetTy, Pred == FCmpInst::FCMP_ORD);
4125 std::optional<KnownFPClass> FullKnownClassLHS;
4127// Lazily compute the possible classes for LHS. Avoid computing it twice if 4129auto computeLHSClass = [=, &FullKnownClassLHS](
FPClassTest InterestedFlags =
4131if (FullKnownClassLHS)
4132return *FullKnownClassLHS;
4137// Fold out compares that express a class test. 4139// FIXME: Should be able to perform folds without context 4140// instruction. Always pass in the context function? 4145 FullKnownClassLHS = computeLHSClass();
4146if ((FullKnownClassLHS->KnownFPClasses & ClassTest) ==
fcNone)
4148if ((FullKnownClassLHS->KnownFPClasses & ~ClassTest) ==
fcNone)
4153// Handle fcmp with constant RHS. 4155// TODO: If we always required a context function, we wouldn't need to 4156// special case nans. 4160// TODO: Need version fcmpToClassTest which returns implied class when the 4161// compare isn't a complete class test. e.g. > 1.0 implies fcPositive, but 4162// isn't implementable as a class call. 4163if (
C->isNegative() && !
C->isNegZero()) {
4166// TODO: We can catch more cases by using a range check rather than 4167// relying on CannotBeOrderedLessThanZero. 4169case FCmpInst::FCMP_UGE:
4170case FCmpInst::FCMP_UGT:
4171case FCmpInst::FCMP_UNE: {
4174// (X >= 0) implies (X > C) when (C < 0) 4179case FCmpInst::FCMP_OEQ:
4180case FCmpInst::FCMP_OLE:
4181case FCmpInst::FCMP_OLT: {
4184// (X >= 0) implies !(X < C) when (C < 0) 4193// Check comparison of [minnum/maxnum with constant] with other constant. 4200 cast<IntrinsicInst>(
LHS)->getIntrinsicID() == Intrinsic::maxnum;
4201// The ordered relationship and minnum/maxnum guarantee that we do not 4202// have NaN constants, so ordered/unordered preds are handled the same. 4204case FCmpInst::FCMP_OEQ:
4205case FCmpInst::FCMP_UEQ:
4206// minnum(X, LesserC) == C --> false 4207// maxnum(X, GreaterC) == C --> false 4209case FCmpInst::FCMP_ONE:
4210case FCmpInst::FCMP_UNE:
4211// minnum(X, LesserC) != C --> true 4212// maxnum(X, GreaterC) != C --> true 4214case FCmpInst::FCMP_OGE:
4215case FCmpInst::FCMP_UGE:
4216case FCmpInst::FCMP_OGT:
4217case FCmpInst::FCMP_UGT:
4218// minnum(X, LesserC) >= C --> false 4219// minnum(X, LesserC) > C --> false 4220// maxnum(X, GreaterC) >= C --> true 4221// maxnum(X, GreaterC) > C --> true 4222return ConstantInt::get(
RetTy, IsMaxNum);
4223case FCmpInst::FCMP_OLE:
4224case FCmpInst::FCMP_ULE:
4225case FCmpInst::FCMP_OLT:
4226case FCmpInst::FCMP_ULT:
4227// minnum(X, LesserC) <= C --> true 4228// minnum(X, LesserC) < C --> true 4229// maxnum(X, GreaterC) <= C --> false 4230// maxnum(X, GreaterC) < C --> false 4231return ConstantInt::get(
RetTy, !IsMaxNum);
4233// TRUE/FALSE/ORD/UNO should be handled before this. 4239// TODO: Could fold this with above if there were a matcher which returned all 4240// classes in a non-splat vector. 4243case FCmpInst::FCMP_OGE:
4244case FCmpInst::FCMP_ULT: {
4251// Positive or zero X >= 0.0 --> true 4252// Positive or zero X < 0.0 --> false 4258case FCmpInst::FCMP_UGE:
4259case FCmpInst::FCMP_OLT: {
4263// Positive or zero or nan X >= 0.0 --> true 4264// Positive or zero or nan X < 0.0 --> false 4274// If the comparison is with the result of a select instruction, check whether 4275// comparing with either branch of the select always yields the same value. 4276if (isa<SelectInst>(
LHS) || isa<SelectInst>(
RHS))
4280// If the comparison is with the result of a phi instruction, check whether 4281// doing the compare with each incoming phi value yields a common result. 4282if (isa<PHINode>(
LHS) || isa<PHINode>(
RHS))
4295ArrayRef<std::pair<Value *, Value *>> Ops,
4297bool AllowRefinement,
4299unsigned MaxRecurse) {
4301"If AllowRefinement=false then CanUseUndef=false");
4302for (
constauto &OpAndRepOp : Ops) {
4303// We cannot replace a constant, and shouldn't even try. 4304if (isa<Constant>(OpAndRepOp.first))
4307// Trivial replacement. 4308if (V == OpAndRepOp.first)
4309return OpAndRepOp.second;
4315auto *
I = dyn_cast<Instruction>(V);
4319// The arguments of a phi node might refer to a value from a previous 4324// Don't fold away llvm.is.constant checks based on assumptions. 4325if (
match(
I, m_Intrinsic<Intrinsic::is_constant>()))
4328// Don't simplify freeze. 4329if (isa<FreezeInst>(
I))
4332for (
constauto &OpAndRepOp : Ops) {
4333// For vector types, the simplification must hold per-lane, so forbid 4334// potentially cross-lane operations like shufflevector. 4335if (OpAndRepOp.first->getType()->isVectorTy() &&
4340// Replace Op with RepOp in instruction operands. 4342bool AnyReplaced =
false;
4343for (
Value *InstOp :
I->operands()) {
4345 InstOp, Ops, Q, AllowRefinement, DropFlags, MaxRecurse)) {
4347 AnyReplaced = InstOp != NewInstOp;
4352// Bail out if any operand is undef and SimplifyQuery disables undef 4353// simplification. Constant folding currently doesn't respect this option. 4361if (!AllowRefinement) {
4362// General InstSimplify functions may refine the result, e.g. by returning 4363// a constant for a potentially poison value. To avoid this, implement only 4364// a few non-refining but profitable transforms here. 4366if (
auto *BO = dyn_cast<BinaryOperator>(
I)) {
4367unsigned Opcode = BO->getOpcode();
4368// id op x -> x, x op id -> x 4369// Exclude floats, because x op id may produce a different NaN value. 4370if (!BO->getType()->isFPOrFPVectorTy()) {
4378// x & x -> x, x | x -> x 4379if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
4380 NewOps[0] == NewOps[1]) {
4381// or disjoint x, x results in poison. 4382if (
auto *PDI = dyn_cast<PossiblyDisjointInst>(BO)) {
4383if (PDI->isDisjoint()) {
4392// x - x -> 0, x ^ x -> 0. This is non-refining, because x is non-poison 4393// by assumption and this case never wraps, so nowrap flags can be 4395if ((Opcode == Instruction::Sub || Opcode == Instruction::Xor) &&
4396 NewOps[0] == NewOps[1] &&
4397any_of(Ops, [=](
constauto &Rep) {
return NewOps[0] == Rep.second; }))
4400// If we are substituting an absorber constant into a binop and extra 4401// poison can't leak if we remove the select -- because both operands of 4402// the binop are based on the same value -- then it may be safe to replace 4403// the value with the absorber constant. Examples: 4404// (Op == 0) ? 0 : (Op & -Op) --> Op & -Op 4405// (Op == 0) ? 0 : (Op * (binop Op, C)) --> Op * (binop Op, C) 4406// (Op == -1) ? -1 : (Op | (binop C, Op) --> Op | (binop C, Op) 4408if ((NewOps[0] == Absorber || NewOps[1] == Absorber) &&
4410 [=](
constauto &Rep) {
returnimpliesPoison(BO, Rep.first); }))
4414if (isa<GetElementPtrInst>(
I)) {
4415// getelementptr x, 0 -> x. 4416// This never returns poison, even if inbounds is set. 4421// The simplification queries below may return the original value. Consider: 4422// %div = udiv i32 %arg, %arg2 4423// %mul = mul nsw i32 %div, %arg2 4424// %cmp = icmp eq i32 %mul, %arg 4425// %sel = select i1 %cmp, i32 %div, i32 undef 4426// Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which 4427// simplifies back to %arg. This can only happen because %mul does not 4428// dominate %div. To ensure a consistent return value contract, we make sure 4429// that this case returns nullptr as well. 4430auto PreventSelfSimplify = [V](
Value *Simplified) {
4431return Simplified != V ? Simplified :
nullptr;
4434return PreventSelfSimplify(
4438// If all operands are constant after substituting Op for RepOp then we can 4439// constant fold the instruction. 4441for (
Value *NewOp : NewOps) {
4442if (
Constant *ConstOp = dyn_cast<Constant>(NewOp))
4449// %cmp = icmp eq i32 %x, 2147483647 4450// %add = add nsw i32 %x, 1 4451// %sel = select i1 %cmp, i32 -2147483648, i32 %add 4453// We can't replace %sel with %add unless we strip away the flags (which 4454// will be done in InstCombine). 4455// TODO: This may be unsound, because it only catches some forms of 4457if (!AllowRefinement) {
4459// abs cannot create poison if the value is known to never be int_min. 4460if (
auto *
II = dyn_cast<IntrinsicInst>(
I);
4461II &&
II->getIntrinsicID() == Intrinsic::abs) {
4462if (!ConstOps[0]->isNotMinSignedValue())
4468/*AllowNonDeterministic=*/false);
4469if (DropFlags && Res &&
I->hasPoisonGeneratingAnnotations())
4475/*AllowNonDeterministic=*/false);
4480bool AllowRefinement,
4482unsigned MaxRecurse) {
4484 DropFlags, MaxRecurse);
4489bool AllowRefinement,
4491// If refinement is disabled, also disable undef simplifications (which are 4492// always refinements) in SimplifyQuery. 4493if (!AllowRefinement)
4496 return ::simplifyWithOpReplaced(V,
Op, RepOp, Q, AllowRefinement, DropFlags,
4500/// Try to simplify a select instruction when its condition operand is an 4501/// integer comparison where one operand of the compare is a constant. 4503constAPInt *
Y,
bool TrueWhenUnset) {
4506// (X & Y) == 0 ? X & ~Y : X --> X 4507// (X & Y) != 0 ? X & ~Y : X --> X & ~Y 4510return TrueWhenUnset ? FalseVal : TrueVal;
4512// (X & Y) == 0 ? X : X & ~Y --> X & ~Y 4513// (X & Y) != 0 ? X : X & ~Y --> X 4516return TrueWhenUnset ? FalseVal : TrueVal;
4518if (
Y->isPowerOf2()) {
4519// (X & Y) == 0 ? X | Y : X --> X | Y 4520// (X & Y) != 0 ? X | Y : X --> X 4523// We can't return the or if it has the disjoint flag. 4524if (TrueWhenUnset && cast<PossiblyDisjointInst>(TrueVal)->isDisjoint())
4526return TrueWhenUnset ? TrueVal : FalseVal;
4529// (X & Y) == 0 ? X : X | Y --> X 4530// (X & Y) != 0 ? X : X | Y --> X | Y 4533// We can't return the or if it has the disjoint flag. 4534if (!TrueWhenUnset && cast<PossiblyDisjointInst>(FalseVal)->isDisjoint())
4536return TrueWhenUnset ? TrueVal : FalseVal;
4546// Canonicalize common cmp+sel operand as CmpLHS. 4547if (CmpRHS == TVal || CmpRHS == FVal) {
4549 Pred = ICmpInst::getSwappedPredicate(Pred);
4552// Canonicalize common cmp+sel operand as TVal. 4553if (CmpLHS == FVal) {
4555 Pred = ICmpInst::getInversePredicate(Pred);
4558// A vector select may be shuffling together elements that are equivalent 4559// based on the max/min/select relationship. 4560Value *
X = CmpLHS, *
Y = CmpRHS;
4561bool PeekedThroughSelectShuffle =
false;
4562auto *Shuf = dyn_cast<ShuffleVectorInst>(FVal);
4563if (Shuf && Shuf->isSelect()) {
4564if (Shuf->getOperand(0) ==
Y)
4565 FVal = Shuf->getOperand(1);
4566elseif (Shuf->getOperand(1) ==
Y)
4567 FVal = Shuf->getOperand(0);
4570 PeekedThroughSelectShuffle =
true;
4573// (X pred Y) ? X : max/min(X, Y) 4574auto *MMI = dyn_cast<MinMaxIntrinsic>(FVal);
4575if (!MMI || TVal !=
X ||
4579// (X > Y) ? X : max(X, Y) --> max(X, Y) 4580// (X >= Y) ? X : max(X, Y) --> max(X, Y) 4581// (X < Y) ? X : min(X, Y) --> min(X, Y) 4582// (X <= Y) ? X : min(X, Y) --> min(X, Y) 4584// The equivalence allows a vector select (shuffle) of max/min and Y. Ex: 4585// (X > Y) ? X : (Z ? max(X, Y) : Y) 4586// If Z is true, this reduces as above, and if Z is false: 4587// (X > Y) ? X : Y --> max(X, Y) 4592// Other transforms are not valid with a shuffle. 4593if (PeekedThroughSelectShuffle)
4596// (X == Y) ? X : max/min(X, Y) --> max/min(X, Y) 4600// (X != Y) ? X : max/min(X, Y) --> X 4604// (X < Y) ? X : max(X, Y) --> X 4605// (X <= Y) ? X : max(X, Y) --> X 4606// (X > Y) ? X : min(X, Y) --> X 4607// (X >= Y) ? X : min(X, Y) --> X 4615/// An alternative way to test if a bit is set or not uses sgt/slt instead of 4622 Res->Pred == ICmpInst::ICMP_EQ);
4627/// Try to simplify a select instruction when its condition operand is an 4628/// integer equality or floating-point equivalence comparison. 4630ArrayRef<std::pair<Value *, Value *>> Replacements,
Value *TrueVal,
4632Value *SimplifiedFalseVal =
4634/* AllowRefinement */false,
4635/* DropFlags */nullptr, MaxRecurse);
4636if (!SimplifiedFalseVal)
4637 SimplifiedFalseVal = FalseVal;
4639Value *SimplifiedTrueVal =
4641/* AllowRefinement */true,
4642/* DropFlags */nullptr, MaxRecurse);
4643if (!SimplifiedTrueVal)
4644 SimplifiedTrueVal = TrueVal;
4646if (SimplifiedFalseVal == SimplifiedTrueVal)
4652/// Try to simplify a select instruction when its condition operand is an 4653/// integer comparison. 4657unsigned MaxRecurse) {
4659Value *CmpLHS, *CmpRHS;
4666// Canonicalize ne to eq predicate. 4667if (Pred == ICmpInst::ICMP_NE) {
4668 Pred = ICmpInst::ICMP_EQ;
4672// Check for integer min/max with a limit constant: 4673// X > MIN_INT ? X : MIN_INT --> X 4674// X < MAX_INT ? X : MAX_INT --> X 4675if (TrueVal->getType()->isIntOrIntVectorTy()) {
4683X->getType()->getScalarSizeInBits());
4689if (Pred == ICmpInst::ICMP_EQ &&
match(CmpRHS,
m_Zero())) {
4694/*TrueWhenUnset=*/true))
4697// Test for a bogus zero-shift-guard-op around funnel-shift or rotate. 4701// (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X 4702// (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X 4703if (
match(TrueVal, isFsh) && FalseVal ==
X && CmpLHS == ShAmt)
4706// Test for a zero-shift-guard-op around rotates. These are used to 4707// avoid UB from oversized shifts in raw IR rotate patterns, but the 4708// intrinsics do not have that problem. 4709// We do not allow this transform for the general funnel shift case because 4710// that would not preserve the poison safety of the original code. 4714// (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt) 4715// (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt) 4716if (
match(FalseVal, isRotate) && TrueVal ==
X && CmpLHS == ShAmt &&
4717 Pred == ICmpInst::ICMP_EQ)
4720// X == 0 ? abs(X) : -abs(X) --> -abs(X) 4721// X == 0 ? -abs(X) : abs(X) --> abs(X) 4731// Check for other compares that behave like bit test. 4736// If we have a scalar equality comparison, then we know the value in one of 4737// the arms of the select. See if substituting this value into the arm and 4738// simplifying the result yields the same value as the other arm. 4739if (Pred == ICmpInst::ICMP_EQ) {
4741 FalseVal, Q, MaxRecurse))
4744 FalseVal, Q, MaxRecurse))
4749// select((X | Y) == 0 ? X : 0) --> 0 (commuted 2 ways) 4752// (X | Y) == 0 implies X == 0 and Y == 0. 4754 {{
X, CmpRHS}, {
Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4758// select((X & Y) == -1 ? X : -1) --> -1 (commuted 2 ways) 4761// (X & Y) == -1 implies X == -1 and Y == -1. 4763 {{
X, CmpRHS}, {
Y, CmpRHS}}, TrueVal, FalseVal, Q, MaxRecurse))
4771/// Try to simplify a select instruction when its condition operand is a 4772/// floating-point comparison. 4775unsigned MaxRecurse) {
4777Value *CmpLHS, *CmpRHS;
4782bool IsEquiv =
I->isEquivalence();
4783if (
I->isEquivalence(
/*Invert=*/true)) {
4785 Pred = FCmpInst::getInversePredicate(Pred);
4789// This transforms is safe if at least one operand is known to not be zero. 4790// Otherwise, the select can change the sign of a zero operand. 4800// Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped. 4801if (CmpLHS ==
F && CmpRHS ==
T)
4804if (CmpLHS !=
T || CmpRHS !=
F)
4807// This transform is also safe if we do not have (do not care about) -0.0. 4809// (T == F) ? T : F --> F 4810if (Pred == FCmpInst::FCMP_OEQ)
4813// (T != F) ? T : F --> T 4814if (Pred == FCmpInst::FCMP_UNE)
4821/// Given operands for a SelectInst, see if we can fold the result. 4822/// If not, this returns null. 4825if (
auto *CondC = dyn_cast<Constant>(
Cond)) {
4826if (
auto *TrueC = dyn_cast<Constant>(TrueVal))
4827if (
auto *FalseC = dyn_cast<Constant>(FalseVal))
4831// select poison, X, Y -> poison 4832if (isa<PoisonValue>(CondC))
4835// select undef, X, Y -> X or Y 4837return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
4839// select true, X, Y --> X 4840// select false, X, Y --> Y 4841// For vectors, allow undef/poison elements in the condition to match the 4842// defined elements, so we can eliminate the select. 4849assert(
Cond->getType()->isIntOrIntVectorTy(1) &&
4850"Select must have bool or bool vector condition");
4851assert(TrueVal->getType() == FalseVal->getType() &&
4852"Select must have same types for true/false ops");
4854if (
Cond->getType() == TrueVal->getType()) {
4855// select i1 Cond, i1 true, i1 false --> i1 Cond 4859// (X && Y) ? X : Y --> Y (commuted 2 ways) 4863// (X || Y) ? X : Y --> X (commuted 2 ways) 4867// (X || Y) ? false : X --> false (commuted 2 ways) 4872// Match patterns that end in logical-and. 4874// !(X || Y) && X --> false (commuted 2 ways) 4877// X && !(X || Y) --> false (commuted 2 ways) 4881// (X || Y) && Y --> Y (commuted 2 ways) 4884// Y && (X || Y) --> Y (commuted 2 ways) 4888// (X || Y) && (X || !Y) --> X (commuted 8 ways) 4898// Match patterns that end in logical-or. 4900// !(X && Y) || X --> true (commuted 2 ways) 4903// X || !(X && Y) --> true (commuted 2 ways) 4907// (X && Y) || Y --> Y (commuted 2 ways) 4910// Y || (X && Y) --> Y (commuted 2 ways) 4916// select ?, X, X -> X 4917if (TrueVal == FalseVal)
4920if (
Cond == TrueVal) {
4921// select i1 X, i1 X, i1 false --> X (logical-and) 4924// select i1 X, i1 X, i1 true --> true 4928if (
Cond == FalseVal) {
4929// select i1 X, i1 true, i1 X --> X (logical-or) 4932// select i1 X, i1 false, i1 X --> false 4937// If the true or false value is poison, we can fold to the other value. 4938// If the true or false value is undef, we can fold to the other value as 4939// long as the other value isn't poison. 4940// select ?, poison, X -> X 4941// select ?, undef, X -> X 4942if (isa<PoisonValue>(TrueVal) ||
4945// select ?, X, poison -> X 4946// select ?, X, undef -> X 4947if (isa<PoisonValue>(FalseVal) ||
4951// Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC'' 4953if (isa<FixedVectorType>(TrueVal->getType()) &&
4957 cast<FixedVectorType>(TrueC->
getType())->getNumElements();
4959for (
unsigned i = 0; i != NumElts; ++i) {
4960// Bail out on incomplete vector constants. 4963if (!TEltC || !FEltC)
4966// If the elements match (undef or not), that value is the result. If only 4967// one element is undef, choose the defined element as the safe result. 4970elseif (isa<PoisonValue>(TEltC) ||
4973elseif (isa<PoisonValue>(FEltC) ||
4979if (NewC.
size() == NumElts)
4992return *Imp ? TrueVal : FalseVal;
5002/// Given operands for an GetElementPtrInst, see if we can fold the result. 5003/// If not, this returns null. 5007// The type of the GEP pointer operand. 5009 cast<PointerType>(
Ptr->getType()->getScalarType())->getAddressSpace();
5011// getelementptr P -> P. 5015// Compute the (pointer) type returned by the GEP instruction. 5020// If one of the operands is a vector, the result type is a vector of 5021// pointers. All vector operands must have the same number of elements. 5022if (
VectorType *VT = dyn_cast<VectorType>(
Op->getType())) {
5023 GEPTy = VectorType::get(GEPTy, VT->getElementCount());
5029// All-zero GEP is a no-op, unless it performs a vector splat. 5030if (
Ptr->getType() == GEPTy &&
5034// getelementptr poison, idx -> poison 5035// getelementptr baseptr, poison -> poison 5036if (isa<PoisonValue>(
Ptr) ||
5037any_of(Indices, [](
constauto *V) {
return isa<PoisonValue>(V); }))
5040// getelementptr undef, idx -> undef 5046return isa<ScalableVectorType>(V->getType());
5049if (Indices.
size() == 1) {
5051if (!IsScalableVec && Ty->
isSized()) {
5055// getelementptr P, N -> P if P points to a type of zero size. 5056if (TyAllocSize == 0 &&
Ptr->getType() == GEPTy)
5059// The following transforms are only safe if the ptrtoint cast 5060// doesn't truncate the pointers. 5061if (Indices[0]->
getType()->getScalarSizeInBits() ==
5063auto CanSimplify = [GEPTy, &
P,
Ptr]() ->
bool {
5064returnP->getType() == GEPTy &&
5067// getelementptr V, (sub P, V) -> P if P points to a type of size 1. 5068if (TyAllocSize == 1 &&
5074// getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of 5079 TyAllocSize == 1ULL <<
C && CanSimplify())
5082// getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of 5095 [](
Value *
Idx) { return match(Idx, m_Zero()); })) {
5099APInt BasePtrOffset(IdxWidth, 0);
5100Value *StrippedBasePtr =
5101Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.
DL, BasePtrOffset);
5103// Avoid creating inttoptr of zero here: While LLVMs treatment of 5104// inttoptr is generally conservative, this particular case is folded to 5105// a null pointer, which will have incorrect provenance. 5107// gep (gep V, C), (sub 0, V) -> C 5110 !BasePtrOffset.
isZero()) {
5111auto *CI = ConstantInt::get(GEPTy->
getContext(), BasePtrOffset);
5114// gep (gep V, C), (xor V, -1) -> C-1 5117 !BasePtrOffset.
isOne()) {
5118auto *CI = ConstantInt::get(GEPTy->
getContext(), BasePtrOffset - 1);
5124// Check to see if this is constant foldable. 5125if (!isa<Constant>(
Ptr) ||
5126 !
all_of(Indices, [](
Value *V) {
return isa<Constant>(V); }))
5143/// Given operands for an InsertValueInst, see if we can fold the result. 5144/// If not, this returns null. 5148if (
Constant *CAgg = dyn_cast<Constant>(Agg))
5149if (
Constant *CVal = dyn_cast<Constant>(Val))
5152// insertvalue x, poison, n -> x 5153// insertvalue x, undef, n -> x if x cannot be poison 5154if (isa<PoisonValue>(Val) ||
5158// insertvalue x, (extractvalue y, n), n 5160if (EV->getAggregateOperand()->getType() == Agg->
getType() &&
5161 EV->getIndices() == Idxs) {
5162// insertvalue poison, (extractvalue y, n), n -> y 5163// insertvalue undef, (extractvalue y, n), n -> y if y cannot be poison 5164if (isa<PoisonValue>(Agg) ||
5167return EV->getAggregateOperand();
5169// insertvalue y, (extractvalue y, n), n -> y 5170if (Agg == EV->getAggregateOperand())
5180 return ::simplifyInsertValueInst(Agg, Val, Idxs, Q,
RecursionLimit);
5185// Try to constant fold. 5186auto *VecC = dyn_cast<Constant>(Vec);
5187auto *ValC = dyn_cast<Constant>(Val);
5188auto *IdxC = dyn_cast<Constant>(
Idx);
5189if (VecC && ValC && IdxC)
5192// For fixed-length vector, fold into poison if index is out of bounds. 5193if (
auto *CI = dyn_cast<ConstantInt>(
Idx)) {
5194if (isa<FixedVectorType>(Vec->
getType()) &&
5195 CI->uge(cast<FixedVectorType>(Vec->
getType())->getNumElements()))
5199// If index is undef, it might be out of bounds (see above case) 5203// If the scalar is poison, or it is undef and there is no risk of 5204// propagating poison from the vector value, simplify to the vector value. 5205if (isa<PoisonValue>(Val) ||
5209// Inserting the splatted value into a constant splat does nothing. 5210if (VecC && ValC && VecC->getSplatValue() == ValC)
5213// If we are extracting a value from a vector, then inserting it into the same 5214// place, that's the input vector: 5215// insertelt Vec, (extractelt Vec, Idx), Idx --> Vec 5222/// Given operands for an ExtractValueInst, see if we can fold the result. 5223/// If not, this returns null. 5226if (
auto *CAgg = dyn_cast<Constant>(Agg))
5229// extractvalue x, (insertvalue y, elt, n), n -> elt 5230unsigned NumIdxs = Idxs.
size();
5231for (
auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI !=
nullptr;
5232 IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
5234unsigned NumInsertValueIdxs = InsertValueIdxs.
size();
5235unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
5236if (InsertValueIdxs.
slice(0, NumCommonIdxs) ==
5237 Idxs.
slice(0, NumCommonIdxs)) {
5238if (NumIdxs == NumInsertValueIdxs)
5239return IVI->getInsertedValueOperand();
5252/// Given operands for an ExtractElementInst, see if we can fold the result. 5253/// If not, this returns null. 5256auto *VecVTy = cast<VectorType>(Vec->
getType());
5257if (
auto *CVec = dyn_cast<Constant>(Vec)) {
5258if (
auto *CIdx = dyn_cast<Constant>(
Idx))
5265// An undef extract index can be arbitrarily chosen to be an out-of-range 5266// index value, which would result in the instruction being poison. 5270// If extracting a specified index from the vector, see if we can recursively 5271// find a previously computed scalar that was inserted into the vector. 5272if (
auto *IdxC = dyn_cast<ConstantInt>(
Idx)) {
5273// For fixed-length vector, fold into undef if index is out of bounds. 5274unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
5275if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
5277// Handle case where an element is extracted from a splat. 5278if (IdxC->getValue().ult(MinNumElts))
5284// extractelt x, (insertelt y, elt, n), n -> elt 5285// If the possibly-variable indices are trivially known to be equal 5286// (because they are the same operand) then use the value that was 5287// inserted directly. 5288auto *IE = dyn_cast<InsertElementInst>(Vec);
5289if (IE && IE->getOperand(2) ==
Idx)
5290return IE->getOperand(1);
5292// The index is not relevant if our vector is a splat. 5304/// See if we can fold the given phi. If not, returns null. 5307// WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE 5308// here, because the PHI we may succeed simplifying to was not 5309// def-reachable from the original PHI! 5311// If all of the PHI's incoming values are the same then replace the PHI node 5312// with the common value. 5313Value *CommonValue =
nullptr;
5314bool HasPoisonInput =
false;
5315bool HasUndefInput =
false;
5317// If the incoming value is the phi node itself, it can safely be skipped. 5321 HasPoisonInput =
true;
5325// Remember that we saw an undef value, but otherwise ignore them. 5326 HasUndefInput =
true;
5329if (CommonValue &&
Incoming != CommonValue)
5330returnnullptr;
// Not the same, bail out. 5334// If CommonValue is null then all of the incoming values were either undef, 5335// poison or equal to the phi node itself. 5340if (HasPoisonInput || HasUndefInput) {
5341// If we have a PHI node like phi(X, undef, X), where X is defined by some 5342// instruction, we cannot return X as the result of the PHI node unless it 5343// dominates the PHI block. 5347// Make sure we do not replace an undef value with poison. 5359if (
auto *
C = dyn_cast<Constant>(
Op))
5362if (
auto *CI = dyn_cast<CastInst>(
Op)) {
5363auto *Src = CI->getOperand(0);
5364Type *SrcTy = Src->getType();
5365Type *MidTy = CI->getType();
5367if (Src->getType() == Ty) {
5377 SrcIntPtrTy, MidIntPtrTy,
5378 DstIntPtrTy) == Instruction::BitCast)
5384if (CastOpc == Instruction::BitCast)
5385if (
Op->getType() == Ty)
5388// ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X 5390if (CastOpc == Instruction::PtrToInt &&
5404/// For the given destination element of a shuffle, peek through shuffles to 5405/// match a root vector source operand that contains that element in the same 5406/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s). 5408int MaskVal,
Value *RootVec,
5409unsigned MaxRecurse) {
5413// Bail out if any mask value is undefined. That kind of shuffle may be 5414// simplified further based on demanded bits or other folds. 5418// The mask value chooses which source operand we need to look at next. 5419int InVecNumElts = cast<FixedVectorType>(Op0->
getType())->getNumElements();
5420int RootElt = MaskVal;
5421Value *SourceOp = Op0;
5422if (MaskVal >= InVecNumElts) {
5423 RootElt = MaskVal - InVecNumElts;
5427// If the source operand is a shuffle itself, look through it to find the 5428// matching root vector. 5429if (
auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
5431 DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
5432 SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
5435// The source operand is not a shuffle. Initialize the root vector value for 5436// this shuffle if that has not been done yet. 5440// Give up as soon as a source operand does not match the existing root value. 5441if (RootVec != SourceOp)
5444// The element must be coming from the same lane in the source vector 5445// (although it may have crossed lanes in intermediate shuffles). 5446if (RootElt != DestElt)
5455unsigned MaxRecurse) {
5459auto *InVecTy = cast<VectorType>(Op0->
getType());
5460unsigned MaskNumElts = Mask.size();
5461ElementCount InVecEltCount = InVecTy->getElementCount();
5466 Indices.
assign(Mask.begin(), Mask.end());
5468// Canonicalization: If mask does not select elements from an input vector, 5469// replace that input vector with poison. 5471bool MaskSelects0 =
false, MaskSelects1 =
false;
5473for (
unsigned i = 0; i != MaskNumElts; ++i) {
5474if (Indices[i] == -1)
5476if ((
unsigned)Indices[i] < InVecNumElts)
5487auto *Op0Const = dyn_cast<Constant>(Op0);
5488auto *Op1Const = dyn_cast<Constant>(Op1);
5490// If all operands are constant, constant fold the shuffle. This 5491// transformation depends on the value of the mask which is not known at 5492// compile time for scalable vectors 5493if (Op0Const && Op1Const)
5496// Canonicalization: if only one input vector is constant, it shall be the 5497// second one. This transformation depends on the value of the mask which 5498// is not known at compile time for scalable vectors 5499if (!Scalable && Op0Const && !Op1Const) {
5505// A splat of an inserted scalar constant becomes a vector constant: 5506// shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...> 5507// NOTE: We may have commuted above, so analyze the updated Indices, not the 5508// original mask constant. 5509// NOTE: This transformation depends on the value of the mask which is not 5510// known at compile time for scalable vectors 5515// Match a splat shuffle mask of the insert index allowing undef elements. 5517if (
all_of(Indices, [InsertIndex](
int MaskElt) {
5518return MaskElt == InsertIndex || MaskElt == -1;
5520assert(isa<UndefValue>(Op1) &&
"Expected undef operand 1 for splat");
5522// Shuffle mask poisons become poison constant result elements. 5524for (
unsigned i = 0; i != MaskNumElts; ++i)
5525if (Indices[i] == -1)
5531// A shuffle of a splat is always the splat itself. Legal if the shuffle's 5532// value type is same as the input vectors' type. 5533if (
auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
5538// All remaining transformation depend on the value of the mask, which is 5539// not known at compile time for scalable vectors. 5543// Don't fold a shuffle with undef mask elements. This may get folded in a 5544// better way using demanded bits or other analysis. 5545// TODO: Should we allow this? 5549// Check if every element of this shuffle can be mapped back to the 5550// corresponding element of a single root vector. If so, we don't need this 5551// shuffle. This handles simple identity shuffles as well as chains of 5552// shuffles that may widen/narrow and/or move elements across lanes and back. 5553Value *RootVec =
nullptr;
5554for (
unsigned i = 0; i != MaskNumElts; ++i) {
5555// Note that recursion is limited for each vector element, so if any element 5556// exceeds the limit, this will fail to simplify. 5560// We can't replace a widening/narrowing shuffle with one of its operands. 5567/// Given operands for a ShuffleVectorInst, fold the result or return null. 5576if (
auto *
C = dyn_cast<Constant>(
Op))
5581/// Given the operand for an FNeg, see if we can fold the result. If not, this 5589// fneg (fneg X) ==> X 5601/// Try to propagate existing NaN values when possible. If not, replace the 5602/// constant or elements in the constant with a canonical NaN. 5604Type *Ty = In->getType();
5605if (
auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
5606unsigned NumElts = VecTy->getNumElements();
5608for (
unsigned i = 0; i != NumElts; ++i) {
5609Constant *EltC = In->getAggregateElement(i);
5610// Poison elements propagate. NaN propagates except signaling is quieted. 5611// Replace unknown or undef elements with canonical NaN. 5612if (EltC && isa<PoisonValue>(EltC))
5614elseif (EltC && EltC->
isNaN())
5615 NewC[i] = ConstantFP::get(
5616 EltC->
getType(), cast<ConstantFP>(EltC)->getValue().makeQuiet());
5623// If it is not a fixed vector, but not a simple NaN either, return a 5628// If we known this is a NaN, and it's scalable vector, we must have a splat 5629// on our hands. Grab that before splatting a QNaN constant. 5630if (isa<ScalableVectorType>(Ty)) {
5631auto *
Splat = In->getSplatValue();
5633"Found a scalable-vector NaN but not a splat");
5637// Propagate an existing QNaN constant. If it is an SNaN, make it quiet, but 5638// preserve the sign/payload. 5639return ConstantFP::get(Ty, cast<ConstantFP>(In)->getValue().makeQuiet());
5642/// Perform folds that are common to any floating-point operation. This implies 5643/// transforms based on poison/undef/NaN because the operation itself makes no 5644/// difference to the result. 5649// Poison is independent of anything else. It always propagates from an 5650// operand to a math result. 5654for (
Value *V : Ops) {
5659// If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand 5660// (an undef operand can be chosen to be Nan/Inf), then the result of 5661// this operation is poison. 5662if (FMF.
noNaNs() && (IsNan || IsUndef))
5664if (FMF.
noInfs() && (IsInf || IsUndef))
5668// Undef does not propagate because undef means that all bits can take on 5669// any value. If this is undef * NaN for example, then the result values 5670// (at least the exponent bits) are limited. Assume the undef is a 5671// canonical NaN and propagate that. 5684/// Given operands for an FAdd, see if we can fold the result. If not, this 5690RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5699// With strict/constrained FP, we have these possible edge cases that do 5700// not simplify to Op0: 5701// fadd SNaN, -0.0 --> QNaN 5702// fadd +0.0, -0.0 --> -0.0 (but only with round toward negative) 5709// fadd X, 0 ==> X, when we know X is not -0 5719// With nnan: X + {+/-}Inf --> {+/-}Inf 5723// With nnan: -X + X --> 0.0 (and commuted variant) 5724// We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN. 5725// Negative zeros are allowed because we always end up with positive zero: 5726// X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 5727// X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0 5728// X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0 5729// X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0 5750/// Given operands for an FSub, see if we can fold the result. If not, this 5756RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5771// fsub X, -0 ==> X, when we know X is not -0 5777// fsub -0.0, (fsub -0.0, X) ==> X 5778// fsub -0.0, (fneg X) ==> X 5784// fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored. 5785// fsub 0.0, (fneg X) ==> X if signed zeros are ignored. 5796// fsub nnan x, x ==> 0.0 5800// With nnan: {+/-}Inf - X --> {+/-}Inf 5804// With nnan: X - {+/-}Inf --> {-/+}Inf 5829// Canonicalize special constants as operand 1. 5838// X * 0.0 --> 0.0 (with nnan and nsz) 5845// +normal number * (-)0.0 --> (-)0.0 5848// -normal number * (-)0.0 --> -(-)0.0 5854// sqrt(X) * sqrt(X) --> X, if we can: 5855// 1. Remove the intermediate rounding (reassociate). 5856// 2. Ignore non-zero negative numbers because sqrt would produce NAN. 5857// 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0. 5866/// Given the operands for an FMul, see if we can fold the result 5871RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5876// Now apply simplifications that do not require rounding. 5877returnsimplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
5884 return ::simplifyFAddInst(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
5892 return ::simplifyFSubInst(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
5900 return ::simplifyFMulInst(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
5908 return ::simplifyFMAFMul(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
5916RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5932// Requires that NaNs are off (X could be zero) and signed zeroes are 5933// ignored (X could be positive or negative, so the output sign is unknown). 5938// X / X -> 1.0 is legal when NaNs are ignored. 5939// We can ignore infinities because INF/INF is NaN. 5941return ConstantFP::get(Op0->
getType(), 1.0);
5943// (X * Y) / Y --> X if we can reassociate to the above form. 5948// -X / X -> -1.0 and 5949// X / -X -> -1.0 are legal when NaNs are ignored. 5950// We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored. 5953return ConstantFP::get(Op0->
getType(), -1.0);
5955// nnan ninf X / [-]0.0 -> poison 5967 return ::simplifyFDivInst(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
5975RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
5986// Unlike fdiv, the result of frem always matches the sign of the dividend. 5987// The constant match may include undef elements in a vector, so return a full 5988// zero constant as the result. 6005 return ::simplifyFRemInst(Op0, Op1, FMF, Q,
RecursionLimit, ExBehavior,
6009//=== Helper functions for higher up the class hierarchy. 6011/// Given the operand for a UnaryOperator, see if we can fold the result. 6012/// If not, this returns null. 6014unsigned MaxRecurse) {
6016case Instruction::FNeg:
6023/// Given the operand for a UnaryOperator, see if we can fold the result. 6024/// If not, this returns null. 6025/// Try to use FastMathFlags when folding the result. 6028unsigned MaxRecurse) {
6030case Instruction::FNeg:
6046/// Given operands for a BinaryOperator, see if we can fold the result. 6047/// If not, this returns null. 6051case Instruction::Add:
6054case Instruction::Sub:
6057case Instruction::Mul:
6060case Instruction::SDiv:
6062case Instruction::UDiv:
6064case Instruction::SRem:
6066case Instruction::URem:
6068case Instruction::Shl:
6071case Instruction::LShr:
6073case Instruction::AShr:
6075case Instruction::And:
6077case Instruction::Or:
6079case Instruction::Xor:
6081case Instruction::FAdd:
6083case Instruction::FSub:
6085case Instruction::FMul:
6087case Instruction::FDiv:
6089case Instruction::FRem:
6096/// Given operands for a BinaryOperator, see if we can fold the result. 6097/// If not, this returns null. 6098/// Try to use FastMathFlags when folding the result. 6101unsigned MaxRecurse) {
6103case Instruction::FAdd:
6105case Instruction::FSub:
6107case Instruction::FMul:
6109case Instruction::FDiv:
6126/// Given operands for a CmpInst, see if we can fold the result. 6144// Unary idempotent: f(f(x)) = f(x) 6145case Intrinsic::fabs:
6146case Intrinsic::floor:
6147case Intrinsic::ceil:
6148case Intrinsic::trunc:
6149case Intrinsic::rint:
6150case Intrinsic::nearbyint:
6151case Intrinsic::round:
6152case Intrinsic::roundeven:
6153case Intrinsic::canonicalize:
6154case Intrinsic::arithmetic_fence:
6159/// Return true if the intrinsic rounds a floating-point value to an integral 6160/// floating-point value (not an integer type). 6166case Intrinsic::floor:
6167case Intrinsic::ceil:
6168case Intrinsic::trunc:
6169case Intrinsic::rint:
6170case Intrinsic::nearbyint:
6171case Intrinsic::round:
6172case Intrinsic::roundeven:
6186auto *OffsetConstInt = dyn_cast<ConstantInt>(
Offset);
6187if (!OffsetConstInt || OffsetConstInt->getBitWidth() > 64)
6191DL.getIndexTypeSizeInBits(
Ptr->getType()));
6192if (OffsetInt.
srem(4) != 0)
6200auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
6204if (LoadedCE->getOpcode() == Instruction::Trunc) {
6205 LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6210if (LoadedCE->getOpcode() != Instruction::Sub)
6213auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
6214if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
6216auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
6220APInt LoadedRHSOffset;
6223 PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
6229// TODO: Need to pass in FastMathFlags 6232// ldexp(poison, x) -> poison 6233// ldexp(x, poison) -> poison 6234if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6237// ldexp(undef, x) -> nan 6242// TODO: Could insert a canonicalize for strict 6244// ldexp(x, undef) -> x 6252// These cases should be safe, even with strictfp. 6253// ldexp(0.0, x) -> 0.0 6254// ldexp(-0.0, x) -> -0.0 6255// ldexp(inf, x) -> inf 6256// ldexp(-inf, x) -> -inf 6257if (
C && (
C->isZero() ||
C->isInfinity()))
6260// These are canonicalization dropping, could do it if we knew how we could 6261// ignore denormal flushes and target handling of nan payload bits. 6265// TODO: Could quiet this with strictfp if the exception mode isn't strict. 6267return ConstantFP::get(Op0->
getType(),
C->makeQuiet());
6271// TODO: Could fold this if we know the exception mode isn't 6272// strict, we know the denormal mode and other target modes. 6282// Idempotent functions return the same result when called repeatedly. 6285if (
auto *
II = dyn_cast<IntrinsicInst>(Op0))
6286if (
II->getIntrinsicID() == IID)
6290// Converting from int or calling a rounding function always results in a 6291// finite integral number or infinity. For those inputs, rounding functions 6292// always return the same value, so the (2nd) rounding is eliminated. Ex: 6293// floor (sitofp x) -> sitofp x 6294// round (ceil x) -> ceil x 6295auto *
II = dyn_cast<IntrinsicInst>(Op0);
6303case Intrinsic::fabs:
6307case Intrinsic::bswap:
6308// bswap(bswap(x)) -> x 6312case Intrinsic::bitreverse:
6313// bitreverse(bitreverse(x)) -> x 6317case Intrinsic::ctpop: {
6318// ctpop(X) -> 1 iff X is non-zero power of 2. 6321return ConstantInt::get(Op0->
getType(), 1);
6322// If everything but the lowest bit is zero, that bit is the pop-count. Ex: 6323// ctpop(and X, 1) --> and X, 1 6332if (Call->hasAllowReassoc() &&
6336case Intrinsic::exp2:
6337// exp2(log2(x)) -> x 6338if (Call->hasAllowReassoc() &&
6342case Intrinsic::exp10:
6343// exp10(log10(x)) -> x 6344if (Call->hasAllowReassoc() &&
6350if (Call->hasAllowReassoc() &&
6354case Intrinsic::log2:
6355// log2(exp2(x)) -> x 6356if (Call->hasAllowReassoc() &&
6362case Intrinsic::log10:
6363// log10(pow(10.0, x)) -> x 6364// log10(exp10(x)) -> x 6365if (Call->hasAllowReassoc() &&
6371case Intrinsic::vector_reverse:
6372// vector.reverse(vector.reverse(x)) -> x 6375// vector.reverse(splat(X)) -> splat(X) 6379case Intrinsic::frexp: {
6380// Frexp is idempotent with the added complication of the struct return. 6395/// Given a min/max intrinsic, see if it can be removed based on having an 6396/// operand that is another min/max intrinsic with shared operand(s). The caller 6397/// is expected to swap the operand arguments to handle commutation. 6403auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
6408if (Op1 ==
X || Op1 ==
Y ||
6410// max (max X, Y), X --> max X, Y 6413// max (min X, Y), X --> X 6420/// Given a min/max intrinsic, see if it can be removed based on having an 6421/// operand that is another min/max intrinsic with shared operand(s). The caller 6422/// is expected to swap the operand arguments to handle commutation. 6425assert((IID == Intrinsic::maxnum || IID == Intrinsic::minnum ||
6426 IID == Intrinsic::maximum || IID == Intrinsic::minimum) &&
6427"Unsupported intrinsic");
6429auto *
M0 = dyn_cast<IntrinsicInst>(Op0);
6430// If Op0 is not the same intrinsic as IID, do not process. 6431// This is a difference with integer min/max handling. We do not process the 6432// case like max(min(X,Y),min(X,Y)) => min(X,Y). But it can be handled by GVN. 6433if (!
M0 ||
M0->getIntrinsicID() != IID)
6437// Simple case, m(m(X,Y), X) => m(X, Y) 6438// m(m(X,Y), Y) => m(X, Y) 6439// For minimum/maximum, X is NaN => m(NaN, Y) == NaN and m(NaN, NaN) == NaN. 6440// For minimum/maximum, Y is NaN => m(X, NaN) == NaN and m(NaN, NaN) == NaN. 6441// For minnum/maxnum, X is NaN => m(NaN, Y) == Y and m(Y, Y) == Y. 6442// For minnum/maxnum, Y is NaN => m(X, NaN) == X and m(X, NaN) == X. 6443if (X0 == Op1 || Y0 == Op1)
6446auto *
M1 = dyn_cast<IntrinsicInst>(Op1);
6452// we have a case m(m(X,Y),m'(X,Y)) taking into account m' is commutative. 6453// if m' is m or inversion of m => m(m(X,Y),m'(X,Y)) == m(X,Y). 6454// For minimum/maximum, X is NaN => m(NaN,Y) == m'(NaN, Y) == NaN. 6455// For minimum/maximum, Y is NaN => m(X,NaN) == m'(X, NaN) == NaN. 6456// For minnum/maxnum, X is NaN => m(NaN,Y) == m'(NaN, Y) == Y. 6457// For minnum/maxnum, Y is NaN => m(X,NaN) == m'(X, NaN) == X. 6458if ((X0 == X1 && Y0 == Y1) || (X0 == Y1 && Y0 == X1))
6469unsignedBitWidth = ReturnType->getScalarSizeInBits();
6472// abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here. 6473// It is always ok to pick the earlier abs. We'll just lose nsw if its only 6479case Intrinsic::cttz: {
6485case Intrinsic::ctlz: {
6493case Intrinsic::ptrmask: {
6494if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
6497// NOTE: We can't apply this simplifications based on the value of Op1 6498// because we need to preserve provenance. 6504"Invalid mask width");
6505// If index-width (mask size) is less than pointer-size then mask is 6510// NOTE: We may have attributes associated with the return value of the 6511// llvm.ptrmask intrinsic that will be lost when we just return the 6512// operand. We should try to preserve them. 6519// See if we only masking off bits we know are already zero due to 6521APInt IrrelevantPtrBits =
6524 Instruction::Or,
C, ConstantInt::get(
C->getType(), IrrelevantPtrBits),
6526if (
C !=
nullptr &&
C->isAllOnesValue())
6531case Intrinsic::smax:
6532case Intrinsic::smin:
6533case Intrinsic::umax:
6534case Intrinsic::umin: {
6535// If the arguments are the same, this is a no-op. 6539// Canonicalize immediate constant operand as Op1. 6543// Assume undef is the limit value. 6545return ConstantInt::get(
6550// Clamp to limit value. For example: 6551// umax(i8 %x, i8 255) --> 255 6553return ConstantInt::get(ReturnType, *
C);
6555// If the constant op is the opposite of the limit value, the other must 6556// be larger/smaller or equal. For example: 6557// umin(i8 %x, i8 255) --> %x 6562// Remove nested call if constant operands allow it. Example: 6563// max (max X, 7), 5 -> max X, 7 6564auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
6565if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
6566// TODO: loosen undef/splat restrictions for vector constants. 6567Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
6571 ICmpInst::getNonStrictPredicate(
6591case Intrinsic::scmp:
6592case Intrinsic::ucmp: {
6593// Fold to a constant if the relationship between operands can be 6594// established with certainty 6599 IID == Intrinsic::scmp ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
6601return ConstantInt::get(ReturnType, 1);
6604 IID == Intrinsic::scmp ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
6610case Intrinsic::usub_with_overflow:
6611case Intrinsic::ssub_with_overflow:
6612// X - X -> { 0, false } 6613// X - undef -> { 0, false } 6614// undef - X -> { 0, false } 6618case Intrinsic::uadd_with_overflow:
6619case Intrinsic::sadd_with_overflow:
6620// X + undef -> { -1, false } 6621// undef + x -> { -1, false } 6624 cast<StructType>(ReturnType),
6629case Intrinsic::umul_with_overflow:
6630case Intrinsic::smul_with_overflow:
6631// 0 * X -> { 0, false } 6632// X * 0 -> { 0, false } 6635// undef * X -> { 0, false } 6636// X * undef -> { 0, false } 6640case Intrinsic::uadd_sat:
6641// sat(MAX + X) -> MAX 6642// sat(X + MAX) -> MAX 6646case Intrinsic::sadd_sat:
6647// sat(X + undef) -> -1 6648// sat(undef + X) -> -1 6649// For unsigned: Assume undef is MAX, thus we saturate to MAX (-1). 6650// For signed: Assume undef is ~X, in which case X + ~X = -1. 6661case Intrinsic::usub_sat:
6662// sat(0 - X) -> 0, sat(X - MAX) -> 0 6666case Intrinsic::ssub_sat:
6667// X - X -> 0, X - undef -> 0, undef - X -> 0 6674case Intrinsic::load_relative:
6675if (
auto *C0 = dyn_cast<Constant>(Op0))
6676if (
auto *C1 = dyn_cast<Constant>(Op1))
6679case Intrinsic::powi:
6680if (
auto *Power = dyn_cast<ConstantInt>(Op1)) {
6683return ConstantFP::get(Op0->
getType(), 1.0);
6689case Intrinsic::ldexp:
6691case Intrinsic::copysign:
6692// copysign X, X --> X 6695// copysign -X, X --> X 6696// copysign X, -X --> -X 6701case Intrinsic::is_fpclass: {
6702if (isa<PoisonValue>(Op0))
6705uint64_t Mask = cast<ConstantInt>(Op1)->getZExtValue();
6706// If all tests are made, it doesn't matter what the value is. 6708return ConstantInt::get(ReturnType,
true);
6710return ConstantInt::get(ReturnType,
false);
6715case Intrinsic::maxnum:
6716case Intrinsic::minnum:
6717case Intrinsic::maximum:
6718case Intrinsic::minimum: {
6719// If the arguments are the same, this is a no-op. 6723// Canonicalize constant operand as Op1. 6724if (isa<Constant>(Op0))
6727// If an argument is undef, return the other argument. 6731bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
6732bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
6734// minnum(X, nan) -> X 6735// maxnum(X, nan) -> X 6736// minimum(X, nan) -> nan 6737// maximum(X, nan) -> nan 6739return PropagateNaN ?
propagateNaN(cast<Constant>(Op1)) : Op0;
6741// In the following folds, inf can be replaced with the largest finite 6742// float, if the ninf flag is set. 6745 (
C->isInfinity() || (Call && Call->hasNoInfs() &&
C->isLargest()))) {
6746// minnum(X, -inf) -> -inf 6747// maxnum(X, +inf) -> +inf 6748// minimum(X, -inf) -> -inf if nnan 6749// maximum(X, +inf) -> +inf if nnan 6750if (
C->isNegative() == IsMin &&
6751 (!PropagateNaN || (Call && Call->hasNoNaNs())))
6752return ConstantFP::get(ReturnType, *
C);
6754// minnum(X, +inf) -> X if nnan 6755// maxnum(X, -inf) -> X if nnan 6756// minimum(X, +inf) -> X 6757// maximum(X, -inf) -> X 6758if (
C->isNegative() != IsMin &&
6759 (PropagateNaN || (Call && Call->hasNoNaNs())))
6763// Min/max of the same operation with common operand: 6764// m(m(X, Y)), X --> m(X, Y) (4 commuted variants) 6772case Intrinsic::vector_extract: {
6773// (extract_vector (insert_vector _, X, 0), 0) -> X 6774unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
6778 IdxN == 0 &&
X->getType() == ReturnType)
6793// Operand bundles should not be in Args. 6794assert(Call->arg_size() == Args.size());
6795unsigned NumOperands = Args.size();
6799// Most of the intrinsics with no operands have some kind of side effect. 6803case Intrinsic::vscale: {
6807return ConstantInt::get(
RetTy,
C->getZExtValue());
6815if (NumOperands == 1)
6818if (NumOperands == 2)
6822// Handle intrinsics with 3 or more arguments. 6824case Intrinsic::masked_load:
6825case Intrinsic::masked_gather: {
6826Value *MaskArg = Args[2];
6827Value *PassthruArg = Args[3];
6828// If the mask is all zeros or undef, the "passthru" argument is the result. 6833case Intrinsic::fshl:
6834case Intrinsic::fshr: {
6835Value *Op0 = Args[0], *Op1 = Args[1], *ShAmtArg = Args[2];
6837// If both operands are undef, the result is undef. 6841// If shift amount is undef, assume it is zero. 6843return Args[IID == Intrinsic::fshl ? 0 : 1];
6847// If there's effectively no shift, return the 1st arg or 2nd arg. 6850return Args[IID == Intrinsic::fshl ? 0 : 1];
6853// Rotating zero by anything is zero. 6855return ConstantInt::getNullValue(
F->getReturnType());
6857// Rotating -1 by anything is -1. 6859return ConstantInt::getAllOnesValue(
F->getReturnType());
6863case Intrinsic::experimental_constrained_fma: {
6864auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6866 *FPI->getRoundingMode()))
6871case Intrinsic::fmuladd: {
6873 RoundingMode::NearestTiesToEven))
6877case Intrinsic::smul_fix:
6878case Intrinsic::smul_fix_sat: {
6879Value *Op0 = Args[0];
6880Value *Op1 = Args[1];
6881Value *Op2 = Args[2];
6882Type *ReturnType =
F->getReturnType();
6884// Canonicalize constant operand as Op1 (ConstantFolding handles the case 6885// when both Op0 and Op1 are constant so we do not care about that special 6887if (isa<Constant>(Op0))
6898// X * (1 << Scale) -> X 6901 cast<ConstantInt>(Op2)->getZExtValue());
6907case Intrinsic::vector_insert: {
6908Value *Vec = Args[0];
6909Value *SubVec = Args[1];
6911Type *ReturnType =
F->getReturnType();
6913// (insert_vector Y, (extract_vector X, 0), 0) -> X 6914// where: Y is X, or Y is undef 6915unsigned IdxN = cast<ConstantInt>(
Idx)->getZExtValue();
6920X->getType() == ReturnType)
6925case Intrinsic::experimental_constrained_fadd: {
6926auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6928 *FPI->getExceptionBehavior(),
6929 *FPI->getRoundingMode());
6931case Intrinsic::experimental_constrained_fsub: {
6932auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6934 *FPI->getExceptionBehavior(),
6935 *FPI->getRoundingMode());
6937case Intrinsic::experimental_constrained_fmul: {
6938auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6940 *FPI->getExceptionBehavior(),
6941 *FPI->getRoundingMode());
6943case Intrinsic::experimental_constrained_fdiv: {
6944auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6946 *FPI->getExceptionBehavior(),
6947 *FPI->getRoundingMode());
6949case Intrinsic::experimental_constrained_frem: {
6950auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
6952 *FPI->getExceptionBehavior(),
6953 *FPI->getRoundingMode());
6955case Intrinsic::experimental_constrained_ldexp:
6957case Intrinsic::experimental_gc_relocate: {
6962// Undef is undef, even after relocation. 6963if (isa<UndefValue>(DerivedPtr) || isa<UndefValue>(BasePtr)) {
6967if (
auto *PT = dyn_cast<PointerType>(GCR.
getType())) {
6968// For now, the assumption is that the relocation of null will be null 6969// for most any collector. If this ever changes, a corresponding hook 6970// should be added to GCStrategy and this code should check it first. 6971if (isa<ConstantPointerNull>(DerivedPtr)) {
6972// Use null-pointer of gc_relocate's type to replace it. 6986auto *
F = dyn_cast<Function>(Callee);
6991 ConstantArgs.
reserve(Args.size());
6992for (
Value *Arg : Args) {
6995if (isa<MetadataAsValue>(Arg))
7007// Args should not contain operand bundle operands. 7008assert(Call->arg_size() == Args.size());
7010// musttail calls can only be simplified if they are also DCEd. 7011// As we can't guarantee this here, don't simplify them. 7012if (Call->isMustTailCall())
7015// call undef -> poison 7016// call null -> poison 7017if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
7023auto *
F = dyn_cast<Function>(Callee);
7024if (
F &&
F->isIntrinsic())
7032assert(isa<ConstrainedFPIntrinsic>(Call));
7041/// Given operands for a Freeze, see if we can fold the result. 7043// Use a utility function defined in ValueTracking. 7046// We have room for improvement. 7051 return ::simplifyFreezeInst(Op0, Q);
7059if (
auto *PtrOpC = dyn_cast<Constant>(PtrOp))
7062// We can only fold the load if it is from a constant global with definitive 7063// initializer. Skip expensive logic if this is not the case. 7065if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
7068// If GlobalVariable's initializer is uniform, then return the constant 7069// regardless of its offset. 7074// Try to convert operand into a constant by stripping offsets while looking 7075// through invariant.group intrinsics. 7078 Q.
DL,
Offset,
/* AllowNonInbounts */true,
7079/* AllowInvariantGroup */true);
7081// Index size may have changed due to address space casts. 7090/// See if we can compute a simplified version of this instruction. 7091/// If not, this returns null. 7096unsigned MaxRecurse) {
7097assert(
I->getFunction() &&
"instruction should be inserted in a function");
7099"context instruction should be in the same function");
7103switch (
I->getOpcode()) {
7108 [](
Value *V) { return cast<Constant>(V); });
7112case Instruction::FNeg:
7114case Instruction::FAdd:
7117case Instruction::Add:
7121case Instruction::FSub:
7124case Instruction::Sub:
7128case Instruction::FMul:
7131case Instruction::Mul:
7135case Instruction::SDiv:
7139case Instruction::UDiv:
7143case Instruction::FDiv:
7146case Instruction::SRem:
7148case Instruction::URem:
7150case Instruction::FRem:
7153case Instruction::Shl:
7157case Instruction::LShr:
7161case Instruction::AShr:
7165case Instruction::And:
7167case Instruction::Or:
7169case Instruction::Xor:
7171case Instruction::ICmp:
7173 NewOps[1], Q, MaxRecurse);
7174case Instruction::FCmp:
7176 NewOps[1],
I->getFastMathFlags(), Q, MaxRecurse);
7177case Instruction::Select:
7179case Instruction::GetElementPtr: {
7180auto *GEPI = cast<GetElementPtrInst>(
I);
7182ArrayRef(NewOps).slice(1), GEPI->getNoWrapFlags(), Q,
7185case Instruction::InsertValue: {
7190case Instruction::InsertElement:
7192case Instruction::ExtractValue: {
7193auto *EVI = cast<ExtractValueInst>(
I);
7197case Instruction::ExtractElement:
7199case Instruction::ShuffleVector: {
7200auto *SVI = cast<ShuffleVectorInst>(
I);
7202 SVI->getShuffleMask(), SVI->getType(), Q,
7205case Instruction::PHI:
7207case Instruction::Call:
7209 cast<CallInst>(
I), NewOps.
back(),
7210 NewOps.
drop_back(1 + cast<CallInst>(
I)->getNumTotalBundleOperands()), Q);
7211case Instruction::Freeze:
7213#define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc: 7214#include "llvm/IR/Instruction.def" 7215#undef HANDLE_CAST_INST 7218case Instruction::Alloca:
7219// No simplifications for Alloca and it can't be constant folded. 7221case Instruction::Load:
7230"Number of operands should match the instruction!");
7231 return ::simplifyInstructionWithOperands(
I, NewOps, SQ,
RecursionLimit);
7238 /// If called on unreachable code, the instruction may simplify to itself. 7239 /// Make life easier for users by detecting that case here, and returning a 7240 /// safe value instead. 7244/// Implementation of recursive simplification through an instruction's 7247/// This is the common implementation of the recursive simplification routines. 7248/// If we have a pre-simplified value in 'SimpleV', that is forcibly used to 7249/// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of 7250/// instructions to process and attempt to simplify it using 7251/// InstructionSimplify. Recursively visited users which could not be 7252/// simplified themselves are to the optional UnsimplifiedUsers set for 7253/// further processing by the caller. 7255/// This routine returns 'true' only when *it* simplifies something. The passed 7256/// in simplified value does not count toward this. 7261bool Simplified =
false;
7265// If we have an explicit value to collapse to, do that round of the 7266// simplification loop by hand initially. 7268for (
User *U :
I->users())
7270 Worklist.
insert(cast<Instruction>(U));
7272// Replace the instruction with its simplified value. 7273I->replaceAllUsesWith(SimpleV);
7275if (!
I->isEHPad() && !
I->isTerminator() && !
I->mayHaveSideEffects())
7276I->eraseFromParent();
7281// Note that we must test the size on each iteration, the worklist can grow. 7285// See if this instruction simplifies. 7288if (UnsimplifiedUsers)
7289 UnsimplifiedUsers->insert(
I);
7295// Stash away all the uses of the old instruction so we can check them for 7296// recursive simplifications after a RAUW. This is cheaper than checking all 7297// uses of To on the recursive step in most cases. 7298for (
User *U :
I->users())
7299 Worklist.
insert(cast<Instruction>(U));
7301// Replace the instruction with its simplified value. 7302I->replaceAllUsesWith(SimpleV);
7304if (!
I->isEHPad() && !
I->isTerminator() && !
I->mayHaveSideEffects())
7305I->eraseFromParent();
7314assert(
I != SimpleV &&
"replaceAndRecursivelySimplify(X,X) is not valid!");
7315assert(SimpleV &&
"Must provide a simplified value.");
7323auto *DT = DTWP ? &DTWP->getDomTree() :
nullptr;
7325auto *TLI = TLIWP ? &TLIWP->
getTLI(
F) :
nullptr;
7328return {
F.getDataLayout(), TLI, DT, AC};
7336template <
classT,
class... TArgs>
7339auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(
F);
7340auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(
F);
7341auto *AC = AM.template getCachedResult<AssumptionAnalysis>(
F);
7342return {
F.getDataLayout(), TLI, DT, AC};
7356void InstSimplifyFolder::anchor() {}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
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")
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
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static Value * simplifyFreezeInst(Value *Op0, const SimplifyQuery &Q)
Given operands for a Freeze, see if we can fold the result.
static Value * simplifyCmpSelFalseCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with false branch of select.
static Value * simplifyCmpSelCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse, Constant *TrueOrFalse)
Simplify comparison with true or false branch of select: sel = select i1 cond, i32 tv,...
static Value * simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS, CmpPredicate Pred, Value *TrueVal, Value *FalseVal)
An alternative way to test if a bit is set or not uses sgt/slt instead of eq/ne.
static Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an LShr, see if we can fold the result.
static Value * simplifyUDivInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a UDiv, see if we can fold the result.
static Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1)
Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/ma...
static Value * simplifySubInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a Sub, see if we can fold the result.
static Value * simplifyFCmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an FCmpInst, see if we can fold the result.
static Value * expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L, Value *R, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify binops of form "A op (B op' C)" or the commuted variant by distributing op over op'.
static Constant * foldOrCommuteConstant(Instruction::BinaryOps Opcode, Value *&Op0, Value *&Op1, const SimplifyQuery &Q)
static bool haveNonOverlappingStorage(const Value *V1, const Value *V2)
Return true if V1 and V2 are each the base of some distict storage region [V, object_size(V)] which d...
static Constant * foldConstant(Instruction::UnaryOps Opcode, Value *&Op, const SimplifyQuery &Q)
static Value * handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
We know comparison with both branches of select can be simplified, but they are not equal.
static Value * threadCmpOverPHI(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a PHI instruction, try to simplify the comparison by seeing whether ...
static Constant * propagateNaN(Constant *In)
Try to propagate existing NaN values when possible.
static Value * simplifyICmpOfBools(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Fold an icmp when its operands have i1 scalar type.
static Value * simplifyICmpWithBinOpOnLHS(CmpPredicate Pred, BinaryOperator *LBO, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an AShr, see if we can fold the result.
static Value * simplifyRelativeLoad(Constant *Ptr, Constant *Offset, const DataLayout &DL)
static Value * simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SDiv and UDiv.
static Value * simplifyPHINode(PHINode *PN, ArrayRef< Value * > IncomingValues, const SimplifyQuery &Q)
See if we can fold the given phi. If not, returns null.
static Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &, unsigned)
Given operands for an ExtractValueInst, see if we can fold the result.
static Value * simplifySelectInst(Value *, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a SelectInst, see if we can fold the result.
static Value * simplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Add, see if we can fold the result.
static Value * simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
static bool isSameCompare(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS)
isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
static Value * simplifyAndCommutative(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &SQ, unsigned MaxRecurse)
See if we can compute a simplified version of this instruction.
static bool isIdempotent(Intrinsic::ID ID)
static std::optional< ConstantRange > getRange(Value *V, const InstrInfoQuery &IIQ)
Helper method to get range from metadata or attribute.
static Value * simplifyAndOrOfICmpsWithCtpop(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
Try to simplify and/or of icmp with ctpop intrinsic.
static Value * simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp, ICmpInst *UnsignedICmp, bool IsAnd, const SimplifyQuery &Q)
Commuted variants are assumed to be handled by calling this function again with the parameters swappe...
static Value * tryConstantFoldCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
static Value * simplifyWithOpsReplaced(Value *V, ArrayRef< std::pair< Value *, Value * > > Ops, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags, unsigned MaxRecurse)
static Value * simplifyICmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an ICmpInst, see if we can fold the result.
static Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q, unsigned)
Given operands for an ExtractElementInst, see if we can fold the result.
static Value * simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
static Value * simplifyICmpWithMinMax(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
simplify integer comparisons where at least one operand of the compare matches an integer min/max idi...
static Value * simplifyCmpSelTrueCase(CmpPredicate Pred, Value *LHS, Value *RHS, Value *Cond, const SimplifyQuery &Q, unsigned MaxRecurse)
Simplify comparison with true branch of select.
static Value * simplifyIntrinsic(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
static void getUnsignedMonotonicValues(SmallPtrSetImpl< Value * > &Res, Value *V, MonotonicType Type, unsigned Depth=0)
Get values V_i such that V uge V_i (GreaterEq) or V ule V_i (LowerEq).
static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q)
Returns true if a shift by Amount always yields poison.
static APInt stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V, bool AllowNonInbounds=false)
Compute the base pointer and cumulative constant offsets for V.
static Value * simplifyCmpInst(CmpPredicate, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a CmpInst, see if we can fold the result.
static Value * simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior, RoundingMode Rounding)
static Value * simplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an LShr or AShr, see if we can fold the result.
static Value * simplifyICmpWithIntrinsicOnLHS(CmpPredicate Pred, Value *LHS, Value *RHS)
static Value * simplifySDivInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an SDiv, see if we can fold the result.
static Value * simplifyByDomEq(unsigned Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Test if there is a dominating equivalence condition for the two operands.
static Value * simplifyFPUnOp(unsigned, Value *, const FastMathFlags &, const SimplifyQuery &, unsigned)
Given the operand for a UnaryOperator, see if we can fold the result.
static Value * simplifyICmpWithBinOp(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
TODO: A large part of this logic is duplicated in InstCombine's foldICmpBinOp().
static Value * simplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FAdd, see if we can fold the result.
static Value * simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
static Value * expandBinOp(Instruction::BinaryOps Opcode, Value *V, Value *OtherOp, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a binary operator of form "V op OtherOp" where V is "(B0 opex B1)" by distributing 'o...
static Value * simplifyICmpWithZero(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Try hard to fold icmp with zero RHS because this is a common case.
static Value * simplifyICmpWithConstant(CmpPredicate Pred, Value *LHS, Value *RHS, const InstrInfoQuery &IIQ)
static Value * simplifySelectWithFCmp(Value *Cond, Value *T, Value *F, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is a floating-point comparison.
static Constant * getFalse(Type *Ty)
For a boolean type or a vector of boolean type, return false or a vector with every element false.
static Value * simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Check for common or similar folds of integer division or integer remainder.
static bool removesFPFraction(Intrinsic::ID ID)
Return true if the intrinsic rounds a floating-point value to an integral floating-point value (not a...
static Value * simplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
static Value * simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1, const InstrInfoQuery &IIQ)
static Value * simplifySelectWithEquivalence(ArrayRef< std::pair< Value *, Value * > > Replacements, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is an integer equality or floating-po...
static Value * simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a Mul, see if we can fold the result.
static Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse)
Given the operand for an FNeg, see if we can fold the result.
static Value * simplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for an Or, see if we can fold the result.
static bool trySimplifyICmpWithAdds(CmpPredicate Pred, Value *LHS, Value *RHS, const InstrInfoQuery &IIQ)
static Value * simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X, const APInt *Y, bool TrueWhenUnset)
Try to simplify a select instruction when its condition operand is an integer comparison where one op...
static Value * simplifyAssociativeBinOp(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Generic simplifications for associative binary operations.
static Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, see if we can fold the result.
static Value * threadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with an operand that is a PHI instruction, try to simplify the bino...
static Value * simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS, CmpPredicate Pred, Value *TVal, Value *FVal)
static Value * simplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
static Value * simplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FSub, see if we can fold the result.
static Value * simplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a Xor, see if we can fold the result.
static Value * simplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for a URem, see if we can fold the result.
static Constant * simplifyFPOp(ArrayRef< Value * > Ops, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior, RoundingMode Rounding)
Perform folds that are common to any floating-point operation.
static Value * threadCmpOverSelect(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a comparison with a select instruction, try to simplify the comparison by seeing wheth...
static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Implementation of recursive simplification through an instruction's uses.
static bool isAllocDisjoint(const Value *V)
Return true if the underlying object (storage) must be disjoint from storage returned by any noalias ...
static Constant * getTrue(Type *Ty)
For a boolean type or a vector of boolean type, return true or a vector with every element true.
static Value * simplifyGEPInst(Type *, Value *, ArrayRef< Value * >, GEPNoWrapFlags, const SimplifyQuery &, unsigned)
Given operands for an GetElementPtrInst, see if we can fold the result.
static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, unsigned MaxRecurse, bool IsSigned)
Return true if we can simplify X / Y to 0.
static Value * simplifyLdexp(Value *Op0, Value *Op1, const SimplifyQuery &Q, bool IsStrict)
static Value * simplifyLogicOfAddSub(Value *Op0, Value *Op1, Instruction::BinaryOps Opcode)
Given a bitwise logic op, check if the operands are add/sub with a common source value and inverted c...
static Value * simplifyOrLogic(Value *X, Value *Y)
static Type * getCompareTy(Value *Op)
static Value * simplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, unsigned)
static Value * simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1, const SimplifyQuery &Q)
static Value * simplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for a BinaryOperator, see if we can fold the result.
static bool isICmpTrue(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
Given a predicate and two operands, return true if the comparison is true.
static Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q, unsigned)
Given operands for an InsertValueInst, see if we can fold the result.
static Value * simplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned)
Given operands for an And, see if we can fold the result.
static Value * foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, int MaskVal, Value *RootVec, unsigned MaxRecurse)
For the given destination element of a shuffle, peek through shuffles to match a root vector source o...
static Value * simplifyAndOrOfFCmps(const SimplifyQuery &Q, FCmpInst *LHS, FCmpInst *RHS, bool IsAnd)
static Value * extractEquivalentCondition(Value *V, CmpPredicate Pred, Value *LHS, Value *RHS)
Rummage around inside V looking for something equivalent to the comparison "LHS Pred RHS".
static Value * simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, Value *Op1, bool IsAnd)
static Value * simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags, unsigned MaxRecurse)
static Value * threadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse)
In the case of a binary operation with a select instruction as an operand, try to simplify the binop ...
static Value * simplifyICmpUsingMonotonicValues(CmpPredicate Pred, Value *LHS, Value *RHS)
static Constant * computePointerDifference(const DataLayout &DL, Value *LHS, Value *RHS)
Compute the constant difference between two pointer values.
static Value * simplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an SRem, see if we can fold the result.
static Value * simplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF, const SimplifyQuery &Q, unsigned MaxRecurse, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given the operands for an FMul, see if we can fold the result.
static Value * simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd)
Test if a pair of compares with a shared operand and 2 constants has an empty set intersection,...
static Value * simplifyAndOrWithICmpEq(unsigned Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
static Value * simplifyICmpWithDominatingAssume(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static Value * simplifyShift(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, bool IsNSW, const SimplifyQuery &Q, unsigned MaxRecurse)
Given operands for an Shl, LShr or AShr, see if we can fold the result.
static Constant * computePointerICmp(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
static Value * simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, const SimplifyQuery &Q, unsigned MaxRecurse)
These are simplifications common to SRem and URem.
static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT)
Does the given value dominate the specified phi node?
static Value * simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q, unsigned MaxRecurse)
Try to simplify a select instruction when its condition operand is an integer comparison.
static Value * foldMinimumMaximumSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1)
Given a min/max intrinsic, see if it can be removed based on having an operand that is another min/ma...
static Value * simplifyUnaryIntrinsic(Function *F, Value *Op0, const SimplifyQuery &Q, const CallBase *Call)
This header provides classes for managing per-loop analyses.
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const SmallVectorImpl< MachineOperand > & Cond
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 implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static SymbolRef::Type getType(const Symbol *Sym)
static const uint32_t IV[8]
Class for arbitrary precision integers.
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
void setSignBit()
Set the sign bit to 1.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
unsigned countr_zero() const
Count the number of trailing zero bits.
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
bool getBoolValue() const
Convert APInt to a boolean value.
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
bool isMask(unsigned numBits) const
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.
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
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.
bool isOne() const
Determine if this is a value of 1.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
an instruction to allocate memory on the stack
A container for analyses that lazily runs them and caches their results.
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),...
const T & back() const
back - Get the last element.
size_t size() const
size - Get the array size.
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
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.
An immutable pass that tracks lazily created AssumptionCache objects.
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
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...
BinaryOps getOpcode() const
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
static unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, Type *DstIntPtrTy)
Determine how a pair of casts can be eliminated, if they can be at all.
This class is the base class for the comparison instructions.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Predicate getStrictPredicate() const
For example, SGE -> SGT, SLE -> SLT, ULE -> ULT, UGE -> UGT.
bool isFalseWhenEqual() const
This is just a convenience.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ ICMP_ULT
unsigned less than
@ ICMP_SGE
signed greater or equal
@ ICMP_ULE
unsigned less or equal
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
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.
bool isIntPredicate() const
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
static Constant * getNot(Constant *C)
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
static Constant * getZero(Type *Ty, bool Negative=false)
static Constant * getNegativeZero(Type *Ty)
static Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static ConstantInt * getBool(LLVMContext &Context, bool V)
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
This class represents a range of values.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
bool isEmptySet() const
Return true if this set contains no members.
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...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static Constant * get(StructType *T, ArrayRef< Constant * > V)
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
static Constant * getAllOnesValue(Type *Ty)
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool isNaN() const
Return true if this is a floating-point NaN constant or a vector floating-point constant with all NaN...
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
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.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Legacy analysis pass which computes a DominatorTree.
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.
This instruction compares its operands according to the predicate given to the constructor.
Convenience struct for specifying and reasoning about fast-math flags.
bool noSignedZeros() const
bool allowReassoc() const
Flag queries.
Represents calls to the gc.relocate intrinsic.
Value * getBasePtr() const
Value * getDerivedPtr() const
Represents flags for the getelementptr instruction/expression.
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
This instruction compares its operands according to the predicate given to the constructor.
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
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.
bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
const Function * getFunction() const
Return the function this instruction belongs to.
An instruction for reading from memory.
bool isVolatile() const
Return true if this is a load from a volatile memory location.
static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits)
Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, so there is a certain thre...
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
op_range incoming_values()
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.
Pass interface - Implemented by all 'passes'.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents a cast from a pointer to an integer.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
size_type size() const
Determine the number of elements in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetLibraryInfo & getTLI(const Function &F)
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
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.
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.
bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
static IntegerType * getInt32Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
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.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
LLVMContext & getContext() const
All values hold a context through their type.
This class represents zero extension of integer types.
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.
#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.
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
cst_pred_ty< is_negative > m_Negative()
Match an integer or vector of negative values.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::FMul, true > m_c_FMul(const LHS &L, const RHS &R)
Matches FMul with LHS and RHS in either order.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cstfp_pred_ty< is_inf > m_Inf()
Match a positive or negative infinity FP constant.
m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::FSub > m_FSub(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< cstfp_pred_ty< is_any_zero_fp >, RHS, Instruction::FSub > m_FNegNSZ(const RHS &X)
Match 'fneg X' as 'fsub +-0.0, X'.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
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.
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)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
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.
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
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.
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
specific_fpval m_SpecificFP(double V)
Match a specific floating point value or vector with all elements equal to the value.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_Sqrt(const Opnd0 &Op0)
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.
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
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.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoSignedWrap > m_NSWShl(const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Shl, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWShl(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWMul(const LHS &L, const RHS &R)
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)
cst_pred_ty< custom_checkfn< APInt > > m_CheckedInt(function_ref< bool(const APInt &)> CheckFn)
Match an integer or vector where CheckFn(ele) for each element is true.
specific_fpval m_FPOne()
Match a float 1.0 or vector with all elements equal to 1.0.
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.
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > > > m_c_MaxOrMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
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.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
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)
Exact_match< T > m_Exact(const T &SubPattern)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
cstfp_pred_ty< is_pos_zero_fp > m_PosZeroFP()
Match a floating-point positive zero.
BinaryOp_match< LHS, RHS, Instruction::FAdd, true > m_c_FAdd(const LHS &L, const RHS &R)
Matches FAdd with LHS and RHS in either order.
LogicalOp_match< LHS, RHS, Instruction::And, true > m_c_LogicalAnd(const LHS &L, const RHS &R)
Matches L && R with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
cstfp_pred_ty< is_nan > m_NaN()
Match an arbitrary NaN constant.
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)
m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)
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.
LogicalOp_match< LHS, RHS, Instruction::Or, true > m_c_LogicalOr(const LHS &L, const RHS &R)
Matches L || R with LHS and RHS in either order.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap > m_NSWMul(const LHS &L, const RHS &R)
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.
ExceptionBehavior
Exception behavior used for floating point operations.
@ ebStrict
This corresponds to "fpexcept.strict".
@ ebIgnore
This corresponds to "fpexcept.ignore".
This is an optimization pass for GlobalISel generic memory operations.
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
Value * simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a AShr, fold the result or return nulll.
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Value * simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FMul, fold the result or return null.
Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
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,...
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
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.
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
Value * simplifySDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for an SDiv, fold the result or return null.
Value * simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q)
Given operand for a UnaryOperator, fold the result or return null.
bool isDefaultFPEnvironment(fp::ExceptionBehavior EB, RoundingMode RM)
Returns true if the exception handling behavior and rounding mode match what is used in the default f...
Value * simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Mul, fold the result or return null.
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Value * simplifyCall(CallBase *Call, Value *Callee, ArrayRef< Value * > Args, const SimplifyQuery &Q)
Given a callsite, callee, and arguments, fold the result or return null.
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
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 canRoundingModeBe(RoundingMode RM, RoundingMode QRM)
Returns true if the rounding mode RM may be QRM at compile time or at run time.
bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
Value * simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q)
Given operands for an FCmpInst, fold the result or return null.
Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
Value * simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef< int > Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
Value * simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Or, fold the result or return null.
Value * simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an Xor, fold the result or return null.
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.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
unsigned M1(unsigned Val)
Value * simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Sub, fold the result or return null.
Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, SmallSetVector< Instruction *, 8 > *UnsimplifiedUsers=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
SelectPatternFlavor
Specific patterns of select instructions we can match.
Value * simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for a Shl, fold the result or return null.
Value * simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q)
Given operand for an FNeg, fold the result or return null.
Value * simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FSub, fold the result or return null.
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
Value * simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FRem, fold the result or return null.
Value * simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FAdd, fold the result or return null.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
Value * simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact, const SimplifyQuery &Q)
Given operands for a LShr, fold the result or return null.
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Value * simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an ICmpInst, fold the result or return null.
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 * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Value * simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an And, fold the result or return null.
Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
Value * simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an InsertValueInst, fold the result or return null.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
Value * simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for an FDiv, fold the result or return null.
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
Value * simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q)
Given a load instruction and its pointer operand, fold the result or return null.
Value * simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF, const SimplifyQuery &Q, fp::ExceptionBehavior ExBehavior=fp::ebIgnore, RoundingMode Rounding=RoundingMode::NearestTiesToEven)
Given operands for the multiplication of a FMA, fold the result or return null.
Value * simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q)
Given a constrained FP intrinsic call, tries to compute its simplified version.
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
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.
@ Or
Bitwise or logical OR of integers.
std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register,...
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...
Value * simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact, const SimplifyQuery &Q)
Given operands for a UDiv, fold the result or return null.
DWARFExpression::Operation Op
Value * simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, Value *Op0, Value *Op1, const SimplifyQuery &Q, const CallBase *Call)
Given operands for a BinaryIntrinsic, fold the result or return null.
RoundingMode
Rounding mode.
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.
unsigned M0(unsigned Val)
Value * simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
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...
Value * simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const SimplifyQuery &Q, bool AllowRefinement, SmallVectorImpl< Instruction * > *DropFlags=nullptr)
See if V simplifies when its operand Op is replaced with RepOp.
bool maskIsAllZeroOrUndef(Value *Mask)
Given a mask vector of i1, Return true if all of the elements of this predicate mask are known to be ...
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 * simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for an SRem, fold the result or return null.
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.
std::optional< bool > computeKnownFPSignBit(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return false if we can prove that the specified FP value's sign bit is 0.
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.
bool cannotBeNegativeZero(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if we can prove that the specified FP value is never equal to -0.0.
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
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...
Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
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.
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
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...
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
bool isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd, Use *&Y)
Match one of the patterns up to the select/logic op: Op0 = icmp ne i4 X, 0 Agg = call { i4,...
bool canIgnoreSNaN(fp::ExceptionBehavior EB, FastMathFlags FMF)
Returns true if the possibility of a signaling NaN can be safely ignored.
Value * simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a URem, fold the result or return null.
Value * simplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
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 swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
This callback is used in conjunction with PointerMayBeCaptured.
virtual void tooManyUses()=0
tooManyUses - The depth of traversal has breached a limit.
virtual bool captured(const Use *U)=0
captured - Information about the pointer was captured by the user of use U.
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
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 hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
bool isNonNegative() const
Returns true if this value is known to be non-negative.
bool isZero() const
Returns true if value is all zero.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
bool hasConflict() const
Returns true if there is conflicting information.
unsigned getBitWidth() const
Get the bit width of this value.
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
bool isNegative() const
Returns true if this value is known to be negative.
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).
bool isKnownAlwaysNaN() const
Return true if it's known this must always be a nan.
static constexpr FPClassTest OrderedLessThanZeroMask
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 cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
Mode EvalMode
How we want to evaluate this object's size.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
bool CanUseUndef
Controls whether simplifications are allowed to constrain the range of possible values for uses of un...
SimplifyQuery getWithInstruction(const Instruction *I) const
bool isUndefValue(Value *V) const
If CanUseUndef is true, returns whether V is undef.
const TargetLibraryInfo * TLI
SimplifyQuery getWithoutUndef() const