2012-02-25 Catherine Moore <clm@codesourcery.com>
[official-gcc.git] / gcc / ipa-prop.c
blobf68349363b09596018bf2003995caa55a8358b80
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 /* Return index of the formal whose tree is PTREE in function which corresponds
66 to INFO. */
68 static int
69 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor_t> descriptors, tree ptree)
71 int i, count;
73 count = descriptors.length ();
74 for (i = 0; i < count; i++)
75 if (descriptors[i].decl == ptree)
76 return i;
78 return -1;
81 /* Return index of the formal whose tree is PTREE in function which corresponds
82 to INFO. */
84 int
85 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
87 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
90 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
91 NODE. */
93 static void
94 ipa_populate_param_decls (struct cgraph_node *node,
95 vec<ipa_param_descriptor_t> &descriptors)
97 tree fndecl;
98 tree fnargs;
99 tree parm;
100 int param_num;
102 fndecl = node->symbol.decl;
103 fnargs = DECL_ARGUMENTS (fndecl);
104 param_num = 0;
105 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
107 descriptors[param_num].decl = parm;
108 param_num++;
112 /* Return how many formal parameters FNDECL has. */
114 static inline int
115 count_formal_params (tree fndecl)
117 tree parm;
118 int count = 0;
120 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
121 count++;
123 return count;
126 /* Initialize the ipa_node_params structure associated with NODE by counting
127 the function parameters, creating the descriptors and populating their
128 param_decls. */
130 void
131 ipa_initialize_node_params (struct cgraph_node *node)
133 struct ipa_node_params *info = IPA_NODE_REF (node);
135 if (!info->descriptors.exists ())
137 int param_count;
139 param_count = count_formal_params (node->symbol.decl);
140 if (param_count)
142 info->descriptors.safe_grow_cleared (param_count);
143 ipa_populate_param_decls (node, info->descriptors);
148 /* Print the jump functions associated with call graph edge CS to file F. */
150 static void
151 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
153 int i, count;
155 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
156 for (i = 0; i < count; i++)
158 struct ipa_jump_func *jump_func;
159 enum jump_func_type type;
161 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
162 type = jump_func->type;
164 fprintf (f, " param %d: ", i);
165 if (type == IPA_JF_UNKNOWN)
166 fprintf (f, "UNKNOWN\n");
167 else if (type == IPA_JF_KNOWN_TYPE)
169 fprintf (f, "KNOWN TYPE: base ");
170 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
171 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
172 jump_func->value.known_type.offset);
173 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
174 fprintf (f, "\n");
176 else if (type == IPA_JF_CONST)
178 tree val = jump_func->value.constant;
179 fprintf (f, "CONST: ");
180 print_generic_expr (f, val, 0);
181 if (TREE_CODE (val) == ADDR_EXPR
182 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
184 fprintf (f, " -> ");
185 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
188 fprintf (f, "\n");
190 else if (type == IPA_JF_PASS_THROUGH)
192 fprintf (f, "PASS THROUGH: ");
193 fprintf (f, "%d, op %s",
194 jump_func->value.pass_through.formal_id,
195 tree_code_name[(int)
196 jump_func->value.pass_through.operation]);
197 if (jump_func->value.pass_through.operation != NOP_EXPR)
199 fprintf (f, " ");
200 print_generic_expr (f,
201 jump_func->value.pass_through.operand, 0);
203 if (jump_func->value.pass_through.agg_preserved)
204 fprintf (f, ", agg_preserved");
205 fprintf (f, "\n");
207 else if (type == IPA_JF_ANCESTOR)
209 fprintf (f, "ANCESTOR: ");
210 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
211 jump_func->value.ancestor.formal_id,
212 jump_func->value.ancestor.offset);
213 print_generic_expr (f, jump_func->value.ancestor.type, 0);
214 if (jump_func->value.ancestor.agg_preserved)
215 fprintf (f, ", agg_preserved");
216 fprintf (f, "\n");
219 if (jump_func->agg.items)
221 struct ipa_agg_jf_item *item;
222 int j;
224 fprintf (f, " Aggregate passed by %s:\n",
225 jump_func->agg.by_ref ? "reference" : "value");
226 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
228 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
229 item->offset);
230 if (TYPE_P (item->value))
231 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
232 tree_low_cst (TYPE_SIZE (item->value), 1));
233 else
235 fprintf (f, "cst: ");
236 print_generic_expr (f, item->value, 0);
238 fprintf (f, "\n");
245 /* Print the jump functions of all arguments on all call graph edges going from
246 NODE to file F. */
248 void
249 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
251 struct cgraph_edge *cs;
252 int i;
254 fprintf (f, " Jump functions of caller %s:\n", cgraph_node_name (node));
255 for (cs = node->callees; cs; cs = cs->next_callee)
257 if (!ipa_edge_args_info_available_for_edge_p (cs))
258 continue;
260 fprintf (f, " callsite %s/%i -> %s/%i : \n",
261 xstrdup (cgraph_node_name (node)), node->uid,
262 xstrdup (cgraph_node_name (cs->callee)), cs->callee->uid);
263 ipa_print_node_jump_functions_for_edge (f, cs);
266 for (cs = node->indirect_calls, i = 0; cs; cs = cs->next_callee, i++)
268 if (!ipa_edge_args_info_available_for_edge_p (cs))
269 continue;
271 if (cs->call_stmt)
273 fprintf (f, " indirect callsite %d for stmt ", i);
274 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
276 else
277 fprintf (f, " indirect callsite %d :\n", i);
278 ipa_print_node_jump_functions_for_edge (f, cs);
283 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
285 void
286 ipa_print_all_jump_functions (FILE *f)
288 struct cgraph_node *node;
290 fprintf (f, "\nJump functions:\n");
291 FOR_EACH_FUNCTION (node)
293 ipa_print_node_jump_functions (f, node);
297 /* Set JFUNC to be a known type jump function. */
299 static void
300 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
301 tree base_type, tree component_type)
303 jfunc->type = IPA_JF_KNOWN_TYPE;
304 jfunc->value.known_type.offset = offset,
305 jfunc->value.known_type.base_type = base_type;
306 jfunc->value.known_type.component_type = component_type;
309 /* Set JFUNC to be a constant jmp function. */
311 static void
312 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant)
314 constant = unshare_expr (constant);
315 if (constant && EXPR_P (constant))
316 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
317 jfunc->type = IPA_JF_CONST;
318 jfunc->value.constant = unshare_expr_without_location (constant);
321 /* Set JFUNC to be a simple pass-through jump function. */
322 static void
323 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
324 bool agg_preserved)
326 jfunc->type = IPA_JF_PASS_THROUGH;
327 jfunc->value.pass_through.operand = NULL_TREE;
328 jfunc->value.pass_through.formal_id = formal_id;
329 jfunc->value.pass_through.operation = NOP_EXPR;
330 jfunc->value.pass_through.agg_preserved = agg_preserved;
333 /* Set JFUNC to be an arithmetic pass through jump function. */
335 static void
336 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
337 tree operand, enum tree_code operation)
339 jfunc->type = IPA_JF_PASS_THROUGH;
340 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
341 jfunc->value.pass_through.formal_id = formal_id;
342 jfunc->value.pass_through.operation = operation;
343 jfunc->value.pass_through.agg_preserved = false;
346 /* Set JFUNC to be an ancestor jump function. */
348 static void
349 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
350 tree type, int formal_id, bool agg_preserved)
352 jfunc->type = IPA_JF_ANCESTOR;
353 jfunc->value.ancestor.formal_id = formal_id;
354 jfunc->value.ancestor.offset = offset;
355 jfunc->value.ancestor.type = type;
356 jfunc->value.ancestor.agg_preserved = agg_preserved;
359 /* Structure to be passed in between detect_type_change and
360 check_stmt_for_type_change. */
362 struct type_change_info
364 /* Offset into the object where there is the virtual method pointer we are
365 looking for. */
366 HOST_WIDE_INT offset;
367 /* The declaration or SSA_NAME pointer of the base that we are checking for
368 type change. */
369 tree object;
370 /* If we actually can tell the type that the object has changed to, it is
371 stored in this field. Otherwise it remains NULL_TREE. */
372 tree known_current_type;
373 /* Set to true if dynamic type change has been detected. */
374 bool type_maybe_changed;
375 /* Set to true if multiple types have been encountered. known_current_type
376 must be disregarded in that case. */
377 bool multiple_types_encountered;
380 /* Return true if STMT can modify a virtual method table pointer.
382 This function makes special assumptions about both constructors and
383 destructors which are all the functions that are allowed to alter the VMT
384 pointers. It assumes that destructors begin with assignment into all VMT
385 pointers and that constructors essentially look in the following way:
387 1) The very first thing they do is that they call constructors of ancestor
388 sub-objects that have them.
390 2) Then VMT pointers of this and all its ancestors is set to new values
391 corresponding to the type corresponding to the constructor.
393 3) Only afterwards, other stuff such as constructor of member sub-objects
394 and the code written by the user is run. Only this may include calling
395 virtual functions, directly or indirectly.
397 There is no way to call a constructor of an ancestor sub-object in any
398 other way.
400 This means that we do not have to care whether constructors get the correct
401 type information because they will always change it (in fact, if we define
402 the type to be given by the VMT pointer, it is undefined).
404 The most important fact to derive from the above is that if, for some
405 statement in the section 3, we try to detect whether the dynamic type has
406 changed, we can safely ignore all calls as we examine the function body
407 backwards until we reach statements in section 2 because these calls cannot
408 be ancestor constructors or destructors (if the input is not bogus) and so
409 do not change the dynamic type (this holds true only for automatically
410 allocated objects but at the moment we devirtualize only these). We then
411 must detect that statements in section 2 change the dynamic type and can try
412 to derive the new type. That is enough and we can stop, we will never see
413 the calls into constructors of sub-objects in this code. Therefore we can
414 safely ignore all call statements that we traverse.
417 static bool
418 stmt_may_be_vtbl_ptr_store (gimple stmt)
420 if (is_gimple_call (stmt))
421 return false;
422 else if (is_gimple_assign (stmt))
424 tree lhs = gimple_assign_lhs (stmt);
426 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
428 if (flag_strict_aliasing
429 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
430 return false;
432 if (TREE_CODE (lhs) == COMPONENT_REF
433 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
434 return false;
435 /* In the future we might want to use get_base_ref_and_offset to find
436 if there is a field corresponding to the offset and if so, proceed
437 almost like if it was a component ref. */
440 return true;
443 /* If STMT can be proved to be an assignment to the virtual method table
444 pointer of ANALYZED_OBJ and the type associated with the new table
445 identified, return the type. Otherwise return NULL_TREE. */
447 static tree
448 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
450 HOST_WIDE_INT offset, size, max_size;
451 tree lhs, rhs, base;
453 if (!gimple_assign_single_p (stmt))
454 return NULL_TREE;
456 lhs = gimple_assign_lhs (stmt);
457 rhs = gimple_assign_rhs1 (stmt);
458 if (TREE_CODE (lhs) != COMPONENT_REF
459 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))
460 || TREE_CODE (rhs) != ADDR_EXPR)
461 return NULL_TREE;
462 rhs = get_base_address (TREE_OPERAND (rhs, 0));
463 if (!rhs
464 || TREE_CODE (rhs) != VAR_DECL
465 || !DECL_VIRTUAL_P (rhs))
466 return NULL_TREE;
468 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
469 if (offset != tci->offset
470 || size != POINTER_SIZE
471 || max_size != POINTER_SIZE)
472 return NULL_TREE;
473 if (TREE_CODE (base) == MEM_REF)
475 if (TREE_CODE (tci->object) != MEM_REF
476 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
477 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
478 TREE_OPERAND (base, 1)))
479 return NULL_TREE;
481 else if (tci->object != base)
482 return NULL_TREE;
484 return DECL_CONTEXT (rhs);
487 /* Callback of walk_aliased_vdefs and a helper function for
488 detect_type_change to check whether a particular statement may modify
489 the virtual table pointer, and if possible also determine the new type of
490 the (sub-)object. It stores its result into DATA, which points to a
491 type_change_info structure. */
493 static bool
494 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
496 gimple stmt = SSA_NAME_DEF_STMT (vdef);
497 struct type_change_info *tci = (struct type_change_info *) data;
499 if (stmt_may_be_vtbl_ptr_store (stmt))
501 tree type;
502 type = extr_type_from_vtbl_ptr_store (stmt, tci);
503 if (tci->type_maybe_changed
504 && type != tci->known_current_type)
505 tci->multiple_types_encountered = true;
506 tci->known_current_type = type;
507 tci->type_maybe_changed = true;
508 return true;
510 else
511 return false;
516 /* Like detect_type_change but with extra argument COMP_TYPE which will become
517 the component type part of new JFUNC of dynamic type change is detected and
518 the new base type is identified. */
520 static bool
521 detect_type_change_1 (tree arg, tree base, tree comp_type, gimple call,
522 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
524 struct type_change_info tci;
525 ao_ref ao;
527 gcc_checking_assert (DECL_P (arg)
528 || TREE_CODE (arg) == MEM_REF
529 || handled_component_p (arg));
530 /* Const calls cannot call virtual methods through VMT and so type changes do
531 not matter. */
532 if (!flag_devirtualize || !gimple_vuse (call))
533 return false;
535 ao_ref_init (&ao, arg);
536 ao.base = base;
537 ao.offset = offset;
538 ao.size = POINTER_SIZE;
539 ao.max_size = ao.size;
541 tci.offset = offset;
542 tci.object = get_base_address (arg);
543 tci.known_current_type = NULL_TREE;
544 tci.type_maybe_changed = false;
545 tci.multiple_types_encountered = false;
547 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
548 &tci, NULL);
549 if (!tci.type_maybe_changed)
550 return false;
552 if (!tci.known_current_type
553 || tci.multiple_types_encountered
554 || offset != 0)
555 jfunc->type = IPA_JF_UNKNOWN;
556 else
557 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
559 return true;
562 /* Detect whether the dynamic type of ARG has changed (before callsite CALL) by
563 looking for assignments to its virtual table pointer. If it is, return true
564 and fill in the jump function JFUNC with relevant type information or set it
565 to unknown. ARG is the object itself (not a pointer to it, unless
566 dereferenced). BASE is the base of the memory access as returned by
567 get_ref_base_and_extent, as is the offset. */
569 static bool
570 detect_type_change (tree arg, tree base, gimple call,
571 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
573 return detect_type_change_1 (arg, base, TREE_TYPE (arg), call, jfunc, offset);
576 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
577 SSA name (its dereference will become the base and the offset is assumed to
578 be zero). */
580 static bool
581 detect_type_change_ssa (tree arg, gimple call, struct ipa_jump_func *jfunc)
583 tree comp_type;
585 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
586 if (!flag_devirtualize
587 || !POINTER_TYPE_P (TREE_TYPE (arg))
588 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != RECORD_TYPE)
589 return false;
591 comp_type = TREE_TYPE (TREE_TYPE (arg));
592 arg = build2 (MEM_REF, ptr_type_node, arg,
593 build_int_cst (ptr_type_node, 0));
595 return detect_type_change_1 (arg, arg, comp_type, call, jfunc, 0);
598 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
599 boolean variable pointed to by DATA. */
601 static bool
602 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
603 void *data)
605 bool *b = (bool *) data;
606 *b = true;
607 return true;
610 /* Return true if a load from a formal parameter PARM_LOAD is known to retreive
611 a value known not to be modified in this function before reaching the
612 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
613 information about the parameter. */
615 static bool
616 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
617 gimple stmt, tree parm_load)
619 bool modified = false;
620 bitmap *visited_stmts;
621 ao_ref refd;
623 if (parm_ainfo && parm_ainfo->parm_modified)
624 return false;
626 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
627 ao_ref_init (&refd, parm_load);
628 /* We can cache visited statements only when parm_ainfo is available and when
629 we are looking at a naked load of the whole parameter. */
630 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
631 visited_stmts = NULL;
632 else
633 visited_stmts = &parm_ainfo->parm_visited_statements;
634 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
635 visited_stmts);
636 if (parm_ainfo && modified)
637 parm_ainfo->parm_modified = true;
638 return !modified;
641 /* If STMT is an assignment that loads a value from an parameter declaration,
642 return the index of the parameter in ipa_node_params which has not been
643 modified. Otherwise return -1. */
645 static int
646 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
647 struct param_analysis_info *parms_ainfo,
648 gimple stmt)
650 int index;
651 tree op1;
653 if (!gimple_assign_single_p (stmt))
654 return -1;
656 op1 = gimple_assign_rhs1 (stmt);
657 if (TREE_CODE (op1) != PARM_DECL)
658 return -1;
660 index = ipa_get_param_decl_index_1 (descriptors, op1);
661 if (index < 0
662 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
663 : NULL, stmt, op1))
664 return -1;
666 return index;
669 /* Return true if memory reference REF loads data that are known to be
670 unmodified in this function before reaching statement STMT. PARM_AINFO, if
671 non-NULL, is a pointer to a structure containing temporary information about
672 PARM. */
674 static bool
675 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
676 gimple stmt, tree ref)
678 bool modified = false;
679 ao_ref refd;
681 gcc_checking_assert (gimple_vuse (stmt));
682 if (parm_ainfo && parm_ainfo->ref_modified)
683 return false;
685 ao_ref_init (&refd, ref);
686 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
687 NULL);
688 if (parm_ainfo && modified)
689 parm_ainfo->ref_modified = true;
690 return !modified;
693 /* Return true if the data pointed to by PARM is known to be unmodified in this
694 function before reaching call statement CALL into which it is passed.
695 PARM_AINFO is a pointer to a structure containing temporary information
696 about PARM. */
698 static bool
699 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
700 gimple call, tree parm)
702 bool modified = false;
703 ao_ref refd;
705 /* It's unnecessary to calculate anything about memory contnets for a const
706 function because it is not goin to use it. But do not cache the result
707 either. Also, no such calculations for non-pointers. */
708 if (!gimple_vuse (call)
709 || !POINTER_TYPE_P (TREE_TYPE (parm)))
710 return false;
712 if (parm_ainfo->pt_modified)
713 return false;
715 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
716 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
717 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
718 if (modified)
719 parm_ainfo->pt_modified = true;
720 return !modified;
723 /* Return true if we can prove that OP is a memory reference loading unmodified
724 data from an aggregate passed as a parameter and if the aggregate is passed
725 by reference, that the alias type of the load corresponds to the type of the
726 formal parameter (so that we can rely on this type for TBAA in callers).
727 INFO and PARMS_AINFO describe parameters of the current function (but the
728 latter can be NULL), STMT is the load statement. If function returns true,
729 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
730 within the aggregate and whether it is a load from a value passed by
731 reference respectively. */
733 static bool
734 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
735 struct param_analysis_info *parms_ainfo, gimple stmt,
736 tree op, int *index_p, HOST_WIDE_INT *offset_p,
737 bool *by_ref_p)
739 int index;
740 HOST_WIDE_INT size, max_size;
741 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
743 if (max_size == -1 || max_size != size || *offset_p < 0)
744 return false;
746 if (DECL_P (base))
748 int index = ipa_get_param_decl_index_1 (descriptors, base);
749 if (index >= 0
750 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
751 : NULL, stmt, op))
753 *index_p = index;
754 *by_ref_p = false;
755 return true;
757 return false;
760 if (TREE_CODE (base) != MEM_REF
761 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
762 || !integer_zerop (TREE_OPERAND (base, 1)))
763 return false;
765 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
767 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
768 index = ipa_get_param_decl_index_1 (descriptors, parm);
770 else
772 /* This branch catches situations where a pointer parameter is not a
773 gimple register, for example:
775 void hip7(S*) (struct S * p)
777 void (*<T2e4>) (struct S *) D.1867;
778 struct S * p.1;
780 <bb 2>:
781 p.1_1 = p;
782 D.1867_2 = p.1_1->f;
783 D.1867_2 ();
784 gdp = &p;
787 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
788 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
791 if (index >= 0
792 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
793 stmt, op))
795 *index_p = index;
796 *by_ref_p = true;
797 return true;
799 return false;
802 /* Just like the previous function, just without the param_analysis_info
803 pointer, for users outside of this file. */
805 bool
806 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
807 tree op, int *index_p, HOST_WIDE_INT *offset_p,
808 bool *by_ref_p)
810 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
811 offset_p, by_ref_p);
814 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
815 of an assignment statement STMT, try to determine whether we are actually
816 handling any of the following cases and construct an appropriate jump
817 function into JFUNC if so:
819 1) The passed value is loaded from a formal parameter which is not a gimple
820 register (most probably because it is addressable, the value has to be
821 scalar) and we can guarantee the value has not changed. This case can
822 therefore be described by a simple pass-through jump function. For example:
824 foo (int a)
826 int a.0;
828 a.0_2 = a;
829 bar (a.0_2);
831 2) The passed value can be described by a simple arithmetic pass-through
832 jump function. E.g.
834 foo (int a)
836 int D.2064;
838 D.2064_4 = a.1(D) + 4;
839 bar (D.2064_4);
841 This case can also occur in combination of the previous one, e.g.:
843 foo (int a, int z)
845 int a.0;
846 int D.2064;
848 a.0_3 = a;
849 D.2064_4 = a.0_3 + 4;
850 foo (D.2064_4);
852 3) The passed value is an address of an object within another one (which
853 also passed by reference). Such situations are described by an ancestor
854 jump function and describe situations such as:
856 B::foo() (struct B * const this)
858 struct A * D.1845;
860 D.1845_2 = &this_1(D)->D.1748;
861 A::bar (D.1845_2);
863 INFO is the structure describing individual parameters access different
864 stages of IPA optimizations. PARMS_AINFO contains the information that is
865 only needed for intraprocedural analysis. */
867 static void
868 compute_complex_assign_jump_func (struct ipa_node_params *info,
869 struct param_analysis_info *parms_ainfo,
870 struct ipa_jump_func *jfunc,
871 gimple call, gimple stmt, tree name)
873 HOST_WIDE_INT offset, size, max_size;
874 tree op1, tc_ssa, base, ssa;
875 int index;
877 op1 = gimple_assign_rhs1 (stmt);
879 if (TREE_CODE (op1) == SSA_NAME)
881 if (SSA_NAME_IS_DEFAULT_DEF (op1))
882 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
883 else
884 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
885 SSA_NAME_DEF_STMT (op1));
886 tc_ssa = op1;
888 else
890 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
891 tc_ssa = gimple_assign_lhs (stmt);
894 if (index >= 0)
896 tree op2 = gimple_assign_rhs2 (stmt);
898 if (op2)
900 if (!is_gimple_ip_invariant (op2)
901 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
902 && !useless_type_conversion_p (TREE_TYPE (name),
903 TREE_TYPE (op1))))
904 return;
906 ipa_set_jf_arith_pass_through (jfunc, index, op2,
907 gimple_assign_rhs_code (stmt));
909 else if (gimple_assign_single_p (stmt)
910 && !detect_type_change_ssa (tc_ssa, call, jfunc))
912 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
913 call, tc_ssa);
914 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
916 return;
919 if (TREE_CODE (op1) != ADDR_EXPR)
920 return;
921 op1 = TREE_OPERAND (op1, 0);
922 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
923 return;
924 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
925 if (TREE_CODE (base) != MEM_REF
926 /* If this is a varying address, punt. */
927 || max_size == -1
928 || max_size != size)
929 return;
930 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
931 ssa = TREE_OPERAND (base, 0);
932 if (TREE_CODE (ssa) != SSA_NAME
933 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
934 || offset < 0)
935 return;
937 /* Dynamic types are changed only in constructors and destructors and */
938 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
939 if (index >= 0
940 && !detect_type_change (op1, base, call, jfunc, offset))
941 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
942 parm_ref_data_pass_through_p (&parms_ainfo[index],
943 call, ssa));
946 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
947 it looks like:
949 iftmp.1_3 = &obj_2(D)->D.1762;
951 The base of the MEM_REF must be a default definition SSA NAME of a
952 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
953 whole MEM_REF expression is returned and the offset calculated from any
954 handled components and the MEM_REF itself is stored into *OFFSET. The whole
955 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
957 static tree
958 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
960 HOST_WIDE_INT size, max_size;
961 tree expr, parm, obj;
963 if (!gimple_assign_single_p (assign))
964 return NULL_TREE;
965 expr = gimple_assign_rhs1 (assign);
967 if (TREE_CODE (expr) != ADDR_EXPR)
968 return NULL_TREE;
969 expr = TREE_OPERAND (expr, 0);
970 obj = expr;
971 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
973 if (TREE_CODE (expr) != MEM_REF
974 /* If this is a varying address, punt. */
975 || max_size == -1
976 || max_size != size
977 || *offset < 0)
978 return NULL_TREE;
979 parm = TREE_OPERAND (expr, 0);
980 if (TREE_CODE (parm) != SSA_NAME
981 || !SSA_NAME_IS_DEFAULT_DEF (parm)
982 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
983 return NULL_TREE;
985 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
986 *obj_p = obj;
987 return expr;
991 /* Given that an actual argument is an SSA_NAME that is a result of a phi
992 statement PHI, try to find out whether NAME is in fact a
993 multiple-inheritance typecast from a descendant into an ancestor of a formal
994 parameter and thus can be described by an ancestor jump function and if so,
995 write the appropriate function into JFUNC.
997 Essentially we want to match the following pattern:
999 if (obj_2(D) != 0B)
1000 goto <bb 3>;
1001 else
1002 goto <bb 4>;
1004 <bb 3>:
1005 iftmp.1_3 = &obj_2(D)->D.1762;
1007 <bb 4>:
1008 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1009 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1010 return D.1879_6; */
1012 static void
1013 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1014 struct param_analysis_info *parms_ainfo,
1015 struct ipa_jump_func *jfunc,
1016 gimple call, gimple phi)
1018 HOST_WIDE_INT offset;
1019 gimple assign, cond;
1020 basic_block phi_bb, assign_bb, cond_bb;
1021 tree tmp, parm, expr, obj;
1022 int index, i;
1024 if (gimple_phi_num_args (phi) != 2)
1025 return;
1027 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1028 tmp = PHI_ARG_DEF (phi, 0);
1029 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1030 tmp = PHI_ARG_DEF (phi, 1);
1031 else
1032 return;
1033 if (TREE_CODE (tmp) != SSA_NAME
1034 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1035 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1036 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1037 return;
1039 assign = SSA_NAME_DEF_STMT (tmp);
1040 assign_bb = gimple_bb (assign);
1041 if (!single_pred_p (assign_bb))
1042 return;
1043 expr = get_ancestor_addr_info (assign, &obj, &offset);
1044 if (!expr)
1045 return;
1046 parm = TREE_OPERAND (expr, 0);
1047 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1048 gcc_assert (index >= 0);
1050 cond_bb = single_pred (assign_bb);
1051 cond = last_stmt (cond_bb);
1052 if (!cond
1053 || gimple_code (cond) != GIMPLE_COND
1054 || gimple_cond_code (cond) != NE_EXPR
1055 || gimple_cond_lhs (cond) != parm
1056 || !integer_zerop (gimple_cond_rhs (cond)))
1057 return;
1059 phi_bb = gimple_bb (phi);
1060 for (i = 0; i < 2; i++)
1062 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1063 if (pred != assign_bb && pred != cond_bb)
1064 return;
1067 if (!detect_type_change (obj, expr, call, jfunc, offset))
1068 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1069 parm_ref_data_pass_through_p (&parms_ainfo[index],
1070 call, parm));
1073 /* Given OP which is passed as an actual argument to a called function,
1074 determine if it is possible to construct a KNOWN_TYPE jump function for it
1075 and if so, create one and store it to JFUNC. */
1077 static void
1078 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1079 gimple call)
1081 HOST_WIDE_INT offset, size, max_size;
1082 tree base;
1084 if (!flag_devirtualize
1085 || TREE_CODE (op) != ADDR_EXPR
1086 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
1087 return;
1089 op = TREE_OPERAND (op, 0);
1090 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1091 if (!DECL_P (base)
1092 || max_size == -1
1093 || max_size != size
1094 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1095 || is_global_var (base))
1096 return;
1098 if (!TYPE_BINFO (TREE_TYPE (base))
1099 || detect_type_change (op, base, call, jfunc, offset))
1100 return;
1102 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base), TREE_TYPE (op));
1105 /* Inspect the given TYPE and return true iff it has the same structure (the
1106 same number of fields of the same types) as a C++ member pointer. If
1107 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1108 corresponding fields there. */
1110 static bool
1111 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1113 tree fld;
1115 if (TREE_CODE (type) != RECORD_TYPE)
1116 return false;
1118 fld = TYPE_FIELDS (type);
1119 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1120 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1121 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1122 return false;
1124 if (method_ptr)
1125 *method_ptr = fld;
1127 fld = DECL_CHAIN (fld);
1128 if (!fld || INTEGRAL_TYPE_P (fld)
1129 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1130 return false;
1131 if (delta)
1132 *delta = fld;
1134 if (DECL_CHAIN (fld))
1135 return false;
1137 return true;
1140 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1141 return the rhs of its defining statement. Otherwise return RHS as it
1142 is. */
1144 static inline tree
1145 get_ssa_def_if_simple_copy (tree rhs)
1147 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1149 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1151 if (gimple_assign_single_p (def_stmt))
1152 rhs = gimple_assign_rhs1 (def_stmt);
1153 else
1154 break;
1156 return rhs;
1159 /* Simple linked list, describing known contents of an aggregate beforere
1160 call. */
1162 struct ipa_known_agg_contents_list
1164 /* Offset and size of the described part of the aggregate. */
1165 HOST_WIDE_INT offset, size;
1166 /* Known constant value or NULL if the contents is known to be unknown. */
1167 tree constant;
1168 /* Pointer to the next structure in the list. */
1169 struct ipa_known_agg_contents_list *next;
1172 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1173 in ARG is filled in with constant values. ARG can either be an aggregate
1174 expression or a pointer to an aggregate. JFUNC is the jump function into
1175 which the constants are subsequently stored. */
1177 static void
1178 determine_known_aggregate_parts (gimple call, tree arg,
1179 struct ipa_jump_func *jfunc)
1181 struct ipa_known_agg_contents_list *list = NULL;
1182 int item_count = 0, const_count = 0;
1183 HOST_WIDE_INT arg_offset, arg_size;
1184 gimple_stmt_iterator gsi;
1185 tree arg_base;
1186 bool check_ref, by_ref;
1187 ao_ref r;
1189 /* The function operates in three stages. First, we prepare check_ref, r,
1190 arg_base and arg_offset based on what is actually passed as an actual
1191 argument. */
1193 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1195 by_ref = true;
1196 if (TREE_CODE (arg) == SSA_NAME)
1198 tree type_size;
1199 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1200 return;
1201 check_ref = true;
1202 arg_base = arg;
1203 arg_offset = 0;
1204 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1205 arg_size = tree_low_cst (type_size, 1);
1206 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1208 else if (TREE_CODE (arg) == ADDR_EXPR)
1210 HOST_WIDE_INT arg_max_size;
1212 arg = TREE_OPERAND (arg, 0);
1213 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1214 &arg_max_size);
1215 if (arg_max_size == -1
1216 || arg_max_size != arg_size
1217 || arg_offset < 0)
1218 return;
1219 if (DECL_P (arg_base))
1221 tree size;
1222 check_ref = false;
1223 size = build_int_cst (integer_type_node, arg_size);
1224 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1226 else
1227 return;
1229 else
1230 return;
1232 else
1234 HOST_WIDE_INT arg_max_size;
1236 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1238 by_ref = false;
1239 check_ref = false;
1240 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1241 &arg_max_size);
1242 if (arg_max_size == -1
1243 || arg_max_size != arg_size
1244 || arg_offset < 0)
1245 return;
1247 ao_ref_init (&r, arg);
1250 /* Second stage walks back the BB, looks at individual statements and as long
1251 as it is confident of how the statements affect contents of the
1252 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1253 describing it. */
1254 gsi = gsi_for_stmt (call);
1255 gsi_prev (&gsi);
1256 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1258 struct ipa_known_agg_contents_list *n, **p;
1259 gimple stmt = gsi_stmt (gsi);
1260 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1261 tree lhs, rhs, lhs_base;
1262 bool partial_overlap;
1264 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1265 continue;
1266 if (!gimple_assign_single_p (stmt))
1267 break;
1269 lhs = gimple_assign_lhs (stmt);
1270 rhs = gimple_assign_rhs1 (stmt);
1271 if (!is_gimple_reg_type (rhs))
1272 break;
1274 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1275 &lhs_max_size);
1276 if (lhs_max_size == -1
1277 || lhs_max_size != lhs_size
1278 || (lhs_offset < arg_offset
1279 && lhs_offset + lhs_size > arg_offset)
1280 || (lhs_offset < arg_offset + arg_size
1281 && lhs_offset + lhs_size > arg_offset + arg_size))
1282 break;
1284 if (check_ref)
1286 if (TREE_CODE (lhs_base) != MEM_REF
1287 || TREE_OPERAND (lhs_base, 0) != arg_base
1288 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1289 break;
1291 else if (lhs_base != arg_base)
1293 if (DECL_P (lhs_base))
1294 continue;
1295 else
1296 break;
1299 if (lhs_offset + lhs_size < arg_offset
1300 || lhs_offset >= (arg_offset + arg_size))
1301 continue;
1303 partial_overlap = false;
1304 p = &list;
1305 while (*p && (*p)->offset < lhs_offset)
1307 if ((*p)->offset + (*p)->size > lhs_offset)
1309 partial_overlap = true;
1310 break;
1312 p = &(*p)->next;
1314 if (partial_overlap)
1315 break;
1316 if (*p && (*p)->offset < lhs_offset + lhs_size)
1318 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1319 /* We already know this value is subsequently overwritten with
1320 something else. */
1321 continue;
1322 else
1323 /* Otherwise this is a partial overlap which we cannot
1324 represent. */
1325 break;
1328 rhs = get_ssa_def_if_simple_copy (rhs);
1329 n = XALLOCA (struct ipa_known_agg_contents_list);
1330 n->size = lhs_size;
1331 n->offset = lhs_offset;
1332 if (is_gimple_ip_invariant (rhs))
1334 n->constant = rhs;
1335 const_count++;
1337 else
1338 n->constant = NULL_TREE;
1339 n->next = *p;
1340 *p = n;
1342 item_count++;
1343 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1344 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1345 break;
1348 /* Third stage just goes over the list and creates an appropriate vector of
1349 ipa_agg_jf_item structures out of it, of sourse only if there are
1350 any known constants to begin with. */
1352 if (const_count)
1354 jfunc->agg.by_ref = by_ref;
1355 vec_alloc (jfunc->agg.items, const_count);
1356 while (list)
1358 if (list->constant)
1360 struct ipa_agg_jf_item item;
1361 item.offset = list->offset - arg_offset;
1362 item.value = unshare_expr_without_location (list->constant);
1363 jfunc->agg.items->quick_push (item);
1365 list = list->next;
1370 /* Compute jump function for all arguments of callsite CS and insert the
1371 information in the jump_functions array in the ipa_edge_args corresponding
1372 to this callsite. */
1374 static void
1375 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1376 struct cgraph_edge *cs)
1378 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1379 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1380 gimple call = cs->call_stmt;
1381 int n, arg_num = gimple_call_num_args (call);
1383 if (arg_num == 0 || args->jump_functions)
1384 return;
1385 vec_safe_grow_cleared (args->jump_functions, arg_num);
1387 for (n = 0; n < arg_num; n++)
1389 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1390 tree arg = gimple_call_arg (call, n);
1392 if (is_gimple_ip_invariant (arg))
1393 ipa_set_jf_constant (jfunc, arg);
1394 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1395 && TREE_CODE (arg) == PARM_DECL)
1397 int index = ipa_get_param_decl_index (info, arg);
1399 gcc_assert (index >=0);
1400 /* Aggregate passed by value, check for pass-through, otherwise we
1401 will attempt to fill in aggregate contents later in this
1402 for cycle. */
1403 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1405 ipa_set_jf_simple_pass_through (jfunc, index, false);
1406 continue;
1409 else if (TREE_CODE (arg) == SSA_NAME)
1411 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1413 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1414 if (index >= 0
1415 && !detect_type_change_ssa (arg, call, jfunc))
1417 bool agg_p;
1418 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1419 call, arg);
1420 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
1423 else
1425 gimple stmt = SSA_NAME_DEF_STMT (arg);
1426 if (is_gimple_assign (stmt))
1427 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1428 call, stmt, arg);
1429 else if (gimple_code (stmt) == GIMPLE_PHI)
1430 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1431 call, stmt);
1434 else
1435 compute_known_type_jump_func (arg, jfunc, call);
1437 if ((jfunc->type != IPA_JF_PASS_THROUGH
1438 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1439 && (jfunc->type != IPA_JF_ANCESTOR
1440 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1441 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1442 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1443 determine_known_aggregate_parts (call, arg, jfunc);
1447 /* Compute jump functions for all edges - both direct and indirect - outgoing
1448 from NODE. Also count the actual arguments in the process. */
1450 static void
1451 ipa_compute_jump_functions (struct cgraph_node *node,
1452 struct param_analysis_info *parms_ainfo)
1454 struct cgraph_edge *cs;
1456 for (cs = node->callees; cs; cs = cs->next_callee)
1458 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1459 NULL);
1460 /* We do not need to bother analyzing calls to unknown
1461 functions unless they may become known during lto/whopr. */
1462 if (!callee->analyzed && !flag_lto)
1463 continue;
1464 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1467 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1468 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1471 /* If STMT looks like a statement loading a value from a member pointer formal
1472 parameter, return that parameter and store the offset of the field to
1473 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1474 might be clobbered). If USE_DELTA, then we look for a use of the delta
1475 field rather than the pfn. */
1477 static tree
1478 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1479 HOST_WIDE_INT *offset_p)
1481 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1483 if (!gimple_assign_single_p (stmt))
1484 return NULL_TREE;
1486 rhs = gimple_assign_rhs1 (stmt);
1487 if (TREE_CODE (rhs) == COMPONENT_REF)
1489 ref_field = TREE_OPERAND (rhs, 1);
1490 rhs = TREE_OPERAND (rhs, 0);
1492 else
1493 ref_field = NULL_TREE;
1494 if (TREE_CODE (rhs) != MEM_REF)
1495 return NULL_TREE;
1496 rec = TREE_OPERAND (rhs, 0);
1497 if (TREE_CODE (rec) != ADDR_EXPR)
1498 return NULL_TREE;
1499 rec = TREE_OPERAND (rec, 0);
1500 if (TREE_CODE (rec) != PARM_DECL
1501 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1502 return NULL_TREE;
1503 ref_offset = TREE_OPERAND (rhs, 1);
1505 if (use_delta)
1506 fld = delta_field;
1507 else
1508 fld = ptr_field;
1509 if (offset_p)
1510 *offset_p = int_bit_position (fld);
1512 if (ref_field)
1514 if (integer_nonzerop (ref_offset))
1515 return NULL_TREE;
1516 return ref_field == fld ? rec : NULL_TREE;
1518 else
1519 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1520 : NULL_TREE;
1523 /* Returns true iff T is an SSA_NAME defined by a statement. */
1525 static bool
1526 ipa_is_ssa_with_stmt_def (tree t)
1528 if (TREE_CODE (t) == SSA_NAME
1529 && !SSA_NAME_IS_DEFAULT_DEF (t))
1530 return true;
1531 else
1532 return false;
1535 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1536 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1537 indirect call graph edge. */
1539 static struct cgraph_edge *
1540 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1542 struct cgraph_edge *cs;
1544 cs = cgraph_edge (node, stmt);
1545 cs->indirect_info->param_index = param_index;
1546 cs->indirect_info->offset = 0;
1547 cs->indirect_info->polymorphic = 0;
1548 cs->indirect_info->agg_contents = 0;
1549 return cs;
1552 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1553 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1554 intermediate information about each formal parameter. Currently it checks
1555 whether the call calls a pointer that is a formal parameter and if so, the
1556 parameter is marked with the called flag and an indirect call graph edge
1557 describing the call is created. This is very simple for ordinary pointers
1558 represented in SSA but not-so-nice when it comes to member pointers. The
1559 ugly part of this function does nothing more than trying to match the
1560 pattern of such a call. An example of such a pattern is the gimple dump
1561 below, the call is on the last line:
1563 <bb 2>:
1564 f$__delta_5 = f.__delta;
1565 f$__pfn_24 = f.__pfn;
1568 <bb 2>:
1569 f$__delta_5 = MEM[(struct *)&f];
1570 f$__pfn_24 = MEM[(struct *)&f + 4B];
1572 and a few lines below:
1574 <bb 5>
1575 D.2496_3 = (int) f$__pfn_24;
1576 D.2497_4 = D.2496_3 & 1;
1577 if (D.2497_4 != 0)
1578 goto <bb 3>;
1579 else
1580 goto <bb 4>;
1582 <bb 6>:
1583 D.2500_7 = (unsigned int) f$__delta_5;
1584 D.2501_8 = &S + D.2500_7;
1585 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1586 D.2503_10 = *D.2502_9;
1587 D.2504_12 = f$__pfn_24 + -1;
1588 D.2505_13 = (unsigned int) D.2504_12;
1589 D.2506_14 = D.2503_10 + D.2505_13;
1590 D.2507_15 = *D.2506_14;
1591 iftmp.11_16 = (String:: *) D.2507_15;
1593 <bb 7>:
1594 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1595 D.2500_19 = (unsigned int) f$__delta_5;
1596 D.2508_20 = &S + D.2500_19;
1597 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1599 Such patterns are results of simple calls to a member pointer:
1601 int doprinting (int (MyString::* f)(int) const)
1603 MyString S ("somestring");
1605 return (S.*f)(4);
1608 Moreover, the function also looks for called pointers loaded from aggregates
1609 passed by value or reference. */
1611 static void
1612 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1613 struct ipa_node_params *info,
1614 struct param_analysis_info *parms_ainfo,
1615 gimple call, tree target)
1617 gimple def;
1618 tree n1, n2;
1619 gimple d1, d2;
1620 tree rec, rec2, cond;
1621 gimple branch;
1622 int index;
1623 basic_block bb, virt_bb, join;
1624 HOST_WIDE_INT offset;
1625 bool by_ref;
1627 if (SSA_NAME_IS_DEFAULT_DEF (target))
1629 tree var = SSA_NAME_VAR (target);
1630 index = ipa_get_param_decl_index (info, var);
1631 if (index >= 0)
1632 ipa_note_param_call (node, index, call);
1633 return;
1636 def = SSA_NAME_DEF_STMT (target);
1637 if (gimple_assign_single_p (def)
1638 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1639 gimple_assign_rhs1 (def), &index, &offset,
1640 &by_ref))
1642 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1643 cs->indirect_info->offset = offset;
1644 cs->indirect_info->agg_contents = 1;
1645 cs->indirect_info->by_ref = by_ref;
1646 return;
1649 /* Now we need to try to match the complex pattern of calling a member
1650 pointer. */
1651 if (gimple_code (def) != GIMPLE_PHI
1652 || gimple_phi_num_args (def) != 2
1653 || !POINTER_TYPE_P (TREE_TYPE (target))
1654 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1655 return;
1657 /* First, we need to check whether one of these is a load from a member
1658 pointer that is a parameter to this function. */
1659 n1 = PHI_ARG_DEF (def, 0);
1660 n2 = PHI_ARG_DEF (def, 1);
1661 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1662 return;
1663 d1 = SSA_NAME_DEF_STMT (n1);
1664 d2 = SSA_NAME_DEF_STMT (n2);
1666 join = gimple_bb (def);
1667 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1669 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1670 return;
1672 bb = EDGE_PRED (join, 0)->src;
1673 virt_bb = gimple_bb (d2);
1675 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1677 bb = EDGE_PRED (join, 1)->src;
1678 virt_bb = gimple_bb (d1);
1680 else
1681 return;
1683 /* Second, we need to check that the basic blocks are laid out in the way
1684 corresponding to the pattern. */
1686 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1687 || single_pred (virt_bb) != bb
1688 || single_succ (virt_bb) != join)
1689 return;
1691 /* Third, let's see that the branching is done depending on the least
1692 significant bit of the pfn. */
1694 branch = last_stmt (bb);
1695 if (!branch || gimple_code (branch) != GIMPLE_COND)
1696 return;
1698 if ((gimple_cond_code (branch) != NE_EXPR
1699 && gimple_cond_code (branch) != EQ_EXPR)
1700 || !integer_zerop (gimple_cond_rhs (branch)))
1701 return;
1703 cond = gimple_cond_lhs (branch);
1704 if (!ipa_is_ssa_with_stmt_def (cond))
1705 return;
1707 def = SSA_NAME_DEF_STMT (cond);
1708 if (!is_gimple_assign (def)
1709 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1710 || !integer_onep (gimple_assign_rhs2 (def)))
1711 return;
1713 cond = gimple_assign_rhs1 (def);
1714 if (!ipa_is_ssa_with_stmt_def (cond))
1715 return;
1717 def = SSA_NAME_DEF_STMT (cond);
1719 if (is_gimple_assign (def)
1720 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1722 cond = gimple_assign_rhs1 (def);
1723 if (!ipa_is_ssa_with_stmt_def (cond))
1724 return;
1725 def = SSA_NAME_DEF_STMT (cond);
1728 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1729 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1730 == ptrmemfunc_vbit_in_delta),
1731 NULL);
1732 if (rec != rec2)
1733 return;
1735 index = ipa_get_param_decl_index (info, rec);
1736 if (index >= 0
1737 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1739 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1740 cs->indirect_info->offset = offset;
1741 cs->indirect_info->agg_contents = 1;
1744 return;
1747 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1748 object referenced in the expression is a formal parameter of the caller
1749 (described by INFO), create a call note for the statement. */
1751 static void
1752 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1753 struct ipa_node_params *info, gimple call,
1754 tree target)
1756 struct cgraph_edge *cs;
1757 struct cgraph_indirect_call_info *ii;
1758 struct ipa_jump_func jfunc;
1759 tree obj = OBJ_TYPE_REF_OBJECT (target);
1760 int index;
1761 HOST_WIDE_INT anc_offset;
1763 if (!flag_devirtualize)
1764 return;
1766 if (TREE_CODE (obj) != SSA_NAME)
1767 return;
1769 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1771 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1772 return;
1774 anc_offset = 0;
1775 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1776 gcc_assert (index >= 0);
1777 if (detect_type_change_ssa (obj, call, &jfunc))
1778 return;
1780 else
1782 gimple stmt = SSA_NAME_DEF_STMT (obj);
1783 tree expr;
1785 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1786 if (!expr)
1787 return;
1788 index = ipa_get_param_decl_index (info,
1789 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1790 gcc_assert (index >= 0);
1791 if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1792 return;
1795 cs = ipa_note_param_call (node, index, call);
1796 ii = cs->indirect_info;
1797 ii->offset = anc_offset;
1798 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1799 ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1800 ii->polymorphic = 1;
1803 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1804 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1805 containing intermediate information about each formal parameter. */
1807 static void
1808 ipa_analyze_call_uses (struct cgraph_node *node,
1809 struct ipa_node_params *info,
1810 struct param_analysis_info *parms_ainfo, gimple call)
1812 tree target = gimple_call_fn (call);
1814 if (!target)
1815 return;
1816 if (TREE_CODE (target) == SSA_NAME)
1817 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
1818 else if (TREE_CODE (target) == OBJ_TYPE_REF)
1819 ipa_analyze_virtual_call_uses (node, info, call, target);
1823 /* Analyze the call statement STMT with respect to formal parameters (described
1824 in INFO) of caller given by NODE. Currently it only checks whether formal
1825 parameters are called. PARMS_AINFO is a pointer to a vector containing
1826 intermediate information about each formal parameter. */
1828 static void
1829 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1830 struct param_analysis_info *parms_ainfo, gimple stmt)
1832 if (is_gimple_call (stmt))
1833 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
1836 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1837 If OP is a parameter declaration, mark it as used in the info structure
1838 passed in DATA. */
1840 static bool
1841 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
1842 tree op, void *data)
1844 struct ipa_node_params *info = (struct ipa_node_params *) data;
1846 op = get_base_address (op);
1847 if (op
1848 && TREE_CODE (op) == PARM_DECL)
1850 int index = ipa_get_param_decl_index (info, op);
1851 gcc_assert (index >= 0);
1852 ipa_set_param_used (info, index, true);
1855 return false;
1858 /* Scan the function body of NODE and inspect the uses of formal parameters.
1859 Store the findings in various structures of the associated ipa_node_params
1860 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
1861 vector containing intermediate information about each formal parameter. */
1863 static void
1864 ipa_analyze_params_uses (struct cgraph_node *node,
1865 struct param_analysis_info *parms_ainfo)
1867 tree decl = node->symbol.decl;
1868 basic_block bb;
1869 struct function *func;
1870 gimple_stmt_iterator gsi;
1871 struct ipa_node_params *info = IPA_NODE_REF (node);
1872 int i;
1874 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1875 return;
1877 for (i = 0; i < ipa_get_param_count (info); i++)
1879 tree parm = ipa_get_param (info, i);
1880 tree ddef;
1881 /* For SSA regs see if parameter is used. For non-SSA we compute
1882 the flag during modification analysis. */
1883 if (is_gimple_reg (parm)
1884 && (ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->symbol.decl),
1885 parm)) != NULL_TREE
1886 && !has_zero_uses (ddef))
1887 ipa_set_param_used (info, i, true);
1890 func = DECL_STRUCT_FUNCTION (decl);
1891 FOR_EACH_BB_FN (bb, func)
1893 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1895 gimple stmt = gsi_stmt (gsi);
1897 if (is_gimple_debug (stmt))
1898 continue;
1900 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
1901 walk_stmt_load_store_addr_ops (stmt, info,
1902 visit_ref_for_mod_analysis,
1903 visit_ref_for_mod_analysis,
1904 visit_ref_for_mod_analysis);
1906 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1907 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
1908 visit_ref_for_mod_analysis,
1909 visit_ref_for_mod_analysis,
1910 visit_ref_for_mod_analysis);
1913 info->uses_analysis_done = 1;
1916 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
1918 static void
1919 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
1921 int i;
1923 for (i = 0; i < param_count; i++)
1925 if (parms_ainfo[i].parm_visited_statements)
1926 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
1927 if (parms_ainfo[i].pt_visited_statements)
1928 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
1932 /* Initialize the array describing properties of of formal parameters
1933 of NODE, analyze their uses and compute jump functions associated
1934 with actual arguments of calls from within NODE. */
1936 void
1937 ipa_analyze_node (struct cgraph_node *node)
1939 struct ipa_node_params *info;
1940 struct param_analysis_info *parms_ainfo;
1941 int param_count;
1943 ipa_check_create_node_params ();
1944 ipa_check_create_edge_args ();
1945 info = IPA_NODE_REF (node);
1946 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1947 ipa_initialize_node_params (node);
1949 param_count = ipa_get_param_count (info);
1950 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
1951 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
1953 ipa_analyze_params_uses (node, parms_ainfo);
1954 ipa_compute_jump_functions (node, parms_ainfo);
1956 free_parms_ainfo (parms_ainfo, param_count);
1957 pop_cfun ();
1961 /* Update the jump function DST when the call graph edge corresponding to SRC is
1962 is being inlined, knowing that DST is of type ancestor and src of known
1963 type. */
1965 static void
1966 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
1967 struct ipa_jump_func *dst)
1969 HOST_WIDE_INT combined_offset;
1970 tree combined_type;
1972 combined_offset = ipa_get_jf_known_type_offset (src)
1973 + ipa_get_jf_ancestor_offset (dst);
1974 combined_type = ipa_get_jf_ancestor_type (dst);
1976 ipa_set_jf_known_type (dst, combined_offset,
1977 ipa_get_jf_known_type_base_type (src),
1978 combined_type);
1981 /* Update the jump functions associated with call graph edge E when the call
1982 graph edge CS is being inlined, assuming that E->caller is already (possibly
1983 indirectly) inlined into CS->callee and that E has not been inlined. */
1985 static void
1986 update_jump_functions_after_inlining (struct cgraph_edge *cs,
1987 struct cgraph_edge *e)
1989 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1990 struct ipa_edge_args *args = IPA_EDGE_REF (e);
1991 int count = ipa_get_cs_argument_count (args);
1992 int i;
1994 for (i = 0; i < count; i++)
1996 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
1998 if (dst->type == IPA_JF_ANCESTOR)
2000 struct ipa_jump_func *src;
2001 int dst_fid = dst->value.ancestor.formal_id;
2003 /* Variable number of arguments can cause havoc if we try to access
2004 one that does not exist in the inlined edge. So make sure we
2005 don't. */
2006 if (dst_fid >= ipa_get_cs_argument_count (top))
2008 dst->type = IPA_JF_UNKNOWN;
2009 continue;
2012 src = ipa_get_ith_jump_func (top, dst_fid);
2014 if (src->agg.items
2015 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2017 struct ipa_agg_jf_item *item;
2018 int j;
2020 /* Currently we do not produce clobber aggregate jump functions,
2021 replace with merging when we do. */
2022 gcc_assert (!dst->agg.items);
2024 dst->agg.items = vec_safe_copy (src->agg.items);
2025 dst->agg.by_ref = src->agg.by_ref;
2026 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2027 item->offset -= dst->value.ancestor.offset;
2030 if (src->type == IPA_JF_KNOWN_TYPE)
2031 combine_known_type_and_ancestor_jfs (src, dst);
2032 else if (src->type == IPA_JF_PASS_THROUGH
2033 && src->value.pass_through.operation == NOP_EXPR)
2035 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2036 dst->value.ancestor.agg_preserved &=
2037 src->value.pass_through.agg_preserved;
2039 else if (src->type == IPA_JF_ANCESTOR)
2041 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2042 dst->value.ancestor.offset += src->value.ancestor.offset;
2043 dst->value.ancestor.agg_preserved &=
2044 src->value.ancestor.agg_preserved;
2046 else
2047 dst->type = IPA_JF_UNKNOWN;
2049 else if (dst->type == IPA_JF_PASS_THROUGH)
2051 struct ipa_jump_func *src;
2052 /* We must check range due to calls with variable number of arguments
2053 and we cannot combine jump functions with operations. */
2054 if (dst->value.pass_through.operation == NOP_EXPR
2055 && (dst->value.pass_through.formal_id
2056 < ipa_get_cs_argument_count (top)))
2058 bool agg_p;
2059 int dst_fid = dst->value.pass_through.formal_id;
2060 src = ipa_get_ith_jump_func (top, dst_fid);
2061 agg_p = dst->value.pass_through.agg_preserved;
2063 dst->type = src->type;
2064 dst->value = src->value;
2066 if (src->agg.items
2067 && (agg_p || !src->agg.by_ref))
2069 /* Currently we do not produce clobber aggregate jump
2070 functions, replace with merging when we do. */
2071 gcc_assert (!dst->agg.items);
2073 dst->agg.by_ref = src->agg.by_ref;
2074 dst->agg.items = vec_safe_copy (src->agg.items);
2077 if (!agg_p)
2079 if (dst->type == IPA_JF_PASS_THROUGH)
2080 dst->value.pass_through.agg_preserved = false;
2081 else if (dst->type == IPA_JF_ANCESTOR)
2082 dst->value.ancestor.agg_preserved = false;
2085 else
2086 dst->type = IPA_JF_UNKNOWN;
2091 /* If TARGET is an addr_expr of a function declaration, make it the destination
2092 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2094 struct cgraph_edge *
2095 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2097 struct cgraph_node *callee;
2098 struct inline_edge_summary *es = inline_edge_summary (ie);
2100 if (TREE_CODE (target) == ADDR_EXPR)
2101 target = TREE_OPERAND (target, 0);
2102 if (TREE_CODE (target) != FUNCTION_DECL)
2104 target = canonicalize_constructor_val (target, NULL);
2105 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2107 if (dump_file)
2108 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2109 " in (%s/%i).\n",
2110 cgraph_node_name (ie->caller), ie->caller->uid);
2111 return NULL;
2114 callee = cgraph_get_node (target);
2116 /* Because may-edges are not explicitely represented and vtable may be external,
2117 we may create the first reference to the object in the unit. */
2118 if (!callee || callee->global.inlined_to)
2120 struct cgraph_node *first_clone = callee;
2122 /* We are better to ensure we can refer to it.
2123 In the case of static functions we are out of luck, since we already
2124 removed its body. In the case of public functions we may or may
2125 not introduce the reference. */
2126 if (!canonicalize_constructor_val (target, NULL)
2127 || !TREE_PUBLIC (target))
2129 if (dump_file)
2130 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2131 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2132 xstrdup (cgraph_node_name (ie->caller)), ie->caller->uid,
2133 xstrdup (cgraph_node_name (ie->callee)), ie->callee->uid);
2134 return NULL;
2137 /* Create symbol table node. Even if inline clone exists, we can not take
2138 it as a target of non-inlined call. */
2139 callee = cgraph_create_node (target);
2141 /* OK, we previously inlined the function, then removed the offline copy and
2142 now we want it back for external call. This can happen when devirtualizing
2143 while inlining function called once that happens after extern inlined and
2144 virtuals are already removed. In this case introduce the external node
2145 and make it available for call. */
2146 if (first_clone)
2148 first_clone->clone_of = callee;
2149 callee->clones = first_clone;
2150 symtab_prevail_in_asm_name_hash ((symtab_node)callee);
2151 symtab_insert_node_to_hashtable ((symtab_node)callee);
2152 if (dump_file)
2153 fprintf (dump_file, "ipa-prop: Introduced new external node "
2154 "(%s/%i) and turned into root of the clone tree.\n",
2155 xstrdup (cgraph_node_name (callee)), callee->uid);
2157 else if (dump_file)
2158 fprintf (dump_file, "ipa-prop: Introduced new external node "
2159 "(%s/%i).\n",
2160 xstrdup (cgraph_node_name (callee)), callee->uid);
2162 ipa_check_create_node_params ();
2164 /* We can not make edges to inline clones. It is bug that someone removed
2165 the cgraph node too early. */
2166 gcc_assert (!callee->global.inlined_to);
2168 cgraph_make_edge_direct (ie, callee);
2169 es = inline_edge_summary (ie);
2170 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2171 - eni_size_weights.call_cost);
2172 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2173 - eni_time_weights.call_cost);
2174 if (dump_file)
2176 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2177 "(%s/%i -> %s/%i), for stmt ",
2178 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2179 xstrdup (cgraph_node_name (ie->caller)), ie->caller->uid,
2180 xstrdup (cgraph_node_name (ie->callee)), ie->callee->uid);
2181 if (ie->call_stmt)
2182 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2183 else
2184 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2186 callee = cgraph_function_or_thunk_node (callee, NULL);
2188 return ie;
2191 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2192 return NULL if there is not any. BY_REF specifies whether the value has to
2193 be passed by reference or by value. */
2195 tree
2196 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2197 HOST_WIDE_INT offset, bool by_ref)
2199 struct ipa_agg_jf_item *item;
2200 int i;
2202 if (by_ref != agg->by_ref)
2203 return NULL;
2205 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2206 if (item->offset == offset)
2208 /* Currently we do not have clobber values, return NULL for them once
2209 we do. */
2210 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2211 return item->value;
2213 return NULL;
2216 /* Try to find a destination for indirect edge IE that corresponds to a simple
2217 call or a call of a member function pointer and where the destination is a
2218 pointer formal parameter described by jump function JFUNC. If it can be
2219 determined, return the newly direct edge, otherwise return NULL.
2220 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2222 static struct cgraph_edge *
2223 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2224 struct ipa_jump_func *jfunc,
2225 struct ipa_node_params *new_root_info)
2227 tree target;
2229 if (ie->indirect_info->agg_contents)
2230 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2231 ie->indirect_info->offset,
2232 ie->indirect_info->by_ref);
2233 else
2234 target = ipa_value_from_jfunc (new_root_info, jfunc);
2235 if (!target)
2236 return NULL;
2237 return ipa_make_edge_direct_to_target (ie, target);
2240 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2241 call based on a formal parameter which is described by jump function JFUNC
2242 and if it can be determined, make it direct and return the direct edge.
2243 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2244 are relative to. */
2246 static struct cgraph_edge *
2247 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2248 struct ipa_jump_func *jfunc,
2249 struct ipa_node_params *new_root_info)
2251 tree binfo, target;
2253 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2255 if (!binfo)
2256 return NULL;
2258 if (TREE_CODE (binfo) != TREE_BINFO)
2260 binfo = gimple_extract_devirt_binfo_from_cst (binfo);
2261 if (!binfo)
2262 return NULL;
2265 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2266 ie->indirect_info->otr_type);
2267 if (binfo)
2268 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2269 binfo);
2270 else
2271 return NULL;
2273 if (target)
2274 return ipa_make_edge_direct_to_target (ie, target);
2275 else
2276 return NULL;
2279 /* Update the param called notes associated with NODE when CS is being inlined,
2280 assuming NODE is (potentially indirectly) inlined into CS->callee.
2281 Moreover, if the callee is discovered to be constant, create a new cgraph
2282 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2283 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2285 static bool
2286 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2287 struct cgraph_node *node,
2288 vec<cgraph_edge_p> *new_edges)
2290 struct ipa_edge_args *top;
2291 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2292 struct ipa_node_params *new_root_info;
2293 bool res = false;
2295 ipa_check_create_edge_args ();
2296 top = IPA_EDGE_REF (cs);
2297 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2298 ? cs->caller->global.inlined_to
2299 : cs->caller);
2301 for (ie = node->indirect_calls; ie; ie = next_ie)
2303 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2304 struct ipa_jump_func *jfunc;
2305 int param_index;
2307 next_ie = ie->next_callee;
2309 if (ici->param_index == -1)
2310 continue;
2312 /* We must check range due to calls with variable number of arguments: */
2313 if (ici->param_index >= ipa_get_cs_argument_count (top))
2315 ici->param_index = -1;
2316 continue;
2319 param_index = ici->param_index;
2320 jfunc = ipa_get_ith_jump_func (top, param_index);
2322 if (!flag_indirect_inlining)
2323 new_direct_edge = NULL;
2324 else if (ici->polymorphic)
2325 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2326 new_root_info);
2327 else
2328 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2329 new_root_info);
2330 if (new_direct_edge)
2332 new_direct_edge->indirect_inlining_edge = 1;
2333 if (new_direct_edge->call_stmt)
2334 new_direct_edge->call_stmt_cannot_inline_p
2335 = !gimple_check_call_matching_types (new_direct_edge->call_stmt,
2336 new_direct_edge->callee->symbol.decl);
2337 if (new_edges)
2339 new_edges->safe_push (new_direct_edge);
2340 top = IPA_EDGE_REF (cs);
2341 res = true;
2344 else if (jfunc->type == IPA_JF_PASS_THROUGH
2345 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2347 if (ici->agg_contents
2348 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2349 ici->param_index = -1;
2350 else
2351 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2353 else if (jfunc->type == IPA_JF_ANCESTOR)
2355 if (ici->agg_contents
2356 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2357 ici->param_index = -1;
2358 else
2360 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2361 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2364 else
2365 /* Either we can find a destination for this edge now or never. */
2366 ici->param_index = -1;
2369 return res;
2372 /* Recursively traverse subtree of NODE (including node) made of inlined
2373 cgraph_edges when CS has been inlined and invoke
2374 update_indirect_edges_after_inlining on all nodes and
2375 update_jump_functions_after_inlining on all non-inlined edges that lead out
2376 of this subtree. Newly discovered indirect edges will be added to
2377 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2378 created. */
2380 static bool
2381 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2382 struct cgraph_node *node,
2383 vec<cgraph_edge_p> *new_edges)
2385 struct cgraph_edge *e;
2386 bool res;
2388 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2390 for (e = node->callees; e; e = e->next_callee)
2391 if (!e->inline_failed)
2392 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2393 else
2394 update_jump_functions_after_inlining (cs, e);
2395 for (e = node->indirect_calls; e; e = e->next_callee)
2396 update_jump_functions_after_inlining (cs, e);
2398 return res;
2401 /* Update jump functions and call note functions on inlining the call site CS.
2402 CS is expected to lead to a node already cloned by
2403 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2404 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2405 created. */
2407 bool
2408 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2409 vec<cgraph_edge_p> *new_edges)
2411 bool changed;
2412 /* Do nothing if the preparation phase has not been carried out yet
2413 (i.e. during early inlining). */
2414 if (!ipa_node_params_vector.exists ())
2415 return false;
2416 gcc_assert (ipa_edge_args_vector);
2418 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2420 /* We do not keep jump functions of inlined edges up to date. Better to free
2421 them so we do not access them accidentally. */
2422 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
2423 return changed;
2426 /* Frees all dynamically allocated structures that the argument info points
2427 to. */
2429 void
2430 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2432 vec_free (args->jump_functions);
2433 memset (args, 0, sizeof (*args));
2436 /* Free all ipa_edge structures. */
2438 void
2439 ipa_free_all_edge_args (void)
2441 int i;
2442 struct ipa_edge_args *args;
2444 if (!ipa_edge_args_vector)
2445 return;
2447 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2448 ipa_free_edge_args_substructures (args);
2450 vec_free (ipa_edge_args_vector);
2453 /* Frees all dynamically allocated structures that the param info points
2454 to. */
2456 void
2457 ipa_free_node_params_substructures (struct ipa_node_params *info)
2459 info->descriptors.release ();
2460 free (info->lattices);
2461 /* Lattice values and their sources are deallocated with their alocation
2462 pool. */
2463 info->known_vals.release ();
2464 memset (info, 0, sizeof (*info));
2467 /* Free all ipa_node_params structures. */
2469 void
2470 ipa_free_all_node_params (void)
2472 int i;
2473 struct ipa_node_params *info;
2475 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
2476 ipa_free_node_params_substructures (info);
2478 ipa_node_params_vector.release ();
2481 /* Set the aggregate replacements of NODE to be AGGVALS. */
2483 void
2484 ipa_set_node_agg_value_chain (struct cgraph_node *node,
2485 struct ipa_agg_replacement_value *aggvals)
2487 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
2488 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
2490 (*ipa_node_agg_replacements)[node->uid] = aggvals;
2493 /* Hook that is called by cgraph.c when an edge is removed. */
2495 static void
2496 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
2498 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2499 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
2500 return;
2501 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
2504 /* Hook that is called by cgraph.c when a node is removed. */
2506 static void
2507 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2509 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2510 if (ipa_node_params_vector.length () > (unsigned)node->uid)
2511 ipa_free_node_params_substructures (IPA_NODE_REF (node));
2512 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
2513 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
2516 /* Hook that is called by cgraph.c when an edge is duplicated. */
2518 static void
2519 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2520 __attribute__((unused)) void *data)
2522 struct ipa_edge_args *old_args, *new_args;
2523 unsigned int i;
2525 ipa_check_create_edge_args ();
2527 old_args = IPA_EDGE_REF (src);
2528 new_args = IPA_EDGE_REF (dst);
2530 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
2532 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
2533 (*new_args->jump_functions)[i].agg.items
2534 = vec_safe_copy ((*old_args->jump_functions)[i].agg.items);
2537 /* Hook that is called by cgraph.c when a node is duplicated. */
2539 static void
2540 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
2541 ATTRIBUTE_UNUSED void *data)
2543 struct ipa_node_params *old_info, *new_info;
2544 struct ipa_agg_replacement_value *old_av, *new_av;
2546 ipa_check_create_node_params ();
2547 old_info = IPA_NODE_REF (src);
2548 new_info = IPA_NODE_REF (dst);
2550 new_info->descriptors = old_info->descriptors.copy ();
2551 new_info->lattices = NULL;
2552 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
2554 new_info->uses_analysis_done = old_info->uses_analysis_done;
2555 new_info->node_enqueued = old_info->node_enqueued;
2557 old_av = ipa_get_agg_replacements_for_node (src);
2558 if (!old_av)
2559 return;
2561 new_av = NULL;
2562 while (old_av)
2564 struct ipa_agg_replacement_value *v;
2566 v = ggc_alloc_ipa_agg_replacement_value ();
2567 memcpy (v, old_av, sizeof (*v));
2568 v->next = new_av;
2569 new_av = v;
2570 old_av = old_av->next;
2572 ipa_set_node_agg_value_chain (dst, new_av);
2576 /* Analyze newly added function into callgraph. */
2578 static void
2579 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2581 ipa_analyze_node (node);
2584 /* Register our cgraph hooks if they are not already there. */
2586 void
2587 ipa_register_cgraph_hooks (void)
2589 if (!edge_removal_hook_holder)
2590 edge_removal_hook_holder =
2591 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
2592 if (!node_removal_hook_holder)
2593 node_removal_hook_holder =
2594 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
2595 if (!edge_duplication_hook_holder)
2596 edge_duplication_hook_holder =
2597 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
2598 if (!node_duplication_hook_holder)
2599 node_duplication_hook_holder =
2600 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
2601 function_insertion_hook_holder =
2602 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
2605 /* Unregister our cgraph hooks if they are not already there. */
2607 static void
2608 ipa_unregister_cgraph_hooks (void)
2610 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2611 edge_removal_hook_holder = NULL;
2612 cgraph_remove_node_removal_hook (node_removal_hook_holder);
2613 node_removal_hook_holder = NULL;
2614 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2615 edge_duplication_hook_holder = NULL;
2616 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2617 node_duplication_hook_holder = NULL;
2618 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2619 function_insertion_hook_holder = NULL;
2622 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2623 longer needed after ipa-cp. */
2625 void
2626 ipa_free_all_structures_after_ipa_cp (void)
2628 if (!optimize)
2630 ipa_free_all_edge_args ();
2631 ipa_free_all_node_params ();
2632 free_alloc_pool (ipcp_sources_pool);
2633 free_alloc_pool (ipcp_values_pool);
2634 free_alloc_pool (ipcp_agg_lattice_pool);
2635 ipa_unregister_cgraph_hooks ();
2639 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2640 longer needed after indirect inlining. */
2642 void
2643 ipa_free_all_structures_after_iinln (void)
2645 ipa_free_all_edge_args ();
2646 ipa_free_all_node_params ();
2647 ipa_unregister_cgraph_hooks ();
2648 if (ipcp_sources_pool)
2649 free_alloc_pool (ipcp_sources_pool);
2650 if (ipcp_values_pool)
2651 free_alloc_pool (ipcp_values_pool);
2652 if (ipcp_agg_lattice_pool)
2653 free_alloc_pool (ipcp_agg_lattice_pool);
2656 /* Print ipa_tree_map data structures of all functions in the
2657 callgraph to F. */
2659 void
2660 ipa_print_node_params (FILE *f, struct cgraph_node *node)
2662 int i, count;
2663 tree temp;
2664 struct ipa_node_params *info;
2666 if (!node->analyzed)
2667 return;
2668 info = IPA_NODE_REF (node);
2669 fprintf (f, " function %s parameter descriptors:\n",
2670 cgraph_node_name (node));
2671 count = ipa_get_param_count (info);
2672 for (i = 0; i < count; i++)
2674 temp = ipa_get_param (info, i);
2675 if (TREE_CODE (temp) == PARM_DECL)
2676 fprintf (f, " param %d : %s", i,
2677 (DECL_NAME (temp)
2678 ? (*lang_hooks.decl_printable_name) (temp, 2)
2679 : "(unnamed)"));
2680 if (ipa_is_param_used (info, i))
2681 fprintf (f, " used");
2682 fprintf (f, "\n");
2686 /* Print ipa_tree_map data structures of all functions in the
2687 callgraph to F. */
2689 void
2690 ipa_print_all_params (FILE * f)
2692 struct cgraph_node *node;
2694 fprintf (f, "\nFunction parameters:\n");
2695 FOR_EACH_FUNCTION (node)
2696 ipa_print_node_params (f, node);
2699 /* Return a heap allocated vector containing formal parameters of FNDECL. */
2701 vec<tree>
2702 ipa_get_vector_of_formal_parms (tree fndecl)
2704 vec<tree> args;
2705 int count;
2706 tree parm;
2708 count = count_formal_params (fndecl);
2709 args.create (count);
2710 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2711 args.quick_push (parm);
2713 return args;
2716 /* Return a heap allocated vector containing types of formal parameters of
2717 function type FNTYPE. */
2719 static inline vec<tree>
2720 get_vector_of_formal_parm_types (tree fntype)
2722 vec<tree> types;
2723 int count = 0;
2724 tree t;
2726 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2727 count++;
2729 types.create (count);
2730 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2731 types.quick_push (TREE_VALUE (t));
2733 return types;
2736 /* Modify the function declaration FNDECL and its type according to the plan in
2737 ADJUSTMENTS. It also sets base fields of individual adjustments structures
2738 to reflect the actual parameters being modified which are determined by the
2739 base_index field. */
2741 void
2742 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
2743 const char *synth_parm_prefix)
2745 vec<tree> oparms, otypes;
2746 tree orig_type, new_type = NULL;
2747 tree old_arg_types, t, new_arg_types = NULL;
2748 tree parm, *link = &DECL_ARGUMENTS (fndecl);
2749 int i, len = adjustments.length ();
2750 tree new_reversed = NULL;
2751 bool care_for_types, last_parm_void;
2753 if (!synth_parm_prefix)
2754 synth_parm_prefix = "SYNTH";
2756 oparms = ipa_get_vector_of_formal_parms (fndecl);
2757 orig_type = TREE_TYPE (fndecl);
2758 old_arg_types = TYPE_ARG_TYPES (orig_type);
2760 /* The following test is an ugly hack, some functions simply don't have any
2761 arguments in their type. This is probably a bug but well... */
2762 care_for_types = (old_arg_types != NULL_TREE);
2763 if (care_for_types)
2765 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
2766 == void_type_node);
2767 otypes = get_vector_of_formal_parm_types (orig_type);
2768 if (last_parm_void)
2769 gcc_assert (oparms.length () + 1 == otypes.length ());
2770 else
2771 gcc_assert (oparms.length () == otypes.length ());
2773 else
2775 last_parm_void = false;
2776 otypes.create (0);
2779 for (i = 0; i < len; i++)
2781 struct ipa_parm_adjustment *adj;
2782 gcc_assert (link);
2784 adj = &adjustments[i];
2785 parm = oparms[adj->base_index];
2786 adj->base = parm;
2788 if (adj->copy_param)
2790 if (care_for_types)
2791 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
2792 new_arg_types);
2793 *link = parm;
2794 link = &DECL_CHAIN (parm);
2796 else if (!adj->remove_param)
2798 tree new_parm;
2799 tree ptype;
2801 if (adj->by_ref)
2802 ptype = build_pointer_type (adj->type);
2803 else
2804 ptype = adj->type;
2806 if (care_for_types)
2807 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
2809 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
2810 ptype);
2811 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
2813 DECL_ARTIFICIAL (new_parm) = 1;
2814 DECL_ARG_TYPE (new_parm) = ptype;
2815 DECL_CONTEXT (new_parm) = fndecl;
2816 TREE_USED (new_parm) = 1;
2817 DECL_IGNORED_P (new_parm) = 1;
2818 layout_decl (new_parm, 0);
2820 adj->base = parm;
2821 adj->reduction = new_parm;
2823 *link = new_parm;
2825 link = &DECL_CHAIN (new_parm);
2829 *link = NULL_TREE;
2831 if (care_for_types)
2833 new_reversed = nreverse (new_arg_types);
2834 if (last_parm_void)
2836 if (new_reversed)
2837 TREE_CHAIN (new_arg_types) = void_list_node;
2838 else
2839 new_reversed = void_list_node;
2843 /* Use copy_node to preserve as much as possible from original type
2844 (debug info, attribute lists etc.)
2845 Exception is METHOD_TYPEs must have THIS argument.
2846 When we are asked to remove it, we need to build new FUNCTION_TYPE
2847 instead. */
2848 if (TREE_CODE (orig_type) != METHOD_TYPE
2849 || (adjustments[0].copy_param
2850 && adjustments[0].base_index == 0))
2852 new_type = build_distinct_type_copy (orig_type);
2853 TYPE_ARG_TYPES (new_type) = new_reversed;
2855 else
2857 new_type
2858 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
2859 new_reversed));
2860 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
2861 DECL_VINDEX (fndecl) = NULL_TREE;
2864 /* When signature changes, we need to clear builtin info. */
2865 if (DECL_BUILT_IN (fndecl))
2867 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
2868 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
2871 /* This is a new type, not a copy of an old type. Need to reassociate
2872 variants. We can handle everything except the main variant lazily. */
2873 t = TYPE_MAIN_VARIANT (orig_type);
2874 if (orig_type != t)
2876 TYPE_MAIN_VARIANT (new_type) = t;
2877 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
2878 TYPE_NEXT_VARIANT (t) = new_type;
2880 else
2882 TYPE_MAIN_VARIANT (new_type) = new_type;
2883 TYPE_NEXT_VARIANT (new_type) = NULL;
2886 TREE_TYPE (fndecl) = new_type;
2887 DECL_VIRTUAL_P (fndecl) = 0;
2888 otypes.release ();
2889 oparms.release ();
2892 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
2893 If this is a directly recursive call, CS must be NULL. Otherwise it must
2894 contain the corresponding call graph edge. */
2896 void
2897 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
2898 ipa_parm_adjustment_vec adjustments)
2900 vec<tree> vargs;
2901 vec<tree, va_gc> **debug_args = NULL;
2902 gimple new_stmt;
2903 gimple_stmt_iterator gsi;
2904 tree callee_decl;
2905 int i, len;
2907 len = adjustments.length ();
2908 vargs.create (len);
2909 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->symbol.decl;
2911 gsi = gsi_for_stmt (stmt);
2912 for (i = 0; i < len; i++)
2914 struct ipa_parm_adjustment *adj;
2916 adj = &adjustments[i];
2918 if (adj->copy_param)
2920 tree arg = gimple_call_arg (stmt, adj->base_index);
2922 vargs.quick_push (arg);
2924 else if (!adj->remove_param)
2926 tree expr, base, off;
2927 location_t loc;
2928 unsigned int deref_align;
2929 bool deref_base = false;
2931 /* We create a new parameter out of the value of the old one, we can
2932 do the following kind of transformations:
2934 - A scalar passed by reference is converted to a scalar passed by
2935 value. (adj->by_ref is false and the type of the original
2936 actual argument is a pointer to a scalar).
2938 - A part of an aggregate is passed instead of the whole aggregate.
2939 The part can be passed either by value or by reference, this is
2940 determined by value of adj->by_ref. Moreover, the code below
2941 handles both situations when the original aggregate is passed by
2942 value (its type is not a pointer) and when it is passed by
2943 reference (it is a pointer to an aggregate).
2945 When the new argument is passed by reference (adj->by_ref is true)
2946 it must be a part of an aggregate and therefore we form it by
2947 simply taking the address of a reference inside the original
2948 aggregate. */
2950 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
2951 base = gimple_call_arg (stmt, adj->base_index);
2952 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
2953 : EXPR_LOCATION (base);
2955 if (TREE_CODE (base) != ADDR_EXPR
2956 && POINTER_TYPE_P (TREE_TYPE (base)))
2957 off = build_int_cst (adj->alias_ptr_type,
2958 adj->offset / BITS_PER_UNIT);
2959 else
2961 HOST_WIDE_INT base_offset;
2962 tree prev_base;
2963 bool addrof;
2965 if (TREE_CODE (base) == ADDR_EXPR)
2967 base = TREE_OPERAND (base, 0);
2968 addrof = true;
2970 else
2971 addrof = false;
2972 prev_base = base;
2973 base = get_addr_base_and_unit_offset (base, &base_offset);
2974 /* Aggregate arguments can have non-invariant addresses. */
2975 if (!base)
2977 base = build_fold_addr_expr (prev_base);
2978 off = build_int_cst (adj->alias_ptr_type,
2979 adj->offset / BITS_PER_UNIT);
2981 else if (TREE_CODE (base) == MEM_REF)
2983 if (!addrof)
2985 deref_base = true;
2986 deref_align = TYPE_ALIGN (TREE_TYPE (base));
2988 off = build_int_cst (adj->alias_ptr_type,
2989 base_offset
2990 + adj->offset / BITS_PER_UNIT);
2991 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
2992 off);
2993 base = TREE_OPERAND (base, 0);
2995 else
2997 off = build_int_cst (adj->alias_ptr_type,
2998 base_offset
2999 + adj->offset / BITS_PER_UNIT);
3000 base = build_fold_addr_expr (base);
3004 if (!adj->by_ref)
3006 tree type = adj->type;
3007 unsigned int align;
3008 unsigned HOST_WIDE_INT misalign;
3010 if (deref_base)
3012 align = deref_align;
3013 misalign = 0;
3015 else
3017 get_pointer_alignment_1 (base, &align, &misalign);
3018 if (TYPE_ALIGN (type) > align)
3019 align = TYPE_ALIGN (type);
3021 misalign += (tree_to_double_int (off)
3022 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3023 * BITS_PER_UNIT);
3024 misalign = misalign & (align - 1);
3025 if (misalign != 0)
3026 align = (misalign & -misalign);
3027 if (align < TYPE_ALIGN (type))
3028 type = build_aligned_type (type, align);
3029 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3031 else
3033 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3034 expr = build_fold_addr_expr (expr);
3037 expr = force_gimple_operand_gsi (&gsi, expr,
3038 adj->by_ref
3039 || is_gimple_reg_type (adj->type),
3040 NULL, true, GSI_SAME_STMT);
3041 vargs.quick_push (expr);
3043 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3045 unsigned int ix;
3046 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3047 gimple def_temp;
3049 arg = gimple_call_arg (stmt, adj->base_index);
3050 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3052 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3053 continue;
3054 arg = fold_convert_loc (gimple_location (stmt),
3055 TREE_TYPE (origin), arg);
3057 if (debug_args == NULL)
3058 debug_args = decl_debug_args_insert (callee_decl);
3059 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3060 if (ddecl == origin)
3062 ddecl = (**debug_args)[ix + 1];
3063 break;
3065 if (ddecl == NULL)
3067 ddecl = make_node (DEBUG_EXPR_DECL);
3068 DECL_ARTIFICIAL (ddecl) = 1;
3069 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3070 DECL_MODE (ddecl) = DECL_MODE (origin);
3072 vec_safe_push (*debug_args, origin);
3073 vec_safe_push (*debug_args, ddecl);
3075 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3076 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3080 if (dump_file && (dump_flags & TDF_DETAILS))
3082 fprintf (dump_file, "replacing stmt:");
3083 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3086 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3087 vargs.release ();
3088 if (gimple_call_lhs (stmt))
3089 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3091 gimple_set_block (new_stmt, gimple_block (stmt));
3092 if (gimple_has_location (stmt))
3093 gimple_set_location (new_stmt, gimple_location (stmt));
3094 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3095 gimple_call_copy_flags (new_stmt, stmt);
3097 if (dump_file && (dump_flags & TDF_DETAILS))
3099 fprintf (dump_file, "with stmt:");
3100 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3101 fprintf (dump_file, "\n");
3103 gsi_replace (&gsi, new_stmt, true);
3104 if (cs)
3105 cgraph_set_call_stmt (cs, new_stmt);
3106 update_ssa (TODO_update_ssa);
3107 free_dominance_info (CDI_DOMINATORS);
3110 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3112 static bool
3113 index_in_adjustments_multiple_times_p (int base_index,
3114 ipa_parm_adjustment_vec adjustments)
3116 int i, len = adjustments.length ();
3117 bool one = false;
3119 for (i = 0; i < len; i++)
3121 struct ipa_parm_adjustment *adj;
3122 adj = &adjustments[i];
3124 if (adj->base_index == base_index)
3126 if (one)
3127 return true;
3128 else
3129 one = true;
3132 return false;
3136 /* Return adjustments that should have the same effect on function parameters
3137 and call arguments as if they were first changed according to adjustments in
3138 INNER and then by adjustments in OUTER. */
3140 ipa_parm_adjustment_vec
3141 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3142 ipa_parm_adjustment_vec outer)
3144 int i, outlen = outer.length ();
3145 int inlen = inner.length ();
3146 int removals = 0;
3147 ipa_parm_adjustment_vec adjustments, tmp;
3149 tmp.create (inlen);
3150 for (i = 0; i < inlen; i++)
3152 struct ipa_parm_adjustment *n;
3153 n = &inner[i];
3155 if (n->remove_param)
3156 removals++;
3157 else
3158 tmp.quick_push (*n);
3161 adjustments.create (outlen + removals);
3162 for (i = 0; i < outlen; i++)
3164 struct ipa_parm_adjustment r;
3165 struct ipa_parm_adjustment *out = &outer[i];
3166 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3168 memset (&r, 0, sizeof (r));
3169 gcc_assert (!in->remove_param);
3170 if (out->remove_param)
3172 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3174 r.remove_param = true;
3175 adjustments.quick_push (r);
3177 continue;
3180 r.base_index = in->base_index;
3181 r.type = out->type;
3183 /* FIXME: Create nonlocal value too. */
3185 if (in->copy_param && out->copy_param)
3186 r.copy_param = true;
3187 else if (in->copy_param)
3188 r.offset = out->offset;
3189 else if (out->copy_param)
3190 r.offset = in->offset;
3191 else
3192 r.offset = in->offset + out->offset;
3193 adjustments.quick_push (r);
3196 for (i = 0; i < inlen; i++)
3198 struct ipa_parm_adjustment *n = &inner[i];
3200 if (n->remove_param)
3201 adjustments.quick_push (*n);
3204 tmp.release ();
3205 return adjustments;
3208 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3209 friendly way, assuming they are meant to be applied to FNDECL. */
3211 void
3212 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3213 tree fndecl)
3215 int i, len = adjustments.length ();
3216 bool first = true;
3217 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3219 fprintf (file, "IPA param adjustments: ");
3220 for (i = 0; i < len; i++)
3222 struct ipa_parm_adjustment *adj;
3223 adj = &adjustments[i];
3225 if (!first)
3226 fprintf (file, " ");
3227 else
3228 first = false;
3230 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3231 print_generic_expr (file, parms[adj->base_index], 0);
3232 if (adj->base)
3234 fprintf (file, ", base: ");
3235 print_generic_expr (file, adj->base, 0);
3237 if (adj->reduction)
3239 fprintf (file, ", reduction: ");
3240 print_generic_expr (file, adj->reduction, 0);
3242 if (adj->new_ssa_base)
3244 fprintf (file, ", new_ssa_base: ");
3245 print_generic_expr (file, adj->new_ssa_base, 0);
3248 if (adj->copy_param)
3249 fprintf (file, ", copy_param");
3250 else if (adj->remove_param)
3251 fprintf (file, ", remove_param");
3252 else
3253 fprintf (file, ", offset %li", (long) adj->offset);
3254 if (adj->by_ref)
3255 fprintf (file, ", by_ref");
3256 print_node_brief (file, ", type: ", adj->type, 0);
3257 fprintf (file, "\n");
3259 parms.release ();
3262 /* Dump the AV linked list. */
3264 void
3265 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3267 bool comma = false;
3268 fprintf (f, " Aggregate replacements:");
3269 for (; av; av = av->next)
3271 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3272 av->index, av->offset);
3273 print_generic_expr (f, av->value, 0);
3274 comma = true;
3276 fprintf (f, "\n");
3279 /* Stream out jump function JUMP_FUNC to OB. */
3281 static void
3282 ipa_write_jump_function (struct output_block *ob,
3283 struct ipa_jump_func *jump_func)
3285 struct ipa_agg_jf_item *item;
3286 struct bitpack_d bp;
3287 int i, count;
3289 streamer_write_uhwi (ob, jump_func->type);
3290 switch (jump_func->type)
3292 case IPA_JF_UNKNOWN:
3293 break;
3294 case IPA_JF_KNOWN_TYPE:
3295 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3296 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3297 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3298 break;
3299 case IPA_JF_CONST:
3300 gcc_assert (
3301 EXPR_LOCATION (jump_func->value.constant) == UNKNOWN_LOCATION);
3302 stream_write_tree (ob, jump_func->value.constant, true);
3303 break;
3304 case IPA_JF_PASS_THROUGH:
3305 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3306 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3307 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3308 bp = bitpack_create (ob->main_stream);
3309 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3310 streamer_write_bitpack (&bp);
3311 break;
3312 case IPA_JF_ANCESTOR:
3313 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3314 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3315 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3316 bp = bitpack_create (ob->main_stream);
3317 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3318 streamer_write_bitpack (&bp);
3319 break;
3322 count = vec_safe_length (jump_func->agg.items);
3323 streamer_write_uhwi (ob, count);
3324 if (count)
3326 bp = bitpack_create (ob->main_stream);
3327 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3328 streamer_write_bitpack (&bp);
3331 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3333 streamer_write_uhwi (ob, item->offset);
3334 stream_write_tree (ob, item->value, true);
3338 /* Read in jump function JUMP_FUNC from IB. */
3340 static void
3341 ipa_read_jump_function (struct lto_input_block *ib,
3342 struct ipa_jump_func *jump_func,
3343 struct data_in *data_in)
3345 struct bitpack_d bp;
3346 int i, count;
3348 jump_func->type = (enum jump_func_type) streamer_read_uhwi (ib);
3349 switch (jump_func->type)
3351 case IPA_JF_UNKNOWN:
3352 break;
3353 case IPA_JF_KNOWN_TYPE:
3354 jump_func->value.known_type.offset = streamer_read_uhwi (ib);
3355 jump_func->value.known_type.base_type = stream_read_tree (ib, data_in);
3356 jump_func->value.known_type.component_type = stream_read_tree (ib,
3357 data_in);
3358 break;
3359 case IPA_JF_CONST:
3360 jump_func->value.constant = stream_read_tree (ib, data_in);
3361 break;
3362 case IPA_JF_PASS_THROUGH:
3363 jump_func->value.pass_through.operand = stream_read_tree (ib, data_in);
3364 jump_func->value.pass_through.formal_id = streamer_read_uhwi (ib);
3365 jump_func->value.pass_through.operation
3366 = (enum tree_code) streamer_read_uhwi (ib);
3367 bp = streamer_read_bitpack (ib);
3368 jump_func->value.pass_through.agg_preserved = bp_unpack_value (&bp, 1);
3369 break;
3370 case IPA_JF_ANCESTOR:
3371 jump_func->value.ancestor.offset = streamer_read_uhwi (ib);
3372 jump_func->value.ancestor.type = stream_read_tree (ib, data_in);
3373 jump_func->value.ancestor.formal_id = streamer_read_uhwi (ib);
3374 bp = streamer_read_bitpack (ib);
3375 jump_func->value.ancestor.agg_preserved = bp_unpack_value (&bp, 1);
3376 break;
3379 count = streamer_read_uhwi (ib);
3380 vec_alloc (jump_func->agg.items, count);
3381 if (count)
3383 bp = streamer_read_bitpack (ib);
3384 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
3386 for (i = 0; i < count; i++)
3388 struct ipa_agg_jf_item item;
3389 item.offset = streamer_read_uhwi (ib);
3390 item.value = stream_read_tree (ib, data_in);
3391 jump_func->agg.items->quick_push (item);
3395 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
3396 relevant to indirect inlining to OB. */
3398 static void
3399 ipa_write_indirect_edge_info (struct output_block *ob,
3400 struct cgraph_edge *cs)
3402 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3403 struct bitpack_d bp;
3405 streamer_write_hwi (ob, ii->param_index);
3406 streamer_write_hwi (ob, ii->offset);
3407 bp = bitpack_create (ob->main_stream);
3408 bp_pack_value (&bp, ii->polymorphic, 1);
3409 bp_pack_value (&bp, ii->agg_contents, 1);
3410 bp_pack_value (&bp, ii->by_ref, 1);
3411 streamer_write_bitpack (&bp);
3413 if (ii->polymorphic)
3415 streamer_write_hwi (ob, ii->otr_token);
3416 stream_write_tree (ob, ii->otr_type, true);
3420 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
3421 relevant to indirect inlining from IB. */
3423 static void
3424 ipa_read_indirect_edge_info (struct lto_input_block *ib,
3425 struct data_in *data_in ATTRIBUTE_UNUSED,
3426 struct cgraph_edge *cs)
3428 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3429 struct bitpack_d bp;
3431 ii->param_index = (int) streamer_read_hwi (ib);
3432 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
3433 bp = streamer_read_bitpack (ib);
3434 ii->polymorphic = bp_unpack_value (&bp, 1);
3435 ii->agg_contents = bp_unpack_value (&bp, 1);
3436 ii->by_ref = bp_unpack_value (&bp, 1);
3437 if (ii->polymorphic)
3439 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
3440 ii->otr_type = stream_read_tree (ib, data_in);
3444 /* Stream out NODE info to OB. */
3446 static void
3447 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
3449 int node_ref;
3450 lto_symtab_encoder_t encoder;
3451 struct ipa_node_params *info = IPA_NODE_REF (node);
3452 int j;
3453 struct cgraph_edge *e;
3454 struct bitpack_d bp;
3456 encoder = ob->decl_state->symtab_node_encoder;
3457 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
3458 streamer_write_uhwi (ob, node_ref);
3460 bp = bitpack_create (ob->main_stream);
3461 gcc_assert (info->uses_analysis_done
3462 || ipa_get_param_count (info) == 0);
3463 gcc_assert (!info->node_enqueued);
3464 gcc_assert (!info->ipcp_orig_node);
3465 for (j = 0; j < ipa_get_param_count (info); j++)
3466 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
3467 streamer_write_bitpack (&bp);
3468 for (e = node->callees; e; e = e->next_callee)
3470 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3472 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3473 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3474 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3476 for (e = node->indirect_calls; e; e = e->next_callee)
3478 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3480 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3481 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3482 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3483 ipa_write_indirect_edge_info (ob, e);
3487 /* Stream in NODE info from IB. */
3489 static void
3490 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
3491 struct data_in *data_in)
3493 struct ipa_node_params *info = IPA_NODE_REF (node);
3494 int k;
3495 struct cgraph_edge *e;
3496 struct bitpack_d bp;
3498 ipa_initialize_node_params (node);
3500 bp = streamer_read_bitpack (ib);
3501 if (ipa_get_param_count (info) != 0)
3502 info->uses_analysis_done = true;
3503 info->node_enqueued = false;
3504 for (k = 0; k < ipa_get_param_count (info); k++)
3505 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
3506 for (e = node->callees; e; e = e->next_callee)
3508 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3509 int count = streamer_read_uhwi (ib);
3511 if (!count)
3512 continue;
3513 vec_safe_grow_cleared (args->jump_functions, count);
3515 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3516 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
3518 for (e = node->indirect_calls; e; e = e->next_callee)
3520 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3521 int count = streamer_read_uhwi (ib);
3523 if (count)
3525 vec_safe_grow_cleared (args->jump_functions, count);
3526 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3527 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k),
3528 data_in);
3530 ipa_read_indirect_edge_info (ib, data_in, e);
3534 /* Write jump functions for nodes in SET. */
3536 void
3537 ipa_prop_write_jump_functions (void)
3539 struct cgraph_node *node;
3540 struct output_block *ob;
3541 unsigned int count = 0;
3542 lto_symtab_encoder_iterator lsei;
3543 lto_symtab_encoder_t encoder;
3546 if (!ipa_node_params_vector.exists ())
3547 return;
3549 ob = create_output_block (LTO_section_jump_functions);
3550 encoder = ob->decl_state->symtab_node_encoder;
3551 ob->cgraph_node = NULL;
3552 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3553 lsei_next_function_in_partition (&lsei))
3555 node = lsei_cgraph_node (lsei);
3556 if (cgraph_function_with_gimple_body_p (node)
3557 && IPA_NODE_REF (node) != NULL)
3558 count++;
3561 streamer_write_uhwi (ob, count);
3563 /* Process all of the functions. */
3564 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3565 lsei_next_function_in_partition (&lsei))
3567 node = lsei_cgraph_node (lsei);
3568 if (cgraph_function_with_gimple_body_p (node)
3569 && IPA_NODE_REF (node) != NULL)
3570 ipa_write_node_info (ob, node);
3572 streamer_write_char_stream (ob->main_stream, 0);
3573 produce_asm (ob, NULL);
3574 destroy_output_block (ob);
3577 /* Read section in file FILE_DATA of length LEN with data DATA. */
3579 static void
3580 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
3581 size_t len)
3583 const struct lto_function_header *header =
3584 (const struct lto_function_header *) data;
3585 const int cfg_offset = sizeof (struct lto_function_header);
3586 const int main_offset = cfg_offset + header->cfg_size;
3587 const int string_offset = main_offset + header->main_size;
3588 struct data_in *data_in;
3589 struct lto_input_block ib_main;
3590 unsigned int i;
3591 unsigned int count;
3593 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
3594 header->main_size);
3596 data_in =
3597 lto_data_in_create (file_data, (const char *) data + string_offset,
3598 header->string_size, vNULL);
3599 count = streamer_read_uhwi (&ib_main);
3601 for (i = 0; i < count; i++)
3603 unsigned int index;
3604 struct cgraph_node *node;
3605 lto_symtab_encoder_t encoder;
3607 index = streamer_read_uhwi (&ib_main);
3608 encoder = file_data->symtab_node_encoder;
3609 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3610 gcc_assert (node->analyzed);
3611 ipa_read_node_info (&ib_main, node, data_in);
3613 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
3614 len);
3615 lto_data_in_delete (data_in);
3618 /* Read ipcp jump functions. */
3620 void
3621 ipa_prop_read_jump_functions (void)
3623 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3624 struct lto_file_decl_data *file_data;
3625 unsigned int j = 0;
3627 ipa_check_create_node_params ();
3628 ipa_check_create_edge_args ();
3629 ipa_register_cgraph_hooks ();
3631 while ((file_data = file_data_vec[j++]))
3633 size_t len;
3634 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
3636 if (data)
3637 ipa_prop_read_section (file_data, data, len);
3641 /* After merging units, we can get mismatch in argument counts.
3642 Also decl merging might've rendered parameter lists obsolete.
3643 Also compute called_with_variable_arg info. */
3645 void
3646 ipa_update_after_lto_read (void)
3648 struct cgraph_node *node;
3650 ipa_check_create_node_params ();
3651 ipa_check_create_edge_args ();
3653 FOR_EACH_DEFINED_FUNCTION (node)
3654 if (node->analyzed)
3655 ipa_initialize_node_params (node);
3658 void
3659 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
3661 int node_ref;
3662 unsigned int count = 0;
3663 lto_symtab_encoder_t encoder;
3664 struct ipa_agg_replacement_value *aggvals, *av;
3666 aggvals = ipa_get_agg_replacements_for_node (node);
3667 encoder = ob->decl_state->symtab_node_encoder;
3668 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
3669 streamer_write_uhwi (ob, node_ref);
3671 for (av = aggvals; av; av = av->next)
3672 count++;
3673 streamer_write_uhwi (ob, count);
3675 for (av = aggvals; av; av = av->next)
3677 streamer_write_uhwi (ob, av->offset);
3678 streamer_write_uhwi (ob, av->index);
3679 stream_write_tree (ob, av->value, true);
3683 /* Stream in the aggregate value replacement chain for NODE from IB. */
3685 static void
3686 read_agg_replacement_chain (struct lto_input_block *ib,
3687 struct cgraph_node *node,
3688 struct data_in *data_in)
3690 struct ipa_agg_replacement_value *aggvals = NULL;
3691 unsigned int count, i;
3693 count = streamer_read_uhwi (ib);
3694 for (i = 0; i <count; i++)
3696 struct ipa_agg_replacement_value *av;
3698 av = ggc_alloc_ipa_agg_replacement_value ();
3699 av->offset = streamer_read_uhwi (ib);
3700 av->index = streamer_read_uhwi (ib);
3701 av->value = stream_read_tree (ib, data_in);
3702 av->next = aggvals;
3703 aggvals = av;
3705 ipa_set_node_agg_value_chain (node, aggvals);
3708 /* Write all aggregate replacement for nodes in set. */
3710 void
3711 ipa_prop_write_all_agg_replacement (void)
3713 struct cgraph_node *node;
3714 struct output_block *ob;
3715 unsigned int count = 0;
3716 lto_symtab_encoder_iterator lsei;
3717 lto_symtab_encoder_t encoder;
3719 if (!ipa_node_agg_replacements)
3720 return;
3722 ob = create_output_block (LTO_section_ipcp_transform);
3723 encoder = ob->decl_state->symtab_node_encoder;
3724 ob->cgraph_node = NULL;
3725 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3726 lsei_next_function_in_partition (&lsei))
3728 node = lsei_cgraph_node (lsei);
3729 if (cgraph_function_with_gimple_body_p (node)
3730 && ipa_get_agg_replacements_for_node (node) != NULL)
3731 count++;
3734 streamer_write_uhwi (ob, count);
3736 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3737 lsei_next_function_in_partition (&lsei))
3739 node = lsei_cgraph_node (lsei);
3740 if (cgraph_function_with_gimple_body_p (node)
3741 && ipa_get_agg_replacements_for_node (node) != NULL)
3742 write_agg_replacement_chain (ob, node);
3744 streamer_write_char_stream (ob->main_stream, 0);
3745 produce_asm (ob, NULL);
3746 destroy_output_block (ob);
3749 /* Read replacements section in file FILE_DATA of length LEN with data
3750 DATA. */
3752 static void
3753 read_replacements_section (struct lto_file_decl_data *file_data,
3754 const char *data,
3755 size_t len)
3757 const struct lto_function_header *header =
3758 (const struct lto_function_header *) data;
3759 const int cfg_offset = sizeof (struct lto_function_header);
3760 const int main_offset = cfg_offset + header->cfg_size;
3761 const int string_offset = main_offset + header->main_size;
3762 struct data_in *data_in;
3763 struct lto_input_block ib_main;
3764 unsigned int i;
3765 unsigned int count;
3767 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
3768 header->main_size);
3770 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
3771 header->string_size, vNULL);
3772 count = streamer_read_uhwi (&ib_main);
3774 for (i = 0; i < count; i++)
3776 unsigned int index;
3777 struct cgraph_node *node;
3778 lto_symtab_encoder_t encoder;
3780 index = streamer_read_uhwi (&ib_main);
3781 encoder = file_data->symtab_node_encoder;
3782 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3783 gcc_assert (node->analyzed);
3784 read_agg_replacement_chain (&ib_main, node, data_in);
3786 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
3787 len);
3788 lto_data_in_delete (data_in);
3791 /* Read IPA-CP aggregate replacements. */
3793 void
3794 ipa_prop_read_all_agg_replacement (void)
3796 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3797 struct lto_file_decl_data *file_data;
3798 unsigned int j = 0;
3800 while ((file_data = file_data_vec[j++]))
3802 size_t len;
3803 const char *data = lto_get_section_data (file_data,
3804 LTO_section_ipcp_transform,
3805 NULL, &len);
3806 if (data)
3807 read_replacements_section (file_data, data, len);
3811 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
3812 NODE. */
3814 static void
3815 adjust_agg_replacement_values (struct cgraph_node *node,
3816 struct ipa_agg_replacement_value *aggval)
3818 struct ipa_agg_replacement_value *v;
3819 int i, c = 0, d = 0, *adj;
3821 if (!node->clone.combined_args_to_skip)
3822 return;
3824 for (v = aggval; v; v = v->next)
3826 gcc_assert (v->index >= 0);
3827 if (c < v->index)
3828 c = v->index;
3830 c++;
3832 adj = XALLOCAVEC (int, c);
3833 for (i = 0; i < c; i++)
3834 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
3836 adj[i] = -1;
3837 d++;
3839 else
3840 adj[i] = i - d;
3842 for (v = aggval; v; v = v->next)
3843 v->index = adj[v->index];
3847 /* Function body transformation phase. */
3849 unsigned int
3850 ipcp_transform_function (struct cgraph_node *node)
3852 vec<ipa_param_descriptor_t> descriptors = vNULL;
3853 struct param_analysis_info *parms_ainfo;
3854 struct ipa_agg_replacement_value *aggval;
3855 gimple_stmt_iterator gsi;
3856 basic_block bb;
3857 int param_count;
3858 bool cfg_changed = false, something_changed = false;
3860 gcc_checking_assert (cfun);
3861 gcc_checking_assert (current_function_decl);
3863 if (dump_file)
3864 fprintf (dump_file, "Modification phase of node %s/%i\n",
3865 cgraph_node_name (node), node->uid);
3867 aggval = ipa_get_agg_replacements_for_node (node);
3868 if (!aggval)
3869 return 0;
3870 param_count = count_formal_params (node->symbol.decl);
3871 if (param_count == 0)
3872 return 0;
3873 adjust_agg_replacement_values (node, aggval);
3874 if (dump_file)
3875 ipa_dump_agg_replacement_values (dump_file, aggval);
3876 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
3877 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
3878 descriptors.safe_grow_cleared (param_count);
3879 ipa_populate_param_decls (node, descriptors);
3881 FOR_EACH_BB (bb)
3882 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3884 struct ipa_agg_replacement_value *v;
3885 gimple stmt = gsi_stmt (gsi);
3886 tree rhs, val, t;
3887 HOST_WIDE_INT offset;
3888 int index;
3889 bool by_ref, vce;
3891 if (!gimple_assign_load_p (stmt))
3892 continue;
3893 rhs = gimple_assign_rhs1 (stmt);
3894 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
3895 continue;
3897 vce = false;
3898 t = rhs;
3899 while (handled_component_p (t))
3901 /* V_C_E can do things like convert an array of integers to one
3902 bigger integer and similar things we do not handle below. */
3903 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
3905 vce = true;
3906 break;
3908 t = TREE_OPERAND (t, 0);
3910 if (vce)
3911 continue;
3913 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
3914 rhs, &index, &offset, &by_ref))
3915 continue;
3916 for (v = aggval; v; v = v->next)
3917 if (v->index == index
3918 && v->offset == offset)
3919 break;
3920 if (!v)
3921 continue;
3923 gcc_checking_assert (is_gimple_ip_invariant (v->value));
3924 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
3926 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
3927 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
3928 else if (TYPE_SIZE (TREE_TYPE (rhs))
3929 == TYPE_SIZE (TREE_TYPE (v->value)))
3930 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
3931 else
3933 if (dump_file)
3935 fprintf (dump_file, " const ");
3936 print_generic_expr (dump_file, v->value, 0);
3937 fprintf (dump_file, " can't be converted to type of ");
3938 print_generic_expr (dump_file, rhs, 0);
3939 fprintf (dump_file, "\n");
3941 continue;
3944 else
3945 val = v->value;
3947 if (dump_file && (dump_flags & TDF_DETAILS))
3949 fprintf (dump_file, "Modifying stmt:\n ");
3950 print_gimple_stmt (dump_file, stmt, 0, 0);
3952 gimple_assign_set_rhs_from_tree (&gsi, val);
3953 update_stmt (stmt);
3955 if (dump_file && (dump_flags & TDF_DETAILS))
3957 fprintf (dump_file, "into:\n ");
3958 print_gimple_stmt (dump_file, stmt, 0, 0);
3959 fprintf (dump_file, "\n");
3962 something_changed = true;
3963 if (maybe_clean_eh_stmt (stmt)
3964 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
3965 cfg_changed = true;
3968 (*ipa_node_agg_replacements)[node->uid] = NULL;
3969 free_parms_ainfo (parms_ainfo, param_count);
3970 descriptors.release ();
3972 if (!something_changed)
3973 return 0;
3974 else if (cfg_changed)
3975 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
3976 else
3977 return TODO_update_ssa_only_virtuals;