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