| 1 | /* Information about function binary interfaces. |
| 2 | Copyright (C) 2019-2026 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 = 12; |
| 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 (set: m_full_reg_clobbers, bit: 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 (set: m_full_and_partial_reg_clobbers, bit: 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 (regs: 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 a function call is allowed to alter |
| 103 | partially but not fully; i.e. those in full_and_partial_reg_clobbers () |
| 104 | but not in full_reg_clobbers (). |
| 105 | |
| 106 | If a register X is in this set and if we don't know which parts of |
| 107 | X are live (typically because we don't bother to track X's mode), |
| 108 | then the conservative assumptions are: |
| 109 | |
| 110 | - to ignore the call when computing reaching definitions |
| 111 | - to treat X as clobbered when computing availability |
| 112 | |
| 113 | For example, if we have: |
| 114 | |
| 115 | A: X := Y |
| 116 | B: call that partially clobbers X |
| 117 | C: ... := ... X ... |
| 118 | |
| 119 | and don't track the mode of X when computing reaching definitions, |
| 120 | then the conservative assumption is that A's definition survives |
| 121 | until C. But if we have: |
| 122 | |
| 123 | A: X := Y |
| 124 | B: call that partially clobbers X |
| 125 | C: ... := ... Y ... |
| 126 | |
| 127 | and don't track the mode of X when computing availability, then the |
| 128 | conservative assumption is that Y is not available in X at C. */ |
| 129 | HARD_REG_SET |
| 130 | only_partial_reg_clobbers () const |
| 131 | { |
| 132 | return full_and_partial_reg_clobbers () & ~full_reg_clobbers (); |
| 133 | } |
| 134 | |
| 135 | /* Return the set of registers that cannot be used to hold a value of |
| 136 | mode MODE across a function call. That is: |
| 137 | |
| 138 | (reg:REGNO MODE) |
| 139 | |
| 140 | might be clobbered by a call whenever: |
| 141 | |
| 142 | overlaps_hard_reg_set (mode_clobbers (MODE), MODE, REGNO) |
| 143 | |
| 144 | In allocation terms, the registers in the returned set conflict |
| 145 | with any value of mode MODE that is live across a call. */ |
| 146 | HARD_REG_SET |
| 147 | mode_clobbers (machine_mode mode) const |
| 148 | { |
| 149 | return m_mode_clobbers[mode]; |
| 150 | } |
| 151 | |
| 152 | void initialize (unsigned int, const_hard_reg_set); |
| 153 | void add_full_reg_clobber (unsigned int); |
| 154 | |
| 155 | private: |
| 156 | unsigned int m_id : NUM_ABI_IDS; |
| 157 | unsigned int m_initialized : 1; |
| 158 | HARD_REG_SET m_full_reg_clobbers; |
| 159 | HARD_REG_SET m_full_and_partial_reg_clobbers; |
| 160 | HARD_REG_SET m_mode_clobbers[NUM_MACHINE_MODES]; |
| 161 | }; |
| 162 | |
| 163 | /* Describes either a predefined ABI or the ABI of a particular function. |
| 164 | In the latter case, the ABI might make use of extra function-specific |
| 165 | information, such as for -fipa-ra. */ |
| 166 | class function_abi |
| 167 | { |
| 168 | public: |
| 169 | /* Initialize the structure for a general function with the given ABI. */ |
| 170 | function_abi (const predefined_function_abi &base_abi) |
| 171 | : m_base_abi (&base_abi), |
| 172 | m_mask (base_abi.full_and_partial_reg_clobbers ()) {} |
| 173 | |
| 174 | /* Initialize the structure for a function that has the given ABI and |
| 175 | that is known not to clobber registers outside MASK. */ |
| 176 | function_abi (const predefined_function_abi &base_abi, |
| 177 | const_hard_reg_set mask) |
| 178 | : m_base_abi (&base_abi), m_mask (mask) {} |
| 179 | |
| 180 | /* The predefined ABI from which this ABI is derived. */ |
| 181 | const predefined_function_abi &base_abi () const { return *m_base_abi; } |
| 182 | |
| 183 | /* The target-specific identifier of the predefined ABI. */ |
| 184 | unsigned int id () const { return m_base_abi->id (); } |
| 185 | |
| 186 | /* See the corresponding predefined_function_abi functions for |
| 187 | details about the following functions. */ |
| 188 | |
| 189 | HARD_REG_SET |
| 190 | full_reg_clobbers () const |
| 191 | { |
| 192 | return m_mask & m_base_abi->full_reg_clobbers (); |
| 193 | } |
| 194 | |
| 195 | HARD_REG_SET |
| 196 | full_and_partial_reg_clobbers () const |
| 197 | { |
| 198 | return m_mask & m_base_abi->full_and_partial_reg_clobbers (); |
| 199 | } |
| 200 | |
| 201 | HARD_REG_SET |
| 202 | only_partial_reg_clobbers () const |
| 203 | { |
| 204 | return m_mask & m_base_abi->only_partial_reg_clobbers (); |
| 205 | } |
| 206 | |
| 207 | HARD_REG_SET |
| 208 | mode_clobbers (machine_mode mode) const |
| 209 | { |
| 210 | return m_mask & m_base_abi->mode_clobbers (mode); |
| 211 | } |
| 212 | |
| 213 | bool |
| 214 | clobbers_full_reg_p (unsigned int regno) const |
| 215 | { |
| 216 | return (TEST_HARD_REG_BIT (set: m_mask, bit: regno) |
| 217 | & m_base_abi->clobbers_full_reg_p (regno)); |
| 218 | } |
| 219 | |
| 220 | bool |
| 221 | clobbers_at_least_part_of_reg_p (unsigned int regno) const |
| 222 | { |
| 223 | return (TEST_HARD_REG_BIT (set: m_mask, bit: regno) |
| 224 | & m_base_abi->clobbers_at_least_part_of_reg_p (regno)); |
| 225 | } |
| 226 | |
| 227 | bool |
| 228 | clobbers_reg_p (machine_mode mode, unsigned int regno) const |
| 229 | { |
| 230 | return overlaps_hard_reg_set_p (regs: mode_clobbers (mode), mode, regno); |
| 231 | } |
| 232 | |
| 233 | bool |
| 234 | operator== (const function_abi &other) const |
| 235 | { |
| 236 | return m_base_abi == other.m_base_abi && m_mask == other.m_mask; |
| 237 | } |
| 238 | |
| 239 | bool |
| 240 | operator!= (const function_abi &other) const |
| 241 | { |
| 242 | return !operator== (other); |
| 243 | } |
| 244 | |
| 245 | protected: |
| 246 | const predefined_function_abi *m_base_abi; |
| 247 | HARD_REG_SET m_mask; |
| 248 | }; |
| 249 | |
| 250 | /* This class collects information about the ABIs of functions that are |
| 251 | called in a particular region of code. It is mostly intended to be |
| 252 | used as a local variable during an IR walk. */ |
| 253 | class function_abi_aggregator |
| 254 | { |
| 255 | public: |
| 256 | function_abi_aggregator () : m_abi_clobbers () {} |
| 257 | |
| 258 | /* Record that the code region calls a function with the given ABI. */ |
| 259 | void |
| 260 | note_callee_abi (const function_abi &abi) |
| 261 | { |
| 262 | m_abi_clobbers[abi.id ()] |= abi.full_and_partial_reg_clobbers (); |
| 263 | } |
| 264 | |
| 265 | HARD_REG_SET caller_save_regs (const function_abi &) const; |
| 266 | |
| 267 | private: |
| 268 | HARD_REG_SET m_abi_clobbers[NUM_ABI_IDS]; |
| 269 | }; |
| 270 | |
| 271 | struct target_function_abi_info |
| 272 | { |
| 273 | /* An array of all the target ABIs that are available in this |
| 274 | translation unit. Not all entries are used for all targets, |
| 275 | but the structures are relatively small, and using a fixed-size |
| 276 | array avoids extra indirection. |
| 277 | |
| 278 | There are various ways of getting an ABI descriptor: |
| 279 | |
| 280 | * fndecl_abi (FNDECL) is the ABI of function FNDECL. |
| 281 | |
| 282 | * fntype_abi (FNTYPE) is the ABI of a function with type FNTYPE. |
| 283 | |
| 284 | * crtl->abi is the ABI of the function that we are currently |
| 285 | compiling to rtl. |
| 286 | |
| 287 | * insn_callee_abi (INSN) is the ABI used by the target of call insn INSN. |
| 288 | |
| 289 | * eh_edge_abi is the "ABI" used when taking an EH edge from an |
| 290 | exception-throwing statement to an exception handler. Catching |
| 291 | exceptions from calls can be treated as an abnormal return from |
| 292 | those calls, and this ABI therefore describes the ABI of functions |
| 293 | on such an abnormal return. Statements that throw non-call |
| 294 | exceptions can be treated as being implicitly wrapped in a call |
| 295 | that has such an abnormal return. |
| 296 | |
| 297 | At present, no target needs to support more than one EH ABI. |
| 298 | |
| 299 | * function_abis[N] is the ABI with identifier N. This can be useful |
| 300 | when referring back to ABIs that have been collected by number in |
| 301 | a bitmask, such as after walking function calls in a particular |
| 302 | region of code. |
| 303 | |
| 304 | * default_function_abi refers specifically to the target's default |
| 305 | choice of ABI, regardless of which (if any) functions actually |
| 306 | use it. This ABI and data derived from it do *not* provide |
| 307 | globally conservatively-correct information, so it is only |
| 308 | useful in very specific circumstances. */ |
| 309 | predefined_function_abi x_function_abis[NUM_ABI_IDS]; |
| 310 | }; |
| 311 | |
| 312 | extern target_function_abi_info default_target_function_abi_info; |
| 313 | #if SWITCHABLE_TARGET |
| 314 | extern target_function_abi_info *this_target_function_abi_info; |
| 315 | #else |
| 316 | #define this_target_function_abi_info (&default_target_function_abi_info) |
| 317 | #endif |
| 318 | |
| 319 | /* See the comment above x_function_abis for when these macros should be used. |
| 320 | At present, eh_edge_abi is always the default ABI, but that could change |
| 321 | in future if a target needs it to. */ |
| 322 | #define function_abis \ |
| 323 | (this_target_function_abi_info->x_function_abis) |
| 324 | #define default_function_abi \ |
| 325 | (this_target_function_abi_info->x_function_abis[0]) |
| 326 | #define eh_edge_abi default_function_abi |
| 327 | |
| 328 | extern HARD_REG_SET call_clobbers_in_region (unsigned int, const_hard_reg_set, |
| 329 | machine_mode mode); |
| 330 | |
| 331 | /* Return true if (reg:MODE REGNO) might be clobbered by one of the |
| 332 | calls in a region described by ABIS and MASK, where: |
| 333 | |
| 334 | * Bit ID of ABIS is set if the region contains a call with |
| 335 | function_abi identifier ID. |
| 336 | |
| 337 | * MASK contains all the registers that are fully or partially |
| 338 | clobbered by calls in the region. |
| 339 | |
| 340 | This is not quite as accurate as testing each individual call, |
| 341 | but it's a close and conservatively-correct approximation. |
| 342 | It's much better for some targets than: |
| 343 | |
| 344 | overlaps_hard_reg_set_p (MASK, MODE, REGNO). */ |
| 345 | |
| 346 | inline bool |
| 347 | call_clobbered_in_region_p (unsigned int abis, const_hard_reg_set mask, |
| 348 | machine_mode mode, unsigned int regno) |
| 349 | { |
| 350 | HARD_REG_SET clobbers = call_clobbers_in_region (abis, mask, mode); |
| 351 | return overlaps_hard_reg_set_p (regs: clobbers, mode, regno); |
| 352 | } |
| 353 | |
| 354 | extern const predefined_function_abi &fntype_abi (const_tree); |
| 355 | extern function_abi fndecl_abi (const_tree); |
| 356 | extern function_abi insn_callee_abi (const rtx_insn *); |
| 357 | extern function_abi expr_callee_abi (const_tree); |
| 358 | |
| 359 | #endif |
| 360 | |