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
;
761 cs
= cgraph_edge (node
, stmt
);
762 cs
->indirect_info
->param_index
= formal_id
;
765 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
766 (described by INFO). Currently it checks whether the call calls a pointer
767 that is a formal parameter and if so, the parameter is marked with the
768 called flag and an indirect call graph edge describing the call is created.
769 This is very simple for ordinary pointers represented in SSA but not-so-nice
770 when it comes to member pointers. The ugly part of this function does
771 nothing more than trying to match the pattern of such a call. An example of
772 such a pattern is the gimple dump below, the call is on the last line:
775 f$__delta_5 = f.__delta;
776 f$__pfn_24 = f.__pfn;
777 D.2496_3 = (int) f$__pfn_24;
778 D.2497_4 = D.2496_3 & 1;
785 D.2500_7 = (unsigned int) f$__delta_5;
786 D.2501_8 = &S + D.2500_7;
787 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
788 D.2503_10 = *D.2502_9;
789 D.2504_12 = f$__pfn_24 + -1;
790 D.2505_13 = (unsigned int) D.2504_12;
791 D.2506_14 = D.2503_10 + D.2505_13;
792 D.2507_15 = *D.2506_14;
793 iftmp.11_16 = (String:: *) D.2507_15;
796 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
797 D.2500_19 = (unsigned int) f$__delta_5;
798 D.2508_20 = &S + D.2500_19;
799 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
801 Such patterns are results of simple calls to a member pointer:
803 int doprinting (int (MyString::* f)(int) const)
805 MyString S ("somestring");
812 ipa_analyze_call_uses (struct cgraph_node
*node
, struct ipa_node_params
*info
,
815 tree target
= gimple_call_fn (call
);
820 tree rec
, rec2
, cond
;
823 basic_block bb
, virt_bb
, join
;
825 if (TREE_CODE (target
) != SSA_NAME
)
828 var
= SSA_NAME_VAR (target
);
829 if (SSA_NAME_IS_DEFAULT_DEF (target
))
831 /* assuming TREE_CODE (var) == PARM_DECL */
832 index
= ipa_get_param_decl_index (info
, var
);
834 ipa_note_param_call (node
, index
, call
);
838 /* Now we need to try to match the complex pattern of calling a member
841 if (!POINTER_TYPE_P (TREE_TYPE (target
))
842 || TREE_CODE (TREE_TYPE (TREE_TYPE (target
))) != METHOD_TYPE
)
845 def
= SSA_NAME_DEF_STMT (target
);
846 if (gimple_code (def
) != GIMPLE_PHI
)
849 if (gimple_phi_num_args (def
) != 2)
852 /* First, we need to check whether one of these is a load from a member
853 pointer that is a parameter to this function. */
854 n1
= PHI_ARG_DEF (def
, 0);
855 n2
= PHI_ARG_DEF (def
, 1);
856 if (!ipa_is_ssa_with_stmt_def (n1
) || !ipa_is_ssa_with_stmt_def (n2
))
858 d1
= SSA_NAME_DEF_STMT (n1
);
859 d2
= SSA_NAME_DEF_STMT (n2
);
861 if ((rec
= ipa_get_stmt_member_ptr_load_param (d1
, false)))
863 if (ipa_get_stmt_member_ptr_load_param (d2
, false))
867 virt_bb
= gimple_bb (d2
);
869 else if ((rec
= ipa_get_stmt_member_ptr_load_param (d2
, false)))
872 virt_bb
= gimple_bb (d1
);
877 /* Second, we need to check that the basic blocks are laid out in the way
878 corresponding to the pattern. */
880 join
= gimple_bb (def
);
881 if (!single_pred_p (virt_bb
) || !single_succ_p (virt_bb
)
882 || single_pred (virt_bb
) != bb
883 || single_succ (virt_bb
) != join
)
886 /* Third, let's see that the branching is done depending on the least
887 significant bit of the pfn. */
889 branch
= last_stmt (bb
);
890 if (gimple_code (branch
) != GIMPLE_COND
)
893 if (gimple_cond_code (branch
) != NE_EXPR
894 || !integer_zerop (gimple_cond_rhs (branch
)))
897 cond
= gimple_cond_lhs (branch
);
898 if (!ipa_is_ssa_with_stmt_def (cond
))
901 def
= SSA_NAME_DEF_STMT (cond
);
902 if (!is_gimple_assign (def
)
903 || gimple_assign_rhs_code (def
) != BIT_AND_EXPR
904 || !integer_onep (gimple_assign_rhs2 (def
)))
907 cond
= gimple_assign_rhs1 (def
);
908 if (!ipa_is_ssa_with_stmt_def (cond
))
911 def
= SSA_NAME_DEF_STMT (cond
);
913 if (is_gimple_assign (def
)
914 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
916 cond
= gimple_assign_rhs1 (def
);
917 if (!ipa_is_ssa_with_stmt_def (cond
))
919 def
= SSA_NAME_DEF_STMT (cond
);
922 rec2
= ipa_get_stmt_member_ptr_load_param (def
,
923 (TARGET_PTRMEMFUNC_VBIT_LOCATION
924 == ptrmemfunc_vbit_in_delta
));
929 index
= ipa_get_param_decl_index (info
, rec
);
930 if (index
>= 0 && !ipa_is_param_modified (info
, index
))
931 ipa_note_param_call (node
, index
, call
);
936 /* Analyze the call statement STMT with respect to formal parameters (described
937 in INFO) of caller given by NODE. Currently it only checks whether formal
938 parameters are called. */
941 ipa_analyze_stmt_uses (struct cgraph_node
*node
, struct ipa_node_params
*info
,
944 if (is_gimple_call (stmt
))
945 ipa_analyze_call_uses (node
, info
, stmt
);
948 /* Scan the function body of NODE and inspect the uses of formal parameters.
949 Store the findings in various structures of the associated ipa_node_params
950 structure, such as parameter flags, notes etc. */
953 ipa_analyze_params_uses (struct cgraph_node
*node
)
955 tree decl
= node
->decl
;
957 struct function
*func
;
958 gimple_stmt_iterator gsi
;
959 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
961 if (ipa_get_param_count (info
) == 0 || info
->uses_analysis_done
)
964 func
= DECL_STRUCT_FUNCTION (decl
);
965 FOR_EACH_BB_FN (bb
, func
)
967 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
969 gimple stmt
= gsi_stmt (gsi
);
970 ipa_analyze_stmt_uses (node
, info
, stmt
);
974 info
->uses_analysis_done
= 1;
977 /* Update the jump functions associated with call graph edge E when the call
978 graph edge CS is being inlined, assuming that E->caller is already (possibly
979 indirectly) inlined into CS->callee and that E has not been inlined.
981 We keep pass through functions only if they do not contain any operation.
982 This is sufficient for inlining and greately simplifies things. */
985 update_jump_functions_after_inlining (struct cgraph_edge
*cs
,
986 struct cgraph_edge
*e
)
988 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
989 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
990 int count
= ipa_get_cs_argument_count (args
);
993 for (i
= 0; i
< count
; i
++)
995 struct ipa_jump_func
*src
, *dst
= ipa_get_ith_jump_func (args
, i
);
997 if (dst
->type
== IPA_JF_ANCESTOR
)
999 dst
->type
= IPA_JF_UNKNOWN
;
1003 if (dst
->type
!= IPA_JF_PASS_THROUGH
)
1006 /* We must check range due to calls with variable number of arguments and
1007 we cannot combine jump functions with operations. */
1008 if (dst
->value
.pass_through
.operation
!= NOP_EXPR
1009 || (dst
->value
.pass_through
.formal_id
1010 >= ipa_get_cs_argument_count (top
)))
1012 dst
->type
= IPA_JF_UNKNOWN
;
1016 src
= ipa_get_ith_jump_func (top
, dst
->value
.pass_through
.formal_id
);
1021 /* Print out a debug message to file F that we have discovered that an indirect
1022 call described by NT is in fact a call of a known constant function described
1023 by JFUNC. NODE is the node where the call is. */
1026 print_edge_addition_message (FILE *f
, struct cgraph_edge
*e
,
1027 struct ipa_jump_func
*jfunc
)
1029 fprintf (f
, "ipa-prop: Discovered an indirect call to a known target (");
1030 if (jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1032 print_node_brief (f
, "", jfunc
->value
.member_cst
.pfn
, 0);
1033 print_node_brief (f
, ", ", jfunc
->value
.member_cst
.delta
, 0);
1036 print_node_brief(f
, "", jfunc
->value
.constant
, 0);
1038 fprintf (f
, ") in %s: ", cgraph_node_name (e
->caller
));
1039 print_gimple_stmt (f
, e
->call_stmt
, 2, TDF_SLIM
);
1042 /* Update the param called notes associated with NODE when CS is being inlined,
1043 assuming NODE is (potentially indirectly) inlined into CS->callee.
1044 Moreover, if the callee is discovered to be constant, create a new cgraph
1045 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1046 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1049 update_indirect_edges_after_inlining (struct cgraph_edge
*cs
,
1050 struct cgraph_node
*node
,
1051 VEC (cgraph_edge_p
, heap
) **new_edges
)
1053 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
1054 struct cgraph_edge
*ie
, *next_ie
;
1057 ipa_check_create_edge_args ();
1059 for (ie
= node
->indirect_calls
; ie
; ie
= next_ie
)
1061 struct cgraph_indirect_call_info
*ici
= ie
->indirect_info
;
1062 struct ipa_jump_func
*jfunc
;
1064 next_ie
= ie
->next_callee
;
1065 if (bitmap_bit_p (iinlining_processed_edges
, ie
->uid
))
1068 /* If we ever use indirect edges for anything other than indirect
1069 inlining, we will need to skip those with negative param_indices. */
1070 if (ici
->param_index
== -1)
1073 /* We must check range due to calls with variable number of arguments: */
1074 if (ici
->param_index
>= ipa_get_cs_argument_count (top
))
1076 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
1080 jfunc
= ipa_get_ith_jump_func (top
, ici
->param_index
);
1081 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1082 && jfunc
->value
.pass_through
.operation
== NOP_EXPR
)
1083 ici
->param_index
= jfunc
->value
.pass_through
.formal_id
;
1084 else if (jfunc
->type
== IPA_JF_CONST
1085 || jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1087 struct cgraph_node
*callee
;
1090 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
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
, ie
, jfunc
);
1110 cgraph_make_edge_direct (ie
, callee
);
1111 ie
->indirect_inlining_edge
= 1;
1113 VEC_safe_push (cgraph_edge_p
, heap
, *new_edges
, ie
);
1114 top
= IPA_EDGE_REF (cs
);
1118 /* Ancestor jump functions and pass theoughs with operations should
1119 not be used on parameters that then get called. */
1120 gcc_assert (jfunc
->type
== IPA_JF_UNKNOWN
);
1121 bitmap_set_bit (iinlining_processed_edges
, ie
->uid
);
1128 /* Recursively traverse subtree of NODE (including node) made of inlined
1129 cgraph_edges when CS has been inlined and invoke
1130 update_indirect_edges_after_inlining on all nodes and
1131 update_jump_functions_after_inlining on all non-inlined edges that lead out
1132 of this subtree. Newly discovered indirect edges will be added to
1133 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1137 propagate_info_to_inlined_callees (struct cgraph_edge
*cs
,
1138 struct cgraph_node
*node
,
1139 VEC (cgraph_edge_p
, heap
) **new_edges
)
1141 struct cgraph_edge
*e
;
1144 res
= update_indirect_edges_after_inlining (cs
, node
, new_edges
);
1146 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1147 if (!e
->inline_failed
)
1148 res
|= propagate_info_to_inlined_callees (cs
, e
->callee
, new_edges
);
1150 update_jump_functions_after_inlining (cs
, e
);
1155 /* Update jump functions and call note functions on inlining the call site CS.
1156 CS is expected to lead to a node already cloned by
1157 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1158 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1162 ipa_propagate_indirect_call_infos (struct cgraph_edge
*cs
,
1163 VEC (cgraph_edge_p
, heap
) **new_edges
)
1165 /* FIXME lto: We do not stream out indirect call information. */
1169 /* Do nothing if the preparation phase has not been carried out yet
1170 (i.e. during early inlining). */
1171 if (!ipa_node_params_vector
)
1173 gcc_assert (ipa_edge_args_vector
);
1175 return propagate_info_to_inlined_callees (cs
, cs
->callee
, new_edges
);
1178 /* Frees all dynamically allocated structures that the argument info points
1182 ipa_free_edge_args_substructures (struct ipa_edge_args
*args
)
1184 if (args
->jump_functions
)
1185 ggc_free (args
->jump_functions
);
1187 memset (args
, 0, sizeof (*args
));
1190 /* Free all ipa_edge structures. */
1193 ipa_free_all_edge_args (void)
1196 struct ipa_edge_args
*args
;
1199 VEC_iterate (ipa_edge_args_t
, ipa_edge_args_vector
, i
, args
);
1201 ipa_free_edge_args_substructures (args
);
1203 VEC_free (ipa_edge_args_t
, gc
, ipa_edge_args_vector
);
1204 ipa_edge_args_vector
= NULL
;
1207 /* Frees all dynamically allocated structures that the param info points
1211 ipa_free_node_params_substructures (struct ipa_node_params
*info
)
1214 free (info
->params
);
1216 memset (info
, 0, sizeof (*info
));
1219 /* Free all ipa_node_params structures. */
1222 ipa_free_all_node_params (void)
1225 struct ipa_node_params
*info
;
1228 VEC_iterate (ipa_node_params_t
, ipa_node_params_vector
, i
, info
);
1230 ipa_free_node_params_substructures (info
);
1232 VEC_free (ipa_node_params_t
, heap
, ipa_node_params_vector
);
1233 ipa_node_params_vector
= NULL
;
1236 /* Hook that is called by cgraph.c when an edge is removed. */
1239 ipa_edge_removal_hook (struct cgraph_edge
*cs
, void *data ATTRIBUTE_UNUSED
)
1241 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1242 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
1243 <= (unsigned)cs
->uid
)
1245 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs
));
1248 /* Hook that is called by cgraph.c when a node is removed. */
1251 ipa_node_removal_hook (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
1253 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1254 if (VEC_length (ipa_node_params_t
, ipa_node_params_vector
)
1255 <= (unsigned)node
->uid
)
1257 ipa_free_node_params_substructures (IPA_NODE_REF (node
));
1260 /* Helper function to duplicate an array of size N that is at SRC and store a
1261 pointer to it to DST. Nothing is done if SRC is NULL. */
1264 duplicate_array (void *src
, size_t n
)
1276 /* Like duplicate_array byt in GGC memory. */
1279 duplicate_ggc_array (void *src
, size_t n
)
1291 /* Hook that is called by cgraph.c when a node is duplicated. */
1294 ipa_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
1295 __attribute__((unused
)) void *data
)
1297 struct ipa_edge_args
*old_args
, *new_args
;
1300 ipa_check_create_edge_args ();
1302 old_args
= IPA_EDGE_REF (src
);
1303 new_args
= IPA_EDGE_REF (dst
);
1305 arg_count
= ipa_get_cs_argument_count (old_args
);
1306 ipa_set_cs_argument_count (new_args
, arg_count
);
1307 new_args
->jump_functions
= (struct ipa_jump_func
*)
1308 duplicate_ggc_array (old_args
->jump_functions
,
1309 sizeof (struct ipa_jump_func
) * arg_count
);
1311 if (iinlining_processed_edges
1312 && bitmap_bit_p (iinlining_processed_edges
, src
->uid
))
1313 bitmap_set_bit (iinlining_processed_edges
, dst
->uid
);
1316 /* Hook that is called by cgraph.c when a node is duplicated. */
1319 ipa_node_duplication_hook (struct cgraph_node
*src
, struct cgraph_node
*dst
,
1320 __attribute__((unused
)) void *data
)
1322 struct ipa_node_params
*old_info
, *new_info
;
1325 ipa_check_create_node_params ();
1326 old_info
= IPA_NODE_REF (src
);
1327 new_info
= IPA_NODE_REF (dst
);
1328 param_count
= ipa_get_param_count (old_info
);
1330 ipa_set_param_count (new_info
, param_count
);
1331 new_info
->params
= (struct ipa_param_descriptor
*)
1332 duplicate_array (old_info
->params
,
1333 sizeof (struct ipa_param_descriptor
) * param_count
);
1334 new_info
->ipcp_orig_node
= old_info
->ipcp_orig_node
;
1335 new_info
->count_scale
= old_info
->count_scale
;
1338 /* Register our cgraph hooks if they are not already there. */
1341 ipa_register_cgraph_hooks (void)
1343 if (!edge_removal_hook_holder
)
1344 edge_removal_hook_holder
=
1345 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook
, NULL
);
1346 if (!node_removal_hook_holder
)
1347 node_removal_hook_holder
=
1348 cgraph_add_node_removal_hook (&ipa_node_removal_hook
, NULL
);
1349 if (!edge_duplication_hook_holder
)
1350 edge_duplication_hook_holder
=
1351 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook
, NULL
);
1352 if (!node_duplication_hook_holder
)
1353 node_duplication_hook_holder
=
1354 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook
, NULL
);
1357 /* Unregister our cgraph hooks if they are not already there. */
1360 ipa_unregister_cgraph_hooks (void)
1362 cgraph_remove_edge_removal_hook (edge_removal_hook_holder
);
1363 edge_removal_hook_holder
= NULL
;
1364 cgraph_remove_node_removal_hook (node_removal_hook_holder
);
1365 node_removal_hook_holder
= NULL
;
1366 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder
);
1367 edge_duplication_hook_holder
= NULL
;
1368 cgraph_remove_node_duplication_hook (node_duplication_hook_holder
);
1369 node_duplication_hook_holder
= NULL
;
1372 /* Allocate all necessary data strucutures necessary for indirect inlining. */
1375 ipa_create_all_structures_for_iinln (void)
1377 iinlining_processed_edges
= BITMAP_ALLOC (NULL
);
1380 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1381 longer needed after ipa-cp. */
1384 ipa_free_all_structures_after_ipa_cp (void)
1386 if (!flag_indirect_inlining
)
1388 ipa_free_all_edge_args ();
1389 ipa_free_all_node_params ();
1390 ipa_unregister_cgraph_hooks ();
1394 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1395 longer needed after indirect inlining. */
1398 ipa_free_all_structures_after_iinln (void)
1400 BITMAP_FREE (iinlining_processed_edges
);
1402 ipa_free_all_edge_args ();
1403 ipa_free_all_node_params ();
1404 ipa_unregister_cgraph_hooks ();
1407 /* Print ipa_tree_map data structures of all functions in the
1411 ipa_print_node_params (FILE * f
, struct cgraph_node
*node
)
1415 struct ipa_node_params
*info
;
1417 if (!node
->analyzed
)
1419 info
= IPA_NODE_REF (node
);
1420 fprintf (f
, " function %s Trees :: \n", cgraph_node_name (node
));
1421 count
= ipa_get_param_count (info
);
1422 for (i
= 0; i
< count
; i
++)
1424 temp
= ipa_get_param (info
, i
);
1425 if (TREE_CODE (temp
) == PARM_DECL
)
1426 fprintf (f
, " param %d : %s", i
,
1428 ? (*lang_hooks
.decl_printable_name
) (temp
, 2)
1430 if (ipa_is_param_modified (info
, i
))
1431 fprintf (f
, " modified");
1436 /* Print ipa_tree_map data structures of all functions in the
1440 ipa_print_all_params (FILE * f
)
1442 struct cgraph_node
*node
;
1444 fprintf (f
, "\nFunction parameters:\n");
1445 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1446 ipa_print_node_params (f
, node
);
1449 /* Return a heap allocated vector containing formal parameters of FNDECL. */
1452 ipa_get_vector_of_formal_parms (tree fndecl
)
1454 VEC(tree
, heap
) *args
;
1458 count
= count_formal_params_1 (fndecl
);
1459 args
= VEC_alloc (tree
, heap
, count
);
1460 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
1461 VEC_quick_push (tree
, args
, parm
);
1466 /* Return a heap allocated vector containing types of formal parameters of
1467 function type FNTYPE. */
1469 static inline VEC(tree
, heap
) *
1470 get_vector_of_formal_parm_types (tree fntype
)
1472 VEC(tree
, heap
) *types
;
1476 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1479 types
= VEC_alloc (tree
, heap
, count
);
1480 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1481 VEC_quick_push (tree
, types
, TREE_VALUE (t
));
1486 /* Modify the function declaration FNDECL and its type according to the plan in
1487 ADJUSTMENTS. It also sets base fields of individual adjustments structures
1488 to reflect the actual parameters being modified which are determined by the
1489 base_index field. */
1492 ipa_modify_formal_parameters (tree fndecl
, ipa_parm_adjustment_vec adjustments
,
1493 const char *synth_parm_prefix
)
1495 VEC(tree
, heap
) *oparms
, *otypes
;
1496 tree orig_type
, new_type
= NULL
;
1497 tree old_arg_types
, t
, new_arg_types
= NULL
;
1498 tree parm
, *link
= &DECL_ARGUMENTS (fndecl
);
1499 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1500 tree new_reversed
= NULL
;
1501 bool care_for_types
, last_parm_void
;
1503 if (!synth_parm_prefix
)
1504 synth_parm_prefix
= "SYNTH";
1506 oparms
= ipa_get_vector_of_formal_parms (fndecl
);
1507 orig_type
= TREE_TYPE (fndecl
);
1508 old_arg_types
= TYPE_ARG_TYPES (orig_type
);
1510 /* The following test is an ugly hack, some functions simply don't have any
1511 arguments in their type. This is probably a bug but well... */
1512 care_for_types
= (old_arg_types
!= NULL_TREE
);
1515 last_parm_void
= (TREE_VALUE (tree_last (old_arg_types
))
1517 otypes
= get_vector_of_formal_parm_types (orig_type
);
1519 gcc_assert (VEC_length (tree
, oparms
) + 1 == VEC_length (tree
, otypes
));
1521 gcc_assert (VEC_length (tree
, oparms
) == VEC_length (tree
, otypes
));
1525 last_parm_void
= false;
1529 for (i
= 0; i
< len
; i
++)
1531 struct ipa_parm_adjustment
*adj
;
1534 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1535 parm
= VEC_index (tree
, oparms
, adj
->base_index
);
1538 if (adj
->copy_param
)
1541 new_arg_types
= tree_cons (NULL_TREE
, VEC_index (tree
, otypes
,
1545 link
= &TREE_CHAIN (parm
);
1547 else if (!adj
->remove_param
)
1553 ptype
= build_pointer_type (adj
->type
);
1558 new_arg_types
= tree_cons (NULL_TREE
, ptype
, new_arg_types
);
1560 new_parm
= build_decl (UNKNOWN_LOCATION
, PARM_DECL
, NULL_TREE
,
1562 DECL_NAME (new_parm
) = create_tmp_var_name (synth_parm_prefix
);
1564 DECL_ARTIFICIAL (new_parm
) = 1;
1565 DECL_ARG_TYPE (new_parm
) = ptype
;
1566 DECL_CONTEXT (new_parm
) = fndecl
;
1567 TREE_USED (new_parm
) = 1;
1568 DECL_IGNORED_P (new_parm
) = 1;
1569 layout_decl (new_parm
, 0);
1571 add_referenced_var (new_parm
);
1572 mark_sym_for_renaming (new_parm
);
1574 adj
->reduction
= new_parm
;
1578 link
= &TREE_CHAIN (new_parm
);
1586 new_reversed
= nreverse (new_arg_types
);
1590 TREE_CHAIN (new_arg_types
) = void_list_node
;
1592 new_reversed
= void_list_node
;
1596 /* Use copy_node to preserve as much as possible from original type
1597 (debug info, attribute lists etc.)
1598 Exception is METHOD_TYPEs must have THIS argument.
1599 When we are asked to remove it, we need to build new FUNCTION_TYPE
1601 if (TREE_CODE (orig_type
) != METHOD_TYPE
1602 || (VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->copy_param
1603 && VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->base_index
== 0))
1605 new_type
= copy_node (orig_type
);
1606 TYPE_ARG_TYPES (new_type
) = new_reversed
;
1611 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type
),
1613 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
1614 DECL_VINDEX (fndecl
) = NULL_TREE
;
1617 /* This is a new type, not a copy of an old type. Need to reassociate
1618 variants. We can handle everything except the main variant lazily. */
1619 t
= TYPE_MAIN_VARIANT (orig_type
);
1622 TYPE_MAIN_VARIANT (new_type
) = t
;
1623 TYPE_NEXT_VARIANT (new_type
) = TYPE_NEXT_VARIANT (t
);
1624 TYPE_NEXT_VARIANT (t
) = new_type
;
1628 TYPE_MAIN_VARIANT (new_type
) = new_type
;
1629 TYPE_NEXT_VARIANT (new_type
) = NULL
;
1632 TREE_TYPE (fndecl
) = new_type
;
1634 VEC_free (tree
, heap
, otypes
);
1635 VEC_free (tree
, heap
, oparms
);
1638 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1639 If this is a directly recursive call, CS must be NULL. Otherwise it must
1640 contain the corresponding call graph edge. */
1643 ipa_modify_call_arguments (struct cgraph_edge
*cs
, gimple stmt
,
1644 ipa_parm_adjustment_vec adjustments
)
1646 VEC(tree
, heap
) *vargs
;
1648 gimple_stmt_iterator gsi
;
1652 len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1653 vargs
= VEC_alloc (tree
, heap
, len
);
1655 gsi
= gsi_for_stmt (stmt
);
1656 for (i
= 0; i
< len
; i
++)
1658 struct ipa_parm_adjustment
*adj
;
1660 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1662 if (adj
->copy_param
)
1664 tree arg
= gimple_call_arg (stmt
, adj
->base_index
);
1666 VEC_quick_push (tree
, vargs
, arg
);
1668 else if (!adj
->remove_param
)
1670 tree expr
, orig_expr
;
1671 bool allow_ptr
, repl_found
;
1673 orig_expr
= expr
= gimple_call_arg (stmt
, adj
->base_index
);
1674 if (TREE_CODE (expr
) == ADDR_EXPR
)
1677 expr
= TREE_OPERAND (expr
, 0);
1682 repl_found
= build_ref_for_offset (&expr
, TREE_TYPE (expr
),
1683 adj
->offset
, adj
->type
,
1688 expr
= build_fold_addr_expr (expr
);
1692 tree ptrtype
= build_pointer_type (adj
->type
);
1694 if (!POINTER_TYPE_P (TREE_TYPE (expr
)))
1695 expr
= build_fold_addr_expr (expr
);
1696 if (!useless_type_conversion_p (ptrtype
, TREE_TYPE (expr
)))
1697 expr
= fold_convert (ptrtype
, expr
);
1698 expr
= fold_build2 (POINTER_PLUS_EXPR
, ptrtype
, expr
,
1699 build_int_cst (sizetype
,
1700 adj
->offset
/ BITS_PER_UNIT
));
1702 expr
= fold_build1 (INDIRECT_REF
, adj
->type
, expr
);
1704 expr
= force_gimple_operand_gsi (&gsi
, expr
,
1706 || is_gimple_reg_type (adj
->type
),
1707 NULL
, true, GSI_SAME_STMT
);
1708 VEC_quick_push (tree
, vargs
, expr
);
1712 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1714 fprintf (dump_file
, "replacing stmt:");
1715 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0, 0);
1718 callee_decl
= !cs
? gimple_call_fndecl (stmt
) : cs
->callee
->decl
;
1719 new_stmt
= gimple_build_call_vec (callee_decl
, vargs
);
1720 VEC_free (tree
, heap
, vargs
);
1721 if (gimple_call_lhs (stmt
))
1722 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
1724 gimple_set_block (new_stmt
, gimple_block (stmt
));
1725 if (gimple_has_location (stmt
))
1726 gimple_set_location (new_stmt
, gimple_location (stmt
));
1727 gimple_call_copy_flags (new_stmt
, stmt
);
1728 gimple_call_set_chain (new_stmt
, gimple_call_chain (stmt
));
1730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1732 fprintf (dump_file
, "with stmt:");
1733 print_gimple_stmt (dump_file
, new_stmt
, 0, 0);
1734 fprintf (dump_file
, "\n");
1736 gsi_replace (&gsi
, new_stmt
, true);
1738 cgraph_set_call_stmt (cs
, new_stmt
);
1739 update_ssa (TODO_update_ssa
);
1740 free_dominance_info (CDI_DOMINATORS
);
1743 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
1746 index_in_adjustments_multiple_times_p (int base_index
,
1747 ipa_parm_adjustment_vec adjustments
)
1749 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1752 for (i
= 0; i
< len
; i
++)
1754 struct ipa_parm_adjustment
*adj
;
1755 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1757 if (adj
->base_index
== base_index
)
1769 /* Return adjustments that should have the same effect on function parameters
1770 and call arguments as if they were first changed according to adjustments in
1771 INNER and then by adjustments in OUTER. */
1773 ipa_parm_adjustment_vec
1774 ipa_combine_adjustments (ipa_parm_adjustment_vec inner
,
1775 ipa_parm_adjustment_vec outer
)
1777 int i
, outlen
= VEC_length (ipa_parm_adjustment_t
, outer
);
1778 int inlen
= VEC_length (ipa_parm_adjustment_t
, inner
);
1780 ipa_parm_adjustment_vec adjustments
, tmp
;
1782 tmp
= VEC_alloc (ipa_parm_adjustment_t
, heap
, inlen
);
1783 for (i
= 0; i
< inlen
; i
++)
1785 struct ipa_parm_adjustment
*n
;
1786 n
= VEC_index (ipa_parm_adjustment_t
, inner
, i
);
1788 if (n
->remove_param
)
1791 VEC_quick_push (ipa_parm_adjustment_t
, tmp
, n
);
1794 adjustments
= VEC_alloc (ipa_parm_adjustment_t
, heap
, outlen
+ removals
);
1795 for (i
= 0; i
< outlen
; i
++)
1797 struct ipa_parm_adjustment
*r
;
1798 struct ipa_parm_adjustment
*out
= VEC_index (ipa_parm_adjustment_t
,
1800 struct ipa_parm_adjustment
*in
= VEC_index (ipa_parm_adjustment_t
, tmp
,
1803 gcc_assert (!in
->remove_param
);
1804 if (out
->remove_param
)
1806 if (!index_in_adjustments_multiple_times_p (in
->base_index
, tmp
))
1808 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1809 memset (r
, 0, sizeof (*r
));
1810 r
->remove_param
= true;
1815 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1816 memset (r
, 0, sizeof (*r
));
1817 r
->base_index
= in
->base_index
;
1818 r
->type
= out
->type
;
1820 /* FIXME: Create nonlocal value too. */
1822 if (in
->copy_param
&& out
->copy_param
)
1823 r
->copy_param
= true;
1824 else if (in
->copy_param
)
1825 r
->offset
= out
->offset
;
1826 else if (out
->copy_param
)
1827 r
->offset
= in
->offset
;
1829 r
->offset
= in
->offset
+ out
->offset
;
1832 for (i
= 0; i
< inlen
; i
++)
1834 struct ipa_parm_adjustment
*n
= VEC_index (ipa_parm_adjustment_t
,
1837 if (n
->remove_param
)
1838 VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, n
);
1841 VEC_free (ipa_parm_adjustment_t
, heap
, tmp
);
1845 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1846 friendly way, assuming they are meant to be applied to FNDECL. */
1849 ipa_dump_param_adjustments (FILE *file
, ipa_parm_adjustment_vec adjustments
,
1852 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1854 VEC(tree
, heap
) *parms
= ipa_get_vector_of_formal_parms (fndecl
);
1856 fprintf (file
, "IPA param adjustments: ");
1857 for (i
= 0; i
< len
; i
++)
1859 struct ipa_parm_adjustment
*adj
;
1860 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1863 fprintf (file
, " ");
1867 fprintf (file
, "%i. base_index: %i - ", i
, adj
->base_index
);
1868 print_generic_expr (file
, VEC_index (tree
, parms
, adj
->base_index
), 0);
1871 fprintf (file
, ", base: ");
1872 print_generic_expr (file
, adj
->base
, 0);
1876 fprintf (file
, ", reduction: ");
1877 print_generic_expr (file
, adj
->reduction
, 0);
1879 if (adj
->new_ssa_base
)
1881 fprintf (file
, ", new_ssa_base: ");
1882 print_generic_expr (file
, adj
->new_ssa_base
, 0);
1885 if (adj
->copy_param
)
1886 fprintf (file
, ", copy_param");
1887 else if (adj
->remove_param
)
1888 fprintf (file
, ", remove_param");
1890 fprintf (file
, ", offset %li", (long) adj
->offset
);
1892 fprintf (file
, ", by_ref");
1893 print_node_brief (file
, ", type: ", adj
->type
, 0);
1894 fprintf (file
, "\n");
1896 VEC_free (tree
, heap
, parms
);
1899 /* Stream out jump function JUMP_FUNC to OB. */
1902 ipa_write_jump_function (struct output_block
*ob
,
1903 struct ipa_jump_func
*jump_func
)
1905 lto_output_uleb128_stream (ob
->main_stream
,
1908 switch (jump_func
->type
)
1910 case IPA_JF_UNKNOWN
:
1913 lto_output_tree (ob
, jump_func
->value
.constant
, true);
1915 case IPA_JF_PASS_THROUGH
:
1916 lto_output_tree (ob
, jump_func
->value
.pass_through
.operand
, true);
1917 lto_output_uleb128_stream (ob
->main_stream
,
1918 jump_func
->value
.pass_through
.formal_id
);
1919 lto_output_uleb128_stream (ob
->main_stream
,
1920 jump_func
->value
.pass_through
.operation
);
1922 case IPA_JF_ANCESTOR
:
1923 lto_output_uleb128_stream (ob
->main_stream
,
1924 jump_func
->value
.ancestor
.offset
);
1925 lto_output_tree (ob
, jump_func
->value
.ancestor
.type
, true);
1926 lto_output_uleb128_stream (ob
->main_stream
,
1927 jump_func
->value
.ancestor
.formal_id
);
1929 case IPA_JF_CONST_MEMBER_PTR
:
1930 lto_output_tree (ob
, jump_func
->value
.member_cst
.pfn
, true);
1931 lto_output_tree (ob
, jump_func
->value
.member_cst
.delta
, false);
1936 /* Read in jump function JUMP_FUNC from IB. */
1939 ipa_read_jump_function (struct lto_input_block
*ib
,
1940 struct ipa_jump_func
*jump_func
,
1941 struct data_in
*data_in
)
1943 jump_func
->type
= (enum jump_func_type
) lto_input_uleb128 (ib
);
1945 switch (jump_func
->type
)
1947 case IPA_JF_UNKNOWN
:
1950 jump_func
->value
.constant
= lto_input_tree (ib
, data_in
);
1952 case IPA_JF_PASS_THROUGH
:
1953 jump_func
->value
.pass_through
.operand
= lto_input_tree (ib
, data_in
);
1954 jump_func
->value
.pass_through
.formal_id
= lto_input_uleb128 (ib
);
1955 jump_func
->value
.pass_through
.operation
= (enum tree_code
) lto_input_uleb128 (ib
);
1957 case IPA_JF_ANCESTOR
:
1958 jump_func
->value
.ancestor
.offset
= lto_input_uleb128 (ib
);
1959 jump_func
->value
.ancestor
.type
= lto_input_tree (ib
, data_in
);
1960 jump_func
->value
.ancestor
.formal_id
= lto_input_uleb128 (ib
);
1962 case IPA_JF_CONST_MEMBER_PTR
:
1963 jump_func
->value
.member_cst
.pfn
= lto_input_tree (ib
, data_in
);
1964 jump_func
->value
.member_cst
.delta
= lto_input_tree (ib
, data_in
);
1969 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
1970 relevant to indirect inlining to OB. */
1973 ipa_write_indirect_edge_info (struct output_block
*ob
,
1974 struct cgraph_edge
*cs
)
1976 struct cgraph_indirect_call_info
*ii
= cs
->indirect_info
;
1978 lto_output_sleb128_stream (ob
->main_stream
, ii
->param_index
);
1981 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
1982 relevant to indirect inlining from IB. */
1985 ipa_read_indirect_edge_info (struct lto_input_block
*ib
,
1986 struct data_in
*data_in ATTRIBUTE_UNUSED
,
1987 struct cgraph_edge
*cs
)
1989 struct cgraph_indirect_call_info
*ii
= cs
->indirect_info
;
1991 ii
->param_index
= (int) lto_input_sleb128 (ib
);
1994 /* Stream out NODE info to OB. */
1997 ipa_write_node_info (struct output_block
*ob
, struct cgraph_node
*node
)
2000 lto_cgraph_encoder_t encoder
;
2001 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2003 struct cgraph_edge
*e
;
2004 struct bitpack_d
*bp
;
2006 encoder
= ob
->decl_state
->cgraph_node_encoder
;
2007 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
2008 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
2010 bp
= bitpack_create ();
2011 bp_pack_value (bp
, info
->called_with_var_arguments
, 1);
2012 bp_pack_value (bp
, info
->uses_analysis_done
, 1);
2013 gcc_assert (info
->modification_analysis_done
2014 || ipa_get_param_count (info
) == 0);
2015 gcc_assert (!info
->node_enqueued
);
2016 gcc_assert (!info
->ipcp_orig_node
);
2017 for (j
= 0; j
< ipa_get_param_count (info
); j
++)
2018 bp_pack_value (bp
, info
->params
[j
].modified
, 1);
2019 lto_output_bitpack (ob
->main_stream
, bp
);
2020 bitpack_delete (bp
);
2021 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2023 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2025 lto_output_uleb128_stream (ob
->main_stream
,
2026 ipa_get_cs_argument_count (args
));
2027 for (j
= 0; j
< ipa_get_cs_argument_count (args
); j
++)
2028 ipa_write_jump_function (ob
, ipa_get_ith_jump_func (args
, j
));
2030 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2031 ipa_write_indirect_edge_info (ob
, e
);
2034 /* Srtream in NODE info from IB. */
2037 ipa_read_node_info (struct lto_input_block
*ib
, struct cgraph_node
*node
,
2038 struct data_in
*data_in
)
2040 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2042 struct cgraph_edge
*e
;
2043 struct bitpack_d
*bp
;
2045 ipa_initialize_node_params (node
);
2047 bp
= lto_input_bitpack (ib
);
2048 info
->called_with_var_arguments
= bp_unpack_value (bp
, 1);
2049 info
->uses_analysis_done
= bp_unpack_value (bp
, 1);
2050 if (ipa_get_param_count (info
) != 0)
2052 info
->modification_analysis_done
= true;
2053 info
->uses_analysis_done
= true;
2055 info
->node_enqueued
= false;
2056 for (k
= 0; k
< ipa_get_param_count (info
); k
++)
2057 info
->params
[k
].modified
= bp_unpack_value (bp
, 1);
2058 bitpack_delete (bp
);
2059 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2061 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2062 int count
= lto_input_uleb128 (ib
);
2064 ipa_set_cs_argument_count (args
, count
);
2068 args
->jump_functions
= GGC_CNEWVEC (struct ipa_jump_func
,
2069 ipa_get_cs_argument_count (args
));
2070 for (k
= 0; k
< ipa_get_cs_argument_count (args
); k
++)
2071 ipa_read_jump_function (ib
, ipa_get_ith_jump_func (args
, k
), data_in
);
2073 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
2074 ipa_read_indirect_edge_info (ib
, data_in
, e
);
2077 /* Write jump functions for nodes in SET. */
2080 ipa_prop_write_jump_functions (cgraph_node_set set
)
2082 struct cgraph_node
*node
;
2083 struct output_block
*ob
= create_output_block (LTO_section_jump_functions
);
2084 unsigned int count
= 0;
2085 cgraph_node_set_iterator csi
;
2087 ob
->cgraph_node
= NULL
;
2089 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2091 node
= csi_node (csi
);
2092 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2096 lto_output_uleb128_stream (ob
->main_stream
, count
);
2098 /* Process all of the functions. */
2099 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2101 node
= csi_node (csi
);
2102 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2103 ipa_write_node_info (ob
, node
);
2105 lto_output_1_stream (ob
->main_stream
, 0);
2106 produce_asm (ob
, NULL
);
2107 destroy_output_block (ob
);
2110 /* Read section in file FILE_DATA of length LEN with data DATA. */
2113 ipa_prop_read_section (struct lto_file_decl_data
*file_data
, const char *data
,
2116 const struct lto_function_header
*header
=
2117 (const struct lto_function_header
*) data
;
2118 const int32_t cfg_offset
= sizeof (struct lto_function_header
);
2119 const int32_t main_offset
= cfg_offset
+ header
->cfg_size
;
2120 const int32_t string_offset
= main_offset
+ header
->main_size
;
2121 struct data_in
*data_in
;
2122 struct lto_input_block ib_main
;
2126 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
2130 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
2131 header
->string_size
, NULL
);
2132 count
= lto_input_uleb128 (&ib_main
);
2134 for (i
= 0; i
< count
; i
++)
2137 struct cgraph_node
*node
;
2138 lto_cgraph_encoder_t encoder
;
2140 index
= lto_input_uleb128 (&ib_main
);
2141 encoder
= file_data
->cgraph_node_encoder
;
2142 node
= lto_cgraph_encoder_deref (encoder
, index
);
2143 ipa_read_node_info (&ib_main
, node
, data_in
);
2145 lto_free_section_data (file_data
, LTO_section_jump_functions
, NULL
, data
,
2147 lto_data_in_delete (data_in
);
2150 /* Read ipcp jump functions. */
2153 ipa_prop_read_jump_functions (void)
2155 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
2156 struct lto_file_decl_data
*file_data
;
2159 ipa_check_create_node_params ();
2160 ipa_check_create_edge_args ();
2161 ipa_register_cgraph_hooks ();
2163 while ((file_data
= file_data_vec
[j
++]))
2166 const char *data
= lto_get_section_data (file_data
, LTO_section_jump_functions
, NULL
, &len
);
2169 ipa_prop_read_section (file_data
, data
, len
);
2173 /* After merging units, we can get mismatch in argument counts.
2174 Also decl merging might've rendered parameter lists obsolette.
2175 Also compute called_with_variable_arg info. */
2178 ipa_update_after_lto_read (void)
2180 struct cgraph_node
*node
;
2181 struct cgraph_edge
*cs
;
2183 ipa_check_create_node_params ();
2184 ipa_check_create_edge_args ();
2186 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2188 ipa_initialize_node_params (node
);
2190 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2192 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
2194 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs
))
2195 != ipa_get_param_count (IPA_NODE_REF (cs
->callee
)))
2196 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs
->callee
));