您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

667 行
20KB

  1. /* Interprocedural semantic function equality pass
  2. Copyright (C) 2014-2020 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. namespace ipa_icf {
  17. class sem_item;
  18. /* Congruence class encompasses a collection of either functions or
  19. read-only variables. These items are considered to be equivalent
  20. if not proved the opposite. */
  21. class congruence_class
  22. {
  23. public:
  24. /* Congruence class constructor for a new class with _ID. */
  25. congruence_class (unsigned int _id): in_worklist (false), id (_id),
  26. referenced_by_count (0)
  27. {
  28. }
  29. /* Destructor. */
  30. ~congruence_class ()
  31. {
  32. }
  33. /* Dump function prints all class members to a FILE with an INDENT. */
  34. void dump (FILE *file, unsigned int indent = 0) const;
  35. /* Returns true if there's a member that is used from another group. */
  36. bool is_class_used (void);
  37. /* Flag is used in case we want to remove a class from worklist and
  38. delete operation is quite expensive for
  39. the data structure (linked list). */
  40. bool in_worklist;
  41. /* Vector of all group members. */
  42. auto_vec <sem_item *> members;
  43. /* Global unique class identifier. */
  44. unsigned int id;
  45. /* Total number of references to items of this class. */
  46. unsigned referenced_by_count;
  47. };
  48. /* Semantic item type enum. */
  49. enum sem_item_type
  50. {
  51. FUNC,
  52. VAR
  53. };
  54. /* Class is container for address references for a symtab_node. */
  55. class symbol_compare_collection
  56. {
  57. public:
  58. /* Constructor. */
  59. symbol_compare_collection (symtab_node *node);
  60. /* Destructor. */
  61. ~symbol_compare_collection ()
  62. {
  63. m_references.release ();
  64. m_interposables.release ();
  65. }
  66. /* Vector of address references. */
  67. vec<symtab_node *> m_references;
  68. /* Vector of interposable references. */
  69. vec<symtab_node *> m_interposables;
  70. };
  71. /* Hash traits for symbol_compare_collection map. */
  72. struct symbol_compare_hash : nofree_ptr_hash <symbol_compare_collection>
  73. {
  74. static hashval_t
  75. hash (value_type v)
  76. {
  77. inchash::hash hstate;
  78. hstate.add_int (v->m_references.length ());
  79. for (unsigned i = 0; i < v->m_references.length (); i++)
  80. hstate.add_int (v->m_references[i]->ultimate_alias_target ()->order);
  81. hstate.add_int (v->m_interposables.length ());
  82. for (unsigned i = 0; i < v->m_interposables.length (); i++)
  83. hstate.add_int (v->m_interposables[i]->ultimate_alias_target ()->order);
  84. return hstate.end ();
  85. }
  86. static bool
  87. equal (value_type a, value_type b)
  88. {
  89. if (a->m_references.length () != b->m_references.length ()
  90. || a->m_interposables.length () != b->m_interposables.length ())
  91. return false;
  92. for (unsigned i = 0; i < a->m_references.length (); i++)
  93. if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1)
  94. return false;
  95. for (unsigned i = 0; i < a->m_interposables.length (); i++)
  96. if (!a->m_interposables[i]->semantically_equivalent_p
  97. (b->m_interposables[i]))
  98. return false;
  99. return true;
  100. }
  101. };
  102. /* Semantic item usage pair. */
  103. class sem_usage_pair
  104. {
  105. public:
  106. /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
  107. sem_usage_pair (sem_item *_item, unsigned int _index);
  108. /* Target semantic item where an item is used. */
  109. sem_item *item;
  110. /* Index of usage of such an item. */
  111. unsigned int index;
  112. };
  113. struct sem_usage_pair_hash : pointer_hash <sem_usage_pair>
  114. {
  115. static inline hashval_t hash (sem_usage_pair *);
  116. static inline bool equal (sem_usage_pair *, sem_usage_pair *);
  117. };
  118. inline hashval_t
  119. sem_usage_pair_hash::hash (sem_usage_pair *pair)
  120. {
  121. inchash::hash hstate;
  122. hstate.add_ptr (pair->item);
  123. hstate.add_int (pair->index);
  124. return hstate.end ();
  125. }
  126. inline bool
  127. sem_usage_pair_hash::equal (sem_usage_pair *p1, sem_usage_pair *p2)
  128. {
  129. return p1->item == p2->item && p1->index == p2->index;
  130. }
  131. struct sem_usage_hash : sem_usage_pair_hash, typed_delete_remove <sem_usage_pair> {};
  132. typedef hash_map<sem_usage_hash, auto_vec<sem_item *> > ref_map;
  133. typedef std::pair<symtab_node *, symtab_node *> symtab_pair;
  134. /* Semantic item is a base class that encapsulates all shared functionality
  135. for both semantic function and variable items. */
  136. class sem_item
  137. {
  138. public:
  139. /* Semantic item constructor for a node of _TYPE, where STACK is used
  140. for bitmap memory allocation. */
  141. sem_item (sem_item_type _type, bitmap_obstack *stack);
  142. /* Semantic item constructor for a node of _TYPE, where STACK is used
  143. for bitmap memory allocation. The item is based on symtab node _NODE. */
  144. sem_item (sem_item_type _type, symtab_node *_node, bitmap_obstack *stack);
  145. virtual ~sem_item ();
  146. /* Dump function for debugging purpose. */
  147. DEBUG_FUNCTION void dump (void);
  148. /* Semantic item initialization function. */
  149. virtual void init (ipa_icf_gimple::func_checker *) = 0;
  150. /* Add reference to a semantic TARGET. */
  151. void add_reference (ref_map *map, sem_item *target);
  152. /* Fast equality function based on knowledge known in WPA. */
  153. virtual bool equals_wpa (sem_item *item,
  154. hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
  155. /* Returns true if the item equals to ITEM given as argument. */
  156. virtual bool equals (sem_item *item,
  157. hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
  158. /* References independent hash function. */
  159. virtual hashval_t get_hash (void) = 0;
  160. /* Set new hash value of the item. */
  161. void set_hash (hashval_t hash);
  162. /* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
  163. be applied. */
  164. virtual bool merge (sem_item *alias_item) = 0;
  165. /* Dump symbol to FILE. */
  166. virtual void dump_to_file (FILE *file) = 0;
  167. /* Update hash by address sensitive references. */
  168. void update_hash_by_addr_refs (hash_map <symtab_node *,
  169. sem_item *> &m_symtab_node_map);
  170. /* Update hash by computed local hash values taken from different
  171. semantic items. */
  172. void update_hash_by_local_refs (hash_map <symtab_node *,
  173. sem_item *> &m_symtab_node_map);
  174. /* Return base tree that can be used for compatible_types_p and
  175. contains_polymorphic_type_p comparison. */
  176. static bool get_base_types (tree *t1, tree *t2);
  177. /* Return true if target supports alias symbols. */
  178. bool target_supports_symbol_aliases_p (void);
  179. /* Item type. */
  180. sem_item_type type;
  181. /* Symtab node. */
  182. symtab_node *node;
  183. /* Declaration tree node. */
  184. tree decl;
  185. /* Number of references to a semantic symbols (function calls,
  186. variable references). */
  187. unsigned reference_count;
  188. /* Pointer to a congruence class the item belongs to. */
  189. congruence_class *cls;
  190. /* Index of the item in a class belonging to. */
  191. unsigned int index_in_class;
  192. /* A bitmap with indices of all classes referencing this item. */
  193. bitmap usage_index_bitmap;
  194. /* List of tree references (either FUNC_DECL or VAR_DECL). */
  195. vec <tree> tree_refs;
  196. /* A set with symbol table references. */
  197. hash_set <symtab_node *> refs_set;
  198. /* Temporary hash used where hash values of references are added. */
  199. hashval_t global_hash;
  200. /* Number of references to this symbol. */
  201. unsigned referenced_by_count;
  202. protected:
  203. /* Cached, once calculated hash for the item. */
  204. /* Compare properties of symbol that does not affect semantics of symbol
  205. itself but affects semantics of its references.
  206. If ADDRESS is true, do extra checking needed for IPA_REF_ADDR. */
  207. static bool compare_referenced_symbol_properties (symtab_node *used_by,
  208. symtab_node *n1,
  209. symtab_node *n2,
  210. bool address);
  211. /* Hash properties compared by compare_referenced_symbol_properties. */
  212. void hash_referenced_symbol_properties (symtab_node *ref,
  213. inchash::hash &hstate,
  214. bool address);
  215. /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
  216. point to a same function. Comparison can be skipped if IGNORED_NODES
  217. contains these nodes. ADDRESS indicate if address is taken. */
  218. bool compare_symbol_references (hash_map <symtab_node *, sem_item *>
  219. &ignored_nodes,
  220. symtab_node *n1, symtab_node *n2,
  221. bool address);
  222. protected:
  223. /* Hash of item. */
  224. hashval_t m_hash;
  225. /* Indicated whether a hash value has been set or not. */
  226. bool m_hash_set;
  227. private:
  228. /* Initialize internal data structures. Bitmap STACK is used for
  229. bitmap memory allocation process. */
  230. void setup (bitmap_obstack *stack);
  231. /* Because types can be arbitrarily large, avoid quadratic bottleneck. */
  232. static hash_map<const_tree, hashval_t> m_type_hash_cache;
  233. }; // class sem_item
  234. class sem_function: public sem_item
  235. {
  236. public:
  237. /* Semantic function constructor that uses STACK as bitmap memory stack. */
  238. sem_function (bitmap_obstack *stack);
  239. /* Constructor based on callgraph node _NODE.
  240. Bitmap STACK is used for memory allocation. */
  241. sem_function (cgraph_node *_node, bitmap_obstack *stack);
  242. ~sem_function ();
  243. virtual void init (ipa_icf_gimple::func_checker *);
  244. virtual bool equals_wpa (sem_item *item,
  245. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  246. virtual hashval_t get_hash (void);
  247. virtual bool equals (sem_item *item,
  248. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  249. virtual bool merge (sem_item *alias_item);
  250. /* Dump symbol to FILE. */
  251. virtual void dump_to_file (FILE *file)
  252. {
  253. gcc_assert (file);
  254. dump_function_to_file (decl, file, TDF_DETAILS);
  255. }
  256. /* Returns cgraph_node. */
  257. inline cgraph_node *get_node (void)
  258. {
  259. return dyn_cast <cgraph_node *> (node);
  260. }
  261. /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
  262. void hash_stmt (gimple *stmt, inchash::hash &inchash);
  263. /* Return true if polymorphic comparison must be processed. */
  264. bool compare_polymorphic_p (void);
  265. /* For a given call graph NODE, the function constructs new
  266. semantic function item. */
  267. static sem_function *parse (cgraph_node *node, bitmap_obstack *stack,
  268. ipa_icf_gimple::func_checker *checker);
  269. /* Perform additional checks needed to match types of used function
  270. parameters. */
  271. bool compatible_parm_types_p (tree, tree);
  272. /* Exception handling region tree. */
  273. eh_region region_tree;
  274. /* Number of function arguments. */
  275. unsigned int arg_count;
  276. /* Total amount of edges in the function. */
  277. unsigned int edge_count;
  278. /* Vector of sizes of all basic blocks. */
  279. vec <unsigned int> bb_sizes;
  280. /* Control flow graph checksum. */
  281. hashval_t cfg_checksum;
  282. /* GIMPLE codes hash value. */
  283. hashval_t gcode_hash;
  284. /* Total number of SSA names used in the function. */
  285. unsigned ssa_names_size;
  286. /* Array of structures for all basic blocks. */
  287. vec <ipa_icf_gimple::sem_bb *> bb_sorted;
  288. /* Return true if parameter I may be used. */
  289. bool param_used_p (unsigned int i);
  290. private:
  291. /* Calculates hash value based on a BASIC_BLOCK. */
  292. hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
  293. /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
  294. true value is returned if phi nodes are semantically
  295. equivalent in these blocks . */
  296. bool compare_phi_node (basic_block bb1, basic_block bb2);
  297. /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
  298. corresponds to TARGET. */
  299. bool bb_dict_test (vec<int> *bb_dict, int source, int target);
  300. /* If cgraph edges E1 and E2 are indirect calls, verify that
  301. ICF flags are the same. */
  302. bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
  303. /* Processes function equality comparison. */
  304. bool equals_private (sem_item *item);
  305. /* Function checker stores binding between functions. */
  306. ipa_icf_gimple::func_checker *m_checker;
  307. /* COMPARED_FUNC is a function that we compare to. */
  308. sem_function *m_compared_func;
  309. }; // class sem_function
  310. class sem_variable: public sem_item
  311. {
  312. public:
  313. /* Semantic variable constructor that uses STACK as bitmap memory stack. */
  314. sem_variable (bitmap_obstack *stack);
  315. /* Constructor based on callgraph node _NODE.
  316. Bitmap STACK is used for memory allocation. */
  317. sem_variable (varpool_node *_node, bitmap_obstack *stack);
  318. /* Semantic variable initialization function. */
  319. virtual void init (ipa_icf_gimple::func_checker *);
  320. virtual hashval_t get_hash (void);
  321. virtual bool merge (sem_item *alias_item);
  322. virtual void dump_to_file (FILE *file);
  323. virtual bool equals (sem_item *item,
  324. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  325. /* Fast equality variable based on knowledge known in WPA. */
  326. virtual bool equals_wpa (sem_item *item,
  327. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  328. /* Returns varpool_node. */
  329. inline varpool_node *get_node (void)
  330. {
  331. return dyn_cast <varpool_node *> (node);
  332. }
  333. /* Parser function that visits a varpool NODE. */
  334. static sem_variable *parse (varpool_node *node, bitmap_obstack *stack,
  335. ipa_icf_gimple::func_checker *checker);
  336. private:
  337. /* Compares trees T1 and T2 for semantic equality. */
  338. static bool equals (tree t1, tree t2);
  339. }; // class sem_variable
  340. class sem_item_optimizer;
  341. struct congruence_class_group
  342. {
  343. hashval_t hash;
  344. sem_item_type type;
  345. vec <congruence_class *> classes;
  346. };
  347. /* Congruence class set structure. */
  348. struct congruence_class_hash : nofree_ptr_hash <congruence_class_group>
  349. {
  350. static inline hashval_t hash (const congruence_class_group *item)
  351. {
  352. return item->hash;
  353. }
  354. static inline int equal (const congruence_class_group *item1,
  355. const congruence_class_group *item2)
  356. {
  357. return item1->hash == item2->hash && item1->type == item2->type;
  358. }
  359. };
  360. struct traverse_split_pair
  361. {
  362. sem_item_optimizer *optimizer;
  363. class congruence_class *cls;
  364. };
  365. /* Semantic item optimizer includes all top-level logic
  366. related to semantic equality comparison. */
  367. class sem_item_optimizer
  368. {
  369. public:
  370. sem_item_optimizer ();
  371. ~sem_item_optimizer ();
  372. /* Function responsible for visiting all potential functions and
  373. read-only variables that can be merged. */
  374. void parse_funcs_and_vars (void);
  375. /* Optimizer entry point which returns true in case it processes
  376. a merge operation. True is returned if there's a merge operation
  377. processed. */
  378. bool execute (void);
  379. /* Dump function. */
  380. void dump (void);
  381. /* Verify congruence classes if checking is enabled. */
  382. void checking_verify_classes (void);
  383. /* Verify congruence classes. */
  384. void verify_classes (void);
  385. /* Write IPA ICF summary for symbols. */
  386. void write_summary (void);
  387. /* Read IPA ICF summary for symbols. */
  388. void read_summary (void);
  389. /* Callgraph removal hook called for a NODE with a custom DATA. */
  390. static void cgraph_removal_hook (cgraph_node *node, void *data);
  391. /* Varpool removal hook called for a NODE with a custom DATA. */
  392. static void varpool_removal_hook (varpool_node *node, void *data);
  393. /* Worklist of congruence classes that can potentially
  394. refine classes of congruence. */
  395. fibonacci_heap<unsigned, congruence_class> worklist;
  396. /* Remove semantic ITEM and release memory. */
  397. void remove_item (sem_item *item);
  398. /* Remove symtab NODE triggered by symtab removal hooks. */
  399. void remove_symtab_node (symtab_node *node);
  400. /* Register callgraph and varpool hooks. */
  401. void register_hooks (void);
  402. /* Unregister callgraph and varpool hooks. */
  403. void unregister_hooks (void);
  404. /* Adds a CLS to hashtable associated by hash value. */
  405. void add_class (congruence_class *cls);
  406. /* Gets a congruence class group based on given HASH value and TYPE. */
  407. congruence_class_group *get_group_by_hash (hashval_t hash,
  408. sem_item_type type);
  409. private:
  410. /* For each semantic item, append hash values of references. */
  411. void update_hash_by_addr_refs ();
  412. /* Congruence classes are built by hash value. */
  413. void build_hash_based_classes (void);
  414. /* Semantic items in classes having more than one element and initialized.
  415. In case of WPA, we load function body. */
  416. unsigned int parse_nonsingleton_classes (void);
  417. /* Equality function for semantic items is used to subdivide existing
  418. classes. If IN_WPA, fast equality function is invoked. */
  419. void subdivide_classes_by_equality (bool in_wpa = false);
  420. /* Subdivide classes by address and interposable references
  421. that members of the class reference.
  422. Example can be a pair of functions that have an address
  423. taken from a function. If these addresses are different the class
  424. is split. */
  425. unsigned subdivide_classes_by_sensitive_refs();
  426. /* Debug function prints all informations about congruence classes. */
  427. void dump_cong_classes (void);
  428. /* Build references according to call graph. */
  429. void build_graph (void);
  430. /* Iterative congruence reduction function. */
  431. void process_cong_reduction (void);
  432. /* After reduction is done, we can declare all items in a group
  433. to be equal. PREV_CLASS_COUNT is start number of classes
  434. before reduction. True is returned if there's a merge operation
  435. processed. LOADED_SYMBOLS is number of symbols that were loaded
  436. in WPA. */
  437. bool merge_classes (unsigned int prev_class_count,
  438. unsigned int loaded_symbols);
  439. /* Fixup points to analysis info. */
  440. void fixup_points_to_sets (void);
  441. /* Fixup points to set PT. */
  442. void fixup_pt_set (struct pt_solution *pt);
  443. /* Adds a newly created congruence class CLS to worklist. */
  444. void worklist_push (congruence_class *cls);
  445. /* Pops a class from worklist. */
  446. congruence_class *worklist_pop ();
  447. /* Every usage of a congruence class CLS is a candidate that can split the
  448. collection of classes. Bitmap stack BMSTACK is used for bitmap
  449. allocation. */
  450. void do_congruence_step (congruence_class *cls);
  451. /* Tests if a class CLS used as INDEXth splits any congruence classes.
  452. Bitmap stack BMSTACK is used for bitmap allocation. */
  453. bool do_congruence_step_for_index (congruence_class *cls, unsigned int index);
  454. /* Makes pairing between a congruence class CLS and semantic ITEM. */
  455. static void add_item_to_class (congruence_class *cls, sem_item *item);
  456. /* Disposes split map traverse function. CLS is congruence
  457. class, BSLOT is bitmap slot we want to release. DATA is mandatory,
  458. but unused argument. */
  459. static bool release_split_map (congruence_class * const &cls, bitmap const &b,
  460. traverse_split_pair *pair);
  461. /* Process split operation for a congruence class CLS,
  462. where bitmap B splits congruence class members. DATA is used
  463. as argument of split pair. */
  464. static bool traverse_congruence_split (congruence_class * const &cls,
  465. bitmap const &b,
  466. traverse_split_pair *pair);
  467. /* Compare function for sorting pairs in do_congruence_step_f. */
  468. static int sort_congruence_split (const void *, const void *);
  469. /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
  470. contains LEN bytes. */
  471. void read_section (lto_file_decl_data *file_data, const char *data,
  472. size_t len);
  473. /* Removes all callgraph and varpool nodes that are marked by symtab
  474. as deleted. */
  475. void filter_removed_items (void);
  476. /* Vector of semantic items. */
  477. vec <sem_item *> m_items;
  478. /* A set containing all items removed by hooks. */
  479. hash_set <symtab_node *> m_removed_items_set;
  480. /* Hashtable of congruence classes. */
  481. hash_table <congruence_class_hash> m_classes;
  482. /* Count of congruence classes. */
  483. unsigned int m_classes_count;
  484. /* Map data structure maps symtab nodes to semantic items. */
  485. hash_map <symtab_node *, sem_item *> m_symtab_node_map;
  486. /* Set to true if a splitter class is removed. */
  487. bool splitter_class_removed;
  488. /* Global unique class id counter. */
  489. static unsigned int class_id;
  490. /* Callgraph node removal hook holder. */
  491. cgraph_node_hook_list *m_cgraph_node_hooks;
  492. /* Varpool node removal hook holder. */
  493. varpool_node_hook_list *m_varpool_node_hooks;
  494. /* Bitmap stack. */
  495. bitmap_obstack m_bmstack;
  496. /* Vector of merged variables. Needed for fixup of points-to-analysis
  497. info. */
  498. vec <symtab_pair> m_merged_variables;
  499. /* Hash map will all references. */
  500. ref_map m_references;
  501. }; // class sem_item_optimizer
  502. } // ipa_icf namespace