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