OSDN Git Service

2009-04-07 Paolo Bonzini <bonzini@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / sync.md
1 ;; GCC machine description for i386 synchronization instructions.
2 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009
3 ;; Free Software Foundation, Inc.
4 ;;
5 ;; This file is part of GCC.
6 ;;
7 ;; GCC is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3, or (at your option)
10 ;; any later version.
11 ;;
12 ;; GCC is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GCC; see the file COPYING3.  If not see
19 ;; <http://www.gnu.org/licenses/>.
20
21 (define_mode_iterator IMODE [QI HI SI (DI "TARGET_64BIT")])
22 (define_mode_attr modesuffix [(QI "b") (HI "w") (SI "l") (DI "q")])
23 (define_mode_attr modeconstraint [(QI "q") (HI "r") (SI "r") (DI "r")])
24 (define_mode_attr immconstraint [(QI "i") (HI "i") (SI "i") (DI "e")])
25
26 (define_mode_iterator CASMODE [QI HI SI (DI "TARGET_64BIT || TARGET_CMPXCHG8B")
27                            (TI "TARGET_64BIT && TARGET_CMPXCHG16B")])
28 (define_mode_iterator DCASMODE
29   [(DI "!TARGET_64BIT && TARGET_CMPXCHG8B && !flag_pic")
30    (TI "TARGET_64BIT && TARGET_CMPXCHG16B")])
31 (define_mode_attr doublemodesuffix [(DI "8") (TI "16")])
32 (define_mode_attr DCASHMODE [(DI "SI") (TI "DI")])
33
34 (define_expand "memory_barrier"
35   [(set (match_dup 0)
36         (unspec:BLK [(match_dup 0)] UNSPEC_MFENCE))]
37   ""
38 {
39   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
40   MEM_VOLATILE_P (operands[0]) = 1;
41
42   if (!(TARGET_64BIT || TARGET_SSE2))
43     {
44       emit_insn (gen_memory_barrier_nosse (operands[0]));
45       DONE;
46     }
47 })
48
49 (define_insn "memory_barrier_nosse"
50   [(set (match_operand:BLK 0 "" "")
51         (unspec:BLK [(match_dup 0)] UNSPEC_MFENCE))
52    (clobber (reg:CC FLAGS_REG))]
53   "!(TARGET_64BIT || TARGET_SSE2)"
54   "lock{%;| }or{l}\t{$0, (%%esp)|DWORD PTR [esp], 0}"
55   [(set_attr "memory" "unknown")])
56
57 ;; ??? It would be possible to use cmpxchg8b on pentium for DImode
58 ;; changes.  It's complicated because the insn uses ecx:ebx as the
59 ;; new value; note that the registers are reversed from the order
60 ;; that they'd be in with (reg:DI 2 ecx).  Similarly for TImode
61 ;; data in 64-bit mode.
62
63 (define_expand "sync_compare_and_swap<mode>"
64   [(parallel
65     [(set (match_operand:CASMODE 0 "register_operand" "")
66           (match_operand:CASMODE 1 "memory_operand" ""))
67      (set (match_dup 1)
68           (unspec_volatile:CASMODE
69             [(match_dup 1)
70              (match_operand:CASMODE 2 "register_operand" "")
71              (match_operand:CASMODE 3 "register_operand" "")]
72             UNSPECV_CMPXCHG))
73      (clobber (reg:CC FLAGS_REG))])]
74   "TARGET_CMPXCHG"
75 {
76   if ((<MODE>mode == DImode && !TARGET_64BIT) || <MODE>mode == TImode)
77     {
78       enum machine_mode hmode = <MODE>mode == DImode ? SImode : DImode;
79       rtx low = simplify_gen_subreg (hmode, operands[3], <MODE>mode, 0);
80       rtx high = simplify_gen_subreg (hmode, operands[3], <MODE>mode,
81                                       GET_MODE_SIZE (hmode));
82       low = force_reg (hmode, low);
83       high = force_reg (hmode, high);
84       if (<MODE>mode == DImode)
85         {
86           if (flag_pic && !cmpxchg8b_pic_memory_operand (operands[1], DImode))
87             operands[1] = replace_equiv_address (operands[1],
88                                                  force_reg (Pmode,
89                                                             XEXP (operands[1],
90                                                                   0)));
91           emit_insn (gen_sync_double_compare_and_swapdi
92                      (operands[0], operands[1], operands[2], low, high));
93         }
94       else if (<MODE>mode == TImode)
95         emit_insn (gen_sync_double_compare_and_swapti
96                    (operands[0], operands[1], operands[2], low, high));
97       else
98         gcc_unreachable ();
99       DONE;
100     }
101 })
102
103 (define_insn "*sync_compare_and_swap<mode>"
104   [(set (match_operand:IMODE 0 "register_operand" "=a")
105         (match_operand:IMODE 1 "memory_operand" "+m"))
106    (set (match_dup 1)
107         (unspec_volatile:IMODE
108           [(match_dup 1)
109            (match_operand:IMODE 2 "register_operand" "a")
110            (match_operand:IMODE 3 "register_operand" "<modeconstraint>")]
111           UNSPECV_CMPXCHG))
112    (clobber (reg:CC FLAGS_REG))]
113   "TARGET_CMPXCHG"
114   "lock{%;| }cmpxchg{<modesuffix>}\t{%3, %1|%1, %3}")
115
116 (define_insn "sync_double_compare_and_swap<mode>"
117   [(set (match_operand:DCASMODE 0 "register_operand" "=A")
118         (match_operand:DCASMODE 1 "memory_operand" "+m"))
119    (set (match_dup 1)
120         (unspec_volatile:DCASMODE
121           [(match_dup 1)
122            (match_operand:DCASMODE 2 "register_operand" "A")
123            (match_operand:<DCASHMODE> 3 "register_operand" "b")
124            (match_operand:<DCASHMODE> 4 "register_operand" "c")]
125           UNSPECV_CMPXCHG))
126    (clobber (reg:CC FLAGS_REG))]
127   ""
128   "lock{%;| }cmpxchg<doublemodesuffix>b\t%1")
129
130 ;; Theoretically we'd like to use constraint "r" (any reg) for operand
131 ;; 3, but that includes ecx.  If operand 3 and 4 are the same (like when
132 ;; the input is -1LL) GCC might chose to allocate operand 3 to ecx, like
133 ;; operand 4.  This breaks, as the xchg will move the PIC register contents
134 ;; to %ecx then --> boom.  Operands 3 and 4 really need to be different
135 ;; registers, which in this case means operand 3 must not be ecx.
136 ;; Instead of playing tricks with fake early clobbers or the like we
137 ;; just enumerate all regs possible here, which (as this is !TARGET_64BIT)
138 ;; are just esi and edi.
139 (define_insn "*sync_double_compare_and_swapdi_pic"
140   [(set (match_operand:DI 0 "register_operand" "=A")
141         (match_operand:DI 1 "cmpxchg8b_pic_memory_operand" "+m"))
142    (set (match_dup 1)
143         (unspec_volatile:DI
144           [(match_dup 1)
145            (match_operand:DI 2 "register_operand" "A")
146            (match_operand:SI 3 "register_operand" "SD")
147            (match_operand:SI 4 "register_operand" "c")]
148           UNSPECV_CMPXCHG))
149    (clobber (reg:CC FLAGS_REG))]
150   "!TARGET_64BIT && TARGET_CMPXCHG8B && flag_pic"
151   "xchg{l}\t%%ebx, %3\;lock{%;| }cmpxchg8b\t%1\;xchg{l}\t%%ebx, %3")
152
153 (define_expand "sync_compare_and_swap_cc<mode>"
154   [(parallel
155     [(set (match_operand:CASMODE 0 "register_operand" "")
156           (match_operand:CASMODE 1 "memory_operand" ""))
157      (set (match_dup 1)
158           (unspec_volatile:CASMODE
159             [(match_dup 1)
160              (match_operand:CASMODE 2 "register_operand" "")
161              (match_operand:CASMODE 3 "register_operand" "")]
162             UNSPECV_CMPXCHG))
163      (set (match_dup 4)
164           (compare:CCZ
165             (unspec_volatile:CASMODE
166               [(match_dup 1) (match_dup 2) (match_dup 3)] UNSPECV_CMPXCHG)
167             (match_dup 2)))])]
168   "TARGET_CMPXCHG"
169 {
170   operands[4] = gen_rtx_REG (CCZmode, FLAGS_REG);
171   ix86_compare_op0 = operands[4];
172   ix86_compare_op1 = const0_rtx;
173   if ((<MODE>mode == DImode && !TARGET_64BIT) || <MODE>mode == TImode)
174     {
175       enum machine_mode hmode = <MODE>mode == DImode ? SImode : DImode;
176       rtx low = simplify_gen_subreg (hmode, operands[3], <MODE>mode, 0);
177       rtx high = simplify_gen_subreg (hmode, operands[3], <MODE>mode,
178                                       GET_MODE_SIZE (hmode));
179       low = force_reg (hmode, low);
180       high = force_reg (hmode, high);
181       if (<MODE>mode == DImode)
182         {
183           if (flag_pic && !cmpxchg8b_pic_memory_operand (operands[1], DImode))
184             operands[1] = replace_equiv_address (operands[1],
185                                                  force_reg (Pmode,
186                                                             XEXP (operands[1],
187                                                                   0)));
188           emit_insn (gen_sync_double_compare_and_swap_ccdi
189                      (operands[0], operands[1], operands[2], low, high));
190         }
191       else if (<MODE>mode == TImode)
192         emit_insn (gen_sync_double_compare_and_swap_ccti
193                    (operands[0], operands[1], operands[2], low, high));
194       else
195         gcc_unreachable ();
196       DONE;
197     }
198 })
199
200 (define_insn "*sync_compare_and_swap_cc<mode>"
201   [(set (match_operand:IMODE 0 "register_operand" "=a")
202         (match_operand:IMODE 1 "memory_operand" "+m"))
203    (set (match_dup 1)
204         (unspec_volatile:IMODE
205           [(match_dup 1)
206            (match_operand:IMODE 2 "register_operand" "a")
207            (match_operand:IMODE 3 "register_operand" "<modeconstraint>")]
208           UNSPECV_CMPXCHG))
209    (set (reg:CCZ FLAGS_REG)
210         (compare:CCZ
211           (unspec_volatile:IMODE
212             [(match_dup 1) (match_dup 2) (match_dup 3)] UNSPECV_CMPXCHG)
213           (match_dup 2)))]
214   "TARGET_CMPXCHG"
215   "lock{%;| }cmpxchg{<modesuffix>}\t{%3, %1|%1, %3}")
216
217 (define_insn "sync_double_compare_and_swap_cc<mode>"
218   [(set (match_operand:DCASMODE 0 "register_operand" "=A")
219         (match_operand:DCASMODE 1 "memory_operand" "+m"))
220    (set (match_dup 1)
221         (unspec_volatile:DCASMODE
222           [(match_dup 1)
223            (match_operand:DCASMODE 2 "register_operand" "A")
224            (match_operand:<DCASHMODE> 3 "register_operand" "b")
225            (match_operand:<DCASHMODE> 4 "register_operand" "c")]
226           UNSPECV_CMPXCHG))
227    (set (reg:CCZ FLAGS_REG)
228         (compare:CCZ
229           (unspec_volatile:DCASMODE
230             [(match_dup 1) (match_dup 2) (match_dup 3) (match_dup 4)]
231             UNSPECV_CMPXCHG)
232           (match_dup 2)))]
233   ""
234   "lock{%;| }cmpxchg<doublemodesuffix>b\t%1")
235
236 ;; See above for the explanation of using the constraint "SD" for
237 ;; operand 3.
238 (define_insn "*sync_double_compare_and_swap_ccdi_pic"
239   [(set (match_operand:DI 0 "register_operand" "=A")
240         (match_operand:DI 1 "cmpxchg8b_pic_memory_operand" "+m"))
241    (set (match_dup 1)
242         (unspec_volatile:DI
243           [(match_dup 1)
244            (match_operand:DI 2 "register_operand" "A")
245            (match_operand:SI 3 "register_operand" "SD")
246            (match_operand:SI 4 "register_operand" "c")]
247           UNSPECV_CMPXCHG))
248    (set (reg:CCZ FLAGS_REG)
249         (compare:CCZ
250           (unspec_volatile:DI
251             [(match_dup 1) (match_dup 2) (match_dup 3) (match_dup 4)]
252             UNSPECV_CMPXCHG)
253           (match_dup 2)))]
254   "!TARGET_64BIT && TARGET_CMPXCHG8B && flag_pic"
255   "xchg{l}\t%%ebx, %3\;lock{%;| }cmpxchg8b\t%1\;xchg{l}\t%%ebx, %3")
256
257 (define_insn "sync_old_add<mode>"
258   [(set (match_operand:IMODE 0 "register_operand" "=<modeconstraint>")
259         (unspec_volatile:IMODE
260           [(match_operand:IMODE 1 "memory_operand" "+m")] UNSPECV_XCHG))
261    (set (match_dup 1)
262         (plus:IMODE (match_dup 1)
263                     (match_operand:IMODE 2 "register_operand" "0")))
264    (clobber (reg:CC FLAGS_REG))]
265   "TARGET_XADD"
266   "lock{%;| }xadd{<modesuffix>}\t{%0, %1|%1, %0}")
267
268 ;; Recall that xchg implicitly sets LOCK#, so adding it again wastes space.
269 (define_insn "sync_lock_test_and_set<mode>"
270   [(set (match_operand:IMODE 0 "register_operand" "=<modeconstraint>")
271         (unspec_volatile:IMODE
272           [(match_operand:IMODE 1 "memory_operand" "+m")] UNSPECV_XCHG))
273    (set (match_dup 1)
274         (match_operand:IMODE 2 "register_operand" "0"))]
275   ""
276   "xchg{<modesuffix>}\t{%1, %0|%0, %1}")
277
278 (define_insn "sync_add<mode>"
279   [(set (match_operand:IMODE 0 "memory_operand" "+m")
280         (unspec_volatile:IMODE
281           [(plus:IMODE (match_dup 0)
282              (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
283           UNSPECV_LOCK))
284    (clobber (reg:CC FLAGS_REG))]
285   ""
286 {
287   if (TARGET_USE_INCDEC)
288     {
289       if (operands[1] == const1_rtx)
290         return "lock{%;| }inc{<modesuffix>}\t%0";
291       if (operands[1] == constm1_rtx)
292         return "lock{%;| }dec{<modesuffix>}\t%0";
293     }
294
295   return "lock{%;| }add{<modesuffix>}\t{%1, %0|%0, %1}";
296 })
297
298 (define_insn "sync_sub<mode>"
299   [(set (match_operand:IMODE 0 "memory_operand" "+m")
300         (unspec_volatile:IMODE
301           [(minus:IMODE (match_dup 0)
302              (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
303           UNSPECV_LOCK))
304    (clobber (reg:CC FLAGS_REG))]
305   ""
306 {
307   if (TARGET_USE_INCDEC)
308     {
309       if (operands[1] == const1_rtx)
310         return "lock{%;| }dec{<modesuffix>}\t%0";
311       if (operands[1] == constm1_rtx)
312         return "lock{%;| }inc{<modesuffix>}\t%0";
313     }
314
315   return "lock{%;| }sub{<modesuffix>}\t{%1, %0|%0, %1}";
316 })
317
318 (define_insn "sync_ior<mode>"
319   [(set (match_operand:IMODE 0 "memory_operand" "+m")
320         (unspec_volatile:IMODE
321           [(ior:IMODE (match_dup 0)
322              (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
323           UNSPECV_LOCK))
324    (clobber (reg:CC FLAGS_REG))]
325   ""
326   "lock{%;| }or{<modesuffix>}\t{%1, %0|%0, %1}")
327
328 (define_insn "sync_and<mode>"
329   [(set (match_operand:IMODE 0 "memory_operand" "+m")
330         (unspec_volatile:IMODE
331           [(and:IMODE (match_dup 0)
332              (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
333           UNSPECV_LOCK))
334    (clobber (reg:CC FLAGS_REG))]
335   ""
336   "lock{%;| }and{<modesuffix>}\t{%1, %0|%0, %1}")
337
338 (define_insn "sync_xor<mode>"
339   [(set (match_operand:IMODE 0 "memory_operand" "+m")
340         (unspec_volatile:IMODE
341           [(xor:IMODE (match_dup 0)
342              (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
343           UNSPECV_LOCK))
344    (clobber (reg:CC FLAGS_REG))]
345   ""
346   "lock{%;| }xor{<modesuffix>}\t{%1, %0|%0, %1}")