1 ;; Machine description for RISC-V Bit Manipulation operations.
2 ;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 ;; This file is part of GCC.
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)
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.
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/>.
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"
29 [(set_attr "type" "bitmanip,load")
30 (set_attr "mode" "DI")])
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")))]
39 [(set_attr "type" "bitmanip")
40 (set_attr "mode" "<X:MODE>")])
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.
49 [(set (match_operand:DI 0 "register_operand")
50 (sign_extend:DI (div:SI (plus:SI (subreg:SI (ashift:DI (match_operand:DI 1 "register_operand")
51 (match_operand:QI 2 "imm123_operand")) 0)
52 (subreg:SI (match_operand:DI 3 "register_operand") 0))
53 (subreg:SI (match_operand:DI 4 "register_operand") 0))))
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) (subreg:SI (match_dup 4) 0))))])
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).
65 ; To enable this optimization, we split [ slli(.uw)?, addw, zext.w ]
66 ; into [ sh[123]add, zext.w ] for use during combine.
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)))])
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 [(set (match_dup 0) (plus:DI (ashift:DI (match_dup 1) (match_dup 2)) (match_dup 4)))
85 (set (match_dup 0) (zero_extend:DI (subreg:SI (match_dup 0) 0)))])
87 ; Make sure that an andi followed by a sh[123]add remains a two instruction
88 ; sequence--and is not torn apart into slli, slri, add.
89 (define_insn_and_split "*andi_add.uw"
90 [(set (match_operand:DI 0 "register_operand" "=r")
91 (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
92 (match_operand:QI 2 "imm123_operand" "Ds3"))
93 (match_operand:DI 3 "consecutive_bits_operand" ""))
94 (match_operand:DI 4 "register_operand" "r")))
95 (clobber (match_scratch:DI 5 "=&r"))]
96 "TARGET_64BIT && TARGET_ZBA
97 && riscv_shamt_matches_mask_p (INTVAL (operands[2]), INTVAL (operands[3]))
98 && SMALL_OPERAND (INTVAL (operands[3]) >> INTVAL (operands[2]))"
100 "&& reload_completed"
101 [(set (match_dup 5) (and:DI (match_dup 1) (match_dup 3)))
102 (set (match_dup 0) (plus:DI (ashift:DI (match_dup 5) (match_dup 2))
105 operands[3] = GEN_INT (INTVAL (operands[3]) >> INTVAL (operands[2]));
108 (define_insn "*shNadduw"
109 [(set (match_operand:DI 0 "register_operand" "=r")
111 (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
112 (match_operand:QI 2 "imm123_operand" "Ds3"))
113 (match_operand 3 "immediate_operand" "n"))
114 (match_operand:DI 4 "register_operand" "r")))]
115 "TARGET_64BIT && TARGET_ZBA
116 && (INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff"
117 "sh%2add.uw\t%0,%1,%4"
118 [(set_attr "type" "bitmanip")
119 (set_attr "mode" "DI")])
121 ;; During combine, we may encounter an attempt to combine
122 ;; slli rtmp, rs, #imm
124 ;; sh[123]add rd, rtmp, rs2
125 ;; which will lead to the immediate not satisfying the above constraints.
126 ;; By splitting the compound expression, we can simplify to a slli and a
129 [(set (match_operand:DI 0 "register_operand")
130 (plus:DI (and:DI (ashift:DI (match_operand:DI 1 "register_operand")
131 (match_operand:QI 2 "immediate_operand"))
132 (match_operand:DI 3 "consecutive_bits_operand"))
133 (match_operand:DI 4 "register_operand")))
134 (clobber (match_operand:DI 5 "register_operand"))]
135 "TARGET_64BIT && TARGET_ZBA"
136 [(set (match_dup 5) (ashift:DI (match_dup 1) (match_dup 6)))
137 (set (match_dup 0) (plus:DI (and:DI (ashift:DI (match_dup 5)
142 unsigned HOST_WIDE_INT mask = UINTVAL (operands[3]);
143 /* scale: shift within the sh[123]add.uw */
144 unsigned HOST_WIDE_INT scale = 32 - clz_hwi (mask);
145 /* bias: pre-scale amount (i.e. the prior shift amount) */
146 int bias = ctz_hwi (mask) - scale;
148 /* If the bias + scale don't add up to operand[2], reject. */
149 if ((scale + bias) != UINTVAL (operands[2]))
152 /* If the shift-amount is out-of-range for sh[123]add.uw, reject. */
153 if ((scale < 1) || (scale > 3))
156 /* If there's no bias, the '*shNadduw' pattern should have matched. */
160 operands[6] = GEN_INT (bias);
161 operands[7] = GEN_INT (scale);
162 operands[8] = GEN_INT (0xffffffffULL << scale);
165 (define_insn "*add.uw"
166 [(set (match_operand:DI 0 "register_operand" "=r")
167 (plus:DI (zero_extend:DI
168 (match_operand:SI 1 "register_operand" "r"))
169 (match_operand:DI 2 "register_operand" "r")))]
170 "TARGET_64BIT && TARGET_ZBA"
172 [(set_attr "type" "bitmanip")
173 (set_attr "mode" "DI")])
175 (define_insn "*slliuw"
176 [(set (match_operand:DI 0 "register_operand" "=r")
177 (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
178 (match_operand:QI 2 "immediate_operand" "I"))
179 (match_operand 3 "immediate_operand" "n")))]
180 "TARGET_64BIT && TARGET_ZBA
181 && (INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff"
183 [(set_attr "type" "bitmanip")
184 (set_attr "mode" "DI")])
188 (define_expand "clzdi2"
189 [(set (match_operand:DI 0 "register_operand")
190 (clz:DI (match_operand:DI 1 "register_operand")))]
191 "TARGET_64BIT && (TARGET_ZBB || TARGET_XTHEADBB)")
193 (define_expand "clzsi2"
194 [(set (match_operand:SI 0 "register_operand")
195 (clz:SI (match_operand:SI 1 "register_operand")))]
196 "TARGET_ZBB || (!TARGET_64BIT && TARGET_XTHEADBB)")
198 (define_expand "ctz<mode>2"
199 [(set (match_operand:GPR 0 "register_operand")
200 (ctz:GPR (match_operand:GPR 1 "register_operand")))]
203 (define_expand "popcount<mode>2"
204 [(set (match_operand:GPR 0 "register_operand")
205 (popcount:GPR (match_operand:GPR 1 "register_operand")))]
208 (define_insn "*<optab>_not<mode>"
209 [(set (match_operand:X 0 "register_operand" "=r")
210 (bitmanip_bitwise:X (not:X (match_operand:X 1 "register_operand" "r"))
211 (match_operand:X 2 "register_operand" "r")))]
212 "TARGET_ZBB || TARGET_ZBKB"
214 [(set_attr "type" "bitmanip")
215 (set_attr "mode" "<X:MODE>")])
217 ;; '(a >= 0) ? b : 0' is emitted branchless (from if-conversion). Without a
218 ;; bit of extra help for combine (i.e., the below split), we end up emitting
219 ;; not/srai/and instead of combining the not into an andn.
221 [(set (match_operand:DI 0 "register_operand")
222 (and:DI (neg:DI (ge:DI (match_operand:DI 1 "register_operand")
224 (match_operand:DI 2 "register_operand")))
225 (clobber (match_operand:DI 3 "register_operand"))]
226 "TARGET_ZBB || TARGET_ZBKB"
227 [(set (match_dup 3) (ashiftrt:DI (match_dup 1) (const_int 63)))
228 (set (match_dup 0) (and:DI (not:DI (match_dup 3)) (match_dup 2)))])
230 (define_insn "*xor_not<mode>"
231 [(set (match_operand:X 0 "register_operand" "=r")
232 (not:X (xor:X (match_operand:X 1 "register_operand" "r")
233 (match_operand:X 2 "register_operand" "r"))))]
234 "TARGET_ZBB || TARGET_ZBKB"
236 [(set_attr "type" "bitmanip")
237 (set_attr "mode" "<X:MODE>")])
239 (define_insn "*<bitmanip_optab>si2"
240 [(set (match_operand:SI 0 "register_operand" "=r")
241 (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r")))]
243 "<bitmanip_insn>%~\t%0,%1"
244 [(set_attr "type" "<bitmanip_insn>")
245 (set_attr "mode" "SI")])
247 (define_insn "*<bitmanip_optab>disi2"
248 [(set (match_operand:DI 0 "register_operand" "=r")
250 (clz_ctz_pcnt:SI (match_operand:SI 1 "register_operand" "r"))))]
251 "TARGET_64BIT && TARGET_ZBB"
252 "<bitmanip_insn>w\t%0,%1"
253 [(set_attr "type" "<bitmanip_insn>")
254 (set_attr "mode" "SI")])
256 (define_insn "*<bitmanip_optab>di2"
257 [(set (match_operand:DI 0 "register_operand" "=r")
258 (clz_ctz_pcnt:DI (match_operand:DI 1 "register_operand" "r")))]
259 "TARGET_64BIT && TARGET_ZBB"
260 "<bitmanip_insn>\t%0,%1"
261 [(set_attr "type" "<bitmanip_insn>")
262 (set_attr "mode" "DI")])
264 (define_insn "*zero_extendhi<GPR:mode>2_bitmanip"
265 [(set (match_operand:GPR 0 "register_operand" "=r,r")
266 (zero_extend:GPR (match_operand:HI 1 "nonimmediate_operand" "r,m")))]
271 [(set_attr "type" "bitmanip,load")
272 (set_attr "mode" "<GPR:MODE>")])
274 (define_insn "*extend<SHORT:mode><SUPERQI:mode>2_zbb"
275 [(set (match_operand:SUPERQI 0 "register_operand" "=r,r")
277 (match_operand:SHORT 1 "nonimmediate_operand" " r,m")))]
280 sext.<SHORT:size>\t%0,%1
281 l<SHORT:size>\t%0,%1"
282 [(set_attr "type" "bitmanip,load")
283 (set_attr "mode" "<SUPERQI:MODE>")])
285 (define_insn "*zero_extendhi<GPR:mode>2_zbb"
286 [(set (match_operand:GPR 0 "register_operand" "=r,r")
288 (match_operand:HI 1 "nonimmediate_operand" " r,m")))]
293 [(set_attr "type" "bitmanip,load")
294 (set_attr "mode" "HI")])
296 (define_expand "rotr<mode>3"
297 [(set (match_operand:GPR 0 "register_operand")
298 (rotatert:GPR (match_operand:GPR 1 "register_operand")
299 (match_operand:QI 2 "arith_operand")))]
300 "TARGET_ZBB || TARGET_XTHEADBB || TARGET_ZBKB"
302 if (TARGET_XTHEADBB && !immediate_operand (operands[2], VOIDmode))
306 (define_insn "*rotrsi3"
307 [(set (match_operand:SI 0 "register_operand" "=r")
308 (rotatert:SI (match_operand:SI 1 "register_operand" "r")
309 (match_operand:QI 2 "arith_operand" "rI")))]
310 "TARGET_ZBB || TARGET_ZBKB"
312 [(set_attr "type" "bitmanip")])
314 (define_insn "*rotrdi3"
315 [(set (match_operand:DI 0 "register_operand" "=r")
316 (rotatert:DI (match_operand:DI 1 "register_operand" "r")
317 (match_operand:QI 2 "arith_operand" "rI")))]
318 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
320 [(set_attr "type" "bitmanip")])
322 (define_insn "*rotrsi3_sext"
323 [(set (match_operand:DI 0 "register_operand" "=r")
324 (sign_extend:DI (rotatert:SI (match_operand:SI 1 "register_operand" "r")
325 (match_operand:QI 2 "arith_operand" "rI"))))]
326 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
328 [(set_attr "type" "bitmanip")])
330 (define_insn "rotlsi3"
331 [(set (match_operand:SI 0 "register_operand" "=r")
332 (rotate:SI (match_operand:SI 1 "register_operand" "r")
333 (match_operand:QI 2 "register_operand" "r")))]
334 "TARGET_ZBB || TARGET_ZBKB"
336 [(set_attr "type" "bitmanip")])
338 (define_insn "rotldi3"
339 [(set (match_operand:DI 0 "register_operand" "=r")
340 (rotate:DI (match_operand:DI 1 "register_operand" "r")
341 (match_operand:QI 2 "register_operand" "r")))]
342 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
344 [(set_attr "type" "bitmanip")])
346 (define_insn "rotlsi3_sext"
347 [(set (match_operand:DI 0 "register_operand" "=r")
348 (sign_extend:DI (rotate:SI (match_operand:SI 1 "register_operand" "r")
349 (match_operand:QI 2 "register_operand" "r"))))]
350 "TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)"
352 [(set_attr "type" "bitmanip")])
354 ;; orc.b (or-combine) is added as an unspec for the benefit of the support
355 ;; for optimized string functions (such as strcmp).
356 (define_insn "orcb<mode>2"
357 [(set (match_operand:X 0 "register_operand" "=r")
358 (unspec:X [(match_operand:X 1 "register_operand" "r")] UNSPEC_ORC_B))]
361 [(set_attr "type" "bitmanip")])
363 (define_expand "bswapdi2"
364 [(set (match_operand:DI 0 "register_operand")
365 (bswap:DI (match_operand:DI 1 "register_operand")))]
366 "TARGET_64BIT && (TARGET_ZBB || TARGET_XTHEADBB || TARGET_ZBKB)")
368 (define_expand "bswapsi2"
369 [(set (match_operand:SI 0 "register_operand")
370 (bswap:SI (match_operand:SI 1 "register_operand")))]
371 "(!TARGET_64BIT && (TARGET_ZBB || TARGET_ZBKB)) || TARGET_XTHEADBB")
373 (define_insn "*bswap<mode>2"
374 [(set (match_operand:X 0 "register_operand" "=r")
375 (bswap:X (match_operand:X 1 "register_operand" "r")))]
376 "TARGET_ZBB || TARGET_ZBKB"
378 [(set_attr "type" "bitmanip")])
380 ;; HI bswap can be emulated using SI/DI bswap followed
381 ;; by a logical shift right
382 ;; SI bswap for TARGET_64BIT is already similarly in
384 (define_expand "bswaphi2"
385 [(set (match_operand:HI 0 "register_operand" "=r")
386 (bswap:HI (match_operand:HI 1 "register_operand" "r")))]
389 rtx tmp = gen_reg_rtx (word_mode);
390 rtx newop1 = gen_lowpart (word_mode, operands[1]);
392 emit_insn (gen_bswapdi2 (tmp, newop1));
394 emit_insn (gen_bswapsi2 (tmp, newop1));
395 rtx tmp1 = gen_reg_rtx (word_mode);
397 emit_insn (gen_lshrdi3 (tmp1, tmp, GEN_INT (64 - 16)));
399 emit_insn (gen_lshrsi3 (tmp1, tmp, GEN_INT (32 - 16)));
400 emit_move_insn (operands[0], gen_lowpart (HImode, tmp1));
404 (define_expand "<bitmanip_optab>di3"
405 [(set (match_operand:DI 0 "register_operand" "=r")
406 (bitmanip_minmax:DI (match_operand:DI 1 "register_operand" "r")
407 (match_operand:DI 2 "register_operand" "r")))]
408 "TARGET_64BIT && TARGET_ZBB")
410 (define_expand "<bitmanip_optab>si3"
411 [(set (match_operand:SI 0 "register_operand" "=r")
412 (bitmanip_minmax:SI (match_operand:SI 1 "register_operand" "r")
413 (match_operand:SI 2 "register_operand" "r")))]
418 rtx t = gen_reg_rtx (DImode);
419 operands[1] = force_reg (DImode, gen_rtx_SIGN_EXTEND (DImode, operands[1]));
420 operands[2] = force_reg (DImode, gen_rtx_SIGN_EXTEND (DImode, operands[2]));
421 emit_insn (gen_<bitmanip_optab>di3 (t, operands[1], operands[2]));
422 emit_move_insn (operands[0], gen_lowpart (SImode, t));
427 (define_insn "*<bitmanip_optab><mode>3"
428 [(set (match_operand:X 0 "register_operand" "=r")
429 (bitmanip_minmax:X (match_operand:X 1 "register_operand" "r")
430 (match_operand:X 2 "reg_or_0_operand" "rJ")))]
432 "<bitmanip_insn>\t%0,%1,%z2"
433 [(set_attr "type" "<bitmanip_insn>")])
435 ;; Optimize the common case of a SImode min/max against a constant
436 ;; that is safe both for sign- and zero-extension.
437 (define_insn_and_split "*minmax"
438 [(set (match_operand:DI 0 "register_operand" "=r")
441 (bitmanip_minmax:DI (zero_extend:DI (match_operand:SI 1 "register_operand" "r"))
442 (match_operand:DI 2 "immediate_operand" "i"))
444 (clobber (match_scratch:DI 3 "=&r"))
445 (clobber (match_scratch:DI 4 "=&r"))]
446 "TARGET_64BIT && TARGET_ZBB && sext_hwi (INTVAL (operands[2]), 32) >= 0"
448 "&& reload_completed"
449 [(set (match_dup 3) (sign_extend:DI (match_dup 1)))
450 (set (match_dup 4) (match_dup 2))
451 (set (match_dup 0) (<minmax_optab>:DI (match_dup 3) (match_dup 4)))])
455 (define_insn "*bset<mode>"
456 [(set (match_operand:X 0 "register_operand" "=r")
457 (ior:X (ashift:X (const_int 1)
458 (match_operand:QI 2 "register_operand" "r"))
459 (match_operand:X 1 "register_operand" "r")))]
462 [(set_attr "type" "bitmanip")])
464 (define_insn "*bset<mode>_mask"
465 [(set (match_operand:X 0 "register_operand" "=r")
466 (ior:X (ashift:X (const_int 1)
468 (and:X (match_operand:X 2 "register_operand" "r")
469 (match_operand 3 "<X:shiftm1>" "<X:shiftm1p>")) 0))
470 (match_operand:X 1 "register_operand" "r")))]
473 [(set_attr "type" "bitmanip")])
475 (define_insn "*bset<mode>_1"
476 [(set (match_operand:X 0 "register_operand" "=r")
477 (ashift:X (const_int 1)
478 (match_operand:QI 1 "register_operand" "r")))]
481 [(set_attr "type" "bitmanip")])
483 (define_insn "*bset<mode>_1_mask"
484 [(set (match_operand:X 0 "register_operand" "=r")
485 (ashift:X (const_int 1)
487 (and:X (match_operand:X 1 "register_operand" "r")
488 (match_operand 2 "<X:shiftm1>" "<X:shiftm1p>")) 0)))]
491 [(set_attr "type" "bitmanip")])
493 (define_insn "*bseti<mode>"
494 [(set (match_operand:X 0 "register_operand" "=r")
495 (ior:X (match_operand:X 1 "register_operand" "r")
496 (match_operand:X 2 "single_bit_mask_operand" "DbS")))]
499 [(set_attr "type" "bitmanip")])
501 ;; As long as the SImode operand is not a partial subreg, we can use a
502 ;; bseti without postprocessing, as the middle end is smart enough to
503 ;; stay away from the signbit.
504 (define_insn "*bsetidisi"
505 [(set (match_operand:DI 0 "register_operand" "=r")
506 (ior:DI (sign_extend:DI (match_operand:SI 1 "register_operand" "r"))
507 (match_operand 2 "single_bit_mask_operand" "i")))]
508 "TARGET_ZBS && TARGET_64BIT
509 && !partial_subreg_p (operands[1])"
511 [(set_attr "type" "bitmanip")])
513 (define_insn "*bclr<mode>"
514 [(set (match_operand:X 0 "register_operand" "=r")
515 (and:X (rotate:X (const_int -2)
516 (match_operand:QI 2 "register_operand" "r"))
517 (match_operand:X 1 "register_operand" "r")))]
520 [(set_attr "type" "bitmanip")])
522 (define_insn "*bclri<mode>"
523 [(set (match_operand:X 0 "register_operand" "=r")
524 (and:X (match_operand:X 1 "register_operand" "r")
525 (match_operand:X 2 "not_single_bit_mask_operand" "DnS")))]
528 [(set_attr "type" "bitmanip")])
530 ;; In case we have "val & ~IMM" where ~IMM has 2 bits set.
531 (define_insn_and_split "*bclri<mode>_nottwobits"
532 [(set (match_operand:X 0 "register_operand" "=r")
533 (and:X (match_operand:X 1 "register_operand" "r")
534 (match_operand:X 2 "const_nottwobits_not_arith_operand" "i")))]
535 "TARGET_ZBS && !paradoxical_subreg_p (operands[1])"
537 "&& reload_completed"
538 [(set (match_dup 0) (and:X (match_dup 1) (match_dup 3)))
539 (set (match_dup 0) (and:X (match_dup 0) (match_dup 4)))]
541 unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]);
542 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
544 operands[3] = GEN_INT (~bits | topbit);
545 operands[4] = GEN_INT (~topbit);
548 ;; In case of a paradoxical subreg, the sign bit and the high bits are
549 ;; not allowed to be changed
550 (define_insn_and_split "*bclridisi_nottwobits"
551 [(set (match_operand:DI 0 "register_operand" "=r")
552 (and:DI (match_operand:DI 1 "register_operand" "r")
553 (match_operand:DI 2 "const_nottwobits_not_arith_operand" "i")))]
554 "TARGET_64BIT && TARGET_ZBS
555 && clz_hwi (~UINTVAL (operands[2])) > 33"
557 "&& reload_completed"
558 [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 3)))
559 (set (match_dup 0) (and:DI (match_dup 0) (match_dup 4)))]
561 unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]);
562 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
564 operands[3] = GEN_INT (~bits | topbit);
565 operands[4] = GEN_INT (~topbit);
568 (define_insn "*binv<mode>"
569 [(set (match_operand:X 0 "register_operand" "=r")
570 (xor:X (ashift:X (const_int 1)
571 (match_operand:QI 2 "register_operand" "r"))
572 (match_operand:X 1 "register_operand" "r")))]
575 [(set_attr "type" "bitmanip")])
577 (define_insn "*binvi<mode>"
578 [(set (match_operand:X 0 "register_operand" "=r")
579 (xor:X (match_operand:X 1 "register_operand" "r")
580 (match_operand:X 2 "single_bit_mask_operand" "DbS")))]
583 [(set_attr "type" "bitmanip")])
585 (define_insn "*bext<mode>"
586 [(set (match_operand:X 0 "register_operand" "=r")
587 (zero_extract:X (match_operand:X 1 "register_operand" "r")
590 (match_operand:QI 2 "register_operand" "r"))))]
593 [(set_attr "type" "bitmanip")])
595 ;; When performing `(a & (1UL << bitno)) ? 0 : -1` the combiner
596 ;; usually has the `bitno` typed as X-mode (i.e. no further
597 ;; zero-extension is performed around the bitno).
598 (define_insn "*bext<mode>"
599 [(set (match_operand:X 0 "register_operand" "=r")
600 (zero_extract:X (match_operand:X 1 "register_operand" "r")
602 (match_operand:X 2 "register_operand" "r")))]
605 [(set_attr "type" "bitmanip")])
607 (define_insn "*bexti"
608 [(set (match_operand:X 0 "register_operand" "=r")
609 (zero_extract:X (match_operand:X 1 "register_operand" "r")
611 (match_operand 2 "immediate_operand" "n")))]
612 "TARGET_ZBS && UINTVAL (operands[2]) < GET_MODE_BITSIZE (<MODE>mode)"
614 [(set_attr "type" "bitmanip")])
616 ;; Split for "(a & (1 << BIT_NO)) ? 0 : 1":
617 ;; We avoid reassociating "(~(a >> BIT_NO)) & 1" into "((~a) >> BIT_NO) & 1",
618 ;; so we don't have to use a temporary. Instead we extract the bit and then
619 ;; invert bit 0 ("a ^ 1") only.
621 [(set (match_operand:X 0 "register_operand")
622 (and:X (not:X (lshiftrt:X (match_operand:X 1 "register_operand")
623 (subreg:QI (match_operand:X 2 "register_operand") 0)))
626 [(set (match_dup 0) (zero_extract:X (match_dup 1)
629 (set (match_dup 0) (xor:X (match_dup 0) (const_int 1)))])
631 ;; We can create a polarity-reversed mask (i.e. bit N -> { set = 0, clear = -1 })
632 ;; using a bext(i) followed by an addi instruction.
633 ;; This splits the canonical representation of "(a & (1 << BIT_NO)) ? 0 : -1".
635 [(set (match_operand:GPR 0 "register_operand")
636 (neg:GPR (eq:GPR (zero_extract:GPR (match_operand:GPR 1 "register_operand")
641 [(set (match_dup 0) (zero_extract:GPR (match_dup 1) (const_int 1) (match_dup 2)))
642 (set (match_dup 0) (plus:GPR (match_dup 0) (const_int -1)))])
644 ;; Catch those cases where we can use a bseti/binvi + ori/xori or
645 ;; bseti/binvi + bseti/binvi instead of a lui + addi + or/xor sequence.
646 (define_insn_and_split "*<or_optab>i<mode>_extrabit"
647 [(set (match_operand:X 0 "register_operand" "=r")
648 (any_or:X (match_operand:X 1 "register_operand" "r")
649 (match_operand:X 2 "uimm_extra_bit_or_twobits" "i")))]
652 "&& reload_completed"
653 [(set (match_dup 0) (<or_optab>:X (match_dup 1) (match_dup 3)))
654 (set (match_dup 0) (<or_optab>:X (match_dup 0) (match_dup 4)))]
656 unsigned HOST_WIDE_INT bits = UINTVAL (operands[2]);
657 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits);
659 operands[3] = GEN_INT (bits &~ topbit);
660 operands[4] = GEN_INT (topbit);
663 ;; Same to use blcri + andi and blcri + bclri
664 (define_insn_and_split "*andi<mode>_extrabit"
665 [(set (match_operand:X 0 "register_operand" "=r")
666 (and:X (match_operand:X 1 "register_operand" "r")
667 (match_operand:X 2 "not_uimm_extra_bit_or_nottwobits" "i")))]
670 "&& reload_completed"
671 [(set (match_dup 0) (and:X (match_dup 1) (match_dup 3)))
672 (set (match_dup 0) (and:X (match_dup 0) (match_dup 4)))]
674 unsigned HOST_WIDE_INT bits = UINTVAL (operands[2]);
675 unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (~bits);
677 operands[3] = GEN_INT (bits | topbit);
678 operands[4] = GEN_INT (~topbit);
681 ;; IF_THEN_ELSE: test for 2 bits of opposite polarity
682 (define_insn_and_split "*branch<X:mode>_mask_twobits_equals_singlebit"
685 (match_operator 1 "equality_operator"
686 [(and:X (match_operand:X 2 "register_operand" "r")
687 (match_operand:X 3 "const_twobits_not_arith_operand" "i"))
688 (match_operand:X 4 "single_bit_mask_operand" "i")])
689 (label_ref (match_operand 0 "" ""))
691 (clobber (match_scratch:X 5 "=&r"))
692 (clobber (match_scratch:X 6 "=&r"))]
693 "TARGET_ZBS && TARGET_ZBB"
695 "&& reload_completed"
696 [(set (match_dup 5) (zero_extract:X (match_dup 2)
699 (set (match_dup 6) (zero_extract:X (match_dup 2)
702 (set (match_dup 6) (and:X (not:X (match_dup 6)) (match_dup 5)))
703 (set (pc) (if_then_else (match_op_dup 1 [(match_dup 6) (const_int 0)])
704 (label_ref (match_dup 0))
707 unsigned HOST_WIDE_INT twobits_mask = UINTVAL (operands[3]);
708 unsigned HOST_WIDE_INT singlebit_mask = UINTVAL (operands[4]);
710 /* We should never see an unsatisfiable condition. */
711 gcc_assert (twobits_mask & singlebit_mask);
713 int setbit = ctz_hwi (singlebit_mask);
714 int clearbit = ctz_hwi (twobits_mask & ~singlebit_mask);
716 operands[1] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == NE ? EQ : NE,
717 <X:MODE>mode, operands[6], GEN_INT(0));
719 operands[8] = GEN_INT (setbit);
720 operands[9] = GEN_INT (clearbit);