From fb4ffccacb6050a478a18f303bca53cc1ba4f9d6 Mon Sep 17 00:00:00 2001 From: Philip Reames Date: Tue, 10 Mar 2015 22:52:37 +0000 Subject: [PATCH] If a conditional branch jumps to the same target, remove the condition Given that large parts of inst combine is restricted to instructions which have one use, getting rid of a use on the condition can help the effectiveness of the optimizer. Also, it allows the condition to potentially be deleted by instcombine rather than waiting for another pass. I noticed this completely by accident in another test case. It's not anything that actually came from a real workload. p.s. We should probably do the same thing for switch instructions. Differential Revision: http://reviews.llvm.org/D8220 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231881 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstructionCombining.cpp | 9 +++++++++ test/Transforms/InstCombine/branch.ll | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 test/Transforms/InstCombine/branch.ll diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 1fe036b5fbb..434d129bf39 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2002,6 +2002,15 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { return &BI; } + // If the condition is irrelevant, remove the use so that other + // transforms on the condition become more effective. + if (BI.isConditional() && + BI.getSuccessor(0) == BI.getSuccessor(1) && + !isa(BI.getCondition())) { + BI.setCondition(UndefValue::get(BI.getCondition()->getType())); + return &BI; + } + // Canonicalize fcmp_one -> fcmp_oeq FCmpInst::Predicate FPred; Value *Y; if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), diff --git a/test/Transforms/InstCombine/branch.ll b/test/Transforms/InstCombine/branch.ll new file mode 100644 index 00000000000..5be93cc2023 --- /dev/null +++ b/test/Transforms/InstCombine/branch.ll @@ -0,0 +1,16 @@ +; RUN: opt -instcombine -S < %s | FileCheck %s + +define i32 @test(i32 %x) { +; CHECK-LABEL: @test +entry: +; CHECK-NOT: icmp +; CHECK: br i1 undef, + %cmp = icmp ult i32 %x, 7 + br i1 %cmp, label %merge, label %merge +merge: +; CHECK-LABEL: merge: +; CHECK: ret i32 %x + ret i32 %x +} + + -- 2.34.1