@@ -1346,6 +1346,8 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
13461346 for (unsigned i = 0 ; i != 2 ; ++i) {
13471347 Value *L = P->getIncomingValue (i);
13481348 Value *R = P->getIncomingValue (!i);
1349+ Instruction *RInst = P->getIncomingBlock (!i)->getTerminator ();
1350+ Instruction *LInst = P->getIncomingBlock (i)->getTerminator ();
13491351 Operator *LU = dyn_cast<Operator>(L);
13501352 if (!LU)
13511353 continue ;
@@ -1367,13 +1369,22 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
13671369 L = LL;
13681370 else
13691371 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+
13701379 // Ok, we have a PHI of the form L op= R. Check for low
13711380 // zero bits.
1372- computeKnownBits (R, Known2, Depth + 1 , Q);
1381+ RecQ.CxtI = RInst;
1382+ computeKnownBits (R, Known2, Depth + 1 , RecQ);
13731383
13741384 // We need to take the minimum number of known bits
13751385 KnownBits Known3 (Known);
1376- computeKnownBits (L, Known3, Depth + 1 , Q);
1386+ RecQ.CxtI = LInst;
1387+ computeKnownBits (L, Known3, Depth + 1 , RecQ);
13771388
13781389 Known.Zero .setLowBits (std::min (Known2.countMinTrailingZeros (),
13791390 Known3.countMinTrailingZeros ()));
@@ -1429,14 +1440,22 @@ static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
14291440
14301441 Known.Zero .setAllBits ();
14311442 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);
14331445 // Skip direct self references.
14341446 if (IncValue == P) continue ;
14351447
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+
14361455 Known2 = KnownBits (BitWidth);
14371456 // Recurse, but cap the recursion to one level, because we don't
14381457 // want to waste time spinning around in loops.
1439- computeKnownBits (IncValue, Known2, MaxDepth - 1 , Q );
1458+ computeKnownBits (IncValue, Known2, MaxDepth - 1 , RecQ );
14401459 Known.Zero &= Known2.Zero ;
14411460 Known.One &= Known2.One ;
14421461 // If all bits have been ruled out, there's no need to check
0 commit comments