Debian Codegraph @

gcc/config/riscv/sync.md

raw
1 ;; Machine description for RISC-V atomic operations.
2 ;; Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 ;; Contributed by Andrew Waterman ([email protected]).
4 ;; Based on MIPS target for GNU compiler.
5
6 ;; This file is part of GCC.
7
8 ;; GCC is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
12
13 ;; GCC is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GCC; see the file COPYING3. If not see
20 ;; <http://www.gnu.org/licenses/>.
21
22 (define_c_enum "unspec" [
23 UNSPEC_COMPARE_AND_SWAP
24 UNSPEC_COMPARE_AND_SWAP_SUBWORD
25 UNSPEC_SYNC_OLD_OP
26 UNSPEC_SYNC_OLD_OP_SUBWORD
27 UNSPEC_SYNC_OLD_OP_ZABHA
28 UNSPEC_SYNC_EXCHANGE
29 UNSPEC_SYNC_EXCHANGE_SUBWORD
30 UNSPEC_SYNC_EXCHANGE_ZABHA
31 UNSPEC_ATOMIC_LOAD
32 UNSPEC_ATOMIC_STORE
33 UNSPEC_MEMORY_BARRIER
34 ])
35
36 ;; Memory barriers.
37
38 (define_expand "mem_thread_fence"
39 [(match_operand:SI 0 "const_int_operand" "")] ;; model
40 ""
41 {
42 enum memmodel model = memmodel_base (INTVAL (operands[0]));
43
44 if (TARGET_ZTSO && model == MEMMODEL_SEQ_CST)
45 {
46 rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
47 MEM_VOLATILE_P (mem) = 1;
48 emit_insn (gen_mem_thread_fence_ztso (mem, operands[0]));
49 }
50 else if (!TARGET_ZTSO && model != MEMMODEL_RELAXED)
51 {
52 rtx mem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
53 MEM_VOLATILE_P (mem) = 1;
54 emit_insn (gen_mem_thread_fence_rvwmo (mem, operands[0]));
55 }
56 DONE;
57 })
58
59 ;; Atomic memory operations.
60
61 (define_expand "atomic_load<mode>"
62 [(match_operand:ANYI 0 "register_operand")
63 (match_operand:ANYI 1 "memory_operand")
64 (match_operand:SI 2 "const_int_operand")] ;; model
65 ""
66 {
67 if (TARGET_ZTSO)
68 emit_insn (gen_atomic_load_ztso<mode> (operands[0], operands[1],
69 operands[2]));
70 else
71 emit_insn (gen_atomic_load_rvwmo<mode> (operands[0], operands[1],
72 operands[2]));
73 DONE;
74 })
75
76 (define_expand "atomic_store<mode>"
77 [(match_operand:ANYI 0 "memory_operand")
78 (match_operand:ANYI 1 "reg_or_0_operand")
79 (match_operand:SI 2 "const_int_operand")] ;; model
80 ""
81 {
82 if (TARGET_ZTSO)
83 emit_insn (gen_atomic_store_ztso<mode> (operands[0], operands[1],
84 operands[2]));
85 else
86 emit_insn (gen_atomic_store_rvwmo<mode> (operands[0], operands[1],
87 operands[2]));
88 DONE;
89 })
90
91 ;; AMO ops
92
93 (define_insn "atomic_<atomic_optab><mode>"
94 [(set (match_operand:SHORT 0 "memory_operand" "+A")
95 (unspec_volatile:SHORT
96 [(any_atomic:SHORT (match_dup 0)
97 (match_operand:SHORT 1 "reg_or_0_operand" "rJ"))
98 (match_operand:SI 2 "const_int_operand")] ;; model
99 UNSPEC_SYNC_OLD_OP_ZABHA))]
100 "TARGET_ZABHA"
101 "amo<insn>.<amobh>%A2\tzero,%z1,%0"
102 [(set_attr "type" "atomic")
103 (set (attr "length") (const_int 4))])
104
105 (define_expand "atomic_<atomic_optab><mode>"
106 [(any_atomic:GPR (match_operand:GPR 0 "memory_operand") ;; mem location
107 (match_operand:GPR 1 "reg_or_0_operand")) ;; value for op
108 (match_operand:SI 2 "const_int_operand")] ;; model
109 "TARGET_ZAAMO || TARGET_ZALRSC"
110 {
111 if (TARGET_ZAAMO)
112 emit_insn (gen_amo_atomic_<atomic_optab><mode> (operands[0], operands[1],
113 operands[2]));
114 else
115 emit_insn (gen_lrsc_atomic_<atomic_optab><mode> (operands[0], operands[1],
116 operands[2]));
117 DONE;
118 })
119
120 (define_insn "amo_atomic_<atomic_optab><mode>"
121 [(set (match_operand:GPR 0 "memory_operand" "+A")
122 (unspec_volatile:GPR
123 [(any_atomic:GPR (match_dup 0)
124 (match_operand:GPR 1 "reg_or_0_operand" "rJ"))
125 (match_operand:SI 2 "const_int_operand")] ;; model
126 UNSPEC_SYNC_OLD_OP))]
127 "TARGET_ZAAMO"
128 "amo<insn>.<amo>%A2\tzero,%z1,%0"
129 [(set_attr "type" "atomic")
130 (set (attr "length") (const_int 4))])
131
132 (define_insn "lrsc_atomic_<atomic_optab><mode>"
133 [(set (match_operand:GPR 0 "memory_operand" "+A")
134 (unspec_volatile:GPR
135 [(any_atomic:GPR (match_dup 0)
136 (match_operand:GPR 1 "reg_or_0_operand" "rJ"))
137 (match_operand:SI 2 "const_int_operand")] ;; model
138 UNSPEC_SYNC_OLD_OP))
139 (clobber (match_scratch:GPR 3 "=&r"))] ;; tmp_1
140 "!TARGET_ZAAMO && TARGET_ZALRSC"
141 {
142 return "1:\;"
143 "lr.<amo>%I2\t%3, %0\;"
144 "<insn>\t%3, %3, %1\;"
145 "sc.<amo>%J2\t%3, %3, %0\;"
146 "bnez\t%3, 1b";
147 }
148 [(set_attr "type" "atomic")
149 (set (attr "length") (const_int 16))])
150
151 ;; AMO fetch ops
152
153 (define_expand "atomic_fetch_<atomic_optab><mode>"
154 [(match_operand:GPR 0 "register_operand") ;; old value at mem
155 (any_atomic:GPR (match_operand:GPR 1 "memory_operand") ;; mem location
156 (match_operand:GPR 2 "reg_or_0_operand")) ;; value for op
157 (match_operand:SI 3 "const_int_operand")] ;; model
158 "TARGET_ZAAMO || TARGET_ZALRSC"
159 {
160 if (TARGET_ZAAMO)
161 emit_insn (gen_amo_atomic_fetch_<atomic_optab><mode> (operands[0], operands[1],
162 operands[2], operands[3]));
163 else
164 emit_insn (gen_lrsc_atomic_fetch_<atomic_optab><mode> (operands[0], operands[1],
165 operands[2], operands[3]));
166 DONE;
167 })
168
169 (define_insn "amo_atomic_fetch_<atomic_optab><mode>"
170 [(set (match_operand:GPR 0 "register_operand" "=&r")
171 (match_operand:GPR 1 "memory_operand" "+A"))
172 (set (match_dup 1)
173 (unspec_volatile:GPR
174 [(any_atomic:GPR (match_dup 1)
175 (match_operand:GPR 2 "reg_or_0_operand" "rJ"))
176 (match_operand:SI 3 "const_int_operand")] ;; model
177 UNSPEC_SYNC_OLD_OP))]
178 "TARGET_ZAAMO"
179 "amo<insn>.<amo>%A3\t%0,%z2,%1"
180 [(set_attr "type" "atomic")
181 (set (attr "length") (const_int 4))])
182
183 (define_insn "lrsc_atomic_fetch_<atomic_optab><mode>"
184 [(set (match_operand:GPR 0 "register_operand" "=&r")
185 (match_operand:GPR 1 "memory_operand" "+A"))
186 (set (match_dup 1)
187 (unspec_volatile:GPR
188 [(any_atomic:GPR (match_dup 1)
189 (match_operand:GPR 2 "reg_or_0_operand" "rJ"))
190 (match_operand:SI 3 "const_int_operand")] ;; model
191 UNSPEC_SYNC_OLD_OP))
192 (clobber (match_scratch:GPR 4 "=&r"))] ;; tmp_1
193 "!TARGET_ZAAMO && TARGET_ZALRSC"
194 {
195 return "1:\;"
196 "lr.<amo>%I3\t%0, %1\;"
197 "<insn>\t%4, %0, %2\;"
198 "sc.<amo>%J3\t%4, %4, %1\;"
199 "bnez\t%4, 1b";
200 }
201 [(set_attr "type" "atomic")
202 (set (attr "length") (const_int 16))])
203
204 (define_insn "subword_atomic_fetch_strong_<atomic_optab>"
205 [(set (match_operand:SI 0 "register_operand" "=&r") ;; old value at mem
206 (match_operand:SI 1 "memory_operand" "+A")) ;; mem location
207 (set (match_dup 1)
208 (unspec_volatile:SI
209 [(any_atomic:SI (match_dup 1)
210 (match_operand:SI 2 "register_operand" "rI")) ;; value for op
211 (match_operand:SI 3 "const_int_operand")] ;; model
212 UNSPEC_SYNC_OLD_OP_SUBWORD))
213 (match_operand:SI 4 "register_operand" "rI") ;; mask
214 (match_operand:SI 5 "register_operand" "rI") ;; not_mask
215 (clobber (match_scratch:SI 6 "=&r")) ;; tmp_1
216 (clobber (match_scratch:SI 7 "=&r"))] ;; tmp_2
217 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
218 {
219 return "1:\;"
220 "lr.w%I3\t%0, %1\;"
221 "<insn>\t%6, %0, %2\;"
222 "and\t%6, %6, %4\;"
223 "and\t%7, %0, %5\;"
224 "or\t%7, %7, %6\;"
225 "sc.w%J3\t%6, %7, %1\;"
226 "bnez\t%6, 1b";
227 }
228 [(set_attr "type" "multi")
229 (set (attr "length") (const_int 28))])
230
231 (define_expand "atomic_fetch_nand<mode>"
232 [(match_operand:SHORT 0 "register_operand") ;; old value at mem
233 (not:SHORT (and:SHORT (match_operand:SHORT 1 "memory_operand") ;; mem location
234 (match_operand:SHORT 2 "reg_or_0_operand"))) ;; value for op
235 (match_operand:SI 3 "const_int_operand")] ;; model
236 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
237 {
238 /* We have no QImode/HImode atomics, so form a mask, then use
239 subword_atomic_fetch_strong_nand to implement a LR/SC version of the
240 operation. */
241
242 /* Logic duplicated in gcc/libgcc/config/riscv/atomic.c for use when inlining
243 is disabled. */
244
245 rtx old = gen_reg_rtx (SImode);
246 rtx mem = operands[1];
247 rtx value = operands[2];
248 rtx model = operands[3];
249 rtx aligned_mem = gen_reg_rtx (SImode);
250 rtx shift = gen_reg_rtx (SImode);
251 rtx mask = gen_reg_rtx (SImode);
252 rtx not_mask = gen_reg_rtx (SImode);
253
254 riscv_subword_address (mem, &aligned_mem, &shift, &mask, &not_mask);
255
256 rtx shifted_value = gen_reg_rtx (SImode);
257 riscv_lshift_subword (<MODE>mode, value, shift, &shifted_value);
258
259 emit_insn (gen_subword_atomic_fetch_strong_nand (old, aligned_mem,
260 shifted_value, model,
261 mask, not_mask));
262
263 emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old,
264 gen_lowpart (QImode, shift)));
265
266 emit_move_insn (operands[0], gen_lowpart (<MODE>mode, old));
267
268 DONE;
269 })
270
271 (define_insn "subword_atomic_fetch_strong_nand"
272 [(set (match_operand:SI 0 "register_operand" "=&r") ;; old value at mem
273 (match_operand:SI 1 "memory_operand" "+A")) ;; mem location
274 (set (match_dup 1)
275 (unspec_volatile:SI
276 [(not:SI (and:SI (match_dup 1)
277 (match_operand:SI 2 "register_operand" "rI"))) ;; value for op
278 (match_operand:SI 3 "const_int_operand")] ;; mask
279 UNSPEC_SYNC_OLD_OP_SUBWORD))
280 (match_operand:SI 4 "register_operand" "rI") ;; mask
281 (match_operand:SI 5 "register_operand" "rI") ;; not_mask
282 (clobber (match_scratch:SI 6 "=&r")) ;; tmp_1
283 (clobber (match_scratch:SI 7 "=&r"))] ;; tmp_2
284 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
285 {
286 return "1:\;"
287 "lr.w%I3\t%0, %1\;"
288 "and\t%6, %0, %2\;"
289 "not\t%6, %6\;"
290 "and\t%6, %6, %4\;"
291 "and\t%7, %0, %5\;"
292 "or\t%7, %7, %6\;"
293 "sc.w%J3\t%6, %7, %1\;"
294 "bnez\t%6, 1b";
295 }
296 [(set_attr "type" "multi")
297 (set (attr "length") (const_int 32))])
298
299 (define_expand "atomic_fetch_<atomic_optab><mode>"
300 [(match_operand:SHORT 0 "register_operand") ;; old value at mem
301 (any_atomic:SHORT (match_operand:SHORT 1 "memory_operand") ;; mem location
302 (match_operand:SHORT 2 "reg_or_0_operand")) ;; value for op
303 (match_operand:SI 3 "const_int_operand")] ;; model
304 "(TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC) || TARGET_ZABHA"
305 {
306 if (TARGET_ZABHA)
307 emit_insn(gen_zabha_atomic_fetch_<atomic_optab><mode> (operands[0], operands[1],
308 operands[2], operands[3]));
309 else
310 emit_insn(gen_lrsc_atomic_fetch_<atomic_optab><mode> (operands[0], operands[1],
311 operands[2], operands[3]));
312 DONE;
313 })
314
315 (define_insn "zabha_atomic_fetch_<atomic_optab><mode>"
316 [(set (match_operand:SHORT 0 "register_operand" "=&r")
317 (match_operand:SHORT 1 "memory_operand" "+A"))
318 (set (match_dup 1)
319 (unspec_volatile:SHORT
320 [(any_atomic:SHORT (match_dup 1)
321 (match_operand:SHORT 2 "reg_or_0_operand" "rJ"))
322 (match_operand:SI 3 "const_int_operand")] ;; model
323 UNSPEC_SYNC_OLD_OP_ZABHA))]
324 "TARGET_ZABHA"
325 "amo<insn>.<amobh>%A3\t%0,%z2,%1"
326 [(set_attr "type" "atomic")
327 (set (attr "length") (const_int 4))])
328
329 (define_expand "lrsc_atomic_fetch_<atomic_optab><mode>"
330 [(match_operand:SHORT 0 "register_operand") ;; old value at mem
331 (any_atomic:SHORT (match_operand:SHORT 1 "memory_operand") ;; mem location
332 (match_operand:SHORT 2 "reg_or_0_operand")) ;; value for op
333 (match_operand:SI 3 "const_int_operand")] ;; model
334 "!TARGET_ZABHA && TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
335 {
336 /* We have no QImode/HImode atomics, so form a mask, then use
337 subword_atomic_fetch_strong_<mode> to implement a LR/SC version of the
338 operation. */
339
340 /* Logic duplicated in gcc/libgcc/config/riscv/atomic.c for use when inlining
341 is disabled. */
342
343 rtx old = gen_reg_rtx (SImode);
344 rtx mem = operands[1];
345 rtx value = operands[2];
346 rtx model = operands[3];
347 rtx aligned_mem = gen_reg_rtx (SImode);
348 rtx shift = gen_reg_rtx (SImode);
349 rtx mask = gen_reg_rtx (SImode);
350 rtx not_mask = gen_reg_rtx (SImode);
351
352 riscv_subword_address (mem, &aligned_mem, &shift, &mask, &not_mask);
353
354 rtx shifted_value = gen_reg_rtx (SImode);
355 riscv_lshift_subword (<MODE>mode, value, shift, &shifted_value);
356
357 emit_insn (gen_subword_atomic_fetch_strong_<atomic_optab> (old, aligned_mem,
358 shifted_value,
359 model, mask,
360 not_mask));
361
362 emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old,
363 gen_lowpart (QImode, shift)));
364
365 emit_move_insn (operands[0], gen_lowpart (<MODE>mode, old));
366
367 DONE;
368 })
369
370 ; Atomic exchange ops
371
372 (define_expand "atomic_exchange<mode>"
373 [(match_operand:GPR 0 "register_operand") ;; old value at mem
374 (match_operand:GPR 1 "memory_operand") ;; mem location
375 (match_operand:GPR 2 "register_operand") ;; value for op
376 (match_operand:SI 3 "const_int_operand")] ;; model
377 "TARGET_ZAAMO || TARGET_ZALRSC"
378 {
379 if (TARGET_ZAAMO)
380 emit_insn (gen_amo_atomic_exchange<mode> (operands[0], operands[1],
381 operands[2], operands[3]));
382 else
383 emit_insn (gen_lrsc_atomic_exchange<mode> (operands[0], operands[1],
384 operands[2], operands[3]));
385 DONE;
386 })
387
388 (define_insn "amo_atomic_exchange<mode>"
389 [(set (match_operand:GPR 0 "register_operand" "=&r")
390 (unspec_volatile:GPR
391 [(match_operand:GPR 1 "memory_operand" "+A")
392 (match_operand:SI 3 "const_int_operand")] ;; model
393 UNSPEC_SYNC_EXCHANGE))
394 (set (match_dup 1)
395 (match_operand:GPR 2 "register_operand" "0"))]
396 "TARGET_ZAAMO"
397 "amoswap.<amo>%A3\t%0,%z2,%1"
398 [(set_attr "type" "atomic")
399 (set (attr "length") (const_int 4))])
400
401 (define_insn "lrsc_atomic_exchange<mode>"
402 [(set (match_operand:GPR 0 "register_operand" "=&r")
403 (unspec_volatile:GPR
404 [(match_operand:GPR 1 "memory_operand" "+A")
405 (match_operand:SI 3 "const_int_operand")] ;; model
406 UNSPEC_SYNC_EXCHANGE))
407 (set (match_dup 1)
408 (match_operand:GPR 2 "register_operand" "0"))
409 (clobber (match_scratch:GPR 4 "=&r"))] ;; tmp_1
410 "!TARGET_ZAAMO && TARGET_ZALRSC"
411 {
412 return "1:\;"
413 "lr.<amo>%I3\t%4, %1\;"
414 "sc.<amo>%J3\t%0, %0, %1\;"
415 "bnez\t%0, 1b\;"
416 "mv\t%0, %4";
417 }
418 [(set_attr "type" "atomic")
419 (set (attr "length") (const_int 16))])
420
421 (define_expand "atomic_exchange<mode>"
422 [(match_operand:SHORT 0 "register_operand") ;; old value at mem
423 (match_operand:SHORT 1 "memory_operand") ;; mem location
424 (match_operand:SHORT 2 "register_operand") ;; value
425 (match_operand:SI 3 "const_int_operand")] ;; model
426 "(TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC) || TARGET_ZABHA"
427 {
428 if (TARGET_ZABHA)
429 emit_insn(gen_zabha_atomic_exchange<mode>(operands[0], operands[1],
430 operands[2], operands[3]));
431 else
432 emit_insn(gen_lrsc_atomic_exchange<mode>(operands[0], operands[1],
433 operands[2], operands[3]));
434 DONE;
435 })
436
437 (define_insn "zabha_atomic_exchange<mode>"
438 [(set (match_operand:SHORT 0 "register_operand" "=&r")
439 (unspec_volatile:SHORT
440 [(match_operand:SHORT 1 "memory_operand" "+A")
441 (match_operand:SI 3 "const_int_operand")] ;; model
442 UNSPEC_SYNC_EXCHANGE_ZABHA))
443 (set (match_dup 1)
444 (match_operand:SHORT 2 "register_operand" "0"))]
445 "TARGET_ZABHA"
446 "amoswap.<amobh>%A3\t%0,%z2,%1"
447 [(set_attr "type" "atomic")
448 (set (attr "length") (const_int 4))])
449
450 (define_expand "lrsc_atomic_exchange<mode>"
451 [(match_operand:SHORT 0 "register_operand") ;; old value at mem
452 (match_operand:SHORT 1 "memory_operand") ;; mem location
453 (match_operand:SHORT 2 "register_operand") ;; value
454 (match_operand:SI 3 "const_int_operand")] ;; model
455 "!TARGET_ZABHA && TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
456 {
457 rtx old = gen_reg_rtx (SImode);
458 rtx mem = operands[1];
459 rtx value = operands[2];
460 rtx model = operands[3];
461 rtx aligned_mem = gen_reg_rtx (SImode);
462 rtx shift = gen_reg_rtx (SImode);
463 rtx mask = gen_reg_rtx (SImode);
464 rtx not_mask = gen_reg_rtx (SImode);
465
466 riscv_subword_address (mem, &aligned_mem, &shift, &mask, &not_mask);
467
468 rtx shifted_value = gen_reg_rtx (SImode);
469 riscv_lshift_subword (<MODE>mode, value, shift, &shifted_value);
470 emit_move_insn (shifted_value, gen_rtx_AND (SImode, shifted_value, mask));
471
472 emit_insn (gen_subword_atomic_exchange_strong (old, aligned_mem,
473 shifted_value, model,
474 not_mask));
475
476 emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old,
477 gen_lowpart (QImode, shift)));
478
479 emit_move_insn (operands[0], gen_lowpart (<MODE>mode, old));
480 DONE;
481 })
482
483 (define_insn "subword_atomic_exchange_strong"
484 [(set (match_operand:SI 0 "register_operand" "=&r") ;; old value at mem
485 (match_operand:SI 1 "memory_operand" "+A")) ;; mem location
486 (set (match_dup 1)
487 (unspec_volatile:SI
488 [(match_operand:SI 2 "reg_or_0_operand" "rI") ;; value
489 (match_operand:SI 3 "const_int_operand")] ;; model
490 UNSPEC_SYNC_EXCHANGE_SUBWORD))
491 (match_operand:SI 4 "reg_or_0_operand" "rI") ;; not_mask
492 (clobber (match_scratch:SI 5 "=&r"))] ;; tmp_1
493 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
494 {
495 return "1:\;"
496 "lr.w%I3\t%0, %1\;"
497 "and\t%5, %0, %4\;"
498 "or\t%5, %5, %2\;"
499 "sc.w%J3\t%5, %5, %1\;"
500 "bnez\t%5, 1b";
501 }
502 [(set_attr "type" "multi")
503 (set (attr "length") (const_int 20))])
504
505 ; Atomic CAS ops
506
507 (define_insn "zalrsc_atomic_cas_value_strong<mode>"
508 [(set (match_operand:GPR 0 "register_operand" "=&r")
509 (match_operand:GPR 1 "memory_operand" "+A"))
510 (set (match_dup 1)
511 (unspec_volatile:GPR [(match_operand:GPR 2 "reg_or_0_operand" "rJ")
512 (match_operand:GPR 3 "reg_or_0_operand" "rJ")
513 (match_operand:SI 4 "const_int_operand") ;; mod_s
514 (match_operand:SI 5 "const_int_operand")] ;; mod_f
515 UNSPEC_COMPARE_AND_SWAP))
516 (clobber (match_scratch:GPR 6 "=&r"))]
517 "TARGET_ZALRSC"
518 {
519 enum memmodel model_success = (enum memmodel) INTVAL (operands[4]);
520 enum memmodel model_failure = (enum memmodel) INTVAL (operands[5]);
521 /* Find the union of the two memory models so we can satisfy both success
522 and failure memory models. */
523 operands[5] = GEN_INT (riscv_union_memmodels (model_success, model_failure));
524 return "1:\;"
525 "lr.<amo>%I5\t%0,%1\;"
526 "bne\t%0,%z2,1f\;"
527 "sc.<amo>%J5\t%6,%z3,%1\;"
528 "bnez\t%6,1b\;"
529 "1:";
530 }
531 [(set_attr "type" "multi")
532 (set (attr "length") (const_int 16))])
533
534 ;; Implement compare_exchange with a conservative leading fence when
535 ;; model_failure is seq_cst.
536 ;; This allows us to be compatible with the ISA manual Table A.6 and Table A.7
537 ;; (A6C and A7).
538 ;; More details: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/444
539 (define_insn "zacas_atomic_cas_value_strong<mode>"
540 [(set (match_operand:GPR 0 "register_operand" "=&r") ;; val output
541 (match_operand:GPR 1 "memory_operand" "+A")) ;; memory
542 (set (match_dup 1)
543 (unspec_volatile:GPR [(match_operand:GPR 2 "register_operand" "0") ;; expected val
544 (match_operand:GPR 3 "reg_or_0_operand" "rJ") ;; desired val
545 (match_operand:SI 4 "const_int_operand") ;; mod_s
546 (match_operand:SI 5 "const_int_operand")] ;; mod_f
547 UNSPEC_COMPARE_AND_SWAP))]
548 "TARGET_ZACAS"
549 {
550 enum memmodel model_success = (enum memmodel) INTVAL (operands[4]);
551 enum memmodel model_failure = (enum memmodel) INTVAL (operands[5]);
552 /* Find the union of the two memory models so we can satisfy both success
553 and failure memory models. */
554 operands[4] = GEN_INT (riscv_union_memmodels (model_success, model_failure));
555
556 if (model_failure == MEMMODEL_SEQ_CST)
557 return "fence\trw,rw\;"
558 "amocas.<amo>%A4\t%0,%z3,%1";
559 else
560 return "amocas.<amo>%A4\t%0,%z3,%1";
561 }
562 [(set_attr "type" "atomic")
563 (set (attr "length")
564 (symbol_ref "(is_mm_seq_cst (memmodel_from_int (INTVAL (operands[5]))) ? 8
565 : 4)"))])
566
567 (define_expand "atomic_compare_and_swap<mode>"
568 [(match_operand:SI 0 "register_operand" "") ;; bool output
569 (match_operand:GPR 1 "register_operand" "") ;; val output
570 (match_operand:GPR 2 "memory_operand" "") ;; memory
571 (match_operand:GPR 3 "register_operand" "") ;; expected value
572 (match_operand:GPR 4 "reg_or_0_operand" "") ;; desired value
573 (match_operand:SI 5 "const_int_operand" "") ;; is_weak
574 (match_operand:SI 6 "const_int_operand" "") ;; mod_s
575 (match_operand:SI 7 "const_int_operand" "")] ;; mod_f
576 "TARGET_ZALRSC || TARGET_ZACAS"
577 {
578 if (word_mode != <MODE>mode && operands[3] != const0_rtx)
579 {
580 /* We don't have SI mode compare on RV64, so we need to make sure expected
581 value is sign-extended. */
582 rtx tmp0 = gen_reg_rtx (word_mode);
583 emit_insn (gen_extend_insn (tmp0, operands[3], word_mode, <MODE>mode, 0));
584 operands[3] = gen_lowpart (<MODE>mode, tmp0);
585 }
586
587 if (TARGET_ZACAS)
588 emit_insn (gen_zacas_atomic_cas_value_strong<mode> (operands[1],
589 operands[2],
590 operands[3],
591 operands[4],
592 operands[6],
593 operands[7]));
594 else
595 emit_insn (gen_zalrsc_atomic_cas_value_strong<mode> (operands[1],
596 operands[2],
597 operands[3],
598 operands[4],
599 operands[6],
600 operands[7]));
601
602 rtx compare = operands[1];
603 if (operands[3] != const0_rtx)
604 {
605 rtx difference = gen_rtx_MINUS (<MODE>mode, operands[1], operands[3]);
606 compare = gen_reg_rtx (<MODE>mode);
607 emit_insn (gen_rtx_SET (compare, difference));
608 }
609
610 if (word_mode != <MODE>mode)
611 {
612 rtx reg = gen_reg_rtx (word_mode);
613 emit_insn (gen_rtx_SET (reg, gen_rtx_SIGN_EXTEND (word_mode, compare)));
614 compare = reg;
615 }
616
617 emit_insn (gen_rtx_SET (operands[0], gen_rtx_EQ (SImode, compare, const0_rtx)));
618 DONE;
619 })
620
621 ;; Implement compare_exchange with a conservative leading fence when
622 ;; model_failure is seq_cst.
623 ;; This allows us to be compatible with the ISA manual Table A.6 and Table A.7
624 ;; (A6C and A7).
625 ;; More details: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/444
626 (define_insn "zacas_atomic_cas_value_strong<mode>"
627 [(set (match_operand:SHORT 0 "register_operand" "=&r") ;; val output
628 (match_operand:SHORT 1 "memory_operand" "+A")) ;; memory
629 (set (match_dup 1)
630 (unspec_volatile:SHORT [(match_operand:SHORT 2 "register_operand" "0") ;; expected_val
631 (match_operand:SHORT 3 "reg_or_0_operand" "rJ") ;; desired_val
632 (match_operand:SI 4 "const_int_operand") ;; mod_s
633 (match_operand:SI 5 "const_int_operand")] ;; mod_f
634 UNSPEC_COMPARE_AND_SWAP))]
635 "TARGET_ZACAS && TARGET_ZABHA"
636 {
637 enum memmodel model_success = (enum memmodel) INTVAL (operands[4]);
638 enum memmodel model_failure = (enum memmodel) INTVAL (operands[5]);
639 /* Find the union of the two memory models so we can satisfy both success
640 and failure memory models. */
641 operands[4] = GEN_INT (riscv_union_memmodels (model_success, model_failure));
642
643 if (model_failure == MEMMODEL_SEQ_CST)
644 return "fence\trw,rw\;"
645 "amocas.<amobh>%A4\t%0,%z3,%1";
646 else
647 return "amocas.<amobh>%A4\t%0,%z3,%1";
648 }
649 [(set_attr "type" "atomic")
650 (set (attr "length")
651 (symbol_ref "(is_mm_seq_cst (memmodel_from_int (INTVAL (operands[5]))) ? 8
652 : 4)"))])
653
654 (define_expand "atomic_compare_and_swap<mode>"
655 [(match_operand:SI 0 "register_operand") ;; bool output
656 (match_operand:SHORT 1 "register_operand") ;; val output
657 (match_operand:SHORT 2 "memory_operand") ;; memory
658 (match_operand:SHORT 3 "register_operand") ;; expected value
659 (match_operand:SHORT 4 "reg_or_0_operand") ;; desired value
660 (match_operand:SI 5 "const_int_operand") ;; is_weak
661 (match_operand:SI 6 "const_int_operand") ;; mod_s
662 (match_operand:SI 7 "const_int_operand")] ;; mod_f
663 "(TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC) || (TARGET_ZACAS && TARGET_ZABHA)"
664 {
665
666 if (TARGET_ZACAS && TARGET_ZABHA)
667 emit_insn (gen_zacas_atomic_cas_value_strong<mode> (operands[1],
668 operands[2],
669 operands[3],
670 operands[4],
671 operands[6],
672 operands[7]));
673 else
674 emit_insn (gen_zalrsc_atomic_cas_value_strong<mode> (operands[1],
675 operands[2],
676 operands[3],
677 operands[4],
678 operands[6],
679 operands[7]));
680
681 rtx val = gen_reg_rtx (SImode);
682 if (operands[1] != const0_rtx)
683 emit_move_insn (val, gen_rtx_SIGN_EXTEND (SImode, operands[1]));
684 else
685 emit_move_insn (val, const0_rtx);
686
687 rtx exp = gen_reg_rtx (SImode);
688 if (operands[3] != const0_rtx)
689 emit_move_insn (exp, gen_rtx_SIGN_EXTEND (SImode, operands[3]));
690 else
691 emit_move_insn (exp, const0_rtx);
692
693 rtx compare = val;
694 if (exp != const0_rtx)
695 {
696 rtx difference = gen_rtx_MINUS (SImode, val, exp);
697 compare = gen_reg_rtx (SImode);
698 emit_move_insn (compare, difference);
699 }
700
701 if (word_mode != SImode)
702 {
703 rtx reg = gen_reg_rtx (word_mode);
704 emit_move_insn (reg, gen_rtx_SIGN_EXTEND (word_mode, compare));
705 compare = reg;
706 }
707
708 emit_move_insn (operands[0], gen_rtx_EQ (SImode, compare, const0_rtx));
709 DONE;
710 })
711
712 (define_expand "zalrsc_atomic_cas_value_strong<mode>"
713 [(match_operand:SHORT 0 "register_operand") ;; val output
714 (match_operand:SHORT 1 "memory_operand") ;; memory
715 (match_operand:SHORT 2 "reg_or_0_operand") ;; expected value
716 (match_operand:SHORT 3 "reg_or_0_operand") ;; desired value
717 (match_operand:SI 4 "const_int_operand") ;; mod_s
718 (match_operand:SI 5 "const_int_operand") ;; mod_f
719 (match_scratch:SHORT 6)]
720 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
721 {
722 /* We have no QImode/HImode atomics, so form a mask, then use
723 subword_atomic_cas_strong<mode> to implement a LR/SC version of the
724 operation. */
725
726 /* Logic duplicated in gcc/libgcc/config/riscv/atomic.c for use when inlining
727 is disabled. */
728
729 rtx old = gen_reg_rtx (SImode);
730 rtx mem = operands[1];
731 rtx aligned_mem = gen_reg_rtx (SImode);
732 rtx shift = gen_reg_rtx (SImode);
733 rtx mask = gen_reg_rtx (SImode);
734 rtx not_mask = gen_reg_rtx (SImode);
735
736 riscv_subword_address (mem, &aligned_mem, &shift, &mask, &not_mask);
737
738 rtx o = operands[2];
739 rtx n = operands[3];
740 rtx shifted_o = gen_reg_rtx (SImode);
741 rtx shifted_n = gen_reg_rtx (SImode);
742
743 riscv_lshift_subword (<MODE>mode, o, shift, &shifted_o);
744 riscv_lshift_subword (<MODE>mode, n, shift, &shifted_n);
745
746 emit_move_insn (shifted_o, gen_rtx_AND (SImode, shifted_o, mask));
747 emit_move_insn (shifted_n, gen_rtx_AND (SImode, shifted_n, mask));
748
749 enum memmodel model_success = (enum memmodel) INTVAL (operands[4]);
750 enum memmodel model_failure = (enum memmodel) INTVAL (operands[5]);
751 /* Find the union of the two memory models so we can satisfy both success
752 and failure memory models. */
753 rtx model = GEN_INT (riscv_union_memmodels (model_success, model_failure));
754
755 emit_insn (gen_subword_atomic_cas_strong (old, aligned_mem,
756 shifted_o, shifted_n,
757 model, mask, not_mask));
758
759 emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old,
760 gen_lowpart (QImode, shift)));
761
762 emit_move_insn (operands[0], gen_lowpart (<MODE>mode, old));
763
764 DONE;
765 })
766
767 (define_insn "subword_atomic_cas_strong"
768 [(set (match_operand:SI 0 "register_operand" "=&r") ;; old value at mem
769 (match_operand:SI 1 "memory_operand" "+A")) ;; mem location
770 (set (match_dup 1)
771 (unspec_volatile:SI [(match_operand:SI 2 "reg_or_0_operand" "rJ") ;; expected value
772 (match_operand:SI 3 "reg_or_0_operand" "rJ")] ;; desired value
773 UNSPEC_COMPARE_AND_SWAP_SUBWORD))
774 (match_operand:SI 4 "const_int_operand") ;; model
775 (match_operand:SI 5 "register_operand" "rI") ;; mask
776 (match_operand:SI 6 "register_operand" "rI") ;; not_mask
777 (clobber (match_scratch:SI 7 "=&r"))] ;; tmp_1
778 "TARGET_ZALRSC && TARGET_INLINE_SUBWORD_ATOMIC"
779 {
780 return "1:\;"
781 "lr.w%I4\t%0, %1\;"
782 "and\t%7, %0, %5\;"
783 "bne\t%7, %z2, 1f\;"
784 "and\t%7, %0, %6\;"
785 "or\t%7, %7, %3\;"
786 "sc.w%J4\t%7, %7, %1\;"
787 "bnez\t%7, 1b\;"
788 "1:";
789 }
790 [(set_attr "type" "multi")
791 (set (attr "length") (const_int 28))])
792
793 (define_expand "atomic_test_and_set"
794 [(match_operand:QI 0 "register_operand" "") ;; bool output
795 (match_operand:QI 1 "memory_operand" "+A") ;; memory
796 (match_operand:SI 2 "const_int_operand" "")] ;; model
797 "TARGET_ZAAMO || TARGET_ZALRSC"
798 {
799 /* We have no QImode atomics, so use the address LSBs to form a mask,
800 then use an aligned SImode atomic. */
801 rtx old = gen_reg_rtx (SImode);
802 rtx mem = operands[1];
803 rtx model = operands[2];
804 rtx set = gen_reg_rtx (QImode);
805 rtx aligned_mem = gen_reg_rtx (SImode);
806 rtx shift = gen_reg_rtx (SImode);
807
808 /* Unused. */
809 rtx _mask = gen_reg_rtx (SImode);
810 rtx _not_mask = gen_reg_rtx (SImode);
811
812 riscv_subword_address (mem, &aligned_mem, &shift, &_mask, &_not_mask);
813
814 emit_move_insn (set, GEN_INT (1));
815 rtx shifted_set = gen_reg_rtx (SImode);
816 riscv_lshift_subword (QImode, set, shift, &shifted_set);
817
818 if (TARGET_ZAAMO)
819 emit_insn (gen_amo_atomic_fetch_orsi (old, aligned_mem, shifted_set, model));
820 else if (TARGET_ZALRSC)
821 emit_insn (gen_lrsc_atomic_fetch_orsi (old, aligned_mem, shifted_set, model));
822
823 emit_move_insn (old, gen_rtx_ASHIFTRT (SImode, old,
824 gen_lowpart (QImode, shift)));
825
826 emit_move_insn (operands[0], gen_lowpart (QImode, old));
827
828 DONE;
829 })