1  /* Information about function binary interfaces.
       2     Copyright (C) 2019-2023 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  /* This class collects information about the ABIs of functions that are
     212     called in a particular region of code.  It is mostly intended to be
     213     used as a local variable during an IR walk.  */
     214  class function_abi_aggregator
     215  {
     216  public:
     217    function_abi_aggregator () : m_abi_clobbers () {}
     218  
     219    /* Record that the code region calls a function with the given ABI.  */
     220    void
     221    note_callee_abi (const function_abi &abi)
     222    {
     223      m_abi_clobbers[abi.id ()] |= abi.full_and_partial_reg_clobbers ();
     224    }
     225  
     226    HARD_REG_SET caller_save_regs (const function_abi &) const;
     227  
     228  private:
     229    HARD_REG_SET m_abi_clobbers[NUM_ABI_IDS];
     230  };
     231  
     232  struct target_function_abi_info
     233  {
     234    /* An array of all the target ABIs that are available in this
     235       translation unit.  Not all entries are used for all targets,
     236       but the structures are relatively small, and using a fixed-size
     237       array avoids extra indirection.
     238  
     239       There are various ways of getting an ABI descriptor:
     240  
     241       * fndecl_abi (FNDECL) is the ABI of function FNDECL.
     242  
     243       * fntype_abi (FNTYPE) is the ABI of a function with type FNTYPE.
     244  
     245       * crtl->abi is the ABI of the function that we are currently
     246         compiling to rtl.
     247  
     248       * insn_callee_abi (INSN) is the ABI used by the target of call insn INSN.
     249  
     250       * eh_edge_abi is the "ABI" used when taking an EH edge from an
     251         exception-throwing statement to an exception handler.  Catching
     252         exceptions from calls can be treated as an abnormal return from
     253         those calls, and this ABI therefore describes the ABI of functions
     254         on such an abnormal return.  Statements that throw non-call
     255         exceptions can be treated as being implicitly wrapped in a call
     256         that has such an abnormal return.
     257  
     258         At present, no target needs to support more than one EH ABI.
     259  
     260       * function_abis[N] is the ABI with identifier N.  This can be useful
     261         when referring back to ABIs that have been collected by number in
     262         a bitmask, such as after walking function calls in a particular
     263         region of code.
     264  
     265       * default_function_abi refers specifically to the target's default
     266         choice of ABI, regardless of which (if any) functions actually
     267         use it.  This ABI and data derived from it do *not* provide
     268         globally conservatively-correct information, so it is only
     269         useful in very specific circumstances.  */
     270    predefined_function_abi x_function_abis[NUM_ABI_IDS];
     271  };
     272  
     273  extern target_function_abi_info default_target_function_abi_info;
     274  #if SWITCHABLE_TARGET
     275  extern target_function_abi_info *this_target_function_abi_info;
     276  #else
     277  #define this_target_function_abi_info (&default_target_function_abi_info)
     278  #endif
     279  
     280  /* See the comment above x_function_abis for when these macros should be used.
     281     At present, eh_edge_abi is always the default ABI, but that could change
     282     in future if a target needs it to.  */
     283  #define function_abis \
     284    (this_target_function_abi_info->x_function_abis)
     285  #define default_function_abi \
     286    (this_target_function_abi_info->x_function_abis[0])
     287  #define eh_edge_abi default_function_abi
     288  
     289  extern HARD_REG_SET call_clobbers_in_region (unsigned int, const_hard_reg_set,
     290  					     machine_mode mode);
     291  
     292  /* Return true if (reg:MODE REGNO) might be clobbered by one of the
     293     calls in a region described by ABIS and MASK, where:
     294  
     295     * Bit ID of ABIS is set if the region contains a call with
     296       function_abi identifier ID.
     297  
     298     * MASK contains all the registers that are fully or partially
     299       clobbered by calls in the region.
     300  
     301     This is not quite as accurate as testing each individual call,
     302     but it's a close and conservatively-correct approximation.
     303     It's much better for some targets than:
     304  
     305       overlaps_hard_reg_set_p (MASK, MODE, REGNO).  */
     306  
     307  inline bool
     308  call_clobbered_in_region_p (unsigned int abis, const_hard_reg_set mask,
     309  			    machine_mode mode, unsigned int regno)
     310  {
     311    HARD_REG_SET clobbers = call_clobbers_in_region (abis, mask, mode);
     312    return overlaps_hard_reg_set_p (clobbers, mode, regno);
     313  }
     314  
     315  extern const predefined_function_abi &fntype_abi (const_tree);
     316  extern function_abi fndecl_abi (const_tree);
     317  extern function_abi insn_callee_abi (const rtx_insn *);
     318  extern function_abi expr_callee_abi (const_tree);
     319  
     320  #endif