Skip to content

Commit 1f5a245

Browse files
committed
[ValueTracking] Handle not cond to assume.
1 parent b6be53d commit 1f5a245

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -441,16 +441,28 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
441441
}
442442

443443
static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
444-
SmallVector<const Value *, 16> WorkSet(1, I);
445-
SmallPtrSet<const Value *, 32> Visited;
446-
SmallPtrSet<const Value *, 16> EphValues;
447444

448445
// The instruction defining an assumption's condition itself is always
449446
// considered ephemeral to that assumption (even if it has other
450447
// non-ephemeral users). See r246696's test case for an example.
451448
if (is_contained(I->operands(), E))
452449
return true;
453450

451+
SmallPtrSet<const Value *, 32> Visited{I};
452+
SmallPtrSet<const Value *, 16> EphValues{I};
453+
454+
Value *X;
455+
if (match(I, m_Intrinsic<Intrinsic::assume>(m_Not(m_Value(X))))) {
456+
if (X == E)
457+
return true;
458+
auto *Not = cast<Instruction>(I->getOperand(0));
459+
Visited.insert(Not);
460+
EphValues.insert(Not);
461+
I = Not;
462+
}
463+
464+
SmallVector<const Value *, 16> WorkSet(I->operands());
465+
454466
while (!WorkSet.empty()) {
455467
const Value *V = WorkSet.pop_back_val();
456468
if (!Visited.insert(V).second)
@@ -463,13 +475,11 @@ static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
463475
if (V == E)
464476
return true;
465477

466-
if (V == I || (isa<Instruction>(V) &&
467-
!cast<Instruction>(V)->mayHaveSideEffects() &&
468-
!cast<Instruction>(V)->isTerminator())) {
469-
EphValues.insert(V);
470-
if (const User *U = dyn_cast<User>(V))
471-
append_range(WorkSet, U->operands());
472-
}
478+
if (const auto *II = dyn_cast<Instruction>(V))
479+
if (!II->mayHaveSideEffects() && !II->isTerminator()) {
480+
EphValues.insert(V);
481+
append_range(WorkSet, II->operands());
482+
}
473483
}
474484
}
475485

@@ -10258,11 +10268,8 @@ void llvm::findValuesAffectedByCondition(
1025810268
CmpPredicate Pred;
1025910269
Value *A, *B, *X;
1026010270

10261-
if (IsAssume) {
10271+
if (IsAssume)
1026210272
AddAffected(V);
10263-
if (match(V, m_Not(m_Value(X))))
10264-
AddAffected(X);
10265-
}
1026610273

1026710274
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
1026810275
// assume(A && B) is split to -> assume(A); assume(B);
@@ -10347,8 +10354,7 @@ void llvm::findValuesAffectedByCondition(
1034710354
// Assume is checked here as X is already added above for assumes in
1034810355
// addValueAffectedByCondition
1034910356
AddAffected(X);
10350-
} else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10351-
// Assume is checked here to avoid issues with ephemeral values
10357+
} else if (match(V, m_Not(m_Value(X)))) {
1035210358
Worklist.push_back(X);
1035310359
}
1035410360
}

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,7 @@ define i1 @not_cond_use(i8 %x) {
983983
; CHECK-NEXT: tail call void @use(i1 [[CMP]])
984984
; CHECK-NEXT: [[NOT:%.*]] = xor i1 [[CMP]], true
985985
; CHECK-NEXT: tail call void @llvm.assume(i1 [[NOT]])
986-
; CHECK-NEXT: [[RVAL:%.*]] = icmp eq i8 [[X]], 0
987-
; CHECK-NEXT: ret i1 [[RVAL]]
986+
; CHECK-NEXT: ret i1 false
988987
;
989988
%cmp = icmp eq i8 %x, 0
990989
tail call void @use(i1 %cmp)

0 commit comments

Comments
 (0)