Skip to content

Commit c1df47e

Browse files
committed
ValueTracking: integrate udiv/urem with shift code
1 parent 5d702a1 commit c1df47e

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,16 +1427,23 @@ static void computeKnownBitsFromOperator(const Operator *I,
14271427
// this is sufficient to catch some interesting cases.
14281428
unsigned Opcode = BO->getOpcode();
14291429

1430-
// If this is a shift recurrence, we know the bits being shifted in.
1431-
// We can combine that with information about the start value of the
1432-
// recurrence to conclude facts about the result.
14331430
switch (Opcode) {
1431+
// If this is a shift recurrence, we know the bits being shifted in. We
1432+
// can combine that with information about the start value of the
1433+
// recurrence to conclude facts about the result. If this is a udiv
1434+
// recurrence, we know that the result can never exceed either the
1435+
// numerator or the start value, whichever is greater.
14341436
case Instruction::LShr:
14351437
case Instruction::AShr:
1436-
case Instruction::Shl: {
1438+
case Instruction::Shl:
1439+
case Instruction::UDiv:
14371440
if (BO->getOperand(0) != I)
14381441
break;
1442+
[[fallthrough]];
14391443

1444+
// For a urem recurrence, the result can never exceed the start value. The
1445+
// start value could either be the numerator or the denominator.
1446+
case Instruction::URem: {
14401447
// We have matched a recurrence of the form:
14411448
// %iv = [R, %entry], [%iv.next, %backedge]
14421449
// %iv.next = shift_op %iv, L
@@ -1454,8 +1461,10 @@ static void computeKnownBitsFromOperator(const Operator *I,
14541461
Known.Zero.setLowBits(Known2.countMinTrailingZeros());
14551462
break;
14561463
case Instruction::LShr:
1457-
// A lshr recurrence will preserve the leading zeros of the
1458-
// start value
1464+
case Instruction::UDiv:
1465+
case Instruction::URem:
1466+
// lshr, udiv, and urem recurrences will preserve the leading zeros of
1467+
// the start value.
14591468
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
14601469
break;
14611470
case Instruction::AShr:
@@ -1544,27 +1553,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
15441553
break;
15451554
}
15461555

1547-
case Instruction::UDiv:
1548-
// For UDiv, the result can never exceed either the numerator, or the
1549-
// start value, whichever is greater. The case where the PHI is not
1550-
// the numerator of the UDiv is already handled by other code.
1551-
if (BO->getOperand(0) != P)
1552-
break;
1553-
[[fallthrough]];
1554-
1555-
case Instruction::URem: {
1556-
// For URem, the result can never exceed the start value.
1557-
SimplifyQuery RecQ = Q.getWithoutCondContext();
1558-
1559-
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1560-
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1561-
1562-
RecQ.CxtI = RInst;
1563-
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1564-
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1565-
break;
1566-
}
1567-
15681556
default:
15691557
break;
15701558
}

0 commit comments

Comments
 (0)