From 5bd75b1a3b8fcb83e2e53392bb8f84eb2ebf1368 Mon Sep 17 00:00:00 2001 From: Cong Hou Date: Thu, 27 Aug 2015 00:37:40 +0000 Subject: [PATCH] Fixed a bug that edge weights are not assigned correctly when lowering switch statement. This is a one-line-change patch that moves the update to UnhandledWeights to the correct position: it should be updated for all clusters instead of just range clusters. Differential Revision: http://reviews.llvm.org/D12391 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246129 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../SelectionDAG/SelectionDAGBuilder.cpp | 2 +- test/CodeGen/X86/switch-edge-weight.ll | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/CodeGen/X86/switch-edge-weight.ll diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 9ccd6267492..48811bdea59 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -8033,6 +8033,7 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond, // Put Cond in a virtual register to make it available from the new blocks. ExportFromCurrentBlock(Cond); } + UnhandledWeights -= I->Weight; switch (I->Kind) { case CC_JumpTable: { @@ -8103,7 +8104,6 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond, } // The false weight is the sum of all unhandled cases. - UnhandledWeights -= I->Weight; CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB, I->Weight, UnhandledWeights); diff --git a/test/CodeGen/X86/switch-edge-weight.ll b/test/CodeGen/X86/switch-edge-weight.ll new file mode 100644 index 00000000000..c4cba436ebb --- /dev/null +++ b/test/CodeGen/X86/switch-edge-weight.ll @@ -0,0 +1,41 @@ +; RUN: llc -march=x86-64 -print-machineinstrs=expand-isel-pseudos %s -o /dev/null 2>&1 | FileCheck %s + + +; CHECK-LABEL: test + +define void @test(i32 %x) nounwind { +entry: + switch i32 %x, label %sw.default [ + i32 54, label %sw.bb + i32 55, label %sw.bb + i32 56, label %sw.bb + i32 58, label %sw.bb + i32 67, label %sw.bb + i32 68, label %sw.bb + i32 134, label %sw.bb + i32 140, label %sw.bb + ], !prof !1 + +sw.bb: + call void @foo(i32 0) + br label %sw.epilog + +sw.default: + call void @foo(i32 1) + br label %sw.epilog + +sw.epilog: + ret void + +; Check if weights are correctly assigned to edges generated from switch +; statement. +; +; CHECK: BB#4: +; CHECK: Successors according to CFG: BB#1(10) BB#6(10) +; CHECK: BB#6: +; CHECK: Successors according to CFG: BB#1(10) BB#2(10) +} + +declare void @foo(i32) + +!1 = !{!"branch_weights", i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10, i32 10} -- 2.34.1