1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009, 2010
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
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
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/>. */
23 #include "coretypes.h"
25 #include "langhooks.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-inline.h"
36 #include "diagnostic.h"
37 #include "lto-streamer.h"
39 /* Vector where the parameter infos are actually stored. */
40 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
41 /* Vector where the parameter infos are actually stored. */
42 VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
44 /* Holders of ipa cgraph hooks: */
45 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
46 static struct cgraph_node_hook_list *node_removal_hook_holder;
47 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
48 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
50 /* Add cgraph NODE described by INFO to the worklist WL regardless of whether
51 it is in one or not. It should almost never be used directly, as opposed to
52 ipa_push_func_to_list. */
55 ipa_push_func_to_list_1 (struct ipa_func_list **wl,
56 struct cgraph_node *node,
57 struct ipa_node_params *info)
59 struct ipa_func_list *temp;
61 info->node_enqueued = 1;
62 temp = XCNEW (struct ipa_func_list);
68 /* Initialize worklist to contain all functions. */
70 struct ipa_func_list *
71 ipa_init_func_list (void)
73 struct cgraph_node *node;
74 struct ipa_func_list * wl;
77 for (node = cgraph_nodes; node; node = node->next)
80 struct ipa_node_params *info = IPA_NODE_REF (node);
81 /* Unreachable nodes should have been eliminated before ipcp and
83 gcc_assert (node->needed || node->reachable);
84 ipa_push_func_to_list_1 (&wl, node, info);
90 /* Remove a function from the worklist WL and return it. */
93 ipa_pop_func_from_list (struct ipa_func_list **wl)
95 struct ipa_node_params *info;
96 struct ipa_func_list *first;
97 struct cgraph_node *node;
104 info = IPA_NODE_REF (node);
105 info->node_enqueued = 0;
109 /* Return index of the formal whose tree is PTREE in function which corresponds
113 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
117 count = ipa_get_param_count (info);
118 for (i = 0; i < count; i++)
119 if (ipa_get_param(info, i) == ptree)
125 /* Populate the param_decl field in parameter descriptors of INFO that
126 corresponds to NODE. */
129 ipa_populate_param_decls (struct cgraph_node *node,
130 struct ipa_node_params *info)
138 fnargs = DECL_ARGUMENTS (fndecl);
140 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
142 info->params[param_num].decl = parm;
147 /* Return how many formal parameters FNDECL has. */
150 count_formal_params_1 (tree fndecl)
155 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
161 /* Count number of formal parameters in NOTE. Store the result to the
162 appropriate field of INFO. */
165 ipa_count_formal_params (struct cgraph_node *node,
166 struct ipa_node_params *info)
170 param_num = count_formal_params_1 (node->decl);
171 ipa_set_param_count (info, param_num);
174 /* Initialize the ipa_node_params structure associated with NODE by counting
175 the function parameters, creating the descriptors and populating their
179 ipa_initialize_node_params (struct cgraph_node *node)
181 struct ipa_node_params *info = IPA_NODE_REF (node);
185 ipa_count_formal_params (node, info);
186 info->params = XCNEWVEC (struct ipa_param_descriptor,
187 ipa_get_param_count (info));
188 ipa_populate_param_decls (node, info);
192 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
193 parameters. If OP is a parameter declaration, mark it as modified in the
194 info structure passed in DATA. */
197 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
200 struct ipa_node_params *info = (struct ipa_node_params *) data;
202 op = get_base_address (op);
204 && TREE_CODE (op) == PARM_DECL)
206 int index = ipa_get_param_decl_index (info, op);
207 gcc_assert (index >= 0);
208 info->params[index].modified = true;
214 /* Compute which formal parameters of function associated with NODE are locally
215 modified or their address is taken. Note that this does not apply on
216 parameters with SSA names but those can and should be analyzed
220 ipa_detect_param_modifications (struct cgraph_node *node)
222 tree decl = node->decl;
224 struct function *func;
225 gimple_stmt_iterator gsi;
226 struct ipa_node_params *info = IPA_NODE_REF (node);
228 if (ipa_get_param_count (info) == 0 || info->modification_analysis_done)
231 func = DECL_STRUCT_FUNCTION (decl);
232 FOR_EACH_BB_FN (bb, func)
233 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
234 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info, NULL,
235 visit_store_addr_for_mod_analysis,
236 visit_store_addr_for_mod_analysis);
238 info->modification_analysis_done = 1;
241 /* Count number of arguments callsite CS has and store it in
242 ipa_edge_args structure corresponding to this callsite. */
245 ipa_count_arguments (struct cgraph_edge *cs)
250 stmt = cs->call_stmt;
251 gcc_assert (is_gimple_call (stmt));
252 arg_num = gimple_call_num_args (stmt);
253 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
254 <= (unsigned) cgraph_edge_max_uid)
255 VEC_safe_grow_cleared (ipa_edge_args_t, gc,
256 ipa_edge_args_vector, cgraph_edge_max_uid + 1);
257 ipa_set_cs_argument_count (IPA_EDGE_REF (cs), arg_num);
260 /* Print the jump functions of all arguments on all call graph edges going from
264 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
267 struct cgraph_edge *cs;
268 struct ipa_jump_func *jump_func;
269 enum jump_func_type type;
271 fprintf (f, " Jump functions of caller %s:\n", cgraph_node_name (node));
272 for (cs = node->callees; cs; cs = cs->next_callee)
274 if (!ipa_edge_args_info_available_for_edge_p (cs))
277 fprintf (f, " callsite %s ", cgraph_node_name (node));
278 fprintf (f, "-> %s :: \n", cgraph_node_name (cs->callee));
280 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
281 for (i = 0; i < count; i++)
283 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
284 type = jump_func->type;
286 fprintf (f, " param %d: ", i);
287 if (type == IPA_JF_UNKNOWN)
288 fprintf (f, "UNKNOWN\n");
289 else if (type == IPA_JF_CONST)
291 tree val = jump_func->value.constant;
292 fprintf (f, "CONST: ");
293 print_generic_expr (f, val, 0);
294 if (TREE_CODE (val) == ADDR_EXPR
295 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
298 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
303 else if (type == IPA_JF_CONST_MEMBER_PTR)
305 fprintf (f, "CONST MEMBER PTR: ");
306 print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
308 print_generic_expr (f, jump_func->value.member_cst.delta, 0);
311 else if (type == IPA_JF_PASS_THROUGH)
313 fprintf (f, "PASS THROUGH: ");
314 fprintf (f, "%d, op %s ",
315 jump_func->value.pass_through.formal_id,
317 jump_func->value.pass_through.operation]);
318 if (jump_func->value.pass_through.operation != NOP_EXPR)
319 print_generic_expr (dump_file,
320 jump_func->value.pass_through.operand, 0);
321 fprintf (dump_file, "\n");
323 else if (type == IPA_JF_ANCESTOR)
325 fprintf (f, "ANCESTOR: ");
326 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC"\n",
327 jump_func->value.ancestor.formal_id,
328 jump_func->value.ancestor.offset);
334 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
337 ipa_print_all_jump_functions (FILE *f)
339 struct cgraph_node *node;
341 fprintf (f, "\nJump functions:\n");
342 for (node = cgraph_nodes; node; node = node->next)
344 ipa_print_node_jump_functions (f, node);
348 /* Determine whether passing ssa name NAME constitutes a polynomial
349 pass-through function or getting an address of an acestor and if so, write
350 such a jump function to JFUNC. INFO describes the caller. */
353 compute_complex_pass_through (struct ipa_node_params *info,
354 struct ipa_jump_func *jfunc,
357 HOST_WIDE_INT offset, size, max_size;
360 gimple stmt = SSA_NAME_DEF_STMT (name);
362 if (!is_gimple_assign (stmt))
364 op1 = gimple_assign_rhs1 (stmt);
365 op2 = gimple_assign_rhs2 (stmt);
369 if (TREE_CODE (op1) != SSA_NAME
370 || !SSA_NAME_IS_DEFAULT_DEF (op1)
371 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
372 && !useless_type_conversion_p (TREE_TYPE (name),
374 || !is_gimple_ip_invariant (op2))
377 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
380 jfunc->type = IPA_JF_PASS_THROUGH;
381 jfunc->value.pass_through.formal_id = index;
382 jfunc->value.pass_through.operation = gimple_assign_rhs_code (stmt);
383 jfunc->value.pass_through.operand = op2;
388 if (TREE_CODE (op1) != ADDR_EXPR)
390 op1 = TREE_OPERAND (op1, 0);
391 type = TREE_TYPE (op1);
393 op1 = get_ref_base_and_extent (op1, &offset, &size, &max_size);
394 if (TREE_CODE (op1) != INDIRECT_REF
395 /* If this is a varying address, punt. */
399 op1 = TREE_OPERAND (op1, 0);
400 if (TREE_CODE (op1) != SSA_NAME
401 || !SSA_NAME_IS_DEFAULT_DEF (op1))
404 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
407 jfunc->type = IPA_JF_ANCESTOR;
408 jfunc->value.ancestor.formal_id = index;
409 jfunc->value.ancestor.offset = offset;
410 jfunc->value.ancestor.type = type;
415 /* Determine the jump functions of scalar arguments. Scalar means SSA names
416 and constants of a number of selected types. INFO is the ipa_node_params
417 structure associated with the caller, FUNCTIONS is a pointer to an array of
418 jump function structures associated with CALL which is the call statement
422 compute_scalar_jump_functions (struct ipa_node_params *info,
423 struct ipa_jump_func *functions,
429 for (num = 0; num < gimple_call_num_args (call); num++)
431 arg = gimple_call_arg (call, num);
433 if (is_gimple_ip_invariant (arg))
435 functions[num].type = IPA_JF_CONST;
436 functions[num].value.constant = arg;
438 else if (TREE_CODE (arg) == SSA_NAME)
440 if (SSA_NAME_IS_DEFAULT_DEF (arg))
442 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
446 functions[num].type = IPA_JF_PASS_THROUGH;
447 functions[num].value.pass_through.formal_id = index;
448 functions[num].value.pass_through.operation = NOP_EXPR;
452 compute_complex_pass_through (info, &functions[num], arg);
457 /* Inspect the given TYPE and return true iff it has the same structure (the
458 same number of fields of the same types) as a C++ member pointer. If
459 METHOD_PTR and DELTA are non-NULL, store the trees representing the
460 corresponding fields there. */
463 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
467 if (TREE_CODE (type) != RECORD_TYPE)
470 fld = TYPE_FIELDS (type);
471 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
472 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
478 fld = TREE_CHAIN (fld);
479 if (!fld || INTEGRAL_TYPE_P (fld))
484 if (TREE_CHAIN (fld))
490 /* Go through arguments of the CALL and for every one that looks like a member
491 pointer, check whether it can be safely declared pass-through and if so,
492 mark that to the corresponding item of jump FUNCTIONS. Return true iff
493 there are non-pass-through member pointers within the arguments. INFO
494 describes formal parameters of the caller. */
497 compute_pass_through_member_ptrs (struct ipa_node_params *info,
498 struct ipa_jump_func *functions,
501 bool undecided_members = false;
505 for (num = 0; num < gimple_call_num_args (call); num++)
507 arg = gimple_call_arg (call, num);
509 if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
511 if (TREE_CODE (arg) == PARM_DECL)
513 int index = ipa_get_param_decl_index (info, arg);
515 gcc_assert (index >=0);
516 if (!ipa_is_param_modified (info, index))
518 functions[num].type = IPA_JF_PASS_THROUGH;
519 functions[num].value.pass_through.formal_id = index;
520 functions[num].value.pass_through.operation = NOP_EXPR;
523 undecided_members = true;
526 undecided_members = true;
530 return undecided_members;
533 /* Simple function filling in a member pointer constant jump function (with PFN
534 and DELTA as the constant value) into JFUNC. */
537 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
538 tree pfn, tree delta)
540 jfunc->type = IPA_JF_CONST_MEMBER_PTR;
541 jfunc->value.member_cst.pfn = pfn;
542 jfunc->value.member_cst.delta = delta;
545 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
546 return the rhs of its defining statement. */
549 get_ssa_def_if_simple_copy (tree rhs)
551 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
553 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
555 if (gimple_assign_single_p (def_stmt))
556 rhs = gimple_assign_rhs1 (def_stmt);
563 /* Traverse statements from CALL backwards, scanning whether the argument ARG
564 which is a member pointer is filled in with constant values. If it is, fill
565 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
566 fields of the record type of the member pointer. To give an example, we
567 look for a pattern looking like the following:
569 D.2515.__pfn ={v} printStuff;
570 D.2515.__delta ={v} 0;
571 i_1 = doprinting (D.2515); */
574 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
575 tree delta_field, struct ipa_jump_func *jfunc)
577 gimple_stmt_iterator gsi;
578 tree method = NULL_TREE;
579 tree delta = NULL_TREE;
581 gsi = gsi_for_stmt (call);
584 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
586 gimple stmt = gsi_stmt (gsi);
589 if (!gimple_assign_single_p (stmt))
592 lhs = gimple_assign_lhs (stmt);
593 rhs = gimple_assign_rhs1 (stmt);
595 if (TREE_CODE (lhs) != COMPONENT_REF
596 || TREE_OPERAND (lhs, 0) != arg)
599 fld = TREE_OPERAND (lhs, 1);
600 if (!method && fld == method_field)
602 rhs = get_ssa_def_if_simple_copy (rhs);
603 if (TREE_CODE (rhs) == ADDR_EXPR
604 && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
605 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
607 method = TREE_OPERAND (rhs, 0);
610 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
618 if (!delta && fld == delta_field)
620 rhs = get_ssa_def_if_simple_copy (rhs);
621 if (TREE_CODE (rhs) == INTEGER_CST)
626 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
638 /* Go through the arguments of the CALL and for every member pointer within
639 tries determine whether it is a constant. If it is, create a corresponding
640 constant jump function in FUNCTIONS which is an array of jump functions
641 associated with the call. */
644 compute_cst_member_ptr_arguments (struct ipa_jump_func *functions,
648 tree arg, method_field, delta_field;
650 for (num = 0; num < gimple_call_num_args (call); num++)
652 arg = gimple_call_arg (call, num);
654 if (functions[num].type == IPA_JF_UNKNOWN
655 && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
657 determine_cst_member_ptr (call, arg, method_field, delta_field,
662 /* Compute jump function for all arguments of callsite CS and insert the
663 information in the jump_functions array in the ipa_edge_args corresponding
667 ipa_compute_jump_functions (struct cgraph_edge *cs)
669 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
670 struct ipa_edge_args *arguments = IPA_EDGE_REF (cs);
673 if (ipa_get_cs_argument_count (arguments) == 0 || arguments->jump_functions)
675 arguments->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
676 ipa_get_cs_argument_count (arguments));
678 call = cs->call_stmt;
679 gcc_assert (is_gimple_call (call));
681 /* We will deal with constants and SSA scalars first: */
682 compute_scalar_jump_functions (info, arguments->jump_functions, call);
684 /* Let's check whether there are any potential member pointers and if so,
685 whether we can determine their functions as pass_through. */
686 if (!compute_pass_through_member_ptrs (info, arguments->jump_functions, call))
689 /* Finally, let's check whether we actually pass a new constant member
691 compute_cst_member_ptr_arguments (arguments->jump_functions, call);
694 /* If RHS looks like a rhs of a statement loading pfn from a member
695 pointer formal parameter, return the parameter, otherwise return
696 NULL. If USE_DELTA, then we look for a use of the delta field
697 rather than the pfn. */
700 ipa_get_member_ptr_load_param (tree rhs, bool use_delta)
706 if (TREE_CODE (rhs) != COMPONENT_REF)
709 rec = TREE_OPERAND (rhs, 0);
710 if (TREE_CODE (rec) != PARM_DECL
711 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
714 fld = TREE_OPERAND (rhs, 1);
715 if (use_delta ? (fld == delta_field) : (fld == ptr_field))
721 /* If STMT looks like a statement loading a value from a member pointer formal
722 parameter, this function returns that parameter. */
725 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta)
729 if (!gimple_assign_single_p (stmt))
732 rhs = gimple_assign_rhs1 (stmt);
733 return ipa_get_member_ptr_load_param (rhs, use_delta);
736 /* Returns true iff T is an SSA_NAME defined by a statement. */
739 ipa_is_ssa_with_stmt_def (tree t)
741 if (TREE_CODE (t) == SSA_NAME
742 && !SSA_NAME_IS_DEFAULT_DEF (t))
748 /* Creates a new note describing a call to a parameter number FORMAL_ID and
749 attaches it to the linked list of INFO. It also sets the called flag of the
750 parameter. STMT is the corresponding call statement. */
753 ipa_note_param_call (struct ipa_node_params *info, int formal_id,
756 struct ipa_param_call_note *note;
757 basic_block bb = gimple_bb (stmt);
759 note = XCNEW (struct ipa_param_call_note);
760 note->formal_id = formal_id;
762 note->lto_stmt_uid = gimple_uid (stmt);
763 note->count = bb->count;
764 note->frequency = compute_call_stmt_bb_frequency (current_function_decl, bb);
765 note->loop_nest = bb->loop_depth;
767 note->next = info->param_calls;
768 info->param_calls = note;
773 /* Analyze the CALL and examine uses of formal parameters of the caller
774 (described by INFO). Currently it checks whether the call calls a pointer
775 that is a formal parameter and if so, the parameter is marked with the
776 called flag and a note describing the call is created. This is very simple
777 for ordinary pointers represented in SSA but not-so-nice when it comes to
778 member pointers. The ugly part of this function does nothing more than
779 tries to match the pattern of such a call. An example of such a pattern is
780 the gimple dump below, the call is on the last line:
783 f$__delta_5 = f.__delta;
784 f$__pfn_24 = f.__pfn;
785 D.2496_3 = (int) f$__pfn_24;
786 D.2497_4 = D.2496_3 & 1;
793 D.2500_7 = (unsigned int) f$__delta_5;
794 D.2501_8 = &S + D.2500_7;
795 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
796 D.2503_10 = *D.2502_9;
797 D.2504_12 = f$__pfn_24 + -1;
798 D.2505_13 = (unsigned int) D.2504_12;
799 D.2506_14 = D.2503_10 + D.2505_13;
800 D.2507_15 = *D.2506_14;
801 iftmp.11_16 = (String:: *) D.2507_15;
804 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
805 D.2500_19 = (unsigned int) f$__delta_5;
806 D.2508_20 = &S + D.2500_19;
807 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
809 Such patterns are results of simple calls to a member pointer:
811 int doprinting (int (MyString::* f)(int) const)
813 MyString S ("somestring");
820 ipa_analyze_call_uses (struct ipa_node_params *info, gimple call)
822 tree target = gimple_call_fn (call);
827 tree rec, rec2, cond;
830 basic_block bb, virt_bb, join;
832 if (TREE_CODE (target) != SSA_NAME)
835 var = SSA_NAME_VAR (target);
836 if (SSA_NAME_IS_DEFAULT_DEF (target))
838 /* assuming TREE_CODE (var) == PARM_DECL */
839 index = ipa_get_param_decl_index (info, var);
841 ipa_note_param_call (info, index, call);
845 /* Now we need to try to match the complex pattern of calling a member
848 if (!POINTER_TYPE_P (TREE_TYPE (target))
849 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
852 def = SSA_NAME_DEF_STMT (target);
853 if (gimple_code (def) != GIMPLE_PHI)
856 if (gimple_phi_num_args (def) != 2)
859 /* First, we need to check whether one of these is a load from a member
860 pointer that is a parameter to this function. */
861 n1 = PHI_ARG_DEF (def, 0);
862 n2 = PHI_ARG_DEF (def, 1);
863 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
865 d1 = SSA_NAME_DEF_STMT (n1);
866 d2 = SSA_NAME_DEF_STMT (n2);
868 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
870 if (ipa_get_stmt_member_ptr_load_param (d2, false))
874 virt_bb = gimple_bb (d2);
876 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
879 virt_bb = gimple_bb (d1);
884 /* Second, we need to check that the basic blocks are laid out in the way
885 corresponding to the pattern. */
887 join = gimple_bb (def);
888 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
889 || single_pred (virt_bb) != bb
890 || single_succ (virt_bb) != join)
893 /* Third, let's see that the branching is done depending on the least
894 significant bit of the pfn. */
896 branch = last_stmt (bb);
897 if (gimple_code (branch) != GIMPLE_COND)
900 if (gimple_cond_code (branch) != NE_EXPR
901 || !integer_zerop (gimple_cond_rhs (branch)))
904 cond = gimple_cond_lhs (branch);
905 if (!ipa_is_ssa_with_stmt_def (cond))
908 def = SSA_NAME_DEF_STMT (cond);
909 if (!is_gimple_assign (def)
910 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
911 || !integer_onep (gimple_assign_rhs2 (def)))
914 cond = gimple_assign_rhs1 (def);
915 if (!ipa_is_ssa_with_stmt_def (cond))
918 def = SSA_NAME_DEF_STMT (cond);
920 if (is_gimple_assign (def)
921 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
923 cond = gimple_assign_rhs1 (def);
924 if (!ipa_is_ssa_with_stmt_def (cond))
926 def = SSA_NAME_DEF_STMT (cond);
929 rec2 = ipa_get_stmt_member_ptr_load_param (def,
930 (TARGET_PTRMEMFUNC_VBIT_LOCATION
931 == ptrmemfunc_vbit_in_delta));
936 index = ipa_get_param_decl_index (info, rec);
937 if (index >= 0 && !ipa_is_param_modified (info, index))
938 ipa_note_param_call (info, index, call);
943 /* Analyze the statement STMT with respect to formal parameters (described in
944 INFO) and their uses. Currently it only checks whether formal parameters
948 ipa_analyze_stmt_uses (struct ipa_node_params *info, gimple stmt)
950 if (is_gimple_call (stmt))
951 ipa_analyze_call_uses (info, stmt);
954 /* Scan the function body of NODE and inspect the uses of formal parameters.
955 Store the findings in various structures of the associated ipa_node_params
956 structure, such as parameter flags, notes etc. */
959 ipa_analyze_params_uses (struct cgraph_node *node)
961 tree decl = node->decl;
963 struct function *func;
964 gimple_stmt_iterator gsi;
965 struct ipa_node_params *info = IPA_NODE_REF (node);
967 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
970 func = DECL_STRUCT_FUNCTION (decl);
971 FOR_EACH_BB_FN (bb, func)
973 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
975 gimple stmt = gsi_stmt (gsi);
976 ipa_analyze_stmt_uses (info, stmt);
980 info->uses_analysis_done = 1;
983 /* Update the jump functions associated with call graph edge E when the call
984 graph edge CS is being inlined, assuming that E->caller is already (possibly
985 indirectly) inlined into CS->callee and that E has not been inlined.
987 We keep pass through functions only if they do not contain any operation.
988 This is sufficient for inlining and greately simplifies things. */
991 update_jump_functions_after_inlining (struct cgraph_edge *cs,
992 struct cgraph_edge *e)
994 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
995 struct ipa_edge_args *args = IPA_EDGE_REF (e);
996 int count = ipa_get_cs_argument_count (args);
999 for (i = 0; i < count; i++)
1001 struct ipa_jump_func *src, *dst = ipa_get_ith_jump_func (args, i);
1003 if (dst->type == IPA_JF_ANCESTOR)
1005 dst->type = IPA_JF_UNKNOWN;
1009 if (dst->type != IPA_JF_PASS_THROUGH)
1012 /* We must check range due to calls with variable number of arguments and
1013 we cannot combine jump functions with operations. */
1014 if (dst->value.pass_through.operation != NOP_EXPR
1015 || (dst->value.pass_through.formal_id
1016 >= ipa_get_cs_argument_count (top)))
1018 dst->type = IPA_JF_UNKNOWN;
1022 src = ipa_get_ith_jump_func (top, dst->value.pass_through.formal_id);
1027 /* Print out a debug message to file F that we have discovered that an indirect
1028 call described by NT is in fact a call of a known constant function described
1029 by JFUNC. NODE is the node where the call is. */
1032 print_edge_addition_message (FILE *f, struct ipa_param_call_note *nt,
1033 struct ipa_jump_func *jfunc,
1034 struct cgraph_node *node)
1036 fprintf (f, "ipa-prop: Discovered an indirect call to a known target (");
1037 if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1039 print_node_brief (f, "", jfunc->value.member_cst.pfn, 0);
1040 print_node_brief (f, ", ", jfunc->value.member_cst.delta, 0);
1043 print_node_brief(f, "", jfunc->value.constant, 0);
1045 fprintf (f, ") in %s: ", cgraph_node_name (node));
1046 print_gimple_stmt (f, nt->stmt, 2, TDF_SLIM);
1049 /* Update the param called notes associated with NODE when CS is being inlined,
1050 assuming NODE is (potentially indirectly) inlined into CS->callee.
1051 Moreover, if the callee is discovered to be constant, create a new cgraph
1052 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1053 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1056 update_call_notes_after_inlining (struct cgraph_edge *cs,
1057 struct cgraph_node *node,
1058 VEC (cgraph_edge_p, heap) **new_edges)
1060 struct ipa_node_params *info = IPA_NODE_REF (node);
1061 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1062 struct ipa_param_call_note *nt;
1065 for (nt = info->param_calls; nt; nt = nt->next)
1067 struct ipa_jump_func *jfunc;
1072 /* We must check range due to calls with variable number of arguments: */
1073 if (nt->formal_id >= ipa_get_cs_argument_count (top))
1075 nt->processed = true;
1079 jfunc = ipa_get_ith_jump_func (top, nt->formal_id);
1080 if (jfunc->type == IPA_JF_PASS_THROUGH
1081 && jfunc->value.pass_through.operation == NOP_EXPR)
1082 nt->formal_id = jfunc->value.pass_through.formal_id;
1083 else if (jfunc->type == IPA_JF_CONST
1084 || jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1086 struct cgraph_node *callee;
1087 struct cgraph_edge *new_indirect_edge;
1090 nt->processed = true;
1091 if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1092 decl = jfunc->value.member_cst.pfn;
1094 decl = jfunc->value.constant;
1096 if (TREE_CODE (decl) != ADDR_EXPR)
1098 decl = TREE_OPERAND (decl, 0);
1100 if (TREE_CODE (decl) != FUNCTION_DECL)
1102 callee = cgraph_node (decl);
1103 if (!callee || !callee->local.inlinable)
1108 print_edge_addition_message (dump_file, nt, jfunc, node);
1110 new_indirect_edge = cgraph_create_edge (node, callee, nt->stmt,
1111 nt->count, nt->frequency,
1113 new_indirect_edge->lto_stmt_uid = nt->lto_stmt_uid;
1114 new_indirect_edge->indirect_call = 1;
1115 ipa_check_create_edge_args ();
1117 VEC_safe_push (cgraph_edge_p, heap, *new_edges, new_indirect_edge);
1118 top = IPA_EDGE_REF (cs);
1122 /* Ancestor jum functions and pass theoughs with operations should
1123 not be used on parameters that then get called. */
1124 gcc_assert (jfunc->type == IPA_JF_UNKNOWN);
1125 nt->processed = true;
1131 /* Recursively traverse subtree of NODE (including node) made of inlined
1132 cgraph_edges when CS has been inlined and invoke
1133 update_call_notes_after_inlining on all nodes and
1134 update_jump_functions_after_inlining on all non-inlined edges that lead out
1135 of this subtree. Newly discovered indirect edges will be added to
1136 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1140 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1141 struct cgraph_node *node,
1142 VEC (cgraph_edge_p, heap) **new_edges)
1144 struct cgraph_edge *e;
1147 res = update_call_notes_after_inlining (cs, node, new_edges);
1149 for (e = node->callees; e; e = e->next_callee)
1150 if (!e->inline_failed)
1151 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1153 update_jump_functions_after_inlining (cs, e);
1158 /* Update jump functions and call note functions on inlining the call site CS.
1159 CS is expected to lead to a node already cloned by
1160 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1161 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1165 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1166 VEC (cgraph_edge_p, heap) **new_edges)
1168 /* FIXME lto: We do not stream out indirect call information. */
1172 /* Do nothing if the preparation phase has not been carried out yet
1173 (i.e. during early inlining). */
1174 if (!ipa_node_params_vector)
1176 gcc_assert (ipa_edge_args_vector);
1178 return propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1181 /* Frees all dynamically allocated structures that the argument info points
1185 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1187 if (args->jump_functions)
1188 ggc_free (args->jump_functions);
1190 memset (args, 0, sizeof (*args));
1193 /* Free all ipa_edge structures. */
1196 ipa_free_all_edge_args (void)
1199 struct ipa_edge_args *args;
1202 VEC_iterate (ipa_edge_args_t, ipa_edge_args_vector, i, args);
1204 ipa_free_edge_args_substructures (args);
1206 VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1207 ipa_edge_args_vector = NULL;
1210 /* Frees all dynamically allocated structures that the param info points
1214 ipa_free_node_params_substructures (struct ipa_node_params *info)
1217 free (info->params);
1219 while (info->param_calls)
1221 struct ipa_param_call_note *note = info->param_calls;
1222 info->param_calls = note->next;
1226 memset (info, 0, sizeof (*info));
1229 /* Free all ipa_node_params structures. */
1232 ipa_free_all_node_params (void)
1235 struct ipa_node_params *info;
1238 VEC_iterate (ipa_node_params_t, ipa_node_params_vector, i, info);
1240 ipa_free_node_params_substructures (info);
1242 VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1243 ipa_node_params_vector = NULL;
1246 /* Hook that is called by cgraph.c when an edge is removed. */
1249 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1251 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1252 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1253 <= (unsigned)cs->uid)
1255 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1258 /* Hook that is called by cgraph.c when a node is removed. */
1261 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1263 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1264 if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1265 <= (unsigned)node->uid)
1267 ipa_free_node_params_substructures (IPA_NODE_REF (node));
1270 /* Helper function to duplicate an array of size N that is at SRC and store a
1271 pointer to it to DST. Nothing is done if SRC is NULL. */
1274 duplicate_array (void *src, size_t n)
1286 /* Like duplicate_array byt in GGC memory. */
1289 duplicate_ggc_array (void *src, size_t n)
1301 /* Hook that is called by cgraph.c when a node is duplicated. */
1304 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1305 __attribute__((unused)) void *data)
1307 struct ipa_edge_args *old_args, *new_args;
1310 ipa_check_create_edge_args ();
1312 old_args = IPA_EDGE_REF (src);
1313 new_args = IPA_EDGE_REF (dst);
1315 arg_count = ipa_get_cs_argument_count (old_args);
1316 ipa_set_cs_argument_count (new_args, arg_count);
1317 new_args->jump_functions = (struct ipa_jump_func *)
1318 duplicate_ggc_array (old_args->jump_functions,
1319 sizeof (struct ipa_jump_func) * arg_count);
1322 /* Hook that is called by cgraph.c when a node is duplicated. */
1325 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1326 __attribute__((unused)) void *data)
1328 struct ipa_node_params *old_info, *new_info;
1329 struct ipa_param_call_note *note;
1332 ipa_check_create_node_params ();
1333 old_info = IPA_NODE_REF (src);
1334 new_info = IPA_NODE_REF (dst);
1335 param_count = ipa_get_param_count (old_info);
1337 ipa_set_param_count (new_info, param_count);
1338 new_info->params = (struct ipa_param_descriptor *)
1339 duplicate_array (old_info->params,
1340 sizeof (struct ipa_param_descriptor) * param_count);
1341 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1342 new_info->count_scale = old_info->count_scale;
1344 for (note = old_info->param_calls; note; note = note->next)
1346 struct ipa_param_call_note *nn;
1348 nn = (struct ipa_param_call_note *)
1349 xcalloc (1, sizeof (struct ipa_param_call_note));
1350 memcpy (nn, note, sizeof (struct ipa_param_call_note));
1351 nn->next = new_info->param_calls;
1352 new_info->param_calls = nn;
1356 /* Register our cgraph hooks if they are not already there. */
1359 ipa_register_cgraph_hooks (void)
1361 if (!edge_removal_hook_holder)
1362 edge_removal_hook_holder =
1363 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
1364 if (!node_removal_hook_holder)
1365 node_removal_hook_holder =
1366 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
1367 if (!edge_duplication_hook_holder)
1368 edge_duplication_hook_holder =
1369 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
1370 if (!node_duplication_hook_holder)
1371 node_duplication_hook_holder =
1372 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
1375 /* Unregister our cgraph hooks if they are not already there. */
1378 ipa_unregister_cgraph_hooks (void)
1380 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
1381 edge_removal_hook_holder = NULL;
1382 cgraph_remove_node_removal_hook (node_removal_hook_holder);
1383 node_removal_hook_holder = NULL;
1384 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
1385 edge_duplication_hook_holder = NULL;
1386 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
1387 node_duplication_hook_holder = NULL;
1390 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1391 longer needed after ipa-cp. */
1394 free_all_ipa_structures_after_ipa_cp (void)
1396 if (!flag_indirect_inlining)
1398 ipa_free_all_edge_args ();
1399 ipa_free_all_node_params ();
1400 ipa_unregister_cgraph_hooks ();
1404 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1405 longer needed after indirect inlining. */
1408 free_all_ipa_structures_after_iinln (void)
1410 ipa_free_all_edge_args ();
1411 ipa_free_all_node_params ();
1412 ipa_unregister_cgraph_hooks ();
1415 /* Print ipa_tree_map data structures of all functions in the
1419 ipa_print_node_params (FILE * f, struct cgraph_node *node)
1423 struct ipa_node_params *info;
1425 if (!node->analyzed)
1427 info = IPA_NODE_REF (node);
1428 fprintf (f, " function %s Trees :: \n", cgraph_node_name (node));
1429 count = ipa_get_param_count (info);
1430 for (i = 0; i < count; i++)
1432 temp = ipa_get_param (info, i);
1433 if (TREE_CODE (temp) == PARM_DECL)
1434 fprintf (f, " param %d : %s", i,
1436 ? (*lang_hooks.decl_printable_name) (temp, 2)
1438 if (ipa_is_param_modified (info, i))
1439 fprintf (f, " modified");
1444 /* Print ipa_tree_map data structures of all functions in the
1448 ipa_print_all_params (FILE * f)
1450 struct cgraph_node *node;
1452 fprintf (f, "\nFunction parameters:\n");
1453 for (node = cgraph_nodes; node; node = node->next)
1454 ipa_print_node_params (f, node);
1457 /* Return a heap allocated vector containing formal parameters of FNDECL. */
1460 ipa_get_vector_of_formal_parms (tree fndecl)
1462 VEC(tree, heap) *args;
1466 count = count_formal_params_1 (fndecl);
1467 args = VEC_alloc (tree, heap, count);
1468 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
1469 VEC_quick_push (tree, args, parm);
1474 /* Return a heap allocated vector containing types of formal parameters of
1475 function type FNTYPE. */
1477 static inline VEC(tree, heap) *
1478 get_vector_of_formal_parm_types (tree fntype)
1480 VEC(tree, heap) *types;
1484 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1487 types = VEC_alloc (tree, heap, count);
1488 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
1489 VEC_quick_push (tree, types, TREE_VALUE (t));
1494 /* Modify the function declaration FNDECL and its type according to the plan in
1495 ADJUSTMENTS. It also sets base fields of individual adjustments structures
1496 to reflect the actual parameters being modified which are determined by the
1497 base_index field. */
1500 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
1501 const char *synth_parm_prefix)
1503 VEC(tree, heap) *oparms, *otypes;
1504 tree orig_type, new_type = NULL;
1505 tree old_arg_types, t, new_arg_types = NULL;
1506 tree parm, *link = &DECL_ARGUMENTS (fndecl);
1507 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1508 tree new_reversed = NULL;
1509 bool care_for_types, last_parm_void;
1511 if (!synth_parm_prefix)
1512 synth_parm_prefix = "SYNTH";
1514 oparms = ipa_get_vector_of_formal_parms (fndecl);
1515 orig_type = TREE_TYPE (fndecl);
1516 old_arg_types = TYPE_ARG_TYPES (orig_type);
1518 /* The following test is an ugly hack, some functions simply don't have any
1519 arguments in their type. This is probably a bug but well... */
1520 care_for_types = (old_arg_types != NULL_TREE);
1523 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
1525 otypes = get_vector_of_formal_parm_types (orig_type);
1527 gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
1529 gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
1533 last_parm_void = false;
1537 for (i = 0; i < len; i++)
1539 struct ipa_parm_adjustment *adj;
1542 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1543 parm = VEC_index (tree, oparms, adj->base_index);
1546 if (adj->copy_param)
1549 new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
1553 link = &TREE_CHAIN (parm);
1555 else if (!adj->remove_param)
1561 ptype = build_pointer_type (adj->type);
1566 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
1568 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
1570 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
1572 DECL_ARTIFICIAL (new_parm) = 1;
1573 DECL_ARG_TYPE (new_parm) = ptype;
1574 DECL_CONTEXT (new_parm) = fndecl;
1575 TREE_USED (new_parm) = 1;
1576 DECL_IGNORED_P (new_parm) = 1;
1577 layout_decl (new_parm, 0);
1579 add_referenced_var (new_parm);
1580 mark_sym_for_renaming (new_parm);
1582 adj->reduction = new_parm;
1586 link = &TREE_CHAIN (new_parm);
1594 new_reversed = nreverse (new_arg_types);
1598 TREE_CHAIN (new_arg_types) = void_list_node;
1600 new_reversed = void_list_node;
1604 /* Use copy_node to preserve as much as possible from original type
1605 (debug info, attribute lists etc.)
1606 Exception is METHOD_TYPEs must have THIS argument.
1607 When we are asked to remove it, we need to build new FUNCTION_TYPE
1609 if (TREE_CODE (orig_type) != METHOD_TYPE
1610 || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
1611 && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
1613 new_type = copy_node (orig_type);
1614 TYPE_ARG_TYPES (new_type) = new_reversed;
1619 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
1621 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
1622 DECL_VINDEX (fndecl) = NULL_TREE;
1625 /* This is a new type, not a copy of an old type. Need to reassociate
1626 variants. We can handle everything except the main variant lazily. */
1627 t = TYPE_MAIN_VARIANT (orig_type);
1630 TYPE_MAIN_VARIANT (new_type) = t;
1631 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
1632 TYPE_NEXT_VARIANT (t) = new_type;
1636 TYPE_MAIN_VARIANT (new_type) = new_type;
1637 TYPE_NEXT_VARIANT (new_type) = NULL;
1640 TREE_TYPE (fndecl) = new_type;
1642 VEC_free (tree, heap, otypes);
1643 VEC_free (tree, heap, oparms);
1646 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1647 If this is a directly recursive call, CS must be NULL. Otherwise it must
1648 contain the corresponding call graph edge. */
1651 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
1652 ipa_parm_adjustment_vec adjustments)
1654 VEC(tree, heap) *vargs;
1656 gimple_stmt_iterator gsi;
1660 len = VEC_length (ipa_parm_adjustment_t, adjustments);
1661 vargs = VEC_alloc (tree, heap, len);
1663 gsi = gsi_for_stmt (stmt);
1664 for (i = 0; i < len; i++)
1666 struct ipa_parm_adjustment *adj;
1668 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1670 if (adj->copy_param)
1672 tree arg = gimple_call_arg (stmt, adj->base_index);
1674 VEC_quick_push (tree, vargs, arg);
1676 else if (!adj->remove_param)
1678 tree expr, orig_expr;
1679 bool allow_ptr, repl_found;
1681 orig_expr = expr = gimple_call_arg (stmt, adj->base_index);
1682 if (TREE_CODE (expr) == ADDR_EXPR)
1685 expr = TREE_OPERAND (expr, 0);
1690 repl_found = build_ref_for_offset (&expr, TREE_TYPE (expr),
1691 adj->offset, adj->type,
1696 expr = build_fold_addr_expr (expr);
1700 tree ptrtype = build_pointer_type (adj->type);
1702 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1703 expr = build_fold_addr_expr (expr);
1704 if (!useless_type_conversion_p (ptrtype, TREE_TYPE (expr)))
1705 expr = fold_convert (ptrtype, expr);
1706 expr = fold_build2 (POINTER_PLUS_EXPR, ptrtype, expr,
1707 build_int_cst (sizetype,
1708 adj->offset / BITS_PER_UNIT));
1710 expr = fold_build1 (INDIRECT_REF, adj->type, expr);
1712 expr = force_gimple_operand_gsi (&gsi, expr,
1714 || is_gimple_reg_type (adj->type),
1715 NULL, true, GSI_SAME_STMT);
1716 VEC_quick_push (tree, vargs, expr);
1720 if (dump_file && (dump_flags & TDF_DETAILS))
1722 fprintf (dump_file, "replacing stmt:");
1723 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
1726 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
1727 new_stmt = gimple_build_call_vec (callee_decl, vargs);
1728 VEC_free (tree, heap, vargs);
1729 if (gimple_call_lhs (stmt))
1730 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
1732 gimple_set_block (new_stmt, gimple_block (stmt));
1733 if (gimple_has_location (stmt))
1734 gimple_set_location (new_stmt, gimple_location (stmt));
1735 gimple_call_copy_flags (new_stmt, stmt);
1736 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
1738 if (dump_file && (dump_flags & TDF_DETAILS))
1740 fprintf (dump_file, "with stmt:");
1741 print_gimple_stmt (dump_file, new_stmt, 0, 0);
1742 fprintf (dump_file, "\n");
1744 gsi_replace (&gsi, new_stmt, true);
1746 cgraph_set_call_stmt (cs, new_stmt);
1747 update_ssa (TODO_update_ssa);
1748 free_dominance_info (CDI_DOMINATORS);
1751 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
1754 index_in_adjustments_multiple_times_p (int base_index,
1755 ipa_parm_adjustment_vec adjustments)
1757 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1760 for (i = 0; i < len; i++)
1762 struct ipa_parm_adjustment *adj;
1763 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1765 if (adj->base_index == base_index)
1777 /* Return adjustments that should have the same effect on function parameters
1778 and call arguments as if they were first changed according to adjustments in
1779 INNER and then by adjustments in OUTER. */
1781 ipa_parm_adjustment_vec
1782 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
1783 ipa_parm_adjustment_vec outer)
1785 int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
1786 int inlen = VEC_length (ipa_parm_adjustment_t, inner);
1788 ipa_parm_adjustment_vec adjustments, tmp;
1790 tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
1791 for (i = 0; i < inlen; i++)
1793 struct ipa_parm_adjustment *n;
1794 n = VEC_index (ipa_parm_adjustment_t, inner, i);
1796 if (n->remove_param)
1799 VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
1802 adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
1803 for (i = 0; i < outlen; i++)
1805 struct ipa_parm_adjustment *r;
1806 struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
1808 struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
1811 gcc_assert (!in->remove_param);
1812 if (out->remove_param)
1814 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
1816 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1817 memset (r, 0, sizeof (*r));
1818 r->remove_param = true;
1823 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
1824 memset (r, 0, sizeof (*r));
1825 r->base_index = in->base_index;
1826 r->type = out->type;
1828 /* FIXME: Create nonlocal value too. */
1830 if (in->copy_param && out->copy_param)
1831 r->copy_param = true;
1832 else if (in->copy_param)
1833 r->offset = out->offset;
1834 else if (out->copy_param)
1835 r->offset = in->offset;
1837 r->offset = in->offset + out->offset;
1840 for (i = 0; i < inlen; i++)
1842 struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
1845 if (n->remove_param)
1846 VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
1849 VEC_free (ipa_parm_adjustment_t, heap, tmp);
1853 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1854 friendly way, assuming they are meant to be applied to FNDECL. */
1857 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
1860 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
1862 VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
1864 fprintf (file, "IPA param adjustments: ");
1865 for (i = 0; i < len; i++)
1867 struct ipa_parm_adjustment *adj;
1868 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
1871 fprintf (file, " ");
1875 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
1876 print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
1879 fprintf (file, ", base: ");
1880 print_generic_expr (file, adj->base, 0);
1884 fprintf (file, ", reduction: ");
1885 print_generic_expr (file, adj->reduction, 0);
1887 if (adj->new_ssa_base)
1889 fprintf (file, ", new_ssa_base: ");
1890 print_generic_expr (file, adj->new_ssa_base, 0);
1893 if (adj->copy_param)
1894 fprintf (file, ", copy_param");
1895 else if (adj->remove_param)
1896 fprintf (file, ", remove_param");
1898 fprintf (file, ", offset %li", (long) adj->offset);
1900 fprintf (file, ", by_ref");
1901 print_node_brief (file, ", type: ", adj->type, 0);
1902 fprintf (file, "\n");
1904 VEC_free (tree, heap, parms);
1907 /* Stream out jump function JUMP_FUNC to OB. */
1910 ipa_write_jump_function (struct output_block *ob,
1911 struct ipa_jump_func *jump_func)
1913 lto_output_uleb128_stream (ob->main_stream,
1916 switch (jump_func->type)
1918 case IPA_JF_UNKNOWN:
1921 lto_output_tree (ob, jump_func->value.constant, true);
1923 case IPA_JF_PASS_THROUGH:
1924 lto_output_tree (ob, jump_func->value.pass_through.operand, true);
1925 lto_output_uleb128_stream (ob->main_stream,
1926 jump_func->value.pass_through.formal_id);
1927 lto_output_uleb128_stream (ob->main_stream,
1928 jump_func->value.pass_through.operation);
1930 case IPA_JF_ANCESTOR:
1931 lto_output_uleb128_stream (ob->main_stream,
1932 jump_func->value.ancestor.offset);
1933 lto_output_tree (ob, jump_func->value.ancestor.type, true);
1934 lto_output_uleb128_stream (ob->main_stream,
1935 jump_func->value.ancestor.formal_id);
1937 case IPA_JF_CONST_MEMBER_PTR:
1938 lto_output_tree (ob, jump_func->value.member_cst.pfn, true);
1939 lto_output_tree (ob, jump_func->value.member_cst.delta, false);
1944 /* Read in jump function JUMP_FUNC from IB. */
1947 ipa_read_jump_function (struct lto_input_block *ib,
1948 struct ipa_jump_func *jump_func,
1949 struct data_in *data_in)
1951 jump_func->type = (enum jump_func_type) lto_input_uleb128 (ib);
1953 switch (jump_func->type)
1955 case IPA_JF_UNKNOWN:
1958 jump_func->value.constant = lto_input_tree (ib, data_in);
1960 case IPA_JF_PASS_THROUGH:
1961 jump_func->value.pass_through.operand = lto_input_tree (ib, data_in);
1962 jump_func->value.pass_through.formal_id = lto_input_uleb128 (ib);
1963 jump_func->value.pass_through.operation = (enum tree_code) lto_input_uleb128 (ib);
1965 case IPA_JF_ANCESTOR:
1966 jump_func->value.ancestor.offset = lto_input_uleb128 (ib);
1967 jump_func->value.ancestor.type = lto_input_tree (ib, data_in);
1968 jump_func->value.ancestor.formal_id = lto_input_uleb128 (ib);
1970 case IPA_JF_CONST_MEMBER_PTR:
1971 jump_func->value.member_cst.pfn = lto_input_tree (ib, data_in);
1972 jump_func->value.member_cst.delta = lto_input_tree (ib, data_in);
1977 /* Stream out a parameter call note. */
1980 ipa_write_param_call_note (struct output_block *ob,
1981 struct ipa_param_call_note *note)
1983 gcc_assert (!note->processed);
1984 lto_output_uleb128_stream (ob->main_stream, gimple_uid (note->stmt));
1985 lto_output_sleb128_stream (ob->main_stream, note->formal_id);
1986 lto_output_sleb128_stream (ob->main_stream, note->count);
1987 lto_output_sleb128_stream (ob->main_stream, note->frequency);
1988 lto_output_sleb128_stream (ob->main_stream, note->loop_nest);
1991 /* Read in a parameter call note. */
1994 ipa_read_param_call_note (struct lto_input_block *ib,
1995 struct ipa_node_params *info)
1998 struct ipa_param_call_note *note = XCNEW (struct ipa_param_call_note);
2000 note->lto_stmt_uid = (unsigned int) lto_input_uleb128 (ib);
2001 note->formal_id = (int) lto_input_sleb128 (ib);
2002 note->count = (gcov_type) lto_input_sleb128 (ib);
2003 note->frequency = (int) lto_input_sleb128 (ib);
2004 note->loop_nest = (int) lto_input_sleb128 (ib);
2006 note->next = info->param_calls;
2007 info->param_calls = note;
2011 /* Stream out NODE info to OB. */
2014 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2017 lto_cgraph_encoder_t encoder;
2018 struct ipa_node_params *info = IPA_NODE_REF (node);
2020 struct cgraph_edge *e;
2021 struct bitpack_d *bp;
2023 struct ipa_param_call_note *note;
2025 encoder = ob->decl_state->cgraph_node_encoder;
2026 node_ref = lto_cgraph_encoder_encode (encoder, node);
2027 lto_output_uleb128_stream (ob->main_stream, node_ref);
2029 bp = bitpack_create ();
2030 bp_pack_value (bp, info->called_with_var_arguments, 1);
2031 bp_pack_value (bp, info->uses_analysis_done, 1);
2032 gcc_assert (info->modification_analysis_done
2033 || ipa_get_param_count (info) == 0);
2034 gcc_assert (!info->node_enqueued);
2035 gcc_assert (!info->ipcp_orig_node);
2036 for (j = 0; j < ipa_get_param_count (info); j++)
2037 bp_pack_value (bp, info->params[j].modified, 1);
2038 lto_output_bitpack (ob->main_stream, bp);
2039 bitpack_delete (bp);
2040 for (e = node->callees; e; e = e->next_callee)
2042 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2044 lto_output_uleb128_stream (ob->main_stream,
2045 ipa_get_cs_argument_count (args));
2046 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2047 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2050 for (note = info->param_calls; note; note = note->next)
2052 lto_output_uleb128_stream (ob->main_stream, note_count);
2053 for (note = info->param_calls; note; note = note->next)
2054 ipa_write_param_call_note (ob, note);
2057 /* Srtream in NODE info from IB. */
2060 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2061 struct data_in *data_in)
2063 struct ipa_node_params *info = IPA_NODE_REF (node);
2065 struct cgraph_edge *e;
2066 struct bitpack_d *bp;
2069 ipa_initialize_node_params (node);
2071 bp = lto_input_bitpack (ib);
2072 info->called_with_var_arguments = bp_unpack_value (bp, 1);
2073 info->uses_analysis_done = bp_unpack_value (bp, 1);
2074 if (ipa_get_param_count (info) != 0)
2076 info->modification_analysis_done = true;
2077 info->uses_analysis_done = true;
2079 info->node_enqueued = false;
2080 for (k = 0; k < ipa_get_param_count (info); k++)
2081 info->params[k].modified = bp_unpack_value (bp, 1);
2082 bitpack_delete (bp);
2083 for (e = node->callees; e; e = e->next_callee)
2085 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2086 int count = lto_input_uleb128 (ib);
2088 ipa_set_cs_argument_count (args, count);
2092 args->jump_functions = GGC_CNEWVEC (struct ipa_jump_func,
2093 ipa_get_cs_argument_count (args));
2094 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2095 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2098 note_count = lto_input_uleb128 (ib);
2099 for (i = 0; i < note_count; i++)
2100 ipa_read_param_call_note (ib, info);
2103 /* Write jump functions for nodes in SET. */
2106 ipa_prop_write_jump_functions (cgraph_node_set set)
2108 struct cgraph_node *node;
2109 struct output_block *ob = create_output_block (LTO_section_jump_functions);
2110 unsigned int count = 0;
2111 cgraph_node_set_iterator csi;
2113 ob->cgraph_node = NULL;
2115 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2117 node = csi_node (csi);
2118 if (node->analyzed && IPA_NODE_REF (node) != NULL)
2122 lto_output_uleb128_stream (ob->main_stream, count);
2124 /* Process all of the functions. */
2125 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2127 node = csi_node (csi);
2128 if (node->analyzed && IPA_NODE_REF (node) != NULL)
2129 ipa_write_node_info (ob, node);
2131 lto_output_1_stream (ob->main_stream, 0);
2132 produce_asm (ob, NULL);
2133 destroy_output_block (ob);
2136 /* Read section in file FILE_DATA of length LEN with data DATA. */
2139 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2142 const struct lto_function_header *header =
2143 (const struct lto_function_header *) data;
2144 const int32_t cfg_offset = sizeof (struct lto_function_header);
2145 const int32_t main_offset = cfg_offset + header->cfg_size;
2146 const int32_t string_offset = main_offset + header->main_size;
2147 struct data_in *data_in;
2148 struct lto_input_block ib_main;
2152 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2156 lto_data_in_create (file_data, (const char *) data + string_offset,
2157 header->string_size, NULL);
2158 count = lto_input_uleb128 (&ib_main);
2160 for (i = 0; i < count; i++)
2163 struct cgraph_node *node;
2164 lto_cgraph_encoder_t encoder;
2166 index = lto_input_uleb128 (&ib_main);
2167 encoder = file_data->cgraph_node_encoder;
2168 node = lto_cgraph_encoder_deref (encoder, index);
2169 ipa_read_node_info (&ib_main, node, data_in);
2171 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2173 lto_data_in_delete (data_in);
2176 /* Read ipcp jump functions. */
2179 ipa_prop_read_jump_functions (void)
2181 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2182 struct lto_file_decl_data *file_data;
2185 ipa_check_create_node_params ();
2186 ipa_check_create_edge_args ();
2187 ipa_register_cgraph_hooks ();
2189 while ((file_data = file_data_vec[j++]))
2192 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2195 ipa_prop_read_section (file_data, data, len);
2199 /* After merging units, we can get mismatch in argument counts.
2200 Also decl merging might've rendered parameter lists obsolette.
2201 Also compute called_with_variable_arg info. */
2204 ipa_update_after_lto_read (void)
2206 struct cgraph_node *node;
2207 struct cgraph_edge *cs;
2209 ipa_check_create_node_params ();
2210 ipa_check_create_edge_args ();
2212 for (node = cgraph_nodes; node; node = node->next)
2214 ipa_initialize_node_params (node);
2216 for (node = cgraph_nodes; node; node = node->next)
2218 for (cs = node->callees; cs; cs = cs->next_callee)
2220 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
2221 != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
2222 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
2226 /* Walk param call notes of NODE and set their call statements given the uid
2227 stored in each note and STMTS which is an array of statements indexed by the
2231 lto_ipa_fixup_call_notes (struct cgraph_node *node, gimple *stmts)
2233 struct ipa_node_params *info;
2234 struct ipa_param_call_note *note;
2236 ipa_check_create_node_params ();
2237 info = IPA_NODE_REF (node);
2238 note = info->param_calls;
2239 /* If there are no notes or they have already been fixed up (the same fixup
2240 is called for both inlining and ipa-cp), there's nothing to do. */
2241 if (!note || note->stmt)
2246 note->stmt = stmts[note->lto_stmt_uid];