Daily bump.
[official-gcc.git] / gcc / ipa-prop.c
blobc3890f74bfb37ca75a83ef6bd8a2950e596b5112
1 /* Interprocedural analyses.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "basic-block.h"
25 #include "tree-ssa-alias.h"
26 #include "internal-fn.h"
27 #include "gimple-fold.h"
28 #include "tree-eh.h"
29 #include "gimple-expr.h"
30 #include "is-a.h"
31 #include "gimple.h"
32 #include "expr.h"
33 #include "stor-layout.h"
34 #include "print-tree.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimplify-me.h"
38 #include "gimple-walk.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "ipa-prop.h"
42 #include "bitmap.h"
43 #include "gimple-ssa.h"
44 #include "tree-cfg.h"
45 #include "tree-phinodes.h"
46 #include "ssa-iterators.h"
47 #include "tree-into-ssa.h"
48 #include "tree-dfa.h"
49 #include "tree-pass.h"
50 #include "tree-inline.h"
51 #include "ipa-inline.h"
52 #include "flags.h"
53 #include "diagnostic.h"
54 #include "gimple-pretty-print.h"
55 #include "lto-streamer.h"
56 #include "data-streamer.h"
57 #include "tree-streamer.h"
58 #include "params.h"
59 #include "ipa-utils.h"
60 #include "stringpool.h"
61 #include "tree-ssanames.h"
63 /* Intermediate information about a parameter that is only useful during the
64 run of ipa_analyze_node and is not kept afterwards. */
66 struct param_analysis_info
68 bool parm_modified, ref_modified, pt_modified;
69 bitmap parm_visited_statements, pt_visited_statements;
72 /* Vector where the parameter infos are actually stored. */
73 vec<ipa_node_params> ipa_node_params_vector;
74 /* Vector of known aggregate values in cloned nodes. */
75 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
76 /* Vector where the parameter infos are actually stored. */
77 vec<ipa_edge_args, va_gc> *ipa_edge_args_vector;
79 /* Holders of ipa cgraph hooks: */
80 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
81 static struct cgraph_node_hook_list *node_removal_hook_holder;
82 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
83 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
84 static struct cgraph_node_hook_list *function_insertion_hook_holder;
86 /* Description of a reference to an IPA constant. */
87 struct ipa_cst_ref_desc
89 /* Edge that corresponds to the statement which took the reference. */
90 struct cgraph_edge *cs;
91 /* Linked list of duplicates created when call graph edges are cloned. */
92 struct ipa_cst_ref_desc *next_duplicate;
93 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
94 if out of control. */
95 int refcount;
98 /* Allocation pool for reference descriptions. */
100 static alloc_pool ipa_refdesc_pool;
102 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
103 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
105 static bool
106 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
108 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
109 struct cl_optimization *os;
111 if (!fs_opts)
112 return false;
113 os = TREE_OPTIMIZATION (fs_opts);
114 return !os->x_optimize || !os->x_flag_ipa_cp;
117 /* Return index of the formal whose tree is PTREE in function which corresponds
118 to INFO. */
120 static int
121 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor> descriptors, tree ptree)
123 int i, count;
125 count = descriptors.length ();
126 for (i = 0; i < count; i++)
127 if (descriptors[i].decl == ptree)
128 return i;
130 return -1;
133 /* Return index of the formal whose tree is PTREE in function which corresponds
134 to INFO. */
137 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
139 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
142 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
143 NODE. */
145 static void
146 ipa_populate_param_decls (struct cgraph_node *node,
147 vec<ipa_param_descriptor> &descriptors)
149 tree fndecl;
150 tree fnargs;
151 tree parm;
152 int param_num;
154 fndecl = node->decl;
155 gcc_assert (gimple_has_body_p (fndecl));
156 fnargs = DECL_ARGUMENTS (fndecl);
157 param_num = 0;
158 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
160 descriptors[param_num].decl = parm;
161 descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm));
162 param_num++;
166 /* Return how many formal parameters FNDECL has. */
168 static inline int
169 count_formal_params (tree fndecl)
171 tree parm;
172 int count = 0;
173 gcc_assert (gimple_has_body_p (fndecl));
175 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
176 count++;
178 return count;
181 /* Return the declaration of Ith formal parameter of the function corresponding
182 to INFO. Note there is no setter function as this array is built just once
183 using ipa_initialize_node_params. */
185 void
186 ipa_dump_param (FILE *file, struct ipa_node_params *info, int i)
188 fprintf (file, "param #%i", i);
189 if (info->descriptors[i].decl)
191 fprintf (file, " ");
192 print_generic_expr (file, info->descriptors[i].decl, 0);
196 /* Initialize the ipa_node_params structure associated with NODE
197 to hold PARAM_COUNT parameters. */
199 void
200 ipa_alloc_node_params (struct cgraph_node *node, int param_count)
202 struct ipa_node_params *info = IPA_NODE_REF (node);
204 if (!info->descriptors.exists () && param_count)
205 info->descriptors.safe_grow_cleared (param_count);
208 /* Initialize the ipa_node_params structure associated with NODE by counting
209 the function parameters, creating the descriptors and populating their
210 param_decls. */
212 void
213 ipa_initialize_node_params (struct cgraph_node *node)
215 struct ipa_node_params *info = IPA_NODE_REF (node);
217 if (!info->descriptors.exists ())
219 ipa_alloc_node_params (node, count_formal_params (node->decl));
220 ipa_populate_param_decls (node, info->descriptors);
224 /* Print the jump functions associated with call graph edge CS to file F. */
226 static void
227 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
229 int i, count;
231 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
232 for (i = 0; i < count; i++)
234 struct ipa_jump_func *jump_func;
235 enum jump_func_type type;
237 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
238 type = jump_func->type;
240 fprintf (f, " param %d: ", i);
241 if (type == IPA_JF_UNKNOWN)
242 fprintf (f, "UNKNOWN\n");
243 else if (type == IPA_JF_KNOWN_TYPE)
245 fprintf (f, "KNOWN TYPE: base ");
246 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
247 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
248 jump_func->value.known_type.offset);
249 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
250 fprintf (f, "\n");
252 else if (type == IPA_JF_CONST)
254 tree val = jump_func->value.constant.value;
255 fprintf (f, "CONST: ");
256 print_generic_expr (f, val, 0);
257 if (TREE_CODE (val) == ADDR_EXPR
258 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
260 fprintf (f, " -> ");
261 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
264 fprintf (f, "\n");
266 else if (type == IPA_JF_PASS_THROUGH)
268 fprintf (f, "PASS THROUGH: ");
269 fprintf (f, "%d, op %s",
270 jump_func->value.pass_through.formal_id,
271 get_tree_code_name(jump_func->value.pass_through.operation));
272 if (jump_func->value.pass_through.operation != NOP_EXPR)
274 fprintf (f, " ");
275 print_generic_expr (f,
276 jump_func->value.pass_through.operand, 0);
278 if (jump_func->value.pass_through.agg_preserved)
279 fprintf (f, ", agg_preserved");
280 if (jump_func->value.pass_through.type_preserved)
281 fprintf (f, ", type_preserved");
282 fprintf (f, "\n");
284 else if (type == IPA_JF_ANCESTOR)
286 fprintf (f, "ANCESTOR: ");
287 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
288 jump_func->value.ancestor.formal_id,
289 jump_func->value.ancestor.offset);
290 print_generic_expr (f, jump_func->value.ancestor.type, 0);
291 if (jump_func->value.ancestor.agg_preserved)
292 fprintf (f, ", agg_preserved");
293 if (jump_func->value.ancestor.type_preserved)
294 fprintf (f, ", type_preserved");
295 fprintf (f, "\n");
298 if (jump_func->agg.items)
300 struct ipa_agg_jf_item *item;
301 int j;
303 fprintf (f, " Aggregate passed by %s:\n",
304 jump_func->agg.by_ref ? "reference" : "value");
305 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
307 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
308 item->offset);
309 if (TYPE_P (item->value))
310 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
311 tree_to_uhwi (TYPE_SIZE (item->value)));
312 else
314 fprintf (f, "cst: ");
315 print_generic_expr (f, item->value, 0);
317 fprintf (f, "\n");
324 /* Print the jump functions of all arguments on all call graph edges going from
325 NODE to file F. */
327 void
328 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
330 struct cgraph_edge *cs;
332 fprintf (f, " Jump functions of caller %s/%i:\n", node->name (),
333 node->order);
334 for (cs = node->callees; cs; cs = cs->next_callee)
336 if (!ipa_edge_args_info_available_for_edge_p (cs))
337 continue;
339 fprintf (f, " callsite %s/%i -> %s/%i : \n",
340 xstrdup (node->name ()), node->order,
341 xstrdup (cs->callee->name ()),
342 cs->callee->order);
343 ipa_print_node_jump_functions_for_edge (f, cs);
346 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
348 struct cgraph_indirect_call_info *ii;
349 if (!ipa_edge_args_info_available_for_edge_p (cs))
350 continue;
352 ii = cs->indirect_info;
353 if (ii->agg_contents)
354 fprintf (f, " indirect %s callsite, calling param %i, "
355 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
356 ii->member_ptr ? "member ptr" : "aggregate",
357 ii->param_index, ii->offset,
358 ii->by_ref ? "by reference" : "by_value");
359 else
360 fprintf (f, " indirect %s callsite, calling param %i, "
361 "offset " HOST_WIDE_INT_PRINT_DEC,
362 ii->polymorphic ? "polymorphic" : "simple", ii->param_index,
363 ii->offset);
365 if (cs->call_stmt)
367 fprintf (f, ", for stmt ");
368 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
370 else
371 fprintf (f, "\n");
372 ipa_print_node_jump_functions_for_edge (f, cs);
376 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
378 void
379 ipa_print_all_jump_functions (FILE *f)
381 struct cgraph_node *node;
383 fprintf (f, "\nJump functions:\n");
384 FOR_EACH_FUNCTION (node)
386 ipa_print_node_jump_functions (f, node);
390 /* Set JFUNC to be a known type jump function. */
392 static void
393 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
394 tree base_type, tree component_type)
396 gcc_assert (TREE_CODE (component_type) == RECORD_TYPE
397 && TYPE_BINFO (component_type));
398 if (!flag_devirtualize)
399 return;
400 gcc_assert (BINFO_VTABLE (TYPE_BINFO (component_type)));
401 jfunc->type = IPA_JF_KNOWN_TYPE;
402 jfunc->value.known_type.offset = offset,
403 jfunc->value.known_type.base_type = base_type;
404 jfunc->value.known_type.component_type = component_type;
405 gcc_assert (component_type);
408 /* Set JFUNC to be a copy of another jmp (to be used by jump function
409 combination code). The two functions will share their rdesc. */
411 static void
412 ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
413 struct ipa_jump_func *src)
416 gcc_checking_assert (src->type == IPA_JF_CONST);
417 dst->type = IPA_JF_CONST;
418 dst->value.constant = src->value.constant;
421 /* Set JFUNC to be a constant jmp function. */
423 static void
424 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
425 struct cgraph_edge *cs)
427 constant = unshare_expr (constant);
428 if (constant && EXPR_P (constant))
429 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
430 jfunc->type = IPA_JF_CONST;
431 jfunc->value.constant.value = unshare_expr_without_location (constant);
433 if (TREE_CODE (constant) == ADDR_EXPR
434 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
436 struct ipa_cst_ref_desc *rdesc;
437 if (!ipa_refdesc_pool)
438 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
439 sizeof (struct ipa_cst_ref_desc), 32);
441 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
442 rdesc->cs = cs;
443 rdesc->next_duplicate = NULL;
444 rdesc->refcount = 1;
445 jfunc->value.constant.rdesc = rdesc;
447 else
448 jfunc->value.constant.rdesc = NULL;
451 /* Set JFUNC to be a simple pass-through jump function. */
452 static void
453 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
454 bool agg_preserved, bool type_preserved)
456 jfunc->type = IPA_JF_PASS_THROUGH;
457 jfunc->value.pass_through.operand = NULL_TREE;
458 jfunc->value.pass_through.formal_id = formal_id;
459 jfunc->value.pass_through.operation = NOP_EXPR;
460 jfunc->value.pass_through.agg_preserved = agg_preserved;
461 jfunc->value.pass_through.type_preserved = type_preserved;
464 /* Set JFUNC to be an arithmetic pass through jump function. */
466 static void
467 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
468 tree operand, enum tree_code operation)
470 jfunc->type = IPA_JF_PASS_THROUGH;
471 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
472 jfunc->value.pass_through.formal_id = formal_id;
473 jfunc->value.pass_through.operation = operation;
474 jfunc->value.pass_through.agg_preserved = false;
475 jfunc->value.pass_through.type_preserved = false;
478 /* Set JFUNC to be an ancestor jump function. */
480 static void
481 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
482 tree type, int formal_id, bool agg_preserved,
483 bool type_preserved)
485 if (!flag_devirtualize)
486 type_preserved = false;
487 gcc_assert (!type_preserved
488 || (TREE_CODE (type) == RECORD_TYPE
489 && TYPE_BINFO (type)
490 && BINFO_VTABLE (TYPE_BINFO (type))));
491 jfunc->type = IPA_JF_ANCESTOR;
492 jfunc->value.ancestor.formal_id = formal_id;
493 jfunc->value.ancestor.offset = offset;
494 jfunc->value.ancestor.type = type_preserved ? type : NULL;
495 jfunc->value.ancestor.agg_preserved = agg_preserved;
496 jfunc->value.ancestor.type_preserved = type_preserved;
499 /* Extract the acual BINFO being described by JFUNC which must be a known type
500 jump function. */
502 tree
503 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
505 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
506 if (!base_binfo)
507 return NULL_TREE;
508 return get_binfo_at_offset (base_binfo,
509 jfunc->value.known_type.offset,
510 jfunc->value.known_type.component_type);
513 /* Structure to be passed in between detect_type_change and
514 check_stmt_for_type_change. */
516 struct type_change_info
518 /* Offset into the object where there is the virtual method pointer we are
519 looking for. */
520 HOST_WIDE_INT offset;
521 /* The declaration or SSA_NAME pointer of the base that we are checking for
522 type change. */
523 tree object;
524 /* If we actually can tell the type that the object has changed to, it is
525 stored in this field. Otherwise it remains NULL_TREE. */
526 tree known_current_type;
527 /* Set to true if dynamic type change has been detected. */
528 bool type_maybe_changed;
529 /* Set to true if multiple types have been encountered. known_current_type
530 must be disregarded in that case. */
531 bool multiple_types_encountered;
534 /* Return true if STMT can modify a virtual method table pointer.
536 This function makes special assumptions about both constructors and
537 destructors which are all the functions that are allowed to alter the VMT
538 pointers. It assumes that destructors begin with assignment into all VMT
539 pointers and that constructors essentially look in the following way:
541 1) The very first thing they do is that they call constructors of ancestor
542 sub-objects that have them.
544 2) Then VMT pointers of this and all its ancestors is set to new values
545 corresponding to the type corresponding to the constructor.
547 3) Only afterwards, other stuff such as constructor of member sub-objects
548 and the code written by the user is run. Only this may include calling
549 virtual functions, directly or indirectly.
551 There is no way to call a constructor of an ancestor sub-object in any
552 other way.
554 This means that we do not have to care whether constructors get the correct
555 type information because they will always change it (in fact, if we define
556 the type to be given by the VMT pointer, it is undefined).
558 The most important fact to derive from the above is that if, for some
559 statement in the section 3, we try to detect whether the dynamic type has
560 changed, we can safely ignore all calls as we examine the function body
561 backwards until we reach statements in section 2 because these calls cannot
562 be ancestor constructors or destructors (if the input is not bogus) and so
563 do not change the dynamic type (this holds true only for automatically
564 allocated objects but at the moment we devirtualize only these). We then
565 must detect that statements in section 2 change the dynamic type and can try
566 to derive the new type. That is enough and we can stop, we will never see
567 the calls into constructors of sub-objects in this code. Therefore we can
568 safely ignore all call statements that we traverse.
571 static bool
572 stmt_may_be_vtbl_ptr_store (gimple stmt)
574 if (is_gimple_call (stmt))
575 return false;
576 /* TODO: Skip clobbers, doing so triggers problem in PR60306. */
577 else if (is_gimple_assign (stmt))
579 tree lhs = gimple_assign_lhs (stmt);
581 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
583 if (flag_strict_aliasing
584 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
585 return false;
587 if (TREE_CODE (lhs) == COMPONENT_REF
588 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
589 return false;
590 /* In the future we might want to use get_base_ref_and_offset to find
591 if there is a field corresponding to the offset and if so, proceed
592 almost like if it was a component ref. */
595 return true;
598 /* If STMT can be proved to be an assignment to the virtual method table
599 pointer of ANALYZED_OBJ and the type associated with the new table
600 identified, return the type. Otherwise return NULL_TREE. */
602 static tree
603 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
605 HOST_WIDE_INT offset, size, max_size;
606 tree lhs, rhs, base, binfo;
608 if (!gimple_assign_single_p (stmt))
609 return NULL_TREE;
611 lhs = gimple_assign_lhs (stmt);
612 rhs = gimple_assign_rhs1 (stmt);
613 if (TREE_CODE (lhs) != COMPONENT_REF
614 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
615 return NULL_TREE;
617 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
618 if (offset != tci->offset
619 || size != POINTER_SIZE
620 || max_size != POINTER_SIZE)
621 return NULL_TREE;
622 if (TREE_CODE (base) == MEM_REF)
624 if (TREE_CODE (tci->object) != MEM_REF
625 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
626 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
627 TREE_OPERAND (base, 1)))
628 return NULL_TREE;
630 else if (tci->object != base)
631 return NULL_TREE;
633 binfo = vtable_pointer_value_to_binfo (rhs);
635 /* FIXME: vtable_pointer_value_to_binfo may return BINFO of a
636 base of outer type. In this case we would need to either
637 work on binfos or translate it back to outer type and offset.
638 KNOWN_TYPE jump functions are not ready for that, yet. */
639 if (!binfo || TYPE_BINFO (BINFO_TYPE (binfo)) != binfo)
640 return NULL;
642 return BINFO_TYPE (binfo);
645 /* Callback of walk_aliased_vdefs and a helper function for
646 detect_type_change to check whether a particular statement may modify
647 the virtual table pointer, and if possible also determine the new type of
648 the (sub-)object. It stores its result into DATA, which points to a
649 type_change_info structure. */
651 static bool
652 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
654 gimple stmt = SSA_NAME_DEF_STMT (vdef);
655 struct type_change_info *tci = (struct type_change_info *) data;
657 if (stmt_may_be_vtbl_ptr_store (stmt))
659 tree type;
660 type = extr_type_from_vtbl_ptr_store (stmt, tci);
661 if (tci->type_maybe_changed
662 && type != tci->known_current_type)
663 tci->multiple_types_encountered = true;
664 tci->known_current_type = type;
665 tci->type_maybe_changed = true;
666 return true;
668 else
669 return false;
674 /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
675 callsite CALL) by looking for assignments to its virtual table pointer. If
676 it is, return true and fill in the jump function JFUNC with relevant type
677 information or set it to unknown. ARG is the object itself (not a pointer
678 to it, unless dereferenced). BASE is the base of the memory access as
679 returned by get_ref_base_and_extent, as is the offset. */
681 static bool
682 detect_type_change (tree arg, tree base, tree comp_type, gimple call,
683 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
685 struct type_change_info tci;
686 ao_ref ao;
688 gcc_checking_assert (DECL_P (arg)
689 || TREE_CODE (arg) == MEM_REF
690 || handled_component_p (arg));
691 /* Const calls cannot call virtual methods through VMT and so type changes do
692 not matter. */
693 if (!flag_devirtualize || !gimple_vuse (call)
694 /* Be sure expected_type is polymorphic. */
695 || !comp_type
696 || TREE_CODE (comp_type) != RECORD_TYPE
697 || !TYPE_BINFO (comp_type)
698 || !BINFO_VTABLE (TYPE_BINFO (comp_type)))
699 return true;
701 /* C++ methods are not allowed to change THIS pointer unless they
702 are constructors or destructors. */
703 if (TREE_CODE (base) == MEM_REF
704 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
705 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
706 && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0))) == PARM_DECL
707 && TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
708 && !DECL_CXX_CONSTRUCTOR_P (current_function_decl)
709 && !DECL_CXX_DESTRUCTOR_P (current_function_decl)
710 && (SSA_NAME_VAR (TREE_OPERAND (base, 0))
711 == DECL_ARGUMENTS (current_function_decl)))
712 return false;
714 ao_ref_init (&ao, arg);
715 ao.base = base;
716 ao.offset = offset;
717 ao.size = POINTER_SIZE;
718 ao.max_size = ao.size;
720 tci.offset = offset;
721 tci.object = get_base_address (arg);
722 tci.known_current_type = NULL_TREE;
723 tci.type_maybe_changed = false;
724 tci.multiple_types_encountered = false;
726 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
727 &tci, NULL);
728 if (!tci.type_maybe_changed)
729 return false;
731 if (!tci.known_current_type
732 || tci.multiple_types_encountered
733 || offset != 0)
734 jfunc->type = IPA_JF_UNKNOWN;
735 else
736 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
738 return true;
741 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
742 SSA name (its dereference will become the base and the offset is assumed to
743 be zero). */
745 static bool
746 detect_type_change_ssa (tree arg, tree comp_type,
747 gimple call, struct ipa_jump_func *jfunc)
749 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
750 if (!flag_devirtualize
751 || !POINTER_TYPE_P (TREE_TYPE (arg)))
752 return false;
754 arg = build2 (MEM_REF, ptr_type_node, arg,
755 build_int_cst (ptr_type_node, 0));
757 return detect_type_change (arg, arg, comp_type, call, jfunc, 0);
760 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
761 boolean variable pointed to by DATA. */
763 static bool
764 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
765 void *data)
767 bool *b = (bool *) data;
768 *b = true;
769 return true;
772 /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
773 a value known not to be modified in this function before reaching the
774 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
775 information about the parameter. */
777 static bool
778 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
779 gimple stmt, tree parm_load)
781 bool modified = false;
782 bitmap *visited_stmts;
783 ao_ref refd;
785 if (parm_ainfo && parm_ainfo->parm_modified)
786 return false;
788 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
789 ao_ref_init (&refd, parm_load);
790 /* We can cache visited statements only when parm_ainfo is available and when
791 we are looking at a naked load of the whole parameter. */
792 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
793 visited_stmts = NULL;
794 else
795 visited_stmts = &parm_ainfo->parm_visited_statements;
796 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
797 visited_stmts);
798 if (parm_ainfo && modified)
799 parm_ainfo->parm_modified = true;
800 return !modified;
803 /* If STMT is an assignment that loads a value from an parameter declaration,
804 return the index of the parameter in ipa_node_params which has not been
805 modified. Otherwise return -1. */
807 static int
808 load_from_unmodified_param (vec<ipa_param_descriptor> descriptors,
809 struct param_analysis_info *parms_ainfo,
810 gimple stmt)
812 int index;
813 tree op1;
815 if (!gimple_assign_single_p (stmt))
816 return -1;
818 op1 = gimple_assign_rhs1 (stmt);
819 if (TREE_CODE (op1) != PARM_DECL)
820 return -1;
822 index = ipa_get_param_decl_index_1 (descriptors, op1);
823 if (index < 0
824 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
825 : NULL, stmt, op1))
826 return -1;
828 return index;
831 /* Return true if memory reference REF loads data that are known to be
832 unmodified in this function before reaching statement STMT. PARM_AINFO, if
833 non-NULL, is a pointer to a structure containing temporary information about
834 PARM. */
836 static bool
837 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
838 gimple stmt, tree ref)
840 bool modified = false;
841 ao_ref refd;
843 gcc_checking_assert (gimple_vuse (stmt));
844 if (parm_ainfo && parm_ainfo->ref_modified)
845 return false;
847 ao_ref_init (&refd, ref);
848 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
849 NULL);
850 if (parm_ainfo && modified)
851 parm_ainfo->ref_modified = true;
852 return !modified;
855 /* Return true if the data pointed to by PARM is known to be unmodified in this
856 function before reaching call statement CALL into which it is passed.
857 PARM_AINFO is a pointer to a structure containing temporary information
858 about PARM. */
860 static bool
861 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
862 gimple call, tree parm)
864 bool modified = false;
865 ao_ref refd;
867 /* It's unnecessary to calculate anything about memory contnets for a const
868 function because it is not goin to use it. But do not cache the result
869 either. Also, no such calculations for non-pointers. */
870 if (!gimple_vuse (call)
871 || !POINTER_TYPE_P (TREE_TYPE (parm)))
872 return false;
874 if (parm_ainfo->pt_modified)
875 return false;
877 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
878 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
879 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
880 if (modified)
881 parm_ainfo->pt_modified = true;
882 return !modified;
885 /* Return true if we can prove that OP is a memory reference loading unmodified
886 data from an aggregate passed as a parameter and if the aggregate is passed
887 by reference, that the alias type of the load corresponds to the type of the
888 formal parameter (so that we can rely on this type for TBAA in callers).
889 INFO and PARMS_AINFO describe parameters of the current function (but the
890 latter can be NULL), STMT is the load statement. If function returns true,
891 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
892 within the aggregate and whether it is a load from a value passed by
893 reference respectively. */
895 static bool
896 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor> descriptors,
897 struct param_analysis_info *parms_ainfo, gimple stmt,
898 tree op, int *index_p, HOST_WIDE_INT *offset_p,
899 HOST_WIDE_INT *size_p, bool *by_ref_p)
901 int index;
902 HOST_WIDE_INT size, max_size;
903 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
905 if (max_size == -1 || max_size != size || *offset_p < 0)
906 return false;
908 if (DECL_P (base))
910 int index = ipa_get_param_decl_index_1 (descriptors, base);
911 if (index >= 0
912 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
913 : NULL, stmt, op))
915 *index_p = index;
916 *by_ref_p = false;
917 if (size_p)
918 *size_p = size;
919 return true;
921 return false;
924 if (TREE_CODE (base) != MEM_REF
925 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
926 || !integer_zerop (TREE_OPERAND (base, 1)))
927 return false;
929 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
931 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
932 index = ipa_get_param_decl_index_1 (descriptors, parm);
934 else
936 /* This branch catches situations where a pointer parameter is not a
937 gimple register, for example:
939 void hip7(S*) (struct S * p)
941 void (*<T2e4>) (struct S *) D.1867;
942 struct S * p.1;
944 <bb 2>:
945 p.1_1 = p;
946 D.1867_2 = p.1_1->f;
947 D.1867_2 ();
948 gdp = &p;
951 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
952 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
955 if (index >= 0
956 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
957 stmt, op))
959 *index_p = index;
960 *by_ref_p = true;
961 if (size_p)
962 *size_p = size;
963 return true;
965 return false;
968 /* Just like the previous function, just without the param_analysis_info
969 pointer, for users outside of this file. */
971 bool
972 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
973 tree op, int *index_p, HOST_WIDE_INT *offset_p,
974 bool *by_ref_p)
976 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
977 offset_p, NULL, by_ref_p);
980 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
981 of an assignment statement STMT, try to determine whether we are actually
982 handling any of the following cases and construct an appropriate jump
983 function into JFUNC if so:
985 1) The passed value is loaded from a formal parameter which is not a gimple
986 register (most probably because it is addressable, the value has to be
987 scalar) and we can guarantee the value has not changed. This case can
988 therefore be described by a simple pass-through jump function. For example:
990 foo (int a)
992 int a.0;
994 a.0_2 = a;
995 bar (a.0_2);
997 2) The passed value can be described by a simple arithmetic pass-through
998 jump function. E.g.
1000 foo (int a)
1002 int D.2064;
1004 D.2064_4 = a.1(D) + 4;
1005 bar (D.2064_4);
1007 This case can also occur in combination of the previous one, e.g.:
1009 foo (int a, int z)
1011 int a.0;
1012 int D.2064;
1014 a.0_3 = a;
1015 D.2064_4 = a.0_3 + 4;
1016 foo (D.2064_4);
1018 3) The passed value is an address of an object within another one (which
1019 also passed by reference). Such situations are described by an ancestor
1020 jump function and describe situations such as:
1022 B::foo() (struct B * const this)
1024 struct A * D.1845;
1026 D.1845_2 = &this_1(D)->D.1748;
1027 A::bar (D.1845_2);
1029 INFO is the structure describing individual parameters access different
1030 stages of IPA optimizations. PARMS_AINFO contains the information that is
1031 only needed for intraprocedural analysis. */
1033 static void
1034 compute_complex_assign_jump_func (struct ipa_node_params *info,
1035 struct param_analysis_info *parms_ainfo,
1036 struct ipa_jump_func *jfunc,
1037 gimple call, gimple stmt, tree name,
1038 tree param_type)
1040 HOST_WIDE_INT offset, size, max_size;
1041 tree op1, tc_ssa, base, ssa;
1042 int index;
1044 op1 = gimple_assign_rhs1 (stmt);
1046 if (TREE_CODE (op1) == SSA_NAME)
1048 if (SSA_NAME_IS_DEFAULT_DEF (op1))
1049 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1050 else
1051 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
1052 SSA_NAME_DEF_STMT (op1));
1053 tc_ssa = op1;
1055 else
1057 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
1058 tc_ssa = gimple_assign_lhs (stmt);
1061 if (index >= 0)
1063 tree op2 = gimple_assign_rhs2 (stmt);
1065 if (op2)
1067 if (!is_gimple_ip_invariant (op2)
1068 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
1069 && !useless_type_conversion_p (TREE_TYPE (name),
1070 TREE_TYPE (op1))))
1071 return;
1073 ipa_set_jf_arith_pass_through (jfunc, index, op2,
1074 gimple_assign_rhs_code (stmt));
1076 else if (gimple_assign_single_p (stmt))
1078 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1079 call, tc_ssa);
1080 bool type_p = false;
1082 if (param_type && POINTER_TYPE_P (param_type))
1083 type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type),
1084 call, jfunc);
1085 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1086 ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p);
1088 return;
1091 if (TREE_CODE (op1) != ADDR_EXPR)
1092 return;
1093 op1 = TREE_OPERAND (op1, 0);
1094 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
1095 return;
1096 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1097 if (TREE_CODE (base) != MEM_REF
1098 /* If this is a varying address, punt. */
1099 || max_size == -1
1100 || max_size != size)
1101 return;
1102 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
1103 ssa = TREE_OPERAND (base, 0);
1104 if (TREE_CODE (ssa) != SSA_NAME
1105 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1106 || offset < 0)
1107 return;
1109 /* Dynamic types are changed in constructors and destructors. */
1110 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1111 if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1113 bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type),
1114 call, jfunc, offset);
1115 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1116 ipa_set_ancestor_jf (jfunc, offset,
1117 type_p ? TREE_TYPE (param_type) : NULL, index,
1118 parm_ref_data_pass_through_p (&parms_ainfo[index],
1119 call, ssa), type_p);
1123 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1124 it looks like:
1126 iftmp.1_3 = &obj_2(D)->D.1762;
1128 The base of the MEM_REF must be a default definition SSA NAME of a
1129 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1130 whole MEM_REF expression is returned and the offset calculated from any
1131 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1132 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1134 static tree
1135 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1137 HOST_WIDE_INT size, max_size;
1138 tree expr, parm, obj;
1140 if (!gimple_assign_single_p (assign))
1141 return NULL_TREE;
1142 expr = gimple_assign_rhs1 (assign);
1144 if (TREE_CODE (expr) != ADDR_EXPR)
1145 return NULL_TREE;
1146 expr = TREE_OPERAND (expr, 0);
1147 obj = expr;
1148 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1150 if (TREE_CODE (expr) != MEM_REF
1151 /* If this is a varying address, punt. */
1152 || max_size == -1
1153 || max_size != size
1154 || *offset < 0)
1155 return NULL_TREE;
1156 parm = TREE_OPERAND (expr, 0);
1157 if (TREE_CODE (parm) != SSA_NAME
1158 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1159 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1160 return NULL_TREE;
1162 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1163 *obj_p = obj;
1164 return expr;
1168 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1169 statement PHI, try to find out whether NAME is in fact a
1170 multiple-inheritance typecast from a descendant into an ancestor of a formal
1171 parameter and thus can be described by an ancestor jump function and if so,
1172 write the appropriate function into JFUNC.
1174 Essentially we want to match the following pattern:
1176 if (obj_2(D) != 0B)
1177 goto <bb 3>;
1178 else
1179 goto <bb 4>;
1181 <bb 3>:
1182 iftmp.1_3 = &obj_2(D)->D.1762;
1184 <bb 4>:
1185 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1186 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1187 return D.1879_6; */
1189 static void
1190 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1191 struct param_analysis_info *parms_ainfo,
1192 struct ipa_jump_func *jfunc,
1193 gimple call, gimple phi, tree param_type)
1195 HOST_WIDE_INT offset;
1196 gimple assign, cond;
1197 basic_block phi_bb, assign_bb, cond_bb;
1198 tree tmp, parm, expr, obj;
1199 int index, i;
1201 if (gimple_phi_num_args (phi) != 2)
1202 return;
1204 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1205 tmp = PHI_ARG_DEF (phi, 0);
1206 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1207 tmp = PHI_ARG_DEF (phi, 1);
1208 else
1209 return;
1210 if (TREE_CODE (tmp) != SSA_NAME
1211 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1212 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1213 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1214 return;
1216 assign = SSA_NAME_DEF_STMT (tmp);
1217 assign_bb = gimple_bb (assign);
1218 if (!single_pred_p (assign_bb))
1219 return;
1220 expr = get_ancestor_addr_info (assign, &obj, &offset);
1221 if (!expr)
1222 return;
1223 parm = TREE_OPERAND (expr, 0);
1224 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1225 if (index < 0)
1226 return;
1228 cond_bb = single_pred (assign_bb);
1229 cond = last_stmt (cond_bb);
1230 if (!cond
1231 || gimple_code (cond) != GIMPLE_COND
1232 || gimple_cond_code (cond) != NE_EXPR
1233 || gimple_cond_lhs (cond) != parm
1234 || !integer_zerop (gimple_cond_rhs (cond)))
1235 return;
1237 phi_bb = gimple_bb (phi);
1238 for (i = 0; i < 2; i++)
1240 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1241 if (pred != assign_bb && pred != cond_bb)
1242 return;
1245 bool type_p = false;
1246 if (param_type && POINTER_TYPE_P (param_type))
1247 type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type),
1248 call, jfunc, offset);
1249 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1250 ipa_set_ancestor_jf (jfunc, offset, type_p ? TREE_TYPE (param_type) : NULL, index,
1251 parm_ref_data_pass_through_p (&parms_ainfo[index],
1252 call, parm), type_p);
1255 /* Given OP which is passed as an actual argument to a called function,
1256 determine if it is possible to construct a KNOWN_TYPE jump function for it
1257 and if so, create one and store it to JFUNC.
1258 EXPECTED_TYPE represents a type the argument should be in */
1260 static void
1261 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1262 gimple call, tree expected_type)
1264 HOST_WIDE_INT offset, size, max_size;
1265 tree base;
1267 if (!flag_devirtualize
1268 || TREE_CODE (op) != ADDR_EXPR
1269 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE
1270 /* Be sure expected_type is polymorphic. */
1271 || !expected_type
1272 || TREE_CODE (expected_type) != RECORD_TYPE
1273 || !TYPE_BINFO (expected_type)
1274 || !BINFO_VTABLE (TYPE_BINFO (expected_type)))
1275 return;
1277 op = TREE_OPERAND (op, 0);
1278 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1279 if (!DECL_P (base)
1280 || max_size == -1
1281 || max_size != size
1282 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1283 || is_global_var (base))
1284 return;
1286 if (detect_type_change (op, base, expected_type, call, jfunc, offset))
1287 return;
1289 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base),
1290 expected_type);
1293 /* Inspect the given TYPE and return true iff it has the same structure (the
1294 same number of fields of the same types) as a C++ member pointer. If
1295 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1296 corresponding fields there. */
1298 static bool
1299 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1301 tree fld;
1303 if (TREE_CODE (type) != RECORD_TYPE)
1304 return false;
1306 fld = TYPE_FIELDS (type);
1307 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1308 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1309 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1310 return false;
1312 if (method_ptr)
1313 *method_ptr = fld;
1315 fld = DECL_CHAIN (fld);
1316 if (!fld || INTEGRAL_TYPE_P (fld)
1317 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (fld)))
1318 return false;
1319 if (delta)
1320 *delta = fld;
1322 if (DECL_CHAIN (fld))
1323 return false;
1325 return true;
1328 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1329 return the rhs of its defining statement. Otherwise return RHS as it
1330 is. */
1332 static inline tree
1333 get_ssa_def_if_simple_copy (tree rhs)
1335 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1337 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1339 if (gimple_assign_single_p (def_stmt))
1340 rhs = gimple_assign_rhs1 (def_stmt);
1341 else
1342 break;
1344 return rhs;
1347 /* Simple linked list, describing known contents of an aggregate beforere
1348 call. */
1350 struct ipa_known_agg_contents_list
1352 /* Offset and size of the described part of the aggregate. */
1353 HOST_WIDE_INT offset, size;
1354 /* Known constant value or NULL if the contents is known to be unknown. */
1355 tree constant;
1356 /* Pointer to the next structure in the list. */
1357 struct ipa_known_agg_contents_list *next;
1360 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1361 in ARG is filled in with constant values. ARG can either be an aggregate
1362 expression or a pointer to an aggregate. ARG_TYPE is the type of the aggregate.
1363 JFUNC is the jump function into which the constants are subsequently stored. */
1365 static void
1366 determine_known_aggregate_parts (gimple call, tree arg, tree arg_type,
1367 struct ipa_jump_func *jfunc)
1369 struct ipa_known_agg_contents_list *list = NULL;
1370 int item_count = 0, const_count = 0;
1371 HOST_WIDE_INT arg_offset, arg_size;
1372 gimple_stmt_iterator gsi;
1373 tree arg_base;
1374 bool check_ref, by_ref;
1375 ao_ref r;
1377 /* The function operates in three stages. First, we prepare check_ref, r,
1378 arg_base and arg_offset based on what is actually passed as an actual
1379 argument. */
1381 if (POINTER_TYPE_P (arg_type))
1383 by_ref = true;
1384 if (TREE_CODE (arg) == SSA_NAME)
1386 tree type_size;
1387 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (arg_type))))
1388 return;
1389 check_ref = true;
1390 arg_base = arg;
1391 arg_offset = 0;
1392 type_size = TYPE_SIZE (TREE_TYPE (arg_type));
1393 arg_size = tree_to_uhwi (type_size);
1394 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1396 else if (TREE_CODE (arg) == ADDR_EXPR)
1398 HOST_WIDE_INT arg_max_size;
1400 arg = TREE_OPERAND (arg, 0);
1401 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1402 &arg_max_size);
1403 if (arg_max_size == -1
1404 || arg_max_size != arg_size
1405 || arg_offset < 0)
1406 return;
1407 if (DECL_P (arg_base))
1409 tree size;
1410 check_ref = false;
1411 size = build_int_cst (integer_type_node, arg_size);
1412 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1414 else
1415 return;
1417 else
1418 return;
1420 else
1422 HOST_WIDE_INT arg_max_size;
1424 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1426 by_ref = false;
1427 check_ref = false;
1428 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1429 &arg_max_size);
1430 if (arg_max_size == -1
1431 || arg_max_size != arg_size
1432 || arg_offset < 0)
1433 return;
1435 ao_ref_init (&r, arg);
1438 /* Second stage walks back the BB, looks at individual statements and as long
1439 as it is confident of how the statements affect contents of the
1440 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1441 describing it. */
1442 gsi = gsi_for_stmt (call);
1443 gsi_prev (&gsi);
1444 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1446 struct ipa_known_agg_contents_list *n, **p;
1447 gimple stmt = gsi_stmt (gsi);
1448 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1449 tree lhs, rhs, lhs_base;
1450 bool partial_overlap;
1452 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1453 continue;
1454 if (!gimple_assign_single_p (stmt))
1455 break;
1457 lhs = gimple_assign_lhs (stmt);
1458 rhs = gimple_assign_rhs1 (stmt);
1459 if (!is_gimple_reg_type (TREE_TYPE (rhs))
1460 || TREE_CODE (lhs) == BIT_FIELD_REF
1461 || contains_bitfld_component_ref_p (lhs))
1462 break;
1464 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1465 &lhs_max_size);
1466 if (lhs_max_size == -1
1467 || lhs_max_size != lhs_size
1468 || (lhs_offset < arg_offset
1469 && lhs_offset + lhs_size > arg_offset)
1470 || (lhs_offset < arg_offset + arg_size
1471 && lhs_offset + lhs_size > arg_offset + arg_size))
1472 break;
1474 if (check_ref)
1476 if (TREE_CODE (lhs_base) != MEM_REF
1477 || TREE_OPERAND (lhs_base, 0) != arg_base
1478 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1479 break;
1481 else if (lhs_base != arg_base)
1483 if (DECL_P (lhs_base))
1484 continue;
1485 else
1486 break;
1489 if (lhs_offset + lhs_size < arg_offset
1490 || lhs_offset >= (arg_offset + arg_size))
1491 continue;
1493 partial_overlap = false;
1494 p = &list;
1495 while (*p && (*p)->offset < lhs_offset)
1497 if ((*p)->offset + (*p)->size > lhs_offset)
1499 partial_overlap = true;
1500 break;
1502 p = &(*p)->next;
1504 if (partial_overlap)
1505 break;
1506 if (*p && (*p)->offset < lhs_offset + lhs_size)
1508 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1509 /* We already know this value is subsequently overwritten with
1510 something else. */
1511 continue;
1512 else
1513 /* Otherwise this is a partial overlap which we cannot
1514 represent. */
1515 break;
1518 rhs = get_ssa_def_if_simple_copy (rhs);
1519 n = XALLOCA (struct ipa_known_agg_contents_list);
1520 n->size = lhs_size;
1521 n->offset = lhs_offset;
1522 if (is_gimple_ip_invariant (rhs))
1524 n->constant = rhs;
1525 const_count++;
1527 else
1528 n->constant = NULL_TREE;
1529 n->next = *p;
1530 *p = n;
1532 item_count++;
1533 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1534 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1535 break;
1538 /* Third stage just goes over the list and creates an appropriate vector of
1539 ipa_agg_jf_item structures out of it, of sourse only if there are
1540 any known constants to begin with. */
1542 if (const_count)
1544 jfunc->agg.by_ref = by_ref;
1545 vec_alloc (jfunc->agg.items, const_count);
1546 while (list)
1548 if (list->constant)
1550 struct ipa_agg_jf_item item;
1551 item.offset = list->offset - arg_offset;
1552 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1553 item.value = unshare_expr_without_location (list->constant);
1554 jfunc->agg.items->quick_push (item);
1556 list = list->next;
1561 static tree
1562 ipa_get_callee_param_type (struct cgraph_edge *e, int i)
1564 int n;
1565 tree type = (e->callee
1566 ? TREE_TYPE (e->callee->decl)
1567 : gimple_call_fntype (e->call_stmt));
1568 tree t = TYPE_ARG_TYPES (type);
1570 for (n = 0; n < i; n++)
1572 if (!t)
1573 break;
1574 t = TREE_CHAIN (t);
1576 if (t)
1577 return TREE_VALUE (t);
1578 if (!e->callee)
1579 return NULL;
1580 t = DECL_ARGUMENTS (e->callee->decl);
1581 for (n = 0; n < i; n++)
1583 if (!t)
1584 return NULL;
1585 t = TREE_CHAIN (t);
1587 if (t)
1588 return TREE_TYPE (t);
1589 return NULL;
1592 /* Compute jump function for all arguments of callsite CS and insert the
1593 information in the jump_functions array in the ipa_edge_args corresponding
1594 to this callsite. */
1596 static void
1597 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1598 struct cgraph_edge *cs)
1600 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1601 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1602 gimple call = cs->call_stmt;
1603 int n, arg_num = gimple_call_num_args (call);
1605 if (arg_num == 0 || args->jump_functions)
1606 return;
1607 vec_safe_grow_cleared (args->jump_functions, arg_num);
1609 if (gimple_call_internal_p (call))
1610 return;
1611 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1612 return;
1614 for (n = 0; n < arg_num; n++)
1616 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1617 tree arg = gimple_call_arg (call, n);
1618 tree param_type = ipa_get_callee_param_type (cs, n);
1620 if (is_gimple_ip_invariant (arg))
1621 ipa_set_jf_constant (jfunc, arg, cs);
1622 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1623 && TREE_CODE (arg) == PARM_DECL)
1625 int index = ipa_get_param_decl_index (info, arg);
1627 gcc_assert (index >=0);
1628 /* Aggregate passed by value, check for pass-through, otherwise we
1629 will attempt to fill in aggregate contents later in this
1630 for cycle. */
1631 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1633 ipa_set_jf_simple_pass_through (jfunc, index, false, false);
1634 continue;
1637 else if (TREE_CODE (arg) == SSA_NAME)
1639 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1641 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1642 if (index >= 0)
1644 bool agg_p, type_p;
1645 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1646 call, arg);
1647 if (param_type && POINTER_TYPE_P (param_type))
1648 type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type),
1649 call, jfunc);
1650 else
1651 type_p = false;
1652 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1653 ipa_set_jf_simple_pass_through (jfunc, index, agg_p,
1654 type_p);
1657 else
1659 gimple stmt = SSA_NAME_DEF_STMT (arg);
1660 if (is_gimple_assign (stmt))
1661 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1662 call, stmt, arg, param_type);
1663 else if (gimple_code (stmt) == GIMPLE_PHI)
1664 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1665 call, stmt, param_type);
1668 else
1669 compute_known_type_jump_func (arg, jfunc, call,
1670 param_type
1671 && POINTER_TYPE_P (param_type)
1672 ? TREE_TYPE (param_type)
1673 : NULL);
1675 /* If ARG is pointer, we can not use its type to determine the type of aggregate
1676 passed (because type conversions are ignored in gimple). Usually we can
1677 safely get type from function declaration, but in case of K&R prototypes or
1678 variadic functions we can try our luck with type of the pointer passed.
1679 TODO: Since we look for actual initialization of the memory object, we may better
1680 work out the type based on the memory stores we find. */
1681 if (!param_type)
1682 param_type = TREE_TYPE (arg);
1684 if ((jfunc->type != IPA_JF_PASS_THROUGH
1685 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1686 && (jfunc->type != IPA_JF_ANCESTOR
1687 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1688 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1689 || POINTER_TYPE_P (param_type)))
1690 determine_known_aggregate_parts (call, arg, param_type, jfunc);
1694 /* Compute jump functions for all edges - both direct and indirect - outgoing
1695 from NODE. Also count the actual arguments in the process. */
1697 static void
1698 ipa_compute_jump_functions (struct cgraph_node *node,
1699 struct param_analysis_info *parms_ainfo)
1701 struct cgraph_edge *cs;
1703 for (cs = node->callees; cs; cs = cs->next_callee)
1705 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1706 NULL);
1707 /* We do not need to bother analyzing calls to unknown
1708 functions unless they may become known during lto/whopr. */
1709 if (!callee->definition && !flag_lto)
1710 continue;
1711 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1714 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1715 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1718 /* If STMT looks like a statement loading a value from a member pointer formal
1719 parameter, return that parameter and store the offset of the field to
1720 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1721 might be clobbered). If USE_DELTA, then we look for a use of the delta
1722 field rather than the pfn. */
1724 static tree
1725 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1726 HOST_WIDE_INT *offset_p)
1728 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1730 if (!gimple_assign_single_p (stmt))
1731 return NULL_TREE;
1733 rhs = gimple_assign_rhs1 (stmt);
1734 if (TREE_CODE (rhs) == COMPONENT_REF)
1736 ref_field = TREE_OPERAND (rhs, 1);
1737 rhs = TREE_OPERAND (rhs, 0);
1739 else
1740 ref_field = NULL_TREE;
1741 if (TREE_CODE (rhs) != MEM_REF)
1742 return NULL_TREE;
1743 rec = TREE_OPERAND (rhs, 0);
1744 if (TREE_CODE (rec) != ADDR_EXPR)
1745 return NULL_TREE;
1746 rec = TREE_OPERAND (rec, 0);
1747 if (TREE_CODE (rec) != PARM_DECL
1748 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1749 return NULL_TREE;
1750 ref_offset = TREE_OPERAND (rhs, 1);
1752 if (use_delta)
1753 fld = delta_field;
1754 else
1755 fld = ptr_field;
1756 if (offset_p)
1757 *offset_p = int_bit_position (fld);
1759 if (ref_field)
1761 if (integer_nonzerop (ref_offset))
1762 return NULL_TREE;
1763 return ref_field == fld ? rec : NULL_TREE;
1765 else
1766 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1767 : NULL_TREE;
1770 /* Returns true iff T is an SSA_NAME defined by a statement. */
1772 static bool
1773 ipa_is_ssa_with_stmt_def (tree t)
1775 if (TREE_CODE (t) == SSA_NAME
1776 && !SSA_NAME_IS_DEFAULT_DEF (t))
1777 return true;
1778 else
1779 return false;
1782 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1783 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1784 indirect call graph edge. */
1786 static struct cgraph_edge *
1787 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1789 struct cgraph_edge *cs;
1791 cs = cgraph_edge (node, stmt);
1792 cs->indirect_info->param_index = param_index;
1793 cs->indirect_info->agg_contents = 0;
1794 cs->indirect_info->member_ptr = 0;
1795 return cs;
1798 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1799 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1800 intermediate information about each formal parameter. Currently it checks
1801 whether the call calls a pointer that is a formal parameter and if so, the
1802 parameter is marked with the called flag and an indirect call graph edge
1803 describing the call is created. This is very simple for ordinary pointers
1804 represented in SSA but not-so-nice when it comes to member pointers. The
1805 ugly part of this function does nothing more than trying to match the
1806 pattern of such a call. An example of such a pattern is the gimple dump
1807 below, the call is on the last line:
1809 <bb 2>:
1810 f$__delta_5 = f.__delta;
1811 f$__pfn_24 = f.__pfn;
1814 <bb 2>:
1815 f$__delta_5 = MEM[(struct *)&f];
1816 f$__pfn_24 = MEM[(struct *)&f + 4B];
1818 and a few lines below:
1820 <bb 5>
1821 D.2496_3 = (int) f$__pfn_24;
1822 D.2497_4 = D.2496_3 & 1;
1823 if (D.2497_4 != 0)
1824 goto <bb 3>;
1825 else
1826 goto <bb 4>;
1828 <bb 6>:
1829 D.2500_7 = (unsigned int) f$__delta_5;
1830 D.2501_8 = &S + D.2500_7;
1831 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1832 D.2503_10 = *D.2502_9;
1833 D.2504_12 = f$__pfn_24 + -1;
1834 D.2505_13 = (unsigned int) D.2504_12;
1835 D.2506_14 = D.2503_10 + D.2505_13;
1836 D.2507_15 = *D.2506_14;
1837 iftmp.11_16 = (String:: *) D.2507_15;
1839 <bb 7>:
1840 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1841 D.2500_19 = (unsigned int) f$__delta_5;
1842 D.2508_20 = &S + D.2500_19;
1843 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1845 Such patterns are results of simple calls to a member pointer:
1847 int doprinting (int (MyString::* f)(int) const)
1849 MyString S ("somestring");
1851 return (S.*f)(4);
1854 Moreover, the function also looks for called pointers loaded from aggregates
1855 passed by value or reference. */
1857 static void
1858 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1859 struct ipa_node_params *info,
1860 struct param_analysis_info *parms_ainfo,
1861 gimple call, tree target)
1863 gimple def;
1864 tree n1, n2;
1865 gimple d1, d2;
1866 tree rec, rec2, cond;
1867 gimple branch;
1868 int index;
1869 basic_block bb, virt_bb, join;
1870 HOST_WIDE_INT offset;
1871 bool by_ref;
1873 if (SSA_NAME_IS_DEFAULT_DEF (target))
1875 tree var = SSA_NAME_VAR (target);
1876 index = ipa_get_param_decl_index (info, var);
1877 if (index >= 0)
1878 ipa_note_param_call (node, index, call);
1879 return;
1882 def = SSA_NAME_DEF_STMT (target);
1883 if (gimple_assign_single_p (def)
1884 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1885 gimple_assign_rhs1 (def), &index, &offset,
1886 NULL, &by_ref))
1888 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1889 if (cs->indirect_info->offset != offset)
1890 cs->indirect_info->outer_type = NULL;
1891 cs->indirect_info->offset = offset;
1892 cs->indirect_info->agg_contents = 1;
1893 cs->indirect_info->by_ref = by_ref;
1894 return;
1897 /* Now we need to try to match the complex pattern of calling a member
1898 pointer. */
1899 if (gimple_code (def) != GIMPLE_PHI
1900 || gimple_phi_num_args (def) != 2
1901 || !POINTER_TYPE_P (TREE_TYPE (target))
1902 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1903 return;
1905 /* First, we need to check whether one of these is a load from a member
1906 pointer that is a parameter to this function. */
1907 n1 = PHI_ARG_DEF (def, 0);
1908 n2 = PHI_ARG_DEF (def, 1);
1909 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1910 return;
1911 d1 = SSA_NAME_DEF_STMT (n1);
1912 d2 = SSA_NAME_DEF_STMT (n2);
1914 join = gimple_bb (def);
1915 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1917 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1918 return;
1920 bb = EDGE_PRED (join, 0)->src;
1921 virt_bb = gimple_bb (d2);
1923 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1925 bb = EDGE_PRED (join, 1)->src;
1926 virt_bb = gimple_bb (d1);
1928 else
1929 return;
1931 /* Second, we need to check that the basic blocks are laid out in the way
1932 corresponding to the pattern. */
1934 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1935 || single_pred (virt_bb) != bb
1936 || single_succ (virt_bb) != join)
1937 return;
1939 /* Third, let's see that the branching is done depending on the least
1940 significant bit of the pfn. */
1942 branch = last_stmt (bb);
1943 if (!branch || gimple_code (branch) != GIMPLE_COND)
1944 return;
1946 if ((gimple_cond_code (branch) != NE_EXPR
1947 && gimple_cond_code (branch) != EQ_EXPR)
1948 || !integer_zerop (gimple_cond_rhs (branch)))
1949 return;
1951 cond = gimple_cond_lhs (branch);
1952 if (!ipa_is_ssa_with_stmt_def (cond))
1953 return;
1955 def = SSA_NAME_DEF_STMT (cond);
1956 if (!is_gimple_assign (def)
1957 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1958 || !integer_onep (gimple_assign_rhs2 (def)))
1959 return;
1961 cond = gimple_assign_rhs1 (def);
1962 if (!ipa_is_ssa_with_stmt_def (cond))
1963 return;
1965 def = SSA_NAME_DEF_STMT (cond);
1967 if (is_gimple_assign (def)
1968 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1970 cond = gimple_assign_rhs1 (def);
1971 if (!ipa_is_ssa_with_stmt_def (cond))
1972 return;
1973 def = SSA_NAME_DEF_STMT (cond);
1976 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1977 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1978 == ptrmemfunc_vbit_in_delta),
1979 NULL);
1980 if (rec != rec2)
1981 return;
1983 index = ipa_get_param_decl_index (info, rec);
1984 if (index >= 0
1985 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1987 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1988 if (cs->indirect_info->offset != offset)
1989 cs->indirect_info->outer_type = NULL;
1990 cs->indirect_info->offset = offset;
1991 cs->indirect_info->agg_contents = 1;
1992 cs->indirect_info->member_ptr = 1;
1995 return;
1998 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1999 object referenced in the expression is a formal parameter of the caller
2000 (described by INFO), create a call note for the statement. */
2002 static void
2003 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
2004 struct ipa_node_params *info, gimple call,
2005 tree target)
2007 struct cgraph_edge *cs;
2008 struct cgraph_indirect_call_info *ii;
2009 struct ipa_jump_func jfunc;
2010 tree obj = OBJ_TYPE_REF_OBJECT (target);
2011 int index;
2012 HOST_WIDE_INT anc_offset;
2014 if (!flag_devirtualize)
2015 return;
2017 if (TREE_CODE (obj) != SSA_NAME)
2018 return;
2020 if (SSA_NAME_IS_DEFAULT_DEF (obj))
2022 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
2023 return;
2025 anc_offset = 0;
2026 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
2027 gcc_assert (index >= 0);
2028 if (detect_type_change_ssa (obj, obj_type_ref_class (target),
2029 call, &jfunc))
2030 return;
2032 else
2034 gimple stmt = SSA_NAME_DEF_STMT (obj);
2035 tree expr;
2037 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
2038 if (!expr)
2039 return;
2040 index = ipa_get_param_decl_index (info,
2041 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
2042 gcc_assert (index >= 0);
2043 if (detect_type_change (obj, expr, obj_type_ref_class (target),
2044 call, &jfunc, anc_offset))
2045 return;
2048 cs = ipa_note_param_call (node, index, call);
2049 ii = cs->indirect_info;
2050 ii->offset = anc_offset;
2051 ii->otr_token = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (target));
2052 ii->otr_type = obj_type_ref_class (target);
2053 ii->polymorphic = 1;
2056 /* Analyze a call statement CALL whether and how it utilizes formal parameters
2057 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
2058 containing intermediate information about each formal parameter. */
2060 static void
2061 ipa_analyze_call_uses (struct cgraph_node *node,
2062 struct ipa_node_params *info,
2063 struct param_analysis_info *parms_ainfo, gimple call)
2065 tree target = gimple_call_fn (call);
2066 struct cgraph_edge *cs;
2068 if (!target
2069 || (TREE_CODE (target) != SSA_NAME
2070 && !virtual_method_call_p (target)))
2071 return;
2073 /* If we previously turned the call into a direct call, there is
2074 no need to analyze. */
2075 cs = cgraph_edge (node, call);
2076 if (cs && !cs->indirect_unknown_callee)
2077 return;
2078 if (TREE_CODE (target) == SSA_NAME)
2079 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
2080 else if (virtual_method_call_p (target))
2081 ipa_analyze_virtual_call_uses (node, info, call, target);
2085 /* Analyze the call statement STMT with respect to formal parameters (described
2086 in INFO) of caller given by NODE. Currently it only checks whether formal
2087 parameters are called. PARMS_AINFO is a pointer to a vector containing
2088 intermediate information about each formal parameter. */
2090 static void
2091 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
2092 struct param_analysis_info *parms_ainfo, gimple stmt)
2094 if (is_gimple_call (stmt))
2095 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
2098 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
2099 If OP is a parameter declaration, mark it as used in the info structure
2100 passed in DATA. */
2102 static bool
2103 visit_ref_for_mod_analysis (gimple, tree op, tree, void *data)
2105 struct ipa_node_params *info = (struct ipa_node_params *) data;
2107 op = get_base_address (op);
2108 if (op
2109 && TREE_CODE (op) == PARM_DECL)
2111 int index = ipa_get_param_decl_index (info, op);
2112 gcc_assert (index >= 0);
2113 ipa_set_param_used (info, index, true);
2116 return false;
2119 /* Scan the function body of NODE and inspect the uses of formal parameters.
2120 Store the findings in various structures of the associated ipa_node_params
2121 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
2122 vector containing intermediate information about each formal parameter. */
2124 static void
2125 ipa_analyze_params_uses (struct cgraph_node *node,
2126 struct param_analysis_info *parms_ainfo)
2128 tree decl = node->decl;
2129 basic_block bb;
2130 struct function *func;
2131 gimple_stmt_iterator gsi;
2132 struct ipa_node_params *info = IPA_NODE_REF (node);
2133 int i;
2135 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
2136 return;
2138 info->uses_analysis_done = 1;
2139 if (ipa_func_spec_opts_forbid_analysis_p (node))
2141 for (i = 0; i < ipa_get_param_count (info); i++)
2143 ipa_set_param_used (info, i, true);
2144 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
2146 return;
2149 for (i = 0; i < ipa_get_param_count (info); i++)
2151 tree parm = ipa_get_param (info, i);
2152 int controlled_uses = 0;
2154 /* For SSA regs see if parameter is used. For non-SSA we compute
2155 the flag during modification analysis. */
2156 if (is_gimple_reg (parm))
2158 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
2159 parm);
2160 if (ddef && !has_zero_uses (ddef))
2162 imm_use_iterator imm_iter;
2163 use_operand_p use_p;
2165 ipa_set_param_used (info, i, true);
2166 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
2167 if (!is_gimple_call (USE_STMT (use_p)))
2169 if (!is_gimple_debug (USE_STMT (use_p)))
2171 controlled_uses = IPA_UNDESCRIBED_USE;
2172 break;
2175 else
2176 controlled_uses++;
2178 else
2179 controlled_uses = 0;
2181 else
2182 controlled_uses = IPA_UNDESCRIBED_USE;
2183 ipa_set_controlled_uses (info, i, controlled_uses);
2186 func = DECL_STRUCT_FUNCTION (decl);
2187 FOR_EACH_BB_FN (bb, func)
2189 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2191 gimple stmt = gsi_stmt (gsi);
2193 if (is_gimple_debug (stmt))
2194 continue;
2196 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
2197 walk_stmt_load_store_addr_ops (stmt, info,
2198 visit_ref_for_mod_analysis,
2199 visit_ref_for_mod_analysis,
2200 visit_ref_for_mod_analysis);
2202 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2203 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
2204 visit_ref_for_mod_analysis,
2205 visit_ref_for_mod_analysis,
2206 visit_ref_for_mod_analysis);
2210 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
2212 static void
2213 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
2215 int i;
2217 for (i = 0; i < param_count; i++)
2219 if (parms_ainfo[i].parm_visited_statements)
2220 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
2221 if (parms_ainfo[i].pt_visited_statements)
2222 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
2226 /* Initialize the array describing properties of of formal parameters
2227 of NODE, analyze their uses and compute jump functions associated
2228 with actual arguments of calls from within NODE. */
2230 void
2231 ipa_analyze_node (struct cgraph_node *node)
2233 struct ipa_node_params *info;
2234 struct param_analysis_info *parms_ainfo;
2235 int param_count;
2237 ipa_check_create_node_params ();
2238 ipa_check_create_edge_args ();
2239 info = IPA_NODE_REF (node);
2240 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2241 ipa_initialize_node_params (node);
2243 param_count = ipa_get_param_count (info);
2244 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
2245 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
2247 ipa_analyze_params_uses (node, parms_ainfo);
2248 ipa_compute_jump_functions (node, parms_ainfo);
2250 free_parms_ainfo (parms_ainfo, param_count);
2251 pop_cfun ();
2254 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2255 attempt a type-based devirtualization. If successful, return the
2256 target function declaration, otherwise return NULL. */
2258 tree
2259 ipa_intraprocedural_devirtualization (gimple call)
2261 tree binfo, token, fndecl;
2262 struct ipa_jump_func jfunc;
2263 tree otr = gimple_call_fn (call);
2265 jfunc.type = IPA_JF_UNKNOWN;
2266 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2267 call, obj_type_ref_class (otr));
2268 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2269 return NULL_TREE;
2270 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2271 if (!binfo)
2272 return NULL_TREE;
2273 token = OBJ_TYPE_REF_TOKEN (otr);
2274 fndecl = gimple_get_virt_method_for_binfo (tree_to_uhwi (token),
2275 binfo);
2276 #ifdef ENABLE_CHECKING
2277 if (fndecl)
2278 gcc_assert (possible_polymorphic_call_target_p
2279 (otr, cgraph_get_node (fndecl)));
2280 #endif
2281 return fndecl;
2284 /* Update the jump function DST when the call graph edge corresponding to SRC is
2285 is being inlined, knowing that DST is of type ancestor and src of known
2286 type. */
2288 static void
2289 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2290 struct ipa_jump_func *dst)
2292 HOST_WIDE_INT combined_offset;
2293 tree combined_type;
2295 if (!ipa_get_jf_ancestor_type_preserved (dst))
2297 dst->type = IPA_JF_UNKNOWN;
2298 return;
2301 combined_offset = ipa_get_jf_known_type_offset (src)
2302 + ipa_get_jf_ancestor_offset (dst);
2303 combined_type = ipa_get_jf_ancestor_type (dst);
2305 ipa_set_jf_known_type (dst, combined_offset,
2306 ipa_get_jf_known_type_base_type (src),
2307 combined_type);
2310 /* Update the jump functions associated with call graph edge E when the call
2311 graph edge CS is being inlined, assuming that E->caller is already (possibly
2312 indirectly) inlined into CS->callee and that E has not been inlined. */
2314 static void
2315 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2316 struct cgraph_edge *e)
2318 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2319 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2320 int count = ipa_get_cs_argument_count (args);
2321 int i;
2323 for (i = 0; i < count; i++)
2325 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2327 if (dst->type == IPA_JF_ANCESTOR)
2329 struct ipa_jump_func *src;
2330 int dst_fid = dst->value.ancestor.formal_id;
2332 /* Variable number of arguments can cause havoc if we try to access
2333 one that does not exist in the inlined edge. So make sure we
2334 don't. */
2335 if (dst_fid >= ipa_get_cs_argument_count (top))
2337 dst->type = IPA_JF_UNKNOWN;
2338 continue;
2341 src = ipa_get_ith_jump_func (top, dst_fid);
2343 if (src->agg.items
2344 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2346 struct ipa_agg_jf_item *item;
2347 int j;
2349 /* Currently we do not produce clobber aggregate jump functions,
2350 replace with merging when we do. */
2351 gcc_assert (!dst->agg.items);
2353 dst->agg.items = vec_safe_copy (src->agg.items);
2354 dst->agg.by_ref = src->agg.by_ref;
2355 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2356 item->offset -= dst->value.ancestor.offset;
2359 if (src->type == IPA_JF_KNOWN_TYPE)
2360 combine_known_type_and_ancestor_jfs (src, dst);
2361 else if (src->type == IPA_JF_PASS_THROUGH
2362 && src->value.pass_through.operation == NOP_EXPR)
2364 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2365 dst->value.ancestor.agg_preserved &=
2366 src->value.pass_through.agg_preserved;
2367 dst->value.ancestor.type_preserved &=
2368 src->value.pass_through.type_preserved;
2370 else if (src->type == IPA_JF_ANCESTOR)
2372 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2373 dst->value.ancestor.offset += src->value.ancestor.offset;
2374 dst->value.ancestor.agg_preserved &=
2375 src->value.ancestor.agg_preserved;
2376 dst->value.ancestor.type_preserved &=
2377 src->value.ancestor.type_preserved;
2379 else
2380 dst->type = IPA_JF_UNKNOWN;
2382 else if (dst->type == IPA_JF_PASS_THROUGH)
2384 struct ipa_jump_func *src;
2385 /* We must check range due to calls with variable number of arguments
2386 and we cannot combine jump functions with operations. */
2387 if (dst->value.pass_through.operation == NOP_EXPR
2388 && (dst->value.pass_through.formal_id
2389 < ipa_get_cs_argument_count (top)))
2391 int dst_fid = dst->value.pass_through.formal_id;
2392 src = ipa_get_ith_jump_func (top, dst_fid);
2393 bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
2395 switch (src->type)
2397 case IPA_JF_UNKNOWN:
2398 dst->type = IPA_JF_UNKNOWN;
2399 break;
2400 case IPA_JF_KNOWN_TYPE:
2401 if (ipa_get_jf_pass_through_type_preserved (dst))
2402 ipa_set_jf_known_type (dst,
2403 ipa_get_jf_known_type_offset (src),
2404 ipa_get_jf_known_type_base_type (src),
2405 ipa_get_jf_known_type_component_type (src));
2406 else
2407 dst->type = IPA_JF_UNKNOWN;
2408 break;
2409 case IPA_JF_CONST:
2410 ipa_set_jf_cst_copy (dst, src);
2411 break;
2413 case IPA_JF_PASS_THROUGH:
2415 int formal_id = ipa_get_jf_pass_through_formal_id (src);
2416 enum tree_code operation;
2417 operation = ipa_get_jf_pass_through_operation (src);
2419 if (operation == NOP_EXPR)
2421 bool agg_p, type_p;
2422 agg_p = dst_agg_p
2423 && ipa_get_jf_pass_through_agg_preserved (src);
2424 type_p = ipa_get_jf_pass_through_type_preserved (src)
2425 && ipa_get_jf_pass_through_type_preserved (dst);
2426 ipa_set_jf_simple_pass_through (dst, formal_id,
2427 agg_p, type_p);
2429 else
2431 tree operand = ipa_get_jf_pass_through_operand (src);
2432 ipa_set_jf_arith_pass_through (dst, formal_id, operand,
2433 operation);
2435 break;
2437 case IPA_JF_ANCESTOR:
2439 bool agg_p, type_p;
2440 agg_p = dst_agg_p
2441 && ipa_get_jf_ancestor_agg_preserved (src);
2442 type_p = ipa_get_jf_ancestor_type_preserved (src)
2443 && ipa_get_jf_pass_through_type_preserved (dst);
2444 ipa_set_ancestor_jf (dst,
2445 ipa_get_jf_ancestor_offset (src),
2446 ipa_get_jf_ancestor_type (src),
2447 ipa_get_jf_ancestor_formal_id (src),
2448 agg_p, type_p);
2449 break;
2451 default:
2452 gcc_unreachable ();
2455 if (src->agg.items
2456 && (dst_agg_p || !src->agg.by_ref))
2458 /* Currently we do not produce clobber aggregate jump
2459 functions, replace with merging when we do. */
2460 gcc_assert (!dst->agg.items);
2462 dst->agg.by_ref = src->agg.by_ref;
2463 dst->agg.items = vec_safe_copy (src->agg.items);
2466 else
2467 dst->type = IPA_JF_UNKNOWN;
2472 /* If TARGET is an addr_expr of a function declaration, make it the destination
2473 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2475 struct cgraph_edge *
2476 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2478 struct cgraph_node *callee;
2479 struct inline_edge_summary *es = inline_edge_summary (ie);
2480 bool unreachable = false;
2482 if (TREE_CODE (target) == ADDR_EXPR)
2483 target = TREE_OPERAND (target, 0);
2484 if (TREE_CODE (target) != FUNCTION_DECL)
2486 target = canonicalize_constructor_val (target, NULL);
2487 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2489 if (ie->indirect_info->member_ptr)
2490 /* Member pointer call that goes through a VMT lookup. */
2491 return NULL;
2493 if (dump_file)
2494 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2495 " in %s/%i, making it unreachable.\n",
2496 ie->caller->name (), ie->caller->order);
2497 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2498 callee = cgraph_get_create_node (target);
2499 unreachable = true;
2501 else
2502 callee = cgraph_get_node (target);
2504 else
2505 callee = cgraph_get_node (target);
2507 /* Because may-edges are not explicitely represented and vtable may be external,
2508 we may create the first reference to the object in the unit. */
2509 if (!callee || callee->global.inlined_to)
2512 /* We are better to ensure we can refer to it.
2513 In the case of static functions we are out of luck, since we already
2514 removed its body. In the case of public functions we may or may
2515 not introduce the reference. */
2516 if (!canonicalize_constructor_val (target, NULL)
2517 || !TREE_PUBLIC (target))
2519 if (dump_file)
2520 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2521 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2522 xstrdup (ie->caller->name ()),
2523 ie->caller->order,
2524 xstrdup (ie->callee->name ()),
2525 ie->callee->order);
2526 return NULL;
2528 callee = cgraph_get_create_node (target);
2530 ipa_check_create_node_params ();
2532 /* We can not make edges to inline clones. It is bug that someone removed
2533 the cgraph node too early. */
2534 gcc_assert (!callee->global.inlined_to);
2536 if (dump_file && !unreachable)
2538 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2539 "(%s/%i -> %s/%i), for stmt ",
2540 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2541 xstrdup (ie->caller->name ()),
2542 ie->caller->order,
2543 xstrdup (callee->name ()),
2544 callee->order);
2545 if (ie->call_stmt)
2546 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2547 else
2548 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2550 ie = cgraph_make_edge_direct (ie, callee);
2551 es = inline_edge_summary (ie);
2552 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2553 - eni_size_weights.call_cost);
2554 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2555 - eni_time_weights.call_cost);
2557 return ie;
2560 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2561 return NULL if there is not any. BY_REF specifies whether the value has to
2562 be passed by reference or by value. */
2564 tree
2565 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2566 HOST_WIDE_INT offset, bool by_ref)
2568 struct ipa_agg_jf_item *item;
2569 int i;
2571 if (by_ref != agg->by_ref)
2572 return NULL;
2574 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2575 if (item->offset == offset)
2577 /* Currently we do not have clobber values, return NULL for them once
2578 we do. */
2579 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2580 return item->value;
2582 return NULL;
2585 /* Remove a reference to SYMBOL from the list of references of a node given by
2586 reference description RDESC. Return true if the reference has been
2587 successfully found and removed. */
2589 static bool
2590 remove_described_reference (symtab_node *symbol, struct ipa_cst_ref_desc *rdesc)
2592 struct ipa_ref *to_del;
2593 struct cgraph_edge *origin;
2595 origin = rdesc->cs;
2596 if (!origin)
2597 return false;
2598 to_del = ipa_find_reference (origin->caller, symbol,
2599 origin->call_stmt, origin->lto_stmt_uid);
2600 if (!to_del)
2601 return false;
2603 ipa_remove_reference (to_del);
2604 if (dump_file)
2605 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2606 xstrdup (origin->caller->name ()),
2607 origin->caller->order, xstrdup (symbol->name ()));
2608 return true;
2611 /* If JFUNC has a reference description with refcount different from
2612 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2613 NULL. JFUNC must be a constant jump function. */
2615 static struct ipa_cst_ref_desc *
2616 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2618 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2619 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2620 return rdesc;
2621 else
2622 return NULL;
2625 /* If the value of constant jump function JFUNC is an address of a function
2626 declaration, return the associated call graph node. Otherwise return
2627 NULL. */
2629 static cgraph_node *
2630 cgraph_node_for_jfunc (struct ipa_jump_func *jfunc)
2632 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
2633 tree cst = ipa_get_jf_constant (jfunc);
2634 if (TREE_CODE (cst) != ADDR_EXPR
2635 || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL)
2636 return NULL;
2638 return cgraph_get_node (TREE_OPERAND (cst, 0));
2642 /* If JFUNC is a constant jump function with a usable rdesc, decrement its
2643 refcount and if it hits zero, remove reference to SYMBOL from the caller of
2644 the edge specified in the rdesc. Return false if either the symbol or the
2645 reference could not be found, otherwise return true. */
2647 static bool
2648 try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
2650 struct ipa_cst_ref_desc *rdesc;
2651 if (jfunc->type == IPA_JF_CONST
2652 && (rdesc = jfunc_rdesc_usable (jfunc))
2653 && --rdesc->refcount == 0)
2655 symtab_node *symbol = cgraph_node_for_jfunc (jfunc);
2656 if (!symbol)
2657 return false;
2659 return remove_described_reference (symbol, rdesc);
2661 return true;
2664 /* Try to find a destination for indirect edge IE that corresponds to a simple
2665 call or a call of a member function pointer and where the destination is a
2666 pointer formal parameter described by jump function JFUNC. If it can be
2667 determined, return the newly direct edge, otherwise return NULL.
2668 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2670 static struct cgraph_edge *
2671 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2672 struct ipa_jump_func *jfunc,
2673 struct ipa_node_params *new_root_info)
2675 struct cgraph_edge *cs;
2676 tree target;
2677 bool agg_contents = ie->indirect_info->agg_contents;
2679 if (ie->indirect_info->agg_contents)
2680 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2681 ie->indirect_info->offset,
2682 ie->indirect_info->by_ref);
2683 else
2684 target = ipa_value_from_jfunc (new_root_info, jfunc);
2685 if (!target)
2686 return NULL;
2687 cs = ipa_make_edge_direct_to_target (ie, target);
2689 if (cs && !agg_contents)
2691 bool ok;
2692 gcc_checking_assert (cs->callee
2693 && (cs != ie
2694 || jfunc->type != IPA_JF_CONST
2695 || !cgraph_node_for_jfunc (jfunc)
2696 || cs->callee == cgraph_node_for_jfunc (jfunc)));
2697 ok = try_decrement_rdesc_refcount (jfunc);
2698 gcc_checking_assert (ok);
2701 return cs;
2704 /* Return the target to be used in cases of impossible devirtualization. IE
2705 and target (the latter can be NULL) are dumped when dumping is enabled. */
2707 tree
2708 ipa_impossible_devirt_target (struct cgraph_edge *ie, tree target)
2710 if (dump_file)
2712 if (target)
2713 fprintf (dump_file,
2714 "Type inconsistent devirtualization: %s/%i->%s\n",
2715 ie->caller->name (), ie->caller->order,
2716 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (target)));
2717 else
2718 fprintf (dump_file,
2719 "No devirtualization target in %s/%i\n",
2720 ie->caller->name (), ie->caller->order);
2722 tree new_target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2723 cgraph_get_create_node (new_target);
2724 return new_target;
2727 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2728 call based on a formal parameter which is described by jump function JFUNC
2729 and if it can be determined, make it direct and return the direct edge.
2730 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2731 are relative to. */
2733 static struct cgraph_edge *
2734 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2735 struct ipa_jump_func *jfunc,
2736 struct ipa_node_params *new_root_info)
2738 tree binfo, target;
2740 if (!flag_devirtualize)
2741 return NULL;
2743 /* First try to do lookup via known virtual table pointer value. */
2744 if (!ie->indirect_info->by_ref)
2746 tree vtable;
2747 unsigned HOST_WIDE_INT offset;
2748 tree t = ipa_find_agg_cst_for_param (&jfunc->agg,
2749 ie->indirect_info->offset,
2750 true);
2751 if (t && vtable_pointer_value_to_vtable (t, &vtable, &offset))
2753 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
2754 vtable, offset);
2755 if (target)
2757 if ((TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE
2758 && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE)
2759 || !possible_polymorphic_call_target_p
2760 (ie, cgraph_get_node (target)))
2761 target = ipa_impossible_devirt_target (ie, target);
2762 return ipa_make_edge_direct_to_target (ie, target);
2767 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2769 if (!binfo)
2770 return NULL;
2772 if (TREE_CODE (binfo) != TREE_BINFO)
2774 ipa_polymorphic_call_context context;
2775 vec <cgraph_node *>targets;
2776 bool final;
2778 if (!get_polymorphic_call_info_from_invariant
2779 (&context, binfo, ie->indirect_info->otr_type,
2780 ie->indirect_info->offset))
2781 return NULL;
2782 targets = possible_polymorphic_call_targets
2783 (ie->indirect_info->otr_type,
2784 ie->indirect_info->otr_token,
2785 context, &final);
2786 if (!final || targets.length () > 1)
2787 return NULL;
2788 if (targets.length () == 1)
2789 target = targets[0]->decl;
2790 else
2791 target = ipa_impossible_devirt_target (ie, NULL_TREE);
2793 else
2795 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2796 ie->indirect_info->otr_type);
2797 if (binfo)
2798 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2799 binfo);
2800 else
2801 return NULL;
2804 if (target)
2806 if (!possible_polymorphic_call_target_p (ie, cgraph_get_node (target)))
2807 target = ipa_impossible_devirt_target (ie, target);
2808 return ipa_make_edge_direct_to_target (ie, target);
2810 else
2811 return NULL;
2814 /* Update the param called notes associated with NODE when CS is being inlined,
2815 assuming NODE is (potentially indirectly) inlined into CS->callee.
2816 Moreover, if the callee is discovered to be constant, create a new cgraph
2817 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2818 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2820 static bool
2821 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2822 struct cgraph_node *node,
2823 vec<cgraph_edge_p> *new_edges)
2825 struct ipa_edge_args *top;
2826 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2827 struct ipa_node_params *new_root_info;
2828 bool res = false;
2830 ipa_check_create_edge_args ();
2831 top = IPA_EDGE_REF (cs);
2832 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2833 ? cs->caller->global.inlined_to
2834 : cs->caller);
2836 for (ie = node->indirect_calls; ie; ie = next_ie)
2838 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2839 struct ipa_jump_func *jfunc;
2840 int param_index;
2842 next_ie = ie->next_callee;
2844 if (ici->param_index == -1)
2845 continue;
2847 /* We must check range due to calls with variable number of arguments: */
2848 if (ici->param_index >= ipa_get_cs_argument_count (top))
2850 ici->param_index = -1;
2851 continue;
2854 param_index = ici->param_index;
2855 jfunc = ipa_get_ith_jump_func (top, param_index);
2857 if (!flag_indirect_inlining)
2858 new_direct_edge = NULL;
2859 else if (ici->polymorphic)
2860 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2861 new_root_info);
2862 else
2863 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2864 new_root_info);
2865 /* If speculation was removed, then we need to do nothing. */
2866 if (new_direct_edge && new_direct_edge != ie)
2868 new_direct_edge->indirect_inlining_edge = 1;
2869 top = IPA_EDGE_REF (cs);
2870 res = true;
2872 else if (new_direct_edge)
2874 new_direct_edge->indirect_inlining_edge = 1;
2875 if (new_direct_edge->call_stmt)
2876 new_direct_edge->call_stmt_cannot_inline_p
2877 = !gimple_check_call_matching_types (
2878 new_direct_edge->call_stmt,
2879 new_direct_edge->callee->decl, false);
2880 if (new_edges)
2882 new_edges->safe_push (new_direct_edge);
2883 res = true;
2885 top = IPA_EDGE_REF (cs);
2887 else if (jfunc->type == IPA_JF_PASS_THROUGH
2888 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2890 if ((ici->agg_contents
2891 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2892 || (ici->polymorphic
2893 && !ipa_get_jf_pass_through_type_preserved (jfunc)))
2894 ici->param_index = -1;
2895 else
2896 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2898 else if (jfunc->type == IPA_JF_ANCESTOR)
2900 if ((ici->agg_contents
2901 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2902 || (ici->polymorphic
2903 && !ipa_get_jf_ancestor_type_preserved (jfunc)))
2904 ici->param_index = -1;
2905 else
2907 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2908 if (ipa_get_jf_ancestor_offset (jfunc))
2909 ici->outer_type = NULL;
2910 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2913 else
2914 /* Either we can find a destination for this edge now or never. */
2915 ici->param_index = -1;
2918 return res;
2921 /* Recursively traverse subtree of NODE (including node) made of inlined
2922 cgraph_edges when CS has been inlined and invoke
2923 update_indirect_edges_after_inlining on all nodes and
2924 update_jump_functions_after_inlining on all non-inlined edges that lead out
2925 of this subtree. Newly discovered indirect edges will be added to
2926 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2927 created. */
2929 static bool
2930 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2931 struct cgraph_node *node,
2932 vec<cgraph_edge_p> *new_edges)
2934 struct cgraph_edge *e;
2935 bool res;
2937 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2939 for (e = node->callees; e; e = e->next_callee)
2940 if (!e->inline_failed)
2941 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2942 else
2943 update_jump_functions_after_inlining (cs, e);
2944 for (e = node->indirect_calls; e; e = e->next_callee)
2945 update_jump_functions_after_inlining (cs, e);
2947 return res;
2950 /* Combine two controlled uses counts as done during inlining. */
2952 static int
2953 combine_controlled_uses_counters (int c, int d)
2955 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
2956 return IPA_UNDESCRIBED_USE;
2957 else
2958 return c + d - 1;
2961 /* Propagate number of controlled users from CS->caleee to the new root of the
2962 tree of inlined nodes. */
2964 static void
2965 propagate_controlled_uses (struct cgraph_edge *cs)
2967 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
2968 struct cgraph_node *new_root = cs->caller->global.inlined_to
2969 ? cs->caller->global.inlined_to : cs->caller;
2970 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
2971 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
2972 int count, i;
2974 count = MIN (ipa_get_cs_argument_count (args),
2975 ipa_get_param_count (old_root_info));
2976 for (i = 0; i < count; i++)
2978 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2979 struct ipa_cst_ref_desc *rdesc;
2981 if (jf->type == IPA_JF_PASS_THROUGH)
2983 int src_idx, c, d;
2984 src_idx = ipa_get_jf_pass_through_formal_id (jf);
2985 c = ipa_get_controlled_uses (new_root_info, src_idx);
2986 d = ipa_get_controlled_uses (old_root_info, i);
2988 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
2989 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
2990 c = combine_controlled_uses_counters (c, d);
2991 ipa_set_controlled_uses (new_root_info, src_idx, c);
2992 if (c == 0 && new_root_info->ipcp_orig_node)
2994 struct cgraph_node *n;
2995 struct ipa_ref *ref;
2996 tree t = new_root_info->known_vals[src_idx];
2998 if (t && TREE_CODE (t) == ADDR_EXPR
2999 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
3000 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
3001 && (ref = ipa_find_reference (new_root,
3002 n, NULL, 0)))
3004 if (dump_file)
3005 fprintf (dump_file, "ipa-prop: Removing cloning-created "
3006 "reference from %s/%i to %s/%i.\n",
3007 xstrdup (new_root->name ()),
3008 new_root->order,
3009 xstrdup (n->name ()), n->order);
3010 ipa_remove_reference (ref);
3014 else if (jf->type == IPA_JF_CONST
3015 && (rdesc = jfunc_rdesc_usable (jf)))
3017 int d = ipa_get_controlled_uses (old_root_info, i);
3018 int c = rdesc->refcount;
3019 rdesc->refcount = combine_controlled_uses_counters (c, d);
3020 if (rdesc->refcount == 0)
3022 tree cst = ipa_get_jf_constant (jf);
3023 struct cgraph_node *n;
3024 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
3025 && TREE_CODE (TREE_OPERAND (cst, 0))
3026 == FUNCTION_DECL);
3027 n = cgraph_get_node (TREE_OPERAND (cst, 0));
3028 if (n)
3030 struct cgraph_node *clone;
3031 bool ok;
3032 ok = remove_described_reference (n, rdesc);
3033 gcc_checking_assert (ok);
3035 clone = cs->caller;
3036 while (clone->global.inlined_to
3037 && clone != rdesc->cs->caller
3038 && IPA_NODE_REF (clone)->ipcp_orig_node)
3040 struct ipa_ref *ref;
3041 ref = ipa_find_reference (clone,
3042 n, NULL, 0);
3043 if (ref)
3045 if (dump_file)
3046 fprintf (dump_file, "ipa-prop: Removing "
3047 "cloning-created reference "
3048 "from %s/%i to %s/%i.\n",
3049 xstrdup (clone->name ()),
3050 clone->order,
3051 xstrdup (n->name ()),
3052 n->order);
3053 ipa_remove_reference (ref);
3055 clone = clone->callers->caller;
3062 for (i = ipa_get_param_count (old_root_info);
3063 i < ipa_get_cs_argument_count (args);
3064 i++)
3066 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
3068 if (jf->type == IPA_JF_CONST)
3070 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
3071 if (rdesc)
3072 rdesc->refcount = IPA_UNDESCRIBED_USE;
3074 else if (jf->type == IPA_JF_PASS_THROUGH)
3075 ipa_set_controlled_uses (new_root_info,
3076 jf->value.pass_through.formal_id,
3077 IPA_UNDESCRIBED_USE);
3081 /* Update jump functions and call note functions on inlining the call site CS.
3082 CS is expected to lead to a node already cloned by
3083 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
3084 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
3085 created. */
3087 bool
3088 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
3089 vec<cgraph_edge_p> *new_edges)
3091 bool changed;
3092 /* Do nothing if the preparation phase has not been carried out yet
3093 (i.e. during early inlining). */
3094 if (!ipa_node_params_vector.exists ())
3095 return false;
3096 gcc_assert (ipa_edge_args_vector);
3098 propagate_controlled_uses (cs);
3099 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
3101 return changed;
3104 /* Frees all dynamically allocated structures that the argument info points
3105 to. */
3107 void
3108 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
3110 vec_free (args->jump_functions);
3111 memset (args, 0, sizeof (*args));
3114 /* Free all ipa_edge structures. */
3116 void
3117 ipa_free_all_edge_args (void)
3119 int i;
3120 struct ipa_edge_args *args;
3122 if (!ipa_edge_args_vector)
3123 return;
3125 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
3126 ipa_free_edge_args_substructures (args);
3128 vec_free (ipa_edge_args_vector);
3131 /* Frees all dynamically allocated structures that the param info points
3132 to. */
3134 void
3135 ipa_free_node_params_substructures (struct ipa_node_params *info)
3137 info->descriptors.release ();
3138 free (info->lattices);
3139 /* Lattice values and their sources are deallocated with their alocation
3140 pool. */
3141 info->known_vals.release ();
3142 memset (info, 0, sizeof (*info));
3145 /* Free all ipa_node_params structures. */
3147 void
3148 ipa_free_all_node_params (void)
3150 int i;
3151 struct ipa_node_params *info;
3153 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
3154 ipa_free_node_params_substructures (info);
3156 ipa_node_params_vector.release ();
3159 /* Set the aggregate replacements of NODE to be AGGVALS. */
3161 void
3162 ipa_set_node_agg_value_chain (struct cgraph_node *node,
3163 struct ipa_agg_replacement_value *aggvals)
3165 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
3166 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
3168 (*ipa_node_agg_replacements)[node->uid] = aggvals;
3171 /* Hook that is called by cgraph.c when an edge is removed. */
3173 static void
3174 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
3176 struct ipa_edge_args *args;
3178 /* During IPA-CP updating we can be called on not-yet analyzed clones. */
3179 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
3180 return;
3182 args = IPA_EDGE_REF (cs);
3183 if (args->jump_functions)
3185 struct ipa_jump_func *jf;
3186 int i;
3187 FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
3189 struct ipa_cst_ref_desc *rdesc;
3190 try_decrement_rdesc_refcount (jf);
3191 if (jf->type == IPA_JF_CONST
3192 && (rdesc = ipa_get_jf_constant_rdesc (jf))
3193 && rdesc->cs == cs)
3194 rdesc->cs = NULL;
3198 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
3201 /* Hook that is called by cgraph.c when a node is removed. */
3203 static void
3204 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3206 /* During IPA-CP updating we can be called on not-yet analyze clones. */
3207 if (ipa_node_params_vector.length () > (unsigned)node->uid)
3208 ipa_free_node_params_substructures (IPA_NODE_REF (node));
3209 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
3210 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
3213 /* Hook that is called by cgraph.c when an edge is duplicated. */
3215 static void
3216 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3217 __attribute__((unused)) void *data)
3219 struct ipa_edge_args *old_args, *new_args;
3220 unsigned int i;
3222 ipa_check_create_edge_args ();
3224 old_args = IPA_EDGE_REF (src);
3225 new_args = IPA_EDGE_REF (dst);
3227 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
3229 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
3231 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
3232 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
3234 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
3236 if (src_jf->type == IPA_JF_CONST)
3238 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
3240 if (!src_rdesc)
3241 dst_jf->value.constant.rdesc = NULL;
3242 else if (src->caller == dst->caller)
3244 struct ipa_ref *ref;
3245 symtab_node *n = cgraph_node_for_jfunc (src_jf);
3246 gcc_checking_assert (n);
3247 ref = ipa_find_reference (src->caller, n,
3248 src->call_stmt, src->lto_stmt_uid);
3249 gcc_checking_assert (ref);
3250 ipa_clone_ref (ref, dst->caller, ref->stmt);
3252 gcc_checking_assert (ipa_refdesc_pool);
3253 struct ipa_cst_ref_desc *dst_rdesc
3254 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3255 dst_rdesc->cs = dst;
3256 dst_rdesc->refcount = src_rdesc->refcount;
3257 dst_rdesc->next_duplicate = NULL;
3258 dst_jf->value.constant.rdesc = dst_rdesc;
3260 else if (src_rdesc->cs == src)
3262 struct ipa_cst_ref_desc *dst_rdesc;
3263 gcc_checking_assert (ipa_refdesc_pool);
3264 dst_rdesc
3265 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3266 dst_rdesc->cs = dst;
3267 dst_rdesc->refcount = src_rdesc->refcount;
3268 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
3269 src_rdesc->next_duplicate = dst_rdesc;
3270 dst_jf->value.constant.rdesc = dst_rdesc;
3272 else
3274 struct ipa_cst_ref_desc *dst_rdesc;
3275 /* This can happen during inlining, when a JFUNC can refer to a
3276 reference taken in a function up in the tree of inline clones.
3277 We need to find the duplicate that refers to our tree of
3278 inline clones. */
3280 gcc_assert (dst->caller->global.inlined_to);
3281 for (dst_rdesc = src_rdesc->next_duplicate;
3282 dst_rdesc;
3283 dst_rdesc = dst_rdesc->next_duplicate)
3285 struct cgraph_node *top;
3286 top = dst_rdesc->cs->caller->global.inlined_to
3287 ? dst_rdesc->cs->caller->global.inlined_to
3288 : dst_rdesc->cs->caller;
3289 if (dst->caller->global.inlined_to == top)
3290 break;
3292 gcc_assert (dst_rdesc);
3293 dst_jf->value.constant.rdesc = dst_rdesc;
3299 /* Hook that is called by cgraph.c when a node is duplicated. */
3301 static void
3302 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
3303 ATTRIBUTE_UNUSED void *data)
3305 struct ipa_node_params *old_info, *new_info;
3306 struct ipa_agg_replacement_value *old_av, *new_av;
3308 ipa_check_create_node_params ();
3309 old_info = IPA_NODE_REF (src);
3310 new_info = IPA_NODE_REF (dst);
3312 new_info->descriptors = old_info->descriptors.copy ();
3313 new_info->lattices = NULL;
3314 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
3316 new_info->uses_analysis_done = old_info->uses_analysis_done;
3317 new_info->node_enqueued = old_info->node_enqueued;
3319 old_av = ipa_get_agg_replacements_for_node (src);
3320 if (!old_av)
3321 return;
3323 new_av = NULL;
3324 while (old_av)
3326 struct ipa_agg_replacement_value *v;
3328 v = ggc_alloc_ipa_agg_replacement_value ();
3329 memcpy (v, old_av, sizeof (*v));
3330 v->next = new_av;
3331 new_av = v;
3332 old_av = old_av->next;
3334 ipa_set_node_agg_value_chain (dst, new_av);
3338 /* Analyze newly added function into callgraph. */
3340 static void
3341 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3343 if (cgraph_function_with_gimple_body_p (node))
3344 ipa_analyze_node (node);
3347 /* Register our cgraph hooks if they are not already there. */
3349 void
3350 ipa_register_cgraph_hooks (void)
3352 if (!edge_removal_hook_holder)
3353 edge_removal_hook_holder =
3354 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
3355 if (!node_removal_hook_holder)
3356 node_removal_hook_holder =
3357 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
3358 if (!edge_duplication_hook_holder)
3359 edge_duplication_hook_holder =
3360 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
3361 if (!node_duplication_hook_holder)
3362 node_duplication_hook_holder =
3363 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
3364 function_insertion_hook_holder =
3365 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
3368 /* Unregister our cgraph hooks if they are not already there. */
3370 static void
3371 ipa_unregister_cgraph_hooks (void)
3373 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3374 edge_removal_hook_holder = NULL;
3375 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3376 node_removal_hook_holder = NULL;
3377 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3378 edge_duplication_hook_holder = NULL;
3379 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3380 node_duplication_hook_holder = NULL;
3381 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3382 function_insertion_hook_holder = NULL;
3385 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3386 longer needed after ipa-cp. */
3388 void
3389 ipa_free_all_structures_after_ipa_cp (void)
3391 if (!optimize)
3393 ipa_free_all_edge_args ();
3394 ipa_free_all_node_params ();
3395 free_alloc_pool (ipcp_sources_pool);
3396 free_alloc_pool (ipcp_values_pool);
3397 free_alloc_pool (ipcp_agg_lattice_pool);
3398 ipa_unregister_cgraph_hooks ();
3399 if (ipa_refdesc_pool)
3400 free_alloc_pool (ipa_refdesc_pool);
3404 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3405 longer needed after indirect inlining. */
3407 void
3408 ipa_free_all_structures_after_iinln (void)
3410 ipa_free_all_edge_args ();
3411 ipa_free_all_node_params ();
3412 ipa_unregister_cgraph_hooks ();
3413 if (ipcp_sources_pool)
3414 free_alloc_pool (ipcp_sources_pool);
3415 if (ipcp_values_pool)
3416 free_alloc_pool (ipcp_values_pool);
3417 if (ipcp_agg_lattice_pool)
3418 free_alloc_pool (ipcp_agg_lattice_pool);
3419 if (ipa_refdesc_pool)
3420 free_alloc_pool (ipa_refdesc_pool);
3423 /* Print ipa_tree_map data structures of all functions in the
3424 callgraph to F. */
3426 void
3427 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3429 int i, count;
3430 struct ipa_node_params *info;
3432 if (!node->definition)
3433 return;
3434 info = IPA_NODE_REF (node);
3435 fprintf (f, " function %s/%i parameter descriptors:\n",
3436 node->name (), node->order);
3437 count = ipa_get_param_count (info);
3438 for (i = 0; i < count; i++)
3440 int c;
3442 fprintf (f, " ");
3443 ipa_dump_param (f, info, i);
3444 if (ipa_is_param_used (info, i))
3445 fprintf (f, " used");
3446 c = ipa_get_controlled_uses (info, i);
3447 if (c == IPA_UNDESCRIBED_USE)
3448 fprintf (f, " undescribed_use");
3449 else
3450 fprintf (f, " controlled_uses=%i", c);
3451 fprintf (f, "\n");
3455 /* Print ipa_tree_map data structures of all functions in the
3456 callgraph to F. */
3458 void
3459 ipa_print_all_params (FILE * f)
3461 struct cgraph_node *node;
3463 fprintf (f, "\nFunction parameters:\n");
3464 FOR_EACH_FUNCTION (node)
3465 ipa_print_node_params (f, node);
3468 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3470 vec<tree>
3471 ipa_get_vector_of_formal_parms (tree fndecl)
3473 vec<tree> args;
3474 int count;
3475 tree parm;
3477 gcc_assert (!flag_wpa);
3478 count = count_formal_params (fndecl);
3479 args.create (count);
3480 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3481 args.quick_push (parm);
3483 return args;
3486 /* Return a heap allocated vector containing types of formal parameters of
3487 function type FNTYPE. */
3489 vec<tree>
3490 ipa_get_vector_of_formal_parm_types (tree fntype)
3492 vec<tree> types;
3493 int count = 0;
3494 tree t;
3496 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3497 count++;
3499 types.create (count);
3500 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3501 types.quick_push (TREE_VALUE (t));
3503 return types;
3506 /* Modify the function declaration FNDECL and its type according to the plan in
3507 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3508 to reflect the actual parameters being modified which are determined by the
3509 base_index field. */
3511 void
3512 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments)
3514 vec<tree> oparms = ipa_get_vector_of_formal_parms (fndecl);
3515 tree orig_type = TREE_TYPE (fndecl);
3516 tree old_arg_types = TYPE_ARG_TYPES (orig_type);
3518 /* The following test is an ugly hack, some functions simply don't have any
3519 arguments in their type. This is probably a bug but well... */
3520 bool care_for_types = (old_arg_types != NULL_TREE);
3521 bool last_parm_void;
3522 vec<tree> otypes;
3523 if (care_for_types)
3525 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3526 == void_type_node);
3527 otypes = ipa_get_vector_of_formal_parm_types (orig_type);
3528 if (last_parm_void)
3529 gcc_assert (oparms.length () + 1 == otypes.length ());
3530 else
3531 gcc_assert (oparms.length () == otypes.length ());
3533 else
3535 last_parm_void = false;
3536 otypes.create (0);
3539 int len = adjustments.length ();
3540 tree *link = &DECL_ARGUMENTS (fndecl);
3541 tree new_arg_types = NULL;
3542 for (int i = 0; i < len; i++)
3544 struct ipa_parm_adjustment *adj;
3545 gcc_assert (link);
3547 adj = &adjustments[i];
3548 tree parm;
3549 if (adj->op == IPA_PARM_OP_NEW)
3550 parm = NULL;
3551 else
3552 parm = oparms[adj->base_index];
3553 adj->base = parm;
3555 if (adj->op == IPA_PARM_OP_COPY)
3557 if (care_for_types)
3558 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3559 new_arg_types);
3560 *link = parm;
3561 link = &DECL_CHAIN (parm);
3563 else if (adj->op != IPA_PARM_OP_REMOVE)
3565 tree new_parm;
3566 tree ptype;
3568 if (adj->by_ref)
3569 ptype = build_pointer_type (adj->type);
3570 else
3572 ptype = adj->type;
3573 if (is_gimple_reg_type (ptype))
3575 unsigned malign = GET_MODE_ALIGNMENT (TYPE_MODE (ptype));
3576 if (TYPE_ALIGN (ptype) < malign)
3577 ptype = build_aligned_type (ptype, malign);
3581 if (care_for_types)
3582 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3584 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3585 ptype);
3586 const char *prefix = adj->arg_prefix ? adj->arg_prefix : "SYNTH";
3587 DECL_NAME (new_parm) = create_tmp_var_name (prefix);
3588 DECL_ARTIFICIAL (new_parm) = 1;
3589 DECL_ARG_TYPE (new_parm) = ptype;
3590 DECL_CONTEXT (new_parm) = fndecl;
3591 TREE_USED (new_parm) = 1;
3592 DECL_IGNORED_P (new_parm) = 1;
3593 layout_decl (new_parm, 0);
3595 if (adj->op == IPA_PARM_OP_NEW)
3596 adj->base = NULL;
3597 else
3598 adj->base = parm;
3599 adj->new_decl = new_parm;
3601 *link = new_parm;
3602 link = &DECL_CHAIN (new_parm);
3606 *link = NULL_TREE;
3608 tree new_reversed = NULL;
3609 if (care_for_types)
3611 new_reversed = nreverse (new_arg_types);
3612 if (last_parm_void)
3614 if (new_reversed)
3615 TREE_CHAIN (new_arg_types) = void_list_node;
3616 else
3617 new_reversed = void_list_node;
3621 /* Use copy_node to preserve as much as possible from original type
3622 (debug info, attribute lists etc.)
3623 Exception is METHOD_TYPEs must have THIS argument.
3624 When we are asked to remove it, we need to build new FUNCTION_TYPE
3625 instead. */
3626 tree new_type = NULL;
3627 if (TREE_CODE (orig_type) != METHOD_TYPE
3628 || (adjustments[0].op == IPA_PARM_OP_COPY
3629 && adjustments[0].base_index == 0))
3631 new_type = build_distinct_type_copy (orig_type);
3632 TYPE_ARG_TYPES (new_type) = new_reversed;
3634 else
3636 new_type
3637 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3638 new_reversed));
3639 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3640 DECL_VINDEX (fndecl) = NULL_TREE;
3643 /* When signature changes, we need to clear builtin info. */
3644 if (DECL_BUILT_IN (fndecl))
3646 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3647 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3650 /* This is a new type, not a copy of an old type. Need to reassociate
3651 variants. We can handle everything except the main variant lazily. */
3652 tree t = TYPE_MAIN_VARIANT (orig_type);
3653 if (orig_type != t)
3655 TYPE_MAIN_VARIANT (new_type) = t;
3656 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3657 TYPE_NEXT_VARIANT (t) = new_type;
3659 else
3661 TYPE_MAIN_VARIANT (new_type) = new_type;
3662 TYPE_NEXT_VARIANT (new_type) = NULL;
3665 TREE_TYPE (fndecl) = new_type;
3666 DECL_VIRTUAL_P (fndecl) = 0;
3667 DECL_LANG_SPECIFIC (fndecl) = NULL;
3668 otypes.release ();
3669 oparms.release ();
3672 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3673 If this is a directly recursive call, CS must be NULL. Otherwise it must
3674 contain the corresponding call graph edge. */
3676 void
3677 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3678 ipa_parm_adjustment_vec adjustments)
3680 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3681 vec<tree> vargs;
3682 vec<tree, va_gc> **debug_args = NULL;
3683 gimple new_stmt;
3684 gimple_stmt_iterator gsi, prev_gsi;
3685 tree callee_decl;
3686 int i, len;
3688 len = adjustments.length ();
3689 vargs.create (len);
3690 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
3691 ipa_remove_stmt_references (current_node, stmt);
3693 gsi = gsi_for_stmt (stmt);
3694 prev_gsi = gsi;
3695 gsi_prev (&prev_gsi);
3696 for (i = 0; i < len; i++)
3698 struct ipa_parm_adjustment *adj;
3700 adj = &adjustments[i];
3702 if (adj->op == IPA_PARM_OP_COPY)
3704 tree arg = gimple_call_arg (stmt, adj->base_index);
3706 vargs.quick_push (arg);
3708 else if (adj->op != IPA_PARM_OP_REMOVE)
3710 tree expr, base, off;
3711 location_t loc;
3712 unsigned int deref_align = 0;
3713 bool deref_base = false;
3715 /* We create a new parameter out of the value of the old one, we can
3716 do the following kind of transformations:
3718 - A scalar passed by reference is converted to a scalar passed by
3719 value. (adj->by_ref is false and the type of the original
3720 actual argument is a pointer to a scalar).
3722 - A part of an aggregate is passed instead of the whole aggregate.
3723 The part can be passed either by value or by reference, this is
3724 determined by value of adj->by_ref. Moreover, the code below
3725 handles both situations when the original aggregate is passed by
3726 value (its type is not a pointer) and when it is passed by
3727 reference (it is a pointer to an aggregate).
3729 When the new argument is passed by reference (adj->by_ref is true)
3730 it must be a part of an aggregate and therefore we form it by
3731 simply taking the address of a reference inside the original
3732 aggregate. */
3734 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3735 base = gimple_call_arg (stmt, adj->base_index);
3736 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3737 : EXPR_LOCATION (base);
3739 if (TREE_CODE (base) != ADDR_EXPR
3740 && POINTER_TYPE_P (TREE_TYPE (base)))
3741 off = build_int_cst (adj->alias_ptr_type,
3742 adj->offset / BITS_PER_UNIT);
3743 else
3745 HOST_WIDE_INT base_offset;
3746 tree prev_base;
3747 bool addrof;
3749 if (TREE_CODE (base) == ADDR_EXPR)
3751 base = TREE_OPERAND (base, 0);
3752 addrof = true;
3754 else
3755 addrof = false;
3756 prev_base = base;
3757 base = get_addr_base_and_unit_offset (base, &base_offset);
3758 /* Aggregate arguments can have non-invariant addresses. */
3759 if (!base)
3761 base = build_fold_addr_expr (prev_base);
3762 off = build_int_cst (adj->alias_ptr_type,
3763 adj->offset / BITS_PER_UNIT);
3765 else if (TREE_CODE (base) == MEM_REF)
3767 if (!addrof)
3769 deref_base = true;
3770 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3772 off = build_int_cst (adj->alias_ptr_type,
3773 base_offset
3774 + adj->offset / BITS_PER_UNIT);
3775 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3776 off);
3777 base = TREE_OPERAND (base, 0);
3779 else
3781 off = build_int_cst (adj->alias_ptr_type,
3782 base_offset
3783 + adj->offset / BITS_PER_UNIT);
3784 base = build_fold_addr_expr (base);
3788 if (!adj->by_ref)
3790 tree type = adj->type;
3791 unsigned int align;
3792 unsigned HOST_WIDE_INT misalign;
3794 if (deref_base)
3796 align = deref_align;
3797 misalign = 0;
3799 else
3801 get_pointer_alignment_1 (base, &align, &misalign);
3802 if (TYPE_ALIGN (type) > align)
3803 align = TYPE_ALIGN (type);
3805 misalign += (tree_to_double_int (off)
3806 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3807 * BITS_PER_UNIT);
3808 misalign = misalign & (align - 1);
3809 if (misalign != 0)
3810 align = (misalign & -misalign);
3811 if (align < TYPE_ALIGN (type))
3812 type = build_aligned_type (type, align);
3813 base = force_gimple_operand_gsi (&gsi, base,
3814 true, NULL, true, GSI_SAME_STMT);
3815 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3816 /* If expr is not a valid gimple call argument emit
3817 a load into a temporary. */
3818 if (is_gimple_reg_type (TREE_TYPE (expr)))
3820 gimple tem = gimple_build_assign (NULL_TREE, expr);
3821 if (gimple_in_ssa_p (cfun))
3823 gimple_set_vuse (tem, gimple_vuse (stmt));
3824 expr = make_ssa_name (TREE_TYPE (expr), tem);
3826 else
3827 expr = create_tmp_reg (TREE_TYPE (expr), NULL);
3828 gimple_assign_set_lhs (tem, expr);
3829 gsi_insert_before (&gsi, tem, GSI_SAME_STMT);
3832 else
3834 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3835 expr = build_fold_addr_expr (expr);
3836 expr = force_gimple_operand_gsi (&gsi, expr,
3837 true, NULL, true, GSI_SAME_STMT);
3839 vargs.quick_push (expr);
3841 if (adj->op != IPA_PARM_OP_COPY && MAY_HAVE_DEBUG_STMTS)
3843 unsigned int ix;
3844 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3845 gimple def_temp;
3847 arg = gimple_call_arg (stmt, adj->base_index);
3848 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3850 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3851 continue;
3852 arg = fold_convert_loc (gimple_location (stmt),
3853 TREE_TYPE (origin), arg);
3855 if (debug_args == NULL)
3856 debug_args = decl_debug_args_insert (callee_decl);
3857 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3858 if (ddecl == origin)
3860 ddecl = (**debug_args)[ix + 1];
3861 break;
3863 if (ddecl == NULL)
3865 ddecl = make_node (DEBUG_EXPR_DECL);
3866 DECL_ARTIFICIAL (ddecl) = 1;
3867 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3868 DECL_MODE (ddecl) = DECL_MODE (origin);
3870 vec_safe_push (*debug_args, origin);
3871 vec_safe_push (*debug_args, ddecl);
3873 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3874 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3878 if (dump_file && (dump_flags & TDF_DETAILS))
3880 fprintf (dump_file, "replacing stmt:");
3881 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3884 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3885 vargs.release ();
3886 if (gimple_call_lhs (stmt))
3887 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3889 gimple_set_block (new_stmt, gimple_block (stmt));
3890 if (gimple_has_location (stmt))
3891 gimple_set_location (new_stmt, gimple_location (stmt));
3892 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3893 gimple_call_copy_flags (new_stmt, stmt);
3894 if (gimple_in_ssa_p (cfun))
3896 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3897 if (gimple_vdef (stmt))
3899 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3900 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
3904 if (dump_file && (dump_flags & TDF_DETAILS))
3906 fprintf (dump_file, "with stmt:");
3907 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3908 fprintf (dump_file, "\n");
3910 gsi_replace (&gsi, new_stmt, true);
3911 if (cs)
3912 cgraph_set_call_stmt (cs, new_stmt);
3915 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
3916 gsi_prev (&gsi);
3918 while (gsi_stmt (gsi) != gsi_stmt (prev_gsi));
3921 /* If the expression *EXPR should be replaced by a reduction of a parameter, do
3922 so. ADJUSTMENTS is a pointer to a vector of adjustments. CONVERT
3923 specifies whether the function should care about type incompatibility the
3924 current and new expressions. If it is false, the function will leave
3925 incompatibility issues to the caller. Return true iff the expression
3926 was modified. */
3928 bool
3929 ipa_modify_expr (tree *expr, bool convert,
3930 ipa_parm_adjustment_vec adjustments)
3932 struct ipa_parm_adjustment *cand
3933 = ipa_get_adjustment_candidate (&expr, &convert, adjustments, false);
3934 if (!cand)
3935 return false;
3937 tree src;
3938 if (cand->by_ref)
3939 src = build_simple_mem_ref (cand->new_decl);
3940 else
3941 src = cand->new_decl;
3943 if (dump_file && (dump_flags & TDF_DETAILS))
3945 fprintf (dump_file, "About to replace expr ");
3946 print_generic_expr (dump_file, *expr, 0);
3947 fprintf (dump_file, " with ");
3948 print_generic_expr (dump_file, src, 0);
3949 fprintf (dump_file, "\n");
3952 if (convert && !useless_type_conversion_p (TREE_TYPE (*expr), cand->type))
3954 tree vce = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (*expr), src);
3955 *expr = vce;
3957 else
3958 *expr = src;
3959 return true;
3962 /* If T is an SSA_NAME, return NULL if it is not a default def or
3963 return its base variable if it is. If IGNORE_DEFAULT_DEF is true,
3964 the base variable is always returned, regardless if it is a default
3965 def. Return T if it is not an SSA_NAME. */
3967 static tree
3968 get_ssa_base_param (tree t, bool ignore_default_def)
3970 if (TREE_CODE (t) == SSA_NAME)
3972 if (ignore_default_def || SSA_NAME_IS_DEFAULT_DEF (t))
3973 return SSA_NAME_VAR (t);
3974 else
3975 return NULL_TREE;
3977 return t;
3980 /* Given an expression, return an adjustment entry specifying the
3981 transformation to be done on EXPR. If no suitable adjustment entry
3982 was found, returns NULL.
3984 If IGNORE_DEFAULT_DEF is set, consider SSA_NAMEs which are not a
3985 default def, otherwise bail on them.
3987 If CONVERT is non-NULL, this function will set *CONVERT if the
3988 expression provided is a component reference. ADJUSTMENTS is the
3989 adjustments vector. */
3991 ipa_parm_adjustment *
3992 ipa_get_adjustment_candidate (tree **expr, bool *convert,
3993 ipa_parm_adjustment_vec adjustments,
3994 bool ignore_default_def)
3996 if (TREE_CODE (**expr) == BIT_FIELD_REF
3997 || TREE_CODE (**expr) == IMAGPART_EXPR
3998 || TREE_CODE (**expr) == REALPART_EXPR)
4000 *expr = &TREE_OPERAND (**expr, 0);
4001 if (convert)
4002 *convert = true;
4005 HOST_WIDE_INT offset, size, max_size;
4006 tree base = get_ref_base_and_extent (**expr, &offset, &size, &max_size);
4007 if (!base || size == -1 || max_size == -1)
4008 return NULL;
4010 if (TREE_CODE (base) == MEM_REF)
4012 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
4013 base = TREE_OPERAND (base, 0);
4016 base = get_ssa_base_param (base, ignore_default_def);
4017 if (!base || TREE_CODE (base) != PARM_DECL)
4018 return NULL;
4020 struct ipa_parm_adjustment *cand = NULL;
4021 unsigned int len = adjustments.length ();
4022 for (unsigned i = 0; i < len; i++)
4024 struct ipa_parm_adjustment *adj = &adjustments[i];
4026 if (adj->base == base
4027 && (adj->offset == offset || adj->op == IPA_PARM_OP_REMOVE))
4029 cand = adj;
4030 break;
4034 if (!cand || cand->op == IPA_PARM_OP_COPY || cand->op == IPA_PARM_OP_REMOVE)
4035 return NULL;
4036 return cand;
4039 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
4041 static bool
4042 index_in_adjustments_multiple_times_p (int base_index,
4043 ipa_parm_adjustment_vec adjustments)
4045 int i, len = adjustments.length ();
4046 bool one = false;
4048 for (i = 0; i < len; i++)
4050 struct ipa_parm_adjustment *adj;
4051 adj = &adjustments[i];
4053 if (adj->base_index == base_index)
4055 if (one)
4056 return true;
4057 else
4058 one = true;
4061 return false;
4065 /* Return adjustments that should have the same effect on function parameters
4066 and call arguments as if they were first changed according to adjustments in
4067 INNER and then by adjustments in OUTER. */
4069 ipa_parm_adjustment_vec
4070 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
4071 ipa_parm_adjustment_vec outer)
4073 int i, outlen = outer.length ();
4074 int inlen = inner.length ();
4075 int removals = 0;
4076 ipa_parm_adjustment_vec adjustments, tmp;
4078 tmp.create (inlen);
4079 for (i = 0; i < inlen; i++)
4081 struct ipa_parm_adjustment *n;
4082 n = &inner[i];
4084 if (n->op == IPA_PARM_OP_REMOVE)
4085 removals++;
4086 else
4088 /* FIXME: Handling of new arguments are not implemented yet. */
4089 gcc_assert (n->op != IPA_PARM_OP_NEW);
4090 tmp.quick_push (*n);
4094 adjustments.create (outlen + removals);
4095 for (i = 0; i < outlen; i++)
4097 struct ipa_parm_adjustment r;
4098 struct ipa_parm_adjustment *out = &outer[i];
4099 struct ipa_parm_adjustment *in = &tmp[out->base_index];
4101 memset (&r, 0, sizeof (r));
4102 gcc_assert (in->op != IPA_PARM_OP_REMOVE);
4103 if (out->op == IPA_PARM_OP_REMOVE)
4105 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
4107 r.op = IPA_PARM_OP_REMOVE;
4108 adjustments.quick_push (r);
4110 continue;
4112 else
4114 /* FIXME: Handling of new arguments are not implemented yet. */
4115 gcc_assert (out->op != IPA_PARM_OP_NEW);
4118 r.base_index = in->base_index;
4119 r.type = out->type;
4121 /* FIXME: Create nonlocal value too. */
4123 if (in->op == IPA_PARM_OP_COPY && out->op == IPA_PARM_OP_COPY)
4124 r.op = IPA_PARM_OP_COPY;
4125 else if (in->op == IPA_PARM_OP_COPY)
4126 r.offset = out->offset;
4127 else if (out->op == IPA_PARM_OP_COPY)
4128 r.offset = in->offset;
4129 else
4130 r.offset = in->offset + out->offset;
4131 adjustments.quick_push (r);
4134 for (i = 0; i < inlen; i++)
4136 struct ipa_parm_adjustment *n = &inner[i];
4138 if (n->op == IPA_PARM_OP_REMOVE)
4139 adjustments.quick_push (*n);
4142 tmp.release ();
4143 return adjustments;
4146 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
4147 friendly way, assuming they are meant to be applied to FNDECL. */
4149 void
4150 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
4151 tree fndecl)
4153 int i, len = adjustments.length ();
4154 bool first = true;
4155 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
4157 fprintf (file, "IPA param adjustments: ");
4158 for (i = 0; i < len; i++)
4160 struct ipa_parm_adjustment *adj;
4161 adj = &adjustments[i];
4163 if (!first)
4164 fprintf (file, " ");
4165 else
4166 first = false;
4168 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
4169 print_generic_expr (file, parms[adj->base_index], 0);
4170 if (adj->base)
4172 fprintf (file, ", base: ");
4173 print_generic_expr (file, adj->base, 0);
4175 if (adj->new_decl)
4177 fprintf (file, ", new_decl: ");
4178 print_generic_expr (file, adj->new_decl, 0);
4180 if (adj->new_ssa_base)
4182 fprintf (file, ", new_ssa_base: ");
4183 print_generic_expr (file, adj->new_ssa_base, 0);
4186 if (adj->op == IPA_PARM_OP_COPY)
4187 fprintf (file, ", copy_param");
4188 else if (adj->op == IPA_PARM_OP_REMOVE)
4189 fprintf (file, ", remove_param");
4190 else
4191 fprintf (file, ", offset %li", (long) adj->offset);
4192 if (adj->by_ref)
4193 fprintf (file, ", by_ref");
4194 print_node_brief (file, ", type: ", adj->type, 0);
4195 fprintf (file, "\n");
4197 parms.release ();
4200 /* Dump the AV linked list. */
4202 void
4203 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
4205 bool comma = false;
4206 fprintf (f, " Aggregate replacements:");
4207 for (; av; av = av->next)
4209 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
4210 av->index, av->offset);
4211 print_generic_expr (f, av->value, 0);
4212 comma = true;
4214 fprintf (f, "\n");
4217 /* Stream out jump function JUMP_FUNC to OB. */
4219 static void
4220 ipa_write_jump_function (struct output_block *ob,
4221 struct ipa_jump_func *jump_func)
4223 struct ipa_agg_jf_item *item;
4224 struct bitpack_d bp;
4225 int i, count;
4227 streamer_write_uhwi (ob, jump_func->type);
4228 switch (jump_func->type)
4230 case IPA_JF_UNKNOWN:
4231 break;
4232 case IPA_JF_KNOWN_TYPE:
4233 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
4234 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
4235 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
4236 break;
4237 case IPA_JF_CONST:
4238 gcc_assert (
4239 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
4240 stream_write_tree (ob, jump_func->value.constant.value, true);
4241 break;
4242 case IPA_JF_PASS_THROUGH:
4243 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
4244 if (jump_func->value.pass_through.operation == NOP_EXPR)
4246 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4247 bp = bitpack_create (ob->main_stream);
4248 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
4249 bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1);
4250 streamer_write_bitpack (&bp);
4252 else
4254 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
4255 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
4257 break;
4258 case IPA_JF_ANCESTOR:
4259 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
4260 stream_write_tree (ob, jump_func->value.ancestor.type, true);
4261 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
4262 bp = bitpack_create (ob->main_stream);
4263 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
4264 bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1);
4265 streamer_write_bitpack (&bp);
4266 break;
4269 count = vec_safe_length (jump_func->agg.items);
4270 streamer_write_uhwi (ob, count);
4271 if (count)
4273 bp = bitpack_create (ob->main_stream);
4274 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
4275 streamer_write_bitpack (&bp);
4278 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
4280 streamer_write_uhwi (ob, item->offset);
4281 stream_write_tree (ob, item->value, true);
4285 /* Read in jump function JUMP_FUNC from IB. */
4287 static void
4288 ipa_read_jump_function (struct lto_input_block *ib,
4289 struct ipa_jump_func *jump_func,
4290 struct cgraph_edge *cs,
4291 struct data_in *data_in)
4293 enum jump_func_type jftype;
4294 enum tree_code operation;
4295 int i, count;
4297 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
4298 switch (jftype)
4300 case IPA_JF_UNKNOWN:
4301 jump_func->type = IPA_JF_UNKNOWN;
4302 break;
4303 case IPA_JF_KNOWN_TYPE:
4305 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4306 tree base_type = stream_read_tree (ib, data_in);
4307 tree component_type = stream_read_tree (ib, data_in);
4309 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
4310 break;
4312 case IPA_JF_CONST:
4313 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
4314 break;
4315 case IPA_JF_PASS_THROUGH:
4316 operation = (enum tree_code) streamer_read_uhwi (ib);
4317 if (operation == NOP_EXPR)
4319 int formal_id = streamer_read_uhwi (ib);
4320 struct bitpack_d bp = streamer_read_bitpack (ib);
4321 bool agg_preserved = bp_unpack_value (&bp, 1);
4322 bool type_preserved = bp_unpack_value (&bp, 1);
4323 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved,
4324 type_preserved);
4326 else
4328 tree operand = stream_read_tree (ib, data_in);
4329 int formal_id = streamer_read_uhwi (ib);
4330 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
4331 operation);
4333 break;
4334 case IPA_JF_ANCESTOR:
4336 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4337 tree type = stream_read_tree (ib, data_in);
4338 int formal_id = streamer_read_uhwi (ib);
4339 struct bitpack_d bp = streamer_read_bitpack (ib);
4340 bool agg_preserved = bp_unpack_value (&bp, 1);
4341 bool type_preserved = bp_unpack_value (&bp, 1);
4343 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved,
4344 type_preserved);
4345 break;
4349 count = streamer_read_uhwi (ib);
4350 vec_alloc (jump_func->agg.items, count);
4351 if (count)
4353 struct bitpack_d bp = streamer_read_bitpack (ib);
4354 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
4356 for (i = 0; i < count; i++)
4358 struct ipa_agg_jf_item item;
4359 item.offset = streamer_read_uhwi (ib);
4360 item.value = stream_read_tree (ib, data_in);
4361 jump_func->agg.items->quick_push (item);
4365 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
4366 relevant to indirect inlining to OB. */
4368 static void
4369 ipa_write_indirect_edge_info (struct output_block *ob,
4370 struct cgraph_edge *cs)
4372 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4373 struct bitpack_d bp;
4375 streamer_write_hwi (ob, ii->param_index);
4376 streamer_write_hwi (ob, ii->offset);
4377 bp = bitpack_create (ob->main_stream);
4378 bp_pack_value (&bp, ii->polymorphic, 1);
4379 bp_pack_value (&bp, ii->agg_contents, 1);
4380 bp_pack_value (&bp, ii->member_ptr, 1);
4381 bp_pack_value (&bp, ii->by_ref, 1);
4382 bp_pack_value (&bp, ii->maybe_in_construction, 1);
4383 bp_pack_value (&bp, ii->maybe_derived_type, 1);
4384 streamer_write_bitpack (&bp);
4386 if (ii->polymorphic)
4388 streamer_write_hwi (ob, ii->otr_token);
4389 stream_write_tree (ob, ii->otr_type, true);
4390 stream_write_tree (ob, ii->outer_type, true);
4394 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
4395 relevant to indirect inlining from IB. */
4397 static void
4398 ipa_read_indirect_edge_info (struct lto_input_block *ib,
4399 struct data_in *data_in ATTRIBUTE_UNUSED,
4400 struct cgraph_edge *cs)
4402 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4403 struct bitpack_d bp;
4405 ii->param_index = (int) streamer_read_hwi (ib);
4406 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
4407 bp = streamer_read_bitpack (ib);
4408 ii->polymorphic = bp_unpack_value (&bp, 1);
4409 ii->agg_contents = bp_unpack_value (&bp, 1);
4410 ii->member_ptr = bp_unpack_value (&bp, 1);
4411 ii->by_ref = bp_unpack_value (&bp, 1);
4412 ii->maybe_in_construction = bp_unpack_value (&bp, 1);
4413 ii->maybe_derived_type = bp_unpack_value (&bp, 1);
4414 if (ii->polymorphic)
4416 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
4417 ii->otr_type = stream_read_tree (ib, data_in);
4418 ii->outer_type = stream_read_tree (ib, data_in);
4422 /* Stream out NODE info to OB. */
4424 static void
4425 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
4427 int node_ref;
4428 lto_symtab_encoder_t encoder;
4429 struct ipa_node_params *info = IPA_NODE_REF (node);
4430 int j;
4431 struct cgraph_edge *e;
4432 struct bitpack_d bp;
4434 encoder = ob->decl_state->symtab_node_encoder;
4435 node_ref = lto_symtab_encoder_encode (encoder, node);
4436 streamer_write_uhwi (ob, node_ref);
4438 streamer_write_uhwi (ob, ipa_get_param_count (info));
4439 for (j = 0; j < ipa_get_param_count (info); j++)
4440 streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
4441 bp = bitpack_create (ob->main_stream);
4442 gcc_assert (info->uses_analysis_done
4443 || ipa_get_param_count (info) == 0);
4444 gcc_assert (!info->node_enqueued);
4445 gcc_assert (!info->ipcp_orig_node);
4446 for (j = 0; j < ipa_get_param_count (info); j++)
4447 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
4448 streamer_write_bitpack (&bp);
4449 for (j = 0; j < ipa_get_param_count (info); j++)
4450 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
4451 for (e = node->callees; e; e = e->next_callee)
4453 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4455 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4456 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4457 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4459 for (e = node->indirect_calls; e; e = e->next_callee)
4461 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4463 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4464 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4465 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4466 ipa_write_indirect_edge_info (ob, e);
4470 /* Stream in NODE info from IB. */
4472 static void
4473 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
4474 struct data_in *data_in)
4476 struct ipa_node_params *info = IPA_NODE_REF (node);
4477 int k;
4478 struct cgraph_edge *e;
4479 struct bitpack_d bp;
4481 ipa_alloc_node_params (node, streamer_read_uhwi (ib));
4483 for (k = 0; k < ipa_get_param_count (info); k++)
4484 info->descriptors[k].move_cost = streamer_read_uhwi (ib);
4486 bp = streamer_read_bitpack (ib);
4487 if (ipa_get_param_count (info) != 0)
4488 info->uses_analysis_done = true;
4489 info->node_enqueued = false;
4490 for (k = 0; k < ipa_get_param_count (info); k++)
4491 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
4492 for (k = 0; k < ipa_get_param_count (info); k++)
4493 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
4494 for (e = node->callees; e; e = e->next_callee)
4496 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4497 int count = streamer_read_uhwi (ib);
4499 if (!count)
4500 continue;
4501 vec_safe_grow_cleared (args->jump_functions, count);
4503 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4504 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4505 data_in);
4507 for (e = node->indirect_calls; e; e = e->next_callee)
4509 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4510 int count = streamer_read_uhwi (ib);
4512 if (count)
4514 vec_safe_grow_cleared (args->jump_functions, count);
4515 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4516 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4517 data_in);
4519 ipa_read_indirect_edge_info (ib, data_in, e);
4523 /* Write jump functions for nodes in SET. */
4525 void
4526 ipa_prop_write_jump_functions (void)
4528 struct cgraph_node *node;
4529 struct output_block *ob;
4530 unsigned int count = 0;
4531 lto_symtab_encoder_iterator lsei;
4532 lto_symtab_encoder_t encoder;
4535 if (!ipa_node_params_vector.exists ())
4536 return;
4538 ob = create_output_block (LTO_section_jump_functions);
4539 encoder = ob->decl_state->symtab_node_encoder;
4540 ob->cgraph_node = NULL;
4541 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4542 lsei_next_function_in_partition (&lsei))
4544 node = lsei_cgraph_node (lsei);
4545 if (cgraph_function_with_gimple_body_p (node)
4546 && IPA_NODE_REF (node) != NULL)
4547 count++;
4550 streamer_write_uhwi (ob, count);
4552 /* Process all of the functions. */
4553 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4554 lsei_next_function_in_partition (&lsei))
4556 node = lsei_cgraph_node (lsei);
4557 if (cgraph_function_with_gimple_body_p (node)
4558 && IPA_NODE_REF (node) != NULL)
4559 ipa_write_node_info (ob, node);
4561 streamer_write_char_stream (ob->main_stream, 0);
4562 produce_asm (ob, NULL);
4563 destroy_output_block (ob);
4566 /* Read section in file FILE_DATA of length LEN with data DATA. */
4568 static void
4569 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
4570 size_t len)
4572 const struct lto_function_header *header =
4573 (const struct lto_function_header *) data;
4574 const int cfg_offset = sizeof (struct lto_function_header);
4575 const int main_offset = cfg_offset + header->cfg_size;
4576 const int string_offset = main_offset + header->main_size;
4577 struct data_in *data_in;
4578 struct lto_input_block ib_main;
4579 unsigned int i;
4580 unsigned int count;
4582 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4583 header->main_size);
4585 data_in =
4586 lto_data_in_create (file_data, (const char *) data + string_offset,
4587 header->string_size, vNULL);
4588 count = streamer_read_uhwi (&ib_main);
4590 for (i = 0; i < count; i++)
4592 unsigned int index;
4593 struct cgraph_node *node;
4594 lto_symtab_encoder_t encoder;
4596 index = streamer_read_uhwi (&ib_main);
4597 encoder = file_data->symtab_node_encoder;
4598 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4599 gcc_assert (node->definition);
4600 ipa_read_node_info (&ib_main, node, data_in);
4602 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4603 len);
4604 lto_data_in_delete (data_in);
4607 /* Read ipcp jump functions. */
4609 void
4610 ipa_prop_read_jump_functions (void)
4612 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4613 struct lto_file_decl_data *file_data;
4614 unsigned int j = 0;
4616 ipa_check_create_node_params ();
4617 ipa_check_create_edge_args ();
4618 ipa_register_cgraph_hooks ();
4620 while ((file_data = file_data_vec[j++]))
4622 size_t len;
4623 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4625 if (data)
4626 ipa_prop_read_section (file_data, data, len);
4630 /* After merging units, we can get mismatch in argument counts.
4631 Also decl merging might've rendered parameter lists obsolete.
4632 Also compute called_with_variable_arg info. */
4634 void
4635 ipa_update_after_lto_read (void)
4637 ipa_check_create_node_params ();
4638 ipa_check_create_edge_args ();
4641 void
4642 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4644 int node_ref;
4645 unsigned int count = 0;
4646 lto_symtab_encoder_t encoder;
4647 struct ipa_agg_replacement_value *aggvals, *av;
4649 aggvals = ipa_get_agg_replacements_for_node (node);
4650 encoder = ob->decl_state->symtab_node_encoder;
4651 node_ref = lto_symtab_encoder_encode (encoder, node);
4652 streamer_write_uhwi (ob, node_ref);
4654 for (av = aggvals; av; av = av->next)
4655 count++;
4656 streamer_write_uhwi (ob, count);
4658 for (av = aggvals; av; av = av->next)
4660 struct bitpack_d bp;
4662 streamer_write_uhwi (ob, av->offset);
4663 streamer_write_uhwi (ob, av->index);
4664 stream_write_tree (ob, av->value, true);
4666 bp = bitpack_create (ob->main_stream);
4667 bp_pack_value (&bp, av->by_ref, 1);
4668 streamer_write_bitpack (&bp);
4672 /* Stream in the aggregate value replacement chain for NODE from IB. */
4674 static void
4675 read_agg_replacement_chain (struct lto_input_block *ib,
4676 struct cgraph_node *node,
4677 struct data_in *data_in)
4679 struct ipa_agg_replacement_value *aggvals = NULL;
4680 unsigned int count, i;
4682 count = streamer_read_uhwi (ib);
4683 for (i = 0; i <count; i++)
4685 struct ipa_agg_replacement_value *av;
4686 struct bitpack_d bp;
4688 av = ggc_alloc_ipa_agg_replacement_value ();
4689 av->offset = streamer_read_uhwi (ib);
4690 av->index = streamer_read_uhwi (ib);
4691 av->value = stream_read_tree (ib, data_in);
4692 bp = streamer_read_bitpack (ib);
4693 av->by_ref = bp_unpack_value (&bp, 1);
4694 av->next = aggvals;
4695 aggvals = av;
4697 ipa_set_node_agg_value_chain (node, aggvals);
4700 /* Write all aggregate replacement for nodes in set. */
4702 void
4703 ipa_prop_write_all_agg_replacement (void)
4705 struct cgraph_node *node;
4706 struct output_block *ob;
4707 unsigned int count = 0;
4708 lto_symtab_encoder_iterator lsei;
4709 lto_symtab_encoder_t encoder;
4711 if (!ipa_node_agg_replacements)
4712 return;
4714 ob = create_output_block (LTO_section_ipcp_transform);
4715 encoder = ob->decl_state->symtab_node_encoder;
4716 ob->cgraph_node = NULL;
4717 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4718 lsei_next_function_in_partition (&lsei))
4720 node = lsei_cgraph_node (lsei);
4721 if (cgraph_function_with_gimple_body_p (node)
4722 && ipa_get_agg_replacements_for_node (node) != NULL)
4723 count++;
4726 streamer_write_uhwi (ob, count);
4728 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4729 lsei_next_function_in_partition (&lsei))
4731 node = lsei_cgraph_node (lsei);
4732 if (cgraph_function_with_gimple_body_p (node)
4733 && ipa_get_agg_replacements_for_node (node) != NULL)
4734 write_agg_replacement_chain (ob, node);
4736 streamer_write_char_stream (ob->main_stream, 0);
4737 produce_asm (ob, NULL);
4738 destroy_output_block (ob);
4741 /* Read replacements section in file FILE_DATA of length LEN with data
4742 DATA. */
4744 static void
4745 read_replacements_section (struct lto_file_decl_data *file_data,
4746 const char *data,
4747 size_t len)
4749 const struct lto_function_header *header =
4750 (const struct lto_function_header *) data;
4751 const int cfg_offset = sizeof (struct lto_function_header);
4752 const int main_offset = cfg_offset + header->cfg_size;
4753 const int string_offset = main_offset + header->main_size;
4754 struct data_in *data_in;
4755 struct lto_input_block ib_main;
4756 unsigned int i;
4757 unsigned int count;
4759 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4760 header->main_size);
4762 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4763 header->string_size, vNULL);
4764 count = streamer_read_uhwi (&ib_main);
4766 for (i = 0; i < count; i++)
4768 unsigned int index;
4769 struct cgraph_node *node;
4770 lto_symtab_encoder_t encoder;
4772 index = streamer_read_uhwi (&ib_main);
4773 encoder = file_data->symtab_node_encoder;
4774 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4775 gcc_assert (node->definition);
4776 read_agg_replacement_chain (&ib_main, node, data_in);
4778 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4779 len);
4780 lto_data_in_delete (data_in);
4783 /* Read IPA-CP aggregate replacements. */
4785 void
4786 ipa_prop_read_all_agg_replacement (void)
4788 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4789 struct lto_file_decl_data *file_data;
4790 unsigned int j = 0;
4792 while ((file_data = file_data_vec[j++]))
4794 size_t len;
4795 const char *data = lto_get_section_data (file_data,
4796 LTO_section_ipcp_transform,
4797 NULL, &len);
4798 if (data)
4799 read_replacements_section (file_data, data, len);
4803 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4804 NODE. */
4806 static void
4807 adjust_agg_replacement_values (struct cgraph_node *node,
4808 struct ipa_agg_replacement_value *aggval)
4810 struct ipa_agg_replacement_value *v;
4811 int i, c = 0, d = 0, *adj;
4813 if (!node->clone.combined_args_to_skip)
4814 return;
4816 for (v = aggval; v; v = v->next)
4818 gcc_assert (v->index >= 0);
4819 if (c < v->index)
4820 c = v->index;
4822 c++;
4824 adj = XALLOCAVEC (int, c);
4825 for (i = 0; i < c; i++)
4826 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4828 adj[i] = -1;
4829 d++;
4831 else
4832 adj[i] = i - d;
4834 for (v = aggval; v; v = v->next)
4835 v->index = adj[v->index];
4839 /* Function body transformation phase. */
4841 unsigned int
4842 ipcp_transform_function (struct cgraph_node *node)
4844 vec<ipa_param_descriptor> descriptors = vNULL;
4845 struct param_analysis_info *parms_ainfo;
4846 struct ipa_agg_replacement_value *aggval;
4847 gimple_stmt_iterator gsi;
4848 basic_block bb;
4849 int param_count;
4850 bool cfg_changed = false, something_changed = false;
4852 gcc_checking_assert (cfun);
4853 gcc_checking_assert (current_function_decl);
4855 if (dump_file)
4856 fprintf (dump_file, "Modification phase of node %s/%i\n",
4857 node->name (), node->order);
4859 aggval = ipa_get_agg_replacements_for_node (node);
4860 if (!aggval)
4861 return 0;
4862 param_count = count_formal_params (node->decl);
4863 if (param_count == 0)
4864 return 0;
4865 adjust_agg_replacement_values (node, aggval);
4866 if (dump_file)
4867 ipa_dump_agg_replacement_values (dump_file, aggval);
4868 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
4869 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
4870 descriptors.safe_grow_cleared (param_count);
4871 ipa_populate_param_decls (node, descriptors);
4873 FOR_EACH_BB_FN (bb, cfun)
4874 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4876 struct ipa_agg_replacement_value *v;
4877 gimple stmt = gsi_stmt (gsi);
4878 tree rhs, val, t;
4879 HOST_WIDE_INT offset, size;
4880 int index;
4881 bool by_ref, vce;
4883 if (!gimple_assign_load_p (stmt))
4884 continue;
4885 rhs = gimple_assign_rhs1 (stmt);
4886 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
4887 continue;
4889 vce = false;
4890 t = rhs;
4891 while (handled_component_p (t))
4893 /* V_C_E can do things like convert an array of integers to one
4894 bigger integer and similar things we do not handle below. */
4895 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
4897 vce = true;
4898 break;
4900 t = TREE_OPERAND (t, 0);
4902 if (vce)
4903 continue;
4905 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
4906 rhs, &index, &offset, &size, &by_ref))
4907 continue;
4908 for (v = aggval; v; v = v->next)
4909 if (v->index == index
4910 && v->offset == offset)
4911 break;
4912 if (!v
4913 || v->by_ref != by_ref
4914 || tree_to_shwi (TYPE_SIZE (TREE_TYPE (v->value))) != size)
4915 continue;
4917 gcc_checking_assert (is_gimple_ip_invariant (v->value));
4918 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
4920 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
4921 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
4922 else if (TYPE_SIZE (TREE_TYPE (rhs))
4923 == TYPE_SIZE (TREE_TYPE (v->value)))
4924 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
4925 else
4927 if (dump_file)
4929 fprintf (dump_file, " const ");
4930 print_generic_expr (dump_file, v->value, 0);
4931 fprintf (dump_file, " can't be converted to type of ");
4932 print_generic_expr (dump_file, rhs, 0);
4933 fprintf (dump_file, "\n");
4935 continue;
4938 else
4939 val = v->value;
4941 if (dump_file && (dump_flags & TDF_DETAILS))
4943 fprintf (dump_file, "Modifying stmt:\n ");
4944 print_gimple_stmt (dump_file, stmt, 0, 0);
4946 gimple_assign_set_rhs_from_tree (&gsi, val);
4947 update_stmt (stmt);
4949 if (dump_file && (dump_flags & TDF_DETAILS))
4951 fprintf (dump_file, "into:\n ");
4952 print_gimple_stmt (dump_file, stmt, 0, 0);
4953 fprintf (dump_file, "\n");
4956 something_changed = true;
4957 if (maybe_clean_eh_stmt (stmt)
4958 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4959 cfg_changed = true;
4962 (*ipa_node_agg_replacements)[node->uid] = NULL;
4963 free_parms_ainfo (parms_ainfo, param_count);
4964 descriptors.release ();
4966 if (!something_changed)
4967 return 0;
4968 else if (cfg_changed)
4969 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
4970 else
4971 return TODO_update_ssa_only_virtuals;