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
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
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/>. */
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
31 /* Congruence class constructor for a new class with _ID. */
32 congruence_class (unsigned int _id
): in_worklist (false), id(_id
)
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). */
52 /* Vector of all group members. */
53 auto_vec
<sem_item
*> members
;
55 /* Global unique class identifier. */
59 /* Semantic item type enum. */
66 /* Semantic item usage pair. */
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. */
76 /* Index of usage of such an item. */
80 /* Semantic item is a base class that encapsulates all shared functionality
81 for both semantic function and variable items. */
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
);
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
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. */
142 static bool get_base_types (tree
*t1
, tree
*t2
);
150 /* Declaration tree node. */
153 /* Semantic references used that generate congruence groups. */
154 vec
<sem_item
*> refs
;
156 /* Pointer to a congruence class the item belongs to. */
157 congruence_class
*cls
;
159 /* Index of the item in a class belonging to. */
160 unsigned int index_in_class
;
162 /* List of semantic items where the instance is used. */
163 vec
<sem_usage_pair
*> usages
;
165 /* A bitmap with indices of all classes referencing this item. */
166 bitmap usage_index_bitmap
;
168 /* List of tree references (either FUNC_DECL or VAR_DECL). */
169 vec
<tree
> tree_refs
;
171 /* A set with symbol table references. */
172 hash_set
<symtab_node
*> refs_set
;
175 /* Cached, once calculated hash for the item. */
179 /* Initialize internal data structures. Bitmap STACK is used for
180 bitmap memory allocation process. */
181 void setup (bitmap_obstack
*stack
);
184 class sem_function
: public sem_item
187 /* Semantic function constructor that uses STACK as bitmap memory stack. */
188 sem_function (bitmap_obstack
*stack
);
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
);
196 inline virtual void init_wpa (void)
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
);
209 /* Dump symbol to FILE. */
210 virtual void dump_to_file (FILE *file
)
213 dump_function_to_file (decl
, file
, TDF_DETAILS
);
216 /* Parses function arguments and result type. */
217 void parse_tree_args (void);
219 /* Returns cgraph_node. */
220 inline cgraph_node
*get_node (void)
222 return dyn_cast
<cgraph_node
*> (node
);
225 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
226 void hash_stmt (inchash::hash
*inchash
, gimple stmt
);
228 /* Return true if polymorphic comparison must be processed. */
229 bool compare_polymorphic_p (void);
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
);
235 /* Exception handling region tree. */
236 eh_region region_tree
;
238 /* Result type tree node. */
241 /* Array of argument tree types. */
242 vec
<tree
> arg_types
;
244 /* Number of function arguments. */
245 unsigned int arg_count
;
247 /* Total amount of edges in the function. */
248 unsigned int edge_count
;
250 /* Vector of sizes of all basic blocks. */
251 vec
<unsigned int> bb_sizes
;
253 /* Control flow graph checksum. */
254 hashval_t cfg_checksum
;
256 /* GIMPLE codes hash value. */
257 hashval_t gcode_hash
;
259 /* Total number of SSA names used in the function. */
260 unsigned ssa_names_size
;
262 /* Array of structures for all basic blocks. */
263 vec
<ipa_icf_gimple::sem_bb
*> bb_sorted
;
266 /* Calculates hash value based on a BASIC_BLOCK. */
267 hashval_t
get_bb_hash (const ipa_icf_gimple::sem_bb
*basic_block
);
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
);
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
);
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
);
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
);
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
*>
292 symtab_node
*n1
, symtab_node
*n2
);
294 /* Processes function equality comparison. */
295 bool equals_private (sem_item
*item
,
296 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
298 /* Returns true if tree T can be compared as a handled component. */
299 static bool icf_handled_component_p (tree t
);
301 /* Function checker stores binding between functions. */
302 ipa_icf_gimple::func_checker
*m_checker
;
304 /* COMPARED_FUNC is a function that we compare to. */
305 sem_function
*m_compared_func
;
306 }; // class sem_function
308 class sem_variable
: public sem_item
311 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
312 sem_variable (bitmap_obstack
*stack
);
314 /* Constructor based on callgraph node _NODE with computed hash _HASH.
315 Bitmap STACK is used for memory allocation. */
317 sem_variable (varpool_node
*_node
, hashval_t _hash
, bitmap_obstack
*stack
);
319 inline virtual void init_wpa (void) {}
321 /* Semantic variable initialization function. */
322 inline virtual void init (void)
324 decl
= get_node ()->decl
;
325 ctor
= ctor_for_folding (decl
);
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
);
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
))
338 gcc_assert (item
->type
== VAR
);
342 /* Returns varpool_node. */
343 inline varpool_node
*get_node (void)
345 return dyn_cast
<varpool_node
*> (node
);
348 /* Parser function that visits a varpool NODE. */
349 static sem_variable
*parse (varpool_node
*node
, bitmap_obstack
*stack
);
351 /* Variable constructor. */
355 /* Iterates though a constructor and identifies tree references
356 we are interested in semantic function equality. */
357 void parse_tree_refs (tree t
);
359 /* Compares trees T1 and T2 for semantic equality. */
360 static bool equals (tree t1
, tree t2
);
362 /* Compare that symbol sections are either NULL or have same name. */
363 bool compare_sections (sem_variable
*alias
);
365 }; // class sem_variable
367 class sem_item_optimizer
;
369 struct congruence_class_group
373 vec
<congruence_class
*> classes
;
376 /* Congruence class set structure. */
377 struct congruence_class_group_hash
: typed_noop_remove
<congruence_class_group
>
379 typedef congruence_class_group value_type
;
380 typedef congruence_class_group compare_type
;
382 static inline hashval_t
hash (const value_type
*item
)
387 static inline int equal (const value_type
*item1
, const compare_type
*item2
)
389 return item1
->hash
== item2
->hash
&& item1
->type
== item2
->type
;
393 struct traverse_split_pair
395 sem_item_optimizer
*optimizer
;
396 class congruence_class
*cls
;
399 /* Semantic item optimizer includes all top-level logic
400 related to semantic equality comparison. */
401 class sem_item_optimizer
404 sem_item_optimizer ();
405 ~sem_item_optimizer ();
407 /* Function responsible for visiting all potential functions and
408 read-only variables that can be merged. */
409 void parse_funcs_and_vars (void);
411 /* Optimizer entry point. */
417 /* Verify congruence classes if checking is enabled. */
418 void verify_classes (void);
420 /* Write IPA ICF summary for symbols. */
421 void write_summary (void);
423 /* Read IPA IPA ICF summary for symbols. */
424 void read_summary (void);
426 /* Callgraph removal hook called for a NODE with a custom DATA. */
427 static void cgraph_removal_hook (cgraph_node
*node
, void *data
);
429 /* Varpool removal hook called for a NODE with a custom DATA. */
430 static void varpool_removal_hook (varpool_node
*node
, void *data
);
432 /* Worklist of congruence classes that can potentially
433 refine classes of congruence. */
434 std::list
<congruence_class
*> worklist
;
436 /* Remove semantic ITEM and release memory. */
437 void remove_item (sem_item
*item
);
439 /* Remove symtab NODE triggered by symtab removal hooks. */
440 void remove_symtab_node (symtab_node
*node
);
442 /* Register callgraph and varpool hooks. */
443 void register_hooks (void);
445 /* Unregister callgraph and varpool hooks. */
446 void unregister_hooks (void);
448 /* Adds a CLS to hashtable associated by hash value. */
449 void add_class (congruence_class
*cls
);
451 /* Gets a congruence class group based on given HASH value and TYPE. */
452 congruence_class_group
*get_group_by_hash (hashval_t hash
,
457 /* Congruence classes are built by hash value. */
458 void build_hash_based_classes (void);
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);
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);
468 /* Debug function prints all informations about congruence classes. */
469 void dump_cong_classes (void);
471 /* Build references according to call graph. */
472 void build_graph (void);
474 /* Iterative congruence reduction function. */
475 void process_cong_reduction (void);
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
480 void merge_classes (unsigned int prev_class_count
);
482 /* Adds a newly created congruence class CLS to worklist. */
483 void worklist_push (congruence_class
*cls
);
485 /* Pops a class from worklist. */
486 congruence_class
*worklist_pop ();
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
491 void do_congruence_step (congruence_class
*cls
);
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
);
497 /* Makes pairing between a congruence class CLS and semantic ITEM. */
498 static void add_item_to_class (congruence_class
*cls
, sem_item
*item
);
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
);
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
,
511 traverse_split_pair
*pair
);
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
,
518 /* Removes all callgraph and varpool nodes that are marked by symtab
520 void filter_removed_items (void);
522 /* Vector of semantic items. */
523 vec
<sem_item
*> m_items
;
525 /* A set containing all items removed by hooks. */
526 hash_set
<symtab_node
*> m_removed_items_set
;
528 /* Hashtable of congruence classes */
529 hash_table
<congruence_class_group_hash
> m_classes
;
531 /* Count of congruence classes. */
532 unsigned int m_classes_count
;
534 /* Map data structure maps symtab nodes to semantic items. */
535 hash_map
<symtab_node
*, sem_item
*> m_symtab_node_map
;
537 /* Set to true if a splitter class is removed. */
538 bool splitter_class_removed
;
540 /* Global unique class id counter. */
541 static unsigned int class_id
;
543 /* Callgraph node removal hook holder. */
544 cgraph_node_hook_list
*m_cgraph_node_hooks
;
546 /* Varpool node removal hook holder. */
547 varpool_node_hook_list
*m_varpool_node_hooks
;
550 bitmap_obstack m_bmstack
;
551 }; // class sem_item_optimizer
553 } // ipa_icf namespace