PR target/58838
[official-gcc.git] / gcc / ipa-prop.c
blob9a4b528169afbcae8c4fd2ace14599f5339d4a48
1 /* Interprocedural analyses.
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "langhooks.h"
25 #include "ggc.h"
26 #include "target.h"
27 #include "ipa-prop.h"
28 #include "bitmap.h"
29 #include "gimple-ssa.h"
30 #include "tree-cfg.h"
31 #include "tree-phinodes.h"
32 #include "ssa-iterators.h"
33 #include "tree-into-ssa.h"
34 #include "tree-dfa.h"
35 #include "tree-pass.h"
36 #include "tree-inline.h"
37 #include "ipa-inline.h"
38 #include "flags.h"
39 #include "diagnostic.h"
40 #include "gimple-pretty-print.h"
41 #include "lto-streamer.h"
42 #include "data-streamer.h"
43 #include "tree-streamer.h"
44 #include "params.h"
45 #include "ipa-utils.h"
47 /* Intermediate information about a parameter that is only useful during the
48 run of ipa_analyze_node and is not kept afterwards. */
50 struct param_analysis_info
52 bool parm_modified, ref_modified, pt_modified;
53 bitmap parm_visited_statements, pt_visited_statements;
56 /* Vector where the parameter infos are actually stored. */
57 vec<ipa_node_params_t> ipa_node_params_vector;
58 /* Vector of known aggregate values in cloned nodes. */
59 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
60 /* Vector where the parameter infos are actually stored. */
61 vec<ipa_edge_args_t, va_gc> *ipa_edge_args_vector;
63 /* Holders of ipa cgraph hooks: */
64 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
65 static struct cgraph_node_hook_list *node_removal_hook_holder;
66 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
67 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
68 static struct cgraph_node_hook_list *function_insertion_hook_holder;
70 /* Description of a reference to an IPA constant. */
71 struct ipa_cst_ref_desc
73 /* Edge that corresponds to the statement which took the reference. */
74 struct cgraph_edge *cs;
75 /* Linked list of duplicates created when call graph edges are cloned. */
76 struct ipa_cst_ref_desc *next_duplicate;
77 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
78 if out of control. */
79 int refcount;
82 /* Allocation pool for reference descriptions. */
84 static alloc_pool ipa_refdesc_pool;
86 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
87 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
89 static bool
90 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
92 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->symbol.decl);
93 struct cl_optimization *os;
95 if (!fs_opts)
96 return false;
97 os = TREE_OPTIMIZATION (fs_opts);
98 return !os->x_optimize || !os->x_flag_ipa_cp;
101 /* Return index of the formal whose tree is PTREE in function which corresponds
102 to INFO. */
104 static int
105 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor_t> descriptors, tree ptree)
107 int i, count;
109 count = descriptors.length ();
110 for (i = 0; i < count; i++)
111 if (descriptors[i].decl == ptree)
112 return i;
114 return -1;
117 /* Return index of the formal whose tree is PTREE in function which corresponds
118 to INFO. */
121 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
123 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
126 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
127 NODE. */
129 static void
130 ipa_populate_param_decls (struct cgraph_node *node,
131 vec<ipa_param_descriptor_t> &descriptors)
133 tree fndecl;
134 tree fnargs;
135 tree parm;
136 int param_num;
138 fndecl = node->symbol.decl;
139 gcc_assert (gimple_has_body_p (fndecl));
140 fnargs = DECL_ARGUMENTS (fndecl);
141 param_num = 0;
142 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
144 descriptors[param_num].decl = parm;
145 descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm));
146 param_num++;
150 /* Return how many formal parameters FNDECL has. */
152 static inline int
153 count_formal_params (tree fndecl)
155 tree parm;
156 int count = 0;
157 gcc_assert (gimple_has_body_p (fndecl));
159 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
160 count++;
162 return count;
165 /* Return the declaration of Ith formal parameter of the function corresponding
166 to INFO. Note there is no setter function as this array is built just once
167 using ipa_initialize_node_params. */
169 void
170 ipa_dump_param (FILE *file, struct ipa_node_params *info, int i)
172 fprintf (file, "param #%i", i);
173 if (info->descriptors[i].decl)
175 fprintf (file, " ");
176 print_generic_expr (file, info->descriptors[i].decl, 0);
180 /* Initialize the ipa_node_params structure associated with NODE
181 to hold PARAM_COUNT parameters. */
183 void
184 ipa_alloc_node_params (struct cgraph_node *node, int param_count)
186 struct ipa_node_params *info = IPA_NODE_REF (node);
188 if (!info->descriptors.exists () && param_count)
189 info->descriptors.safe_grow_cleared (param_count);
192 /* Initialize the ipa_node_params structure associated with NODE by counting
193 the function parameters, creating the descriptors and populating their
194 param_decls. */
196 void
197 ipa_initialize_node_params (struct cgraph_node *node)
199 struct ipa_node_params *info = IPA_NODE_REF (node);
201 if (!info->descriptors.exists ())
203 ipa_alloc_node_params (node, count_formal_params (node->symbol.decl));
204 ipa_populate_param_decls (node, info->descriptors);
208 /* Print the jump functions associated with call graph edge CS to file F. */
210 static void
211 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
213 int i, count;
215 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
216 for (i = 0; i < count; i++)
218 struct ipa_jump_func *jump_func;
219 enum jump_func_type type;
221 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
222 type = jump_func->type;
224 fprintf (f, " param %d: ", i);
225 if (type == IPA_JF_UNKNOWN)
226 fprintf (f, "UNKNOWN\n");
227 else if (type == IPA_JF_KNOWN_TYPE)
229 fprintf (f, "KNOWN TYPE: base ");
230 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
231 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
232 jump_func->value.known_type.offset);
233 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
234 fprintf (f, "\n");
236 else if (type == IPA_JF_CONST)
238 tree val = jump_func->value.constant.value;
239 fprintf (f, "CONST: ");
240 print_generic_expr (f, val, 0);
241 if (TREE_CODE (val) == ADDR_EXPR
242 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
244 fprintf (f, " -> ");
245 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
248 fprintf (f, "\n");
250 else if (type == IPA_JF_PASS_THROUGH)
252 fprintf (f, "PASS THROUGH: ");
253 fprintf (f, "%d, op %s",
254 jump_func->value.pass_through.formal_id,
255 get_tree_code_name(jump_func->value.pass_through.operation));
256 if (jump_func->value.pass_through.operation != NOP_EXPR)
258 fprintf (f, " ");
259 print_generic_expr (f,
260 jump_func->value.pass_through.operand, 0);
262 if (jump_func->value.pass_through.agg_preserved)
263 fprintf (f, ", agg_preserved");
264 if (jump_func->value.pass_through.type_preserved)
265 fprintf (f, ", type_preserved");
266 fprintf (f, "\n");
268 else if (type == IPA_JF_ANCESTOR)
270 fprintf (f, "ANCESTOR: ");
271 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
272 jump_func->value.ancestor.formal_id,
273 jump_func->value.ancestor.offset);
274 print_generic_expr (f, jump_func->value.ancestor.type, 0);
275 if (jump_func->value.ancestor.agg_preserved)
276 fprintf (f, ", agg_preserved");
277 if (jump_func->value.ancestor.type_preserved)
278 fprintf (f, ", type_preserved");
279 fprintf (f, "\n");
282 if (jump_func->agg.items)
284 struct ipa_agg_jf_item *item;
285 int j;
287 fprintf (f, " Aggregate passed by %s:\n",
288 jump_func->agg.by_ref ? "reference" : "value");
289 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
291 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
292 item->offset);
293 if (TYPE_P (item->value))
294 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
295 tree_low_cst (TYPE_SIZE (item->value), 1));
296 else
298 fprintf (f, "cst: ");
299 print_generic_expr (f, item->value, 0);
301 fprintf (f, "\n");
308 /* Print the jump functions of all arguments on all call graph edges going from
309 NODE to file F. */
311 void
312 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
314 struct cgraph_edge *cs;
316 fprintf (f, " Jump functions of caller %s/%i:\n", cgraph_node_name (node),
317 node->symbol.order);
318 for (cs = node->callees; cs; cs = cs->next_callee)
320 if (!ipa_edge_args_info_available_for_edge_p (cs))
321 continue;
323 fprintf (f, " callsite %s/%i -> %s/%i : \n",
324 xstrdup (cgraph_node_name (node)), node->symbol.order,
325 xstrdup (cgraph_node_name (cs->callee)),
326 cs->callee->symbol.order);
327 ipa_print_node_jump_functions_for_edge (f, cs);
330 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
332 struct cgraph_indirect_call_info *ii;
333 if (!ipa_edge_args_info_available_for_edge_p (cs))
334 continue;
336 ii = cs->indirect_info;
337 if (ii->agg_contents)
338 fprintf (f, " indirect %s callsite, calling param %i, "
339 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
340 ii->member_ptr ? "member ptr" : "aggregate",
341 ii->param_index, ii->offset,
342 ii->by_ref ? "by reference" : "by_value");
343 else
344 fprintf (f, " indirect %s callsite, calling param %i",
345 ii->polymorphic ? "polymorphic" : "simple", ii->param_index);
347 if (cs->call_stmt)
349 fprintf (f, ", for stmt ");
350 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
352 else
353 fprintf (f, "\n");
354 ipa_print_node_jump_functions_for_edge (f, cs);
358 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
360 void
361 ipa_print_all_jump_functions (FILE *f)
363 struct cgraph_node *node;
365 fprintf (f, "\nJump functions:\n");
366 FOR_EACH_FUNCTION (node)
368 ipa_print_node_jump_functions (f, node);
372 /* Set JFUNC to be a known type jump function. */
374 static void
375 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
376 tree base_type, tree component_type)
378 gcc_assert (TREE_CODE (component_type) == RECORD_TYPE
379 && TYPE_BINFO (component_type));
380 jfunc->type = IPA_JF_KNOWN_TYPE;
381 jfunc->value.known_type.offset = offset,
382 jfunc->value.known_type.base_type = base_type;
383 jfunc->value.known_type.component_type = component_type;
386 /* Set JFUNC to be a copy of another jmp (to be used by jump function
387 combination code). The two functions will share their rdesc. */
389 static void
390 ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
391 struct ipa_jump_func *src)
394 gcc_checking_assert (src->type == IPA_JF_CONST);
395 dst->type = IPA_JF_CONST;
396 dst->value.constant = src->value.constant;
399 /* Set JFUNC to be a constant jmp function. */
401 static void
402 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
403 struct cgraph_edge *cs)
405 constant = unshare_expr (constant);
406 if (constant && EXPR_P (constant))
407 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
408 jfunc->type = IPA_JF_CONST;
409 jfunc->value.constant.value = unshare_expr_without_location (constant);
411 if (TREE_CODE (constant) == ADDR_EXPR
412 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
414 struct ipa_cst_ref_desc *rdesc;
415 if (!ipa_refdesc_pool)
416 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
417 sizeof (struct ipa_cst_ref_desc), 32);
419 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
420 rdesc->cs = cs;
421 rdesc->next_duplicate = NULL;
422 rdesc->refcount = 1;
423 jfunc->value.constant.rdesc = rdesc;
425 else
426 jfunc->value.constant.rdesc = NULL;
429 /* Set JFUNC to be a simple pass-through jump function. */
430 static void
431 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
432 bool agg_preserved, bool type_preserved)
434 jfunc->type = IPA_JF_PASS_THROUGH;
435 jfunc->value.pass_through.operand = NULL_TREE;
436 jfunc->value.pass_through.formal_id = formal_id;
437 jfunc->value.pass_through.operation = NOP_EXPR;
438 jfunc->value.pass_through.agg_preserved = agg_preserved;
439 jfunc->value.pass_through.type_preserved = type_preserved;
442 /* Set JFUNC to be an arithmetic pass through jump function. */
444 static void
445 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
446 tree operand, enum tree_code operation)
448 jfunc->type = IPA_JF_PASS_THROUGH;
449 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
450 jfunc->value.pass_through.formal_id = formal_id;
451 jfunc->value.pass_through.operation = operation;
452 jfunc->value.pass_through.agg_preserved = false;
453 jfunc->value.pass_through.type_preserved = false;
456 /* Set JFUNC to be an ancestor jump function. */
458 static void
459 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
460 tree type, int formal_id, bool agg_preserved,
461 bool type_preserved)
463 jfunc->type = IPA_JF_ANCESTOR;
464 jfunc->value.ancestor.formal_id = formal_id;
465 jfunc->value.ancestor.offset = offset;
466 jfunc->value.ancestor.type = type;
467 jfunc->value.ancestor.agg_preserved = agg_preserved;
468 jfunc->value.ancestor.type_preserved = type_preserved;
471 /* Extract the acual BINFO being described by JFUNC which must be a known type
472 jump function. */
474 tree
475 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
477 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
478 if (!base_binfo)
479 return NULL_TREE;
480 return get_binfo_at_offset (base_binfo,
481 jfunc->value.known_type.offset,
482 jfunc->value.known_type.component_type);
485 /* Structure to be passed in between detect_type_change and
486 check_stmt_for_type_change. */
488 struct type_change_info
490 /* Offset into the object where there is the virtual method pointer we are
491 looking for. */
492 HOST_WIDE_INT offset;
493 /* The declaration or SSA_NAME pointer of the base that we are checking for
494 type change. */
495 tree object;
496 /* If we actually can tell the type that the object has changed to, it is
497 stored in this field. Otherwise it remains NULL_TREE. */
498 tree known_current_type;
499 /* Set to true if dynamic type change has been detected. */
500 bool type_maybe_changed;
501 /* Set to true if multiple types have been encountered. known_current_type
502 must be disregarded in that case. */
503 bool multiple_types_encountered;
506 /* Return true if STMT can modify a virtual method table pointer.
508 This function makes special assumptions about both constructors and
509 destructors which are all the functions that are allowed to alter the VMT
510 pointers. It assumes that destructors begin with assignment into all VMT
511 pointers and that constructors essentially look in the following way:
513 1) The very first thing they do is that they call constructors of ancestor
514 sub-objects that have them.
516 2) Then VMT pointers of this and all its ancestors is set to new values
517 corresponding to the type corresponding to the constructor.
519 3) Only afterwards, other stuff such as constructor of member sub-objects
520 and the code written by the user is run. Only this may include calling
521 virtual functions, directly or indirectly.
523 There is no way to call a constructor of an ancestor sub-object in any
524 other way.
526 This means that we do not have to care whether constructors get the correct
527 type information because they will always change it (in fact, if we define
528 the type to be given by the VMT pointer, it is undefined).
530 The most important fact to derive from the above is that if, for some
531 statement in the section 3, we try to detect whether the dynamic type has
532 changed, we can safely ignore all calls as we examine the function body
533 backwards until we reach statements in section 2 because these calls cannot
534 be ancestor constructors or destructors (if the input is not bogus) and so
535 do not change the dynamic type (this holds true only for automatically
536 allocated objects but at the moment we devirtualize only these). We then
537 must detect that statements in section 2 change the dynamic type and can try
538 to derive the new type. That is enough and we can stop, we will never see
539 the calls into constructors of sub-objects in this code. Therefore we can
540 safely ignore all call statements that we traverse.
543 static bool
544 stmt_may_be_vtbl_ptr_store (gimple stmt)
546 if (is_gimple_call (stmt))
547 return false;
548 else if (is_gimple_assign (stmt))
550 tree lhs = gimple_assign_lhs (stmt);
552 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
554 if (flag_strict_aliasing
555 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
556 return false;
558 if (TREE_CODE (lhs) == COMPONENT_REF
559 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
560 return false;
561 /* In the future we might want to use get_base_ref_and_offset to find
562 if there is a field corresponding to the offset and if so, proceed
563 almost like if it was a component ref. */
566 return true;
569 /* If STMT can be proved to be an assignment to the virtual method table
570 pointer of ANALYZED_OBJ and the type associated with the new table
571 identified, return the type. Otherwise return NULL_TREE. */
573 static tree
574 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
576 HOST_WIDE_INT offset, size, max_size;
577 tree lhs, rhs, base;
579 if (!gimple_assign_single_p (stmt))
580 return NULL_TREE;
582 lhs = gimple_assign_lhs (stmt);
583 rhs = gimple_assign_rhs1 (stmt);
584 if (TREE_CODE (lhs) != COMPONENT_REF
585 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))
586 || TREE_CODE (rhs) != ADDR_EXPR)
587 return NULL_TREE;
588 rhs = get_base_address (TREE_OPERAND (rhs, 0));
589 if (!rhs
590 || TREE_CODE (rhs) != VAR_DECL
591 || !DECL_VIRTUAL_P (rhs))
592 return NULL_TREE;
594 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
595 if (offset != tci->offset
596 || size != POINTER_SIZE
597 || max_size != POINTER_SIZE)
598 return NULL_TREE;
599 if (TREE_CODE (base) == MEM_REF)
601 if (TREE_CODE (tci->object) != MEM_REF
602 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
603 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
604 TREE_OPERAND (base, 1)))
605 return NULL_TREE;
607 else if (tci->object != base)
608 return NULL_TREE;
610 return DECL_CONTEXT (rhs);
613 /* Callback of walk_aliased_vdefs and a helper function for
614 detect_type_change to check whether a particular statement may modify
615 the virtual table pointer, and if possible also determine the new type of
616 the (sub-)object. It stores its result into DATA, which points to a
617 type_change_info structure. */
619 static bool
620 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
622 gimple stmt = SSA_NAME_DEF_STMT (vdef);
623 struct type_change_info *tci = (struct type_change_info *) data;
625 if (stmt_may_be_vtbl_ptr_store (stmt))
627 tree type;
628 type = extr_type_from_vtbl_ptr_store (stmt, tci);
629 if (tci->type_maybe_changed
630 && type != tci->known_current_type)
631 tci->multiple_types_encountered = true;
632 tci->known_current_type = type;
633 tci->type_maybe_changed = true;
634 return true;
636 else
637 return false;
642 /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
643 callsite CALL) by looking for assignments to its virtual table pointer. If
644 it is, return true and fill in the jump function JFUNC with relevant type
645 information or set it to unknown. ARG is the object itself (not a pointer
646 to it, unless dereferenced). BASE is the base of the memory access as
647 returned by get_ref_base_and_extent, as is the offset. */
649 static bool
650 detect_type_change (tree arg, tree base, tree comp_type, gimple call,
651 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
653 struct type_change_info tci;
654 ao_ref ao;
656 gcc_checking_assert (DECL_P (arg)
657 || TREE_CODE (arg) == MEM_REF
658 || handled_component_p (arg));
659 /* Const calls cannot call virtual methods through VMT and so type changes do
660 not matter. */
661 if (!flag_devirtualize || !gimple_vuse (call)
662 /* Be sure expected_type is polymorphic. */
663 || !comp_type
664 || TREE_CODE (comp_type) != RECORD_TYPE
665 || !TYPE_BINFO (comp_type)
666 || !BINFO_VTABLE (TYPE_BINFO (comp_type)))
667 return false;
669 ao_ref_init (&ao, arg);
670 ao.base = base;
671 ao.offset = offset;
672 ao.size = POINTER_SIZE;
673 ao.max_size = ao.size;
675 tci.offset = offset;
676 tci.object = get_base_address (arg);
677 tci.known_current_type = NULL_TREE;
678 tci.type_maybe_changed = false;
679 tci.multiple_types_encountered = false;
681 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
682 &tci, NULL);
683 if (!tci.type_maybe_changed)
684 return false;
686 if (!tci.known_current_type
687 || tci.multiple_types_encountered
688 || offset != 0)
689 jfunc->type = IPA_JF_UNKNOWN;
690 else
691 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
693 return true;
696 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
697 SSA name (its dereference will become the base and the offset is assumed to
698 be zero). */
700 static bool
701 detect_type_change_ssa (tree arg, tree comp_type,
702 gimple call, struct ipa_jump_func *jfunc)
704 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
705 if (!flag_devirtualize
706 || !POINTER_TYPE_P (TREE_TYPE (arg)))
707 return false;
709 arg = build2 (MEM_REF, ptr_type_node, arg,
710 build_int_cst (ptr_type_node, 0));
712 return detect_type_change (arg, arg, comp_type, call, jfunc, 0);
715 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
716 boolean variable pointed to by DATA. */
718 static bool
719 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
720 void *data)
722 bool *b = (bool *) data;
723 *b = true;
724 return true;
727 /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
728 a value known not to be modified in this function before reaching the
729 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
730 information about the parameter. */
732 static bool
733 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
734 gimple stmt, tree parm_load)
736 bool modified = false;
737 bitmap *visited_stmts;
738 ao_ref refd;
740 if (parm_ainfo && parm_ainfo->parm_modified)
741 return false;
743 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
744 ao_ref_init (&refd, parm_load);
745 /* We can cache visited statements only when parm_ainfo is available and when
746 we are looking at a naked load of the whole parameter. */
747 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
748 visited_stmts = NULL;
749 else
750 visited_stmts = &parm_ainfo->parm_visited_statements;
751 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
752 visited_stmts);
753 if (parm_ainfo && modified)
754 parm_ainfo->parm_modified = true;
755 return !modified;
758 /* If STMT is an assignment that loads a value from an parameter declaration,
759 return the index of the parameter in ipa_node_params which has not been
760 modified. Otherwise return -1. */
762 static int
763 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
764 struct param_analysis_info *parms_ainfo,
765 gimple stmt)
767 int index;
768 tree op1;
770 if (!gimple_assign_single_p (stmt))
771 return -1;
773 op1 = gimple_assign_rhs1 (stmt);
774 if (TREE_CODE (op1) != PARM_DECL)
775 return -1;
777 index = ipa_get_param_decl_index_1 (descriptors, op1);
778 if (index < 0
779 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
780 : NULL, stmt, op1))
781 return -1;
783 return index;
786 /* Return true if memory reference REF loads data that are known to be
787 unmodified in this function before reaching statement STMT. PARM_AINFO, if
788 non-NULL, is a pointer to a structure containing temporary information about
789 PARM. */
791 static bool
792 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
793 gimple stmt, tree ref)
795 bool modified = false;
796 ao_ref refd;
798 gcc_checking_assert (gimple_vuse (stmt));
799 if (parm_ainfo && parm_ainfo->ref_modified)
800 return false;
802 ao_ref_init (&refd, ref);
803 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
804 NULL);
805 if (parm_ainfo && modified)
806 parm_ainfo->ref_modified = true;
807 return !modified;
810 /* Return true if the data pointed to by PARM is known to be unmodified in this
811 function before reaching call statement CALL into which it is passed.
812 PARM_AINFO is a pointer to a structure containing temporary information
813 about PARM. */
815 static bool
816 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
817 gimple call, tree parm)
819 bool modified = false;
820 ao_ref refd;
822 /* It's unnecessary to calculate anything about memory contnets for a const
823 function because it is not goin to use it. But do not cache the result
824 either. Also, no such calculations for non-pointers. */
825 if (!gimple_vuse (call)
826 || !POINTER_TYPE_P (TREE_TYPE (parm)))
827 return false;
829 if (parm_ainfo->pt_modified)
830 return false;
832 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
833 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
834 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
835 if (modified)
836 parm_ainfo->pt_modified = true;
837 return !modified;
840 /* Return true if we can prove that OP is a memory reference loading unmodified
841 data from an aggregate passed as a parameter and if the aggregate is passed
842 by reference, that the alias type of the load corresponds to the type of the
843 formal parameter (so that we can rely on this type for TBAA in callers).
844 INFO and PARMS_AINFO describe parameters of the current function (but the
845 latter can be NULL), STMT is the load statement. If function returns true,
846 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
847 within the aggregate and whether it is a load from a value passed by
848 reference respectively. */
850 static bool
851 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
852 struct param_analysis_info *parms_ainfo, gimple stmt,
853 tree op, int *index_p, HOST_WIDE_INT *offset_p,
854 bool *by_ref_p)
856 int index;
857 HOST_WIDE_INT size, max_size;
858 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
860 if (max_size == -1 || max_size != size || *offset_p < 0)
861 return false;
863 if (DECL_P (base))
865 int index = ipa_get_param_decl_index_1 (descriptors, base);
866 if (index >= 0
867 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
868 : NULL, stmt, op))
870 *index_p = index;
871 *by_ref_p = false;
872 return true;
874 return false;
877 if (TREE_CODE (base) != MEM_REF
878 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
879 || !integer_zerop (TREE_OPERAND (base, 1)))
880 return false;
882 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
884 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
885 index = ipa_get_param_decl_index_1 (descriptors, parm);
887 else
889 /* This branch catches situations where a pointer parameter is not a
890 gimple register, for example:
892 void hip7(S*) (struct S * p)
894 void (*<T2e4>) (struct S *) D.1867;
895 struct S * p.1;
897 <bb 2>:
898 p.1_1 = p;
899 D.1867_2 = p.1_1->f;
900 D.1867_2 ();
901 gdp = &p;
904 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
905 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
908 if (index >= 0
909 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
910 stmt, op))
912 *index_p = index;
913 *by_ref_p = true;
914 return true;
916 return false;
919 /* Just like the previous function, just without the param_analysis_info
920 pointer, for users outside of this file. */
922 bool
923 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
924 tree op, int *index_p, HOST_WIDE_INT *offset_p,
925 bool *by_ref_p)
927 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
928 offset_p, by_ref_p);
931 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
932 of an assignment statement STMT, try to determine whether we are actually
933 handling any of the following cases and construct an appropriate jump
934 function into JFUNC if so:
936 1) The passed value is loaded from a formal parameter which is not a gimple
937 register (most probably because it is addressable, the value has to be
938 scalar) and we can guarantee the value has not changed. This case can
939 therefore be described by a simple pass-through jump function. For example:
941 foo (int a)
943 int a.0;
945 a.0_2 = a;
946 bar (a.0_2);
948 2) The passed value can be described by a simple arithmetic pass-through
949 jump function. E.g.
951 foo (int a)
953 int D.2064;
955 D.2064_4 = a.1(D) + 4;
956 bar (D.2064_4);
958 This case can also occur in combination of the previous one, e.g.:
960 foo (int a, int z)
962 int a.0;
963 int D.2064;
965 a.0_3 = a;
966 D.2064_4 = a.0_3 + 4;
967 foo (D.2064_4);
969 3) The passed value is an address of an object within another one (which
970 also passed by reference). Such situations are described by an ancestor
971 jump function and describe situations such as:
973 B::foo() (struct B * const this)
975 struct A * D.1845;
977 D.1845_2 = &this_1(D)->D.1748;
978 A::bar (D.1845_2);
980 INFO is the structure describing individual parameters access different
981 stages of IPA optimizations. PARMS_AINFO contains the information that is
982 only needed for intraprocedural analysis. */
984 static void
985 compute_complex_assign_jump_func (struct ipa_node_params *info,
986 struct param_analysis_info *parms_ainfo,
987 struct ipa_jump_func *jfunc,
988 gimple call, gimple stmt, tree name,
989 tree param_type)
991 HOST_WIDE_INT offset, size, max_size;
992 tree op1, tc_ssa, base, ssa;
993 int index;
995 op1 = gimple_assign_rhs1 (stmt);
997 if (TREE_CODE (op1) == SSA_NAME)
999 if (SSA_NAME_IS_DEFAULT_DEF (op1))
1000 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1001 else
1002 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
1003 SSA_NAME_DEF_STMT (op1));
1004 tc_ssa = op1;
1006 else
1008 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
1009 tc_ssa = gimple_assign_lhs (stmt);
1012 if (index >= 0)
1014 tree op2 = gimple_assign_rhs2 (stmt);
1016 if (op2)
1018 if (!is_gimple_ip_invariant (op2)
1019 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
1020 && !useless_type_conversion_p (TREE_TYPE (name),
1021 TREE_TYPE (op1))))
1022 return;
1024 ipa_set_jf_arith_pass_through (jfunc, index, op2,
1025 gimple_assign_rhs_code (stmt));
1027 else if (gimple_assign_single_p (stmt))
1029 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1030 call, tc_ssa);
1031 bool type_p = false;
1033 if (param_type && POINTER_TYPE_P (param_type))
1034 type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type),
1035 call, jfunc);
1036 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1037 ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p);
1039 return;
1042 if (TREE_CODE (op1) != ADDR_EXPR)
1043 return;
1044 op1 = TREE_OPERAND (op1, 0);
1045 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
1046 return;
1047 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1048 if (TREE_CODE (base) != MEM_REF
1049 /* If this is a varying address, punt. */
1050 || max_size == -1
1051 || max_size != size)
1052 return;
1053 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
1054 ssa = TREE_OPERAND (base, 0);
1055 if (TREE_CODE (ssa) != SSA_NAME
1056 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1057 || offset < 0)
1058 return;
1060 /* Dynamic types are changed in constructors and destructors. */
1061 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1062 if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1064 bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type),
1065 call, jfunc, offset);
1066 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1067 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
1068 parm_ref_data_pass_through_p (&parms_ainfo[index],
1069 call, ssa), type_p);
1073 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1074 it looks like:
1076 iftmp.1_3 = &obj_2(D)->D.1762;
1078 The base of the MEM_REF must be a default definition SSA NAME of a
1079 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1080 whole MEM_REF expression is returned and the offset calculated from any
1081 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1082 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1084 static tree
1085 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1087 HOST_WIDE_INT size, max_size;
1088 tree expr, parm, obj;
1090 if (!gimple_assign_single_p (assign))
1091 return NULL_TREE;
1092 expr = gimple_assign_rhs1 (assign);
1094 if (TREE_CODE (expr) != ADDR_EXPR)
1095 return NULL_TREE;
1096 expr = TREE_OPERAND (expr, 0);
1097 obj = expr;
1098 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1100 if (TREE_CODE (expr) != MEM_REF
1101 /* If this is a varying address, punt. */
1102 || max_size == -1
1103 || max_size != size
1104 || *offset < 0)
1105 return NULL_TREE;
1106 parm = TREE_OPERAND (expr, 0);
1107 if (TREE_CODE (parm) != SSA_NAME
1108 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1109 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1110 return NULL_TREE;
1112 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1113 *obj_p = obj;
1114 return expr;
1118 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1119 statement PHI, try to find out whether NAME is in fact a
1120 multiple-inheritance typecast from a descendant into an ancestor of a formal
1121 parameter and thus can be described by an ancestor jump function and if so,
1122 write the appropriate function into JFUNC.
1124 Essentially we want to match the following pattern:
1126 if (obj_2(D) != 0B)
1127 goto <bb 3>;
1128 else
1129 goto <bb 4>;
1131 <bb 3>:
1132 iftmp.1_3 = &obj_2(D)->D.1762;
1134 <bb 4>:
1135 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1136 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1137 return D.1879_6; */
1139 static void
1140 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1141 struct param_analysis_info *parms_ainfo,
1142 struct ipa_jump_func *jfunc,
1143 gimple call, gimple phi, tree param_type)
1145 HOST_WIDE_INT offset;
1146 gimple assign, cond;
1147 basic_block phi_bb, assign_bb, cond_bb;
1148 tree tmp, parm, expr, obj;
1149 int index, i;
1151 if (gimple_phi_num_args (phi) != 2)
1152 return;
1154 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1155 tmp = PHI_ARG_DEF (phi, 0);
1156 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1157 tmp = PHI_ARG_DEF (phi, 1);
1158 else
1159 return;
1160 if (TREE_CODE (tmp) != SSA_NAME
1161 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1162 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1163 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1164 return;
1166 assign = SSA_NAME_DEF_STMT (tmp);
1167 assign_bb = gimple_bb (assign);
1168 if (!single_pred_p (assign_bb))
1169 return;
1170 expr = get_ancestor_addr_info (assign, &obj, &offset);
1171 if (!expr)
1172 return;
1173 parm = TREE_OPERAND (expr, 0);
1174 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1175 gcc_assert (index >= 0);
1177 cond_bb = single_pred (assign_bb);
1178 cond = last_stmt (cond_bb);
1179 if (!cond
1180 || gimple_code (cond) != GIMPLE_COND
1181 || gimple_cond_code (cond) != NE_EXPR
1182 || gimple_cond_lhs (cond) != parm
1183 || !integer_zerop (gimple_cond_rhs (cond)))
1184 return;
1186 phi_bb = gimple_bb (phi);
1187 for (i = 0; i < 2; i++)
1189 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1190 if (pred != assign_bb && pred != cond_bb)
1191 return;
1194 bool type_p = false;
1195 if (param_type && POINTER_TYPE_P (param_type))
1196 type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type),
1197 call, jfunc, offset);
1198 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1199 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1200 parm_ref_data_pass_through_p (&parms_ainfo[index],
1201 call, parm), type_p);
1204 /* Given OP which is passed as an actual argument to a called function,
1205 determine if it is possible to construct a KNOWN_TYPE jump function for it
1206 and if so, create one and store it to JFUNC.
1207 EXPECTED_TYPE represents a type the argument should be in */
1209 static void
1210 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1211 gimple call, tree expected_type)
1213 HOST_WIDE_INT offset, size, max_size;
1214 tree base;
1216 if (!flag_devirtualize
1217 || TREE_CODE (op) != ADDR_EXPR
1218 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE
1219 /* Be sure expected_type is polymorphic. */
1220 || !expected_type
1221 || TREE_CODE (expected_type) != RECORD_TYPE
1222 || !TYPE_BINFO (expected_type)
1223 || !BINFO_VTABLE (TYPE_BINFO (expected_type)))
1224 return;
1226 op = TREE_OPERAND (op, 0);
1227 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1228 if (!DECL_P (base)
1229 || max_size == -1
1230 || max_size != size
1231 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1232 || is_global_var (base))
1233 return;
1235 if (detect_type_change (op, base, expected_type, call, jfunc, offset))
1236 return;
1238 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base),
1239 expected_type);
1242 /* Inspect the given TYPE and return true iff it has the same structure (the
1243 same number of fields of the same types) as a C++ member pointer. If
1244 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1245 corresponding fields there. */
1247 static bool
1248 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1250 tree fld;
1252 if (TREE_CODE (type) != RECORD_TYPE)
1253 return false;
1255 fld = TYPE_FIELDS (type);
1256 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1257 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1258 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1259 return false;
1261 if (method_ptr)
1262 *method_ptr = fld;
1264 fld = DECL_CHAIN (fld);
1265 if (!fld || INTEGRAL_TYPE_P (fld)
1266 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1267 return false;
1268 if (delta)
1269 *delta = fld;
1271 if (DECL_CHAIN (fld))
1272 return false;
1274 return true;
1277 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1278 return the rhs of its defining statement. Otherwise return RHS as it
1279 is. */
1281 static inline tree
1282 get_ssa_def_if_simple_copy (tree rhs)
1284 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1286 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1288 if (gimple_assign_single_p (def_stmt))
1289 rhs = gimple_assign_rhs1 (def_stmt);
1290 else
1291 break;
1293 return rhs;
1296 /* Simple linked list, describing known contents of an aggregate beforere
1297 call. */
1299 struct ipa_known_agg_contents_list
1301 /* Offset and size of the described part of the aggregate. */
1302 HOST_WIDE_INT offset, size;
1303 /* Known constant value or NULL if the contents is known to be unknown. */
1304 tree constant;
1305 /* Pointer to the next structure in the list. */
1306 struct ipa_known_agg_contents_list *next;
1309 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1310 in ARG is filled in with constant values. ARG can either be an aggregate
1311 expression or a pointer to an aggregate. JFUNC is the jump function into
1312 which the constants are subsequently stored. */
1314 static void
1315 determine_known_aggregate_parts (gimple call, tree arg,
1316 struct ipa_jump_func *jfunc)
1318 struct ipa_known_agg_contents_list *list = NULL;
1319 int item_count = 0, const_count = 0;
1320 HOST_WIDE_INT arg_offset, arg_size;
1321 gimple_stmt_iterator gsi;
1322 tree arg_base;
1323 bool check_ref, by_ref;
1324 ao_ref r;
1326 /* The function operates in three stages. First, we prepare check_ref, r,
1327 arg_base and arg_offset based on what is actually passed as an actual
1328 argument. */
1330 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1332 by_ref = true;
1333 if (TREE_CODE (arg) == SSA_NAME)
1335 tree type_size;
1336 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1337 return;
1338 check_ref = true;
1339 arg_base = arg;
1340 arg_offset = 0;
1341 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1342 arg_size = tree_low_cst (type_size, 1);
1343 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1345 else if (TREE_CODE (arg) == ADDR_EXPR)
1347 HOST_WIDE_INT arg_max_size;
1349 arg = TREE_OPERAND (arg, 0);
1350 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1351 &arg_max_size);
1352 if (arg_max_size == -1
1353 || arg_max_size != arg_size
1354 || arg_offset < 0)
1355 return;
1356 if (DECL_P (arg_base))
1358 tree size;
1359 check_ref = false;
1360 size = build_int_cst (integer_type_node, arg_size);
1361 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1363 else
1364 return;
1366 else
1367 return;
1369 else
1371 HOST_WIDE_INT arg_max_size;
1373 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1375 by_ref = false;
1376 check_ref = false;
1377 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1378 &arg_max_size);
1379 if (arg_max_size == -1
1380 || arg_max_size != arg_size
1381 || arg_offset < 0)
1382 return;
1384 ao_ref_init (&r, arg);
1387 /* Second stage walks back the BB, looks at individual statements and as long
1388 as it is confident of how the statements affect contents of the
1389 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1390 describing it. */
1391 gsi = gsi_for_stmt (call);
1392 gsi_prev (&gsi);
1393 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1395 struct ipa_known_agg_contents_list *n, **p;
1396 gimple stmt = gsi_stmt (gsi);
1397 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1398 tree lhs, rhs, lhs_base;
1399 bool partial_overlap;
1401 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1402 continue;
1403 if (!gimple_assign_single_p (stmt))
1404 break;
1406 lhs = gimple_assign_lhs (stmt);
1407 rhs = gimple_assign_rhs1 (stmt);
1408 if (!is_gimple_reg_type (rhs)
1409 || TREE_CODE (lhs) == BIT_FIELD_REF
1410 || contains_bitfld_component_ref_p (lhs))
1411 break;
1413 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1414 &lhs_max_size);
1415 if (lhs_max_size == -1
1416 || lhs_max_size != lhs_size
1417 || (lhs_offset < arg_offset
1418 && lhs_offset + lhs_size > arg_offset)
1419 || (lhs_offset < arg_offset + arg_size
1420 && lhs_offset + lhs_size > arg_offset + arg_size))
1421 break;
1423 if (check_ref)
1425 if (TREE_CODE (lhs_base) != MEM_REF
1426 || TREE_OPERAND (lhs_base, 0) != arg_base
1427 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1428 break;
1430 else if (lhs_base != arg_base)
1432 if (DECL_P (lhs_base))
1433 continue;
1434 else
1435 break;
1438 if (lhs_offset + lhs_size < arg_offset
1439 || lhs_offset >= (arg_offset + arg_size))
1440 continue;
1442 partial_overlap = false;
1443 p = &list;
1444 while (*p && (*p)->offset < lhs_offset)
1446 if ((*p)->offset + (*p)->size > lhs_offset)
1448 partial_overlap = true;
1449 break;
1451 p = &(*p)->next;
1453 if (partial_overlap)
1454 break;
1455 if (*p && (*p)->offset < lhs_offset + lhs_size)
1457 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1458 /* We already know this value is subsequently overwritten with
1459 something else. */
1460 continue;
1461 else
1462 /* Otherwise this is a partial overlap which we cannot
1463 represent. */
1464 break;
1467 rhs = get_ssa_def_if_simple_copy (rhs);
1468 n = XALLOCA (struct ipa_known_agg_contents_list);
1469 n->size = lhs_size;
1470 n->offset = lhs_offset;
1471 if (is_gimple_ip_invariant (rhs))
1473 n->constant = rhs;
1474 const_count++;
1476 else
1477 n->constant = NULL_TREE;
1478 n->next = *p;
1479 *p = n;
1481 item_count++;
1482 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1483 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1484 break;
1487 /* Third stage just goes over the list and creates an appropriate vector of
1488 ipa_agg_jf_item structures out of it, of sourse only if there are
1489 any known constants to begin with. */
1491 if (const_count)
1493 jfunc->agg.by_ref = by_ref;
1494 vec_alloc (jfunc->agg.items, const_count);
1495 while (list)
1497 if (list->constant)
1499 struct ipa_agg_jf_item item;
1500 item.offset = list->offset - arg_offset;
1501 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1502 item.value = unshare_expr_without_location (list->constant);
1503 jfunc->agg.items->quick_push (item);
1505 list = list->next;
1510 static tree
1511 ipa_get_callee_param_type (struct cgraph_edge *e, int i)
1513 int n;
1514 tree type = (e->callee
1515 ? TREE_TYPE (e->callee->symbol.decl)
1516 : gimple_call_fntype (e->call_stmt));
1517 tree t = TYPE_ARG_TYPES (type);
1519 for (n = 0; n < i; n++)
1521 if (!t)
1522 break;
1523 t = TREE_CHAIN (t);
1525 if (t)
1526 return TREE_VALUE (t);
1527 if (!e->callee)
1528 return NULL;
1529 t = DECL_ARGUMENTS (e->callee->symbol.decl);
1530 for (n = 0; n < i; n++)
1532 if (!t)
1533 return NULL;
1534 t = TREE_CHAIN (t);
1536 if (t)
1537 return TREE_TYPE (t);
1538 return NULL;
1541 /* Compute jump function for all arguments of callsite CS and insert the
1542 information in the jump_functions array in the ipa_edge_args corresponding
1543 to this callsite. */
1545 static void
1546 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1547 struct cgraph_edge *cs)
1549 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1550 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1551 gimple call = cs->call_stmt;
1552 int n, arg_num = gimple_call_num_args (call);
1554 if (arg_num == 0 || args->jump_functions)
1555 return;
1556 vec_safe_grow_cleared (args->jump_functions, arg_num);
1558 if (gimple_call_internal_p (call))
1559 return;
1560 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1561 return;
1563 for (n = 0; n < arg_num; n++)
1565 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1566 tree arg = gimple_call_arg (call, n);
1567 tree param_type = ipa_get_callee_param_type (cs, n);
1569 if (is_gimple_ip_invariant (arg))
1570 ipa_set_jf_constant (jfunc, arg, cs);
1571 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1572 && TREE_CODE (arg) == PARM_DECL)
1574 int index = ipa_get_param_decl_index (info, arg);
1576 gcc_assert (index >=0);
1577 /* Aggregate passed by value, check for pass-through, otherwise we
1578 will attempt to fill in aggregate contents later in this
1579 for cycle. */
1580 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1582 ipa_set_jf_simple_pass_through (jfunc, index, false, false);
1583 continue;
1586 else if (TREE_CODE (arg) == SSA_NAME)
1588 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1590 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1591 if (index >= 0)
1593 bool agg_p, type_p;
1594 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1595 call, arg);
1596 if (param_type && POINTER_TYPE_P (param_type))
1597 type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type),
1598 call, jfunc);
1599 else
1600 type_p = false;
1601 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1602 ipa_set_jf_simple_pass_through (jfunc, index, agg_p,
1603 type_p);
1606 else
1608 gimple stmt = SSA_NAME_DEF_STMT (arg);
1609 if (is_gimple_assign (stmt))
1610 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1611 call, stmt, arg, param_type);
1612 else if (gimple_code (stmt) == GIMPLE_PHI)
1613 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1614 call, stmt, param_type);
1617 else
1618 compute_known_type_jump_func (arg, jfunc, call,
1619 param_type
1620 && POINTER_TYPE_P (param_type)
1621 ? TREE_TYPE (param_type)
1622 : NULL);
1624 if ((jfunc->type != IPA_JF_PASS_THROUGH
1625 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1626 && (jfunc->type != IPA_JF_ANCESTOR
1627 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1628 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1629 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1630 determine_known_aggregate_parts (call, arg, jfunc);
1634 /* Compute jump functions for all edges - both direct and indirect - outgoing
1635 from NODE. Also count the actual arguments in the process. */
1637 static void
1638 ipa_compute_jump_functions (struct cgraph_node *node,
1639 struct param_analysis_info *parms_ainfo)
1641 struct cgraph_edge *cs;
1643 for (cs = node->callees; cs; cs = cs->next_callee)
1645 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1646 NULL);
1647 /* We do not need to bother analyzing calls to unknown
1648 functions unless they may become known during lto/whopr. */
1649 if (!callee->symbol.definition && !flag_lto)
1650 continue;
1651 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1654 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1655 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1658 /* If STMT looks like a statement loading a value from a member pointer formal
1659 parameter, return that parameter and store the offset of the field to
1660 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1661 might be clobbered). If USE_DELTA, then we look for a use of the delta
1662 field rather than the pfn. */
1664 static tree
1665 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1666 HOST_WIDE_INT *offset_p)
1668 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1670 if (!gimple_assign_single_p (stmt))
1671 return NULL_TREE;
1673 rhs = gimple_assign_rhs1 (stmt);
1674 if (TREE_CODE (rhs) == COMPONENT_REF)
1676 ref_field = TREE_OPERAND (rhs, 1);
1677 rhs = TREE_OPERAND (rhs, 0);
1679 else
1680 ref_field = NULL_TREE;
1681 if (TREE_CODE (rhs) != MEM_REF)
1682 return NULL_TREE;
1683 rec = TREE_OPERAND (rhs, 0);
1684 if (TREE_CODE (rec) != ADDR_EXPR)
1685 return NULL_TREE;
1686 rec = TREE_OPERAND (rec, 0);
1687 if (TREE_CODE (rec) != PARM_DECL
1688 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1689 return NULL_TREE;
1690 ref_offset = TREE_OPERAND (rhs, 1);
1692 if (use_delta)
1693 fld = delta_field;
1694 else
1695 fld = ptr_field;
1696 if (offset_p)
1697 *offset_p = int_bit_position (fld);
1699 if (ref_field)
1701 if (integer_nonzerop (ref_offset))
1702 return NULL_TREE;
1703 return ref_field == fld ? rec : NULL_TREE;
1705 else
1706 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1707 : NULL_TREE;
1710 /* Returns true iff T is an SSA_NAME defined by a statement. */
1712 static bool
1713 ipa_is_ssa_with_stmt_def (tree t)
1715 if (TREE_CODE (t) == SSA_NAME
1716 && !SSA_NAME_IS_DEFAULT_DEF (t))
1717 return true;
1718 else
1719 return false;
1722 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1723 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1724 indirect call graph edge. */
1726 static struct cgraph_edge *
1727 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1729 struct cgraph_edge *cs;
1731 cs = cgraph_edge (node, stmt);
1732 cs->indirect_info->param_index = param_index;
1733 cs->indirect_info->offset = 0;
1734 cs->indirect_info->polymorphic = 0;
1735 cs->indirect_info->agg_contents = 0;
1736 cs->indirect_info->member_ptr = 0;
1737 return cs;
1740 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1741 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1742 intermediate information about each formal parameter. Currently it checks
1743 whether the call calls a pointer that is a formal parameter and if so, the
1744 parameter is marked with the called flag and an indirect call graph edge
1745 describing the call is created. This is very simple for ordinary pointers
1746 represented in SSA but not-so-nice when it comes to member pointers. The
1747 ugly part of this function does nothing more than trying to match the
1748 pattern of such a call. An example of such a pattern is the gimple dump
1749 below, the call is on the last line:
1751 <bb 2>:
1752 f$__delta_5 = f.__delta;
1753 f$__pfn_24 = f.__pfn;
1756 <bb 2>:
1757 f$__delta_5 = MEM[(struct *)&f];
1758 f$__pfn_24 = MEM[(struct *)&f + 4B];
1760 and a few lines below:
1762 <bb 5>
1763 D.2496_3 = (int) f$__pfn_24;
1764 D.2497_4 = D.2496_3 & 1;
1765 if (D.2497_4 != 0)
1766 goto <bb 3>;
1767 else
1768 goto <bb 4>;
1770 <bb 6>:
1771 D.2500_7 = (unsigned int) f$__delta_5;
1772 D.2501_8 = &S + D.2500_7;
1773 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1774 D.2503_10 = *D.2502_9;
1775 D.2504_12 = f$__pfn_24 + -1;
1776 D.2505_13 = (unsigned int) D.2504_12;
1777 D.2506_14 = D.2503_10 + D.2505_13;
1778 D.2507_15 = *D.2506_14;
1779 iftmp.11_16 = (String:: *) D.2507_15;
1781 <bb 7>:
1782 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1783 D.2500_19 = (unsigned int) f$__delta_5;
1784 D.2508_20 = &S + D.2500_19;
1785 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1787 Such patterns are results of simple calls to a member pointer:
1789 int doprinting (int (MyString::* f)(int) const)
1791 MyString S ("somestring");
1793 return (S.*f)(4);
1796 Moreover, the function also looks for called pointers loaded from aggregates
1797 passed by value or reference. */
1799 static void
1800 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1801 struct ipa_node_params *info,
1802 struct param_analysis_info *parms_ainfo,
1803 gimple call, tree target)
1805 gimple def;
1806 tree n1, n2;
1807 gimple d1, d2;
1808 tree rec, rec2, cond;
1809 gimple branch;
1810 int index;
1811 basic_block bb, virt_bb, join;
1812 HOST_WIDE_INT offset;
1813 bool by_ref;
1815 if (SSA_NAME_IS_DEFAULT_DEF (target))
1817 tree var = SSA_NAME_VAR (target);
1818 index = ipa_get_param_decl_index (info, var);
1819 if (index >= 0)
1820 ipa_note_param_call (node, index, call);
1821 return;
1824 def = SSA_NAME_DEF_STMT (target);
1825 if (gimple_assign_single_p (def)
1826 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1827 gimple_assign_rhs1 (def), &index, &offset,
1828 &by_ref))
1830 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1831 cs->indirect_info->offset = offset;
1832 cs->indirect_info->agg_contents = 1;
1833 cs->indirect_info->by_ref = by_ref;
1834 return;
1837 /* Now we need to try to match the complex pattern of calling a member
1838 pointer. */
1839 if (gimple_code (def) != GIMPLE_PHI
1840 || gimple_phi_num_args (def) != 2
1841 || !POINTER_TYPE_P (TREE_TYPE (target))
1842 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1843 return;
1845 /* First, we need to check whether one of these is a load from a member
1846 pointer that is a parameter to this function. */
1847 n1 = PHI_ARG_DEF (def, 0);
1848 n2 = PHI_ARG_DEF (def, 1);
1849 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1850 return;
1851 d1 = SSA_NAME_DEF_STMT (n1);
1852 d2 = SSA_NAME_DEF_STMT (n2);
1854 join = gimple_bb (def);
1855 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1857 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1858 return;
1860 bb = EDGE_PRED (join, 0)->src;
1861 virt_bb = gimple_bb (d2);
1863 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1865 bb = EDGE_PRED (join, 1)->src;
1866 virt_bb = gimple_bb (d1);
1868 else
1869 return;
1871 /* Second, we need to check that the basic blocks are laid out in the way
1872 corresponding to the pattern. */
1874 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1875 || single_pred (virt_bb) != bb
1876 || single_succ (virt_bb) != join)
1877 return;
1879 /* Third, let's see that the branching is done depending on the least
1880 significant bit of the pfn. */
1882 branch = last_stmt (bb);
1883 if (!branch || gimple_code (branch) != GIMPLE_COND)
1884 return;
1886 if ((gimple_cond_code (branch) != NE_EXPR
1887 && gimple_cond_code (branch) != EQ_EXPR)
1888 || !integer_zerop (gimple_cond_rhs (branch)))
1889 return;
1891 cond = gimple_cond_lhs (branch);
1892 if (!ipa_is_ssa_with_stmt_def (cond))
1893 return;
1895 def = SSA_NAME_DEF_STMT (cond);
1896 if (!is_gimple_assign (def)
1897 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1898 || !integer_onep (gimple_assign_rhs2 (def)))
1899 return;
1901 cond = gimple_assign_rhs1 (def);
1902 if (!ipa_is_ssa_with_stmt_def (cond))
1903 return;
1905 def = SSA_NAME_DEF_STMT (cond);
1907 if (is_gimple_assign (def)
1908 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1910 cond = gimple_assign_rhs1 (def);
1911 if (!ipa_is_ssa_with_stmt_def (cond))
1912 return;
1913 def = SSA_NAME_DEF_STMT (cond);
1916 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1917 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1918 == ptrmemfunc_vbit_in_delta),
1919 NULL);
1920 if (rec != rec2)
1921 return;
1923 index = ipa_get_param_decl_index (info, rec);
1924 if (index >= 0
1925 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1927 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1928 cs->indirect_info->offset = offset;
1929 cs->indirect_info->agg_contents = 1;
1930 cs->indirect_info->member_ptr = 1;
1933 return;
1936 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1937 object referenced in the expression is a formal parameter of the caller
1938 (described by INFO), create a call note for the statement. */
1940 static void
1941 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1942 struct ipa_node_params *info, gimple call,
1943 tree target)
1945 struct cgraph_edge *cs;
1946 struct cgraph_indirect_call_info *ii;
1947 struct ipa_jump_func jfunc;
1948 tree obj = OBJ_TYPE_REF_OBJECT (target);
1949 int index;
1950 HOST_WIDE_INT anc_offset;
1952 if (!flag_devirtualize)
1953 return;
1955 if (TREE_CODE (obj) != SSA_NAME)
1956 return;
1958 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1960 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1961 return;
1963 anc_offset = 0;
1964 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1965 gcc_assert (index >= 0);
1966 if (detect_type_change_ssa (obj, obj_type_ref_class (target),
1967 call, &jfunc))
1968 return;
1970 else
1972 gimple stmt = SSA_NAME_DEF_STMT (obj);
1973 tree expr;
1975 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1976 if (!expr)
1977 return;
1978 index = ipa_get_param_decl_index (info,
1979 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1980 gcc_assert (index >= 0);
1981 if (detect_type_change (obj, expr, obj_type_ref_class (target),
1982 call, &jfunc, anc_offset))
1983 return;
1986 cs = ipa_note_param_call (node, index, call);
1987 ii = cs->indirect_info;
1988 ii->offset = anc_offset;
1989 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1990 ii->otr_type = obj_type_ref_class (target);
1991 ii->polymorphic = 1;
1994 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1995 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1996 containing intermediate information about each formal parameter. */
1998 static void
1999 ipa_analyze_call_uses (struct cgraph_node *node,
2000 struct ipa_node_params *info,
2001 struct param_analysis_info *parms_ainfo, gimple call)
2003 tree target = gimple_call_fn (call);
2005 if (!target)
2006 return;
2007 if (TREE_CODE (target) == SSA_NAME)
2008 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
2009 else if (virtual_method_call_p (target))
2010 ipa_analyze_virtual_call_uses (node, info, call, target);
2014 /* Analyze the call statement STMT with respect to formal parameters (described
2015 in INFO) of caller given by NODE. Currently it only checks whether formal
2016 parameters are called. PARMS_AINFO is a pointer to a vector containing
2017 intermediate information about each formal parameter. */
2019 static void
2020 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
2021 struct param_analysis_info *parms_ainfo, gimple stmt)
2023 if (is_gimple_call (stmt))
2024 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
2027 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
2028 If OP is a parameter declaration, mark it as used in the info structure
2029 passed in DATA. */
2031 static bool
2032 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
2033 tree op, void *data)
2035 struct ipa_node_params *info = (struct ipa_node_params *) data;
2037 op = get_base_address (op);
2038 if (op
2039 && TREE_CODE (op) == PARM_DECL)
2041 int index = ipa_get_param_decl_index (info, op);
2042 gcc_assert (index >= 0);
2043 ipa_set_param_used (info, index, true);
2046 return false;
2049 /* Scan the function body of NODE and inspect the uses of formal parameters.
2050 Store the findings in various structures of the associated ipa_node_params
2051 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
2052 vector containing intermediate information about each formal parameter. */
2054 static void
2055 ipa_analyze_params_uses (struct cgraph_node *node,
2056 struct param_analysis_info *parms_ainfo)
2058 tree decl = node->symbol.decl;
2059 basic_block bb;
2060 struct function *func;
2061 gimple_stmt_iterator gsi;
2062 struct ipa_node_params *info = IPA_NODE_REF (node);
2063 int i;
2065 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
2066 return;
2068 info->uses_analysis_done = 1;
2069 if (ipa_func_spec_opts_forbid_analysis_p (node))
2071 for (i = 0; i < ipa_get_param_count (info); i++)
2073 ipa_set_param_used (info, i, true);
2074 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
2076 return;
2079 for (i = 0; i < ipa_get_param_count (info); i++)
2081 tree parm = ipa_get_param (info, i);
2082 int controlled_uses = 0;
2084 /* For SSA regs see if parameter is used. For non-SSA we compute
2085 the flag during modification analysis. */
2086 if (is_gimple_reg (parm))
2088 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->symbol.decl),
2089 parm);
2090 if (ddef && !has_zero_uses (ddef))
2092 imm_use_iterator imm_iter;
2093 use_operand_p use_p;
2095 ipa_set_param_used (info, i, true);
2096 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
2097 if (!is_gimple_call (USE_STMT (use_p)))
2099 controlled_uses = IPA_UNDESCRIBED_USE;
2100 break;
2102 else
2103 controlled_uses++;
2105 else
2106 controlled_uses = 0;
2108 else
2109 controlled_uses = IPA_UNDESCRIBED_USE;
2110 ipa_set_controlled_uses (info, i, controlled_uses);
2113 func = DECL_STRUCT_FUNCTION (decl);
2114 FOR_EACH_BB_FN (bb, func)
2116 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2118 gimple stmt = gsi_stmt (gsi);
2120 if (is_gimple_debug (stmt))
2121 continue;
2123 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
2124 walk_stmt_load_store_addr_ops (stmt, info,
2125 visit_ref_for_mod_analysis,
2126 visit_ref_for_mod_analysis,
2127 visit_ref_for_mod_analysis);
2129 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2130 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
2131 visit_ref_for_mod_analysis,
2132 visit_ref_for_mod_analysis,
2133 visit_ref_for_mod_analysis);
2137 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
2139 static void
2140 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
2142 int i;
2144 for (i = 0; i < param_count; i++)
2146 if (parms_ainfo[i].parm_visited_statements)
2147 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
2148 if (parms_ainfo[i].pt_visited_statements)
2149 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
2153 /* Initialize the array describing properties of of formal parameters
2154 of NODE, analyze their uses and compute jump functions associated
2155 with actual arguments of calls from within NODE. */
2157 void
2158 ipa_analyze_node (struct cgraph_node *node)
2160 struct ipa_node_params *info;
2161 struct param_analysis_info *parms_ainfo;
2162 int param_count;
2164 ipa_check_create_node_params ();
2165 ipa_check_create_edge_args ();
2166 info = IPA_NODE_REF (node);
2167 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2168 ipa_initialize_node_params (node);
2170 param_count = ipa_get_param_count (info);
2171 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
2172 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
2174 ipa_analyze_params_uses (node, parms_ainfo);
2175 ipa_compute_jump_functions (node, parms_ainfo);
2177 free_parms_ainfo (parms_ainfo, param_count);
2178 pop_cfun ();
2181 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2182 attempt a type-based devirtualization. If successful, return the
2183 target function declaration, otherwise return NULL. */
2185 tree
2186 ipa_intraprocedural_devirtualization (gimple call)
2188 tree binfo, token, fndecl;
2189 struct ipa_jump_func jfunc;
2190 tree otr = gimple_call_fn (call);
2192 jfunc.type = IPA_JF_UNKNOWN;
2193 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2194 call, obj_type_ref_class (otr));
2195 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2196 return NULL_TREE;
2197 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2198 if (!binfo)
2199 return NULL_TREE;
2200 token = OBJ_TYPE_REF_TOKEN (otr);
2201 fndecl = gimple_get_virt_method_for_binfo (tree_low_cst (token, 1),
2202 binfo);
2203 #ifdef ENABLE_CHECKING
2204 if (fndecl)
2205 gcc_assert (possible_polymorphic_call_target_p
2206 (otr, cgraph_get_node (fndecl)));
2207 #endif
2208 return fndecl;
2211 /* Update the jump function DST when the call graph edge corresponding to SRC is
2212 is being inlined, knowing that DST is of type ancestor and src of known
2213 type. */
2215 static void
2216 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2217 struct ipa_jump_func *dst)
2219 HOST_WIDE_INT combined_offset;
2220 tree combined_type;
2222 if (!ipa_get_jf_ancestor_type_preserved (dst))
2224 dst->type = IPA_JF_UNKNOWN;
2225 return;
2228 combined_offset = ipa_get_jf_known_type_offset (src)
2229 + ipa_get_jf_ancestor_offset (dst);
2230 combined_type = ipa_get_jf_ancestor_type (dst);
2232 ipa_set_jf_known_type (dst, combined_offset,
2233 ipa_get_jf_known_type_base_type (src),
2234 combined_type);
2237 /* Update the jump functions associated with call graph edge E when the call
2238 graph edge CS is being inlined, assuming that E->caller is already (possibly
2239 indirectly) inlined into CS->callee and that E has not been inlined. */
2241 static void
2242 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2243 struct cgraph_edge *e)
2245 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2246 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2247 int count = ipa_get_cs_argument_count (args);
2248 int i;
2250 for (i = 0; i < count; i++)
2252 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2254 if (dst->type == IPA_JF_ANCESTOR)
2256 struct ipa_jump_func *src;
2257 int dst_fid = dst->value.ancestor.formal_id;
2259 /* Variable number of arguments can cause havoc if we try to access
2260 one that does not exist in the inlined edge. So make sure we
2261 don't. */
2262 if (dst_fid >= ipa_get_cs_argument_count (top))
2264 dst->type = IPA_JF_UNKNOWN;
2265 continue;
2268 src = ipa_get_ith_jump_func (top, dst_fid);
2270 if (src->agg.items
2271 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2273 struct ipa_agg_jf_item *item;
2274 int j;
2276 /* Currently we do not produce clobber aggregate jump functions,
2277 replace with merging when we do. */
2278 gcc_assert (!dst->agg.items);
2280 dst->agg.items = vec_safe_copy (src->agg.items);
2281 dst->agg.by_ref = src->agg.by_ref;
2282 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2283 item->offset -= dst->value.ancestor.offset;
2286 if (src->type == IPA_JF_KNOWN_TYPE)
2287 combine_known_type_and_ancestor_jfs (src, dst);
2288 else if (src->type == IPA_JF_PASS_THROUGH
2289 && src->value.pass_through.operation == NOP_EXPR)
2291 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2292 dst->value.ancestor.agg_preserved &=
2293 src->value.pass_through.agg_preserved;
2294 dst->value.ancestor.type_preserved &=
2295 src->value.pass_through.type_preserved;
2297 else if (src->type == IPA_JF_ANCESTOR)
2299 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2300 dst->value.ancestor.offset += src->value.ancestor.offset;
2301 dst->value.ancestor.agg_preserved &=
2302 src->value.ancestor.agg_preserved;
2303 dst->value.ancestor.type_preserved &=
2304 src->value.ancestor.type_preserved;
2306 else
2307 dst->type = IPA_JF_UNKNOWN;
2309 else if (dst->type == IPA_JF_PASS_THROUGH)
2311 struct ipa_jump_func *src;
2312 /* We must check range due to calls with variable number of arguments
2313 and we cannot combine jump functions with operations. */
2314 if (dst->value.pass_through.operation == NOP_EXPR
2315 && (dst->value.pass_through.formal_id
2316 < ipa_get_cs_argument_count (top)))
2318 int dst_fid = dst->value.pass_through.formal_id;
2319 src = ipa_get_ith_jump_func (top, dst_fid);
2320 bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
2322 switch (src->type)
2324 case IPA_JF_UNKNOWN:
2325 dst->type = IPA_JF_UNKNOWN;
2326 break;
2327 case IPA_JF_KNOWN_TYPE:
2328 ipa_set_jf_known_type (dst,
2329 ipa_get_jf_known_type_offset (src),
2330 ipa_get_jf_known_type_base_type (src),
2331 ipa_get_jf_known_type_base_type (src));
2332 break;
2333 case IPA_JF_CONST:
2334 ipa_set_jf_cst_copy (dst, src);
2335 break;
2337 case IPA_JF_PASS_THROUGH:
2339 int formal_id = ipa_get_jf_pass_through_formal_id (src);
2340 enum tree_code operation;
2341 operation = ipa_get_jf_pass_through_operation (src);
2343 if (operation == NOP_EXPR)
2345 bool agg_p, type_p;
2346 agg_p = dst_agg_p
2347 && ipa_get_jf_pass_through_agg_preserved (src);
2348 type_p = ipa_get_jf_pass_through_type_preserved (src)
2349 && ipa_get_jf_pass_through_type_preserved (dst);
2350 ipa_set_jf_simple_pass_through (dst, formal_id,
2351 agg_p, type_p);
2353 else
2355 tree operand = ipa_get_jf_pass_through_operand (src);
2356 ipa_set_jf_arith_pass_through (dst, formal_id, operand,
2357 operation);
2359 break;
2361 case IPA_JF_ANCESTOR:
2363 bool agg_p, type_p;
2364 agg_p = dst_agg_p
2365 && ipa_get_jf_ancestor_agg_preserved (src);
2366 type_p = ipa_get_jf_ancestor_type_preserved (src)
2367 && ipa_get_jf_pass_through_type_preserved (dst);
2368 ipa_set_ancestor_jf (dst,
2369 ipa_get_jf_ancestor_offset (src),
2370 ipa_get_jf_ancestor_type (src),
2371 ipa_get_jf_ancestor_formal_id (src),
2372 agg_p, type_p);
2373 break;
2375 default:
2376 gcc_unreachable ();
2379 if (src->agg.items
2380 && (dst_agg_p || !src->agg.by_ref))
2382 /* Currently we do not produce clobber aggregate jump
2383 functions, replace with merging when we do. */
2384 gcc_assert (!dst->agg.items);
2386 dst->agg.by_ref = src->agg.by_ref;
2387 dst->agg.items = vec_safe_copy (src->agg.items);
2390 else
2391 dst->type = IPA_JF_UNKNOWN;
2396 /* If TARGET is an addr_expr of a function declaration, make it the destination
2397 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2399 struct cgraph_edge *
2400 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2402 struct cgraph_node *callee;
2403 struct inline_edge_summary *es = inline_edge_summary (ie);
2404 bool unreachable = false;
2406 if (TREE_CODE (target) == ADDR_EXPR)
2407 target = TREE_OPERAND (target, 0);
2408 if (TREE_CODE (target) != FUNCTION_DECL)
2410 target = canonicalize_constructor_val (target, NULL);
2411 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2413 if (ie->indirect_info->member_ptr)
2414 /* Member pointer call that goes through a VMT lookup. */
2415 return NULL;
2417 if (dump_file)
2418 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2419 " in %s/%i, making it unreachable.\n",
2420 cgraph_node_name (ie->caller), ie->caller->symbol.order);
2421 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2422 callee = cgraph_get_create_node (target);
2423 unreachable = true;
2425 else
2426 callee = cgraph_get_node (target);
2428 else
2429 callee = cgraph_get_node (target);
2431 /* Because may-edges are not explicitely represented and vtable may be external,
2432 we may create the first reference to the object in the unit. */
2433 if (!callee || callee->global.inlined_to)
2436 /* We are better to ensure we can refer to it.
2437 In the case of static functions we are out of luck, since we already
2438 removed its body. In the case of public functions we may or may
2439 not introduce the reference. */
2440 if (!canonicalize_constructor_val (target, NULL)
2441 || !TREE_PUBLIC (target))
2443 if (dump_file)
2444 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2445 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2446 xstrdup (cgraph_node_name (ie->caller)),
2447 ie->caller->symbol.order,
2448 xstrdup (cgraph_node_name (ie->callee)),
2449 ie->callee->symbol.order);
2450 return NULL;
2452 callee = cgraph_get_create_real_symbol_node (target);
2454 ipa_check_create_node_params ();
2456 /* We can not make edges to inline clones. It is bug that someone removed
2457 the cgraph node too early. */
2458 gcc_assert (!callee->global.inlined_to);
2460 if (dump_file && !unreachable)
2462 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2463 "(%s/%i -> %s/%i), for stmt ",
2464 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2465 xstrdup (cgraph_node_name (ie->caller)),
2466 ie->caller->symbol.order,
2467 xstrdup (cgraph_node_name (callee)),
2468 callee->symbol.order);
2469 if (ie->call_stmt)
2470 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2471 else
2472 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2474 ie = cgraph_make_edge_direct (ie, callee);
2475 es = inline_edge_summary (ie);
2476 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2477 - eni_size_weights.call_cost);
2478 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2479 - eni_time_weights.call_cost);
2481 return ie;
2484 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2485 return NULL if there is not any. BY_REF specifies whether the value has to
2486 be passed by reference or by value. */
2488 tree
2489 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2490 HOST_WIDE_INT offset, bool by_ref)
2492 struct ipa_agg_jf_item *item;
2493 int i;
2495 if (by_ref != agg->by_ref)
2496 return NULL;
2498 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2499 if (item->offset == offset)
2501 /* Currently we do not have clobber values, return NULL for them once
2502 we do. */
2503 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2504 return item->value;
2506 return NULL;
2509 /* Remove a reference to SYMBOL from the list of references of a node given by
2510 reference description RDESC. Return true if the reference has been
2511 successfully found and removed. */
2513 static bool
2514 remove_described_reference (symtab_node symbol, struct ipa_cst_ref_desc *rdesc)
2516 struct ipa_ref *to_del;
2517 struct cgraph_edge *origin;
2519 origin = rdesc->cs;
2520 if (!origin)
2521 return false;
2522 to_del = ipa_find_reference ((symtab_node) origin->caller, symbol,
2523 origin->call_stmt, origin->lto_stmt_uid);
2524 if (!to_del)
2525 return false;
2527 ipa_remove_reference (to_del);
2528 if (dump_file)
2529 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2530 xstrdup (cgraph_node_name (origin->caller)),
2531 origin->caller->symbol.order, xstrdup (symtab_node_name (symbol)));
2532 return true;
2535 /* If JFUNC has a reference description with refcount different from
2536 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2537 NULL. JFUNC must be a constant jump function. */
2539 static struct ipa_cst_ref_desc *
2540 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2542 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2543 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2544 return rdesc;
2545 else
2546 return NULL;
2549 /* If the value of constant jump function JFUNC is an address of a function
2550 declaration, return the associated call graph node. Otherwise return
2551 NULL. */
2553 static cgraph_node *
2554 cgraph_node_for_jfunc (struct ipa_jump_func *jfunc)
2556 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
2557 tree cst = ipa_get_jf_constant (jfunc);
2558 if (TREE_CODE (cst) != ADDR_EXPR
2559 || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL)
2560 return NULL;
2562 return cgraph_get_node (TREE_OPERAND (cst, 0));
2566 /* If JFUNC is a constant jump function with a usable rdesc, decrement its
2567 refcount and if it hits zero, remove reference to SYMBOL from the caller of
2568 the edge specified in the rdesc. Return false if either the symbol or the
2569 reference could not be found, otherwise return true. */
2571 static bool
2572 try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
2574 struct ipa_cst_ref_desc *rdesc;
2575 if (jfunc->type == IPA_JF_CONST
2576 && (rdesc = jfunc_rdesc_usable (jfunc))
2577 && --rdesc->refcount == 0)
2579 symtab_node symbol = (symtab_node) cgraph_node_for_jfunc (jfunc);
2580 if (!symbol)
2581 return false;
2583 return remove_described_reference (symbol, rdesc);
2585 return true;
2588 /* Try to find a destination for indirect edge IE that corresponds to a simple
2589 call or a call of a member function pointer and where the destination is a
2590 pointer formal parameter described by jump function JFUNC. If it can be
2591 determined, return the newly direct edge, otherwise return NULL.
2592 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2594 static struct cgraph_edge *
2595 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2596 struct ipa_jump_func *jfunc,
2597 struct ipa_node_params *new_root_info)
2599 struct cgraph_edge *cs;
2600 tree target;
2601 bool agg_contents = ie->indirect_info->agg_contents;
2603 if (ie->indirect_info->agg_contents)
2604 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2605 ie->indirect_info->offset,
2606 ie->indirect_info->by_ref);
2607 else
2608 target = ipa_value_from_jfunc (new_root_info, jfunc);
2609 if (!target)
2610 return NULL;
2611 cs = ipa_make_edge_direct_to_target (ie, target);
2613 if (cs && !agg_contents)
2615 bool ok;
2616 gcc_checking_assert (cs->callee
2617 && (cs != ie
2618 || jfunc->type != IPA_JF_CONST
2619 || !cgraph_node_for_jfunc (jfunc)
2620 || cs->callee == cgraph_node_for_jfunc (jfunc)));
2621 ok = try_decrement_rdesc_refcount (jfunc);
2622 gcc_checking_assert (ok);
2625 return cs;
2628 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2629 call based on a formal parameter which is described by jump function JFUNC
2630 and if it can be determined, make it direct and return the direct edge.
2631 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2632 are relative to. */
2634 static struct cgraph_edge *
2635 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2636 struct ipa_jump_func *jfunc,
2637 struct ipa_node_params *new_root_info)
2639 tree binfo, target;
2641 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2643 if (!binfo)
2644 return NULL;
2646 if (TREE_CODE (binfo) != TREE_BINFO)
2648 binfo = gimple_extract_devirt_binfo_from_cst
2649 (binfo, ie->indirect_info->otr_type);
2650 if (!binfo)
2651 return NULL;
2654 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2655 ie->indirect_info->otr_type);
2656 if (binfo)
2657 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2658 binfo);
2659 else
2660 return NULL;
2662 if (target)
2664 #ifdef ENABLE_CHECKING
2665 gcc_assert (possible_polymorphic_call_target_p
2666 (ie, cgraph_get_node (target)));
2667 #endif
2668 return ipa_make_edge_direct_to_target (ie, target);
2670 else
2671 return NULL;
2674 /* Update the param called notes associated with NODE when CS is being inlined,
2675 assuming NODE is (potentially indirectly) inlined into CS->callee.
2676 Moreover, if the callee is discovered to be constant, create a new cgraph
2677 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2678 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2680 static bool
2681 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2682 struct cgraph_node *node,
2683 vec<cgraph_edge_p> *new_edges)
2685 struct ipa_edge_args *top;
2686 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2687 struct ipa_node_params *new_root_info;
2688 bool res = false;
2690 ipa_check_create_edge_args ();
2691 top = IPA_EDGE_REF (cs);
2692 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2693 ? cs->caller->global.inlined_to
2694 : cs->caller);
2696 for (ie = node->indirect_calls; ie; ie = next_ie)
2698 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2699 struct ipa_jump_func *jfunc;
2700 int param_index;
2702 next_ie = ie->next_callee;
2704 if (ici->param_index == -1)
2705 continue;
2707 /* We must check range due to calls with variable number of arguments: */
2708 if (ici->param_index >= ipa_get_cs_argument_count (top))
2710 ici->param_index = -1;
2711 continue;
2714 param_index = ici->param_index;
2715 jfunc = ipa_get_ith_jump_func (top, param_index);
2717 if (!flag_indirect_inlining)
2718 new_direct_edge = NULL;
2719 else if (ici->polymorphic)
2720 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2721 new_root_info);
2722 else
2723 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2724 new_root_info);
2725 /* If speculation was removed, then we need to do nothing. */
2726 if (new_direct_edge && new_direct_edge != ie)
2728 new_direct_edge->indirect_inlining_edge = 1;
2729 top = IPA_EDGE_REF (cs);
2730 res = true;
2732 else if (new_direct_edge)
2734 new_direct_edge->indirect_inlining_edge = 1;
2735 if (new_direct_edge->call_stmt)
2736 new_direct_edge->call_stmt_cannot_inline_p
2737 = !gimple_check_call_matching_types (
2738 new_direct_edge->call_stmt,
2739 new_direct_edge->callee->symbol.decl, false);
2740 if (new_edges)
2742 new_edges->safe_push (new_direct_edge);
2743 res = true;
2745 top = IPA_EDGE_REF (cs);
2747 else if (jfunc->type == IPA_JF_PASS_THROUGH
2748 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2750 if (ici->agg_contents
2751 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2752 ici->param_index = -1;
2753 else
2754 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2756 else if (jfunc->type == IPA_JF_ANCESTOR)
2758 if (ici->agg_contents
2759 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2760 ici->param_index = -1;
2761 else
2763 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2764 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2767 else
2768 /* Either we can find a destination for this edge now or never. */
2769 ici->param_index = -1;
2772 return res;
2775 /* Recursively traverse subtree of NODE (including node) made of inlined
2776 cgraph_edges when CS has been inlined and invoke
2777 update_indirect_edges_after_inlining on all nodes and
2778 update_jump_functions_after_inlining on all non-inlined edges that lead out
2779 of this subtree. Newly discovered indirect edges will be added to
2780 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2781 created. */
2783 static bool
2784 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2785 struct cgraph_node *node,
2786 vec<cgraph_edge_p> *new_edges)
2788 struct cgraph_edge *e;
2789 bool res;
2791 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2793 for (e = node->callees; e; e = e->next_callee)
2794 if (!e->inline_failed)
2795 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2796 else
2797 update_jump_functions_after_inlining (cs, e);
2798 for (e = node->indirect_calls; e; e = e->next_callee)
2799 update_jump_functions_after_inlining (cs, e);
2801 return res;
2804 /* Combine two controlled uses counts as done during inlining. */
2806 static int
2807 combine_controlled_uses_counters (int c, int d)
2809 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
2810 return IPA_UNDESCRIBED_USE;
2811 else
2812 return c + d - 1;
2815 /* Propagate number of controlled users from CS->caleee to the new root of the
2816 tree of inlined nodes. */
2818 static void
2819 propagate_controlled_uses (struct cgraph_edge *cs)
2821 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
2822 struct cgraph_node *new_root = cs->caller->global.inlined_to
2823 ? cs->caller->global.inlined_to : cs->caller;
2824 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
2825 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
2826 int count, i;
2828 count = MIN (ipa_get_cs_argument_count (args),
2829 ipa_get_param_count (old_root_info));
2830 for (i = 0; i < count; i++)
2832 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2833 struct ipa_cst_ref_desc *rdesc;
2835 if (jf->type == IPA_JF_PASS_THROUGH)
2837 int src_idx, c, d;
2838 src_idx = ipa_get_jf_pass_through_formal_id (jf);
2839 c = ipa_get_controlled_uses (new_root_info, src_idx);
2840 d = ipa_get_controlled_uses (old_root_info, i);
2842 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
2843 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
2844 c = combine_controlled_uses_counters (c, d);
2845 ipa_set_controlled_uses (new_root_info, src_idx, c);
2846 if (c == 0 && new_root_info->ipcp_orig_node)
2848 struct cgraph_node *n;
2849 struct ipa_ref *ref;
2850 tree t = new_root_info->known_vals[src_idx];
2852 if (t && TREE_CODE (t) == ADDR_EXPR
2853 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2854 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
2855 && (ref = ipa_find_reference ((symtab_node) new_root,
2856 (symtab_node) n, NULL, 0)))
2858 if (dump_file)
2859 fprintf (dump_file, "ipa-prop: Removing cloning-created "
2860 "reference from %s/%i to %s/%i.\n",
2861 xstrdup (cgraph_node_name (new_root)),
2862 new_root->symbol.order,
2863 xstrdup (cgraph_node_name (n)), n->symbol.order);
2864 ipa_remove_reference (ref);
2868 else if (jf->type == IPA_JF_CONST
2869 && (rdesc = jfunc_rdesc_usable (jf)))
2871 int d = ipa_get_controlled_uses (old_root_info, i);
2872 int c = rdesc->refcount;
2873 rdesc->refcount = combine_controlled_uses_counters (c, d);
2874 if (rdesc->refcount == 0)
2876 tree cst = ipa_get_jf_constant (jf);
2877 struct cgraph_node *n;
2878 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
2879 && TREE_CODE (TREE_OPERAND (cst, 0))
2880 == FUNCTION_DECL);
2881 n = cgraph_get_node (TREE_OPERAND (cst, 0));
2882 if (n)
2884 struct cgraph_node *clone;
2885 bool ok;
2886 ok = remove_described_reference ((symtab_node) n, rdesc);
2887 gcc_checking_assert (ok);
2889 clone = cs->caller;
2890 while (clone->global.inlined_to
2891 && clone != rdesc->cs->caller
2892 && IPA_NODE_REF (clone)->ipcp_orig_node)
2894 struct ipa_ref *ref;
2895 ref = ipa_find_reference ((symtab_node) clone,
2896 (symtab_node) n, NULL, 0);
2897 if (ref)
2899 if (dump_file)
2900 fprintf (dump_file, "ipa-prop: Removing "
2901 "cloning-created reference "
2902 "from %s/%i to %s/%i.\n",
2903 xstrdup (cgraph_node_name (clone)),
2904 clone->symbol.order,
2905 xstrdup (cgraph_node_name (n)),
2906 n->symbol.order);
2907 ipa_remove_reference (ref);
2909 clone = clone->callers->caller;
2916 for (i = ipa_get_param_count (old_root_info);
2917 i < ipa_get_cs_argument_count (args);
2918 i++)
2920 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2922 if (jf->type == IPA_JF_CONST)
2924 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
2925 if (rdesc)
2926 rdesc->refcount = IPA_UNDESCRIBED_USE;
2928 else if (jf->type == IPA_JF_PASS_THROUGH)
2929 ipa_set_controlled_uses (new_root_info,
2930 jf->value.pass_through.formal_id,
2931 IPA_UNDESCRIBED_USE);
2935 /* Update jump functions and call note functions on inlining the call site CS.
2936 CS is expected to lead to a node already cloned by
2937 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2938 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2939 created. */
2941 bool
2942 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2943 vec<cgraph_edge_p> *new_edges)
2945 bool changed;
2946 /* Do nothing if the preparation phase has not been carried out yet
2947 (i.e. during early inlining). */
2948 if (!ipa_node_params_vector.exists ())
2949 return false;
2950 gcc_assert (ipa_edge_args_vector);
2952 propagate_controlled_uses (cs);
2953 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2955 return changed;
2958 /* Frees all dynamically allocated structures that the argument info points
2959 to. */
2961 void
2962 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2964 vec_free (args->jump_functions);
2965 memset (args, 0, sizeof (*args));
2968 /* Free all ipa_edge structures. */
2970 void
2971 ipa_free_all_edge_args (void)
2973 int i;
2974 struct ipa_edge_args *args;
2976 if (!ipa_edge_args_vector)
2977 return;
2979 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2980 ipa_free_edge_args_substructures (args);
2982 vec_free (ipa_edge_args_vector);
2985 /* Frees all dynamically allocated structures that the param info points
2986 to. */
2988 void
2989 ipa_free_node_params_substructures (struct ipa_node_params *info)
2991 info->descriptors.release ();
2992 free (info->lattices);
2993 /* Lattice values and their sources are deallocated with their alocation
2994 pool. */
2995 info->known_vals.release ();
2996 memset (info, 0, sizeof (*info));
2999 /* Free all ipa_node_params structures. */
3001 void
3002 ipa_free_all_node_params (void)
3004 int i;
3005 struct ipa_node_params *info;
3007 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
3008 ipa_free_node_params_substructures (info);
3010 ipa_node_params_vector.release ();
3013 /* Set the aggregate replacements of NODE to be AGGVALS. */
3015 void
3016 ipa_set_node_agg_value_chain (struct cgraph_node *node,
3017 struct ipa_agg_replacement_value *aggvals)
3019 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
3020 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
3022 (*ipa_node_agg_replacements)[node->uid] = aggvals;
3025 /* Hook that is called by cgraph.c when an edge is removed. */
3027 static void
3028 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
3030 struct ipa_edge_args *args;
3032 /* During IPA-CP updating we can be called on not-yet analyzed clones. */
3033 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
3034 return;
3036 args = IPA_EDGE_REF (cs);
3037 if (args->jump_functions)
3039 struct ipa_jump_func *jf;
3040 int i;
3041 FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
3043 struct ipa_cst_ref_desc *rdesc;
3044 try_decrement_rdesc_refcount (jf);
3045 if (jf->type == IPA_JF_CONST
3046 && (rdesc = ipa_get_jf_constant_rdesc (jf))
3047 && rdesc->cs == cs)
3048 rdesc->cs = NULL;
3052 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
3055 /* Hook that is called by cgraph.c when a node is removed. */
3057 static void
3058 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3060 /* During IPA-CP updating we can be called on not-yet analyze clones. */
3061 if (ipa_node_params_vector.length () > (unsigned)node->uid)
3062 ipa_free_node_params_substructures (IPA_NODE_REF (node));
3063 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
3064 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
3067 /* Hook that is called by cgraph.c when an edge is duplicated. */
3069 static void
3070 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3071 __attribute__((unused)) void *data)
3073 struct ipa_edge_args *old_args, *new_args;
3074 unsigned int i;
3076 ipa_check_create_edge_args ();
3078 old_args = IPA_EDGE_REF (src);
3079 new_args = IPA_EDGE_REF (dst);
3081 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
3083 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
3085 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
3086 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
3088 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
3090 if (src_jf->type == IPA_JF_CONST)
3092 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
3094 if (!src_rdesc)
3095 dst_jf->value.constant.rdesc = NULL;
3096 else if (src->caller == dst->caller)
3098 struct ipa_ref *ref;
3099 symtab_node n = (symtab_node) cgraph_node_for_jfunc (src_jf);
3100 gcc_checking_assert (n);
3101 ref = ipa_find_reference ((symtab_node) src->caller, n,
3102 src->call_stmt, src->lto_stmt_uid);
3103 gcc_checking_assert (ref);
3104 ipa_clone_ref (ref, (symtab_node) dst->caller, ref->stmt);
3106 gcc_checking_assert (ipa_refdesc_pool);
3107 struct ipa_cst_ref_desc *dst_rdesc
3108 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3109 dst_rdesc->cs = dst;
3110 dst_rdesc->refcount = src_rdesc->refcount;
3111 dst_rdesc->next_duplicate = NULL;
3112 dst_jf->value.constant.rdesc = dst_rdesc;
3114 else if (src_rdesc->cs == src)
3116 struct ipa_cst_ref_desc *dst_rdesc;
3117 gcc_checking_assert (ipa_refdesc_pool);
3118 dst_rdesc
3119 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3120 dst_rdesc->cs = dst;
3121 dst_rdesc->refcount = src_rdesc->refcount;
3122 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
3123 src_rdesc->next_duplicate = dst_rdesc;
3124 dst_jf->value.constant.rdesc = dst_rdesc;
3126 else
3128 struct ipa_cst_ref_desc *dst_rdesc;
3129 /* This can happen during inlining, when a JFUNC can refer to a
3130 reference taken in a function up in the tree of inline clones.
3131 We need to find the duplicate that refers to our tree of
3132 inline clones. */
3134 gcc_assert (dst->caller->global.inlined_to);
3135 for (dst_rdesc = src_rdesc->next_duplicate;
3136 dst_rdesc;
3137 dst_rdesc = dst_rdesc->next_duplicate)
3139 struct cgraph_node *top;
3140 top = dst_rdesc->cs->caller->global.inlined_to
3141 ? dst_rdesc->cs->caller->global.inlined_to
3142 : dst_rdesc->cs->caller;
3143 if (dst->caller->global.inlined_to == top)
3144 break;
3146 gcc_assert (dst_rdesc);
3147 dst_jf->value.constant.rdesc = dst_rdesc;
3153 /* Hook that is called by cgraph.c when a node is duplicated. */
3155 static void
3156 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
3157 ATTRIBUTE_UNUSED void *data)
3159 struct ipa_node_params *old_info, *new_info;
3160 struct ipa_agg_replacement_value *old_av, *new_av;
3162 ipa_check_create_node_params ();
3163 old_info = IPA_NODE_REF (src);
3164 new_info = IPA_NODE_REF (dst);
3166 new_info->descriptors = old_info->descriptors.copy ();
3167 new_info->lattices = NULL;
3168 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
3170 new_info->uses_analysis_done = old_info->uses_analysis_done;
3171 new_info->node_enqueued = old_info->node_enqueued;
3173 old_av = ipa_get_agg_replacements_for_node (src);
3174 if (!old_av)
3175 return;
3177 new_av = NULL;
3178 while (old_av)
3180 struct ipa_agg_replacement_value *v;
3182 v = ggc_alloc_ipa_agg_replacement_value ();
3183 memcpy (v, old_av, sizeof (*v));
3184 v->next = new_av;
3185 new_av = v;
3186 old_av = old_av->next;
3188 ipa_set_node_agg_value_chain (dst, new_av);
3192 /* Analyze newly added function into callgraph. */
3194 static void
3195 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3197 ipa_analyze_node (node);
3200 /* Register our cgraph hooks if they are not already there. */
3202 void
3203 ipa_register_cgraph_hooks (void)
3205 if (!edge_removal_hook_holder)
3206 edge_removal_hook_holder =
3207 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
3208 if (!node_removal_hook_holder)
3209 node_removal_hook_holder =
3210 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
3211 if (!edge_duplication_hook_holder)
3212 edge_duplication_hook_holder =
3213 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
3214 if (!node_duplication_hook_holder)
3215 node_duplication_hook_holder =
3216 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
3217 function_insertion_hook_holder =
3218 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
3221 /* Unregister our cgraph hooks if they are not already there. */
3223 static void
3224 ipa_unregister_cgraph_hooks (void)
3226 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3227 edge_removal_hook_holder = NULL;
3228 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3229 node_removal_hook_holder = NULL;
3230 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3231 edge_duplication_hook_holder = NULL;
3232 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3233 node_duplication_hook_holder = NULL;
3234 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3235 function_insertion_hook_holder = NULL;
3238 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3239 longer needed after ipa-cp. */
3241 void
3242 ipa_free_all_structures_after_ipa_cp (void)
3244 if (!optimize)
3246 ipa_free_all_edge_args ();
3247 ipa_free_all_node_params ();
3248 free_alloc_pool (ipcp_sources_pool);
3249 free_alloc_pool (ipcp_values_pool);
3250 free_alloc_pool (ipcp_agg_lattice_pool);
3251 ipa_unregister_cgraph_hooks ();
3252 if (ipa_refdesc_pool)
3253 free_alloc_pool (ipa_refdesc_pool);
3257 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3258 longer needed after indirect inlining. */
3260 void
3261 ipa_free_all_structures_after_iinln (void)
3263 ipa_free_all_edge_args ();
3264 ipa_free_all_node_params ();
3265 ipa_unregister_cgraph_hooks ();
3266 if (ipcp_sources_pool)
3267 free_alloc_pool (ipcp_sources_pool);
3268 if (ipcp_values_pool)
3269 free_alloc_pool (ipcp_values_pool);
3270 if (ipcp_agg_lattice_pool)
3271 free_alloc_pool (ipcp_agg_lattice_pool);
3272 if (ipa_refdesc_pool)
3273 free_alloc_pool (ipa_refdesc_pool);
3276 /* Print ipa_tree_map data structures of all functions in the
3277 callgraph to F. */
3279 void
3280 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3282 int i, count;
3283 struct ipa_node_params *info;
3285 if (!node->symbol.definition)
3286 return;
3287 info = IPA_NODE_REF (node);
3288 fprintf (f, " function %s/%i parameter descriptors:\n",
3289 cgraph_node_name (node), node->symbol.order);
3290 count = ipa_get_param_count (info);
3291 for (i = 0; i < count; i++)
3293 int c;
3295 ipa_dump_param (f, info, i);
3296 if (ipa_is_param_used (info, i))
3297 fprintf (f, " used");
3298 c = ipa_get_controlled_uses (info, i);
3299 if (c == IPA_UNDESCRIBED_USE)
3300 fprintf (f, " undescribed_use");
3301 else
3302 fprintf (f, " controlled_uses=%i", c);
3303 fprintf (f, "\n");
3307 /* Print ipa_tree_map data structures of all functions in the
3308 callgraph to F. */
3310 void
3311 ipa_print_all_params (FILE * f)
3313 struct cgraph_node *node;
3315 fprintf (f, "\nFunction parameters:\n");
3316 FOR_EACH_FUNCTION (node)
3317 ipa_print_node_params (f, node);
3320 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3322 vec<tree>
3323 ipa_get_vector_of_formal_parms (tree fndecl)
3325 vec<tree> args;
3326 int count;
3327 tree parm;
3329 gcc_assert (!flag_wpa);
3330 count = count_formal_params (fndecl);
3331 args.create (count);
3332 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3333 args.quick_push (parm);
3335 return args;
3338 /* Return a heap allocated vector containing types of formal parameters of
3339 function type FNTYPE. */
3341 static inline vec<tree>
3342 get_vector_of_formal_parm_types (tree fntype)
3344 vec<tree> types;
3345 int count = 0;
3346 tree t;
3348 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3349 count++;
3351 types.create (count);
3352 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3353 types.quick_push (TREE_VALUE (t));
3355 return types;
3358 /* Modify the function declaration FNDECL and its type according to the plan in
3359 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3360 to reflect the actual parameters being modified which are determined by the
3361 base_index field. */
3363 void
3364 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
3365 const char *synth_parm_prefix)
3367 vec<tree> oparms, otypes;
3368 tree orig_type, new_type = NULL;
3369 tree old_arg_types, t, new_arg_types = NULL;
3370 tree parm, *link = &DECL_ARGUMENTS (fndecl);
3371 int i, len = adjustments.length ();
3372 tree new_reversed = NULL;
3373 bool care_for_types, last_parm_void;
3375 if (!synth_parm_prefix)
3376 synth_parm_prefix = "SYNTH";
3378 oparms = ipa_get_vector_of_formal_parms (fndecl);
3379 orig_type = TREE_TYPE (fndecl);
3380 old_arg_types = TYPE_ARG_TYPES (orig_type);
3382 /* The following test is an ugly hack, some functions simply don't have any
3383 arguments in their type. This is probably a bug but well... */
3384 care_for_types = (old_arg_types != NULL_TREE);
3385 if (care_for_types)
3387 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3388 == void_type_node);
3389 otypes = get_vector_of_formal_parm_types (orig_type);
3390 if (last_parm_void)
3391 gcc_assert (oparms.length () + 1 == otypes.length ());
3392 else
3393 gcc_assert (oparms.length () == otypes.length ());
3395 else
3397 last_parm_void = false;
3398 otypes.create (0);
3401 for (i = 0; i < len; i++)
3403 struct ipa_parm_adjustment *adj;
3404 gcc_assert (link);
3406 adj = &adjustments[i];
3407 parm = oparms[adj->base_index];
3408 adj->base = parm;
3410 if (adj->copy_param)
3412 if (care_for_types)
3413 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3414 new_arg_types);
3415 *link = parm;
3416 link = &DECL_CHAIN (parm);
3418 else if (!adj->remove_param)
3420 tree new_parm;
3421 tree ptype;
3423 if (adj->by_ref)
3424 ptype = build_pointer_type (adj->type);
3425 else
3426 ptype = adj->type;
3428 if (care_for_types)
3429 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3431 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3432 ptype);
3433 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
3435 DECL_ARTIFICIAL (new_parm) = 1;
3436 DECL_ARG_TYPE (new_parm) = ptype;
3437 DECL_CONTEXT (new_parm) = fndecl;
3438 TREE_USED (new_parm) = 1;
3439 DECL_IGNORED_P (new_parm) = 1;
3440 layout_decl (new_parm, 0);
3442 adj->base = parm;
3443 adj->reduction = new_parm;
3445 *link = new_parm;
3447 link = &DECL_CHAIN (new_parm);
3451 *link = NULL_TREE;
3453 if (care_for_types)
3455 new_reversed = nreverse (new_arg_types);
3456 if (last_parm_void)
3458 if (new_reversed)
3459 TREE_CHAIN (new_arg_types) = void_list_node;
3460 else
3461 new_reversed = void_list_node;
3465 /* Use copy_node to preserve as much as possible from original type
3466 (debug info, attribute lists etc.)
3467 Exception is METHOD_TYPEs must have THIS argument.
3468 When we are asked to remove it, we need to build new FUNCTION_TYPE
3469 instead. */
3470 if (TREE_CODE (orig_type) != METHOD_TYPE
3471 || (adjustments[0].copy_param
3472 && adjustments[0].base_index == 0))
3474 new_type = build_distinct_type_copy (orig_type);
3475 TYPE_ARG_TYPES (new_type) = new_reversed;
3477 else
3479 new_type
3480 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3481 new_reversed));
3482 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3483 DECL_VINDEX (fndecl) = NULL_TREE;
3486 /* When signature changes, we need to clear builtin info. */
3487 if (DECL_BUILT_IN (fndecl))
3489 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3490 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3493 /* This is a new type, not a copy of an old type. Need to reassociate
3494 variants. We can handle everything except the main variant lazily. */
3495 t = TYPE_MAIN_VARIANT (orig_type);
3496 if (orig_type != t)
3498 TYPE_MAIN_VARIANT (new_type) = t;
3499 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3500 TYPE_NEXT_VARIANT (t) = new_type;
3502 else
3504 TYPE_MAIN_VARIANT (new_type) = new_type;
3505 TYPE_NEXT_VARIANT (new_type) = NULL;
3508 TREE_TYPE (fndecl) = new_type;
3509 DECL_VIRTUAL_P (fndecl) = 0;
3510 otypes.release ();
3511 oparms.release ();
3514 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3515 If this is a directly recursive call, CS must be NULL. Otherwise it must
3516 contain the corresponding call graph edge. */
3518 void
3519 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3520 ipa_parm_adjustment_vec adjustments)
3522 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3523 vec<tree> vargs;
3524 vec<tree, va_gc> **debug_args = NULL;
3525 gimple new_stmt;
3526 gimple_stmt_iterator gsi, prev_gsi;
3527 tree callee_decl;
3528 int i, len;
3530 len = adjustments.length ();
3531 vargs.create (len);
3532 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->symbol.decl;
3533 ipa_remove_stmt_references ((symtab_node) current_node, stmt);
3535 gsi = gsi_for_stmt (stmt);
3536 prev_gsi = gsi;
3537 gsi_prev (&prev_gsi);
3538 for (i = 0; i < len; i++)
3540 struct ipa_parm_adjustment *adj;
3542 adj = &adjustments[i];
3544 if (adj->copy_param)
3546 tree arg = gimple_call_arg (stmt, adj->base_index);
3548 vargs.quick_push (arg);
3550 else if (!adj->remove_param)
3552 tree expr, base, off;
3553 location_t loc;
3554 unsigned int deref_align = 0;
3555 bool deref_base = false;
3557 /* We create a new parameter out of the value of the old one, we can
3558 do the following kind of transformations:
3560 - A scalar passed by reference is converted to a scalar passed by
3561 value. (adj->by_ref is false and the type of the original
3562 actual argument is a pointer to a scalar).
3564 - A part of an aggregate is passed instead of the whole aggregate.
3565 The part can be passed either by value or by reference, this is
3566 determined by value of adj->by_ref. Moreover, the code below
3567 handles both situations when the original aggregate is passed by
3568 value (its type is not a pointer) and when it is passed by
3569 reference (it is a pointer to an aggregate).
3571 When the new argument is passed by reference (adj->by_ref is true)
3572 it must be a part of an aggregate and therefore we form it by
3573 simply taking the address of a reference inside the original
3574 aggregate. */
3576 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3577 base = gimple_call_arg (stmt, adj->base_index);
3578 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3579 : EXPR_LOCATION (base);
3581 if (TREE_CODE (base) != ADDR_EXPR
3582 && POINTER_TYPE_P (TREE_TYPE (base)))
3583 off = build_int_cst (adj->alias_ptr_type,
3584 adj->offset / BITS_PER_UNIT);
3585 else
3587 HOST_WIDE_INT base_offset;
3588 tree prev_base;
3589 bool addrof;
3591 if (TREE_CODE (base) == ADDR_EXPR)
3593 base = TREE_OPERAND (base, 0);
3594 addrof = true;
3596 else
3597 addrof = false;
3598 prev_base = base;
3599 base = get_addr_base_and_unit_offset (base, &base_offset);
3600 /* Aggregate arguments can have non-invariant addresses. */
3601 if (!base)
3603 base = build_fold_addr_expr (prev_base);
3604 off = build_int_cst (adj->alias_ptr_type,
3605 adj->offset / BITS_PER_UNIT);
3607 else if (TREE_CODE (base) == MEM_REF)
3609 if (!addrof)
3611 deref_base = true;
3612 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3614 off = build_int_cst (adj->alias_ptr_type,
3615 base_offset
3616 + adj->offset / BITS_PER_UNIT);
3617 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3618 off);
3619 base = TREE_OPERAND (base, 0);
3621 else
3623 off = build_int_cst (adj->alias_ptr_type,
3624 base_offset
3625 + adj->offset / BITS_PER_UNIT);
3626 base = build_fold_addr_expr (base);
3630 if (!adj->by_ref)
3632 tree type = adj->type;
3633 unsigned int align;
3634 unsigned HOST_WIDE_INT misalign;
3636 if (deref_base)
3638 align = deref_align;
3639 misalign = 0;
3641 else
3643 get_pointer_alignment_1 (base, &align, &misalign);
3644 if (TYPE_ALIGN (type) > align)
3645 align = TYPE_ALIGN (type);
3647 misalign += (tree_to_double_int (off)
3648 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3649 * BITS_PER_UNIT);
3650 misalign = misalign & (align - 1);
3651 if (misalign != 0)
3652 align = (misalign & -misalign);
3653 if (align < TYPE_ALIGN (type))
3654 type = build_aligned_type (type, align);
3655 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3657 else
3659 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3660 expr = build_fold_addr_expr (expr);
3663 expr = force_gimple_operand_gsi (&gsi, expr,
3664 adj->by_ref
3665 || is_gimple_reg_type (adj->type),
3666 NULL, true, GSI_SAME_STMT);
3667 vargs.quick_push (expr);
3669 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3671 unsigned int ix;
3672 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3673 gimple def_temp;
3675 arg = gimple_call_arg (stmt, adj->base_index);
3676 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3678 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3679 continue;
3680 arg = fold_convert_loc (gimple_location (stmt),
3681 TREE_TYPE (origin), arg);
3683 if (debug_args == NULL)
3684 debug_args = decl_debug_args_insert (callee_decl);
3685 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3686 if (ddecl == origin)
3688 ddecl = (**debug_args)[ix + 1];
3689 break;
3691 if (ddecl == NULL)
3693 ddecl = make_node (DEBUG_EXPR_DECL);
3694 DECL_ARTIFICIAL (ddecl) = 1;
3695 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3696 DECL_MODE (ddecl) = DECL_MODE (origin);
3698 vec_safe_push (*debug_args, origin);
3699 vec_safe_push (*debug_args, ddecl);
3701 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3702 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3706 if (dump_file && (dump_flags & TDF_DETAILS))
3708 fprintf (dump_file, "replacing stmt:");
3709 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3712 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3713 vargs.release ();
3714 if (gimple_call_lhs (stmt))
3715 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3717 gimple_set_block (new_stmt, gimple_block (stmt));
3718 if (gimple_has_location (stmt))
3719 gimple_set_location (new_stmt, gimple_location (stmt));
3720 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3721 gimple_call_copy_flags (new_stmt, stmt);
3723 if (dump_file && (dump_flags & TDF_DETAILS))
3725 fprintf (dump_file, "with stmt:");
3726 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3727 fprintf (dump_file, "\n");
3729 gsi_replace (&gsi, new_stmt, true);
3730 if (cs)
3731 cgraph_set_call_stmt (cs, new_stmt);
3734 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
3735 gsi_prev (&gsi);
3737 while ((gsi_end_p (prev_gsi) && !gsi_end_p (gsi))
3738 || (!gsi_end_p (prev_gsi) && gsi_stmt (gsi) == gsi_stmt (prev_gsi)));
3740 update_ssa (TODO_update_ssa);
3741 free_dominance_info (CDI_DOMINATORS);
3744 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3746 static bool
3747 index_in_adjustments_multiple_times_p (int base_index,
3748 ipa_parm_adjustment_vec adjustments)
3750 int i, len = adjustments.length ();
3751 bool one = false;
3753 for (i = 0; i < len; i++)
3755 struct ipa_parm_adjustment *adj;
3756 adj = &adjustments[i];
3758 if (adj->base_index == base_index)
3760 if (one)
3761 return true;
3762 else
3763 one = true;
3766 return false;
3770 /* Return adjustments that should have the same effect on function parameters
3771 and call arguments as if they were first changed according to adjustments in
3772 INNER and then by adjustments in OUTER. */
3774 ipa_parm_adjustment_vec
3775 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3776 ipa_parm_adjustment_vec outer)
3778 int i, outlen = outer.length ();
3779 int inlen = inner.length ();
3780 int removals = 0;
3781 ipa_parm_adjustment_vec adjustments, tmp;
3783 tmp.create (inlen);
3784 for (i = 0; i < inlen; i++)
3786 struct ipa_parm_adjustment *n;
3787 n = &inner[i];
3789 if (n->remove_param)
3790 removals++;
3791 else
3792 tmp.quick_push (*n);
3795 adjustments.create (outlen + removals);
3796 for (i = 0; i < outlen; i++)
3798 struct ipa_parm_adjustment r;
3799 struct ipa_parm_adjustment *out = &outer[i];
3800 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3802 memset (&r, 0, sizeof (r));
3803 gcc_assert (!in->remove_param);
3804 if (out->remove_param)
3806 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3808 r.remove_param = true;
3809 adjustments.quick_push (r);
3811 continue;
3814 r.base_index = in->base_index;
3815 r.type = out->type;
3817 /* FIXME: Create nonlocal value too. */
3819 if (in->copy_param && out->copy_param)
3820 r.copy_param = true;
3821 else if (in->copy_param)
3822 r.offset = out->offset;
3823 else if (out->copy_param)
3824 r.offset = in->offset;
3825 else
3826 r.offset = in->offset + out->offset;
3827 adjustments.quick_push (r);
3830 for (i = 0; i < inlen; i++)
3832 struct ipa_parm_adjustment *n = &inner[i];
3834 if (n->remove_param)
3835 adjustments.quick_push (*n);
3838 tmp.release ();
3839 return adjustments;
3842 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3843 friendly way, assuming they are meant to be applied to FNDECL. */
3845 void
3846 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3847 tree fndecl)
3849 int i, len = adjustments.length ();
3850 bool first = true;
3851 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3853 fprintf (file, "IPA param adjustments: ");
3854 for (i = 0; i < len; i++)
3856 struct ipa_parm_adjustment *adj;
3857 adj = &adjustments[i];
3859 if (!first)
3860 fprintf (file, " ");
3861 else
3862 first = false;
3864 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3865 print_generic_expr (file, parms[adj->base_index], 0);
3866 if (adj->base)
3868 fprintf (file, ", base: ");
3869 print_generic_expr (file, adj->base, 0);
3871 if (adj->reduction)
3873 fprintf (file, ", reduction: ");
3874 print_generic_expr (file, adj->reduction, 0);
3876 if (adj->new_ssa_base)
3878 fprintf (file, ", new_ssa_base: ");
3879 print_generic_expr (file, adj->new_ssa_base, 0);
3882 if (adj->copy_param)
3883 fprintf (file, ", copy_param");
3884 else if (adj->remove_param)
3885 fprintf (file, ", remove_param");
3886 else
3887 fprintf (file, ", offset %li", (long) adj->offset);
3888 if (adj->by_ref)
3889 fprintf (file, ", by_ref");
3890 print_node_brief (file, ", type: ", adj->type, 0);
3891 fprintf (file, "\n");
3893 parms.release ();
3896 /* Dump the AV linked list. */
3898 void
3899 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3901 bool comma = false;
3902 fprintf (f, " Aggregate replacements:");
3903 for (; av; av = av->next)
3905 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3906 av->index, av->offset);
3907 print_generic_expr (f, av->value, 0);
3908 comma = true;
3910 fprintf (f, "\n");
3913 /* Stream out jump function JUMP_FUNC to OB. */
3915 static void
3916 ipa_write_jump_function (struct output_block *ob,
3917 struct ipa_jump_func *jump_func)
3919 struct ipa_agg_jf_item *item;
3920 struct bitpack_d bp;
3921 int i, count;
3923 streamer_write_uhwi (ob, jump_func->type);
3924 switch (jump_func->type)
3926 case IPA_JF_UNKNOWN:
3927 break;
3928 case IPA_JF_KNOWN_TYPE:
3929 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3930 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3931 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3932 break;
3933 case IPA_JF_CONST:
3934 gcc_assert (
3935 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
3936 stream_write_tree (ob, jump_func->value.constant.value, true);
3937 break;
3938 case IPA_JF_PASS_THROUGH:
3939 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3940 if (jump_func->value.pass_through.operation == NOP_EXPR)
3942 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3943 bp = bitpack_create (ob->main_stream);
3944 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3945 bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1);
3946 streamer_write_bitpack (&bp);
3948 else
3950 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3951 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3953 break;
3954 case IPA_JF_ANCESTOR:
3955 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3956 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3957 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3958 bp = bitpack_create (ob->main_stream);
3959 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3960 bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1);
3961 streamer_write_bitpack (&bp);
3962 break;
3965 count = vec_safe_length (jump_func->agg.items);
3966 streamer_write_uhwi (ob, count);
3967 if (count)
3969 bp = bitpack_create (ob->main_stream);
3970 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3971 streamer_write_bitpack (&bp);
3974 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3976 streamer_write_uhwi (ob, item->offset);
3977 stream_write_tree (ob, item->value, true);
3981 /* Read in jump function JUMP_FUNC from IB. */
3983 static void
3984 ipa_read_jump_function (struct lto_input_block *ib,
3985 struct ipa_jump_func *jump_func,
3986 struct cgraph_edge *cs,
3987 struct data_in *data_in)
3989 enum jump_func_type jftype;
3990 enum tree_code operation;
3991 int i, count;
3993 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
3994 switch (jftype)
3996 case IPA_JF_UNKNOWN:
3997 jump_func->type = IPA_JF_UNKNOWN;
3998 break;
3999 case IPA_JF_KNOWN_TYPE:
4001 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4002 tree base_type = stream_read_tree (ib, data_in);
4003 tree component_type = stream_read_tree (ib, data_in);
4005 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
4006 break;
4008 case IPA_JF_CONST:
4009 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
4010 break;
4011 case IPA_JF_PASS_THROUGH:
4012 operation = (enum tree_code) streamer_read_uhwi (ib);
4013 if (operation == NOP_EXPR)
4015 int formal_id = streamer_read_uhwi (ib);
4016 struct bitpack_d bp = streamer_read_bitpack (ib);
4017 bool agg_preserved = bp_unpack_value (&bp, 1);
4018 bool type_preserved = bp_unpack_value (&bp, 1);
4019 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved,
4020 type_preserved);
4022 else
4024 tree operand = stream_read_tree (ib, data_in);
4025 int formal_id = streamer_read_uhwi (ib);
4026 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
4027 operation);
4029 break;
4030 case IPA_JF_ANCESTOR:
4032 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4033 tree type = stream_read_tree (ib, data_in);
4034 int formal_id = streamer_read_uhwi (ib);
4035 struct bitpack_d bp = streamer_read_bitpack (ib);
4036 bool agg_preserved = bp_unpack_value (&bp, 1);
4037 bool type_preserved = bp_unpack_value (&bp, 1);
4039 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved,
4040 type_preserved);
4041 break;
4045 count = streamer_read_uhwi (ib);
4046 vec_alloc (jump_func->agg.items, count);
4047 if (count)
4049 struct bitpack_d bp = streamer_read_bitpack (ib);
4050 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
4052 for (i = 0; i < count; i++)
4054 struct ipa_agg_jf_item item;
4055 item.offset = streamer_read_uhwi (ib);
4056 item.value = stream_read_tree (ib, data_in);
4057 jump_func->agg.items->quick_push (item);
4061 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
4062 relevant to indirect inlining to OB. */
4064 static void
4065 ipa_write_indirect_edge_info (struct output_block *ob,
4066 struct cgraph_edge *cs)
4068 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4069 struct bitpack_d bp;
4071 streamer_write_hwi (ob, ii->param_index);
4072 streamer_write_hwi (ob, ii->offset);
4073 bp = bitpack_create (ob->main_stream);
4074 bp_pack_value (&bp, ii->polymorphic, 1);
4075 bp_pack_value (&bp, ii->agg_contents, 1);
4076 bp_pack_value (&bp, ii->member_ptr, 1);
4077 bp_pack_value (&bp, ii->by_ref, 1);
4078 streamer_write_bitpack (&bp);
4080 if (ii->polymorphic)
4082 streamer_write_hwi (ob, ii->otr_token);
4083 stream_write_tree (ob, ii->otr_type, true);
4087 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
4088 relevant to indirect inlining from IB. */
4090 static void
4091 ipa_read_indirect_edge_info (struct lto_input_block *ib,
4092 struct data_in *data_in ATTRIBUTE_UNUSED,
4093 struct cgraph_edge *cs)
4095 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4096 struct bitpack_d bp;
4098 ii->param_index = (int) streamer_read_hwi (ib);
4099 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
4100 bp = streamer_read_bitpack (ib);
4101 ii->polymorphic = bp_unpack_value (&bp, 1);
4102 ii->agg_contents = bp_unpack_value (&bp, 1);
4103 ii->member_ptr = bp_unpack_value (&bp, 1);
4104 ii->by_ref = bp_unpack_value (&bp, 1);
4105 if (ii->polymorphic)
4107 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
4108 ii->otr_type = stream_read_tree (ib, data_in);
4112 /* Stream out NODE info to OB. */
4114 static void
4115 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
4117 int node_ref;
4118 lto_symtab_encoder_t encoder;
4119 struct ipa_node_params *info = IPA_NODE_REF (node);
4120 int j;
4121 struct cgraph_edge *e;
4122 struct bitpack_d bp;
4124 encoder = ob->decl_state->symtab_node_encoder;
4125 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
4126 streamer_write_uhwi (ob, node_ref);
4128 streamer_write_uhwi (ob, ipa_get_param_count (info));
4129 for (j = 0; j < ipa_get_param_count (info); j++)
4130 streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
4131 bp = bitpack_create (ob->main_stream);
4132 gcc_assert (info->uses_analysis_done
4133 || ipa_get_param_count (info) == 0);
4134 gcc_assert (!info->node_enqueued);
4135 gcc_assert (!info->ipcp_orig_node);
4136 for (j = 0; j < ipa_get_param_count (info); j++)
4137 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
4138 streamer_write_bitpack (&bp);
4139 for (j = 0; j < ipa_get_param_count (info); j++)
4140 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
4141 for (e = node->callees; e; e = e->next_callee)
4143 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4145 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4146 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4147 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4149 for (e = node->indirect_calls; e; e = e->next_callee)
4151 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4153 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4154 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4155 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4156 ipa_write_indirect_edge_info (ob, e);
4160 /* Stream in NODE info from IB. */
4162 static void
4163 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
4164 struct data_in *data_in)
4166 struct ipa_node_params *info = IPA_NODE_REF (node);
4167 int k;
4168 struct cgraph_edge *e;
4169 struct bitpack_d bp;
4171 ipa_alloc_node_params (node, streamer_read_uhwi (ib));
4173 for (k = 0; k < ipa_get_param_count (info); k++)
4174 info->descriptors[k].move_cost = streamer_read_uhwi (ib);
4176 bp = streamer_read_bitpack (ib);
4177 if (ipa_get_param_count (info) != 0)
4178 info->uses_analysis_done = true;
4179 info->node_enqueued = false;
4180 for (k = 0; k < ipa_get_param_count (info); k++)
4181 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
4182 for (k = 0; k < ipa_get_param_count (info); k++)
4183 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
4184 for (e = node->callees; e; e = e->next_callee)
4186 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4187 int count = streamer_read_uhwi (ib);
4189 if (!count)
4190 continue;
4191 vec_safe_grow_cleared (args->jump_functions, count);
4193 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4194 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4195 data_in);
4197 for (e = node->indirect_calls; e; e = e->next_callee)
4199 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4200 int count = streamer_read_uhwi (ib);
4202 if (count)
4204 vec_safe_grow_cleared (args->jump_functions, count);
4205 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4206 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4207 data_in);
4209 ipa_read_indirect_edge_info (ib, data_in, e);
4213 /* Write jump functions for nodes in SET. */
4215 void
4216 ipa_prop_write_jump_functions (void)
4218 struct cgraph_node *node;
4219 struct output_block *ob;
4220 unsigned int count = 0;
4221 lto_symtab_encoder_iterator lsei;
4222 lto_symtab_encoder_t encoder;
4225 if (!ipa_node_params_vector.exists ())
4226 return;
4228 ob = create_output_block (LTO_section_jump_functions);
4229 encoder = ob->decl_state->symtab_node_encoder;
4230 ob->cgraph_node = NULL;
4231 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4232 lsei_next_function_in_partition (&lsei))
4234 node = lsei_cgraph_node (lsei);
4235 if (cgraph_function_with_gimple_body_p (node)
4236 && IPA_NODE_REF (node) != NULL)
4237 count++;
4240 streamer_write_uhwi (ob, count);
4242 /* Process all of the functions. */
4243 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4244 lsei_next_function_in_partition (&lsei))
4246 node = lsei_cgraph_node (lsei);
4247 if (cgraph_function_with_gimple_body_p (node)
4248 && IPA_NODE_REF (node) != NULL)
4249 ipa_write_node_info (ob, node);
4251 streamer_write_char_stream (ob->main_stream, 0);
4252 produce_asm (ob, NULL);
4253 destroy_output_block (ob);
4256 /* Read section in file FILE_DATA of length LEN with data DATA. */
4258 static void
4259 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
4260 size_t len)
4262 const struct lto_function_header *header =
4263 (const struct lto_function_header *) data;
4264 const int cfg_offset = sizeof (struct lto_function_header);
4265 const int main_offset = cfg_offset + header->cfg_size;
4266 const int string_offset = main_offset + header->main_size;
4267 struct data_in *data_in;
4268 struct lto_input_block ib_main;
4269 unsigned int i;
4270 unsigned int count;
4272 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4273 header->main_size);
4275 data_in =
4276 lto_data_in_create (file_data, (const char *) data + string_offset,
4277 header->string_size, vNULL);
4278 count = streamer_read_uhwi (&ib_main);
4280 for (i = 0; i < count; i++)
4282 unsigned int index;
4283 struct cgraph_node *node;
4284 lto_symtab_encoder_t encoder;
4286 index = streamer_read_uhwi (&ib_main);
4287 encoder = file_data->symtab_node_encoder;
4288 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4289 gcc_assert (node->symbol.definition);
4290 ipa_read_node_info (&ib_main, node, data_in);
4292 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4293 len);
4294 lto_data_in_delete (data_in);
4297 /* Read ipcp jump functions. */
4299 void
4300 ipa_prop_read_jump_functions (void)
4302 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4303 struct lto_file_decl_data *file_data;
4304 unsigned int j = 0;
4306 ipa_check_create_node_params ();
4307 ipa_check_create_edge_args ();
4308 ipa_register_cgraph_hooks ();
4310 while ((file_data = file_data_vec[j++]))
4312 size_t len;
4313 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4315 if (data)
4316 ipa_prop_read_section (file_data, data, len);
4320 /* After merging units, we can get mismatch in argument counts.
4321 Also decl merging might've rendered parameter lists obsolete.
4322 Also compute called_with_variable_arg info. */
4324 void
4325 ipa_update_after_lto_read (void)
4327 ipa_check_create_node_params ();
4328 ipa_check_create_edge_args ();
4331 void
4332 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4334 int node_ref;
4335 unsigned int count = 0;
4336 lto_symtab_encoder_t encoder;
4337 struct ipa_agg_replacement_value *aggvals, *av;
4339 aggvals = ipa_get_agg_replacements_for_node (node);
4340 encoder = ob->decl_state->symtab_node_encoder;
4341 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
4342 streamer_write_uhwi (ob, node_ref);
4344 for (av = aggvals; av; av = av->next)
4345 count++;
4346 streamer_write_uhwi (ob, count);
4348 for (av = aggvals; av; av = av->next)
4350 struct bitpack_d bp;
4352 streamer_write_uhwi (ob, av->offset);
4353 streamer_write_uhwi (ob, av->index);
4354 stream_write_tree (ob, av->value, true);
4356 bp = bitpack_create (ob->main_stream);
4357 bp_pack_value (&bp, av->by_ref, 1);
4358 streamer_write_bitpack (&bp);
4362 /* Stream in the aggregate value replacement chain for NODE from IB. */
4364 static void
4365 read_agg_replacement_chain (struct lto_input_block *ib,
4366 struct cgraph_node *node,
4367 struct data_in *data_in)
4369 struct ipa_agg_replacement_value *aggvals = NULL;
4370 unsigned int count, i;
4372 count = streamer_read_uhwi (ib);
4373 for (i = 0; i <count; i++)
4375 struct ipa_agg_replacement_value *av;
4376 struct bitpack_d bp;
4378 av = ggc_alloc_ipa_agg_replacement_value ();
4379 av->offset = streamer_read_uhwi (ib);
4380 av->index = streamer_read_uhwi (ib);
4381 av->value = stream_read_tree (ib, data_in);
4382 bp = streamer_read_bitpack (ib);
4383 av->by_ref = bp_unpack_value (&bp, 1);
4384 av->next = aggvals;
4385 aggvals = av;
4387 ipa_set_node_agg_value_chain (node, aggvals);
4390 /* Write all aggregate replacement for nodes in set. */
4392 void
4393 ipa_prop_write_all_agg_replacement (void)
4395 struct cgraph_node *node;
4396 struct output_block *ob;
4397 unsigned int count = 0;
4398 lto_symtab_encoder_iterator lsei;
4399 lto_symtab_encoder_t encoder;
4401 if (!ipa_node_agg_replacements)
4402 return;
4404 ob = create_output_block (LTO_section_ipcp_transform);
4405 encoder = ob->decl_state->symtab_node_encoder;
4406 ob->cgraph_node = NULL;
4407 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4408 lsei_next_function_in_partition (&lsei))
4410 node = lsei_cgraph_node (lsei);
4411 if (cgraph_function_with_gimple_body_p (node)
4412 && ipa_get_agg_replacements_for_node (node) != NULL)
4413 count++;
4416 streamer_write_uhwi (ob, count);
4418 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4419 lsei_next_function_in_partition (&lsei))
4421 node = lsei_cgraph_node (lsei);
4422 if (cgraph_function_with_gimple_body_p (node)
4423 && ipa_get_agg_replacements_for_node (node) != NULL)
4424 write_agg_replacement_chain (ob, node);
4426 streamer_write_char_stream (ob->main_stream, 0);
4427 produce_asm (ob, NULL);
4428 destroy_output_block (ob);
4431 /* Read replacements section in file FILE_DATA of length LEN with data
4432 DATA. */
4434 static void
4435 read_replacements_section (struct lto_file_decl_data *file_data,
4436 const char *data,
4437 size_t len)
4439 const struct lto_function_header *header =
4440 (const struct lto_function_header *) data;
4441 const int cfg_offset = sizeof (struct lto_function_header);
4442 const int main_offset = cfg_offset + header->cfg_size;
4443 const int string_offset = main_offset + header->main_size;
4444 struct data_in *data_in;
4445 struct lto_input_block ib_main;
4446 unsigned int i;
4447 unsigned int count;
4449 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4450 header->main_size);
4452 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4453 header->string_size, vNULL);
4454 count = streamer_read_uhwi (&ib_main);
4456 for (i = 0; i < count; i++)
4458 unsigned int index;
4459 struct cgraph_node *node;
4460 lto_symtab_encoder_t encoder;
4462 index = streamer_read_uhwi (&ib_main);
4463 encoder = file_data->symtab_node_encoder;
4464 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4465 gcc_assert (node->symbol.definition);
4466 read_agg_replacement_chain (&ib_main, node, data_in);
4468 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4469 len);
4470 lto_data_in_delete (data_in);
4473 /* Read IPA-CP aggregate replacements. */
4475 void
4476 ipa_prop_read_all_agg_replacement (void)
4478 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4479 struct lto_file_decl_data *file_data;
4480 unsigned int j = 0;
4482 while ((file_data = file_data_vec[j++]))
4484 size_t len;
4485 const char *data = lto_get_section_data (file_data,
4486 LTO_section_ipcp_transform,
4487 NULL, &len);
4488 if (data)
4489 read_replacements_section (file_data, data, len);
4493 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4494 NODE. */
4496 static void
4497 adjust_agg_replacement_values (struct cgraph_node *node,
4498 struct ipa_agg_replacement_value *aggval)
4500 struct ipa_agg_replacement_value *v;
4501 int i, c = 0, d = 0, *adj;
4503 if (!node->clone.combined_args_to_skip)
4504 return;
4506 for (v = aggval; v; v = v->next)
4508 gcc_assert (v->index >= 0);
4509 if (c < v->index)
4510 c = v->index;
4512 c++;
4514 adj = XALLOCAVEC (int, c);
4515 for (i = 0; i < c; i++)
4516 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4518 adj[i] = -1;
4519 d++;
4521 else
4522 adj[i] = i - d;
4524 for (v = aggval; v; v = v->next)
4525 v->index = adj[v->index];
4529 /* Function body transformation phase. */
4531 unsigned int
4532 ipcp_transform_function (struct cgraph_node *node)
4534 vec<ipa_param_descriptor_t> descriptors = vNULL;
4535 struct param_analysis_info *parms_ainfo;
4536 struct ipa_agg_replacement_value *aggval;
4537 gimple_stmt_iterator gsi;
4538 basic_block bb;
4539 int param_count;
4540 bool cfg_changed = false, something_changed = false;
4542 gcc_checking_assert (cfun);
4543 gcc_checking_assert (current_function_decl);
4545 if (dump_file)
4546 fprintf (dump_file, "Modification phase of node %s/%i\n",
4547 cgraph_node_name (node), node->symbol.order);
4549 aggval = ipa_get_agg_replacements_for_node (node);
4550 if (!aggval)
4551 return 0;
4552 param_count = count_formal_params (node->symbol.decl);
4553 if (param_count == 0)
4554 return 0;
4555 adjust_agg_replacement_values (node, aggval);
4556 if (dump_file)
4557 ipa_dump_agg_replacement_values (dump_file, aggval);
4558 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
4559 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
4560 descriptors.safe_grow_cleared (param_count);
4561 ipa_populate_param_decls (node, descriptors);
4563 FOR_EACH_BB (bb)
4564 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4566 struct ipa_agg_replacement_value *v;
4567 gimple stmt = gsi_stmt (gsi);
4568 tree rhs, val, t;
4569 HOST_WIDE_INT offset;
4570 int index;
4571 bool by_ref, vce;
4573 if (!gimple_assign_load_p (stmt))
4574 continue;
4575 rhs = gimple_assign_rhs1 (stmt);
4576 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
4577 continue;
4579 vce = false;
4580 t = rhs;
4581 while (handled_component_p (t))
4583 /* V_C_E can do things like convert an array of integers to one
4584 bigger integer and similar things we do not handle below. */
4585 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
4587 vce = true;
4588 break;
4590 t = TREE_OPERAND (t, 0);
4592 if (vce)
4593 continue;
4595 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
4596 rhs, &index, &offset, &by_ref))
4597 continue;
4598 for (v = aggval; v; v = v->next)
4599 if (v->index == index
4600 && v->offset == offset)
4601 break;
4602 if (!v || v->by_ref != by_ref)
4603 continue;
4605 gcc_checking_assert (is_gimple_ip_invariant (v->value));
4606 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
4608 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
4609 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
4610 else if (TYPE_SIZE (TREE_TYPE (rhs))
4611 == TYPE_SIZE (TREE_TYPE (v->value)))
4612 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
4613 else
4615 if (dump_file)
4617 fprintf (dump_file, " const ");
4618 print_generic_expr (dump_file, v->value, 0);
4619 fprintf (dump_file, " can't be converted to type of ");
4620 print_generic_expr (dump_file, rhs, 0);
4621 fprintf (dump_file, "\n");
4623 continue;
4626 else
4627 val = v->value;
4629 if (dump_file && (dump_flags & TDF_DETAILS))
4631 fprintf (dump_file, "Modifying stmt:\n ");
4632 print_gimple_stmt (dump_file, stmt, 0, 0);
4634 gimple_assign_set_rhs_from_tree (&gsi, val);
4635 update_stmt (stmt);
4637 if (dump_file && (dump_flags & TDF_DETAILS))
4639 fprintf (dump_file, "into:\n ");
4640 print_gimple_stmt (dump_file, stmt, 0, 0);
4641 fprintf (dump_file, "\n");
4644 something_changed = true;
4645 if (maybe_clean_eh_stmt (stmt)
4646 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4647 cfg_changed = true;
4650 (*ipa_node_agg_replacements)[node->uid] = NULL;
4651 free_parms_ainfo (parms_ainfo, param_count);
4652 descriptors.release ();
4654 if (!something_changed)
4655 return 0;
4656 else if (cfg_changed)
4657 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
4658 else
4659 return TODO_update_ssa_only_virtuals;