| 1 | /* Classes for purging state at function_points. |
| 2 | Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
| 4 | |
| 5 | This file is part of GCC. |
| 6 | |
| 7 | GCC is free software; you can redistribute it and/or modify it |
| 8 | under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3, or (at your option) |
| 10 | any later version. |
| 11 | |
| 12 | GCC is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GCC; see the file COPYING3. If not see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #ifndef GCC_ANALYZER_STATE_PURGE_H |
| 22 | #define GCC_ANALYZER_STATE_PURGE_H |
| 23 | |
| 24 | /* Hash traits for function_point. */ |
| 25 | |
| 26 | template <> struct default_hash_traits<function_point> |
| 27 | : public pod_hash_traits<function_point> |
| 28 | { |
| 29 | static const bool empty_zero_p = false; |
| 30 | }; |
| 31 | |
| 32 | template <> |
| 33 | inline hashval_t |
| 34 | pod_hash_traits<function_point>::hash (value_type v) |
| 35 | { |
| 36 | return v.hash (); |
| 37 | } |
| 38 | |
| 39 | template <> |
| 40 | inline bool |
| 41 | pod_hash_traits<function_point>::equal (const value_type &existing, |
| 42 | const value_type &candidate) |
| 43 | { |
| 44 | return existing == candidate; |
| 45 | } |
| 46 | template <> |
| 47 | inline void |
| 48 | pod_hash_traits<function_point>::mark_deleted (value_type &v) |
| 49 | { |
| 50 | v = function_point::deleted (); |
| 51 | } |
| 52 | template <> |
| 53 | inline void |
| 54 | pod_hash_traits<function_point>::mark_empty (value_type &v) |
| 55 | { |
| 56 | v = function_point::empty (); |
| 57 | } |
| 58 | template <> |
| 59 | inline bool |
| 60 | pod_hash_traits<function_point>::is_deleted (value_type v) |
| 61 | { |
| 62 | return v.get_kind () == PK_DELETED; |
| 63 | } |
| 64 | template <> |
| 65 | inline bool |
| 66 | pod_hash_traits<function_point>::is_empty (value_type v) |
| 67 | { |
| 68 | return v.get_kind () == PK_EMPTY; |
| 69 | } |
| 70 | |
| 71 | namespace ana { |
| 72 | |
| 73 | /* The result of analyzing which decls and SSA names can be purged from state at |
| 74 | different points in the program, so that we can simplify program_state |
| 75 | objects, in the hope of reducing state-blowup. */ |
| 76 | |
| 77 | class state_purge_map : public log_user |
| 78 | { |
| 79 | public: |
| 80 | typedef ordered_hash_map<tree, state_purge_per_ssa_name *> ssa_map_t; |
| 81 | typedef ssa_map_t::iterator ssa_iterator; |
| 82 | |
| 83 | typedef ordered_hash_map<tree, state_purge_per_decl *> decl_map_t; |
| 84 | typedef decl_map_t::iterator decl_iterator; |
| 85 | |
| 86 | state_purge_map (const supergraph &sg, |
| 87 | region_model_manager *mgr, |
| 88 | logger *logger); |
| 89 | ~state_purge_map (); |
| 90 | |
| 91 | const state_purge_per_ssa_name &get_data_for_ssa_name (tree name) const |
| 92 | { |
| 93 | gcc_assert (TREE_CODE (name) == SSA_NAME); |
| 94 | if (tree var = SSA_NAME_VAR (name)) |
| 95 | if (TREE_CODE (var) == VAR_DECL) |
| 96 | gcc_assert (!VAR_DECL_IS_VIRTUAL_OPERAND (var)); |
| 97 | |
| 98 | state_purge_per_ssa_name **slot |
| 99 | = const_cast <ssa_map_t&> (m_ssa_map).get (k: name); |
| 100 | return **slot; |
| 101 | } |
| 102 | |
| 103 | const state_purge_per_decl *get_any_data_for_decl (tree decl) const |
| 104 | { |
| 105 | gcc_assert (TREE_CODE (decl) == VAR_DECL |
| 106 | || TREE_CODE (decl) == PARM_DECL |
| 107 | || TREE_CODE (decl) == RESULT_DECL); |
| 108 | if (state_purge_per_decl **slot |
| 109 | = const_cast <decl_map_t&> (m_decl_map).get (k: decl)) |
| 110 | return *slot; |
| 111 | else |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | state_purge_per_decl & |
| 116 | get_or_create_data_for_decl (const function &fun, tree decl); |
| 117 | |
| 118 | const supergraph &get_sg () const { return m_sg; } |
| 119 | |
| 120 | ssa_iterator begin_ssas () const { return m_ssa_map.begin (); } |
| 121 | ssa_iterator end_ssas () const { return m_ssa_map.end (); } |
| 122 | |
| 123 | decl_iterator begin_decls () const { return m_decl_map.begin (); } |
| 124 | decl_iterator end_decls () const { return m_decl_map.end (); } |
| 125 | |
| 126 | private: |
| 127 | DISABLE_COPY_AND_ASSIGN (state_purge_map); |
| 128 | |
| 129 | const supergraph &m_sg; |
| 130 | ssa_map_t m_ssa_map; |
| 131 | decl_map_t m_decl_map; |
| 132 | }; |
| 133 | |
| 134 | /* Base class for state_purge_per_ssa_name and state_purge_per_decl. */ |
| 135 | |
| 136 | class state_purge_per_tree |
| 137 | { |
| 138 | public: |
| 139 | const function &get_function () const { return m_fun; } |
| 140 | tree get_fndecl () const { return m_fun.decl; } |
| 141 | |
| 142 | protected: |
| 143 | typedef hash_set<function_point> point_set_t; |
| 144 | |
| 145 | state_purge_per_tree (const function &fun) |
| 146 | : m_fun (fun) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | const function &m_fun; |
| 152 | }; |
| 153 | |
| 154 | /* The part of a state_purge_map relating to a specific SSA name. |
| 155 | |
| 156 | The result of analyzing a given SSA name, recording which |
| 157 | function_points need to retain state information about it to handle |
| 158 | their successor states, so that we can simplify program_state objects, |
| 159 | in the hope of reducing state-blowup. */ |
| 160 | |
| 161 | class state_purge_per_ssa_name : public state_purge_per_tree |
| 162 | { |
| 163 | public: |
| 164 | state_purge_per_ssa_name (const state_purge_map &map, |
| 165 | tree name, |
| 166 | const function &fun); |
| 167 | |
| 168 | bool needed_at_point_p (const function_point &point) const; |
| 169 | |
| 170 | private: |
| 171 | static function_point before_use_stmt (const state_purge_map &map, |
| 172 | const gimple *use_stmt); |
| 173 | |
| 174 | void add_to_worklist (const function_point &point, |
| 175 | auto_vec<function_point> *worklist, |
| 176 | logger *logger); |
| 177 | |
| 178 | void process_point (const function_point &point, |
| 179 | auto_vec<function_point> *worklist, |
| 180 | const state_purge_map &map); |
| 181 | |
| 182 | point_set_t m_points_needing_name; |
| 183 | tree m_name; |
| 184 | }; |
| 185 | |
| 186 | /* The part of a state_purge_map relating to a specific decl. |
| 187 | |
| 188 | Analogous to state_purge_per_ssa_name, but for local decls. |
| 189 | |
| 190 | This is more involved than the SSA name case, because we also need |
| 191 | to handle pointers and components. */ |
| 192 | |
| 193 | class state_purge_per_decl : public state_purge_per_tree |
| 194 | { |
| 195 | public: |
| 196 | state_purge_per_decl (const state_purge_map &map, |
| 197 | tree decl, |
| 198 | const function &fun); |
| 199 | |
| 200 | bool needed_at_point_p (const function_point &point) const; |
| 201 | |
| 202 | void add_needed_at (const function_point &point); |
| 203 | void add_pointed_to_at (const function_point &point); |
| 204 | void process_worklists (const state_purge_map &map, |
| 205 | region_model_manager *mgr); |
| 206 | |
| 207 | private: |
| 208 | static function_point before_use_stmt (const state_purge_map &map, |
| 209 | const gimple *use_stmt); |
| 210 | |
| 211 | void add_to_worklist (const function_point &point, |
| 212 | auto_vec<function_point> *worklist, |
| 213 | point_set_t *seen, |
| 214 | logger *logger); |
| 215 | |
| 216 | void process_point_backwards (const function_point &point, |
| 217 | auto_vec<function_point> *worklist, |
| 218 | point_set_t *seen, |
| 219 | const state_purge_map &map, |
| 220 | const region_model &model); |
| 221 | void process_point_forwards (const function_point &point, |
| 222 | auto_vec<function_point> *worklist, |
| 223 | point_set_t *seen, |
| 224 | const state_purge_map &map); |
| 225 | |
| 226 | point_set_t m_points_needing_decl; |
| 227 | point_set_t m_points_taking_address; |
| 228 | tree m_decl; |
| 229 | }; |
| 230 | |
| 231 | /* Subclass of dot_annotator for use by -fdump-analyzer-state-purge. |
| 232 | Annotate the .dot output with state-purge information. */ |
| 233 | |
| 234 | class state_purge_annotator : public dot_annotator |
| 235 | { |
| 236 | public: |
| 237 | state_purge_annotator (const state_purge_map *map) : m_map (map) {} |
| 238 | |
| 239 | bool add_node_annotations (graphviz_out *gv, const supernode &n, bool) |
| 240 | const final override; |
| 241 | |
| 242 | void add_stmt_annotations (graphviz_out *gv, const gimple *stmt, |
| 243 | bool within_row) |
| 244 | const final override; |
| 245 | |
| 246 | private: |
| 247 | void print_needed (graphviz_out *gv, |
| 248 | const function_point &point, |
| 249 | bool within_table) const; |
| 250 | |
| 251 | const state_purge_map *m_map; |
| 252 | }; |
| 253 | |
| 254 | } // namespace ana |
| 255 | |
| 256 | #endif /* GCC_ANALYZER_STATE_PURGE_H */ |
| 257 | |