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