Define std::to_array for Debug Mode
[official-gcc.git] / gcc / ipa-icf.h
blob2bf0f156ef631b4beeba897f5d6dc7dc2c778fe4
1 /* Interprocedural semantic function equality pass
2 Copyright (C) 2014-2019 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),
33 referenced_by_count (0)
37 /* Destructor. */
38 ~congruence_class ()
42 /* Dump function prints all class members to a FILE with an INDENT. */
43 void dump (FILE *file, unsigned int indent = 0) const;
45 /* Returns true if there's a member that is used from another group. */
46 bool is_class_used (void);
48 /* Flag is used in case we want to remove a class from worklist and
49 delete operation is quite expensive for
50 the data structure (linked list). */
51 bool in_worklist;
53 /* Vector of all group members. */
54 auto_vec <sem_item *> members;
56 /* Global unique class identifier. */
57 unsigned int id;
59 /* Total number of references to items of this class. */
60 unsigned referenced_by_count;
63 /* Semantic item type enum. */
64 enum sem_item_type
66 FUNC,
67 VAR
70 /* Class is container for address references for a symtab_node. */
72 class symbol_compare_collection
74 public:
75 /* Constructor. */
76 symbol_compare_collection (symtab_node *node);
78 /* Destructor. */
79 ~symbol_compare_collection ()
81 m_references.release ();
82 m_interposables.release ();
85 /* Vector of address references. */
86 vec<symtab_node *> m_references;
88 /* Vector of interposable references. */
89 vec<symtab_node *> m_interposables;
92 /* Hash traits for symbol_compare_collection map. */
94 struct symbol_compare_hash : nofree_ptr_hash <symbol_compare_collection>
96 static hashval_t
97 hash (value_type v)
99 inchash::hash hstate;
100 hstate.add_int (v->m_references.length ());
102 for (unsigned i = 0; i < v->m_references.length (); i++)
103 hstate.add_int (v->m_references[i]->ultimate_alias_target ()->order);
105 hstate.add_int (v->m_interposables.length ());
107 for (unsigned i = 0; i < v->m_interposables.length (); i++)
108 hstate.add_int (v->m_interposables[i]->ultimate_alias_target ()->order);
110 return hstate.end ();
113 static bool
114 equal (value_type a, value_type b)
116 if (a->m_references.length () != b->m_references.length ()
117 || a->m_interposables.length () != b->m_interposables.length ())
118 return false;
120 for (unsigned i = 0; i < a->m_references.length (); i++)
121 if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1)
122 return false;
124 for (unsigned i = 0; i < a->m_interposables.length (); i++)
125 if (!a->m_interposables[i]->semantically_equivalent_p
126 (b->m_interposables[i]))
127 return false;
129 return true;
133 /* Semantic item usage pair. */
134 class sem_usage_pair
136 public:
137 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
138 sem_usage_pair (sem_item *_item, unsigned int _index);
140 /* Target semantic item where an item is used. */
141 sem_item *item;
143 /* Index of usage of such an item. */
144 unsigned int index;
147 struct sem_usage_pair_hash : pointer_hash <sem_usage_pair>
149 static inline hashval_t hash (sem_usage_pair *);
150 static inline bool equal (sem_usage_pair *, sem_usage_pair *);
153 inline hashval_t
154 sem_usage_pair_hash::hash (sem_usage_pair *pair)
156 inchash::hash hstate;
158 hstate.add_ptr (pair->item);
159 hstate.add_int (pair->index);
161 return hstate.end ();
164 inline bool
165 sem_usage_pair_hash::equal (sem_usage_pair *p1, sem_usage_pair *p2)
167 return p1->item == p2->item && p1->index == p2->index;
170 struct sem_usage_hash : sem_usage_pair_hash, typed_delete_remove <sem_usage_pair> {};
171 typedef hash_map<sem_usage_hash, auto_vec<sem_item *> > ref_map;
173 typedef std::pair<symtab_node *, symtab_node *> symtab_pair;
175 /* Semantic item is a base class that encapsulates all shared functionality
176 for both semantic function and variable items. */
177 class sem_item
179 public:
180 /* Semantic item constructor for a node of _TYPE, where STACK is used
181 for bitmap memory allocation. */
182 sem_item (sem_item_type _type, bitmap_obstack *stack);
184 /* Semantic item constructor for a node of _TYPE, where STACK is used
185 for bitmap memory allocation. The item is based on symtab node _NODE. */
186 sem_item (sem_item_type _type, symtab_node *_node, bitmap_obstack *stack);
188 virtual ~sem_item ();
190 /* Dump function for debugging purpose. */
191 DEBUG_FUNCTION void dump (void);
193 /* Semantic item initialization function. */
194 virtual void init (void) = 0;
196 /* Add reference to a semantic TARGET. */
197 void add_reference (ref_map *map, sem_item *target);
199 /* Fast equality function based on knowledge known in WPA. */
200 virtual bool equals_wpa (sem_item *item,
201 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
203 /* Returns true if the item equals to ITEM given as arguemnt. */
204 virtual bool equals (sem_item *item,
205 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
207 /* References independent hash function. */
208 virtual hashval_t get_hash (void) = 0;
210 /* Set new hash value of the item. */
211 void set_hash (hashval_t hash);
213 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
214 be applied. */
215 virtual bool merge (sem_item *alias_item) = 0;
217 /* Dump symbol to FILE. */
218 virtual void dump_to_file (FILE *file) = 0;
220 /* Update hash by address sensitive references. */
221 void update_hash_by_addr_refs (hash_map <symtab_node *,
222 sem_item *> &m_symtab_node_map);
224 /* Update hash by computed local hash values taken from different
225 semantic items. */
226 void update_hash_by_local_refs (hash_map <symtab_node *,
227 sem_item *> &m_symtab_node_map);
229 /* Return base tree that can be used for compatible_types_p and
230 contains_polymorphic_type_p comparison. */
231 static bool get_base_types (tree *t1, tree *t2);
233 /* Return true if target supports alias symbols. */
234 bool target_supports_symbol_aliases_p (void);
236 /* Item type. */
237 sem_item_type type;
239 /* Symtab node. */
240 symtab_node *node;
242 /* Declaration tree node. */
243 tree decl;
245 /* Number of references to a semantic symbols (function calls,
246 variable references). */
247 unsigned reference_count;
249 /* Pointer to a congruence class the item belongs to. */
250 congruence_class *cls;
252 /* Index of the item in a class belonging to. */
253 unsigned int index_in_class;
255 /* A bitmap with indices of all classes referencing this item. */
256 bitmap usage_index_bitmap;
258 /* List of tree references (either FUNC_DECL or VAR_DECL). */
259 vec <tree> tree_refs;
261 /* A set with symbol table references. */
262 hash_set <symtab_node *> refs_set;
264 /* Temporary hash used where hash values of references are added. */
265 hashval_t global_hash;
267 /* Number of references to this symbol. */
268 unsigned referenced_by_count;
269 protected:
270 /* Cached, once calculated hash for the item. */
272 /* Accumulate to HSTATE a hash of expression EXP. */
273 static void add_expr (const_tree exp, inchash::hash &hstate);
274 /* Accumulate to HSTATE a hash of type T. */
275 static void add_type (const_tree t, inchash::hash &hstate);
277 /* Compare properties of symbol that does not affect semantics of symbol
278 itself but affects semantics of its references.
279 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR. */
280 static bool compare_referenced_symbol_properties (symtab_node *used_by,
281 symtab_node *n1,
282 symtab_node *n2,
283 bool address);
285 /* Hash properties compared by compare_referenced_symbol_properties. */
286 void hash_referenced_symbol_properties (symtab_node *ref,
287 inchash::hash &hstate,
288 bool address);
290 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
291 point to a same function. Comparison can be skipped if IGNORED_NODES
292 contains these nodes. ADDRESS indicate if address is taken. */
293 bool compare_symbol_references (hash_map <symtab_node *, sem_item *>
294 &ignored_nodes,
295 symtab_node *n1, symtab_node *n2,
296 bool address);
297 protected:
298 /* Hash of item. */
299 hashval_t m_hash;
301 /* Indicated whether a hash value has been set or not. */
302 bool m_hash_set;
304 private:
305 /* Initialize internal data structures. Bitmap STACK is used for
306 bitmap memory allocation process. */
307 void setup (bitmap_obstack *stack);
309 /* Because types can be arbitrarily large, avoid quadratic bottleneck. */
310 static hash_map<const_tree, hashval_t> m_type_hash_cache;
311 }; // class sem_item
313 class sem_function: public sem_item
315 public:
316 /* Semantic function constructor that uses STACK as bitmap memory stack. */
317 sem_function (bitmap_obstack *stack);
319 /* Constructor based on callgraph node _NODE.
320 Bitmap STACK is used for memory allocation. */
321 sem_function (cgraph_node *_node, bitmap_obstack *stack);
323 ~sem_function ();
325 virtual void init (void);
326 virtual bool equals_wpa (sem_item *item,
327 hash_map <symtab_node *, sem_item *> &ignored_nodes);
328 virtual hashval_t get_hash (void);
329 virtual bool equals (sem_item *item,
330 hash_map <symtab_node *, sem_item *> &ignored_nodes);
331 virtual bool merge (sem_item *alias_item);
333 /* Dump symbol to FILE. */
334 virtual void dump_to_file (FILE *file)
336 gcc_assert (file);
337 dump_function_to_file (decl, file, TDF_DETAILS);
340 /* Returns cgraph_node. */
341 inline cgraph_node *get_node (void)
343 return dyn_cast <cgraph_node *> (node);
346 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
347 void hash_stmt (gimple *stmt, inchash::hash &inchash);
349 /* Return true if polymorphic comparison must be processed. */
350 bool compare_polymorphic_p (void);
352 /* For a given call graph NODE, the function constructs new
353 semantic function item. */
354 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
356 /* Perform additional checks needed to match types of used function
357 paramters. */
358 bool compatible_parm_types_p (tree, tree);
360 /* Exception handling region tree. */
361 eh_region region_tree;
363 /* Number of function arguments. */
364 unsigned int arg_count;
366 /* Total amount of edges in the function. */
367 unsigned int edge_count;
369 /* Vector of sizes of all basic blocks. */
370 vec <unsigned int> bb_sizes;
372 /* Control flow graph checksum. */
373 hashval_t cfg_checksum;
375 /* GIMPLE codes hash value. */
376 hashval_t gcode_hash;
378 /* Total number of SSA names used in the function. */
379 unsigned ssa_names_size;
381 /* Array of structures for all basic blocks. */
382 vec <ipa_icf_gimple::sem_bb *> bb_sorted;
384 /* Return true if parameter I may be used. */
385 bool param_used_p (unsigned int i);
387 private:
388 /* Calculates hash value based on a BASIC_BLOCK. */
389 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
391 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
392 true value is returned if phi nodes are semantically
393 equivalent in these blocks . */
394 bool compare_phi_node (basic_block bb1, basic_block bb2);
396 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
397 corresponds to TARGET. */
398 bool bb_dict_test (vec<int> *bb_dict, int source, int target);
400 /* If cgraph edges E1 and E2 are indirect calls, verify that
401 ICF flags are the same. */
402 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
404 /* Processes function equality comparison. */
405 bool equals_private (sem_item *item);
407 /* Returns true if tree T can be compared as a handled component. */
408 static bool icf_handled_component_p (tree t);
410 /* Function checker stores binding between functions. */
411 ipa_icf_gimple::func_checker *m_checker;
413 /* COMPARED_FUNC is a function that we compare to. */
414 sem_function *m_compared_func;
415 }; // class sem_function
417 class sem_variable: public sem_item
419 public:
420 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
421 sem_variable (bitmap_obstack *stack);
423 /* Constructor based on callgraph node _NODE.
424 Bitmap STACK is used for memory allocation. */
426 sem_variable (varpool_node *_node, bitmap_obstack *stack);
428 /* Semantic variable initialization function. */
429 inline virtual void init (void)
431 decl = get_node ()->decl;
434 virtual hashval_t get_hash (void);
435 virtual bool merge (sem_item *alias_item);
436 virtual void dump_to_file (FILE *file);
437 virtual bool equals (sem_item *item,
438 hash_map <symtab_node *, sem_item *> &ignored_nodes);
440 /* Fast equality variable based on knowledge known in WPA. */
441 virtual bool equals_wpa (sem_item *item,
442 hash_map <symtab_node *, sem_item *> &ignored_nodes);
444 /* Returns varpool_node. */
445 inline varpool_node *get_node (void)
447 return dyn_cast <varpool_node *> (node);
450 /* Parser function that visits a varpool NODE. */
451 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
453 private:
454 /* Compares trees T1 and T2 for semantic equality. */
455 static bool equals (tree t1, tree t2);
456 }; // class sem_variable
458 class sem_item_optimizer;
460 struct congruence_class_group
462 hashval_t hash;
463 sem_item_type type;
464 vec <congruence_class *> classes;
467 /* Congruence class set structure. */
468 struct congruence_class_hash : nofree_ptr_hash <congruence_class_group>
470 static inline hashval_t hash (const congruence_class_group *item)
472 return item->hash;
475 static inline int equal (const congruence_class_group *item1,
476 const congruence_class_group *item2)
478 return item1->hash == item2->hash && item1->type == item2->type;
482 struct traverse_split_pair
484 sem_item_optimizer *optimizer;
485 class congruence_class *cls;
488 /* Semantic item optimizer includes all top-level logic
489 related to semantic equality comparison. */
490 class sem_item_optimizer
492 public:
493 sem_item_optimizer ();
494 ~sem_item_optimizer ();
496 /* Function responsible for visiting all potential functions and
497 read-only variables that can be merged. */
498 void parse_funcs_and_vars (void);
500 /* Optimizer entry point which returns true in case it processes
501 a merge operation. True is returned if there's a merge operation
502 processed. */
503 bool execute (void);
505 /* Dump function. */
506 void dump (void);
508 /* Verify congruence classes if checking is enabled. */
509 void checking_verify_classes (void);
511 /* Verify congruence classes. */
512 void verify_classes (void);
514 /* Write IPA ICF summary for symbols. */
515 void write_summary (void);
517 /* Read IPA ICF summary for symbols. */
518 void read_summary (void);
520 /* Callgraph removal hook called for a NODE with a custom DATA. */
521 static void cgraph_removal_hook (cgraph_node *node, void *data);
523 /* Varpool removal hook called for a NODE with a custom DATA. */
524 static void varpool_removal_hook (varpool_node *node, void *data);
526 /* Worklist of congruence classes that can potentially
527 refine classes of congruence. */
528 fibonacci_heap<unsigned, congruence_class> worklist;
530 /* Remove semantic ITEM and release memory. */
531 void remove_item (sem_item *item);
533 /* Remove symtab NODE triggered by symtab removal hooks. */
534 void remove_symtab_node (symtab_node *node);
536 /* Register callgraph and varpool hooks. */
537 void register_hooks (void);
539 /* Unregister callgraph and varpool hooks. */
540 void unregister_hooks (void);
542 /* Adds a CLS to hashtable associated by hash value. */
543 void add_class (congruence_class *cls);
545 /* Gets a congruence class group based on given HASH value and TYPE. */
546 congruence_class_group *get_group_by_hash (hashval_t hash,
547 sem_item_type type);
548 private:
550 /* For each semantic item, append hash values of references. */
551 void update_hash_by_addr_refs ();
553 /* Congruence classes are built by hash value. */
554 void build_hash_based_classes (void);
556 /* Semantic items in classes having more than one element and initialized.
557 In case of WPA, we load function body. */
558 void parse_nonsingleton_classes (void);
560 /* Equality function for semantic items is used to subdivide existing
561 classes. If IN_WPA, fast equality function is invoked. */
562 void subdivide_classes_by_equality (bool in_wpa = false);
564 /* Subdivide classes by address and interposable references
565 that members of the class reference.
566 Example can be a pair of functions that have an address
567 taken from a function. If these addresses are different the class
568 is split. */
569 unsigned subdivide_classes_by_sensitive_refs();
571 /* Debug function prints all informations about congruence classes. */
572 void dump_cong_classes (void);
574 /* Build references according to call graph. */
575 void build_graph (void);
577 /* Iterative congruence reduction function. */
578 void process_cong_reduction (void);
580 /* After reduction is done, we can declare all items in a group
581 to be equal. PREV_CLASS_COUNT is start number of classes
582 before reduction. True is returned if there's a merge operation
583 processed. */
584 bool merge_classes (unsigned int prev_class_count);
586 /* Fixup points to analysis info. */
587 void fixup_points_to_sets (void);
589 /* Fixup points to set PT. */
590 void fixup_pt_set (struct pt_solution *pt);
592 /* Adds a newly created congruence class CLS to worklist. */
593 void worklist_push (congruence_class *cls);
595 /* Pops a class from worklist. */
596 congruence_class *worklist_pop ();
598 /* Every usage of a congruence class CLS is a candidate that can split the
599 collection of classes. Bitmap stack BMSTACK is used for bitmap
600 allocation. */
601 void do_congruence_step (congruence_class *cls);
603 /* Tests if a class CLS used as INDEXth splits any congruence classes.
604 Bitmap stack BMSTACK is used for bitmap allocation. */
605 bool do_congruence_step_for_index (congruence_class *cls, unsigned int index);
607 /* Makes pairing between a congruence class CLS and semantic ITEM. */
608 static void add_item_to_class (congruence_class *cls, sem_item *item);
610 /* Disposes split map traverse function. CLS is congruence
611 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
612 but unused argument. */
613 static bool release_split_map (congruence_class * const &cls, bitmap const &b,
614 traverse_split_pair *pair);
616 /* Process split operation for a cognruence class CLS,
617 where bitmap B splits congruence class members. DATA is used
618 as argument of split pair. */
619 static bool traverse_congruence_split (congruence_class * const &cls,
620 bitmap const &b,
621 traverse_split_pair *pair);
623 /* Compare function for sorting pairs in do_congruence_step_f. */
624 static int sort_congruence_split (const void *, const void *);
626 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
627 contains LEN bytes. */
628 void read_section (lto_file_decl_data *file_data, const char *data,
629 size_t len);
631 /* Removes all callgraph and varpool nodes that are marked by symtab
632 as deleted. */
633 void filter_removed_items (void);
635 /* Vector of semantic items. */
636 vec <sem_item *> m_items;
638 /* A set containing all items removed by hooks. */
639 hash_set <symtab_node *> m_removed_items_set;
641 /* Hashtable of congruence classes. */
642 hash_table <congruence_class_hash> m_classes;
644 /* Count of congruence classes. */
645 unsigned int m_classes_count;
647 /* Map data structure maps symtab nodes to semantic items. */
648 hash_map <symtab_node *, sem_item *> m_symtab_node_map;
650 /* Set to true if a splitter class is removed. */
651 bool splitter_class_removed;
653 /* Global unique class id counter. */
654 static unsigned int class_id;
656 /* Callgraph node removal hook holder. */
657 cgraph_node_hook_list *m_cgraph_node_hooks;
659 /* Varpool node removal hook holder. */
660 varpool_node_hook_list *m_varpool_node_hooks;
662 /* Bitmap stack. */
663 bitmap_obstack m_bmstack;
665 /* Vector of merged variables. Needed for fixup of points-to-analysis
666 info. */
667 vec <symtab_pair> m_merged_variables;
669 /* Hash map will all references. */
670 ref_map m_references;
671 }; // class sem_item_optimizer
673 } // ipa_icf namespace