1 /* Interprocedural semantic function equality pass 2 Copyright (C) 2014-2016 Free Software Foundation, Inc. 3 4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz> 5 6 This file is part of GCC. 7 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. 12 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. 17 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/>. */ 21 22 namespace ipa_icf { 23 class 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. */ 28 class congruence_class 29 { 30 public: 31 /* Congruence class constructor for a new class with _ID. */ congruence_class(unsigned int _id)32 congruence_class (unsigned int _id): in_worklist (false), id(_id) 33 { 34 } 35 36 /* Destructor. */ ~congruence_class()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. */ 60 enum sem_item_type 61 { 62 FUNC, 63 VAR 64 }; 65 66 /* Class is container for address references for a symtab_node. */ 67 68 class symbol_compare_collection 69 { 70 public: 71 /* Constructor. */ 72 symbol_compare_collection (symtab_node *node); 73 74 /* Destructor. */ ~symbol_compare_collection()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 90 struct symbol_compare_hash : nofree_ptr_hash <symbol_compare_collection> 91 { 92 static hashval_t hashsymbol_compare_hash93 hash (value_type 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++) 99 hstate.add_int (v->m_references[i]->ultimate_alias_target ()->order); 100 101 hstate.add_int (v->m_interposables.length ()); 102 103 for (unsigned i = 0; i < v->m_interposables.length (); i++) 104 hstate.add_int (v->m_interposables[i]->ultimate_alias_target ()->order); 105 106 return hstate.end (); 107 } 108 109 static bool equalsymbol_compare_hash110 equal (value_type a, value_type b) 111 { 112 if (a->m_references.length () != b->m_references.length () 113 || a->m_interposables.length () != b->m_interposables.length ()) 114 return false; 115 116 for (unsigned i = 0; i < a->m_references.length (); i++) 117 if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1) 118 return false; 119 120 for (unsigned i = 0; i < a->m_interposables.length (); i++) 121 if (!a->m_interposables[i]->semantically_equivalent_p 122 (b->m_interposables[i])) 123 return false; 124 125 return true; 126 } 127 }; 128 129 130 /* Semantic item usage pair. */ 131 class sem_usage_pair 132 { 133 public: 134 /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */ 135 sem_usage_pair (sem_item *_item, unsigned int _index); 136 137 /* Target semantic item where an item is used. */ 138 sem_item *item; 139 140 /* Index of usage of such an item. */ 141 unsigned int index; 142 }; 143 144 /* Semantic item is a base class that encapsulates all shared functionality 145 for both semantic function and variable items. */ 146 class sem_item 147 { 148 public: 149 /* Semantic item constructor for a node of _TYPE, where STACK is used 150 for bitmap memory allocation. */ 151 sem_item (sem_item_type _type, bitmap_obstack *stack); 152 153 /* Semantic item constructor for a node of _TYPE, where STACK is used 154 for bitmap memory allocation. The item is based on symtab node _NODE. */ 155 sem_item (sem_item_type _type, symtab_node *_node, bitmap_obstack *stack); 156 157 virtual ~sem_item (); 158 159 /* Dump function for debugging purpose. */ 160 DEBUG_FUNCTION void dump (void); 161 162 /* Initialize semantic item by info reachable during LTO WPA phase. */ 163 virtual void init_wpa (void) = 0; 164 165 /* Semantic item initialization function. */ 166 virtual void init (void) = 0; 167 168 /* Add reference to a semantic TARGET. */ 169 void add_reference (sem_item *target); 170 171 /* Fast equality function based on knowledge known in WPA. */ 172 virtual bool equals_wpa (sem_item *item, 173 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0; 174 175 /* Returns true if the item equals to ITEM given as arguemnt. */ 176 virtual bool equals (sem_item *item, 177 hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0; 178 179 /* References independent hash function. */ 180 virtual hashval_t get_hash (void) = 0; 181 182 /* Set new hash value of the item. */ 183 void set_hash (hashval_t hash); 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 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 201 /* Return base tree that can be used for compatible_types_p and 202 contains_polymorphic_type_p comparison. */ 203 static bool get_base_types (tree *t1, tree *t2); 204 205 /* Return true if target supports alias symbols. */ 206 bool target_supports_symbol_aliases_p (void); 207 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 238 /* Temporary hash used where hash values of references are added. */ 239 hashval_t global_hash; 240 protected: 241 /* Cached, once calculated hash for the item. */ 242 243 /* Accumulate to HSTATE a hash of expression EXP. */ 244 static void add_expr (const_tree exp, inchash::hash &hstate); 245 /* Accumulate to HSTATE a hash of type T. */ 246 static void add_type (const_tree t, inchash::hash &hstate); 247 248 /* Compare properties of symbol that does not affect semantics of symbol 249 itself but affects semantics of its references. 250 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR. */ 251 static bool compare_referenced_symbol_properties (symtab_node *used_by, 252 symtab_node *n1, 253 symtab_node *n2, 254 bool address); 255 256 /* Compare two attribute lists. */ 257 static bool compare_attributes (const_tree list1, const_tree list2); 258 259 /* Hash properties compared by compare_referenced_symbol_properties. */ 260 void hash_referenced_symbol_properties (symtab_node *ref, 261 inchash::hash &hstate, 262 bool address); 263 264 /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs 265 point to a same function. Comparison can be skipped if IGNORED_NODES 266 contains these nodes. ADDRESS indicate if address is taken. */ 267 bool compare_symbol_references (hash_map <symtab_node *, sem_item *> 268 &ignored_nodes, 269 symtab_node *n1, symtab_node *n2, 270 bool address); 271 protected: 272 /* Hash of item. */ 273 hashval_t m_hash; 274 275 /* Indicated whether a hash value has been set or not. */ 276 bool m_hash_set; 277 278 private: 279 /* Initialize internal data structures. Bitmap STACK is used for 280 bitmap memory allocation process. */ 281 void setup (bitmap_obstack *stack); 282 }; // class sem_item 283 284 class sem_function: public sem_item 285 { 286 public: 287 /* Semantic function constructor that uses STACK as bitmap memory stack. */ 288 sem_function (bitmap_obstack *stack); 289 290 /* Constructor based on callgraph node _NODE. 291 Bitmap STACK is used for memory allocation. */ 292 sem_function (cgraph_node *_node, bitmap_obstack *stack); 293 294 ~sem_function (); 295 init_wpa(void)296 inline virtual void init_wpa (void) 297 { 298 } 299 300 virtual void init (void); 301 virtual bool equals_wpa (sem_item *item, 302 hash_map <symtab_node *, sem_item *> &ignored_nodes); 303 virtual hashval_t get_hash (void); 304 virtual bool equals (sem_item *item, 305 hash_map <symtab_node *, sem_item *> &ignored_nodes); 306 virtual bool merge (sem_item *alias_item); 307 308 /* Dump symbol to FILE. */ dump_to_file(FILE * file)309 virtual void dump_to_file (FILE *file) 310 { 311 gcc_assert (file); 312 dump_function_to_file (decl, file, TDF_DETAILS); 313 } 314 315 /* Returns cgraph_node. */ get_node(void)316 inline cgraph_node *get_node (void) 317 { 318 return dyn_cast <cgraph_node *> (node); 319 } 320 321 /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */ 322 void hash_stmt (gimple *stmt, inchash::hash &inchash); 323 324 /* Return true if polymorphic comparison must be processed. */ 325 bool compare_polymorphic_p (void); 326 327 /* For a given call graph NODE, the function constructs new 328 semantic function item. */ 329 static sem_function *parse (cgraph_node *node, bitmap_obstack *stack); 330 331 /* Perform additional checks needed to match types of used function 332 paramters. */ 333 bool compatible_parm_types_p (tree, tree); 334 335 /* Exception handling region tree. */ 336 eh_region region_tree; 337 338 /* Number of function arguments. */ 339 unsigned int arg_count; 340 341 /* Total amount of edges in the function. */ 342 unsigned int edge_count; 343 344 /* Vector of sizes of all basic blocks. */ 345 vec <unsigned int> bb_sizes; 346 347 /* Control flow graph checksum. */ 348 hashval_t cfg_checksum; 349 350 /* GIMPLE codes hash value. */ 351 hashval_t gcode_hash; 352 353 /* Total number of SSA names used in the function. */ 354 unsigned ssa_names_size; 355 356 /* Array of structures for all basic blocks. */ 357 vec <ipa_icf_gimple::sem_bb *> bb_sorted; 358 359 /* Return true if parameter I may be used. */ 360 bool param_used_p (unsigned int i); 361 362 private: 363 /* Calculates hash value based on a BASIC_BLOCK. */ 364 hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block); 365 366 /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC), 367 true value is returned if phi nodes are semantically 368 equivalent in these blocks . */ 369 bool compare_phi_node (basic_block bb1, basic_block bb2); 370 371 /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB 372 corresponds to TARGET. */ 373 bool bb_dict_test (vec<int> *bb_dict, int source, int target); 374 375 /* If cgraph edges E1 and E2 are indirect calls, verify that 376 ICF flags are the same. */ 377 bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2); 378 379 /* Processes function equality comparison. */ 380 bool equals_private (sem_item *item); 381 382 /* Returns true if tree T can be compared as a handled component. */ 383 static bool icf_handled_component_p (tree t); 384 385 /* Function checker stores binding between functions. */ 386 ipa_icf_gimple::func_checker *m_checker; 387 388 /* COMPARED_FUNC is a function that we compare to. */ 389 sem_function *m_compared_func; 390 }; // class sem_function 391 392 class sem_variable: public sem_item 393 { 394 public: 395 /* Semantic variable constructor that uses STACK as bitmap memory stack. */ 396 sem_variable (bitmap_obstack *stack); 397 398 /* Constructor based on callgraph node _NODE. 399 Bitmap STACK is used for memory allocation. */ 400 401 sem_variable (varpool_node *_node, bitmap_obstack *stack); 402 init_wpa(void)403 inline virtual void init_wpa (void) {} 404 405 /* Semantic variable initialization function. */ init(void)406 inline virtual void init (void) 407 { 408 decl = get_node ()->decl; 409 } 410 411 virtual hashval_t get_hash (void); 412 virtual bool merge (sem_item *alias_item); 413 virtual void dump_to_file (FILE *file); 414 virtual bool equals (sem_item *item, 415 hash_map <symtab_node *, sem_item *> &ignored_nodes); 416 417 /* Fast equality variable based on knowledge known in WPA. */ 418 virtual bool equals_wpa (sem_item *item, 419 hash_map <symtab_node *, sem_item *> &ignored_nodes); 420 421 /* Returns varpool_node. */ get_node(void)422 inline varpool_node *get_node (void) 423 { 424 return dyn_cast <varpool_node *> (node); 425 } 426 427 /* Parser function that visits a varpool NODE. */ 428 static sem_variable *parse (varpool_node *node, bitmap_obstack *stack); 429 430 private: 431 /* Compares trees T1 and T2 for semantic equality. */ 432 static bool equals (tree t1, tree t2); 433 }; // class sem_variable 434 435 class sem_item_optimizer; 436 437 struct congruence_class_group 438 { 439 hashval_t hash; 440 sem_item_type type; 441 vec <congruence_class *> classes; 442 }; 443 444 /* Congruence class set structure. */ 445 struct congruence_class_group_hash : nofree_ptr_hash <congruence_class_group> 446 { hashcongruence_class_group_hash447 static inline hashval_t hash (const congruence_class_group *item) 448 { 449 return item->hash; 450 } 451 equalcongruence_class_group_hash452 static inline int equal (const congruence_class_group *item1, 453 const congruence_class_group *item2) 454 { 455 return item1->hash == item2->hash && item1->type == item2->type; 456 } 457 }; 458 459 struct traverse_split_pair 460 { 461 sem_item_optimizer *optimizer; 462 class congruence_class *cls; 463 }; 464 465 /* Semantic item optimizer includes all top-level logic 466 related to semantic equality comparison. */ 467 class sem_item_optimizer 468 { 469 public: 470 sem_item_optimizer (); 471 ~sem_item_optimizer (); 472 473 /* Function responsible for visiting all potential functions and 474 read-only variables that can be merged. */ 475 void parse_funcs_and_vars (void); 476 477 /* Optimizer entry point which returns true in case it processes 478 a merge operation. True is returned if there's a merge operation 479 processed. */ 480 bool execute (void); 481 482 /* Dump function. */ 483 void dump (void); 484 485 /* Verify congruence classes if checking is enabled. */ 486 void checking_verify_classes (void); 487 488 /* Verify congruence classes. */ 489 void verify_classes (void); 490 491 /* Write IPA ICF summary for symbols. */ 492 void write_summary (void); 493 494 /* Read IPA ICF summary for symbols. */ 495 void read_summary (void); 496 497 /* Callgraph removal hook called for a NODE with a custom DATA. */ 498 static void cgraph_removal_hook (cgraph_node *node, void *data); 499 500 /* Varpool removal hook called for a NODE with a custom DATA. */ 501 static void varpool_removal_hook (varpool_node *node, void *data); 502 503 /* Worklist of congruence classes that can potentially 504 refine classes of congruence. */ 505 std::list<congruence_class *> worklist; 506 507 /* Remove semantic ITEM and release memory. */ 508 void remove_item (sem_item *item); 509 510 /* Remove symtab NODE triggered by symtab removal hooks. */ 511 void remove_symtab_node (symtab_node *node); 512 513 /* Register callgraph and varpool hooks. */ 514 void register_hooks (void); 515 516 /* Unregister callgraph and varpool hooks. */ 517 void unregister_hooks (void); 518 519 /* Adds a CLS to hashtable associated by hash value. */ 520 void add_class (congruence_class *cls); 521 522 /* Gets a congruence class group based on given HASH value and TYPE. */ 523 congruence_class_group *get_group_by_hash (hashval_t hash, 524 sem_item_type type); 525 526 /* Because types can be arbitrarily large, avoid quadratic bottleneck. */ 527 hash_map<const_tree, hashval_t> m_type_hash_cache; 528 private: 529 530 /* For each semantic item, append hash values of references. */ 531 void update_hash_by_addr_refs (); 532 533 /* Congruence classes are built by hash value. */ 534 void build_hash_based_classes (void); 535 536 /* Semantic items in classes having more than one element and initialized. 537 In case of WPA, we load function body. */ 538 void parse_nonsingleton_classes (void); 539 540 /* Equality function for semantic items is used to subdivide existing 541 classes. If IN_WPA, fast equality function is invoked. */ 542 void subdivide_classes_by_equality (bool in_wpa = false); 543 544 /* Subdivide classes by address and interposable references 545 that members of the class reference. 546 Example can be a pair of functions that have an address 547 taken from a function. If these addresses are different the class 548 is split. */ 549 unsigned subdivide_classes_by_sensitive_refs(); 550 551 /* Debug function prints all informations about congruence classes. */ 552 void dump_cong_classes (void); 553 554 /* Build references according to call graph. */ 555 void build_graph (void); 556 557 /* Iterative congruence reduction function. */ 558 void process_cong_reduction (void); 559 560 /* After reduction is done, we can declare all items in a group 561 to be equal. PREV_CLASS_COUNT is start number of classes 562 before reduction. True is returned if there's a merge operation 563 processed. */ 564 bool merge_classes (unsigned int prev_class_count); 565 566 /* Adds a newly created congruence class CLS to worklist. */ 567 void worklist_push (congruence_class *cls); 568 569 /* Pops a class from worklist. */ 570 congruence_class *worklist_pop (); 571 572 /* Every usage of a congruence class CLS is a candidate that can split the 573 collection of classes. Bitmap stack BMSTACK is used for bitmap 574 allocation. */ 575 void do_congruence_step (congruence_class *cls); 576 577 /* Tests if a class CLS used as INDEXth splits any congruence classes. 578 Bitmap stack BMSTACK is used for bitmap allocation. */ 579 void do_congruence_step_for_index (congruence_class *cls, unsigned int index); 580 581 /* Makes pairing between a congruence class CLS and semantic ITEM. */ 582 static void add_item_to_class (congruence_class *cls, sem_item *item); 583 584 /* Disposes split map traverse function. CLS is congruence 585 class, BSLOT is bitmap slot we want to release. DATA is mandatory, 586 but unused argument. */ 587 static bool release_split_map (congruence_class * const &cls, bitmap const &b, 588 traverse_split_pair *pair); 589 590 /* Process split operation for a cognruence class CLS, 591 where bitmap B splits congruence class members. DATA is used 592 as argument of split pair. */ 593 static bool traverse_congruence_split (congruence_class * const &cls, 594 bitmap const &b, 595 traverse_split_pair *pair); 596 597 /* Reads a section from LTO stream file FILE_DATA. Input block for DATA 598 contains LEN bytes. */ 599 void read_section (lto_file_decl_data *file_data, const char *data, 600 size_t len); 601 602 /* Removes all callgraph and varpool nodes that are marked by symtab 603 as deleted. */ 604 void filter_removed_items (void); 605 606 /* Vector of semantic items. */ 607 vec <sem_item *> m_items; 608 609 /* A set containing all items removed by hooks. */ 610 hash_set <symtab_node *> m_removed_items_set; 611 612 /* Hashtable of congruence classes */ 613 hash_table <congruence_class_group_hash> m_classes; 614 615 /* Count of congruence classes. */ 616 unsigned int m_classes_count; 617 618 /* Map data structure maps symtab nodes to semantic items. */ 619 hash_map <symtab_node *, sem_item *> m_symtab_node_map; 620 621 /* Set to true if a splitter class is removed. */ 622 bool splitter_class_removed; 623 624 /* Global unique class id counter. */ 625 static unsigned int class_id; 626 627 /* Callgraph node removal hook holder. */ 628 cgraph_node_hook_list *m_cgraph_node_hooks; 629 630 /* Varpool node removal hook holder. */ 631 varpool_node_hook_list *m_varpool_node_hooks; 632 633 /* Bitmap stack. */ 634 bitmap_obstack m_bmstack; 635 }; // class sem_item_optimizer 636 637 } // ipa_icf namespace 638