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