diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-04-26 17:11:17 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-04-26 17:11:17 +0000 |
commit | d66607bd8cd137e6c90b46ac6d4979c6942ea427 (patch) | |
tree | 70a0aaa54c79b02809ba8d87dcc1a309fc6c11c8 /llvm/test/CodeGen/X86/cmov-into-branch.ll | |
parent | ce36c1f2ec83581d4a83b6f5f012287368a6c951 (diff) | |
download | bcm5719-llvm-d66607bd8cd137e6c90b46ac6d4979c6942ea427.tar.gz bcm5719-llvm-d66607bd8cd137e6c90b46ac6d4979c6942ea427.zip |
[CodeGenPrepare] use branch weight metadata to decide if a select should be turned into a branch
This is part of solving PR27344:
https://llvm.org/bugs/show_bug.cgi?id=27344
CGP should undo the SimplifyCFG transform for the same reason that earlier patches have used this
same mechanism: it's possible that passes between SimplifyCFG and CGP may be able to optimize the
IR further with a select in place.
For the TLI hook default, >99% taken or not taken is chosen as the default threshold for a highly
predictable branch. Even the most limited HW branch predictors will be correct on this branch almost
all the time, so even a massive mispredict penalty perf loss would be overcome by the win from all
the times the branch was predicted correctly.
As a follow-up, we could make the default target hook less conservative by using the SchedMachineModel's
MispredictPenalty. Or we could just let targets override the default by implementing the hook with that
and other target-specific options. Note that trying to statically determine mispredict rates for
close-to-balanced profile weight data is generally impossible if the HW is sufficiently advanced. Ie,
50/50 taken/not-taken might still be 100% predictable.
Finally, note that this patch as-is will not solve PR27344 because the current __builtin_unpredictable()
branch weight default values are 4 and 64. A proposal to change that is in D19435.
Differential Revision: http://reviews.llvm.org/D19488
llvm-svn: 267572
Diffstat (limited to 'llvm/test/CodeGen/X86/cmov-into-branch.ll')
-rw-r--r-- | llvm/test/CodeGen/X86/cmov-into-branch.ll | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/llvm/test/CodeGen/X86/cmov-into-branch.ll b/llvm/test/CodeGen/X86/cmov-into-branch.ll index 99aa4d51429..35c1d3498a5 100644 --- a/llvm/test/CodeGen/X86/cmov-into-branch.ll +++ b/llvm/test/CodeGen/X86/cmov-into-branch.ll @@ -79,13 +79,15 @@ define i32 @weighted_select1(i32 %a, i32 %b) { ret i32 %sel } -; TODO: If a select is obviously predictable, turn it into a branch. +; If a select is obviously predictable, turn it into a branch. define i32 @weighted_select2(i32 %a, i32 %b) { ; CHECK-LABEL: weighted_select2: ; CHECK: # BB#0: ; CHECK-NEXT: testl %edi, %edi -; CHECK-NEXT: cmovnel %edi, %esi -; CHECK-NEXT: movl %esi, %eax +; CHECK-NEXT: jne [[LABEL_BB5:.*]] +; CHECK: movl %esi, %edi +; CHECK-NEXT: [[LABEL_BB5]] +; CHECK-NEXT: movl %edi, %eax ; CHECK-NEXT: retq ; %cmp = icmp ne i32 %a, 0 @@ -93,6 +95,27 @@ define i32 @weighted_select2(i32 %a, i32 %b) { ret i32 %sel } +; Note the reversed profile weights: it doesn't matter if it's +; obviously true or obviously false. +; Either one should become a branch rather than conditional move. +; TODO: But likely true vs. likely false should affect basic block placement? +define i32 @weighted_select3(i32 %a, i32 %b) { +; CHECK-LABEL: weighted_select3: +; CHECK: # BB#0: +; CHECK-NEXT: testl %edi, %edi +; CHECK-NEXT: jne [[LABEL_BB6:.*]] +; CHECK: movl %esi, %edi +; CHECK-NEXT: [[LABEL_BB6]] +; CHECK-NEXT: movl %edi, %eax +; CHECK-NEXT: retq +; + %cmp = icmp ne i32 %a, 0 + %sel = select i1 %cmp, i32 %a, i32 %b, !prof !2 + ret i32 %sel +} + + !0 = !{!"branch_weights", i32 1, i32 99} !1 = !{!"branch_weights", i32 1, i32 100} +!2 = !{!"branch_weights", i32 100, i32 1} |