PR c++/87582
[official-gcc.git] / gcc / ipa-icf.h
bloba64b3852efb63c348e433a32db8eae150343abb9
1 /* Interprocedural semantic function equality pass
2 Copyright (C) 2014-2018 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_hash : nofree_ptr_hash <symbol_compare_collection>
92 static hashval_t
93 hash (value_type 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_int (v->m_references[i]->ultimate_alias_target ()->order);
101 hstate.add_int (v->m_interposables.length ());
103 for (unsigned i = 0; i < v->m_interposables.length (); i++)
104 hstate.add_int (v->m_interposables[i]->ultimate_alias_target ()->order);
106 return hstate.end ();
109 static bool
110 equal (value_type a, value_type b)
112 if (a->m_references.length () != b->m_references.length ()
113 || a->m_interposables.length () != b->m_interposables.length ())
114 return false;
116 for (unsigned i = 0; i < a->m_references.length (); i++)
117 if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1)
118 return false;
120 for (unsigned i = 0; i < a->m_interposables.length (); i++)
121 if (!a->m_interposables[i]->semantically_equivalent_p
122 (b->m_interposables[i]))
123 return false;
125 return true;
130 /* Semantic item usage pair. */
131 class sem_usage_pair
133 public:
134 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
135 sem_usage_pair (sem_item *_item, unsigned int _index);
137 /* Target semantic item where an item is used. */
138 sem_item *item;
140 /* Index of usage of such an item. */
141 unsigned int index;
144 typedef std::pair<symtab_node *, symtab_node *> symtab_pair;
146 /* Semantic item is a base class that encapsulates all shared functionality
147 for both semantic function and variable items. */
148 class sem_item
150 public:
151 /* Semantic item constructor for a node of _TYPE, where STACK is used
152 for bitmap memory allocation. */
153 sem_item (sem_item_type _type, bitmap_obstack *stack);
155 /* Semantic item constructor for a node of _TYPE, where STACK is used
156 for bitmap memory allocation. The item is based on symtab node _NODE. */
157 sem_item (sem_item_type _type, symtab_node *_node, bitmap_obstack *stack);
159 virtual ~sem_item ();
161 /* Dump function for debugging purpose. */
162 DEBUG_FUNCTION void dump (void);
164 /* Initialize semantic item by info reachable during LTO WPA phase. */
165 virtual void init_wpa (void) = 0;
167 /* Semantic item initialization function. */
168 virtual void init (void) = 0;
170 /* Add reference to a semantic TARGET. */
171 void add_reference (sem_item *target);
173 /* Fast equality function based on knowledge known in WPA. */
174 virtual bool equals_wpa (sem_item *item,
175 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
177 /* Returns true if the item equals to ITEM given as arguemnt. */
178 virtual bool equals (sem_item *item,
179 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
181 /* References independent hash function. */
182 virtual hashval_t get_hash (void) = 0;
184 /* Set new hash value of the item. */
185 void set_hash (hashval_t hash);
187 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
188 be applied. */
189 virtual bool merge (sem_item *alias_item) = 0;
191 /* Dump symbol to FILE. */
192 virtual void dump_to_file (FILE *file) = 0;
194 /* Update hash by address sensitive references. */
195 void update_hash_by_addr_refs (hash_map <symtab_node *,
196 sem_item *> &m_symtab_node_map);
198 /* Update hash by computed local hash values taken from different
199 semantic items. */
200 void update_hash_by_local_refs (hash_map <symtab_node *,
201 sem_item *> &m_symtab_node_map);
203 /* Return base tree that can be used for compatible_types_p and
204 contains_polymorphic_type_p comparison. */
205 static bool get_base_types (tree *t1, tree *t2);
207 /* Return true if target supports alias symbols. */
208 bool target_supports_symbol_aliases_p (void);
210 /* Item type. */
211 sem_item_type type;
213 /* Symtab node. */
214 symtab_node *node;
216 /* Declaration tree node. */
217 tree decl;
219 /* Semantic references used that generate congruence groups. */
220 vec <sem_item *> refs;
222 /* Pointer to a congruence class the item belongs to. */
223 congruence_class *cls;
225 /* Index of the item in a class belonging to. */
226 unsigned int index_in_class;
228 /* List of semantic items where the instance is used. */
229 vec <sem_usage_pair *> usages;
231 /* A bitmap with indices of all classes referencing this item. */
232 bitmap usage_index_bitmap;
234 /* List of tree references (either FUNC_DECL or VAR_DECL). */
235 vec <tree> tree_refs;
237 /* A set with symbol table references. */
238 hash_set <symtab_node *> refs_set;
240 /* Temporary hash used where hash values of references are added. */
241 hashval_t global_hash;
242 protected:
243 /* Cached, once calculated hash for the item. */
245 /* Accumulate to HSTATE a hash of expression EXP. */
246 static void add_expr (const_tree exp, inchash::hash &hstate);
247 /* Accumulate to HSTATE a hash of type T. */
248 static void add_type (const_tree t, inchash::hash &hstate);
250 /* Compare properties of symbol that does not affect semantics of symbol
251 itself but affects semantics of its references.
252 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR. */
253 static bool compare_referenced_symbol_properties (symtab_node *used_by,
254 symtab_node *n1,
255 symtab_node *n2,
256 bool address);
258 /* Compare two attribute lists. */
259 static bool compare_attributes (const_tree list1, const_tree list2);
261 /* Hash properties compared by compare_referenced_symbol_properties. */
262 void hash_referenced_symbol_properties (symtab_node *ref,
263 inchash::hash &hstate,
264 bool address);
266 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
267 point to a same function. Comparison can be skipped if IGNORED_NODES
268 contains these nodes. ADDRESS indicate if address is taken. */
269 bool compare_symbol_references (hash_map <symtab_node *, sem_item *>
270 &ignored_nodes,
271 symtab_node *n1, symtab_node *n2,
272 bool address);
273 protected:
274 /* Hash of item. */
275 hashval_t m_hash;
277 /* Indicated whether a hash value has been set or not. */
278 bool m_hash_set;
280 private:
281 /* Initialize internal data structures. Bitmap STACK is used for
282 bitmap memory allocation process. */
283 void setup (bitmap_obstack *stack);
285 /* Because types can be arbitrarily large, avoid quadratic bottleneck. */
286 static hash_map<const_tree, hashval_t> m_type_hash_cache;
287 }; // class sem_item
289 class sem_function: public sem_item
291 public:
292 /* Semantic function constructor that uses STACK as bitmap memory stack. */
293 sem_function (bitmap_obstack *stack);
295 /* Constructor based on callgraph node _NODE.
296 Bitmap STACK is used for memory allocation. */
297 sem_function (cgraph_node *_node, bitmap_obstack *stack);
299 ~sem_function ();
301 inline virtual void init_wpa (void)
305 virtual void init (void);
306 virtual bool equals_wpa (sem_item *item,
307 hash_map <symtab_node *, sem_item *> &ignored_nodes);
308 virtual hashval_t get_hash (void);
309 virtual bool equals (sem_item *item,
310 hash_map <symtab_node *, sem_item *> &ignored_nodes);
311 virtual bool merge (sem_item *alias_item);
313 /* Dump symbol to FILE. */
314 virtual void dump_to_file (FILE *file)
316 gcc_assert (file);
317 dump_function_to_file (decl, file, TDF_DETAILS);
320 /* Returns cgraph_node. */
321 inline cgraph_node *get_node (void)
323 return dyn_cast <cgraph_node *> (node);
326 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
327 void hash_stmt (gimple *stmt, inchash::hash &inchash);
329 /* Return true if polymorphic comparison must be processed. */
330 bool compare_polymorphic_p (void);
332 /* For a given call graph NODE, the function constructs new
333 semantic function item. */
334 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
336 /* Perform additional checks needed to match types of used function
337 paramters. */
338 bool compatible_parm_types_p (tree, tree);
340 /* Exception handling region tree. */
341 eh_region region_tree;
343 /* Number of function arguments. */
344 unsigned int arg_count;
346 /* Total amount of edges in the function. */
347 unsigned int edge_count;
349 /* Vector of sizes of all basic blocks. */
350 vec <unsigned int> bb_sizes;
352 /* Control flow graph checksum. */
353 hashval_t cfg_checksum;
355 /* GIMPLE codes hash value. */
356 hashval_t gcode_hash;
358 /* Total number of SSA names used in the function. */
359 unsigned ssa_names_size;
361 /* Array of structures for all basic blocks. */
362 vec <ipa_icf_gimple::sem_bb *> bb_sorted;
364 /* Return true if parameter I may be used. */
365 bool param_used_p (unsigned int i);
367 private:
368 /* Calculates hash value based on a BASIC_BLOCK. */
369 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
371 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
372 true value is returned if phi nodes are semantically
373 equivalent in these blocks . */
374 bool compare_phi_node (basic_block bb1, basic_block bb2);
376 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
377 corresponds to TARGET. */
378 bool bb_dict_test (vec<int> *bb_dict, int source, int target);
380 /* If cgraph edges E1 and E2 are indirect calls, verify that
381 ICF flags are the same. */
382 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
384 /* Processes function equality comparison. */
385 bool equals_private (sem_item *item);
387 /* Returns true if tree T can be compared as a handled component. */
388 static bool icf_handled_component_p (tree t);
390 /* Function checker stores binding between functions. */
391 ipa_icf_gimple::func_checker *m_checker;
393 /* COMPARED_FUNC is a function that we compare to. */
394 sem_function *m_compared_func;
395 }; // class sem_function
397 class sem_variable: public sem_item
399 public:
400 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
401 sem_variable (bitmap_obstack *stack);
403 /* Constructor based on callgraph node _NODE.
404 Bitmap STACK is used for memory allocation. */
406 sem_variable (varpool_node *_node, bitmap_obstack *stack);
408 inline virtual void init_wpa (void) {}
410 /* Semantic variable initialization function. */
411 inline virtual void init (void)
413 decl = get_node ()->decl;
416 virtual hashval_t get_hash (void);
417 virtual bool merge (sem_item *alias_item);
418 virtual void dump_to_file (FILE *file);
419 virtual bool equals (sem_item *item,
420 hash_map <symtab_node *, sem_item *> &ignored_nodes);
422 /* Fast equality variable based on knowledge known in WPA. */
423 virtual bool equals_wpa (sem_item *item,
424 hash_map <symtab_node *, sem_item *> &ignored_nodes);
426 /* Returns varpool_node. */
427 inline varpool_node *get_node (void)
429 return dyn_cast <varpool_node *> (node);
432 /* Parser function that visits a varpool NODE. */
433 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
435 private:
436 /* Compares trees T1 and T2 for semantic equality. */
437 static bool equals (tree t1, tree t2);
438 }; // class sem_variable
440 class sem_item_optimizer;
442 struct congruence_class_group
444 hashval_t hash;
445 sem_item_type type;
446 vec <congruence_class *> classes;
449 /* Congruence class set structure. */
450 struct congruence_class_hash : nofree_ptr_hash <congruence_class_group>
452 static inline hashval_t hash (const congruence_class_group *item)
454 return item->hash;
457 static inline int equal (const congruence_class_group *item1,
458 const congruence_class_group *item2)
460 return item1->hash == item2->hash && item1->type == item2->type;
464 struct traverse_split_pair
466 sem_item_optimizer *optimizer;
467 class congruence_class *cls;
470 /* Semantic item optimizer includes all top-level logic
471 related to semantic equality comparison. */
472 class sem_item_optimizer
474 public:
475 sem_item_optimizer ();
476 ~sem_item_optimizer ();
478 /* Function responsible for visiting all potential functions and
479 read-only variables that can be merged. */
480 void parse_funcs_and_vars (void);
482 /* Optimizer entry point which returns true in case it processes
483 a merge operation. True is returned if there's a merge operation
484 processed. */
485 bool execute (void);
487 /* Dump function. */
488 void dump (void);
490 /* Verify congruence classes if checking is enabled. */
491 void checking_verify_classes (void);
493 /* Verify congruence classes. */
494 void verify_classes (void);
496 /* Write IPA ICF summary for symbols. */
497 void write_summary (void);
499 /* Read IPA ICF summary for symbols. */
500 void read_summary (void);
502 /* Callgraph removal hook called for a NODE with a custom DATA. */
503 static void cgraph_removal_hook (cgraph_node *node, void *data);
505 /* Varpool removal hook called for a NODE with a custom DATA. */
506 static void varpool_removal_hook (varpool_node *node, void *data);
508 /* Worklist of congruence classes that can potentially
509 refine classes of congruence. */
510 std::list<congruence_class *> worklist;
512 /* Remove semantic ITEM and release memory. */
513 void remove_item (sem_item *item);
515 /* Remove symtab NODE triggered by symtab removal hooks. */
516 void remove_symtab_node (symtab_node *node);
518 /* Register callgraph and varpool hooks. */
519 void register_hooks (void);
521 /* Unregister callgraph and varpool hooks. */
522 void unregister_hooks (void);
524 /* Adds a CLS to hashtable associated by hash value. */
525 void add_class (congruence_class *cls);
527 /* Gets a congruence class group based on given HASH value and TYPE. */
528 congruence_class_group *get_group_by_hash (hashval_t hash,
529 sem_item_type type);
530 private:
532 /* For each semantic item, append hash values of references. */
533 void update_hash_by_addr_refs ();
535 /* Congruence classes are built by hash value. */
536 void build_hash_based_classes (void);
538 /* Semantic items in classes having more than one element and initialized.
539 In case of WPA, we load function body. */
540 void parse_nonsingleton_classes (void);
542 /* Equality function for semantic items is used to subdivide existing
543 classes. If IN_WPA, fast equality function is invoked. */
544 void subdivide_classes_by_equality (bool in_wpa = false);
546 /* Subdivide classes by address and interposable references
547 that members of the class reference.
548 Example can be a pair of functions that have an address
549 taken from a function. If these addresses are different the class
550 is split. */
551 unsigned subdivide_classes_by_sensitive_refs();
553 /* Debug function prints all informations about congruence classes. */
554 void dump_cong_classes (void);
556 /* Build references according to call graph. */
557 void build_graph (void);
559 /* Iterative congruence reduction function. */
560 void process_cong_reduction (void);
562 /* After reduction is done, we can declare all items in a group
563 to be equal. PREV_CLASS_COUNT is start number of classes
564 before reduction. True is returned if there's a merge operation
565 processed. */
566 bool merge_classes (unsigned int prev_class_count);
568 /* Fixup points to analysis info. */
569 void fixup_points_to_sets (void);
571 /* Fixup points to set PT. */
572 void fixup_pt_set (struct pt_solution *pt);
574 /* Adds a newly created congruence class CLS to worklist. */
575 void worklist_push (congruence_class *cls);
577 /* Pops a class from worklist. */
578 congruence_class *worklist_pop ();
580 /* Every usage of a congruence class CLS is a candidate that can split the
581 collection of classes. Bitmap stack BMSTACK is used for bitmap
582 allocation. */
583 void do_congruence_step (congruence_class *cls);
585 /* Tests if a class CLS used as INDEXth splits any congruence classes.
586 Bitmap stack BMSTACK is used for bitmap allocation. */
587 void do_congruence_step_for_index (congruence_class *cls, unsigned int index);
589 /* Makes pairing between a congruence class CLS and semantic ITEM. */
590 static void add_item_to_class (congruence_class *cls, sem_item *item);
592 /* Disposes split map traverse function. CLS is congruence
593 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
594 but unused argument. */
595 static bool release_split_map (congruence_class * const &cls, bitmap const &b,
596 traverse_split_pair *pair);
598 /* Process split operation for a cognruence class CLS,
599 where bitmap B splits congruence class members. DATA is used
600 as argument of split pair. */
601 static bool traverse_congruence_split (congruence_class * const &cls,
602 bitmap const &b,
603 traverse_split_pair *pair);
605 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
606 contains LEN bytes. */
607 void read_section (lto_file_decl_data *file_data, const char *data,
608 size_t len);
610 /* Removes all callgraph and varpool nodes that are marked by symtab
611 as deleted. */
612 void filter_removed_items (void);
614 /* Vector of semantic items. */
615 vec <sem_item *> m_items;
617 /* A set containing all items removed by hooks. */
618 hash_set <symtab_node *> m_removed_items_set;
620 /* Hashtable of congruence classes. */
621 hash_table <congruence_class_hash> m_classes;
623 /* Count of congruence classes. */
624 unsigned int m_classes_count;
626 /* Map data structure maps symtab nodes to semantic items. */
627 hash_map <symtab_node *, sem_item *> m_symtab_node_map;
629 /* Set to true if a splitter class is removed. */
630 bool splitter_class_removed;
632 /* Global unique class id counter. */
633 static unsigned int class_id;
635 /* Callgraph node removal hook holder. */
636 cgraph_node_hook_list *m_cgraph_node_hooks;
638 /* Varpool node removal hook holder. */
639 varpool_node_hook_list *m_varpool_node_hooks;
641 /* Bitmap stack. */
642 bitmap_obstack m_bmstack;
644 /* Vector of merged variables. Needed for fixup of points-to-analysis
645 info. */
646 vec <symtab_pair> m_merged_variables;
647 }; // class sem_item_optimizer
649 } // ipa_icf namespace