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