ipa-icf.h 20 KB

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