From 5a4ca8b550afea96c0539083b470f87b40226688 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Mon, 16 Nov 2020 21:44:13 +0000 Subject: [PATCH] [ConstraintElimination] Add support for Or. When processing conditional branches, if the condition is an OR of 2 compares and the false successor only has the current block as predecessor, queue both negated conditions for the false successor --- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 16 ++++++++++++++++ llvm/test/Transforms/ConstraintElimination/or.ll | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index 714fae9..c43cb39 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -166,6 +166,22 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { auto *Br = dyn_cast(BB.getTerminator()); if (!Br || !Br->isConditional()) continue; + + // If the condition is an OR of 2 compares and the false successor only has + // the current block as predecessor, queue both negated conditions for the + // false successor. + if (match(Br->getCondition(), m_Or(m_Cmp(), m_Cmp()))) { + BasicBlock *FalseSuccessor = Br->getSuccessor(1); + if (FalseSuccessor->getSinglePredecessor()) { + auto *OrI = cast(Br->getCondition()); + WorkList.emplace_back(DT.getNode(FalseSuccessor), + cast(OrI->getOperand(0)), true); + WorkList.emplace_back(DT.getNode(FalseSuccessor), + cast(OrI->getOperand(1)), true); + } + continue; + } + auto *CmpI = dyn_cast(Br->getCondition()); if (!CmpI) continue; diff --git a/llvm/test/Transforms/ConstraintElimination/or.ll b/llvm/test/Transforms/ConstraintElimination/or.ll index 400fedd..31a1803 100644 --- a/llvm/test/Transforms/ConstraintElimination/or.ll +++ b/llvm/test/Transforms/ConstraintElimination/or.ll @@ -18,15 +18,15 @@ define i32 @test_or_ule(i32 %x, i32 %y, i32 %z, i32 %a) { ; CHECK-NEXT: ret i32 10 ; CHECK: exit: ; CHECK-NEXT: [[F_1:%.*]] = icmp ule i32 [[X]], [[Z]] -; CHECK-NEXT: call void @use(i1 [[F_1]]) +; CHECK-NEXT: call void @use(i1 false) ; CHECK-NEXT: [[C_5:%.*]] = icmp ule i32 [[X]], [[A]] ; CHECK-NEXT: call void @use(i1 [[C_5]]) ; CHECK-NEXT: [[T_1:%.*]] = icmp ugt i32 [[Y]], [[Z]] -; CHECK-NEXT: call void @use(i1 [[T_1]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[T_2:%.*]] = icmp ugt i32 [[X]], [[Y]] -; CHECK-NEXT: call void @use(i1 [[T_2]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: [[T_3:%.*]] = icmp ugt i32 [[X]], [[Z]] -; CHECK-NEXT: call void @use(i1 [[T_3]]) +; CHECK-NEXT: call void @use(i1 true) ; CHECK-NEXT: ret i32 20 ; entry: -- 2.7.4