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 /* Bitmap with all UIDs of call graph edges that have been already processed
45 by indirect inlining. */
46 static bitmap iinlining_processed_edges
;
48 /* Holders of ipa cgraph hooks: */
49 static struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
50 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
51 static struct cgraph_2edge_hook_list
*edge_duplication_hook_holder
;
52 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
54 /* Add cgraph NODE described by INFO to the worklist WL regardless of whether
55 it is in one or not. It should almost never be used directly, as opposed to
56 ipa_push_func_to_list. */
59 ipa_push_func_to_list_1 (struct ipa_func_list
**wl
,
60 struct cgraph_node
*node
,
61 struct ipa_node_params
*info
)
63 struct ipa_func_list
*temp
;
65 info
->node_enqueued
= 1;
66 temp
= XCNEW (struct ipa_func_list
);
72 /* Initialize worklist to contain all functions. */
74 struct ipa_func_list
*
75 ipa_init_func_list (void)
77 struct cgraph_node
*node
;
78 struct ipa_func_list
* wl
;
81 for (node
= cgraph_nodes
; node
; node
= node
->next
)
84 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
85 /* Unreachable nodes should have been eliminated before ipcp and
87 gcc_assert (node
->needed
|| node
->reachable
);
88 ipa_push_func_to_list_1 (&wl
, node
, info
);
94 /* Remove a function from the worklist WL and return it. */
97 ipa_pop_func_from_list (struct ipa_func_list
**wl
)
99 struct ipa_node_params
*info
;
100 struct ipa_func_list
*first
;
101 struct cgraph_node
*node
;
108 info
= IPA_NODE_REF (node
);
109 info
->node_enqueued
= 0;
113 /* Return index of the formal whose tree is PTREE in function which corresponds
117 ipa_get_param_decl_index (struct ipa_node_params
*info
, tree ptree
)
121 count
= ipa_get_param_count (info
);
122 for (i
= 0; i
< count
; i
++)
123 if (ipa_get_param(info
, i
) == ptree
)
129 /* Populate the param_decl field in parameter descriptors of INFO that
130 corresponds to NODE. */
133 ipa_populate_param_decls (struct cgraph_node
*node
,
134 struct ipa_node_params
*info
)
142 fnargs
= DECL_ARGUMENTS (fndecl
);
144 for (parm
= fnargs
; parm
; parm
= TREE_CHAIN (parm
))
146 info
->params
[param_num
].decl
= parm
;
151 /* Return how many formal parameters FNDECL has. */
154 count_formal_params_1 (tree fndecl
)
159 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
165 /* Count number of formal parameters in NOTE. Store the result to the
166 appropriate field of INFO. */
169 ipa_count_formal_params (struct cgraph_node
*node
,
170 struct ipa_node_params
*info
)
174 param_num
= count_formal_params_1 (node
->decl
);
175 ipa_set_param_count (info
, param_num
);
178 /* Initialize the ipa_node_params structure associated with NODE by counting
179 the function parameters, creating the descriptors and populating their
183 ipa_initialize_node_params (struct cgraph_node
*node
)
185 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
189 ipa_count_formal_params (node
, info
);
190 info
->params
= XCNEWVEC (struct ipa_param_descriptor
,
191 ipa_get_param_count (info
));
192 ipa_populate_param_decls (node
, info
);
196 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
197 parameters. If OP is a parameter declaration, mark it as modified in the
198 info structure passed in DATA. */
201 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED
,
204 struct ipa_node_params
*info
= (struct ipa_node_params
*) data
;
206 op
= get_base_address (op
);
208 && TREE_CODE (op
) == PARM_DECL
)
210 int index
= ipa_get_param_decl_index (info
, op
);
211 gcc_assert (index
>= 0);
212 info
->params
[index
].modified
= true;
218 /* Compute which formal parameters of function associated with NODE are locally
219 modified or their address is taken. Note that this does not apply on
220 parameters with SSA names but those can and should be analyzed
224 ipa_detect_param_modifications (struct cgraph_node
*node
)
226 tree decl
= node
->decl
;
228 struct function
*func
;
229 gimple_stmt_iterator gsi
;
230 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
232 if (ipa_get_param_count (info
) == 0 || info
->modification_analysis_done
)
235 func
= DECL_STRUCT_FUNCTION (decl
);
236 FOR_EACH_BB_FN (bb
, func
)
237 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
238 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), info
, NULL
,
239 visit_store_addr_for_mod_analysis
,
240 visit_store_addr_for_mod_analysis
);
242 info
->modification_analysis_done
= 1;
245 /* Count number of arguments callsite CS has and store it in
246 ipa_edge_args structure corresponding to this callsite. */
249 ipa_count_arguments (struct cgraph_edge
*cs
)
254 stmt
= cs
->call_stmt
;
255 gcc_assert (is_gimple_call (stmt
));
256 arg_num
= gimple_call_num_args (stmt
);
257 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
258 <= (unsigned) cgraph_edge_max_uid
)
259 VEC_safe_grow_cleared (ipa_edge_args_t
, gc
,
260 ipa_edge_args_vector
, cgraph_edge_max_uid
+ 1);
261 ipa_set_cs_argument_count (IPA_EDGE_REF (cs
), arg_num
);
264 /* Print the jump functions of all arguments on all call graph edges going from
268 ipa_print_node_jump_functions (FILE *f
, struct cgraph_node
*node
)
271 struct cgraph_edge
*cs
;
272 struct ipa_jump_func
*jump_func
;
273 enum jump_func_type type
;
275 fprintf (f
, " Jump functions of caller %s:\n", cgraph_node_name (node
));
276 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
278 if (!ipa_edge_args_info_available_for_edge_p (cs
))
281 fprintf (f
, " callsite %s ", cgraph_node_name (node
));
282 fprintf (f
, "-> %s :: \n", cgraph_node_name (cs
->callee
));
284 count
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
285 for (i
= 0; i
< count
; i
++)
287 jump_func
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), i
);
288 type
= jump_func
->type
;
290 fprintf (f
, " param %d: ", i
);
291 if (type
== IPA_JF_UNKNOWN
)
292 fprintf (f
, "UNKNOWN\n");
293 else if (type
== IPA_JF_CONST
)
295 tree val
= jump_func
->value
.constant
;
296 fprintf (f
, "CONST: ");
297 print_generic_expr (f
, val
, 0);
298 if (TREE_CODE (val
) == ADDR_EXPR
299 && TREE_CODE (TREE_OPERAND (val
, 0)) == CONST_DECL
)
302 print_generic_expr (f
, DECL_INITIAL (TREE_OPERAND (val
, 0)),
307 else if (type
== IPA_JF_CONST_MEMBER_PTR
)
309 fprintf (f
, "CONST MEMBER PTR: ");
310 print_generic_expr (f
, jump_func
->value
.member_cst
.pfn
, 0);
312 print_generic_expr (f
, jump_func
->value
.member_cst
.delta
, 0);
315 else if (type
== IPA_JF_PASS_THROUGH
)
317 fprintf (f
, "PASS THROUGH: ");
318 fprintf (f
, "%d, op %s ",
319 jump_func
->value
.pass_through
.formal_id
,
321 jump_func
->value
.pass_through
.operation
]);
322 if (jump_func
->value
.pass_through
.operation
!= NOP_EXPR
)
323 print_generic_expr (dump_file
,
324 jump_func
->value
.pass_through
.operand
, 0);
325 fprintf (dump_file
, "\n");
327 else if (type
== IPA_JF_ANCESTOR
)
329 fprintf (f
, "ANCESTOR: ");
330 fprintf (f
, "%d, offset "HOST_WIDE_INT_PRINT_DEC
"\n",
331 jump_func
->value
.ancestor
.formal_id
,
332 jump_func
->value
.ancestor
.offset
);
338 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
341 ipa_print_all_jump_functions (FILE *f
)
343 struct cgraph_node
*node
;
345 fprintf (f
, "\nJump functions:\n");
346 for (node
= cgraph_nodes
; node
; node
= node
->next
)
348 ipa_print_node_jump_functions (f
, node
);
352 /* Determine whether passing ssa name NAME constitutes a polynomial
353 pass-through function or getting an address of an acestor and if so, write
354 such a jump function to JFUNC. INFO describes the caller. */
357 compute_complex_pass_through (struct ipa_node_params
*info
,
358 struct ipa_jump_func
*jfunc
,
361 HOST_WIDE_INT offset
, size
, max_size
;
364 gimple stmt
= SSA_NAME_DEF_STMT (name
);
366 if (!is_gimple_assign (stmt
))
368 op1
= gimple_assign_rhs1 (stmt
);
369 op2
= gimple_assign_rhs2 (stmt
);
373 if (TREE_CODE (op1
) != SSA_NAME
374 || !SSA_NAME_IS_DEFAULT_DEF (op1
)
375 || (TREE_CODE_CLASS (gimple_expr_code (stmt
)) != tcc_comparison
376 && !useless_type_conversion_p (TREE_TYPE (name
),
378 || !is_gimple_ip_invariant (op2
))
381 index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (op1
));
384 jfunc
->type
= IPA_JF_PASS_THROUGH
;
385 jfunc
->value
.pass_through
.formal_id
= index
;
386 jfunc
->value
.pass_through
.operation
= gimple_assign_rhs_code (stmt
);
387 jfunc
->value
.pass_through
.operand
= op2
;
392 if (TREE_CODE (op1
) != ADDR_EXPR
)
394 op1
= TREE_OPERAND (op1
, 0);
395 type
= TREE_TYPE (op1
);
397 op1
= get_ref_base_and_extent (op1
, &offset
, &size
, &max_size
);
398 if (TREE_CODE (op1
) != INDIRECT_REF
399 /* If this is a varying address, punt. */
403 op1
= TREE_OPERAND (op1
, 0);
404 if (TREE_CODE (op1
) != SSA_NAME
405 || !SSA_NAME_IS_DEFAULT_DEF (op1
))
408 index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (op1
));
411 jfunc
->type
= IPA_JF_ANCESTOR
;
412 jfunc
->value
.ancestor
.formal_id
= index
;
413 jfunc
->value
.ancestor
.offset
= offset
;
414 jfunc
->value
.ancestor
.type
= type
;
419 /* Determine the jump functions of scalar arguments. Scalar means SSA names
420 and constants of a number of selected types. INFO is the ipa_node_params
421 structure associated with the caller, FUNCTIONS is a pointer to an array of
422 jump function structures associated with CALL which is the call statement
426 compute_scalar_jump_functions (struct ipa_node_params
*info
,
427 struct ipa_jump_func
*functions
,
433 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
435 arg
= gimple_call_arg (call
, num
);
437 if (is_gimple_ip_invariant (arg
))
439 functions
[num
].type
= IPA_JF_CONST
;
440 functions
[num
].value
.constant
= arg
;
442 else if (TREE_CODE (arg
) == SSA_NAME
)
444 if (SSA_NAME_IS_DEFAULT_DEF (arg
))
446 int index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (arg
));
450 functions
[num
].type
= IPA_JF_PASS_THROUGH
;
451 functions
[num
].value
.pass_through
.formal_id
= index
;
452 functions
[num
].value
.pass_through
.operation
= NOP_EXPR
;
456 compute_complex_pass_through (info
, &functions
[num
], arg
);
461 /* Inspect the given TYPE and return true iff it has the same structure (the
462 same number of fields of the same types) as a C++ member pointer. If
463 METHOD_PTR and DELTA are non-NULL, store the trees representing the
464 corresponding fields there. */
467 type_like_member_ptr_p (tree type
, tree
*method_ptr
, tree
*delta
)
471 if (TREE_CODE (type
) != RECORD_TYPE
)
474 fld
= TYPE_FIELDS (type
);
475 if (!fld
|| !POINTER_TYPE_P (TREE_TYPE (fld
))
476 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld
))) != METHOD_TYPE
)
482 fld
= TREE_CHAIN (fld
);
483 if (!fld
|| INTEGRAL_TYPE_P (fld
))
488 if (TREE_CHAIN (fld
))
494 /* Go through arguments of the CALL and for every one that looks like a member
495 pointer, check whether it can be safely declared pass-through and if so,
496 mark that to the corresponding item of jump FUNCTIONS. Return true iff
497 there are non-pass-through member pointers within the arguments. INFO
498 describes formal parameters of the caller. */
501 compute_pass_through_member_ptrs (struct ipa_node_params
*info
,
502 struct ipa_jump_func
*functions
,
505 bool undecided_members
= false;
509 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
511 arg
= gimple_call_arg (call
, num
);
513 if (type_like_member_ptr_p (TREE_TYPE (arg
), NULL
, NULL
))
515 if (TREE_CODE (arg
) == PARM_DECL
)
517 int index
= ipa_get_param_decl_index (info
, arg
);
519 gcc_assert (index
>=0);
520 if (!ipa_is_param_modified (info
, index
))
522 functions
[num
].type
= IPA_JF_PASS_THROUGH
;
523 functions
[num
].value
.pass_through
.formal_id
= index
;
524 functions
[num
].value
.pass_through
.operation
= NOP_EXPR
;
527 undecided_members
= true;
530 undecided_members
= true;
534 return undecided_members
;
537 /* Simple function filling in a member pointer constant jump function (with PFN
538 and DELTA as the constant value) into JFUNC. */
541 fill_member_ptr_cst_jump_function (struct ipa_jump_func
*jfunc
,
542 tree pfn
, tree delta
)
544 jfunc
->type
= IPA_JF_CONST_MEMBER_PTR
;
545 jfunc
->value
.member_cst
.pfn
= pfn
;
546 jfunc
->value
.member_cst
.delta
= delta
;
549 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
550 return the rhs of its defining statement. */
553 get_ssa_def_if_simple_copy (tree rhs
)
555 while (TREE_CODE (rhs
) == SSA_NAME
&& !SSA_NAME_IS_DEFAULT_DEF (rhs
))
557 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs
);
559 if (gimple_assign_single_p (def_stmt
))
560 rhs
= gimple_assign_rhs1 (def_stmt
);
567 /* Traverse statements from CALL backwards, scanning whether the argument ARG
568 which is a member pointer is filled in with constant values. If it is, fill
569 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
570 fields of the record type of the member pointer. To give an example, we
571 look for a pattern looking like the following:
573 D.2515.__pfn ={v} printStuff;
574 D.2515.__delta ={v} 0;
575 i_1 = doprinting (D.2515); */
578 determine_cst_member_ptr (gimple call
, tree arg
, tree method_field
,
579 tree delta_field
, struct ipa_jump_func
*jfunc
)
581 gimple_stmt_iterator gsi
;
582 tree method
= NULL_TREE
;
583 tree delta
= NULL_TREE
;
585 gsi
= gsi_for_stmt (call
);
588 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
590 gimple stmt
= gsi_stmt (gsi
);
593 if (!gimple_assign_single_p (stmt
))
596 lhs
= gimple_assign_lhs (stmt
);
597 rhs
= gimple_assign_rhs1 (stmt
);
599 if (TREE_CODE (lhs
) != COMPONENT_REF
600 || TREE_OPERAND (lhs
, 0) != arg
)
603 fld
= TREE_OPERAND (lhs
, 1);
604 if (!method
&& fld
== method_field
)
606 rhs
= get_ssa_def_if_simple_copy (rhs
);
607 if (TREE_CODE (rhs
) == ADDR_EXPR
608 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == FUNCTION_DECL
609 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs
, 0))) == METHOD_TYPE
)
611 method
= TREE_OPERAND (rhs
, 0);
614 fill_member_ptr_cst_jump_function (jfunc
, rhs
, delta
);
622 if (!delta
&& fld
== delta_field
)
624 rhs
= get_ssa_def_if_simple_copy (rhs
);
625 if (TREE_CODE (rhs
) == INTEGER_CST
)
630 fill_member_ptr_cst_jump_function (jfunc
, rhs
, delta
);
642 /* Go through the arguments of the CALL and for every member pointer within
643 tries determine whether it is a constant. If it is, create a corresponding
644 constant jump function in FUNCTIONS which is an array of jump functions
645 associated with the call. */
648 compute_cst_member_ptr_arguments (struct ipa_jump_func
*functions
,
652 tree arg
, method_field
, delta_field
;
654 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
656 arg
= gimple_call_arg (call
, num
);
658 if (functions
[num
].type
== IPA_JF_UNKNOWN
659 && type_like_member_ptr_p (TREE_TYPE (arg
), &method_field
,
661 determine_cst_member_ptr (call
, arg
, method_field
, delta_field
,
666 /* Compute jump function for all arguments of callsite CS and insert the
667 information in the jump_functions array in the ipa_edge_args corresponding
671 ipa_compute_jump_functions (struct cgraph_edge
*cs
)
673 struct ipa_node_params
*info
= IPA_NODE_REF (cs
->caller
);
674 struct ipa_edge_args
*arguments
= IPA_EDGE_REF (cs
);
677 if (ipa_get_cs_argument_count (arguments
) == 0 || arguments
->jump_functions
)
679 arguments
->jump_functions
= GGC_CNEWVEC (struct ipa_jump_func
,
680 ipa_get_cs_argument_count (arguments
));
682 call
= cs
->call_stmt
;
683 gcc_assert (is_gimple_call (call
));
685 /* We will deal with constants and SSA scalars first: */
686 compute_scalar_jump_functions (info
, arguments
->jump_functions
, call
);
688 /* Let's check whether there are any potential member pointers and if so,
689 whether we can determine their functions as pass_through. */
690 if (!compute_pass_through_member_ptrs (info
, arguments
->jump_functions
, call
))
693 /* Finally, let's check whether we actually pass a new constant member
695 compute_cst_member_ptr_arguments (arguments
->jump_functions
, call
);
698 /* If RHS looks like a rhs of a statement loading pfn from a member
699 pointer formal parameter, return the parameter, otherwise return
700 NULL. If USE_DELTA, then we look for a use of the delta field
701 rather than the pfn. */
704 ipa_get_member_ptr_load_param (tree rhs
, bool use_delta
)
710 if (TREE_CODE (rhs
) != COMPONENT_REF
)
713 rec
= TREE_OPERAND (rhs
, 0);
714 if (TREE_CODE (rec
) != PARM_DECL
715 || !type_like_member_ptr_p (TREE_TYPE (rec
), &ptr_field
, &delta_field
))
718 fld
= TREE_OPERAND (rhs
, 1);
719 if (use_delta
? (fld
== delta_field
) : (fld
== ptr_field
))
725 /* If STMT looks like a statement loading a value from a member pointer formal
726 parameter, this function returns that parameter. */
729 ipa_get_stmt_member_ptr_load_param (gimple stmt
, bool use_delta
)
733 if (!gimple_assign_single_p (stmt
))
736 rhs
= gimple_assign_rhs1 (stmt
);
737 return ipa_get_member_ptr_load_param (rhs
, use_delta
);
740 /* Returns true iff T is an SSA_NAME defined by a statement. */
743 ipa_is_ssa_with_stmt_def (tree t
)
745 if (TREE_CODE (t
) == SSA_NAME
746 && !SSA_NAME_IS_DEFAULT_DEF (t
))
752 /* Create a new indirect call graph edge describing a call to a parameter
753 number FORMAL_ID and and set the called flag of the parameter. NODE is the
754 caller. STMT is the corresponding call statement. */
757 ipa_note_param_call (struct cgraph_node
*node
, int formal_id
, gimple stmt
)
759 struct cgraph_edge
*cs
;
760 basic_block bb
= gimple_bb (stmt
);
763 freq
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
764 cs
= cgraph_create_indirect_edge (node
, stmt
, bb
->count
, freq
,
766 cs
->indirect_info
->param_index
= formal_id
;
769 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
770 (described by INFO). Currently it checks whether the call calls a pointer
771 that is a formal parameter and if so, the parameter is marked with the
772 called flag and an indirect call graph edge describing the call is created.
773 This is very simple for ordinary pointers represented in SSA but not-so-nice
774 when it comes to member pointers. The ugly part of this function does
775 nothing more than trying to match the pattern of such a call. An example of
776 such a pattern is the gimple dump below, the call is on the last line:
779 f$__delta_5 = f.__delta;
780 f$__pfn_24 = f.__pfn;
781 D.2496_3 = (int) f$__pfn_24;
782 D.2497_4 = D.2496_3 & 1;
789 D.2500_7 = (unsigned int) f$__delta_5;
790 D.2501_8 = &S + D.2500_7;
791 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
792 D.2503_10 = *D.2502_9;
793 D.2504_12 = f$__pfn_24 + -1;
794 D.2505_13 = (unsigned int) D.2504_12;
795 D.2506_14 = D.2503_10 + D.2505_13;
796 D.2507_15 = *D.2506_14;
797 iftmp.11_16 = (String:: *) D.2507_15;
800 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
801 D.2500_19 = (unsigned int) f$__delta_5;
802 D.2508_20 = &S + D.2500_19;
803 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
805 Such patterns are results of simple calls to a member pointer:
807 int doprinting (int (MyString::* f)(int) const)
809 MyString S ("somestring");
816 ipa_analyze_call_uses (struct cgraph_node
*node
, struct ipa_node_params
*info
,
819 tree target
= gimple_call_fn (call
);
824 tree rec
, rec2
, cond
;
827 basic_block bb
, virt_bb
, join
;
829 if (TREE_CODE (target
) != SSA_NAME
)
832 var
= SSA_NAME_VAR (target
);
833 if (SSA_NAME_IS_DEFAULT_DEF (target
))
835 /* assuming TREE_CODE (var) == PARM_DECL */
836 index
= ipa_get_param_decl_index (info
, var
);
838 ipa_note_param_call (node
, index
, call
);
842 /* Now we need to try to match the complex pattern of calling a member
845 if (!POINTER_TYPE_P (TREE_TYPE (target
))
846 || TREE_CODE (TREE_TYPE (TREE_TYPE (target
))) != METHOD_TYPE
)
849 def
= SSA_NAME_DEF_STMT (target
);
850 if (gimple_code (def
) != GIMPLE_PHI
)
853 if (gimple_phi_num_args (def
) != 2)
856 /* First, we need to check whether one of these is a load from a member
857 pointer that is a parameter to this function. */
858 n1
= PHI_ARG_DEF (def
, 0);
859 n2
= PHI_ARG_DEF (def
, 1);
860 if (!ipa_is_ssa_with_stmt_def (n1
) || !ipa_is_ssa_with_stmt_def (n2
))
862 d1
= SSA_NAME_DEF_STMT (n1
);
863 d2
= SSA_NAME_DEF_STMT (n2
);
865 if ((rec
= ipa_get_stmt_member_ptr_load_param (d1
, false)))
867 if (ipa_get_stmt_member_ptr_load_param (d2
, false))
871 virt_bb
= gimple_bb (d2
);
873 else if ((rec
= ipa_get_stmt_member_ptr_load_param (d2
, false)))
876 virt_bb
= gimple_bb (d1
);
881 /* Second, we need to check that the basic blocks are laid out in the way
882 corresponding to the pattern. */
884 join
= gimple_bb (def
);
885 if (!single_pred_p (virt_bb
) || !single_succ_p (virt_bb
)
886 || single_pred (virt_bb
) != bb
887 || single_succ (virt_bb
) != join
)
890 /* Third, let's see that the branching is done depending on the least
891 significant bit of the pfn. */
893 branch
= last_stmt (bb
);
894 if (gimple_code (branch
) != GIMPLE_COND
)
897 if (gimple_cond_code (branch
) != NE_EXPR
898 || !integer_zerop (gimple_cond_rhs (branch
)))
901 cond
= gimple_cond_lhs (branch
);
902 if (!ipa_is_ssa_with_stmt_def (cond
))
905 def
= SSA_NAME_DEF_STMT (cond
);
906 if (!is_gimple_assign (def
)
907 || gimple_assign_rhs_code (def
) != BIT_AND_EXPR
908 || !integer_onep (gimple_assign_rhs2 (def
)))
911 cond
= gimple_assign_rhs1 (def
);
912 if (!ipa_is_ssa_with_stmt_def (cond
))
915 def
= SSA_NAME_DEF_STMT (cond
);
917 if (is_gimple_assign (def
)
918 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
920 cond
= gimple_assign_rhs1 (def
);
921 if (!ipa_is_ssa_with_stmt_def (cond
))
923 def
= SSA_NAME_DEF_STMT (cond
);
926 rec2
= ipa_get_stmt_member_ptr_load_param (def
,
927 (TARGET_PTRMEMFUNC_VBIT_LOCATION
928 == ptrmemfunc_vbit_in_delta
));
933 index
= ipa_get_param_decl_index (info
, rec
);
934 if (index
>= 0 && !ipa_is_param_modified (info
, index
))
935 ipa_note_param_call (node
, index
, call
);
940 /* Analyze the call statement STMT with respect to formal parameters (described
941 in INFO) of caller given by NODE. Currently it only checks whether formal
942 parameters are called. */
945 ipa_analyze_stmt_uses (struct cgraph_node
*node
, struct ipa_node_params
*info
,
948 if (is_gimple_call (stmt
))
949 ipa_analyze_call_uses (node
, info
, stmt
);
952 /* Scan the function body of NODE and inspect the uses of formal parameters.
953 Store the findings in various structures of the associated ipa_node_params
954 structure, such as parameter flags, notes etc. */
957 ipa_analyze_params_uses (struct cgraph_node
*node
)
959 tree decl
= node
->decl
;
961 struct function
*func
;
962 gimple_stmt_iterator gsi
;
963 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
965 if (ipa_get_param_count (info
) == 0 || info
->uses_analysis_done
)
968 func
= DECL_STRUCT_FUNCTION (decl
);
969 FOR_EACH_BB_FN (bb
, func
)
971 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
973 gimple stmt
= gsi_stmt (gsi
);
974 ipa_analyze_stmt_uses (node
, info
, stmt
);
978 info
->uses_analysis_done
= 1;
981 /* Update the jump functions associated with call graph edge E when the call
982 graph edge CS is being inlined, assuming that E->caller is already (possibly
983 indirectly) inlined into CS->callee and that E has not been inlined.
985 We keep pass through functions only if they do not contain any operation.
986 This is sufficient for inlining and greately simplifies things. */
989 update_jump_functions_after_inlining (struct cgraph_edge
*cs
,
990 struct cgraph_edge
*e
)
992 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
993 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
994 int count
= ipa_get_cs_argument_count (args
);
997 for (i
= 0; i
< count
; i
++)
999 struct ipa_jump_func
*src
, *dst
= ipa_get_ith_jump_func (args
, i
);
1001 if (dst
->type
== IPA_JF_ANCESTOR
)
1003 dst
->type
= IPA_JF_UNKNOWN
;
1007 if (dst
->type
!= IPA_JF_PASS_THROUGH
)
1010 /* We must check range due to calls with variable number of arguments and
1011 we cannot combine jump functions with operations. */
1012 if (dst
->value
.pass_through
.operation
!= NOP_EXPR
1013 || (dst
->value
.pass_through
.formal_id
1014 >= ipa_get_cs_argument_count (top
)))
1016 dst
->type
= IPA_JF_UNKNOWN
;
1020 src
= ipa_get_ith_jump_func (top
, dst
->value
.pass_through
.formal_id
);
1025 /* Print out a debug message to file F that we have discovered that an indirect
1026 call described by NT is in fact a call of a known constant function described
1027 by JFUNC. NODE is the node where the call is. */
1030 print_edge_addition_message (FILE *f
, struct cgraph_edge
*e
,
1031 struct ipa_jump_func
*jfunc
)
1033 fprintf (f
, "ipa-prop: Discovered an indirect call to a known target (");
1034 if (jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1036 print_node_brief (f
, "", jfunc
->value
.member_cst
.pfn
, 0);
1037 print_node_brief (f
, ", ", jfunc
->value
.member_cst
.delta
, 0);
1040 print_node_brief(f
, "", jfunc
->value
.constant
, 0);
1042 fprintf (f
, ") in %s: ", cgraph_node_name (e
->caller
));
1043 print_gimple_stmt (f
, e
->call_stmt
, 2, TDF_SLIM
);
1046 /* Update the param called notes associated with NODE when CS is being inlined,
1047 assuming NODE is (potentially indirectly) inlined into CS->callee.
1048 Moreover, if the callee is discovered to be constant, create a new cgraph
1049 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1050 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1053 update_indirect_edges_after_inlining (struct cgraph_edge
*cs
,
1054 struct cgraph_node
*node
,
1055 VEC (cgraph_edge_p
, heap
) **new_edges
)
1057 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
1058 struct cgraph_edge
*ie
, *next_ie
;
1061 ipa_check_create_edge_args ();
1063 for (ie
= node
->indirect_calls
; ie
; ie
= next_ie
)
1065 struct cgraph_indirect_call_info
*ici
= ie
->indirect_info
;
1066 struct ipa_jump_func
*jfunc
;
1068 next_ie
= ie
->next_callee
;
1069 if (bitmap_bit_p (iinlining_processed_edges
, ie
->uid
))
1072 /* If we ever use indirect edges for anything other than indirect
1073 inlining, we will need to skip those with negative param_indices. */
1074 gcc_assert (ici
->param_index
>= 0);
1076 /* We must check range due to calls with variable number of arguments: */
1077 if (ici
->param_index
>= ipa_get_cs_argument_count (top
))
1079 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
1083 jfunc
= ipa_get_ith_jump_func (top
, ici
->param_index
);
1084 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1085 && jfunc
->value
.pass_through
.operation
== NOP_EXPR
)
1086 ici
->param_index
= jfunc
->value
.pass_through
.formal_id
;
1087 else if (jfunc
->type
== IPA_JF_CONST
1088 || jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1090 struct cgraph_node
*callee
;
1093 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
1094 if (jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1095 decl
= jfunc
->value
.member_cst
.pfn
;
1097 decl
= jfunc
->value
.constant
;
1099 if (TREE_CODE (decl
) != ADDR_EXPR
)
1101 decl
= TREE_OPERAND (decl
, 0);
1103 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1105 callee
= cgraph_node (decl
);
1106 if (!callee
|| !callee
->local
.inlinable
)
1111 print_edge_addition_message (dump_file
, ie
, jfunc
);
1113 cgraph_make_edge_direct (ie
, callee
);
1114 ie
->indirect_inlining_edge
= 1;
1116 VEC_safe_push (cgraph_edge_p
, heap
, *new_edges
, ie
);
1117 top
= IPA_EDGE_REF (cs
);
1121 /* Ancestor jump functions and pass theoughs with operations should
1122 not be used on parameters that then get called. */
1123 gcc_assert (jfunc
->type
== IPA_JF_UNKNOWN
);
1124 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
1131 /* Recursively traverse subtree of NODE (including node) made of inlined
1132 cgraph_edges when CS has been inlined and invoke
1133 update_indirect_edges_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_indirect_edges_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 memset (info
, 0, sizeof (*info
));
1222 /* Free all ipa_node_params structures. */
1225 ipa_free_all_node_params (void)
1228 struct ipa_node_params
*info
;
1231 VEC_iterate (ipa_node_params_t
, ipa_node_params_vector
, i
, info
);
1233 ipa_free_node_params_substructures (info
);
1235 VEC_free (ipa_node_params_t
, heap
, ipa_node_params_vector
);
1236 ipa_node_params_vector
= NULL
;
1239 /* Hook that is called by cgraph.c when an edge is removed. */
1242 ipa_edge_removal_hook (struct cgraph_edge
*cs
, void *data ATTRIBUTE_UNUSED
)
1244 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1245 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
1246 <= (unsigned)cs
->uid
)
1248 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs
));
1251 /* Hook that is called by cgraph.c when a node is removed. */
1254 ipa_node_removal_hook (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
1256 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1257 if (VEC_length (ipa_node_params_t
, ipa_node_params_vector
)
1258 <= (unsigned)node
->uid
)
1260 ipa_free_node_params_substructures (IPA_NODE_REF (node
));
1263 /* Helper function to duplicate an array of size N that is at SRC and store a
1264 pointer to it to DST. Nothing is done if SRC is NULL. */
1267 duplicate_array (void *src
, size_t n
)
1279 /* Like duplicate_array byt in GGC memory. */
1282 duplicate_ggc_array (void *src
, size_t n
)
1294 /* Hook that is called by cgraph.c when a node is duplicated. */
1297 ipa_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
1298 __attribute__((unused
)) void *data
)
1300 struct ipa_edge_args
*old_args
, *new_args
;
1303 ipa_check_create_edge_args ();
1305 old_args
= IPA_EDGE_REF (src
);
1306 new_args
= IPA_EDGE_REF (dst
);
1308 arg_count
= ipa_get_cs_argument_count (old_args
);
1309 ipa_set_cs_argument_count (new_args
, arg_count
);
1310 new_args
->jump_functions
= (struct ipa_jump_func
*)
1311 duplicate_ggc_array (old_args
->jump_functions
,
1312 sizeof (struct ipa_jump_func
) * arg_count
);
1314 if (iinlining_processed_edges
1315 && bitmap_bit_p (iinlining_processed_edges
, src
->uid
))
1316 bitmap_set_bit (iinlining_processed_edges
, dst
->uid
);
1319 /* Hook that is called by cgraph.c when a node is duplicated. */
1322 ipa_node_duplication_hook (struct cgraph_node
*src
, struct cgraph_node
*dst
,
1323 __attribute__((unused
)) void *data
)
1325 struct ipa_node_params
*old_info
, *new_info
;
1328 ipa_check_create_node_params ();
1329 old_info
= IPA_NODE_REF (src
);
1330 new_info
= IPA_NODE_REF (dst
);
1331 param_count
= ipa_get_param_count (old_info
);
1333 ipa_set_param_count (new_info
, param_count
);
1334 new_info
->params
= (struct ipa_param_descriptor
*)
1335 duplicate_array (old_info
->params
,
1336 sizeof (struct ipa_param_descriptor
) * param_count
);
1337 new_info
->ipcp_orig_node
= old_info
->ipcp_orig_node
;
1338 new_info
->count_scale
= old_info
->count_scale
;
1341 /* Register our cgraph hooks if they are not already there. */
1344 ipa_register_cgraph_hooks (void)
1346 if (!edge_removal_hook_holder
)
1347 edge_removal_hook_holder
=
1348 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook
, NULL
);
1349 if (!node_removal_hook_holder
)
1350 node_removal_hook_holder
=
1351 cgraph_add_node_removal_hook (&ipa_node_removal_hook
, NULL
);
1352 if (!edge_duplication_hook_holder
)
1353 edge_duplication_hook_holder
=
1354 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook
, NULL
);
1355 if (!node_duplication_hook_holder
)
1356 node_duplication_hook_holder
=
1357 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook
, NULL
);
1360 /* Unregister our cgraph hooks if they are not already there. */
1363 ipa_unregister_cgraph_hooks (void)
1365 cgraph_remove_edge_removal_hook (edge_removal_hook_holder
);
1366 edge_removal_hook_holder
= NULL
;
1367 cgraph_remove_node_removal_hook (node_removal_hook_holder
);
1368 node_removal_hook_holder
= NULL
;
1369 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder
);
1370 edge_duplication_hook_holder
= NULL
;
1371 cgraph_remove_node_duplication_hook (node_duplication_hook_holder
);
1372 node_duplication_hook_holder
= NULL
;
1375 /* Allocate all necessary data strucutures necessary for indirect inlining. */
1378 ipa_create_all_structures_for_iinln (void)
1380 iinlining_processed_edges
= BITMAP_ALLOC (NULL
);
1383 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1384 longer needed after ipa-cp. */
1387 ipa_free_all_structures_after_ipa_cp (void)
1389 if (!flag_indirect_inlining
)
1391 ipa_free_all_edge_args ();
1392 ipa_free_all_node_params ();
1393 ipa_unregister_cgraph_hooks ();
1397 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1398 longer needed after indirect inlining. */
1401 ipa_free_all_structures_after_iinln (void)
1403 BITMAP_FREE (iinlining_processed_edges
);
1405 ipa_free_all_edge_args ();
1406 ipa_free_all_node_params ();
1407 ipa_unregister_cgraph_hooks ();
1410 /* Print ipa_tree_map data structures of all functions in the
1414 ipa_print_node_params (FILE * f
, struct cgraph_node
*node
)
1418 struct ipa_node_params
*info
;
1420 if (!node
->analyzed
)
1422 info
= IPA_NODE_REF (node
);
1423 fprintf (f
, " function %s Trees :: \n", cgraph_node_name (node
));
1424 count
= ipa_get_param_count (info
);
1425 for (i
= 0; i
< count
; i
++)
1427 temp
= ipa_get_param (info
, i
);
1428 if (TREE_CODE (temp
) == PARM_DECL
)
1429 fprintf (f
, " param %d : %s", i
,
1431 ? (*lang_hooks
.decl_printable_name
) (temp
, 2)
1433 if (ipa_is_param_modified (info
, i
))
1434 fprintf (f
, " modified");
1439 /* Print ipa_tree_map data structures of all functions in the
1443 ipa_print_all_params (FILE * f
)
1445 struct cgraph_node
*node
;
1447 fprintf (f
, "\nFunction parameters:\n");
1448 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1449 ipa_print_node_params (f
, node
);
1452 /* Return a heap allocated vector containing formal parameters of FNDECL. */
1455 ipa_get_vector_of_formal_parms (tree fndecl
)
1457 VEC(tree
, heap
) *args
;
1461 count
= count_formal_params_1 (fndecl
);
1462 args
= VEC_alloc (tree
, heap
, count
);
1463 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
1464 VEC_quick_push (tree
, args
, parm
);
1469 /* Return a heap allocated vector containing types of formal parameters of
1470 function type FNTYPE. */
1472 static inline VEC(tree
, heap
) *
1473 get_vector_of_formal_parm_types (tree fntype
)
1475 VEC(tree
, heap
) *types
;
1479 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1482 types
= VEC_alloc (tree
, heap
, count
);
1483 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1484 VEC_quick_push (tree
, types
, TREE_VALUE (t
));
1489 /* Modify the function declaration FNDECL and its type according to the plan in
1490 ADJUSTMENTS. It also sets base fields of individual adjustments structures
1491 to reflect the actual parameters being modified which are determined by the
1492 base_index field. */
1495 ipa_modify_formal_parameters (tree fndecl
, ipa_parm_adjustment_vec adjustments
,
1496 const char *synth_parm_prefix
)
1498 VEC(tree
, heap
) *oparms
, *otypes
;
1499 tree orig_type
, new_type
= NULL
;
1500 tree old_arg_types
, t
, new_arg_types
= NULL
;
1501 tree parm
, *link
= &DECL_ARGUMENTS (fndecl
);
1502 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1503 tree new_reversed
= NULL
;
1504 bool care_for_types
, last_parm_void
;
1506 if (!synth_parm_prefix
)
1507 synth_parm_prefix
= "SYNTH";
1509 oparms
= ipa_get_vector_of_formal_parms (fndecl
);
1510 orig_type
= TREE_TYPE (fndecl
);
1511 old_arg_types
= TYPE_ARG_TYPES (orig_type
);
1513 /* The following test is an ugly hack, some functions simply don't have any
1514 arguments in their type. This is probably a bug but well... */
1515 care_for_types
= (old_arg_types
!= NULL_TREE
);
1518 last_parm_void
= (TREE_VALUE (tree_last (old_arg_types
))
1520 otypes
= get_vector_of_formal_parm_types (orig_type
);
1522 gcc_assert (VEC_length (tree
, oparms
) + 1 == VEC_length (tree
, otypes
));
1524 gcc_assert (VEC_length (tree
, oparms
) == VEC_length (tree
, otypes
));
1528 last_parm_void
= false;
1532 for (i
= 0; i
< len
; i
++)
1534 struct ipa_parm_adjustment
*adj
;
1537 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1538 parm
= VEC_index (tree
, oparms
, adj
->base_index
);
1541 if (adj
->copy_param
)
1544 new_arg_types
= tree_cons (NULL_TREE
, VEC_index (tree
, otypes
,
1548 link
= &TREE_CHAIN (parm
);
1550 else if (!adj
->remove_param
)
1556 ptype
= build_pointer_type (adj
->type
);
1561 new_arg_types
= tree_cons (NULL_TREE
, ptype
, new_arg_types
);
1563 new_parm
= build_decl (UNKNOWN_LOCATION
, PARM_DECL
, NULL_TREE
,
1565 DECL_NAME (new_parm
) = create_tmp_var_name (synth_parm_prefix
);
1567 DECL_ARTIFICIAL (new_parm
) = 1;
1568 DECL_ARG_TYPE (new_parm
) = ptype
;
1569 DECL_CONTEXT (new_parm
) = fndecl
;
1570 TREE_USED (new_parm
) = 1;
1571 DECL_IGNORED_P (new_parm
) = 1;
1572 layout_decl (new_parm
, 0);
1574 add_referenced_var (new_parm
);
1575 mark_sym_for_renaming (new_parm
);
1577 adj
->reduction
= new_parm
;
1581 link
= &TREE_CHAIN (new_parm
);
1589 new_reversed
= nreverse (new_arg_types
);
1593 TREE_CHAIN (new_arg_types
) = void_list_node
;
1595 new_reversed
= void_list_node
;
1599 /* Use copy_node to preserve as much as possible from original type
1600 (debug info, attribute lists etc.)
1601 Exception is METHOD_TYPEs must have THIS argument.
1602 When we are asked to remove it, we need to build new FUNCTION_TYPE
1604 if (TREE_CODE (orig_type
) != METHOD_TYPE
1605 || (VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->copy_param
1606 && VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->base_index
== 0))
1608 new_type
= copy_node (orig_type
);
1609 TYPE_ARG_TYPES (new_type
) = new_reversed
;
1614 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type
),
1616 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
1617 DECL_VINDEX (fndecl
) = NULL_TREE
;
1620 /* This is a new type, not a copy of an old type. Need to reassociate
1621 variants. We can handle everything except the main variant lazily. */
1622 t
= TYPE_MAIN_VARIANT (orig_type
);
1625 TYPE_MAIN_VARIANT (new_type
) = t
;
1626 TYPE_NEXT_VARIANT (new_type
) = TYPE_NEXT_VARIANT (t
);
1627 TYPE_NEXT_VARIANT (t
) = new_type
;
1631 TYPE_MAIN_VARIANT (new_type
) = new_type
;
1632 TYPE_NEXT_VARIANT (new_type
) = NULL
;
1635 TREE_TYPE (fndecl
) = new_type
;
1637 VEC_free (tree
, heap
, otypes
);
1638 VEC_free (tree
, heap
, oparms
);
1641 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1642 If this is a directly recursive call, CS must be NULL. Otherwise it must
1643 contain the corresponding call graph edge. */
1646 ipa_modify_call_arguments (struct cgraph_edge
*cs
, gimple stmt
,
1647 ipa_parm_adjustment_vec adjustments
)
1649 VEC(tree
, heap
) *vargs
;
1651 gimple_stmt_iterator gsi
;
1655 len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1656 vargs
= VEC_alloc (tree
, heap
, len
);
1658 gsi
= gsi_for_stmt (stmt
);
1659 for (i
= 0; i
< len
; i
++)
1661 struct ipa_parm_adjustment
*adj
;
1663 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1665 if (adj
->copy_param
)
1667 tree arg
= gimple_call_arg (stmt
, adj
->base_index
);
1669 VEC_quick_push (tree
, vargs
, arg
);
1671 else if (!adj
->remove_param
)
1673 tree expr
, orig_expr
;
1674 bool allow_ptr
, repl_found
;
1676 orig_expr
= expr
= gimple_call_arg (stmt
, adj
->base_index
);
1677 if (TREE_CODE (expr
) == ADDR_EXPR
)
1680 expr
= TREE_OPERAND (expr
, 0);
1685 repl_found
= build_ref_for_offset (&expr
, TREE_TYPE (expr
),
1686 adj
->offset
, adj
->type
,
1691 expr
= build_fold_addr_expr (expr
);
1695 tree ptrtype
= build_pointer_type (adj
->type
);
1697 if (!POINTER_TYPE_P (TREE_TYPE (expr
)))
1698 expr
= build_fold_addr_expr (expr
);
1699 if (!useless_type_conversion_p (ptrtype
, TREE_TYPE (expr
)))
1700 expr
= fold_convert (ptrtype
, expr
);
1701 expr
= fold_build2 (POINTER_PLUS_EXPR
, ptrtype
, expr
,
1702 build_int_cst (sizetype
,
1703 adj
->offset
/ BITS_PER_UNIT
));
1705 expr
= fold_build1 (INDIRECT_REF
, adj
->type
, expr
);
1707 expr
= force_gimple_operand_gsi (&gsi
, expr
,
1709 || is_gimple_reg_type (adj
->type
),
1710 NULL
, true, GSI_SAME_STMT
);
1711 VEC_quick_push (tree
, vargs
, expr
);
1715 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1717 fprintf (dump_file
, "replacing stmt:");
1718 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0, 0);
1721 callee_decl
= !cs
? gimple_call_fndecl (stmt
) : cs
->callee
->decl
;
1722 new_stmt
= gimple_build_call_vec (callee_decl
, vargs
);
1723 VEC_free (tree
, heap
, vargs
);
1724 if (gimple_call_lhs (stmt
))
1725 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
1727 gimple_set_block (new_stmt
, gimple_block (stmt
));
1728 if (gimple_has_location (stmt
))
1729 gimple_set_location (new_stmt
, gimple_location (stmt
));
1730 gimple_call_copy_flags (new_stmt
, stmt
);
1731 gimple_call_set_chain (new_stmt
, gimple_call_chain (stmt
));
1733 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1735 fprintf (dump_file
, "with stmt:");
1736 print_gimple_stmt (dump_file
, new_stmt
, 0, 0);
1737 fprintf (dump_file
, "\n");
1739 gsi_replace (&gsi
, new_stmt
, true);
1741 cgraph_set_call_stmt (cs
, new_stmt
);
1742 update_ssa (TODO_update_ssa
);
1743 free_dominance_info (CDI_DOMINATORS
);
1746 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
1749 index_in_adjustments_multiple_times_p (int base_index
,
1750 ipa_parm_adjustment_vec adjustments
)
1752 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1755 for (i
= 0; i
< len
; i
++)
1757 struct ipa_parm_adjustment
*adj
;
1758 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1760 if (adj
->base_index
== base_index
)
1772 /* Return adjustments that should have the same effect on function parameters
1773 and call arguments as if they were first changed according to adjustments in
1774 INNER and then by adjustments in OUTER. */
1776 ipa_parm_adjustment_vec
1777 ipa_combine_adjustments (ipa_parm_adjustment_vec inner
,
1778 ipa_parm_adjustment_vec outer
)
1780 int i
, outlen
= VEC_length (ipa_parm_adjustment_t
, outer
);
1781 int inlen
= VEC_length (ipa_parm_adjustment_t
, inner
);
1783 ipa_parm_adjustment_vec adjustments
, tmp
;
1785 tmp
= VEC_alloc (ipa_parm_adjustment_t
, heap
, inlen
);
1786 for (i
= 0; i
< inlen
; i
++)
1788 struct ipa_parm_adjustment
*n
;
1789 n
= VEC_index (ipa_parm_adjustment_t
, inner
, i
);
1791 if (n
->remove_param
)
1794 VEC_quick_push (ipa_parm_adjustment_t
, tmp
, n
);
1797 adjustments
= VEC_alloc (ipa_parm_adjustment_t
, heap
, outlen
+ removals
);
1798 for (i
= 0; i
< outlen
; i
++)
1800 struct ipa_parm_adjustment
*r
;
1801 struct ipa_parm_adjustment
*out
= VEC_index (ipa_parm_adjustment_t
,
1803 struct ipa_parm_adjustment
*in
= VEC_index (ipa_parm_adjustment_t
, tmp
,
1806 gcc_assert (!in
->remove_param
);
1807 if (out
->remove_param
)
1809 if (!index_in_adjustments_multiple_times_p (in
->base_index
, tmp
))
1811 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1812 memset (r
, 0, sizeof (*r
));
1813 r
->remove_param
= true;
1818 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1819 memset (r
, 0, sizeof (*r
));
1820 r
->base_index
= in
->base_index
;
1821 r
->type
= out
->type
;
1823 /* FIXME: Create nonlocal value too. */
1825 if (in
->copy_param
&& out
->copy_param
)
1826 r
->copy_param
= true;
1827 else if (in
->copy_param
)
1828 r
->offset
= out
->offset
;
1829 else if (out
->copy_param
)
1830 r
->offset
= in
->offset
;
1832 r
->offset
= in
->offset
+ out
->offset
;
1835 for (i
= 0; i
< inlen
; i
++)
1837 struct ipa_parm_adjustment
*n
= VEC_index (ipa_parm_adjustment_t
,
1840 if (n
->remove_param
)
1841 VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, n
);
1844 VEC_free (ipa_parm_adjustment_t
, heap
, tmp
);
1848 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1849 friendly way, assuming they are meant to be applied to FNDECL. */
1852 ipa_dump_param_adjustments (FILE *file
, ipa_parm_adjustment_vec adjustments
,
1855 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1857 VEC(tree
, heap
) *parms
= ipa_get_vector_of_formal_parms (fndecl
);
1859 fprintf (file
, "IPA param adjustments: ");
1860 for (i
= 0; i
< len
; i
++)
1862 struct ipa_parm_adjustment
*adj
;
1863 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1866 fprintf (file
, " ");
1870 fprintf (file
, "%i. base_index: %i - ", i
, adj
->base_index
);
1871 print_generic_expr (file
, VEC_index (tree
, parms
, adj
->base_index
), 0);
1874 fprintf (file
, ", base: ");
1875 print_generic_expr (file
, adj
->base
, 0);
1879 fprintf (file
, ", reduction: ");
1880 print_generic_expr (file
, adj
->reduction
, 0);
1882 if (adj
->new_ssa_base
)
1884 fprintf (file
, ", new_ssa_base: ");
1885 print_generic_expr (file
, adj
->new_ssa_base
, 0);
1888 if (adj
->copy_param
)
1889 fprintf (file
, ", copy_param");
1890 else if (adj
->remove_param
)
1891 fprintf (file
, ", remove_param");
1893 fprintf (file
, ", offset %li", (long) adj
->offset
);
1895 fprintf (file
, ", by_ref");
1896 print_node_brief (file
, ", type: ", adj
->type
, 0);
1897 fprintf (file
, "\n");
1899 VEC_free (tree
, heap
, parms
);
1902 /* Stream out jump function JUMP_FUNC to OB. */
1905 ipa_write_jump_function (struct output_block
*ob
,
1906 struct ipa_jump_func
*jump_func
)
1908 lto_output_uleb128_stream (ob
->main_stream
,
1911 switch (jump_func
->type
)
1913 case IPA_JF_UNKNOWN
:
1916 lto_output_tree (ob
, jump_func
->value
.constant
, true);
1918 case IPA_JF_PASS_THROUGH
:
1919 lto_output_tree (ob
, jump_func
->value
.pass_through
.operand
, true);
1920 lto_output_uleb128_stream (ob
->main_stream
,
1921 jump_func
->value
.pass_through
.formal_id
);
1922 lto_output_uleb128_stream (ob
->main_stream
,
1923 jump_func
->value
.pass_through
.operation
);
1925 case IPA_JF_ANCESTOR
:
1926 lto_output_uleb128_stream (ob
->main_stream
,
1927 jump_func
->value
.ancestor
.offset
);
1928 lto_output_tree (ob
, jump_func
->value
.ancestor
.type
, true);
1929 lto_output_uleb128_stream (ob
->main_stream
,
1930 jump_func
->value
.ancestor
.formal_id
);
1932 case IPA_JF_CONST_MEMBER_PTR
:
1933 lto_output_tree (ob
, jump_func
->value
.member_cst
.pfn
, true);
1934 lto_output_tree (ob
, jump_func
->value
.member_cst
.delta
, false);
1939 /* Read in jump function JUMP_FUNC from IB. */
1942 ipa_read_jump_function (struct lto_input_block
*ib
,
1943 struct ipa_jump_func
*jump_func
,
1944 struct data_in
*data_in
)
1946 jump_func
->type
= (enum jump_func_type
) lto_input_uleb128 (ib
);
1948 switch (jump_func
->type
)
1950 case IPA_JF_UNKNOWN
:
1953 jump_func
->value
.constant
= lto_input_tree (ib
, data_in
);
1955 case IPA_JF_PASS_THROUGH
:
1956 jump_func
->value
.pass_through
.operand
= lto_input_tree (ib
, data_in
);
1957 jump_func
->value
.pass_through
.formal_id
= lto_input_uleb128 (ib
);
1958 jump_func
->value
.pass_through
.operation
= (enum tree_code
) lto_input_uleb128 (ib
);
1960 case IPA_JF_ANCESTOR
:
1961 jump_func
->value
.ancestor
.offset
= lto_input_uleb128 (ib
);
1962 jump_func
->value
.ancestor
.type
= lto_input_tree (ib
, data_in
);
1963 jump_func
->value
.ancestor
.formal_id
= lto_input_uleb128 (ib
);
1965 case IPA_JF_CONST_MEMBER_PTR
:
1966 jump_func
->value
.member_cst
.pfn
= lto_input_tree (ib
, data_in
);
1967 jump_func
->value
.member_cst
.delta
= lto_input_tree (ib
, data_in
);
1972 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
1973 relevant to indirect inlining to OB. */
1976 ipa_write_indirect_edge_info (struct output_block
*ob
,
1977 struct cgraph_edge
*cs
)
1979 struct cgraph_indirect_call_info
*ii
= cs
->indirect_info
;
1981 lto_output_sleb128_stream (ob
->main_stream
, ii
->param_index
);
1984 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
1985 relevant to indirect inlining from IB. */
1988 ipa_read_indirect_edge_info (struct lto_input_block
*ib
,
1989 struct data_in
*data_in ATTRIBUTE_UNUSED
,
1990 struct cgraph_edge
*cs
)
1992 struct cgraph_indirect_call_info
*ii
= cs
->indirect_info
;
1994 ii
->param_index
= (int) lto_input_sleb128 (ib
);
1997 /* Stream out NODE info to OB. */
2000 ipa_write_node_info (struct output_block
*ob
, struct cgraph_node
*node
)
2003 lto_cgraph_encoder_t encoder
;
2004 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2006 struct cgraph_edge
*e
;
2007 struct bitpack_d
*bp
;
2009 encoder
= ob
->decl_state
->cgraph_node_encoder
;
2010 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
2011 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
2013 bp
= bitpack_create ();
2014 bp_pack_value (bp
, info
->called_with_var_arguments
, 1);
2015 bp_pack_value (bp
, info
->uses_analysis_done
, 1);
2016 gcc_assert (info
->modification_analysis_done
2017 || ipa_get_param_count (info
) == 0);
2018 gcc_assert (!info
->node_enqueued
);
2019 gcc_assert (!info
->ipcp_orig_node
);
2020 for (j
= 0; j
< ipa_get_param_count (info
); j
++)
2021 bp_pack_value (bp
, info
->params
[j
].modified
, 1);
2022 lto_output_bitpack (ob
->main_stream
, bp
);
2023 bitpack_delete (bp
);
2024 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2026 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2028 lto_output_uleb128_stream (ob
->main_stream
,
2029 ipa_get_cs_argument_count (args
));
2030 for (j
= 0; j
< ipa_get_cs_argument_count (args
); j
++)
2031 ipa_write_jump_function (ob
, ipa_get_ith_jump_func (args
, j
));
2033 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2034 ipa_write_indirect_edge_info (ob
, e
);
2037 /* Srtream in NODE info from IB. */
2040 ipa_read_node_info (struct lto_input_block
*ib
, struct cgraph_node
*node
,
2041 struct data_in
*data_in
)
2043 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2045 struct cgraph_edge
*e
;
2046 struct bitpack_d
*bp
;
2048 ipa_initialize_node_params (node
);
2050 bp
= lto_input_bitpack (ib
);
2051 info
->called_with_var_arguments
= bp_unpack_value (bp
, 1);
2052 info
->uses_analysis_done
= bp_unpack_value (bp
, 1);
2053 if (ipa_get_param_count (info
) != 0)
2055 info
->modification_analysis_done
= true;
2056 info
->uses_analysis_done
= true;
2058 info
->node_enqueued
= false;
2059 for (k
= 0; k
< ipa_get_param_count (info
); k
++)
2060 info
->params
[k
].modified
= bp_unpack_value (bp
, 1);
2061 bitpack_delete (bp
);
2062 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2064 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2065 int count
= lto_input_uleb128 (ib
);
2067 ipa_set_cs_argument_count (args
, count
);
2071 args
->jump_functions
= GGC_CNEWVEC (struct ipa_jump_func
,
2072 ipa_get_cs_argument_count (args
));
2073 for (k
= 0; k
< ipa_get_cs_argument_count (args
); k
++)
2074 ipa_read_jump_function (ib
, ipa_get_ith_jump_func (args
, k
), data_in
);
2076 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2077 ipa_read_indirect_edge_info (ib
, data_in
, e
);
2080 /* Write jump functions for nodes in SET. */
2083 ipa_prop_write_jump_functions (cgraph_node_set set
)
2085 struct cgraph_node
*node
;
2086 struct output_block
*ob
= create_output_block (LTO_section_jump_functions
);
2087 unsigned int count
= 0;
2088 cgraph_node_set_iterator csi
;
2090 ob
->cgraph_node
= NULL
;
2092 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2094 node
= csi_node (csi
);
2095 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2099 lto_output_uleb128_stream (ob
->main_stream
, count
);
2101 /* Process all of the functions. */
2102 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2104 node
= csi_node (csi
);
2105 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2106 ipa_write_node_info (ob
, node
);
2108 lto_output_1_stream (ob
->main_stream
, 0);
2109 produce_asm (ob
, NULL
);
2110 destroy_output_block (ob
);
2113 /* Read section in file FILE_DATA of length LEN with data DATA. */
2116 ipa_prop_read_section (struct lto_file_decl_data
*file_data
, const char *data
,
2119 const struct lto_function_header
*header
=
2120 (const struct lto_function_header
*) data
;
2121 const int32_t cfg_offset
= sizeof (struct lto_function_header
);
2122 const int32_t main_offset
= cfg_offset
+ header
->cfg_size
;
2123 const int32_t string_offset
= main_offset
+ header
->main_size
;
2124 struct data_in
*data_in
;
2125 struct lto_input_block ib_main
;
2129 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
2133 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
2134 header
->string_size
, NULL
);
2135 count
= lto_input_uleb128 (&ib_main
);
2137 for (i
= 0; i
< count
; i
++)
2140 struct cgraph_node
*node
;
2141 lto_cgraph_encoder_t encoder
;
2143 index
= lto_input_uleb128 (&ib_main
);
2144 encoder
= file_data
->cgraph_node_encoder
;
2145 node
= lto_cgraph_encoder_deref (encoder
, index
);
2146 ipa_read_node_info (&ib_main
, node
, data_in
);
2148 lto_free_section_data (file_data
, LTO_section_jump_functions
, NULL
, data
,
2150 lto_data_in_delete (data_in
);
2153 /* Read ipcp jump functions. */
2156 ipa_prop_read_jump_functions (void)
2158 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
2159 struct lto_file_decl_data
*file_data
;
2162 ipa_check_create_node_params ();
2163 ipa_check_create_edge_args ();
2164 ipa_register_cgraph_hooks ();
2166 while ((file_data
= file_data_vec
[j
++]))
2169 const char *data
= lto_get_section_data (file_data
, LTO_section_jump_functions
, NULL
, &len
);
2172 ipa_prop_read_section (file_data
, data
, len
);
2176 /* After merging units, we can get mismatch in argument counts.
2177 Also decl merging might've rendered parameter lists obsolette.
2178 Also compute called_with_variable_arg info. */
2181 ipa_update_after_lto_read (void)
2183 struct cgraph_node
*node
;
2184 struct cgraph_edge
*cs
;
2186 ipa_check_create_node_params ();
2187 ipa_check_create_edge_args ();
2189 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2191 ipa_initialize_node_params (node
);
2193 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2195 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
2197 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs
))
2198 != ipa_get_param_count (IPA_NODE_REF (cs
->callee
)))
2199 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs
->callee
));