1 ;; GCC machine description for SPARC synchronization instructions.
2 ;; Copyright (C) 2005-2015 Free Software Foundation, Inc.
4 ;; This file is part of GCC.
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)
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_mode_iterator I12MODE [QI HI])
21 (define_mode_iterator I124MODE [QI HI SI])
22 (define_mode_iterator I24MODE [HI SI])
23 (define_mode_iterator I48MODE [SI (DI "TARGET_ARCH64 || TARGET_V8PLUS")])
24 (define_mode_attr modesuffix [(SI "") (DI "x")])
26 (define_expand "mem_thread_fence"
27 [(match_operand:SI 0 "const_int_operand")]
28 "TARGET_V8 || TARGET_V9"
30 enum memmodel model = (enum memmodel) INTVAL (operands[0]);
31 sparc_emit_membar_for_model (model, 3, 3);
35 (define_expand "membar"
37 (unspec:BLK [(match_dup 1) (match_operand:SI 0 "const_int_operand")]
39 "TARGET_V8 || TARGET_V9"
41 operands[1] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
42 MEM_VOLATILE_P (operands[1]) = 1;
45 ;; A compiler-only memory barrier. Generic code, when checking for the
46 ;; existence of various named patterns, uses asm("":::"memory") when we
47 ;; don't need an actual instruction. Here, it's easiest to pretend that
48 ;; membar 0 is such a barrier. Further, this gives us a nice hook to
49 ;; ignore all such barriers on Sparc V7.
50 (define_insn "*membar_empty"
51 [(set (match_operand:BLK 0 "" "")
52 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "zero_or_v7_operand")]
56 [(set_attr "type" "multi")
57 (set_attr "length" "0")])
59 ;; For V8, STBAR is exactly membar #StoreStore, by definition.
60 (define_insn "*membar_storestore"
61 [(set (match_operand:BLK 0 "" "")
62 (unspec:BLK [(match_dup 0) (const_int 8)] UNSPEC_MEMBAR))]
65 [(set_attr "type" "multi")])
67 ;; For LEON3, STB has the effect of membar #StoreLoad.
68 (define_insn "*membar_storeload_leon3"
69 [(set (match_operand:BLK 0 "" "")
70 (unspec:BLK [(match_dup 0) (const_int 2)] UNSPEC_MEMBAR))]
73 [(set_attr "type" "store")])
75 ;; For V8, LDSTUB has the effect of membar #StoreLoad.
76 (define_insn "*membar_storeload"
77 [(set (match_operand:BLK 0 "" "")
78 (unspec:BLK [(match_dup 0) (const_int 2)] UNSPEC_MEMBAR))]
79 "TARGET_V8 && !TARGET_LEON3"
80 "ldstub\t[%%sp-1], %%g0"
81 [(set_attr "type" "multi")])
83 ;; Put the two together, in combination with the fact that V8 implements PSO
84 ;; as its weakest memory model, means a full barrier. Match all remaining
85 ;; instances of the membar pattern for Sparc V8.
86 (define_insn "*membar_v8"
87 [(set (match_operand:BLK 0 "" "")
88 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
91 "stbar\n\tldstub\t[%%sp-1], %%g0"
92 [(set_attr "type" "multi")
93 (set_attr "length" "2")])
95 ;; For V9, we have the full membar instruction.
96 (define_insn "*membar"
97 [(set (match_operand:BLK 0 "" "")
98 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
102 [(set_attr "type" "multi")])
105 [(set (match_operand:BLK 0 "" "")
106 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
108 (set (match_operand:BLK 2 "" "")
109 (unspec:BLK [(match_dup 2) (match_operand:SI 3 "const_int_operand")]
112 [(set (match_operand:BLK 0 "" "")
113 (unspec:BLK [(match_dup 0) (match_dup 1)] UNSPEC_MEMBAR))]
114 { operands[1] = GEN_INT (UINTVAL (operands[1]) | UINTVAL (operands[3])); })
116 (define_expand "atomic_load<mode>"
117 [(match_operand:I 0 "register_operand" "")
118 (match_operand:I 1 "memory_operand" "")
119 (match_operand:SI 2 "const_int_operand" "")]
122 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
124 sparc_emit_membar_for_model (model, 1, 1);
126 if (TARGET_ARCH64 || <MODE>mode != DImode)
127 emit_move_insn (operands[0], operands[1]);
129 emit_insn (gen_atomic_loaddi_1 (operands[0], operands[1]));
131 sparc_emit_membar_for_model (model, 1, 2);
135 (define_insn "atomic_loaddi_1"
136 [(set (match_operand:DI 0 "register_operand" "=U,?*f")
137 (unspec:DI [(match_operand:DI 1 "memory_operand" "m,m")]
141 [(set_attr "type" "load,fpload")])
143 (define_expand "atomic_store<mode>"
144 [(match_operand:I 0 "memory_operand" "")
145 (match_operand:I 1 "register_operand" "")
146 (match_operand:SI 2 "const_int_operand" "")]
149 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
151 sparc_emit_membar_for_model (model, 2, 1);
153 if (TARGET_ARCH64 || <MODE>mode != DImode)
154 emit_move_insn (operands[0], operands[1]);
156 emit_insn (gen_atomic_storedi_1 (operands[0], operands[1]));
158 sparc_emit_membar_for_model (model, 2, 2);
162 (define_insn "atomic_storedi_1"
163 [(set (match_operand:DI 0 "memory_operand" "=m,m,m")
165 [(match_operand:DI 1 "register_or_v9_zero_operand" "J,U,?*f")]
172 [(set_attr "type" "store,store,fpstore")
173 (set_attr "cpu_feature" "v9,*,*")])
175 (define_expand "atomic_compare_and_swap<mode>"
176 [(match_operand:SI 0 "register_operand" "") ;; bool output
177 (match_operand:I 1 "register_operand" "") ;; val output
178 (match_operand:I 2 "mem_noofs_operand" "") ;; memory
179 (match_operand:I 3 "register_operand" "") ;; expected
180 (match_operand:I 4 "register_operand" "") ;; desired
181 (match_operand:SI 5 "const_int_operand" "") ;; is_weak
182 (match_operand:SI 6 "const_int_operand" "") ;; mod_s
183 (match_operand:SI 7 "const_int_operand" "")] ;; mod_f
184 "(TARGET_V9 || TARGET_LEON3)
185 && (<MODE>mode != DImode || TARGET_ARCH64 || TARGET_V8PLUS)"
187 sparc_expand_compare_and_swap (operands);
191 (define_expand "atomic_compare_and_swap<mode>_1"
193 [(set (match_operand:I48MODE 0 "register_operand" "")
194 (match_operand:I48MODE 1 "mem_noofs_operand" ""))
196 (unspec_volatile:I48MODE
197 [(match_operand:I48MODE 2 "register_operand" "")
198 (match_operand:I48MODE 3 "register_operand" "")]
200 "TARGET_V9 || TARGET_LEON3"
203 (define_insn "*atomic_compare_and_swap<mode>_1"
204 [(set (match_operand:I48MODE 0 "register_operand" "=r")
205 (match_operand:I48MODE 1 "mem_noofs_operand" "+w"))
207 (unspec_volatile:I48MODE
208 [(match_operand:I48MODE 2 "register_operand" "r")
209 (match_operand:I48MODE 3 "register_operand" "0")]
211 "TARGET_V9 && (<MODE>mode != DImode || TARGET_ARCH64)"
212 "cas<modesuffix>\t%1, %2, %0"
213 [(set_attr "type" "multi")])
215 (define_insn "*atomic_compare_and_swap_leon3_1"
216 [(set (match_operand:SI 0 "register_operand" "=r")
217 (match_operand:SI 1 "mem_noofs_operand" "+w"))
220 [(match_operand:SI 2 "register_operand" "r")
221 (match_operand:SI 3 "register_operand" "0")]
225 if (TARGET_USER_MODE)
226 return "casa\t%1 0xa, %2, %0"; /* ASI for user data space. */
228 return "casa\t%1 0xb, %2, %0"; /* ASI for supervisor data space. */
230 [(set_attr "type" "multi")])
232 (define_insn "*atomic_compare_and_swapdi_v8plus"
233 [(set (match_operand:DI 0 "register_operand" "=h")
234 (match_operand:DI 1 "mem_noofs_operand" "+w"))
237 [(match_operand:DI 2 "register_operand" "h")
238 (match_operand:DI 3 "register_operand" "0")]
242 if (sparc_check_64 (operands[3], insn) <= 0)
243 output_asm_insn ("srl\t%L3, 0, %L3", operands);
244 output_asm_insn ("sllx\t%H3, 32, %H3", operands);
245 output_asm_insn ("or\t%L3, %H3, %L3", operands);
246 if (sparc_check_64 (operands[2], insn) <= 0)
247 output_asm_insn ("srl\t%L2, 0, %L2", operands);
248 output_asm_insn ("sllx\t%H2, 32, %H3", operands);
249 output_asm_insn ("or\t%L2, %H3, %H3", operands);
250 output_asm_insn ("casx\t%1, %H3, %L3", operands);
251 return "srlx\t%L3, 32, %H3";
253 [(set_attr "type" "multi")
254 (set_attr "length" "8")])
256 (define_expand "atomic_exchangesi"
257 [(match_operand:SI 0 "register_operand" "")
258 (match_operand:SI 1 "memory_operand" "")
259 (match_operand:SI 2 "register_operand" "")
260 (match_operand:SI 3 "const_int_operand" "")]
261 "(TARGET_V8 || TARGET_V9) && !sparc_fix_ut699"
263 enum memmodel model = (enum memmodel) INTVAL (operands[3]);
265 sparc_emit_membar_for_model (model, 3, 1);
266 emit_insn (gen_swapsi (operands[0], operands[1], operands[2]));
267 sparc_emit_membar_for_model (model, 3, 2);
271 (define_insn "swapsi"
272 [(set (match_operand:SI 0 "register_operand" "=r")
273 (unspec_volatile:SI [(match_operand:SI 1 "memory_operand" "+m")]
276 (match_operand:SI 2 "register_operand" "0"))]
277 "(TARGET_V8 || TARGET_V9) && !sparc_fix_ut699"
279 [(set_attr "type" "multi")])
281 (define_expand "atomic_test_and_set"
282 [(match_operand:QI 0 "register_operand" "")
283 (match_operand:QI 1 "memory_operand" "")
284 (match_operand:SI 2 "const_int_operand" "")]
287 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
290 sparc_emit_membar_for_model (model, 3, 1);
291 emit_insn (gen_ldstub (operands[0], operands[1]));
292 sparc_emit_membar_for_model (model, 3, 2);
294 /* Convert the 0/0xff result we would otherwise have to a boolean.
295 I.e. ignore all but bit 0. */
296 ret = expand_simple_binop (QImode, AND, operands[0], const1_rtx,
297 operands[0], true, OPTAB_LIB_WIDEN);
298 if (ret != operands[0])
299 emit_move_insn (operands[0], ret);
304 (define_insn "ldstub"
305 [(set (match_operand:QI 0 "register_operand" "=r")
306 (unspec_volatile:QI [(match_operand:QI 1 "memory_operand" "+m")]
308 (set (match_dup 1) (const_int -1))]
311 [(set_attr "type" "multi")])