ipa-icf.h 19 KB

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