Mark ChangeLog
[official-gcc.git] / gcc / ipa-prop.c
blob1f638442503d9c08b87d1d9ad5bc579a8f8c0889
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 if (optimize)
628 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
629 ao_ref_init (&refd, parm_load);
630 /* We can cache visited statements only when parm_ainfo is available and
631 when we are looking at a naked load of the whole parameter. */
632 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
633 visited_stmts = NULL;
634 else
635 visited_stmts = &parm_ainfo->parm_visited_statements;
636 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
637 visited_stmts);
639 else
640 modified = true;
642 if (parm_ainfo && modified)
643 parm_ainfo->parm_modified = true;
644 return !modified;
647 /* If STMT is an assignment that loads a value from an parameter declaration,
648 return the index of the parameter in ipa_node_params which has not been
649 modified. Otherwise return -1. */
651 static int
652 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
653 struct param_analysis_info *parms_ainfo,
654 gimple stmt)
656 int index;
657 tree op1;
659 if (!gimple_assign_single_p (stmt))
660 return -1;
662 op1 = gimple_assign_rhs1 (stmt);
663 if (TREE_CODE (op1) != PARM_DECL)
664 return -1;
666 index = ipa_get_param_decl_index_1 (descriptors, op1);
667 if (index < 0
668 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
669 : NULL, stmt, op1))
670 return -1;
672 return index;
675 /* Return true if memory reference REF loads data that are known to be
676 unmodified in this function before reaching statement STMT. PARM_AINFO, if
677 non-NULL, is a pointer to a structure containing temporary information about
678 PARM. */
680 static bool
681 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
682 gimple stmt, tree ref)
684 bool modified = false;
685 ao_ref refd;
687 if (parm_ainfo && parm_ainfo->ref_modified)
688 return false;
690 if (optimize)
692 gcc_checking_assert (gimple_vuse (stmt));
693 ao_ref_init (&refd, ref);
694 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
695 NULL);
697 else
698 modified = true;
700 if (parm_ainfo && modified)
701 parm_ainfo->ref_modified = true;
702 return !modified;
705 /* Return true if the data pointed to by PARM is known to be unmodified in this
706 function before reaching call statement CALL into which it is passed.
707 PARM_AINFO is a pointer to a structure containing temporary information
708 about PARM. */
710 static bool
711 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
712 gimple call, tree parm)
714 bool modified = false;
715 ao_ref refd;
717 /* It's unnecessary to calculate anything about memory contnets for a const
718 function because it is not goin to use it. But do not cache the result
719 either. Also, no such calculations for non-pointers. */
720 if (!gimple_vuse (call)
721 || !POINTER_TYPE_P (TREE_TYPE (parm)))
722 return false;
724 if (parm_ainfo->pt_modified)
725 return false;
727 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
728 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
729 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
730 if (modified)
731 parm_ainfo->pt_modified = true;
732 return !modified;
735 /* Return true if we can prove that OP is a memory reference loading unmodified
736 data from an aggregate passed as a parameter and if the aggregate is passed
737 by reference, that the alias type of the load corresponds to the type of the
738 formal parameter (so that we can rely on this type for TBAA in callers).
739 INFO and PARMS_AINFO describe parameters of the current function (but the
740 latter can be NULL), STMT is the load statement. If function returns true,
741 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
742 within the aggregate and whether it is a load from a value passed by
743 reference respectively. */
745 static bool
746 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
747 struct param_analysis_info *parms_ainfo, gimple stmt,
748 tree op, int *index_p, HOST_WIDE_INT *offset_p,
749 HOST_WIDE_INT *size_p, bool *by_ref_p)
751 int index;
752 HOST_WIDE_INT size, max_size;
753 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
755 if (max_size == -1 || max_size != size || *offset_p < 0)
756 return false;
758 if (DECL_P (base))
760 int index = ipa_get_param_decl_index_1 (descriptors, base);
761 if (index >= 0
762 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
763 : NULL, stmt, op))
765 *index_p = index;
766 *by_ref_p = false;
767 if (size_p)
768 *size_p = size;
769 return true;
771 return false;
774 if (TREE_CODE (base) != MEM_REF
775 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
776 || !integer_zerop (TREE_OPERAND (base, 1)))
777 return false;
779 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
781 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
782 index = ipa_get_param_decl_index_1 (descriptors, parm);
784 else
786 /* This branch catches situations where a pointer parameter is not a
787 gimple register, for example:
789 void hip7(S*) (struct S * p)
791 void (*<T2e4>) (struct S *) D.1867;
792 struct S * p.1;
794 <bb 2>:
795 p.1_1 = p;
796 D.1867_2 = p.1_1->f;
797 D.1867_2 ();
798 gdp = &p;
801 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
802 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
805 if (index >= 0
806 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
807 stmt, op))
809 *index_p = index;
810 *by_ref_p = true;
811 if (size_p)
812 *size_p = size;
813 return true;
815 return false;
818 /* Just like the previous function, just without the param_analysis_info
819 pointer, for users outside of this file. */
821 bool
822 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
823 tree op, int *index_p, HOST_WIDE_INT *offset_p,
824 bool *by_ref_p)
826 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
827 offset_p, NULL, by_ref_p);
830 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
831 of an assignment statement STMT, try to determine whether we are actually
832 handling any of the following cases and construct an appropriate jump
833 function into JFUNC if so:
835 1) The passed value is loaded from a formal parameter which is not a gimple
836 register (most probably because it is addressable, the value has to be
837 scalar) and we can guarantee the value has not changed. This case can
838 therefore be described by a simple pass-through jump function. For example:
840 foo (int a)
842 int a.0;
844 a.0_2 = a;
845 bar (a.0_2);
847 2) The passed value can be described by a simple arithmetic pass-through
848 jump function. E.g.
850 foo (int a)
852 int D.2064;
854 D.2064_4 = a.1(D) + 4;
855 bar (D.2064_4);
857 This case can also occur in combination of the previous one, e.g.:
859 foo (int a, int z)
861 int a.0;
862 int D.2064;
864 a.0_3 = a;
865 D.2064_4 = a.0_3 + 4;
866 foo (D.2064_4);
868 3) The passed value is an address of an object within another one (which
869 also passed by reference). Such situations are described by an ancestor
870 jump function and describe situations such as:
872 B::foo() (struct B * const this)
874 struct A * D.1845;
876 D.1845_2 = &this_1(D)->D.1748;
877 A::bar (D.1845_2);
879 INFO is the structure describing individual parameters access different
880 stages of IPA optimizations. PARMS_AINFO contains the information that is
881 only needed for intraprocedural analysis. */
883 static void
884 compute_complex_assign_jump_func (struct ipa_node_params *info,
885 struct param_analysis_info *parms_ainfo,
886 struct ipa_jump_func *jfunc,
887 gimple call, gimple stmt, tree name)
889 HOST_WIDE_INT offset, size, max_size;
890 tree op1, tc_ssa, base, ssa;
891 int index;
893 op1 = gimple_assign_rhs1 (stmt);
895 if (TREE_CODE (op1) == SSA_NAME)
897 if (SSA_NAME_IS_DEFAULT_DEF (op1))
898 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
899 else
900 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
901 SSA_NAME_DEF_STMT (op1));
902 tc_ssa = op1;
904 else
906 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
907 tc_ssa = gimple_assign_lhs (stmt);
910 if (index >= 0)
912 tree op2 = gimple_assign_rhs2 (stmt);
914 if (op2)
916 if (!is_gimple_ip_invariant (op2)
917 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
918 && !useless_type_conversion_p (TREE_TYPE (name),
919 TREE_TYPE (op1))))
920 return;
922 ipa_set_jf_arith_pass_through (jfunc, index, op2,
923 gimple_assign_rhs_code (stmt));
925 else if (gimple_assign_single_p (stmt)
926 && !detect_type_change_ssa (tc_ssa, call, jfunc))
928 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
929 call, tc_ssa);
930 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
932 return;
935 if (TREE_CODE (op1) != ADDR_EXPR)
936 return;
937 op1 = TREE_OPERAND (op1, 0);
938 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
939 return;
940 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
941 if (TREE_CODE (base) != MEM_REF
942 /* If this is a varying address, punt. */
943 || max_size == -1
944 || max_size != size)
945 return;
946 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
947 ssa = TREE_OPERAND (base, 0);
948 if (TREE_CODE (ssa) != SSA_NAME
949 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
950 || offset < 0)
951 return;
953 /* Dynamic types are changed only in constructors and destructors and */
954 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
955 if (index >= 0
956 && !detect_type_change (op1, base, call, jfunc, offset))
957 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
958 parm_ref_data_pass_through_p (&parms_ainfo[index],
959 call, ssa));
962 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
963 it looks like:
965 iftmp.1_3 = &obj_2(D)->D.1762;
967 The base of the MEM_REF must be a default definition SSA NAME of a
968 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
969 whole MEM_REF expression is returned and the offset calculated from any
970 handled components and the MEM_REF itself is stored into *OFFSET. The whole
971 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
973 static tree
974 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
976 HOST_WIDE_INT size, max_size;
977 tree expr, parm, obj;
979 if (!gimple_assign_single_p (assign))
980 return NULL_TREE;
981 expr = gimple_assign_rhs1 (assign);
983 if (TREE_CODE (expr) != ADDR_EXPR)
984 return NULL_TREE;
985 expr = TREE_OPERAND (expr, 0);
986 obj = expr;
987 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
989 if (TREE_CODE (expr) != MEM_REF
990 /* If this is a varying address, punt. */
991 || max_size == -1
992 || max_size != size
993 || *offset < 0)
994 return NULL_TREE;
995 parm = TREE_OPERAND (expr, 0);
996 if (TREE_CODE (parm) != SSA_NAME
997 || !SSA_NAME_IS_DEFAULT_DEF (parm)
998 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
999 return NULL_TREE;
1001 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1002 *obj_p = obj;
1003 return expr;
1007 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1008 statement PHI, try to find out whether NAME is in fact a
1009 multiple-inheritance typecast from a descendant into an ancestor of a formal
1010 parameter and thus can be described by an ancestor jump function and if so,
1011 write the appropriate function into JFUNC.
1013 Essentially we want to match the following pattern:
1015 if (obj_2(D) != 0B)
1016 goto <bb 3>;
1017 else
1018 goto <bb 4>;
1020 <bb 3>:
1021 iftmp.1_3 = &obj_2(D)->D.1762;
1023 <bb 4>:
1024 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1025 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1026 return D.1879_6; */
1028 static void
1029 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1030 struct param_analysis_info *parms_ainfo,
1031 struct ipa_jump_func *jfunc,
1032 gimple call, gimple phi)
1034 HOST_WIDE_INT offset;
1035 gimple assign, cond;
1036 basic_block phi_bb, assign_bb, cond_bb;
1037 tree tmp, parm, expr, obj;
1038 int index, i;
1040 if (gimple_phi_num_args (phi) != 2)
1041 return;
1043 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1044 tmp = PHI_ARG_DEF (phi, 0);
1045 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1046 tmp = PHI_ARG_DEF (phi, 1);
1047 else
1048 return;
1049 if (TREE_CODE (tmp) != SSA_NAME
1050 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1051 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1052 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1053 return;
1055 assign = SSA_NAME_DEF_STMT (tmp);
1056 assign_bb = gimple_bb (assign);
1057 if (!single_pred_p (assign_bb))
1058 return;
1059 expr = get_ancestor_addr_info (assign, &obj, &offset);
1060 if (!expr)
1061 return;
1062 parm = TREE_OPERAND (expr, 0);
1063 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1064 if (index < 0)
1065 return;
1067 cond_bb = single_pred (assign_bb);
1068 cond = last_stmt (cond_bb);
1069 if (!cond
1070 || gimple_code (cond) != GIMPLE_COND
1071 || gimple_cond_code (cond) != NE_EXPR
1072 || gimple_cond_lhs (cond) != parm
1073 || !integer_zerop (gimple_cond_rhs (cond)))
1074 return;
1076 phi_bb = gimple_bb (phi);
1077 for (i = 0; i < 2; i++)
1079 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1080 if (pred != assign_bb && pred != cond_bb)
1081 return;
1084 if (!detect_type_change (obj, expr, call, jfunc, offset))
1085 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1086 parm_ref_data_pass_through_p (&parms_ainfo[index],
1087 call, parm));
1090 /* Given OP which is passed as an actual argument to a called function,
1091 determine if it is possible to construct a KNOWN_TYPE jump function for it
1092 and if so, create one and store it to JFUNC. */
1094 static void
1095 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1096 gimple call)
1098 HOST_WIDE_INT offset, size, max_size;
1099 tree base;
1101 if (!flag_devirtualize
1102 || TREE_CODE (op) != ADDR_EXPR
1103 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
1104 return;
1106 op = TREE_OPERAND (op, 0);
1107 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1108 if (!DECL_P (base)
1109 || max_size == -1
1110 || max_size != size
1111 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1112 || is_global_var (base))
1113 return;
1115 if (!TYPE_BINFO (TREE_TYPE (base))
1116 || detect_type_change (op, base, call, jfunc, offset))
1117 return;
1119 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base), TREE_TYPE (op));
1122 /* Inspect the given TYPE and return true iff it has the same structure (the
1123 same number of fields of the same types) as a C++ member pointer. If
1124 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1125 corresponding fields there. */
1127 static bool
1128 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1130 tree fld;
1132 if (TREE_CODE (type) != RECORD_TYPE)
1133 return false;
1135 fld = TYPE_FIELDS (type);
1136 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1137 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1138 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1139 return false;
1141 if (method_ptr)
1142 *method_ptr = fld;
1144 fld = DECL_CHAIN (fld);
1145 if (!fld || INTEGRAL_TYPE_P (fld)
1146 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1147 return false;
1148 if (delta)
1149 *delta = fld;
1151 if (DECL_CHAIN (fld))
1152 return false;
1154 return true;
1157 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1158 return the rhs of its defining statement. Otherwise return RHS as it
1159 is. */
1161 static inline tree
1162 get_ssa_def_if_simple_copy (tree rhs)
1164 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1166 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1168 if (gimple_assign_single_p (def_stmt))
1169 rhs = gimple_assign_rhs1 (def_stmt);
1170 else
1171 break;
1173 return rhs;
1176 /* Simple linked list, describing known contents of an aggregate beforere
1177 call. */
1179 struct ipa_known_agg_contents_list
1181 /* Offset and size of the described part of the aggregate. */
1182 HOST_WIDE_INT offset, size;
1183 /* Known constant value or NULL if the contents is known to be unknown. */
1184 tree constant;
1185 /* Pointer to the next structure in the list. */
1186 struct ipa_known_agg_contents_list *next;
1189 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1190 in ARG is filled in with constant values. ARG can either be an aggregate
1191 expression or a pointer to an aggregate. JFUNC is the jump function into
1192 which the constants are subsequently stored. */
1194 static void
1195 determine_known_aggregate_parts (gimple call, tree arg,
1196 struct ipa_jump_func *jfunc)
1198 struct ipa_known_agg_contents_list *list = NULL;
1199 int item_count = 0, const_count = 0;
1200 HOST_WIDE_INT arg_offset, arg_size;
1201 gimple_stmt_iterator gsi;
1202 tree arg_base;
1203 bool check_ref, by_ref;
1204 ao_ref r;
1206 /* The function operates in three stages. First, we prepare check_ref, r,
1207 arg_base and arg_offset based on what is actually passed as an actual
1208 argument. */
1210 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1212 by_ref = true;
1213 if (TREE_CODE (arg) == SSA_NAME)
1215 tree type_size;
1216 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1217 return;
1218 check_ref = true;
1219 arg_base = arg;
1220 arg_offset = 0;
1221 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1222 arg_size = tree_low_cst (type_size, 1);
1223 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1225 else if (TREE_CODE (arg) == ADDR_EXPR)
1227 HOST_WIDE_INT arg_max_size;
1229 arg = TREE_OPERAND (arg, 0);
1230 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1231 &arg_max_size);
1232 if (arg_max_size == -1
1233 || arg_max_size != arg_size
1234 || arg_offset < 0)
1235 return;
1236 if (DECL_P (arg_base))
1238 tree size;
1239 check_ref = false;
1240 size = build_int_cst (integer_type_node, arg_size);
1241 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1243 else
1244 return;
1246 else
1247 return;
1249 else
1251 HOST_WIDE_INT arg_max_size;
1253 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1255 by_ref = false;
1256 check_ref = false;
1257 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1258 &arg_max_size);
1259 if (arg_max_size == -1
1260 || arg_max_size != arg_size
1261 || arg_offset < 0)
1262 return;
1264 ao_ref_init (&r, arg);
1267 /* Second stage walks back the BB, looks at individual statements and as long
1268 as it is confident of how the statements affect contents of the
1269 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1270 describing it. */
1271 gsi = gsi_for_stmt (call);
1272 gsi_prev (&gsi);
1273 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1275 struct ipa_known_agg_contents_list *n, **p;
1276 gimple stmt = gsi_stmt (gsi);
1277 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1278 tree lhs, rhs, lhs_base;
1279 bool partial_overlap;
1281 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1282 continue;
1283 if (!gimple_assign_single_p (stmt))
1284 break;
1286 lhs = gimple_assign_lhs (stmt);
1287 rhs = gimple_assign_rhs1 (stmt);
1288 if (!is_gimple_reg_type (rhs)
1289 || TREE_CODE (lhs) == BIT_FIELD_REF
1290 || contains_bitfld_component_ref_p (lhs))
1291 break;
1293 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1294 &lhs_max_size);
1295 if (lhs_max_size == -1
1296 || lhs_max_size != lhs_size
1297 || (lhs_offset < arg_offset
1298 && lhs_offset + lhs_size > arg_offset)
1299 || (lhs_offset < arg_offset + arg_size
1300 && lhs_offset + lhs_size > arg_offset + arg_size))
1301 break;
1303 if (check_ref)
1305 if (TREE_CODE (lhs_base) != MEM_REF
1306 || TREE_OPERAND (lhs_base, 0) != arg_base
1307 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1308 break;
1310 else if (lhs_base != arg_base)
1312 if (DECL_P (lhs_base))
1313 continue;
1314 else
1315 break;
1318 if (lhs_offset + lhs_size < arg_offset
1319 || lhs_offset >= (arg_offset + arg_size))
1320 continue;
1322 partial_overlap = false;
1323 p = &list;
1324 while (*p && (*p)->offset < lhs_offset)
1326 if ((*p)->offset + (*p)->size > lhs_offset)
1328 partial_overlap = true;
1329 break;
1331 p = &(*p)->next;
1333 if (partial_overlap)
1334 break;
1335 if (*p && (*p)->offset < lhs_offset + lhs_size)
1337 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1338 /* We already know this value is subsequently overwritten with
1339 something else. */
1340 continue;
1341 else
1342 /* Otherwise this is a partial overlap which we cannot
1343 represent. */
1344 break;
1347 rhs = get_ssa_def_if_simple_copy (rhs);
1348 n = XALLOCA (struct ipa_known_agg_contents_list);
1349 n->size = lhs_size;
1350 n->offset = lhs_offset;
1351 if (is_gimple_ip_invariant (rhs))
1353 n->constant = rhs;
1354 const_count++;
1356 else
1357 n->constant = NULL_TREE;
1358 n->next = *p;
1359 *p = n;
1361 item_count++;
1362 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1363 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1364 break;
1367 /* Third stage just goes over the list and creates an appropriate vector of
1368 ipa_agg_jf_item structures out of it, of sourse only if there are
1369 any known constants to begin with. */
1371 if (const_count)
1373 jfunc->agg.by_ref = by_ref;
1374 vec_alloc (jfunc->agg.items, const_count);
1375 while (list)
1377 if (list->constant)
1379 struct ipa_agg_jf_item item;
1380 item.offset = list->offset - arg_offset;
1381 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1382 item.value = unshare_expr_without_location (list->constant);
1383 jfunc->agg.items->quick_push (item);
1385 list = list->next;
1390 /* Compute jump function for all arguments of callsite CS and insert the
1391 information in the jump_functions array in the ipa_edge_args corresponding
1392 to this callsite. */
1394 static void
1395 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1396 struct cgraph_edge *cs)
1398 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1399 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1400 gimple call = cs->call_stmt;
1401 int n, arg_num = gimple_call_num_args (call);
1403 if (arg_num == 0 || args->jump_functions)
1404 return;
1405 vec_safe_grow_cleared (args->jump_functions, arg_num);
1407 for (n = 0; n < arg_num; n++)
1409 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1410 tree arg = gimple_call_arg (call, n);
1412 if (is_gimple_ip_invariant (arg))
1413 ipa_set_jf_constant (jfunc, arg);
1414 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1415 && TREE_CODE (arg) == PARM_DECL)
1417 int index = ipa_get_param_decl_index (info, arg);
1419 gcc_assert (index >=0);
1420 /* Aggregate passed by value, check for pass-through, otherwise we
1421 will attempt to fill in aggregate contents later in this
1422 for cycle. */
1423 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1425 ipa_set_jf_simple_pass_through (jfunc, index, false);
1426 continue;
1429 else if (TREE_CODE (arg) == SSA_NAME)
1431 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1433 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1434 if (index >= 0
1435 && !detect_type_change_ssa (arg, call, jfunc))
1437 bool agg_p;
1438 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1439 call, arg);
1440 ipa_set_jf_simple_pass_through (jfunc, index, agg_p);
1443 else
1445 gimple stmt = SSA_NAME_DEF_STMT (arg);
1446 if (is_gimple_assign (stmt))
1447 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1448 call, stmt, arg);
1449 else if (gimple_code (stmt) == GIMPLE_PHI)
1450 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1451 call, stmt);
1454 else
1455 compute_known_type_jump_func (arg, jfunc, call);
1457 if ((jfunc->type != IPA_JF_PASS_THROUGH
1458 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1459 && (jfunc->type != IPA_JF_ANCESTOR
1460 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1461 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1462 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1463 determine_known_aggregate_parts (call, arg, jfunc);
1467 /* Compute jump functions for all edges - both direct and indirect - outgoing
1468 from NODE. Also count the actual arguments in the process. */
1470 static void
1471 ipa_compute_jump_functions (struct cgraph_node *node,
1472 struct param_analysis_info *parms_ainfo)
1474 struct cgraph_edge *cs;
1476 if (!optimize)
1477 return;
1479 for (cs = node->callees; cs; cs = cs->next_callee)
1481 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1482 NULL);
1483 /* We do not need to bother analyzing calls to unknown
1484 functions unless they may become known during lto/whopr. */
1485 if (!callee->analyzed && !flag_lto)
1486 continue;
1487 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1490 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1491 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1494 /* If STMT looks like a statement loading a value from a member pointer formal
1495 parameter, return that parameter and store the offset of the field to
1496 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1497 might be clobbered). If USE_DELTA, then we look for a use of the delta
1498 field rather than the pfn. */
1500 static tree
1501 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1502 HOST_WIDE_INT *offset_p)
1504 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1506 if (!gimple_assign_single_p (stmt))
1507 return NULL_TREE;
1509 rhs = gimple_assign_rhs1 (stmt);
1510 if (TREE_CODE (rhs) == COMPONENT_REF)
1512 ref_field = TREE_OPERAND (rhs, 1);
1513 rhs = TREE_OPERAND (rhs, 0);
1515 else
1516 ref_field = NULL_TREE;
1517 if (TREE_CODE (rhs) != MEM_REF)
1518 return NULL_TREE;
1519 rec = TREE_OPERAND (rhs, 0);
1520 if (TREE_CODE (rec) != ADDR_EXPR)
1521 return NULL_TREE;
1522 rec = TREE_OPERAND (rec, 0);
1523 if (TREE_CODE (rec) != PARM_DECL
1524 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1525 return NULL_TREE;
1526 ref_offset = TREE_OPERAND (rhs, 1);
1528 if (use_delta)
1529 fld = delta_field;
1530 else
1531 fld = ptr_field;
1532 if (offset_p)
1533 *offset_p = int_bit_position (fld);
1535 if (ref_field)
1537 if (integer_nonzerop (ref_offset))
1538 return NULL_TREE;
1539 return ref_field == fld ? rec : NULL_TREE;
1541 else
1542 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1543 : NULL_TREE;
1546 /* Returns true iff T is an SSA_NAME defined by a statement. */
1548 static bool
1549 ipa_is_ssa_with_stmt_def (tree t)
1551 if (TREE_CODE (t) == SSA_NAME
1552 && !SSA_NAME_IS_DEFAULT_DEF (t))
1553 return true;
1554 else
1555 return false;
1558 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1559 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1560 indirect call graph edge. */
1562 static struct cgraph_edge *
1563 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1565 struct cgraph_edge *cs;
1567 cs = cgraph_edge (node, stmt);
1568 cs->indirect_info->param_index = param_index;
1569 cs->indirect_info->offset = 0;
1570 cs->indirect_info->polymorphic = 0;
1571 cs->indirect_info->agg_contents = 0;
1572 return cs;
1575 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1576 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1577 intermediate information about each formal parameter. Currently it checks
1578 whether the call calls a pointer that is a formal parameter and if so, the
1579 parameter is marked with the called flag and an indirect call graph edge
1580 describing the call is created. This is very simple for ordinary pointers
1581 represented in SSA but not-so-nice when it comes to member pointers. The
1582 ugly part of this function does nothing more than trying to match the
1583 pattern of such a call. An example of such a pattern is the gimple dump
1584 below, the call is on the last line:
1586 <bb 2>:
1587 f$__delta_5 = f.__delta;
1588 f$__pfn_24 = f.__pfn;
1591 <bb 2>:
1592 f$__delta_5 = MEM[(struct *)&f];
1593 f$__pfn_24 = MEM[(struct *)&f + 4B];
1595 and a few lines below:
1597 <bb 5>
1598 D.2496_3 = (int) f$__pfn_24;
1599 D.2497_4 = D.2496_3 & 1;
1600 if (D.2497_4 != 0)
1601 goto <bb 3>;
1602 else
1603 goto <bb 4>;
1605 <bb 6>:
1606 D.2500_7 = (unsigned int) f$__delta_5;
1607 D.2501_8 = &S + D.2500_7;
1608 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1609 D.2503_10 = *D.2502_9;
1610 D.2504_12 = f$__pfn_24 + -1;
1611 D.2505_13 = (unsigned int) D.2504_12;
1612 D.2506_14 = D.2503_10 + D.2505_13;
1613 D.2507_15 = *D.2506_14;
1614 iftmp.11_16 = (String:: *) D.2507_15;
1616 <bb 7>:
1617 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1618 D.2500_19 = (unsigned int) f$__delta_5;
1619 D.2508_20 = &S + D.2500_19;
1620 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1622 Such patterns are results of simple calls to a member pointer:
1624 int doprinting (int (MyString::* f)(int) const)
1626 MyString S ("somestring");
1628 return (S.*f)(4);
1631 Moreover, the function also looks for called pointers loaded from aggregates
1632 passed by value or reference. */
1634 static void
1635 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1636 struct ipa_node_params *info,
1637 struct param_analysis_info *parms_ainfo,
1638 gimple call, tree target)
1640 gimple def;
1641 tree n1, n2;
1642 gimple d1, d2;
1643 tree rec, rec2, cond;
1644 gimple branch;
1645 int index;
1646 basic_block bb, virt_bb, join;
1647 HOST_WIDE_INT offset;
1648 bool by_ref;
1650 if (SSA_NAME_IS_DEFAULT_DEF (target))
1652 tree var = SSA_NAME_VAR (target);
1653 index = ipa_get_param_decl_index (info, var);
1654 if (index >= 0)
1655 ipa_note_param_call (node, index, call);
1656 return;
1659 def = SSA_NAME_DEF_STMT (target);
1660 if (gimple_assign_single_p (def)
1661 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1662 gimple_assign_rhs1 (def), &index, &offset,
1663 NULL, &by_ref))
1665 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1666 cs->indirect_info->offset = offset;
1667 cs->indirect_info->agg_contents = 1;
1668 cs->indirect_info->by_ref = by_ref;
1669 return;
1672 /* Now we need to try to match the complex pattern of calling a member
1673 pointer. */
1674 if (gimple_code (def) != GIMPLE_PHI
1675 || gimple_phi_num_args (def) != 2
1676 || !POINTER_TYPE_P (TREE_TYPE (target))
1677 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1678 return;
1680 /* First, we need to check whether one of these is a load from a member
1681 pointer that is a parameter to this function. */
1682 n1 = PHI_ARG_DEF (def, 0);
1683 n2 = PHI_ARG_DEF (def, 1);
1684 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1685 return;
1686 d1 = SSA_NAME_DEF_STMT (n1);
1687 d2 = SSA_NAME_DEF_STMT (n2);
1689 join = gimple_bb (def);
1690 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1692 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1693 return;
1695 bb = EDGE_PRED (join, 0)->src;
1696 virt_bb = gimple_bb (d2);
1698 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1700 bb = EDGE_PRED (join, 1)->src;
1701 virt_bb = gimple_bb (d1);
1703 else
1704 return;
1706 /* Second, we need to check that the basic blocks are laid out in the way
1707 corresponding to the pattern. */
1709 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1710 || single_pred (virt_bb) != bb
1711 || single_succ (virt_bb) != join)
1712 return;
1714 /* Third, let's see that the branching is done depending on the least
1715 significant bit of the pfn. */
1717 branch = last_stmt (bb);
1718 if (!branch || gimple_code (branch) != GIMPLE_COND)
1719 return;
1721 if ((gimple_cond_code (branch) != NE_EXPR
1722 && gimple_cond_code (branch) != EQ_EXPR)
1723 || !integer_zerop (gimple_cond_rhs (branch)))
1724 return;
1726 cond = gimple_cond_lhs (branch);
1727 if (!ipa_is_ssa_with_stmt_def (cond))
1728 return;
1730 def = SSA_NAME_DEF_STMT (cond);
1731 if (!is_gimple_assign (def)
1732 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1733 || !integer_onep (gimple_assign_rhs2 (def)))
1734 return;
1736 cond = gimple_assign_rhs1 (def);
1737 if (!ipa_is_ssa_with_stmt_def (cond))
1738 return;
1740 def = SSA_NAME_DEF_STMT (cond);
1742 if (is_gimple_assign (def)
1743 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1745 cond = gimple_assign_rhs1 (def);
1746 if (!ipa_is_ssa_with_stmt_def (cond))
1747 return;
1748 def = SSA_NAME_DEF_STMT (cond);
1751 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1752 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1753 == ptrmemfunc_vbit_in_delta),
1754 NULL);
1755 if (rec != rec2)
1756 return;
1758 index = ipa_get_param_decl_index (info, rec);
1759 if (index >= 0
1760 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1762 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1763 cs->indirect_info->offset = offset;
1764 cs->indirect_info->agg_contents = 1;
1767 return;
1770 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1771 object referenced in the expression is a formal parameter of the caller
1772 (described by INFO), create a call note for the statement. */
1774 static void
1775 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1776 struct ipa_node_params *info, gimple call,
1777 tree target)
1779 struct cgraph_edge *cs;
1780 struct cgraph_indirect_call_info *ii;
1781 struct ipa_jump_func jfunc;
1782 tree obj = OBJ_TYPE_REF_OBJECT (target);
1783 int index;
1784 HOST_WIDE_INT anc_offset;
1786 if (!flag_devirtualize)
1787 return;
1789 if (TREE_CODE (obj) != SSA_NAME)
1790 return;
1792 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1794 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1795 return;
1797 anc_offset = 0;
1798 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1799 gcc_assert (index >= 0);
1800 if (detect_type_change_ssa (obj, call, &jfunc))
1801 return;
1803 else
1805 gimple stmt = SSA_NAME_DEF_STMT (obj);
1806 tree expr;
1808 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1809 if (!expr)
1810 return;
1811 index = ipa_get_param_decl_index (info,
1812 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1813 gcc_assert (index >= 0);
1814 if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1815 return;
1818 cs = ipa_note_param_call (node, index, call);
1819 ii = cs->indirect_info;
1820 ii->offset = anc_offset;
1821 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1822 ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1823 ii->polymorphic = 1;
1826 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1827 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1828 containing intermediate information about each formal parameter. */
1830 static void
1831 ipa_analyze_call_uses (struct cgraph_node *node,
1832 struct ipa_node_params *info,
1833 struct param_analysis_info *parms_ainfo, gimple call)
1835 tree target = gimple_call_fn (call);
1837 if (!target)
1838 return;
1839 if (TREE_CODE (target) == SSA_NAME)
1840 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
1841 else if (TREE_CODE (target) == OBJ_TYPE_REF)
1842 ipa_analyze_virtual_call_uses (node, info, call, target);
1846 /* Analyze the call statement STMT with respect to formal parameters (described
1847 in INFO) of caller given by NODE. Currently it only checks whether formal
1848 parameters are called. PARMS_AINFO is a pointer to a vector containing
1849 intermediate information about each formal parameter. */
1851 static void
1852 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1853 struct param_analysis_info *parms_ainfo, gimple stmt)
1855 if (is_gimple_call (stmt))
1856 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
1859 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1860 If OP is a parameter declaration, mark it as used in the info structure
1861 passed in DATA. */
1863 static bool
1864 visit_ref_for_mod_analysis (gimple, tree op, tree, void *data)
1866 struct ipa_node_params *info = (struct ipa_node_params *) data;
1868 op = get_base_address (op);
1869 if (op
1870 && TREE_CODE (op) == PARM_DECL)
1872 int index = ipa_get_param_decl_index (info, op);
1873 gcc_assert (index >= 0);
1874 ipa_set_param_used (info, index, true);
1877 return false;
1880 /* Scan the function body of NODE and inspect the uses of formal parameters.
1881 Store the findings in various structures of the associated ipa_node_params
1882 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
1883 vector containing intermediate information about each formal parameter. */
1885 static void
1886 ipa_analyze_params_uses (struct cgraph_node *node,
1887 struct param_analysis_info *parms_ainfo)
1889 tree decl = node->symbol.decl;
1890 basic_block bb;
1891 struct function *func;
1892 gimple_stmt_iterator gsi;
1893 struct ipa_node_params *info = IPA_NODE_REF (node);
1894 int i;
1896 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1897 return;
1899 for (i = 0; i < ipa_get_param_count (info); i++)
1901 tree parm = ipa_get_param (info, i);
1902 tree ddef;
1903 /* For SSA regs see if parameter is used. For non-SSA we compute
1904 the flag during modification analysis. */
1905 if (is_gimple_reg (parm)
1906 && (ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->symbol.decl),
1907 parm)) != NULL_TREE
1908 && !has_zero_uses (ddef))
1909 ipa_set_param_used (info, i, true);
1912 func = DECL_STRUCT_FUNCTION (decl);
1913 FOR_EACH_BB_FN (bb, func)
1915 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1917 gimple stmt = gsi_stmt (gsi);
1919 if (is_gimple_debug (stmt))
1920 continue;
1922 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
1923 walk_stmt_load_store_addr_ops (stmt, info,
1924 visit_ref_for_mod_analysis,
1925 visit_ref_for_mod_analysis,
1926 visit_ref_for_mod_analysis);
1928 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1929 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
1930 visit_ref_for_mod_analysis,
1931 visit_ref_for_mod_analysis,
1932 visit_ref_for_mod_analysis);
1935 info->uses_analysis_done = 1;
1938 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
1940 static void
1941 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
1943 int i;
1945 for (i = 0; i < param_count; i++)
1947 if (parms_ainfo[i].parm_visited_statements)
1948 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
1949 if (parms_ainfo[i].pt_visited_statements)
1950 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
1954 /* Initialize the array describing properties of of formal parameters
1955 of NODE, analyze their uses and compute jump functions associated
1956 with actual arguments of calls from within NODE. */
1958 void
1959 ipa_analyze_node (struct cgraph_node *node)
1961 struct ipa_node_params *info;
1962 struct param_analysis_info *parms_ainfo;
1963 int param_count;
1965 ipa_check_create_node_params ();
1966 ipa_check_create_edge_args ();
1967 info = IPA_NODE_REF (node);
1968 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1969 ipa_initialize_node_params (node);
1971 param_count = ipa_get_param_count (info);
1972 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
1973 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
1975 ipa_analyze_params_uses (node, parms_ainfo);
1976 ipa_compute_jump_functions (node, parms_ainfo);
1978 free_parms_ainfo (parms_ainfo, param_count);
1979 pop_cfun ();
1983 /* Update the jump function DST when the call graph edge corresponding to SRC is
1984 is being inlined, knowing that DST is of type ancestor and src of known
1985 type. */
1987 static void
1988 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
1989 struct ipa_jump_func *dst)
1991 HOST_WIDE_INT combined_offset;
1992 tree combined_type;
1994 combined_offset = ipa_get_jf_known_type_offset (src)
1995 + ipa_get_jf_ancestor_offset (dst);
1996 combined_type = ipa_get_jf_ancestor_type (dst);
1998 ipa_set_jf_known_type (dst, combined_offset,
1999 ipa_get_jf_known_type_base_type (src),
2000 combined_type);
2003 /* Update the jump functions associated with call graph edge E when the call
2004 graph edge CS is being inlined, assuming that E->caller is already (possibly
2005 indirectly) inlined into CS->callee and that E has not been inlined. */
2007 static void
2008 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2009 struct cgraph_edge *e)
2011 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2012 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2013 int count = ipa_get_cs_argument_count (args);
2014 int i;
2016 for (i = 0; i < count; i++)
2018 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2020 if (dst->type == IPA_JF_ANCESTOR)
2022 struct ipa_jump_func *src;
2023 int dst_fid = dst->value.ancestor.formal_id;
2025 /* Variable number of arguments can cause havoc if we try to access
2026 one that does not exist in the inlined edge. So make sure we
2027 don't. */
2028 if (dst_fid >= ipa_get_cs_argument_count (top))
2030 dst->type = IPA_JF_UNKNOWN;
2031 continue;
2034 src = ipa_get_ith_jump_func (top, dst_fid);
2036 if (src->agg.items
2037 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2039 struct ipa_agg_jf_item *item;
2040 int j;
2042 /* Currently we do not produce clobber aggregate jump functions,
2043 replace with merging when we do. */
2044 gcc_assert (!dst->agg.items);
2046 dst->agg.items = vec_safe_copy (src->agg.items);
2047 dst->agg.by_ref = src->agg.by_ref;
2048 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2049 item->offset -= dst->value.ancestor.offset;
2052 if (src->type == IPA_JF_KNOWN_TYPE)
2053 combine_known_type_and_ancestor_jfs (src, dst);
2054 else if (src->type == IPA_JF_PASS_THROUGH
2055 && src->value.pass_through.operation == NOP_EXPR)
2057 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2058 dst->value.ancestor.agg_preserved &=
2059 src->value.pass_through.agg_preserved;
2061 else if (src->type == IPA_JF_ANCESTOR)
2063 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2064 dst->value.ancestor.offset += src->value.ancestor.offset;
2065 dst->value.ancestor.agg_preserved &=
2066 src->value.ancestor.agg_preserved;
2068 else
2069 dst->type = IPA_JF_UNKNOWN;
2071 else if (dst->type == IPA_JF_PASS_THROUGH)
2073 struct ipa_jump_func *src;
2074 /* We must check range due to calls with variable number of arguments
2075 and we cannot combine jump functions with operations. */
2076 if (dst->value.pass_through.operation == NOP_EXPR
2077 && (dst->value.pass_through.formal_id
2078 < ipa_get_cs_argument_count (top)))
2080 bool agg_p;
2081 int dst_fid = dst->value.pass_through.formal_id;
2082 src = ipa_get_ith_jump_func (top, dst_fid);
2083 agg_p = dst->value.pass_through.agg_preserved;
2085 dst->type = src->type;
2086 dst->value = src->value;
2088 if (src->agg.items
2089 && (agg_p || !src->agg.by_ref))
2091 /* Currently we do not produce clobber aggregate jump
2092 functions, replace with merging when we do. */
2093 gcc_assert (!dst->agg.items);
2095 dst->agg.by_ref = src->agg.by_ref;
2096 dst->agg.items = vec_safe_copy (src->agg.items);
2099 if (!agg_p)
2101 if (dst->type == IPA_JF_PASS_THROUGH)
2102 dst->value.pass_through.agg_preserved = false;
2103 else if (dst->type == IPA_JF_ANCESTOR)
2104 dst->value.ancestor.agg_preserved = false;
2107 else
2108 dst->type = IPA_JF_UNKNOWN;
2113 /* If TARGET is an addr_expr of a function declaration, make it the destination
2114 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2116 struct cgraph_edge *
2117 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2119 struct cgraph_node *callee;
2120 struct inline_edge_summary *es = inline_edge_summary (ie);
2122 if (TREE_CODE (target) == ADDR_EXPR)
2123 target = TREE_OPERAND (target, 0);
2124 if (TREE_CODE (target) != FUNCTION_DECL)
2126 target = canonicalize_constructor_val (target, NULL);
2127 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2129 if (dump_file)
2130 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2131 " in (%s/%i).\n",
2132 cgraph_node_name (ie->caller), ie->caller->uid);
2133 return NULL;
2136 callee = cgraph_get_node (target);
2138 /* Because may-edges are not explicitely represented and vtable may be external,
2139 we may create the first reference to the object in the unit. */
2140 if (!callee || callee->global.inlined_to)
2143 /* We are better to ensure we can refer to it.
2144 In the case of static functions we are out of luck, since we already
2145 removed its body. In the case of public functions we may or may
2146 not introduce the reference. */
2147 if (!canonicalize_constructor_val (target, NULL)
2148 || !TREE_PUBLIC (target))
2150 if (dump_file)
2151 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2152 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2153 xstrdup (cgraph_node_name (ie->caller)), ie->caller->uid,
2154 xstrdup (cgraph_node_name (ie->callee)), ie->callee->uid);
2155 return NULL;
2157 callee = cgraph_get_create_real_symbol_node (target);
2159 ipa_check_create_node_params ();
2161 /* We can not make edges to inline clones. It is bug that someone removed
2162 the cgraph node too early. */
2163 gcc_assert (!callee->global.inlined_to);
2165 cgraph_make_edge_direct (ie, callee);
2166 es = inline_edge_summary (ie);
2167 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2168 - eni_size_weights.call_cost);
2169 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2170 - eni_time_weights.call_cost);
2171 if (dump_file)
2173 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2174 "(%s/%i -> %s/%i), for stmt ",
2175 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2176 xstrdup (cgraph_node_name (ie->caller)), ie->caller->uid,
2177 xstrdup (cgraph_node_name (ie->callee)), ie->callee->uid);
2178 if (ie->call_stmt)
2179 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2180 else
2181 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2183 callee = cgraph_function_or_thunk_node (callee, NULL);
2185 return ie;
2188 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2189 return NULL if there is not any. BY_REF specifies whether the value has to
2190 be passed by reference or by value. */
2192 tree
2193 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2194 HOST_WIDE_INT offset, bool by_ref)
2196 struct ipa_agg_jf_item *item;
2197 int i;
2199 if (by_ref != agg->by_ref)
2200 return NULL;
2202 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2203 if (item->offset == offset)
2205 /* Currently we do not have clobber values, return NULL for them once
2206 we do. */
2207 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2208 return item->value;
2210 return NULL;
2213 /* Try to find a destination for indirect edge IE that corresponds to a simple
2214 call or a call of a member function pointer and where the destination is a
2215 pointer formal parameter described by jump function JFUNC. If it can be
2216 determined, return the newly direct edge, otherwise return NULL.
2217 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2219 static struct cgraph_edge *
2220 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2221 struct ipa_jump_func *jfunc,
2222 struct ipa_node_params *new_root_info)
2224 tree target;
2226 if (ie->indirect_info->agg_contents)
2227 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2228 ie->indirect_info->offset,
2229 ie->indirect_info->by_ref);
2230 else
2231 target = ipa_value_from_jfunc (new_root_info, jfunc);
2232 if (!target)
2233 return NULL;
2234 return ipa_make_edge_direct_to_target (ie, target);
2237 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2238 call based on a formal parameter which is described by jump function JFUNC
2239 and if it can be determined, make it direct and return the direct edge.
2240 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2241 are relative to. */
2243 static struct cgraph_edge *
2244 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2245 struct ipa_jump_func *jfunc,
2246 struct ipa_node_params *new_root_info)
2248 tree binfo, target;
2250 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2252 if (!binfo)
2253 return NULL;
2255 if (TREE_CODE (binfo) != TREE_BINFO)
2257 binfo = gimple_extract_devirt_binfo_from_cst (binfo);
2258 if (!binfo)
2259 return NULL;
2262 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2263 ie->indirect_info->otr_type);
2264 if (binfo)
2265 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2266 binfo);
2267 else
2268 return NULL;
2270 if (target)
2271 return ipa_make_edge_direct_to_target (ie, target);
2272 else
2273 return NULL;
2276 /* Update the param called notes associated with NODE when CS is being inlined,
2277 assuming NODE is (potentially indirectly) inlined into CS->callee.
2278 Moreover, if the callee is discovered to be constant, create a new cgraph
2279 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2280 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2282 static bool
2283 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2284 struct cgraph_node *node,
2285 vec<cgraph_edge_p> *new_edges)
2287 struct ipa_edge_args *top;
2288 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2289 struct ipa_node_params *new_root_info;
2290 bool res = false;
2292 ipa_check_create_edge_args ();
2293 top = IPA_EDGE_REF (cs);
2294 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2295 ? cs->caller->global.inlined_to
2296 : cs->caller);
2298 for (ie = node->indirect_calls; ie; ie = next_ie)
2300 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2301 struct ipa_jump_func *jfunc;
2302 int param_index;
2304 next_ie = ie->next_callee;
2306 if (ici->param_index == -1)
2307 continue;
2309 /* We must check range due to calls with variable number of arguments: */
2310 if (ici->param_index >= ipa_get_cs_argument_count (top))
2312 ici->param_index = -1;
2313 continue;
2316 param_index = ici->param_index;
2317 jfunc = ipa_get_ith_jump_func (top, param_index);
2319 if (!flag_indirect_inlining)
2320 new_direct_edge = NULL;
2321 else if (ici->polymorphic)
2322 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2323 new_root_info);
2324 else
2325 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2326 new_root_info);
2327 if (new_direct_edge)
2329 new_direct_edge->indirect_inlining_edge = 1;
2330 if (new_direct_edge->call_stmt)
2331 new_direct_edge->call_stmt_cannot_inline_p
2332 = !gimple_check_call_matching_types (new_direct_edge->call_stmt,
2333 new_direct_edge->callee->symbol.decl);
2334 if (new_edges)
2336 new_edges->safe_push (new_direct_edge);
2337 top = IPA_EDGE_REF (cs);
2338 res = true;
2341 else if (jfunc->type == IPA_JF_PASS_THROUGH
2342 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2344 if (ici->agg_contents
2345 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2346 ici->param_index = -1;
2347 else
2348 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2350 else if (jfunc->type == IPA_JF_ANCESTOR)
2352 if (ici->agg_contents
2353 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2354 ici->param_index = -1;
2355 else
2357 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2358 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2361 else
2362 /* Either we can find a destination for this edge now or never. */
2363 ici->param_index = -1;
2366 return res;
2369 /* Recursively traverse subtree of NODE (including node) made of inlined
2370 cgraph_edges when CS has been inlined and invoke
2371 update_indirect_edges_after_inlining on all nodes and
2372 update_jump_functions_after_inlining on all non-inlined edges that lead out
2373 of this subtree. Newly discovered indirect edges will be added to
2374 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2375 created. */
2377 static bool
2378 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2379 struct cgraph_node *node,
2380 vec<cgraph_edge_p> *new_edges)
2382 struct cgraph_edge *e;
2383 bool res;
2385 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2387 for (e = node->callees; e; e = e->next_callee)
2388 if (!e->inline_failed)
2389 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2390 else
2391 update_jump_functions_after_inlining (cs, e);
2392 for (e = node->indirect_calls; e; e = e->next_callee)
2393 update_jump_functions_after_inlining (cs, e);
2395 return res;
2398 /* Update jump functions and call note functions on inlining the call site CS.
2399 CS is expected to lead to a node already cloned by
2400 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2401 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2402 created. */
2404 bool
2405 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2406 vec<cgraph_edge_p> *new_edges)
2408 bool changed;
2409 /* Do nothing if the preparation phase has not been carried out yet
2410 (i.e. during early inlining). */
2411 if (!ipa_node_params_vector.exists ())
2412 return false;
2413 gcc_assert (ipa_edge_args_vector);
2415 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2417 /* We do not keep jump functions of inlined edges up to date. Better to free
2418 them so we do not access them accidentally. */
2419 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
2420 return changed;
2423 /* Frees all dynamically allocated structures that the argument info points
2424 to. */
2426 void
2427 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2429 vec_free (args->jump_functions);
2430 memset (args, 0, sizeof (*args));
2433 /* Free all ipa_edge structures. */
2435 void
2436 ipa_free_all_edge_args (void)
2438 int i;
2439 struct ipa_edge_args *args;
2441 if (!ipa_edge_args_vector)
2442 return;
2444 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2445 ipa_free_edge_args_substructures (args);
2447 vec_free (ipa_edge_args_vector);
2450 /* Frees all dynamically allocated structures that the param info points
2451 to. */
2453 void
2454 ipa_free_node_params_substructures (struct ipa_node_params *info)
2456 info->descriptors.release ();
2457 free (info->lattices);
2458 /* Lattice values and their sources are deallocated with their alocation
2459 pool. */
2460 info->known_vals.release ();
2461 memset (info, 0, sizeof (*info));
2464 /* Free all ipa_node_params structures. */
2466 void
2467 ipa_free_all_node_params (void)
2469 int i;
2470 struct ipa_node_params *info;
2472 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
2473 ipa_free_node_params_substructures (info);
2475 ipa_node_params_vector.release ();
2478 /* Set the aggregate replacements of NODE to be AGGVALS. */
2480 void
2481 ipa_set_node_agg_value_chain (struct cgraph_node *node,
2482 struct ipa_agg_replacement_value *aggvals)
2484 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
2485 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
2487 (*ipa_node_agg_replacements)[node->uid] = aggvals;
2490 /* Hook that is called by cgraph.c when an edge is removed. */
2492 static void
2493 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
2495 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2496 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
2497 return;
2498 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
2501 /* Hook that is called by cgraph.c when a node is removed. */
2503 static void
2504 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2506 /* During IPA-CP updating we can be called on not-yet analyze clones. */
2507 if (ipa_node_params_vector.length () > (unsigned)node->uid)
2508 ipa_free_node_params_substructures (IPA_NODE_REF (node));
2509 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
2510 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
2513 /* Hook that is called by cgraph.c when an edge is duplicated. */
2515 static void
2516 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
2517 __attribute__((unused)) void *data)
2519 struct ipa_edge_args *old_args, *new_args;
2520 unsigned int i;
2522 ipa_check_create_edge_args ();
2524 old_args = IPA_EDGE_REF (src);
2525 new_args = IPA_EDGE_REF (dst);
2527 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
2529 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
2530 (*new_args->jump_functions)[i].agg.items
2531 = vec_safe_copy ((*old_args->jump_functions)[i].agg.items);
2534 /* Hook that is called by cgraph.c when a node is duplicated. */
2536 static void
2537 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
2538 ATTRIBUTE_UNUSED void *data)
2540 struct ipa_node_params *old_info, *new_info;
2541 struct ipa_agg_replacement_value *old_av, *new_av;
2543 ipa_check_create_node_params ();
2544 old_info = IPA_NODE_REF (src);
2545 new_info = IPA_NODE_REF (dst);
2547 new_info->descriptors = old_info->descriptors.copy ();
2548 new_info->lattices = NULL;
2549 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
2551 new_info->uses_analysis_done = old_info->uses_analysis_done;
2552 new_info->node_enqueued = old_info->node_enqueued;
2554 old_av = ipa_get_agg_replacements_for_node (src);
2555 if (!old_av)
2556 return;
2558 new_av = NULL;
2559 while (old_av)
2561 struct ipa_agg_replacement_value *v;
2563 v = ggc_alloc_ipa_agg_replacement_value ();
2564 memcpy (v, old_av, sizeof (*v));
2565 v->next = new_av;
2566 new_av = v;
2567 old_av = old_av->next;
2569 ipa_set_node_agg_value_chain (dst, new_av);
2573 /* Analyze newly added function into callgraph. */
2575 static void
2576 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2578 ipa_analyze_node (node);
2581 /* Register our cgraph hooks if they are not already there. */
2583 void
2584 ipa_register_cgraph_hooks (void)
2586 if (!edge_removal_hook_holder)
2587 edge_removal_hook_holder =
2588 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
2589 if (!node_removal_hook_holder)
2590 node_removal_hook_holder =
2591 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
2592 if (!edge_duplication_hook_holder)
2593 edge_duplication_hook_holder =
2594 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
2595 if (!node_duplication_hook_holder)
2596 node_duplication_hook_holder =
2597 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
2598 function_insertion_hook_holder =
2599 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
2602 /* Unregister our cgraph hooks if they are not already there. */
2604 static void
2605 ipa_unregister_cgraph_hooks (void)
2607 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2608 edge_removal_hook_holder = NULL;
2609 cgraph_remove_node_removal_hook (node_removal_hook_holder);
2610 node_removal_hook_holder = NULL;
2611 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2612 edge_duplication_hook_holder = NULL;
2613 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2614 node_duplication_hook_holder = NULL;
2615 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2616 function_insertion_hook_holder = NULL;
2619 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2620 longer needed after ipa-cp. */
2622 void
2623 ipa_free_all_structures_after_ipa_cp (void)
2625 if (!optimize)
2627 ipa_free_all_edge_args ();
2628 ipa_free_all_node_params ();
2629 free_alloc_pool (ipcp_sources_pool);
2630 free_alloc_pool (ipcp_values_pool);
2631 free_alloc_pool (ipcp_agg_lattice_pool);
2632 ipa_unregister_cgraph_hooks ();
2636 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2637 longer needed after indirect inlining. */
2639 void
2640 ipa_free_all_structures_after_iinln (void)
2642 ipa_free_all_edge_args ();
2643 ipa_free_all_node_params ();
2644 ipa_unregister_cgraph_hooks ();
2645 if (ipcp_sources_pool)
2646 free_alloc_pool (ipcp_sources_pool);
2647 if (ipcp_values_pool)
2648 free_alloc_pool (ipcp_values_pool);
2649 if (ipcp_agg_lattice_pool)
2650 free_alloc_pool (ipcp_agg_lattice_pool);
2653 /* Print ipa_tree_map data structures of all functions in the
2654 callgraph to F. */
2656 void
2657 ipa_print_node_params (FILE *f, struct cgraph_node *node)
2659 int i, count;
2660 tree temp;
2661 struct ipa_node_params *info;
2663 if (!node->analyzed)
2664 return;
2665 info = IPA_NODE_REF (node);
2666 fprintf (f, " function %s parameter descriptors:\n",
2667 cgraph_node_name (node));
2668 count = ipa_get_param_count (info);
2669 for (i = 0; i < count; i++)
2671 temp = ipa_get_param (info, i);
2672 if (TREE_CODE (temp) == PARM_DECL)
2673 fprintf (f, " param %d : %s", i,
2674 (DECL_NAME (temp)
2675 ? (*lang_hooks.decl_printable_name) (temp, 2)
2676 : "(unnamed)"));
2677 if (ipa_is_param_used (info, i))
2678 fprintf (f, " used");
2679 fprintf (f, "\n");
2683 /* Print ipa_tree_map data structures of all functions in the
2684 callgraph to F. */
2686 void
2687 ipa_print_all_params (FILE * f)
2689 struct cgraph_node *node;
2691 fprintf (f, "\nFunction parameters:\n");
2692 FOR_EACH_FUNCTION (node)
2693 ipa_print_node_params (f, node);
2696 /* Return a heap allocated vector containing formal parameters of FNDECL. */
2698 vec<tree>
2699 ipa_get_vector_of_formal_parms (tree fndecl)
2701 vec<tree> args;
2702 int count;
2703 tree parm;
2705 count = count_formal_params (fndecl);
2706 args.create (count);
2707 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2708 args.quick_push (parm);
2710 return args;
2713 /* Return a heap allocated vector containing types of formal parameters of
2714 function type FNTYPE. */
2716 static inline vec<tree>
2717 get_vector_of_formal_parm_types (tree fntype)
2719 vec<tree> types;
2720 int count = 0;
2721 tree t;
2723 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2724 count++;
2726 types.create (count);
2727 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2728 types.quick_push (TREE_VALUE (t));
2730 return types;
2733 /* Modify the function declaration FNDECL and its type according to the plan in
2734 ADJUSTMENTS. It also sets base fields of individual adjustments structures
2735 to reflect the actual parameters being modified which are determined by the
2736 base_index field. */
2738 void
2739 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
2740 const char *synth_parm_prefix)
2742 vec<tree> oparms, otypes;
2743 tree orig_type, new_type = NULL;
2744 tree old_arg_types, t, new_arg_types = NULL;
2745 tree parm, *link = &DECL_ARGUMENTS (fndecl);
2746 int i, len = adjustments.length ();
2747 tree new_reversed = NULL;
2748 bool care_for_types, last_parm_void;
2750 if (!synth_parm_prefix)
2751 synth_parm_prefix = "SYNTH";
2753 oparms = ipa_get_vector_of_formal_parms (fndecl);
2754 orig_type = TREE_TYPE (fndecl);
2755 old_arg_types = TYPE_ARG_TYPES (orig_type);
2757 /* The following test is an ugly hack, some functions simply don't have any
2758 arguments in their type. This is probably a bug but well... */
2759 care_for_types = (old_arg_types != NULL_TREE);
2760 if (care_for_types)
2762 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
2763 == void_type_node);
2764 otypes = get_vector_of_formal_parm_types (orig_type);
2765 if (last_parm_void)
2766 gcc_assert (oparms.length () + 1 == otypes.length ());
2767 else
2768 gcc_assert (oparms.length () == otypes.length ());
2770 else
2772 last_parm_void = false;
2773 otypes.create (0);
2776 for (i = 0; i < len; i++)
2778 struct ipa_parm_adjustment *adj;
2779 gcc_assert (link);
2781 adj = &adjustments[i];
2782 parm = oparms[adj->base_index];
2783 adj->base = parm;
2785 if (adj->copy_param)
2787 if (care_for_types)
2788 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
2789 new_arg_types);
2790 *link = parm;
2791 link = &DECL_CHAIN (parm);
2793 else if (!adj->remove_param)
2795 tree new_parm;
2796 tree ptype;
2798 if (adj->by_ref)
2799 ptype = build_pointer_type (adj->type);
2800 else
2801 ptype = adj->type;
2803 if (care_for_types)
2804 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
2806 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
2807 ptype);
2808 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
2810 DECL_ARTIFICIAL (new_parm) = 1;
2811 DECL_ARG_TYPE (new_parm) = ptype;
2812 DECL_CONTEXT (new_parm) = fndecl;
2813 TREE_USED (new_parm) = 1;
2814 DECL_IGNORED_P (new_parm) = 1;
2815 layout_decl (new_parm, 0);
2817 adj->base = parm;
2818 adj->reduction = new_parm;
2820 *link = new_parm;
2822 link = &DECL_CHAIN (new_parm);
2826 *link = NULL_TREE;
2828 if (care_for_types)
2830 new_reversed = nreverse (new_arg_types);
2831 if (last_parm_void)
2833 if (new_reversed)
2834 TREE_CHAIN (new_arg_types) = void_list_node;
2835 else
2836 new_reversed = void_list_node;
2840 /* Use copy_node to preserve as much as possible from original type
2841 (debug info, attribute lists etc.)
2842 Exception is METHOD_TYPEs must have THIS argument.
2843 When we are asked to remove it, we need to build new FUNCTION_TYPE
2844 instead. */
2845 if (TREE_CODE (orig_type) != METHOD_TYPE
2846 || (adjustments[0].copy_param
2847 && adjustments[0].base_index == 0))
2849 new_type = build_distinct_type_copy (orig_type);
2850 TYPE_ARG_TYPES (new_type) = new_reversed;
2852 else
2854 new_type
2855 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
2856 new_reversed));
2857 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
2858 DECL_VINDEX (fndecl) = NULL_TREE;
2861 /* When signature changes, we need to clear builtin info. */
2862 if (DECL_BUILT_IN (fndecl))
2864 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
2865 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
2868 /* This is a new type, not a copy of an old type. Need to reassociate
2869 variants. We can handle everything except the main variant lazily. */
2870 t = TYPE_MAIN_VARIANT (orig_type);
2871 if (orig_type != t)
2873 TYPE_MAIN_VARIANT (new_type) = t;
2874 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
2875 TYPE_NEXT_VARIANT (t) = new_type;
2877 else
2879 TYPE_MAIN_VARIANT (new_type) = new_type;
2880 TYPE_NEXT_VARIANT (new_type) = NULL;
2883 TREE_TYPE (fndecl) = new_type;
2884 DECL_VIRTUAL_P (fndecl) = 0;
2885 otypes.release ();
2886 oparms.release ();
2889 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
2890 If this is a directly recursive call, CS must be NULL. Otherwise it must
2891 contain the corresponding call graph edge. */
2893 void
2894 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
2895 ipa_parm_adjustment_vec adjustments)
2897 vec<tree> vargs;
2898 vec<tree, va_gc> **debug_args = NULL;
2899 gimple new_stmt;
2900 gimple_stmt_iterator gsi;
2901 tree callee_decl;
2902 int i, len;
2904 len = adjustments.length ();
2905 vargs.create (len);
2906 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->symbol.decl;
2908 gsi = gsi_for_stmt (stmt);
2909 for (i = 0; i < len; i++)
2911 struct ipa_parm_adjustment *adj;
2913 adj = &adjustments[i];
2915 if (adj->copy_param)
2917 tree arg = gimple_call_arg (stmt, adj->base_index);
2919 vargs.quick_push (arg);
2921 else if (!adj->remove_param)
2923 tree expr, base, off;
2924 location_t loc;
2925 unsigned int deref_align;
2926 bool deref_base = false;
2928 /* We create a new parameter out of the value of the old one, we can
2929 do the following kind of transformations:
2931 - A scalar passed by reference is converted to a scalar passed by
2932 value. (adj->by_ref is false and the type of the original
2933 actual argument is a pointer to a scalar).
2935 - A part of an aggregate is passed instead of the whole aggregate.
2936 The part can be passed either by value or by reference, this is
2937 determined by value of adj->by_ref. Moreover, the code below
2938 handles both situations when the original aggregate is passed by
2939 value (its type is not a pointer) and when it is passed by
2940 reference (it is a pointer to an aggregate).
2942 When the new argument is passed by reference (adj->by_ref is true)
2943 it must be a part of an aggregate and therefore we form it by
2944 simply taking the address of a reference inside the original
2945 aggregate. */
2947 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
2948 base = gimple_call_arg (stmt, adj->base_index);
2949 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
2950 : EXPR_LOCATION (base);
2952 if (TREE_CODE (base) != ADDR_EXPR
2953 && POINTER_TYPE_P (TREE_TYPE (base)))
2954 off = build_int_cst (adj->alias_ptr_type,
2955 adj->offset / BITS_PER_UNIT);
2956 else
2958 HOST_WIDE_INT base_offset;
2959 tree prev_base;
2960 bool addrof;
2962 if (TREE_CODE (base) == ADDR_EXPR)
2964 base = TREE_OPERAND (base, 0);
2965 addrof = true;
2967 else
2968 addrof = false;
2969 prev_base = base;
2970 base = get_addr_base_and_unit_offset (base, &base_offset);
2971 /* Aggregate arguments can have non-invariant addresses. */
2972 if (!base)
2974 base = build_fold_addr_expr (prev_base);
2975 off = build_int_cst (adj->alias_ptr_type,
2976 adj->offset / BITS_PER_UNIT);
2978 else if (TREE_CODE (base) == MEM_REF)
2980 if (!addrof)
2982 deref_base = true;
2983 deref_align = TYPE_ALIGN (TREE_TYPE (base));
2985 off = build_int_cst (adj->alias_ptr_type,
2986 base_offset
2987 + adj->offset / BITS_PER_UNIT);
2988 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
2989 off);
2990 base = TREE_OPERAND (base, 0);
2992 else
2994 off = build_int_cst (adj->alias_ptr_type,
2995 base_offset
2996 + adj->offset / BITS_PER_UNIT);
2997 base = build_fold_addr_expr (base);
3001 if (!adj->by_ref)
3003 tree type = adj->type;
3004 unsigned int align;
3005 unsigned HOST_WIDE_INT misalign;
3007 if (deref_base)
3009 align = deref_align;
3010 misalign = 0;
3012 else
3014 get_pointer_alignment_1 (base, &align, &misalign);
3015 if (TYPE_ALIGN (type) > align)
3016 align = TYPE_ALIGN (type);
3018 misalign += (tree_to_double_int (off)
3019 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3020 * BITS_PER_UNIT);
3021 misalign = misalign & (align - 1);
3022 if (misalign != 0)
3023 align = (misalign & -misalign);
3024 if (align < TYPE_ALIGN (type))
3025 type = build_aligned_type (type, align);
3026 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3028 else
3030 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3031 expr = build_fold_addr_expr (expr);
3034 expr = force_gimple_operand_gsi (&gsi, expr,
3035 adj->by_ref
3036 || is_gimple_reg_type (adj->type),
3037 NULL, true, GSI_SAME_STMT);
3038 vargs.quick_push (expr);
3040 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3042 unsigned int ix;
3043 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3044 gimple def_temp;
3046 arg = gimple_call_arg (stmt, adj->base_index);
3047 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3049 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3050 continue;
3051 arg = fold_convert_loc (gimple_location (stmt),
3052 TREE_TYPE (origin), arg);
3054 if (debug_args == NULL)
3055 debug_args = decl_debug_args_insert (callee_decl);
3056 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3057 if (ddecl == origin)
3059 ddecl = (**debug_args)[ix + 1];
3060 break;
3062 if (ddecl == NULL)
3064 ddecl = make_node (DEBUG_EXPR_DECL);
3065 DECL_ARTIFICIAL (ddecl) = 1;
3066 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3067 DECL_MODE (ddecl) = DECL_MODE (origin);
3069 vec_safe_push (*debug_args, origin);
3070 vec_safe_push (*debug_args, ddecl);
3072 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3073 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3077 if (dump_file && (dump_flags & TDF_DETAILS))
3079 fprintf (dump_file, "replacing stmt:");
3080 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3083 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3084 vargs.release ();
3085 if (gimple_call_lhs (stmt))
3086 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3088 gimple_set_block (new_stmt, gimple_block (stmt));
3089 if (gimple_has_location (stmt))
3090 gimple_set_location (new_stmt, gimple_location (stmt));
3091 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3092 gimple_call_copy_flags (new_stmt, stmt);
3094 if (dump_file && (dump_flags & TDF_DETAILS))
3096 fprintf (dump_file, "with stmt:");
3097 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3098 fprintf (dump_file, "\n");
3100 gsi_replace (&gsi, new_stmt, true);
3101 if (cs)
3102 cgraph_set_call_stmt (cs, new_stmt);
3103 update_ssa (TODO_update_ssa);
3104 free_dominance_info (CDI_DOMINATORS);
3107 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3109 static bool
3110 index_in_adjustments_multiple_times_p (int base_index,
3111 ipa_parm_adjustment_vec adjustments)
3113 int i, len = adjustments.length ();
3114 bool one = false;
3116 for (i = 0; i < len; i++)
3118 struct ipa_parm_adjustment *adj;
3119 adj = &adjustments[i];
3121 if (adj->base_index == base_index)
3123 if (one)
3124 return true;
3125 else
3126 one = true;
3129 return false;
3133 /* Return adjustments that should have the same effect on function parameters
3134 and call arguments as if they were first changed according to adjustments in
3135 INNER and then by adjustments in OUTER. */
3137 ipa_parm_adjustment_vec
3138 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3139 ipa_parm_adjustment_vec outer)
3141 int i, outlen = outer.length ();
3142 int inlen = inner.length ();
3143 int removals = 0;
3144 ipa_parm_adjustment_vec adjustments, tmp;
3146 tmp.create (inlen);
3147 for (i = 0; i < inlen; i++)
3149 struct ipa_parm_adjustment *n;
3150 n = &inner[i];
3152 if (n->remove_param)
3153 removals++;
3154 else
3155 tmp.quick_push (*n);
3158 adjustments.create (outlen + removals);
3159 for (i = 0; i < outlen; i++)
3161 struct ipa_parm_adjustment r;
3162 struct ipa_parm_adjustment *out = &outer[i];
3163 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3165 memset (&r, 0, sizeof (r));
3166 gcc_assert (!in->remove_param);
3167 if (out->remove_param)
3169 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3171 r.remove_param = true;
3172 adjustments.quick_push (r);
3174 continue;
3177 r.base_index = in->base_index;
3178 r.type = out->type;
3180 /* FIXME: Create nonlocal value too. */
3182 if (in->copy_param && out->copy_param)
3183 r.copy_param = true;
3184 else if (in->copy_param)
3185 r.offset = out->offset;
3186 else if (out->copy_param)
3187 r.offset = in->offset;
3188 else
3189 r.offset = in->offset + out->offset;
3190 adjustments.quick_push (r);
3193 for (i = 0; i < inlen; i++)
3195 struct ipa_parm_adjustment *n = &inner[i];
3197 if (n->remove_param)
3198 adjustments.quick_push (*n);
3201 tmp.release ();
3202 return adjustments;
3205 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3206 friendly way, assuming they are meant to be applied to FNDECL. */
3208 void
3209 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3210 tree fndecl)
3212 int i, len = adjustments.length ();
3213 bool first = true;
3214 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3216 fprintf (file, "IPA param adjustments: ");
3217 for (i = 0; i < len; i++)
3219 struct ipa_parm_adjustment *adj;
3220 adj = &adjustments[i];
3222 if (!first)
3223 fprintf (file, " ");
3224 else
3225 first = false;
3227 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3228 print_generic_expr (file, parms[adj->base_index], 0);
3229 if (adj->base)
3231 fprintf (file, ", base: ");
3232 print_generic_expr (file, adj->base, 0);
3234 if (adj->reduction)
3236 fprintf (file, ", reduction: ");
3237 print_generic_expr (file, adj->reduction, 0);
3239 if (adj->new_ssa_base)
3241 fprintf (file, ", new_ssa_base: ");
3242 print_generic_expr (file, adj->new_ssa_base, 0);
3245 if (adj->copy_param)
3246 fprintf (file, ", copy_param");
3247 else if (adj->remove_param)
3248 fprintf (file, ", remove_param");
3249 else
3250 fprintf (file, ", offset %li", (long) adj->offset);
3251 if (adj->by_ref)
3252 fprintf (file, ", by_ref");
3253 print_node_brief (file, ", type: ", adj->type, 0);
3254 fprintf (file, "\n");
3256 parms.release ();
3259 /* Dump the AV linked list. */
3261 void
3262 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3264 bool comma = false;
3265 fprintf (f, " Aggregate replacements:");
3266 for (; av; av = av->next)
3268 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3269 av->index, av->offset);
3270 print_generic_expr (f, av->value, 0);
3271 comma = true;
3273 fprintf (f, "\n");
3276 /* Stream out jump function JUMP_FUNC to OB. */
3278 static void
3279 ipa_write_jump_function (struct output_block *ob,
3280 struct ipa_jump_func *jump_func)
3282 struct ipa_agg_jf_item *item;
3283 struct bitpack_d bp;
3284 int i, count;
3286 streamer_write_uhwi (ob, jump_func->type);
3287 switch (jump_func->type)
3289 case IPA_JF_UNKNOWN:
3290 break;
3291 case IPA_JF_KNOWN_TYPE:
3292 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3293 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3294 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3295 break;
3296 case IPA_JF_CONST:
3297 gcc_assert (
3298 EXPR_LOCATION (jump_func->value.constant) == UNKNOWN_LOCATION);
3299 stream_write_tree (ob, jump_func->value.constant, true);
3300 break;
3301 case IPA_JF_PASS_THROUGH:
3302 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3303 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3304 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3305 bp = bitpack_create (ob->main_stream);
3306 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3307 streamer_write_bitpack (&bp);
3308 break;
3309 case IPA_JF_ANCESTOR:
3310 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3311 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3312 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3313 bp = bitpack_create (ob->main_stream);
3314 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3315 streamer_write_bitpack (&bp);
3316 break;
3319 count = vec_safe_length (jump_func->agg.items);
3320 streamer_write_uhwi (ob, count);
3321 if (count)
3323 bp = bitpack_create (ob->main_stream);
3324 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3325 streamer_write_bitpack (&bp);
3328 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3330 streamer_write_uhwi (ob, item->offset);
3331 stream_write_tree (ob, item->value, true);
3335 /* Read in jump function JUMP_FUNC from IB. */
3337 static void
3338 ipa_read_jump_function (struct lto_input_block *ib,
3339 struct ipa_jump_func *jump_func,
3340 struct data_in *data_in)
3342 struct bitpack_d bp;
3343 int i, count;
3345 jump_func->type = (enum jump_func_type) streamer_read_uhwi (ib);
3346 switch (jump_func->type)
3348 case IPA_JF_UNKNOWN:
3349 break;
3350 case IPA_JF_KNOWN_TYPE:
3351 jump_func->value.known_type.offset = streamer_read_uhwi (ib);
3352 jump_func->value.known_type.base_type = stream_read_tree (ib, data_in);
3353 jump_func->value.known_type.component_type = stream_read_tree (ib,
3354 data_in);
3355 break;
3356 case IPA_JF_CONST:
3357 jump_func->value.constant = stream_read_tree (ib, data_in);
3358 break;
3359 case IPA_JF_PASS_THROUGH:
3360 jump_func->value.pass_through.operand = stream_read_tree (ib, data_in);
3361 jump_func->value.pass_through.formal_id = streamer_read_uhwi (ib);
3362 jump_func->value.pass_through.operation
3363 = (enum tree_code) streamer_read_uhwi (ib);
3364 bp = streamer_read_bitpack (ib);
3365 jump_func->value.pass_through.agg_preserved = bp_unpack_value (&bp, 1);
3366 break;
3367 case IPA_JF_ANCESTOR:
3368 jump_func->value.ancestor.offset = streamer_read_uhwi (ib);
3369 jump_func->value.ancestor.type = stream_read_tree (ib, data_in);
3370 jump_func->value.ancestor.formal_id = streamer_read_uhwi (ib);
3371 bp = streamer_read_bitpack (ib);
3372 jump_func->value.ancestor.agg_preserved = bp_unpack_value (&bp, 1);
3373 break;
3376 count = streamer_read_uhwi (ib);
3377 vec_alloc (jump_func->agg.items, count);
3378 if (count)
3380 bp = streamer_read_bitpack (ib);
3381 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
3383 for (i = 0; i < count; i++)
3385 struct ipa_agg_jf_item item;
3386 item.offset = streamer_read_uhwi (ib);
3387 item.value = stream_read_tree (ib, data_in);
3388 jump_func->agg.items->quick_push (item);
3392 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
3393 relevant to indirect inlining to OB. */
3395 static void
3396 ipa_write_indirect_edge_info (struct output_block *ob,
3397 struct cgraph_edge *cs)
3399 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3400 struct bitpack_d bp;
3402 streamer_write_hwi (ob, ii->param_index);
3403 streamer_write_hwi (ob, ii->offset);
3404 bp = bitpack_create (ob->main_stream);
3405 bp_pack_value (&bp, ii->polymorphic, 1);
3406 bp_pack_value (&bp, ii->agg_contents, 1);
3407 bp_pack_value (&bp, ii->by_ref, 1);
3408 streamer_write_bitpack (&bp);
3410 if (ii->polymorphic)
3412 streamer_write_hwi (ob, ii->otr_token);
3413 stream_write_tree (ob, ii->otr_type, true);
3417 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
3418 relevant to indirect inlining from IB. */
3420 static void
3421 ipa_read_indirect_edge_info (struct lto_input_block *ib,
3422 struct data_in *data_in ATTRIBUTE_UNUSED,
3423 struct cgraph_edge *cs)
3425 struct cgraph_indirect_call_info *ii = cs->indirect_info;
3426 struct bitpack_d bp;
3428 ii->param_index = (int) streamer_read_hwi (ib);
3429 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
3430 bp = streamer_read_bitpack (ib);
3431 ii->polymorphic = bp_unpack_value (&bp, 1);
3432 ii->agg_contents = bp_unpack_value (&bp, 1);
3433 ii->by_ref = bp_unpack_value (&bp, 1);
3434 if (ii->polymorphic)
3436 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
3437 ii->otr_type = stream_read_tree (ib, data_in);
3441 /* Stream out NODE info to OB. */
3443 static void
3444 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
3446 int node_ref;
3447 lto_symtab_encoder_t encoder;
3448 struct ipa_node_params *info = IPA_NODE_REF (node);
3449 int j;
3450 struct cgraph_edge *e;
3451 struct bitpack_d bp;
3453 encoder = ob->decl_state->symtab_node_encoder;
3454 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
3455 streamer_write_uhwi (ob, node_ref);
3457 bp = bitpack_create (ob->main_stream);
3458 gcc_assert (info->uses_analysis_done
3459 || ipa_get_param_count (info) == 0);
3460 gcc_assert (!info->node_enqueued);
3461 gcc_assert (!info->ipcp_orig_node);
3462 for (j = 0; j < ipa_get_param_count (info); j++)
3463 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
3464 streamer_write_bitpack (&bp);
3465 for (e = node->callees; e; e = e->next_callee)
3467 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3469 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3470 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3471 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3473 for (e = node->indirect_calls; e; e = e->next_callee)
3475 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3477 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
3478 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
3479 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
3480 ipa_write_indirect_edge_info (ob, e);
3484 /* Stream in NODE info from IB. */
3486 static void
3487 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
3488 struct data_in *data_in)
3490 struct ipa_node_params *info = IPA_NODE_REF (node);
3491 int k;
3492 struct cgraph_edge *e;
3493 struct bitpack_d bp;
3495 ipa_initialize_node_params (node);
3497 bp = streamer_read_bitpack (ib);
3498 if (ipa_get_param_count (info) != 0)
3499 info->uses_analysis_done = true;
3500 info->node_enqueued = false;
3501 for (k = 0; k < ipa_get_param_count (info); k++)
3502 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
3503 for (e = node->callees; e; e = e->next_callee)
3505 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3506 int count = streamer_read_uhwi (ib);
3508 if (!count)
3509 continue;
3510 vec_safe_grow_cleared (args->jump_functions, count);
3512 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3513 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
3515 for (e = node->indirect_calls; e; e = e->next_callee)
3517 struct ipa_edge_args *args = IPA_EDGE_REF (e);
3518 int count = streamer_read_uhwi (ib);
3520 if (count)
3522 vec_safe_grow_cleared (args->jump_functions, count);
3523 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
3524 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k),
3525 data_in);
3527 ipa_read_indirect_edge_info (ib, data_in, e);
3531 /* Write jump functions for nodes in SET. */
3533 void
3534 ipa_prop_write_jump_functions (void)
3536 struct cgraph_node *node;
3537 struct output_block *ob;
3538 unsigned int count = 0;
3539 lto_symtab_encoder_iterator lsei;
3540 lto_symtab_encoder_t encoder;
3543 if (!ipa_node_params_vector.exists ())
3544 return;
3546 ob = create_output_block (LTO_section_jump_functions);
3547 encoder = ob->decl_state->symtab_node_encoder;
3548 ob->cgraph_node = NULL;
3549 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3550 lsei_next_function_in_partition (&lsei))
3552 node = lsei_cgraph_node (lsei);
3553 if (cgraph_function_with_gimple_body_p (node)
3554 && IPA_NODE_REF (node) != NULL)
3555 count++;
3558 streamer_write_uhwi (ob, count);
3560 /* Process all of the functions. */
3561 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3562 lsei_next_function_in_partition (&lsei))
3564 node = lsei_cgraph_node (lsei);
3565 if (cgraph_function_with_gimple_body_p (node)
3566 && IPA_NODE_REF (node) != NULL)
3567 ipa_write_node_info (ob, node);
3569 streamer_write_char_stream (ob->main_stream, 0);
3570 produce_asm (ob, NULL);
3571 destroy_output_block (ob);
3574 /* Read section in file FILE_DATA of length LEN with data DATA. */
3576 static void
3577 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
3578 size_t len)
3580 const struct lto_function_header *header =
3581 (const struct lto_function_header *) data;
3582 const int cfg_offset = sizeof (struct lto_function_header);
3583 const int main_offset = cfg_offset + header->cfg_size;
3584 const int string_offset = main_offset + header->main_size;
3585 struct data_in *data_in;
3586 struct lto_input_block ib_main;
3587 unsigned int i;
3588 unsigned int count;
3590 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
3591 header->main_size);
3593 data_in =
3594 lto_data_in_create (file_data, (const char *) data + string_offset,
3595 header->string_size, vNULL);
3596 count = streamer_read_uhwi (&ib_main);
3598 for (i = 0; i < count; i++)
3600 unsigned int index;
3601 struct cgraph_node *node;
3602 lto_symtab_encoder_t encoder;
3604 index = streamer_read_uhwi (&ib_main);
3605 encoder = file_data->symtab_node_encoder;
3606 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3607 gcc_assert (node->analyzed);
3608 ipa_read_node_info (&ib_main, node, data_in);
3610 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
3611 len);
3612 lto_data_in_delete (data_in);
3615 /* Read ipcp jump functions. */
3617 void
3618 ipa_prop_read_jump_functions (void)
3620 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3621 struct lto_file_decl_data *file_data;
3622 unsigned int j = 0;
3624 ipa_check_create_node_params ();
3625 ipa_check_create_edge_args ();
3626 ipa_register_cgraph_hooks ();
3628 while ((file_data = file_data_vec[j++]))
3630 size_t len;
3631 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
3633 if (data)
3634 ipa_prop_read_section (file_data, data, len);
3638 /* After merging units, we can get mismatch in argument counts.
3639 Also decl merging might've rendered parameter lists obsolete.
3640 Also compute called_with_variable_arg info. */
3642 void
3643 ipa_update_after_lto_read (void)
3645 struct cgraph_node *node;
3647 ipa_check_create_node_params ();
3648 ipa_check_create_edge_args ();
3650 FOR_EACH_DEFINED_FUNCTION (node)
3651 if (node->analyzed)
3652 ipa_initialize_node_params (node);
3655 void
3656 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
3658 int node_ref;
3659 unsigned int count = 0;
3660 lto_symtab_encoder_t encoder;
3661 struct ipa_agg_replacement_value *aggvals, *av;
3663 aggvals = ipa_get_agg_replacements_for_node (node);
3664 encoder = ob->decl_state->symtab_node_encoder;
3665 node_ref = lto_symtab_encoder_encode (encoder, (symtab_node) node);
3666 streamer_write_uhwi (ob, node_ref);
3668 for (av = aggvals; av; av = av->next)
3669 count++;
3670 streamer_write_uhwi (ob, count);
3672 for (av = aggvals; av; av = av->next)
3674 struct bitpack_d bp;
3676 streamer_write_uhwi (ob, av->offset);
3677 streamer_write_uhwi (ob, av->index);
3678 stream_write_tree (ob, av->value, true);
3680 bp = bitpack_create (ob->main_stream);
3681 bp_pack_value (&bp, av->by_ref, 1);
3682 streamer_write_bitpack (&bp);
3686 /* Stream in the aggregate value replacement chain for NODE from IB. */
3688 static void
3689 read_agg_replacement_chain (struct lto_input_block *ib,
3690 struct cgraph_node *node,
3691 struct data_in *data_in)
3693 struct ipa_agg_replacement_value *aggvals = NULL;
3694 unsigned int count, i;
3696 count = streamer_read_uhwi (ib);
3697 for (i = 0; i <count; i++)
3699 struct ipa_agg_replacement_value *av;
3700 struct bitpack_d bp;
3702 av = ggc_alloc_ipa_agg_replacement_value ();
3703 av->offset = streamer_read_uhwi (ib);
3704 av->index = streamer_read_uhwi (ib);
3705 av->value = stream_read_tree (ib, data_in);
3706 bp = streamer_read_bitpack (ib);
3707 av->by_ref = bp_unpack_value (&bp, 1);
3708 av->next = aggvals;
3709 aggvals = av;
3711 ipa_set_node_agg_value_chain (node, aggvals);
3714 /* Write all aggregate replacement for nodes in set. */
3716 void
3717 ipa_prop_write_all_agg_replacement (void)
3719 struct cgraph_node *node;
3720 struct output_block *ob;
3721 unsigned int count = 0;
3722 lto_symtab_encoder_iterator lsei;
3723 lto_symtab_encoder_t encoder;
3725 if (!ipa_node_agg_replacements)
3726 return;
3728 ob = create_output_block (LTO_section_ipcp_transform);
3729 encoder = ob->decl_state->symtab_node_encoder;
3730 ob->cgraph_node = NULL;
3731 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3732 lsei_next_function_in_partition (&lsei))
3734 node = lsei_cgraph_node (lsei);
3735 if (cgraph_function_with_gimple_body_p (node)
3736 && ipa_get_agg_replacements_for_node (node) != NULL)
3737 count++;
3740 streamer_write_uhwi (ob, count);
3742 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
3743 lsei_next_function_in_partition (&lsei))
3745 node = lsei_cgraph_node (lsei);
3746 if (cgraph_function_with_gimple_body_p (node)
3747 && ipa_get_agg_replacements_for_node (node) != NULL)
3748 write_agg_replacement_chain (ob, node);
3750 streamer_write_char_stream (ob->main_stream, 0);
3751 produce_asm (ob, NULL);
3752 destroy_output_block (ob);
3755 /* Read replacements section in file FILE_DATA of length LEN with data
3756 DATA. */
3758 static void
3759 read_replacements_section (struct lto_file_decl_data *file_data,
3760 const char *data,
3761 size_t len)
3763 const struct lto_function_header *header =
3764 (const struct lto_function_header *) data;
3765 const int cfg_offset = sizeof (struct lto_function_header);
3766 const int main_offset = cfg_offset + header->cfg_size;
3767 const int string_offset = main_offset + header->main_size;
3768 struct data_in *data_in;
3769 struct lto_input_block ib_main;
3770 unsigned int i;
3771 unsigned int count;
3773 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
3774 header->main_size);
3776 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
3777 header->string_size, vNULL);
3778 count = streamer_read_uhwi (&ib_main);
3780 for (i = 0; i < count; i++)
3782 unsigned int index;
3783 struct cgraph_node *node;
3784 lto_symtab_encoder_t encoder;
3786 index = streamer_read_uhwi (&ib_main);
3787 encoder = file_data->symtab_node_encoder;
3788 node = cgraph (lto_symtab_encoder_deref (encoder, index));
3789 gcc_assert (node->analyzed);
3790 read_agg_replacement_chain (&ib_main, node, data_in);
3792 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
3793 len);
3794 lto_data_in_delete (data_in);
3797 /* Read IPA-CP aggregate replacements. */
3799 void
3800 ipa_prop_read_all_agg_replacement (void)
3802 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
3803 struct lto_file_decl_data *file_data;
3804 unsigned int j = 0;
3806 while ((file_data = file_data_vec[j++]))
3808 size_t len;
3809 const char *data = lto_get_section_data (file_data,
3810 LTO_section_ipcp_transform,
3811 NULL, &len);
3812 if (data)
3813 read_replacements_section (file_data, data, len);
3817 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
3818 NODE. */
3820 static void
3821 adjust_agg_replacement_values (struct cgraph_node *node,
3822 struct ipa_agg_replacement_value *aggval)
3824 struct ipa_agg_replacement_value *v;
3825 int i, c = 0, d = 0, *adj;
3827 if (!node->clone.combined_args_to_skip)
3828 return;
3830 for (v = aggval; v; v = v->next)
3832 gcc_assert (v->index >= 0);
3833 if (c < v->index)
3834 c = v->index;
3836 c++;
3838 adj = XALLOCAVEC (int, c);
3839 for (i = 0; i < c; i++)
3840 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
3842 adj[i] = -1;
3843 d++;
3845 else
3846 adj[i] = i - d;
3848 for (v = aggval; v; v = v->next)
3849 v->index = adj[v->index];
3853 /* Function body transformation phase. */
3855 unsigned int
3856 ipcp_transform_function (struct cgraph_node *node)
3858 vec<ipa_param_descriptor_t> descriptors = vNULL;
3859 struct param_analysis_info *parms_ainfo;
3860 struct ipa_agg_replacement_value *aggval;
3861 gimple_stmt_iterator gsi;
3862 basic_block bb;
3863 int param_count;
3864 bool cfg_changed = false, something_changed = false;
3866 gcc_checking_assert (cfun);
3867 gcc_checking_assert (current_function_decl);
3869 if (dump_file)
3870 fprintf (dump_file, "Modification phase of node %s/%i\n",
3871 cgraph_node_name (node), node->uid);
3873 aggval = ipa_get_agg_replacements_for_node (node);
3874 if (!aggval)
3875 return 0;
3876 param_count = count_formal_params (node->symbol.decl);
3877 if (param_count == 0)
3878 return 0;
3879 adjust_agg_replacement_values (node, aggval);
3880 if (dump_file)
3881 ipa_dump_agg_replacement_values (dump_file, aggval);
3882 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
3883 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
3884 descriptors.safe_grow_cleared (param_count);
3885 ipa_populate_param_decls (node, descriptors);
3887 FOR_EACH_BB (bb)
3888 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3890 struct ipa_agg_replacement_value *v;
3891 gimple stmt = gsi_stmt (gsi);
3892 tree rhs, val, t;
3893 HOST_WIDE_INT offset, size;
3894 int index;
3895 bool by_ref, vce;
3897 if (!gimple_assign_load_p (stmt))
3898 continue;
3899 rhs = gimple_assign_rhs1 (stmt);
3900 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
3901 continue;
3903 vce = false;
3904 t = rhs;
3905 while (handled_component_p (t))
3907 /* V_C_E can do things like convert an array of integers to one
3908 bigger integer and similar things we do not handle below. */
3909 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
3911 vce = true;
3912 break;
3914 t = TREE_OPERAND (t, 0);
3916 if (vce)
3917 continue;
3919 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
3920 rhs, &index, &offset, &size, &by_ref))
3921 continue;
3922 for (v = aggval; v; v = v->next)
3923 if (v->index == index
3924 && v->offset == offset)
3925 break;
3926 if (!v
3927 || v->by_ref != by_ref
3928 || tree_low_cst (TYPE_SIZE (TREE_TYPE (v->value)), 0) != size)
3929 continue;
3931 gcc_checking_assert (is_gimple_ip_invariant (v->value));
3932 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
3934 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
3935 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
3936 else if (TYPE_SIZE (TREE_TYPE (rhs))
3937 == TYPE_SIZE (TREE_TYPE (v->value)))
3938 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
3939 else
3941 if (dump_file)
3943 fprintf (dump_file, " const ");
3944 print_generic_expr (dump_file, v->value, 0);
3945 fprintf (dump_file, " can't be converted to type of ");
3946 print_generic_expr (dump_file, rhs, 0);
3947 fprintf (dump_file, "\n");
3949 continue;
3952 else
3953 val = v->value;
3955 if (dump_file && (dump_flags & TDF_DETAILS))
3957 fprintf (dump_file, "Modifying stmt:\n ");
3958 print_gimple_stmt (dump_file, stmt, 0, 0);
3960 gimple_assign_set_rhs_from_tree (&gsi, val);
3961 update_stmt (stmt);
3963 if (dump_file && (dump_flags & TDF_DETAILS))
3965 fprintf (dump_file, "into:\n ");
3966 print_gimple_stmt (dump_file, stmt, 0, 0);
3967 fprintf (dump_file, "\n");
3970 something_changed = true;
3971 if (maybe_clean_eh_stmt (stmt)
3972 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
3973 cfg_changed = true;
3976 (*ipa_node_agg_replacements)[node->uid] = NULL;
3977 free_parms_ainfo (parms_ainfo, param_count);
3978 descriptors.release ();
3980 if (!something_changed)
3981 return 0;
3982 else if (cfg_changed)
3983 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
3984 else
3985 return TODO_update_ssa_only_virtuals;