@@ -996,8 +996,8 @@ static bool isDeadPHICycle(PHINode *PN,
996
996
// / Return true if this phi node is always equal to NonPhiInVal.
997
997
// / This happens with mutually cyclic phi nodes like:
998
998
// / z = some value; x = phi (y, z); y = phi (x, z)
999
- static bool PHIsEqualValue (PHINode *PN, Value *NonPhiInVal,
1000
- SmallPtrSetImpl<PHINode*> &ValueEqualPHIs) {
999
+ static bool PHIsEqualValue (PHINode *PN, Value *& NonPhiInVal,
1000
+ SmallPtrSetImpl<PHINode *> &ValueEqualPHIs) {
1001
1001
// See if we already saw this PHI node.
1002
1002
if (!ValueEqualPHIs.insert (PN).second )
1003
1003
return true ;
@@ -1010,8 +1010,11 @@ static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
1010
1010
// the value.
1011
1011
for (Value *Op : PN->incoming_values ()) {
1012
1012
if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
1013
- if (!PHIsEqualValue (OpPN, NonPhiInVal, ValueEqualPHIs))
1014
- return false ;
1013
+ if (!PHIsEqualValue (OpPN, NonPhiInVal, ValueEqualPHIs)) {
1014
+ if (NonPhiInVal)
1015
+ return false ;
1016
+ NonPhiInVal = OpPN;
1017
+ }
1015
1018
} else if (Op != NonPhiInVal)
1016
1019
return false ;
1017
1020
}
@@ -1478,33 +1481,35 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
1478
1481
// z = some value; x = phi (y, z); y = phi (x, z)
1479
1482
// where the phi nodes don't necessarily need to be in the same block. Do a
1480
1483
// quick check to see if the PHI node only contains a single non-phi value, if
1481
- // so, scan to see if the phi cycle is actually equal to that value.
1484
+ // so, scan to see if the phi cycle is actually equal to that value. If the
1485
+ // phi has no non-phi values then allow the "NonPhiInVal" to be set later if
1486
+ // one of the phis itself does not have a single input.
1482
1487
{
1483
1488
unsigned InValNo = 0 , NumIncomingVals = PN.getNumIncomingValues ();
1484
1489
// Scan for the first non-phi operand.
1485
1490
while (InValNo != NumIncomingVals &&
1486
1491
isa<PHINode>(PN.getIncomingValue (InValNo)))
1487
1492
++InValNo;
1488
1493
1489
- if (InValNo != NumIncomingVals) {
1490
- Value *NonPhiInVal = PN.getIncomingValue (InValNo);
1494
+ Value *NonPhiInVal =
1495
+ InValNo != NumIncomingVals ? PN.getIncomingValue (InValNo) : nullptr ;
1491
1496
1492
- // Scan the rest of the operands to see if there are any conflicts, if so
1493
- // there is no need to recursively scan other phis.
1497
+ // Scan the rest of the operands to see if there are any conflicts, if so
1498
+ // there is no need to recursively scan other phis.
1499
+ if (NonPhiInVal)
1494
1500
for (++InValNo; InValNo != NumIncomingVals; ++InValNo) {
1495
1501
Value *OpVal = PN.getIncomingValue (InValNo);
1496
1502
if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
1497
1503
break ;
1498
1504
}
1499
1505
1500
- // If we scanned over all operands, then we have one unique value plus
1501
- // phi values. Scan PHI nodes to see if they all merge in each other or
1502
- // the value.
1503
- if (InValNo == NumIncomingVals) {
1504
- SmallPtrSet<PHINode*, 16 > ValueEqualPHIs;
1505
- if (PHIsEqualValue (&PN, NonPhiInVal, ValueEqualPHIs))
1506
- return replaceInstUsesWith (PN, NonPhiInVal);
1507
- }
1506
+ // If we scanned over all operands, then we have one unique value plus
1507
+ // phi values. Scan PHI nodes to see if they all merge in each other or
1508
+ // the value.
1509
+ if (InValNo == NumIncomingVals) {
1510
+ SmallPtrSet<PHINode *, 16 > ValueEqualPHIs;
1511
+ if (PHIsEqualValue (&PN, NonPhiInVal, ValueEqualPHIs))
1512
+ return replaceInstUsesWith (PN, NonPhiInVal);
1508
1513
}
1509
1514
}
1510
1515
0 commit comments