From ab85dcc7b8d14edf8f2e716850739cfeb5bf0ccd Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Thu, 13 Nov 2014 17:58:51 +0000 Subject: [PATCH] ARM: avoid duplicating branches during constant islands. We were using a naive heuristic to determine whether a basic block already had an unconditional branch at the end. This mostly corresponded to reality (assuming branches got optimised) because there's not much point in a branch to the next block, but could go wrong. llvm-svn: 221904 --- llvm/lib/Target/ARM/ARMConstantIslandPass.cpp | 16 ++++++++++------ llvm/test/CodeGen/ARM/constant-islands.ll | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 llvm/test/CodeGen/ARM/constant-islands.ll diff --git a/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp b/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp index fa9bfa2..1d3ca1a 100644 --- a/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp +++ b/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp @@ -275,6 +275,7 @@ namespace { private: void doInitialPlacement(std::vector &CPEMIs); + bool BBHasFallthrough(MachineBasicBlock *MBB); CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); unsigned getCPELogAlign(const MachineInstr *CPEMI); void scanFunctionJumpTables(); @@ -566,7 +567,7 @@ ARMConstantIslands::doInitialPlacement(std::vector &CPEMIs) { /// BBHasFallthrough - Return true if the specified basic block can fallthrough /// into the block immediately after it. -static bool BBHasFallthrough(MachineBasicBlock *MBB) { +bool ARMConstantIslands::BBHasFallthrough(MachineBasicBlock *MBB) { // Get the next machine basic block in the function. MachineFunction::iterator MBBI = MBB; // Can't fall off end of function. @@ -574,12 +575,15 @@ static bool BBHasFallthrough(MachineBasicBlock *MBB) { return false; MachineBasicBlock *NextBB = std::next(MBBI); - for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), - E = MBB->succ_end(); I != E; ++I) - if (*I == NextBB) - return true; + if (std::find(MBB->succ_begin(), MBB->succ_end(), NextBB) == MBB->succ_end()) + return false; - return false; + // Try to analyze the end of the block. A potential fallthrough may already + // have an unconditional branch for whatever reason. + MachineBasicBlock *TBB, *FBB; + SmallVector Cond; + bool TooDifficult = TII->AnalyzeBranch(*MBB, TBB, FBB, Cond); + return TooDifficult || FBB == nullptr; } /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, diff --git a/llvm/test/CodeGen/ARM/constant-islands.ll b/llvm/test/CodeGen/ARM/constant-islands.ll new file mode 100644 index 0000000..afa4b85 --- /dev/null +++ b/llvm/test/CodeGen/ARM/constant-islands.ll @@ -0,0 +1,25 @@ +; RUN: llc -mtriple=thumbv7-linux-gnueabihf -O0 -fast-isel=0 -o - %s | FileCheck %s + +define void @test_no_duplicate_branches(float %in) { +; CHECK-LABEL: test_no_duplicate_branches: +; CHECK: vldr {{s[0-9]+}}, [[CONST:\.LCPI[0-9]+_[0-9]+]] +; CHECK: b .LBB +; CHECK-NOT: b .LBB +; CHECK: [[CONST]]: +; CHECK-NEXT: .long 1150963712 + + %tst = fcmp oeq float %in, 1234.5 + + %chain = zext i1 %tst to i32 + + br i1 %tst, label %true, label %false + +true: + call i32 @llvm.arm.space(i32 2000, i32 undef) + ret void + +false: + ret void +} + +declare i32 @llvm.arm.space(i32, i32) -- 2.7.4