xref: /llvm-project/llvm/test/Transforms/CodeGenPrepare/X86/widen_switch.ll (revision cd19af74c031f0f538050d00b26bab3fbca07414)
1;; x86 is chosen to show the transform when 8-bit and 16-bit registers are available.
2
3; RUN: opt < %s -codegenprepare -S -mtriple=x86_64-unknown-unknown    | FileCheck %s --check-prefix=X86
4; RUN: opt < %s -debugify -codegenprepare -S -mtriple=x86_64-unknown-unknown | FileCheck %s --check-prefix=DEBUG
5
6; X86 prefers i32 over i16 for address calculation.
7
8define i32 @widen_switch_i16(i32 %a)  {
9entry:
10  %trunc = trunc i32 %a to i16
11  switch i16 %trunc, label %sw.default [
12    i16 1, label %sw.bb0
13    i16 -1, label %sw.bb1
14  ]
15
16sw.bb0:
17  br label %return
18
19sw.bb1:
20  br label %return
21
22sw.default:
23  br label %return
24
25return:
26  %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
27  ret i32 %retval
28
29; X86-LABEL: @widen_switch_i16(
30; X86:       %trunc = trunc i32 %a to i16
31; X86-NEXT:  %0 = zext i16 %trunc to i32
32; X86-NEXT:  switch i32 %0, label %sw.default [
33; X86-NEXT:    i32 1, label %sw.bb0
34; X86-NEXT:    i32 65535, label %sw.bb1
35}
36
37; Widen to 32-bit from a smaller, non-native type.
38
39define i32 @widen_switch_i17(i32 %a)  {
40entry:
41  %trunc = trunc i32 %a to i17
42  switch i17 %trunc, label %sw.default [
43    i17 10, label %sw.bb0
44    i17 -1, label %sw.bb1
45  ]
46
47sw.bb0:
48  br label %return
49
50sw.bb1:
51  br label %return
52
53sw.default:
54  br label %return
55
56return:
57  %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
58  ret i32 %retval
59
60; X86-LABEL: @widen_switch_i17(
61; X86:       %0 = zext i17 %trunc to i32
62; X86-NEXT:  switch i32 %0, label %sw.default [
63; X86-NEXT:    i32 10, label %sw.bb0
64; X86-NEXT:    i32 131071, label %sw.bb1
65
66; DEBUG-LABEL: @widen_switch_i17(
67; DEBUG:       zext i17 %trunc to i32, !dbg [[switch_loc:![0-9]+]]
68; DEBUG-NEXT:  switch i32 {{.*}} [
69; DEBUG-NEXT:    label %sw.bb0
70; DEBUG-NEXT:    label %sw.bb1
71; DEBUG-NEXT:  ], !dbg [[switch_loc]]
72}
73
74; If the switch condition is a sign-extended function argument, then the
75; condition and cases should be sign-extended rather than zero-extended
76; because the sign-extension can be optimized away.
77
78define i32 @widen_switch_i16_sext(i2 signext %a)  {
79entry:
80  switch i2 %a, label %sw.default [
81    i2 1, label %sw.bb0
82    i2 -1, label %sw.bb1
83  ]
84
85sw.bb0:
86  br label %return
87
88sw.bb1:
89  br label %return
90
91sw.default:
92  br label %return
93
94return:
95  %retval = phi i32 [ -1, %sw.default ], [ 0, %sw.bb0 ], [ 1, %sw.bb1 ]
96  ret i32 %retval
97
98; X86-LABEL: @widen_switch_i16_sext(
99; X86:       %0 = sext i2 %a to i32
100; X86-NEXT:  switch i32 %0, label %sw.default [
101; X86-NEXT:    i32 1, label %sw.bb0
102; X86-NEXT:    i32 -1, label %sw.bb1
103}
104
105