]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/function-abi.h
Add a function for getting the ABI of a call insn target
[thirdparty/gcc.git] / gcc / function-abi.h
1 /* Information about fuunction binary interfaces.
2 Copyright (C) 2019 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 it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 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 #ifndef GCC_FUNCTION_ABI_H
21 #define GCC_FUNCTION_ABI_H
22
23 /* Most targets use the same ABI for all functions in a translation
24 unit, but some targets support interoperability between several ABIs.
25 Each such ABI has a unique 0-based identifier, with 0 always being
26 the default choice of ABI.
27
28 NUM_ABI_IDS is the maximum number of such ABIs that GCC can handle at once.
29 A bitfield with this number of bits can represent any combinaion of the
30 supported ABIs. */
31 const size_t NUM_ABI_IDS = 8;
32
33 /* Information about one of the target's predefined ABIs. */
34 class predefined_function_abi
35 {
36 public:
37 /* A target-specific identifier for this ABI. The value must be in
38 the range [0, NUM_ABI_IDS - 1]. */
39 unsigned int id () const { return m_id; }
40
41 /* True if this ABI has been initialized. */
42 bool initialized_p () const { return m_initialized; }
43
44 /* Return true if a function call is allowed to alter every bit of
45 register REGNO, so that the register contains an arbitrary value
46 on return. If so, the register cannot hold any part of a value
47 that is live across a call. */
48 bool
49 clobbers_full_reg_p (unsigned int regno) const
50 {
51 return TEST_HARD_REG_BIT (m_full_reg_clobbers, regno);
52 }
53
54 /* Return true if a function call is allowed to alter some or all bits
55 of register REGNO.
56
57 This is true whenever clobbers_full_reg_p (REGNO) is true. It is
58 also true if, for example, the ABI says that a call must preserve the
59 low 32 or 64 bits of REGNO, but can clobber the upper bits of REGNO.
60 In the latter case, it is possible for REGNO to hold values that
61 are live across a call, provided that the value occupies only the
62 call-preserved part of the register. */
63 bool
64 clobbers_at_least_part_of_reg_p (unsigned int regno) const
65 {
66 return TEST_HARD_REG_BIT (m_full_and_partial_reg_clobbers, regno);
67 }
68
69 /* Return true if a function call is allowed to clobber at least part
70 of (reg:MODE REGNO). If so, it is not possible for the register
71 as a whole to be live across a call. */
72 bool
73 clobbers_reg_p (machine_mode mode, unsigned int regno) const
74 {
75 return overlaps_hard_reg_set_p (m_mode_clobbers[mode], mode, regno);
76 }
77
78 /* Return the set of registers that a function call is allowed to
79 alter completely, so that the registers contain arbitrary values
80 on return. This doesn't include registers that a call can only
81 partly clobber (as per TARGET_HARD_REGNO_CALL_PART_CLOBBERED).
82
83 These registers cannot hold any part of a value that is live across
84 a call. */
85 HARD_REG_SET full_reg_clobbers () const { return m_full_reg_clobbers; }
86
87 /* Return the set of registers that a function call is allowed to alter
88 to some degree. For example, if an ABI says that a call must preserve
89 the low 32 or 64 bits of a register R, but can clobber the upper bits
90 of R, R would be in this set but not in full_reg_clobbers ().
91
92 This set is a superset of full_reg_clobbers (). It is possible for a
93 register in full_and_partial_reg_clobbers () & ~full_reg_clobbers ()
94 to contain values that are live across a call, provided that the live
95 value only occupies the call-preserved part of the register. */
96 HARD_REG_SET
97 full_and_partial_reg_clobbers () const
98 {
99 return m_full_and_partial_reg_clobbers;
100 }
101
102 /* Return the set of registers that cannot be used to hold a value of
103 mode MODE across a function call. That is:
104
105 (reg:REGNO MODE)
106
107 might be clobbered by a call whenever:
108
109 overlaps_hard_reg_set (mode_clobbers (MODE), MODE, REGNO)
110
111 In allocation terms, the registers in the returned set conflict
112 with any value of mode MODE that is live across a call. */
113 HARD_REG_SET
114 mode_clobbers (machine_mode mode) const
115 {
116 return m_mode_clobbers[mode];
117 }
118
119 void initialize (unsigned int, const_hard_reg_set);
120 void add_full_reg_clobber (unsigned int);
121
122 private:
123 unsigned int m_id : NUM_ABI_IDS;
124 unsigned int m_initialized : 1;
125 HARD_REG_SET m_full_reg_clobbers;
126 HARD_REG_SET m_full_and_partial_reg_clobbers;
127 HARD_REG_SET m_mode_clobbers[NUM_MACHINE_MODES];
128 };
129
130 /* Describes either a predefined ABI or the ABI of a particular function.
131 In the latter case, the ABI might make use of extra function-specific
132 information, such as for -fipa-ra. */
133 class function_abi
134 {
135 public:
136 /* Initialize the structure for a general function with the given ABI. */
137 function_abi (const predefined_function_abi &base_abi)
138 : m_base_abi (&base_abi),
139 m_mask (base_abi.full_and_partial_reg_clobbers ()) {}
140
141 /* Initialize the structure for a function that has the given ABI and
142 that is known not to clobber registers outside MASK. */
143 function_abi (const predefined_function_abi &base_abi,
144 const_hard_reg_set mask)
145 : m_base_abi (&base_abi), m_mask (mask) {}
146
147 /* The predefined ABI from which this ABI is derived. */
148 const predefined_function_abi &base_abi () const { return *m_base_abi; }
149
150 /* The target-specific identifier of the predefined ABI. */
151 unsigned int id () const { return m_base_abi->id (); }
152
153 /* See the corresponding predefined_function_abi functions for
154 details about the following functions. */
155
156 HARD_REG_SET
157 full_reg_clobbers () const
158 {
159 return m_mask & m_base_abi->full_reg_clobbers ();
160 }
161
162 HARD_REG_SET
163 full_and_partial_reg_clobbers () const
164 {
165 return m_mask & m_base_abi->full_and_partial_reg_clobbers ();
166 }
167
168 HARD_REG_SET
169 mode_clobbers (machine_mode mode) const
170 {
171 return m_mask & m_base_abi->mode_clobbers (mode);
172 }
173
174 bool
175 clobbers_full_reg_p (unsigned int regno) const
176 {
177 return (TEST_HARD_REG_BIT (m_mask, regno)
178 & m_base_abi->clobbers_full_reg_p (regno));
179 }
180
181 bool
182 clobbers_at_least_part_of_reg_p (unsigned int regno) const
183 {
184 return (TEST_HARD_REG_BIT (m_mask, regno)
185 & m_base_abi->clobbers_at_least_part_of_reg_p (regno));
186 }
187
188 bool
189 clobbers_reg_p (machine_mode mode, unsigned int regno) const
190 {
191 return overlaps_hard_reg_set_p (mode_clobbers (mode), mode, regno);
192 }
193
194 bool
195 operator== (const function_abi &other) const
196 {
197 return m_base_abi == other.m_base_abi && m_mask == other.m_mask;
198 }
199
200 bool
201 operator!= (const function_abi &other) const
202 {
203 return !operator== (other);
204 }
205
206 protected:
207 const predefined_function_abi *m_base_abi;
208 HARD_REG_SET m_mask;
209 };
210
211 struct target_function_abi_info
212 {
213 /* An array of all the target ABIs that are available in this
214 translation unit. Not all entries are used for all targets,
215 but the structures are relatively small, and using a fixed-size
216 array avoids extra indirection.
217
218 There are various ways of getting an ABI descriptor:
219
220 * fndecl_abi (FNDECL) is the ABI of function FNDECL.
221
222 * fntype_abi (FNTYPE) is the ABI of a function with type FNTYPE.
223
224 * crtl->abi is the ABI of the function that we are currently
225 compiling to rtl.
226
227 * insn_callee_abi (INSN) is the ABI used by the target of call insn INSN.
228
229 * eh_edge_abi is the "ABI" used when taking an EH edge from an
230 exception-throwing statement to an exception handler. Catching
231 exceptions from calls can be treated as an abnormal return from
232 those calls, and this ABI therefore describes the ABI of functions
233 on such an abnormal return. Statements that throw non-call
234 exceptions can be treated as being implicitly wrapped in a call
235 that has such an abnormal return.
236
237 At present, no target needs to support more than one EH ABI.
238
239 * function_abis[N] is the ABI with identifier N. This can be useful
240 when referring back to ABIs that have been collected by number in
241 a bitmask, such as after walking function calls in a particular
242 region of code.
243
244 * default_function_abi refers specifically to the target's default
245 choice of ABI, regardless of which (if any) functions actually
246 use it. This ABI and data derived from it do *not* provide
247 globally conservatively-correct information, so it is only
248 useful in very specific circumstances. */
249 predefined_function_abi x_function_abis[NUM_ABI_IDS];
250 };
251
252 extern target_function_abi_info default_target_function_abi_info;
253 #if SWITCHABLE_TARGET
254 extern target_function_abi_info *this_target_function_abi_info;
255 #else
256 #define this_target_function_abi_info (&default_target_function_abi_info)
257 #endif
258
259 /* See the comment above x_function_abis for when these macros should be used.
260 At present, eh_edge_abi is always the default ABI, but that could change
261 in future if a target needs it to. */
262 #define function_abis \
263 (this_target_function_abi_info->x_function_abis)
264 #define default_function_abi \
265 (this_target_function_abi_info->x_function_abis[0])
266 #define eh_edge_abi default_function_abi
267
268 extern const predefined_function_abi &fntype_abi (const_tree);
269 extern function_abi fndecl_abi (const_tree);
270 extern function_abi insn_callee_abi (const rtx_insn *);
271
272 #endif