* rtl.h (insn_location): Declare.
[official-gcc.git] / gcc / ipa-prop.c
blob167082160ebb979e797fb7244f39d157548eedfc
1 /* Interprocedural analyses.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "basic-block.h"
25 #include "tree-ssa-alias.h"
26 #include "internal-fn.h"
27 #include "gimple-fold.h"
28 #include "tree-eh.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "expr.h"
33 #include "stor-layout.h"
34 #include "print-tree.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimplify-me.h"
38 #include "gimple-walk.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "ipa-prop.h"
42 #include "bitmap.h"
43 #include "gimple-ssa.h"
44 #include "tree-cfg.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "tree-into-ssa.h"
48 #include "tree-dfa.h"
49 #include "tree-pass.h"
50 #include "tree-inline.h"
51 #include "ipa-inline.h"
52 #include "flags.h"
53 #include "diagnostic.h"
54 #include "gimple-pretty-print.h"
55 #include "lto-streamer.h"
56 #include "data-streamer.h"
57 #include "tree-streamer.h"
58 #include "params.h"
59 #include "ipa-utils.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
62 #include "dbgcnt.h"
63 #include "domwalk.h"
64 #include "builtins.h"
66 /* Intermediate information that we get from alias analysis about a particular
67 parameter in a particular basic_block. When a parameter or the memory it
68 references is marked modified, we use that information in all dominatd
69 blocks without cosulting alias analysis oracle. */
71 struct param_aa_status
73 /* Set when this structure contains meaningful information. If not, the
74 structure describing a dominating BB should be used instead. */
75 bool valid;
77 /* Whether we have seen something which might have modified the data in
78 question. PARM is for the parameter itself, REF is for data it points to
79 but using the alias type of individual accesses and PT is the same thing
80 but for computing aggregate pass-through functions using a very inclusive
81 ao_ref. */
82 bool parm_modified, ref_modified, pt_modified;
85 /* Information related to a given BB that used only when looking at function
86 body. */
88 struct ipa_bb_info
90 /* Call graph edges going out of this BB. */
91 vec<cgraph_edge_p> cg_edges;
92 /* Alias analysis statuses of each formal parameter at this bb. */
93 vec<param_aa_status> param_aa_statuses;
96 /* Structure with global information that is only used when looking at function
97 body. */
99 struct func_body_info
101 /* The node that is being analyzed. */
102 cgraph_node *node;
104 /* Its info. */
105 struct ipa_node_params *info;
107 /* Information about individual BBs. */
108 vec<ipa_bb_info> bb_infos;
110 /* Number of parameters. */
111 int param_count;
113 /* Number of statements already walked by when analyzing this function. */
114 unsigned int aa_walked;
117 /* Vector where the parameter infos are actually stored. */
118 vec<ipa_node_params> ipa_node_params_vector;
119 /* Vector of known aggregate values in cloned nodes. */
120 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
121 /* Vector where the parameter infos are actually stored. */
122 vec<ipa_edge_args, va_gc> *ipa_edge_args_vector;
124 /* Holders of ipa cgraph hooks: */
125 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
126 static struct cgraph_node_hook_list *node_removal_hook_holder;
127 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
128 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
129 static struct cgraph_node_hook_list *function_insertion_hook_holder;
131 /* Description of a reference to an IPA constant. */
132 struct ipa_cst_ref_desc
134 /* Edge that corresponds to the statement which took the reference. */
135 struct cgraph_edge *cs;
136 /* Linked list of duplicates created when call graph edges are cloned. */
137 struct ipa_cst_ref_desc *next_duplicate;
138 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
139 if out of control. */
140 int refcount;
143 /* Allocation pool for reference descriptions. */
145 static alloc_pool ipa_refdesc_pool;
147 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
148 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
150 static bool
151 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
153 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
154 struct cl_optimization *os;
156 if (!fs_opts)
157 return false;
158 os = TREE_OPTIMIZATION (fs_opts);
159 return !os->x_optimize || !os->x_flag_ipa_cp;
162 /* Return index of the formal whose tree is PTREE in function which corresponds
163 to INFO. */
165 static int
166 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor> descriptors, tree ptree)
168 int i, count;
170 count = descriptors.length ();
171 for (i = 0; i < count; i++)
172 if (descriptors[i].decl == ptree)
173 return i;
175 return -1;
178 /* Return index of the formal whose tree is PTREE in function which corresponds
179 to INFO. */
182 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
184 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
187 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
188 NODE. */
190 static void
191 ipa_populate_param_decls (struct cgraph_node *node,
192 vec<ipa_param_descriptor> &descriptors)
194 tree fndecl;
195 tree fnargs;
196 tree parm;
197 int param_num;
199 fndecl = node->decl;
200 gcc_assert (gimple_has_body_p (fndecl));
201 fnargs = DECL_ARGUMENTS (fndecl);
202 param_num = 0;
203 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
205 descriptors[param_num].decl = parm;
206 descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm));
207 param_num++;
211 /* Return how many formal parameters FNDECL has. */
213 static inline int
214 count_formal_params (tree fndecl)
216 tree parm;
217 int count = 0;
218 gcc_assert (gimple_has_body_p (fndecl));
220 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
221 count++;
223 return count;
226 /* Return the declaration of Ith formal parameter of the function corresponding
227 to INFO. Note there is no setter function as this array is built just once
228 using ipa_initialize_node_params. */
230 void
231 ipa_dump_param (FILE *file, struct ipa_node_params *info, int i)
233 fprintf (file, "param #%i", i);
234 if (info->descriptors[i].decl)
236 fprintf (file, " ");
237 print_generic_expr (file, info->descriptors[i].decl, 0);
241 /* Initialize the ipa_node_params structure associated with NODE
242 to hold PARAM_COUNT parameters. */
244 void
245 ipa_alloc_node_params (struct cgraph_node *node, int param_count)
247 struct ipa_node_params *info = IPA_NODE_REF (node);
249 if (!info->descriptors.exists () && param_count)
250 info->descriptors.safe_grow_cleared (param_count);
253 /* Initialize the ipa_node_params structure associated with NODE by counting
254 the function parameters, creating the descriptors and populating their
255 param_decls. */
257 void
258 ipa_initialize_node_params (struct cgraph_node *node)
260 struct ipa_node_params *info = IPA_NODE_REF (node);
262 if (!info->descriptors.exists ())
264 ipa_alloc_node_params (node, count_formal_params (node->decl));
265 ipa_populate_param_decls (node, info->descriptors);
269 /* Print the jump functions associated with call graph edge CS to file F. */
271 static void
272 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
274 int i, count;
276 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
277 for (i = 0; i < count; i++)
279 struct ipa_jump_func *jump_func;
280 enum jump_func_type type;
282 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
283 type = jump_func->type;
285 fprintf (f, " param %d: ", i);
286 if (type == IPA_JF_UNKNOWN)
287 fprintf (f, "UNKNOWN\n");
288 else if (type == IPA_JF_KNOWN_TYPE)
290 fprintf (f, "KNOWN TYPE: base ");
291 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
292 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
293 jump_func->value.known_type.offset);
294 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
295 fprintf (f, "\n");
297 else if (type == IPA_JF_CONST)
299 tree val = jump_func->value.constant.value;
300 fprintf (f, "CONST: ");
301 print_generic_expr (f, val, 0);
302 if (TREE_CODE (val) == ADDR_EXPR
303 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
305 fprintf (f, " -> ");
306 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
309 fprintf (f, "\n");
311 else if (type == IPA_JF_PASS_THROUGH)
313 fprintf (f, "PASS THROUGH: ");
314 fprintf (f, "%d, op %s",
315 jump_func->value.pass_through.formal_id,
316 get_tree_code_name(jump_func->value.pass_through.operation));
317 if (jump_func->value.pass_through.operation != NOP_EXPR)
319 fprintf (f, " ");
320 print_generic_expr (f,
321 jump_func->value.pass_through.operand, 0);
323 if (jump_func->value.pass_through.agg_preserved)
324 fprintf (f, ", agg_preserved");
325 if (jump_func->value.pass_through.type_preserved)
326 fprintf (f, ", type_preserved");
327 fprintf (f, "\n");
329 else if (type == IPA_JF_ANCESTOR)
331 fprintf (f, "ANCESTOR: ");
332 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
333 jump_func->value.ancestor.formal_id,
334 jump_func->value.ancestor.offset);
335 print_generic_expr (f, jump_func->value.ancestor.type, 0);
336 if (jump_func->value.ancestor.agg_preserved)
337 fprintf (f, ", agg_preserved");
338 if (jump_func->value.ancestor.type_preserved)
339 fprintf (f, ", type_preserved");
340 fprintf (f, "\n");
343 if (jump_func->agg.items)
345 struct ipa_agg_jf_item *item;
346 int j;
348 fprintf (f, " Aggregate passed by %s:\n",
349 jump_func->agg.by_ref ? "reference" : "value");
350 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
352 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
353 item->offset);
354 if (TYPE_P (item->value))
355 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
356 tree_to_uhwi (TYPE_SIZE (item->value)));
357 else
359 fprintf (f, "cst: ");
360 print_generic_expr (f, item->value, 0);
362 fprintf (f, "\n");
369 /* Print the jump functions of all arguments on all call graph edges going from
370 NODE to file F. */
372 void
373 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
375 struct cgraph_edge *cs;
377 fprintf (f, " Jump functions of caller %s/%i:\n", node->name (),
378 node->order);
379 for (cs = node->callees; cs; cs = cs->next_callee)
381 if (!ipa_edge_args_info_available_for_edge_p (cs))
382 continue;
384 fprintf (f, " callsite %s/%i -> %s/%i : \n",
385 xstrdup (node->name ()), node->order,
386 xstrdup (cs->callee->name ()),
387 cs->callee->order);
388 ipa_print_node_jump_functions_for_edge (f, cs);
391 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
393 struct cgraph_indirect_call_info *ii;
394 if (!ipa_edge_args_info_available_for_edge_p (cs))
395 continue;
397 ii = cs->indirect_info;
398 if (ii->agg_contents)
399 fprintf (f, " indirect %s callsite, calling param %i, "
400 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
401 ii->member_ptr ? "member ptr" : "aggregate",
402 ii->param_index, ii->offset,
403 ii->by_ref ? "by reference" : "by_value");
404 else
405 fprintf (f, " indirect %s callsite, calling param %i, "
406 "offset " HOST_WIDE_INT_PRINT_DEC,
407 ii->polymorphic ? "polymorphic" : "simple", ii->param_index,
408 ii->offset);
410 if (cs->call_stmt)
412 fprintf (f, ", for stmt ");
413 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
415 else
416 fprintf (f, "\n");
417 ipa_print_node_jump_functions_for_edge (f, cs);
421 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
423 void
424 ipa_print_all_jump_functions (FILE *f)
426 struct cgraph_node *node;
428 fprintf (f, "\nJump functions:\n");
429 FOR_EACH_FUNCTION (node)
431 ipa_print_node_jump_functions (f, node);
435 /* Set JFUNC to be a known type jump function. */
437 static void
438 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
439 tree base_type, tree component_type)
441 gcc_assert (TREE_CODE (component_type) == RECORD_TYPE
442 && TYPE_BINFO (component_type));
443 if (!flag_devirtualize)
444 return;
445 gcc_assert (BINFO_VTABLE (TYPE_BINFO (component_type)));
446 jfunc->type = IPA_JF_KNOWN_TYPE;
447 jfunc->value.known_type.offset = offset,
448 jfunc->value.known_type.base_type = base_type;
449 jfunc->value.known_type.component_type = component_type;
450 gcc_assert (component_type);
453 /* Set JFUNC to be a copy of another jmp (to be used by jump function
454 combination code). The two functions will share their rdesc. */
456 static void
457 ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
458 struct ipa_jump_func *src)
461 gcc_checking_assert (src->type == IPA_JF_CONST);
462 dst->type = IPA_JF_CONST;
463 dst->value.constant = src->value.constant;
466 /* Set JFUNC to be a constant jmp function. */
468 static void
469 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
470 struct cgraph_edge *cs)
472 constant = unshare_expr (constant);
473 if (constant && EXPR_P (constant))
474 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
475 jfunc->type = IPA_JF_CONST;
476 jfunc->value.constant.value = unshare_expr_without_location (constant);
478 if (TREE_CODE (constant) == ADDR_EXPR
479 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
481 struct ipa_cst_ref_desc *rdesc;
482 if (!ipa_refdesc_pool)
483 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
484 sizeof (struct ipa_cst_ref_desc), 32);
486 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
487 rdesc->cs = cs;
488 rdesc->next_duplicate = NULL;
489 rdesc->refcount = 1;
490 jfunc->value.constant.rdesc = rdesc;
492 else
493 jfunc->value.constant.rdesc = NULL;
496 /* Set JFUNC to be a simple pass-through jump function. */
497 static void
498 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
499 bool agg_preserved, bool type_preserved)
501 jfunc->type = IPA_JF_PASS_THROUGH;
502 jfunc->value.pass_through.operand = NULL_TREE;
503 jfunc->value.pass_through.formal_id = formal_id;
504 jfunc->value.pass_through.operation = NOP_EXPR;
505 jfunc->value.pass_through.agg_preserved = agg_preserved;
506 jfunc->value.pass_through.type_preserved = type_preserved;
509 /* Set JFUNC to be an arithmetic pass through jump function. */
511 static void
512 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
513 tree operand, enum tree_code operation)
515 jfunc->type = IPA_JF_PASS_THROUGH;
516 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
517 jfunc->value.pass_through.formal_id = formal_id;
518 jfunc->value.pass_through.operation = operation;
519 jfunc->value.pass_through.agg_preserved = false;
520 jfunc->value.pass_through.type_preserved = false;
523 /* Set JFUNC to be an ancestor jump function. */
525 static void
526 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
527 tree type, int formal_id, bool agg_preserved,
528 bool type_preserved)
530 if (!flag_devirtualize)
531 type_preserved = false;
532 gcc_assert (!type_preserved
533 || (TREE_CODE (type) == RECORD_TYPE
534 && TYPE_BINFO (type)
535 && BINFO_VTABLE (TYPE_BINFO (type))));
536 jfunc->type = IPA_JF_ANCESTOR;
537 jfunc->value.ancestor.formal_id = formal_id;
538 jfunc->value.ancestor.offset = offset;
539 jfunc->value.ancestor.type = type_preserved ? type : NULL;
540 jfunc->value.ancestor.agg_preserved = agg_preserved;
541 jfunc->value.ancestor.type_preserved = type_preserved;
544 /* Extract the acual BINFO being described by JFUNC which must be a known type
545 jump function. */
547 tree
548 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
550 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
551 if (!base_binfo)
552 return NULL_TREE;
553 return get_binfo_at_offset (base_binfo,
554 jfunc->value.known_type.offset,
555 jfunc->value.known_type.component_type);
558 /* Get IPA BB information about the given BB. FBI is the context of analyzis
559 of this function body. */
561 static struct ipa_bb_info *
562 ipa_get_bb_info (struct func_body_info *fbi, basic_block bb)
564 gcc_checking_assert (fbi);
565 return &fbi->bb_infos[bb->index];
568 /* Structure to be passed in between detect_type_change and
569 check_stmt_for_type_change. */
571 struct type_change_info
573 /* Offset into the object where there is the virtual method pointer we are
574 looking for. */
575 HOST_WIDE_INT offset;
576 /* The declaration or SSA_NAME pointer of the base that we are checking for
577 type change. */
578 tree object;
579 /* If we actually can tell the type that the object has changed to, it is
580 stored in this field. Otherwise it remains NULL_TREE. */
581 tree known_current_type;
582 /* Set to true if dynamic type change has been detected. */
583 bool type_maybe_changed;
584 /* Set to true if multiple types have been encountered. known_current_type
585 must be disregarded in that case. */
586 bool multiple_types_encountered;
589 /* Return true if STMT can modify a virtual method table pointer.
591 This function makes special assumptions about both constructors and
592 destructors which are all the functions that are allowed to alter the VMT
593 pointers. It assumes that destructors begin with assignment into all VMT
594 pointers and that constructors essentially look in the following way:
596 1) The very first thing they do is that they call constructors of ancestor
597 sub-objects that have them.
599 2) Then VMT pointers of this and all its ancestors is set to new values
600 corresponding to the type corresponding to the constructor.
602 3) Only afterwards, other stuff such as constructor of member sub-objects
603 and the code written by the user is run. Only this may include calling
604 virtual functions, directly or indirectly.
606 There is no way to call a constructor of an ancestor sub-object in any
607 other way.
609 This means that we do not have to care whether constructors get the correct
610 type information because they will always change it (in fact, if we define
611 the type to be given by the VMT pointer, it is undefined).
613 The most important fact to derive from the above is that if, for some
614 statement in the section 3, we try to detect whether the dynamic type has
615 changed, we can safely ignore all calls as we examine the function body
616 backwards until we reach statements in section 2 because these calls cannot
617 be ancestor constructors or destructors (if the input is not bogus) and so
618 do not change the dynamic type (this holds true only for automatically
619 allocated objects but at the moment we devirtualize only these). We then
620 must detect that statements in section 2 change the dynamic type and can try
621 to derive the new type. That is enough and we can stop, we will never see
622 the calls into constructors of sub-objects in this code. Therefore we can
623 safely ignore all call statements that we traverse.
626 static bool
627 stmt_may_be_vtbl_ptr_store (gimple stmt)
629 if (is_gimple_call (stmt))
630 return false;
631 /* TODO: Skip clobbers, doing so triggers problem in PR60306. */
632 else if (is_gimple_assign (stmt))
634 tree lhs = gimple_assign_lhs (stmt);
636 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
638 if (flag_strict_aliasing
639 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
640 return false;
642 if (TREE_CODE (lhs) == COMPONENT_REF
643 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
644 return false;
645 /* In the future we might want to use get_base_ref_and_offset to find
646 if there is a field corresponding to the offset and if so, proceed
647 almost like if it was a component ref. */
650 return true;
653 /* If STMT can be proved to be an assignment to the virtual method table
654 pointer of ANALYZED_OBJ and the type associated with the new table
655 identified, return the type. Otherwise return NULL_TREE. */
657 static tree
658 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
660 HOST_WIDE_INT offset, size, max_size;
661 tree lhs, rhs, base, binfo;
663 if (!gimple_assign_single_p (stmt))
664 return NULL_TREE;
666 lhs = gimple_assign_lhs (stmt);
667 rhs = gimple_assign_rhs1 (stmt);
668 if (TREE_CODE (lhs) != COMPONENT_REF
669 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
670 return NULL_TREE;
672 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
673 if (offset != tci->offset
674 || size != POINTER_SIZE
675 || max_size != POINTER_SIZE)
676 return NULL_TREE;
677 if (TREE_CODE (base) == MEM_REF)
679 if (TREE_CODE (tci->object) != MEM_REF
680 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
681 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
682 TREE_OPERAND (base, 1)))
683 return NULL_TREE;
685 else if (tci->object != base)
686 return NULL_TREE;
688 binfo = vtable_pointer_value_to_binfo (rhs);
690 /* FIXME: vtable_pointer_value_to_binfo may return BINFO of a
691 base of outer type. In this case we would need to either
692 work on binfos or translate it back to outer type and offset.
693 KNOWN_TYPE jump functions are not ready for that, yet. */
694 if (!binfo || TYPE_BINFO (BINFO_TYPE (binfo)) != binfo)
695 return NULL;
697 return BINFO_TYPE (binfo);
700 /* Callback of walk_aliased_vdefs and a helper function for
701 detect_type_change to check whether a particular statement may modify
702 the virtual table pointer, and if possible also determine the new type of
703 the (sub-)object. It stores its result into DATA, which points to a
704 type_change_info structure. */
706 static bool
707 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
709 gimple stmt = SSA_NAME_DEF_STMT (vdef);
710 struct type_change_info *tci = (struct type_change_info *) data;
712 if (stmt_may_be_vtbl_ptr_store (stmt))
714 tree type;
715 type = extr_type_from_vtbl_ptr_store (stmt, tci);
716 if (tci->type_maybe_changed
717 && type != tci->known_current_type)
718 tci->multiple_types_encountered = true;
719 tci->known_current_type = type;
720 tci->type_maybe_changed = true;
721 return true;
723 else
724 return false;
729 /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
730 callsite CALL) by looking for assignments to its virtual table pointer. If
731 it is, return true and fill in the jump function JFUNC with relevant type
732 information or set it to unknown. ARG is the object itself (not a pointer
733 to it, unless dereferenced). BASE is the base of the memory access as
734 returned by get_ref_base_and_extent, as is the offset. */
736 static bool
737 detect_type_change (tree arg, tree base, tree comp_type, gimple call,
738 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
740 struct type_change_info tci;
741 ao_ref ao;
743 gcc_checking_assert (DECL_P (arg)
744 || TREE_CODE (arg) == MEM_REF
745 || handled_component_p (arg));
746 /* Const calls cannot call virtual methods through VMT and so type changes do
747 not matter. */
748 if (!flag_devirtualize || !gimple_vuse (call)
749 /* Be sure expected_type is polymorphic. */
750 || !comp_type
751 || TREE_CODE (comp_type) != RECORD_TYPE
752 || !TYPE_BINFO (comp_type)
753 || !BINFO_VTABLE (TYPE_BINFO (comp_type)))
754 return true;
756 /* C++ methods are not allowed to change THIS pointer unless they
757 are constructors or destructors. */
758 if (TREE_CODE (base) == MEM_REF
759 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
760 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
761 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0))) == PARM_DECL
762 && TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
763 && !DECL_CXX_CONSTRUCTOR_P (current_function_decl)
764 && !DECL_CXX_DESTRUCTOR_P (current_function_decl)
765 && (SSA_NAME_VAR (TREE_OPERAND (base, 0))
766 == DECL_ARGUMENTS (current_function_decl)))
767 return false;
769 ao_ref_init (&ao, arg);
770 ao.base = base;
771 ao.offset = offset;
772 ao.size = POINTER_SIZE;
773 ao.max_size = ao.size;
775 tci.offset = offset;
776 tci.object = get_base_address (arg);
777 tci.known_current_type = NULL_TREE;
778 tci.type_maybe_changed = false;
779 tci.multiple_types_encountered = false;
781 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
782 &tci, NULL);
783 if (!tci.type_maybe_changed)
784 return false;
786 if (!tci.known_current_type
787 || tci.multiple_types_encountered
788 || offset != 0)
789 jfunc->type = IPA_JF_UNKNOWN;
790 else
791 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
793 return true;
796 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
797 SSA name (its dereference will become the base and the offset is assumed to
798 be zero). */
800 static bool
801 detect_type_change_ssa (tree arg, tree comp_type,
802 gimple call, struct ipa_jump_func *jfunc)
804 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
805 if (!flag_devirtualize
806 || !POINTER_TYPE_P (TREE_TYPE (arg)))
807 return false;
809 arg = build2 (MEM_REF, ptr_type_node, arg,
810 build_int_cst (ptr_type_node, 0));
812 return detect_type_change (arg, arg, comp_type, call, jfunc, 0);
815 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
816 boolean variable pointed to by DATA. */
818 static bool
819 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
820 void *data)
822 bool *b = (bool *) data;
823 *b = true;
824 return true;
827 /* Return true if we have already walked so many statements in AA that we
828 should really just start giving up. */
830 static bool
831 aa_overwalked (struct func_body_info *fbi)
833 gcc_checking_assert (fbi);
834 return fbi->aa_walked > (unsigned) PARAM_VALUE (PARAM_IPA_MAX_AA_STEPS);
837 /* Find the nearest valid aa status for parameter specified by INDEX that
838 dominates BB. */
840 static struct param_aa_status *
841 find_dominating_aa_status (struct func_body_info *fbi, basic_block bb,
842 int index)
844 while (true)
846 bb = get_immediate_dominator (CDI_DOMINATORS, bb);
847 if (!bb)
848 return NULL;
849 struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
850 if (!bi->param_aa_statuses.is_empty ()
851 && bi->param_aa_statuses[index].valid)
852 return &bi->param_aa_statuses[index];
856 /* Get AA status structure for the given BB and parameter with INDEX. Allocate
857 structures and/or intialize the result with a dominating description as
858 necessary. */
860 static struct param_aa_status *
861 parm_bb_aa_status_for_bb (struct func_body_info *fbi, basic_block bb,
862 int index)
864 gcc_checking_assert (fbi);
865 struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
866 if (bi->param_aa_statuses.is_empty ())
867 bi->param_aa_statuses.safe_grow_cleared (fbi->param_count);
868 struct param_aa_status *paa = &bi->param_aa_statuses[index];
869 if (!paa->valid)
871 gcc_checking_assert (!paa->parm_modified
872 && !paa->ref_modified
873 && !paa->pt_modified);
874 struct param_aa_status *dom_paa;
875 dom_paa = find_dominating_aa_status (fbi, bb, index);
876 if (dom_paa)
877 *paa = *dom_paa;
878 else
879 paa->valid = true;
882 return paa;
885 /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
886 a value known not to be modified in this function before reaching the
887 statement STMT. FBI holds information about the function we have so far
888 gathered but do not survive the summary building stage. */
890 static bool
891 parm_preserved_before_stmt_p (struct func_body_info *fbi, int index,
892 gimple stmt, tree parm_load)
894 struct param_aa_status *paa;
895 bool modified = false;
896 ao_ref refd;
898 /* FIXME: FBI can be NULL if we are being called from outside
899 ipa_node_analysis or ipcp_transform_function, which currently happens
900 during inlining analysis. It would be great to extend fbi's lifetime and
901 always have it. Currently, we are just not afraid of too much walking in
902 that case. */
903 if (fbi)
905 if (aa_overwalked (fbi))
906 return false;
907 paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
908 if (paa->parm_modified)
909 return false;
911 else
912 paa = NULL;
914 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
915 ao_ref_init (&refd, parm_load);
916 int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
917 &modified, NULL);
918 if (fbi)
919 fbi->aa_walked += walked;
920 if (paa && modified)
921 paa->parm_modified = true;
922 return !modified;
925 /* If STMT is an assignment that loads a value from an parameter declaration,
926 return the index of the parameter in ipa_node_params which has not been
927 modified. Otherwise return -1. */
929 static int
930 load_from_unmodified_param (struct func_body_info *fbi,
931 vec<ipa_param_descriptor> descriptors,
932 gimple stmt)
934 int index;
935 tree op1;
937 if (!gimple_assign_single_p (stmt))
938 return -1;
940 op1 = gimple_assign_rhs1 (stmt);
941 if (TREE_CODE (op1) != PARM_DECL)
942 return -1;
944 index = ipa_get_param_decl_index_1 (descriptors, op1);
945 if (index < 0
946 || !parm_preserved_before_stmt_p (fbi, index, stmt, op1))
947 return -1;
949 return index;
952 /* Return true if memory reference REF (which must be a load through parameter
953 with INDEX) loads data that are known to be unmodified in this function
954 before reaching statement STMT. */
956 static bool
957 parm_ref_data_preserved_p (struct func_body_info *fbi,
958 int index, gimple stmt, tree ref)
960 struct param_aa_status *paa;
961 bool modified = false;
962 ao_ref refd;
964 /* FIXME: FBI can be NULL if we are being called from outside
965 ipa_node_analysis or ipcp_transform_function, which currently happens
966 during inlining analysis. It would be great to extend fbi's lifetime and
967 always have it. Currently, we are just not afraid of too much walking in
968 that case. */
969 if (fbi)
971 if (aa_overwalked (fbi))
972 return false;
973 paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (stmt), index);
974 if (paa->ref_modified)
975 return false;
977 else
978 paa = NULL;
980 gcc_checking_assert (gimple_vuse (stmt));
981 ao_ref_init (&refd, ref);
982 int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
983 &modified, NULL);
984 if (fbi)
985 fbi->aa_walked += walked;
986 if (paa && modified)
987 paa->ref_modified = true;
988 return !modified;
991 /* Return true if the data pointed to by PARM (which is a parameter with INDEX)
992 is known to be unmodified in this function before reaching call statement
993 CALL into which it is passed. FBI describes the function body. */
995 static bool
996 parm_ref_data_pass_through_p (struct func_body_info *fbi, int index,
997 gimple call, tree parm)
999 bool modified = false;
1000 ao_ref refd;
1002 /* It's unnecessary to calculate anything about memory contnets for a const
1003 function because it is not goin to use it. But do not cache the result
1004 either. Also, no such calculations for non-pointers. */
1005 if (!gimple_vuse (call)
1006 || !POINTER_TYPE_P (TREE_TYPE (parm))
1007 || aa_overwalked (fbi))
1008 return false;
1010 struct param_aa_status *paa = parm_bb_aa_status_for_bb (fbi, gimple_bb (call),
1011 index);
1012 if (paa->pt_modified)
1013 return false;
1015 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
1016 int walked = walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified,
1017 &modified, NULL);
1018 fbi->aa_walked += walked;
1019 if (modified)
1020 paa->pt_modified = true;
1021 return !modified;
1024 /* Return true if we can prove that OP is a memory reference loading unmodified
1025 data from an aggregate passed as a parameter and if the aggregate is passed
1026 by reference, that the alias type of the load corresponds to the type of the
1027 formal parameter (so that we can rely on this type for TBAA in callers).
1028 INFO and PARMS_AINFO describe parameters of the current function (but the
1029 latter can be NULL), STMT is the load statement. If function returns true,
1030 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
1031 within the aggregate and whether it is a load from a value passed by
1032 reference respectively. */
1034 static bool
1035 ipa_load_from_parm_agg_1 (struct func_body_info *fbi,
1036 vec<ipa_param_descriptor> descriptors,
1037 gimple stmt, tree op, int *index_p,
1038 HOST_WIDE_INT *offset_p, HOST_WIDE_INT *size_p,
1039 bool *by_ref_p)
1041 int index;
1042 HOST_WIDE_INT size, max_size;
1043 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
1045 if (max_size == -1 || max_size != size || *offset_p < 0)
1046 return false;
1048 if (DECL_P (base))
1050 int index = ipa_get_param_decl_index_1 (descriptors, base);
1051 if (index >= 0
1052 && parm_preserved_before_stmt_p (fbi, index, stmt, op))
1054 *index_p = index;
1055 *by_ref_p = false;
1056 if (size_p)
1057 *size_p = size;
1058 return true;
1060 return false;
1063 if (TREE_CODE (base) != MEM_REF
1064 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
1065 || !integer_zerop (TREE_OPERAND (base, 1)))
1066 return false;
1068 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
1070 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
1071 index = ipa_get_param_decl_index_1 (descriptors, parm);
1073 else
1075 /* This branch catches situations where a pointer parameter is not a
1076 gimple register, for example:
1078 void hip7(S*) (struct S * p)
1080 void (*<T2e4>) (struct S *) D.1867;
1081 struct S * p.1;
1083 <bb 2>:
1084 p.1_1 = p;
1085 D.1867_2 = p.1_1->f;
1086 D.1867_2 ();
1087 gdp = &p;
1090 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
1091 index = load_from_unmodified_param (fbi, descriptors, def);
1094 if (index >= 0
1095 && parm_ref_data_preserved_p (fbi, index, stmt, op))
1097 *index_p = index;
1098 *by_ref_p = true;
1099 if (size_p)
1100 *size_p = size;
1101 return true;
1103 return false;
1106 /* Just like the previous function, just without the param_analysis_info
1107 pointer, for users outside of this file. */
1109 bool
1110 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
1111 tree op, int *index_p, HOST_WIDE_INT *offset_p,
1112 bool *by_ref_p)
1114 return ipa_load_from_parm_agg_1 (NULL, info->descriptors, stmt, op, index_p,
1115 offset_p, NULL, by_ref_p);
1118 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
1119 of an assignment statement STMT, try to determine whether we are actually
1120 handling any of the following cases and construct an appropriate jump
1121 function into JFUNC if so:
1123 1) The passed value is loaded from a formal parameter which is not a gimple
1124 register (most probably because it is addressable, the value has to be
1125 scalar) and we can guarantee the value has not changed. This case can
1126 therefore be described by a simple pass-through jump function. For example:
1128 foo (int a)
1130 int a.0;
1132 a.0_2 = a;
1133 bar (a.0_2);
1135 2) The passed value can be described by a simple arithmetic pass-through
1136 jump function. E.g.
1138 foo (int a)
1140 int D.2064;
1142 D.2064_4 = a.1(D) + 4;
1143 bar (D.2064_4);
1145 This case can also occur in combination of the previous one, e.g.:
1147 foo (int a, int z)
1149 int a.0;
1150 int D.2064;
1152 a.0_3 = a;
1153 D.2064_4 = a.0_3 + 4;
1154 foo (D.2064_4);
1156 3) The passed value is an address of an object within another one (which
1157 also passed by reference). Such situations are described by an ancestor
1158 jump function and describe situations such as:
1160 B::foo() (struct B * const this)
1162 struct A * D.1845;
1164 D.1845_2 = &this_1(D)->D.1748;
1165 A::bar (D.1845_2);
1167 INFO is the structure describing individual parameters access different
1168 stages of IPA optimizations. PARMS_AINFO contains the information that is
1169 only needed for intraprocedural analysis. */
1171 static void
1172 compute_complex_assign_jump_func (struct func_body_info *fbi,
1173 struct ipa_node_params *info,
1174 struct ipa_jump_func *jfunc,
1175 gimple call, gimple stmt, tree name,
1176 tree param_type)
1178 HOST_WIDE_INT offset, size, max_size;
1179 tree op1, tc_ssa, base, ssa;
1180 int index;
1182 op1 = gimple_assign_rhs1 (stmt);
1184 if (TREE_CODE (op1) == SSA_NAME)
1186 if (SSA_NAME_IS_DEFAULT_DEF (op1))
1187 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1188 else
1189 index = load_from_unmodified_param (fbi, info->descriptors,
1190 SSA_NAME_DEF_STMT (op1));
1191 tc_ssa = op1;
1193 else
1195 index = load_from_unmodified_param (fbi, info->descriptors, stmt);
1196 tc_ssa = gimple_assign_lhs (stmt);
1199 if (index >= 0)
1201 tree op2 = gimple_assign_rhs2 (stmt);
1203 if (op2)
1205 if (!is_gimple_ip_invariant (op2)
1206 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
1207 && !useless_type_conversion_p (TREE_TYPE (name),
1208 TREE_TYPE (op1))))
1209 return;
1211 ipa_set_jf_arith_pass_through (jfunc, index, op2,
1212 gimple_assign_rhs_code (stmt));
1214 else if (gimple_assign_single_p (stmt))
1216 bool agg_p = parm_ref_data_pass_through_p (fbi, index, call, tc_ssa);
1217 bool type_p = false;
1219 if (param_type && POINTER_TYPE_P (param_type))
1220 type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type),
1221 call, jfunc);
1222 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1223 ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p);
1225 return;
1228 if (TREE_CODE (op1) != ADDR_EXPR)
1229 return;
1230 op1 = TREE_OPERAND (op1, 0);
1231 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
1232 return;
1233 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1234 if (TREE_CODE (base) != MEM_REF
1235 /* If this is a varying address, punt. */
1236 || max_size == -1
1237 || max_size != size)
1238 return;
1239 offset += mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
1240 ssa = TREE_OPERAND (base, 0);
1241 if (TREE_CODE (ssa) != SSA_NAME
1242 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1243 || offset < 0)
1244 return;
1246 /* Dynamic types are changed in constructors and destructors. */
1247 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1248 if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1250 bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type),
1251 call, jfunc, offset);
1252 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1253 ipa_set_ancestor_jf (jfunc, offset,
1254 type_p ? TREE_TYPE (param_type) : NULL, index,
1255 parm_ref_data_pass_through_p (fbi, index,
1256 call, ssa), type_p);
1260 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1261 it looks like:
1263 iftmp.1_3 = &obj_2(D)->D.1762;
1265 The base of the MEM_REF must be a default definition SSA NAME of a
1266 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1267 whole MEM_REF expression is returned and the offset calculated from any
1268 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1269 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1271 static tree
1272 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1274 HOST_WIDE_INT size, max_size;
1275 tree expr, parm, obj;
1277 if (!gimple_assign_single_p (assign))
1278 return NULL_TREE;
1279 expr = gimple_assign_rhs1 (assign);
1281 if (TREE_CODE (expr) != ADDR_EXPR)
1282 return NULL_TREE;
1283 expr = TREE_OPERAND (expr, 0);
1284 obj = expr;
1285 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1287 if (TREE_CODE (expr) != MEM_REF
1288 /* If this is a varying address, punt. */
1289 || max_size == -1
1290 || max_size != size
1291 || *offset < 0)
1292 return NULL_TREE;
1293 parm = TREE_OPERAND (expr, 0);
1294 if (TREE_CODE (parm) != SSA_NAME
1295 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1296 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1297 return NULL_TREE;
1299 *offset += mem_ref_offset (expr).to_short_addr () * BITS_PER_UNIT;
1300 *obj_p = obj;
1301 return expr;
1305 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1306 statement PHI, try to find out whether NAME is in fact a
1307 multiple-inheritance typecast from a descendant into an ancestor of a formal
1308 parameter and thus can be described by an ancestor jump function and if so,
1309 write the appropriate function into JFUNC.
1311 Essentially we want to match the following pattern:
1313 if (obj_2(D) != 0B)
1314 goto <bb 3>;
1315 else
1316 goto <bb 4>;
1318 <bb 3>:
1319 iftmp.1_3 = &obj_2(D)->D.1762;
1321 <bb 4>:
1322 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1323 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1324 return D.1879_6; */
1326 static void
1327 compute_complex_ancestor_jump_func (struct func_body_info *fbi,
1328 struct ipa_node_params *info,
1329 struct ipa_jump_func *jfunc,
1330 gimple call, gimple phi, tree param_type)
1332 HOST_WIDE_INT offset;
1333 gimple assign, cond;
1334 basic_block phi_bb, assign_bb, cond_bb;
1335 tree tmp, parm, expr, obj;
1336 int index, i;
1338 if (gimple_phi_num_args (phi) != 2)
1339 return;
1341 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1342 tmp = PHI_ARG_DEF (phi, 0);
1343 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1344 tmp = PHI_ARG_DEF (phi, 1);
1345 else
1346 return;
1347 if (TREE_CODE (tmp) != SSA_NAME
1348 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1349 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1350 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1351 return;
1353 assign = SSA_NAME_DEF_STMT (tmp);
1354 assign_bb = gimple_bb (assign);
1355 if (!single_pred_p (assign_bb))
1356 return;
1357 expr = get_ancestor_addr_info (assign, &obj, &offset);
1358 if (!expr)
1359 return;
1360 parm = TREE_OPERAND (expr, 0);
1361 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1362 if (index < 0)
1363 return;
1365 cond_bb = single_pred (assign_bb);
1366 cond = last_stmt (cond_bb);
1367 if (!cond
1368 || gimple_code (cond) != GIMPLE_COND
1369 || gimple_cond_code (cond) != NE_EXPR
1370 || gimple_cond_lhs (cond) != parm
1371 || !integer_zerop (gimple_cond_rhs (cond)))
1372 return;
1374 phi_bb = gimple_bb (phi);
1375 for (i = 0; i < 2; i++)
1377 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1378 if (pred != assign_bb && pred != cond_bb)
1379 return;
1382 bool type_p = false;
1383 if (param_type && POINTER_TYPE_P (param_type))
1384 type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type),
1385 call, jfunc, offset);
1386 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1387 ipa_set_ancestor_jf (jfunc, offset, type_p ? TREE_TYPE (param_type) : NULL,
1388 index,
1389 parm_ref_data_pass_through_p (fbi, index, call, parm),
1390 type_p);
1393 /* Given OP which is passed as an actual argument to a called function,
1394 determine if it is possible to construct a KNOWN_TYPE jump function for it
1395 and if so, create one and store it to JFUNC.
1396 EXPECTED_TYPE represents a type the argument should be in */
1398 static void
1399 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1400 gimple call, tree expected_type)
1402 HOST_WIDE_INT offset, size, max_size;
1403 tree base;
1405 if (!flag_devirtualize
1406 || TREE_CODE (op) != ADDR_EXPR
1407 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE
1408 /* Be sure expected_type is polymorphic. */
1409 || !expected_type
1410 || TREE_CODE (expected_type) != RECORD_TYPE
1411 || !TYPE_BINFO (expected_type)
1412 || !BINFO_VTABLE (TYPE_BINFO (expected_type)))
1413 return;
1415 op = TREE_OPERAND (op, 0);
1416 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1417 if (!DECL_P (base)
1418 || max_size == -1
1419 || max_size != size
1420 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1421 || is_global_var (base))
1422 return;
1424 if (detect_type_change (op, base, expected_type, call, jfunc, offset))
1425 return;
1427 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base),
1428 expected_type);
1431 /* Inspect the given TYPE and return true iff it has the same structure (the
1432 same number of fields of the same types) as a C++ member pointer. If
1433 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1434 corresponding fields there. */
1436 static bool
1437 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1439 tree fld;
1441 if (TREE_CODE (type) != RECORD_TYPE)
1442 return false;
1444 fld = TYPE_FIELDS (type);
1445 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1446 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1447 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1448 return false;
1450 if (method_ptr)
1451 *method_ptr = fld;
1453 fld = DECL_CHAIN (fld);
1454 if (!fld || INTEGRAL_TYPE_P (fld)
1455 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1456 return false;
1457 if (delta)
1458 *delta = fld;
1460 if (DECL_CHAIN (fld))
1461 return false;
1463 return true;
1466 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1467 return the rhs of its defining statement. Otherwise return RHS as it
1468 is. */
1470 static inline tree
1471 get_ssa_def_if_simple_copy (tree rhs)
1473 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1475 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1477 if (gimple_assign_single_p (def_stmt))
1478 rhs = gimple_assign_rhs1 (def_stmt);
1479 else
1480 break;
1482 return rhs;
1485 /* Simple linked list, describing known contents of an aggregate beforere
1486 call. */
1488 struct ipa_known_agg_contents_list
1490 /* Offset and size of the described part of the aggregate. */
1491 HOST_WIDE_INT offset, size;
1492 /* Known constant value or NULL if the contents is known to be unknown. */
1493 tree constant;
1494 /* Pointer to the next structure in the list. */
1495 struct ipa_known_agg_contents_list *next;
1498 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1499 in ARG is filled in with constant values. ARG can either be an aggregate
1500 expression or a pointer to an aggregate. ARG_TYPE is the type of the aggregate.
1501 JFUNC is the jump function into which the constants are subsequently stored. */
1503 static void
1504 determine_known_aggregate_parts (gimple call, tree arg, tree arg_type,
1505 struct ipa_jump_func *jfunc)
1507 struct ipa_known_agg_contents_list *list = NULL;
1508 int item_count = 0, const_count = 0;
1509 HOST_WIDE_INT arg_offset, arg_size;
1510 gimple_stmt_iterator gsi;
1511 tree arg_base;
1512 bool check_ref, by_ref;
1513 ao_ref r;
1515 /* The function operates in three stages. First, we prepare check_ref, r,
1516 arg_base and arg_offset based on what is actually passed as an actual
1517 argument. */
1519 if (POINTER_TYPE_P (arg_type))
1521 by_ref = true;
1522 if (TREE_CODE (arg) == SSA_NAME)
1524 tree type_size;
1525 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type))))
1526 return;
1527 check_ref = true;
1528 arg_base = arg;
1529 arg_offset = 0;
1530 type_size = TYPE_SIZE (TREE_TYPE (arg_type));
1531 arg_size = tree_to_uhwi (type_size);
1532 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1534 else if (TREE_CODE (arg) == ADDR_EXPR)
1536 HOST_WIDE_INT arg_max_size;
1538 arg = TREE_OPERAND (arg, 0);
1539 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1540 &arg_max_size);
1541 if (arg_max_size == -1
1542 || arg_max_size != arg_size
1543 || arg_offset < 0)
1544 return;
1545 if (DECL_P (arg_base))
1547 tree size;
1548 check_ref = false;
1549 size = build_int_cst (integer_type_node, arg_size);
1550 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1552 else
1553 return;
1555 else
1556 return;
1558 else
1560 HOST_WIDE_INT arg_max_size;
1562 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1564 by_ref = false;
1565 check_ref = false;
1566 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1567 &arg_max_size);
1568 if (arg_max_size == -1
1569 || arg_max_size != arg_size
1570 || arg_offset < 0)
1571 return;
1573 ao_ref_init (&r, arg);
1576 /* Second stage walks back the BB, looks at individual statements and as long
1577 as it is confident of how the statements affect contents of the
1578 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1579 describing it. */
1580 gsi = gsi_for_stmt (call);
1581 gsi_prev (&gsi);
1582 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1584 struct ipa_known_agg_contents_list *n, **p;
1585 gimple stmt = gsi_stmt (gsi);
1586 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1587 tree lhs, rhs, lhs_base;
1588 bool partial_overlap;
1590 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1591 continue;
1592 if (!gimple_assign_single_p (stmt))
1593 break;
1595 lhs = gimple_assign_lhs (stmt);
1596 rhs = gimple_assign_rhs1 (stmt);
1597 if (!is_gimple_reg_type (TREE_TYPE (rhs))
1598 || TREE_CODE (lhs) == BIT_FIELD_REF
1599 || contains_bitfld_component_ref_p (lhs))
1600 break;
1602 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1603 &lhs_max_size);
1604 if (lhs_max_size == -1
1605 || lhs_max_size != lhs_size
1606 || (lhs_offset < arg_offset
1607 && lhs_offset + lhs_size > arg_offset)
1608 || (lhs_offset < arg_offset + arg_size
1609 && lhs_offset + lhs_size > arg_offset + arg_size))
1610 break;
1612 if (check_ref)
1614 if (TREE_CODE (lhs_base) != MEM_REF
1615 || TREE_OPERAND (lhs_base, 0) != arg_base
1616 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1617 break;
1619 else if (lhs_base != arg_base)
1621 if (DECL_P (lhs_base))
1622 continue;
1623 else
1624 break;
1627 if (lhs_offset + lhs_size < arg_offset
1628 || lhs_offset >= (arg_offset + arg_size))
1629 continue;
1631 partial_overlap = false;
1632 p = &list;
1633 while (*p && (*p)->offset < lhs_offset)
1635 if ((*p)->offset + (*p)->size > lhs_offset)
1637 partial_overlap = true;
1638 break;
1640 p = &(*p)->next;
1642 if (partial_overlap)
1643 break;
1644 if (*p && (*p)->offset < lhs_offset + lhs_size)
1646 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1647 /* We already know this value is subsequently overwritten with
1648 something else. */
1649 continue;
1650 else
1651 /* Otherwise this is a partial overlap which we cannot
1652 represent. */
1653 break;
1656 rhs = get_ssa_def_if_simple_copy (rhs);
1657 n = XALLOCA (struct ipa_known_agg_contents_list);
1658 n->size = lhs_size;
1659 n->offset = lhs_offset;
1660 if (is_gimple_ip_invariant (rhs))
1662 n->constant = rhs;
1663 const_count++;
1665 else
1666 n->constant = NULL_TREE;
1667 n->next = *p;
1668 *p = n;
1670 item_count++;
1671 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1672 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1673 break;
1676 /* Third stage just goes over the list and creates an appropriate vector of
1677 ipa_agg_jf_item structures out of it, of sourse only if there are
1678 any known constants to begin with. */
1680 if (const_count)
1682 jfunc->agg.by_ref = by_ref;
1683 vec_alloc (jfunc->agg.items, const_count);
1684 while (list)
1686 if (list->constant)
1688 struct ipa_agg_jf_item item;
1689 item.offset = list->offset - arg_offset;
1690 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1691 item.value = unshare_expr_without_location (list->constant);
1692 jfunc->agg.items->quick_push (item);
1694 list = list->next;
1699 static tree
1700 ipa_get_callee_param_type (struct cgraph_edge *e, int i)
1702 int n;
1703 tree type = (e->callee
1704 ? TREE_TYPE (e->callee->decl)
1705 : gimple_call_fntype (e->call_stmt));
1706 tree t = TYPE_ARG_TYPES (type);
1708 for (n = 0; n < i; n++)
1710 if (!t)
1711 break;
1712 t = TREE_CHAIN (t);
1714 if (t)
1715 return TREE_VALUE (t);
1716 if (!e->callee)
1717 return NULL;
1718 t = DECL_ARGUMENTS (e->callee->decl);
1719 for (n = 0; n < i; n++)
1721 if (!t)
1722 return NULL;
1723 t = TREE_CHAIN (t);
1725 if (t)
1726 return TREE_TYPE (t);
1727 return NULL;
1730 /* Compute jump function for all arguments of callsite CS and insert the
1731 information in the jump_functions array in the ipa_edge_args corresponding
1732 to this callsite. */
1734 static void
1735 ipa_compute_jump_functions_for_edge (struct func_body_info *fbi,
1736 struct cgraph_edge *cs)
1738 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1739 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1740 gimple call = cs->call_stmt;
1741 int n, arg_num = gimple_call_num_args (call);
1743 if (arg_num == 0 || args->jump_functions)
1744 return;
1745 vec_safe_grow_cleared (args->jump_functions, arg_num);
1747 if (gimple_call_internal_p (call))
1748 return;
1749 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1750 return;
1752 for (n = 0; n < arg_num; n++)
1754 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1755 tree arg = gimple_call_arg (call, n);
1756 tree param_type = ipa_get_callee_param_type (cs, n);
1758 if (is_gimple_ip_invariant (arg))
1759 ipa_set_jf_constant (jfunc, arg, cs);
1760 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1761 && TREE_CODE (arg) == PARM_DECL)
1763 int index = ipa_get_param_decl_index (info, arg);
1765 gcc_assert (index >=0);
1766 /* Aggregate passed by value, check for pass-through, otherwise we
1767 will attempt to fill in aggregate contents later in this
1768 for cycle. */
1769 if (parm_preserved_before_stmt_p (fbi, index, call, arg))
1771 ipa_set_jf_simple_pass_through (jfunc, index, false, false);
1772 continue;
1775 else if (TREE_CODE (arg) == SSA_NAME)
1777 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1779 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1780 if (index >= 0)
1782 bool agg_p, type_p;
1783 agg_p = parm_ref_data_pass_through_p (fbi, index, call, arg);
1784 if (param_type && POINTER_TYPE_P (param_type))
1785 type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type),
1786 call, jfunc);
1787 else
1788 type_p = false;
1789 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1790 ipa_set_jf_simple_pass_through (jfunc, index, agg_p,
1791 type_p);
1794 else
1796 gimple stmt = SSA_NAME_DEF_STMT (arg);
1797 if (is_gimple_assign (stmt))
1798 compute_complex_assign_jump_func (fbi, info, jfunc,
1799 call, stmt, arg, param_type);
1800 else if (gimple_code (stmt) == GIMPLE_PHI)
1801 compute_complex_ancestor_jump_func (fbi, info, jfunc,
1802 call, stmt, param_type);
1805 else
1806 compute_known_type_jump_func (arg, jfunc, call,
1807 param_type
1808 && POINTER_TYPE_P (param_type)
1809 ? TREE_TYPE (param_type)
1810 : NULL);
1812 /* If ARG is pointer, we can not use its type to determine the type of aggregate
1813 passed (because type conversions are ignored in gimple). Usually we can
1814 safely get type from function declaration, but in case of K&R prototypes or
1815 variadic functions we can try our luck with type of the pointer passed.
1816 TODO: Since we look for actual initialization of the memory object, we may better
1817 work out the type based on the memory stores we find. */
1818 if (!param_type)
1819 param_type = TREE_TYPE (arg);
1821 if ((jfunc->type != IPA_JF_PASS_THROUGH
1822 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1823 && (jfunc->type != IPA_JF_ANCESTOR
1824 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1825 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1826 || POINTER_TYPE_P (param_type)))
1827 determine_known_aggregate_parts (call, arg, param_type, jfunc);
1831 /* Compute jump functions for all edges - both direct and indirect - outgoing
1832 from BB. */
1834 static void
1835 ipa_compute_jump_functions_for_bb (struct func_body_info *fbi, basic_block bb)
1837 struct ipa_bb_info *bi = ipa_get_bb_info (fbi, bb);
1838 int i;
1839 struct cgraph_edge *cs;
1841 FOR_EACH_VEC_ELT_REVERSE (bi->cg_edges, i, cs)
1843 struct cgraph_node *callee = cs->callee;
1845 if (callee)
1847 cgraph_function_or_thunk_node (callee, NULL);
1848 /* We do not need to bother analyzing calls to unknown functions
1849 unless they may become known during lto/whopr. */
1850 if (!callee->definition && !flag_lto)
1851 continue;
1853 ipa_compute_jump_functions_for_edge (fbi, cs);
1857 /* If STMT looks like a statement loading a value from a member pointer formal
1858 parameter, return that parameter and store the offset of the field to
1859 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1860 might be clobbered). If USE_DELTA, then we look for a use of the delta
1861 field rather than the pfn. */
1863 static tree
1864 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1865 HOST_WIDE_INT *offset_p)
1867 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1869 if (!gimple_assign_single_p (stmt))
1870 return NULL_TREE;
1872 rhs = gimple_assign_rhs1 (stmt);
1873 if (TREE_CODE (rhs) == COMPONENT_REF)
1875 ref_field = TREE_OPERAND (rhs, 1);
1876 rhs = TREE_OPERAND (rhs, 0);
1878 else
1879 ref_field = NULL_TREE;
1880 if (TREE_CODE (rhs) != MEM_REF)
1881 return NULL_TREE;
1882 rec = TREE_OPERAND (rhs, 0);
1883 if (TREE_CODE (rec) != ADDR_EXPR)
1884 return NULL_TREE;
1885 rec = TREE_OPERAND (rec, 0);
1886 if (TREE_CODE (rec) != PARM_DECL
1887 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1888 return NULL_TREE;
1889 ref_offset = TREE_OPERAND (rhs, 1);
1891 if (use_delta)
1892 fld = delta_field;
1893 else
1894 fld = ptr_field;
1895 if (offset_p)
1896 *offset_p = int_bit_position (fld);
1898 if (ref_field)
1900 if (integer_nonzerop (ref_offset))
1901 return NULL_TREE;
1902 return ref_field == fld ? rec : NULL_TREE;
1904 else
1905 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1906 : NULL_TREE;
1909 /* Returns true iff T is an SSA_NAME defined by a statement. */
1911 static bool
1912 ipa_is_ssa_with_stmt_def (tree t)
1914 if (TREE_CODE (t) == SSA_NAME
1915 && !SSA_NAME_IS_DEFAULT_DEF (t))
1916 return true;
1917 else
1918 return false;
1921 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1922 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1923 indirect call graph edge. */
1925 static struct cgraph_edge *
1926 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1928 struct cgraph_edge *cs;
1930 cs = cgraph_edge (node, stmt);
1931 cs->indirect_info->param_index = param_index;
1932 cs->indirect_info->agg_contents = 0;
1933 cs->indirect_info->member_ptr = 0;
1934 return cs;
1937 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1938 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1939 intermediate information about each formal parameter. Currently it checks
1940 whether the call calls a pointer that is a formal parameter and if so, the
1941 parameter is marked with the called flag and an indirect call graph edge
1942 describing the call is created. This is very simple for ordinary pointers
1943 represented in SSA but not-so-nice when it comes to member pointers. The
1944 ugly part of this function does nothing more than trying to match the
1945 pattern of such a call. An example of such a pattern is the gimple dump
1946 below, the call is on the last line:
1948 <bb 2>:
1949 f$__delta_5 = f.__delta;
1950 f$__pfn_24 = f.__pfn;
1953 <bb 2>:
1954 f$__delta_5 = MEM[(struct *)&f];
1955 f$__pfn_24 = MEM[(struct *)&f + 4B];
1957 and a few lines below:
1959 <bb 5>
1960 D.2496_3 = (int) f$__pfn_24;
1961 D.2497_4 = D.2496_3 & 1;
1962 if (D.2497_4 != 0)
1963 goto <bb 3>;
1964 else
1965 goto <bb 4>;
1967 <bb 6>:
1968 D.2500_7 = (unsigned int) f$__delta_5;
1969 D.2501_8 = &S + D.2500_7;
1970 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1971 D.2503_10 = *D.2502_9;
1972 D.2504_12 = f$__pfn_24 + -1;
1973 D.2505_13 = (unsigned int) D.2504_12;
1974 D.2506_14 = D.2503_10 + D.2505_13;
1975 D.2507_15 = *D.2506_14;
1976 iftmp.11_16 = (String:: *) D.2507_15;
1978 <bb 7>:
1979 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1980 D.2500_19 = (unsigned int) f$__delta_5;
1981 D.2508_20 = &S + D.2500_19;
1982 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1984 Such patterns are results of simple calls to a member pointer:
1986 int doprinting (int (MyString::* f)(int) const)
1988 MyString S ("somestring");
1990 return (S.*f)(4);
1993 Moreover, the function also looks for called pointers loaded from aggregates
1994 passed by value or reference. */
1996 static void
1997 ipa_analyze_indirect_call_uses (struct func_body_info *fbi, gimple call,
1998 tree target)
2000 struct ipa_node_params *info = fbi->info;
2001 HOST_WIDE_INT offset;
2002 bool by_ref;
2004 if (SSA_NAME_IS_DEFAULT_DEF (target))
2006 tree var = SSA_NAME_VAR (target);
2007 int index = ipa_get_param_decl_index (info, var);
2008 if (index >= 0)
2009 ipa_note_param_call (fbi->node, index, call);
2010 return;
2013 int index;
2014 gimple def = SSA_NAME_DEF_STMT (target);
2015 if (gimple_assign_single_p (def)
2016 && ipa_load_from_parm_agg_1 (fbi, info->descriptors, def,
2017 gimple_assign_rhs1 (def), &index, &offset,
2018 NULL, &by_ref))
2020 struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index, call);
2021 if (cs->indirect_info->offset != offset)
2022 cs->indirect_info->outer_type = NULL;
2023 cs->indirect_info->offset = offset;
2024 cs->indirect_info->agg_contents = 1;
2025 cs->indirect_info->by_ref = by_ref;
2026 return;
2029 /* Now we need to try to match the complex pattern of calling a member
2030 pointer. */
2031 if (gimple_code (def) != GIMPLE_PHI
2032 || gimple_phi_num_args (def) != 2
2033 || !POINTER_TYPE_P (TREE_TYPE (target))
2034 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
2035 return;
2037 /* First, we need to check whether one of these is a load from a member
2038 pointer that is a parameter to this function. */
2039 tree n1 = PHI_ARG_DEF (def, 0);
2040 tree n2 = PHI_ARG_DEF (def, 1);
2041 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
2042 return;
2043 gimple d1 = SSA_NAME_DEF_STMT (n1);
2044 gimple d2 = SSA_NAME_DEF_STMT (n2);
2046 tree rec;
2047 basic_block bb, virt_bb;
2048 basic_block join = gimple_bb (def);
2049 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
2051 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
2052 return;
2054 bb = EDGE_PRED (join, 0)->src;
2055 virt_bb = gimple_bb (d2);
2057 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
2059 bb = EDGE_PRED (join, 1)->src;
2060 virt_bb = gimple_bb (d1);
2062 else
2063 return;
2065 /* Second, we need to check that the basic blocks are laid out in the way
2066 corresponding to the pattern. */
2068 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
2069 || single_pred (virt_bb) != bb
2070 || single_succ (virt_bb) != join)
2071 return;
2073 /* Third, let's see that the branching is done depending on the least
2074 significant bit of the pfn. */
2076 gimple branch = last_stmt (bb);
2077 if (!branch || gimple_code (branch) != GIMPLE_COND)
2078 return;
2080 if ((gimple_cond_code (branch) != NE_EXPR
2081 && gimple_cond_code (branch) != EQ_EXPR)
2082 || !integer_zerop (gimple_cond_rhs (branch)))
2083 return;
2085 tree cond = gimple_cond_lhs (branch);
2086 if (!ipa_is_ssa_with_stmt_def (cond))
2087 return;
2089 def = SSA_NAME_DEF_STMT (cond);
2090 if (!is_gimple_assign (def)
2091 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
2092 || !integer_onep (gimple_assign_rhs2 (def)))
2093 return;
2095 cond = gimple_assign_rhs1 (def);
2096 if (!ipa_is_ssa_with_stmt_def (cond))
2097 return;
2099 def = SSA_NAME_DEF_STMT (cond);
2101 if (is_gimple_assign (def)
2102 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
2104 cond = gimple_assign_rhs1 (def);
2105 if (!ipa_is_ssa_with_stmt_def (cond))
2106 return;
2107 def = SSA_NAME_DEF_STMT (cond);
2110 tree rec2;
2111 rec2 = ipa_get_stmt_member_ptr_load_param (def,
2112 (TARGET_PTRMEMFUNC_VBIT_LOCATION
2113 == ptrmemfunc_vbit_in_delta),
2114 NULL);
2115 if (rec != rec2)
2116 return;
2118 index = ipa_get_param_decl_index (info, rec);
2119 if (index >= 0
2120 && parm_preserved_before_stmt_p (fbi, index, call, rec))
2122 struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index, call);
2123 if (cs->indirect_info->offset != offset)
2124 cs->indirect_info->outer_type = NULL;
2125 cs->indirect_info->offset = offset;
2126 cs->indirect_info->agg_contents = 1;
2127 cs->indirect_info->member_ptr = 1;
2130 return;
2133 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
2134 object referenced in the expression is a formal parameter of the caller
2135 FBI->node (described by FBI->info), create a call note for the
2136 statement. */
2138 static void
2139 ipa_analyze_virtual_call_uses (struct func_body_info *fbi,
2140 gimple call, tree target)
2142 tree obj = OBJ_TYPE_REF_OBJECT (target);
2143 int index;
2144 HOST_WIDE_INT anc_offset;
2146 if (!flag_devirtualize)
2147 return;
2149 if (TREE_CODE (obj) != SSA_NAME)
2150 return;
2152 struct ipa_node_params *info = fbi->info;
2153 if (SSA_NAME_IS_DEFAULT_DEF (obj))
2155 struct ipa_jump_func jfunc;
2156 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
2157 return;
2159 anc_offset = 0;
2160 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
2161 gcc_assert (index >= 0);
2162 if (detect_type_change_ssa (obj, obj_type_ref_class (target),
2163 call, &jfunc))
2164 return;
2166 else
2168 struct ipa_jump_func jfunc;
2169 gimple stmt = SSA_NAME_DEF_STMT (obj);
2170 tree expr;
2172 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
2173 if (!expr)
2174 return;
2175 index = ipa_get_param_decl_index (info,
2176 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
2177 gcc_assert (index >= 0);
2178 if (detect_type_change (obj, expr, obj_type_ref_class (target),
2179 call, &jfunc, anc_offset))
2180 return;
2183 struct cgraph_edge *cs = ipa_note_param_call (fbi->node, index, call);
2184 struct cgraph_indirect_call_info *ii = cs->indirect_info;
2185 ii->offset = anc_offset;
2186 ii->otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
2187 ii->otr_type = obj_type_ref_class (target);
2188 ii->polymorphic = 1;
2191 /* Analyze a call statement CALL whether and how it utilizes formal parameters
2192 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
2193 containing intermediate information about each formal parameter. */
2195 static void
2196 ipa_analyze_call_uses (struct func_body_info *fbi, gimple call)
2198 tree target = gimple_call_fn (call);
2200 if (!target
2201 || (TREE_CODE (target) != SSA_NAME
2202 && !virtual_method_call_p (target)))
2203 return;
2205 /* If we previously turned the call into a direct call, there is
2206 no need to analyze. */
2207 struct cgraph_edge *cs = cgraph_edge (fbi->node, call);
2208 if (cs && !cs->indirect_unknown_callee)
2209 return;
2210 if (TREE_CODE (target) == SSA_NAME)
2211 ipa_analyze_indirect_call_uses (fbi, call, target);
2212 else if (virtual_method_call_p (target))
2213 ipa_analyze_virtual_call_uses (fbi, call, target);
2217 /* Analyze the call statement STMT with respect to formal parameters (described
2218 in INFO) of caller given by FBI->NODE. Currently it only checks whether
2219 formal parameters are called. */
2221 static void
2222 ipa_analyze_stmt_uses (struct func_body_info *fbi, gimple stmt)
2224 if (is_gimple_call (stmt))
2225 ipa_analyze_call_uses (fbi, stmt);
2228 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
2229 If OP is a parameter declaration, mark it as used in the info structure
2230 passed in DATA. */
2232 static bool
2233 visit_ref_for_mod_analysis (gimple, tree op, tree, void *data)
2235 struct ipa_node_params *info = (struct ipa_node_params *) data;
2237 op = get_base_address (op);
2238 if (op
2239 && TREE_CODE (op) == PARM_DECL)
2241 int index = ipa_get_param_decl_index (info, op);
2242 gcc_assert (index >= 0);
2243 ipa_set_param_used (info, index, true);
2246 return false;
2249 /* Scan the statements in BB and inspect the uses of formal parameters. Store
2250 the findings in various structures of the associated ipa_node_params
2251 structure, such as parameter flags, notes etc. FBI holds various data about
2252 the function being analyzed. */
2254 static void
2255 ipa_analyze_params_uses_in_bb (struct func_body_info *fbi, basic_block bb)
2257 gimple_stmt_iterator gsi;
2258 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2260 gimple stmt = gsi_stmt (gsi);
2262 if (is_gimple_debug (stmt))
2263 continue;
2265 ipa_analyze_stmt_uses (fbi, stmt);
2266 walk_stmt_load_store_addr_ops (stmt, fbi->info,
2267 visit_ref_for_mod_analysis,
2268 visit_ref_for_mod_analysis,
2269 visit_ref_for_mod_analysis);
2271 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2272 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), fbi->info,
2273 visit_ref_for_mod_analysis,
2274 visit_ref_for_mod_analysis,
2275 visit_ref_for_mod_analysis);
2278 /* Calculate controlled uses of parameters of NODE. */
2280 static void
2281 ipa_analyze_controlled_uses (struct cgraph_node *node)
2283 struct ipa_node_params *info = IPA_NODE_REF (node);
2285 for (int i = 0; i < ipa_get_param_count (info); i++)
2287 tree parm = ipa_get_param (info, i);
2288 int controlled_uses = 0;
2290 /* For SSA regs see if parameter is used. For non-SSA we compute
2291 the flag during modification analysis. */
2292 if (is_gimple_reg (parm))
2294 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
2295 parm);
2296 if (ddef && !has_zero_uses (ddef))
2298 imm_use_iterator imm_iter;
2299 use_operand_p use_p;
2301 ipa_set_param_used (info, i, true);
2302 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
2303 if (!is_gimple_call (USE_STMT (use_p)))
2305 if (!is_gimple_debug (USE_STMT (use_p)))
2307 controlled_uses = IPA_UNDESCRIBED_USE;
2308 break;
2311 else
2312 controlled_uses++;
2314 else
2315 controlled_uses = 0;
2317 else
2318 controlled_uses = IPA_UNDESCRIBED_USE;
2319 ipa_set_controlled_uses (info, i, controlled_uses);
2323 /* Free stuff in BI. */
2325 static void
2326 free_ipa_bb_info (struct ipa_bb_info *bi)
2328 bi->cg_edges.release ();
2329 bi->param_aa_statuses.release ();
2332 /* Dominator walker driving the analysis. */
2334 class analysis_dom_walker : public dom_walker
2336 public:
2337 analysis_dom_walker (struct func_body_info *fbi)
2338 : dom_walker (CDI_DOMINATORS), m_fbi (fbi) {}
2340 virtual void before_dom_children (basic_block);
2342 private:
2343 struct func_body_info *m_fbi;
2346 void
2347 analysis_dom_walker::before_dom_children (basic_block bb)
2349 ipa_analyze_params_uses_in_bb (m_fbi, bb);
2350 ipa_compute_jump_functions_for_bb (m_fbi, bb);
2353 /* Initialize the array describing properties of of formal parameters
2354 of NODE, analyze their uses and compute jump functions associated
2355 with actual arguments of calls from within NODE. */
2357 void
2358 ipa_analyze_node (struct cgraph_node *node)
2360 struct func_body_info fbi;
2361 struct ipa_node_params *info;
2363 ipa_check_create_node_params ();
2364 ipa_check_create_edge_args ();
2365 info = IPA_NODE_REF (node);
2367 if (info->analysis_done)
2368 return;
2369 info->analysis_done = 1;
2371 if (ipa_func_spec_opts_forbid_analysis_p (node))
2373 for (int i = 0; i < ipa_get_param_count (info); i++)
2375 ipa_set_param_used (info, i, true);
2376 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
2378 return;
2381 struct function *func = DECL_STRUCT_FUNCTION (node->decl);
2382 push_cfun (func);
2383 calculate_dominance_info (CDI_DOMINATORS);
2384 ipa_initialize_node_params (node);
2385 ipa_analyze_controlled_uses (node);
2387 fbi.node = node;
2388 fbi.info = IPA_NODE_REF (node);
2389 fbi.bb_infos = vNULL;
2390 fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun));
2391 fbi.param_count = ipa_get_param_count (info);
2392 fbi.aa_walked = 0;
2394 for (struct cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
2396 ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
2397 bi->cg_edges.safe_push (cs);
2400 for (struct cgraph_edge *cs = node->indirect_calls; cs; cs = cs->next_callee)
2402 ipa_bb_info *bi = ipa_get_bb_info (&fbi, gimple_bb (cs->call_stmt));
2403 bi->cg_edges.safe_push (cs);
2406 analysis_dom_walker (&fbi).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2408 int i;
2409 struct ipa_bb_info *bi;
2410 FOR_EACH_VEC_ELT (fbi.bb_infos, i, bi)
2411 free_ipa_bb_info (bi);
2412 fbi.bb_infos.release ();
2413 free_dominance_info (CDI_DOMINATORS);
2414 pop_cfun ();
2417 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2418 attempt a type-based devirtualization. If successful, return the
2419 target function declaration, otherwise return NULL. */
2421 tree
2422 ipa_intraprocedural_devirtualization (gimple call)
2424 tree binfo, token, fndecl;
2425 struct ipa_jump_func jfunc;
2426 tree otr = gimple_call_fn (call);
2428 jfunc.type = IPA_JF_UNKNOWN;
2429 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2430 call, obj_type_ref_class (otr));
2431 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2432 return NULL_TREE;
2433 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2434 if (!binfo)
2435 return NULL_TREE;
2436 token = OBJ_TYPE_REF_TOKEN (otr);
2437 fndecl = gimple_get_virt_method_for_binfo (tree_to_uhwi (token),
2438 binfo);
2439 #ifdef ENABLE_CHECKING
2440 if (fndecl)
2441 gcc_assert (possible_polymorphic_call_target_p
2442 (otr, cgraph_get_node (fndecl)));
2443 #endif
2444 return fndecl;
2447 /* Update the jump function DST when the call graph edge corresponding to SRC is
2448 is being inlined, knowing that DST is of type ancestor and src of known
2449 type. */
2451 static void
2452 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2453 struct ipa_jump_func *dst)
2455 HOST_WIDE_INT combined_offset;
2456 tree combined_type;
2458 if (!ipa_get_jf_ancestor_type_preserved (dst))
2460 dst->type = IPA_JF_UNKNOWN;
2461 return;
2464 combined_offset = ipa_get_jf_known_type_offset (src)
2465 + ipa_get_jf_ancestor_offset (dst);
2466 combined_type = ipa_get_jf_ancestor_type (dst);
2468 ipa_set_jf_known_type (dst, combined_offset,
2469 ipa_get_jf_known_type_base_type (src),
2470 combined_type);
2473 /* Update the jump functions associated with call graph edge E when the call
2474 graph edge CS is being inlined, assuming that E->caller is already (possibly
2475 indirectly) inlined into CS->callee and that E has not been inlined. */
2477 static void
2478 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2479 struct cgraph_edge *e)
2481 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2482 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2483 int count = ipa_get_cs_argument_count (args);
2484 int i;
2486 for (i = 0; i < count; i++)
2488 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2490 if (dst->type == IPA_JF_ANCESTOR)
2492 struct ipa_jump_func *src;
2493 int dst_fid = dst->value.ancestor.formal_id;
2495 /* Variable number of arguments can cause havoc if we try to access
2496 one that does not exist in the inlined edge. So make sure we
2497 don't. */
2498 if (dst_fid >= ipa_get_cs_argument_count (top))
2500 dst->type = IPA_JF_UNKNOWN;
2501 continue;
2504 src = ipa_get_ith_jump_func (top, dst_fid);
2506 if (src->agg.items
2507 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2509 struct ipa_agg_jf_item *item;
2510 int j;
2512 /* Currently we do not produce clobber aggregate jump functions,
2513 replace with merging when we do. */
2514 gcc_assert (!dst->agg.items);
2516 dst->agg.items = vec_safe_copy (src->agg.items);
2517 dst->agg.by_ref = src->agg.by_ref;
2518 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2519 item->offset -= dst->value.ancestor.offset;
2522 if (src->type == IPA_JF_KNOWN_TYPE)
2523 combine_known_type_and_ancestor_jfs (src, dst);
2524 else if (src->type == IPA_JF_PASS_THROUGH
2525 && src->value.pass_through.operation == NOP_EXPR)
2527 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2528 dst->value.ancestor.agg_preserved &=
2529 src->value.pass_through.agg_preserved;
2530 dst->value.ancestor.type_preserved &=
2531 src->value.pass_through.type_preserved;
2533 else if (src->type == IPA_JF_ANCESTOR)
2535 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2536 dst->value.ancestor.offset += src->value.ancestor.offset;
2537 dst->value.ancestor.agg_preserved &=
2538 src->value.ancestor.agg_preserved;
2539 dst->value.ancestor.type_preserved &=
2540 src->value.ancestor.type_preserved;
2542 else
2543 dst->type = IPA_JF_UNKNOWN;
2545 else if (dst->type == IPA_JF_PASS_THROUGH)
2547 struct ipa_jump_func *src;
2548 /* We must check range due to calls with variable number of arguments
2549 and we cannot combine jump functions with operations. */
2550 if (dst->value.pass_through.operation == NOP_EXPR
2551 && (dst->value.pass_through.formal_id
2552 < ipa_get_cs_argument_count (top)))
2554 int dst_fid = dst->value.pass_through.formal_id;
2555 src = ipa_get_ith_jump_func (top, dst_fid);
2556 bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
2558 switch (src->type)
2560 case IPA_JF_UNKNOWN:
2561 dst->type = IPA_JF_UNKNOWN;
2562 break;
2563 case IPA_JF_KNOWN_TYPE:
2564 if (ipa_get_jf_pass_through_type_preserved (dst))
2565 ipa_set_jf_known_type (dst,
2566 ipa_get_jf_known_type_offset (src),
2567 ipa_get_jf_known_type_base_type (src),
2568 ipa_get_jf_known_type_component_type (src));
2569 else
2570 dst->type = IPA_JF_UNKNOWN;
2571 break;
2572 case IPA_JF_CONST:
2573 ipa_set_jf_cst_copy (dst, src);
2574 break;
2576 case IPA_JF_PASS_THROUGH:
2578 int formal_id = ipa_get_jf_pass_through_formal_id (src);
2579 enum tree_code operation;
2580 operation = ipa_get_jf_pass_through_operation (src);
2582 if (operation == NOP_EXPR)
2584 bool agg_p, type_p;
2585 agg_p = dst_agg_p
2586 && ipa_get_jf_pass_through_agg_preserved (src);
2587 type_p = ipa_get_jf_pass_through_type_preserved (src)
2588 && ipa_get_jf_pass_through_type_preserved (dst);
2589 ipa_set_jf_simple_pass_through (dst, formal_id,
2590 agg_p, type_p);
2592 else
2594 tree operand = ipa_get_jf_pass_through_operand (src);
2595 ipa_set_jf_arith_pass_through (dst, formal_id, operand,
2596 operation);
2598 break;
2600 case IPA_JF_ANCESTOR:
2602 bool agg_p, type_p;
2603 agg_p = dst_agg_p
2604 && ipa_get_jf_ancestor_agg_preserved (src);
2605 type_p = ipa_get_jf_ancestor_type_preserved (src)
2606 && ipa_get_jf_pass_through_type_preserved (dst);
2607 ipa_set_ancestor_jf (dst,
2608 ipa_get_jf_ancestor_offset (src),
2609 ipa_get_jf_ancestor_type (src),
2610 ipa_get_jf_ancestor_formal_id (src),
2611 agg_p, type_p);
2612 break;
2614 default:
2615 gcc_unreachable ();
2618 if (src->agg.items
2619 && (dst_agg_p || !src->agg.by_ref))
2621 /* Currently we do not produce clobber aggregate jump
2622 functions, replace with merging when we do. */
2623 gcc_assert (!dst->agg.items);
2625 dst->agg.by_ref = src->agg.by_ref;
2626 dst->agg.items = vec_safe_copy (src->agg.items);
2629 else
2630 dst->type = IPA_JF_UNKNOWN;
2635 /* If TARGET is an addr_expr of a function declaration, make it the destination
2636 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2638 struct cgraph_edge *
2639 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2641 struct cgraph_node *callee;
2642 struct inline_edge_summary *es = inline_edge_summary (ie);
2643 bool unreachable = false;
2645 if (TREE_CODE (target) == ADDR_EXPR)
2646 target = TREE_OPERAND (target, 0);
2647 if (TREE_CODE (target) != FUNCTION_DECL)
2649 target = canonicalize_constructor_val (target, NULL);
2650 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2652 if (ie->indirect_info->member_ptr)
2653 /* Member pointer call that goes through a VMT lookup. */
2654 return NULL;
2656 if (dump_enabled_p ())
2658 location_t loc = gimple_location (ie->call_stmt);
2659 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
2660 "discovered direct call to non-function in %s/%i, "
2661 "making it __builtin_unreachable\n",
2662 ie->caller->name (),
2663 ie->caller->order);
2665 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2666 callee = cgraph_get_create_node (target);
2667 unreachable = true;
2669 else
2670 callee = cgraph_get_node (target);
2672 else
2673 callee = cgraph_get_node (target);
2675 /* Because may-edges are not explicitely represented and vtable may be external,
2676 we may create the first reference to the object in the unit. */
2677 if (!callee || callee->global.inlined_to)
2680 /* We are better to ensure we can refer to it.
2681 In the case of static functions we are out of luck, since we already
2682 removed its body. In the case of public functions we may or may
2683 not introduce the reference. */
2684 if (!canonicalize_constructor_val (target, NULL)
2685 || !TREE_PUBLIC (target))
2687 if (dump_file)
2688 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2689 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2690 xstrdup (ie->caller->name ()),
2691 ie->caller->order,
2692 xstrdup (ie->callee->name ()),
2693 ie->callee->order);
2694 return NULL;
2696 callee = cgraph_get_create_node (target);
2699 if (!dbg_cnt (devirt))
2700 return NULL;
2702 ipa_check_create_node_params ();
2704 /* We can not make edges to inline clones. It is bug that someone removed
2705 the cgraph node too early. */
2706 gcc_assert (!callee->global.inlined_to);
2708 if (dump_file && !unreachable)
2710 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2711 "(%s/%i -> %s/%i), for stmt ",
2712 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2713 xstrdup (ie->caller->name ()),
2714 ie->caller->order,
2715 xstrdup (callee->name ()),
2716 callee->order);
2717 if (ie->call_stmt)
2718 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2719 else
2720 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2722 if (dump_enabled_p ())
2724 location_t loc = gimple_location (ie->call_stmt);
2725 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
2726 "converting indirect call in %s to direct call to %s\n",
2727 ie->caller->name (), callee->name ());
2729 ie = cgraph_make_edge_direct (ie, callee);
2730 es = inline_edge_summary (ie);
2731 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2732 - eni_size_weights.call_cost);
2733 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2734 - eni_time_weights.call_cost);
2736 return ie;
2739 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2740 return NULL if there is not any. BY_REF specifies whether the value has to
2741 be passed by reference or by value. */
2743 tree
2744 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2745 HOST_WIDE_INT offset, bool by_ref)
2747 struct ipa_agg_jf_item *item;
2748 int i;
2750 if (by_ref != agg->by_ref)
2751 return NULL;
2753 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2754 if (item->offset == offset)
2756 /* Currently we do not have clobber values, return NULL for them once
2757 we do. */
2758 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2759 return item->value;
2761 return NULL;
2764 /* Remove a reference to SYMBOL from the list of references of a node given by
2765 reference description RDESC. Return true if the reference has been
2766 successfully found and removed. */
2768 static bool
2769 remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
2771 struct ipa_ref *to_del;
2772 struct cgraph_edge *origin;
2774 origin = rdesc->cs;
2775 if (!origin)
2776 return false;
2777 to_del = ipa_find_reference (origin->caller, symbol,
2778 origin->call_stmt, origin->lto_stmt_uid);
2779 if (!to_del)
2780 return false;
2782 ipa_remove_reference (to_del);
2783 if (dump_file)
2784 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2785 xstrdup (origin->caller->name ()),
2786 origin->caller->order, xstrdup (symbol->name ()));
2787 return true;
2790 /* If JFUNC has a reference description with refcount different from
2791 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2792 NULL. JFUNC must be a constant jump function. */
2794 static struct ipa_cst_ref_desc *
2795 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2797 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2798 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2799 return rdesc;
2800 else
2801 return NULL;
2804 /* If the value of constant jump function JFUNC is an address of a function
2805 declaration, return the associated call graph node. Otherwise return
2806 NULL. */
2808 static cgraph_node *
2809 cgraph_node_for_jfunc (struct ipa_jump_func *jfunc)
2811 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
2812 tree cst = ipa_get_jf_constant (jfunc);
2813 if (TREE_CODE (cst) != ADDR_EXPR
2814 || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL)
2815 return NULL;
2817 return cgraph_get_node (TREE_OPERAND (cst, 0));
2821 /* If JFUNC is a constant jump function with a usable rdesc, decrement its
2822 refcount and if it hits zero, remove reference to SYMBOL from the caller of
2823 the edge specified in the rdesc. Return false if either the symbol or the
2824 reference could not be found, otherwise return true. */
2826 static bool
2827 try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
2829 struct ipa_cst_ref_desc *rdesc;
2830 if (jfunc->type == IPA_JF_CONST
2831 && (rdesc = jfunc_rdesc_usable (jfunc))
2832 && --rdesc->refcount == 0)
2834 symtab_node *symbol = cgraph_node_for_jfunc (jfunc);
2835 if (!symbol)
2836 return false;
2838 return remove_described_reference (symbol, rdesc);
2840 return true;
2843 /* Try to find a destination for indirect edge IE that corresponds to a simple
2844 call or a call of a member function pointer and where the destination is a
2845 pointer formal parameter described by jump function JFUNC. If it can be
2846 determined, return the newly direct edge, otherwise return NULL.
2847 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2849 static struct cgraph_edge *
2850 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2851 struct ipa_jump_func *jfunc,
2852 struct ipa_node_params *new_root_info)
2854 struct cgraph_edge *cs;
2855 tree target;
2856 bool agg_contents = ie->indirect_info->agg_contents;
2858 if (ie->indirect_info->agg_contents)
2859 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2860 ie->indirect_info->offset,
2861 ie->indirect_info->by_ref);
2862 else
2863 target = ipa_value_from_jfunc (new_root_info, jfunc);
2864 if (!target)
2865 return NULL;
2866 cs = ipa_make_edge_direct_to_target (ie, target);
2868 if (cs && !agg_contents)
2870 bool ok;
2871 gcc_checking_assert (cs->callee
2872 && (cs != ie
2873 || jfunc->type != IPA_JF_CONST
2874 || !cgraph_node_for_jfunc (jfunc)
2875 || cs->callee == cgraph_node_for_jfunc (jfunc)));
2876 ok = try_decrement_rdesc_refcount (jfunc);
2877 gcc_checking_assert (ok);
2880 return cs;
2883 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2884 call based on a formal parameter which is described by jump function JFUNC
2885 and if it can be determined, make it direct and return the direct edge.
2886 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2887 are relative to. */
2889 static struct cgraph_edge *
2890 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2891 struct ipa_jump_func *jfunc,
2892 struct ipa_node_params *new_root_info)
2894 tree binfo, target;
2896 if (!flag_devirtualize)
2897 return NULL;
2899 /* First try to do lookup via known virtual table pointer value. */
2900 if (!ie->indirect_info->by_ref)
2902 tree vtable;
2903 unsigned HOST_WIDE_INT offset;
2904 tree t = ipa_find_agg_cst_for_param (&jfunc->agg,
2905 ie->indirect_info->offset,
2906 true);
2907 if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset))
2909 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
2910 vtable, offset);
2911 if (target)
2913 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
2914 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
2915 || !possible_polymorphic_call_target_p
2916 (ie, cgraph_get_node (target)))
2918 if (dump_file)
2919 fprintf (dump_file,
2920 "Type inconsident devirtualization: %s/%i->%s\n",
2921 ie->caller->name (), ie->caller->order,
2922 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
2923 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2924 cgraph_get_create_node (target);
2926 return ipa_make_edge_direct_to_target (ie, target);
2931 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2933 if (!binfo)
2934 return NULL;
2936 if (TREE_CODE (binfo) != TREE_BINFO)
2938 ipa_polymorphic_call_context context;
2939 vec <cgraph_node *>targets;
2940 bool final;
2942 if (!get_polymorphic_call_info_from_invariant
2943 (&context, binfo, ie->indirect_info->otr_type,
2944 ie->indirect_info->offset))
2945 return NULL;
2946 targets = possible_polymorphic_call_targets
2947 (ie->indirect_info->otr_type,
2948 ie->indirect_info->otr_token,
2949 context, &final);
2950 if (!final || targets.length () > 1)
2951 return NULL;
2952 if (targets.length () == 1)
2953 target = targets[0]->decl;
2954 else
2956 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2957 cgraph_get_create_node (target);
2960 else
2962 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2963 ie->indirect_info->otr_type);
2964 if (binfo)
2965 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2966 binfo);
2967 else
2968 return NULL;
2971 if (target)
2973 #ifdef ENABLE_CHECKING
2974 gcc_assert (possible_polymorphic_call_target_p
2975 (ie, cgraph_get_node (target)));
2976 #endif
2977 return ipa_make_edge_direct_to_target (ie, target);
2979 else
2980 return NULL;
2983 /* Update the param called notes associated with NODE when CS is being inlined,
2984 assuming NODE is (potentially indirectly) inlined into CS->callee.
2985 Moreover, if the callee is discovered to be constant, create a new cgraph
2986 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2987 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2989 static bool
2990 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2991 struct cgraph_node *node,
2992 vec<cgraph_edge_p> *new_edges)
2994 struct ipa_edge_args *top;
2995 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2996 struct ipa_node_params *new_root_info;
2997 bool res = false;
2999 ipa_check_create_edge_args ();
3000 top = IPA_EDGE_REF (cs);
3001 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
3002 ? cs->caller->global.inlined_to
3003 : cs->caller);
3005 for (ie = node->indirect_calls; ie; ie = next_ie)
3007 struct cgraph_indirect_call_info *ici = ie->indirect_info;
3008 struct ipa_jump_func *jfunc;
3009 int param_index;
3011 next_ie = ie->next_callee;
3013 if (ici->param_index == -1)
3014 continue;
3016 /* We must check range due to calls with variable number of arguments: */
3017 if (ici->param_index >= ipa_get_cs_argument_count (top))
3019 ici->param_index = -1;
3020 continue;
3023 param_index = ici->param_index;
3024 jfunc = ipa_get_ith_jump_func (top, param_index);
3026 if (!flag_indirect_inlining)
3027 new_direct_edge = NULL;
3028 else if (ici->polymorphic)
3029 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
3030 new_root_info);
3031 else
3032 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
3033 new_root_info);
3034 /* If speculation was removed, then we need to do nothing. */
3035 if (new_direct_edge && new_direct_edge != ie)
3037 new_direct_edge->indirect_inlining_edge = 1;
3038 top = IPA_EDGE_REF (cs);
3039 res = true;
3041 else if (new_direct_edge)
3043 new_direct_edge->indirect_inlining_edge = 1;
3044 if (new_direct_edge->call_stmt)
3045 new_direct_edge->call_stmt_cannot_inline_p
3046 = !gimple_check_call_matching_types (
3047 new_direct_edge->call_stmt,
3048 new_direct_edge->callee->decl, false);
3049 if (new_edges)
3051 new_edges->safe_push (new_direct_edge);
3052 res = true;
3054 top = IPA_EDGE_REF (cs);
3056 else if (jfunc->type == IPA_JF_PASS_THROUGH
3057 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3059 if ((ici->agg_contents
3060 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
3061 || (ici->polymorphic
3062 && !ipa_get_jf_pass_through_type_preserved (jfunc)))
3063 ici->param_index = -1;
3064 else
3065 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
3067 else if (jfunc->type == IPA_JF_ANCESTOR)
3069 if ((ici->agg_contents
3070 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
3071 || (ici->polymorphic
3072 && !ipa_get_jf_ancestor_type_preserved (jfunc)))
3073 ici->param_index = -1;
3074 else
3076 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
3077 if (ipa_get_jf_ancestor_offset (jfunc))
3078 ici->outer_type = NULL;
3079 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
3082 else
3083 /* Either we can find a destination for this edge now or never. */
3084 ici->param_index = -1;
3087 return res;
3090 /* Recursively traverse subtree of NODE (including node) made of inlined
3091 cgraph_edges when CS has been inlined and invoke
3092 update_indirect_edges_after_inlining on all nodes and
3093 update_jump_functions_after_inlining on all non-inlined edges that lead out
3094 of this subtree. Newly discovered indirect edges will be added to
3095 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
3096 created. */
3098 static bool
3099 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
3100 struct cgraph_node *node,
3101 vec<cgraph_edge_p> *new_edges)
3103 struct cgraph_edge *e;
3104 bool res;
3106 res = update_indirect_edges_after_inlining (cs, node, new_edges);
3108 for (e = node->callees; e; e = e->next_callee)
3109 if (!e->inline_failed)
3110 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
3111 else
3112 update_jump_functions_after_inlining (cs, e);
3113 for (e = node->indirect_calls; e; e = e->next_callee)
3114 update_jump_functions_after_inlining (cs, e);
3116 return res;
3119 /* Combine two controlled uses counts as done during inlining. */
3121 static int
3122 combine_controlled_uses_counters (int c, int d)
3124 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
3125 return IPA_UNDESCRIBED_USE;
3126 else
3127 return c + d - 1;
3130 /* Propagate number of controlled users from CS->caleee to the new root of the
3131 tree of inlined nodes. */
3133 static void
3134 propagate_controlled_uses (struct cgraph_edge *cs)
3136 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
3137 struct cgraph_node *new_root = cs->caller->global.inlined_to
3138 ? cs->caller->global.inlined_to : cs->caller;
3139 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
3140 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
3141 int count, i;
3143 count = MIN (ipa_get_cs_argument_count (args),
3144 ipa_get_param_count (old_root_info));
3145 for (i = 0; i < count; i++)
3147 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
3148 struct ipa_cst_ref_desc *rdesc;
3150 if (jf->type == IPA_JF_PASS_THROUGH)
3152 int src_idx, c, d;
3153 src_idx = ipa_get_jf_pass_through_formal_id (jf);
3154 c = ipa_get_controlled_uses (new_root_info, src_idx);
3155 d = ipa_get_controlled_uses (old_root_info, i);
3157 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
3158 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
3159 c = combine_controlled_uses_counters (c, d);
3160 ipa_set_controlled_uses (new_root_info, src_idx, c);
3161 if (c == 0 && new_root_info->ipcp_orig_node)
3163 struct cgraph_node *n;
3164 struct ipa_ref *ref;
3165 tree t = new_root_info->known_vals[src_idx];
3167 if (t && TREE_CODE (t) == ADDR_EXPR
3168 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
3169 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
3170 && (ref = ipa_find_reference (new_root,
3171 n, NULL, 0)))
3173 if (dump_file)
3174 fprintf (dump_file, "ipa-prop: Removing cloning-created "
3175 "reference from %s/%i to %s/%i.\n",
3176 xstrdup (new_root->name ()),
3177 new_root->order,
3178 xstrdup (n->name ()), n->order);
3179 ipa_remove_reference (ref);
3183 else if (jf->type == IPA_JF_CONST
3184 && (rdesc = jfunc_rdesc_usable (jf)))
3186 int d = ipa_get_controlled_uses (old_root_info, i);
3187 int c = rdesc->refcount;
3188 rdesc->refcount = combine_controlled_uses_counters (c, d);
3189 if (rdesc->refcount == 0)
3191 tree cst = ipa_get_jf_constant (jf);
3192 struct cgraph_node *n;
3193 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
3194 && TREE_CODE (TREE_OPERAND (cst, 0))
3195 == FUNCTION_DECL);
3196 n = cgraph_get_node (TREE_OPERAND (cst, 0));
3197 if (n)
3199 struct cgraph_node *clone;
3200 bool ok;
3201 ok = remove_described_reference (n, rdesc);
3202 gcc_checking_assert (ok);
3204 clone = cs->caller;
3205 while (clone->global.inlined_to
3206 && clone != rdesc->cs->caller
3207 && IPA_NODE_REF (clone)->ipcp_orig_node)
3209 struct ipa_ref *ref;
3210 ref = ipa_find_reference (clone,
3211 n, NULL, 0);
3212 if (ref)
3214 if (dump_file)
3215 fprintf (dump_file, "ipa-prop: Removing "
3216 "cloning-created reference "
3217 "from %s/%i to %s/%i.\n",
3218 xstrdup (clone->name ()),
3219 clone->order,
3220 xstrdup (n->name ()),
3221 n->order);
3222 ipa_remove_reference (ref);
3224 clone = clone->callers->caller;
3231 for (i = ipa_get_param_count (old_root_info);
3232 i < ipa_get_cs_argument_count (args);
3233 i++)
3235 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
3237 if (jf->type == IPA_JF_CONST)
3239 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
3240 if (rdesc)
3241 rdesc->refcount = IPA_UNDESCRIBED_USE;
3243 else if (jf->type == IPA_JF_PASS_THROUGH)
3244 ipa_set_controlled_uses (new_root_info,
3245 jf->value.pass_through.formal_id,
3246 IPA_UNDESCRIBED_USE);
3250 /* Update jump functions and call note functions on inlining the call site CS.
3251 CS is expected to lead to a node already cloned by
3252 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
3253 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
3254 created. */
3256 bool
3257 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
3258 vec<cgraph_edge_p> *new_edges)
3260 bool changed;
3261 /* Do nothing if the preparation phase has not been carried out yet
3262 (i.e. during early inlining). */
3263 if (!ipa_node_params_vector.exists ())
3264 return false;
3265 gcc_assert (ipa_edge_args_vector);
3267 propagate_controlled_uses (cs);
3268 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
3270 return changed;
3273 /* Frees all dynamically allocated structures that the argument info points
3274 to. */
3276 void
3277 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
3279 vec_free (args->jump_functions);
3280 memset (args, 0, sizeof (*args));
3283 /* Free all ipa_edge structures. */
3285 void
3286 ipa_free_all_edge_args (void)
3288 int i;
3289 struct ipa_edge_args *args;
3291 if (!ipa_edge_args_vector)
3292 return;
3294 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
3295 ipa_free_edge_args_substructures (args);
3297 vec_free (ipa_edge_args_vector);
3300 /* Frees all dynamically allocated structures that the param info points
3301 to. */
3303 void
3304 ipa_free_node_params_substructures (struct ipa_node_params *info)
3306 info->descriptors.release ();
3307 free (info->lattices);
3308 /* Lattice values and their sources are deallocated with their alocation
3309 pool. */
3310 info->known_vals.release ();
3311 memset (info, 0, sizeof (*info));
3314 /* Free all ipa_node_params structures. */
3316 void
3317 ipa_free_all_node_params (void)
3319 int i;
3320 struct ipa_node_params *info;
3322 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
3323 ipa_free_node_params_substructures (info);
3325 ipa_node_params_vector.release ();
3328 /* Set the aggregate replacements of NODE to be AGGVALS. */
3330 void
3331 ipa_set_node_agg_value_chain (struct cgraph_node *node,
3332 struct ipa_agg_replacement_value *aggvals)
3334 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
3335 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
3337 (*ipa_node_agg_replacements)[node->uid] = aggvals;
3340 /* Hook that is called by cgraph.c when an edge is removed. */
3342 static void
3343 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
3345 struct ipa_edge_args *args;
3347 /* During IPA-CP updating we can be called on not-yet analyzed clones. */
3348 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
3349 return;
3351 args = IPA_EDGE_REF (cs);
3352 if (args->jump_functions)
3354 struct ipa_jump_func *jf;
3355 int i;
3356 FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
3358 struct ipa_cst_ref_desc *rdesc;
3359 try_decrement_rdesc_refcount (jf);
3360 if (jf->type == IPA_JF_CONST
3361 && (rdesc = ipa_get_jf_constant_rdesc (jf))
3362 && rdesc->cs == cs)
3363 rdesc->cs = NULL;
3367 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
3370 /* Hook that is called by cgraph.c when a node is removed. */
3372 static void
3373 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3375 /* During IPA-CP updating we can be called on not-yet analyze clones. */
3376 if (ipa_node_params_vector.length () > (unsigned)node->uid)
3377 ipa_free_node_params_substructures (IPA_NODE_REF (node));
3378 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
3379 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
3382 /* Hook that is called by cgraph.c when an edge is duplicated. */
3384 static void
3385 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3386 __attribute__((unused)) void *data)
3388 struct ipa_edge_args *old_args, *new_args;
3389 unsigned int i;
3391 ipa_check_create_edge_args ();
3393 old_args = IPA_EDGE_REF (src);
3394 new_args = IPA_EDGE_REF (dst);
3396 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
3398 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
3400 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
3401 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
3403 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
3405 if (src_jf->type == IPA_JF_CONST)
3407 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
3409 if (!src_rdesc)
3410 dst_jf->value.constant.rdesc = NULL;
3411 else if (src->caller == dst->caller)
3413 struct ipa_ref *ref;
3414 symtab_node *n = cgraph_node_for_jfunc (src_jf);
3415 gcc_checking_assert (n);
3416 ref = ipa_find_reference (src->caller, n,
3417 src->call_stmt, src->lto_stmt_uid);
3418 gcc_checking_assert (ref);
3419 ipa_clone_ref (ref, dst->caller, ref->stmt);
3421 gcc_checking_assert (ipa_refdesc_pool);
3422 struct ipa_cst_ref_desc *dst_rdesc
3423 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3424 dst_rdesc->cs = dst;
3425 dst_rdesc->refcount = src_rdesc->refcount;
3426 dst_rdesc->next_duplicate = NULL;
3427 dst_jf->value.constant.rdesc = dst_rdesc;
3429 else if (src_rdesc->cs == src)
3431 struct ipa_cst_ref_desc *dst_rdesc;
3432 gcc_checking_assert (ipa_refdesc_pool);
3433 dst_rdesc
3434 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3435 dst_rdesc->cs = dst;
3436 dst_rdesc->refcount = src_rdesc->refcount;
3437 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
3438 src_rdesc->next_duplicate = dst_rdesc;
3439 dst_jf->value.constant.rdesc = dst_rdesc;
3441 else
3443 struct ipa_cst_ref_desc *dst_rdesc;
3444 /* This can happen during inlining, when a JFUNC can refer to a
3445 reference taken in a function up in the tree of inline clones.
3446 We need to find the duplicate that refers to our tree of
3447 inline clones. */
3449 gcc_assert (dst->caller->global.inlined_to);
3450 for (dst_rdesc = src_rdesc->next_duplicate;
3451 dst_rdesc;
3452 dst_rdesc = dst_rdesc->next_duplicate)
3454 struct cgraph_node *top;
3455 top = dst_rdesc->cs->caller->global.inlined_to
3456 ? dst_rdesc->cs->caller->global.inlined_to
3457 : dst_rdesc->cs->caller;
3458 if (dst->caller->global.inlined_to == top)
3459 break;
3461 gcc_assert (dst_rdesc);
3462 dst_jf->value.constant.rdesc = dst_rdesc;
3468 /* Hook that is called by cgraph.c when a node is duplicated. */
3470 static void
3471 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
3472 ATTRIBUTE_UNUSED void *data)
3474 struct ipa_node_params *old_info, *new_info;
3475 struct ipa_agg_replacement_value *old_av, *new_av;
3477 ipa_check_create_node_params ();
3478 old_info = IPA_NODE_REF (src);
3479 new_info = IPA_NODE_REF (dst);
3481 new_info->descriptors = old_info->descriptors.copy ();
3482 new_info->lattices = NULL;
3483 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
3485 new_info->analysis_done = old_info->analysis_done;
3486 new_info->node_enqueued = old_info->node_enqueued;
3488 old_av = ipa_get_agg_replacements_for_node (src);
3489 if (!old_av)
3490 return;
3492 new_av = NULL;
3493 while (old_av)
3495 struct ipa_agg_replacement_value *v;
3497 v = ggc_alloc<ipa_agg_replacement_value> ();
3498 memcpy (v, old_av, sizeof (*v));
3499 v->next = new_av;
3500 new_av = v;
3501 old_av = old_av->next;
3503 ipa_set_node_agg_value_chain (dst, new_av);
3507 /* Analyze newly added function into callgraph. */
3509 static void
3510 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3512 if (cgraph_function_with_gimple_body_p (node))
3513 ipa_analyze_node (node);
3516 /* Register our cgraph hooks if they are not already there. */
3518 void
3519 ipa_register_cgraph_hooks (void)
3521 if (!edge_removal_hook_holder)
3522 edge_removal_hook_holder =
3523 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
3524 if (!node_removal_hook_holder)
3525 node_removal_hook_holder =
3526 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
3527 if (!edge_duplication_hook_holder)
3528 edge_duplication_hook_holder =
3529 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
3530 if (!node_duplication_hook_holder)
3531 node_duplication_hook_holder =
3532 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
3533 function_insertion_hook_holder =
3534 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
3537 /* Unregister our cgraph hooks if they are not already there. */
3539 static void
3540 ipa_unregister_cgraph_hooks (void)
3542 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3543 edge_removal_hook_holder = NULL;
3544 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3545 node_removal_hook_holder = NULL;
3546 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3547 edge_duplication_hook_holder = NULL;
3548 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3549 node_duplication_hook_holder = NULL;
3550 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3551 function_insertion_hook_holder = NULL;
3554 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3555 longer needed after ipa-cp. */
3557 void
3558 ipa_free_all_structures_after_ipa_cp (void)
3560 if (!optimize)
3562 ipa_free_all_edge_args ();
3563 ipa_free_all_node_params ();
3564 free_alloc_pool (ipcp_sources_pool);
3565 free_alloc_pool (ipcp_values_pool);
3566 free_alloc_pool (ipcp_agg_lattice_pool);
3567 ipa_unregister_cgraph_hooks ();
3568 if (ipa_refdesc_pool)
3569 free_alloc_pool (ipa_refdesc_pool);
3573 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3574 longer needed after indirect inlining. */
3576 void
3577 ipa_free_all_structures_after_iinln (void)
3579 ipa_free_all_edge_args ();
3580 ipa_free_all_node_params ();
3581 ipa_unregister_cgraph_hooks ();
3582 if (ipcp_sources_pool)
3583 free_alloc_pool (ipcp_sources_pool);
3584 if (ipcp_values_pool)
3585 free_alloc_pool (ipcp_values_pool);
3586 if (ipcp_agg_lattice_pool)
3587 free_alloc_pool (ipcp_agg_lattice_pool);
3588 if (ipa_refdesc_pool)
3589 free_alloc_pool (ipa_refdesc_pool);
3592 /* Print ipa_tree_map data structures of all functions in the
3593 callgraph to F. */
3595 void
3596 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3598 int i, count;
3599 struct ipa_node_params *info;
3601 if (!node->definition)
3602 return;
3603 info = IPA_NODE_REF (node);
3604 fprintf (f, " function %s/%i parameter descriptors:\n",
3605 node->name (), node->order);
3606 count = ipa_get_param_count (info);
3607 for (i = 0; i < count; i++)
3609 int c;
3611 fprintf (f, " ");
3612 ipa_dump_param (f, info, i);
3613 if (ipa_is_param_used (info, i))
3614 fprintf (f, " used");
3615 c = ipa_get_controlled_uses (info, i);
3616 if (c == IPA_UNDESCRIBED_USE)
3617 fprintf (f, " undescribed_use");
3618 else
3619 fprintf (f, " controlled_uses=%i", c);
3620 fprintf (f, "\n");
3624 /* Print ipa_tree_map data structures of all functions in the
3625 callgraph to F. */
3627 void
3628 ipa_print_all_params (FILE * f)
3630 struct cgraph_node *node;
3632 fprintf (f, "\nFunction parameters:\n");
3633 FOR_EACH_FUNCTION (node)
3634 ipa_print_node_params (f, node);
3637 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3639 vec<tree>
3640 ipa_get_vector_of_formal_parms (tree fndecl)
3642 vec<tree> args;
3643 int count;
3644 tree parm;
3646 gcc_assert (!flag_wpa);
3647 count = count_formal_params (fndecl);
3648 args.create (count);
3649 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3650 args.quick_push (parm);
3652 return args;
3655 /* Return a heap allocated vector containing types of formal parameters of
3656 function type FNTYPE. */
3658 vec<tree>
3659 ipa_get_vector_of_formal_parm_types (tree fntype)
3661 vec<tree> types;
3662 int count = 0;
3663 tree t;
3665 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3666 count++;
3668 types.create (count);
3669 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3670 types.quick_push (TREE_VALUE (t));
3672 return types;
3675 /* Modify the function declaration FNDECL and its type according to the plan in
3676 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3677 to reflect the actual parameters being modified which are determined by the
3678 base_index field. */
3680 void
3681 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments)
3683 vec<tree> oparms = ipa_get_vector_of_formal_parms (fndecl);
3684 tree orig_type = TREE_TYPE (fndecl);
3685 tree old_arg_types = TYPE_ARG_TYPES (orig_type);
3687 /* The following test is an ugly hack, some functions simply don't have any
3688 arguments in their type. This is probably a bug but well... */
3689 bool care_for_types = (old_arg_types != NULL_TREE);
3690 bool last_parm_void;
3691 vec<tree> otypes;
3692 if (care_for_types)
3694 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3695 == void_type_node);
3696 otypes = ipa_get_vector_of_formal_parm_types (orig_type);
3697 if (last_parm_void)
3698 gcc_assert (oparms.length () + 1 == otypes.length ());
3699 else
3700 gcc_assert (oparms.length () == otypes.length ());
3702 else
3704 last_parm_void = false;
3705 otypes.create (0);
3708 int len = adjustments.length ();
3709 tree *link = &DECL_ARGUMENTS (fndecl);
3710 tree new_arg_types = NULL;
3711 for (int i = 0; i < len; i++)
3713 struct ipa_parm_adjustment *adj;
3714 gcc_assert (link);
3716 adj = &adjustments[i];
3717 tree parm;
3718 if (adj->op == IPA_PARM_OP_NEW)
3719 parm = NULL;
3720 else
3721 parm = oparms[adj->base_index];
3722 adj->base = parm;
3724 if (adj->op == IPA_PARM_OP_COPY)
3726 if (care_for_types)
3727 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3728 new_arg_types);
3729 *link = parm;
3730 link = &DECL_CHAIN (parm);
3732 else if (adj->op != IPA_PARM_OP_REMOVE)
3734 tree new_parm;
3735 tree ptype;
3737 if (adj->by_ref)
3738 ptype = build_pointer_type (adj->type);
3739 else
3741 ptype = adj->type;
3742 if (is_gimple_reg_type (ptype))
3744 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ptype));
3745 if (TYPE_ALIGN (ptype) < malign)
3746 ptype = build_aligned_type (ptype, malign);
3750 if (care_for_types)
3751 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3753 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3754 ptype);
3755 const char *prefix = adj->arg_prefix ? adj->arg_prefix : "SYNTH";
3756 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
3757 DECL_ARTIFICIAL (new_parm) = 1;
3758 DECL_ARG_TYPE (new_parm) = ptype;
3759 DECL_CONTEXT (new_parm) = fndecl;
3760 TREE_USED (new_parm) = 1;
3761 DECL_IGNORED_P (new_parm) = 1;
3762 layout_decl (new_parm, 0);
3764 if (adj->op == IPA_PARM_OP_NEW)
3765 adj->base = NULL;
3766 else
3767 adj->base = parm;
3768 adj->new_decl = new_parm;
3770 *link = new_parm;
3771 link = &DECL_CHAIN (new_parm);
3775 *link = NULL_TREE;
3777 tree new_reversed = NULL;
3778 if (care_for_types)
3780 new_reversed = nreverse (new_arg_types);
3781 if (last_parm_void)
3783 if (new_reversed)
3784 TREE_CHAIN (new_arg_types) = void_list_node;
3785 else
3786 new_reversed = void_list_node;
3790 /* Use copy_node to preserve as much as possible from original type
3791 (debug info, attribute lists etc.)
3792 Exception is METHOD_TYPEs must have THIS argument.
3793 When we are asked to remove it, we need to build new FUNCTION_TYPE
3794 instead. */
3795 tree new_type = NULL;
3796 if (TREE_CODE (orig_type) != METHOD_TYPE
3797 || (adjustments[0].op == IPA_PARM_OP_COPY
3798 && adjustments[0].base_index == 0))
3800 new_type = build_distinct_type_copy (orig_type);
3801 TYPE_ARG_TYPES (new_type) = new_reversed;
3803 else
3805 new_type
3806 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3807 new_reversed));
3808 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3809 DECL_VINDEX (fndecl) = NULL_TREE;
3812 /* When signature changes, we need to clear builtin info. */
3813 if (DECL_BUILT_IN (fndecl))
3815 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3816 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3819 /* This is a new type, not a copy of an old type. Need to reassociate
3820 variants. We can handle everything except the main variant lazily. */
3821 tree t = TYPE_MAIN_VARIANT (orig_type);
3822 if (orig_type != t)
3824 TYPE_MAIN_VARIANT (new_type) = t;
3825 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3826 TYPE_NEXT_VARIANT (t) = new_type;
3828 else
3830 TYPE_MAIN_VARIANT (new_type) = new_type;
3831 TYPE_NEXT_VARIANT (new_type) = NULL;
3834 TREE_TYPE (fndecl) = new_type;
3835 DECL_VIRTUAL_P (fndecl) = 0;
3836 DECL_LANG_SPECIFIC (fndecl) = NULL;
3837 otypes.release ();
3838 oparms.release ();
3841 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3842 If this is a directly recursive call, CS must be NULL. Otherwise it must
3843 contain the corresponding call graph edge. */
3845 void
3846 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3847 ipa_parm_adjustment_vec adjustments)
3849 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3850 vec<tree> vargs;
3851 vec<tree, va_gc> **debug_args = NULL;
3852 gimple new_stmt;
3853 gimple_stmt_iterator gsi, prev_gsi;
3854 tree callee_decl;
3855 int i, len;
3857 len = adjustments.length ();
3858 vargs.create (len);
3859 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
3860 ipa_remove_stmt_references (current_node, stmt);
3862 gsi = gsi_for_stmt (stmt);
3863 prev_gsi = gsi;
3864 gsi_prev (&prev_gsi);
3865 for (i = 0; i < len; i++)
3867 struct ipa_parm_adjustment *adj;
3869 adj = &adjustments[i];
3871 if (adj->op == IPA_PARM_OP_COPY)
3873 tree arg = gimple_call_arg (stmt, adj->base_index);
3875 vargs.quick_push (arg);
3877 else if (adj->op != IPA_PARM_OP_REMOVE)
3879 tree expr, base, off;
3880 location_t loc;
3881 unsigned int deref_align = 0;
3882 bool deref_base = false;
3884 /* We create a new parameter out of the value of the old one, we can
3885 do the following kind of transformations:
3887 - A scalar passed by reference is converted to a scalar passed by
3888 value. (adj->by_ref is false and the type of the original
3889 actual argument is a pointer to a scalar).
3891 - A part of an aggregate is passed instead of the whole aggregate.
3892 The part can be passed either by value or by reference, this is
3893 determined by value of adj->by_ref. Moreover, the code below
3894 handles both situations when the original aggregate is passed by
3895 value (its type is not a pointer) and when it is passed by
3896 reference (it is a pointer to an aggregate).
3898 When the new argument is passed by reference (adj->by_ref is true)
3899 it must be a part of an aggregate and therefore we form it by
3900 simply taking the address of a reference inside the original
3901 aggregate. */
3903 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3904 base = gimple_call_arg (stmt, adj->base_index);
3905 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3906 : EXPR_LOCATION (base);
3908 if (TREE_CODE (base) != ADDR_EXPR
3909 && POINTER_TYPE_P (TREE_TYPE (base)))
3910 off = build_int_cst (adj->alias_ptr_type,
3911 adj->offset / BITS_PER_UNIT);
3912 else
3914 HOST_WIDE_INT base_offset;
3915 tree prev_base;
3916 bool addrof;
3918 if (TREE_CODE (base) == ADDR_EXPR)
3920 base = TREE_OPERAND (base, 0);
3921 addrof = true;
3923 else
3924 addrof = false;
3925 prev_base = base;
3926 base = get_addr_base_and_unit_offset (base, &base_offset);
3927 /* Aggregate arguments can have non-invariant addresses. */
3928 if (!base)
3930 base = build_fold_addr_expr (prev_base);
3931 off = build_int_cst (adj->alias_ptr_type,
3932 adj->offset / BITS_PER_UNIT);
3934 else if (TREE_CODE (base) == MEM_REF)
3936 if (!addrof)
3938 deref_base = true;
3939 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3941 off = build_int_cst (adj->alias_ptr_type,
3942 base_offset
3943 + adj->offset / BITS_PER_UNIT);
3944 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3945 off);
3946 base = TREE_OPERAND (base, 0);
3948 else
3950 off = build_int_cst (adj->alias_ptr_type,
3951 base_offset
3952 + adj->offset / BITS_PER_UNIT);
3953 base = build_fold_addr_expr (base);
3957 if (!adj->by_ref)
3959 tree type = adj->type;
3960 unsigned int align;
3961 unsigned HOST_WIDE_INT misalign;
3963 if (deref_base)
3965 align = deref_align;
3966 misalign = 0;
3968 else
3970 get_pointer_alignment_1 (base, &align, &misalign);
3971 if (TYPE_ALIGN (type) > align)
3972 align = TYPE_ALIGN (type);
3974 misalign += (offset_int::from (off, SIGNED).to_short_addr ()
3975 * BITS_PER_UNIT);
3976 misalign = misalign & (align - 1);
3977 if (misalign != 0)
3978 align = (misalign & -misalign);
3979 if (align < TYPE_ALIGN (type))
3980 type = build_aligned_type (type, align);
3981 base = force_gimple_operand_gsi (&gsi, base,
3982 true, NULL, true, GSI_SAME_STMT);
3983 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3984 /* If expr is not a valid gimple call argument emit
3985 a load into a temporary. */
3986 if (is_gimple_reg_type (TREE_TYPE (expr)))
3988 gimple tem = gimple_build_assign (NULL_TREE, expr);
3989 if (gimple_in_ssa_p (cfun))
3991 gimple_set_vuse (tem, gimple_vuse (stmt));
3992 expr = make_ssa_name (TREE_TYPE (expr), tem);
3994 else
3995 expr = create_tmp_reg (TREE_TYPE (expr), NULL);
3996 gimple_assign_set_lhs (tem, expr);
3997 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
4000 else
4002 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
4003 expr = build_fold_addr_expr (expr);
4004 expr = force_gimple_operand_gsi (&gsi, expr,
4005 true, NULL, true, GSI_SAME_STMT);
4007 vargs.quick_push (expr);
4009 if (adj->op != IPA_PARM_OP_COPY && MAY_HAVE_DEBUG_STMTS)
4011 unsigned int ix;
4012 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
4013 gimple def_temp;
4015 arg = gimple_call_arg (stmt, adj->base_index);
4016 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
4018 if (!fold_convertible_p (TREE_TYPE (origin), arg))
4019 continue;
4020 arg = fold_convert_loc (gimple_location (stmt),
4021 TREE_TYPE (origin), arg);
4023 if (debug_args == NULL)
4024 debug_args = decl_debug_args_insert (callee_decl);
4025 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
4026 if (ddecl == origin)
4028 ddecl = (**debug_args)[ix + 1];
4029 break;
4031 if (ddecl == NULL)
4033 ddecl = make_node (DEBUG_EXPR_DECL);
4034 DECL_ARTIFICIAL (ddecl) = 1;
4035 TREE_TYPE (ddecl) = TREE_TYPE (origin);
4036 DECL_MODE (ddecl) = DECL_MODE (origin);
4038 vec_safe_push (*debug_args, origin);
4039 vec_safe_push (*debug_args, ddecl);
4041 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
4042 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
4046 if (dump_file && (dump_flags & TDF_DETAILS))
4048 fprintf (dump_file, "replacing stmt:");
4049 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
4052 new_stmt = gimple_build_call_vec (callee_decl, vargs);
4053 vargs.release ();
4054 if (gimple_call_lhs (stmt))
4055 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
4057 gimple_set_block (new_stmt, gimple_block (stmt));
4058 if (gimple_has_location (stmt))
4059 gimple_set_location (new_stmt, gimple_location (stmt));
4060 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
4061 gimple_call_copy_flags (new_stmt, stmt);
4062 if (gimple_in_ssa_p (cfun))
4064 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
4065 if (gimple_vdef (stmt))
4067 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
4068 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
4072 if (dump_file && (dump_flags & TDF_DETAILS))
4074 fprintf (dump_file, "with stmt:");
4075 print_gimple_stmt (dump_file, new_stmt, 0, 0);
4076 fprintf (dump_file, "\n");
4078 gsi_replace (&gsi, new_stmt, true);
4079 if (cs)
4080 cgraph_set_call_stmt (cs, new_stmt);
4083 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
4084 gsi_prev (&gsi);
4086 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
4089 /* If the expression *EXPR should be replaced by a reduction of a parameter, do
4090 so. ADJUSTMENTS is a pointer to a vector of adjustments. CONVERT
4091 specifies whether the function should care about type incompatibility the
4092 current and new expressions. If it is false, the function will leave
4093 incompatibility issues to the caller. Return true iff the expression
4094 was modified. */
4096 bool
4097 ipa_modify_expr (tree *expr, bool convert,
4098 ipa_parm_adjustment_vec adjustments)
4100 struct ipa_parm_adjustment *cand
4101 = ipa_get_adjustment_candidate (&expr, &convert, adjustments, false);
4102 if (!cand)
4103 return false;
4105 tree src;
4106 if (cand->by_ref)
4107 src = build_simple_mem_ref (cand->new_decl);
4108 else
4109 src = cand->new_decl;
4111 if (dump_file && (dump_flags & TDF_DETAILS))
4113 fprintf (dump_file, "About to replace expr ");
4114 print_generic_expr (dump_file, *expr, 0);
4115 fprintf (dump_file, " with ");
4116 print_generic_expr (dump_file, src, 0);
4117 fprintf (dump_file, "\n");
4120 if (convert && !useless_type_conversion_p (TREE_TYPE (*expr), cand->type))
4122 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*expr), src);
4123 *expr = vce;
4125 else
4126 *expr = src;
4127 return true;
4130 /* If T is an SSA_NAME, return NULL if it is not a default def or
4131 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
4132 the base variable is always returned, regardless if it is a default
4133 def. Return T if it is not an SSA_NAME. */
4135 static tree
4136 get_ssa_base_param (tree t, bool ignore_default_def)
4138 if (TREE_CODE (t) == SSA_NAME)
4140 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
4141 return SSA_NAME_VAR (t);
4142 else
4143 return NULL_TREE;
4145 return t;
4148 /* Given an expression, return an adjustment entry specifying the
4149 transformation to be done on EXPR. If no suitable adjustment entry
4150 was found, returns NULL.
4152 If IGNORE_DEFAULT_DEF is set, consider SSA_NAMEs which are not a
4153 default def, otherwise bail on them.
4155 If CONVERT is non-NULL, this function will set *CONVERT if the
4156 expression provided is a component reference. ADJUSTMENTS is the
4157 adjustments vector. */
4159 ipa_parm_adjustment *
4160 ipa_get_adjustment_candidate (tree **expr, bool *convert,
4161 ipa_parm_adjustment_vec adjustments,
4162 bool ignore_default_def)
4164 if (TREE_CODE (**expr) == BIT_FIELD_REF
4165 || TREE_CODE (**expr) == IMAGPART_EXPR
4166 || TREE_CODE (**expr) == REALPART_EXPR)
4168 *expr = &TREE_OPERAND (**expr, 0);
4169 if (convert)
4170 *convert = true;
4173 HOST_WIDE_INT offset, size, max_size;
4174 tree base = get_ref_base_and_extent (**expr, &offset, &size, &max_size);
4175 if (!base || size == -1 || max_size == -1)
4176 return NULL;
4178 if (TREE_CODE (base) == MEM_REF)
4180 offset += mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
4181 base = TREE_OPERAND (base, 0);
4184 base = get_ssa_base_param (base, ignore_default_def);
4185 if (!base || TREE_CODE (base) != PARM_DECL)
4186 return NULL;
4188 struct ipa_parm_adjustment *cand = NULL;
4189 unsigned int len = adjustments.length ();
4190 for (unsigned i = 0; i < len; i++)
4192 struct ipa_parm_adjustment *adj = &adjustments[i];
4194 if (adj->base == base
4195 && (adj->offset == offset || adj->op == IPA_PARM_OP_REMOVE))
4197 cand = adj;
4198 break;
4202 if (!cand || cand->op == IPA_PARM_OP_COPY || cand->op == IPA_PARM_OP_REMOVE)
4203 return NULL;
4204 return cand;
4207 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
4209 static bool
4210 index_in_adjustments_multiple_times_p (int base_index,
4211 ipa_parm_adjustment_vec adjustments)
4213 int i, len = adjustments.length ();
4214 bool one = false;
4216 for (i = 0; i < len; i++)
4218 struct ipa_parm_adjustment *adj;
4219 adj = &adjustments[i];
4221 if (adj->base_index == base_index)
4223 if (one)
4224 return true;
4225 else
4226 one = true;
4229 return false;
4233 /* Return adjustments that should have the same effect on function parameters
4234 and call arguments as if they were first changed according to adjustments in
4235 INNER and then by adjustments in OUTER. */
4237 ipa_parm_adjustment_vec
4238 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
4239 ipa_parm_adjustment_vec outer)
4241 int i, outlen = outer.length ();
4242 int inlen = inner.length ();
4243 int removals = 0;
4244 ipa_parm_adjustment_vec adjustments, tmp;
4246 tmp.create (inlen);
4247 for (i = 0; i < inlen; i++)
4249 struct ipa_parm_adjustment *n;
4250 n = &inner[i];
4252 if (n->op == IPA_PARM_OP_REMOVE)
4253 removals++;
4254 else
4256 /* FIXME: Handling of new arguments are not implemented yet. */
4257 gcc_assert (n->op != IPA_PARM_OP_NEW);
4258 tmp.quick_push (*n);
4262 adjustments.create (outlen + removals);
4263 for (i = 0; i < outlen; i++)
4265 struct ipa_parm_adjustment r;
4266 struct ipa_parm_adjustment *out = &outer[i];
4267 struct ipa_parm_adjustment *in = &tmp[out->base_index];
4269 memset (&r, 0, sizeof (r));
4270 gcc_assert (in->op != IPA_PARM_OP_REMOVE);
4271 if (out->op == IPA_PARM_OP_REMOVE)
4273 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
4275 r.op = IPA_PARM_OP_REMOVE;
4276 adjustments.quick_push (r);
4278 continue;
4280 else
4282 /* FIXME: Handling of new arguments are not implemented yet. */
4283 gcc_assert (out->op != IPA_PARM_OP_NEW);
4286 r.base_index = in->base_index;
4287 r.type = out->type;
4289 /* FIXME: Create nonlocal value too. */
4291 if (in->op == IPA_PARM_OP_COPY && out->op == IPA_PARM_OP_COPY)
4292 r.op = IPA_PARM_OP_COPY;
4293 else if (in->op == IPA_PARM_OP_COPY)
4294 r.offset = out->offset;
4295 else if (out->op == IPA_PARM_OP_COPY)
4296 r.offset = in->offset;
4297 else
4298 r.offset = in->offset + out->offset;
4299 adjustments.quick_push (r);
4302 for (i = 0; i < inlen; i++)
4304 struct ipa_parm_adjustment *n = &inner[i];
4306 if (n->op == IPA_PARM_OP_REMOVE)
4307 adjustments.quick_push (*n);
4310 tmp.release ();
4311 return adjustments;
4314 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
4315 friendly way, assuming they are meant to be applied to FNDECL. */
4317 void
4318 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
4319 tree fndecl)
4321 int i, len = adjustments.length ();
4322 bool first = true;
4323 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
4325 fprintf (file, "IPA param adjustments: ");
4326 for (i = 0; i < len; i++)
4328 struct ipa_parm_adjustment *adj;
4329 adj = &adjustments[i];
4331 if (!first)
4332 fprintf (file, " ");
4333 else
4334 first = false;
4336 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
4337 print_generic_expr (file, parms[adj->base_index], 0);
4338 if (adj->base)
4340 fprintf (file, ", base: ");
4341 print_generic_expr (file, adj->base, 0);
4343 if (adj->new_decl)
4345 fprintf (file, ", new_decl: ");
4346 print_generic_expr (file, adj->new_decl, 0);
4348 if (adj->new_ssa_base)
4350 fprintf (file, ", new_ssa_base: ");
4351 print_generic_expr (file, adj->new_ssa_base, 0);
4354 if (adj->op == IPA_PARM_OP_COPY)
4355 fprintf (file, ", copy_param");
4356 else if (adj->op == IPA_PARM_OP_REMOVE)
4357 fprintf (file, ", remove_param");
4358 else
4359 fprintf (file, ", offset %li", (long) adj->offset);
4360 if (adj->by_ref)
4361 fprintf (file, ", by_ref");
4362 print_node_brief (file, ", type: ", adj->type, 0);
4363 fprintf (file, "\n");
4365 parms.release ();
4368 /* Dump the AV linked list. */
4370 void
4371 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
4373 bool comma = false;
4374 fprintf (f, " Aggregate replacements:");
4375 for (; av; av = av->next)
4377 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
4378 av->index, av->offset);
4379 print_generic_expr (f, av->value, 0);
4380 comma = true;
4382 fprintf (f, "\n");
4385 /* Stream out jump function JUMP_FUNC to OB. */
4387 static void
4388 ipa_write_jump_function (struct output_block *ob,
4389 struct ipa_jump_func *jump_func)
4391 struct ipa_agg_jf_item *item;
4392 struct bitpack_d bp;
4393 int i, count;
4395 streamer_write_uhwi (ob, jump_func->type);
4396 switch (jump_func->type)
4398 case IPA_JF_UNKNOWN:
4399 break;
4400 case IPA_JF_KNOWN_TYPE:
4401 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
4402 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
4403 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
4404 break;
4405 case IPA_JF_CONST:
4406 gcc_assert (
4407 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
4408 stream_write_tree (ob, jump_func->value.constant.value, true);
4409 break;
4410 case IPA_JF_PASS_THROUGH:
4411 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
4412 if (jump_func->value.pass_through.operation == NOP_EXPR)
4414 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4415 bp = bitpack_create (ob->main_stream);
4416 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
4417 bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1);
4418 streamer_write_bitpack (&bp);
4420 else
4422 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
4423 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4425 break;
4426 case IPA_JF_ANCESTOR:
4427 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
4428 stream_write_tree (ob, jump_func->value.ancestor.type, true);
4429 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
4430 bp = bitpack_create (ob->main_stream);
4431 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
4432 bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1);
4433 streamer_write_bitpack (&bp);
4434 break;
4437 count = vec_safe_length (jump_func->agg.items);
4438 streamer_write_uhwi (ob, count);
4439 if (count)
4441 bp = bitpack_create (ob->main_stream);
4442 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
4443 streamer_write_bitpack (&bp);
4446 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
4448 streamer_write_uhwi (ob, item->offset);
4449 stream_write_tree (ob, item->value, true);
4453 /* Read in jump function JUMP_FUNC from IB. */
4455 static void
4456 ipa_read_jump_function (struct lto_input_block *ib,
4457 struct ipa_jump_func *jump_func,
4458 struct cgraph_edge *cs,
4459 struct data_in *data_in)
4461 enum jump_func_type jftype;
4462 enum tree_code operation;
4463 int i, count;
4465 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
4466 switch (jftype)
4468 case IPA_JF_UNKNOWN:
4469 jump_func->type = IPA_JF_UNKNOWN;
4470 break;
4471 case IPA_JF_KNOWN_TYPE:
4473 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4474 tree base_type = stream_read_tree (ib, data_in);
4475 tree component_type = stream_read_tree (ib, data_in);
4477 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
4478 break;
4480 case IPA_JF_CONST:
4481 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
4482 break;
4483 case IPA_JF_PASS_THROUGH:
4484 operation = (enum tree_code) streamer_read_uhwi (ib);
4485 if (operation == NOP_EXPR)
4487 int formal_id = streamer_read_uhwi (ib);
4488 struct bitpack_d bp = streamer_read_bitpack (ib);
4489 bool agg_preserved = bp_unpack_value (&bp, 1);
4490 bool type_preserved = bp_unpack_value (&bp, 1);
4491 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved,
4492 type_preserved);
4494 else
4496 tree operand = stream_read_tree (ib, data_in);
4497 int formal_id = streamer_read_uhwi (ib);
4498 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
4499 operation);
4501 break;
4502 case IPA_JF_ANCESTOR:
4504 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4505 tree type = stream_read_tree (ib, data_in);
4506 int formal_id = streamer_read_uhwi (ib);
4507 struct bitpack_d bp = streamer_read_bitpack (ib);
4508 bool agg_preserved = bp_unpack_value (&bp, 1);
4509 bool type_preserved = bp_unpack_value (&bp, 1);
4511 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved,
4512 type_preserved);
4513 break;
4517 count = streamer_read_uhwi (ib);
4518 vec_alloc (jump_func->agg.items, count);
4519 if (count)
4521 struct bitpack_d bp = streamer_read_bitpack (ib);
4522 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
4524 for (i = 0; i < count; i++)
4526 struct ipa_agg_jf_item item;
4527 item.offset = streamer_read_uhwi (ib);
4528 item.value = stream_read_tree (ib, data_in);
4529 jump_func->agg.items->quick_push (item);
4533 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
4534 relevant to indirect inlining to OB. */
4536 static void
4537 ipa_write_indirect_edge_info (struct output_block *ob,
4538 struct cgraph_edge *cs)
4540 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4541 struct bitpack_d bp;
4543 streamer_write_hwi (ob, ii->param_index);
4544 streamer_write_hwi (ob, ii->offset);
4545 bp = bitpack_create (ob->main_stream);
4546 bp_pack_value (&bp, ii->polymorphic, 1);
4547 bp_pack_value (&bp, ii->agg_contents, 1);
4548 bp_pack_value (&bp, ii->member_ptr, 1);
4549 bp_pack_value (&bp, ii->by_ref, 1);
4550 bp_pack_value (&bp, ii->maybe_in_construction, 1);
4551 bp_pack_value (&bp, ii->maybe_derived_type, 1);
4552 streamer_write_bitpack (&bp);
4554 if (ii->polymorphic)
4556 streamer_write_hwi (ob, ii->otr_token);
4557 stream_write_tree (ob, ii->otr_type, true);
4558 stream_write_tree (ob, ii->outer_type, true);
4562 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
4563 relevant to indirect inlining from IB. */
4565 static void
4566 ipa_read_indirect_edge_info (struct lto_input_block *ib,
4567 struct data_in *data_in ATTRIBUTE_UNUSED,
4568 struct cgraph_edge *cs)
4570 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4571 struct bitpack_d bp;
4573 ii->param_index = (int) streamer_read_hwi (ib);
4574 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
4575 bp = streamer_read_bitpack (ib);
4576 ii->polymorphic = bp_unpack_value (&bp, 1);
4577 ii->agg_contents = bp_unpack_value (&bp, 1);
4578 ii->member_ptr = bp_unpack_value (&bp, 1);
4579 ii->by_ref = bp_unpack_value (&bp, 1);
4580 ii->maybe_in_construction = bp_unpack_value (&bp, 1);
4581 ii->maybe_derived_type = bp_unpack_value (&bp, 1);
4582 if (ii->polymorphic)
4584 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
4585 ii->otr_type = stream_read_tree (ib, data_in);
4586 ii->outer_type = stream_read_tree (ib, data_in);
4590 /* Stream out NODE info to OB. */
4592 static void
4593 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
4595 int node_ref;
4596 lto_symtab_encoder_t encoder;
4597 struct ipa_node_params *info = IPA_NODE_REF (node);
4598 int j;
4599 struct cgraph_edge *e;
4600 struct bitpack_d bp;
4602 encoder = ob->decl_state->symtab_node_encoder;
4603 node_ref = lto_symtab_encoder_encode (encoder, node);
4604 streamer_write_uhwi (ob, node_ref);
4606 streamer_write_uhwi (ob, ipa_get_param_count (info));
4607 for (j = 0; j < ipa_get_param_count (info); j++)
4608 streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
4609 bp = bitpack_create (ob->main_stream);
4610 gcc_assert (info->analysis_done
4611 || ipa_get_param_count (info) == 0);
4612 gcc_assert (!info->node_enqueued);
4613 gcc_assert (!info->ipcp_orig_node);
4614 for (j = 0; j < ipa_get_param_count (info); j++)
4615 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
4616 streamer_write_bitpack (&bp);
4617 for (j = 0; j < ipa_get_param_count (info); j++)
4618 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
4619 for (e = node->callees; e; e = e->next_callee)
4621 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4623 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4624 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4625 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4627 for (e = node->indirect_calls; e; e = e->next_callee)
4629 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4631 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4632 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4633 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4634 ipa_write_indirect_edge_info (ob, e);
4638 /* Stream in NODE info from IB. */
4640 static void
4641 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
4642 struct data_in *data_in)
4644 struct ipa_node_params *info = IPA_NODE_REF (node);
4645 int k;
4646 struct cgraph_edge *e;
4647 struct bitpack_d bp;
4649 ipa_alloc_node_params (node, streamer_read_uhwi (ib));
4651 for (k = 0; k < ipa_get_param_count (info); k++)
4652 info->descriptors[k].move_cost = streamer_read_uhwi (ib);
4654 bp = streamer_read_bitpack (ib);
4655 if (ipa_get_param_count (info) != 0)
4656 info->analysis_done = true;
4657 info->node_enqueued = false;
4658 for (k = 0; k < ipa_get_param_count (info); k++)
4659 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
4660 for (k = 0; k < ipa_get_param_count (info); k++)
4661 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
4662 for (e = node->callees; e; e = e->next_callee)
4664 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4665 int count = streamer_read_uhwi (ib);
4667 if (!count)
4668 continue;
4669 vec_safe_grow_cleared (args->jump_functions, count);
4671 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4672 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4673 data_in);
4675 for (e = node->indirect_calls; e; e = e->next_callee)
4677 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4678 int count = streamer_read_uhwi (ib);
4680 if (count)
4682 vec_safe_grow_cleared (args->jump_functions, count);
4683 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4684 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4685 data_in);
4687 ipa_read_indirect_edge_info (ib, data_in, e);
4691 /* Write jump functions for nodes in SET. */
4693 void
4694 ipa_prop_write_jump_functions (void)
4696 struct cgraph_node *node;
4697 struct output_block *ob;
4698 unsigned int count = 0;
4699 lto_symtab_encoder_iterator lsei;
4700 lto_symtab_encoder_t encoder;
4703 if (!ipa_node_params_vector.exists ())
4704 return;
4706 ob = create_output_block (LTO_section_jump_functions);
4707 encoder = ob->decl_state->symtab_node_encoder;
4708 ob->cgraph_node = NULL;
4709 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4710 lsei_next_function_in_partition (&lsei))
4712 node = lsei_cgraph_node (lsei);
4713 if (cgraph_function_with_gimple_body_p (node)
4714 && IPA_NODE_REF (node) != NULL)
4715 count++;
4718 streamer_write_uhwi (ob, count);
4720 /* Process all of the functions. */
4721 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4722 lsei_next_function_in_partition (&lsei))
4724 node = lsei_cgraph_node (lsei);
4725 if (cgraph_function_with_gimple_body_p (node)
4726 && IPA_NODE_REF (node) != NULL)
4727 ipa_write_node_info (ob, node);
4729 streamer_write_char_stream (ob->main_stream, 0);
4730 produce_asm (ob, NULL);
4731 destroy_output_block (ob);
4734 /* Read section in file FILE_DATA of length LEN with data DATA. */
4736 static void
4737 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
4738 size_t len)
4740 const struct lto_function_header *header =
4741 (const struct lto_function_header *) data;
4742 const int cfg_offset = sizeof (struct lto_function_header);
4743 const int main_offset = cfg_offset + header->cfg_size;
4744 const int string_offset = main_offset + header->main_size;
4745 struct data_in *data_in;
4746 struct lto_input_block ib_main;
4747 unsigned int i;
4748 unsigned int count;
4750 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4751 header->main_size);
4753 data_in =
4754 lto_data_in_create (file_data, (const char *) data + string_offset,
4755 header->string_size, vNULL);
4756 count = streamer_read_uhwi (&ib_main);
4758 for (i = 0; i < count; i++)
4760 unsigned int index;
4761 struct cgraph_node *node;
4762 lto_symtab_encoder_t encoder;
4764 index = streamer_read_uhwi (&ib_main);
4765 encoder = file_data->symtab_node_encoder;
4766 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4767 gcc_assert (node->definition);
4768 ipa_read_node_info (&ib_main, node, data_in);
4770 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4771 len);
4772 lto_data_in_delete (data_in);
4775 /* Read ipcp jump functions. */
4777 void
4778 ipa_prop_read_jump_functions (void)
4780 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4781 struct lto_file_decl_data *file_data;
4782 unsigned int j = 0;
4784 ipa_check_create_node_params ();
4785 ipa_check_create_edge_args ();
4786 ipa_register_cgraph_hooks ();
4788 while ((file_data = file_data_vec[j++]))
4790 size_t len;
4791 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4793 if (data)
4794 ipa_prop_read_section (file_data, data, len);
4798 /* After merging units, we can get mismatch in argument counts.
4799 Also decl merging might've rendered parameter lists obsolete.
4800 Also compute called_with_variable_arg info. */
4802 void
4803 ipa_update_after_lto_read (void)
4805 ipa_check_create_node_params ();
4806 ipa_check_create_edge_args ();
4809 void
4810 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4812 int node_ref;
4813 unsigned int count = 0;
4814 lto_symtab_encoder_t encoder;
4815 struct ipa_agg_replacement_value *aggvals, *av;
4817 aggvals = ipa_get_agg_replacements_for_node (node);
4818 encoder = ob->decl_state->symtab_node_encoder;
4819 node_ref = lto_symtab_encoder_encode (encoder, node);
4820 streamer_write_uhwi (ob, node_ref);
4822 for (av = aggvals; av; av = av->next)
4823 count++;
4824 streamer_write_uhwi (ob, count);
4826 for (av = aggvals; av; av = av->next)
4828 struct bitpack_d bp;
4830 streamer_write_uhwi (ob, av->offset);
4831 streamer_write_uhwi (ob, av->index);
4832 stream_write_tree (ob, av->value, true);
4834 bp = bitpack_create (ob->main_stream);
4835 bp_pack_value (&bp, av->by_ref, 1);
4836 streamer_write_bitpack (&bp);
4840 /* Stream in the aggregate value replacement chain for NODE from IB. */
4842 static void
4843 read_agg_replacement_chain (struct lto_input_block *ib,
4844 struct cgraph_node *node,
4845 struct data_in *data_in)
4847 struct ipa_agg_replacement_value *aggvals = NULL;
4848 unsigned int count, i;
4850 count = streamer_read_uhwi (ib);
4851 for (i = 0; i <count; i++)
4853 struct ipa_agg_replacement_value *av;
4854 struct bitpack_d bp;
4856 av = ggc_alloc<ipa_agg_replacement_value> ();
4857 av->offset = streamer_read_uhwi (ib);
4858 av->index = streamer_read_uhwi (ib);
4859 av->value = stream_read_tree (ib, data_in);
4860 bp = streamer_read_bitpack (ib);
4861 av->by_ref = bp_unpack_value (&bp, 1);
4862 av->next = aggvals;
4863 aggvals = av;
4865 ipa_set_node_agg_value_chain (node, aggvals);
4868 /* Write all aggregate replacement for nodes in set. */
4870 void
4871 ipa_prop_write_all_agg_replacement (void)
4873 struct cgraph_node *node;
4874 struct output_block *ob;
4875 unsigned int count = 0;
4876 lto_symtab_encoder_iterator lsei;
4877 lto_symtab_encoder_t encoder;
4879 if (!ipa_node_agg_replacements)
4880 return;
4882 ob = create_output_block (LTO_section_ipcp_transform);
4883 encoder = ob->decl_state->symtab_node_encoder;
4884 ob->cgraph_node = NULL;
4885 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4886 lsei_next_function_in_partition (&lsei))
4888 node = lsei_cgraph_node (lsei);
4889 if (cgraph_function_with_gimple_body_p (node)
4890 && ipa_get_agg_replacements_for_node (node) != NULL)
4891 count++;
4894 streamer_write_uhwi (ob, count);
4896 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4897 lsei_next_function_in_partition (&lsei))
4899 node = lsei_cgraph_node (lsei);
4900 if (cgraph_function_with_gimple_body_p (node)
4901 && ipa_get_agg_replacements_for_node (node) != NULL)
4902 write_agg_replacement_chain (ob, node);
4904 streamer_write_char_stream (ob->main_stream, 0);
4905 produce_asm (ob, NULL);
4906 destroy_output_block (ob);
4909 /* Read replacements section in file FILE_DATA of length LEN with data
4910 DATA. */
4912 static void
4913 read_replacements_section (struct lto_file_decl_data *file_data,
4914 const char *data,
4915 size_t len)
4917 const struct lto_function_header *header =
4918 (const struct lto_function_header *) data;
4919 const int cfg_offset = sizeof (struct lto_function_header);
4920 const int main_offset = cfg_offset + header->cfg_size;
4921 const int string_offset = main_offset + header->main_size;
4922 struct data_in *data_in;
4923 struct lto_input_block ib_main;
4924 unsigned int i;
4925 unsigned int count;
4927 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4928 header->main_size);
4930 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4931 header->string_size, vNULL);
4932 count = streamer_read_uhwi (&ib_main);
4934 for (i = 0; i < count; i++)
4936 unsigned int index;
4937 struct cgraph_node *node;
4938 lto_symtab_encoder_t encoder;
4940 index = streamer_read_uhwi (&ib_main);
4941 encoder = file_data->symtab_node_encoder;
4942 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4943 gcc_assert (node->definition);
4944 read_agg_replacement_chain (&ib_main, node, data_in);
4946 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4947 len);
4948 lto_data_in_delete (data_in);
4951 /* Read IPA-CP aggregate replacements. */
4953 void
4954 ipa_prop_read_all_agg_replacement (void)
4956 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4957 struct lto_file_decl_data *file_data;
4958 unsigned int j = 0;
4960 while ((file_data = file_data_vec[j++]))
4962 size_t len;
4963 const char *data = lto_get_section_data (file_data,
4964 LTO_section_ipcp_transform,
4965 NULL, &len);
4966 if (data)
4967 read_replacements_section (file_data, data, len);
4971 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4972 NODE. */
4974 static void
4975 adjust_agg_replacement_values (struct cgraph_node *node,
4976 struct ipa_agg_replacement_value *aggval)
4978 struct ipa_agg_replacement_value *v;
4979 int i, c = 0, d = 0, *adj;
4981 if (!node->clone.combined_args_to_skip)
4982 return;
4984 for (v = aggval; v; v = v->next)
4986 gcc_assert (v->index >= 0);
4987 if (c < v->index)
4988 c = v->index;
4990 c++;
4992 adj = XALLOCAVEC (int, c);
4993 for (i = 0; i < c; i++)
4994 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4996 adj[i] = -1;
4997 d++;
4999 else
5000 adj[i] = i - d;
5002 for (v = aggval; v; v = v->next)
5003 v->index = adj[v->index];
5006 /* Dominator walker driving the ipcp modification phase. */
5008 class ipcp_modif_dom_walker : public dom_walker
5010 public:
5011 ipcp_modif_dom_walker (struct func_body_info *fbi,
5012 vec<ipa_param_descriptor> descs,
5013 struct ipa_agg_replacement_value *av,
5014 bool *sc, bool *cc)
5015 : dom_walker (CDI_DOMINATORS), m_fbi (fbi), m_descriptors (descs),
5016 m_aggval (av), m_something_changed (sc), m_cfg_changed (cc) {}
5018 virtual void before_dom_children (basic_block);
5020 private:
5021 struct func_body_info *m_fbi;
5022 vec<ipa_param_descriptor> m_descriptors;
5023 struct ipa_agg_replacement_value *m_aggval;
5024 bool *m_something_changed, *m_cfg_changed;
5027 void
5028 ipcp_modif_dom_walker::before_dom_children (basic_block bb)
5030 gimple_stmt_iterator gsi;
5031 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5033 struct ipa_agg_replacement_value *v;
5034 gimple stmt = gsi_stmt (gsi);
5035 tree rhs, val, t;
5036 HOST_WIDE_INT offset, size;
5037 int index;
5038 bool by_ref, vce;
5040 if (!gimple_assign_load_p (stmt))
5041 continue;
5042 rhs = gimple_assign_rhs1 (stmt);
5043 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
5044 continue;
5046 vce = false;
5047 t = rhs;
5048 while (handled_component_p (t))
5050 /* V_C_E can do things like convert an array of integers to one
5051 bigger integer and similar things we do not handle below. */
5052 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
5054 vce = true;
5055 break;
5057 t = TREE_OPERAND (t, 0);
5059 if (vce)
5060 continue;
5062 if (!ipa_load_from_parm_agg_1 (m_fbi, m_descriptors, stmt, rhs, &index,
5063 &offset, &size, &by_ref))
5064 continue;
5065 for (v = m_aggval; v; v = v->next)
5066 if (v->index == index
5067 && v->offset == offset)
5068 break;
5069 if (!v
5070 || v->by_ref != by_ref
5071 || tree_to_shwi (TYPE_SIZE (TREE_TYPE (v->value))) != size)
5072 continue;
5074 gcc_checking_assert (is_gimple_ip_invariant (v->value));
5075 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
5077 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
5078 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
5079 else if (TYPE_SIZE (TREE_TYPE (rhs))
5080 == TYPE_SIZE (TREE_TYPE (v->value)))
5081 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
5082 else
5084 if (dump_file)
5086 fprintf (dump_file, " const ");
5087 print_generic_expr (dump_file, v->value, 0);
5088 fprintf (dump_file, " can't be converted to type of ");
5089 print_generic_expr (dump_file, rhs, 0);
5090 fprintf (dump_file, "\n");
5092 continue;
5095 else
5096 val = v->value;
5098 if (dump_file && (dump_flags & TDF_DETAILS))
5100 fprintf (dump_file, "Modifying stmt:\n ");
5101 print_gimple_stmt (dump_file, stmt, 0, 0);
5103 gimple_assign_set_rhs_from_tree (&gsi, val);
5104 update_stmt (stmt);
5106 if (dump_file && (dump_flags & TDF_DETAILS))
5108 fprintf (dump_file, "into:\n ");
5109 print_gimple_stmt (dump_file, stmt, 0, 0);
5110 fprintf (dump_file, "\n");
5113 *m_something_changed = true;
5114 if (maybe_clean_eh_stmt (stmt)
5115 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
5116 *m_cfg_changed = true;
5121 /* IPCP transformation phase doing propagation of aggregate values. */
5123 unsigned int
5124 ipcp_transform_function (struct cgraph_node *node)
5126 vec<ipa_param_descriptor> descriptors = vNULL;
5127 struct func_body_info fbi;
5128 struct ipa_agg_replacement_value *aggval;
5129 int param_count;
5130 bool cfg_changed = false, something_changed = false;
5132 gcc_checking_assert (cfun);
5133 gcc_checking_assert (current_function_decl);
5135 if (dump_file)
5136 fprintf (dump_file, "Modification phase of node %s/%i\n",
5137 node->name (), node->order);
5139 aggval = ipa_get_agg_replacements_for_node (node);
5140 if (!aggval)
5141 return 0;
5142 param_count = count_formal_params (node->decl);
5143 if (param_count == 0)
5144 return 0;
5145 adjust_agg_replacement_values (node, aggval);
5146 if (dump_file)
5147 ipa_dump_agg_replacement_values (dump_file, aggval);
5149 fbi.node = node;
5150 fbi.info = NULL;
5151 fbi.bb_infos = vNULL;
5152 fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun));
5153 fbi.param_count = param_count;
5154 fbi.aa_walked = 0;
5156 descriptors.safe_grow_cleared (param_count);
5157 ipa_populate_param_decls (node, descriptors);
5158 calculate_dominance_info (CDI_DOMINATORS);
5159 ipcp_modif_dom_walker (&fbi, descriptors, aggval, &something_changed,
5160 &cfg_changed).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
5162 int i;
5163 struct ipa_bb_info *bi;
5164 FOR_EACH_VEC_ELT (fbi.bb_infos, i, bi)
5165 free_ipa_bb_info (bi);
5166 fbi.bb_infos.release ();
5167 free_dominance_info (CDI_DOMINATORS);
5168 (*ipa_node_agg_replacements)[node->uid] = NULL;
5169 descriptors.release ();
5171 if (!something_changed)
5172 return 0;
5173 else if (cfg_changed)
5174 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
5175 else
5176 return TODO_update_ssa_only_virtuals;