* config/rx/rx.c (ADD_RX_BUILTIN0): New macro, used for builtins
[official-gcc.git] / gcc / ipa-prop.c
blob1ceabfabb3f48e6dba7d0d84294579b3469dcca8
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 "gimple.h"
25 #include "langhooks.h"
26 #include "ggc.h"
27 #include "target.h"
28 #include "ipa-prop.h"
29 #include "bitmap.h"
30 #include "gimple-ssa.h"
31 #include "tree-cfg.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-into-ssa.h"
35 #include "tree-dfa.h"
36 #include "tree-pass.h"
37 #include "tree-inline.h"
38 #include "ipa-inline.h"
39 #include "flags.h"
40 #include "diagnostic.h"
41 #include "gimple-pretty-print.h"
42 #include "lto-streamer.h"
43 #include "data-streamer.h"
44 #include "tree-streamer.h"
45 #include "params.h"
46 #include "ipa-utils.h"
48 /* Intermediate information about a parameter that is only useful during the
49 run of ipa_analyze_node and is not kept afterwards. */
51 struct param_analysis_info
53 bool parm_modified, ref_modified, pt_modified;
54 bitmap parm_visited_statements, pt_visited_statements;
57 /* Vector where the parameter infos are actually stored. */
58 vec<ipa_node_params_t> ipa_node_params_vector;
59 /* Vector of known aggregate values in cloned nodes. */
60 vec<ipa_agg_replacement_value_p, va_gc> *ipa_node_agg_replacements;
61 /* Vector where the parameter infos are actually stored. */
62 vec<ipa_edge_args_t, va_gc> *ipa_edge_args_vector;
64 /* Holders of ipa cgraph hooks: */
65 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
66 static struct cgraph_node_hook_list *node_removal_hook_holder;
67 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
68 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
69 static struct cgraph_node_hook_list *function_insertion_hook_holder;
71 /* Description of a reference to an IPA constant. */
72 struct ipa_cst_ref_desc
74 /* Edge that corresponds to the statement which took the reference. */
75 struct cgraph_edge *cs;
76 /* Linked list of duplicates created when call graph edges are cloned. */
77 struct ipa_cst_ref_desc *next_duplicate;
78 /* Number of references in IPA structures, IPA_UNDESCRIBED_USE if the value
79 if out of control. */
80 int refcount;
83 /* Allocation pool for reference descriptions. */
85 static alloc_pool ipa_refdesc_pool;
87 /* Return true if DECL_FUNCTION_SPECIFIC_OPTIMIZATION of the decl associated
88 with NODE should prevent us from analyzing it for the purposes of IPA-CP. */
90 static bool
91 ipa_func_spec_opts_forbid_analysis_p (struct cgraph_node *node)
93 tree fs_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (node->decl);
94 struct cl_optimization *os;
96 if (!fs_opts)
97 return false;
98 os = TREE_OPTIMIZATION (fs_opts);
99 return !os->x_optimize || !os->x_flag_ipa_cp;
102 /* Return index of the formal whose tree is PTREE in function which corresponds
103 to INFO. */
105 static int
106 ipa_get_param_decl_index_1 (vec<ipa_param_descriptor_t> descriptors, tree ptree)
108 int i, count;
110 count = descriptors.length ();
111 for (i = 0; i < count; i++)
112 if (descriptors[i].decl == ptree)
113 return i;
115 return -1;
118 /* Return index of the formal whose tree is PTREE in function which corresponds
119 to INFO. */
122 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
124 return ipa_get_param_decl_index_1 (info->descriptors, ptree);
127 /* Populate the param_decl field in parameter DESCRIPTORS that correspond to
128 NODE. */
130 static void
131 ipa_populate_param_decls (struct cgraph_node *node,
132 vec<ipa_param_descriptor_t> &descriptors)
134 tree fndecl;
135 tree fnargs;
136 tree parm;
137 int param_num;
139 fndecl = node->decl;
140 gcc_assert (gimple_has_body_p (fndecl));
141 fnargs = DECL_ARGUMENTS (fndecl);
142 param_num = 0;
143 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
145 descriptors[param_num].decl = parm;
146 descriptors[param_num].move_cost = estimate_move_cost (TREE_TYPE (parm));
147 param_num++;
151 /* Return how many formal parameters FNDECL has. */
153 static inline int
154 count_formal_params (tree fndecl)
156 tree parm;
157 int count = 0;
158 gcc_assert (gimple_has_body_p (fndecl));
160 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
161 count++;
163 return count;
166 /* Return the declaration of Ith formal parameter of the function corresponding
167 to INFO. Note there is no setter function as this array is built just once
168 using ipa_initialize_node_params. */
170 void
171 ipa_dump_param (FILE *file, struct ipa_node_params *info, int i)
173 fprintf (file, "param #%i", i);
174 if (info->descriptors[i].decl)
176 fprintf (file, " ");
177 print_generic_expr (file, info->descriptors[i].decl, 0);
181 /* Initialize the ipa_node_params structure associated with NODE
182 to hold PARAM_COUNT parameters. */
184 void
185 ipa_alloc_node_params (struct cgraph_node *node, int param_count)
187 struct ipa_node_params *info = IPA_NODE_REF (node);
189 if (!info->descriptors.exists () && param_count)
190 info->descriptors.safe_grow_cleared (param_count);
193 /* Initialize the ipa_node_params structure associated with NODE by counting
194 the function parameters, creating the descriptors and populating their
195 param_decls. */
197 void
198 ipa_initialize_node_params (struct cgraph_node *node)
200 struct ipa_node_params *info = IPA_NODE_REF (node);
202 if (!info->descriptors.exists ())
204 ipa_alloc_node_params (node, count_formal_params (node->decl));
205 ipa_populate_param_decls (node, info->descriptors);
209 /* Print the jump functions associated with call graph edge CS to file F. */
211 static void
212 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
214 int i, count;
216 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
217 for (i = 0; i < count; i++)
219 struct ipa_jump_func *jump_func;
220 enum jump_func_type type;
222 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
223 type = jump_func->type;
225 fprintf (f, " param %d: ", i);
226 if (type == IPA_JF_UNKNOWN)
227 fprintf (f, "UNKNOWN\n");
228 else if (type == IPA_JF_KNOWN_TYPE)
230 fprintf (f, "KNOWN TYPE: base ");
231 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
232 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
233 jump_func->value.known_type.offset);
234 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
235 fprintf (f, "\n");
237 else if (type == IPA_JF_CONST)
239 tree val = jump_func->value.constant.value;
240 fprintf (f, "CONST: ");
241 print_generic_expr (f, val, 0);
242 if (TREE_CODE (val) == ADDR_EXPR
243 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
245 fprintf (f, " -> ");
246 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
249 fprintf (f, "\n");
251 else if (type == IPA_JF_PASS_THROUGH)
253 fprintf (f, "PASS THROUGH: ");
254 fprintf (f, "%d, op %s",
255 jump_func->value.pass_through.formal_id,
256 get_tree_code_name(jump_func->value.pass_through.operation));
257 if (jump_func->value.pass_through.operation != NOP_EXPR)
259 fprintf (f, " ");
260 print_generic_expr (f,
261 jump_func->value.pass_through.operand, 0);
263 if (jump_func->value.pass_through.agg_preserved)
264 fprintf (f, ", agg_preserved");
265 if (jump_func->value.pass_through.type_preserved)
266 fprintf (f, ", type_preserved");
267 fprintf (f, "\n");
269 else if (type == IPA_JF_ANCESTOR)
271 fprintf (f, "ANCESTOR: ");
272 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
273 jump_func->value.ancestor.formal_id,
274 jump_func->value.ancestor.offset);
275 print_generic_expr (f, jump_func->value.ancestor.type, 0);
276 if (jump_func->value.ancestor.agg_preserved)
277 fprintf (f, ", agg_preserved");
278 if (jump_func->value.ancestor.type_preserved)
279 fprintf (f, ", type_preserved");
280 fprintf (f, "\n");
283 if (jump_func->agg.items)
285 struct ipa_agg_jf_item *item;
286 int j;
288 fprintf (f, " Aggregate passed by %s:\n",
289 jump_func->agg.by_ref ? "reference" : "value");
290 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, j, item)
292 fprintf (f, " offset: " HOST_WIDE_INT_PRINT_DEC ", ",
293 item->offset);
294 if (TYPE_P (item->value))
295 fprintf (f, "clobber of " HOST_WIDE_INT_PRINT_DEC " bits",
296 tree_low_cst (TYPE_SIZE (item->value), 1));
297 else
299 fprintf (f, "cst: ");
300 print_generic_expr (f, item->value, 0);
302 fprintf (f, "\n");
309 /* Print the jump functions of all arguments on all call graph edges going from
310 NODE to file F. */
312 void
313 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
315 struct cgraph_edge *cs;
317 fprintf (f, " Jump functions of caller %s/%i:\n", cgraph_node_name (node),
318 node->order);
319 for (cs = node->callees; cs; cs = cs->next_callee)
321 if (!ipa_edge_args_info_available_for_edge_p (cs))
322 continue;
324 fprintf (f, " callsite %s/%i -> %s/%i : \n",
325 xstrdup (cgraph_node_name (node)), node->order,
326 xstrdup (cgraph_node_name (cs->callee)),
327 cs->callee->order);
328 ipa_print_node_jump_functions_for_edge (f, cs);
331 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
333 struct cgraph_indirect_call_info *ii;
334 if (!ipa_edge_args_info_available_for_edge_p (cs))
335 continue;
337 ii = cs->indirect_info;
338 if (ii->agg_contents)
339 fprintf (f, " indirect %s callsite, calling param %i, "
340 "offset " HOST_WIDE_INT_PRINT_DEC ", %s",
341 ii->member_ptr ? "member ptr" : "aggregate",
342 ii->param_index, ii->offset,
343 ii->by_ref ? "by reference" : "by_value");
344 else
345 fprintf (f, " indirect %s callsite, calling param %i",
346 ii->polymorphic ? "polymorphic" : "simple", ii->param_index);
348 if (cs->call_stmt)
350 fprintf (f, ", for stmt ");
351 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
353 else
354 fprintf (f, "\n");
355 ipa_print_node_jump_functions_for_edge (f, cs);
359 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
361 void
362 ipa_print_all_jump_functions (FILE *f)
364 struct cgraph_node *node;
366 fprintf (f, "\nJump functions:\n");
367 FOR_EACH_FUNCTION (node)
369 ipa_print_node_jump_functions (f, node);
373 /* Set JFUNC to be a known type jump function. */
375 static void
376 ipa_set_jf_known_type (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
377 tree base_type, tree component_type)
379 gcc_assert (TREE_CODE (component_type) == RECORD_TYPE
380 && TYPE_BINFO (component_type));
381 jfunc->type = IPA_JF_KNOWN_TYPE;
382 jfunc->value.known_type.offset = offset,
383 jfunc->value.known_type.base_type = base_type;
384 jfunc->value.known_type.component_type = component_type;
387 /* Set JFUNC to be a copy of another jmp (to be used by jump function
388 combination code). The two functions will share their rdesc. */
390 static void
391 ipa_set_jf_cst_copy (struct ipa_jump_func *dst,
392 struct ipa_jump_func *src)
395 gcc_checking_assert (src->type == IPA_JF_CONST);
396 dst->type = IPA_JF_CONST;
397 dst->value.constant = src->value.constant;
400 /* Set JFUNC to be a constant jmp function. */
402 static void
403 ipa_set_jf_constant (struct ipa_jump_func *jfunc, tree constant,
404 struct cgraph_edge *cs)
406 constant = unshare_expr (constant);
407 if (constant && EXPR_P (constant))
408 SET_EXPR_LOCATION (constant, UNKNOWN_LOCATION);
409 jfunc->type = IPA_JF_CONST;
410 jfunc->value.constant.value = unshare_expr_without_location (constant);
412 if (TREE_CODE (constant) == ADDR_EXPR
413 && TREE_CODE (TREE_OPERAND (constant, 0)) == FUNCTION_DECL)
415 struct ipa_cst_ref_desc *rdesc;
416 if (!ipa_refdesc_pool)
417 ipa_refdesc_pool = create_alloc_pool ("IPA-PROP ref descriptions",
418 sizeof (struct ipa_cst_ref_desc), 32);
420 rdesc = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
421 rdesc->cs = cs;
422 rdesc->next_duplicate = NULL;
423 rdesc->refcount = 1;
424 jfunc->value.constant.rdesc = rdesc;
426 else
427 jfunc->value.constant.rdesc = NULL;
430 /* Set JFUNC to be a simple pass-through jump function. */
431 static void
432 ipa_set_jf_simple_pass_through (struct ipa_jump_func *jfunc, int formal_id,
433 bool agg_preserved, bool type_preserved)
435 jfunc->type = IPA_JF_PASS_THROUGH;
436 jfunc->value.pass_through.operand = NULL_TREE;
437 jfunc->value.pass_through.formal_id = formal_id;
438 jfunc->value.pass_through.operation = NOP_EXPR;
439 jfunc->value.pass_through.agg_preserved = agg_preserved;
440 jfunc->value.pass_through.type_preserved = type_preserved;
443 /* Set JFUNC to be an arithmetic pass through jump function. */
445 static void
446 ipa_set_jf_arith_pass_through (struct ipa_jump_func *jfunc, int formal_id,
447 tree operand, enum tree_code operation)
449 jfunc->type = IPA_JF_PASS_THROUGH;
450 jfunc->value.pass_through.operand = unshare_expr_without_location (operand);
451 jfunc->value.pass_through.formal_id = formal_id;
452 jfunc->value.pass_through.operation = operation;
453 jfunc->value.pass_through.agg_preserved = false;
454 jfunc->value.pass_through.type_preserved = false;
457 /* Set JFUNC to be an ancestor jump function. */
459 static void
460 ipa_set_ancestor_jf (struct ipa_jump_func *jfunc, HOST_WIDE_INT offset,
461 tree type, int formal_id, bool agg_preserved,
462 bool type_preserved)
464 jfunc->type = IPA_JF_ANCESTOR;
465 jfunc->value.ancestor.formal_id = formal_id;
466 jfunc->value.ancestor.offset = offset;
467 jfunc->value.ancestor.type = type;
468 jfunc->value.ancestor.agg_preserved = agg_preserved;
469 jfunc->value.ancestor.type_preserved = type_preserved;
472 /* Extract the acual BINFO being described by JFUNC which must be a known type
473 jump function. */
475 tree
476 ipa_binfo_from_known_type_jfunc (struct ipa_jump_func *jfunc)
478 tree base_binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
479 if (!base_binfo)
480 return NULL_TREE;
481 return get_binfo_at_offset (base_binfo,
482 jfunc->value.known_type.offset,
483 jfunc->value.known_type.component_type);
486 /* Structure to be passed in between detect_type_change and
487 check_stmt_for_type_change. */
489 struct type_change_info
491 /* Offset into the object where there is the virtual method pointer we are
492 looking for. */
493 HOST_WIDE_INT offset;
494 /* The declaration or SSA_NAME pointer of the base that we are checking for
495 type change. */
496 tree object;
497 /* If we actually can tell the type that the object has changed to, it is
498 stored in this field. Otherwise it remains NULL_TREE. */
499 tree known_current_type;
500 /* Set to true if dynamic type change has been detected. */
501 bool type_maybe_changed;
502 /* Set to true if multiple types have been encountered. known_current_type
503 must be disregarded in that case. */
504 bool multiple_types_encountered;
507 /* Return true if STMT can modify a virtual method table pointer.
509 This function makes special assumptions about both constructors and
510 destructors which are all the functions that are allowed to alter the VMT
511 pointers. It assumes that destructors begin with assignment into all VMT
512 pointers and that constructors essentially look in the following way:
514 1) The very first thing they do is that they call constructors of ancestor
515 sub-objects that have them.
517 2) Then VMT pointers of this and all its ancestors is set to new values
518 corresponding to the type corresponding to the constructor.
520 3) Only afterwards, other stuff such as constructor of member sub-objects
521 and the code written by the user is run. Only this may include calling
522 virtual functions, directly or indirectly.
524 There is no way to call a constructor of an ancestor sub-object in any
525 other way.
527 This means that we do not have to care whether constructors get the correct
528 type information because they will always change it (in fact, if we define
529 the type to be given by the VMT pointer, it is undefined).
531 The most important fact to derive from the above is that if, for some
532 statement in the section 3, we try to detect whether the dynamic type has
533 changed, we can safely ignore all calls as we examine the function body
534 backwards until we reach statements in section 2 because these calls cannot
535 be ancestor constructors or destructors (if the input is not bogus) and so
536 do not change the dynamic type (this holds true only for automatically
537 allocated objects but at the moment we devirtualize only these). We then
538 must detect that statements in section 2 change the dynamic type and can try
539 to derive the new type. That is enough and we can stop, we will never see
540 the calls into constructors of sub-objects in this code. Therefore we can
541 safely ignore all call statements that we traverse.
544 static bool
545 stmt_may_be_vtbl_ptr_store (gimple stmt)
547 if (is_gimple_call (stmt))
548 return false;
549 else if (is_gimple_assign (stmt))
551 tree lhs = gimple_assign_lhs (stmt);
553 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
555 if (flag_strict_aliasing
556 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
557 return false;
559 if (TREE_CODE (lhs) == COMPONENT_REF
560 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
561 return false;
562 /* In the future we might want to use get_base_ref_and_offset to find
563 if there is a field corresponding to the offset and if so, proceed
564 almost like if it was a component ref. */
567 return true;
570 /* If STMT can be proved to be an assignment to the virtual method table
571 pointer of ANALYZED_OBJ and the type associated with the new table
572 identified, return the type. Otherwise return NULL_TREE. */
574 static tree
575 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci)
577 HOST_WIDE_INT offset, size, max_size;
578 tree lhs, rhs, base;
580 if (!gimple_assign_single_p (stmt))
581 return NULL_TREE;
583 lhs = gimple_assign_lhs (stmt);
584 rhs = gimple_assign_rhs1 (stmt);
585 if (TREE_CODE (lhs) != COMPONENT_REF
586 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1))
587 || TREE_CODE (rhs) != ADDR_EXPR)
588 return NULL_TREE;
589 rhs = get_base_address (TREE_OPERAND (rhs, 0));
590 if (!rhs
591 || TREE_CODE (rhs) != VAR_DECL
592 || !DECL_VIRTUAL_P (rhs))
593 return NULL_TREE;
595 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
596 if (offset != tci->offset
597 || size != POINTER_SIZE
598 || max_size != POINTER_SIZE)
599 return NULL_TREE;
600 if (TREE_CODE (base) == MEM_REF)
602 if (TREE_CODE (tci->object) != MEM_REF
603 || TREE_OPERAND (tci->object, 0) != TREE_OPERAND (base, 0)
604 || !tree_int_cst_equal (TREE_OPERAND (tci->object, 1),
605 TREE_OPERAND (base, 1)))
606 return NULL_TREE;
608 else if (tci->object != base)
609 return NULL_TREE;
611 return DECL_CONTEXT (rhs);
614 /* Callback of walk_aliased_vdefs and a helper function for
615 detect_type_change to check whether a particular statement may modify
616 the virtual table pointer, and if possible also determine the new type of
617 the (sub-)object. It stores its result into DATA, which points to a
618 type_change_info structure. */
620 static bool
621 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
623 gimple stmt = SSA_NAME_DEF_STMT (vdef);
624 struct type_change_info *tci = (struct type_change_info *) data;
626 if (stmt_may_be_vtbl_ptr_store (stmt))
628 tree type;
629 type = extr_type_from_vtbl_ptr_store (stmt, tci);
630 if (tci->type_maybe_changed
631 && type != tci->known_current_type)
632 tci->multiple_types_encountered = true;
633 tci->known_current_type = type;
634 tci->type_maybe_changed = true;
635 return true;
637 else
638 return false;
643 /* Detect whether the dynamic type of ARG of COMP_TYPE has changed (before
644 callsite CALL) by looking for assignments to its virtual table pointer. If
645 it is, return true and fill in the jump function JFUNC with relevant type
646 information or set it to unknown. ARG is the object itself (not a pointer
647 to it, unless dereferenced). BASE is the base of the memory access as
648 returned by get_ref_base_and_extent, as is the offset. */
650 static bool
651 detect_type_change (tree arg, tree base, tree comp_type, gimple call,
652 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
654 struct type_change_info tci;
655 ao_ref ao;
657 gcc_checking_assert (DECL_P (arg)
658 || TREE_CODE (arg) == MEM_REF
659 || handled_component_p (arg));
660 /* Const calls cannot call virtual methods through VMT and so type changes do
661 not matter. */
662 if (!flag_devirtualize || !gimple_vuse (call)
663 /* Be sure expected_type is polymorphic. */
664 || !comp_type
665 || TREE_CODE (comp_type) != RECORD_TYPE
666 || !TYPE_BINFO (comp_type)
667 || !BINFO_VTABLE (TYPE_BINFO (comp_type)))
668 return false;
670 ao_ref_init (&ao, arg);
671 ao.base = base;
672 ao.offset = offset;
673 ao.size = POINTER_SIZE;
674 ao.max_size = ao.size;
676 tci.offset = offset;
677 tci.object = get_base_address (arg);
678 tci.known_current_type = NULL_TREE;
679 tci.type_maybe_changed = false;
680 tci.multiple_types_encountered = false;
682 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
683 &tci, NULL);
684 if (!tci.type_maybe_changed)
685 return false;
687 if (!tci.known_current_type
688 || tci.multiple_types_encountered
689 || offset != 0)
690 jfunc->type = IPA_JF_UNKNOWN;
691 else
692 ipa_set_jf_known_type (jfunc, 0, tci.known_current_type, comp_type);
694 return true;
697 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
698 SSA name (its dereference will become the base and the offset is assumed to
699 be zero). */
701 static bool
702 detect_type_change_ssa (tree arg, tree comp_type,
703 gimple call, struct ipa_jump_func *jfunc)
705 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
706 if (!flag_devirtualize
707 || !POINTER_TYPE_P (TREE_TYPE (arg)))
708 return false;
710 arg = build2 (MEM_REF, ptr_type_node, arg,
711 build_int_cst (ptr_type_node, 0));
713 return detect_type_change (arg, arg, comp_type, call, jfunc, 0);
716 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
717 boolean variable pointed to by DATA. */
719 static bool
720 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
721 void *data)
723 bool *b = (bool *) data;
724 *b = true;
725 return true;
728 /* Return true if a load from a formal parameter PARM_LOAD is known to retrieve
729 a value known not to be modified in this function before reaching the
730 statement STMT. PARM_AINFO is a pointer to a structure containing temporary
731 information about the parameter. */
733 static bool
734 parm_preserved_before_stmt_p (struct param_analysis_info *parm_ainfo,
735 gimple stmt, tree parm_load)
737 bool modified = false;
738 bitmap *visited_stmts;
739 ao_ref refd;
741 if (parm_ainfo && parm_ainfo->parm_modified)
742 return false;
744 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
745 ao_ref_init (&refd, parm_load);
746 /* We can cache visited statements only when parm_ainfo is available and when
747 we are looking at a naked load of the whole parameter. */
748 if (!parm_ainfo || TREE_CODE (parm_load) != PARM_DECL)
749 visited_stmts = NULL;
750 else
751 visited_stmts = &parm_ainfo->parm_visited_statements;
752 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
753 visited_stmts);
754 if (parm_ainfo && modified)
755 parm_ainfo->parm_modified = true;
756 return !modified;
759 /* If STMT is an assignment that loads a value from an parameter declaration,
760 return the index of the parameter in ipa_node_params which has not been
761 modified. Otherwise return -1. */
763 static int
764 load_from_unmodified_param (vec<ipa_param_descriptor_t> descriptors,
765 struct param_analysis_info *parms_ainfo,
766 gimple stmt)
768 int index;
769 tree op1;
771 if (!gimple_assign_single_p (stmt))
772 return -1;
774 op1 = gimple_assign_rhs1 (stmt);
775 if (TREE_CODE (op1) != PARM_DECL)
776 return -1;
778 index = ipa_get_param_decl_index_1 (descriptors, op1);
779 if (index < 0
780 || !parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
781 : NULL, stmt, op1))
782 return -1;
784 return index;
787 /* Return true if memory reference REF loads data that are known to be
788 unmodified in this function before reaching statement STMT. PARM_AINFO, if
789 non-NULL, is a pointer to a structure containing temporary information about
790 PARM. */
792 static bool
793 parm_ref_data_preserved_p (struct param_analysis_info *parm_ainfo,
794 gimple stmt, tree ref)
796 bool modified = false;
797 ao_ref refd;
799 gcc_checking_assert (gimple_vuse (stmt));
800 if (parm_ainfo && parm_ainfo->ref_modified)
801 return false;
803 ao_ref_init (&refd, ref);
804 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified, &modified,
805 NULL);
806 if (parm_ainfo && modified)
807 parm_ainfo->ref_modified = true;
808 return !modified;
811 /* Return true if the data pointed to by PARM is known to be unmodified in this
812 function before reaching call statement CALL into which it is passed.
813 PARM_AINFO is a pointer to a structure containing temporary information
814 about PARM. */
816 static bool
817 parm_ref_data_pass_through_p (struct param_analysis_info *parm_ainfo,
818 gimple call, tree parm)
820 bool modified = false;
821 ao_ref refd;
823 /* It's unnecessary to calculate anything about memory contnets for a const
824 function because it is not goin to use it. But do not cache the result
825 either. Also, no such calculations for non-pointers. */
826 if (!gimple_vuse (call)
827 || !POINTER_TYPE_P (TREE_TYPE (parm)))
828 return false;
830 if (parm_ainfo->pt_modified)
831 return false;
833 ao_ref_init_from_ptr_and_size (&refd, parm, NULL_TREE);
834 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified, &modified,
835 parm_ainfo ? &parm_ainfo->pt_visited_statements : NULL);
836 if (modified)
837 parm_ainfo->pt_modified = true;
838 return !modified;
841 /* Return true if we can prove that OP is a memory reference loading unmodified
842 data from an aggregate passed as a parameter and if the aggregate is passed
843 by reference, that the alias type of the load corresponds to the type of the
844 formal parameter (so that we can rely on this type for TBAA in callers).
845 INFO and PARMS_AINFO describe parameters of the current function (but the
846 latter can be NULL), STMT is the load statement. If function returns true,
847 *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
848 within the aggregate and whether it is a load from a value passed by
849 reference respectively. */
851 static bool
852 ipa_load_from_parm_agg_1 (vec<ipa_param_descriptor_t> descriptors,
853 struct param_analysis_info *parms_ainfo, gimple stmt,
854 tree op, int *index_p, HOST_WIDE_INT *offset_p,
855 bool *by_ref_p)
857 int index;
858 HOST_WIDE_INT size, max_size;
859 tree base = get_ref_base_and_extent (op, offset_p, &size, &max_size);
861 if (max_size == -1 || max_size != size || *offset_p < 0)
862 return false;
864 if (DECL_P (base))
866 int index = ipa_get_param_decl_index_1 (descriptors, base);
867 if (index >= 0
868 && parm_preserved_before_stmt_p (parms_ainfo ? &parms_ainfo[index]
869 : NULL, stmt, op))
871 *index_p = index;
872 *by_ref_p = false;
873 return true;
875 return false;
878 if (TREE_CODE (base) != MEM_REF
879 || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
880 || !integer_zerop (TREE_OPERAND (base, 1)))
881 return false;
883 if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
885 tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
886 index = ipa_get_param_decl_index_1 (descriptors, parm);
888 else
890 /* This branch catches situations where a pointer parameter is not a
891 gimple register, for example:
893 void hip7(S*) (struct S * p)
895 void (*<T2e4>) (struct S *) D.1867;
896 struct S * p.1;
898 <bb 2>:
899 p.1_1 = p;
900 D.1867_2 = p.1_1->f;
901 D.1867_2 ();
902 gdp = &p;
905 gimple def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
906 index = load_from_unmodified_param (descriptors, parms_ainfo, def);
909 if (index >= 0
910 && parm_ref_data_preserved_p (parms_ainfo ? &parms_ainfo[index] : NULL,
911 stmt, op))
913 *index_p = index;
914 *by_ref_p = true;
915 return true;
917 return false;
920 /* Just like the previous function, just without the param_analysis_info
921 pointer, for users outside of this file. */
923 bool
924 ipa_load_from_parm_agg (struct ipa_node_params *info, gimple stmt,
925 tree op, int *index_p, HOST_WIDE_INT *offset_p,
926 bool *by_ref_p)
928 return ipa_load_from_parm_agg_1 (info->descriptors, NULL, stmt, op, index_p,
929 offset_p, by_ref_p);
932 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
933 of an assignment statement STMT, try to determine whether we are actually
934 handling any of the following cases and construct an appropriate jump
935 function into JFUNC if so:
937 1) The passed value is loaded from a formal parameter which is not a gimple
938 register (most probably because it is addressable, the value has to be
939 scalar) and we can guarantee the value has not changed. This case can
940 therefore be described by a simple pass-through jump function. For example:
942 foo (int a)
944 int a.0;
946 a.0_2 = a;
947 bar (a.0_2);
949 2) The passed value can be described by a simple arithmetic pass-through
950 jump function. E.g.
952 foo (int a)
954 int D.2064;
956 D.2064_4 = a.1(D) + 4;
957 bar (D.2064_4);
959 This case can also occur in combination of the previous one, e.g.:
961 foo (int a, int z)
963 int a.0;
964 int D.2064;
966 a.0_3 = a;
967 D.2064_4 = a.0_3 + 4;
968 foo (D.2064_4);
970 3) The passed value is an address of an object within another one (which
971 also passed by reference). Such situations are described by an ancestor
972 jump function and describe situations such as:
974 B::foo() (struct B * const this)
976 struct A * D.1845;
978 D.1845_2 = &this_1(D)->D.1748;
979 A::bar (D.1845_2);
981 INFO is the structure describing individual parameters access different
982 stages of IPA optimizations. PARMS_AINFO contains the information that is
983 only needed for intraprocedural analysis. */
985 static void
986 compute_complex_assign_jump_func (struct ipa_node_params *info,
987 struct param_analysis_info *parms_ainfo,
988 struct ipa_jump_func *jfunc,
989 gimple call, gimple stmt, tree name,
990 tree param_type)
992 HOST_WIDE_INT offset, size, max_size;
993 tree op1, tc_ssa, base, ssa;
994 int index;
996 op1 = gimple_assign_rhs1 (stmt);
998 if (TREE_CODE (op1) == SSA_NAME)
1000 if (SSA_NAME_IS_DEFAULT_DEF (op1))
1001 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
1002 else
1003 index = load_from_unmodified_param (info->descriptors, parms_ainfo,
1004 SSA_NAME_DEF_STMT (op1));
1005 tc_ssa = op1;
1007 else
1009 index = load_from_unmodified_param (info->descriptors, parms_ainfo, stmt);
1010 tc_ssa = gimple_assign_lhs (stmt);
1013 if (index >= 0)
1015 tree op2 = gimple_assign_rhs2 (stmt);
1017 if (op2)
1019 if (!is_gimple_ip_invariant (op2)
1020 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
1021 && !useless_type_conversion_p (TREE_TYPE (name),
1022 TREE_TYPE (op1))))
1023 return;
1025 ipa_set_jf_arith_pass_through (jfunc, index, op2,
1026 gimple_assign_rhs_code (stmt));
1028 else if (gimple_assign_single_p (stmt))
1030 bool agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1031 call, tc_ssa);
1032 bool type_p = false;
1034 if (param_type && POINTER_TYPE_P (param_type))
1035 type_p = !detect_type_change_ssa (tc_ssa, TREE_TYPE (param_type),
1036 call, jfunc);
1037 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1038 ipa_set_jf_simple_pass_through (jfunc, index, agg_p, type_p);
1040 return;
1043 if (TREE_CODE (op1) != ADDR_EXPR)
1044 return;
1045 op1 = TREE_OPERAND (op1, 0);
1046 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
1047 return;
1048 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
1049 if (TREE_CODE (base) != MEM_REF
1050 /* If this is a varying address, punt. */
1051 || max_size == -1
1052 || max_size != size)
1053 return;
1054 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
1055 ssa = TREE_OPERAND (base, 0);
1056 if (TREE_CODE (ssa) != SSA_NAME
1057 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
1058 || offset < 0)
1059 return;
1061 /* Dynamic types are changed in constructors and destructors. */
1062 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
1063 if (index >= 0 && param_type && POINTER_TYPE_P (param_type))
1065 bool type_p = !detect_type_change (op1, base, TREE_TYPE (param_type),
1066 call, jfunc, offset);
1067 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1068 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (op1), index,
1069 parm_ref_data_pass_through_p (&parms_ainfo[index],
1070 call, ssa), type_p);
1074 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
1075 it looks like:
1077 iftmp.1_3 = &obj_2(D)->D.1762;
1079 The base of the MEM_REF must be a default definition SSA NAME of a
1080 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
1081 whole MEM_REF expression is returned and the offset calculated from any
1082 handled components and the MEM_REF itself is stored into *OFFSET. The whole
1083 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
1085 static tree
1086 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
1088 HOST_WIDE_INT size, max_size;
1089 tree expr, parm, obj;
1091 if (!gimple_assign_single_p (assign))
1092 return NULL_TREE;
1093 expr = gimple_assign_rhs1 (assign);
1095 if (TREE_CODE (expr) != ADDR_EXPR)
1096 return NULL_TREE;
1097 expr = TREE_OPERAND (expr, 0);
1098 obj = expr;
1099 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
1101 if (TREE_CODE (expr) != MEM_REF
1102 /* If this is a varying address, punt. */
1103 || max_size == -1
1104 || max_size != size
1105 || *offset < 0)
1106 return NULL_TREE;
1107 parm = TREE_OPERAND (expr, 0);
1108 if (TREE_CODE (parm) != SSA_NAME
1109 || !SSA_NAME_IS_DEFAULT_DEF (parm)
1110 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
1111 return NULL_TREE;
1113 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
1114 *obj_p = obj;
1115 return expr;
1119 /* Given that an actual argument is an SSA_NAME that is a result of a phi
1120 statement PHI, try to find out whether NAME is in fact a
1121 multiple-inheritance typecast from a descendant into an ancestor of a formal
1122 parameter and thus can be described by an ancestor jump function and if so,
1123 write the appropriate function into JFUNC.
1125 Essentially we want to match the following pattern:
1127 if (obj_2(D) != 0B)
1128 goto <bb 3>;
1129 else
1130 goto <bb 4>;
1132 <bb 3>:
1133 iftmp.1_3 = &obj_2(D)->D.1762;
1135 <bb 4>:
1136 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
1137 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
1138 return D.1879_6; */
1140 static void
1141 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
1142 struct param_analysis_info *parms_ainfo,
1143 struct ipa_jump_func *jfunc,
1144 gimple call, gimple phi, tree param_type)
1146 HOST_WIDE_INT offset;
1147 gimple assign, cond;
1148 basic_block phi_bb, assign_bb, cond_bb;
1149 tree tmp, parm, expr, obj;
1150 int index, i;
1152 if (gimple_phi_num_args (phi) != 2)
1153 return;
1155 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
1156 tmp = PHI_ARG_DEF (phi, 0);
1157 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
1158 tmp = PHI_ARG_DEF (phi, 1);
1159 else
1160 return;
1161 if (TREE_CODE (tmp) != SSA_NAME
1162 || SSA_NAME_IS_DEFAULT_DEF (tmp)
1163 || !POINTER_TYPE_P (TREE_TYPE (tmp))
1164 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
1165 return;
1167 assign = SSA_NAME_DEF_STMT (tmp);
1168 assign_bb = gimple_bb (assign);
1169 if (!single_pred_p (assign_bb))
1170 return;
1171 expr = get_ancestor_addr_info (assign, &obj, &offset);
1172 if (!expr)
1173 return;
1174 parm = TREE_OPERAND (expr, 0);
1175 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
1176 gcc_assert (index >= 0);
1178 cond_bb = single_pred (assign_bb);
1179 cond = last_stmt (cond_bb);
1180 if (!cond
1181 || gimple_code (cond) != GIMPLE_COND
1182 || gimple_cond_code (cond) != NE_EXPR
1183 || gimple_cond_lhs (cond) != parm
1184 || !integer_zerop (gimple_cond_rhs (cond)))
1185 return;
1187 phi_bb = gimple_bb (phi);
1188 for (i = 0; i < 2; i++)
1190 basic_block pred = EDGE_PRED (phi_bb, i)->src;
1191 if (pred != assign_bb && pred != cond_bb)
1192 return;
1195 bool type_p = false;
1196 if (param_type && POINTER_TYPE_P (param_type))
1197 type_p = !detect_type_change (obj, expr, TREE_TYPE (param_type),
1198 call, jfunc, offset);
1199 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1200 ipa_set_ancestor_jf (jfunc, offset, TREE_TYPE (obj), index,
1201 parm_ref_data_pass_through_p (&parms_ainfo[index],
1202 call, parm), type_p);
1205 /* Given OP which is passed as an actual argument to a called function,
1206 determine if it is possible to construct a KNOWN_TYPE jump function for it
1207 and if so, create one and store it to JFUNC.
1208 EXPECTED_TYPE represents a type the argument should be in */
1210 static void
1211 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
1212 gimple call, tree expected_type)
1214 HOST_WIDE_INT offset, size, max_size;
1215 tree base;
1217 if (!flag_devirtualize
1218 || TREE_CODE (op) != ADDR_EXPR
1219 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE
1220 /* Be sure expected_type is polymorphic. */
1221 || !expected_type
1222 || TREE_CODE (expected_type) != RECORD_TYPE
1223 || !TYPE_BINFO (expected_type)
1224 || !BINFO_VTABLE (TYPE_BINFO (expected_type)))
1225 return;
1227 op = TREE_OPERAND (op, 0);
1228 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
1229 if (!DECL_P (base)
1230 || max_size == -1
1231 || max_size != size
1232 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
1233 || is_global_var (base))
1234 return;
1236 if (detect_type_change (op, base, expected_type, call, jfunc, offset))
1237 return;
1239 ipa_set_jf_known_type (jfunc, offset, TREE_TYPE (base),
1240 expected_type);
1243 /* Inspect the given TYPE and return true iff it has the same structure (the
1244 same number of fields of the same types) as a C++ member pointer. If
1245 METHOD_PTR and DELTA are non-NULL, store the trees representing the
1246 corresponding fields there. */
1248 static bool
1249 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
1251 tree fld;
1253 if (TREE_CODE (type) != RECORD_TYPE)
1254 return false;
1256 fld = TYPE_FIELDS (type);
1257 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
1258 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE
1259 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1260 return false;
1262 if (method_ptr)
1263 *method_ptr = fld;
1265 fld = DECL_CHAIN (fld);
1266 if (!fld || INTEGRAL_TYPE_P (fld)
1267 || !host_integerp (DECL_FIELD_OFFSET (fld), 1))
1268 return false;
1269 if (delta)
1270 *delta = fld;
1272 if (DECL_CHAIN (fld))
1273 return false;
1275 return true;
1278 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
1279 return the rhs of its defining statement. Otherwise return RHS as it
1280 is. */
1282 static inline tree
1283 get_ssa_def_if_simple_copy (tree rhs)
1285 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
1287 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
1289 if (gimple_assign_single_p (def_stmt))
1290 rhs = gimple_assign_rhs1 (def_stmt);
1291 else
1292 break;
1294 return rhs;
1297 /* Simple linked list, describing known contents of an aggregate beforere
1298 call. */
1300 struct ipa_known_agg_contents_list
1302 /* Offset and size of the described part of the aggregate. */
1303 HOST_WIDE_INT offset, size;
1304 /* Known constant value or NULL if the contents is known to be unknown. */
1305 tree constant;
1306 /* Pointer to the next structure in the list. */
1307 struct ipa_known_agg_contents_list *next;
1310 /* Traverse statements from CALL backwards, scanning whether an aggregate given
1311 in ARG is filled in with constant values. ARG can either be an aggregate
1312 expression or a pointer to an aggregate. JFUNC is the jump function into
1313 which the constants are subsequently stored. */
1315 static void
1316 determine_known_aggregate_parts (gimple call, tree arg,
1317 struct ipa_jump_func *jfunc)
1319 struct ipa_known_agg_contents_list *list = NULL;
1320 int item_count = 0, const_count = 0;
1321 HOST_WIDE_INT arg_offset, arg_size;
1322 gimple_stmt_iterator gsi;
1323 tree arg_base;
1324 bool check_ref, by_ref;
1325 ao_ref r;
1327 /* The function operates in three stages. First, we prepare check_ref, r,
1328 arg_base and arg_offset based on what is actually passed as an actual
1329 argument. */
1331 if (POINTER_TYPE_P (TREE_TYPE (arg)))
1333 by_ref = true;
1334 if (TREE_CODE (arg) == SSA_NAME)
1336 tree type_size;
1337 if (!host_integerp (TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg))), 1))
1338 return;
1339 check_ref = true;
1340 arg_base = arg;
1341 arg_offset = 0;
1342 type_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (arg)));
1343 arg_size = tree_low_cst (type_size, 1);
1344 ao_ref_init_from_ptr_and_size (&r, arg_base, NULL_TREE);
1346 else if (TREE_CODE (arg) == ADDR_EXPR)
1348 HOST_WIDE_INT arg_max_size;
1350 arg = TREE_OPERAND (arg, 0);
1351 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1352 &arg_max_size);
1353 if (arg_max_size == -1
1354 || arg_max_size != arg_size
1355 || arg_offset < 0)
1356 return;
1357 if (DECL_P (arg_base))
1359 tree size;
1360 check_ref = false;
1361 size = build_int_cst (integer_type_node, arg_size);
1362 ao_ref_init_from_ptr_and_size (&r, arg_base, size);
1364 else
1365 return;
1367 else
1368 return;
1370 else
1372 HOST_WIDE_INT arg_max_size;
1374 gcc_checking_assert (AGGREGATE_TYPE_P (TREE_TYPE (arg)));
1376 by_ref = false;
1377 check_ref = false;
1378 arg_base = get_ref_base_and_extent (arg, &arg_offset, &arg_size,
1379 &arg_max_size);
1380 if (arg_max_size == -1
1381 || arg_max_size != arg_size
1382 || arg_offset < 0)
1383 return;
1385 ao_ref_init (&r, arg);
1388 /* Second stage walks back the BB, looks at individual statements and as long
1389 as it is confident of how the statements affect contents of the
1390 aggregates, it builds a sorted linked list of ipa_agg_jf_list structures
1391 describing it. */
1392 gsi = gsi_for_stmt (call);
1393 gsi_prev (&gsi);
1394 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
1396 struct ipa_known_agg_contents_list *n, **p;
1397 gimple stmt = gsi_stmt (gsi);
1398 HOST_WIDE_INT lhs_offset, lhs_size, lhs_max_size;
1399 tree lhs, rhs, lhs_base;
1400 bool partial_overlap;
1402 if (!stmt_may_clobber_ref_p_1 (stmt, &r))
1403 continue;
1404 if (!gimple_assign_single_p (stmt))
1405 break;
1407 lhs = gimple_assign_lhs (stmt);
1408 rhs = gimple_assign_rhs1 (stmt);
1409 if (!is_gimple_reg_type (rhs)
1410 || TREE_CODE (lhs) == BIT_FIELD_REF
1411 || contains_bitfld_component_ref_p (lhs))
1412 break;
1414 lhs_base = get_ref_base_and_extent (lhs, &lhs_offset, &lhs_size,
1415 &lhs_max_size);
1416 if (lhs_max_size == -1
1417 || lhs_max_size != lhs_size
1418 || (lhs_offset < arg_offset
1419 && lhs_offset + lhs_size > arg_offset)
1420 || (lhs_offset < arg_offset + arg_size
1421 && lhs_offset + lhs_size > arg_offset + arg_size))
1422 break;
1424 if (check_ref)
1426 if (TREE_CODE (lhs_base) != MEM_REF
1427 || TREE_OPERAND (lhs_base, 0) != arg_base
1428 || !integer_zerop (TREE_OPERAND (lhs_base, 1)))
1429 break;
1431 else if (lhs_base != arg_base)
1433 if (DECL_P (lhs_base))
1434 continue;
1435 else
1436 break;
1439 if (lhs_offset + lhs_size < arg_offset
1440 || lhs_offset >= (arg_offset + arg_size))
1441 continue;
1443 partial_overlap = false;
1444 p = &list;
1445 while (*p && (*p)->offset < lhs_offset)
1447 if ((*p)->offset + (*p)->size > lhs_offset)
1449 partial_overlap = true;
1450 break;
1452 p = &(*p)->next;
1454 if (partial_overlap)
1455 break;
1456 if (*p && (*p)->offset < lhs_offset + lhs_size)
1458 if ((*p)->offset == lhs_offset && (*p)->size == lhs_size)
1459 /* We already know this value is subsequently overwritten with
1460 something else. */
1461 continue;
1462 else
1463 /* Otherwise this is a partial overlap which we cannot
1464 represent. */
1465 break;
1468 rhs = get_ssa_def_if_simple_copy (rhs);
1469 n = XALLOCA (struct ipa_known_agg_contents_list);
1470 n->size = lhs_size;
1471 n->offset = lhs_offset;
1472 if (is_gimple_ip_invariant (rhs))
1474 n->constant = rhs;
1475 const_count++;
1477 else
1478 n->constant = NULL_TREE;
1479 n->next = *p;
1480 *p = n;
1482 item_count++;
1483 if (const_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)
1484 || item_count == 2 * PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS))
1485 break;
1488 /* Third stage just goes over the list and creates an appropriate vector of
1489 ipa_agg_jf_item structures out of it, of sourse only if there are
1490 any known constants to begin with. */
1492 if (const_count)
1494 jfunc->agg.by_ref = by_ref;
1495 vec_alloc (jfunc->agg.items, const_count);
1496 while (list)
1498 if (list->constant)
1500 struct ipa_agg_jf_item item;
1501 item.offset = list->offset - arg_offset;
1502 gcc_assert ((item.offset % BITS_PER_UNIT) == 0);
1503 item.value = unshare_expr_without_location (list->constant);
1504 jfunc->agg.items->quick_push (item);
1506 list = list->next;
1511 static tree
1512 ipa_get_callee_param_type (struct cgraph_edge *e, int i)
1514 int n;
1515 tree type = (e->callee
1516 ? TREE_TYPE (e->callee->decl)
1517 : gimple_call_fntype (e->call_stmt));
1518 tree t = TYPE_ARG_TYPES (type);
1520 for (n = 0; n < i; n++)
1522 if (!t)
1523 break;
1524 t = TREE_CHAIN (t);
1526 if (t)
1527 return TREE_VALUE (t);
1528 if (!e->callee)
1529 return NULL;
1530 t = DECL_ARGUMENTS (e->callee->decl);
1531 for (n = 0; n < i; n++)
1533 if (!t)
1534 return NULL;
1535 t = TREE_CHAIN (t);
1537 if (t)
1538 return TREE_TYPE (t);
1539 return NULL;
1542 /* Compute jump function for all arguments of callsite CS and insert the
1543 information in the jump_functions array in the ipa_edge_args corresponding
1544 to this callsite. */
1546 static void
1547 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1548 struct cgraph_edge *cs)
1550 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1551 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1552 gimple call = cs->call_stmt;
1553 int n, arg_num = gimple_call_num_args (call);
1555 if (arg_num == 0 || args->jump_functions)
1556 return;
1557 vec_safe_grow_cleared (args->jump_functions, arg_num);
1559 if (gimple_call_internal_p (call))
1560 return;
1561 if (ipa_func_spec_opts_forbid_analysis_p (cs->caller))
1562 return;
1564 for (n = 0; n < arg_num; n++)
1566 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, n);
1567 tree arg = gimple_call_arg (call, n);
1568 tree param_type = ipa_get_callee_param_type (cs, n);
1570 if (is_gimple_ip_invariant (arg))
1571 ipa_set_jf_constant (jfunc, arg, cs);
1572 else if (!is_gimple_reg_type (TREE_TYPE (arg))
1573 && TREE_CODE (arg) == PARM_DECL)
1575 int index = ipa_get_param_decl_index (info, arg);
1577 gcc_assert (index >=0);
1578 /* Aggregate passed by value, check for pass-through, otherwise we
1579 will attempt to fill in aggregate contents later in this
1580 for cycle. */
1581 if (parm_preserved_before_stmt_p (&parms_ainfo[index], call, arg))
1583 ipa_set_jf_simple_pass_through (jfunc, index, false, false);
1584 continue;
1587 else if (TREE_CODE (arg) == SSA_NAME)
1589 if (SSA_NAME_IS_DEFAULT_DEF (arg))
1591 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
1592 if (index >= 0)
1594 bool agg_p, type_p;
1595 agg_p = parm_ref_data_pass_through_p (&parms_ainfo[index],
1596 call, arg);
1597 if (param_type && POINTER_TYPE_P (param_type))
1598 type_p = !detect_type_change_ssa (arg, TREE_TYPE (param_type),
1599 call, jfunc);
1600 else
1601 type_p = false;
1602 if (type_p || jfunc->type == IPA_JF_UNKNOWN)
1603 ipa_set_jf_simple_pass_through (jfunc, index, agg_p,
1604 type_p);
1607 else
1609 gimple stmt = SSA_NAME_DEF_STMT (arg);
1610 if (is_gimple_assign (stmt))
1611 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
1612 call, stmt, arg, param_type);
1613 else if (gimple_code (stmt) == GIMPLE_PHI)
1614 compute_complex_ancestor_jump_func (info, parms_ainfo, jfunc,
1615 call, stmt, param_type);
1618 else
1619 compute_known_type_jump_func (arg, jfunc, call,
1620 param_type
1621 && POINTER_TYPE_P (param_type)
1622 ? TREE_TYPE (param_type)
1623 : NULL);
1625 if ((jfunc->type != IPA_JF_PASS_THROUGH
1626 || !ipa_get_jf_pass_through_agg_preserved (jfunc))
1627 && (jfunc->type != IPA_JF_ANCESTOR
1628 || !ipa_get_jf_ancestor_agg_preserved (jfunc))
1629 && (AGGREGATE_TYPE_P (TREE_TYPE (arg))
1630 || (POINTER_TYPE_P (TREE_TYPE (arg)))))
1631 determine_known_aggregate_parts (call, arg, jfunc);
1635 /* Compute jump functions for all edges - both direct and indirect - outgoing
1636 from NODE. Also count the actual arguments in the process. */
1638 static void
1639 ipa_compute_jump_functions (struct cgraph_node *node,
1640 struct param_analysis_info *parms_ainfo)
1642 struct cgraph_edge *cs;
1644 for (cs = node->callees; cs; cs = cs->next_callee)
1646 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1647 NULL);
1648 /* We do not need to bother analyzing calls to unknown
1649 functions unless they may become known during lto/whopr. */
1650 if (!callee->definition && !flag_lto)
1651 continue;
1652 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1655 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1656 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1659 /* If STMT looks like a statement loading a value from a member pointer formal
1660 parameter, return that parameter and store the offset of the field to
1661 *OFFSET_P, if it is non-NULL. Otherwise return NULL (but *OFFSET_P still
1662 might be clobbered). If USE_DELTA, then we look for a use of the delta
1663 field rather than the pfn. */
1665 static tree
1666 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta,
1667 HOST_WIDE_INT *offset_p)
1669 tree rhs, rec, ref_field, ref_offset, fld, ptr_field, delta_field;
1671 if (!gimple_assign_single_p (stmt))
1672 return NULL_TREE;
1674 rhs = gimple_assign_rhs1 (stmt);
1675 if (TREE_CODE (rhs) == COMPONENT_REF)
1677 ref_field = TREE_OPERAND (rhs, 1);
1678 rhs = TREE_OPERAND (rhs, 0);
1680 else
1681 ref_field = NULL_TREE;
1682 if (TREE_CODE (rhs) != MEM_REF)
1683 return NULL_TREE;
1684 rec = TREE_OPERAND (rhs, 0);
1685 if (TREE_CODE (rec) != ADDR_EXPR)
1686 return NULL_TREE;
1687 rec = TREE_OPERAND (rec, 0);
1688 if (TREE_CODE (rec) != PARM_DECL
1689 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1690 return NULL_TREE;
1691 ref_offset = TREE_OPERAND (rhs, 1);
1693 if (use_delta)
1694 fld = delta_field;
1695 else
1696 fld = ptr_field;
1697 if (offset_p)
1698 *offset_p = int_bit_position (fld);
1700 if (ref_field)
1702 if (integer_nonzerop (ref_offset))
1703 return NULL_TREE;
1704 return ref_field == fld ? rec : NULL_TREE;
1706 else
1707 return tree_int_cst_equal (byte_position (fld), ref_offset) ? rec
1708 : NULL_TREE;
1711 /* Returns true iff T is an SSA_NAME defined by a statement. */
1713 static bool
1714 ipa_is_ssa_with_stmt_def (tree t)
1716 if (TREE_CODE (t) == SSA_NAME
1717 && !SSA_NAME_IS_DEFAULT_DEF (t))
1718 return true;
1719 else
1720 return false;
1723 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1724 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1725 indirect call graph edge. */
1727 static struct cgraph_edge *
1728 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1730 struct cgraph_edge *cs;
1732 cs = cgraph_edge (node, stmt);
1733 cs->indirect_info->param_index = param_index;
1734 cs->indirect_info->offset = 0;
1735 cs->indirect_info->polymorphic = 0;
1736 cs->indirect_info->agg_contents = 0;
1737 cs->indirect_info->member_ptr = 0;
1738 return cs;
1741 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1742 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1743 intermediate information about each formal parameter. Currently it checks
1744 whether the call calls a pointer that is a formal parameter and if so, the
1745 parameter is marked with the called flag and an indirect call graph edge
1746 describing the call is created. This is very simple for ordinary pointers
1747 represented in SSA but not-so-nice when it comes to member pointers. The
1748 ugly part of this function does nothing more than trying to match the
1749 pattern of such a call. An example of such a pattern is the gimple dump
1750 below, the call is on the last line:
1752 <bb 2>:
1753 f$__delta_5 = f.__delta;
1754 f$__pfn_24 = f.__pfn;
1757 <bb 2>:
1758 f$__delta_5 = MEM[(struct *)&f];
1759 f$__pfn_24 = MEM[(struct *)&f + 4B];
1761 and a few lines below:
1763 <bb 5>
1764 D.2496_3 = (int) f$__pfn_24;
1765 D.2497_4 = D.2496_3 & 1;
1766 if (D.2497_4 != 0)
1767 goto <bb 3>;
1768 else
1769 goto <bb 4>;
1771 <bb 6>:
1772 D.2500_7 = (unsigned int) f$__delta_5;
1773 D.2501_8 = &S + D.2500_7;
1774 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1775 D.2503_10 = *D.2502_9;
1776 D.2504_12 = f$__pfn_24 + -1;
1777 D.2505_13 = (unsigned int) D.2504_12;
1778 D.2506_14 = D.2503_10 + D.2505_13;
1779 D.2507_15 = *D.2506_14;
1780 iftmp.11_16 = (String:: *) D.2507_15;
1782 <bb 7>:
1783 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1784 D.2500_19 = (unsigned int) f$__delta_5;
1785 D.2508_20 = &S + D.2500_19;
1786 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1788 Such patterns are results of simple calls to a member pointer:
1790 int doprinting (int (MyString::* f)(int) const)
1792 MyString S ("somestring");
1794 return (S.*f)(4);
1797 Moreover, the function also looks for called pointers loaded from aggregates
1798 passed by value or reference. */
1800 static void
1801 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1802 struct ipa_node_params *info,
1803 struct param_analysis_info *parms_ainfo,
1804 gimple call, tree target)
1806 gimple def;
1807 tree n1, n2;
1808 gimple d1, d2;
1809 tree rec, rec2, cond;
1810 gimple branch;
1811 int index;
1812 basic_block bb, virt_bb, join;
1813 HOST_WIDE_INT offset;
1814 bool by_ref;
1816 if (SSA_NAME_IS_DEFAULT_DEF (target))
1818 tree var = SSA_NAME_VAR (target);
1819 index = ipa_get_param_decl_index (info, var);
1820 if (index >= 0)
1821 ipa_note_param_call (node, index, call);
1822 return;
1825 def = SSA_NAME_DEF_STMT (target);
1826 if (gimple_assign_single_p (def)
1827 && ipa_load_from_parm_agg_1 (info->descriptors, parms_ainfo, def,
1828 gimple_assign_rhs1 (def), &index, &offset,
1829 &by_ref))
1831 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1832 cs->indirect_info->offset = offset;
1833 cs->indirect_info->agg_contents = 1;
1834 cs->indirect_info->by_ref = by_ref;
1835 return;
1838 /* Now we need to try to match the complex pattern of calling a member
1839 pointer. */
1840 if (gimple_code (def) != GIMPLE_PHI
1841 || gimple_phi_num_args (def) != 2
1842 || !POINTER_TYPE_P (TREE_TYPE (target))
1843 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1844 return;
1846 /* First, we need to check whether one of these is a load from a member
1847 pointer that is a parameter to this function. */
1848 n1 = PHI_ARG_DEF (def, 0);
1849 n2 = PHI_ARG_DEF (def, 1);
1850 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1851 return;
1852 d1 = SSA_NAME_DEF_STMT (n1);
1853 d2 = SSA_NAME_DEF_STMT (n2);
1855 join = gimple_bb (def);
1856 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false, &offset)))
1858 if (ipa_get_stmt_member_ptr_load_param (d2, false, NULL))
1859 return;
1861 bb = EDGE_PRED (join, 0)->src;
1862 virt_bb = gimple_bb (d2);
1864 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false, &offset)))
1866 bb = EDGE_PRED (join, 1)->src;
1867 virt_bb = gimple_bb (d1);
1869 else
1870 return;
1872 /* Second, we need to check that the basic blocks are laid out in the way
1873 corresponding to the pattern. */
1875 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1876 || single_pred (virt_bb) != bb
1877 || single_succ (virt_bb) != join)
1878 return;
1880 /* Third, let's see that the branching is done depending on the least
1881 significant bit of the pfn. */
1883 branch = last_stmt (bb);
1884 if (!branch || gimple_code (branch) != GIMPLE_COND)
1885 return;
1887 if ((gimple_cond_code (branch) != NE_EXPR
1888 && gimple_cond_code (branch) != EQ_EXPR)
1889 || !integer_zerop (gimple_cond_rhs (branch)))
1890 return;
1892 cond = gimple_cond_lhs (branch);
1893 if (!ipa_is_ssa_with_stmt_def (cond))
1894 return;
1896 def = SSA_NAME_DEF_STMT (cond);
1897 if (!is_gimple_assign (def)
1898 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1899 || !integer_onep (gimple_assign_rhs2 (def)))
1900 return;
1902 cond = gimple_assign_rhs1 (def);
1903 if (!ipa_is_ssa_with_stmt_def (cond))
1904 return;
1906 def = SSA_NAME_DEF_STMT (cond);
1908 if (is_gimple_assign (def)
1909 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1911 cond = gimple_assign_rhs1 (def);
1912 if (!ipa_is_ssa_with_stmt_def (cond))
1913 return;
1914 def = SSA_NAME_DEF_STMT (cond);
1917 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1918 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1919 == ptrmemfunc_vbit_in_delta),
1920 NULL);
1921 if (rec != rec2)
1922 return;
1924 index = ipa_get_param_decl_index (info, rec);
1925 if (index >= 0
1926 && parm_preserved_before_stmt_p (&parms_ainfo[index], call, rec))
1928 struct cgraph_edge *cs = ipa_note_param_call (node, index, call);
1929 cs->indirect_info->offset = offset;
1930 cs->indirect_info->agg_contents = 1;
1931 cs->indirect_info->member_ptr = 1;
1934 return;
1937 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1938 object referenced in the expression is a formal parameter of the caller
1939 (described by INFO), create a call note for the statement. */
1941 static void
1942 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1943 struct ipa_node_params *info, gimple call,
1944 tree target)
1946 struct cgraph_edge *cs;
1947 struct cgraph_indirect_call_info *ii;
1948 struct ipa_jump_func jfunc;
1949 tree obj = OBJ_TYPE_REF_OBJECT (target);
1950 int index;
1951 HOST_WIDE_INT anc_offset;
1953 if (!flag_devirtualize)
1954 return;
1956 if (TREE_CODE (obj) != SSA_NAME)
1957 return;
1959 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1961 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1962 return;
1964 anc_offset = 0;
1965 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1966 gcc_assert (index >= 0);
1967 if (detect_type_change_ssa (obj, obj_type_ref_class (target),
1968 call, &jfunc))
1969 return;
1971 else
1973 gimple stmt = SSA_NAME_DEF_STMT (obj);
1974 tree expr;
1976 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1977 if (!expr)
1978 return;
1979 index = ipa_get_param_decl_index (info,
1980 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1981 gcc_assert (index >= 0);
1982 if (detect_type_change (obj, expr, obj_type_ref_class (target),
1983 call, &jfunc, anc_offset))
1984 return;
1987 cs = ipa_note_param_call (node, index, call);
1988 ii = cs->indirect_info;
1989 ii->offset = anc_offset;
1990 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1991 ii->otr_type = obj_type_ref_class (target);
1992 ii->polymorphic = 1;
1995 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1996 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1997 containing intermediate information about each formal parameter. */
1999 static void
2000 ipa_analyze_call_uses (struct cgraph_node *node,
2001 struct ipa_node_params *info,
2002 struct param_analysis_info *parms_ainfo, gimple call)
2004 tree target = gimple_call_fn (call);
2006 if (!target)
2007 return;
2008 if (TREE_CODE (target) == SSA_NAME)
2009 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
2010 else if (virtual_method_call_p (target))
2011 ipa_analyze_virtual_call_uses (node, info, call, target);
2015 /* Analyze the call statement STMT with respect to formal parameters (described
2016 in INFO) of caller given by NODE. Currently it only checks whether formal
2017 parameters are called. PARMS_AINFO is a pointer to a vector containing
2018 intermediate information about each formal parameter. */
2020 static void
2021 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
2022 struct param_analysis_info *parms_ainfo, gimple stmt)
2024 if (is_gimple_call (stmt))
2025 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
2028 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
2029 If OP is a parameter declaration, mark it as used in the info structure
2030 passed in DATA. */
2032 static bool
2033 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
2034 tree op, void *data)
2036 struct ipa_node_params *info = (struct ipa_node_params *) data;
2038 op = get_base_address (op);
2039 if (op
2040 && TREE_CODE (op) == PARM_DECL)
2042 int index = ipa_get_param_decl_index (info, op);
2043 gcc_assert (index >= 0);
2044 ipa_set_param_used (info, index, true);
2047 return false;
2050 /* Scan the function body of NODE and inspect the uses of formal parameters.
2051 Store the findings in various structures of the associated ipa_node_params
2052 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
2053 vector containing intermediate information about each formal parameter. */
2055 static void
2056 ipa_analyze_params_uses (struct cgraph_node *node,
2057 struct param_analysis_info *parms_ainfo)
2059 tree decl = node->decl;
2060 basic_block bb;
2061 struct function *func;
2062 gimple_stmt_iterator gsi;
2063 struct ipa_node_params *info = IPA_NODE_REF (node);
2064 int i;
2066 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
2067 return;
2069 info->uses_analysis_done = 1;
2070 if (ipa_func_spec_opts_forbid_analysis_p (node))
2072 for (i = 0; i < ipa_get_param_count (info); i++)
2074 ipa_set_param_used (info, i, true);
2075 ipa_set_controlled_uses (info, i, IPA_UNDESCRIBED_USE);
2077 return;
2080 for (i = 0; i < ipa_get_param_count (info); i++)
2082 tree parm = ipa_get_param (info, i);
2083 int controlled_uses = 0;
2085 /* For SSA regs see if parameter is used. For non-SSA we compute
2086 the flag during modification analysis. */
2087 if (is_gimple_reg (parm))
2089 tree ddef = ssa_default_def (DECL_STRUCT_FUNCTION (node->decl),
2090 parm);
2091 if (ddef && !has_zero_uses (ddef))
2093 imm_use_iterator imm_iter;
2094 use_operand_p use_p;
2096 ipa_set_param_used (info, i, true);
2097 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, ddef)
2098 if (!is_gimple_call (USE_STMT (use_p)))
2100 controlled_uses = IPA_UNDESCRIBED_USE;
2101 break;
2103 else
2104 controlled_uses++;
2106 else
2107 controlled_uses = 0;
2109 else
2110 controlled_uses = IPA_UNDESCRIBED_USE;
2111 ipa_set_controlled_uses (info, i, controlled_uses);
2114 func = DECL_STRUCT_FUNCTION (decl);
2115 FOR_EACH_BB_FN (bb, func)
2117 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2119 gimple stmt = gsi_stmt (gsi);
2121 if (is_gimple_debug (stmt))
2122 continue;
2124 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
2125 walk_stmt_load_store_addr_ops (stmt, info,
2126 visit_ref_for_mod_analysis,
2127 visit_ref_for_mod_analysis,
2128 visit_ref_for_mod_analysis);
2130 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2131 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
2132 visit_ref_for_mod_analysis,
2133 visit_ref_for_mod_analysis,
2134 visit_ref_for_mod_analysis);
2138 /* Free stuff in PARMS_AINFO, assume there are PARAM_COUNT parameters. */
2140 static void
2141 free_parms_ainfo (struct param_analysis_info *parms_ainfo, int param_count)
2143 int i;
2145 for (i = 0; i < param_count; i++)
2147 if (parms_ainfo[i].parm_visited_statements)
2148 BITMAP_FREE (parms_ainfo[i].parm_visited_statements);
2149 if (parms_ainfo[i].pt_visited_statements)
2150 BITMAP_FREE (parms_ainfo[i].pt_visited_statements);
2154 /* Initialize the array describing properties of of formal parameters
2155 of NODE, analyze their uses and compute jump functions associated
2156 with actual arguments of calls from within NODE. */
2158 void
2159 ipa_analyze_node (struct cgraph_node *node)
2161 struct ipa_node_params *info;
2162 struct param_analysis_info *parms_ainfo;
2163 int param_count;
2165 ipa_check_create_node_params ();
2166 ipa_check_create_edge_args ();
2167 info = IPA_NODE_REF (node);
2168 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2169 ipa_initialize_node_params (node);
2171 param_count = ipa_get_param_count (info);
2172 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
2173 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
2175 ipa_analyze_params_uses (node, parms_ainfo);
2176 ipa_compute_jump_functions (node, parms_ainfo);
2178 free_parms_ainfo (parms_ainfo, param_count);
2179 pop_cfun ();
2182 /* Given a statement CALL which must be a GIMPLE_CALL calling an OBJ_TYPE_REF
2183 attempt a type-based devirtualization. If successful, return the
2184 target function declaration, otherwise return NULL. */
2186 tree
2187 ipa_intraprocedural_devirtualization (gimple call)
2189 tree binfo, token, fndecl;
2190 struct ipa_jump_func jfunc;
2191 tree otr = gimple_call_fn (call);
2193 jfunc.type = IPA_JF_UNKNOWN;
2194 compute_known_type_jump_func (OBJ_TYPE_REF_OBJECT (otr), &jfunc,
2195 call, obj_type_ref_class (otr));
2196 if (jfunc.type != IPA_JF_KNOWN_TYPE)
2197 return NULL_TREE;
2198 binfo = ipa_binfo_from_known_type_jfunc (&jfunc);
2199 if (!binfo)
2200 return NULL_TREE;
2201 token = OBJ_TYPE_REF_TOKEN (otr);
2202 fndecl = gimple_get_virt_method_for_binfo (tree_low_cst (token, 1),
2203 binfo);
2204 #ifdef ENABLE_CHECKING
2205 if (fndecl)
2206 gcc_assert (possible_polymorphic_call_target_p
2207 (otr, cgraph_get_node (fndecl)));
2208 #endif
2209 return fndecl;
2212 /* Update the jump function DST when the call graph edge corresponding to SRC is
2213 is being inlined, knowing that DST is of type ancestor and src of known
2214 type. */
2216 static void
2217 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
2218 struct ipa_jump_func *dst)
2220 HOST_WIDE_INT combined_offset;
2221 tree combined_type;
2223 if (!ipa_get_jf_ancestor_type_preserved (dst))
2225 dst->type = IPA_JF_UNKNOWN;
2226 return;
2229 combined_offset = ipa_get_jf_known_type_offset (src)
2230 + ipa_get_jf_ancestor_offset (dst);
2231 combined_type = ipa_get_jf_ancestor_type (dst);
2233 ipa_set_jf_known_type (dst, combined_offset,
2234 ipa_get_jf_known_type_base_type (src),
2235 combined_type);
2238 /* Update the jump functions associated with call graph edge E when the call
2239 graph edge CS is being inlined, assuming that E->caller is already (possibly
2240 indirectly) inlined into CS->callee and that E has not been inlined. */
2242 static void
2243 update_jump_functions_after_inlining (struct cgraph_edge *cs,
2244 struct cgraph_edge *e)
2246 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
2247 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2248 int count = ipa_get_cs_argument_count (args);
2249 int i;
2251 for (i = 0; i < count; i++)
2253 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
2255 if (dst->type == IPA_JF_ANCESTOR)
2257 struct ipa_jump_func *src;
2258 int dst_fid = dst->value.ancestor.formal_id;
2260 /* Variable number of arguments can cause havoc if we try to access
2261 one that does not exist in the inlined edge. So make sure we
2262 don't. */
2263 if (dst_fid >= ipa_get_cs_argument_count (top))
2265 dst->type = IPA_JF_UNKNOWN;
2266 continue;
2269 src = ipa_get_ith_jump_func (top, dst_fid);
2271 if (src->agg.items
2272 && (dst->value.ancestor.agg_preserved || !src->agg.by_ref))
2274 struct ipa_agg_jf_item *item;
2275 int j;
2277 /* Currently we do not produce clobber aggregate jump functions,
2278 replace with merging when we do. */
2279 gcc_assert (!dst->agg.items);
2281 dst->agg.items = vec_safe_copy (src->agg.items);
2282 dst->agg.by_ref = src->agg.by_ref;
2283 FOR_EACH_VEC_SAFE_ELT (dst->agg.items, j, item)
2284 item->offset -= dst->value.ancestor.offset;
2287 if (src->type == IPA_JF_KNOWN_TYPE)
2288 combine_known_type_and_ancestor_jfs (src, dst);
2289 else if (src->type == IPA_JF_PASS_THROUGH
2290 && src->value.pass_through.operation == NOP_EXPR)
2292 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
2293 dst->value.ancestor.agg_preserved &=
2294 src->value.pass_through.agg_preserved;
2295 dst->value.ancestor.type_preserved &=
2296 src->value.pass_through.type_preserved;
2298 else if (src->type == IPA_JF_ANCESTOR)
2300 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
2301 dst->value.ancestor.offset += src->value.ancestor.offset;
2302 dst->value.ancestor.agg_preserved &=
2303 src->value.ancestor.agg_preserved;
2304 dst->value.ancestor.type_preserved &=
2305 src->value.ancestor.type_preserved;
2307 else
2308 dst->type = IPA_JF_UNKNOWN;
2310 else if (dst->type == IPA_JF_PASS_THROUGH)
2312 struct ipa_jump_func *src;
2313 /* We must check range due to calls with variable number of arguments
2314 and we cannot combine jump functions with operations. */
2315 if (dst->value.pass_through.operation == NOP_EXPR
2316 && (dst->value.pass_through.formal_id
2317 < ipa_get_cs_argument_count (top)))
2319 int dst_fid = dst->value.pass_through.formal_id;
2320 src = ipa_get_ith_jump_func (top, dst_fid);
2321 bool dst_agg_p = ipa_get_jf_pass_through_agg_preserved (dst);
2323 switch (src->type)
2325 case IPA_JF_UNKNOWN:
2326 dst->type = IPA_JF_UNKNOWN;
2327 break;
2328 case IPA_JF_KNOWN_TYPE:
2329 ipa_set_jf_known_type (dst,
2330 ipa_get_jf_known_type_offset (src),
2331 ipa_get_jf_known_type_base_type (src),
2332 ipa_get_jf_known_type_base_type (src));
2333 break;
2334 case IPA_JF_CONST:
2335 ipa_set_jf_cst_copy (dst, src);
2336 break;
2338 case IPA_JF_PASS_THROUGH:
2340 int formal_id = ipa_get_jf_pass_through_formal_id (src);
2341 enum tree_code operation;
2342 operation = ipa_get_jf_pass_through_operation (src);
2344 if (operation == NOP_EXPR)
2346 bool agg_p, type_p;
2347 agg_p = dst_agg_p
2348 && ipa_get_jf_pass_through_agg_preserved (src);
2349 type_p = ipa_get_jf_pass_through_type_preserved (src)
2350 && ipa_get_jf_pass_through_type_preserved (dst);
2351 ipa_set_jf_simple_pass_through (dst, formal_id,
2352 agg_p, type_p);
2354 else
2356 tree operand = ipa_get_jf_pass_through_operand (src);
2357 ipa_set_jf_arith_pass_through (dst, formal_id, operand,
2358 operation);
2360 break;
2362 case IPA_JF_ANCESTOR:
2364 bool agg_p, type_p;
2365 agg_p = dst_agg_p
2366 && ipa_get_jf_ancestor_agg_preserved (src);
2367 type_p = ipa_get_jf_ancestor_type_preserved (src)
2368 && ipa_get_jf_pass_through_type_preserved (dst);
2369 ipa_set_ancestor_jf (dst,
2370 ipa_get_jf_ancestor_offset (src),
2371 ipa_get_jf_ancestor_type (src),
2372 ipa_get_jf_ancestor_formal_id (src),
2373 agg_p, type_p);
2374 break;
2376 default:
2377 gcc_unreachable ();
2380 if (src->agg.items
2381 && (dst_agg_p || !src->agg.by_ref))
2383 /* Currently we do not produce clobber aggregate jump
2384 functions, replace with merging when we do. */
2385 gcc_assert (!dst->agg.items);
2387 dst->agg.by_ref = src->agg.by_ref;
2388 dst->agg.items = vec_safe_copy (src->agg.items);
2391 else
2392 dst->type = IPA_JF_UNKNOWN;
2397 /* If TARGET is an addr_expr of a function declaration, make it the destination
2398 of an indirect edge IE and return the edge. Otherwise, return NULL. */
2400 struct cgraph_edge *
2401 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
2403 struct cgraph_node *callee;
2404 struct inline_edge_summary *es = inline_edge_summary (ie);
2405 bool unreachable = false;
2407 if (TREE_CODE (target) == ADDR_EXPR)
2408 target = TREE_OPERAND (target, 0);
2409 if (TREE_CODE (target) != FUNCTION_DECL)
2411 target = canonicalize_constructor_val (target, NULL);
2412 if (!target || TREE_CODE (target) != FUNCTION_DECL)
2414 if (ie->indirect_info->member_ptr)
2415 /* Member pointer call that goes through a VMT lookup. */
2416 return NULL;
2418 if (dump_file)
2419 fprintf (dump_file, "ipa-prop: Discovered direct call to non-function"
2420 " in %s/%i, making it unreachable.\n",
2421 cgraph_node_name (ie->caller), ie->caller->order);
2422 target = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
2423 callee = cgraph_get_create_node (target);
2424 unreachable = true;
2426 else
2427 callee = cgraph_get_node (target);
2429 else
2430 callee = cgraph_get_node (target);
2432 /* Because may-edges are not explicitely represented and vtable may be external,
2433 we may create the first reference to the object in the unit. */
2434 if (!callee || callee->global.inlined_to)
2437 /* We are better to ensure we can refer to it.
2438 In the case of static functions we are out of luck, since we already
2439 removed its body. In the case of public functions we may or may
2440 not introduce the reference. */
2441 if (!canonicalize_constructor_val (target, NULL)
2442 || !TREE_PUBLIC (target))
2444 if (dump_file)
2445 fprintf (dump_file, "ipa-prop: Discovered call to a known target "
2446 "(%s/%i -> %s/%i) but can not refer to it. Giving up.\n",
2447 xstrdup (cgraph_node_name (ie->caller)),
2448 ie->caller->order,
2449 xstrdup (cgraph_node_name (ie->callee)),
2450 ie->callee->order);
2451 return NULL;
2453 callee = cgraph_get_create_real_symbol_node (target);
2455 ipa_check_create_node_params ();
2457 /* We can not make edges to inline clones. It is bug that someone removed
2458 the cgraph node too early. */
2459 gcc_assert (!callee->global.inlined_to);
2461 if (dump_file && !unreachable)
2463 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
2464 "(%s/%i -> %s/%i), for stmt ",
2465 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
2466 xstrdup (cgraph_node_name (ie->caller)),
2467 ie->caller->order,
2468 xstrdup (cgraph_node_name (callee)),
2469 callee->order);
2470 if (ie->call_stmt)
2471 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
2472 else
2473 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
2475 ie = cgraph_make_edge_direct (ie, callee);
2476 es = inline_edge_summary (ie);
2477 es->call_stmt_size -= (eni_size_weights.indirect_call_cost
2478 - eni_size_weights.call_cost);
2479 es->call_stmt_time -= (eni_time_weights.indirect_call_cost
2480 - eni_time_weights.call_cost);
2482 return ie;
2485 /* Retrieve value from aggregate jump function AGG for the given OFFSET or
2486 return NULL if there is not any. BY_REF specifies whether the value has to
2487 be passed by reference or by value. */
2489 tree
2490 ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *agg,
2491 HOST_WIDE_INT offset, bool by_ref)
2493 struct ipa_agg_jf_item *item;
2494 int i;
2496 if (by_ref != agg->by_ref)
2497 return NULL;
2499 FOR_EACH_VEC_SAFE_ELT (agg->items, i, item)
2500 if (item->offset == offset)
2502 /* Currently we do not have clobber values, return NULL for them once
2503 we do. */
2504 gcc_checking_assert (is_gimple_ip_invariant (item->value));
2505 return item->value;
2507 return NULL;
2510 /* Remove a reference to SYMBOL from the list of references of a node given by
2511 reference description RDESC. Return true if the reference has been
2512 successfully found and removed. */
2514 static bool
2515 remove_described_reference (symtab_node symbol, struct ipa_cst_ref_desc *rdesc)
2517 struct ipa_ref *to_del;
2518 struct cgraph_edge *origin;
2520 origin = rdesc->cs;
2521 if (!origin)
2522 return false;
2523 to_del = ipa_find_reference (origin->caller, symbol,
2524 origin->call_stmt, origin->lto_stmt_uid);
2525 if (!to_del)
2526 return false;
2528 ipa_remove_reference (to_del);
2529 if (dump_file)
2530 fprintf (dump_file, "ipa-prop: Removed a reference from %s/%i to %s.\n",
2531 xstrdup (cgraph_node_name (origin->caller)),
2532 origin->caller->order, xstrdup (symtab_node_name (symbol)));
2533 return true;
2536 /* If JFUNC has a reference description with refcount different from
2537 IPA_UNDESCRIBED_USE, return the reference description, otherwise return
2538 NULL. JFUNC must be a constant jump function. */
2540 static struct ipa_cst_ref_desc *
2541 jfunc_rdesc_usable (struct ipa_jump_func *jfunc)
2543 struct ipa_cst_ref_desc *rdesc = ipa_get_jf_constant_rdesc (jfunc);
2544 if (rdesc && rdesc->refcount != IPA_UNDESCRIBED_USE)
2545 return rdesc;
2546 else
2547 return NULL;
2550 /* If the value of constant jump function JFUNC is an address of a function
2551 declaration, return the associated call graph node. Otherwise return
2552 NULL. */
2554 static cgraph_node *
2555 cgraph_node_for_jfunc (struct ipa_jump_func *jfunc)
2557 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
2558 tree cst = ipa_get_jf_constant (jfunc);
2559 if (TREE_CODE (cst) != ADDR_EXPR
2560 || TREE_CODE (TREE_OPERAND (cst, 0)) != FUNCTION_DECL)
2561 return NULL;
2563 return cgraph_get_node (TREE_OPERAND (cst, 0));
2567 /* If JFUNC is a constant jump function with a usable rdesc, decrement its
2568 refcount and if it hits zero, remove reference to SYMBOL from the caller of
2569 the edge specified in the rdesc. Return false if either the symbol or the
2570 reference could not be found, otherwise return true. */
2572 static bool
2573 try_decrement_rdesc_refcount (struct ipa_jump_func *jfunc)
2575 struct ipa_cst_ref_desc *rdesc;
2576 if (jfunc->type == IPA_JF_CONST
2577 && (rdesc = jfunc_rdesc_usable (jfunc))
2578 && --rdesc->refcount == 0)
2580 symtab_node symbol = cgraph_node_for_jfunc (jfunc);
2581 if (!symbol)
2582 return false;
2584 return remove_described_reference (symbol, rdesc);
2586 return true;
2589 /* Try to find a destination for indirect edge IE that corresponds to a simple
2590 call or a call of a member function pointer and where the destination is a
2591 pointer formal parameter described by jump function JFUNC. If it can be
2592 determined, return the newly direct edge, otherwise return NULL.
2593 NEW_ROOT_INFO is the node info that JFUNC lattices are relative to. */
2595 static struct cgraph_edge *
2596 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
2597 struct ipa_jump_func *jfunc,
2598 struct ipa_node_params *new_root_info)
2600 struct cgraph_edge *cs;
2601 tree target;
2602 bool agg_contents = ie->indirect_info->agg_contents;
2604 if (ie->indirect_info->agg_contents)
2605 target = ipa_find_agg_cst_for_param (&jfunc->agg,
2606 ie->indirect_info->offset,
2607 ie->indirect_info->by_ref);
2608 else
2609 target = ipa_value_from_jfunc (new_root_info, jfunc);
2610 if (!target)
2611 return NULL;
2612 cs = ipa_make_edge_direct_to_target (ie, target);
2614 if (cs && !agg_contents)
2616 bool ok;
2617 gcc_checking_assert (cs->callee
2618 && (cs != ie
2619 || jfunc->type != IPA_JF_CONST
2620 || !cgraph_node_for_jfunc (jfunc)
2621 || cs->callee == cgraph_node_for_jfunc (jfunc)));
2622 ok = try_decrement_rdesc_refcount (jfunc);
2623 gcc_checking_assert (ok);
2626 return cs;
2629 /* Try to find a destination for indirect edge IE that corresponds to a virtual
2630 call based on a formal parameter which is described by jump function JFUNC
2631 and if it can be determined, make it direct and return the direct edge.
2632 Otherwise, return NULL. NEW_ROOT_INFO is the node info that JFUNC lattices
2633 are relative to. */
2635 static struct cgraph_edge *
2636 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
2637 struct ipa_jump_func *jfunc,
2638 struct ipa_node_params *new_root_info)
2640 tree binfo, target;
2642 binfo = ipa_value_from_jfunc (new_root_info, jfunc);
2644 if (!binfo)
2645 return NULL;
2647 if (TREE_CODE (binfo) != TREE_BINFO)
2649 binfo = gimple_extract_devirt_binfo_from_cst
2650 (binfo, ie->indirect_info->otr_type);
2651 if (!binfo)
2652 return NULL;
2655 binfo = get_binfo_at_offset (binfo, ie->indirect_info->offset,
2656 ie->indirect_info->otr_type);
2657 if (binfo)
2658 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
2659 binfo);
2660 else
2661 return NULL;
2663 if (target)
2665 #ifdef ENABLE_CHECKING
2666 gcc_assert (possible_polymorphic_call_target_p
2667 (ie, cgraph_get_node (target)));
2668 #endif
2669 return ipa_make_edge_direct_to_target (ie, target);
2671 else
2672 return NULL;
2675 /* Update the param called notes associated with NODE when CS is being inlined,
2676 assuming NODE is (potentially indirectly) inlined into CS->callee.
2677 Moreover, if the callee is discovered to be constant, create a new cgraph
2678 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
2679 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
2681 static bool
2682 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
2683 struct cgraph_node *node,
2684 vec<cgraph_edge_p> *new_edges)
2686 struct ipa_edge_args *top;
2687 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
2688 struct ipa_node_params *new_root_info;
2689 bool res = false;
2691 ipa_check_create_edge_args ();
2692 top = IPA_EDGE_REF (cs);
2693 new_root_info = IPA_NODE_REF (cs->caller->global.inlined_to
2694 ? cs->caller->global.inlined_to
2695 : cs->caller);
2697 for (ie = node->indirect_calls; ie; ie = next_ie)
2699 struct cgraph_indirect_call_info *ici = ie->indirect_info;
2700 struct ipa_jump_func *jfunc;
2701 int param_index;
2703 next_ie = ie->next_callee;
2705 if (ici->param_index == -1)
2706 continue;
2708 /* We must check range due to calls with variable number of arguments: */
2709 if (ici->param_index >= ipa_get_cs_argument_count (top))
2711 ici->param_index = -1;
2712 continue;
2715 param_index = ici->param_index;
2716 jfunc = ipa_get_ith_jump_func (top, param_index);
2718 if (!flag_indirect_inlining)
2719 new_direct_edge = NULL;
2720 else if (ici->polymorphic)
2721 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc,
2722 new_root_info);
2723 else
2724 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc,
2725 new_root_info);
2726 /* If speculation was removed, then we need to do nothing. */
2727 if (new_direct_edge && new_direct_edge != ie)
2729 new_direct_edge->indirect_inlining_edge = 1;
2730 top = IPA_EDGE_REF (cs);
2731 res = true;
2733 else if (new_direct_edge)
2735 new_direct_edge->indirect_inlining_edge = 1;
2736 if (new_direct_edge->call_stmt)
2737 new_direct_edge->call_stmt_cannot_inline_p
2738 = !gimple_check_call_matching_types (
2739 new_direct_edge->call_stmt,
2740 new_direct_edge->callee->decl, false);
2741 if (new_edges)
2743 new_edges->safe_push (new_direct_edge);
2744 res = true;
2746 top = IPA_EDGE_REF (cs);
2748 else if (jfunc->type == IPA_JF_PASS_THROUGH
2749 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2751 if (ici->agg_contents
2752 && !ipa_get_jf_pass_through_agg_preserved (jfunc))
2753 ici->param_index = -1;
2754 else
2755 ici->param_index = ipa_get_jf_pass_through_formal_id (jfunc);
2757 else if (jfunc->type == IPA_JF_ANCESTOR)
2759 if (ici->agg_contents
2760 && !ipa_get_jf_ancestor_agg_preserved (jfunc))
2761 ici->param_index = -1;
2762 else
2764 ici->param_index = ipa_get_jf_ancestor_formal_id (jfunc);
2765 ici->offset += ipa_get_jf_ancestor_offset (jfunc);
2768 else
2769 /* Either we can find a destination for this edge now or never. */
2770 ici->param_index = -1;
2773 return res;
2776 /* Recursively traverse subtree of NODE (including node) made of inlined
2777 cgraph_edges when CS has been inlined and invoke
2778 update_indirect_edges_after_inlining on all nodes and
2779 update_jump_functions_after_inlining on all non-inlined edges that lead out
2780 of this subtree. Newly discovered indirect edges will be added to
2781 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
2782 created. */
2784 static bool
2785 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
2786 struct cgraph_node *node,
2787 vec<cgraph_edge_p> *new_edges)
2789 struct cgraph_edge *e;
2790 bool res;
2792 res = update_indirect_edges_after_inlining (cs, node, new_edges);
2794 for (e = node->callees; e; e = e->next_callee)
2795 if (!e->inline_failed)
2796 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
2797 else
2798 update_jump_functions_after_inlining (cs, e);
2799 for (e = node->indirect_calls; e; e = e->next_callee)
2800 update_jump_functions_after_inlining (cs, e);
2802 return res;
2805 /* Combine two controlled uses counts as done during inlining. */
2807 static int
2808 combine_controlled_uses_counters (int c, int d)
2810 if (c == IPA_UNDESCRIBED_USE || d == IPA_UNDESCRIBED_USE)
2811 return IPA_UNDESCRIBED_USE;
2812 else
2813 return c + d - 1;
2816 /* Propagate number of controlled users from CS->caleee to the new root of the
2817 tree of inlined nodes. */
2819 static void
2820 propagate_controlled_uses (struct cgraph_edge *cs)
2822 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
2823 struct cgraph_node *new_root = cs->caller->global.inlined_to
2824 ? cs->caller->global.inlined_to : cs->caller;
2825 struct ipa_node_params *new_root_info = IPA_NODE_REF (new_root);
2826 struct ipa_node_params *old_root_info = IPA_NODE_REF (cs->callee);
2827 int count, i;
2829 count = MIN (ipa_get_cs_argument_count (args),
2830 ipa_get_param_count (old_root_info));
2831 for (i = 0; i < count; i++)
2833 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2834 struct ipa_cst_ref_desc *rdesc;
2836 if (jf->type == IPA_JF_PASS_THROUGH)
2838 int src_idx, c, d;
2839 src_idx = ipa_get_jf_pass_through_formal_id (jf);
2840 c = ipa_get_controlled_uses (new_root_info, src_idx);
2841 d = ipa_get_controlled_uses (old_root_info, i);
2843 gcc_checking_assert (ipa_get_jf_pass_through_operation (jf)
2844 == NOP_EXPR || c == IPA_UNDESCRIBED_USE);
2845 c = combine_controlled_uses_counters (c, d);
2846 ipa_set_controlled_uses (new_root_info, src_idx, c);
2847 if (c == 0 && new_root_info->ipcp_orig_node)
2849 struct cgraph_node *n;
2850 struct ipa_ref *ref;
2851 tree t = new_root_info->known_vals[src_idx];
2853 if (t && TREE_CODE (t) == ADDR_EXPR
2854 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL
2855 && (n = cgraph_get_node (TREE_OPERAND (t, 0)))
2856 && (ref = ipa_find_reference (new_root,
2857 n, NULL, 0)))
2859 if (dump_file)
2860 fprintf (dump_file, "ipa-prop: Removing cloning-created "
2861 "reference from %s/%i to %s/%i.\n",
2862 xstrdup (cgraph_node_name (new_root)),
2863 new_root->order,
2864 xstrdup (cgraph_node_name (n)), n->order);
2865 ipa_remove_reference (ref);
2869 else if (jf->type == IPA_JF_CONST
2870 && (rdesc = jfunc_rdesc_usable (jf)))
2872 int d = ipa_get_controlled_uses (old_root_info, i);
2873 int c = rdesc->refcount;
2874 rdesc->refcount = combine_controlled_uses_counters (c, d);
2875 if (rdesc->refcount == 0)
2877 tree cst = ipa_get_jf_constant (jf);
2878 struct cgraph_node *n;
2879 gcc_checking_assert (TREE_CODE (cst) == ADDR_EXPR
2880 && TREE_CODE (TREE_OPERAND (cst, 0))
2881 == FUNCTION_DECL);
2882 n = cgraph_get_node (TREE_OPERAND (cst, 0));
2883 if (n)
2885 struct cgraph_node *clone;
2886 bool ok;
2887 ok = remove_described_reference (n, rdesc);
2888 gcc_checking_assert (ok);
2890 clone = cs->caller;
2891 while (clone->global.inlined_to
2892 && clone != rdesc->cs->caller
2893 && IPA_NODE_REF (clone)->ipcp_orig_node)
2895 struct ipa_ref *ref;
2896 ref = ipa_find_reference (clone,
2897 n, NULL, 0);
2898 if (ref)
2900 if (dump_file)
2901 fprintf (dump_file, "ipa-prop: Removing "
2902 "cloning-created reference "
2903 "from %s/%i to %s/%i.\n",
2904 xstrdup (cgraph_node_name (clone)),
2905 clone->order,
2906 xstrdup (cgraph_node_name (n)),
2907 n->order);
2908 ipa_remove_reference (ref);
2910 clone = clone->callers->caller;
2917 for (i = ipa_get_param_count (old_root_info);
2918 i < ipa_get_cs_argument_count (args);
2919 i++)
2921 struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i);
2923 if (jf->type == IPA_JF_CONST)
2925 struct ipa_cst_ref_desc *rdesc = jfunc_rdesc_usable (jf);
2926 if (rdesc)
2927 rdesc->refcount = IPA_UNDESCRIBED_USE;
2929 else if (jf->type == IPA_JF_PASS_THROUGH)
2930 ipa_set_controlled_uses (new_root_info,
2931 jf->value.pass_through.formal_id,
2932 IPA_UNDESCRIBED_USE);
2936 /* Update jump functions and call note functions on inlining the call site CS.
2937 CS is expected to lead to a node already cloned by
2938 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
2939 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
2940 created. */
2942 bool
2943 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
2944 vec<cgraph_edge_p> *new_edges)
2946 bool changed;
2947 /* Do nothing if the preparation phase has not been carried out yet
2948 (i.e. during early inlining). */
2949 if (!ipa_node_params_vector.exists ())
2950 return false;
2951 gcc_assert (ipa_edge_args_vector);
2953 propagate_controlled_uses (cs);
2954 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
2956 return changed;
2959 /* Frees all dynamically allocated structures that the argument info points
2960 to. */
2962 void
2963 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
2965 vec_free (args->jump_functions);
2966 memset (args, 0, sizeof (*args));
2969 /* Free all ipa_edge structures. */
2971 void
2972 ipa_free_all_edge_args (void)
2974 int i;
2975 struct ipa_edge_args *args;
2977 if (!ipa_edge_args_vector)
2978 return;
2980 FOR_EACH_VEC_ELT (*ipa_edge_args_vector, i, args)
2981 ipa_free_edge_args_substructures (args);
2983 vec_free (ipa_edge_args_vector);
2986 /* Frees all dynamically allocated structures that the param info points
2987 to. */
2989 void
2990 ipa_free_node_params_substructures (struct ipa_node_params *info)
2992 info->descriptors.release ();
2993 free (info->lattices);
2994 /* Lattice values and their sources are deallocated with their alocation
2995 pool. */
2996 info->known_vals.release ();
2997 memset (info, 0, sizeof (*info));
3000 /* Free all ipa_node_params structures. */
3002 void
3003 ipa_free_all_node_params (void)
3005 int i;
3006 struct ipa_node_params *info;
3008 FOR_EACH_VEC_ELT (ipa_node_params_vector, i, info)
3009 ipa_free_node_params_substructures (info);
3011 ipa_node_params_vector.release ();
3014 /* Set the aggregate replacements of NODE to be AGGVALS. */
3016 void
3017 ipa_set_node_agg_value_chain (struct cgraph_node *node,
3018 struct ipa_agg_replacement_value *aggvals)
3020 if (vec_safe_length (ipa_node_agg_replacements) <= (unsigned) cgraph_max_uid)
3021 vec_safe_grow_cleared (ipa_node_agg_replacements, cgraph_max_uid + 1);
3023 (*ipa_node_agg_replacements)[node->uid] = aggvals;
3026 /* Hook that is called by cgraph.c when an edge is removed. */
3028 static void
3029 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
3031 struct ipa_edge_args *args;
3033 /* During IPA-CP updating we can be called on not-yet analyzed clones. */
3034 if (vec_safe_length (ipa_edge_args_vector) <= (unsigned)cs->uid)
3035 return;
3037 args = IPA_EDGE_REF (cs);
3038 if (args->jump_functions)
3040 struct ipa_jump_func *jf;
3041 int i;
3042 FOR_EACH_VEC_ELT (*args->jump_functions, i, jf)
3044 struct ipa_cst_ref_desc *rdesc;
3045 try_decrement_rdesc_refcount (jf);
3046 if (jf->type == IPA_JF_CONST
3047 && (rdesc = ipa_get_jf_constant_rdesc (jf))
3048 && rdesc->cs == cs)
3049 rdesc->cs = NULL;
3053 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
3056 /* Hook that is called by cgraph.c when a node is removed. */
3058 static void
3059 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3061 /* During IPA-CP updating we can be called on not-yet analyze clones. */
3062 if (ipa_node_params_vector.length () > (unsigned)node->uid)
3063 ipa_free_node_params_substructures (IPA_NODE_REF (node));
3064 if (vec_safe_length (ipa_node_agg_replacements) > (unsigned)node->uid)
3065 (*ipa_node_agg_replacements)[(unsigned)node->uid] = NULL;
3068 /* Hook that is called by cgraph.c when an edge is duplicated. */
3070 static void
3071 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
3072 __attribute__((unused)) void *data)
3074 struct ipa_edge_args *old_args, *new_args;
3075 unsigned int i;
3077 ipa_check_create_edge_args ();
3079 old_args = IPA_EDGE_REF (src);
3080 new_args = IPA_EDGE_REF (dst);
3082 new_args->jump_functions = vec_safe_copy (old_args->jump_functions);
3084 for (i = 0; i < vec_safe_length (old_args->jump_functions); i++)
3086 struct ipa_jump_func *src_jf = ipa_get_ith_jump_func (old_args, i);
3087 struct ipa_jump_func *dst_jf = ipa_get_ith_jump_func (new_args, i);
3089 dst_jf->agg.items = vec_safe_copy (dst_jf->agg.items);
3091 if (src_jf->type == IPA_JF_CONST)
3093 struct ipa_cst_ref_desc *src_rdesc = jfunc_rdesc_usable (src_jf);
3095 if (!src_rdesc)
3096 dst_jf->value.constant.rdesc = NULL;
3097 else if (src->caller == dst->caller)
3099 struct ipa_ref *ref;
3100 symtab_node n = cgraph_node_for_jfunc (src_jf);
3101 gcc_checking_assert (n);
3102 ref = ipa_find_reference (src->caller, n,
3103 src->call_stmt, src->lto_stmt_uid);
3104 gcc_checking_assert (ref);
3105 ipa_clone_ref (ref, dst->caller, ref->stmt);
3107 gcc_checking_assert (ipa_refdesc_pool);
3108 struct ipa_cst_ref_desc *dst_rdesc
3109 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3110 dst_rdesc->cs = dst;
3111 dst_rdesc->refcount = src_rdesc->refcount;
3112 dst_rdesc->next_duplicate = NULL;
3113 dst_jf->value.constant.rdesc = dst_rdesc;
3115 else if (src_rdesc->cs == src)
3117 struct ipa_cst_ref_desc *dst_rdesc;
3118 gcc_checking_assert (ipa_refdesc_pool);
3119 dst_rdesc
3120 = (struct ipa_cst_ref_desc *) pool_alloc (ipa_refdesc_pool);
3121 dst_rdesc->cs = dst;
3122 dst_rdesc->refcount = src_rdesc->refcount;
3123 dst_rdesc->next_duplicate = src_rdesc->next_duplicate;
3124 src_rdesc->next_duplicate = dst_rdesc;
3125 dst_jf->value.constant.rdesc = dst_rdesc;
3127 else
3129 struct ipa_cst_ref_desc *dst_rdesc;
3130 /* This can happen during inlining, when a JFUNC can refer to a
3131 reference taken in a function up in the tree of inline clones.
3132 We need to find the duplicate that refers to our tree of
3133 inline clones. */
3135 gcc_assert (dst->caller->global.inlined_to);
3136 for (dst_rdesc = src_rdesc->next_duplicate;
3137 dst_rdesc;
3138 dst_rdesc = dst_rdesc->next_duplicate)
3140 struct cgraph_node *top;
3141 top = dst_rdesc->cs->caller->global.inlined_to
3142 ? dst_rdesc->cs->caller->global.inlined_to
3143 : dst_rdesc->cs->caller;
3144 if (dst->caller->global.inlined_to == top)
3145 break;
3147 gcc_assert (dst_rdesc);
3148 dst_jf->value.constant.rdesc = dst_rdesc;
3154 /* Hook that is called by cgraph.c when a node is duplicated. */
3156 static void
3157 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
3158 ATTRIBUTE_UNUSED void *data)
3160 struct ipa_node_params *old_info, *new_info;
3161 struct ipa_agg_replacement_value *old_av, *new_av;
3163 ipa_check_create_node_params ();
3164 old_info = IPA_NODE_REF (src);
3165 new_info = IPA_NODE_REF (dst);
3167 new_info->descriptors = old_info->descriptors.copy ();
3168 new_info->lattices = NULL;
3169 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
3171 new_info->uses_analysis_done = old_info->uses_analysis_done;
3172 new_info->node_enqueued = old_info->node_enqueued;
3174 old_av = ipa_get_agg_replacements_for_node (src);
3175 if (!old_av)
3176 return;
3178 new_av = NULL;
3179 while (old_av)
3181 struct ipa_agg_replacement_value *v;
3183 v = ggc_alloc_ipa_agg_replacement_value ();
3184 memcpy (v, old_av, sizeof (*v));
3185 v->next = new_av;
3186 new_av = v;
3187 old_av = old_av->next;
3189 ipa_set_node_agg_value_chain (dst, new_av);
3193 /* Analyze newly added function into callgraph. */
3195 static void
3196 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
3198 ipa_analyze_node (node);
3201 /* Register our cgraph hooks if they are not already there. */
3203 void
3204 ipa_register_cgraph_hooks (void)
3206 if (!edge_removal_hook_holder)
3207 edge_removal_hook_holder =
3208 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
3209 if (!node_removal_hook_holder)
3210 node_removal_hook_holder =
3211 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
3212 if (!edge_duplication_hook_holder)
3213 edge_duplication_hook_holder =
3214 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
3215 if (!node_duplication_hook_holder)
3216 node_duplication_hook_holder =
3217 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
3218 function_insertion_hook_holder =
3219 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
3222 /* Unregister our cgraph hooks if they are not already there. */
3224 static void
3225 ipa_unregister_cgraph_hooks (void)
3227 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
3228 edge_removal_hook_holder = NULL;
3229 cgraph_remove_node_removal_hook (node_removal_hook_holder);
3230 node_removal_hook_holder = NULL;
3231 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
3232 edge_duplication_hook_holder = NULL;
3233 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
3234 node_duplication_hook_holder = NULL;
3235 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
3236 function_insertion_hook_holder = NULL;
3239 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3240 longer needed after ipa-cp. */
3242 void
3243 ipa_free_all_structures_after_ipa_cp (void)
3245 if (!optimize)
3247 ipa_free_all_edge_args ();
3248 ipa_free_all_node_params ();
3249 free_alloc_pool (ipcp_sources_pool);
3250 free_alloc_pool (ipcp_values_pool);
3251 free_alloc_pool (ipcp_agg_lattice_pool);
3252 ipa_unregister_cgraph_hooks ();
3253 if (ipa_refdesc_pool)
3254 free_alloc_pool (ipa_refdesc_pool);
3258 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
3259 longer needed after indirect inlining. */
3261 void
3262 ipa_free_all_structures_after_iinln (void)
3264 ipa_free_all_edge_args ();
3265 ipa_free_all_node_params ();
3266 ipa_unregister_cgraph_hooks ();
3267 if (ipcp_sources_pool)
3268 free_alloc_pool (ipcp_sources_pool);
3269 if (ipcp_values_pool)
3270 free_alloc_pool (ipcp_values_pool);
3271 if (ipcp_agg_lattice_pool)
3272 free_alloc_pool (ipcp_agg_lattice_pool);
3273 if (ipa_refdesc_pool)
3274 free_alloc_pool (ipa_refdesc_pool);
3277 /* Print ipa_tree_map data structures of all functions in the
3278 callgraph to F. */
3280 void
3281 ipa_print_node_params (FILE *f, struct cgraph_node *node)
3283 int i, count;
3284 struct ipa_node_params *info;
3286 if (!node->definition)
3287 return;
3288 info = IPA_NODE_REF (node);
3289 fprintf (f, " function %s/%i parameter descriptors:\n",
3290 cgraph_node_name (node), node->order);
3291 count = ipa_get_param_count (info);
3292 for (i = 0; i < count; i++)
3294 int c;
3296 ipa_dump_param (f, info, i);
3297 if (ipa_is_param_used (info, i))
3298 fprintf (f, " used");
3299 c = ipa_get_controlled_uses (info, i);
3300 if (c == IPA_UNDESCRIBED_USE)
3301 fprintf (f, " undescribed_use");
3302 else
3303 fprintf (f, " controlled_uses=%i", c);
3304 fprintf (f, "\n");
3308 /* Print ipa_tree_map data structures of all functions in the
3309 callgraph to F. */
3311 void
3312 ipa_print_all_params (FILE * f)
3314 struct cgraph_node *node;
3316 fprintf (f, "\nFunction parameters:\n");
3317 FOR_EACH_FUNCTION (node)
3318 ipa_print_node_params (f, node);
3321 /* Return a heap allocated vector containing formal parameters of FNDECL. */
3323 vec<tree>
3324 ipa_get_vector_of_formal_parms (tree fndecl)
3326 vec<tree> args;
3327 int count;
3328 tree parm;
3330 gcc_assert (!flag_wpa);
3331 count = count_formal_params (fndecl);
3332 args.create (count);
3333 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
3334 args.quick_push (parm);
3336 return args;
3339 /* Return a heap allocated vector containing types of formal parameters of
3340 function type FNTYPE. */
3342 static inline vec<tree>
3343 get_vector_of_formal_parm_types (tree fntype)
3345 vec<tree> types;
3346 int count = 0;
3347 tree t;
3349 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3350 count++;
3352 types.create (count);
3353 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
3354 types.quick_push (TREE_VALUE (t));
3356 return types;
3359 /* Modify the function declaration FNDECL and its type according to the plan in
3360 ADJUSTMENTS. It also sets base fields of individual adjustments structures
3361 to reflect the actual parameters being modified which are determined by the
3362 base_index field. */
3364 void
3365 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
3366 const char *synth_parm_prefix)
3368 vec<tree> oparms, otypes;
3369 tree orig_type, new_type = NULL;
3370 tree old_arg_types, t, new_arg_types = NULL;
3371 tree parm, *link = &DECL_ARGUMENTS (fndecl);
3372 int i, len = adjustments.length ();
3373 tree new_reversed = NULL;
3374 bool care_for_types, last_parm_void;
3376 if (!synth_parm_prefix)
3377 synth_parm_prefix = "SYNTH";
3379 oparms = ipa_get_vector_of_formal_parms (fndecl);
3380 orig_type = TREE_TYPE (fndecl);
3381 old_arg_types = TYPE_ARG_TYPES (orig_type);
3383 /* The following test is an ugly hack, some functions simply don't have any
3384 arguments in their type. This is probably a bug but well... */
3385 care_for_types = (old_arg_types != NULL_TREE);
3386 if (care_for_types)
3388 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
3389 == void_type_node);
3390 otypes = get_vector_of_formal_parm_types (orig_type);
3391 if (last_parm_void)
3392 gcc_assert (oparms.length () + 1 == otypes.length ());
3393 else
3394 gcc_assert (oparms.length () == otypes.length ());
3396 else
3398 last_parm_void = false;
3399 otypes.create (0);
3402 for (i = 0; i < len; i++)
3404 struct ipa_parm_adjustment *adj;
3405 gcc_assert (link);
3407 adj = &adjustments[i];
3408 parm = oparms[adj->base_index];
3409 adj->base = parm;
3411 if (adj->copy_param)
3413 if (care_for_types)
3414 new_arg_types = tree_cons (NULL_TREE, otypes[adj->base_index],
3415 new_arg_types);
3416 *link = parm;
3417 link = &DECL_CHAIN (parm);
3419 else if (!adj->remove_param)
3421 tree new_parm;
3422 tree ptype;
3424 if (adj->by_ref)
3425 ptype = build_pointer_type (adj->type);
3426 else
3427 ptype = adj->type;
3429 if (care_for_types)
3430 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
3432 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
3433 ptype);
3434 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
3436 DECL_ARTIFICIAL (new_parm) = 1;
3437 DECL_ARG_TYPE (new_parm) = ptype;
3438 DECL_CONTEXT (new_parm) = fndecl;
3439 TREE_USED (new_parm) = 1;
3440 DECL_IGNORED_P (new_parm) = 1;
3441 layout_decl (new_parm, 0);
3443 adj->base = parm;
3444 adj->reduction = new_parm;
3446 *link = new_parm;
3448 link = &DECL_CHAIN (new_parm);
3452 *link = NULL_TREE;
3454 if (care_for_types)
3456 new_reversed = nreverse (new_arg_types);
3457 if (last_parm_void)
3459 if (new_reversed)
3460 TREE_CHAIN (new_arg_types) = void_list_node;
3461 else
3462 new_reversed = void_list_node;
3466 /* Use copy_node to preserve as much as possible from original type
3467 (debug info, attribute lists etc.)
3468 Exception is METHOD_TYPEs must have THIS argument.
3469 When we are asked to remove it, we need to build new FUNCTION_TYPE
3470 instead. */
3471 if (TREE_CODE (orig_type) != METHOD_TYPE
3472 || (adjustments[0].copy_param
3473 && adjustments[0].base_index == 0))
3475 new_type = build_distinct_type_copy (orig_type);
3476 TYPE_ARG_TYPES (new_type) = new_reversed;
3478 else
3480 new_type
3481 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
3482 new_reversed));
3483 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
3484 DECL_VINDEX (fndecl) = NULL_TREE;
3487 /* When signature changes, we need to clear builtin info. */
3488 if (DECL_BUILT_IN (fndecl))
3490 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
3491 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
3494 /* This is a new type, not a copy of an old type. Need to reassociate
3495 variants. We can handle everything except the main variant lazily. */
3496 t = TYPE_MAIN_VARIANT (orig_type);
3497 if (orig_type != t)
3499 TYPE_MAIN_VARIANT (new_type) = t;
3500 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
3501 TYPE_NEXT_VARIANT (t) = new_type;
3503 else
3505 TYPE_MAIN_VARIANT (new_type) = new_type;
3506 TYPE_NEXT_VARIANT (new_type) = NULL;
3509 TREE_TYPE (fndecl) = new_type;
3510 DECL_VIRTUAL_P (fndecl) = 0;
3511 otypes.release ();
3512 oparms.release ();
3515 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
3516 If this is a directly recursive call, CS must be NULL. Otherwise it must
3517 contain the corresponding call graph edge. */
3519 void
3520 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
3521 ipa_parm_adjustment_vec adjustments)
3523 struct cgraph_node *current_node = cgraph_get_node (current_function_decl);
3524 vec<tree> vargs;
3525 vec<tree, va_gc> **debug_args = NULL;
3526 gimple new_stmt;
3527 gimple_stmt_iterator gsi, prev_gsi;
3528 tree callee_decl;
3529 int i, len;
3531 len = adjustments.length ();
3532 vargs.create (len);
3533 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
3534 ipa_remove_stmt_references (current_node, stmt);
3536 gsi = gsi_for_stmt (stmt);
3537 prev_gsi = gsi;
3538 gsi_prev (&prev_gsi);
3539 for (i = 0; i < len; i++)
3541 struct ipa_parm_adjustment *adj;
3543 adj = &adjustments[i];
3545 if (adj->copy_param)
3547 tree arg = gimple_call_arg (stmt, adj->base_index);
3549 vargs.quick_push (arg);
3551 else if (!adj->remove_param)
3553 tree expr, base, off;
3554 location_t loc;
3555 unsigned int deref_align = 0;
3556 bool deref_base = false;
3558 /* We create a new parameter out of the value of the old one, we can
3559 do the following kind of transformations:
3561 - A scalar passed by reference is converted to a scalar passed by
3562 value. (adj->by_ref is false and the type of the original
3563 actual argument is a pointer to a scalar).
3565 - A part of an aggregate is passed instead of the whole aggregate.
3566 The part can be passed either by value or by reference, this is
3567 determined by value of adj->by_ref. Moreover, the code below
3568 handles both situations when the original aggregate is passed by
3569 value (its type is not a pointer) and when it is passed by
3570 reference (it is a pointer to an aggregate).
3572 When the new argument is passed by reference (adj->by_ref is true)
3573 it must be a part of an aggregate and therefore we form it by
3574 simply taking the address of a reference inside the original
3575 aggregate. */
3577 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
3578 base = gimple_call_arg (stmt, adj->base_index);
3579 loc = DECL_P (base) ? DECL_SOURCE_LOCATION (base)
3580 : EXPR_LOCATION (base);
3582 if (TREE_CODE (base) != ADDR_EXPR
3583 && POINTER_TYPE_P (TREE_TYPE (base)))
3584 off = build_int_cst (adj->alias_ptr_type,
3585 adj->offset / BITS_PER_UNIT);
3586 else
3588 HOST_WIDE_INT base_offset;
3589 tree prev_base;
3590 bool addrof;
3592 if (TREE_CODE (base) == ADDR_EXPR)
3594 base = TREE_OPERAND (base, 0);
3595 addrof = true;
3597 else
3598 addrof = false;
3599 prev_base = base;
3600 base = get_addr_base_and_unit_offset (base, &base_offset);
3601 /* Aggregate arguments can have non-invariant addresses. */
3602 if (!base)
3604 base = build_fold_addr_expr (prev_base);
3605 off = build_int_cst (adj->alias_ptr_type,
3606 adj->offset / BITS_PER_UNIT);
3608 else if (TREE_CODE (base) == MEM_REF)
3610 if (!addrof)
3612 deref_base = true;
3613 deref_align = TYPE_ALIGN (TREE_TYPE (base));
3615 off = build_int_cst (adj->alias_ptr_type,
3616 base_offset
3617 + adj->offset / BITS_PER_UNIT);
3618 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
3619 off);
3620 base = TREE_OPERAND (base, 0);
3622 else
3624 off = build_int_cst (adj->alias_ptr_type,
3625 base_offset
3626 + adj->offset / BITS_PER_UNIT);
3627 base = build_fold_addr_expr (base);
3631 if (!adj->by_ref)
3633 tree type = adj->type;
3634 unsigned int align;
3635 unsigned HOST_WIDE_INT misalign;
3637 if (deref_base)
3639 align = deref_align;
3640 misalign = 0;
3642 else
3644 get_pointer_alignment_1 (base, &align, &misalign);
3645 if (TYPE_ALIGN (type) > align)
3646 align = TYPE_ALIGN (type);
3648 misalign += (tree_to_double_int (off)
3649 .sext (TYPE_PRECISION (TREE_TYPE (off))).low
3650 * BITS_PER_UNIT);
3651 misalign = misalign & (align - 1);
3652 if (misalign != 0)
3653 align = (misalign & -misalign);
3654 if (align < TYPE_ALIGN (type))
3655 type = build_aligned_type (type, align);
3656 expr = fold_build2_loc (loc, MEM_REF, type, base, off);
3658 else
3660 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
3661 expr = build_fold_addr_expr (expr);
3664 expr = force_gimple_operand_gsi (&gsi, expr,
3665 adj->by_ref
3666 || is_gimple_reg_type (adj->type),
3667 NULL, true, GSI_SAME_STMT);
3668 vargs.quick_push (expr);
3670 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
3672 unsigned int ix;
3673 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
3674 gimple def_temp;
3676 arg = gimple_call_arg (stmt, adj->base_index);
3677 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
3679 if (!fold_convertible_p (TREE_TYPE (origin), arg))
3680 continue;
3681 arg = fold_convert_loc (gimple_location (stmt),
3682 TREE_TYPE (origin), arg);
3684 if (debug_args == NULL)
3685 debug_args = decl_debug_args_insert (callee_decl);
3686 for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl); ix += 2)
3687 if (ddecl == origin)
3689 ddecl = (**debug_args)[ix + 1];
3690 break;
3692 if (ddecl == NULL)
3694 ddecl = make_node (DEBUG_EXPR_DECL);
3695 DECL_ARTIFICIAL (ddecl) = 1;
3696 TREE_TYPE (ddecl) = TREE_TYPE (origin);
3697 DECL_MODE (ddecl) = DECL_MODE (origin);
3699 vec_safe_push (*debug_args, origin);
3700 vec_safe_push (*debug_args, ddecl);
3702 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg), stmt);
3703 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
3707 if (dump_file && (dump_flags & TDF_DETAILS))
3709 fprintf (dump_file, "replacing stmt:");
3710 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
3713 new_stmt = gimple_build_call_vec (callee_decl, vargs);
3714 vargs.release ();
3715 if (gimple_call_lhs (stmt))
3716 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3718 gimple_set_block (new_stmt, gimple_block (stmt));
3719 if (gimple_has_location (stmt))
3720 gimple_set_location (new_stmt, gimple_location (stmt));
3721 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3722 gimple_call_copy_flags (new_stmt, stmt);
3724 if (dump_file && (dump_flags & TDF_DETAILS))
3726 fprintf (dump_file, "with stmt:");
3727 print_gimple_stmt (dump_file, new_stmt, 0, 0);
3728 fprintf (dump_file, "\n");
3730 gsi_replace (&gsi, new_stmt, true);
3731 if (cs)
3732 cgraph_set_call_stmt (cs, new_stmt);
3735 ipa_record_stmt_references (current_node, gsi_stmt (gsi));
3736 gsi_prev (&gsi);
3738 while ((gsi_end_p (prev_gsi) && !gsi_end_p (gsi))
3739 || (!gsi_end_p (prev_gsi) && gsi_stmt (gsi) == gsi_stmt (prev_gsi)));
3741 update_ssa (TODO_update_ssa);
3742 free_dominance_info (CDI_DOMINATORS);
3745 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
3747 static bool
3748 index_in_adjustments_multiple_times_p (int base_index,
3749 ipa_parm_adjustment_vec adjustments)
3751 int i, len = adjustments.length ();
3752 bool one = false;
3754 for (i = 0; i < len; i++)
3756 struct ipa_parm_adjustment *adj;
3757 adj = &adjustments[i];
3759 if (adj->base_index == base_index)
3761 if (one)
3762 return true;
3763 else
3764 one = true;
3767 return false;
3771 /* Return adjustments that should have the same effect on function parameters
3772 and call arguments as if they were first changed according to adjustments in
3773 INNER and then by adjustments in OUTER. */
3775 ipa_parm_adjustment_vec
3776 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
3777 ipa_parm_adjustment_vec outer)
3779 int i, outlen = outer.length ();
3780 int inlen = inner.length ();
3781 int removals = 0;
3782 ipa_parm_adjustment_vec adjustments, tmp;
3784 tmp.create (inlen);
3785 for (i = 0; i < inlen; i++)
3787 struct ipa_parm_adjustment *n;
3788 n = &inner[i];
3790 if (n->remove_param)
3791 removals++;
3792 else
3793 tmp.quick_push (*n);
3796 adjustments.create (outlen + removals);
3797 for (i = 0; i < outlen; i++)
3799 struct ipa_parm_adjustment r;
3800 struct ipa_parm_adjustment *out = &outer[i];
3801 struct ipa_parm_adjustment *in = &tmp[out->base_index];
3803 memset (&r, 0, sizeof (r));
3804 gcc_assert (!in->remove_param);
3805 if (out->remove_param)
3807 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
3809 r.remove_param = true;
3810 adjustments.quick_push (r);
3812 continue;
3815 r.base_index = in->base_index;
3816 r.type = out->type;
3818 /* FIXME: Create nonlocal value too. */
3820 if (in->copy_param && out->copy_param)
3821 r.copy_param = true;
3822 else if (in->copy_param)
3823 r.offset = out->offset;
3824 else if (out->copy_param)
3825 r.offset = in->offset;
3826 else
3827 r.offset = in->offset + out->offset;
3828 adjustments.quick_push (r);
3831 for (i = 0; i < inlen; i++)
3833 struct ipa_parm_adjustment *n = &inner[i];
3835 if (n->remove_param)
3836 adjustments.quick_push (*n);
3839 tmp.release ();
3840 return adjustments;
3843 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
3844 friendly way, assuming they are meant to be applied to FNDECL. */
3846 void
3847 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
3848 tree fndecl)
3850 int i, len = adjustments.length ();
3851 bool first = true;
3852 vec<tree> parms = ipa_get_vector_of_formal_parms (fndecl);
3854 fprintf (file, "IPA param adjustments: ");
3855 for (i = 0; i < len; i++)
3857 struct ipa_parm_adjustment *adj;
3858 adj = &adjustments[i];
3860 if (!first)
3861 fprintf (file, " ");
3862 else
3863 first = false;
3865 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
3866 print_generic_expr (file, parms[adj->base_index], 0);
3867 if (adj->base)
3869 fprintf (file, ", base: ");
3870 print_generic_expr (file, adj->base, 0);
3872 if (adj->reduction)
3874 fprintf (file, ", reduction: ");
3875 print_generic_expr (file, adj->reduction, 0);
3877 if (adj->new_ssa_base)
3879 fprintf (file, ", new_ssa_base: ");
3880 print_generic_expr (file, adj->new_ssa_base, 0);
3883 if (adj->copy_param)
3884 fprintf (file, ", copy_param");
3885 else if (adj->remove_param)
3886 fprintf (file, ", remove_param");
3887 else
3888 fprintf (file, ", offset %li", (long) adj->offset);
3889 if (adj->by_ref)
3890 fprintf (file, ", by_ref");
3891 print_node_brief (file, ", type: ", adj->type, 0);
3892 fprintf (file, "\n");
3894 parms.release ();
3897 /* Dump the AV linked list. */
3899 void
3900 ipa_dump_agg_replacement_values (FILE *f, struct ipa_agg_replacement_value *av)
3902 bool comma = false;
3903 fprintf (f, " Aggregate replacements:");
3904 for (; av; av = av->next)
3906 fprintf (f, "%s %i[" HOST_WIDE_INT_PRINT_DEC "]=", comma ? "," : "",
3907 av->index, av->offset);
3908 print_generic_expr (f, av->value, 0);
3909 comma = true;
3911 fprintf (f, "\n");
3914 /* Stream out jump function JUMP_FUNC to OB. */
3916 static void
3917 ipa_write_jump_function (struct output_block *ob,
3918 struct ipa_jump_func *jump_func)
3920 struct ipa_agg_jf_item *item;
3921 struct bitpack_d bp;
3922 int i, count;
3924 streamer_write_uhwi (ob, jump_func->type);
3925 switch (jump_func->type)
3927 case IPA_JF_UNKNOWN:
3928 break;
3929 case IPA_JF_KNOWN_TYPE:
3930 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
3931 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
3932 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
3933 break;
3934 case IPA_JF_CONST:
3935 gcc_assert (
3936 EXPR_LOCATION (jump_func->value.constant.value) == UNKNOWN_LOCATION);
3937 stream_write_tree (ob, jump_func->value.constant.value, true);
3938 break;
3939 case IPA_JF_PASS_THROUGH:
3940 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
3941 if (jump_func->value.pass_through.operation == NOP_EXPR)
3943 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3944 bp = bitpack_create (ob->main_stream);
3945 bp_pack_value (&bp, jump_func->value.pass_through.agg_preserved, 1);
3946 bp_pack_value (&bp, jump_func->value.pass_through.type_preserved, 1);
3947 streamer_write_bitpack (&bp);
3949 else
3951 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
3952 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
3954 break;
3955 case IPA_JF_ANCESTOR:
3956 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
3957 stream_write_tree (ob, jump_func->value.ancestor.type, true);
3958 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
3959 bp = bitpack_create (ob->main_stream);
3960 bp_pack_value (&bp, jump_func->value.ancestor.agg_preserved, 1);
3961 bp_pack_value (&bp, jump_func->value.ancestor.type_preserved, 1);
3962 streamer_write_bitpack (&bp);
3963 break;
3966 count = vec_safe_length (jump_func->agg.items);
3967 streamer_write_uhwi (ob, count);
3968 if (count)
3970 bp = bitpack_create (ob->main_stream);
3971 bp_pack_value (&bp, jump_func->agg.by_ref, 1);
3972 streamer_write_bitpack (&bp);
3975 FOR_EACH_VEC_SAFE_ELT (jump_func->agg.items, i, item)
3977 streamer_write_uhwi (ob, item->offset);
3978 stream_write_tree (ob, item->value, true);
3982 /* Read in jump function JUMP_FUNC from IB. */
3984 static void
3985 ipa_read_jump_function (struct lto_input_block *ib,
3986 struct ipa_jump_func *jump_func,
3987 struct cgraph_edge *cs,
3988 struct data_in *data_in)
3990 enum jump_func_type jftype;
3991 enum tree_code operation;
3992 int i, count;
3994 jftype = (enum jump_func_type) streamer_read_uhwi (ib);
3995 switch (jftype)
3997 case IPA_JF_UNKNOWN:
3998 jump_func->type = IPA_JF_UNKNOWN;
3999 break;
4000 case IPA_JF_KNOWN_TYPE:
4002 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4003 tree base_type = stream_read_tree (ib, data_in);
4004 tree component_type = stream_read_tree (ib, data_in);
4006 ipa_set_jf_known_type (jump_func, offset, base_type, component_type);
4007 break;
4009 case IPA_JF_CONST:
4010 ipa_set_jf_constant (jump_func, stream_read_tree (ib, data_in), cs);
4011 break;
4012 case IPA_JF_PASS_THROUGH:
4013 operation = (enum tree_code) streamer_read_uhwi (ib);
4014 if (operation == NOP_EXPR)
4016 int formal_id = streamer_read_uhwi (ib);
4017 struct bitpack_d bp = streamer_read_bitpack (ib);
4018 bool agg_preserved = bp_unpack_value (&bp, 1);
4019 bool type_preserved = bp_unpack_value (&bp, 1);
4020 ipa_set_jf_simple_pass_through (jump_func, formal_id, agg_preserved,
4021 type_preserved);
4023 else
4025 tree operand = stream_read_tree (ib, data_in);
4026 int formal_id = streamer_read_uhwi (ib);
4027 ipa_set_jf_arith_pass_through (jump_func, formal_id, operand,
4028 operation);
4030 break;
4031 case IPA_JF_ANCESTOR:
4033 HOST_WIDE_INT offset = streamer_read_uhwi (ib);
4034 tree type = stream_read_tree (ib, data_in);
4035 int formal_id = streamer_read_uhwi (ib);
4036 struct bitpack_d bp = streamer_read_bitpack (ib);
4037 bool agg_preserved = bp_unpack_value (&bp, 1);
4038 bool type_preserved = bp_unpack_value (&bp, 1);
4040 ipa_set_ancestor_jf (jump_func, offset, type, formal_id, agg_preserved,
4041 type_preserved);
4042 break;
4046 count = streamer_read_uhwi (ib);
4047 vec_alloc (jump_func->agg.items, count);
4048 if (count)
4050 struct bitpack_d bp = streamer_read_bitpack (ib);
4051 jump_func->agg.by_ref = bp_unpack_value (&bp, 1);
4053 for (i = 0; i < count; i++)
4055 struct ipa_agg_jf_item item;
4056 item.offset = streamer_read_uhwi (ib);
4057 item.value = stream_read_tree (ib, data_in);
4058 jump_func->agg.items->quick_push (item);
4062 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
4063 relevant to indirect inlining to OB. */
4065 static void
4066 ipa_write_indirect_edge_info (struct output_block *ob,
4067 struct cgraph_edge *cs)
4069 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4070 struct bitpack_d bp;
4072 streamer_write_hwi (ob, ii->param_index);
4073 streamer_write_hwi (ob, ii->offset);
4074 bp = bitpack_create (ob->main_stream);
4075 bp_pack_value (&bp, ii->polymorphic, 1);
4076 bp_pack_value (&bp, ii->agg_contents, 1);
4077 bp_pack_value (&bp, ii->member_ptr, 1);
4078 bp_pack_value (&bp, ii->by_ref, 1);
4079 streamer_write_bitpack (&bp);
4081 if (ii->polymorphic)
4083 streamer_write_hwi (ob, ii->otr_token);
4084 stream_write_tree (ob, ii->otr_type, true);
4088 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
4089 relevant to indirect inlining from IB. */
4091 static void
4092 ipa_read_indirect_edge_info (struct lto_input_block *ib,
4093 struct data_in *data_in ATTRIBUTE_UNUSED,
4094 struct cgraph_edge *cs)
4096 struct cgraph_indirect_call_info *ii = cs->indirect_info;
4097 struct bitpack_d bp;
4099 ii->param_index = (int) streamer_read_hwi (ib);
4100 ii->offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
4101 bp = streamer_read_bitpack (ib);
4102 ii->polymorphic = bp_unpack_value (&bp, 1);
4103 ii->agg_contents = bp_unpack_value (&bp, 1);
4104 ii->member_ptr = bp_unpack_value (&bp, 1);
4105 ii->by_ref = bp_unpack_value (&bp, 1);
4106 if (ii->polymorphic)
4108 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
4109 ii->otr_type = stream_read_tree (ib, data_in);
4113 /* Stream out NODE info to OB. */
4115 static void
4116 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
4118 int node_ref;
4119 lto_symtab_encoder_t encoder;
4120 struct ipa_node_params *info = IPA_NODE_REF (node);
4121 int j;
4122 struct cgraph_edge *e;
4123 struct bitpack_d bp;
4125 encoder = ob->decl_state->symtab_node_encoder;
4126 node_ref = lto_symtab_encoder_encode (encoder, node);
4127 streamer_write_uhwi (ob, node_ref);
4129 streamer_write_uhwi (ob, ipa_get_param_count (info));
4130 for (j = 0; j < ipa_get_param_count (info); j++)
4131 streamer_write_uhwi (ob, ipa_get_param_move_cost (info, j));
4132 bp = bitpack_create (ob->main_stream);
4133 gcc_assert (info->uses_analysis_done
4134 || ipa_get_param_count (info) == 0);
4135 gcc_assert (!info->node_enqueued);
4136 gcc_assert (!info->ipcp_orig_node);
4137 for (j = 0; j < ipa_get_param_count (info); j++)
4138 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
4139 streamer_write_bitpack (&bp);
4140 for (j = 0; j < ipa_get_param_count (info); j++)
4141 streamer_write_hwi (ob, ipa_get_controlled_uses (info, j));
4142 for (e = node->callees; e; e = e->next_callee)
4144 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4146 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4147 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4148 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4150 for (e = node->indirect_calls; e; e = e->next_callee)
4152 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4154 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
4155 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
4156 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
4157 ipa_write_indirect_edge_info (ob, e);
4161 /* Stream in NODE info from IB. */
4163 static void
4164 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
4165 struct data_in *data_in)
4167 struct ipa_node_params *info = IPA_NODE_REF (node);
4168 int k;
4169 struct cgraph_edge *e;
4170 struct bitpack_d bp;
4172 ipa_alloc_node_params (node, streamer_read_uhwi (ib));
4174 for (k = 0; k < ipa_get_param_count (info); k++)
4175 info->descriptors[k].move_cost = streamer_read_uhwi (ib);
4177 bp = streamer_read_bitpack (ib);
4178 if (ipa_get_param_count (info) != 0)
4179 info->uses_analysis_done = true;
4180 info->node_enqueued = false;
4181 for (k = 0; k < ipa_get_param_count (info); k++)
4182 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
4183 for (k = 0; k < ipa_get_param_count (info); k++)
4184 ipa_set_controlled_uses (info, k, streamer_read_hwi (ib));
4185 for (e = node->callees; e; e = e->next_callee)
4187 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4188 int count = streamer_read_uhwi (ib);
4190 if (!count)
4191 continue;
4192 vec_safe_grow_cleared (args->jump_functions, count);
4194 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4195 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4196 data_in);
4198 for (e = node->indirect_calls; e; e = e->next_callee)
4200 struct ipa_edge_args *args = IPA_EDGE_REF (e);
4201 int count = streamer_read_uhwi (ib);
4203 if (count)
4205 vec_safe_grow_cleared (args->jump_functions, count);
4206 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
4207 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), e,
4208 data_in);
4210 ipa_read_indirect_edge_info (ib, data_in, e);
4214 /* Write jump functions for nodes in SET. */
4216 void
4217 ipa_prop_write_jump_functions (void)
4219 struct cgraph_node *node;
4220 struct output_block *ob;
4221 unsigned int count = 0;
4222 lto_symtab_encoder_iterator lsei;
4223 lto_symtab_encoder_t encoder;
4226 if (!ipa_node_params_vector.exists ())
4227 return;
4229 ob = create_output_block (LTO_section_jump_functions);
4230 encoder = ob->decl_state->symtab_node_encoder;
4231 ob->cgraph_node = NULL;
4232 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4233 lsei_next_function_in_partition (&lsei))
4235 node = lsei_cgraph_node (lsei);
4236 if (cgraph_function_with_gimple_body_p (node)
4237 && IPA_NODE_REF (node) != NULL)
4238 count++;
4241 streamer_write_uhwi (ob, count);
4243 /* Process all of the functions. */
4244 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4245 lsei_next_function_in_partition (&lsei))
4247 node = lsei_cgraph_node (lsei);
4248 if (cgraph_function_with_gimple_body_p (node)
4249 && IPA_NODE_REF (node) != NULL)
4250 ipa_write_node_info (ob, node);
4252 streamer_write_char_stream (ob->main_stream, 0);
4253 produce_asm (ob, NULL);
4254 destroy_output_block (ob);
4257 /* Read section in file FILE_DATA of length LEN with data DATA. */
4259 static void
4260 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
4261 size_t len)
4263 const struct lto_function_header *header =
4264 (const struct lto_function_header *) data;
4265 const int cfg_offset = sizeof (struct lto_function_header);
4266 const int main_offset = cfg_offset + header->cfg_size;
4267 const int string_offset = main_offset + header->main_size;
4268 struct data_in *data_in;
4269 struct lto_input_block ib_main;
4270 unsigned int i;
4271 unsigned int count;
4273 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4274 header->main_size);
4276 data_in =
4277 lto_data_in_create (file_data, (const char *) data + string_offset,
4278 header->string_size, vNULL);
4279 count = streamer_read_uhwi (&ib_main);
4281 for (i = 0; i < count; i++)
4283 unsigned int index;
4284 struct cgraph_node *node;
4285 lto_symtab_encoder_t encoder;
4287 index = streamer_read_uhwi (&ib_main);
4288 encoder = file_data->symtab_node_encoder;
4289 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4290 gcc_assert (node->definition);
4291 ipa_read_node_info (&ib_main, node, data_in);
4293 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4294 len);
4295 lto_data_in_delete (data_in);
4298 /* Read ipcp jump functions. */
4300 void
4301 ipa_prop_read_jump_functions (void)
4303 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4304 struct lto_file_decl_data *file_data;
4305 unsigned int j = 0;
4307 ipa_check_create_node_params ();
4308 ipa_check_create_edge_args ();
4309 ipa_register_cgraph_hooks ();
4311 while ((file_data = file_data_vec[j++]))
4313 size_t len;
4314 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
4316 if (data)
4317 ipa_prop_read_section (file_data, data, len);
4321 /* After merging units, we can get mismatch in argument counts.
4322 Also decl merging might've rendered parameter lists obsolete.
4323 Also compute called_with_variable_arg info. */
4325 void
4326 ipa_update_after_lto_read (void)
4328 ipa_check_create_node_params ();
4329 ipa_check_create_edge_args ();
4332 void
4333 write_agg_replacement_chain (struct output_block *ob, struct cgraph_node *node)
4335 int node_ref;
4336 unsigned int count = 0;
4337 lto_symtab_encoder_t encoder;
4338 struct ipa_agg_replacement_value *aggvals, *av;
4340 aggvals = ipa_get_agg_replacements_for_node (node);
4341 encoder = ob->decl_state->symtab_node_encoder;
4342 node_ref = lto_symtab_encoder_encode (encoder, node);
4343 streamer_write_uhwi (ob, node_ref);
4345 for (av = aggvals; av; av = av->next)
4346 count++;
4347 streamer_write_uhwi (ob, count);
4349 for (av = aggvals; av; av = av->next)
4351 struct bitpack_d bp;
4353 streamer_write_uhwi (ob, av->offset);
4354 streamer_write_uhwi (ob, av->index);
4355 stream_write_tree (ob, av->value, true);
4357 bp = bitpack_create (ob->main_stream);
4358 bp_pack_value (&bp, av->by_ref, 1);
4359 streamer_write_bitpack (&bp);
4363 /* Stream in the aggregate value replacement chain for NODE from IB. */
4365 static void
4366 read_agg_replacement_chain (struct lto_input_block *ib,
4367 struct cgraph_node *node,
4368 struct data_in *data_in)
4370 struct ipa_agg_replacement_value *aggvals = NULL;
4371 unsigned int count, i;
4373 count = streamer_read_uhwi (ib);
4374 for (i = 0; i <count; i++)
4376 struct ipa_agg_replacement_value *av;
4377 struct bitpack_d bp;
4379 av = ggc_alloc_ipa_agg_replacement_value ();
4380 av->offset = streamer_read_uhwi (ib);
4381 av->index = streamer_read_uhwi (ib);
4382 av->value = stream_read_tree (ib, data_in);
4383 bp = streamer_read_bitpack (ib);
4384 av->by_ref = bp_unpack_value (&bp, 1);
4385 av->next = aggvals;
4386 aggvals = av;
4388 ipa_set_node_agg_value_chain (node, aggvals);
4391 /* Write all aggregate replacement for nodes in set. */
4393 void
4394 ipa_prop_write_all_agg_replacement (void)
4396 struct cgraph_node *node;
4397 struct output_block *ob;
4398 unsigned int count = 0;
4399 lto_symtab_encoder_iterator lsei;
4400 lto_symtab_encoder_t encoder;
4402 if (!ipa_node_agg_replacements)
4403 return;
4405 ob = create_output_block (LTO_section_ipcp_transform);
4406 encoder = ob->decl_state->symtab_node_encoder;
4407 ob->cgraph_node = NULL;
4408 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4409 lsei_next_function_in_partition (&lsei))
4411 node = lsei_cgraph_node (lsei);
4412 if (cgraph_function_with_gimple_body_p (node)
4413 && ipa_get_agg_replacements_for_node (node) != NULL)
4414 count++;
4417 streamer_write_uhwi (ob, count);
4419 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
4420 lsei_next_function_in_partition (&lsei))
4422 node = lsei_cgraph_node (lsei);
4423 if (cgraph_function_with_gimple_body_p (node)
4424 && ipa_get_agg_replacements_for_node (node) != NULL)
4425 write_agg_replacement_chain (ob, node);
4427 streamer_write_char_stream (ob->main_stream, 0);
4428 produce_asm (ob, NULL);
4429 destroy_output_block (ob);
4432 /* Read replacements section in file FILE_DATA of length LEN with data
4433 DATA. */
4435 static void
4436 read_replacements_section (struct lto_file_decl_data *file_data,
4437 const char *data,
4438 size_t len)
4440 const struct lto_function_header *header =
4441 (const struct lto_function_header *) data;
4442 const int cfg_offset = sizeof (struct lto_function_header);
4443 const int main_offset = cfg_offset + header->cfg_size;
4444 const int string_offset = main_offset + header->main_size;
4445 struct data_in *data_in;
4446 struct lto_input_block ib_main;
4447 unsigned int i;
4448 unsigned int count;
4450 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
4451 header->main_size);
4453 data_in = lto_data_in_create (file_data, (const char *) data + string_offset,
4454 header->string_size, vNULL);
4455 count = streamer_read_uhwi (&ib_main);
4457 for (i = 0; i < count; i++)
4459 unsigned int index;
4460 struct cgraph_node *node;
4461 lto_symtab_encoder_t encoder;
4463 index = streamer_read_uhwi (&ib_main);
4464 encoder = file_data->symtab_node_encoder;
4465 node = cgraph (lto_symtab_encoder_deref (encoder, index));
4466 gcc_assert (node->definition);
4467 read_agg_replacement_chain (&ib_main, node, data_in);
4469 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
4470 len);
4471 lto_data_in_delete (data_in);
4474 /* Read IPA-CP aggregate replacements. */
4476 void
4477 ipa_prop_read_all_agg_replacement (void)
4479 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4480 struct lto_file_decl_data *file_data;
4481 unsigned int j = 0;
4483 while ((file_data = file_data_vec[j++]))
4485 size_t len;
4486 const char *data = lto_get_section_data (file_data,
4487 LTO_section_ipcp_transform,
4488 NULL, &len);
4489 if (data)
4490 read_replacements_section (file_data, data, len);
4494 /* Adjust the aggregate replacements in AGGVAL to reflect parameters skipped in
4495 NODE. */
4497 static void
4498 adjust_agg_replacement_values (struct cgraph_node *node,
4499 struct ipa_agg_replacement_value *aggval)
4501 struct ipa_agg_replacement_value *v;
4502 int i, c = 0, d = 0, *adj;
4504 if (!node->clone.combined_args_to_skip)
4505 return;
4507 for (v = aggval; v; v = v->next)
4509 gcc_assert (v->index >= 0);
4510 if (c < v->index)
4511 c = v->index;
4513 c++;
4515 adj = XALLOCAVEC (int, c);
4516 for (i = 0; i < c; i++)
4517 if (bitmap_bit_p (node->clone.combined_args_to_skip, i))
4519 adj[i] = -1;
4520 d++;
4522 else
4523 adj[i] = i - d;
4525 for (v = aggval; v; v = v->next)
4526 v->index = adj[v->index];
4530 /* Function body transformation phase. */
4532 unsigned int
4533 ipcp_transform_function (struct cgraph_node *node)
4535 vec<ipa_param_descriptor_t> descriptors = vNULL;
4536 struct param_analysis_info *parms_ainfo;
4537 struct ipa_agg_replacement_value *aggval;
4538 gimple_stmt_iterator gsi;
4539 basic_block bb;
4540 int param_count;
4541 bool cfg_changed = false, something_changed = false;
4543 gcc_checking_assert (cfun);
4544 gcc_checking_assert (current_function_decl);
4546 if (dump_file)
4547 fprintf (dump_file, "Modification phase of node %s/%i\n",
4548 cgraph_node_name (node), node->order);
4550 aggval = ipa_get_agg_replacements_for_node (node);
4551 if (!aggval)
4552 return 0;
4553 param_count = count_formal_params (node->decl);
4554 if (param_count == 0)
4555 return 0;
4556 adjust_agg_replacement_values (node, aggval);
4557 if (dump_file)
4558 ipa_dump_agg_replacement_values (dump_file, aggval);
4559 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
4560 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
4561 descriptors.safe_grow_cleared (param_count);
4562 ipa_populate_param_decls (node, descriptors);
4564 FOR_EACH_BB (bb)
4565 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4567 struct ipa_agg_replacement_value *v;
4568 gimple stmt = gsi_stmt (gsi);
4569 tree rhs, val, t;
4570 HOST_WIDE_INT offset;
4571 int index;
4572 bool by_ref, vce;
4574 if (!gimple_assign_load_p (stmt))
4575 continue;
4576 rhs = gimple_assign_rhs1 (stmt);
4577 if (!is_gimple_reg_type (TREE_TYPE (rhs)))
4578 continue;
4580 vce = false;
4581 t = rhs;
4582 while (handled_component_p (t))
4584 /* V_C_E can do things like convert an array of integers to one
4585 bigger integer and similar things we do not handle below. */
4586 if (TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
4588 vce = true;
4589 break;
4591 t = TREE_OPERAND (t, 0);
4593 if (vce)
4594 continue;
4596 if (!ipa_load_from_parm_agg_1 (descriptors, parms_ainfo, stmt,
4597 rhs, &index, &offset, &by_ref))
4598 continue;
4599 for (v = aggval; v; v = v->next)
4600 if (v->index == index
4601 && v->offset == offset)
4602 break;
4603 if (!v || v->by_ref != by_ref)
4604 continue;
4606 gcc_checking_assert (is_gimple_ip_invariant (v->value));
4607 if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (v->value)))
4609 if (fold_convertible_p (TREE_TYPE (rhs), v->value))
4610 val = fold_build1 (NOP_EXPR, TREE_TYPE (rhs), v->value);
4611 else if (TYPE_SIZE (TREE_TYPE (rhs))
4612 == TYPE_SIZE (TREE_TYPE (v->value)))
4613 val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs), v->value);
4614 else
4616 if (dump_file)
4618 fprintf (dump_file, " const ");
4619 print_generic_expr (dump_file, v->value, 0);
4620 fprintf (dump_file, " can't be converted to type of ");
4621 print_generic_expr (dump_file, rhs, 0);
4622 fprintf (dump_file, "\n");
4624 continue;
4627 else
4628 val = v->value;
4630 if (dump_file && (dump_flags & TDF_DETAILS))
4632 fprintf (dump_file, "Modifying stmt:\n ");
4633 print_gimple_stmt (dump_file, stmt, 0, 0);
4635 gimple_assign_set_rhs_from_tree (&gsi, val);
4636 update_stmt (stmt);
4638 if (dump_file && (dump_flags & TDF_DETAILS))
4640 fprintf (dump_file, "into:\n ");
4641 print_gimple_stmt (dump_file, stmt, 0, 0);
4642 fprintf (dump_file, "\n");
4645 something_changed = true;
4646 if (maybe_clean_eh_stmt (stmt)
4647 && gimple_purge_dead_eh_edges (gimple_bb (stmt)))
4648 cfg_changed = true;
4651 (*ipa_node_agg_replacements)[node->uid] = NULL;
4652 free_parms_ainfo (parms_ainfo, param_count);
4653 descriptors.release ();
4655 if (!something_changed)
4656 return 0;
4657 else if (cfg_changed)
4658 return TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
4659 else
4660 return TODO_update_ssa_only_virtuals;