1 ;; Machine description for PowerPC synchronization instructions.
2 ;; Copyright (C) 2005 Free Software Foundation, Inc.
3 ;; Contributed by Geoffrey Keating.
5 ;; This file is part of GCC.
7 ;; GCC is free software; you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published
9 ;; by the Free Software Foundation; either version 2, or (at your
10 ;; option) any later version.
12 ;; GCC is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 ;; License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GCC; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 ;; MA 02110-1301, USA.
22 (define_mode_attr larx [(SI "lwarx") (DI "ldarx")])
23 (define_mode_attr stcx [(SI "stwcx.") (DI "stdcx.")])
25 (define_code_macro FETCHOP [plus minus ior xor and])
26 (define_code_attr fetchop_name
27 [(plus "add") (minus "sub") (ior "ior") (xor "xor") (and "and")])
28 (define_code_attr fetchop_pred
29 [(plus "add_operand") (minus "gpc_reg_operand")
30 (ior "logical_operand") (xor "logical_operand") (and "and_operand")])
31 (define_code_attr fetchopsi_constr
32 [(plus "rIL") (minus "r") (ior "rKL") (xor "rKL") (and "rTKL")])
33 (define_code_attr fetchopdi_constr
34 [(plus "rIL") (minus "r") (ior "rKJF") (xor "rKJF") (and "rSTKJ")])
36 (define_expand "memory_barrier"
37 [(set (mem:BLK (match_dup 0))
38 (unspec:BLK [(mem:BLK (match_dup 0))] UNSPEC_SYNC))]
41 operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
42 MEM_VOLATILE_P (operands[0]) = 1;
45 (define_insn "*sync_internal"
46 [(set (match_operand:BLK 0 "" "")
47 (unspec:BLK [(match_operand:BLK 1 "" "")] UNSPEC_SYNC))]
50 [(set_attr "type" "sync")])
52 (define_insn "load_locked_<mode>"
53 [(set (match_operand:GPR 0 "gpc_reg_operand" "=r")
55 [(match_operand:GPR 1 "memory_operand" "Z")] UNSPECV_LL))]
58 [(set_attr "type" "load_l")])
60 (define_insn "store_conditional_<mode>"
61 [(set (match_operand:CC 0 "cc_reg_operand" "=x")
62 (unspec_volatile:CC [(const_int 0)] UNSPECV_SC))
63 (set (match_operand:GPR 1 "memory_operand" "=Z")
64 (match_operand:GPR 2 "gpc_reg_operand" "r"))]
67 [(set_attr "type" "store_c")])
69 (define_insn_and_split "sync_compare_and_swap<mode>"
70 [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
71 (match_operand:GPR 1 "memory_operand" "+Z"))
74 [(match_operand:GPR 2 "reg_or_short_operand" "rI")
75 (match_operand:GPR 3 "gpc_reg_operand" "r")]
77 (clobber (match_scratch:GPR 4 "=&r"))
78 (clobber (match_scratch:CC 5 "=&x"))]
84 rs6000_split_compare_and_swap (operands[0], operands[1], operands[2],
85 operands[3], operands[4]);
89 (define_expand "sync_compare_and_swaphi"
90 [(match_operand:HI 0 "gpc_reg_operand" "")
91 (match_operand:HI 1 "memory_operand" "")
92 (match_operand:HI 2 "gpc_reg_operand" "")
93 (match_operand:HI 3 "gpc_reg_operand" "")]
96 rs6000_expand_compare_and_swapqhi (operands[0], operands[1],
97 operands[2], operands[3]);
101 (define_expand "sync_compare_and_swapqi"
102 [(match_operand:QI 0 "gpc_reg_operand" "")
103 (match_operand:QI 1 "memory_operand" "")
104 (match_operand:QI 2 "gpc_reg_operand" "")
105 (match_operand:QI 3 "gpc_reg_operand" "")]
108 rs6000_expand_compare_and_swapqhi (operands[0], operands[1],
109 operands[2], operands[3]);
113 (define_insn_and_split "sync_compare_and_swapqhi_internal"
114 [(set (match_operand:SI 0 "gpc_reg_operand" "=&r")
115 (match_operand:SI 4 "memory_operand" "+Z"))
118 [(match_operand:SI 1 "gpc_reg_operand" "r")
119 (match_operand:SI 2 "gpc_reg_operand" "r")
120 (match_operand:SI 3 "gpc_reg_operand" "r")]
122 (clobber (match_scratch:SI 5 "=&r"))
123 (clobber (match_scratch:CC 6 "=&x"))]
126 "&& reload_completed"
129 rs6000_split_compare_and_swapqhi (operands[0], operands[1],
130 operands[2], operands[3], operands[4],
135 (define_insn_and_split "sync_lock_test_and_set<mode>"
136 [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
137 (match_operand:GPR 1 "memory_operand" "+Z"))
140 [(match_operand:GPR 2 "reg_or_short_operand" "rL")]
142 (clobber (match_scratch:GPR 3 "=&r"))
143 (clobber (match_scratch:CC 4 "=&x"))]
146 "&& reload_completed"
149 rs6000_split_lock_test_and_set (operands[0], operands[1], operands[2],
154 (define_expand "sync_<fetchop_name><mode>"
155 [(parallel [(set (match_operand:INT1 0 "memory_operand" "")
157 [(FETCHOP:INT1 (match_dup 0)
158 (match_operand:INT1 1 "<fetchop_pred>" ""))]
160 (clobber (scratch:INT1))
161 (clobber (scratch:CC))])]
165 if (<MODE>mode != SImode && <MODE>mode != DImode)
167 if (PPC405_ERRATUM77)
169 rs6000_emit_sync (<CODE>, <MODE>mode, operands[0], operands[1],
170 NULL_RTX, NULL_RTX, true);
175 (define_insn_and_split "*sync_<fetchop_name>si_internal"
176 [(set (match_operand:SI 0 "memory_operand" "+Z")
178 [(FETCHOP:SI (match_dup 0)
179 (match_operand:SI 1 "<fetchop_pred>" "<fetchopsi_constr>"))]
181 (clobber (match_scratch:SI 2 "=&b"))
182 (clobber (match_scratch:CC 3 "=&x"))]
185 "&& reload_completed"
188 rs6000_split_atomic_op (<CODE>, operands[0], operands[1],
189 NULL_RTX, NULL_RTX, operands[2]);
193 (define_insn_and_split "*sync_<fetchop_name>di_internal"
194 [(set (match_operand:DI 0 "memory_operand" "+Z")
196 [(FETCHOP:DI (match_dup 0)
197 (match_operand:DI 1 "<fetchop_pred>" "<fetchopdi_constr>"))]
199 (clobber (match_scratch:DI 2 "=&b"))
200 (clobber (match_scratch:CC 3 "=&x"))]
203 "&& reload_completed"
206 rs6000_split_atomic_op (<CODE>, operands[0], operands[1],
207 NULL_RTX, NULL_RTX, operands[2]);
211 (define_expand "sync_nand<mode>"
212 [(parallel [(set (match_operand:INT1 0 "memory_operand" "")
214 [(and:INT1 (not:INT1 (match_dup 0))
215 (match_operand:INT1 1 "gpc_reg_operand" ""))]
217 (clobber (scratch:INT1))
218 (clobber (scratch:CC))])]
222 if (<MODE>mode != SImode && <MODE>mode != DImode)
224 if (PPC405_ERRATUM77)
226 rs6000_emit_sync (AND, <MODE>mode,
227 gen_rtx_NOT (<MODE>mode, operands[0]),
229 NULL_RTX, NULL_RTX, true);
234 (define_insn_and_split "*sync_nand<mode>_internal"
235 [(set (match_operand:GPR 0 "memory_operand" "+Z")
237 [(and:GPR (not:GPR (match_dup 0))
238 (match_operand:GPR 1 "gpc_reg_operand" "r"))]
240 (clobber (match_scratch:GPR 2 "=&r"))
241 (clobber (match_scratch:CC 3 "=&x"))]
244 "&& reload_completed"
247 rs6000_split_atomic_op (NOT, operands[0], operands[1],
248 NULL_RTX, NULL_RTX, operands[2]);
252 (define_expand "sync_old_<fetchop_name><mode>"
253 [(parallel [(set (match_operand:INT1 0 "gpc_reg_operand" "")
254 (match_operand:INT1 1 "memory_operand" ""))
257 [(FETCHOP:INT1 (match_dup 1)
258 (match_operand:INT1 2 "<fetchop_pred>" ""))]
260 (clobber (scratch:INT1))
261 (clobber (scratch:CC))])]
265 if (<MODE>mode != SImode && <MODE>mode != DImode)
267 if (PPC405_ERRATUM77)
269 rs6000_emit_sync (<CODE>, <MODE>mode, operands[1], operands[2],
270 operands[0], NULL_RTX, true);
275 (define_insn_and_split "*sync_old_<fetchop_name>si_internal"
276 [(set (match_operand:SI 0 "gpc_reg_operand" "=&r")
277 (match_operand:SI 1 "memory_operand" "+Z"))
280 [(FETCHOP:SI (match_dup 1)
281 (match_operand:SI 2 "<fetchop_pred>" "<fetchopsi_constr>"))]
283 (clobber (match_scratch:SI 3 "=&b"))
284 (clobber (match_scratch:CC 4 "=&x"))]
287 "&& reload_completed"
290 rs6000_split_atomic_op (<CODE>, operands[1], operands[2],
291 operands[0], NULL_RTX, operands[3]);
295 (define_insn_and_split "*sync_old_<fetchop_name>di_internal"
296 [(set (match_operand:DI 0 "gpc_reg_operand" "=&r")
297 (match_operand:DI 1 "memory_operand" "+Z"))
300 [(FETCHOP:DI (match_dup 1)
301 (match_operand:DI 2 "<fetchop_pred>" "<fetchopdi_constr>"))]
303 (clobber (match_scratch:DI 3 "=&b"))
304 (clobber (match_scratch:CC 4 "=&x"))]
307 "&& reload_completed"
310 rs6000_split_atomic_op (<CODE>, operands[1], operands[2],
311 operands[0], NULL_RTX, operands[3]);
315 (define_expand "sync_old_nand<mode>"
316 [(parallel [(set (match_operand:INT1 0 "gpc_reg_operand" "")
317 (match_operand:INT1 1 "memory_operand" ""))
320 [(and:INT1 (not:INT1 (match_dup 1))
321 (match_operand:INT1 2 "gpc_reg_operand" ""))]
323 (clobber (scratch:INT1))
324 (clobber (scratch:CC))])]
328 if (<MODE>mode != SImode && <MODE>mode != DImode)
330 if (PPC405_ERRATUM77)
332 rs6000_emit_sync (AND, <MODE>mode,
333 gen_rtx_NOT (<MODE>mode, operands[1]),
335 operands[0], NULL_RTX, true);
340 (define_insn_and_split "*sync_old_nand<mode>_internal"
341 [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
342 (match_operand:GPR 1 "memory_operand" "+Z"))
345 [(and:GPR (not:GPR (match_dup 1))
346 (match_operand:GPR 2 "gpc_reg_operand" "r"))]
348 (clobber (match_scratch:GPR 3 "=&r"))
349 (clobber (match_scratch:CC 4 "=&x"))]
352 "&& reload_completed"
355 rs6000_split_atomic_op (NOT, operands[1], operands[2],
356 operands[0], NULL_RTX, operands[3]);
360 (define_expand "sync_new_<fetchop_name><mode>"
361 [(parallel [(set (match_operand:INT1 0 "gpc_reg_operand" "")
363 (match_operand:INT1 1 "memory_operand" "")
364 (match_operand:INT1 2 "<fetchop_pred>" "")))
367 [(FETCHOP:INT1 (match_dup 1) (match_dup 2))]
369 (clobber (scratch:INT1))
370 (clobber (scratch:CC))])]
374 if (<MODE>mode != SImode && <MODE>mode != DImode)
376 if (PPC405_ERRATUM77)
378 rs6000_emit_sync (<CODE>, <MODE>mode, operands[1], operands[2],
379 NULL_RTX, operands[0], true);
384 (define_insn_and_split "*sync_new_<fetchop_name>si_internal"
385 [(set (match_operand:SI 0 "gpc_reg_operand" "=&r")
387 (match_operand:SI 1 "memory_operand" "+Z")
388 (match_operand:SI 2 "<fetchop_pred>" "<fetchopsi_constr>")))
391 [(FETCHOP:SI (match_dup 1) (match_dup 2))]
393 (clobber (match_scratch:SI 3 "=&b"))
394 (clobber (match_scratch:CC 4 "=&x"))]
397 "&& reload_completed"
400 rs6000_split_atomic_op (<CODE>, operands[1], operands[2],
401 NULL_RTX, operands[0], operands[3]);
405 (define_insn_and_split "*sync_new_<fetchop_name>di_internal"
406 [(set (match_operand:DI 0 "gpc_reg_operand" "=&r")
408 (match_operand:DI 1 "memory_operand" "+Z")
409 (match_operand:DI 2 "<fetchop_pred>" "<fetchopdi_constr>")))
412 [(FETCHOP:DI (match_dup 1) (match_dup 2))]
414 (clobber (match_scratch:DI 3 "=&b"))
415 (clobber (match_scratch:CC 4 "=&x"))]
418 "&& reload_completed"
421 rs6000_split_atomic_op (<CODE>, operands[1], operands[2],
422 NULL_RTX, operands[0], operands[3]);
426 (define_expand "sync_new_nand<mode>"
427 [(parallel [(set (match_operand:INT1 0 "gpc_reg_operand" "")
429 (not:INT1 (match_operand:INT1 1 "memory_operand" ""))
430 (match_operand:INT1 2 "gpc_reg_operand" "")))
433 [(and:INT1 (not:INT1 (match_dup 1)) (match_dup 2))]
435 (clobber (scratch:INT1))
436 (clobber (scratch:CC))])]
440 if (<MODE>mode != SImode && <MODE>mode != DImode)
442 if (PPC405_ERRATUM77)
444 rs6000_emit_sync (AND, <MODE>mode,
445 gen_rtx_NOT (<MODE>mode, operands[1]),
447 NULL_RTX, operands[0], true);
452 (define_insn_and_split "*sync_new_nand<mode>_internal"
453 [(set (match_operand:GPR 0 "gpc_reg_operand" "=&r")
455 (not:GPR (match_operand:GPR 1 "memory_operand" "+Z"))
456 (match_operand:GPR 2 "gpc_reg_operand" "r")))
459 [(and:GPR (not:GPR (match_dup 1)) (match_dup 2))]
461 (clobber (match_scratch:GPR 3 "=&r"))
462 (clobber (match_scratch:CC 4 "=&x"))]
465 "&& reload_completed"
468 rs6000_split_atomic_op (NOT, operands[1], operands[2],
469 NULL_RTX, operands[0], operands[3]);
473 ; and<mode> without cr0 clobber to avoid generation of additional clobber
474 ; in atomic splitters causing internal consistency failure.
475 ; cr0 already clobbered by larx/stcx.
476 (define_insn "*atomic_andsi"
477 [(set (match_operand:SI 0 "gpc_reg_operand" "=r,r,r,r")
478 (unspec:SI [(match_operand:SI 1 "gpc_reg_operand" "%r,r,r,r")
479 (match_operand:SI 2 "and_operand" "?r,T,K,L")]
484 {rlinm|rlwinm} %0,%1,0,%m2,%M2
485 {andil.|andi.} %0,%1,%b2
486 {andiu.|andis.} %0,%1,%u2"
487 [(set_attr "type" "*,*,compare,compare")])
489 (define_insn "*atomic_anddi"
490 [(set (match_operand:DI 0 "gpc_reg_operand" "=r,r,r,r,r")
491 (unspec:DI [(match_operand:DI 1 "gpc_reg_operand" "%r,r,r,r,r")
492 (match_operand:DI 2 "and_operand" "?r,S,T,K,J")]
498 rlwinm %0,%1,0,%m2,%M2
501 [(set_attr "type" "*,*,*,compare,compare")
502 (set_attr "length" "4,4,4,4,4")])
504 ; the sync_*_internal patterns all have these operands:
505 ; 0 - memory location
507 ; 2 - value in memory after operation
508 ; 3 - value in memory immediately before operation
510 (define_insn "*sync_addshort_internal"
511 [(set (match_operand:SI 2 "gpc_reg_operand" "=&r")
512 (ior:SI (and:SI (plus:SI (match_operand:SI 0 "memory_operand" "+Z")
513 (match_operand:SI 1 "add_operand" "rI"))
514 (match_operand:SI 4 "gpc_reg_operand" "r"))
515 (and:SI (not:SI (match_dup 4)) (match_dup 0))))
516 (set (match_operand:SI 3 "gpc_reg_operand" "=&b") (match_dup 0))
518 (unspec:SI [(ior:SI (and:SI (plus:SI (match_dup 0) (match_dup 1))
520 (and:SI (not:SI (match_dup 4)) (match_dup 0)))]
522 (clobber (match_scratch:CC 5 "=&x"))
523 (clobber (match_scratch:SI 6 "=&r"))]
524 "TARGET_POWERPC && !PPC405_ERRATUM77"
525 "lwarx %3,%y0\n\tadd%I1 %2,%3,%1\n\tandc %6,%3,%4\n\tand %2,%2,%4\n\tor %2,%2,%6\n\tstwcx. %2,%y0\n\tbne- $-24"
526 [(set_attr "length" "28")])
528 (define_insn "*sync_subshort_internal"
529 [(set (match_operand:SI 2 "gpc_reg_operand" "=&r")
530 (ior:SI (and:SI (minus:SI (match_operand:SI 0 "memory_operand" "+Z")
531 (match_operand:SI 1 "add_operand" "rI"))
532 (match_operand:SI 4 "gpc_reg_operand" "r"))
533 (and:SI (not:SI (match_dup 4)) (match_dup 0))))
534 (set (match_operand:SI 3 "gpc_reg_operand" "=&b") (match_dup 0))
536 (unspec:SI [(ior:SI (and:SI (minus:SI (match_dup 0) (match_dup 1))
538 (and:SI (not:SI (match_dup 4)) (match_dup 0)))]
540 (clobber (match_scratch:CC 5 "=&x"))
541 (clobber (match_scratch:SI 6 "=&r"))]
542 "TARGET_POWERPC && !PPC405_ERRATUM77"
543 "lwarx %3,%y0\n\tsubf %2,%1,%3\n\tandc %6,%3,%4\n\tand %2,%2,%4\n\tor %2,%2,%6\n\tstwcx. %2,%y0\n\tbne- $-24"
544 [(set_attr "length" "28")])
546 (define_insn "*sync_andsi_internal"
547 [(set (match_operand:SI 2 "gpc_reg_operand" "=&r,&r,&r,&r")
548 (and:SI (match_operand:SI 0 "memory_operand" "+Z,Z,Z,Z")
549 (match_operand:SI 1 "and_operand" "r,T,K,L")))
550 (set (match_operand:SI 3 "gpc_reg_operand" "=&b,&b,&b,&b") (match_dup 0))
552 (unspec:SI [(and:SI (match_dup 0) (match_dup 1))]
554 (clobber (match_scratch:CC 4 "=&x,&x,&x,&x"))]
555 "TARGET_POWERPC && !PPC405_ERRATUM77"
557 lwarx %3,%y0\n\tand %2,%3,%1\n\tstwcx. %2,%y0\n\tbne- $-12
558 lwarx %3,%y0\n\trlwinm %2,%3,0,%m1,%M1\n\tstwcx. %2,%y0\n\tbne- $-12
559 lwarx %3,%y0\n\tandi. %2,%3,%b1\n\tstwcx. %2,%y0\n\tbne- $-12
560 lwarx %3,%y0\n\tandis. %2,%3,%u1\n\tstwcx. %2,%y0\n\tbne- $-12"
561 [(set_attr "length" "16,16,16,16")])
563 (define_insn "*sync_boolsi_internal"
564 [(set (match_operand:SI 2 "gpc_reg_operand" "=&r,&r,&r")
565 (match_operator:SI 4 "boolean_or_operator"
566 [(match_operand:SI 0 "memory_operand" "+Z,Z,Z")
567 (match_operand:SI 1 "logical_operand" "r,K,L")]))
568 (set (match_operand:SI 3 "gpc_reg_operand" "=&b,&b,&b") (match_dup 0))
569 (set (match_dup 0) (unspec:SI [(match_dup 4)] UNSPEC_SYNC_OP))
570 (clobber (match_scratch:CC 5 "=&x,&x,&x"))]
571 "TARGET_POWERPC && !PPC405_ERRATUM77"
573 lwarx %3,%y0\n\t%q4 %2,%3,%1\n\tstwcx. %2,%y0\n\tbne- $-12
574 lwarx %3,%y0\n\t%q4i %2,%3,%b1\n\tstwcx. %2,%y0\n\tbne- $-12
575 lwarx %3,%y0\n\t%q4is %2,%3,%u1\n\tstwcx. %2,%y0\n\tbne- $-12"
576 [(set_attr "length" "16,16,16")])
578 ; This pattern could also take immediate values of operand 1,
579 ; since the non-NOT version of the operator is used; but this is not
580 ; very useful, since in practice operand 1 is a full 32-bit value.
581 ; Likewise, operand 5 is in practice either <= 2^16 or it is a register.
582 (define_insn "*sync_boolcshort_internal"
583 [(set (match_operand:SI 2 "gpc_reg_operand" "=&r")
584 (match_operator:SI 4 "boolean_operator"
585 [(xor:SI (match_operand:SI 0 "memory_operand" "+Z")
586 (match_operand:SI 5 "logical_operand" "rK"))
587 (match_operand:SI 1 "gpc_reg_operand" "r")]))
588 (set (match_operand:SI 3 "gpc_reg_operand" "=&b") (match_dup 0))
589 (set (match_dup 0) (unspec:SI [(match_dup 4)] UNSPEC_SYNC_OP))
590 (clobber (match_scratch:CC 6 "=&x"))]
591 "TARGET_POWERPC && !PPC405_ERRATUM77"
592 "lwarx %3,%y0\n\txor%I2 %2,%3,%5\n\t%q4 %2,%2,%1\n\tstwcx. %2,%y0\n\tbne- $-16"
593 [(set_attr "length" "20")])
596 [(set (mem:BLK (match_scratch 0 "X"))
597 (unspec_volatile:BLK [(mem:BLK (match_scratch 1 "X"))] UNSPEC_ISYNC))]
600 [(set_attr "type" "isync")])
602 (define_expand "sync_lock_release<mode>"
603 [(set (match_operand:INT 0 "memory_operand")
604 (match_operand:INT 1 "any_operand"))]
608 emit_insn (gen_lwsync ());
609 emit_move_insn (operands[0], operands[1]);
613 ; Some AIX assemblers don't accept lwsync, so we use a .long.
614 (define_insn "lwsync"
615 [(set (mem:BLK (match_scratch 0 "X"))
616 (unspec_volatile:BLK [(mem:BLK (match_scratch 1 "X"))] UNSPEC_LWSYNC))]
619 if (TARGET_NO_LWSYNC)
622 return ".long 0x7c2004ac";
624 [(set_attr "type" "sync")])