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