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