Skip to content

Commit ebd3aeb

Browse files
committed
Use helper introduced in 8020be0 to simplify ValueTracking [NFC]
Direct rewrite of the code the helper was extracted from.
1 parent e4dd614 commit ebd3aeb

File tree

2 files changed

+106
-127
lines changed

2 files changed

+106
-127
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
755755
/// %inc = binop %iv, %step
756756
/// NOTE: This is intentional simple. If you want the ability to analyze
757757
/// non-trivial loop conditons, see ScalarEvolution instead.
758-
bool matchSimpleRecurrence(PHINode *P, BinaryOperator *&BO,
758+
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
759759
Value *&Start, Value *&Step);
760760

761761
/// Return true if RHS is known to be implied true by LHS. Return false if

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 105 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,136 +1371,115 @@ static void computeKnownBitsFromOperator(const Operator *I,
13711371
}
13721372
case Instruction::PHI: {
13731373
const PHINode *P = cast<PHINode>(I);
1374-
// Handle the case of a simple two-predecessor recurrence PHI.
1375-
// There's a lot more that could theoretically be done here, but
1376-
// this is sufficient to catch some interesting cases.
1377-
if (P->getNumIncomingValues() == 2) {
1378-
for (unsigned i = 0; i != 2; ++i) {
1379-
Value *L = P->getIncomingValue(i);
1380-
Value *R = P->getIncomingValue(!i);
1381-
Instruction *RInst = P->getIncomingBlock(!i)->getTerminator();
1382-
Instruction *LInst = P->getIncomingBlock(i)->getTerminator();
1383-
Operator *LU = dyn_cast<Operator>(L);
1384-
if (!LU)
1385-
continue;
1386-
unsigned Opcode = LU->getOpcode();
1387-
1388-
1389-
// If this is a shift recurrence, we know the bits being shifted in.
1390-
// We can combine that with information about the start value of the
1391-
// recurrence to conclude facts about the result.
1392-
if (Opcode == Instruction::LShr ||
1393-
Opcode == Instruction::AShr ||
1394-
Opcode == Instruction::Shl) {
1395-
Value *LL = LU->getOperand(0);
1396-
Value *LR = LU->getOperand(1);
1397-
// Find a recurrence.
1398-
if (LL == I)
1399-
L = LR;
1400-
else
1401-
continue; // Check for recurrence with L and R flipped.
1402-
1403-
// We have matched a recurrence of the form:
1404-
// %iv = [R, %entry], [%iv.next, %backedge]
1405-
// %iv.next = shift_op %iv, L
1406-
1407-
// Recurse with the phi context to avoid concern about whether facts
1408-
// inferred hold at original context instruction. TODO: It may be
1409-
// correct to use the original context. IF warranted, explore and
1410-
// add sufficient tests to cover.
1411-
Query RecQ = Q;
1412-
RecQ.CxtI = P;
1413-
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1414-
switch (Opcode) {
1415-
case Instruction::Shl:
1416-
// A shl recurrence will only increase the tailing zeros
1417-
Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1418-
break;
1419-
case Instruction::LShr:
1420-
// A lshr recurrence will preserve the leading zeros of the
1421-
// start value
1422-
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1423-
break;
1424-
case Instruction::AShr:
1425-
// An ashr recurrence will extend the initial sign bit
1426-
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1427-
Known.One.setHighBits(Known2.countMinLeadingOnes());
1428-
break;
1429-
};
1430-
}
1374+
BinaryOperator *BO = nullptr;
1375+
Value *R = nullptr, *L = nullptr;
1376+
if (matchSimpleRecurrence(P, BO, R, L)) {
1377+
// Handle the case of a simple two-predecessor recurrence PHI.
1378+
// There's a lot more that could theoretically be done here, but
1379+
// this is sufficient to catch some interesting cases.
1380+
unsigned Opcode = BO->getOpcode();
1381+
1382+
// If this is a shift recurrence, we know the bits being shifted in.
1383+
// We can combine that with information about the start value of the
1384+
// recurrence to conclude facts about the result.
1385+
if (Opcode == Instruction::LShr ||
1386+
Opcode == Instruction::AShr ||
1387+
Opcode == Instruction::Shl) {
1388+
1389+
// We have matched a recurrence of the form:
1390+
// %iv = [R, %entry], [%iv.next, %backedge]
1391+
// %iv.next = shift_op %iv, L
1392+
1393+
// Recurse with the phi context to avoid concern about whether facts
1394+
// inferred hold at original context instruction. TODO: It may be
1395+
// correct to use the original context. IF warranted, explore and
1396+
// add sufficient tests to cover.
1397+
Query RecQ = Q;
1398+
RecQ.CxtI = P;
1399+
computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1400+
switch (Opcode) {
1401+
case Instruction::Shl:
1402+
// A shl recurrence will only increase the tailing zeros
1403+
Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1404+
break;
1405+
case Instruction::LShr:
1406+
// A lshr recurrence will preserve the leading zeros of the
1407+
// start value
1408+
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1409+
break;
1410+
case Instruction::AShr:
1411+
// An ashr recurrence will extend the initial sign bit
1412+
Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1413+
Known.One.setHighBits(Known2.countMinLeadingOnes());
1414+
break;
1415+
};
1416+
}
14311417

1432-
// Check for operations that have the property that if
1433-
// both their operands have low zero bits, the result
1434-
// will have low zero bits.
1435-
if (Opcode == Instruction::Add ||
1436-
Opcode == Instruction::Sub ||
1437-
Opcode == Instruction::And ||
1438-
Opcode == Instruction::Or ||
1439-
Opcode == Instruction::Mul) {
1440-
Value *LL = LU->getOperand(0);
1441-
Value *LR = LU->getOperand(1);
1442-
// Find a recurrence.
1443-
if (LL == I)
1444-
L = LR;
1445-
else if (LR == I)
1446-
L = LL;
1447-
else
1448-
continue; // Check for recurrence with L and R flipped.
1449-
1450-
// Change the context instruction to the "edge" that flows into the
1451-
// phi. This is important because that is where the value is actually
1452-
// "evaluated" even though it is used later somewhere else. (see also
1453-
// D69571).
1454-
Query RecQ = Q;
1455-
1456-
// Ok, we have a PHI of the form L op= R. Check for low
1457-
// zero bits.
1458-
RecQ.CxtI = RInst;
1459-
computeKnownBits(R, Known2, Depth + 1, RecQ);
1460-
1461-
// We need to take the minimum number of known bits
1462-
KnownBits Known3(BitWidth);
1463-
RecQ.CxtI = LInst;
1464-
computeKnownBits(L, Known3, Depth + 1, RecQ);
1465-
1466-
Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1467-
Known3.countMinTrailingZeros()));
1468-
1469-
auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU);
1470-
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1471-
// If initial value of recurrence is nonnegative, and we are adding
1472-
// a nonnegative number with nsw, the result can only be nonnegative
1473-
// or poison value regardless of the number of times we execute the
1474-
// add in phi recurrence. If initial value is negative and we are
1475-
// adding a negative number with nsw, the result can only be
1476-
// negative or poison value. Similar arguments apply to sub and mul.
1477-
//
1478-
// (add non-negative, non-negative) --> non-negative
1479-
// (add negative, negative) --> negative
1480-
if (Opcode == Instruction::Add) {
1481-
if (Known2.isNonNegative() && Known3.isNonNegative())
1482-
Known.makeNonNegative();
1483-
else if (Known2.isNegative() && Known3.isNegative())
1484-
Known.makeNegative();
1485-
}
1486-
1487-
// (sub nsw non-negative, negative) --> non-negative
1488-
// (sub nsw negative, non-negative) --> negative
1489-
else if (Opcode == Instruction::Sub && LL == I) {
1490-
if (Known2.isNonNegative() && Known3.isNegative())
1491-
Known.makeNonNegative();
1492-
else if (Known2.isNegative() && Known3.isNonNegative())
1493-
Known.makeNegative();
1494-
}
1495-
1496-
// (mul nsw non-negative, non-negative) --> non-negative
1497-
else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1498-
Known3.isNonNegative())
1418+
// Check for operations that have the property that if
1419+
// both their operands have low zero bits, the result
1420+
// will have low zero bits.
1421+
if (Opcode == Instruction::Add ||
1422+
Opcode == Instruction::Sub ||
1423+
Opcode == Instruction::And ||
1424+
Opcode == Instruction::Or ||
1425+
Opcode == Instruction::Mul) {
1426+
// Change the context instruction to the "edge" that flows into the
1427+
// phi. This is important because that is where the value is actually
1428+
// "evaluated" even though it is used later somewhere else. (see also
1429+
// D69571).
1430+
Query RecQ = Q;
1431+
1432+
unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1433+
Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1434+
Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator();
1435+
1436+
// Ok, we have a PHI of the form L op= R. Check for low
1437+
// zero bits.
1438+
RecQ.CxtI = RInst;
1439+
computeKnownBits(R, Known2, Depth + 1, RecQ);
1440+
1441+
// We need to take the minimum number of known bits
1442+
KnownBits Known3(BitWidth);
1443+
RecQ.CxtI = LInst;
1444+
computeKnownBits(L, Known3, Depth + 1, RecQ);
1445+
1446+
Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1447+
Known3.countMinTrailingZeros()));
1448+
1449+
auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1450+
if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1451+
// If initial value of recurrence is nonnegative, and we are adding
1452+
// a nonnegative number with nsw, the result can only be nonnegative
1453+
// or poison value regardless of the number of times we execute the
1454+
// add in phi recurrence. If initial value is negative and we are
1455+
// adding a negative number with nsw, the result can only be
1456+
// negative or poison value. Similar arguments apply to sub and mul.
1457+
//
1458+
// (add non-negative, non-negative) --> non-negative
1459+
// (add negative, negative) --> negative
1460+
if (Opcode == Instruction::Add) {
1461+
if (Known2.isNonNegative() && Known3.isNonNegative())
14991462
Known.makeNonNegative();
1463+
else if (Known2.isNegative() && Known3.isNegative())
1464+
Known.makeNegative();
15001465
}
15011466

1502-
break;
1467+
// (sub nsw non-negative, negative) --> non-negative
1468+
// (sub nsw negative, non-negative) --> negative
1469+
else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
1470+
if (Known2.isNonNegative() && Known3.isNegative())
1471+
Known.makeNonNegative();
1472+
else if (Known2.isNegative() && Known3.isNonNegative())
1473+
Known.makeNegative();
1474+
}
1475+
1476+
// (mul nsw non-negative, non-negative) --> non-negative
1477+
else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1478+
Known3.isNonNegative())
1479+
Known.makeNonNegative();
15031480
}
1481+
1482+
break;
15041483
}
15051484
}
15061485

@@ -6053,7 +6032,7 @@ llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
60536032
return {Intrinsic::not_intrinsic, false};
60546033
}
60556034

6056-
bool llvm::matchSimpleRecurrence(PHINode *P, BinaryOperator *&BO,
6035+
bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
60576036
Value *&Start, Value *&Step) {
60586037
// Handle the case of a simple two-predecessor recurrence PHI.
60596038
// There's a lot more that could theoretically be done here, but

0 commit comments

Comments
 (0)