]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-icf.c
re PR rtl-optimization/66351 (r223883 miscompiles stage2 compiler on ia64)
[thirdparty/gcc.git] / gcc / ipa-icf.c
CommitLineData
b84d4347 1/* Interprocedural Identical Code Folding pass
5624e564 2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
b84d4347
ML
3
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* Interprocedural Identical Code Folding for functions and
23 read-only variables.
24
25 The goal of this transformation is to discover functions and read-only
26 variables which do have exactly the same semantics.
27
28 In case of functions,
29 we could either create a virtual clone or do a simple function wrapper
30 that will call equivalent function. If the function is just locally visible,
31 all function calls can be redirected. For read-only variables, we create
32 aliases if possible.
33
34 Optimization pass arranges as follows:
35 1) All functions and read-only variables are visited and internal
36 data structure, either sem_function or sem_variables is created.
37 2) For every symbol from the previous step, VAR_DECL and FUNCTION_DECL are
38 saved and matched to corresponding sem_items.
39 3) These declaration are ignored for equality check and are solved
40 by Value Numbering algorithm published by Alpert, Zadeck in 1992.
41 4) We compute hash value for each symbol.
42 5) Congruence classes are created based on hash value. If hash value are
43 equal, equals function is called and symbols are deeply compared.
44 We must prove that all SSA names, declarations and other items
45 correspond.
46 6) Value Numbering is executed for these classes. At the end of the process
47 all symbol members in remaining classes can be merged.
48 7) Merge operation creates alias in case of read-only variables. For
49 callgraph node, we must decide if we can redirect local calls,
50 create an alias or a thunk.
51
52*/
53
54#include "config.h"
55#include "system.h"
46305737 56#include <list>
b84d4347 57#include "coretypes.h"
40e23961
MC
58#include "alias.h"
59#include "symtab.h"
60#include "options.h"
40e23961
MC
61#include "tree.h"
62#include "fold-const.h"
63#include "predict.h"
60393bbc
AM
64#include "tm.h"
65#include "hard-reg-set.h"
60393bbc
AM
66#include "function.h"
67#include "dominance.h"
68#include "cfg.h"
b84d4347
ML
69#include "basic-block.h"
70#include "tree-ssa-alias.h"
71#include "internal-fn.h"
72#include "gimple-expr.h"
b84d4347 73#include "gimple.h"
36566b39
PK
74#include "rtl.h"
75#include "flags.h"
36566b39
PK
76#include "insn-config.h"
77#include "expmed.h"
78#include "dojump.h"
79#include "explow.h"
80#include "calls.h"
81#include "emit-rtl.h"
82#include "varasm.h"
83#include "stmt.h"
b84d4347
ML
84#include "expr.h"
85#include "gimple-iterator.h"
86#include "gimple-ssa.h"
87#include "tree-cfg.h"
88#include "tree-phinodes.h"
89#include "stringpool.h"
90#include "tree-ssanames.h"
91#include "tree-dfa.h"
92#include "tree-pass.h"
93#include "gimple-pretty-print.h"
c582198b
AM
94#include "plugin-api.h"
95#include "ipa-ref.h"
96#include "cgraph.h"
97#include "alloc-pool.h"
dd912cb8 98#include "symbol-summary.h"
c582198b 99#include "ipa-prop.h"
b84d4347
ML
100#include "ipa-inline.h"
101#include "cfgloop.h"
102#include "except.h"
b84d4347
ML
103#include "coverage.h"
104#include "attribs.h"
105#include "print-tree.h"
106#include "lto-streamer.h"
107#include "data-streamer.h"
108#include "ipa-utils.h"
b84d4347
ML
109#include "ipa-icf-gimple.h"
110#include "ipa-icf.h"
46305737 111#include "stor-layout.h"
7aeb92b4 112#include "dbgcnt.h"
b84d4347
ML
113
114using namespace ipa_icf_gimple;
115
116namespace ipa_icf {
5ebd0e61 117
69f6b1f4
JH
118/* Initialization and computation of symtab node hash, there data
119 are propagated later on. */
120
121static sem_item_optimizer *optimizer = NULL;
122
5ebd0e61
ML
123/* Constructor. */
124
125symbol_compare_collection::symbol_compare_collection (symtab_node *node)
126{
127 m_references.create (0);
128 m_interposables.create (0);
129
130 ipa_ref *ref;
131
132 if (is_a <varpool_node *> (node) && DECL_VIRTUAL_P (node->decl))
133 return;
134
977b01e3 135 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
5ebd0e61 136 {
5ebd0e61
ML
137 if (ref->address_matters_p ())
138 m_references.safe_push (ref->referred);
139
140 if (ref->referred->get_availability () <= AVAIL_INTERPOSABLE)
141 {
0a7246ee 142 if (ref->address_matters_p ())
5ebd0e61
ML
143 m_references.safe_push (ref->referred);
144 else
145 m_interposables.safe_push (ref->referred);
146 }
147 }
148
149 if (is_a <cgraph_node *> (node))
150 {
151 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
152
153 for (cgraph_edge *e = cnode->callees; e; e = e->next_callee)
154 if (e->callee->get_availability () <= AVAIL_INTERPOSABLE)
155 m_interposables.safe_push (e->callee);
156 }
157}
158
b84d4347
ML
159/* Constructor for key value pair, where _ITEM is key and _INDEX is a target. */
160
161sem_usage_pair::sem_usage_pair (sem_item *_item, unsigned int _index):
162 item (_item), index (_index)
163{
164}
165
166/* Semantic item constructor for a node of _TYPE, where STACK is used
167 for bitmap memory allocation. */
168
169sem_item::sem_item (sem_item_type _type,
170 bitmap_obstack *stack): type(_type), hash(0)
171{
172 setup (stack);
173}
174
175/* Semantic item constructor for a node of _TYPE, where STACK is used
176 for bitmap memory allocation. The item is based on symtab node _NODE
177 with computed _HASH. */
178
179sem_item::sem_item (sem_item_type _type, symtab_node *_node,
180 hashval_t _hash, bitmap_obstack *stack): type(_type),
181 node (_node), hash (_hash)
182{
183 decl = node->decl;
184 setup (stack);
185}
186
187/* Add reference to a semantic TARGET. */
188
189void
190sem_item::add_reference (sem_item *target)
191{
192 refs.safe_push (target);
193 unsigned index = refs.length ();
194 target->usages.safe_push (new sem_usage_pair(this, index));
195 bitmap_set_bit (target->usage_index_bitmap, index);
196 refs_set.add (target->node);
197}
198
199/* Initialize internal data structures. Bitmap STACK is used for
200 bitmap memory allocation process. */
201
202void
203sem_item::setup (bitmap_obstack *stack)
204{
205 gcc_checking_assert (node);
206
207 refs.create (0);
208 tree_refs.create (0);
209 usages.create (0);
210 usage_index_bitmap = BITMAP_ALLOC (stack);
211}
212
213sem_item::~sem_item ()
214{
215 for (unsigned i = 0; i < usages.length (); i++)
216 delete usages[i];
217
218 refs.release ();
219 tree_refs.release ();
220 usages.release ();
221
222 BITMAP_FREE (usage_index_bitmap);
223}
224
225/* Dump function for debugging purpose. */
226
227DEBUG_FUNCTION void
228sem_item::dump (void)
229{
230 if (dump_file)
231 {
232 fprintf (dump_file, "[%s] %s (%u) (tree:%p)\n", type == FUNC ? "func" : "var",
1aec2ecc 233 node->name(), node->order, (void *) node->decl);
b84d4347
ML
234 fprintf (dump_file, " hash: %u\n", get_hash ());
235 fprintf (dump_file, " references: ");
236
237 for (unsigned i = 0; i < refs.length (); i++)
1aec2ecc 238 fprintf (dump_file, "%s%s ", refs[i]->node->name (),
b84d4347
ML
239 i < refs.length() - 1 ? "," : "");
240
241 fprintf (dump_file, "\n");
242 }
243}
244
f657d665
ML
245/* Return true if target supports alias symbols. */
246
247bool
248sem_item::target_supports_symbol_aliases_p (void)
249{
250#if !defined (ASM_OUTPUT_DEF) || (!defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL))
251 return false;
252#else
253 return true;
254#endif
255}
256
b84d4347
ML
257/* Semantic function constructor that uses STACK as bitmap memory stack. */
258
259sem_function::sem_function (bitmap_obstack *stack): sem_item (FUNC, stack),
260 m_checker (NULL), m_compared_func (NULL)
261{
262 arg_types.create (0);
263 bb_sizes.create (0);
264 bb_sorted.create (0);
265}
266
267/* Constructor based on callgraph node _NODE with computed hash _HASH.
268 Bitmap STACK is used for memory allocation. */
269sem_function::sem_function (cgraph_node *node, hashval_t hash,
270 bitmap_obstack *stack):
271 sem_item (FUNC, node, hash, stack),
272 m_checker (NULL), m_compared_func (NULL)
273{
274 arg_types.create (0);
275 bb_sizes.create (0);
276 bb_sorted.create (0);
277}
278
279sem_function::~sem_function ()
280{
281 for (unsigned i = 0; i < bb_sorted.length (); i++)
e27d328a 282 delete (bb_sorted[i]);
b84d4347
ML
283
284 arg_types.release ();
285 bb_sizes.release ();
286 bb_sorted.release ();
287}
288
289/* Calculates hash value based on a BASIC_BLOCK. */
290
291hashval_t
292sem_function::get_bb_hash (const sem_bb *basic_block)
293{
294 inchash::hash hstate;
295
296 hstate.add_int (basic_block->nondbg_stmt_count);
297 hstate.add_int (basic_block->edge_count);
298
299 return hstate.end ();
300}
301
302/* References independent hash function. */
303
304hashval_t
305sem_function::get_hash (void)
306{
307 if(!hash)
308 {
309 inchash::hash hstate;
310 hstate.add_int (177454); /* Random number for function type. */
311
312 hstate.add_int (arg_count);
313 hstate.add_int (cfg_checksum);
314 hstate.add_int (gcode_hash);
315
316 for (unsigned i = 0; i < bb_sorted.length (); i++)
317 hstate.merge_hash (get_bb_hash (bb_sorted[i]));
318
319 for (unsigned i = 0; i < bb_sizes.length (); i++)
320 hstate.add_int (bb_sizes[i]);
321
69f6b1f4
JH
322
323 /* Add common features of declaration itself. */
324 if (DECL_FUNCTION_SPECIFIC_TARGET (decl))
325 hstate.add_wide_int
326 (cl_target_option_hash
327 (TREE_TARGET_OPTION (DECL_FUNCTION_SPECIFIC_TARGET (decl))));
328 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))
329 (cl_optimization_hash
330 (TREE_OPTIMIZATION (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (decl))));
69f6b1f4
JH
331 hstate.add_flag (DECL_CXX_CONSTRUCTOR_P (decl));
332 hstate.add_flag (DECL_CXX_DESTRUCTOR_P (decl));
333
b84d4347
ML
334 hash = hstate.end ();
335 }
336
337 return hash;
338}
339
b3587b99
JH
340/* Return ture if A1 and A2 represent equivalent function attribute lists.
341 Based on comp_type_attributes. */
342
343bool
344sem_item::compare_attributes (const_tree a1, const_tree a2)
345{
346 const_tree a;
347 if (a1 == a2)
348 return true;
349 for (a = a1; a != NULL_TREE; a = TREE_CHAIN (a))
350 {
351 const struct attribute_spec *as;
352 const_tree attr;
353
354 as = lookup_attribute_spec (get_attribute_name (a));
355 /* TODO: We can introduce as->affects_decl_identity
356 and as->affects_decl_reference_identity if attribute mismatch
357 gets a common reason to give up on merging. It may not be worth
358 the effort.
359 For example returns_nonnull affects only references, while
360 optimize attribute can be ignored because it is already lowered
361 into flags representation and compared separately. */
362 if (!as)
363 continue;
364
365 attr = lookup_attribute (as->name, CONST_CAST_TREE (a2));
366 if (!attr || !attribute_value_equal (a, attr))
367 break;
368 }
369 if (!a)
370 {
371 for (a = a2; a != NULL_TREE; a = TREE_CHAIN (a))
372 {
373 const struct attribute_spec *as;
374
375 as = lookup_attribute_spec (get_attribute_name (a));
376 if (!as)
377 continue;
378
379 if (!lookup_attribute (as->name, CONST_CAST_TREE (a1)))
380 break;
381 /* We don't need to compare trees again, as we did this
382 already in first loop. */
383 }
384 if (!a)
385 return true;
386 }
387 /* TODO: As in comp_type_attributes we may want to introduce target hook. */
388 return false;
389}
390
977b01e3
JH
391/* Compare properties of symbols N1 and N2 that does not affect semantics of
392 symbol itself but affects semantics of its references from USED_BY (which
393 may be NULL if it is unknown). If comparsion is false, symbols
394 can still be merged but any symbols referring them can't.
395
396 If ADDRESS is true, do extra checking needed for IPA_REF_ADDR.
397
398 TODO: We can also split attributes to those that determine codegen of
399 a function body/variable constructor itself and those that are used when
400 referring to it. */
401
402bool
403sem_item::compare_referenced_symbol_properties (symtab_node *used_by,
404 symtab_node *n1,
405 symtab_node *n2,
406 bool address)
407{
408 if (is_a <cgraph_node *> (n1))
409 {
410 /* Inline properties matters: we do now want to merge uses of inline
411 function to uses of normal function because inline hint would be lost.
412 We however can merge inline function to noinline because the alias
413 will keep its DECL_DECLARED_INLINE flag.
414
415 Also ignore inline flag when optimizing for size or when function
416 is known to not be inlinable.
417
418 TODO: the optimize_size checks can also be assumed to be true if
419 unit has no !optimize_size functions. */
420
421 if ((!used_by || address || !is_a <cgraph_node *> (used_by)
422 || !opt_for_fn (used_by->decl, optimize_size))
423 && !opt_for_fn (n1->decl, optimize_size)
424 && n1->get_availability () > AVAIL_INTERPOSABLE
425 && (!DECL_UNINLINABLE (n1->decl) || !DECL_UNINLINABLE (n2->decl)))
426 {
427 if (DECL_DISREGARD_INLINE_LIMITS (n1->decl)
428 != DECL_DISREGARD_INLINE_LIMITS (n2->decl))
429 return return_false_with_msg
430 ("DECL_DISREGARD_INLINE_LIMITS are different");
431
432 if (DECL_DECLARED_INLINE_P (n1->decl)
433 != DECL_DECLARED_INLINE_P (n2->decl))
434 return return_false_with_msg ("inline attributes are different");
435 }
436
437 if (DECL_IS_OPERATOR_NEW (n1->decl)
438 != DECL_IS_OPERATOR_NEW (n2->decl))
439 return return_false_with_msg ("operator new flags are different");
440 }
441
442 /* Merging two definitions with a reference to equivalent vtables, but
443 belonging to a different type may result in ipa-polymorphic-call analysis
444 giving a wrong answer about the dynamic type of instance. */
445 if (is_a <varpool_node *> (n1))
446 {
447 if ((DECL_VIRTUAL_P (n1->decl) || DECL_VIRTUAL_P (n2->decl))
448 && (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl)
449 || !types_must_be_same_for_odr (DECL_CONTEXT (n1->decl),
450 DECL_CONTEXT (n2->decl)))
451 && (!used_by || !is_a <cgraph_node *> (used_by) || address
452 || opt_for_fn (used_by->decl, flag_devirtualize)))
453 return return_false_with_msg
454 ("references to virtual tables can not be merged");
b3587b99
JH
455
456 if (address && DECL_ALIGN (n1->decl) != DECL_ALIGN (n2->decl))
457 return return_false_with_msg ("alignment mismatch");
458
459 /* For functions we compare attributes in equals_wpa, because we do
460 not know what attributes may cause codegen differences, but for
461 variables just compare attributes for references - the codegen
462 for constructors is affected only by those attributes that we lower
463 to explicit representation (such as DECL_ALIGN or DECL_SECTION). */
464 if (!compare_attributes (DECL_ATTRIBUTES (n1->decl),
465 DECL_ATTRIBUTES (n2->decl)))
466 return return_false_with_msg ("different var decl attributes");
467 if (comp_type_attributes (TREE_TYPE (n1->decl),
468 TREE_TYPE (n2->decl)) != 1)
469 return return_false_with_msg ("different var type attributes");
977b01e3
JH
470 }
471
472 /* When matching virtual tables, be sure to also match information
473 relevant for polymorphic call analysis. */
474 if (used_by && is_a <varpool_node *> (used_by)
475 && DECL_VIRTUAL_P (used_by->decl))
476 {
477 if (DECL_VIRTUAL_P (n1->decl) != DECL_VIRTUAL_P (n2->decl))
478 return return_false_with_msg ("virtual flag mismatch");
479 if (DECL_VIRTUAL_P (n1->decl) && is_a <cgraph_node *> (n1)
480 && (DECL_FINAL_P (n1->decl) != DECL_FINAL_P (n2->decl)))
481 return return_false_with_msg ("final flag mismatch");
482 }
483 return true;
484}
485
486/* Hash properties that are compared by compare_referenced_symbol_properties. */
487
488void
489sem_item::hash_referenced_symbol_properties (symtab_node *ref,
490 inchash::hash &hstate,
491 bool address)
492{
493 if (is_a <cgraph_node *> (ref))
494 {
d57c9945 495 if ((type != FUNC || address || !opt_for_fn (decl, optimize_size))
977b01e3
JH
496 && !opt_for_fn (ref->decl, optimize_size)
497 && !DECL_UNINLINABLE (ref->decl))
498 {
499 hstate.add_flag (DECL_DISREGARD_INLINE_LIMITS (ref->decl));
500 hstate.add_flag (DECL_DECLARED_INLINE_P (ref->decl));
501 }
502 hstate.add_flag (DECL_IS_OPERATOR_NEW (ref->decl));
503 }
504 else if (is_a <varpool_node *> (ref))
505 {
506 hstate.add_flag (DECL_VIRTUAL_P (ref->decl));
b3587b99
JH
507 if (address)
508 hstate.add_int (DECL_ALIGN (ref->decl));
977b01e3
JH
509 }
510}
511
512
b84d4347
ML
513/* For a given symbol table nodes N1 and N2, we check that FUNCTION_DECLs
514 point to a same function. Comparison can be skipped if IGNORED_NODES
b6cddc7f 515 contains these nodes. ADDRESS indicate if address is taken. */
b84d4347
ML
516
517bool
977b01e3 518sem_item::compare_symbol_references (
b6cddc7f
ML
519 hash_map <symtab_node *, sem_item *> &ignored_nodes,
520 symtab_node *n1, symtab_node *n2, bool address)
b84d4347 521{
b6cddc7f
ML
522 enum availability avail1, avail2;
523
fe75bd20
JH
524 if (n1 == n2)
525 return true;
526
523f0450
JH
527 /* Never match variable and function. */
528 if (is_a <varpool_node *> (n1) != is_a <varpool_node *> (n2))
529 return false;
530
977b01e3
JH
531 if (!compare_referenced_symbol_properties (node, n1, n2, address))
532 return false;
b6cddc7f
ML
533 if (address && n1->equal_address_to (n2) == 1)
534 return true;
535 if (!address && n1->semantically_equivalent_p (n2))
b84d4347
ML
536 return true;
537
b6cddc7f
ML
538 n1 = n1->ultimate_alias_target (&avail1);
539 n2 = n2->ultimate_alias_target (&avail2);
540
541 if (avail1 >= AVAIL_INTERPOSABLE && ignored_nodes.get (n1)
542 && avail2 >= AVAIL_INTERPOSABLE && ignored_nodes.get (n2))
543 return true;
b84d4347
ML
544
545 return return_false_with_msg ("different references");
546}
547
548/* If cgraph edges E1 and E2 are indirect calls, verify that
549 ECF flags are the same. */
550
551bool sem_function::compare_edge_flags (cgraph_edge *e1, cgraph_edge *e2)
552{
553 if (e1->indirect_info && e2->indirect_info)
554 {
555 int e1_flags = e1->indirect_info->ecf_flags;
556 int e2_flags = e2->indirect_info->ecf_flags;
557
558 if (e1_flags != e2_flags)
559 return return_false_with_msg ("ICF flags are different");
560 }
561 else if (e1->indirect_info || e2->indirect_info)
562 return false;
563
564 return true;
565}
566
b3587b99
JH
567/* Return true if parameter I may be used. */
568
569bool
570sem_function::param_used_p (unsigned int i)
571{
572 if (ipa_node_params_sum == NULL)
573 return false;
574
575 struct ipa_node_params *parms_info = IPA_NODE_REF (get_node ());
576
577 if (parms_info->descriptors.is_empty ()
578 || parms_info->descriptors.length () <= i)
579 return true;
580
581 return ipa_is_param_used (IPA_NODE_REF (get_node ()), i);
582}
583
b84d4347
ML
584/* Fast equality function based on knowledge known in WPA. */
585
586bool
587sem_function::equals_wpa (sem_item *item,
588 hash_map <symtab_node *, sem_item *> &ignored_nodes)
589{
590 gcc_assert (item->type == FUNC);
fb5c322e
JH
591 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
592 cgraph_node *cnode2 = dyn_cast <cgraph_node *> (item->node);
b84d4347
ML
593
594 m_compared_func = static_cast<sem_function *> (item);
595
596 if (arg_types.length () != m_compared_func->arg_types.length ())
597 return return_false_with_msg ("different number of arguments");
598
fb5c322e
JH
599 if (cnode->thunk.thunk_p != cnode2->thunk.thunk_p)
600 return return_false_with_msg ("thunk_p mismatch");
601
602 if (cnode->thunk.thunk_p)
603 {
604 if (cnode->thunk.fixed_offset != cnode2->thunk.fixed_offset)
605 return return_false_with_msg ("thunk fixed_offset mismatch");
606 if (cnode->thunk.virtual_value != cnode2->thunk.virtual_value)
607 return return_false_with_msg ("thunk virtual_value mismatch");
608 if (cnode->thunk.this_adjusting != cnode2->thunk.this_adjusting)
609 return return_false_with_msg ("thunk this_adjusting mismatch");
610 if (cnode->thunk.virtual_offset_p != cnode2->thunk.virtual_offset_p)
611 return return_false_with_msg ("thunk virtual_offset_p mismatch");
612 if (cnode->thunk.add_pointer_bounds_args
613 != cnode2->thunk.add_pointer_bounds_args)
614 return return_false_with_msg ("thunk add_pointer_bounds_args mismatch");
615 }
616
c4234544
JH
617 /* Compare special function DECL attributes. */
618 if (DECL_FUNCTION_PERSONALITY (decl)
619 != DECL_FUNCTION_PERSONALITY (item->decl))
620 return return_false_with_msg ("function personalities are different");
621
c4234544
JH
622 if (DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl)
623 != DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (item->decl))
624 return return_false_with_msg ("intrument function entry exit "
625 "attributes are different");
626
627 if (DECL_NO_LIMIT_STACK (decl) != DECL_NO_LIMIT_STACK (item->decl))
628 return return_false_with_msg ("no stack limit attributes are different");
629
060cfff4 630 if (DECL_CXX_CONSTRUCTOR_P (decl) != DECL_CXX_CONSTRUCTOR_P (item->decl))
69f6b1f4 631 return return_false_with_msg ("DECL_CXX_CONSTRUCTOR mismatch");
060cfff4
JH
632
633 if (DECL_CXX_DESTRUCTOR_P (decl) != DECL_CXX_DESTRUCTOR_P (item->decl))
69f6b1f4 634 return return_false_with_msg ("DECL_CXX_DESTRUCTOR mismatch");
060cfff4 635
b3587b99
JH
636 /* TODO: pure/const flags mostly matters only for references, except for
637 the fact that codegen takes LOOPING flag as a hint that loops are
638 finite. We may arrange the code to always pick leader that has least
639 specified flags and then this can go into comparing symbol properties. */
c4234544
JH
640 if (flags_from_decl_or_type (decl) != flags_from_decl_or_type (item->decl))
641 return return_false_with_msg ("decl_or_type flags are different");
642
060cfff4
JH
643 /* Do not match polymorphic constructors of different types. They calls
644 type memory location for ipa-polymorphic-call and we do not want
645 it to get confused by wrong type. */
646 if (DECL_CXX_CONSTRUCTOR_P (decl)
647 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
648 {
649 if (TREE_CODE (TREE_TYPE (item->decl)) != METHOD_TYPE)
650 return return_false_with_msg ("DECL_CXX_CONSTURCTOR type mismatch");
651 else if (!func_checker::compatible_polymorphic_types_p
70e7f2a2
JH
652 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
653 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
060cfff4
JH
654 return return_false_with_msg ("ctor polymorphic type mismatch");
655 }
656
c4234544
JH
657 /* Checking function TARGET and OPTIMIZATION flags. */
658 cl_target_option *tar1 = target_opts_for_fn (decl);
659 cl_target_option *tar2 = target_opts_for_fn (item->decl);
660
661 if (tar1 != tar2 && !cl_target_option_eq (tar1, tar2))
662 {
663 if (dump_file && (dump_flags & TDF_DETAILS))
664 {
665 fprintf (dump_file, "target flags difference");
666 cl_target_option_print_diff (dump_file, 2, tar1, tar2);
667 }
668
669 return return_false_with_msg ("Target flags are different");
670 }
671
672 cl_optimization *opt1 = opts_for_fn (decl);
673 cl_optimization *opt2 = opts_for_fn (item->decl);
674
675 if (opt1 != opt2 && memcmp (opt1, opt2, sizeof(cl_optimization)))
676 {
677 if (dump_file && (dump_flags & TDF_DETAILS))
678 {
679 fprintf (dump_file, "optimization flags difference");
680 cl_optimization_print_diff (dump_file, 2, opt1, opt2);
681 }
682
683 return return_false_with_msg ("optimization flags are different");
684 }
685
686 /* Result type checking. */
687 if (!func_checker::compatible_types_p (result_type,
688 m_compared_func->result_type))
689 return return_false_with_msg ("result types are different");
690
b84d4347
ML
691 /* Checking types of arguments. */
692 for (unsigned i = 0; i < arg_types.length (); i++)
693 {
694 /* This guard is here for function pointer with attributes (pr59927.c). */
695 if (!arg_types[i] || !m_compared_func->arg_types[i])
696 return return_false_with_msg ("NULL argument type");
697
b3587b99
JH
698 /* We always need to match types so we are sure the callin conventions
699 are compatible. */
b84d4347 700 if (!func_checker::compatible_types_p (arg_types[i],
060cfff4 701 m_compared_func->arg_types[i]))
b84d4347 702 return return_false_with_msg ("argument type is different");
b3587b99
JH
703
704 /* On used arguments we need to do a bit more of work. */
705 if (!param_used_p (i))
706 continue;
fe75bd20
JH
707 if (POINTER_TYPE_P (arg_types[i])
708 && (TYPE_RESTRICT (arg_types[i])
709 != TYPE_RESTRICT (m_compared_func->arg_types[i])))
710 return return_false_with_msg ("argument restrict flag mismatch");
977b01e3
JH
711 /* nonnull_arg_p implies non-zero range to REFERENCE types. */
712 if (POINTER_TYPE_P (arg_types[i])
713 && TREE_CODE (arg_types[i])
714 != TREE_CODE (m_compared_func->arg_types[i])
715 && opt_for_fn (decl, flag_delete_null_pointer_checks))
716 return return_false_with_msg ("pointer wrt reference mismatch");
b84d4347
ML
717 }
718
b84d4347
ML
719 if (node->num_references () != item->node->num_references ())
720 return return_false_with_msg ("different number of references");
721
b3587b99
JH
722 /* Checking function attributes.
723 This is quadratic in number of attributes */
fe75bd20
JH
724 if (comp_type_attributes (TREE_TYPE (decl),
725 TREE_TYPE (item->decl)) != 1)
726 return return_false_with_msg ("different type attributes");
b3587b99
JH
727 if (!compare_attributes (DECL_ATTRIBUTES (decl),
728 DECL_ATTRIBUTES (item->decl)))
729 return return_false_with_msg ("different decl attributes");
fe75bd20 730
060cfff4
JH
731 /* The type of THIS pointer type memory location for
732 ipa-polymorphic-call-analysis. */
733 if (opt_for_fn (decl, flag_devirtualize)
734 && (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
735 || TREE_CODE (TREE_TYPE (item->decl)) == METHOD_TYPE)
b3587b99 736 && param_used_p (0)
060cfff4
JH
737 && compare_polymorphic_p ())
738 {
739 if (TREE_CODE (TREE_TYPE (decl)) != TREE_CODE (TREE_TYPE (item->decl)))
740 return return_false_with_msg ("METHOD_TYPE and FUNCTION_TYPE mismatch");
741 if (!func_checker::compatible_polymorphic_types_p
70e7f2a2
JH
742 (TYPE_METHOD_BASETYPE (TREE_TYPE (decl)),
743 TYPE_METHOD_BASETYPE (TREE_TYPE (item->decl)), false))
060cfff4
JH
744 return return_false_with_msg ("THIS pointer ODR type mismatch");
745 }
746
b84d4347
ML
747 ipa_ref *ref = NULL, *ref2 = NULL;
748 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
749 {
750 item->node->iterate_reference (i, ref2);
751
b3587b99
JH
752 if (ref->use != ref2->use)
753 return return_false_with_msg ("reference use mismatch");
754
977b01e3 755 if (!compare_symbol_references (ignored_nodes, ref->referred,
b6cddc7f
ML
756 ref2->referred,
757 ref->address_matters_p ()))
b84d4347
ML
758 return false;
759 }
760
761 cgraph_edge *e1 = dyn_cast <cgraph_node *> (node)->callees;
762 cgraph_edge *e2 = dyn_cast <cgraph_node *> (item->node)->callees;
763
764 while (e1 && e2)
765 {
977b01e3 766 if (!compare_symbol_references (ignored_nodes, e1->callee,
b6cddc7f 767 e2->callee, false))
b84d4347 768 return false;
b3587b99
JH
769 if (!compare_edge_flags (e1, e2))
770 return false;
b84d4347
ML
771
772 e1 = e1->next_callee;
773 e2 = e2->next_callee;
774 }
775
776 if (e1 || e2)
b3587b99
JH
777 return return_false_with_msg ("different number of calls");
778
779 e1 = dyn_cast <cgraph_node *> (node)->indirect_calls;
780 e2 = dyn_cast <cgraph_node *> (item->node)->indirect_calls;
781
782 while (e1 && e2)
783 {
784 if (!compare_edge_flags (e1, e2))
785 return false;
786
787 e1 = e1->next_callee;
788 e2 = e2->next_callee;
789 }
790
791 if (e1 || e2)
792 return return_false_with_msg ("different number of indirect calls");
b84d4347
ML
793
794 return true;
795}
796
3ab93359
ML
797/* Update hash by address sensitive references. We iterate over all
798 sensitive references (address_matters_p) and we hash ultime alias
799 target of these nodes, which can improve a semantic item hash.
977b01e3
JH
800
801 Also hash in referenced symbols properties. This can be done at any time
802 (as the properties should not change), but it is convenient to do it here
803 while we walk the references anyway. */
3ab93359
ML
804
805void
806sem_item::update_hash_by_addr_refs (hash_map <symtab_node *,
807 sem_item *> &m_symtab_node_map)
808{
3ab93359
ML
809 ipa_ref* ref;
810 inchash::hash hstate (hash);
977b01e3
JH
811
812 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
3ab93359 813 {
b3587b99 814 hstate.add_int (ref->use);
977b01e3
JH
815 hash_referenced_symbol_properties (ref->referred, hstate,
816 ref->use == IPA_REF_ADDR);
3ab93359 817 if (ref->address_matters_p () || !m_symtab_node_map.get (ref->referred))
f45be077 818 hstate.add_int (ref->referred->ultimate_alias_target ()->order);
3ab93359
ML
819 }
820
821 if (is_a <cgraph_node *> (node))
822 {
823 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callers; e;
824 e = e->next_caller)
825 {
826 sem_item **result = m_symtab_node_map.get (e->callee);
977b01e3 827 hash_referenced_symbol_properties (e->callee, hstate, false);
3ab93359 828 if (!result)
f45be077 829 hstate.add_int (e->callee->ultimate_alias_target ()->order);
3ab93359
ML
830 }
831 }
832
833 hash = hstate.end ();
834}
835
836/* Update hash by computed local hash values taken from different
977b01e3
JH
837 semantic items.
838 TODO: stronger SCC based hashing would be desirable here. */
3ab93359
ML
839
840void
841sem_item::update_hash_by_local_refs (hash_map <symtab_node *,
842 sem_item *> &m_symtab_node_map)
843{
977b01e3 844 ipa_ref* ref;
3ab93359 845 inchash::hash state (hash);
977b01e3
JH
846
847 for (unsigned j = 0; node->iterate_reference (j, ref); j++)
3ab93359 848 {
3ab93359
ML
849 sem_item **result = m_symtab_node_map.get (ref->referring);
850 if (result)
c153ad03 851 state.merge_hash ((*result)->hash);
3ab93359
ML
852 }
853
854 if (type == FUNC)
855 {
856 for (cgraph_edge *e = dyn_cast <cgraph_node *> (node)->callees; e;
857 e = e->next_callee)
858 {
859 sem_item **result = m_symtab_node_map.get (e->caller);
860 if (result)
861 state.merge_hash ((*result)->hash);
862 }
863 }
864
865 global_hash = state.end ();
866}
867
b84d4347
ML
868/* Returns true if the item equals to ITEM given as argument. */
869
870bool
871sem_function::equals (sem_item *item,
fb5c322e 872 hash_map <symtab_node *, sem_item *> &)
b84d4347
ML
873{
874 gcc_assert (item->type == FUNC);
fb5c322e 875 bool eq = equals_private (item);
b84d4347
ML
876
877 if (m_checker != NULL)
878 {
879 delete m_checker;
880 m_checker = NULL;
881 }
882
883 if (dump_file && (dump_flags & TDF_DETAILS))
884 fprintf (dump_file,
885 "Equals called for:%s:%s (%u:%u) (%s:%s) with result: %s\n\n",
1aec2ecc
ML
886 xstrdup_for_dump (node->name()),
887 xstrdup_for_dump (item->node->name ()),
888 node->order,
889 item->node->order,
890 xstrdup_for_dump (node->asm_name ()),
891 xstrdup_for_dump (item->node->asm_name ()),
892 eq ? "true" : "false");
b84d4347
ML
893
894 return eq;
895}
896
897/* Processes function equality comparison. */
898
899bool
fb5c322e 900sem_function::equals_private (sem_item *item)
b84d4347
ML
901{
902 if (item->type != FUNC)
903 return false;
904
905 basic_block bb1, bb2;
906 edge e1, e2;
907 edge_iterator ei1, ei2;
b84d4347
ML
908 bool result = true;
909 tree arg1, arg2;
910
911 m_compared_func = static_cast<sem_function *> (item);
912
913 gcc_assert (decl != item->decl);
914
915 if (bb_sorted.length () != m_compared_func->bb_sorted.length ()
916 || edge_count != m_compared_func->edge_count
917 || cfg_checksum != m_compared_func->cfg_checksum)
918 return return_false ();
919
b84d4347
ML
920 m_checker = new func_checker (decl, m_compared_func->decl,
921 compare_polymorphic_p (),
922 false,
923 &refs_set,
924 &m_compared_func->refs_set);
b84d4347
ML
925 for (arg1 = DECL_ARGUMENTS (decl),
926 arg2 = DECL_ARGUMENTS (m_compared_func->decl);
927 arg1; arg1 = DECL_CHAIN (arg1), arg2 = DECL_CHAIN (arg2))
928 if (!m_checker->compare_decl (arg1, arg2))
929 return return_false ();
930
fb5c322e
JH
931 if (!dyn_cast <cgraph_node *> (node)->has_gimple_body_p ())
932 return true;
933
b84d4347
ML
934 /* Fill-up label dictionary. */
935 for (unsigned i = 0; i < bb_sorted.length (); ++i)
936 {
937 m_checker->parse_labels (bb_sorted[i]);
938 m_checker->parse_labels (m_compared_func->bb_sorted[i]);
939 }
940
941 /* Checking all basic blocks. */
942 for (unsigned i = 0; i < bb_sorted.length (); ++i)
943 if(!m_checker->compare_bb (bb_sorted[i], m_compared_func->bb_sorted[i]))
944 return return_false();
945
946 dump_message ("All BBs are equal\n");
947
c190efcc
ML
948 auto_vec <int> bb_dict;
949
b84d4347
ML
950 /* Basic block edges check. */
951 for (unsigned i = 0; i < bb_sorted.length (); ++i)
952 {
b84d4347
ML
953 bb1 = bb_sorted[i]->bb;
954 bb2 = m_compared_func->bb_sorted[i]->bb;
955
956 ei2 = ei_start (bb2->preds);
957
958 for (ei1 = ei_start (bb1->preds); ei_cond (ei1, &e1); ei_next (&ei1))
959 {
960 ei_cond (ei2, &e2);
961
962 if (e1->flags != e2->flags)
963 return return_false_with_msg ("flags comparison returns false");
964
1216ea72 965 if (!bb_dict_test (&bb_dict, e1->src->index, e2->src->index))
b84d4347
ML
966 return return_false_with_msg ("edge comparison returns false");
967
1216ea72 968 if (!bb_dict_test (&bb_dict, e1->dest->index, e2->dest->index))
b84d4347
ML
969 return return_false_with_msg ("BB comparison returns false");
970
971 if (!m_checker->compare_edge (e1, e2))
972 return return_false_with_msg ("edge comparison returns false");
973
974 ei_next (&ei2);
975 }
976 }
977
978 /* Basic block PHI nodes comparison. */
979 for (unsigned i = 0; i < bb_sorted.length (); i++)
980 if (!compare_phi_node (bb_sorted[i]->bb, m_compared_func->bb_sorted[i]->bb))
981 return return_false_with_msg ("PHI node comparison returns false");
982
983 return result;
984}
985
106b5466
JH
986/* Set LOCAL_P of NODE to true if DATA is non-NULL.
987 Helper for call_for_symbol_thunks_and_aliases. */
988
989static bool
990set_local (cgraph_node *node, void *data)
991{
992 node->local.local = data != NULL;
993 return false;
994}
995
412049de 996/* TREE_ADDRESSABLE of NODE to true.
0a7246ee
JH
997 Helper for call_for_symbol_thunks_and_aliases. */
998
999static bool
1000set_addressable (varpool_node *node, void *)
1001{
1002 TREE_ADDRESSABLE (node->decl) = 1;
1003 return false;
1004}
1005
412049de
JH
1006/* Clear DECL_RTL of NODE.
1007 Helper for call_for_symbol_thunks_and_aliases. */
1008
1009static bool
1010clear_decl_rtl (symtab_node *node, void *)
1011{
1012 SET_DECL_RTL (node->decl, NULL);
1013 return false;
1014}
1015
0a7246ee
JH
1016/* Redirect all callers of N and its aliases to TO. Remove aliases if
1017 possible. Return number of redirections made. */
1018
1019static int
1020redirect_all_callers (cgraph_node *n, cgraph_node *to)
1021{
1022 int nredirected = 0;
1023 ipa_ref *ref;
17d1bf76 1024 cgraph_edge *e = n->callers;
0a7246ee 1025
17d1bf76 1026 while (e)
0a7246ee 1027 {
17d1bf76
ML
1028 /* Redirecting thunks to interposable symbols or symbols in other sections
1029 may not be supported by target output code. Play safe for now and
1030 punt on redirection. */
1031 if (!e->caller->thunk.thunk_p)
1032 {
1033 struct cgraph_edge *nexte = e->next_caller;
1034 e->redirect_callee (to);
1035 e = nexte;
1036 nredirected++;
1037 }
1038 else
1039 e = e->next_callee;
0a7246ee
JH
1040 }
1041 for (unsigned i = 0; n->iterate_direct_aliases (i, ref);)
1042 {
1043 bool removed = false;
1044 cgraph_node *n_alias = dyn_cast <cgraph_node *> (ref->referring);
1045
1046 if ((DECL_COMDAT_GROUP (n->decl)
1047 && (DECL_COMDAT_GROUP (n->decl)
1048 == DECL_COMDAT_GROUP (n_alias->decl)))
1049 || (n_alias->get_availability () > AVAIL_INTERPOSABLE
1050 && n->get_availability () > AVAIL_INTERPOSABLE))
1051 {
1052 nredirected += redirect_all_callers (n_alias, to);
1053 if (n_alias->can_remove_if_no_direct_calls_p ()
17d1bf76
ML
1054 && !n_alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1055 NULL, true)
0a7246ee
JH
1056 && !n_alias->has_aliases_p ())
1057 n_alias->remove ();
1058 }
1059 if (!removed)
1060 i++;
1061 }
1062 return nredirected;
1063}
1064
b84d4347
ML
1065/* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
1066 be applied. */
0a7246ee 1067
b84d4347
ML
1068bool
1069sem_function::merge (sem_item *alias_item)
1070{
1071 gcc_assert (alias_item->type == FUNC);
1072
1073 sem_function *alias_func = static_cast<sem_function *> (alias_item);
1074
1075 cgraph_node *original = get_node ();
0a7246ee 1076 cgraph_node *local_original = NULL;
b84d4347 1077 cgraph_node *alias = alias_func->get_node ();
b84d4347 1078
0a7246ee 1079 bool create_wrapper = false;
b84d4347
ML
1080 bool create_alias = false;
1081 bool redirect_callers = false;
0a7246ee
JH
1082 bool remove = false;
1083
b84d4347 1084 bool original_discardable = false;
c7a06bc1 1085 bool original_discarded = false;
b84d4347 1086
0a7246ee
JH
1087 bool original_address_matters = original->address_matters_p ();
1088 bool alias_address_matters = alias->address_matters_p ();
1089
257291fc
JH
1090 if (DECL_EXTERNAL (alias->decl))
1091 {
1092 if (dump_file)
1093 fprintf (dump_file, "Not unifying; alias is external.\n\n");
1094 return false;
1095 }
1096
0a7246ee
JH
1097 if (DECL_NO_INLINE_WARNING_P (original->decl)
1098 != DECL_NO_INLINE_WARNING_P (alias->decl))
1099 {
1100 if (dump_file)
1101 fprintf (dump_file,
1102 "Not unifying; "
1103 "DECL_NO_INLINE_WARNING mismatch.\n\n");
1104 return false;
1105 }
1106
b84d4347
ML
1107 /* Do not attempt to mix functions from different user sections;
1108 we do not know what user intends with those. */
1109 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
1110 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
1111 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
1112 {
1113 if (dump_file)
1114 fprintf (dump_file,
0a7246ee
JH
1115 "Not unifying; "
1116 "original and alias are in different sections.\n\n");
b84d4347
ML
1117 return false;
1118 }
1119
1120 /* See if original is in a section that can be discarded if the main
c7a06bc1 1121 symbol is not used. */
0a7246ee 1122
c7a06bc1
JH
1123 if (original->can_be_discarded_p ())
1124 original_discardable = true;
1125 /* Also consider case where we have resolution info and we know that
0a7246ee
JH
1126 original's definition is not going to be used. In this case we can not
1127 create alias to original. */
c7a06bc1
JH
1128 if (node->resolution != LDPR_UNKNOWN
1129 && !decl_binds_to_current_def_p (node->decl))
1130 original_discardable = original_discarded = true;
b84d4347 1131
0a7246ee
JH
1132 /* Creating a symtab alias is the optimal way to merge.
1133 It however can not be used in the following cases:
1134
1135 1) if ORIGINAL and ALIAS may be possibly compared for address equality.
1136 2) if ORIGINAL is in a section that may be discarded by linker or if
1137 it is an external functions where we can not create an alias
1138 (ORIGINAL_DISCARDABLE)
1139 3) if target do not support symbol aliases.
bbcdfb93 1140 4) original and alias lie in different comdat groups.
0a7246ee
JH
1141
1142 If we can not produce alias, we will turn ALIAS into WRAPPER of ORIGINAL
1143 and/or redirect all callers from ALIAS to ORIGINAL. */
1144 if ((original_address_matters && alias_address_matters)
bbcdfb93
JH
1145 || (original_discardable
1146 && (!DECL_COMDAT_GROUP (alias->decl)
1147 || (DECL_COMDAT_GROUP (alias->decl)
1148 != DECL_COMDAT_GROUP (original->decl))))
c7a06bc1 1149 || original_discarded
bbcdfb93
JH
1150 || !sem_item::target_supports_symbol_aliases_p ()
1151 || DECL_COMDAT_GROUP (alias->decl) != DECL_COMDAT_GROUP (original->decl))
b84d4347 1152 {
0a7246ee
JH
1153 /* First see if we can produce wrapper. */
1154
977b01e3
JH
1155 /* Symbol properties that matter for references must be preserved.
1156 TODO: We can produce wrapper, but we need to produce alias of ORIGINAL
1157 with proper properties. */
1158 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1159 alias->address_taken))
1160 {
1161 if (dump_file)
1162 fprintf (dump_file,
1163 "Wrapper cannot be created because referenced symbol "
1164 "properties mismatch\n");
1165 }
0a7246ee
JH
1166 /* Do not turn function in one comdat group into wrapper to another
1167 comdat group. Other compiler producing the body of the
1168 another comdat group may make opossite decision and with unfortunate
1169 linker choices this may close a loop. */
977b01e3
JH
1170 else if (DECL_COMDAT_GROUP (original->decl)
1171 && DECL_COMDAT_GROUP (alias->decl)
1172 && (DECL_COMDAT_GROUP (alias->decl)
1173 != DECL_COMDAT_GROUP (original->decl)))
0a7246ee
JH
1174 {
1175 if (dump_file)
1176 fprintf (dump_file,
1177 "Wrapper cannot be created because of COMDAT\n");
1178 }
1179 else if (DECL_STATIC_CHAIN (alias->decl))
1180 {
1181 if (dump_file)
1182 fprintf (dump_file,
1183 "Can not create wrapper of nested functions.\n");
1184 }
1185 /* TODO: We can also deal with variadic functions never calling
1186 VA_START. */
1187 else if (stdarg_p (TREE_TYPE (alias->decl)))
1188 {
1189 if (dump_file)
1190 fprintf (dump_file,
1191 "can not create wrapper of stdarg function.\n");
1192 }
1193 else if (inline_summaries
1194 && inline_summaries->get (alias)->self_size <= 2)
1195 {
1196 if (dump_file)
1197 fprintf (dump_file, "Wrapper creation is not "
1198 "profitable (function is too small).\n");
1199 }
1200 /* If user paid attention to mark function noinline, assume it is
1201 somewhat special and do not try to turn it into a wrapper that can
1202 not be undone by inliner. */
1203 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (alias->decl)))
1204 {
1205 if (dump_file)
1206 fprintf (dump_file, "Wrappers are not created for noinline.\n");
1207 }
1208 else
1209 create_wrapper = true;
1210
1211 /* We can redirect local calls in the case both alias and orignal
1212 are not interposable. */
b84d4347 1213 redirect_callers
0a7246ee
JH
1214 = alias->get_availability () > AVAIL_INTERPOSABLE
1215 && original->get_availability () > AVAIL_INTERPOSABLE
1216 && !alias->instrumented_version;
977b01e3
JH
1217 /* TODO: We can redirect, but we need to produce alias of ORIGINAL
1218 with proper properties. */
1219 if (!sem_item::compare_referenced_symbol_properties (NULL, original, alias,
1220 alias->address_taken))
1221 redirect_callers = false;
b84d4347 1222
0a7246ee
JH
1223 if (!redirect_callers && !create_wrapper)
1224 {
1225 if (dump_file)
1226 fprintf (dump_file, "Not unifying; can not redirect callers nor "
1227 "produce wrapper\n\n");
1228 return false;
1229 }
1230
1231 /* Work out the symbol the wrapper should call.
1232 If ORIGINAL is interposable, we need to call a local alias.
c7a06bc1
JH
1233 Also produce local alias (if possible) as an optimization.
1234
1235 Local aliases can not be created inside comdat groups because that
1236 prevents inlining. */
1237 if (!original_discardable && !original->get_comdat_group ())
0a7246ee
JH
1238 {
1239 local_original
1240 = dyn_cast <cgraph_node *> (original->noninterposable_alias ());
1241 if (!local_original
1242 && original->get_availability () > AVAIL_INTERPOSABLE)
1243 local_original = original;
0a7246ee
JH
1244 }
1245 /* If we can not use local alias, fallback to the original
1246 when possible. */
1247 else if (original->get_availability () > AVAIL_INTERPOSABLE)
1248 local_original = original;
c7a06bc1
JH
1249
1250 /* If original is COMDAT local, we can not really redirect calls outside
1251 of its comdat group to it. */
1252 if (original->comdat_local_p ())
1253 redirect_callers = false;
0a7246ee
JH
1254 if (!local_original)
1255 {
1256 if (dump_file)
1257 fprintf (dump_file, "Not unifying; "
1258 "can not produce local alias.\n\n");
1259 return false;
1260 }
f657d665 1261
0a7246ee
JH
1262 if (!redirect_callers && !create_wrapper)
1263 {
1264 if (dump_file)
1265 fprintf (dump_file, "Not unifying; "
1266 "can not redirect callers nor produce a wrapper\n\n");
1267 return false;
1268 }
1269 if (!create_wrapper
17d1bf76
ML
1270 && !alias->call_for_symbol_and_aliases (cgraph_node::has_thunk_p,
1271 NULL, true)
0a7246ee
JH
1272 && !alias->can_remove_if_no_direct_calls_p ())
1273 {
1274 if (dump_file)
1275 fprintf (dump_file, "Not unifying; can not make wrapper and "
1276 "function has other uses than direct calls\n\n");
1277 return false;
1278 }
544dafa6 1279 }
0a7246ee
JH
1280 else
1281 create_alias = true;
544dafa6 1282
b84d4347
ML
1283 if (redirect_callers)
1284 {
0a7246ee 1285 int nredirected = redirect_all_callers (alias, local_original);
b84d4347 1286
0a7246ee
JH
1287 if (nredirected)
1288 {
1289 alias->icf_merged = true;
1290 local_original->icf_merged = true;
b84d4347 1291
0a7246ee
JH
1292 if (dump_file && nredirected)
1293 fprintf (dump_file, "%i local calls have been "
1294 "redirected.\n", nredirected);
b84d4347
ML
1295 }
1296
0a7246ee
JH
1297 /* If all callers was redirected, do not produce wrapper. */
1298 if (alias->can_remove_if_no_direct_calls_p ()
1299 && !alias->has_aliases_p ())
1300 {
1301 create_wrapper = false;
1302 remove = true;
1303 }
1304 gcc_assert (!create_alias);
b84d4347 1305 }
b84d4347
ML
1306 else if (create_alias)
1307 {
1308 alias->icf_merged = true;
1309
1310 /* Remove the function's body. */
1311 ipa_merge_profiles (original, alias);
1312 alias->release_body (true);
1313 alias->reset ();
412049de
JH
1314 /* Notice global symbol possibly produced RTL. */
1315 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
1316 NULL, true);
b84d4347
ML
1317
1318 /* Create the alias. */
1319 cgraph_node::create_alias (alias_func->decl, decl);
1320 alias->resolve_alias (original);
1321
106b5466
JH
1322 original->call_for_symbol_thunks_and_aliases
1323 (set_local, (void *)(size_t) original->local_p (), true);
9d4ded75 1324
b84d4347 1325 if (dump_file)
0a7246ee 1326 fprintf (dump_file, "Unified; Function alias has been created.\n\n");
b84d4347 1327 }
0a7246ee 1328 if (create_wrapper)
b84d4347 1329 {
0a7246ee
JH
1330 gcc_assert (!create_alias);
1331 alias->icf_merged = true;
1332 local_original->icf_merged = true;
b84d4347 1333
0a7246ee
JH
1334 ipa_merge_profiles (local_original, alias, true);
1335 alias->create_wrapper (local_original);
b84d4347 1336
0a7246ee
JH
1337 if (dump_file)
1338 fprintf (dump_file, "Unified; Wrapper has been created.\n\n");
1339 }
17d1bf76
ML
1340
1341 /* It's possible that redirection can hit thunks that block
1342 redirection opportunities. */
1343 gcc_assert (alias->icf_merged || remove || redirect_callers);
0a7246ee 1344 original->icf_merged = true;
34b42fb0 1345
0a7246ee
JH
1346 /* Inform the inliner about cross-module merging. */
1347 if ((original->lto_file_data || alias->lto_file_data)
1348 && original->lto_file_data != alias->lto_file_data)
1349 local_original->merged = original->merged = true;
34b42fb0 1350
0a7246ee
JH
1351 if (remove)
1352 {
1353 ipa_merge_profiles (original, alias);
1354 alias->release_body ();
1355 alias->reset ();
1356 alias->body_removed = true;
b84d4347 1357 alias->icf_merged = true;
b84d4347 1358 if (dump_file)
0a7246ee 1359 fprintf (dump_file, "Unified; Function body was removed.\n");
b84d4347 1360 }
b84d4347
ML
1361
1362 return true;
1363}
1364
1365/* Semantic item initialization function. */
1366
1367void
1368sem_function::init (void)
1369{
1370 if (in_lto_p)
70486010 1371 get_node ()->get_untransformed_body ();
b84d4347
ML
1372
1373 tree fndecl = node->decl;
1374 function *func = DECL_STRUCT_FUNCTION (fndecl);
1375
1376 gcc_assert (func);
1377 gcc_assert (SSANAMES (func));
1378
1379 ssa_names_size = SSANAMES (func)->length ();
1380 node = node;
1381
1382 decl = fndecl;
1383 region_tree = func->eh->region_tree;
1384
1385 /* iterating all function arguments. */
1386 arg_count = count_formal_params (fndecl);
1387
1388 edge_count = n_edges_for_fn (func);
fb5c322e
JH
1389 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1390 if (!cnode->thunk.thunk_p)
1391 {
1392 cfg_checksum = coverage_compute_cfg_checksum (func);
b84d4347 1393
fb5c322e 1394 inchash::hash hstate;
b84d4347 1395
fb5c322e
JH
1396 basic_block bb;
1397 FOR_EACH_BB_FN (bb, func)
b84d4347 1398 {
fb5c322e 1399 unsigned nondbg_stmt_count = 0;
b84d4347 1400
fb5c322e
JH
1401 edge e;
1402 for (edge_iterator ei = ei_start (bb->preds); ei_cond (ei, &e);
1403 ei_next (&ei))
1404 cfg_checksum = iterative_hash_host_wide_int (e->flags,
1405 cfg_checksum);
1406
1407 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1408 gsi_next (&gsi))
b84d4347 1409 {
fb5c322e
JH
1410 gimple stmt = gsi_stmt (gsi);
1411
1412 if (gimple_code (stmt) != GIMPLE_DEBUG
1413 && gimple_code (stmt) != GIMPLE_PREDICT)
1414 {
1415 hash_stmt (stmt, hstate);
1416 nondbg_stmt_count++;
1417 }
b84d4347 1418 }
b84d4347 1419
fb5c322e
JH
1420 gcode_hash = hstate.end ();
1421 bb_sizes.safe_push (nondbg_stmt_count);
b84d4347 1422
fb5c322e
JH
1423 /* Inserting basic block to hash table. */
1424 sem_bb *semantic_bb = new sem_bb (bb, nondbg_stmt_count,
1425 EDGE_COUNT (bb->preds)
1426 + EDGE_COUNT (bb->succs));
b84d4347 1427
fb5c322e
JH
1428 bb_sorted.safe_push (semantic_bb);
1429 }
1430 }
1431 else
1432 {
1433 cfg_checksum = 0;
1434 inchash::hash hstate;
1435 hstate.add_wide_int (cnode->thunk.fixed_offset);
1436 hstate.add_wide_int (cnode->thunk.virtual_value);
1437 hstate.add_flag (cnode->thunk.this_adjusting);
1438 hstate.add_flag (cnode->thunk.virtual_offset_p);
1439 hstate.add_flag (cnode->thunk.add_pointer_bounds_args);
1440 gcode_hash = hstate.end ();
1441 }
b84d4347
ML
1442
1443 parse_tree_args ();
1444}
1445
a8d93817
JH
1446/* Accumulate to HSTATE a hash of expression EXP.
1447 Identical to inchash::add_expr, but guaranteed to be stable across LTO
1448 and DECL equality classes. */
1449
1450void
1451sem_item::add_expr (const_tree exp, inchash::hash &hstate)
1452{
1453 if (exp == NULL_TREE)
1454 {
1455 hstate.merge_hash (0);
1456 return;
1457 }
1458
1459 /* Handled component can be matched in a cureful way proving equivalence
1460 even if they syntactically differ. Just skip them. */
1461 STRIP_NOPS (exp);
1462 while (handled_component_p (exp))
1463 exp = TREE_OPERAND (exp, 0);
1464
1465 enum tree_code code = TREE_CODE (exp);
1466 hstate.add_int (code);
1467
1468 switch (code)
1469 {
1470 /* Use inchash::add_expr for everything that is LTO stable. */
1471 case VOID_CST:
1472 case INTEGER_CST:
1473 case REAL_CST:
1474 case FIXED_CST:
1475 case STRING_CST:
1476 case COMPLEX_CST:
1477 case VECTOR_CST:
1478 inchash::add_expr (exp, hstate);
1479 break;
1480 case CONSTRUCTOR:
1481 {
1482 unsigned HOST_WIDE_INT idx;
1483 tree value;
1484
1485 hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1486
1487 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
1488 if (value)
1489 add_expr (value, hstate);
1490 break;
1491 }
1492 case ADDR_EXPR:
1493 case FDESC_EXPR:
1494 add_expr (get_base_address (TREE_OPERAND (exp, 0)), hstate);
1495 break;
1496 case SSA_NAME:
1497 case VAR_DECL:
1498 case CONST_DECL:
1499 case PARM_DECL:
1500 hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1501 break;
1502 case MEM_REF:
1503 case POINTER_PLUS_EXPR:
1504 case MINUS_EXPR:
1505 case RANGE_EXPR:
1506 add_expr (TREE_OPERAND (exp, 0), hstate);
1507 add_expr (TREE_OPERAND (exp, 1), hstate);
1508 break;
1509 case PLUS_EXPR:
1510 {
1511 inchash::hash one, two;
1512 add_expr (TREE_OPERAND (exp, 0), one);
1513 add_expr (TREE_OPERAND (exp, 1), two);
1514 hstate.add_commutative (one, two);
1515 }
1516 break;
1517 CASE_CONVERT:
1518 hstate.add_wide_int (int_size_in_bytes (TREE_TYPE (exp)));
1519 return add_expr (TREE_OPERAND (exp, 0), hstate);
1520 default:
1521 break;
1522 }
1523}
1524
69f6b1f4
JH
1525/* Accumulate to HSTATE a hash of type t.
1526 TYpes that may end up being compatible after LTO type merging needs to have
1527 the same hash. */
1528
1529void
1530sem_item::add_type (const_tree type, inchash::hash &hstate)
1531{
1532 if (type == NULL_TREE)
1533 {
1534 hstate.merge_hash (0);
1535 return;
1536 }
1537
1538 type = TYPE_MAIN_VARIANT (type);
1539 if (TYPE_CANONICAL (type))
1540 type = TYPE_CANONICAL (type);
1541
1542 if (!AGGREGATE_TYPE_P (type))
1543 hstate.add_int (TYPE_MODE (type));
1544
1545 if (TREE_CODE (type) == COMPLEX_TYPE)
1546 {
1547 hstate.add_int (COMPLEX_TYPE);
1548 sem_item::add_type (TREE_TYPE (type), hstate);
1549 }
1550 else if (INTEGRAL_TYPE_P (type))
1551 {
1552 hstate.add_int (INTEGER_TYPE);
1553 hstate.add_flag (TYPE_UNSIGNED (type));
1554 hstate.add_int (TYPE_PRECISION (type));
1555 }
1556 else if (VECTOR_TYPE_P (type))
1557 {
1558 hstate.add_int (VECTOR_TYPE);
1559 hstate.add_int (TYPE_PRECISION (type));
1560 sem_item::add_type (TREE_TYPE (type), hstate);
1561 }
1562 else if (TREE_CODE (type) == ARRAY_TYPE)
1563 {
1564 hstate.add_int (ARRAY_TYPE);
1565 /* Do not hash size, so complete and incomplete types can match. */
1566 sem_item::add_type (TREE_TYPE (type), hstate);
1567 }
1568 else if (RECORD_OR_UNION_TYPE_P (type))
1569 {
1570 hashval_t *val = optimizer->m_type_hash_cache.get (type);
1571
1572 if (!val)
1573 {
1574 inchash::hash hstate2;
1575 unsigned nf;
1576 tree f;
1577 hashval_t hash;
1578
1579 hstate2.add_int (RECORD_TYPE);
1580 gcc_assert (COMPLETE_TYPE_P (type));
1581
1582 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
1583 if (TREE_CODE (f) == FIELD_DECL)
1584 {
1585 add_type (TREE_TYPE (f), hstate2);
1586 nf++;
1587 }
1588
1589 hstate2.add_int (nf);
1590 hash = hstate2.end ();
1591 hstate.add_wide_int (hash);
1592 optimizer->m_type_hash_cache.put (type, hash);
1593 }
1594 else
1595 hstate.add_wide_int (*val);
1596 }
1597}
1598
b84d4347
ML
1599/* Improve accumulated hash for HSTATE based on a gimple statement STMT. */
1600
1601void
a8d93817 1602sem_function::hash_stmt (gimple stmt, inchash::hash &hstate)
b84d4347
ML
1603{
1604 enum gimple_code code = gimple_code (stmt);
1605
a8d93817 1606 hstate.add_int (code);
b84d4347 1607
a8d93817 1608 switch (code)
b84d4347 1609 {
69f6b1f4
JH
1610 case GIMPLE_SWITCH:
1611 add_expr (gimple_switch_index (as_a <gswitch *> (stmt)), hstate);
1612 break;
a8d93817 1613 case GIMPLE_ASSIGN:
69f6b1f4 1614 hstate.add_int (gimple_assign_rhs_code (stmt));
a8d93817
JH
1615 if (commutative_tree_code (gimple_assign_rhs_code (stmt))
1616 || commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
b84d4347 1617 {
a8d93817 1618 inchash::hash one, two;
b84d4347 1619
a8d93817 1620 add_expr (gimple_assign_rhs1 (stmt), one);
69f6b1f4 1621 add_type (TREE_TYPE (gimple_assign_rhs1 (stmt)), one);
a8d93817
JH
1622 add_expr (gimple_assign_rhs2 (stmt), two);
1623 hstate.add_commutative (one, two);
69f6b1f4
JH
1624 if (commutative_ternary_tree_code (gimple_assign_rhs_code (stmt)))
1625 {
1626 add_expr (gimple_assign_rhs3 (stmt), hstate);
1627 add_type (TREE_TYPE (gimple_assign_rhs3 (stmt)), hstate);
1628 }
a8d93817 1629 add_expr (gimple_assign_lhs (stmt), hstate);
69f6b1f4 1630 add_type (TREE_TYPE (gimple_assign_lhs (stmt)), two);
a8d93817 1631 break;
b84d4347 1632 }
a8d93817
JH
1633 /* ... fall through ... */
1634 case GIMPLE_CALL:
1635 case GIMPLE_ASM:
1636 case GIMPLE_COND:
1637 case GIMPLE_GOTO:
1638 case GIMPLE_RETURN:
1639 /* All these statements are equivalent if their operands are. */
1640 for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
69f6b1f4
JH
1641 {
1642 add_expr (gimple_op (stmt, i), hstate);
1643 if (gimple_op (stmt, i))
1644 add_type (TREE_TYPE (gimple_op (stmt, i)), hstate);
1645 }
a8d93817
JH
1646 default:
1647 break;
b84d4347
ML
1648 }
1649}
1650
1651
1652/* Return true if polymorphic comparison must be processed. */
1653
1654bool
1655sem_function::compare_polymorphic_p (void)
1656{
060cfff4
JH
1657 struct cgraph_edge *e;
1658
69f6b1f4 1659 if (!opt_for_fn (get_node ()->decl, flag_devirtualize))
060cfff4 1660 return false;
69f6b1f4 1661 if (get_node ()->indirect_calls != NULL)
060cfff4
JH
1662 return true;
1663 /* TODO: We can do simple propagation determining what calls may lead to
1664 a polymorphic call. */
69f6b1f4 1665 for (e = get_node ()->callees; e; e = e->next_callee)
060cfff4
JH
1666 if (e->callee->definition
1667 && opt_for_fn (e->callee->decl, flag_devirtualize))
1668 return true;
1669 return false;
b84d4347
ML
1670}
1671
1672/* For a given call graph NODE, the function constructs new
1673 semantic function item. */
1674
1675sem_function *
1676sem_function::parse (cgraph_node *node, bitmap_obstack *stack)
1677{
1678 tree fndecl = node->decl;
1679 function *func = DECL_STRUCT_FUNCTION (fndecl);
1680
fb5c322e 1681 if (!func || (!node->has_gimple_body_p () && !node->thunk.thunk_p))
b84d4347
ML
1682 return NULL;
1683
1684 if (lookup_attribute_by_prefix ("omp ", DECL_ATTRIBUTES (node->decl)) != NULL)
1685 return NULL;
1686
1687 sem_function *f = new sem_function (node, 0, stack);
1688
1689 f->init ();
1690
1691 return f;
1692}
1693
1694/* Parses function arguments and result type. */
1695
1696void
1697sem_function::parse_tree_args (void)
1698{
1699 tree result;
1700
1701 if (arg_types.exists ())
1702 arg_types.release ();
1703
1704 arg_types.create (4);
1705 tree fnargs = DECL_ARGUMENTS (decl);
1706
1707 for (tree parm = fnargs; parm; parm = DECL_CHAIN (parm))
1708 arg_types.safe_push (DECL_ARG_TYPE (parm));
1709
1710 /* Function result type. */
1711 result = DECL_RESULT (decl);
1712 result_type = result ? TREE_TYPE (result) : NULL;
1713
1714 /* During WPA, we can get arguments by following method. */
1715 if (!fnargs)
1716 {
1717 tree type = TYPE_ARG_TYPES (TREE_TYPE (decl));
1718 for (tree parm = type; parm; parm = TREE_CHAIN (parm))
1719 arg_types.safe_push (TYPE_CANONICAL (TREE_VALUE (parm)));
1720
1721 result_type = TREE_TYPE (TREE_TYPE (decl));
1722 }
1723}
1724
1725/* For given basic blocks BB1 and BB2 (from functions FUNC1 and FUNC),
1726 return true if phi nodes are semantically equivalent in these blocks . */
1727
1728bool
1729sem_function::compare_phi_node (basic_block bb1, basic_block bb2)
1730{
538dd0b7
DM
1731 gphi_iterator si1, si2;
1732 gphi *phi1, *phi2;
b84d4347
ML
1733 unsigned size1, size2, i;
1734 tree t1, t2;
1735 edge e1, e2;
1736
1737 gcc_assert (bb1 != NULL);
1738 gcc_assert (bb2 != NULL);
1739
1740 si2 = gsi_start_phis (bb2);
1741 for (si1 = gsi_start_phis (bb1); !gsi_end_p (si1);
1742 gsi_next (&si1))
1743 {
1744 gsi_next_nonvirtual_phi (&si1);
1745 gsi_next_nonvirtual_phi (&si2);
1746
1747 if (gsi_end_p (si1) && gsi_end_p (si2))
1748 break;
1749
1750 if (gsi_end_p (si1) || gsi_end_p (si2))
1751 return return_false();
1752
538dd0b7
DM
1753 phi1 = si1.phi ();
1754 phi2 = si2.phi ();
b84d4347 1755
59f084e0
ML
1756 tree phi_result1 = gimple_phi_result (phi1);
1757 tree phi_result2 = gimple_phi_result (phi2);
1758
1759 if (!m_checker->compare_operand (phi_result1, phi_result2))
1760 return return_false_with_msg ("PHI results are different");
1761
b84d4347
ML
1762 size1 = gimple_phi_num_args (phi1);
1763 size2 = gimple_phi_num_args (phi2);
1764
1765 if (size1 != size2)
1766 return return_false ();
1767
1768 for (i = 0; i < size1; ++i)
1769 {
1770 t1 = gimple_phi_arg (phi1, i)->def;
1771 t2 = gimple_phi_arg (phi2, i)->def;
1772
1773 if (!m_checker->compare_operand (t1, t2))
1774 return return_false ();
1775
1776 e1 = gimple_phi_arg_edge (phi1, i);
1777 e2 = gimple_phi_arg_edge (phi2, i);
1778
1779 if (!m_checker->compare_edge (e1, e2))
1780 return return_false ();
1781 }
1782
1783 gsi_next (&si2);
1784 }
1785
1786 return true;
1787}
1788
1789/* Returns true if tree T can be compared as a handled component. */
1790
1791bool
1792sem_function::icf_handled_component_p (tree t)
1793{
1794 tree_code tc = TREE_CODE (t);
1795
e5115cf9
EB
1796 return (handled_component_p (t)
1797 || tc == ADDR_EXPR || tc == MEM_REF || tc == OBJ_TYPE_REF);
b84d4347
ML
1798}
1799
1800/* Basic blocks dictionary BB_DICT returns true if SOURCE index BB
1801 corresponds to TARGET. */
1802
1803bool
1216ea72 1804sem_function::bb_dict_test (vec<int> *bb_dict, int source, int target)
b84d4347 1805{
c190efcc
ML
1806 source++;
1807 target++;
1808
1216ea72
TS
1809 if (bb_dict->length () <= (unsigned)source)
1810 bb_dict->safe_grow_cleared (source + 1);
c190efcc 1811
1216ea72 1812 if ((*bb_dict)[source] == 0)
b84d4347 1813 {
1216ea72 1814 (*bb_dict)[source] = target;
b84d4347
ML
1815 return true;
1816 }
1817 else
1216ea72 1818 return (*bb_dict)[source] == target;
b84d4347
ML
1819}
1820
b84d4347
ML
1821
1822/* Semantic variable constructor that uses STACK as bitmap memory stack. */
1823
1824sem_variable::sem_variable (bitmap_obstack *stack): sem_item (VAR, stack)
1825{
1826}
1827
1828/* Constructor based on varpool node _NODE with computed hash _HASH.
1829 Bitmap STACK is used for memory allocation. */
1830
1831sem_variable::sem_variable (varpool_node *node, hashval_t _hash,
1832 bitmap_obstack *stack): sem_item(VAR,
1833 node, _hash, stack)
1834{
1835 gcc_checking_assert (node);
1836 gcc_checking_assert (get_node ());
1837}
1838
46305737
JH
1839/* Fast equality function based on knowledge known in WPA. */
1840
1841bool
1842sem_variable::equals_wpa (sem_item *item,
1843 hash_map <symtab_node *, sem_item *> &ignored_nodes)
1844{
1845 gcc_assert (item->type == VAR);
1846
1847 if (node->num_references () != item->node->num_references ())
1848 return return_false_with_msg ("different number of references");
1849
1850 if (DECL_TLS_MODEL (decl) || DECL_TLS_MODEL (item->decl))
1851 return return_false_with_msg ("TLS model");
1852
b3587b99
JH
1853 /* DECL_ALIGN is safe to merge, because we will always chose the largest
1854 alignment out of all aliases. */
46305737
JH
1855
1856 if (DECL_VIRTUAL_P (decl) != DECL_VIRTUAL_P (item->decl))
1857 return return_false_with_msg ("Virtual flag mismatch");
1858
1859 if (DECL_SIZE (decl) != DECL_SIZE (item->decl)
1860 && ((!DECL_SIZE (decl) || !DECL_SIZE (item->decl))
1861 || !operand_equal_p (DECL_SIZE (decl),
1862 DECL_SIZE (item->decl), OEP_ONLY_CONST)))
1863 return return_false_with_msg ("size mismatch");
1864
1865 /* Do not attempt to mix data from different user sections;
1866 we do not know what user intends with those. */
1867 if (((DECL_SECTION_NAME (decl) && !node->implicit_section)
1868 || (DECL_SECTION_NAME (item->decl) && !item->node->implicit_section))
1869 && DECL_SECTION_NAME (decl) != DECL_SECTION_NAME (item->decl))
1870 return return_false_with_msg ("user section mismatch");
1871
1872 if (DECL_IN_TEXT_SECTION (decl) != DECL_IN_TEXT_SECTION (item->decl))
1873 return return_false_with_msg ("text section");
1874
1875 ipa_ref *ref = NULL, *ref2 = NULL;
1876 for (unsigned i = 0; node->iterate_reference (i, ref); i++)
1877 {
1878 item->node->iterate_reference (i, ref2);
1879
b3587b99
JH
1880 if (ref->use != ref2->use)
1881 return return_false_with_msg ("reference use mismatch");
1882
977b01e3 1883 if (!compare_symbol_references (ignored_nodes,
46305737
JH
1884 ref->referred, ref2->referred,
1885 ref->address_matters_p ()))
1886 return false;
1887 }
1888
1889 return true;
1890}
1891
b84d4347 1892/* Returns true if the item equals to ITEM given as argument. */
c4c0f336 1893
b84d4347
ML
1894bool
1895sem_variable::equals (sem_item *item,
c4c0f336 1896 hash_map <symtab_node *, sem_item *> &)
b84d4347
ML
1897{
1898 gcc_assert (item->type == VAR);
c4c0f336 1899 bool ret;
b84d4347 1900
c4c0f336
ML
1901 if (DECL_INITIAL (decl) == error_mark_node && in_lto_p)
1902 dyn_cast <varpool_node *>(node)->get_constructor ();
1903 if (DECL_INITIAL (item->decl) == error_mark_node && in_lto_p)
1904 dyn_cast <varpool_node *>(item->node)->get_constructor ();
b84d4347 1905
9374ef82
ML
1906 /* As seen in PR ipa/65303 we have to compare variables types. */
1907 if (!func_checker::compatible_types_p (TREE_TYPE (decl),
1908 TREE_TYPE (item->decl)))
1909 return return_false_with_msg ("variables types are different");
1910
c4c0f336
ML
1911 ret = sem_variable::equals (DECL_INITIAL (decl),
1912 DECL_INITIAL (item->node->decl));
1913 if (dump_file && (dump_flags & TDF_DETAILS))
1914 fprintf (dump_file,
1915 "Equals called for vars:%s:%s (%u:%u) (%s:%s) with result: %s\n\n",
1aec2ecc
ML
1916 xstrdup_for_dump (node->name()),
1917 xstrdup_for_dump (item->node->name ()),
1918 node->order, item->node->order,
1919 xstrdup_for_dump (node->asm_name ()),
1920 xstrdup_for_dump (item->node->asm_name ()), ret ? "true" : "false");
b84d4347 1921
c4c0f336 1922 return ret;
b84d4347
ML
1923}
1924
1925/* Compares trees T1 and T2 for semantic equality. */
1926
1927bool
1928sem_variable::equals (tree t1, tree t2)
1929{
46305737
JH
1930 if (!t1 || !t2)
1931 return return_with_debug (t1 == t2);
1932 if (t1 == t2)
1933 return true;
b84d4347
ML
1934 tree_code tc1 = TREE_CODE (t1);
1935 tree_code tc2 = TREE_CODE (t2);
1936
1937 if (tc1 != tc2)
46305737 1938 return return_false_with_msg ("TREE_CODE mismatch");
b84d4347
ML
1939
1940 switch (tc1)
1941 {
1942 case CONSTRUCTOR:
1943 {
46305737
JH
1944 vec<constructor_elt, va_gc> *v1, *v2;
1945 unsigned HOST_WIDE_INT idx;
b84d4347 1946
46305737
JH
1947 enum tree_code typecode = TREE_CODE (TREE_TYPE (t1));
1948 if (typecode != TREE_CODE (TREE_TYPE (t2)))
1949 return return_false_with_msg ("constructor type mismatch");
b84d4347 1950
46305737
JH
1951 if (typecode == ARRAY_TYPE)
1952 {
1953 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
1954 /* For arrays, check that the sizes all match. */
1955 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
1956 || size_1 == -1
1957 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
1958 return return_false_with_msg ("constructor array size mismatch");
1959 }
1960 else if (!func_checker::compatible_types_p (TREE_TYPE (t1),
1961 TREE_TYPE (t2)))
1962 return return_false_with_msg ("constructor type incompatible");
b84d4347 1963
46305737
JH
1964 v1 = CONSTRUCTOR_ELTS (t1);
1965 v2 = CONSTRUCTOR_ELTS (t2);
1966 if (vec_safe_length (v1) != vec_safe_length (v2))
1967 return return_false_with_msg ("constructor number of elts mismatch");
1968
1969 for (idx = 0; idx < vec_safe_length (v1); ++idx)
1970 {
1971 constructor_elt *c1 = &(*v1)[idx];
1972 constructor_elt *c2 = &(*v2)[idx];
1973
1974 /* Check that each value is the same... */
1975 if (!sem_variable::equals (c1->value, c2->value))
1976 return false;
1977 /* ... and that they apply to the same fields! */
1978 if (!sem_variable::equals (c1->index, c2->index))
1979 return false;
1980 }
b84d4347
ML
1981 return true;
1982 }
1983 case MEM_REF:
1984 {
1985 tree x1 = TREE_OPERAND (t1, 0);
1986 tree x2 = TREE_OPERAND (t2, 0);
1987 tree y1 = TREE_OPERAND (t1, 1);
1988 tree y2 = TREE_OPERAND (t2, 1);
1989
060cfff4 1990 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
b84d4347
ML
1991 return return_false ();
1992
1993 /* Type of the offset on MEM_REF does not matter. */
46305737
JH
1994 return return_with_debug (sem_variable::equals (x1, x2)
1995 && wi::to_offset (y1)
1996 == wi::to_offset (y2));
b84d4347 1997 }
b84d4347 1998 case ADDR_EXPR:
46305737 1999 case FDESC_EXPR:
b84d4347
ML
2000 {
2001 tree op1 = TREE_OPERAND (t1, 0);
2002 tree op2 = TREE_OPERAND (t2, 0);
2003 return sem_variable::equals (op1, op2);
2004 }
46305737 2005 /* References to other vars/decls are compared using ipa-ref. */
b84d4347
ML
2006 case FUNCTION_DECL:
2007 case VAR_DECL:
46305737
JH
2008 if (decl_in_symtab_p (t1) && decl_in_symtab_p (t2))
2009 return true;
2010 return return_false_with_msg ("Declaration mismatch");
2011 case CONST_DECL:
2012 /* TODO: We can check CONST_DECL by its DECL_INITIAL, but for that we
2013 need to process its VAR/FUNCTION references without relying on ipa-ref
2014 compare. */
b84d4347
ML
2015 case FIELD_DECL:
2016 case LABEL_DECL:
46305737 2017 return return_false_with_msg ("Declaration mismatch");
b84d4347 2018 case INTEGER_CST:
46305737
JH
2019 /* Integer constants are the same only if the same width of type. */
2020 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2021 return return_false_with_msg ("INTEGER_CST precision mismatch");
2022 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2023 return return_false_with_msg ("INTEGER_CST mode mismatch");
2024 return return_with_debug (tree_int_cst_equal (t1, t2));
b84d4347 2025 case STRING_CST:
46305737
JH
2026 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2027 return return_false_with_msg ("STRING_CST mode mismatch");
2028 if (TREE_STRING_LENGTH (t1) != TREE_STRING_LENGTH (t2))
2029 return return_false_with_msg ("STRING_CST length mismatch");
2030 if (memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2031 TREE_STRING_LENGTH (t1)))
2032 return return_false_with_msg ("STRING_CST mismatch");
2033 return true;
2034 case FIXED_CST:
2035 /* Fixed constants are the same only if the same width of type. */
2036 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2037 return return_false_with_msg ("FIXED_CST precision mismatch");
2038
2039 return return_with_debug (FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2040 TREE_FIXED_CST (t2)));
b84d4347 2041 case COMPLEX_CST:
46305737
JH
2042 return (sem_variable::equals (TREE_REALPART (t1), TREE_REALPART (t2))
2043 && sem_variable::equals (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2044 case REAL_CST:
2045 /* Real constants are the same only if the same width of type. */
2046 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2047 return return_false_with_msg ("REAL_CST precision mismatch");
2048 return return_with_debug (REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1),
2049 TREE_REAL_CST (t2)));
2050 case VECTOR_CST:
2051 {
2052 unsigned i;
2053
2054 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
2055 return return_false_with_msg ("VECTOR_CST nelts mismatch");
2056
2057 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
2058 if (!sem_variable::equals (VECTOR_CST_ELT (t1, i),
2059 VECTOR_CST_ELT (t2, i)))
2060 return 0;
2061
2062 return 1;
2063 }
b84d4347 2064 case ARRAY_REF:
46305737
JH
2065 case ARRAY_RANGE_REF:
2066 {
2067 tree x1 = TREE_OPERAND (t1, 0);
2068 tree x2 = TREE_OPERAND (t2, 0);
2069 tree y1 = TREE_OPERAND (t1, 1);
2070 tree y2 = TREE_OPERAND (t2, 1);
2071
3c031cbe 2072 if (!sem_variable::equals (x1, x2) || !sem_variable::equals (y1, y2))
46305737
JH
2073 return false;
2074 if (!sem_variable::equals (array_ref_low_bound (t1),
2075 array_ref_low_bound (t2)))
2076 return false;
2077 if (!sem_variable::equals (array_ref_element_size (t1),
2078 array_ref_element_size (t2)))
2079 return false;
2080 return true;
2081 }
2082
2083 case COMPONENT_REF:
b84d4347 2084 case POINTER_PLUS_EXPR:
46305737
JH
2085 case PLUS_EXPR:
2086 case MINUS_EXPR:
2087 case RANGE_EXPR:
b84d4347
ML
2088 {
2089 tree x1 = TREE_OPERAND (t1, 0);
2090 tree x2 = TREE_OPERAND (t2, 0);
2091 tree y1 = TREE_OPERAND (t1, 1);
2092 tree y2 = TREE_OPERAND (t2, 1);
2093
2094 return sem_variable::equals (x1, x2) && sem_variable::equals (y1, y2);
2095 }
46305737
JH
2096
2097 CASE_CONVERT:
2098 case VIEW_CONVERT_EXPR:
060cfff4 2099 if (!func_checker::compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
46305737
JH
2100 return return_false ();
2101 return sem_variable::equals (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
b84d4347
ML
2102 case ERROR_MARK:
2103 return return_false_with_msg ("ERROR_MARK");
2104 default:
2105 return return_false_with_msg ("Unknown TREE code reached");
2106 }
2107}
2108
2109/* Parser function that visits a varpool NODE. */
2110
2111sem_variable *
2112sem_variable::parse (varpool_node *node, bitmap_obstack *stack)
2113{
6fb12821
ML
2114 if (TREE_THIS_VOLATILE (node->decl) || DECL_HARD_REGISTER (node->decl)
2115 || node->alias)
b84d4347
ML
2116 return NULL;
2117
2118 sem_variable *v = new sem_variable (node, 0, stack);
2119
2120 v->init ();
2121
2122 return v;
2123}
2124
2125/* References independent hash function. */
2126
2127hashval_t
2128sem_variable::get_hash (void)
2129{
2130 if (hash)
c4c0f336 2131 return hash;
3ab93359 2132
a8d93817
JH
2133 /* All WPA streamed in symbols should have their hashes computed at compile
2134 time. At this point, the constructor may not be in memory at all.
2135 DECL_INITIAL (decl) would be error_mark_node in that case. */
2136 gcc_assert (!node->lto_file_data);
2137 tree ctor = DECL_INITIAL (decl);
b84d4347
ML
2138 inchash::hash hstate;
2139
2140 hstate.add_int (456346417);
a8d93817
JH
2141 if (DECL_SIZE (decl) && tree_fits_shwi_p (DECL_SIZE (decl)))
2142 hstate.add_wide_int (tree_to_shwi (DECL_SIZE (decl)));
2143 add_expr (ctor, hstate);
b84d4347
ML
2144 hash = hstate.end ();
2145
2146 return hash;
2147}
2148
2149/* Merges instance with an ALIAS_ITEM, where alias, thunk or redirection can
2150 be applied. */
2151
2152bool
2153sem_variable::merge (sem_item *alias_item)
2154{
2155 gcc_assert (alias_item->type == VAR);
2156
f657d665
ML
2157 if (!sem_item::target_supports_symbol_aliases_p ())
2158 {
2159 if (dump_file)
0a7246ee
JH
2160 fprintf (dump_file, "Not unifying; "
2161 "Symbol aliases are not supported by target\n\n");
f657d665
ML
2162 return false;
2163 }
2164
257291fc
JH
2165 if (DECL_EXTERNAL (alias_item->decl))
2166 {
2167 if (dump_file)
2168 fprintf (dump_file, "Not unifying; alias is external.\n\n");
2169 return false;
2170 }
2171
b84d4347
ML
2172 sem_variable *alias_var = static_cast<sem_variable *> (alias_item);
2173
2174 varpool_node *original = get_node ();
2175 varpool_node *alias = alias_var->get_node ();
2176 bool original_discardable = false;
2177
0a7246ee
JH
2178 bool original_address_matters = original->address_matters_p ();
2179 bool alias_address_matters = alias->address_matters_p ();
2180
b84d4347 2181 /* See if original is in a section that can be discarded if the main
0a7246ee
JH
2182 symbol is not used.
2183 Also consider case where we have resolution info and we know that
2184 original's definition is not going to be used. In this case we can not
2185 create alias to original. */
2186 if (original->can_be_discarded_p ()
2187 || (node->resolution != LDPR_UNKNOWN
2188 && !decl_binds_to_current_def_p (node->decl)))
b84d4347
ML
2189 original_discardable = true;
2190
2191 gcc_assert (!TREE_ASM_WRITTEN (alias->decl));
2192
0a7246ee
JH
2193 /* Constant pool machinery is not quite ready for aliases.
2194 TODO: varasm code contains logic for merging DECL_IN_CONSTANT_POOL.
2195 For LTO merging does not happen that is an important missing feature.
2196 We can enable merging with LTO if the DECL_IN_CONSTANT_POOL
2197 flag is dropped and non-local symbol name is assigned. */
2198 if (DECL_IN_CONSTANT_POOL (alias->decl)
2199 || DECL_IN_CONSTANT_POOL (original->decl))
b84d4347
ML
2200 {
2201 if (dump_file)
0a7246ee
JH
2202 fprintf (dump_file,
2203 "Not unifying; constant pool variables.\n\n");
2204 return false;
2205 }
b84d4347 2206
0a7246ee
JH
2207 /* Do not attempt to mix functions from different user sections;
2208 we do not know what user intends with those. */
2209 if (((DECL_SECTION_NAME (original->decl) && !original->implicit_section)
2210 || (DECL_SECTION_NAME (alias->decl) && !alias->implicit_section))
2211 && DECL_SECTION_NAME (original->decl) != DECL_SECTION_NAME (alias->decl))
2212 {
2213 if (dump_file)
2214 fprintf (dump_file,
2215 "Not unifying; "
2216 "original and alias are in different sections.\n\n");
b84d4347
ML
2217 return false;
2218 }
0a7246ee
JH
2219
2220 /* We can not merge if address comparsion metters. */
2221 if (original_address_matters && alias_address_matters
2222 && flag_merge_constants < 2)
b84d4347 2223 {
0a7246ee
JH
2224 if (dump_file)
2225 fprintf (dump_file,
2226 "Not unifying; "
2227 "adress of original and alias may be compared.\n\n");
2228 return false;
2229 }
c7a06bc1
JH
2230 if (DECL_COMDAT_GROUP (original->decl) != DECL_COMDAT_GROUP (alias->decl))
2231 {
2232 if (dump_file)
2233 fprintf (dump_file, "Not unifying; alias cannot be created; "
2234 "across comdat group boundary\n\n");
2235
2236 return false;
2237 }
b84d4347 2238
c7a06bc1 2239 if (original_discardable)
0a7246ee
JH
2240 {
2241 if (dump_file)
2242 fprintf (dump_file, "Not unifying; alias cannot be created; "
2243 "target is discardable\n\n");
b84d4347 2244
0a7246ee
JH
2245 return false;
2246 }
2247 else
2248 {
2249 gcc_assert (!original->alias);
2250 gcc_assert (!alias->alias);
b84d4347
ML
2251
2252 alias->analyzed = false;
2253
2254 DECL_INITIAL (alias->decl) = NULL;
412049de
JH
2255 ((symtab_node *)alias)->call_for_symbol_and_aliases (clear_decl_rtl,
2256 NULL, true);
d5e254e1 2257 alias->need_bounds_init = false;
b84d4347 2258 alias->remove_all_references ();
0a7246ee
JH
2259 if (TREE_ADDRESSABLE (alias->decl))
2260 original->call_for_symbol_and_aliases (set_addressable, NULL, true);
b84d4347
ML
2261
2262 varpool_node::create_alias (alias_var->decl, decl);
2263 alias->resolve_alias (original);
2264
2265 if (dump_file)
0a7246ee 2266 fprintf (dump_file, "Unified; Variable alias has been created.\n\n");
b84d4347
ML
2267
2268 return true;
2269 }
2270}
2271
b84d4347
ML
2272/* Dump symbol to FILE. */
2273
2274void
2275sem_variable::dump_to_file (FILE *file)
2276{
2277 gcc_assert (file);
2278
2279 print_node (file, "", decl, 0);
2280 fprintf (file, "\n\n");
2281}
2282
b84d4347
ML
2283unsigned int sem_item_optimizer::class_id = 0;
2284
2285sem_item_optimizer::sem_item_optimizer (): worklist (0), m_classes (0),
2286 m_classes_count (0), m_cgraph_node_hooks (NULL), m_varpool_node_hooks (NULL)
2287{
2288 m_items.create (0);
2289 bitmap_obstack_initialize (&m_bmstack);
2290}
2291
2292sem_item_optimizer::~sem_item_optimizer ()
2293{
2294 for (unsigned int i = 0; i < m_items.length (); i++)
2295 delete m_items[i];
2296
2297 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
2298 it != m_classes.end (); ++it)
2299 {
2300 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
2301 delete (*it)->classes[i];
2302
2303 (*it)->classes.release ();
ba2dbfce 2304 free (*it);
b84d4347
ML
2305 }
2306
2307 m_items.release ();
2308
2309 bitmap_obstack_release (&m_bmstack);
2310}
2311
2312/* Write IPA ICF summary for symbols. */
2313
2314void
2315sem_item_optimizer::write_summary (void)
2316{
2317 unsigned int count = 0;
2318
2319 output_block *ob = create_output_block (LTO_section_ipa_icf);
2320 lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
2321 ob->symbol = NULL;
2322
2323 /* Calculate number of symbols to be serialized. */
2324 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2325 !lsei_end_p (lsei);
2326 lsei_next_in_partition (&lsei))
2327 {
2328 symtab_node *node = lsei_node (lsei);
2329
2330 if (m_symtab_node_map.get (node))
2331 count++;
2332 }
2333
2334 streamer_write_uhwi (ob, count);
2335
2336 /* Process all of the symbols. */
2337 for (lto_symtab_encoder_iterator lsei = lsei_start_in_partition (encoder);
2338 !lsei_end_p (lsei);
2339 lsei_next_in_partition (&lsei))
2340 {
2341 symtab_node *node = lsei_node (lsei);
2342
2343 sem_item **item = m_symtab_node_map.get (node);
2344
2345 if (item && *item)
2346 {
2347 int node_ref = lto_symtab_encoder_encode (encoder, node);
2348 streamer_write_uhwi_stream (ob->main_stream, node_ref);
2349
2350 streamer_write_uhwi (ob, (*item)->get_hash ());
2351 }
2352 }
2353
2354 streamer_write_char_stream (ob->main_stream, 0);
2355 produce_asm (ob, NULL);
2356 destroy_output_block (ob);
2357}
2358
2359/* Reads a section from LTO stream file FILE_DATA. Input block for DATA
2360 contains LEN bytes. */
2361
2362void
2363sem_item_optimizer::read_section (lto_file_decl_data *file_data,
2364 const char *data, size_t len)
2365{
2366 const lto_function_header *header =
2367 (const lto_function_header *) data;
2368 const int cfg_offset = sizeof (lto_function_header);
2369 const int main_offset = cfg_offset + header->cfg_size;
2370 const int string_offset = main_offset + header->main_size;
2371 data_in *data_in;
2372 unsigned int i;
2373 unsigned int count;
2374
2375 lto_input_block ib_main ((const char *) data + main_offset, 0,
db847fa8 2376 header->main_size, file_data->mode_table);
b84d4347
ML
2377
2378 data_in =
2379 lto_data_in_create (file_data, (const char *) data + string_offset,
2380 header->string_size, vNULL);
2381
2382 count = streamer_read_uhwi (&ib_main);
2383
2384 for (i = 0; i < count; i++)
2385 {
2386 unsigned int index;
2387 symtab_node *node;
2388 lto_symtab_encoder_t encoder;
2389
2390 index = streamer_read_uhwi (&ib_main);
2391 encoder = file_data->symtab_node_encoder;
2392 node = lto_symtab_encoder_deref (encoder, index);
2393
2394 hashval_t hash = streamer_read_uhwi (&ib_main);
2395
2396 gcc_assert (node->definition);
2397
2398 if (dump_file)
1aec2ecc
ML
2399 fprintf (dump_file, "Symbol added:%s (tree: %p, uid:%u)\n",
2400 node->asm_name (), (void *) node->decl, node->order);
b84d4347
ML
2401
2402 if (is_a<cgraph_node *> (node))
2403 {
2404 cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
2405
2406 m_items.safe_push (new sem_function (cnode, hash, &m_bmstack));
2407 }
2408 else
2409 {
2410 varpool_node *vnode = dyn_cast <varpool_node *> (node);
2411
2412 m_items.safe_push (new sem_variable (vnode, hash, &m_bmstack));
2413 }
2414 }
2415
2416 lto_free_section_data (file_data, LTO_section_ipa_icf, NULL, data,
2417 len);
2418 lto_data_in_delete (data_in);
2419}
2420
2421/* Read IPA IPA ICF summary for symbols. */
2422
2423void
2424sem_item_optimizer::read_summary (void)
2425{
2426 lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2427 lto_file_decl_data *file_data;
2428 unsigned int j = 0;
2429
2430 while ((file_data = file_data_vec[j++]))
2431 {
2432 size_t len;
2433 const char *data = lto_get_section_data (file_data,
2434 LTO_section_ipa_icf, NULL, &len);
2435
2436 if (data)
2437 read_section (file_data, data, len);
2438 }
2439}
2440
2441/* Register callgraph and varpool hooks. */
2442
2443void
2444sem_item_optimizer::register_hooks (void)
2445{
e3e968e9
ML
2446 if (!m_cgraph_node_hooks)
2447 m_cgraph_node_hooks = symtab->add_cgraph_removal_hook
2448 (&sem_item_optimizer::cgraph_removal_hook, this);
b84d4347 2449
e3e968e9
ML
2450 if (!m_varpool_node_hooks)
2451 m_varpool_node_hooks = symtab->add_varpool_removal_hook
2452 (&sem_item_optimizer::varpool_removal_hook, this);
b84d4347
ML
2453}
2454
2455/* Unregister callgraph and varpool hooks. */
2456
2457void
2458sem_item_optimizer::unregister_hooks (void)
2459{
2460 if (m_cgraph_node_hooks)
2461 symtab->remove_cgraph_removal_hook (m_cgraph_node_hooks);
2462
2463 if (m_varpool_node_hooks)
2464 symtab->remove_varpool_removal_hook (m_varpool_node_hooks);
2465}
2466
2467/* Adds a CLS to hashtable associated by hash value. */
2468
2469void
2470sem_item_optimizer::add_class (congruence_class *cls)
2471{
2472 gcc_assert (cls->members.length ());
2473
2474 congruence_class_group *group = get_group_by_hash (
2475 cls->members[0]->get_hash (),
2476 cls->members[0]->type);
2477 group->classes.safe_push (cls);
2478}
2479
2480/* Gets a congruence class group based on given HASH value and TYPE. */
2481
2482congruence_class_group *
2483sem_item_optimizer::get_group_by_hash (hashval_t hash, sem_item_type type)
2484{
2485 congruence_class_group *item = XNEW (congruence_class_group);
2486 item->hash = hash;
2487 item->type = type;
2488
2489 congruence_class_group **slot = m_classes.find_slot (item, INSERT);
2490
2491 if (*slot)
2492 free (item);
2493 else
2494 {
2495 item->classes.create (1);
2496 *slot = item;
2497 }
2498
2499 return *slot;
2500}
2501
2502/* Callgraph removal hook called for a NODE with a custom DATA. */
2503
2504void
2505sem_item_optimizer::cgraph_removal_hook (cgraph_node *node, void *data)
2506{
2507 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2508 optimizer->remove_symtab_node (node);
2509}
2510
2511/* Varpool removal hook called for a NODE with a custom DATA. */
2512
2513void
2514sem_item_optimizer::varpool_removal_hook (varpool_node *node, void *data)
2515{
2516 sem_item_optimizer *optimizer = (sem_item_optimizer *) data;
2517 optimizer->remove_symtab_node (node);
2518}
2519
2520/* Remove symtab NODE triggered by symtab removal hooks. */
2521
2522void
2523sem_item_optimizer::remove_symtab_node (symtab_node *node)
2524{
2525 gcc_assert (!m_classes.elements());
2526
2527 m_removed_items_set.add (node);
2528}
2529
2530void
2531sem_item_optimizer::remove_item (sem_item *item)
2532{
2533 if (m_symtab_node_map.get (item->node))
2534 m_symtab_node_map.remove (item->node);
2535 delete item;
2536}
2537
2538/* Removes all callgraph and varpool nodes that are marked by symtab
2539 as deleted. */
2540
2541void
2542sem_item_optimizer::filter_removed_items (void)
2543{
2544 auto_vec <sem_item *> filtered;
2545
2546 for (unsigned int i = 0; i < m_items.length(); i++)
2547 {
2548 sem_item *item = m_items[i];
2549
90190bb3
ML
2550 if (m_removed_items_set.contains (item->node))
2551 {
b84d4347
ML
2552 remove_item (item);
2553 continue;
90190bb3 2554 }
b84d4347
ML
2555
2556 if (item->type == FUNC)
90190bb3 2557 {
b84d4347
ML
2558 cgraph_node *cnode = static_cast <sem_function *>(item)->get_node ();
2559
060cfff4 2560 if (in_lto_p && (cnode->alias || cnode->body_removed))
90190bb3
ML
2561 remove_item (item);
2562 else
2563 filtered.safe_push (item);
2564 }
2565 else /* VAR. */
2566 {
2567 if (!flag_ipa_icf_variables)
2568 remove_item (item);
2569 else
c4c0f336
ML
2570 {
2571 /* Filter out non-readonly variables. */
2572 tree decl = item->decl;
2573 if (TREE_READONLY (decl))
2574 filtered.safe_push (item);
2575 else
2576 remove_item (item);
2577 }
90190bb3 2578 }
b84d4347
ML
2579 }
2580
2581 /* Clean-up of released semantic items. */
2582
2583 m_items.release ();
2584 for (unsigned int i = 0; i < filtered.length(); i++)
2585 m_items.safe_push (filtered[i]);
2586}
2587
bd31fe14
ML
2588/* Optimizer entry point which returns true in case it processes
2589 a merge operation. True is returned if there's a merge operation
2590 processed. */
b84d4347 2591
bd31fe14 2592bool
b84d4347
ML
2593sem_item_optimizer::execute (void)
2594{
2595 filter_removed_items ();
8b048701
JJ
2596 unregister_hooks ();
2597
3ab93359
ML
2598 build_graph ();
2599 update_hash_by_addr_refs ();
b84d4347
ML
2600 build_hash_based_classes ();
2601
2602 if (dump_file)
2603 fprintf (dump_file, "Dump after hash based groups\n");
2604 dump_cong_classes ();
2605
2606 for (unsigned int i = 0; i < m_items.length(); i++)
2607 m_items[i]->init_wpa ();
2608
b84d4347
ML
2609 subdivide_classes_by_equality (true);
2610
2611 if (dump_file)
2612 fprintf (dump_file, "Dump after WPA based types groups\n");
2613
2614 dump_cong_classes ();
2615
2616 process_cong_reduction ();
2617 verify_classes ();
2618
2619 if (dump_file)
2620 fprintf (dump_file, "Dump after callgraph-based congruence reduction\n");
2621
2622 dump_cong_classes ();
2623
2624 parse_nonsingleton_classes ();
2625 subdivide_classes_by_equality ();
2626
2627 if (dump_file)
2628 fprintf (dump_file, "Dump after full equality comparison of groups\n");
2629
2630 dump_cong_classes ();
2631
2632 unsigned int prev_class_count = m_classes_count;
2633
2634 process_cong_reduction ();
2635 dump_cong_classes ();
2636 verify_classes ();
bd31fe14 2637 bool merged_p = merge_classes (prev_class_count);
b84d4347
ML
2638
2639 if (dump_file && (dump_flags & TDF_DETAILS))
2640 symtab_node::dump_table (dump_file);
bd31fe14
ML
2641
2642 return merged_p;
b84d4347
ML
2643}
2644
2645/* Function responsible for visiting all potential functions and
2646 read-only variables that can be merged. */
2647
2648void
2649sem_item_optimizer::parse_funcs_and_vars (void)
2650{
2651 cgraph_node *cnode;
2652
2653 if (flag_ipa_icf_functions)
2654 FOR_EACH_DEFINED_FUNCTION (cnode)
2655 {
2656 sem_function *f = sem_function::parse (cnode, &m_bmstack);
2657 if (f)
2658 {
2659 m_items.safe_push (f);
2660 m_symtab_node_map.put (cnode, f);
2661
2662 if (dump_file)
1aec2ecc 2663 fprintf (dump_file, "Parsed function:%s\n", f->node->asm_name ());
b84d4347
ML
2664
2665 if (dump_file && (dump_flags & TDF_DETAILS))
2666 f->dump_to_file (dump_file);
2667 }
2668 else if (dump_file)
2669 fprintf (dump_file, "Not parsed function:%s\n", cnode->asm_name ());
2670 }
2671
2672 varpool_node *vnode;
2673
2674 if (flag_ipa_icf_variables)
2675 FOR_EACH_DEFINED_VARIABLE (vnode)
2676 {
2677 sem_variable *v = sem_variable::parse (vnode, &m_bmstack);
2678
2679 if (v)
2680 {
2681 m_items.safe_push (v);
2682 m_symtab_node_map.put (vnode, v);
2683 }
2684 }
2685}
2686
2687/* Makes pairing between a congruence class CLS and semantic ITEM. */
2688
2689void
2690sem_item_optimizer::add_item_to_class (congruence_class *cls, sem_item *item)
2691{
2692 item->index_in_class = cls->members.length ();
2693 cls->members.safe_push (item);
2694 item->cls = cls;
2695}
2696
3ab93359
ML
2697/* For each semantic item, append hash values of references. */
2698
2699void
2700sem_item_optimizer::update_hash_by_addr_refs ()
2701{
69f6b1f4
JH
2702 /* First, append to hash sensitive references and class type if it need to
2703 be matched for ODR. */
3ab93359 2704 for (unsigned i = 0; i < m_items.length (); i++)
69f6b1f4
JH
2705 {
2706 m_items[i]->update_hash_by_addr_refs (m_symtab_node_map);
2707 if (m_items[i]->type == FUNC)
2708 {
2709 if (TREE_CODE (TREE_TYPE (m_items[i]->decl)) == METHOD_TYPE
2710 && contains_polymorphic_type_p
70e7f2a2 2711 (TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl)))
69f6b1f4 2712 && (DECL_CXX_CONSTRUCTOR_P (m_items[i]->decl)
b3587b99 2713 || (static_cast<sem_function *> (m_items[i])->param_used_p (0)
69f6b1f4
JH
2714 && static_cast<sem_function *> (m_items[i])
2715 ->compare_polymorphic_p ())))
2716 {
2717 tree class_type
70e7f2a2 2718 = TYPE_METHOD_BASETYPE (TREE_TYPE (m_items[i]->decl));
69f6b1f4
JH
2719 inchash::hash hstate (m_items[i]->hash);
2720
2721 if (TYPE_NAME (class_type)
2722 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (class_type)))
2723 hstate.add_wide_int
2724 (IDENTIFIER_HASH_VALUE
2725 (DECL_ASSEMBLER_NAME (TYPE_NAME (class_type))));
2726
2727 m_items[i]->hash = hstate.end ();
2728 }
2729 }
2730 }
3ab93359
ML
2731
2732 /* Once all symbols have enhanced hash value, we can append
2733 hash values of symbols that are seen by IPA ICF and are
2734 references by a semantic item. Newly computed values
2735 are saved to global_hash member variable. */
2736 for (unsigned i = 0; i < m_items.length (); i++)
2737 m_items[i]->update_hash_by_local_refs (m_symtab_node_map);
2738
2739 /* Global hash value replace current hash values. */
2740 for (unsigned i = 0; i < m_items.length (); i++)
2741 m_items[i]->hash = m_items[i]->global_hash;
2742}
2743
b84d4347
ML
2744/* Congruence classes are built by hash value. */
2745
2746void
2747sem_item_optimizer::build_hash_based_classes (void)
2748{
2749 for (unsigned i = 0; i < m_items.length (); i++)
2750 {
2751 sem_item *item = m_items[i];
2752
3ab93359 2753 congruence_class_group *group = get_group_by_hash (item->hash,
b84d4347
ML
2754 item->type);
2755
2756 if (!group->classes.length ())
2757 {
2758 m_classes_count++;
2759 group->classes.safe_push (new congruence_class (class_id++));
2760 }
2761
2762 add_item_to_class (group->classes[0], item);
2763 }
2764}
2765
2766/* Build references according to call graph. */
2767
2768void
2769sem_item_optimizer::build_graph (void)
2770{
2771 for (unsigned i = 0; i < m_items.length (); i++)
2772 {
2773 sem_item *item = m_items[i];
2774 m_symtab_node_map.put (item->node, item);
2775 }
2776
2777 for (unsigned i = 0; i < m_items.length (); i++)
2778 {
2779 sem_item *item = m_items[i];
2780
2781 if (item->type == FUNC)
2782 {
2783 cgraph_node *cnode = dyn_cast <cgraph_node *> (item->node);
2784
2785 cgraph_edge *e = cnode->callees;
2786 while (e)
2787 {
b6cddc7f
ML
2788 sem_item **slot = m_symtab_node_map.get
2789 (e->callee->ultimate_alias_target ());
b84d4347
ML
2790 if (slot)
2791 item->add_reference (*slot);
2792
2793 e = e->next_callee;
2794 }
2795 }
2796
2797 ipa_ref *ref = NULL;
2798 for (unsigned i = 0; item->node->iterate_reference (i, ref); i++)
2799 {
b6cddc7f
ML
2800 sem_item **slot = m_symtab_node_map.get
2801 (ref->referred->ultimate_alias_target ());
b84d4347
ML
2802 if (slot)
2803 item->add_reference (*slot);
2804 }
2805 }
2806}
2807
2808/* Semantic items in classes having more than one element and initialized.
2809 In case of WPA, we load function body. */
2810
2811void
2812sem_item_optimizer::parse_nonsingleton_classes (void)
2813{
2814 unsigned int init_called_count = 0;
2815
2816 for (unsigned i = 0; i < m_items.length (); i++)
2817 if (m_items[i]->cls->members.length () > 1)
2818 {
2819 m_items[i]->init ();
2820 init_called_count++;
2821 }
2822
2823 if (dump_file)
2824 fprintf (dump_file, "Init called for %u items (%.2f%%).\n", init_called_count,
f1c859ee 2825 m_items.length () ? 100.0f * init_called_count / m_items.length (): 0.0f);
b84d4347
ML
2826}
2827
2828/* Equality function for semantic items is used to subdivide existing
2829 classes. If IN_WPA, fast equality function is invoked. */
2830
2831void
2832sem_item_optimizer::subdivide_classes_by_equality (bool in_wpa)
2833{
2834 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
2835 it != m_classes.end (); ++it)
2836 {
2837 unsigned int class_count = (*it)->classes.length ();
2838
2839 for (unsigned i = 0; i < class_count; i++)
2840 {
2841 congruence_class *c = (*it)->classes [i];
2842
2843 if (c->members.length() > 1)
2844 {
2845 auto_vec <sem_item *> new_vector;
2846
2847 sem_item *first = c->members[0];
2848 new_vector.safe_push (first);
2849
2850 unsigned class_split_first = (*it)->classes.length ();
2851
2852 for (unsigned j = 1; j < c->members.length (); j++)
2853 {
2854 sem_item *item = c->members[j];
2855
2856 bool equals = in_wpa ? first->equals_wpa (item,
2857 m_symtab_node_map) : first->equals (item, m_symtab_node_map);
2858
2859 if (equals)
2860 new_vector.safe_push (item);
2861 else
2862 {
2863 bool integrated = false;
2864
2865 for (unsigned k = class_split_first; k < (*it)->classes.length (); k++)
2866 {
2867 sem_item *x = (*it)->classes[k]->members[0];
2868 bool equals = in_wpa ? x->equals_wpa (item,
2869 m_symtab_node_map) : x->equals (item, m_symtab_node_map);
2870
2871 if (equals)
2872 {
2873 integrated = true;
2874 add_item_to_class ((*it)->classes[k], item);
2875
2876 break;
2877 }
2878 }
2879
2880 if (!integrated)
2881 {
2882 congruence_class *c = new congruence_class (class_id++);
2883 m_classes_count++;
2884 add_item_to_class (c, item);
2885
2886 (*it)->classes.safe_push (c);
2887 }
2888 }
2889 }
2890
2891 // we replace newly created new_vector for the class we've just splitted
2892 c->members.release ();
2893 c->members.create (new_vector.length ());
2894
2895 for (unsigned int j = 0; j < new_vector.length (); j++)
2896 add_item_to_class (c, new_vector[j]);
2897 }
2898 }
2899 }
2900
2901 verify_classes ();
2902}
2903
5ebd0e61
ML
2904/* Subdivide classes by address references that members of the class
2905 reference. Example can be a pair of functions that have an address
2906 taken from a function. If these addresses are different the class
2907 is split. */
2908
2909unsigned
2910sem_item_optimizer::subdivide_classes_by_sensitive_refs ()
2911{
bbd08a5d
ML
2912 typedef hash_map <symbol_compare_collection *, vec <sem_item *>,
2913 symbol_compare_hashmap_traits> subdivide_hash_map;
2914
5ebd0e61
ML
2915 unsigned newly_created_classes = 0;
2916
2917 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
2918 it != m_classes.end (); ++it)
2919 {
2920 unsigned int class_count = (*it)->classes.length ();
2921 auto_vec<congruence_class *> new_classes;
2922
2923 for (unsigned i = 0; i < class_count; i++)
2924 {
2925 congruence_class *c = (*it)->classes [i];
2926
2927 if (c->members.length() > 1)
2928 {
bbd08a5d 2929 subdivide_hash_map split_map;
5ebd0e61
ML
2930
2931 for (unsigned j = 0; j < c->members.length (); j++)
2932 {
2933 sem_item *source_node = c->members[j];
2934
2935 symbol_compare_collection *collection = new symbol_compare_collection (source_node->node);
2936
bbd08a5d
ML
2937 bool existed;
2938 vec <sem_item *> *slot = &split_map.get_or_insert (collection,
2939 &existed);
5ebd0e61
ML
2940 gcc_checking_assert (slot);
2941
2942 slot->safe_push (source_node);
bbd08a5d
ML
2943
2944 if (existed)
2945 delete collection;
5ebd0e61
ML
2946 }
2947
2948 /* If the map contains more than one key, we have to split the map
2949 appropriately. */
2950 if (split_map.elements () != 1)
2951 {
2952 bool first_class = true;
2953
bbd08a5d
ML
2954 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2955 it2 != split_map.end (); ++it2)
5ebd0e61
ML
2956 {
2957 congruence_class *new_cls;
2958 new_cls = new congruence_class (class_id++);
2959
2960 for (unsigned k = 0; k < (*it2).second.length (); k++)
2961 add_item_to_class (new_cls, (*it2).second[k]);
2962
2963 worklist_push (new_cls);
2964 newly_created_classes++;
2965
2966 if (first_class)
2967 {
2968 (*it)->classes[i] = new_cls;
2969 first_class = false;
2970 }
2971 else
2972 {
2973 new_classes.safe_push (new_cls);
2974 m_classes_count++;
2975 }
2976 }
2977 }
bbd08a5d
ML
2978
2979 /* Release memory. */
2980 for (subdivide_hash_map::iterator it2 = split_map.begin ();
2981 it2 != split_map.end (); ++it2)
2982 {
2983 delete (*it2).first;
2984 (*it2).second.release ();
2985 }
5ebd0e61
ML
2986 }
2987 }
2988
2989 for (unsigned i = 0; i < new_classes.length (); i++)
2990 (*it)->classes.safe_push (new_classes[i]);
2991 }
2992
2993 return newly_created_classes;
2994}
2995
b84d4347
ML
2996/* Verify congruence classes if checking is enabled. */
2997
2998void
2999sem_item_optimizer::verify_classes (void)
3000{
3001#if ENABLE_CHECKING
3002 for (hash_table <congruence_class_group_hash>::iterator it = m_classes.begin ();
3003 it != m_classes.end (); ++it)
3004 {
3005 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3006 {
3007 congruence_class *cls = (*it)->classes[i];
3008
3009 gcc_checking_assert (cls);
3010 gcc_checking_assert (cls->members.length () > 0);
3011
3012 for (unsigned int j = 0; j < cls->members.length (); j++)
3013 {
3014 sem_item *item = cls->members[j];
3015
3016 gcc_checking_assert (item);
3017 gcc_checking_assert (item->cls == cls);
3018
3019 for (unsigned k = 0; k < item->usages.length (); k++)
3020 {
3021 sem_usage_pair *usage = item->usages[k];
3022 gcc_checking_assert (usage->item->index_in_class <
3023 usage->item->cls->members.length ());
3024 }
3025 }
3026 }
3027 }
3028#endif
3029}
3030
3031/* Disposes split map traverse function. CLS_PTR is pointer to congruence
3032 class, BSLOT is bitmap slot we want to release. DATA is mandatory,
3033 but unused argument. */
3034
3035bool
3036sem_item_optimizer::release_split_map (congruence_class * const &,
3037 bitmap const &b, traverse_split_pair *)
3038{
3039 bitmap bmp = b;
3040
3041 BITMAP_FREE (bmp);
3042
3043 return true;
3044}
3045
3046/* Process split operation for a class given as pointer CLS_PTR,
3047 where bitmap B splits congruence class members. DATA is used
3048 as argument of split pair. */
3049
3050bool
3051sem_item_optimizer::traverse_congruence_split (congruence_class * const &cls,
3052 bitmap const &b, traverse_split_pair *pair)
3053{
3054 sem_item_optimizer *optimizer = pair->optimizer;
3055 const congruence_class *splitter_cls = pair->cls;
3056
3057 /* If counted bits are greater than zero and less than the number of members
3058 a group will be splitted. */
3059 unsigned popcount = bitmap_count_bits (b);
3060
3061 if (popcount > 0 && popcount < cls->members.length ())
3062 {
3063 congruence_class* newclasses[2] = { new congruence_class (class_id++), new congruence_class (class_id++) };
3064
3065 for (unsigned int i = 0; i < cls->members.length (); i++)
3066 {
3067 int target = bitmap_bit_p (b, i);
3068 congruence_class *tc = newclasses[target];
3069
3070 add_item_to_class (tc, cls->members[i]);
3071 }
3072
3073#ifdef ENABLE_CHECKING
3074 for (unsigned int i = 0; i < 2; i++)
3075 gcc_checking_assert (newclasses[i]->members.length ());
3076#endif
3077
3078 if (splitter_cls == cls)
3079 optimizer->splitter_class_removed = true;
3080
3081 /* Remove old class from worklist if presented. */
3082 bool in_worklist = cls->in_worklist;
3083
3084 if (in_worklist)
3085 cls->in_worklist = false;
3086
3087 congruence_class_group g;
3088 g.hash = cls->members[0]->get_hash ();
3089 g.type = cls->members[0]->type;
3090
3091 congruence_class_group *slot = optimizer->m_classes.find(&g);
3092
3093 for (unsigned int i = 0; i < slot->classes.length (); i++)
3094 if (slot->classes[i] == cls)
3095 {
3096 slot->classes.ordered_remove (i);
3097 break;
3098 }
3099
3100 /* New class will be inserted and integrated to work list. */
3101 for (unsigned int i = 0; i < 2; i++)
3102 optimizer->add_class (newclasses[i]);
3103
3104 /* Two classes replace one, so that increment just by one. */
3105 optimizer->m_classes_count++;
3106
3107 /* If OLD class was presented in the worklist, we remove the class
3108 and replace it will both newly created classes. */
3109 if (in_worklist)
3110 for (unsigned int i = 0; i < 2; i++)
3111 optimizer->worklist_push (newclasses[i]);
3112 else /* Just smaller class is inserted. */
3113 {
3114 unsigned int smaller_index = newclasses[0]->members.length () <
3115 newclasses[1]->members.length () ?
3116 0 : 1;
3117 optimizer->worklist_push (newclasses[smaller_index]);
3118 }
3119
3120 if (dump_file && (dump_flags & TDF_DETAILS))
3121 {
3122 fprintf (dump_file, " congruence class splitted:\n");
3123 cls->dump (dump_file, 4);
3124
3125 fprintf (dump_file, " newly created groups:\n");
3126 for (unsigned int i = 0; i < 2; i++)
3127 newclasses[i]->dump (dump_file, 4);
3128 }
3129
3130 /* Release class if not presented in work list. */
3131 if (!in_worklist)
3132 delete cls;
3133 }
3134
3135
3136 return true;
3137}
3138
3139/* Tests if a class CLS used as INDEXth splits any congruence classes.
3140 Bitmap stack BMSTACK is used for bitmap allocation. */
3141
3142void
3143sem_item_optimizer::do_congruence_step_for_index (congruence_class *cls,
3144 unsigned int index)
3145{
3146 hash_map <congruence_class *, bitmap> split_map;
3147
3148 for (unsigned int i = 0; i < cls->members.length (); i++)
3149 {
3150 sem_item *item = cls->members[i];
3151
3152 /* Iterate all usages that have INDEX as usage of the item. */
3153 for (unsigned int j = 0; j < item->usages.length (); j++)
3154 {
3155 sem_usage_pair *usage = item->usages[j];
3156
3157 if (usage->index != index)
3158 continue;
3159
3160 bitmap *slot = split_map.get (usage->item->cls);
3161 bitmap b;
3162
3163 if(!slot)
3164 {
3165 b = BITMAP_ALLOC (&m_bmstack);
3166 split_map.put (usage->item->cls, b);
3167 }
3168 else
3169 b = *slot;
3170
3171#if ENABLE_CHECKING
3172 gcc_checking_assert (usage->item->cls);
3173 gcc_checking_assert (usage->item->index_in_class <
3174 usage->item->cls->members.length ());
3175#endif
3176
3177 bitmap_set_bit (b, usage->item->index_in_class);
3178 }
3179 }
3180
3181 traverse_split_pair pair;
3182 pair.optimizer = this;
3183 pair.cls = cls;
3184
3185 splitter_class_removed = false;
3186 split_map.traverse
3187 <traverse_split_pair *, sem_item_optimizer::traverse_congruence_split> (&pair);
3188
3189 /* Bitmap clean-up. */
3190 split_map.traverse
3191 <traverse_split_pair *, sem_item_optimizer::release_split_map> (NULL);
3192}
3193
3194/* Every usage of a congruence class CLS is a candidate that can split the
3195 collection of classes. Bitmap stack BMSTACK is used for bitmap
3196 allocation. */
3197
3198void
3199sem_item_optimizer::do_congruence_step (congruence_class *cls)
3200{
3201 bitmap_iterator bi;
3202 unsigned int i;
3203
3204 bitmap usage = BITMAP_ALLOC (&m_bmstack);
3205
3206 for (unsigned int i = 0; i < cls->members.length (); i++)
3207 bitmap_ior_into (usage, cls->members[i]->usage_index_bitmap);
3208
3209 EXECUTE_IF_SET_IN_BITMAP (usage, 0, i, bi)
3210 {
3211 if (dump_file && (dump_flags & TDF_DETAILS))
3212 fprintf (dump_file, " processing congruece step for class: %u, index: %u\n",
3213 cls->id, i);
3214
3215 do_congruence_step_for_index (cls, i);
3216
3217 if (splitter_class_removed)
3218 break;
3219 }
3220
3221 BITMAP_FREE (usage);
3222}
3223
3224/* Adds a newly created congruence class CLS to worklist. */
3225
3226void
3227sem_item_optimizer::worklist_push (congruence_class *cls)
3228{
3229 /* Return if the class CLS is already presented in work list. */
3230 if (cls->in_worklist)
3231 return;
3232
3233 cls->in_worklist = true;
3234 worklist.push_back (cls);
3235}
3236
3237/* Pops a class from worklist. */
3238
3239congruence_class *
3240sem_item_optimizer::worklist_pop (void)
3241{
3242 congruence_class *cls;
3243
3244 while (!worklist.empty ())
3245 {
3246 cls = worklist.front ();
3247 worklist.pop_front ();
3248 if (cls->in_worklist)
3249 {
3250 cls->in_worklist = false;
3251
3252 return cls;
3253 }
3254 else
3255 {
3256 /* Work list item was already intended to be removed.
3257 The only reason for doing it is to split a class.
3258 Thus, the class CLS is deleted. */
3259 delete cls;
3260 }
3261 }
3262
3263 return NULL;
3264}
3265
3266/* Iterative congruence reduction function. */
3267
3268void
3269sem_item_optimizer::process_cong_reduction (void)
3270{
3271 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3272 it != m_classes.end (); ++it)
3273 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3274 if ((*it)->classes[i]->is_class_used ())
3275 worklist_push ((*it)->classes[i]);
3276
3277 if (dump_file)
3278 fprintf (dump_file, "Worklist has been filled with: %lu\n",
10568163 3279 (unsigned long) worklist.size ());
b84d4347
ML
3280
3281 if (dump_file && (dump_flags & TDF_DETAILS))
3282 fprintf (dump_file, "Congruence class reduction\n");
3283
3284 congruence_class *cls;
5ebd0e61
ML
3285
3286 /* Process complete congruence reduction. */
b84d4347
ML
3287 while ((cls = worklist_pop ()) != NULL)
3288 do_congruence_step (cls);
5ebd0e61
ML
3289
3290 /* Subdivide newly created classes according to references. */
3291 unsigned new_classes = subdivide_classes_by_sensitive_refs ();
3292
3293 if (dump_file)
3294 fprintf (dump_file, "Address reference subdivision created: %u "
3295 "new classes.\n", new_classes);
b84d4347
ML
3296}
3297
3298/* Debug function prints all informations about congruence classes. */
3299
3300void
3301sem_item_optimizer::dump_cong_classes (void)
3302{
3303 if (!dump_file)
3304 return;
3305
3306 fprintf (dump_file,
3307 "Congruence classes: %u (unique hash values: %lu), with total: %u items\n",
10568163 3308 m_classes_count, (unsigned long) m_classes.elements(), m_items.length ());
b84d4347
ML
3309
3310 /* Histogram calculation. */
3311 unsigned int max_index = 0;
3312 unsigned int* histogram = XCNEWVEC (unsigned int, m_items.length () + 1);
3313
3314 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3315 it != m_classes.end (); ++it)
3316
3317 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3318 {
3319 unsigned int c = (*it)->classes[i]->members.length ();
3320 histogram[c]++;
3321
3322 if (c > max_index)
3323 max_index = c;
3324 }
3325
3326 fprintf (dump_file,
3327 "Class size histogram [num of members]: number of classe number of classess\n");
3328
3329 for (unsigned int i = 0; i <= max_index; i++)
3330 if (histogram[i])
3331 fprintf (dump_file, "[%u]: %u classes\n", i, histogram[i]);
3332
3333 fprintf (dump_file, "\n\n");
3334
3335
3336 if (dump_flags & TDF_DETAILS)
3337 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3338 it != m_classes.end (); ++it)
3339 {
3340 fprintf (dump_file, " group: with %u classes:\n", (*it)->classes.length ());
3341
3342 for (unsigned i = 0; i < (*it)->classes.length (); i++)
3343 {
3344 (*it)->classes[i]->dump (dump_file, 4);
3345
3346 if(i < (*it)->classes.length () - 1)
3347 fprintf (dump_file, " ");
3348 }
3349 }
3350
3351 free (histogram);
3352}
3353
3354/* After reduction is done, we can declare all items in a group
3355 to be equal. PREV_CLASS_COUNT is start number of classes
bd31fe14
ML
3356 before reduction. True is returned if there's a merge operation
3357 processed. */
b84d4347 3358
bd31fe14 3359bool
b84d4347
ML
3360sem_item_optimizer::merge_classes (unsigned int prev_class_count)
3361{
3362 unsigned int item_count = m_items.length ();
3363 unsigned int class_count = m_classes_count;
3364 unsigned int equal_items = item_count - class_count;
3365
3366 unsigned int non_singular_classes_count = 0;
3367 unsigned int non_singular_classes_sum = 0;
3368
bd31fe14
ML
3369 bool merged_p = false;
3370
b84d4347
ML
3371 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3372 it != m_classes.end (); ++it)
3373 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3374 {
3375 congruence_class *c = (*it)->classes[i];
3376 if (c->members.length () > 1)
3377 {
3378 non_singular_classes_count++;
3379 non_singular_classes_sum += c->members.length ();
3380 }
3381 }
3382
3383 if (dump_file)
3384 {
3385 fprintf (dump_file, "\nItem count: %u\n", item_count);
3386 fprintf (dump_file, "Congruent classes before: %u, after: %u\n",
3387 prev_class_count, class_count);
3388 fprintf (dump_file, "Average class size before: %.2f, after: %.2f\n",
f1c859ee
ML
3389 prev_class_count ? 1.0f * item_count / prev_class_count : 0.0f,
3390 class_count ? 1.0f * item_count / class_count : 0.0f);
b84d4347 3391 fprintf (dump_file, "Average non-singular class size: %.2f, count: %u\n",
f1c859ee
ML
3392 non_singular_classes_count ? 1.0f * non_singular_classes_sum /
3393 non_singular_classes_count : 0.0f,
b84d4347
ML
3394 non_singular_classes_count);
3395 fprintf (dump_file, "Equal symbols: %u\n", equal_items);
3396 fprintf (dump_file, "Fraction of visited symbols: %.2f%%\n\n",
f1c859ee 3397 item_count ? 100.0f * equal_items / item_count : 0.0f);
b84d4347
ML
3398 }
3399
3400 for (hash_table<congruence_class_group_hash>::iterator it = m_classes.begin ();
3401 it != m_classes.end (); ++it)
3402 for (unsigned int i = 0; i < (*it)->classes.length (); i++)
3403 {
3404 congruence_class *c = (*it)->classes[i];
3405
3406 if (c->members.length () == 1)
3407 continue;
3408
3409 gcc_assert (c->members.length ());
3410
3411 sem_item *source = c->members[0];
3412
3413 for (unsigned int j = 1; j < c->members.length (); j++)
3414 {
3415 sem_item *alias = c->members[j];
b84d4347
ML
3416
3417 if (dump_file)
3418 {
3419 fprintf (dump_file, "Semantic equality hit:%s->%s\n",
1aec2ecc
ML
3420 xstrdup_for_dump (source->node->name ()),
3421 xstrdup_for_dump (alias->node->name ()));
b84d4347 3422 fprintf (dump_file, "Assembler symbol names:%s->%s\n",
1aec2ecc
ML
3423 xstrdup_for_dump (source->node->asm_name ()),
3424 xstrdup_for_dump (alias->node->asm_name ()));
b84d4347
ML
3425 }
3426
185c9e56
ML
3427 if (lookup_attribute ("no_icf", DECL_ATTRIBUTES (alias->decl)))
3428 {
3429 if (dump_file)
3430 fprintf (dump_file,
3431 "Merge operation is skipped due to no_icf "
3432 "attribute.\n\n");
3433
3434 continue;
3435 }
3436
b84d4347
ML
3437 if (dump_file && (dump_flags & TDF_DETAILS))
3438 {
3439 source->dump_to_file (dump_file);
3440 alias->dump_to_file (dump_file);
3441 }
3442
7aeb92b4
ML
3443 if (dbg_cnt (merged_ipa_icf))
3444 merged_p |= source->merge (alias);
b84d4347
ML
3445 }
3446 }
bd31fe14
ML
3447
3448 return merged_p;
b84d4347
ML
3449}
3450
3451/* Dump function prints all class members to a FILE with an INDENT. */
3452
3453void
3454congruence_class::dump (FILE *file, unsigned int indent) const
3455{
3456 FPRINTF_SPACES (file, indent, "class with id: %u, hash: %u, items: %u\n",
3457 id, members[0]->get_hash (), members.length ());
3458
3459 FPUTS_SPACES (file, indent + 2, "");
3460 for (unsigned i = 0; i < members.length (); i++)
1aec2ecc
ML
3461 fprintf (file, "%s(%p/%u) ", members[i]->node->asm_name (),
3462 (void *) members[i]->decl,
b84d4347
ML
3463 members[i]->node->order);
3464
3465 fprintf (file, "\n");
3466}
3467
3468/* Returns true if there's a member that is used from another group. */
3469
3470bool
3471congruence_class::is_class_used (void)
3472{
3473 for (unsigned int i = 0; i < members.length (); i++)
3474 if (members[i]->usages.length ())
3475 return true;
3476
3477 return false;
3478}
3479
b84d4347
ML
3480/* Generate pass summary for IPA ICF pass. */
3481
3482static void
3483ipa_icf_generate_summary (void)
3484{
3485 if (!optimizer)
3486 optimizer = new sem_item_optimizer ();
3487
e3e968e9 3488 optimizer->register_hooks ();
b84d4347
ML
3489 optimizer->parse_funcs_and_vars ();
3490}
3491
3492/* Write pass summary for IPA ICF pass. */
3493
3494static void
3495ipa_icf_write_summary (void)
3496{
3497 gcc_assert (optimizer);
3498
3499 optimizer->write_summary ();
3500}
3501
3502/* Read pass summary for IPA ICF pass. */
3503
3504static void
3505ipa_icf_read_summary (void)
3506{
3507 if (!optimizer)
3508 optimizer = new sem_item_optimizer ();
3509
3510 optimizer->read_summary ();
3511 optimizer->register_hooks ();
3512}
3513
3514/* Semantic equality exection function. */
3515
3516static unsigned int
3517ipa_icf_driver (void)
3518{
3519 gcc_assert (optimizer);
3520
bd31fe14 3521 bool merged_p = optimizer->execute ();
b84d4347
ML
3522
3523 delete optimizer;
9612a39a 3524 optimizer = NULL;
b84d4347 3525
bd31fe14 3526 return merged_p ? TODO_remove_functions : 0;
b84d4347
ML
3527}
3528
3529const pass_data pass_data_ipa_icf =
3530{
3531 IPA_PASS, /* type */
3532 "icf", /* name */
3533 OPTGROUP_IPA, /* optinfo_flags */
3534 TV_IPA_ICF, /* tv_id */
3535 0, /* properties_required */
3536 0, /* properties_provided */
3537 0, /* properties_destroyed */
3538 0, /* todo_flags_start */
3539 0, /* todo_flags_finish */
3540};
3541
3542class pass_ipa_icf : public ipa_opt_pass_d
3543{
3544public:
3545 pass_ipa_icf (gcc::context *ctxt)
3546 : ipa_opt_pass_d (pass_data_ipa_icf, ctxt,
3547 ipa_icf_generate_summary, /* generate_summary */
3548 ipa_icf_write_summary, /* write_summary */
3549 ipa_icf_read_summary, /* read_summary */
3550 NULL, /*
3551 write_optimization_summary */
3552 NULL, /*
3553 read_optimization_summary */
3554 NULL, /* stmt_fixup */
3555 0, /* function_transform_todo_flags_start */
3556 NULL, /* function_transform */
3557 NULL) /* variable_transform */
3558 {}
3559
3560 /* opt_pass methods: */
3561 virtual bool gate (function *)
3562 {
b16650ac 3563 return in_lto_p || flag_ipa_icf_variables || flag_ipa_icf_functions;
b84d4347
ML
3564 }
3565
3566 virtual unsigned int execute (function *)
3567 {
3568 return ipa_icf_driver();
3569 }
3570}; // class pass_ipa_icf
3571
3572} // ipa_icf namespace
3573
3574ipa_opt_pass_d *
3575make_pass_ipa_icf (gcc::context *ctxt)
3576{
3577 return new ipa_icf::pass_ipa_icf (ctxt);
3578}