function-abi.h 11 KB

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