gcc-defs.exp (dg-additional-files-options): Extend regsub for dg-additional-files...
[official-gcc.git] / gcc / ipa-prop.c
blob07af67724f5df71c5a04bf4a48aab4a3d723ce0c
1 /* Interprocedural analyses.
2 Copyright (C) 2005-2013 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 "gimple.h"
25 #include "langhooks.h"
26 #include "ggc.h"
27 #include "target.h"
28 #include "ipa-prop.h"
29 #include "bitmap.h"
30 #include "gimple-ssa.h"
31 #include "tree-cfg.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-into-ssa.h"
35 #include "tree-dfa.h"
36 #include "tree-pass.h"
37 #include "tree-inline.h"
38 #include "ipa-inline.h"
39 #include "flags.h"
40 #include "diagnostic.h"
41 #include "gimple-pretty-print.h"
42 #include "lto-streamer.h"
43 #include "data-streamer.h"
44 #include "tree-streamer.h"
45 #include "params.h"
46 #include "ipa-utils.h"
48 /* Intermediate information about a parameter that is only useful during the
49 run of ipa_analyze_node and is not kept afterwards. */
51 struct param_analysis_info
53 bool parm_modified, ref_modified, pt_modified;
54 bitmap parm_visited_statements, pt_visited_statements;
57 /* Vector where the parameter infos are actually stored. */
58 vec<ipa_node_params_t> ipa_node_params_vector;
59 /* Vector of known aggregate values in cloned nodes. */
60 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
61 /* Vector where the parameter infos are actually stored. */
62 vec<ipa_edge_args_t, va_gc> *ipa_edge_args_vector;
64 /* Holders of ipa cgraph hooks: */
65 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
66 static struct cgraph_node_hook_list *node_removal_hook_holder;
67 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
68 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
69 static struct cgraph_node_hook_list *function_insertion_hook_holder;
71 /* Description of a reference to an IPA constant. */
72 struct ipa_cst_ref_desc
74 /* Edge that corresponds to the statement which took the reference. */
75 struct cgraph_edge *cs;
76 /* Linked list of duplicates created when call graph edges are cloned. */
77 struct ipa_cst_ref_desc *next_duplicate;
78 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
79 if out of control. */
80 int refcount;
83 /* Allocation pool for reference descriptions. */
85 static alloc_pool ipa_refdesc_pool;
87 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
88 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
90 static bool
91 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
93 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
94 struct cl_optimization *os;
96 if (!fs_opts)
97 return false;
98 os = TREE_OPTIMIZATION (fs_opts);
99 return !os->x_optimize || !os->x_flag_ipa_cp;
102 /* Return index of the formal whose tree is PTREE in function which corresponds
103 to INFO. */
105 static int
106 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor_t> descriptors, tree ptree)
108 int i, count;
110 count = descriptors.length ();
111 for (i = 0; i < count; i++)
112 if (descriptors[i].decl == ptree)
113 return i;
115 return -1;
118 /* Return index of the formal whose tree is PTREE in function which corresponds
119 to INFO. */
122 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
124 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
127 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
128 NODE. */
130 static void
131 ipa_populate_param_decls (struct cgraph_node *node,
132 vec<ipa_param_descriptor_t> &descriptors)
134 tree fndecl;
135 tree fnargs;
136 tree parm;
137 int param_num;
139 fndecl = node->decl;
140 gcc_assert (gimple_has_body_p (fndecl));
141 fnargs = DECL_ARGUMENTS (fndecl);
142 param_num = 0;
143 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
145 descriptors[param_num].decl = parm;
146 descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm));
147 param_num++;
151 /* Return how many formal parameters FNDECL has. */
153 static inline int
154 count_formal_params (tree fndecl)
156 tree parm;
157 int count = 0;
158 gcc_assert (gimple_has_body_p (fndecl));
160 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
161 count++;
163 return count;
166 /* Return the declaration of Ith formal parameter of the function corresponding
167 to INFO. Note there is no setter function as this array is built just once
168 using ipa_initialize_node_params. */
170 void
171 ipa_dump_param (FILE *file, struct ipa_node_params *info, int i)
173 fprintf (file, "param #%i", i);
174 if (info->descriptors[i].decl)
176 fprintf (file, " ");
177 print_generic_expr (file, info->descriptors[i].decl, 0);
181 /* Initialize the ipa_node_params structure associated with NODE
182 to hold PARAM_COUNT parameters. */
184 void
185 ipa_alloc_node_params (struct cgraph_node *node, int param_count)
187 struct ipa_node_params *info = IPA_NODE_REF (node);
189 if (!info->descriptors.exists () && param_count)
190 info->descriptors.safe_grow_cleared (param_count);
193 /* Initialize the ipa_node_params structure associated with NODE by counting
194 the function parameters, creating the descriptors and populating their
195 param_decls. */
197 void
198 ipa_initialize_node_params (struct cgraph_node *node)
200 struct ipa_node_params *info = IPA_NODE_REF (node);
202 if (!info->descriptors.exists ())
204 ipa_alloc_node_params (node, count_formal_params (node->decl));
205 ipa_populate_param_decls (node, info->descriptors);
209 /* Print the jump functions associated with call graph edge CS to file F. */
211 static void
212 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
214 int i, count;
216 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
217 for (i = 0; i < count; i++)
219 struct ipa_jump_func *jump_func;
220 enum jump_func_type type;
222 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
223 type = jump_func->type;
225 fprintf (f, " param %d: ", i);
226 if (type == IPA_JF_UNKNOWN)
227 fprintf (f, "UNKNOWN\n");
228 else if (type == IPA_JF_KNOWN_TYPE)
230 fprintf (f, "KNOWN TYPE: base ");
231 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
232 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
233 jump_func->value.known_type.offset);
234 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
235 fprintf (f, "\n");
237 else if (type == IPA_JF_CONST)
239 tree val = jump_func->value.constant.value;
240 fprintf (f, "CONST: ");
241 print_generic_expr (f, val, 0);
242 if (TREE_CODE (val) == ADDR_EXPR
243 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
245 fprintf (f, " -> ");
246 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
249 fprintf (f, "\n");
251 else if (type == IPA_JF_PASS_THROUGH)
253 fprintf (f, "PASS THROUGH: ");
254 fprintf (f, "%d, op %s",
255 jump_func->value.pass_through.formal_id,
256 get_tree_code_name(jump_func->value.pass_through.operation));
257 if (jump_func->value.pass_through.operation != NOP_EXPR)
259 fprintf (f, " ");
260 print_generic_expr (f,
261 jump_func->value.pass_through.operand, 0);
263 if (jump_func->value.pass_through.agg_preserved)
264 fprintf (f, ", agg_preserved");
265 if (jump_func->value.pass_through.type_preserved)
266 fprintf (f, ", type_preserved");
267 fprintf (f, "\n");
269 else if (type == IPA_JF_ANCESTOR)
271 fprintf (f, "ANCESTOR: ");
272 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
273 jump_func->value.ancestor.formal_id,
274 jump_func->value.ancestor.offset);
275 print_generic_expr (f, jump_func->value.ancestor.type, 0);
276 if (jump_func->value.ancestor.agg_preserved)
277 fprintf (f, ", agg_preserved");
278 if (jump_func->value.ancestor.type_preserved)
279 fprintf (f, ", type_preserved");
280 fprintf (f, "\n");
283 if (jump_func->agg.items)
285 struct ipa_agg_jf_item *item;
286 int j;
288 fprintf (f, " Aggregate passed by %s:\n",
289 jump_func->agg.by_ref ? "reference" : "value");
290 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
292 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
293 item->offset);
294 if (TYPE_P (item->value))
295 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
296 tree_low_cst (TYPE_SIZE (item->value), 1));
297 else
299 fprintf (f, "cst: ");
300 print_generic_expr (f, item->value, 0);
302 fprintf (f, "\n");
309 /* Print the jump functions of all arguments on all call graph edges going from
310 NODE to file F. */
312 void
313 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
315 struct cgraph_edge *cs;
317 fprintf (f, " Jump functions of caller %s/%i:\n", cgraph_node_name (node),
318 node->order);
319 for (cs = node->callees; cs; cs = cs->next_callee)
321 if (!ipa_edge_args_info_available_for_edge_p (cs))
322 continue;
324 fprintf (f, " callsite %s/%i -> %s/%i : \n",
325 xstrdup (cgraph_node_name (node)), node->order,
326 xstrdup (cgraph_node_name (cs->callee)),
327 cs->callee->order);
328 ipa_print_node_jump_functions_for_edge (f, cs);
331 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
333 struct cgraph_indirect_call_info *ii;
334 if (!ipa_edge_args_info_available_for_edge_p (cs))
335 continue;
337 ii = cs->indirect_info;
338 if (ii->agg_contents)
339 fprintf (f, " indirect %s callsite, calling param %i, "
340 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
341 ii->member_ptr ? "member ptr" : "aggregate",
342 ii->param_index, ii->offset,
343 ii->by_ref ? "by reference" : "by_value");
344 else
345 fprintf (f, " indirect %s callsite, calling param %i",
346 ii->polymorphic ? "polymorphic" : "simple", ii->param_index);
348 if (cs->call_stmt)
350 fprintf (f, ", for stmt ");
351 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
353 else
354 fprintf (f, "\n");
355 ipa_print_node_jump_functions_for_edge (f, cs);
359 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
361 void
362 ipa_print_all_jump_functions (FILE *f)
364 struct cgraph_node *node;
366 fprintf (f, "\nJump functions:\n");
367 FOR_EACH_FUNCTION (node)
369 ipa_print_node_jump_functions (f, node);
373 /* Set JFUNC to be a known type jump function. */
375 static void
376 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
377 tree base_type, tree component_type)
379 gcc_assert (TREE_CODE (component_type) == RECORD_TYPE
380 && TYPE_BINFO (component_type));
381 jfunc->type = IPA_JF_KNOWN_TYPE;
382 jfunc->value.known_type.offset = offset,
383 jfunc->value.known_type.base_type = base_type;
384 jfunc->value.known_type.component_type = component_type;
387 /* Set JFUNC to be a copy of another jmp (to be used by jump function
388 combination code). The two functions will share their rdesc. */
390 static void
391 ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
392 struct ipa_jump_func *src)
395 gcc_checking_assert (src->type == IPA_JF_CONST);
396 dst->type = IPA_JF_CONST;
397 dst->value.constant = src->value.constant;
400 /* Set JFUNC to be a constant jmp function. */
402 static void
403 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
404 struct cgraph_edge *cs)
406 constant = unshare_expr (constant);
407 if (constant && EXPR_P (constant))
408 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
409 jfunc->type = IPA_JF_CONST;
410 jfunc->value.constant.value = unshare_expr_without_location (constant);
412 if (TREE_CODE (constant) == ADDR_EXPR
413 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
415 struct ipa_cst_ref_desc *rdesc;
416 if (!ipa_refdesc_pool)
417 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
418 sizeof (struct ipa_cst_ref_desc), 32);
420 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
421 rdesc->cs = cs;
422 rdesc->next_duplicate = NULL;
423 rdesc->refcount = 1;
424 jfunc->value.constant.rdesc = rdesc;
426 else
427 jfunc->value.constant.rdesc = NULL;
430 /* Set JFUNC to be a simple pass-through jump function. */
431 static void
432 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
433 bool agg_preserved, bool type_preserved)
435 jfunc->type = IPA_JF_PASS_THROUGH;
436 jfunc->value.pass_through.operand = NULL_TREE;
437 jfunc->value.pass_through.formal_id = formal_id;
438 jfunc->value.pass_through.operation = NOP_EXPR;
439 jfunc->value.pass_through.agg_preserved = agg_preserved;
440 jfunc->value.pass_through.type_preserved = type_preserved;
443 /* Set JFUNC to be an arithmetic pass through jump function. */
445 static void
446 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
447 tree operand, enum tree_code operation)
449 jfunc->type = IPA_JF_PASS_THROUGH;
450 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
451 jfunc->value.pass_through.formal_id = formal_id;
452 jfunc->value.pass_through.operation = operation;
453 jfunc->value.pass_through.agg_preserved = false;
454 jfunc->value.pass_through.type_preserved = false;
457 /* Set JFUNC to be an ancestor jump function. */
459 static void
460 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
461 tree type, int formal_id, bool agg_preserved,
462 bool type_preserved)
464 jfunc->type = IPA_JF_ANCESTOR;
465 jfunc->value.ancestor.formal_id = formal_id;
466 jfunc->value.ancestor.offset = offset;
467 jfunc->value.ancestor.type = type;
468 jfunc->value.ancestor.agg_preserved = agg_preserved;
469 jfunc->value.ancestor.type_preserved = type_preserved;
472 /* Extract the acual BINFO being described by JFUNC which must be a known type
473 jump function. */
475 tree
476 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
478 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
479 if (!base_binfo)
480 return NULL_TREE;
481 return get_binfo_at_offset (base_binfo,
482 jfunc->value.known_type.offset,
483 jfunc->value.known_type.component_type);
486 /* Structure to be passed in between detect_type_change and
487 check_stmt_for_type_change. */
489 struct type_change_info
491 /* Offset into the object where there is the virtual method pointer we are
492 looking for. */
493 HOST_WIDE_INT offset;
494 /* The declaration or SSA_NAME pointer of the base that we are checking for
495 type change. */
496 tree object;
497 /* If we actually can tell the type that the object has changed to, it is
498 stored in this field. Otherwise it remains NULL_TREE. */
499 tree known_current_type;
500 /* Set to true if dynamic type change has been detected. */
501 bool type_maybe_changed;
502 /* Set to true if multiple types have been encountered. known_current_type
503 must be disregarded in that case. */
504 bool multiple_types_encountered;
507 /* Return true if STMT can modify a virtual method table pointer.
509 This function makes special assumptions about both constructors and
510 destructors which are all the functions that are allowed to alter the VMT
511 pointers. It assumes that destructors begin with assignment into all VMT
512 pointers and that constructors essentially look in the following way:
514 1) The very first thing they do is that they call constructors of ancestor
515 sub-objects that have them.
517 2) Then VMT pointers of this and all its ancestors is set to new values
518 corresponding to the type corresponding to the constructor.
520 3) Only afterwards, other stuff such as constructor of member sub-objects
521 and the code written by the user is run. Only this may include calling
522 virtual functions, directly or indirectly.
524 There is no way to call a constructor of an ancestor sub-object in any
525 other way.
527 This means that we do not have to care whether constructors get the correct
528 type information because they will always change it (in fact, if we define
529 the type to be given by the VMT pointer, it is undefined).
531 The most important fact to derive from the above is that if, for some
532 statement in the section 3, we try to detect whether the dynamic type has
533 changed, we can safely ignore all calls as we examine the function body
534 backwards until we reach statements in section 2 because these calls cannot
535 be ancestor constructors or destructors (if the input is not bogus) and so
536 do not change the dynamic type (this holds true only for automatically
537 allocated objects but at the moment we devirtualize only these). We then
538 must detect that statements in section 2 change the dynamic type and can try
539 to derive the new type. That is enough and we can stop, we will never see
540 the calls into constructors of sub-objects in this code. Therefore we can
541 safely ignore all call statements that we traverse.
544 static bool
545 stmt_may_be_vtbl_ptr_store (gimple stmt)
547 if (is_gimple_call (stmt))
548 return false;
549 else if (is_gimple_assign (stmt))
551 tree lhs = gimple_assign_lhs (stmt);
553 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
555 if (flag_strict_aliasing
556 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
557 return false;
559 if (TREE_CODE (lhs) == COMPONENT_REF
560 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
561 return false;
562 /* In the future we might want to use get_base_ref_and_offset to find
563 if there is a field corresponding to the offset and if so, proceed
564 almost like if it was a component ref. */
567 return true;
570 /* If STMT can be proved to be an assignment to the virtual method table
571 pointer of ANALYZED_OBJ and the type associated with the new table
572 identified, return the type. Otherwise return NULL_TREE. */
574 static tree
575 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
577 HOST_WIDE_INT offset, size, max_size;
578 tree lhs, rhs, base;
580 if (!gimple_assign_single_p (stmt))
581 return NULL_TREE;
583 lhs = gimple_assign_lhs (stmt);
584 rhs = gimple_assign_rhs1 (stmt);
585 if (TREE_CODE (lhs) != COMPONENT_REF
586 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))
587 || TREE_CODE (rhs) != ADDR_EXPR)
588 return NULL_TREE;
589 rhs = get_base_address (TREE_OPERAND (rhs, 0));
590 if (!rhs
591 || TREE_CODE (rhs) != VAR_DECL
592 || !DECL_VIRTUAL_P (rhs))
593 return NULL_TREE;
595 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
596 if (offset != tci->offset
597 || size != POINTER_SIZE
598 || max_size != POINTER_SIZE)
599 return NULL_TREE;
600 if (TREE_CODE (base) == MEM_REF)
602 if (TREE_CODE (tci->object) != MEM_REF
603 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
604 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
605 TREE_OPERAND (base, 1)))
606 return NULL_TREE;
608 else if (tci->object != base)
609 return NULL_TREE;
611 return DECL_CONTEXT (rhs);
614 /* Callback of walk_aliased_vdefs and a helper function for
615 detect_type_change to check whether a particular statement may modify
616 the virtual table pointer, and if possible also determine the new type of
617 the (sub-)object. It stores its result into DATA, which points to a
618 type_change_info structure. */
620 static bool
621 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
623 gimple stmt = SSA_NAME_DEF_STMT (vdef);
624 struct type_change_info *tci = (struct type_change_info *) data;
626 if (stmt_may_be_vtbl_ptr_store (stmt))
628 tree type;
629 type = extr_type_from_vtbl_ptr_store (stmt, tci);
630 if (tci->type_maybe_changed
631 && type != tci->known_current_type)
632 tci->multiple_types_encountered = true;
633 tci->known_current_type = type;
634 tci->type_maybe_changed = true;
635 return true;
637 else
638 return false;
643 /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
644 callsite CALL) by looking for assignments to its virtual table pointer. If
645 it is, return true and fill in the jump function JFUNC with relevant type
646 information or set it to unknown. ARG is the object itself (not a pointer
647 to it, unless dereferenced). BASE is the base of the memory access as
648 returned by get_ref_base_and_extent, as is the offset. */
650 static bool
651 detect_type_change (tree arg, tree base, tree comp_type, gimple call,
652 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
654 struct type_change_info tci;
655 ao_ref ao;
657 gcc_checking_assert (DECL_P (arg)
658 || TREE_CODE (arg) == MEM_REF
659 || handled_component_p (arg));
660 /* Const calls cannot call virtual methods through VMT and so type changes do
661 not matter. */
662 if (!flag_devirtualize || !gimple_vuse (call)
663 /* Be sure expected_type is polymorphic. */
664 || !comp_type
665 || TREE_CODE (comp_type) != RECORD_TYPE
666 || !TYPE_BINFO (comp_type)
667 || !BINFO_VTABLE (TYPE_BINFO (comp_type)))
668 return false;
670 ao_ref_init (&ao, arg);
671 ao.base = base;
672 ao.offset = offset;
673 ao.size = POINTER_SIZE;
674 ao.max_size = ao.size;
676 tci.offset = offset;
677 tci.object = get_base_address (arg);
678 tci.known_current_type = NULL_TREE;
679 tci.type_maybe_changed = false;
680 tci.multiple_types_encountered = false;
682 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
683 &tci, NULL);
684 if (!tci.type_maybe_changed)
685 return false;
687 if (!tci.known_current_type
688 || tci.multiple_types_encountered
689 || offset != 0)
690 jfunc->type = IPA_JF_UNKNOWN;
691 else
692 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
694 return true;
697 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
698 SSA name (its dereference will become the base and the offset is assumed to
699 be zero). */
701 static bool
702 detect_type_change_ssa (tree arg, tree comp_type,
703 gimple call, struct ipa_jump_func *jfunc)
705 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
706 if (!flag_devirtualize
707 || !POINTER_TYPE_P (TREE_TYPE (arg)))
708 return false;
710 arg = build2 (MEM_REF, ptr_type_node, arg,
711 build_int_cst (ptr_type_node, 0));
713 return detect_type_change (arg, arg, comp_type, call, jfunc, 0);
716 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
717 boolean variable pointed to by DATA. */
719 static bool
720 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
721 void *data)
723 bool *b = (bool *) data;
724 *b = true;
725 return true;
728 /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
729 a value known not to be modified in this function before reaching the
730 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
731 information about the parameter. */
733 static bool
734 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
735 gimple stmt, tree parm_load)
737 bool modified = false;
738 bitmap *visited_stmts;
739 ao_ref refd;
741 if (parm_ainfo && parm_ainfo->parm_modified)
742 return false;
744 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
745 ao_ref_init (&refd, parm_load);
746 /* We can cache visited statements only when parm_ainfo is available and when
747 we are looking at a naked load of the whole parameter. */
748 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
749 visited_stmts = NULL;
750 else
751 visited_stmts = &parm_ainfo->parm_visited_statements;
752 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
753 visited_stmts);
754 if (parm_ainfo && modified)
755 parm_ainfo->parm_modified = true;
756 return !modified;
759 /* If STMT is an assignment that loads a value from an parameter declaration,
760 return the index of the parameter in ipa_node_params which has not been
761 modified. Otherwise return -1. */
763 static int
764 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
765 struct param_analysis_info *parms_ainfo,
766 gimple stmt)
768 int index;
769 tree op1;
771 if (!gimple_assign_single_p (stmt))
772 return -1;
774 op1 = gimple_assign_rhs1 (stmt);
775 if (TREE_CODE (op1) != PARM_DECL)
776 return -1;
778 index = ipa_get_param_decl_index_1 (descriptors, op1);
779 if (index < 0
780 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
781 : NULL, stmt, op1))
782 return -1;
784 return index;
787 /* Return true if memory reference REF loads data that are known to be
788 unmodified in this function before reaching statement STMT. PARM_AINFO, if
789 non-NULL, is a pointer to a structure containing temporary information about
790 PARM. */
792 static bool
793 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
794 gimple stmt, tree ref)
796 bool modified = false;
797 ao_ref refd;
799 gcc_checking_assert (gimple_vuse (stmt));
800 if (parm_ainfo && parm_ainfo->ref_modified)
801 return false;
803 ao_ref_init (&refd, ref);
804 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
805 NULL);
806 if (parm_ainfo && modified)
807 parm_ainfo->ref_modified = true;
808 return !modified;
811 /* Return true if the data pointed to by PARM is known to be unmodified in this
812 function before reaching call statement CALL into which it is passed.
813 PARM_AINFO is a pointer to a structure containing temporary information
814 about PARM. */
816 static bool
817 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
818 gimple call, tree parm)
820 bool modified = false;
821 ao_ref refd;
823 /* It's unnecessary to calculate anything about memory contnets for a const
824 function because it is not goin to use it. But do not cache the result
825 either. Also, no such calculations for non-pointers. */
826 if (!gimple_vuse (call)
827 || !POINTER_TYPE_P (TREE_TYPE (parm)))
828 return false;
830 if (parm_ainfo->pt_modified)
831 return false;
833 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
834 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
835 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
836 if (modified)
837 parm_ainfo->pt_modified = true;
838 return !modified;
841 /* Return true if we can prove that OP is a memory reference loading unmodified
842 data from an aggregate passed as a parameter and if the aggregate is passed
843 by reference, that the alias type of the load corresponds to the type of the
844 formal parameter (so that we can rely on this type for TBAA in callers).
845 INFO and PARMS_AINFO describe parameters of the current function (but the
846 latter can be NULL), STMT is the load statement. If function returns true,
847 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
848 within the aggregate and whether it is a load from a value passed by
849 reference respectively. */
851 static bool
852 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
853 struct param_analysis_info *parms_ainfo, gimple stmt,
854 tree op, int *index_p, HOST_WIDE_INT *offset_p,
855 HOST_WIDE_INT *size_p, bool *by_ref_p)
857 int index;
858 HOST_WIDE_INT size, max_size;
859 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
861 if (max_size == -1 || max_size != size || *offset_p < 0)
862 return false;
864 if (DECL_P (base))
866 int index = ipa_get_param_decl_index_1 (descriptors, base);
867 if (index >= 0
868 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
869 : NULL, stmt, op))
871 *index_p = index;
872 *by_ref_p = false;
873 if (size_p)
874 *size_p = size;
875 return true;
877 return false;
880 if (TREE_CODE (base) != MEM_REF
881 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
882 || !integer_zerop (TREE_OPERAND (base, 1)))
883 return false;
885 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
887 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
888 index = ipa_get_param_decl_index_1 (descriptors, parm);
890 else
892 /* This branch catches situations where a pointer parameter is not a
893 gimple register, for example:
895 void hip7(S*) (struct S * p)
897 void (*<T2e4>) (struct S *) D.1867;
898 struct S * p.1;
900 <bb 2>:
901 p.1_1 = p;
902 D.1867_2 = p.1_1->f;
903 D.1867_2 ();
904 gdp = &p;
907 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
908 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
911 if (index >= 0
912 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
913 stmt, op))
915 *index_p = index;
916 *by_ref_p = true;
917 if (size_p)
918 *size_p = size;
919 return true;
921 return false;
924 /* Just like the previous function, just without the param_analysis_info
925 pointer, for users outside of this file. */
927 bool
928 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
929 tree op, int *index_p, HOST_WIDE_INT *offset_p,
930 bool *by_ref_p)
932 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
933 offset_p, NULL, by_ref_p);
936 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
937 of an assignment statement STMT, try to determine whether we are actually
938 handling any of the following cases and construct an appropriate jump
939 function into JFUNC if so:
941 1) The passed value is loaded from a formal parameter which is not a gimple
942 register (most probably because it is addressable, the value has to be
943 scalar) and we can guarantee the value has not changed. This case can
944 therefore be described by a simple pass-through jump function. For example:
946 foo (int a)
948 int a.0;
950 a.0_2 = a;
951 bar (a.0_2);
953 2) The passed value can be described by a simple arithmetic pass-through
954 jump function. E.g.
956 foo (int a)
958 int D.2064;
960 D.2064_4 = a.1(D) + 4;
961 bar (D.2064_4);
963 This case can also occur in combination of the previous one, e.g.:
965 foo (int a, int z)
967 int a.0;
968 int D.2064;
970 a.0_3 = a;
971 D.2064_4 = a.0_3 + 4;
972 foo (D.2064_4);
974 3) The passed value is an address of an object within another one (which
975 also passed by reference). Such situations are described by an ancestor
976 jump function and describe situations such as:
978 B::foo() (struct B * const this)
980 struct A * D.1845;
982 D.1845_2 = &this_1(D)->D.1748;
983 A::bar (D.1845_2);
985 INFO is the structure describing individual parameters access different
986 stages of IPA optimizations. PARMS_AINFO contains the information that is
987 only needed for intraprocedural analysis. */
989 static void
990 compute_complex_assign_jump_func (struct ipa_node_params *info,
991 struct param_analysis_info *parms_ainfo,
992 struct ipa_jump_func *jfunc,
993 gimple call, gimple stmt, tree name,
994 tree param_type)
996 HOST_WIDE_INT offset, size, max_size;
997 tree op1, tc_ssa, base, ssa;
998 int index;
1000 op1 = gimple_assign_rhs1 (stmt);
1002 if (TREE_CODE (op1) == SSA_NAME)
1004 if (SSA_NAME_IS_DEFAULT_DEF (op1))
1005 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1006 else
1007 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
1008 SSA_NAME_DEF_STMT (op1));
1009 tc_ssa = op1;
1011 else
1013 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
1014 tc_ssa = gimple_assign_lhs (stmt);
1017 if (index >= 0)
1019 tree op2 = gimple_assign_rhs2 (stmt);
1021 if (op2)
1023 if (!is_gimple_ip_invariant (op2)
1024 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
1025 && !useless_type_conversion_p (TREE_TYPE (name),
1026 TREE_TYPE (op1))))
1027 return;
1029 ipa_set_jf_arith_pass_through (jfunc, index, op2,
1030 gimple_assign_rhs_code (stmt));
1032 else if (gimple_assign_single_p (stmt))
1034 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1035 call, tc_ssa);
1036 bool type_p = false;
1038 if (param_type && POINTER_TYPE_P (param_type))
1039 type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type),
1040 call, jfunc);
1041 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1042 ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p);
1044 return;
1047 if (TREE_CODE (op1) != ADDR_EXPR)
1048 return;
1049 op1 = TREE_OPERAND (op1, 0);
1050 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
1051 return;
1052 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1053 if (TREE_CODE (base) != MEM_REF
1054 /* If this is a varying address, punt. */
1055 || max_size == -1
1056 || max_size != size)
1057 return;
1058 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
1059 ssa = TREE_OPERAND (base, 0);
1060 if (TREE_CODE (ssa) != SSA_NAME
1061 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1062 || offset < 0)
1063 return;
1065 /* Dynamic types are changed in constructors and destructors. */
1066 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1067 if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1069 bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type),
1070 call, jfunc, offset);
1071 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1072 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
1073 parm_ref_data_pass_through_p (&parms_ainfo[index],
1074 call, ssa), type_p);
1078 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1079 it looks like:
1081 iftmp.1_3 = &obj_2(D)->D.1762;
1083 The base of the MEM_REF must be a default definition SSA NAME of a
1084 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1085 whole MEM_REF expression is returned and the offset calculated from any
1086 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1087 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1089 static tree
1090 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1092 HOST_WIDE_INT size, max_size;
1093 tree expr, parm, obj;
1095 if (!gimple_assign_single_p (assign))
1096 return NULL_TREE;
1097 expr = gimple_assign_rhs1 (assign);
1099 if (TREE_CODE (expr) != ADDR_EXPR)
1100 return NULL_TREE;
1101 expr = TREE_OPERAND (expr, 0);
1102 obj = expr;
1103 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1105 if (TREE_CODE (expr) != MEM_REF
1106 /* If this is a varying address, punt. */
1107 || max_size == -1
1108 || max_size != size
1109 || *offset < 0)
1110 return NULL_TREE;
1111 parm = TREE_OPERAND (expr, 0);
1112 if (TREE_CODE (parm) != SSA_NAME
1113 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1114 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1115 return NULL_TREE;
1117 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1118 *obj_p = obj;
1119 return expr;
1123 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1124 statement PHI, try to find out whether NAME is in fact a
1125 multiple-inheritance typecast from a descendant into an ancestor of a formal
1126 parameter and thus can be described by an ancestor jump function and if so,
1127 write the appropriate function into JFUNC.
1129 Essentially we want to match the following pattern:
1131 if (obj_2(D) != 0B)
1132 goto <bb 3>;
1133 else
1134 goto <bb 4>;
1136 <bb 3>:
1137 iftmp.1_3 = &obj_2(D)->D.1762;
1139 <bb 4>:
1140 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1141 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1142 return D.1879_6; */
1144 static void
1145 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1146 struct param_analysis_info *parms_ainfo,
1147 struct ipa_jump_func *jfunc,
1148 gimple call, gimple phi, tree param_type)
1150 HOST_WIDE_INT offset;
1151 gimple assign, cond;
1152 basic_block phi_bb, assign_bb, cond_bb;
1153 tree tmp, parm, expr, obj;
1154 int index, i;
1156 if (gimple_phi_num_args (phi) != 2)
1157 return;
1159 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1160 tmp = PHI_ARG_DEF (phi, 0);
1161 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1162 tmp = PHI_ARG_DEF (phi, 1);
1163 else
1164 return;
1165 if (TREE_CODE (tmp) != SSA_NAME
1166 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1167 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1168 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1169 return;
1171 assign = SSA_NAME_DEF_STMT (tmp);
1172 assign_bb = gimple_bb (assign);
1173 if (!single_pred_p (assign_bb))
1174 return;
1175 expr = get_ancestor_addr_info (assign, &obj, &offset);
1176 if (!expr)
1177 return;
1178 parm = TREE_OPERAND (expr, 0);
1179 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1180 gcc_assert (index >= 0);
1182 cond_bb = single_pred (assign_bb);
1183 cond = last_stmt (cond_bb);
1184 if (!cond
1185 || gimple_code (cond) != GIMPLE_COND
1186 || gimple_cond_code (cond) != NE_EXPR
1187 || gimple_cond_lhs (cond) != parm
1188 || !integer_zerop (gimple_cond_rhs (cond)))
1189 return;
1191 phi_bb = gimple_bb (phi);
1192 for (i = 0; i < 2; i++)
1194 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1195 if (pred != assign_bb && pred != cond_bb)
1196 return;
1199 bool type_p = false;
1200 if (param_type && POINTER_TYPE_P (param_type))
1201 type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type),
1202 call, jfunc, offset);
1203 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1204 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1205 parm_ref_data_pass_through_p (&parms_ainfo[index],
1206 call, parm), type_p);
1209 /* Given OP which is passed as an actual argument to a called function,
1210 determine if it is possible to construct a KNOWN_TYPE jump function for it
1211 and if so, create one and store it to JFUNC.
1212 EXPECTED_TYPE represents a type the argument should be in */
1214 static void
1215 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1216 gimple call, tree expected_type)
1218 HOST_WIDE_INT offset, size, max_size;
1219 tree base;
1221 if (!flag_devirtualize
1222 || TREE_CODE (op) != ADDR_EXPR
1223 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE
1224 /* Be sure expected_type is polymorphic. */
1225 || !expected_type
1226 || TREE_CODE (expected_type) != RECORD_TYPE
1227 || !TYPE_BINFO (expected_type)
1228 || !BINFO_VTABLE (TYPE_BINFO (expected_type)))
1229 return;
1231 op = TREE_OPERAND (op, 0);
1232 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1233 if (!DECL_P (base)
1234 || max_size == -1
1235 || max_size != size
1236 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1237 || is_global_var (base))
1238 return;
1240 if (detect_type_change (op, base, expected_type, call, jfunc, offset))
1241 return;
1243 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base),
1244 expected_type);
1247 /* Inspect the given TYPE and return true iff it has the same structure (the
1248 same number of fields of the same types) as a C++ member pointer. If
1249 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1250 corresponding fields there. */
1252 static bool
1253 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1255 tree fld;
1257 if (TREE_CODE (type) != RECORD_TYPE)
1258 return false;
1260 fld = TYPE_FIELDS (type);
1261 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1262 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1263 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1264 return false;
1266 if (method_ptr)
1267 *method_ptr = fld;
1269 fld = DECL_CHAIN (fld);
1270 if (!fld || INTEGRAL_TYPE_P (fld)
1271 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1272 return false;
1273 if (delta)
1274 *delta = fld;
1276 if (DECL_CHAIN (fld))
1277 return false;
1279 return true;
1282 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1283 return the rhs of its defining statement. Otherwise return RHS as it
1284 is. */
1286 static inline tree
1287 get_ssa_def_if_simple_copy (tree rhs)
1289 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1291 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1293 if (gimple_assign_single_p (def_stmt))
1294 rhs = gimple_assign_rhs1 (def_stmt);
1295 else
1296 break;
1298 return rhs;
1301 /* Simple linked list, describing known contents of an aggregate beforere
1302 call. */
1304 struct ipa_known_agg_contents_list
1306 /* Offset and size of the described part of the aggregate. */
1307 HOST_WIDE_INT offset, size;
1308 /* Known constant value or NULL if the contents is known to be unknown. */
1309 tree constant;
1310 /* Pointer to the next structure in the list. */
1311 struct ipa_known_agg_contents_list *next;
1314 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1315 in ARG is filled in with constant values. ARG can either be an aggregate
1316 expression or a pointer to an aggregate. JFUNC is the jump function into
1317 which the constants are subsequently stored. */
1319 static void
1320 determine_known_aggregate_parts (gimple call, tree arg,
1321 struct ipa_jump_func *jfunc)
1323 struct ipa_known_agg_contents_list *list = NULL;
1324 int item_count = 0, const_count = 0;
1325 HOST_WIDE_INT arg_offset, arg_size;
1326 gimple_stmt_iterator gsi;
1327 tree arg_base;
1328 bool check_ref, by_ref;
1329 ao_ref r;
1331 /* The function operates in three stages. First, we prepare check_ref, r,
1332 arg_base and arg_offset based on what is actually passed as an actual
1333 argument. */
1335 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1337 by_ref = true;
1338 if (TREE_CODE (arg) == SSA_NAME)
1340 tree type_size;
1341 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1342 return;
1343 check_ref = true;
1344 arg_base = arg;
1345 arg_offset = 0;
1346 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1347 arg_size = tree_low_cst (type_size, 1);
1348 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1350 else if (TREE_CODE (arg) == ADDR_EXPR)
1352 HOST_WIDE_INT arg_max_size;
1354 arg = TREE_OPERAND (arg, 0);
1355 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1356 &arg_max_size);
1357 if (arg_max_size == -1
1358 || arg_max_size != arg_size
1359 || arg_offset < 0)
1360 return;
1361 if (DECL_P (arg_base))
1363 tree size;
1364 check_ref = false;
1365 size = build_int_cst (integer_type_node, arg_size);
1366 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1368 else
1369 return;
1371 else
1372 return;
1374 else
1376 HOST_WIDE_INT arg_max_size;
1378 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1380 by_ref = false;
1381 check_ref = false;
1382 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1383 &arg_max_size);
1384 if (arg_max_size == -1
1385 || arg_max_size != arg_size
1386 || arg_offset < 0)
1387 return;
1389 ao_ref_init (&r, arg);
1392 /* Second stage walks back the BB, looks at individual statements and as long
1393 as it is confident of how the statements affect contents of the
1394 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1395 describing it. */
1396 gsi = gsi_for_stmt (call);
1397 gsi_prev (&gsi);
1398 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1400 struct ipa_known_agg_contents_list *n, **p;
1401 gimple stmt = gsi_stmt (gsi);
1402 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1403 tree lhs, rhs, lhs_base;
1404 bool partial_overlap;
1406 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1407 continue;
1408 if (!gimple_assign_single_p (stmt))
1409 break;
1411 lhs = gimple_assign_lhs (stmt);
1412 rhs = gimple_assign_rhs1 (stmt);
1413 if (!is_gimple_reg_type (rhs)
1414 || TREE_CODE (lhs) == BIT_FIELD_REF
1415 || contains_bitfld_component_ref_p (lhs))
1416 break;
1418 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1419 &lhs_max_size);
1420 if (lhs_max_size == -1
1421 || lhs_max_size != lhs_size
1422 || (lhs_offset < arg_offset
1423 && lhs_offset + lhs_size > arg_offset)
1424 || (lhs_offset < arg_offset + arg_size
1425 && lhs_offset + lhs_size > arg_offset + arg_size))
1426 break;
1428 if (check_ref)
1430 if (TREE_CODE (lhs_base) != MEM_REF
1431 || TREE_OPERAND (lhs_base, 0) != arg_base
1432 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1433 break;
1435 else if (lhs_base != arg_base)
1437 if (DECL_P (lhs_base))
1438 continue;
1439 else
1440 break;
1443 if (lhs_offset + lhs_size < arg_offset
1444 || lhs_offset >= (arg_offset + arg_size))
1445 continue;
1447 partial_overlap = false;
1448 p = &list;
1449 while (*p && (*p)->offset < lhs_offset)
1451 if ((*p)->offset + (*p)->size > lhs_offset)
1453 partial_overlap = true;
1454 break;
1456 p = &(*p)->next;
1458 if (partial_overlap)
1459 break;
1460 if (*p && (*p)->offset < lhs_offset + lhs_size)
1462 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1463 /* We already know this value is subsequently overwritten with
1464 something else. */
1465 continue;
1466 else
1467 /* Otherwise this is a partial overlap which we cannot
1468 represent. */
1469 break;
1472 rhs = get_ssa_def_if_simple_copy (rhs);
1473 n = XALLOCA (struct ipa_known_agg_contents_list);
1474 n->size = lhs_size;
1475 n->offset = lhs_offset;
1476 if (is_gimple_ip_invariant (rhs))
1478 n->constant = rhs;
1479 const_count++;
1481 else
1482 n->constant = NULL_TREE;
1483 n->next = *p;
1484 *p = n;
1486 item_count++;
1487 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1488 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1489 break;
1492 /* Third stage just goes over the list and creates an appropriate vector of
1493 ipa_agg_jf_item structures out of it, of sourse only if there are
1494 any known constants to begin with. */
1496 if (const_count)
1498 jfunc->agg.by_ref = by_ref;
1499 vec_alloc (jfunc->agg.items, const_count);
1500 while (list)
1502 if (list->constant)
1504 struct ipa_agg_jf_item item;
1505 item.offset = list->offset - arg_offset;
1506 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1507 item.value = unshare_expr_without_location (list->constant);
1508 jfunc->agg.items->quick_push (item);
1510 list = list->next;
1515 static tree
1516 ipa_get_callee_param_type (struct cgraph_edge *e, int i)
1518 int n;
1519 tree type = (e->callee
1520 ? TREE_TYPE (e->callee->decl)
1521 : gimple_call_fntype (e->call_stmt));
1522 tree t = TYPE_ARG_TYPES (type);
1524 for (n = 0; n < i; n++)
1526 if (!t)
1527 break;
1528 t = TREE_CHAIN (t);
1530 if (t)
1531 return TREE_VALUE (t);
1532 if (!e->callee)
1533 return NULL;
1534 t = DECL_ARGUMENTS (e->callee->decl);
1535 for (n = 0; n < i; n++)
1537 if (!t)
1538 return NULL;
1539 t = TREE_CHAIN (t);
1541 if (t)
1542 return TREE_TYPE (t);
1543 return NULL;
1546 /* Compute jump function for all arguments of callsite CS and insert the
1547 information in the jump_functions array in the ipa_edge_args corresponding
1548 to this callsite. */
1550 static void
1551 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1552 struct cgraph_edge *cs)
1554 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1555 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1556 gimple call = cs->call_stmt;
1557 int n, arg_num = gimple_call_num_args (call);
1559 if (arg_num == 0 || args->jump_functions)
1560 return;
1561 vec_safe_grow_cleared (args->jump_functions, arg_num);
1563 if (gimple_call_internal_p (call))
1564 return;
1565 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1566 return;
1568 for (n = 0; n < arg_num; n++)
1570 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1571 tree arg = gimple_call_arg (call, n);
1572 tree param_type = ipa_get_callee_param_type (cs, n);
1574 if (is_gimple_ip_invariant (arg))
1575 ipa_set_jf_constant (jfunc, arg, cs);
1576 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1577 && TREE_CODE (arg) == PARM_DECL)
1579 int index = ipa_get_param_decl_index (info, arg);
1581 gcc_assert (index >=0);
1582 /* Aggregate passed by value, check for pass-through, otherwise we
1583 will attempt to fill in aggregate contents later in this
1584 for cycle. */
1585 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1587 ipa_set_jf_simple_pass_through (jfunc, index, false, false);
1588 continue;
1591 else if (TREE_CODE (arg) == SSA_NAME)
1593 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1595 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1596 if (index >= 0)
1598 bool agg_p, type_p;
1599 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1600 call, arg);
1601 if (param_type && POINTER_TYPE_P (param_type))
1602 type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type),
1603 call, jfunc);
1604 else
1605 type_p = false;
1606 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1607 ipa_set_jf_simple_pass_through (jfunc, index, agg_p,
1608 type_p);
1611 else
1613 gimple stmt = SSA_NAME_DEF_STMT (arg);
1614 if (is_gimple_assign (stmt))
1615 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1616 call, stmt, arg, param_type);
1617 else if (gimple_code (stmt) == GIMPLE_PHI)
1618 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1619 call, stmt, param_type);
1622 else
1623 compute_known_type_jump_func (arg, jfunc, call,
1624 param_type
1625 && POINTER_TYPE_P (param_type)
1626 ? TREE_TYPE (param_type)
1627 : NULL);
1629 if ((jfunc->type != IPA_JF_PASS_THROUGH
1630 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1631 && (jfunc->type != IPA_JF_ANCESTOR
1632 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1633 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1634 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1635 determine_known_aggregate_parts (call, arg, jfunc);
1639 /* Compute jump functions for all edges - both direct and indirect - outgoing
1640 from NODE. Also count the actual arguments in the process. */
1642 static void
1643 ipa_compute_jump_functions (struct cgraph_node *node,
1644 struct param_analysis_info *parms_ainfo)
1646 struct cgraph_edge *cs;
1648 for (cs = node->callees; cs; cs = cs->next_callee)
1650 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1651 NULL);
1652 /* We do not need to bother analyzing calls to unknown
1653 functions unless they may become known during lto/whopr. */
1654 if (!callee->definition && !flag_lto)
1655 continue;
1656 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1659 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1660 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1663 /* If STMT looks like a statement loading a value from a member pointer formal
1664 parameter, return that parameter and store the offset of the field to
1665 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1666 might be clobbered). If USE_DELTA, then we look for a use of the delta
1667 field rather than the pfn. */
1669 static tree
1670 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1671 HOST_WIDE_INT *offset_p)
1673 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1675 if (!gimple_assign_single_p (stmt))
1676 return NULL_TREE;
1678 rhs = gimple_assign_rhs1 (stmt);
1679 if (TREE_CODE (rhs) == COMPONENT_REF)
1681 ref_field = TREE_OPERAND (rhs, 1);
1682 rhs = TREE_OPERAND (rhs, 0);
1684 else
1685 ref_field = NULL_TREE;
1686 if (TREE_CODE (rhs) != MEM_REF)
1687 return NULL_TREE;
1688 rec = TREE_OPERAND (rhs, 0);
1689 if (TREE_CODE (rec) != ADDR_EXPR)
1690 return NULL_TREE;
1691 rec = TREE_OPERAND (rec, 0);
1692 if (TREE_CODE (rec) != PARM_DECL
1693 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1694 return NULL_TREE;
1695 ref_offset = TREE_OPERAND (rhs, 1);
1697 if (use_delta)
1698 fld = delta_field;
1699 else
1700 fld = ptr_field;
1701 if (offset_p)
1702 *offset_p = int_bit_position (fld);
1704 if (ref_field)
1706 if (integer_nonzerop (ref_offset))
1707 return NULL_TREE;
1708 return ref_field == fld ? rec : NULL_TREE;
1710 else
1711 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1712 : NULL_TREE;
1715 /* Returns true iff T is an SSA_NAME defined by a statement. */
1717 static bool
1718 ipa_is_ssa_with_stmt_def (tree t)
1720 if (TREE_CODE (t) == SSA_NAME
1721 && !SSA_NAME_IS_DEFAULT_DEF (t))
1722 return true;
1723 else
1724 return false;
1727 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1728 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1729 indirect call graph edge. */
1731 static struct cgraph_edge *
1732 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1734 struct cgraph_edge *cs;
1736 cs = cgraph_edge (node, stmt);
1737 cs->indirect_info->param_index = param_index;
1738 cs->indirect_info->offset = 0;
1739 cs->indirect_info->polymorphic = 0;
1740 cs->indirect_info->agg_contents = 0;
1741 cs->indirect_info->member_ptr = 0;
1742 return cs;
1745 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1746 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1747 intermediate information about each formal parameter. Currently it checks
1748 whether the call calls a pointer that is a formal parameter and if so, the
1749 parameter is marked with the called flag and an indirect call graph edge
1750 describing the call is created. This is very simple for ordinary pointers
1751 represented in SSA but not-so-nice when it comes to member pointers. The
1752 ugly part of this function does nothing more than trying to match the
1753 pattern of such a call. An example of such a pattern is the gimple dump
1754 below, the call is on the last line:
1756 <bb 2>:
1757 f$__delta_5 = f.__delta;
1758 f$__pfn_24 = f.__pfn;
1761 <bb 2>:
1762 f$__delta_5 = MEM[(struct *)&f];
1763 f$__pfn_24 = MEM[(struct *)&f + 4B];
1765 and a few lines below:
1767 <bb 5>
1768 D.2496_3 = (int) f$__pfn_24;
1769 D.2497_4 = D.2496_3 & 1;
1770 if (D.2497_4 != 0)
1771 goto <bb 3>;
1772 else
1773 goto <bb 4>;
1775 <bb 6>:
1776 D.2500_7 = (unsigned int) f$__delta_5;
1777 D.2501_8 = &S + D.2500_7;
1778 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1779 D.2503_10 = *D.2502_9;
1780 D.2504_12 = f$__pfn_24 + -1;
1781 D.2505_13 = (unsigned int) D.2504_12;
1782 D.2506_14 = D.2503_10 + D.2505_13;
1783 D.2507_15 = *D.2506_14;
1784 iftmp.11_16 = (String:: *) D.2507_15;
1786 <bb 7>:
1787 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1788 D.2500_19 = (unsigned int) f$__delta_5;
1789 D.2508_20 = &S + D.2500_19;
1790 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1792 Such patterns are results of simple calls to a member pointer:
1794 int doprinting (int (MyString::* f)(int) const)
1796 MyString S ("somestring");
1798 return (S.*f)(4);
1801 Moreover, the function also looks for called pointers loaded from aggregates
1802 passed by value or reference. */
1804 static void
1805 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1806 struct ipa_node_params *info,
1807 struct param_analysis_info *parms_ainfo,
1808 gimple call, tree target)
1810 gimple def;
1811 tree n1, n2;
1812 gimple d1, d2;
1813 tree rec, rec2, cond;
1814 gimple branch;
1815 int index;
1816 basic_block bb, virt_bb, join;
1817 HOST_WIDE_INT offset;
1818 bool by_ref;
1820 if (SSA_NAME_IS_DEFAULT_DEF (target))
1822 tree var = SSA_NAME_VAR (target);
1823 index = ipa_get_param_decl_index (info, var);
1824 if (index >= 0)
1825 ipa_note_param_call (node, index, call);
1826 return;
1829 def = SSA_NAME_DEF_STMT (target);
1830 if (gimple_assign_single_p (def)
1831 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1832 gimple_assign_rhs1 (def), &index, &offset,
1833 NULL, &by_ref))
1835 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1836 cs->indirect_info->offset = offset;
1837 cs->indirect_info->agg_contents = 1;
1838 cs->indirect_info->by_ref = by_ref;
1839 return;
1842 /* Now we need to try to match the complex pattern of calling a member
1843 pointer. */
1844 if (gimple_code (def) != GIMPLE_PHI
1845 || gimple_phi_num_args (def) != 2
1846 || !POINTER_TYPE_P (TREE_TYPE (target))
1847 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1848 return;
1850 /* First, we need to check whether one of these is a load from a member
1851 pointer that is a parameter to this function. */
1852 n1 = PHI_ARG_DEF (def, 0);
1853 n2 = PHI_ARG_DEF (def, 1);
1854 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1855 return;
1856 d1 = SSA_NAME_DEF_STMT (n1);
1857 d2 = SSA_NAME_DEF_STMT (n2);
1859 join = gimple_bb (def);
1860 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1862 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1863 return;
1865 bb = EDGE_PRED (join, 0)->src;
1866 virt_bb = gimple_bb (d2);
1868 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1870 bb = EDGE_PRED (join, 1)->src;
1871 virt_bb = gimple_bb (d1);
1873 else
1874 return;
1876 /* Second, we need to check that the basic blocks are laid out in the way
1877 corresponding to the pattern. */
1879 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1880 || single_pred (virt_bb) != bb
1881 || single_succ (virt_bb) != join)
1882 return;
1884 /* Third, let's see that the branching is done depending on the least
1885 significant bit of the pfn. */
1887 branch = last_stmt (bb);
1888 if (!branch || gimple_code (branch) != GIMPLE_COND)
1889 return;
1891 if ((gimple_cond_code (branch) != NE_EXPR
1892 && gimple_cond_code (branch) != EQ_EXPR)
1893 || !integer_zerop (gimple_cond_rhs (branch)))
1894 return;
1896 cond = gimple_cond_lhs (branch);
1897 if (!ipa_is_ssa_with_stmt_def (cond))
1898 return;
1900 def = SSA_NAME_DEF_STMT (cond);
1901 if (!is_gimple_assign (def)
1902 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1903 || !integer_onep (gimple_assign_rhs2 (def)))
1904 return;
1906 cond = gimple_assign_rhs1 (def);
1907 if (!ipa_is_ssa_with_stmt_def (cond))
1908 return;
1910 def = SSA_NAME_DEF_STMT (cond);
1912 if (is_gimple_assign (def)
1913 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1915 cond = gimple_assign_rhs1 (def);
1916 if (!ipa_is_ssa_with_stmt_def (cond))
1917 return;
1918 def = SSA_NAME_DEF_STMT (cond);
1921 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1922 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1923 == ptrmemfunc_vbit_in_delta),
1924 NULL);
1925 if (rec != rec2)
1926 return;
1928 index = ipa_get_param_decl_index (info, rec);
1929 if (index >= 0
1930 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1932 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1933 cs->indirect_info->offset = offset;
1934 cs->indirect_info->agg_contents = 1;
1935 cs->indirect_info->member_ptr = 1;
1938 return;
1941 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1942 object referenced in the expression is a formal parameter of the caller
1943 (described by INFO), create a call note for the statement. */
1945 static void
1946 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1947 struct ipa_node_params *info, gimple call,
1948 tree target)
1950 struct cgraph_edge *cs;
1951 struct cgraph_indirect_call_info *ii;
1952 struct ipa_jump_func jfunc;
1953 tree obj = OBJ_TYPE_REF_OBJECT (target);
1954 int index;
1955 HOST_WIDE_INT anc_offset;
1957 if (!flag_devirtualize)
1958 return;
1960 if (TREE_CODE (obj) != SSA_NAME)
1961 return;
1963 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1965 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1966 return;
1968 anc_offset = 0;
1969 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1970 gcc_assert (index >= 0);
1971 if (detect_type_change_ssa (obj, obj_type_ref_class (target),
1972 call, &jfunc))
1973 return;
1975 else
1977 gimple stmt = SSA_NAME_DEF_STMT (obj);
1978 tree expr;
1980 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1981 if (!expr)
1982 return;
1983 index = ipa_get_param_decl_index (info,
1984 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1985 gcc_assert (index >= 0);
1986 if (detect_type_change (obj, expr, obj_type_ref_class (target),
1987 call, &jfunc, anc_offset))
1988 return;
1991 cs = ipa_note_param_call (node, index, call);
1992 ii = cs->indirect_info;
1993 ii->offset = anc_offset;
1994 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1995 ii->otr_type = obj_type_ref_class (target);
1996 ii->polymorphic = 1;
1999 /* Analyze a call statement CALL whether and how it utilizes formal parameters
2000 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
2001 containing intermediate information about each formal parameter. */
2003 static void
2004 ipa_analyze_call_uses (struct cgraph_node *node,
2005 struct ipa_node_params *info,
2006 struct param_analysis_info *parms_ainfo, gimple call)
2008 tree target = gimple_call_fn (call);
2010 if (!target)
2011 return;
2012 if (TREE_CODE (target) == SSA_NAME)
2013 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
2014 else if (virtual_method_call_p (target))
2015 ipa_analyze_virtual_call_uses (node, info, call, target);
2019 /* Analyze the call statement STMT with respect to formal parameters (described
2020 in INFO) of caller given by NODE. Currently it only checks whether formal
2021 parameters are called. PARMS_AINFO is a pointer to a vector containing
2022 intermediate information about each formal parameter. */
2024 static void
2025 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
2026 struct param_analysis_info *parms_ainfo, gimple stmt)
2028 if (is_gimple_call (stmt))
2029 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
2032 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
2033 If OP is a parameter declaration, mark it as used in the info structure
2034 passed in DATA. */
2036 static bool
2037 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
2038 tree op, void *data)
2040 struct ipa_node_params *info = (struct ipa_node_params *) data;
2042 op = get_base_address (op);
2043 if (op
2044 && TREE_CODE (op) == PARM_DECL)
2046 int index = ipa_get_param_decl_index (info, op);
2047 gcc_assert (index >= 0);
2048 ipa_set_param_used (info, index, true);
2051 return false;
2054 /* Scan the function body of NODE and inspect the uses of formal parameters.
2055 Store the findings in various structures of the associated ipa_node_params
2056 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
2057 vector containing intermediate information about each formal parameter. */
2059 static void
2060 ipa_analyze_params_uses (struct cgraph_node *node,
2061 struct param_analysis_info *parms_ainfo)
2063 tree decl = node->decl;
2064 basic_block bb;
2065 struct function *func;
2066 gimple_stmt_iterator gsi;
2067 struct ipa_node_params *info = IPA_NODE_REF (node);
2068 int i;
2070 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
2071 return;
2073 info->uses_analysis_done = 1;
2074 if (ipa_func_spec_opts_forbid_analysis_p (node))
2076 for (i = 0; i < ipa_get_param_count (info); i++)
2078 ipa_set_param_used (info, i, true);
2079 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
2081 return;
2084 for (i = 0; i < ipa_get_param_count (info); i++)
2086 tree parm = ipa_get_param (info, i);
2087 int controlled_uses = 0;
2089 /* For SSA regs see if parameter is used. For non-SSA we compute
2090 the flag during modification analysis. */
2091 if (is_gimple_reg (parm))
2093 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
2094 parm);
2095 if (ddef && !has_zero_uses (ddef))
2097 imm_use_iterator imm_iter;
2098 use_operand_p use_p;
2100 ipa_set_param_used (info, i, true);
2101 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
2102 if (!is_gimple_call (USE_STMT (use_p)))
2104 controlled_uses = IPA_UNDESCRIBED_USE;
2105 break;
2107 else
2108 controlled_uses++;
2110 else
2111 controlled_uses = 0;
2113 else
2114 controlled_uses = IPA_UNDESCRIBED_USE;
2115 ipa_set_controlled_uses (info, i, controlled_uses);
2118 func = DECL_STRUCT_FUNCTION (decl);
2119 FOR_EACH_BB_FN (bb, func)
2121 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2123 gimple stmt = gsi_stmt (gsi);
2125 if (is_gimple_debug (stmt))
2126 continue;
2128 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
2129 walk_stmt_load_store_addr_ops (stmt, info,
2130 visit_ref_for_mod_analysis,
2131 visit_ref_for_mod_analysis,
2132 visit_ref_for_mod_analysis);
2134 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2135 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
2136 visit_ref_for_mod_analysis,
2137 visit_ref_for_mod_analysis,
2138 visit_ref_for_mod_analysis);
2142 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
2144 static void
2145 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
2147 int i;
2149 for (i = 0; i < param_count; i++)
2151 if (parms_ainfo[i].parm_visited_statements)
2152 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
2153 if (parms_ainfo[i].pt_visited_statements)
2154 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
2158 /* Initialize the array describing properties of of formal parameters
2159 of NODE, analyze their uses and compute jump functions associated
2160 with actual arguments of calls from within NODE. */
2162 void
2163 ipa_analyze_node (struct cgraph_node *node)
2165 struct ipa_node_params *info;
2166 struct param_analysis_info *parms_ainfo;
2167 int param_count;
2169 ipa_check_create_node_params ();
2170 ipa_check_create_edge_args ();
2171 info = IPA_NODE_REF (node);
2172 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2173 ipa_initialize_node_params (node);
2175 param_count = ipa_get_param_count (info);
2176 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
2177 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
2179 ipa_analyze_params_uses (node, parms_ainfo);
2180 ipa_compute_jump_functions (node, parms_ainfo);
2182 free_parms_ainfo (parms_ainfo, param_count);
2183 pop_cfun ();
2186 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2187 attempt a type-based devirtualization. If successful, return the
2188 target function declaration, otherwise return NULL. */
2190 tree
2191 ipa_intraprocedural_devirtualization (gimple call)
2193 tree binfo, token, fndecl;
2194 struct ipa_jump_func jfunc;
2195 tree otr = gimple_call_fn (call);
2197 jfunc.type = IPA_JF_UNKNOWN;
2198 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2199 call, obj_type_ref_class (otr));
2200 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2201 return NULL_TREE;
2202 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2203 if (!binfo)
2204 return NULL_TREE;
2205 token = OBJ_TYPE_REF_TOKEN (otr);
2206 fndecl = gimple_get_virt_method_for_binfo (tree_low_cst (token, 1),
2207 binfo);
2208 #ifdef ENABLE_CHECKING
2209 if (fndecl)
2210 gcc_assert (possible_polymorphic_call_target_p
2211 (otr, cgraph_get_node (fndecl)));
2212 #endif
2213 return fndecl;
2216 /* Update the jump function DST when the call graph edge corresponding to SRC is
2217 is being inlined, knowing that DST is of type ancestor and src of known
2218 type. */
2220 static void
2221 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2222 struct ipa_jump_func *dst)
2224 HOST_WIDE_INT combined_offset;
2225 tree combined_type;
2227 if (!ipa_get_jf_ancestor_type_preserved (dst))
2229 dst->type = IPA_JF_UNKNOWN;
2230 return;
2233 combined_offset = ipa_get_jf_known_type_offset (src)
2234 + ipa_get_jf_ancestor_offset (dst);
2235 combined_type = ipa_get_jf_ancestor_type (dst);
2237 ipa_set_jf_known_type (dst, combined_offset,
2238 ipa_get_jf_known_type_base_type (src),
2239 combined_type);
2242 /* Update the jump functions associated with call graph edge E when the call
2243 graph edge CS is being inlined, assuming that E->caller is already (possibly
2244 indirectly) inlined into CS->callee and that E has not been inlined. */
2246 static void
2247 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2248 struct cgraph_edge *e)
2250 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2251 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2252 int count = ipa_get_cs_argument_count (args);
2253 int i;
2255 for (i = 0; i < count; i++)
2257 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2259 if (dst->type == IPA_JF_ANCESTOR)
2261 struct ipa_jump_func *src;
2262 int dst_fid = dst->value.ancestor.formal_id;
2264 /* Variable number of arguments can cause havoc if we try to access
2265 one that does not exist in the inlined edge. So make sure we
2266 don't. */
2267 if (dst_fid >= ipa_get_cs_argument_count (top))
2269 dst->type = IPA_JF_UNKNOWN;
2270 continue;
2273 src = ipa_get_ith_jump_func (top, dst_fid);
2275 if (src->agg.items
2276 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2278 struct ipa_agg_jf_item *item;
2279 int j;
2281 /* Currently we do not produce clobber aggregate jump functions,
2282 replace with merging when we do. */
2283 gcc_assert (!dst->agg.items);
2285 dst->agg.items = vec_safe_copy (src->agg.items);
2286 dst->agg.by_ref = src->agg.by_ref;
2287 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2288 item->offset -= dst->value.ancestor.offset;
2291 if (src->type == IPA_JF_KNOWN_TYPE)
2292 combine_known_type_and_ancestor_jfs (src, dst);
2293 else if (src->type == IPA_JF_PASS_THROUGH
2294 && src->value.pass_through.operation == NOP_EXPR)
2296 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2297 dst->value.ancestor.agg_preserved &=
2298 src->value.pass_through.agg_preserved;
2299 dst->value.ancestor.type_preserved &=
2300 src->value.pass_through.type_preserved;
2302 else if (src->type == IPA_JF_ANCESTOR)
2304 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2305 dst->value.ancestor.offset += src->value.ancestor.offset;
2306 dst->value.ancestor.agg_preserved &=
2307 src->value.ancestor.agg_preserved;
2308 dst->value.ancestor.type_preserved &=
2309 src->value.ancestor.type_preserved;
2311 else
2312 dst->type = IPA_JF_UNKNOWN;
2314 else if (dst->type == IPA_JF_PASS_THROUGH)
2316 struct ipa_jump_func *src;
2317 /* We must check range due to calls with variable number of arguments
2318 and we cannot combine jump functions with operations. */
2319 if (dst->value.pass_through.operation == NOP_EXPR
2320 && (dst->value.pass_through.formal_id
2321 < ipa_get_cs_argument_count (top)))
2323 int dst_fid = dst->value.pass_through.formal_id;
2324 src = ipa_get_ith_jump_func (top, dst_fid);
2325 bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
2327 switch (src->type)
2329 case IPA_JF_UNKNOWN:
2330 dst->type = IPA_JF_UNKNOWN;
2331 break;
2332 case IPA_JF_KNOWN_TYPE:
2333 ipa_set_jf_known_type (dst,
2334 ipa_get_jf_known_type_offset (src),
2335 ipa_get_jf_known_type_base_type (src),
2336 ipa_get_jf_known_type_base_type (src));
2337 break;
2338 case IPA_JF_CONST:
2339 ipa_set_jf_cst_copy (dst, src);
2340 break;
2342 case IPA_JF_PASS_THROUGH:
2344 int formal_id = ipa_get_jf_pass_through_formal_id (src);
2345 enum tree_code operation;
2346 operation = ipa_get_jf_pass_through_operation (src);
2348 if (operation == NOP_EXPR)
2350 bool agg_p, type_p;
2351 agg_p = dst_agg_p
2352 && ipa_get_jf_pass_through_agg_preserved (src);
2353 type_p = ipa_get_jf_pass_through_type_preserved (src)
2354 && ipa_get_jf_pass_through_type_preserved (dst);
2355 ipa_set_jf_simple_pass_through (dst, formal_id,
2356 agg_p, type_p);
2358 else
2360 tree operand = ipa_get_jf_pass_through_operand (src);
2361 ipa_set_jf_arith_pass_through (dst, formal_id, operand,
2362 operation);
2364 break;
2366 case IPA_JF_ANCESTOR:
2368 bool agg_p, type_p;
2369 agg_p = dst_agg_p
2370 && ipa_get_jf_ancestor_agg_preserved (src);
2371 type_p = ipa_get_jf_ancestor_type_preserved (src)
2372 && ipa_get_jf_pass_through_type_preserved (dst);
2373 ipa_set_ancestor_jf (dst,
2374 ipa_get_jf_ancestor_offset (src),
2375 ipa_get_jf_ancestor_type (src),
2376 ipa_get_jf_ancestor_formal_id (src),
2377 agg_p, type_p);
2378 break;
2380 default:
2381 gcc_unreachable ();
2384 if (src->agg.items
2385 && (dst_agg_p || !src->agg.by_ref))
2387 /* Currently we do not produce clobber aggregate jump
2388 functions, replace with merging when we do. */
2389 gcc_assert (!dst->agg.items);
2391 dst->agg.by_ref = src->agg.by_ref;
2392 dst->agg.items = vec_safe_copy (src->agg.items);
2395 else
2396 dst->type = IPA_JF_UNKNOWN;
2401 /* If TARGET is an addr_expr of a function declaration, make it the destination
2402 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2404 struct cgraph_edge *
2405 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2407 struct cgraph_node *callee;
2408 struct inline_edge_summary *es = inline_edge_summary (ie);
2409 bool unreachable = false;
2411 if (TREE_CODE (target) == ADDR_EXPR)
2412 target = TREE_OPERAND (target, 0);
2413 if (TREE_CODE (target) != FUNCTION_DECL)
2415 target = canonicalize_constructor_val (target, NULL);
2416 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2418 if (ie->indirect_info->member_ptr)
2419 /* Member pointer call that goes through a VMT lookup. */
2420 return NULL;
2422 if (dump_file)
2423 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2424 " in %s/%i, making it unreachable.\n",
2425 cgraph_node_name (ie->caller), ie->caller->order);
2426 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2427 callee = cgraph_get_create_node (target);
2428 unreachable = true;
2430 else
2431 callee = cgraph_get_node (target);
2433 else
2434 callee = cgraph_get_node (target);
2436 /* Because may-edges are not explicitely represented and vtable may be external,
2437 we may create the first reference to the object in the unit. */
2438 if (!callee || callee->global.inlined_to)
2441 /* We are better to ensure we can refer to it.
2442 In the case of static functions we are out of luck, since we already
2443 removed its body. In the case of public functions we may or may
2444 not introduce the reference. */
2445 if (!canonicalize_constructor_val (target, NULL)
2446 || !TREE_PUBLIC (target))
2448 if (dump_file)
2449 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2450 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2451 xstrdup (cgraph_node_name (ie->caller)),
2452 ie->caller->order,
2453 xstrdup (cgraph_node_name (ie->callee)),
2454 ie->callee->order);
2455 return NULL;
2457 callee = cgraph_get_create_real_symbol_node (target);
2459 ipa_check_create_node_params ();
2461 /* We can not make edges to inline clones. It is bug that someone removed
2462 the cgraph node too early. */
2463 gcc_assert (!callee->global.inlined_to);
2465 if (dump_file && !unreachable)
2467 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2468 "(%s/%i -> %s/%i), for stmt ",
2469 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2470 xstrdup (cgraph_node_name (ie->caller)),
2471 ie->caller->order,
2472 xstrdup (cgraph_node_name (callee)),
2473 callee->order);
2474 if (ie->call_stmt)
2475 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2476 else
2477 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2479 ie = cgraph_make_edge_direct (ie, callee);
2480 es = inline_edge_summary (ie);
2481 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2482 - eni_size_weights.call_cost);
2483 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2484 - eni_time_weights.call_cost);
2486 return ie;
2489 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2490 return NULL if there is not any. BY_REF specifies whether the value has to
2491 be passed by reference or by value. */
2493 tree
2494 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2495 HOST_WIDE_INT offset, bool by_ref)
2497 struct ipa_agg_jf_item *item;
2498 int i;
2500 if (by_ref != agg->by_ref)
2501 return NULL;
2503 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2504 if (item->offset == offset)
2506 /* Currently we do not have clobber values, return NULL for them once
2507 we do. */
2508 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2509 return item->value;
2511 return NULL;
2514 /* Remove a reference to SYMBOL from the list of references of a node given by
2515 reference description RDESC. Return true if the reference has been
2516 successfully found and removed. */
2518 static bool
2519 remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
2521 struct ipa_ref *to_del;
2522 struct cgraph_edge *origin;
2524 origin = rdesc->cs;
2525 if (!origin)
2526 return false;
2527 to_del = ipa_find_reference (origin->caller, symbol,
2528 origin->call_stmt, origin->lto_stmt_uid);
2529 if (!to_del)
2530 return false;
2532 ipa_remove_reference (to_del);
2533 if (dump_file)
2534 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2535 xstrdup (cgraph_node_name (origin->caller)),
2536 origin->caller->order, xstrdup (symtab_node_name (symbol)));
2537 return true;
2540 /* If JFUNC has a reference description with refcount different from
2541 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2542 NULL. JFUNC must be a constant jump function. */
2544 static struct ipa_cst_ref_desc *
2545 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2547 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2548 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2549 return rdesc;
2550 else
2551 return NULL;
2554 /* If the value of constant jump function JFUNC is an address of a function
2555 declaration, return the associated call graph node. Otherwise return
2556 NULL. */
2558 static cgraph_node *
2559 cgraph_node_for_jfunc (struct ipa_jump_func *jfunc)
2561 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
2562 tree cst = ipa_get_jf_constant (jfunc);
2563 if (TREE_CODE (cst) != ADDR_EXPR
2564 || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL)
2565 return NULL;
2567 return cgraph_get_node (TREE_OPERAND (cst, 0));
2571 /* If JFUNC is a constant jump function with a usable rdesc, decrement its
2572 refcount and if it hits zero, remove reference to SYMBOL from the caller of
2573 the edge specified in the rdesc. Return false if either the symbol or the
2574 reference could not be found, otherwise return true. */
2576 static bool
2577 try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
2579 struct ipa_cst_ref_desc *rdesc;
2580 if (jfunc->type == IPA_JF_CONST
2581 && (rdesc = jfunc_rdesc_usable (jfunc))
2582 && --rdesc->refcount == 0)
2584 symtab_node *symbol = cgraph_node_for_jfunc (jfunc);
2585 if (!symbol)
2586 return false;
2588 return remove_described_reference (symbol, rdesc);
2590 return true;
2593 /* Try to find a destination for indirect edge IE that corresponds to a simple
2594 call or a call of a member function pointer and where the destination is a
2595 pointer formal parameter described by jump function JFUNC. If it can be
2596 determined, return the newly direct edge, otherwise return NULL.
2597 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2599 static struct cgraph_edge *
2600 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2601 struct ipa_jump_func *jfunc,
2602 struct ipa_node_params *new_root_info)
2604 struct cgraph_edge *cs;
2605 tree target;
2606 bool agg_contents = ie->indirect_info->agg_contents;
2608 if (ie->indirect_info->agg_contents)
2609 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2610 ie->indirect_info->offset,
2611 ie->indirect_info->by_ref);
2612 else
2613 target = ipa_value_from_jfunc (new_root_info, jfunc);
2614 if (!target)
2615 return NULL;
2616 cs = ipa_make_edge_direct_to_target (ie, target);
2618 if (cs && !agg_contents)
2620 bool ok;
2621 gcc_checking_assert (cs->callee
2622 && (cs != ie
2623 || jfunc->type != IPA_JF_CONST
2624 || !cgraph_node_for_jfunc (jfunc)
2625 || cs->callee == cgraph_node_for_jfunc (jfunc)));
2626 ok = try_decrement_rdesc_refcount (jfunc);
2627 gcc_checking_assert (ok);
2630 return cs;
2633 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2634 call based on a formal parameter which is described by jump function JFUNC
2635 and if it can be determined, make it direct and return the direct edge.
2636 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2637 are relative to. */
2639 static struct cgraph_edge *
2640 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2641 struct ipa_jump_func *jfunc,
2642 struct ipa_node_params *new_root_info)
2644 tree binfo, target;
2646 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2648 if (!binfo)
2649 return NULL;
2651 if (TREE_CODE (binfo) != TREE_BINFO)
2653 binfo = gimple_extract_devirt_binfo_from_cst
2654 (binfo, ie->indirect_info->otr_type);
2655 if (!binfo)
2656 return NULL;
2659 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2660 ie->indirect_info->otr_type);
2661 if (binfo)
2662 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2663 binfo);
2664 else
2665 return NULL;
2667 if (target)
2669 #ifdef ENABLE_CHECKING
2670 gcc_assert (possible_polymorphic_call_target_p
2671 (ie, cgraph_get_node (target)));
2672 #endif
2673 return ipa_make_edge_direct_to_target (ie, target);
2675 else
2676 return NULL;
2679 /* Update the param called notes associated with NODE when CS is being inlined,
2680 assuming NODE is (potentially indirectly) inlined into CS->callee.
2681 Moreover, if the callee is discovered to be constant, create a new cgraph
2682 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2683 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2685 static bool
2686 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2687 struct cgraph_node *node,
2688 vec<cgraph_edge_p> *new_edges)
2690 struct ipa_edge_args *top;
2691 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2692 struct ipa_node_params *new_root_info;
2693 bool res = false;
2695 ipa_check_create_edge_args ();
2696 top = IPA_EDGE_REF (cs);
2697 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2698 ? cs->caller->global.inlined_to
2699 : cs->caller);
2701 for (ie = node->indirect_calls; ie; ie = next_ie)
2703 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2704 struct ipa_jump_func *jfunc;
2705 int param_index;
2707 next_ie = ie->next_callee;
2709 if (ici->param_index == -1)
2710 continue;
2712 /* We must check range due to calls with variable number of arguments: */
2713 if (ici->param_index >= ipa_get_cs_argument_count (top))
2715 ici->param_index = -1;
2716 continue;
2719 param_index = ici->param_index;
2720 jfunc = ipa_get_ith_jump_func (top, param_index);
2722 if (!flag_indirect_inlining)
2723 new_direct_edge = NULL;
2724 else if (ici->polymorphic)
2725 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2726 new_root_info);
2727 else
2728 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2729 new_root_info);
2730 /* If speculation was removed, then we need to do nothing. */
2731 if (new_direct_edge && new_direct_edge != ie)
2733 new_direct_edge->indirect_inlining_edge = 1;
2734 top = IPA_EDGE_REF (cs);
2735 res = true;
2737 else if (new_direct_edge)
2739 new_direct_edge->indirect_inlining_edge = 1;
2740 if (new_direct_edge->call_stmt)
2741 new_direct_edge->call_stmt_cannot_inline_p
2742 = !gimple_check_call_matching_types (
2743 new_direct_edge->call_stmt,
2744 new_direct_edge->callee->decl, false);
2745 if (new_edges)
2747 new_edges->safe_push (new_direct_edge);
2748 res = true;
2750 top = IPA_EDGE_REF (cs);
2752 else if (jfunc->type == IPA_JF_PASS_THROUGH
2753 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2755 if (ici->agg_contents
2756 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2757 ici->param_index = -1;
2758 else
2759 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2761 else if (jfunc->type == IPA_JF_ANCESTOR)
2763 if (ici->agg_contents
2764 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2765 ici->param_index = -1;
2766 else
2768 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2769 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2772 else
2773 /* Either we can find a destination for this edge now or never. */
2774 ici->param_index = -1;
2777 return res;
2780 /* Recursively traverse subtree of NODE (including node) made of inlined
2781 cgraph_edges when CS has been inlined and invoke
2782 update_indirect_edges_after_inlining on all nodes and
2783 update_jump_functions_after_inlining on all non-inlined edges that lead out
2784 of this subtree. Newly discovered indirect edges will be added to
2785 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2786 created. */
2788 static bool
2789 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2790 struct cgraph_node *node,
2791 vec<cgraph_edge_p> *new_edges)
2793 struct cgraph_edge *e;
2794 bool res;
2796 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2798 for (e = node->callees; e; e = e->next_callee)
2799 if (!e->inline_failed)
2800 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2801 else
2802 update_jump_functions_after_inlining (cs, e);
2803 for (e = node->indirect_calls; e; e = e->next_callee)
2804 update_jump_functions_after_inlining (cs, e);
2806 return res;
2809 /* Combine two controlled uses counts as done during inlining. */
2811 static int
2812 combine_controlled_uses_counters (int c, int d)
2814 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
2815 return IPA_UNDESCRIBED_USE;
2816 else
2817 return c + d - 1;
2820 /* Propagate number of controlled users from CS->caleee to the new root of the
2821 tree of inlined nodes. */
2823 static void
2824 propagate_controlled_uses (struct cgraph_edge *cs)
2826 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
2827 struct cgraph_node *new_root = cs->caller->global.inlined_to
2828 ? cs->caller->global.inlined_to : cs->caller;
2829 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
2830 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
2831 int count, i;
2833 count = MIN (ipa_get_cs_argument_count (args),
2834 ipa_get_param_count (old_root_info));
2835 for (i = 0; i < count; i++)
2837 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2838 struct ipa_cst_ref_desc *rdesc;
2840 if (jf->type == IPA_JF_PASS_THROUGH)
2842 int src_idx, c, d;
2843 src_idx = ipa_get_jf_pass_through_formal_id (jf);
2844 c = ipa_get_controlled_uses (new_root_info, src_idx);
2845 d = ipa_get_controlled_uses (old_root_info, i);
2847 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
2848 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
2849 c = combine_controlled_uses_counters (c, d);
2850 ipa_set_controlled_uses (new_root_info, src_idx, c);
2851 if (c == 0 && new_root_info->ipcp_orig_node)
2853 struct cgraph_node *n;
2854 struct ipa_ref *ref;
2855 tree t = new_root_info->known_vals[src_idx];
2857 if (t && TREE_CODE (t) == ADDR_EXPR
2858 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2859 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
2860 && (ref = ipa_find_reference (new_root,
2861 n, NULL, 0)))
2863 if (dump_file)
2864 fprintf (dump_file, "ipa-prop: Removing cloning-created "
2865 "reference from %s/%i to %s/%i.\n",
2866 xstrdup (cgraph_node_name (new_root)),
2867 new_root->order,
2868 xstrdup (cgraph_node_name (n)), n->order);
2869 ipa_remove_reference (ref);
2873 else if (jf->type == IPA_JF_CONST
2874 && (rdesc = jfunc_rdesc_usable (jf)))
2876 int d = ipa_get_controlled_uses (old_root_info, i);
2877 int c = rdesc->refcount;
2878 rdesc->refcount = combine_controlled_uses_counters (c, d);
2879 if (rdesc->refcount == 0)
2881 tree cst = ipa_get_jf_constant (jf);
2882 struct cgraph_node *n;
2883 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
2884 && TREE_CODE (TREE_OPERAND (cst, 0))
2885 == FUNCTION_DECL);
2886 n = cgraph_get_node (TREE_OPERAND (cst, 0));
2887 if (n)
2889 struct cgraph_node *clone;
2890 bool ok;
2891 ok = remove_described_reference (n, rdesc);
2892 gcc_checking_assert (ok);
2894 clone = cs->caller;
2895 while (clone->global.inlined_to
2896 && clone != rdesc->cs->caller
2897 && IPA_NODE_REF (clone)->ipcp_orig_node)
2899 struct ipa_ref *ref;
2900 ref = ipa_find_reference (clone,
2901 n, NULL, 0);
2902 if (ref)
2904 if (dump_file)
2905 fprintf (dump_file, "ipa-prop: Removing "
2906 "cloning-created reference "
2907 "from %s/%i to %s/%i.\n",
2908 xstrdup (cgraph_node_name (clone)),
2909 clone->order,
2910 xstrdup (cgraph_node_name (n)),
2911 n->order);
2912 ipa_remove_reference (ref);
2914 clone = clone->callers->caller;
2921 for (i = ipa_get_param_count (old_root_info);
2922 i < ipa_get_cs_argument_count (args);
2923 i++)
2925 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2927 if (jf->type == IPA_JF_CONST)
2929 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
2930 if (rdesc)
2931 rdesc->refcount = IPA_UNDESCRIBED_USE;
2933 else if (jf->type == IPA_JF_PASS_THROUGH)
2934 ipa_set_controlled_uses (new_root_info,
2935 jf->value.pass_through.formal_id,
2936 IPA_UNDESCRIBED_USE);
2940 /* Update jump functions and call note functions on inlining the call site CS.
2941 CS is expected to lead to a node already cloned by
2942 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2943 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2944 created. */
2946 bool
2947 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2948 vec<cgraph_edge_p> *new_edges)
2950 bool changed;
2951 /* Do nothing if the preparation phase has not been carried out yet
2952 (i.e. during early inlining). */
2953 if (!ipa_node_params_vector.exists ())
2954 return false;
2955 gcc_assert (ipa_edge_args_vector);
2957 propagate_controlled_uses (cs);
2958 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2960 return changed;
2963 /* Frees all dynamically allocated structures that the argument info points
2964 to. */
2966 void
2967 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2969 vec_free (args->jump_functions);
2970 memset (args, 0, sizeof (*args));
2973 /* Free all ipa_edge structures. */
2975 void
2976 ipa_free_all_edge_args (void)
2978 int i;
2979 struct ipa_edge_args *args;
2981 if (!ipa_edge_args_vector)
2982 return;
2984 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2985 ipa_free_edge_args_substructures (args);
2987 vec_free (ipa_edge_args_vector);
2990 /* Frees all dynamically allocated structures that the param info points
2991 to. */
2993 void
2994 ipa_free_node_params_substructures (struct ipa_node_params *info)
2996 info->descriptors.release ();
2997 free (info->lattices);
2998 /* Lattice values and their sources are deallocated with their alocation
2999 pool. */
3000 info->known_vals.release ();
3001 memset (info, 0, sizeof (*info));
3004 /* Free all ipa_node_params structures. */
3006 void
3007 ipa_free_all_node_params (void)
3009 int i;
3010 struct ipa_node_params *info;
3012 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
3013 ipa_free_node_params_substructures (info);
3015 ipa_node_params_vector.release ();
3018 /* Set the aggregate replacements of NODE to be AGGVALS. */
3020 void
3021 ipa_set_node_agg_value_chain (struct cgraph_node *node,
3022 struct ipa_agg_replacement_value *aggvals)
3024 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
3025 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
3027 (*ipa_node_agg_replacements)[node->uid] = aggvals;
3030 /* Hook that is called by cgraph.c when an edge is removed. */
3032 static void
3033 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
3035 struct ipa_edge_args *args;
3037 /* During IPA-CP updating we can be called on not-yet analyzed clones. */
3038 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
3039 return;
3041 args = IPA_EDGE_REF (cs);
3042 if (args->jump_functions)
3044 struct ipa_jump_func *jf;
3045 int i;
3046 FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
3048 struct ipa_cst_ref_desc *rdesc;
3049 try_decrement_rdesc_refcount (jf);
3050 if (jf->type == IPA_JF_CONST
3051 && (rdesc = ipa_get_jf_constant_rdesc (jf))
3052 && rdesc->cs == cs)
3053 rdesc->cs = NULL;
3057 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
3060 /* Hook that is called by cgraph.c when a node is removed. */
3062 static void
3063 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3065 /* During IPA-CP updating we can be called on not-yet analyze clones. */
3066 if (ipa_node_params_vector.length () > (unsigned)node->uid)
3067 ipa_free_node_params_substructures (IPA_NODE_REF (node));
3068 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
3069 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
3072 /* Hook that is called by cgraph.c when an edge is duplicated. */
3074 static void
3075 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3076 __attribute__((unused)) void *data)
3078 struct ipa_edge_args *old_args, *new_args;
3079 unsigned int i;
3081 ipa_check_create_edge_args ();
3083 old_args = IPA_EDGE_REF (src);
3084 new_args = IPA_EDGE_REF (dst);
3086 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
3088 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
3090 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
3091 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
3093 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
3095 if (src_jf->type == IPA_JF_CONST)
3097 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
3099 if (!src_rdesc)
3100 dst_jf->value.constant.rdesc = NULL;
3101 else if (src->caller == dst->caller)
3103 struct ipa_ref *ref;
3104 symtab_node *n = cgraph_node_for_jfunc (src_jf);
3105 gcc_checking_assert (n);
3106 ref = ipa_find_reference (src->caller, n,
3107 src->call_stmt, src->lto_stmt_uid);
3108 gcc_checking_assert (ref);
3109 ipa_clone_ref (ref, dst->caller, ref->stmt);
3111 gcc_checking_assert (ipa_refdesc_pool);
3112 struct ipa_cst_ref_desc *dst_rdesc
3113 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3114 dst_rdesc->cs = dst;
3115 dst_rdesc->refcount = src_rdesc->refcount;
3116 dst_rdesc->next_duplicate = NULL;
3117 dst_jf->value.constant.rdesc = dst_rdesc;
3119 else if (src_rdesc->cs == src)
3121 struct ipa_cst_ref_desc *dst_rdesc;
3122 gcc_checking_assert (ipa_refdesc_pool);
3123 dst_rdesc
3124 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3125 dst_rdesc->cs = dst;
3126 dst_rdesc->refcount = src_rdesc->refcount;
3127 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
3128 src_rdesc->next_duplicate = dst_rdesc;
3129 dst_jf->value.constant.rdesc = dst_rdesc;
3131 else
3133 struct ipa_cst_ref_desc *dst_rdesc;
3134 /* This can happen during inlining, when a JFUNC can refer to a
3135 reference taken in a function up in the tree of inline clones.
3136 We need to find the duplicate that refers to our tree of
3137 inline clones. */
3139 gcc_assert (dst->caller->global.inlined_to);
3140 for (dst_rdesc = src_rdesc->next_duplicate;
3141 dst_rdesc;
3142 dst_rdesc = dst_rdesc->next_duplicate)
3144 struct cgraph_node *top;
3145 top = dst_rdesc->cs->caller->global.inlined_to
3146 ? dst_rdesc->cs->caller->global.inlined_to
3147 : dst_rdesc->cs->caller;
3148 if (dst->caller->global.inlined_to == top)
3149 break;
3151 gcc_assert (dst_rdesc);
3152 dst_jf->value.constant.rdesc = dst_rdesc;
3158 /* Hook that is called by cgraph.c when a node is duplicated. */
3160 static void
3161 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
3162 ATTRIBUTE_UNUSED void *data)
3164 struct ipa_node_params *old_info, *new_info;
3165 struct ipa_agg_replacement_value *old_av, *new_av;
3167 ipa_check_create_node_params ();
3168 old_info = IPA_NODE_REF (src);
3169 new_info = IPA_NODE_REF (dst);
3171 new_info->descriptors = old_info->descriptors.copy ();
3172 new_info->lattices = NULL;
3173 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
3175 new_info->uses_analysis_done = old_info->uses_analysis_done;
3176 new_info->node_enqueued = old_info->node_enqueued;
3178 old_av = ipa_get_agg_replacements_for_node (src);
3179 if (!old_av)
3180 return;
3182 new_av = NULL;
3183 while (old_av)
3185 struct ipa_agg_replacement_value *v;
3187 v = ggc_alloc_ipa_agg_replacement_value ();
3188 memcpy (v, old_av, sizeof (*v));
3189 v->next = new_av;
3190 new_av = v;
3191 old_av = old_av->next;
3193 ipa_set_node_agg_value_chain (dst, new_av);
3197 /* Analyze newly added function into callgraph. */
3199 static void
3200 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3202 ipa_analyze_node (node);
3205 /* Register our cgraph hooks if they are not already there. */
3207 void
3208 ipa_register_cgraph_hooks (void)
3210 if (!edge_removal_hook_holder)
3211 edge_removal_hook_holder =
3212 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
3213 if (!node_removal_hook_holder)
3214 node_removal_hook_holder =
3215 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
3216 if (!edge_duplication_hook_holder)
3217 edge_duplication_hook_holder =
3218 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
3219 if (!node_duplication_hook_holder)
3220 node_duplication_hook_holder =
3221 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
3222 function_insertion_hook_holder =
3223 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
3226 /* Unregister our cgraph hooks if they are not already there. */
3228 static void
3229 ipa_unregister_cgraph_hooks (void)
3231 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3232 edge_removal_hook_holder = NULL;
3233 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3234 node_removal_hook_holder = NULL;
3235 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3236 edge_duplication_hook_holder = NULL;
3237 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3238 node_duplication_hook_holder = NULL;
3239 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3240 function_insertion_hook_holder = NULL;
3243 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3244 longer needed after ipa-cp. */
3246 void
3247 ipa_free_all_structures_after_ipa_cp (void)
3249 if (!optimize)
3251 ipa_free_all_edge_args ();
3252 ipa_free_all_node_params ();
3253 free_alloc_pool (ipcp_sources_pool);
3254 free_alloc_pool (ipcp_values_pool);
3255 free_alloc_pool (ipcp_agg_lattice_pool);
3256 ipa_unregister_cgraph_hooks ();
3257 if (ipa_refdesc_pool)
3258 free_alloc_pool (ipa_refdesc_pool);
3262 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3263 longer needed after indirect inlining. */
3265 void
3266 ipa_free_all_structures_after_iinln (void)
3268 ipa_free_all_edge_args ();
3269 ipa_free_all_node_params ();
3270 ipa_unregister_cgraph_hooks ();
3271 if (ipcp_sources_pool)
3272 free_alloc_pool (ipcp_sources_pool);
3273 if (ipcp_values_pool)
3274 free_alloc_pool (ipcp_values_pool);
3275 if (ipcp_agg_lattice_pool)
3276 free_alloc_pool (ipcp_agg_lattice_pool);
3277 if (ipa_refdesc_pool)
3278 free_alloc_pool (ipa_refdesc_pool);
3281 /* Print ipa_tree_map data structures of all functions in the
3282 callgraph to F. */
3284 void
3285 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3287 int i, count;
3288 struct ipa_node_params *info;
3290 if (!node->definition)
3291 return;
3292 info = IPA_NODE_REF (node);
3293 fprintf (f, " function %s/%i parameter descriptors:\n",
3294 cgraph_node_name (node), node->order);
3295 count = ipa_get_param_count (info);
3296 for (i = 0; i < count; i++)
3298 int c;
3300 ipa_dump_param (f, info, i);
3301 if (ipa_is_param_used (info, i))
3302 fprintf (f, " used");
3303 c = ipa_get_controlled_uses (info, i);
3304 if (c == IPA_UNDESCRIBED_USE)
3305 fprintf (f, " undescribed_use");
3306 else
3307 fprintf (f, " controlled_uses=%i", c);
3308 fprintf (f, "\n");
3312 /* Print ipa_tree_map data structures of all functions in the
3313 callgraph to F. */
3315 void
3316 ipa_print_all_params (FILE * f)
3318 struct cgraph_node *node;
3320 fprintf (f, "\nFunction parameters:\n");
3321 FOR_EACH_FUNCTION (node)
3322 ipa_print_node_params (f, node);
3325 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3327 vec<tree>
3328 ipa_get_vector_of_formal_parms (tree fndecl)
3330 vec<tree> args;
3331 int count;
3332 tree parm;
3334 gcc_assert (!flag_wpa);
3335 count = count_formal_params (fndecl);
3336 args.create (count);
3337 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3338 args.quick_push (parm);
3340 return args;
3343 /* Return a heap allocated vector containing types of formal parameters of
3344 function type FNTYPE. */
3346 static inline vec<tree>
3347 get_vector_of_formal_parm_types (tree fntype)
3349 vec<tree> types;
3350 int count = 0;
3351 tree t;
3353 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3354 count++;
3356 types.create (count);
3357 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3358 types.quick_push (TREE_VALUE (t));
3360 return types;
3363 /* Modify the function declaration FNDECL and its type according to the plan in
3364 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3365 to reflect the actual parameters being modified which are determined by the
3366 base_index field. */
3368 void
3369 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
3370 const char *synth_parm_prefix)
3372 vec<tree> oparms, otypes;
3373 tree orig_type, new_type = NULL;
3374 tree old_arg_types, t, new_arg_types = NULL;
3375 tree parm, *link = &DECL_ARGUMENTS (fndecl);
3376 int i, len = adjustments.length ();
3377 tree new_reversed = NULL;
3378 bool care_for_types, last_parm_void;
3380 if (!synth_parm_prefix)
3381 synth_parm_prefix = "SYNTH";
3383 oparms = ipa_get_vector_of_formal_parms (fndecl);
3384 orig_type = TREE_TYPE (fndecl);
3385 old_arg_types = TYPE_ARG_TYPES (orig_type);
3387 /* The following test is an ugly hack, some functions simply don't have any
3388 arguments in their type. This is probably a bug but well... */
3389 care_for_types = (old_arg_types != NULL_TREE);
3390 if (care_for_types)
3392 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3393 == void_type_node);
3394 otypes = get_vector_of_formal_parm_types (orig_type);
3395 if (last_parm_void)
3396 gcc_assert (oparms.length () + 1 == otypes.length ());
3397 else
3398 gcc_assert (oparms.length () == otypes.length ());
3400 else
3402 last_parm_void = false;
3403 otypes.create (0);
3406 for (i = 0; i < len; i++)
3408 struct ipa_parm_adjustment *adj;
3409 gcc_assert (link);
3411 adj = &adjustments[i];
3412 parm = oparms[adj->base_index];
3413 adj->base = parm;
3415 if (adj->copy_param)
3417 if (care_for_types)
3418 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3419 new_arg_types);
3420 *link = parm;
3421 link = &DECL_CHAIN (parm);
3423 else if (!adj->remove_param)
3425 tree new_parm;
3426 tree ptype;
3428 if (adj->by_ref)
3429 ptype = build_pointer_type (adj->type);
3430 else
3431 ptype = adj->type;
3433 if (care_for_types)
3434 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3436 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3437 ptype);
3438 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
3440 DECL_ARTIFICIAL (new_parm) = 1;
3441 DECL_ARG_TYPE (new_parm) = ptype;
3442 DECL_CONTEXT (new_parm) = fndecl;
3443 TREE_USED (new_parm) = 1;
3444 DECL_IGNORED_P (new_parm) = 1;
3445 layout_decl (new_parm, 0);
3447 adj->base = parm;
3448 adj->reduction = new_parm;
3450 *link = new_parm;
3452 link = &DECL_CHAIN (new_parm);
3456 *link = NULL_TREE;
3458 if (care_for_types)
3460 new_reversed = nreverse (new_arg_types);
3461 if (last_parm_void)
3463 if (new_reversed)
3464 TREE_CHAIN (new_arg_types) = void_list_node;
3465 else
3466 new_reversed = void_list_node;
3470 /* Use copy_node to preserve as much as possible from original type
3471 (debug info, attribute lists etc.)
3472 Exception is METHOD_TYPEs must have THIS argument.
3473 When we are asked to remove it, we need to build new FUNCTION_TYPE
3474 instead. */
3475 if (TREE_CODE (orig_type) != METHOD_TYPE
3476 || (adjustments[0].copy_param
3477 && adjustments[0].base_index == 0))
3479 new_type = build_distinct_type_copy (orig_type);
3480 TYPE_ARG_TYPES (new_type) = new_reversed;
3482 else
3484 new_type
3485 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3486 new_reversed));
3487 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3488 DECL_VINDEX (fndecl) = NULL_TREE;
3491 /* When signature changes, we need to clear builtin info. */
3492 if (DECL_BUILT_IN (fndecl))
3494 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3495 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3498 /* This is a new type, not a copy of an old type. Need to reassociate
3499 variants. We can handle everything except the main variant lazily. */
3500 t = TYPE_MAIN_VARIANT (orig_type);
3501 if (orig_type != t)
3503 TYPE_MAIN_VARIANT (new_type) = t;
3504 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3505 TYPE_NEXT_VARIANT (t) = new_type;
3507 else
3509 TYPE_MAIN_VARIANT (new_type) = new_type;
3510 TYPE_NEXT_VARIANT (new_type) = NULL;
3513 TREE_TYPE (fndecl) = new_type;
3514 DECL_VIRTUAL_P (fndecl) = 0;
3515 otypes.release ();
3516 oparms.release ();
3519 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3520 If this is a directly recursive call, CS must be NULL. Otherwise it must
3521 contain the corresponding call graph edge. */
3523 void
3524 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3525 ipa_parm_adjustment_vec adjustments)
3527 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3528 vec<tree> vargs;
3529 vec<tree, va_gc> **debug_args = NULL;
3530 gimple new_stmt;
3531 gimple_stmt_iterator gsi, prev_gsi;
3532 tree callee_decl;
3533 int i, len;
3535 len = adjustments.length ();
3536 vargs.create (len);
3537 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
3538 ipa_remove_stmt_references (current_node, stmt);
3540 gsi = gsi_for_stmt (stmt);
3541 prev_gsi = gsi;
3542 gsi_prev (&prev_gsi);
3543 for (i = 0; i < len; i++)
3545 struct ipa_parm_adjustment *adj;
3547 adj = &adjustments[i];
3549 if (adj->copy_param)
3551 tree arg = gimple_call_arg (stmt, adj->base_index);
3553 vargs.quick_push (arg);
3555 else if (!adj->remove_param)
3557 tree expr, base, off;
3558 location_t loc;
3559 unsigned int deref_align = 0;
3560 bool deref_base = false;
3562 /* We create a new parameter out of the value of the old one, we can
3563 do the following kind of transformations:
3565 - A scalar passed by reference is converted to a scalar passed by
3566 value. (adj->by_ref is false and the type of the original
3567 actual argument is a pointer to a scalar).
3569 - A part of an aggregate is passed instead of the whole aggregate.
3570 The part can be passed either by value or by reference, this is
3571 determined by value of adj->by_ref. Moreover, the code below
3572 handles both situations when the original aggregate is passed by
3573 value (its type is not a pointer) and when it is passed by
3574 reference (it is a pointer to an aggregate).
3576 When the new argument is passed by reference (adj->by_ref is true)
3577 it must be a part of an aggregate and therefore we form it by
3578 simply taking the address of a reference inside the original
3579 aggregate. */
3581 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3582 base = gimple_call_arg (stmt, adj->base_index);
3583 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3584 : EXPR_LOCATION (base);
3586 if (TREE_CODE (base) != ADDR_EXPR
3587 && POINTER_TYPE_P (TREE_TYPE (base)))
3588 off = build_int_cst (adj->alias_ptr_type,
3589 adj->offset / BITS_PER_UNIT);
3590 else
3592 HOST_WIDE_INT base_offset;
3593 tree prev_base;
3594 bool addrof;
3596 if (TREE_CODE (base) == ADDR_EXPR)
3598 base = TREE_OPERAND (base, 0);
3599 addrof = true;
3601 else
3602 addrof = false;
3603 prev_base = base;
3604 base = get_addr_base_and_unit_offset (base, &base_offset);
3605 /* Aggregate arguments can have non-invariant addresses. */
3606 if (!base)
3608 base = build_fold_addr_expr (prev_base);
3609 off = build_int_cst (adj->alias_ptr_type,
3610 adj->offset / BITS_PER_UNIT);
3612 else if (TREE_CODE (base) == MEM_REF)
3614 if (!addrof)
3616 deref_base = true;
3617 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3619 off = build_int_cst (adj->alias_ptr_type,
3620 base_offset
3621 + adj->offset / BITS_PER_UNIT);
3622 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3623 off);
3624 base = TREE_OPERAND (base, 0);
3626 else
3628 off = build_int_cst (adj->alias_ptr_type,
3629 base_offset
3630 + adj->offset / BITS_PER_UNIT);
3631 base = build_fold_addr_expr (base);
3635 if (!adj->by_ref)
3637 tree type = adj->type;
3638 unsigned int align;
3639 unsigned HOST_WIDE_INT misalign;
3641 if (deref_base)
3643 align = deref_align;
3644 misalign = 0;
3646 else
3648 get_pointer_alignment_1 (base, &align, &misalign);
3649 if (TYPE_ALIGN (type) > align)
3650 align = TYPE_ALIGN (type);
3652 misalign += (tree_to_double_int (off)
3653 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3654 * BITS_PER_UNIT);
3655 misalign = misalign & (align - 1);
3656 if (misalign != 0)
3657 align = (misalign & -misalign);
3658 if (align < TYPE_ALIGN (type))
3659 type = build_aligned_type (type, align);
3660 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3662 else
3664 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3665 expr = build_fold_addr_expr (expr);
3668 expr = force_gimple_operand_gsi (&gsi, expr,
3669 adj->by_ref
3670 || is_gimple_reg_type (adj->type),
3671 NULL, true, GSI_SAME_STMT);
3672 vargs.quick_push (expr);
3674 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3676 unsigned int ix;
3677 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3678 gimple def_temp;
3680 arg = gimple_call_arg (stmt, adj->base_index);
3681 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3683 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3684 continue;
3685 arg = fold_convert_loc (gimple_location (stmt),
3686 TREE_TYPE (origin), arg);
3688 if (debug_args == NULL)
3689 debug_args = decl_debug_args_insert (callee_decl);
3690 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3691 if (ddecl == origin)
3693 ddecl = (**debug_args)[ix + 1];
3694 break;
3696 if (ddecl == NULL)
3698 ddecl = make_node (DEBUG_EXPR_DECL);
3699 DECL_ARTIFICIAL (ddecl) = 1;
3700 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3701 DECL_MODE (ddecl) = DECL_MODE (origin);
3703 vec_safe_push (*debug_args, origin);
3704 vec_safe_push (*debug_args, ddecl);
3706 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3707 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3711 if (dump_file && (dump_flags & TDF_DETAILS))
3713 fprintf (dump_file, "replacing stmt:");
3714 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3717 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3718 vargs.release ();
3719 if (gimple_call_lhs (stmt))
3720 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3722 gimple_set_block (new_stmt, gimple_block (stmt));
3723 if (gimple_has_location (stmt))
3724 gimple_set_location (new_stmt, gimple_location (stmt));
3725 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3726 gimple_call_copy_flags (new_stmt, stmt);
3728 if (dump_file && (dump_flags & TDF_DETAILS))
3730 fprintf (dump_file, "with stmt:");
3731 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3732 fprintf (dump_file, "\n");
3734 gsi_replace (&gsi, new_stmt, true);
3735 if (cs)
3736 cgraph_set_call_stmt (cs, new_stmt);
3739 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
3740 gsi_prev (&gsi);
3742 while ((gsi_end_p (prev_gsi) && !gsi_end_p (gsi))
3743 || (!gsi_end_p (prev_gsi) && gsi_stmt (gsi) == gsi_stmt (prev_gsi)));
3745 update_ssa (TODO_update_ssa);
3746 free_dominance_info (CDI_DOMINATORS);
3749 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3751 static bool
3752 index_in_adjustments_multiple_times_p (int base_index,
3753 ipa_parm_adjustment_vec adjustments)
3755 int i, len = adjustments.length ();
3756 bool one = false;
3758 for (i = 0; i < len; i++)
3760 struct ipa_parm_adjustment *adj;
3761 adj = &adjustments[i];
3763 if (adj->base_index == base_index)
3765 if (one)
3766 return true;
3767 else
3768 one = true;
3771 return false;
3775 /* Return adjustments that should have the same effect on function parameters
3776 and call arguments as if they were first changed according to adjustments in
3777 INNER and then by adjustments in OUTER. */
3779 ipa_parm_adjustment_vec
3780 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3781 ipa_parm_adjustment_vec outer)
3783 int i, outlen = outer.length ();
3784 int inlen = inner.length ();
3785 int removals = 0;
3786 ipa_parm_adjustment_vec adjustments, tmp;
3788 tmp.create (inlen);
3789 for (i = 0; i < inlen; i++)
3791 struct ipa_parm_adjustment *n;
3792 n = &inner[i];
3794 if (n->remove_param)
3795 removals++;
3796 else
3797 tmp.quick_push (*n);
3800 adjustments.create (outlen + removals);
3801 for (i = 0; i < outlen; i++)
3803 struct ipa_parm_adjustment r;
3804 struct ipa_parm_adjustment *out = &outer[i];
3805 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3807 memset (&r, 0, sizeof (r));
3808 gcc_assert (!in->remove_param);
3809 if (out->remove_param)
3811 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3813 r.remove_param = true;
3814 adjustments.quick_push (r);
3816 continue;
3819 r.base_index = in->base_index;
3820 r.type = out->type;
3822 /* FIXME: Create nonlocal value too. */
3824 if (in->copy_param && out->copy_param)
3825 r.copy_param = true;
3826 else if (in->copy_param)
3827 r.offset = out->offset;
3828 else if (out->copy_param)
3829 r.offset = in->offset;
3830 else
3831 r.offset = in->offset + out->offset;
3832 adjustments.quick_push (r);
3835 for (i = 0; i < inlen; i++)
3837 struct ipa_parm_adjustment *n = &inner[i];
3839 if (n->remove_param)
3840 adjustments.quick_push (*n);
3843 tmp.release ();
3844 return adjustments;
3847 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3848 friendly way, assuming they are meant to be applied to FNDECL. */
3850 void
3851 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3852 tree fndecl)
3854 int i, len = adjustments.length ();
3855 bool first = true;
3856 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3858 fprintf (file, "IPA param adjustments: ");
3859 for (i = 0; i < len; i++)
3861 struct ipa_parm_adjustment *adj;
3862 adj = &adjustments[i];
3864 if (!first)
3865 fprintf (file, " ");
3866 else
3867 first = false;
3869 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3870 print_generic_expr (file, parms[adj->base_index], 0);
3871 if (adj->base)
3873 fprintf (file, ", base: ");
3874 print_generic_expr (file, adj->base, 0);
3876 if (adj->reduction)
3878 fprintf (file, ", reduction: ");
3879 print_generic_expr (file, adj->reduction, 0);
3881 if (adj->new_ssa_base)
3883 fprintf (file, ", new_ssa_base: ");
3884 print_generic_expr (file, adj->new_ssa_base, 0);
3887 if (adj->copy_param)
3888 fprintf (file, ", copy_param");
3889 else if (adj->remove_param)
3890 fprintf (file, ", remove_param");
3891 else
3892 fprintf (file, ", offset %li", (long) adj->offset);
3893 if (adj->by_ref)
3894 fprintf (file, ", by_ref");
3895 print_node_brief (file, ", type: ", adj->type, 0);
3896 fprintf (file, "\n");
3898 parms.release ();
3901 /* Dump the AV linked list. */
3903 void
3904 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3906 bool comma = false;
3907 fprintf (f, " Aggregate replacements:");
3908 for (; av; av = av->next)
3910 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3911 av->index, av->offset);
3912 print_generic_expr (f, av->value, 0);
3913 comma = true;
3915 fprintf (f, "\n");
3918 /* Stream out jump function JUMP_FUNC to OB. */
3920 static void
3921 ipa_write_jump_function (struct output_block *ob,
3922 struct ipa_jump_func *jump_func)
3924 struct ipa_agg_jf_item *item;
3925 struct bitpack_d bp;
3926 int i, count;
3928 streamer_write_uhwi (ob, jump_func->type);
3929 switch (jump_func->type)
3931 case IPA_JF_UNKNOWN:
3932 break;
3933 case IPA_JF_KNOWN_TYPE:
3934 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3935 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3936 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3937 break;
3938 case IPA_JF_CONST:
3939 gcc_assert (
3940 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
3941 stream_write_tree (ob, jump_func->value.constant.value, true);
3942 break;
3943 case IPA_JF_PASS_THROUGH:
3944 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3945 if (jump_func->value.pass_through.operation == NOP_EXPR)
3947 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3948 bp = bitpack_create (ob->main_stream);
3949 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3950 bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1);
3951 streamer_write_bitpack (&bp);
3953 else
3955 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3956 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3958 break;
3959 case IPA_JF_ANCESTOR:
3960 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3961 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3962 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3963 bp = bitpack_create (ob->main_stream);
3964 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3965 bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1);
3966 streamer_write_bitpack (&bp);
3967 break;
3970 count = vec_safe_length (jump_func->agg.items);
3971 streamer_write_uhwi (ob, count);
3972 if (count)
3974 bp = bitpack_create (ob->main_stream);
3975 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3976 streamer_write_bitpack (&bp);
3979 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3981 streamer_write_uhwi (ob, item->offset);
3982 stream_write_tree (ob, item->value, true);
3986 /* Read in jump function JUMP_FUNC from IB. */
3988 static void
3989 ipa_read_jump_function (struct lto_input_block *ib,
3990 struct ipa_jump_func *jump_func,
3991 struct cgraph_edge *cs,
3992 struct data_in *data_in)
3994 enum jump_func_type jftype;
3995 enum tree_code operation;
3996 int i, count;
3998 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
3999 switch (jftype)
4001 case IPA_JF_UNKNOWN:
4002 jump_func->type = IPA_JF_UNKNOWN;
4003 break;
4004 case IPA_JF_KNOWN_TYPE:
4006 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4007 tree base_type = stream_read_tree (ib, data_in);
4008 tree component_type = stream_read_tree (ib, data_in);
4010 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
4011 break;
4013 case IPA_JF_CONST:
4014 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
4015 break;
4016 case IPA_JF_PASS_THROUGH:
4017 operation = (enum tree_code) streamer_read_uhwi (ib);
4018 if (operation == NOP_EXPR)
4020 int formal_id = streamer_read_uhwi (ib);
4021 struct bitpack_d bp = streamer_read_bitpack (ib);
4022 bool agg_preserved = bp_unpack_value (&bp, 1);
4023 bool type_preserved = bp_unpack_value (&bp, 1);
4024 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved,
4025 type_preserved);
4027 else
4029 tree operand = stream_read_tree (ib, data_in);
4030 int formal_id = streamer_read_uhwi (ib);
4031 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
4032 operation);
4034 break;
4035 case IPA_JF_ANCESTOR:
4037 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4038 tree type = stream_read_tree (ib, data_in);
4039 int formal_id = streamer_read_uhwi (ib);
4040 struct bitpack_d bp = streamer_read_bitpack (ib);
4041 bool agg_preserved = bp_unpack_value (&bp, 1);
4042 bool type_preserved = bp_unpack_value (&bp, 1);
4044 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved,
4045 type_preserved);
4046 break;
4050 count = streamer_read_uhwi (ib);
4051 vec_alloc (jump_func->agg.items, count);
4052 if (count)
4054 struct bitpack_d bp = streamer_read_bitpack (ib);
4055 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
4057 for (i = 0; i < count; i++)
4059 struct ipa_agg_jf_item item;
4060 item.offset = streamer_read_uhwi (ib);
4061 item.value = stream_read_tree (ib, data_in);
4062 jump_func->agg.items->quick_push (item);
4066 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
4067 relevant to indirect inlining to OB. */
4069 static void
4070 ipa_write_indirect_edge_info (struct output_block *ob,
4071 struct cgraph_edge *cs)
4073 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4074 struct bitpack_d bp;
4076 streamer_write_hwi (ob, ii->param_index);
4077 streamer_write_hwi (ob, ii->offset);
4078 bp = bitpack_create (ob->main_stream);
4079 bp_pack_value (&bp, ii->polymorphic, 1);
4080 bp_pack_value (&bp, ii->agg_contents, 1);
4081 bp_pack_value (&bp, ii->member_ptr, 1);
4082 bp_pack_value (&bp, ii->by_ref, 1);
4083 streamer_write_bitpack (&bp);
4085 if (ii->polymorphic)
4087 streamer_write_hwi (ob, ii->otr_token);
4088 stream_write_tree (ob, ii->otr_type, true);
4092 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
4093 relevant to indirect inlining from IB. */
4095 static void
4096 ipa_read_indirect_edge_info (struct lto_input_block *ib,
4097 struct data_in *data_in ATTRIBUTE_UNUSED,
4098 struct cgraph_edge *cs)
4100 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4101 struct bitpack_d bp;
4103 ii->param_index = (int) streamer_read_hwi (ib);
4104 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
4105 bp = streamer_read_bitpack (ib);
4106 ii->polymorphic = bp_unpack_value (&bp, 1);
4107 ii->agg_contents = bp_unpack_value (&bp, 1);
4108 ii->member_ptr = bp_unpack_value (&bp, 1);
4109 ii->by_ref = bp_unpack_value (&bp, 1);
4110 if (ii->polymorphic)
4112 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
4113 ii->otr_type = stream_read_tree (ib, data_in);
4117 /* Stream out NODE info to OB. */
4119 static void
4120 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
4122 int node_ref;
4123 lto_symtab_encoder_t encoder;
4124 struct ipa_node_params *info = IPA_NODE_REF (node);
4125 int j;
4126 struct cgraph_edge *e;
4127 struct bitpack_d bp;
4129 encoder = ob->decl_state->symtab_node_encoder;
4130 node_ref = lto_symtab_encoder_encode (encoder, node);
4131 streamer_write_uhwi (ob, node_ref);
4133 streamer_write_uhwi (ob, ipa_get_param_count (info));
4134 for (j = 0; j < ipa_get_param_count (info); j++)
4135 streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
4136 bp = bitpack_create (ob->main_stream);
4137 gcc_assert (info->uses_analysis_done
4138 || ipa_get_param_count (info) == 0);
4139 gcc_assert (!info->node_enqueued);
4140 gcc_assert (!info->ipcp_orig_node);
4141 for (j = 0; j < ipa_get_param_count (info); j++)
4142 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
4143 streamer_write_bitpack (&bp);
4144 for (j = 0; j < ipa_get_param_count (info); j++)
4145 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
4146 for (e = node->callees; e; e = e->next_callee)
4148 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4150 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4151 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4152 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4154 for (e = node->indirect_calls; e; e = e->next_callee)
4156 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4158 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4159 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4160 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4161 ipa_write_indirect_edge_info (ob, e);
4165 /* Stream in NODE info from IB. */
4167 static void
4168 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
4169 struct data_in *data_in)
4171 struct ipa_node_params *info = IPA_NODE_REF (node);
4172 int k;
4173 struct cgraph_edge *e;
4174 struct bitpack_d bp;
4176 ipa_alloc_node_params (node, streamer_read_uhwi (ib));
4178 for (k = 0; k < ipa_get_param_count (info); k++)
4179 info->descriptors[k].move_cost = streamer_read_uhwi (ib);
4181 bp = streamer_read_bitpack (ib);
4182 if (ipa_get_param_count (info) != 0)
4183 info->uses_analysis_done = true;
4184 info->node_enqueued = false;
4185 for (k = 0; k < ipa_get_param_count (info); k++)
4186 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
4187 for (k = 0; k < ipa_get_param_count (info); k++)
4188 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
4189 for (e = node->callees; e; e = e->next_callee)
4191 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4192 int count = streamer_read_uhwi (ib);
4194 if (!count)
4195 continue;
4196 vec_safe_grow_cleared (args->jump_functions, count);
4198 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4199 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4200 data_in);
4202 for (e = node->indirect_calls; e; e = e->next_callee)
4204 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4205 int count = streamer_read_uhwi (ib);
4207 if (count)
4209 vec_safe_grow_cleared (args->jump_functions, count);
4210 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4211 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4212 data_in);
4214 ipa_read_indirect_edge_info (ib, data_in, e);
4218 /* Write jump functions for nodes in SET. */
4220 void
4221 ipa_prop_write_jump_functions (void)
4223 struct cgraph_node *node;
4224 struct output_block *ob;
4225 unsigned int count = 0;
4226 lto_symtab_encoder_iterator lsei;
4227 lto_symtab_encoder_t encoder;
4230 if (!ipa_node_params_vector.exists ())
4231 return;
4233 ob = create_output_block (LTO_section_jump_functions);
4234 encoder = ob->decl_state->symtab_node_encoder;
4235 ob->cgraph_node = NULL;
4236 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4237 lsei_next_function_in_partition (&lsei))
4239 node = lsei_cgraph_node (lsei);
4240 if (cgraph_function_with_gimple_body_p (node)
4241 && IPA_NODE_REF (node) != NULL)
4242 count++;
4245 streamer_write_uhwi (ob, count);
4247 /* Process all of the functions. */
4248 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4249 lsei_next_function_in_partition (&lsei))
4251 node = lsei_cgraph_node (lsei);
4252 if (cgraph_function_with_gimple_body_p (node)
4253 && IPA_NODE_REF (node) != NULL)
4254 ipa_write_node_info (ob, node);
4256 streamer_write_char_stream (ob->main_stream, 0);
4257 produce_asm (ob, NULL);
4258 destroy_output_block (ob);
4261 /* Read section in file FILE_DATA of length LEN with data DATA. */
4263 static void
4264 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
4265 size_t len)
4267 const struct lto_function_header *header =
4268 (const struct lto_function_header *) data;
4269 const int cfg_offset = sizeof (struct lto_function_header);
4270 const int main_offset = cfg_offset + header->cfg_size;
4271 const int string_offset = main_offset + header->main_size;
4272 struct data_in *data_in;
4273 struct lto_input_block ib_main;
4274 unsigned int i;
4275 unsigned int count;
4277 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4278 header->main_size);
4280 data_in =
4281 lto_data_in_create (file_data, (const char *) data + string_offset,
4282 header->string_size, vNULL);
4283 count = streamer_read_uhwi (&ib_main);
4285 for (i = 0; i < count; i++)
4287 unsigned int index;
4288 struct cgraph_node *node;
4289 lto_symtab_encoder_t encoder;
4291 index = streamer_read_uhwi (&ib_main);
4292 encoder = file_data->symtab_node_encoder;
4293 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4294 gcc_assert (node->definition);
4295 ipa_read_node_info (&ib_main, node, data_in);
4297 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4298 len);
4299 lto_data_in_delete (data_in);
4302 /* Read ipcp jump functions. */
4304 void
4305 ipa_prop_read_jump_functions (void)
4307 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4308 struct lto_file_decl_data *file_data;
4309 unsigned int j = 0;
4311 ipa_check_create_node_params ();
4312 ipa_check_create_edge_args ();
4313 ipa_register_cgraph_hooks ();
4315 while ((file_data = file_data_vec[j++]))
4317 size_t len;
4318 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4320 if (data)
4321 ipa_prop_read_section (file_data, data, len);
4325 /* After merging units, we can get mismatch in argument counts.
4326 Also decl merging might've rendered parameter lists obsolete.
4327 Also compute called_with_variable_arg info. */
4329 void
4330 ipa_update_after_lto_read (void)
4332 ipa_check_create_node_params ();
4333 ipa_check_create_edge_args ();
4336 void
4337 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4339 int node_ref;
4340 unsigned int count = 0;
4341 lto_symtab_encoder_t encoder;
4342 struct ipa_agg_replacement_value *aggvals, *av;
4344 aggvals = ipa_get_agg_replacements_for_node (node);
4345 encoder = ob->decl_state->symtab_node_encoder;
4346 node_ref = lto_symtab_encoder_encode (encoder, node);
4347 streamer_write_uhwi (ob, node_ref);
4349 for (av = aggvals; av; av = av->next)
4350 count++;
4351 streamer_write_uhwi (ob, count);
4353 for (av = aggvals; av; av = av->next)
4355 struct bitpack_d bp;
4357 streamer_write_uhwi (ob, av->offset);
4358 streamer_write_uhwi (ob, av->index);
4359 stream_write_tree (ob, av->value, true);
4361 bp = bitpack_create (ob->main_stream);
4362 bp_pack_value (&bp, av->by_ref, 1);
4363 streamer_write_bitpack (&bp);
4367 /* Stream in the aggregate value replacement chain for NODE from IB. */
4369 static void
4370 read_agg_replacement_chain (struct lto_input_block *ib,
4371 struct cgraph_node *node,
4372 struct data_in *data_in)
4374 struct ipa_agg_replacement_value *aggvals = NULL;
4375 unsigned int count, i;
4377 count = streamer_read_uhwi (ib);
4378 for (i = 0; i <count; i++)
4380 struct ipa_agg_replacement_value *av;
4381 struct bitpack_d bp;
4383 av = ggc_alloc_ipa_agg_replacement_value ();
4384 av->offset = streamer_read_uhwi (ib);
4385 av->index = streamer_read_uhwi (ib);
4386 av->value = stream_read_tree (ib, data_in);
4387 bp = streamer_read_bitpack (ib);
4388 av->by_ref = bp_unpack_value (&bp, 1);
4389 av->next = aggvals;
4390 aggvals = av;
4392 ipa_set_node_agg_value_chain (node, aggvals);
4395 /* Write all aggregate replacement for nodes in set. */
4397 void
4398 ipa_prop_write_all_agg_replacement (void)
4400 struct cgraph_node *node;
4401 struct output_block *ob;
4402 unsigned int count = 0;
4403 lto_symtab_encoder_iterator lsei;
4404 lto_symtab_encoder_t encoder;
4406 if (!ipa_node_agg_replacements)
4407 return;
4409 ob = create_output_block (LTO_section_ipcp_transform);
4410 encoder = ob->decl_state->symtab_node_encoder;
4411 ob->cgraph_node = NULL;
4412 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4413 lsei_next_function_in_partition (&lsei))
4415 node = lsei_cgraph_node (lsei);
4416 if (cgraph_function_with_gimple_body_p (node)
4417 && ipa_get_agg_replacements_for_node (node) != NULL)
4418 count++;
4421 streamer_write_uhwi (ob, count);
4423 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4424 lsei_next_function_in_partition (&lsei))
4426 node = lsei_cgraph_node (lsei);
4427 if (cgraph_function_with_gimple_body_p (node)
4428 && ipa_get_agg_replacements_for_node (node) != NULL)
4429 write_agg_replacement_chain (ob, node);
4431 streamer_write_char_stream (ob->main_stream, 0);
4432 produce_asm (ob, NULL);
4433 destroy_output_block (ob);
4436 /* Read replacements section in file FILE_DATA of length LEN with data
4437 DATA. */
4439 static void
4440 read_replacements_section (struct lto_file_decl_data *file_data,
4441 const char *data,
4442 size_t len)
4444 const struct lto_function_header *header =
4445 (const struct lto_function_header *) data;
4446 const int cfg_offset = sizeof (struct lto_function_header);
4447 const int main_offset = cfg_offset + header->cfg_size;
4448 const int string_offset = main_offset + header->main_size;
4449 struct data_in *data_in;
4450 struct lto_input_block ib_main;
4451 unsigned int i;
4452 unsigned int count;
4454 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4455 header->main_size);
4457 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4458 header->string_size, vNULL);
4459 count = streamer_read_uhwi (&ib_main);
4461 for (i = 0; i < count; i++)
4463 unsigned int index;
4464 struct cgraph_node *node;
4465 lto_symtab_encoder_t encoder;
4467 index = streamer_read_uhwi (&ib_main);
4468 encoder = file_data->symtab_node_encoder;
4469 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4470 gcc_assert (node->definition);
4471 read_agg_replacement_chain (&ib_main, node, data_in);
4473 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4474 len);
4475 lto_data_in_delete (data_in);
4478 /* Read IPA-CP aggregate replacements. */
4480 void
4481 ipa_prop_read_all_agg_replacement (void)
4483 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4484 struct lto_file_decl_data *file_data;
4485 unsigned int j = 0;
4487 while ((file_data = file_data_vec[j++]))
4489 size_t len;
4490 const char *data = lto_get_section_data (file_data,
4491 LTO_section_ipcp_transform,
4492 NULL, &len);
4493 if (data)
4494 read_replacements_section (file_data, data, len);
4498 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4499 NODE. */
4501 static void
4502 adjust_agg_replacement_values (struct cgraph_node *node,
4503 struct ipa_agg_replacement_value *aggval)
4505 struct ipa_agg_replacement_value *v;
4506 int i, c = 0, d = 0, *adj;
4508 if (!node->clone.combined_args_to_skip)
4509 return;
4511 for (v = aggval; v; v = v->next)
4513 gcc_assert (v->index >= 0);
4514 if (c < v->index)
4515 c = v->index;
4517 c++;
4519 adj = XALLOCAVEC (int, c);
4520 for (i = 0; i < c; i++)
4521 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4523 adj[i] = -1;
4524 d++;
4526 else
4527 adj[i] = i - d;
4529 for (v = aggval; v; v = v->next)
4530 v->index = adj[v->index];
4534 /* Function body transformation phase. */
4536 unsigned int
4537 ipcp_transform_function (struct cgraph_node *node)
4539 vec<ipa_param_descriptor_t> descriptors = vNULL;
4540 struct param_analysis_info *parms_ainfo;
4541 struct ipa_agg_replacement_value *aggval;
4542 gimple_stmt_iterator gsi;
4543 basic_block bb;
4544 int param_count;
4545 bool cfg_changed = false, something_changed = false;
4547 gcc_checking_assert (cfun);
4548 gcc_checking_assert (current_function_decl);
4550 if (dump_file)
4551 fprintf (dump_file, "Modification phase of node %s/%i\n",
4552 cgraph_node_name (node), node->order);
4554 aggval = ipa_get_agg_replacements_for_node (node);
4555 if (!aggval)
4556 return 0;
4557 param_count = count_formal_params (node->decl);
4558 if (param_count == 0)
4559 return 0;
4560 adjust_agg_replacement_values (node, aggval);
4561 if (dump_file)
4562 ipa_dump_agg_replacement_values (dump_file, aggval);
4563 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
4564 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
4565 descriptors.safe_grow_cleared (param_count);
4566 ipa_populate_param_decls (node, descriptors);
4568 FOR_EACH_BB (bb)
4569 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4571 struct ipa_agg_replacement_value *v;
4572 gimple stmt = gsi_stmt (gsi);
4573 tree rhs, val, t;
4574 HOST_WIDE_INT offset, size;
4575 int index;
4576 bool by_ref, vce;
4578 if (!gimple_assign_load_p (stmt))
4579 continue;
4580 rhs = gimple_assign_rhs1 (stmt);
4581 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
4582 continue;
4584 vce = false;
4585 t = rhs;
4586 while (handled_component_p (t))
4588 /* V_C_E can do things like convert an array of integers to one
4589 bigger integer and similar things we do not handle below. */
4590 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
4592 vce = true;
4593 break;
4595 t = TREE_OPERAND (t, 0);
4597 if (vce)
4598 continue;
4600 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
4601 rhs, &index, &offset, &size, &by_ref))
4602 continue;
4603 for (v = aggval; v; v = v->next)
4604 if (v->index == index
4605 && v->offset == offset)
4606 break;
4607 if (!v
4608 || v->by_ref != by_ref
4609 || tree_low_cst (TYPE_SIZE (TREE_TYPE (v->value)), 0) != size)
4610 continue;
4612 gcc_checking_assert (is_gimple_ip_invariant (v->value));
4613 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
4615 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
4616 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
4617 else if (TYPE_SIZE (TREE_TYPE (rhs))
4618 == TYPE_SIZE (TREE_TYPE (v->value)))
4619 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
4620 else
4622 if (dump_file)
4624 fprintf (dump_file, " const ");
4625 print_generic_expr (dump_file, v->value, 0);
4626 fprintf (dump_file, " can't be converted to type of ");
4627 print_generic_expr (dump_file, rhs, 0);
4628 fprintf (dump_file, "\n");
4630 continue;
4633 else
4634 val = v->value;
4636 if (dump_file && (dump_flags & TDF_DETAILS))
4638 fprintf (dump_file, "Modifying stmt:\n ");
4639 print_gimple_stmt (dump_file, stmt, 0, 0);
4641 gimple_assign_set_rhs_from_tree (&gsi, val);
4642 update_stmt (stmt);
4644 if (dump_file && (dump_flags & TDF_DETAILS))
4646 fprintf (dump_file, "into:\n ");
4647 print_gimple_stmt (dump_file, stmt, 0, 0);
4648 fprintf (dump_file, "\n");
4651 something_changed = true;
4652 if (maybe_clean_eh_stmt (stmt)
4653 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4654 cfg_changed = true;
4657 (*ipa_node_agg_replacements)[node->uid] = NULL;
4658 free_parms_ainfo (parms_ainfo, param_count);
4659 descriptors.release ();
4661 if (!something_changed)
4662 return 0;
4663 else if (cfg_changed)
4664 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
4665 else
4666 return TODO_update_ssa_only_virtuals;