1 ; RUN: llc -mtriple=armv4t-eabi %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=V4T
2 ; RUN: llc -mtriple=armv6-eabi %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=V6
3 ; RUN: llc -mtriple=armv6t2-eabi %s -o - | FileCheck %s --check-prefix=CHECK --check-prefix=V6T2
5 ; Check for several conditions that should result in USAT.
6 ; For example, the base test is equivalent to
7 ; x < 0 ? 0 : (x > k ? k : x) in C. All patterns that bound x
8 ; to the interval [0, k] where k + 1 is a power of 2 can be
9 ; transformed into USAT. At the end there are some tests
10 ; checking that conditionals are not transformed if they don't
11 ; match the right pattern.
14 ; Base tests with different bit widths
17 ; x < 0 ? 0 : (x > k ? k : x)
19 define i32 @unsigned_sat_base_32bit(i32 %x) #0 {
20 ; CHECK-LABEL: unsigned_sat_base_32bit:
21 ; V6: usat r0, #23, r0
22 ; V6T2: usat r0, #23, r0
25 %cmpLow = icmp slt i32 %x, 0
26 %cmpUp = icmp sgt i32 %x, 8388607
27 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %x
28 %saturateLow = select i1 %cmpLow, i32 0, i32 %saturateUp
32 ; x < 0 ? 0 : (x > k ? k : x)
34 define i16 @unsigned_sat_base_16bit(i16 %x) #0 {
35 ; CHECK-LABEL: unsigned_sat_base_16bit:
36 ; V6: usat r0, #11, r0
37 ; V6T2: usat r0, #11, r0
40 %cmpLow = icmp slt i16 %x, 0
41 %cmpUp = icmp sgt i16 %x, 2047
42 %saturateUp = select i1 %cmpUp, i16 2047, i16 %x
43 %saturateLow = select i1 %cmpLow, i16 0, i16 %saturateUp
47 ; x < 0 ? 0 : (x > k ? k : x)
49 define i8 @unsigned_sat_base_8bit(i8 %x) #0 {
50 ; CHECK-LABEL: unsigned_sat_base_8bit:
52 ; V6T2: usat r0, #5, r0
55 %cmpLow = icmp slt i8 %x, 0
56 %cmpUp = icmp sgt i8 %x, 31
57 %saturateUp = select i1 %cmpUp, i8 31, i8 %x
58 %saturateLow = select i1 %cmpLow, i8 0, i8 %saturateUp
63 ; Tests where the conditionals that check for upper and lower bounds,
64 ; or the < and > operators, are arranged in different ways. Only some
65 ; of the possible combinations that lead to USAT are tested.
67 ; x < 0 ? 0 : (x < k ? x : k)
68 define i32 @unsigned_sat_lower_upper_1(i32 %x) #0 {
69 ; CHECK-LABEL: unsigned_sat_lower_upper_1:
70 ; V6: usat r0, #23, r0
71 ; V6T2: usat r0, #23, r0
74 %cmpLow = icmp slt i32 %x, 0
75 %cmpUp = icmp slt i32 %x, 8388607
76 %saturateUp = select i1 %cmpUp, i32 %x, i32 8388607
77 %saturateLow = select i1 %cmpLow, i32 0, i32 %saturateUp
81 ; x > 0 ? (x > k ? k : x) : 0
82 define i32 @unsigned_sat_lower_upper_2(i32 %x) #0 {
83 ; CHECK-LABEL: unsigned_sat_lower_upper_2:
84 ; V6: usat r0, #23, r0
85 ; V6T2: usat r0, #23, r0
88 %cmpLow = icmp sgt i32 %x, 0
89 %cmpUp = icmp sgt i32 %x, 8388607
90 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %x
91 %saturateLow = select i1 %cmpLow, i32 %saturateUp, i32 0
95 ; x < k ? (x < 0 ? 0 : x) : k
96 define i32 @unsigned_sat_upper_lower_1(i32 %x) #0 {
97 ; CHECK-LABEL: unsigned_sat_upper_lower_1:
98 ; V6: usat r0, #23, r0
99 ; V6T2: usat r0, #23, r0
102 %cmpUp = icmp slt i32 %x, 8388607
103 %cmpLow = icmp slt i32 %x, 0
104 %saturateLow = select i1 %cmpLow, i32 0, i32 %x
105 %saturateUp = select i1 %cmpUp, i32 %saturateLow, i32 8388607
109 ; x > k ? k : (x < 0 ? 0 : x)
110 define i32 @unsigned_sat_upper_lower_2(i32 %x) #0 {
111 ; CHECK-LABEL: unsigned_sat_upper_lower_2:
112 ; V6: usat r0, #23, r0
113 ; V6T2: usat r0, #23, r0
116 %cmpUp = icmp sgt i32 %x, 8388607
117 %cmpLow = icmp slt i32 %x, 0
118 %saturateLow = select i1 %cmpLow, i32 0, i32 %x
119 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
123 ; k < x ? k : (x > 0 ? x : 0)
124 define i32 @unsigned_sat_upper_lower_3(i32 %x) #0 {
125 ; CHECK-LABEL: unsigned_sat_upper_lower_3:
126 ; V6: usat r0, #23, r0
127 ; V6T2: usat r0, #23, r0
130 %cmpUp = icmp slt i32 8388607, %x
131 %cmpLow = icmp sgt i32 %x, 0
132 %saturateLow = select i1 %cmpLow, i32 %x, i32 0
133 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
138 ; The following tests check for patterns that should not transform
139 ; into USAT but are similar enough that could confuse the selector.
141 ; x > k ? k : (x > 0 ? 0 : x)
142 ; First condition upper-saturates, second doesn't lower-saturate.
143 define i32 @no_unsigned_sat_missing_lower(i32 %x) #0 {
144 ; CHECK-LABEL: no_unsigned_sat_missing_lower
147 %cmpUp = icmp sgt i32 %x, 8388607
148 %cmpLow = icmp sgt i32 %x, 0
149 %saturateLow = select i1 %cmpLow, i32 0, i32 %x
150 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
154 ; x < k ? k : (x < 0 ? 0 : x)
155 ; Second condition lower-saturates, first doesn't upper-saturate.
156 define i32 @no_unsigned_sat_missing_upper(i32 %x) #0 {
157 ; CHECK-LABEL: no_unsigned_sat_missing_upper:
160 %cmpUp = icmp slt i32 %x, 8388607
161 %cmpLow = icmp slt i32 %x, 0
162 %saturateLow = select i1 %cmpLow, i32 0, i32 %x
163 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
167 ; Lower constant is different in the select and in the compare
168 define i32 @no_unsigned_sat_incorrect_constant(i32 %x) #0 {
169 ; CHECK-LABEL: no_unsigned_sat_incorrect_constant:
172 %cmpUp = icmp sgt i32 %x, 8388607
173 %cmpLow = icmp slt i32 %x, 0
174 %saturateLow = select i1 %cmpLow, i32 -1, i32 %x
175 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
179 ; The interval is not [0, k]
180 define i32 @no_unsigned_sat_incorrect_interval(i32 %x) #0 {
181 ; CHECK-LABEL: no_unsigned_sat_incorrect_interval:
184 %cmpUp = icmp sgt i32 %x, 8388607
185 %cmpLow = icmp slt i32 %x, -4
186 %saturateLow = select i1 %cmpLow, i32 -4, i32 %x
187 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
191 ; The returned value (y) is not the same as the tested value (x).
192 define i32 @no_unsigned_sat_incorrect_return(i32 %x, i32 %y) #0 {
193 ; CHECK-LABEL: no_unsigned_sat_incorrect_return:
196 %cmpUp = icmp sgt i32 %x, 8388607
197 %cmpLow = icmp slt i32 %x, 0
198 %saturateLow = select i1 %cmpLow, i32 0, i32 %y
199 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow
203 ; One of the values in a compare (y) is not the same as the rest
204 ; of the compare and select values (x).
205 define i32 @no_unsigned_sat_incorrect_compare(i32 %x, i32 %y) #0 {
206 ; CHECK-LABEL: no_unsigned_sat_incorrect_compare:
209 %cmpUp = icmp sgt i32 %x, 8388607
210 %cmpLow = icmp slt i32 %y, 0
211 %saturateLow = select i1 %cmpLow, i32 0, i32 %x
212 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow