Debian Codegraph @

gcc/config/bpf/atomic.md

raw
1 ;; Machine description for eBPF.
2 ;; Copyright (C) 2023-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
21 (define_mode_iterator AMO [SI DI])
22
23 ;;; Plain atomic modify operations.
24
25 ;; The BPF instruction set provides non-fetching atomic instructions
26 ;; that could be used to implement the corresponding named insns:
27 ;;
28 ;; atomic_add -> aadd (aka xadd)
29 ;; atomic_and -> aand
30 ;; atomic_or -> aor
31 ;; atomic_xor -> axor
32 ;;
33 ;; However, we are not including insns for these here because the
34 ;; non-fetching BPF atomic instruction imply different memory ordering
35 ;; semantics than the fetching BPF atomic instruction used to
36 ;; implement the atomic_fetch_* insns below (afadd, afand, afor,
37 ;; afxor) and they cannot be used interchangeably, as it is expected
38 ;; by GCC when it uses a non-fetching variant as an optimization of a
39 ;; fetching operation where the returned value is not used.
40
41 ;;; Feching (read-modify-store) versions of atomic operations.
42
43 (define_insn "atomic_fetch_add<AMO:mode>"
44 [(set (match_operand:AMO 0 "register_operand" "=r") ; output
45 (match_operand:AMO 1 "memory_operand" "+m"))
46 (set (match_dup 1)
47 (unspec_volatile:AMO
48 [(plus:AMO (match_dup 1)
49 (match_operand:AMO 2 "nonmemory_operand" "0")) ; second operand to op
50 (match_operand:AMO 3 "const_int_operand")] ;; Memory model
51 UNSPEC_AFADD))]
52 "bpf_has_v3_atomics"
53 "{afadd<msuffix>\t%1,%0|%w0 = atomic_fetch_add((<smop> *)%1, %w0)}")
54
55 (define_insn "atomic_fetch_and<AMO:mode>"
56 [(set (match_operand:AMO 0 "register_operand" "=r")
57 (match_operand:AMO 1 "memory_operand" "+m"))
58 (set (match_dup 1)
59 (unspec_volatile:AMO
60 [(and:AMO (match_dup 1)
61 (match_operand:AMO 2 "nonmemory_operand" "0"))
62 (match_operand:AMO 3 "const_int_operand")]
63 UNSPEC_AFAND))]
64 "bpf_has_v3_atomics"
65 "{afand<msuffix>\t%1,%0|%w0 = atomic_fetch_and((<smop> *)%1, %w0)}")
66
67 (define_insn "atomic_fetch_or<AMO:mode>"
68 [(set (match_operand:AMO 0 "register_operand" "=r")
69 (match_operand:AMO 1 "memory_operand" "+m"))
70 (set (match_dup 1)
71 (unspec_volatile:AMO
72 [(ior:AMO (match_dup 1)
73 (match_operand:AMO 2 "nonmemory_operand" "0"))
74 (match_operand:AMO 3 "const_int_operand")]
75 UNSPEC_AFOR))]
76 "bpf_has_v3_atomics"
77 "{afor<msuffix>\t%1,%0|%w0 = atomic_fetch_or((<smop> *)%1, %w0)}")
78
79 (define_insn "atomic_fetch_xor<AMO:mode>"
80 [(set (match_operand:AMO 0 "register_operand" "=r")
81 (match_operand:AMO 1 "memory_operand" "+m"))
82 (set (match_dup 1)
83 (unspec_volatile:AMO
84 [(xor:AMO (match_dup 1)
85 (match_operand:AMO 2 "nonmemory_operand" "0"))
86 (match_operand:AMO 3 "const_int_operand")]
87 UNSPEC_AFXOR))]
88 "bpf_has_v3_atomics"
89 "{afxor<msuffix>\t%1,%0|%w0 = atomic_fetch_xor((<smop> *)%1, %w0)}")
90
91 ;; Weird suffixes used in pseudo-c atomic compare-exchange insns.
92 (define_mode_attr pcaxsuffix [(SI "32_32") (DI "_64")])
93
94 (define_insn "atomic_exchange<AMO:mode>"
95 [(set (match_operand:AMO 0 "register_operand" "=r")
96 (unspec_volatile:AMO
97 [(match_operand:AMO 1 "memory_operand" "+m")
98 (match_operand:AMO 3 "const_int_operand")]
99 UNSPEC_AXCHG))
100 (set (match_dup 1)
101 (match_operand:AMO 2 "nonmemory_operand" "0"))]
102 "bpf_has_v3_atomics"
103 "{axchg<msuffix>\t%1,%0|%w0 = xchg<pcaxsuffix>(%M1, %w0)}")
104
105 ;; The eBPF atomic-compare-and-exchange instruction has the form
106 ;; acmp [%dst+offset], %src
107 ;; The instruction atomically compares the value addressed by %dst+offset
108 ;; with register R0. If they match, the value at %dst+offset is overwritten
109 ;; with the value of %src. Otherwise, no write occurs. In either case, the
110 ;; original value of %dst+offset is zero-extended and loaded back into R0.
111
112 (define_expand "atomic_compare_and_swap<AMO:mode>"
113 [(match_operand:SI 0 "register_operand" "=r") ;; bool success
114 (match_operand:AMO 1 "register_operand" "=r") ;; old value
115 (match_operand:AMO 2 "memory_operand" "+m") ;; memory
116 (match_operand:AMO 3 "register_operand") ;; expected
117 (match_operand:AMO 4 "register_operand") ;; desired
118 (match_operand:SI 5 "const_int_operand") ;; is_weak (unused)
119 (match_operand:SI 6 "const_int_operand") ;; success model (unused)
120 (match_operand:SI 7 "const_int_operand")] ;; failure model (unused)
121 "bpf_has_v3_atomics"
122 {
123 /* Load the expected value (into R0 by constraint of below). */
124 emit_move_insn (operands[1], operands[3]);
125
126 /* Emit the acmp. */
127 emit_insn (gen_atomic_compare_and_swap<AMO:mode>_1 (operands[1], operands[2], operands[3], operands[4]));
128
129 /* Assume that the operation was successful. */
130 emit_move_insn (operands[0], const1_rtx);
131 rtx_code_label *success_label = gen_label_rtx ();
132
133 /* Compare value that was in memory (now in R0/op[1]) to expected value.
134 If they are equal, then the write occurred. Otherwise, indicate fail in output. */
135 emit_cmp_and_jump_insns (operands[1], operands[3], EQ, 0,
136 GET_MODE (operands[1]), 1, success_label);
137 emit_move_insn (operands[0], const0_rtx);
138
139 if (success_label)
140 {
141 emit_label (success_label);
142 LABEL_NUSES (success_label) = 1;
143 }
144 DONE;
145 })
146
147 (define_insn "atomic_compare_and_swap<AMO:mode>_1"
148 [(set (match_operand:AMO 0 "register_operand" "+t") ;; R0 is both input (expected value)
149 (unspec_volatile:AMO ;; and output (original value)
150 [(match_dup 0) ;; result depends on R0
151 (match_operand:AMO 1 "memory_operand") ;; memory
152 (match_operand:AMO 2 "register_operand") ;; expected
153 (match_operand:AMO 3 "register_operand")] ;; desired
154 UNSPEC_ACMP))]
155 "bpf_has_v3_atomics"
156 "{acmp<msuffix>\t%1,%3|%w0 = cmpxchg<pcaxsuffix>(%M1, %w0, %w3)}")