]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-icf.h
Daily bump.
[thirdparty/gcc.git] / gcc / ipa-icf.h
CommitLineData
b84d4347 1/* Interprocedural semantic function equality pass
5624e564 2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
b84d4347
ML
3
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22namespace ipa_icf {
23class 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. */
28class congruence_class
29{
30public:
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. */
60enum sem_item_type
61{
62 FUNC,
63 VAR
64};
65
5ebd0e61
ML
66/* Class is container for address references for a symtab_node. */
67
68class symbol_compare_collection
69{
70public:
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
90struct 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++)
69f6b1f4 99 hstate.add_int (v->m_references[i]->ultimate_alias_target ()->order);
5ebd0e61
ML
100
101 hstate.add_int (v->m_interposables.length ());
102
103 for (unsigned i = 0; i < v->m_interposables.length (); i++)
69f6b1f4 104 hstate.add_int (v->m_interposables[i]->ultimate_alias_target ()->order);
5ebd0e61
ML
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 {
b94097dc
ML
113 if (a->m_references.length () != b->m_references.length ()
114 || a->m_interposables.length () != b->m_interposables.length ())
5ebd0e61
ML
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
b84d4347
ML
131/* Semantic item usage pair. */
132class sem_usage_pair
133{
134public:
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. */
147class sem_item
148{
149public:
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
b84d4347
ML
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;
177
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;
181
182 /* References independent hash function. */
183 virtual hashval_t get_hash (void) = 0;
184
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;
188
189 /* Dump symbol to FILE. */
190 virtual void dump_to_file (FILE *file) = 0;
191
3ab93359
ML
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);
195
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);
200
b84d4347
ML
201 /* Return base tree that can be used for compatible_types_p and
202 contains_polymorphic_type_p comparison. */
b84d4347
ML
203 static bool get_base_types (tree *t1, tree *t2);
204
f657d665
ML
205 /* Return true if target supports alias symbols. */
206 bool target_supports_symbol_aliases_p (void);
207
b84d4347
ML
208 /* Item type. */
209 sem_item_type type;
210
211 /* Symtab node. */
212 symtab_node *node;
213
214 /* Declaration tree node. */
215 tree decl;
216
217 /* Semantic references used that generate congruence groups. */
218 vec <sem_item *> refs;
219
220 /* Pointer to a congruence class the item belongs to. */
221 congruence_class *cls;
222
223 /* Index of the item in a class belonging to. */
224 unsigned int index_in_class;
225
226 /* List of semantic items where the instance is used. */
227 vec <sem_usage_pair *> usages;
228
229 /* A bitmap with indices of all classes referencing this item. */
230 bitmap usage_index_bitmap;
231
232 /* List of tree references (either FUNC_DECL or VAR_DECL). */
233 vec <tree> tree_refs;
234
235 /* A set with symbol table references. */
236 hash_set <symtab_node *> refs_set;
237
3ab93359
ML
238 /* Hash of item. */
239 hashval_t hash;
240
241 /* Temporary hash used where hash values of references are added. */
242 hashval_t global_hash;
b84d4347
ML
243protected:
244 /* Cached, once calculated hash for the item. */
46305737 245
69f6b1f4 246 /* Accumulate to HSTATE a hash of expression EXP. */
a8d93817 247 static void add_expr (const_tree exp, inchash::hash &hstate);
69f6b1f4
JH
248 /* Accumulate to HSTATE a hash of type T. */
249 static void add_type (const_tree t, inchash::hash &hstate);
b84d4347 250
46305737
JH
251 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
252 point to a same function. Comparison can be skipped if IGNORED_NODES
253 contains these nodes. ADDRESS indicate if address is taken. */
254 bool compare_cgraph_references (hash_map <symtab_node *, sem_item *>
255 &ignored_nodes,
256 symtab_node *n1, symtab_node *n2,
257 bool address);
258
b84d4347
ML
259private:
260 /* Initialize internal data structures. Bitmap STACK is used for
261 bitmap memory allocation process. */
262 void setup (bitmap_obstack *stack);
263}; // class sem_item
264
265class sem_function: public sem_item
266{
267public:
268 /* Semantic function constructor that uses STACK as bitmap memory stack. */
269 sem_function (bitmap_obstack *stack);
270
271 /* Constructor based on callgraph node _NODE with computed hash _HASH.
272 Bitmap STACK is used for memory allocation. */
273 sem_function (cgraph_node *_node, hashval_t _hash, bitmap_obstack *stack);
274
275 ~sem_function ();
276
277 inline virtual void init_wpa (void)
278 {
279 parse_tree_args ();
280 }
281
282 virtual void init (void);
283 virtual bool equals_wpa (sem_item *item,
284 hash_map <symtab_node *, sem_item *> &ignored_nodes);
285 virtual hashval_t get_hash (void);
286 virtual bool equals (sem_item *item,
287 hash_map <symtab_node *, sem_item *> &ignored_nodes);
288 virtual bool merge (sem_item *alias_item);
289
290 /* Dump symbol to FILE. */
291 virtual void dump_to_file (FILE *file)
292 {
293 gcc_assert (file);
294 dump_function_to_file (decl, file, TDF_DETAILS);
295 }
296
297 /* Parses function arguments and result type. */
298 void parse_tree_args (void);
299
300 /* Returns cgraph_node. */
301 inline cgraph_node *get_node (void)
302 {
303 return dyn_cast <cgraph_node *> (node);
304 }
305
306 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
a8d93817 307 void hash_stmt (gimple stmt, inchash::hash &inchash);
b84d4347
ML
308
309 /* Return true if polymorphic comparison must be processed. */
310 bool compare_polymorphic_p (void);
311
312 /* For a given call graph NODE, the function constructs new
313 semantic function item. */
314 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
315
316 /* Exception handling region tree. */
317 eh_region region_tree;
318
319 /* Result type tree node. */
320 tree result_type;
321
322 /* Array of argument tree types. */
323 vec <tree> arg_types;
324
325 /* Number of function arguments. */
326 unsigned int arg_count;
327
328 /* Total amount of edges in the function. */
329 unsigned int edge_count;
330
331 /* Vector of sizes of all basic blocks. */
332 vec <unsigned int> bb_sizes;
333
334 /* Control flow graph checksum. */
335 hashval_t cfg_checksum;
336
337 /* GIMPLE codes hash value. */
338 hashval_t gcode_hash;
339
340 /* Total number of SSA names used in the function. */
341 unsigned ssa_names_size;
342
343 /* Array of structures for all basic blocks. */
344 vec <ipa_icf_gimple::sem_bb *> bb_sorted;
345
346private:
347 /* Calculates hash value based on a BASIC_BLOCK. */
348 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
349
350 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
351 true value is returned if phi nodes are semantically
352 equivalent in these blocks . */
353 bool compare_phi_node (basic_block bb1, basic_block bb2);
354
355 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
356 corresponds to TARGET. */
1216ea72 357 bool bb_dict_test (vec<int> *bb_dict, int source, int target);
b84d4347 358
b84d4347
ML
359 /* If cgraph edges E1 and E2 are indirect calls, verify that
360 ICF flags are the same. */
361 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
362
b84d4347
ML
363 /* Processes function equality comparison. */
364 bool equals_private (sem_item *item,
365 hash_map <symtab_node *, sem_item *> &ignored_nodes);
366
367 /* Returns true if tree T can be compared as a handled component. */
368 static bool icf_handled_component_p (tree t);
369
370 /* Function checker stores binding between functions. */
371 ipa_icf_gimple::func_checker *m_checker;
372
373 /* COMPARED_FUNC is a function that we compare to. */
374 sem_function *m_compared_func;
375}; // class sem_function
376
377class sem_variable: public sem_item
378{
379public:
380 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
381 sem_variable (bitmap_obstack *stack);
382
383 /* Constructor based on callgraph node _NODE with computed hash _HASH.
384 Bitmap STACK is used for memory allocation. */
385
386 sem_variable (varpool_node *_node, hashval_t _hash, bitmap_obstack *stack);
387
388 inline virtual void init_wpa (void) {}
389
390 /* Semantic variable initialization function. */
391 inline virtual void init (void)
392 {
393 decl = get_node ()->decl;
b84d4347
ML
394 }
395
396 virtual hashval_t get_hash (void);
397 virtual bool merge (sem_item *alias_item);
398 virtual void dump_to_file (FILE *file);
399 virtual bool equals (sem_item *item,
400 hash_map <symtab_node *, sem_item *> &ignored_nodes);
401
402 /* Fast equality variable based on knowledge known in WPA. */
46305737
JH
403 virtual bool equals_wpa (sem_item *item,
404 hash_map <symtab_node *, sem_item *> &ignored_nodes);
b84d4347
ML
405
406 /* Returns varpool_node. */
407 inline varpool_node *get_node (void)
408 {
409 return dyn_cast <varpool_node *> (node);
410 }
411
412 /* Parser function that visits a varpool NODE. */
413 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
414
b84d4347 415private:
b84d4347
ML
416 /* Compares trees T1 and T2 for semantic equality. */
417 static bool equals (tree t1, tree t2);
b84d4347
ML
418}; // class sem_variable
419
420class sem_item_optimizer;
421
422struct congruence_class_group
423{
424 hashval_t hash;
425 sem_item_type type;
426 vec <congruence_class *> classes;
427};
428
429/* Congruence class set structure. */
430struct congruence_class_group_hash: typed_noop_remove <congruence_class_group>
431{
432 typedef congruence_class_group value_type;
433 typedef congruence_class_group compare_type;
434
435 static inline hashval_t hash (const value_type *item)
436 {
437 return item->hash;
438 }
439
440 static inline int equal (const value_type *item1, const compare_type *item2)
441 {
442 return item1->hash == item2->hash && item1->type == item2->type;
443 }
444};
445
446struct traverse_split_pair
447{
448 sem_item_optimizer *optimizer;
449 class congruence_class *cls;
450};
451
452/* Semantic item optimizer includes all top-level logic
453 related to semantic equality comparison. */
454class sem_item_optimizer
455{
456public:
457 sem_item_optimizer ();
458 ~sem_item_optimizer ();
459
460 /* Function responsible for visiting all potential functions and
461 read-only variables that can be merged. */
462 void parse_funcs_and_vars (void);
463
bd31fe14
ML
464 /* Optimizer entry point which returns true in case it processes
465 a merge operation. True is returned if there's a merge operation
466 processed. */
467 bool execute (void);
b84d4347
ML
468
469 /* Dump function. */
470 void dump (void);
471
472 /* Verify congruence classes if checking is enabled. */
473 void verify_classes (void);
474
475 /* Write IPA ICF summary for symbols. */
476 void write_summary (void);
477
478 /* Read IPA IPA ICF summary for symbols. */
479 void read_summary (void);
480
481 /* Callgraph removal hook called for a NODE with a custom DATA. */
482 static void cgraph_removal_hook (cgraph_node *node, void *data);
483
484 /* Varpool removal hook called for a NODE with a custom DATA. */
485 static void varpool_removal_hook (varpool_node *node, void *data);
486
487 /* Worklist of congruence classes that can potentially
488 refine classes of congruence. */
489 std::list<congruence_class *> worklist;
490
491 /* Remove semantic ITEM and release memory. */
492 void remove_item (sem_item *item);
493
494 /* Remove symtab NODE triggered by symtab removal hooks. */
495 void remove_symtab_node (symtab_node *node);
496
497 /* Register callgraph and varpool hooks. */
498 void register_hooks (void);
499
500 /* Unregister callgraph and varpool hooks. */
501 void unregister_hooks (void);
502
503 /* Adds a CLS to hashtable associated by hash value. */
504 void add_class (congruence_class *cls);
505
506 /* Gets a congruence class group based on given HASH value and TYPE. */
507 congruence_class_group *get_group_by_hash (hashval_t hash,
508 sem_item_type type);
509
69f6b1f4
JH
510 /* Because types can be arbitrarily large, avoid quadratic bottleneck. */
511 hash_map<const_tree, hashval_t> m_type_hash_cache;
b84d4347
ML
512private:
513
3ab93359
ML
514 /* For each semantic item, append hash values of references. */
515 void update_hash_by_addr_refs ();
516
b84d4347
ML
517 /* Congruence classes are built by hash value. */
518 void build_hash_based_classes (void);
519
520 /* Semantic items in classes having more than one element and initialized.
521 In case of WPA, we load function body. */
522 void parse_nonsingleton_classes (void);
523
524 /* Equality function for semantic items is used to subdivide existing
525 classes. If IN_WPA, fast equality function is invoked. */
526 void subdivide_classes_by_equality (bool in_wpa = false);
527
5ebd0e61
ML
528 /* Subdivide classes by address and interposable references
529 that members of the class reference.
530 Example can be a pair of functions that have an address
531 taken from a function. If these addresses are different the class
532 is split. */
533 unsigned subdivide_classes_by_sensitive_refs();
534
b84d4347
ML
535 /* Debug function prints all informations about congruence classes. */
536 void dump_cong_classes (void);
537
538 /* Build references according to call graph. */
539 void build_graph (void);
540
541 /* Iterative congruence reduction function. */
542 void process_cong_reduction (void);
543
544 /* After reduction is done, we can declare all items in a group
545 to be equal. PREV_CLASS_COUNT is start number of classes
bd31fe14
ML
546 before reduction. True is returned if there's a merge operation
547 processed. */
548 bool merge_classes (unsigned int prev_class_count);
b84d4347
ML
549
550 /* Adds a newly created congruence class CLS to worklist. */
551 void worklist_push (congruence_class *cls);
552
553 /* Pops a class from worklist. */
554 congruence_class *worklist_pop ();
555
556 /* Every usage of a congruence class CLS is a candidate that can split the
557 collection of classes. Bitmap stack BMSTACK is used for bitmap
558 allocation. */
559 void do_congruence_step (congruence_class *cls);
560
561 /* Tests if a class CLS used as INDEXth splits any congruence classes.
562 Bitmap stack BMSTACK is used for bitmap allocation. */
563 void do_congruence_step_for_index (congruence_class *cls, unsigned int index);
564
565 /* Makes pairing between a congruence class CLS and semantic ITEM. */
566 static void add_item_to_class (congruence_class *cls, sem_item *item);
567
568 /* Disposes split map traverse function. CLS is congruence
569 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
570 but unused argument. */
571 static bool release_split_map (congruence_class * const &cls, bitmap const &b,
572 traverse_split_pair *pair);
573
574 /* Process split operation for a cognruence class CLS,
575 where bitmap B splits congruence class members. DATA is used
576 as argument of split pair. */
577 static bool traverse_congruence_split (congruence_class * const &cls,
578 bitmap const &b,
579 traverse_split_pair *pair);
580
581 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
582 contains LEN bytes. */
583 void read_section (lto_file_decl_data *file_data, const char *data,
584 size_t len);
585
586 /* Removes all callgraph and varpool nodes that are marked by symtab
587 as deleted. */
588 void filter_removed_items (void);
589
590 /* Vector of semantic items. */
591 vec <sem_item *> m_items;
592
593 /* A set containing all items removed by hooks. */
594 hash_set <symtab_node *> m_removed_items_set;
595
596 /* Hashtable of congruence classes */
597 hash_table <congruence_class_group_hash> m_classes;
598
599 /* Count of congruence classes. */
600 unsigned int m_classes_count;
601
602 /* Map data structure maps symtab nodes to semantic items. */
603 hash_map <symtab_node *, sem_item *> m_symtab_node_map;
604
605 /* Set to true if a splitter class is removed. */
606 bool splitter_class_removed;
607
608 /* Global unique class id counter. */
609 static unsigned int class_id;
610
611 /* Callgraph node removal hook holder. */
612 cgraph_node_hook_list *m_cgraph_node_hooks;
613
614 /* Varpool node removal hook holder. */
615 varpool_node_hook_list *m_varpool_node_hooks;
616
617 /* Bitmap stack. */
618 bitmap_obstack m_bmstack;
619}; // class sem_item_optimizer
620
621} // ipa_icf namespace