ipa-icf.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Interprocedural semantic function equality pass
  2. Copyright (C) 2014-2015 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_hashmap_traits: default_hashmap_traits
  70. {
  71. static hashval_t
  72. hash (const symbol_compare_collection *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_keys (const symbol_compare_collection *a,
  85. const symbol_compare_collection *b)
  86. {
  87. if (a->m_references.length () != b->m_references.length ()
  88. || a->m_interposables.length () != b->m_interposables.length ())
  89. return false;
  90. for (unsigned i = 0; i < a->m_references.length (); i++)
  91. if (a->m_references[i]->equal_address_to (b->m_references[i]) != 1)
  92. return false;
  93. for (unsigned i = 0; i < a->m_interposables.length (); i++)
  94. if (!a->m_interposables[i]->semantically_equivalent_p
  95. (b->m_interposables[i]))
  96. return false;
  97. return true;
  98. }
  99. };
  100. /* Semantic item usage pair. */
  101. class sem_usage_pair
  102. {
  103. public:
  104. /* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
  105. sem_usage_pair (sem_item *_item, unsigned int _index);
  106. /* Target semantic item where an item is used. */
  107. sem_item *item;
  108. /* Index of usage of such an item. */
  109. unsigned int index;
  110. };
  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. with computed _HASH. */
  122. sem_item (sem_item_type _type, symtab_node *_node, hashval_t _hash,
  123. bitmap_obstack *stack);
  124. virtual ~sem_item ();
  125. /* Dump function for debugging purpose. */
  126. DEBUG_FUNCTION void dump (void);
  127. /* Initialize semantic item by info reachable during LTO WPA phase. */
  128. virtual void init_wpa (void) = 0;
  129. /* Semantic item initialization function. */
  130. virtual void init (void) = 0;
  131. /* Add reference to a semantic TARGET. */
  132. void add_reference (sem_item *target);
  133. /* Fast equality function based on knowledge known in WPA. */
  134. virtual bool equals_wpa (sem_item *item,
  135. hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
  136. /* Returns true if the item equals to ITEM given as arguemnt. */
  137. virtual bool equals (sem_item *item,
  138. hash_map <symtab_node *, sem_item *> &ignored_nodes) = 0;
  139. /* References independent hash function. */
  140. virtual hashval_t get_hash (void) = 0;
  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. /* Hash of item. */
  179. hashval_t hash;
  180. /* Temporary hash used where hash values of references are added. */
  181. hashval_t global_hash;
  182. protected:
  183. /* Cached, once calculated hash for the item. */
  184. /* Accumulate to HSTATE a hash of expression EXP. */
  185. static void add_expr (const_tree exp, inchash::hash &hstate);
  186. /* Accumulate to HSTATE a hash of type T. */
  187. static void add_type (const_tree t, inchash::hash &hstate);
  188. /* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
  189. point to a same function. Comparison can be skipped if IGNORED_NODES
  190. contains these nodes. ADDRESS indicate if address is taken. */
  191. bool compare_cgraph_references (hash_map <symtab_node *, sem_item *>
  192. &ignored_nodes,
  193. symtab_node *n1, symtab_node *n2,
  194. bool address);
  195. private:
  196. /* Initialize internal data structures. Bitmap STACK is used for
  197. bitmap memory allocation process. */
  198. void setup (bitmap_obstack *stack);
  199. }; // class sem_item
  200. class sem_function: public sem_item
  201. {
  202. public:
  203. /* Semantic function constructor that uses STACK as bitmap memory stack. */
  204. sem_function (bitmap_obstack *stack);
  205. /* Constructor based on callgraph node _NODE with computed hash _HASH.
  206. Bitmap STACK is used for memory allocation. */
  207. sem_function (cgraph_node *_node, hashval_t _hash, bitmap_obstack *stack);
  208. ~sem_function ();
  209. inline virtual void init_wpa (void)
  210. {
  211. }
  212. virtual void init (void);
  213. virtual bool equals_wpa (sem_item *item,
  214. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  215. virtual hashval_t get_hash (void);
  216. virtual bool equals (sem_item *item,
  217. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  218. virtual bool merge (sem_item *alias_item);
  219. /* Dump symbol to FILE. */
  220. virtual void dump_to_file (FILE *file)
  221. {
  222. gcc_assert (file);
  223. dump_function_to_file (decl, file, TDF_DETAILS);
  224. }
  225. /* Returns cgraph_node. */
  226. inline cgraph_node *get_node (void)
  227. {
  228. return dyn_cast <cgraph_node *> (node);
  229. }
  230. /* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
  231. void hash_stmt (gimple stmt, inchash::hash &inchash);
  232. /* Return true if polymorphic comparison must be processed. */
  233. bool compare_polymorphic_p (void);
  234. /* For a given call graph NODE, the function constructs new
  235. semantic function item. */
  236. static sem_function *parse (cgraph_node *node, bitmap_obstack *stack);
  237. /* Perform additional checks needed to match types of used function
  238. paramters. */
  239. bool compatible_parm_types_p (tree, tree);
  240. /* Exception handling region tree. */
  241. eh_region region_tree;
  242. /* Number of function arguments. */
  243. unsigned int arg_count;
  244. /* Total amount of edges in the function. */
  245. unsigned int edge_count;
  246. /* Vector of sizes of all basic blocks. */
  247. vec <unsigned int> bb_sizes;
  248. /* Control flow graph checksum. */
  249. hashval_t cfg_checksum;
  250. /* GIMPLE codes hash value. */
  251. hashval_t gcode_hash;
  252. /* Total number of SSA names used in the function. */
  253. unsigned ssa_names_size;
  254. /* Array of structures for all basic blocks. */
  255. vec <ipa_icf_gimple::sem_bb *> bb_sorted;
  256. /* Return true if parameter I may be used. */
  257. bool param_used_p (unsigned int i);
  258. private:
  259. /* Calculates hash value based on a BASIC_BLOCK. */
  260. hashval_t get_bb_hash (const ipa_icf_gimple::sem_bb *basic_block);
  261. /* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
  262. true value is returned if phi nodes are semantically
  263. equivalent in these blocks . */
  264. bool compare_phi_node (basic_block bb1, basic_block bb2);
  265. /* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
  266. corresponds to TARGET. */
  267. bool bb_dict_test (vec<int> *bb_dict, int source, int target);
  268. /* If cgraph edges E1 and E2 are indirect calls, verify that
  269. ICF flags are the same. */
  270. bool compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2);
  271. /* Processes function equality comparison. */
  272. bool equals_private (sem_item *item,
  273. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  274. /* Returns true if tree T can be compared as a handled component. */
  275. static bool icf_handled_component_p (tree t);
  276. /* Function checker stores binding between functions. */
  277. ipa_icf_gimple::func_checker *m_checker;
  278. /* COMPARED_FUNC is a function that we compare to. */
  279. sem_function *m_compared_func;
  280. }; // class sem_function
  281. class sem_variable: public sem_item
  282. {
  283. public:
  284. /* Semantic variable constructor that uses STACK as bitmap memory stack. */
  285. sem_variable (bitmap_obstack *stack);
  286. /* Constructor based on callgraph node _NODE with computed hash _HASH.
  287. Bitmap STACK is used for memory allocation. */
  288. sem_variable (varpool_node *_node, hashval_t _hash, bitmap_obstack *stack);
  289. inline virtual void init_wpa (void) {}
  290. /* Semantic variable initialization function. */
  291. inline virtual void init (void)
  292. {
  293. decl = get_node ()->decl;
  294. }
  295. virtual hashval_t get_hash (void);
  296. virtual bool merge (sem_item *alias_item);
  297. virtual void dump_to_file (FILE *file);
  298. virtual bool equals (sem_item *item,
  299. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  300. /* Fast equality variable based on knowledge known in WPA. */
  301. virtual bool equals_wpa (sem_item *item,
  302. hash_map <symtab_node *, sem_item *> &ignored_nodes);
  303. /* Returns varpool_node. */
  304. inline varpool_node *get_node (void)
  305. {
  306. return dyn_cast <varpool_node *> (node);
  307. }
  308. /* Parser function that visits a varpool NODE. */
  309. static sem_variable *parse (varpool_node *node, bitmap_obstack *stack);
  310. private:
  311. /* Compares trees T1 and T2 for semantic equality. */
  312. static bool equals (tree t1, tree t2);
  313. }; // class sem_variable
  314. class sem_item_optimizer;
  315. struct congruence_class_group
  316. {
  317. hashval_t hash;
  318. sem_item_type type;
  319. vec <congruence_class *> classes;
  320. };
  321. /* Congruence class set structure. */
  322. struct congruence_class_group_hash: typed_noop_remove <congruence_class_group>
  323. {
  324. typedef congruence_class_group value_type;
  325. typedef congruence_class_group compare_type;
  326. static inline hashval_t hash (const value_type *item)
  327. {
  328. return item->hash;
  329. }
  330. static inline int equal (const value_type *item1, const compare_type *item2)
  331. {
  332. return item1->hash == item2->hash && item1->type == item2->type;
  333. }
  334. };
  335. struct traverse_split_pair
  336. {
  337. sem_item_optimizer *optimizer;
  338. class congruence_class *cls;
  339. };
  340. /* Semantic item optimizer includes all top-level logic
  341. related to semantic equality comparison. */
  342. class sem_item_optimizer
  343. {
  344. public:
  345. sem_item_optimizer ();
  346. ~sem_item_optimizer ();
  347. /* Function responsible for visiting all potential functions and
  348. read-only variables that can be merged. */
  349. void parse_funcs_and_vars (void);
  350. /* Optimizer entry point which returns true in case it processes
  351. a merge operation. True is returned if there's a merge operation
  352. processed. */
  353. bool execute (void);
  354. /* Dump function. */
  355. void dump (void);
  356. /* Verify congruence classes if checking is enabled. */
  357. void verify_classes (void);
  358. /* Write IPA ICF summary for symbols. */
  359. void write_summary (void);
  360. /* Read IPA IPA ICF summary for symbols. */
  361. void read_summary (void);
  362. /* Callgraph removal hook called for a NODE with a custom DATA. */
  363. static void cgraph_removal_hook (cgraph_node *node, void *data);
  364. /* Varpool removal hook called for a NODE with a custom DATA. */
  365. static void varpool_removal_hook (varpool_node *node, void *data);
  366. /* Worklist of congruence classes that can potentially
  367. refine classes of congruence. */
  368. std::list<congruence_class *> worklist;
  369. /* Remove semantic ITEM and release memory. */
  370. void remove_item (sem_item *item);
  371. /* Remove symtab NODE triggered by symtab removal hooks. */
  372. void remove_symtab_node (symtab_node *node);
  373. /* Register callgraph and varpool hooks. */
  374. void register_hooks (void);
  375. /* Unregister callgraph and varpool hooks. */
  376. void unregister_hooks (void);
  377. /* Adds a CLS to hashtable associated by hash value. */
  378. void add_class (congruence_class *cls);
  379. /* Gets a congruence class group based on given HASH value and TYPE. */
  380. congruence_class_group *get_group_by_hash (hashval_t hash,
  381. sem_item_type type);
  382. /* Because types can be arbitrarily large, avoid quadratic bottleneck. */
  383. hash_map<const_tree, hashval_t> m_type_hash_cache;
  384. private:
  385. /* For each semantic item, append hash values of references. */
  386. void update_hash_by_addr_refs ();
  387. /* Congruence classes are built by hash value. */
  388. void build_hash_based_classes (void);
  389. /* Semantic items in classes having more than one element and initialized.
  390. In case of WPA, we load function body. */
  391. void parse_nonsingleton_classes (void);
  392. /* Equality function for semantic items is used to subdivide existing
  393. classes. If IN_WPA, fast equality function is invoked. */
  394. void subdivide_classes_by_equality (bool in_wpa = false);
  395. /* Subdivide classes by address and interposable references
  396. that members of the class reference.
  397. Example can be a pair of functions that have an address
  398. taken from a function. If these addresses are different the class
  399. is split. */
  400. unsigned subdivide_classes_by_sensitive_refs();
  401. /* Debug function prints all informations about congruence classes. */
  402. void dump_cong_classes (void);
  403. /* Build references according to call graph. */
  404. void build_graph (void);
  405. /* Iterative congruence reduction function. */
  406. void process_cong_reduction (void);
  407. /* After reduction is done, we can declare all items in a group
  408. to be equal. PREV_CLASS_COUNT is start number of classes
  409. before reduction. True is returned if there's a merge operation
  410. processed. */
  411. bool merge_classes (unsigned int prev_class_count);
  412. /* Adds a newly created congruence class CLS to worklist. */
  413. void worklist_push (congruence_class *cls);
  414. /* Pops a class from worklist. */
  415. congruence_class *worklist_pop ();
  416. /* Every usage of a congruence class CLS is a candidate that can split the
  417. collection of classes. Bitmap stack BMSTACK is used for bitmap
  418. allocation. */
  419. void do_congruence_step (congruence_class *cls);
  420. /* Tests if a class CLS used as INDEXth splits any congruence classes.
  421. Bitmap stack BMSTACK is used for bitmap allocation. */
  422. void do_congruence_step_for_index (congruence_class *cls, unsigned int index);
  423. /* Makes pairing between a congruence class CLS and semantic ITEM. */
  424. static void add_item_to_class (congruence_class *cls, sem_item *item);
  425. /* Disposes split map traverse function. CLS is congruence
  426. class, BSLOT is bitmap slot we want to release. DATA is mandatory,
  427. but unused argument. */
  428. static bool release_split_map (congruence_class * const &cls, bitmap const &b,
  429. traverse_split_pair *pair);
  430. /* Process split operation for a cognruence class CLS,
  431. where bitmap B splits congruence class members. DATA is used
  432. as argument of split pair. */
  433. static bool traverse_congruence_split (congruence_class * const &cls,
  434. bitmap const &b,
  435. traverse_split_pair *pair);
  436. /* Reads a section from LTO stream file FILE_DATA. Input block for DATA
  437. contains LEN bytes. */
  438. void read_section (lto_file_decl_data *file_data, const char *data,
  439. size_t len);
  440. /* Removes all callgraph and varpool nodes that are marked by symtab
  441. as deleted. */
  442. void filter_removed_items (void);
  443. /* Vector of semantic items. */
  444. vec <sem_item *> m_items;
  445. /* A set containing all items removed by hooks. */
  446. hash_set <symtab_node *> m_removed_items_set;
  447. /* Hashtable of congruence classes */
  448. hash_table <congruence_class_group_hash> m_classes;
  449. /* Count of congruence classes. */
  450. unsigned int m_classes_count;
  451. /* Map data structure maps symtab nodes to semantic items. */
  452. hash_map <symtab_node *, sem_item *> m_symtab_node_map;
  453. /* Set to true if a splitter class is removed. */
  454. bool splitter_class_removed;
  455. /* Global unique class id counter. */
  456. static unsigned int class_id;
  457. /* Callgraph node removal hook holder. */
  458. cgraph_node_hook_list *m_cgraph_node_hooks;
  459. /* Varpool node removal hook holder. */
  460. varpool_node_hook_list *m_varpool_node_hooks;
  461. /* Bitmap stack. */
  462. bitmap_obstack m_bmstack;
  463. }; // class sem_item_optimizer
  464. } // ipa_icf namespace