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