Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -8010,8 +8010,24 @@ // The jump block hasn't been inserted yet; insert it here. MachineBasicBlock *JumpMBB = JT->MBB; CurMF->insert(BBI, JumpMBB); - addSuccessorWithWeight(CurMBB, Fallthrough); - addSuccessorWithWeight(CurMBB, JumpMBB); + + // Collect the sum of weights of outgoing edges from JumpMBB, which will + // be the edge weight on CurMBB->JumpMBB. + uint64_t JumpWeight = 0; + for (auto Succ : JumpMBB->successors()) + JumpWeight += getEdgeWeight(JumpMBB, Succ); + uint32_t FallthruWeight = getEdgeWeight(CurMBB, Fallthrough); + + // Scale down the sum if it cannot fit in 32-bit. + if (JumpWeight > UINT32_MAX) { + uint32_t Scale = JumpWeight / UINT32_MAX + 1; + JumpWeight /= Scale; + FallthruWeight /= Scale; + } + + addSuccessorWithWeight(CurMBB, Fallthrough, FallthruWeight); + addSuccessorWithWeight(CurMBB, JumpMBB, + static_cast(JumpWeight)); // The jump table header will be inserted in our current block, do the // range check, and fall through to our fallthrough block. Index: test/CodeGen/ARM/jump-table-islands.ll =================================================================== --- test/CodeGen/ARM/jump-table-islands.ll +++ test/CodeGen/ARM/jump-table-islands.ll @@ -13,7 +13,7 @@ ; CHECK: .long LBB{{[0-9]+_[0-9]+}}-[[JUMP_TABLE]] ; CHECK: [[SKIP_TABLE]]: -; CHECK: add pc, {{r[0-9]+}}, {{r[0-9]+}} +; CHECK: add pc, lr, {{r[0-9]+}} br i1 %tst, label %simple, label %complex simple: Index: test/CodeGen/Mips/nacl-align.ll =================================================================== --- test/CodeGen/Mips/nacl-align.ll +++ test/CodeGen/Mips/nacl-align.ll @@ -44,18 +44,17 @@ ; CHECK-NEXT: ${{BB[0-9]+_[0-9]+}}: ; CHECK-NEXT: jr $ra ; CHECK-NEXT: addiu $2, $zero, 111 -; CHECK-NEXT: .align 4 ; CHECK-NEXT: ${{BB[0-9]+_[0-9]+}}: ; CHECK-NEXT: jr $ra -; CHECK-NEXT: addiu $2, $zero, 222 +; CHECK-NEXT: addiu $2, $zero, 555 ; CHECK-NEXT: .align 4 ; CHECK-NEXT: ${{BB[0-9]+_[0-9]+}}: ; CHECK-NEXT: jr $ra -; CHECK-NEXT: addiu $2, $zero, 333 +; CHECK-NEXT: addiu $2, $zero, 222 ; CHECK-NEXT: .align 4 ; CHECK-NEXT: ${{BB[0-9]+_[0-9]+}}: ; CHECK-NEXT: jr $ra -; CHECK-NEXT: addiu $2, $zero, 444 +; CHECK-NEXT: addiu $2, $zero, 333 } Index: test/CodeGen/X86/switch-jump-table.ll =================================================================== --- test/CodeGen/X86/switch-jump-table.ll +++ test/CodeGen/X86/switch-jump-table.ll @@ -7,11 +7,11 @@ ; CHECK-LABEL: sum2: ; CHECK: movl 4(%esp), [[REG:%e[a-z]{2}]] ; CHECK: cmpl $3, [[REG]] -; CHECK: jbe .LBB0_1 +; CHECK: ja .LBB0_6 +; CHECK-NEXT: # BB#1: +; CHECK-NEXT: jmpl *.LJTI0_0(,[[REG]],4) ; CHECK: movl $4 ; CHECK: retl -; CHECK-LABEL: .LBB0_1: -; CHECK-NEXT: jmpl *.LJTI0_0(,[[REG]],4) entry: switch i32 %x, label %default [ Index: test/CodeGen/X86/switch.ll =================================================================== --- test/CodeGen/X86/switch.ll +++ test/CodeGen/X86/switch.ll @@ -90,7 +90,7 @@ ; but with 6-8, the whole switch is suitable for a jump table. ; CHECK-LABEL: jt_is_better ; CHECK: cmpl $8 -; CHECK: jbe +; CHECK: ja ; CHECK: jmpq *.LJTI }