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 4 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. 12 13; 14; Base tests with different bit widths 15; 16 17; x < 0 ? 0 : (x > k ? k : x) 18; 32-bit base test 19define 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 23; V4T-NOT: usat 24entry: 25 %0 = icmp slt i32 %x, 8388607 26 %saturateUp = select i1 %0, i32 %x, i32 8388607 27 %1 = icmp sgt i32 %saturateUp, 0 28 %saturateLow = select i1 %1, i32 %saturateUp, i32 0 29 ret i32 %saturateLow 30} 31 32; x < 0 ? 0 : (x > k ? k : x) 33; 16-bit base test 34define 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 38; V4T-NOT: usat 39entry: 40 %0 = icmp slt i16 %x, 2047 41 %saturateUp = select i1 %0, i16 %x, i16 2047 42 %1 = icmp sgt i16 %saturateUp, 0 43 %saturateLow = select i1 %1, i16 %saturateUp, i16 0 44 ret i16 %saturateLow 45} 46 47; x < 0 ? 0 : (x > k ? k : x) 48; 8-bit base test 49define i8 @unsigned_sat_base_8bit(i8 %x) #0 { 50; CHECK-LABEL: unsigned_sat_base_8bit: 51; V6: usat r0, #5, r0 52; V6T2: usat r0, #5, r0 53; V4T-NOT: usat 54entry: 55 %0 = icmp slt i8 %x, 31 56 %saturateUp = select i1 %0, i8 %x, i8 31 57 %1 = icmp sgt i8 %saturateUp, 0 58 %saturateLow = select i1 %1, i8 %saturateUp, i8 0 59 ret i8 %saturateLow 60} 61 62; 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. 66; 67; x < 0 ? 0 : (x < k ? x : k) 68define 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 72; V4T-NOT: usat 73entry: 74 %cmpUp = icmp slt i32 %x, 8388607 75 %saturateUp = select i1 %cmpUp, i32 %x, i32 8388607 76 %0 = icmp sgt i32 %saturateUp, 0 77 %saturateLow = select i1 %0, i32 %saturateUp, i32 0 78 ret i32 %saturateLow 79} 80 81; x > 0 ? (x > k ? k : x) : 0 82define 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 86; V4T-NOT: usat 87entry: 88 %0 = icmp slt i32 %x, 8388607 89 %saturateUp = select i1 %0, i32 %x, i32 8388607 90 %1 = icmp sgt i32 %saturateUp, 0 91 %saturateLow = select i1 %1, i32 %saturateUp, i32 0 92 ret i32 %saturateLow 93} 94 95; x < k ? (x < 0 ? 0 : x) : k 96define 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 100; V4T-NOT: usat 101entry: 102 %0 = icmp sgt i32 %x, 0 103 %saturateLow = select i1 %0, i32 %x, i32 0 104 %1 = icmp slt i32 %saturateLow, 8388607 105 %saturateUp = select i1 %1, i32 %saturateLow, i32 8388607 106 ret i32 %saturateUp 107} 108 109; x > k ? k : (x < 0 ? 0 : x) 110define 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 114; V4T-NOT: usat 115entry: 116 %0 = icmp sgt i32 %x, 0 117 %saturateLow = select i1 %0, i32 %x, i32 0 118 %1 = icmp slt i32 %saturateLow, 8388607 119 %saturateUp = select i1 %1, i32 %saturateLow, i32 8388607 120 ret i32 %saturateUp 121} 122 123; k < x ? k : (x > 0 ? x : 0) 124define 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 128; V4T-NOT: usat 129entry: 130 %cmpLow = icmp sgt i32 %x, 0 131 %saturateLow = select i1 %cmpLow, i32 %x, i32 0 132 %0 = icmp slt i32 %saturateLow, 8388607 133 %saturateUp = select i1 %0, i32 %saturateLow, i32 8388607 134 ret i32 %saturateUp 135} 136 137; 138; The following tests check for patterns that should not transform 139; into USAT but are similar enough that could confuse the selector. 140; 141; x > k ? k : (x > 0 ? 0 : x) 142; First condition upper-saturates, second doesn't lower-saturate. 143define i32 @no_unsigned_sat_missing_lower(i32 %x) #0 { 144; CHECK-LABEL: no_unsigned_sat_missing_lower 145; CHECK-NOT: usat 146entry: 147 %cmpUp = icmp sgt i32 %x, 8388607 148 %0 = icmp slt i32 %x, 0 149 %saturateLow = select i1 %0, i32 %x, i32 0 150 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow 151 ret i32 %saturateUp 152} 153 154; x < k ? k : (x < 0 ? 0 : x) 155; Second condition lower-saturates, first doesn't upper-saturate. 156define i32 @no_unsigned_sat_missing_upper(i32 %x) #0 { 157; CHECK-LABEL: no_unsigned_sat_missing_upper: 158; CHECK-NOT: usat 159entry: 160 %cmpUp = icmp slt i32 %x, 8388607 161 %0 = icmp sgt i32 %x, 0 162 %saturateLow = select i1 %0, i32 %x, i32 0 163 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow 164 ret i32 %saturateUp 165} 166 167; Lower constant is different in the select and in the compare 168define i32 @no_unsigned_sat_incorrect_constant(i32 %x) #0 { 169; CHECK-LABEL: no_unsigned_sat_incorrect_constant: 170; CHECK-NOT: usat 171entry: 172 %cmpLow.inv = icmp sgt i32 %x, -1 173 %saturateLow = select i1 %cmpLow.inv, i32 %x, i32 -1 174 %0 = icmp slt i32 %saturateLow, 8388607 175 %saturateUp = select i1 %0, i32 %saturateLow, i32 8388607 176 ret i32 %saturateUp 177} 178 179; The interval is [0, k] but k+1 is not a power of 2 180define i32 @no_unsigned_sat_incorrect_constant2(i32 %x) #0 { 181; CHECK-LABEL: no_unsigned_sat_incorrect_constant2: 182; CHECK-NOT: usat 183entry: 184 %0 = icmp sgt i32 %x, 0 185 %saturateLow = select i1 %0, i32 %x, i32 0 186 %1 = icmp slt i32 %saturateLow, 8388609 187 %saturateUp = select i1 %1, i32 %saturateLow, i32 8388609 188 ret i32 %saturateUp 189} 190 191; The interval is not [0, k] 192define i32 @no_unsigned_sat_incorrect_interval(i32 %x) #0 { 193; CHECK-LABEL: no_unsigned_sat_incorrect_interval: 194; CHECK-NOT: usat 195entry: 196 %0 = icmp sgt i32 %x, -4 197 %saturateLow = select i1 %0, i32 %x, i32 -4 198 %1 = icmp slt i32 %saturateLow, 8388607 199 %saturateUp = select i1 %1, i32 %saturateLow, i32 8388607 200 ret i32 %saturateUp 201} 202 203; The returned value (y) is not the same as the tested value (x). 204define i32 @no_unsigned_sat_incorrect_return(i32 %x, i32 %y) #0 { 205; CHECK-LABEL: no_unsigned_sat_incorrect_return: 206; CHECK-NOT: usat 207entry: 208 %cmpUp = icmp sgt i32 %x, 8388607 209 %cmpLow = icmp slt i32 %x, 0 210 %saturateLow = select i1 %cmpLow, i32 0, i32 %y 211 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow 212 ret i32 %saturateUp 213} 214 215; One of the values in a compare (y) is not the same as the rest 216; of the compare and select values (x). 217define i32 @no_unsigned_sat_incorrect_compare(i32 %x, i32 %y) #0 { 218; CHECK-LABEL: no_unsigned_sat_incorrect_compare: 219; CHECK-NOT: usat 220entry: 221 %cmpUp = icmp sgt i32 %x, 8388607 222 %cmpLow = icmp slt i32 %y, 0 223 %saturateLow = select i1 %cmpLow, i32 0, i32 %x 224 %saturateUp = select i1 %cmpUp, i32 8388607, i32 %saturateLow 225 ret i32 %saturateUp 226} 227