1 /* Interprocedural semantic function equality pass
2 Copyright (C) 2014-2015 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 /* Class is container for address references for a symtab_node. */
68 class symbol_compare_collection
72 symbol_compare_collection (symtab_node
*node
);
75 ~symbol_compare_collection ()
77 m_references
.release ();
78 m_interposables
.release ();
81 /* Vector of address references. */
82 vec
<symtab_node
*> m_references
;
84 /* Vector of interposable references. */
85 vec
<symtab_node
*> m_interposables
;
88 /* Hash traits for symbol_compare_collection map. */
90 struct symbol_compare_hashmap_traits
: default_hashmap_traits
93 hash (const symbol_compare_collection
*v
)
96 hstate
.add_int (v
->m_references
.length ());
98 for (unsigned i
= 0; i
< v
->m_references
.length (); i
++)
99 hstate
.add_ptr (v
->m_references
[i
]->ultimate_alias_target ());
101 hstate
.add_int (v
->m_interposables
.length ());
103 for (unsigned i
= 0; i
< v
->m_interposables
.length (); i
++)
104 hstate
.add_ptr (v
->m_interposables
[i
]->ultimate_alias_target ());
106 return hstate
.end ();
110 equal_keys (const symbol_compare_collection
*a
,
111 const symbol_compare_collection
*b
)
113 if (a
->m_references
.length () != b
->m_references
.length ()
114 || a
->m_interposables
.length () != b
->m_interposables
.length ())
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)
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
]))
131 /* Semantic item usage pair. */
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
);
138 /* Target semantic item where an item is used. */
141 /* Index of usage of such an item. */
145 /* Semantic item is a base class that encapsulates all shared functionality
146 for both semantic function and variable items. */
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
);
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
);
160 virtual ~sem_item ();
162 /* Dump function for debugging purpose. */
163 DEBUG_FUNCTION
void dump (void);
165 /* Initialize semantic item by info reachable during LTO WPA phase. */
166 virtual void init_wpa (void) = 0;
168 /* Semantic item initialization function. */
169 virtual void init (void) = 0;
171 /* Add reference to a semantic TARGET. */
172 void add_reference (sem_item
*target
);
174 /* Gets symbol name of the item. */
175 const char *name (void)
177 return node
->name ();
180 /* Gets assembler name of the item. */
181 const char *asm_name (void)
183 return node
->asm_name ();
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;
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;
194 /* References independent hash function. */
195 virtual hashval_t
get_hash (void) = 0;
197 /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
199 virtual bool merge (sem_item
*alias_item
) = 0;
201 /* Dump symbol to FILE. */
202 virtual void dump_to_file (FILE *file
) = 0;
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
);
208 /* Return true if target supports alias symbols. */
209 bool target_supports_symbol_aliases_p (void);
217 /* Declaration tree node. */
220 /* Semantic references used that generate congruence groups. */
221 vec
<sem_item
*> refs
;
223 /* Pointer to a congruence class the item belongs to. */
224 congruence_class
*cls
;
226 /* Index of the item in a class belonging to. */
227 unsigned int index_in_class
;
229 /* List of semantic items where the instance is used. */
230 vec
<sem_usage_pair
*> usages
;
232 /* A bitmap with indices of all classes referencing this item. */
233 bitmap usage_index_bitmap
;
235 /* List of tree references (either FUNC_DECL or VAR_DECL). */
236 vec
<tree
> tree_refs
;
238 /* A set with symbol table references. */
239 hash_set
<symtab_node
*> refs_set
;
242 /* Cached, once calculated hash for the item. */
245 /* Accumulate to HSTATE a hash of constructor expression EXP. */
246 static void add_expr (const_tree exp
, inchash::hash
&hstate
);
248 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
249 point to a same function. Comparison can be skipped if IGNORED_NODES
250 contains these nodes. ADDRESS indicate if address is taken. */
251 bool compare_cgraph_references (hash_map
<symtab_node
*, sem_item
*>
253 symtab_node
*n1
, symtab_node
*n2
,
257 /* Initialize internal data structures. Bitmap STACK is used for
258 bitmap memory allocation process. */
259 void setup (bitmap_obstack
*stack
);
262 class sem_function
: public sem_item
265 /* Semantic function constructor that uses STACK as bitmap memory stack. */
266 sem_function (bitmap_obstack
*stack
);
268 /* Constructor based on callgraph node _NODE with computed hash _HASH.
269 Bitmap STACK is used for memory allocation. */
270 sem_function (cgraph_node
*_node
, hashval_t _hash
, bitmap_obstack
*stack
);
274 inline virtual void init_wpa (void)
279 virtual void init (void);
280 virtual bool equals_wpa (sem_item
*item
,
281 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
282 virtual hashval_t
get_hash (void);
283 virtual bool equals (sem_item
*item
,
284 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
285 virtual bool merge (sem_item
*alias_item
);
287 /* Dump symbol to FILE. */
288 virtual void dump_to_file (FILE *file
)
291 dump_function_to_file (decl
, file
, TDF_DETAILS
);
294 /* Parses function arguments and result type. */
295 void parse_tree_args (void);
297 /* Returns cgraph_node. */
298 inline cgraph_node
*get_node (void)
300 return dyn_cast
<cgraph_node
*> (node
);
303 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
304 void hash_stmt (gimple stmt
, inchash::hash
&inchash
);
306 /* Return true if polymorphic comparison must be processed. */
307 bool compare_polymorphic_p (void);
309 /* For a given call graph NODE, the function constructs new
310 semantic function item. */
311 static sem_function
*parse (cgraph_node
*node
, bitmap_obstack
*stack
);
313 /* Exception handling region tree. */
314 eh_region region_tree
;
316 /* Result type tree node. */
319 /* Array of argument tree types. */
320 vec
<tree
> arg_types
;
322 /* Number of function arguments. */
323 unsigned int arg_count
;
325 /* Total amount of edges in the function. */
326 unsigned int edge_count
;
328 /* Vector of sizes of all basic blocks. */
329 vec
<unsigned int> bb_sizes
;
331 /* Control flow graph checksum. */
332 hashval_t cfg_checksum
;
334 /* GIMPLE codes hash value. */
335 hashval_t gcode_hash
;
337 /* Total number of SSA names used in the function. */
338 unsigned ssa_names_size
;
340 /* Array of structures for all basic blocks. */
341 vec
<ipa_icf_gimple::sem_bb
*> bb_sorted
;
344 /* Calculates hash value based on a BASIC_BLOCK. */
345 hashval_t
get_bb_hash (const ipa_icf_gimple::sem_bb
*basic_block
);
347 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
348 true value is returned if phi nodes are semantically
349 equivalent in these blocks . */
350 bool compare_phi_node (basic_block bb1
, basic_block bb2
);
352 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
353 corresponds to TARGET. */
354 bool bb_dict_test (vec
<int> *bb_dict
, int source
, int target
);
356 /* If cgraph edges E1 and E2 are indirect calls, verify that
357 ICF flags are the same. */
358 bool compare_edge_flags (cgraph_edge
*e1
, cgraph_edge
*e2
);
360 /* Processes function equality comparison. */
361 bool equals_private (sem_item
*item
,
362 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
364 /* Returns true if tree T can be compared as a handled component. */
365 static bool icf_handled_component_p (tree t
);
367 /* Function checker stores binding between functions. */
368 ipa_icf_gimple::func_checker
*m_checker
;
370 /* COMPARED_FUNC is a function that we compare to. */
371 sem_function
*m_compared_func
;
372 }; // class sem_function
374 class sem_variable
: public sem_item
377 /* Semantic variable constructor that uses STACK as bitmap memory stack. */
378 sem_variable (bitmap_obstack
*stack
);
380 /* Constructor based on callgraph node _NODE with computed hash _HASH.
381 Bitmap STACK is used for memory allocation. */
383 sem_variable (varpool_node
*_node
, hashval_t _hash
, bitmap_obstack
*stack
);
385 inline virtual void init_wpa (void) {}
387 /* Semantic variable initialization function. */
388 inline virtual void init (void)
390 decl
= get_node ()->decl
;
393 virtual hashval_t
get_hash (void);
394 virtual bool merge (sem_item
*alias_item
);
395 virtual void dump_to_file (FILE *file
);
396 virtual bool equals (sem_item
*item
,
397 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
399 /* Fast equality variable based on knowledge known in WPA. */
400 virtual bool equals_wpa (sem_item
*item
,
401 hash_map
<symtab_node
*, sem_item
*> &ignored_nodes
);
403 /* Returns varpool_node. */
404 inline varpool_node
*get_node (void)
406 return dyn_cast
<varpool_node
*> (node
);
409 /* Parser function that visits a varpool NODE. */
410 static sem_variable
*parse (varpool_node
*node
, bitmap_obstack
*stack
);
413 /* Compares trees T1 and T2 for semantic equality. */
414 static bool equals (tree t1
, tree t2
);
415 }; // class sem_variable
417 class sem_item_optimizer
;
419 struct congruence_class_group
423 vec
<congruence_class
*> classes
;
426 /* Congruence class set structure. */
427 struct congruence_class_group_hash
: typed_noop_remove
<congruence_class_group
>
429 typedef congruence_class_group value_type
;
430 typedef congruence_class_group compare_type
;
432 static inline hashval_t
hash (const value_type
*item
)
437 static inline int equal (const value_type
*item1
, const compare_type
*item2
)
439 return item1
->hash
== item2
->hash
&& item1
->type
== item2
->type
;
443 struct traverse_split_pair
445 sem_item_optimizer
*optimizer
;
446 class congruence_class
*cls
;
449 /* Semantic item optimizer includes all top-level logic
450 related to semantic equality comparison. */
451 class sem_item_optimizer
454 sem_item_optimizer ();
455 ~sem_item_optimizer ();
457 /* Function responsible for visiting all potential functions and
458 read-only variables that can be merged. */
459 void parse_funcs_and_vars (void);
461 /* Optimizer entry point which returns true in case it processes
462 a merge operation. True is returned if there's a merge operation
469 /* Verify congruence classes if checking is enabled. */
470 void verify_classes (void);
472 /* Write IPA ICF summary for symbols. */
473 void write_summary (void);
475 /* Read IPA IPA ICF summary for symbols. */
476 void read_summary (void);
478 /* Callgraph removal hook called for a NODE with a custom DATA. */
479 static void cgraph_removal_hook (cgraph_node
*node
, void *data
);
481 /* Varpool removal hook called for a NODE with a custom DATA. */
482 static void varpool_removal_hook (varpool_node
*node
, void *data
);
484 /* Worklist of congruence classes that can potentially
485 refine classes of congruence. */
486 std::list
<congruence_class
*> worklist
;
488 /* Remove semantic ITEM and release memory. */
489 void remove_item (sem_item
*item
);
491 /* Remove symtab NODE triggered by symtab removal hooks. */
492 void remove_symtab_node (symtab_node
*node
);
494 /* Register callgraph and varpool hooks. */
495 void register_hooks (void);
497 /* Unregister callgraph and varpool hooks. */
498 void unregister_hooks (void);
500 /* Adds a CLS to hashtable associated by hash value. */
501 void add_class (congruence_class
*cls
);
503 /* Gets a congruence class group based on given HASH value and TYPE. */
504 congruence_class_group
*get_group_by_hash (hashval_t hash
,
509 /* Congruence classes are built by hash value. */
510 void build_hash_based_classes (void);
512 /* Semantic items in classes having more than one element and initialized.
513 In case of WPA, we load function body. */
514 void parse_nonsingleton_classes (void);
516 /* Equality function for semantic items is used to subdivide existing
517 classes. If IN_WPA, fast equality function is invoked. */
518 void subdivide_classes_by_equality (bool in_wpa
= false);
520 /* Subdivide classes by address and interposable references
521 that members of the class reference.
522 Example can be a pair of functions that have an address
523 taken from a function. If these addresses are different the class
525 unsigned subdivide_classes_by_sensitive_refs();
527 /* Debug function prints all informations about congruence classes. */
528 void dump_cong_classes (void);
530 /* Build references according to call graph. */
531 void build_graph (void);
533 /* Iterative congruence reduction function. */
534 void process_cong_reduction (void);
536 /* After reduction is done, we can declare all items in a group
537 to be equal. PREV_CLASS_COUNT is start number of classes
538 before reduction. True is returned if there's a merge operation
540 bool merge_classes (unsigned int prev_class_count
);
542 /* Adds a newly created congruence class CLS to worklist. */
543 void worklist_push (congruence_class
*cls
);
545 /* Pops a class from worklist. */
546 congruence_class
*worklist_pop ();
548 /* Every usage of a congruence class CLS is a candidate that can split the
549 collection of classes. Bitmap stack BMSTACK is used for bitmap
551 void do_congruence_step (congruence_class
*cls
);
553 /* Tests if a class CLS used as INDEXth splits any congruence classes.
554 Bitmap stack BMSTACK is used for bitmap allocation. */
555 void do_congruence_step_for_index (congruence_class
*cls
, unsigned int index
);
557 /* Makes pairing between a congruence class CLS and semantic ITEM. */
558 static void add_item_to_class (congruence_class
*cls
, sem_item
*item
);
560 /* Disposes split map traverse function. CLS is congruence
561 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
562 but unused argument. */
563 static bool release_split_map (congruence_class
* const &cls
, bitmap
const &b
,
564 traverse_split_pair
*pair
);
566 /* Process split operation for a cognruence class CLS,
567 where bitmap B splits congruence class members. DATA is used
568 as argument of split pair. */
569 static bool traverse_congruence_split (congruence_class
* const &cls
,
571 traverse_split_pair
*pair
);
573 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
574 contains LEN bytes. */
575 void read_section (lto_file_decl_data
*file_data
, const char *data
,
578 /* Removes all callgraph and varpool nodes that are marked by symtab
580 void filter_removed_items (void);
582 /* Vector of semantic items. */
583 vec
<sem_item
*> m_items
;
585 /* A set containing all items removed by hooks. */
586 hash_set
<symtab_node
*> m_removed_items_set
;
588 /* Hashtable of congruence classes */
589 hash_table
<congruence_class_group_hash
> m_classes
;
591 /* Count of congruence classes. */
592 unsigned int m_classes_count
;
594 /* Map data structure maps symtab nodes to semantic items. */
595 hash_map
<symtab_node
*, sem_item
*> m_symtab_node_map
;
597 /* Set to true if a splitter class is removed. */
598 bool splitter_class_removed
;
600 /* Global unique class id counter. */
601 static unsigned int class_id
;
603 /* Callgraph node removal hook holder. */
604 cgraph_node_hook_list
*m_cgraph_node_hooks
;
606 /* Varpool node removal hook holder. */
607 varpool_node_hook_list
*m_varpool_node_hooks
;
610 bitmap_obstack m_bmstack
;
611 }; // class sem_item_optimizer
613 } // ipa_icf namespace