2016-11-17 Aaron Sawdey <acsawdey@linux.vnet.ibm.com>
[official-gcc.git] / gcc / config / i386 / sync.md
blobb065e2365ffadc3663843f57e2028975e0060eb2
1 ;; GCC machine description for i386 synchronization instructions.
2 ;; Copyright (C) 2005-2016 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.
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/>.
20 (define_c_enum "unspec" [
21   UNSPEC_LFENCE
22   UNSPEC_SFENCE
23   UNSPEC_MFENCE
25   UNSPEC_FILD_ATOMIC
26   UNSPEC_FIST_ATOMIC
28   ;; __atomic support
29   UNSPEC_LDA
30   UNSPEC_STA
33 (define_c_enum "unspecv" [
34   UNSPECV_CMPXCHG
35   UNSPECV_XCHG
36   UNSPECV_LOCK
39 (define_expand "sse2_lfence"
40   [(set (match_dup 0)
41         (unspec:BLK [(match_dup 0)] UNSPEC_LFENCE))]
42   "TARGET_SSE2"
44   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
45   MEM_VOLATILE_P (operands[0]) = 1;
48 (define_insn "*sse2_lfence"
49   [(set (match_operand:BLK 0)
50         (unspec:BLK [(match_dup 0)] UNSPEC_LFENCE))]
51   "TARGET_SSE2"
52   "lfence"
53   [(set_attr "type" "sse")
54    (set_attr "length_address" "0")
55    (set_attr "atom_sse_attr" "lfence")
56    (set_attr "memory" "unknown")])
58 (define_expand "sse_sfence"
59   [(set (match_dup 0)
60         (unspec:BLK [(match_dup 0)] UNSPEC_SFENCE))]
61   "TARGET_SSE || TARGET_3DNOW_A"
63   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
64   MEM_VOLATILE_P (operands[0]) = 1;
67 (define_insn "*sse_sfence"
68   [(set (match_operand:BLK 0)
69         (unspec:BLK [(match_dup 0)] UNSPEC_SFENCE))]
70   "TARGET_SSE || TARGET_3DNOW_A"
71   "sfence"
72   [(set_attr "type" "sse")
73    (set_attr "length_address" "0")
74    (set_attr "atom_sse_attr" "fence")
75    (set_attr "memory" "unknown")])
77 (define_expand "sse2_mfence"
78   [(set (match_dup 0)
79         (unspec:BLK [(match_dup 0)] UNSPEC_MFENCE))]
80   "TARGET_SSE2"
82   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
83   MEM_VOLATILE_P (operands[0]) = 1;
86 (define_insn "mfence_sse2"
87   [(set (match_operand:BLK 0)
88         (unspec:BLK [(match_dup 0)] UNSPEC_MFENCE))]
89   "TARGET_64BIT || TARGET_SSE2"
90   "mfence"
91   [(set_attr "type" "sse")
92    (set_attr "length_address" "0")
93    (set_attr "atom_sse_attr" "fence")
94    (set_attr "memory" "unknown")])
96 (define_insn "mfence_nosse"
97   [(set (match_operand:BLK 0)
98         (unspec:BLK [(match_dup 0)] UNSPEC_MFENCE))
99    (clobber (reg:CC FLAGS_REG))]
100   "!(TARGET_64BIT || TARGET_SSE2)"
101   "lock{%;} or{l}\t{$0, -4(%%esp)|DWORD PTR [esp-4], 0}"
102   [(set_attr "memory" "unknown")])
104 (define_expand "mem_thread_fence"
105   [(match_operand:SI 0 "const_int_operand")]            ;; model
106   ""
108   enum memmodel model = memmodel_from_int (INTVAL (operands[0]));
110   /* Unless this is a SEQ_CST fence, the i386 memory model is strong
111      enough not to require barriers of any kind.  */
112   if (is_mm_seq_cst (model))
113     {
114       rtx (*mfence_insn)(rtx);
115       rtx mem;
117       if (TARGET_64BIT || TARGET_SSE2)
118         mfence_insn = gen_mfence_sse2;
119       else
120         mfence_insn = gen_mfence_nosse;
122       mem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
123       MEM_VOLATILE_P (mem) = 1;
125       emit_insn (mfence_insn (mem));
126     }
127   DONE;
130 ;; ??? From volume 3 section 8.1.1 Guaranteed Atomic Operations,
131 ;; Only beginning at Pentium family processors do we get any guarantee of
132 ;; atomicity in aligned 64-bit quantities.  Beginning at P6, we get a
133 ;; guarantee for 64-bit accesses that do not cross a cacheline boundary.
135 ;; Note that the TARGET_CMPXCHG8B test below is a stand-in for "Pentium".
137 ;; Importantly, *no* processor makes atomicity guarantees for larger
138 ;; accesses.  In particular, there's no way to perform an atomic TImode
139 ;; move, despite the apparent applicability of MOVDQA et al.
141 (define_mode_iterator ATOMIC
142    [QI HI SI
143     (DI "TARGET_64BIT || (TARGET_CMPXCHG8B && (TARGET_80387 || TARGET_SSE))")
144    ])
146 (define_expand "atomic_load<mode>"
147   [(set (match_operand:ATOMIC 0 "nonimmediate_operand")
148         (unspec:ATOMIC [(match_operand:ATOMIC 1 "memory_operand")
149                         (match_operand:SI 2 "const_int_operand")]
150                        UNSPEC_LDA))]
151   ""
153   /* For DImode on 32-bit, we can use the FPU to perform the load.  */
154   if (<MODE>mode == DImode && !TARGET_64BIT)
155     emit_insn (gen_atomic_loaddi_fpu
156                (operands[0], operands[1],
157                 assign_386_stack_local (DImode, SLOT_TEMP)));
158   else
159     {
160       rtx dst = operands[0];
162       if (MEM_P (dst))
163         dst = gen_reg_rtx (<MODE>mode);
165       emit_move_insn (dst, operands[1]);
167       /* Fix up the destination if needed.  */
168       if (dst != operands[0])
169         emit_move_insn (operands[0], dst);
170     }
171   DONE;
174 (define_insn_and_split "atomic_loaddi_fpu"
175   [(set (match_operand:DI 0 "nonimmediate_operand" "=x,m,?r")
176         (unspec:DI [(match_operand:DI 1 "memory_operand" "m,m,m")]
177                    UNSPEC_LDA))
178    (clobber (match_operand:DI 2 "memory_operand" "=X,X,m"))
179    (clobber (match_scratch:DF 3 "=X,xf,xf"))]
180   "!TARGET_64BIT && (TARGET_80387 || TARGET_SSE)"
181   "#"
182   "&& reload_completed"
183   [(const_int 0)]
185   rtx dst = operands[0], src = operands[1];
186   rtx mem = operands[2], tmp = operands[3];
188   if (SSE_REG_P (dst))
189     emit_move_insn (dst, src);
190   else
191     {
192       if (MEM_P (dst))
193         mem = dst;
195       if (STACK_REG_P (tmp))
196         {
197           emit_insn (gen_loaddi_via_fpu (tmp, src));
198           emit_insn (gen_storedi_via_fpu (mem, tmp));
199         }
200       else
201         {
202           adjust_reg_mode (tmp, DImode);
203           emit_move_insn (tmp, src);
204           emit_move_insn (mem, tmp);
205         }
207       if (mem != dst)
208         emit_move_insn (dst, mem);
209     }
210   DONE;
213 (define_peephole2
214   [(set (match_operand:DF 0 "fp_register_operand")
215         (unspec:DF [(match_operand:DI 1 "memory_operand")]
216                    UNSPEC_FILD_ATOMIC))
217    (set (match_operand:DI 2 "memory_operand")
218         (unspec:DI [(match_dup 0)]
219                    UNSPEC_FIST_ATOMIC))
220    (set (match_operand:DF 3 "fp_register_operand")
221         (match_operand:DF 4 "memory_operand"))]
222   "!TARGET_64BIT
223    && peep2_reg_dead_p (2, operands[0])
224    && rtx_equal_p (operands[4], adjust_address_nv (operands[2], DFmode, 0))"
225   [(set (match_dup 3) (match_dup 5))]
226   "operands[5] = gen_lowpart (DFmode, operands[1]);")
228 (define_peephole2
229   [(set (match_operand:DI 0 "sse_reg_operand")
230         (match_operand:DI 1 "memory_operand"))
231    (set (match_operand:DI 2 "memory_operand")
232         (match_dup 0))
233    (set (match_operand:DF 3 "fp_register_operand")
234         (match_operand:DF 4 "memory_operand"))]
235   "!TARGET_64BIT
236    && peep2_reg_dead_p (2, operands[0])
237    && rtx_equal_p (operands[4], adjust_address_nv (operands[2], DFmode, 0))"
238   [(set (match_dup 3) (match_dup 5))]
239   "operands[5] = gen_lowpart (DFmode, operands[1]);")
241 (define_expand "atomic_store<mode>"
242   [(set (match_operand:ATOMIC 0 "memory_operand")
243         (unspec:ATOMIC [(match_operand:ATOMIC 1 "nonimmediate_operand")
244                         (match_operand:SI 2 "const_int_operand")]
245                        UNSPEC_STA))]
246   ""
248   enum memmodel model = memmodel_from_int (INTVAL (operands[2]));
250   if (<MODE>mode == DImode && !TARGET_64BIT)
251     {
252       /* For DImode on 32-bit, we can use the FPU to perform the store.  */
253       /* Note that while we could perform a cmpxchg8b loop, that turns
254          out to be significantly larger than this plus a barrier.  */
255       emit_insn (gen_atomic_storedi_fpu
256                  (operands[0], operands[1],
257                   assign_386_stack_local (DImode, SLOT_TEMP)));
258     }
259   else
260     {
261       operands[1] = force_reg (<MODE>mode, operands[1]);
263       /* For seq-cst stores, when we lack MFENCE, use XCHG.  */
264       if (is_mm_seq_cst (model) && !(TARGET_64BIT || TARGET_SSE2))
265         {
266           emit_insn (gen_atomic_exchange<mode> (gen_reg_rtx (<MODE>mode),
267                                                 operands[0], operands[1],
268                                                 operands[2]));
269           DONE;
270         }
272       /* Otherwise use a store.  */
273       emit_insn (gen_atomic_store<mode>_1 (operands[0], operands[1],
274                                            operands[2]));
275     }
276   /* ... followed by an MFENCE, if required.  */
277   if (is_mm_seq_cst (model))
278     emit_insn (gen_mem_thread_fence (operands[2]));
279   DONE;
282 (define_insn "atomic_store<mode>_1"
283   [(set (match_operand:SWI 0 "memory_operand" "=m")
284         (unspec:SWI [(match_operand:SWI 1 "<nonmemory_operand>" "<r><i>")
285                      (match_operand:SI 2 "const_int_operand")]
286                     UNSPEC_STA))]
287   ""
288   "%K2mov{<imodesuffix>}\t{%1, %0|%0, %1}")
290 (define_insn_and_split "atomic_storedi_fpu"
291   [(set (match_operand:DI 0 "memory_operand" "=m,m,m")
292         (unspec:DI [(match_operand:DI 1 "nonimmediate_operand" "x,m,?r")]
293                    UNSPEC_STA))
294    (clobber (match_operand:DI 2 "memory_operand" "=X,X,m"))
295    (clobber (match_scratch:DF 3 "=X,xf,xf"))]
296   "!TARGET_64BIT && (TARGET_80387 || TARGET_SSE)"
297   "#"
298   "&& reload_completed"
299   [(const_int 0)]
301   rtx dst = operands[0], src = operands[1];
302   rtx mem = operands[2], tmp = operands[3];
304   if (!SSE_REG_P (src))
305     {
306       if (REG_P (src))
307         {
308           emit_move_insn (mem, src);
309           src = mem;
310         }
312       if (STACK_REG_P (tmp))
313         {
314           emit_insn (gen_loaddi_via_fpu (tmp, src));
315           emit_insn (gen_storedi_via_fpu (dst, tmp));
316           DONE;
317         }
318       else
319         {
320           adjust_reg_mode (tmp, DImode);
321           emit_move_insn (tmp, src);
322           src = tmp;
323         }
324     }
325   emit_move_insn (dst, src);
326   DONE;
329 (define_peephole2
330   [(set (match_operand:DF 0 "memory_operand")
331         (match_operand:DF 1 "fp_register_operand"))
332    (set (match_operand:DF 2 "fp_register_operand")
333         (unspec:DF [(match_operand:DI 3 "memory_operand")]
334                    UNSPEC_FILD_ATOMIC))
335    (set (match_operand:DI 4 "memory_operand")
336         (unspec:DI [(match_dup 2)]
337                    UNSPEC_FIST_ATOMIC))]
338   "!TARGET_64BIT
339    && peep2_reg_dead_p (3, operands[2])
340    && rtx_equal_p (operands[0], adjust_address_nv (operands[3], DFmode, 0))"
341   [(set (match_dup 5) (match_dup 1))]
342   "operands[5] = gen_lowpart (DFmode, operands[4]);")
344 (define_peephole2
345   [(set (match_operand:DF 0 "memory_operand")
346         (match_operand:DF 1 "fp_register_operand"))
347    (set (match_operand:DI 2 "sse_reg_operand")
348         (match_operand:DI 3 "memory_operand"))
349    (set (match_operand:DI 4 "memory_operand")
350         (match_dup 2))]
351   "!TARGET_64BIT
352    && peep2_reg_dead_p (3, operands[2])
353    && rtx_equal_p (operands[0], adjust_address_nv (operands[3], DFmode, 0))"
354   [(set (match_dup 5) (match_dup 1))]
355   "operands[5] = gen_lowpart (DFmode, operands[4]);")
357 ;; ??? You'd think that we'd be able to perform this via FLOAT + FIX_TRUNC
358 ;; operations.  But the fix_trunc patterns want way more setup than we want
359 ;; to provide.  Note that the scratch is DFmode instead of XFmode in order
360 ;; to make it easy to allocate a scratch in either SSE or FP_REGs above.
362 (define_insn "loaddi_via_fpu"
363   [(set (match_operand:DF 0 "register_operand" "=f")
364         (unspec:DF [(match_operand:DI 1 "memory_operand" "m")]
365                    UNSPEC_FILD_ATOMIC))]
366   "TARGET_80387"
367   "fild%Z1\t%1"
368   [(set_attr "type" "fmov")
369    (set_attr "mode" "DF")
370    (set_attr "fp_int_src" "true")])
372 (define_insn "storedi_via_fpu"
373   [(set (match_operand:DI 0 "memory_operand" "=m")
374         (unspec:DI [(match_operand:DF 1 "register_operand" "f")]
375                    UNSPEC_FIST_ATOMIC))]
376   "TARGET_80387"
378   gcc_assert (find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != NULL_RTX);
380   return "fistp%Z0\t%0";
382   [(set_attr "type" "fmov")
383    (set_attr "mode" "DI")])
385 (define_expand "atomic_compare_and_swap<mode>"
386   [(match_operand:QI 0 "register_operand")      ;; bool success output
387    (match_operand:SWI124 1 "register_operand")  ;; oldval output
388    (match_operand:SWI124 2 "memory_operand")    ;; memory
389    (match_operand:SWI124 3 "register_operand")  ;; expected input
390    (match_operand:SWI124 4 "register_operand")  ;; newval input
391    (match_operand:SI 5 "const_int_operand")     ;; is_weak
392    (match_operand:SI 6 "const_int_operand")     ;; success model
393    (match_operand:SI 7 "const_int_operand")]    ;; failure model
394   "TARGET_CMPXCHG"
396   emit_insn
397    (gen_atomic_compare_and_swap<mode>_1
398     (operands[1], operands[2], operands[3], operands[4], operands[6]));
399   ix86_expand_setcc (operands[0], EQ, gen_rtx_REG (CCZmode, FLAGS_REG),
400                      const0_rtx);
401   DONE;
404 (define_mode_iterator CASMODE
405   [(DI "TARGET_64BIT || TARGET_CMPXCHG8B")
406    (TI "TARGET_64BIT && TARGET_CMPXCHG16B")])
407 (define_mode_attr CASHMODE [(DI "SI") (TI "DI")])
409 (define_expand "atomic_compare_and_swap<mode>"
410   [(match_operand:QI 0 "register_operand")      ;; bool success output
411    (match_operand:CASMODE 1 "register_operand") ;; oldval output
412    (match_operand:CASMODE 2 "memory_operand")   ;; memory
413    (match_operand:CASMODE 3 "register_operand") ;; expected input
414    (match_operand:CASMODE 4 "register_operand") ;; newval input
415    (match_operand:SI 5 "const_int_operand")     ;; is_weak
416    (match_operand:SI 6 "const_int_operand")     ;; success model
417    (match_operand:SI 7 "const_int_operand")]    ;; failure model
418   "TARGET_CMPXCHG"
420   if (<MODE>mode == DImode && TARGET_64BIT)
421     {
422       emit_insn
423        (gen_atomic_compare_and_swapdi_1
424         (operands[1], operands[2], operands[3], operands[4], operands[6]));
425     }
426   else
427     {
428       machine_mode hmode = <CASHMODE>mode;
430       emit_insn
431        (gen_atomic_compare_and_swap<mode>_doubleword
432         (operands[1], operands[2], operands[3],
433          gen_lowpart (hmode, operands[4]), gen_highpart (hmode, operands[4]),
434          operands[6]));
435     }
437   ix86_expand_setcc (operands[0], EQ, gen_rtx_REG (CCZmode, FLAGS_REG),
438                      const0_rtx);
439   DONE;
442 ;; For double-word compare and swap, we are obliged to play tricks with
443 ;; the input newval (op3:op4) because the Intel register numbering does
444 ;; not match the gcc register numbering, so the pair must be CX:BX.
446 (define_mode_attr doublemodesuffix [(SI "8") (DI "16")])
448 (define_insn "atomic_compare_and_swap<dwi>_doubleword"
449   [(set (match_operand:<DWI> 0 "register_operand" "=A")
450         (unspec_volatile:<DWI>
451           [(match_operand:<DWI> 1 "memory_operand" "+m")
452            (match_operand:<DWI> 2 "register_operand" "0")
453            (match_operand:DWIH 3 "register_operand" "b")
454            (match_operand:DWIH 4 "register_operand" "c")
455            (match_operand:SI 5 "const_int_operand")]
456           UNSPECV_CMPXCHG))
457    (set (match_dup 1)
458         (unspec_volatile:<DWI> [(const_int 0)] UNSPECV_CMPXCHG))
459    (set (reg:CCZ FLAGS_REG)
460         (unspec_volatile:CCZ [(const_int 0)] UNSPECV_CMPXCHG))]
461   "TARGET_CMPXCHG<doublemodesuffix>B"
462   "lock{%;} %K5cmpxchg<doublemodesuffix>b\t%1")
464 (define_insn "atomic_compare_and_swap<mode>_1"
465   [(set (match_operand:SWI 0 "register_operand" "=a")
466         (unspec_volatile:SWI
467           [(match_operand:SWI 1 "memory_operand" "+m")
468            (match_operand:SWI 2 "register_operand" "0")
469            (match_operand:SWI 3 "register_operand" "<r>")
470            (match_operand:SI 4 "const_int_operand")]
471           UNSPECV_CMPXCHG))
472    (set (match_dup 1)
473         (unspec_volatile:SWI [(const_int 0)] UNSPECV_CMPXCHG))
474    (set (reg:CCZ FLAGS_REG)
475         (unspec_volatile:CCZ [(const_int 0)] UNSPECV_CMPXCHG))]
476   "TARGET_CMPXCHG"
477   "lock{%;} %K4cmpxchg{<imodesuffix>}\t{%3, %1|%1, %3}")
479 ;; For operand 2 nonmemory_operand predicate is used instead of
480 ;; register_operand to allow combiner to better optimize atomic
481 ;; additions of constants.
482 (define_insn "atomic_fetch_add<mode>"
483   [(set (match_operand:SWI 0 "register_operand" "=<r>")
484         (unspec_volatile:SWI
485           [(match_operand:SWI 1 "memory_operand" "+m")
486            (match_operand:SI 3 "const_int_operand")]            ;; model
487           UNSPECV_XCHG))
488    (set (match_dup 1)
489         (plus:SWI (match_dup 1)
490                   (match_operand:SWI 2 "nonmemory_operand" "0")))
491    (clobber (reg:CC FLAGS_REG))]
492   "TARGET_XADD"
493   "lock{%;} %K3xadd{<imodesuffix>}\t{%0, %1|%1, %0}")
495 ;; This peephole2 and following insn optimize
496 ;; __sync_fetch_and_add (x, -N) == N into just lock {add,sub,inc,dec}
497 ;; followed by testing of flags instead of lock xadd and comparisons.
498 (define_peephole2
499   [(set (match_operand:SWI 0 "register_operand")
500         (match_operand:SWI 2 "const_int_operand"))
501    (parallel [(set (match_dup 0)
502                    (unspec_volatile:SWI
503                      [(match_operand:SWI 1 "memory_operand")
504                       (match_operand:SI 4 "const_int_operand")]
505                      UNSPECV_XCHG))
506               (set (match_dup 1)
507                    (plus:SWI (match_dup 1)
508                              (match_dup 0)))
509               (clobber (reg:CC FLAGS_REG))])
510    (set (reg:CCZ FLAGS_REG)
511         (compare:CCZ (match_dup 0)
512                      (match_operand:SWI 3 "const_int_operand")))]
513   "peep2_reg_dead_p (3, operands[0])
514    && (unsigned HOST_WIDE_INT) INTVAL (operands[2])
515       == -(unsigned HOST_WIDE_INT) INTVAL (operands[3])
516    && !reg_overlap_mentioned_p (operands[0], operands[1])"
517   [(parallel [(set (reg:CCZ FLAGS_REG)
518                    (compare:CCZ
519                      (unspec_volatile:SWI [(match_dup 1) (match_dup 4)]
520                                           UNSPECV_XCHG)
521                      (match_dup 3)))
522               (set (match_dup 1)
523                    (plus:SWI (match_dup 1)
524                              (match_dup 2)))])])
526 ;; Likewise, but for the -Os special case of *mov<mode>_or.
527 (define_peephole2
528   [(parallel [(set (match_operand:SWI 0 "register_operand")
529                    (match_operand:SWI 2 "constm1_operand"))
530               (clobber (reg:CC FLAGS_REG))])
531    (parallel [(set (match_dup 0)
532                    (unspec_volatile:SWI
533                      [(match_operand:SWI 1 "memory_operand")
534                       (match_operand:SI 4 "const_int_operand")]
535                      UNSPECV_XCHG))
536               (set (match_dup 1)
537                    (plus:SWI (match_dup 1)
538                              (match_dup 0)))
539               (clobber (reg:CC FLAGS_REG))])
540    (set (reg:CCZ FLAGS_REG)
541         (compare:CCZ (match_dup 0)
542                      (match_operand:SWI 3 "const_int_operand")))]
543   "peep2_reg_dead_p (3, operands[0])
544    && (unsigned HOST_WIDE_INT) INTVAL (operands[2])
545       == -(unsigned HOST_WIDE_INT) INTVAL (operands[3])
546    && !reg_overlap_mentioned_p (operands[0], operands[1])"
547   [(parallel [(set (reg:CCZ FLAGS_REG)
548                    (compare:CCZ
549                      (unspec_volatile:SWI [(match_dup 1) (match_dup 4)]
550                                           UNSPECV_XCHG)
551                      (match_dup 3)))
552               (set (match_dup 1)
553                    (plus:SWI (match_dup 1)
554                              (match_dup 2)))])])
556 (define_insn "*atomic_fetch_add_cmp<mode>"
557   [(set (reg:CCZ FLAGS_REG)
558         (compare:CCZ
559           (unspec_volatile:SWI
560             [(match_operand:SWI 0 "memory_operand" "+m")
561              (match_operand:SI 3 "const_int_operand")]          ;; model
562             UNSPECV_XCHG)
563           (match_operand:SWI 2 "const_int_operand" "i")))
564    (set (match_dup 0)
565         (plus:SWI (match_dup 0)
566                   (match_operand:SWI 1 "const_int_operand" "i")))]
567   "(unsigned HOST_WIDE_INT) INTVAL (operands[1])
568    == -(unsigned HOST_WIDE_INT) INTVAL (operands[2])"
570   if (incdec_operand (operands[1], <MODE>mode))
571     {
572       if (operands[1] == const1_rtx)
573         return "lock{%;} %K3inc{<imodesuffix>}\t%0";
574       else
575         {
576           gcc_assert (operands[1] == constm1_rtx);
577           return "lock{%;} %K3dec{<imodesuffix>}\t%0";
578         }
579     }
581   if (x86_maybe_negate_const_int (&operands[1], <MODE>mode))
582     return "lock{%;} %K3sub{<imodesuffix>}\t{%1, %0|%0, %1}";
584   return "lock{%;} %K3add{<imodesuffix>}\t{%1, %0|%0, %1}";
587 ;; Recall that xchg implicitly sets LOCK#, so adding it again wastes space.
588 ;; In addition, it is always a full barrier, so we can ignore the memory model.
589 (define_insn "atomic_exchange<mode>"
590   [(set (match_operand:SWI 0 "register_operand" "=<r>")         ;; output
591         (unspec_volatile:SWI
592           [(match_operand:SWI 1 "memory_operand" "+m")          ;; memory
593            (match_operand:SI 3 "const_int_operand")]            ;; model
594           UNSPECV_XCHG))
595    (set (match_dup 1)
596         (match_operand:SWI 2 "register_operand" "0"))]          ;; input
597   ""
598   "%K3xchg{<imodesuffix>}\t{%1, %0|%0, %1}")
600 (define_insn "atomic_add<mode>"
601   [(set (match_operand:SWI 0 "memory_operand" "+m")
602         (unspec_volatile:SWI
603           [(plus:SWI (match_dup 0)
604                      (match_operand:SWI 1 "nonmemory_operand" "<r><i>"))
605            (match_operand:SI 2 "const_int_operand")]            ;; model
606           UNSPECV_LOCK))
607    (clobber (reg:CC FLAGS_REG))]
608   ""
610   if (incdec_operand (operands[1], <MODE>mode))
611     {
612       if (operands[1] == const1_rtx)
613         return "lock{%;} %K2inc{<imodesuffix>}\t%0";
614       else
615         {
616           gcc_assert (operands[1] == constm1_rtx);
617           return "lock{%;} %K2dec{<imodesuffix>}\t%0";
618         }
619     }
621   if (x86_maybe_negate_const_int (&operands[1], <MODE>mode))
622     return "lock{%;} %K2sub{<imodesuffix>}\t{%1, %0|%0, %1}";
624   return "lock{%;} %K2add{<imodesuffix>}\t{%1, %0|%0, %1}";
627 (define_insn "atomic_sub<mode>"
628   [(set (match_operand:SWI 0 "memory_operand" "+m")
629         (unspec_volatile:SWI
630           [(minus:SWI (match_dup 0)
631                       (match_operand:SWI 1 "nonmemory_operand" "<r><i>"))
632            (match_operand:SI 2 "const_int_operand")]            ;; model
633           UNSPECV_LOCK))
634    (clobber (reg:CC FLAGS_REG))]
635   ""
637   if (incdec_operand (operands[1], <MODE>mode))
638     {
639       if (operands[1] == const1_rtx)
640         return "lock{%;} %K2dec{<imodesuffix>}\t%0";
641       else
642         {
643           gcc_assert (operands[1] == constm1_rtx);
644           return "lock{%;} %K2inc{<imodesuffix>}\t%0";
645         }
646     }
648   if (x86_maybe_negate_const_int (&operands[1], <MODE>mode))
649     return "lock{%;} %K2add{<imodesuffix>}\t{%1, %0|%0, %1}";
651   return "lock{%;} %K2sub{<imodesuffix>}\t{%1, %0|%0, %1}";
654 (define_insn "atomic_<logic><mode>"
655   [(set (match_operand:SWI 0 "memory_operand" "+m")
656         (unspec_volatile:SWI
657           [(any_logic:SWI (match_dup 0)
658                           (match_operand:SWI 1 "nonmemory_operand" "<r><i>"))
659            (match_operand:SI 2 "const_int_operand")]            ;; model
660           UNSPECV_LOCK))
661    (clobber (reg:CC FLAGS_REG))]
662   ""
663   "lock{%;} %K2<logic>{<imodesuffix>}\t{%1, %0|%0, %1}")
665 (define_expand "atomic_bit_test_and_set<mode>"
666   [(match_operand:SWI248 0 "register_operand")
667    (match_operand:SWI248 1 "memory_operand")
668    (match_operand:SWI248 2 "nonmemory_operand")
669    (match_operand:SI 3 "const_int_operand") ;; model
670    (match_operand:SI 4 "const_int_operand")]
671   ""
673   emit_insn (gen_atomic_bit_test_and_set<mode>_1 (operands[1], operands[2],
674                                                   operands[3]));
675   rtx tem = gen_reg_rtx (QImode);
676   ix86_expand_setcc (tem, EQ, gen_rtx_REG (CCCmode, FLAGS_REG), const0_rtx);
677   rtx result = convert_modes (<MODE>mode, QImode, tem, 1);
678   if (operands[4] == const0_rtx)
679     result = expand_simple_binop (<MODE>mode, ASHIFT, result,
680                                   operands[2], operands[0], 0, OPTAB_DIRECT);
681   if (result != operands[0])
682     emit_move_insn (operands[0], result);
683   DONE;
686 (define_insn "atomic_bit_test_and_set<mode>_1"
687   [(set (reg:CCC FLAGS_REG)
688         (compare:CCC
689           (unspec_volatile:SWI248
690             [(match_operand:SWI248 0 "memory_operand" "+m")
691              (match_operand:SI 2 "const_int_operand")]          ;; model
692             UNSPECV_XCHG)
693           (const_int 0)))
694    (set (zero_extract:SWI248 (match_dup 0)
695                              (const_int 1)
696                              (match_operand:SWI248 1 "nonmemory_operand" "rN"))
697         (const_int 1))]
698   ""
699   "lock{%;} %K2bts{<imodesuffix>}\t{%1, %0|%0, %1}")
701 (define_expand "atomic_bit_test_and_complement<mode>"
702   [(match_operand:SWI248 0 "register_operand")
703    (match_operand:SWI248 1 "memory_operand")
704    (match_operand:SWI248 2 "nonmemory_operand")
705    (match_operand:SI 3 "const_int_operand") ;; model
706    (match_operand:SI 4 "const_int_operand")]
707   ""
709   emit_insn (gen_atomic_bit_test_and_complement<mode>_1 (operands[1],
710                                                          operands[2],
711                                                          operands[3]));
712   rtx tem = gen_reg_rtx (QImode);
713   ix86_expand_setcc (tem, EQ, gen_rtx_REG (CCCmode, FLAGS_REG), const0_rtx);
714   rtx result = convert_modes (<MODE>mode, QImode, tem, 1);
715   if (operands[4] == const0_rtx)
716     result = expand_simple_binop (<MODE>mode, ASHIFT, result,
717                                   operands[2], operands[0], 0, OPTAB_DIRECT);
718   if (result != operands[0])
719     emit_move_insn (operands[0], result);
720   DONE;
723 (define_insn "atomic_bit_test_and_complement<mode>_1"
724   [(set (reg:CCC FLAGS_REG)
725         (compare:CCC
726           (unspec_volatile:SWI248
727             [(match_operand:SWI248 0 "memory_operand" "+m")
728              (match_operand:SI 2 "const_int_operand")]          ;; model
729             UNSPECV_XCHG)
730           (const_int 0)))
731    (set (zero_extract:SWI248 (match_dup 0)
732                              (const_int 1)
733                              (match_operand:SWI248 1 "nonmemory_operand" "rN"))
734         (not:SWI248 (zero_extract:SWI248 (match_dup 0)
735                                          (const_int 1)
736                                          (match_dup 1))))]
737   ""
738   "lock{%;} %K2btc{<imodesuffix>}\t{%1, %0|%0, %1}")
740 (define_expand "atomic_bit_test_and_reset<mode>"
741   [(match_operand:SWI248 0 "register_operand")
742    (match_operand:SWI248 1 "memory_operand")
743    (match_operand:SWI248 2 "nonmemory_operand")
744    (match_operand:SI 3 "const_int_operand") ;; model
745    (match_operand:SI 4 "const_int_operand")]
746   ""
748   emit_insn (gen_atomic_bit_test_and_reset<mode>_1 (operands[1], operands[2],
749                                                     operands[3]));
750   rtx tem = gen_reg_rtx (QImode);
751   ix86_expand_setcc (tem, EQ, gen_rtx_REG (CCCmode, FLAGS_REG), const0_rtx);
752   rtx result = convert_modes (<MODE>mode, QImode, tem, 1);
753   if (operands[4] == const0_rtx)
754     result = expand_simple_binop (<MODE>mode, ASHIFT, result,
755                                   operands[2], operands[0], 0, OPTAB_DIRECT);
756   if (result != operands[0])
757     emit_move_insn (operands[0], result);
758   DONE;
761 (define_insn "atomic_bit_test_and_reset<mode>_1"
762   [(set (reg:CCC FLAGS_REG)
763         (compare:CCC
764           (unspec_volatile:SWI248
765             [(match_operand:SWI248 0 "memory_operand" "+m")
766              (match_operand:SI 2 "const_int_operand")]          ;; model
767             UNSPECV_XCHG)
768           (const_int 0)))
769    (set (zero_extract:SWI248 (match_dup 0)
770                              (const_int 1)
771                              (match_operand:SWI248 1 "nonmemory_operand" "rN"))
772         (const_int 0))]
773   ""
774   "lock{%;} %K2btr{<imodesuffix>}\t{%1, %0|%0, %1}")