Debian Codegraph @

gcc/config/sparc/sync.md

raw
1 ;; GCC machine description for SPARC synchronization instructions.
2 ;; Copyright (C) 2005-2025 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.
10 ;;
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.
15 ;;
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/>.
19
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")])
25
26 (define_expand "mem_thread_fence"
27 [(match_operand:SI 0 "const_int_operand")]
28 "TARGET_V8 || TARGET_V9"
29 {
30 enum memmodel model = (enum memmodel) INTVAL (operands[0]);
31 sparc_emit_membar_for_model (model, 3, 3);
32 DONE;
33 })
34
35 (define_expand "membar"
36 [(set (match_dup 1)
37 (unspec:BLK [(match_dup 1) (match_operand:SI 0 "const_int_operand")]
38 UNSPEC_MEMBAR))]
39 "TARGET_V8 || TARGET_V9"
40 {
41 operands[1] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
42 MEM_VOLATILE_P (operands[1]) = 1;
43 })
44
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")]
53 UNSPEC_MEMBAR))]
54 ""
55 ""
56 [(set_attr "type" "multi")
57 (set_attr "length" "0")])
58
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))]
63 "TARGET_V8"
64 "stbar"
65 [(set_attr "type" "multi")])
66
67 ;; For V8, LDSTUB has the effect of membar #StoreLoad.
68 (define_insn "membar_storeload"
69 [(set (match_operand:BLK 0 "" "")
70 (unspec:BLK [(match_dup 0) (const_int 2)] UNSPEC_MEMBAR))]
71 "TARGET_V8"
72 {
73 if (sparc_fix_gr712rc)
74 return ".align\t16\n\tldstub\t[%%sp-1], %%g0";
75 else
76 return "ldstub\t[%%sp-1], %%g0";
77 }
78 [(set_attr "type" "multi")
79 (set (attr "length") (if_then_else (eq_attr "fix_gr712rc" "true")
80 (const_int 4) (const_int 1)))])
81
82 ;; Put the two together, in combination with the fact that V8 implements PSO
83 ;; as its weakest memory model, means a full barrier. Match all remaining
84 ;; instances of the membar pattern for Sparc V8.
85 (define_insn "*membar_v8"
86 [(set (match_operand:BLK 0 "" "")
87 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
88 UNSPEC_MEMBAR))]
89 "TARGET_V8"
90 {
91 if (sparc_fix_gr712rc)
92 return "stbar\n.align\t16\n\tldstub\t[%%sp-1], %%g0";
93 else
94 return "stbar\n\tldstub\t[%%sp-1], %%g0";
95 }
96 [(set_attr "type" "multi")
97 (set (attr "length") (if_then_else (eq_attr "fix_gr712rc" "true")
98 (const_int 5) (const_int 2)))])
99
100 ;; For V9, we have the full membar instruction.
101 (define_insn "*membar"
102 [(set (match_operand:BLK 0 "" "")
103 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
104 UNSPEC_MEMBAR))]
105 "TARGET_V9"
106 "membar\t%1"
107 [(set_attr "type" "multi")])
108
109 (define_peephole2
110 [(set (match_operand:BLK 0 "" "")
111 (unspec:BLK [(match_dup 0) (match_operand:SI 1 "const_int_operand")]
112 UNSPEC_MEMBAR))
113 (set (match_operand:BLK 2 "" "")
114 (unspec:BLK [(match_dup 2) (match_operand:SI 3 "const_int_operand")]
115 UNSPEC_MEMBAR))]
116 ""
117 [(set (match_operand:BLK 0 "" "")
118 (unspec:BLK [(match_dup 0) (match_dup 1)] UNSPEC_MEMBAR))]
119 { operands[1] = GEN_INT (UINTVAL (operands[1]) | UINTVAL (operands[3])); })
120
121 (define_expand "atomic_load<mode>"
122 [(match_operand:I 0 "register_operand" "")
123 (match_operand:I 1 "memory_operand" "")
124 (match_operand:SI 2 "const_int_operand" "")]
125 ""
126 {
127 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
128
129 sparc_emit_membar_for_model (model, 1, 1);
130
131 if (TARGET_ARCH64 || <MODE>mode != DImode)
132 emit_move_insn (operands[0], operands[1]);
133 else
134 emit_insn (gen_atomic_loaddi_1 (operands[0], operands[1]));
135
136 sparc_emit_membar_for_model (model, 1, 2);
137 DONE;
138 })
139
140 (define_insn "atomic_loaddi_1"
141 [(set (match_operand:DI 0 "register_operand" "=r,?*f")
142 (unspec:DI [(match_operand:DI 1 "memory_operand" "T,T")]
143 UNSPEC_ATOMIC))]
144 "TARGET_ARCH32"
145 "ldd\t%1, %0"
146 [(set_attr "type" "load,fpload")])
147
148 (define_expand "atomic_store<mode>"
149 [(match_operand:I 0 "memory_operand" "")
150 (match_operand:I 1 "register_operand" "")
151 (match_operand:SI 2 "const_int_operand" "")]
152 ""
153 {
154 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
155
156 sparc_emit_membar_for_model (model, 2, 1);
157
158 if (TARGET_ARCH64 || <MODE>mode != DImode)
159 emit_move_insn (operands[0], operands[1]);
160 else
161 emit_insn (gen_atomic_storedi_1 (operands[0], operands[1]));
162
163 sparc_emit_membar_for_model (model, 2, 2);
164 DONE;
165 })
166
167 (define_insn "atomic_storedi_1"
168 [(set (match_operand:DI 0 "memory_operand" "=T,T,T")
169 (unspec:DI
170 [(match_operand:DI 1 "register_or_v9_zero_operand" "J,r,?*f")]
171 UNSPEC_ATOMIC))]
172 "TARGET_ARCH32"
173 "@
174 stx\t%r1, %0
175 std\t%1, %0
176 std\t%1, %0"
177 [(set_attr "type" "store,store,fpstore")
178 (set_attr "cpu_feature" "v9,*,*")])
179
180 (define_expand "atomic_compare_and_swap<mode>"
181 [(match_operand:SI 0 "register_operand" "") ;; bool output
182 (match_operand:I 1 "register_operand" "") ;; val output
183 (match_operand:I 2 "mem_noofs_operand" "") ;; memory
184 (match_operand:I 3 "register_operand" "") ;; expected
185 (match_operand:I 4 "register_operand" "") ;; desired
186 (match_operand:SI 5 "const_int_operand" "") ;; is_weak
187 (match_operand:SI 6 "const_int_operand" "") ;; mod_s
188 (match_operand:SI 7 "const_int_operand" "")] ;; mod_f
189 "(TARGET_V9 || TARGET_LEON3)
190 && (<MODE>mode != DImode || TARGET_ARCH64 || TARGET_V8PLUS)"
191 {
192 sparc_expand_compare_and_swap (operands);
193 DONE;
194 })
195
196 (define_expand "atomic_compare_and_swap<mode>_1"
197 [(parallel
198 [(set (match_operand:I48MODE 0 "register_operand" "")
199 (match_operand:I48MODE 1 "mem_noofs_operand" ""))
200 (set (match_dup 1)
201 (unspec_volatile:I48MODE
202 [(match_operand:I48MODE 2 "register_operand" "")
203 (match_operand:I48MODE 3 "register_operand" "")]
204 UNSPECV_CAS))])]
205 "TARGET_V9 || TARGET_LEON3"
206 "")
207
208 (define_insn "*atomic_compare_and_swap<mode>_1"
209 [(set (match_operand:I48MODE 0 "register_operand" "=r")
210 (match_operand:I48MODE 1 "mem_noofs_operand" "+W"))
211 (set (match_dup 1)
212 (unspec_volatile:I48MODE
213 [(match_operand:I48MODE 2 "register_operand" "r")
214 (match_operand:I48MODE 3 "register_operand" "0")]
215 UNSPECV_CAS))]
216 "TARGET_V9 && (<MODE>mode != DImode || TARGET_ARCH64)"
217 "cas<modesuffix>\t%1, %2, %0"
218 [(set_attr "type" "multi")])
219
220 (define_insn "atomic_compare_and_swap_leon3_1"
221 [(set (match_operand:SI 0 "register_operand" "=r")
222 (match_operand:SI 1 "mem_noofs_operand" "+W"))
223 (set (match_dup 1)
224 (unspec_volatile:SI
225 [(match_operand:SI 2 "register_operand" "r")
226 (match_operand:SI 3 "register_operand" "0")]
227 UNSPECV_CAS))]
228 "TARGET_LEON3"
229 {
230 if (sparc_fix_gr712rc)
231 output_asm_insn (".align\t16", operands);
232 if (TARGET_SV_MODE)
233 return "casa\t%1 0xb, %2, %0"; /* ASI for supervisor data space. */
234 else
235 return "casa\t%1 0xa, %2, %0"; /* ASI for user data space. */
236 }
237 [(set_attr "type" "multi")
238 (set (attr "length") (if_then_else (eq_attr "fix_gr712rc" "true")
239 (const_int 4) (const_int 1)))])
240
241 (define_insn "*atomic_compare_and_swapdi_v8plus"
242 [(set (match_operand:DI 0 "register_operand" "=h")
243 (match_operand:DI 1 "mem_noofs_operand" "+W"))
244 (set (match_dup 1)
245 (unspec_volatile:DI
246 [(match_operand:DI 2 "register_operand" "h")
247 (match_operand:DI 3 "register_operand" "0")]
248 UNSPECV_CAS))]
249 "TARGET_V8PLUS"
250 {
251 if (sparc_check_64 (operands[3], insn) <= 0)
252 output_asm_insn ("srl\t%L3, 0, %L3", operands);
253 output_asm_insn ("sllx\t%H3, 32, %H3", operands);
254 output_asm_insn ("or\t%L3, %H3, %L3", operands);
255 if (sparc_check_64 (operands[2], insn) <= 0)
256 output_asm_insn ("srl\t%L2, 0, %L2", operands);
257 output_asm_insn ("sllx\t%H2, 32, %H3", operands);
258 output_asm_insn ("or\t%L2, %H3, %H3", operands);
259 output_asm_insn ("casx\t%1, %H3, %L3", operands);
260 return "srlx\t%L3, 32, %H3";
261 }
262 [(set_attr "type" "multi")
263 (set_attr "length" "8")])
264
265 (define_expand "atomic_exchangesi"
266 [(match_operand:SI 0 "register_operand" "")
267 (match_operand:SI 1 "memory_operand" "")
268 (match_operand:SI 2 "register_operand" "")
269 (match_operand:SI 3 "const_int_operand" "")]
270 "(TARGET_V8 || TARGET_V9) && !sparc_fix_ut699"
271 {
272 enum memmodel model = (enum memmodel) INTVAL (operands[3]);
273
274 sparc_emit_membar_for_model (model, 3, 1);
275 emit_insn (gen_swapsi (operands[0], operands[1], operands[2]));
276 sparc_emit_membar_for_model (model, 3, 2);
277 DONE;
278 })
279
280 (define_insn "swapsi"
281 [(set (match_operand:SI 0 "register_operand" "=r")
282 (unspec_volatile:SI [(match_operand:SI 1 "memory_operand" "+m")]
283 UNSPECV_SWAP))
284 (set (match_dup 1)
285 (match_operand:SI 2 "register_operand" "0"))]
286 "(TARGET_V8 || TARGET_V9) && !sparc_fix_ut699"
287 {
288 if (sparc_fix_gr712rc)
289 return ".align\t16\n\tswap\t%1, %0";
290 else
291 return "swap\t%1, %0";
292 }
293 [(set_attr "type" "multi")
294 (set (attr "length") (if_then_else (eq_attr "fix_gr712rc" "true")
295 (const_int 4) (const_int 1)))])
296
297 (define_expand "atomic_test_and_set"
298 [(match_operand:QI 0 "register_operand" "")
299 (match_operand:QI 1 "memory_operand" "")
300 (match_operand:SI 2 "const_int_operand" "")]
301 "!sparc_fix_ut699"
302 {
303 enum memmodel model = (enum memmodel) INTVAL (operands[2]);
304 rtx ret;
305
306 sparc_emit_membar_for_model (model, 3, 1);
307 emit_insn (gen_ldstub (operands[0], operands[1]));
308 sparc_emit_membar_for_model (model, 3, 2);
309
310 /* Convert the 0/0xff result we would otherwise have to a boolean.
311 I.e. ignore all but bit 0. */
312 ret = expand_simple_binop (QImode, AND, operands[0], const1_rtx,
313 operands[0], true, OPTAB_LIB_WIDEN);
314 if (ret != operands[0])
315 emit_move_insn (operands[0], ret);
316
317 DONE;
318 })
319
320 (define_insn "ldstub"
321 [(set (match_operand:QI 0 "register_operand" "=r")
322 (unspec_volatile:QI [(match_operand:QI 1 "memory_operand" "+m")]
323 UNSPECV_LDSTUB))
324 (set (match_dup 1) (const_int -1))]
325 "!sparc_fix_ut699"
326 {
327 if (sparc_fix_gr712rc)
328 return ".align\t16\n\tldstub\t%1, %0";
329 else
330 return "ldstub\t%1, %0";
331 }
332 [(set_attr "type" "multi")
333 (set (attr "length") (if_then_else (eq_attr "fix_gr712rc" "true")
334 (const_int 4) (const_int 1)))])