Skip to content

Commit 78089d5

Browse files
authored
ValueTracking: refactor recurrence-matching (NFC) (#109659)
1 parent 296a00b commit 78089d5

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,9 +1430,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
14301430
// If this is a shift recurrence, we know the bits being shifted in.
14311431
// We can combine that with information about the start value of the
14321432
// recurrence to conclude facts about the result.
1433-
if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
1434-
Opcode == Instruction::Shl) &&
1435-
BO->getOperand(0) == I) {
1433+
switch (Opcode) {
1434+
case Instruction::LShr:
1435+
case Instruction::AShr:
1436+
case Instruction::Shl: {
1437+
if (BO->getOperand(0) != I)
1438+
break;
14361439

14371440
// We have matched a recurrence of the form:
14381441
// %iv = [R, %entry], [%iv.next, %backedge]
@@ -1460,17 +1463,18 @@ static void computeKnownBitsFromOperator(const Operator *I,
14601463
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
14611464
Known.One.setHighBits(Known2.countMinLeadingOnes());
14621465
break;
1463-
};
1466+
}
1467+
break;
14641468
}
14651469

14661470
// Check for operations that have the property that if
14671471
// both their operands have low zero bits, the result
14681472
// will have low zero bits.
1469-
if (Opcode == Instruction::Add ||
1470-
Opcode == Instruction::Sub ||
1471-
Opcode == Instruction::And ||
1472-
Opcode == Instruction::Or ||
1473-
Opcode == Instruction::Mul) {
1473+
case Instruction::Add:
1474+
case Instruction::Sub:
1475+
case Instruction::And:
1476+
case Instruction::Or:
1477+
case Instruction::Mul: {
14741478
// Change the context instruction to the "edge" that flows into the
14751479
// phi. This is important because that is where the value is actually
14761480
// "evaluated" even though it is used later somewhere else. (see also
@@ -1495,37 +1499,52 @@ static void computeKnownBitsFromOperator(const Operator *I,
14951499
Known3.countMinTrailingZeros()));
14961500

14971501
auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1498-
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1499-
// If initial value of recurrence is nonnegative, and we are adding
1500-
// a nonnegative number with nsw, the result can only be nonnegative
1501-
// or poison value regardless of the number of times we execute the
1502-
// add in phi recurrence. If initial value is negative and we are
1503-
// adding a negative number with nsw, the result can only be
1504-
// negative or poison value. Similar arguments apply to sub and mul.
1505-
//
1506-
// (add non-negative, non-negative) --> non-negative
1507-
// (add negative, negative) --> negative
1508-
if (Opcode == Instruction::Add) {
1509-
if (Known2.isNonNegative() && Known3.isNonNegative())
1510-
Known.makeNonNegative();
1511-
else if (Known2.isNegative() && Known3.isNegative())
1512-
Known.makeNegative();
1513-
}
1502+
if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1503+
break;
15141504

1515-
// (sub nsw non-negative, negative) --> non-negative
1516-
// (sub nsw negative, non-negative) --> negative
1517-
else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
1518-
if (Known2.isNonNegative() && Known3.isNegative())
1519-
Known.makeNonNegative();
1520-
else if (Known2.isNegative() && Known3.isNonNegative())
1521-
Known.makeNegative();
1522-
}
1505+
switch (Opcode) {
1506+
// If initial value of recurrence is nonnegative, and we are adding
1507+
// a nonnegative number with nsw, the result can only be nonnegative
1508+
// or poison value regardless of the number of times we execute the
1509+
// add in phi recurrence. If initial value is negative and we are
1510+
// adding a negative number with nsw, the result can only be
1511+
// negative or poison value. Similar arguments apply to sub and mul.
1512+
//
1513+
// (add non-negative, non-negative) --> non-negative
1514+
// (add negative, negative) --> negative
1515+
case Instruction::Add: {
1516+
if (Known2.isNonNegative() && Known3.isNonNegative())
1517+
Known.makeNonNegative();
1518+
else if (Known2.isNegative() && Known3.isNegative())
1519+
Known.makeNegative();
1520+
break;
1521+
}
15231522

1524-
// (mul nsw non-negative, non-negative) --> non-negative
1525-
else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1526-
Known3.isNonNegative())
1523+
// (sub nsw non-negative, negative) --> non-negative
1524+
// (sub nsw negative, non-negative) --> negative
1525+
case Instruction::Sub: {
1526+
if (BO->getOperand(0) != I)
1527+
break;
1528+
if (Known2.isNonNegative() && Known3.isNegative())
15271529
Known.makeNonNegative();
1530+
else if (Known2.isNegative() && Known3.isNonNegative())
1531+
Known.makeNegative();
1532+
break;
15281533
}
1534+
1535+
// (mul nsw non-negative, non-negative) --> non-negative
1536+
case Instruction::Mul:
1537+
if (Known2.isNonNegative() && Known3.isNonNegative())
1538+
Known.makeNonNegative();
1539+
break;
1540+
1541+
default:
1542+
break;
1543+
}
1544+
break;
1545+
}
1546+
default:
1547+
break;
15291548
}
15301549
}
15311550

0 commit comments

Comments
 (0)