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