re PR lto/64693 (PCH failed with --with-build-config=bootstrap-lto)
[gcc.git] / gcc / ipa-icf.h
1 /* Interprocedural semantic function equality pass
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 namespace ipa_icf {
23 class sem_item;
24
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
29 {
30 public:
31 /* Congruence class constructor for a new class with _ID. */
32 congruence_class (unsigned int _id): in_worklist (false), id(_id)
33 {
34 }
35
36 /* Destructor. */
37 ~congruence_class ()
38 {
39 }
40
41 /* Dump function prints all class members to a FILE with an INDENT. */
42 void dump (FILE *file, unsigned int indent = 0) const;
43
44 /* Returns true if there's a member that is used from another group. */
45 bool is_class_used (void);
46
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;
51
52 /* Vector of all group members. */
53 auto_vec <sem_item *> members;
54
55 /* Global unique class identifier. */
56 unsigned int id;
57 };
58
59 /* Semantic item type enum. */
60 enum sem_item_type
61 {
62 FUNC,
63 VAR
64 };
65
66 /* Class is container for address references for a symtab_node. */
67
68 class symbol_compare_collection
69 {
70 public:
71 /* Constructor. */
72 symbol_compare_collection (symtab_node *node);
73
74 /* Destructor. */
75 ~symbol_compare_collection ()
76 {
77 m_references.release ();
78 m_interposables.release ();
79 }
80
81 /* Vector of address references. */
82 vec<symtab_node *> m_references;
83
84 /* Vector of interposable references. */
85 vec<symtab_node *> m_interposables;
86 };
87
88 /* Hash traits for symbol_compare_collection map. */
89
90 struct symbol_compare_hashmap_traits: default_hashmap_traits
91 {
92 static hashval_t
93 hash (const symbol_compare_collection *v)
94 {
95 inchash::hash hstate;
96 hstate.add_int (v->m_references.length ());
97
98 for (unsigned i = 0; i < v->m_references.length (); i++)
99 hstate.add_ptr (v->m_references[i]->ultimate_alias_target ());
100
101 hstate.add_int (v->m_interposables.length ());
102
103 for (unsigned i = 0; i < v->m_interposables.length (); i++)
104 hstate.add_ptr (v->m_interposables[i]->ultimate_alias_target ());
105
106 return hstate.end ();
107 }
108
109 static bool
110 equal_keys (const symbol_compare_collection *a,
111 const symbol_compare_collection *b)
112 {
113 if (a->m_references.length () != b->m_references.length ())
114 return false;
115
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;
119
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;
124
125 return true;
126 }
127 };
128
129
130 /* Semantic item usage pair. */
131 class sem_usage_pair
132 {
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);
136
137 /* Target semantic item where an item is used. */
138 sem_item *item;
139
140 /* Index of usage of such an item. */
141 unsigned int index;
142 };
143
144 /* Semantic item is a base class that encapsulates all shared functionality
145 for both semantic function and variable items. */
146 class sem_item
147 {
148 public:
149 /* Semantic item constructor for a node of _TYPE, where STACK is used
150 for bitmap memory allocation. */
151 sem_item (sem_item_type _type, bitmap_obstack *stack);
152
153 /* Semantic item constructor for a node of _TYPE, where STACK is used
154 for bitmap memory allocation. The item is based on symtab node _NODE
155 with computed _HASH. */
156 sem_item (sem_item_type _type, symtab_node *_node, hashval_t _hash,
157 bitmap_obstack *stack);
158
159 virtual ~sem_item ();
160
161 /* Dump function for debugging purpose. */
162 DEBUG_FUNCTION void dump (void);
163
164 /* Initialize semantic item by info reachable during LTO WPA phase. */
165 virtual void init_wpa (void) = 0;
166
167 /* Semantic item initialization function. */
168 virtual void init (void) = 0;
169
170 /* Add reference to a semantic TARGET. */
171 void add_reference (sem_item *target);
172
173 /* Gets symbol name of the item. */
174 const char *name (void)
175 {
176 return node->name ();
177 }
178
179 /* Gets assembler name of the item. */
180 const char *asm_name (void)
181 {
182 return node->asm_name ();
183 }
184
185 /* Fast equality function based on knowledge known in WPA. */
186 virtual bool equals_wpa (sem_item *item,
187 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
188
189 /* Returns true if the item equals to ITEM given as arguemnt. */
190 virtual bool equals (sem_item *item,
191 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
192
193 /* References independent hash function. */
194 virtual hashval_t get_hash (void) = 0;
195
196 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
197 be applied. */
198 virtual bool merge (sem_item *alias_item) = 0;
199
200 /* Dump symbol to FILE. */
201 virtual void dump_to_file (FILE *file) = 0;
202
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);
206
207 /* Return true if target supports alias symbols. */
208 bool target_supports_symbol_aliases_p (void);
209
210 /* Item type. */
211 sem_item_type type;
212
213 /* Symtab node. */
214 symtab_node *node;
215
216 /* Declaration tree node. */
217 tree decl;
218
219 /* Semantic references used that generate congruence groups. */
220 vec <sem_item *> refs;
221
222 /* Pointer to a congruence class the item belongs to. */
223 congruence_class *cls;
224
225 /* Index of the item in a class belonging to. */
226 unsigned int index_in_class;
227
228 /* List of semantic items where the instance is used. */
229 vec <sem_usage_pair *> usages;
230
231 /* A bitmap with indices of all classes referencing this item. */
232 bitmap usage_index_bitmap;
233
234 /* List of tree references (either FUNC_DECL or VAR_DECL). */
235 vec <tree> tree_refs;
236
237 /* A set with symbol table references. */
238 hash_set <symtab_node *> refs_set;
239
240 protected:
241 /* Cached, once calculated hash for the item. */
242 hashval_t hash;
243
244 private:
245 /* Initialize internal data structures. Bitmap STACK is used for
246 bitmap memory allocation process. */
247 void setup (bitmap_obstack *stack);
248 }; // class sem_item
249
250 class sem_function: public sem_item
251 {
252 public:
253 /* Semantic function constructor that uses STACK as bitmap memory stack. */
254 sem_function (bitmap_obstack *stack);
255
256 /* Constructor based on callgraph node _NODE with computed hash _HASH.
257 Bitmap STACK is used for memory allocation. */
258 sem_function (cgraph_node *_node, hashval_t _hash, bitmap_obstack *stack);
259
260 ~sem_function ();
261
262 inline virtual void init_wpa (void)
263 {
264 parse_tree_args ();
265 }
266
267 virtual void init (void);
268 virtual bool equals_wpa (sem_item *item,
269 hash_map <symtab_node *, sem_item *> &ignored_nodes);
270 virtual hashval_t get_hash (void);
271 virtual bool equals (sem_item *item,
272 hash_map <symtab_node *, sem_item *> &ignored_nodes);
273 virtual bool merge (sem_item *alias_item);
274
275 /* Dump symbol to FILE. */
276 virtual void dump_to_file (FILE *file)
277 {
278 gcc_assert (file);
279 dump_function_to_file (decl, file, TDF_DETAILS);
280 }
281
282 /* Parses function arguments and result type. */
283 void parse_tree_args (void);
284
285 /* Returns cgraph_node. */
286 inline cgraph_node *get_node (void)
287 {
288 return dyn_cast <cgraph_node *> (node);
289 }
290
291 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
292 void hash_stmt (inchash::hash *inchash, gimple stmt);
293
294 /* Return true if polymorphic comparison must be processed. */
295 bool compare_polymorphic_p (void);
296
297 /* For a given call graph NODE, the function constructs new
298 semantic function item. */
299 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
300
301 /* Exception handling region tree. */
302 eh_region region_tree;
303
304 /* Result type tree node. */
305 tree result_type;
306
307 /* Array of argument tree types. */
308 vec <tree> arg_types;
309
310 /* Number of function arguments. */
311 unsigned int arg_count;
312
313 /* Total amount of edges in the function. */
314 unsigned int edge_count;
315
316 /* Vector of sizes of all basic blocks. */
317 vec <unsigned int> bb_sizes;
318
319 /* Control flow graph checksum. */
320 hashval_t cfg_checksum;
321
322 /* GIMPLE codes hash value. */
323 hashval_t gcode_hash;
324
325 /* Total number of SSA names used in the function. */
326 unsigned ssa_names_size;
327
328 /* Array of structures for all basic blocks. */
329 vec <ipa_icf_gimple::sem_bb *> bb_sorted;
330
331 private:
332 /* Calculates hash value based on a BASIC_BLOCK. */
333 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
334
335 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
336 true value is returned if phi nodes are semantically
337 equivalent in these blocks . */
338 bool compare_phi_node (basic_block bb1, basic_block bb2);
339
340 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
341 corresponds to TARGET. */
342 bool bb_dict_test (vec<int> *bb_dict, int source, int target);
343
344 /* Iterates all tree types in T1 and T2 and returns true if all types
345 are compatible. If COMPARE_POLYMORPHIC is set to true,
346 more strict comparison is executed. */
347 bool compare_type_list (tree t1, tree t2, bool compare_polymorphic);
348
349 /* If cgraph edges E1 and E2 are indirect calls, verify that
350 ICF flags are the same. */
351 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
352
353 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
354 point to a same function. Comparison can be skipped if IGNORED_NODES
355 contains these nodes. */
356 bool compare_cgraph_references (hash_map <symtab_node *, sem_item *>
357 &ignored_nodes,
358 symtab_node *n1, symtab_node *n2);
359
360 /* Processes function equality comparison. */
361 bool equals_private (sem_item *item,
362 hash_map <symtab_node *, sem_item *> &ignored_nodes);
363
364 /* Returns true if tree T can be compared as a handled component. */
365 static bool icf_handled_component_p (tree t);
366
367 /* Function checker stores binding between functions. */
368 ipa_icf_gimple::func_checker *m_checker;
369
370 /* COMPARED_FUNC is a function that we compare to. */
371 sem_function *m_compared_func;
372 }; // class sem_function
373
374 class sem_variable: public sem_item
375 {
376 public:
377 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
378 sem_variable (bitmap_obstack *stack);
379
380 /* Constructor based on callgraph node _NODE with computed hash _HASH.
381 Bitmap STACK is used for memory allocation. */
382
383 sem_variable (varpool_node *_node, hashval_t _hash, bitmap_obstack *stack);
384
385 inline virtual void init_wpa (void) {}
386
387 /* Semantic variable initialization function. */
388 inline virtual void init (void)
389 {
390 decl = get_node ()->decl;
391 ctor = ctor_for_folding (decl);
392 }
393
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);
399
400 /* Fast equality variable based on knowledge known in WPA. */
401 inline virtual bool equals_wpa (sem_item *item,
402 hash_map <symtab_node *, sem_item *> & ARG_UNUSED(ignored_nodes))
403 {
404 gcc_assert (item->type == VAR);
405 return true;
406 }
407
408 /* Returns varpool_node. */
409 inline varpool_node *get_node (void)
410 {
411 return dyn_cast <varpool_node *> (node);
412 }
413
414 /* Parser function that visits a varpool NODE. */
415 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
416
417 /* Variable constructor. */
418 tree ctor;
419
420 private:
421 /* Iterates though a constructor and identifies tree references
422 we are interested in semantic function equality. */
423 void parse_tree_refs (tree t);
424
425 /* Compares trees T1 and T2 for semantic equality. */
426 static bool equals (tree t1, tree t2);
427
428 /* Compare that symbol sections are either NULL or have same name. */
429 bool compare_sections (sem_variable *alias);
430
431 }; // class sem_variable
432
433 class sem_item_optimizer;
434
435 struct congruence_class_group
436 {
437 hashval_t hash;
438 sem_item_type type;
439 vec <congruence_class *> classes;
440 };
441
442 /* Congruence class set structure. */
443 struct congruence_class_group_hash: typed_noop_remove <congruence_class_group>
444 {
445 typedef congruence_class_group value_type;
446 typedef congruence_class_group compare_type;
447
448 static inline hashval_t hash (const value_type *item)
449 {
450 return item->hash;
451 }
452
453 static inline int equal (const value_type *item1, const compare_type *item2)
454 {
455 return item1->hash == item2->hash && item1->type == item2->type;
456 }
457 };
458
459 struct traverse_split_pair
460 {
461 sem_item_optimizer *optimizer;
462 class congruence_class *cls;
463 };
464
465 /* Semantic item optimizer includes all top-level logic
466 related to semantic equality comparison. */
467 class sem_item_optimizer
468 {
469 public:
470 sem_item_optimizer ();
471 ~sem_item_optimizer ();
472
473 /* Function responsible for visiting all potential functions and
474 read-only variables that can be merged. */
475 void parse_funcs_and_vars (void);
476
477 /* Optimizer entry point. */
478 void execute (void);
479
480 /* Dump function. */
481 void dump (void);
482
483 /* Verify congruence classes if checking is enabled. */
484 void verify_classes (void);
485
486 /* Write IPA ICF summary for symbols. */
487 void write_summary (void);
488
489 /* Read IPA IPA ICF summary for symbols. */
490 void read_summary (void);
491
492 /* Callgraph removal hook called for a NODE with a custom DATA. */
493 static void cgraph_removal_hook (cgraph_node *node, void *data);
494
495 /* Varpool removal hook called for a NODE with a custom DATA. */
496 static void varpool_removal_hook (varpool_node *node, void *data);
497
498 /* Worklist of congruence classes that can potentially
499 refine classes of congruence. */
500 std::list<congruence_class *> worklist;
501
502 /* Remove semantic ITEM and release memory. */
503 void remove_item (sem_item *item);
504
505 /* Remove symtab NODE triggered by symtab removal hooks. */
506 void remove_symtab_node (symtab_node *node);
507
508 /* Register callgraph and varpool hooks. */
509 void register_hooks (void);
510
511 /* Unregister callgraph and varpool hooks. */
512 void unregister_hooks (void);
513
514 /* Adds a CLS to hashtable associated by hash value. */
515 void add_class (congruence_class *cls);
516
517 /* Gets a congruence class group based on given HASH value and TYPE. */
518 congruence_class_group *get_group_by_hash (hashval_t hash,
519 sem_item_type type);
520
521 private:
522
523 /* Congruence classes are built by hash value. */
524 void build_hash_based_classes (void);
525
526 /* Semantic items in classes having more than one element and initialized.
527 In case of WPA, we load function body. */
528 void parse_nonsingleton_classes (void);
529
530 /* Equality function for semantic items is used to subdivide existing
531 classes. If IN_WPA, fast equality function is invoked. */
532 void subdivide_classes_by_equality (bool in_wpa = false);
533
534 /* Subdivide classes by address and interposable references
535 that members of the class reference.
536 Example can be a pair of functions that have an address
537 taken from a function. If these addresses are different the class
538 is split. */
539 unsigned subdivide_classes_by_sensitive_refs();
540
541 /* Debug function prints all informations about congruence classes. */
542 void dump_cong_classes (void);
543
544 /* Build references according to call graph. */
545 void build_graph (void);
546
547 /* Iterative congruence reduction function. */
548 void process_cong_reduction (void);
549
550 /* After reduction is done, we can declare all items in a group
551 to be equal. PREV_CLASS_COUNT is start number of classes
552 before reduction. */
553 void merge_classes (unsigned int prev_class_count);
554
555 /* Adds a newly created congruence class CLS to worklist. */
556 void worklist_push (congruence_class *cls);
557
558 /* Pops a class from worklist. */
559 congruence_class *worklist_pop ();
560
561 /* Every usage of a congruence class CLS is a candidate that can split the
562 collection of classes. Bitmap stack BMSTACK is used for bitmap
563 allocation. */
564 void do_congruence_step (congruence_class *cls);
565
566 /* Tests if a class CLS used as INDEXth splits any congruence classes.
567 Bitmap stack BMSTACK is used for bitmap allocation. */
568 void do_congruence_step_for_index (congruence_class *cls, unsigned int index);
569
570 /* Makes pairing between a congruence class CLS and semantic ITEM. */
571 static void add_item_to_class (congruence_class *cls, sem_item *item);
572
573 /* Disposes split map traverse function. CLS is congruence
574 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
575 but unused argument. */
576 static bool release_split_map (congruence_class * const &cls, bitmap const &b,
577 traverse_split_pair *pair);
578
579 /* Process split operation for a cognruence class CLS,
580 where bitmap B splits congruence class members. DATA is used
581 as argument of split pair. */
582 static bool traverse_congruence_split (congruence_class * const &cls,
583 bitmap const &b,
584 traverse_split_pair *pair);
585
586 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
587 contains LEN bytes. */
588 void read_section (lto_file_decl_data *file_data, const char *data,
589 size_t len);
590
591 /* Removes all callgraph and varpool nodes that are marked by symtab
592 as deleted. */
593 void filter_removed_items (void);
594
595 /* Vector of semantic items. */
596 vec <sem_item *> m_items;
597
598 /* A set containing all items removed by hooks. */
599 hash_set <symtab_node *> m_removed_items_set;
600
601 /* Hashtable of congruence classes */
602 hash_table <congruence_class_group_hash> m_classes;
603
604 /* Count of congruence classes. */
605 unsigned int m_classes_count;
606
607 /* Map data structure maps symtab nodes to semantic items. */
608 hash_map <symtab_node *, sem_item *> m_symtab_node_map;
609
610 /* Set to true if a splitter class is removed. */
611 bool splitter_class_removed;
612
613 /* Global unique class id counter. */
614 static unsigned int class_id;
615
616 /* Callgraph node removal hook holder. */
617 cgraph_node_hook_list *m_cgraph_node_hooks;
618
619 /* Varpool node removal hook holder. */
620 varpool_node_hook_list *m_varpool_node_hooks;
621
622 /* Bitmap stack. */
623 bitmap_obstack m_bmstack;
624 }; // class sem_item_optimizer
625
626 } // ipa_icf namespace