summaryrefslogtreecommitdiff
path: root/test/Transforms/SimplifyCFG
diff options
context:
space:
mode:
authorHans Wennborg <hans@hanshq.net>2014-03-12 18:35:40 +0000
committerHans Wennborg <hans@hanshq.net>2014-03-12 18:35:40 +0000
commit09a31f31545b7360c78265f9ca9cd35430ea401d (patch)
treea4ffbf24e8eb4b02594803060b38c148dc859684 /test/Transforms/SimplifyCFG
parent365f0455a6b202d349619ad9be3f2bccabc76d92 (diff)
downloadllvm-09a31f31545b7360c78265f9ca9cd35430ea401d.tar.gz
llvm-09a31f31545b7360c78265f9ca9cd35430ea401d.tar.bz2
llvm-09a31f31545b7360c78265f9ca9cd35430ea401d.tar.xz
Allow switch-to-lookup table for tables with holes by adding bitmask check
This allows us to generate table lookups for code such as: unsigned test(unsigned x) { switch (x) { case 100: return 0; case 101: return 1; case 103: return 2; case 105: return 3; case 107: return 4; case 109: return 5; case 110: return 6; default: return f(x); } } Since cases 102, 104, etc. are not constants, the lookup table has holes in those positions. We therefore guard the table lookup with a bitmask check. Patch by Jasper Neumann! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203694 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Transforms/SimplifyCFG')
-rw-r--r--test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll30
1 files changed, 28 insertions, 2 deletions
diff --git a/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll b/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
index b47fa02af2..81079b1aa5 100644
--- a/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
+++ b/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll
@@ -832,7 +832,7 @@ return:
; CHECK-NOT: switch i32
}
-; This lookup table will have holes, so we cannot build it without default result.
+; This lookup table will have holes, so we need to test for the holes.
define i32 @nodefaultwithholes(i32 %c) {
entry:
switch i32 %c, label %sw.default [
@@ -853,8 +853,34 @@ return:
ret i32 %x
; CHECK-LABEL: @nodefaultwithholes(
-; CHECK-NOT: @switch.table
+; CHECK: entry:
+; CHECK: br i1 %{{.*}}, label %switch.hole_check, label %sw.default
+; CHECK: switch.hole_check:
+; CHECK-NEXT: %switch.maskindex = trunc i32 %switch.tableidx to i6
+; CHECK-NEXT: %switch.shifted = lshr i6 -17, %switch.maskindex
+; The mask is binary 101111.
+; CHECK-NEXT: %switch.lobit = trunc i6 %switch.shifted to i1
+; CHECK-NEXT: br i1 %switch.lobit, label %switch.lookup, label %sw.default
+; CHECK-NOT: switch i32
+}
+
+; We don't build lookup tables with holes for switches with less than four cases.
+define i32 @threecasesholes(i32 %c) {
+entry:
+ switch i32 %c, label %sw.default [
+ i32 0, label %return
+ i32 1, label %sw.bb1
+ i32 3, label %sw.bb2
+ ]
+sw.bb1: br label %return
+sw.bb2: br label %return
+sw.default: br label %return
+return:
+ %x = phi i32 [ %c, %sw.default ], [ 5, %sw.bb2 ], [ 7, %sw.bb1 ], [ 9, %entry ]
+ ret i32 %x
+; CHECK-LABEL: @threecasesholes(
; CHECK: switch i32
+; CHECK-NOT: @switch.table
}
; We build lookup tables for switches with three or more cases.