name-lookup.c (lookup_arg_dependent): Use conditional timevars.
[official-gcc.git] / gcc / ipa-prop.c
blob066bbdbf03e35325f70fcb9ab93491320dcd23c8
1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "langhooks.h"
26 #include "ggc.h"
27 #include "target.h"
28 #include "cgraph.h"
29 #include "ipa-prop.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-inline.h"
33 #include "gimple.h"
34 #include "flags.h"
35 #include "timevar.h"
36 #include "flags.h"
37 #include "diagnostic.h"
38 #include "tree-pretty-print.h"
39 #include "gimple-pretty-print.h"
40 #include "lto-streamer.h"
41 #include "data-streamer.h"
42 #include "tree-streamer.h"
45 /* Intermediate information about a parameter that is only useful during the
46 run of ipa_analyze_node and is not kept afterwards. */
48 struct param_analysis_info
50 bool modified;
51 bitmap visited_statements;
54 /* Vector where the parameter infos are actually stored. */
55 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
56 /* Vector where the parameter infos are actually stored. */
57 VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
59 /* Bitmap with all UIDs of call graph edges that have been already processed
60 by indirect inlining. */
61 static bitmap iinlining_processed_edges;
63 /* Holders of ipa cgraph hooks: */
64 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
65 static struct cgraph_node_hook_list *node_removal_hook_holder;
66 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
67 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
68 static struct cgraph_node_hook_list *function_insertion_hook_holder;
70 /* Return index of the formal whose tree is PTREE in function which corresponds
71 to INFO. */
73 int
74 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
76 int i, count;
78 count = ipa_get_param_count (info);
79 for (i = 0; i < count; i++)
80 if (ipa_get_param (info, i) == ptree)
81 return i;
83 return -1;
86 /* Populate the param_decl field in parameter descriptors of INFO that
87 corresponds to NODE. */
89 static void
90 ipa_populate_param_decls (struct cgraph_node *node,
91 struct ipa_node_params *info)
93 tree fndecl;
94 tree fnargs;
95 tree parm;
96 int param_num;
98 fndecl = node->decl;
99 fnargs = DECL_ARGUMENTS (fndecl);
100 param_num = 0;
101 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
103 VEC_index (ipa_param_descriptor_t,
104 info->descriptors, param_num)->decl = parm;
105 param_num++;
109 /* Return how many formal parameters FNDECL has. */
111 static inline int
112 count_formal_params (tree fndecl)
114 tree parm;
115 int count = 0;
117 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
118 count++;
120 return count;
123 /* Initialize the ipa_node_params structure associated with NODE by counting
124 the function parameters, creating the descriptors and populating their
125 param_decls. */
127 void
128 ipa_initialize_node_params (struct cgraph_node *node)
130 struct ipa_node_params *info = IPA_NODE_REF (node);
132 if (!info->descriptors)
134 int param_count;
136 param_count = count_formal_params (node->decl);
137 if (param_count)
139 VEC_safe_grow_cleared (ipa_param_descriptor_t, heap,
140 info->descriptors, param_count);
141 ipa_populate_param_decls (node, info);
146 /* Print the jump functions associated with call graph edge CS to file F. */
148 static void
149 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
151 int i, count;
153 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
154 for (i = 0; i < count; i++)
156 struct ipa_jump_func *jump_func;
157 enum jump_func_type type;
159 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
160 type = jump_func->type;
162 fprintf (f, " param %d: ", i);
163 if (type == IPA_JF_UNKNOWN)
164 fprintf (f, "UNKNOWN\n");
165 else if (type == IPA_JF_KNOWN_TYPE)
167 tree binfo_type = TREE_TYPE (jump_func->value.base_binfo);
168 fprintf (f, "KNOWN TYPE, type in binfo is: ");
169 print_generic_expr (f, binfo_type, 0);
170 fprintf (f, " (%u)\n", TYPE_UID (binfo_type));
172 else if (type == IPA_JF_CONST)
174 tree val = jump_func->value.constant;
175 fprintf (f, "CONST: ");
176 print_generic_expr (f, val, 0);
177 if (TREE_CODE (val) == ADDR_EXPR
178 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
180 fprintf (f, " -> ");
181 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
184 fprintf (f, "\n");
186 else if (type == IPA_JF_CONST_MEMBER_PTR)
188 fprintf (f, "CONST MEMBER PTR: ");
189 print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
190 fprintf (f, ", ");
191 print_generic_expr (f, jump_func->value.member_cst.delta, 0);
192 fprintf (f, "\n");
194 else if (type == IPA_JF_PASS_THROUGH)
196 fprintf (f, "PASS THROUGH: ");
197 fprintf (f, "%d, op %s ",
198 jump_func->value.pass_through.formal_id,
199 tree_code_name[(int)
200 jump_func->value.pass_through.operation]);
201 if (jump_func->value.pass_through.operation != NOP_EXPR)
202 print_generic_expr (dump_file,
203 jump_func->value.pass_through.operand, 0);
204 fprintf (dump_file, "\n");
206 else if (type == IPA_JF_ANCESTOR)
208 fprintf (f, "ANCESTOR: ");
209 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
210 jump_func->value.ancestor.formal_id,
211 jump_func->value.ancestor.offset);
212 print_generic_expr (f, jump_func->value.ancestor.type, 0);
213 fprintf (dump_file, "\n");
219 /* Print the jump functions of all arguments on all call graph edges going from
220 NODE to file F. */
222 void
223 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
225 struct cgraph_edge *cs;
226 int i;
228 fprintf (f, " Jump functions of caller %s:\n", cgraph_node_name (node));
229 for (cs = node->callees; cs; cs = cs->next_callee)
231 if (!ipa_edge_args_info_available_for_edge_p (cs))
232 continue;
234 fprintf (f, " callsite %s/%i -> %s/%i : \n",
235 cgraph_node_name (node), node->uid,
236 cgraph_node_name (cs->callee), cs->callee->uid);
237 ipa_print_node_jump_functions_for_edge (f, cs);
240 for (cs = node->indirect_calls, i = 0; cs; cs = cs->next_callee, i++)
242 if (!ipa_edge_args_info_available_for_edge_p (cs))
243 continue;
245 if (cs->call_stmt)
247 fprintf (f, " indirect callsite %d for stmt ", i);
248 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
250 else
251 fprintf (f, " indirect callsite %d :\n", i);
252 ipa_print_node_jump_functions_for_edge (f, cs);
257 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
259 void
260 ipa_print_all_jump_functions (FILE *f)
262 struct cgraph_node *node;
264 fprintf (f, "\nJump functions:\n");
265 for (node = cgraph_nodes; node; node = node->next)
267 ipa_print_node_jump_functions (f, node);
271 /* Structure to be passed in between detect_type_change and
272 check_stmt_for_type_change. */
274 struct type_change_info
276 /* Set to true if dynamic type change has been detected. */
277 bool type_maybe_changed;
280 /* Return true if STMT can modify a virtual method table pointer.
282 This function makes special assumptions about both constructors and
283 destructors which are all the functions that are allowed to alter the VMT
284 pointers. It assumes that destructors begin with assignment into all VMT
285 pointers and that constructors essentially look in the following way:
287 1) The very first thing they do is that they call constructors of ancestor
288 sub-objects that have them.
290 2) Then VMT pointers of this and all its ancestors is set to new values
291 corresponding to the type corresponding to the constructor.
293 3) Only afterwards, other stuff such as constructor of member sub-objects
294 and the code written by the user is run. Only this may include calling
295 virtual functions, directly or indirectly.
297 There is no way to call a constructor of an ancestor sub-object in any
298 other way.
300 This means that we do not have to care whether constructors get the correct
301 type information because they will always change it (in fact, if we define
302 the type to be given by the VMT pointer, it is undefined).
304 The most important fact to derive from the above is that if, for some
305 statement in the section 3, we try to detect whether the dynamic type has
306 changed, we can safely ignore all calls as we examine the function body
307 backwards until we reach statements in section 2 because these calls cannot
308 be ancestor constructors or destructors (if the input is not bogus) and so
309 do not change the dynamic type (this holds true only for automatically
310 allocated objects but at the moment we devirtualize only these). We then
311 must detect that statements in section 2 change the dynamic type and can try
312 to derive the new type. That is enough and we can stop, we will never see
313 the calls into constructors of sub-objects in this code. Therefore we can
314 safely ignore all call statements that we traverse.
317 static bool
318 stmt_may_be_vtbl_ptr_store (gimple stmt)
320 if (is_gimple_call (stmt))
321 return false;
322 else if (is_gimple_assign (stmt))
324 tree lhs = gimple_assign_lhs (stmt);
326 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
328 if (flag_strict_aliasing
329 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
330 return false;
332 if (TREE_CODE (lhs) == COMPONENT_REF
333 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
334 return false;
335 /* In the future we might want to use get_base_ref_and_offset to find
336 if there is a field corresponding to the offset and if so, proceed
337 almost like if it was a component ref. */
340 return true;
343 /* Callback of walk_aliased_vdefs and a helper function for
344 detect_type_change to check whether a particular statement may modify
345 the virtual table pointer, and if possible also determine the new type of
346 the (sub-)object. It stores its result into DATA, which points to a
347 type_change_info structure. */
349 static bool
350 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
352 gimple stmt = SSA_NAME_DEF_STMT (vdef);
353 struct type_change_info *tci = (struct type_change_info *) data;
355 if (stmt_may_be_vtbl_ptr_store (stmt))
357 tci->type_maybe_changed = true;
358 return true;
360 else
361 return false;
364 /* Detect whether the dynamic type of ARG has changed (before callsite CALL) by
365 looking for assignments to its virtual table pointer. If it is, return true
366 and fill in the jump function JFUNC with relevant type information or set it
367 to unknown. ARG is the object itself (not a pointer to it, unless
368 dereferenced). BASE is the base of the memory access as returned by
369 get_ref_base_and_extent, as is the offset. */
371 static bool
372 detect_type_change (tree arg, tree base, gimple call,
373 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
375 struct type_change_info tci;
376 ao_ref ao;
378 gcc_checking_assert (DECL_P (arg)
379 || TREE_CODE (arg) == MEM_REF
380 || handled_component_p (arg));
381 /* Const calls cannot call virtual methods through VMT and so type changes do
382 not matter. */
383 if (!flag_devirtualize || !gimple_vuse (call))
384 return false;
386 tci.type_maybe_changed = false;
388 ao.ref = arg;
389 ao.base = base;
390 ao.offset = offset;
391 ao.size = POINTER_SIZE;
392 ao.max_size = ao.size;
393 ao.ref_alias_set = -1;
394 ao.base_alias_set = -1;
396 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
397 &tci, NULL);
398 if (!tci.type_maybe_changed)
399 return false;
401 jfunc->type = IPA_JF_UNKNOWN;
402 return true;
405 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
406 SSA name (its dereference will become the base and the offset is assumed to
407 be zero). */
409 static bool
410 detect_type_change_ssa (tree arg, gimple call, struct ipa_jump_func *jfunc)
412 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
413 if (!flag_devirtualize
414 || !POINTER_TYPE_P (TREE_TYPE (arg))
415 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != RECORD_TYPE)
416 return false;
418 arg = build2 (MEM_REF, ptr_type_node, arg,
419 build_int_cst (ptr_type_node, 0));
421 return detect_type_change (arg, arg, call, jfunc, 0);
425 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
426 of an assignment statement STMT, try to find out whether NAME can be
427 described by a (possibly polynomial) pass-through jump-function or an
428 ancestor jump function and if so, write the appropriate function into
429 JFUNC */
431 static void
432 compute_complex_assign_jump_func (struct ipa_node_params *info,
433 struct ipa_jump_func *jfunc,
434 gimple call, gimple stmt, tree name)
436 HOST_WIDE_INT offset, size, max_size;
437 tree op1, op2, base, ssa;
438 int index;
440 op1 = gimple_assign_rhs1 (stmt);
441 op2 = gimple_assign_rhs2 (stmt);
443 if (TREE_CODE (op1) == SSA_NAME
444 && SSA_NAME_IS_DEFAULT_DEF (op1))
446 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
447 if (index < 0)
448 return;
450 if (op2)
452 if (!is_gimple_ip_invariant (op2)
453 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
454 && !useless_type_conversion_p (TREE_TYPE (name),
455 TREE_TYPE (op1))))
456 return;
458 jfunc->type = IPA_JF_PASS_THROUGH;
459 jfunc->value.pass_through.formal_id = index;
460 jfunc->value.pass_through.operation = gimple_assign_rhs_code (stmt);
461 jfunc->value.pass_through.operand = op2;
463 else if (gimple_assign_unary_nop_p (stmt)
464 && !detect_type_change_ssa (op1, call, jfunc))
466 jfunc->type = IPA_JF_PASS_THROUGH;
467 jfunc->value.pass_through.formal_id = index;
468 jfunc->value.pass_through.operation = NOP_EXPR;
470 return;
473 if (TREE_CODE (op1) != ADDR_EXPR)
474 return;
475 op1 = TREE_OPERAND (op1, 0);
476 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
477 return;
478 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
479 if (TREE_CODE (base) != MEM_REF
480 /* If this is a varying address, punt. */
481 || max_size == -1
482 || max_size != size)
483 return;
484 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
485 ssa = TREE_OPERAND (base, 0);
486 if (TREE_CODE (ssa) != SSA_NAME
487 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
488 || offset < 0)
489 return;
491 /* Dynamic types are changed only in constructors and destructors and */
492 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
493 if (index >= 0
494 && !detect_type_change (op1, base, call, jfunc, offset))
496 jfunc->type = IPA_JF_ANCESTOR;
497 jfunc->value.ancestor.formal_id = index;
498 jfunc->value.ancestor.offset = offset;
499 jfunc->value.ancestor.type = TREE_TYPE (op1);
503 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
504 it looks like:
506 iftmp.1_3 = &obj_2(D)->D.1762;
508 The base of the MEM_REF must be a default definition SSA NAME of a
509 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
510 whole MEM_REF expression is returned and the offset calculated from any
511 handled components and the MEM_REF itself is stored into *OFFSET. The whole
512 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
514 static tree
515 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
517 HOST_WIDE_INT size, max_size;
518 tree expr, parm, obj;
520 if (!gimple_assign_single_p (assign))
521 return NULL_TREE;
522 expr = gimple_assign_rhs1 (assign);
524 if (TREE_CODE (expr) != ADDR_EXPR)
525 return NULL_TREE;
526 expr = TREE_OPERAND (expr, 0);
527 obj = expr;
528 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
530 if (TREE_CODE (expr) != MEM_REF
531 /* If this is a varying address, punt. */
532 || max_size == -1
533 || max_size != size
534 || *offset < 0)
535 return NULL_TREE;
536 parm = TREE_OPERAND (expr, 0);
537 if (TREE_CODE (parm) != SSA_NAME
538 || !SSA_NAME_IS_DEFAULT_DEF (parm)
539 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
540 return NULL_TREE;
542 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
543 *obj_p = obj;
544 return expr;
548 /* Given that an actual argument is an SSA_NAME that is a result of a phi
549 statement PHI, try to find out whether NAME is in fact a
550 multiple-inheritance typecast from a descendant into an ancestor of a formal
551 parameter and thus can be described by an ancestor jump function and if so,
552 write the appropriate function into JFUNC.
554 Essentially we want to match the following pattern:
556 if (obj_2(D) != 0B)
557 goto <bb 3>;
558 else
559 goto <bb 4>;
561 <bb 3>:
562 iftmp.1_3 = &obj_2(D)->D.1762;
564 <bb 4>:
565 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
566 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
567 return D.1879_6; */
569 static void
570 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
571 struct ipa_jump_func *jfunc,
572 gimple call, gimple phi)
574 HOST_WIDE_INT offset;
575 gimple assign, cond;
576 basic_block phi_bb, assign_bb, cond_bb;
577 tree tmp, parm, expr, obj;
578 int index, i;
580 if (gimple_phi_num_args (phi) != 2)
581 return;
583 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
584 tmp = PHI_ARG_DEF (phi, 0);
585 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
586 tmp = PHI_ARG_DEF (phi, 1);
587 else
588 return;
589 if (TREE_CODE (tmp) != SSA_NAME
590 || SSA_NAME_IS_DEFAULT_DEF (tmp)
591 || !POINTER_TYPE_P (TREE_TYPE (tmp))
592 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
593 return;
595 assign = SSA_NAME_DEF_STMT (tmp);
596 assign_bb = gimple_bb (assign);
597 if (!single_pred_p (assign_bb))
598 return;
599 expr = get_ancestor_addr_info (assign, &obj, &offset);
600 if (!expr)
601 return;
602 parm = TREE_OPERAND (expr, 0);
603 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
604 gcc_assert (index >= 0);
606 cond_bb = single_pred (assign_bb);
607 cond = last_stmt (cond_bb);
608 if (!cond
609 || gimple_code (cond) != GIMPLE_COND
610 || gimple_cond_code (cond) != NE_EXPR
611 || gimple_cond_lhs (cond) != parm
612 || !integer_zerop (gimple_cond_rhs (cond)))
613 return;
615 phi_bb = gimple_bb (phi);
616 for (i = 0; i < 2; i++)
618 basic_block pred = EDGE_PRED (phi_bb, i)->src;
619 if (pred != assign_bb && pred != cond_bb)
620 return;
623 if (!detect_type_change (obj, expr, call, jfunc, offset))
625 jfunc->type = IPA_JF_ANCESTOR;
626 jfunc->value.ancestor.formal_id = index;
627 jfunc->value.ancestor.offset = offset;
628 jfunc->value.ancestor.type = TREE_TYPE (obj);
632 /* Given OP which is passed as an actual argument to a called function,
633 determine if it is possible to construct a KNOWN_TYPE jump function for it
634 and if so, create one and store it to JFUNC. */
636 static void
637 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
638 gimple call)
640 HOST_WIDE_INT offset, size, max_size;
641 tree base, binfo;
643 if (!flag_devirtualize
644 || TREE_CODE (op) != ADDR_EXPR
645 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
646 return;
648 op = TREE_OPERAND (op, 0);
649 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
650 if (!DECL_P (base)
651 || max_size == -1
652 || max_size != size
653 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
654 || is_global_var (base))
655 return;
657 if (detect_type_change (op, base, call, jfunc, offset))
658 return;
660 binfo = TYPE_BINFO (TREE_TYPE (base));
661 if (!binfo)
662 return;
663 binfo = get_binfo_at_offset (binfo, offset, TREE_TYPE (op));
664 if (binfo)
666 jfunc->type = IPA_JF_KNOWN_TYPE;
667 jfunc->value.base_binfo = binfo;
672 /* Determine the jump functions of scalar arguments. Scalar means SSA names
673 and constants of a number of selected types. INFO is the ipa_node_params
674 structure associated with the caller, FUNCTIONS is a pointer to an array of
675 jump function structures associated with CALL which is the call statement
676 being examined.*/
678 static void
679 compute_scalar_jump_functions (struct ipa_node_params *info,
680 struct ipa_edge_args *args,
681 gimple call)
683 tree arg;
684 unsigned num = 0;
686 for (num = 0; num < gimple_call_num_args (call); num++)
688 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
689 arg = gimple_call_arg (call, num);
691 if (is_gimple_ip_invariant (arg))
693 jfunc->type = IPA_JF_CONST;
694 jfunc->value.constant = arg;
696 else if (TREE_CODE (arg) == SSA_NAME)
698 if (SSA_NAME_IS_DEFAULT_DEF (arg))
700 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
702 if (index >= 0
703 && !detect_type_change_ssa (arg, call, jfunc))
705 jfunc->type = IPA_JF_PASS_THROUGH;
706 jfunc->value.pass_through.formal_id = index;
707 jfunc->value.pass_through.operation = NOP_EXPR;
710 else
712 gimple stmt = SSA_NAME_DEF_STMT (arg);
713 if (is_gimple_assign (stmt))
714 compute_complex_assign_jump_func (info, jfunc, call, stmt, arg);
715 else if (gimple_code (stmt) == GIMPLE_PHI)
716 compute_complex_ancestor_jump_func (info, jfunc, call, stmt);
719 else
720 compute_known_type_jump_func (arg, jfunc, call);
724 /* Inspect the given TYPE and return true iff it has the same structure (the
725 same number of fields of the same types) as a C++ member pointer. If
726 METHOD_PTR and DELTA are non-NULL, store the trees representing the
727 corresponding fields there. */
729 static bool
730 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
732 tree fld;
734 if (TREE_CODE (type) != RECORD_TYPE)
735 return false;
737 fld = TYPE_FIELDS (type);
738 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
739 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
740 return false;
742 if (method_ptr)
743 *method_ptr = fld;
745 fld = DECL_CHAIN (fld);
746 if (!fld || INTEGRAL_TYPE_P (fld))
747 return false;
748 if (delta)
749 *delta = fld;
751 if (DECL_CHAIN (fld))
752 return false;
754 return true;
757 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
758 boolean variable pointed to by DATA. */
760 static bool
761 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
762 void *data)
764 bool *b = (bool *) data;
765 *b = true;
766 return true;
769 /* Return true if the formal parameter PARM might have been modified in this
770 function before reaching the statement CALL. PARM_INFO is a pointer to a
771 structure containing intermediate information about PARM. */
773 static bool
774 is_parm_modified_before_call (struct param_analysis_info *parm_info,
775 gimple call, tree parm)
777 bool modified = false;
778 ao_ref refd;
780 if (parm_info->modified)
781 return true;
783 ao_ref_init (&refd, parm);
784 walk_aliased_vdefs (&refd, gimple_vuse (call), mark_modified,
785 &modified, &parm_info->visited_statements);
786 if (modified)
788 parm_info->modified = true;
789 return true;
791 return false;
794 /* Go through arguments of the CALL and for every one that looks like a member
795 pointer, check whether it can be safely declared pass-through and if so,
796 mark that to the corresponding item of jump FUNCTIONS. Return true iff
797 there are non-pass-through member pointers within the arguments. INFO
798 describes formal parameters of the caller. PARMS_INFO is a pointer to a
799 vector containing intermediate information about each formal parameter. */
801 static bool
802 compute_pass_through_member_ptrs (struct ipa_node_params *info,
803 struct param_analysis_info *parms_info,
804 struct ipa_edge_args *args,
805 gimple call)
807 bool undecided_members = false;
808 unsigned num;
809 tree arg;
811 for (num = 0; num < gimple_call_num_args (call); num++)
813 arg = gimple_call_arg (call, num);
815 if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
817 if (TREE_CODE (arg) == PARM_DECL)
819 int index = ipa_get_param_decl_index (info, arg);
821 gcc_assert (index >=0);
822 if (!is_parm_modified_before_call (&parms_info[index], call, arg))
824 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args,
825 num);
826 jfunc->type = IPA_JF_PASS_THROUGH;
827 jfunc->value.pass_through.formal_id = index;
828 jfunc->value.pass_through.operation = NOP_EXPR;
830 else
831 undecided_members = true;
833 else
834 undecided_members = true;
838 return undecided_members;
841 /* Simple function filling in a member pointer constant jump function (with PFN
842 and DELTA as the constant value) into JFUNC. */
844 static void
845 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
846 tree pfn, tree delta)
848 jfunc->type = IPA_JF_CONST_MEMBER_PTR;
849 jfunc->value.member_cst.pfn = pfn;
850 jfunc->value.member_cst.delta = delta;
853 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
854 return the rhs of its defining statement. */
856 static inline tree
857 get_ssa_def_if_simple_copy (tree rhs)
859 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
861 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
863 if (gimple_assign_single_p (def_stmt))
864 rhs = gimple_assign_rhs1 (def_stmt);
865 else
866 break;
868 return rhs;
871 /* Traverse statements from CALL backwards, scanning whether the argument ARG
872 which is a member pointer is filled in with constant values. If it is, fill
873 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
874 fields of the record type of the member pointer. To give an example, we
875 look for a pattern looking like the following:
877 D.2515.__pfn ={v} printStuff;
878 D.2515.__delta ={v} 0;
879 i_1 = doprinting (D.2515); */
881 static void
882 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
883 tree delta_field, struct ipa_jump_func *jfunc)
885 gimple_stmt_iterator gsi;
886 tree method = NULL_TREE;
887 tree delta = NULL_TREE;
889 gsi = gsi_for_stmt (call);
891 gsi_prev (&gsi);
892 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
894 gimple stmt = gsi_stmt (gsi);
895 tree lhs, rhs, fld;
897 if (!stmt_may_clobber_ref_p (stmt, arg))
898 continue;
899 if (!gimple_assign_single_p (stmt))
900 return;
902 lhs = gimple_assign_lhs (stmt);
903 rhs = gimple_assign_rhs1 (stmt);
905 if (TREE_CODE (lhs) != COMPONENT_REF
906 || TREE_OPERAND (lhs, 0) != arg)
907 return;
909 fld = TREE_OPERAND (lhs, 1);
910 if (!method && fld == method_field)
912 rhs = get_ssa_def_if_simple_copy (rhs);
913 if (TREE_CODE (rhs) == ADDR_EXPR
914 && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
915 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
917 method = TREE_OPERAND (rhs, 0);
918 if (delta)
920 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
921 return;
924 else
925 return;
928 if (!delta && fld == delta_field)
930 rhs = get_ssa_def_if_simple_copy (rhs);
931 if (TREE_CODE (rhs) == INTEGER_CST)
933 delta = rhs;
934 if (method)
936 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
937 return;
940 else
941 return;
945 return;
948 /* Go through the arguments of the CALL and for every member pointer within
949 tries determine whether it is a constant. If it is, create a corresponding
950 constant jump function in FUNCTIONS which is an array of jump functions
951 associated with the call. */
953 static void
954 compute_cst_member_ptr_arguments (struct ipa_edge_args *args,
955 gimple call)
957 unsigned num;
958 tree arg, method_field, delta_field;
960 for (num = 0; num < gimple_call_num_args (call); num++)
962 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
963 arg = gimple_call_arg (call, num);
965 if (jfunc->type == IPA_JF_UNKNOWN
966 && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
967 &delta_field))
968 determine_cst_member_ptr (call, arg, method_field, delta_field, jfunc);
972 /* Compute jump function for all arguments of callsite CS and insert the
973 information in the jump_functions array in the ipa_edge_args corresponding
974 to this callsite. */
976 static void
977 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_info,
978 struct cgraph_edge *cs)
980 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
981 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
982 gimple call = cs->call_stmt;
983 int arg_num = gimple_call_num_args (call);
985 if (arg_num == 0 || args->jump_functions)
986 return;
987 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, arg_num);
989 /* We will deal with constants and SSA scalars first: */
990 compute_scalar_jump_functions (info, args, call);
992 /* Let's check whether there are any potential member pointers and if so,
993 whether we can determine their functions as pass_through. */
994 if (!compute_pass_through_member_ptrs (info, parms_info, args, call))
995 return;
997 /* Finally, let's check whether we actually pass a new constant member
998 pointer here... */
999 compute_cst_member_ptr_arguments (args, call);
1002 /* Compute jump functions for all edges - both direct and indirect - outgoing
1003 from NODE. Also count the actual arguments in the process. */
1005 static void
1006 ipa_compute_jump_functions (struct cgraph_node *node,
1007 struct param_analysis_info *parms_info)
1009 struct cgraph_edge *cs;
1011 for (cs = node->callees; cs; cs = cs->next_callee)
1013 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1014 NULL);
1015 /* We do not need to bother analyzing calls to unknown
1016 functions unless they may become known during lto/whopr. */
1017 if (!callee->analyzed && !flag_lto)
1018 continue;
1019 ipa_compute_jump_functions_for_edge (parms_info, cs);
1022 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1023 ipa_compute_jump_functions_for_edge (parms_info, cs);
1026 /* If RHS looks like a rhs of a statement loading pfn from a member
1027 pointer formal parameter, return the parameter, otherwise return
1028 NULL. If USE_DELTA, then we look for a use of the delta field
1029 rather than the pfn. */
1031 static tree
1032 ipa_get_member_ptr_load_param (tree rhs, bool use_delta)
1034 tree rec, ref_field, ref_offset, fld, fld_offset, ptr_field, delta_field;
1036 if (TREE_CODE (rhs) == COMPONENT_REF)
1038 ref_field = TREE_OPERAND (rhs, 1);
1039 rhs = TREE_OPERAND (rhs, 0);
1041 else
1042 ref_field = NULL_TREE;
1043 if (TREE_CODE (rhs) != MEM_REF)
1044 return NULL_TREE;
1045 rec = TREE_OPERAND (rhs, 0);
1046 if (TREE_CODE (rec) != ADDR_EXPR)
1047 return NULL_TREE;
1048 rec = TREE_OPERAND (rec, 0);
1049 if (TREE_CODE (rec) != PARM_DECL
1050 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1051 return NULL_TREE;
1053 ref_offset = TREE_OPERAND (rhs, 1);
1055 if (ref_field)
1057 if (integer_nonzerop (ref_offset))
1058 return NULL_TREE;
1060 if (use_delta)
1061 fld = delta_field;
1062 else
1063 fld = ptr_field;
1065 return ref_field == fld ? rec : NULL_TREE;
1068 if (use_delta)
1069 fld_offset = byte_position (delta_field);
1070 else
1071 fld_offset = byte_position (ptr_field);
1073 return tree_int_cst_equal (ref_offset, fld_offset) ? rec : NULL_TREE;
1076 /* If STMT looks like a statement loading a value from a member pointer formal
1077 parameter, this function returns that parameter. */
1079 static tree
1080 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta)
1082 tree rhs;
1084 if (!gimple_assign_single_p (stmt))
1085 return NULL_TREE;
1087 rhs = gimple_assign_rhs1 (stmt);
1088 return ipa_get_member_ptr_load_param (rhs, use_delta);
1091 /* Returns true iff T is an SSA_NAME defined by a statement. */
1093 static bool
1094 ipa_is_ssa_with_stmt_def (tree t)
1096 if (TREE_CODE (t) == SSA_NAME
1097 && !SSA_NAME_IS_DEFAULT_DEF (t))
1098 return true;
1099 else
1100 return false;
1103 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1104 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1105 indirect call graph edge. */
1107 static struct cgraph_edge *
1108 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1110 struct cgraph_edge *cs;
1112 cs = cgraph_edge (node, stmt);
1113 cs->indirect_info->param_index = param_index;
1114 cs->indirect_info->anc_offset = 0;
1115 cs->indirect_info->polymorphic = 0;
1116 return cs;
1119 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1120 (described by INFO). PARMS_INFO is a pointer to a vector containing
1121 intermediate information about each formal parameter. Currently it checks
1122 whether the call calls a pointer that is a formal parameter and if so, the
1123 parameter is marked with the called flag and an indirect call graph edge
1124 describing the call is created. This is very simple for ordinary pointers
1125 represented in SSA but not-so-nice when it comes to member pointers. The
1126 ugly part of this function does nothing more than trying to match the
1127 pattern of such a call. An example of such a pattern is the gimple dump
1128 below, the call is on the last line:
1130 <bb 2>:
1131 f$__delta_5 = f.__delta;
1132 f$__pfn_24 = f.__pfn;
1135 <bb 2>:
1136 f$__delta_5 = MEM[(struct *)&f];
1137 f$__pfn_24 = MEM[(struct *)&f + 4B];
1139 and a few lines below:
1141 <bb 5>
1142 D.2496_3 = (int) f$__pfn_24;
1143 D.2497_4 = D.2496_3 & 1;
1144 if (D.2497_4 != 0)
1145 goto <bb 3>;
1146 else
1147 goto <bb 4>;
1149 <bb 6>:
1150 D.2500_7 = (unsigned int) f$__delta_5;
1151 D.2501_8 = &S + D.2500_7;
1152 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1153 D.2503_10 = *D.2502_9;
1154 D.2504_12 = f$__pfn_24 + -1;
1155 D.2505_13 = (unsigned int) D.2504_12;
1156 D.2506_14 = D.2503_10 + D.2505_13;
1157 D.2507_15 = *D.2506_14;
1158 iftmp.11_16 = (String:: *) D.2507_15;
1160 <bb 7>:
1161 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1162 D.2500_19 = (unsigned int) f$__delta_5;
1163 D.2508_20 = &S + D.2500_19;
1164 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1166 Such patterns are results of simple calls to a member pointer:
1168 int doprinting (int (MyString::* f)(int) const)
1170 MyString S ("somestring");
1172 return (S.*f)(4);
1176 static void
1177 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1178 struct ipa_node_params *info,
1179 struct param_analysis_info *parms_info,
1180 gimple call, tree target)
1182 gimple def;
1183 tree n1, n2;
1184 gimple d1, d2;
1185 tree rec, rec2, cond;
1186 gimple branch;
1187 int index;
1188 basic_block bb, virt_bb, join;
1190 if (SSA_NAME_IS_DEFAULT_DEF (target))
1192 tree var = SSA_NAME_VAR (target);
1193 index = ipa_get_param_decl_index (info, var);
1194 if (index >= 0)
1195 ipa_note_param_call (node, index, call);
1196 return;
1199 /* Now we need to try to match the complex pattern of calling a member
1200 pointer. */
1202 if (!POINTER_TYPE_P (TREE_TYPE (target))
1203 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1204 return;
1206 def = SSA_NAME_DEF_STMT (target);
1207 if (gimple_code (def) != GIMPLE_PHI)
1208 return;
1210 if (gimple_phi_num_args (def) != 2)
1211 return;
1213 /* First, we need to check whether one of these is a load from a member
1214 pointer that is a parameter to this function. */
1215 n1 = PHI_ARG_DEF (def, 0);
1216 n2 = PHI_ARG_DEF (def, 1);
1217 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1218 return;
1219 d1 = SSA_NAME_DEF_STMT (n1);
1220 d2 = SSA_NAME_DEF_STMT (n2);
1222 join = gimple_bb (def);
1223 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
1225 if (ipa_get_stmt_member_ptr_load_param (d2, false))
1226 return;
1228 bb = EDGE_PRED (join, 0)->src;
1229 virt_bb = gimple_bb (d2);
1231 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
1233 bb = EDGE_PRED (join, 1)->src;
1234 virt_bb = gimple_bb (d1);
1236 else
1237 return;
1239 /* Second, we need to check that the basic blocks are laid out in the way
1240 corresponding to the pattern. */
1242 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1243 || single_pred (virt_bb) != bb
1244 || single_succ (virt_bb) != join)
1245 return;
1247 /* Third, let's see that the branching is done depending on the least
1248 significant bit of the pfn. */
1250 branch = last_stmt (bb);
1251 if (!branch || gimple_code (branch) != GIMPLE_COND)
1252 return;
1254 if ((gimple_cond_code (branch) != NE_EXPR
1255 && gimple_cond_code (branch) != EQ_EXPR)
1256 || !integer_zerop (gimple_cond_rhs (branch)))
1257 return;
1259 cond = gimple_cond_lhs (branch);
1260 if (!ipa_is_ssa_with_stmt_def (cond))
1261 return;
1263 def = SSA_NAME_DEF_STMT (cond);
1264 if (!is_gimple_assign (def)
1265 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1266 || !integer_onep (gimple_assign_rhs2 (def)))
1267 return;
1269 cond = gimple_assign_rhs1 (def);
1270 if (!ipa_is_ssa_with_stmt_def (cond))
1271 return;
1273 def = SSA_NAME_DEF_STMT (cond);
1275 if (is_gimple_assign (def)
1276 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1278 cond = gimple_assign_rhs1 (def);
1279 if (!ipa_is_ssa_with_stmt_def (cond))
1280 return;
1281 def = SSA_NAME_DEF_STMT (cond);
1284 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1285 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1286 == ptrmemfunc_vbit_in_delta));
1288 if (rec != rec2)
1289 return;
1291 index = ipa_get_param_decl_index (info, rec);
1292 if (index >= 0 && !is_parm_modified_before_call (&parms_info[index],
1293 call, rec))
1294 ipa_note_param_call (node, index, call);
1296 return;
1299 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1300 object referenced in the expression is a formal parameter of the caller
1301 (described by INFO), create a call note for the statement. */
1303 static void
1304 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1305 struct ipa_node_params *info, gimple call,
1306 tree target)
1308 struct cgraph_edge *cs;
1309 struct cgraph_indirect_call_info *ii;
1310 struct ipa_jump_func jfunc;
1311 tree obj = OBJ_TYPE_REF_OBJECT (target);
1312 int index;
1313 HOST_WIDE_INT anc_offset;
1315 if (!flag_devirtualize)
1316 return;
1318 if (TREE_CODE (obj) != SSA_NAME)
1319 return;
1321 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1323 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1324 return;
1326 anc_offset = 0;
1327 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1328 gcc_assert (index >= 0);
1329 if (detect_type_change_ssa (obj, call, &jfunc))
1330 return;
1332 else
1334 gimple stmt = SSA_NAME_DEF_STMT (obj);
1335 tree expr;
1337 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1338 if (!expr)
1339 return;
1340 index = ipa_get_param_decl_index (info,
1341 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1342 gcc_assert (index >= 0);
1343 if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1344 return;
1347 cs = ipa_note_param_call (node, index, call);
1348 ii = cs->indirect_info;
1349 ii->anc_offset = anc_offset;
1350 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1351 ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1352 ii->polymorphic = 1;
1355 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1356 of the caller (described by INFO). PARMS_INFO is a pointer to a vector
1357 containing intermediate information about each formal parameter. */
1359 static void
1360 ipa_analyze_call_uses (struct cgraph_node *node,
1361 struct ipa_node_params *info,
1362 struct param_analysis_info *parms_info, gimple call)
1364 tree target = gimple_call_fn (call);
1366 if (!target)
1367 return;
1368 if (TREE_CODE (target) == SSA_NAME)
1369 ipa_analyze_indirect_call_uses (node, info, parms_info, call, target);
1370 else if (TREE_CODE (target) == OBJ_TYPE_REF)
1371 ipa_analyze_virtual_call_uses (node, info, call, target);
1375 /* Analyze the call statement STMT with respect to formal parameters (described
1376 in INFO) of caller given by NODE. Currently it only checks whether formal
1377 parameters are called. PARMS_INFO is a pointer to a vector containing
1378 intermediate information about each formal parameter. */
1380 static void
1381 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1382 struct param_analysis_info *parms_info, gimple stmt)
1384 if (is_gimple_call (stmt))
1385 ipa_analyze_call_uses (node, info, parms_info, stmt);
1388 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1389 If OP is a parameter declaration, mark it as used in the info structure
1390 passed in DATA. */
1392 static bool
1393 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
1394 tree op, void *data)
1396 struct ipa_node_params *info = (struct ipa_node_params *) data;
1398 op = get_base_address (op);
1399 if (op
1400 && TREE_CODE (op) == PARM_DECL)
1402 int index = ipa_get_param_decl_index (info, op);
1403 gcc_assert (index >= 0);
1404 ipa_set_param_used (info, index, true);
1407 return false;
1410 /* Scan the function body of NODE and inspect the uses of formal parameters.
1411 Store the findings in various structures of the associated ipa_node_params
1412 structure, such as parameter flags, notes etc. PARMS_INFO is a pointer to a
1413 vector containing intermediate information about each formal parameter. */
1415 static void
1416 ipa_analyze_params_uses (struct cgraph_node *node,
1417 struct param_analysis_info *parms_info)
1419 tree decl = node->decl;
1420 basic_block bb;
1421 struct function *func;
1422 gimple_stmt_iterator gsi;
1423 struct ipa_node_params *info = IPA_NODE_REF (node);
1424 int i;
1426 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1427 return;
1429 for (i = 0; i < ipa_get_param_count (info); i++)
1431 tree parm = ipa_get_param (info, i);
1432 /* For SSA regs see if parameter is used. For non-SSA we compute
1433 the flag during modification analysis. */
1434 if (is_gimple_reg (parm)
1435 && gimple_default_def (DECL_STRUCT_FUNCTION (node->decl), parm))
1436 ipa_set_param_used (info, i, true);
1439 func = DECL_STRUCT_FUNCTION (decl);
1440 FOR_EACH_BB_FN (bb, func)
1442 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1444 gimple stmt = gsi_stmt (gsi);
1446 if (is_gimple_debug (stmt))
1447 continue;
1449 ipa_analyze_stmt_uses (node, info, parms_info, stmt);
1450 walk_stmt_load_store_addr_ops (stmt, info,
1451 visit_ref_for_mod_analysis,
1452 visit_ref_for_mod_analysis,
1453 visit_ref_for_mod_analysis);
1455 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
1456 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
1457 visit_ref_for_mod_analysis,
1458 visit_ref_for_mod_analysis,
1459 visit_ref_for_mod_analysis);
1462 info->uses_analysis_done = 1;
1465 /* Initialize the array describing properties of of formal parameters
1466 of NODE, analyze their uses and compute jump functions associated
1467 with actual arguments of calls from within NODE. */
1469 void
1470 ipa_analyze_node (struct cgraph_node *node)
1472 struct ipa_node_params *info;
1473 struct param_analysis_info *parms_info;
1474 int i, param_count;
1476 ipa_check_create_node_params ();
1477 ipa_check_create_edge_args ();
1478 info = IPA_NODE_REF (node);
1479 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1480 current_function_decl = node->decl;
1481 ipa_initialize_node_params (node);
1483 param_count = ipa_get_param_count (info);
1484 parms_info = XALLOCAVEC (struct param_analysis_info, param_count);
1485 memset (parms_info, 0, sizeof (struct param_analysis_info) * param_count);
1487 ipa_analyze_params_uses (node, parms_info);
1488 ipa_compute_jump_functions (node, parms_info);
1490 for (i = 0; i < param_count; i++)
1491 if (parms_info[i].visited_statements)
1492 BITMAP_FREE (parms_info[i].visited_statements);
1494 current_function_decl = NULL;
1495 pop_cfun ();
1499 /* Update the jump function DST when the call graph edge corresponding to SRC is
1500 is being inlined, knowing that DST is of type ancestor and src of known
1501 type. */
1503 static void
1504 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
1505 struct ipa_jump_func *dst)
1507 tree new_binfo;
1509 new_binfo = get_binfo_at_offset (src->value.base_binfo,
1510 dst->value.ancestor.offset,
1511 dst->value.ancestor.type);
1512 if (new_binfo)
1514 dst->type = IPA_JF_KNOWN_TYPE;
1515 dst->value.base_binfo = new_binfo;
1517 else
1518 dst->type = IPA_JF_UNKNOWN;
1521 /* Update the jump functions associated with call graph edge E when the call
1522 graph edge CS is being inlined, assuming that E->caller is already (possibly
1523 indirectly) inlined into CS->callee and that E has not been inlined. */
1525 static void
1526 update_jump_functions_after_inlining (struct cgraph_edge *cs,
1527 struct cgraph_edge *e)
1529 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1530 struct ipa_edge_args *args = IPA_EDGE_REF (e);
1531 int count = ipa_get_cs_argument_count (args);
1532 int i;
1534 for (i = 0; i < count; i++)
1536 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
1538 if (dst->type == IPA_JF_ANCESTOR)
1540 struct ipa_jump_func *src;
1542 /* Variable number of arguments can cause havoc if we try to access
1543 one that does not exist in the inlined edge. So make sure we
1544 don't. */
1545 if (dst->value.ancestor.formal_id >= ipa_get_cs_argument_count (top))
1547 dst->type = IPA_JF_UNKNOWN;
1548 continue;
1551 src = ipa_get_ith_jump_func (top, dst->value.ancestor.formal_id);
1552 if (src->type == IPA_JF_KNOWN_TYPE)
1553 combine_known_type_and_ancestor_jfs (src, dst);
1554 else if (src->type == IPA_JF_PASS_THROUGH
1555 && src->value.pass_through.operation == NOP_EXPR)
1556 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
1557 else if (src->type == IPA_JF_ANCESTOR)
1559 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
1560 dst->value.ancestor.offset += src->value.ancestor.offset;
1562 else
1563 dst->type = IPA_JF_UNKNOWN;
1565 else if (dst->type == IPA_JF_PASS_THROUGH)
1567 struct ipa_jump_func *src;
1568 /* We must check range due to calls with variable number of arguments
1569 and we cannot combine jump functions with operations. */
1570 if (dst->value.pass_through.operation == NOP_EXPR
1571 && (dst->value.pass_through.formal_id
1572 < ipa_get_cs_argument_count (top)))
1574 src = ipa_get_ith_jump_func (top,
1575 dst->value.pass_through.formal_id);
1576 *dst = *src;
1578 else
1579 dst->type = IPA_JF_UNKNOWN;
1584 /* If TARGET is an addr_expr of a function declaration, make it the destination
1585 of an indirect edge IE and return the edge. Otherwise, return NULL. */
1587 struct cgraph_edge *
1588 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
1590 struct cgraph_node *callee;
1592 if (TREE_CODE (target) == ADDR_EXPR)
1593 target = TREE_OPERAND (target, 0);
1594 if (TREE_CODE (target) != FUNCTION_DECL)
1595 return NULL;
1596 callee = cgraph_get_node (target);
1597 if (!callee)
1598 return NULL;
1599 ipa_check_create_node_params ();
1601 /* We can not make edges to inline clones. It is bug that someone removed
1602 the cgraph node too early. */
1603 gcc_assert (!callee->global.inlined_to);
1605 cgraph_make_edge_direct (ie, callee);
1606 if (dump_file)
1608 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
1609 "(%s/%i -> %s/%i), for stmt ",
1610 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
1611 cgraph_node_name (ie->caller), ie->caller->uid,
1612 cgraph_node_name (ie->callee), ie->callee->uid);
1613 if (ie->call_stmt)
1614 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
1615 else
1616 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
1618 callee = cgraph_function_or_thunk_node (callee, NULL);
1620 return ie;
1623 /* Try to find a destination for indirect edge IE that corresponds to a simple
1624 call or a call of a member function pointer and where the destination is a
1625 pointer formal parameter described by jump function JFUNC. If it can be
1626 determined, return the newly direct edge, otherwise return NULL. */
1628 static struct cgraph_edge *
1629 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
1630 struct ipa_jump_func *jfunc)
1632 tree target;
1634 if (jfunc->type == IPA_JF_CONST)
1635 target = jfunc->value.constant;
1636 else if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1637 target = jfunc->value.member_cst.pfn;
1638 else
1639 return NULL;
1641 return ipa_make_edge_direct_to_target (ie, target);
1644 /* Try to find a destination for indirect edge IE that corresponds to a
1645 virtual call based on a formal parameter which is described by jump
1646 function JFUNC and if it can be determined, make it direct and return the
1647 direct edge. Otherwise, return NULL. */
1649 static struct cgraph_edge *
1650 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
1651 struct ipa_jump_func *jfunc)
1653 tree binfo, type, target;
1654 HOST_WIDE_INT token;
1656 if (jfunc->type == IPA_JF_KNOWN_TYPE)
1657 binfo = jfunc->value.base_binfo;
1658 else
1659 return NULL;
1661 if (!binfo)
1662 return NULL;
1664 token = ie->indirect_info->otr_token;
1665 type = ie->indirect_info->otr_type;
1666 binfo = get_binfo_at_offset (binfo, ie->indirect_info->anc_offset, type);
1667 if (binfo)
1668 target = gimple_get_virt_method_for_binfo (token, binfo);
1669 else
1670 return NULL;
1672 if (target)
1673 return ipa_make_edge_direct_to_target (ie, target);
1674 else
1675 return NULL;
1678 /* Update the param called notes associated with NODE when CS is being inlined,
1679 assuming NODE is (potentially indirectly) inlined into CS->callee.
1680 Moreover, if the callee is discovered to be constant, create a new cgraph
1681 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1682 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1684 static bool
1685 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
1686 struct cgraph_node *node,
1687 VEC (cgraph_edge_p, heap) **new_edges)
1689 struct ipa_edge_args *top;
1690 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
1691 bool res = false;
1693 ipa_check_create_edge_args ();
1694 top = IPA_EDGE_REF (cs);
1696 for (ie = node->indirect_calls; ie; ie = next_ie)
1698 struct cgraph_indirect_call_info *ici = ie->indirect_info;
1699 struct ipa_jump_func *jfunc;
1701 next_ie = ie->next_callee;
1702 if (bitmap_bit_p (iinlining_processed_edges, ie->uid))
1703 continue;
1705 /* If we ever use indirect edges for anything other than indirect
1706 inlining, we will need to skip those with negative param_indices. */
1707 if (ici->param_index == -1)
1708 continue;
1710 /* We must check range due to calls with variable number of arguments: */
1711 if (ici->param_index >= ipa_get_cs_argument_count (top))
1713 bitmap_set_bit (iinlining_processed_edges, ie->uid);
1714 continue;
1717 jfunc = ipa_get_ith_jump_func (top, ici->param_index);
1718 if (jfunc->type == IPA_JF_PASS_THROUGH
1719 && jfunc->value.pass_through.operation == NOP_EXPR)
1720 ici->param_index = jfunc->value.pass_through.formal_id;
1721 else if (jfunc->type == IPA_JF_ANCESTOR)
1723 ici->param_index = jfunc->value.ancestor.formal_id;
1724 ici->anc_offset += jfunc->value.ancestor.offset;
1726 else
1727 /* Either we can find a destination for this edge now or never. */
1728 bitmap_set_bit (iinlining_processed_edges, ie->uid);
1730 if (ici->polymorphic)
1731 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc);
1732 else
1733 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc);
1735 if (new_direct_edge)
1737 new_direct_edge->indirect_inlining_edge = 1;
1738 if (new_edges)
1740 VEC_safe_push (cgraph_edge_p, heap, *new_edges,
1741 new_direct_edge);
1742 top = IPA_EDGE_REF (cs);
1743 res = true;
1748 return res;
1751 /* Recursively traverse subtree of NODE (including node) made of inlined
1752 cgraph_edges when CS has been inlined and invoke
1753 update_indirect_edges_after_inlining on all nodes and
1754 update_jump_functions_after_inlining on all non-inlined edges that lead out
1755 of this subtree. Newly discovered indirect edges will be added to
1756 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1757 created. */
1759 static bool
1760 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1761 struct cgraph_node *node,
1762 VEC (cgraph_edge_p, heap) **new_edges)
1764 struct cgraph_edge *e;
1765 bool res;
1767 res = update_indirect_edges_after_inlining (cs, node, new_edges);
1769 for (e = node->callees; e; e = e->next_callee)
1770 if (!e->inline_failed)
1771 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1772 else
1773 update_jump_functions_after_inlining (cs, e);
1775 return res;
1778 /* Update jump functions and call note functions on inlining the call site CS.
1779 CS is expected to lead to a node already cloned by
1780 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1781 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1782 created. */
1784 bool
1785 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1786 VEC (cgraph_edge_p, heap) **new_edges)
1788 /* Do nothing if the preparation phase has not been carried out yet
1789 (i.e. during early inlining). */
1790 if (!ipa_node_params_vector)
1791 return false;
1792 gcc_assert (ipa_edge_args_vector);
1794 return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1797 /* Frees all dynamically allocated structures that the argument info points
1798 to. */
1800 void
1801 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1803 if (args->jump_functions)
1804 ggc_free (args->jump_functions);
1806 memset (args, 0, sizeof (*args));
1809 /* Free all ipa_edge structures. */
1811 void
1812 ipa_free_all_edge_args (void)
1814 int i;
1815 struct ipa_edge_args *args;
1817 FOR_EACH_VEC_ELT (ipa_edge_args_t, ipa_edge_args_vector, i, args)
1818 ipa_free_edge_args_substructures (args);
1820 VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1821 ipa_edge_args_vector = NULL;
1824 /* Frees all dynamically allocated structures that the param info points
1825 to. */
1827 void
1828 ipa_free_node_params_substructures (struct ipa_node_params *info)
1830 VEC_free (ipa_param_descriptor_t, heap, info->descriptors);
1831 free (info->lattices);
1832 /* Lattice values and their sources are deallocated with their alocation
1833 pool. */
1834 VEC_free (tree, heap, info->known_vals);
1835 memset (info, 0, sizeof (*info));
1838 /* Free all ipa_node_params structures. */
1840 void
1841 ipa_free_all_node_params (void)
1843 int i;
1844 struct ipa_node_params *info;
1846 FOR_EACH_VEC_ELT (ipa_node_params_t, ipa_node_params_vector, i, info)
1847 ipa_free_node_params_substructures (info);
1849 VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1850 ipa_node_params_vector = NULL;
1853 /* Hook that is called by cgraph.c when an edge is removed. */
1855 static void
1856 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1858 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1859 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1860 <= (unsigned)cs->uid)
1861 return;
1862 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1865 /* Hook that is called by cgraph.c when a node is removed. */
1867 static void
1868 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1870 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1871 if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1872 <= (unsigned)node->uid)
1873 return;
1874 ipa_free_node_params_substructures (IPA_NODE_REF (node));
1877 /* Hook that is called by cgraph.c when a node is duplicated. */
1879 static void
1880 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1881 __attribute__((unused)) void *data)
1883 struct ipa_edge_args *old_args, *new_args;
1885 ipa_check_create_edge_args ();
1887 old_args = IPA_EDGE_REF (src);
1888 new_args = IPA_EDGE_REF (dst);
1890 new_args->jump_functions = VEC_copy (ipa_jump_func_t, gc,
1891 old_args->jump_functions);
1893 if (iinlining_processed_edges
1894 && bitmap_bit_p (iinlining_processed_edges, src->uid))
1895 bitmap_set_bit (iinlining_processed_edges, dst->uid);
1898 /* Hook that is called by cgraph.c when a node is duplicated. */
1900 static void
1901 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1902 ATTRIBUTE_UNUSED void *data)
1904 struct ipa_node_params *old_info, *new_info;
1906 ipa_check_create_node_params ();
1907 old_info = IPA_NODE_REF (src);
1908 new_info = IPA_NODE_REF (dst);
1910 new_info->descriptors = VEC_copy (ipa_param_descriptor_t, heap,
1911 old_info->descriptors);
1912 new_info->lattices = NULL;
1913 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1915 new_info->uses_analysis_done = old_info->uses_analysis_done;
1916 new_info->node_enqueued = old_info->node_enqueued;
1920 /* Analyze newly added function into callgraph. */
1922 static void
1923 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1925 ipa_analyze_node (node);
1928 /* Register our cgraph hooks if they are not already there. */
1930 void
1931 ipa_register_cgraph_hooks (void)
1933 if (!edge_removal_hook_holder)
1934 edge_removal_hook_holder =
1935 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1936 if (!node_removal_hook_holder)
1937 node_removal_hook_holder =
1938 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1939 if (!edge_duplication_hook_holder)
1940 edge_duplication_hook_holder =
1941 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1942 if (!node_duplication_hook_holder)
1943 node_duplication_hook_holder =
1944 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1945 function_insertion_hook_holder =
1946 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
1949 /* Unregister our cgraph hooks if they are not already there. */
1951 static void
1952 ipa_unregister_cgraph_hooks (void)
1954 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1955 edge_removal_hook_holder = NULL;
1956 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1957 node_removal_hook_holder = NULL;
1958 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1959 edge_duplication_hook_holder = NULL;
1960 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1961 node_duplication_hook_holder = NULL;
1962 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
1963 function_insertion_hook_holder = NULL;
1966 /* Allocate all necessary data structures necessary for indirect inlining. */
1968 void
1969 ipa_create_all_structures_for_iinln (void)
1971 iinlining_processed_edges = BITMAP_ALLOC (NULL);
1974 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1975 longer needed after ipa-cp. */
1977 void
1978 ipa_free_all_structures_after_ipa_cp (void)
1980 if (!flag_indirect_inlining)
1982 ipa_free_all_edge_args ();
1983 ipa_free_all_node_params ();
1984 free_alloc_pool (ipcp_sources_pool);
1985 free_alloc_pool (ipcp_values_pool);
1986 ipa_unregister_cgraph_hooks ();
1990 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1991 longer needed after indirect inlining. */
1993 void
1994 ipa_free_all_structures_after_iinln (void)
1996 BITMAP_FREE (iinlining_processed_edges);
1998 ipa_free_all_edge_args ();
1999 ipa_free_all_node_params ();
2000 ipa_unregister_cgraph_hooks ();
2001 if (ipcp_sources_pool)
2002 free_alloc_pool (ipcp_sources_pool);
2003 if (ipcp_values_pool)
2004 free_alloc_pool (ipcp_values_pool);
2007 /* Print ipa_tree_map data structures of all functions in the
2008 callgraph to F. */
2010 void
2011 ipa_print_node_params (FILE * f, struct cgraph_node *node)
2013 int i, count;
2014 tree temp;
2015 struct ipa_node_params *info;
2017 if (!node->analyzed)
2018 return;
2019 info = IPA_NODE_REF (node);
2020 fprintf (f, " function %s parameter descriptors:\n",
2021 cgraph_node_name (node));
2022 count = ipa_get_param_count (info);
2023 for (i = 0; i < count; i++)
2025 temp = ipa_get_param (info, i);
2026 if (TREE_CODE (temp) == PARM_DECL)
2027 fprintf (f, " param %d : %s", i,
2028 (DECL_NAME (temp)
2029 ? (*lang_hooks.decl_printable_name) (temp, 2)
2030 : "(unnamed)"));
2031 if (ipa_is_param_used (info, i))
2032 fprintf (f, " used");
2033 fprintf (f, "\n");
2037 /* Print ipa_tree_map data structures of all functions in the
2038 callgraph to F. */
2040 void
2041 ipa_print_all_params (FILE * f)
2043 struct cgraph_node *node;
2045 fprintf (f, "\nFunction parameters:\n");
2046 for (node = cgraph_nodes; node; node = node->next)
2047 ipa_print_node_params (f, node);
2050 /* Return a heap allocated vector containing formal parameters of FNDECL. */
2052 VEC(tree, heap) *
2053 ipa_get_vector_of_formal_parms (tree fndecl)
2055 VEC(tree, heap) *args;
2056 int count;
2057 tree parm;
2059 count = count_formal_params (fndecl);
2060 args = VEC_alloc (tree, heap, count);
2061 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2062 VEC_quick_push (tree, args, parm);
2064 return args;
2067 /* Return a heap allocated vector containing types of formal parameters of
2068 function type FNTYPE. */
2070 static inline VEC(tree, heap) *
2071 get_vector_of_formal_parm_types (tree fntype)
2073 VEC(tree, heap) *types;
2074 int count = 0;
2075 tree t;
2077 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2078 count++;
2080 types = VEC_alloc (tree, heap, count);
2081 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2082 VEC_quick_push (tree, types, TREE_VALUE (t));
2084 return types;
2087 /* Modify the function declaration FNDECL and its type according to the plan in
2088 ADJUSTMENTS. It also sets base fields of individual adjustments structures
2089 to reflect the actual parameters being modified which are determined by the
2090 base_index field. */
2092 void
2093 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
2094 const char *synth_parm_prefix)
2096 VEC(tree, heap) *oparms, *otypes;
2097 tree orig_type, new_type = NULL;
2098 tree old_arg_types, t, new_arg_types = NULL;
2099 tree parm, *link = &DECL_ARGUMENTS (fndecl);
2100 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2101 tree new_reversed = NULL;
2102 bool care_for_types, last_parm_void;
2104 if (!synth_parm_prefix)
2105 synth_parm_prefix = "SYNTH";
2107 oparms = ipa_get_vector_of_formal_parms (fndecl);
2108 orig_type = TREE_TYPE (fndecl);
2109 old_arg_types = TYPE_ARG_TYPES (orig_type);
2111 /* The following test is an ugly hack, some functions simply don't have any
2112 arguments in their type. This is probably a bug but well... */
2113 care_for_types = (old_arg_types != NULL_TREE);
2114 if (care_for_types)
2116 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
2117 == void_type_node);
2118 otypes = get_vector_of_formal_parm_types (orig_type);
2119 if (last_parm_void)
2120 gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
2121 else
2122 gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
2124 else
2126 last_parm_void = false;
2127 otypes = NULL;
2130 for (i = 0; i < len; i++)
2132 struct ipa_parm_adjustment *adj;
2133 gcc_assert (link);
2135 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2136 parm = VEC_index (tree, oparms, adj->base_index);
2137 adj->base = parm;
2139 if (adj->copy_param)
2141 if (care_for_types)
2142 new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
2143 adj->base_index),
2144 new_arg_types);
2145 *link = parm;
2146 link = &DECL_CHAIN (parm);
2148 else if (!adj->remove_param)
2150 tree new_parm;
2151 tree ptype;
2153 if (adj->by_ref)
2154 ptype = build_pointer_type (adj->type);
2155 else
2156 ptype = adj->type;
2158 if (care_for_types)
2159 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
2161 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
2162 ptype);
2163 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
2165 DECL_ARTIFICIAL (new_parm) = 1;
2166 DECL_ARG_TYPE (new_parm) = ptype;
2167 DECL_CONTEXT (new_parm) = fndecl;
2168 TREE_USED (new_parm) = 1;
2169 DECL_IGNORED_P (new_parm) = 1;
2170 layout_decl (new_parm, 0);
2172 add_referenced_var (new_parm);
2173 mark_sym_for_renaming (new_parm);
2174 adj->base = parm;
2175 adj->reduction = new_parm;
2177 *link = new_parm;
2179 link = &DECL_CHAIN (new_parm);
2183 *link = NULL_TREE;
2185 if (care_for_types)
2187 new_reversed = nreverse (new_arg_types);
2188 if (last_parm_void)
2190 if (new_reversed)
2191 TREE_CHAIN (new_arg_types) = void_list_node;
2192 else
2193 new_reversed = void_list_node;
2197 /* Use copy_node to preserve as much as possible from original type
2198 (debug info, attribute lists etc.)
2199 Exception is METHOD_TYPEs must have THIS argument.
2200 When we are asked to remove it, we need to build new FUNCTION_TYPE
2201 instead. */
2202 if (TREE_CODE (orig_type) != METHOD_TYPE
2203 || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
2204 && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
2206 new_type = build_distinct_type_copy (orig_type);
2207 TYPE_ARG_TYPES (new_type) = new_reversed;
2209 else
2211 new_type
2212 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
2213 new_reversed));
2214 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
2215 DECL_VINDEX (fndecl) = NULL_TREE;
2218 /* When signature changes, we need to clear builtin info. */
2219 if (DECL_BUILT_IN (fndecl))
2221 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
2222 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
2225 /* This is a new type, not a copy of an old type. Need to reassociate
2226 variants. We can handle everything except the main variant lazily. */
2227 t = TYPE_MAIN_VARIANT (orig_type);
2228 if (orig_type != t)
2230 TYPE_MAIN_VARIANT (new_type) = t;
2231 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
2232 TYPE_NEXT_VARIANT (t) = new_type;
2234 else
2236 TYPE_MAIN_VARIANT (new_type) = new_type;
2237 TYPE_NEXT_VARIANT (new_type) = NULL;
2240 TREE_TYPE (fndecl) = new_type;
2241 DECL_VIRTUAL_P (fndecl) = 0;
2242 if (otypes)
2243 VEC_free (tree, heap, otypes);
2244 VEC_free (tree, heap, oparms);
2247 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
2248 If this is a directly recursive call, CS must be NULL. Otherwise it must
2249 contain the corresponding call graph edge. */
2251 void
2252 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
2253 ipa_parm_adjustment_vec adjustments)
2255 VEC(tree, heap) *vargs;
2256 VEC(tree, gc) **debug_args = NULL;
2257 gimple new_stmt;
2258 gimple_stmt_iterator gsi;
2259 tree callee_decl;
2260 int i, len;
2262 len = VEC_length (ipa_parm_adjustment_t, adjustments);
2263 vargs = VEC_alloc (tree, heap, len);
2264 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
2266 gsi = gsi_for_stmt (stmt);
2267 for (i = 0; i < len; i++)
2269 struct ipa_parm_adjustment *adj;
2271 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2273 if (adj->copy_param)
2275 tree arg = gimple_call_arg (stmt, adj->base_index);
2277 VEC_quick_push (tree, vargs, arg);
2279 else if (!adj->remove_param)
2281 tree expr, base, off;
2282 location_t loc;
2284 /* We create a new parameter out of the value of the old one, we can
2285 do the following kind of transformations:
2287 - A scalar passed by reference is converted to a scalar passed by
2288 value. (adj->by_ref is false and the type of the original
2289 actual argument is a pointer to a scalar).
2291 - A part of an aggregate is passed instead of the whole aggregate.
2292 The part can be passed either by value or by reference, this is
2293 determined by value of adj->by_ref. Moreover, the code below
2294 handles both situations when the original aggregate is passed by
2295 value (its type is not a pointer) and when it is passed by
2296 reference (it is a pointer to an aggregate).
2298 When the new argument is passed by reference (adj->by_ref is true)
2299 it must be a part of an aggregate and therefore we form it by
2300 simply taking the address of a reference inside the original
2301 aggregate. */
2303 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
2304 base = gimple_call_arg (stmt, adj->base_index);
2305 loc = EXPR_LOCATION (base);
2307 if (TREE_CODE (base) != ADDR_EXPR
2308 && POINTER_TYPE_P (TREE_TYPE (base)))
2309 off = build_int_cst (adj->alias_ptr_type,
2310 adj->offset / BITS_PER_UNIT);
2311 else
2313 HOST_WIDE_INT base_offset;
2314 tree prev_base;
2316 if (TREE_CODE (base) == ADDR_EXPR)
2317 base = TREE_OPERAND (base, 0);
2318 prev_base = base;
2319 base = get_addr_base_and_unit_offset (base, &base_offset);
2320 /* Aggregate arguments can have non-invariant addresses. */
2321 if (!base)
2323 base = build_fold_addr_expr (prev_base);
2324 off = build_int_cst (adj->alias_ptr_type,
2325 adj->offset / BITS_PER_UNIT);
2327 else if (TREE_CODE (base) == MEM_REF)
2329 off = build_int_cst (adj->alias_ptr_type,
2330 base_offset
2331 + adj->offset / BITS_PER_UNIT);
2332 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
2333 off);
2334 base = TREE_OPERAND (base, 0);
2336 else
2338 off = build_int_cst (adj->alias_ptr_type,
2339 base_offset
2340 + adj->offset / BITS_PER_UNIT);
2341 base = build_fold_addr_expr (base);
2345 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
2346 if (adj->by_ref)
2347 expr = build_fold_addr_expr (expr);
2349 expr = force_gimple_operand_gsi (&gsi, expr,
2350 adj->by_ref
2351 || is_gimple_reg_type (adj->type),
2352 NULL, true, GSI_SAME_STMT);
2353 VEC_quick_push (tree, vargs, expr);
2355 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
2357 unsigned int ix;
2358 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
2359 gimple def_temp;
2361 arg = gimple_call_arg (stmt, adj->base_index);
2362 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
2364 if (!fold_convertible_p (TREE_TYPE (origin), arg))
2365 continue;
2366 arg = fold_convert_loc (gimple_location (stmt),
2367 TREE_TYPE (origin), arg);
2369 if (debug_args == NULL)
2370 debug_args = decl_debug_args_insert (callee_decl);
2371 for (ix = 0; VEC_iterate (tree, *debug_args, ix, ddecl); ix += 2)
2372 if (ddecl == origin)
2374 ddecl = VEC_index (tree, *debug_args, ix + 1);
2375 break;
2377 if (ddecl == NULL)
2379 ddecl = make_node (DEBUG_EXPR_DECL);
2380 DECL_ARTIFICIAL (ddecl) = 1;
2381 TREE_TYPE (ddecl) = TREE_TYPE (origin);
2382 DECL_MODE (ddecl) = DECL_MODE (origin);
2384 VEC_safe_push (tree, gc, *debug_args, origin);
2385 VEC_safe_push (tree, gc, *debug_args, ddecl);
2387 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
2388 stmt);
2389 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2395 fprintf (dump_file, "replacing stmt:");
2396 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
2399 new_stmt = gimple_build_call_vec (callee_decl, vargs);
2400 VEC_free (tree, heap, vargs);
2401 if (gimple_call_lhs (stmt))
2402 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2404 gimple_set_block (new_stmt, gimple_block (stmt));
2405 if (gimple_has_location (stmt))
2406 gimple_set_location (new_stmt, gimple_location (stmt));
2407 gimple_call_copy_flags (new_stmt, stmt);
2408 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2410 if (dump_file && (dump_flags & TDF_DETAILS))
2412 fprintf (dump_file, "with stmt:");
2413 print_gimple_stmt (dump_file, new_stmt, 0, 0);
2414 fprintf (dump_file, "\n");
2416 gsi_replace (&gsi, new_stmt, true);
2417 if (cs)
2418 cgraph_set_call_stmt (cs, new_stmt);
2419 update_ssa (TODO_update_ssa);
2420 free_dominance_info (CDI_DOMINATORS);
2423 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
2425 static bool
2426 index_in_adjustments_multiple_times_p (int base_index,
2427 ipa_parm_adjustment_vec adjustments)
2429 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2430 bool one = false;
2432 for (i = 0; i < len; i++)
2434 struct ipa_parm_adjustment *adj;
2435 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2437 if (adj->base_index == base_index)
2439 if (one)
2440 return true;
2441 else
2442 one = true;
2445 return false;
2449 /* Return adjustments that should have the same effect on function parameters
2450 and call arguments as if they were first changed according to adjustments in
2451 INNER and then by adjustments in OUTER. */
2453 ipa_parm_adjustment_vec
2454 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
2455 ipa_parm_adjustment_vec outer)
2457 int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
2458 int inlen = VEC_length (ipa_parm_adjustment_t, inner);
2459 int removals = 0;
2460 ipa_parm_adjustment_vec adjustments, tmp;
2462 tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
2463 for (i = 0; i < inlen; i++)
2465 struct ipa_parm_adjustment *n;
2466 n = VEC_index (ipa_parm_adjustment_t, inner, i);
2468 if (n->remove_param)
2469 removals++;
2470 else
2471 VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
2474 adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
2475 for (i = 0; i < outlen; i++)
2477 struct ipa_parm_adjustment *r;
2478 struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
2479 outer, i);
2480 struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
2481 out->base_index);
2483 gcc_assert (!in->remove_param);
2484 if (out->remove_param)
2486 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
2488 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2489 memset (r, 0, sizeof (*r));
2490 r->remove_param = true;
2492 continue;
2495 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2496 memset (r, 0, sizeof (*r));
2497 r->base_index = in->base_index;
2498 r->type = out->type;
2500 /* FIXME: Create nonlocal value too. */
2502 if (in->copy_param && out->copy_param)
2503 r->copy_param = true;
2504 else if (in->copy_param)
2505 r->offset = out->offset;
2506 else if (out->copy_param)
2507 r->offset = in->offset;
2508 else
2509 r->offset = in->offset + out->offset;
2512 for (i = 0; i < inlen; i++)
2514 struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
2515 inner, i);
2517 if (n->remove_param)
2518 VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
2521 VEC_free (ipa_parm_adjustment_t, heap, tmp);
2522 return adjustments;
2525 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
2526 friendly way, assuming they are meant to be applied to FNDECL. */
2528 void
2529 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
2530 tree fndecl)
2532 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2533 bool first = true;
2534 VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
2536 fprintf (file, "IPA param adjustments: ");
2537 for (i = 0; i < len; i++)
2539 struct ipa_parm_adjustment *adj;
2540 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2542 if (!first)
2543 fprintf (file, " ");
2544 else
2545 first = false;
2547 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
2548 print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
2549 if (adj->base)
2551 fprintf (file, ", base: ");
2552 print_generic_expr (file, adj->base, 0);
2554 if (adj->reduction)
2556 fprintf (file, ", reduction: ");
2557 print_generic_expr (file, adj->reduction, 0);
2559 if (adj->new_ssa_base)
2561 fprintf (file, ", new_ssa_base: ");
2562 print_generic_expr (file, adj->new_ssa_base, 0);
2565 if (adj->copy_param)
2566 fprintf (file, ", copy_param");
2567 else if (adj->remove_param)
2568 fprintf (file, ", remove_param");
2569 else
2570 fprintf (file, ", offset %li", (long) adj->offset);
2571 if (adj->by_ref)
2572 fprintf (file, ", by_ref");
2573 print_node_brief (file, ", type: ", adj->type, 0);
2574 fprintf (file, "\n");
2576 VEC_free (tree, heap, parms);
2579 /* Stream out jump function JUMP_FUNC to OB. */
2581 static void
2582 ipa_write_jump_function (struct output_block *ob,
2583 struct ipa_jump_func *jump_func)
2585 streamer_write_uhwi (ob, jump_func->type);
2587 switch (jump_func->type)
2589 case IPA_JF_UNKNOWN:
2590 break;
2591 case IPA_JF_KNOWN_TYPE:
2592 stream_write_tree (ob, jump_func->value.base_binfo, true);
2593 break;
2594 case IPA_JF_CONST:
2595 stream_write_tree (ob, jump_func->value.constant, true);
2596 break;
2597 case IPA_JF_PASS_THROUGH:
2598 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
2599 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
2600 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
2601 break;
2602 case IPA_JF_ANCESTOR:
2603 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
2604 stream_write_tree (ob, jump_func->value.ancestor.type, true);
2605 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
2606 break;
2607 case IPA_JF_CONST_MEMBER_PTR:
2608 stream_write_tree (ob, jump_func->value.member_cst.pfn, true);
2609 stream_write_tree (ob, jump_func->value.member_cst.delta, false);
2610 break;
2614 /* Read in jump function JUMP_FUNC from IB. */
2616 static void
2617 ipa_read_jump_function (struct lto_input_block *ib,
2618 struct ipa_jump_func *jump_func,
2619 struct data_in *data_in)
2621 jump_func->type = (enum jump_func_type) streamer_read_uhwi (ib);
2623 switch (jump_func->type)
2625 case IPA_JF_UNKNOWN:
2626 break;
2627 case IPA_JF_KNOWN_TYPE:
2628 jump_func->value.base_binfo = stream_read_tree (ib, data_in);
2629 break;
2630 case IPA_JF_CONST:
2631 jump_func->value.constant = stream_read_tree (ib, data_in);
2632 break;
2633 case IPA_JF_PASS_THROUGH:
2634 jump_func->value.pass_through.operand = stream_read_tree (ib, data_in);
2635 jump_func->value.pass_through.formal_id = streamer_read_uhwi (ib);
2636 jump_func->value.pass_through.operation
2637 = (enum tree_code) streamer_read_uhwi (ib);
2638 break;
2639 case IPA_JF_ANCESTOR:
2640 jump_func->value.ancestor.offset = streamer_read_uhwi (ib);
2641 jump_func->value.ancestor.type = stream_read_tree (ib, data_in);
2642 jump_func->value.ancestor.formal_id = streamer_read_uhwi (ib);
2643 break;
2644 case IPA_JF_CONST_MEMBER_PTR:
2645 jump_func->value.member_cst.pfn = stream_read_tree (ib, data_in);
2646 jump_func->value.member_cst.delta = stream_read_tree (ib, data_in);
2647 break;
2651 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
2652 relevant to indirect inlining to OB. */
2654 static void
2655 ipa_write_indirect_edge_info (struct output_block *ob,
2656 struct cgraph_edge *cs)
2658 struct cgraph_indirect_call_info *ii = cs->indirect_info;
2659 struct bitpack_d bp;
2661 streamer_write_hwi (ob, ii->param_index);
2662 streamer_write_hwi (ob, ii->anc_offset);
2663 bp = bitpack_create (ob->main_stream);
2664 bp_pack_value (&bp, ii->polymorphic, 1);
2665 streamer_write_bitpack (&bp);
2667 if (ii->polymorphic)
2669 streamer_write_hwi (ob, ii->otr_token);
2670 stream_write_tree (ob, ii->otr_type, true);
2674 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
2675 relevant to indirect inlining from IB. */
2677 static void
2678 ipa_read_indirect_edge_info (struct lto_input_block *ib,
2679 struct data_in *data_in ATTRIBUTE_UNUSED,
2680 struct cgraph_edge *cs)
2682 struct cgraph_indirect_call_info *ii = cs->indirect_info;
2683 struct bitpack_d bp;
2685 ii->param_index = (int) streamer_read_hwi (ib);
2686 ii->anc_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
2687 bp = streamer_read_bitpack (ib);
2688 ii->polymorphic = bp_unpack_value (&bp, 1);
2689 if (ii->polymorphic)
2691 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
2692 ii->otr_type = stream_read_tree (ib, data_in);
2696 /* Stream out NODE info to OB. */
2698 static void
2699 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2701 int node_ref;
2702 lto_cgraph_encoder_t encoder;
2703 struct ipa_node_params *info = IPA_NODE_REF (node);
2704 int j;
2705 struct cgraph_edge *e;
2706 struct bitpack_d bp;
2708 encoder = ob->decl_state->cgraph_node_encoder;
2709 node_ref = lto_cgraph_encoder_encode (encoder, node);
2710 streamer_write_uhwi (ob, node_ref);
2712 bp = bitpack_create (ob->main_stream);
2713 gcc_assert (info->uses_analysis_done
2714 || ipa_get_param_count (info) == 0);
2715 gcc_assert (!info->node_enqueued);
2716 gcc_assert (!info->ipcp_orig_node);
2717 for (j = 0; j < ipa_get_param_count (info); j++)
2718 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
2719 streamer_write_bitpack (&bp);
2720 for (e = node->callees; e; e = e->next_callee)
2722 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2724 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2725 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2726 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2728 for (e = node->indirect_calls; e; e = e->next_callee)
2730 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2732 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2733 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2734 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2735 ipa_write_indirect_edge_info (ob, e);
2739 /* Stream in NODE info from IB. */
2741 static void
2742 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2743 struct data_in *data_in)
2745 struct ipa_node_params *info = IPA_NODE_REF (node);
2746 int k;
2747 struct cgraph_edge *e;
2748 struct bitpack_d bp;
2750 ipa_initialize_node_params (node);
2752 bp = streamer_read_bitpack (ib);
2753 if (ipa_get_param_count (info) != 0)
2754 info->uses_analysis_done = true;
2755 info->node_enqueued = false;
2756 for (k = 0; k < ipa_get_param_count (info); k++)
2757 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
2758 for (e = node->callees; e; e = e->next_callee)
2760 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2761 int count = streamer_read_uhwi (ib);
2763 if (!count)
2764 continue;
2765 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, count);
2767 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2768 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2770 for (e = node->indirect_calls; e; e = e->next_callee)
2772 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2773 int count = streamer_read_uhwi (ib);
2775 if (count)
2777 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions,
2778 count);
2779 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2780 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k),
2781 data_in);
2783 ipa_read_indirect_edge_info (ib, data_in, e);
2787 /* Write jump functions for nodes in SET. */
2789 void
2790 ipa_prop_write_jump_functions (cgraph_node_set set)
2792 struct cgraph_node *node;
2793 struct output_block *ob;
2794 unsigned int count = 0;
2795 cgraph_node_set_iterator csi;
2797 if (!ipa_node_params_vector)
2798 return;
2800 ob = create_output_block (LTO_section_jump_functions);
2801 ob->cgraph_node = NULL;
2802 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2804 node = csi_node (csi);
2805 if (cgraph_function_with_gimple_body_p (node)
2806 && IPA_NODE_REF (node) != NULL)
2807 count++;
2810 streamer_write_uhwi (ob, count);
2812 /* Process all of the functions. */
2813 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2815 node = csi_node (csi);
2816 if (cgraph_function_with_gimple_body_p (node)
2817 && IPA_NODE_REF (node) != NULL)
2818 ipa_write_node_info (ob, node);
2820 streamer_write_char_stream (ob->main_stream, 0);
2821 produce_asm (ob, NULL);
2822 destroy_output_block (ob);
2825 /* Read section in file FILE_DATA of length LEN with data DATA. */
2827 static void
2828 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2829 size_t len)
2831 const struct lto_function_header *header =
2832 (const struct lto_function_header *) data;
2833 const int32_t cfg_offset = sizeof (struct lto_function_header);
2834 const int32_t main_offset = cfg_offset + header->cfg_size;
2835 const int32_t string_offset = main_offset + header->main_size;
2836 struct data_in *data_in;
2837 struct lto_input_block ib_main;
2838 unsigned int i;
2839 unsigned int count;
2841 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2842 header->main_size);
2844 data_in =
2845 lto_data_in_create (file_data, (const char *) data + string_offset,
2846 header->string_size, NULL);
2847 count = streamer_read_uhwi (&ib_main);
2849 for (i = 0; i < count; i++)
2851 unsigned int index;
2852 struct cgraph_node *node;
2853 lto_cgraph_encoder_t encoder;
2855 index = streamer_read_uhwi (&ib_main);
2856 encoder = file_data->cgraph_node_encoder;
2857 node = lto_cgraph_encoder_deref (encoder, index);
2858 gcc_assert (node->analyzed);
2859 ipa_read_node_info (&ib_main, node, data_in);
2861 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2862 len);
2863 lto_data_in_delete (data_in);
2866 /* Read ipcp jump functions. */
2868 void
2869 ipa_prop_read_jump_functions (void)
2871 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2872 struct lto_file_decl_data *file_data;
2873 unsigned int j = 0;
2875 ipa_check_create_node_params ();
2876 ipa_check_create_edge_args ();
2877 ipa_register_cgraph_hooks ();
2879 while ((file_data = file_data_vec[j++]))
2881 size_t len;
2882 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2884 if (data)
2885 ipa_prop_read_section (file_data, data, len);
2889 /* After merging units, we can get mismatch in argument counts.
2890 Also decl merging might've rendered parameter lists obsolete.
2891 Also compute called_with_variable_arg info. */
2893 void
2894 ipa_update_after_lto_read (void)
2896 struct cgraph_node *node;
2898 ipa_check_create_node_params ();
2899 ipa_check_create_edge_args ();
2901 for (node = cgraph_nodes; node; node = node->next)
2902 if (node->analyzed)
2903 ipa_initialize_node_params (node);