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 : 17279606 : 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 : 17279606 : value_range vr;
88 : 17279606 : p->get_vrange (vr);
89 : 17279606 : inchash::hash hstate;
90 : 17279606 : add_vrange (vr, hstate);
91 : 17279606 : return hstate.end ();
92 : 17279606 : }
93 : : static bool
94 : 24103921 : equal (const ipa_vr *a, const vrange *b)
95 : : {
96 : 24103921 : return a->equal_p (*b);
97 : : }
98 : : static const bool empty_zero_p = true;
99 : : static void
100 : 1322 : mark_empty (ipa_vr *&p)
101 : : {
102 : 1322 : p = NULL;
103 : : }
104 : : static bool
105 : : is_empty (const ipa_vr *p)
106 : : {
107 : : return p == NULL;
108 : : }
109 : : static bool
110 : 44323066 : is_deleted (const ipa_vr *p)
111 : : {
112 : 44323066 : return p == reinterpret_cast<const ipa_vr *> (1);
113 : : }
114 : : static void
115 : 154981 : mark_deleted (ipa_vr *&p)
116 : : {
117 : 154981 : 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 : 649610 : ipa_vr::ipa_vr ()
145 : 649610 : : m_storage (NULL),
146 : 649610 : m_type (NULL)
147 : : {
148 : 649610 : }
149 : :
150 : 733697 : ipa_vr::ipa_vr (const vrange &r)
151 : 733697 : : m_storage (ggc_alloc_vrange_storage (r)),
152 : 733697 : m_type (r.type ())
153 : : {
154 : 733697 : }
155 : :
156 : : bool
157 : 24103921 : ipa_vr::equal_p (const vrange &r) const
158 : : {
159 : 24103921 : gcc_checking_assert (!r.undefined_p ());
160 : 24103921 : return (types_compatible_p (m_type, r.type ()) && m_storage->equal_p (r));
161 : : }
162 : :
163 : : bool
164 : 78026 : ipa_vr::equal_p (const ipa_vr &o) const
165 : : {
166 : 78026 : if (!known_p ())
167 : 0 : return !o.known_p ();
168 : :
169 : 78026 : if (!types_compatible_p (m_type, o.m_type))
170 : : return false;
171 : :
172 : 78026 : value_range r;
173 : 78026 : o.get_vrange (r);
174 : 78026 : return m_storage->equal_p (r);
175 : 78026 : }
176 : :
177 : : void
178 : 28419205 : ipa_vr::get_vrange (value_range &r) const
179 : : {
180 : 28419205 : r.set_type (m_type);
181 : 28419205 : m_storage->get_vrange (r, m_type);
182 : 28419205 : }
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 : 605666 : ipa_vr::streamer_read (lto_input_block *ib, data_in *data_in)
195 : : {
196 : 605666 : struct bitpack_d bp = streamer_read_bitpack (ib);
197 : 605666 : bool known = bp_unpack_value (&bp, 1);
198 : 605666 : if (known)
199 : : {
200 : 433422 : value_range vr;
201 : 433422 : streamer_read_value_range (ib, data_in, vr);
202 : 433422 : if (!m_storage || !m_storage->fits_p (vr))
203 : : {
204 : 433422 : if (m_storage)
205 : 0 : ggc_free (m_storage);
206 : 433422 : m_storage = ggc_alloc_vrange_storage (vr);
207 : : }
208 : 433422 : m_storage->set_vrange (vr);
209 : 433422 : m_type = vr.type ();
210 : 433422 : }
211 : : else
212 : : {
213 : 172244 : m_storage = NULL;
214 : 172244 : m_type = NULL;
215 : : }
216 : 605666 : }
217 : :
218 : : void
219 : 462053 : ipa_vr::streamer_write (output_block *ob) const
220 : : {
221 : 462053 : struct bitpack_d bp = bitpack_create (ob->main_stream);
222 : 462053 : bp_pack_value (&bp, !!m_storage, 1);
223 : 462053 : streamer_write_bitpack (&bp);
224 : 462053 : if (m_storage)
225 : : {
226 : 460766 : value_range vr (m_type);
227 : 460766 : m_storage->get_vrange (vr, m_type);
228 : 460766 : streamer_write_vrange (ob, vr);
229 : 460766 : }
230 : 462053 : }
231 : :
232 : : void
233 : 720 : ipa_vr::dump (FILE *out) const
234 : : {
235 : 720 : if (known_p ())
236 : : {
237 : 720 : value_range vr (m_type);
238 : 720 : m_storage->get_vrange (vr, m_type);
239 : 720 : vr.dump (out);
240 : 720 : }
241 : : else
242 : 0 : fprintf (out, "NO RANGE");
243 : 720 : }
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 : 85288 : ipa_return_value_sum_t (symbol_table *table, bool ggc):
274 : 170576 : function_summary <ipa_return_value_summary *> (table, ggc) { }
275 : :
276 : : /* Hook that is called by summary when a node is duplicated. */
277 : 542210 : void duplicate (cgraph_node *,
278 : : cgraph_node *,
279 : : ipa_return_value_summary *data,
280 : : ipa_return_value_summary *data2) final override
281 : : {
282 : 542210 : *data2=*data;
283 : 542210 : }
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 : 4065088 : ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
295 : : {
296 : 4065088 : tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
297 : :
298 : 4065088 : if (!fs_opts)
299 : : return false;
300 : 546260 : 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 : 42808719 : ipa_get_param_decl_index_1 (vec<ipa_param_descriptor, va_gc> *descriptors,
308 : : tree ptree)
309 : : {
310 : 42808719 : int i, count;
311 : :
312 : 42808719 : count = vec_safe_length (descriptors);
313 : 86996226 : for (i = 0; i < count; i++)
314 : 76537981 : 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 : 26642935 : ipa_get_param_decl_index (class ipa_node_params *info, tree ptree)
325 : : {
326 : 26642935 : 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 : 5593467 : ipa_populate_param_decls (struct cgraph_node *node,
338 : : vec<ipa_param_descriptor, va_gc> &descriptors)
339 : : {
340 : 5593467 : tree fndecl;
341 : 5593467 : tree fnargs;
342 : 5593467 : tree parm;
343 : 5593467 : int param_num;
344 : :
345 : 5593467 : fndecl = node->decl;
346 : 5593467 : gcc_assert (gimple_has_body_p (fndecl));
347 : 5593467 : fnargs = DECL_ARGUMENTS (fndecl);
348 : 5593467 : param_num = 0;
349 : 18308140 : for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
350 : : {
351 : 12714673 : descriptors[param_num].decl_or_type = parm;
352 : 12714673 : unsigned int cost = estimate_move_cost (TREE_TYPE (parm), true);
353 : 12714673 : descriptors[param_num].move_cost = cost;
354 : : /* Watch overflow, move_cost is a bitfield. */
355 : 12714673 : gcc_checking_assert (cost == descriptors[param_num].move_cost);
356 : 12714673 : param_num++;
357 : : }
358 : 5593467 : }
359 : :
360 : : /* Return how many formal parameters FNDECL has. */
361 : :
362 : : int
363 : 15297370 : count_formal_params (tree fndecl)
364 : : {
365 : 15297370 : tree parm;
366 : 15297370 : int count = 0;
367 : 15297370 : gcc_assert (gimple_has_body_p (fndecl));
368 : :
369 : 47069803 : for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
370 : 31772433 : count++;
371 : :
372 : 15297370 : 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 : 581 : ipa_dump_param (FILE *file, class ipa_node_params *info, int i)
381 : : {
382 : 581 : fprintf (file, "param #%i", i);
383 : 581 : if ((*info->descriptors)[i].decl_or_type)
384 : : {
385 : 581 : fprintf (file, " ");
386 : 581 : print_generic_expr (file, (*info->descriptors)[i].decl_or_type);
387 : : }
388 : 581 : }
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 : 6468876 : ipa_alloc_node_params (struct cgraph_node *node, int param_count)
395 : : {
396 : 6468876 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
397 : :
398 : 6468876 : if (!info->descriptors && param_count)
399 : : {
400 : 5644918 : vec_safe_grow_cleared (info->descriptors, param_count, true);
401 : 5644918 : 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 : 6392137 : ipa_initialize_node_params (struct cgraph_node *node)
413 : : {
414 : 6392137 : ipa_node_params *info = ipa_node_params_sum->get_create (node);
415 : :
416 : 6392137 : if (!info->descriptors
417 : 6392137 : && ipa_alloc_node_params (node, count_formal_params (node->decl)))
418 : 5592178 : ipa_populate_param_decls (node, *info->descriptors);
419 : 6392137 : }
420 : :
421 : : /* Print VAL which is extracted from a jump function to F. */
422 : :
423 : : void
424 : 1113 : ipa_print_constant_value (FILE *f, tree val)
425 : : {
426 : 1113 : print_generic_expr (f, val);
427 : :
428 : : /* This is in keeping with values_equal_for_ipcp_p. */
429 : 1113 : if (TREE_CODE (val) == ADDR_EXPR
430 : 1113 : && (TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL
431 : 249 : || (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 : 1113 : }
438 : :
439 : : /* Print contents of JFUNC to F. If CTX is non-NULL, dump it too. */
440 : :
441 : : DEBUG_FUNCTION void
442 : 1006 : ipa_dump_jump_function (FILE *f, ipa_jump_func *jump_func,
443 : : class ipa_polymorphic_call_context *ctx)
444 : : {
445 : 1006 : enum jump_func_type type = jump_func->type;
446 : :
447 : 1006 : if (type == IPA_JF_UNKNOWN)
448 : 240 : fprintf (f, "UNKNOWN\n");
449 : 766 : else if (type == IPA_JF_CONST)
450 : : {
451 : 303 : fprintf (f, "CONST: ");
452 : 303 : ipa_print_constant_value (f, jump_func->value.constant.value);
453 : 303 : 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 : 1006 : if (jump_func->agg.items)
490 : : {
491 : 90 : struct ipa_agg_jf_item *item;
492 : 90 : int j;
493 : :
494 : 180 : fprintf (f, " Aggregate passed by %s:\n",
495 : 90 : jump_func->agg.by_ref ? "reference" : "value");
496 : 373 : FOR_EACH_VEC_ELT (*jump_func->agg.items, j, item)
497 : : {
498 : 193 : fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
499 : : item->offset);
500 : 193 : fprintf (f, "type: ");
501 : 193 : print_generic_expr (f, item->type);
502 : 193 : fprintf (f, ", ");
503 : 193 : if (item->jftype == IPA_JF_PASS_THROUGH)
504 : 7 : fprintf (f, "PASS THROUGH: %d,",
505 : : item->value.pass_through.formal_id);
506 : 186 : 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 : 193 : if (item->jftype == IPA_JF_PASS_THROUGH
517 : 193 : || item->jftype == IPA_JF_LOAD_AGG)
518 : : {
519 : 18 : fprintf (f, " op %s",
520 : : get_tree_code_name (item->value.pass_through.operation));
521 : 18 : 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 : 175 : else if (item->jftype == IPA_JF_CONST)
532 : : {
533 : 175 : fprintf (f, "CONST: ");
534 : 175 : 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 : 193 : fprintf (f, "\n");
540 : : }
541 : : }
542 : :
543 : 1006 : if (ctx && !ctx->useless_p ())
544 : : {
545 : 378 : fprintf (f, " Context: ");
546 : 378 : ctx->dump (dump_file);
547 : : }
548 : :
549 : 1006 : if (jump_func->m_vr)
550 : : {
551 : 720 : fprintf (f, " ");
552 : 720 : jump_func->m_vr->dump (f);
553 : 720 : fprintf (f, "\n");
554 : : }
555 : : else
556 : 286 : fprintf (f, " Unknown VR\n");
557 : 1006 : }
558 : :
559 : : /* Print the jump functions associated with call graph edge CS to file F. */
560 : :
561 : : static void
562 : 712 : ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
563 : : {
564 : 712 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
565 : 712 : int count = ipa_get_cs_argument_count (args);
566 : :
567 : 1718 : for (int i = 0; i < count; i++)
568 : : {
569 : 1006 : struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
570 : 1006 : class ipa_polymorphic_call_context *ctx
571 : 1006 : = ipa_get_ith_polymorhic_call_context (args, i);
572 : :
573 : 1006 : fprintf (f, " param %d: ", i);
574 : 1006 : ipa_dump_jump_function (f, jump_func, ctx);
575 : : }
576 : 712 : }
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 : 1063 : ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
584 : : {
585 : 1063 : struct cgraph_edge *cs;
586 : :
587 : 1063 : fprintf (f, " Jump functions of caller %s:\n", node->dump_name ());
588 : 2110 : for (cs = node->callees; cs; cs = cs->next_callee)
589 : : {
590 : :
591 : 1047 : fprintf (f, " callsite %s -> %s : \n",
592 : : node->dump_name (),
593 : 1047 : cs->callee->dump_name ());
594 : 1047 : if (!ipa_edge_args_info_available_for_edge_p (cs))
595 : 482 : fprintf (f, " no arg info\n");
596 : : else
597 : 565 : ipa_print_node_jump_functions_for_edge (f, cs);
598 : : }
599 : :
600 : 1210 : 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 : 1063 : }
632 : :
633 : : /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
634 : :
635 : : void
636 : 159 : ipa_print_all_jump_functions (FILE *f)
637 : : {
638 : 159 : struct cgraph_node *node;
639 : :
640 : 159 : fprintf (f, "\nJump functions:\n");
641 : 2416 : FOR_EACH_FUNCTION (node)
642 : : {
643 : 1049 : ipa_print_node_jump_functions (f, node);
644 : : }
645 : 159 : }
646 : :
647 : : /* Set jfunc to be a know-really nothing jump function. */
648 : :
649 : : static void
650 : 499442 : ipa_set_jf_unknown (struct ipa_jump_func *jfunc)
651 : : {
652 : 499442 : jfunc->type = IPA_JF_UNKNOWN;
653 : 485472 : }
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 : 981564 : ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
660 : : struct ipa_jump_func *src)
661 : :
662 : : {
663 : 981564 : gcc_checking_assert (src->type == IPA_JF_CONST);
664 : 981564 : dst->type = IPA_JF_CONST;
665 : 981564 : dst->value.constant = src->value.constant;
666 : 981564 : }
667 : :
668 : : /* Set JFUNC to be a constant jmp function. */
669 : :
670 : : static void
671 : 2733264 : ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
672 : : struct cgraph_edge *cs)
673 : : {
674 : 2733264 : jfunc->type = IPA_JF_CONST;
675 : 2733264 : jfunc->value.constant.value = unshare_expr_without_location (constant);
676 : :
677 : 2733264 : if (TREE_CODE (constant) == ADDR_EXPR
678 : 2733264 : && (TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL
679 : 972432 : || (VAR_P (TREE_OPERAND (constant, 0))
680 : 345671 : && TREE_STATIC (TREE_OPERAND (constant, 0)))))
681 : : {
682 : 385032 : struct ipa_cst_ref_desc *rdesc;
683 : :
684 : 385032 : rdesc = ipa_refdesc_pool.allocate ();
685 : 385032 : rdesc->cs = cs;
686 : 385032 : rdesc->next_duplicate = NULL;
687 : 385032 : rdesc->refcount = 1;
688 : 385032 : jfunc->value.constant.rdesc = rdesc;
689 : : }
690 : : else
691 : 2348232 : jfunc->value.constant.rdesc = NULL;
692 : 2733264 : }
693 : :
694 : : /* Set JFUNC to be a simple pass-through jump function. */
695 : : static void
696 : 1394737 : ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
697 : : bool agg_preserved)
698 : : {
699 : 1394737 : jfunc->type = IPA_JF_PASS_THROUGH;
700 : 1394737 : jfunc->value.pass_through.operand = NULL_TREE;
701 : 1394737 : jfunc->value.pass_through.op_type = NULL_TREE;
702 : 1394737 : jfunc->value.pass_through.formal_id = formal_id;
703 : 1394737 : jfunc->value.pass_through.operation = NOP_EXPR;
704 : 1394737 : jfunc->value.pass_through.agg_preserved = agg_preserved;
705 : 1394737 : jfunc->value.pass_through.refdesc_decremented = false;
706 : 1257765 : }
707 : :
708 : : /* Set JFUNC to be an unary pass through jump function. */
709 : :
710 : : static void
711 : 1024 : ipa_set_jf_unary_pass_through (struct ipa_jump_func *jfunc, int formal_id,
712 : : enum tree_code operation, tree op_type)
713 : : {
714 : 1024 : jfunc->type = IPA_JF_PASS_THROUGH;
715 : 1024 : jfunc->value.pass_through.operand = NULL_TREE;
716 : 1024 : jfunc->value.pass_through.op_type = op_type;
717 : 1024 : jfunc->value.pass_through.formal_id = formal_id;
718 : 1024 : jfunc->value.pass_through.operation = operation;
719 : 1024 : jfunc->value.pass_through.agg_preserved = false;
720 : 1024 : jfunc->value.pass_through.refdesc_decremented = false;
721 : 1024 : }
722 : : /* Set JFUNC to be an arithmetic pass through jump function. */
723 : :
724 : : static void
725 : 41669 : 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 : 41669 : jfunc->type = IPA_JF_PASS_THROUGH;
730 : 0 : jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
731 : 41669 : jfunc->value.pass_through.op_type = op_type;
732 : 41669 : jfunc->value.pass_through.formal_id = formal_id;
733 : 41669 : jfunc->value.pass_through.operation = operation;
734 : 41669 : jfunc->value.pass_through.agg_preserved = false;
735 : 41669 : jfunc->value.pass_through.refdesc_decremented = false;
736 : 41669 : }
737 : :
738 : : /* Set JFUNC to be an ancestor jump function. */
739 : :
740 : : static void
741 : 188764 : 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 : 188764 : jfunc->type = IPA_JF_ANCESTOR;
745 : 188764 : jfunc->value.ancestor.formal_id = formal_id;
746 : 188764 : jfunc->value.ancestor.offset = offset;
747 : 188764 : jfunc->value.ancestor.agg_preserved = agg_preserved;
748 : 188764 : jfunc->value.ancestor.keep_null = keep_null;
749 : 188132 : }
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 : 33952843 : ipa_get_bb_info (struct ipa_func_body_info *fbi, basic_block bb)
756 : : {
757 : 28412719 : gcc_checking_assert (fbi);
758 : 33952843 : 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 : 10392 : 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 : 10392 : 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 : 10392 : if (DECL_STRUCT_FUNCTION (function)->after_inlining)
888 : : return true;
889 : 10392 : if (TREE_CODE (arg) == SSA_NAME
890 : 10392 : && SSA_NAME_IS_DEFAULT_DEF (arg)
891 : 20784 : && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
892 : : {
893 : : /* Normal (non-THIS) argument. */
894 : 10392 : if ((SSA_NAME_VAR (arg) != DECL_ARGUMENTS (function)
895 : 9747 : || 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 : 19551 : || (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
900 : 9159 : && !DECL_CXX_CONSTRUCTOR_P (function)
901 : 9158 : && !DECL_CXX_DESTRUCTOR_P (function)
902 : 9157 : && (SSA_NAME_VAR (arg) == DECL_ARGUMENTS (function))))
903 : : {
904 : : /* Walk the inline stack and watch out for ctors/dtors. */
905 : 46058 : for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
906 : 17834 : block = BLOCK_SUPERCONTEXT (block))
907 : 17849 : 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 : 9621 : detect_type_change_ssa (ipa_func_body_info *fbi, tree arg, tree comp_type,
1003 : : gcall *call)
1004 : : {
1005 : 9621 : gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
1006 : 9621 : if (!flag_devirtualize
1007 : 9621 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
1008 : : return false;
1009 : :
1010 : 9621 : 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 : 1676101 : mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
1025 : : void *data)
1026 : : {
1027 : 1676101 : bool *b = (bool *) data;
1028 : 1676101 : *b = true;
1029 : 1676101 : 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 : 4503322 : find_dominating_aa_status (struct ipa_func_body_info *fbi, basic_block bb,
1037 : : int index)
1038 : : {
1039 : 13536713 : while (true)
1040 : : {
1041 : 13536713 : bb = get_immediate_dominator (CDI_DOMINATORS, bb);
1042 : 13536713 : if (!bb)
1043 : : return NULL;
1044 : 10481796 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1045 : 12600430 : if (!bi->param_aa_statuses.is_empty ()
1046 : 2118634 : && bi->param_aa_statuses[index].valid)
1047 : 1448405 : 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 : 6771049 : parm_bb_aa_status_for_bb (struct ipa_func_body_info *fbi, basic_block bb,
1057 : : int index)
1058 : : {
1059 : 6771049 : gcc_checking_assert (fbi);
1060 : 6771049 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1061 : 6771049 : if (bi->param_aa_statuses.is_empty ())
1062 : 3862512 : bi->param_aa_statuses.safe_grow_cleared (fbi->param_count, true);
1063 : 6771049 : struct ipa_param_aa_status *paa = &bi->param_aa_statuses[index];
1064 : 6771049 : if (!paa->valid)
1065 : : {
1066 : 4503322 : gcc_checking_assert (!paa->parm_modified
1067 : : && !paa->ref_modified
1068 : : && !paa->pt_modified);
1069 : 4503322 : struct ipa_param_aa_status *dom_paa;
1070 : 4503322 : dom_paa = find_dominating_aa_status (fbi, bb, index);
1071 : 4503322 : if (dom_paa)
1072 : 1448405 : *paa = *dom_paa;
1073 : : else
1074 : 3054917 : paa->valid = true;
1075 : : }
1076 : :
1077 : 6771049 : 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 : 1195935 : parm_preserved_before_stmt_p (struct ipa_func_body_info *fbi, int index,
1087 : : gimple *stmt, tree parm_load)
1088 : : {
1089 : 1195935 : struct ipa_param_aa_status *paa;
1090 : 1195935 : bool modified = false;
1091 : 1195935 : ao_ref refd;
1092 : :
1093 : 1195935 : tree base = get_base_address (parm_load);
1094 : 1195935 : gcc_assert (TREE_CODE (base) == PARM_DECL);
1095 : 1195935 : if (TREE_READONLY (base))
1096 : : return true;
1097 : :
1098 : 1119508 : gcc_checking_assert (fbi);
1099 : 1119508 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1100 : 1119508 : if (paa->parm_modified || fbi->aa_walk_budget == 0)
1101 : : return false;
1102 : :
1103 : 1904536 : gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
1104 : 952268 : ao_ref_init (&refd, parm_load);
1105 : 1904536 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1106 : : &modified, NULL, NULL,
1107 : : fbi->aa_walk_budget);
1108 : 952268 : if (walked < 0)
1109 : : {
1110 : 8 : modified = true;
1111 : 8 : fbi->aa_walk_budget = 0;
1112 : : }
1113 : : else
1114 : 952260 : fbi->aa_walk_budget -= walked;
1115 : 952268 : if (paa && modified)
1116 : 113573 : paa->parm_modified = true;
1117 : 952268 : 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 : 6587761 : load_from_unmodified_param (struct ipa_func_body_info *fbi,
1126 : : vec<ipa_param_descriptor, va_gc> *descriptors,
1127 : : gimple *stmt)
1128 : : {
1129 : 6587761 : int index;
1130 : 6587761 : tree op1;
1131 : :
1132 : 6587761 : if (!gimple_assign_single_p (stmt))
1133 : : return -1;
1134 : :
1135 : 4397434 : op1 = gimple_assign_rhs1 (stmt);
1136 : 4397434 : if (TREE_CODE (op1) != PARM_DECL)
1137 : : return -1;
1138 : :
1139 : 70157 : index = ipa_get_param_decl_index_1 (descriptors, op1);
1140 : 70157 : if (index < 0
1141 : 70157 : || !parm_preserved_before_stmt_p (fbi, index, stmt, op1))
1142 : 25493 : 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 : 4819011 : parm_ref_data_preserved_p (struct ipa_func_body_info *fbi,
1153 : : int index, gimple *stmt, tree ref)
1154 : : {
1155 : 4819011 : struct ipa_param_aa_status *paa;
1156 : 4819011 : bool modified = false;
1157 : 4819011 : ao_ref refd;
1158 : :
1159 : 4819011 : gcc_checking_assert (fbi);
1160 : 4819011 : paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
1161 : 4819011 : if (paa->ref_modified || fbi->aa_walk_budget == 0)
1162 : : return false;
1163 : :
1164 : 7500756 : gcc_checking_assert (gimple_vuse (stmt));
1165 : 3750378 : ao_ref_init (&refd, ref);
1166 : 7500756 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
1167 : : &modified, NULL, NULL,
1168 : : fbi->aa_walk_budget);
1169 : 3750378 : if (walked < 0)
1170 : : {
1171 : 8 : modified = true;
1172 : 8 : fbi->aa_walk_budget = 0;
1173 : : }
1174 : : else
1175 : 3750370 : fbi->aa_walk_budget -= walked;
1176 : 3750378 : if (modified)
1177 : 632197 : paa->ref_modified = true;
1178 : 3750378 : 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 : 1168968 : parm_ref_data_pass_through_p (struct ipa_func_body_info *fbi, int index,
1187 : : gimple *call, tree parm)
1188 : : {
1189 : 1168968 : bool modified = false;
1190 : 1168968 : 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 : 1616683 : if (!gimple_vuse (call)
1196 : 1168968 : || !POINTER_TYPE_P (TREE_TYPE (parm)))
1197 : : return false;
1198 : :
1199 : 832530 : struct ipa_param_aa_status *paa = parm_bb_aa_status_for_bb (fbi,
1200 : : gimple_bb (call),
1201 : : index);
1202 : 832530 : if (paa->pt_modified || fbi->aa_walk_budget == 0)
1203 : : return false;
1204 : :
1205 : 721253 : ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
1206 : 1442506 : int walked = walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified,
1207 : : &modified, NULL, NULL,
1208 : : fbi->aa_walk_budget);
1209 : 721253 : if (walked < 0)
1210 : : {
1211 : 0 : fbi->aa_walk_budget = 0;
1212 : 0 : modified = true;
1213 : : }
1214 : : else
1215 : 721253 : fbi->aa_walk_budget -= walked;
1216 : 721253 : if (modified)
1217 : 268172 : paa->pt_modified = true;
1218 : 721253 : 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 : 25457294 : 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 : 25457294 : int index;
1247 : 25457294 : HOST_WIDE_INT size;
1248 : 25457294 : bool reverse;
1249 : 25457294 : tree base = get_ref_base_and_extent_hwi (op, offset_p, &size, &reverse);
1250 : :
1251 : 25457294 : if (!base
1252 : 24088785 : || (*offset_p / BITS_PER_UNIT) > UINT_MAX)
1253 : : return false;
1254 : :
1255 : : /* We can not propagate across volatile loads. */
1256 : 24088754 : if (TREE_THIS_VOLATILE (op))
1257 : : return false;
1258 : :
1259 : 22880606 : if (DECL_P (base))
1260 : : {
1261 : 11162286 : int index = ipa_get_param_decl_index_1 (descriptors, base);
1262 : 11162286 : if (index >= 0
1263 : 11162286 : && parm_preserved_before_stmt_p (fbi, index, stmt, op))
1264 : : {
1265 : 802498 : *index_p = index;
1266 : 802498 : *by_ref_p = false;
1267 : 802498 : if (size_p)
1268 : 22712 : *size_p = size;
1269 : 802498 : if (guaranteed_unmodified)
1270 : 137 : *guaranteed_unmodified = true;
1271 : 802498 : return true;
1272 : : }
1273 : 10359788 : return false;
1274 : : }
1275 : :
1276 : 11718320 : if (TREE_CODE (base) != MEM_REF
1277 : 11010078 : || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1278 : 22724740 : || !integer_zerop (TREE_OPERAND (base, 1)))
1279 : 1695011 : return false;
1280 : :
1281 : 10023309 : if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1282 : : {
1283 : 4933341 : tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1284 : 4933341 : 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 : 5089968 : gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
1304 : 5089968 : index = load_from_unmodified_param (fbi, descriptors, def);
1305 : : }
1306 : :
1307 : 10023309 : if (index >= 0)
1308 : : {
1309 : 4818704 : bool data_preserved = parm_ref_data_preserved_p (fbi, index, stmt, op);
1310 : 4818704 : if (!data_preserved && !guaranteed_unmodified)
1311 : : return false;
1312 : :
1313 : 3118390 : *index_p = index;
1314 : 3118390 : *by_ref_p = true;
1315 : 3118390 : if (size_p)
1316 : 29095 : *size_p = size;
1317 : 3118390 : if (guaranteed_unmodified)
1318 : 2203 : *guaranteed_unmodified = data_preserved;
1319 : 3118390 : 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 : 357582 : 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 : 357582 : int index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1342 : 357582 : poly_int64 size;
1343 : :
1344 : : /* Load value from a parameter declaration. */
1345 : 357582 : if (index >= 0)
1346 : : {
1347 : 196 : *offset_p = -1;
1348 : 196 : return index;
1349 : : }
1350 : :
1351 : 357386 : if (!gimple_assign_load_p (stmt))
1352 : : return -1;
1353 : :
1354 : 181953 : tree rhs = gimple_assign_rhs1 (stmt);
1355 : :
1356 : : /* Skip memory reference containing VIEW_CONVERT_EXPR. */
1357 : 329525 : for (tree t = rhs; handled_component_p (t); t = TREE_OPERAND (t, 0))
1358 : 147597 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
1359 : : return -1;
1360 : :
1361 : : /* Skip memory reference containing bit-field. */
1362 : 181928 : if (TREE_CODE (rhs) == BIT_FIELD_REF
1363 : 181928 : || contains_bitfld_component_ref_p (rhs))
1364 : 0 : return -1;
1365 : :
1366 : 181928 : if (!ipa_load_from_parm_agg (fbi, info->descriptors, stmt, rhs, &index,
1367 : : offset_p, &size, by_ref_p))
1368 : : return -1;
1369 : :
1370 : 49026 : gcc_assert (!maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (rhs))),
1371 : : size));
1372 : 49026 : if (!*by_ref_p)
1373 : : {
1374 : 22157 : tree param_type = ipa_get_type (info, index);
1375 : :
1376 : 22157 : if (!param_type || !AGGREGATE_TYPE_P (param_type))
1377 : : return -1;
1378 : : }
1379 : 26869 : else if (TREE_THIS_VOLATILE (rhs))
1380 : : return -1;
1381 : :
1382 : 47533 : 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 : 15864384 : unadjusted_ptr_and_unit_offset (tree op, tree *ret, poly_int64 *offset_ret)
1392 : : {
1393 : 15864384 : poly_int64 offset = 0;
1394 : 15864384 : bool offset_known = true;
1395 : 15864384 : int i;
1396 : :
1397 : 20500184 : for (i = 0; i < param_ipa_jump_function_lookups; i++)
1398 : : {
1399 : 20499012 : if (TREE_CODE (op) == ADDR_EXPR)
1400 : : {
1401 : 1570636 : poly_int64 extra_offset;
1402 : 1570636 : tree base = get_addr_base_and_unit_offset (TREE_OPERAND (op, 0),
1403 : : &extra_offset);
1404 : 1570636 : if (!base)
1405 : : {
1406 : 26940 : base = get_base_address (TREE_OPERAND (op, 0));
1407 : 26940 : if (TREE_CODE (base) != MEM_REF)
1408 : : break;
1409 : : offset_known = false;
1410 : : }
1411 : : else
1412 : : {
1413 : 1543696 : if (TREE_CODE (base) != MEM_REF)
1414 : : break;
1415 : 258744 : offset += extra_offset;
1416 : : }
1417 : 258744 : op = TREE_OPERAND (base, 0);
1418 : 258744 : if (mem_ref_offset (base).to_shwi (&extra_offset))
1419 : 258744 : offset += extra_offset;
1420 : : else
1421 : : offset_known = false;
1422 : : }
1423 : 18928376 : else if (TREE_CODE (op) == SSA_NAME
1424 : 18928376 : && !SSA_NAME_IS_DEFAULT_DEF (op))
1425 : : {
1426 : 6507858 : gimple *pstmt = SSA_NAME_DEF_STMT (op);
1427 : :
1428 : 6507858 : if (gimple_assign_single_p (pstmt))
1429 : 3182932 : op = gimple_assign_rhs1 (pstmt);
1430 : 3324926 : else if (is_gimple_assign (pstmt)
1431 : 3324926 : && gimple_assign_rhs_code (pstmt) == POINTER_PLUS_EXPR)
1432 : : {
1433 : 1194124 : poly_int64 extra_offset = 0;
1434 : 1194124 : if (ptrdiff_tree_p (gimple_assign_rhs2 (pstmt),
1435 : : &extra_offset))
1436 : 1194124 : offset += extra_offset;
1437 : : else
1438 : : offset_known = false;
1439 : 1194124 : op = gimple_assign_rhs1 (pstmt);
1440 : : }
1441 : : else
1442 : : break;
1443 : : }
1444 : : else
1445 : : break;
1446 : : }
1447 : 15864384 : *ret = op;
1448 : 15864384 : *offset_ret = offset;
1449 : 15864384 : 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 : 1250975 : 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 : 1250975 : HOST_WIDE_INT offset, size;
1513 : 1250975 : tree op1, tc_ssa, base, ssa;
1514 : 1250975 : bool reverse;
1515 : 1250975 : int index;
1516 : :
1517 : 1250975 : op1 = gimple_assign_rhs1 (stmt);
1518 : :
1519 : 1250975 : if (TREE_CODE (op1) == SSA_NAME)
1520 : : {
1521 : 380100 : if (SSA_NAME_IS_DEFAULT_DEF (op1))
1522 : 110764 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1523 : : else
1524 : 269336 : index = load_from_unmodified_param (fbi, info->descriptors,
1525 : 269336 : SSA_NAME_DEF_STMT (op1));
1526 : : tc_ssa = op1;
1527 : : }
1528 : : else
1529 : : {
1530 : 870875 : index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1531 : 870875 : tc_ssa = gimple_assign_lhs (stmt);
1532 : : }
1533 : :
1534 : 1250975 : if (index >= 0)
1535 : : {
1536 : 111824 : if (lto_variably_modified_type_p (TREE_TYPE (name)))
1537 : 1083878 : return;
1538 : :
1539 : 111778 : switch (gimple_assign_rhs_class (stmt))
1540 : : {
1541 : 63882 : case GIMPLE_BINARY_RHS:
1542 : 63882 : {
1543 : 63882 : tree op2 = gimple_assign_rhs2 (stmt);
1544 : 63882 : if (!is_gimple_ip_invariant (op2)
1545 : 63882 : || ((TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
1546 : : != tcc_comparison)
1547 : 32709 : && !useless_type_conversion_p (TREE_TYPE (name),
1548 : 32709 : TREE_TYPE (op1))))
1549 : 23742 : return;
1550 : :
1551 : 40140 : ipa_set_jf_arith_pass_through (jfunc, index, op2,
1552 : : gimple_assign_rhs_code (stmt),
1553 : 40140 : TREE_TYPE (name));
1554 : 40140 : 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 : 47065 : case GIMPLE_UNARY_RHS:
1564 : 47065 : if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
1565 : 996 : ipa_set_jf_unary_pass_through (jfunc, index,
1566 : : gimple_assign_rhs_code (stmt),
1567 : 996 : TREE_TYPE (name));
1568 : 88036 : default:;
1569 : : }
1570 : 88036 : return;
1571 : : }
1572 : :
1573 : 1139151 : if (TREE_CODE (op1) != ADDR_EXPR)
1574 : : return;
1575 : 240737 : op1 = TREE_OPERAND (op1, 0);
1576 : 240737 : base = get_ref_base_and_extent_hwi (op1, &offset, &size, &reverse);
1577 : 240737 : offset_int mem_offset;
1578 : 240737 : if (!base
1579 : 214260 : || TREE_CODE (base) != MEM_REF
1580 : 442279 : || !mem_ref_offset (base).is_constant (&mem_offset))
1581 : 39195 : return;
1582 : 201542 : offset += mem_offset.to_short_addr () * BITS_PER_UNIT;
1583 : 201542 : ssa = TREE_OPERAND (base, 0);
1584 : 201542 : if (TREE_CODE (ssa) != SSA_NAME
1585 : 201542 : || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1586 : 368738 : || offset < 0)
1587 : : return;
1588 : :
1589 : : /* Dynamic types are changed in constructors and destructors. */
1590 : 167097 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1591 : 167097 : if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1592 : 164248 : ipa_set_ancestor_jf (jfunc, offset, index,
1593 : 164248 : 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 : 18267 : get_ancestor_addr_info (gimple *assign, tree *obj_p, HOST_WIDE_INT *offset)
1610 : : {
1611 : 18267 : HOST_WIDE_INT size;
1612 : 18267 : tree expr, parm, obj;
1613 : 18267 : bool reverse;
1614 : :
1615 : 18267 : if (!gimple_assign_single_p (assign))
1616 : : return NULL_TREE;
1617 : 10686 : expr = gimple_assign_rhs1 (assign);
1618 : :
1619 : 10686 : if (TREE_CODE (expr) != ADDR_EXPR)
1620 : : return NULL_TREE;
1621 : 4433 : expr = TREE_OPERAND (expr, 0);
1622 : 4433 : obj = expr;
1623 : 4433 : expr = get_ref_base_and_extent_hwi (expr, offset, &size, &reverse);
1624 : :
1625 : 4433 : offset_int mem_offset;
1626 : 4433 : if (!expr
1627 : 4431 : || TREE_CODE (expr) != MEM_REF
1628 : 8864 : || !mem_ref_offset (expr).is_constant (&mem_offset))
1629 : 2 : return NULL_TREE;
1630 : 4431 : parm = TREE_OPERAND (expr, 0);
1631 : 4431 : if (TREE_CODE (parm) != SSA_NAME
1632 : 4431 : || !SSA_NAME_IS_DEFAULT_DEF (parm)
1633 : 5228 : || 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 : 99167 : 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 : 99167 : HOST_WIDE_INT offset;
1670 : 99167 : gimple *assign;
1671 : 99167 : basic_block phi_bb, assign_bb, cond_bb;
1672 : 99167 : tree tmp, parm, expr, obj;
1673 : 99167 : int index, i;
1674 : :
1675 : 99167 : if (gimple_phi_num_args (phi) != 2)
1676 : 99153 : return;
1677 : :
1678 : 76843 : if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1679 : 3931 : tmp = PHI_ARG_DEF (phi, 0);
1680 : 72912 : else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1681 : 13214 : tmp = PHI_ARG_DEF (phi, 1);
1682 : : else
1683 : : return;
1684 : 17145 : if (TREE_CODE (tmp) != SSA_NAME
1685 : 15009 : || SSA_NAME_IS_DEFAULT_DEF (tmp)
1686 : 14896 : || !POINTER_TYPE_P (TREE_TYPE (tmp))
1687 : 20169 : || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1688 : : return;
1689 : :
1690 : 992 : assign = SSA_NAME_DEF_STMT (tmp);
1691 : 992 : assign_bb = gimple_bb (assign);
1692 : 99875 : if (!single_pred_p (assign_bb))
1693 : : return;
1694 : 722 : expr = get_ancestor_addr_info (assign, &obj, &offset);
1695 : 722 : 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 : 890 : type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1730 : : {
1731 : 890 : tree fld;
1732 : :
1733 : 890 : if (TREE_CODE (type) != RECORD_TYPE)
1734 : : return false;
1735 : :
1736 : 890 : fld = TYPE_FIELDS (type);
1737 : 890 : if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1738 : 890 : || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1739 : 1780 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1740 : : return false;
1741 : :
1742 : 890 : if (method_ptr)
1743 : 890 : *method_ptr = fld;
1744 : :
1745 : 890 : fld = DECL_CHAIN (fld);
1746 : 890 : if (!fld || INTEGRAL_TYPE_P (fld)
1747 : 1780 : || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1748 : : return false;
1749 : 890 : if (delta)
1750 : 890 : *delta = fld;
1751 : :
1752 : 890 : 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 : 123190 : get_ssa_def_if_simple_copy (tree rhs, gimple **rhs_stmt)
1764 : : {
1765 : 161728 : while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1766 : : {
1767 : 87723 : gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
1768 : :
1769 : 87723 : if (gimple_assign_single_p (def_stmt))
1770 : 38538 : rhs = gimple_assign_rhs1 (def_stmt);
1771 : : else
1772 : : break;
1773 : 38538 : *rhs_stmt = def_stmt;
1774 : : }
1775 : 123190 : 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 : 2757302 : add_to_agg_contents_list (struct ipa_known_agg_contents_list **plist,
1801 : : struct ipa_known_agg_contents_list *item)
1802 : : {
1803 : 2757302 : struct ipa_known_agg_contents_list *list = *plist;
1804 : :
1805 : 5070747 : for (; list; list = list->next)
1806 : : {
1807 : 3863620 : if (list->offset >= item->offset)
1808 : : break;
1809 : :
1810 : 2313445 : plist = &list->next;
1811 : : }
1812 : :
1813 : 2757302 : item->next = list;
1814 : 2757302 : *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 : 1065903 : clobber_by_agg_contents_list_p (struct ipa_known_agg_contents_list *list,
1822 : : struct ipa_known_agg_contents_list *item)
1823 : : {
1824 : 2267750 : for (; list; list = list->next)
1825 : : {
1826 : 1835541 : if (list->offset >= item->offset)
1827 : 621127 : return list->offset < item->offset + item->size;
1828 : :
1829 : 1214414 : 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 : 344809 : 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 : 344809 : vec_safe_reserve (jfunc->agg.items, value_count, true);
1846 : 1350467 : for (; list; list = list->next)
1847 : : {
1848 : 1005658 : struct ipa_agg_jf_item item;
1849 : 1005658 : tree operand = list->value.pass_through.operand;
1850 : :
1851 : 1005658 : if (list->value.pass_through.formal_id >= 0)
1852 : : {
1853 : : /* Content value is derived from some formal paramerter. */
1854 : 87490 : if (list->value.offset >= 0)
1855 : 46275 : item.jftype = IPA_JF_LOAD_AGG;
1856 : : else
1857 : 41215 : item.jftype = IPA_JF_PASS_THROUGH;
1858 : :
1859 : 87490 : item.value.load_agg = list->value;
1860 : 87490 : if (operand)
1861 : 8914 : item.value.pass_through.operand
1862 : 8914 : = unshare_expr_without_location (operand);
1863 : : }
1864 : 918168 : else if (operand)
1865 : : {
1866 : : /* Content value is known constant. */
1867 : 918168 : item.jftype = IPA_JF_CONST;
1868 : 918168 : item.value.constant = unshare_expr_without_location (operand);
1869 : : }
1870 : : else
1871 : 0 : continue;
1872 : :
1873 : 1005658 : item.type = list->type;
1874 : 1005658 : gcc_assert (tree_to_shwi (TYPE_SIZE (list->type)) == list->size);
1875 : :
1876 : 1005658 : item.offset = list->offset - arg_offset;
1877 : 1005658 : gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1878 : :
1879 : 1005658 : jfunc->agg.items->quick_push (item);
1880 : : }
1881 : 344809 : }
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 : 1753863 : analyze_agg_content_value (struct ipa_func_body_info *fbi,
1910 : : struct ipa_load_agg_data *agg_value,
1911 : : gimple *stmt)
1912 : : {
1913 : 1753863 : tree lhs = gimple_assign_lhs (stmt);
1914 : 1753863 : tree rhs1 = gimple_assign_rhs1 (stmt);
1915 : 1753863 : enum tree_code code;
1916 : 1753863 : int index = -1;
1917 : :
1918 : : /* Initialize jump function data for the aggregate part. */
1919 : 1753863 : memset (agg_value, 0, sizeof (*agg_value));
1920 : 1753863 : agg_value->pass_through.operation = NOP_EXPR;
1921 : 1753863 : agg_value->pass_through.formal_id = -1;
1922 : 1753863 : agg_value->offset = -1;
1923 : :
1924 : 1753863 : if (AGGREGATE_TYPE_P (TREE_TYPE (lhs)) /* TODO: Support aggregate type. */
1925 : 1561351 : || TREE_THIS_VOLATILE (lhs)
1926 : 1560691 : || TREE_CODE (lhs) == BIT_FIELD_REF
1927 : 1560683 : || contains_bitfld_component_ref_p (lhs))
1928 : 205864 : return;
1929 : :
1930 : : /* Skip SSA copies. */
1931 : 1823332 : while (gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS)
1932 : : {
1933 : 1735029 : if (TREE_CODE (rhs1) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (rhs1))
1934 : : break;
1935 : :
1936 : 375085 : stmt = SSA_NAME_DEF_STMT (rhs1);
1937 : 375085 : if (!is_gimple_assign (stmt))
1938 : : break;
1939 : :
1940 : 275333 : lhs = gimple_assign_lhs (stmt);
1941 : 275333 : rhs1 = gimple_assign_rhs1 (stmt);
1942 : : }
1943 : :
1944 : 1547999 : 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 : 39938 : if (gimple_phi_num_args (phi) != 2
1961 : 39938 : || lto_variably_modified_type_p (TREE_TYPE (lhs)))
1962 : 12643 : return;
1963 : 27295 : tree arg0 = gimple_phi_arg_def (phi, 0);
1964 : 27295 : tree arg1 = gimple_phi_arg_def (phi, 1);
1965 : 27295 : tree operand;
1966 : :
1967 : 27295 : if (is_gimple_ip_invariant (arg1))
1968 : : {
1969 : : operand = arg1;
1970 : : rhs1 = arg0;
1971 : : }
1972 : 22322 : else if (is_gimple_ip_invariant (arg0))
1973 : : {
1974 : : operand = arg0;
1975 : : rhs1 = arg1;
1976 : : }
1977 : : else
1978 : : return;
1979 : :
1980 : 9548 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
1981 : 9548 : if (!is_gimple_assign (stmt))
1982 : : return;
1983 : :
1984 : 3720 : code = ASSERT_EXPR;
1985 : 3720 : agg_value->pass_through.operand = operand;
1986 : 3720 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
1987 : : }
1988 : 1508061 : else if (is_gimple_assign (stmt))
1989 : : {
1990 : 1448247 : code = gimple_assign_rhs_code (stmt);
1991 : 1448247 : switch (gimple_assign_rhs_class (stmt))
1992 : : {
1993 : 1359944 : case GIMPLE_SINGLE_RHS:
1994 : 1359944 : if (is_gimple_ip_invariant (rhs1))
1995 : : {
1996 : 976863 : agg_value->pass_through.operand = rhs1;
1997 : 976863 : return;
1998 : : }
1999 : : code = NOP_EXPR;
2000 : : break;
2001 : :
2002 : 32049 : 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 : 32049 : if (TREE_CODE_CLASS (code) != tcc_unary || CONVERT_EXPR_CODE_P (code)
2012 : 34051 : || lto_variably_modified_type_p (TREE_TYPE (lhs)))
2013 : 30047 : return;
2014 : :
2015 : 2002 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &stmt);
2016 : 2002 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2017 : 2002 : break;
2018 : :
2019 : 55824 : case GIMPLE_BINARY_RHS:
2020 : 55824 : {
2021 : 55824 : gimple *rhs1_stmt = stmt;
2022 : 55824 : gimple *rhs2_stmt = stmt;
2023 : 55824 : tree rhs2 = gimple_assign_rhs2 (stmt);
2024 : :
2025 : 55824 : if (lto_variably_modified_type_p (TREE_TYPE (lhs)))
2026 : 27570 : return;
2027 : :
2028 : 55820 : rhs1 = get_ssa_def_if_simple_copy (rhs1, &rhs1_stmt);
2029 : 55820 : rhs2 = get_ssa_def_if_simple_copy (rhs2, &rhs2_stmt);
2030 : :
2031 : 55820 : if (is_gimple_ip_invariant (rhs2))
2032 : : {
2033 : 28254 : agg_value->pass_through.operand = rhs2;
2034 : 28254 : agg_value->pass_through.op_type = TREE_TYPE (lhs);
2035 : 28254 : stmt = rhs1_stmt;
2036 : : }
2037 : 27566 : else if (is_gimple_ip_invariant (rhs1))
2038 : : {
2039 : 2516 : if (TREE_CODE_CLASS (code) == tcc_comparison)
2040 : 0 : code = swap_tree_comparison (code);
2041 : 2516 : 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 : 28254 : if (TREE_CODE_CLASS (code) != tcc_comparison
2053 : 55940 : && !useless_type_conversion_p (TREE_TYPE (lhs),
2054 : 27686 : TREE_TYPE (rhs1)))
2055 : : return;
2056 : : }
2057 : 28254 : break;
2058 : :
2059 : : default:
2060 : : return;
2061 : : }
2062 : : }
2063 : : else
2064 : : return;
2065 : :
2066 : 417057 : if (TREE_CODE (rhs1) != SSA_NAME)
2067 : 357582 : index = load_from_unmodified_param_or_agg (fbi, fbi->info, stmt,
2068 : : &agg_value->offset,
2069 : : &agg_value->by_ref);
2070 : 59475 : else if (SSA_NAME_IS_DEFAULT_DEF (rhs1))
2071 : 44654 : index = ipa_get_param_decl_index (fbi->info, SSA_NAME_VAR (rhs1));
2072 : :
2073 : 402236 : if (index >= 0)
2074 : : {
2075 : 89040 : if (agg_value->offset >= 0)
2076 : 47533 : agg_value->type = TREE_TYPE (rhs1);
2077 : 89040 : agg_value->pass_through.formal_id = index;
2078 : 89040 : agg_value->pass_through.operation = code;
2079 : : }
2080 : : else
2081 : 328017 : 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 : 2865619 : 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 : 2865619 : HOST_WIDE_INT lhs_offset, lhs_size;
2096 : 2865619 : bool reverse;
2097 : :
2098 : 2865619 : if (!is_gimple_assign (stmt))
2099 : : return false;
2100 : :
2101 : 1875616 : tree lhs = gimple_assign_lhs (stmt);
2102 : 1875616 : tree lhs_base = get_ref_base_and_extent_hwi (lhs, &lhs_offset, &lhs_size,
2103 : : &reverse);
2104 : 1875616 : if (!lhs_base)
2105 : : return false;
2106 : :
2107 : 1873959 : if (check_ref)
2108 : : {
2109 : 155645 : if (TREE_CODE (lhs_base) != MEM_REF
2110 : 121106 : || TREE_OPERAND (lhs_base, 0) != base
2111 : 201885 : || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
2112 : 116230 : return false;
2113 : : }
2114 : 1718314 : else if (lhs_base != base)
2115 : : return false;
2116 : :
2117 : 1753863 : content->offset = lhs_offset;
2118 : 1753863 : content->size = lhs_size;
2119 : 1753863 : content->type = TREE_TYPE (lhs);
2120 : 1753863 : content->next = NULL;
2121 : :
2122 : 1753863 : analyze_agg_content_value (fbi, &content->value, stmt);
2123 : 1753863 : 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 : 3376113 : 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 : 3376113 : struct ipa_known_agg_contents_list *list = NULL, *all_list = NULL;
2140 : 3376113 : bitmap visited = NULL;
2141 : 3376113 : int item_count = 0, value_count = 0;
2142 : 3376113 : HOST_WIDE_INT arg_offset, arg_size;
2143 : 3376113 : tree arg_base;
2144 : 3376113 : bool check_ref, by_ref;
2145 : 3376113 : ao_ref r;
2146 : 3376113 : int max_agg_items = opt_for_fn (fbi->node->decl, param_ipa_max_agg_items);
2147 : :
2148 : 3376113 : if (max_agg_items == 0)
2149 : 827578 : 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 : 3376113 : if (POINTER_TYPE_P (arg_type))
2156 : : {
2157 : 3001327 : by_ref = true;
2158 : 3001327 : if (TREE_CODE (arg) == SSA_NAME)
2159 : : {
2160 : 1102803 : tree type_size;
2161 : 1102803 : if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type)))
2162 : 1102803 : || !POINTER_TYPE_P (TREE_TYPE (arg)))
2163 : : return;
2164 : 789240 : check_ref = true;
2165 : 789240 : arg_base = arg;
2166 : 789240 : arg_offset = 0;
2167 : 789240 : type_size = TYPE_SIZE (TREE_TYPE (arg_type));
2168 : 789240 : arg_size = tree_to_uhwi (type_size);
2169 : 789240 : ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
2170 : : }
2171 : 1898524 : else if (TREE_CODE (arg) == ADDR_EXPR)
2172 : : {
2173 : 1827783 : bool reverse;
2174 : :
2175 : 1827783 : arg = TREE_OPERAND (arg, 0);
2176 : 1827783 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2177 : : &arg_size, &reverse);
2178 : 1827783 : if (!arg_base)
2179 : 443198 : return;
2180 : 1826568 : if (DECL_P (arg_base))
2181 : : {
2182 : 1384585 : check_ref = false;
2183 : 1384585 : ao_ref_init (&r, arg_base);
2184 : : }
2185 : : else
2186 : : return;
2187 : : }
2188 : : else
2189 : : return;
2190 : : }
2191 : : else
2192 : : {
2193 : 374786 : bool reverse;
2194 : :
2195 : 374786 : gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
2196 : :
2197 : 374786 : by_ref = false;
2198 : 374786 : check_ref = false;
2199 : 374786 : arg_base = get_ref_base_and_extent_hwi (arg, &arg_offset,
2200 : : &arg_size, &reverse);
2201 : 374786 : if (!arg_base)
2202 : 76 : return;
2203 : :
2204 : 374710 : 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 : 2548535 : for (tree dom_vuse = gimple_vuse (call);
2214 : 24341100 : dom_vuse && fbi->aa_walk_budget > 0;)
2215 : : {
2216 : 23835750 : gimple *stmt = SSA_NAME_DEF_STMT (dom_vuse);
2217 : :
2218 : 23835750 : if (gimple_code (stmt) == GIMPLE_PHI)
2219 : : {
2220 : 2314288 : dom_vuse = get_continuation_for_phi (stmt, &r, true,
2221 : 1157144 : fbi->aa_walk_budget,
2222 : : &visited, false, NULL, NULL);
2223 : 1157144 : continue;
2224 : : }
2225 : :
2226 : 22678606 : fbi->aa_walk_budget--;
2227 : 22678606 : if (stmt_may_clobber_ref_p_1 (stmt, &r))
2228 : : {
2229 : 2865619 : struct ipa_known_agg_contents_list *content
2230 : 2865619 : = XALLOCA (struct ipa_known_agg_contents_list);
2231 : :
2232 : 2865619 : 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 : 1753863 : if ((content->value.pass_through.formal_id >= 0
2238 : 1664823 : || content->value.pass_through.operand)
2239 : 1065903 : && !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 : 2759659 : && (content->offset + content->size - arg_offset
2244 : : <= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT))
2245 : : {
2246 : 1005658 : struct ipa_known_agg_contents_list *copy
2247 : 1005658 : = 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 : 1005658 : add_to_agg_contents_list (&list, (*copy = *content, copy));
2252 : :
2253 : 1005658 : if (++value_count == max_agg_items)
2254 : : break;
2255 : : }
2256 : :
2257 : : /* Add to the list consisting of all dominating virtual operands. */
2258 : 1751644 : add_to_agg_contents_list (&all_list, content);
2259 : :
2260 : 1751644 : if (++item_count == 2 * max_agg_items)
2261 : : break;
2262 : : }
2263 : 42198856 : dom_vuse = gimple_vuse (stmt);
2264 : : }
2265 : :
2266 : 2548535 : if (visited)
2267 : 623120 : 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 : 2548535 : if (value_count)
2274 : : {
2275 : 344809 : jfunc->agg.by_ref = by_ref;
2276 : 344809 : 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 : 6270082 : ipa_get_callee_param_type (struct cgraph_edge *e, int i)
2286 : : {
2287 : 6270082 : int n;
2288 : 6270082 : tree type = (e->callee
2289 : 6270082 : ? TREE_TYPE (e->callee->decl)
2290 : 6270082 : : gimple_call_fntype (e->call_stmt));
2291 : 6270082 : tree t = TYPE_ARG_TYPES (type);
2292 : :
2293 : 12812201 : for (n = 0; n < i; n++)
2294 : : {
2295 : 6794537 : if (!t)
2296 : : break;
2297 : 6542119 : t = TREE_CHAIN (t);
2298 : : }
2299 : 6270082 : if (t && t != void_list_node)
2300 : 5925483 : return TREE_VALUE (t);
2301 : 344599 : if (!e->callee)
2302 : : return NULL;
2303 : 323373 : t = DECL_ARGUMENTS (e->callee->decl);
2304 : 850039 : for (n = 0; n < i; n++)
2305 : : {
2306 : 805557 : if (!t)
2307 : : return NULL;
2308 : 526666 : t = TREE_CHAIN (t);
2309 : : }
2310 : 44482 : 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 : 5689667 : ipa_get_value_range (const vrange &tmp)
2320 : : {
2321 : 5689667 : inchash::hash hstate;
2322 : 5689667 : inchash::add_vrange (tmp, hstate);
2323 : 5689667 : hashval_t hash = hstate.end ();
2324 : 5689667 : ipa_vr **slot = ipa_vr_hash_table->find_slot_with_hash (&tmp, hash, INSERT);
2325 : 5689667 : if (*slot)
2326 : : return *slot;
2327 : :
2328 : 620218 : ipa_vr *vr = new (ggc_alloc<ipa_vr> ()) ipa_vr (tmp);
2329 : 620218 : *slot = vr;
2330 : 620218 : 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 : 4942388 : ipa_set_jfunc_vr (ipa_jump_func *jf, const vrange &tmp)
2338 : : {
2339 : 1870282 : jf->m_vr = ipa_get_value_range (tmp);
2340 : 3072106 : }
2341 : :
2342 : : static void
2343 : 581679 : ipa_set_jfunc_vr (ipa_jump_func *jf, const ipa_vr &vr)
2344 : : {
2345 : 581679 : value_range tmp;
2346 : 581679 : vr.get_vrange (tmp);
2347 : 581679 : ipa_set_jfunc_vr (jf, tmp);
2348 : 581679 : }
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 : 1336066 : ipa_get_range_from_ip_invariant (vrange &r, tree val, cgraph_node *context_node)
2356 : : {
2357 : 1336066 : 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 : 1335043 : 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 : 6270082 : skip_a_safe_conversion_op (tree t)
2395 : : {
2396 : 6270082 : if (TREE_CODE (t) != SSA_NAME
2397 : 6270082 : || SSA_NAME_IS_DEFAULT_DEF (t))
2398 : : return t;
2399 : :
2400 : 1545938 : gimple *def = SSA_NAME_DEF_STMT (t);
2401 : 1545938 : if (!is_gimple_assign (def)
2402 : 1275867 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def))
2403 : 246352 : || !INTEGRAL_TYPE_P (TREE_TYPE (t))
2404 : 1726898 : || !INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def))))
2405 : : return t;
2406 : :
2407 : 172937 : tree rhs1 = gimple_assign_rhs1 (def);
2408 : 172937 : if (TYPE_PRECISION (TREE_TYPE (t))
2409 : 172937 : >= 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 : 15054 : init_callback_edge_summary (struct cgraph_edge *cbe, tree attr)
2430 : : {
2431 : 15054 : ipa_edge_args *cb_args = ipa_edge_args_sum->get_create (cbe);
2432 : 15054 : size_t jf_vec_length = callback_num_args(attr);
2433 : 15054 : vec_safe_grow_cleared (cb_args->jump_functions,
2434 : : jf_vec_length, true);
2435 : 15054 : }
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 : 2968544 : ipa_compute_jump_functions_for_edge (struct ipa_func_body_info *fbi,
2443 : : struct cgraph_edge *cs)
2444 : : {
2445 : 2968544 : ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
2446 : 2968544 : ipa_edge_args *args = ipa_edge_args_sum->get_create (cs);
2447 : 2968544 : gcall *call = cs->call_stmt;
2448 : 2968544 : int n, arg_num = gimple_call_num_args (call);
2449 : 2968544 : bool useful_context = false;
2450 : :
2451 : 2968544 : if (arg_num == 0 || args->jump_functions)
2452 : 274849 : return;
2453 : 2693695 : vec_safe_grow_cleared (args->jump_functions, arg_num, true);
2454 : 2693695 : if (flag_devirtualize)
2455 : 2516025 : vec_safe_grow_cleared (args->polymorphic_call_contexts, arg_num, true);
2456 : :
2457 : 2693695 : if (gimple_call_internal_p (call))
2458 : : return;
2459 : 2693695 : if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
2460 : : return;
2461 : :
2462 : 2693695 : auto_vec<cgraph_edge*> callback_edges;
2463 : 8963777 : for (n = 0; n < arg_num; n++)
2464 : : {
2465 : 6270082 : struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
2466 : 6270082 : tree arg = gimple_call_arg (call, n);
2467 : 6270082 : tree param_type = ipa_get_callee_param_type (cs, n);
2468 : 6270082 : if (flag_devirtualize && POINTER_TYPE_P (TREE_TYPE (arg)))
2469 : : {
2470 : 3223869 : tree instance;
2471 : 3223869 : class ipa_polymorphic_call_context context (cs->caller->decl,
2472 : 3223869 : arg, cs->call_stmt,
2473 : 3223869 : &instance);
2474 : 3223869 : context.get_dynamic_type (instance, arg, NULL, cs->call_stmt,
2475 : : &fbi->aa_walk_budget);
2476 : 3223869 : *ipa_get_ith_polymorhic_call_context (args, n) = context;
2477 : 6447738 : if (!context.useless_p ())
2478 : : useful_context = true;
2479 : : }
2480 : :
2481 : 6270082 : value_range vr (TREE_TYPE (arg));
2482 : 6270082 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2483 : : {
2484 : 6952048 : if (!get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2485 : 3476024 : || vr.varying_p ()
2486 : 6045074 : || vr.undefined_p ())
2487 : : {
2488 : 907296 : bool strict_overflow = false;
2489 : 907296 : if (tree_single_nonzero_warnv_p (arg, &strict_overflow))
2490 : 0 : vr.set_nonzero (TREE_TYPE (arg));
2491 : : else
2492 : 907296 : vr.set_varying (TREE_TYPE (arg));
2493 : : }
2494 : 3476024 : gcc_assert (!vr.undefined_p ());
2495 : 3476024 : unsigned HOST_WIDE_INT bitpos;
2496 : 3476024 : unsigned align = BITS_PER_UNIT;
2497 : :
2498 : 3476024 : if (!vr.singleton_p ())
2499 : 3405692 : get_pointer_alignment_1 (arg, &align, &bitpos);
2500 : :
2501 : 3476024 : if (align > BITS_PER_UNIT
2502 : 3476024 : && opt_for_fn (cs->caller->decl, flag_ipa_bit_cp))
2503 : : {
2504 : 1288603 : unsigned prec = TYPE_PRECISION (TREE_TYPE (arg));
2505 : 1288603 : wide_int mask
2506 : 2577206 : = wi::bit_and_not (wi::mask (prec, false, prec),
2507 : 1288603 : wide_int::from (align / BITS_PER_UNIT - 1,
2508 : 1288603 : prec, UNSIGNED));
2509 : 1288603 : wide_int value = wide_int::from (bitpos / BITS_PER_UNIT, prec,
2510 : 1288603 : UNSIGNED);
2511 : 1288603 : irange_bitmask bm (value, mask);
2512 : 1288603 : vr.update_bitmask (bm);
2513 : 1288603 : ipa_set_jfunc_vr (jfunc, vr);
2514 : 1288603 : }
2515 : 2187421 : else if (!vr.varying_p ())
2516 : 1341741 : ipa_set_jfunc_vr (jfunc, vr);
2517 : : else
2518 : 845680 : gcc_assert (!jfunc->m_vr);
2519 : : }
2520 : : else
2521 : : {
2522 : 2794058 : if (param_type
2523 : 2484424 : && ipa_vr_supported_type_p (TREE_TYPE (arg))
2524 : 2794196 : && ipa_vr_supported_type_p (param_type)
2525 : 3676818 : && get_range_query (cfun)->range_of_expr (vr, arg, cs->call_stmt)
2526 : 4632467 : && !vr.undefined_p ())
2527 : : {
2528 : 1838271 : value_range resvr (vr);
2529 : 1838271 : range_cast (resvr, param_type);
2530 : 1838271 : if (!resvr.undefined_p () && !resvr.varying_p ())
2531 : 1451822 : ipa_set_jfunc_vr (jfunc, resvr);
2532 : : else
2533 : 386449 : gcc_assert (!jfunc->m_vr);
2534 : 1838271 : }
2535 : : else
2536 : 955787 : gcc_assert (!jfunc->m_vr);
2537 : : }
2538 : :
2539 : 6270082 : arg = skip_a_safe_conversion_op (arg);
2540 : 6270082 : if (is_gimple_ip_invariant (arg)
2541 : 6270082 : || (VAR_P (arg) && is_global_var (arg) && TREE_READONLY (arg)))
2542 : : {
2543 : 2285910 : ipa_set_jf_constant (jfunc, arg, cs);
2544 : 2285910 : if (TREE_CODE (arg) == ADDR_EXPR)
2545 : : {
2546 : 856115 : tree pointee = TREE_OPERAND (arg, 0);
2547 : 856115 : if (TREE_CODE (pointee) == FUNCTION_DECL && !cs->callback
2548 : 43415 : && cs->callee)
2549 : : {
2550 : : /* Argument is a pointer to a function. Look for a callback
2551 : : attribute describing this argument. */
2552 : 43065 : tree callback_attr
2553 : 43065 : = lookup_attribute (CALLBACK_ATTR_IDENT,
2554 : 43065 : DECL_ATTRIBUTES (cs->callee->decl));
2555 : 86130 : for (; callback_attr;
2556 : : callback_attr
2557 : 0 : = lookup_attribute (CALLBACK_ATTR_IDENT,
2558 : 0 : TREE_CHAIN (callback_attr)))
2559 : 13300 : 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 : 43065 : if (!callback_attr
2565 : 43065 : && callback_is_special_cased (cs->callee->decl, call))
2566 : : {
2567 : 1754 : callback_attr
2568 : 1754 : = 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 : 1754 : 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 : 43065 : if (callback_attr)
2580 : : {
2581 : 15054 : cgraph_node *kernel_node
2582 : 15054 : = cgraph_node::get_create (pointee);
2583 : 15054 : unsigned callback_id = n;
2584 : 15054 : cgraph_edge *cbe
2585 : 15054 : = cs->make_callback (kernel_node, callback_id);
2586 : 15054 : init_callback_edge_summary (cbe, callback_attr);
2587 : 15054 : callback_edges.safe_push (cbe);
2588 : : }
2589 : : }
2590 : : }
2591 : : }
2592 : 3984172 : else if (!is_gimple_reg_type (TREE_TYPE (arg))
2593 : 3984172 : && TREE_CODE (arg) == PARM_DECL)
2594 : : {
2595 : 85316 : int index = ipa_get_param_decl_index (info, arg);
2596 : :
2597 : 85316 : 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 : 85316 : if (parm_preserved_before_stmt_p (fbi, index, call, arg))
2602 : : {
2603 : 67822 : ipa_set_jf_simple_pass_through (jfunc, index, false);
2604 : 67822 : continue;
2605 : : }
2606 : : }
2607 : 3898856 : else if (TREE_CODE (arg) == SSA_NAME)
2608 : : {
2609 : 2546296 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
2610 : : {
2611 : 1012409 : int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
2612 : 1012409 : if (index >= 0)
2613 : : {
2614 : 1003877 : bool agg_p;
2615 : 1003877 : agg_p = parm_ref_data_pass_through_p (fbi, index, call, arg);
2616 : 1003877 : ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
2617 : : }
2618 : : }
2619 : : else
2620 : : {
2621 : 1533887 : gimple *stmt = SSA_NAME_DEF_STMT (arg);
2622 : 1533887 : if (is_gimple_assign (stmt))
2623 : 1250975 : compute_complex_assign_jump_func (fbi, info, jfunc,
2624 : : call, stmt, arg, param_type);
2625 : 282912 : else if (gimple_code (stmt) == GIMPLE_PHI)
2626 : 99167 : 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 : 6202260 : if (!param_type)
2639 : 342406 : param_type = TREE_TYPE (arg);
2640 : :
2641 : 6202260 : if ((jfunc->type != IPA_JF_PASS_THROUGH
2642 : 1045842 : || !ipa_get_jf_pass_through_agg_preserved (jfunc))
2643 : 5815259 : && (jfunc->type != IPA_JF_ANCESTOR
2644 : 164262 : || !ipa_get_jf_ancestor_agg_preserved (jfunc))
2645 : 11951439 : && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
2646 : 5374341 : || POINTER_TYPE_P (param_type)))
2647 : 3376113 : determine_known_aggregate_parts (fbi, call, arg, param_type, jfunc);
2648 : 6270082 : }
2649 : :
2650 : 2693695 : 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 : 30108 : for (j = 0; j < callback_edges.length (); j++)
2658 : : {
2659 : 15054 : cgraph_edge *callback_edge = callback_edges[j];
2660 : 15054 : ipa_edge_args *cb_summary
2661 : 15054 : = ipa_edge_args_sum->get_create (callback_edge);
2662 : 15054 : auto_vec<int> arg_mapping
2663 : 15054 : = callback_get_arg_mapping (callback_edge, cs);
2664 : 15054 : unsigned i;
2665 : 30108 : for (i = 0; i < arg_mapping.length (); i++)
2666 : : {
2667 : 15054 : if (arg_mapping[i] == -1)
2668 : 0 : continue;
2669 : 15054 : class ipa_jump_func *src
2670 : 15054 : = ipa_get_ith_jump_func (args, arg_mapping[i]);
2671 : 15054 : class ipa_jump_func *dst = ipa_get_ith_jump_func (cb_summary, i);
2672 : 15054 : ipa_duplicate_jump_function (cs, callback_edge, src, dst);
2673 : : }
2674 : 15054 : }
2675 : : }
2676 : :
2677 : 2693695 : if (!useful_context)
2678 : 4666716 : vec_free (args->polymorphic_call_contexts);
2679 : 2693695 : }
2680 : :
2681 : : /* Compute jump functions for all edges - both direct and indirect - outgoing
2682 : : from BB. */
2683 : :
2684 : : static void
2685 : 11159874 : ipa_compute_jump_functions_for_bb (struct ipa_func_body_info *fbi, basic_block bb)
2686 : : {
2687 : 11159874 : struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
2688 : 11159874 : int i;
2689 : 11159874 : struct cgraph_edge *cs;
2690 : :
2691 : 20778736 : FOR_EACH_VEC_ELT_REVERSE (bi->cg_edges, i, cs)
2692 : : {
2693 : 5540124 : struct cgraph_node *callee = cs->callee;
2694 : :
2695 : 5540124 : if (callee)
2696 : : {
2697 : 5398243 : 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 : 3510050 : if (!callee->definition && !flag_lto
2701 : 5413284 : && !gimple_call_fnspec (cs->call_stmt).known_p ()
2702 : 7984864 : && !callback_edge_callee_has_attr (cs))
2703 : 2571580 : continue;
2704 : : }
2705 : 2968544 : ipa_compute_jump_functions_for_edge (fbi, cs);
2706 : : }
2707 : 11159874 : }
2708 : :
2709 : : /* If STMT looks like a statement loading a value from a member pointer formal
2710 : : parameter, return that parameter and store the offset of the field to
2711 : : *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
2712 : : might be clobbered). If USE_DELTA, then we look for a use of the delta
2713 : : field rather than the pfn. */
2714 : :
2715 : : static tree
2716 : 2197 : ipa_get_stmt_member_ptr_load_param (gimple *stmt, bool use_delta,
2717 : : HOST_WIDE_INT *offset_p)
2718 : : {
2719 : 2197 : tree rhs, fld, ptr_field, delta_field;
2720 : 2197 : tree ref_field = NULL_TREE;
2721 : 2197 : tree ref_offset = NULL_TREE;
2722 : :
2723 : 2197 : if (!gimple_assign_single_p (stmt))
2724 : : return NULL_TREE;
2725 : :
2726 : 2197 : rhs = gimple_assign_rhs1 (stmt);
2727 : 2197 : if (TREE_CODE (rhs) == COMPONENT_REF)
2728 : : {
2729 : 1321 : ref_field = TREE_OPERAND (rhs, 1);
2730 : 1321 : rhs = TREE_OPERAND (rhs, 0);
2731 : : }
2732 : :
2733 : 2197 : if (TREE_CODE (rhs) == MEM_REF)
2734 : : {
2735 : 1492 : ref_offset = TREE_OPERAND (rhs, 1);
2736 : 1492 : if (ref_field && integer_nonzerop (ref_offset))
2737 : : return NULL_TREE;
2738 : : }
2739 : 705 : else if (!ref_field)
2740 : : return NULL_TREE;
2741 : :
2742 : 2197 : if (TREE_CODE (rhs) == MEM_REF
2743 : 1492 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
2744 : 3689 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (rhs, 0)))
2745 : : {
2746 : 614 : rhs = TREE_OPERAND (rhs, 0);
2747 : 614 : if (TREE_CODE (SSA_NAME_VAR (rhs)) != PARM_DECL
2748 : 614 : || !type_like_member_ptr_p (TREE_TYPE (TREE_TYPE (rhs)), &ptr_field,
2749 : : &delta_field))
2750 : 0 : return NULL_TREE;
2751 : : }
2752 : : else
2753 : : {
2754 : 1583 : if (TREE_CODE (rhs) == MEM_REF
2755 : 1583 : && TREE_CODE (TREE_OPERAND (rhs, 0)) == ADDR_EXPR)
2756 : 0 : rhs = TREE_OPERAND (TREE_OPERAND (rhs, 0), 0);
2757 : 1583 : if (TREE_CODE (rhs) != PARM_DECL
2758 : 1583 : || !type_like_member_ptr_p (TREE_TYPE (rhs), &ptr_field,
2759 : : &delta_field))
2760 : 1307 : return NULL_TREE;
2761 : : }
2762 : :
2763 : 890 : if (use_delta)
2764 : 0 : fld = delta_field;
2765 : : else
2766 : 890 : fld = ptr_field;
2767 : :
2768 : 890 : if (ref_field)
2769 : : {
2770 : 890 : if (ref_field != fld)
2771 : : return NULL_TREE;
2772 : : }
2773 : 0 : else if (!tree_int_cst_equal (byte_position (fld), ref_offset))
2774 : : return NULL_TREE;
2775 : :
2776 : 890 : if (offset_p)
2777 : 445 : *offset_p = int_bit_position (fld);
2778 : : return rhs;
2779 : : }
2780 : :
2781 : : /* Returns true iff T is an SSA_NAME defined by a statement. */
2782 : :
2783 : : static bool
2784 : 3087 : ipa_is_ssa_with_stmt_def (tree t)
2785 : : {
2786 : 3087 : if (TREE_CODE (t) == SSA_NAME
2787 : 3087 : && !SSA_NAME_IS_DEFAULT_DEF (t))
2788 : : return true;
2789 : : else
2790 : 0 : return false;
2791 : : }
2792 : :
2793 : : /* Find the indirect call graph edge corresponding to STMT and mark it as a
2794 : : call to a parameter number PARAM_INDEX. NODE is the caller. Return the
2795 : : indirect call graph edge.
2796 : : If POLYMORPHIC is true record is as a destination of polymorphic call. */
2797 : :
2798 : : static struct cgraph_edge *
2799 : 16948 : ipa_note_param_call (struct cgraph_node *node, int param_index,
2800 : : gcall *stmt, bool polymorphic)
2801 : : {
2802 : 16948 : struct cgraph_edge *cs;
2803 : :
2804 : 16948 : cs = node->get_edge (stmt);
2805 : 16948 : cs->indirect_info->param_index = param_index;
2806 : 16948 : cs->indirect_info->agg_contents = 0;
2807 : 16948 : cs->indirect_info->member_ptr = 0;
2808 : 16948 : cs->indirect_info->guaranteed_unmodified = 0;
2809 : 16948 : ipa_node_params *info = ipa_node_params_sum->get (node);
2810 : 16948 : ipa_set_param_used_by_indirect_call (info, param_index, true);
2811 : 16948 : if (cs->indirect_info->polymorphic || polymorphic)
2812 : 10392 : ipa_set_param_used_by_polymorphic_call (info, param_index, true);
2813 : 16948 : return cs;
2814 : : }
2815 : :
2816 : : /* Analyze the CALL and examine uses of formal parameters of the caller NODE
2817 : : (described by INFO). PARMS_AINFO is a pointer to a vector containing
2818 : : intermediate information about each formal parameter. Currently it checks
2819 : : whether the call calls a pointer that is a formal parameter and if so, the
2820 : : parameter is marked with the called flag and an indirect call graph edge
2821 : : describing the call is created. This is very simple for ordinary pointers
2822 : : represented in SSA but not-so-nice when it comes to member pointers. The
2823 : : ugly part of this function does nothing more than trying to match the
2824 : : pattern of such a call. Look up the documentation of macro
2825 : : TARGET_PTRMEMFUNC_VBIT_LOCATION for details. An example of such a pattern
2826 : : is the gimple dump below, the call is on the last line:
2827 : :
2828 : : <bb 2>:
2829 : : f$__delta_5 = f.__delta;
2830 : : f$__pfn_24 = f.__pfn;
2831 : :
2832 : : or
2833 : : <bb 2>:
2834 : : f$__delta_5 = MEM[(struct *)&f];
2835 : : f$__pfn_24 = MEM[(struct *)&f + 4B];
2836 : :
2837 : : and a few lines below:
2838 : :
2839 : : <bb 5>
2840 : : D.2496_3 = (int) f$__pfn_24;
2841 : : D.2497_4 = D.2496_3 & 1;
2842 : : if (D.2497_4 != 0)
2843 : : goto <bb 3>;
2844 : : else
2845 : : goto <bb 4>;
2846 : :
2847 : : <bb 6>:
2848 : : D.2500_7 = (unsigned int) f$__delta_5;
2849 : : D.2501_8 = &S + D.2500_7;
2850 : : D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
2851 : : D.2503_10 = *D.2502_9;
2852 : : D.2504_12 = f$__pfn_24 + -1;
2853 : : D.2505_13 = (unsigned int) D.2504_12;
2854 : : D.2506_14 = D.2503_10 + D.2505_13;
2855 : : D.2507_15 = *D.2506_14;
2856 : : iftmp.11_16 = (String:: *) D.2507_15;
2857 : :
2858 : : <bb 7>:
2859 : : # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
2860 : : D.2500_19 = (unsigned int) f$__delta_5;
2861 : : D.2508_20 = &S + D.2500_19;
2862 : : D.2493_21 = iftmp.11_1 (D.2508_20, 4);
2863 : :
2864 : : Such patterns are results of simple calls to a member pointer:
2865 : :
2866 : : int doprinting (int (MyString::* f)(int) const)
2867 : : {
2868 : : MyString S ("somestring");
2869 : :
2870 : : return (S.*f)(4);
2871 : : }
2872 : :
2873 : : Moreover, the function also looks for called pointers loaded from aggregates
2874 : : passed by value or reference. */
2875 : :
2876 : : static void
2877 : 114028 : ipa_analyze_indirect_call_uses (struct ipa_func_body_info *fbi, gcall *call,
2878 : : tree target)
2879 : : {
2880 : 114028 : class ipa_node_params *info = fbi->info;
2881 : 114028 : HOST_WIDE_INT offset;
2882 : 114028 : bool by_ref;
2883 : :
2884 : 114028 : if (SSA_NAME_IS_DEFAULT_DEF (target))
2885 : : {
2886 : 3781 : tree var = SSA_NAME_VAR (target);
2887 : 3781 : int index = ipa_get_param_decl_index (info, var);
2888 : 3781 : if (index >= 0)
2889 : 3779 : ipa_note_param_call (fbi->node, index, call, false);
2890 : 3781 : return;
2891 : : }
2892 : :
2893 : 110247 : int index;
2894 : 110247 : gimple *def = SSA_NAME_DEF_STMT (target);
2895 : 110247 : bool guaranteed_unmodified;
2896 : 110247 : if (gimple_assign_single_p (def)
2897 : 110247 : && ipa_load_from_parm_agg (fbi, info->descriptors, def,
2898 : : gimple_assign_rhs1 (def), &index, &offset,
2899 : : NULL, &by_ref, &guaranteed_unmodified))
2900 : : {
2901 : 2340 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
2902 : : call, false);
2903 : 2340 : cs->indirect_info->offset = offset;
2904 : 2340 : cs->indirect_info->agg_contents = 1;
2905 : 2340 : cs->indirect_info->by_ref = by_ref;
2906 : 2340 : cs->indirect_info->guaranteed_unmodified = guaranteed_unmodified;
2907 : 2340 : return;
2908 : : }
2909 : :
2910 : : /* Now we need to try to match the complex pattern of calling a member
2911 : : pointer. */
2912 : 107907 : if (gimple_code (def) != GIMPLE_PHI
2913 : 1154 : || gimple_phi_num_args (def) != 2
2914 : 1146 : || !POINTER_TYPE_P (TREE_TYPE (target))
2915 : 109053 : || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
2916 : : return;
2917 : :
2918 : : /* First, we need to check whether one of these is a load from a member
2919 : : pointer that is a parameter to this function. */
2920 : 876 : tree n1 = PHI_ARG_DEF (def, 0);
2921 : 876 : tree n2 = PHI_ARG_DEF (def, 1);
2922 : 1752 : if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
2923 : : return;
2924 : 876 : gimple *d1 = SSA_NAME_DEF_STMT (n1);
2925 : 876 : gimple *d2 = SSA_NAME_DEF_STMT (n2);
2926 : :
2927 : 876 : tree rec;
2928 : 876 : basic_block bb, virt_bb;
2929 : 876 : basic_block join = gimple_bb (def);
2930 : 876 : if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
2931 : : {
2932 : 0 : if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
2933 : : return;
2934 : :
2935 : 0 : bb = EDGE_PRED (join, 0)->src;
2936 : 0 : virt_bb = gimple_bb (d2);
2937 : : }
2938 : 876 : else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
2939 : : {
2940 : 445 : bb = EDGE_PRED (join, 1)->src;
2941 : 445 : virt_bb = gimple_bb (d1);
2942 : : }
2943 : : else
2944 : : return;
2945 : :
2946 : : /* Second, we need to check that the basic blocks are laid out in the way
2947 : : corresponding to the pattern. */
2948 : :
2949 : 890 : if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
2950 : 890 : || single_succ (virt_bb) != join)
2951 : : return;
2952 : :
2953 : :
2954 : 445 : if (single_pred (virt_bb) != bb)
2955 : : {
2956 : : /* In cases when the distinction between a normal and a virtual
2957 : : function is encoded in the delta field, the load of the
2958 : : actual non-virtual function pointer can be in its own BB. */
2959 : :
2960 : 0 : if (!single_pred_p (bb) || !single_succ_p (bb))
2961 : : return;
2962 : 0 : bb = single_pred (bb);
2963 : 0 : if (bb != single_pred (virt_bb))
2964 : : return;
2965 : : }
2966 : :
2967 : : /* Third, let's see that the branching is done depending on the least
2968 : : significant bit of the pfn. */
2969 : :
2970 : 890 : gcond *branch = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
2971 : 445 : if (!branch)
2972 : : return;
2973 : :
2974 : 445 : if ((gimple_cond_code (branch) != NE_EXPR
2975 : 0 : && gimple_cond_code (branch) != EQ_EXPR)
2976 : 445 : || !integer_zerop (gimple_cond_rhs (branch)))
2977 : 0 : return;
2978 : :
2979 : 445 : tree cond = gimple_cond_lhs (branch);
2980 : 445 : if (!ipa_is_ssa_with_stmt_def (cond))
2981 : : return;
2982 : :
2983 : 445 : def = SSA_NAME_DEF_STMT (cond);
2984 : 445 : if (!is_gimple_assign (def)
2985 : 445 : || gimple_assign_rhs_code (def) != BIT_AND_EXPR
2986 : 890 : || !integer_onep (gimple_assign_rhs2 (def)))
2987 : 0 : return;
2988 : :
2989 : 445 : cond = gimple_assign_rhs1 (def);
2990 : 445 : if (!ipa_is_ssa_with_stmt_def (cond))
2991 : : return;
2992 : :
2993 : 445 : def = SSA_NAME_DEF_STMT (cond);
2994 : :
2995 : 445 : if (is_gimple_assign (def)
2996 : 445 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
2997 : : {
2998 : 445 : cond = gimple_assign_rhs1 (def);
2999 : 445 : if (!ipa_is_ssa_with_stmt_def (cond))
3000 : : return;
3001 : 445 : def = SSA_NAME_DEF_STMT (cond);
3002 : : }
3003 : :
3004 : 445 : tree rec2;
3005 : 445 : rec2 = ipa_get_stmt_member_ptr_load_param (def,
3006 : : (TARGET_PTRMEMFUNC_VBIT_LOCATION
3007 : : == ptrmemfunc_vbit_in_delta),
3008 : : NULL);
3009 : 445 : if (rec != rec2)
3010 : : return;
3011 : :
3012 : 445 : if (TREE_CODE (rec) == SSA_NAME)
3013 : : {
3014 : 307 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (rec));
3015 : 307 : if (index < 0
3016 : 307 : || !parm_ref_data_preserved_p (fbi, index, call,
3017 : : gimple_assign_rhs1 (def)))
3018 : 8 : return;
3019 : 299 : by_ref = true;
3020 : : }
3021 : : else
3022 : : {
3023 : 138 : index = ipa_get_param_decl_index (info, rec);
3024 : 138 : if (index < 0
3025 : 138 : || !parm_preserved_before_stmt_p (fbi, index, call, rec))
3026 : 0 : return;
3027 : 138 : by_ref = false;
3028 : : }
3029 : :
3030 : 437 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
3031 : : call, false);
3032 : 437 : cs->indirect_info->offset = offset;
3033 : 437 : cs->indirect_info->agg_contents = 1;
3034 : 437 : cs->indirect_info->member_ptr = 1;
3035 : 437 : cs->indirect_info->by_ref = by_ref;
3036 : 437 : cs->indirect_info->guaranteed_unmodified = 1;
3037 : :
3038 : 437 : return;
3039 : : }
3040 : :
3041 : : /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
3042 : : object referenced in the expression is a formal parameter of the caller
3043 : : FBI->node (described by FBI->info), create a call note for the
3044 : : statement. */
3045 : :
3046 : : static void
3047 : 27781 : ipa_analyze_virtual_call_uses (struct ipa_func_body_info *fbi,
3048 : : gcall *call, tree target)
3049 : : {
3050 : 27781 : tree obj = OBJ_TYPE_REF_OBJECT (target);
3051 : 27781 : int index;
3052 : 27781 : HOST_WIDE_INT anc_offset;
3053 : :
3054 : 27781 : if (!flag_devirtualize)
3055 : 17389 : return;
3056 : :
3057 : 27525 : if (TREE_CODE (obj) != SSA_NAME)
3058 : : return;
3059 : :
3060 : 27166 : class ipa_node_params *info = fbi->info;
3061 : 27166 : if (SSA_NAME_IS_DEFAULT_DEF (obj))
3062 : : {
3063 : 9621 : if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
3064 : : return;
3065 : :
3066 : 9621 : anc_offset = 0;
3067 : 9621 : index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
3068 : 9621 : gcc_assert (index >= 0);
3069 : 9621 : if (detect_type_change_ssa (fbi, obj, obj_type_ref_class (target),
3070 : : call))
3071 : : return;
3072 : : }
3073 : : else
3074 : : {
3075 : 17545 : gimple *stmt = SSA_NAME_DEF_STMT (obj);
3076 : 17545 : tree expr;
3077 : :
3078 : 17545 : expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
3079 : 17545 : if (!expr)
3080 : : return;
3081 : 771 : index = ipa_get_param_decl_index (info,
3082 : 771 : SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
3083 : 771 : gcc_assert (index >= 0);
3084 : 771 : if (detect_type_change (fbi, obj, expr, obj_type_ref_class (target),
3085 : : call, anc_offset))
3086 : : return;
3087 : : }
3088 : :
3089 : 10392 : struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index,
3090 : : call, true);
3091 : 10392 : class cgraph_indirect_call_info *ii = cs->indirect_info;
3092 : 10392 : ii->offset = anc_offset;
3093 : 10392 : ii->otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
3094 : 10392 : ii->otr_type = obj_type_ref_class (target);
3095 : 10392 : ii->polymorphic = 1;
3096 : : }
3097 : :
3098 : : /* Analyze a call statement CALL whether and how it utilizes formal parameters
3099 : : of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
3100 : : containing intermediate information about each formal parameter. */
3101 : :
3102 : : static void
3103 : 5769629 : ipa_analyze_call_uses (struct ipa_func_body_info *fbi, gcall *call)
3104 : : {
3105 : 5769629 : tree target = gimple_call_fn (call);
3106 : :
3107 : 5769629 : if (!target
3108 : 5769629 : || (TREE_CODE (target) != SSA_NAME
3109 : 5426096 : && !virtual_method_call_p (target)))
3110 : 5627818 : return;
3111 : :
3112 : 141811 : struct cgraph_edge *cs = fbi->node->get_edge (call);
3113 : : /* If we previously turned the call into a direct call, there is
3114 : : no need to analyze. */
3115 : 141811 : if (cs && !cs->indirect_unknown_callee)
3116 : : return;
3117 : :
3118 : 141809 : if (cs->indirect_info->polymorphic && flag_devirtualize)
3119 : : {
3120 : 27525 : tree instance;
3121 : 27525 : tree target = gimple_call_fn (call);
3122 : 27525 : ipa_polymorphic_call_context context (current_function_decl,
3123 : 27525 : target, call, &instance);
3124 : :
3125 : 27525 : gcc_checking_assert (cs->indirect_info->otr_type
3126 : : == obj_type_ref_class (target));
3127 : 27525 : gcc_checking_assert (cs->indirect_info->otr_token
3128 : : == tree_to_shwi (OBJ_TYPE_REF_TOKEN (target)));
3129 : :
3130 : 55050 : cs->indirect_info->vptr_changed
3131 : 55050 : = !context.get_dynamic_type (instance,
3132 : 27525 : OBJ_TYPE_REF_OBJECT (target),
3133 : : obj_type_ref_class (target), call,
3134 : : &fbi->aa_walk_budget);
3135 : 27525 : cs->indirect_info->context = context;
3136 : : }
3137 : :
3138 : 141809 : if (TREE_CODE (target) == SSA_NAME)
3139 : 114028 : ipa_analyze_indirect_call_uses (fbi, call, target);
3140 : 27781 : else if (virtual_method_call_p (target))
3141 : 27781 : ipa_analyze_virtual_call_uses (fbi, call, target);
3142 : : }
3143 : :
3144 : :
3145 : : /* Analyze the call statement STMT with respect to formal parameters (described
3146 : : in INFO) of caller given by FBI->NODE. Currently it only checks whether
3147 : : formal parameters are called. */
3148 : :
3149 : : static void
3150 : 31633447 : ipa_analyze_stmt_uses (struct ipa_func_body_info *fbi, gimple *stmt)
3151 : : {
3152 : 31633447 : if (is_gimple_call (stmt))
3153 : 5769629 : ipa_analyze_call_uses (fbi, as_a <gcall *> (stmt));
3154 : 31633447 : }
3155 : :
3156 : : /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
3157 : : If OP is a parameter declaration, mark it as used in the info structure
3158 : : passed in DATA. */
3159 : :
3160 : : static bool
3161 : 19869585 : visit_ref_for_mod_analysis (gimple *, tree op, tree, void *data)
3162 : : {
3163 : 19869585 : class ipa_node_params *info = (class ipa_node_params *) data;
3164 : :
3165 : 19869585 : op = get_base_address (op);
3166 : 19869585 : if (op
3167 : 19869585 : && TREE_CODE (op) == PARM_DECL)
3168 : : {
3169 : 484413 : int index = ipa_get_param_decl_index (info, op);
3170 : 484413 : gcc_assert (index >= 0);
3171 : 484413 : ipa_set_param_used (info, index, true);
3172 : : }
3173 : :
3174 : 19869585 : return false;
3175 : : }
3176 : :
3177 : : /* Scan the statements in BB and inspect the uses of formal parameters. Store
3178 : : the findings in various structures of the associated ipa_node_params
3179 : : structure, such as parameter flags, notes etc. FBI holds various data about
3180 : : the function being analyzed. */
3181 : :
3182 : : static void
3183 : 11159874 : ipa_analyze_params_uses_in_bb (struct ipa_func_body_info *fbi, basic_block bb)
3184 : : {
3185 : 11159874 : gimple_stmt_iterator gsi;
3186 : 78002187 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3187 : : {
3188 : 55682439 : gimple *stmt = gsi_stmt (gsi);
3189 : :
3190 : 55682439 : if (is_gimple_debug (stmt))
3191 : 24048992 : continue;
3192 : :
3193 : 31633447 : ipa_analyze_stmt_uses (fbi, stmt);
3194 : 31633447 : walk_stmt_load_store_addr_ops (stmt, fbi->info,
3195 : : visit_ref_for_mod_analysis,
3196 : : visit_ref_for_mod_analysis,
3197 : : visit_ref_for_mod_analysis);
3198 : : }
3199 : 14200334 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3200 : 3040460 : walk_stmt_load_store_addr_ops (gsi_stmt (gsi), fbi->info,
3201 : : visit_ref_for_mod_analysis,
3202 : : visit_ref_for_mod_analysis,
3203 : : visit_ref_for_mod_analysis);
3204 : 11159874 : }
3205 : :
3206 : : /* Return true EXPR is a load from a dereference of SSA_NAME NAME. */
3207 : :
3208 : : static bool
3209 : 4246212 : load_from_dereferenced_name (tree expr, tree name)
3210 : : {
3211 : 4246212 : tree base = get_base_address (expr);
3212 : 4246212 : return (TREE_CODE (base) == MEM_REF
3213 : 4246212 : && TREE_OPERAND (base, 0) == name);
3214 : : }
3215 : :
3216 : : /* Calculate controlled uses of parameters of NODE. */
3217 : :
3218 : : static void
3219 : 1370483 : ipa_analyze_controlled_uses (struct cgraph_node *node)
3220 : : {
3221 : 1370483 : ipa_node_params *info = ipa_node_params_sum->get (node);
3222 : :
3223 : 7478448 : for (int i = 0; i < ipa_get_param_count (info); i++)
3224 : : {
3225 : 2495571 : tree parm = ipa_get_param (info, i);
3226 : 2495571 : int call_uses = 0;
3227 : 2495571 : bool load_dereferenced = false;
3228 : :
3229 : : /* For SSA regs see if parameter is used. For non-SSA we compute
3230 : : the flag during modification analysis. */
3231 : 2495571 : if (is_gimple_reg (parm))
3232 : : {
3233 : 2260894 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
3234 : : parm);
3235 : 2260894 : if (ddef && !has_zero_uses (ddef))
3236 : : {
3237 : 1985510 : imm_use_iterator imm_iter;
3238 : 1985510 : gimple *stmt;
3239 : :
3240 : 1985510 : ipa_set_param_used (info, i, true);
3241 : 6817844 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, ddef)
3242 : : {
3243 : 3970221 : if (is_gimple_debug (stmt))
3244 : 737413 : continue;
3245 : :
3246 : 3232808 : int all_stmt_uses = 0;
3247 : 3232808 : use_operand_p use_p;
3248 : 6493724 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3249 : 3260916 : all_stmt_uses++;
3250 : :
3251 : 3232808 : if (is_gimple_call (stmt))
3252 : : {
3253 : 1229403 : if (gimple_call_internal_p (stmt))
3254 : : {
3255 : : call_uses = IPA_UNDESCRIBED_USE;
3256 : : break;
3257 : : }
3258 : 1174745 : int recognized_stmt_uses;
3259 : 1174745 : if (gimple_call_fn (stmt) == ddef)
3260 : : recognized_stmt_uses = 1;
3261 : : else
3262 : 1171032 : recognized_stmt_uses = 0;
3263 : 1174745 : unsigned arg_count = gimple_call_num_args (stmt);
3264 : 5196781 : for (unsigned i = 0; i < arg_count; i++)
3265 : : {
3266 : 4022036 : tree arg = gimple_call_arg (stmt, i);
3267 : 4022036 : if (arg == ddef)
3268 : 1160088 : recognized_stmt_uses++;
3269 : 2861948 : else if (load_from_dereferenced_name (arg, ddef))
3270 : : {
3271 : 17658 : load_dereferenced = true;
3272 : 17658 : recognized_stmt_uses++;
3273 : : }
3274 : : }
3275 : :
3276 : 1174745 : if (recognized_stmt_uses != all_stmt_uses)
3277 : : {
3278 : : call_uses = IPA_UNDESCRIBED_USE;
3279 : : break;
3280 : : }
3281 : 1166797 : if (call_uses >= 0)
3282 : 1166797 : call_uses += all_stmt_uses;
3283 : : }
3284 : 2003405 : else if (gimple_assign_single_p (stmt))
3285 : : {
3286 : 1385132 : tree rhs = gimple_assign_rhs1 (stmt);
3287 : 1385132 : if (all_stmt_uses != 1
3288 : 1385132 : || !load_from_dereferenced_name (rhs, ddef))
3289 : : {
3290 : : call_uses = IPA_UNDESCRIBED_USE;
3291 : : break;
3292 : : }
3293 : : load_dereferenced = true;
3294 : : }
3295 : : else
3296 : : {
3297 : : call_uses = IPA_UNDESCRIBED_USE;
3298 : : break;
3299 : : }
3300 : 1985510 : }
3301 : : }
3302 : : else
3303 : : call_uses = 0;
3304 : : }
3305 : : else
3306 : : call_uses = IPA_UNDESCRIBED_USE;
3307 : 2495571 : ipa_set_controlled_uses (info, i, call_uses);
3308 : 2495571 : ipa_set_param_load_dereferenced (info, i, load_dereferenced);
3309 : : }
3310 : 1370483 : }
3311 : :
3312 : : /* Free stuff in BI. */
3313 : :
3314 : : static void
3315 : 63902455 : free_ipa_bb_info (struct ipa_bb_info *bi)
3316 : : {
3317 : 0 : bi->cg_edges.release ();
3318 : 63902455 : bi->param_aa_statuses.release ();
3319 : 0 : }
3320 : :
3321 : : /* Dominator walker driving the analysis. */
3322 : :
3323 : 2740966 : class analysis_dom_walker : public dom_walker
3324 : : {
3325 : : public:
3326 : 1370483 : analysis_dom_walker (struct ipa_func_body_info *fbi)
3327 : 2740966 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi) {}
3328 : :
3329 : : edge before_dom_children (basic_block) final override;
3330 : :
3331 : : private:
3332 : : struct ipa_func_body_info *m_fbi;
3333 : : };
3334 : :
3335 : : edge
3336 : 11159874 : analysis_dom_walker::before_dom_children (basic_block bb)
3337 : : {
3338 : 11159874 : ipa_analyze_params_uses_in_bb (m_fbi, bb);
3339 : 11159874 : ipa_compute_jump_functions_for_bb (m_fbi, bb);
3340 : 11159874 : return NULL;
3341 : : }
3342 : :
3343 : : /* Release body info FBI. */
3344 : :
3345 : : void
3346 : 8664270 : ipa_release_body_info (struct ipa_func_body_info *fbi)
3347 : : {
3348 : 8664270 : int i;
3349 : 8664270 : struct ipa_bb_info *bi;
3350 : :
3351 : 72543648 : FOR_EACH_VEC_ELT (fbi->bb_infos, i, bi)
3352 : 127758756 : free_ipa_bb_info (bi);
3353 : 8664270 : fbi->bb_infos.release ();
3354 : 8664270 : }
3355 : :
3356 : : /* Initialize the array describing properties of formal parameters
3357 : : of NODE, analyze their uses and compute jump functions associated
3358 : : with actual arguments of calls from within NODE. */
3359 : :
3360 : : void
3361 : 2689118 : ipa_analyze_node (struct cgraph_node *node)
3362 : : {
3363 : 2689118 : struct ipa_func_body_info fbi;
3364 : 2689118 : class ipa_node_params *info;
3365 : :
3366 : 2689118 : ipa_check_create_node_params ();
3367 : 2689118 : ipa_check_create_edge_args ();
3368 : 2689118 : info = ipa_node_params_sum->get_create (node);
3369 : :
3370 : 2689118 : if (info->analysis_done)
3371 : 1318635 : return;
3372 : 1371393 : info->analysis_done = 1;
3373 : :
3374 : 1371393 : if (ipa_func_spec_opts_forbid_analysis_p (node)
3375 : 1371393 : || (count_formal_params (node->decl)
3376 : : >= (1 << IPA_PROP_ARG_INDEX_LIMIT_BITS)))
3377 : : {
3378 : 1318635 : gcc_assert (!ipa_get_param_count (info));
3379 : : return;
3380 : : }
3381 : :
3382 : 1370483 : struct function *func = DECL_STRUCT_FUNCTION (node->decl);
3383 : 1370483 : push_cfun (func);
3384 : 1370483 : calculate_dominance_info (CDI_DOMINATORS);
3385 : 1370483 : ipa_initialize_node_params (node);
3386 : 1370483 : ipa_analyze_controlled_uses (node);
3387 : :
3388 : 1370483 : fbi.node = node;
3389 : 1370483 : fbi.info = info;
3390 : 1370483 : fbi.bb_infos = vNULL;
3391 : 1370483 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
3392 : 1370483 : fbi.param_count = ipa_get_param_count (info);
3393 : 1370483 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
3394 : :
3395 : 6768726 : for (struct cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
3396 : : {
3397 : 5398243 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3398 : 5398243 : bi->cg_edges.safe_push (cs);
3399 : : }
3400 : :
3401 : 1512364 : for (struct cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
3402 : : {
3403 : 141881 : ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
3404 : 141881 : bi->cg_edges.safe_push (cs);
3405 : : }
3406 : :
3407 : 1370483 : enable_ranger (cfun, false);
3408 : 1370483 : analysis_dom_walker (&fbi).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3409 : 1370483 : disable_ranger (cfun);
3410 : :
3411 : 1370483 : ipa_release_body_info (&fbi);
3412 : 1370483 : free_dominance_info (CDI_DOMINATORS);
3413 : 1370483 : pop_cfun ();
3414 : : }
3415 : :
3416 : : /* Update the jump functions associated with call graph edge E when the call
3417 : : graph edge CS is being inlined, assuming that E->caller is already (possibly
3418 : : indirectly) inlined into CS->callee and that E has not been inlined. */
3419 : :
3420 : : static void
3421 : 2484169 : update_jump_functions_after_inlining (struct cgraph_edge *cs,
3422 : : struct cgraph_edge *e)
3423 : : {
3424 : 2484169 : ipa_edge_args *top = ipa_edge_args_sum->get (cs);
3425 : 2484169 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
3426 : 2484169 : if (!args)
3427 : : return;
3428 : 1404298 : ipa_node_params *old_inline_root_info = ipa_node_params_sum->get (cs->callee);
3429 : 1404298 : ipa_node_params *new_inline_root_info
3430 : 1404298 : = ipa_node_params_sum->get (cs->caller->inlined_to
3431 : : ? cs->caller->inlined_to : cs->caller);
3432 : 1404298 : int count = ipa_get_cs_argument_count (args);
3433 : 1404298 : int i;
3434 : :
3435 : 4137736 : for (i = 0; i < count; i++)
3436 : : {
3437 : 2733438 : struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
3438 : 2733438 : class ipa_polymorphic_call_context *dst_ctx
3439 : 2733438 : = ipa_get_ith_polymorhic_call_context (args, i);
3440 : :
3441 : 2733438 : if (dst->agg.items)
3442 : : {
3443 : : struct ipa_agg_jf_item *item;
3444 : : int j;
3445 : :
3446 : 262170 : FOR_EACH_VEC_ELT (*dst->agg.items, j, item)
3447 : : {
3448 : 179628 : int dst_fid;
3449 : 179628 : struct ipa_jump_func *src;
3450 : :
3451 : 318681 : if (item->jftype != IPA_JF_PASS_THROUGH
3452 : 179628 : && item->jftype != IPA_JF_LOAD_AGG)
3453 : 139053 : continue;
3454 : :
3455 : 40575 : dst_fid = item->value.pass_through.formal_id;
3456 : 81150 : if (!top || dst_fid >= ipa_get_cs_argument_count (top))
3457 : : {
3458 : 0 : item->jftype = IPA_JF_UNKNOWN;
3459 : 0 : continue;
3460 : : }
3461 : :
3462 : 40575 : item->value.pass_through.formal_id = -1;
3463 : 40575 : src = ipa_get_ith_jump_func (top, dst_fid);
3464 : 40575 : if (src->type == IPA_JF_CONST)
3465 : : {
3466 : 3133 : if (item->jftype == IPA_JF_PASS_THROUGH
3467 : 2869 : && item->value.pass_through.operation == NOP_EXPR)
3468 : : {
3469 : 2806 : item->jftype = IPA_JF_CONST;
3470 : 2806 : item->value.constant = src->value.constant.value;
3471 : 2806 : continue;
3472 : : }
3473 : : }
3474 : 37442 : else if (src->type == IPA_JF_PASS_THROUGH
3475 : 5793 : && src->value.pass_through.operation == NOP_EXPR)
3476 : : {
3477 : 5631 : if (item->jftype == IPA_JF_PASS_THROUGH
3478 : 3843 : || !item->value.load_agg.by_ref
3479 : 2648 : || src->value.pass_through.agg_preserved)
3480 : 3776 : item->value.pass_through.formal_id
3481 : 3776 : = src->value.pass_through.formal_id;
3482 : : }
3483 : 31811 : else if (src->type == IPA_JF_ANCESTOR)
3484 : : {
3485 : 4858 : if (item->jftype == IPA_JF_PASS_THROUGH)
3486 : : {
3487 : 1118 : if (!src->value.ancestor.offset)
3488 : 749 : item->value.pass_through.formal_id
3489 : 749 : = src->value.ancestor.formal_id;
3490 : : }
3491 : 3740 : else if (src->value.ancestor.agg_preserved)
3492 : : {
3493 : 1440 : gcc_checking_assert (item->value.load_agg.by_ref);
3494 : :
3495 : 1440 : item->value.pass_through.formal_id
3496 : 1440 : = src->value.ancestor.formal_id;
3497 : 1440 : item->value.load_agg.offset
3498 : 1440 : += src->value.ancestor.offset;
3499 : : }
3500 : : }
3501 : :
3502 : 37769 : if (item->value.pass_through.formal_id < 0)
3503 : 31804 : item->jftype = IPA_JF_UNKNOWN;
3504 : : }
3505 : : }
3506 : :
3507 : 2733438 : if (!top)
3508 : : {
3509 : 13182 : ipa_set_jf_unknown (dst);
3510 : 13182 : continue;
3511 : : }
3512 : :
3513 : 2720256 : if (dst->type == IPA_JF_ANCESTOR)
3514 : : {
3515 : 152810 : struct ipa_jump_func *src;
3516 : 152810 : int dst_fid = dst->value.ancestor.formal_id;
3517 : 152810 : class ipa_polymorphic_call_context *src_ctx
3518 : 152810 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3519 : :
3520 : : /* Variable number of arguments can cause havoc if we try to access
3521 : : one that does not exist in the inlined edge. So make sure we
3522 : : don't. */
3523 : 305620 : if (dst_fid >= ipa_get_cs_argument_count (top))
3524 : : {
3525 : 0 : ipa_set_jf_unknown (dst);
3526 : 0 : continue;
3527 : : }
3528 : :
3529 : 152810 : src = ipa_get_ith_jump_func (top, dst_fid);
3530 : :
3531 : 152810 : if (src_ctx && !src_ctx->useless_p ())
3532 : : {
3533 : 44165 : class ipa_polymorphic_call_context ctx = *src_ctx;
3534 : :
3535 : : /* TODO: Make type preserved safe WRT contexts. */
3536 : 44165 : if (!ipa_get_jf_ancestor_type_preserved (dst))
3537 : 29806 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3538 : 44165 : ctx.offset_by (dst->value.ancestor.offset);
3539 : 88330 : if (!ctx.useless_p ())
3540 : : {
3541 : 41254 : if (!dst_ctx)
3542 : : {
3543 : 6806 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3544 : : count, true);
3545 : 6806 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3546 : : }
3547 : :
3548 : 41254 : dst_ctx->combine_with (ctx);
3549 : : }
3550 : : }
3551 : :
3552 : : /* Parameter and argument in ancestor jump function must be pointer
3553 : : type, which means access to aggregate must be by-reference. */
3554 : 152810 : gcc_assert (!src->agg.items || src->agg.by_ref);
3555 : :
3556 : 152810 : if (src->agg.items && dst->value.ancestor.agg_preserved)
3557 : : {
3558 : 1910 : struct ipa_agg_jf_item *item;
3559 : 1910 : int j;
3560 : :
3561 : : /* Currently we do not produce clobber aggregate jump functions,
3562 : : replace with merging when we do. */
3563 : 1910 : gcc_assert (!dst->agg.items);
3564 : :
3565 : 1910 : dst->agg.items = vec_safe_copy (src->agg.items);
3566 : 1910 : dst->agg.by_ref = src->agg.by_ref;
3567 : 6865 : FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
3568 : 4955 : item->offset -= dst->value.ancestor.offset;
3569 : : }
3570 : :
3571 : 152810 : if (src->type == IPA_JF_PASS_THROUGH
3572 : 25287 : && src->value.pass_through.operation == NOP_EXPR)
3573 : : {
3574 : 25283 : dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
3575 : 25283 : dst->value.ancestor.agg_preserved &=
3576 : 25283 : src->value.pass_through.agg_preserved;
3577 : : }
3578 : 127527 : else if (src->type == IPA_JF_ANCESTOR)
3579 : : {
3580 : 8796 : dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
3581 : 8796 : dst->value.ancestor.offset += src->value.ancestor.offset;
3582 : 8796 : dst->value.ancestor.agg_preserved &=
3583 : 8796 : src->value.ancestor.agg_preserved;
3584 : 8796 : dst->value.ancestor.keep_null |= src->value.ancestor.keep_null;
3585 : : }
3586 : : else
3587 : 118731 : ipa_set_jf_unknown (dst);
3588 : : }
3589 : 2567446 : else if (dst->type == IPA_JF_PASS_THROUGH)
3590 : : {
3591 : 726653 : struct ipa_jump_func *src;
3592 : : /* We must check range due to calls with variable number of arguments
3593 : : and we cannot combine jump functions with operations. */
3594 : 726653 : if (dst->value.pass_through.operation == NOP_EXPR
3595 : 726653 : && (top && dst->value.pass_through.formal_id
3596 : 694712 : < ipa_get_cs_argument_count (top)))
3597 : : {
3598 : 694698 : int dst_fid = dst->value.pass_through.formal_id;
3599 : 694698 : src = ipa_get_ith_jump_func (top, dst_fid);
3600 : 694698 : bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
3601 : 694698 : class ipa_polymorphic_call_context *src_ctx
3602 : 797180 : = ipa_get_ith_polymorhic_call_context (top, dst_fid);
3603 : :
3604 : 102482 : if (src_ctx && !src_ctx->useless_p ())
3605 : : {
3606 : 64916 : class ipa_polymorphic_call_context ctx = *src_ctx;
3607 : :
3608 : : /* TODO: Make type preserved safe WRT contexts. */
3609 : 64916 : if (!ipa_get_jf_pass_through_type_preserved (dst))
3610 : 31220 : ctx.possible_dynamic_type_change (e->in_polymorphic_cdtor);
3611 : 129832 : if (!ctx.useless_p ())
3612 : : {
3613 : 63292 : if (!dst_ctx)
3614 : : {
3615 : 12600 : vec_safe_grow_cleared (args->polymorphic_call_contexts,
3616 : : count, true);
3617 : 12600 : dst_ctx = ipa_get_ith_polymorhic_call_context (args, i);
3618 : : }
3619 : 63292 : dst_ctx->combine_with (ctx);
3620 : : }
3621 : : }
3622 : 694698 : switch (src->type)
3623 : : {
3624 : 284767 : case IPA_JF_UNKNOWN:
3625 : 284767 : ipa_set_jf_unknown (dst);
3626 : 284767 : break;
3627 : 131181 : case IPA_JF_CONST:
3628 : 131181 : {
3629 : 131181 : bool rd = ipa_get_jf_pass_through_refdesc_decremented (dst);
3630 : 131181 : ipa_set_jf_cst_copy (dst, src);
3631 : 131181 : if (rd)
3632 : 25 : ipa_zap_jf_refdesc (dst);
3633 : : }
3634 : :
3635 : : break;
3636 : :
3637 : 254866 : case IPA_JF_PASS_THROUGH:
3638 : 254866 : {
3639 : 254866 : int formal_id = ipa_get_jf_pass_through_formal_id (src);
3640 : 254866 : enum tree_code operation;
3641 : 254866 : operation = ipa_get_jf_pass_through_operation (src);
3642 : :
3643 : 254866 : tree old_ir_ptype = ipa_get_type (old_inline_root_info,
3644 : : dst_fid);
3645 : 254866 : tree new_ir_ptype = ipa_get_type (new_inline_root_info,
3646 : : formal_id);
3647 : 254866 : if (!useless_type_conversion_p (old_ir_ptype, new_ir_ptype))
3648 : : {
3649 : : /* Jump-function construction now permits type-casts
3650 : : from an integer to another if the latter can hold
3651 : : all values or has at least the same precision.
3652 : : However, as we're combining multiple pass-through
3653 : : functions together, we are losing information about
3654 : : signedness and thus if conversions should sign or
3655 : : zero extend. Therefore we must prevent combining
3656 : : such jump-function if signednesses do not match. */
3657 : 1788 : if (!INTEGRAL_TYPE_P (old_ir_ptype)
3658 : 1000 : || !INTEGRAL_TYPE_P (new_ir_ptype)
3659 : 2000 : || (TYPE_UNSIGNED (new_ir_ptype)
3660 : 1000 : != TYPE_UNSIGNED (old_ir_ptype)))
3661 : : {
3662 : 788 : ipa_set_jf_unknown (dst);
3663 : 788 : continue;
3664 : : }
3665 : : }
3666 : :
3667 : 254078 : if (operation == NOP_EXPR)
3668 : : {
3669 : 253059 : bool agg_p;
3670 : 506118 : agg_p = dst_agg_p
3671 : 253059 : && ipa_get_jf_pass_through_agg_preserved (src);
3672 : 253059 : ipa_set_jf_simple_pass_through (dst, formal_id, agg_p);
3673 : : }
3674 : 1019 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
3675 : : {
3676 : 9 : tree op_t = ipa_get_jf_pass_through_op_type (src);
3677 : 9 : ipa_set_jf_unary_pass_through (dst, formal_id, operation,
3678 : : op_t);
3679 : : }
3680 : : else
3681 : : {
3682 : 1010 : tree operand = ipa_get_jf_pass_through_operand (src);
3683 : 1010 : tree op_t = ipa_get_jf_pass_through_op_type (src);
3684 : 1010 : ipa_set_jf_arith_pass_through (dst, formal_id, operand,
3685 : : operation, op_t);
3686 : : }
3687 : : break;
3688 : : }
3689 : 23884 : case IPA_JF_ANCESTOR:
3690 : 23884 : {
3691 : 23884 : bool agg_p;
3692 : 47768 : agg_p = dst_agg_p
3693 : 23884 : && ipa_get_jf_ancestor_agg_preserved (src);
3694 : 23884 : ipa_set_ancestor_jf (dst,
3695 : : ipa_get_jf_ancestor_offset (src),
3696 : : ipa_get_jf_ancestor_formal_id (src),
3697 : : agg_p,
3698 : 23884 : ipa_get_jf_ancestor_keep_null (src));
3699 : 23884 : break;
3700 : : }
3701 : 0 : default:
3702 : 0 : gcc_unreachable ();
3703 : : }
3704 : :
3705 : 693910 : if (src->m_vr && src->m_vr->known_p ())
3706 : : {
3707 : 469388 : value_range svr (src->m_vr->type ());
3708 : 469388 : if (!dst->m_vr || !dst->m_vr->known_p ())
3709 : 184963 : ipa_set_jfunc_vr (dst, *src->m_vr);
3710 : 284425 : else if (ipa_vr_operation_and_type_effects (svr, *src->m_vr,
3711 : : NOP_EXPR,
3712 : 284425 : dst->m_vr->type (),
3713 : 284425 : src->m_vr->type ()))
3714 : : {
3715 : 284417 : value_range dvr;
3716 : 284417 : dst->m_vr->get_vrange (dvr);
3717 : 284417 : dvr.intersect (svr);
3718 : 284417 : if (!dvr.undefined_p ())
3719 : 278543 : ipa_set_jfunc_vr (dst, dvr);
3720 : 284417 : }
3721 : 469388 : }
3722 : :
3723 : 693910 : if (src->agg.items
3724 : 28149 : && (dst_agg_p || !src->agg.by_ref))
3725 : : {
3726 : : /* Currently we do not produce clobber aggregate jump
3727 : : functions, replace with merging when we do. */
3728 : 22449 : gcc_assert (!dst->agg.items);
3729 : :
3730 : 22449 : dst->agg.by_ref = src->agg.by_ref;
3731 : 22449 : dst->agg.items = vec_safe_copy (src->agg.items);
3732 : : }
3733 : : }
3734 : : else
3735 : 31955 : ipa_set_jf_unknown (dst);
3736 : : }
3737 : : }
3738 : : }
3739 : :
3740 : : /* If TARGET is an addr_expr of a function declaration, make it the
3741 : : (SPECULATIVE)destination of an indirect edge IE and return the edge.
3742 : : Otherwise, return NULL. */
3743 : :
3744 : : struct cgraph_edge *
3745 : 4199 : ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target,
3746 : : bool speculative)
3747 : : {
3748 : 4199 : struct cgraph_node *callee;
3749 : 4199 : bool unreachable = false;
3750 : :
3751 : 4199 : if (TREE_CODE (target) == ADDR_EXPR)
3752 : 1247 : target = TREE_OPERAND (target, 0);
3753 : 4199 : if (TREE_CODE (target) != FUNCTION_DECL)
3754 : : {
3755 : 17 : target = canonicalize_constructor_val (target, NULL);
3756 : 17 : if (!target || TREE_CODE (target) != FUNCTION_DECL)
3757 : : {
3758 : : /* Member pointer call that goes through a VMT lookup. */
3759 : 17 : if (ie->indirect_info->member_ptr
3760 : : /* Or if target is not an invariant expression and we do not
3761 : : know if it will evaulate to function at runtime.
3762 : : This can happen when folding through &VAR, where &VAR
3763 : : is IP invariant, but VAR itself is not.
3764 : :
3765 : : TODO: Revisit this when GCC 5 is branched. It seems that
3766 : : member_ptr check is not needed and that we may try to fold
3767 : : the expression and see if VAR is readonly. */
3768 : 17 : || !is_gimple_ip_invariant (target))
3769 : : {
3770 : 6 : if (dump_enabled_p ())
3771 : : {
3772 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3773 : : "discovered direct call non-invariant %s\n",
3774 : 0 : ie->caller->dump_name ());
3775 : : }
3776 : 6 : return NULL;
3777 : : }
3778 : :
3779 : :
3780 : 11 : if (dump_enabled_p ())
3781 : : {
3782 : 0 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3783 : : "discovered direct call to non-function in %s, "
3784 : : "making it __builtin_unreachable\n",
3785 : 0 : ie->caller->dump_name ());
3786 : : }
3787 : :
3788 : 11 : target = builtin_decl_unreachable ();
3789 : 11 : callee = cgraph_node::get_create (target);
3790 : 11 : unreachable = true;
3791 : : }
3792 : : else
3793 : 0 : callee = cgraph_node::get (target);
3794 : : }
3795 : : else
3796 : 4182 : callee = cgraph_node::get (target);
3797 : :
3798 : : /* Because may-edges are not explicitely represented and vtable may be external,
3799 : : we may create the first reference to the object in the unit. */
3800 : 4193 : if (!callee || callee->inlined_to)
3801 : : {
3802 : :
3803 : : /* We are better to ensure we can refer to it.
3804 : : In the case of static functions we are out of luck, since we already
3805 : : removed its body. In the case of public functions we may or may
3806 : : not introduce the reference. */
3807 : 0 : if (!canonicalize_constructor_val (target, NULL)
3808 : 0 : || !TREE_PUBLIC (target))
3809 : : {
3810 : 0 : if (dump_file)
3811 : 0 : fprintf (dump_file, "ipa-prop: Discovered call to a known target "
3812 : : "(%s -> %s) but cannot refer to it. Giving up.\n",
3813 : 0 : ie->caller->dump_name (),
3814 : 0 : ie->callee->dump_name ());
3815 : 0 : return NULL;
3816 : : }
3817 : 0 : callee = cgraph_node::get_create (target);
3818 : : }
3819 : :
3820 : : /* If the edge is already speculated. */
3821 : 4193 : if (speculative && ie->speculative)
3822 : : {
3823 : 1 : if (dump_file)
3824 : : {
3825 : 0 : cgraph_edge *e2 = ie->speculative_call_for_target (callee);
3826 : 0 : if (!e2)
3827 : : {
3828 : 0 : if (dump_file)
3829 : 0 : fprintf (dump_file, "ipa-prop: Discovered call to a "
3830 : : "speculative target (%s -> %s) but the call is "
3831 : : "already speculated to different target. "
3832 : : "Giving up.\n",
3833 : 0 : ie->caller->dump_name (), callee->dump_name ());
3834 : : }
3835 : : else
3836 : : {
3837 : 0 : if (dump_file)
3838 : 0 : fprintf (dump_file,
3839 : : "ipa-prop: Discovered call to a speculative target "
3840 : : "(%s -> %s) this agree with previous speculation.\n",
3841 : 0 : ie->caller->dump_name (), callee->dump_name ());
3842 : : }
3843 : : }
3844 : 1 : return NULL;
3845 : : }
3846 : :
3847 : 4192 : if (!dbg_cnt (devirt))
3848 : : return NULL;
3849 : :
3850 : 4192 : ipa_check_create_node_params ();
3851 : :
3852 : : /* We cannot make edges to inline clones. It is bug that someone removed
3853 : : the cgraph node too early. */
3854 : 4192 : gcc_assert (!callee->inlined_to);
3855 : :
3856 : 4192 : if (dump_file && !unreachable)
3857 : : {
3858 : 591 : fprintf (dump_file, "ipa-prop: Discovered %s call to a %s target "
3859 : : "(%s -> %s), for stmt ",
3860 : 197 : ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
3861 : : speculative ? "speculative" : "known",
3862 : 197 : ie->caller->dump_name (),
3863 : : callee->dump_name ());
3864 : 197 : if (ie->call_stmt)
3865 : 190 : print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
3866 : : else
3867 : 7 : fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
3868 : : }
3869 : 4192 : if (dump_enabled_p ())
3870 : : {
3871 : 394 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, ie->call_stmt,
3872 : : "converting indirect call in %s to direct call to %s\n",
3873 : 197 : ie->caller->dump_name (), callee->dump_name ());
3874 : : }
3875 : 4192 : if (!speculative)
3876 : : {
3877 : 4164 : struct cgraph_edge *orig = ie;
3878 : 4164 : ie = cgraph_edge::make_direct (ie, callee);
3879 : : /* If we resolved speculative edge the cost is already up to date
3880 : : for direct call (adjusted by inline_edge_duplication_hook). */
3881 : 4164 : if (ie == orig)
3882 : : {
3883 : 3065 : ipa_call_summary *es = ipa_call_summaries->get (ie);
3884 : 3065 : es->call_stmt_size -= (eni_size_weights.indirect_call_cost
3885 : 3065 : - eni_size_weights.call_cost);
3886 : 3065 : es->call_stmt_time -= (eni_time_weights.indirect_call_cost
3887 : 3065 : - eni_time_weights.call_cost);
3888 : : }
3889 : : }
3890 : : else
3891 : : {
3892 : 28 : if (!callee->can_be_discarded_p ())
3893 : : {
3894 : 4 : cgraph_node *alias;
3895 : 4 : alias = dyn_cast<cgraph_node *> (callee->noninterposable_alias ());
3896 : : if (alias)
3897 : 28 : callee = alias;
3898 : : }
3899 : : /* make_speculative will update ie's cost to direct call cost. */
3900 : 28 : ie = ie->make_speculative
3901 : 28 : (callee, ie->count.apply_scale (8, 10));
3902 : : }
3903 : :
3904 : : return ie;
3905 : : }
3906 : :
3907 : : /* Attempt to locate an interprocedural constant at a given REQ_OFFSET in
3908 : : CONSTRUCTOR and return it. Return NULL if the search fails for some
3909 : : reason. */
3910 : :
3911 : : static tree
3912 : 8993 : find_constructor_constant_at_offset (tree constructor, HOST_WIDE_INT req_offset)
3913 : : {
3914 : 13942 : tree type = TREE_TYPE (constructor);
3915 : 13942 : if (TREE_CODE (type) != ARRAY_TYPE
3916 : 13942 : && TREE_CODE (type) != RECORD_TYPE)
3917 : : return NULL;
3918 : :
3919 : 13930 : unsigned ix;
3920 : 13930 : tree index, val;
3921 : 19410 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (constructor), ix, index, val)
3922 : : {
3923 : 18945 : HOST_WIDE_INT elt_offset;
3924 : 18945 : if (TREE_CODE (type) == ARRAY_TYPE)
3925 : : {
3926 : 269 : offset_int off;
3927 : 269 : tree unit_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
3928 : 269 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
3929 : :
3930 : 269 : if (index)
3931 : : {
3932 : 269 : if (TREE_CODE (index) == RANGE_EXPR)
3933 : 48 : off = wi::to_offset (TREE_OPERAND (index, 0));
3934 : : else
3935 : 221 : off = wi::to_offset (index);
3936 : 269 : if (TYPE_DOMAIN (type) && TYPE_MIN_VALUE (TYPE_DOMAIN (type)))
3937 : : {
3938 : 269 : tree low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3939 : 269 : gcc_assert (TREE_CODE (unit_size) == INTEGER_CST);
3940 : 269 : off = wi::sext (off - wi::to_offset (low_bound),
3941 : 269 : TYPE_PRECISION (TREE_TYPE (index)));
3942 : : }
3943 : 269 : off *= wi::to_offset (unit_size);
3944 : : /* ??? Handle more than just the first index of a
3945 : : RANGE_EXPR. */
3946 : : }
3947 : : else
3948 : 0 : off = wi::to_offset (unit_size) * ix;
3949 : :
3950 : 269 : off = wi::lshift (off, LOG2_BITS_PER_UNIT);
3951 : 269 : if (!wi::fits_shwi_p (off) || wi::neg_p (off))
3952 : 0 : continue;
3953 : 269 : elt_offset = off.to_shwi ();
3954 : : }
3955 : 18676 : else if (TREE_CODE (type) == RECORD_TYPE)
3956 : : {
3957 : 18676 : gcc_checking_assert (index && TREE_CODE (index) == FIELD_DECL);
3958 : 18676 : if (DECL_BIT_FIELD (index))
3959 : 0 : continue;
3960 : 18676 : elt_offset = int_bit_position (index);
3961 : : }
3962 : : else
3963 : 0 : gcc_unreachable ();
3964 : :
3965 : 18945 : if (elt_offset > req_offset)
3966 : : return NULL;
3967 : :
3968 : 18945 : if (TREE_CODE (val) == CONSTRUCTOR)
3969 : 4949 : return find_constructor_constant_at_offset (val,
3970 : 4949 : req_offset - elt_offset);
3971 : :
3972 : 13996 : if (elt_offset == req_offset
3973 : 8981 : && is_gimple_reg_type (TREE_TYPE (val))
3974 : 22977 : && is_gimple_ip_invariant (val))
3975 : : return val;
3976 : : }
3977 : : return NULL;
3978 : : }
3979 : :
3980 : : /* Check whether SCALAR could be used to look up an aggregate interprocedural
3981 : : invariant from a static constructor and if so, return it. Otherwise return
3982 : : NULL. */
3983 : :
3984 : : tree
3985 : 13509333 : ipa_find_agg_cst_from_init (tree scalar, HOST_WIDE_INT offset, bool by_ref)
3986 : : {
3987 : 13509333 : if (by_ref)
3988 : : {
3989 : 13497172 : if (TREE_CODE (scalar) != ADDR_EXPR)
3990 : : return NULL;
3991 : 4397141 : scalar = TREE_OPERAND (scalar, 0);
3992 : : }
3993 : :
3994 : 4409302 : if (!VAR_P (scalar)
3995 : 2903087 : || !is_global_var (scalar)
3996 : 112378 : || !TREE_READONLY (scalar)
3997 : 13124 : || !DECL_INITIAL (scalar)
3998 : 4420403 : || TREE_CODE (DECL_INITIAL (scalar)) != CONSTRUCTOR)
3999 : : return NULL;
4000 : :
4001 : 8993 : return find_constructor_constant_at_offset (DECL_INITIAL (scalar), offset);
4002 : : }
4003 : :
4004 : : /* Retrieve value from AGG_JFUNC for the given OFFSET or return NULL if there
4005 : : is none. BY_REF specifies whether the value has to be passed by reference
4006 : : or by value. */
4007 : :
4008 : : static tree
4009 : 31772 : ipa_find_agg_cst_from_jfunc_items (struct ipa_agg_jump_function *agg_jfunc,
4010 : : ipa_node_params *src_info,
4011 : : cgraph_node *src_node,
4012 : : HOST_WIDE_INT offset, bool by_ref)
4013 : : {
4014 : 31772 : if (by_ref != agg_jfunc->by_ref)
4015 : : return NULL_TREE;
4016 : :
4017 : 4969 : for (const ipa_agg_jf_item &item : agg_jfunc->items)
4018 : 1756 : if (item.offset == offset)
4019 : 1307 : return ipa_agg_value_from_jfunc (src_info, src_node, &item);
4020 : :
4021 : : return NULL_TREE;
4022 : : }
4023 : :
4024 : : /* Remove a reference to SYMBOL from the list of references of a node given by
4025 : : reference description RDESC. Return true if the reference has been
4026 : : successfully found and removed. */
4027 : :
4028 : : static bool
4029 : 9493 : remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
4030 : : {
4031 : 9493 : struct ipa_ref *to_del;
4032 : 9493 : struct cgraph_edge *origin;
4033 : :
4034 : 9493 : origin = rdesc->cs;
4035 : 9493 : if (!origin)
4036 : : return false;
4037 : 9493 : to_del = origin->caller->find_reference (symbol, origin->call_stmt,
4038 : : origin->lto_stmt_uid, IPA_REF_ADDR);
4039 : 9493 : if (!to_del)
4040 : : return false;
4041 : :
4042 : 9493 : to_del->remove_reference ();
4043 : 9493 : if (dump_file)
4044 : 26 : fprintf (dump_file, "ipa-prop: Removed a reference from %s to %s.\n",
4045 : 13 : origin->caller->dump_name (), symbol->dump_name ());
4046 : : return true;
4047 : : }
4048 : :
4049 : : /* If JFUNC has a reference description with refcount different from
4050 : : IPA_UNDESCRIBED_USE, return the reference description, otherwise return
4051 : : NULL. JFUNC must be a constant jump function. */
4052 : :
4053 : : static struct ipa_cst_ref_desc *
4054 : 1267885 : jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
4055 : : {
4056 : 1267885 : struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
4057 : 1267885 : if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
4058 : : return rdesc;
4059 : : else
4060 : 1076305 : return NULL;
4061 : : }
4062 : :
4063 : : /* If the value of constant jump function JFUNC is an address of a function
4064 : : declaration, return the associated call graph node. Otherwise return
4065 : : NULL. */
4066 : :
4067 : : static symtab_node *
4068 : 1659 : symtab_node_for_jfunc (struct ipa_jump_func *jfunc)
4069 : : {
4070 : 1659 : gcc_checking_assert (jfunc->type == IPA_JF_CONST);
4071 : 1659 : tree cst = ipa_get_jf_constant (jfunc);
4072 : 1659 : if (TREE_CODE (cst) != ADDR_EXPR
4073 : 1659 : || (TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL
4074 : 10 : && TREE_CODE (TREE_OPERAND (cst, 0)) != VAR_DECL))
4075 : : return NULL;
4076 : :
4077 : 1649 : return symtab_node::get (TREE_OPERAND (cst, 0));
4078 : : }
4079 : :
4080 : :
4081 : : /* If JFUNC is a constant jump function with a usable rdesc, decrement its
4082 : : refcount and if it hits zero, remove reference to SYMBOL from the caller of
4083 : : the edge specified in the rdesc. Return false if either the symbol or the
4084 : : reference could not be found, otherwise return true. */
4085 : :
4086 : : static bool
4087 : 900 : try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
4088 : : {
4089 : 900 : struct ipa_cst_ref_desc *rdesc;
4090 : 900 : if (jfunc->type == IPA_JF_CONST
4091 : 900 : && (rdesc = jfunc_rdesc_usable (jfunc))
4092 : 1775 : && --rdesc->refcount == 0)
4093 : : {
4094 : 745 : symtab_node *symbol = symtab_node_for_jfunc (jfunc);
4095 : 745 : if (!symbol)
4096 : : return false;
4097 : :
4098 : 745 : return remove_described_reference (symbol, rdesc);
4099 : : }
4100 : : return true;
4101 : : }
4102 : :
4103 : : /* Try to find a destination for indirect edge IE that corresponds to a simple
4104 : : call or a call of a member function pointer and where the destination is a
4105 : : pointer formal parameter described by jump function JFUNC. TARGET_TYPE is
4106 : : the type of the parameter to which the result of JFUNC is passed. If it can
4107 : : be determined, return the newly direct edge, otherwise return NULL.
4108 : : NEW_ROOT and NEW_ROOT_INFO is the node and its info that JFUNC lattices are
4109 : : relative to. */
4110 : :
4111 : : static struct cgraph_edge *
4112 : 10916 : try_make_edge_direct_simple_call (struct cgraph_edge *ie,
4113 : : struct ipa_jump_func *jfunc, tree target_type,
4114 : : struct cgraph_node *new_root,
4115 : : class ipa_node_params *new_root_info)
4116 : : {
4117 : 10916 : struct cgraph_edge *cs;
4118 : 10916 : tree target = NULL_TREE;
4119 : 10916 : bool agg_contents = ie->indirect_info->agg_contents;
4120 : 10916 : tree scalar = ipa_value_from_jfunc (new_root_info, jfunc, target_type);
4121 : 10916 : if (agg_contents)
4122 : : {
4123 : 8909 : if (scalar)
4124 : 15 : target = ipa_find_agg_cst_from_init (scalar, ie->indirect_info->offset,
4125 : 15 : ie->indirect_info->by_ref);
4126 : 8909 : if (!target && ie->indirect_info->guaranteed_unmodified)
4127 : 7763 : target = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
4128 : : new_root,
4129 : : ie->indirect_info->offset,
4130 : : ie->indirect_info->by_ref);
4131 : : }
4132 : : else
4133 : : target = scalar;
4134 : 10915 : if (!target)
4135 : : return NULL;
4136 : 1264 : cs = ipa_make_edge_direct_to_target (ie, target);
4137 : :
4138 : 1264 : if (cs && !agg_contents)
4139 : : {
4140 : 900 : bool ok;
4141 : 900 : gcc_checking_assert (cs->callee
4142 : : && (cs != ie
4143 : : || jfunc->type != IPA_JF_CONST
4144 : : || !symtab_node_for_jfunc (jfunc)
4145 : : || cs->callee == symtab_node_for_jfunc (jfunc)));
4146 : 900 : ok = try_decrement_rdesc_refcount (jfunc);
4147 : 900 : gcc_checking_assert (ok);
4148 : : }
4149 : :
4150 : : return cs;
4151 : : }
4152 : :
4153 : : /* Return the target to be used in cases of impossible devirtualization. IE
4154 : : and target (the latter can be NULL) are dumped when dumping is enabled. */
4155 : :
4156 : : tree
4157 : 508 : ipa_impossible_devirt_target (struct cgraph_edge *ie, tree target)
4158 : : {
4159 : 508 : if (dump_file)
4160 : : {
4161 : 57 : if (target)
4162 : 18 : fprintf (dump_file,
4163 : : "Type inconsistent devirtualization: %s->%s\n",
4164 : 18 : ie->caller->dump_name (),
4165 : 18 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
4166 : : else
4167 : 39 : fprintf (dump_file,
4168 : : "No devirtualization target in %s\n",
4169 : 39 : ie->caller->dump_name ());
4170 : : }
4171 : 508 : tree new_target = builtin_decl_unreachable ();
4172 : 508 : cgraph_node::get_create (new_target);
4173 : 508 : return new_target;
4174 : : }
4175 : :
4176 : : /* Try to find a destination for indirect edge IE that corresponds to a virtual
4177 : : call based on a formal parameter which is described by jump function JFUNC
4178 : : and if it can be determined, make it direct and return the direct edge.
4179 : : Otherwise, return NULL. CTX describes the polymorphic context that the
4180 : : parameter the call is based on brings along with it. NEW_ROOT and
4181 : : NEW_ROOT_INFO is the node and its info that JFUNC lattices are relative
4182 : : to. */
4183 : :
4184 : : static struct cgraph_edge *
4185 : 24012 : try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
4186 : : struct ipa_jump_func *jfunc,
4187 : : class ipa_polymorphic_call_context ctx,
4188 : : struct cgraph_node *new_root,
4189 : : class ipa_node_params *new_root_info)
4190 : : {
4191 : 24012 : tree target = NULL;
4192 : 24012 : bool speculative = false;
4193 : :
4194 : 24012 : if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
4195 : : return NULL;
4196 : :
4197 : 24012 : gcc_assert (!ie->indirect_info->by_ref);
4198 : :
4199 : : /* Try to do lookup via known virtual table pointer value. */
4200 : 24012 : if (!ie->indirect_info->vptr_changed
4201 : 24012 : || opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively))
4202 : : {
4203 : 24012 : tree vtable;
4204 : 24012 : unsigned HOST_WIDE_INT offset;
4205 : 24012 : tree t = NULL_TREE;
4206 : 24012 : if (jfunc->type == IPA_JF_CONST)
4207 : 330 : t = ipa_find_agg_cst_from_init (ipa_get_jf_constant (jfunc),
4208 : : ie->indirect_info->offset, true);
4209 : 330 : if (!t)
4210 : 24009 : t = ipa_find_agg_cst_from_jfunc_items (&jfunc->agg, new_root_info,
4211 : : new_root,
4212 : 24009 : ie->indirect_info->offset, true);
4213 : 24012 : if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset))
4214 : : {
4215 : 921 : bool can_refer;
4216 : 921 : t = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
4217 : : vtable, offset, &can_refer);
4218 : 921 : if (can_refer)
4219 : : {
4220 : 904 : if (!t
4221 : 904 : || fndecl_built_in_p (t, BUILT_IN_UNREACHABLE,
4222 : : BUILT_IN_UNREACHABLE_TRAP)
4223 : 1760 : || !possible_polymorphic_call_target_p
4224 : 856 : (ie, cgraph_node::get (t)))
4225 : : {
4226 : : /* Do not speculate builtin_unreachable, it is stupid! */
4227 : 90 : if (!ie->indirect_info->vptr_changed)
4228 : 90 : target = ipa_impossible_devirt_target (ie, target);
4229 : : else
4230 : : target = NULL;
4231 : : }
4232 : : else
4233 : : {
4234 : 814 : target = t;
4235 : 814 : speculative = ie->indirect_info->vptr_changed;
4236 : : }
4237 : : }
4238 : : }
4239 : : }
4240 : :
4241 : 24012 : ipa_polymorphic_call_context ie_context (ie);
4242 : 24012 : vec <cgraph_node *>targets;
4243 : 24012 : bool final;
4244 : :
4245 : 24012 : ctx.offset_by (ie->indirect_info->offset);
4246 : 24012 : if (ie->indirect_info->vptr_changed)
4247 : 8625 : ctx.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
4248 : : ie->indirect_info->otr_type);
4249 : 24012 : ctx.combine_with (ie_context, ie->indirect_info->otr_type);
4250 : 24012 : targets = possible_polymorphic_call_targets
4251 : 48024 : (ie->indirect_info->otr_type,
4252 : 24012 : ie->indirect_info->otr_token,
4253 : : ctx, &final);
4254 : 26385 : if (final && targets.length () <= 1)
4255 : : {
4256 : 2338 : speculative = false;
4257 : 2338 : if (targets.length () == 1)
4258 : 2287 : target = targets[0]->decl;
4259 : : else
4260 : 51 : target = ipa_impossible_devirt_target (ie, NULL_TREE);
4261 : : }
4262 : 21665 : else if (!target && opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
4263 : 43257 : && !ie->speculative && ie->maybe_hot_p ())
4264 : : {
4265 : 14549 : cgraph_node *n;
4266 : 29098 : n = try_speculative_devirtualization (ie->indirect_info->otr_type,
4267 : : ie->indirect_info->otr_token,
4268 : 14549 : ie->indirect_info->context);
4269 : 14549 : if (n)
4270 : : {
4271 : 19 : target = n->decl;
4272 : 19 : speculative = true;
4273 : : }
4274 : : }
4275 : :
4276 : 24012 : if (target)
4277 : : {
4278 : 2366 : if (!possible_polymorphic_call_target_p
4279 : 2366 : (ie, cgraph_node::get_create (target)))
4280 : : {
4281 : 51 : if (speculative)
4282 : : return NULL;
4283 : 51 : target = ipa_impossible_devirt_target (ie, target);
4284 : : }
4285 : 2366 : return ipa_make_edge_direct_to_target (ie, target, speculative);
4286 : : }
4287 : : else
4288 : : return NULL;
4289 : : }
4290 : :
4291 : : /* Update the param called notes associated with NODE when CS is being inlined,
4292 : : assuming NODE is (potentially indirectly) inlined into CS->callee.
4293 : : Moreover, if the callee is discovered to be constant, create a new cgraph
4294 : : edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
4295 : : unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
4296 : :
4297 : : static bool
4298 : 1604068 : update_indirect_edges_after_inlining (struct cgraph_edge *cs,
4299 : : struct cgraph_node *node,
4300 : : vec<cgraph_edge *> *new_edges)
4301 : : {
4302 : 1604068 : class ipa_edge_args *top;
4303 : 1604068 : struct cgraph_edge *ie, *next_ie, *new_direct_edge;
4304 : 1604068 : struct cgraph_node *new_root;
4305 : 1604068 : class ipa_node_params *new_root_info, *inlined_node_info;
4306 : 1604068 : bool res = false;
4307 : :
4308 : 1604068 : ipa_check_create_edge_args ();
4309 : 1604068 : top = ipa_edge_args_sum->get (cs);
4310 : 1164967 : new_root = cs->caller->inlined_to
4311 : 1604068 : ? cs->caller->inlined_to : cs->caller;
4312 : 1604068 : new_root_info = ipa_node_params_sum->get (new_root);
4313 : 1604068 : inlined_node_info = ipa_node_params_sum->get (cs->callee->function_symbol ());
4314 : :
4315 : 1685710 : for (ie = node->indirect_calls; ie; ie = next_ie)
4316 : : {
4317 : 81642 : class cgraph_indirect_call_info *ici = ie->indirect_info;
4318 : 81642 : struct ipa_jump_func *jfunc;
4319 : 81642 : int param_index;
4320 : :
4321 : 81642 : next_ie = ie->next_callee;
4322 : :
4323 : 81642 : if (ici->param_index == -1)
4324 : 96583 : continue;
4325 : :
4326 : : /* We must check range due to calls with variable number of arguments: */
4327 : 70301 : if (!top || ici->param_index >= ipa_get_cs_argument_count (top))
4328 : : {
4329 : 35 : ici->param_index = -1;
4330 : 35 : continue;
4331 : : }
4332 : :
4333 : 35132 : param_index = ici->param_index;
4334 : 35132 : jfunc = ipa_get_ith_jump_func (top, param_index);
4335 : :
4336 : 35132 : auto_vec<cgraph_node *, 4> spec_targets;
4337 : 35132 : if (ie->speculative)
4338 : 7787 : for (cgraph_edge *direct = ie->first_speculative_call_target ();
4339 : 19395 : direct;
4340 : 11608 : direct = direct->next_speculative_call_target ())
4341 : 11608 : spec_targets.safe_push (direct->callee);
4342 : :
4343 : 35132 : if (!opt_for_fn (node->decl, flag_indirect_inlining))
4344 : 204 : new_direct_edge = NULL;
4345 : 34928 : else if (ici->polymorphic)
4346 : : {
4347 : 24012 : ipa_polymorphic_call_context ctx;
4348 : 24012 : ctx = ipa_context_from_jfunc (new_root_info, cs, param_index, jfunc);
4349 : 24012 : new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc, ctx,
4350 : : new_root,
4351 : : new_root_info);
4352 : : }
4353 : : else
4354 : : {
4355 : 10916 : tree target_type = ipa_get_type (inlined_node_info, param_index);
4356 : 10916 : new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
4357 : : target_type,
4358 : : new_root,
4359 : : new_root_info);
4360 : : }
4361 : :
4362 : : /* If speculation was removed, then we need to do nothing. */
4363 : 3623 : if (new_direct_edge && new_direct_edge != ie
4364 : 36184 : && spec_targets.contains (new_direct_edge->callee))
4365 : : {
4366 : 1027 : new_direct_edge->indirect_inlining_edge = 1;
4367 : 1027 : res = true;
4368 : 1027 : if (!new_direct_edge->speculative)
4369 : 1027 : continue;
4370 : : }
4371 : 34105 : else if (new_direct_edge)
4372 : : {
4373 : 2596 : new_direct_edge->indirect_inlining_edge = 1;
4374 : 2596 : if (new_edges)
4375 : : {
4376 : 2584 : new_edges->safe_push (new_direct_edge);
4377 : 2584 : res = true;
4378 : : }
4379 : : /* If speculative edge was introduced we still need to update
4380 : : call info of the indirect edge. */
4381 : 2596 : if (!new_direct_edge->speculative)
4382 : 2571 : continue;
4383 : : }
4384 : 31534 : if (jfunc->type == IPA_JF_PASS_THROUGH
4385 : 31534 : && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
4386 : : {
4387 : 6198 : if (ici->agg_contents
4388 : 1521 : && !ipa_get_jf_pass_through_agg_preserved (jfunc)
4389 : 7187 : && !ici->polymorphic)
4390 : 989 : ici->param_index = -1;
4391 : : else
4392 : : {
4393 : 5209 : ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
4394 : 5209 : if (ici->polymorphic
4395 : 5209 : && !ipa_get_jf_pass_through_type_preserved (jfunc))
4396 : 2359 : ici->vptr_changed = true;
4397 : 5209 : ipa_set_param_used_by_indirect_call (new_root_info,
4398 : : ici->param_index, true);
4399 : 5209 : if (ici->polymorphic)
4400 : 3617 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4401 : : ici->param_index, true);
4402 : : }
4403 : : }
4404 : 25336 : else if (jfunc->type == IPA_JF_ANCESTOR)
4405 : : {
4406 : 600 : if (ici->agg_contents
4407 : 163 : && !ipa_get_jf_ancestor_agg_preserved (jfunc)
4408 : 657 : && !ici->polymorphic)
4409 : 57 : ici->param_index = -1;
4410 : : else
4411 : : {
4412 : 543 : ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
4413 : 543 : ici->offset += ipa_get_jf_ancestor_offset (jfunc);
4414 : 543 : if (ici->polymorphic
4415 : 543 : && !ipa_get_jf_ancestor_type_preserved (jfunc))
4416 : 397 : ici->vptr_changed = true;
4417 : 543 : ipa_set_param_used_by_indirect_call (new_root_info,
4418 : : ici->param_index, true);
4419 : 543 : if (ici->polymorphic)
4420 : 437 : ipa_set_param_used_by_polymorphic_call (new_root_info,
4421 : : ici->param_index, true);
4422 : : }
4423 : : }
4424 : : else
4425 : : /* Either we can find a destination for this edge now or never. */
4426 : 24736 : ici->param_index = -1;
4427 : 35132 : }
4428 : :
4429 : 1604068 : return res;
4430 : : }
4431 : :
4432 : : /* Recursively traverse subtree of NODE (including node) made of inlined
4433 : : cgraph_edges when CS has been inlined and invoke
4434 : : update_indirect_edges_after_inlining on all nodes and
4435 : : update_jump_functions_after_inlining on all non-inlined edges that lead out
4436 : : of this subtree. Newly discovered indirect edges will be added to
4437 : : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
4438 : : created. */
4439 : :
4440 : : static bool
4441 : 1604068 : propagate_info_to_inlined_callees (struct cgraph_edge *cs,
4442 : : struct cgraph_node *node,
4443 : : vec<cgraph_edge *> *new_edges)
4444 : : {
4445 : 1604068 : struct cgraph_edge *e;
4446 : 1604068 : bool res;
4447 : :
4448 : 1604068 : res = update_indirect_edges_after_inlining (cs, node, new_edges);
4449 : :
4450 : 4679426 : for (e = node->callees; e; e = e->next_callee)
4451 : 3075358 : if (!e->inline_failed)
4452 : 669233 : res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
4453 : : else
4454 : 2406125 : update_jump_functions_after_inlining (cs, e);
4455 : 1682112 : for (e = node->indirect_calls; e; e = e->next_callee)
4456 : 78044 : update_jump_functions_after_inlining (cs, e);
4457 : :
4458 : 1604068 : return res;
4459 : : }
4460 : :
4461 : : /* Combine two controlled uses counts as done during inlining. */
4462 : :
4463 : : static int
4464 : 400369 : combine_controlled_uses_counters (int c, int d)
4465 : : {
4466 : 0 : if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
4467 : : return IPA_UNDESCRIBED_USE;
4468 : : else
4469 : 111463 : return c + d - 1;
4470 : : }
4471 : :
4472 : : /* Propagate number of controlled users from CS->caleee to the new root of the
4473 : : tree of inlined nodes. */
4474 : :
4475 : : static void
4476 : 934835 : propagate_controlled_uses (struct cgraph_edge *cs)
4477 : : {
4478 : 934835 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4479 : 934835 : if (!args)
4480 : : return;
4481 : 699565 : struct cgraph_node *new_root = cs->caller->inlined_to
4482 : 933110 : ? cs->caller->inlined_to : cs->caller;
4483 : 933110 : ipa_node_params *new_root_info = ipa_node_params_sum->get (new_root);
4484 : 933110 : ipa_node_params *old_root_info = ipa_node_params_sum->get (cs->callee);
4485 : 933110 : int count, i;
4486 : :
4487 : 933110 : if (!old_root_info)
4488 : : return;
4489 : :
4490 : 1811601 : count = MIN (ipa_get_cs_argument_count (args),
4491 : : ipa_get_param_count (old_root_info));
4492 : 2801397 : for (i = 0; i < count; i++)
4493 : : {
4494 : 1868370 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4495 : 1868370 : struct ipa_cst_ref_desc *rdesc;
4496 : :
4497 : 1868370 : if (jf->type == IPA_JF_PASS_THROUGH
4498 : 1868370 : && !ipa_get_jf_pass_through_refdesc_decremented (jf))
4499 : : {
4500 : 342853 : int src_idx, c, d;
4501 : 342853 : src_idx = ipa_get_jf_pass_through_formal_id (jf);
4502 : 342853 : c = ipa_get_controlled_uses (new_root_info, src_idx);
4503 : 342853 : d = ipa_get_controlled_uses (old_root_info, i);
4504 : :
4505 : 342853 : gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
4506 : : == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
4507 : 342853 : c = combine_controlled_uses_counters (c, d);
4508 : 342853 : ipa_set_controlled_uses (new_root_info, src_idx, c);
4509 : 342853 : bool lderef = true;
4510 : 342853 : if (c != IPA_UNDESCRIBED_USE)
4511 : : {
4512 : 98832 : lderef = (ipa_get_param_load_dereferenced (new_root_info, src_idx)
4513 : 98832 : || ipa_get_param_load_dereferenced (old_root_info, i));
4514 : 98832 : ipa_set_param_load_dereferenced (new_root_info, src_idx, lderef);
4515 : : }
4516 : :
4517 : 342853 : if (c == 0 && !lderef && new_root_info->ipcp_orig_node)
4518 : : {
4519 : 240 : struct cgraph_node *n;
4520 : 240 : struct ipa_ref *ref;
4521 : 240 : tree t = new_root_info->known_csts[src_idx];
4522 : :
4523 : 215 : if (t && TREE_CODE (t) == ADDR_EXPR
4524 : 196 : && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
4525 : 9 : && (n = cgraph_node::get (TREE_OPERAND (t, 0)))
4526 : 249 : && (ref = new_root->find_reference (n, NULL, 0,
4527 : : IPA_REF_ADDR)))
4528 : : {
4529 : 9 : if (dump_file)
4530 : 1 : fprintf (dump_file, "ipa-prop: Removing cloning-created "
4531 : : "reference from %s to %s.\n",
4532 : : new_root->dump_name (),
4533 : : n->dump_name ());
4534 : 9 : ref->remove_reference ();
4535 : : }
4536 : : }
4537 : : }
4538 : 1525517 : else if (jf->type == IPA_JF_CONST
4539 : 1525517 : && (rdesc = jfunc_rdesc_usable (jf)))
4540 : : {
4541 : 57516 : int d = ipa_get_controlled_uses (old_root_info, i);
4542 : 57516 : int c = rdesc->refcount;
4543 : 57516 : tree cst = ipa_get_jf_constant (jf);
4544 : 57516 : rdesc->refcount = combine_controlled_uses_counters (c, d);
4545 : 57516 : if (rdesc->refcount != IPA_UNDESCRIBED_USE
4546 : 12631 : && ipa_get_param_load_dereferenced (old_root_info, i)
4547 : 2149 : && TREE_CODE (cst) == ADDR_EXPR
4548 : 59665 : && VAR_P (TREE_OPERAND (cst, 0)))
4549 : : {
4550 : 2148 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4551 : 2148 : new_root->create_reference (n, IPA_REF_LOAD, NULL);
4552 : 2148 : if (dump_file)
4553 : 3 : fprintf (dump_file, "ipa-prop: Address IPA constant will reach "
4554 : : "a load so adding LOAD reference from %s to %s.\n",
4555 : : new_root->dump_name (), n->dump_name ());
4556 : : }
4557 : 57516 : if (rdesc->refcount == 0)
4558 : : {
4559 : 8748 : gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
4560 : : && ((TREE_CODE (TREE_OPERAND (cst, 0))
4561 : : == FUNCTION_DECL)
4562 : : || VAR_P (TREE_OPERAND (cst, 0))));
4563 : :
4564 : 8748 : symtab_node *n = symtab_node::get (TREE_OPERAND (cst, 0));
4565 : 8748 : if (n)
4566 : : {
4567 : 8748 : remove_described_reference (n, rdesc);
4568 : 8748 : cgraph_node *clone = cs->caller;
4569 : 8748 : while (clone->inlined_to
4570 : 2106 : && clone->ipcp_clone
4571 : 9121 : && clone != rdesc->cs->caller)
4572 : : {
4573 : 156 : struct ipa_ref *ref;
4574 : 156 : ref = clone->find_reference (n, NULL, 0, IPA_REF_ADDR);
4575 : 156 : if (ref)
4576 : : {
4577 : 153 : if (dump_file)
4578 : 1 : fprintf (dump_file, "ipa-prop: Removing "
4579 : : "cloning-created reference "
4580 : : "from %s to %s.\n",
4581 : : clone->dump_name (),
4582 : : n->dump_name ());
4583 : 153 : ref->remove_reference ();
4584 : : }
4585 : 156 : clone = clone->callers->caller;
4586 : : }
4587 : : }
4588 : : }
4589 : : }
4590 : : }
4591 : :
4592 : 1811701 : for (i = ipa_get_param_count (old_root_info);
4593 : 1812253 : i < ipa_get_cs_argument_count (args);
4594 : : i++)
4595 : : {
4596 : 326 : struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
4597 : :
4598 : 326 : if (jf->type == IPA_JF_CONST)
4599 : : {
4600 : 294 : struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
4601 : 294 : if (rdesc)
4602 : 232 : rdesc->refcount = IPA_UNDESCRIBED_USE;
4603 : : }
4604 : 32 : else if (jf->type == IPA_JF_PASS_THROUGH)
4605 : 5 : ipa_set_controlled_uses (new_root_info,
4606 : : jf->value.pass_through.formal_id,
4607 : : IPA_UNDESCRIBED_USE);
4608 : : }
4609 : : }
4610 : :
4611 : : /* Update jump functions and call note functions on inlining the call site CS.
4612 : : CS is expected to lead to a node already cloned by
4613 : : cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
4614 : : *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
4615 : : created. */
4616 : :
4617 : : bool
4618 : 4033390 : ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
4619 : : vec<cgraph_edge *> *new_edges)
4620 : : {
4621 : 4033390 : bool changed;
4622 : : /* Do nothing if the preparation phase has not been carried out yet
4623 : : (i.e. during early inlining). */
4624 : 4033390 : if (!ipa_node_params_sum)
4625 : : return false;
4626 : 934835 : gcc_assert (ipa_edge_args_sum);
4627 : :
4628 : 934835 : propagate_controlled_uses (cs);
4629 : 934835 : changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
4630 : 934835 : ipa_node_params_sum->remove (cs->callee);
4631 : :
4632 : 934835 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4633 : 934835 : if (args)
4634 : : {
4635 : 933110 : bool ok = true;
4636 : 933110 : if (args->jump_functions)
4637 : : {
4638 : : struct ipa_jump_func *jf;
4639 : : int i;
4640 : 2543999 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
4641 : 1725752 : if (jf->type == IPA_JF_CONST
4642 : 1725752 : && ipa_get_jf_constant_rdesc (jf))
4643 : : {
4644 : : ok = false;
4645 : : break;
4646 : : }
4647 : : }
4648 : 878657 : if (ok)
4649 : 872700 : ipa_edge_args_sum->remove (cs);
4650 : : }
4651 : 934835 : if (ipcp_transformation_sum)
4652 : 659201 : ipcp_transformation_sum->remove (cs->callee);
4653 : :
4654 : : return changed;
4655 : : }
4656 : :
4657 : : /* Ensure that array of edge arguments infos is big enough to accommodate a
4658 : : structure for all edges and reallocates it if not. Also, allocate
4659 : : associated hash tables is they do not already exist. */
4660 : :
4661 : : void
4662 : 4817508 : ipa_check_create_edge_args (void)
4663 : : {
4664 : 4817508 : if (!ipa_edge_args_sum)
4665 : 244931 : ipa_edge_args_sum
4666 : 244931 : = (new (ggc_alloc_no_dtor<ipa_edge_args_sum_t> ())
4667 : 244931 : ipa_edge_args_sum_t (symtab, true));
4668 : 4817508 : if (!ipa_vr_hash_table)
4669 : 172932 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4670 : 4817508 : }
4671 : :
4672 : : /* Free all ipa_edge structures. */
4673 : :
4674 : : void
4675 : 462790 : ipa_free_all_edge_args (void)
4676 : : {
4677 : 462790 : if (!ipa_edge_args_sum)
4678 : : return;
4679 : :
4680 : 231204 : ggc_delete (ipa_edge_args_sum);
4681 : 231204 : ipa_edge_args_sum = NULL;
4682 : : }
4683 : :
4684 : : /* Free all ipa_node_params structures. */
4685 : :
4686 : : void
4687 : 5484430 : ipa_free_all_node_params (void)
4688 : : {
4689 : 5484430 : if (ipa_node_params_sum)
4690 : 5252844 : ggc_delete (ipa_node_params_sum);
4691 : 5484430 : ipa_node_params_sum = NULL;
4692 : 5484430 : }
4693 : :
4694 : : /* Initialize IPA CP transformation summary and also allocate any necessary hash
4695 : : tables if they do not already exist. */
4696 : :
4697 : : void
4698 : 90333 : ipcp_transformation_initialize (void)
4699 : : {
4700 : 90333 : if (!ipa_vr_hash_table)
4701 : 1839 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
4702 : 90333 : if (ipcp_transformation_sum == NULL)
4703 : : {
4704 : 19382 : ipcp_transformation_sum = ipcp_transformation_t::create_ggc (symtab);
4705 : 19382 : ipcp_transformation_sum->disable_insertion_hook ();
4706 : : }
4707 : 90333 : }
4708 : :
4709 : : /* Release the IPA CP transformation summary. */
4710 : :
4711 : : void
4712 : 260333 : ipcp_free_transformation_sum (void)
4713 : : {
4714 : 260333 : if (!ipcp_transformation_sum)
4715 : : return;
4716 : :
4717 : 19373 : ipcp_transformation_sum->~function_summary<ipcp_transformation *> ();
4718 : 19373 : ggc_free (ipcp_transformation_sum);
4719 : 19373 : ipcp_transformation_sum = NULL;
4720 : : }
4721 : :
4722 : : /* Set the aggregate replacements of NODE to be AGGVALS. */
4723 : :
4724 : : void
4725 : 20690 : ipa_set_node_agg_value_chain (struct cgraph_node *node,
4726 : : vec<ipa_argagg_value, va_gc> *aggs)
4727 : : {
4728 : 20690 : ipcp_transformation_initialize ();
4729 : 20690 : ipcp_transformation *s = ipcp_transformation_sum->get_create (node);
4730 : 20690 : s->m_agg_values = aggs;
4731 : 20690 : }
4732 : :
4733 : : /* Hook that is called by cgraph.cc when an edge is removed. Adjust reference
4734 : : count data structures accordingly. */
4735 : :
4736 : : void
4737 : 0 : ipa_edge_args_sum_t::remove (cgraph_edge *cs, ipa_edge_args *args)
4738 : : {
4739 : 0 : if (args->jump_functions)
4740 : : {
4741 : : struct ipa_jump_func *jf;
4742 : : int i;
4743 : 0 : FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
4744 : : {
4745 : 0 : struct ipa_cst_ref_desc *rdesc;
4746 : 0 : try_decrement_rdesc_refcount (jf);
4747 : 0 : if (jf->type == IPA_JF_CONST
4748 : 0 : && (rdesc = ipa_get_jf_constant_rdesc (jf))
4749 : 0 : && rdesc->cs == cs)
4750 : 0 : rdesc->cs = NULL;
4751 : : }
4752 : : }
4753 : 0 : }
4754 : :
4755 : : /* Copy information from SRC_JF to DST_JF which correstpond to call graph edges
4756 : : SRC and DST. */
4757 : :
4758 : : static void
4759 : 2590213 : ipa_duplicate_jump_function (cgraph_edge *src, cgraph_edge *dst,
4760 : : ipa_jump_func *src_jf, ipa_jump_func *dst_jf)
4761 : : {
4762 : 2590213 : dst_jf->agg.items = vec_safe_copy (src_jf->agg.items);
4763 : 2590213 : dst_jf->agg.by_ref = src_jf->agg.by_ref;
4764 : :
4765 : : /* We can avoid calling ipa_set_jfunc_vr since it would only look up the
4766 : : place in the hash_table where the source m_vr resides. */
4767 : 2590213 : dst_jf->m_vr = src_jf->m_vr;
4768 : :
4769 : 2590213 : if (src_jf->type == IPA_JF_CONST)
4770 : : {
4771 : 850383 : ipa_set_jf_cst_copy (dst_jf, src_jf);
4772 : 850383 : struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
4773 : :
4774 : 850383 : if (!src_rdesc)
4775 : 718386 : dst_jf->value.constant.rdesc = NULL;
4776 : 131997 : else if (src->caller == dst->caller)
4777 : : {
4778 : : /* Creation of a speculative edge. If the source edge is the one
4779 : : grabbing a reference, we must create a new (duplicate)
4780 : : reference description. Otherwise they refer to the same
4781 : : description corresponding to a reference taken in a function
4782 : : src->caller is inlined to. In that case we just must
4783 : : increment the refcount. */
4784 : 16 : if (src_rdesc->cs == src)
4785 : : {
4786 : 16 : symtab_node *n = symtab_node_for_jfunc (src_jf);
4787 : 16 : gcc_checking_assert (n);
4788 : 16 : ipa_ref *ref
4789 : 16 : = src->caller->find_reference (n, src->call_stmt,
4790 : : src->lto_stmt_uid,
4791 : : IPA_REF_ADDR);
4792 : 16 : gcc_checking_assert (ref);
4793 : 16 : dst->caller->clone_reference (ref, ref->stmt);
4794 : :
4795 : 16 : ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
4796 : 16 : dst_rdesc->cs = dst;
4797 : 16 : dst_rdesc->refcount = src_rdesc->refcount;
4798 : 16 : dst_rdesc->next_duplicate = NULL;
4799 : 16 : dst_jf->value.constant.rdesc = dst_rdesc;
4800 : : }
4801 : : else
4802 : : {
4803 : 0 : src_rdesc->refcount++;
4804 : 0 : dst_jf->value.constant.rdesc = src_rdesc;
4805 : : }
4806 : : }
4807 : 131981 : else if (src_rdesc->cs == src)
4808 : : {
4809 : 131676 : struct ipa_cst_ref_desc *dst_rdesc = ipa_refdesc_pool.allocate ();
4810 : 131676 : dst_rdesc->cs = dst;
4811 : 131676 : dst_rdesc->refcount = src_rdesc->refcount;
4812 : 131676 : dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
4813 : 131676 : src_rdesc->next_duplicate = dst_rdesc;
4814 : 131676 : dst_jf->value.constant.rdesc = dst_rdesc;
4815 : : }
4816 : : else
4817 : : {
4818 : 305 : struct ipa_cst_ref_desc *dst_rdesc;
4819 : : /* This can happen during inlining, when a JFUNC can refer to a
4820 : : reference taken in a function up in the tree of inline clones.
4821 : : We need to find the duplicate that refers to our tree of
4822 : : inline clones. */
4823 : :
4824 : 305 : gcc_assert (dst->caller->inlined_to);
4825 : 305 : for (dst_rdesc = src_rdesc->next_duplicate;
4826 : 305 : dst_rdesc;
4827 : 0 : dst_rdesc = dst_rdesc->next_duplicate)
4828 : : {
4829 : 305 : struct cgraph_node *top;
4830 : 0 : top = dst_rdesc->cs->caller->inlined_to
4831 : 305 : ? dst_rdesc->cs->caller->inlined_to
4832 : : : dst_rdesc->cs->caller;
4833 : 305 : if (dst->caller->inlined_to == top)
4834 : : break;
4835 : : }
4836 : 305 : gcc_assert (dst_rdesc);
4837 : 305 : dst_jf->value.constant.rdesc = dst_rdesc;
4838 : : }
4839 : : }
4840 : 1739830 : else if (src_jf->type == IPA_JF_PASS_THROUGH)
4841 : : {
4842 : 625473 : dst_jf->type = IPA_JF_PASS_THROUGH;
4843 : 625473 : dst_jf->value.pass_through = src_jf->value.pass_through;
4844 : 625473 : if (src->caller == dst->caller)
4845 : : {
4846 : 10963 : struct cgraph_node *inline_root = dst->caller->inlined_to
4847 : 10987 : ? dst->caller->inlined_to : dst->caller;
4848 : 10987 : ipa_node_params *root_info = ipa_node_params_sum->get (inline_root);
4849 : 10987 : int idx = ipa_get_jf_pass_through_formal_id (dst_jf);
4850 : :
4851 : 10987 : int c = ipa_get_controlled_uses (root_info, idx);
4852 : 10987 : if (c != IPA_UNDESCRIBED_USE)
4853 : : {
4854 : 2533 : c++;
4855 : 2533 : ipa_set_controlled_uses (root_info, idx, c);
4856 : : }
4857 : : }
4858 : : }
4859 : 1114357 : else if (src_jf->type == IPA_JF_ANCESTOR)
4860 : : {
4861 : 105521 : dst_jf->type = IPA_JF_ANCESTOR;
4862 : 105521 : dst_jf->value.ancestor = src_jf->value.ancestor;
4863 : : }
4864 : : else
4865 : 1008836 : gcc_assert (src_jf->type == IPA_JF_UNKNOWN);
4866 : 2590213 : }
4867 : :
4868 : : /* Method invoked when an edge is duplicated. Copy ipa_edge_args and adjust
4869 : : reference count data strucutres accordingly. */
4870 : :
4871 : : void
4872 : 1178196 : ipa_edge_args_sum_t::duplicate (cgraph_edge *src, cgraph_edge *dst,
4873 : : ipa_edge_args *old_args, ipa_edge_args *new_args)
4874 : : {
4875 : 1178196 : unsigned int i;
4876 : :
4877 : 1178196 : if (old_args->polymorphic_call_contexts)
4878 : 153023 : new_args->polymorphic_call_contexts
4879 : 153023 : = vec_safe_copy (old_args->polymorphic_call_contexts);
4880 : :
4881 : 1178196 : if (!vec_safe_length (old_args->jump_functions))
4882 : : {
4883 : 56333 : new_args->jump_functions = NULL;
4884 : 56333 : return;
4885 : : }
4886 : 1121863 : vec_safe_grow_cleared (new_args->jump_functions,
4887 : 1121863 : old_args->jump_functions->length (), true);
4888 : :
4889 : 3697022 : for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
4890 : : {
4891 : 2575159 : struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
4892 : 2575159 : struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
4893 : :
4894 : 2575159 : ipa_duplicate_jump_function (src, dst, src_jf, dst_jf);
4895 : : }
4896 : : }
4897 : :
4898 : : /* Analyze newly added function into callgraph. */
4899 : :
4900 : : static void
4901 : 40650 : ipa_add_new_function (cgraph_node *node, void *data ATTRIBUTE_UNUSED)
4902 : : {
4903 : 40650 : if (node->has_gimple_body_p ())
4904 : 40650 : ipa_analyze_node (node);
4905 : 40650 : }
4906 : :
4907 : : /* Hook that is called by summary when a node is duplicated. */
4908 : :
4909 : : void
4910 : 751754 : ipa_node_params_t::duplicate(cgraph_node *, cgraph_node *,
4911 : : ipa_node_params *old_info,
4912 : : ipa_node_params *new_info)
4913 : : {
4914 : 751754 : new_info->descriptors = vec_safe_copy (old_info->descriptors);
4915 : 751754 : gcc_assert (new_info->lattices.is_empty ());
4916 : 751754 : new_info->ipcp_orig_node = old_info->ipcp_orig_node;
4917 : 751754 : new_info->known_csts = old_info->known_csts.copy ();
4918 : 751754 : new_info->known_contexts = old_info->known_contexts.copy ();
4919 : :
4920 : 751754 : new_info->analysis_done = old_info->analysis_done;
4921 : 751754 : new_info->node_enqueued = old_info->node_enqueued;
4922 : 751754 : new_info->versionable = old_info->versionable;
4923 : 751754 : }
4924 : :
4925 : : /* Duplication of ipcp transformation summaries. */
4926 : :
4927 : : void
4928 : 60762 : ipcp_transformation_t::duplicate(cgraph_node *, cgraph_node *dst,
4929 : : ipcp_transformation *src_trans,
4930 : : ipcp_transformation *dst_trans)
4931 : : {
4932 : : /* Avoid redundant work of duplicating vectors we will never use. */
4933 : 60762 : if (dst->inlined_to)
4934 : : return;
4935 : 8630 : dst_trans->m_agg_values = vec_safe_copy (src_trans->m_agg_values);
4936 : 15538 : dst_trans->m_vr = vec_safe_copy (src_trans->m_vr);
4937 : : }
4938 : :
4939 : : /* Register our cgraph hooks if they are not already there. */
4940 : :
4941 : : void
4942 : 382330 : ipa_register_cgraph_hooks (void)
4943 : : {
4944 : 382330 : ipa_check_create_node_params ();
4945 : 382330 : ipa_check_create_edge_args ();
4946 : :
4947 : 764660 : function_insertion_hook_holder =
4948 : 382330 : symtab->add_cgraph_insertion_hook (&ipa_add_new_function, NULL);
4949 : 382330 : }
4950 : :
4951 : : /* Unregister our cgraph hooks if they are not already there. */
4952 : :
4953 : : static void
4954 : 462790 : ipa_unregister_cgraph_hooks (void)
4955 : : {
4956 : 462790 : if (function_insertion_hook_holder)
4957 : 231204 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
4958 : 462790 : function_insertion_hook_holder = NULL;
4959 : 462790 : }
4960 : :
4961 : : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
4962 : : longer needed after ipa-cp. */
4963 : :
4964 : : void
4965 : 128825 : ipa_free_all_structures_after_ipa_cp (void)
4966 : : {
4967 : 128825 : if (!optimize && !in_lto_p)
4968 : : {
4969 : 0 : ipa_free_all_edge_args ();
4970 : 0 : ipa_free_all_node_params ();
4971 : 0 : ipcp_sources_pool.release ();
4972 : 0 : ipcp_cst_values_pool.release ();
4973 : 0 : ipcp_poly_ctx_values_pool.release ();
4974 : 0 : ipcp_agg_lattice_pool.release ();
4975 : 0 : ipa_unregister_cgraph_hooks ();
4976 : 0 : ipa_refdesc_pool.release ();
4977 : : }
4978 : 128825 : }
4979 : :
4980 : : /* Free all ipa_node_params and all ipa_edge_args structures if they are no
4981 : : longer needed after indirect inlining. */
4982 : :
4983 : : void
4984 : 462790 : ipa_free_all_structures_after_iinln (void)
4985 : : {
4986 : 462790 : ipa_free_all_edge_args ();
4987 : 462790 : ipa_free_all_node_params ();
4988 : 462790 : ipa_unregister_cgraph_hooks ();
4989 : 462790 : ipcp_sources_pool.release ();
4990 : 462790 : ipcp_cst_values_pool.release ();
4991 : 462790 : ipcp_poly_ctx_values_pool.release ();
4992 : 462790 : ipcp_agg_lattice_pool.release ();
4993 : 462790 : ipa_refdesc_pool.release ();
4994 : 462790 : }
4995 : :
4996 : : /* Print ipa_tree_map data structures of all functions in the
4997 : : callgraph to F. */
4998 : :
4999 : : void
5000 : 234 : ipa_print_node_params (FILE *f, struct cgraph_node *node)
5001 : : {
5002 : 234 : int i, count;
5003 : 234 : class ipa_node_params *info;
5004 : :
5005 : 234 : if (!node->definition)
5006 : : return;
5007 : 177 : info = ipa_node_params_sum->get (node);
5008 : 177 : fprintf (f, " function %s parameter descriptors:\n", node->dump_name ());
5009 : 177 : if (!info)
5010 : : {
5011 : 0 : fprintf (f, " no params return\n");
5012 : 0 : return;
5013 : : }
5014 : 177 : count = ipa_get_param_count (info);
5015 : 352 : for (i = 0; i < count; i++)
5016 : : {
5017 : 175 : int c;
5018 : :
5019 : 175 : fprintf (f, " ");
5020 : 175 : ipa_dump_param (f, info, i);
5021 : 175 : if (ipa_is_param_used (info, i))
5022 : 168 : fprintf (f, " used");
5023 : 175 : if (ipa_is_param_used_by_ipa_predicates (info, i))
5024 : 103 : fprintf (f, " used_by_ipa_predicates");
5025 : 175 : if (ipa_is_param_used_by_indirect_call (info, i))
5026 : 10 : fprintf (f, " used_by_indirect_call");
5027 : 175 : if (ipa_is_param_used_by_polymorphic_call (info, i))
5028 : 0 : fprintf (f, " used_by_polymorphic_call");
5029 : 175 : c = ipa_get_controlled_uses (info, i);
5030 : 175 : if (c == IPA_UNDESCRIBED_USE)
5031 : 102 : fprintf (f, " undescribed_use");
5032 : : else
5033 : 125 : fprintf (f, " controlled_uses=%i %s", c,
5034 : 73 : ipa_get_param_load_dereferenced (info, i)
5035 : : ? "(load_dereferenced)" : "");
5036 : 175 : fprintf (f, "\n");
5037 : : }
5038 : : }
5039 : :
5040 : : /* Print ipa_tree_map data structures of all functions in the
5041 : : callgraph to F. */
5042 : :
5043 : : void
5044 : 46 : ipa_print_all_params (FILE * f)
5045 : : {
5046 : 46 : struct cgraph_node *node;
5047 : :
5048 : 46 : fprintf (f, "\nFunction parameters:\n");
5049 : 532 : FOR_EACH_FUNCTION (node)
5050 : 220 : ipa_print_node_params (f, node);
5051 : 46 : }
5052 : :
5053 : : /* Stream out jump function JUMP_FUNC to OB. */
5054 : :
5055 : : static void
5056 : 605146 : ipa_write_jump_function (struct output_block *ob,
5057 : : struct ipa_jump_func *jump_func)
5058 : : {
5059 : 605146 : struct ipa_agg_jf_item *item;
5060 : 605146 : struct bitpack_d bp;
5061 : 605146 : int i, count;
5062 : 605146 : int flag = 0;
5063 : :
5064 : : /* ADDR_EXPRs are very comon IP invariants; save some streamer data
5065 : : as well as WPA memory by handling them specially. */
5066 : 605146 : if (jump_func->type == IPA_JF_CONST
5067 : 463321 : && TREE_CODE (jump_func->value.constant.value) == ADDR_EXPR)
5068 : 605146 : flag = 1;
5069 : :
5070 : 605146 : streamer_write_uhwi (ob, jump_func->type * 2 + flag);
5071 : 605146 : switch (jump_func->type)
5072 : : {
5073 : : case IPA_JF_UNKNOWN:
5074 : : break;
5075 : 463321 : case IPA_JF_CONST:
5076 : 463321 : gcc_assert (
5077 : : EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
5078 : 463321 : stream_write_tree (ob,
5079 : : flag
5080 : : ? TREE_OPERAND (jump_func->value.constant.value, 0)
5081 : : : jump_func->value.constant.value, true);
5082 : 463321 : break;
5083 : 76067 : case IPA_JF_PASS_THROUGH:
5084 : 76067 : streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
5085 : 76067 : if (jump_func->value.pass_through.operation == NOP_EXPR)
5086 : : {
5087 : 75260 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5088 : 75260 : bp = bitpack_create (ob->main_stream);
5089 : 75260 : bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
5090 : 75260 : gcc_assert (!jump_func->value.pass_through.refdesc_decremented);
5091 : 75260 : streamer_write_bitpack (&bp);
5092 : : }
5093 : 807 : else if (TREE_CODE_CLASS (jump_func->value.pass_through.operation)
5094 : : == tcc_unary)
5095 : : {
5096 : 39 : stream_write_tree (ob, jump_func->value.pass_through.op_type, true);
5097 : 39 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5098 : : }
5099 : : else
5100 : : {
5101 : 768 : stream_write_tree (ob, jump_func->value.pass_through.op_type, true);
5102 : 768 : stream_write_tree (ob, jump_func->value.pass_through.operand, true);
5103 : 768 : streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
5104 : : }
5105 : : break;
5106 : 1269 : case IPA_JF_ANCESTOR:
5107 : 1269 : streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
5108 : 1269 : streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
5109 : 1269 : bp = bitpack_create (ob->main_stream);
5110 : 1269 : bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
5111 : 1269 : bp_pack_value (&bp, jump_func->value.ancestor.keep_null, 1);
5112 : 1269 : streamer_write_bitpack (&bp);
5113 : 1269 : break;
5114 : 0 : default:
5115 : 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
5116 : : }
5117 : :
5118 : 605146 : count = vec_safe_length (jump_func->agg.items);
5119 : 605146 : streamer_write_uhwi (ob, count);
5120 : 605146 : if (count)
5121 : : {
5122 : 3246 : bp = bitpack_create (ob->main_stream);
5123 : 3246 : bp_pack_value (&bp, jump_func->agg.by_ref, 1);
5124 : 3246 : streamer_write_bitpack (&bp);
5125 : : }
5126 : :
5127 : 611590 : FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
5128 : : {
5129 : 6444 : stream_write_tree (ob, item->type, true);
5130 : 6444 : streamer_write_uhwi (ob, item->offset);
5131 : 6444 : streamer_write_uhwi (ob, item->jftype);
5132 : 6444 : switch (item->jftype)
5133 : : {
5134 : : case IPA_JF_UNKNOWN:
5135 : : break;
5136 : 5931 : case IPA_JF_CONST:
5137 : 5931 : stream_write_tree (ob, item->value.constant, true);
5138 : 5931 : break;
5139 : 513 : case IPA_JF_PASS_THROUGH:
5140 : 513 : case IPA_JF_LOAD_AGG:
5141 : 513 : streamer_write_uhwi (ob, item->value.pass_through.operation);
5142 : 513 : streamer_write_uhwi (ob, item->value.pass_through.formal_id);
5143 : 513 : if (item->value.pass_through.operation != NOP_EXPR)
5144 : 4 : stream_write_tree (ob, item->value.pass_through.op_type, true);
5145 : 513 : if (TREE_CODE_CLASS (item->value.pass_through.operation)
5146 : : != tcc_unary)
5147 : 4 : stream_write_tree (ob, item->value.pass_through.operand, true);
5148 : 513 : if (item->jftype == IPA_JF_LOAD_AGG)
5149 : : {
5150 : 73 : stream_write_tree (ob, item->value.load_agg.type, true);
5151 : 73 : streamer_write_uhwi (ob, item->value.load_agg.offset);
5152 : 73 : bp = bitpack_create (ob->main_stream);
5153 : 73 : bp_pack_value (&bp, item->value.load_agg.by_ref, 1);
5154 : 73 : streamer_write_bitpack (&bp);
5155 : : }
5156 : : break;
5157 : 0 : default:
5158 : 0 : fatal_error (UNKNOWN_LOCATION,
5159 : : "invalid jump function in LTO stream");
5160 : : }
5161 : : }
5162 : :
5163 : 605146 : bp = bitpack_create (ob->main_stream);
5164 : 605146 : if (jump_func->m_vr)
5165 : 420643 : jump_func->m_vr->streamer_write (ob);
5166 : : else
5167 : : {
5168 : 184503 : bp_pack_value (&bp, false, 1);
5169 : 184503 : streamer_write_bitpack (&bp);
5170 : : }
5171 : 605146 : }
5172 : :
5173 : : /* Read in jump function JUMP_FUNC from IB. */
5174 : :
5175 : : static void
5176 : 567679 : ipa_read_jump_function (class lto_input_block *ib,
5177 : : struct ipa_jump_func *jump_func,
5178 : : struct cgraph_edge *cs,
5179 : : class data_in *data_in,
5180 : : bool prevails)
5181 : : {
5182 : 567679 : enum jump_func_type jftype;
5183 : 567679 : enum tree_code operation;
5184 : 567679 : int i, count;
5185 : 567679 : int val = streamer_read_uhwi (ib);
5186 : 567679 : bool flag = val & 1;
5187 : :
5188 : 567679 : jftype = (enum jump_func_type) (val / 2);
5189 : 567679 : switch (jftype)
5190 : : {
5191 : 50019 : case IPA_JF_UNKNOWN:
5192 : 50019 : ipa_set_jf_unknown (jump_func);
5193 : 50019 : break;
5194 : 447354 : case IPA_JF_CONST:
5195 : 447354 : {
5196 : 447354 : tree t = stream_read_tree (ib, data_in);
5197 : 447354 : if (flag && prevails)
5198 : 161500 : t = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (t)), t);
5199 : 447354 : ipa_set_jf_constant (jump_func, t, cs);
5200 : : }
5201 : 447354 : break;
5202 : 69688 : case IPA_JF_PASS_THROUGH:
5203 : 69688 : operation = (enum tree_code) streamer_read_uhwi (ib);
5204 : 69688 : if (operation == NOP_EXPR)
5205 : : {
5206 : 69150 : int formal_id = streamer_read_uhwi (ib);
5207 : 69150 : struct bitpack_d bp = streamer_read_bitpack (ib);
5208 : 69150 : bool agg_preserved = bp_unpack_value (&bp, 1);
5209 : 69150 : ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved);
5210 : : }
5211 : 538 : else if (TREE_CODE_CLASS (operation) == tcc_unary)
5212 : : {
5213 : 19 : tree op_type = stream_read_tree (ib, data_in);
5214 : 19 : int formal_id = streamer_read_uhwi (ib);
5215 : 19 : ipa_set_jf_unary_pass_through (jump_func, formal_id, operation,
5216 : : op_type);
5217 : : }
5218 : : else
5219 : : {
5220 : 519 : tree op_type = stream_read_tree (ib, data_in);
5221 : 519 : tree operand = stream_read_tree (ib, data_in);
5222 : 519 : int formal_id = streamer_read_uhwi (ib);
5223 : 519 : ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
5224 : : operation, op_type);
5225 : : }
5226 : : break;
5227 : 618 : case IPA_JF_ANCESTOR:
5228 : 618 : {
5229 : 618 : HOST_WIDE_INT offset = streamer_read_uhwi (ib);
5230 : 618 : int formal_id = streamer_read_uhwi (ib);
5231 : 618 : struct bitpack_d bp = streamer_read_bitpack (ib);
5232 : 618 : bool agg_preserved = bp_unpack_value (&bp, 1);
5233 : 618 : bool keep_null = bp_unpack_value (&bp, 1);
5234 : 618 : ipa_set_ancestor_jf (jump_func, offset, formal_id, agg_preserved,
5235 : : keep_null);
5236 : 618 : break;
5237 : : }
5238 : 0 : default:
5239 : 0 : fatal_error (UNKNOWN_LOCATION, "invalid jump function in LTO stream");
5240 : : }
5241 : :
5242 : 567679 : count = streamer_read_uhwi (ib);
5243 : 567679 : if (prevails)
5244 : : {
5245 : 567673 : jump_func->agg.items = NULL;
5246 : 567673 : vec_safe_reserve (jump_func->agg.items, count, true);
5247 : : }
5248 : 567679 : if (count)
5249 : : {
5250 : 2895 : struct bitpack_d bp = streamer_read_bitpack (ib);
5251 : 2895 : jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
5252 : : }
5253 : 573540 : for (i = 0; i < count; i++)
5254 : : {
5255 : 5861 : struct ipa_agg_jf_item item;
5256 : 5861 : item.type = stream_read_tree (ib, data_in);
5257 : 5861 : item.offset = streamer_read_uhwi (ib);
5258 : 5861 : item.jftype = (enum jump_func_type) streamer_read_uhwi (ib);
5259 : :
5260 : 5861 : switch (item.jftype)
5261 : : {
5262 : : case IPA_JF_UNKNOWN:
5263 : : break;
5264 : 5434 : case IPA_JF_CONST:
5265 : 5434 : item.value.constant = stream_read_tree (ib, data_in);
5266 : 5434 : break;
5267 : 427 : case IPA_JF_PASS_THROUGH:
5268 : 427 : case IPA_JF_LOAD_AGG:
5269 : 427 : operation = (enum tree_code) streamer_read_uhwi (ib);
5270 : 427 : item.value.pass_through.operation = operation;
5271 : 427 : item.value.pass_through.formal_id = streamer_read_uhwi (ib);
5272 : 427 : if (operation != NOP_EXPR)
5273 : 0 : item.value.pass_through.op_type = stream_read_tree (ib, data_in);
5274 : : else
5275 : 427 : item.value.pass_through.op_type = NULL_TREE;
5276 : 427 : if (TREE_CODE_CLASS (operation) == tcc_unary)
5277 : 427 : item.value.pass_through.operand = NULL_TREE;
5278 : : else
5279 : 0 : item.value.pass_through.operand = stream_read_tree (ib, data_in);
5280 : 427 : if (item.jftype == IPA_JF_LOAD_AGG)
5281 : : {
5282 : 37 : struct bitpack_d bp;
5283 : 37 : item.value.load_agg.type = stream_read_tree (ib, data_in);
5284 : 37 : item.value.load_agg.offset = streamer_read_uhwi (ib);
5285 : 37 : bp = streamer_read_bitpack (ib);
5286 : 37 : item.value.load_agg.by_ref = bp_unpack_value (&bp, 1);
5287 : : }
5288 : : break;
5289 : 0 : default:
5290 : 0 : fatal_error (UNKNOWN_LOCATION,
5291 : : "invalid jump function in LTO stream");
5292 : : }
5293 : 5861 : if (prevails)
5294 : 5861 : jump_func->agg.items->quick_push (item);
5295 : : }
5296 : :
5297 : 567679 : ipa_vr vr;
5298 : 567679 : vr.streamer_read (ib, data_in);
5299 : 567679 : if (vr.known_p ())
5300 : : {
5301 : 396722 : if (prevails)
5302 : 396716 : ipa_set_jfunc_vr (jump_func, vr);
5303 : : }
5304 : : else
5305 : 170957 : jump_func->m_vr = NULL;
5306 : 567679 : }
5307 : :
5308 : : /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
5309 : : relevant to indirect inlining to OB. */
5310 : :
5311 : : static void
5312 : 1818 : ipa_write_indirect_edge_info (struct output_block *ob,
5313 : : struct cgraph_edge *cs)
5314 : : {
5315 : 1818 : class cgraph_indirect_call_info *ii = cs->indirect_info;
5316 : 1818 : struct bitpack_d bp;
5317 : :
5318 : 1818 : streamer_write_hwi (ob, ii->param_index);
5319 : 1818 : bp = bitpack_create (ob->main_stream);
5320 : 1818 : bp_pack_value (&bp, ii->polymorphic, 1);
5321 : 1818 : bp_pack_value (&bp, ii->agg_contents, 1);
5322 : 1818 : bp_pack_value (&bp, ii->member_ptr, 1);
5323 : 1818 : bp_pack_value (&bp, ii->by_ref, 1);
5324 : 1818 : bp_pack_value (&bp, ii->guaranteed_unmodified, 1);
5325 : 1818 : bp_pack_value (&bp, ii->vptr_changed, 1);
5326 : 1818 : streamer_write_bitpack (&bp);
5327 : 1818 : if (ii->agg_contents || ii->polymorphic)
5328 : 302 : streamer_write_hwi (ob, ii->offset);
5329 : : else
5330 : 1516 : gcc_assert (ii->offset == 0);
5331 : :
5332 : 1818 : if (ii->polymorphic)
5333 : : {
5334 : 247 : streamer_write_hwi (ob, ii->otr_token);
5335 : 247 : stream_write_tree (ob, ii->otr_type, true);
5336 : 247 : ii->context.stream_out (ob);
5337 : : }
5338 : 1818 : }
5339 : :
5340 : : /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
5341 : : relevant to indirect inlining from IB. */
5342 : :
5343 : : static void
5344 : 1401 : ipa_read_indirect_edge_info (class lto_input_block *ib,
5345 : : class data_in *data_in,
5346 : : struct cgraph_edge *cs,
5347 : : class ipa_node_params *info)
5348 : : {
5349 : 1401 : class cgraph_indirect_call_info *ii = cs->indirect_info;
5350 : 1401 : struct bitpack_d bp;
5351 : :
5352 : 1401 : ii->param_index = (int) streamer_read_hwi (ib);
5353 : 1401 : bp = streamer_read_bitpack (ib);
5354 : 1401 : ii->polymorphic = bp_unpack_value (&bp, 1);
5355 : 1401 : ii->agg_contents = bp_unpack_value (&bp, 1);
5356 : 1401 : ii->member_ptr = bp_unpack_value (&bp, 1);
5357 : 1401 : ii->by_ref = bp_unpack_value (&bp, 1);
5358 : 1401 : ii->guaranteed_unmodified = bp_unpack_value (&bp, 1);
5359 : 1401 : ii->vptr_changed = bp_unpack_value (&bp, 1);
5360 : 1401 : if (ii->agg_contents || ii->polymorphic)
5361 : 114 : ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
5362 : : else
5363 : 1287 : ii->offset = 0;
5364 : 1401 : if (ii->polymorphic)
5365 : : {
5366 : 83 : ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
5367 : 83 : ii->otr_type = stream_read_tree (ib, data_in);
5368 : 83 : ii->context.stream_in (ib, data_in);
5369 : : }
5370 : 1401 : if (info && ii->param_index >= 0)
5371 : : {
5372 : 322 : if (ii->polymorphic)
5373 : 60 : ipa_set_param_used_by_polymorphic_call (info,
5374 : : ii->param_index , true);
5375 : 322 : ipa_set_param_used_by_indirect_call (info,
5376 : : ii->param_index, true);
5377 : : }
5378 : 1401 : }
5379 : :
5380 : : /* Stream out NODE info to OB. */
5381 : :
5382 : : static void
5383 : 91688 : ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
5384 : : {
5385 : 91688 : int node_ref;
5386 : 91688 : lto_symtab_encoder_t encoder;
5387 : 91688 : ipa_node_params *info = ipa_node_params_sum->get (node);
5388 : 91688 : int j;
5389 : 91688 : struct cgraph_edge *e;
5390 : 91688 : struct bitpack_d bp;
5391 : :
5392 : 91688 : encoder = ob->decl_state->symtab_node_encoder;
5393 : 91688 : node_ref = lto_symtab_encoder_encode (encoder, node);
5394 : 91688 : streamer_write_uhwi (ob, node_ref);
5395 : :
5396 : 91688 : streamer_write_uhwi (ob, ipa_get_param_count (info));
5397 : 444275 : for (j = 0; j < ipa_get_param_count (info); j++)
5398 : 99480 : streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
5399 : 91688 : bp = bitpack_create (ob->main_stream);
5400 : 91688 : gcc_assert (info->analysis_done
5401 : : || ipa_get_param_count (info) == 0);
5402 : 91688 : gcc_assert (!info->node_enqueued);
5403 : 91688 : gcc_assert (!info->ipcp_orig_node);
5404 : 352587 : for (j = 0; j < ipa_get_param_count (info); j++)
5405 : : {
5406 : : /* TODO: We could just not stream the bit in the undescribed case. */
5407 : 99480 : bool d = (ipa_get_controlled_uses (info, j) != IPA_UNDESCRIBED_USE)
5408 : 99480 : ? ipa_get_param_load_dereferenced (info, j) : true;
5409 : 99480 : bp_pack_value (&bp, d, 1);
5410 : 99480 : bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
5411 : : }
5412 : 91688 : streamer_write_bitpack (&bp);
5413 : 444275 : for (j = 0; j < ipa_get_param_count (info); j++)
5414 : : {
5415 : 99480 : streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
5416 : 99480 : stream_write_tree (ob, ipa_get_type (info, j), true);
5417 : : }
5418 : 452603 : for (e = node->callees; e; e = e->next_callee)
5419 : : {
5420 : 360915 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5421 : :
5422 : 360915 : if (!args)
5423 : : {
5424 : 742 : streamer_write_uhwi (ob, 0);
5425 : 742 : continue;
5426 : : }
5427 : :
5428 : 360173 : streamer_write_uhwi (ob,
5429 : 360173 : ipa_get_cs_argument_count (args) * 2
5430 : 360173 : + (args->polymorphic_call_contexts != NULL));
5431 : 2177964 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5432 : : {
5433 : 602974 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5434 : 602974 : if (args->polymorphic_call_contexts != NULL)
5435 : 2247 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5436 : : }
5437 : : }
5438 : 93506 : for (e = node->indirect_calls; e; e = e->next_callee)
5439 : : {
5440 : 1818 : ipa_edge_args *args = ipa_edge_args_sum->get (e);
5441 : 1818 : if (!args)
5442 : 6 : streamer_write_uhwi (ob, 0);
5443 : : else
5444 : : {
5445 : 1812 : streamer_write_uhwi (ob,
5446 : 1812 : ipa_get_cs_argument_count (args) * 2
5447 : 1812 : + (args->polymorphic_call_contexts != NULL));
5448 : 9639 : for (j = 0; j < ipa_get_cs_argument_count (args); j++)
5449 : : {
5450 : 2172 : ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
5451 : 2172 : if (args->polymorphic_call_contexts != NULL)
5452 : 406 : ipa_get_ith_polymorhic_call_context (args, j)->stream_out (ob);
5453 : : }
5454 : : }
5455 : 1818 : ipa_write_indirect_edge_info (ob, e);
5456 : : }
5457 : 91688 : }
5458 : :
5459 : : /* Stream in edge E from IB. */
5460 : :
5461 : : static void
5462 : 333281 : ipa_read_edge_info (class lto_input_block *ib,
5463 : : class data_in *data_in,
5464 : : struct cgraph_edge *e, bool prevails)
5465 : : {
5466 : 333281 : int count = streamer_read_uhwi (ib);
5467 : 333281 : bool contexts_computed = count & 1;
5468 : :
5469 : 333281 : count /= 2;
5470 : 333281 : if (!count)
5471 : : return;
5472 : 233230 : if (prevails
5473 : 233230 : && (e->possibly_call_in_translation_unit_p ()
5474 : : /* Also stream in jump functions to builtins in hope that they
5475 : : will get fnspecs. */
5476 : 115158 : || fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL)))
5477 : : {
5478 : 222264 : ipa_edge_args *args = ipa_edge_args_sum->get_create (e);
5479 : 222264 : vec_safe_grow_cleared (args->jump_functions, count, true);
5480 : 222264 : if (contexts_computed)
5481 : 564 : vec_safe_grow_cleared (args->polymorphic_call_contexts, count, true);
5482 : 772426 : for (int k = 0; k < count; k++)
5483 : : {
5484 : 550162 : ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
5485 : : data_in, prevails);
5486 : 550162 : if (contexts_computed)
5487 : 874 : ipa_get_ith_polymorhic_call_context (args, k)->stream_in
5488 : 874 : (ib, data_in);
5489 : : }
5490 : : }
5491 : : else
5492 : : {
5493 : 28483 : for (int k = 0; k < count; k++)
5494 : : {
5495 : 17517 : struct ipa_jump_func dummy;
5496 : 17517 : ipa_read_jump_function (ib, &dummy, e,
5497 : : data_in, prevails);
5498 : 17517 : if (contexts_computed)
5499 : : {
5500 : 423 : class ipa_polymorphic_call_context ctx;
5501 : 423 : ctx.stream_in (ib, data_in);
5502 : : }
5503 : : }
5504 : : }
5505 : : }
5506 : :
5507 : : /* Stream in NODE info from IB. */
5508 : :
5509 : : static void
5510 : 76757 : ipa_read_node_info (class lto_input_block *ib, struct cgraph_node *node,
5511 : : class data_in *data_in)
5512 : : {
5513 : 76757 : int k;
5514 : 76757 : struct cgraph_edge *e;
5515 : 76757 : struct bitpack_d bp;
5516 : 76757 : bool prevails = node->prevailing_p ();
5517 : 76757 : ipa_node_params *info
5518 : 76757 : = prevails ? ipa_node_params_sum->get_create (node) : NULL;
5519 : :
5520 : 76757 : int param_count = streamer_read_uhwi (ib);
5521 : 76757 : if (prevails)
5522 : : {
5523 : 76739 : ipa_alloc_node_params (node, param_count);
5524 : 230755 : for (k = 0; k < param_count; k++)
5525 : 77277 : (*info->descriptors)[k].move_cost = streamer_read_uhwi (ib);
5526 : 76739 : if (ipa_get_param_count (info) != 0)
5527 : 52740 : info->analysis_done = true;
5528 : 76739 : info->node_enqueued = false;
5529 : : }
5530 : : else
5531 : 27 : for (k = 0; k < param_count; k++)
5532 : 9 : streamer_read_uhwi (ib);
5533 : :
5534 : 76757 : bp = streamer_read_bitpack (ib);
5535 : 154043 : for (k = 0; k < param_count; k++)
5536 : : {
5537 : 77286 : bool load_dereferenced = bp_unpack_value (&bp, 1);
5538 : 77286 : bool used = bp_unpack_value (&bp, 1);
5539 : :
5540 : 77286 : if (prevails)
5541 : : {
5542 : 77277 : ipa_set_param_load_dereferenced (info, k, load_dereferenced);
5543 : 77277 : ipa_set_param_used (info, k, used);
5544 : : }
5545 : : }
5546 : 154043 : for (k = 0; k < param_count; k++)
5547 : : {
5548 : 77286 : int nuses = streamer_read_hwi (ib);
5549 : 77286 : tree type = stream_read_tree (ib, data_in);
5550 : :
5551 : 77286 : if (prevails)
5552 : : {
5553 : 77277 : ipa_set_controlled_uses (info, k, nuses);
5554 : 77277 : (*info->descriptors)[k].decl_or_type = type;
5555 : : }
5556 : : }
5557 : 408637 : for (e = node->callees; e; e = e->next_callee)
5558 : 331880 : ipa_read_edge_info (ib, data_in, e, prevails);
5559 : 78158 : for (e = node->indirect_calls; e; e = e->next_callee)
5560 : : {
5561 : 1401 : ipa_read_edge_info (ib, data_in, e, prevails);
5562 : 1401 : ipa_read_indirect_edge_info (ib, data_in, e, info);
5563 : : }
5564 : 76757 : }
5565 : :
5566 : : /* Stream out ipa_return_summary. */
5567 : : static void
5568 : 33469 : ipa_write_return_summaries (output_block *ob)
5569 : : {
5570 : 33469 : if (!ipa_return_value_sum)
5571 : : {
5572 : 17432 : streamer_write_uhwi (ob, 0);
5573 : 17432 : return;
5574 : : }
5575 : :
5576 : 16037 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5577 : 16037 : unsigned int count = 0;
5578 : 441982 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5579 : : {
5580 : 204954 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5581 : 409908 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5582 : 170115 : ipa_return_value_summary *v;
5583 : :
5584 : 170115 : if (cnode && cnode->definition && !cnode->alias
5585 : 127663 : && (v = ipa_return_value_sum->get (cnode))
5586 : 24595 : && v->vr)
5587 : 24595 : count++;
5588 : : }
5589 : 16037 : streamer_write_uhwi (ob, count);
5590 : :
5591 : 441982 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5592 : : {
5593 : 204954 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5594 : 409908 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5595 : 170115 : ipa_return_value_summary *v;
5596 : :
5597 : 170115 : if (cnode && cnode->definition && !cnode->alias
5598 : 127663 : && (v = ipa_return_value_sum->get (cnode))
5599 : 24595 : && v->vr)
5600 : : {
5601 : 24595 : streamer_write_uhwi
5602 : 24595 : (ob,
5603 : 24595 : lto_symtab_encoder_encode (encoder, cnode));
5604 : 24595 : v->vr->streamer_write (ob);
5605 : : }
5606 : : }
5607 : : }
5608 : :
5609 : : /* Write jump functions for nodes in SET. */
5610 : :
5611 : : void
5612 : 24263 : ipa_prop_write_jump_functions (void)
5613 : : {
5614 : 24263 : struct output_block *ob;
5615 : 24263 : unsigned int count = 0;
5616 : 24263 : lto_symtab_encoder_iterator lsei;
5617 : 24263 : lto_symtab_encoder_t encoder;
5618 : :
5619 : 24263 : if (!ipa_node_params_sum || !ipa_edge_args_sum)
5620 : 0 : return;
5621 : :
5622 : 24263 : ob = create_output_block (LTO_section_jump_functions);
5623 : 24263 : encoder = ob->decl_state->symtab_node_encoder;
5624 : 24263 : ob->symbol = NULL;
5625 : 129612 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5626 : 105349 : lsei_next_function_in_partition (&lsei))
5627 : : {
5628 : 105349 : cgraph_node *node = lsei_cgraph_node (lsei);
5629 : 105349 : if (node->has_gimple_body_p ()
5630 : 105349 : && ipa_node_params_sum->get (node) != NULL)
5631 : 91688 : count++;
5632 : : }
5633 : :
5634 : 24263 : streamer_write_uhwi (ob, count);
5635 : :
5636 : : /* Process all of the functions. */
5637 : 129612 : for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
5638 : 105349 : lsei_next_function_in_partition (&lsei))
5639 : : {
5640 : 105349 : cgraph_node *node = lsei_cgraph_node (lsei);
5641 : 105349 : if (node->has_gimple_body_p ()
5642 : 105349 : && ipa_node_params_sum->get (node) != NULL)
5643 : 91688 : ipa_write_node_info (ob, node);
5644 : : }
5645 : 24263 : ipa_write_return_summaries (ob);
5646 : 24263 : produce_asm (ob);
5647 : 24263 : destroy_output_block (ob);
5648 : : }
5649 : :
5650 : : /* Record that return value range of N is VAL. */
5651 : :
5652 : : static void
5653 : 747279 : ipa_record_return_value_range_1 (cgraph_node *n, value_range val)
5654 : : {
5655 : 747279 : if (!ipa_return_value_sum)
5656 : : {
5657 : 85288 : if (!ipa_vr_hash_table)
5658 : 74818 : ipa_vr_hash_table = hash_table<ipa_vr_ggc_hash_traits>::create_ggc (37);
5659 : 85288 : ipa_return_value_sum = new (ggc_alloc_no_dtor <ipa_return_value_sum_t> ())
5660 : 85288 : ipa_return_value_sum_t (symtab, true);
5661 : 85288 : ipa_return_value_sum->disable_insertion_hook ();
5662 : : }
5663 : 747279 : ipa_return_value_sum->get_create (n)->vr = ipa_get_value_range (val);
5664 : 747279 : if (dump_file && (dump_flags & TDF_DETAILS))
5665 : : {
5666 : 21 : fprintf (dump_file, "Recording return range of %s:", n->dump_name ());
5667 : 21 : val.dump (dump_file);
5668 : 21 : fprintf (dump_file, "\n");
5669 : : }
5670 : 747279 : }
5671 : :
5672 : : /* Stream out ipa_return_summary. */
5673 : : static void
5674 : 23413 : ipa_read_return_summaries (lto_input_block *ib,
5675 : : struct lto_file_decl_data *file_data,
5676 : : class data_in *data_in)
5677 : : {
5678 : 23413 : unsigned int f_count = streamer_read_uhwi (ib);
5679 : 44585 : for (unsigned int i = 0; i < f_count; i++)
5680 : : {
5681 : 21172 : unsigned int index = streamer_read_uhwi (ib);
5682 : 21172 : lto_symtab_encoder_t encoder = file_data->symtab_node_encoder;
5683 : 21172 : struct cgraph_node *node
5684 : : = dyn_cast <cgraph_node *>
5685 : 21172 : (lto_symtab_encoder_deref (encoder, index));
5686 : 21172 : ipa_vr rvr;
5687 : 21172 : rvr.streamer_read (ib, data_in);
5688 : 21172 : if (node->prevailing_p ())
5689 : : {
5690 : 21170 : value_range tmp;
5691 : 21170 : rvr.get_vrange (tmp);
5692 : 21170 : ipa_record_return_value_range_1 (node, tmp);
5693 : 21170 : }
5694 : : }
5695 : 23413 : }
5696 : :
5697 : : /* Read section in file FILE_DATA of length LEN with data DATA. */
5698 : :
5699 : : static void
5700 : 14207 : ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
5701 : : size_t len)
5702 : : {
5703 : 14207 : const struct lto_function_header *header =
5704 : : (const struct lto_function_header *) data;
5705 : 14207 : const int cfg_offset = sizeof (struct lto_function_header);
5706 : 14207 : const int main_offset = cfg_offset + header->cfg_size;
5707 : 14207 : const int string_offset = main_offset + header->main_size;
5708 : 14207 : class data_in *data_in;
5709 : 14207 : unsigned int i;
5710 : 14207 : unsigned int count;
5711 : :
5712 : 14207 : lto_input_block ib_main ((const char *) data + main_offset,
5713 : 14207 : header->main_size, file_data);
5714 : :
5715 : 14207 : data_in =
5716 : 28414 : lto_data_in_create (file_data, (const char *) data + string_offset,
5717 : 14207 : header->string_size, vNULL);
5718 : 14207 : count = streamer_read_uhwi (&ib_main);
5719 : :
5720 : 90964 : for (i = 0; i < count; i++)
5721 : : {
5722 : 76757 : unsigned int index;
5723 : 76757 : struct cgraph_node *node;
5724 : 76757 : lto_symtab_encoder_t encoder;
5725 : :
5726 : 76757 : index = streamer_read_uhwi (&ib_main);
5727 : 76757 : encoder = file_data->symtab_node_encoder;
5728 : 76757 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
5729 : : index));
5730 : 76757 : gcc_assert (node->definition);
5731 : 76757 : ipa_read_node_info (&ib_main, node, data_in);
5732 : : }
5733 : 14207 : ipa_read_return_summaries (&ib_main, file_data, data_in);
5734 : 14207 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
5735 : : len);
5736 : 14207 : lto_data_in_delete (data_in);
5737 : 14207 : }
5738 : :
5739 : : /* Read ipcp jump functions. */
5740 : :
5741 : : void
5742 : 13167 : ipa_prop_read_jump_functions (void)
5743 : : {
5744 : 13167 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
5745 : 13167 : struct lto_file_decl_data *file_data;
5746 : 13167 : unsigned int j = 0;
5747 : :
5748 : 13167 : ipa_check_create_node_params ();
5749 : 13167 : ipa_check_create_edge_args ();
5750 : 13167 : ipa_register_cgraph_hooks ();
5751 : :
5752 : 40541 : while ((file_data = file_data_vec[j++]))
5753 : : {
5754 : 14207 : size_t len;
5755 : 14207 : const char *data
5756 : 14207 : = lto_get_summary_section_data (file_data, LTO_section_jump_functions,
5757 : : &len);
5758 : 14207 : if (data)
5759 : 14207 : ipa_prop_read_section (file_data, data, len);
5760 : : }
5761 : 13167 : }
5762 : :
5763 : : /* Return true if the IPA-CP transformation summary TS is non-NULL and contains
5764 : : useful info. */
5765 : : static bool
5766 : 178090 : useful_ipcp_transformation_info_p (ipcp_transformation *ts)
5767 : : {
5768 : 178090 : if (!ts)
5769 : : return false;
5770 : 21804 : if (!vec_safe_is_empty (ts->m_agg_values)
5771 : 21402 : || !vec_safe_is_empty (ts->m_vr))
5772 : 21552 : return true;
5773 : : return false;
5774 : : }
5775 : :
5776 : : /* Write into OB IPA-CP transfromation summary TS describing NODE. */
5777 : :
5778 : : void
5779 : 10763 : write_ipcp_transformation_info (output_block *ob, cgraph_node *node,
5780 : : ipcp_transformation *ts)
5781 : : {
5782 : 10763 : lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder;
5783 : 10763 : int node_ref = lto_symtab_encoder_encode (encoder, node);
5784 : 10763 : streamer_write_uhwi (ob, node_ref);
5785 : :
5786 : 10961 : streamer_write_uhwi (ob, vec_safe_length (ts->m_agg_values));
5787 : 12329 : for (const ipa_argagg_value &av : ts->m_agg_values)
5788 : : {
5789 : 1170 : struct bitpack_d bp;
5790 : :
5791 : 1170 : stream_write_tree (ob, av.value, true);
5792 : 1170 : streamer_write_uhwi (ob, av.unit_offset);
5793 : 1170 : streamer_write_uhwi (ob, av.index);
5794 : :
5795 : 1170 : bp = bitpack_create (ob->main_stream);
5796 : 1170 : bp_pack_value (&bp, av.by_ref, 1);
5797 : 1170 : bp_pack_value (&bp, av.killed, 1);
5798 : 1170 : streamer_write_bitpack (&bp);
5799 : : }
5800 : :
5801 : : /* If all instances of this node are inlined, ipcp info is not useful. */
5802 : 10763 : if (!lto_symtab_encoder_only_for_inlining_p (encoder, node))
5803 : : {
5804 : 19378 : streamer_write_uhwi (ob, vec_safe_length (ts->m_vr));
5805 : 45880 : for (const ipa_vr &parm_vr : ts->m_vr)
5806 : 16815 : parm_vr.streamer_write (ob);
5807 : : }
5808 : : else
5809 : 1072 : streamer_write_uhwi (ob, 0);
5810 : 10763 : }
5811 : :
5812 : : /* Stream in the aggregate value replacement chain for NODE from IB. */
5813 : :
5814 : : static void
5815 : 10763 : read_ipcp_transformation_info (lto_input_block *ib, cgraph_node *node,
5816 : : data_in *data_in)
5817 : : {
5818 : 10763 : unsigned int count, i;
5819 : 10763 : ipcp_transformation_initialize ();
5820 : 10763 : ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
5821 : :
5822 : 10763 : count = streamer_read_uhwi (ib);
5823 : 10763 : if (count > 0)
5824 : : {
5825 : 198 : vec_safe_grow_cleared (ts->m_agg_values, count, true);
5826 : 1368 : for (i = 0; i <count; i++)
5827 : : {
5828 : 1170 : ipa_argagg_value *av = &(*ts->m_agg_values)[i];;
5829 : :
5830 : 1170 : av->value = stream_read_tree (ib, data_in);
5831 : 1170 : av->unit_offset = streamer_read_uhwi (ib);
5832 : 1170 : av->index = streamer_read_uhwi (ib);
5833 : :
5834 : 1170 : bitpack_d bp = streamer_read_bitpack (ib);
5835 : 1170 : av->by_ref = bp_unpack_value (&bp, 1);
5836 : 1170 : av->killed = bp_unpack_value (&bp, 1);
5837 : : }
5838 : : }
5839 : :
5840 : 10763 : count = streamer_read_uhwi (ib);
5841 : 10763 : if (count > 0)
5842 : : {
5843 : 9687 : vec_safe_grow_cleared (ts->m_vr, count, true);
5844 : 26502 : for (i = 0; i < count; i++)
5845 : : {
5846 : 16815 : ipa_vr *parm_vr;
5847 : 16815 : parm_vr = &(*ts->m_vr)[i];
5848 : 16815 : parm_vr->streamer_read (ib, data_in);
5849 : : }
5850 : : }
5851 : 10763 : }
5852 : :
5853 : :
5854 : : /* Write all aggregate replacement for nodes in set. */
5855 : :
5856 : : void
5857 : 9206 : ipcp_write_transformation_summaries (void)
5858 : : {
5859 : 9206 : struct output_block *ob;
5860 : 9206 : unsigned int count = 0;
5861 : 9206 : lto_symtab_encoder_t encoder;
5862 : :
5863 : 9206 : ob = create_output_block (LTO_section_ipcp_transform);
5864 : 9206 : encoder = ob->decl_state->symtab_node_encoder;
5865 : 9206 : ob->symbol = NULL;
5866 : :
5867 : 245597 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5868 : : {
5869 : 113597 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5870 : 113597 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5871 : 113597 : if (!cnode)
5872 : 24552 : continue;
5873 : 89045 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
5874 : 89045 : if (useful_ipcp_transformation_info_p (ts)
5875 : 89045 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
5876 : 10763 : count++;
5877 : : }
5878 : :
5879 : 9206 : streamer_write_uhwi (ob, count);
5880 : :
5881 : 245597 : for (int i = 0; i < lto_symtab_encoder_size (encoder); i++)
5882 : : {
5883 : 113597 : toplevel_node *tnode = lto_symtab_encoder_deref (encoder, i);
5884 : 113597 : cgraph_node *cnode = dyn_cast <cgraph_node *> (tnode);
5885 : 113597 : if (!cnode)
5886 : 24552 : continue;
5887 : 89045 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
5888 : 89045 : if (useful_ipcp_transformation_info_p (ts)
5889 : 89045 : && lto_symtab_encoder_encode_body_p (encoder, cnode))
5890 : 10763 : write_ipcp_transformation_info (ob, cnode, ts);
5891 : : }
5892 : 9206 : ipa_write_return_summaries (ob);
5893 : 9206 : produce_asm (ob);
5894 : 9206 : destroy_output_block (ob);
5895 : 9206 : }
5896 : :
5897 : : /* Read replacements section in file FILE_DATA of length LEN with data
5898 : : DATA. */
5899 : :
5900 : : static void
5901 : 9206 : read_replacements_section (struct lto_file_decl_data *file_data,
5902 : : const char *data,
5903 : : size_t len)
5904 : : {
5905 : 9206 : const struct lto_function_header *header =
5906 : : (const struct lto_function_header *) data;
5907 : 9206 : const int cfg_offset = sizeof (struct lto_function_header);
5908 : 9206 : const int main_offset = cfg_offset + header->cfg_size;
5909 : 9206 : const int string_offset = main_offset + header->main_size;
5910 : 9206 : class data_in *data_in;
5911 : 9206 : unsigned int i;
5912 : 9206 : unsigned int count;
5913 : :
5914 : 9206 : lto_input_block ib_main ((const char *) data + main_offset,
5915 : 9206 : header->main_size, file_data);
5916 : :
5917 : 9206 : data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
5918 : 9206 : header->string_size, vNULL);
5919 : 9206 : count = streamer_read_uhwi (&ib_main);
5920 : :
5921 : 19969 : for (i = 0; i < count; i++)
5922 : : {
5923 : 10763 : unsigned int index;
5924 : 10763 : struct cgraph_node *node;
5925 : 10763 : lto_symtab_encoder_t encoder;
5926 : :
5927 : 10763 : index = streamer_read_uhwi (&ib_main);
5928 : 10763 : encoder = file_data->symtab_node_encoder;
5929 : 10763 : node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
5930 : : index));
5931 : 10763 : read_ipcp_transformation_info (&ib_main, node, data_in);
5932 : : }
5933 : 9206 : ipa_read_return_summaries (&ib_main, file_data, data_in);
5934 : 9206 : lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
5935 : : len);
5936 : 9206 : lto_data_in_delete (data_in);
5937 : 9206 : }
5938 : :
5939 : : /* Read IPA-CP aggregate replacements. */
5940 : :
5941 : : void
5942 : 9206 : ipcp_read_transformation_summaries (void)
5943 : : {
5944 : 9206 : struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
5945 : 9206 : struct lto_file_decl_data *file_data;
5946 : 9206 : unsigned int j = 0;
5947 : :
5948 : 27618 : while ((file_data = file_data_vec[j++]))
5949 : : {
5950 : 9206 : size_t len;
5951 : 9206 : const char *data
5952 : 9206 : = lto_get_summary_section_data (file_data, LTO_section_ipcp_transform,
5953 : : &len);
5954 : 9206 : if (data)
5955 : 9206 : read_replacements_section (file_data, data, len);
5956 : : }
5957 : 9206 : }
5958 : :
5959 : : /* Adjust the aggregate replacements in TS to reflect any parameter removals
5960 : : which might have already taken place. If after adjustments there are no
5961 : : aggregate replacements left, the m_agg_values will be set to NULL. In other
5962 : : cases, it may be shrunk. */
5963 : :
5964 : : static void
5965 : 1854 : adjust_agg_replacement_values (cgraph_node *node, ipcp_transformation *ts)
5966 : : {
5967 : 1854 : clone_info *cinfo = clone_info::get (node);
5968 : 1854 : if (!cinfo || !cinfo->param_adjustments)
5969 : : return;
5970 : :
5971 : 928 : auto_vec<int, 16> new_indices;
5972 : 928 : cinfo->param_adjustments->get_updated_indices (&new_indices);
5973 : 928 : bool removed_item = false;
5974 : 928 : unsigned dst_index = 0;
5975 : 928 : unsigned count = ts->m_agg_values->length ();
5976 : 5018 : for (unsigned i = 0; i < count; i++)
5977 : : {
5978 : 4090 : ipa_argagg_value *v = &(*ts->m_agg_values)[i];
5979 : 4090 : gcc_checking_assert (v->index >= 0);
5980 : :
5981 : 4090 : int new_idx = -1;
5982 : 4090 : if ((unsigned) v->index < new_indices.length ())
5983 : 2577 : new_idx = new_indices[v->index];
5984 : :
5985 : 2577 : if (new_idx >= 0)
5986 : : {
5987 : 1792 : v->index = new_idx;
5988 : 1792 : if (removed_item)
5989 : 15 : (*ts->m_agg_values)[dst_index] = *v;
5990 : 1792 : dst_index++;
5991 : : }
5992 : : else
5993 : : removed_item = true;
5994 : : }
5995 : :
5996 : 928 : if (dst_index == 0)
5997 : : {
5998 : 565 : ggc_free (ts->m_agg_values);
5999 : 565 : ts->m_agg_values = NULL;
6000 : : }
6001 : 363 : else if (removed_item)
6002 : 28 : ts->m_agg_values->truncate (dst_index);
6003 : :
6004 : 928 : return;
6005 : 928 : }
6006 : :
6007 : : /* Dominator walker driving the ipcp modification phase. */
6008 : :
6009 : : class ipcp_modif_dom_walker : public dom_walker
6010 : : {
6011 : : public:
6012 : 1289 : ipcp_modif_dom_walker (struct ipa_func_body_info *fbi,
6013 : : vec<ipa_param_descriptor, va_gc> *descs,
6014 : : ipcp_transformation *ts, bool *sc)
6015 : 2578 : : dom_walker (CDI_DOMINATORS), m_fbi (fbi), m_descriptors (descs),
6016 : 1289 : m_ts (ts), m_something_changed (sc) {}
6017 : :
6018 : : edge before_dom_children (basic_block) final override;
6019 : 1289 : bool cleanup_eh ()
6020 : 1289 : { return gimple_purge_all_dead_eh_edges (m_need_eh_cleanup); }
6021 : :
6022 : : private:
6023 : : struct ipa_func_body_info *m_fbi;
6024 : : vec<ipa_param_descriptor, va_gc> *m_descriptors;
6025 : : ipcp_transformation *m_ts;
6026 : : bool *m_something_changed;
6027 : : auto_bitmap m_need_eh_cleanup;
6028 : : };
6029 : :
6030 : : edge
6031 : 21788 : ipcp_modif_dom_walker::before_dom_children (basic_block bb)
6032 : : {
6033 : 21788 : gimple_stmt_iterator gsi;
6034 : 124649 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6035 : : {
6036 : 81073 : gimple *stmt = gsi_stmt (gsi);
6037 : 81073 : tree rhs, val, t;
6038 : 81073 : HOST_WIDE_INT bit_offset;
6039 : 81073 : poly_int64 size;
6040 : 81073 : int index;
6041 : 81073 : bool by_ref, vce;
6042 : :
6043 : 81073 : if (!gimple_assign_load_p (stmt))
6044 : 79533 : continue;
6045 : 11493 : rhs = gimple_assign_rhs1 (stmt);
6046 : 11493 : if (!is_gimple_reg_type (TREE_TYPE (rhs)))
6047 : 732 : continue;
6048 : :
6049 : 25404 : vce = false;
6050 : : t = rhs;
6051 : 25404 : while (handled_component_p (t))
6052 : : {
6053 : : /* V_C_E can do things like convert an array of integers to one
6054 : : bigger integer and similar things we do not handle below. */
6055 : 14643 : if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
6056 : : {
6057 : : vce = true;
6058 : : break;
6059 : : }
6060 : 14643 : t = TREE_OPERAND (t, 0);
6061 : : }
6062 : 10761 : if (vce)
6063 : 0 : continue;
6064 : :
6065 : 10761 : if (!ipa_load_from_parm_agg (m_fbi, m_descriptors, stmt, rhs, &index,
6066 : : &bit_offset, &size, &by_ref))
6067 : 7980 : continue;
6068 : 2781 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
6069 : 2781 : ipa_argagg_value_list avl (m_ts);
6070 : 2781 : tree v = avl.get_value (index, unit_offset, by_ref);
6071 : :
6072 : 4022 : if (!v
6073 : 2781 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), size))
6074 : 1241 : continue;
6075 : :
6076 : 1540 : gcc_checking_assert (is_gimple_ip_invariant (v));
6077 : 1540 : if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v)))
6078 : : {
6079 : 0 : if (fold_convertible_p (TREE_TYPE (rhs), v))
6080 : 0 : val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v);
6081 : 0 : else if (TYPE_SIZE (TREE_TYPE (rhs))
6082 : 0 : == TYPE_SIZE (TREE_TYPE (v)))
6083 : 0 : val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v);
6084 : : else
6085 : : {
6086 : 0 : if (dump_file)
6087 : : {
6088 : 0 : fprintf (dump_file, " const ");
6089 : 0 : print_generic_expr (dump_file, v);
6090 : 0 : fprintf (dump_file, " can't be converted to type of ");
6091 : 0 : print_generic_expr (dump_file, rhs);
6092 : 0 : fprintf (dump_file, "\n");
6093 : : }
6094 : 0 : continue;
6095 : : }
6096 : : }
6097 : : else
6098 : : val = v;
6099 : :
6100 : 1540 : if (dump_file && (dump_flags & TDF_DETAILS))
6101 : : {
6102 : 38 : fprintf (dump_file, "Modifying stmt:\n ");
6103 : 38 : print_gimple_stmt (dump_file, stmt, 0);
6104 : : }
6105 : 1540 : gimple_assign_set_rhs_from_tree (&gsi, val);
6106 : 1540 : update_stmt (stmt);
6107 : :
6108 : 1540 : if (dump_file && (dump_flags & TDF_DETAILS))
6109 : : {
6110 : 38 : fprintf (dump_file, "into:\n ");
6111 : 38 : print_gimple_stmt (dump_file, stmt, 0);
6112 : 38 : fprintf (dump_file, "\n");
6113 : : }
6114 : :
6115 : 1540 : *m_something_changed = true;
6116 : 1540 : if (maybe_clean_eh_stmt (stmt))
6117 : 9 : bitmap_set_bit (m_need_eh_cleanup, bb->index);
6118 : : }
6119 : 21788 : return NULL;
6120 : : }
6121 : :
6122 : : /* If IPA-CP discovered a constant in parameter PARM at OFFSET of a given SIZE
6123 : : - whether passed by reference or not is given by BY_REF - return that
6124 : : constant. Otherwise return NULL_TREE. The is supposed to be used only
6125 : : after clone materialization and transformation is done (because it asserts
6126 : : that killed constants have been pruned). */
6127 : :
6128 : : tree
6129 : 4337041 : ipcp_get_aggregate_const (struct function *func, tree parm, bool by_ref,
6130 : : HOST_WIDE_INT bit_offset, HOST_WIDE_INT bit_size)
6131 : : {
6132 : 4337041 : cgraph_node *node = cgraph_node::get (func->decl);
6133 : 4337041 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
6134 : :
6135 : 4337041 : if (!ts || !ts->m_agg_values)
6136 : : return NULL_TREE;
6137 : :
6138 : 9768 : int index = ts->get_param_index (func->decl, parm);
6139 : 9768 : if (index < 0)
6140 : : return NULL_TREE;
6141 : :
6142 : 9713 : ipa_argagg_value_list avl (ts);
6143 : 9713 : unsigned unit_offset = bit_offset / BITS_PER_UNIT;
6144 : 9713 : const ipa_argagg_value *av = avl.get_elt (index, unit_offset);
6145 : 9713 : if (!av || av->by_ref != by_ref)
6146 : : return NULL_TREE;
6147 : 1954 : gcc_assert (!av->killed);
6148 : 1954 : tree v = av->value;
6149 : 1954 : if (!v
6150 : 1954 : || maybe_ne (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (v))), bit_size))
6151 : 714 : return NULL_TREE;
6152 : :
6153 : : return v;
6154 : : }
6155 : :
6156 : : /* Return true if we have recorded VALUE and MASK about PARM.
6157 : : Set VALUE and MASk accordingly. */
6158 : :
6159 : : bool
6160 : 7657964 : ipcp_get_parm_bits (tree parm, tree *value, widest_int *mask)
6161 : : {
6162 : 7657964 : cgraph_node *cnode = cgraph_node::get (current_function_decl);
6163 : 7657964 : ipcp_transformation *ts = ipcp_get_transformation_summary (cnode);
6164 : 7657964 : if (!ts
6165 : 120501 : || vec_safe_length (ts->m_vr) == 0
6166 : 7810834 : || !ipa_vr_supported_type_p (TREE_TYPE (parm)))
6167 : : return false;
6168 : :
6169 : 112607 : int i = ts->get_param_index (current_function_decl, parm);
6170 : 112607 : if (i < 0)
6171 : : return false;
6172 : 111269 : clone_info *cinfo = clone_info::get (cnode);
6173 : 111269 : if (cinfo && cinfo->param_adjustments)
6174 : : {
6175 : 29217 : i = cinfo->param_adjustments->get_original_index (i);
6176 : 29217 : if (i < 0)
6177 : : return false;
6178 : : }
6179 : :
6180 : 102797 : vec<ipa_vr, va_gc> &vr = *ts->m_vr;
6181 : 102797 : if (!vr[i].known_p ())
6182 : : return false;
6183 : 82189 : value_range tmp;
6184 : 82189 : vr[i].get_vrange (tmp);
6185 : 82189 : if (tmp.undefined_p () || tmp.varying_p ())
6186 : : return false;
6187 : 82189 : irange_bitmask bm;
6188 : 82189 : bm = tmp.get_bitmask ();
6189 : 82189 : *mask = widest_int::from (bm.mask (), TYPE_SIGN (TREE_TYPE (parm)));
6190 : 82189 : *value = wide_int_to_tree (TREE_TYPE (parm), bm.value ());
6191 : 82189 : return true;
6192 : 82189 : }
6193 : :
6194 : : /* Update value range of formal parameters of NODE as described in TS. */
6195 : :
6196 : : static void
6197 : 21444 : ipcp_update_vr (struct cgraph_node *node, ipcp_transformation *ts)
6198 : : {
6199 : 21444 : if (vec_safe_is_empty (ts->m_vr))
6200 : 632 : return;
6201 : 20812 : const vec<ipa_vr, va_gc> &vr = *ts->m_vr;
6202 : 20812 : unsigned count = vr.length ();
6203 : 20812 : if (!count)
6204 : : return;
6205 : :
6206 : 20812 : auto_vec<int, 16> new_indices;
6207 : 20812 : bool need_remapping = false;
6208 : 20812 : clone_info *cinfo = clone_info::get (node);
6209 : 20812 : if (cinfo && cinfo->param_adjustments)
6210 : : {
6211 : 6000 : cinfo->param_adjustments->get_updated_indices (&new_indices);
6212 : 6000 : need_remapping = true;
6213 : : }
6214 : 20812 : auto_vec <tree, 16> parm_decls;
6215 : 20812 : push_function_arg_decls (&parm_decls, node->decl);
6216 : :
6217 : 71314 : for (unsigned i = 0; i < count; ++i)
6218 : : {
6219 : 50502 : tree parm;
6220 : 50502 : int remapped_idx;
6221 : 50502 : if (need_remapping)
6222 : : {
6223 : 17787 : if (i >= new_indices.length ())
6224 : 7818 : continue;
6225 : 9969 : remapped_idx = new_indices[i];
6226 : 9969 : if (remapped_idx < 0)
6227 : 2206 : continue;
6228 : : }
6229 : : else
6230 : 32715 : remapped_idx = i;
6231 : :
6232 : 40478 : parm = parm_decls[remapped_idx];
6233 : :
6234 : 40478 : gcc_checking_assert (parm);
6235 : 40478 : tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl), parm);
6236 : :
6237 : 40478 : if (!ddef || !is_gimple_reg (parm))
6238 : 5635 : continue;
6239 : :
6240 : 34843 : if (vr[i].known_p ())
6241 : : {
6242 : 27406 : value_range tmp;
6243 : 27406 : vr[i].get_vrange (tmp);
6244 : :
6245 : 27406 : if (!tmp.undefined_p () && !tmp.varying_p ())
6246 : : {
6247 : 27406 : if (dump_file)
6248 : : {
6249 : 97 : fprintf (dump_file, "Setting value range of param %u "
6250 : : "(now %i) ", i, remapped_idx);
6251 : 97 : tmp.dump (dump_file);
6252 : 97 : fprintf (dump_file, "]\n");
6253 : : }
6254 : 27406 : set_range_info (ddef, tmp);
6255 : :
6256 : 42326 : if (POINTER_TYPE_P (TREE_TYPE (parm))
6257 : 30395 : && opt_for_fn (node->decl, flag_ipa_bit_cp))
6258 : : {
6259 : 15474 : irange_bitmask bm = tmp.get_bitmask ();
6260 : 15474 : unsigned tem = bm.mask ().to_uhwi ();
6261 : 15474 : unsigned HOST_WIDE_INT bitpos = bm.value ().to_uhwi ();
6262 : 15474 : unsigned align = tem & -tem;
6263 : 15474 : unsigned misalign = bitpos & (align - 1);
6264 : :
6265 : 15474 : if (align > 1)
6266 : : {
6267 : 12937 : if (dump_file)
6268 : : {
6269 : 73 : fprintf (dump_file,
6270 : : "Adjusting mask for param %u to ", i);
6271 : 73 : print_hex (bm.mask (), dump_file);
6272 : 73 : fprintf (dump_file, "\n");
6273 : : }
6274 : :
6275 : 12937 : if (dump_file)
6276 : 73 : fprintf (dump_file,
6277 : : "Adjusting align: %u, misalign: %u\n",
6278 : : align, misalign);
6279 : :
6280 : 12937 : unsigned old_align, old_misalign;
6281 : 12937 : struct ptr_info_def *pi = get_ptr_info (ddef);
6282 : 12937 : bool old_known = get_ptr_info_alignment (pi, &old_align,
6283 : : &old_misalign);
6284 : :
6285 : 12937 : if (old_known && old_align > align)
6286 : : {
6287 : 0 : if (dump_file)
6288 : : {
6289 : 0 : fprintf (dump_file,
6290 : : "But alignment was already %u.\n",
6291 : : old_align);
6292 : 0 : if ((old_misalign & (align - 1)) != misalign)
6293 : 0 : fprintf (dump_file,
6294 : : "old_misalign (%u) and misalign "
6295 : : "(%u) mismatch\n",
6296 : : old_misalign, misalign);
6297 : : }
6298 : 0 : continue;
6299 : : }
6300 : :
6301 : 12937 : if (dump_file
6302 : 73 : && old_known
6303 : 0 : && ((misalign & (old_align - 1)) != old_misalign))
6304 : 0 : fprintf (dump_file,
6305 : : "old_misalign (%u) and misalign (%u) "
6306 : : "mismatch\n",
6307 : : old_misalign, misalign);
6308 : :
6309 : 12937 : set_ptr_info_alignment (pi, align, misalign);
6310 : : }
6311 : 15474 : }
6312 : 11932 : else if (dump_file && INTEGRAL_TYPE_P (TREE_TYPE (parm)))
6313 : : {
6314 : 20 : irange &r = as_a<irange> (tmp);
6315 : 20 : irange_bitmask bm = r.get_bitmask ();
6316 : 20 : unsigned prec = TYPE_PRECISION (TREE_TYPE (parm));
6317 : 20 : if (wi::ne_p (bm.mask (), wi::shwi (-1, prec)))
6318 : : {
6319 : 13 : fprintf (dump_file,
6320 : : "Adjusting mask for param %u to ", i);
6321 : 13 : print_hex (bm.mask (), dump_file);
6322 : 13 : fprintf (dump_file, "\n");
6323 : : }
6324 : 20 : }
6325 : : }
6326 : 27406 : }
6327 : : }
6328 : 20812 : }
6329 : :
6330 : : /* IPCP transformation phase doing propagation of aggregate values. */
6331 : :
6332 : : unsigned int
6333 : 968634 : ipcp_transform_function (struct cgraph_node *node)
6334 : : {
6335 : 968634 : struct ipa_func_body_info fbi;
6336 : 968634 : int param_count;
6337 : :
6338 : 968634 : gcc_checking_assert (cfun);
6339 : 968634 : gcc_checking_assert (current_function_decl);
6340 : :
6341 : 968634 : if (dump_file)
6342 : 688 : fprintf (dump_file, "Modification phase of node %s\n",
6343 : : node->dump_name ());
6344 : :
6345 : 968634 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
6346 : 968634 : if (!ts
6347 : 968634 : || (vec_safe_is_empty (ts->m_agg_values)
6348 : 20867 : && vec_safe_is_empty (ts->m_vr)))
6349 : : return 0;
6350 : :
6351 : 21444 : ts->maybe_create_parm_idx_map (cfun->decl);
6352 : 21444 : ipcp_update_vr (node, ts);
6353 : 969455 : if (vec_safe_is_empty (ts->m_agg_values))
6354 : : return 0;
6355 : 2110 : param_count = count_formal_params (node->decl);
6356 : 2110 : if (param_count == 0)
6357 : : return 0;
6358 : :
6359 : 1854 : adjust_agg_replacement_values (node, ts);
6360 : 1854 : if (vec_safe_is_empty (ts->m_agg_values))
6361 : : {
6362 : 565 : if (dump_file)
6363 : 4 : fprintf (dump_file, " All affected aggregate parameters were either "
6364 : : "removed or converted into scalars, phase done.\n");
6365 : 565 : return 0;
6366 : : }
6367 : 1289 : if (dump_file)
6368 : : {
6369 : 47 : fprintf (dump_file, " Aggregate replacements:");
6370 : 47 : ipa_argagg_value_list avs (ts);
6371 : 47 : avs.dump (dump_file);
6372 : : }
6373 : :
6374 : 1289 : fbi.node = node;
6375 : 1289 : fbi.info = NULL;
6376 : 1289 : fbi.bb_infos = vNULL;
6377 : 1289 : fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
6378 : 1289 : fbi.param_count = param_count;
6379 : 1289 : fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps);
6380 : :
6381 : 1289 : vec<ipa_param_descriptor, va_gc> *descriptors = NULL;
6382 : 1289 : vec_safe_grow_cleared (descriptors, param_count, true);
6383 : 1289 : ipa_populate_param_decls (node, *descriptors);
6384 : 1289 : bool modified_mem_access = false;
6385 : 1289 : calculate_dominance_info (CDI_DOMINATORS);
6386 : 1289 : ipcp_modif_dom_walker walker (&fbi, descriptors, ts, &modified_mem_access);
6387 : 1289 : walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
6388 : 1289 : free_dominance_info (CDI_DOMINATORS);
6389 : 1289 : bool cfg_changed = walker.cleanup_eh ();
6390 : :
6391 : 1289 : int i;
6392 : 1289 : struct ipa_bb_info *bi;
6393 : 25655 : FOR_EACH_VEC_ELT (fbi.bb_infos, i, bi)
6394 : 46154 : free_ipa_bb_info (bi);
6395 : 1289 : fbi.bb_infos.release ();
6396 : :
6397 : 1289 : ts->remove_argaggs_if ([](const ipa_argagg_value &v)
6398 : : {
6399 : 4891 : return v.killed;
6400 : : });
6401 : :
6402 : 1289 : vec_free (descriptors);
6403 : 1289 : if (cfg_changed)
6404 : 1 : delete_unreachable_blocks_update_callgraph (node, false);
6405 : :
6406 : 1289 : return modified_mem_access ? TODO_update_ssa_only_virtuals : 0;
6407 : 1289 : }
6408 : :
6409 : : /* Record that current function return value range is VAL. */
6410 : :
6411 : : void
6412 : 726109 : ipa_record_return_value_range (value_range val)
6413 : : {
6414 : 726109 : ipa_record_return_value_range_1
6415 : 726109 : (cgraph_node::get (current_function_decl), val);
6416 : 726109 : }
6417 : :
6418 : : /* Return true if value range of DECL is known and if so initialize RANGE. */
6419 : :
6420 : : bool
6421 : 11831505 : ipa_return_value_range (value_range &range, tree decl)
6422 : : {
6423 : 11831505 : cgraph_node *n = cgraph_node::get (decl);
6424 : 11831505 : if (!n || !ipa_return_value_sum)
6425 : : return false;
6426 : 9608415 : enum availability avail;
6427 : 9608415 : n = n->ultimate_alias_target (&avail);
6428 : 9608415 : if (avail < AVAIL_AVAILABLE)
6429 : : return false;
6430 : 2194870 : if (n->decl != decl && !useless_type_conversion_p (TREE_TYPE (decl), TREE_TYPE (n->decl)))
6431 : : return false;
6432 : 2194870 : ipa_return_value_summary *v = ipa_return_value_sum->get (n);
6433 : 2194870 : if (!v)
6434 : : return false;
6435 : 593773 : v->vr->get_vrange (range);
6436 : 593773 : return true;
6437 : : }
6438 : :
6439 : : /* Reset all state within ipa-prop.cc so that we can rerun the compiler
6440 : : within the same process. For use by toplev::finalize. */
6441 : :
6442 : : void
6443 : 260333 : ipa_prop_cc_finalize (void)
6444 : : {
6445 : 260333 : if (function_insertion_hook_holder)
6446 : 13395 : symtab->remove_cgraph_insertion_hook (function_insertion_hook_holder);
6447 : 260333 : function_insertion_hook_holder = NULL;
6448 : :
6449 : 260333 : if (ipa_edge_args_sum)
6450 : 13722 : ggc_delete (ipa_edge_args_sum);
6451 : 260333 : ipa_edge_args_sum = NULL;
6452 : :
6453 : 260333 : if (ipa_node_params_sum)
6454 : 13722 : ggc_delete (ipa_node_params_sum);
6455 : 260333 : ipa_node_params_sum = NULL;
6456 : 260333 : }
6457 : :
6458 : : /* Return true if the two pass_through components of two jump functions are
6459 : : known to be equivalent. AGG_JF denotes whether they are part of aggregate
6460 : : functions or not. The function can be used before the IPA phase of IPA-CP
6461 : : or inlining because it cannot cope with refdesc changes these passes can
6462 : : carry out. */
6463 : :
6464 : : static bool
6465 : 42979 : ipa_agg_pass_through_jf_equivalent_p (ipa_pass_through_data *ipt1,
6466 : : ipa_pass_through_data *ipt2,
6467 : : bool agg_jf)
6468 : :
6469 : : {
6470 : 42979 : gcc_assert (agg_jf ||
6471 : : (!ipt1->refdesc_decremented && !ipt2->refdesc_decremented));
6472 : 42979 : if (ipt1->operation != ipt2->operation
6473 : 42979 : || ipt1->formal_id != ipt2->formal_id
6474 : 42979 : || (!agg_jf && (ipt1->agg_preserved != ipt2->agg_preserved)))
6475 : : return false;
6476 : 42979 : if (ipt1->operation != NOP_EXPR
6477 : 42979 : && (TYPE_MAIN_VARIANT (ipt1->op_type)
6478 : 5849 : != TYPE_MAIN_VARIANT (ipt2->op_type)))
6479 : : return false;
6480 : 42971 : if (((ipt1->operand != NULL_TREE) != (ipt2->operand != NULL_TREE))
6481 : 42971 : || (ipt1->operand
6482 : 5841 : && !values_equal_for_ipcp_p (ipt1->operand, ipt2->operand)))
6483 : 0 : return false;
6484 : : return true;
6485 : : }
6486 : :
6487 : : /* Return true if the two aggregate jump functions are known to be equivalent.
6488 : : The function can be used before the IPA phase of IPA-CP or inlining because
6489 : : it cannot cope with refdesc changes these passes can carry out. */
6490 : :
6491 : : static bool
6492 : 3753 : ipa_agg_jump_functions_equivalent_p (ipa_agg_jf_item *ajf1,
6493 : : ipa_agg_jf_item *ajf2)
6494 : : {
6495 : 3753 : if (ajf1->offset != ajf2->offset
6496 : 3753 : || ajf1->jftype != ajf2->jftype
6497 : 7506 : || !types_compatible_p (ajf1->type, ajf2->type))
6498 : 0 : return false;
6499 : :
6500 : 3753 : switch (ajf1->jftype)
6501 : : {
6502 : 2012 : case IPA_JF_CONST:
6503 : 2012 : if (!values_equal_for_ipcp_p (ajf1->value.constant,
6504 : : ajf2->value.constant))
6505 : : return false;
6506 : : break;
6507 : 938 : case IPA_JF_PASS_THROUGH:
6508 : 938 : {
6509 : 938 : ipa_pass_through_data *ipt1 = &ajf1->value.pass_through;
6510 : 938 : ipa_pass_through_data *ipt2 = &ajf2->value.pass_through;
6511 : 938 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, true))
6512 : : return false;
6513 : : }
6514 : : break;
6515 : 803 : case IPA_JF_LOAD_AGG:
6516 : 803 : {
6517 : 803 : ipa_load_agg_data *ila1 = &ajf1->value.load_agg;
6518 : 803 : ipa_load_agg_data *ila2 = &ajf2->value.load_agg;
6519 : 803 : if (!ipa_agg_pass_through_jf_equivalent_p (&ila1->pass_through,
6520 : : &ila2->pass_through, true))
6521 : : return false;
6522 : 803 : if (ila1->offset != ila2->offset
6523 : 803 : || ila1->by_ref != ila2->by_ref
6524 : 1606 : || !types_compatible_p (ila1->type, ila2->type))
6525 : 0 : return false;
6526 : : }
6527 : : break;
6528 : 0 : default:
6529 : 0 : gcc_unreachable ();
6530 : : }
6531 : : return true;
6532 : : }
6533 : :
6534 : : /* Return true if the two jump functions are known to be equivalent. The
6535 : : function can be used before the IPA phase of IPA-CP or inlining because it
6536 : : cannot cope with refdesc changes these passes can carry out. */
6537 : :
6538 : : bool
6539 : 112255 : ipa_jump_functions_equivalent_p (ipa_jump_func *jf1, ipa_jump_func *jf2)
6540 : : {
6541 : 112255 : if (jf1->type != jf2->type)
6542 : : return false;
6543 : :
6544 : 112255 : switch (jf1->type)
6545 : : {
6546 : : case IPA_JF_UNKNOWN:
6547 : : break;
6548 : 21960 : case IPA_JF_CONST:
6549 : 21960 : {
6550 : 21960 : tree cst1 = ipa_get_jf_constant (jf1);
6551 : 21960 : tree cst2 = ipa_get_jf_constant (jf2);
6552 : 21960 : if (!values_equal_for_ipcp_p (cst1, cst2))
6553 : : return false;
6554 : :
6555 : 21959 : ipa_cst_ref_desc *rd1 = jfunc_rdesc_usable (jf1);
6556 : 21959 : ipa_cst_ref_desc *rd2 = jfunc_rdesc_usable (jf2);
6557 : 21959 : if (rd1 && rd2)
6558 : : {
6559 : 480 : gcc_assert (rd1->refcount == 1
6560 : : && rd2->refcount == 1);
6561 : 480 : gcc_assert (!rd1->next_duplicate && !rd2->next_duplicate);
6562 : : }
6563 : 21479 : else if (rd1)
6564 : : return false;
6565 : 21479 : else if (rd2)
6566 : : return false;
6567 : : }
6568 : : break;
6569 : 41238 : case IPA_JF_PASS_THROUGH:
6570 : 41238 : {
6571 : 41238 : ipa_pass_through_data *ipt1 = &jf1->value.pass_through;
6572 : 41238 : ipa_pass_through_data *ipt2 = &jf2->value.pass_through;
6573 : 41238 : if (!ipa_agg_pass_through_jf_equivalent_p (ipt1, ipt2, false))
6574 : : return false;
6575 : : }
6576 : : break;
6577 : 13704 : case IPA_JF_ANCESTOR:
6578 : 13704 : {
6579 : 13704 : ipa_ancestor_jf_data *ia1 = &jf1->value.ancestor;
6580 : 13704 : ipa_ancestor_jf_data *ia2 = &jf2->value.ancestor;
6581 : :
6582 : 13704 : if (ia1->formal_id != ia2->formal_id
6583 : 13704 : || ia1->agg_preserved != ia2->agg_preserved
6584 : 13704 : || ia1->keep_null != ia2->keep_null
6585 : 13704 : || ia1->offset != ia2->offset)
6586 : : return false;
6587 : : }
6588 : : break;
6589 : 0 : default:
6590 : 0 : gcc_unreachable ();
6591 : : }
6592 : :
6593 : 112246 : if (((jf1->m_vr != nullptr) != (jf2->m_vr != nullptr))
6594 : 112246 : || (jf1->m_vr && !jf1->m_vr->equal_p (*jf2->m_vr)))
6595 : 5 : return false;
6596 : :
6597 : 112241 : unsigned alen = vec_safe_length (jf1->agg.items);
6598 : 114744 : if (vec_safe_length (jf2->agg.items) != alen)
6599 : : return false;
6600 : :
6601 : 112240 : if (!alen)
6602 : : return true;
6603 : :
6604 : 2503 : if (jf1->agg.by_ref != jf2->agg.by_ref)
6605 : : return false;
6606 : :
6607 : 6256 : for (unsigned i = 0 ; i < alen; i++)
6608 : 3753 : if (!ipa_agg_jump_functions_equivalent_p (&(*jf1->agg.items)[i],
6609 : 3753 : &(*jf2->agg.items)[i]))
6610 : : return false;
6611 : :
6612 : : return true;
6613 : : }
6614 : :
6615 : : #include "gt-ipa-prop.h"
|