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