Branch data Line data Source code
1 : : /* Interprocedural analyses.
2 : : Copyright (C) 2005-2025 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 : :
64 : : /* Function summary where the parameter infos are actually stored. */
65 : : ipa_node_params_t *ipa_node_params_sum = NULL;
66 : :
67 : : function_summary <ipcp_transformation *> *ipcp_transformation_sum = NULL;
68 : :
69 : : /* Edge summary for IPA-CP edge information. */
70 : : ipa_edge_args_sum_t *ipa_edge_args_sum;
71 : :
72 : : /* Traits for a hash table for reusing ranges. */
73 : :
74 : : struct ipa_vr_ggc_hash_traits : public ggc_cache_remove <ipa_vr *>
75 : : {
76 : : typedef ipa_vr *value_type;
77 : : typedef const vrange *compare_type;
78 : : static hashval_t
79 : 15117216 : hash (const ipa_vr *p)
80 : : {
81 : : // This never get called, except in the verification code, as
82 : : // ipa_get_value_range() calculates the hash itself. This
83 : : // function is mostly here for completness' sake.
84 : 15117216 : value_range vr;
85 : 15117216 : p->get_vrange (vr);
86 : 15117216 : inchash::hash hstate;
87 : 15117216 : add_vrange (vr, hstate);
88 : 15117216 : return hstate.end ();
89 : 15117216 : }
90 : : static bool
91 : 21440602 : equal (const ipa_vr *a, const vrange *b)
92 : : {
93 : 21440602 : return a->equal_p (*b);
94 : : }
95 : : static const bool empty_zero_p = true;
96 : : static void
97 : 1229 : mark_empty (ipa_vr *&p)
98 : : {
99 : 1229 : p = NULL;
100 : : }
101 : : static bool
102 : : is_empty (const ipa_vr *p)
103 : : {
104 : : return p == NULL;
105 : : }
106 : : static bool
107 : 40378199 : is_deleted (const ipa_vr *p)
108 : : {
109 : 40378199 : return p == reinterpret_cast<const ipa_vr *> (1);
110 : : }
111 : : static void
112 : 144146 : mark_deleted (ipa_vr *&p)
113 : : {
114 : 144146 : p = reinterpret_cast<ipa_vr *> (1);
115 : : }
116 : : };
117 : :
118 : : /* Hash table for avoid repeated allocations of equal ranges. */
119 : : static GTY ((cache)) hash_table<ipa_vr_ggc_hash_traits> *ipa_vr_hash_table;
120 : :
121 : : /* Holders of ipa cgraph hooks: */
122 : : static struct cgraph_node_hook_list *function_insertion_hook_holder;
123 : :
124 : : /* Description of a reference to an IPA constant. */
125 : : struct ipa_cst_ref_desc
126 : : {
127 : : /* Edge that corresponds to the statement which took the reference. */
128 : : struct cgraph_edge *cs;
129 : : /* Linked list of duplicates created when call graph edges are cloned. */
130 : : struct ipa_cst_ref_desc *next_duplicate;
131 : : /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
132 : : if out of control. */
133 : : int refcount;
134 : : };
135 : :
136 : : /* Allocation pool for reference descriptions. */
137 : :
138 : : static object_allocator<ipa_cst_ref_desc> ipa_refdesc_pool
139 : : ("IPA-PROP ref descriptions");
140 : :
141 : 620873 : ipa_vr::ipa_vr ()
142 : 620873 : : m_storage (NULL),
143 : 620873 : m_type (NULL)
144 : : {
145 : 620873 : }
146 : :
147 : 688670 : ipa_vr::ipa_vr (const vrange &r)
148 : 688670 : : m_storage (ggc_alloc_vrange_storage (r)),
149 : 688670 : m_type (r.type ())
150 : : {
151 : 688670 : }
152 : :
153 : : bool
154 : 21440602 : ipa_vr::equal_p (const vrange &r) const
155 : : {
156 : 21440602 : gcc_checking_assert (!r.undefined_p ());
157 : 21440602 : return (types_compatible_p (m_type, r.type ()) && m_storage->equal_p (r));
158 : : }
159 : :
160 : : bool
161 : 69312 : ipa_vr::equal_p (const ipa_vr &o) const
162 : : {
163 : 69312 : if (!known_p ())
164 : 0 : return !o.known_p ();
165 : :
166 : 69312 : if (!types_compatible_p (m_type, o.m_type))
167 : : return false;
168 : :
169 : 69312 : value_range r;
170 : 69312 : o.get_vrange (r);
171 : 69312 : return m_storage->equal_p (r);
172 : 69312 : }
173 : :
174 : : void
175 : 24562748 : ipa_vr::get_vrange (value_range &r) const
176 : : {
177 : 24562748 : r.set_type (m_type);
178 : 24562748 : m_storage->get_vrange (r, m_type);
179 : 24562748 : }
180 : :
181 : : void
182 : 1704 : ipa_vr::set_unknown ()
183 : : {
184 : 1704 : if (m_storage)
185 : 1704 : ggc_free (m_storage);
186 : :
187 : 1704 : m_storage = NULL;
188 : 1704 : }
189 : :
190 : : void
191 : 582550 : ipa_vr::streamer_read (lto_input_block *ib, data_in *data_in)
192 : : {
193 : 582550 : struct bitpack_d bp = streamer_read_bitpack (ib);
194 : 582550 : bool known = bp_unpack_value (&bp, 1);
195 : 582550 : if (known)
196 : : {
197 : 410562 : value_range vr;
198 : 410562 : streamer_read_value_range (ib, data_in, vr);
199 : 410562 : if (!m_storage || !m_storage->fits_p (vr))
200 : : {
201 : 410562 : if (m_storage)
202 : 0 : ggc_free (m_storage);
203 : 410562 : m_storage = ggc_alloc_vrange_storage (vr);
204 : : }
205 : 410562 : m_storage->set_vrange (vr);
206 : 410562 : m_type = vr.type ();
207 : 410562 : }
208 : : else
209 : : {
210 : 171988 : m_storage = NULL;
211 : 171988 : m_type = NULL;
212 : : }
213 : 582550 : }
214 : :
215 : : void
216 : 435602 : ipa_vr::streamer_write (output_block *ob) const
217 : : {
218 : 435602 : struct bitpack_d bp = bitpack_create (ob->main_stream);
219 : 435602 : bp_pack_value (&bp, !!m_storage, 1);
220 : 435602 : streamer_write_bitpack (&bp);
221 : 435602 : if (m_storage)
222 : : {
223 : 434305 : value_range vr (m_type);
224 : 434305 : m_storage->get_vrange (vr, m_type);
225 : 434305 : streamer_write_vrange (ob, vr);
226 : 434305 : }
227 : 435602 : }
228 : :
229 : : void
230 : 693 : ipa_vr::dump (FILE *out) const
231 : : {
232 : 693 : if (known_p ())
233 : : {
234 : 693 : value_range vr (m_type);
235 : 693 : m_storage->get_vrange (vr, m_type);
236 : 693 : vr.dump (out);
237 : 693 : }
238 : : else
239 : 0 : fprintf (out, "NO RANGE");
240 : 693 : }
241 : :
242 : : // These stubs are because we use an ipa_vr in a hash_traits and
243 : : // hash-traits.h defines an extern of gt_ggc_mx (T &) instead of
244 : : // picking up the gt_ggc_mx (T *) version.
245 : : void
246 : 0 : gt_pch_nx (ipa_vr *&x)
247 : : {
248 : 0 : return gt_pch_nx ((ipa_vr *) x);
249 : : }
250 : :
251 : : void
252 : 0 : gt_ggc_mx (ipa_vr *&x)
253 : : {
254 : 0 : return gt_ggc_mx ((ipa_vr *) x);
255 : : }
256 : :
257 : : /* Analysis summery of function call return value. */
258 : : struct GTY(()) ipa_return_value_summary
259 : : {
260 : : /* Known value range.
261 : : This needs to be wrapped in struccture due to specific way
262 : : we allocate ipa_vr. */
263 : : ipa_vr *vr;
264 : : };
265 : :
266 : : /* Function summary for return values. */
267 : : class ipa_return_value_sum_t : public function_summary <ipa_return_value_summary *>
268 : : {
269 : : public:
270 : 77944 : ipa_return_value_sum_t (symbol_table *table, bool ggc):
271 : 155888 : function_summary <ipa_return_value_summary *> (table, ggc) { }
272 : :
273 : : /* Hook that is called by summary when a node is duplicated. */
274 : 470789 : void duplicate (cgraph_node *,
275 : : cgraph_node *,
276 : : ipa_return_value_summary *data,
277 : : ipa_return_value_summary *data2) final override
278 : : {
279 : 470789 : *data2=*data;
280 : 470789 : }
281 : : };
282 : :
283 : : /* Variable hoding the return value summary. */
284 : : static GTY(()) function_summary <ipa_return_value_summary *> *ipa_return_value_sum;
285 : :
286 : :
287 : : /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
288 : : with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
289 : :
290 : : static bool
291 : 3818137 : ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
292 : : {
293 : 3818137 : tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
294 : :
295 : 3818137 : if (!fs_opts)
296 : : return false;
297 : 550313 : return !opt_for_fn (node->decl, optimize) || !opt_for_fn (node->decl, flag_ipa_cp);
298 : : }
299 : :
300 : : /* Return index of the formal whose tree is PTREE in function which corresponds
301 : : to INFO. */
302 : :
303 : : static int
304 : 40516312 : ipa_get_param_decl_index_1 (vec<ipa_param_descriptor, va_gc> *descriptors,
305 : : tree ptree)
306 : : {
307 : 40516312 : int i, count;
308 : :
309 : 40516312 : count = vec_safe_length (descriptors);
310 : 83726210 : for (i = 0; i < count; i++)
311 : 73423948 : if ((*descriptors)[i].decl_or_type == ptree)
312 : : return i;
313 : :
314 : : return -1;
315 : : }
316 : :
317 : : /* Return index of the formal whose tree is PTREE in function which corresponds
318 : : to INFO. */
319 : :
320 : : int
321 : 24991341 : ipa_get_param_decl_index (class ipa_node_params *info, tree ptree)
322 : : {
323 : 24991341 : return ipa_get_param_decl_index_1 (info->descriptors, ptree);
324 : : }
325 : :
326 : : /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
327 : : NODE. */
328 : :
329 : : static void
330 : 5193411 : ipa_populate_param_decls (struct cgraph_node *node,
331 : : vec<ipa_param_descriptor, va_gc> &descriptors)
332 : : {
333 : 5193411 : tree fndecl;
334 : 5193411 : tree fnargs;
335 : 5193411 : tree parm;
336 : 5193411 : int param_num;
337 : :
338 : 5193411 : fndecl = node->decl;
339 : 5193411 : gcc_assert (gimple_has_body_p (fndecl));
340 : 5193411 : fnargs = DECL_ARGUMENTS (fndecl);
341 : 5193411 : param_num = 0;
342 : 17161533 : for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
343 : : {
344 : 11968122 : descriptors[param_num].decl_or_type = parm;
345 : 11968122 : unsigned int cost = estimate_move_cost (TREE_TYPE (parm), true);
346 : 11968122 : descriptors[param_num].move_cost = cost;
347 : : /* Watch overflow, move_cost is a bitfield. */
348 : 11968122 : gcc_checking_assert (cost == descriptors[param_num].move_cost);
349 : 11968122 : param_num++;
350 : : }
351 : 5193411 : }
352 : :
353 : : /* Return how many formal parameters FNDECL has. */
354 : :
355 : : int
356 : 14312978 : count_formal_params (tree fndecl)
357 : : {
358 : 14312978 : tree parm;
359 : 14312978 : int count = 0;
360 : 14312978 : gcc_assert (gimple_has_body_p (fndecl));
361 : :
362 : 44340021 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
363 : 30027043 : count++;
364 : :
365 : 14312978 : return count;
366 : : }
367 : :
368 : : /* Return the declaration of Ith formal parameter of the function corresponding
369 : : to INFO. Note there is no setter function as this array is built just once
370 : : using ipa_initialize_node_params. */
371 : :
372 : : void
373 : 543 : ipa_dump_param (FILE *file, class ipa_node_params *info, int i)
374 : : {
375 : 543 : fprintf (file, "param #%i", i);
376 : 543 : if ((*info->descriptors)[i].decl_or_type)
377 : : {
378 : 543 : fprintf (file, " ");
379 : 543 : print_generic_expr (file, (*info->descriptors)[i].decl_or_type);
380 : : }
381 : 543 : }
382 : :
383 : : /* If necessary, allocate vector of parameter descriptors in info of NODE.
384 : : Return true if they were allocated, false if not. */
385 : :
386 : : static bool
387 : 6042443 : ipa_alloc_node_params (struct cgraph_node *node, int param_count)
388 : : {
389 : 6042443 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
390 : :
391 : 6042443 : if (!info->descriptors && param_count)
392 : : {
393 : 5243842 : vec_safe_grow_cleared (info->descriptors, param_count, true);
394 : 5243842 : return true;
395 : : }
396 : : else
397 : : return false;
398 : : }
399 : :
400 : : /* Initialize the ipa_node_params structure associated with NODE by counting
401 : : the function parameters, creating the descriptors and populating their
402 : : param_decls. */
403 : :
404 : : void
405 : 5967300 : ipa_initialize_node_params (struct cgraph_node *node)
406 : : {
407 : 5967300 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
408 : :
409 : 5967300 : if (!info->descriptors
410 : 5967300 : && ipa_alloc_node_params (node, count_formal_params (node->decl)))
411 : 5192266 : ipa_populate_param_decls (node, *info->descriptors);
412 : 5967300 : }
413 : :
414 : : /* Print VAL which is extracted from a jump function to F. */
415 : :
416 : : void
417 : 1024 : ipa_print_constant_value (FILE *f, tree val)
418 : : {
419 : 1024 : print_generic_expr (f, val);
420 : :
421 : : /* This is in keeping with values_equal_for_ipcp_p. */
422 : 1024 : if (TREE_CODE (val) == ADDR_EXPR
423 : 1024 : && (TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL
424 : 224 : || (TREE_CODE (TREE_OPERAND (val, 0)) == VAR_DECL
425 : 101 : && DECL_IN_CONSTANT_POOL (TREE_OPERAND (val, 0)))))
426 : : {
427 : 0 : fputs (" -> ", f);
428 : 0 : print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)));
429 : : }
430 : 1024 : }
431 : :
432 : : /* Print contents of JFUNC to F. If CTX is non-NULL, dump it too. */
433 : :
434 : : DEBUG_FUNCTION void
435 : 928 : ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
436 : : class ipa_polymorphic_call_context *ctx)
437 : : {
438 : 928 : enum jump_func_type type = jump_func->type;
439 : :
440 : 928 : if (type == IPA_JF_UNKNOWN)
441 : 235 : fprintf (f, "UNKNOWN\n");
442 : 693 : else if (type == IPA_JF_CONST)
443 : : {
444 : 281 : fprintf (f, "CONST: ");
445 : 281 : ipa_print_constant_value (f, jump_func->value.constant.value);
446 : 281 : fprintf (f, "\n");
447 : : }
448 : 412 : else if (type == IPA_JF_PASS_THROUGH)
449 : : {
450 : 351 : fprintf (f, "PASS THROUGH: ");
451 : 351 : fprintf (f, "%d, op %s",
452 : : jump_func->value.pass_through.formal_id,
453 : : get_tree_code_name(jump_func->value.pass_through.operation));
454 : 351 : if (jump_func->value.pass_through.operation != NOP_EXPR)
455 : : {
456 : 31 : fprintf (f, " ");
457 : 31 : print_generic_expr (f, jump_func->value.pass_through.operand);
458 : : }
459 : 351 : if (jump_func->value.pass_through.agg_preserved)
460 : 109 : fprintf (f, ", agg_preserved");
461 : 351 : if (jump_func->value.pass_through.refdesc_decremented)
462 : 0 : fprintf (f, ", refdesc_decremented");
463 : 351 : fprintf (f, "\n");
464 : : }
465 : 61 : else if (type == IPA_JF_ANCESTOR)
466 : : {
467 : 61 : fprintf (f, "ANCESTOR: ");
468 : 61 : fprintf (f, "%d, offset " HOST_WIDE_INT_PRINT_DEC,
469 : : jump_func->value.ancestor.formal_id,
470 : : jump_func->value.ancestor.offset);
471 : 61 : if (jump_func->value.ancestor.agg_preserved)
472 : 26 : fprintf (f, ", agg_preserved");
473 : 61 : if (jump_func->value.ancestor.keep_null)
474 : 4 : fprintf (f, ", keep_null");
475 : 61 : fprintf (f, "\n");
476 : : }
477 : :
478 : 928 : if (jump_func->agg.items)
479 : : {
480 : 86 : struct ipa_agg_jf_item *item;
481 : 86 : int j;
482 : :
483 : 172 : fprintf (f, " Aggregate passed by %s:\n",
484 : 86 : jump_func->agg.by_ref ? "reference" : "value");
485 : 361 : FOR_EACH_VEC_ELT (*jump_func->agg.items, j, item)
486 : : {
487 : 189 : fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
488 : : item->offset);
489 : 189 : fprintf (f, "type: ");
490 : 189 : print_generic_expr (f, item->type);
491 : 189 : fprintf (f, ", ");
492 : 189 : if (item->jftype == IPA_JF_PASS_THROUGH)
493 : 5 : fprintf (f, "PASS THROUGH: %d,",
494 : : item->value.pass_through.formal_id);
495 : 184 : else if (item->jftype == IPA_JF_LOAD_AGG)
496 : : {
497 : 11 : fprintf (f, "LOAD AGG: %d",
498 : : item->value.pass_through.formal_id);
499 : 11 : fprintf (f, " [offset: " HOST_WIDE_INT_PRINT_DEC ", by %s],",
500 : : item->value.load_agg.offset,
501 : 11 : item->value.load_agg.by_ref ? "reference"
502 : : : "value");
503 : : }
504 : :
505 : 189 : if (item->jftype == IPA_JF_PASS_THROUGH
506 : 189 : || item->jftype == IPA_JF_LOAD_AGG)
507 : : {
508 : 16 : fprintf (f, " op %s",
509 : : get_tree_code_name (item->value.pass_through.operation));
510 : 16 : if (item->value.pass_through.operation != NOP_EXPR)
511 : : {
512 : 9 : fprintf (f, " ");
513 : 9 : print_generic_expr (f, item->value.pass_through.operand);
514 : : }
515 : : }
516 : 173 : else if (item->jftype == IPA_JF_CONST)
517 : : {
518 : 173 : fprintf (f, "CONST: ");
519 : 173 : ipa_print_constant_value (f, item->value.constant);
520 : : }
521 : 0 : else if (item->jftype == IPA_JF_UNKNOWN)
522 : 0 : fprintf (f, "UNKNOWN: " HOST_WIDE_INT_PRINT_DEC " bits",
523 : 0 : tree_to_uhwi (TYPE_SIZE (item->type)));
524 : 189 : fprintf (f, "\n");
525 : : }
526 : : }
527 : :
528 : 928 : if (ctx && !ctx->useless_p ())
529 : : {
530 : 354 : fprintf (f, " Context: ");
531 : 354 : ctx->dump (dump_file);
532 : : }
533 : :
534 : 928 : if (jump_func->m_vr)
535 : : {
536 : 693 : jump_func->m_vr->dump (f);
537 : 693 : fprintf (f, "\n");
538 : : }
539 : : else
540 : 235 : fprintf (f, " Unknown VR\n");
541 : 928 : }
542 : :
543 : : /* Print the jump functions associated with call graph edge CS to file F. */
544 : :
545 : : static void
546 : 675 : ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
547 : : {
548 : 675 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
549 : 675 : int count = ipa_get_cs_argument_count (args);
550 : :
551 : 1603 : for (int i = 0; i < count; i++)
552 : : {
553 : 928 : struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
554 : 928 : class ipa_polymorphic_call_context *ctx
555 : 928 : = ipa_get_ith_polymorhic_call_context (args, i);
556 : :
557 : 928 : fprintf (f, " param %d: ", i);
558 : 928 : ipa_dump_jump_function (f, jump_func, ctx);
559 : : }
560 : 675 : }
561 : :
562 : :
563 : : /* Print the jump functions of all arguments on all call graph edges going from
564 : : NODE to file F. */
565 : :
566 : : void
567 : 1027 : ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
568 : : {
569 : 1027 : struct cgraph_edge *cs;
570 : :
571 : 1027 : fprintf (f, " Jump functions of caller %s:\n", node->dump_name ());
572 : 2025 : for (cs = node->callees; cs; cs = cs->next_callee)
573 : : {
574 : :
575 : 998 : fprintf (f, " callsite %s -> %s : \n",
576 : : node->dump_name (),
577 : 998 : cs->callee->dump_name ());
578 : 998 : if (!ipa_edge_args_info_available_for_edge_p (cs))
579 : 470 : fprintf (f, " no arg info\n");
580 : : else
581 : 528 : ipa_print_node_jump_functions_for_edge (f, cs);
582 : : }
583 : :
584 : 1174 : for (cs = node->indirect_calls; cs; cs = cs->next_callee)
585 : : {
586 : 147 : class cgraph_indirect_call_info *ii;
587 : :
588 : 147 : ii = cs->indirect_info;
589 : 147 : if (ii->agg_contents)
590 : 20 : fprintf (f, " indirect %s callsite, calling param %i, "
591 : : "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
592 : 20 : ii->member_ptr ? "member ptr" : "aggregate",
593 : : ii->param_index, ii->offset,
594 : 20 : ii->by_ref ? "by reference" : "by_value");
595 : : else
596 : 127 : fprintf (f, " indirect %s callsite, calling param %i, "
597 : : "offset " HOST_WIDE_INT_PRINT_DEC,
598 : 127 : ii->polymorphic ? "polymorphic" : "simple", ii->param_index,
599 : : ii->offset);
600 : :
601 : 147 : if (cs->call_stmt)
602 : : {
603 : 131 : fprintf (f, ", for stmt ");
604 : 131 : print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
605 : : }
606 : : else
607 : 16 : fprintf (f, "\n");
608 : 147 : if (ii->polymorphic)
609 : 108 : ii->context.dump (f);
610 : 147 : if (!ipa_edge_args_info_available_for_edge_p (cs))
611 : 0 : fprintf (f, " no arg info\n");
612 : : else
613 : 147 : ipa_print_node_jump_functions_for_edge (f, cs);
614 : : }
615 : 1027 : }
616 : :
617 : : /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
618 : :
619 : : void
620 : 153 : ipa_print_all_jump_functions (FILE *f)
621 : : {
622 : 153 : struct cgraph_node *node;
623 : :
624 : 153 : fprintf (f, "\nJump functions:\n");
625 : 2332 : FOR_EACH_FUNCTION (node)
626 : : {
627 : 1013 : ipa_print_node_jump_functions (f, node);
628 : : }
629 : 153 : }
630 : :
631 : : /* Set jfunc to be a know-really nothing jump function. */
632 : :
633 : : static void
634 : 450797 : ipa_set_jf_unknown (struct ipa_jump_func *jfunc)
635 : : {
636 : 450797 : jfunc->type = IPA_JF_UNKNOWN;
637 : 437705 : }
638 : :
639 : : /* Set JFUNC to be a copy of another jmp (to be used by jump function
640 : : combination code). The two functions will share their rdesc. */
641 : :
642 : : static void
643 : 922352 : ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
644 : : struct ipa_jump_func *src)
645 : :
646 : : {
647 : 922352 : gcc_checking_assert (src->type == IPA_JF_CONST);
648 : 922352 : dst->type = IPA_JF_CONST;
649 : 922352 : dst->value.constant = src->value.constant;
650 : 922352 : }
651 : :
652 : : /* Set JFUNC to be a constant jmp function. */
653 : :
654 : : static void
655 : 2653038 : ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
656 : : struct cgraph_edge *cs)
657 : : {
658 : 2653038 : jfunc->type = IPA_JF_CONST;
659 : 2653038 : jfunc->value.constant.value = unshare_expr_without_location (constant);
660 : :
661 : 2653038 : if (TREE_CODE (constant) == ADDR_EXPR
662 : 2653038 : && (TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL
663 : 976940 : || (VAR_P (TREE_OPERAND (constant, 0))
664 : 358376 : && TREE_STATIC (TREE_OPERAND (constant, 0)))))
665 : : {
666 : 381999 : struct ipa_cst_ref_desc *rdesc;
667 : :
668 : 381999 : rdesc = ipa_refdesc_pool.allocate ();
669 : 381999 : rdesc->cs = cs;
670 : 381999 : rdesc->next_duplicate = NULL;
671 : 381999 : rdesc->refcount = 1;
672 : 381999 : jfunc->value.constant.rdesc = rdesc;
673 : : }
674 : : else
675 : 2271039 : jfunc->value.constant.rdesc = NULL;
676 : 2653038 : }
677 : :
678 : : /* Set JFUNC to be a simple pass-through jump function. */
679 : : static void
680 : 1328378 : ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
681 : : bool agg_preserved)
682 : : {
683 : 1328378 : jfunc->type = IPA_JF_PASS_THROUGH;
684 : 1328378 : jfunc->value.pass_through.operand = NULL_TREE;
685 : 1328378 : jfunc->value.pass_through.formal_id = formal_id;
686 : 1328378 : jfunc->value.pass_through.operation = NOP_EXPR;
687 : 1328378 : jfunc->value.pass_through.agg_preserved = agg_preserved;
688 : 1328378 : jfunc->value.pass_through.refdesc_decremented = false;
689 : 1192476 : }
690 : :
691 : : /* Set JFUNC to be an unary pass through jump function. */
692 : :
693 : : static void
694 : 1030 : ipa_set_jf_unary_pass_through (struct ipa_jump_func *jfunc, int formal_id,
695 : : enum tree_code operation)
696 : : {
697 : 1030 : jfunc->type = IPA_JF_PASS_THROUGH;
698 : 1030 : jfunc->value.pass_through.operand = NULL_TREE;
699 : 1030 : jfunc->value.pass_through.formal_id = formal_id;
700 : 1030 : jfunc->value.pass_through.operation = operation;
701 : 1030 : jfunc->value.pass_through.agg_preserved = false;
702 : 1030 : jfunc->value.pass_through.refdesc_decremented = false;
703 : 1030 : }
704 : : /* Set JFUNC to be an arithmetic pass through jump function. */
705 : :
706 : : static void
707 : 39706 : ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
708 : : tree operand, enum tree_code operation)
709 : : {
710 : 39706 : jfunc->type = IPA_JF_PASS_THROUGH;
711 : 0 : jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
712 : 39706 : jfunc->value.pass_through.formal_id = formal_id;
713 : 39706 : jfunc->value.pass_through.operation = operation;
714 : 39706 : jfunc->value.pass_through.agg_preserved = false;
715 : 39706 : jfunc->value.pass_through.refdesc_decremented = false;
716 : 39706 : }
717 : :
718 : : /* Set JFUNC to be an ancestor jump function. */
719 : :
720 : : static void
721 : 167126 : ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
722 : : int formal_id, bool agg_preserved, bool keep_null)
723 : : {
724 : 167126 : jfunc->type = IPA_JF_ANCESTOR;
725 : 167126 : jfunc->value.ancestor.formal_id = formal_id;
726 : 167126 : jfunc->value.ancestor.offset = offset;
727 : 167126 : jfunc->value.ancestor.agg_preserved = agg_preserved;
728 : 167126 : jfunc->value.ancestor.keep_null = keep_null;
729 : 166500 : }
730 : :
731 : : /* Get IPA BB information about the given BB. FBI is the context of analyzis
732 : : of this function body. */
733 : :
734 : : static struct ipa_bb_info *
735 : 31949211 : ipa_get_bb_info (struct ipa_func_body_info *fbi, basic_block bb)
736 : : {
737 : 26661893 : gcc_checking_assert (fbi);
738 : 31949211 : return &fbi->bb_infos[bb->index];
739 : : }
740 : :
741 : : /* Structure to be passed in between detect_type_change and
742 : : check_stmt_for_type_change. */
743 : :
744 : : struct prop_type_change_info
745 : : {
746 : : /* Offset into the object where there is the virtual method pointer we are
747 : : looking for. */
748 : : HOST_WIDE_INT offset;
749 : : /* The declaration or SSA_NAME pointer of the base that we are checking for
750 : : type change. */
751 : : tree object;
752 : : /* Set to true if dynamic type change has been detected. */
753 : : bool type_maybe_changed;
754 : : };
755 : :
756 : : /* Return true if STMT can modify a virtual method table pointer.
757 : :
758 : : This function makes special assumptions about both constructors and
759 : : destructors which are all the functions that are allowed to alter the VMT
760 : : pointers. It assumes that destructors begin with assignment into all VMT
761 : : pointers and that constructors essentially look in the following way:
762 : :
763 : : 1) The very first thing they do is that they call constructors of ancestor
764 : : sub-objects that have them.
765 : :
766 : : 2) Then VMT pointers of this and all its ancestors is set to new values
767 : : corresponding to the type corresponding to the constructor.
768 : :
769 : : 3) Only afterwards, other stuff such as constructor of member sub-objects
770 : : and the code written by the user is run. Only this may include calling
771 : : virtual functions, directly or indirectly.
772 : :
773 : : There is no way to call a constructor of an ancestor sub-object in any
774 : : other way.
775 : :
776 : : This means that we do not have to care whether constructors get the correct
777 : : type information because they will always change it (in fact, if we define
778 : : the type to be given by the VMT pointer, it is undefined).
779 : :
780 : : The most important fact to derive from the above is that if, for some
781 : : statement in the section 3, we try to detect whether the dynamic type has
782 : : changed, we can safely ignore all calls as we examine the function body
783 : : backwards until we reach statements in section 2 because these calls cannot
784 : : be ancestor constructors or destructors (if the input is not bogus) and so
785 : : do not change the dynamic type (this holds true only for automatically
786 : : allocated objects but at the moment we devirtualize only these). We then
787 : : must detect that statements in section 2 change the dynamic type and can try
788 : : to derive the new type. That is enough and we can stop, we will never see
789 : : the calls into constructors of sub-objects in this code. Therefore we can
790 : : safely ignore all call statements that we traverse.
791 : : */
792 : :
793 : : static bool
794 : 13 : stmt_may_be_vtbl_ptr_store (gimple *stmt)
795 : : {
796 : 13 : if (is_gimple_call (stmt))
797 : : return false;
798 : 4 : if (gimple_clobber_p (stmt))
799 : : return false;
800 : 0 : else if (is_gimple_assign (stmt))
801 : : {
802 : 0 : tree lhs = gimple_assign_lhs (stmt);
803 : :
804 : 0 : if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
805 : : {
806 : 0 : if (flag_strict_aliasing
807 : 0 : && !POINTER_TYPE_P (TREE_TYPE (lhs)))
808 : : return false;
809 : :
810 : 0 : if (TREE_CODE (lhs) == COMPONENT_REF
811 : 0 : && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
812 : 0 : return false;
813 : : /* In the future we might want to use get_ref_base_and_extent to find
814 : : if there is a field corresponding to the offset and if so, proceed
815 : : almost like if it was a component ref. */
816 : : }
817 : : }
818 : : return true;
819 : : }
820 : :
821 : : /* Callback of walk_aliased_vdefs and a helper function for detect_type_change
822 : : to check whether a particular statement may modify the virtual table
823 : : pointerIt stores its result into DATA, which points to a
824 : : prop_type_change_info structure. */
825 : :
826 : : static bool
827 : 13 : check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
828 : : {
829 : 13 : gimple *stmt = SSA_NAME_DEF_STMT (vdef);
830 : 13 : struct prop_type_change_info *tci = (struct prop_type_change_info *) data;
831 : :
832 : 13 : if (stmt_may_be_vtbl_ptr_store (stmt))
833 : : {
834 : 0 : tci->type_maybe_changed = true;
835 : 0 : return true;
836 : : }
837 : : else
838 : : return false;
839 : : }
840 : :
841 : : /* See if ARG is PARAM_DECl describing instance passed by pointer
842 : : or reference in FUNCTION. Return false if the dynamic type may change
843 : : in between beggining of the function until CALL is invoked.
844 : :
845 : : Generally functions are not allowed to change type of such instances,
846 : : but they call destructors. We assume that methods cannot destroy the THIS
847 : : pointer. Also as a special cases, constructor and destructors may change
848 : : type of the THIS pointer. */
849 : :
850 : : static bool
851 : 9633 : param_type_may_change_p (tree function, tree arg, gimple *call)
852 : : {
853 : : /* Pure functions cannot do any changes on the dynamic type;
854 : : that require writting to memory. */
855 : 9633 : if (flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
856 : : return false;
857 : : /* We need to check if we are within inlined consturctor
858 : : or destructor (ideally we would have way to check that the
859 : : inline cdtor is actually working on ARG, but we don't have
860 : : easy tie on this, so punt on all non-pure cdtors.
861 : : We may also record the types of cdtors and once we know type
862 : : of the instance match them.
863 : :
864 : : Also code unification optimizations may merge calls from
865 : : different blocks making return values unreliable. So
866 : : do nothing during late optimization. */
867 : 9633 : if (DECL_STRUCT_FUNCTION (function)->after_inlining)
868 : : return true;
869 : 9633 : if (TREE_CODE (arg) == SSA_NAME
870 : 9633 : && SSA_NAME_IS_DEFAULT_DEF (arg)
871 : 19266 : && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
872 : : {
873 : : /* Normal (non-THIS) argument. */
874 : 9633 : if ((SSA_NAME_VAR (arg) != DECL_ARGUMENTS (function)
875 : 9002 : || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE)
876 : : /* THIS pointer of an method - here we want to watch constructors
877 : : and destructors as those definitely may change the dynamic
878 : : type. */
879 : 18058 : || (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
880 : 8425 : && !DECL_CXX_CONSTRUCTOR_P (function)
881 : 8424 : && !DECL_CXX_DESTRUCTOR_P (function)
882 : 8423 : && (SSA_NAME_VAR (arg) == DECL_ARGUMENTS (function))))
883 : : {
884 : : /* Walk the inline stack and watch out for ctors/dtors. */
885 : 42145 : for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
886 : 16257 : block = BLOCK_SUPERCONTEXT (block))
887 : 16272 : if (inlined_polymorphic_ctor_dtor_block_p (block, false))
888 : : return true;
889 : : return false;
890 : : }
891 : : }
892 : : return true;
893 : : }
894 : :
895 : : /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
896 : : callsite CALL) by looking for assignments to its virtual table pointer. If
897 : : it is, return true. ARG is the object itself (not a pointer
898 : : to it, unless dereferenced). BASE is the base of the memory access as
899 : : returned by get_ref_base_and_extent, as is the offset.
900 : :
901 : : This is helper function for detect_type_change and detect_type_change_ssa
902 : : that does the heavy work which is usually unnecesary. */
903 : :
904 : : static bool
905 : 17 : detect_type_change_from_memory_writes (ipa_func_body_info *fbi, tree arg,
906 : : tree base, tree comp_type, gcall *call,
907 : : HOST_WIDE_INT offset)
908 : : {
909 : 17 : struct prop_type_change_info tci;
910 : 17 : ao_ref ao;
911 : :
912 : 17 : gcc_checking_assert (DECL_P (arg)
913 : : || TREE_CODE (arg) == MEM_REF
914 : : || handled_component_p (arg));
915 : :
916 : 17 : comp_type = TYPE_MAIN_VARIANT (comp_type);
917 : :
918 : : /* Const calls cannot call virtual methods through VMT and so type changes do
919 : : not matter. */
920 : 17 : if (!flag_devirtualize || !gimple_vuse (call)
921 : : /* Be sure expected_type is polymorphic. */
922 : 17 : || !comp_type
923 : 17 : || TREE_CODE (comp_type) != RECORD_TYPE
924 : 17 : || !TYPE_BINFO (TYPE_MAIN_VARIANT (comp_type))
925 : 34 : || !BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (comp_type))))
926 : : return true;
927 : :
928 : 17 : if (fbi->aa_walk_budget == 0)
929 : : return false;
930 : :
931 : 17 : ao_ref_init (&ao, arg);
932 : 17 : ao.base = base;
933 : 17 : ao.offset = offset;
934 : 17 : ao.size = POINTER_SIZE;
935 : 17 : ao.max_size = ao.size;
936 : :
937 : 17 : tci.offset = offset;
938 : 17 : tci.object = get_base_address (arg);
939 : 17 : tci.type_maybe_changed = false;
940 : :
941 : 17 : int walked
942 : 34 : = walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
943 : : &tci, NULL, NULL, fbi->aa_walk_budget);
944 : 17 : if (walked >= 0)
945 : 17 : fbi->aa_walk_budget -= walked;
946 : : else
947 : 0 : fbi->aa_walk_budget = 0;
948 : :
949 : 17 : if (walked >= 0 && !tci.type_maybe_changed)
950 : : return false;
951 : :
952 : : return true;
953 : : }
954 : :
955 : : /* Detect whether the dynamic type of ARG of COMP_TYPE may have changed.
956 : : If it is, return true. ARG is the object itself (not a pointer
957 : : to it, unless dereferenced). BASE is the base of the memory access as
958 : : returned by get_ref_base_and_extent, as is the offset. */
959 : :
960 : : static bool
961 : 753 : detect_type_change (ipa_func_body_info *fbi, tree arg, tree base,
962 : : tree comp_type, gcall *call,
963 : : HOST_WIDE_INT offset)
964 : : {
965 : 753 : if (!flag_devirtualize)
966 : : return false;
967 : :
968 : 753 : if (TREE_CODE (base) == MEM_REF
969 : 1506 : && !param_type_may_change_p (current_function_decl,
970 : 753 : TREE_OPERAND (base, 0),
971 : : call))
972 : : return false;
973 : 12 : return detect_type_change_from_memory_writes (fbi, arg, base, comp_type,
974 : 12 : call, offset);
975 : : }
976 : :
977 : : /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
978 : : SSA name (its dereference will become the base and the offset is assumed to
979 : : be zero). */
980 : :
981 : : static bool
982 : 8880 : detect_type_change_ssa (ipa_func_body_info *fbi, tree arg, tree comp_type,
983 : : gcall *call)
984 : : {
985 : 8880 : gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
986 : 8880 : if (!flag_devirtualize
987 : 8880 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
988 : : return false;
989 : :
990 : 8880 : if (!param_type_may_change_p (current_function_decl, arg, call))
991 : : return false;
992 : :
993 : 5 : arg = build2 (MEM_REF, ptr_type_node, arg,
994 : 5 : build_int_cst (ptr_type_node, 0));
995 : :
996 : 5 : return detect_type_change_from_memory_writes (fbi, arg, arg, comp_type,
997 : 5 : call, 0);
998 : : }
999 : :
1000 : : /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
1001 : : boolean variable pointed to by DATA. */
1002 : :
1003 : : static bool
1004 : 1584792 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1005 : : void *data)
1006 : : {
1007 : 1584792 : bool *b = (bool *) data;
1008 : 1584792 : *b = true;
1009 : 1584792 : return true;
1010 : : }
1011 : :
1012 : : /* Find the nearest valid aa status for parameter specified by INDEX that
1013 : : dominates BB. */
1014 : :
1015 : : static struct ipa_param_aa_status *
1016 : 4134593 : find_dominating_aa_status (struct ipa_func_body_info *fbi, basic_block bb,
1017 : : int index)
1018 : : {
1019 : 12578010 : while (true)
1020 : : {
1021 : 12578010 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1022 : 12578010 : if (!bb)
1023 : : return NULL;
1024 : 9791656 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1025 : 11757834 : if (!bi->param_aa_statuses.is_empty ()
1026 : 1966178 : && bi->param_aa_statuses[index].valid)
1027 : 1348239 : return &bi->param_aa_statuses[index];
1028 : : }
1029 : : }
1030 : :
1031 : : /* Get AA status structure for the given BB and parameter with INDEX. Allocate
1032 : : structures and/or intialize the result with a dominating description as
1033 : : necessary. */
1034 : :
1035 : : static struct ipa_param_aa_status *
1036 : 6233614 : parm_bb_aa_status_for_bb (struct ipa_func_body_info *fbi, basic_block bb,
1037 : : int index)
1038 : : {
1039 : 6233614 : gcc_checking_assert (fbi);
1040 : 6233614 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1041 : 6233614 : if (bi->param_aa_statuses.is_empty ())
1042 : 3543592 : bi->param_aa_statuses.safe_grow_cleared (fbi->param_count, true);
1043 : 6233614 : struct ipa_param_aa_status *paa = &bi->param_aa_statuses[index];
1044 : 6233614 : if (!paa->valid)
1045 : : {
1046 : 4134593 : gcc_checking_assert (!paa->parm_modified
1047 : : && !paa->ref_modified
1048 : : && !paa->pt_modified);
1049 : 4134593 : struct ipa_param_aa_status *dom_paa;
1050 : 4134593 : dom_paa = find_dominating_aa_status (fbi, bb, index);
1051 : 4134593 : if (dom_paa)
1052 : 1348239 : *paa = *dom_paa;
1053 : : else
1054 : 2786354 : paa->valid = true;
1055 : : }
1056 : :
1057 : 6233614 : return paa;
1058 : : }
1059 : :
1060 : : /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
1061 : : a value known not to be modified in this function before reaching the
1062 : : statement STMT. FBI holds information about the function we have so far
1063 : : gathered but do not survive the summary building stage. */
1064 : :
1065 : : static bool
1066 : 1115622 : parm_preserved_before_stmt_p (struct ipa_func_body_info *fbi, int index,
1067 : : gimple *stmt, tree parm_load)
1068 : : {
1069 : 1115622 : struct ipa_param_aa_status *paa;
1070 : 1115622 : bool modified = false;
1071 : 1115622 : ao_ref refd;
1072 : :
1073 : 1115622 : tree base = get_base_address (parm_load);
1074 : 1115622 : gcc_assert (TREE_CODE (base) == PARM_DECL);
1075 : 1115622 : if (TREE_READONLY (base))
1076 : : return true;
1077 : :
1078 : 1039365 : gcc_checking_assert (fbi);
1079 : 1039365 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1080 : 1039365 : if (paa->parm_modified || fbi->aa_walk_budget == 0)
1081 : : return false;
1082 : :
1083 : 1767294 : gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
1084 : 883647 : ao_ref_init (&refd, parm_load);
1085 : 1767294 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1086 : : &modified, NULL, NULL,
1087 : : fbi->aa_walk_budget);
1088 : 883647 : if (walked < 0)
1089 : : {
1090 : 8 : modified = true;
1091 : 8 : fbi->aa_walk_budget = 0;
1092 : : }
1093 : : else
1094 : 883639 : fbi->aa_walk_budget -= walked;
1095 : 883647 : if (paa && modified)
1096 : 89344 : paa->parm_modified = true;
1097 : 883647 : return !modified;
1098 : : }
1099 : :
1100 : : /* If STMT is an assignment that loads a value from an parameter declaration,
1101 : : return the index of the parameter in ipa_node_params which has not been
1102 : : modified. Otherwise return -1. */
1103 : :
1104 : : static int
1105 : 6278322 : load_from_unmodified_param (struct ipa_func_body_info *fbi,
1106 : : vec<ipa_param_descriptor, va_gc> *descriptors,
1107 : : gimple *stmt)
1108 : : {
1109 : 6278322 : int index;
1110 : 6278322 : tree op1;
1111 : :
1112 : 6278322 : if (!gimple_assign_single_p (stmt))
1113 : : return -1;
1114 : :
1115 : 4196546 : op1 = gimple_assign_rhs1 (stmt);
1116 : 4196546 : if (TREE_CODE (op1) != PARM_DECL)
1117 : : return -1;
1118 : :
1119 : 69710 : index = ipa_get_param_decl_index_1 (descriptors, op1);
1120 : 69710 : if (index < 0
1121 : 69710 : || !parm_preserved_before_stmt_p (fbi, index, stmt, op1))
1122 : 24627 : return -1;
1123 : :
1124 : : return index;
1125 : : }
1126 : :
1127 : : /* Return true if memory reference REF (which must be a load through parameter
1128 : : with INDEX) loads data that are known to be unmodified in this function
1129 : : before reaching statement STMT. */
1130 : :
1131 : : static bool
1132 : 4409636 : parm_ref_data_preserved_p (struct ipa_func_body_info *fbi,
1133 : : int index, gimple *stmt, tree ref)
1134 : : {
1135 : 4409636 : struct ipa_param_aa_status *paa;
1136 : 4409636 : bool modified = false;
1137 : 4409636 : ao_ref refd;
1138 : :
1139 : 4409636 : gcc_checking_assert (fbi);
1140 : 4409636 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1141 : 4409636 : if (paa->ref_modified || fbi->aa_walk_budget == 0)
1142 : : return false;
1143 : :
1144 : 6847526 : gcc_checking_assert (gimple_vuse (stmt));
1145 : 3423763 : ao_ref_init (&refd, ref);
1146 : 6847526 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1147 : : &modified, NULL, NULL,
1148 : : fbi->aa_walk_budget);
1149 : 3423763 : if (walked < 0)
1150 : : {
1151 : 8 : modified = true;
1152 : 8 : fbi->aa_walk_budget = 0;
1153 : : }
1154 : : else
1155 : 3423755 : fbi->aa_walk_budget -= walked;
1156 : 3423763 : if (modified)
1157 : 590759 : paa->ref_modified = true;
1158 : 3423763 : return !modified;
1159 : : }
1160 : :
1161 : : /* Return true if the data pointed to by PARM (which is a parameter with INDEX)
1162 : : is known to be unmodified in this function before reaching call statement
1163 : : CALL into which it is passed. FBI describes the function body. */
1164 : :
1165 : : static bool
1166 : 1104341 : parm_ref_data_pass_through_p (struct ipa_func_body_info *fbi, int index,
1167 : : gimple *call, tree parm)
1168 : : {
1169 : 1104341 : bool modified = false;
1170 : 1104341 : ao_ref refd;
1171 : :
1172 : : /* It's unnecessary to calculate anything about memory contnets for a const
1173 : : function because it is not goin to use it. But do not cache the result
1174 : : either. Also, no such calculations for non-pointers. */
1175 : 1540185 : if (!gimple_vuse (call)
1176 : 1104341 : || !POINTER_TYPE_P (TREE_TYPE (parm)))
1177 : : return false;
1178 : :
1179 : 784613 : struct ipa_param_aa_status *paa = parm_bb_aa_status_for_bb (fbi,
1180 : : gimple_bb (call),
1181 : : index);
1182 : 784613 : if (paa->pt_modified || fbi->aa_walk_budget == 0)
1183 : : return false;
1184 : :
1185 : 668497 : ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
1186 : 1336994 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified,
1187 : : &modified, NULL, NULL,
1188 : : fbi->aa_walk_budget);
1189 : 668497 : if (walked < 0)
1190 : : {
1191 : 0 : fbi->aa_walk_budget = 0;
1192 : 0 : modified = true;
1193 : : }
1194 : : else
1195 : 668497 : fbi->aa_walk_budget -= walked;
1196 : 668497 : if (modified)
1197 : 262666 : paa->pt_modified = true;
1198 : 668497 : return !modified;
1199 : : }
1200 : :
1201 : : /* Return true if we can prove that OP is a memory reference loading
1202 : : data from an aggregate passed as a parameter.
1203 : :
1204 : : The function works in two modes. If GUARANTEED_UNMODIFIED is NULL, it return
1205 : : false if it cannot prove that the value has not been modified before the
1206 : : load in STMT. If GUARANTEED_UNMODIFIED is not NULL, it will return true even
1207 : : if it cannot prove the value has not been modified, in that case it will
1208 : : store false to *GUARANTEED_UNMODIFIED, otherwise it will store true there.
1209 : :
1210 : : INFO and PARMS_AINFO describe parameters of the current function (but the
1211 : : latter can be NULL), STMT is the load statement. If function returns true,
1212 : : *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
1213 : : within the aggregate and whether it is a load from a value passed by
1214 : : reference respectively.
1215 : :
1216 : : Return false if the offset divided by BITS_PER_UNIT would not fit into an
1217 : : unsigned int. */
1218 : :
1219 : : bool
1220 : 24410335 : ipa_load_from_parm_agg (struct ipa_func_body_info *fbi,
1221 : : vec<ipa_param_descriptor, va_gc> *descriptors,
1222 : : gimple *stmt, tree op, int *index_p,
1223 : : HOST_WIDE_INT *offset_p, poly_int64 *size_p,
1224 : : bool *by_ref_p, bool *guaranteed_unmodified)
1225 : : {
1226 : 24410335 : int index;
1227 : 24410335 : HOST_WIDE_INT size;
1228 : 24410335 : bool reverse;
1229 : 24410335 : tree base = get_ref_base_and_extent_hwi (op, offset_p, &size, &reverse);
1230 : :
1231 : 24410335 : if (!base
1232 : 23106655 : || (*offset_p / BITS_PER_UNIT) > UINT_MAX)
1233 : : return false;
1234 : :
1235 : : /* We can not propagate across volatile loads. */
1236 : 23106624 : if (TREE_THIS_VOLATILE (op))
1237 : : return false;
1238 : :
1239 : 21905914 : if (DECL_P (base))
1240 : : {
1241 : 10930711 : int index = ipa_get_param_decl_index_1 (descriptors, base);
1242 : 10930711 : if (index >= 0
1243 : 10930711 : && parm_preserved_before_stmt_p (fbi, index, stmt, op))
1244 : : {
1245 : 758521 : *index_p = index;
1246 : 758521 : *by_ref_p = false;
1247 : 758521 : if (size_p)
1248 : 24694 : *size_p = size;
1249 : 758521 : if (guaranteed_unmodified)
1250 : 151 : *guaranteed_unmodified = true;
1251 : 758521 : return true;
1252 : : }
1253 : 10172190 : return false;
1254 : : }
1255 : :
1256 : 10975203 : if (TREE_CODE (base) != MEM_REF
1257 : 10295112 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1258 : 21266789 : || !integer_zerop (TREE_OPERAND (base, 1)))
1259 : 1554916 : return false;
1260 : :
1261 : 9420287 : if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1262 : : {
1263 : 4524550 : tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1264 : 4524550 : index = ipa_get_param_decl_index_1 (descriptors, parm);
1265 : : }
1266 : : else
1267 : : {
1268 : : /* This branch catches situations where a pointer parameter is not a
1269 : : gimple register, for example:
1270 : :
1271 : : void hip7(S*) (struct S * p)
1272 : : {
1273 : : void (*<T2e4>) (struct S *) D.1867;
1274 : : struct S * p.1;
1275 : :
1276 : : <bb 2>:
1277 : : p.1_1 = p;
1278 : : D.1867_2 = p.1_1->f;
1279 : : D.1867_2 ();
1280 : : gdp = &p;
1281 : : */
1282 : :
1283 : 4895737 : gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
1284 : 4895737 : index = load_from_unmodified_param (fbi, descriptors, def);
1285 : : }
1286 : :
1287 : 9420287 : if (index >= 0)
1288 : : {
1289 : 4409350 : bool data_preserved = parm_ref_data_preserved_p (fbi, index, stmt, op);
1290 : 4409350 : if (!data_preserved && !guaranteed_unmodified)
1291 : : return false;
1292 : :
1293 : 2833138 : *index_p = index;
1294 : 2833138 : *by_ref_p = true;
1295 : 2833138 : if (size_p)
1296 : 22301 : *size_p = size;
1297 : 2833138 : if (guaranteed_unmodified)
1298 : 2104 : *guaranteed_unmodified = data_preserved;
1299 : 2833138 : return true;
1300 : : }
1301 : : return false;
1302 : : }
1303 : :
1304 : : /* If STMT is an assignment that loads a value from a parameter declaration,
1305 : : or from an aggregate passed as the parameter either by value or reference,
1306 : : return the index of the parameter in ipa_node_params. Otherwise return -1.
1307 : :
1308 : : FBI holds gathered information about the function. INFO describes
1309 : : parameters of the function, STMT is the assignment statement. If it is a
1310 : : memory load from an aggregate, *OFFSET_P is filled with offset within the
1311 : : aggregate, and *BY_REF_P specifies whether the aggregate is passed by
1312 : : reference. */
1313 : :
1314 : : static int
1315 : 320528 : load_from_unmodified_param_or_agg (struct ipa_func_body_info *fbi,
1316 : : class ipa_node_params *info,
1317 : : gimple *stmt,
1318 : : HOST_WIDE_INT *offset_p,
1319 : : bool *by_ref_p)
1320 : : {
1321 : 320528 : int index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1322 : 320528 : poly_int64 size;
1323 : :
1324 : : /* Load value from a parameter declaration. */
1325 : 320528 : if (index >= 0)
1326 : : {
1327 : 176 : *offset_p = -1;
1328 : 176 : return index;
1329 : : }
1330 : :
1331 : 320352 : if (!gimple_assign_load_p (stmt))
1332 : : return -1;
1333 : :
1334 : 187789 : tree rhs = gimple_assign_rhs1 (stmt);
1335 : :
1336 : : /* Skip memory reference containing VIEW_CONVERT_EXPR. */
1337 : 419420 : for (tree t = rhs; handled_component_p (t); t = TREE_OPERAND (t, 0))
1338 : 231635 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1339 : : return -1;
1340 : :
1341 : : /* Skip memory reference containing bit-field. */
1342 : 187785 : if (TREE_CODE (rhs) == BIT_FIELD_REF
1343 : 187785 : || contains_bitfld_component_ref_p (rhs))
1344 : 0 : return -1;
1345 : :
1346 : 187785 : if (!ipa_load_from_parm_agg (fbi, info->descriptors, stmt, rhs, &index,
1347 : : offset_p, &size, by_ref_p))
1348 : : return -1;
1349 : :
1350 : 44532 : gcc_assert (!maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (rhs))),
1351 : : size));
1352 : 44532 : if (!*by_ref_p)
1353 : : {
1354 : 24173 : tree param_type = ipa_get_type (info, index);
1355 : :
1356 : 24173 : if (!param_type || !AGGREGATE_TYPE_P (param_type))
1357 : : return -1;
1358 : : }
1359 : 20359 : else if (TREE_THIS_VOLATILE (rhs))
1360 : : return -1;
1361 : :
1362 : 43141 : return index;
1363 : : }
1364 : :
1365 : : /* Walk pointer adjustemnts from OP (such as POINTER_PLUS and ADDR_EXPR)
1366 : : to find original pointer. Initialize RET to the pointer which results from
1367 : : the walk.
1368 : : If offset is known return true and initialize OFFSET_RET. */
1369 : :
1370 : : bool
1371 : 15075729 : unadjusted_ptr_and_unit_offset (tree op, tree *ret, poly_int64 *offset_ret)
1372 : : {
1373 : 15075729 : poly_int64 offset = 0;
1374 : 15075729 : bool offset_known = true;
1375 : 15075729 : int i;
1376 : :
1377 : 19405953 : for (i = 0; i < param_ipa_jump_function_lookups; i++)
1378 : : {
1379 : 19404635 : if (TREE_CODE (op) == ADDR_EXPR)
1380 : : {
1381 : 1476251 : poly_int64 extra_offset;
1382 : 1476251 : tree base = get_addr_base_and_unit_offset (TREE_OPERAND (op, 0),
1383 : : &extra_offset);
1384 : 1476251 : if (!base)
1385 : : {
1386 : 23071 : base = get_base_address (TREE_OPERAND (op, 0));
1387 : 23071 : if (TREE_CODE (base) != MEM_REF)
1388 : : break;
1389 : : offset_known = false;
1390 : : }
1391 : : else
1392 : : {
1393 : 1453180 : if (TREE_CODE (base) != MEM_REF)
1394 : : break;
1395 : 231957 : offset += extra_offset;
1396 : : }
1397 : 231957 : op = TREE_OPERAND (base, 0);
1398 : 231957 : if (mem_ref_offset (base).to_shwi (&extra_offset))
1399 : 231957 : offset += extra_offset;
1400 : : else
1401 : : offset_known = false;
1402 : : }
1403 : 17928384 : else if (TREE_CODE (op) == SSA_NAME
1404 : 17928384 : && !SSA_NAME_IS_DEFAULT_DEF (op))
1405 : : {
1406 : 6074736 : gimple *pstmt = SSA_NAME_DEF_STMT (op);
1407 : :
1408 : 6074736 : if (gimple_assign_single_p (pstmt))
1409 : 2953933 : op = gimple_assign_rhs1 (pstmt);
1410 : 3120803 : else if (is_gimple_assign (pstmt)
1411 : 3120803 : && gimple_assign_rhs_code (pstmt) == POINTER_PLUS_EXPR)
1412 : : {
1413 : 1144334 : poly_int64 extra_offset = 0;
1414 : 1144334 : if (ptrdiff_tree_p (gimple_assign_rhs2 (pstmt),
1415 : : &extra_offset))
1416 : 1144334 : offset += extra_offset;
1417 : : else
1418 : : offset_known = false;
1419 : 1144334 : op = gimple_assign_rhs1 (pstmt);
1420 : : }
1421 : : else
1422 : : break;
1423 : : }
1424 : : else
1425 : : break;
1426 : : }
1427 : 15075729 : *ret = op;
1428 : 15075729 : *offset_ret = offset;
1429 : 15075729 : return offset_known;
1430 : : }
1431 : :
1432 : : /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
1433 : : of an assignment statement STMT, try to determine whether we are actually
1434 : : handling any of the following cases and construct an appropriate jump
1435 : : function into JFUNC if so:
1436 : :
1437 : : 1) The passed value is loaded from a formal parameter which is not a gimple
1438 : : register (most probably because it is addressable, the value has to be
1439 : : scalar) and we can guarantee the value has not changed. This case can
1440 : : therefore be described by a simple pass-through jump function. For example:
1441 : :
1442 : : foo (int a)
1443 : : {
1444 : : int a.0;
1445 : :
1446 : : a.0_2 = a;
1447 : : bar (a.0_2);
1448 : :
1449 : : 2) The passed value can be described by a simple arithmetic pass-through
1450 : : jump function. E.g.
1451 : :
1452 : : foo (int a)
1453 : : {
1454 : : int D.2064;
1455 : :
1456 : : D.2064_4 = a.1(D) + 4;
1457 : : bar (D.2064_4);
1458 : :
1459 : : This case can also occur in combination of the previous one, e.g.:
1460 : :
1461 : : foo (int a, int z)
1462 : : {
1463 : : int a.0;
1464 : : int D.2064;
1465 : :
1466 : : a.0_3 = a;
1467 : : D.2064_4 = a.0_3 + 4;
1468 : : foo (D.2064_4);
1469 : :
1470 : : 3) The passed value is an address of an object within another one (which
1471 : : also passed by reference). Such situations are described by an ancestor
1472 : : jump function and describe situations such as:
1473 : :
1474 : : B::foo() (struct B * const this)
1475 : : {
1476 : : struct A * D.1845;
1477 : :
1478 : : D.1845_2 = &this_1(D)->D.1748;
1479 : : A::bar (D.1845_2);
1480 : :
1481 : : INFO is the structure describing individual parameters access different
1482 : : stages of IPA optimizations. PARMS_AINFO contains the information that is
1483 : : only needed for intraprocedural analysis. */
1484 : :
1485 : : static void
1486 : 1170288 : compute_complex_assign_jump_func (struct ipa_func_body_info *fbi,
1487 : : class ipa_node_params *info,
1488 : : struct ipa_jump_func *jfunc,
1489 : : gcall *call, gimple *stmt, tree name,
1490 : : tree param_type)
1491 : : {
1492 : 1170288 : HOST_WIDE_INT offset, size;
1493 : 1170288 : tree op1, tc_ssa, base, ssa;
1494 : 1170288 : bool reverse;
1495 : 1170288 : int index;
1496 : :
1497 : 1170288 : op1 = gimple_assign_rhs1 (stmt);
1498 : :
1499 : 1170288 : if (TREE_CODE (op1) == SSA_NAME)
1500 : : {
1501 : 361275 : if (SSA_NAME_IS_DEFAULT_DEF (op1))
1502 : 108231 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1503 : : else
1504 : 253044 : index = load_from_unmodified_param (fbi, info->descriptors,
1505 : 253044 : SSA_NAME_DEF_STMT (op1));
1506 : : tc_ssa = op1;
1507 : : }
1508 : : else
1509 : : {
1510 : 809013 : index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1511 : 809013 : tc_ssa = gimple_assign_lhs (stmt);
1512 : : }
1513 : :
1514 : 1170288 : if (index >= 0)
1515 : : {
1516 : 109648 : switch (gimple_assign_rhs_class (stmt))
1517 : : {
1518 : 61139 : case GIMPLE_BINARY_RHS:
1519 : 61139 : {
1520 : 61139 : tree op2 = gimple_assign_rhs2 (stmt);
1521 : 61139 : if (!is_gimple_ip_invariant (op2)
1522 : 61139 : || ((TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1523 : : != tcc_comparison)
1524 : 31264 : && !useless_type_conversion_p (TREE_TYPE (name),
1525 : 31264 : TREE_TYPE (op1))))
1526 : 1020719 : return;
1527 : :
1528 : 38274 : ipa_set_jf_arith_pass_through (jfunc, index, op2,
1529 : : gimple_assign_rhs_code (stmt));
1530 : 38274 : break;
1531 : : }
1532 : 1252 : case GIMPLE_SINGLE_RHS:
1533 : 1252 : {
1534 : 1252 : bool agg_p = parm_ref_data_pass_through_p (fbi, index, call,
1535 : : tc_ssa);
1536 : 1252 : ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
1537 : 1252 : break;
1538 : : }
1539 : 47255 : case GIMPLE_UNARY_RHS:
1540 : 47255 : if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
1541 : 1002 : ipa_set_jf_unary_pass_through (jfunc, index,
1542 : : gimple_assign_rhs_code (stmt));
1543 : 86783 : default:;
1544 : : }
1545 : 86783 : return;
1546 : : }
1547 : :
1548 : 1060640 : if (TREE_CODE (op1) != ADDR_EXPR)
1549 : : return;
1550 : 220125 : op1 = TREE_OPERAND (op1, 0);
1551 : 220125 : base = get_ref_base_and_extent_hwi (op1, &offset, &size, &reverse);
1552 : 220125 : offset_int mem_offset;
1553 : 220125 : if (!base
1554 : 193361 : || TREE_CODE (base) != MEM_REF
1555 : 400815 : || !mem_ref_offset (base).is_constant (&mem_offset))
1556 : 39435 : return;
1557 : 180690 : offset += mem_offset.to_short_addr () * BITS_PER_UNIT;
1558 : 180690 : ssa = TREE_OPERAND (base, 0);
1559 : 180690 : if (TREE_CODE (ssa) != SSA_NAME
1560 : 180690 : || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1561 : 330358 : || offset < 0)
1562 : : return;
1563 : :
1564 : : /* Dynamic types are changed in constructors and destructors. */
1565 : 149569 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1566 : 149569 : if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1567 : 146570 : ipa_set_ancestor_jf (jfunc, offset, index,
1568 : 146570 : parm_ref_data_pass_through_p (fbi, index, call, ssa),
1569 : : false);
1570 : : }
1571 : :
1572 : : /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1573 : : it looks like:
1574 : :
1575 : : iftmp.1_3 = &obj_2(D)->D.1762;
1576 : :
1577 : : The base of the MEM_REF must be a default definition SSA NAME of a
1578 : : parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1579 : : whole MEM_REF expression is returned and the offset calculated from any
1580 : : handled components and the MEM_REF itself is stored into *OFFSET. The whole
1581 : : RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1582 : :
1583 : : static tree
1584 : 16324 : get_ancestor_addr_info (gimple *assign, tree *obj_p, HOST_WIDE_INT *offset)
1585 : : {
1586 : 16324 : HOST_WIDE_INT size;
1587 : 16324 : tree expr, parm, obj;
1588 : 16324 : bool reverse;
1589 : :
1590 : 16324 : if (!gimple_assign_single_p (assign))
1591 : : return NULL_TREE;
1592 : 9286 : expr = gimple_assign_rhs1 (assign);
1593 : :
1594 : 9286 : if (TREE_CODE (expr) != ADDR_EXPR)
1595 : : return NULL_TREE;
1596 : 4327 : expr = TREE_OPERAND (expr, 0);
1597 : 4327 : obj = expr;
1598 : 4327 : expr = get_ref_base_and_extent_hwi (expr, offset, &size, &reverse);
1599 : :
1600 : 4327 : offset_int mem_offset;
1601 : 4327 : if (!expr
1602 : 4325 : || TREE_CODE (expr) != MEM_REF
1603 : 8652 : || !mem_ref_offset (expr).is_constant (&mem_offset))
1604 : 2 : return NULL_TREE;
1605 : 4325 : parm = TREE_OPERAND (expr, 0);
1606 : 4325 : if (TREE_CODE (parm) != SSA_NAME
1607 : 4325 : || !SSA_NAME_IS_DEFAULT_DEF (parm)
1608 : 5104 : || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1609 : : return NULL_TREE;
1610 : :
1611 : 779 : *offset += mem_offset.to_short_addr () * BITS_PER_UNIT;
1612 : 779 : *obj_p = obj;
1613 : 779 : return expr;
1614 : : }
1615 : :
1616 : :
1617 : : /* Given that an actual argument is an SSA_NAME that is a result of a phi
1618 : : statement PHI, try to find out whether NAME is in fact a
1619 : : multiple-inheritance typecast from a descendant into an ancestor of a formal
1620 : : parameter and thus can be described by an ancestor jump function and if so,
1621 : : write the appropriate function into JFUNC.
1622 : :
1623 : : Essentially we want to match the following pattern:
1624 : :
1625 : : if (obj_2(D) != 0B)
1626 : : goto <bb 3>;
1627 : : else
1628 : : goto <bb 4>;
1629 : :
1630 : : <bb 3>:
1631 : : iftmp.1_3 = &obj_2(D)->D.1762;
1632 : :
1633 : : <bb 4>:
1634 : : # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1635 : : D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1636 : : return D.1879_6; */
1637 : :
1638 : : static void
1639 : 85060 : compute_complex_ancestor_jump_func (struct ipa_func_body_info *fbi,
1640 : : class ipa_node_params *info,
1641 : : struct ipa_jump_func *jfunc,
1642 : : gcall *call, gphi *phi)
1643 : : {
1644 : 85060 : HOST_WIDE_INT offset;
1645 : 85060 : gimple *assign;
1646 : 85060 : basic_block phi_bb, assign_bb, cond_bb;
1647 : 85060 : tree tmp, parm, expr, obj;
1648 : 85060 : int index, i;
1649 : :
1650 : 85060 : if (gimple_phi_num_args (phi) != 2)
1651 : 85046 : return;
1652 : :
1653 : 70530 : if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1654 : 4180 : tmp = PHI_ARG_DEF (phi, 0);
1655 : 66350 : else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1656 : 11222 : tmp = PHI_ARG_DEF (phi, 1);
1657 : : else
1658 : : return;
1659 : 15402 : if (TREE_CODE (tmp) != SSA_NAME
1660 : 13141 : || SSA_NAME_IS_DEFAULT_DEF (tmp)
1661 : 13010 : || !POINTER_TYPE_P (TREE_TYPE (tmp))
1662 : 18618 : || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1663 : : return;
1664 : :
1665 : 1173 : assign = SSA_NAME_DEF_STMT (tmp);
1666 : 1173 : assign_bb = gimple_bb (assign);
1667 : 85751 : if (!single_pred_p (assign_bb))
1668 : : return;
1669 : 705 : expr = get_ancestor_addr_info (assign, &obj, &offset);
1670 : 705 : if (!expr)
1671 : : return;
1672 : 26 : parm = TREE_OPERAND (expr, 0);
1673 : 26 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1674 : 26 : if (index < 0)
1675 : : return;
1676 : :
1677 : 26 : cond_bb = single_pred (assign_bb);
1678 : 52 : gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
1679 : 26 : if (!cond
1680 : 26 : || gimple_cond_code (cond) != NE_EXPR
1681 : 26 : || gimple_cond_lhs (cond) != parm
1682 : 14 : || !integer_zerop (gimple_cond_rhs (cond)))
1683 : 12 : return;
1684 : :
1685 : 14 : phi_bb = gimple_bb (phi);
1686 : 42 : for (i = 0; i < 2; i++)
1687 : : {
1688 : 28 : basic_block pred = EDGE_PRED (phi_bb, i)->src;
1689 : 28 : if (pred != assign_bb && pred != cond_bb)
1690 : : return;
1691 : : }
1692 : :
1693 : 14 : ipa_set_ancestor_jf (jfunc, offset, index,
1694 : 14 : parm_ref_data_pass_through_p (fbi, index, call, parm),
1695 : : true);
1696 : : }
1697 : :
1698 : : /* Inspect the given TYPE and return true iff it has the same structure (the
1699 : : same number of fields of the same types) as a C++ member pointer. If
1700 : : METHOD_PTR and DELTA are non-NULL, store the trees representing the
1701 : : corresponding fields there. */
1702 : :
1703 : : static bool
1704 : 848 : type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1705 : : {
1706 : 848 : tree fld;
1707 : :
1708 : 848 : if (TREE_CODE (type) != RECORD_TYPE)
1709 : : return false;
1710 : :
1711 : 848 : fld = TYPE_FIELDS (type);
1712 : 848 : if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1713 : 848 : || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1714 : 1696 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1715 : : return false;
1716 : :
1717 : 848 : if (method_ptr)
1718 : 848 : *method_ptr = fld;
1719 : :
1720 : 848 : fld = DECL_CHAIN (fld);
1721 : 848 : if (!fld || INTEGRAL_TYPE_P (fld)
1722 : 1696 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1723 : : return false;
1724 : 848 : if (delta)
1725 : 848 : *delta = fld;
1726 : :
1727 : 848 : if (DECL_CHAIN (fld))
1728 : : return false;
1729 : :
1730 : : return true;
1731 : : }
1732 : :
1733 : : /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1734 : : return the rhs of its defining statement, and this statement is stored in
1735 : : *RHS_STMT. Otherwise return RHS as it is. */
1736 : :
1737 : : static inline tree
1738 : 95405 : get_ssa_def_if_simple_copy (tree rhs, gimple **rhs_stmt)
1739 : : {
1740 : 128178 : while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1741 : : {
1742 : 68991 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
1743 : :
1744 : 68991 : if (gimple_assign_single_p (def_stmt))
1745 : 32773 : rhs = gimple_assign_rhs1 (def_stmt);
1746 : : else
1747 : : break;
1748 : 32773 : *rhs_stmt = def_stmt;
1749 : : }
1750 : 95405 : return rhs;
1751 : : }
1752 : :
1753 : : /* Simple linked list, describing contents of an aggregate before call. */
1754 : :
1755 : : struct ipa_known_agg_contents_list
1756 : : {
1757 : : /* Offset and size of the described part of the aggregate. */
1758 : : HOST_WIDE_INT offset, size;
1759 : :
1760 : : /* Type of the described part of the aggregate. */
1761 : : tree type;
1762 : :
1763 : : /* Known constant value or jump function data describing contents. */
1764 : : struct ipa_load_agg_data value;
1765 : :
1766 : : /* Pointer to the next structure in the list. */
1767 : : struct ipa_known_agg_contents_list *next;
1768 : : };
1769 : :
1770 : : /* Add an aggregate content item into a linked list of
1771 : : ipa_known_agg_contents_list structure, in which all elements
1772 : : are sorted ascendingly by offset. */
1773 : :
1774 : : static inline void
1775 : 2505659 : add_to_agg_contents_list (struct ipa_known_agg_contents_list **plist,
1776 : : struct ipa_known_agg_contents_list *item)
1777 : : {
1778 : 2505659 : struct ipa_known_agg_contents_list *list = *plist;
1779 : :
1780 : 4634771 : for (; list; list = list->next)
1781 : : {
1782 : 3598083 : if (list->offset >= item->offset)
1783 : : break;
1784 : :
1785 : 2129112 : plist = &list->next;
1786 : : }
1787 : :
1788 : 2505659 : item->next = list;
1789 : 2505659 : *plist = item;
1790 : : }
1791 : :
1792 : : /* Check whether a given aggregate content is clobbered by certain element in
1793 : : a linked list of ipa_known_agg_contents_list. */
1794 : :
1795 : : static inline bool
1796 : 971013 : clobber_by_agg_contents_list_p (struct ipa_known_agg_contents_list *list,
1797 : : struct ipa_known_agg_contents_list *item)
1798 : : {
1799 : 2107081 : for (; list; list = list->next)
1800 : : {
1801 : 1727325 : if (list->offset >= item->offset)
1802 : 578741 : return list->offset < item->offset + item->size;
1803 : :
1804 : 1148584 : if (list->offset + list->size > item->offset)
1805 : : return true;
1806 : : }
1807 : :
1808 : : return false;
1809 : : }
1810 : :
1811 : : /* Build aggregate jump function from LIST, assuming there are exactly
1812 : : VALUE_COUNT entries there and that offset of the passed argument
1813 : : is ARG_OFFSET and store it into JFUNC. */
1814 : :
1815 : : static void
1816 : 301667 : build_agg_jump_func_from_list (struct ipa_known_agg_contents_list *list,
1817 : : int value_count, HOST_WIDE_INT arg_offset,
1818 : : struct ipa_jump_func *jfunc)
1819 : : {
1820 : 301667 : vec_safe_reserve (jfunc->agg.items, value_count, true);
1821 : 1207615 : for (; list; list = list->next)
1822 : : {
1823 : 905948 : struct ipa_agg_jf_item item;
1824 : 905948 : tree operand = list->value.pass_through.operand;
1825 : :
1826 : 905948 : if (list->value.pass_through.formal_id >= 0)
1827 : : {
1828 : : /* Content value is derived from some formal paramerter. */
1829 : 69944 : if (list->value.offset >= 0)
1830 : 41869 : item.jftype = IPA_JF_LOAD_AGG;
1831 : : else
1832 : 28075 : item.jftype = IPA_JF_PASS_THROUGH;
1833 : :
1834 : 69944 : item.value.load_agg = list->value;
1835 : 69944 : if (operand)
1836 : 7152 : item.value.pass_through.operand
1837 : 7152 : = unshare_expr_without_location (operand);
1838 : : }
1839 : 836004 : else if (operand)
1840 : : {
1841 : : /* Content value is known constant. */
1842 : 836004 : item.jftype = IPA_JF_CONST;
1843 : 836004 : item.value.constant = unshare_expr_without_location (operand);
1844 : : }
1845 : : else
1846 : 0 : continue;
1847 : :
1848 : 905948 : item.type = list->type;
1849 : 905948 : gcc_assert (tree_to_shwi (TYPE_SIZE (list->type)) == list->size);
1850 : :
1851 : 905948 : item.offset = list->offset - arg_offset;
1852 : 905948 : gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1853 : :
1854 : 905948 : jfunc->agg.items->quick_push (item);
1855 : : }
1856 : 301667 : }
1857 : :
1858 : : /* Given an assignment statement STMT, try to collect information into
1859 : : AGG_VALUE that will be used to construct jump function for RHS of the
1860 : : assignment, from which content value of an aggregate part comes.
1861 : :
1862 : : Besides constant and simple pass-through jump functions, also try to
1863 : : identify whether it matches the following pattern that can be described by
1864 : : a load-value-from-aggregate jump function, which is a derivative of simple
1865 : : pass-through jump function.
1866 : :
1867 : : foo (int *p)
1868 : : {
1869 : : ...
1870 : :
1871 : : *(q_5 + 4) = *(p_3(D) + 28) op 1;
1872 : : bar (q_5);
1873 : : }
1874 : :
1875 : : Here IPA_LOAD_AGG_DATA data structure is informative enough to describe
1876 : : constant, simple pass-through and load-vale-from-aggregate. If value
1877 : : is constant, it will be kept in field OPERAND, and field FORMAL_ID is
1878 : : set to -1. For simple pass-through and load-value-from-aggregate, field
1879 : : FORMAL_ID specifies the related formal parameter index, and field
1880 : : OFFSET can be used to distinguish them, -1 means simple pass-through,
1881 : : otherwise means load-value-from-aggregate. */
1882 : :
1883 : : static void
1884 : 1601583 : analyze_agg_content_value (struct ipa_func_body_info *fbi,
1885 : : struct ipa_load_agg_data *agg_value,
1886 : : gimple *stmt)
1887 : : {
1888 : 1601583 : tree lhs = gimple_assign_lhs (stmt);
1889 : 1601583 : tree rhs1 = gimple_assign_rhs1 (stmt);
1890 : 1601583 : enum tree_code code;
1891 : 1601583 : int index = -1;
1892 : :
1893 : : /* Initialize jump function data for the aggregate part. */
1894 : 1601583 : memset (agg_value, 0, sizeof (*agg_value));
1895 : 1601583 : agg_value->pass_through.operation = NOP_EXPR;
1896 : 1601583 : agg_value->pass_through.formal_id = -1;
1897 : 1601583 : agg_value->offset = -1;
1898 : :
1899 : 1601583 : if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) /* TODO: Support aggregate type. */
1900 : 1397144 : || TREE_THIS_VOLATILE (lhs)
1901 : 1396692 : || TREE_CODE (lhs) == BIT_FIELD_REF
1902 : 1396684 : || contains_bitfld_component_ref_p (lhs))
1903 : 211344 : return;
1904 : :
1905 : : /* Skip SSA copies. */
1906 : 1648215 : while (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1907 : : {
1908 : 1584175 : if (TREE_CODE (rhs1) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (rhs1))
1909 : : break;
1910 : :
1911 : 350034 : stmt = SSA_NAME_DEF_STMT (rhs1);
1912 : 350034 : if (!is_gimple_assign (stmt))
1913 : : break;
1914 : :
1915 : 257976 : rhs1 = gimple_assign_rhs1 (stmt);
1916 : : }
1917 : :
1918 : 1390239 : if (gphi *phi = dyn_cast<gphi *> (stmt))
1919 : : {
1920 : : /* Also special case like the following (a is a formal parameter):
1921 : :
1922 : : _12 = *a_11(D).dim[0].stride;
1923 : : ...
1924 : : # iftmp.22_9 = PHI <_12(2), 1(3)>
1925 : : ...
1926 : : parm.6.dim[0].stride = iftmp.22_9;
1927 : : ...
1928 : : __x_MOD_foo (&parm.6, b_31(D));
1929 : :
1930 : : The aggregate function describing parm.6.dim[0].stride is encoded as a
1931 : : PASS-THROUGH jump function with ASSERT_EXPR operation whith operand 1
1932 : : (the constant from the PHI node). */
1933 : :
1934 : 33033 : if (gimple_phi_num_args (phi) != 2)
1935 : : return;
1936 : 27193 : tree arg0 = gimple_phi_arg_def (phi, 0);
1937 : 27193 : tree arg1 = gimple_phi_arg_def (phi, 1);
1938 : 27193 : tree operand;
1939 : :
1940 : 27193 : if (is_gimple_ip_invariant (arg1))
1941 : : {
1942 : : operand = arg1;
1943 : : rhs1 = arg0;
1944 : : }
1945 : 23394 : else if (is_gimple_ip_invariant (arg0))
1946 : : {
1947 : : operand = arg0;
1948 : : rhs1 = arg1;
1949 : : }
1950 : : else
1951 : : return;
1952 : :
1953 : 7894 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
1954 : 7894 : if (!is_gimple_assign (stmt))
1955 : : return;
1956 : :
1957 : 3107 : code = ASSERT_EXPR;
1958 : 3107 : agg_value->pass_through.operand = operand;
1959 : : }
1960 : 1357206 : else if (is_gimple_assign (stmt))
1961 : : {
1962 : 1298181 : code = gimple_assign_rhs_code (stmt);
1963 : 1298181 : switch (gimple_assign_rhs_class (stmt))
1964 : : {
1965 : 1234141 : case GIMPLE_SINGLE_RHS:
1966 : 1234141 : if (is_gimple_ip_invariant (rhs1))
1967 : : {
1968 : 899591 : agg_value->pass_through.operand = rhs1;
1969 : 899591 : return;
1970 : : }
1971 : : code = NOP_EXPR;
1972 : : break;
1973 : :
1974 : 20504 : case GIMPLE_UNARY_RHS:
1975 : : /* NOTE: A GIMPLE_UNARY_RHS operation might not be tcc_unary
1976 : : (truth_not_expr is example), GIMPLE_BINARY_RHS does not imply
1977 : : tcc_binary, this subtleness is somewhat misleading.
1978 : :
1979 : : Since tcc_unary is widely used in IPA-CP code to check an operation
1980 : : with one operand, here we only allow tc_unary operation to avoid
1981 : : possible problem. Then we can use (opclass == tc_unary) or not to
1982 : : distinguish unary and binary. */
1983 : 20504 : if (TREE_CODE_CLASS (code) != tcc_unary || CONVERT_EXPR_CODE_P (code))
1984 : : return;
1985 : :
1986 : 1575 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
1987 : 1575 : break;
1988 : :
1989 : 42968 : case GIMPLE_BINARY_RHS:
1990 : 42968 : {
1991 : 42968 : gimple *rhs1_stmt = stmt;
1992 : 42968 : gimple *rhs2_stmt = stmt;
1993 : 42968 : tree rhs2 = gimple_assign_rhs2 (stmt);
1994 : :
1995 : 42968 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &rhs1_stmt);
1996 : 42968 : rhs2 = get_ssa_def_if_simple_copy (rhs2, &rhs2_stmt);
1997 : :
1998 : 42968 : if (is_gimple_ip_invariant (rhs2))
1999 : : {
2000 : 20789 : agg_value->pass_through.operand = rhs2;
2001 : 20789 : stmt = rhs1_stmt;
2002 : : }
2003 : 22179 : else if (is_gimple_ip_invariant (rhs1))
2004 : : {
2005 : 2687 : if (TREE_CODE_CLASS (code) == tcc_comparison)
2006 : 0 : code = swap_tree_comparison (code);
2007 : 2687 : else if (!commutative_tree_code (code))
2008 : 22179 : return;
2009 : :
2010 : 0 : agg_value->pass_through.operand = rhs1;
2011 : 0 : stmt = rhs2_stmt;
2012 : 0 : rhs1 = rhs2;
2013 : : }
2014 : : else
2015 : : return;
2016 : :
2017 : 20789 : if (TREE_CODE_CLASS (code) != tcc_comparison
2018 : 40989 : && !useless_type_conversion_p (TREE_TYPE (lhs),
2019 : 20200 : TREE_TYPE (rhs1)))
2020 : : return;
2021 : : }
2022 : 20789 : break;
2023 : :
2024 : : default:
2025 : : return;
2026 : : }
2027 : : }
2028 : : else
2029 : : return;
2030 : :
2031 : 360021 : if (TREE_CODE (rhs1) != SSA_NAME)
2032 : 320528 : index = load_from_unmodified_param_or_agg (fbi, fbi->info, stmt,
2033 : : &agg_value->offset,
2034 : : &agg_value->by_ref);
2035 : 39493 : else if (SSA_NAME_IS_DEFAULT_DEF (rhs1))
2036 : 29806 : index = ipa_get_param_decl_index (fbi->info, SSA_NAME_VAR (rhs1));
2037 : :
2038 : 350334 : if (index >= 0)
2039 : : {
2040 : 71422 : if (agg_value->offset >= 0)
2041 : 43141 : agg_value->type = TREE_TYPE (rhs1);
2042 : 71422 : agg_value->pass_through.formal_id = index;
2043 : 71422 : agg_value->pass_through.operation = code;
2044 : : }
2045 : : else
2046 : 288599 : agg_value->pass_through.operand = NULL_TREE;
2047 : : }
2048 : :
2049 : : /* If STMT is a memory store to the object whose address is BASE, extract
2050 : : information (offset, size, and value) into CONTENT, and return true,
2051 : : otherwise we conservatively assume the whole object is modified with
2052 : : unknown content, and return false. CHECK_REF means that access to object
2053 : : is expected to be in form of MEM_REF expression. */
2054 : :
2055 : : static bool
2056 : 2618524 : extract_mem_content (struct ipa_func_body_info *fbi,
2057 : : gimple *stmt, tree base, bool check_ref,
2058 : : struct ipa_known_agg_contents_list *content)
2059 : : {
2060 : 2618524 : HOST_WIDE_INT lhs_offset, lhs_size;
2061 : 2618524 : bool reverse;
2062 : :
2063 : 2618524 : if (!is_gimple_assign (stmt))
2064 : : return false;
2065 : :
2066 : 1716887 : tree lhs = gimple_assign_lhs (stmt);
2067 : 1716887 : tree lhs_base = get_ref_base_and_extent_hwi (lhs, &lhs_offset, &lhs_size,
2068 : : &reverse);
2069 : 1716887 : if (!lhs_base)
2070 : : return false;
2071 : :
2072 : 1715277 : if (check_ref)
2073 : : {
2074 : 149232 : if (TREE_CODE (lhs_base) != MEM_REF
2075 : 124007 : || TREE_OPERAND (lhs_base, 0) != base
2076 : 192888 : || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
2077 : 110421 : return false;
2078 : : }
2079 : 1566045 : else if (lhs_base != base)
2080 : : return false;
2081 : :
2082 : 1601583 : content->offset = lhs_offset;
2083 : 1601583 : content->size = lhs_size;
2084 : 1601583 : content->type = TREE_TYPE (lhs);
2085 : 1601583 : content->next = NULL;
2086 : :
2087 : 1601583 : analyze_agg_content_value (fbi, &content->value, stmt);
2088 : 1601583 : return true;
2089 : : }
2090 : :
2091 : : /* Traverse statements from CALL backwards, scanning whether an aggregate given
2092 : : in ARG is filled in constants or values that are derived from caller's
2093 : : formal parameter in the way described by some kinds of jump functions. FBI
2094 : : is the context of the caller function for interprocedural analysis. ARG can
2095 : : either be an aggregate expression or a pointer to an aggregate. ARG_TYPE is
2096 : : the type of the aggregate, JFUNC is the jump function for the aggregate. */
2097 : :
2098 : : static void
2099 : 3128752 : determine_known_aggregate_parts (struct ipa_func_body_info *fbi,
2100 : : gcall *call, tree arg,
2101 : : tree arg_type,
2102 : : struct ipa_jump_func *jfunc)
2103 : : {
2104 : 3128752 : struct ipa_known_agg_contents_list *list = NULL, *all_list = NULL;
2105 : 3128752 : bitmap visited = NULL;
2106 : 3128752 : int item_count = 0, value_count = 0;
2107 : 3128752 : HOST_WIDE_INT arg_offset, arg_size;
2108 : 3128752 : tree arg_base;
2109 : 3128752 : bool check_ref, by_ref;
2110 : 3128752 : ao_ref r;
2111 : 3128752 : int max_agg_items = opt_for_fn (fbi->node->decl, param_ipa_max_agg_items);
2112 : :
2113 : 3128752 : if (max_agg_items == 0)
2114 : 794111 : return;
2115 : :
2116 : : /* The function operates in three stages. First, we prepare check_ref, r,
2117 : : arg_base and arg_offset based on what is actually passed as an actual
2118 : : argument. */
2119 : :
2120 : 3128752 : if (POINTER_TYPE_P (arg_type))
2121 : : {
2122 : 2797373 : by_ref = true;
2123 : 2797373 : if (TREE_CODE (arg) == SSA_NAME)
2124 : : {
2125 : 1049068 : tree type_size;
2126 : 1049068 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type)))
2127 : 1049068 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
2128 : : return;
2129 : 747295 : check_ref = true;
2130 : 747295 : arg_base = arg;
2131 : 747295 : arg_offset = 0;
2132 : 747295 : type_size = TYPE_SIZE (TREE_TYPE (arg_type));
2133 : 747295 : arg_size = tree_to_uhwi (type_size);
2134 : 747295 : ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
2135 : : }
2136 : 1748305 : else if (TREE_CODE (arg) == ADDR_EXPR)
2137 : : {
2138 : 1690758 : bool reverse;
2139 : :
2140 : 1690758 : arg = TREE_OPERAND (arg, 0);
2141 : 1690758 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2142 : : &arg_size, &reverse);
2143 : 1690758 : if (!arg_base)
2144 : 434719 : return;
2145 : 1689544 : if (DECL_P (arg_base))
2146 : : {
2147 : 1256039 : check_ref = false;
2148 : 1256039 : ao_ref_init (&r, arg_base);
2149 : : }
2150 : : else
2151 : : return;
2152 : : }
2153 : : else
2154 : : return;
2155 : : }
2156 : : else
2157 : : {
2158 : 331379 : bool reverse;
2159 : :
2160 : 331379 : gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
2161 : :
2162 : 331379 : by_ref = false;
2163 : 331379 : check_ref = false;
2164 : 331379 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2165 : : &arg_size, &reverse);
2166 : 331379 : if (!arg_base)
2167 : 72 : return;
2168 : :
2169 : 331307 : ao_ref_init (&r, arg);
2170 : : }
2171 : :
2172 : : /* Second stage traverses virtual SSA web backwards starting from the call
2173 : : statement, only looks at individual dominating virtual operand (its
2174 : : definition dominates the call), as long as it is confident that content
2175 : : of the aggregate is affected by definition of the virtual operand, it
2176 : : builds a sorted linked list of ipa_agg_jf_list describing that. */
2177 : :
2178 : 2334641 : for (tree dom_vuse = gimple_vuse (call);
2179 : 21962517 : dom_vuse && fbi->aa_walk_budget > 0;)
2180 : : {
2181 : 21499083 : gimple *stmt = SSA_NAME_DEF_STMT (dom_vuse);
2182 : :
2183 : 21499083 : if (gimple_code (stmt) == GIMPLE_PHI)
2184 : : {
2185 : 2192714 : dom_vuse = get_continuation_for_phi (stmt, &r, true,
2186 : 1096357 : fbi->aa_walk_budget,
2187 : : &visited, false, NULL, NULL);
2188 : 1096357 : continue;
2189 : : }
2190 : :
2191 : 20402726 : fbi->aa_walk_budget--;
2192 : 20402726 : if (stmt_may_clobber_ref_p_1 (stmt, &r))
2193 : : {
2194 : 2618524 : struct ipa_known_agg_contents_list *content
2195 : 2618524 : = XALLOCA (struct ipa_known_agg_contents_list);
2196 : :
2197 : 2618524 : if (!extract_mem_content (fbi, stmt, arg_base, check_ref, content))
2198 : : break;
2199 : :
2200 : : /* Now we get a dominating virtual operand, and need to check
2201 : : whether its value is clobbered any other dominating one. */
2202 : 1601583 : if ((content->value.pass_through.formal_id >= 0
2203 : 1530161 : || content->value.pass_through.operand)
2204 : 971013 : && !clobber_by_agg_contents_list_p (all_list, content)
2205 : : /* Since IPA-CP stores results with unsigned int offsets, we can
2206 : : discard those which would not fit now before we stream them to
2207 : : WPA. */
2208 : 2507669 : && (content->offset + content->size - arg_offset
2209 : : <= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT))
2210 : : {
2211 : 905948 : struct ipa_known_agg_contents_list *copy
2212 : 905948 : = XALLOCA (struct ipa_known_agg_contents_list);
2213 : :
2214 : : /* Add to the list consisting of only dominating virtual
2215 : : operands, whose definitions can finally reach the call. */
2216 : 905948 : add_to_agg_contents_list (&list, (*copy = *content, copy));
2217 : :
2218 : 905948 : if (++value_count == max_agg_items)
2219 : : break;
2220 : : }
2221 : :
2222 : : /* Add to the list consisting of all dominating virtual operands. */
2223 : 1599711 : add_to_agg_contents_list (&all_list, content);
2224 : :
2225 : 1599711 : if (++item_count == 2 * max_agg_items)
2226 : : break;
2227 : : }
2228 : 37914091 : dom_vuse = gimple_vuse (stmt);
2229 : : }
2230 : :
2231 : 2334641 : if (visited)
2232 : 577210 : BITMAP_FREE (visited);
2233 : :
2234 : : /* Third stage just goes over the list and creates an appropriate vector of
2235 : : ipa_agg_jf_item structures out of it, of course only if there are
2236 : : any meaningful items to begin with. */
2237 : :
2238 : 2334641 : if (value_count)
2239 : : {
2240 : 301667 : jfunc->agg.by_ref = by_ref;
2241 : 301667 : build_agg_jump_func_from_list (list, value_count, arg_offset, jfunc);
2242 : : }
2243 : : }
2244 : :
2245 : :
2246 : : /* Return the Ith param type of callee associated with call graph
2247 : : edge E. */
2248 : :
2249 : : tree
2250 : 5860181 : ipa_get_callee_param_type (struct cgraph_edge *e, int i)
2251 : : {
2252 : 5860181 : int n;
2253 : 5860181 : tree type = (e->callee
2254 : 5860181 : ? TREE_TYPE (e->callee->decl)
2255 : 271602 : : gimple_call_fntype (e->call_stmt));
2256 : 5860181 : tree t = TYPE_ARG_TYPES (type);
2257 : :
2258 : 11961728 : for (n = 0; n < i; n++)
2259 : : {
2260 : 6353401 : if (!t)
2261 : : break;
2262 : 6101547 : t = TREE_CHAIN (t);
2263 : : }
2264 : 5860181 : if (t && t != void_list_node)
2265 : 5517415 : return TREE_VALUE (t);
2266 : 342766 : if (!e->callee)
2267 : : return NULL;
2268 : 322055 : t = DECL_ARGUMENTS (e->callee->decl);
2269 : 848568 : for (n = 0; n < i; n++)
2270 : : {
2271 : 804093 : if (!t)
2272 : : return NULL;
2273 : 526513 : t = TREE_CHAIN (t);
2274 : : }
2275 : 44475 : if (t)
2276 : 2185 : return TREE_TYPE (t);
2277 : : return NULL;
2278 : : }
2279 : :
2280 : : /* Return a pointer to an ipa_vr just like TMP, but either find it in
2281 : : ipa_vr_hash_table or allocate it in GC memory. */
2282 : :
2283 : : static ipa_vr *
2284 : 5272170 : ipa_get_value_range (const vrange &tmp)
2285 : : {
2286 : 5272170 : inchash::hash hstate;
2287 : 5272170 : inchash::add_vrange (tmp, hstate);
2288 : 5272170 : hashval_t hash = hstate.end ();
2289 : 5272170 : ipa_vr **slot = ipa_vr_hash_table->find_slot_with_hash (&tmp, hash, INSERT);
2290 : 5272170 : if (*slot)
2291 : : return *slot;
2292 : :
2293 : 582441 : ipa_vr *vr = new (ggc_alloc<ipa_vr> ()) ipa_vr (tmp);
2294 : 582441 : *slot = vr;
2295 : 582441 : return vr;
2296 : : }
2297 : :
2298 : : /* Assign to JF a pointer to a range just like TMP but either fetch a
2299 : : copy from ipa_vr_hash_table or allocate a new on in GC memory. */
2300 : :
2301 : : static void
2302 : 4593744 : ipa_set_jfunc_vr (ipa_jump_func *jf, const vrange &tmp)
2303 : : {
2304 : 1731578 : jf->m_vr = ipa_get_value_range (tmp);
2305 : 2862166 : }
2306 : :
2307 : : static void
2308 : 573891 : ipa_set_jfunc_vr (ipa_jump_func *jf, const ipa_vr &vr)
2309 : : {
2310 : 573891 : value_range tmp;
2311 : 573891 : vr.get_vrange (tmp);
2312 : 573891 : ipa_set_jfunc_vr (jf, tmp);
2313 : 573891 : }
2314 : :
2315 : : /* Given VAL that conforms to is_gimple_ip_invariant, produce a VRANGE that
2316 : : represents it as a range. CONTEXT_NODE is the call graph node representing
2317 : : the function for which optimization flags should be evaluated. */
2318 : :
2319 : : void
2320 : 1099686 : ipa_get_range_from_ip_invariant (vrange &r, tree val, cgraph_node *context_node)
2321 : : {
2322 : 1099686 : if (TREE_CODE (val) == ADDR_EXPR)
2323 : : {
2324 : 766 : symtab_node *symbol;
2325 : 766 : tree base = TREE_OPERAND (val, 0);
2326 : 766 : if (!DECL_P (base))
2327 : : {
2328 : 150 : r.set_varying (TREE_TYPE (val));
2329 : 150 : return;
2330 : : }
2331 : 616 : if (!decl_in_symtab_p (base))
2332 : : {
2333 : 0 : r.set_nonzero (TREE_TYPE (val));
2334 : 0 : return;
2335 : : }
2336 : 616 : if (!(symbol = symtab_node::get (base)))
2337 : : {
2338 : 0 : r.set_varying (TREE_TYPE (val));
2339 : 0 : return;
2340 : : }
2341 : :
2342 : 616 : bool delete_null_pointer_checks
2343 : 616 : = opt_for_fn (context_node->decl, flag_delete_null_pointer_checks);
2344 : 616 : if (symbol->nonzero_address (delete_null_pointer_checks))
2345 : 616 : r.set_nonzero (TREE_TYPE (val));
2346 : : else
2347 : 0 : r.set_varying (TREE_TYPE (val));
2348 : : }
2349 : : else
2350 : 1098920 : r.set (val, val);
2351 : : }
2352 : :
2353 : : /* If T is an SSA_NAME that is the result of a simple type conversion statement
2354 : : from an integer type to another integer type which is known to be able to
2355 : : represent the values the operand of the conversion can hold, return the
2356 : : operand of that conversion, otherwise return T. */
2357 : :
2358 : : static tree
2359 : 5860181 : skip_a_safe_conversion_op (tree t)
2360 : : {
2361 : 5860181 : if (TREE_CODE (t) != SSA_NAME
2362 : 5860181 : || SSA_NAME_IS_DEFAULT_DEF (t))
2363 : : return t;
2364 : :
2365 : 1444366 : gimple *def = SSA_NAME_DEF_STMT (t);
2366 : 1444366 : if (!is_gimple_assign (def)
2367 : 1204661 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def))
2368 : 267621 : || !INTEGRAL_TYPE_P (TREE_TYPE (t))
2369 : 1646845 : || !INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def))))
2370 : : return t;
2371 : :
2372 : 194549 : tree rhs1 = gimple_assign_rhs1 (def);
2373 : 194549 : if (TYPE_PRECISION (TREE_TYPE (t))
2374 : 194549 : >= TYPE_PRECISION (TREE_TYPE (rhs1)))
2375 : : return gimple_assign_rhs1 (def);
2376 : :
2377 : 5482 : value_range vr (TREE_TYPE (rhs1));
2378 : 10964 : if (!get_range_query (cfun)->range_of_expr (vr, rhs1, def)
2379 : 5482 : || vr.undefined_p ())
2380 : : return t;
2381 : :
2382 : 5474 : irange &ir = as_a <irange> (vr);
2383 : 5474 : if (range_fits_type_p (&ir, TYPE_PRECISION (TREE_TYPE (t)),
2384 : 5474 : TYPE_SIGN (TREE_TYPE (t))))
2385 : 3275 : return gimple_assign_rhs1 (def);
2386 : :
2387 : : return t;
2388 : 5482 : }
2389 : :
2390 : : /* Compute jump function for all arguments of callsite CS and insert the
2391 : : information in the jump_functions array in the ipa_edge_args corresponding
2392 : : to this callsite. */
2393 : :
2394 : : static void
2395 : 2781430 : ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi,
2396 : : struct cgraph_edge *cs)
2397 : : {
2398 : 2781430 : ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
2399 : 2781430 : ipa_edge_args *args = ipa_edge_args_sum->get_create (cs);
2400 : 2781430 : gcall *call = cs->call_stmt;
2401 : 2781430 : int n, arg_num = gimple_call_num_args (call);
2402 : 2781430 : bool useful_context = false;
2403 : :
2404 : 2781430 : if (arg_num == 0 || args->jump_functions)
2405 : : return;
2406 : 2513634 : vec_safe_grow_cleared (args->jump_functions, arg_num, true);
2407 : 2513634 : if (flag_devirtualize)
2408 : 2338675 : vec_safe_grow_cleared (args->polymorphic_call_contexts, arg_num, true);
2409 : :
2410 : 2513634 : if (gimple_call_internal_p (call))
2411 : : return;
2412 : 2513634 : if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
2413 : : return;
2414 : :
2415 : 8373815 : for (n = 0; n < arg_num; n++)
2416 : : {
2417 : 5860181 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
2418 : 5860181 : tree arg = gimple_call_arg (call, n);
2419 : 5860181 : tree param_type = ipa_get_callee_param_type (cs, n);
2420 : 5860181 : if (flag_devirtualize && POINTER_TYPE_P (TREE_TYPE (arg)))
2421 : : {
2422 : 2966082 : tree instance;
2423 : 2966082 : class ipa_polymorphic_call_context context (cs->caller->decl,
2424 : 2966082 : arg, cs->call_stmt,
2425 : 2966082 : &instance);
2426 : 2966082 : context.get_dynamic_type (instance, arg, NULL, cs->call_stmt,
2427 : : &fbi->aa_walk_budget);
2428 : 2966082 : *ipa_get_ith_polymorhic_call_context (args, n) = context;
2429 : 5932164 : if (!context.useless_p ())
2430 : : useful_context = true;
2431 : : }
2432 : :
2433 : 5860181 : value_range vr (TREE_TYPE (arg));
2434 : 5860181 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2435 : : {
2436 : 6443212 : if (!get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2437 : 3221606 : || vr.varying_p ()
2438 : 5582777 : || vr.undefined_p ())
2439 : : {
2440 : 860762 : bool strict_overflow = false;
2441 : 860762 : if (tree_single_nonzero_warnv_p (arg, &strict_overflow))
2442 : 0 : vr.set_nonzero (TREE_TYPE (arg));
2443 : : else
2444 : 860762 : vr.set_varying (TREE_TYPE (arg));
2445 : : }
2446 : 3221606 : gcc_assert (!vr.undefined_p ());
2447 : 3221606 : unsigned HOST_WIDE_INT bitpos;
2448 : 3221606 : unsigned align = BITS_PER_UNIT;
2449 : :
2450 : 3221606 : if (!vr.singleton_p ())
2451 : 3164493 : get_pointer_alignment_1 (arg, &align, &bitpos);
2452 : :
2453 : 3221606 : if (align > BITS_PER_UNIT
2454 : 3221606 : && opt_for_fn (cs->caller->decl, flag_ipa_bit_cp))
2455 : : {
2456 : 1157687 : unsigned prec = TYPE_PRECISION (TREE_TYPE (arg));
2457 : 1157687 : wide_int mask
2458 : 2315374 : = wi::bit_and_not (wi::mask (prec, false, prec),
2459 : 1157687 : wide_int::from (align / BITS_PER_UNIT - 1,
2460 : 1157687 : prec, UNSIGNED));
2461 : 1157687 : wide_int value = wide_int::from (bitpos / BITS_PER_UNIT, prec,
2462 : 1157687 : UNSIGNED);
2463 : 1157687 : irange_bitmask bm (value, mask);
2464 : 1157687 : vr.update_bitmask (bm);
2465 : 1157687 : ipa_set_jfunc_vr (jfunc, vr);
2466 : 1157687 : }
2467 : 2063919 : else if (!vr.varying_p ())
2468 : 1263741 : ipa_set_jfunc_vr (jfunc, vr);
2469 : : else
2470 : 800178 : gcc_assert (!jfunc->m_vr);
2471 : : }
2472 : : else
2473 : : {
2474 : 2638575 : if (param_type
2475 : 2330063 : && ipa_vr_supported_type_p (TREE_TYPE (arg))
2476 : 2638705 : && ipa_vr_supported_type_p (param_type)
2477 : 3461886 : && get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2478 : 4369518 : && !vr.undefined_p ())
2479 : : {
2480 : 1730813 : value_range resvr (vr);
2481 : 1730813 : range_cast (resvr, param_type);
2482 : 1730813 : if (!resvr.undefined_p () && !resvr.varying_p ())
2483 : 1366217 : ipa_set_jfunc_vr (jfunc, resvr);
2484 : : else
2485 : 364596 : gcc_assert (!jfunc->m_vr);
2486 : 1730813 : }
2487 : : else
2488 : 907762 : gcc_assert (!jfunc->m_vr);
2489 : : }
2490 : :
2491 : 5860181 : arg = skip_a_safe_conversion_op (arg);
2492 : 5860181 : if (is_gimple_ip_invariant (arg)
2493 : 5860181 : || (VAR_P (arg)
2494 : 287349 : && is_global_var (arg)
2495 : 9037 : && TREE_READONLY (arg)))
2496 : 2206324 : ipa_set_jf_constant (jfunc, arg, cs);
2497 : 3653857 : else if (!is_gimple_reg_type (TREE_TYPE (arg))
2498 : 3653857 : && TREE_CODE (arg) == PARM_DECL)
2499 : : {
2500 : 80619 : int index = ipa_get_param_decl_index (info, arg);
2501 : :
2502 : 80619 : gcc_assert (index >=0);
2503 : : /* Aggregate passed by value, check for pass-through, otherwise we
2504 : : will attempt to fill in aggregate contents later in this
2505 : : for cycle. */
2506 : 80619 : if (parm_preserved_before_stmt_p (fbi, index, call, arg))
2507 : : {
2508 : 66818 : ipa_set_jf_simple_pass_through (jfunc, index, false);
2509 : 66818 : continue;
2510 : : }
2511 : : }
2512 : 3573238 : else if (TREE_CODE (arg) == SSA_NAME)
2513 : : {
2514 : 2388891 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
2515 : : {
2516 : 964674 : int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
2517 : 964674 : if (index >= 0)
2518 : : {
2519 : 956505 : bool agg_p;
2520 : 956505 : agg_p = parm_ref_data_pass_through_p (fbi, index, call, arg);
2521 : 956505 : ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
2522 : : }
2523 : : }
2524 : : else
2525 : : {
2526 : 1424217 : gimple *stmt = SSA_NAME_DEF_STMT (arg);
2527 : 1424217 : if (is_gimple_assign (stmt))
2528 : 1170288 : compute_complex_assign_jump_func (fbi, info, jfunc,
2529 : : call, stmt, arg, param_type);
2530 : 253929 : else if (gimple_code (stmt) == GIMPLE_PHI)
2531 : 85060 : compute_complex_ancestor_jump_func (fbi, info, jfunc,
2532 : : call,
2533 : : as_a <gphi *> (stmt));
2534 : : }
2535 : : }
2536 : :
2537 : : /* If ARG is pointer, we cannot use its type to determine the type of aggregate
2538 : : passed (because type conversions are ignored in gimple). Usually we can
2539 : : safely get type from function declaration, but in case of K&R prototypes or
2540 : : variadic functions we can try our luck with type of the pointer passed.
2541 : : TODO: Since we look for actual initialization of the memory object, we may better
2542 : : work out the type based on the memory stores we find. */
2543 : 5793363 : if (!param_type)
2544 : 340579 : param_type = TREE_TYPE (arg);
2545 : :
2546 : 5793363 : if ((jfunc->type != IPA_JF_PASS_THROUGH
2547 : 997033 : || !ipa_get_jf_pass_through_agg_preserved (jfunc))
2548 : 5433675 : && (jfunc->type != IPA_JF_ANCESTOR
2549 : 146584 : || !ipa_get_jf_ancestor_agg_preserved (jfunc))
2550 : 11180895 : && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
2551 : 5056089 : || POINTER_TYPE_P (param_type)))
2552 : 3128752 : determine_known_aggregate_parts (fbi, call, arg, param_type, jfunc);
2553 : 5860181 : }
2554 : 2513634 : if (!useful_context)
2555 : 4384429 : vec_free (args->polymorphic_call_contexts);
2556 : : }
2557 : :
2558 : : /* Compute jump functions for all edges - both direct and indirect - outgoing
2559 : : from BB. */
2560 : :
2561 : : static void
2562 : 10636623 : ipa_compute_jump_functions_for_bb (struct ipa_func_body_info *fbi, basic_block bb)
2563 : : {
2564 : 10636623 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
2565 : 10636623 : int i;
2566 : 10636623 : struct cgraph_edge *cs;
2567 : :
2568 : 19804579 : FOR_EACH_VEC_ELT_REVERSE (bi->cg_edges, i, cs)
2569 : : {
2570 : 5287318 : struct cgraph_node *callee = cs->callee;
2571 : :
2572 : 5287318 : if (callee)
2573 : : {
2574 : 5149844 : callee = callee->ultimate_alias_target ();
2575 : : /* We do not need to bother analyzing calls to unknown functions
2576 : : unless they may become known during lto/whopr. */
2577 : 3419234 : if (!callee->definition && !flag_lto
2578 : 8569078 : && !gimple_call_fnspec (cs->call_stmt).known_p ())
2579 : 2505888 : continue;
2580 : : }
2581 : 2781430 : ipa_compute_jump_functions_for_edge (fbi, cs);
2582 : : }
2583 : 10636623 : }
2584 : :
2585 : : /* If STMT looks like a statement loading a value from a member pointer formal
2586 : : parameter, return that parameter and store the offset of the field to
2587 : : *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
2588 : : might be clobbered). If USE_DELTA, then we look for a use of the delta
2589 : : field rather than the pfn. */
2590 : :
2591 : : static tree
2592 : 2130 : ipa_get_stmt_member_ptr_load_param (gimple *stmt, bool use_delta,
2593 : : HOST_WIDE_INT *offset_p)
2594 : : {
2595 : 2130 : tree rhs, fld, ptr_field, delta_field;
2596 : 2130 : tree ref_field = NULL_TREE;
2597 : 2130 : tree ref_offset = NULL_TREE;
2598 : :
2599 : 2130 : if (!gimple_assign_single_p (stmt))
2600 : : return NULL_TREE;
2601 : :
2602 : 2130 : rhs = gimple_assign_rhs1 (stmt);
2603 : 2130 : if (TREE_CODE (rhs) == COMPONENT_REF)
2604 : : {
2605 : 1277 : ref_field = TREE_OPERAND (rhs, 1);
2606 : 1277 : rhs = TREE_OPERAND (rhs, 0);
2607 : : }
2608 : :
2609 : 2130 : if (TREE_CODE (rhs) == MEM_REF)
2610 : : {
2611 : 1427 : ref_offset = TREE_OPERAND (rhs, 1);
2612 : 1427 : if (ref_field && integer_nonzerop (ref_offset))
2613 : : return NULL_TREE;
2614 : : }
2615 : 703 : else if (!ref_field)
2616 : : return NULL_TREE;
2617 : :
2618 : 2130 : if (TREE_CODE (rhs) == MEM_REF
2619 : 1427 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
2620 : 3557 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (rhs, 0)))
2621 : : {
2622 : 572 : rhs = TREE_OPERAND (rhs, 0);
2623 : 572 : if (TREE_CODE (SSA_NAME_VAR (rhs)) != PARM_DECL
2624 : 572 : || !type_like_member_ptr_p (TREE_TYPE (TREE_TYPE (rhs)), &ptr_field,
2625 : : &delta_field))
2626 : 0 : return NULL_TREE;
2627 : : }
2628 : : else
2629 : : {
2630 : 1558 : if (TREE_CODE (rhs) == MEM_REF
2631 : 1558 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR)
2632 : 0 : rhs = TREE_OPERAND (TREE_OPERAND (rhs, 0), 0);
2633 : 1558 : if (TREE_CODE (rhs) != PARM_DECL
2634 : 1558 : || !type_like_member_ptr_p (TREE_TYPE (rhs), &ptr_field,
2635 : : &delta_field))
2636 : 1282 : return NULL_TREE;
2637 : : }
2638 : :
2639 : 848 : if (use_delta)
2640 : 0 : fld = delta_field;
2641 : : else
2642 : 848 : fld = ptr_field;
2643 : :
2644 : 848 : if (ref_field)
2645 : : {
2646 : 848 : if (ref_field != fld)
2647 : : return NULL_TREE;
2648 : : }
2649 : 0 : else if (!tree_int_cst_equal (byte_position (fld), ref_offset))
2650 : : return NULL_TREE;
2651 : :
2652 : 848 : if (offset_p)
2653 : 424 : *offset_p = int_bit_position (fld);
2654 : : return rhs;
2655 : : }
2656 : :
2657 : : /* Returns true iff T is an SSA_NAME defined by a statement. */
2658 : :
2659 : : static bool
2660 : 2978 : ipa_is_ssa_with_stmt_def (tree t)
2661 : : {
2662 : 2978 : if (TREE_CODE (t) == SSA_NAME
2663 : 2978 : && !SSA_NAME_IS_DEFAULT_DEF (t))
2664 : : return true;
2665 : : else
2666 : 0 : return false;
2667 : : }
2668 : :
2669 : : /* Find the indirect call graph edge corresponding to STMT and mark it as a
2670 : : call to a parameter number PARAM_INDEX. NODE is the caller. Return the
2671 : : indirect call graph edge.
2672 : : If POLYMORPHIC is true record is as a destination of polymorphic call. */
2673 : :
2674 : : static struct cgraph_edge *
2675 : 15827 : ipa_note_param_call (struct cgraph_node *node, int param_index,
2676 : : gcall *stmt, bool polymorphic)
2677 : : {
2678 : 15827 : struct cgraph_edge *cs;
2679 : :
2680 : 15827 : cs = node->get_edge (stmt);
2681 : 15827 : cs->indirect_info->param_index = param_index;
2682 : 15827 : cs->indirect_info->agg_contents = 0;
2683 : 15827 : cs->indirect_info->member_ptr = 0;
2684 : 15827 : cs->indirect_info->guaranteed_unmodified = 0;
2685 : 15827 : ipa_node_params *info = ipa_node_params_sum->get (node);
2686 : 15827 : ipa_set_param_used_by_indirect_call (info, param_index, true);
2687 : 15827 : if (cs->indirect_info->polymorphic || polymorphic)
2688 : 9633 : ipa_set_param_used_by_polymorphic_call (info, param_index, true);
2689 : 15827 : return cs;
2690 : : }
2691 : :
2692 : : /* Analyze the CALL and examine uses of formal parameters of the caller NODE
2693 : : (described by INFO). PARMS_AINFO is a pointer to a vector containing
2694 : : intermediate information about each formal parameter. Currently it checks
2695 : : whether the call calls a pointer that is a formal parameter and if so, the
2696 : : parameter is marked with the called flag and an indirect call graph edge
2697 : : describing the call is created. This is very simple for ordinary pointers
2698 : : represented in SSA but not-so-nice when it comes to member pointers. The
2699 : : ugly part of this function does nothing more than trying to match the
2700 : : pattern of such a call. Look up the documentation of macro
2701 : : TARGET_PTRMEMFUNC_VBIT_LOCATION for details. An example of such a pattern
2702 : : is the gimple dump below, the call is on the last line:
2703 : :
2704 : : <bb 2>:
2705 : : f$__delta_5 = f.__delta;
2706 : : f$__pfn_24 = f.__pfn;
2707 : :
2708 : : or
2709 : : <bb 2>:
2710 : : f$__delta_5 = MEM[(struct *)&f];
2711 : : f$__pfn_24 = MEM[(struct *)&f + 4B];
2712 : :
2713 : : and a few lines below:
2714 : :
2715 : : <bb 5>
2716 : : D.2496_3 = (int) f$__pfn_24;
2717 : : D.2497_4 = D.2496_3 & 1;
2718 : : if (D.2497_4 != 0)
2719 : : goto <bb 3>;
2720 : : else
2721 : : goto <bb 4>;
2722 : :
2723 : : <bb 6>:
2724 : : D.2500_7 = (unsigned int) f$__delta_5;
2725 : : D.2501_8 = &S + D.2500_7;
2726 : : D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
2727 : : D.2503_10 = *D.2502_9;
2728 : : D.2504_12 = f$__pfn_24 + -1;
2729 : : D.2505_13 = (unsigned int) D.2504_12;
2730 : : D.2506_14 = D.2503_10 + D.2505_13;
2731 : : D.2507_15 = *D.2506_14;
2732 : : iftmp.11_16 = (String:: *) D.2507_15;
2733 : :
2734 : : <bb 7>:
2735 : : # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
2736 : : D.2500_19 = (unsigned int) f$__delta_5;
2737 : : D.2508_20 = &S + D.2500_19;
2738 : : D.2493_21 = iftmp.11_1 (D.2508_20, 4);
2739 : :
2740 : : Such patterns are results of simple calls to a member pointer:
2741 : :
2742 : : int doprinting (int (MyString::* f)(int) const)
2743 : : {
2744 : : MyString S ("somestring");
2745 : :
2746 : : return (S.*f)(4);
2747 : : }
2748 : :
2749 : : Moreover, the function also looks for called pointers loaded from aggregates
2750 : : passed by value or reference. */
2751 : :
2752 : : static void
2753 : 112289 : ipa_analyze_indirect_call_uses (struct ipa_func_body_info *fbi, gcall *call,
2754 : : tree target)
2755 : : {
2756 : 112289 : class ipa_node_params *info = fbi->info;
2757 : 112289 : HOST_WIDE_INT offset;
2758 : 112289 : bool by_ref;
2759 : :
2760 : 112289 : if (SSA_NAME_IS_DEFAULT_DEF (target))
2761 : : {
2762 : 3525 : tree var = SSA_NAME_VAR (target);
2763 : 3525 : int index = ipa_get_param_decl_index (info, var);
2764 : 3525 : if (index >= 0)
2765 : 3523 : ipa_note_param_call (fbi->node, index, call, false);
2766 : 3525 : return;
2767 : : }
2768 : :
2769 : 108764 : int index;
2770 : 108764 : gimple *def = SSA_NAME_DEF_STMT (target);
2771 : 108764 : bool guaranteed_unmodified;
2772 : 108764 : if (gimple_assign_single_p (def)
2773 : 108764 : && ipa_load_from_parm_agg (fbi, info->descriptors, def,
2774 : : gimple_assign_rhs1 (def), &index, &offset,
2775 : : NULL, &by_ref, &guaranteed_unmodified))
2776 : : {
2777 : 2255 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
2778 : : call, false);
2779 : 2255 : cs->indirect_info->offset = offset;
2780 : 2255 : cs->indirect_info->agg_contents = 1;
2781 : 2255 : cs->indirect_info->by_ref = by_ref;
2782 : 2255 : cs->indirect_info->guaranteed_unmodified = guaranteed_unmodified;
2783 : 2255 : return;
2784 : : }
2785 : :
2786 : : /* Now we need to try to match the complex pattern of calling a member
2787 : : pointer. */
2788 : 106509 : if (gimple_code (def) != GIMPLE_PHI
2789 : 1130 : || gimple_phi_num_args (def) != 2
2790 : 1122 : || !POINTER_TYPE_P (TREE_TYPE (target))
2791 : 107631 : || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
2792 : : return;
2793 : :
2794 : : /* First, we need to check whether one of these is a load from a member
2795 : : pointer that is a parameter to this function. */
2796 : 853 : tree n1 = PHI_ARG_DEF (def, 0);
2797 : 853 : tree n2 = PHI_ARG_DEF (def, 1);
2798 : 1706 : if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
2799 : : return;
2800 : 853 : gimple *d1 = SSA_NAME_DEF_STMT (n1);
2801 : 853 : gimple *d2 = SSA_NAME_DEF_STMT (n2);
2802 : :
2803 : 853 : tree rec;
2804 : 853 : basic_block bb, virt_bb;
2805 : 853 : basic_block join = gimple_bb (def);
2806 : 853 : if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
2807 : : {
2808 : 0 : if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
2809 : : return;
2810 : :
2811 : 0 : bb = EDGE_PRED (join, 0)->src;
2812 : 0 : virt_bb = gimple_bb (d2);
2813 : : }
2814 : 853 : else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
2815 : : {
2816 : 424 : bb = EDGE_PRED (join, 1)->src;
2817 : 424 : virt_bb = gimple_bb (d1);
2818 : : }
2819 : : else
2820 : : return;
2821 : :
2822 : : /* Second, we need to check that the basic blocks are laid out in the way
2823 : : corresponding to the pattern. */
2824 : :
2825 : 848 : if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
2826 : 848 : || single_succ (virt_bb) != join)
2827 : : return;
2828 : :
2829 : :
2830 : 424 : if (single_pred (virt_bb) != bb)
2831 : : {
2832 : : /* In cases when the distinction between a normal and a virtual
2833 : : function is encoded in the delta field, the load of the
2834 : : actual non-virtual function pointer can be in its own BB. */
2835 : :
2836 : 0 : if (!single_pred_p (bb) || !single_succ_p (bb))
2837 : : return;
2838 : 0 : bb = single_pred (bb);
2839 : 0 : if (bb != single_pred (virt_bb))
2840 : : return;
2841 : : }
2842 : :
2843 : : /* Third, let's see that the branching is done depending on the least
2844 : : significant bit of the pfn. */
2845 : :
2846 : 848 : gcond *branch = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
2847 : 424 : if (!branch)
2848 : : return;
2849 : :
2850 : 424 : if ((gimple_cond_code (branch) != NE_EXPR
2851 : 0 : && gimple_cond_code (branch) != EQ_EXPR)
2852 : 424 : || !integer_zerop (gimple_cond_rhs (branch)))
2853 : 0 : return;
2854 : :
2855 : 424 : tree cond = gimple_cond_lhs (branch);
2856 : 424 : if (!ipa_is_ssa_with_stmt_def (cond))
2857 : : return;
2858 : :
2859 : 424 : def = SSA_NAME_DEF_STMT (cond);
2860 : 424 : if (!is_gimple_assign (def)
2861 : 424 : || gimple_assign_rhs_code (def) != BIT_AND_EXPR
2862 : 848 : || !integer_onep (gimple_assign_rhs2 (def)))
2863 : 0 : return;
2864 : :
2865 : 424 : cond = gimple_assign_rhs1 (def);
2866 : 424 : if (!ipa_is_ssa_with_stmt_def (cond))
2867 : : return;
2868 : :
2869 : 424 : def = SSA_NAME_DEF_STMT (cond);
2870 : :
2871 : 424 : if (is_gimple_assign (def)
2872 : 424 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
2873 : : {
2874 : 424 : cond = gimple_assign_rhs1 (def);
2875 : 424 : if (!ipa_is_ssa_with_stmt_def (cond))
2876 : : return;
2877 : 424 : def = SSA_NAME_DEF_STMT (cond);
2878 : : }
2879 : :
2880 : 424 : tree rec2;
2881 : 424 : rec2 = ipa_get_stmt_member_ptr_load_param (def,
2882 : : (TARGET_PTRMEMFUNC_VBIT_LOCATION
2883 : : == ptrmemfunc_vbit_in_delta),
2884 : : NULL);
2885 : 424 : if (rec != rec2)
2886 : : return;
2887 : :
2888 : 424 : if (TREE_CODE (rec) == SSA_NAME)
2889 : : {
2890 : 286 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (rec));
2891 : 286 : if (index < 0
2892 : 286 : || !parm_ref_data_preserved_p (fbi, index, call,
2893 : : gimple_assign_rhs1 (def)))
2894 : 8 : return;
2895 : 278 : by_ref = true;
2896 : : }
2897 : : else
2898 : : {
2899 : 138 : index = ipa_get_param_decl_index (info, rec);
2900 : 138 : if (index < 0
2901 : 138 : || !parm_preserved_before_stmt_p (fbi, index, call, rec))
2902 : 0 : return;
2903 : 138 : by_ref = false;
2904 : : }
2905 : :
2906 : 416 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
2907 : : call, false);
2908 : 416 : cs->indirect_info->offset = offset;
2909 : 416 : cs->indirect_info->agg_contents = 1;
2910 : 416 : cs->indirect_info->member_ptr = 1;
2911 : 416 : cs->indirect_info->by_ref = by_ref;
2912 : 416 : cs->indirect_info->guaranteed_unmodified = 1;
2913 : :
2914 : 416 : return;
2915 : : }
2916 : :
2917 : : /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
2918 : : object referenced in the expression is a formal parameter of the caller
2919 : : FBI->node (described by FBI->info), create a call note for the
2920 : : statement. */
2921 : :
2922 : : static void
2923 : 25113 : ipa_analyze_virtual_call_uses (struct ipa_func_body_info *fbi,
2924 : : gcall *call, tree target)
2925 : : {
2926 : 25113 : tree obj = OBJ_TYPE_REF_OBJECT (target);
2927 : 25113 : int index;
2928 : 25113 : HOST_WIDE_INT anc_offset;
2929 : :
2930 : 25113 : if (!flag_devirtualize)
2931 : 15480 : return;
2932 : :
2933 : 24858 : if (TREE_CODE (obj) != SSA_NAME)
2934 : : return;
2935 : :
2936 : 24499 : class ipa_node_params *info = fbi->info;
2937 : 24499 : if (SSA_NAME_IS_DEFAULT_DEF (obj))
2938 : : {
2939 : 8880 : if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
2940 : : return;
2941 : :
2942 : 8880 : anc_offset = 0;
2943 : 8880 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
2944 : 8880 : gcc_assert (index >= 0);
2945 : 8880 : if (detect_type_change_ssa (fbi, obj, obj_type_ref_class (target),
2946 : : call))
2947 : : return;
2948 : : }
2949 : : else
2950 : : {
2951 : 15619 : gimple *stmt = SSA_NAME_DEF_STMT (obj);
2952 : 15619 : tree expr;
2953 : :
2954 : 15619 : expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
2955 : 15619 : if (!expr)
2956 : : return;
2957 : 753 : index = ipa_get_param_decl_index (info,
2958 : 753 : SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
2959 : 753 : gcc_assert (index >= 0);
2960 : 753 : if (detect_type_change (fbi, obj, expr, obj_type_ref_class (target),
2961 : : call, anc_offset))
2962 : : return;
2963 : : }
2964 : :
2965 : 9633 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
2966 : : call, true);
2967 : 9633 : class cgraph_indirect_call_info *ii = cs->indirect_info;
2968 : 9633 : ii->offset = anc_offset;
2969 : 9633 : ii->otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
2970 : 9633 : ii->otr_type = obj_type_ref_class (target);
2971 : 9633 : ii->polymorphic = 1;
2972 : : }
2973 : :
2974 : : /* Analyze a call statement CALL whether and how it utilizes formal parameters
2975 : : of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
2976 : : containing intermediate information about each formal parameter. */
2977 : :
2978 : : static void
2979 : 5481272 : ipa_analyze_call_uses (struct ipa_func_body_info *fbi, gcall *call)
2980 : : {
2981 : 5481272 : tree target = gimple_call_fn (call);
2982 : :
2983 : 5481272 : if (!target
2984 : 5481272 : || (TREE_CODE (target) != SSA_NAME
2985 : 5175029 : && !virtual_method_call_p (target)))
2986 : 5343867 : return;
2987 : :
2988 : 137405 : struct cgraph_edge *cs = fbi->node->get_edge (call);
2989 : : /* If we previously turned the call into a direct call, there is
2990 : : no need to analyze. */
2991 : 137405 : if (cs && !cs->indirect_unknown_callee)
2992 : : return;
2993 : :
2994 : 137402 : if (cs->indirect_info->polymorphic && flag_devirtualize)
2995 : : {
2996 : 24858 : tree instance;
2997 : 24858 : tree target = gimple_call_fn (call);
2998 : 24858 : ipa_polymorphic_call_context context (current_function_decl,
2999 : 24858 : target, call, &instance);
3000 : :
3001 : 24858 : gcc_checking_assert (cs->indirect_info->otr_type
3002 : : == obj_type_ref_class (target));
3003 : 24858 : gcc_checking_assert (cs->indirect_info->otr_token
3004 : : == tree_to_shwi (OBJ_TYPE_REF_TOKEN (target)));
3005 : :
3006 : 49716 : cs->indirect_info->vptr_changed
3007 : 49716 : = !context.get_dynamic_type (instance,
3008 : 24858 : OBJ_TYPE_REF_OBJECT (target),
3009 : : obj_type_ref_class (target), call,
3010 : : &fbi->aa_walk_budget);
3011 : 24858 : cs->indirect_info->context = context;
3012 : : }
3013 : :
3014 : 137402 : if (TREE_CODE (target) == SSA_NAME)
3015 : 112289 : ipa_analyze_indirect_call_uses (fbi, call, target);
3016 : 25113 : else if (virtual_method_call_p (target))
3017 : 25113 : ipa_analyze_virtual_call_uses (fbi, call, target);
3018 : : }
3019 : :
3020 : :
3021 : : /* Analyze the call statement STMT with respect to formal parameters (described
3022 : : in INFO) of caller given by FBI->NODE. Currently it only checks whether
3023 : : formal parameters are called. */
3024 : :
3025 : : static void
3026 : 30232731 : ipa_analyze_stmt_uses (struct ipa_func_body_info *fbi, gimple *stmt)
3027 : : {
3028 : 30232731 : if (is_gimple_call (stmt))
3029 : 5481272 : ipa_analyze_call_uses (fbi, as_a <gcall *> (stmt));
3030 : 30232731 : }
3031 : :
3032 : : /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
3033 : : If OP is a parameter declaration, mark it as used in the info structure
3034 : : passed in DATA. */
3035 : :
3036 : : static bool
3037 : 18812842 : visit_ref_for_mod_analysis (gimple *, tree op, tree, void *data)
3038 : : {
3039 : 18812842 : class ipa_node_params *info = (class ipa_node_params *) data;
3040 : :
3041 : 18812842 : op = get_base_address (op);
3042 : 18812842 : if (op
3043 : 18812842 : && TREE_CODE (op) == PARM_DECL)
3044 : : {
3045 : 433137 : int index = ipa_get_param_decl_index (info, op);
3046 : 433137 : gcc_assert (index >= 0);
3047 : 433137 : ipa_set_param_used (info, index, true);
3048 : : }
3049 : :
3050 : 18812842 : return false;
3051 : : }
3052 : :
3053 : : /* Scan the statements in BB and inspect the uses of formal parameters. Store
3054 : : the findings in various structures of the associated ipa_node_params
3055 : : structure, such as parameter flags, notes etc. FBI holds various data about
3056 : : the function being analyzed. */
3057 : :
3058 : : static void
3059 : 10636623 : ipa_analyze_params_uses_in_bb (struct ipa_func_body_info *fbi, basic_block bb)
3060 : : {
3061 : 10636623 : gimple_stmt_iterator gsi;
3062 : 72744847 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3063 : : {
3064 : 51471601 : gimple *stmt = gsi_stmt (gsi);
3065 : :
3066 : 51471601 : if (is_gimple_debug (stmt))
3067 : 21238870 : continue;
3068 : :
3069 : 30232731 : ipa_analyze_stmt_uses (fbi, stmt);
3070 : 30232731 : walk_stmt_load_store_addr_ops (stmt, fbi->info,
3071 : : visit_ref_for_mod_analysis,
3072 : : visit_ref_for_mod_analysis,
3073 : : visit_ref_for_mod_analysis);
3074 : : }
3075 : 13570623 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3076 : 2934000 : walk_stmt_load_store_addr_ops (gsi_stmt (gsi), fbi->info,
3077 : : visit_ref_for_mod_analysis,
3078 : : visit_ref_for_mod_analysis,
3079 : : visit_ref_for_mod_analysis);
3080 : 10636623 : }
3081 : :
3082 : : /* Return true EXPR is a load from a dereference of SSA_NAME NAME. */
3083 : :
3084 : : static bool
3085 : 4028016 : load_from_dereferenced_name (tree expr, tree name)
3086 : : {
3087 : 4028016 : tree base = get_base_address (expr);
3088 : 4028016 : return (TREE_CODE (base) == MEM_REF
3089 : 4028016 : && TREE_OPERAND (base, 0) == name);
3090 : : }
3091 : :
3092 : : /* Calculate controlled uses of parameters of NODE. */
3093 : :
3094 : : static void
3095 : 1303620 : ipa_analyze_controlled_uses (struct cgraph_node *node)
3096 : : {
3097 : 1303620 : ipa_node_params *info = ipa_node_params_sum->get (node);
3098 : :
3099 : 7099969 : for (int i = 0; i < ipa_get_param_count (info); i++)
3100 : : {
3101 : 2369306 : tree parm = ipa_get_param (info, i);
3102 : 2369306 : int call_uses = 0;
3103 : 2369306 : bool load_dereferenced = false;
3104 : :
3105 : : /* For SSA regs see if parameter is used. For non-SSA we compute
3106 : : the flag during modification analysis. */
3107 : 2369306 : if (is_gimple_reg (parm))
3108 : : {
3109 : 2157848 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
3110 : : parm);
3111 : 2157848 : if (ddef && !has_zero_uses (ddef))
3112 : : {
3113 : 1891312 : imm_use_iterator imm_iter;
3114 : 1891312 : gimple *stmt;
3115 : :
3116 : 1891312 : ipa_set_param_used (info, i, true);
3117 : 4514647 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, ddef)
3118 : : {
3119 : 3712523 : if (is_gimple_debug (stmt))
3120 : 646575 : continue;
3121 : :
3122 : 3065948 : int all_stmt_uses = 0;
3123 : 3065948 : use_operand_p use_p;
3124 : 6157980 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3125 : 3092032 : all_stmt_uses++;
3126 : :
3127 : 3065948 : if (is_gimple_call (stmt))
3128 : : {
3129 : 1158838 : if (gimple_call_internal_p (stmt))
3130 : : {
3131 : : call_uses = IPA_UNDESCRIBED_USE;
3132 : : break;
3133 : : }
3134 : 1104476 : int recognized_stmt_uses;
3135 : 1104476 : if (gimple_call_fn (stmt) == ddef)
3136 : : recognized_stmt_uses = 1;
3137 : : else
3138 : 1101010 : recognized_stmt_uses = 0;
3139 : 1104476 : unsigned arg_count = gimple_call_num_args (stmt);
3140 : 4938572 : for (unsigned i = 0; i < arg_count; i++)
3141 : : {
3142 : 3834096 : tree arg = gimple_call_arg (stmt, i);
3143 : 3834096 : if (arg == ddef)
3144 : 1093752 : recognized_stmt_uses++;
3145 : 2740344 : else if (load_from_dereferenced_name (arg, ddef))
3146 : : {
3147 : 13609 : load_dereferenced = true;
3148 : 13609 : recognized_stmt_uses++;
3149 : : }
3150 : : }
3151 : :
3152 : 1104476 : if (recognized_stmt_uses != all_stmt_uses)
3153 : : {
3154 : : call_uses = IPA_UNDESCRIBED_USE;
3155 : : break;
3156 : : }
3157 : 1096620 : if (call_uses >= 0)
3158 : 1096620 : call_uses += all_stmt_uses;
3159 : : }
3160 : 1907110 : else if (gimple_assign_single_p (stmt))
3161 : : {
3162 : 1289268 : tree rhs = gimple_assign_rhs1 (stmt);
3163 : 1289268 : if (all_stmt_uses != 1
3164 : 1289268 : || !load_from_dereferenced_name (rhs, ddef))
3165 : : {
3166 : : call_uses = IPA_UNDESCRIBED_USE;
3167 : : break;
3168 : : }
3169 : : load_dereferenced = true;
3170 : : }
3171 : : else
3172 : : {
3173 : : call_uses = IPA_UNDESCRIBED_USE;
3174 : : break;
3175 : : }
3176 : 1891312 : }
3177 : : }
3178 : : else
3179 : : call_uses = 0;
3180 : : }
3181 : : else
3182 : : call_uses = IPA_UNDESCRIBED_USE;
3183 : 2369306 : ipa_set_controlled_uses (info, i, call_uses);
3184 : 2369306 : ipa_set_param_load_dereferenced (info, i, load_dereferenced);
3185 : : }
3186 : 1303620 : }
3187 : :
3188 : : /* Free stuff in BI. */
3189 : :
3190 : : static void
3191 : 60343010 : free_ipa_bb_info (struct ipa_bb_info *bi)
3192 : : {
3193 : 0 : bi->cg_edges.release ();
3194 : 60343010 : bi->param_aa_statuses.release ();
3195 : 0 : }
3196 : :
3197 : : /* Dominator walker driving the analysis. */
3198 : :
3199 : 2607240 : class analysis_dom_walker : public dom_walker
3200 : : {
3201 : : public:
3202 : 1303620 : analysis_dom_walker (struct ipa_func_body_info *fbi)
3203 : 2607240 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi) {}
3204 : :
3205 : : edge before_dom_children (basic_block) final override;
3206 : :
3207 : : private:
3208 : : struct ipa_func_body_info *m_fbi;
3209 : : };
3210 : :
3211 : : edge
3212 : 10636623 : analysis_dom_walker::before_dom_children (basic_block bb)
3213 : : {
3214 : 10636623 : ipa_analyze_params_uses_in_bb (m_fbi, bb);
3215 : 10636623 : ipa_compute_jump_functions_for_bb (m_fbi, bb);
3216 : 10636623 : return NULL;
3217 : : }
3218 : :
3219 : : /* Release body info FBI. */
3220 : :
3221 : : void
3222 : 8169998 : ipa_release_body_info (struct ipa_func_body_info *fbi)
3223 : : {
3224 : 8169998 : int i;
3225 : 8169998 : struct ipa_bb_info *bi;
3226 : :
3227 : 68493498 : FOR_EACH_VEC_ELT (fbi->bb_infos, i, bi)
3228 : 120647000 : free_ipa_bb_info (bi);
3229 : 8169998 : fbi->bb_infos.release ();
3230 : 8169998 : }
3231 : :
3232 : : /* Initialize the array describing properties of formal parameters
3233 : : of NODE, analyze their uses and compute jump functions associated
3234 : : with actual arguments of calls from within NODE. */
3235 : :
3236 : : void
3237 : 2556900 : ipa_analyze_node (struct cgraph_node *node)
3238 : : {
3239 : 2556900 : struct ipa_func_body_info fbi;
3240 : 2556900 : class ipa_node_params *info;
3241 : :
3242 : 2556900 : ipa_check_create_node_params ();
3243 : 2556900 : ipa_check_create_edge_args ();
3244 : 2556900 : info = ipa_node_params_sum->get_create (node);
3245 : :
3246 : 2556900 : if (info->analysis_done)
3247 : 1253280 : return;
3248 : 1304503 : info->analysis_done = 1;
3249 : :
3250 : 1304503 : if (ipa_func_spec_opts_forbid_analysis_p (node)
3251 : 1304503 : || (count_formal_params (node->decl)
3252 : : >= (1 << IPA_PROP_ARG_INDEX_LIMIT_BITS)))
3253 : : {
3254 : 1253280 : gcc_assert (!ipa_get_param_count (info));
3255 : : return;
3256 : : }
3257 : :
3258 : 1303620 : struct function *func = DECL_STRUCT_FUNCTION (node->decl);
3259 : 1303620 : push_cfun (func);
3260 : 1303620 : calculate_dominance_info (CDI_DOMINATORS);
3261 : 1303620 : ipa_initialize_node_params (node);
3262 : 1303620 : ipa_analyze_controlled_uses (node);
3263 : :
3264 : 1303620 : fbi.node = node;
3265 : 1303620 : fbi.info = info;
3266 : 1303620 : fbi.bb_infos = vNULL;
3267 : 1303620 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
3268 : 1303620 : fbi.param_count = ipa_get_param_count (info);
3269 : 1303620 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
3270 : :
3271 : 6453464 : for (struct cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
3272 : : {
3273 : 5149844 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3274 : 5149844 : bi->cg_edges.safe_push (cs);
3275 : : }
3276 : :
3277 : 1441094 : for (struct cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
3278 : : {
3279 : 137474 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3280 : 137474 : bi->cg_edges.safe_push (cs);
3281 : : }
3282 : :
3283 : 1303620 : enable_ranger (cfun, false);
3284 : 1303620 : analysis_dom_walker (&fbi).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3285 : 1303620 : disable_ranger (cfun);
3286 : :
3287 : 1303620 : ipa_release_body_info (&fbi);
3288 : 1303620 : free_dominance_info (CDI_DOMINATORS);
3289 : 1303620 : pop_cfun ();
3290 : : }
3291 : :
3292 : : /* Update the jump functions associated with call graph edge E when the call
3293 : : graph edge CS is being inlined, assuming that E->caller is already (possibly
3294 : : indirectly) inlined into CS->callee and that E has not been inlined. */
3295 : :
3296 : : static void
3297 : 2220283 : update_jump_functions_after_inlining (struct cgraph_edge *cs,
3298 : : struct cgraph_edge *e)
3299 : : {
3300 : 2220283 : ipa_edge_args *top = ipa_edge_args_sum->get (cs);
3301 : 2220283 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
3302 : 2220283 : if (!args)
3303 : : return;
3304 : 1212644 : int count = ipa_get_cs_argument_count (args);
3305 : 1212644 : int i;
3306 : :
3307 : 3627119 : for (i = 0; i < count; i++)
3308 : : {
3309 : 2414475 : struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
3310 : 2414475 : class ipa_polymorphic_call_context *dst_ctx
3311 : 2414475 : = ipa_get_ith_polymorhic_call_context (args, i);
3312 : :
3313 : 2414475 : if (dst->agg.items)
3314 : : {
3315 : : struct ipa_agg_jf_item *item;
3316 : : int j;
3317 : :
3318 : 198543 : FOR_EACH_VEC_ELT (*dst->agg.items, j, item)
3319 : : {
3320 : 133514 : int dst_fid;
3321 : 133514 : struct ipa_jump_func *src;
3322 : :
3323 : 236094 : if (item->jftype != IPA_JF_PASS_THROUGH
3324 : 133514 : && item->jftype != IPA_JF_LOAD_AGG)
3325 : 102580 : continue;
3326 : :
3327 : 30934 : dst_fid = item->value.pass_through.formal_id;
3328 : 61868 : if (!top || dst_fid >= ipa_get_cs_argument_count (top))
3329 : : {
3330 : 0 : item->jftype = IPA_JF_UNKNOWN;
3331 : 0 : continue;
3332 : : }
3333 : :
3334 : 30934 : item->value.pass_through.formal_id = -1;
3335 : 30934 : src = ipa_get_ith_jump_func (top, dst_fid);
3336 : 30934 : if (src->type == IPA_JF_CONST)
3337 : : {
3338 : 1260 : if (item->jftype == IPA_JF_PASS_THROUGH
3339 : 1150 : && item->value.pass_through.operation == NOP_EXPR)
3340 : : {
3341 : 1102 : item->jftype = IPA_JF_CONST;
3342 : 1102 : item->value.constant = src->value.constant.value;
3343 : 1102 : continue;
3344 : : }
3345 : : }
3346 : 29674 : else if (src->type == IPA_JF_PASS_THROUGH
3347 : 4822 : && src->value.pass_through.operation == NOP_EXPR)
3348 : : {
3349 : 4670 : if (item->jftype == IPA_JF_PASS_THROUGH
3350 : 3250 : || !item->value.load_agg.by_ref
3351 : 1962 : || src->value.pass_through.agg_preserved)
3352 : 3349 : item->value.pass_through.formal_id
3353 : 3349 : = src->value.pass_through.formal_id;
3354 : : }
3355 : 25004 : else if (src->type == IPA_JF_ANCESTOR)
3356 : : {
3357 : 1763 : if (item->jftype == IPA_JF_PASS_THROUGH)
3358 : : {
3359 : 862 : if (!src->value.ancestor.offset)
3360 : 565 : item->value.pass_through.formal_id
3361 : 565 : = src->value.ancestor.formal_id;
3362 : : }
3363 : 901 : else if (src->value.ancestor.agg_preserved)
3364 : : {
3365 : 136 : gcc_checking_assert (item->value.load_agg.by_ref);
3366 : :
3367 : 136 : item->value.pass_through.formal_id
3368 : 136 : = src->value.ancestor.formal_id;
3369 : 136 : item->value.load_agg.offset
3370 : 136 : += src->value.ancestor.offset;
3371 : : }
3372 : : }
3373 : :
3374 : 29832 : if (item->value.pass_through.formal_id < 0)
3375 : 25782 : item->jftype = IPA_JF_UNKNOWN;
3376 : : }
3377 : : }
3378 : :
3379 : 2414475 : if (!top)
3380 : : {
3381 : 13092 : ipa_set_jf_unknown (dst);
3382 : 13092 : continue;
3383 : : }
3384 : :
3385 : 2401383 : if (dst->type == IPA_JF_ANCESTOR)
3386 : : {
3387 : 134697 : struct ipa_jump_func *src;
3388 : 134697 : int dst_fid = dst->value.ancestor.formal_id;
3389 : 134697 : class ipa_polymorphic_call_context *src_ctx
3390 : 134697 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3391 : :
3392 : : /* Variable number of arguments can cause havoc if we try to access
3393 : : one that does not exist in the inlined edge. So make sure we
3394 : : don't. */
3395 : 269394 : if (dst_fid >= ipa_get_cs_argument_count (top))
3396 : : {
3397 : 0 : ipa_set_jf_unknown (dst);
3398 : 0 : continue;
3399 : : }
3400 : :
3401 : 134697 : src = ipa_get_ith_jump_func (top, dst_fid);
3402 : :
3403 : 134697 : if (src_ctx && !src_ctx->useless_p ())
3404 : : {
3405 : 39048 : class ipa_polymorphic_call_context ctx = *src_ctx;
3406 : :
3407 : : /* TODO: Make type preserved safe WRT contexts. */
3408 : 39048 : if (!ipa_get_jf_ancestor_type_preserved (dst))
3409 : 31731 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3410 : 39048 : ctx.offset_by (dst->value.ancestor.offset);
3411 : 78096 : if (!ctx.useless_p ())
3412 : : {
3413 : 36580 : if (!dst_ctx)
3414 : : {
3415 : 6234 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3416 : : count, true);
3417 : 6234 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3418 : : }
3419 : :
3420 : 36580 : dst_ctx->combine_with (ctx);
3421 : : }
3422 : : }
3423 : :
3424 : : /* Parameter and argument in ancestor jump function must be pointer
3425 : : type, which means access to aggregate must be by-reference. */
3426 : 134697 : gcc_assert (!src->agg.items || src->agg.by_ref);
3427 : :
3428 : 134697 : if (src->agg.items && dst->value.ancestor.agg_preserved)
3429 : : {
3430 : 1652 : struct ipa_agg_jf_item *item;
3431 : 1652 : int j;
3432 : :
3433 : : /* Currently we do not produce clobber aggregate jump functions,
3434 : : replace with merging when we do. */
3435 : 1652 : gcc_assert (!dst->agg.items);
3436 : :
3437 : 1652 : dst->agg.items = vec_safe_copy (src->agg.items);
3438 : 1652 : dst->agg.by_ref = src->agg.by_ref;
3439 : 6298 : FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
3440 : 4646 : item->offset -= dst->value.ancestor.offset;
3441 : : }
3442 : :
3443 : 134697 : if (src->type == IPA_JF_PASS_THROUGH
3444 : 22571 : && src->value.pass_through.operation == NOP_EXPR)
3445 : : {
3446 : 22567 : dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
3447 : 22567 : dst->value.ancestor.agg_preserved &=
3448 : 22567 : src->value.pass_through.agg_preserved;
3449 : : }
3450 : 112130 : else if (src->type == IPA_JF_ANCESTOR)
3451 : : {
3452 : 6408 : dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
3453 : 6408 : dst->value.ancestor.offset += src->value.ancestor.offset;
3454 : 6408 : dst->value.ancestor.agg_preserved &=
3455 : 6408 : src->value.ancestor.agg_preserved;
3456 : 6408 : dst->value.ancestor.keep_null |= src->value.ancestor.keep_null;
3457 : : }
3458 : : else
3459 : 105722 : ipa_set_jf_unknown (dst);
3460 : : }
3461 : 2266686 : else if (dst->type == IPA_JF_PASS_THROUGH)
3462 : : {
3463 : 656706 : struct ipa_jump_func *src;
3464 : : /* We must check range due to calls with variable number of arguments
3465 : : and we cannot combine jump functions with operations. */
3466 : 656706 : if (dst->value.pass_through.operation == NOP_EXPR
3467 : 656706 : && (top && dst->value.pass_through.formal_id
3468 : 628185 : < ipa_get_cs_argument_count (top)))
3469 : : {
3470 : 628171 : int dst_fid = dst->value.pass_through.formal_id;
3471 : 628171 : src = ipa_get_ith_jump_func (top, dst_fid);
3472 : 628171 : bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
3473 : 628171 : class ipa_polymorphic_call_context *src_ctx
3474 : 718452 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3475 : :
3476 : 90281 : if (src_ctx && !src_ctx->useless_p ())
3477 : : {
3478 : 50849 : class ipa_polymorphic_call_context ctx = *src_ctx;
3479 : :
3480 : : /* TODO: Make type preserved safe WRT contexts. */
3481 : 50849 : if (!ipa_get_jf_pass_through_type_preserved (dst))
3482 : 22170 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3483 : 101698 : if (!ctx.useless_p ())
3484 : : {
3485 : 49172 : if (!dst_ctx)
3486 : : {
3487 : 12517 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3488 : : count, true);
3489 : 12517 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3490 : : }
3491 : 49172 : dst_ctx->combine_with (ctx);
3492 : : }
3493 : : }
3494 : 628171 : switch (src->type)
3495 : : {
3496 : 254661 : case IPA_JF_UNKNOWN:
3497 : 254661 : ipa_set_jf_unknown (dst);
3498 : 254661 : break;
3499 : 117942 : case IPA_JF_CONST:
3500 : 117942 : {
3501 : 117942 : bool rd = ipa_get_jf_pass_through_refdesc_decremented (dst);
3502 : 117942 : ipa_set_jf_cst_copy (dst, src);
3503 : 117942 : if (rd)
3504 : 25 : ipa_zap_jf_refdesc (dst);
3505 : : }
3506 : :
3507 : : break;
3508 : :
3509 : 235638 : case IPA_JF_PASS_THROUGH:
3510 : 235638 : {
3511 : 235638 : int formal_id = ipa_get_jf_pass_through_formal_id (src);
3512 : 235638 : enum tree_code operation;
3513 : 235638 : operation = ipa_get_jf_pass_through_operation (src);
3514 : :
3515 : 235638 : if (operation == NOP_EXPR)
3516 : : {
3517 : 234719 : bool agg_p;
3518 : 469438 : agg_p = dst_agg_p
3519 : 234719 : && ipa_get_jf_pass_through_agg_preserved (src);
3520 : 234719 : ipa_set_jf_simple_pass_through (dst, formal_id, agg_p);
3521 : : }
3522 : 919 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
3523 : 9 : ipa_set_jf_unary_pass_through (dst, formal_id, operation);
3524 : : else
3525 : : {
3526 : 910 : tree operand = ipa_get_jf_pass_through_operand (src);
3527 : 910 : ipa_set_jf_arith_pass_through (dst, formal_id, operand,
3528 : : operation);
3529 : : }
3530 : : break;
3531 : : }
3532 : 19930 : case IPA_JF_ANCESTOR:
3533 : 19930 : {
3534 : 19930 : bool agg_p;
3535 : 39860 : agg_p = dst_agg_p
3536 : 19930 : && ipa_get_jf_ancestor_agg_preserved (src);
3537 : 19930 : ipa_set_ancestor_jf (dst,
3538 : : ipa_get_jf_ancestor_offset (src),
3539 : : ipa_get_jf_ancestor_formal_id (src),
3540 : : agg_p,
3541 : 19930 : ipa_get_jf_ancestor_keep_null (src));
3542 : 19930 : break;
3543 : : }
3544 : 0 : default:
3545 : 0 : gcc_unreachable ();
3546 : : }
3547 : :
3548 : 628171 : if (src->m_vr && src->m_vr->known_p ())
3549 : : {
3550 : 416756 : value_range svr (src->m_vr->type ());
3551 : 416756 : if (!dst->m_vr || !dst->m_vr->known_p ())
3552 : 178850 : ipa_set_jfunc_vr (dst, *src->m_vr);
3553 : 237906 : else if (ipa_vr_operation_and_type_effects (svr, *src->m_vr,
3554 : : NOP_EXPR,
3555 : 237906 : dst->m_vr->type (),
3556 : 237906 : src->m_vr->type ()))
3557 : : {
3558 : 237898 : value_range dvr;
3559 : 237898 : dst->m_vr->get_vrange (dvr);
3560 : 237898 : dvr.intersect (svr);
3561 : 237898 : if (!dvr.undefined_p ())
3562 : 232208 : ipa_set_jfunc_vr (dst, dvr);
3563 : 237898 : }
3564 : 416756 : }
3565 : :
3566 : 628171 : if (src->agg.items
3567 : 23978 : && (dst_agg_p || !src->agg.by_ref))
3568 : : {
3569 : : /* Currently we do not produce clobber aggregate jump
3570 : : functions, replace with merging when we do. */
3571 : 19613 : gcc_assert (!dst->agg.items);
3572 : :
3573 : 19613 : dst->agg.by_ref = src->agg.by_ref;
3574 : 19613 : dst->agg.items = vec_safe_copy (src->agg.items);
3575 : : }
3576 : : }
3577 : : else
3578 : 28535 : ipa_set_jf_unknown (dst);
3579 : : }
3580 : : }
3581 : : }
3582 : :
3583 : : /* If TARGET is an addr_expr of a function declaration, make it the
3584 : : (SPECULATIVE)destination of an indirect edge IE and return the edge.
3585 : : Otherwise, return NULL. */
3586 : :
3587 : : struct cgraph_edge *
3588 : 2506 : ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target,
3589 : : bool speculative)
3590 : : {
3591 : 2506 : struct cgraph_node *callee;
3592 : 2506 : bool unreachable = false;
3593 : :
3594 : 2506 : if (TREE_CODE (target) == ADDR_EXPR)
3595 : 483 : target = TREE_OPERAND (target, 0);
3596 : 2506 : if (TREE_CODE (target) != FUNCTION_DECL)
3597 : : {
3598 : 14 : target = canonicalize_constructor_val (target, NULL);
3599 : 14 : if (!target || TREE_CODE (target) != FUNCTION_DECL)
3600 : : {
3601 : : /* Member pointer call that goes through a VMT lookup. */
3602 : 14 : if (ie->indirect_info->member_ptr
3603 : : /* Or if target is not an invariant expression and we do not
3604 : : know if it will evaulate to function at runtime.
3605 : : This can happen when folding through &VAR, where &VAR
3606 : : is IP invariant, but VAR itself is not.
3607 : :
3608 : : TODO: Revisit this when GCC 5 is branched. It seems that
3609 : : member_ptr check is not needed and that we may try to fold
3610 : : the expression and see if VAR is readonly. */
3611 : 14 : || !is_gimple_ip_invariant (target))
3612 : : {
3613 : 3 : if (dump_enabled_p ())
3614 : : {
3615 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3616 : : "discovered direct call non-invariant %s\n",
3617 : 0 : ie->caller->dump_name ());
3618 : : }
3619 : 3 : return NULL;
3620 : : }
3621 : :
3622 : :
3623 : 11 : if (dump_enabled_p ())
3624 : : {
3625 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3626 : : "discovered direct call to non-function in %s, "
3627 : : "making it __builtin_unreachable\n",
3628 : 0 : ie->caller->dump_name ());
3629 : : }
3630 : :
3631 : 11 : target = builtin_decl_unreachable ();
3632 : 11 : callee = cgraph_node::get_create (target);
3633 : 11 : unreachable = true;
3634 : : }
3635 : : else
3636 : 0 : callee = cgraph_node::get (target);
3637 : : }
3638 : : else
3639 : 2492 : callee = cgraph_node::get (target);
3640 : :
3641 : : /* Because may-edges are not explicitely represented and vtable may be external,
3642 : : we may create the first reference to the object in the unit. */
3643 : 2503 : if (!callee || callee->inlined_to)
3644 : : {
3645 : :
3646 : : /* We are better to ensure we can refer to it.
3647 : : In the case of static functions we are out of luck, since we already
3648 : : removed its body. In the case of public functions we may or may
3649 : : not introduce the reference. */
3650 : 0 : if (!canonicalize_constructor_val (target, NULL)
3651 : 0 : || !TREE_PUBLIC (target))
3652 : : {
3653 : 0 : if (dump_file)
3654 : 0 : fprintf (dump_file, "ipa-prop: Discovered call to a known target "
3655 : : "(%s -> %s) but cannot refer to it. Giving up.\n",
3656 : 0 : ie->caller->dump_name (),
3657 : 0 : ie->callee->dump_name ());
3658 : 0 : return NULL;
3659 : : }
3660 : 0 : callee = cgraph_node::get_create (target);
3661 : : }
3662 : :
3663 : : /* If the edge is already speculated. */
3664 : 2503 : if (speculative && ie->speculative)
3665 : : {
3666 : 1 : if (dump_file)
3667 : : {
3668 : 0 : cgraph_edge *e2 = ie->speculative_call_for_target (callee);
3669 : 0 : if (!e2)
3670 : : {
3671 : 0 : if (dump_file)
3672 : 0 : fprintf (dump_file, "ipa-prop: Discovered call to a "
3673 : : "speculative target (%s -> %s) but the call is "
3674 : : "already speculated to different target. "
3675 : : "Giving up.\n",
3676 : 0 : ie->caller->dump_name (), callee->dump_name ());
3677 : : }
3678 : : else
3679 : : {
3680 : 0 : if (dump_file)
3681 : 0 : fprintf (dump_file,
3682 : : "ipa-prop: Discovered call to a speculative target "
3683 : : "(%s -> %s) this agree with previous speculation.\n",
3684 : 0 : ie->caller->dump_name (), callee->dump_name ());
3685 : : }
3686 : : }
3687 : 1 : return NULL;
3688 : : }
3689 : :
3690 : 2502 : if (!dbg_cnt (devirt))
3691 : : return NULL;
3692 : :
3693 : 2502 : ipa_check_create_node_params ();
3694 : :
3695 : : /* We cannot make edges to inline clones. It is bug that someone removed
3696 : : the cgraph node too early. */
3697 : 2502 : gcc_assert (!callee->inlined_to);
3698 : :
3699 : 2502 : if (dump_file && !unreachable)
3700 : : {
3701 : 522 : fprintf (dump_file, "ipa-prop: Discovered %s call to a %s target "
3702 : : "(%s -> %s), for stmt ",
3703 : 174 : ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
3704 : : speculative ? "speculative" : "known",
3705 : 174 : ie->caller->dump_name (),
3706 : : callee->dump_name ());
3707 : 174 : if (ie->call_stmt)
3708 : 166 : print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
3709 : : else
3710 : 8 : fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
3711 : : }
3712 : 2502 : if (dump_enabled_p ())
3713 : : {
3714 : 348 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3715 : : "converting indirect call in %s to direct call to %s\n",
3716 : 174 : ie->caller->dump_name (), callee->dump_name ());
3717 : : }
3718 : 2502 : if (!speculative)
3719 : : {
3720 : 2485 : struct cgraph_edge *orig = ie;
3721 : 2485 : ie = cgraph_edge::make_direct (ie, callee);
3722 : : /* If we resolved speculative edge the cost is already up to date
3723 : : for direct call (adjusted by inline_edge_duplication_hook). */
3724 : 2485 : if (ie == orig)
3725 : : {
3726 : 2196 : ipa_call_summary *es = ipa_call_summaries->get (ie);
3727 : 2196 : es->call_stmt_size -= (eni_size_weights.indirect_call_cost
3728 : 2196 : - eni_size_weights.call_cost);
3729 : 2196 : es->call_stmt_time -= (eni_time_weights.indirect_call_cost
3730 : 2196 : - eni_time_weights.call_cost);
3731 : : }
3732 : : }
3733 : : else
3734 : : {
3735 : 17 : if (!callee->can_be_discarded_p ())
3736 : : {
3737 : 3 : cgraph_node *alias;
3738 : 3 : alias = dyn_cast<cgraph_node *> (callee->noninterposable_alias ());
3739 : : if (alias)
3740 : 17 : callee = alias;
3741 : : }
3742 : : /* make_speculative will update ie's cost to direct call cost. */
3743 : 17 : ie = ie->make_speculative
3744 : 17 : (callee, ie->count.apply_scale (8, 10));
3745 : : }
3746 : :
3747 : : return ie;
3748 : : }
3749 : :
3750 : : /* Attempt to locate an interprocedural constant at a given REQ_OFFSET in
3751 : : CONSTRUCTOR and return it. Return NULL if the search fails for some
3752 : : reason. */
3753 : :
3754 : : static tree
3755 : 11032 : find_constructor_constant_at_offset (tree constructor, HOST_WIDE_INT req_offset)
3756 : : {
3757 : 15746 : tree type = TREE_TYPE (constructor);
3758 : 15746 : if (TREE_CODE (type) != ARRAY_TYPE
3759 : 15746 : && TREE_CODE (type) != RECORD_TYPE)
3760 : : return NULL;
3761 : :
3762 : 15734 : unsigned ix;
3763 : 15734 : tree index, val;
3764 : 20996 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (constructor), ix, index, val)
3765 : : {
3766 : 20526 : HOST_WIDE_INT elt_offset;
3767 : 20526 : if (TREE_CODE (type) == ARRAY_TYPE)
3768 : : {
3769 : 237 : offset_int off;
3770 : 237 : tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
3771 : 237 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
3772 : :
3773 : 237 : if (index)
3774 : : {
3775 : 237 : if (TREE_CODE (index) == RANGE_EXPR)
3776 : 48 : off = wi::to_offset (TREE_OPERAND (index, 0));
3777 : : else
3778 : 189 : off = wi::to_offset (index);
3779 : 237 : if (TYPE_DOMAIN (type) && TYPE_MIN_VALUE (TYPE_DOMAIN (type)))
3780 : : {
3781 : 237 : tree low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3782 : 237 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
3783 : 237 : off = wi::sext (off - wi::to_offset (low_bound),
3784 : 237 : TYPE_PRECISION (TREE_TYPE (index)));
3785 : : }
3786 : 237 : off *= wi::to_offset (unit_size);
3787 : : /* ??? Handle more than just the first index of a
3788 : : RANGE_EXPR. */
3789 : : }
3790 : : else
3791 : 0 : off = wi::to_offset (unit_size) * ix;
3792 : :
3793 : 237 : off = wi::lshift (off, LOG2_BITS_PER_UNIT);
3794 : 237 : if (!wi::fits_shwi_p (off) || wi::neg_p (off))
3795 : 0 : continue;
3796 : 237 : elt_offset = off.to_shwi ();
3797 : : }
3798 : 20289 : else if (TREE_CODE (type) == RECORD_TYPE)
3799 : : {
3800 : 20289 : gcc_checking_assert (index && TREE_CODE (index) == FIELD_DECL);
3801 : 20289 : if (DECL_BIT_FIELD (index))
3802 : 0 : continue;
3803 : 20289 : elt_offset = int_bit_position (index);
3804 : : }
3805 : : else
3806 : 0 : gcc_unreachable ();
3807 : :
3808 : 20526 : if (elt_offset > req_offset)
3809 : : return NULL;
3810 : :
3811 : 20526 : if (TREE_CODE (val) == CONSTRUCTOR)
3812 : 4714 : return find_constructor_constant_at_offset (val,
3813 : 4714 : req_offset - elt_offset);
3814 : :
3815 : 15812 : if (elt_offset == req_offset
3816 : 11020 : && is_gimple_reg_type (TREE_TYPE (val))
3817 : 26832 : && is_gimple_ip_invariant (val))
3818 : : return val;
3819 : : }
3820 : : return NULL;
3821 : : }
3822 : :
3823 : : /* Check whether SCALAR could be used to look up an aggregate interprocedural
3824 : : invariant from a static constructor and if so, return it. Otherwise return
3825 : : NULL. */
3826 : :
3827 : : tree
3828 : 10510894 : ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, bool by_ref)
3829 : : {
3830 : 10510894 : if (by_ref)
3831 : : {
3832 : 10496806 : if (TREE_CODE (scalar) != ADDR_EXPR)
3833 : : return NULL;
3834 : 3585098 : scalar = TREE_OPERAND (scalar, 0);
3835 : : }
3836 : :
3837 : 3599186 : if (!VAR_P (scalar)
3838 : 2353054 : || !is_global_var (scalar)
3839 : 105134 : || !TREE_READONLY (scalar)
3840 : 15521 : || !DECL_INITIAL (scalar)
3841 : 3612708 : || TREE_CODE (DECL_INITIAL (scalar)) != CONSTRUCTOR)
3842 : : return NULL;
3843 : :
3844 : 11032 : return find_constructor_constant_at_offset (DECL_INITIAL (scalar), offset);
3845 : : }
3846 : :
3847 : : /* Retrieve value from AGG_JFUNC for the given OFFSET or return NULL if there
3848 : : is none. BY_REF specifies whether the value has to be passed by reference
3849 : : or by value. */
3850 : :
3851 : : static tree
3852 : 25671 : ipa_find_agg_cst_from_jfunc_items (struct ipa_agg_jump_function *agg_jfunc,
3853 : : ipa_node_params *src_info,
3854 : : cgraph_node *src_node,
3855 : : HOST_WIDE_INT offset, bool by_ref)
3856 : : {
3857 : 25671 : if (by_ref != agg_jfunc->by_ref)
3858 : : return NULL_TREE;
3859 : :
3860 : 2841 : for (const ipa_agg_jf_item &item : agg_jfunc->items)
3861 : 993 : if (item.offset == offset)
3862 : 780 : return ipa_agg_value_from_jfunc (src_info, src_node, &item);
3863 : :
3864 : : return NULL_TREE;
3865 : : }
3866 : :
3867 : : /* Remove a reference to SYMBOL from the list of references of a node given by
3868 : : reference description RDESC. Return true if the reference has been
3869 : : successfully found and removed. */
3870 : :
3871 : : static bool
3872 : 6604 : remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
3873 : : {
3874 : 6604 : struct ipa_ref *to_del;
3875 : 6604 : struct cgraph_edge *origin;
3876 : :
3877 : 6604 : origin = rdesc->cs;
3878 : 6604 : if (!origin)
3879 : : return false;
3880 : 6604 : to_del = origin->caller->find_reference (symbol, origin->call_stmt,
3881 : : origin->lto_stmt_uid, IPA_REF_ADDR);
3882 : 6604 : if (!to_del)
3883 : : return false;
3884 : :
3885 : 6604 : to_del->remove_reference ();
3886 : 6604 : if (dump_file)
3887 : 26 : fprintf (dump_file, "ipa-prop: Removed a reference from %s to %s.\n",
3888 : 13 : origin->caller->dump_name (), symbol->dump_name ());
3889 : : return true;
3890 : : }
3891 : :
3892 : : /* If JFUNC has a reference description with refcount different from
3893 : : IPA_UNDESCRIBED_USE, return the reference description, otherwise return
3894 : : NULL. JFUNC must be a constant jump function. */
3895 : :
3896 : : static struct ipa_cst_ref_desc *
3897 : 1159089 : jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
3898 : : {
3899 : 1159089 : struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
3900 : 1159089 : if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
3901 : : return rdesc;
3902 : : else
3903 : 982281 : return NULL;
3904 : : }
3905 : :
3906 : : /* If the value of constant jump function JFUNC is an address of a function
3907 : : declaration, return the associated call graph node. Otherwise return
3908 : : NULL. */
3909 : :
3910 : : static symtab_node *
3911 : 290 : symtab_node_for_jfunc (struct ipa_jump_func *jfunc)
3912 : : {
3913 : 290 : gcc_checking_assert (jfunc->type == IPA_JF_CONST);
3914 : 290 : tree cst = ipa_get_jf_constant (jfunc);
3915 : 290 : if (TREE_CODE (cst) != ADDR_EXPR
3916 : 290 : || (TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL
3917 : 10 : && TREE_CODE (TREE_OPERAND (cst, 0)) != VAR_DECL))
3918 : : return NULL;
3919 : :
3920 : 280 : return symtab_node::get (TREE_OPERAND (cst, 0));
3921 : : }
3922 : :
3923 : :
3924 : : /* If JFUNC is a constant jump function with a usable rdesc, decrement its
3925 : : refcount and if it hits zero, remove reference to SYMBOL from the caller of
3926 : : the edge specified in the rdesc. Return false if either the symbol or the
3927 : : reference could not be found, otherwise return true. */
3928 : :
3929 : : static bool
3930 : 170 : try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
3931 : : {
3932 : 170 : struct ipa_cst_ref_desc *rdesc;
3933 : 170 : if (jfunc->type == IPA_JF_CONST
3934 : 170 : && (rdesc = jfunc_rdesc_usable (jfunc))
3935 : 318 : && --rdesc->refcount == 0)
3936 : : {
3937 : 106 : symtab_node *symbol = symtab_node_for_jfunc (jfunc);
3938 : 106 : if (!symbol)
3939 : : return false;
3940 : :
3941 : 106 : return remove_described_reference (symbol, rdesc);
3942 : : }
3943 : : return true;
3944 : : }
3945 : :
3946 : : /* Try to find a destination for indirect edge IE that corresponds to a simple
3947 : : call or a call of a member function pointer and where the destination is a
3948 : : pointer formal parameter described by jump function JFUNC. TARGET_TYPE is
3949 : : the type of the parameter to which the result of JFUNC is passed. If it can
3950 : : be determined, return the newly direct edge, otherwise return NULL.
3951 : : NEW_ROOT and NEW_ROOT_INFO is the node and its info that JFUNC lattices are
3952 : : relative to. */
3953 : :
3954 : : static struct cgraph_edge *
3955 : 9492 : try_make_edge_direct_simple_call (struct cgraph_edge *ie,
3956 : : struct ipa_jump_func *jfunc, tree target_type,
3957 : : struct cgraph_node *new_root,
3958 : : class ipa_node_params *new_root_info)
3959 : : {
3960 : 9492 : struct cgraph_edge *cs;
3961 : 9492 : tree target = NULL_TREE;
3962 : 9492 : bool agg_contents = ie->indirect_info->agg_contents;
3963 : 9492 : tree scalar = ipa_value_from_jfunc (new_root_info, jfunc, target_type);
3964 : 9492 : if (agg_contents)
3965 : : {
3966 : 8499 : if (scalar)
3967 : 7 : target = ipa_find_agg_cst_from_init (scalar, ie->indirect_info->offset,
3968 : 7 : ie->indirect_info->by_ref);
3969 : 8499 : if (!target && ie->indirect_info->guaranteed_unmodified)
3970 : 7645 : target = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
3971 : : new_root,
3972 : : ie->indirect_info->offset,
3973 : : ie->indirect_info->by_ref);
3974 : : }
3975 : : else
3976 : : target = scalar;
3977 : 9491 : if (!target)
3978 : : return NULL;
3979 : 497 : cs = ipa_make_edge_direct_to_target (ie, target);
3980 : :
3981 : 497 : if (cs && !agg_contents)
3982 : : {
3983 : 170 : bool ok;
3984 : 170 : gcc_checking_assert (cs->callee
3985 : : && (cs != ie
3986 : : || jfunc->type != IPA_JF_CONST
3987 : : || !symtab_node_for_jfunc (jfunc)
3988 : : || cs->callee == symtab_node_for_jfunc (jfunc)));
3989 : 170 : ok = try_decrement_rdesc_refcount (jfunc);
3990 : 170 : gcc_checking_assert (ok);
3991 : : }
3992 : :
3993 : : return cs;
3994 : : }
3995 : :
3996 : : /* Return the target to be used in cases of impossible devirtualization. IE
3997 : : and target (the latter can be NULL) are dumped when dumping is enabled. */
3998 : :
3999 : : tree
4000 : 468 : ipa_impossible_devirt_target (struct cgraph_edge *ie, tree target)
4001 : : {
4002 : 468 : if (dump_file)
4003 : : {
4004 : 33 : if (target)
4005 : 18 : fprintf (dump_file,
4006 : : "Type inconsistent devirtualization: %s->%s\n",
4007 : 18 : ie->caller->dump_name (),
4008 : 18 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
4009 : : else
4010 : 15 : fprintf (dump_file,
4011 : : "No devirtualization target in %s\n",
4012 : 15 : ie->caller->dump_name ());
4013 : : }
4014 : 468 : tree new_target = builtin_decl_unreachable ();
4015 : 468 : cgraph_node::get_create (new_target);
4016 : 468 : return new_target;
4017 : : }
4018 : :
4019 : : /* Try to find a destination for indirect edge IE that corresponds to a virtual
4020 : : call based on a formal parameter which is described by jump function JFUNC
4021 : : and if it can be determined, make it direct and return the direct edge.
4022 : : Otherwise, return NULL. CTX describes the polymorphic context that the
4023 : : parameter the call is based on brings along with it. NEW_ROOT and
4024 : : NEW_ROOT_INFO is the node and its info that JFUNC lattices are relative
4025 : : to. */
4026 : :
4027 : : static struct cgraph_edge *
4028 : 18029 : try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
4029 : : struct ipa_jump_func *jfunc,
4030 : : class ipa_polymorphic_call_context ctx,
4031 : : struct cgraph_node *new_root,
4032 : : class ipa_node_params *new_root_info)
4033 : : {
4034 : 18029 : tree target = NULL;
4035 : 18029 : bool speculative = false;
4036 : :
4037 : 18029 : if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
4038 : : return NULL;
4039 : :
4040 : 18029 : gcc_assert (!ie->indirect_info->by_ref);
4041 : :
4042 : : /* Try to do lookup via known virtual table pointer value. */
4043 : 18029 : if (!ie->indirect_info->vptr_changed
4044 : 18029 : || opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively))
4045 : : {
4046 : 18029 : tree vtable;
4047 : 18029 : unsigned HOST_WIDE_INT offset;
4048 : 18029 : tree t = NULL_TREE;
4049 : 18029 : if (jfunc->type == IPA_JF_CONST)
4050 : 316 : t = ipa_find_agg_cst_from_init (ipa_get_jf_constant (jfunc),
4051 : : ie->indirect_info->offset, true);
4052 : 316 : if (!t)
4053 : 18026 : t = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
4054 : : new_root,
4055 : 18026 : ie->indirect_info->offset, true);
4056 : 18029 : if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset))
4057 : : {
4058 : 416 : bool can_refer;
4059 : 416 : t = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
4060 : : vtable, offset, &can_refer);
4061 : 416 : if (can_refer)
4062 : : {
4063 : 399 : if (!t
4064 : 399 : || fndecl_built_in_p (t, BUILT_IN_UNREACHABLE,
4065 : : BUILT_IN_UNREACHABLE_TRAP)
4066 : 750 : || !possible_polymorphic_call_target_p
4067 : 351 : (ie, cgraph_node::get (t)))
4068 : : {
4069 : : /* Do not speculate builtin_unreachable, it is stupid! */
4070 : 88 : if (!ie->indirect_info->vptr_changed)
4071 : 88 : target = ipa_impossible_devirt_target (ie, target);
4072 : : else
4073 : : target = NULL;
4074 : : }
4075 : : else
4076 : : {
4077 : 311 : target = t;
4078 : 311 : speculative = ie->indirect_info->vptr_changed;
4079 : : }
4080 : : }
4081 : : }
4082 : : }
4083 : :
4084 : 18029 : ipa_polymorphic_call_context ie_context (ie);
4085 : 18029 : vec <cgraph_node *>targets;
4086 : 18029 : bool final;
4087 : :
4088 : 18029 : ctx.offset_by (ie->indirect_info->offset);
4089 : 18029 : if (ie->indirect_info->vptr_changed)
4090 : 7417 : ctx.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
4091 : : ie->indirect_info->otr_type);
4092 : 18029 : ctx.combine_with (ie_context, ie->indirect_info->otr_type);
4093 : 18029 : targets = possible_polymorphic_call_targets
4094 : 36058 : (ie->indirect_info->otr_type,
4095 : 18029 : ie->indirect_info->otr_token,
4096 : : ctx, &final);
4097 : 19559 : if (final && targets.length () <= 1)
4098 : : {
4099 : 1464 : speculative = false;
4100 : 1464 : if (targets.length () == 1)
4101 : 1413 : target = targets[0]->decl;
4102 : : else
4103 : 51 : target = ipa_impossible_devirt_target (ie, NULL_TREE);
4104 : : }
4105 : 16529 : else if (!target && opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
4106 : 33021 : && !ie->speculative && ie->maybe_hot_p ())
4107 : : {
4108 : 12335 : cgraph_node *n;
4109 : 24670 : n = try_speculative_devirtualization (ie->indirect_info->otr_type,
4110 : : ie->indirect_info->otr_token,
4111 : 12335 : ie->indirect_info->context);
4112 : 12335 : if (n)
4113 : : {
4114 : 5 : target = n->decl;
4115 : 5 : speculative = true;
4116 : : }
4117 : : }
4118 : :
4119 : 18029 : if (target)
4120 : : {
4121 : 1505 : if (!possible_polymorphic_call_target_p
4122 : 1505 : (ie, cgraph_node::get_create (target)))
4123 : : {
4124 : 48 : if (speculative)
4125 : : return NULL;
4126 : 48 : target = ipa_impossible_devirt_target (ie, target);
4127 : : }
4128 : 1505 : return ipa_make_edge_direct_to_target (ie, target, speculative);
4129 : : }
4130 : : else
4131 : : return NULL;
4132 : : }
4133 : :
4134 : : /* Update the param called notes associated with NODE when CS is being inlined,
4135 : : assuming NODE is (potentially indirectly) inlined into CS->callee.
4136 : : Moreover, if the callee is discovered to be constant, create a new cgraph
4137 : : edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
4138 : : unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
4139 : :
4140 : : static bool
4141 : 1359033 : update_indirect_edges_after_inlining (struct cgraph_edge *cs,
4142 : : struct cgraph_node *node,
4143 : : vec<cgraph_edge *> *new_edges)
4144 : : {
4145 : 1359033 : class ipa_edge_args *top;
4146 : 1359033 : struct cgraph_edge *ie, *next_ie, *new_direct_edge;
4147 : 1359033 : struct cgraph_node *new_root;
4148 : 1359033 : class ipa_node_params *new_root_info, *inlined_node_info;
4149 : 1359033 : bool res = false;
4150 : :
4151 : 1359033 : ipa_check_create_edge_args ();
4152 : 1359033 : top = ipa_edge_args_sum->get (cs);
4153 : 2718066 : new_root = cs->caller->inlined_to
4154 : 1359033 : ? cs->caller->inlined_to : cs->caller;
4155 : 1359033 : new_root_info = ipa_node_params_sum->get (new_root);
4156 : 1359033 : inlined_node_info = ipa_node_params_sum->get (cs->callee->function_symbol ());
4157 : :
4158 : 1425999 : for (ie = node->indirect_calls; ie; ie = next_ie)
4159 : : {
4160 : 66966 : class cgraph_indirect_call_info *ici = ie->indirect_info;
4161 : 66966 : struct ipa_jump_func *jfunc;
4162 : 66966 : int param_index;
4163 : :
4164 : 66966 : next_ie = ie->next_callee;
4165 : :
4166 : 66966 : if (ici->param_index == -1)
4167 : 80460 : continue;
4168 : :
4169 : : /* We must check range due to calls with variable number of arguments: */
4170 : 55461 : if (!top || ici->param_index >= ipa_get_cs_argument_count (top))
4171 : : {
4172 : 35 : ici->param_index = -1;
4173 : 35 : continue;
4174 : : }
4175 : :
4176 : 27712 : param_index = ici->param_index;
4177 : 27712 : jfunc = ipa_get_ith_jump_func (top, param_index);
4178 : :
4179 : 27712 : auto_vec<cgraph_node *, 4> spec_targets;
4180 : 27712 : if (ie->speculative)
4181 : 4039 : for (cgraph_edge *direct = ie->first_speculative_call_target ();
4182 : 8080 : direct;
4183 : 4041 : direct = direct->next_speculative_call_target ())
4184 : 4041 : spec_targets.safe_push (direct->callee);
4185 : :
4186 : 27712 : if (!opt_for_fn (node->decl, flag_indirect_inlining))
4187 : 191 : new_direct_edge = NULL;
4188 : 27521 : else if (ici->polymorphic)
4189 : : {
4190 : 18029 : ipa_polymorphic_call_context ctx;
4191 : 18029 : ctx = ipa_context_from_jfunc (new_root_info, cs, param_index, jfunc);
4192 : 18029 : new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc, ctx,
4193 : : new_root,
4194 : : new_root_info);
4195 : : }
4196 : : else
4197 : : {
4198 : 9492 : tree target_type = ipa_get_type (inlined_node_info, param_index);
4199 : 9492 : new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
4200 : : target_type,
4201 : : new_root,
4202 : : new_root_info);
4203 : : }
4204 : :
4205 : : /* If speculation was removed, then we need to do nothing. */
4206 : 1998 : if (new_direct_edge && new_direct_edge != ie
4207 : 27960 : && spec_targets.contains (new_direct_edge->callee))
4208 : : {
4209 : 237 : new_direct_edge->indirect_inlining_edge = 1;
4210 : 237 : res = true;
4211 : 237 : if (!new_direct_edge->speculative)
4212 : 237 : continue;
4213 : : }
4214 : 27475 : else if (new_direct_edge)
4215 : : {
4216 : 1761 : new_direct_edge->indirect_inlining_edge = 1;
4217 : 1761 : if (new_edges)
4218 : : {
4219 : 1721 : new_edges->safe_push (new_direct_edge);
4220 : 1721 : res = true;
4221 : : }
4222 : : /* If speculative edge was introduced we still need to update
4223 : : call info of the indirect edge. */
4224 : 1761 : if (!new_direct_edge->speculative)
4225 : 1750 : continue;
4226 : : }
4227 : 25725 : if (jfunc->type == IPA_JF_PASS_THROUGH
4228 : 25725 : && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4229 : : {
4230 : 5481 : if (ici->agg_contents
4231 : 1509 : && !ipa_get_jf_pass_through_agg_preserved (jfunc)
4232 : 6473 : && !ici->polymorphic)
4233 : 992 : ici->param_index = -1;
4234 : : else
4235 : : {
4236 : 4489 : ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
4237 : 4489 : if (ici->polymorphic
4238 : 4489 : && !ipa_get_jf_pass_through_type_preserved (jfunc))
4239 : 1935 : ici->vptr_changed = true;
4240 : 4489 : ipa_set_param_used_by_indirect_call (new_root_info,
4241 : : ici->param_index, true);
4242 : 4489 : if (ici->polymorphic)
4243 : 3182 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4244 : : ici->param_index, true);
4245 : : }
4246 : : }
4247 : 20244 : else if (jfunc->type == IPA_JF_ANCESTOR)
4248 : : {
4249 : 339 : if (ici->agg_contents
4250 : 107 : && !ipa_get_jf_ancestor_agg_preserved (jfunc)
4251 : 368 : && !ici->polymorphic)
4252 : 29 : ici->param_index = -1;
4253 : : else
4254 : : {
4255 : 310 : ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
4256 : 310 : ici->offset += ipa_get_jf_ancestor_offset (jfunc);
4257 : 310 : if (ici->polymorphic
4258 : 310 : && !ipa_get_jf_ancestor_type_preserved (jfunc))
4259 : 197 : ici->vptr_changed = true;
4260 : 310 : ipa_set_param_used_by_indirect_call (new_root_info,
4261 : : ici->param_index, true);
4262 : 310 : if (ici->polymorphic)
4263 : 232 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4264 : : ici->param_index, true);
4265 : : }
4266 : : }
4267 : : else
4268 : : /* Either we can find a destination for this edge now or never. */
4269 : 19905 : ici->param_index = -1;
4270 : 27712 : }
4271 : :
4272 : 1359033 : return res;
4273 : : }
4274 : :
4275 : : /* Recursively traverse subtree of NODE (including node) made of inlined
4276 : : cgraph_edges when CS has been inlined and invoke
4277 : : update_indirect_edges_after_inlining on all nodes and
4278 : : update_jump_functions_after_inlining on all non-inlined edges that lead out
4279 : : of this subtree. Newly discovered indirect edges will be added to
4280 : : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
4281 : : created. */
4282 : :
4283 : : static bool
4284 : 1359033 : propagate_info_to_inlined_callees (struct cgraph_edge *cs,
4285 : : struct cgraph_node *node,
4286 : : vec<cgraph_edge *> *new_edges)
4287 : : {
4288 : 1359033 : struct cgraph_edge *e;
4289 : 1359033 : bool res;
4290 : :
4291 : 1359033 : res = update_indirect_edges_after_inlining (cs, node, new_edges);
4292 : :
4293 : 4049637 : for (e = node->callees; e; e = e->next_callee)
4294 : 2690604 : if (!e->inline_failed)
4295 : 535300 : res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
4296 : : else
4297 : 2155304 : update_jump_functions_after_inlining (cs, e);
4298 : 1424012 : for (e = node->indirect_calls; e; e = e->next_callee)
4299 : 64979 : update_jump_functions_after_inlining (cs, e);
4300 : :
4301 : 1359033 : return res;
4302 : : }
4303 : :
4304 : : /* Combine two controlled uses counts as done during inlining. */
4305 : :
4306 : : static int
4307 : 346379 : combine_controlled_uses_counters (int c, int d)
4308 : : {
4309 : 0 : if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
4310 : : return IPA_UNDESCRIBED_USE;
4311 : : else
4312 : 96923 : return c + d - 1;
4313 : : }
4314 : :
4315 : : /* Propagate number of controlled users from CS->caleee to the new root of the
4316 : : tree of inlined nodes. */
4317 : :
4318 : : static void
4319 : 823733 : propagate_controlled_uses (struct cgraph_edge *cs)
4320 : : {
4321 : 823733 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4322 : 823733 : if (!args)
4323 : : return;
4324 : 1644314 : struct cgraph_node *new_root = cs->caller->inlined_to
4325 : 822157 : ? cs->caller->inlined_to : cs->caller;
4326 : 822157 : ipa_node_params *new_root_info = ipa_node_params_sum->get (new_root);
4327 : 822157 : ipa_node_params *old_root_info = ipa_node_params_sum->get (cs->callee);
4328 : 822157 : int count, i;
4329 : :
4330 : 822157 : if (!old_root_info)
4331 : : return;
4332 : :
4333 : 1578946 : count = MIN (ipa_get_cs_argument_count (args),
4334 : : ipa_get_param_count (old_root_info));
4335 : 2426200 : for (i = 0; i < count; i++)
4336 : : {
4337 : 1604123 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4338 : 1604123 : struct ipa_cst_ref_desc *rdesc;
4339 : :
4340 : 1604123 : if (jf->type == IPA_JF_PASS_THROUGH
4341 : 1604123 : && !ipa_get_jf_pass_through_refdesc_decremented (jf))
4342 : : {
4343 : 300251 : int src_idx, c, d;
4344 : 300251 : src_idx = ipa_get_jf_pass_through_formal_id (jf);
4345 : 300251 : c = ipa_get_controlled_uses (new_root_info, src_idx);
4346 : 300251 : d = ipa_get_controlled_uses (old_root_info, i);
4347 : :
4348 : 300251 : gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
4349 : : == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
4350 : 300251 : c = combine_controlled_uses_counters (c, d);
4351 : 300251 : ipa_set_controlled_uses (new_root_info, src_idx, c);
4352 : 300251 : bool lderef = true;
4353 : 300251 : if (c != IPA_UNDESCRIBED_USE)
4354 : : {
4355 : 88162 : lderef = (ipa_get_param_load_dereferenced (new_root_info, src_idx)
4356 : 88162 : || ipa_get_param_load_dereferenced (old_root_info, i));
4357 : 88162 : ipa_set_param_load_dereferenced (new_root_info, src_idx, lderef);
4358 : : }
4359 : :
4360 : 300251 : if (c == 0 && !lderef && new_root_info->ipcp_orig_node)
4361 : : {
4362 : 185 : struct cgraph_node *n;
4363 : 185 : struct ipa_ref *ref;
4364 : 185 : tree t = new_root_info->known_csts[src_idx];
4365 : :
4366 : 165 : if (t && TREE_CODE (t) == ADDR_EXPR
4367 : 148 : && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
4368 : 4 : && (n = cgraph_node::get (TREE_OPERAND (t, 0)))
4369 : 189 : && (ref = new_root->find_reference (n, NULL, 0,
4370 : : IPA_REF_ADDR)))
4371 : : {
4372 : 4 : if (dump_file)
4373 : 1 : fprintf (dump_file, "ipa-prop: Removing cloning-created "
4374 : : "reference from %s to %s.\n",
4375 : : new_root->dump_name (),
4376 : : n->dump_name ());
4377 : 4 : ref->remove_reference ();
4378 : : }
4379 : : }
4380 : : }
4381 : 1303872 : else if (jf->type == IPA_JF_CONST
4382 : 1303872 : && (rdesc = jfunc_rdesc_usable (jf)))
4383 : : {
4384 : 46128 : int d = ipa_get_controlled_uses (old_root_info, i);
4385 : 46128 : int c = rdesc->refcount;
4386 : 46128 : tree cst = ipa_get_jf_constant (jf);
4387 : 46128 : rdesc->refcount = combine_controlled_uses_counters (c, d);
4388 : 46128 : if (rdesc->refcount != IPA_UNDESCRIBED_USE
4389 : 8761 : && ipa_get_param_load_dereferenced (old_root_info, i)
4390 : 1815 : && TREE_CODE (cst) == ADDR_EXPR
4391 : 47943 : && VAR_P (TREE_OPERAND (cst, 0)))
4392 : : {
4393 : 1814 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4394 : 1814 : new_root->create_reference (n, IPA_REF_LOAD, NULL);
4395 : 1814 : if (dump_file)
4396 : 3 : fprintf (dump_file, "ipa-prop: Address IPA constant will reach "
4397 : : "a load so adding LOAD reference from %s to %s.\n",
4398 : : new_root->dump_name (), n->dump_name ());
4399 : : }
4400 : 46128 : if (rdesc->refcount == 0)
4401 : : {
4402 : 6498 : gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
4403 : : && ((TREE_CODE (TREE_OPERAND (cst, 0))
4404 : : == FUNCTION_DECL)
4405 : : || VAR_P (TREE_OPERAND (cst, 0))));
4406 : :
4407 : 6498 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4408 : 6498 : if (n)
4409 : : {
4410 : 6498 : remove_described_reference (n, rdesc);
4411 : 6498 : cgraph_node *clone = cs->caller;
4412 : 6498 : while (clone->inlined_to
4413 : 1423 : && clone->ipcp_clone
4414 : 6705 : && clone != rdesc->cs->caller)
4415 : : {
4416 : 82 : struct ipa_ref *ref;
4417 : 82 : ref = clone->find_reference (n, NULL, 0, IPA_REF_ADDR);
4418 : 82 : if (ref)
4419 : : {
4420 : 79 : if (dump_file)
4421 : 1 : fprintf (dump_file, "ipa-prop: Removing "
4422 : : "cloning-created reference "
4423 : : "from %s to %s.\n",
4424 : : clone->dump_name (),
4425 : : n->dump_name ());
4426 : 79 : ref->remove_reference ();
4427 : : }
4428 : 82 : clone = clone->callers->caller;
4429 : : }
4430 : : }
4431 : : }
4432 : : }
4433 : : }
4434 : :
4435 : 1579038 : for (i = ipa_get_param_count (old_root_info);
4436 : 1579588 : i < ipa_get_cs_argument_count (args);
4437 : : i++)
4438 : : {
4439 : 321 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4440 : :
4441 : 321 : if (jf->type == IPA_JF_CONST)
4442 : : {
4443 : 288 : struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
4444 : 288 : if (rdesc)
4445 : 232 : rdesc->refcount = IPA_UNDESCRIBED_USE;
4446 : : }
4447 : 33 : else if (jf->type == IPA_JF_PASS_THROUGH)
4448 : 6 : ipa_set_controlled_uses (new_root_info,
4449 : : jf->value.pass_through.formal_id,
4450 : : IPA_UNDESCRIBED_USE);
4451 : : }
4452 : : }
4453 : :
4454 : : /* Update jump functions and call note functions on inlining the call site CS.
4455 : : CS is expected to lead to a node already cloned by
4456 : : cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
4457 : : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
4458 : : created. */
4459 : :
4460 : : bool
4461 : 3552849 : ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
4462 : : vec<cgraph_edge *> *new_edges)
4463 : : {
4464 : 3552849 : bool changed;
4465 : : /* Do nothing if the preparation phase has not been carried out yet
4466 : : (i.e. during early inlining). */
4467 : 3552849 : if (!ipa_node_params_sum)
4468 : : return false;
4469 : 823733 : gcc_assert (ipa_edge_args_sum);
4470 : :
4471 : 823733 : propagate_controlled_uses (cs);
4472 : 823733 : changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
4473 : 823733 : ipa_node_params_sum->remove (cs->callee);
4474 : :
4475 : 823733 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4476 : 823733 : if (args)
4477 : : {
4478 : 822157 : bool ok = true;
4479 : 822157 : if (args->jump_functions)
4480 : : {
4481 : : struct ipa_jump_func *jf;
4482 : : int i;
4483 : 2200327 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
4484 : 1492795 : if (jf->type == IPA_JF_CONST
4485 : 1492795 : && ipa_get_jf_constant_rdesc (jf))
4486 : : {
4487 : : ok = false;
4488 : : break;
4489 : : }
4490 : : }
4491 : 756949 : if (ok)
4492 : 772740 : ipa_edge_args_sum->remove (cs);
4493 : : }
4494 : 823733 : if (ipcp_transformation_sum)
4495 : 572862 : ipcp_transformation_sum->remove (cs->callee);
4496 : :
4497 : : return changed;
4498 : : }
4499 : :
4500 : : /* Ensure that array of edge arguments infos is big enough to accommodate a
4501 : : structure for all edges and reallocates it if not. Also, allocate
4502 : : associated hash tables is they do not already exist. */
4503 : :
4504 : : void
4505 : 4428946 : ipa_check_create_edge_args (void)
4506 : : {
4507 : 4428946 : if (!ipa_edge_args_sum)
4508 : 240403 : ipa_edge_args_sum
4509 : 240403 : = (new (ggc_alloc_no_dtor<ipa_edge_args_sum_t> ())
4510 : 240403 : ipa_edge_args_sum_t (symtab, true));
4511 : 4428946 : if (!ipa_vr_hash_table)
4512 : 170431 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4513 : 4428946 : }
4514 : :
4515 : : /* Free all ipa_edge structures. */
4516 : :
4517 : : void
4518 : 454244 : ipa_free_all_edge_args (void)
4519 : : {
4520 : 454244 : if (!ipa_edge_args_sum)
4521 : : return;
4522 : :
4523 : 226853 : ggc_delete (ipa_edge_args_sum);
4524 : 226853 : ipa_edge_args_sum = NULL;
4525 : : }
4526 : :
4527 : : /* Free all ipa_node_params structures. */
4528 : :
4529 : : void
4530 : 5117910 : ipa_free_all_node_params (void)
4531 : : {
4532 : 5117910 : if (ipa_node_params_sum)
4533 : 4890519 : ggc_delete (ipa_node_params_sum);
4534 : 5117910 : ipa_node_params_sum = NULL;
4535 : 5117910 : }
4536 : :
4537 : : /* Initialize IPA CP transformation summary and also allocate any necessary hash
4538 : : tables if they do not already exist. */
4539 : :
4540 : : void
4541 : 83312 : ipcp_transformation_initialize (void)
4542 : : {
4543 : 83312 : if (!ipa_vr_hash_table)
4544 : 1819 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4545 : 83312 : if (ipcp_transformation_sum == NULL)
4546 : : {
4547 : 18331 : ipcp_transformation_sum = ipcp_transformation_t::create_ggc (symtab);
4548 : 18331 : ipcp_transformation_sum->disable_insertion_hook ();
4549 : : }
4550 : 83312 : }
4551 : :
4552 : : /* Release the IPA CP transformation summary. */
4553 : :
4554 : : void
4555 : 254814 : ipcp_free_transformation_sum (void)
4556 : : {
4557 : 254814 : if (!ipcp_transformation_sum)
4558 : : return;
4559 : :
4560 : 18322 : ipcp_transformation_sum->~function_summary<ipcp_transformation *> ();
4561 : 18322 : ggc_free (ipcp_transformation_sum);
4562 : 18322 : ipcp_transformation_sum = NULL;
4563 : : }
4564 : :
4565 : : /* Set the aggregate replacements of NODE to be AGGVALS. */
4566 : :
4567 : : void
4568 : 17641 : ipa_set_node_agg_value_chain (struct cgraph_node *node,
4569 : : vec<ipa_argagg_value, va_gc> *aggs)
4570 : : {
4571 : 17641 : ipcp_transformation_initialize ();
4572 : 17641 : ipcp_transformation *s = ipcp_transformation_sum->get_create (node);
4573 : 17641 : s->m_agg_values = aggs;
4574 : 17641 : }
4575 : :
4576 : : /* Hook that is called by cgraph.cc when an edge is removed. Adjust reference
4577 : : count data structures accordingly. */
4578 : :
4579 : : void
4580 : 0 : ipa_edge_args_sum_t::remove (cgraph_edge *cs, ipa_edge_args *args)
4581 : : {
4582 : 0 : if (args->jump_functions)
4583 : : {
4584 : : struct ipa_jump_func *jf;
4585 : : int i;
4586 : 0 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
4587 : : {
4588 : 0 : struct ipa_cst_ref_desc *rdesc;
4589 : 0 : try_decrement_rdesc_refcount (jf);
4590 : 0 : if (jf->type == IPA_JF_CONST
4591 : 0 : && (rdesc = ipa_get_jf_constant_rdesc (jf))
4592 : 0 : && rdesc->cs == cs)
4593 : 0 : rdesc->cs = NULL;
4594 : : }
4595 : : }
4596 : 0 : }
4597 : :
4598 : : /* Copy information from SRC_JF to DST_JF which correstpond to call graph edges
4599 : : SRC and DST. */
4600 : :
4601 : : static void
4602 : 2291043 : ipa_duplicate_jump_function (cgraph_edge *src, cgraph_edge *dst,
4603 : : ipa_jump_func *src_jf, ipa_jump_func *dst_jf)
4604 : : {
4605 : 2291043 : dst_jf->agg.items = vec_safe_copy (src_jf->agg.items);
4606 : 2291043 : dst_jf->agg.by_ref = src_jf->agg.by_ref;
4607 : :
4608 : : /* We can avoid calling ipa_set_jfunc_vr since it would only look up the
4609 : : place in the hash_table where the source m_vr resides. */
4610 : 2291043 : dst_jf->m_vr = src_jf->m_vr;
4611 : :
4612 : 2291043 : if (src_jf->type == IPA_JF_CONST)
4613 : : {
4614 : 804410 : ipa_set_jf_cst_copy (dst_jf, src_jf);
4615 : 804410 : struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
4616 : :
4617 : 804410 : if (!src_rdesc)
4618 : 675318 : dst_jf->value.constant.rdesc = NULL;
4619 : 129092 : else if (src->caller == dst->caller)
4620 : : {
4621 : : /* Creation of a speculative edge. If the source edge is the one
4622 : : grabbing a reference, we must create a new (duplicate)
4623 : : reference description. Otherwise they refer to the same
4624 : : description corresponding to a reference taken in a function
4625 : : src->caller is inlined to. In that case we just must
4626 : : increment the refcount. */
4627 : 16 : if (src_rdesc->cs == src)
4628 : : {
4629 : 16 : symtab_node *n = symtab_node_for_jfunc (src_jf);
4630 : 16 : gcc_checking_assert (n);
4631 : 16 : ipa_ref *ref
4632 : 16 : = src->caller->find_reference (n, src->call_stmt,
4633 : : src->lto_stmt_uid,
4634 : : IPA_REF_ADDR);
4635 : 16 : gcc_checking_assert (ref);
4636 : 16 : dst->caller->clone_reference (ref, ref->stmt);
4637 : :
4638 : 16 : ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
4639 : 16 : dst_rdesc->cs = dst;
4640 : 16 : dst_rdesc->refcount = src_rdesc->refcount;
4641 : 16 : dst_rdesc->next_duplicate = NULL;
4642 : 16 : dst_jf->value.constant.rdesc = dst_rdesc;
4643 : : }
4644 : : else
4645 : : {
4646 : 0 : src_rdesc->refcount++;
4647 : 0 : dst_jf->value.constant.rdesc = src_rdesc;
4648 : : }
4649 : : }
4650 : 129076 : else if (src_rdesc->cs == src)
4651 : : {
4652 : 128688 : struct ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
4653 : 128688 : dst_rdesc->cs = dst;
4654 : 128688 : dst_rdesc->refcount = src_rdesc->refcount;
4655 : 128688 : dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
4656 : 128688 : src_rdesc->next_duplicate = dst_rdesc;
4657 : 128688 : dst_jf->value.constant.rdesc = dst_rdesc;
4658 : : }
4659 : : else
4660 : : {
4661 : 388 : struct ipa_cst_ref_desc *dst_rdesc;
4662 : : /* This can happen during inlining, when a JFUNC can refer to a
4663 : : reference taken in a function up in the tree of inline clones.
4664 : : We need to find the duplicate that refers to our tree of
4665 : : inline clones. */
4666 : :
4667 : 388 : gcc_assert (dst->caller->inlined_to);
4668 : 388 : for (dst_rdesc = src_rdesc->next_duplicate;
4669 : 388 : dst_rdesc;
4670 : 0 : dst_rdesc = dst_rdesc->next_duplicate)
4671 : : {
4672 : 388 : struct cgraph_node *top;
4673 : 776 : top = dst_rdesc->cs->caller->inlined_to
4674 : 388 : ? dst_rdesc->cs->caller->inlined_to
4675 : : : dst_rdesc->cs->caller;
4676 : 388 : if (dst->caller->inlined_to == top)
4677 : : break;
4678 : : }
4679 : 388 : gcc_assert (dst_rdesc);
4680 : 388 : dst_jf->value.constant.rdesc = dst_rdesc;
4681 : : }
4682 : : }
4683 : 1486633 : else if (src_jf->type == IPA_JF_PASS_THROUGH)
4684 : : {
4685 : 557328 : dst_jf->type = IPA_JF_PASS_THROUGH;
4686 : 557328 : dst_jf->value.pass_through = src_jf->value.pass_through;
4687 : 557328 : if (src->caller == dst->caller)
4688 : : {
4689 : 9144 : struct cgraph_node *inline_root = dst->caller->inlined_to
4690 : 4572 : ? dst->caller->inlined_to : dst->caller;
4691 : 4572 : ipa_node_params *root_info = ipa_node_params_sum->get (inline_root);
4692 : 4572 : int idx = ipa_get_jf_pass_through_formal_id (dst_jf);
4693 : :
4694 : 4572 : int c = ipa_get_controlled_uses (root_info, idx);
4695 : 4572 : if (c != IPA_UNDESCRIBED_USE)
4696 : : {
4697 : 693 : c++;
4698 : 693 : ipa_set_controlled_uses (root_info, idx, c);
4699 : : }
4700 : : }
4701 : : }
4702 : 929305 : else if (src_jf->type == IPA_JF_ANCESTOR)
4703 : : {
4704 : 91404 : dst_jf->type = IPA_JF_ANCESTOR;
4705 : 91404 : dst_jf->value.ancestor = src_jf->value.ancestor;
4706 : : }
4707 : : else
4708 : 837901 : gcc_assert (src_jf->type == IPA_JF_UNKNOWN);
4709 : 2291043 : }
4710 : :
4711 : : /* Method invoked when an edge is duplicated. Copy ipa_edge_args and adjust
4712 : : reference count data strucutres accordingly. */
4713 : :
4714 : : void
4715 : 1019848 : ipa_edge_args_sum_t::duplicate (cgraph_edge *src, cgraph_edge *dst,
4716 : : ipa_edge_args *old_args, ipa_edge_args *new_args)
4717 : : {
4718 : 1019848 : unsigned int i;
4719 : :
4720 : 1019848 : if (old_args->polymorphic_call_contexts)
4721 : 111457 : new_args->polymorphic_call_contexts
4722 : 111457 : = vec_safe_copy (old_args->polymorphic_call_contexts);
4723 : :
4724 : 1019848 : if (!vec_safe_length (old_args->jump_functions))
4725 : : {
4726 : 39530 : new_args->jump_functions = NULL;
4727 : 39530 : return;
4728 : : }
4729 : 980318 : vec_safe_grow_cleared (new_args->jump_functions,
4730 : 980318 : old_args->jump_functions->length (), true);
4731 : :
4732 : 3271361 : for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
4733 : : {
4734 : 2291043 : struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
4735 : 2291043 : struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
4736 : :
4737 : 2291043 : ipa_duplicate_jump_function (src, dst, src_jf, dst_jf);
4738 : : }
4739 : : }
4740 : :
4741 : : /* Analyze newly added function into callgraph. */
4742 : :
4743 : : static void
4744 : 38067 : ipa_add_new_function (cgraph_node *node, void *data ATTRIBUTE_UNUSED)
4745 : : {
4746 : 38067 : if (node->has_gimple_body_p ())
4747 : 38067 : ipa_analyze_node (node);
4748 : 38067 : }
4749 : :
4750 : : /* Hook that is called by summary when a node is duplicated. */
4751 : :
4752 : : void
4753 : 660367 : ipa_node_params_t::duplicate(cgraph_node *, cgraph_node *,
4754 : : ipa_node_params *old_info,
4755 : : ipa_node_params *new_info)
4756 : : {
4757 : 660367 : new_info->descriptors = vec_safe_copy (old_info->descriptors);
4758 : 660367 : gcc_assert (new_info->lattices.is_empty ());
4759 : 660367 : new_info->ipcp_orig_node = old_info->ipcp_orig_node;
4760 : 660367 : new_info->known_csts = old_info->known_csts.copy ();
4761 : 660367 : new_info->known_contexts = old_info->known_contexts.copy ();
4762 : :
4763 : 660367 : new_info->analysis_done = old_info->analysis_done;
4764 : 660367 : new_info->node_enqueued = old_info->node_enqueued;
4765 : 660367 : new_info->versionable = old_info->versionable;
4766 : 660367 : }
4767 : :
4768 : : /* Duplication of ipcp transformation summaries. */
4769 : :
4770 : : void
4771 : 51188 : ipcp_transformation_t::duplicate(cgraph_node *, cgraph_node *dst,
4772 : : ipcp_transformation *src_trans,
4773 : : ipcp_transformation *dst_trans)
4774 : : {
4775 : : /* Avoid redundant work of duplicating vectors we will never use. */
4776 : 51188 : if (dst->inlined_to)
4777 : : return;
4778 : 7328 : dst_trans->m_agg_values = vec_safe_copy (src_trans->m_agg_values);
4779 : 13437 : dst_trans->m_vr = vec_safe_copy (src_trans->m_vr);
4780 : : }
4781 : :
4782 : : /* Register our cgraph hooks if they are not already there. */
4783 : :
4784 : : void
4785 : 374485 : ipa_register_cgraph_hooks (void)
4786 : : {
4787 : 374485 : ipa_check_create_node_params ();
4788 : 374485 : ipa_check_create_edge_args ();
4789 : :
4790 : 748970 : function_insertion_hook_holder =
4791 : 374485 : symtab->add_cgraph_insertion_hook (&ipa_add_new_function, NULL);
4792 : 374485 : }
4793 : :
4794 : : /* Unregister our cgraph hooks if they are not already there. */
4795 : :
4796 : : static void
4797 : 454244 : ipa_unregister_cgraph_hooks (void)
4798 : : {
4799 : 454244 : if (function_insertion_hook_holder)
4800 : 226853 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
4801 : 454244 : function_insertion_hook_holder = NULL;
4802 : 454244 : }
4803 : :
4804 : : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
4805 : : longer needed after ipa-cp. */
4806 : :
4807 : : void
4808 : 125714 : ipa_free_all_structures_after_ipa_cp (void)
4809 : : {
4810 : 125714 : if (!optimize && !in_lto_p)
4811 : : {
4812 : 0 : ipa_free_all_edge_args ();
4813 : 0 : ipa_free_all_node_params ();
4814 : 0 : ipcp_sources_pool.release ();
4815 : 0 : ipcp_cst_values_pool.release ();
4816 : 0 : ipcp_poly_ctx_values_pool.release ();
4817 : 0 : ipcp_agg_lattice_pool.release ();
4818 : 0 : ipa_unregister_cgraph_hooks ();
4819 : 0 : ipa_refdesc_pool.release ();
4820 : : }
4821 : 125714 : }
4822 : :
4823 : : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
4824 : : longer needed after indirect inlining. */
4825 : :
4826 : : void
4827 : 454244 : ipa_free_all_structures_after_iinln (void)
4828 : : {
4829 : 454244 : ipa_free_all_edge_args ();
4830 : 454244 : ipa_free_all_node_params ();
4831 : 454244 : ipa_unregister_cgraph_hooks ();
4832 : 454244 : ipcp_sources_pool.release ();
4833 : 454244 : ipcp_cst_values_pool.release ();
4834 : 454244 : ipcp_poly_ctx_values_pool.release ();
4835 : 454244 : ipcp_agg_lattice_pool.release ();
4836 : 454244 : ipa_refdesc_pool.release ();
4837 : 454244 : }
4838 : :
4839 : : /* Print ipa_tree_map data structures of all functions in the
4840 : : callgraph to F. */
4841 : :
4842 : : void
4843 : 214 : ipa_print_node_params (FILE *f, struct cgraph_node *node)
4844 : : {
4845 : 214 : int i, count;
4846 : 214 : class ipa_node_params *info;
4847 : :
4848 : 214 : if (!node->definition)
4849 : : return;
4850 : 167 : info = ipa_node_params_sum->get (node);
4851 : 167 : fprintf (f, " function %s parameter descriptors:\n", node->dump_name ());
4852 : 167 : if (!info)
4853 : : {
4854 : 0 : fprintf (f, " no params return\n");
4855 : 0 : return;
4856 : : }
4857 : 167 : count = ipa_get_param_count (info);
4858 : 334 : for (i = 0; i < count; i++)
4859 : : {
4860 : 167 : int c;
4861 : :
4862 : 167 : fprintf (f, " ");
4863 : 167 : ipa_dump_param (f, info, i);
4864 : 167 : if (ipa_is_param_used (info, i))
4865 : 161 : fprintf (f, " used");
4866 : 167 : if (ipa_is_param_used_by_ipa_predicates (info, i))
4867 : 100 : fprintf (f, " used_by_ipa_predicates");
4868 : 167 : if (ipa_is_param_used_by_indirect_call (info, i))
4869 : 10 : fprintf (f, " used_by_indirect_call");
4870 : 167 : if (ipa_is_param_used_by_polymorphic_call (info, i))
4871 : 0 : fprintf (f, " used_by_polymorphic_call");
4872 : 167 : c = ipa_get_controlled_uses (info, i);
4873 : 167 : if (c == IPA_UNDESCRIBED_USE)
4874 : 98 : fprintf (f, " undescribed_use");
4875 : : else
4876 : 119 : fprintf (f, " controlled_uses=%i %s", c,
4877 : 69 : ipa_get_param_load_dereferenced (info, i)
4878 : : ? "(load_dereferenced)" : "");
4879 : 167 : fprintf (f, "\n");
4880 : : }
4881 : : }
4882 : :
4883 : : /* Print ipa_tree_map data structures of all functions in the
4884 : : callgraph to F. */
4885 : :
4886 : : void
4887 : 43 : ipa_print_all_params (FILE * f)
4888 : : {
4889 : 43 : struct cgraph_node *node;
4890 : :
4891 : 43 : fprintf (f, "\nFunction parameters:\n");
4892 : 486 : FOR_EACH_FUNCTION (node)
4893 : 200 : ipa_print_node_params (f, node);
4894 : 43 : }
4895 : :
4896 : : /* Stream out jump function JUMP_FUNC to OB. */
4897 : :
4898 : : static void
4899 : 603087 : ipa_write_jump_function (struct output_block *ob,
4900 : : struct ipa_jump_func *jump_func)
4901 : : {
4902 : 603087 : struct ipa_agg_jf_item *item;
4903 : 603087 : struct bitpack_d bp;
4904 : 603087 : int i, count;
4905 : 603087 : int flag = 0;
4906 : :
4907 : : /* ADDR_EXPRs are very comon IP invariants; save some streamer data
4908 : : as well as WPA memory by handling them specially. */
4909 : 603087 : if (jump_func->type == IPA_JF_CONST
4910 : 462692 : && TREE_CODE (jump_func->value.constant.value) == ADDR_EXPR)
4911 : 603087 : flag = 1;
4912 : :
4913 : 603087 : streamer_write_uhwi (ob, jump_func->type * 2 + flag);
4914 : 603087 : switch (jump_func->type)
4915 : : {
4916 : : case IPA_JF_UNKNOWN:
4917 : : break;
4918 : 462692 : case IPA_JF_CONST:
4919 : 462692 : gcc_assert (
4920 : : EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
4921 : 462692 : stream_write_tree (ob,
4922 : : flag
4923 : : ? TREE_OPERAND (jump_func->value.constant.value, 0)
4924 : : : jump_func->value.constant.value, true);
4925 : 462692 : break;
4926 : 75996 : case IPA_JF_PASS_THROUGH:
4927 : 75996 : streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
4928 : 75996 : if (jump_func->value.pass_through.operation == NOP_EXPR)
4929 : : {
4930 : 75191 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4931 : 75191 : bp = bitpack_create (ob->main_stream);
4932 : 75191 : bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
4933 : 75191 : gcc_assert (!jump_func->value.pass_through.refdesc_decremented);
4934 : 75191 : streamer_write_bitpack (&bp);
4935 : : }
4936 : 805 : else if (TREE_CODE_CLASS (jump_func->value.pass_through.operation)
4937 : : == tcc_unary)
4938 : 39 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4939 : : else
4940 : : {
4941 : 766 : stream_write_tree (ob, jump_func->value.pass_through.operand, true);
4942 : 766 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4943 : : }
4944 : : break;
4945 : 1262 : case IPA_JF_ANCESTOR:
4946 : 1262 : streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
4947 : 1262 : streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
4948 : 1262 : bp = bitpack_create (ob->main_stream);
4949 : 1262 : bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
4950 : 1262 : bp_pack_value (&bp, jump_func->value.ancestor.keep_null, 1);
4951 : 1262 : streamer_write_bitpack (&bp);
4952 : 1262 : break;
4953 : 0 : default:
4954 : 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
4955 : : }
4956 : :
4957 : 603087 : count = vec_safe_length (jump_func->agg.items);
4958 : 603087 : streamer_write_uhwi (ob, count);
4959 : 603087 : if (count)
4960 : : {
4961 : 2999 : bp = bitpack_create (ob->main_stream);
4962 : 2999 : bp_pack_value (&bp, jump_func->agg.by_ref, 1);
4963 : 2999 : streamer_write_bitpack (&bp);
4964 : : }
4965 : :
4966 : 609180 : FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
4967 : : {
4968 : 6093 : stream_write_tree (ob, item->type, true);
4969 : 6093 : streamer_write_uhwi (ob, item->offset);
4970 : 6093 : streamer_write_uhwi (ob, item->jftype);
4971 : 6093 : switch (item->jftype)
4972 : : {
4973 : : case IPA_JF_UNKNOWN:
4974 : : break;
4975 : 5569 : case IPA_JF_CONST:
4976 : 5569 : stream_write_tree (ob, item->value.constant, true);
4977 : 5569 : break;
4978 : 524 : case IPA_JF_PASS_THROUGH:
4979 : 524 : case IPA_JF_LOAD_AGG:
4980 : 524 : streamer_write_uhwi (ob, item->value.pass_through.operation);
4981 : 524 : streamer_write_uhwi (ob, item->value.pass_through.formal_id);
4982 : 524 : if (TREE_CODE_CLASS (item->value.pass_through.operation)
4983 : : != tcc_unary)
4984 : 4 : stream_write_tree (ob, item->value.pass_through.operand, true);
4985 : 524 : if (item->jftype == IPA_JF_LOAD_AGG)
4986 : : {
4987 : 88 : stream_write_tree (ob, item->value.load_agg.type, true);
4988 : 88 : streamer_write_uhwi (ob, item->value.load_agg.offset);
4989 : 88 : bp = bitpack_create (ob->main_stream);
4990 : 88 : bp_pack_value (&bp, item->value.load_agg.by_ref, 1);
4991 : 88 : streamer_write_bitpack (&bp);
4992 : : }
4993 : : break;
4994 : 0 : default:
4995 : 0 : fatal_error (UNKNOWN_LOCATION,
4996 : : "invalid jump function in LTO stream");
4997 : : }
4998 : : }
4999 : :
5000 : 603087 : bp = bitpack_create (ob->main_stream);
5001 : 603087 : if (jump_func->m_vr)
5002 : 418790 : jump_func->m_vr->streamer_write (ob);
5003 : : else
5004 : : {
5005 : 184297 : bp_pack_value (&bp, false, 1);
5006 : 184297 : streamer_write_bitpack (&bp);
5007 : : }
5008 : 603087 : }
5009 : :
5010 : : /* Read in jump function JUMP_FUNC from IB. */
5011 : :
5012 : : static void
5013 : 565738 : ipa_read_jump_function (class lto_input_block *ib,
5014 : : struct ipa_jump_func *jump_func,
5015 : : struct cgraph_edge *cs,
5016 : : class data_in *data_in,
5017 : : bool prevails)
5018 : : {
5019 : 565738 : enum jump_func_type jftype;
5020 : 565738 : enum tree_code operation;
5021 : 565738 : int i, count;
5022 : 565738 : int val = streamer_read_uhwi (ib);
5023 : 565738 : bool flag = val & 1;
5024 : :
5025 : 565738 : jftype = (enum jump_func_type) (val / 2);
5026 : 565738 : switch (jftype)
5027 : : {
5028 : 48787 : case IPA_JF_UNKNOWN:
5029 : 48787 : ipa_set_jf_unknown (jump_func);
5030 : 48787 : break;
5031 : 446714 : case IPA_JF_CONST:
5032 : 446714 : {
5033 : 446714 : tree t = stream_read_tree (ib, data_in);
5034 : 446714 : if (flag && prevails)
5035 : 161678 : t = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
5036 : 446714 : ipa_set_jf_constant (jump_func, t, cs);
5037 : : }
5038 : 446714 : break;
5039 : 69625 : case IPA_JF_PASS_THROUGH:
5040 : 69625 : operation = (enum tree_code) streamer_read_uhwi (ib);
5041 : 69625 : if (operation == NOP_EXPR)
5042 : : {
5043 : 69084 : int formal_id = streamer_read_uhwi (ib);
5044 : 69084 : struct bitpack_d bp = streamer_read_bitpack (ib);
5045 : 69084 : bool agg_preserved = bp_unpack_value (&bp, 1);
5046 : 69084 : ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved);
5047 : : }
5048 : 541 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
5049 : : {
5050 : 19 : int formal_id = streamer_read_uhwi (ib);
5051 : 19 : ipa_set_jf_unary_pass_through (jump_func, formal_id, operation);
5052 : : }
5053 : : else
5054 : : {
5055 : 522 : tree operand = stream_read_tree (ib, data_in);
5056 : 522 : int formal_id = streamer_read_uhwi (ib);
5057 : 522 : ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
5058 : : operation);
5059 : : }
5060 : : break;
5061 : 612 : case IPA_JF_ANCESTOR:
5062 : 612 : {
5063 : 612 : HOST_WIDE_INT offset = streamer_read_uhwi (ib);
5064 : 612 : int formal_id = streamer_read_uhwi (ib);
5065 : 612 : struct bitpack_d bp = streamer_read_bitpack (ib);
5066 : 612 : bool agg_preserved = bp_unpack_value (&bp, 1);
5067 : 612 : bool keep_null = bp_unpack_value (&bp, 1);
5068 : 612 : ipa_set_ancestor_jf (jump_func, offset, formal_id, agg_preserved,
5069 : : keep_null);
5070 : 612 : break;
5071 : : }
5072 : 0 : default:
5073 : 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
5074 : : }
5075 : :
5076 : 565738 : count = streamer_read_uhwi (ib);
5077 : 565738 : if (prevails)
5078 : : {
5079 : 565732 : jump_func->agg.items = NULL;
5080 : 565732 : vec_safe_reserve (jump_func->agg.items, count, true);
5081 : : }
5082 : 565738 : if (count)
5083 : : {
5084 : 2651 : struct bitpack_d bp = streamer_read_bitpack (ib);
5085 : 2651 : jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
5086 : : }
5087 : 571257 : for (i = 0; i < count; i++)
5088 : : {
5089 : 5519 : struct ipa_agg_jf_item item;
5090 : 5519 : item.type = stream_read_tree (ib, data_in);
5091 : 5519 : item.offset = streamer_read_uhwi (ib);
5092 : 5519 : item.jftype = (enum jump_func_type) streamer_read_uhwi (ib);
5093 : :
5094 : 5519 : switch (item.jftype)
5095 : : {
5096 : : case IPA_JF_UNKNOWN:
5097 : : break;
5098 : 5093 : case IPA_JF_CONST:
5099 : 5093 : item.value.constant = stream_read_tree (ib, data_in);
5100 : 5093 : break;
5101 : 426 : case IPA_JF_PASS_THROUGH:
5102 : 426 : case IPA_JF_LOAD_AGG:
5103 : 426 : operation = (enum tree_code) streamer_read_uhwi (ib);
5104 : 426 : item.value.pass_through.operation = operation;
5105 : 426 : item.value.pass_through.formal_id = streamer_read_uhwi (ib);
5106 : 426 : if (TREE_CODE_CLASS (operation) == tcc_unary)
5107 : 426 : item.value.pass_through.operand = NULL_TREE;
5108 : : else
5109 : 0 : item.value.pass_through.operand = stream_read_tree (ib, data_in);
5110 : 426 : if (item.jftype == IPA_JF_LOAD_AGG)
5111 : : {
5112 : 36 : struct bitpack_d bp;
5113 : 36 : item.value.load_agg.type = stream_read_tree (ib, data_in);
5114 : 36 : item.value.load_agg.offset = streamer_read_uhwi (ib);
5115 : 36 : bp = streamer_read_bitpack (ib);
5116 : 36 : item.value.load_agg.by_ref = bp_unpack_value (&bp, 1);
5117 : : }
5118 : : break;
5119 : 0 : default:
5120 : 0 : fatal_error (UNKNOWN_LOCATION,
5121 : : "invalid jump function in LTO stream");
5122 : : }
5123 : 5519 : if (prevails)
5124 : 5519 : jump_func->agg.items->quick_push (item);
5125 : : }
5126 : :
5127 : 565738 : ipa_vr vr;
5128 : 565738 : vr.streamer_read (ib, data_in);
5129 : 565738 : if (vr.known_p ())
5130 : : {
5131 : 395047 : if (prevails)
5132 : 395041 : ipa_set_jfunc_vr (jump_func, vr);
5133 : : }
5134 : : else
5135 : 170691 : jump_func->m_vr = NULL;
5136 : 565738 : }
5137 : :
5138 : : /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
5139 : : relevant to indirect inlining to OB. */
5140 : :
5141 : : static void
5142 : 1758 : ipa_write_indirect_edge_info (struct output_block *ob,
5143 : : struct cgraph_edge *cs)
5144 : : {
5145 : 1758 : class cgraph_indirect_call_info *ii = cs->indirect_info;
5146 : 1758 : struct bitpack_d bp;
5147 : :
5148 : 1758 : streamer_write_hwi (ob, ii->param_index);
5149 : 1758 : bp = bitpack_create (ob->main_stream);
5150 : 1758 : bp_pack_value (&bp, ii->polymorphic, 1);
5151 : 1758 : bp_pack_value (&bp, ii->agg_contents, 1);
5152 : 1758 : bp_pack_value (&bp, ii->member_ptr, 1);
5153 : 1758 : bp_pack_value (&bp, ii->by_ref, 1);
5154 : 1758 : bp_pack_value (&bp, ii->guaranteed_unmodified, 1);
5155 : 1758 : bp_pack_value (&bp, ii->vptr_changed, 1);
5156 : 1758 : streamer_write_bitpack (&bp);
5157 : 1758 : if (ii->agg_contents || ii->polymorphic)
5158 : 294 : streamer_write_hwi (ob, ii->offset);
5159 : : else
5160 : 1464 : gcc_assert (ii->offset == 0);
5161 : :
5162 : 1758 : if (ii->polymorphic)
5163 : : {
5164 : 241 : streamer_write_hwi (ob, ii->otr_token);
5165 : 241 : stream_write_tree (ob, ii->otr_type, true);
5166 : 241 : ii->context.stream_out (ob);
5167 : : }
5168 : 1758 : }
5169 : :
5170 : : /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
5171 : : relevant to indirect inlining from IB. */
5172 : :
5173 : : static void
5174 : 1352 : ipa_read_indirect_edge_info (class lto_input_block *ib,
5175 : : class data_in *data_in,
5176 : : struct cgraph_edge *cs,
5177 : : class ipa_node_params *info)
5178 : : {
5179 : 1352 : class cgraph_indirect_call_info *ii = cs->indirect_info;
5180 : 1352 : struct bitpack_d bp;
5181 : :
5182 : 1352 : ii->param_index = (int) streamer_read_hwi (ib);
5183 : 1352 : bp = streamer_read_bitpack (ib);
5184 : 1352 : ii->polymorphic = bp_unpack_value (&bp, 1);
5185 : 1352 : ii->agg_contents = bp_unpack_value (&bp, 1);
5186 : 1352 : ii->member_ptr = bp_unpack_value (&bp, 1);
5187 : 1352 : ii->by_ref = bp_unpack_value (&bp, 1);
5188 : 1352 : ii->guaranteed_unmodified = bp_unpack_value (&bp, 1);
5189 : 1352 : ii->vptr_changed = bp_unpack_value (&bp, 1);
5190 : 1352 : if (ii->agg_contents || ii->polymorphic)
5191 : 111 : ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
5192 : : else
5193 : 1241 : ii->offset = 0;
5194 : 1352 : if (ii->polymorphic)
5195 : : {
5196 : 82 : ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
5197 : 82 : ii->otr_type = stream_read_tree (ib, data_in);
5198 : 82 : ii->context.stream_in (ib, data_in);
5199 : : }
5200 : 1352 : if (info && ii->param_index >= 0)
5201 : : {
5202 : 320 : if (ii->polymorphic)
5203 : 60 : ipa_set_param_used_by_polymorphic_call (info,
5204 : : ii->param_index , true);
5205 : 320 : ipa_set_param_used_by_indirect_call (info,
5206 : : ii->param_index, true);
5207 : : }
5208 : 1352 : }
5209 : :
5210 : : /* Stream out NODE info to OB. */
5211 : :
5212 : : static void
5213 : 89848 : ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
5214 : : {
5215 : 89848 : int node_ref;
5216 : 89848 : lto_symtab_encoder_t encoder;
5217 : 89848 : ipa_node_params *info = ipa_node_params_sum->get (node);
5218 : 89848 : int j;
5219 : 89848 : struct cgraph_edge *e;
5220 : 89848 : struct bitpack_d bp;
5221 : :
5222 : 89848 : encoder = ob->decl_state->symtab_node_encoder;
5223 : 89848 : node_ref = lto_symtab_encoder_encode (encoder, node);
5224 : 89848 : streamer_write_uhwi (ob, node_ref);
5225 : :
5226 : 89848 : streamer_write_uhwi (ob, ipa_get_param_count (info));
5227 : 435134 : for (j = 0; j < ipa_get_param_count (info); j++)
5228 : 97402 : streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
5229 : 89848 : bp = bitpack_create (ob->main_stream);
5230 : 89848 : gcc_assert (info->analysis_done
5231 : : || ipa_get_param_count (info) == 0);
5232 : 89848 : gcc_assert (!info->node_enqueued);
5233 : 89848 : gcc_assert (!info->ipcp_orig_node);
5234 : 345286 : for (j = 0; j < ipa_get_param_count (info); j++)
5235 : : {
5236 : : /* TODO: We could just not stream the bit in the undescribed case. */
5237 : 97402 : bool d = (ipa_get_controlled_uses (info, j) != IPA_UNDESCRIBED_USE)
5238 : 97402 : ? ipa_get_param_load_dereferenced (info, j) : true;
5239 : 97402 : bp_pack_value (&bp, d, 1);
5240 : 97402 : bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
5241 : : }
5242 : 89848 : streamer_write_bitpack (&bp);
5243 : 345286 : for (j = 0; j < ipa_get_param_count (info); j++)
5244 : : {
5245 : 97402 : streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
5246 : 97402 : stream_write_tree (ob, ipa_get_type (info, j), true);
5247 : : }
5248 : 448422 : for (e = node->callees; e; e = e->next_callee)
5249 : : {
5250 : 358574 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5251 : :
5252 : 358574 : if (!args)
5253 : : {
5254 : 708 : streamer_write_uhwi (ob, 0);
5255 : 708 : continue;
5256 : : }
5257 : :
5258 : 357866 : streamer_write_uhwi (ob,
5259 : 357866 : ipa_get_cs_argument_count (args) * 2
5260 : 357866 : + (args->polymorphic_call_contexts != NULL));
5261 : 2167735 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5262 : : {
5263 : 600972 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5264 : 600972 : if (args->polymorphic_call_contexts != NULL)
5265 : 2271 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5266 : : }
5267 : : }
5268 : 91606 : for (e = node->indirect_calls; e; e = e->next_callee)
5269 : : {
5270 : 1758 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5271 : 1758 : if (!args)
5272 : 6 : streamer_write_uhwi (ob, 0);
5273 : : else
5274 : : {
5275 : 1752 : streamer_write_uhwi (ob,
5276 : 1752 : ipa_get_cs_argument_count (args) * 2
5277 : 1752 : + (args->polymorphic_call_contexts != NULL));
5278 : 9351 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5279 : : {
5280 : 2115 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5281 : 2115 : if (args->polymorphic_call_contexts != NULL)
5282 : 397 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5283 : : }
5284 : : }
5285 : 1758 : ipa_write_indirect_edge_info (ob, e);
5286 : : }
5287 : 89848 : }
5288 : :
5289 : : /* Stream in edge E from IB. */
5290 : :
5291 : : static void
5292 : 331164 : ipa_read_edge_info (class lto_input_block *ib,
5293 : : class data_in *data_in,
5294 : : struct cgraph_edge *e, bool prevails)
5295 : : {
5296 : 331164 : int count = streamer_read_uhwi (ib);
5297 : 331164 : bool contexts_computed = count & 1;
5298 : :
5299 : 331164 : count /= 2;
5300 : 331164 : if (!count)
5301 : : return;
5302 : 231661 : if (prevails
5303 : 231661 : && (e->possibly_call_in_translation_unit_p ()
5304 : : /* Also stream in jump functions to builtins in hope that they
5305 : : will get fnspecs. */
5306 : 114374 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
5307 : : {
5308 : 221555 : ipa_edge_args *args = ipa_edge_args_sum->get_create (e);
5309 : 221555 : vec_safe_grow_cleared (args->jump_functions, count, true);
5310 : 221555 : if (contexts_computed)
5311 : 579 : vec_safe_grow_cleared (args->polymorphic_call_contexts, count, true);
5312 : 771390 : for (int k = 0; k < count; k++)
5313 : : {
5314 : 549835 : ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
5315 : : data_in, prevails);
5316 : 549835 : if (contexts_computed)
5317 : 897 : ipa_get_ith_polymorhic_call_context (args, k)->stream_in
5318 : 897 : (ib, data_in);
5319 : : }
5320 : : }
5321 : : else
5322 : : {
5323 : 26009 : for (int k = 0; k < count; k++)
5324 : : {
5325 : 15903 : struct ipa_jump_func dummy;
5326 : 15903 : ipa_read_jump_function (ib, &dummy, e,
5327 : : data_in, prevails);
5328 : 15903 : if (contexts_computed)
5329 : : {
5330 : 412 : class ipa_polymorphic_call_context ctx;
5331 : 412 : ctx.stream_in (ib, data_in);
5332 : : }
5333 : : }
5334 : : }
5335 : : }
5336 : :
5337 : : /* Stream in NODE info from IB. */
5338 : :
5339 : : static void
5340 : 75161 : ipa_read_node_info (class lto_input_block *ib, struct cgraph_node *node,
5341 : : class data_in *data_in)
5342 : : {
5343 : 75161 : int k;
5344 : 75161 : struct cgraph_edge *e;
5345 : 75161 : struct bitpack_d bp;
5346 : 75161 : bool prevails = node->prevailing_p ();
5347 : 75161 : ipa_node_params *info
5348 : 75161 : = prevails ? ipa_node_params_sum->get_create (node) : NULL;
5349 : :
5350 : 75161 : int param_count = streamer_read_uhwi (ib);
5351 : 75161 : if (prevails)
5352 : : {
5353 : 75143 : ipa_alloc_node_params (node, param_count);
5354 : 225731 : for (k = 0; k < param_count; k++)
5355 : 75445 : (*info->descriptors)[k].move_cost = streamer_read_uhwi (ib);
5356 : 75143 : if (ipa_get_param_count (info) != 0)
5357 : 51576 : info->analysis_done = true;
5358 : 75143 : info->node_enqueued = false;
5359 : : }
5360 : : else
5361 : 27 : for (k = 0; k < param_count; k++)
5362 : 9 : streamer_read_uhwi (ib);
5363 : :
5364 : 75161 : bp = streamer_read_bitpack (ib);
5365 : 150615 : for (k = 0; k < param_count; k++)
5366 : : {
5367 : 75454 : bool load_dereferenced = bp_unpack_value (&bp, 1);
5368 : 75454 : bool used = bp_unpack_value (&bp, 1);
5369 : :
5370 : 75454 : if (prevails)
5371 : : {
5372 : 75445 : ipa_set_param_load_dereferenced (info, k, load_dereferenced);
5373 : 75445 : ipa_set_param_used (info, k, used);
5374 : : }
5375 : : }
5376 : 150615 : for (k = 0; k < param_count; k++)
5377 : : {
5378 : 75454 : int nuses = streamer_read_hwi (ib);
5379 : 75454 : tree type = stream_read_tree (ib, data_in);
5380 : :
5381 : 75454 : if (prevails)
5382 : : {
5383 : 75445 : ipa_set_controlled_uses (info, k, nuses);
5384 : 75445 : (*info->descriptors)[k].decl_or_type = type;
5385 : : }
5386 : : }
5387 : 404973 : for (e = node->callees; e; e = e->next_callee)
5388 : 329812 : ipa_read_edge_info (ib, data_in, e, prevails);
5389 : 76513 : for (e = node->indirect_calls; e; e = e->next_callee)
5390 : : {
5391 : 1352 : ipa_read_edge_info (ib, data_in, e, prevails);
5392 : 1352 : ipa_read_indirect_edge_info (ib, data_in, e, info);
5393 : : }
5394 : 75161 : }
5395 : :
5396 : : /* Write jump functions for nodes in SET. */
5397 : :
5398 : : void
5399 : 23763 : ipa_prop_write_jump_functions (void)
5400 : : {
5401 : 23763 : struct output_block *ob;
5402 : 23763 : unsigned int count = 0;
5403 : 23763 : lto_symtab_encoder_iterator lsei;
5404 : 23763 : lto_symtab_encoder_t encoder;
5405 : :
5406 : 23763 : if (!ipa_node_params_sum || !ipa_edge_args_sum)
5407 : 0 : return;
5408 : :
5409 : 23763 : ob = create_output_block (LTO_section_jump_functions);
5410 : 23763 : encoder = ob->decl_state->symtab_node_encoder;
5411 : 23763 : ob->symbol = NULL;
5412 : 127291 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5413 : 103528 : lsei_next_function_in_partition (&lsei))
5414 : : {
5415 : 103528 : cgraph_node *node = lsei_cgraph_node (lsei);
5416 : 103528 : if (node->has_gimple_body_p ()
5417 : 103528 : && ipa_node_params_sum->get (node) != NULL)
5418 : 89848 : count++;
5419 : : }
5420 : :
5421 : 23763 : streamer_write_uhwi (ob, count);
5422 : :
5423 : : /* Process all of the functions. */
5424 : 127291 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5425 : 103528 : lsei_next_function_in_partition (&lsei))
5426 : : {
5427 : 103528 : cgraph_node *node = lsei_cgraph_node (lsei);
5428 : 103528 : if (node->has_gimple_body_p ()
5429 : 103528 : && ipa_node_params_sum->get (node) != NULL)
5430 : 89848 : ipa_write_node_info (ob, node);
5431 : : }
5432 : 23763 : streamer_write_char_stream (ob->main_stream, 0);
5433 : 23763 : produce_asm (ob);
5434 : 23763 : destroy_output_block (ob);
5435 : : }
5436 : :
5437 : : /* Read section in file FILE_DATA of length LEN with data DATA. */
5438 : :
5439 : : static void
5440 : 13839 : ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
5441 : : size_t len)
5442 : : {
5443 : 13839 : const struct lto_function_header *header =
5444 : : (const struct lto_function_header *) data;
5445 : 13839 : const int cfg_offset = sizeof (struct lto_function_header);
5446 : 13839 : const int main_offset = cfg_offset + header->cfg_size;
5447 : 13839 : const int string_offset = main_offset + header->main_size;
5448 : 13839 : class data_in *data_in;
5449 : 13839 : unsigned int i;
5450 : 13839 : unsigned int count;
5451 : :
5452 : 13839 : lto_input_block ib_main ((const char *) data + main_offset,
5453 : 13839 : header->main_size, file_data);
5454 : :
5455 : 13839 : data_in =
5456 : 27678 : lto_data_in_create (file_data, (const char *) data + string_offset,
5457 : 13839 : header->string_size, vNULL);
5458 : 13839 : count = streamer_read_uhwi (&ib_main);
5459 : :
5460 : 89000 : for (i = 0; i < count; i++)
5461 : : {
5462 : 75161 : unsigned int index;
5463 : 75161 : struct cgraph_node *node;
5464 : 75161 : lto_symtab_encoder_t encoder;
5465 : :
5466 : 75161 : index = streamer_read_uhwi (&ib_main);
5467 : 75161 : encoder = file_data->symtab_node_encoder;
5468 : 75161 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
5469 : : index));
5470 : 75161 : gcc_assert (node->definition);
5471 : 75161 : ipa_read_node_info (&ib_main, node, data_in);
5472 : : }
5473 : 13839 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
5474 : : len);
5475 : 13839 : lto_data_in_delete (data_in);
5476 : 13839 : }
5477 : :
5478 : : /* Read ipcp jump functions. */
5479 : :
5480 : : void
5481 : 12814 : ipa_prop_read_jump_functions (void)
5482 : : {
5483 : 12814 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
5484 : 12814 : struct lto_file_decl_data *file_data;
5485 : 12814 : unsigned int j = 0;
5486 : :
5487 : 12814 : ipa_check_create_node_params ();
5488 : 12814 : ipa_check_create_edge_args ();
5489 : 12814 : ipa_register_cgraph_hooks ();
5490 : :
5491 : 39467 : while ((file_data = file_data_vec[j++]))
5492 : : {
5493 : 13839 : size_t len;
5494 : 13839 : const char *data
5495 : 13839 : = lto_get_summary_section_data (file_data, LTO_section_jump_functions,
5496 : : &len);
5497 : 13839 : if (data)
5498 : 13839 : ipa_prop_read_section (file_data, data, len);
5499 : : }
5500 : 12814 : }
5501 : :
5502 : : /* Return true if the IPA-CP transformation summary TS is non-NULL and contains
5503 : : useful info. */
5504 : : static bool
5505 : 162974 : useful_ipcp_transformation_info_p (ipcp_transformation *ts)
5506 : : {
5507 : 162974 : if (!ts)
5508 : : return false;
5509 : 21704 : if (!vec_safe_is_empty (ts->m_agg_values)
5510 : 21314 : || !vec_safe_is_empty (ts->m_vr))
5511 : 21456 : return true;
5512 : : return false;
5513 : : }
5514 : :
5515 : : /* Write into OB IPA-CP transfromation summary TS describing NODE. */
5516 : :
5517 : : void
5518 : 10715 : write_ipcp_transformation_info (output_block *ob, cgraph_node *node,
5519 : : ipcp_transformation *ts)
5520 : : {
5521 : 10715 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5522 : 10715 : int node_ref = lto_symtab_encoder_encode (encoder, node);
5523 : 10715 : streamer_write_uhwi (ob, node_ref);
5524 : :
5525 : 10907 : streamer_write_uhwi (ob, vec_safe_length (ts->m_agg_values));
5526 : 12216 : for (const ipa_argagg_value &av : ts->m_agg_values)
5527 : : {
5528 : 1117 : struct bitpack_d bp;
5529 : :
5530 : 1117 : stream_write_tree (ob, av.value, true);
5531 : 1117 : streamer_write_uhwi (ob, av.unit_offset);
5532 : 1117 : streamer_write_uhwi (ob, av.index);
5533 : :
5534 : 1117 : bp = bitpack_create (ob->main_stream);
5535 : 1117 : bp_pack_value (&bp, av.by_ref, 1);
5536 : 1117 : bp_pack_value (&bp, av.killed, 1);
5537 : 1117 : streamer_write_bitpack (&bp);
5538 : : }
5539 : :
5540 : : /* If all instances of this node are inlined, ipcp info is not useful. */
5541 : 10715 : if (!lto_symtab_encoder_only_for_inlining_p (encoder, node))
5542 : : {
5543 : 19288 : streamer_write_uhwi (ob, vec_safe_length (ts->m_vr));
5544 : 45742 : for (const ipa_vr &parm_vr : ts->m_vr)
5545 : 16812 : parm_vr.streamer_write (ob);
5546 : : }
5547 : : else
5548 : 1069 : streamer_write_uhwi (ob, 0);
5549 : 10715 : }
5550 : :
5551 : : /* Stream in the aggregate value replacement chain for NODE from IB. */
5552 : :
5553 : : static void
5554 : 10715 : read_ipcp_transformation_info (lto_input_block *ib, cgraph_node *node,
5555 : : data_in *data_in)
5556 : : {
5557 : 10715 : unsigned int count, i;
5558 : 10715 : ipcp_transformation_initialize ();
5559 : 10715 : ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
5560 : :
5561 : 10715 : count = streamer_read_uhwi (ib);
5562 : 10715 : if (count > 0)
5563 : : {
5564 : 192 : vec_safe_grow_cleared (ts->m_agg_values, count, true);
5565 : 1309 : for (i = 0; i <count; i++)
5566 : : {
5567 : 1117 : ipa_argagg_value *av = &(*ts->m_agg_values)[i];;
5568 : :
5569 : 1117 : av->value = stream_read_tree (ib, data_in);
5570 : 1117 : av->unit_offset = streamer_read_uhwi (ib);
5571 : 1117 : av->index = streamer_read_uhwi (ib);
5572 : :
5573 : 1117 : bitpack_d bp = streamer_read_bitpack (ib);
5574 : 1117 : av->by_ref = bp_unpack_value (&bp, 1);
5575 : 1117 : av->killed = bp_unpack_value (&bp, 1);
5576 : : }
5577 : : }
5578 : :
5579 : 10715 : count = streamer_read_uhwi (ib);
5580 : 10715 : if (count > 0)
5581 : : {
5582 : 9642 : vec_safe_grow_cleared (ts->m_vr, count, true);
5583 : 26454 : for (i = 0; i < count; i++)
5584 : : {
5585 : 16812 : ipa_vr *parm_vr;
5586 : 16812 : parm_vr = &(*ts->m_vr)[i];
5587 : 16812 : parm_vr->streamer_read (ib, data_in);
5588 : : }
5589 : : }
5590 : 10715 : }
5591 : :
5592 : : /* Write all aggregate replacement for nodes in set. */
5593 : :
5594 : : void
5595 : 8943 : ipcp_write_transformation_summaries (void)
5596 : : {
5597 : 8943 : struct output_block *ob;
5598 : 8943 : unsigned int count = 0;
5599 : 8943 : lto_symtab_encoder_t encoder;
5600 : :
5601 : 8943 : ob = create_output_block (LTO_section_ipcp_transform);
5602 : 8943 : encoder = ob->decl_state->symtab_node_encoder;
5603 : 8943 : ob->symbol = NULL;
5604 : :
5605 : 229325 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5606 : : {
5607 : 105724 : symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
5608 : 105724 : cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
5609 : 105724 : if (!cnode)
5610 : 24237 : continue;
5611 : 81487 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
5612 : 81487 : if (useful_ipcp_transformation_info_p (ts)
5613 : 81487 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
5614 : 10715 : count++;
5615 : : }
5616 : :
5617 : 8943 : streamer_write_uhwi (ob, count);
5618 : :
5619 : 229325 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5620 : : {
5621 : 105724 : symtab_node *snode = lto_symtab_encoder_deref (encoder, i);
5622 : 105724 : cgraph_node *cnode = dyn_cast <cgraph_node *> (snode);
5623 : 105724 : if (!cnode)
5624 : 24237 : continue;
5625 : 81487 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
5626 : 81487 : if (useful_ipcp_transformation_info_p (ts)
5627 : 81487 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
5628 : 10715 : write_ipcp_transformation_info (ob, cnode, ts);
5629 : : }
5630 : 8943 : streamer_write_char_stream (ob->main_stream, 0);
5631 : 8943 : produce_asm (ob);
5632 : 8943 : destroy_output_block (ob);
5633 : 8943 : }
5634 : :
5635 : : /* Read replacements section in file FILE_DATA of length LEN with data
5636 : : DATA. */
5637 : :
5638 : : static void
5639 : 8943 : read_replacements_section (struct lto_file_decl_data *file_data,
5640 : : const char *data,
5641 : : size_t len)
5642 : : {
5643 : 8943 : const struct lto_function_header *header =
5644 : : (const struct lto_function_header *) data;
5645 : 8943 : const int cfg_offset = sizeof (struct lto_function_header);
5646 : 8943 : const int main_offset = cfg_offset + header->cfg_size;
5647 : 8943 : const int string_offset = main_offset + header->main_size;
5648 : 8943 : class data_in *data_in;
5649 : 8943 : unsigned int i;
5650 : 8943 : unsigned int count;
5651 : :
5652 : 8943 : lto_input_block ib_main ((const char *) data + main_offset,
5653 : 8943 : header->main_size, file_data);
5654 : :
5655 : 8943 : data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
5656 : 8943 : header->string_size, vNULL);
5657 : 8943 : count = streamer_read_uhwi (&ib_main);
5658 : :
5659 : 19658 : for (i = 0; i < count; i++)
5660 : : {
5661 : 10715 : unsigned int index;
5662 : 10715 : struct cgraph_node *node;
5663 : 10715 : lto_symtab_encoder_t encoder;
5664 : :
5665 : 10715 : index = streamer_read_uhwi (&ib_main);
5666 : 10715 : encoder = file_data->symtab_node_encoder;
5667 : 10715 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
5668 : : index));
5669 : 10715 : read_ipcp_transformation_info (&ib_main, node, data_in);
5670 : : }
5671 : 8943 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
5672 : : len);
5673 : 8943 : lto_data_in_delete (data_in);
5674 : 8943 : }
5675 : :
5676 : : /* Read IPA-CP aggregate replacements. */
5677 : :
5678 : : void
5679 : 8943 : ipcp_read_transformation_summaries (void)
5680 : : {
5681 : 8943 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
5682 : 8943 : struct lto_file_decl_data *file_data;
5683 : 8943 : unsigned int j = 0;
5684 : :
5685 : 26829 : while ((file_data = file_data_vec[j++]))
5686 : : {
5687 : 8943 : size_t len;
5688 : 8943 : const char *data
5689 : 8943 : = lto_get_summary_section_data (file_data, LTO_section_ipcp_transform,
5690 : : &len);
5691 : 8943 : if (data)
5692 : 8943 : read_replacements_section (file_data, data, len);
5693 : : }
5694 : 8943 : }
5695 : :
5696 : : /* Adjust the aggregate replacements in TS to reflect any parameter removals
5697 : : which might have already taken place. If after adjustments there are no
5698 : : aggregate replacements left, the m_agg_values will be set to NULL. In other
5699 : : cases, it may be shrunk. */
5700 : :
5701 : : static void
5702 : 1631 : adjust_agg_replacement_values (cgraph_node *node, ipcp_transformation *ts)
5703 : : {
5704 : 1631 : clone_info *cinfo = clone_info::get (node);
5705 : 1631 : if (!cinfo || !cinfo->param_adjustments)
5706 : : return;
5707 : :
5708 : 817 : auto_vec<int, 16> new_indices;
5709 : 817 : cinfo->param_adjustments->get_updated_indices (&new_indices);
5710 : 817 : bool removed_item = false;
5711 : 817 : unsigned dst_index = 0;
5712 : 817 : unsigned count = ts->m_agg_values->length ();
5713 : 4449 : for (unsigned i = 0; i < count; i++)
5714 : : {
5715 : 3632 : ipa_argagg_value *v = &(*ts->m_agg_values)[i];
5716 : 3632 : gcc_checking_assert (v->index >= 0);
5717 : :
5718 : 3632 : int new_idx = -1;
5719 : 3632 : if ((unsigned) v->index < new_indices.length ())
5720 : 2286 : new_idx = new_indices[v->index];
5721 : :
5722 : 2286 : if (new_idx >= 0)
5723 : : {
5724 : 1518 : v->index = new_idx;
5725 : 1518 : if (removed_item)
5726 : 23 : (*ts->m_agg_values)[dst_index] = *v;
5727 : 1518 : dst_index++;
5728 : : }
5729 : : else
5730 : : removed_item = true;
5731 : : }
5732 : :
5733 : 817 : if (dst_index == 0)
5734 : : {
5735 : 486 : ggc_free (ts->m_agg_values);
5736 : 486 : ts->m_agg_values = NULL;
5737 : : }
5738 : 331 : else if (removed_item)
5739 : 34 : ts->m_agg_values->truncate (dst_index);
5740 : :
5741 : 817 : return;
5742 : 817 : }
5743 : :
5744 : : /* Dominator walker driving the ipcp modification phase. */
5745 : :
5746 : : class ipcp_modif_dom_walker : public dom_walker
5747 : : {
5748 : : public:
5749 : 1145 : ipcp_modif_dom_walker (struct ipa_func_body_info *fbi,
5750 : : vec<ipa_param_descriptor, va_gc> *descs,
5751 : : ipcp_transformation *ts, bool *sc)
5752 : 2290 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi), m_descriptors (descs),
5753 : 1145 : m_ts (ts), m_something_changed (sc) {}
5754 : :
5755 : : edge before_dom_children (basic_block) final override;
5756 : 1145 : bool cleanup_eh ()
5757 : 1145 : { return gimple_purge_all_dead_eh_edges (m_need_eh_cleanup); }
5758 : :
5759 : : private:
5760 : : struct ipa_func_body_info *m_fbi;
5761 : : vec<ipa_param_descriptor, va_gc> *m_descriptors;
5762 : : ipcp_transformation *m_ts;
5763 : : bool *m_something_changed;
5764 : : auto_bitmap m_need_eh_cleanup;
5765 : : };
5766 : :
5767 : : edge
5768 : 18365 : ipcp_modif_dom_walker::before_dom_children (basic_block bb)
5769 : : {
5770 : 18365 : gimple_stmt_iterator gsi;
5771 : 99701 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5772 : : {
5773 : 62971 : gimple *stmt = gsi_stmt (gsi);
5774 : 62971 : tree rhs, val, t;
5775 : 62971 : HOST_WIDE_INT bit_offset;
5776 : 62971 : poly_int64 size;
5777 : 62971 : int index;
5778 : 62971 : bool by_ref, vce;
5779 : :
5780 : 62971 : if (!gimple_assign_load_p (stmt))
5781 : 61714 : continue;
5782 : 9890 : rhs = gimple_assign_rhs1 (stmt);
5783 : 9890 : if (!is_gimple_reg_type (TREE_TYPE (rhs)))
5784 : 532 : continue;
5785 : :
5786 : 23236 : vce = false;
5787 : : t = rhs;
5788 : 23236 : while (handled_component_p (t))
5789 : : {
5790 : : /* V_C_E can do things like convert an array of integers to one
5791 : : bigger integer and similar things we do not handle below. */
5792 : 13878 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
5793 : : {
5794 : : vce = true;
5795 : : break;
5796 : : }
5797 : 13878 : t = TREE_OPERAND (t, 0);
5798 : : }
5799 : 9358 : if (vce)
5800 : 0 : continue;
5801 : :
5802 : 9358 : if (!ipa_load_from_parm_agg (m_fbi, m_descriptors, stmt, rhs, &index,
5803 : : &bit_offset, &size, &by_ref))
5804 : 6895 : continue;
5805 : 2463 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
5806 : 2463 : ipa_argagg_value_list avl (m_ts);
5807 : 2463 : tree v = avl.get_value (index, unit_offset, by_ref);
5808 : :
5809 : 3669 : if (!v
5810 : 2463 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), size))
5811 : 1206 : continue;
5812 : :
5813 : 1257 : gcc_checking_assert (is_gimple_ip_invariant (v));
5814 : 1257 : if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v)))
5815 : : {
5816 : 0 : if (fold_convertible_p (TREE_TYPE (rhs), v))
5817 : 0 : val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v);
5818 : 0 : else if (TYPE_SIZE (TREE_TYPE (rhs))
5819 : 0 : == TYPE_SIZE (TREE_TYPE (v)))
5820 : 0 : val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v);
5821 : : else
5822 : : {
5823 : 0 : if (dump_file)
5824 : : {
5825 : 0 : fprintf (dump_file, " const ");
5826 : 0 : print_generic_expr (dump_file, v);
5827 : 0 : fprintf (dump_file, " can't be converted to type of ");
5828 : 0 : print_generic_expr (dump_file, rhs);
5829 : 0 : fprintf (dump_file, "\n");
5830 : : }
5831 : 0 : continue;
5832 : : }
5833 : : }
5834 : : else
5835 : : val = v;
5836 : :
5837 : 1257 : if (dump_file && (dump_flags & TDF_DETAILS))
5838 : : {
5839 : 38 : fprintf (dump_file, "Modifying stmt:\n ");
5840 : 38 : print_gimple_stmt (dump_file, stmt, 0);
5841 : : }
5842 : 1257 : gimple_assign_set_rhs_from_tree (&gsi, val);
5843 : 1257 : update_stmt (stmt);
5844 : :
5845 : 1257 : if (dump_file && (dump_flags & TDF_DETAILS))
5846 : : {
5847 : 38 : fprintf (dump_file, "into:\n ");
5848 : 38 : print_gimple_stmt (dump_file, stmt, 0);
5849 : 38 : fprintf (dump_file, "\n");
5850 : : }
5851 : :
5852 : 1257 : *m_something_changed = true;
5853 : 1257 : if (maybe_clean_eh_stmt (stmt))
5854 : 9 : bitmap_set_bit (m_need_eh_cleanup, bb->index);
5855 : : }
5856 : 18365 : return NULL;
5857 : : }
5858 : :
5859 : : /* If IPA-CP discovered a constant in parameter PARM at OFFSET of a given SIZE
5860 : : - whether passed by reference or not is given by BY_REF - return that
5861 : : constant. Otherwise return NULL_TREE. The is supposed to be used only
5862 : : after clone materialization and transformation is done (because it asserts
5863 : : that killed constants have been pruned). */
5864 : :
5865 : : tree
5866 : 3773358 : ipcp_get_aggregate_const (struct function *func, tree parm, bool by_ref,
5867 : : HOST_WIDE_INT bit_offset, HOST_WIDE_INT bit_size)
5868 : : {
5869 : 3773358 : cgraph_node *node = cgraph_node::get (func->decl);
5870 : 3773358 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
5871 : :
5872 : 3773358 : if (!ts || !ts->m_agg_values)
5873 : : return NULL_TREE;
5874 : :
5875 : 9411 : int index = ts->get_param_index (func->decl, parm);
5876 : 9411 : if (index < 0)
5877 : : return NULL_TREE;
5878 : :
5879 : 9346 : ipa_argagg_value_list avl (ts);
5880 : 9346 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
5881 : 9346 : const ipa_argagg_value *av = avl.get_elt (index, unit_offset);
5882 : 9346 : if (!av || av->by_ref != by_ref)
5883 : : return NULL_TREE;
5884 : 1804 : gcc_assert (!av->killed);
5885 : 1804 : tree v = av->value;
5886 : 1804 : if (!v
5887 : 1804 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), bit_size))
5888 : 686 : return NULL_TREE;
5889 : :
5890 : : return v;
5891 : : }
5892 : :
5893 : : /* Return true if we have recorded VALUE and MASK about PARM.
5894 : : Set VALUE and MASk accordingly. */
5895 : :
5896 : : bool
5897 : 7731406 : ipcp_get_parm_bits (tree parm, tree *value, widest_int *mask)
5898 : : {
5899 : 7731406 : cgraph_node *cnode = cgraph_node::get (current_function_decl);
5900 : 7731406 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
5901 : 7731406 : if (!ts
5902 : 116123 : || vec_safe_length (ts->m_vr) == 0
5903 : 7880167 : || !ipa_vr_supported_type_p (TREE_TYPE (parm)))
5904 : : return false;
5905 : :
5906 : 109457 : int i = ts->get_param_index (current_function_decl, parm);
5907 : 109457 : if (i < 0)
5908 : : return false;
5909 : 108113 : clone_info *cinfo = clone_info::get (cnode);
5910 : 108113 : if (cinfo && cinfo->param_adjustments)
5911 : : {
5912 : 28443 : i = cinfo->param_adjustments->get_original_index (i);
5913 : 28443 : if (i < 0)
5914 : : return false;
5915 : : }
5916 : :
5917 : 99584 : vec<ipa_vr, va_gc> &vr = *ts->m_vr;
5918 : 99584 : if (!vr[i].known_p ())
5919 : : return false;
5920 : 78997 : value_range tmp;
5921 : 78997 : vr[i].get_vrange (tmp);
5922 : 78997 : if (tmp.undefined_p () || tmp.varying_p ())
5923 : : return false;
5924 : 78997 : irange_bitmask bm;
5925 : 78997 : bm = tmp.get_bitmask ();
5926 : 78997 : *mask = widest_int::from (bm.mask (), TYPE_SIGN (TREE_TYPE (parm)));
5927 : 78997 : *value = wide_int_to_tree (TREE_TYPE (parm), bm.value ());
5928 : 78997 : return true;
5929 : 78997 : }
5930 : :
5931 : : /* Update value range of formal parameters of NODE as described in TS. */
5932 : :
5933 : : static void
5934 : 20534 : ipcp_update_vr (struct cgraph_node *node, ipcp_transformation *ts)
5935 : : {
5936 : 20534 : if (vec_safe_is_empty (ts->m_vr))
5937 : 406 : return;
5938 : 20128 : const vec<ipa_vr, va_gc> &vr = *ts->m_vr;
5939 : 20128 : unsigned count = vr.length ();
5940 : 20128 : if (!count)
5941 : : return;
5942 : :
5943 : 20128 : auto_vec<int, 16> new_indices;
5944 : 20128 : bool need_remapping = false;
5945 : 20128 : clone_info *cinfo = clone_info::get (node);
5946 : 20128 : if (cinfo && cinfo->param_adjustments)
5947 : : {
5948 : 5733 : cinfo->param_adjustments->get_updated_indices (&new_indices);
5949 : 5733 : need_remapping = true;
5950 : : }
5951 : 20128 : auto_vec <tree, 16> parm_decls;
5952 : 20128 : push_function_arg_decls (&parm_decls, node->decl);
5953 : :
5954 : 68406 : for (unsigned i = 0; i < count; ++i)
5955 : : {
5956 : 48278 : tree parm;
5957 : 48278 : int remapped_idx;
5958 : 48278 : if (need_remapping)
5959 : : {
5960 : 16471 : if (i >= new_indices.length ())
5961 : 7466 : continue;
5962 : 9005 : remapped_idx = new_indices[i];
5963 : 9005 : if (remapped_idx < 0)
5964 : 1975 : continue;
5965 : : }
5966 : : else
5967 : 31807 : remapped_idx = i;
5968 : :
5969 : 38837 : parm = parm_decls[remapped_idx];
5970 : :
5971 : 38837 : gcc_checking_assert (parm);
5972 : 38837 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl), parm);
5973 : :
5974 : 38837 : if (!ddef || !is_gimple_reg (parm))
5975 : 5071 : continue;
5976 : :
5977 : 33766 : if (vr[i].known_p ())
5978 : : {
5979 : 26338 : value_range tmp;
5980 : 26338 : vr[i].get_vrange (tmp);
5981 : :
5982 : 26338 : if (!tmp.undefined_p () && !tmp.varying_p ())
5983 : : {
5984 : 26338 : if (dump_file)
5985 : : {
5986 : 95 : fprintf (dump_file, "Setting value range of param %u "
5987 : : "(now %i) ", i, remapped_idx);
5988 : 95 : tmp.dump (dump_file);
5989 : 95 : fprintf (dump_file, "]\n");
5990 : : }
5991 : 26338 : set_range_info (ddef, tmp);
5992 : :
5993 : 40668 : if (POINTER_TYPE_P (TREE_TYPE (parm))
5994 : 29297 : && opt_for_fn (node->decl, flag_ipa_bit_cp))
5995 : : {
5996 : 14966 : irange_bitmask bm = tmp.get_bitmask ();
5997 : 14966 : unsigned tem = bm.mask ().to_uhwi ();
5998 : 14966 : unsigned HOST_WIDE_INT bitpos = bm.value ().to_uhwi ();
5999 : 14966 : unsigned align = tem & -tem;
6000 : 14966 : unsigned misalign = bitpos & (align - 1);
6001 : :
6002 : 14966 : if (align > 1)
6003 : : {
6004 : 12470 : if (dump_file)
6005 : : {
6006 : 71 : fprintf (dump_file,
6007 : : "Adjusting mask for param %u to ", i);
6008 : 71 : print_hex (bm.mask (), dump_file);
6009 : 71 : fprintf (dump_file, "\n");
6010 : : }
6011 : :
6012 : 12470 : if (dump_file)
6013 : 71 : fprintf (dump_file,
6014 : : "Adjusting align: %u, misalign: %u\n",
6015 : : align, misalign);
6016 : :
6017 : 12470 : unsigned old_align, old_misalign;
6018 : 12470 : struct ptr_info_def *pi = get_ptr_info (ddef);
6019 : 12470 : bool old_known = get_ptr_info_alignment (pi, &old_align,
6020 : : &old_misalign);
6021 : :
6022 : 12470 : if (old_known && old_align > align)
6023 : : {
6024 : 0 : if (dump_file)
6025 : : {
6026 : 0 : fprintf (dump_file,
6027 : : "But alignment was already %u.\n",
6028 : : old_align);
6029 : 0 : if ((old_misalign & (align - 1)) != misalign)
6030 : 0 : fprintf (dump_file,
6031 : : "old_misalign (%u) and misalign "
6032 : : "(%u) mismatch\n",
6033 : : old_misalign, misalign);
6034 : : }
6035 : 0 : continue;
6036 : : }
6037 : :
6038 : 12470 : if (dump_file
6039 : 71 : && old_known
6040 : 0 : && ((misalign & (old_align - 1)) != old_misalign))
6041 : 0 : fprintf (dump_file,
6042 : : "old_misalign (%u) and misalign (%u) "
6043 : : "mismatch\n",
6044 : : old_misalign, misalign);
6045 : :
6046 : 12470 : set_ptr_info_alignment (pi, align, misalign);
6047 : : }
6048 : 14966 : }
6049 : 11372 : else if (dump_file && INTEGRAL_TYPE_P (TREE_TYPE (parm)))
6050 : : {
6051 : 20 : irange &r = as_a<irange> (tmp);
6052 : 20 : irange_bitmask bm = r.get_bitmask ();
6053 : 20 : unsigned prec = TYPE_PRECISION (TREE_TYPE (parm));
6054 : 20 : if (wi::ne_p (bm.mask (), wi::shwi (-1, prec)))
6055 : : {
6056 : 13 : fprintf (dump_file,
6057 : : "Adjusting mask for param %u to ", i);
6058 : 13 : print_hex (bm.mask (), dump_file);
6059 : 13 : fprintf (dump_file, "\n");
6060 : : }
6061 : 20 : }
6062 : : }
6063 : 26338 : }
6064 : : }
6065 : 20128 : }
6066 : :
6067 : : /* IPCP transformation phase doing propagation of aggregate values. */
6068 : :
6069 : : unsigned int
6070 : 937368 : ipcp_transform_function (struct cgraph_node *node)
6071 : : {
6072 : 937368 : struct ipa_func_body_info fbi;
6073 : 937368 : int param_count;
6074 : :
6075 : 937368 : gcc_checking_assert (cfun);
6076 : 937368 : gcc_checking_assert (current_function_decl);
6077 : :
6078 : 937368 : if (dump_file)
6079 : 651 : fprintf (dump_file, "Modification phase of node %s\n",
6080 : : node->dump_name ());
6081 : :
6082 : 937368 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
6083 : 937368 : if (!ts
6084 : 937368 : || (vec_safe_is_empty (ts->m_agg_values)
6085 : 19855 : && vec_safe_is_empty (ts->m_vr)))
6086 : : return 0;
6087 : :
6088 : 20534 : ts->maybe_create_parm_idx_map (cfun->decl);
6089 : 20534 : ipcp_update_vr (node, ts);
6090 : 938061 : if (vec_safe_is_empty (ts->m_agg_values))
6091 : : return 0;
6092 : 1838 : param_count = count_formal_params (node->decl);
6093 : 1838 : if (param_count == 0)
6094 : : return 0;
6095 : :
6096 : 1631 : adjust_agg_replacement_values (node, ts);
6097 : 1631 : if (vec_safe_is_empty (ts->m_agg_values))
6098 : : {
6099 : 486 : if (dump_file)
6100 : 2 : fprintf (dump_file, " All affected aggregate parameters were either "
6101 : : "removed or converted into scalars, phase done.\n");
6102 : 486 : return 0;
6103 : : }
6104 : 1145 : if (dump_file)
6105 : : {
6106 : 47 : fprintf (dump_file, " Aggregate replacements:");
6107 : 47 : ipa_argagg_value_list avs (ts);
6108 : 47 : avs.dump (dump_file);
6109 : : }
6110 : :
6111 : 1145 : fbi.node = node;
6112 : 1145 : fbi.info = NULL;
6113 : 1145 : fbi.bb_infos = vNULL;
6114 : 1145 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
6115 : 1145 : fbi.param_count = param_count;
6116 : 1145 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
6117 : :
6118 : 1145 : vec<ipa_param_descriptor, va_gc> *descriptors = NULL;
6119 : 1145 : vec_safe_grow_cleared (descriptors, param_count, true);
6120 : 1145 : ipa_populate_param_decls (node, *descriptors);
6121 : 1145 : bool modified_mem_access = false;
6122 : 1145 : calculate_dominance_info (CDI_DOMINATORS);
6123 : 1145 : ipcp_modif_dom_walker walker (&fbi, descriptors, ts, &modified_mem_access);
6124 : 1145 : walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6125 : 1145 : free_dominance_info (CDI_DOMINATORS);
6126 : 1145 : bool cfg_changed = walker.cleanup_eh ();
6127 : :
6128 : 1145 : int i;
6129 : 1145 : struct ipa_bb_info *bi;
6130 : 21800 : FOR_EACH_VEC_ELT (fbi.bb_infos, i, bi)
6131 : 39020 : free_ipa_bb_info (bi);
6132 : 1145 : fbi.bb_infos.release ();
6133 : :
6134 : 1145 : ts->remove_argaggs_if ([](const ipa_argagg_value &v)
6135 : : {
6136 : 4388 : return v.killed;
6137 : : });
6138 : :
6139 : 1145 : vec_free (descriptors);
6140 : 1145 : if (cfg_changed)
6141 : 1 : delete_unreachable_blocks_update_callgraph (node, false);
6142 : :
6143 : 1145 : return modified_mem_access ? TODO_update_ssa_only_virtuals : 0;
6144 : 1145 : }
6145 : :
6146 : : /* Record that current function return value range is VAL. */
6147 : :
6148 : : void
6149 : 678426 : ipa_record_return_value_range (value_range val)
6150 : : {
6151 : 678426 : cgraph_node *n = cgraph_node::get (current_function_decl);
6152 : 678426 : if (!ipa_return_value_sum)
6153 : : {
6154 : 77944 : if (!ipa_vr_hash_table)
6155 : 72603 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
6156 : 77944 : ipa_return_value_sum = new (ggc_alloc_no_dtor <ipa_return_value_sum_t> ())
6157 : 77944 : ipa_return_value_sum_t (symtab, true);
6158 : 77944 : ipa_return_value_sum->disable_insertion_hook ();
6159 : : }
6160 : 678426 : ipa_return_value_sum->get_create (n)->vr = ipa_get_value_range (val);
6161 : 678426 : if (dump_file && (dump_flags & TDF_DETAILS))
6162 : : {
6163 : 22 : fprintf (dump_file, "Recording return range ");
6164 : 22 : val.dump (dump_file);
6165 : 22 : fprintf (dump_file, "\n");
6166 : : }
6167 : 678426 : }
6168 : :
6169 : : /* Return true if value range of DECL is known and if so initialize RANGE. */
6170 : :
6171 : : bool
6172 : 11116982 : ipa_return_value_range (value_range &range, tree decl)
6173 : : {
6174 : 11116982 : cgraph_node *n = cgraph_node::get (decl);
6175 : 11116982 : if (!n || !ipa_return_value_sum)
6176 : : return false;
6177 : 8924075 : enum availability avail;
6178 : 8924075 : n = n->ultimate_alias_target (&avail);
6179 : 8924075 : if (avail < AVAIL_AVAILABLE)
6180 : : return false;
6181 : 1815185 : if (n->decl != decl && !useless_type_conversion_p (TREE_TYPE (decl), TREE_TYPE (n->decl)))
6182 : : return false;
6183 : 1815185 : ipa_return_value_summary *v = ipa_return_value_sum->get (n);
6184 : 1815185 : if (!v)
6185 : : return false;
6186 : 511165 : v->vr->get_vrange (range);
6187 : 511165 : return true;
6188 : : }
6189 : :
6190 : : /* Reset all state within ipa-prop.cc so that we can rerun the compiler
6191 : : within the same process. For use by toplev::finalize. */
6192 : :
6193 : : void
6194 : 254814 : ipa_prop_cc_finalize (void)
6195 : : {
6196 : 254814 : if (function_insertion_hook_holder)
6197 : 13205 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
6198 : 254814 : function_insertion_hook_holder = NULL;
6199 : :
6200 : 254814 : if (ipa_edge_args_sum)
6201 : 13545 : ggc_delete (ipa_edge_args_sum);
6202 : 254814 : ipa_edge_args_sum = NULL;
6203 : :
6204 : 254814 : if (ipa_node_params_sum)
6205 : 13545 : ggc_delete (ipa_node_params_sum);
6206 : 254814 : ipa_node_params_sum = NULL;
6207 : 254814 : }
6208 : :
6209 : : /* Return true if the two pass_through components of two jump functions are
6210 : : known to be equivalent. AGG_JF denotes whether they are part of aggregate
6211 : : functions or not. The function can be used before the IPA phase of IPA-CP
6212 : : or inlining because it cannot cope with refdesc changes these passes can
6213 : : carry out. */
6214 : :
6215 : : static bool
6216 : 45480 : ipa_agg_pass_through_jf_equivalent_p (ipa_pass_through_data *ipt1,
6217 : : ipa_pass_through_data *ipt2,
6218 : : bool agg_jf)
6219 : :
6220 : : {
6221 : 45480 : gcc_assert (agg_jf ||
6222 : : (!ipt1->refdesc_decremented && !ipt2->refdesc_decremented));
6223 : 45480 : if (ipt1->operation != ipt2->operation
6224 : 45480 : || ipt1->formal_id != ipt2->formal_id
6225 : 45480 : || (!agg_jf && (ipt1->agg_preserved != ipt2->agg_preserved)))
6226 : : return false;
6227 : 45480 : if (((ipt1->operand != NULL_TREE) != (ipt2->operand != NULL_TREE))
6228 : 45480 : || (ipt1->operand
6229 : 5614 : && !values_equal_for_ipcp_p (ipt1->operand, ipt2->operand)))
6230 : 0 : return false;
6231 : : return true;
6232 : : }
6233 : :
6234 : : /* Return true if the two aggregate jump functions are known to be equivalent.
6235 : : The function can be used before the IPA phase of IPA-CP or inlining because
6236 : : it cannot cope with refdesc changes these passes can carry out. */
6237 : :
6238 : : static bool
6239 : 3726 : ipa_agg_jump_functions_equivalent_p (ipa_agg_jf_item *ajf1,
6240 : : ipa_agg_jf_item *ajf2)
6241 : : {
6242 : 3726 : if (ajf1->offset != ajf2->offset
6243 : 3726 : || ajf1->jftype != ajf2->jftype
6244 : 7452 : || !types_compatible_p (ajf1->type, ajf2->type))
6245 : 0 : return false;
6246 : :
6247 : 3726 : switch (ajf1->jftype)
6248 : : {
6249 : 2246 : case IPA_JF_CONST:
6250 : 2246 : if (!values_equal_for_ipcp_p (ajf1->value.constant,
6251 : : ajf2->value.constant))
6252 : : return false;
6253 : : break;
6254 : 754 : case IPA_JF_PASS_THROUGH:
6255 : 754 : {
6256 : 754 : ipa_pass_through_data *ipt1 = &ajf1->value.pass_through;
6257 : 754 : ipa_pass_through_data *ipt2 = &ajf2->value.pass_through;
6258 : 754 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, true))
6259 : : return false;
6260 : : }
6261 : : break;
6262 : 726 : case IPA_JF_LOAD_AGG:
6263 : 726 : {
6264 : 726 : ipa_load_agg_data *ila1 = &ajf1->value.load_agg;
6265 : 726 : ipa_load_agg_data *ila2 = &ajf2->value.load_agg;
6266 : 726 : if (!ipa_agg_pass_through_jf_equivalent_p (&ila1->pass_through,
6267 : : &ila2->pass_through, true))
6268 : : return false;
6269 : 726 : if (ila1->offset != ila2->offset
6270 : 726 : || ila1->by_ref != ila2->by_ref
6271 : 1452 : || !types_compatible_p (ila1->type, ila2->type))
6272 : 0 : return false;
6273 : : }
6274 : : break;
6275 : 0 : default:
6276 : 0 : gcc_unreachable ();
6277 : : }
6278 : : return true;
6279 : : }
6280 : :
6281 : : /* Return true if the two jump functions are known to be equivalent. The
6282 : : function can be used before the IPA phase of IPA-CP or inlining because it
6283 : : cannot cope with refdesc changes these passes can carry out. */
6284 : :
6285 : : bool
6286 : 107535 : ipa_jump_functions_equivalent_p (ipa_jump_func *jf1, ipa_jump_func *jf2)
6287 : : {
6288 : 107535 : if (jf1->type != jf2->type)
6289 : : return false;
6290 : :
6291 : 107535 : switch (jf1->type)
6292 : : {
6293 : : case IPA_JF_UNKNOWN:
6294 : : break;
6295 : 20255 : case IPA_JF_CONST:
6296 : 20255 : {
6297 : 20255 : tree cst1 = ipa_get_jf_constant (jf1);
6298 : 20255 : tree cst2 = ipa_get_jf_constant (jf2);
6299 : 20255 : if (!values_equal_for_ipcp_p (cst1, cst2))
6300 : : return false;
6301 : :
6302 : 20254 : ipa_cst_ref_desc *rd1 = jfunc_rdesc_usable (jf1);
6303 : 20254 : ipa_cst_ref_desc *rd2 = jfunc_rdesc_usable (jf2);
6304 : 20254 : if (rd1 && rd2)
6305 : : {
6306 : 604 : gcc_assert (rd1->refcount == 1
6307 : : && rd2->refcount == 1);
6308 : 604 : gcc_assert (!rd1->next_duplicate && !rd2->next_duplicate);
6309 : : }
6310 : 19650 : else if (rd1)
6311 : : return false;
6312 : 19650 : else if (rd2)
6313 : : return false;
6314 : : }
6315 : : break;
6316 : 44000 : case IPA_JF_PASS_THROUGH:
6317 : 44000 : {
6318 : 44000 : ipa_pass_through_data *ipt1 = &jf1->value.pass_through;
6319 : 44000 : ipa_pass_through_data *ipt2 = &jf2->value.pass_through;
6320 : 44000 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, false))
6321 : : return false;
6322 : : }
6323 : : break;
6324 : 8218 : case IPA_JF_ANCESTOR:
6325 : 8218 : {
6326 : 8218 : ipa_ancestor_jf_data *ia1 = &jf1->value.ancestor;
6327 : 8218 : ipa_ancestor_jf_data *ia2 = &jf2->value.ancestor;
6328 : :
6329 : 8218 : if (ia1->formal_id != ia2->formal_id
6330 : 8218 : || ia1->agg_preserved != ia2->agg_preserved
6331 : 8218 : || ia1->keep_null != ia2->keep_null
6332 : 8218 : || ia1->offset != ia2->offset)
6333 : : return false;
6334 : : }
6335 : : break;
6336 : 0 : default:
6337 : 0 : gcc_unreachable ();
6338 : : }
6339 : :
6340 : 107534 : if (((jf1->m_vr != nullptr) != (jf2->m_vr != nullptr))
6341 : 107534 : || (jf1->m_vr && !jf1->m_vr->equal_p (*jf2->m_vr)))
6342 : 5 : return false;
6343 : :
6344 : 107529 : unsigned alen = vec_safe_length (jf1->agg.items);
6345 : 110017 : if (vec_safe_length (jf2->agg.items) != alen)
6346 : : return false;
6347 : :
6348 : 107528 : if (!alen)
6349 : : return true;
6350 : :
6351 : 2488 : if (jf1->agg.by_ref != jf2->agg.by_ref)
6352 : : return false;
6353 : :
6354 : 6214 : for (unsigned i = 0 ; i < alen; i++)
6355 : 3726 : if (!ipa_agg_jump_functions_equivalent_p (&(*jf1->agg.items)[i],
6356 : 3726 : &(*jf2->agg.items)[i]))
6357 : : return false;
6358 : :
6359 : : return true;
6360 : : }
6361 : :
6362 : : #include "gt-ipa-prop.h"
|