1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s
4 ;; Test generation of the bstrins.d instruction.
5 ;; There are 8 patterns that can be matched to bstrins.d. See performORCombine
9 ;; R = or (and X, mask0), (and (shl Y, lsb), mask1)
11 ;; R = BSTRINS X, Y, msb, lsb
12 define i64 @pat1(i64 %a, i64 %b) nounwind {
15 ; CHECK-NEXT: bstrins.d $a0, $a1, 39, 16
17 %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
19 %and2 = and i64 %shl, 1099511562240 ; 0x000000ffffff0000
20 %or = or i64 %and1, %and2
24 define i64 @pat1_swap(i64 %a, i64 %b) nounwind {
25 ; CHECK-LABEL: pat1_swap:
27 ; CHECK-NEXT: bstrins.d $a0, $a1, 39, 16
29 %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
31 %and2 = and i64 %shl, 1099511562240 ; 0x000000ffffff0000
32 %or = or i64 %and2, %and1
37 ;; R = or (and X, mask0), (shl (and Y, mask1), lsb)
39 ;; R = BSTRINS X, Y, msb, lsb
40 define i64 @pat2(i64 %a, i64 %b) nounwind {
43 ; CHECK-NEXT: bstrins.d $a0, $a1, 39, 16
45 %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
46 %and2 = and i64 %b, 16777215 ; 0x0000000000ffffff
47 %shl = shl i64 %and2, 16
48 %or = or i64 %and1, %shl
52 define i64 @pat2_swap(i64 %a, i64 %b) nounwind {
53 ; CHECK-LABEL: pat2_swap:
55 ; CHECK-NEXT: bstrins.d $a0, $a1, 39, 16
57 %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
58 %and2 = and i64 %b, 16777215 ; 0x0000000000ffffff
59 %shl = shl i64 %and2, 16
60 %or = or i64 %shl, %and1
65 ;; R = or (and X, mask0), (and Y, mask1)
67 ;; R = BSTRINS X, (srl (and Y, mask1), lsb), msb, lsb
68 define i64 @pat3(i64 %a, i64 %b) nounwind {
71 ; CHECK-NEXT: andi $a1, $a1, 288
72 ; CHECK-NEXT: srli.d $a1, $a1, 4
73 ; CHECK-NEXT: bstrins.d $a0, $a1, 11, 4
75 %and1 = and i64 %a, -4081 ; 0xfffffffffffff00f
76 %and2 = and i64 %b, 288 ; 0x0000000000000120
77 %or = or i64 %and1, %and2
81 define i64 @pat3_swap(i64 %a, i64 %b) nounwind {
82 ; CHECK-LABEL: pat3_swap:
84 ; CHECK-NEXT: andi $a1, $a1, 288
85 ; CHECK-NEXT: srli.d $a1, $a1, 4
86 ; CHECK-NEXT: bstrins.d $a0, $a1, 11, 4
88 %and1 = and i64 %a, -4081 ; 0xfffffffffffff00f
89 %and2 = and i64 %b, 288 ; 0x0000000000000120
90 %or = or i64 %and2, %and1
95 ;; R = or (and X, mask), (shl Y, shamt)
97 ;; R = BSTRINS X, Y, 63, shamt
98 define i64 @pat4(i64 %a, i64 %b) nounwind {
101 ; CHECK-NEXT: bstrins.d $a0, $a1, 63, 8
103 %and = and i64 %a, 255
105 %or = or i64 %and, %shl
109 define i64 @pat4_swap(i64 %a, i64 %b) nounwind {
110 ; CHECK-LABEL: pat4_swap:
112 ; CHECK-NEXT: bstrins.d $a0, $a1, 63, 8
114 %and = and i64 %a, 255
116 %or = or i64 %shl, %and
121 ;; R = or (and X, mask0), const
123 ;; R = BSTRINS X, (const >> lsb), msb, lsb
124 define i64 @pat5(i64 %a) nounwind {
127 ; CHECK-NEXT: lu12i.w $a1, 74565
128 ; CHECK-NEXT: ori $a1, $a1, 1656
129 ; CHECK-NEXT: bstrins.d $a0, $a1, 47, 16
131 %and = and i64 %a, 18446462598732906495 ; 0xffff00000000ffff
132 %or = or i64 %and, 20015998304256 ; 0x0000123456780000
136 ;; Pattern 6: a = b | ((c & mask) << shamt)
137 ;; In this testcase b is 0x123456000000789a, but in fact we do not require b
138 ;; being a constant. As long as all positions in b to be overwritten by the
139 ;; incoming bits are known to be zero, the pattern could be matched.
140 define i64 @pat6(i64 %c) nounwind {
143 ; CHECK-NEXT: lu12i.w $a1, 7
144 ; CHECK-NEXT: ori $a1, $a1, 2202
145 ; CHECK-NEXT: lu32i.d $a1, 284160
146 ; CHECK-NEXT: lu52i.d $a1, $a1, 291
147 ; CHECK-NEXT: bstrins.d $a1, $a0, 39, 16
148 ; CHECK-NEXT: move $a0, $a1
150 %and = and i64 %c, 16777215 ; 0x0000000000ffffff
151 %shl = shl i64 %and, 16
152 %or = or i64 %shl, 1311767949471676570 ; 0x123456000000789a
156 ;; Pattern 7: a = b | ((c << shamt) & shifted_mask)
157 ;; Similar to pattern 6.
158 define i64 @pat7(i64 %c) nounwind {
161 ; CHECK-NEXT: lu12i.w $a1, 7
162 ; CHECK-NEXT: ori $a1, $a1, 2202
163 ; CHECK-NEXT: lu32i.d $a1, 284160
164 ; CHECK-NEXT: lu52i.d $a1, $a1, 291
165 ; CHECK-NEXT: bstrins.d $a1, $a0, 39, 16
166 ; CHECK-NEXT: move $a0, $a1
168 %shl = shl i64 %c, 16
169 %and = and i64 %shl, 1099511562240 ; 0x000000ffffff0000
170 %or = or i64 %and, 1311767949471676570 ; 0x123456000000789a
174 ;; Pattern 8: a = b | (c & shifted_mask)
175 ;; Similar to pattern 7 but without shift to c.
176 define i64 @pat8(i64 %c) nounwind {
179 ; CHECK-NEXT: srli.d $a1, $a0, 16
180 ; CHECK-NEXT: lu12i.w $a0, 7
181 ; CHECK-NEXT: ori $a0, $a0, 2202
182 ; CHECK-NEXT: lu32i.d $a0, 284160
183 ; CHECK-NEXT: lu52i.d $a0, $a0, 291
184 ; CHECK-NEXT: bstrins.d $a0, $a1, 39, 16
186 %and = and i64 %c, 1099511562240 ; 0x000000ffffff0000
187 %or = or i64 %and, 1311767949471676570 ; 0x123456000000789a
191 ;; Test that bstrins.d is not generated because constant OR operand
192 ;; doesn't fit into bits cleared by constant AND operand.
193 define i64 @no_bstrins_d(i64 %a) nounwind {
194 ; CHECK-LABEL: no_bstrins_d:
196 ; CHECK-NEXT: lu12i.w $a1, 354185
197 ; CHECK-NEXT: lu32i.d $a1, 4660
198 ; CHECK-NEXT: or $a0, $a0, $a1
199 ; CHECK-NEXT: lu12i.w $a1, 354191
200 ; CHECK-NEXT: ori $a1, $a1, 4095
201 ; CHECK-NEXT: lu32i.d $a1, -60876
202 ; CHECK-NEXT: and $a0, $a0, $a1
204 %and = and i64 %a, 18446462598732906495 ; 0xffff00000000ffff
205 %or = or i64 %and, 20015998341120 ; 0x0000123456789000