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