Skip to content

Commit 6f9b0a7

Browse files
committed
[ValueTracking] Compute knownbits for (and/or cond0, cond1) on both sides of branch
The false branch for `and` and true branch for `or` provide less information (intersection as opposed to union), but still can give some useful information. Closes #82818
1 parent eca0bd1 commit 6f9b0a7

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

llvm/lib/Analysis/DomConditionCache.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ static void findAffectedValues(Value *Cond,
3434
}
3535
};
3636

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

4645
CmpInst::Predicate Pred;
4746
Value *A, *B;
48-
// Only recurse into and/or if it matches the top-level and/or type.
49-
if (TopLevelIsAnd ? match(V, m_LogicalAnd(m_Value(A), m_Value(B)))
50-
: match(V, m_LogicalOr(m_Value(A), m_Value(B)))) {
47+
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
5148
Worklist.push_back(A);
5249
Worklist.push_back(B);
5350
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,17 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,
711711
const SimplifyQuery &SQ, bool Invert) {
712712
Value *A, *B;
713713
if (Depth < MaxAnalysisRecursionDepth &&
714-
(Invert ? match(Cond, m_LogicalOr(m_Value(A), m_Value(B)))
715-
: match(Cond, m_LogicalAnd(m_Value(A), m_Value(B))))) {
716-
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, Invert);
717-
computeKnownBitsFromCond(V, B, Known, Depth + 1, SQ, Invert);
714+
match(Cond, m_LogicalOp(m_Value(A), m_Value(B)))) {
715+
KnownBits Known2(Known.getBitWidth());
716+
KnownBits Known3(Known.getBitWidth());
717+
computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
718+
computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
719+
if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
720+
: match(Cond, m_LogicalAnd(m_Value(), m_Value())))
721+
Known2 = Known2.unionWith(Known3);
722+
else
723+
Known2 = Known2.intersectWith(Known3);
724+
Known = Known.unionWith(Known2);
718725
}
719726

720727
if (auto *Cmp = dyn_cast<ICmpInst>(Cond))

llvm/test/Transforms/InstCombine/known-bits.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ define i8 @test_cond_and_bothways(i8 %x) {
136136
; CHECK-NEXT: [[OR1:%.*]] = or i8 [[X]], -4
137137
; CHECK-NEXT: ret i8 [[OR1]]
138138
; CHECK: exit:
139-
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -4
140-
; CHECK-NEXT: ret i8 [[OR2]]
139+
; CHECK-NEXT: ret i8 -4
141140
;
142141
%and = and i8 %x, 91
143142
%cmp0 = icmp ne i8 %and, 24
@@ -162,8 +161,7 @@ define i8 @test_cond_or_bothways(i8 %x) {
162161
; CHECK-NEXT: [[COND:%.*]] = or i1 [[CMP0]], [[CMP1]]
163162
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
164163
; CHECK: if:
165-
; CHECK-NEXT: [[OR1:%.*]] = or i8 [[X]], -4
166-
; CHECK-NEXT: ret i8 [[OR1]]
164+
; CHECK-NEXT: ret i8 -4
167165
; CHECK: exit:
168166
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -4
169167
; CHECK-NEXT: ret i8 [[OR2]]

0 commit comments

Comments
 (0)