1  /* Feature dependency helpers for AArch64.
       2     Copyright (C) 2022-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
       7     under the terms of the GNU General Public License as published by
       8     the Free Software Foundation; either version 3, or (at your option)
       9     any later version.
      10  
      11     GCC is distributed in the hope that it will be useful, but
      12     WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14     General Public License 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 AARCH64_FEATURE_DEPS_H
      21  #define AARCH64_FEATURE_DEPS_H 1
      22  
      23  namespace {
      24  namespace feature_deps {
      25  
      26  /* Together, these definitions of get_flags take a list of
      27     feature names (representing functions that are defined below)
      28     and return the set of associated flags.  */
      29  constexpr aarch64_feature_flags get_flags () { return 0; }
      30  
      31  template<typename T1, typename ...Ts>
      32  constexpr aarch64_feature_flags
      33  get_flags (T1 i, Ts... args)
      34  {
      35    return i ().flag | get_flags (args...);
      36  }
      37  
      38  /* Like get_flags, but return the transitive closure of those features
      39     and the ones that they rely on.  */
      40  constexpr aarch64_feature_flags get_enable () { return 0; }
      41  
      42  template<typename T1, typename ...Ts>
      43  constexpr aarch64_feature_flags
      44  get_enable (T1 i, Ts... args)
      45  {
      46    return i ().enable | get_enable (args...);
      47  }
      48  
      49  /* Define info<FEATURE> such that it has the following static constant
      50     variables:
      51  
      52     - flag: the aarch64_feature_flags bit associated with FEATURE
      53  
      54     - enable: the transitive closure of the features that FEATURE requires,
      55       plus FLAG itself
      56  
      57     - explicit_on: the transitive closure of the features that an
      58       explicit +FEATURE enables, including FLAG itself.  This is
      59       always a superset of ENABLE
      60  
      61     Also define a function FEATURE () that returns an info<FEATURE>
      62     (which is an empty structure, since all members are static).
      63  
      64     Building up the list feature-by-feature ensures that the definition
      65     files are in topological order.  */
      66  template<aarch64_feature> struct info;
      67  
      68  #define HANDLE(IDENT, REQUIRES, EXPLICIT_ON)				\
      69    template<> struct info<aarch64_feature::IDENT> {			\
      70      static constexpr auto flag = AARCH64_FL_##IDENT;			\
      71      static constexpr auto enable = flag | get_enable REQUIRES;		\
      72      static constexpr auto explicit_on = enable | get_enable EXPLICIT_ON; \
      73    };									\
      74    constexpr info<aarch64_feature::IDENT> IDENT ()			\
      75    {									\
      76      return info<aarch64_feature::IDENT> ();				\
      77    }
      78  #define AARCH64_OPT_EXTENSION(A, IDENT, REQUIRES, EXPLICIT_ON, E, F) \
      79    HANDLE (IDENT, REQUIRES, EXPLICIT_ON)
      80  #define AARCH64_ARCH(A, B, IDENT, D, REQUIRES) HANDLE (IDENT, REQUIRES, ())
      81  #include "config/aarch64/aarch64-option-extensions.def"
      82  #include "config/aarch64/aarch64-arches.def"
      83  #undef HANDLE
      84  
      85  /* Return the set of all features that would need to be disabled if
      86     the features in MASK are disabled.
      87  
      88     Note that the size of the expression varies linearly with the number
      89     of features, which means that invoking this function once per feature
      90     is quadratic in the number of features.  However, collecting the same
      91     information at compiler start-up is likely to be quadratic too, so
      92     we're better off paying the cost once per compiler build rather than
      93     once per compiler run.  */
      94  constexpr aarch64_feature_flags
      95  get_flags_off (aarch64_feature_flags mask)
      96  {
      97    return (0
      98  #define AARCH64_OPT_EXTENSION(A, IDENT, C, D, E, F) \
      99  	  | (feature_deps::IDENT ().enable & mask ? AARCH64_FL_##IDENT : 0)
     100  #include "config/aarch64/aarch64-option-extensions.def"
     101  	  );
     102  }
     103  
     104  /* Define root_off_<IDENT> variables for each feature, giving the set of
     105     features that must be turned off by +noIDENT.  This set is not transitively
     106     closed; use get_flags_off to complete the closure.  */
     107  #define AARCH64_OPT_EXTENSION(A, IDENT, C, D, EXPLICIT_OFF, F) \
     108    constexpr auto root_off_##IDENT \
     109      = AARCH64_FL_##IDENT | get_flags EXPLICIT_OFF;
     110  #include "config/aarch64/aarch64-option-extensions.def"
     111  
     112  /* Define cpu_<NAME> variables for each CPU, giving the transitive
     113     closure of all the features that the CPU supports.  */
     114  #define AARCH64_CORE(A, CORE_IDENT, C, ARCH_IDENT, FEATURES, F, G, H, I) \
     115    constexpr auto cpu_##CORE_IDENT = ARCH_IDENT ().enable | get_enable FEATURES;
     116  #include "config/aarch64/aarch64-cores.def"
     117  
     118  }
     119  }
     120  
     121  #endif