1 ;; GCC machine description for i386 synchronization instructions.
2 ;; Copyright (C) 2005, 2006
3 ;; Free Software Foundation, Inc.
5 ;; This file is part of GCC.
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 2, or (at your option)
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.
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
19 ;; the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 (define_mode_macro IMODE [QI HI SI (DI "TARGET_64BIT")])
23 (define_mode_attr modesuffix [(QI "b") (HI "w") (SI "l") (DI "q")])
24 (define_mode_attr modeconstraint [(QI "q") (HI "r") (SI "r") (DI "r")])
25 (define_mode_attr immconstraint [(QI "i") (HI "i") (SI "i") (DI "e")])
27 (define_mode_macro CASMODE [QI HI SI (DI "TARGET_64BIT || TARGET_CMPXCHG8B")
28 (TI "TARGET_64BIT && TARGET_CMPXCHG16B")])
29 (define_mode_macro DCASMODE
30 [(DI "!TARGET_64BIT && TARGET_CMPXCHG8B && !flag_pic")
31 (TI "TARGET_64BIT && TARGET_CMPXCHG16B")])
32 (define_mode_attr doublemodesuffix [(DI "8") (TI "16")])
33 (define_mode_attr DCASHMODE [(DI "SI") (TI "DI")])
35 ;; ??? It would be possible to use cmpxchg8b on pentium for DImode
36 ;; changes. It's complicated because the insn uses ecx:ebx as the
37 ;; new value; note that the registers are reversed from the order
38 ;; that they'd be in with (reg:DI 2 ecx). Similarly for TImode
39 ;; data in 64-bit mode.
41 (define_expand "sync_compare_and_swap<mode>"
43 [(set (match_operand:CASMODE 0 "register_operand" "")
44 (match_operand:CASMODE 1 "memory_operand" ""))
46 (unspec_volatile:CASMODE
48 (match_operand:CASMODE 2 "register_operand" "")
49 (match_operand:CASMODE 3 "register_operand" "")]
51 (clobber (reg:CC FLAGS_REG))])]
54 if ((<MODE>mode == DImode && !TARGET_64BIT) || <MODE>mode == TImode)
56 enum machine_mode hmode = <MODE>mode == DImode ? SImode : DImode;
57 rtx low = simplify_gen_subreg (hmode, operands[3], <MODE>mode, 0);
58 rtx high = simplify_gen_subreg (hmode, operands[3], <MODE>mode,
59 GET_MODE_SIZE (hmode));
60 low = force_reg (hmode, low);
61 high = force_reg (hmode, high);
62 if (<MODE>mode == DImode)
63 emit_insn (gen_sync_double_compare_and_swapdi
64 (operands[0], operands[1], operands[2], low, high));
65 else if (<MODE>mode == TImode)
66 emit_insn (gen_sync_double_compare_and_swapti
67 (operands[0], operands[1], operands[2], low, high));
74 (define_insn "*sync_compare_and_swap<mode>"
75 [(set (match_operand:IMODE 0 "register_operand" "=a")
76 (match_operand:IMODE 1 "memory_operand" "+m"))
78 (unspec_volatile:IMODE
80 (match_operand:IMODE 2 "register_operand" "a")
81 (match_operand:IMODE 3 "register_operand" "<modeconstraint>")]
83 (clobber (reg:CC FLAGS_REG))]
85 "lock{\;| }cmpxchg{<modesuffix>\t%3, %1| %1, %3}")
87 (define_insn "sync_double_compare_and_swap<mode>"
88 [(set (match_operand:DCASMODE 0 "register_operand" "=A")
89 (match_operand:DCASMODE 1 "memory_operand" "+m"))
91 (unspec_volatile:DCASMODE
93 (match_operand:DCASMODE 2 "register_operand" "A")
94 (match_operand:<DCASHMODE> 3 "register_operand" "b")
95 (match_operand:<DCASHMODE> 4 "register_operand" "c")]
97 (clobber (reg:CC FLAGS_REG))]
99 "lock{\;| }cmpxchg<doublemodesuffix>b{\t| }%1")
101 (define_insn "*sync_double_compare_and_swapdi_pic"
102 [(set (match_operand:DI 0 "register_operand" "=A")
103 (match_operand:DI 1 "memory_operand" "+m"))
107 (match_operand:DI 2 "register_operand" "A")
108 (match_operand:SI 3 "register_operand" "r")
109 (match_operand:SI 4 "register_operand" "c")]
111 (clobber (reg:CC FLAGS_REG))]
112 "!TARGET_64BIT && TARGET_CMPXCHG8B && flag_pic"
113 "xchg{l}\t%%ebx, %3\;lock{\;| }cmpxchg8b{\t| }%1\;xchg{l}\t%%ebx, %3")
115 (define_expand "sync_compare_and_swap_cc<mode>"
117 [(set (match_operand:CASMODE 0 "register_operand" "")
118 (match_operand:CASMODE 1 "memory_operand" ""))
120 (unspec_volatile:CASMODE
122 (match_operand:CASMODE 2 "register_operand" "")
123 (match_operand:CASMODE 3 "register_operand" "")]
127 (unspec_volatile:CASMODE
128 [(match_dup 1) (match_dup 2) (match_dup 3)] UNSPECV_CMPXCHG_2)
132 operands[4] = gen_rtx_REG (CCZmode, FLAGS_REG);
133 ix86_compare_op0 = operands[3];
134 ix86_compare_op1 = NULL;
135 ix86_compare_emitted = operands[4];
136 if ((<MODE>mode == DImode && !TARGET_64BIT) || <MODE>mode == TImode)
138 enum machine_mode hmode = <MODE>mode == DImode ? SImode : DImode;
139 rtx low = simplify_gen_subreg (hmode, operands[3], <MODE>mode, 0);
140 rtx high = simplify_gen_subreg (hmode, operands[3], <MODE>mode,
141 GET_MODE_SIZE (hmode));
142 low = force_reg (hmode, low);
143 high = force_reg (hmode, high);
144 if (<MODE>mode == DImode)
145 emit_insn (gen_sync_double_compare_and_swap_ccdi
146 (operands[0], operands[1], operands[2], low, high));
147 else if (<MODE>mode == TImode)
148 emit_insn (gen_sync_double_compare_and_swap_ccti
149 (operands[0], operands[1], operands[2], low, high));
156 (define_insn "*sync_compare_and_swap_cc<mode>"
157 [(set (match_operand:IMODE 0 "register_operand" "=a")
158 (match_operand:IMODE 1 "memory_operand" "+m"))
160 (unspec_volatile:IMODE
162 (match_operand:IMODE 2 "register_operand" "a")
163 (match_operand:IMODE 3 "register_operand" "<modeconstraint>")]
165 (set (reg:CCZ FLAGS_REG)
167 (unspec_volatile:IMODE
168 [(match_dup 1) (match_dup 2) (match_dup 3)] UNSPECV_CMPXCHG_2)
171 "lock{\;| }cmpxchg{<modesuffix>\t%3, %1| %1, %3}")
173 (define_insn "sync_double_compare_and_swap_cc<mode>"
174 [(set (match_operand:DCASMODE 0 "register_operand" "=A")
175 (match_operand:DCASMODE 1 "memory_operand" "+m"))
177 (unspec_volatile:DCASMODE
179 (match_operand:DCASMODE 2 "register_operand" "A")
180 (match_operand:<DCASHMODE> 3 "register_operand" "b")
181 (match_operand:<DCASHMODE> 4 "register_operand" "c")]
183 (set (reg:CCZ FLAGS_REG)
185 (unspec_volatile:DCASMODE
186 [(match_dup 1) (match_dup 2) (match_dup 3) (match_dup 4)]
190 "lock{\;| }cmpxchg<doublemodesuffix>b{\t| }%1")
192 (define_insn "*sync_double_compare_and_swap_ccdi_pic"
193 [(set (match_operand:DI 0 "register_operand" "=A")
194 (match_operand:DI 1 "memory_operand" "+m"))
198 (match_operand:DI 2 "register_operand" "A")
199 (match_operand:SI 3 "register_operand" "r")
200 (match_operand:SI 4 "register_operand" "c")]
202 (set (reg:CCZ FLAGS_REG)
205 [(match_dup 1) (match_dup 2) (match_dup 3) (match_dup 4)]
208 "!TARGET_64BIT && TARGET_CMPXCHG8B && flag_pic"
209 "xchg{l}\t%%ebx, %3\;lock{\;| }cmpxchg8b{\t| }%1\;xchg{l}\t%%ebx, %3")
211 (define_insn "sync_old_add<mode>"
212 [(set (match_operand:IMODE 0 "register_operand" "=<modeconstraint>")
213 (unspec_volatile:IMODE
214 [(match_operand:IMODE 1 "memory_operand" "+m")] UNSPECV_XCHG))
216 (plus:IMODE (match_dup 1)
217 (match_operand:IMODE 2 "register_operand" "0")))
218 (clobber (reg:CC FLAGS_REG))]
220 "lock{\;| }xadd{<modesuffix>\t%0, %1| %1, %0}")
222 ;; Recall that xchg implicitly sets LOCK#, so adding it again wastes space.
223 (define_insn "sync_lock_test_and_set<mode>"
224 [(set (match_operand:IMODE 0 "register_operand" "=<modeconstraint>")
225 (unspec_volatile:IMODE
226 [(match_operand:IMODE 1 "memory_operand" "+m")] UNSPECV_XCHG))
228 (match_operand:IMODE 2 "register_operand" "0"))]
230 "xchg{<modesuffix>}\t{%1, %0|%0, %1}")
232 (define_insn "sync_add<mode>"
233 [(set (match_operand:IMODE 0 "memory_operand" "+m")
234 (unspec_volatile:IMODE
235 [(plus:IMODE (match_dup 0)
236 (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
238 (clobber (reg:CC FLAGS_REG))]
241 if (TARGET_USE_INCDEC)
243 if (operands[1] == const1_rtx)
244 return "lock{\;| }inc{<modesuffix>\t| }%0";
245 if (operands[1] == constm1_rtx)
246 return "lock{\;| }dec{<modesuffix>\t| }%0";
249 return "lock{\;| }add{<modesuffix>\t%1, %0| %0, %1}";
252 (define_insn "sync_sub<mode>"
253 [(set (match_operand:IMODE 0 "memory_operand" "+m")
254 (unspec_volatile:IMODE
255 [(minus:IMODE (match_dup 0)
256 (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
258 (clobber (reg:CC FLAGS_REG))]
261 if (TARGET_USE_INCDEC)
263 if (operands[1] == const1_rtx)
264 return "lock{\;| }dec{<modesuffix>\t| }%0";
265 if (operands[1] == constm1_rtx)
266 return "lock{\;| }inc{<modesuffix>\t| }%0";
269 return "lock{\;| }sub{<modesuffix>\t%1, %0| %0, %1}";
272 (define_insn "sync_ior<mode>"
273 [(set (match_operand:IMODE 0 "memory_operand" "+m")
274 (unspec_volatile:IMODE
275 [(ior:IMODE (match_dup 0)
276 (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
278 (clobber (reg:CC FLAGS_REG))]
280 "lock{\;| }or{<modesuffix>\t%1, %0| %0, %1}")
282 (define_insn "sync_and<mode>"
283 [(set (match_operand:IMODE 0 "memory_operand" "+m")
284 (unspec_volatile:IMODE
285 [(and:IMODE (match_dup 0)
286 (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
288 (clobber (reg:CC FLAGS_REG))]
290 "lock{\;| }and{<modesuffix>\t%1, %0| %0, %1}")
292 (define_insn "sync_xor<mode>"
293 [(set (match_operand:IMODE 0 "memory_operand" "+m")
294 (unspec_volatile:IMODE
295 [(xor:IMODE (match_dup 0)
296 (match_operand:IMODE 1 "nonmemory_operand" "<modeconstraint><immconstraint>"))]
298 (clobber (reg:CC FLAGS_REG))]
300 "lock{\;| }xor{<modesuffix>\t%1, %0| %0, %1}")