Skip to content

Commit d5a8743

Browse files
committed
[ConstraintElim] Extend checkOrAndOpImpliedByOther to handle and/or expr trees
1 parent e48a70e commit d5a8743

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,18 +1511,29 @@ static bool checkOrAndOpImpliedByOther(
15111511
if (OtherOpIdx != 0 && isa<SelectInst>(JoinOp))
15121512
return false;
15131513

1514-
if (!match(JoinOp->getOperand(OtherOpIdx),
1515-
m_ICmp(Pred, m_Value(A), m_Value(B))))
1516-
return false;
1517-
1518-
// For OR, check if the negated condition implies CmpToCheck.
1519-
bool IsOr = match(JoinOp, m_LogicalOr());
1520-
if (IsOr)
1521-
Pred = CmpInst::getInversePredicate(Pred);
1522-
15231514
// Optimistically add fact from first condition.
15241515
unsigned OldSize = DFSInStack.size();
1525-
Info.addFact(Pred, A, B, CB.NumIn, CB.NumOut, DFSInStack);
1516+
// For OR, check if the negated condition implies CmpToCheck.
1517+
bool IsOr = match(JoinOp, m_LogicalOr());
1518+
SmallVector<Value *, 4> Worklist({JoinOp->getOperand(OtherOpIdx)});
1519+
while (!Worklist.empty()) {
1520+
Value *Val = Worklist.pop_back_val();
1521+
Value *LHS, *RHS;
1522+
ICmpInst::Predicate Pred;
1523+
if (match(Val, m_ICmp(Pred, m_Value(LHS), m_Value(RHS)))) {
1524+
if (IsOr)
1525+
Pred = CmpInst::getInversePredicate(Pred);
1526+
Info.addFact(Pred, LHS, RHS, CB.NumIn, CB.NumOut, DFSInStack);
1527+
continue;
1528+
}
1529+
if (IsOr ? match(Val, m_LogicalOr(m_Value(LHS), m_Value(RHS)))
1530+
: match(Val, m_LogicalAnd(m_Value(LHS), m_Value(RHS)))) {
1531+
Worklist.push_back(LHS);
1532+
Worklist.push_back(RHS);
1533+
continue;
1534+
}
1535+
return false;
1536+
}
15261537
if (OldSize == DFSInStack.size())
15271538
return false;
15281539

llvm/test/Transforms/ConstraintElimination/or.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ define i1 @test_or_chain_ule_1(i4 %x, i4 %y, i4 %z, i4 %a, i4 %b) {
124124
; CHECK-NEXT: [[C_3:%.*]] = icmp ule i4 2, [[X]]
125125
; CHECK-NEXT: [[C_4:%.*]] = icmp ule i4 2, [[A:%.*]]
126126
; CHECK-NEXT: [[OR_1:%.*]] = or i1 [[C_1]], [[C_2]]
127-
; CHECK-NEXT: [[OR_2:%.*]] = or i1 [[OR_1]], [[C_3]]
127+
; CHECK-NEXT: [[OR_2:%.*]] = or i1 [[OR_1]], true
128128
; CHECK-NEXT: [[OR_3:%.*]] = or i1 [[C_4]], [[OR_2]]
129129
; CHECK-NEXT: br i1 [[OR_3]], label [[BB1:%.*]], label [[EXIT:%.*]]
130130
; CHECK: bb1:

llvm/test/Transforms/ConstraintElimination/unreachable-bb.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define void @f(i32 noundef %v0, i32 noundef %v1, i32 noundef %v2) {
99
; CHECK-NEXT: [[CMP1:%.*]] = icmp sge i32 [[V1]], [[V2]]
1010
; CHECK-NEXT: [[AND1:%.*]] = and i1 [[CMP0]], [[CMP1]]
1111
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[V0]], [[V2]]
12-
; CHECK-NEXT: [[AND2:%.*]] = and i1 [[CMP2]], [[AND1]]
12+
; CHECK-NEXT: [[AND2:%.*]] = and i1 false, [[AND1]]
1313
; CHECK-NEXT: br i1 [[AND2]], label %[[IF_THEN:.*]], label %[[RETURN:.*]]
1414
; CHECK: [[IF_THEN]]:
1515
; CHECK-NEXT: call void @side_effect()

0 commit comments

Comments
 (0)