Skip to content

Commit 5c8c5b9

Browse files
committed
ValueTracking: refactor recurrence-matching (NFC)
1 parent a861ed4 commit 5c8c5b9

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,9 +1419,12 @@ static void computeKnownBitsFromOperator(const Operator *I,
14191419
// If this is a shift recurrence, we know the bits being shifted in.
14201420
// We can combine that with information about the start value of the
14211421
// recurrence to conclude facts about the result.
1422-
if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
1423-
Opcode == Instruction::Shl) &&
1424-
BO->getOperand(0) == I) {
1422+
switch (Opcode) {
1423+
case Instruction::LShr:
1424+
case Instruction::AShr:
1425+
case Instruction::Shl: {
1426+
if (BO->getOperand(0) != I)
1427+
break;
14251428

14261429
// We have matched a recurrence of the form:
14271430
// %iv = [R, %entry], [%iv.next, %backedge]
@@ -1449,17 +1452,18 @@ static void computeKnownBitsFromOperator(const Operator *I,
14491452
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
14501453
Known.One.setHighBits(Known2.countMinLeadingOnes());
14511454
break;
1452-
};
1455+
}
1456+
break;
14531457
}
14541458

14551459
// Check for operations that have the property that if
14561460
// both their operands have low zero bits, the result
14571461
// will have low zero bits.
1458-
if (Opcode == Instruction::Add ||
1459-
Opcode == Instruction::Sub ||
1460-
Opcode == Instruction::And ||
1461-
Opcode == Instruction::Or ||
1462-
Opcode == Instruction::Mul) {
1462+
case Instruction::Add:
1463+
case Instruction::Sub:
1464+
case Instruction::And:
1465+
case Instruction::Or:
1466+
case Instruction::Mul: {
14631467
// Change the context instruction to the "edge" that flows into the
14641468
// phi. This is important because that is where the value is actually
14651469
// "evaluated" even though it is used later somewhere else. (see also
@@ -1484,38 +1488,51 @@ static void computeKnownBitsFromOperator(const Operator *I,
14841488
Known3.countMinTrailingZeros()));
14851489

14861490
auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1487-
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1488-
// If initial value of recurrence is nonnegative, and we are adding
1489-
// a nonnegative number with nsw, the result can only be nonnegative
1490-
// or poison value regardless of the number of times we execute the
1491-
// add in phi recurrence. If initial value is negative and we are
1492-
// adding a negative number with nsw, the result can only be
1493-
// negative or poison value. Similar arguments apply to sub and mul.
1494-
//
1495-
// (add non-negative, non-negative) --> non-negative
1496-
// (add negative, negative) --> negative
1497-
if (Opcode == Instruction::Add) {
1498-
if (Known2.isNonNegative() && Known3.isNonNegative())
1499-
Known.makeNonNegative();
1500-
else if (Known2.isNegative() && Known3.isNegative())
1501-
Known.makeNegative();
1502-
}
1491+
if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1492+
break;
15031493

1504-
// (sub nsw non-negative, negative) --> non-negative
1505-
// (sub nsw negative, non-negative) --> negative
1506-
else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
1507-
if (Known2.isNonNegative() && Known3.isNegative())
1508-
Known.makeNonNegative();
1509-
else if (Known2.isNegative() && Known3.isNonNegative())
1510-
Known.makeNegative();
1511-
}
1494+
switch (Opcode) {
1495+
// If initial value of recurrence is nonnegative, and we are adding
1496+
// a nonnegative number with nsw, the result can only be nonnegative
1497+
// or poison value regardless of the number of times we execute the
1498+
// add in phi recurrence. If initial value is negative and we are
1499+
// adding a negative number with nsw, the result can only be
1500+
// negative or poison value. Similar arguments apply to sub and mul.
1501+
//
1502+
// (add non-negative, non-negative) --> non-negative
1503+
// (add negative, negative) --> negative
1504+
case Instruction::Add: {
1505+
if (Known2.isNonNegative() && Known3.isNonNegative())
1506+
Known.makeNonNegative();
1507+
else if (Known2.isNegative() && Known3.isNegative())
1508+
Known.makeNegative();
1509+
break;
1510+
}
15121511

1513-
// (mul nsw non-negative, non-negative) --> non-negative
1514-
else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1515-
Known3.isNonNegative())
1512+
// (sub nsw non-negative, negative) --> non-negative
1513+
// (sub nsw negative, non-negative) --> negative
1514+
case Instruction::Sub: {
1515+
if (BO->getOperand(0) != I)
1516+
break;
1517+
if (Known2.isNonNegative() && Known3.isNegative())
15161518
Known.makeNonNegative();
1519+
else if (Known2.isNegative() && Known3.isNonNegative())
1520+
Known.makeNegative();
1521+
break;
15171522
}
15181523

1524+
// (mul nsw non-negative, non-negative) --> non-negative
1525+
case Instruction::Mul:
1526+
if (Known2.isNonNegative() && Known3.isNonNegative())
1527+
Known.makeNonNegative();
1528+
break;
1529+
1530+
default:
1531+
break;
1532+
}
1533+
break;
1534+
}
1535+
default:
15191536
break;
15201537
}
15211538
}

0 commit comments

Comments
 (0)