@@ -1346,6 +1346,8 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
1346
1346
for (unsigned i = 0 ; i != 2 ; ++i) {
1347
1347
Value *L = P->getIncomingValue (i);
1348
1348
Value *R = P->getIncomingValue (!i);
1349
+ Instruction *RInst = P->getIncomingBlock (!i)->getTerminator ();
1350
+ Instruction *LInst = P->getIncomingBlock (i)->getTerminator ();
1349
1351
Operator *LU = dyn_cast<Operator>(L);
1350
1352
if (!LU)
1351
1353
continue ;
@@ -1367,13 +1369,22 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
1367
1369
L = LL;
1368
1370
else
1369
1371
break ;
1372
+
1373
+ // Change the context instruction to the "edge" that flows into the
1374
+ // phi. This is important because that is where the value is actually
1375
+ // "evaluated" even though it is used later somewhere else. (see also
1376
+ // D69571).
1377
+ Query RecQ = Q;
1378
+
1370
1379
// Ok, we have a PHI of the form L op= R. Check for low
1371
1380
// zero bits.
1372
- computeKnownBits (R, Known2, Depth + 1 , Q);
1381
+ RecQ.CxtI = RInst;
1382
+ computeKnownBits (R, Known2, Depth + 1 , RecQ);
1373
1383
1374
1384
// We need to take the minimum number of known bits
1375
1385
KnownBits Known3 (Known);
1376
- computeKnownBits (L, Known3, Depth + 1 , Q);
1386
+ RecQ.CxtI = LInst;
1387
+ computeKnownBits (L, Known3, Depth + 1 , RecQ);
1377
1388
1378
1389
Known.Zero .setLowBits (std::min (Known2.countMinTrailingZeros (),
1379
1390
Known3.countMinTrailingZeros ()));
@@ -1429,14 +1440,22 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
1429
1440
1430
1441
Known.Zero .setAllBits ();
1431
1442
Known.One .setAllBits ();
1432
- for (Value *IncValue : P->incoming_values ()) {
1443
+ for (unsigned u = 0 , e = P->getNumIncomingValues (); u < e; ++u) {
1444
+ Value *IncValue = P->getIncomingValue (u);
1433
1445
// Skip direct self references.
1434
1446
if (IncValue == P) continue ;
1435
1447
1448
+ // Change the context instruction to the "edge" that flows into the
1449
+ // phi. This is important because that is where the value is actually
1450
+ // "evaluated" even though it is used later somewhere else. (see also
1451
+ // D69571).
1452
+ Query RecQ = Q;
1453
+ RecQ.CxtI = P->getIncomingBlock (u)->getTerminator ();
1454
+
1436
1455
Known2 = KnownBits (BitWidth);
1437
1456
// Recurse, but cap the recursion to one level, because we don't
1438
1457
// want to waste time spinning around in loops.
1439
- computeKnownBits (IncValue, Known2, MaxDepth - 1 , Q );
1458
+ computeKnownBits (IncValue, Known2, MaxDepth - 1 , RecQ );
1440
1459
Known.Zero &= Known2.Zero ;
1441
1460
Known.One &= Known2.One ;
1442
1461
// If all bits have been ruled out, there's no need to check
0 commit comments