Debian Codegraph @

gcc/ipa-prop.cc

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