IPA ICF: include hash values of references.
[official-gcc.git] / gcc / ipa-icf.h
blobcd21cacc94b62db7fce1ffbf3e88aa6246b90308
1 /* Interprocedural semantic function equality pass
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 namespace ipa_icf {
23 class sem_item;
25 /* Congruence class encompasses a collection of either functions or
26 read-only variables. These items are considered to be equivalent
27 if not proved the oposite. */
28 class congruence_class
30 public:
31 /* Congruence class constructor for a new class with _ID. */
32 congruence_class (unsigned int _id): in_worklist (false), id(_id)
36 /* Destructor. */
37 ~congruence_class ()
41 /* Dump function prints all class members to a FILE with an INDENT. */
42 void dump (FILE *file, unsigned int indent = 0) const;
44 /* Returns true if there's a member that is used from another group. */
45 bool is_class_used (void);
47 /* Flag is used in case we want to remove a class from worklist and
48 delete operation is quite expensive for
49 the data structure (linked list). */
50 bool in_worklist;
52 /* Vector of all group members. */
53 auto_vec <sem_item *> members;
55 /* Global unique class identifier. */
56 unsigned int id;
59 /* Semantic item type enum. */
60 enum sem_item_type
62 FUNC,
63 VAR
66 /* Class is container for address references for a symtab_node. */
68 class symbol_compare_collection
70 public:
71 /* Constructor. */
72 symbol_compare_collection (symtab_node *node);
74 /* Destructor. */
75 ~symbol_compare_collection ()
77 m_references.release ();
78 m_interposables.release ();
81 /* Vector of address references. */
82 vec<symtab_node *> m_references;
84 /* Vector of interposable references. */
85 vec<symtab_node *> m_interposables;
88 /* Hash traits for symbol_compare_collection map. */
90 struct symbol_compare_hashmap_traits: default_hashmap_traits
92 static hashval_t
93 hash (const symbol_compare_collection *v)
95 inchash::hash hstate;
96 hstate.add_int (v->m_references.length ());
98 for (unsigned i = 0; i < v->m_references.length (); i++)
99 hstate.add_ptr (v->m_references[i]->ultimate_alias_target ());
101 hstate.add_int (v->m_interposables.length ());
103 for (unsigned i = 0; i < v->m_interposables.length (); i++)
104 hstate.add_ptr (v->m_interposables[i]->ultimate_alias_target ());
106 return hstate.end ();
109 static bool
110 equal_keys (const symbol_compare_collection *a,
111 const symbol_compare_collection *b)
113 if (a->m_references.length () != b->m_references.length ()
114 || a->m_interposables.length () != b->m_interposables.length ())
115 return false;
117 for (unsigned i = 0; i < a->m_references.length (); i++)
118 if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1)
119 return false;
121 for (unsigned i = 0; i < a->m_interposables.length (); i++)
122 if (!a->m_interposables[i]->semantically_equivalent_p
123 (b->m_interposables[i]))
124 return false;
126 return true;
131 /* Semantic item usage pair. */
132 class sem_usage_pair
134 public:
135 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
136 sem_usage_pair (sem_item *_item, unsigned int _index);
138 /* Target semantic item where an item is used. */
139 sem_item *item;
141 /* Index of usage of such an item. */
142 unsigned int index;
145 /* Semantic item is a base class that encapsulates all shared functionality
146 for both semantic function and variable items. */
147 class sem_item
149 public:
150 /* Semantic item constructor for a node of _TYPE, where STACK is used
151 for bitmap memory allocation. */
152 sem_item (sem_item_type _type, bitmap_obstack *stack);
154 /* Semantic item constructor for a node of _TYPE, where STACK is used
155 for bitmap memory allocation. The item is based on symtab node _NODE
156 with computed _HASH. */
157 sem_item (sem_item_type _type, symtab_node *_node, hashval_t _hash,
158 bitmap_obstack *stack);
160 virtual ~sem_item ();
162 /* Dump function for debugging purpose. */
163 DEBUG_FUNCTION void dump (void);
165 /* Initialize semantic item by info reachable during LTO WPA phase. */
166 virtual void init_wpa (void) = 0;
168 /* Semantic item initialization function. */
169 virtual void init (void) = 0;
171 /* Add reference to a semantic TARGET. */
172 void add_reference (sem_item *target);
174 /* Fast equality function based on knowledge known in WPA. */
175 virtual bool equals_wpa (sem_item *item,
176 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
178 /* Returns true if the item equals to ITEM given as arguemnt. */
179 virtual bool equals (sem_item *item,
180 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
182 /* References independent hash function. */
183 virtual hashval_t get_hash (void) = 0;
185 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
186 be applied. */
187 virtual bool merge (sem_item *alias_item) = 0;
189 /* Dump symbol to FILE. */
190 virtual void dump_to_file (FILE *file) = 0;
192 /* Update hash by address sensitive references. */
193 void update_hash_by_addr_refs (hash_map <symtab_node *,
194 sem_item *> &m_symtab_node_map);
196 /* Update hash by computed local hash values taken from different
197 semantic items. */
198 void update_hash_by_local_refs (hash_map <symtab_node *,
199 sem_item *> &m_symtab_node_map);
201 /* Return base tree that can be used for compatible_types_p and
202 contains_polymorphic_type_p comparison. */
203 static bool get_base_types (tree *t1, tree *t2);
205 /* Return true if target supports alias symbols. */
206 bool target_supports_symbol_aliases_p (void);
208 /* Item type. */
209 sem_item_type type;
211 /* Symtab node. */
212 symtab_node *node;
214 /* Declaration tree node. */
215 tree decl;
217 /* Semantic references used that generate congruence groups. */
218 vec <sem_item *> refs;
220 /* Pointer to a congruence class the item belongs to. */
221 congruence_class *cls;
223 /* Index of the item in a class belonging to. */
224 unsigned int index_in_class;
226 /* List of semantic items where the instance is used. */
227 vec <sem_usage_pair *> usages;
229 /* A bitmap with indices of all classes referencing this item. */
230 bitmap usage_index_bitmap;
232 /* List of tree references (either FUNC_DECL or VAR_DECL). */
233 vec <tree> tree_refs;
235 /* A set with symbol table references. */
236 hash_set <symtab_node *> refs_set;
238 /* Hash of item. */
239 hashval_t hash;
241 /* Temporary hash used where hash values of references are added. */
242 hashval_t global_hash;
243 protected:
244 /* Cached, once calculated hash for the item. */
246 /* Accumulate to HSTATE a hash of constructor expression EXP. */
247 static void add_expr (const_tree exp, inchash::hash &hstate);
249 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
250 point to a same function. Comparison can be skipped if IGNORED_NODES
251 contains these nodes. ADDRESS indicate if address is taken. */
252 bool compare_cgraph_references (hash_map <symtab_node *, sem_item *>
253 &ignored_nodes,
254 symtab_node *n1, symtab_node *n2,
255 bool address);
257 private:
258 /* Initialize internal data structures. Bitmap STACK is used for
259 bitmap memory allocation process. */
260 void setup (bitmap_obstack *stack);
261 }; // class sem_item
263 class sem_function: public sem_item
265 public:
266 /* Semantic function constructor that uses STACK as bitmap memory stack. */
267 sem_function (bitmap_obstack *stack);
269 /* Constructor based on callgraph node _NODE with computed hash _HASH.
270 Bitmap STACK is used for memory allocation. */
271 sem_function (cgraph_node *_node, hashval_t _hash, bitmap_obstack *stack);
273 ~sem_function ();
275 inline virtual void init_wpa (void)
277 parse_tree_args ();
280 virtual void init (void);
281 virtual bool equals_wpa (sem_item *item,
282 hash_map <symtab_node *, sem_item *> &ignored_nodes);
283 virtual hashval_t get_hash (void);
284 virtual bool equals (sem_item *item,
285 hash_map <symtab_node *, sem_item *> &ignored_nodes);
286 virtual bool merge (sem_item *alias_item);
288 /* Dump symbol to FILE. */
289 virtual void dump_to_file (FILE *file)
291 gcc_assert (file);
292 dump_function_to_file (decl, file, TDF_DETAILS);
295 /* Parses function arguments and result type. */
296 void parse_tree_args (void);
298 /* Returns cgraph_node. */
299 inline cgraph_node *get_node (void)
301 return dyn_cast <cgraph_node *> (node);
304 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
305 void hash_stmt (gimple stmt, inchash::hash &inchash);
307 /* Return true if polymorphic comparison must be processed. */
308 bool compare_polymorphic_p (void);
310 /* For a given call graph NODE, the function constructs new
311 semantic function item. */
312 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
314 /* Exception handling region tree. */
315 eh_region region_tree;
317 /* Result type tree node. */
318 tree result_type;
320 /* Array of argument tree types. */
321 vec <tree> arg_types;
323 /* Number of function arguments. */
324 unsigned int arg_count;
326 /* Total amount of edges in the function. */
327 unsigned int edge_count;
329 /* Vector of sizes of all basic blocks. */
330 vec <unsigned int> bb_sizes;
332 /* Control flow graph checksum. */
333 hashval_t cfg_checksum;
335 /* GIMPLE codes hash value. */
336 hashval_t gcode_hash;
338 /* Total number of SSA names used in the function. */
339 unsigned ssa_names_size;
341 /* Array of structures for all basic blocks. */
342 vec <ipa_icf_gimple::sem_bb *> bb_sorted;
344 private:
345 /* Calculates hash value based on a BASIC_BLOCK. */
346 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
348 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
349 true value is returned if phi nodes are semantically
350 equivalent in these blocks . */
351 bool compare_phi_node (basic_block bb1, basic_block bb2);
353 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
354 corresponds to TARGET. */
355 bool bb_dict_test (vec<int> *bb_dict, int source, int target);
357 /* If cgraph edges E1 and E2 are indirect calls, verify that
358 ICF flags are the same. */
359 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
361 /* Processes function equality comparison. */
362 bool equals_private (sem_item *item,
363 hash_map <symtab_node *, sem_item *> &ignored_nodes);
365 /* Returns true if tree T can be compared as a handled component. */
366 static bool icf_handled_component_p (tree t);
368 /* Function checker stores binding between functions. */
369 ipa_icf_gimple::func_checker *m_checker;
371 /* COMPARED_FUNC is a function that we compare to. */
372 sem_function *m_compared_func;
373 }; // class sem_function
375 class sem_variable: public sem_item
377 public:
378 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
379 sem_variable (bitmap_obstack *stack);
381 /* Constructor based on callgraph node _NODE with computed hash _HASH.
382 Bitmap STACK is used for memory allocation. */
384 sem_variable (varpool_node *_node, hashval_t _hash, bitmap_obstack *stack);
386 inline virtual void init_wpa (void) {}
388 /* Semantic variable initialization function. */
389 inline virtual void init (void)
391 decl = get_node ()->decl;
394 virtual hashval_t get_hash (void);
395 virtual bool merge (sem_item *alias_item);
396 virtual void dump_to_file (FILE *file);
397 virtual bool equals (sem_item *item,
398 hash_map <symtab_node *, sem_item *> &ignored_nodes);
400 /* Fast equality variable based on knowledge known in WPA. */
401 virtual bool equals_wpa (sem_item *item,
402 hash_map <symtab_node *, sem_item *> &ignored_nodes);
404 /* Returns varpool_node. */
405 inline varpool_node *get_node (void)
407 return dyn_cast <varpool_node *> (node);
410 /* Parser function that visits a varpool NODE. */
411 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
413 private:
414 /* Compares trees T1 and T2 for semantic equality. */
415 static bool equals (tree t1, tree t2);
416 }; // class sem_variable
418 class sem_item_optimizer;
420 struct congruence_class_group
422 hashval_t hash;
423 sem_item_type type;
424 vec <congruence_class *> classes;
427 /* Congruence class set structure. */
428 struct congruence_class_group_hash: typed_noop_remove <congruence_class_group>
430 typedef congruence_class_group value_type;
431 typedef congruence_class_group compare_type;
433 static inline hashval_t hash (const value_type *item)
435 return item->hash;
438 static inline int equal (const value_type *item1, const compare_type *item2)
440 return item1->hash == item2->hash && item1->type == item2->type;
444 struct traverse_split_pair
446 sem_item_optimizer *optimizer;
447 class congruence_class *cls;
450 /* Semantic item optimizer includes all top-level logic
451 related to semantic equality comparison. */
452 class sem_item_optimizer
454 public:
455 sem_item_optimizer ();
456 ~sem_item_optimizer ();
458 /* Function responsible for visiting all potential functions and
459 read-only variables that can be merged. */
460 void parse_funcs_and_vars (void);
462 /* Optimizer entry point which returns true in case it processes
463 a merge operation. True is returned if there's a merge operation
464 processed. */
465 bool execute (void);
467 /* Dump function. */
468 void dump (void);
470 /* Verify congruence classes if checking is enabled. */
471 void verify_classes (void);
473 /* Write IPA ICF summary for symbols. */
474 void write_summary (void);
476 /* Read IPA IPA ICF summary for symbols. */
477 void read_summary (void);
479 /* Callgraph removal hook called for a NODE with a custom DATA. */
480 static void cgraph_removal_hook (cgraph_node *node, void *data);
482 /* Varpool removal hook called for a NODE with a custom DATA. */
483 static void varpool_removal_hook (varpool_node *node, void *data);
485 /* Worklist of congruence classes that can potentially
486 refine classes of congruence. */
487 std::list<congruence_class *> worklist;
489 /* Remove semantic ITEM and release memory. */
490 void remove_item (sem_item *item);
492 /* Remove symtab NODE triggered by symtab removal hooks. */
493 void remove_symtab_node (symtab_node *node);
495 /* Register callgraph and varpool hooks. */
496 void register_hooks (void);
498 /* Unregister callgraph and varpool hooks. */
499 void unregister_hooks (void);
501 /* Adds a CLS to hashtable associated by hash value. */
502 void add_class (congruence_class *cls);
504 /* Gets a congruence class group based on given HASH value and TYPE. */
505 congruence_class_group *get_group_by_hash (hashval_t hash,
506 sem_item_type type);
508 private:
510 /* For each semantic item, append hash values of references. */
511 void update_hash_by_addr_refs ();
513 /* Congruence classes are built by hash value. */
514 void build_hash_based_classes (void);
516 /* Semantic items in classes having more than one element and initialized.
517 In case of WPA, we load function body. */
518 void parse_nonsingleton_classes (void);
520 /* Equality function for semantic items is used to subdivide existing
521 classes. If IN_WPA, fast equality function is invoked. */
522 void subdivide_classes_by_equality (bool in_wpa = false);
524 /* Subdivide classes by address and interposable references
525 that members of the class reference.
526 Example can be a pair of functions that have an address
527 taken from a function. If these addresses are different the class
528 is split. */
529 unsigned subdivide_classes_by_sensitive_refs();
531 /* Debug function prints all informations about congruence classes. */
532 void dump_cong_classes (void);
534 /* Build references according to call graph. */
535 void build_graph (void);
537 /* Iterative congruence reduction function. */
538 void process_cong_reduction (void);
540 /* After reduction is done, we can declare all items in a group
541 to be equal. PREV_CLASS_COUNT is start number of classes
542 before reduction. True is returned if there's a merge operation
543 processed. */
544 bool merge_classes (unsigned int prev_class_count);
546 /* Adds a newly created congruence class CLS to worklist. */
547 void worklist_push (congruence_class *cls);
549 /* Pops a class from worklist. */
550 congruence_class *worklist_pop ();
552 /* Every usage of a congruence class CLS is a candidate that can split the
553 collection of classes. Bitmap stack BMSTACK is used for bitmap
554 allocation. */
555 void do_congruence_step (congruence_class *cls);
557 /* Tests if a class CLS used as INDEXth splits any congruence classes.
558 Bitmap stack BMSTACK is used for bitmap allocation. */
559 void do_congruence_step_for_index (congruence_class *cls, unsigned int index);
561 /* Makes pairing between a congruence class CLS and semantic ITEM. */
562 static void add_item_to_class (congruence_class *cls, sem_item *item);
564 /* Disposes split map traverse function. CLS is congruence
565 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
566 but unused argument. */
567 static bool release_split_map (congruence_class * const &cls, bitmap const &b,
568 traverse_split_pair *pair);
570 /* Process split operation for a cognruence class CLS,
571 where bitmap B splits congruence class members. DATA is used
572 as argument of split pair. */
573 static bool traverse_congruence_split (congruence_class * const &cls,
574 bitmap const &b,
575 traverse_split_pair *pair);
577 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
578 contains LEN bytes. */
579 void read_section (lto_file_decl_data *file_data, const char *data,
580 size_t len);
582 /* Removes all callgraph and varpool nodes that are marked by symtab
583 as deleted. */
584 void filter_removed_items (void);
586 /* Vector of semantic items. */
587 vec <sem_item *> m_items;
589 /* A set containing all items removed by hooks. */
590 hash_set <symtab_node *> m_removed_items_set;
592 /* Hashtable of congruence classes */
593 hash_table <congruence_class_group_hash> m_classes;
595 /* Count of congruence classes. */
596 unsigned int m_classes_count;
598 /* Map data structure maps symtab nodes to semantic items. */
599 hash_map <symtab_node *, sem_item *> m_symtab_node_map;
601 /* Set to true if a splitter class is removed. */
602 bool splitter_class_removed;
604 /* Global unique class id counter. */
605 static unsigned int class_id;
607 /* Callgraph node removal hook holder. */
608 cgraph_node_hook_list *m_cgraph_node_hooks;
610 /* Varpool node removal hook holder. */
611 varpool_node_hook_list *m_varpool_node_hooks;
613 /* Bitmap stack. */
614 bitmap_obstack m_bmstack;
615 }; // class sem_item_optimizer
617 } // ipa_icf namespace