1 ; RUN: llc -mtriple=aarch64-none-linux-gnu < %s | FileCheck %s --check-prefix=CHECK 2 3 ; First, a simple example from Clang. The registers could plausibly be 4 ; different, but probably won't be. 5 6 %struct.foo = type { i8, [2 x i8], i8 } 7 8 define [1 x i64] @from_clang([1 x i64] %f.coerce, i32 %n) nounwind readnone { 9 ; CHECK-LABEL: from_clang: 10 ; CHECK: bfi {{w[0-9]+}}, {{w[0-9]+}}, #3, #4 11 12 entry: 13 %f.coerce.fca.0.extract = extractvalue [1 x i64] %f.coerce, 0 14 %tmp.sroa.0.0.extract.trunc = trunc i64 %f.coerce.fca.0.extract to i32 15 %bf.value = shl i32 %n, 3 16 %0 = and i32 %bf.value, 120 17 %f.sroa.0.0.insert.ext.masked = and i32 %tmp.sroa.0.0.extract.trunc, 135 18 %1 = or i32 %f.sroa.0.0.insert.ext.masked, %0 19 %f.sroa.0.0.extract.trunc = zext i32 %1 to i64 20 %tmp1.sroa.1.1.insert.insert = and i64 %f.coerce.fca.0.extract, 4294967040 21 %tmp1.sroa.0.0.insert.insert = or i64 %f.sroa.0.0.extract.trunc, %tmp1.sroa.1.1.insert.insert 22 %.fca.0.insert = insertvalue [1 x i64] undef, i64 %tmp1.sroa.0.0.insert.insert, 0 23 ret [1 x i64] %.fca.0.insert 24 } 25 26 define void @test_whole32(i32* %existing, i32* %new) { 27 ; CHECK-LABEL: test_whole32: 28 29 ; CHECK: bfi {{w[0-9]+}}, {{w[0-9]+}}, #26, #5 30 31 %oldval = load volatile i32, i32* %existing 32 %oldval_keep = and i32 %oldval, 2214592511 ; =0x83ffffff 33 34 %newval = load volatile i32, i32* %new 35 %newval_shifted = shl i32 %newval, 26 36 %newval_masked = and i32 %newval_shifted, 2080374784 ; = 0x7c000000 37 38 %combined = or i32 %oldval_keep, %newval_masked 39 store volatile i32 %combined, i32* %existing 40 41 ret void 42 } 43 44 define void @test_whole64(i64* %existing, i64* %new) { 45 ; CHECK-LABEL: test_whole64: 46 ; CHECK: bfi {{x[0-9]+}}, {{x[0-9]+}}, #26, #14 47 ; CHECK-NOT: and 48 ; CHECK: ret 49 50 %oldval = load volatile i64, i64* %existing 51 %oldval_keep = and i64 %oldval, 18446742974265032703 ; = 0xffffff0003ffffffL 52 53 %newval = load volatile i64, i64* %new 54 %newval_shifted = shl i64 %newval, 26 55 %newval_masked = and i64 %newval_shifted, 1099444518912 ; = 0xfffc000000 56 57 %combined = or i64 %oldval_keep, %newval_masked 58 store volatile i64 %combined, i64* %existing 59 60 ret void 61 } 62 63 define void @test_whole32_from64(i64* %existing, i64* %new) { 64 ; CHECK-LABEL: test_whole32_from64: 65 66 67 ; CHECK: bfxil {{x[0-9]+}}, {{x[0-9]+}}, #0, #16 68 69 ; CHECK: ret 70 71 %oldval = load volatile i64, i64* %existing 72 %oldval_keep = and i64 %oldval, 4294901760 ; = 0xffff0000 73 74 %newval = load volatile i64, i64* %new 75 %newval_masked = and i64 %newval, 65535 ; = 0xffff 76 77 %combined = or i64 %oldval_keep, %newval_masked 78 store volatile i64 %combined, i64* %existing 79 80 ret void 81 } 82 83 define void @test_32bit_masked(i32 *%existing, i32 *%new) { 84 ; CHECK-LABEL: test_32bit_masked: 85 86 ; CHECK: and 87 ; CHECK: bfi [[INSERT:w[0-9]+]], {{w[0-9]+}}, #3, #4 88 89 %oldval = load volatile i32, i32* %existing 90 %oldval_keep = and i32 %oldval, 135 ; = 0x87 91 92 %newval = load volatile i32, i32* %new 93 %newval_shifted = shl i32 %newval, 3 94 %newval_masked = and i32 %newval_shifted, 120 ; = 0x78 95 96 %combined = or i32 %oldval_keep, %newval_masked 97 store volatile i32 %combined, i32* %existing 98 99 ret void 100 } 101 102 define void @test_64bit_masked(i64 *%existing, i64 *%new) { 103 ; CHECK-LABEL: test_64bit_masked: 104 ; CHECK: and 105 ; CHECK: bfi [[INSERT:x[0-9]+]], {{x[0-9]+}}, #40, #8 106 107 %oldval = load volatile i64, i64* %existing 108 %oldval_keep = and i64 %oldval, 1095216660480 ; = 0xff_0000_0000 109 110 %newval = load volatile i64, i64* %new 111 %newval_shifted = shl i64 %newval, 40 112 %newval_masked = and i64 %newval_shifted, 280375465082880 ; = 0xff00_0000_0000 113 114 %combined = or i64 %newval_masked, %oldval_keep 115 store volatile i64 %combined, i64* %existing 116 117 ret void 118 } 119 120 ; Mask is too complicated for literal ANDwwi, make sure other avenues are tried. 121 define void @test_32bit_complexmask(i32 *%existing, i32 *%new) { 122 ; CHECK-LABEL: test_32bit_complexmask: 123 124 ; CHECK: and 125 ; CHECK: bfi {{w[0-9]+}}, {{w[0-9]+}}, #3, #4 126 127 %oldval = load volatile i32, i32* %existing 128 %oldval_keep = and i32 %oldval, 647 ; = 0x287 129 130 %newval = load volatile i32, i32* %new 131 %newval_shifted = shl i32 %newval, 3 132 %newval_masked = and i32 %newval_shifted, 120 ; = 0x278 133 134 %combined = or i32 %oldval_keep, %newval_masked 135 store volatile i32 %combined, i32* %existing 136 137 ret void 138 } 139 140 ; Neither mask is is a contiguous set of 1s. BFI can't be used 141 define void @test_32bit_badmask(i32 *%existing, i32 *%new) { 142 ; CHECK-LABEL: test_32bit_badmask: 143 ; CHECK-NOT: bfi 144 ; CHECK-NOT: bfm 145 ; CHECK: ret 146 147 %oldval = load volatile i32, i32* %existing 148 %oldval_keep = and i32 %oldval, 135 ; = 0x87 149 150 %newval = load volatile i32, i32* %new 151 %newval_shifted = shl i32 %newval, 3 152 %newval_masked = and i32 %newval_shifted, 632 ; = 0x278 153 154 %combined = or i32 %oldval_keep, %newval_masked 155 store volatile i32 %combined, i32* %existing 156 157 ret void 158 } 159 160 ; Ditto 161 define void @test_64bit_badmask(i64 *%existing, i64 *%new) { 162 ; CHECK-LABEL: test_64bit_badmask: 163 ; CHECK-NOT: bfi 164 ; CHECK-NOT: bfm 165 ; CHECK: ret 166 167 %oldval = load volatile i64, i64* %existing 168 %oldval_keep = and i64 %oldval, 135 ; = 0x87 169 170 %newval = load volatile i64, i64* %new 171 %newval_shifted = shl i64 %newval, 3 172 %newval_masked = and i64 %newval_shifted, 664 ; = 0x278 173 174 %combined = or i64 %oldval_keep, %newval_masked 175 store volatile i64 %combined, i64* %existing 176 177 ret void 178 } 179 180 ; Bitfield insert where there's a left-over shr needed at the beginning 181 ; (e.g. result of str.bf1 = str.bf2) 182 define void @test_32bit_with_shr(i32* %existing, i32* %new) { 183 ; CHECK-LABEL: test_32bit_with_shr: 184 185 %oldval = load volatile i32, i32* %existing 186 %oldval_keep = and i32 %oldval, 2214592511 ; =0x83ffffff 187 188 %newval = load i32, i32* %new 189 %newval_shifted = shl i32 %newval, 12 190 %newval_masked = and i32 %newval_shifted, 2080374784 ; = 0x7c000000 191 192 %combined = or i32 %oldval_keep, %newval_masked 193 store volatile i32 %combined, i32* %existing 194 ; CHECK: lsr [[BIT:w[0-9]+]], {{w[0-9]+}}, #14 195 ; CHECK: bfi {{w[0-9]+}}, [[BIT]], #26, #5 196 197 ret void 198 } 199 200 ; Bitfield insert where the second or operand is a better match to be folded into the BFM 201 define void @test_32bit_opnd1_better(i32* %existing, i32* %new) { 202 ; CHECK-LABEL: test_32bit_opnd1_better: 203 204 %oldval = load volatile i32, i32* %existing 205 %oldval_keep = and i32 %oldval, 65535 ; 0x0000ffff 206 207 %newval = load i32, i32* %new 208 %newval_shifted = shl i32 %newval, 16 209 %newval_masked = and i32 %newval_shifted, 16711680 ; 0x00ff0000 210 211 %combined = or i32 %oldval_keep, %newval_masked 212 store volatile i32 %combined, i32* %existing 213 ; CHECK: and [[BIT:w[0-9]+]], {{w[0-9]+}}, #0xffff 214 ; CHECK: bfi [[BIT]], {{w[0-9]+}}, #16, #8 215 216 ret void 217 } 218 219 ; Tests when all the bits from one operand are not useful 220 define i32 @test_nouseful_bits(i8 %a, i32 %b) { 221 ; CHECK-LABEL: test_nouseful_bits: 222 ; CHECK: bfi 223 ; CHECK: bfi 224 ; CHECK: bfi 225 ; CHECK-NOT: bfi 226 ; CHECK-NOT: or 227 ; CHECK: lsl 228 %conv = zext i8 %a to i32 ; 0 0 0 A 229 %shl = shl i32 %b, 8 ; B2 B1 B0 0 230 %or = or i32 %conv, %shl ; B2 B1 B0 A 231 %shl.1 = shl i32 %or, 8 ; B1 B0 A 0 232 %or.1 = or i32 %conv, %shl.1 ; B1 B0 A A 233 %shl.2 = shl i32 %or.1, 8 ; B0 A A 0 234 %or.2 = or i32 %conv, %shl.2 ; B0 A A A 235 %shl.3 = shl i32 %or.2, 8 ; A A A 0 236 %or.3 = or i32 %conv, %shl.3 ; A A A A 237 %shl.4 = shl i32 %or.3, 8 ; A A A 0 238 ret i32 %shl.4 239 } 240