Debian Codegraph @

gcc/config/riscv/bitmanip.md

raw
1 ;; Machine description for RISC-V Bit Manipulation operations.
2 ;; Copyright (C) 2021-2025 Free Software Foundation, Inc.
3
4 ;; This file is part of GCC.
5
6 ;; GCC is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 3, or (at your option)
9 ;; any later version.
10
11 ;; GCC is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GCC; see the file COPYING3. If not see
18 ;; <http://www.gnu.org/licenses/>.
19
20 ;; ZBA extension.
21
22 (define_insn "*zero_extendsidi2_bitmanip"
23 [(set (match_operand:DI 0 "register_operand" "=r,r")
24 (zero_extend:DI (match_operand:SI 1 "nonimmediate_operand" "r,m")))]
25 "TARGET_64BIT && TARGET_ZBA && !TARGET_XTHEADMEMIDX"
26 "@
27 zext.w\t%0,%1
28 lwu\t%0,%1"
29 [(set_attr "type" "bitmanip,load")
30 (set_attr "mode" "DI")])
31
32 (define_insn "*shNadd"
33 [(set (match_operand:X 0 "register_operand" "=r")
34 (plus:X (ashift:X (match_operand:X 1 "register_operand" "r")
35 (match_operand:QI 2 "imm123_operand" "Ds3"))
36 (match_operand:X 3 "register_operand" "r")))]
37 "TARGET_ZBA"
38 "sh%2add\t%0,%1,%3"
39 [(set_attr "type" "bitmanip")
40 (set_attr "mode" "<X:MODE>")])
41
42 ; When using strength-reduction, we will reduce a multiplication to a
43 ; sequence of shifts and adds. If this is performed with 32-bit types
44 ; and followed by a division, the lack of w-form sh[123]add will make
45 ; combination impossible and lead to a slli + addw being generated.
46 ; Split the sequence with the knowledge that a w-form div will perform
47 ; implicit sign-extensions.
48 (define_split
49 [(set (match_operand:DI 0 "register_operand")
50 (sign_extend:DI (div:SI (plus:SI (ashift:SI (subreg:SI (match_operand:DI 1 "register_operand") 0)
51 (match_operand:QI 2 "imm123_operand"))
52 (subreg:SI (match_operand:DI 3 "register_operand") 0))
53 (match_operand:SI 4 "register_operand"))))
54 (clobber (match_operand:DI 5 "register_operand"))]
55 "TARGET_64BIT && TARGET_ZBA"
56 [(set (match_dup 5) (plus:DI (ashift:DI (match_dup 1) (match_dup 2)) (match_dup 3)))
57 (set (match_dup 0) (sign_extend:DI (div:SI (subreg:SI (match_dup 5) 0) (match_dup 4))))])
58
59 ; Zba does not provide W-forms of sh[123]add(.uw)?, which leads to an
60 ; interesting irregularity: we can generate a signed 32-bit result
61 ; using slli(.uw)?+ addw, but a unsigned 32-bit result can be more
62 ; efficiently be generated as sh[123]add+zext.w (the .uw can be
63 ; dropped, if we zero-extend the output anyway).
64 ;
65 ; To enable this optimization, we split [ slli(.uw)?, addw, zext.w ]
66 ; into [ sh[123]add, zext.w ] for use during combine.
67 (define_split
68 [(set (match_operand:DI 0 "register_operand")
69 (zero_extend:DI (plus:SI (ashift:SI (subreg:SI (match_operand:DI 1 "register_operand") 0)
70 (match_operand:QI 2 "imm123_operand"))
71 (subreg:SI (match_operand:DI 3 "register_operand") 0))))]
72 "TARGET_64BIT && TARGET_ZBA"
73 [(set (match_dup 0) (plus:DI (ashift:DI (match_dup 1) (match_dup 2)) (match_dup 3)))
74 (set (match_dup 0) (zero_extend:DI (subreg:SI (match_dup 0) 0)))])
75
76 (define_split
77 [(set (match_operand:DI 0 "register_operand")
78 (zero_extend:DI (plus:SI (subreg:SI (and:DI (ashift:DI (match_operand:DI 1 "register_operand")
79 (match_operand:QI 2 "imm123_operand"))
80 (match_operand:DI 3 "consecutive_bits_operand")) 0)
81 (subreg:SI (match_operand:DI 4 "register_operand") 0))))]
82 "TARGET_64BIT && TARGET_ZBA
83 && riscv_shamt_matches_mask_p (INTVAL (operands[2]), INTVAL (operands[3]))
84 /* Ensure the mask includes all the bits in SImode. */
85 && ((INTVAL (operands[3]) & (HOST_WIDE_INT_1U << 31)) != 0)"
86 [(set (match_dup 0) (plus:DI (ashift:DI (match_dup 1) (match_dup 2)) (match_dup 4)))
87 (set (match_dup 0) (zero_extend:DI (subreg:SI (match_dup 0) 0)))])
88
89 ; Make sure that an andi followed by a sh[123]add remains a two instruction
90 ; sequence--and is not torn apart into slli, slri, add.
91 (define_insn_and_split "*andi_add.uw"
92 [(set (match_operand:DI 0 "register_operand" "=r")
93 (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
94 (match_operand:QI 2 "imm123_operand" "Ds3"))
95 (match_operand:DI 3 "consecutive_bits_operand" ""))
96 (match_operand:DI 4 "register_operand" "r")))
97 (clobber (match_scratch:DI 5 "=&r"))]
98 "TARGET_64BIT && TARGET_ZBA
99 && riscv_shamt_matches_mask_p (INTVAL (operands[2]), INTVAL (operands[3]))
100 && SMALL_OPERAND (INTVAL (operands[3]) >> INTVAL (operands[2]))"
101 "#"
102 "&& reload_completed"
103 [(set (match_dup 5) (and:DI (match_dup 1) (match_dup 3)))
104 (set (match_dup 0) (plus:DI (ashift:DI (match_dup 5) (match_dup 2))
105 (match_dup 4)))]
106 {
107 operands[3] = GEN_INT (INTVAL (operands[3]) >> INTVAL (operands[2]));
108 }
109 [(set_attr "type" "bitmanip")])
110
111 (define_insn "*shNadduw"
112 [(set (match_operand:DI 0 "register_operand" "=r")
113 (plus:DI
114 (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
115 (match_operand:QI 2 "imm123_operand" "Ds3"))
116 (match_operand 3 "immediate_operand" "n"))
117 (match_operand:DI 4 "register_operand" "r")))]
118 "TARGET_64BIT && TARGET_ZBA
119 && (INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff"
120 "sh%2add.uw\t%0,%1,%4"
121 [(set_attr "type" "bitmanip")
122 (set_attr "mode" "DI")])
123
124 ;; During combine, we may encounter an attempt to combine
125 ;; slli rtmp, rs, #imm
126 ;; zext.w rtmp, rtmp
127 ;; sh[123]add rd, rtmp, rs2
128 ;; which will lead to the immediate not satisfying the above constraints.
129 ;; By splitting the compound expression, we can simplify to a slli and a
130 ;; sh[123]add.uw.
131 (define_split
132 [(set (match_operand:DI 0 "register_operand")
133 (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand")
134 (match_operand:QI 2 "immediate_operand"))
135 (match_operand:DI 3 "consecutive_bits_operand"))
136 (match_operand:DI 4 "register_operand")))
137 (clobber (match_operand:DI 5 "register_operand"))]
138 "TARGET_64BIT && TARGET_ZBA"
139 [(set (match_dup 5) (ashift:DI (match_dup 1) (match_dup 6)))
140 (set (match_dup 0) (plus:DI (and:DI (ashift:DI (match_dup 5)
141 (match_dup 7))
142 (match_dup 8))
143 (match_dup 4)))]
144 {
145 unsigned HOST_WIDE_INT mask = UINTVAL (operands[3]);
146 /* scale: shift within the sh[123]add.uw */
147 unsigned HOST_WIDE_INT scale = 32 - clz_hwi (mask);
148 /* bias: pre-scale amount (i.e. the prior shift amount) */
149 int bias = ctz_hwi (mask) - scale;
150
151 /* If the bias + scale don't add up to operand[2], reject. */
152 if ((scale + bias) != UINTVAL (operands[2]))
153 FAIL;
154
155 /* If the shift-amount is out-of-range for sh[123]add.uw, reject. */
156 if ((scale < 1) || (scale > 3))
157 FAIL;
158
159 /* If there's no bias, the '*shNadduw' pattern should have matched. */
160 if (bias == 0)
161 FAIL;
162
163 operands[6] = GEN_INT (bias);
164 operands[7] = GEN_INT (scale);
165 operands[8] = GEN_INT (0xffffffffULL << scale);
166 })
167
168 (define_insn "*add.uw"
169 [(set (match_operand:DI 0 "register_operand" "=r")
170 (plus:DI (zero_extend:DI
171 (match_operand:SI 1 "register_operand" "r"))
172 (match_operand:DI 2 "register_operand" "r")))]
173 "TARGET_64BIT && TARGET_ZBA"
174 "add.uw\t%0,%1,%2"
175 [(set_attr "type" "bitmanip")
176 (set_attr "mode" "DI")])
177
178 (define_insn "*slliuw"
179 [(set (match_operand:DI 0 "register_operand" "=r")
180 (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
181 (match_operand:QI 2 "immediate_operand" "I"))
182 (match_operand 3 "immediate_operand" "n")))]
183 "TARGET_64BIT && TARGET_ZBA
184 && (INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff"
185 "slli.uw\t%0,%1,%2"
186 [(set_attr "type" "bitmanip")
187 (set_attr "mode" "DI")])
188
189 ;; Combine will reassociate the operands in the most useful way here. We
190 ;; just have to give it guidance on where to split the result to facilitate
191 ;; shNadd.uw generation.
192 (define_split
193 [(set (match_operand:DI 0 "register_operand")
194 (plus:DI (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand")
195 (match_operand:QI 2 "imm123_operand"))
196 (match_operand 3 "consecutive_bits32_operand"))
197 (match_operand:DI 4 "register_operand"))
198 (match_operand 5 "immediate_operand")))]
199 "TARGET_64BIT && TARGET_ZBA"
200 [(set (match_dup 0)
201 (plus:DI (and:DI (ashift:DI (match_dup 1) (match_dup 2))
202 (match_dup 3))
203 (match_dup 4)))
204 (set (match_dup 0) (plus:DI (match_dup 0) (match_dup 5)))])
205
206 ;; ZBB extension.
207
208 (define_expand "clzdi2"
209 [(set (match_operand:DI 0 "register_operand")
210 (clz:DI (match_operand:DI 1 "register_operand")))]
211 "TARGET_64BIT && (TARGET_ZBB || TARGET_XTHEADBB)")
212
213 (define_expand "clzsi2"
214 [(set (match_operand:SI 0 "register_operand")
215 (clz:SI (match_operand:SI 1 "register_operand")))]
216 "TARGET_ZBB || (!TARGET_64BIT && TARGET_XTHEADBB)")
217
218 (define_expand "ctz<mode>2"
219 [(set (match_operand:GPR 0 "register_operand")
220 (ctz:GPR (match_operand:GPR 1 "register_operand")))]
221 "TARGET_ZBB")
222
223 (define_expand "popcount<mode>2"
224 [(set (match_operand:GPR 0 "register_operand")
225 (popcount:GPR (match_operand:GPR 1 "register_operand")))]
226 "TARGET_ZBB")
227
228 (define_insn "<optab>_not<mode>3"
229 [(set (match_operand:X 0 "register_operand" "=r")
230 (bitmanip_bitwise:X (not:X (match_operand:X 1 "register_operand" "r"))
231 (match_operand:X 2 "register_operand" "r")))]
232 "TARGET_ZBB || TARGET_ZBKB"
233 "<insn>n\t%0,%2,%1"
234 [(set_attr "type" "bitmanip")
235 (set_attr "mode" "<X:MODE>")])
236
237 (define_insn_and_split "*<optab>_not_const<mode>"
238 [(set (match_operand:X 0 "register_operand" "=r")
239 (bitmanip_bitwise:X (not:X (match_operand:X 1 "register_operand" "r"))
240 (match_operand:X 2 "const_arith_operand" "I")))
241 (clobber (match_scratch:X 3 "=&r"))]
242 "(TARGET_ZBB || TARGET_ZBKB) && !TARGET_ZCB
243 && !optimize_function_for_size_p (cfun)"
244 "#"
245 "&& reload_completed"
246 [(set (match_dup 3) (match_dup 2))
247 (set (match_dup 0) (bitmanip_bitwise:X (not:X (match_dup 1)) (match_dup 3)))]
248 ""
249 [(set_attr "type" "bitmanip")])
250
251 ;; '(a >= 0) ? b : 0' is emitted branchless (from if-conversion). Without a
252 ;; bit of extra help for combine (i.e., the below split), we end up emitting
253 ;; not/srai/and instead of combining the not into an andn.
254 (define_split
255 [(set (match_operand:DI 0 "register_operand")
256 (and:DI (neg:DI (ge:DI (match_operand:DI 1 "register_operand")
257 (const_int 0)))
258 (match_operand:DI 2 "register_operand")))
259 (clobber (match_operand:DI 3 "register_operand"))]
260 "TARGET_ZBB || TARGET_ZBKB"
261 [(set (match_dup 3) (ashiftrt:DI (match_dup 1) (const_int 63)))
262 (set (match_dup 0) (and:DI (not:DI (match_dup 3)) (match_dup 2)))])
263
264 (define_insn "*xor_not<mode>"
265 [(set (match_operand:X 0 "register_operand" "=r")
266 (not:X (xor:X (match_operand:X 1 "register_operand" "r")
267 (match_operand:X 2 "register_operand" "r"))))]
268 "TARGET_ZBB || TARGET_ZBKB"
269 "xnor\t%0,%1,%2"
270 [(set_attr "type" "bitmanip")
271 (set_attr "mode" "<X:MODE>")])
272
273 (define_insn "*<bitmanip_optab>si2"
274 [(set (match_operand:SI 0 "register_operand" "=r")
275 (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r")))]
276 "TARGET_ZBB"
277 "<bitmanip_insn>%~\t%0,%1"
278 [(set_attr "type" "<bitmanip_insn>")
279 (set_attr "mode" "SI")])
280
281 (define_insn "*<bitmanip_optab>disi2"
282 [(set (match_operand:DI 0 "register_operand" "=r")
283 (any_extend:DI
284 (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r"))))]
285 "TARGET_64BIT && TARGET_ZBB"
286 "<bitmanip_insn>w\t%0,%1"
287 [(set_attr "type" "<bitmanip_insn>")
288 (set_attr "mode" "SI")])
289
290 ;; A SImode clz_ctz_pcnt may be extended to DImode via subreg.
291 (define_insn "*<bitmanip_optab>disi2_sext"
292 [(set (match_operand:DI 0 "register_operand" "=r")
293 (and:DI (subreg:DI
294 (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r")) 0)
295 (match_operand:DI 2 "const_int_operand")))]
296 "TARGET_64BIT && TARGET_ZBB && ((INTVAL (operands[2]) & 0x3f) == 0x3f)"
297 "<bitmanip_insn>w\t%0,%1"
298 [(set_attr "type" "<bitmanip_insn>")
299 (set_attr "mode" "SI")])
300
301 (define_insn "*<bitmanip_optab>di2"
302 [(set (match_operand:DI 0 "register_operand" "=r")
303 (clz_ctz_pcnt:DI (match_operand:DI 1 "register_operand" "r")))]
304 "TARGET_64BIT && TARGET_ZBB"
305 "<bitmanip_insn>\t%0,%1"
306 [(set_attr "type" "<bitmanip_insn>")
307 (set_attr "mode" "DI")])
308
309 (define_insn "*zero_extendhi<GPR:mode>2_bitmanip"
310 [(set (match_operand:GPR 0 "register_operand" "=r,r")
311 (zero_extend:GPR (match_operand:HI 1 "nonimmediate_operand" "r,m")))]
312 "TARGET_ZBB && !TARGET_XTHEADMEMIDX"
313 "@
314 zext.h\t%0,%1
315 lhu\t%0,%1"
316 [(set_attr "type" "bitmanip,load")
317 (set_attr "mode" "<GPR:MODE>")])
318
319 (define_insn "*extend<SHORT:mode><SUPERQI:mode>2_bitmanip"
320 [(set (match_operand:SUPERQI 0 "register_operand" "=r,r")
321 (sign_extend:SUPERQI
322 (match_operand:SHORT 1 "nonimmediate_operand" " r,m")))]
323 "TARGET_ZBB && !TARGET_XTHEADMEMIDX"
324 "@
325 sext.<SHORT:size>\t%0,%1
326 l<SHORT:size>\t%0,%1"
327 [(set_attr "type" "bitmanip,load")
328 (set_attr "mode" "<SUPERQI:MODE>")])
329
330 (define_expand "rotrdi3"
331 [(set (match_operand:DI 0 "register_operand")
332 (rotatert:DI (match_operand:DI 1 "register_operand")
333 (match_operand:QI 2 "arith_operand")))]
334 "TARGET_64BIT && (TARGET_ZBB || TARGET_XTHEADBB || TARGET_ZBKB)"
335 {
336 if (TARGET_XTHEADBB && !immediate_operand (operands[2], VOIDmode))
337 FAIL;
338 })
339
340 (define_insn "*rotrsi3"
341 [(set (match_operand:SI 0 "register_operand" "=r")
342 (rotatert:SI (match_operand:SI 1 "register_operand" "r")
343 (match_operand:QI 2 "arith_operand" "rI")))]
344 "TARGET_ZBB || TARGET_ZBKB"
345 "ror%i2%~\t%0,%1,%2"
346 [(set_attr "type" "bitmanip")])
347
348 (define_expand "rotrsi3"
349 [(set (match_operand:SI 0 "register_operand" "=r")
350 (rotatert:SI (match_operand:SI 1 "register_operand" "r")
351 (match_operand:QI 2 "arith_operand" "rI")))]
352 "TARGET_ZBB || TARGET_ZBKB || TARGET_XTHEADBB"
353 {
354 if (TARGET_XTHEADBB && !immediate_operand (operands[2], VOIDmode))
355 FAIL;
356 if (TARGET_64BIT && register_operand (operands[2], QImode))
357 {
358 rtx t = gen_reg_rtx (DImode);
359 emit_insn (gen_rotrsi3_sext (t, operands[1], operands[2]));
360 t = gen_lowpart (SImode, t);
361 SUBREG_PROMOTED_VAR_P (t) = 1;
362 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
363 emit_move_insn (operands[0], t);
364 DONE;
365 }
366 })
367
368 (define_insn "*rotrdi3"
369 [(set (match_operand:DI 0 "register_operand" "=r")
370 (rotatert:DI (match_operand:DI 1 "register_operand" "r")
371 (match_operand:QI 2 "arith_operand" "rI")))]
372 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
373 "ror%i2\t%0,%1,%2"
374 [(set_attr "type" "bitmanip")])
375
376 (define_insn "rotrsi3_sext"
377 [(set (match_operand:DI 0 "register_operand" "=r")
378 (sign_extend:DI (rotatert:SI (match_operand:SI 1 "register_operand" "r")
379 (match_operand:QI 2 "arith_operand" "rI"))))]
380 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
381 "ror%i2%~\t%0,%1,%2"
382 [(set_attr "type" "bitmanip")])
383
384 (define_insn "*rotlsi3"
385 [(set (match_operand:SI 0 "register_operand" "=r")
386 (rotate:SI (match_operand:SI 1 "register_operand" "r")
387 (match_operand:QI 2 "register_operand" "r")))]
388 "TARGET_ZBB || TARGET_ZBKB"
389 "rol%~\t%0,%1,%2"
390 [(set_attr "type" "bitmanip")])
391
392 (define_expand "rotlsi3"
393 [(set (match_operand:SI 0 "register_operand" "=r")
394 (rotate:SI (match_operand:SI 1 "register_operand" "r")
395 (match_operand:QI 2 "register_operand" "r")))]
396 "TARGET_ZBB || TARGET_ZBKB"
397 {
398 if (TARGET_64BIT)
399 {
400 rtx t = gen_reg_rtx (DImode);
401 emit_insn (gen_rotlsi3_sext (t, operands[1], operands[2]));
402 t = gen_lowpart (SImode, t);
403 SUBREG_PROMOTED_VAR_P (t) = 1;
404 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
405 emit_move_insn (operands[0], t);
406 DONE;
407 }
408 })
409
410 (define_insn "rotldi3"
411 [(set (match_operand:DI 0 "register_operand" "=r")
412 (rotate:DI (match_operand:DI 1 "register_operand" "r")
413 (match_operand:QI 2 "register_operand" "r")))]
414 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
415 "rol\t%0,%1,%2"
416 [(set_attr "type" "bitmanip")])
417
418 (define_insn "rotlsi3_sext"
419 [(set (match_operand:DI 0 "register_operand" "=r")
420 (sign_extend:DI (rotate:SI (match_operand:SI 1 "register_operand" "r")
421 (match_operand:QI 2 "register_operand" "r"))))]
422 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
423 "rolw\t%0,%1,%2"
424 [(set_attr "type" "bitmanip")])
425
426 (define_insn "*<bitmanip_optab><mode>3_mask"
427 [(set (match_operand:X 0 "register_operand" "=r")
428 (bitmanip_rotate:X
429 (match_operand:X 1 "register_operand" "r")
430 (match_operator 4 "subreg_lowpart_operator"
431 [(and:X (match_operand:X 2 "register_operand" "r")
432 (match_operand 3 "<X:shiftm1>" "<X:shiftm1p>"))])))]
433 "TARGET_ZBB || TARGET_ZBKB"
434 "<bitmanip_insn>\t%0,%1,%2"
435 [(set_attr "type" "bitmanip")
436 (set_attr "mode" "<X:MODE>")])
437
438 (define_insn "*<bitmanip_optab>3_mask_si"
439 [(set (match_operand:SI 0 "register_operand" "=r")
440 (bitmanip_rotate:SI
441 (match_operand:SI 1 "register_operand" "r")
442 (match_operator 3 "subreg_lowpart_operator"
443 [(and:X (match_operand:SI 2 "register_operand" "r")
444 (const_int 31))])))]
445 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
446 "<bitmanip_insn>w\t%0,%1,%2"
447 [(set_attr "type" "bitmanip")
448 (set_attr "mode" "SI")])
449
450 (define_insn "*<bitmanip_optab>si3_sext_mask"
451 [(set (match_operand:DI 0 "register_operand" "=r")
452 (sign_extend:DI
453 (bitmanip_rotate:SI
454 (match_operand:SI 1 "register_operand" "r")
455 (match_operator 3 "subreg_lowpart_operator"
456 [(and:X (match_operand:GPR 2 "register_operand" "r")
457 (const_int 31))]))))]
458 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
459 "<bitmanip_insn>w\t%0,%1,%2"
460 [(set_attr "type" "bitmanip")
461 (set_attr "mode" "DI")])
462
463 ;; orc.b (or-combine) is added as an unspec for the benefit of the support
464 ;; for optimized string functions (such as strcmp).
465 (define_insn "orcb<mode>2"
466 [(set (match_operand:GPR 0 "register_operand" "=r")
467 (unspec:GPR [(match_operand:GPR 1 "register_operand" "r")] UNSPEC_ORC_B))]
468 "TARGET_ZBB"
469 "orc.b\t%0,%1"
470 [(set_attr "type" "bitmanip")])
471
472 (define_expand "bswapdi2"
473 [(set (match_operand:DI 0 "register_operand")
474 (bswap:DI (match_operand:DI 1 "register_operand")))]
475 "TARGET_64BIT && (TARGET_ZBB || TARGET_XTHEADBB || TARGET_ZBKB)")
476
477 (define_expand "bswapsi2"
478 [(set (match_operand:SI 0 "register_operand")
479 (bswap:SI (match_operand:SI 1 "register_operand")))]
480 "TARGET_ZBB || TARGET_ZBKB || TARGET_XTHEADBB"
481 {
482 /* Expose bswapsi2 on TARGET_64BIT so that the gimple store
483 merging pass will create suitable bswap insns. We can actually
484 just FAIL that case when generating RTL and let the generic code
485 handle it. */
486 if (TARGET_64BIT && !TARGET_XTHEADBB)
487 FAIL;
488 })
489
490
491 (define_insn "*bswap<mode>2"
492 [(set (match_operand:X 0 "register_operand" "=r")
493 (bswap:X (match_operand:X 1 "register_operand" "r")))]
494 "TARGET_ZBB || TARGET_ZBKB"
495 "rev8\t%0,%1"
496 [(set_attr "type" "bitmanip")])
497
498 ;; HI bswap can be emulated using SI/DI bswap followed
499 ;; by a logical shift right
500 ;; SI bswap for TARGET_64BIT is already similarly in
501 ;; the common code.
502 (define_expand "bswaphi2"
503 [(set (match_operand:HI 0 "register_operand" "=r")
504 (bswap:HI (match_operand:HI 1 "register_operand" "r")))]
505 "TARGET_ZBB"
506 {
507 rtx tmp = gen_reg_rtx (word_mode);
508 rtx newop1 = gen_lowpart (word_mode, operands[1]);
509 if (TARGET_64BIT)
510 emit_insn (gen_bswapdi2 (tmp, newop1));
511 else
512 emit_insn (gen_bswapsi2 (tmp, newop1));
513 rtx tmp1 = gen_reg_rtx (word_mode);
514 if (TARGET_64BIT)
515 emit_insn (gen_lshrdi3 (tmp1, tmp, GEN_INT (64 - 16)));
516 else
517 emit_insn (gen_lshrsi3 (tmp1, tmp, GEN_INT (32 - 16)));
518 emit_move_insn (operands[0], gen_lowpart (HImode, tmp1));
519 DONE;
520 })
521
522 (define_expand "<bitmanip_optab>di3"
523 [(set (match_operand:DI 0 "register_operand" "=r")
524 (bitmanip_minmax:DI (match_operand:DI 1 "register_operand" "r")
525 (match_operand:DI 2 "register_operand" "r")))]
526 "TARGET_64BIT && TARGET_ZBB")
527
528 (define_expand "<bitmanip_optab>si3"
529 [(set (match_operand:SI 0 "register_operand" "=r")
530 (bitmanip_minmax:SI (match_operand:SI 1 "register_operand" "r")
531 (match_operand:SI 2 "register_operand" "r")))]
532 "TARGET_ZBB"
533 {
534 if (TARGET_64BIT)
535 {
536 rtx t = gen_reg_rtx (DImode);
537 operands[1] = force_reg (DImode, gen_rtx_SIGN_EXTEND (DImode, operands[1]));
538 operands[2] = force_reg (DImode, gen_rtx_SIGN_EXTEND (DImode, operands[2]));
539 emit_insn (gen_<bitmanip_optab>di3 (t, operands[1], operands[2]));
540 emit_move_insn (operands[0], gen_lowpart (SImode, t));
541 DONE;
542 }
543 })
544
545 (define_insn "*<bitmanip_optab><mode>3"
546 [(set (match_operand:X 0 "register_operand" "=r")
547 (bitmanip_minmax:X (match_operand:X 1 "register_operand" "r")
548 (match_operand:X 2 "reg_or_0_operand" "rJ")))]
549 "TARGET_ZBB"
550 "<bitmanip_insn>\t%0,%1,%z2"
551 [(set_attr "type" "<bitmanip_insn>")])
552
553 ;; Optimize the common case of a SImode min/max against a constant
554 ;; that is safe both for sign- and zero-extension.
555 (define_split
556 [(set (match_operand:DI 0 "register_operand")
557 (sign_extend:DI
558 (subreg:SI
559 (bitmanip_minmax:DI (zero_extend:DI
560 (match_operand:SI 1 "register_operand"))
561 (match_operand:DI 2 "immediate_operand")) 0)))
562 (clobber (match_operand:DI 3 "register_operand"))
563 (clobber (match_operand:DI 4 "register_operand"))]
564 "TARGET_64BIT && TARGET_ZBB && sext_hwi (INTVAL (operands[2]), 32) >= 0"
565 [(set (match_dup 0) (<uminmax_optab>:DI (match_dup 4) (match_dup 3)))]
566 "
567 {
568 /* Load the constant into a register. */
569 emit_move_insn (operands[3], operands[2]);
570
571 /* If operands[1] is a sign extended SUBREG, then we can use it
572 directly. Otherwise extend it into another temporary. */
573 if (SUBREG_P (operands[1])
574 && SUBREG_PROMOTED_VAR_P (operands[1])
575 && SUBREG_PROMOTED_SIGNED_P (operands[1]))
576 operands[4] = SUBREG_REG (operands[1]);
577 else
578 emit_move_insn (operands[4], gen_rtx_SIGN_EXTEND (DImode, operands[1]));
579
580 /* The minmax is actually emitted from the split pattern. */
581 }")
582
583 ;; ZBS extension.
584
585 (define_insn "*<bit_optab><mode>"
586 [(set (match_operand:X 0 "register_operand" "=r")
587 (any_or:X (ashift:X (const_int 1)
588 (match_operand:QI 2 "register_operand" "r"))
589 (match_operand:X 1 "register_operand" "r")))]
590 "TARGET_ZBS"
591 "<bit_optab>\t%0,%1,%2"
592 [(set_attr "type" "bitmanip")])
593
594 (define_insn "*<bit_optab><mode>_mask"
595 [(set (match_operand:X 0 "register_operand" "=r")
596 (any_or:X
597 (ashift:X
598 (const_int 1)
599 (subreg:QI
600 (and:X (match_operand:X 2 "register_operand" "r")
601 (match_operand 3 "<X:shiftm1>" "<X:shiftm1p>")) 0))
602 (match_operand:X 1 "register_operand" "r")))]
603 "TARGET_ZBS"
604 "<bit_optab>\t%0,%1,%2"
605 [(set_attr "type" "bitmanip")])
606
607 (define_insn "*bset<mode>_1"
608 [(set (match_operand:X 0 "register_operand" "=r")
609 (ashift:X (const_int 1)
610 (match_operand:QI 1 "register_operand" "r")))]
611 "TARGET_ZBS"
612 "bset\t%0,x0,%1"
613 [(set_attr "type" "bitmanip")])
614
615 ;; The result will always have bits 32..63 clear, so the zero-extend
616 ;; is redundant. We could split it to bset<mode>_1, but it seems
617 ;; unnecessary.
618 (define_insn "*bsetdi_2"
619 [(set (match_operand:DI 0 "register_operand" "=r")
620 (zero_extend:DI
621 (ashift:SI (const_int 1)
622 (match_operand:QI 1 "register_operand" "r"))))]
623 "TARGET_64BIT && TARGET_ZBS"
624 "bset\t%0,x0,%1"
625 [(set_attr "type" "bitmanip")])
626
627 ;; These two splitters take advantage of the limited range of the
628 ;; shift constant. With the limited range we know the SImode sign
629 ;; bit is never set, thus we can treat this as zero extending and
630 ;; generate the bsetdi_2 pattern.
631 (define_insn_and_split ""
632 [(set (match_operand:DI 0 "register_operand" "=r")
633 (any_extend:DI
634 (ashift:SI (const_int 1)
635 (subreg:QI
636 (and:DI (not:DI (match_operand:DI 1 "register_operand" "r"))
637 (match_operand 2 "const_int_operand")) 0))))
638 (clobber (match_scratch:X 3 "=&r"))]
639 "TARGET_64BIT
640 && TARGET_ZBS
641 && (TARGET_ZBB || TARGET_ZBKB)
642 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
643 "#"
644 "&& reload_completed"
645 [(set (match_dup 3) (match_dup 2))
646 (set (match_dup 3) (and:DI (not:DI (match_dup 1)) (match_dup 3)))
647 (set (match_dup 0) (zero_extend:DI
648 (ashift:SI (const_int 1) (match_dup 4))))]
649 {
650 operands[4] = gen_lowpart (QImode, operands[3]);
651 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f);
652 }
653 [(set_attr "type" "bitmanip")])
654
655 (define_insn_and_split ""
656 [(set (match_operand:DI 0 "register_operand" "=r")
657 (any_extend:DI
658 (ashift:SI (const_int 1)
659 (subreg:QI
660 (and:DI (match_operand:DI 1 "register_operand" "r")
661 (match_operand 2 "const_int_operand")) 0))))]
662 "TARGET_64BIT
663 && TARGET_ZBS
664 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
665 "#"
666 "&& 1"
667 [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 2)))
668 (set (match_dup 0) (zero_extend:DI (ashift:SI
669 (const_int 1)
670 (subreg:QI (match_dup 0) 0))))]
671 { operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f); }
672 [(set_attr "type" "bitmanip")])
673
674 ;; Similarly two patterns for IOR/XOR generating bset/binv to
675 ;; manipulate a bit in a register
676 (define_insn_and_split ""
677 [(set (match_operand:DI 0 "register_operand" "=r")
678 (any_or:DI
679 (any_extend:DI
680 (ashift:SI
681 (const_int 1)
682 (subreg:QI
683 (and:DI (not:DI (match_operand:DI 1 "register_operand" "r"))
684 (match_operand 2 "const_int_operand")) 0)))
685 (match_operand:DI 3 "register_operand" "r")))
686 (clobber (match_scratch:X 4 "=&r"))]
687 "TARGET_64BIT
688 && TARGET_ZBS
689 && (TARGET_ZBB || TARGET_ZBKB)
690 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
691 "#"
692 "&& reload_completed"
693 [(set (match_dup 4) (match_dup 2))
694 (set (match_dup 4) (and:DI (not:DI (match_dup 1)) (match_dup 4)))
695 (set (match_dup 0) (any_or:DI (ashift:DI (const_int 1) (match_dup 5)) (match_dup 3)))]
696 {
697 operands[5] = gen_lowpart (QImode, operands[4]);
698 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f);
699 }
700 [(set_attr "type" "bitmanip")])
701
702 (define_insn_and_split ""
703 [(set (match_operand:DI 0 "register_operand" "=r")
704 (any_or:DI
705 (any_extend:DI
706 (ashift:SI
707 (const_int 1)
708 (subreg:QI
709 (and:DI (match_operand:DI 1 "register_operand" "r")
710 (match_operand 2 "const_int_operand")) 0)))
711 (match_operand:DI 3 "register_operand" "r")))
712 (clobber (match_scratch:X 4 "=&r"))]
713 "TARGET_64BIT
714 && TARGET_ZBS
715 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
716 "#"
717 "&& reload_completed"
718 [(set (match_dup 4) (and:DI (match_dup 1) (match_dup 2)))
719 (set (match_dup 0) (any_or:DI (ashift:DI (const_int 1) (subreg:QI (match_dup 4) 0)) (match_dup 3)))]
720 { operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f); }
721 [(set_attr "type" "bitmanip")])
722
723 ;; Similarly two patterns for AND generating bclr to
724 ;; manipulate a bit in a register
725 (define_insn_and_split ""
726 [(set (match_operand:DI 0 "register_operand" "=r")
727 (and:DI
728 (not:DI
729 (any_extend:DI
730 (ashift:SI
731 (const_int 1)
732 (subreg:QI
733 (and:DI (not:DI (match_operand:DI 1 "register_operand" "r"))
734 (match_operand 2 "const_int_operand")) 0))))
735 (match_operand:DI 3 "register_operand" "r")))
736 (clobber (match_scratch:X 4 "=&r"))]
737 "TARGET_64BIT
738 && TARGET_ZBS
739 && (TARGET_ZBB || TARGET_ZBKB)
740 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
741 "#"
742 "&& reload_completed"
743 [(set (match_dup 4) (match_dup 2))
744 (set (match_dup 4) (and:DI (not:DI (match_dup 1)) (match_dup 4)))
745 (set (match_dup 0) (and:DI (rotate:DI (const_int -2) (match_dup 5)) (match_dup 3)))]
746 {
747 operands[5] = gen_lowpart (QImode, operands[4]);
748 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f);
749 }
750 [(set_attr "type" "bitmanip")])
751
752
753 (define_insn_and_split ""
754 [(set (match_operand:DI 0 "register_operand" "=r")
755 (and:DI
756 (not:DI
757 (any_extend:DI
758 (ashift:SI
759 (const_int 1)
760 (subreg:QI
761 (and:DI (match_operand:DI 1 "register_operand" "r")
762 (match_operand 2 "const_int_operand")) 0))))
763 (match_operand:DI 3 "register_operand" "r")))
764 (clobber (match_scratch:X 4 "=&r"))]
765 "TARGET_64BIT
766 && TARGET_ZBS
767 && (INTVAL (operands[2]) & 0x1f) != 0x1f"
768 "#"
769 "&& reload_completed"
770 [(set (match_dup 4) (and:DI (match_dup 1) (match_dup 2)))
771 (set (match_dup 0) (and:DI (rotate:DI (const_int -2) (match_dup 5)) (match_dup 3)))]
772 {
773 operands[5] = gen_lowpart (QImode, operands[4]);
774 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f);
775 }
776 [(set_attr "type" "bitmanip")])
777
778 (define_insn "*bset<mode>_1_mask"
779 [(set (match_operand:X 0 "register_operand" "=r")
780 (ashift:X (const_int 1)
781 (subreg:QI
782 (and:X (match_operand:X 1 "register_operand" "r")
783 (match_operand 2 "<X:shiftm1>" "<X:shiftm1p>")) 0)))]
784 "TARGET_ZBS"
785 "bset\t%0,x0,%1"
786 [(set_attr "type" "bitmanip")])
787
788 (define_insn "*<bit_optab>i<mode>"
789 [(set (match_operand:X 0 "register_operand" "=r")
790 (any_or:X (match_operand:X 1 "register_operand" "r")
791 (match_operand:X 2 "single_bit_mask_operand" "DbS")))]
792 "TARGET_ZBS"
793 "<bit_optab>i\t%0,%1,%S2"
794 [(set_attr "type" "bitmanip")])
795
796 ;; We can easily handle zero extensions
797 (define_split
798 [(set (match_operand:DI 0 "register_operand")
799 (any_or:DI (zero_extend:DI
800 (ashift:SI (const_int 1)
801 (match_operand:QI 1 "register_operand")))
802 (match_operand:DI 2 "single_bit_mask_operand")))
803 (clobber (match_operand:DI 3 "register_operand"))]
804 "TARGET_64BIT && TARGET_ZBS"
805 [(set (match_dup 3)
806 (match_dup 2))
807 (set (match_dup 0)
808 (any_or:DI (ashift:DI (const_int 1) (match_dup 1))
809 (match_dup 3)))])
810
811 ;; Yet another form of a bset/bclr that can be created by combine.
812 (define_insn "*bsetclr_zero_extract"
813 [(set (zero_extract:X (match_operand:X 0 "register_operand" "+r")
814 (const_int 1)
815 (zero_extend:X
816 (match_operand:QI 1 "register_operand" "r")))
817 (match_operand 2 "immediate_operand" "n"))]
818 "TARGET_ZBS
819 && (operands[2] == CONST0_RTX (<MODE>mode)
820 || operands[2] == CONST1_RTX (<MODE>mode))"
821 {
822 return (operands[2] == CONST0_RTX (<MODE>mode)
823 ? "bclr\t%0,%0,%1"
824 : "bset\t%0,%0,%1");
825 }
826 [(set_attr "type" "bitmanip")])
827
828 (define_insn "*bclr<mode>"
829 [(set (match_operand:X 0 "register_operand" "=r")
830 (and:X (rotate:X (const_int -2)
831 (match_operand:QI 2 "register_operand" "r"))
832 (match_operand:X 1 "register_operand" "r")))]
833 "TARGET_ZBS"
834 "bclr\t%0,%1,%2"
835 [(set_attr "type" "bitmanip")])
836
837 (define_insn "*bclri<mode>"
838 [(set (match_operand:X 0 "register_operand" "=r")
839 (and:X (match_operand:X 1 "register_operand" "r")
840 (match_operand:X 2 "not_single_bit_mask_operand" "DnS")))]
841 "TARGET_ZBS"
842 "bclri\t%0,%1,%T2"
843 [(set_attr "type" "bitmanip")])
844
845 ;; In case we have "val & ~IMM" where ~IMM has 2 bits set.
846 (define_insn_and_split "*bclri<mode>_nottwobits"
847 [(set (match_operand:X 0 "register_operand" "=r")
848 (and:X (match_operand:X 1 "register_operand" "r")
849 (match_operand:X 2 "const_nottwobits_not_arith_operand" "i")))]
850 "TARGET_ZBS && !paradoxical_subreg_p (operands[1])"
851 "#"
852 "&& reload_completed"
853 [(set (match_dup 0) (and:X (match_dup 1) (match_dup 3)))
854 (set (match_dup 0) (and:X (match_dup 0) (match_dup 4)))]
855 {
856 unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]);
857 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
858
859 operands[3] = GEN_INT (~bits | topbit);
860 operands[4] = GEN_INT (~topbit);
861 }
862 [(set_attr "type" "bitmanip")])
863
864 ;; In case of a paradoxical subreg, the sign bit and the high bits are
865 ;; not allowed to be changed
866 (define_insn_and_split "*bclridisi_nottwobits"
867 [(set (match_operand:DI 0 "register_operand" "=r")
868 (and:DI (match_operand:DI 1 "register_operand" "r")
869 (match_operand:DI 2 "const_nottwobits_not_arith_operand" "i")))]
870 "TARGET_64BIT && TARGET_ZBS
871 && clz_hwi (~UINTVAL (operands[2])) > 33"
872 "#"
873 "&& reload_completed"
874 [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 3)))
875 (set (match_dup 0) (and:DI (match_dup 0) (match_dup 4)))]
876 {
877 unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]);
878 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
879
880 operands[3] = GEN_INT (~bits | topbit);
881 operands[4] = GEN_INT (~topbit);
882 }
883 [(set_attr "type" "bitmanip")])
884
885 ;; An outer AND with a constant where bits 31..63 are 0 can be seen as
886 ;; a virtual zero extension from 31 to 64 bits.
887 (define_split
888 [(set (match_operand:DI 0 "register_operand")
889 (and:DI (not:DI (subreg:DI
890 (ashift:SI (const_int 1)
891 (match_operand:QI 1 "register_operand")) 0))
892 (match_operand:DI 2 "arith_operand")))
893 (clobber (match_operand:DI 3 "register_operand"))]
894 "TARGET_64BIT && TARGET_ZBS
895 && clz_hwi (INTVAL (operands[2])) >= 33"
896 [(set (match_dup 3)
897 (match_dup 2))
898 (set (match_dup 0)
899 (and:DI (rotate:DI (const_int -2) (match_dup 1))
900 (match_dup 3)))])
901
902 (define_insn "*bext<mode>"
903 [(set (match_operand:X 0 "register_operand" "=r")
904 (zero_extract:X (match_operand:X 1 "register_operand" "r")
905 (const_int 1)
906 (zero_extend:X
907 (match_operand:QI 2 "register_operand" "r"))))]
908 "TARGET_ZBS"
909 "bext\t%0,%1,%2"
910 [(set_attr "type" "bitmanip")])
911
912 ;; This is a bext followed by a seqz. Normally this would be a 3->2 split
913 ;; But the and-not pattern with a constant operand is a define_insn_and_split,
914 ;; so this looks like a 2->2 split, which combine rejects. So implement it
915 ;; as a define_insn_and_split as well.
916 (define_insn_and_split "*bextseqzdisi"
917 [(set (match_operand:DI 0 "register_operand" "=r")
918 (and:DI
919 (not:DI
920 (subreg:DI
921 (lshiftrt:SI
922 (match_operand:SI 1 "register_operand" "r")
923 (match_operand:QI 2 "register_operand" "r")) 0))
924 (const_int 1)))]
925 "TARGET_64BIT && TARGET_ZBS"
926 "#"
927 "&& 1"
928 [(set (match_dup 0)
929 (zero_extract:DI (match_dup 1)
930 (const_int 1)
931 (zero_extend:DI (match_dup 2))))
932 (set (match_dup 0) (eq:DI (match_dup 0) (const_int 0)))]
933 "operands[1] = gen_lowpart (word_mode, operands[1]);"
934 [(set_attr "type" "bitmanip")])
935
936 (define_insn_and_split "*bextseqzdisi"
937 [(set (match_operand:X 0 "register_operand" "=r")
938 (and:X
939 (not:X
940 (lshiftrt:X
941 (match_operand:X 1 "register_operand" "r")
942 (match_operand:QI 2 "register_operand" "r")))
943 (const_int 1)))]
944 "TARGET_ZBS"
945 "#"
946 "&& 1"
947 [(set (match_dup 0)
948 (zero_extract:X (match_dup 1)
949 (const_int 1)
950 (zero_extend:X (match_dup 2))))
951 (set (match_dup 0) (eq:X (match_dup 0) (const_int 0)))]
952 "operands[1] = gen_lowpart (word_mode, operands[1]);"
953 [(set_attr "type" "bitmanip")])
954
955 ;; The logical-and against 0x1 implicitly extends the result. So we can treat
956 ;; an SImode bext as-if it's DImode without any explicit extension.
957 (define_insn "*bextdisi"
958 [(set (match_operand:DI 0 "register_operand" "=r")
959 (and:DI (subreg:DI (lshiftrt:SI
960 (match_operand:SI 1 "register_operand" "r")
961 (match_operand:QI 2 "register_operand" "r")) 0)
962 (const_int 1)))]
963 "TARGET_64BIT && TARGET_ZBS"
964 "bext\t%0,%1,%2"
965 [(set_attr "type" "bitmanip")])
966
967 ;; When performing `(a & (1UL << bitno)) ? 0 : -1` the combiner
968 ;; usually has the `bitno` typed as X-mode (i.e. no further
969 ;; zero-extension is performed around the bitno).
970 (define_insn "*bext<mode>"
971 [(set (match_operand:X 0 "register_operand" "=r")
972 (zero_extract:X (match_operand:X 1 "register_operand" "r")
973 (const_int 1)
974 (match_operand:X 2 "register_operand" "r")))]
975 "TARGET_ZBS"
976 "bext\t%0,%1,%2"
977 [(set_attr "type" "bitmanip")])
978
979 (define_insn "*bexti"
980 [(set (match_operand:X 0 "register_operand" "=r")
981 (zero_extract:X (match_operand:X 1 "register_operand" "r")
982 (const_int 1)
983 (match_operand 2 "immediate_operand" "n")))]
984 "TARGET_ZBS && UINTVAL (operands[2]) < GET_MODE_BITSIZE (<MODE>mode)"
985 "bexti\t%0,%1,%2"
986 [(set_attr "type" "bitmanip")])
987
988 ;; Split for "(a & (1 << BIT_NO)) ? 0 : 1":
989 ;; We avoid reassociating "(~(a >> BIT_NO)) & 1" into "((~a) >> BIT_NO) & 1",
990 ;; so we don't have to use a temporary. Instead we extract the bit and then
991 ;; invert bit 0 ("a ^ 1") only.
992 (define_split
993 [(set (match_operand:X 0 "register_operand")
994 (and:X (not:X (lshiftrt:X (match_operand:X 1 "register_operand")
995 (match_operand:QI 2 "register_operand")))
996 (const_int 1)))]
997 "TARGET_ZBS"
998 [(set (match_dup 0) (zero_extract:X (match_dup 1)
999 (const_int 1)
1000 (match_dup 2)))
1001 (set (match_dup 0) (xor:X (match_dup 0) (const_int 1)))]
1002 "operands[2] = gen_lowpart (<MODE>mode, operands[2]);")
1003
1004 ;; We can create a polarity-reversed mask (i.e. bit N -> { set = 0, clear = -1 })
1005 ;; using a bext(i) followed by an addi instruction.
1006 ;; This splits the canonical representation of "(a & (1 << BIT_NO)) ? 0 : -1".
1007 (define_split
1008 [(set (match_operand:GPR 0 "register_operand")
1009 (neg:GPR (eq:GPR (zero_extract:GPR (match_operand:GPR 1 "register_operand")
1010 (const_int 1)
1011 (match_operand 2))
1012 (const_int 0))))]
1013 "TARGET_ZBS"
1014 [(set (match_dup 0) (zero_extract:GPR (match_dup 1) (const_int 1) (match_dup 2)))
1015 (set (match_dup 0) (plus:GPR (match_dup 0) (const_int -1)))])
1016
1017 ;; Catch those cases where we can use a bseti/binvi + ori/xori or
1018 ;; bseti/binvi + bseti/binvi instead of a lui + addi + or/xor sequence.
1019 (define_insn_and_split "*<or_optab>i<mode>_extrabit"
1020 [(set (match_operand:X 0 "register_operand" "=r")
1021 (any_or:X (match_operand:X 1 "register_operand" "r")
1022 (match_operand:X 2 "uimm_extra_bit_or_twobits" "i")))]
1023 "TARGET_ZBS && !single_bit_mask_operand (operands[2], VOIDmode)"
1024 "#"
1025 "&& reload_completed"
1026 [(set (match_dup 0) (<or_optab>:X (match_dup 1) (match_dup 3)))
1027 (set (match_dup 0) (<or_optab>:X (match_dup 0) (match_dup 4)))]
1028 {
1029 unsigned HOST_WIDE_INT bits = UINTVAL (operands[2]);
1030 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
1031
1032 operands[3] = GEN_INT (bits &~ topbit);
1033 operands[4] = GEN_INT (topbit);
1034 }
1035 [(set_attr "type" "bitmanip")])
1036
1037 ;; Same to use blcri + andi and blcri + bclri
1038 (define_insn_and_split "*andi<mode>_extrabit"
1039 [(set (match_operand:X 0 "register_operand" "=r")
1040 (and:X (match_operand:X 1 "register_operand" "r")
1041 (match_operand:X 2 "not_uimm_extra_bit_or_nottwobits" "i")))]
1042 "TARGET_ZBS && !not_single_bit_mask_operand (operands[2], VOIDmode)"
1043 "#"
1044 "&& reload_completed"
1045 [(set (match_dup 0) (and:X (match_dup 1) (match_dup 3)))
1046 (set (match_dup 0) (and:X (match_dup 0) (match_dup 4)))]
1047 {
1048 unsigned HOST_WIDE_INT bits = UINTVAL (operands[2]);
1049 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (~bits);
1050
1051 operands[3] = GEN_INT (bits | topbit);
1052 operands[4] = GEN_INT (~topbit);
1053 }
1054 [(set_attr "type" "bitmanip")])
1055
1056 ;; If we have the ZBA extension, then we can clear the upper half of a 64
1057 ;; bit object with a zext.w. So if we have AND where the constant would
1058 ;; require synthesis of two or more instructions, but 32->64 sign extension
1059 ;; of the constant is a simm12, then we can use zext.w+andi. If the adjusted
1060 ;; constant is a single bit constant, then we can use zext.w+bclri
1061 ;;
1062 ;; With the mvconst_internal pattern claiming a single insn to synthesize
1063 ;; constants, this must be a define_insn_and_split.
1064 (define_insn_and_split ""
1065 [(set (match_operand:DI 0 "register_operand" "=r")
1066 (and:DI (match_operand:DI 1 "register_operand" "r")
1067 (match_operand 2 "const_int_operand" "n")))]
1068 "TARGET_64BIT
1069 && TARGET_ZBA
1070 && !paradoxical_subreg_p (operands[1])
1071 /* Only profitable if synthesis takes more than one insn. */
1072 && riscv_const_insns (operands[2], false) != 1
1073 /* We need the upper half to be zero. */
1074 && (INTVAL (operands[2]) & HOST_WIDE_INT_C (0xffffffff00000000)) == 0
1075 /* And the the adjusted constant must either be something we can
1076 implement with andi or bclri. */
1077 && ((SMALL_OPERAND (sext_hwi (INTVAL (operands[2]), 32))
1078 || (TARGET_ZBS && popcount_hwi (INTVAL (operands[2])) == 31))
1079 && INTVAL (operands[2]) != 0x7fffffff)"
1080 "#"
1081 "&& 1"
1082 [(set (match_dup 0) (zero_extend:DI (match_dup 3)))
1083 (set (match_dup 0) (and:DI (match_dup 0) (match_dup 2)))]
1084 "{
1085 operands[3] = gen_lowpart (SImode, operands[1]);
1086 operands[2] = GEN_INT (sext_hwi (INTVAL (operands[2]), 32));
1087 }"
1088 [(set_attr "type" "bitmanip")])
1089
1090 ;; IF_THEN_ELSE: test for 2 bits of opposite polarity
1091 (define_insn_and_split "*branch<X:mode>_mask_twobits_equals_singlebit"
1092 [(set (pc)
1093 (if_then_else
1094 (match_operator 1 "equality_operator"
1095 [(and:X (match_operand:X 2 "register_operand" "r")
1096 (match_operand:X 3 "const_twobits_not_arith_operand" "i"))
1097 (match_operand:X 4 "single_bit_mask_operand" "i")])
1098 (label_ref (match_operand 0 "" ""))
1099 (pc)))
1100 (clobber (match_scratch:X 5 "=&r"))
1101 (clobber (match_scratch:X 6 "=&r"))]
1102 "TARGET_ZBS && TARGET_ZBB"
1103 "#"
1104 "&& reload_completed"
1105 [(set (match_dup 5) (zero_extract:X (match_dup 2)
1106 (const_int 1)
1107 (match_dup 8)))
1108 (set (match_dup 6) (zero_extract:X (match_dup 2)
1109 (const_int 1)
1110 (match_dup 9)))
1111 (set (match_dup 6) (and:X (not:X (match_dup 6)) (match_dup 5)))
1112 (set (pc) (if_then_else (match_op_dup 1 [(match_dup 6) (const_int 0)])
1113 (label_ref (match_dup 0))
1114 (pc)))]
1115 {
1116 unsigned HOST_WIDE_INT twobits_mask = UINTVAL (operands[3]);
1117 unsigned HOST_WIDE_INT singlebit_mask = UINTVAL (operands[4]);
1118
1119 /* We should never see an unsatisfiable condition. */
1120 gcc_assert (twobits_mask & singlebit_mask);
1121
1122 int setbit = ctz_hwi (singlebit_mask);
1123 int clearbit = ctz_hwi (twobits_mask & ~singlebit_mask);
1124
1125 operands[1] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == NE ? EQ : NE,
1126 <X:MODE>mode, operands[6], GEN_INT(0));
1127
1128 operands[8] = GEN_INT (setbit);
1129 operands[9] = GEN_INT (clearbit);
1130 }
1131 [(set_attr "type" "bitmanip")])
1132
1133 ;; IF_THEN_ELSE: test for (a & (1 << BIT_NO))
1134 (define_insn_and_split "*branch<X:mode>_bext"
1135 [(set (pc)
1136 (if_then_else
1137 (match_operator 1 "equality_operator"
1138 [(zero_extract:X (match_operand:X 2 "register_operand" "r")
1139 (const_int 1)
1140 (zero_extend:X
1141 (match_operand:QI 3 "register_operand" "r")))
1142 (const_int 0)])
1143 (label_ref (match_operand 0 "" ""))
1144 (pc)))
1145 (clobber (match_scratch:X 4 "=&r"))]
1146 "TARGET_ZBS"
1147 "#"
1148 "&& reload_completed"
1149 [(set (match_dup 4) (zero_extract:X (match_dup 2)
1150 (const_int 1)
1151 (zero_extend:X (match_dup 3))))
1152 (set (pc) (if_then_else (match_op_dup 1 [(match_dup 4) (const_int 0)])
1153 (label_ref (match_dup 0))
1154 (pc)))]
1155 ""
1156 [(set_attr "type" "bitmanip")])
1157
1158 ;; ZBKC or ZBC extension
1159 (define_insn "riscv_clmul_<mode>"
1160 [(set (match_operand:GPR 0 "register_operand" "=r")
1161 (unspec:GPR [(match_operand:GPR 1 "register_operand" "r")
1162 (match_operand:GPR 2 "register_operand" "r")]
1163 UNSPEC_CLMUL))]
1164 "TARGET_ZBKC || TARGET_ZBC"
1165 "clmul\t%0,%1,%2"
1166 [(set_attr "type" "clmul")])
1167
1168 (define_insn "riscv_clmulh_<mode>"
1169 [(set (match_operand:X 0 "register_operand" "=r")
1170 (unspec:X [(match_operand:X 1 "register_operand" "r")
1171 (match_operand:X 2 "register_operand" "r")]
1172 UNSPEC_CLMULH))]
1173 "TARGET_ZBKC || TARGET_ZBC"
1174 "clmulh\t%0,%1,%2"
1175 [(set_attr "type" "clmul")])
1176
1177 ;; ZBC extension
1178 (define_insn "riscv_clmulr_<mode>"
1179 [(set (match_operand:X 0 "register_operand" "=r")
1180 (unspec:X [(match_operand:X 1 "register_operand" "r")
1181 (match_operand:X 2 "register_operand" "r")]
1182 UNSPEC_CLMULR))]
1183 "TARGET_ZBC"
1184 "clmulr\t%0,%1,%2"
1185 [(set_attr "type" "clmul")])
1186
1187 ;; Reversed CRC 8, 16, 32 for TARGET_64
1188 (define_expand "crc_rev<ANYI1:mode><ANYI:mode>4"
1189 ;; return value (calculated CRC)
1190 [(set (match_operand:ANYI 0 "register_operand" "=r")
1191 ;; initial CRC
1192 (unspec:ANYI [(match_operand:ANYI 1 "register_operand" "r")
1193 ;; data
1194 (match_operand:ANYI1 2 "register_operand" "r")
1195 ;; polynomial without leading 1
1196 (match_operand:ANYI 3)]
1197 UNSPEC_CRC_REV))]
1198 /* We don't support the case when data's size is bigger than CRC's size. */
1199 "<ANYI:MODE>mode >= <ANYI1:MODE>mode"
1200 {
1201 /* If we have the ZBC or ZBKC extension (ie, clmul) and
1202 it is possible to store the quotient within a single variable
1203 (E.g. CRC64's quotient may need 65 bits,
1204 we can't keep it in 64 bit variable.)
1205 then use clmul instruction to implement the CRC,
1206 otherwise (TARGET_ZBKB) generate table based using brev. */
1207 if ((TARGET_ZBKC || TARGET_ZBC) && <ANYI:MODE>mode < word_mode)
1208 expand_reversed_crc_using_clmul (<ANYI:MODE>mode, <ANYI1:MODE>mode,
1209 operands);
1210 else if (TARGET_ZBKB)
1211 /* Generate table-based CRC.
1212 To reflect values use brev and bswap instructions. */
1213 expand_reversed_crc_table_based (operands[0], operands[1],
1214 operands[2], operands[3],
1215 GET_MODE (operands[2]),
1216 generate_reflecting_code_using_brev);
1217 else
1218 /* Generate table-based CRC.
1219 To reflect values use standard reflecting algorithm. */
1220 expand_reversed_crc_table_based (operands[0], operands[1],
1221 operands[2], operands[3],
1222 GET_MODE (operands[2]),
1223 generate_reflecting_code_standard);
1224 DONE;
1225 })
1226
1227 ;; CRC 8, 16, (32 for TARGET_64)
1228 (define_expand "crc<SUBX1:mode><SUBX:mode>4"
1229 ;; return value (calculated CRC)
1230 [(set (match_operand:SUBX 0 "register_operand" "=r")
1231 ;; initial CRC
1232 (unspec:SUBX [(match_operand:SUBX 1 "register_operand" "r")
1233 ;; data
1234 (match_operand:SUBX1 2 "register_operand" "r")
1235 ;; polynomial without leading 1
1236 (match_operand:SUBX 3)]
1237 UNSPEC_CRC))]
1238 /* We don't support the case when data's size is bigger than CRC's size. */
1239 "(TARGET_ZBKC || TARGET_ZBC) && <SUBX:MODE>mode >= <SUBX1:MODE>mode"
1240 {
1241 /* If we have the ZBC or ZBKC extension (ie, clmul) and
1242 it is possible to store the quotient within a single variable
1243 (E.g. CRC64's quotient may need 65 bits,
1244 we can't keep it in 64 bit variable.)
1245 then use clmul instruction to implement the CRC. */
1246 expand_crc_using_clmul (<SUBX:MODE>mode, <SUBX1:MODE>mode, operands);
1247 DONE;
1248 })