llvm.org GIT mirror llvm / b176a4f
Switch lowering: Take branch weight into account when ordering for fall-through Previously, the code would try to put a fall-through case last, even if that meant moving a case with much higher branch weight further down the chain. Ordering by branch weight is most important, putting a fall-through block last is secondary. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235942 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 5 years ago
5 changed file(s) with 40 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
76777677 return a.Weight > b.Weight;
76787678 });
76797679
7680 // Rearrange the case blocks so that the last one falls through if possible.
7681 // Start at the bottom as that's the case with the lowest weight.
7682 // FIXME: Take branch probability into account.
7680 // Rearrange the case blocks so that the last one falls through if possible
7681 // without without changing the order of weights.
76837682 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
76847683 --I;
7684 if (I->Weight > W.LastCluster->Weight)
7685 break;
76857686 if (I->Kind == CC_Range && I->MBB == NextMBB) {
76867687 std::swap(*I, *W.LastCluster);
76877688 break;
4848 ; V8-NEXT: beq
4949 ; V8-NEXT: %tailrecurse.switch
5050 ; V8: cmp
51 ; V8-NEXT: beq
51 ; V8-NEXT: bne
5252 ; V8-NEXT: b
5353 ; The trailing space in the last line checks that the branch is unconditional
5454 switch i32 %and, label %sw.epilog [
5555 i32 1, label %sw.bb
5656 i32 3, label %sw.bb6
5757 i32 2, label %sw.bb8
58 ]
58 ], !prof !1
5959
6060 sw.bb: ; preds = %tailrecurse.switch, %tailrecurse
6161 %shl = shl i32 %acc.tr, 1
133133 return: ; preds = %bb2, %bb, %entry
134134 ret i8 1
135135 }
136
137 !1 = !{!"branch_weights", i32 1, i32 1, i32 3, i32 2 }
1616 ; CHECK: BB#0: derived from LLVM BB %entry
1717 ; CHECK: Successors according to CFG: BB#2(64) BB#4(14)
1818 ; CHECK: BB#4: derived from LLVM BB %entry
19 ; CHECK: Successors according to CFG: BB#1(4) BB#5(10)
19 ; CHECK: Successors according to CFG: BB#1(10) BB#5(4)
2020 ; CHECK: BB#5: derived from LLVM BB %entry
21 ; CHECK: Successors according to CFG: BB#1(10) BB#3(7)
21 ; CHECK: Successors according to CFG: BB#1(4) BB#3(7)
2222
2323 sw.bb:
2424 br label %return
2929 if.end:
3030 switch i64 undef, label %if.end25 [
3131 i64 0, label %if.then4
32 i64 1, label %land.lhs.true14
32 i64 1, label %if.end25
3333 ]
3434
3535 if.then4:
412412 i32 4294967295, i32 2, i32 4294967295,
413413 ; Cases 2,5,8,9:
414414 i32 3, i32 3, i32 3, i32 3}
415
416 define void @order_by_weight_and_fallthrough(i32 %x) {
417 entry:
418 switch i32 %x, label %return [
419 i32 100, label %bb1
420 i32 200, label %bb0
421 i32 300, label %bb0
422 ], !prof !2
423 bb0: tail call void @g(i32 0) br label %return
424 bb1: tail call void @g(i32 1) br label %return
425 return: ret void
426
427 ; Case 200 has the highest weight and should come first. 100 and 300 have the
428 ; same weight, but 300 goes to the 'next' block, so should be last.
429 ; CHECK-LABEL: order_by_weight_and_fallthrough
430 ; CHECK: cmpl $200
431 ; CHECK: cmpl $100
432 ; CHECK: cmpl $300
433 }
434
435 !2 = !{!"branch_weights",
436 ; Default:
437 i32 1,
438 ; Case 100:
439 i32 10,
440 ; Case 200:
441 i32 1000,
442 ; Case 300:
443 i32 10}