1; RUN: llc -mtriple=arm %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-ARM --check-prefix=CHECK-CMP
2; RUN: llc -mtriple=thumb-eabi %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-T --check-prefix=CHECK-CMP
3; RUN: llc -mtriple=thumb-eabi -mcpu=arm1156t2-s -mattr=+thumb2 %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-T2 --check-prefix=CHECK-CMP
4
5
6; Check for clipping against 0 that should result in bic
7;
8; Base tests with different bit widths
9;
10
11; x < 0 ? 0 : x
12; 32-bit base test
13define i32 @sat0_base_32bit(i32 %x) #0 {
14; CHECK-LABEL: sat0_base_32bit:
15; CHECK-CMP-NOT: cmp
16; CHECK-ARM: bic {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
17; CHECK-T2: bic.w {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
18; CHECK-T: asrs [[IM:r[0-9]]], {{r[0-9]}}, #31
19; CHECK-T-NEXT: bics {{r[0-9]}}, [[IM]]
20entry:
21  %cmpLow = icmp slt i32 %x, 0
22  %saturateLow = select i1 %cmpLow, i32 0, i32 %x
23  ret i32 %saturateLow
24}
25
26; x < 0 ? 0 : x
27; 16-bit base test
28define i16 @sat0_base_16bit(i16 %x) #0 {
29; CHECK-LABEL: sat0_base_16bit:
30; CHECK-CMP: cmp
31; CHECK-ARM-NOT: bic
32; CHECK-T2-NOT: bic.w
33; CHECK-T-NOT: bics
34entry:
35  %cmpLow = icmp slt i16 %x, 0
36  %saturateLow = select i1 %cmpLow, i16 0, i16 %x
37  ret i16 %saturateLow
38}
39
40; x < 0 ? 0 : x
41; 8-bit base test
42define i8 @sat0_base_8bit(i8 %x) #0 {
43; CHECK-LABEL: sat0_base_8bit:
44; CHECK-CMP: cmp
45; CHECK-ARM-NOT: bic
46; CHECK-T2-NOT: bic.w
47entry:
48  %cmpLow = icmp slt i8 %x, 0
49  %saturateLow = select i1 %cmpLow, i8 0, i8 %x
50  ret i8 %saturateLow
51}
52
53; Test where the conditional is formed in a different way
54
55; x > 0 ? x : 0
56define i32 @sat0_lower_1(i32 %x) #0 {
57; CHECK-LABEL: sat0_lower_1:
58; CHECK-CMP-NOT: cmp
59; CHECK-ARM: bic {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
60; CHECK-T2: bic.w {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
61; CHECK-T: asrs [[IM:r[0-9]]], {{r[0-9]}}, #31
62; CHECK-T-NEXT: bics {{r[0-9]}}, [[IM]]
63entry:
64  %cmpGt = icmp sgt i32 %x, 0
65  %saturateLow = select i1 %cmpGt, i32 %x, i32 0
66  ret i32 %saturateLow
67}
68
69
70; Check for clipping against -1 that should result in orr
71;
72; Base tests with different bit widths
73;
74
75; x < -1 ? -1 : x
76; 32-bit base test
77define i32 @sat1_base_32bit(i32 %x) #0 {
78; CHECK-LABEL: sat1_base_32bit:
79; CHECK-CMP-NOT: cmp
80; CHECK-ARM: orr {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
81; CHECK-T2: orr.w {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
82; CHECK-T: asrs [[IM:r[0-9]]], {{r[0-9]}}, #31
83; CHECK-T-NEXT: orrs {{r[0-9]}}, [[IM]]
84entry:
85  %cmpLow = icmp slt i32 %x, -1
86  %saturateLow = select i1 %cmpLow, i32 -1, i32 %x
87  ret i32 %saturateLow
88}
89
90; x < -1 ? -1 : x
91; 16-bit base test
92define i16 @sat1_base_16bit(i16 %x) #0 {
93; CHECK-LABEL: sat1_base_16bit:
94; CHECK-ARM: cmn
95; CHECK-T2: cmp
96; CHECK-T: cmp
97entry:
98  %cmpLow = icmp slt i16 %x, -1
99  %saturateLow = select i1 %cmpLow, i16 -1, i16 %x
100  ret i16 %saturateLow
101}
102
103; x < -1 ? -1 : x
104; 8-bit base test
105define i8 @sat1_base_8bit(i8 %x) #0 {
106; CHECK-LABEL: sat1_base_8bit:
107; CHECK-ARM: cmn
108; CHECK-T2: cmp
109; CHECK-T: cmp
110entry:
111  %cmpLow = icmp slt i8 %x, -1
112  %saturateLow = select i1 %cmpLow, i8 -1, i8 %x
113  ret i8 %saturateLow
114}
115
116; Test where the conditional is formed in a different way
117
118; x > -1 ? x : -1
119define i32 @sat1_lower_1(i32 %x) #0 {
120; CHECK-LABEL: sat1_lower_1:
121; CHECK-ARM: orr {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
122; CHECK-T2: orr.w {{r[0-9]}}, [[INPUT:r[0-9]]], [[INPUT]], asr #31
123; CHECK-T: asrs [[IM:r[0-9]]], {{r[0-9]}}, #31
124; CHECK-T-NEXT: orrs {{r[0-9]}}, [[IM]]
125; CHECK-CMP-NOT: cmp
126entry:
127  %cmpGt = icmp sgt i32 %x, -1
128  %saturateLow = select i1 %cmpGt, i32 %x, i32 -1
129  ret i32 %saturateLow
130}
131
132; The following tests for patterns that should not transform into bitops
133; but that are similar enough that could confuse the selector.
134
135; x < 0 ? 0 : y where x and y does not properly match
136define i32 @no_sat0_incorrect_variable(i32 %x, i32 %y) #0 {
137; CHECK-LABEL: no_sat0_incorrect_variable:
138; CHECK-NOT: bic
139; CHECK-NOT: asrs
140; CHECK-CMP: cmp
141entry:
142  %cmpLow = icmp slt i32 %x, 0
143  %saturateLow = select i1 %cmpLow, i32 0, i32 %y
144  ret i32 %saturateLow
145}
146
147; x < 0 ? -1 : x
148define i32 @no_sat0_incorrect_constant(i32 %x) #0 {
149; CHECK-LABEL: no_sat0_incorrect_constant:
150; CHECK-NOT: bic
151; CHECK-NOT: asrs
152; CHECK-CMP: cmp
153entry:
154  %cmpLow = icmp slt i32 %x, 0
155  %saturateLow = select i1 %cmpLow, i32 -1, i32 %x
156  ret i32 %saturateLow
157}
158