* gfortran.fortran-torture/execute/intrinsic_nearest.x: Skip AIX.
[official-gcc.git] / gcc / ipa-prop.c
blob62d7a3c5e845f1f9b40fea42c1cf97a21e5417ca
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 "cgraph.h"
28 #include "ipa-prop.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-inline.h"
32 #include "ipa-inline.h"
33 #include "gimple.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "gimple-pretty-print.h"
37 #include "lto-streamer.h"
38 #include "data-streamer.h"
39 #include "tree-streamer.h"
40 #include "params.h"
42 /* Intermediate information about a parameter that is only useful during the
43 run of ipa_analyze_node and is not kept afterwards. */
45 struct param_analysis_info
47 bool parm_modified, ref_modified, pt_modified;
48 bitmap parm_visited_statements, pt_visited_statements;
51 /* Vector where the parameter infos are actually stored. */
52 vec<ipa_node_params_t> ipa_node_params_vector;
53 /* Vector of known aggregate values in cloned nodes. */
54 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
55 /* Vector where the parameter infos are actually stored. */
56 vec<ipa_edge_args_t, va_gc> *ipa_edge_args_vector;
58 /* Holders of ipa cgraph hooks: */
59 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
60 static struct cgraph_node_hook_list *node_removal_hook_holder;
61 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
62 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
63 static struct cgraph_node_hook_list *function_insertion_hook_holder;
65 /* Description of a reference to an IPA constant. */
66 struct ipa_cst_ref_desc
68 /* Edge that corresponds to the statement which took the reference. */
69 struct cgraph_edge *cs;
70 /* Linked list of duplicates created when call graph edges are cloned. */
71 struct ipa_cst_ref_desc *next_duplicate;
72 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
73 if out of control. */
74 int refcount;
77 /* Allocation pool for reference descriptions. */
79 static alloc_pool ipa_refdesc_pool;
81 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
82 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
84 static bool
85 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
87 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->symbol.decl);
88 struct cl_optimization *os;
90 if (!fs_opts)
91 return false;
92 os = TREE_OPTIMIZATION (fs_opts);
93 return !os->x_optimize || !os->x_flag_ipa_cp;
96 /* Return index of the formal whose tree is PTREE in function which corresponds
97 to INFO. */
99 static int
100 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor_t> descriptors, tree ptree)
102 int i, count;
104 count = descriptors.length ();
105 for (i = 0; i < count; i++)
106 if (descriptors[i].decl == ptree)
107 return i;
109 return -1;
112 /* Return index of the formal whose tree is PTREE in function which corresponds
113 to INFO. */
116 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
118 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
121 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
122 NODE. */
124 static void
125 ipa_populate_param_decls (struct cgraph_node *node,
126 vec<ipa_param_descriptor_t> &descriptors)
128 tree fndecl;
129 tree fnargs;
130 tree parm;
131 int param_num;
133 fndecl = node->symbol.decl;
134 fnargs = DECL_ARGUMENTS (fndecl);
135 param_num = 0;
136 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
138 descriptors[param_num].decl = parm;
139 param_num++;
143 /* Return how many formal parameters FNDECL has. */
145 static inline int
146 count_formal_params (tree fndecl)
148 tree parm;
149 int count = 0;
151 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
152 count++;
154 return count;
157 /* Initialize the ipa_node_params structure associated with NODE by counting
158 the function parameters, creating the descriptors and populating their
159 param_decls. */
161 void
162 ipa_initialize_node_params (struct cgraph_node *node)
164 struct ipa_node_params *info = IPA_NODE_REF (node);
166 if (!info->descriptors.exists ())
168 int param_count;
170 param_count = count_formal_params (node->symbol.decl);
171 if (param_count)
173 info->descriptors.safe_grow_cleared (param_count);
174 ipa_populate_param_decls (node, info->descriptors);
179 /* Print the jump functions associated with call graph edge CS to file F. */
181 static void
182 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
184 int i, count;
186 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
187 for (i = 0; i < count; i++)
189 struct ipa_jump_func *jump_func;
190 enum jump_func_type type;
192 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
193 type = jump_func->type;
195 fprintf (f, " param %d: ", i);
196 if (type == IPA_JF_UNKNOWN)
197 fprintf (f, "UNKNOWN\n");
198 else if (type == IPA_JF_KNOWN_TYPE)
200 fprintf (f, "KNOWN TYPE: base ");
201 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
202 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
203 jump_func->value.known_type.offset);
204 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
205 fprintf (f, "\n");
207 else if (type == IPA_JF_CONST)
209 tree val = jump_func->value.constant.value;
210 fprintf (f, "CONST: ");
211 print_generic_expr (f, val, 0);
212 if (TREE_CODE (val) == ADDR_EXPR
213 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
215 fprintf (f, " -> ");
216 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
219 fprintf (f, "\n");
221 else if (type == IPA_JF_PASS_THROUGH)
223 fprintf (f, "PASS THROUGH: ");
224 fprintf (f, "%d, op %s",
225 jump_func->value.pass_through.formal_id,
226 tree_code_name[(int)
227 jump_func->value.pass_through.operation]);
228 if (jump_func->value.pass_through.operation != NOP_EXPR)
230 fprintf (f, " ");
231 print_generic_expr (f,
232 jump_func->value.pass_through.operand, 0);
234 if (jump_func->value.pass_through.agg_preserved)
235 fprintf (f, ", agg_preserved");
236 fprintf (f, "\n");
238 else if (type == IPA_JF_ANCESTOR)
240 fprintf (f, "ANCESTOR: ");
241 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
242 jump_func->value.ancestor.formal_id,
243 jump_func->value.ancestor.offset);
244 print_generic_expr (f, jump_func->value.ancestor.type, 0);
245 if (jump_func->value.ancestor.agg_preserved)
246 fprintf (f, ", agg_preserved");
247 fprintf (f, "\n");
250 if (jump_func->agg.items)
252 struct ipa_agg_jf_item *item;
253 int j;
255 fprintf (f, " Aggregate passed by %s:\n",
256 jump_func->agg.by_ref ? "reference" : "value");
257 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
259 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
260 item->offset);
261 if (TYPE_P (item->value))
262 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
263 tree_low_cst (TYPE_SIZE (item->value), 1));
264 else
266 fprintf (f, "cst: ");
267 print_generic_expr (f, item->value, 0);
269 fprintf (f, "\n");
276 /* Print the jump functions of all arguments on all call graph edges going from
277 NODE to file F. */
279 void
280 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
282 struct cgraph_edge *cs;
284 fprintf (f, " Jump functions of caller %s/%i:\n", cgraph_node_name (node),
285 node->symbol.order);
286 for (cs = node->callees; cs; cs = cs->next_callee)
288 if (!ipa_edge_args_info_available_for_edge_p (cs))
289 continue;
291 fprintf (f, " callsite %s/%i -> %s/%i : \n",
292 xstrdup (cgraph_node_name (node)), node->symbol.order,
293 xstrdup (cgraph_node_name (cs->callee)),
294 cs->callee->symbol.order);
295 ipa_print_node_jump_functions_for_edge (f, cs);
298 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
300 struct cgraph_indirect_call_info *ii;
301 if (!ipa_edge_args_info_available_for_edge_p (cs))
302 continue;
304 ii = cs->indirect_info;
305 if (ii->agg_contents)
306 fprintf (f, " indirect %s callsite, calling param %i, "
307 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
308 ii->member_ptr ? "member ptr" : "aggregate",
309 ii->param_index, ii->offset,
310 ii->by_ref ? "by reference" : "by_value");
311 else
312 fprintf (f, " indirect %s callsite, calling param %i",
313 ii->polymorphic ? "polymorphic" : "simple", ii->param_index);
315 if (cs->call_stmt)
317 fprintf (f, ", for stmt ");
318 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
320 else
321 fprintf (f, "\n");
322 ipa_print_node_jump_functions_for_edge (f, cs);
326 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
328 void
329 ipa_print_all_jump_functions (FILE *f)
331 struct cgraph_node *node;
333 fprintf (f, "\nJump functions:\n");
334 FOR_EACH_FUNCTION (node)
336 ipa_print_node_jump_functions (f, node);
340 /* Set JFUNC to be a known type jump function. */
342 static void
343 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
344 tree base_type, tree component_type)
346 jfunc->type = IPA_JF_KNOWN_TYPE;
347 jfunc->value.known_type.offset = offset,
348 jfunc->value.known_type.base_type = base_type;
349 jfunc->value.known_type.component_type = component_type;
352 /* Set JFUNC to be a constant jmp function. */
354 static void
355 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
356 struct cgraph_edge *cs)
358 constant = unshare_expr (constant);
359 if (constant && EXPR_P (constant))
360 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
361 jfunc->type = IPA_JF_CONST;
362 jfunc->value.constant.value = unshare_expr_without_location (constant);
364 if (TREE_CODE (constant) == ADDR_EXPR
365 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
367 struct ipa_cst_ref_desc *rdesc;
368 if (!ipa_refdesc_pool)
369 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
370 sizeof (struct ipa_cst_ref_desc), 32);
372 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
373 rdesc->cs = cs;
374 rdesc->next_duplicate = NULL;
375 rdesc->refcount = 1;
376 jfunc->value.constant.rdesc = rdesc;
378 else
379 jfunc->value.constant.rdesc = NULL;
382 /* Set JFUNC to be a simple pass-through jump function. */
383 static void
384 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
385 bool agg_preserved)
387 jfunc->type = IPA_JF_PASS_THROUGH;
388 jfunc->value.pass_through.operand = NULL_TREE;
389 jfunc->value.pass_through.formal_id = formal_id;
390 jfunc->value.pass_through.operation = NOP_EXPR;
391 jfunc->value.pass_through.agg_preserved = agg_preserved;
394 /* Set JFUNC to be an arithmetic pass through jump function. */
396 static void
397 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
398 tree operand, enum tree_code operation)
400 jfunc->type = IPA_JF_PASS_THROUGH;
401 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
402 jfunc->value.pass_through.formal_id = formal_id;
403 jfunc->value.pass_through.operation = operation;
404 jfunc->value.pass_through.agg_preserved = false;
407 /* Set JFUNC to be an ancestor jump function. */
409 static void
410 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
411 tree type, int formal_id, bool agg_preserved)
413 jfunc->type = IPA_JF_ANCESTOR;
414 jfunc->value.ancestor.formal_id = formal_id;
415 jfunc->value.ancestor.offset = offset;
416 jfunc->value.ancestor.type = type;
417 jfunc->value.ancestor.agg_preserved = agg_preserved;
420 /* Extract the acual BINFO being described by JFUNC which must be a known type
421 jump function. */
423 tree
424 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
426 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
427 if (!base_binfo)
428 return NULL_TREE;
429 return get_binfo_at_offset (base_binfo,
430 jfunc->value.known_type.offset,
431 jfunc->value.known_type.component_type);
434 /* Structure to be passed in between detect_type_change and
435 check_stmt_for_type_change. */
437 struct type_change_info
439 /* Offset into the object where there is the virtual method pointer we are
440 looking for. */
441 HOST_WIDE_INT offset;
442 /* The declaration or SSA_NAME pointer of the base that we are checking for
443 type change. */
444 tree object;
445 /* If we actually can tell the type that the object has changed to, it is
446 stored in this field. Otherwise it remains NULL_TREE. */
447 tree known_current_type;
448 /* Set to true if dynamic type change has been detected. */
449 bool type_maybe_changed;
450 /* Set to true if multiple types have been encountered. known_current_type
451 must be disregarded in that case. */
452 bool multiple_types_encountered;
455 /* Return true if STMT can modify a virtual method table pointer.
457 This function makes special assumptions about both constructors and
458 destructors which are all the functions that are allowed to alter the VMT
459 pointers. It assumes that destructors begin with assignment into all VMT
460 pointers and that constructors essentially look in the following way:
462 1) The very first thing they do is that they call constructors of ancestor
463 sub-objects that have them.
465 2) Then VMT pointers of this and all its ancestors is set to new values
466 corresponding to the type corresponding to the constructor.
468 3) Only afterwards, other stuff such as constructor of member sub-objects
469 and the code written by the user is run. Only this may include calling
470 virtual functions, directly or indirectly.
472 There is no way to call a constructor of an ancestor sub-object in any
473 other way.
475 This means that we do not have to care whether constructors get the correct
476 type information because they will always change it (in fact, if we define
477 the type to be given by the VMT pointer, it is undefined).
479 The most important fact to derive from the above is that if, for some
480 statement in the section 3, we try to detect whether the dynamic type has
481 changed, we can safely ignore all calls as we examine the function body
482 backwards until we reach statements in section 2 because these calls cannot
483 be ancestor constructors or destructors (if the input is not bogus) and so
484 do not change the dynamic type (this holds true only for automatically
485 allocated objects but at the moment we devirtualize only these). We then
486 must detect that statements in section 2 change the dynamic type and can try
487 to derive the new type. That is enough and we can stop, we will never see
488 the calls into constructors of sub-objects in this code. Therefore we can
489 safely ignore all call statements that we traverse.
492 static bool
493 stmt_may_be_vtbl_ptr_store (gimple stmt)
495 if (is_gimple_call (stmt))
496 return false;
497 else if (is_gimple_assign (stmt))
499 tree lhs = gimple_assign_lhs (stmt);
501 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
503 if (flag_strict_aliasing
504 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
505 return false;
507 if (TREE_CODE (lhs) == COMPONENT_REF
508 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
509 return false;
510 /* In the future we might want to use get_base_ref_and_offset to find
511 if there is a field corresponding to the offset and if so, proceed
512 almost like if it was a component ref. */
515 return true;
518 /* If STMT can be proved to be an assignment to the virtual method table
519 pointer of ANALYZED_OBJ and the type associated with the new table
520 identified, return the type. Otherwise return NULL_TREE. */
522 static tree
523 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
525 HOST_WIDE_INT offset, size, max_size;
526 tree lhs, rhs, base;
528 if (!gimple_assign_single_p (stmt))
529 return NULL_TREE;
531 lhs = gimple_assign_lhs (stmt);
532 rhs = gimple_assign_rhs1 (stmt);
533 if (TREE_CODE (lhs) != COMPONENT_REF
534 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))
535 || TREE_CODE (rhs) != ADDR_EXPR)
536 return NULL_TREE;
537 rhs = get_base_address (TREE_OPERAND (rhs, 0));
538 if (!rhs
539 || TREE_CODE (rhs) != VAR_DECL
540 || !DECL_VIRTUAL_P (rhs))
541 return NULL_TREE;
543 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
544 if (offset != tci->offset
545 || size != POINTER_SIZE
546 || max_size != POINTER_SIZE)
547 return NULL_TREE;
548 if (TREE_CODE (base) == MEM_REF)
550 if (TREE_CODE (tci->object) != MEM_REF
551 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
552 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
553 TREE_OPERAND (base, 1)))
554 return NULL_TREE;
556 else if (tci->object != base)
557 return NULL_TREE;
559 return DECL_CONTEXT (rhs);
562 /* Callback of walk_aliased_vdefs and a helper function for
563 detect_type_change to check whether a particular statement may modify
564 the virtual table pointer, and if possible also determine the new type of
565 the (sub-)object. It stores its result into DATA, which points to a
566 type_change_info structure. */
568 static bool
569 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
571 gimple stmt = SSA_NAME_DEF_STMT (vdef);
572 struct type_change_info *tci = (struct type_change_info *) data;
574 if (stmt_may_be_vtbl_ptr_store (stmt))
576 tree type;
577 type = extr_type_from_vtbl_ptr_store (stmt, tci);
578 if (tci->type_maybe_changed
579 && type != tci->known_current_type)
580 tci->multiple_types_encountered = true;
581 tci->known_current_type = type;
582 tci->type_maybe_changed = true;
583 return true;
585 else
586 return false;
591 /* Like detect_type_change but with extra argument COMP_TYPE which will become
592 the component type part of new JFUNC of dynamic type change is detected and
593 the new base type is identified. */
595 static bool
596 detect_type_change_1 (tree arg, tree base, tree comp_type, gimple call,
597 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
599 struct type_change_info tci;
600 ao_ref ao;
602 gcc_checking_assert (DECL_P (arg)
603 || TREE_CODE (arg) == MEM_REF
604 || handled_component_p (arg));
605 /* Const calls cannot call virtual methods through VMT and so type changes do
606 not matter. */
607 if (!flag_devirtualize || !gimple_vuse (call))
608 return false;
610 ao_ref_init (&ao, arg);
611 ao.base = base;
612 ao.offset = offset;
613 ao.size = POINTER_SIZE;
614 ao.max_size = ao.size;
616 tci.offset = offset;
617 tci.object = get_base_address (arg);
618 tci.known_current_type = NULL_TREE;
619 tci.type_maybe_changed = false;
620 tci.multiple_types_encountered = false;
622 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
623 &tci, NULL);
624 if (!tci.type_maybe_changed)
625 return false;
627 if (!tci.known_current_type
628 || tci.multiple_types_encountered
629 || offset != 0)
630 jfunc->type = IPA_JF_UNKNOWN;
631 else
632 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
634 return true;
637 /* Detect whether the dynamic type of ARG has changed (before callsite CALL) by
638 looking for assignments to its virtual table pointer. If it is, return true
639 and fill in the jump function JFUNC with relevant type information or set it
640 to unknown. ARG is the object itself (not a pointer to it, unless
641 dereferenced). BASE is the base of the memory access as returned by
642 get_ref_base_and_extent, as is the offset. */
644 static bool
645 detect_type_change (tree arg, tree base, gimple call,
646 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
648 return detect_type_change_1 (arg, base, TREE_TYPE (arg), call, jfunc, offset);
651 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
652 SSA name (its dereference will become the base and the offset is assumed to
653 be zero). */
655 static bool
656 detect_type_change_ssa (tree arg, gimple call, struct ipa_jump_func *jfunc)
658 tree comp_type;
660 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
661 if (!flag_devirtualize
662 || !POINTER_TYPE_P (TREE_TYPE (arg))
663 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != RECORD_TYPE)
664 return false;
666 comp_type = TREE_TYPE (TREE_TYPE (arg));
667 arg = build2 (MEM_REF, ptr_type_node, arg,
668 build_int_cst (ptr_type_node, 0));
670 return detect_type_change_1 (arg, arg, comp_type, call, jfunc, 0);
673 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
674 boolean variable pointed to by DATA. */
676 static bool
677 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
678 void *data)
680 bool *b = (bool *) data;
681 *b = true;
682 return true;
685 /* Return true if a load from a formal parameter PARM_LOAD is known to retreive
686 a value known not to be modified in this function before reaching the
687 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
688 information about the parameter. */
690 static bool
691 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
692 gimple stmt, tree parm_load)
694 bool modified = false;
695 bitmap *visited_stmts;
696 ao_ref refd;
698 if (parm_ainfo && parm_ainfo->parm_modified)
699 return false;
701 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
702 ao_ref_init (&refd, parm_load);
703 /* We can cache visited statements only when parm_ainfo is available and when
704 we are looking at a naked load of the whole parameter. */
705 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
706 visited_stmts = NULL;
707 else
708 visited_stmts = &parm_ainfo->parm_visited_statements;
709 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
710 visited_stmts);
711 if (parm_ainfo && modified)
712 parm_ainfo->parm_modified = true;
713 return !modified;
716 /* If STMT is an assignment that loads a value from an parameter declaration,
717 return the index of the parameter in ipa_node_params which has not been
718 modified. Otherwise return -1. */
720 static int
721 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
722 struct param_analysis_info *parms_ainfo,
723 gimple stmt)
725 int index;
726 tree op1;
728 if (!gimple_assign_single_p (stmt))
729 return -1;
731 op1 = gimple_assign_rhs1 (stmt);
732 if (TREE_CODE (op1) != PARM_DECL)
733 return -1;
735 index = ipa_get_param_decl_index_1 (descriptors, op1);
736 if (index < 0
737 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
738 : NULL, stmt, op1))
739 return -1;
741 return index;
744 /* Return true if memory reference REF loads data that are known to be
745 unmodified in this function before reaching statement STMT. PARM_AINFO, if
746 non-NULL, is a pointer to a structure containing temporary information about
747 PARM. */
749 static bool
750 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
751 gimple stmt, tree ref)
753 bool modified = false;
754 ao_ref refd;
756 gcc_checking_assert (gimple_vuse (stmt));
757 if (parm_ainfo && parm_ainfo->ref_modified)
758 return false;
760 ao_ref_init (&refd, ref);
761 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
762 NULL);
763 if (parm_ainfo && modified)
764 parm_ainfo->ref_modified = true;
765 return !modified;
768 /* Return true if the data pointed to by PARM is known to be unmodified in this
769 function before reaching call statement CALL into which it is passed.
770 PARM_AINFO is a pointer to a structure containing temporary information
771 about PARM. */
773 static bool
774 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
775 gimple call, tree parm)
777 bool modified = false;
778 ao_ref refd;
780 /* It's unnecessary to calculate anything about memory contnets for a const
781 function because it is not goin to use it. But do not cache the result
782 either. Also, no such calculations for non-pointers. */
783 if (!gimple_vuse (call)
784 || !POINTER_TYPE_P (TREE_TYPE (parm)))
785 return false;
787 if (parm_ainfo->pt_modified)
788 return false;
790 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
791 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
792 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
793 if (modified)
794 parm_ainfo->pt_modified = true;
795 return !modified;
798 /* Return true if we can prove that OP is a memory reference loading unmodified
799 data from an aggregate passed as a parameter and if the aggregate is passed
800 by reference, that the alias type of the load corresponds to the type of the
801 formal parameter (so that we can rely on this type for TBAA in callers).
802 INFO and PARMS_AINFO describe parameters of the current function (but the
803 latter can be NULL), STMT is the load statement. If function returns true,
804 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
805 within the aggregate and whether it is a load from a value passed by
806 reference respectively. */
808 static bool
809 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
810 struct param_analysis_info *parms_ainfo, gimple stmt,
811 tree op, int *index_p, HOST_WIDE_INT *offset_p,
812 bool *by_ref_p)
814 int index;
815 HOST_WIDE_INT size, max_size;
816 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
818 if (max_size == -1 || max_size != size || *offset_p < 0)
819 return false;
821 if (DECL_P (base))
823 int index = ipa_get_param_decl_index_1 (descriptors, base);
824 if (index >= 0
825 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
826 : NULL, stmt, op))
828 *index_p = index;
829 *by_ref_p = false;
830 return true;
832 return false;
835 if (TREE_CODE (base) != MEM_REF
836 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
837 || !integer_zerop (TREE_OPERAND (base, 1)))
838 return false;
840 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
842 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
843 index = ipa_get_param_decl_index_1 (descriptors, parm);
845 else
847 /* This branch catches situations where a pointer parameter is not a
848 gimple register, for example:
850 void hip7(S*) (struct S * p)
852 void (*<T2e4>) (struct S *) D.1867;
853 struct S * p.1;
855 <bb 2>:
856 p.1_1 = p;
857 D.1867_2 = p.1_1->f;
858 D.1867_2 ();
859 gdp = &p;
862 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
863 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
866 if (index >= 0
867 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
868 stmt, op))
870 *index_p = index;
871 *by_ref_p = true;
872 return true;
874 return false;
877 /* Just like the previous function, just without the param_analysis_info
878 pointer, for users outside of this file. */
880 bool
881 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
882 tree op, int *index_p, HOST_WIDE_INT *offset_p,
883 bool *by_ref_p)
885 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
886 offset_p, by_ref_p);
889 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
890 of an assignment statement STMT, try to determine whether we are actually
891 handling any of the following cases and construct an appropriate jump
892 function into JFUNC if so:
894 1) The passed value is loaded from a formal parameter which is not a gimple
895 register (most probably because it is addressable, the value has to be
896 scalar) and we can guarantee the value has not changed. This case can
897 therefore be described by a simple pass-through jump function. For example:
899 foo (int a)
901 int a.0;
903 a.0_2 = a;
904 bar (a.0_2);
906 2) The passed value can be described by a simple arithmetic pass-through
907 jump function. E.g.
909 foo (int a)
911 int D.2064;
913 D.2064_4 = a.1(D) + 4;
914 bar (D.2064_4);
916 This case can also occur in combination of the previous one, e.g.:
918 foo (int a, int z)
920 int a.0;
921 int D.2064;
923 a.0_3 = a;
924 D.2064_4 = a.0_3 + 4;
925 foo (D.2064_4);
927 3) The passed value is an address of an object within another one (which
928 also passed by reference). Such situations are described by an ancestor
929 jump function and describe situations such as:
931 B::foo() (struct B * const this)
933 struct A * D.1845;
935 D.1845_2 = &this_1(D)->D.1748;
936 A::bar (D.1845_2);
938 INFO is the structure describing individual parameters access different
939 stages of IPA optimizations. PARMS_AINFO contains the information that is
940 only needed for intraprocedural analysis. */
942 static void
943 compute_complex_assign_jump_func (struct ipa_node_params *info,
944 struct param_analysis_info *parms_ainfo,
945 struct ipa_jump_func *jfunc,
946 gimple call, gimple stmt, tree name)
948 HOST_WIDE_INT offset, size, max_size;
949 tree op1, tc_ssa, base, ssa;
950 int index;
952 op1 = gimple_assign_rhs1 (stmt);
954 if (TREE_CODE (op1) == SSA_NAME)
956 if (SSA_NAME_IS_DEFAULT_DEF (op1))
957 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
958 else
959 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
960 SSA_NAME_DEF_STMT (op1));
961 tc_ssa = op1;
963 else
965 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
966 tc_ssa = gimple_assign_lhs (stmt);
969 if (index >= 0)
971 tree op2 = gimple_assign_rhs2 (stmt);
973 if (op2)
975 if (!is_gimple_ip_invariant (op2)
976 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
977 && !useless_type_conversion_p (TREE_TYPE (name),
978 TREE_TYPE (op1))))
979 return;
981 ipa_set_jf_arith_pass_through (jfunc, index, op2,
982 gimple_assign_rhs_code (stmt));
984 else if (gimple_assign_single_p (stmt)
985 && !detect_type_change_ssa (tc_ssa, call, jfunc))
987 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
988 call, tc_ssa);
989 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
991 return;
994 if (TREE_CODE (op1) != ADDR_EXPR)
995 return;
996 op1 = TREE_OPERAND (op1, 0);
997 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
998 return;
999 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1000 if (TREE_CODE (base) != MEM_REF
1001 /* If this is a varying address, punt. */
1002 || max_size == -1
1003 || max_size != size)
1004 return;
1005 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
1006 ssa = TREE_OPERAND (base, 0);
1007 if (TREE_CODE (ssa) != SSA_NAME
1008 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1009 || offset < 0)
1010 return;
1012 /* Dynamic types are changed only in constructors and destructors and */
1013 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1014 if (index >= 0
1015 && !detect_type_change (op1, base, call, jfunc, offset))
1016 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
1017 parm_ref_data_pass_through_p (&parms_ainfo[index],
1018 call, ssa));
1021 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1022 it looks like:
1024 iftmp.1_3 = &obj_2(D)->D.1762;
1026 The base of the MEM_REF must be a default definition SSA NAME of a
1027 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1028 whole MEM_REF expression is returned and the offset calculated from any
1029 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1030 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1032 static tree
1033 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1035 HOST_WIDE_INT size, max_size;
1036 tree expr, parm, obj;
1038 if (!gimple_assign_single_p (assign))
1039 return NULL_TREE;
1040 expr = gimple_assign_rhs1 (assign);
1042 if (TREE_CODE (expr) != ADDR_EXPR)
1043 return NULL_TREE;
1044 expr = TREE_OPERAND (expr, 0);
1045 obj = expr;
1046 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1048 if (TREE_CODE (expr) != MEM_REF
1049 /* If this is a varying address, punt. */
1050 || max_size == -1
1051 || max_size != size
1052 || *offset < 0)
1053 return NULL_TREE;
1054 parm = TREE_OPERAND (expr, 0);
1055 if (TREE_CODE (parm) != SSA_NAME
1056 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1057 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1058 return NULL_TREE;
1060 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1061 *obj_p = obj;
1062 return expr;
1066 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1067 statement PHI, try to find out whether NAME is in fact a
1068 multiple-inheritance typecast from a descendant into an ancestor of a formal
1069 parameter and thus can be described by an ancestor jump function and if so,
1070 write the appropriate function into JFUNC.
1072 Essentially we want to match the following pattern:
1074 if (obj_2(D) != 0B)
1075 goto <bb 3>;
1076 else
1077 goto <bb 4>;
1079 <bb 3>:
1080 iftmp.1_3 = &obj_2(D)->D.1762;
1082 <bb 4>:
1083 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1084 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1085 return D.1879_6; */
1087 static void
1088 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1089 struct param_analysis_info *parms_ainfo,
1090 struct ipa_jump_func *jfunc,
1091 gimple call, gimple phi)
1093 HOST_WIDE_INT offset;
1094 gimple assign, cond;
1095 basic_block phi_bb, assign_bb, cond_bb;
1096 tree tmp, parm, expr, obj;
1097 int index, i;
1099 if (gimple_phi_num_args (phi) != 2)
1100 return;
1102 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1103 tmp = PHI_ARG_DEF (phi, 0);
1104 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1105 tmp = PHI_ARG_DEF (phi, 1);
1106 else
1107 return;
1108 if (TREE_CODE (tmp) != SSA_NAME
1109 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1110 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1111 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1112 return;
1114 assign = SSA_NAME_DEF_STMT (tmp);
1115 assign_bb = gimple_bb (assign);
1116 if (!single_pred_p (assign_bb))
1117 return;
1118 expr = get_ancestor_addr_info (assign, &obj, &offset);
1119 if (!expr)
1120 return;
1121 parm = TREE_OPERAND (expr, 0);
1122 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1123 gcc_assert (index >= 0);
1125 cond_bb = single_pred (assign_bb);
1126 cond = last_stmt (cond_bb);
1127 if (!cond
1128 || gimple_code (cond) != GIMPLE_COND
1129 || gimple_cond_code (cond) != NE_EXPR
1130 || gimple_cond_lhs (cond) != parm
1131 || !integer_zerop (gimple_cond_rhs (cond)))
1132 return;
1134 phi_bb = gimple_bb (phi);
1135 for (i = 0; i < 2; i++)
1137 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1138 if (pred != assign_bb && pred != cond_bb)
1139 return;
1142 if (!detect_type_change (obj, expr, call, jfunc, offset))
1143 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1144 parm_ref_data_pass_through_p (&parms_ainfo[index],
1145 call, parm));
1148 /* Given OP which is passed as an actual argument to a called function,
1149 determine if it is possible to construct a KNOWN_TYPE jump function for it
1150 and if so, create one and store it to JFUNC. */
1152 static void
1153 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1154 gimple call)
1156 HOST_WIDE_INT offset, size, max_size;
1157 tree base;
1159 if (!flag_devirtualize
1160 || TREE_CODE (op) != ADDR_EXPR
1161 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
1162 return;
1164 op = TREE_OPERAND (op, 0);
1165 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1166 if (!DECL_P (base)
1167 || max_size == -1
1168 || max_size != size
1169 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1170 || is_global_var (base))
1171 return;
1173 if (!TYPE_BINFO (TREE_TYPE (base))
1174 || detect_type_change (op, base, call, jfunc, offset))
1175 return;
1177 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base), TREE_TYPE (op));
1180 /* Inspect the given TYPE and return true iff it has the same structure (the
1181 same number of fields of the same types) as a C++ member pointer. If
1182 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1183 corresponding fields there. */
1185 static bool
1186 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1188 tree fld;
1190 if (TREE_CODE (type) != RECORD_TYPE)
1191 return false;
1193 fld = TYPE_FIELDS (type);
1194 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1195 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1196 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1197 return false;
1199 if (method_ptr)
1200 *method_ptr = fld;
1202 fld = DECL_CHAIN (fld);
1203 if (!fld || INTEGRAL_TYPE_P (fld)
1204 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1205 return false;
1206 if (delta)
1207 *delta = fld;
1209 if (DECL_CHAIN (fld))
1210 return false;
1212 return true;
1215 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1216 return the rhs of its defining statement. Otherwise return RHS as it
1217 is. */
1219 static inline tree
1220 get_ssa_def_if_simple_copy (tree rhs)
1222 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1224 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1226 if (gimple_assign_single_p (def_stmt))
1227 rhs = gimple_assign_rhs1 (def_stmt);
1228 else
1229 break;
1231 return rhs;
1234 /* Simple linked list, describing known contents of an aggregate beforere
1235 call. */
1237 struct ipa_known_agg_contents_list
1239 /* Offset and size of the described part of the aggregate. */
1240 HOST_WIDE_INT offset, size;
1241 /* Known constant value or NULL if the contents is known to be unknown. */
1242 tree constant;
1243 /* Pointer to the next structure in the list. */
1244 struct ipa_known_agg_contents_list *next;
1247 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1248 in ARG is filled in with constant values. ARG can either be an aggregate
1249 expression or a pointer to an aggregate. JFUNC is the jump function into
1250 which the constants are subsequently stored. */
1252 static void
1253 determine_known_aggregate_parts (gimple call, tree arg,
1254 struct ipa_jump_func *jfunc)
1256 struct ipa_known_agg_contents_list *list = NULL;
1257 int item_count = 0, const_count = 0;
1258 HOST_WIDE_INT arg_offset, arg_size;
1259 gimple_stmt_iterator gsi;
1260 tree arg_base;
1261 bool check_ref, by_ref;
1262 ao_ref r;
1264 /* The function operates in three stages. First, we prepare check_ref, r,
1265 arg_base and arg_offset based on what is actually passed as an actual
1266 argument. */
1268 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1270 by_ref = true;
1271 if (TREE_CODE (arg) == SSA_NAME)
1273 tree type_size;
1274 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1275 return;
1276 check_ref = true;
1277 arg_base = arg;
1278 arg_offset = 0;
1279 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1280 arg_size = tree_low_cst (type_size, 1);
1281 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1283 else if (TREE_CODE (arg) == ADDR_EXPR)
1285 HOST_WIDE_INT arg_max_size;
1287 arg = TREE_OPERAND (arg, 0);
1288 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1289 &arg_max_size);
1290 if (arg_max_size == -1
1291 || arg_max_size != arg_size
1292 || arg_offset < 0)
1293 return;
1294 if (DECL_P (arg_base))
1296 tree size;
1297 check_ref = false;
1298 size = build_int_cst (integer_type_node, arg_size);
1299 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1301 else
1302 return;
1304 else
1305 return;
1307 else
1309 HOST_WIDE_INT arg_max_size;
1311 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1313 by_ref = false;
1314 check_ref = false;
1315 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1316 &arg_max_size);
1317 if (arg_max_size == -1
1318 || arg_max_size != arg_size
1319 || arg_offset < 0)
1320 return;
1322 ao_ref_init (&r, arg);
1325 /* Second stage walks back the BB, looks at individual statements and as long
1326 as it is confident of how the statements affect contents of the
1327 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1328 describing it. */
1329 gsi = gsi_for_stmt (call);
1330 gsi_prev (&gsi);
1331 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1333 struct ipa_known_agg_contents_list *n, **p;
1334 gimple stmt = gsi_stmt (gsi);
1335 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1336 tree lhs, rhs, lhs_base;
1337 bool partial_overlap;
1339 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1340 continue;
1341 if (!gimple_assign_single_p (stmt))
1342 break;
1344 lhs = gimple_assign_lhs (stmt);
1345 rhs = gimple_assign_rhs1 (stmt);
1346 if (!is_gimple_reg_type (rhs)
1347 || TREE_CODE (lhs) == BIT_FIELD_REF
1348 || contains_bitfld_component_ref_p (lhs))
1349 break;
1351 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1352 &lhs_max_size);
1353 if (lhs_max_size == -1
1354 || lhs_max_size != lhs_size
1355 || (lhs_offset < arg_offset
1356 && lhs_offset + lhs_size > arg_offset)
1357 || (lhs_offset < arg_offset + arg_size
1358 && lhs_offset + lhs_size > arg_offset + arg_size))
1359 break;
1361 if (check_ref)
1363 if (TREE_CODE (lhs_base) != MEM_REF
1364 || TREE_OPERAND (lhs_base, 0) != arg_base
1365 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1366 break;
1368 else if (lhs_base != arg_base)
1370 if (DECL_P (lhs_base))
1371 continue;
1372 else
1373 break;
1376 if (lhs_offset + lhs_size < arg_offset
1377 || lhs_offset >= (arg_offset + arg_size))
1378 continue;
1380 partial_overlap = false;
1381 p = &list;
1382 while (*p && (*p)->offset < lhs_offset)
1384 if ((*p)->offset + (*p)->size > lhs_offset)
1386 partial_overlap = true;
1387 break;
1389 p = &(*p)->next;
1391 if (partial_overlap)
1392 break;
1393 if (*p && (*p)->offset < lhs_offset + lhs_size)
1395 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1396 /* We already know this value is subsequently overwritten with
1397 something else. */
1398 continue;
1399 else
1400 /* Otherwise this is a partial overlap which we cannot
1401 represent. */
1402 break;
1405 rhs = get_ssa_def_if_simple_copy (rhs);
1406 n = XALLOCA (struct ipa_known_agg_contents_list);
1407 n->size = lhs_size;
1408 n->offset = lhs_offset;
1409 if (is_gimple_ip_invariant (rhs))
1411 n->constant = rhs;
1412 const_count++;
1414 else
1415 n->constant = NULL_TREE;
1416 n->next = *p;
1417 *p = n;
1419 item_count++;
1420 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1421 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1422 break;
1425 /* Third stage just goes over the list and creates an appropriate vector of
1426 ipa_agg_jf_item structures out of it, of sourse only if there are
1427 any known constants to begin with. */
1429 if (const_count)
1431 jfunc->agg.by_ref = by_ref;
1432 vec_alloc (jfunc->agg.items, const_count);
1433 while (list)
1435 if (list->constant)
1437 struct ipa_agg_jf_item item;
1438 item.offset = list->offset - arg_offset;
1439 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1440 item.value = unshare_expr_without_location (list->constant);
1441 jfunc->agg.items->quick_push (item);
1443 list = list->next;
1448 /* Compute jump function for all arguments of callsite CS and insert the
1449 information in the jump_functions array in the ipa_edge_args corresponding
1450 to this callsite. */
1452 static void
1453 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1454 struct cgraph_edge *cs)
1456 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1457 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1458 gimple call = cs->call_stmt;
1459 int n, arg_num = gimple_call_num_args (call);
1461 if (arg_num == 0 || args->jump_functions)
1462 return;
1463 vec_safe_grow_cleared (args->jump_functions, arg_num);
1465 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1466 return;
1468 for (n = 0; n < arg_num; n++)
1470 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1471 tree arg = gimple_call_arg (call, n);
1473 if (is_gimple_ip_invariant (arg))
1474 ipa_set_jf_constant (jfunc, arg, cs);
1475 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1476 && TREE_CODE (arg) == PARM_DECL)
1478 int index = ipa_get_param_decl_index (info, arg);
1480 gcc_assert (index >=0);
1481 /* Aggregate passed by value, check for pass-through, otherwise we
1482 will attempt to fill in aggregate contents later in this
1483 for cycle. */
1484 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1486 ipa_set_jf_simple_pass_through (jfunc, index, false);
1487 continue;
1490 else if (TREE_CODE (arg) == SSA_NAME)
1492 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1494 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1495 if (index >= 0
1496 && !detect_type_change_ssa (arg, call, jfunc))
1498 bool agg_p;
1499 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1500 call, arg);
1501 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
1504 else
1506 gimple stmt = SSA_NAME_DEF_STMT (arg);
1507 if (is_gimple_assign (stmt))
1508 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1509 call, stmt, arg);
1510 else if (gimple_code (stmt) == GIMPLE_PHI)
1511 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1512 call, stmt);
1515 else
1516 compute_known_type_jump_func (arg, jfunc, call);
1518 if ((jfunc->type != IPA_JF_PASS_THROUGH
1519 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1520 && (jfunc->type != IPA_JF_ANCESTOR
1521 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1522 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1523 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1524 determine_known_aggregate_parts (call, arg, jfunc);
1528 /* Compute jump functions for all edges - both direct and indirect - outgoing
1529 from NODE. Also count the actual arguments in the process. */
1531 static void
1532 ipa_compute_jump_functions (struct cgraph_node *node,
1533 struct param_analysis_info *parms_ainfo)
1535 struct cgraph_edge *cs;
1537 for (cs = node->callees; cs; cs = cs->next_callee)
1539 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1540 NULL);
1541 /* We do not need to bother analyzing calls to unknown
1542 functions unless they may become known during lto/whopr. */
1543 if (!callee->symbol.definition && !flag_lto)
1544 continue;
1545 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1548 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1549 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1552 /* If STMT looks like a statement loading a value from a member pointer formal
1553 parameter, return that parameter and store the offset of the field to
1554 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1555 might be clobbered). If USE_DELTA, then we look for a use of the delta
1556 field rather than the pfn. */
1558 static tree
1559 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1560 HOST_WIDE_INT *offset_p)
1562 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1564 if (!gimple_assign_single_p (stmt))
1565 return NULL_TREE;
1567 rhs = gimple_assign_rhs1 (stmt);
1568 if (TREE_CODE (rhs) == COMPONENT_REF)
1570 ref_field = TREE_OPERAND (rhs, 1);
1571 rhs = TREE_OPERAND (rhs, 0);
1573 else
1574 ref_field = NULL_TREE;
1575 if (TREE_CODE (rhs) != MEM_REF)
1576 return NULL_TREE;
1577 rec = TREE_OPERAND (rhs, 0);
1578 if (TREE_CODE (rec) != ADDR_EXPR)
1579 return NULL_TREE;
1580 rec = TREE_OPERAND (rec, 0);
1581 if (TREE_CODE (rec) != PARM_DECL
1582 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1583 return NULL_TREE;
1584 ref_offset = TREE_OPERAND (rhs, 1);
1586 if (use_delta)
1587 fld = delta_field;
1588 else
1589 fld = ptr_field;
1590 if (offset_p)
1591 *offset_p = int_bit_position (fld);
1593 if (ref_field)
1595 if (integer_nonzerop (ref_offset))
1596 return NULL_TREE;
1597 return ref_field == fld ? rec : NULL_TREE;
1599 else
1600 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1601 : NULL_TREE;
1604 /* Returns true iff T is an SSA_NAME defined by a statement. */
1606 static bool
1607 ipa_is_ssa_with_stmt_def (tree t)
1609 if (TREE_CODE (t) == SSA_NAME
1610 && !SSA_NAME_IS_DEFAULT_DEF (t))
1611 return true;
1612 else
1613 return false;
1616 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1617 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1618 indirect call graph edge. */
1620 static struct cgraph_edge *
1621 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1623 struct cgraph_edge *cs;
1625 cs = cgraph_edge (node, stmt);
1626 cs->indirect_info->param_index = param_index;
1627 cs->indirect_info->offset = 0;
1628 cs->indirect_info->polymorphic = 0;
1629 cs->indirect_info->agg_contents = 0;
1630 cs->indirect_info->member_ptr = 0;
1631 return cs;
1634 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1635 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1636 intermediate information about each formal parameter. Currently it checks
1637 whether the call calls a pointer that is a formal parameter and if so, the
1638 parameter is marked with the called flag and an indirect call graph edge
1639 describing the call is created. This is very simple for ordinary pointers
1640 represented in SSA but not-so-nice when it comes to member pointers. The
1641 ugly part of this function does nothing more than trying to match the
1642 pattern of such a call. An example of such a pattern is the gimple dump
1643 below, the call is on the last line:
1645 <bb 2>:
1646 f$__delta_5 = f.__delta;
1647 f$__pfn_24 = f.__pfn;
1650 <bb 2>:
1651 f$__delta_5 = MEM[(struct *)&f];
1652 f$__pfn_24 = MEM[(struct *)&f + 4B];
1654 and a few lines below:
1656 <bb 5>
1657 D.2496_3 = (int) f$__pfn_24;
1658 D.2497_4 = D.2496_3 & 1;
1659 if (D.2497_4 != 0)
1660 goto <bb 3>;
1661 else
1662 goto <bb 4>;
1664 <bb 6>:
1665 D.2500_7 = (unsigned int) f$__delta_5;
1666 D.2501_8 = &S + D.2500_7;
1667 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1668 D.2503_10 = *D.2502_9;
1669 D.2504_12 = f$__pfn_24 + -1;
1670 D.2505_13 = (unsigned int) D.2504_12;
1671 D.2506_14 = D.2503_10 + D.2505_13;
1672 D.2507_15 = *D.2506_14;
1673 iftmp.11_16 = (String:: *) D.2507_15;
1675 <bb 7>:
1676 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1677 D.2500_19 = (unsigned int) f$__delta_5;
1678 D.2508_20 = &S + D.2500_19;
1679 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1681 Such patterns are results of simple calls to a member pointer:
1683 int doprinting (int (MyString::* f)(int) const)
1685 MyString S ("somestring");
1687 return (S.*f)(4);
1690 Moreover, the function also looks for called pointers loaded from aggregates
1691 passed by value or reference. */
1693 static void
1694 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1695 struct ipa_node_params *info,
1696 struct param_analysis_info *parms_ainfo,
1697 gimple call, tree target)
1699 gimple def;
1700 tree n1, n2;
1701 gimple d1, d2;
1702 tree rec, rec2, cond;
1703 gimple branch;
1704 int index;
1705 basic_block bb, virt_bb, join;
1706 HOST_WIDE_INT offset;
1707 bool by_ref;
1709 if (SSA_NAME_IS_DEFAULT_DEF (target))
1711 tree var = SSA_NAME_VAR (target);
1712 index = ipa_get_param_decl_index (info, var);
1713 if (index >= 0)
1714 ipa_note_param_call (node, index, call);
1715 return;
1718 def = SSA_NAME_DEF_STMT (target);
1719 if (gimple_assign_single_p (def)
1720 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1721 gimple_assign_rhs1 (def), &index, &offset,
1722 &by_ref))
1724 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1725 cs->indirect_info->offset = offset;
1726 cs->indirect_info->agg_contents = 1;
1727 cs->indirect_info->by_ref = by_ref;
1728 return;
1731 /* Now we need to try to match the complex pattern of calling a member
1732 pointer. */
1733 if (gimple_code (def) != GIMPLE_PHI
1734 || gimple_phi_num_args (def) != 2
1735 || !POINTER_TYPE_P (TREE_TYPE (target))
1736 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1737 return;
1739 /* First, we need to check whether one of these is a load from a member
1740 pointer that is a parameter to this function. */
1741 n1 = PHI_ARG_DEF (def, 0);
1742 n2 = PHI_ARG_DEF (def, 1);
1743 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1744 return;
1745 d1 = SSA_NAME_DEF_STMT (n1);
1746 d2 = SSA_NAME_DEF_STMT (n2);
1748 join = gimple_bb (def);
1749 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1751 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1752 return;
1754 bb = EDGE_PRED (join, 0)->src;
1755 virt_bb = gimple_bb (d2);
1757 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1759 bb = EDGE_PRED (join, 1)->src;
1760 virt_bb = gimple_bb (d1);
1762 else
1763 return;
1765 /* Second, we need to check that the basic blocks are laid out in the way
1766 corresponding to the pattern. */
1768 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1769 || single_pred (virt_bb) != bb
1770 || single_succ (virt_bb) != join)
1771 return;
1773 /* Third, let's see that the branching is done depending on the least
1774 significant bit of the pfn. */
1776 branch = last_stmt (bb);
1777 if (!branch || gimple_code (branch) != GIMPLE_COND)
1778 return;
1780 if ((gimple_cond_code (branch) != NE_EXPR
1781 && gimple_cond_code (branch) != EQ_EXPR)
1782 || !integer_zerop (gimple_cond_rhs (branch)))
1783 return;
1785 cond = gimple_cond_lhs (branch);
1786 if (!ipa_is_ssa_with_stmt_def (cond))
1787 return;
1789 def = SSA_NAME_DEF_STMT (cond);
1790 if (!is_gimple_assign (def)
1791 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1792 || !integer_onep (gimple_assign_rhs2 (def)))
1793 return;
1795 cond = gimple_assign_rhs1 (def);
1796 if (!ipa_is_ssa_with_stmt_def (cond))
1797 return;
1799 def = SSA_NAME_DEF_STMT (cond);
1801 if (is_gimple_assign (def)
1802 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1804 cond = gimple_assign_rhs1 (def);
1805 if (!ipa_is_ssa_with_stmt_def (cond))
1806 return;
1807 def = SSA_NAME_DEF_STMT (cond);
1810 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1811 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1812 == ptrmemfunc_vbit_in_delta),
1813 NULL);
1814 if (rec != rec2)
1815 return;
1817 index = ipa_get_param_decl_index (info, rec);
1818 if (index >= 0
1819 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1821 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1822 cs->indirect_info->offset = offset;
1823 cs->indirect_info->agg_contents = 1;
1824 cs->indirect_info->member_ptr = 1;
1827 return;
1830 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1831 object referenced in the expression is a formal parameter of the caller
1832 (described by INFO), create a call note for the statement. */
1834 static void
1835 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1836 struct ipa_node_params *info, gimple call,
1837 tree target)
1839 struct cgraph_edge *cs;
1840 struct cgraph_indirect_call_info *ii;
1841 struct ipa_jump_func jfunc;
1842 tree obj = OBJ_TYPE_REF_OBJECT (target);
1843 int index;
1844 HOST_WIDE_INT anc_offset;
1846 if (!flag_devirtualize)
1847 return;
1849 if (TREE_CODE (obj) != SSA_NAME)
1850 return;
1852 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1854 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1855 return;
1857 anc_offset = 0;
1858 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1859 gcc_assert (index >= 0);
1860 if (detect_type_change_ssa (obj, call, &jfunc))
1861 return;
1863 else
1865 gimple stmt = SSA_NAME_DEF_STMT (obj);
1866 tree expr;
1868 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1869 if (!expr)
1870 return;
1871 index = ipa_get_param_decl_index (info,
1872 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1873 gcc_assert (index >= 0);
1874 if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1875 return;
1878 cs = ipa_note_param_call (node, index, call);
1879 ii = cs->indirect_info;
1880 ii->offset = anc_offset;
1881 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1882 ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1883 ii->polymorphic = 1;
1886 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1887 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1888 containing intermediate information about each formal parameter. */
1890 static void
1891 ipa_analyze_call_uses (struct cgraph_node *node,
1892 struct ipa_node_params *info,
1893 struct param_analysis_info *parms_ainfo, gimple call)
1895 tree target = gimple_call_fn (call);
1897 if (!target)
1898 return;
1899 if (TREE_CODE (target) == SSA_NAME)
1900 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
1901 else if (TREE_CODE (target) == OBJ_TYPE_REF)
1902 ipa_analyze_virtual_call_uses (node, info, call, target);
1906 /* Analyze the call statement STMT with respect to formal parameters (described
1907 in INFO) of caller given by NODE. Currently it only checks whether formal
1908 parameters are called. PARMS_AINFO is a pointer to a vector containing
1909 intermediate information about each formal parameter. */
1911 static void
1912 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1913 struct param_analysis_info *parms_ainfo, gimple stmt)
1915 if (is_gimple_call (stmt))
1916 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
1919 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1920 If OP is a parameter declaration, mark it as used in the info structure
1921 passed in DATA. */
1923 static bool
1924 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
1925 tree op, void *data)
1927 struct ipa_node_params *info = (struct ipa_node_params *) data;
1929 op = get_base_address (op);
1930 if (op
1931 && TREE_CODE (op) == PARM_DECL)
1933 int index = ipa_get_param_decl_index (info, op);
1934 gcc_assert (index >= 0);
1935 ipa_set_param_used (info, index, true);
1938 return false;
1941 /* Scan the function body of NODE and inspect the uses of formal parameters.
1942 Store the findings in various structures of the associated ipa_node_params
1943 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
1944 vector containing intermediate information about each formal parameter. */
1946 static void
1947 ipa_analyze_params_uses (struct cgraph_node *node,
1948 struct param_analysis_info *parms_ainfo)
1950 tree decl = node->symbol.decl;
1951 basic_block bb;
1952 struct function *func;
1953 gimple_stmt_iterator gsi;
1954 struct ipa_node_params *info = IPA_NODE_REF (node);
1955 int i;
1957 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1958 return;
1960 info->uses_analysis_done = 1;
1961 if (ipa_func_spec_opts_forbid_analysis_p (node))
1963 for (i = 0; i < ipa_get_param_count (info); i++)
1965 ipa_set_param_used (info, i, true);
1966 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
1968 return;
1971 for (i = 0; i < ipa_get_param_count (info); i++)
1973 tree parm = ipa_get_param (info, i);
1974 int controlled_uses = 0;
1976 /* For SSA regs see if parameter is used. For non-SSA we compute
1977 the flag during modification analysis. */
1978 if (is_gimple_reg (parm))
1980 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->symbol.decl),
1981 parm);
1982 if (ddef && !has_zero_uses (ddef))
1984 imm_use_iterator imm_iter;
1985 use_operand_p use_p;
1987 ipa_set_param_used (info, i, true);
1988 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
1989 if (!is_gimple_call (USE_STMT (use_p)))
1991 controlled_uses = IPA_UNDESCRIBED_USE;
1992 break;
1994 else
1995 controlled_uses++;
1997 else
1998 controlled_uses = 0;
2000 else
2001 controlled_uses = IPA_UNDESCRIBED_USE;
2002 ipa_set_controlled_uses (info, i, controlled_uses);
2005 func = DECL_STRUCT_FUNCTION (decl);
2006 FOR_EACH_BB_FN (bb, func)
2008 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2010 gimple stmt = gsi_stmt (gsi);
2012 if (is_gimple_debug (stmt))
2013 continue;
2015 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
2016 walk_stmt_load_store_addr_ops (stmt, info,
2017 visit_ref_for_mod_analysis,
2018 visit_ref_for_mod_analysis,
2019 visit_ref_for_mod_analysis);
2021 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2022 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
2023 visit_ref_for_mod_analysis,
2024 visit_ref_for_mod_analysis,
2025 visit_ref_for_mod_analysis);
2029 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
2031 static void
2032 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
2034 int i;
2036 for (i = 0; i < param_count; i++)
2038 if (parms_ainfo[i].parm_visited_statements)
2039 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
2040 if (parms_ainfo[i].pt_visited_statements)
2041 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
2045 /* Initialize the array describing properties of of formal parameters
2046 of NODE, analyze their uses and compute jump functions associated
2047 with actual arguments of calls from within NODE. */
2049 void
2050 ipa_analyze_node (struct cgraph_node *node)
2052 struct ipa_node_params *info;
2053 struct param_analysis_info *parms_ainfo;
2054 int param_count;
2056 ipa_check_create_node_params ();
2057 ipa_check_create_edge_args ();
2058 info = IPA_NODE_REF (node);
2059 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2060 ipa_initialize_node_params (node);
2062 param_count = ipa_get_param_count (info);
2063 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
2064 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
2066 ipa_analyze_params_uses (node, parms_ainfo);
2067 ipa_compute_jump_functions (node, parms_ainfo);
2069 free_parms_ainfo (parms_ainfo, param_count);
2070 pop_cfun ();
2073 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2074 attempt a type-based devirtualization. If successful, return the
2075 target function declaration, otherwise return NULL. */
2077 tree
2078 ipa_intraprocedural_devirtualization (gimple call)
2080 tree binfo, token, fndecl;
2081 struct ipa_jump_func jfunc;
2082 tree otr = gimple_call_fn (call);
2084 jfunc.type = IPA_JF_UNKNOWN;
2085 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2086 call);
2087 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2088 return NULL_TREE;
2089 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2090 if (!binfo)
2091 return NULL_TREE;
2092 token = OBJ_TYPE_REF_TOKEN (otr);
2093 fndecl = gimple_get_virt_method_for_binfo (tree_low_cst (token, 1),
2094 binfo);
2095 return fndecl;
2098 /* Update the jump function DST when the call graph edge corresponding to SRC is
2099 is being inlined, knowing that DST is of type ancestor and src of known
2100 type. */
2102 static void
2103 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2104 struct ipa_jump_func *dst)
2106 HOST_WIDE_INT combined_offset;
2107 tree combined_type;
2109 combined_offset = ipa_get_jf_known_type_offset (src)
2110 + ipa_get_jf_ancestor_offset (dst);
2111 combined_type = ipa_get_jf_ancestor_type (dst);
2113 ipa_set_jf_known_type (dst, combined_offset,
2114 ipa_get_jf_known_type_base_type (src),
2115 combined_type);
2118 /* Update the jump functions associated with call graph edge E when the call
2119 graph edge CS is being inlined, assuming that E->caller is already (possibly
2120 indirectly) inlined into CS->callee and that E has not been inlined. */
2122 static void
2123 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2124 struct cgraph_edge *e)
2126 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2127 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2128 int count = ipa_get_cs_argument_count (args);
2129 int i;
2131 for (i = 0; i < count; i++)
2133 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2135 if (dst->type == IPA_JF_ANCESTOR)
2137 struct ipa_jump_func *src;
2138 int dst_fid = dst->value.ancestor.formal_id;
2140 /* Variable number of arguments can cause havoc if we try to access
2141 one that does not exist in the inlined edge. So make sure we
2142 don't. */
2143 if (dst_fid >= ipa_get_cs_argument_count (top))
2145 dst->type = IPA_JF_UNKNOWN;
2146 continue;
2149 src = ipa_get_ith_jump_func (top, dst_fid);
2151 if (src->agg.items
2152 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2154 struct ipa_agg_jf_item *item;
2155 int j;
2157 /* Currently we do not produce clobber aggregate jump functions,
2158 replace with merging when we do. */
2159 gcc_assert (!dst->agg.items);
2161 dst->agg.items = vec_safe_copy (src->agg.items);
2162 dst->agg.by_ref = src->agg.by_ref;
2163 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2164 item->offset -= dst->value.ancestor.offset;
2167 if (src->type == IPA_JF_KNOWN_TYPE)
2168 combine_known_type_and_ancestor_jfs (src, dst);
2169 else if (src->type == IPA_JF_PASS_THROUGH
2170 && src->value.pass_through.operation == NOP_EXPR)
2172 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2173 dst->value.ancestor.agg_preserved &=
2174 src->value.pass_through.agg_preserved;
2176 else if (src->type == IPA_JF_ANCESTOR)
2178 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2179 dst->value.ancestor.offset += src->value.ancestor.offset;
2180 dst->value.ancestor.agg_preserved &=
2181 src->value.ancestor.agg_preserved;
2183 else
2184 dst->type = IPA_JF_UNKNOWN;
2186 else if (dst->type == IPA_JF_PASS_THROUGH)
2188 struct ipa_jump_func *src;
2189 /* We must check range due to calls with variable number of arguments
2190 and we cannot combine jump functions with operations. */
2191 if (dst->value.pass_through.operation == NOP_EXPR
2192 && (dst->value.pass_through.formal_id
2193 < ipa_get_cs_argument_count (top)))
2195 bool agg_p;
2196 int dst_fid = dst->value.pass_through.formal_id;
2197 src = ipa_get_ith_jump_func (top, dst_fid);
2198 agg_p = dst->value.pass_through.agg_preserved;
2200 dst->type = src->type;
2201 dst->value = src->value;
2203 if (src->agg.items
2204 && (agg_p || !src->agg.by_ref))
2206 /* Currently we do not produce clobber aggregate jump
2207 functions, replace with merging when we do. */
2208 gcc_assert (!dst->agg.items);
2210 dst->agg.by_ref = src->agg.by_ref;
2211 dst->agg.items = vec_safe_copy (src->agg.items);
2214 if (!agg_p)
2216 if (dst->type == IPA_JF_PASS_THROUGH)
2217 dst->value.pass_through.agg_preserved = false;
2218 else if (dst->type == IPA_JF_ANCESTOR)
2219 dst->value.ancestor.agg_preserved = false;
2222 else
2223 dst->type = IPA_JF_UNKNOWN;
2228 /* If TARGET is an addr_expr of a function declaration, make it the destination
2229 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2231 struct cgraph_edge *
2232 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2234 struct cgraph_node *callee;
2235 struct inline_edge_summary *es = inline_edge_summary (ie);
2236 bool unreachable = false;
2238 if (TREE_CODE (target) == ADDR_EXPR)
2239 target = TREE_OPERAND (target, 0);
2240 if (TREE_CODE (target) != FUNCTION_DECL)
2242 target = canonicalize_constructor_val (target, NULL);
2243 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2245 if (ie->indirect_info->member_ptr)
2246 /* Member pointer call that goes through a VMT lookup. */
2247 return NULL;
2249 if (dump_file)
2250 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2251 " in %s/%i, making it unreachable.\n",
2252 cgraph_node_name (ie->caller), ie->caller->symbol.order);
2253 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2254 callee = cgraph_get_create_node (target);
2255 unreachable = true;
2257 else
2258 callee = cgraph_get_node (target);
2260 else
2261 callee = cgraph_get_node (target);
2263 /* Because may-edges are not explicitely represented and vtable may be external,
2264 we may create the first reference to the object in the unit. */
2265 if (!callee || callee->global.inlined_to)
2268 /* We are better to ensure we can refer to it.
2269 In the case of static functions we are out of luck, since we already
2270 removed its body. In the case of public functions we may or may
2271 not introduce the reference. */
2272 if (!canonicalize_constructor_val (target, NULL)
2273 || !TREE_PUBLIC (target))
2275 if (dump_file)
2276 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2277 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2278 xstrdup (cgraph_node_name (ie->caller)),
2279 ie->caller->symbol.order,
2280 xstrdup (cgraph_node_name (ie->callee)),
2281 ie->callee->symbol.order);
2282 return NULL;
2284 callee = cgraph_get_create_real_symbol_node (target);
2286 ipa_check_create_node_params ();
2288 /* We can not make edges to inline clones. It is bug that someone removed
2289 the cgraph node too early. */
2290 gcc_assert (!callee->global.inlined_to);
2292 cgraph_make_edge_direct (ie, callee);
2293 es = inline_edge_summary (ie);
2294 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2295 - eni_size_weights.call_cost);
2296 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2297 - eni_time_weights.call_cost);
2298 if (dump_file && !unreachable)
2300 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2301 "(%s/%i -> %s/%i), for stmt ",
2302 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2303 xstrdup (cgraph_node_name (ie->caller)),
2304 ie->caller->symbol.order,
2305 xstrdup (cgraph_node_name (ie->callee)),
2306 ie->callee->symbol.order);
2307 if (ie->call_stmt)
2308 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2309 else
2310 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2312 callee = cgraph_function_or_thunk_node (callee, NULL);
2314 return ie;
2317 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2318 return NULL if there is not any. BY_REF specifies whether the value has to
2319 be passed by reference or by value. */
2321 tree
2322 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2323 HOST_WIDE_INT offset, bool by_ref)
2325 struct ipa_agg_jf_item *item;
2326 int i;
2328 if (by_ref != agg->by_ref)
2329 return NULL;
2331 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2332 if (item->offset == offset)
2334 /* Currently we do not have clobber values, return NULL for them once
2335 we do. */
2336 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2337 return item->value;
2339 return NULL;
2342 /* Remove a reference to SYMBOL from the list of references of a node given by
2343 reference description RDESC. */
2345 static void
2346 remove_described_reference (symtab_node symbol, struct ipa_cst_ref_desc *rdesc)
2348 struct ipa_ref *to_del;
2349 struct cgraph_edge *origin;
2351 origin = rdesc->cs;
2352 to_del = ipa_find_reference ((symtab_node) origin->caller, symbol,
2353 origin->call_stmt);
2354 gcc_assert (to_del);
2355 ipa_remove_reference (to_del);
2356 if (dump_file)
2357 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2358 xstrdup (cgraph_node_name (origin->caller)),
2359 origin->caller->symbol.order, xstrdup (symtab_node_name (symbol)));
2362 /* If JFUNC has a reference description with refcount different from
2363 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2364 NULL. JFUNC must be a constant jump function. */
2366 static struct ipa_cst_ref_desc *
2367 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2369 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2370 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2371 return rdesc;
2372 else
2373 return NULL;
2376 /* Try to find a destination for indirect edge IE that corresponds to a simple
2377 call or a call of a member function pointer and where the destination is a
2378 pointer formal parameter described by jump function JFUNC. If it can be
2379 determined, return the newly direct edge, otherwise return NULL.
2380 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2382 static struct cgraph_edge *
2383 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2384 struct ipa_jump_func *jfunc,
2385 struct ipa_node_params *new_root_info)
2387 struct ipa_cst_ref_desc *rdesc;
2388 struct cgraph_edge *cs;
2389 tree target;
2391 if (ie->indirect_info->agg_contents)
2392 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2393 ie->indirect_info->offset,
2394 ie->indirect_info->by_ref);
2395 else
2396 target = ipa_value_from_jfunc (new_root_info, jfunc);
2397 if (!target)
2398 return NULL;
2399 cs = ipa_make_edge_direct_to_target (ie, target);
2401 if (cs && !ie->indirect_info->agg_contents
2402 && jfunc->type == IPA_JF_CONST
2403 && (rdesc = jfunc_rdesc_usable (jfunc))
2404 && --rdesc->refcount == 0)
2405 remove_described_reference ((symtab_node) cs->callee, rdesc);
2407 return cs;
2410 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2411 call based on a formal parameter which is described by jump function JFUNC
2412 and if it can be determined, make it direct and return the direct edge.
2413 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2414 are relative to. */
2416 static struct cgraph_edge *
2417 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2418 struct ipa_jump_func *jfunc,
2419 struct ipa_node_params *new_root_info)
2421 tree binfo, target;
2423 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2425 if (!binfo)
2426 return NULL;
2428 if (TREE_CODE (binfo) != TREE_BINFO)
2430 binfo = gimple_extract_devirt_binfo_from_cst (binfo);
2431 if (!binfo)
2432 return NULL;
2435 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2436 ie->indirect_info->otr_type);
2437 if (binfo)
2438 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2439 binfo);
2440 else
2441 return NULL;
2443 if (target)
2444 return ipa_make_edge_direct_to_target (ie, target);
2445 else
2446 return NULL;
2449 /* Update the param called notes associated with NODE when CS is being inlined,
2450 assuming NODE is (potentially indirectly) inlined into CS->callee.
2451 Moreover, if the callee is discovered to be constant, create a new cgraph
2452 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2453 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2455 static bool
2456 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2457 struct cgraph_node *node,
2458 vec<cgraph_edge_p> *new_edges)
2460 struct ipa_edge_args *top;
2461 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2462 struct ipa_node_params *new_root_info;
2463 bool res = false;
2465 ipa_check_create_edge_args ();
2466 top = IPA_EDGE_REF (cs);
2467 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2468 ? cs->caller->global.inlined_to
2469 : cs->caller);
2471 for (ie = node->indirect_calls; ie; ie = next_ie)
2473 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2474 struct ipa_jump_func *jfunc;
2475 int param_index;
2477 next_ie = ie->next_callee;
2479 if (ici->param_index == -1)
2480 continue;
2482 /* We must check range due to calls with variable number of arguments: */
2483 if (ici->param_index >= ipa_get_cs_argument_count (top))
2485 ici->param_index = -1;
2486 continue;
2489 param_index = ici->param_index;
2490 jfunc = ipa_get_ith_jump_func (top, param_index);
2492 if (!flag_indirect_inlining)
2493 new_direct_edge = NULL;
2494 else if (ici->polymorphic)
2495 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2496 new_root_info);
2497 else
2498 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2499 new_root_info);
2500 if (new_direct_edge)
2502 new_direct_edge->indirect_inlining_edge = 1;
2503 if (new_direct_edge->call_stmt)
2504 new_direct_edge->call_stmt_cannot_inline_p
2505 = !gimple_check_call_matching_types (
2506 new_direct_edge->call_stmt,
2507 new_direct_edge->callee->symbol.decl, false);
2508 if (new_edges)
2510 new_edges->safe_push (new_direct_edge);
2511 top = IPA_EDGE_REF (cs);
2512 res = true;
2515 else if (jfunc->type == IPA_JF_PASS_THROUGH
2516 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2518 if (ici->agg_contents
2519 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2520 ici->param_index = -1;
2521 else
2522 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2524 else if (jfunc->type == IPA_JF_ANCESTOR)
2526 if (ici->agg_contents
2527 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2528 ici->param_index = -1;
2529 else
2531 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2532 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2535 else
2536 /* Either we can find a destination for this edge now or never. */
2537 ici->param_index = -1;
2540 return res;
2543 /* Recursively traverse subtree of NODE (including node) made of inlined
2544 cgraph_edges when CS has been inlined and invoke
2545 update_indirect_edges_after_inlining on all nodes and
2546 update_jump_functions_after_inlining on all non-inlined edges that lead out
2547 of this subtree. Newly discovered indirect edges will be added to
2548 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2549 created. */
2551 static bool
2552 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2553 struct cgraph_node *node,
2554 vec<cgraph_edge_p> *new_edges)
2556 struct cgraph_edge *e;
2557 bool res;
2559 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2561 for (e = node->callees; e; e = e->next_callee)
2562 if (!e->inline_failed)
2563 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2564 else
2565 update_jump_functions_after_inlining (cs, e);
2566 for (e = node->indirect_calls; e; e = e->next_callee)
2567 update_jump_functions_after_inlining (cs, e);
2569 return res;
2572 /* Combine two controlled uses counts as done during inlining. */
2574 static int
2575 combine_controlled_uses_counters (int c, int d)
2577 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
2578 return IPA_UNDESCRIBED_USE;
2579 else
2580 return c + d - 1;
2583 /* Propagate number of controlled users from CS->caleee to the new root of the
2584 tree of inlined nodes. */
2586 static void
2587 propagate_controlled_uses (struct cgraph_edge *cs)
2589 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
2590 struct cgraph_node *new_root = cs->caller->global.inlined_to
2591 ? cs->caller->global.inlined_to : cs->caller;
2592 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
2593 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
2594 int count, i;
2596 count = MIN (ipa_get_cs_argument_count (args),
2597 ipa_get_param_count (old_root_info));
2598 for (i = 0; i < count; i++)
2600 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2601 struct ipa_cst_ref_desc *rdesc;
2603 if (jf->type == IPA_JF_PASS_THROUGH)
2605 int src_idx, c, d;
2606 src_idx = ipa_get_jf_pass_through_formal_id (jf);
2607 c = ipa_get_controlled_uses (new_root_info, src_idx);
2608 d = ipa_get_controlled_uses (old_root_info, i);
2610 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
2611 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
2612 c = combine_controlled_uses_counters (c, d);
2613 ipa_set_controlled_uses (new_root_info, src_idx, c);
2614 if (c == 0 && new_root_info->ipcp_orig_node)
2616 struct cgraph_node *n;
2617 struct ipa_ref *ref;
2618 tree t = new_root_info->known_vals[src_idx];
2620 if (t && TREE_CODE (t) == ADDR_EXPR
2621 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2622 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
2623 && (ref = ipa_find_reference ((symtab_node) new_root,
2624 (symtab_node) n, NULL)))
2626 if (dump_file)
2627 fprintf (dump_file, "ipa-prop: Removing cloning-created "
2628 "reference from %s/%i to %s/%i.\n",
2629 xstrdup (cgraph_node_name (new_root)),
2630 new_root->symbol.order,
2631 xstrdup (cgraph_node_name (n)), n->symbol.order);
2632 ipa_remove_reference (ref);
2636 else if (jf->type == IPA_JF_CONST
2637 && (rdesc = jfunc_rdesc_usable (jf)))
2639 int d = ipa_get_controlled_uses (old_root_info, i);
2640 int c = rdesc->refcount;
2641 rdesc->refcount = combine_controlled_uses_counters (c, d);
2642 if (rdesc->refcount == 0)
2644 tree cst = ipa_get_jf_constant (jf);
2645 struct cgraph_node *n;
2646 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
2647 && TREE_CODE (TREE_OPERAND (cst, 0))
2648 == FUNCTION_DECL);
2649 n = cgraph_get_node (TREE_OPERAND (cst, 0));
2650 if (n)
2652 struct cgraph_node *clone;
2653 remove_described_reference ((symtab_node) n, rdesc);
2655 clone = cs->caller;
2656 while (clone->global.inlined_to
2657 && clone != rdesc->cs->caller
2658 && IPA_NODE_REF (clone)->ipcp_orig_node)
2660 struct ipa_ref *ref;
2661 ref = ipa_find_reference ((symtab_node) clone,
2662 (symtab_node) n, NULL);
2663 if (ref)
2665 if (dump_file)
2666 fprintf (dump_file, "ipa-prop: Removing "
2667 "cloning-created reference "
2668 "from %s/%i to %s/%i.\n",
2669 xstrdup (cgraph_node_name (clone)),
2670 clone->symbol.order,
2671 xstrdup (cgraph_node_name (n)),
2672 n->symbol.order);
2673 ipa_remove_reference (ref);
2675 clone = clone->callers->caller;
2682 for (i = ipa_get_param_count (old_root_info);
2683 i < ipa_get_cs_argument_count (args);
2684 i++)
2686 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2688 if (jf->type == IPA_JF_CONST)
2690 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
2691 if (rdesc)
2692 rdesc->refcount = IPA_UNDESCRIBED_USE;
2694 else if (jf->type == IPA_JF_PASS_THROUGH)
2695 ipa_set_controlled_uses (new_root_info,
2696 jf->value.pass_through.formal_id,
2697 IPA_UNDESCRIBED_USE);
2701 /* Update jump functions and call note functions on inlining the call site CS.
2702 CS is expected to lead to a node already cloned by
2703 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2704 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2705 created. */
2707 bool
2708 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2709 vec<cgraph_edge_p> *new_edges)
2711 bool changed;
2712 /* Do nothing if the preparation phase has not been carried out yet
2713 (i.e. during early inlining). */
2714 if (!ipa_node_params_vector.exists ())
2715 return false;
2716 gcc_assert (ipa_edge_args_vector);
2718 propagate_controlled_uses (cs);
2719 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2721 return changed;
2724 /* Frees all dynamically allocated structures that the argument info points
2725 to. */
2727 void
2728 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2730 vec_free (args->jump_functions);
2731 memset (args, 0, sizeof (*args));
2734 /* Free all ipa_edge structures. */
2736 void
2737 ipa_free_all_edge_args (void)
2739 int i;
2740 struct ipa_edge_args *args;
2742 if (!ipa_edge_args_vector)
2743 return;
2745 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2746 ipa_free_edge_args_substructures (args);
2748 vec_free (ipa_edge_args_vector);
2751 /* Frees all dynamically allocated structures that the param info points
2752 to. */
2754 void
2755 ipa_free_node_params_substructures (struct ipa_node_params *info)
2757 info->descriptors.release ();
2758 free (info->lattices);
2759 /* Lattice values and their sources are deallocated with their alocation
2760 pool. */
2761 info->known_vals.release ();
2762 memset (info, 0, sizeof (*info));
2765 /* Free all ipa_node_params structures. */
2767 void
2768 ipa_free_all_node_params (void)
2770 int i;
2771 struct ipa_node_params *info;
2773 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
2774 ipa_free_node_params_substructures (info);
2776 ipa_node_params_vector.release ();
2779 /* Set the aggregate replacements of NODE to be AGGVALS. */
2781 void
2782 ipa_set_node_agg_value_chain (struct cgraph_node *node,
2783 struct ipa_agg_replacement_value *aggvals)
2785 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
2786 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
2788 (*ipa_node_agg_replacements)[node->uid] = aggvals;
2791 /* Hook that is called by cgraph.c when an edge is removed. */
2793 static void
2794 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
2796 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2797 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
2798 return;
2799 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
2802 /* Hook that is called by cgraph.c when a node is removed. */
2804 static void
2805 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2807 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2808 if (ipa_node_params_vector.length () > (unsigned)node->uid)
2809 ipa_free_node_params_substructures (IPA_NODE_REF (node));
2810 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
2811 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
2814 /* Hook that is called by cgraph.c when an edge is duplicated. */
2816 static void
2817 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2818 __attribute__((unused)) void *data)
2820 struct ipa_edge_args *old_args, *new_args;
2821 unsigned int i;
2823 ipa_check_create_edge_args ();
2825 old_args = IPA_EDGE_REF (src);
2826 new_args = IPA_EDGE_REF (dst);
2828 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
2830 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
2832 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
2833 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
2835 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
2837 if (src_jf->type == IPA_JF_CONST)
2839 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
2841 if (!src_rdesc)
2842 dst_jf->value.constant.rdesc = NULL;
2843 else if (src_rdesc->cs == src)
2845 struct ipa_cst_ref_desc *dst_rdesc;
2846 gcc_checking_assert (ipa_refdesc_pool);
2847 dst_rdesc
2848 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
2849 dst_rdesc->cs = dst;
2850 dst_rdesc->refcount = src_rdesc->refcount;
2851 if (dst->caller->global.inlined_to)
2853 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
2854 src_rdesc->next_duplicate = dst_rdesc;
2856 dst_jf->value.constant.rdesc = dst_rdesc;
2858 else
2860 struct ipa_cst_ref_desc *dst_rdesc;
2861 /* This can happen during inlining, when a JFUNC can refer to a
2862 reference taken in a function up in the tree of inline clones.
2863 We need to find the duplicate that refers to our tree of
2864 inline clones. */
2866 gcc_assert (dst->caller->global.inlined_to);
2867 for (dst_rdesc = src_rdesc->next_duplicate;
2868 dst_rdesc;
2869 dst_rdesc = dst_rdesc->next_duplicate)
2870 if (dst_rdesc->cs->caller->global.inlined_to
2871 == dst->caller->global.inlined_to)
2872 break;
2873 gcc_assert (dst_rdesc);
2874 dst_jf->value.constant.rdesc = dst_rdesc;
2880 /* Hook that is called by cgraph.c when a node is duplicated. */
2882 static void
2883 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
2884 ATTRIBUTE_UNUSED void *data)
2886 struct ipa_node_params *old_info, *new_info;
2887 struct ipa_agg_replacement_value *old_av, *new_av;
2889 ipa_check_create_node_params ();
2890 old_info = IPA_NODE_REF (src);
2891 new_info = IPA_NODE_REF (dst);
2893 new_info->descriptors = old_info->descriptors.copy ();
2894 new_info->lattices = NULL;
2895 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
2897 new_info->uses_analysis_done = old_info->uses_analysis_done;
2898 new_info->node_enqueued = old_info->node_enqueued;
2900 old_av = ipa_get_agg_replacements_for_node (src);
2901 if (!old_av)
2902 return;
2904 new_av = NULL;
2905 while (old_av)
2907 struct ipa_agg_replacement_value *v;
2909 v = ggc_alloc_ipa_agg_replacement_value ();
2910 memcpy (v, old_av, sizeof (*v));
2911 v->next = new_av;
2912 new_av = v;
2913 old_av = old_av->next;
2915 ipa_set_node_agg_value_chain (dst, new_av);
2919 /* Analyze newly added function into callgraph. */
2921 static void
2922 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2924 ipa_analyze_node (node);
2927 /* Register our cgraph hooks if they are not already there. */
2929 void
2930 ipa_register_cgraph_hooks (void)
2932 if (!edge_removal_hook_holder)
2933 edge_removal_hook_holder =
2934 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
2935 if (!node_removal_hook_holder)
2936 node_removal_hook_holder =
2937 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
2938 if (!edge_duplication_hook_holder)
2939 edge_duplication_hook_holder =
2940 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
2941 if (!node_duplication_hook_holder)
2942 node_duplication_hook_holder =
2943 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
2944 function_insertion_hook_holder =
2945 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
2948 /* Unregister our cgraph hooks if they are not already there. */
2950 static void
2951 ipa_unregister_cgraph_hooks (void)
2953 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2954 edge_removal_hook_holder = NULL;
2955 cgraph_remove_node_removal_hook (node_removal_hook_holder);
2956 node_removal_hook_holder = NULL;
2957 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2958 edge_duplication_hook_holder = NULL;
2959 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2960 node_duplication_hook_holder = NULL;
2961 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2962 function_insertion_hook_holder = NULL;
2965 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2966 longer needed after ipa-cp. */
2968 void
2969 ipa_free_all_structures_after_ipa_cp (void)
2971 if (!optimize)
2973 ipa_free_all_edge_args ();
2974 ipa_free_all_node_params ();
2975 free_alloc_pool (ipcp_sources_pool);
2976 free_alloc_pool (ipcp_values_pool);
2977 free_alloc_pool (ipcp_agg_lattice_pool);
2978 ipa_unregister_cgraph_hooks ();
2979 if (ipa_refdesc_pool)
2980 free_alloc_pool (ipa_refdesc_pool);
2984 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2985 longer needed after indirect inlining. */
2987 void
2988 ipa_free_all_structures_after_iinln (void)
2990 ipa_free_all_edge_args ();
2991 ipa_free_all_node_params ();
2992 ipa_unregister_cgraph_hooks ();
2993 if (ipcp_sources_pool)
2994 free_alloc_pool (ipcp_sources_pool);
2995 if (ipcp_values_pool)
2996 free_alloc_pool (ipcp_values_pool);
2997 if (ipcp_agg_lattice_pool)
2998 free_alloc_pool (ipcp_agg_lattice_pool);
2999 if (ipa_refdesc_pool)
3000 free_alloc_pool (ipa_refdesc_pool);
3003 /* Print ipa_tree_map data structures of all functions in the
3004 callgraph to F. */
3006 void
3007 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3009 int i, count;
3010 tree temp;
3011 struct ipa_node_params *info;
3013 if (!node->symbol.definition)
3014 return;
3015 info = IPA_NODE_REF (node);
3016 fprintf (f, " function %s/%i parameter descriptors:\n",
3017 cgraph_node_name (node), node->symbol.order);
3018 count = ipa_get_param_count (info);
3019 for (i = 0; i < count; i++)
3021 int c;
3023 temp = ipa_get_param (info, i);
3024 if (TREE_CODE (temp) == PARM_DECL)
3025 fprintf (f, " param %d : %s", i,
3026 (DECL_NAME (temp)
3027 ? (*lang_hooks.decl_printable_name) (temp, 2)
3028 : "(unnamed)"));
3029 if (ipa_is_param_used (info, i))
3030 fprintf (f, " used");
3031 c = ipa_get_controlled_uses (info, i);
3032 if (c == IPA_UNDESCRIBED_USE)
3033 fprintf (f, " undescribed_use");
3034 else
3035 fprintf (f, " controlled_uses=%i", c);
3036 fprintf (f, "\n");
3040 /* Print ipa_tree_map data structures of all functions in the
3041 callgraph to F. */
3043 void
3044 ipa_print_all_params (FILE * f)
3046 struct cgraph_node *node;
3048 fprintf (f, "\nFunction parameters:\n");
3049 FOR_EACH_FUNCTION (node)
3050 ipa_print_node_params (f, node);
3053 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3055 vec<tree>
3056 ipa_get_vector_of_formal_parms (tree fndecl)
3058 vec<tree> args;
3059 int count;
3060 tree parm;
3062 count = count_formal_params (fndecl);
3063 args.create (count);
3064 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3065 args.quick_push (parm);
3067 return args;
3070 /* Return a heap allocated vector containing types of formal parameters of
3071 function type FNTYPE. */
3073 static inline vec<tree>
3074 get_vector_of_formal_parm_types (tree fntype)
3076 vec<tree> types;
3077 int count = 0;
3078 tree t;
3080 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3081 count++;
3083 types.create (count);
3084 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3085 types.quick_push (TREE_VALUE (t));
3087 return types;
3090 /* Modify the function declaration FNDECL and its type according to the plan in
3091 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3092 to reflect the actual parameters being modified which are determined by the
3093 base_index field. */
3095 void
3096 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
3097 const char *synth_parm_prefix)
3099 vec<tree> oparms, otypes;
3100 tree orig_type, new_type = NULL;
3101 tree old_arg_types, t, new_arg_types = NULL;
3102 tree parm, *link = &DECL_ARGUMENTS (fndecl);
3103 int i, len = adjustments.length ();
3104 tree new_reversed = NULL;
3105 bool care_for_types, last_parm_void;
3107 if (!synth_parm_prefix)
3108 synth_parm_prefix = "SYNTH";
3110 oparms = ipa_get_vector_of_formal_parms (fndecl);
3111 orig_type = TREE_TYPE (fndecl);
3112 old_arg_types = TYPE_ARG_TYPES (orig_type);
3114 /* The following test is an ugly hack, some functions simply don't have any
3115 arguments in their type. This is probably a bug but well... */
3116 care_for_types = (old_arg_types != NULL_TREE);
3117 if (care_for_types)
3119 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3120 == void_type_node);
3121 otypes = get_vector_of_formal_parm_types (orig_type);
3122 if (last_parm_void)
3123 gcc_assert (oparms.length () + 1 == otypes.length ());
3124 else
3125 gcc_assert (oparms.length () == otypes.length ());
3127 else
3129 last_parm_void = false;
3130 otypes.create (0);
3133 for (i = 0; i < len; i++)
3135 struct ipa_parm_adjustment *adj;
3136 gcc_assert (link);
3138 adj = &adjustments[i];
3139 parm = oparms[adj->base_index];
3140 adj->base = parm;
3142 if (adj->copy_param)
3144 if (care_for_types)
3145 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3146 new_arg_types);
3147 *link = parm;
3148 link = &DECL_CHAIN (parm);
3150 else if (!adj->remove_param)
3152 tree new_parm;
3153 tree ptype;
3155 if (adj->by_ref)
3156 ptype = build_pointer_type (adj->type);
3157 else
3158 ptype = adj->type;
3160 if (care_for_types)
3161 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3163 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3164 ptype);
3165 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
3167 DECL_ARTIFICIAL (new_parm) = 1;
3168 DECL_ARG_TYPE (new_parm) = ptype;
3169 DECL_CONTEXT (new_parm) = fndecl;
3170 TREE_USED (new_parm) = 1;
3171 DECL_IGNORED_P (new_parm) = 1;
3172 layout_decl (new_parm, 0);
3174 adj->base = parm;
3175 adj->reduction = new_parm;
3177 *link = new_parm;
3179 link = &DECL_CHAIN (new_parm);
3183 *link = NULL_TREE;
3185 if (care_for_types)
3187 new_reversed = nreverse (new_arg_types);
3188 if (last_parm_void)
3190 if (new_reversed)
3191 TREE_CHAIN (new_arg_types) = void_list_node;
3192 else
3193 new_reversed = void_list_node;
3197 /* Use copy_node to preserve as much as possible from original type
3198 (debug info, attribute lists etc.)
3199 Exception is METHOD_TYPEs must have THIS argument.
3200 When we are asked to remove it, we need to build new FUNCTION_TYPE
3201 instead. */
3202 if (TREE_CODE (orig_type) != METHOD_TYPE
3203 || (adjustments[0].copy_param
3204 && adjustments[0].base_index == 0))
3206 new_type = build_distinct_type_copy (orig_type);
3207 TYPE_ARG_TYPES (new_type) = new_reversed;
3209 else
3211 new_type
3212 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3213 new_reversed));
3214 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3215 DECL_VINDEX (fndecl) = NULL_TREE;
3218 /* When signature changes, we need to clear builtin info. */
3219 if (DECL_BUILT_IN (fndecl))
3221 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3222 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3225 /* This is a new type, not a copy of an old type. Need to reassociate
3226 variants. We can handle everything except the main variant lazily. */
3227 t = TYPE_MAIN_VARIANT (orig_type);
3228 if (orig_type != t)
3230 TYPE_MAIN_VARIANT (new_type) = t;
3231 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3232 TYPE_NEXT_VARIANT (t) = new_type;
3234 else
3236 TYPE_MAIN_VARIANT (new_type) = new_type;
3237 TYPE_NEXT_VARIANT (new_type) = NULL;
3240 TREE_TYPE (fndecl) = new_type;
3241 DECL_VIRTUAL_P (fndecl) = 0;
3242 otypes.release ();
3243 oparms.release ();
3246 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3247 If this is a directly recursive call, CS must be NULL. Otherwise it must
3248 contain the corresponding call graph edge. */
3250 void
3251 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3252 ipa_parm_adjustment_vec adjustments)
3254 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3255 vec<tree> vargs;
3256 vec<tree, va_gc> **debug_args = NULL;
3257 gimple new_stmt;
3258 gimple_stmt_iterator gsi, prev_gsi;
3259 tree callee_decl;
3260 int i, len;
3262 len = adjustments.length ();
3263 vargs.create (len);
3264 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->symbol.decl;
3265 ipa_remove_stmt_references ((symtab_node) current_node, stmt);
3267 gsi = gsi_for_stmt (stmt);
3268 prev_gsi = gsi;
3269 gsi_prev (&prev_gsi);
3270 for (i = 0; i < len; i++)
3272 struct ipa_parm_adjustment *adj;
3274 adj = &adjustments[i];
3276 if (adj->copy_param)
3278 tree arg = gimple_call_arg (stmt, adj->base_index);
3280 vargs.quick_push (arg);
3282 else if (!adj->remove_param)
3284 tree expr, base, off;
3285 location_t loc;
3286 unsigned int deref_align;
3287 bool deref_base = false;
3289 /* We create a new parameter out of the value of the old one, we can
3290 do the following kind of transformations:
3292 - A scalar passed by reference is converted to a scalar passed by
3293 value. (adj->by_ref is false and the type of the original
3294 actual argument is a pointer to a scalar).
3296 - A part of an aggregate is passed instead of the whole aggregate.
3297 The part can be passed either by value or by reference, this is
3298 determined by value of adj->by_ref. Moreover, the code below
3299 handles both situations when the original aggregate is passed by
3300 value (its type is not a pointer) and when it is passed by
3301 reference (it is a pointer to an aggregate).
3303 When the new argument is passed by reference (adj->by_ref is true)
3304 it must be a part of an aggregate and therefore we form it by
3305 simply taking the address of a reference inside the original
3306 aggregate. */
3308 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3309 base = gimple_call_arg (stmt, adj->base_index);
3310 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3311 : EXPR_LOCATION (base);
3313 if (TREE_CODE (base) != ADDR_EXPR
3314 && POINTER_TYPE_P (TREE_TYPE (base)))
3315 off = build_int_cst (adj->alias_ptr_type,
3316 adj->offset / BITS_PER_UNIT);
3317 else
3319 HOST_WIDE_INT base_offset;
3320 tree prev_base;
3321 bool addrof;
3323 if (TREE_CODE (base) == ADDR_EXPR)
3325 base = TREE_OPERAND (base, 0);
3326 addrof = true;
3328 else
3329 addrof = false;
3330 prev_base = base;
3331 base = get_addr_base_and_unit_offset (base, &base_offset);
3332 /* Aggregate arguments can have non-invariant addresses. */
3333 if (!base)
3335 base = build_fold_addr_expr (prev_base);
3336 off = build_int_cst (adj->alias_ptr_type,
3337 adj->offset / BITS_PER_UNIT);
3339 else if (TREE_CODE (base) == MEM_REF)
3341 if (!addrof)
3343 deref_base = true;
3344 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3346 off = build_int_cst (adj->alias_ptr_type,
3347 base_offset
3348 + adj->offset / BITS_PER_UNIT);
3349 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3350 off);
3351 base = TREE_OPERAND (base, 0);
3353 else
3355 off = build_int_cst (adj->alias_ptr_type,
3356 base_offset
3357 + adj->offset / BITS_PER_UNIT);
3358 base = build_fold_addr_expr (base);
3362 if (!adj->by_ref)
3364 tree type = adj->type;
3365 unsigned int align;
3366 unsigned HOST_WIDE_INT misalign;
3368 if (deref_base)
3370 align = deref_align;
3371 misalign = 0;
3373 else
3375 get_pointer_alignment_1 (base, &align, &misalign);
3376 if (TYPE_ALIGN (type) > align)
3377 align = TYPE_ALIGN (type);
3379 misalign += (tree_to_double_int (off)
3380 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3381 * BITS_PER_UNIT);
3382 misalign = misalign & (align - 1);
3383 if (misalign != 0)
3384 align = (misalign & -misalign);
3385 if (align < TYPE_ALIGN (type))
3386 type = build_aligned_type (type, align);
3387 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3389 else
3391 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3392 expr = build_fold_addr_expr (expr);
3395 expr = force_gimple_operand_gsi (&gsi, expr,
3396 adj->by_ref
3397 || is_gimple_reg_type (adj->type),
3398 NULL, true, GSI_SAME_STMT);
3399 vargs.quick_push (expr);
3401 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3403 unsigned int ix;
3404 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3405 gimple def_temp;
3407 arg = gimple_call_arg (stmt, adj->base_index);
3408 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3410 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3411 continue;
3412 arg = fold_convert_loc (gimple_location (stmt),
3413 TREE_TYPE (origin), arg);
3415 if (debug_args == NULL)
3416 debug_args = decl_debug_args_insert (callee_decl);
3417 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3418 if (ddecl == origin)
3420 ddecl = (**debug_args)[ix + 1];
3421 break;
3423 if (ddecl == NULL)
3425 ddecl = make_node (DEBUG_EXPR_DECL);
3426 DECL_ARTIFICIAL (ddecl) = 1;
3427 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3428 DECL_MODE (ddecl) = DECL_MODE (origin);
3430 vec_safe_push (*debug_args, origin);
3431 vec_safe_push (*debug_args, ddecl);
3433 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3434 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3438 if (dump_file && (dump_flags & TDF_DETAILS))
3440 fprintf (dump_file, "replacing stmt:");
3441 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3444 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3445 vargs.release ();
3446 if (gimple_call_lhs (stmt))
3447 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3449 gimple_set_block (new_stmt, gimple_block (stmt));
3450 if (gimple_has_location (stmt))
3451 gimple_set_location (new_stmt, gimple_location (stmt));
3452 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3453 gimple_call_copy_flags (new_stmt, stmt);
3455 if (dump_file && (dump_flags & TDF_DETAILS))
3457 fprintf (dump_file, "with stmt:");
3458 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3459 fprintf (dump_file, "\n");
3461 gsi_replace (&gsi, new_stmt, true);
3462 if (cs)
3463 cgraph_set_call_stmt (cs, new_stmt);
3466 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
3467 gsi_prev (&gsi);
3469 while ((gsi_end_p (prev_gsi) && !gsi_end_p (gsi))
3470 || (!gsi_end_p (prev_gsi) && gsi_stmt (gsi) == gsi_stmt (prev_gsi)));
3472 update_ssa (TODO_update_ssa);
3473 free_dominance_info (CDI_DOMINATORS);
3476 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3478 static bool
3479 index_in_adjustments_multiple_times_p (int base_index,
3480 ipa_parm_adjustment_vec adjustments)
3482 int i, len = adjustments.length ();
3483 bool one = false;
3485 for (i = 0; i < len; i++)
3487 struct ipa_parm_adjustment *adj;
3488 adj = &adjustments[i];
3490 if (adj->base_index == base_index)
3492 if (one)
3493 return true;
3494 else
3495 one = true;
3498 return false;
3502 /* Return adjustments that should have the same effect on function parameters
3503 and call arguments as if they were first changed according to adjustments in
3504 INNER and then by adjustments in OUTER. */
3506 ipa_parm_adjustment_vec
3507 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3508 ipa_parm_adjustment_vec outer)
3510 int i, outlen = outer.length ();
3511 int inlen = inner.length ();
3512 int removals = 0;
3513 ipa_parm_adjustment_vec adjustments, tmp;
3515 tmp.create (inlen);
3516 for (i = 0; i < inlen; i++)
3518 struct ipa_parm_adjustment *n;
3519 n = &inner[i];
3521 if (n->remove_param)
3522 removals++;
3523 else
3524 tmp.quick_push (*n);
3527 adjustments.create (outlen + removals);
3528 for (i = 0; i < outlen; i++)
3530 struct ipa_parm_adjustment r;
3531 struct ipa_parm_adjustment *out = &outer[i];
3532 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3534 memset (&r, 0, sizeof (r));
3535 gcc_assert (!in->remove_param);
3536 if (out->remove_param)
3538 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3540 r.remove_param = true;
3541 adjustments.quick_push (r);
3543 continue;
3546 r.base_index = in->base_index;
3547 r.type = out->type;
3549 /* FIXME: Create nonlocal value too. */
3551 if (in->copy_param && out->copy_param)
3552 r.copy_param = true;
3553 else if (in->copy_param)
3554 r.offset = out->offset;
3555 else if (out->copy_param)
3556 r.offset = in->offset;
3557 else
3558 r.offset = in->offset + out->offset;
3559 adjustments.quick_push (r);
3562 for (i = 0; i < inlen; i++)
3564 struct ipa_parm_adjustment *n = &inner[i];
3566 if (n->remove_param)
3567 adjustments.quick_push (*n);
3570 tmp.release ();
3571 return adjustments;
3574 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3575 friendly way, assuming they are meant to be applied to FNDECL. */
3577 void
3578 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3579 tree fndecl)
3581 int i, len = adjustments.length ();
3582 bool first = true;
3583 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3585 fprintf (file, "IPA param adjustments: ");
3586 for (i = 0; i < len; i++)
3588 struct ipa_parm_adjustment *adj;
3589 adj = &adjustments[i];
3591 if (!first)
3592 fprintf (file, " ");
3593 else
3594 first = false;
3596 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3597 print_generic_expr (file, parms[adj->base_index], 0);
3598 if (adj->base)
3600 fprintf (file, ", base: ");
3601 print_generic_expr (file, adj->base, 0);
3603 if (adj->reduction)
3605 fprintf (file, ", reduction: ");
3606 print_generic_expr (file, adj->reduction, 0);
3608 if (adj->new_ssa_base)
3610 fprintf (file, ", new_ssa_base: ");
3611 print_generic_expr (file, adj->new_ssa_base, 0);
3614 if (adj->copy_param)
3615 fprintf (file, ", copy_param");
3616 else if (adj->remove_param)
3617 fprintf (file, ", remove_param");
3618 else
3619 fprintf (file, ", offset %li", (long) adj->offset);
3620 if (adj->by_ref)
3621 fprintf (file, ", by_ref");
3622 print_node_brief (file, ", type: ", adj->type, 0);
3623 fprintf (file, "\n");
3625 parms.release ();
3628 /* Dump the AV linked list. */
3630 void
3631 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3633 bool comma = false;
3634 fprintf (f, " Aggregate replacements:");
3635 for (; av; av = av->next)
3637 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3638 av->index, av->offset);
3639 print_generic_expr (f, av->value, 0);
3640 comma = true;
3642 fprintf (f, "\n");
3645 /* Stream out jump function JUMP_FUNC to OB. */
3647 static void
3648 ipa_write_jump_function (struct output_block *ob,
3649 struct ipa_jump_func *jump_func)
3651 struct ipa_agg_jf_item *item;
3652 struct bitpack_d bp;
3653 int i, count;
3655 streamer_write_uhwi (ob, jump_func->type);
3656 switch (jump_func->type)
3658 case IPA_JF_UNKNOWN:
3659 break;
3660 case IPA_JF_KNOWN_TYPE:
3661 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3662 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3663 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3664 break;
3665 case IPA_JF_CONST:
3666 gcc_assert (
3667 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
3668 stream_write_tree (ob, jump_func->value.constant.value, true);
3669 break;
3670 case IPA_JF_PASS_THROUGH:
3671 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3672 if (jump_func->value.pass_through.operation == NOP_EXPR)
3674 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3675 bp = bitpack_create (ob->main_stream);
3676 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3677 streamer_write_bitpack (&bp);
3679 else
3681 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3682 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3684 break;
3685 case IPA_JF_ANCESTOR:
3686 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3687 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3688 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3689 bp = bitpack_create (ob->main_stream);
3690 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3691 streamer_write_bitpack (&bp);
3692 break;
3695 count = vec_safe_length (jump_func->agg.items);
3696 streamer_write_uhwi (ob, count);
3697 if (count)
3699 bp = bitpack_create (ob->main_stream);
3700 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3701 streamer_write_bitpack (&bp);
3704 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3706 streamer_write_uhwi (ob, item->offset);
3707 stream_write_tree (ob, item->value, true);
3711 /* Read in jump function JUMP_FUNC from IB. */
3713 static void
3714 ipa_read_jump_function (struct lto_input_block *ib,
3715 struct ipa_jump_func *jump_func,
3716 struct cgraph_edge *cs,
3717 struct data_in *data_in)
3719 enum jump_func_type jftype;
3720 enum tree_code operation;
3721 int i, count;
3723 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
3724 switch (jftype)
3726 case IPA_JF_UNKNOWN:
3727 jump_func->type = IPA_JF_UNKNOWN;
3728 break;
3729 case IPA_JF_KNOWN_TYPE:
3731 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
3732 tree base_type = stream_read_tree (ib, data_in);
3733 tree component_type = stream_read_tree (ib, data_in);
3735 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
3736 break;
3738 case IPA_JF_CONST:
3739 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
3740 break;
3741 case IPA_JF_PASS_THROUGH:
3742 operation = (enum tree_code) streamer_read_uhwi (ib);
3743 if (operation == NOP_EXPR)
3745 int formal_id = streamer_read_uhwi (ib);
3746 struct bitpack_d bp = streamer_read_bitpack (ib);
3747 bool agg_preserved = bp_unpack_value (&bp, 1);
3748 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved);
3750 else
3752 tree operand = stream_read_tree (ib, data_in);
3753 int formal_id = streamer_read_uhwi (ib);
3754 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
3755 operation);
3757 break;
3758 case IPA_JF_ANCESTOR:
3760 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
3761 tree type = stream_read_tree (ib, data_in);
3762 int formal_id = streamer_read_uhwi (ib);
3763 struct bitpack_d bp = streamer_read_bitpack (ib);
3764 bool agg_preserved = bp_unpack_value (&bp, 1);
3766 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved);
3767 break;
3771 count = streamer_read_uhwi (ib);
3772 vec_alloc (jump_func->agg.items, count);
3773 if (count)
3775 struct bitpack_d bp = streamer_read_bitpack (ib);
3776 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
3778 for (i = 0; i < count; i++)
3780 struct ipa_agg_jf_item item;
3781 item.offset = streamer_read_uhwi (ib);
3782 item.value = stream_read_tree (ib, data_in);
3783 jump_func->agg.items->quick_push (item);
3787 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
3788 relevant to indirect inlining to OB. */
3790 static void
3791 ipa_write_indirect_edge_info (struct output_block *ob,
3792 struct cgraph_edge *cs)
3794 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3795 struct bitpack_d bp;
3797 streamer_write_hwi (ob, ii->param_index);
3798 streamer_write_hwi (ob, ii->offset);
3799 bp = bitpack_create (ob->main_stream);
3800 bp_pack_value (&bp, ii->polymorphic, 1);
3801 bp_pack_value (&bp, ii->agg_contents, 1);
3802 bp_pack_value (&bp, ii->member_ptr, 1);
3803 bp_pack_value (&bp, ii->by_ref, 1);
3804 streamer_write_bitpack (&bp);
3806 if (ii->polymorphic)
3808 streamer_write_hwi (ob, ii->otr_token);
3809 stream_write_tree (ob, ii->otr_type, true);
3813 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
3814 relevant to indirect inlining from IB. */
3816 static void
3817 ipa_read_indirect_edge_info (struct lto_input_block *ib,
3818 struct data_in *data_in ATTRIBUTE_UNUSED,
3819 struct cgraph_edge *cs)
3821 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3822 struct bitpack_d bp;
3824 ii->param_index = (int) streamer_read_hwi (ib);
3825 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
3826 bp = streamer_read_bitpack (ib);
3827 ii->polymorphic = bp_unpack_value (&bp, 1);
3828 ii->agg_contents = bp_unpack_value (&bp, 1);
3829 ii->member_ptr = bp_unpack_value (&bp, 1);
3830 ii->by_ref = bp_unpack_value (&bp, 1);
3831 if (ii->polymorphic)
3833 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
3834 ii->otr_type = stream_read_tree (ib, data_in);
3838 /* Stream out NODE info to OB. */
3840 static void
3841 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
3843 int node_ref;
3844 lto_symtab_encoder_t encoder;
3845 struct ipa_node_params *info = IPA_NODE_REF (node);
3846 int j;
3847 struct cgraph_edge *e;
3848 struct bitpack_d bp;
3850 encoder = ob->decl_state->symtab_node_encoder;
3851 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
3852 streamer_write_uhwi (ob, node_ref);
3854 bp = bitpack_create (ob->main_stream);
3855 gcc_assert (info->uses_analysis_done
3856 || ipa_get_param_count (info) == 0);
3857 gcc_assert (!info->node_enqueued);
3858 gcc_assert (!info->ipcp_orig_node);
3859 for (j = 0; j < ipa_get_param_count (info); j++)
3860 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
3861 streamer_write_bitpack (&bp);
3862 for (j = 0; j < ipa_get_param_count (info); j++)
3863 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
3864 for (e = node->callees; e; e = e->next_callee)
3866 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3868 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3869 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3870 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3872 for (e = node->indirect_calls; e; e = e->next_callee)
3874 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3876 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3877 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3878 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3879 ipa_write_indirect_edge_info (ob, e);
3883 /* Stream in NODE info from IB. */
3885 static void
3886 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
3887 struct data_in *data_in)
3889 struct ipa_node_params *info = IPA_NODE_REF (node);
3890 int k;
3891 struct cgraph_edge *e;
3892 struct bitpack_d bp;
3894 ipa_initialize_node_params (node);
3896 bp = streamer_read_bitpack (ib);
3897 if (ipa_get_param_count (info) != 0)
3898 info->uses_analysis_done = true;
3899 info->node_enqueued = false;
3900 for (k = 0; k < ipa_get_param_count (info); k++)
3901 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
3902 for (k = 0; k < ipa_get_param_count (info); k++)
3903 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
3904 for (e = node->callees; e; e = e->next_callee)
3906 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3907 int count = streamer_read_uhwi (ib);
3909 if (!count)
3910 continue;
3911 vec_safe_grow_cleared (args->jump_functions, count);
3913 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3914 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
3915 data_in);
3917 for (e = node->indirect_calls; e; e = e->next_callee)
3919 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3920 int count = streamer_read_uhwi (ib);
3922 if (count)
3924 vec_safe_grow_cleared (args->jump_functions, count);
3925 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3926 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
3927 data_in);
3929 ipa_read_indirect_edge_info (ib, data_in, e);
3933 /* Write jump functions for nodes in SET. */
3935 void
3936 ipa_prop_write_jump_functions (void)
3938 struct cgraph_node *node;
3939 struct output_block *ob;
3940 unsigned int count = 0;
3941 lto_symtab_encoder_iterator lsei;
3942 lto_symtab_encoder_t encoder;
3945 if (!ipa_node_params_vector.exists ())
3946 return;
3948 ob = create_output_block (LTO_section_jump_functions);
3949 encoder = ob->decl_state->symtab_node_encoder;
3950 ob->cgraph_node = NULL;
3951 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3952 lsei_next_function_in_partition (&lsei))
3954 node = lsei_cgraph_node (lsei);
3955 if (cgraph_function_with_gimple_body_p (node)
3956 && IPA_NODE_REF (node) != NULL)
3957 count++;
3960 streamer_write_uhwi (ob, count);
3962 /* Process all of the functions. */
3963 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3964 lsei_next_function_in_partition (&lsei))
3966 node = lsei_cgraph_node (lsei);
3967 if (cgraph_function_with_gimple_body_p (node)
3968 && IPA_NODE_REF (node) != NULL)
3969 ipa_write_node_info (ob, node);
3971 streamer_write_char_stream (ob->main_stream, 0);
3972 produce_asm (ob, NULL);
3973 destroy_output_block (ob);
3976 /* Read section in file FILE_DATA of length LEN with data DATA. */
3978 static void
3979 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
3980 size_t len)
3982 const struct lto_function_header *header =
3983 (const struct lto_function_header *) data;
3984 const int cfg_offset = sizeof (struct lto_function_header);
3985 const int main_offset = cfg_offset + header->cfg_size;
3986 const int string_offset = main_offset + header->main_size;
3987 struct data_in *data_in;
3988 struct lto_input_block ib_main;
3989 unsigned int i;
3990 unsigned int count;
3992 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
3993 header->main_size);
3995 data_in =
3996 lto_data_in_create (file_data, (const char *) data + string_offset,
3997 header->string_size, vNULL);
3998 count = streamer_read_uhwi (&ib_main);
4000 for (i = 0; i < count; i++)
4002 unsigned int index;
4003 struct cgraph_node *node;
4004 lto_symtab_encoder_t encoder;
4006 index = streamer_read_uhwi (&ib_main);
4007 encoder = file_data->symtab_node_encoder;
4008 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4009 gcc_assert (node->symbol.definition);
4010 ipa_read_node_info (&ib_main, node, data_in);
4012 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4013 len);
4014 lto_data_in_delete (data_in);
4017 /* Read ipcp jump functions. */
4019 void
4020 ipa_prop_read_jump_functions (void)
4022 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4023 struct lto_file_decl_data *file_data;
4024 unsigned int j = 0;
4026 ipa_check_create_node_params ();
4027 ipa_check_create_edge_args ();
4028 ipa_register_cgraph_hooks ();
4030 while ((file_data = file_data_vec[j++]))
4032 size_t len;
4033 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4035 if (data)
4036 ipa_prop_read_section (file_data, data, len);
4040 /* After merging units, we can get mismatch in argument counts.
4041 Also decl merging might've rendered parameter lists obsolete.
4042 Also compute called_with_variable_arg info. */
4044 void
4045 ipa_update_after_lto_read (void)
4047 struct cgraph_node *node;
4049 ipa_check_create_node_params ();
4050 ipa_check_create_edge_args ();
4052 FOR_EACH_DEFINED_FUNCTION (node)
4053 ipa_initialize_node_params (node);
4056 void
4057 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4059 int node_ref;
4060 unsigned int count = 0;
4061 lto_symtab_encoder_t encoder;
4062 struct ipa_agg_replacement_value *aggvals, *av;
4064 aggvals = ipa_get_agg_replacements_for_node (node);
4065 encoder = ob->decl_state->symtab_node_encoder;
4066 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
4067 streamer_write_uhwi (ob, node_ref);
4069 for (av = aggvals; av; av = av->next)
4070 count++;
4071 streamer_write_uhwi (ob, count);
4073 for (av = aggvals; av; av = av->next)
4075 struct bitpack_d bp;
4077 streamer_write_uhwi (ob, av->offset);
4078 streamer_write_uhwi (ob, av->index);
4079 stream_write_tree (ob, av->value, true);
4081 bp = bitpack_create (ob->main_stream);
4082 bp_pack_value (&bp, av->by_ref, 1);
4083 streamer_write_bitpack (&bp);
4087 /* Stream in the aggregate value replacement chain for NODE from IB. */
4089 static void
4090 read_agg_replacement_chain (struct lto_input_block *ib,
4091 struct cgraph_node *node,
4092 struct data_in *data_in)
4094 struct ipa_agg_replacement_value *aggvals = NULL;
4095 unsigned int count, i;
4097 count = streamer_read_uhwi (ib);
4098 for (i = 0; i <count; i++)
4100 struct ipa_agg_replacement_value *av;
4101 struct bitpack_d bp;
4103 av = ggc_alloc_ipa_agg_replacement_value ();
4104 av->offset = streamer_read_uhwi (ib);
4105 av->index = streamer_read_uhwi (ib);
4106 av->value = stream_read_tree (ib, data_in);
4107 bp = streamer_read_bitpack (ib);
4108 av->by_ref = bp_unpack_value (&bp, 1);
4109 av->next = aggvals;
4110 aggvals = av;
4112 ipa_set_node_agg_value_chain (node, aggvals);
4115 /* Write all aggregate replacement for nodes in set. */
4117 void
4118 ipa_prop_write_all_agg_replacement (void)
4120 struct cgraph_node *node;
4121 struct output_block *ob;
4122 unsigned int count = 0;
4123 lto_symtab_encoder_iterator lsei;
4124 lto_symtab_encoder_t encoder;
4126 if (!ipa_node_agg_replacements)
4127 return;
4129 ob = create_output_block (LTO_section_ipcp_transform);
4130 encoder = ob->decl_state->symtab_node_encoder;
4131 ob->cgraph_node = NULL;
4132 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4133 lsei_next_function_in_partition (&lsei))
4135 node = lsei_cgraph_node (lsei);
4136 if (cgraph_function_with_gimple_body_p (node)
4137 && ipa_get_agg_replacements_for_node (node) != NULL)
4138 count++;
4141 streamer_write_uhwi (ob, count);
4143 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4144 lsei_next_function_in_partition (&lsei))
4146 node = lsei_cgraph_node (lsei);
4147 if (cgraph_function_with_gimple_body_p (node)
4148 && ipa_get_agg_replacements_for_node (node) != NULL)
4149 write_agg_replacement_chain (ob, node);
4151 streamer_write_char_stream (ob->main_stream, 0);
4152 produce_asm (ob, NULL);
4153 destroy_output_block (ob);
4156 /* Read replacements section in file FILE_DATA of length LEN with data
4157 DATA. */
4159 static void
4160 read_replacements_section (struct lto_file_decl_data *file_data,
4161 const char *data,
4162 size_t len)
4164 const struct lto_function_header *header =
4165 (const struct lto_function_header *) data;
4166 const int cfg_offset = sizeof (struct lto_function_header);
4167 const int main_offset = cfg_offset + header->cfg_size;
4168 const int string_offset = main_offset + header->main_size;
4169 struct data_in *data_in;
4170 struct lto_input_block ib_main;
4171 unsigned int i;
4172 unsigned int count;
4174 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4175 header->main_size);
4177 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4178 header->string_size, vNULL);
4179 count = streamer_read_uhwi (&ib_main);
4181 for (i = 0; i < count; i++)
4183 unsigned int index;
4184 struct cgraph_node *node;
4185 lto_symtab_encoder_t encoder;
4187 index = streamer_read_uhwi (&ib_main);
4188 encoder = file_data->symtab_node_encoder;
4189 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4190 gcc_assert (node->symbol.definition);
4191 read_agg_replacement_chain (&ib_main, node, data_in);
4193 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4194 len);
4195 lto_data_in_delete (data_in);
4198 /* Read IPA-CP aggregate replacements. */
4200 void
4201 ipa_prop_read_all_agg_replacement (void)
4203 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4204 struct lto_file_decl_data *file_data;
4205 unsigned int j = 0;
4207 while ((file_data = file_data_vec[j++]))
4209 size_t len;
4210 const char *data = lto_get_section_data (file_data,
4211 LTO_section_ipcp_transform,
4212 NULL, &len);
4213 if (data)
4214 read_replacements_section (file_data, data, len);
4218 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4219 NODE. */
4221 static void
4222 adjust_agg_replacement_values (struct cgraph_node *node,
4223 struct ipa_agg_replacement_value *aggval)
4225 struct ipa_agg_replacement_value *v;
4226 int i, c = 0, d = 0, *adj;
4228 if (!node->clone.combined_args_to_skip)
4229 return;
4231 for (v = aggval; v; v = v->next)
4233 gcc_assert (v->index >= 0);
4234 if (c < v->index)
4235 c = v->index;
4237 c++;
4239 adj = XALLOCAVEC (int, c);
4240 for (i = 0; i < c; i++)
4241 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4243 adj[i] = -1;
4244 d++;
4246 else
4247 adj[i] = i - d;
4249 for (v = aggval; v; v = v->next)
4250 v->index = adj[v->index];
4254 /* Function body transformation phase. */
4256 unsigned int
4257 ipcp_transform_function (struct cgraph_node *node)
4259 vec<ipa_param_descriptor_t> descriptors = vNULL;
4260 struct param_analysis_info *parms_ainfo;
4261 struct ipa_agg_replacement_value *aggval;
4262 gimple_stmt_iterator gsi;
4263 basic_block bb;
4264 int param_count;
4265 bool cfg_changed = false, something_changed = false;
4267 gcc_checking_assert (cfun);
4268 gcc_checking_assert (current_function_decl);
4270 if (dump_file)
4271 fprintf (dump_file, "Modification phase of node %s/%i\n",
4272 cgraph_node_name (node), node->symbol.order);
4274 aggval = ipa_get_agg_replacements_for_node (node);
4275 if (!aggval)
4276 return 0;
4277 param_count = count_formal_params (node->symbol.decl);
4278 if (param_count == 0)
4279 return 0;
4280 adjust_agg_replacement_values (node, aggval);
4281 if (dump_file)
4282 ipa_dump_agg_replacement_values (dump_file, aggval);
4283 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
4284 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
4285 descriptors.safe_grow_cleared (param_count);
4286 ipa_populate_param_decls (node, descriptors);
4288 FOR_EACH_BB (bb)
4289 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4291 struct ipa_agg_replacement_value *v;
4292 gimple stmt = gsi_stmt (gsi);
4293 tree rhs, val, t;
4294 HOST_WIDE_INT offset;
4295 int index;
4296 bool by_ref, vce;
4298 if (!gimple_assign_load_p (stmt))
4299 continue;
4300 rhs = gimple_assign_rhs1 (stmt);
4301 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
4302 continue;
4304 vce = false;
4305 t = rhs;
4306 while (handled_component_p (t))
4308 /* V_C_E can do things like convert an array of integers to one
4309 bigger integer and similar things we do not handle below. */
4310 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
4312 vce = true;
4313 break;
4315 t = TREE_OPERAND (t, 0);
4317 if (vce)
4318 continue;
4320 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
4321 rhs, &index, &offset, &by_ref))
4322 continue;
4323 for (v = aggval; v; v = v->next)
4324 if (v->index == index
4325 && v->offset == offset)
4326 break;
4327 if (!v || v->by_ref != by_ref)
4328 continue;
4330 gcc_checking_assert (is_gimple_ip_invariant (v->value));
4331 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
4333 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
4334 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
4335 else if (TYPE_SIZE (TREE_TYPE (rhs))
4336 == TYPE_SIZE (TREE_TYPE (v->value)))
4337 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
4338 else
4340 if (dump_file)
4342 fprintf (dump_file, " const ");
4343 print_generic_expr (dump_file, v->value, 0);
4344 fprintf (dump_file, " can't be converted to type of ");
4345 print_generic_expr (dump_file, rhs, 0);
4346 fprintf (dump_file, "\n");
4348 continue;
4351 else
4352 val = v->value;
4354 if (dump_file && (dump_flags & TDF_DETAILS))
4356 fprintf (dump_file, "Modifying stmt:\n ");
4357 print_gimple_stmt (dump_file, stmt, 0, 0);
4359 gimple_assign_set_rhs_from_tree (&gsi, val);
4360 update_stmt (stmt);
4362 if (dump_file && (dump_flags & TDF_DETAILS))
4364 fprintf (dump_file, "into:\n ");
4365 print_gimple_stmt (dump_file, stmt, 0, 0);
4366 fprintf (dump_file, "\n");
4369 something_changed = true;
4370 if (maybe_clean_eh_stmt (stmt)
4371 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4372 cfg_changed = true;
4375 (*ipa_node_agg_replacements)[node->uid] = NULL;
4376 free_parms_ainfo (parms_ainfo, param_count);
4377 descriptors.release ();
4379 if (!something_changed)
4380 return 0;
4381 else if (cfg_changed)
4382 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
4383 else
4384 return TODO_update_ssa_only_virtuals;