llvm.org GIT mirror llvm / 89fa06b
Switch-to-lookup tables: set threshold to 3 cases There has been an old FIXME to find the right cut-off for when it's worth analyzing and potentially transforming a switch to a lookup table. The switches always have two or more cases. I could not measure any speed-up by transforming a switch with two cases. A switch with three cases gets a nice speed-up, and I couldn't measure any compile-time regression, so I think this is the right threshold. In a Clang self-host, this causes 480 new switches to be transformed, and reduces the final binary size with 8 KB. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199294 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 6 years ago
2 changed file(s) with 39 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
36833683 // GEP needs a runtime relocation in PIC code. We should just build one big
36843684 // string and lookup indices into that.
36853685
3686 // Ignore the switch if the number of cases is too small.
3687 // This is similar to the check when building jump tables in
3688 // SelectionDAGBuilder::handleJTSwitchCase.
3689 // FIXME: Determine the best cut-off.
3690 if (SI->getNumCases() < 4)
3686 // Ignore switches with less than three cases. Lookup tables will not make them
3687 // faster, so we don't analyze them.
3688 if (SI->getNumCases() < 3)
36913689 return false;
36923690
36933691 // Figure out the corresponding result for each case value and phi node in the
855855 ; CHECK-NOT: @switch.table
856856 ; CHECK: switch i32
857857 }
858
859 ; We build lookup tables for switches with three or more cases.
860 define i32 @threecases(i32 %c) {
861 entry:
862 switch i32 %c, label %sw.default [
863 i32 0, label %return
864 i32 1, label %sw.bb1
865 i32 2, label %sw.bb2
866 ]
867 sw.bb1: br label %return
868 sw.bb2: br label %return
869 sw.default: br label %return
870 return:
871 %x = phi i32 [ 3, %sw.default ], [ 5, %sw.bb2 ], [ 7, %sw.bb1 ], [ 9, %entry ]
872 ret i32 %x
873 ; CHECK-LABEL: @threecases(
874 ; CHECK-NOT: switch i32
875 ; CHECK: @switch.table
876 }
877
878 ; We don't build tables for switches with two cases.
879 define i32 @twocases(i32 %c) {
880 entry:
881 switch i32 %c, label %sw.default [
882 i32 0, label %return
883 i32 1, label %sw.bb1
884 ]
885 sw.bb1: br label %return
886 sw.default: br label %return
887 return:
888 %x = phi i32 [ 3, %sw.default ], [ 7, %sw.bb1 ], [ 9, %entry ]
889 ret i32 %x
890 ; CHECK-LABEL: @twocases(
891 ; CHECK: switch i32
892 ; CHECK-NOT: @switch.table
893 }