[PATCH] D37940: Peel off the dominant case in switch statement

Rong Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 15:43:03 PDT 2017


xur created this revision.

This patch peels off the dominant case in switch statement into a branch. This avoids the extra compares when lowering the switch statement into chain of branches in binary search tree style.

For a switch statement like the following:

  switch (i) {
  case     8: s1; break;
  case -8826: s2; break;
  case 18312: s3; break;
  case 18568: s4; break;
  case   129: s5; break;
  }


The lowering generates the following coe

1. BB#0:                                 # %entry cmpl    $128, %edi jg      .LBB0_4
2. BB#1:                                 # %entry movl    $1, %eax cmpl    $8, %edi jne     .LBB0_2
3. BB#10:                                # %return retq

.LBB0_4:                                # %entry

  cmpl    $129, %edi

Here the coml to $128 is extra. This is happening even if i==8 target has only non-zero branch-weights. Like !{!"branch_weights", i32 0, i32 100000, i32 0, i32 0, i32 0, i32 0}

This patch would peel i==8 into a branch and keep the rest in the switch statement.


https://reviews.llvm.org/D37940

Files:
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/SimplifyCFG/peel_switch_case.ll
  test/Transforms/SimplifyCFG/preserve-branchweights-switch-create.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37940.115513.patch
Type: text/x-patch
Size: 9976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170915/979a8c81/attachment.bin>


More information about the llvm-commits mailing list