Skip to content

[ValueTracking] Compute knownbits for (and/or cond0, cond1) on both sides of branch #82818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions llvm/lib/Analysis/DomConditionCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ static void findAffectedValues(Value *Cond,
}
};

bool TopLevelIsAnd = match(Cond, m_LogicalAnd());
SmallVector<Value *, 8> Worklist;
SmallPtrSet<Value *, 8> Visited;
Worklist.push_back(Cond);
Expand All @@ -45,9 +44,7 @@ static void findAffectedValues(Value *Cond,

CmpInst::Predicate Pred;
Value *A, *B;
// Only recurse into and/or if it matches the top-level and/or type.
if (TopLevelIsAnd ? match(V, m_LogicalAnd(m_Value(A), m_Value(B)))
: match(V, m_LogicalOr(m_Value(A), m_Value(B)))) {
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
Worklist.push_back(A);
Worklist.push_back(B);
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {
Expand Down
15 changes: 11 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,17 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,
const SimplifyQuery &SQ, bool Invert) {
Value *A, *B;
if (Depth < MaxAnalysisRecursionDepth &&
(Invert ? match(Cond, m_LogicalOr(m_Value(A), m_Value(B)))
: match(Cond, m_LogicalAnd(m_Value(A), m_Value(B))))) {
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, Invert);
computeKnownBitsFromCond(V, B, Known, Depth + 1, SQ, Invert);
match(Cond, m_LogicalOp(m_Value(A), m_Value(B)))) {
KnownBits Known2(Known.getBitWidth());
KnownBits Known3(Known.getBitWidth());
computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
: match(Cond, m_LogicalAnd(m_Value(), m_Value())))
Known2 = Known2.unionWith(Known3);
else
Known2 = Known2.intersectWith(Known3);
Known = Known.unionWith(Known2);
}

if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
Expand Down
59 changes: 59 additions & 0 deletions llvm/test/Transforms/InstCombine/known-bits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,65 @@ exit:
ret i8 %or2
}


define i8 @test_cond_and_bothways(i8 %x) {
; CHECK-LABEL: @test_cond_and_bothways(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], 91
; CHECK-NEXT: [[CMP0:%.*]] = icmp ne i8 [[AND]], 24
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i8 [[X]], 0
; CHECK-NEXT: [[COND:%.*]] = and i1 [[CMP0]], [[CMP1]]
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
; CHECK: if:
; CHECK-NEXT: [[OR1:%.*]] = or i8 [[X]], -4
; CHECK-NEXT: ret i8 [[OR1]]
; CHECK: exit:
; CHECK-NEXT: ret i8 -4
;
%and = and i8 %x, 91
%cmp0 = icmp ne i8 %and, 24
%cmp1 = icmp ne i8 %x, 0
%cond = and i1 %cmp0, %cmp1
br i1 %cond, label %if, label %exit

if:
%or1 = or i8 %x, -4
ret i8 %or1

exit:
%or2 = or i8 %x, -4
ret i8 %or2
}

define i8 @test_cond_or_bothways(i8 %x) {
; CHECK-LABEL: @test_cond_or_bothways(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], 91
; CHECK-NEXT: [[CMP0:%.*]] = icmp eq i8 [[AND]], 24
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X]], 0
; CHECK-NEXT: [[COND:%.*]] = or i1 [[CMP0]], [[CMP1]]
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
; CHECK: if:
; CHECK-NEXT: ret i8 -4
; CHECK: exit:
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -4
; CHECK-NEXT: ret i8 [[OR2]]
;
%and = and i8 %x, 91
%cmp0 = icmp eq i8 %and, 24
%cmp1 = icmp eq i8 %x, 0
%cond = or i1 %cmp0, %cmp1
br i1 %cond, label %if, label %exit

if:
%or1 = or i8 %x, -4
ret i8 %or1

exit:
%or2 = or i8 %x, -4
ret i8 %or2
}



define i8 @test_cond_and_commuted(i8 %x, i1 %c1, i1 %c2) {
; CHECK-LABEL: @test_cond_and_commuted(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], 3
Expand Down