1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009 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
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
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/>. */
22 #include "coretypes.h"
24 #include "langhooks.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-inline.h"
35 #include "diagnostic.h"
37 /* Vector where the parameter infos are actually stored. */
38 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
39 /* Vector where the parameter infos are actually stored. */
40 VEC (ipa_edge_args_t, heap) *ipa_edge_args_vector;
42 /* Holders of ipa cgraph hooks: */
43 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
44 static struct cgraph_node_hook_list *node_removal_hook_holder;
45 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
46 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
48 /* Add cgraph NODE described by INFO to the worklist WL regardless of whether
49 it is in one or not. It should almost never be used directly, as opposed to
50 ipa_push_func_to_list. */
53 ipa_push_func_to_list_1 (struct ipa_func_list **wl,
54 struct cgraph_node *node,
55 struct ipa_node_params *info)
57 struct ipa_func_list *temp;
59 info->node_enqueued = 1;
60 temp = XCNEW (struct ipa_func_list);
66 /* Initialize worklist to contain all functions. */
68 struct ipa_func_list *
69 ipa_init_func_list (void)
71 struct cgraph_node *node;
72 struct ipa_func_list * wl;
75 for (node = cgraph_nodes; node; node = node->next)
78 struct ipa_node_params *info = IPA_NODE_REF (node);
79 /* Unreachable nodes should have been eliminated before ipcp and
81 gcc_assert (node->needed || node->reachable);
82 ipa_push_func_to_list_1 (&wl, node, info);
88 /* Remove a function from the worklist WL and return it. */
91 ipa_pop_func_from_list (struct ipa_func_list **wl)
93 struct ipa_node_params *info;
94 struct ipa_func_list *first;
95 struct cgraph_node *node;
102 info = IPA_NODE_REF (node);
103 info->node_enqueued = 0;
107 /* Return index of the formal whose tree is PTREE in function which corresponds
111 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
115 count = ipa_get_param_count (info);
116 for (i = 0; i < count; i++)
117 if (ipa_get_param(info, i) == ptree)
123 /* Populate the param_decl field in parameter descriptors of INFO that
124 corresponds to NODE. */
127 ipa_populate_param_decls (struct cgraph_node *node,
128 struct ipa_node_params *info)
136 fnargs = DECL_ARGUMENTS (fndecl);
138 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
140 info->params[param_num].decl = parm;
145 /* Count number of formal parameters in NOTE. Store the result to the
146 appropriate field of INFO. */
149 ipa_count_formal_params (struct cgraph_node *node,
150 struct ipa_node_params *info)
158 fnargs = DECL_ARGUMENTS (fndecl);
160 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
162 ipa_set_param_count (info, param_num);
165 /* Initialize the ipa_node_params structure associated with NODE by counting
166 the function parameters, creating the descriptors and populating their
170 ipa_initialize_node_params (struct cgraph_node *node)
172 struct ipa_node_params *info = IPA_NODE_REF (node);
176 ipa_count_formal_params (node, info);
177 info->params = XCNEWVEC (struct ipa_param_descriptor,
178 ipa_get_param_count (info));
179 ipa_populate_param_decls (node, info);
183 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
184 parameters. If OP is a parameter declaration, mark it as modified in the
185 info structure passed in DATA. */
188 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
191 struct ipa_node_params *info = (struct ipa_node_params *) data;
193 if (TREE_CODE (op) == PARM_DECL)
195 int index = ipa_get_param_decl_index (info, op);
196 gcc_assert (index >= 0);
197 info->params[index].modified = true;
203 /* Compute which formal parameters of function associated with NODE are locally
204 modified or their address is taken. Note that this does not apply on
205 parameters with SSA names but those can and should be analyzed
209 ipa_detect_param_modifications (struct cgraph_node *node)
211 tree decl = node->decl;
213 struct function *func;
214 gimple_stmt_iterator gsi;
215 struct ipa_node_params *info = IPA_NODE_REF (node);
217 if (ipa_get_param_count (info) == 0 || info->modification_analysis_done)
220 func = DECL_STRUCT_FUNCTION (decl);
221 FOR_EACH_BB_FN (bb, func)
222 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
223 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info, NULL,
224 visit_store_addr_for_mod_analysis,
225 visit_store_addr_for_mod_analysis);
227 info->modification_analysis_done = 1;
230 /* Count number of arguments callsite CS has and store it in
231 ipa_edge_args structure corresponding to this callsite. */
234 ipa_count_arguments (struct cgraph_edge *cs)
239 stmt = cs->call_stmt;
240 gcc_assert (is_gimple_call (stmt));
241 arg_num = gimple_call_num_args (stmt);
242 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
243 <= (unsigned) cgraph_edge_max_uid)
244 VEC_safe_grow_cleared (ipa_edge_args_t, heap,
245 ipa_edge_args_vector, cgraph_edge_max_uid + 1);
246 ipa_set_cs_argument_count (IPA_EDGE_REF (cs), arg_num);
249 /* Print the jump functions of all arguments on all call graph edges going from
253 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
256 struct cgraph_edge *cs;
257 struct ipa_jump_func *jump_func;
258 enum jump_func_type type;
260 fprintf (f, " Jump functions of caller %s:\n", cgraph_node_name (node));
261 for (cs = node->callees; cs; cs = cs->next_callee)
263 if (!ipa_edge_args_info_available_for_edge_p (cs))
266 fprintf (f, " callsite %s ", cgraph_node_name (node));
267 fprintf (f, "-> %s :: \n", cgraph_node_name (cs->callee));
269 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
270 for (i = 0; i < count; i++)
272 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
273 type = jump_func->type;
275 fprintf (f, " param %d: ", i);
276 if (type == IPA_JF_UNKNOWN)
277 fprintf (f, "UNKNOWN\n");
278 else if (type == IPA_JF_CONST)
280 tree val = jump_func->value.constant;
281 fprintf (f, "CONST: ");
282 print_generic_expr (f, val, 0);
285 else if (type == IPA_JF_CONST_MEMBER_PTR)
287 fprintf (f, "CONST MEMBER PTR: ");
288 print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
290 print_generic_expr (f, jump_func->value.member_cst.delta, 0);
293 else if (type == IPA_JF_PASS_THROUGH)
295 fprintf (f, "PASS THROUGH: ");
296 fprintf (f, "%d\n", jump_func->value.formal_id);
302 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
305 ipa_print_all_jump_functions (FILE *f)
307 struct cgraph_node *node;
309 fprintf (f, "\nJump functions:\n");
310 for (node = cgraph_nodes; node; node = node->next)
312 ipa_print_node_jump_functions (f, node);
316 /* Determine the jump functions of scalar arguments. Scalar means SSA names
317 and constants of a number of selected types. INFO is the ipa_node_params
318 structure associated with the caller, FUNCTIONS is a pointer to an array of
319 jump function structures associated with CALL which is the call statement
323 compute_scalar_jump_functions (struct ipa_node_params *info,
324 struct ipa_jump_func *functions,
330 for (num = 0; num < gimple_call_num_args (call); num++)
332 arg = gimple_call_arg (call, num);
334 if (is_gimple_ip_invariant (arg))
336 functions[num].type = IPA_JF_CONST;
337 functions[num].value.constant = arg;
339 else if ((TREE_CODE (arg) == SSA_NAME) && SSA_NAME_IS_DEFAULT_DEF (arg))
341 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
345 functions[num].type = IPA_JF_PASS_THROUGH;
346 functions[num].value.formal_id = index;
352 /* Inspect the given TYPE and return true iff it has the same structure (the
353 same number of fields of the same types) as a C++ member pointer. If
354 METHOD_PTR and DELTA are non-NULL, store the trees representing the
355 corresponding fields there. */
358 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
362 if (TREE_CODE (type) != RECORD_TYPE)
365 fld = TYPE_FIELDS (type);
366 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
367 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
373 fld = TREE_CHAIN (fld);
374 if (!fld || INTEGRAL_TYPE_P (fld))
379 if (TREE_CHAIN (fld))
385 /* Go through arguments of the CALL and for every one that looks like a member
386 pointer, check whether it can be safely declared pass-through and if so,
387 mark that to the corresponding item of jump FUNCTIONS. Return true iff
388 there are non-pass-through member pointers within the arguments. INFO
389 describes formal parameters of the caller. */
392 compute_pass_through_member_ptrs (struct ipa_node_params *info,
393 struct ipa_jump_func *functions,
396 bool undecided_members = false;
400 for (num = 0; num < gimple_call_num_args (call); num++)
402 arg = gimple_call_arg (call, num);
404 if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
406 if (TREE_CODE (arg) == PARM_DECL)
408 int index = ipa_get_param_decl_index (info, arg);
410 gcc_assert (index >=0);
411 if (!ipa_is_param_modified (info, index))
413 functions[num].type = IPA_JF_PASS_THROUGH;
414 functions[num].value.formal_id = index;
417 undecided_members = true;
420 undecided_members = true;
424 return undecided_members;
427 /* Simple function filling in a member pointer constant jump function (with PFN
428 and DELTA as the constant value) into JFUNC. */
431 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
432 tree pfn, tree delta)
434 jfunc->type = IPA_JF_CONST_MEMBER_PTR;
435 jfunc->value.member_cst.pfn = pfn;
436 jfunc->value.member_cst.delta = delta;
439 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
440 return the rhs of its defining statement. */
443 get_ssa_def_if_simple_copy (tree rhs)
445 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
447 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
449 if (gimple_assign_single_p (def_stmt))
450 rhs = gimple_assign_rhs1 (def_stmt);
457 /* Traverse statements from CALL backwards, scanning whether the argument ARG
458 which is a member pointer is filled in with constant values. If it is, fill
459 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
460 fields of the record type of the member pointer. To give an example, we
461 look for a pattern looking like the following:
463 D.2515.__pfn ={v} printStuff;
464 D.2515.__delta ={v} 0;
465 i_1 = doprinting (D.2515); */
468 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
469 tree delta_field, struct ipa_jump_func *jfunc)
471 gimple_stmt_iterator gsi;
472 tree method = NULL_TREE;
473 tree delta = NULL_TREE;
475 gsi = gsi_for_stmt (call);
478 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
480 gimple stmt = gsi_stmt (gsi);
483 if (!gimple_assign_single_p (stmt))
486 lhs = gimple_assign_lhs (stmt);
487 rhs = gimple_assign_rhs1 (stmt);
489 if (TREE_CODE (lhs) != COMPONENT_REF
490 || TREE_OPERAND (lhs, 0) != arg)
493 fld = TREE_OPERAND (lhs, 1);
494 if (!method && fld == method_field)
496 rhs = get_ssa_def_if_simple_copy (rhs);
497 if (TREE_CODE (rhs) == ADDR_EXPR
498 && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
499 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
501 method = TREE_OPERAND (rhs, 0);
504 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
512 if (!delta && fld == delta_field)
514 rhs = get_ssa_def_if_simple_copy (rhs);
515 if (TREE_CODE (rhs) == INTEGER_CST)
520 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
532 /* Go through the arguments of the CALL and for every member pointer within
533 tries determine whether it is a constant. If it is, create a corresponding
534 constant jump function in FUNCTIONS which is an array of jump functions
535 associated with the call. */
538 compute_cst_member_ptr_arguments (struct ipa_jump_func *functions,
542 tree arg, method_field, delta_field;
544 for (num = 0; num < gimple_call_num_args (call); num++)
546 arg = gimple_call_arg (call, num);
548 if (functions[num].type == IPA_JF_UNKNOWN
549 && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
551 determine_cst_member_ptr (call, arg, method_field, delta_field,
556 /* Compute jump function for all arguments of callsite CS and insert the
557 information in the jump_functions array in the ipa_edge_args corresponding
561 ipa_compute_jump_functions (struct cgraph_edge *cs)
563 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
564 struct ipa_edge_args *arguments = IPA_EDGE_REF (cs);
567 if (ipa_get_cs_argument_count (arguments) == 0 || arguments->jump_functions)
569 arguments->jump_functions = XCNEWVEC (struct ipa_jump_func,
570 ipa_get_cs_argument_count (arguments));
572 call = cs->call_stmt;
573 gcc_assert (is_gimple_call (call));
575 /* We will deal with constants and SSA scalars first: */
576 compute_scalar_jump_functions (info, arguments->jump_functions, call);
578 /* Let's check whether there are any potential member pointers and if so,
579 whether we can determine their functions as pass_through. */
580 if (!compute_pass_through_member_ptrs (info, arguments->jump_functions, call))
583 /* Finally, let's check whether we actually pass a new constant member
585 compute_cst_member_ptr_arguments (arguments->jump_functions, call);
588 /* If RHS looks like a rhs of a statement loading pfn from a member pointer
589 formal parameter, return the parameter, otherwise return NULL. */
592 ipa_get_member_ptr_load_param (tree rhs)
597 if (TREE_CODE (rhs) != COMPONENT_REF)
600 rec = TREE_OPERAND (rhs, 0);
601 if (TREE_CODE (rec) != PARM_DECL
602 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, NULL))
605 fld = TREE_OPERAND (rhs, 1);
606 if (fld == ptr_field)
612 /* If STMT looks like a statement loading a value from a member pointer formal
613 parameter, this function returns that parameter. */
616 ipa_get_stmt_member_ptr_load_param (gimple stmt)
620 if (!gimple_assign_single_p (stmt))
623 rhs = gimple_assign_rhs1 (stmt);
624 return ipa_get_member_ptr_load_param (rhs);
627 /* Returns true iff T is an SSA_NAME defined by a statement. */
630 ipa_is_ssa_with_stmt_def (tree t)
632 if (TREE_CODE (t) == SSA_NAME
633 && !SSA_NAME_IS_DEFAULT_DEF (t))
639 /* Creates a new note describing a call to a parameter number FORMAL_ID and
640 attaches it to the linked list of INFO. It also sets the called flag of the
641 parameter. STMT is the corresponding call statement. */
644 ipa_note_param_call (struct ipa_node_params *info, int formal_id,
647 struct ipa_param_call_note *note;
648 basic_block bb = gimple_bb (stmt);
650 info->params[formal_id].called = 1;
652 note = XCNEW (struct ipa_param_call_note);
653 note->formal_id = formal_id;
655 note->count = bb->count;
656 note->frequency = compute_call_stmt_bb_frequency (current_function_decl, bb);
658 note->next = info->param_calls;
659 info->param_calls = note;
664 /* Analyze the CALL and examine uses of formal parameters of the caller
665 (described by INFO). Currently it checks whether the call calls a pointer
666 that is a formal parameter and if so, the parameter is marked with the
667 called flag and a note describing the call is created. This is very simple
668 for ordinary pointers represented in SSA but not-so-nice when it comes to
669 member pointers. The ugly part of this function does nothing more than
670 tries to match the pattern of such a call. An example of such a pattern is
671 the gimple dump below, the call is on the last line:
674 f$__delta_5 = f.__delta;
675 f$__pfn_24 = f.__pfn;
676 D.2496_3 = (int) f$__pfn_24;
677 D.2497_4 = D.2496_3 & 1;
684 D.2500_7 = (unsigned int) f$__delta_5;
685 D.2501_8 = &S + D.2500_7;
686 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
687 D.2503_10 = *D.2502_9;
688 D.2504_12 = f$__pfn_24 + -1;
689 D.2505_13 = (unsigned int) D.2504_12;
690 D.2506_14 = D.2503_10 + D.2505_13;
691 D.2507_15 = *D.2506_14;
692 iftmp.11_16 = (String:: *) D.2507_15;
695 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
696 D.2500_19 = (unsigned int) f$__delta_5;
697 D.2508_20 = &S + D.2500_19;
698 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
700 Such patterns are results of simple calls to a member pointer:
702 int doprinting (int (MyString::* f)(int) const)
704 MyString S ("somestring");
711 ipa_analyze_call_uses (struct ipa_node_params *info, gimple call)
713 tree target = gimple_call_fn (call);
718 tree rec, rec2, cond;
721 basic_block bb, virt_bb, join;
723 if (TREE_CODE (target) != SSA_NAME)
726 var = SSA_NAME_VAR (target);
727 if (SSA_NAME_IS_DEFAULT_DEF (target))
729 /* assuming TREE_CODE (var) == PARM_DECL */
730 index = ipa_get_param_decl_index (info, var);
732 ipa_note_param_call (info, index, call);
736 /* Now we need to try to match the complex pattern of calling a member
739 if (!POINTER_TYPE_P (TREE_TYPE (target))
740 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
743 def = SSA_NAME_DEF_STMT (target);
744 if (gimple_code (def) != GIMPLE_PHI)
747 if (gimple_phi_num_args (def) != 2)
750 /* First, we need to check whether one of these is a load from a member
751 pointer that is a parameter to this function. */
752 n1 = PHI_ARG_DEF (def, 0);
753 n2 = PHI_ARG_DEF (def, 1);
754 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
756 d1 = SSA_NAME_DEF_STMT (n1);
757 d2 = SSA_NAME_DEF_STMT (n2);
759 if ((rec = ipa_get_stmt_member_ptr_load_param (d1)))
761 if (ipa_get_stmt_member_ptr_load_param (d2))
765 virt_bb = gimple_bb (d2);
767 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2)))
770 virt_bb = gimple_bb (d1);
775 /* Second, we need to check that the basic blocks are laid out in the way
776 corresponding to the pattern. */
778 join = gimple_bb (def);
779 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
780 || single_pred (virt_bb) != bb
781 || single_succ (virt_bb) != join)
784 /* Third, let's see that the branching is done depending on the least
785 significant bit of the pfn. */
787 branch = last_stmt (bb);
788 if (gimple_code (branch) != GIMPLE_COND)
791 if (gimple_cond_code (branch) != NE_EXPR
792 || !integer_zerop (gimple_cond_rhs (branch)))
795 cond = gimple_cond_lhs (branch);
796 if (!ipa_is_ssa_with_stmt_def (cond))
799 def = SSA_NAME_DEF_STMT (cond);
800 if (!is_gimple_assign (def)
801 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
802 || !integer_onep (gimple_assign_rhs2 (def)))
805 cond = gimple_assign_rhs1 (def);
806 if (!ipa_is_ssa_with_stmt_def (cond))
809 def = SSA_NAME_DEF_STMT (cond);
811 if (is_gimple_assign (def)
812 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
814 cond = gimple_assign_rhs1 (def);
815 if (!ipa_is_ssa_with_stmt_def (cond))
817 def = SSA_NAME_DEF_STMT (cond);
820 rec2 = ipa_get_stmt_member_ptr_load_param (def);
824 index = ipa_get_param_decl_index (info, rec);
825 if (index >= 0 && !ipa_is_param_modified (info, index))
826 ipa_note_param_call (info, index, call);
831 /* Analyze the statement STMT with respect to formal parameters (described in
832 INFO) and their uses. Currently it only checks whether formal parameters
836 ipa_analyze_stmt_uses (struct ipa_node_params *info, gimple stmt)
838 if (is_gimple_call (stmt))
839 ipa_analyze_call_uses (info, stmt);
842 /* Scan the function body of NODE and inspect the uses of formal parameters.
843 Store the findings in various structures of the associated ipa_node_params
844 structure, such as parameter flags, notes etc. */
847 ipa_analyze_params_uses (struct cgraph_node *node)
849 tree decl = node->decl;
851 struct function *func;
852 gimple_stmt_iterator gsi;
853 struct ipa_node_params *info = IPA_NODE_REF (node);
855 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
858 func = DECL_STRUCT_FUNCTION (decl);
859 FOR_EACH_BB_FN (bb, func)
861 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
863 gimple stmt = gsi_stmt (gsi);
864 ipa_analyze_stmt_uses (info, stmt);
868 info->uses_analysis_done = 1;
871 /* Update the jump functions associated with call graph edge E when the call
872 graph edge CS is being inlined, assuming that E->caller is already (possibly
873 indirectly) inlined into CS->callee and that E has not been inlined. */
876 update_jump_functions_after_inlining (struct cgraph_edge *cs,
877 struct cgraph_edge *e)
879 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
880 struct ipa_edge_args *args = IPA_EDGE_REF (e);
881 int count = ipa_get_cs_argument_count (args);
884 for (i = 0; i < count; i++)
886 struct ipa_jump_func *src, *dst = ipa_get_ith_jump_func (args, i);
888 if (dst->type != IPA_JF_PASS_THROUGH)
891 /* We must check range due to calls with variable number of arguments: */
892 if (dst->value.formal_id >= (unsigned) ipa_get_cs_argument_count (top))
894 dst->type = IPA_JF_UNKNOWN;
898 src = ipa_get_ith_jump_func (top, dst->value.formal_id);
903 /* Print out a debug message to file F that we have discovered that an indirect
904 call described by NT is in fact a call of a known constant function described
905 by JFUNC. NODE is the node where the call is. */
908 print_edge_addition_message (FILE *f, struct ipa_param_call_note *nt,
909 struct ipa_jump_func *jfunc,
910 struct cgraph_node *node)
912 fprintf (f, "ipa-prop: Discovered an indirect call to a known target (");
913 if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
915 print_node_brief (f, "", jfunc->value.member_cst.pfn, 0);
916 print_node_brief (f, ", ", jfunc->value.member_cst.delta, 0);
919 print_node_brief(f, "", jfunc->value.constant, 0);
921 fprintf (f, ") in %s: ", cgraph_node_name (node));
922 print_gimple_stmt (f, nt->stmt, 2, TDF_SLIM);
925 /* Update the param called notes associated with NODE when CS is being inlined,
926 assuming NODE is (potentially indirectly) inlined into CS->callee.
927 Moreover, if the callee is discovered to be constant, create a new cgraph
928 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
929 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
932 update_call_notes_after_inlining (struct cgraph_edge *cs,
933 struct cgraph_node *node,
934 VEC (cgraph_edge_p, heap) **new_edges)
936 struct ipa_node_params *info = IPA_NODE_REF (node);
937 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
938 struct ipa_param_call_note *nt;
941 for (nt = info->param_calls; nt; nt = nt->next)
943 struct ipa_jump_func *jfunc;
948 /* We must check range due to calls with variable number of arguments: */
949 if (nt->formal_id >= (unsigned) ipa_get_cs_argument_count (top))
951 nt->processed = true;
955 jfunc = ipa_get_ith_jump_func (top, nt->formal_id);
956 if (jfunc->type == IPA_JF_PASS_THROUGH)
957 nt->formal_id = jfunc->value.formal_id;
958 else if (jfunc->type == IPA_JF_CONST
959 || jfunc->type == IPA_JF_CONST_MEMBER_PTR)
961 struct cgraph_node *callee;
962 struct cgraph_edge *new_indirect_edge;
965 nt->processed = true;
966 if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
967 decl = jfunc->value.member_cst.pfn;
969 decl = jfunc->value.constant;
971 if (TREE_CODE (decl) != ADDR_EXPR)
973 decl = TREE_OPERAND (decl, 0);
975 if (TREE_CODE (decl) != FUNCTION_DECL)
977 callee = cgraph_node (decl);
978 if (!callee || !callee->local.inlinable)
983 print_edge_addition_message (dump_file, nt, jfunc, node);
985 new_indirect_edge = cgraph_create_edge (node, callee, nt->stmt,
986 nt->count, nt->frequency,
988 new_indirect_edge->indirect_call = 1;
989 ipa_check_create_edge_args ();
991 VEC_safe_push (cgraph_edge_p, heap, *new_edges, new_indirect_edge);
992 top = IPA_EDGE_REF (cs);
998 /* Recursively traverse subtree of NODE (including node) made of inlined
999 cgraph_edges when CS has been inlined and invoke
1000 update_call_notes_after_inlining on all nodes and
1001 update_jump_functions_after_inlining on all non-inlined edges that lead out
1002 of this subtree. Newly discovered indirect edges will be added to
1003 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1007 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1008 struct cgraph_node *node,
1009 VEC (cgraph_edge_p, heap) **new_edges)
1011 struct cgraph_edge *e;
1014 res = update_call_notes_after_inlining (cs, node, new_edges);
1016 for (e = node->callees; e; e = e->next_callee)
1017 if (!e->inline_failed)
1018 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1020 update_jump_functions_after_inlining (cs, e);
1025 /* Update jump functions and call note functions on inlining the call site CS.
1026 CS is expected to lead to a node already cloned by
1027 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1028 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1032 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1033 VEC (cgraph_edge_p, heap) **new_edges)
1035 /* Do nothing if the preparation phase has not been carried out yet
1036 (i.e. during early inlining). */
1037 if (!ipa_node_params_vector)
1039 gcc_assert (ipa_edge_args_vector);
1041 return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1044 /* Frees all dynamically allocated structures that the argument info points
1048 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1050 if (args->jump_functions)
1051 free (args->jump_functions);
1053 memset (args, 0, sizeof (*args));
1056 /* Free all ipa_edge structures. */
1059 ipa_free_all_edge_args (void)
1062 struct ipa_edge_args *args;
1065 VEC_iterate (ipa_edge_args_t, ipa_edge_args_vector, i, args);
1067 ipa_free_edge_args_substructures (args);
1069 VEC_free (ipa_edge_args_t, heap, ipa_edge_args_vector);
1070 ipa_edge_args_vector = NULL;
1073 /* Frees all dynamically allocated structures that the param info points
1077 ipa_free_node_params_substructures (struct ipa_node_params *info)
1080 free (info->params);
1082 while (info->param_calls)
1084 struct ipa_param_call_note *note = info->param_calls;
1085 info->param_calls = note->next;
1089 memset (info, 0, sizeof (*info));
1092 /* Free all ipa_node_params structures. */
1095 ipa_free_all_node_params (void)
1098 struct ipa_node_params *info;
1101 VEC_iterate (ipa_node_params_t, ipa_node_params_vector, i, info);
1103 ipa_free_node_params_substructures (info);
1105 VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1106 ipa_node_params_vector = NULL;
1109 /* Hook that is called by cgraph.c when an edge is removed. */
1112 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1114 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1115 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1116 <= (unsigned)cs->uid)
1118 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1121 /* Hook that is called by cgraph.c when a node is removed. */
1124 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1126 ipa_free_node_params_substructures (IPA_NODE_REF (node));
1129 /* Helper function to duplicate an array of size N that is at SRC and store a
1130 pointer to it to DST. Nothing is done if SRC is NULL. */
1133 duplicate_array (void *src, size_t n)
1145 /* Hook that is called by cgraph.c when a node is duplicated. */
1148 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1149 __attribute__((unused)) void *data)
1151 struct ipa_edge_args *old_args, *new_args;
1154 ipa_check_create_edge_args ();
1156 old_args = IPA_EDGE_REF (src);
1157 new_args = IPA_EDGE_REF (dst);
1159 arg_count = ipa_get_cs_argument_count (old_args);
1160 ipa_set_cs_argument_count (new_args, arg_count);
1161 new_args->jump_functions = (struct ipa_jump_func *)
1162 duplicate_array (old_args->jump_functions,
1163 sizeof (struct ipa_jump_func) * arg_count);
1166 /* Hook that is called by cgraph.c when a node is duplicated. */
1169 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1170 __attribute__((unused)) void *data)
1172 struct ipa_node_params *old_info, *new_info;
1173 struct ipa_param_call_note *note;
1176 ipa_check_create_node_params ();
1177 old_info = IPA_NODE_REF (src);
1178 new_info = IPA_NODE_REF (dst);
1179 param_count = ipa_get_param_count (old_info);
1181 ipa_set_param_count (new_info, param_count);
1182 new_info->params = (struct ipa_param_descriptor *)
1183 duplicate_array (old_info->params,
1184 sizeof (struct ipa_param_descriptor) * param_count);
1185 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1186 new_info->count_scale = old_info->count_scale;
1188 for (note = old_info->param_calls; note; note = note->next)
1190 struct ipa_param_call_note *nn;
1192 nn = (struct ipa_param_call_note *)
1193 xcalloc (1, sizeof (struct ipa_param_call_note));
1194 memcpy (nn, note, sizeof (struct ipa_param_call_note));
1195 nn->next = new_info->param_calls;
1196 new_info->param_calls = nn;
1200 /* Register our cgraph hooks if they are not already there. */
1203 ipa_register_cgraph_hooks (void)
1205 if (!edge_removal_hook_holder)
1206 edge_removal_hook_holder =
1207 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1208 if (!node_removal_hook_holder)
1209 node_removal_hook_holder =
1210 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1211 if (!edge_duplication_hook_holder)
1212 edge_duplication_hook_holder =
1213 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1214 if (!node_duplication_hook_holder)
1215 node_duplication_hook_holder =
1216 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1219 /* Unregister our cgraph hooks if they are not already there. */
1222 ipa_unregister_cgraph_hooks (void)
1224 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1225 edge_removal_hook_holder = NULL;
1226 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1227 node_removal_hook_holder = NULL;
1228 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1229 edge_duplication_hook_holder = NULL;
1230 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1231 node_duplication_hook_holder = NULL;
1234 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1235 longer needed after ipa-cp. */
1238 free_all_ipa_structures_after_ipa_cp (void)
1240 if (!flag_indirect_inlining)
1242 ipa_free_all_edge_args ();
1243 ipa_free_all_node_params ();
1244 ipa_unregister_cgraph_hooks ();
1248 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1249 longer needed after indirect inlining. */
1252 free_all_ipa_structures_after_iinln (void)
1254 ipa_free_all_edge_args ();
1255 ipa_free_all_node_params ();
1256 ipa_unregister_cgraph_hooks ();
1259 /* Print ipa_tree_map data structures of all functions in the
1263 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1267 struct ipa_node_params *info;
1269 if (!node->analyzed)
1271 info = IPA_NODE_REF (node);
1272 fprintf (f, " function %s Trees :: \n", cgraph_node_name (node));
1273 count = ipa_get_param_count (info);
1274 for (i = 0; i < count; i++)
1276 temp = ipa_get_param (info, i);
1277 if (TREE_CODE (temp) == PARM_DECL)
1278 fprintf (f, " param %d : %s", i,
1279 (*lang_hooks.decl_printable_name) (temp, 2));
1280 if (ipa_is_param_modified (info, i))
1281 fprintf (f, " modified");
1282 if (ipa_is_param_called (info, i))
1283 fprintf (f, " called");
1288 /* Print ipa_tree_map data structures of all functions in the
1292 ipa_print_all_params (FILE * f)
1294 struct cgraph_node *node;
1296 fprintf (f, "\nFunction parameters:\n");
1297 for (node = cgraph_nodes; node; node = node->next)
1298 ipa_print_node_params (f, node);