Line data Source code
1 : /* Interprocedural analyses.
2 : Copyright (C) 2005-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "rtl.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "alloc-pool.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "tree-streamer.h"
31 : #include "cgraph.h"
32 : #include "diagnostic.h"
33 : #include "fold-const.h"
34 : #include "gimple-iterator.h"
35 : #include "gimple-fold.h"
36 : #include "tree-eh.h"
37 : #include "calls.h"
38 : #include "stor-layout.h"
39 : #include "print-tree.h"
40 : #include "gimplify.h"
41 : #include "gimplify-me.h"
42 : #include "gimple-walk.h"
43 : #include "symbol-summary.h"
44 : #include "sreal.h"
45 : #include "ipa-cp.h"
46 : #include "ipa-prop.h"
47 : #include "tree-cfg.h"
48 : #include "tree-dfa.h"
49 : #include "tree-inline.h"
50 : #include "ipa-fnsummary.h"
51 : #include "gimple-pretty-print.h"
52 : #include "ipa-utils.h"
53 : #include "dbgcnt.h"
54 : #include "domwalk.h"
55 : #include "builtins.h"
56 : #include "tree-cfgcleanup.h"
57 : #include "options.h"
58 : #include "symtab-clones.h"
59 : #include "attr-fnspec.h"
60 : #include "gimple-range.h"
61 : #include "value-range-storage.h"
62 : #include "vr-values.h"
63 : #include "lto-streamer.h"
64 : #include "attribs.h"
65 : #include "attr-callback.h"
66 :
67 : /* Function summary where the parameter infos are actually stored. */
68 : ipa_node_params_t *ipa_node_params_sum = NULL;
69 :
70 : function_summary <ipcp_transformation *> *ipcp_transformation_sum = NULL;
71 :
72 : /* Edge summary for IPA-CP edge information. */
73 : ipa_edge_args_sum_t *ipa_edge_args_sum;
74 :
75 : /* Traits for a hash table for reusing ranges. */
76 :
77 : struct ipa_vr_ggc_hash_traits : public ggc_cache_remove <ipa_vr *>
78 : {
79 : typedef ipa_vr *value_type;
80 : typedef const vrange *compare_type;
81 : static hashval_t
82 17073835 : hash (const ipa_vr *p)
83 : {
84 : // This never get called, except in the verification code, as
85 : // ipa_get_value_range() calculates the hash itself. This
86 : // function is mostly here for completness' sake.
87 17073835 : value_range vr;
88 17073835 : p->get_vrange (vr);
89 17073835 : inchash::hash hstate;
90 17073835 : add_vrange (vr, hstate);
91 17073835 : return hstate.end ();
92 17073835 : }
93 : static bool
94 23736191 : equal (const ipa_vr *a, const vrange *b)
95 : {
96 23736191 : return a->equal_p (*b);
97 : }
98 : static const bool empty_zero_p = true;
99 : static void
100 1837 : mark_empty (ipa_vr *&p)
101 : {
102 1837 : p = NULL;
103 : }
104 : static bool
105 : is_empty (const ipa_vr *p)
106 : {
107 : return p == NULL;
108 : }
109 : static bool
110 43974040 : is_deleted (const ipa_vr *p)
111 : {
112 43974040 : return p == reinterpret_cast<const ipa_vr *> (1);
113 : }
114 : static void
115 148063 : mark_deleted (ipa_vr *&p)
116 : {
117 148063 : p = reinterpret_cast<ipa_vr *> (1);
118 : }
119 : };
120 :
121 : /* Hash table for avoid repeated allocations of equal ranges. */
122 : static GTY ((cache)) hash_table<ipa_vr_ggc_hash_traits> *ipa_vr_hash_table;
123 :
124 : /* Holders of ipa cgraph hooks: */
125 : static struct cgraph_node_hook_list *function_insertion_hook_holder;
126 :
127 : /* Description of a reference to an IPA constant. */
128 : struct ipa_cst_ref_desc
129 : {
130 : /* Edge that corresponds to the statement which took the reference. */
131 : struct cgraph_edge *cs;
132 : /* Linked list of duplicates created when call graph edges are cloned. */
133 : struct ipa_cst_ref_desc *next_duplicate;
134 : /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
135 : is out of control. */
136 : int refcount;
137 : };
138 :
139 : /* Allocation pool for reference descriptions. */
140 :
141 : static object_allocator<ipa_cst_ref_desc> ipa_refdesc_pool
142 : ("IPA-PROP ref descriptions");
143 :
144 655188 : ipa_vr::ipa_vr ()
145 655188 : : m_storage (NULL),
146 655188 : m_type (NULL)
147 : {
148 655188 : }
149 :
150 756612 : ipa_vr::ipa_vr (const vrange &r)
151 756612 : : m_storage (ggc_alloc_vrange_storage (r)),
152 756612 : m_type (r.type ())
153 : {
154 756612 : }
155 :
156 : bool
157 23736191 : ipa_vr::equal_p (const vrange &r) const
158 : {
159 23736191 : gcc_checking_assert (!r.undefined_p ());
160 23736191 : return (types_compatible_p (m_type, r.type ()) && m_storage->equal_p (r));
161 : }
162 :
163 : bool
164 83537 : ipa_vr::equal_p (const ipa_vr &o) const
165 : {
166 83537 : if (!known_p ())
167 0 : return !o.known_p ();
168 :
169 83537 : if (!types_compatible_p (m_type, o.m_type))
170 : return false;
171 :
172 83537 : value_range r;
173 83537 : o.get_vrange (r);
174 83537 : return m_storage->equal_p (r);
175 83537 : }
176 :
177 : void
178 28708903 : ipa_vr::get_vrange (value_range &r) const
179 : {
180 28708903 : r.set_range_class (m_type);
181 28708903 : m_storage->get_vrange (r, m_type);
182 28708903 : }
183 :
184 : void
185 1924 : ipa_vr::set_unknown ()
186 : {
187 1924 : if (m_storage)
188 1924 : ggc_free (m_storage);
189 :
190 1924 : m_storage = NULL;
191 1924 : }
192 :
193 : void
194 611935 : ipa_vr::streamer_read (lto_input_block *ib, data_in *data_in)
195 : {
196 611935 : struct bitpack_d bp = streamer_read_bitpack (ib);
197 611935 : bool known = bp_unpack_value (&bp, 1);
198 611935 : if (known)
199 : {
200 438037 : value_range vr;
201 438037 : streamer_read_value_range (ib, data_in, vr);
202 438037 : if (!m_storage || !m_storage->fits_p (vr))
203 : {
204 438037 : if (m_storage)
205 0 : ggc_free (m_storage);
206 438037 : m_storage = ggc_alloc_vrange_storage (vr);
207 : }
208 438037 : m_storage->set_vrange (vr);
209 438037 : m_type = vr.type ();
210 438037 : }
211 : else
212 : {
213 173898 : m_storage = NULL;
214 173898 : m_type = NULL;
215 : }
216 611935 : }
217 :
218 : void
219 469008 : ipa_vr::streamer_write (output_block *ob) const
220 : {
221 469008 : struct bitpack_d bp = bitpack_create (ob->main_stream);
222 469008 : bp_pack_value (&bp, !!m_storage, 1);
223 469008 : streamer_write_bitpack (&bp);
224 469008 : if (m_storage)
225 : {
226 466498 : value_range vr (m_type);
227 466498 : m_storage->get_vrange (vr, m_type);
228 466498 : streamer_write_vrange (ob, vr);
229 466498 : }
230 469008 : }
231 :
232 : void
233 729 : ipa_vr::dump (FILE *out) const
234 : {
235 729 : if (known_p ())
236 : {
237 729 : value_range vr (m_type);
238 729 : m_storage->get_vrange (vr, m_type);
239 729 : vr.dump (out);
240 729 : }
241 : else
242 0 : fprintf (out, "NO RANGE");
243 729 : }
244 :
245 : // These stubs are because we use an ipa_vr in a hash_traits and
246 : // hash-traits.h defines an extern of gt_ggc_mx (T &) instead of
247 : // picking up the gt_ggc_mx (T *) version.
248 : void
249 0 : gt_pch_nx (ipa_vr *&x)
250 : {
251 0 : return gt_pch_nx ((ipa_vr *) x);
252 : }
253 :
254 : void
255 0 : gt_ggc_mx (ipa_vr *&x)
256 : {
257 0 : return gt_ggc_mx ((ipa_vr *) x);
258 : }
259 :
260 : /* Analysis summery of function call return value. */
261 : struct GTY(()) ipa_return_value_summary
262 : {
263 : /* Known value range.
264 : This needs to be wrapped in struccture due to specific way
265 : we allocate ipa_vr. */
266 : ipa_vr *vr;
267 : };
268 :
269 : /* Function summary for return values. */
270 : class ipa_return_value_sum_t : public function_summary <ipa_return_value_summary *>
271 : {
272 : public:
273 86934 : ipa_return_value_sum_t (symbol_table *table, bool ggc):
274 173868 : function_summary <ipa_return_value_summary *> (table, ggc) { }
275 :
276 : /* Hook that is called by summary when a node is duplicated. */
277 545082 : void duplicate (cgraph_node *,
278 : cgraph_node *,
279 : ipa_return_value_summary *data,
280 : ipa_return_value_summary *data2) final override
281 : {
282 545082 : *data2=*data;
283 545082 : }
284 : };
285 :
286 : /* Structure holding the information that all stores to FLD_OFFSET (measured in
287 : bytes) of a particular record type REC_TYPE was storing a pointer to
288 : function FN or that there were multiple functions, which is denoted by fn
289 : being nullptr. */
290 :
291 : struct GTY((for_user)) noted_fnptr_store
292 : {
293 : tree rec_type;
294 : tree fn;
295 : unsigned fld_offset;
296 : };
297 :
298 : /* Hash traits to have a hash table of noted_fnptr_stores. */
299 :
300 : struct noted_fnptr_hasher : ggc_ptr_hash <noted_fnptr_store>
301 : {
302 : static hashval_t hash (noted_fnptr_store *);
303 : static bool equal (noted_fnptr_store *,
304 : noted_fnptr_store *);
305 : };
306 :
307 : hashval_t
308 607284 : noted_fnptr_hasher::hash (noted_fnptr_store *val)
309 : {
310 1214568 : return iterative_hash_host_wide_int (val->fld_offset,
311 607284 : TYPE_UID (val->rec_type));
312 : }
313 :
314 : bool
315 523789 : noted_fnptr_hasher::equal (noted_fnptr_store *v1,
316 : noted_fnptr_store *v2)
317 : {
318 523789 : return (v1->rec_type == v2->rec_type
319 523789 : && v1->fld_offset == v2->fld_offset);
320 : }
321 :
322 :
323 : /* Structore holding the information that all stores to OFFSET of a particular
324 : record type RECTYPE was storing a pointer to specific function or that there
325 : were multiple such functions. */
326 :
327 : static GTY(()) hash_table <noted_fnptr_hasher> *noted_fnptrs_in_records;
328 :
329 : /* Variable hoding the return value summary. */
330 : static GTY(()) function_summary <ipa_return_value_summary *> *ipa_return_value_sum;
331 :
332 :
333 : /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
334 : with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
335 :
336 : static bool
337 4054566 : ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
338 : {
339 4054566 : tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
340 :
341 4054566 : if (!fs_opts)
342 : return false;
343 550269 : return !opt_for_fn (node->decl, optimize) || !opt_for_fn (node->decl, flag_ipa_cp);
344 : }
345 :
346 : /* Return index of the formal whose tree is PTREE in function which corresponds
347 : to INFO. */
348 :
349 : static int
350 42409803 : ipa_get_param_decl_index_1 (vec<ipa_param_descriptor, va_gc> *descriptors,
351 : tree ptree)
352 : {
353 42409803 : int i, count;
354 :
355 42409803 : count = vec_safe_length (descriptors);
356 86869989 : for (i = 0; i < count; i++)
357 76376598 : if ((*descriptors)[i].decl_or_type == ptree)
358 : return i;
359 :
360 : return -1;
361 : }
362 :
363 : /* Return index of the formal whose tree is PTREE in function which corresponds
364 : to INFO. */
365 :
366 : int
367 26434489 : ipa_get_param_decl_index (class ipa_node_params *info, tree ptree)
368 : {
369 26434489 : return ipa_get_param_decl_index_1 (info->descriptors, ptree);
370 : }
371 :
372 : static void
373 : ipa_duplicate_jump_function (cgraph_edge *src, cgraph_edge *dst,
374 : ipa_jump_func *src_jf, ipa_jump_func *dst_jf);
375 :
376 : /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
377 : NODE. */
378 :
379 : static void
380 5482435 : ipa_populate_param_decls (struct cgraph_node *node,
381 : vec<ipa_param_descriptor, va_gc> &descriptors)
382 : {
383 5482435 : tree fndecl;
384 5482435 : tree fnargs;
385 5482435 : tree parm;
386 5482435 : int param_num;
387 :
388 5482435 : fndecl = node->decl;
389 5482435 : gcc_assert (gimple_has_body_p (fndecl));
390 5482435 : fnargs = DECL_ARGUMENTS (fndecl);
391 5482435 : param_num = 0;
392 18068004 : for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
393 : {
394 12585569 : descriptors[param_num].decl_or_type = parm;
395 12585569 : unsigned int cost = estimate_move_cost (TREE_TYPE (parm), true);
396 12585569 : descriptors[param_num].move_cost = cost;
397 : /* Watch overflow, move_cost is a bitfield. */
398 12585569 : gcc_checking_assert (cost == descriptors[param_num].move_cost);
399 12585569 : param_num++;
400 : }
401 5482435 : }
402 :
403 : /* Return how many formal parameters FNDECL has. */
404 :
405 : int
406 15073100 : count_formal_params (tree fndecl)
407 : {
408 15073100 : tree parm;
409 15073100 : int count = 0;
410 15073100 : gcc_assert (gimple_has_body_p (fndecl));
411 :
412 46575991 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
413 31502891 : count++;
414 :
415 15073100 : return count;
416 : }
417 :
418 : /* Return the declaration of Ith formal parameter of the function corresponding
419 : to INFO. Note there is no setter function as this array is built just once
420 : using ipa_initialize_node_params. */
421 :
422 : void
423 613 : ipa_dump_param (FILE *file, class ipa_node_params *info, int i)
424 : {
425 613 : fprintf (file, "param #%i", i);
426 613 : if ((*info->descriptors)[i].decl_or_type)
427 : {
428 613 : fprintf (file, " ");
429 613 : print_generic_expr (file, (*info->descriptors)[i].decl_or_type);
430 : }
431 613 : }
432 :
433 : /* If necessary, allocate vector of parameter descriptors in info of NODE.
434 : Return true if they were allocated, false if not. */
435 :
436 : static bool
437 6363329 : ipa_alloc_node_params (struct cgraph_node *node, int param_count)
438 : {
439 6363329 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
440 :
441 6363329 : if (!info->descriptors && param_count)
442 : {
443 5534289 : vec_safe_grow_cleared (info->descriptors, param_count, true);
444 5534289 : return true;
445 : }
446 : else
447 : return false;
448 : }
449 :
450 : /* Initialize the ipa_node_params structure associated with NODE by counting
451 : the function parameters, creating the descriptors and populating their
452 : param_decls. */
453 :
454 : void
455 6285699 : ipa_initialize_node_params (struct cgraph_node *node)
456 : {
457 6285699 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
458 :
459 6285699 : if (!info->descriptors
460 6285699 : && ipa_alloc_node_params (node, count_formal_params (node->decl)))
461 5481127 : ipa_populate_param_decls (node, *info->descriptors);
462 6285699 : }
463 :
464 : /* Print VAL which is extracted from a jump function to F. */
465 :
466 : void
467 1195 : ipa_print_constant_value (FILE *f, tree val)
468 : {
469 1195 : print_generic_expr (f, val);
470 :
471 : /* This is in keeping with values_equal_for_ipcp_p. */
472 1195 : if (TREE_CODE (val) == ADDR_EXPR
473 1195 : && (TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL
474 251 : || (TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
475 104 : && DECL_IN_CONSTANT_POOL (TREE_OPERAND (val, 0)))))
476 : {
477 0 : fputs (" -> ", f);
478 0 : print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)));
479 : }
480 1195 : }
481 :
482 : /* Print contents of JFUNC to F. If CTX is non-NULL, dump it too. */
483 :
484 : DEBUG_FUNCTION void
485 1032 : ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
486 : class ipa_polymorphic_call_context *ctx)
487 : {
488 1032 : enum jump_func_type type = jump_func->type;
489 :
490 1032 : if (type == IPA_JF_UNKNOWN)
491 260 : fprintf (f, "UNKNOWN\n");
492 772 : else if (type == IPA_JF_CONST)
493 : {
494 303 : fprintf (f, "CONST: ");
495 303 : ipa_print_constant_value (f, jump_func->value.constant.value);
496 303 : fprintf (f, "\n");
497 : }
498 469 : else if (type == IPA_JF_PASS_THROUGH)
499 : {
500 408 : fprintf (f, "PASS THROUGH: ");
501 408 : fprintf (f, "%d, op %s",
502 : jump_func->value.pass_through.formal_id,
503 : get_tree_code_name(jump_func->value.pass_through.operation));
504 408 : if (jump_func->value.pass_through.operation != NOP_EXPR)
505 : {
506 31 : fprintf (f, " ");
507 31 : if (jump_func->value.pass_through.operand)
508 27 : print_generic_expr (f, jump_func->value.pass_through.operand);
509 31 : fprintf (f, " (in type ");
510 31 : print_generic_expr (f, jump_func->value.pass_through.op_type);
511 31 : fprintf (f, ")");
512 : }
513 408 : if (jump_func->value.pass_through.agg_preserved)
514 134 : fprintf (f, ", agg_preserved");
515 408 : if (jump_func->value.pass_through.refdesc_decremented)
516 0 : fprintf (f, ", refdesc_decremented");
517 408 : fprintf (f, "\n");
518 : }
519 61 : else if (type == IPA_JF_ANCESTOR)
520 : {
521 61 : fprintf (f, "ANCESTOR: ");
522 61 : fprintf (f, "%d, offset " HOST_WIDE_INT_PRINT_DEC,
523 : jump_func->value.ancestor.formal_id,
524 : jump_func->value.ancestor.offset);
525 61 : if (jump_func->value.ancestor.agg_preserved)
526 29 : fprintf (f, ", agg_preserved");
527 61 : if (jump_func->value.ancestor.keep_null)
528 4 : fprintf (f, ", keep_null");
529 61 : fprintf (f, "\n");
530 : }
531 :
532 1032 : if (jump_func->agg.items)
533 : {
534 91 : struct ipa_agg_jf_item *item;
535 91 : int j;
536 :
537 182 : fprintf (f, " Aggregate passed by %s:\n",
538 91 : jump_func->agg.by_ref ? "reference" : "value");
539 377 : FOR_EACH_VEC_ELT (*jump_func->agg.items, j, item)
540 : {
541 195 : fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
542 : item->offset);
543 195 : fprintf (f, "type: ");
544 195 : print_generic_expr (f, item->type);
545 195 : fprintf (f, ", ");
546 195 : if (item->jftype == IPA_JF_PASS_THROUGH)
547 7 : fprintf (f, "PASS THROUGH: %d,",
548 : item->value.pass_through.formal_id);
549 188 : else if (item->jftype == IPA_JF_LOAD_AGG)
550 : {
551 11 : fprintf (f, "LOAD AGG: %d",
552 : item->value.pass_through.formal_id);
553 11 : fprintf (f, " [offset: " HOST_WIDE_INT_PRINT_DEC ", by %s],",
554 : item->value.load_agg.offset,
555 11 : item->value.load_agg.by_ref ? "reference"
556 : : "value");
557 : }
558 :
559 195 : if (item->jftype == IPA_JF_PASS_THROUGH
560 195 : || item->jftype == IPA_JF_LOAD_AGG)
561 : {
562 18 : fprintf (f, " op %s",
563 : get_tree_code_name (item->value.pass_through.operation));
564 18 : if (item->value.pass_through.operation != NOP_EXPR)
565 : {
566 9 : fprintf (f, " ");
567 9 : if (item->value.pass_through.operand)
568 8 : print_generic_expr (f, item->value.pass_through.operand);
569 9 : fprintf (f, " (in type ");
570 9 : print_generic_expr (f, jump_func->value.pass_through.op_type);
571 9 : fprintf (f, ")");
572 : }
573 : }
574 177 : else if (item->jftype == IPA_JF_CONST)
575 : {
576 177 : fprintf (f, "CONST: ");
577 177 : ipa_print_constant_value (f, item->value.constant);
578 : }
579 0 : else if (item->jftype == IPA_JF_UNKNOWN)
580 0 : fprintf (f, "UNKNOWN: " HOST_WIDE_INT_PRINT_DEC " bits",
581 0 : tree_to_uhwi (TYPE_SIZE (item->type)));
582 195 : fprintf (f, "\n");
583 : }
584 : }
585 :
586 1032 : if (ctx && !ctx->useless_p ())
587 : {
588 384 : fprintf (f, " Context: ");
589 384 : ctx->dump (dump_file);
590 : }
591 :
592 1032 : if (jump_func->m_vr)
593 : {
594 729 : fprintf (f, " ");
595 729 : jump_func->m_vr->dump (f);
596 729 : fprintf (f, "\n");
597 : }
598 : else
599 303 : fprintf (f, " Unknown VR\n");
600 1032 : }
601 :
602 : /* Print the jump functions associated with call graph edge CS to file F. */
603 :
604 : static void
605 737 : ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
606 : {
607 737 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
608 737 : int count = ipa_get_cs_argument_count (args);
609 :
610 1769 : for (int i = 0; i < count; i++)
611 : {
612 1032 : struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
613 1032 : class ipa_polymorphic_call_context *ctx
614 1032 : = ipa_get_ith_polymorhic_call_context (args, i);
615 :
616 1032 : fprintf (f, " param %d: ", i);
617 1032 : ipa_dump_jump_function (f, jump_func, ctx);
618 : }
619 737 : }
620 :
621 :
622 : /* Print the jump functions of all arguments on all call graph edges going from
623 : NODE to file F. */
624 :
625 : void
626 1072 : ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
627 : {
628 1072 : struct cgraph_edge *cs;
629 :
630 1072 : fprintf (f, " Jump functions of caller %s:\n", node->dump_name ());
631 2152 : for (cs = node->callees; cs; cs = cs->next_callee)
632 : {
633 :
634 1080 : fprintf (f, " callsite %s -> %s : \n",
635 : node->dump_name (),
636 1080 : cs->callee->dump_name ());
637 1080 : if (!ipa_edge_args_info_available_for_edge_p (cs))
638 490 : fprintf (f, " no arg info\n");
639 : else
640 590 : ipa_print_node_jump_functions_for_edge (f, cs);
641 : }
642 :
643 1219 : for (cs = node->indirect_calls; cs; cs = cs->next_callee)
644 : {
645 147 : fprintf (f, " ");
646 147 : cs->indirect_info->dump (f, false);
647 147 : if (cs->call_stmt)
648 : {
649 131 : fprintf (f, ", for stmt ");
650 131 : print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
651 : }
652 : else
653 16 : fprintf (f, "\n");
654 147 : if (!ipa_edge_args_info_available_for_edge_p (cs))
655 0 : fprintf (f, " no arg info\n");
656 : else
657 147 : ipa_print_node_jump_functions_for_edge (f, cs);
658 : }
659 1072 : }
660 :
661 : /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
662 :
663 : void
664 161 : ipa_print_all_jump_functions (FILE *f)
665 : {
666 161 : struct cgraph_node *node;
667 :
668 161 : fprintf (f, "\nJump functions:\n");
669 1219 : FOR_EACH_FUNCTION (node)
670 : {
671 1058 : ipa_print_node_jump_functions (f, node);
672 : }
673 161 : }
674 :
675 : /* Set jfunc to be a know-really nothing jump function. */
676 :
677 : static void
678 533189 : ipa_set_jf_unknown (struct ipa_jump_func *jfunc)
679 : {
680 533189 : jfunc->type = IPA_JF_UNKNOWN;
681 519104 : }
682 :
683 : /* Set DST to be a copy of another SRC. The two functions will share their
684 : rdesc. */
685 :
686 : static void
687 893060 : ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
688 : struct ipa_jump_func *src)
689 :
690 : {
691 893060 : gcc_checking_assert (src->type == IPA_JF_CONST);
692 893060 : dst->type = IPA_JF_CONST;
693 893060 : dst->value.constant = src->value.constant;
694 893060 : }
695 :
696 : /* Set DST to be a copy of another jump function SRC but possibly adjust it to
697 : a new passed type PARM_TYPE. If the adjustment fails, the jump function can
698 : end up being set to the unknown type. If the conversion is not necessary or
699 : it succeeds and if the destination rdesc has not been already used, the two
700 : functions will share their rdesc. */
701 :
702 : static void
703 160755 : ipa_convert_prop_cst_jf (struct ipa_jump_func *dst,
704 : struct ipa_jump_func *src,
705 : tree parm_type)
706 :
707 : {
708 160755 : gcc_checking_assert (src->type == IPA_JF_CONST);
709 160755 : tree new_val = ipacp_value_safe_for_type (parm_type,
710 : ipa_get_jf_constant (src));
711 160755 : if (new_val)
712 : {
713 160755 : bool rd = ipa_get_jf_pass_through_refdesc_decremented (dst);
714 :
715 160755 : dst->type = IPA_JF_CONST;
716 160755 : dst->value.constant.value = new_val;
717 160755 : if (!rd)
718 160730 : dst->value.constant.rdesc = src->value.constant.rdesc;
719 : else
720 25 : ipa_zap_jf_refdesc (dst);
721 : }
722 : else
723 0 : ipa_set_jf_unknown (dst);
724 160755 : }
725 :
726 : /* Set JFUNC to be a constant jmp function. */
727 :
728 : static void
729 2760580 : ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
730 : struct cgraph_edge *cs)
731 : {
732 2760580 : jfunc->type = IPA_JF_CONST;
733 2760580 : jfunc->value.constant.value = unshare_expr_without_location (constant);
734 :
735 2760580 : if (TREE_CODE (constant) == ADDR_EXPR
736 2760580 : && (TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL
737 986976 : || (VAR_P (TREE_OPERAND (constant, 0))
738 349751 : && TREE_STATIC (TREE_OPERAND (constant, 0)))))
739 : {
740 387498 : struct ipa_cst_ref_desc *rdesc;
741 :
742 387498 : rdesc = ipa_refdesc_pool.allocate ();
743 387498 : rdesc->cs = cs;
744 387498 : rdesc->next_duplicate = NULL;
745 387498 : rdesc->refcount = 1;
746 387498 : jfunc->value.constant.rdesc = rdesc;
747 : }
748 : else
749 2373082 : jfunc->value.constant.rdesc = NULL;
750 2760580 : }
751 :
752 : /* Set JFUNC to be a simple pass-through jump function. */
753 : static void
754 1385113 : ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
755 : bool agg_preserved)
756 : {
757 1385113 : jfunc->type = IPA_JF_PASS_THROUGH;
758 1385113 : jfunc->value.pass_through.operand = NULL_TREE;
759 1385113 : jfunc->value.pass_through.op_type = NULL_TREE;
760 1385113 : jfunc->value.pass_through.formal_id = formal_id;
761 1385113 : jfunc->value.pass_through.operation = NOP_EXPR;
762 1385113 : jfunc->value.pass_through.agg_preserved = agg_preserved;
763 1385113 : jfunc->value.pass_through.refdesc_decremented = false;
764 1251012 : }
765 :
766 : /* Set JFUNC to be an unary pass through jump function. */
767 :
768 : static void
769 1021 : ipa_set_jf_unary_pass_through (struct ipa_jump_func *jfunc, int formal_id,
770 : enum tree_code operation, tree op_type)
771 : {
772 1021 : jfunc->type = IPA_JF_PASS_THROUGH;
773 1021 : jfunc->value.pass_through.operand = NULL_TREE;
774 1021 : jfunc->value.pass_through.op_type = op_type;
775 1021 : jfunc->value.pass_through.formal_id = formal_id;
776 1021 : jfunc->value.pass_through.operation = operation;
777 1021 : jfunc->value.pass_through.agg_preserved = false;
778 1021 : jfunc->value.pass_through.refdesc_decremented = false;
779 1021 : }
780 : /* Set JFUNC to be an arithmetic pass through jump function. */
781 :
782 : static void
783 45782 : ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
784 : tree operand, enum tree_code operation,
785 : tree op_type)
786 : {
787 45782 : jfunc->type = IPA_JF_PASS_THROUGH;
788 0 : jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
789 45782 : jfunc->value.pass_through.op_type = op_type;
790 45782 : jfunc->value.pass_through.formal_id = formal_id;
791 45782 : jfunc->value.pass_through.operation = operation;
792 45782 : jfunc->value.pass_through.agg_preserved = false;
793 45782 : jfunc->value.pass_through.refdesc_decremented = false;
794 45782 : }
795 :
796 : /* Set JFUNC to be an ancestor jump function. */
797 :
798 : static void
799 188929 : ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
800 : int formal_id, bool agg_preserved, bool keep_null)
801 : {
802 188929 : jfunc->type = IPA_JF_ANCESTOR;
803 188929 : jfunc->value.ancestor.formal_id = formal_id;
804 188929 : jfunc->value.ancestor.offset = offset;
805 188929 : jfunc->value.ancestor.agg_preserved = agg_preserved;
806 188929 : jfunc->value.ancestor.keep_null = keep_null;
807 188241 : }
808 :
809 : /* Get IPA BB information about the given BB. FBI is the context of analyzis
810 : of this function body. */
811 :
812 : static struct ipa_bb_info *
813 33293718 : ipa_get_bb_info (struct ipa_func_body_info *fbi, basic_block bb)
814 : {
815 27744664 : gcc_checking_assert (fbi);
816 33293718 : return &fbi->bb_infos[bb->index];
817 : }
818 :
819 : /* Structure to be passed in between detect_type_change and
820 : check_stmt_for_type_change. */
821 :
822 : struct prop_type_change_info
823 : {
824 : /* Offset into the object where there is the virtual method pointer we are
825 : looking for. */
826 : HOST_WIDE_INT offset;
827 : /* The declaration or SSA_NAME pointer of the base that we are checking for
828 : type change. */
829 : tree object;
830 : /* Set to true if dynamic type change has been detected. */
831 : bool type_maybe_changed;
832 : };
833 :
834 : /* Return true if STMT can modify a virtual method table pointer.
835 :
836 : This function makes special assumptions about both constructors and
837 : destructors which are all the functions that are allowed to alter the VMT
838 : pointers. It assumes that destructors begin with assignment into all VMT
839 : pointers and that constructors essentially look in the following way:
840 :
841 : 1) The very first thing they do is that they call constructors of ancestor
842 : sub-objects that have them.
843 :
844 : 2) Then VMT pointers of this and all its ancestors is set to new values
845 : corresponding to the type corresponding to the constructor.
846 :
847 : 3) Only afterwards, other stuff such as constructor of member sub-objects
848 : and the code written by the user is run. Only this may include calling
849 : virtual functions, directly or indirectly.
850 :
851 : There is no way to call a constructor of an ancestor sub-object in any
852 : other way.
853 :
854 : This means that we do not have to care whether constructors get the correct
855 : type information because they will always change it (in fact, if we define
856 : the type to be given by the VMT pointer, it is undefined).
857 :
858 : The most important fact to derive from the above is that if, for some
859 : statement in the section 3, we try to detect whether the dynamic type has
860 : changed, we can safely ignore all calls as we examine the function body
861 : backwards until we reach statements in section 2 because these calls cannot
862 : be ancestor constructors or destructors (if the input is not bogus) and so
863 : do not change the dynamic type (this holds true only for automatically
864 : allocated objects but at the moment we devirtualize only these). We then
865 : must detect that statements in section 2 change the dynamic type and can try
866 : to derive the new type. That is enough and we can stop, we will never see
867 : the calls into constructors of sub-objects in this code. Therefore we can
868 : safely ignore all call statements that we traverse.
869 : */
870 :
871 : static bool
872 9 : stmt_may_be_vtbl_ptr_store (gimple *stmt)
873 : {
874 9 : if (is_gimple_call (stmt))
875 : return false;
876 0 : if (gimple_clobber_p (stmt))
877 : return false;
878 0 : else if (is_gimple_assign (stmt))
879 : {
880 0 : tree lhs = gimple_assign_lhs (stmt);
881 :
882 0 : if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
883 : {
884 0 : if (flag_strict_aliasing
885 0 : && !POINTER_TYPE_P (TREE_TYPE (lhs)))
886 : return false;
887 :
888 0 : if (TREE_CODE (lhs) == COMPONENT_REF
889 0 : && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
890 0 : return false;
891 : /* In the future we might want to use get_ref_base_and_extent to find
892 : if there is a field corresponding to the offset and if so, proceed
893 : almost like if it was a component ref. */
894 : }
895 : }
896 : return true;
897 : }
898 :
899 : /* Callback of walk_aliased_vdefs and a helper function for detect_type_change
900 : to check whether a particular statement may modify the virtual table
901 : pointerIt stores its result into DATA, which points to a
902 : prop_type_change_info structure. */
903 :
904 : static bool
905 9 : check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
906 : {
907 9 : gimple *stmt = SSA_NAME_DEF_STMT (vdef);
908 9 : struct prop_type_change_info *tci = (struct prop_type_change_info *) data;
909 :
910 9 : if (stmt_may_be_vtbl_ptr_store (stmt))
911 : {
912 0 : tci->type_maybe_changed = true;
913 0 : return true;
914 : }
915 : else
916 : return false;
917 : }
918 :
919 : /* See if ARG is PARAM_DECl describing instance passed by pointer
920 : or reference in FUNCTION. Return false if the dynamic type may change
921 : in between beggining of the function until CALL is invoked.
922 :
923 : Generally functions are not allowed to change type of such instances,
924 : but they call destructors. We assume that methods cannot destroy the THIS
925 : pointer. Also as a special cases, constructor and destructors may change
926 : type of the THIS pointer. */
927 :
928 : static bool
929 9472 : param_type_may_change_p (tree function, tree arg, gimple *call)
930 : {
931 : /* Pure functions cannot do any changes on the dynamic type;
932 : that require writing to memory. */
933 9472 : if (flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
934 : return false;
935 : /* We need to check if we are within inlined consturctor
936 : or destructor (ideally we would have way to check that the
937 : inline cdtor is actually working on ARG, but we don't have
938 : easy tie on this, so punt on all non-pure cdtors.
939 : We may also record the types of cdtors and once we know type
940 : of the instance match them.
941 :
942 : Also code unification optimizations may merge calls from
943 : different blocks making return values unreliable. So
944 : do nothing during late optimization. */
945 9472 : if (DECL_STRUCT_FUNCTION (function)->after_inlining)
946 : return true;
947 9472 : if (TREE_CODE (arg) == SSA_NAME
948 9472 : && SSA_NAME_IS_DEFAULT_DEF (arg)
949 18944 : && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
950 : {
951 : /* Normal (non-THIS) argument. */
952 9472 : if ((SSA_NAME_VAR (arg) != DECL_ARGUMENTS (function)
953 8803 : || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE)
954 : /* THIS pointer of an method - here we want to watch constructors
955 : and destructors as those definitely may change the dynamic
956 : type. */
957 17684 : || (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
958 8212 : && !DECL_CXX_CONSTRUCTOR_P (function)
959 8211 : && !DECL_CXX_DESTRUCTOR_P (function)
960 8210 : && (SSA_NAME_VAR (arg) == DECL_ARGUMENTS (function))))
961 : {
962 : /* Walk the inline stack and watch out for ctors/dtors. */
963 40636 : for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
964 15583 : block = BLOCK_SUPERCONTEXT (block))
965 15598 : if (inlined_polymorphic_ctor_dtor_block_p (block, false))
966 : return true;
967 : return false;
968 : }
969 : }
970 : return true;
971 : }
972 :
973 : /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
974 : callsite CALL) by looking for assignments to its virtual table pointer. If
975 : it is, return true. ARG is the object itself (not a pointer
976 : to it, unless dereferenced). BASE is the base of the memory access as
977 : returned by get_ref_base_and_extent, as is the offset.
978 :
979 : This is helper function for detect_type_change and detect_type_change_ssa
980 : that does the heavy work which is usually unnecesary. */
981 :
982 : static bool
983 17 : detect_type_change_from_memory_writes (ipa_func_body_info *fbi, tree arg,
984 : tree base, tree comp_type, gcall *call,
985 : HOST_WIDE_INT offset)
986 : {
987 17 : struct prop_type_change_info tci;
988 17 : ao_ref ao;
989 :
990 17 : gcc_checking_assert (DECL_P (arg)
991 : || TREE_CODE (arg) == MEM_REF
992 : || handled_component_p (arg));
993 :
994 17 : comp_type = TYPE_MAIN_VARIANT (comp_type);
995 :
996 : /* Const calls cannot call virtual methods through VMT and so type changes do
997 : not matter. */
998 17 : if (!flag_devirtualize || !gimple_vuse (call)
999 : /* Be sure expected_type is polymorphic. */
1000 17 : || !comp_type
1001 17 : || TREE_CODE (comp_type) != RECORD_TYPE
1002 17 : || !TYPE_BINFO (TYPE_MAIN_VARIANT (comp_type))
1003 34 : || !BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (comp_type))))
1004 : return true;
1005 :
1006 17 : if (fbi->aa_walk_budget == 0)
1007 : return false;
1008 :
1009 17 : ao_ref_init (&ao, arg);
1010 17 : ao.base = base;
1011 17 : ao.offset = offset;
1012 17 : ao.size = POINTER_SIZE;
1013 17 : ao.max_size = ao.size;
1014 :
1015 17 : tci.offset = offset;
1016 17 : tci.object = get_base_address (arg);
1017 17 : tci.type_maybe_changed = false;
1018 :
1019 17 : int walked
1020 34 : = walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
1021 : &tci, NULL, NULL, fbi->aa_walk_budget);
1022 17 : if (walked >= 0)
1023 17 : fbi->aa_walk_budget -= walked;
1024 : else
1025 0 : fbi->aa_walk_budget = 0;
1026 :
1027 17 : if (walked >= 0 && !tci.type_maybe_changed)
1028 : return false;
1029 :
1030 : return true;
1031 : }
1032 :
1033 : /* Detect whether the dynamic type of ARG of COMP_TYPE may have changed.
1034 : If it is, return true. ARG is the object itself (not a pointer
1035 : to it, unless dereferenced). BASE is the base of the memory access as
1036 : returned by get_ref_base_and_extent, as is the offset. */
1037 :
1038 : static bool
1039 734 : detect_type_change (ipa_func_body_info *fbi, tree arg, tree base,
1040 : tree comp_type, gcall *call,
1041 : HOST_WIDE_INT offset)
1042 : {
1043 734 : if (!flag_devirtualize)
1044 : return false;
1045 :
1046 734 : if (TREE_CODE (base) == MEM_REF
1047 1468 : && !param_type_may_change_p (current_function_decl,
1048 734 : TREE_OPERAND (base, 0),
1049 : call))
1050 : return false;
1051 12 : return detect_type_change_from_memory_writes (fbi, arg, base, comp_type,
1052 12 : call, offset);
1053 : }
1054 :
1055 : /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
1056 : SSA name (its dereference will become the base and the offset is assumed to
1057 : be zero). */
1058 :
1059 : static bool
1060 8738 : detect_type_change_ssa (ipa_func_body_info *fbi, tree arg, tree comp_type,
1061 : gcall *call)
1062 : {
1063 8738 : gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
1064 8738 : if (!flag_devirtualize
1065 8738 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
1066 : return false;
1067 :
1068 8738 : if (!param_type_may_change_p (current_function_decl, arg, call))
1069 : return false;
1070 :
1071 5 : arg = build2 (MEM_REF, ptr_type_node, arg,
1072 : build_int_cst (ptr_type_node, 0));
1073 :
1074 5 : return detect_type_change_from_memory_writes (fbi, arg, arg, comp_type,
1075 5 : call, 0);
1076 : }
1077 :
1078 : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1079 : boolean variable pointed to by DATA. */
1080 :
1081 : static bool
1082 1550304 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1083 : void *data)
1084 : {
1085 1550304 : bool *b = (bool *) data;
1086 1550304 : *b = true;
1087 1550304 : return true;
1088 : }
1089 :
1090 : /* Find the nearest valid aa status for parameter specified by INDEX that
1091 : dominates BB. */
1092 :
1093 : static struct ipa_param_aa_status *
1094 4351110 : find_dominating_aa_status (struct ipa_func_body_info *fbi, basic_block bb,
1095 : int index)
1096 : {
1097 13123825 : while (true)
1098 : {
1099 13123825 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1100 13123825 : if (!bb)
1101 : return NULL;
1102 10188123 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1103 12245840 : if (!bi->param_aa_statuses.is_empty ()
1104 2057717 : && bi->param_aa_statuses[index].valid)
1105 1415408 : return &bi->param_aa_statuses[index];
1106 : }
1107 : }
1108 :
1109 : /* Get AA status structure for the given BB and parameter with INDEX. Allocate
1110 : structures and/or intialize the result with a dominating description as
1111 : necessary. */
1112 :
1113 : static struct ipa_param_aa_status *
1114 6529400 : parm_bb_aa_status_for_bb (struct ipa_func_body_info *fbi, basic_block bb,
1115 : int index)
1116 : {
1117 6529400 : gcc_checking_assert (fbi);
1118 6529400 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1119 6529400 : if (bi->param_aa_statuses.is_empty ())
1120 3743082 : bi->param_aa_statuses.safe_grow_cleared (fbi->param_count, true);
1121 6529400 : struct ipa_param_aa_status *paa = &bi->param_aa_statuses[index];
1122 6529400 : if (!paa->valid)
1123 : {
1124 4351110 : gcc_checking_assert (!paa->parm_modified
1125 : && !paa->ref_modified
1126 : && !paa->pt_modified);
1127 4351110 : struct ipa_param_aa_status *dom_paa;
1128 4351110 : dom_paa = find_dominating_aa_status (fbi, bb, index);
1129 4351110 : if (dom_paa)
1130 1415408 : *paa = *dom_paa;
1131 : else
1132 2935702 : paa->valid = true;
1133 : }
1134 :
1135 6529400 : return paa;
1136 : }
1137 :
1138 : /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
1139 : a value known not to be modified in this function before reaching the
1140 : statement STMT. FBI holds information about the function we have so far
1141 : gathered but do not survive the summary building stage. */
1142 :
1143 : static bool
1144 1140444 : parm_preserved_before_stmt_p (struct ipa_func_body_info *fbi, int index,
1145 : gimple *stmt, tree parm_load)
1146 : {
1147 1140444 : struct ipa_param_aa_status *paa;
1148 1140444 : bool modified = false;
1149 1140444 : ao_ref refd;
1150 :
1151 1140444 : tree base = get_base_address (parm_load);
1152 1140444 : gcc_assert (TREE_CODE (base) == PARM_DECL);
1153 1140444 : if (TREE_READONLY (base))
1154 : return true;
1155 :
1156 1059290 : gcc_checking_assert (fbi);
1157 1059290 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1158 1059290 : if (paa->parm_modified || fbi->aa_walk_budget == 0)
1159 : return false;
1160 :
1161 1819304 : gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
1162 909652 : ao_ref_init (&refd, parm_load);
1163 1819304 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1164 : &modified, NULL, NULL,
1165 : fbi->aa_walk_budget);
1166 909652 : if (walked < 0)
1167 : {
1168 8 : modified = true;
1169 8 : fbi->aa_walk_budget = 0;
1170 : }
1171 : else
1172 909644 : fbi->aa_walk_budget -= walked;
1173 909652 : if (paa && modified)
1174 92700 : paa->parm_modified = true;
1175 909652 : return !modified;
1176 : }
1177 :
1178 : /* If STMT is an assignment that loads a value from an parameter declaration,
1179 : return the index of the parameter in ipa_node_params which has not been
1180 : modified. Otherwise return -1. */
1181 :
1182 : static int
1183 6493934 : load_from_unmodified_param (struct ipa_func_body_info *fbi,
1184 : vec<ipa_param_descriptor, va_gc> *descriptors,
1185 : gimple *stmt)
1186 : {
1187 6493934 : int index;
1188 6493934 : tree op1;
1189 :
1190 6493934 : if (!gimple_assign_single_p (stmt))
1191 : return -1;
1192 :
1193 4332205 : op1 = gimple_assign_rhs1 (stmt);
1194 4332205 : if (TREE_CODE (op1) != PARM_DECL)
1195 : return -1;
1196 :
1197 68991 : index = ipa_get_param_decl_index_1 (descriptors, op1);
1198 68991 : if (index < 0
1199 68991 : || !parm_preserved_before_stmt_p (fbi, index, stmt, op1))
1200 23801 : return -1;
1201 :
1202 : return index;
1203 : }
1204 :
1205 : /* Return true if memory reference REF (which must be a load through parameter
1206 : with INDEX) loads data that are known to be unmodified in this function
1207 : before reaching statement STMT. */
1208 :
1209 : static bool
1210 4646042 : parm_ref_data_preserved_p (struct ipa_func_body_info *fbi,
1211 : int index, gimple *stmt, tree ref)
1212 : {
1213 4646042 : struct ipa_param_aa_status *paa;
1214 4646042 : bool modified = false;
1215 4646042 : ao_ref refd;
1216 :
1217 4646042 : gcc_checking_assert (fbi);
1218 4646042 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1219 4646042 : if (paa->ref_modified || fbi->aa_walk_budget == 0)
1220 : return false;
1221 :
1222 7269894 : gcc_checking_assert (gimple_vuse (stmt));
1223 3634947 : ao_ref_init (&refd, ref);
1224 7269894 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1225 : &modified, NULL, NULL,
1226 : fbi->aa_walk_budget);
1227 3634947 : if (walked < 0)
1228 : {
1229 8 : modified = true;
1230 8 : fbi->aa_walk_budget = 0;
1231 : }
1232 : else
1233 3634939 : fbi->aa_walk_budget -= walked;
1234 3634947 : if (modified)
1235 622317 : paa->ref_modified = true;
1236 3634947 : return !modified;
1237 : }
1238 :
1239 : /* Return true if the data pointed to by PARM (which is a parameter with INDEX)
1240 : is known to be unmodified in this function before reaching call statement
1241 : CALL into which it is passed. FBI describes the function body. */
1242 :
1243 : static bool
1244 1164194 : parm_ref_data_pass_through_p (struct ipa_func_body_info *fbi, int index,
1245 : gimple *call, tree parm)
1246 : {
1247 1164194 : bool modified = false;
1248 1164194 : ao_ref refd;
1249 :
1250 : /* It's unnecessary to calculate anything about memory contnets for a const
1251 : function because it is not goin to use it. But do not cache the result
1252 : either. Also, no such calculations for non-pointers. */
1253 1618982 : if (!gimple_vuse (call)
1254 1164194 : || !POINTER_TYPE_P (TREE_TYPE (parm)))
1255 : return false;
1256 :
1257 824068 : struct ipa_param_aa_status *paa = parm_bb_aa_status_for_bb (fbi,
1258 : gimple_bb (call),
1259 : index);
1260 824068 : if (paa->pt_modified || fbi->aa_walk_budget == 0)
1261 : return false;
1262 :
1263 709406 : ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
1264 1418812 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified,
1265 : &modified, NULL, NULL,
1266 : fbi->aa_walk_budget);
1267 709406 : if (walked < 0)
1268 : {
1269 0 : fbi->aa_walk_budget = 0;
1270 0 : modified = true;
1271 : }
1272 : else
1273 709406 : fbi->aa_walk_budget -= walked;
1274 709406 : if (modified)
1275 259668 : paa->pt_modified = true;
1276 709406 : return !modified;
1277 : }
1278 :
1279 : /* Return true if we can prove that OP is a memory reference loading
1280 : data from an aggregate passed as a parameter.
1281 :
1282 : The function works in two modes. If GUARANTEED_UNMODIFIED is NULL, it return
1283 : false if it cannot prove that the value has not been modified before the
1284 : load in STMT. If GUARANTEED_UNMODIFIED is not NULL, it will return true even
1285 : if it cannot prove the value has not been modified, in that case it will
1286 : store false to *GUARANTEED_UNMODIFIED, otherwise it will store true there.
1287 :
1288 : INFO and PARMS_AINFO describe parameters of the current function (but the
1289 : latter can be NULL), STMT is the load statement. If function returns true,
1290 : *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
1291 : within the aggregate and whether it is a load from a value passed by
1292 : reference respectively.
1293 :
1294 : Return false if the offset divided by BITS_PER_UNIT would not fit into an
1295 : unsigned int. */
1296 :
1297 : bool
1298 25241973 : ipa_load_from_parm_agg (struct ipa_func_body_info *fbi,
1299 : vec<ipa_param_descriptor, va_gc> *descriptors,
1300 : gimple *stmt, tree op, int *index_p,
1301 : HOST_WIDE_INT *offset_p, poly_int64 *size_p,
1302 : bool *by_ref_p, bool *guaranteed_unmodified)
1303 : {
1304 25241973 : int index;
1305 25241973 : HOST_WIDE_INT size;
1306 25241973 : bool reverse;
1307 25241973 : tree base = get_ref_base_and_extent_hwi (op, offset_p, &size, &reverse);
1308 :
1309 25241973 : if (!base
1310 23862720 : || (*offset_p / BITS_PER_UNIT) > UINT_MAX)
1311 : return false;
1312 :
1313 : /* We can not propagate across volatile loads. */
1314 23862689 : if (TREE_THIS_VOLATILE (op))
1315 : return false;
1316 :
1317 22651895 : if (DECL_P (base))
1318 : {
1319 11145576 : int index = ipa_get_param_decl_index_1 (descriptors, base);
1320 11145576 : if (index >= 0
1321 11145576 : && parm_preserved_before_stmt_p (fbi, index, stmt, op))
1322 : {
1323 788115 : *index_p = index;
1324 788115 : *by_ref_p = false;
1325 788115 : if (size_p)
1326 24024 : *size_p = size;
1327 788115 : if (guaranteed_unmodified)
1328 135 : *guaranteed_unmodified = true;
1329 788115 : return true;
1330 : }
1331 10357461 : return false;
1332 : }
1333 :
1334 11506319 : if (TREE_CODE (base) != MEM_REF
1335 10724450 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1336 22227179 : || !integer_zerop (TREE_OPERAND (base, 1)))
1337 1742452 : return false;
1338 :
1339 9763867 : if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1340 : {
1341 4760747 : tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1342 4760747 : index = ipa_get_param_decl_index_1 (descriptors, parm);
1343 : }
1344 : else
1345 : {
1346 : /* This branch catches situations where a pointer parameter is not a
1347 : gimple register, for example:
1348 :
1349 : void hip7(S*) (struct S * p)
1350 : {
1351 : void (*<T2e4>) (struct S *) D.1867;
1352 : struct S * p.1;
1353 :
1354 : <bb 2>:
1355 : p.1_1 = p;
1356 : D.1867_2 = p.1_1->f;
1357 : D.1867_2 ();
1358 : gdp = &p;
1359 : */
1360 :
1361 5003120 : gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
1362 5003120 : index = load_from_unmodified_param (fbi, descriptors, def);
1363 : }
1364 :
1365 9763867 : if (index >= 0)
1366 : {
1367 4645743 : bool data_preserved = parm_ref_data_preserved_p (fbi, index, stmt, op);
1368 4645743 : if (!data_preserved && !guaranteed_unmodified)
1369 : return false;
1370 :
1371 3012845 : *index_p = index;
1372 3012845 : *by_ref_p = true;
1373 3012845 : if (size_p)
1374 27923 : *size_p = size;
1375 3012845 : if (guaranteed_unmodified)
1376 2051 : *guaranteed_unmodified = data_preserved;
1377 3012845 : return true;
1378 : }
1379 : return false;
1380 : }
1381 :
1382 : /* If STMT is an assignment that loads a value from a parameter declaration,
1383 : or from an aggregate passed as the parameter either by value or reference,
1384 : return the index of the parameter in ipa_node_params. Otherwise return -1.
1385 :
1386 : FBI holds gathered information about the function. INFO describes
1387 : parameters of the function, STMT is the assignment statement. If it is a
1388 : memory load from an aggregate, *OFFSET_P is filled with offset within the
1389 : aggregate, and *BY_REF_P specifies whether the aggregate is passed by
1390 : reference. */
1391 :
1392 : static int
1393 352866 : load_from_unmodified_param_or_agg (struct ipa_func_body_info *fbi,
1394 : class ipa_node_params *info,
1395 : gimple *stmt,
1396 : HOST_WIDE_INT *offset_p,
1397 : bool *by_ref_p)
1398 : {
1399 352866 : int index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1400 352866 : poly_int64 size;
1401 :
1402 : /* Load value from a parameter declaration. */
1403 352866 : if (index >= 0)
1404 : {
1405 244 : *offset_p = -1;
1406 244 : return index;
1407 : }
1408 :
1409 352622 : if (!gimple_assign_load_p (stmt))
1410 : return -1;
1411 :
1412 182448 : tree rhs = gimple_assign_rhs1 (stmt);
1413 :
1414 : /* Skip memory reference containing VIEW_CONVERT_EXPR. */
1415 332373 : for (tree t = rhs; handled_component_p (t); t = TREE_OPERAND (t, 0))
1416 149982 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1417 : return -1;
1418 :
1419 : /* Skip memory reference containing bit-field. */
1420 182391 : if (TREE_CODE (rhs) == BIT_FIELD_REF
1421 182391 : || contains_bitfld_component_ref_p (rhs))
1422 0 : return -1;
1423 :
1424 182391 : if (!ipa_load_from_parm_agg (fbi, info->descriptors, stmt, rhs, &index,
1425 : offset_p, &size, by_ref_p))
1426 : return -1;
1427 :
1428 48971 : gcc_assert (!maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (rhs))),
1429 : size));
1430 48971 : if (!*by_ref_p)
1431 : {
1432 23346 : tree param_type = ipa_get_type (info, index);
1433 :
1434 23346 : if (!param_type || !AGGREGATE_TYPE_P (param_type))
1435 : return -1;
1436 : }
1437 25625 : else if (TREE_THIS_VOLATILE (rhs))
1438 : return -1;
1439 :
1440 47377 : return index;
1441 : }
1442 :
1443 : /* Walk pointer adjustemnts from OP (such as POINTER_PLUS and ADDR_EXPR)
1444 : to find original pointer. Initialize RET to the pointer which results from
1445 : the walk.
1446 : If offset is known return true and initialize OFFSET_RET. */
1447 :
1448 : bool
1449 15661275 : unadjusted_ptr_and_unit_offset (tree op, tree *ret, poly_int64 *offset_ret)
1450 : {
1451 15661275 : poly_int64 offset = 0;
1452 15661275 : bool offset_known = true;
1453 15661275 : int i;
1454 :
1455 20267555 : for (i = 0; i < param_ipa_jump_function_lookups; i++)
1456 : {
1457 20266537 : if (TREE_CODE (op) == ADDR_EXPR)
1458 : {
1459 1571383 : poly_int64 extra_offset;
1460 1571383 : tree base = get_addr_base_and_unit_offset (TREE_OPERAND (op, 0),
1461 : &extra_offset);
1462 1571383 : if (!base)
1463 : {
1464 27429 : base = get_base_address (TREE_OPERAND (op, 0));
1465 27429 : if (TREE_CODE (base) != MEM_REF)
1466 : break;
1467 : offset_known = false;
1468 : }
1469 : else
1470 : {
1471 1543954 : if (TREE_CODE (base) != MEM_REF)
1472 : break;
1473 263716 : offset += extra_offset;
1474 : }
1475 263716 : op = TREE_OPERAND (base, 0);
1476 263716 : if (mem_ref_offset (base).to_shwi (&extra_offset))
1477 263716 : offset += extra_offset;
1478 : else
1479 : offset_known = false;
1480 : }
1481 18695154 : else if (TREE_CODE (op) == SSA_NAME
1482 18695154 : && !SSA_NAME_IS_DEFAULT_DEF (op))
1483 : {
1484 6436290 : gimple *pstmt = SSA_NAME_DEF_STMT (op);
1485 :
1486 6436290 : if (gimple_assign_single_p (pstmt))
1487 3138830 : op = gimple_assign_rhs1 (pstmt);
1488 3297460 : else if (is_gimple_assign (pstmt)
1489 3297460 : && gimple_assign_rhs_code (pstmt) == POINTER_PLUS_EXPR)
1490 : {
1491 1203734 : poly_int64 extra_offset = 0;
1492 1203734 : if (ptrdiff_tree_p (gimple_assign_rhs2 (pstmt),
1493 : &extra_offset))
1494 1203734 : offset += extra_offset;
1495 : else
1496 : offset_known = false;
1497 1203734 : op = gimple_assign_rhs1 (pstmt);
1498 : }
1499 : else
1500 : break;
1501 : }
1502 : else
1503 : break;
1504 : }
1505 15661275 : *ret = op;
1506 15661275 : *offset_ret = offset;
1507 15661275 : return offset_known;
1508 : }
1509 :
1510 : /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
1511 : of an assignment statement STMT, try to determine whether we are actually
1512 : handling any of the following cases and construct an appropriate jump
1513 : function into JFUNC if so:
1514 :
1515 : 1) The passed value is loaded from a formal parameter which is not a gimple
1516 : register (most probably because it is addressable, the value has to be
1517 : scalar) and we can guarantee the value has not changed. This case can
1518 : therefore be described by a simple pass-through jump function. For example:
1519 :
1520 : foo (int a)
1521 : {
1522 : int a.0;
1523 :
1524 : a.0_2 = a;
1525 : bar (a.0_2);
1526 :
1527 : 2) The passed value can be described by a simple arithmetic pass-through
1528 : jump function. E.g.
1529 :
1530 : foo (int a)
1531 : {
1532 : int D.2064;
1533 :
1534 : D.2064_4 = a.1(D) + 4;
1535 : bar (D.2064_4);
1536 :
1537 : This case can also occur in combination of the previous one, e.g.:
1538 :
1539 : foo (int a, int z)
1540 : {
1541 : int a.0;
1542 : int D.2064;
1543 :
1544 : a.0_3 = a;
1545 : D.2064_4 = a.0_3 + 4;
1546 : foo (D.2064_4);
1547 :
1548 : 3) The passed value is an address of an object within another one (which
1549 : also passed by reference). Such situations are described by an ancestor
1550 : jump function and describe situations such as:
1551 :
1552 : B::foo() (struct B * const this)
1553 : {
1554 : struct A * D.1845;
1555 :
1556 : D.1845_2 = &this_1(D)->D.1748;
1557 : A::bar (D.1845_2);
1558 :
1559 : INFO is the structure describing individual parameters access different
1560 : stages of IPA optimizations. PARMS_AINFO contains the information that is
1561 : only needed for intraprocedural analysis. */
1562 :
1563 : static void
1564 1254748 : compute_complex_assign_jump_func (struct ipa_func_body_info *fbi,
1565 : class ipa_node_params *info,
1566 : struct ipa_jump_func *jfunc,
1567 : gcall *call, gimple *stmt, tree name,
1568 : tree param_type)
1569 : {
1570 1254748 : HOST_WIDE_INT offset, size;
1571 1254748 : tree op1, tc_ssa, base, ssa;
1572 1254748 : bool reverse;
1573 1254748 : int index;
1574 :
1575 1254748 : op1 = gimple_assign_rhs1 (stmt);
1576 :
1577 1254748 : if (TREE_CODE (op1) == SSA_NAME)
1578 : {
1579 398259 : if (SSA_NAME_IS_DEFAULT_DEF (op1))
1580 116800 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1581 : else
1582 281459 : index = load_from_unmodified_param (fbi, info->descriptors,
1583 281459 : SSA_NAME_DEF_STMT (op1));
1584 : tc_ssa = op1;
1585 : }
1586 : else
1587 : {
1588 856489 : index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1589 856489 : tc_ssa = gimple_assign_lhs (stmt);
1590 : }
1591 :
1592 1254748 : if (index >= 0)
1593 : {
1594 118092 : if (lto_variably_modified_type_p (TREE_TYPE (name)))
1595 1087809 : return;
1596 :
1597 118046 : switch (gimple_assign_rhs_class (stmt))
1598 : {
1599 69920 : case GIMPLE_BINARY_RHS:
1600 69920 : {
1601 69920 : tree op2 = gimple_assign_rhs2 (stmt);
1602 69920 : if (!is_gimple_ip_invariant (op2)
1603 69920 : || ((TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1604 : != tcc_comparison)
1605 33082 : && !useless_type_conversion_p (TREE_TYPE (name),
1606 33082 : TREE_TYPE (op1))))
1607 26035 : return;
1608 :
1609 43885 : ipa_set_jf_arith_pass_through (jfunc, index, op2,
1610 : gimple_assign_rhs_code (stmt),
1611 43885 : TREE_TYPE (name));
1612 43885 : break;
1613 : }
1614 1118 : case GIMPLE_SINGLE_RHS:
1615 1118 : {
1616 1118 : bool agg_p = parm_ref_data_pass_through_p (fbi, index, call,
1617 : tc_ssa);
1618 1118 : ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
1619 1118 : break;
1620 : }
1621 47006 : case GIMPLE_UNARY_RHS:
1622 47006 : if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
1623 994 : ipa_set_jf_unary_pass_through (jfunc, index,
1624 : gimple_assign_rhs_code (stmt),
1625 994 : TREE_TYPE (name));
1626 92011 : default:;
1627 : }
1628 92011 : return;
1629 : }
1630 :
1631 1136656 : if (TREE_CODE (op1) != ADDR_EXPR)
1632 : return;
1633 240862 : op1 = TREE_OPERAND (op1, 0);
1634 240862 : base = get_ref_base_and_extent_hwi (op1, &offset, &size, &reverse);
1635 240862 : offset_int mem_offset;
1636 240862 : if (!base
1637 213779 : || TREE_CODE (base) != MEM_REF
1638 443742 : || !mem_ref_offset (base).is_constant (&mem_offset))
1639 37982 : return;
1640 202880 : offset += mem_offset.to_short_addr () * BITS_PER_UNIT;
1641 202880 : ssa = TREE_OPERAND (base, 0);
1642 202880 : if (TREE_CODE (ssa) != SSA_NAME
1643 202880 : || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1644 369918 : || offset < 0)
1645 : return;
1646 :
1647 : /* Dynamic types are changed in constructors and destructors. */
1648 166939 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1649 166939 : if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1650 164000 : ipa_set_ancestor_jf (jfunc, offset, index,
1651 164000 : parm_ref_data_pass_through_p (fbi, index, call, ssa),
1652 : false);
1653 : }
1654 :
1655 : /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1656 : it looks like:
1657 :
1658 : iftmp.1_3 = &obj_2(D)->D.1762;
1659 :
1660 : The base of the MEM_REF must be a default definition SSA NAME of a
1661 : parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1662 : whole MEM_REF expression is returned and the offset calculated from any
1663 : handled components and the MEM_REF itself is stored into *OFFSET. The whole
1664 : RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1665 :
1666 : static tree
1667 15310 : get_ancestor_addr_info (gimple *assign, tree *obj_p, HOST_WIDE_INT *offset)
1668 : {
1669 15310 : HOST_WIDE_INT size;
1670 15310 : tree expr, parm, obj;
1671 15310 : bool reverse;
1672 :
1673 15310 : if (!gimple_assign_single_p (assign))
1674 : return NULL_TREE;
1675 8529 : expr = gimple_assign_rhs1 (assign);
1676 :
1677 8529 : if (TREE_CODE (expr) != ADDR_EXPR)
1678 : return NULL_TREE;
1679 4392 : expr = TREE_OPERAND (expr, 0);
1680 4392 : obj = expr;
1681 4392 : expr = get_ref_base_and_extent_hwi (expr, offset, &size, &reverse);
1682 :
1683 4392 : offset_int mem_offset;
1684 4392 : if (!expr
1685 4388 : || TREE_CODE (expr) != MEM_REF
1686 8780 : || !mem_ref_offset (expr).is_constant (&mem_offset))
1687 4 : return NULL_TREE;
1688 4388 : parm = TREE_OPERAND (expr, 0);
1689 4388 : if (TREE_CODE (parm) != SSA_NAME
1690 4388 : || !SSA_NAME_IS_DEFAULT_DEF (parm)
1691 5148 : || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1692 : return NULL_TREE;
1693 :
1694 760 : *offset += mem_offset.to_short_addr () * BITS_PER_UNIT;
1695 760 : *obj_p = obj;
1696 760 : return expr;
1697 : }
1698 :
1699 :
1700 : /* Given that an actual argument is an SSA_NAME that is a result of a phi
1701 : statement PHI, try to find out whether NAME is in fact a
1702 : multiple-inheritance typecast from a descendant into an ancestor of a formal
1703 : parameter and thus can be described by an ancestor jump function and if so,
1704 : write the appropriate function into JFUNC.
1705 :
1706 : Essentially we want to match the following pattern:
1707 :
1708 : if (obj_2(D) != 0B)
1709 : goto <bb 3>;
1710 : else
1711 : goto <bb 4>;
1712 :
1713 : <bb 3>:
1714 : iftmp.1_3 = &obj_2(D)->D.1762;
1715 :
1716 : <bb 4>:
1717 : # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1718 : D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1719 : return D.1879_6; */
1720 :
1721 : static void
1722 89247 : compute_complex_ancestor_jump_func (struct ipa_func_body_info *fbi,
1723 : class ipa_node_params *info,
1724 : struct ipa_jump_func *jfunc,
1725 : gcall *call, gphi *phi)
1726 : {
1727 89247 : HOST_WIDE_INT offset;
1728 89247 : gimple *assign;
1729 89247 : basic_block phi_bb, assign_bb, cond_bb;
1730 89247 : tree tmp, parm, expr, obj;
1731 89247 : int index, i;
1732 :
1733 89247 : if (gimple_phi_num_args (phi) != 2)
1734 89233 : return;
1735 :
1736 75780 : if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1737 4094 : tmp = PHI_ARG_DEF (phi, 0);
1738 71686 : else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1739 12749 : tmp = PHI_ARG_DEF (phi, 1);
1740 : else
1741 : return;
1742 16843 : if (TREE_CODE (tmp) != SSA_NAME
1743 14705 : || SSA_NAME_IS_DEFAULT_DEF (tmp)
1744 14594 : || !POINTER_TYPE_P (TREE_TYPE (tmp))
1745 19463 : || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1746 : return;
1747 :
1748 972 : assign = SSA_NAME_DEF_STMT (tmp);
1749 972 : assign_bb = gimple_bb (assign);
1750 89787 : if (!single_pred_p (assign_bb))
1751 : return;
1752 554 : expr = get_ancestor_addr_info (assign, &obj, &offset);
1753 554 : if (!expr)
1754 : return;
1755 26 : parm = TREE_OPERAND (expr, 0);
1756 26 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1757 26 : if (index < 0)
1758 : return;
1759 :
1760 26 : cond_bb = single_pred (assign_bb);
1761 52 : gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
1762 26 : if (!cond
1763 26 : || gimple_cond_code (cond) != NE_EXPR
1764 26 : || gimple_cond_lhs (cond) != parm
1765 14 : || !integer_zerop (gimple_cond_rhs (cond)))
1766 12 : return;
1767 :
1768 14 : phi_bb = gimple_bb (phi);
1769 42 : for (i = 0; i < 2; i++)
1770 : {
1771 28 : basic_block pred = EDGE_PRED (phi_bb, i)->src;
1772 28 : if (pred != assign_bb && pred != cond_bb)
1773 : return;
1774 : }
1775 :
1776 14 : ipa_set_ancestor_jf (jfunc, offset, index,
1777 14 : parm_ref_data_pass_through_p (fbi, index, call, parm),
1778 : true);
1779 : }
1780 :
1781 : /* Inspect the given TYPE and return true iff it has the same structure (the
1782 : same number of fields of the same types) as a C++ member pointer. If
1783 : METHOD_PTR and DELTA are non-NULL, store the trees representing the
1784 : corresponding fields there. */
1785 :
1786 : static bool
1787 876 : type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1788 : {
1789 876 : tree fld;
1790 :
1791 876 : if (TREE_CODE (type) != RECORD_TYPE)
1792 : return false;
1793 :
1794 876 : fld = TYPE_FIELDS (type);
1795 876 : if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1796 876 : || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1797 1752 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1798 : return false;
1799 :
1800 876 : if (method_ptr)
1801 876 : *method_ptr = fld;
1802 :
1803 876 : fld = DECL_CHAIN (fld);
1804 876 : if (!fld || INTEGRAL_TYPE_P (fld)
1805 1752 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1806 : return false;
1807 876 : if (delta)
1808 876 : *delta = fld;
1809 :
1810 876 : if (DECL_CHAIN (fld))
1811 : return false;
1812 :
1813 : return true;
1814 : }
1815 :
1816 : /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1817 : return the rhs of its defining statement, and this statement is stored in
1818 : *RHS_STMT. Otherwise return RHS as it is. */
1819 :
1820 : static inline tree
1821 123137 : get_ssa_def_if_simple_copy (tree rhs, gimple **rhs_stmt)
1822 : {
1823 165318 : while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1824 : {
1825 89750 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
1826 :
1827 89750 : if (gimple_assign_single_p (def_stmt))
1828 42181 : rhs = gimple_assign_rhs1 (def_stmt);
1829 : else
1830 : break;
1831 42181 : *rhs_stmt = def_stmt;
1832 : }
1833 123137 : return rhs;
1834 : }
1835 :
1836 : /* Simple linked list, describing contents of an aggregate before call. */
1837 :
1838 : struct ipa_known_agg_contents_list
1839 : {
1840 : /* Offset and size of the described part of the aggregate. */
1841 : HOST_WIDE_INT offset, size;
1842 :
1843 : /* Type of the described part of the aggregate. */
1844 : tree type;
1845 :
1846 : /* Known constant value or jump function data describing contents. */
1847 : struct ipa_load_agg_data value;
1848 :
1849 : /* Pointer to the next structure in the list. */
1850 : struct ipa_known_agg_contents_list *next;
1851 : };
1852 :
1853 : /* Add an aggregate content item into a linked list of
1854 : ipa_known_agg_contents_list structure, in which all elements
1855 : are sorted ascendingly by offset. */
1856 :
1857 : static inline void
1858 2718883 : add_to_agg_contents_list (struct ipa_known_agg_contents_list **plist,
1859 : struct ipa_known_agg_contents_list *item)
1860 : {
1861 2718883 : struct ipa_known_agg_contents_list *list = *plist;
1862 :
1863 5054285 : for (; list; list = list->next)
1864 : {
1865 3860453 : if (list->offset >= item->offset)
1866 : break;
1867 :
1868 2335402 : plist = &list->next;
1869 : }
1870 :
1871 2718883 : item->next = list;
1872 2718883 : *plist = item;
1873 : }
1874 :
1875 : /* Check whether a given aggregate content is clobbered by certain element in
1876 : a linked list of ipa_known_agg_contents_list. */
1877 :
1878 : static inline bool
1879 1061421 : clobber_by_agg_contents_list_p (struct ipa_known_agg_contents_list *list,
1880 : struct ipa_known_agg_contents_list *item)
1881 : {
1882 2268701 : for (; list; list = list->next)
1883 : {
1884 1835378 : if (list->offset >= item->offset)
1885 615438 : return list->offset < item->offset + item->size;
1886 :
1887 1219940 : if (list->offset + list->size > item->offset)
1888 : return true;
1889 : }
1890 :
1891 : return false;
1892 : }
1893 :
1894 : /* Build aggregate jump function from LIST, assuming there are exactly
1895 : VALUE_COUNT entries there and that offset of the passed argument
1896 : is ARG_OFFSET and store it into JFUNC. */
1897 :
1898 : static void
1899 342049 : build_agg_jump_func_from_list (struct ipa_known_agg_contents_list *list,
1900 : int value_count, HOST_WIDE_INT arg_offset,
1901 : struct ipa_jump_func *jfunc)
1902 : {
1903 342049 : vec_safe_reserve (jfunc->agg.items, value_count, true);
1904 1342097 : for (; list; list = list->next)
1905 : {
1906 1000048 : struct ipa_agg_jf_item item;
1907 1000048 : tree operand = list->value.pass_through.operand;
1908 :
1909 1000048 : if (list->value.pass_through.formal_id >= 0)
1910 : {
1911 : /* Content value is derived from some formal paramerter. */
1912 87682 : if (list->value.offset >= 0)
1913 45838 : item.jftype = IPA_JF_LOAD_AGG;
1914 : else
1915 41844 : item.jftype = IPA_JF_PASS_THROUGH;
1916 :
1917 87682 : item.value.load_agg = list->value;
1918 87682 : if (operand)
1919 8943 : item.value.pass_through.operand
1920 8943 : = unshare_expr_without_location (operand);
1921 : }
1922 912366 : else if (operand)
1923 : {
1924 : /* Content value is known constant. */
1925 912366 : item.jftype = IPA_JF_CONST;
1926 912366 : item.value.constant = unshare_expr_without_location (operand);
1927 : }
1928 : else
1929 0 : continue;
1930 :
1931 1000048 : item.type = list->type;
1932 1000048 : gcc_assert (tree_to_shwi (TYPE_SIZE (list->type)) == list->size);
1933 :
1934 1000048 : item.offset = list->offset - arg_offset;
1935 1000048 : gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1936 :
1937 1000048 : jfunc->agg.items->quick_push (item);
1938 : }
1939 342049 : }
1940 :
1941 : /* Given an assignment statement STMT, try to collect information into
1942 : AGG_VALUE that will be used to construct jump function for RHS of the
1943 : assignment, from which content value of an aggregate part comes.
1944 :
1945 : Besides constant and simple pass-through jump functions, also try to
1946 : identify whether it matches the following pattern that can be described by
1947 : a load-value-from-aggregate jump function, which is a derivative of simple
1948 : pass-through jump function.
1949 :
1950 : foo (int *p)
1951 : {
1952 : ...
1953 :
1954 : *(q_5 + 4) = *(p_3(D) + 28) op 1;
1955 : bar (q_5);
1956 : }
1957 :
1958 : Here IPA_LOAD_AGG_DATA data structure is informative enough to describe
1959 : constant, simple pass-through and load-vale-from-aggregate. If value
1960 : is constant, it will be kept in field OPERAND, and field FORMAL_ID is
1961 : set to -1. For simple pass-through and load-value-from-aggregate, field
1962 : FORMAL_ID specifies the related formal parameter index, and field
1963 : OFFSET can be used to distinguish them, -1 means simple pass-through,
1964 : otherwise means load-value-from-aggregate. */
1965 :
1966 : static void
1967 1721068 : analyze_agg_content_value (struct ipa_func_body_info *fbi,
1968 : struct ipa_load_agg_data *agg_value,
1969 : gimple *stmt)
1970 : {
1971 1721068 : tree lhs = gimple_assign_lhs (stmt);
1972 1721068 : tree rhs1 = gimple_assign_rhs1 (stmt);
1973 1721068 : enum tree_code code;
1974 1721068 : int index = -1;
1975 :
1976 : /* Initialize jump function data for the aggregate part. */
1977 1721068 : memset (agg_value, 0, sizeof (*agg_value));
1978 1721068 : agg_value->pass_through.operation = NOP_EXPR;
1979 1721068 : agg_value->pass_through.formal_id = -1;
1980 1721068 : agg_value->offset = -1;
1981 :
1982 1721068 : if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) /* TODO: Support aggregate type. */
1983 1532106 : || TREE_THIS_VOLATILE (lhs)
1984 1531438 : || TREE_CODE (lhs) == BIT_FIELD_REF
1985 1531430 : || contains_bitfld_component_ref_p (lhs))
1986 198487 : return;
1987 :
1988 : /* Skip SSA copies. */
1989 1797748 : while (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1990 : {
1991 1714967 : if (TREE_CODE (rhs1) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (rhs1))
1992 : break;
1993 :
1994 364447 : stmt = SSA_NAME_DEF_STMT (rhs1);
1995 364447 : if (!is_gimple_assign (stmt))
1996 : break;
1997 :
1998 275167 : lhs = gimple_assign_lhs (stmt);
1999 275167 : rhs1 = gimple_assign_rhs1 (stmt);
2000 : }
2001 :
2002 1522581 : if (gphi *phi = dyn_cast<gphi *> (stmt))
2003 : {
2004 : /* Also special case like the following (a is a formal parameter):
2005 :
2006 : _12 = *a_11(D).dim[0].stride;
2007 : ...
2008 : # iftmp.22_9 = PHI <_12(2), 1(3)>
2009 : ...
2010 : parm.6.dim[0].stride = iftmp.22_9;
2011 : ...
2012 : __x_MOD_foo (&parm.6, b_31(D));
2013 :
2014 : The aggregate function describing parm.6.dim[0].stride is encoded as a
2015 : PASS-THROUGH jump function with ASSERT_EXPR operation whith operand 1
2016 : (the constant from the PHI node). */
2017 :
2018 33583 : if (gimple_phi_num_args (phi) != 2
2019 33583 : || lto_variably_modified_type_p (TREE_TYPE (lhs)))
2020 6704 : return;
2021 26879 : tree arg0 = gimple_phi_arg_def (phi, 0);
2022 26879 : tree arg1 = gimple_phi_arg_def (phi, 1);
2023 26879 : tree operand;
2024 :
2025 26879 : if (is_gimple_ip_invariant (arg1))
2026 : {
2027 : operand = arg1;
2028 : rhs1 = arg0;
2029 : }
2030 22012 : else if (is_gimple_ip_invariant (arg0))
2031 : {
2032 : operand = arg0;
2033 : rhs1 = arg1;
2034 : }
2035 : else
2036 : return;
2037 :
2038 9917 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
2039 9917 : if (!is_gimple_assign (stmt))
2040 : return;
2041 :
2042 4449 : code = ASSERT_EXPR;
2043 4449 : agg_value->pass_through.operand = operand;
2044 4449 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2045 : }
2046 1488998 : else if (is_gimple_assign (stmt))
2047 : {
2048 1433301 : code = gimple_assign_rhs_code (stmt);
2049 1433301 : switch (gimple_assign_rhs_class (stmt))
2050 : {
2051 1350520 : case GIMPLE_SINGLE_RHS:
2052 1350520 : if (is_gimple_ip_invariant (rhs1))
2053 : {
2054 971915 : agg_value->pass_through.operand = rhs1;
2055 971915 : return;
2056 : }
2057 : code = NOP_EXPR;
2058 : break;
2059 :
2060 26742 : case GIMPLE_UNARY_RHS:
2061 : /* NOTE: A GIMPLE_UNARY_RHS operation might not be tcc_unary
2062 : (truth_not_expr is example), GIMPLE_BINARY_RHS does not imply
2063 : tcc_binary, this subtleness is somewhat misleading.
2064 :
2065 : Since tcc_unary is widely used in IPA-CP code to check an operation
2066 : with one operand, here we only allow tc_unary operation to avoid
2067 : possible problem. Then we can use (opclass == tc_unary) or not to
2068 : distinguish unary and binary. */
2069 26742 : if (TREE_CODE_CLASS (code) != tcc_unary || CONVERT_EXPR_CODE_P (code)
2070 28876 : || lto_variably_modified_type_p (TREE_TYPE (lhs)))
2071 24608 : return;
2072 :
2073 2134 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
2074 2134 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2075 2134 : break;
2076 :
2077 55547 : case GIMPLE_BINARY_RHS:
2078 55547 : {
2079 55547 : gimple *rhs1_stmt = stmt;
2080 55547 : gimple *rhs2_stmt = stmt;
2081 55547 : tree rhs2 = gimple_assign_rhs2 (stmt);
2082 :
2083 55547 : if (lto_variably_modified_type_p (TREE_TYPE (lhs)))
2084 28244 : return;
2085 :
2086 55543 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &rhs1_stmt);
2087 55543 : rhs2 = get_ssa_def_if_simple_copy (rhs2, &rhs2_stmt);
2088 :
2089 55543 : if (is_gimple_ip_invariant (rhs2))
2090 : {
2091 27303 : agg_value->pass_through.operand = rhs2;
2092 27303 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2093 27303 : stmt = rhs1_stmt;
2094 : }
2095 28240 : else if (is_gimple_ip_invariant (rhs1))
2096 : {
2097 2190 : if (TREE_CODE_CLASS (code) == tcc_comparison)
2098 0 : code = swap_tree_comparison (code);
2099 2190 : else if (!commutative_tree_code (code))
2100 : return;
2101 :
2102 0 : agg_value->pass_through.operand = rhs1;
2103 0 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2104 0 : stmt = rhs2_stmt;
2105 0 : rhs1 = rhs2;
2106 : }
2107 : else
2108 : return;
2109 :
2110 27303 : if (TREE_CODE_CLASS (code) != tcc_comparison
2111 54068 : && !useless_type_conversion_p (TREE_TYPE (lhs),
2112 26765 : TREE_TYPE (rhs1)))
2113 : return;
2114 : }
2115 27303 : break;
2116 :
2117 : default:
2118 : return;
2119 : }
2120 : }
2121 : else
2122 : return;
2123 :
2124 412491 : if (TREE_CODE (rhs1) != SSA_NAME)
2125 352866 : index = load_from_unmodified_param_or_agg (fbi, fbi->info, stmt,
2126 : &agg_value->offset,
2127 : &agg_value->by_ref);
2128 59625 : else if (SSA_NAME_IS_DEFAULT_DEF (rhs1))
2129 45630 : index = ipa_get_param_decl_index (fbi->info, SSA_NAME_VAR (rhs1));
2130 :
2131 398496 : if (index >= 0)
2132 : {
2133 89506 : if (agg_value->offset >= 0)
2134 47377 : agg_value->type = TREE_TYPE (rhs1);
2135 89506 : agg_value->pass_through.formal_id = index;
2136 89506 : agg_value->pass_through.operation = code;
2137 : }
2138 : else
2139 322985 : agg_value->pass_through.operand = NULL_TREE;
2140 : }
2141 :
2142 : /* If STMT is a memory store to the object whose address is BASE, extract
2143 : information (offset, size, and value) into CONTENT, and return true,
2144 : otherwise we conservatively assume the whole object is modified with
2145 : unknown content, and return false. CHECK_REF means that access to object
2146 : is expected to be in form of MEM_REF expression. */
2147 :
2148 : static bool
2149 2811586 : extract_mem_content (struct ipa_func_body_info *fbi,
2150 : gimple *stmt, tree base, bool check_ref,
2151 : struct ipa_known_agg_contents_list *content)
2152 : {
2153 2811586 : HOST_WIDE_INT lhs_offset, lhs_size;
2154 2811586 : bool reverse;
2155 :
2156 2811586 : if (!is_gimple_assign (stmt))
2157 : return false;
2158 :
2159 1833736 : tree lhs = gimple_assign_lhs (stmt);
2160 1833736 : tree lhs_base = get_ref_base_and_extent_hwi (lhs, &lhs_offset, &lhs_size,
2161 : &reverse);
2162 1833736 : if (!lhs_base)
2163 : return false;
2164 :
2165 1832077 : if (check_ref)
2166 : {
2167 146921 : if (TREE_CODE (lhs_base) != MEM_REF
2168 118530 : || TREE_OPERAND (lhs_base, 0) != base
2169 193129 : || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
2170 106973 : return false;
2171 : }
2172 1685156 : else if (lhs_base != base)
2173 : return false;
2174 :
2175 1721068 : content->offset = lhs_offset;
2176 1721068 : content->size = lhs_size;
2177 1721068 : content->type = TREE_TYPE (lhs);
2178 1721068 : content->next = NULL;
2179 :
2180 1721068 : analyze_agg_content_value (fbi, &content->value, stmt);
2181 1721068 : return true;
2182 : }
2183 :
2184 : /* Traverse statements from CALL backwards, scanning whether an aggregate given
2185 : in ARG is filled in constants or values that are derived from caller's
2186 : formal parameter in the way described by some kinds of jump functions. FBI
2187 : is the context of the caller function for interprocedural analysis. ARG can
2188 : either be an aggregate expression or a pointer to an aggregate. ARG_TYPE is
2189 : the type of the aggregate, JFUNC is the jump function for the aggregate. */
2190 :
2191 : static void
2192 3357319 : determine_known_aggregate_parts (struct ipa_func_body_info *fbi,
2193 : gcall *call, tree arg,
2194 : tree arg_type,
2195 : struct ipa_jump_func *jfunc)
2196 : {
2197 3357319 : struct ipa_known_agg_contents_list *list = NULL, *all_list = NULL;
2198 3357319 : bitmap visited = NULL;
2199 3357319 : int item_count = 0, value_count = 0;
2200 3357319 : HOST_WIDE_INT arg_offset, arg_size;
2201 3357319 : tree arg_base;
2202 3357319 : bool check_ref, by_ref;
2203 3357319 : ao_ref r;
2204 3357319 : int max_agg_items = opt_for_fn (fbi->node->decl, param_ipa_max_agg_items);
2205 :
2206 3357319 : if (max_agg_items == 0)
2207 844204 : return;
2208 :
2209 : /* The function operates in three stages. First, we prepare check_ref, r,
2210 : arg_base and arg_offset based on what is actually passed as an actual
2211 : argument. */
2212 :
2213 3357319 : if (POINTER_TYPE_P (arg_type))
2214 : {
2215 2997117 : by_ref = true;
2216 2997117 : if (TREE_CODE (arg) == SSA_NAME)
2217 : {
2218 1096279 : tree type_size;
2219 1096279 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type)))
2220 1096279 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
2221 : return;
2222 778399 : check_ref = true;
2223 778399 : arg_base = arg;
2224 778399 : arg_offset = 0;
2225 778399 : type_size = TYPE_SIZE (TREE_TYPE (arg_type));
2226 778399 : arg_size = tree_to_uhwi (type_size);
2227 778399 : ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
2228 : }
2229 1900838 : else if (TREE_CODE (arg) == ADDR_EXPR)
2230 : {
2231 1827264 : bool reverse;
2232 :
2233 1827264 : arg = TREE_OPERAND (arg, 0);
2234 1827264 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2235 : &arg_size, &reverse);
2236 1827264 : if (!arg_base)
2237 452674 : return;
2238 1826048 : if (DECL_P (arg_base))
2239 : {
2240 1374590 : check_ref = false;
2241 1374590 : ao_ref_init (&r, arg_base);
2242 : }
2243 : else
2244 : return;
2245 : }
2246 : else
2247 : return;
2248 : }
2249 : else
2250 : {
2251 360202 : bool reverse;
2252 :
2253 360202 : gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
2254 :
2255 360202 : by_ref = false;
2256 360202 : check_ref = false;
2257 360202 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2258 : &arg_size, &reverse);
2259 360202 : if (!arg_base)
2260 76 : return;
2261 :
2262 360126 : ao_ref_init (&r, arg);
2263 : }
2264 :
2265 : /* Second stage traverses virtual SSA web backwards starting from the call
2266 : statement, only looks at individual dominating virtual operand (its
2267 : definition dominates the call), as long as it is confident that content
2268 : of the aggregate is affected by definition of the virtual operand, it
2269 : builds a sorted linked list of ipa_agg_jf_list describing that. */
2270 :
2271 2513115 : for (tree dom_vuse = gimple_vuse (call);
2272 24843063 : dom_vuse && fbi->aa_walk_budget > 0;)
2273 : {
2274 24341567 : gimple *stmt = SSA_NAME_DEF_STMT (dom_vuse);
2275 :
2276 24341567 : if (gphi *phi = dyn_cast <gphi *> (stmt))
2277 : {
2278 2365328 : dom_vuse = get_continuation_for_phi (phi, &r, true,
2279 1182664 : fbi->aa_walk_budget,
2280 : &visited, false, NULL, NULL);
2281 1182664 : continue;
2282 : }
2283 :
2284 23158903 : fbi->aa_walk_budget--;
2285 23158903 : if (stmt_may_clobber_ref_p_1 (stmt, &r))
2286 : {
2287 2811586 : struct ipa_known_agg_contents_list *content
2288 2811586 : = XALLOCA (struct ipa_known_agg_contents_list);
2289 :
2290 2811586 : if (!extract_mem_content (fbi, stmt, arg_base, check_ref, content))
2291 : break;
2292 :
2293 : /* Now we get a dominating virtual operand, and need to check
2294 : whether its value is clobbered any other dominating one. */
2295 1721068 : if ((content->value.pass_through.formal_id >= 0
2296 1631562 : || content->value.pass_through.operand)
2297 1061421 : && !clobber_by_agg_contents_list_p (all_list, content)
2298 : /* Since IPA-CP stores results with unsigned int offsets, we can
2299 : discard those which would not fit now before we stream them to
2300 : WPA. */
2301 2721254 : && (content->offset + content->size - arg_offset
2302 : <= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT))
2303 : {
2304 1000048 : struct ipa_known_agg_contents_list *copy
2305 1000048 : = XALLOCA (struct ipa_known_agg_contents_list);
2306 :
2307 : /* Add to the list consisting of only dominating virtual
2308 : operands, whose definitions can finally reach the call. */
2309 1000048 : add_to_agg_contents_list (&list, (*copy = *content, copy));
2310 :
2311 1000048 : if (++value_count == max_agg_items)
2312 : break;
2313 : }
2314 :
2315 : /* Add to the list consisting of all dominating virtual operands. */
2316 1718835 : add_to_agg_contents_list (&all_list, content);
2317 :
2318 1718835 : if (++item_count == 2 * max_agg_items)
2319 : break;
2320 : }
2321 43212236 : dom_vuse = gimple_vuse (stmt);
2322 : }
2323 :
2324 2513115 : if (visited)
2325 614132 : BITMAP_FREE (visited);
2326 :
2327 : /* Third stage just goes over the list and creates an appropriate vector of
2328 : ipa_agg_jf_item structures out of it, of course only if there are
2329 : any meaningful items to begin with. */
2330 :
2331 2513115 : if (value_count)
2332 : {
2333 342049 : jfunc->agg.by_ref = by_ref;
2334 342049 : build_agg_jump_func_from_list (list, value_count, arg_offset, jfunc);
2335 : }
2336 : }
2337 :
2338 :
2339 : /* Return the Ith param type of callee associated with call graph
2340 : edge E. */
2341 :
2342 : tree
2343 6259395 : ipa_get_callee_param_type (struct cgraph_edge *e, int i)
2344 : {
2345 6259395 : int n;
2346 6259395 : tree type = (e->callee
2347 6259395 : ? TREE_TYPE (e->callee->decl)
2348 6259395 : : gimple_call_fntype (e->call_stmt));
2349 6259395 : tree t = TYPE_ARG_TYPES (type);
2350 :
2351 12863310 : for (n = 0; n < i; n++)
2352 : {
2353 6856879 : if (!t)
2354 : break;
2355 6603915 : t = TREE_CHAIN (t);
2356 : }
2357 6259395 : if (t && t != void_list_node)
2358 5913302 : return TREE_VALUE (t);
2359 346093 : if (!e->callee)
2360 : return NULL;
2361 324307 : t = DECL_ARGUMENTS (e->callee->decl);
2362 850974 : for (n = 0; n < i; n++)
2363 : {
2364 806479 : if (!t)
2365 : return NULL;
2366 526667 : t = TREE_CHAIN (t);
2367 : }
2368 44495 : if (t)
2369 2192 : return TREE_TYPE (t);
2370 : return NULL;
2371 : }
2372 :
2373 : /* Return a pointer to an ipa_vr just like TMP, but either find it in
2374 : ipa_vr_hash_table or allocate it in GC memory. */
2375 :
2376 : static ipa_vr *
2377 5737093 : ipa_get_value_range (const vrange &tmp)
2378 : {
2379 5737093 : inchash::hash hstate;
2380 5737093 : inchash::add_vrange (tmp, hstate);
2381 5737093 : hashval_t hash = hstate.end ();
2382 5737093 : ipa_vr **slot = ipa_vr_hash_table->find_slot_with_hash (&tmp, hash, INSERT);
2383 5737093 : if (*slot)
2384 : return *slot;
2385 :
2386 641681 : ipa_vr *vr = new (ggc_alloc<ipa_vr> ()) ipa_vr (tmp);
2387 641681 : *slot = vr;
2388 641681 : return vr;
2389 : }
2390 :
2391 : /* Assign to JF a pointer to a range just like TMP but either fetch a
2392 : copy from ipa_vr_hash_table or allocate a new on in GC memory. */
2393 :
2394 : static void
2395 4990534 : ipa_set_jfunc_vr (ipa_jump_func *jf, const vrange &tmp)
2396 : {
2397 1887514 : jf->m_vr = ipa_get_value_range (tmp);
2398 3103020 : }
2399 :
2400 : static void
2401 606622 : ipa_set_jfunc_vr (ipa_jump_func *jf, const ipa_vr &vr)
2402 : {
2403 606622 : value_range tmp;
2404 606622 : vr.get_vrange (tmp);
2405 606622 : ipa_set_jfunc_vr (jf, tmp);
2406 606622 : }
2407 :
2408 : /* Given VAL that conforms to is_gimple_ip_invariant, produce a VRANGE that
2409 : represents it as a range. CONTEXT_NODE is the call graph node representing
2410 : the function for which optimization flags should be evaluated. */
2411 :
2412 : void
2413 1621590 : ipa_get_range_from_ip_invariant (vrange &r, tree val, cgraph_node *context_node)
2414 : {
2415 1621590 : if (TREE_CODE (val) == ADDR_EXPR)
2416 : {
2417 1023 : symtab_node *symbol;
2418 1023 : tree base = TREE_OPERAND (val, 0);
2419 1023 : if (!DECL_P (base))
2420 : {
2421 182 : r.set_varying (TREE_TYPE (val));
2422 182 : return;
2423 : }
2424 841 : if (!decl_in_symtab_p (base))
2425 : {
2426 0 : r.set_nonzero (TREE_TYPE (val));
2427 0 : return;
2428 : }
2429 841 : if (!(symbol = symtab_node::get (base)))
2430 : {
2431 0 : r.set_varying (TREE_TYPE (val));
2432 0 : return;
2433 : }
2434 :
2435 841 : bool delete_null_pointer_checks
2436 841 : = opt_for_fn (context_node->decl, flag_delete_null_pointer_checks);
2437 841 : if (symbol->nonzero_address (delete_null_pointer_checks))
2438 841 : r.set_nonzero (TREE_TYPE (val));
2439 : else
2440 0 : r.set_varying (TREE_TYPE (val));
2441 : }
2442 : else
2443 1620567 : r.set (val, val);
2444 : }
2445 :
2446 : /* If T is an SSA_NAME that is the result of a simple type conversion statement
2447 : from an integer type to another integer type which is known to be able to
2448 : represent the values the operand of the conversion can hold, return the
2449 : operand of that conversion, otherwise return T. */
2450 :
2451 : static tree
2452 6259395 : skip_a_safe_conversion_op (tree t)
2453 : {
2454 6259395 : if (TREE_CODE (t) != SSA_NAME
2455 6259395 : || SSA_NAME_IS_DEFAULT_DEF (t))
2456 : return t;
2457 :
2458 1541474 : gimple *def = SSA_NAME_DEF_STMT (t);
2459 1541474 : if (!is_gimple_assign (def)
2460 1277460 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def))
2461 251124 : || !INTEGRAL_TYPE_P (TREE_TYPE (t))
2462 1727309 : || !INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def))))
2463 : return t;
2464 :
2465 177817 : tree rhs1 = gimple_assign_rhs1 (def);
2466 177817 : if (TYPE_PRECISION (TREE_TYPE (t))
2467 177817 : >= TYPE_PRECISION (TREE_TYPE (rhs1)))
2468 : return gimple_assign_rhs1 (def);
2469 :
2470 8295 : value_range vr (TREE_TYPE (rhs1));
2471 16590 : if (!get_range_query (cfun)->range_of_expr (vr, rhs1, def)
2472 8295 : || vr.undefined_p ())
2473 : return t;
2474 :
2475 8277 : irange &ir = as_a <irange> (vr);
2476 8277 : if (range_fits_type_p (&ir, TYPE_PRECISION (TREE_TYPE (t)),
2477 8277 : TYPE_SIGN (TREE_TYPE (t))))
2478 4012 : return gimple_assign_rhs1 (def);
2479 :
2480 : return t;
2481 8295 : }
2482 :
2483 : /* Initializes ipa_edge_args summary of CBE given its callback-carrying edge.
2484 : This primarily means allocating the correct amount of jump functions. */
2485 :
2486 : static inline void
2487 15083 : init_callback_edge_summary (struct cgraph_edge *cbe, tree attr)
2488 : {
2489 15083 : ipa_edge_args *cb_args = ipa_edge_args_sum->get_create (cbe);
2490 15083 : size_t jf_vec_length = callback_num_args(attr);
2491 15083 : vec_safe_grow_cleared (cb_args->jump_functions,
2492 : jf_vec_length, true);
2493 15083 : }
2494 :
2495 : /* Compute jump function for all arguments of callsite CS and insert the
2496 : information in the jump_functions array in the ipa_edge_args corresponding
2497 : to this callsite. */
2498 :
2499 : static void
2500 2961938 : ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi,
2501 : struct cgraph_edge *cs)
2502 : {
2503 2961938 : ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
2504 2961938 : ipa_edge_args *args = ipa_edge_args_sum->get_create (cs);
2505 2961938 : gcall *call = cs->call_stmt;
2506 2961938 : int n, arg_num = gimple_call_num_args (call);
2507 2961938 : bool useful_context = false;
2508 :
2509 2961938 : if (arg_num == 0 || args->jump_functions)
2510 273484 : return;
2511 2688454 : vec_safe_grow_cleared (args->jump_functions, arg_num, true);
2512 2688454 : if (flag_devirtualize)
2513 2507087 : vec_safe_grow_cleared (args->polymorphic_call_contexts, arg_num, true);
2514 :
2515 2688454 : if (gimple_call_internal_p (call))
2516 : return;
2517 2688454 : if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
2518 : return;
2519 :
2520 2688454 : auto_vec<cgraph_edge*> callback_edges;
2521 8947849 : for (n = 0; n < arg_num; n++)
2522 : {
2523 6259395 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
2524 6259395 : tree arg = gimple_call_arg (call, n);
2525 6259395 : tree param_type = ipa_get_callee_param_type (cs, n);
2526 6259395 : if (flag_devirtualize && POINTER_TYPE_P (TREE_TYPE (arg)))
2527 : {
2528 3214939 : tree instance;
2529 3214939 : class ipa_polymorphic_call_context context (cs->caller->decl,
2530 3214939 : arg, cs->call_stmt,
2531 3214939 : &instance);
2532 3214939 : context.get_dynamic_type (instance, arg, NULL, cs->call_stmt,
2533 : &fbi->aa_walk_budget);
2534 3214939 : *ipa_get_ith_polymorhic_call_context (args, n) = context;
2535 6429878 : if (!context.useless_p ())
2536 : useful_context = true;
2537 : }
2538 :
2539 6259395 : value_range vr (TREE_TYPE (arg));
2540 6259395 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2541 : {
2542 6943412 : if (!get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2543 3471706 : || vr.varying_p ()
2544 6025585 : || vr.undefined_p ())
2545 : {
2546 918240 : if (tree_single_nonzero_p (arg))
2547 0 : vr.set_nonzero (TREE_TYPE (arg));
2548 : else
2549 918240 : vr.set_varying (TREE_TYPE (arg));
2550 : }
2551 3471706 : gcc_assert (!vr.undefined_p ());
2552 3471706 : unsigned HOST_WIDE_INT bitpos;
2553 3471706 : unsigned align = BITS_PER_UNIT;
2554 :
2555 3471706 : if (!vr.singleton_p ())
2556 3398549 : get_pointer_alignment_1 (arg, &align, &bitpos);
2557 :
2558 3471706 : if (align > BITS_PER_UNIT
2559 3471706 : && opt_for_fn (cs->caller->decl, flag_ipa_bit_cp))
2560 : {
2561 1280892 : unsigned prec = TYPE_PRECISION (TREE_TYPE (arg));
2562 1280892 : wide_int mask
2563 2561784 : = wi::bit_and_not (wi::mask (prec, false, prec),
2564 1280892 : wide_int::from (align / BITS_PER_UNIT - 1,
2565 1280892 : prec, UNSIGNED));
2566 1280892 : wide_int value = wide_int::from (bitpos / BITS_PER_UNIT, prec,
2567 1280892 : UNSIGNED);
2568 1280892 : irange_bitmask bm (value, mask);
2569 1280892 : vr.update_bitmask (bm);
2570 1280892 : ipa_set_jfunc_vr (jfunc, vr);
2571 1280892 : }
2572 2190814 : else if (!vr.varying_p ())
2573 1335815 : ipa_set_jfunc_vr (jfunc, vr);
2574 : else
2575 854999 : gcc_assert (!jfunc->m_vr);
2576 : }
2577 : else
2578 : {
2579 2787689 : if (param_type
2580 2477106 : && ipa_vr_supported_type_p (TREE_TYPE (arg))
2581 2787827 : && ipa_vr_supported_type_p (param_type)
2582 3700560 : && get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2583 4637969 : && !vr.undefined_p ())
2584 : {
2585 1850142 : value_range resvr (vr);
2586 1850142 : range_cast (resvr, param_type);
2587 1850142 : if (!resvr.undefined_p () && !resvr.varying_p ())
2588 1477863 : ipa_set_jfunc_vr (jfunc, resvr);
2589 : else
2590 372279 : gcc_assert (!jfunc->m_vr);
2591 1850142 : }
2592 : else
2593 937547 : gcc_assert (!jfunc->m_vr);
2594 : }
2595 :
2596 6259395 : arg = skip_a_safe_conversion_op (arg);
2597 6259395 : if (is_gimple_ip_invariant (arg)
2598 6259395 : || (VAR_P (arg) && is_global_var (arg) && TREE_READONLY (arg)))
2599 : {
2600 2312058 : ipa_set_jf_constant (jfunc, arg, cs);
2601 2312058 : if (TREE_CODE (arg) == ADDR_EXPR)
2602 : {
2603 871384 : tree pointee = TREE_OPERAND (arg, 0);
2604 871384 : if (TREE_CODE (pointee) == FUNCTION_DECL && !cs->callback
2605 44160 : && cs->callee)
2606 : {
2607 : /* Argument is a pointer to a function. Look for a callback
2608 : attribute describing this argument. */
2609 43810 : tree callback_attr
2610 43810 : = lookup_attribute (CALLBACK_ATTR_IDENT,
2611 43810 : DECL_ATTRIBUTES (cs->callee->decl));
2612 87620 : for (; callback_attr;
2613 : callback_attr
2614 0 : = lookup_attribute (CALLBACK_ATTR_IDENT,
2615 0 : TREE_CHAIN (callback_attr)))
2616 13313 : if (callback_get_fn_index (callback_attr) == n)
2617 : break;
2618 :
2619 : /* If no callback attribute is found, check if the function is
2620 : a special case. */
2621 43810 : if (!callback_attr
2622 43810 : && callback_is_special_cased (cs->callee->decl, call))
2623 : {
2624 1770 : callback_attr
2625 1770 : = callback_special_case_attr (cs->callee->decl);
2626 : /* Check if the special attribute describes the correct
2627 : attribute, as a special cased function might have
2628 : multiple callbacks. */
2629 1770 : if (callback_get_fn_index (callback_attr) != n)
2630 : callback_attr = NULL;
2631 : }
2632 :
2633 : /* If a callback attribute describing this pointer is found,
2634 : create a callback edge to the pointee function to
2635 : allow for further optimizations. */
2636 43810 : if (callback_attr)
2637 : {
2638 15083 : cgraph_node *kernel_node
2639 15083 : = cgraph_node::get_create (pointee);
2640 15083 : unsigned callback_id = n;
2641 15083 : cgraph_edge *cbe
2642 15083 : = cs->make_callback (kernel_node, callback_id);
2643 15083 : init_callback_edge_summary (cbe, callback_attr);
2644 15083 : callback_edges.safe_push (cbe);
2645 : }
2646 : }
2647 : }
2648 : }
2649 3947337 : else if (!is_gimple_reg_type (TREE_TYPE (arg))
2650 3947337 : && TREE_CODE (arg) == PARM_DECL)
2651 : {
2652 79688 : int index = ipa_get_param_decl_index (info, arg);
2653 :
2654 79688 : gcc_assert (index >=0);
2655 : /* Aggregate passed by value, check for pass-through, otherwise we
2656 : will attempt to fill in aggregate contents later in this
2657 : for cycle. */
2658 79688 : if (parm_preserved_before_stmt_p (fbi, index, call, arg))
2659 : {
2660 64662 : ipa_set_jf_simple_pass_through (jfunc, index, false);
2661 64662 : continue;
2662 : }
2663 : }
2664 3867649 : else if (TREE_CODE (arg) == SSA_NAME)
2665 : {
2666 2539724 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
2667 : {
2668 1009059 : int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
2669 1009059 : if (index >= 0)
2670 : {
2671 999062 : bool agg_p;
2672 999062 : agg_p = parm_ref_data_pass_through_p (fbi, index, call, arg);
2673 999062 : ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
2674 : }
2675 : }
2676 : else
2677 : {
2678 1530665 : gimple *stmt = SSA_NAME_DEF_STMT (arg);
2679 1530665 : if (is_gimple_assign (stmt))
2680 1254748 : compute_complex_assign_jump_func (fbi, info, jfunc,
2681 : call, stmt, arg, param_type);
2682 275917 : else if (gimple_code (stmt) == GIMPLE_PHI)
2683 89247 : compute_complex_ancestor_jump_func (fbi, info, jfunc,
2684 : call,
2685 : as_a <gphi *> (stmt));
2686 : }
2687 : }
2688 :
2689 : /* If ARG is pointer, we cannot use its type to determine the type of aggregate
2690 : passed (because type conversions are ignored in gimple). Usually we can
2691 : safely get type from function declaration, but in case of K&R prototypes or
2692 : variadic functions we can try our luck with type of the pointer passed.
2693 : TODO: Since we look for actual initialization of the memory object, we may better
2694 : work out the type based on the memory stores we find. */
2695 6194733 : if (!param_type)
2696 343899 : param_type = TREE_TYPE (arg);
2697 :
2698 6194733 : if ((jfunc->type != IPA_JF_PASS_THROUGH
2699 1045059 : || !ipa_get_jf_pass_through_agg_preserved (jfunc))
2700 5809506 : && (jfunc->type != IPA_JF_ANCESTOR
2701 164014 : || !ipa_get_jf_ancestor_agg_preserved (jfunc))
2702 11939728 : && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
2703 5384733 : || POINTER_TYPE_P (param_type)))
2704 3357319 : determine_known_aggregate_parts (fbi, call, arg, param_type, jfunc);
2705 6259395 : }
2706 :
2707 2688454 : if (!callback_edges.is_empty ())
2708 : {
2709 : /* For every callback edge, fetch jump functions of arguments
2710 : passed to them and copy them over to their respective summaries.
2711 : This avoids recalculating them for every callback edge, since their
2712 : arguments are just passed through. */
2713 : unsigned j;
2714 30166 : for (j = 0; j < callback_edges.length (); j++)
2715 : {
2716 15083 : cgraph_edge *callback_edge = callback_edges[j];
2717 15083 : ipa_edge_args *cb_summary
2718 15083 : = ipa_edge_args_sum->get_create (callback_edge);
2719 15083 : auto_vec<int> arg_mapping
2720 15083 : = callback_get_arg_mapping (callback_edge, cs);
2721 15083 : unsigned i;
2722 30166 : for (i = 0; i < arg_mapping.length (); i++)
2723 : {
2724 15083 : if (arg_mapping[i] == -1)
2725 0 : continue;
2726 15083 : class ipa_jump_func *src
2727 15083 : = ipa_get_ith_jump_func (args, arg_mapping[i]);
2728 15083 : class ipa_jump_func *dst = ipa_get_ith_jump_func (cb_summary, i);
2729 15083 : ipa_duplicate_jump_function (cs, callback_edge, src, dst);
2730 : }
2731 15083 : }
2732 : }
2733 :
2734 2688454 : if (!useful_context)
2735 4679911 : vec_free (args->polymorphic_call_contexts);
2736 2688454 : }
2737 :
2738 : /* Compute jump functions for all edges - both direct and indirect - outgoing
2739 : from BB. */
2740 :
2741 : static void
2742 11027141 : ipa_compute_jump_functions_for_bb (struct ipa_func_body_info *fbi, basic_block bb)
2743 : {
2744 11027141 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
2745 11027141 : int i;
2746 11027141 : struct cgraph_edge *cs;
2747 :
2748 20631708 : FOR_EACH_VEC_ELT_REVERSE (bi->cg_edges, i, cs)
2749 : {
2750 5549054 : struct cgraph_node *callee = cs->callee;
2751 :
2752 5549054 : if (callee)
2753 : {
2754 5410428 : callee = callee->ultimate_alias_target ();
2755 : /* We do not need to bother analyzing calls to unknown functions
2756 : unless they may become known during lto/whopr. */
2757 3550192 : if (!callee->definition && !flag_lto
2758 5425498 : && !gimple_call_fnspec (cs->call_stmt).known_p ()
2759 8012614 : && !callback_edge_callee_has_attr (cs))
2760 2587116 : continue;
2761 : }
2762 2961938 : ipa_compute_jump_functions_for_edge (fbi, cs);
2763 : }
2764 11027141 : }
2765 :
2766 : /* If REF is a memory access that loads a function pointer (but not a method
2767 : pointer) from a RECORD_TYPE, return true and store the type of the RECORD to
2768 : *REC_TYPE and the byte offset of the field to *FLD_OFFSET. Otherwise return
2769 : false. OHS es the "other hand side" which is used to check type
2770 : compatibility with field in question, when possible. */
2771 :
2772 : static bool
2773 118389 : is_func_ptr_from_record (tree ref, tree *rec_type, unsigned *fld_offset,
2774 : tree ohs)
2775 : {
2776 118401 : if (!POINTER_TYPE_P (TREE_TYPE (ref))
2777 118401 : || TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
2778 : return false;
2779 :
2780 103271 : if (TREE_CODE (ref) == COMPONENT_REF
2781 103271 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
2782 : {
2783 55409 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (ohs)));
2784 55409 : ohs = TREE_TYPE (TREE_TYPE (ohs));
2785 55409 : tree ftype = TREE_TYPE (TREE_OPERAND (ref, 1));
2786 55409 : if (!POINTER_TYPE_P (ftype))
2787 : return false;
2788 55409 : ftype = TREE_TYPE (ftype);
2789 55409 : if (!types_compatible_p (ohs, ftype))
2790 : return false;
2791 :
2792 55280 : tree tree_off = bit_position (TREE_OPERAND (ref, 1));
2793 55280 : if (!tree_fits_shwi_p (tree_off))
2794 : return false;
2795 55280 : HOST_WIDE_INT bit_offset = tree_to_shwi (tree_off);
2796 55280 : if (bit_offset % BITS_PER_UNIT)
2797 : return false;
2798 55280 : HOST_WIDE_INT unit_offset = bit_offset / BITS_PER_UNIT;
2799 55280 : if (unit_offset > UINT_MAX)
2800 : return false;
2801 55280 : *rec_type = TREE_TYPE (TREE_OPERAND (ref, 0));
2802 55280 : *fld_offset = unit_offset;
2803 55280 : return true;
2804 : }
2805 47862 : else if (TREE_CODE (ref) == MEM_REF
2806 4961 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
2807 4961 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0))))
2808 : == RECORD_TYPE)
2809 50012 : && tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
2810 : {
2811 2150 : HOST_WIDE_INT unit_offset = tree_to_shwi (TREE_OPERAND (ref, 1));
2812 2150 : if (unit_offset > UINT_MAX)
2813 : return false;
2814 2150 : *rec_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
2815 2150 : *fld_offset = unit_offset;
2816 2150 : return true;
2817 : }
2818 : return false;
2819 : }
2820 :
2821 : /* If STMT looks like a statement loading a value from a member pointer formal
2822 : parameter, return that parameter and store the offset of the field to
2823 : *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
2824 : might be clobbered). If USE_DELTA, then we look for a use of the delta
2825 : field rather than the pfn. */
2826 :
2827 : static tree
2828 2164 : ipa_get_stmt_member_ptr_load_param (gimple *stmt, bool use_delta,
2829 : HOST_WIDE_INT *offset_p)
2830 : {
2831 2164 : tree rhs, fld, ptr_field, delta_field;
2832 2164 : tree ref_field = NULL_TREE;
2833 2164 : tree ref_offset = NULL_TREE;
2834 :
2835 2164 : if (!gimple_assign_single_p (stmt))
2836 : return NULL_TREE;
2837 :
2838 2164 : rhs = gimple_assign_rhs1 (stmt);
2839 2164 : if (TREE_CODE (rhs) == COMPONENT_REF)
2840 : {
2841 1301 : ref_field = TREE_OPERAND (rhs, 1);
2842 1301 : rhs = TREE_OPERAND (rhs, 0);
2843 : }
2844 :
2845 2164 : if (TREE_CODE (rhs) == MEM_REF)
2846 : {
2847 1463 : ref_offset = TREE_OPERAND (rhs, 1);
2848 1463 : if (ref_field && integer_nonzerop (ref_offset))
2849 : return NULL_TREE;
2850 : }
2851 701 : else if (!ref_field)
2852 : return NULL_TREE;
2853 :
2854 2164 : if (TREE_CODE (rhs) == MEM_REF
2855 1463 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
2856 3627 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (rhs, 0)))
2857 : {
2858 598 : rhs = TREE_OPERAND (rhs, 0);
2859 598 : if (TREE_CODE (SSA_NAME_VAR (rhs)) != PARM_DECL
2860 598 : || !type_like_member_ptr_p (TREE_TYPE (TREE_TYPE (rhs)), &ptr_field,
2861 : &delta_field))
2862 0 : return NULL_TREE;
2863 : }
2864 : else
2865 : {
2866 1566 : if (TREE_CODE (rhs) == MEM_REF
2867 1566 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR)
2868 0 : rhs = TREE_OPERAND (TREE_OPERAND (rhs, 0), 0);
2869 1566 : if (TREE_CODE (rhs) != PARM_DECL
2870 1566 : || !type_like_member_ptr_p (TREE_TYPE (rhs), &ptr_field,
2871 : &delta_field))
2872 1288 : return NULL_TREE;
2873 : }
2874 :
2875 876 : if (use_delta)
2876 0 : fld = delta_field;
2877 : else
2878 876 : fld = ptr_field;
2879 :
2880 876 : if (ref_field)
2881 : {
2882 876 : if (ref_field != fld)
2883 : return NULL_TREE;
2884 : }
2885 0 : else if (!tree_int_cst_equal (byte_position (fld), ref_offset))
2886 : return NULL_TREE;
2887 :
2888 876 : if (offset_p)
2889 438 : *offset_p = int_bit_position (fld);
2890 : return rhs;
2891 : }
2892 :
2893 : /* Returns true iff T is an SSA_NAME defined by a statement. */
2894 :
2895 : static bool
2896 3040 : ipa_is_ssa_with_stmt_def (tree t)
2897 : {
2898 3040 : if (TREE_CODE (t) == SSA_NAME
2899 3040 : && !SSA_NAME_IS_DEFAULT_DEF (t))
2900 : return true;
2901 : else
2902 0 : return false;
2903 : }
2904 :
2905 : /* Analyze the CALL and examine uses of formal parameters of the caller NODE
2906 : (described by INFO). PARMS_AINFO is a pointer to a vector containing
2907 : intermediate information about each formal parameter. Currently it checks
2908 : whether the call calls a pointer that is a formal parameter and if so, the
2909 : parameter is marked with the called flag and an indirect call graph edge
2910 : describing the call is created. This is very simple for ordinary pointers
2911 : represented in SSA but not-so-nice when it comes to member pointers. The
2912 : ugly part of this function does nothing more than trying to match the
2913 : pattern of such a call. Look up the documentation of macro
2914 : TARGET_PTRMEMFUNC_VBIT_LOCATION for details. An example of such a pattern
2915 : is the gimple dump below, the call is on the last line:
2916 :
2917 : <bb 2>:
2918 : f$__delta_5 = f.__delta;
2919 : f$__pfn_24 = f.__pfn;
2920 :
2921 : or
2922 : <bb 2>:
2923 : f$__delta_5 = MEM[(struct *)&f];
2924 : f$__pfn_24 = MEM[(struct *)&f + 4B];
2925 :
2926 : and a few lines below:
2927 :
2928 : <bb 5>
2929 : D.2496_3 = (int) f$__pfn_24;
2930 : D.2497_4 = D.2496_3 & 1;
2931 : if (D.2497_4 != 0)
2932 : goto <bb 3>;
2933 : else
2934 : goto <bb 4>;
2935 :
2936 : <bb 6>:
2937 : D.2500_7 = (unsigned int) f$__delta_5;
2938 : D.2501_8 = &S + D.2500_7;
2939 : D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
2940 : D.2503_10 = *D.2502_9;
2941 : D.2504_12 = f$__pfn_24 + -1;
2942 : D.2505_13 = (unsigned int) D.2504_12;
2943 : D.2506_14 = D.2503_10 + D.2505_13;
2944 : D.2507_15 = *D.2506_14;
2945 : iftmp.11_16 = (String:: *) D.2507_15;
2946 :
2947 : <bb 7>:
2948 : # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
2949 : D.2500_19 = (unsigned int) f$__delta_5;
2950 : D.2508_20 = &S + D.2500_19;
2951 : D.2493_21 = iftmp.11_1 (D.2508_20, 4);
2952 :
2953 : Such patterns are results of simple calls to a member pointer:
2954 :
2955 : int doprinting (int (MyString::* f)(int) const)
2956 : {
2957 : MyString S ("somestring");
2958 :
2959 : return (S.*f)(4);
2960 : }
2961 :
2962 : Moreover, the function also looks for called pointers loaded from aggregates
2963 : passed by value or reference. */
2964 :
2965 : static void
2966 114431 : ipa_analyze_indirect_call_uses (struct ipa_func_body_info *fbi, gcall *call,
2967 : tree target)
2968 : {
2969 114431 : class ipa_node_params *info = fbi->info;
2970 114431 : HOST_WIDE_INT offset;
2971 114431 : bool by_ref;
2972 :
2973 114431 : if (SSA_NAME_IS_DEFAULT_DEF (target))
2974 : {
2975 3789 : tree var = SSA_NAME_VAR (target);
2976 3789 : int index = ipa_get_param_decl_index (info, var);
2977 3789 : if (index >= 0)
2978 : {
2979 3787 : cgraph_edge *cs = fbi->node->get_edge (call);
2980 3787 : cgraph_simple_indirect_info *sii =
2981 3787 : as_a <cgraph_simple_indirect_info *> (cs->indirect_info);
2982 3787 : sii->param_index = index;
2983 3787 : gcc_assert (!sii->agg_contents && !sii->member_ptr);
2984 3787 : ipa_set_param_used_by_indirect_call (info, index, true);
2985 : }
2986 3789 : return;
2987 : }
2988 :
2989 110642 : int index;
2990 110642 : gimple *def = SSA_NAME_DEF_STMT (target);
2991 110642 : bool guaranteed_unmodified;
2992 110642 : if (gimple_assign_single_p (def))
2993 : {
2994 88062 : cgraph_edge *cs = fbi->node->get_edge (call);
2995 88062 : cgraph_simple_indirect_info *sii =
2996 88062 : as_a <cgraph_simple_indirect_info *> (cs->indirect_info);
2997 88062 : tree rectype;
2998 88062 : unsigned fldoff;
2999 88062 : if (is_func_ptr_from_record (gimple_assign_rhs1 (def), &rectype, &fldoff,
3000 : target))
3001 : {
3002 46260 : sii->fnptr_loaded_from_record = 1;
3003 46260 : sii->fld_offset = fldoff;
3004 46260 : sii->rec_type = rectype;
3005 : }
3006 88062 : if (ipa_load_from_parm_agg (fbi, info->descriptors, def,
3007 : gimple_assign_rhs1 (def), &index, &offset,
3008 : NULL, &by_ref, &guaranteed_unmodified))
3009 : {
3010 2186 : sii->param_index = index;
3011 2186 : sii->offset = offset;
3012 2186 : sii->agg_contents = 1;
3013 2186 : sii->by_ref = by_ref;
3014 2186 : sii->guaranteed_unmodified = guaranteed_unmodified;
3015 2186 : ipa_set_param_used_by_indirect_call (info, index, true);
3016 2186 : return;
3017 : }
3018 : }
3019 :
3020 : /* Now we need to try to match the complex pattern of calling a member
3021 : pointer. */
3022 108456 : if (gimple_code (def) != GIMPLE_PHI
3023 1143 : || gimple_phi_num_args (def) != 2
3024 1135 : || !POINTER_TYPE_P (TREE_TYPE (target))
3025 109591 : || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
3026 : return;
3027 :
3028 : /* First, we need to check whether one of these is a load from a member
3029 : pointer that is a parameter to this function. */
3030 863 : tree n1 = PHI_ARG_DEF (def, 0);
3031 863 : tree n2 = PHI_ARG_DEF (def, 1);
3032 1726 : if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
3033 : return;
3034 863 : gimple *d1 = SSA_NAME_DEF_STMT (n1);
3035 863 : gimple *d2 = SSA_NAME_DEF_STMT (n2);
3036 :
3037 863 : tree rec;
3038 863 : basic_block bb, virt_bb;
3039 863 : basic_block join = gimple_bb (def);
3040 863 : if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
3041 : {
3042 0 : if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
3043 : return;
3044 :
3045 0 : bb = EDGE_PRED (join, 0)->src;
3046 0 : virt_bb = gimple_bb (d2);
3047 : }
3048 863 : else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
3049 : {
3050 438 : bb = EDGE_PRED (join, 1)->src;
3051 438 : virt_bb = gimple_bb (d1);
3052 : }
3053 : else
3054 : return;
3055 :
3056 : /* Second, we need to check that the basic blocks are laid out in the way
3057 : corresponding to the pattern. */
3058 :
3059 876 : if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
3060 876 : || single_succ (virt_bb) != join)
3061 : return;
3062 :
3063 :
3064 438 : if (single_pred (virt_bb) != bb)
3065 : {
3066 : /* In cases when the distinction between a normal and a virtual
3067 : function is encoded in the delta field, the load of the
3068 : actual non-virtual function pointer can be in its own BB. */
3069 :
3070 0 : if (!single_pred_p (bb) || !single_succ_p (bb))
3071 : return;
3072 0 : bb = single_pred (bb);
3073 0 : if (bb != single_pred (virt_bb))
3074 : return;
3075 : }
3076 :
3077 : /* Third, let's see that the branching is done depending on the least
3078 : significant bit of the pfn. */
3079 :
3080 876 : gcond *branch = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
3081 438 : if (!branch)
3082 : return;
3083 :
3084 438 : if ((gimple_cond_code (branch) != NE_EXPR
3085 0 : && gimple_cond_code (branch) != EQ_EXPR)
3086 438 : || !integer_zerop (gimple_cond_rhs (branch)))
3087 0 : return;
3088 :
3089 438 : tree cond = gimple_cond_lhs (branch);
3090 438 : if (!ipa_is_ssa_with_stmt_def (cond))
3091 : return;
3092 :
3093 438 : def = SSA_NAME_DEF_STMT (cond);
3094 438 : if (!is_gimple_assign (def)
3095 438 : || gimple_assign_rhs_code (def) != BIT_AND_EXPR
3096 876 : || !integer_onep (gimple_assign_rhs2 (def)))
3097 0 : return;
3098 :
3099 438 : cond = gimple_assign_rhs1 (def);
3100 438 : if (!ipa_is_ssa_with_stmt_def (cond))
3101 : return;
3102 :
3103 438 : def = SSA_NAME_DEF_STMT (cond);
3104 :
3105 438 : if (is_gimple_assign (def)
3106 438 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
3107 : {
3108 438 : cond = gimple_assign_rhs1 (def);
3109 438 : if (!ipa_is_ssa_with_stmt_def (cond))
3110 : return;
3111 438 : def = SSA_NAME_DEF_STMT (cond);
3112 : }
3113 :
3114 438 : tree rec2;
3115 438 : rec2 = ipa_get_stmt_member_ptr_load_param (def,
3116 : (TARGET_PTRMEMFUNC_VBIT_LOCATION
3117 : == ptrmemfunc_vbit_in_delta),
3118 : NULL);
3119 438 : if (rec != rec2)
3120 : return;
3121 :
3122 438 : if (TREE_CODE (rec) == SSA_NAME)
3123 : {
3124 299 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (rec));
3125 299 : if (index < 0
3126 299 : || !parm_ref_data_preserved_p (fbi, index, call,
3127 : gimple_assign_rhs1 (def)))
3128 4 : return;
3129 295 : by_ref = true;
3130 : }
3131 : else
3132 : {
3133 139 : index = ipa_get_param_decl_index (info, rec);
3134 139 : if (index < 0
3135 139 : || !parm_preserved_before_stmt_p (fbi, index, call, rec))
3136 0 : return;
3137 139 : by_ref = false;
3138 : }
3139 :
3140 434 : cgraph_edge *cs = fbi->node->get_edge (call);
3141 434 : cgraph_simple_indirect_info *sii =
3142 434 : as_a <cgraph_simple_indirect_info *> (cs->indirect_info);
3143 434 : sii->param_index = index;
3144 434 : sii->offset = offset;
3145 434 : sii->agg_contents = 1;
3146 434 : sii->member_ptr = 1;
3147 434 : sii->by_ref = by_ref;
3148 434 : sii->guaranteed_unmodified = 1;
3149 434 : ipa_set_param_used_by_indirect_call (info, index, true);
3150 434 : return;
3151 : }
3152 :
3153 : /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
3154 : object referenced in the expression is a formal parameter of the caller
3155 : FBI->node (described by FBI->info), create a call note for the
3156 : statement. */
3157 :
3158 : static void
3159 24123 : ipa_analyze_virtual_call_uses (struct ipa_func_body_info *fbi,
3160 : gcall *call, tree target)
3161 : {
3162 24123 : tree obj = OBJ_TYPE_REF_OBJECT (target);
3163 24123 : int index;
3164 24123 : HOST_WIDE_INT anc_offset;
3165 :
3166 24123 : if (!flag_devirtualize)
3167 14651 : return;
3168 :
3169 23853 : if (TREE_CODE (obj) != SSA_NAME)
3170 : return;
3171 :
3172 23494 : class ipa_node_params *info = fbi->info;
3173 23494 : if (SSA_NAME_IS_DEFAULT_DEF (obj))
3174 : {
3175 8738 : if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
3176 : return;
3177 :
3178 8738 : anc_offset = 0;
3179 8738 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
3180 8738 : gcc_assert (index >= 0);
3181 8738 : if (detect_type_change_ssa (fbi, obj, obj_type_ref_class (target),
3182 : call))
3183 : return;
3184 : }
3185 : else
3186 : {
3187 14756 : gimple *stmt = SSA_NAME_DEF_STMT (obj);
3188 14756 : tree expr;
3189 :
3190 14756 : expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
3191 14756 : if (!expr)
3192 : return;
3193 734 : index = ipa_get_param_decl_index (info,
3194 734 : SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
3195 734 : gcc_assert (index >= 0);
3196 734 : if (detect_type_change (fbi, obj, expr, obj_type_ref_class (target),
3197 : call, anc_offset))
3198 : return;
3199 : }
3200 :
3201 9472 : cgraph_edge *cs = fbi->node->get_edge (call);
3202 9472 : cgraph_polymorphic_indirect_info *pii =
3203 9472 : as_a <cgraph_polymorphic_indirect_info *> (cs->indirect_info);
3204 9472 : pii->param_index = index;
3205 9472 : pii->offset = anc_offset;
3206 9472 : gcc_assert (pii->otr_token == tree_to_shwi (OBJ_TYPE_REF_TOKEN (target)));
3207 9472 : gcc_assert (pii->otr_type = obj_type_ref_class (target));
3208 9472 : ipa_set_param_used_by_indirect_call (info, index, true);
3209 9472 : ipa_set_param_used_by_polymorphic_call (info, index, true);
3210 : }
3211 :
3212 : /* Analyze a call statement CALL whether and how it utilizes formal parameters
3213 : of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
3214 : containing intermediate information about each formal parameter. */
3215 :
3216 : static void
3217 5771129 : ipa_analyze_call_uses (struct ipa_func_body_info *fbi, gcall *call)
3218 : {
3219 5771129 : tree target = gimple_call_fn (call);
3220 :
3221 5771129 : if (!target
3222 5771129 : || (TREE_CODE (target) != SSA_NAME
3223 5434623 : && !virtual_method_call_p (target)))
3224 5632573 : return;
3225 :
3226 138556 : struct cgraph_edge *cs = fbi->node->get_edge (call);
3227 : /* If we previously turned the call into a direct call, there is
3228 : no need to analyze. */
3229 138556 : if (cs && !cs->indirect_unknown_callee)
3230 : return;
3231 :
3232 138554 : cgraph_polymorphic_indirect_info *pii;
3233 138554 : if (flag_devirtualize
3234 138554 : && (pii
3235 134299 : = dyn_cast <cgraph_polymorphic_indirect_info *> (cs->indirect_info)))
3236 : {
3237 23853 : tree instance;
3238 23853 : tree target = gimple_call_fn (call);
3239 23853 : ipa_polymorphic_call_context context (current_function_decl,
3240 23853 : target, call, &instance);
3241 :
3242 23853 : gcc_checking_assert (pii->otr_type == obj_type_ref_class (target));
3243 23853 : gcc_checking_assert (pii->otr_token
3244 : == tree_to_shwi (OBJ_TYPE_REF_TOKEN (target)));
3245 :
3246 23853 : pii->vptr_changed
3247 47706 : = !context.get_dynamic_type (instance,
3248 23853 : OBJ_TYPE_REF_OBJECT (target),
3249 : obj_type_ref_class (target), call,
3250 : &fbi->aa_walk_budget);
3251 23853 : pii->context = context;
3252 : }
3253 :
3254 138554 : if (TREE_CODE (target) == SSA_NAME)
3255 114431 : ipa_analyze_indirect_call_uses (fbi, call, target);
3256 24123 : else if (virtual_method_call_p (target))
3257 24123 : ipa_analyze_virtual_call_uses (fbi, call, target);
3258 : }
3259 :
3260 : /* Store that that there was a store of FN to a record of type REC_TYPE and
3261 : FLD_OFFSET. */
3262 :
3263 : static void
3264 63433 : note_fnptr_in_record (tree rec_type, unsigned fld_offset, tree fn)
3265 : {
3266 63433 : gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3267 63433 : gcc_assert (TREE_CODE (rec_type) == RECORD_TYPE);
3268 63433 : if (!noted_fnptrs_in_records)
3269 6772 : noted_fnptrs_in_records = hash_table<noted_fnptr_hasher>::create_ggc (37);
3270 :
3271 63433 : noted_fnptr_store repr;
3272 63433 : repr.rec_type = rec_type;
3273 63433 : repr.fld_offset = fld_offset;
3274 :
3275 63433 : noted_fnptr_store **slot = noted_fnptrs_in_records->find_slot (&repr,
3276 : NO_INSERT);
3277 63433 : if (slot)
3278 : {
3279 7570 : if ((*slot)->fn && (*slot)->fn != fn)
3280 803 : (*slot)->fn = nullptr;
3281 7570 : return;
3282 : }
3283 :
3284 55863 : slot = noted_fnptrs_in_records->find_slot (&repr, INSERT);
3285 55863 : *slot = ggc_cleared_alloc<noted_fnptr_store> ();
3286 55863 : (*slot)->rec_type = rec_type;
3287 55863 : (*slot)->fn = fn;
3288 55863 : (*slot)->fld_offset = fld_offset;
3289 :
3290 55863 : return;
3291 : }
3292 :
3293 : /* Dump contents of noted_fnptrs_in_records to F in humad readable form. */
3294 :
3295 : void DEBUG_FUNCTION
3296 41 : ipa_dump_noted_record_fnptrs (FILE *f)
3297 : {
3298 41 : if (!noted_fnptrs_in_records)
3299 : {
3300 38 : fprintf (f, "No noted function pointers stored in records.\n\n");
3301 38 : return;
3302 : }
3303 :
3304 3 : fprintf (f, "Noted function pointers stored in records:\n");
3305 7 : for (auto iter = noted_fnptrs_in_records->begin ();
3306 7 : iter != noted_fnptrs_in_records->end ();
3307 4 : ++iter)
3308 : {
3309 4 : const noted_fnptr_store *elem = *iter;
3310 4 : fprintf (f, " Type:");
3311 4 : print_generic_expr (f, elem->rec_type);
3312 4 : fprintf (f, ", offset %ul, function: ", elem->fld_offset);
3313 4 : print_generic_expr (f, elem->fn);
3314 4 : fprintf (f, "\n");
3315 : }
3316 3 : fprintf (f, "\n");
3317 : }
3318 :
3319 : /* Dump contents of noted_fnptrs_in_records to stderr in humad readable
3320 : form. */
3321 :
3322 : void DEBUG_FUNCTION
3323 0 : ipa_debug_noted_record_fnptrs (void)
3324 : {
3325 0 : ipa_dump_noted_record_fnptrs (stderr);
3326 0 : }
3327 :
3328 :
3329 : /* If we have noticed a single function pointer stored into a record of type
3330 : REC_TYPE at the given FLD_OFFSET (measured in bytes), return its
3331 : declaration. Otherwise return NULL_TREE. */
3332 :
3333 : tree
3334 37252 : ipa_single_noted_fnptr_in_record (tree rec_type, unsigned fld_offset)
3335 : {
3336 37252 : if (!noted_fnptrs_in_records)
3337 : return NULL_TREE;
3338 :
3339 35152 : noted_fnptr_store repr;
3340 35152 : repr.rec_type = rec_type;
3341 35152 : repr.fld_offset = fld_offset;
3342 :
3343 35152 : noted_fnptr_store **slot = noted_fnptrs_in_records->find_slot (&repr,
3344 : NO_INSERT);
3345 35152 : if (!slot)
3346 : return NULL_TREE;
3347 3296 : return (*slot)->fn;
3348 : }
3349 :
3350 : /* Free the hash table storing the information about function pointers stored
3351 : to a particular position in record typed strucutres. */
3352 :
3353 : void
3354 128979 : ipa_free_noted_fnptr_calls ()
3355 : {
3356 128979 : if (noted_fnptrs_in_records)
3357 : {
3358 6409 : noted_fnptrs_in_records->empty ();
3359 6409 : noted_fnptrs_in_records = nullptr;
3360 : }
3361 128979 : }
3362 :
3363 : /* Analyze the call statement STMT with respect to formal parameters (described
3364 : in INFO) of caller given by FBI->NODE. Also note any stores of function
3365 : pointers to record typed memory. */
3366 :
3367 : static void
3368 31505685 : ipa_analyze_stmt_uses (struct ipa_func_body_info *fbi, gimple *stmt)
3369 : {
3370 31505685 : if (is_gimple_call (stmt))
3371 5771129 : ipa_analyze_call_uses (fbi, as_a <gcall *> (stmt));
3372 25734556 : else if (gimple_assign_single_p (stmt)
3373 13546925 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
3374 26973855 : && (TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (stmt), 0))
3375 : == FUNCTION_DECL))
3376 : {
3377 30327 : tree rec_type;
3378 30327 : unsigned fld_offset;
3379 30327 : if (is_func_ptr_from_record (gimple_assign_lhs (stmt), &rec_type,
3380 : &fld_offset, gimple_assign_rhs1 (stmt)))
3381 11170 : note_fnptr_in_record (rec_type, fld_offset,
3382 11170 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 0));
3383 : }
3384 31505685 : }
3385 :
3386 : /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
3387 : If OP is a parameter declaration, mark it as used in the info structure
3388 : passed in DATA. */
3389 :
3390 : static bool
3391 19690757 : visit_ref_for_mod_analysis (gimple *, tree op, tree, void *data)
3392 : {
3393 19690757 : class ipa_node_params *info = (class ipa_node_params *) data;
3394 :
3395 19690757 : op = get_base_address (op);
3396 19690757 : if (op
3397 19690757 : && TREE_CODE (op) == PARM_DECL)
3398 : {
3399 455437 : int index = ipa_get_param_decl_index (info, op);
3400 455437 : gcc_assert (index >= 0);
3401 455437 : ipa_set_param_used (info, index, true);
3402 : }
3403 :
3404 19690757 : return false;
3405 : }
3406 :
3407 : /* Scan the statements in BB and inspect the uses of formal parameters. Store
3408 : the findings in various structures of the associated ipa_node_params
3409 : structure, such as parameter flags, notes etc. FBI holds various data about
3410 : the function being analyzed. */
3411 :
3412 : static void
3413 11027141 : ipa_analyze_params_uses_in_bb (struct ipa_func_body_info *fbi, basic_block bb)
3414 : {
3415 11027141 : gimple_stmt_iterator gsi;
3416 77284162 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3417 : {
3418 55229880 : gimple *stmt = gsi_stmt (gsi);
3419 :
3420 55229880 : if (is_gimple_debug (stmt))
3421 23724195 : continue;
3422 :
3423 31505685 : ipa_analyze_stmt_uses (fbi, stmt);
3424 31505685 : walk_stmt_load_store_addr_ops (stmt, fbi->info,
3425 : visit_ref_for_mod_analysis,
3426 : visit_ref_for_mod_analysis,
3427 : visit_ref_for_mod_analysis);
3428 : }
3429 13998871 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3430 2971730 : walk_stmt_load_store_addr_ops (gsi_stmt (gsi), fbi->info,
3431 : visit_ref_for_mod_analysis,
3432 : visit_ref_for_mod_analysis,
3433 : visit_ref_for_mod_analysis);
3434 11027141 : }
3435 :
3436 : /* Return true EXPR is a load from a dereference of SSA_NAME NAME. */
3437 :
3438 : static bool
3439 4180272 : load_from_dereferenced_name (tree expr, tree name)
3440 : {
3441 4180272 : tree base = get_base_address (expr);
3442 4180272 : return (TREE_CODE (base) == MEM_REF
3443 4180272 : && TREE_OPERAND (base, 0) == name);
3444 : }
3445 :
3446 : /* Calculate controlled uses of parameters of NODE. */
3447 :
3448 : static void
3449 1365108 : ipa_analyze_controlled_uses (struct cgraph_node *node)
3450 : {
3451 1365108 : ipa_node_params *info = ipa_node_params_sum->get (node);
3452 :
3453 7455643 : for (int i = 0; i < ipa_get_param_count (info); i++)
3454 : {
3455 2489900 : tree parm = ipa_get_param (info, i);
3456 2489900 : int call_uses = 0;
3457 2489900 : bool load_dereferenced = false;
3458 :
3459 : /* For SSA regs see if parameter is used. For non-SSA we compute
3460 : the flag during modification analysis. */
3461 2489900 : if (is_gimple_reg (parm))
3462 : {
3463 2264458 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
3464 : parm);
3465 2264458 : if (ddef && !has_zero_uses (ddef))
3466 : {
3467 1983285 : imm_use_iterator imm_iter;
3468 1983285 : gimple *stmt;
3469 :
3470 1983285 : ipa_set_param_used (info, i, true);
3471 6797693 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, ddef)
3472 : {
3473 3965066 : if (is_gimple_debug (stmt))
3474 760060 : continue;
3475 :
3476 3205006 : int all_stmt_uses = 0;
3477 3205006 : use_operand_p use_p;
3478 6441122 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3479 3236116 : all_stmt_uses++;
3480 :
3481 3205006 : if (is_gimple_call (stmt))
3482 : {
3483 1199009 : if (gimple_call_internal_p (stmt))
3484 : {
3485 : call_uses = IPA_UNDESCRIBED_USE;
3486 : break;
3487 : }
3488 1144211 : int recognized_stmt_uses;
3489 1144211 : if (gimple_call_fn (stmt) == ddef)
3490 : recognized_stmt_uses = 1;
3491 : else
3492 1140489 : recognized_stmt_uses = 0;
3493 1144211 : unsigned arg_count = gimple_call_num_args (stmt);
3494 5077493 : for (unsigned i = 0; i < arg_count; i++)
3495 : {
3496 3933282 : tree arg = gimple_call_arg (stmt, i);
3497 3933282 : if (arg == ddef)
3498 1131040 : recognized_stmt_uses++;
3499 2802242 : else if (load_from_dereferenced_name (arg, ddef))
3500 : {
3501 15752 : load_dereferenced = true;
3502 15752 : recognized_stmt_uses++;
3503 : }
3504 : }
3505 :
3506 1144211 : if (recognized_stmt_uses != all_stmt_uses)
3507 : {
3508 : call_uses = IPA_UNDESCRIBED_USE;
3509 : break;
3510 : }
3511 1136377 : if (call_uses >= 0)
3512 1136377 : call_uses += all_stmt_uses;
3513 : }
3514 2005997 : else if (gimple_assign_single_p (stmt))
3515 : {
3516 1378883 : tree rhs = gimple_assign_rhs1 (stmt);
3517 1378883 : if (all_stmt_uses != 1
3518 1378883 : || !load_from_dereferenced_name (rhs, ddef))
3519 : {
3520 : call_uses = IPA_UNDESCRIBED_USE;
3521 : break;
3522 : }
3523 : load_dereferenced = true;
3524 : }
3525 : else
3526 : {
3527 : call_uses = IPA_UNDESCRIBED_USE;
3528 : break;
3529 : }
3530 1983285 : }
3531 : }
3532 : else
3533 : call_uses = 0;
3534 : }
3535 : else
3536 : call_uses = IPA_UNDESCRIBED_USE;
3537 2489900 : ipa_set_controlled_uses (info, i, call_uses);
3538 2489900 : ipa_set_param_load_dereferenced (info, i, load_dereferenced);
3539 : }
3540 1365108 : }
3541 :
3542 : /* Free stuff in BI. */
3543 :
3544 : static void
3545 63462425 : free_ipa_bb_info (struct ipa_bb_info *bi)
3546 : {
3547 0 : bi->cg_edges.release ();
3548 63462425 : bi->param_aa_statuses.release ();
3549 0 : }
3550 :
3551 : /* Dominator walker driving the analysis. */
3552 :
3553 2730216 : class analysis_dom_walker : public dom_walker
3554 : {
3555 : public:
3556 1365108 : analysis_dom_walker (struct ipa_func_body_info *fbi)
3557 2730216 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi) {}
3558 :
3559 : edge before_dom_children (basic_block) final override;
3560 :
3561 : private:
3562 : struct ipa_func_body_info *m_fbi;
3563 : };
3564 :
3565 : edge
3566 11027141 : analysis_dom_walker::before_dom_children (basic_block bb)
3567 : {
3568 11027141 : ipa_analyze_params_uses_in_bb (m_fbi, bb);
3569 11027141 : ipa_compute_jump_functions_for_bb (m_fbi, bb);
3570 11027141 : return NULL;
3571 : }
3572 :
3573 : /* Release body info FBI. */
3574 :
3575 : void
3576 8564571 : ipa_release_body_info (struct ipa_func_body_info *fbi)
3577 : {
3578 8564571 : int i;
3579 8564571 : struct ipa_bb_info *bi;
3580 :
3581 72003389 : FOR_EACH_VEC_ELT (fbi->bb_infos, i, bi)
3582 126877636 : free_ipa_bb_info (bi);
3583 8564571 : fbi->bb_infos.release ();
3584 8564571 : }
3585 :
3586 : /* Initialize the array describing properties of formal parameters
3587 : of NODE, analyze their uses and compute jump functions associated
3588 : with actual arguments of calls from within NODE. */
3589 :
3590 : void
3591 2677112 : ipa_analyze_node (struct cgraph_node *node)
3592 : {
3593 2677112 : struct ipa_func_body_info fbi;
3594 2677112 : class ipa_node_params *info;
3595 :
3596 2677112 : ipa_check_create_node_params ();
3597 2677112 : ipa_check_create_edge_args ();
3598 2677112 : info = ipa_node_params_sum->get_create (node);
3599 :
3600 2677112 : if (info->analysis_done)
3601 1312004 : return;
3602 1366112 : info->analysis_done = 1;
3603 :
3604 1366112 : if (ipa_func_spec_opts_forbid_analysis_p (node)
3605 1366112 : || (count_formal_params (node->decl)
3606 : >= (1 << IPA_PROP_ARG_INDEX_LIMIT_BITS)))
3607 : {
3608 1312004 : gcc_assert (!ipa_get_param_count (info));
3609 : return;
3610 : }
3611 :
3612 1365108 : struct function *func = DECL_STRUCT_FUNCTION (node->decl);
3613 1365108 : push_cfun (func);
3614 1365108 : calculate_dominance_info (CDI_DOMINATORS);
3615 1365108 : ipa_initialize_node_params (node);
3616 1365108 : ipa_analyze_controlled_uses (node);
3617 :
3618 1365108 : fbi.node = node;
3619 1365108 : fbi.info = info;
3620 1365108 : fbi.bb_infos = vNULL;
3621 1365108 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
3622 1365108 : fbi.param_count = ipa_get_param_count (info);
3623 1365108 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
3624 :
3625 6775536 : for (struct cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
3626 : {
3627 5410428 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3628 5410428 : bi->cg_edges.safe_push (cs);
3629 : }
3630 :
3631 1503734 : for (struct cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
3632 : {
3633 138626 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3634 138626 : bi->cg_edges.safe_push (cs);
3635 : }
3636 :
3637 1365108 : enable_ranger (cfun, false);
3638 1365108 : analysis_dom_walker (&fbi).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3639 1365108 : disable_ranger (cfun);
3640 :
3641 1365108 : ipa_release_body_info (&fbi);
3642 1365108 : free_dominance_info (CDI_DOMINATORS);
3643 1365108 : pop_cfun ();
3644 : }
3645 :
3646 : /* Analyze NODE and note any function pointers in record-typed static
3647 : initializers.
3648 :
3649 : TODO: The current implementation does not traverse the initializers to scan
3650 : records nested inside other types. It should catch the most basic way of
3651 : writing "virtual functions" in C but can be extended, of course.
3652 : */
3653 :
3654 : void
3655 1673812 : ipa_analyze_var_static_initializer (varpool_node *node)
3656 : {
3657 1673812 : tree decl = node->decl;
3658 1673812 : tree rec_type = TREE_TYPE (decl);
3659 1673812 : if (TREE_CODE (rec_type) != RECORD_TYPE
3660 1673812 : || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
3661 : return;
3662 :
3663 : unsigned ix;
3664 : tree index, val;
3665 3844439 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), ix, index,
3666 : val)
3667 : {
3668 5362965 : if (TREE_CODE (val) != ADDR_EXPR
3669 1005543 : || TREE_CODE (TREE_OPERAND (val, 0)) != FUNCTION_DECL
3670 : /* ObjC can produce constructor elements with NULL indices. */
3671 2758875 : || !index)
3672 2655685 : continue;
3673 51595 : HOST_WIDE_INT elt_offset = int_bit_position (index);
3674 51595 : if ((elt_offset % BITS_PER_UNIT) != 0)
3675 0 : continue;
3676 51595 : elt_offset = elt_offset / BITS_PER_UNIT;
3677 51595 : if (elt_offset > UINT_MAX)
3678 0 : continue;
3679 51595 : note_fnptr_in_record (rec_type, elt_offset, TREE_OPERAND (val, 0));
3680 : }
3681 : }
3682 :
3683 : /* Update the jump functions associated with call graph edge E when the call
3684 : graph edge CS is being inlined, assuming that E->caller is already (possibly
3685 : indirectly) inlined into CS->callee and that E has not been inlined. */
3686 :
3687 : static void
3688 2680164 : update_jump_functions_after_inlining (struct cgraph_edge *cs,
3689 : struct cgraph_edge *e)
3690 : {
3691 2680164 : ipa_edge_args *top = ipa_edge_args_sum->get (cs);
3692 2680164 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
3693 2680164 : if (!args)
3694 : return;
3695 1524823 : ipa_node_params *old_inline_root_info = ipa_node_params_sum->get (cs->callee);
3696 1524823 : ipa_node_params *new_inline_root_info
3697 1524823 : = ipa_node_params_sum->get (cs->caller->inlined_to
3698 : ? cs->caller->inlined_to : cs->caller);
3699 1524823 : int count = ipa_get_cs_argument_count (args);
3700 1524823 : int i;
3701 :
3702 4517200 : for (i = 0; i < count; i++)
3703 : {
3704 2992377 : struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
3705 2992377 : class ipa_polymorphic_call_context *dst_ctx
3706 2992377 : = ipa_get_ith_polymorhic_call_context (args, i);
3707 :
3708 2992377 : if (dst->agg.items)
3709 : {
3710 : struct ipa_agg_jf_item *item;
3711 : int j;
3712 :
3713 253806 : FOR_EACH_VEC_ELT (*dst->agg.items, j, item)
3714 : {
3715 173307 : int dst_fid;
3716 173307 : struct ipa_jump_func *src;
3717 :
3718 309280 : if (item->jftype != IPA_JF_PASS_THROUGH
3719 173307 : && item->jftype != IPA_JF_LOAD_AGG)
3720 135973 : continue;
3721 :
3722 37334 : dst_fid = item->value.pass_through.formal_id;
3723 74665 : if (!top || dst_fid >= ipa_get_cs_argument_count (top))
3724 : {
3725 3 : item->jftype = IPA_JF_UNKNOWN;
3726 3 : continue;
3727 : }
3728 :
3729 37331 : item->value.pass_through.formal_id = -1;
3730 37331 : src = ipa_get_ith_jump_func (top, dst_fid);
3731 37331 : if (src->type == IPA_JF_CONST)
3732 : {
3733 2373 : if (item->jftype == IPA_JF_PASS_THROUGH
3734 2106 : && item->value.pass_through.operation == NOP_EXPR)
3735 : {
3736 2040 : item->jftype = IPA_JF_CONST;
3737 2040 : item->value.constant = src->value.constant.value;
3738 2040 : continue;
3739 : }
3740 : }
3741 34958 : else if (src->type == IPA_JF_PASS_THROUGH
3742 5284 : && src->value.pass_through.operation == NOP_EXPR)
3743 : {
3744 5136 : if (item->jftype == IPA_JF_PASS_THROUGH
3745 3436 : || !item->value.load_agg.by_ref
3746 2231 : || src->value.pass_through.agg_preserved)
3747 3701 : item->value.pass_through.formal_id
3748 3701 : = src->value.pass_through.formal_id;
3749 : }
3750 29822 : else if (src->type == IPA_JF_ANCESTOR)
3751 : {
3752 4535 : if (item->jftype == IPA_JF_PASS_THROUGH)
3753 : {
3754 992 : if (!src->value.ancestor.offset)
3755 683 : item->value.pass_through.formal_id
3756 683 : = src->value.ancestor.formal_id;
3757 : }
3758 3543 : else if (src->value.ancestor.agg_preserved)
3759 : {
3760 1450 : gcc_checking_assert (item->value.load_agg.by_ref);
3761 :
3762 1450 : item->value.pass_through.formal_id
3763 1450 : = src->value.ancestor.formal_id;
3764 1450 : item->value.load_agg.offset
3765 1450 : += src->value.ancestor.offset;
3766 : }
3767 : }
3768 :
3769 35291 : if (item->value.pass_through.formal_id < 0)
3770 29457 : item->jftype = IPA_JF_UNKNOWN;
3771 : }
3772 : }
3773 :
3774 2992377 : if (!top)
3775 : {
3776 13242 : ipa_set_jf_unknown (dst);
3777 13242 : continue;
3778 : }
3779 :
3780 2979135 : if (dst->type == IPA_JF_ANCESTOR)
3781 : {
3782 136382 : struct ipa_jump_func *src;
3783 136382 : int dst_fid = dst->value.ancestor.formal_id;
3784 136382 : class ipa_polymorphic_call_context *src_ctx
3785 136382 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3786 :
3787 : /* Variable number of arguments can cause havoc if we try to access
3788 : one that does not exist in the inlined edge. So make sure we
3789 : don't. */
3790 272764 : if (dst_fid >= ipa_get_cs_argument_count (top))
3791 : {
3792 0 : ipa_set_jf_unknown (dst);
3793 0 : continue;
3794 : }
3795 :
3796 136382 : src = ipa_get_ith_jump_func (top, dst_fid);
3797 :
3798 136382 : if (src_ctx && !src_ctx->useless_p ())
3799 : {
3800 43200 : class ipa_polymorphic_call_context ctx = *src_ctx;
3801 :
3802 : /* TODO: Make type preserved safe WRT contexts. */
3803 43200 : if (!ipa_get_jf_ancestor_type_preserved (dst))
3804 29564 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3805 43200 : ctx.offset_by (dst->value.ancestor.offset);
3806 86400 : if (!ctx.useless_p ())
3807 : {
3808 37700 : if (!dst_ctx)
3809 : {
3810 4599 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3811 : count, true);
3812 4599 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3813 : }
3814 :
3815 37700 : dst_ctx->combine_with (ctx);
3816 : }
3817 : }
3818 :
3819 : /* Parameter and argument in ancestor jump function must be pointer
3820 : type, which means access to aggregate must be by-reference. */
3821 136382 : gcc_assert (!src->agg.items || src->agg.by_ref);
3822 :
3823 136382 : if (src->agg.items && dst->value.ancestor.agg_preserved)
3824 : {
3825 1887 : struct ipa_agg_jf_item *item;
3826 1887 : int j;
3827 :
3828 : /* Currently we do not produce clobber aggregate jump functions,
3829 : replace with merging when we do. */
3830 1887 : gcc_assert (!dst->agg.items);
3831 :
3832 1887 : dst->agg.items = vec_safe_copy (src->agg.items);
3833 1887 : dst->agg.by_ref = src->agg.by_ref;
3834 6266 : FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
3835 4379 : item->offset -= dst->value.ancestor.offset;
3836 : }
3837 :
3838 136382 : if (src->type == IPA_JF_PASS_THROUGH
3839 25241 : && src->value.pass_through.operation == NOP_EXPR)
3840 : {
3841 25237 : dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
3842 25237 : dst->value.ancestor.agg_preserved &=
3843 25237 : src->value.pass_through.agg_preserved;
3844 : }
3845 111145 : else if (src->type == IPA_JF_ANCESTOR)
3846 : {
3847 8648 : dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
3848 8648 : dst->value.ancestor.offset += src->value.ancestor.offset;
3849 8648 : dst->value.ancestor.agg_preserved &=
3850 8648 : src->value.ancestor.agg_preserved;
3851 8648 : dst->value.ancestor.keep_null |= src->value.ancestor.keep_null;
3852 : }
3853 : else
3854 102497 : ipa_set_jf_unknown (dst);
3855 : }
3856 2842753 : else if (dst->type == IPA_JF_PASS_THROUGH)
3857 : {
3858 804030 : struct ipa_jump_func *src;
3859 : /* We must check range due to calls with variable number of arguments
3860 : and we cannot combine jump functions with operations. */
3861 804030 : if (dst->value.pass_through.operation == NOP_EXPR
3862 804030 : && (top && dst->value.pass_through.formal_id
3863 767651 : < ipa_get_cs_argument_count (top)))
3864 : {
3865 767637 : int dst_fid = dst->value.pass_through.formal_id;
3866 767637 : src = ipa_get_ith_jump_func (top, dst_fid);
3867 767637 : bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
3868 767637 : class ipa_polymorphic_call_context *src_ctx
3869 881001 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3870 :
3871 113364 : if (src_ctx && !src_ctx->useless_p ())
3872 : {
3873 64229 : class ipa_polymorphic_call_context ctx = *src_ctx;
3874 :
3875 : /* TODO: Make type preserved safe WRT contexts. */
3876 64229 : if (!ipa_get_jf_pass_through_type_preserved (dst))
3877 27237 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3878 128458 : if (!ctx.useless_p ())
3879 : {
3880 61397 : if (!dst_ctx)
3881 : {
3882 13221 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3883 : count, true);
3884 13221 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3885 : }
3886 61397 : dst_ctx->combine_with (ctx);
3887 : }
3888 : }
3889 767637 : switch (src->type)
3890 : {
3891 329614 : case IPA_JF_UNKNOWN:
3892 329614 : ipa_set_jf_unknown (dst);
3893 329614 : break;
3894 160755 : case IPA_JF_CONST:
3895 160755 : ipa_convert_prop_cst_jf (dst, src,
3896 : ipa_get_type (old_inline_root_info,
3897 : dst_fid));
3898 160755 : break;
3899 :
3900 253027 : case IPA_JF_PASS_THROUGH:
3901 253027 : {
3902 253027 : int formal_id = ipa_get_jf_pass_through_formal_id (src);
3903 253027 : enum tree_code operation;
3904 253027 : operation = ipa_get_jf_pass_through_operation (src);
3905 :
3906 253027 : tree old_ir_ptype = ipa_get_type (old_inline_root_info,
3907 : dst_fid);
3908 253027 : tree new_ir_ptype = ipa_get_type (new_inline_root_info,
3909 : formal_id);
3910 253027 : if (!useless_type_conversion_p (old_ir_ptype, new_ir_ptype))
3911 : {
3912 : /* Jump-function construction now permits type-casts
3913 : from an integer to another if the latter can hold
3914 : all values or has at least the same precision.
3915 : However, as we're combining multiple pass-through
3916 : functions together, we are losing information about
3917 : signedness and thus if conversions should sign or
3918 : zero extend. Therefore we must prevent combining
3919 : such jump-function if signednesses do not match. */
3920 1773 : if (!INTEGRAL_TYPE_P (old_ir_ptype)
3921 930 : || !INTEGRAL_TYPE_P (new_ir_ptype)
3922 1860 : || (TYPE_UNSIGNED (new_ir_ptype)
3923 930 : != TYPE_UNSIGNED (old_ir_ptype)))
3924 : {
3925 843 : ipa_set_jf_unknown (dst);
3926 843 : continue;
3927 : }
3928 : }
3929 :
3930 252184 : if (operation == NOP_EXPR)
3931 : {
3932 250832 : bool agg_p;
3933 501664 : agg_p = dst_agg_p
3934 250832 : && ipa_get_jf_pass_through_agg_preserved (src);
3935 250832 : ipa_set_jf_simple_pass_through (dst, formal_id, agg_p);
3936 : }
3937 1352 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
3938 : {
3939 8 : tree op_t = ipa_get_jf_pass_through_op_type (src);
3940 8 : ipa_set_jf_unary_pass_through (dst, formal_id, operation,
3941 : op_t);
3942 : }
3943 : else
3944 : {
3945 1344 : tree operand = ipa_get_jf_pass_through_operand (src);
3946 1344 : tree op_t = ipa_get_jf_pass_through_op_type (src);
3947 1344 : ipa_set_jf_arith_pass_through (dst, formal_id, operand,
3948 : operation, op_t);
3949 : }
3950 : break;
3951 : }
3952 24241 : case IPA_JF_ANCESTOR:
3953 24241 : {
3954 24241 : bool agg_p;
3955 48482 : agg_p = dst_agg_p
3956 24241 : && ipa_get_jf_ancestor_agg_preserved (src);
3957 24241 : ipa_set_ancestor_jf (dst,
3958 : ipa_get_jf_ancestor_offset (src),
3959 : ipa_get_jf_ancestor_formal_id (src),
3960 : agg_p,
3961 24241 : ipa_get_jf_ancestor_keep_null (src));
3962 24241 : break;
3963 : }
3964 0 : default:
3965 0 : gcc_unreachable ();
3966 : }
3967 :
3968 766794 : if (src->m_vr && src->m_vr->known_p ())
3969 : {
3970 510120 : value_range svr (src->m_vr->type ());
3971 510120 : if (!dst->m_vr || !dst->m_vr->known_p ())
3972 208209 : ipa_set_jfunc_vr (dst, *src->m_vr);
3973 301911 : else if (ipa_vr_operation_and_type_effects (svr, *src->m_vr,
3974 : NOP_EXPR,
3975 301911 : dst->m_vr->type (),
3976 301911 : src->m_vr->type ()))
3977 : {
3978 301903 : value_range dvr;
3979 301903 : dst->m_vr->get_vrange (dvr);
3980 301903 : dvr.intersect (svr);
3981 301903 : if (!dvr.undefined_p ())
3982 289342 : ipa_set_jfunc_vr (dst, dvr);
3983 301903 : }
3984 510120 : }
3985 :
3986 766794 : if (src->agg.items
3987 31008 : && (dst_agg_p || !src->agg.by_ref))
3988 : {
3989 : /* Currently we do not produce clobber aggregate jump
3990 : functions, replace with merging when we do. */
3991 23289 : gcc_assert (!dst->agg.items);
3992 :
3993 23289 : dst->agg.by_ref = src->agg.by_ref;
3994 23289 : dst->agg.items = vec_safe_copy (src->agg.items);
3995 : }
3996 : }
3997 : else
3998 36393 : ipa_set_jf_unknown (dst);
3999 : }
4000 : }
4001 : }
4002 :
4003 : /* If TARGET is an addr_expr of a function declaration, make it the
4004 : (SPECULATIVE)destination of an indirect edge IE and return the edge.
4005 : Otherwise, return NULL. */
4006 :
4007 : struct cgraph_edge *
4008 4010 : ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target,
4009 : bool speculative)
4010 : {
4011 4010 : struct cgraph_node *callee;
4012 4010 : bool unreachable = false;
4013 :
4014 4010 : if (TREE_CODE (target) == ADDR_EXPR)
4015 1357 : target = TREE_OPERAND (target, 0);
4016 4010 : if (TREE_CODE (target) != FUNCTION_DECL)
4017 : {
4018 17 : target = canonicalize_constructor_val (target, NULL);
4019 17 : if (!target || TREE_CODE (target) != FUNCTION_DECL)
4020 : {
4021 17 : cgraph_simple_indirect_info *sii
4022 17 : = dyn_cast <cgraph_simple_indirect_info *> (ie->indirect_info);
4023 : /* Member pointer call that goes through a VMT lookup. */
4024 17 : if ((sii && sii->member_ptr)
4025 : /* Or if target is not an invariant expression and we do not
4026 : know if it will evaulate to function at runtime.
4027 : This can happen when folding through &VAR, where &VAR
4028 : is IP invariant, but VAR itself is not.
4029 :
4030 : TODO: It seems that we may try to fold the expression and see
4031 : if VAR is readonly. */
4032 11 : || !is_gimple_ip_invariant (target))
4033 : {
4034 6 : if (dump_enabled_p ())
4035 : {
4036 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
4037 : "discovered direct call non-invariant %s\n",
4038 0 : ie->caller->dump_name ());
4039 : }
4040 6 : return NULL;
4041 : }
4042 :
4043 :
4044 11 : if (dump_enabled_p ())
4045 : {
4046 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
4047 : "discovered direct call to non-function in %s, "
4048 : "making it __builtin_unreachable\n",
4049 0 : ie->caller->dump_name ());
4050 : }
4051 :
4052 11 : target = builtin_decl_unreachable ();
4053 11 : callee = cgraph_node::get_create (target);
4054 11 : unreachable = true;
4055 11 : }
4056 : else
4057 0 : callee = cgraph_node::get (target);
4058 : }
4059 : else
4060 3993 : callee = cgraph_node::get (target);
4061 :
4062 : /* Because may-edges are not explicitly represented and vtable may be external,
4063 : we may create the first reference to the object in the unit. */
4064 4004 : if (!callee || callee->inlined_to)
4065 : {
4066 :
4067 : /* We are better to ensure we can refer to it.
4068 : In the case of static functions we are out of luck, since we already
4069 : removed its body. In the case of public functions we may or may
4070 : not introduce the reference. */
4071 0 : if (!canonicalize_constructor_val (target, NULL)
4072 0 : || !TREE_PUBLIC (target))
4073 : {
4074 0 : if (dump_file)
4075 0 : fprintf (dump_file, "ipa-prop: Discovered call to a known target "
4076 : "(%s -> %s) but cannot refer to it. Giving up.\n",
4077 0 : ie->caller->dump_name (),
4078 0 : ie->callee->dump_name ());
4079 0 : return NULL;
4080 : }
4081 0 : callee = cgraph_node::get_create (target);
4082 : }
4083 :
4084 : /* If the edge is already speculated. */
4085 4004 : if (speculative && ie->speculative)
4086 : {
4087 2 : if (dump_file)
4088 : {
4089 0 : cgraph_edge *e2 = ie->speculative_call_for_target (callee);
4090 0 : if (!e2)
4091 : {
4092 0 : if (dump_file)
4093 0 : fprintf (dump_file, "ipa-prop: Discovered call to a "
4094 : "speculative target (%s -> %s) but the call is "
4095 : "already speculated to different target. "
4096 : "Giving up.\n",
4097 0 : ie->caller->dump_name (), callee->dump_name ());
4098 : }
4099 : else
4100 : {
4101 0 : if (dump_file)
4102 0 : fprintf (dump_file,
4103 : "ipa-prop: Discovered call to a speculative target "
4104 : "(%s -> %s) this agree with previous speculation.\n",
4105 0 : ie->caller->dump_name (), callee->dump_name ());
4106 : }
4107 : }
4108 2 : return NULL;
4109 : }
4110 :
4111 4002 : if (!dbg_cnt (devirt))
4112 : return NULL;
4113 :
4114 4002 : ipa_check_create_node_params ();
4115 :
4116 : /* We cannot make edges to inline clones. It is bug that someone removed
4117 : the cgraph node too early. */
4118 4002 : gcc_assert (!callee->inlined_to);
4119 :
4120 4002 : if (dump_file && !unreachable)
4121 : {
4122 402 : fprintf (dump_file, "ipa-prop: Discovered %s call to a %s target "
4123 : "(%s -> %s), for stmt ",
4124 402 : is_a <cgraph_polymorphic_indirect_info *> (ie->indirect_info)
4125 : ? "a virtual" : "an indirect",
4126 : speculative ? "speculative" : "known",
4127 201 : ie->caller->dump_name (),
4128 : callee->dump_name ());
4129 201 : if (ie->call_stmt)
4130 193 : print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
4131 : else
4132 8 : fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
4133 : }
4134 4002 : if (dump_enabled_p ())
4135 : {
4136 402 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
4137 : "converting indirect call in %s to direct call to %s\n",
4138 201 : ie->caller->dump_name (), callee->dump_name ());
4139 : }
4140 4002 : if (!speculative)
4141 : {
4142 3975 : struct cgraph_edge *orig = ie;
4143 3975 : ie = cgraph_edge::make_direct (ie, callee);
4144 : /* If we resolved speculative edge the cost is already up to date
4145 : for direct call (adjusted by inline_edge_duplication_hook). */
4146 3975 : if (ie == orig)
4147 : {
4148 3157 : ipa_call_summary *es = ipa_call_summaries->get (ie);
4149 3157 : es->call_stmt_size -= (eni_size_weights.indirect_call_cost
4150 3157 : - eni_size_weights.call_cost);
4151 3157 : es->call_stmt_time -= (eni_time_weights.indirect_call_cost
4152 3157 : - eni_time_weights.call_cost);
4153 : }
4154 : }
4155 : else
4156 : {
4157 27 : if (!callee->can_be_discarded_p ())
4158 : {
4159 4 : cgraph_node *alias;
4160 4 : alias = dyn_cast<cgraph_node *> (callee->noninterposable_alias ());
4161 : if (alias)
4162 27 : callee = alias;
4163 : }
4164 : /* make_speculative will update ie's cost to direct call cost. */
4165 27 : ie = ie->make_speculative
4166 27 : (callee, ie->count.apply_scale (8, 10));
4167 : }
4168 :
4169 : return ie;
4170 : }
4171 :
4172 : /* Attempt to locate an interprocedural constant at a given REQ_OFFSET in
4173 : CONSTRUCTOR and return it. Return NULL if the search fails for some
4174 : reason. */
4175 :
4176 : static tree
4177 11156 : find_constructor_constant_at_offset (tree constructor, HOST_WIDE_INT req_offset)
4178 : {
4179 16480 : tree type = TREE_TYPE (constructor);
4180 16480 : if (TREE_CODE (type) != ARRAY_TYPE
4181 16480 : && TREE_CODE (type) != RECORD_TYPE)
4182 : return NULL;
4183 :
4184 16468 : unsigned ix;
4185 16468 : tree index, val;
4186 22541 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (constructor), ix, index, val)
4187 : {
4188 21869 : HOST_WIDE_INT elt_offset;
4189 21869 : if (TREE_CODE (type) == ARRAY_TYPE)
4190 : {
4191 284 : offset_int off;
4192 284 : tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
4193 284 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
4194 :
4195 284 : if (index)
4196 : {
4197 284 : if (TREE_CODE (index) == RANGE_EXPR)
4198 60 : off = wi::to_offset (TREE_OPERAND (index, 0));
4199 : else
4200 224 : off = wi::to_offset (index);
4201 284 : if (TYPE_DOMAIN (type) && TYPE_MIN_VALUE (TYPE_DOMAIN (type)))
4202 : {
4203 284 : tree low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4204 284 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
4205 284 : off = wi::sext (off - wi::to_offset (low_bound),
4206 284 : TYPE_PRECISION (TREE_TYPE (index)));
4207 : }
4208 284 : off *= wi::to_offset (unit_size);
4209 : /* ??? Handle more than just the first index of a
4210 : RANGE_EXPR. */
4211 : }
4212 : else
4213 0 : off = wi::to_offset (unit_size) * ix;
4214 :
4215 284 : off = wi::lshift (off, LOG2_BITS_PER_UNIT);
4216 284 : if (!wi::fits_shwi_p (off) || wi::neg_p (off))
4217 0 : continue;
4218 284 : elt_offset = off.to_shwi ();
4219 : }
4220 21585 : else if (TREE_CODE (type) == RECORD_TYPE)
4221 : {
4222 21585 : gcc_checking_assert (index && TREE_CODE (index) == FIELD_DECL);
4223 21585 : if (DECL_BIT_FIELD (index))
4224 0 : continue;
4225 21585 : elt_offset = int_bit_position (index);
4226 : }
4227 : else
4228 0 : gcc_unreachable ();
4229 :
4230 21869 : if (elt_offset > req_offset)
4231 : return NULL;
4232 :
4233 21869 : if (TREE_CODE (val) == CONSTRUCTOR)
4234 5324 : return find_constructor_constant_at_offset (val,
4235 5324 : req_offset - elt_offset);
4236 :
4237 16545 : if (elt_offset == req_offset
4238 11144 : && is_gimple_reg_type (TREE_TYPE (val))
4239 27689 : && is_gimple_ip_invariant (val))
4240 : return val;
4241 : }
4242 : return NULL;
4243 : }
4244 :
4245 : /* Check whether SCALAR could be used to look up an aggregate interprocedural
4246 : invariant from a static constructor and if so, return it. Otherwise return
4247 : NULL. */
4248 :
4249 : tree
4250 13346158 : ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, bool by_ref)
4251 : {
4252 13346158 : if (by_ref)
4253 : {
4254 13332673 : if (TREE_CODE (scalar) != ADDR_EXPR)
4255 : return NULL;
4256 4161283 : scalar = TREE_OPERAND (scalar, 0);
4257 : }
4258 :
4259 4174768 : if (!VAR_P (scalar)
4260 2731487 : || !is_global_var (scalar)
4261 294264 : || !TREE_READONLY (scalar)
4262 15703 : || !DECL_INITIAL (scalar)
4263 4188044 : || TREE_CODE (DECL_INITIAL (scalar)) != CONSTRUCTOR)
4264 : return NULL;
4265 :
4266 11156 : return find_constructor_constant_at_offset (DECL_INITIAL (scalar), offset);
4267 : }
4268 :
4269 : /* Retrieve value from AGG_JFUNC for the given OFFSET or return NULL if there
4270 : is none. BY_REF specifies whether the value has to be passed by reference
4271 : or by value. */
4272 :
4273 : static tree
4274 24026 : ipa_find_agg_cst_from_jfunc_items (struct ipa_agg_jump_function *agg_jfunc,
4275 : ipa_node_params *src_info,
4276 : cgraph_node *src_node,
4277 : HOST_WIDE_INT offset, bool by_ref)
4278 : {
4279 24026 : if (by_ref != agg_jfunc->by_ref)
4280 : return NULL_TREE;
4281 :
4282 4226 : for (const ipa_agg_jf_item &item : agg_jfunc->items)
4283 1498 : if (item.offset == offset)
4284 1099 : return ipa_agg_value_from_jfunc (src_info, src_node, &item);
4285 :
4286 : return NULL_TREE;
4287 : }
4288 :
4289 : /* Remove a reference to SYMBOL from the list of references of a node given by
4290 : reference description RDESC. Return true if the reference has been
4291 : successfully found and removed. */
4292 :
4293 : static bool
4294 8506 : remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
4295 : {
4296 8506 : struct ipa_ref *to_del;
4297 8506 : struct cgraph_edge *origin;
4298 :
4299 8506 : origin = rdesc->cs;
4300 8506 : if (!origin)
4301 : return false;
4302 8506 : to_del = origin->caller->find_reference (symbol, origin->call_stmt,
4303 : origin->lto_stmt_uid, IPA_REF_ADDR);
4304 8506 : if (!to_del)
4305 : return false;
4306 :
4307 8506 : to_del->remove_reference ();
4308 8506 : if (dump_file)
4309 26 : fprintf (dump_file, "ipa-prop: Removed a reference from %s to %s.\n",
4310 13 : origin->caller->dump_name (), symbol->dump_name ());
4311 : return true;
4312 : }
4313 :
4314 : /* If JFUNC has a reference description with refcount different from
4315 : IPA_UNDESCRIBED_USE, return the reference description, otherwise return
4316 : NULL. JFUNC must be a constant jump function. */
4317 :
4318 : static struct ipa_cst_ref_desc *
4319 1325631 : jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
4320 : {
4321 1325631 : struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
4322 1325631 : if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
4323 : return rdesc;
4324 : else
4325 1136266 : return NULL;
4326 : }
4327 :
4328 : /* If the value of constant jump function JFUNC is an address of a function
4329 : declaration, return the associated call graph node. Otherwise return
4330 : NULL. */
4331 :
4332 : static symtab_node *
4333 1818 : symtab_node_for_jfunc (struct ipa_jump_func *jfunc)
4334 : {
4335 1818 : gcc_checking_assert (jfunc->type == IPA_JF_CONST);
4336 1818 : tree cst = ipa_get_jf_constant (jfunc);
4337 1818 : if (TREE_CODE (cst) != ADDR_EXPR
4338 1818 : || (TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL
4339 31 : && TREE_CODE (TREE_OPERAND (cst, 0)) != VAR_DECL))
4340 : return NULL;
4341 :
4342 1808 : return symtab_node::get (TREE_OPERAND (cst, 0));
4343 : }
4344 :
4345 :
4346 : /* If JFUNC is a constant jump function with a usable rdesc, decrement its
4347 : refcount and if it hits zero, remove reference to SYMBOL from the caller of
4348 : the edge specified in the rdesc. Return false if either the symbol or the
4349 : reference could not be found, otherwise return true. */
4350 :
4351 : static bool
4352 1001 : try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
4353 : {
4354 1001 : struct ipa_cst_ref_desc *rdesc;
4355 1001 : if (jfunc->type == IPA_JF_CONST
4356 1001 : && (rdesc = jfunc_rdesc_usable (jfunc))
4357 1975 : && --rdesc->refcount == 0)
4358 : {
4359 782 : symtab_node *symbol = symtab_node_for_jfunc (jfunc);
4360 782 : if (!symbol)
4361 : return false;
4362 :
4363 782 : return remove_described_reference (symbol, rdesc);
4364 : }
4365 : return true;
4366 : }
4367 :
4368 : /* Try to find a destination for indirect edge IE that corresponds to a simple
4369 : call or a call of a member function pointer and where the destination is a
4370 : pointer formal parameter described by jump function JFUNC. TARGET_TYPE is
4371 : the type of the parameter to which the result of JFUNC is passed. If it can
4372 : be determined, return the newly direct edge, otherwise return NULL.
4373 : NEW_ROOT and NEW_ROOT_INFO is the node and its info that JFUNC lattices are
4374 : relative to. */
4375 :
4376 : static struct cgraph_edge *
4377 9292 : try_make_edge_direct_simple_call (struct cgraph_edge *ie,
4378 : struct ipa_jump_func *jfunc, tree target_type,
4379 : struct cgraph_node *new_root,
4380 : class ipa_node_params *new_root_info)
4381 : {
4382 9292 : tree target = NULL_TREE;
4383 9292 : cgraph_simple_indirect_info *sii
4384 9292 : = as_a <cgraph_simple_indirect_info *> (ie->indirect_info);
4385 9292 : bool agg_contents = sii->agg_contents;
4386 9292 : tree scalar = ipa_value_from_jfunc (new_root_info, jfunc, target_type);
4387 9292 : if (agg_contents)
4388 : {
4389 7153 : if (scalar)
4390 15 : target = ipa_find_agg_cst_from_init (scalar, sii->offset, sii->by_ref);
4391 7153 : if (!target && sii->guaranteed_unmodified)
4392 5994 : target = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
4393 : new_root, sii->offset,
4394 : sii->by_ref);
4395 : }
4396 : else
4397 : target = scalar;
4398 9291 : if (!target)
4399 : return NULL;
4400 1374 : cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target);
4401 :
4402 1374 : if (cs && !agg_contents)
4403 : {
4404 1001 : bool ok;
4405 1001 : gcc_checking_assert (cs->callee
4406 : && (cs != ie
4407 : || jfunc->type != IPA_JF_CONST
4408 : || !symtab_node_for_jfunc (jfunc)
4409 : || cs->callee == symtab_node_for_jfunc (jfunc)));
4410 1001 : ok = try_decrement_rdesc_refcount (jfunc);
4411 1001 : gcc_checking_assert (ok);
4412 : }
4413 :
4414 : return cs;
4415 : }
4416 :
4417 : /* Return the target to be used in cases of impossible devirtualization. IE
4418 : and target (the latter can be NULL) are dumped when dumping is enabled. */
4419 :
4420 : tree
4421 508 : ipa_impossible_devirt_target (struct cgraph_edge *ie, tree target)
4422 : {
4423 508 : if (dump_file)
4424 : {
4425 57 : if (target)
4426 18 : fprintf (dump_file,
4427 : "Type inconsistent devirtualization: %s->%s\n",
4428 18 : ie->caller->dump_name (),
4429 18 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
4430 : else
4431 39 : fprintf (dump_file,
4432 : "No devirtualization target in %s\n",
4433 39 : ie->caller->dump_name ());
4434 : }
4435 508 : tree new_target = builtin_decl_unreachable ();
4436 508 : cgraph_node::get_create (new_target);
4437 508 : return new_target;
4438 : }
4439 :
4440 : /* Try to find a destination for indirect edge IE that corresponds to a virtual
4441 : call based on a formal parameter which is described by jump function JFUNC
4442 : and if it can be determined, make it direct and return the direct edge.
4443 : Otherwise, return NULL. CTX describes the polymorphic context that the
4444 : parameter the call is based on brings along with it. NEW_ROOT and
4445 : NEW_ROOT_INFO is the node and its info that JFUNC lattices are relative
4446 : to. */
4447 :
4448 : static struct cgraph_edge *
4449 18035 : try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
4450 : struct ipa_jump_func *jfunc,
4451 : class ipa_polymorphic_call_context ctx,
4452 : struct cgraph_node *new_root,
4453 : class ipa_node_params *new_root_info)
4454 : {
4455 18035 : tree target = NULL;
4456 18035 : bool speculative = false;
4457 :
4458 18035 : if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
4459 : return NULL;
4460 18035 : cgraph_polymorphic_indirect_info *pii
4461 18035 : = as_a <cgraph_polymorphic_indirect_info *> (ie->indirect_info);
4462 18035 : if (!pii->usable_p ())
4463 : return nullptr;
4464 :
4465 : /* Try to do lookup via known virtual table pointer value. */
4466 18035 : if (!pii->vptr_changed
4467 18035 : || opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively))
4468 : {
4469 18035 : tree vtable;
4470 18035 : unsigned HOST_WIDE_INT offset;
4471 18035 : tree t = NULL_TREE;
4472 18035 : if (jfunc->type == IPA_JF_CONST)
4473 330 : t = ipa_find_agg_cst_from_init (ipa_get_jf_constant (jfunc),
4474 : pii->offset, true);
4475 330 : if (!t)
4476 18032 : t = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
4477 : new_root, pii->offset, true);
4478 18035 : if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset))
4479 : {
4480 709 : bool can_refer;
4481 709 : t = gimple_get_virt_method_for_vtable (pii->otr_token, vtable, offset,
4482 : &can_refer);
4483 709 : if (can_refer)
4484 : {
4485 692 : if (!t
4486 692 : || fndecl_built_in_p (t, BUILT_IN_UNREACHABLE,
4487 : BUILT_IN_UNREACHABLE_TRAP)
4488 1336 : || !possible_polymorphic_call_target_p
4489 644 : (ie, cgraph_node::get (t)))
4490 : {
4491 : /* Do not speculate builtin_unreachable, it is stupid! */
4492 90 : if (!pii->vptr_changed)
4493 90 : target = ipa_impossible_devirt_target (ie, target);
4494 : else
4495 : target = NULL;
4496 : }
4497 : else
4498 : {
4499 602 : target = t;
4500 602 : speculative = pii->vptr_changed;
4501 : }
4502 : }
4503 : }
4504 : }
4505 :
4506 18035 : ipa_polymorphic_call_context ie_context (ie);
4507 18035 : vec <cgraph_node *>targets;
4508 18035 : bool final;
4509 :
4510 18035 : ctx.offset_by (pii->offset);
4511 18035 : if (pii->vptr_changed)
4512 6206 : ctx.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
4513 : pii->otr_type);
4514 18035 : ctx.combine_with (ie_context, pii->otr_type);
4515 18035 : targets = possible_polymorphic_call_targets (pii->otr_type, pii->otr_token,
4516 : ctx, &final);
4517 20109 : if (final && targets.length () <= 1)
4518 : {
4519 2055 : speculative = false;
4520 2055 : if (targets.length () == 1)
4521 2004 : target = targets[0]->decl;
4522 : else
4523 51 : target = ipa_impossible_devirt_target (ie, NULL_TREE);
4524 : }
4525 15972 : else if (!target && opt_for_fn (ie->caller->decl,
4526 : flag_devirtualize_speculatively)
4527 31870 : && !ie->speculative && ie->maybe_hot_p ())
4528 : {
4529 8779 : cgraph_node *n;
4530 8779 : n = try_speculative_devirtualization (pii->otr_type, pii->otr_token,
4531 : pii->context);
4532 8779 : if (n)
4533 : {
4534 18 : target = n->decl;
4535 18 : speculative = true;
4536 : }
4537 : }
4538 :
4539 18035 : if (target)
4540 : {
4541 2081 : if (!possible_polymorphic_call_target_p
4542 2081 : (ie, cgraph_node::get_create (target)))
4543 : {
4544 51 : if (speculative)
4545 : return NULL;
4546 51 : target = ipa_impossible_devirt_target (ie, target);
4547 : }
4548 2081 : return ipa_make_edge_direct_to_target (ie, target, speculative);
4549 : }
4550 : else
4551 : return NULL;
4552 : }
4553 :
4554 : /* Update the param called notes associated with NODE when CS is being inlined,
4555 : assuming NODE is (potentially indirectly) inlined into CS->callee.
4556 : Moreover, if the callee is discovered to be constant, create a new cgraph
4557 : edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
4558 : unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
4559 :
4560 : static bool
4561 1668755 : update_indirect_edges_after_inlining (struct cgraph_edge *cs,
4562 : struct cgraph_node *node,
4563 : vec<cgraph_edge *> *new_edges)
4564 : {
4565 1668755 : bool res = false;
4566 :
4567 1668755 : ipa_check_create_edge_args ();
4568 1668755 : class ipa_edge_args *top = ipa_edge_args_sum->get (cs);
4569 1151149 : cgraph_node *new_root
4570 1668755 : = cs->caller->inlined_to ? cs->caller->inlined_to : cs->caller;
4571 1668755 : ipa_node_params *new_root_info = ipa_node_params_sum->get (new_root);
4572 1668755 : ipa_node_params *inlined_node_info
4573 1668755 : = ipa_node_params_sum->get (cs->callee->function_symbol ());
4574 :
4575 1668755 : cgraph_edge *next_ie;
4576 1739229 : for (cgraph_edge *ie = node->indirect_calls; ie; ie = next_ie)
4577 : {
4578 70474 : next_ie = ie->next_callee;
4579 :
4580 113448 : if (!top
4581 70349 : || ie->indirect_info->param_index < 0
4582 125478 : || ie->indirect_info->param_index >= ipa_get_cs_argument_count (top))
4583 : {
4584 42974 : ie->indirect_info->param_index = -1;
4585 46397 : continue;
4586 : }
4587 :
4588 27500 : int param_index = ie->indirect_info->param_index;
4589 27500 : cgraph_polymorphic_indirect_info *pii
4590 27500 : = dyn_cast <cgraph_polymorphic_indirect_info *> (ie->indirect_info);
4591 27500 : cgraph_simple_indirect_info *sii
4592 27500 : = dyn_cast <cgraph_simple_indirect_info *> (ie->indirect_info);
4593 27500 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (top, param_index);
4594 :
4595 27500 : auto_vec<cgraph_node *, 4> spec_targets;
4596 27500 : if (ie->speculative)
4597 8030 : for (cgraph_edge *direct = ie->first_speculative_call_target ();
4598 20156 : direct;
4599 12126 : direct = direct->next_speculative_call_target ())
4600 12126 : spec_targets.safe_push (direct->callee);
4601 :
4602 27500 : cgraph_edge *new_direct_edge;
4603 27500 : if (!opt_for_fn (node->decl, flag_indirect_inlining))
4604 173 : new_direct_edge = NULL;
4605 27327 : else if (pii)
4606 : {
4607 18035 : ipa_polymorphic_call_context ctx;
4608 18035 : ctx = ipa_context_from_jfunc (new_root_info, cs, param_index, jfunc);
4609 18035 : new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc, ctx,
4610 : new_root,
4611 : new_root_info);
4612 : }
4613 9292 : else if (sii)
4614 : {
4615 9292 : tree target_type = ipa_get_type (inlined_node_info, param_index);
4616 9292 : new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
4617 : target_type,
4618 : new_root,
4619 : new_root_info);
4620 : }
4621 : else
4622 0 : gcc_unreachable ();
4623 :
4624 : /* If speculation was removed, then we need to do nothing. */
4625 3447 : if (new_direct_edge && new_direct_edge != ie
4626 28247 : && spec_targets.contains (new_direct_edge->callee))
4627 : {
4628 723 : new_direct_edge->indirect_inlining_edge = 1;
4629 723 : res = true;
4630 723 : if (!new_direct_edge->speculative)
4631 723 : continue;
4632 : }
4633 26777 : else if (new_direct_edge)
4634 : {
4635 2724 : new_direct_edge->indirect_inlining_edge = 1;
4636 2724 : if (new_edges)
4637 : {
4638 2720 : new_edges->safe_push (new_direct_edge);
4639 2720 : res = true;
4640 : }
4641 : /* If speculative edge was introduced we still need to update
4642 : call info of the indirect edge. */
4643 2724 : if (!new_direct_edge->speculative)
4644 2700 : continue;
4645 : }
4646 24077 : if (jfunc->type == IPA_JF_PASS_THROUGH
4647 24077 : && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4648 : {
4649 5753 : if (!pii
4650 2600 : && sii->agg_contents
4651 7262 : && !ipa_get_jf_pass_through_agg_preserved (jfunc))
4652 981 : ie->indirect_info->param_index = -1;
4653 : else
4654 : {
4655 4772 : param_index = ipa_get_jf_pass_through_formal_id (jfunc);
4656 4772 : ie->indirect_info->param_index = param_index;
4657 4772 : ipa_set_param_used_by_indirect_call (new_root_info, param_index,
4658 : true);
4659 4772 : if (pii)
4660 : {
4661 3153 : if (!ipa_get_jf_pass_through_type_preserved (jfunc))
4662 2074 : pii->vptr_changed = true;
4663 3153 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4664 : param_index, true);
4665 : }
4666 : }
4667 : }
4668 18324 : else if (jfunc->type == IPA_JF_ANCESTOR)
4669 : {
4670 513 : if (!pii
4671 159 : && sii->agg_contents
4672 672 : && !ipa_get_jf_ancestor_agg_preserved (jfunc))
4673 56 : ie->indirect_info->param_index = -1;
4674 : else
4675 : {
4676 457 : param_index = ipa_get_jf_ancestor_formal_id (jfunc);
4677 457 : ie->indirect_info->param_index = param_index;
4678 457 : ipa_set_param_used_by_indirect_call (new_root_info, param_index,
4679 : true);
4680 457 : if (pii)
4681 : {
4682 354 : pii->offset += ipa_get_jf_ancestor_offset (jfunc);
4683 354 : if (!ipa_get_jf_ancestor_type_preserved (jfunc))
4684 314 : pii->vptr_changed = true;
4685 354 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4686 : param_index, true);
4687 : }
4688 : else
4689 103 : sii->offset += ipa_get_jf_ancestor_offset (jfunc);
4690 : }
4691 : }
4692 : else
4693 : /* Either we can find a destination for this edge now or never. */
4694 17811 : ie->indirect_info->param_index = -1;
4695 27500 : }
4696 :
4697 1668755 : return res;
4698 : }
4699 :
4700 : /* Recursively traverse subtree of NODE (including node) made of inlined
4701 : cgraph_edges when CS has been inlined and invoke
4702 : update_indirect_edges_after_inlining on all nodes and
4703 : update_jump_functions_after_inlining on all non-inlined edges that lead out
4704 : of this subtree. Newly discovered indirect edges will be added to
4705 : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
4706 : created. */
4707 :
4708 : static bool
4709 1668755 : propagate_info_to_inlined_callees (struct cgraph_edge *cs,
4710 : struct cgraph_node *node,
4711 : vec<cgraph_edge *> *new_edges)
4712 : {
4713 1668755 : struct cgraph_edge *e;
4714 1668755 : bool res;
4715 :
4716 1668755 : res = update_indirect_edges_after_inlining (cs, node, new_edges);
4717 :
4718 5010246 : for (e = node->callees; e; e = e->next_callee)
4719 3341491 : if (!e->inline_failed)
4720 728378 : res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
4721 : else
4722 2613113 : update_jump_functions_after_inlining (cs, e);
4723 1735806 : for (e = node->indirect_calls; e; e = e->next_callee)
4724 67051 : update_jump_functions_after_inlining (cs, e);
4725 :
4726 1668755 : return res;
4727 : }
4728 :
4729 : /* Combine two controlled uses counts as done during inlining. */
4730 :
4731 : static int
4732 390635 : combine_controlled_uses_counters (int c, int d)
4733 : {
4734 0 : if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
4735 : return IPA_UNDESCRIBED_USE;
4736 : else
4737 101494 : return c + d - 1;
4738 : }
4739 :
4740 : /* Propagate number of controlled users from CS->caleee to the new root of the
4741 : tree of inlined nodes. */
4742 :
4743 : static void
4744 940377 : propagate_controlled_uses (struct cgraph_edge *cs)
4745 : {
4746 940377 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4747 940377 : if (!args)
4748 : return;
4749 662050 : struct cgraph_node *new_root = cs->caller->inlined_to
4750 938616 : ? cs->caller->inlined_to : cs->caller;
4751 938616 : ipa_node_params *new_root_info = ipa_node_params_sum->get (new_root);
4752 938616 : ipa_node_params *old_root_info = ipa_node_params_sum->get (cs->callee);
4753 938616 : int count, i;
4754 :
4755 938616 : if (!old_root_info)
4756 : return;
4757 :
4758 1822993 : count = MIN (ipa_get_cs_argument_count (args),
4759 : ipa_get_param_count (old_root_info));
4760 2868285 : for (i = 0; i < count; i++)
4761 : {
4762 1929758 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4763 1929758 : struct ipa_cst_ref_desc *rdesc;
4764 :
4765 1929758 : if (jf->type == IPA_JF_PASS_THROUGH
4766 1929758 : && !ipa_get_jf_pass_through_refdesc_decremented (jf))
4767 : {
4768 335242 : int src_idx, c, d;
4769 335242 : src_idx = ipa_get_jf_pass_through_formal_id (jf);
4770 335242 : c = ipa_get_controlled_uses (new_root_info, src_idx);
4771 335242 : d = ipa_get_controlled_uses (old_root_info, i);
4772 :
4773 335242 : gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
4774 : == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
4775 335242 : c = combine_controlled_uses_counters (c, d);
4776 335242 : ipa_set_controlled_uses (new_root_info, src_idx, c);
4777 335242 : bool lderef = true;
4778 335242 : if (c != IPA_UNDESCRIBED_USE)
4779 : {
4780 90074 : lderef = (ipa_get_param_load_dereferenced (new_root_info, src_idx)
4781 90074 : || ipa_get_param_load_dereferenced (old_root_info, i));
4782 90074 : ipa_set_param_load_dereferenced (new_root_info, src_idx, lderef);
4783 : }
4784 :
4785 335242 : if (c == 0 && !lderef && new_root_info->ipcp_orig_node)
4786 : {
4787 181 : struct cgraph_node *n;
4788 181 : struct ipa_ref *ref;
4789 181 : tree t = new_root_info->known_csts[src_idx];
4790 :
4791 159 : if (t && TREE_CODE (t) == ADDR_EXPR
4792 139 : && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
4793 5 : && (n = cgraph_node::get (TREE_OPERAND (t, 0)))
4794 186 : && (ref = new_root->find_reference (n, NULL, 0,
4795 : IPA_REF_ADDR)))
4796 : {
4797 5 : if (dump_file)
4798 1 : fprintf (dump_file, "ipa-prop: Removing cloning-created "
4799 : "reference from %s to %s.\n",
4800 : new_root->dump_name (),
4801 : n->dump_name ());
4802 5 : ref->remove_reference ();
4803 : }
4804 : }
4805 : }
4806 1594516 : else if (jf->type == IPA_JF_CONST
4807 1594516 : && (rdesc = jfunc_rdesc_usable (jf)))
4808 : {
4809 55393 : int d = ipa_get_controlled_uses (old_root_info, i);
4810 55393 : int c = rdesc->refcount;
4811 55393 : tree cst = ipa_get_jf_constant (jf);
4812 55393 : rdesc->refcount = combine_controlled_uses_counters (c, d);
4813 55393 : if (rdesc->refcount != IPA_UNDESCRIBED_USE
4814 11420 : && ipa_get_param_load_dereferenced (old_root_info, i)
4815 2310 : && TREE_CODE (cst) == ADDR_EXPR
4816 57703 : && VAR_P (TREE_OPERAND (cst, 0)))
4817 : {
4818 2309 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4819 2309 : new_root->create_reference (n, IPA_REF_LOAD, NULL);
4820 2309 : if (dump_file)
4821 3 : fprintf (dump_file, "ipa-prop: Address IPA constant will reach "
4822 : "a load so adding LOAD reference from %s to %s.\n",
4823 : new_root->dump_name (), n->dump_name ());
4824 : }
4825 55393 : if (rdesc->refcount == 0)
4826 : {
4827 7724 : gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
4828 : && ((TREE_CODE (TREE_OPERAND (cst, 0))
4829 : == FUNCTION_DECL)
4830 : || VAR_P (TREE_OPERAND (cst, 0))));
4831 :
4832 7724 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4833 7724 : if (n)
4834 : {
4835 7724 : remove_described_reference (n, rdesc);
4836 7724 : cgraph_node *clone = cs->caller;
4837 7724 : while (clone->inlined_to
4838 1593 : && clone->ipcp_clone
4839 7956 : && clone != rdesc->cs->caller)
4840 : {
4841 95 : struct ipa_ref *ref;
4842 95 : ref = clone->find_reference (n, NULL, 0, IPA_REF_ADDR);
4843 95 : if (ref)
4844 : {
4845 92 : if (dump_file)
4846 1 : fprintf (dump_file, "ipa-prop: Removing "
4847 : "cloning-created reference "
4848 : "from %s to %s.\n",
4849 : clone->dump_name (),
4850 : n->dump_name ());
4851 92 : ref->remove_reference ();
4852 : }
4853 95 : clone = clone->callers->caller;
4854 : }
4855 : }
4856 : }
4857 : }
4858 : }
4859 :
4860 1823094 : for (i = ipa_get_param_count (old_root_info);
4861 1823661 : i < ipa_get_cs_argument_count (args);
4862 : i++)
4863 : {
4864 334 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4865 :
4866 334 : if (jf->type == IPA_JF_CONST)
4867 : {
4868 298 : struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
4869 298 : if (rdesc)
4870 235 : rdesc->refcount = IPA_UNDESCRIBED_USE;
4871 : }
4872 36 : else if (jf->type == IPA_JF_PASS_THROUGH)
4873 5 : ipa_set_controlled_uses (new_root_info,
4874 : jf->value.pass_through.formal_id,
4875 : IPA_UNDESCRIBED_USE);
4876 : }
4877 : }
4878 :
4879 : /* Update jump functions and call note functions on inlining the call site CS.
4880 : CS is expected to lead to a node already cloned by
4881 : cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
4882 : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
4883 : created. */
4884 :
4885 : bool
4886 3917843 : ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
4887 : vec<cgraph_edge *> *new_edges)
4888 : {
4889 3917843 : bool changed;
4890 : /* Do nothing if the preparation phase has not been carried out yet
4891 : (i.e. during early inlining). */
4892 3917843 : if (!ipa_node_params_sum)
4893 : return false;
4894 940377 : gcc_assert (ipa_edge_args_sum);
4895 :
4896 940377 : propagate_controlled_uses (cs);
4897 940377 : changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
4898 940377 : ipa_node_params_sum->remove (cs->callee);
4899 :
4900 940377 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4901 940377 : if (args)
4902 : {
4903 938616 : bool ok = true;
4904 938616 : if (args->jump_functions)
4905 : {
4906 : struct ipa_jump_func *jf;
4907 : int i;
4908 2621186 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
4909 1795014 : if (jf->type == IPA_JF_CONST
4910 1795014 : && ipa_get_jf_constant_rdesc (jf))
4911 : {
4912 : ok = false;
4913 : break;
4914 : }
4915 : }
4916 884555 : if (ok)
4917 880233 : ipa_edge_args_sum->remove (cs);
4918 : }
4919 940377 : if (ipcp_transformation_sum)
4920 657679 : ipcp_transformation_sum->remove (cs->callee);
4921 :
4922 : return changed;
4923 : }
4924 :
4925 : /* Ensure that array of edge arguments infos is big enough to accommodate a
4926 : structure for all edges and reallocates it if not. Also, allocate
4927 : associated hash tables is they do not already exist. */
4928 :
4929 : void
4930 4869819 : ipa_check_create_edge_args (void)
4931 : {
4932 4869819 : if (!ipa_edge_args_sum)
4933 244871 : ipa_edge_args_sum
4934 244871 : = (new (ggc_alloc_no_dtor<ipa_edge_args_sum_t> ())
4935 244871 : ipa_edge_args_sum_t (symtab, true));
4936 4869819 : if (!ipa_vr_hash_table)
4937 171641 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4938 4869819 : }
4939 :
4940 : /* Free all ipa_edge structures. */
4941 :
4942 : void
4943 465005 : ipa_free_all_edge_args (void)
4944 : {
4945 465005 : if (!ipa_edge_args_sum)
4946 : return;
4947 :
4948 232558 : ggc_delete (ipa_edge_args_sum);
4949 232558 : ipa_edge_args_sum = NULL;
4950 : }
4951 :
4952 : /* Free all ipa_node_params structures. */
4953 :
4954 : void
4955 5385582 : ipa_free_all_node_params (void)
4956 : {
4957 5385582 : if (ipa_node_params_sum)
4958 5153135 : ggc_delete (ipa_node_params_sum);
4959 5385582 : ipa_node_params_sum = NULL;
4960 5385582 : }
4961 :
4962 : /* Initialize IPA CP transformation summary and also allocate any necessary hash
4963 : tables if they do not already exist. */
4964 :
4965 : void
4966 93291 : ipcp_transformation_initialize (void)
4967 : {
4968 93291 : if (!ipa_vr_hash_table)
4969 1889 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4970 93291 : if (ipcp_transformation_sum == NULL)
4971 : {
4972 19482 : ipcp_transformation_sum = ipcp_transformation_t::create_ggc (symtab);
4973 19482 : ipcp_transformation_sum->disable_insertion_hook ();
4974 : }
4975 93291 : }
4976 :
4977 : /* Release the IPA CP transformation summary. */
4978 :
4979 : void
4980 259439 : ipcp_free_transformation_sum (void)
4981 : {
4982 259439 : if (!ipcp_transformation_sum)
4983 : return;
4984 :
4985 19473 : ipcp_transformation_sum->~function_summary<ipcp_transformation *> ();
4986 19473 : ggc_free (ipcp_transformation_sum);
4987 19473 : ipcp_transformation_sum = NULL;
4988 : }
4989 :
4990 : /* Set the aggregate replacements of NODE to be AGGVALS. */
4991 :
4992 : void
4993 21316 : ipa_set_node_agg_value_chain (struct cgraph_node *node,
4994 : vec<ipa_argagg_value, va_gc> *aggs)
4995 : {
4996 21316 : ipcp_transformation_initialize ();
4997 21316 : ipcp_transformation *s = ipcp_transformation_sum->get_create (node);
4998 21316 : s->m_agg_values = aggs;
4999 21316 : }
5000 :
5001 : /* Hook that is called by cgraph.cc when an edge is removed. Adjust reference
5002 : count data structures accordingly. */
5003 :
5004 : void
5005 0 : ipa_edge_args_sum_t::remove (cgraph_edge *cs, ipa_edge_args *args)
5006 : {
5007 0 : if (args->jump_functions)
5008 : {
5009 : struct ipa_jump_func *jf;
5010 : int i;
5011 0 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
5012 : {
5013 0 : struct ipa_cst_ref_desc *rdesc;
5014 0 : try_decrement_rdesc_refcount (jf);
5015 0 : if (jf->type == IPA_JF_CONST
5016 0 : && (rdesc = ipa_get_jf_constant_rdesc (jf))
5017 0 : && rdesc->cs == cs)
5018 0 : rdesc->cs = NULL;
5019 : }
5020 : }
5021 0 : }
5022 :
5023 : /* Copy information from SRC_JF to DST_JF which correstpond to call graph edges
5024 : SRC and DST. */
5025 :
5026 : static void
5027 2828579 : ipa_duplicate_jump_function (cgraph_edge *src, cgraph_edge *dst,
5028 : ipa_jump_func *src_jf, ipa_jump_func *dst_jf)
5029 : {
5030 2828579 : dst_jf->agg.items = vec_safe_copy (src_jf->agg.items);
5031 2828579 : dst_jf->agg.by_ref = src_jf->agg.by_ref;
5032 :
5033 : /* We can avoid calling ipa_set_jfunc_vr since it would only look up the
5034 : place in the hash_table where the source m_vr resides. */
5035 2828579 : dst_jf->m_vr = src_jf->m_vr;
5036 :
5037 2828579 : if (src_jf->type == IPA_JF_CONST)
5038 : {
5039 893060 : ipa_set_jf_cst_copy (dst_jf, src_jf);
5040 893060 : struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
5041 :
5042 893060 : if (!src_rdesc)
5043 761237 : dst_jf->value.constant.rdesc = NULL;
5044 131823 : else if (src->caller == dst->caller)
5045 : {
5046 : /* Creation of a speculative edge. If the source edge is the one
5047 : grabbing a reference, we must create a new (duplicate)
5048 : reference description. Otherwise they refer to the same
5049 : description corresponding to a reference taken in a function
5050 : src->caller is inlined to. In that case we just must
5051 : increment the refcount. */
5052 37 : if (src_rdesc->cs == src)
5053 : {
5054 37 : symtab_node *n = symtab_node_for_jfunc (src_jf);
5055 37 : gcc_checking_assert (n);
5056 37 : ipa_ref *ref
5057 37 : = src->caller->find_reference (n, src->call_stmt,
5058 : src->lto_stmt_uid,
5059 : IPA_REF_ADDR);
5060 37 : gcc_checking_assert (ref);
5061 37 : dst->caller->clone_reference (ref, ref->stmt);
5062 :
5063 37 : ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
5064 37 : dst_rdesc->cs = dst;
5065 37 : dst_rdesc->refcount = src_rdesc->refcount;
5066 37 : dst_rdesc->next_duplicate = NULL;
5067 37 : dst_jf->value.constant.rdesc = dst_rdesc;
5068 : }
5069 : else
5070 : {
5071 0 : src_rdesc->refcount++;
5072 0 : dst_jf->value.constant.rdesc = src_rdesc;
5073 : }
5074 : }
5075 131786 : else if (src_rdesc->cs == src)
5076 : {
5077 131515 : struct ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
5078 131515 : dst_rdesc->cs = dst;
5079 131515 : dst_rdesc->refcount = src_rdesc->refcount;
5080 131515 : dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
5081 131515 : src_rdesc->next_duplicate = dst_rdesc;
5082 131515 : dst_jf->value.constant.rdesc = dst_rdesc;
5083 : }
5084 : else
5085 : {
5086 271 : struct ipa_cst_ref_desc *dst_rdesc;
5087 : /* This can happen during inlining, when a JFUNC can refer to a
5088 : reference taken in a function up in the tree of inline clones.
5089 : We need to find the duplicate that refers to our tree of
5090 : inline clones. */
5091 :
5092 271 : gcc_assert (dst->caller->inlined_to);
5093 271 : for (dst_rdesc = src_rdesc->next_duplicate;
5094 271 : dst_rdesc;
5095 0 : dst_rdesc = dst_rdesc->next_duplicate)
5096 : {
5097 271 : struct cgraph_node *top;
5098 2 : top = dst_rdesc->cs->caller->inlined_to
5099 271 : ? dst_rdesc->cs->caller->inlined_to
5100 : : dst_rdesc->cs->caller;
5101 271 : if (dst->caller->inlined_to == top)
5102 : break;
5103 : }
5104 271 : gcc_assert (dst_rdesc);
5105 271 : dst_jf->value.constant.rdesc = dst_rdesc;
5106 : }
5107 : }
5108 1935519 : else if (src_jf->type == IPA_JF_PASS_THROUGH)
5109 : {
5110 710702 : dst_jf->type = IPA_JF_PASS_THROUGH;
5111 710702 : dst_jf->value.pass_through = src_jf->value.pass_through;
5112 710702 : if (src->caller == dst->caller)
5113 : {
5114 10291 : struct cgraph_node *inline_root = dst->caller->inlined_to
5115 10314 : ? dst->caller->inlined_to : dst->caller;
5116 10314 : ipa_node_params *root_info = ipa_node_params_sum->get (inline_root);
5117 10314 : int idx = ipa_get_jf_pass_through_formal_id (dst_jf);
5118 :
5119 10314 : int c = ipa_get_controlled_uses (root_info, idx);
5120 10314 : if (c != IPA_UNDESCRIBED_USE)
5121 : {
5122 2642 : c++;
5123 2642 : ipa_set_controlled_uses (root_info, idx, c);
5124 : }
5125 : }
5126 : }
5127 1224817 : else if (src_jf->type == IPA_JF_ANCESTOR)
5128 : {
5129 96501 : dst_jf->type = IPA_JF_ANCESTOR;
5130 96501 : dst_jf->value.ancestor = src_jf->value.ancestor;
5131 : }
5132 : else
5133 1128316 : gcc_assert (src_jf->type == IPA_JF_UNKNOWN);
5134 2828579 : }
5135 :
5136 : /* Method invoked when an edge is duplicated. Copy ipa_edge_args and adjust
5137 : reference count data strucutres accordingly. */
5138 :
5139 : void
5140 1280295 : ipa_edge_args_sum_t::duplicate (cgraph_edge *src, cgraph_edge *dst,
5141 : ipa_edge_args *old_args, ipa_edge_args *new_args)
5142 : {
5143 1280295 : unsigned int i;
5144 :
5145 1280295 : if (old_args->polymorphic_call_contexts)
5146 142434 : new_args->polymorphic_call_contexts
5147 142434 : = vec_safe_copy (old_args->polymorphic_call_contexts);
5148 :
5149 1280295 : if (!vec_safe_length (old_args->jump_functions))
5150 : {
5151 53614 : new_args->jump_functions = NULL;
5152 53614 : return;
5153 : }
5154 1226681 : vec_safe_grow_cleared (new_args->jump_functions,
5155 1226681 : old_args->jump_functions->length (), true);
5156 :
5157 4040177 : for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
5158 : {
5159 2813496 : struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
5160 2813496 : struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
5161 :
5162 2813496 : ipa_duplicate_jump_function (src, dst, src_jf, dst_jf);
5163 : }
5164 : }
5165 :
5166 : /* Analyze newly added function into callgraph. */
5167 :
5168 : static void
5169 40541 : ipa_add_new_function (cgraph_node *node, void *data ATTRIBUTE_UNUSED)
5170 : {
5171 40541 : if (node->has_gimple_body_p ())
5172 40541 : ipa_analyze_node (node);
5173 40541 : }
5174 :
5175 : /* Hook that is called by summary when a node is duplicated. */
5176 :
5177 : void
5178 773175 : ipa_node_params_t::duplicate(cgraph_node *, cgraph_node *,
5179 : ipa_node_params *old_info,
5180 : ipa_node_params *new_info)
5181 : {
5182 773175 : new_info->descriptors = vec_safe_copy (old_info->descriptors);
5183 773175 : gcc_assert (new_info->lattices.is_empty ());
5184 773175 : new_info->ipcp_orig_node = old_info->ipcp_orig_node;
5185 773175 : new_info->known_csts = old_info->known_csts.copy ();
5186 773175 : new_info->known_contexts = old_info->known_contexts.copy ();
5187 :
5188 773175 : new_info->analysis_done = old_info->analysis_done;
5189 773175 : new_info->node_enqueued = old_info->node_enqueued;
5190 773175 : new_info->versionable = old_info->versionable;
5191 773175 : }
5192 :
5193 : /* Duplication of ipcp transformation summaries. */
5194 :
5195 : void
5196 59038 : ipcp_transformation_t::duplicate(cgraph_node *, cgraph_node *dst,
5197 : ipcp_transformation *src_trans,
5198 : ipcp_transformation *dst_trans)
5199 : {
5200 : /* Avoid redundant work of duplicating vectors we will never use. */
5201 59038 : if (dst->inlined_to)
5202 : return;
5203 8286 : dst_trans->m_agg_values = vec_safe_copy (src_trans->m_agg_values);
5204 15331 : dst_trans->m_vr = vec_safe_copy (src_trans->m_vr);
5205 : }
5206 :
5207 : /* Register our cgraph hooks if they are not already there. */
5208 :
5209 : void
5210 382678 : ipa_register_cgraph_hooks (void)
5211 : {
5212 382678 : ipa_check_create_node_params ();
5213 382678 : ipa_check_create_edge_args ();
5214 :
5215 765356 : function_insertion_hook_holder =
5216 382678 : symtab->add_cgraph_insertion_hook (&ipa_add_new_function, NULL);
5217 382678 : }
5218 :
5219 : /* Unregister our cgraph hooks if they are not already there. */
5220 :
5221 : static void
5222 465005 : ipa_unregister_cgraph_hooks (void)
5223 : {
5224 465005 : if (function_insertion_hook_holder)
5225 232558 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
5226 465005 : function_insertion_hook_holder = NULL;
5227 465005 : }
5228 :
5229 : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
5230 : longer needed after ipa-cp. */
5231 :
5232 : void
5233 129027 : ipa_free_all_structures_after_ipa_cp (void)
5234 : {
5235 129027 : if (!optimize && !in_lto_p)
5236 : {
5237 0 : ipa_free_all_edge_args ();
5238 0 : ipa_free_all_node_params ();
5239 0 : ipcp_sources_pool.release ();
5240 0 : ipcp_cst_values_pool.release ();
5241 0 : ipcp_poly_ctx_values_pool.release ();
5242 0 : ipcp_agg_lattice_pool.release ();
5243 0 : ipa_unregister_cgraph_hooks ();
5244 0 : ipa_refdesc_pool.release ();
5245 : }
5246 129027 : }
5247 :
5248 : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
5249 : longer needed after indirect inlining. */
5250 :
5251 : void
5252 465005 : ipa_free_all_structures_after_iinln (void)
5253 : {
5254 465005 : ipa_free_all_edge_args ();
5255 465005 : ipa_free_all_node_params ();
5256 465005 : ipa_unregister_cgraph_hooks ();
5257 465005 : ipcp_sources_pool.release ();
5258 465005 : ipcp_cst_values_pool.release ();
5259 465005 : ipcp_poly_ctx_values_pool.release ();
5260 465005 : ipcp_agg_lattice_pool.release ();
5261 465005 : ipa_refdesc_pool.release ();
5262 465005 : }
5263 :
5264 : /* Print ipa_tree_map data structures of all functions in the
5265 : callgraph to F. */
5266 :
5267 : void
5268 240 : ipa_print_node_params (FILE *f, struct cgraph_node *node)
5269 : {
5270 240 : int i, count;
5271 240 : class ipa_node_params *info;
5272 :
5273 240 : if (!node->definition)
5274 : return;
5275 181 : info = ipa_node_params_sum->get (node);
5276 181 : fprintf (f, " function %s parameter descriptors:\n", node->dump_name ());
5277 181 : if (!info)
5278 : {
5279 0 : fprintf (f, " no params return\n");
5280 0 : return;
5281 : }
5282 181 : count = ipa_get_param_count (info);
5283 367 : for (i = 0; i < count; i++)
5284 : {
5285 186 : int c;
5286 :
5287 186 : fprintf (f, " ");
5288 186 : ipa_dump_param (f, info, i);
5289 186 : if (ipa_is_param_used (info, i))
5290 178 : fprintf (f, " used");
5291 186 : if (ipa_is_param_used_by_ipa_predicates (info, i))
5292 108 : fprintf (f, " used_by_ipa_predicates");
5293 186 : if (ipa_is_param_used_by_indirect_call (info, i))
5294 10 : fprintf (f, " used_by_indirect_call");
5295 186 : if (ipa_is_param_used_by_polymorphic_call (info, i))
5296 0 : fprintf (f, " used_by_polymorphic_call");
5297 186 : c = ipa_get_controlled_uses (info, i);
5298 186 : if (c == IPA_UNDESCRIBED_USE)
5299 108 : fprintf (f, " undescribed_use");
5300 : else
5301 134 : fprintf (f, " controlled_uses=%i %s", c,
5302 78 : ipa_get_param_load_dereferenced (info, i)
5303 : ? "(load_dereferenced)" : "");
5304 186 : fprintf (f, "\n");
5305 : }
5306 : }
5307 :
5308 : /* Print ipa_tree_map data structures of all functions in the
5309 : callgraph to F. */
5310 :
5311 : void
5312 48 : ipa_print_all_params (FILE * f)
5313 : {
5314 48 : struct cgraph_node *node;
5315 :
5316 48 : fprintf (f, "\nFunction parameters:\n");
5317 274 : FOR_EACH_FUNCTION (node)
5318 226 : ipa_print_node_params (f, node);
5319 48 : }
5320 :
5321 : /* Stream out jump function JUMP_FUNC to OB. */
5322 :
5323 : static void
5324 609143 : ipa_write_jump_function (struct output_block *ob,
5325 : struct ipa_jump_func *jump_func)
5326 : {
5327 609143 : struct ipa_agg_jf_item *item;
5328 609143 : struct bitpack_d bp;
5329 609143 : int i, count;
5330 609143 : int flag = 0;
5331 :
5332 : /* ADDR_EXPRs are very comon IP invariants; save some streamer data
5333 : as well as WPA memory by handling them specially. */
5334 609143 : if (jump_func->type == IPA_JF_CONST
5335 464877 : && TREE_CODE (jump_func->value.constant.value) == ADDR_EXPR)
5336 609143 : flag = 1;
5337 :
5338 609143 : streamer_write_uhwi (ob, jump_func->type * 2 + flag);
5339 609143 : switch (jump_func->type)
5340 : {
5341 : case IPA_JF_UNKNOWN:
5342 : break;
5343 464877 : case IPA_JF_CONST:
5344 464877 : gcc_assert (
5345 : EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
5346 464877 : stream_write_tree (ob,
5347 : flag
5348 : ? TREE_OPERAND (jump_func->value.constant.value, 0)
5349 : : jump_func->value.constant.value, true);
5350 464877 : break;
5351 76855 : case IPA_JF_PASS_THROUGH:
5352 76855 : streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
5353 76855 : if (jump_func->value.pass_through.operation == NOP_EXPR)
5354 : {
5355 75996 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5356 75996 : bp = bitpack_create (ob->main_stream);
5357 75996 : bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
5358 75996 : gcc_assert (!jump_func->value.pass_through.refdesc_decremented);
5359 75996 : streamer_write_bitpack (&bp);
5360 : }
5361 859 : else if (TREE_CODE_CLASS (jump_func->value.pass_through.operation)
5362 : == tcc_unary)
5363 : {
5364 39 : stream_write_tree (ob, jump_func->value.pass_through.op_type, true);
5365 39 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5366 : }
5367 : else
5368 : {
5369 820 : stream_write_tree (ob, jump_func->value.pass_through.op_type, true);
5370 820 : stream_write_tree (ob, jump_func->value.pass_through.operand, true);
5371 820 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5372 : }
5373 : break;
5374 1343 : case IPA_JF_ANCESTOR:
5375 1343 : streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
5376 1343 : streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
5377 1343 : bp = bitpack_create (ob->main_stream);
5378 1343 : bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
5379 1343 : bp_pack_value (&bp, jump_func->value.ancestor.keep_null, 1);
5380 1343 : streamer_write_bitpack (&bp);
5381 1343 : break;
5382 0 : default:
5383 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
5384 : }
5385 :
5386 609143 : count = vec_safe_length (jump_func->agg.items);
5387 609143 : streamer_write_uhwi (ob, count);
5388 609143 : if (count)
5389 : {
5390 3281 : bp = bitpack_create (ob->main_stream);
5391 3281 : bp_pack_value (&bp, jump_func->agg.by_ref, 1);
5392 3281 : streamer_write_bitpack (&bp);
5393 : }
5394 :
5395 615741 : FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
5396 : {
5397 6598 : stream_write_tree (ob, item->type, true);
5398 6598 : streamer_write_uhwi (ob, item->offset);
5399 6598 : streamer_write_uhwi (ob, item->jftype);
5400 6598 : switch (item->jftype)
5401 : {
5402 : case IPA_JF_UNKNOWN:
5403 : break;
5404 6083 : case IPA_JF_CONST:
5405 6083 : stream_write_tree (ob, item->value.constant, true);
5406 6083 : break;
5407 515 : case IPA_JF_PASS_THROUGH:
5408 515 : case IPA_JF_LOAD_AGG:
5409 515 : streamer_write_uhwi (ob, item->value.pass_through.operation);
5410 515 : streamer_write_uhwi (ob, item->value.pass_through.formal_id);
5411 515 : if (item->value.pass_through.operation != NOP_EXPR)
5412 4 : stream_write_tree (ob, item->value.pass_through.op_type, true);
5413 515 : if (TREE_CODE_CLASS (item->value.pass_through.operation)
5414 : != tcc_unary)
5415 4 : stream_write_tree (ob, item->value.pass_through.operand, true);
5416 515 : if (item->jftype == IPA_JF_LOAD_AGG)
5417 : {
5418 79 : stream_write_tree (ob, item->value.load_agg.type, true);
5419 79 : streamer_write_uhwi (ob, item->value.load_agg.offset);
5420 79 : bp = bitpack_create (ob->main_stream);
5421 79 : bp_pack_value (&bp, item->value.load_agg.by_ref, 1);
5422 79 : streamer_write_bitpack (&bp);
5423 : }
5424 : break;
5425 0 : default:
5426 0 : fatal_error (UNKNOWN_LOCATION,
5427 : "invalid jump function in LTO stream");
5428 : }
5429 : }
5430 :
5431 609143 : bp = bitpack_create (ob->main_stream);
5432 609143 : if (jump_func->m_vr)
5433 423346 : jump_func->m_vr->streamer_write (ob);
5434 : else
5435 : {
5436 185797 : bp_pack_value (&bp, false, 1);
5437 185797 : streamer_write_bitpack (&bp);
5438 : }
5439 609143 : }
5440 :
5441 : /* Read in jump function JUMP_FUNC from IB. */
5442 :
5443 : static void
5444 569807 : ipa_read_jump_function (class lto_input_block *ib,
5445 : struct ipa_jump_func *jump_func,
5446 : struct cgraph_edge *cs,
5447 : class data_in *data_in,
5448 : bool prevails)
5449 : {
5450 569807 : enum jump_func_type jftype;
5451 569807 : enum tree_code operation;
5452 569807 : int i, count;
5453 569807 : int val = streamer_read_uhwi (ib);
5454 569807 : bool flag = val & 1;
5455 :
5456 569807 : jftype = (enum jump_func_type) (val / 2);
5457 569807 : switch (jftype)
5458 : {
5459 50600 : case IPA_JF_UNKNOWN:
5460 50600 : ipa_set_jf_unknown (jump_func);
5461 50600 : break;
5462 448522 : case IPA_JF_CONST:
5463 448522 : {
5464 448522 : tree t = stream_read_tree (ib, data_in);
5465 448522 : if (flag && prevails)
5466 161595 : t = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
5467 448522 : ipa_set_jf_constant (jump_func, t, cs);
5468 : }
5469 448522 : break;
5470 70011 : case IPA_JF_PASS_THROUGH:
5471 70011 : operation = (enum tree_code) streamer_read_uhwi (ib);
5472 70011 : if (operation == NOP_EXPR)
5473 : {
5474 69439 : int formal_id = streamer_read_uhwi (ib);
5475 69439 : struct bitpack_d bp = streamer_read_bitpack (ib);
5476 69439 : bool agg_preserved = bp_unpack_value (&bp, 1);
5477 69439 : ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved);
5478 : }
5479 572 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
5480 : {
5481 19 : tree op_type = stream_read_tree (ib, data_in);
5482 19 : int formal_id = streamer_read_uhwi (ib);
5483 19 : ipa_set_jf_unary_pass_through (jump_func, formal_id, operation,
5484 : op_type);
5485 : }
5486 : else
5487 : {
5488 553 : tree op_type = stream_read_tree (ib, data_in);
5489 553 : tree operand = stream_read_tree (ib, data_in);
5490 553 : int formal_id = streamer_read_uhwi (ib);
5491 553 : ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
5492 : operation, op_type);
5493 : }
5494 : break;
5495 674 : case IPA_JF_ANCESTOR:
5496 674 : {
5497 674 : HOST_WIDE_INT offset = streamer_read_uhwi (ib);
5498 674 : int formal_id = streamer_read_uhwi (ib);
5499 674 : struct bitpack_d bp = streamer_read_bitpack (ib);
5500 674 : bool agg_preserved = bp_unpack_value (&bp, 1);
5501 674 : bool keep_null = bp_unpack_value (&bp, 1);
5502 674 : ipa_set_ancestor_jf (jump_func, offset, formal_id, agg_preserved,
5503 : keep_null);
5504 674 : break;
5505 : }
5506 0 : default:
5507 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
5508 : }
5509 :
5510 569807 : count = streamer_read_uhwi (ib);
5511 569807 : if (prevails)
5512 : {
5513 569801 : jump_func->agg.items = NULL;
5514 569801 : vec_safe_reserve (jump_func->agg.items, count, true);
5515 : }
5516 569807 : if (count)
5517 : {
5518 2925 : struct bitpack_d bp = streamer_read_bitpack (ib);
5519 2925 : jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
5520 : }
5521 575795 : for (i = 0; i < count; i++)
5522 : {
5523 5988 : struct ipa_agg_jf_item item;
5524 5988 : item.type = stream_read_tree (ib, data_in);
5525 5988 : item.offset = streamer_read_uhwi (ib);
5526 5988 : item.jftype = (enum jump_func_type) streamer_read_uhwi (ib);
5527 :
5528 5988 : switch (item.jftype)
5529 : {
5530 : case IPA_JF_UNKNOWN:
5531 : break;
5532 5553 : case IPA_JF_CONST:
5533 5553 : item.value.constant = stream_read_tree (ib, data_in);
5534 5553 : break;
5535 435 : case IPA_JF_PASS_THROUGH:
5536 435 : case IPA_JF_LOAD_AGG:
5537 435 : operation = (enum tree_code) streamer_read_uhwi (ib);
5538 435 : item.value.pass_through.operation = operation;
5539 435 : item.value.pass_through.formal_id = streamer_read_uhwi (ib);
5540 435 : if (operation != NOP_EXPR)
5541 0 : item.value.pass_through.op_type = stream_read_tree (ib, data_in);
5542 : else
5543 435 : item.value.pass_through.op_type = NULL_TREE;
5544 435 : if (TREE_CODE_CLASS (operation) == tcc_unary)
5545 435 : item.value.pass_through.operand = NULL_TREE;
5546 : else
5547 0 : item.value.pass_through.operand = stream_read_tree (ib, data_in);
5548 435 : if (item.jftype == IPA_JF_LOAD_AGG)
5549 : {
5550 43 : struct bitpack_d bp;
5551 43 : item.value.load_agg.type = stream_read_tree (ib, data_in);
5552 43 : item.value.load_agg.offset = streamer_read_uhwi (ib);
5553 43 : bp = streamer_read_bitpack (ib);
5554 43 : item.value.load_agg.by_ref = bp_unpack_value (&bp, 1);
5555 : }
5556 : break;
5557 0 : default:
5558 0 : fatal_error (UNKNOWN_LOCATION,
5559 : "invalid jump function in LTO stream");
5560 : }
5561 5988 : if (prevails)
5562 5988 : jump_func->agg.items->quick_push (item);
5563 : }
5564 :
5565 569807 : ipa_vr vr;
5566 569807 : vr.streamer_read (ib, data_in);
5567 569807 : if (vr.known_p ())
5568 : {
5569 398419 : if (prevails)
5570 398413 : ipa_set_jfunc_vr (jump_func, vr);
5571 : }
5572 : else
5573 171388 : jump_func->m_vr = NULL;
5574 569807 : }
5575 :
5576 : /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
5577 : relevant to indirect inlining to OB. */
5578 :
5579 : static void
5580 2610 : ipa_write_indirect_edge_info (struct output_block *ob,
5581 : struct cgraph_edge *cs)
5582 : {
5583 2610 : struct bitpack_d bp;
5584 :
5585 2610 : bp = bitpack_create (ob->main_stream);
5586 2610 : bp_pack_enum (&bp, cgraph_indirect_info_kind, CIIK_N_KINDS,
5587 : cs->indirect_info->kind);
5588 2610 : streamer_write_bitpack (&bp);
5589 :
5590 2610 : if (cgraph_polymorphic_indirect_info *pii
5591 2610 : = dyn_cast <cgraph_polymorphic_indirect_info *> (cs->indirect_info))
5592 : {
5593 1023 : bp = bitpack_create (ob->main_stream);
5594 1023 : bp_pack_value (&bp, pii->vptr_changed, 1);
5595 1023 : streamer_write_bitpack (&bp);
5596 :
5597 1023 : streamer_write_hwi (ob, pii->param_index);
5598 1023 : pii->context.stream_out (ob);
5599 1023 : streamer_write_hwi (ob, pii->otr_token);
5600 1023 : stream_write_tree (ob, pii->otr_type, true);
5601 1023 : streamer_write_hwi (ob, pii->offset);
5602 : }
5603 1587 : else if (cgraph_simple_indirect_info *sii
5604 1587 : = dyn_cast <cgraph_simple_indirect_info *> (cs->indirect_info))
5605 : {
5606 1568 : bp = bitpack_create (ob->main_stream);
5607 1568 : bp_pack_value (&bp, sii->agg_contents, 1);
5608 1568 : bp_pack_value (&bp, sii->member_ptr, 1);
5609 1568 : bp_pack_value (&bp, sii->fnptr_loaded_from_record, 1);
5610 1568 : bp_pack_value (&bp, sii->by_ref, 1);
5611 1568 : bp_pack_value (&bp, sii->guaranteed_unmodified, 1);
5612 1568 : streamer_write_bitpack (&bp);
5613 :
5614 1568 : streamer_write_hwi (ob, sii->param_index);
5615 1568 : if (sii->agg_contents)
5616 55 : streamer_write_hwi (ob, sii->offset);
5617 : else
5618 1513 : gcc_assert (sii->offset == 0);
5619 1568 : if (sii->fnptr_loaded_from_record)
5620 : {
5621 128 : stream_write_tree (ob, sii->rec_type, true);
5622 128 : streamer_write_uhwi (ob, sii->fld_offset);
5623 : }
5624 : }
5625 : else
5626 19 : gcc_assert (cs->indirect_info->param_index == -1);
5627 2610 : }
5628 :
5629 : /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
5630 : relevant to indirect inlining from IB. */
5631 :
5632 : static void
5633 1417 : ipa_read_indirect_edge_info (class lto_input_block *ib,
5634 : class data_in *data_in,
5635 : struct cgraph_edge *cs,
5636 : class ipa_node_params *info)
5637 : {
5638 1417 : struct bitpack_d bp;
5639 :
5640 1417 : bp = streamer_read_bitpack (ib);
5641 1417 : enum cgraph_indirect_info_kind ii_kind
5642 1417 : = bp_unpack_enum (&bp, cgraph_indirect_info_kind, CIIK_N_KINDS);
5643 1417 : gcc_assert (ii_kind == cs->indirect_info->kind);
5644 :
5645 1417 : if (cgraph_polymorphic_indirect_info *pii
5646 1417 : = dyn_cast <cgraph_polymorphic_indirect_info *> (cs->indirect_info))
5647 : {
5648 93 : bp = streamer_read_bitpack (ib);
5649 93 : pii->vptr_changed = bp_unpack_value (&bp, 1);
5650 :
5651 93 : pii->param_index = (int) streamer_read_hwi (ib);
5652 93 : pii->context.stream_in (ib, data_in);
5653 93 : pii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
5654 93 : pii->otr_type = stream_read_tree (ib, data_in);
5655 93 : pii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
5656 :
5657 93 : if (info && pii->param_index >= 0)
5658 : {
5659 70 : ipa_set_param_used_by_polymorphic_call (info, pii->param_index, true);
5660 70 : ipa_set_param_used_by_indirect_call (info, pii->param_index, true);
5661 : }
5662 : }
5663 1324 : else if (cgraph_simple_indirect_info *sii
5664 1324 : = dyn_cast <cgraph_simple_indirect_info *> (cs->indirect_info))
5665 : {
5666 1319 : bp = streamer_read_bitpack (ib);
5667 1319 : sii->agg_contents = bp_unpack_value (&bp, 1);
5668 1319 : sii->member_ptr = bp_unpack_value (&bp, 1);
5669 1319 : sii->fnptr_loaded_from_record = bp_unpack_value (&bp, 1);
5670 1319 : sii->by_ref = bp_unpack_value (&bp, 1);
5671 1319 : sii->guaranteed_unmodified = bp_unpack_value (&bp, 1);
5672 :
5673 1319 : sii->param_index = (int) streamer_read_hwi (ib);
5674 1319 : if (sii->agg_contents)
5675 31 : sii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
5676 : else
5677 1288 : sii->offset = 0;
5678 1319 : if (sii->fnptr_loaded_from_record)
5679 : {
5680 66 : sii->rec_type = stream_read_tree (ib, data_in);
5681 66 : sii->fld_offset = (unsigned) streamer_read_uhwi (ib);
5682 : }
5683 1319 : if (info && sii->param_index >= 0)
5684 263 : ipa_set_param_used_by_indirect_call (info, sii->param_index, true);
5685 : }
5686 : else
5687 5 : cs->indirect_info->param_index = -1;
5688 1417 : }
5689 :
5690 : /* Stream out NODE info to OB. */
5691 :
5692 : static void
5693 93065 : ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
5694 : {
5695 93065 : int node_ref;
5696 93065 : lto_symtab_encoder_t encoder;
5697 93065 : ipa_node_params *info = ipa_node_params_sum->get (node);
5698 93065 : int j;
5699 93065 : struct cgraph_edge *e;
5700 93065 : struct bitpack_d bp;
5701 :
5702 93065 : encoder = ob->decl_state->symtab_node_encoder;
5703 93065 : node_ref = lto_symtab_encoder_encode (encoder, node);
5704 93065 : streamer_write_uhwi (ob, node_ref);
5705 :
5706 93065 : streamer_write_uhwi (ob, ipa_get_param_count (info));
5707 450968 : for (j = 0; j < ipa_get_param_count (info); j++)
5708 101079 : streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
5709 93065 : bp = bitpack_create (ob->main_stream);
5710 93065 : gcc_assert (info->analysis_done
5711 : || ipa_get_param_count (info) == 0);
5712 93065 : gcc_assert (!info->node_enqueued);
5713 93065 : gcc_assert (!info->ipcp_orig_node);
5714 357903 : for (j = 0; j < ipa_get_param_count (info); j++)
5715 : {
5716 : /* TODO: We could just not stream the bit in the undescribed case. */
5717 101079 : bool d = (ipa_get_controlled_uses (info, j) != IPA_UNDESCRIBED_USE)
5718 101079 : ? ipa_get_param_load_dereferenced (info, j) : true;
5719 101079 : bp_pack_value (&bp, d, 1);
5720 101079 : bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
5721 : }
5722 93065 : streamer_write_bitpack (&bp);
5723 450968 : for (j = 0; j < ipa_get_param_count (info); j++)
5724 : {
5725 101079 : streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
5726 101079 : stream_write_tree (ob, ipa_get_type (info, j), true);
5727 : }
5728 456355 : for (e = node->callees; e; e = e->next_callee)
5729 : {
5730 363290 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5731 :
5732 363290 : if (!args)
5733 : {
5734 806 : streamer_write_uhwi (ob, 0);
5735 806 : continue;
5736 : }
5737 :
5738 362484 : streamer_write_uhwi (ob,
5739 362484 : ipa_get_cs_argument_count (args) * 2
5740 362484 : + (args->polymorphic_call_contexts != NULL));
5741 2190319 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5742 : {
5743 606017 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5744 606017 : if (args->polymorphic_call_contexts != NULL)
5745 2422 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5746 : }
5747 : }
5748 95675 : for (e = node->indirect_calls; e; e = e->next_callee)
5749 : {
5750 2610 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5751 2610 : if (!args)
5752 6 : streamer_write_uhwi (ob, 0);
5753 : else
5754 : {
5755 2604 : streamer_write_uhwi (ob,
5756 2604 : ipa_get_cs_argument_count (args) * 2
5757 2604 : + (args->polymorphic_call_contexts != NULL));
5758 13922 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5759 : {
5760 3126 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5761 3126 : if (args->polymorphic_call_contexts != NULL)
5762 1321 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5763 : }
5764 : }
5765 2610 : ipa_write_indirect_edge_info (ob, e);
5766 : }
5767 93065 : }
5768 :
5769 : /* Stream in edge E from IB. */
5770 :
5771 : static void
5772 335018 : ipa_read_edge_info (class lto_input_block *ib,
5773 : class data_in *data_in,
5774 : struct cgraph_edge *e, bool prevails)
5775 : {
5776 335018 : int count = streamer_read_uhwi (ib);
5777 335018 : bool contexts_computed = count & 1;
5778 :
5779 335018 : count /= 2;
5780 335018 : if (!count)
5781 : return;
5782 234371 : if (prevails
5783 234371 : && (e->possibly_call_in_translation_unit_p ()
5784 : /* Also stream in jump functions to builtins in hope that they
5785 : will get fnspecs. */
5786 115783 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
5787 : {
5788 223155 : ipa_edge_args *args = ipa_edge_args_sum->get_create (e);
5789 223155 : vec_safe_grow_cleared (args->jump_functions, count, true);
5790 223155 : if (contexts_computed)
5791 636 : vec_safe_grow_cleared (args->polymorphic_call_contexts, count, true);
5792 774991 : for (int k = 0; k < count; k++)
5793 : {
5794 551836 : ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
5795 : data_in, prevails);
5796 551836 : if (contexts_computed)
5797 987 : ipa_get_ith_polymorhic_call_context (args, k)->stream_in
5798 987 : (ib, data_in);
5799 : }
5800 : }
5801 : else
5802 : {
5803 29187 : for (int k = 0; k < count; k++)
5804 : {
5805 17971 : struct ipa_jump_func dummy;
5806 17971 : ipa_read_jump_function (ib, &dummy, e,
5807 : data_in, prevails);
5808 17971 : if (contexts_computed)
5809 : {
5810 445 : class ipa_polymorphic_call_context ctx;
5811 445 : ctx.stream_in (ib, data_in);
5812 : }
5813 : }
5814 : }
5815 : }
5816 :
5817 : /* Stream in NODE info from IB. */
5818 :
5819 : static void
5820 77648 : ipa_read_node_info (class lto_input_block *ib, struct cgraph_node *node,
5821 : class data_in *data_in)
5822 : {
5823 77648 : int k;
5824 77648 : struct cgraph_edge *e;
5825 77648 : struct bitpack_d bp;
5826 77648 : bool prevails = node->prevailing_p ();
5827 77648 : ipa_node_params *info
5828 77648 : = prevails ? ipa_node_params_sum->get_create (node) : NULL;
5829 :
5830 77648 : int param_count = streamer_read_uhwi (ib);
5831 77648 : if (prevails)
5832 : {
5833 77630 : ipa_alloc_node_params (node, param_count);
5834 233526 : for (k = 0; k < param_count; k++)
5835 78266 : (*info->descriptors)[k].move_cost = streamer_read_uhwi (ib);
5836 77630 : if (ipa_get_param_count (info) != 0)
5837 53162 : info->analysis_done = true;
5838 77630 : info->node_enqueued = false;
5839 : }
5840 : else
5841 27 : for (k = 0; k < param_count; k++)
5842 9 : streamer_read_uhwi (ib);
5843 :
5844 77648 : bp = streamer_read_bitpack (ib);
5845 155923 : for (k = 0; k < param_count; k++)
5846 : {
5847 78275 : bool load_dereferenced = bp_unpack_value (&bp, 1);
5848 78275 : bool used = bp_unpack_value (&bp, 1);
5849 :
5850 78275 : if (prevails)
5851 : {
5852 78266 : ipa_set_param_load_dereferenced (info, k, load_dereferenced);
5853 78266 : ipa_set_param_used (info, k, used);
5854 : }
5855 : }
5856 155923 : for (k = 0; k < param_count; k++)
5857 : {
5858 78275 : int nuses = streamer_read_hwi (ib);
5859 78275 : tree type = stream_read_tree (ib, data_in);
5860 :
5861 78275 : if (prevails)
5862 : {
5863 78266 : ipa_set_controlled_uses (info, k, nuses);
5864 78266 : (*info->descriptors)[k].decl_or_type = type;
5865 : }
5866 : }
5867 411249 : for (e = node->callees; e; e = e->next_callee)
5868 333601 : ipa_read_edge_info (ib, data_in, e, prevails);
5869 79065 : for (e = node->indirect_calls; e; e = e->next_callee)
5870 : {
5871 1417 : ipa_read_edge_info (ib, data_in, e, prevails);
5872 1417 : ipa_read_indirect_edge_info (ib, data_in, e, info);
5873 : }
5874 77648 : }
5875 :
5876 : /* Stream out ipa_return_summary. */
5877 : static void
5878 31369 : ipa_write_return_summaries (output_block *ob)
5879 : {
5880 31369 : if (!ipa_return_value_sum)
5881 : {
5882 14862 : streamer_write_uhwi (ob, 0);
5883 14862 : return;
5884 : }
5885 :
5886 16507 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5887 16507 : unsigned int count = 0;
5888 438426 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5889 : {
5890 202706 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5891 405412 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5892 167033 : ipa_return_value_summary *v;
5893 :
5894 167033 : if (cnode && cnode->definition && !cnode->alias
5895 122996 : && (v = ipa_return_value_sum->get (cnode))
5896 25215 : && v->vr)
5897 25215 : count++;
5898 : }
5899 16507 : streamer_write_uhwi (ob, count);
5900 :
5901 438426 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5902 : {
5903 202706 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5904 405412 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5905 167033 : ipa_return_value_summary *v;
5906 :
5907 167033 : if (cnode && cnode->definition && !cnode->alias
5908 122996 : && (v = ipa_return_value_sum->get (cnode))
5909 25215 : && v->vr)
5910 : {
5911 25215 : streamer_write_uhwi
5912 25215 : (ob,
5913 25215 : lto_symtab_encoder_encode (encoder, cnode));
5914 25215 : v->vr->streamer_write (ob);
5915 : }
5916 : }
5917 : }
5918 :
5919 : /* Write jump functions for nodes in SET. */
5920 :
5921 : void
5922 23172 : ipa_prop_write_jump_functions (void)
5923 : {
5924 23172 : struct output_block *ob;
5925 23172 : unsigned int count = 0;
5926 23172 : lto_symtab_encoder_iterator lsei;
5927 23172 : lto_symtab_encoder_t encoder;
5928 :
5929 23172 : if (!ipa_node_params_sum || !ipa_edge_args_sum)
5930 0 : return;
5931 :
5932 23172 : ob = create_output_block (LTO_section_jump_functions);
5933 23172 : encoder = ob->decl_state->symtab_node_encoder;
5934 23172 : ob->symbol = NULL;
5935 128203 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5936 105031 : lsei_next_function_in_partition (&lsei))
5937 : {
5938 105031 : cgraph_node *node = lsei_cgraph_node (lsei);
5939 105031 : if (node->has_gimple_body_p ()
5940 105031 : && ipa_node_params_sum->get (node) != NULL)
5941 93065 : count++;
5942 : }
5943 :
5944 23172 : streamer_write_uhwi (ob, count);
5945 :
5946 : /* Process all of the functions. */
5947 128203 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5948 105031 : lsei_next_function_in_partition (&lsei))
5949 : {
5950 105031 : cgraph_node *node = lsei_cgraph_node (lsei);
5951 105031 : if (node->has_gimple_body_p ()
5952 105031 : && ipa_node_params_sum->get (node) != NULL)
5953 93065 : ipa_write_node_info (ob, node);
5954 : }
5955 23172 : ipa_write_return_summaries (ob);
5956 :
5957 23172 : if (noted_fnptrs_in_records)
5958 : {
5959 340 : count = 0;
5960 1069 : for (auto iter = noted_fnptrs_in_records->begin ();
5961 1069 : iter != noted_fnptrs_in_records->end();
5962 729 : ++iter)
5963 729 : if ((*iter)->fn)
5964 721 : count++;
5965 340 : streamer_write_uhwi (ob, count);
5966 :
5967 1069 : for (auto iter = noted_fnptrs_in_records->begin ();
5968 1409 : iter != noted_fnptrs_in_records->end();
5969 729 : ++iter)
5970 729 : if ((*iter)->fn)
5971 : {
5972 721 : stream_write_tree (ob, (*iter)->rec_type, true);
5973 721 : stream_write_tree (ob, (*iter)->fn, true);
5974 721 : streamer_write_uhwi (ob, (*iter)->fld_offset);
5975 : }
5976 : }
5977 : else
5978 22832 : streamer_write_uhwi (ob, 0);
5979 :
5980 23172 : produce_asm (ob);
5981 23172 : destroy_output_block (ob);
5982 : }
5983 :
5984 : /* Record that return value range of N is VAL. */
5985 :
5986 : static void
5987 746559 : ipa_record_return_value_range_1 (cgraph_node *n, value_range val)
5988 : {
5989 746559 : if (!ipa_return_value_sum)
5990 : {
5991 86934 : if (!ipa_vr_hash_table)
5992 76173 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
5993 86934 : ipa_return_value_sum = new (ggc_alloc_no_dtor <ipa_return_value_sum_t> ())
5994 86934 : ipa_return_value_sum_t (symtab, true);
5995 86934 : ipa_return_value_sum->disable_insertion_hook ();
5996 : }
5997 746559 : ipa_return_value_sum->get_create (n)->vr = ipa_get_value_range (val);
5998 746559 : if (dump_file && (dump_flags & TDF_DETAILS))
5999 : {
6000 21 : fprintf (dump_file, "Recording return range of %s:", n->dump_name ());
6001 21 : val.dump (dump_file);
6002 21 : fprintf (dump_file, "\n");
6003 : }
6004 746559 : }
6005 :
6006 : /* Stream out ipa_return_summary. */
6007 : static void
6008 21499 : ipa_read_return_summaries (lto_input_block *ib,
6009 : struct lto_file_decl_data *file_data,
6010 : class data_in *data_in)
6011 : {
6012 21499 : unsigned int f_count = streamer_read_uhwi (ib);
6013 43180 : for (unsigned int i = 0; i < f_count; i++)
6014 : {
6015 21681 : unsigned int index = streamer_read_uhwi (ib);
6016 21681 : lto_symtab_encoder_t encoder = file_data->symtab_node_encoder;
6017 21681 : struct cgraph_node *node
6018 : = dyn_cast <cgraph_node *>
6019 21681 : (lto_symtab_encoder_deref (encoder, index));
6020 21681 : ipa_vr rvr;
6021 21681 : rvr.streamer_read (ib, data_in);
6022 21681 : if (node->prevailing_p ())
6023 : {
6024 21679 : value_range tmp;
6025 21679 : rvr.get_vrange (tmp);
6026 21679 : ipa_record_return_value_range_1 (node, tmp);
6027 21679 : }
6028 : }
6029 21499 : }
6030 :
6031 : /* Read section in file FILE_DATA of length LEN with data DATA. */
6032 :
6033 : static void
6034 13302 : ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
6035 : size_t len)
6036 : {
6037 13302 : const struct lto_function_header *header =
6038 : (const struct lto_function_header *) data;
6039 13302 : const int cfg_offset = sizeof (struct lto_function_header);
6040 13302 : const int main_offset = cfg_offset + header->cfg_size;
6041 13302 : const int string_offset = main_offset + header->main_size;
6042 13302 : class data_in *data_in;
6043 13302 : unsigned int i;
6044 13302 : unsigned int count;
6045 :
6046 13302 : lto_input_block ib_main ((const char *) data + main_offset,
6047 13302 : header->main_size, file_data);
6048 :
6049 13302 : data_in =
6050 26604 : lto_data_in_create (file_data, (const char *) data + string_offset,
6051 13302 : header->string_size, vNULL);
6052 13302 : count = streamer_read_uhwi (&ib_main);
6053 :
6054 90950 : for (i = 0; i < count; i++)
6055 : {
6056 77648 : unsigned int index;
6057 77648 : struct cgraph_node *node;
6058 77648 : lto_symtab_encoder_t encoder;
6059 :
6060 77648 : index = streamer_read_uhwi (&ib_main);
6061 77648 : encoder = file_data->symtab_node_encoder;
6062 77648 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
6063 : index));
6064 77648 : gcc_assert (node->definition);
6065 77648 : ipa_read_node_info (&ib_main, node, data_in);
6066 : }
6067 13302 : ipa_read_return_summaries (&ib_main, file_data, data_in);
6068 :
6069 13302 : count = streamer_read_uhwi (&ib_main);
6070 13970 : for (i = 0; i < count; i++)
6071 : {
6072 668 : tree rec_type = stream_read_tree (&ib_main, data_in);
6073 668 : tree fn = stream_read_tree (&ib_main, data_in);
6074 668 : unsigned fld_offset = (unsigned) streamer_read_uhwi (&ib_main);
6075 668 : note_fnptr_in_record (rec_type, fld_offset, fn);
6076 : }
6077 :
6078 13302 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
6079 : len);
6080 13302 : lto_data_in_delete (data_in);
6081 13302 : }
6082 :
6083 : /* Read ipcp jump functions. */
6084 :
6085 : void
6086 12247 : ipa_prop_read_jump_functions (void)
6087 : {
6088 12247 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
6089 12247 : struct lto_file_decl_data *file_data;
6090 12247 : unsigned int j = 0;
6091 :
6092 12247 : ipa_check_create_node_params ();
6093 12247 : ipa_check_create_edge_args ();
6094 12247 : ipa_register_cgraph_hooks ();
6095 :
6096 37796 : while ((file_data = file_data_vec[j++]))
6097 : {
6098 13302 : size_t len;
6099 13302 : const char *data
6100 13302 : = lto_get_summary_section_data (file_data, LTO_section_jump_functions,
6101 : &len);
6102 13302 : if (data)
6103 13302 : ipa_prop_read_section (file_data, data, len);
6104 : }
6105 12247 : }
6106 :
6107 : /* Return true if the IPA-CP transformation summary TS is non-NULL and contains
6108 : useful info. */
6109 : static bool
6110 166174 : useful_ipcp_transformation_info_p (ipcp_transformation *ts)
6111 : {
6112 166174 : if (!ts)
6113 : return false;
6114 24284 : if (!vec_safe_is_empty (ts->m_agg_values)
6115 23884 : || !vec_safe_is_empty (ts->m_vr))
6116 24000 : return true;
6117 : return false;
6118 : }
6119 :
6120 : /* Write into OB IPA-CP transfromation summary TS describing NODE. */
6121 :
6122 : void
6123 11982 : write_ipcp_transformation_info (output_block *ob, cgraph_node *node,
6124 : ipcp_transformation *ts)
6125 : {
6126 11982 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
6127 11982 : int node_ref = lto_symtab_encoder_encode (encoder, node);
6128 11982 : streamer_write_uhwi (ob, node_ref);
6129 :
6130 12179 : streamer_write_uhwi (ob, vec_safe_length (ts->m_agg_values));
6131 13607 : for (const ipa_argagg_value &av : ts->m_agg_values)
6132 : {
6133 1231 : struct bitpack_d bp;
6134 :
6135 1231 : stream_write_tree (ob, av.value, true);
6136 1231 : streamer_write_uhwi (ob, av.unit_offset);
6137 1231 : streamer_write_uhwi (ob, av.index);
6138 :
6139 1231 : bp = bitpack_create (ob->main_stream);
6140 1231 : bp_pack_value (&bp, av.by_ref, 1);
6141 1231 : bp_pack_value (&bp, av.killed, 1);
6142 1231 : streamer_write_bitpack (&bp);
6143 : }
6144 :
6145 : /* If all instances of this node are inlined, ipcp info is not useful. */
6146 11982 : if (!lto_symtab_encoder_only_for_inlining_p (encoder, node))
6147 : {
6148 21744 : streamer_write_uhwi (ob, vec_safe_length (ts->m_vr));
6149 53061 : for (const ipa_vr &parm_vr : ts->m_vr)
6150 20447 : parm_vr.streamer_write (ob);
6151 : }
6152 : else
6153 1108 : streamer_write_uhwi (ob, 0);
6154 11982 : }
6155 :
6156 : /* Stream in the aggregate value replacement chain for NODE from IB. */
6157 :
6158 : static void
6159 11982 : read_ipcp_transformation_info (lto_input_block *ib, cgraph_node *node,
6160 : data_in *data_in)
6161 : {
6162 11982 : unsigned int count, i;
6163 11982 : ipcp_transformation_initialize ();
6164 11982 : ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
6165 :
6166 11982 : count = streamer_read_uhwi (ib);
6167 11982 : if (count > 0)
6168 : {
6169 197 : vec_safe_grow_cleared (ts->m_agg_values, count, true);
6170 1428 : for (i = 0; i <count; i++)
6171 : {
6172 1231 : ipa_argagg_value *av = &(*ts->m_agg_values)[i];;
6173 :
6174 1231 : av->value = stream_read_tree (ib, data_in);
6175 1231 : av->unit_offset = streamer_read_uhwi (ib);
6176 1231 : av->index = streamer_read_uhwi (ib);
6177 :
6178 1231 : bitpack_d bp = streamer_read_bitpack (ib);
6179 1231 : av->by_ref = bp_unpack_value (&bp, 1);
6180 1231 : av->killed = bp_unpack_value (&bp, 1);
6181 : }
6182 : }
6183 :
6184 11982 : count = streamer_read_uhwi (ib);
6185 11982 : if (count > 0)
6186 : {
6187 10870 : vec_safe_grow_cleared (ts->m_vr, count, true);
6188 31317 : for (i = 0; i < count; i++)
6189 : {
6190 20447 : ipa_vr *parm_vr;
6191 20447 : parm_vr = &(*ts->m_vr)[i];
6192 20447 : parm_vr->streamer_read (ib, data_in);
6193 : }
6194 : }
6195 11982 : }
6196 :
6197 :
6198 : /* Write all aggregate replacement for nodes in set. */
6199 :
6200 : void
6201 8197 : ipcp_write_transformation_summaries (void)
6202 : {
6203 8197 : struct output_block *ob;
6204 8197 : unsigned int count = 0;
6205 8197 : lto_symtab_encoder_t encoder;
6206 :
6207 8197 : ob = create_output_block (LTO_section_ipcp_transform);
6208 8197 : encoder = ob->decl_state->symtab_node_encoder;
6209 8197 : ob->symbol = NULL;
6210 :
6211 232314 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
6212 : {
6213 107964 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
6214 107964 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
6215 107964 : if (!cnode)
6216 24877 : continue;
6217 83087 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
6218 83087 : if (useful_ipcp_transformation_info_p (ts)
6219 83087 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
6220 11982 : count++;
6221 : }
6222 :
6223 8197 : streamer_write_uhwi (ob, count);
6224 :
6225 232314 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
6226 : {
6227 107964 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
6228 107964 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
6229 107964 : if (!cnode)
6230 24877 : continue;
6231 83087 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
6232 83087 : if (useful_ipcp_transformation_info_p (ts)
6233 83087 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
6234 11982 : write_ipcp_transformation_info (ob, cnode, ts);
6235 : }
6236 8197 : ipa_write_return_summaries (ob);
6237 8197 : produce_asm (ob);
6238 8197 : destroy_output_block (ob);
6239 8197 : }
6240 :
6241 : /* Read replacements section in file FILE_DATA of length LEN with data
6242 : DATA. */
6243 :
6244 : static void
6245 8197 : read_replacements_section (struct lto_file_decl_data *file_data,
6246 : const char *data,
6247 : size_t len)
6248 : {
6249 8197 : const struct lto_function_header *header =
6250 : (const struct lto_function_header *) data;
6251 8197 : const int cfg_offset = sizeof (struct lto_function_header);
6252 8197 : const int main_offset = cfg_offset + header->cfg_size;
6253 8197 : const int string_offset = main_offset + header->main_size;
6254 8197 : class data_in *data_in;
6255 8197 : unsigned int i;
6256 8197 : unsigned int count;
6257 :
6258 8197 : lto_input_block ib_main ((const char *) data + main_offset,
6259 8197 : header->main_size, file_data);
6260 :
6261 8197 : data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
6262 8197 : header->string_size, vNULL);
6263 8197 : count = streamer_read_uhwi (&ib_main);
6264 :
6265 20179 : for (i = 0; i < count; i++)
6266 : {
6267 11982 : unsigned int index;
6268 11982 : struct cgraph_node *node;
6269 11982 : lto_symtab_encoder_t encoder;
6270 :
6271 11982 : index = streamer_read_uhwi (&ib_main);
6272 11982 : encoder = file_data->symtab_node_encoder;
6273 11982 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
6274 : index));
6275 11982 : read_ipcp_transformation_info (&ib_main, node, data_in);
6276 : }
6277 8197 : ipa_read_return_summaries (&ib_main, file_data, data_in);
6278 8197 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
6279 : len);
6280 8197 : lto_data_in_delete (data_in);
6281 8197 : }
6282 :
6283 : /* Read IPA-CP aggregate replacements. */
6284 :
6285 : void
6286 8197 : ipcp_read_transformation_summaries (void)
6287 : {
6288 8197 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
6289 8197 : struct lto_file_decl_data *file_data;
6290 8197 : unsigned int j = 0;
6291 :
6292 24591 : while ((file_data = file_data_vec[j++]))
6293 : {
6294 8197 : size_t len;
6295 8197 : const char *data
6296 8197 : = lto_get_summary_section_data (file_data, LTO_section_ipcp_transform,
6297 : &len);
6298 8197 : if (data)
6299 8197 : read_replacements_section (file_data, data, len);
6300 : }
6301 8197 : }
6302 :
6303 : /* Adjust the aggregate replacements in TS to reflect any parameter removals
6304 : which might have already taken place. If after adjustments there are no
6305 : aggregate replacements left, the m_agg_values will be set to NULL. In other
6306 : cases, it may be shrunk. */
6307 :
6308 : static void
6309 1812 : adjust_agg_replacement_values (cgraph_node *node, ipcp_transformation *ts)
6310 : {
6311 1812 : clone_info *cinfo = clone_info::get (node);
6312 1812 : if (!cinfo || !cinfo->param_adjustments)
6313 : return;
6314 :
6315 867 : auto_vec<int, 16> new_indices;
6316 867 : cinfo->param_adjustments->get_updated_indices (&new_indices);
6317 867 : bool removed_item = false;
6318 867 : unsigned dst_index = 0;
6319 867 : unsigned count = ts->m_agg_values->length ();
6320 4895 : for (unsigned i = 0; i < count; i++)
6321 : {
6322 4028 : ipa_argagg_value *v = &(*ts->m_agg_values)[i];
6323 4028 : gcc_checking_assert (v->index >= 0);
6324 :
6325 4028 : int new_idx = -1;
6326 4028 : if ((unsigned) v->index < new_indices.length ())
6327 2502 : new_idx = new_indices[v->index];
6328 :
6329 2502 : if (new_idx >= 0)
6330 : {
6331 1671 : v->index = new_idx;
6332 1671 : if (removed_item)
6333 23 : (*ts->m_agg_values)[dst_index] = *v;
6334 1671 : dst_index++;
6335 : }
6336 : else
6337 : removed_item = true;
6338 : }
6339 :
6340 867 : if (dst_index == 0)
6341 : {
6342 504 : ggc_free (ts->m_agg_values);
6343 504 : ts->m_agg_values = NULL;
6344 : }
6345 363 : else if (removed_item)
6346 35 : ts->m_agg_values->truncate (dst_index);
6347 :
6348 867 : return;
6349 867 : }
6350 :
6351 : /* Dominator walker driving the ipcp modification phase. */
6352 :
6353 : class ipcp_modif_dom_walker : public dom_walker
6354 : {
6355 : public:
6356 1308 : ipcp_modif_dom_walker (struct ipa_func_body_info *fbi,
6357 : vec<ipa_param_descriptor, va_gc> *descs,
6358 : ipcp_transformation *ts, bool *sc)
6359 2616 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi), m_descriptors (descs),
6360 1308 : m_ts (ts), m_something_changed (sc) {}
6361 :
6362 : edge before_dom_children (basic_block) final override;
6363 1308 : bool cleanup_eh ()
6364 1308 : { return gimple_purge_all_dead_eh_edges (m_need_eh_cleanup); }
6365 :
6366 : private:
6367 : struct ipa_func_body_info *m_fbi;
6368 : vec<ipa_param_descriptor, va_gc> *m_descriptors;
6369 : ipcp_transformation *m_ts;
6370 : bool *m_something_changed;
6371 : auto_bitmap m_need_eh_cleanup;
6372 : };
6373 :
6374 : edge
6375 22299 : ipcp_modif_dom_walker::before_dom_children (basic_block bb)
6376 : {
6377 22299 : gimple_stmt_iterator gsi;
6378 124617 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6379 : {
6380 80019 : gimple *stmt = gsi_stmt (gsi);
6381 80019 : tree rhs, val, t;
6382 80019 : HOST_WIDE_INT bit_offset;
6383 80019 : poly_int64 size;
6384 80019 : int index;
6385 80019 : bool by_ref, vce;
6386 :
6387 80019 : if (!gimple_assign_load_p (stmt))
6388 78497 : continue;
6389 11879 : rhs = gimple_assign_rhs1 (stmt);
6390 11879 : if (!is_gimple_reg_type (TREE_TYPE (rhs)))
6391 633 : continue;
6392 :
6393 26710 : vce = false;
6394 : t = rhs;
6395 26710 : while (handled_component_p (t))
6396 : {
6397 : /* V_C_E can do things like convert an array of integers to one
6398 : bigger integer and similar things we do not handle below. */
6399 15464 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
6400 : {
6401 : vce = true;
6402 : break;
6403 : }
6404 15464 : t = TREE_OPERAND (t, 0);
6405 : }
6406 11246 : if (vce)
6407 0 : continue;
6408 :
6409 11246 : if (!ipa_load_from_parm_agg (m_fbi, m_descriptors, stmt, rhs, &index,
6410 : &bit_offset, &size, &by_ref))
6411 8270 : continue;
6412 2976 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
6413 2976 : ipa_argagg_value_list avl (m_ts);
6414 2976 : tree v = avl.get_value (index, unit_offset, by_ref);
6415 :
6416 4430 : if (!v
6417 2976 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), size))
6418 1454 : continue;
6419 :
6420 1522 : gcc_checking_assert (is_gimple_ip_invariant (v));
6421 1522 : if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v)))
6422 : {
6423 0 : if (fold_convertible_p (TREE_TYPE (rhs), v))
6424 0 : val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v);
6425 0 : else if (TYPE_SIZE (TREE_TYPE (rhs))
6426 0 : == TYPE_SIZE (TREE_TYPE (v)))
6427 0 : val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v);
6428 : else
6429 : {
6430 0 : if (dump_file)
6431 : {
6432 0 : fprintf (dump_file, " const ");
6433 0 : print_generic_expr (dump_file, v);
6434 0 : fprintf (dump_file, " can't be converted to type of ");
6435 0 : print_generic_expr (dump_file, rhs);
6436 0 : fprintf (dump_file, "\n");
6437 : }
6438 0 : continue;
6439 : }
6440 : }
6441 : else
6442 : val = v;
6443 :
6444 1522 : if (dump_file && (dump_flags & TDF_DETAILS))
6445 : {
6446 40 : fprintf (dump_file, "Modifying stmt:\n ");
6447 40 : print_gimple_stmt (dump_file, stmt, 0);
6448 : }
6449 1522 : gimple_assign_set_rhs_from_tree (&gsi, val);
6450 1522 : update_stmt (stmt);
6451 :
6452 1522 : if (dump_file && (dump_flags & TDF_DETAILS))
6453 : {
6454 40 : fprintf (dump_file, "into:\n ");
6455 40 : print_gimple_stmt (dump_file, stmt, 0);
6456 40 : fprintf (dump_file, "\n");
6457 : }
6458 :
6459 1522 : *m_something_changed = true;
6460 1522 : if (maybe_clean_eh_stmt (stmt))
6461 9 : bitmap_set_bit (m_need_eh_cleanup, bb->index);
6462 : }
6463 22299 : return NULL;
6464 : }
6465 :
6466 : /* If IPA-CP discovered a constant in parameter PARM at OFFSET of a given SIZE
6467 : - whether passed by reference or not is given by BY_REF - return that
6468 : constant. Otherwise return NULL_TREE. The is supposed to be used only
6469 : after clone materialization and transformation is done (because it asserts
6470 : that killed constants have been pruned). */
6471 :
6472 : tree
6473 4239747 : ipcp_get_aggregate_const (struct function *func, tree parm, bool by_ref,
6474 : HOST_WIDE_INT bit_offset, HOST_WIDE_INT bit_size)
6475 : {
6476 4239747 : cgraph_node *node = cgraph_node::get (func->decl);
6477 4239747 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
6478 :
6479 4239747 : if (!ts || !ts->m_agg_values)
6480 : return NULL_TREE;
6481 :
6482 9776 : int index = ts->get_param_index (func->decl, parm);
6483 9776 : if (index < 0)
6484 : return NULL_TREE;
6485 :
6486 9723 : ipa_argagg_value_list avl (ts);
6487 9723 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
6488 9723 : const ipa_argagg_value *av = avl.get_elt (index, unit_offset);
6489 9723 : if (!av || av->by_ref != by_ref)
6490 : return NULL_TREE;
6491 1894 : gcc_assert (!av->killed);
6492 1894 : tree v = av->value;
6493 1894 : if (!v
6494 1894 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), bit_size))
6495 688 : return NULL_TREE;
6496 :
6497 : return v;
6498 : }
6499 :
6500 : /* Return true if we have recorded VALUE and MASK about PARM.
6501 : Set VALUE and MASk accordingly. */
6502 :
6503 : bool
6504 7611581 : ipcp_get_parm_bits (tree parm, tree *value, widest_int *mask)
6505 : {
6506 7611581 : cgraph_node *cnode = cgraph_node::get (current_function_decl);
6507 7611581 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
6508 7611581 : if (!ts
6509 122146 : || vec_safe_length (ts->m_vr) == 0
6510 7767720 : || !ipa_vr_supported_type_p (TREE_TYPE (parm)))
6511 : return false;
6512 :
6513 115187 : int i = ts->get_param_index (current_function_decl, parm);
6514 115187 : if (i < 0)
6515 : return false;
6516 113786 : clone_info *cinfo = clone_info::get (cnode);
6517 113786 : if (cinfo && cinfo->param_adjustments)
6518 : {
6519 33135 : i = cinfo->param_adjustments->get_original_index (i);
6520 33135 : if (i < 0)
6521 : return false;
6522 : }
6523 :
6524 104870 : vec<ipa_vr, va_gc> &vr = *ts->m_vr;
6525 104870 : if (!vr[i].known_p ())
6526 : return false;
6527 83473 : value_range tmp;
6528 83473 : vr[i].get_vrange (tmp);
6529 83473 : if (tmp.undefined_p () || tmp.varying_p ())
6530 : return false;
6531 83473 : irange_bitmask bm;
6532 83473 : bm = tmp.get_bitmask ();
6533 83473 : *mask = widest_int::from (bm.mask (), TYPE_SIGN (TREE_TYPE (parm)));
6534 83473 : *value = wide_int_to_tree (TREE_TYPE (parm), bm.value ());
6535 83473 : return true;
6536 83473 : }
6537 :
6538 : /* Update value range of formal parameters of NODE as described in TS. */
6539 :
6540 : static void
6541 22912 : ipcp_update_vr (struct cgraph_node *node, ipcp_transformation *ts)
6542 : {
6543 22912 : if (vec_safe_is_empty (ts->m_vr))
6544 493 : return;
6545 22419 : const vec<ipa_vr, va_gc> &vr = *ts->m_vr;
6546 22419 : unsigned count = vr.length ();
6547 22419 : if (!count)
6548 : return;
6549 :
6550 22419 : auto_vec<int, 16> new_indices;
6551 22419 : bool need_remapping = false;
6552 22419 : clone_info *cinfo = clone_info::get (node);
6553 22419 : if (cinfo && cinfo->param_adjustments)
6554 : {
6555 7775 : cinfo->param_adjustments->get_updated_indices (&new_indices);
6556 7775 : need_remapping = true;
6557 : }
6558 22419 : auto_vec <tree, 16> parm_decls;
6559 22419 : push_function_arg_decls (&parm_decls, node->decl);
6560 :
6561 78422 : for (unsigned i = 0; i < count; ++i)
6562 : {
6563 56003 : tree parm;
6564 56003 : int remapped_idx;
6565 56003 : if (need_remapping)
6566 : {
6567 23139 : if (i >= new_indices.length ())
6568 11280 : continue;
6569 11859 : remapped_idx = new_indices[i];
6570 11859 : if (remapped_idx < 0)
6571 2512 : continue;
6572 : }
6573 : else
6574 32864 : remapped_idx = i;
6575 :
6576 42211 : parm = parm_decls[remapped_idx];
6577 :
6578 42211 : gcc_checking_assert (parm);
6579 42211 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl), parm);
6580 :
6581 42211 : if (!ddef || !is_gimple_reg (parm))
6582 6270 : continue;
6583 :
6584 35941 : if (vr[i].known_p ())
6585 : {
6586 27834 : value_range tmp;
6587 27834 : vr[i].get_vrange (tmp);
6588 :
6589 27834 : if (!tmp.undefined_p () && !tmp.varying_p ())
6590 : {
6591 27834 : if (dump_file)
6592 : {
6593 112 : fprintf (dump_file, "Setting value range of param %u "
6594 : "(now %i) ", i, remapped_idx);
6595 112 : tmp.dump (dump_file);
6596 112 : fprintf (dump_file, "]\n");
6597 : }
6598 27834 : set_range_info (ddef, tmp);
6599 :
6600 42936 : if (POINTER_TYPE_P (TREE_TYPE (parm))
6601 30994 : && opt_for_fn (node->decl, flag_ipa_bit_cp))
6602 : {
6603 15891 : irange_bitmask bm = tmp.get_bitmask ();
6604 15891 : unsigned tem = bm.mask ().to_uhwi ();
6605 15891 : unsigned HOST_WIDE_INT bitpos = bm.value ().to_uhwi ();
6606 15891 : unsigned align = tem & -tem;
6607 15891 : unsigned misalign = bitpos & (align - 1);
6608 :
6609 15891 : if (align > 1)
6610 : {
6611 13145 : if (dump_file)
6612 : {
6613 85 : fprintf (dump_file,
6614 : "Adjusting mask for param %u to ", i);
6615 85 : print_hex (bm.mask (), dump_file);
6616 85 : fprintf (dump_file, "\n");
6617 : }
6618 :
6619 13145 : if (dump_file)
6620 85 : fprintf (dump_file,
6621 : "Adjusting align: %u, misalign: %u\n",
6622 : align, misalign);
6623 :
6624 13145 : unsigned old_align, old_misalign;
6625 13145 : struct ptr_info_def *pi = get_ptr_info (ddef);
6626 13145 : bool old_known = get_ptr_info_alignment (pi, &old_align,
6627 : &old_misalign);
6628 :
6629 13145 : if (old_known && old_align > align)
6630 : {
6631 0 : if (dump_file)
6632 : {
6633 0 : fprintf (dump_file,
6634 : "But alignment was already %u.\n",
6635 : old_align);
6636 0 : if ((old_misalign & (align - 1)) != misalign)
6637 0 : fprintf (dump_file,
6638 : "old_misalign (%u) and misalign "
6639 : "(%u) mismatch\n",
6640 : old_misalign, misalign);
6641 : }
6642 0 : continue;
6643 : }
6644 :
6645 13145 : if (dump_file
6646 85 : && old_known
6647 0 : && ((misalign & (old_align - 1)) != old_misalign))
6648 0 : fprintf (dump_file,
6649 : "old_misalign (%u) and misalign (%u) "
6650 : "mismatch\n",
6651 : old_misalign, misalign);
6652 :
6653 13145 : set_ptr_info_alignment (pi, align, misalign);
6654 : }
6655 15891 : }
6656 11943 : else if (dump_file && INTEGRAL_TYPE_P (TREE_TYPE (parm)))
6657 : {
6658 23 : irange &r = as_a<irange> (tmp);
6659 23 : irange_bitmask bm = r.get_bitmask ();
6660 23 : unsigned prec = TYPE_PRECISION (TREE_TYPE (parm));
6661 23 : if (wi::ne_p (bm.mask (), wi::shwi (-1, prec)))
6662 : {
6663 16 : fprintf (dump_file,
6664 : "Adjusting mask for param %u to ", i);
6665 16 : print_hex (bm.mask (), dump_file);
6666 16 : fprintf (dump_file, "\n");
6667 : }
6668 23 : }
6669 : }
6670 27834 : }
6671 : }
6672 22419 : }
6673 :
6674 : /* IPCP transformation phase doing propagation of aggregate values. */
6675 :
6676 : unsigned int
6677 969039 : ipcp_transform_function (struct cgraph_node *node)
6678 : {
6679 969039 : struct ipa_func_body_info fbi;
6680 969039 : int param_count;
6681 :
6682 969039 : gcc_checking_assert (cfun);
6683 969039 : gcc_checking_assert (current_function_decl);
6684 :
6685 969039 : if (dump_file)
6686 686 : fprintf (dump_file, "Modification phase of node %s\n",
6687 : node->dump_name ());
6688 :
6689 969039 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
6690 969039 : if (!ts
6691 969039 : || (vec_safe_is_empty (ts->m_agg_values)
6692 21959 : && vec_safe_is_empty (ts->m_vr)))
6693 : return 0;
6694 :
6695 22912 : ts->maybe_create_parm_idx_map (cfun->decl);
6696 22912 : ipcp_update_vr (node, ts);
6697 969791 : if (vec_safe_is_empty (ts->m_agg_values))
6698 : return 0;
6699 2060 : param_count = count_formal_params (node->decl);
6700 2060 : if (param_count == 0)
6701 : return 0;
6702 :
6703 1812 : adjust_agg_replacement_values (node, ts);
6704 1812 : if (vec_safe_is_empty (ts->m_agg_values))
6705 : {
6706 504 : if (dump_file)
6707 4 : fprintf (dump_file, " All affected aggregate parameters were either "
6708 : "removed or converted into scalars, phase done.\n");
6709 504 : return 0;
6710 : }
6711 1308 : if (dump_file)
6712 : {
6713 48 : fprintf (dump_file, " Aggregate replacements:");
6714 48 : ipa_argagg_value_list avs (ts);
6715 48 : avs.dump (dump_file);
6716 : }
6717 :
6718 1308 : fbi.node = node;
6719 1308 : fbi.info = NULL;
6720 1308 : fbi.bb_infos = vNULL;
6721 1308 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
6722 1308 : fbi.param_count = param_count;
6723 1308 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
6724 :
6725 1308 : vec<ipa_param_descriptor, va_gc> *descriptors = NULL;
6726 1308 : vec_safe_grow_cleared (descriptors, param_count, true);
6727 1308 : ipa_populate_param_decls (node, *descriptors);
6728 1308 : bool modified_mem_access = false;
6729 1308 : calculate_dominance_info (CDI_DOMINATORS);
6730 1308 : ipcp_modif_dom_walker walker (&fbi, descriptors, ts, &modified_mem_access);
6731 1308 : walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6732 1308 : free_dominance_info (CDI_DOMINATORS);
6733 1308 : bool cfg_changed = walker.cleanup_eh ();
6734 :
6735 1308 : int i;
6736 1308 : struct ipa_bb_info *bi;
6737 26223 : FOR_EACH_VEC_ELT (fbi.bb_infos, i, bi)
6738 47214 : free_ipa_bb_info (bi);
6739 1308 : fbi.bb_infos.release ();
6740 :
6741 1308 : ts->remove_argaggs_if ([](const ipa_argagg_value &v)
6742 : {
6743 4965 : return v.killed;
6744 : });
6745 :
6746 1308 : vec_free (descriptors);
6747 1308 : if (cfg_changed)
6748 1 : delete_unreachable_blocks_update_callgraph (node, false);
6749 :
6750 1308 : return modified_mem_access ? TODO_update_ssa_only_virtuals : 0;
6751 1308 : }
6752 :
6753 : /* Record that current function return value range is VAL. */
6754 :
6755 : void
6756 724880 : ipa_record_return_value_range (value_range val)
6757 : {
6758 724880 : ipa_record_return_value_range_1
6759 724880 : (cgraph_node::get (current_function_decl), val);
6760 724880 : }
6761 :
6762 : /* Return true if value range of DECL is known and if so initialize RANGE. */
6763 :
6764 : bool
6765 11900244 : ipa_return_value_range (value_range &range, tree decl)
6766 : {
6767 11900244 : cgraph_node *n = cgraph_node::get (decl);
6768 11900244 : if (!n || !ipa_return_value_sum)
6769 : return false;
6770 9668403 : enum availability avail;
6771 9668403 : n = n->ultimate_alias_target (&avail);
6772 9668403 : if (avail < AVAIL_AVAILABLE)
6773 : return false;
6774 2071410 : if (n->decl != decl && !useless_type_conversion_p (TREE_TYPE (decl), TREE_TYPE (n->decl)))
6775 : return false;
6776 2071410 : ipa_return_value_summary *v = ipa_return_value_sum->get (n);
6777 2071410 : if (!v)
6778 : return false;
6779 600039 : v->vr->get_vrange (range);
6780 600039 : return true;
6781 : }
6782 :
6783 : /* Reset all state within ipa-prop.cc so that we can rerun the compiler
6784 : within the same process. For use by toplev::finalize. */
6785 :
6786 : void
6787 259439 : ipa_prop_cc_finalize (void)
6788 : {
6789 259439 : if (function_insertion_hook_holder)
6790 11982 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
6791 259439 : function_insertion_hook_holder = NULL;
6792 :
6793 259439 : if (ipa_edge_args_sum)
6794 12308 : ggc_delete (ipa_edge_args_sum);
6795 259439 : ipa_edge_args_sum = NULL;
6796 :
6797 259439 : if (ipa_node_params_sum)
6798 12308 : ggc_delete (ipa_node_params_sum);
6799 259439 : ipa_node_params_sum = NULL;
6800 259439 : }
6801 :
6802 : /* Return true if the two pass_through components of two jump functions are
6803 : known to be equivalent. AGG_JF denotes whether they are part of aggregate
6804 : functions or not. The function can be used before the IPA phase of IPA-CP
6805 : or inlining because it cannot cope with refdesc changes these passes can
6806 : carry out. */
6807 :
6808 : static bool
6809 43721 : ipa_agg_pass_through_jf_equivalent_p (ipa_pass_through_data *ipt1,
6810 : ipa_pass_through_data *ipt2,
6811 : bool agg_jf)
6812 :
6813 : {
6814 43721 : gcc_assert (agg_jf ||
6815 : (!ipt1->refdesc_decremented && !ipt2->refdesc_decremented));
6816 43721 : if (ipt1->operation != ipt2->operation
6817 43721 : || ipt1->formal_id != ipt2->formal_id
6818 43721 : || (!agg_jf && (ipt1->agg_preserved != ipt2->agg_preserved)))
6819 : return false;
6820 43721 : if (ipt1->operation != NOP_EXPR
6821 43721 : && (TYPE_MAIN_VARIANT (ipt1->op_type)
6822 6239 : != TYPE_MAIN_VARIANT (ipt2->op_type)))
6823 : return false;
6824 43713 : if (((ipt1->operand != NULL_TREE) != (ipt2->operand != NULL_TREE))
6825 43713 : || (ipt1->operand
6826 6231 : && !values_equal_for_ipcp_p (ipt1->operand, ipt2->operand)))
6827 0 : return false;
6828 : return true;
6829 : }
6830 :
6831 : /* Return true if the two aggregate jump functions are known to be equivalent.
6832 : The function can be used before the IPA phase of IPA-CP or inlining because
6833 : it cannot cope with refdesc changes these passes can carry out. */
6834 :
6835 : static bool
6836 3607 : ipa_agg_jump_functions_equivalent_p (ipa_agg_jf_item *ajf1,
6837 : ipa_agg_jf_item *ajf2)
6838 : {
6839 3607 : if (ajf1->offset != ajf2->offset
6840 3607 : || ajf1->jftype != ajf2->jftype
6841 7214 : || !types_compatible_p (ajf1->type, ajf2->type))
6842 0 : return false;
6843 :
6844 3607 : switch (ajf1->jftype)
6845 : {
6846 2082 : case IPA_JF_CONST:
6847 2082 : if (!values_equal_for_ipcp_p (ajf1->value.constant,
6848 : ajf2->value.constant))
6849 : return false;
6850 : break;
6851 786 : case IPA_JF_PASS_THROUGH:
6852 786 : {
6853 786 : ipa_pass_through_data *ipt1 = &ajf1->value.pass_through;
6854 786 : ipa_pass_through_data *ipt2 = &ajf2->value.pass_through;
6855 786 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, true))
6856 : return false;
6857 : }
6858 : break;
6859 739 : case IPA_JF_LOAD_AGG:
6860 739 : {
6861 739 : ipa_load_agg_data *ila1 = &ajf1->value.load_agg;
6862 739 : ipa_load_agg_data *ila2 = &ajf2->value.load_agg;
6863 739 : if (!ipa_agg_pass_through_jf_equivalent_p (&ila1->pass_through,
6864 : &ila2->pass_through, true))
6865 : return false;
6866 739 : if (ila1->offset != ila2->offset
6867 739 : || ila1->by_ref != ila2->by_ref
6868 1478 : || !types_compatible_p (ila1->type, ila2->type))
6869 0 : return false;
6870 : }
6871 : break;
6872 0 : default:
6873 0 : gcc_unreachable ();
6874 : }
6875 : return true;
6876 : }
6877 :
6878 : /* Return true if the two jump functions are known to be equivalent. The
6879 : function can be used before the IPA phase of IPA-CP or inlining because it
6880 : cannot cope with refdesc changes these passes can carry out. */
6881 :
6882 : bool
6883 119894 : ipa_jump_functions_equivalent_p (ipa_jump_func *jf1, ipa_jump_func *jf2)
6884 : {
6885 119894 : if (jf1->type != jf2->type)
6886 : return false;
6887 :
6888 119894 : switch (jf1->type)
6889 : {
6890 : case IPA_JF_UNKNOWN:
6891 : break;
6892 23518 : case IPA_JF_CONST:
6893 23518 : {
6894 23518 : tree cst1 = ipa_get_jf_constant (jf1);
6895 23518 : tree cst2 = ipa_get_jf_constant (jf2);
6896 23518 : if (!values_equal_for_ipcp_p (cst1, cst2))
6897 : return false;
6898 :
6899 23517 : ipa_cst_ref_desc *rd1 = jfunc_rdesc_usable (jf1);
6900 23517 : ipa_cst_ref_desc *rd2 = jfunc_rdesc_usable (jf2);
6901 23517 : if (rd1 && rd2)
6902 : {
6903 470 : gcc_assert (rd1->refcount == 1
6904 : && rd2->refcount == 1);
6905 470 : gcc_assert (!rd1->next_duplicate && !rd2->next_duplicate);
6906 : }
6907 23047 : else if (rd1)
6908 : return false;
6909 23047 : else if (rd2)
6910 : return false;
6911 : }
6912 : break;
6913 42196 : case IPA_JF_PASS_THROUGH:
6914 42196 : {
6915 42196 : ipa_pass_through_data *ipt1 = &jf1->value.pass_through;
6916 42196 : ipa_pass_through_data *ipt2 = &jf2->value.pass_through;
6917 42196 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, false))
6918 : return false;
6919 : }
6920 : break;
6921 14748 : case IPA_JF_ANCESTOR:
6922 14748 : {
6923 14748 : ipa_ancestor_jf_data *ia1 = &jf1->value.ancestor;
6924 14748 : ipa_ancestor_jf_data *ia2 = &jf2->value.ancestor;
6925 :
6926 14748 : if (ia1->formal_id != ia2->formal_id
6927 14748 : || ia1->agg_preserved != ia2->agg_preserved
6928 14748 : || ia1->keep_null != ia2->keep_null
6929 14748 : || ia1->offset != ia2->offset)
6930 : return false;
6931 : }
6932 : break;
6933 0 : default:
6934 0 : gcc_unreachable ();
6935 : }
6936 :
6937 119885 : if (((jf1->m_vr != nullptr) != (jf2->m_vr != nullptr))
6938 119885 : || (jf1->m_vr && !jf1->m_vr->equal_p (*jf2->m_vr)))
6939 6 : return false;
6940 :
6941 119879 : unsigned alen = vec_safe_length (jf1->agg.items);
6942 122258 : if (vec_safe_length (jf2->agg.items) != alen)
6943 : return false;
6944 :
6945 119878 : if (!alen)
6946 : return true;
6947 :
6948 2379 : if (jf1->agg.by_ref != jf2->agg.by_ref)
6949 : return false;
6950 :
6951 5986 : for (unsigned i = 0 ; i < alen; i++)
6952 3607 : if (!ipa_agg_jump_functions_equivalent_p (&(*jf1->agg.items)[i],
6953 3607 : &(*jf2->agg.items)[i]))
6954 : return false;
6955 :
6956 : return true;
6957 : }
6958 :
6959 : #include "gt-ipa-prop.h"
|