Skip to content

Commit c3ffe91

Browse files
committed
Backport of part of D71181.
This backports *just* the bug fix portion of llvm@a7d992c#diff-1f9547b463bf33c1dd86f485d0fa410eR1465
1 parent 9f65ad0 commit c3ffe91

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,16 @@ static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
543543

544544
// Evaluate the BinOp on the incoming phi values.
545545
Value *CommonValue = nullptr;
546-
for (Value *Incoming : PI->incoming_values()) {
546+
for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
547+
Value *Incoming = PI->getIncomingValue(u);
548+
Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
547549
// If the incoming value is the phi node itself, it can safely be skipped.
548550
if (Incoming == PI) continue;
549-
Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q, MaxRecurse);
551+
// Change the context instruction to the "edge" that flows into the phi.
552+
// This is important because that is where incoming is actually "evaluated"
553+
// even though it is used later somewhere else.
554+
Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI),
555+
MaxRecurse);
550556
// If the operation failed to simplify, or simplified to a different value
551557
// to previously, then give up.
552558
if (!V || (CommonValue && V != CommonValue))

llvm/lib/Analysis/ValueTracking.cpp

+23-4
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)