1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "langhooks.h"
29 #include "tree-flow.h"
30 #include "tree-pass.h"
31 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "lto-streamer.h"
38 /* Vector where the parameter infos are actually stored. */
39 VEC (ipa_node_params_t
, heap
) *ipa_node_params_vector
;
40 /* Vector where the parameter infos are actually stored. */
41 VEC (ipa_edge_args_t
, gc
) *ipa_edge_args_vector
;
43 /* Holders of ipa cgraph hooks: */
44 static struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
45 static struct cgraph_node_hook_list
*node_removal_hook_holder
;
46 static struct cgraph_2edge_hook_list
*edge_duplication_hook_holder
;
47 static struct cgraph_2node_hook_list
*node_duplication_hook_holder
;
49 /* Add cgraph NODE described by INFO to the worklist WL regardless of whether
50 it is in one or not. It should almost never be used directly, as opposed to
51 ipa_push_func_to_list. */
54 ipa_push_func_to_list_1 (struct ipa_func_list
**wl
,
55 struct cgraph_node
*node
,
56 struct ipa_node_params
*info
)
58 struct ipa_func_list
*temp
;
60 info
->node_enqueued
= 1;
61 temp
= XCNEW (struct ipa_func_list
);
67 /* Initialize worklist to contain all functions. */
69 struct ipa_func_list
*
70 ipa_init_func_list (void)
72 struct cgraph_node
*node
;
73 struct ipa_func_list
* wl
;
76 for (node
= cgraph_nodes
; node
; node
= node
->next
)
79 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
80 /* Unreachable nodes should have been eliminated before ipcp and
82 gcc_assert (node
->needed
|| node
->reachable
);
83 ipa_push_func_to_list_1 (&wl
, node
, info
);
89 /* Remove a function from the worklist WL and return it. */
92 ipa_pop_func_from_list (struct ipa_func_list
**wl
)
94 struct ipa_node_params
*info
;
95 struct ipa_func_list
*first
;
96 struct cgraph_node
*node
;
103 info
= IPA_NODE_REF (node
);
104 info
->node_enqueued
= 0;
108 /* Return index of the formal whose tree is PTREE in function which corresponds
112 ipa_get_param_decl_index (struct ipa_node_params
*info
, tree ptree
)
116 count
= ipa_get_param_count (info
);
117 for (i
= 0; i
< count
; i
++)
118 if (ipa_get_param(info
, i
) == ptree
)
124 /* Populate the param_decl field in parameter descriptors of INFO that
125 corresponds to NODE. */
128 ipa_populate_param_decls (struct cgraph_node
*node
,
129 struct ipa_node_params
*info
)
137 fnargs
= DECL_ARGUMENTS (fndecl
);
139 for (parm
= fnargs
; parm
; parm
= TREE_CHAIN (parm
))
141 info
->params
[param_num
].decl
= parm
;
146 /* Return how many formal parameters FNDECL has. */
149 count_formal_params_1 (tree fndecl
)
154 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
160 /* Count number of formal parameters in NOTE. Store the result to the
161 appropriate field of INFO. */
164 ipa_count_formal_params (struct cgraph_node
*node
,
165 struct ipa_node_params
*info
)
169 param_num
= count_formal_params_1 (node
->decl
);
170 ipa_set_param_count (info
, param_num
);
173 /* Initialize the ipa_node_params structure associated with NODE by counting
174 the function parameters, creating the descriptors and populating their
178 ipa_initialize_node_params (struct cgraph_node
*node
)
180 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
184 ipa_count_formal_params (node
, info
);
185 info
->params
= XCNEWVEC (struct ipa_param_descriptor
,
186 ipa_get_param_count (info
));
187 ipa_populate_param_decls (node
, info
);
191 /* Callback of walk_stmt_load_store_addr_ops for the visit_store and visit_addr
192 parameters. If OP is a parameter declaration, mark it as modified in the
193 info structure passed in DATA. */
196 visit_store_addr_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED
,
199 struct ipa_node_params
*info
= (struct ipa_node_params
*) data
;
201 if (TREE_CODE (op
) == PARM_DECL
)
203 int index
= ipa_get_param_decl_index (info
, op
);
204 gcc_assert (index
>= 0);
205 info
->params
[index
].modified
= true;
211 /* Compute which formal parameters of function associated with NODE are locally
212 modified or their address is taken. Note that this does not apply on
213 parameters with SSA names but those can and should be analyzed
217 ipa_detect_param_modifications (struct cgraph_node
*node
)
219 tree decl
= node
->decl
;
221 struct function
*func
;
222 gimple_stmt_iterator gsi
;
223 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
225 if (ipa_get_param_count (info
) == 0 || info
->modification_analysis_done
)
228 func
= DECL_STRUCT_FUNCTION (decl
);
229 FOR_EACH_BB_FN (bb
, func
)
230 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
231 walk_stmt_load_store_addr_ops (gsi_stmt (gsi
), info
, NULL
,
232 visit_store_addr_for_mod_analysis
,
233 visit_store_addr_for_mod_analysis
);
235 info
->modification_analysis_done
= 1;
238 /* Count number of arguments callsite CS has and store it in
239 ipa_edge_args structure corresponding to this callsite. */
242 ipa_count_arguments (struct cgraph_edge
*cs
)
247 stmt
= cs
->call_stmt
;
248 gcc_assert (is_gimple_call (stmt
));
249 arg_num
= gimple_call_num_args (stmt
);
250 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
251 <= (unsigned) cgraph_edge_max_uid
)
252 VEC_safe_grow_cleared (ipa_edge_args_t
, gc
,
253 ipa_edge_args_vector
, cgraph_edge_max_uid
+ 1);
254 ipa_set_cs_argument_count (IPA_EDGE_REF (cs
), arg_num
);
257 /* Print the jump functions of all arguments on all call graph edges going from
261 ipa_print_node_jump_functions (FILE *f
, struct cgraph_node
*node
)
264 struct cgraph_edge
*cs
;
265 struct ipa_jump_func
*jump_func
;
266 enum jump_func_type type
;
268 fprintf (f
, " Jump functions of caller %s:\n", cgraph_node_name (node
));
269 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
271 if (!ipa_edge_args_info_available_for_edge_p (cs
))
274 fprintf (f
, " callsite %s ", cgraph_node_name (node
));
275 fprintf (f
, "-> %s :: \n", cgraph_node_name (cs
->callee
));
277 count
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
278 for (i
= 0; i
< count
; i
++)
280 jump_func
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), i
);
281 type
= jump_func
->type
;
283 fprintf (f
, " param %d: ", i
);
284 if (type
== IPA_JF_UNKNOWN
)
285 fprintf (f
, "UNKNOWN\n");
286 else if (type
== IPA_JF_CONST
)
288 tree val
= jump_func
->value
.constant
;
289 fprintf (f
, "CONST: ");
290 print_generic_expr (f
, val
, 0);
293 else if (type
== IPA_JF_CONST_MEMBER_PTR
)
295 fprintf (f
, "CONST MEMBER PTR: ");
296 print_generic_expr (f
, jump_func
->value
.member_cst
.pfn
, 0);
298 print_generic_expr (f
, jump_func
->value
.member_cst
.delta
, 0);
301 else if (type
== IPA_JF_PASS_THROUGH
)
303 fprintf (f
, "PASS THROUGH: ");
304 fprintf (f
, "%d, op %s ",
305 jump_func
->value
.pass_through
.formal_id
,
307 jump_func
->value
.pass_through
.operation
]);
308 if (jump_func
->value
.pass_through
.operation
!= NOP_EXPR
)
309 print_generic_expr (dump_file
,
310 jump_func
->value
.pass_through
.operand
, 0);
311 fprintf (dump_file
, "\n");
313 else if (type
== IPA_JF_ANCESTOR
)
315 fprintf (f
, "ANCESTOR: ");
316 fprintf (f
, "%d, offset "HOST_WIDE_INT_PRINT_DEC
"\n",
317 jump_func
->value
.ancestor
.formal_id
,
318 jump_func
->value
.ancestor
.offset
);
324 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
327 ipa_print_all_jump_functions (FILE *f
)
329 struct cgraph_node
*node
;
331 fprintf (f
, "\nJump functions:\n");
332 for (node
= cgraph_nodes
; node
; node
= node
->next
)
334 ipa_print_node_jump_functions (f
, node
);
338 /* Determine whether passing ssa name NAME constitutes a polynomial
339 pass-through function or getting an address of an acestor and if so, write
340 such a jump function to JFUNC. INFO describes the caller. */
343 compute_complex_pass_through (struct ipa_node_params
*info
,
344 struct ipa_jump_func
*jfunc
,
347 HOST_WIDE_INT offset
, size
, max_size
;
350 gimple stmt
= SSA_NAME_DEF_STMT (name
);
352 if (!is_gimple_assign (stmt
))
354 op1
= gimple_assign_rhs1 (stmt
);
355 op2
= gimple_assign_rhs2 (stmt
);
359 if (TREE_CODE (op1
) != SSA_NAME
360 || !SSA_NAME_IS_DEFAULT_DEF (op1
)
361 || (TREE_CODE_CLASS (gimple_expr_code (stmt
)) != tcc_comparison
362 && !useless_type_conversion_p (TREE_TYPE (name
),
364 || !is_gimple_ip_invariant (op2
))
367 index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (op1
));
370 jfunc
->type
= IPA_JF_PASS_THROUGH
;
371 jfunc
->value
.pass_through
.formal_id
= index
;
372 jfunc
->value
.pass_through
.operation
= gimple_assign_rhs_code (stmt
);
373 jfunc
->value
.pass_through
.operand
= op2
;
378 if (TREE_CODE (op1
) != ADDR_EXPR
)
380 op1
= TREE_OPERAND (op1
, 0);
381 type
= TREE_TYPE (op1
);
383 op1
= get_ref_base_and_extent (op1
, &offset
, &size
, &max_size
);
384 if (TREE_CODE (op1
) != INDIRECT_REF
385 /* If this is a varying address, punt. */
389 op1
= TREE_OPERAND (op1
, 0);
390 if (TREE_CODE (op1
) != SSA_NAME
391 || !SSA_NAME_IS_DEFAULT_DEF (op1
))
394 index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (op1
));
397 jfunc
->type
= IPA_JF_ANCESTOR
;
398 jfunc
->value
.ancestor
.formal_id
= index
;
399 jfunc
->value
.ancestor
.offset
= offset
;
400 jfunc
->value
.ancestor
.type
= type
;
405 /* Determine the jump functions of scalar arguments. Scalar means SSA names
406 and constants of a number of selected types. INFO is the ipa_node_params
407 structure associated with the caller, FUNCTIONS is a pointer to an array of
408 jump function structures associated with CALL which is the call statement
412 compute_scalar_jump_functions (struct ipa_node_params
*info
,
413 struct ipa_jump_func
*functions
,
419 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
421 arg
= gimple_call_arg (call
, num
);
423 if (is_gimple_ip_invariant (arg
))
425 functions
[num
].type
= IPA_JF_CONST
;
426 functions
[num
].value
.constant
= arg
;
428 else if (TREE_CODE (arg
) == SSA_NAME
)
430 if (SSA_NAME_IS_DEFAULT_DEF (arg
))
432 int index
= ipa_get_param_decl_index (info
, SSA_NAME_VAR (arg
));
436 functions
[num
].type
= IPA_JF_PASS_THROUGH
;
437 functions
[num
].value
.pass_through
.formal_id
= index
;
438 functions
[num
].value
.pass_through
.operation
= NOP_EXPR
;
442 compute_complex_pass_through (info
, &functions
[num
], arg
);
447 /* Inspect the given TYPE and return true iff it has the same structure (the
448 same number of fields of the same types) as a C++ member pointer. If
449 METHOD_PTR and DELTA are non-NULL, store the trees representing the
450 corresponding fields there. */
453 type_like_member_ptr_p (tree type
, tree
*method_ptr
, tree
*delta
)
457 if (TREE_CODE (type
) != RECORD_TYPE
)
460 fld
= TYPE_FIELDS (type
);
461 if (!fld
|| !POINTER_TYPE_P (TREE_TYPE (fld
))
462 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld
))) != METHOD_TYPE
)
468 fld
= TREE_CHAIN (fld
);
469 if (!fld
|| INTEGRAL_TYPE_P (fld
))
474 if (TREE_CHAIN (fld
))
480 /* Go through arguments of the CALL and for every one that looks like a member
481 pointer, check whether it can be safely declared pass-through and if so,
482 mark that to the corresponding item of jump FUNCTIONS. Return true iff
483 there are non-pass-through member pointers within the arguments. INFO
484 describes formal parameters of the caller. */
487 compute_pass_through_member_ptrs (struct ipa_node_params
*info
,
488 struct ipa_jump_func
*functions
,
491 bool undecided_members
= false;
495 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
497 arg
= gimple_call_arg (call
, num
);
499 if (type_like_member_ptr_p (TREE_TYPE (arg
), NULL
, NULL
))
501 if (TREE_CODE (arg
) == PARM_DECL
)
503 int index
= ipa_get_param_decl_index (info
, arg
);
505 gcc_assert (index
>=0);
506 if (!ipa_is_param_modified (info
, index
))
508 functions
[num
].type
= IPA_JF_PASS_THROUGH
;
509 functions
[num
].value
.pass_through
.formal_id
= index
;
510 functions
[num
].value
.pass_through
.operation
= NOP_EXPR
;
513 undecided_members
= true;
516 undecided_members
= true;
520 return undecided_members
;
523 /* Simple function filling in a member pointer constant jump function (with PFN
524 and DELTA as the constant value) into JFUNC. */
527 fill_member_ptr_cst_jump_function (struct ipa_jump_func
*jfunc
,
528 tree pfn
, tree delta
)
530 jfunc
->type
= IPA_JF_CONST_MEMBER_PTR
;
531 jfunc
->value
.member_cst
.pfn
= pfn
;
532 jfunc
->value
.member_cst
.delta
= delta
;
535 /* If RHS is an SSA_NAMe and it is defined by a simple copy assign statement,
536 return the rhs of its defining statement. */
539 get_ssa_def_if_simple_copy (tree rhs
)
541 while (TREE_CODE (rhs
) == SSA_NAME
&& !SSA_NAME_IS_DEFAULT_DEF (rhs
))
543 gimple def_stmt
= SSA_NAME_DEF_STMT (rhs
);
545 if (gimple_assign_single_p (def_stmt
))
546 rhs
= gimple_assign_rhs1 (def_stmt
);
553 /* Traverse statements from CALL backwards, scanning whether the argument ARG
554 which is a member pointer is filled in with constant values. If it is, fill
555 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
556 fields of the record type of the member pointer. To give an example, we
557 look for a pattern looking like the following:
559 D.2515.__pfn ={v} printStuff;
560 D.2515.__delta ={v} 0;
561 i_1 = doprinting (D.2515); */
564 determine_cst_member_ptr (gimple call
, tree arg
, tree method_field
,
565 tree delta_field
, struct ipa_jump_func
*jfunc
)
567 gimple_stmt_iterator gsi
;
568 tree method
= NULL_TREE
;
569 tree delta
= NULL_TREE
;
571 gsi
= gsi_for_stmt (call
);
574 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
576 gimple stmt
= gsi_stmt (gsi
);
579 if (!gimple_assign_single_p (stmt
))
582 lhs
= gimple_assign_lhs (stmt
);
583 rhs
= gimple_assign_rhs1 (stmt
);
585 if (TREE_CODE (lhs
) != COMPONENT_REF
586 || TREE_OPERAND (lhs
, 0) != arg
)
589 fld
= TREE_OPERAND (lhs
, 1);
590 if (!method
&& fld
== method_field
)
592 rhs
= get_ssa_def_if_simple_copy (rhs
);
593 if (TREE_CODE (rhs
) == ADDR_EXPR
594 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == FUNCTION_DECL
595 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs
, 0))) == METHOD_TYPE
)
597 method
= TREE_OPERAND (rhs
, 0);
600 fill_member_ptr_cst_jump_function (jfunc
, rhs
, delta
);
608 if (!delta
&& fld
== delta_field
)
610 rhs
= get_ssa_def_if_simple_copy (rhs
);
611 if (TREE_CODE (rhs
) == INTEGER_CST
)
616 fill_member_ptr_cst_jump_function (jfunc
, rhs
, delta
);
628 /* Go through the arguments of the CALL and for every member pointer within
629 tries determine whether it is a constant. If it is, create a corresponding
630 constant jump function in FUNCTIONS which is an array of jump functions
631 associated with the call. */
634 compute_cst_member_ptr_arguments (struct ipa_jump_func
*functions
,
638 tree arg
, method_field
, delta_field
;
640 for (num
= 0; num
< gimple_call_num_args (call
); num
++)
642 arg
= gimple_call_arg (call
, num
);
644 if (functions
[num
].type
== IPA_JF_UNKNOWN
645 && type_like_member_ptr_p (TREE_TYPE (arg
), &method_field
,
647 determine_cst_member_ptr (call
, arg
, method_field
, delta_field
,
652 /* Compute jump function for all arguments of callsite CS and insert the
653 information in the jump_functions array in the ipa_edge_args corresponding
657 ipa_compute_jump_functions (struct cgraph_edge
*cs
)
659 struct ipa_node_params
*info
= IPA_NODE_REF (cs
->caller
);
660 struct ipa_edge_args
*arguments
= IPA_EDGE_REF (cs
);
663 if (ipa_get_cs_argument_count (arguments
) == 0 || arguments
->jump_functions
)
665 arguments
->jump_functions
= GGC_CNEWVEC (struct ipa_jump_func
,
666 ipa_get_cs_argument_count (arguments
));
668 call
= cs
->call_stmt
;
669 gcc_assert (is_gimple_call (call
));
671 /* We will deal with constants and SSA scalars first: */
672 compute_scalar_jump_functions (info
, arguments
->jump_functions
, call
);
674 /* Let's check whether there are any potential member pointers and if so,
675 whether we can determine their functions as pass_through. */
676 if (!compute_pass_through_member_ptrs (info
, arguments
->jump_functions
, call
))
679 /* Finally, let's check whether we actually pass a new constant member
681 compute_cst_member_ptr_arguments (arguments
->jump_functions
, call
);
684 /* If RHS looks like a rhs of a statement loading pfn from a member
685 pointer formal parameter, return the parameter, otherwise return
686 NULL. If USE_DELTA, then we look for a use of the delta field
687 rather than the pfn. */
690 ipa_get_member_ptr_load_param (tree rhs
, bool use_delta
)
696 if (TREE_CODE (rhs
) != COMPONENT_REF
)
699 rec
= TREE_OPERAND (rhs
, 0);
700 if (TREE_CODE (rec
) != PARM_DECL
701 || !type_like_member_ptr_p (TREE_TYPE (rec
), &ptr_field
, &delta_field
))
704 fld
= TREE_OPERAND (rhs
, 1);
705 if (use_delta
? (fld
== delta_field
) : (fld
== ptr_field
))
711 /* If STMT looks like a statement loading a value from a member pointer formal
712 parameter, this function returns that parameter. */
715 ipa_get_stmt_member_ptr_load_param (gimple stmt
, bool use_delta
)
719 if (!gimple_assign_single_p (stmt
))
722 rhs
= gimple_assign_rhs1 (stmt
);
723 return ipa_get_member_ptr_load_param (rhs
, use_delta
);
726 /* Returns true iff T is an SSA_NAME defined by a statement. */
729 ipa_is_ssa_with_stmt_def (tree t
)
731 if (TREE_CODE (t
) == SSA_NAME
732 && !SSA_NAME_IS_DEFAULT_DEF (t
))
738 /* Creates a new note describing a call to a parameter number FORMAL_ID and
739 attaches it to the linked list of INFO. It also sets the called flag of the
740 parameter. STMT is the corresponding call statement. */
743 ipa_note_param_call (struct ipa_node_params
*info
, int formal_id
,
746 struct ipa_param_call_note
*note
;
747 basic_block bb
= gimple_bb (stmt
);
749 info
->params
[formal_id
].called
= 1;
751 note
= XCNEW (struct ipa_param_call_note
);
752 note
->formal_id
= formal_id
;
754 note
->lto_stmt_uid
= gimple_uid (stmt
);
755 note
->count
= bb
->count
;
756 note
->frequency
= compute_call_stmt_bb_frequency (current_function_decl
, bb
);
757 note
->loop_nest
= bb
->loop_depth
;
759 note
->next
= info
->param_calls
;
760 info
->param_calls
= note
;
765 /* Analyze the CALL and examine uses of formal parameters of the caller
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 a note describing the call is created. This is very simple
769 for ordinary pointers represented in SSA but not-so-nice when it comes to
770 member pointers. The ugly part of this function does nothing more than
771 tries to match the pattern of such a call. An example of such a pattern is
772 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 ipa_node_params
*info
, gimple call
)
814 tree target
= gimple_call_fn (call
);
819 tree rec
, rec2
, cond
;
822 basic_block bb
, virt_bb
, join
;
824 if (TREE_CODE (target
) != SSA_NAME
)
827 var
= SSA_NAME_VAR (target
);
828 if (SSA_NAME_IS_DEFAULT_DEF (target
))
830 /* assuming TREE_CODE (var) == PARM_DECL */
831 index
= ipa_get_param_decl_index (info
, var
);
833 ipa_note_param_call (info
, index
, call
);
837 /* Now we need to try to match the complex pattern of calling a member
840 if (!POINTER_TYPE_P (TREE_TYPE (target
))
841 || TREE_CODE (TREE_TYPE (TREE_TYPE (target
))) != METHOD_TYPE
)
844 def
= SSA_NAME_DEF_STMT (target
);
845 if (gimple_code (def
) != GIMPLE_PHI
)
848 if (gimple_phi_num_args (def
) != 2)
851 /* First, we need to check whether one of these is a load from a member
852 pointer that is a parameter to this function. */
853 n1
= PHI_ARG_DEF (def
, 0);
854 n2
= PHI_ARG_DEF (def
, 1);
855 if (!ipa_is_ssa_with_stmt_def (n1
) || !ipa_is_ssa_with_stmt_def (n2
))
857 d1
= SSA_NAME_DEF_STMT (n1
);
858 d2
= SSA_NAME_DEF_STMT (n2
);
860 if ((rec
= ipa_get_stmt_member_ptr_load_param (d1
, false)))
862 if (ipa_get_stmt_member_ptr_load_param (d2
, false))
866 virt_bb
= gimple_bb (d2
);
868 else if ((rec
= ipa_get_stmt_member_ptr_load_param (d2
, false)))
871 virt_bb
= gimple_bb (d1
);
876 /* Second, we need to check that the basic blocks are laid out in the way
877 corresponding to the pattern. */
879 join
= gimple_bb (def
);
880 if (!single_pred_p (virt_bb
) || !single_succ_p (virt_bb
)
881 || single_pred (virt_bb
) != bb
882 || single_succ (virt_bb
) != join
)
885 /* Third, let's see that the branching is done depending on the least
886 significant bit of the pfn. */
888 branch
= last_stmt (bb
);
889 if (gimple_code (branch
) != GIMPLE_COND
)
892 if (gimple_cond_code (branch
) != NE_EXPR
893 || !integer_zerop (gimple_cond_rhs (branch
)))
896 cond
= gimple_cond_lhs (branch
);
897 if (!ipa_is_ssa_with_stmt_def (cond
))
900 def
= SSA_NAME_DEF_STMT (cond
);
901 if (!is_gimple_assign (def
)
902 || gimple_assign_rhs_code (def
) != BIT_AND_EXPR
903 || !integer_onep (gimple_assign_rhs2 (def
)))
906 cond
= gimple_assign_rhs1 (def
);
907 if (!ipa_is_ssa_with_stmt_def (cond
))
910 def
= SSA_NAME_DEF_STMT (cond
);
912 if (is_gimple_assign (def
)
913 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def
)))
915 cond
= gimple_assign_rhs1 (def
);
916 if (!ipa_is_ssa_with_stmt_def (cond
))
918 def
= SSA_NAME_DEF_STMT (cond
);
921 rec2
= ipa_get_stmt_member_ptr_load_param (def
,
922 (TARGET_PTRMEMFUNC_VBIT_LOCATION
923 == ptrmemfunc_vbit_in_delta
));
928 index
= ipa_get_param_decl_index (info
, rec
);
929 if (index
>= 0 && !ipa_is_param_modified (info
, index
))
930 ipa_note_param_call (info
, index
, call
);
935 /* Analyze the statement STMT with respect to formal parameters (described in
936 INFO) and their uses. Currently it only checks whether formal parameters
940 ipa_analyze_stmt_uses (struct ipa_node_params
*info
, gimple stmt
)
942 if (is_gimple_call (stmt
))
943 ipa_analyze_call_uses (info
, stmt
);
946 /* Scan the function body of NODE and inspect the uses of formal parameters.
947 Store the findings in various structures of the associated ipa_node_params
948 structure, such as parameter flags, notes etc. */
951 ipa_analyze_params_uses (struct cgraph_node
*node
)
953 tree decl
= node
->decl
;
955 struct function
*func
;
956 gimple_stmt_iterator gsi
;
957 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
959 if (ipa_get_param_count (info
) == 0 || info
->uses_analysis_done
)
962 func
= DECL_STRUCT_FUNCTION (decl
);
963 FOR_EACH_BB_FN (bb
, func
)
965 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
967 gimple stmt
= gsi_stmt (gsi
);
968 ipa_analyze_stmt_uses (info
, stmt
);
972 info
->uses_analysis_done
= 1;
975 /* Update the jump functions associated with call graph edge E when the call
976 graph edge CS is being inlined, assuming that E->caller is already (possibly
977 indirectly) inlined into CS->callee and that E has not been inlined.
979 We keep pass through functions only if they do not contain any operation.
980 This is sufficient for inlining and greately simplifies things. */
983 update_jump_functions_after_inlining (struct cgraph_edge
*cs
,
984 struct cgraph_edge
*e
)
986 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
987 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
988 int count
= ipa_get_cs_argument_count (args
);
991 for (i
= 0; i
< count
; i
++)
993 struct ipa_jump_func
*src
, *dst
= ipa_get_ith_jump_func (args
, i
);
995 if (dst
->type
== IPA_JF_ANCESTOR
)
997 dst
->type
= IPA_JF_UNKNOWN
;
1001 if (dst
->type
!= IPA_JF_PASS_THROUGH
)
1004 /* We must check range due to calls with variable number of arguments and
1005 we cannot combine jump functions with operations. */
1006 if (dst
->value
.pass_through
.operation
!= NOP_EXPR
1007 || (dst
->value
.pass_through
.formal_id
1008 >= ipa_get_cs_argument_count (top
)))
1010 dst
->type
= IPA_JF_UNKNOWN
;
1014 src
= ipa_get_ith_jump_func (top
, dst
->value
.pass_through
.formal_id
);
1019 /* Print out a debug message to file F that we have discovered that an indirect
1020 call described by NT is in fact a call of a known constant function described
1021 by JFUNC. NODE is the node where the call is. */
1024 print_edge_addition_message (FILE *f
, struct ipa_param_call_note
*nt
,
1025 struct ipa_jump_func
*jfunc
,
1026 struct cgraph_node
*node
)
1028 fprintf (f
, "ipa-prop: Discovered an indirect call to a known target (");
1029 if (jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1031 print_node_brief (f
, "", jfunc
->value
.member_cst
.pfn
, 0);
1032 print_node_brief (f
, ", ", jfunc
->value
.member_cst
.delta
, 0);
1035 print_node_brief(f
, "", jfunc
->value
.constant
, 0);
1037 fprintf (f
, ") in %s: ", cgraph_node_name (node
));
1038 print_gimple_stmt (f
, nt
->stmt
, 2, TDF_SLIM
);
1041 /* Update the param called notes associated with NODE when CS is being inlined,
1042 assuming NODE is (potentially indirectly) inlined into CS->callee.
1043 Moreover, if the callee is discovered to be constant, create a new cgraph
1044 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1045 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1048 update_call_notes_after_inlining (struct cgraph_edge
*cs
,
1049 struct cgraph_node
*node
,
1050 VEC (cgraph_edge_p
, heap
) **new_edges
)
1052 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
1053 struct ipa_edge_args
*top
= IPA_EDGE_REF (cs
);
1054 struct ipa_param_call_note
*nt
;
1057 for (nt
= info
->param_calls
; nt
; nt
= nt
->next
)
1059 struct ipa_jump_func
*jfunc
;
1064 /* We must check range due to calls with variable number of arguments: */
1065 if (nt
->formal_id
>= ipa_get_cs_argument_count (top
))
1067 nt
->processed
= true;
1071 jfunc
= ipa_get_ith_jump_func (top
, nt
->formal_id
);
1072 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1073 && jfunc
->value
.pass_through
.operation
== NOP_EXPR
)
1074 nt
->formal_id
= jfunc
->value
.pass_through
.formal_id
;
1075 else if (jfunc
->type
== IPA_JF_CONST
1076 || jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1078 struct cgraph_node
*callee
;
1079 struct cgraph_edge
*new_indirect_edge
;
1082 nt
->processed
= true;
1083 if (jfunc
->type
== IPA_JF_CONST_MEMBER_PTR
)
1084 decl
= jfunc
->value
.member_cst
.pfn
;
1086 decl
= jfunc
->value
.constant
;
1088 if (TREE_CODE (decl
) != ADDR_EXPR
)
1090 decl
= TREE_OPERAND (decl
, 0);
1092 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1094 callee
= cgraph_node (decl
);
1095 if (!callee
|| !callee
->local
.inlinable
)
1100 print_edge_addition_message (dump_file
, nt
, jfunc
, node
);
1102 new_indirect_edge
= cgraph_create_edge (node
, callee
, nt
->stmt
,
1103 nt
->count
, nt
->frequency
,
1105 new_indirect_edge
->lto_stmt_uid
= nt
->lto_stmt_uid
;
1106 new_indirect_edge
->indirect_call
= 1;
1107 ipa_check_create_edge_args ();
1109 VEC_safe_push (cgraph_edge_p
, heap
, *new_edges
, new_indirect_edge
);
1110 top
= IPA_EDGE_REF (cs
);
1114 /* Ancestor jum functions and pass theoughs with operations should
1115 not be used on parameters that then get called. */
1116 gcc_assert (jfunc
->type
== IPA_JF_UNKNOWN
);
1117 nt
->processed
= true;
1123 /* Recursively traverse subtree of NODE (including node) made of inlined
1124 cgraph_edges when CS has been inlined and invoke
1125 update_call_notes_after_inlining on all nodes and
1126 update_jump_functions_after_inlining on all non-inlined edges that lead out
1127 of this subtree. Newly discovered indirect edges will be added to
1128 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1132 propagate_info_to_inlined_callees (struct cgraph_edge
*cs
,
1133 struct cgraph_node
*node
,
1134 VEC (cgraph_edge_p
, heap
) **new_edges
)
1136 struct cgraph_edge
*e
;
1139 res
= update_call_notes_after_inlining (cs
, node
, new_edges
);
1141 for (e
= node
->callees
; e
; e
= e
->next_callee
)
1142 if (!e
->inline_failed
)
1143 res
|= propagate_info_to_inlined_callees (cs
, e
->callee
, new_edges
);
1145 update_jump_functions_after_inlining (cs
, e
);
1150 /* Update jump functions and call note functions on inlining the call site CS.
1151 CS is expected to lead to a node already cloned by
1152 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1153 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1157 ipa_propagate_indirect_call_infos (struct cgraph_edge
*cs
,
1158 VEC (cgraph_edge_p
, heap
) **new_edges
)
1160 /* FIXME lto: We do not stream out indirect call information. */
1164 /* Do nothing if the preparation phase has not been carried out yet
1165 (i.e. during early inlining). */
1166 if (!ipa_node_params_vector
)
1168 gcc_assert (ipa_edge_args_vector
);
1170 return propagate_info_to_inlined_callees (cs
, cs
->callee
, new_edges
);
1173 /* Frees all dynamically allocated structures that the argument info points
1177 ipa_free_edge_args_substructures (struct ipa_edge_args
*args
)
1179 if (args
->jump_functions
)
1180 ggc_free (args
->jump_functions
);
1182 memset (args
, 0, sizeof (*args
));
1185 /* Free all ipa_edge structures. */
1188 ipa_free_all_edge_args (void)
1191 struct ipa_edge_args
*args
;
1194 VEC_iterate (ipa_edge_args_t
, ipa_edge_args_vector
, i
, args
);
1196 ipa_free_edge_args_substructures (args
);
1198 VEC_free (ipa_edge_args_t
, gc
, ipa_edge_args_vector
);
1199 ipa_edge_args_vector
= NULL
;
1202 /* Frees all dynamically allocated structures that the param info points
1206 ipa_free_node_params_substructures (struct ipa_node_params
*info
)
1209 free (info
->params
);
1211 while (info
->param_calls
)
1213 struct ipa_param_call_note
*note
= info
->param_calls
;
1214 info
->param_calls
= note
->next
;
1218 memset (info
, 0, sizeof (*info
));
1221 /* Free all ipa_node_params structures. */
1224 ipa_free_all_node_params (void)
1227 struct ipa_node_params
*info
;
1230 VEC_iterate (ipa_node_params_t
, ipa_node_params_vector
, i
, info
);
1232 ipa_free_node_params_substructures (info
);
1234 VEC_free (ipa_node_params_t
, heap
, ipa_node_params_vector
);
1235 ipa_node_params_vector
= NULL
;
1238 /* Hook that is called by cgraph.c when an edge is removed. */
1241 ipa_edge_removal_hook (struct cgraph_edge
*cs
, void *data ATTRIBUTE_UNUSED
)
1243 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1244 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
1245 <= (unsigned)cs
->uid
)
1247 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs
));
1250 /* Hook that is called by cgraph.c when a node is removed. */
1253 ipa_node_removal_hook (struct cgraph_node
*node
, void *data ATTRIBUTE_UNUSED
)
1255 ipa_free_node_params_substructures (IPA_NODE_REF (node
));
1258 /* Helper function to duplicate an array of size N that is at SRC and store a
1259 pointer to it to DST. Nothing is done if SRC is NULL. */
1262 duplicate_array (void *src
, size_t n
)
1274 /* Like duplicate_array byt in GGC memory. */
1277 duplicate_ggc_array (void *src
, size_t n
)
1289 /* Hook that is called by cgraph.c when a node is duplicated. */
1292 ipa_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
1293 __attribute__((unused
)) void *data
)
1295 struct ipa_edge_args
*old_args
, *new_args
;
1298 ipa_check_create_edge_args ();
1300 old_args
= IPA_EDGE_REF (src
);
1301 new_args
= IPA_EDGE_REF (dst
);
1303 arg_count
= ipa_get_cs_argument_count (old_args
);
1304 ipa_set_cs_argument_count (new_args
, arg_count
);
1305 new_args
->jump_functions
= (struct ipa_jump_func
*)
1306 duplicate_ggc_array (old_args
->jump_functions
,
1307 sizeof (struct ipa_jump_func
) * arg_count
);
1310 /* Hook that is called by cgraph.c when a node is duplicated. */
1313 ipa_node_duplication_hook (struct cgraph_node
*src
, struct cgraph_node
*dst
,
1314 __attribute__((unused
)) void *data
)
1316 struct ipa_node_params
*old_info
, *new_info
;
1317 struct ipa_param_call_note
*note
;
1320 ipa_check_create_node_params ();
1321 old_info
= IPA_NODE_REF (src
);
1322 new_info
= IPA_NODE_REF (dst
);
1323 param_count
= ipa_get_param_count (old_info
);
1325 ipa_set_param_count (new_info
, param_count
);
1326 new_info
->params
= (struct ipa_param_descriptor
*)
1327 duplicate_array (old_info
->params
,
1328 sizeof (struct ipa_param_descriptor
) * param_count
);
1329 new_info
->ipcp_orig_node
= old_info
->ipcp_orig_node
;
1330 new_info
->count_scale
= old_info
->count_scale
;
1332 for (note
= old_info
->param_calls
; note
; note
= note
->next
)
1334 struct ipa_param_call_note
*nn
;
1336 nn
= (struct ipa_param_call_note
*)
1337 xcalloc (1, sizeof (struct ipa_param_call_note
));
1338 memcpy (nn
, note
, sizeof (struct ipa_param_call_note
));
1339 nn
->next
= new_info
->param_calls
;
1340 new_info
->param_calls
= nn
;
1344 /* Register our cgraph hooks if they are not already there. */
1347 ipa_register_cgraph_hooks (void)
1349 if (!edge_removal_hook_holder
)
1350 edge_removal_hook_holder
=
1351 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook
, NULL
);
1352 if (!node_removal_hook_holder
)
1353 node_removal_hook_holder
=
1354 cgraph_add_node_removal_hook (&ipa_node_removal_hook
, NULL
);
1355 if (!edge_duplication_hook_holder
)
1356 edge_duplication_hook_holder
=
1357 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook
, NULL
);
1358 if (!node_duplication_hook_holder
)
1359 node_duplication_hook_holder
=
1360 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook
, NULL
);
1363 /* Unregister our cgraph hooks if they are not already there. */
1366 ipa_unregister_cgraph_hooks (void)
1368 cgraph_remove_edge_removal_hook (edge_removal_hook_holder
);
1369 edge_removal_hook_holder
= NULL
;
1370 cgraph_remove_node_removal_hook (node_removal_hook_holder
);
1371 node_removal_hook_holder
= NULL
;
1372 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder
);
1373 edge_duplication_hook_holder
= NULL
;
1374 cgraph_remove_node_duplication_hook (node_duplication_hook_holder
);
1375 node_duplication_hook_holder
= NULL
;
1378 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1379 longer needed after ipa-cp. */
1382 free_all_ipa_structures_after_ipa_cp (void)
1384 if (!flag_indirect_inlining
)
1386 ipa_free_all_edge_args ();
1387 ipa_free_all_node_params ();
1388 ipa_unregister_cgraph_hooks ();
1392 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
1393 longer needed after indirect inlining. */
1396 free_all_ipa_structures_after_iinln (void)
1398 ipa_free_all_edge_args ();
1399 ipa_free_all_node_params ();
1400 ipa_unregister_cgraph_hooks ();
1403 /* Print ipa_tree_map data structures of all functions in the
1407 ipa_print_node_params (FILE * f
, struct cgraph_node
*node
)
1411 struct ipa_node_params
*info
;
1413 if (!node
->analyzed
)
1415 info
= IPA_NODE_REF (node
);
1416 fprintf (f
, " function %s Trees :: \n", cgraph_node_name (node
));
1417 count
= ipa_get_param_count (info
);
1418 for (i
= 0; i
< count
; i
++)
1420 temp
= ipa_get_param (info
, i
);
1421 if (TREE_CODE (temp
) == PARM_DECL
)
1422 fprintf (f
, " param %d : %s", i
,
1424 ? (*lang_hooks
.decl_printable_name
) (temp
, 2)
1426 if (ipa_is_param_modified (info
, i
))
1427 fprintf (f
, " modified");
1428 if (ipa_is_param_called (info
, i
))
1429 fprintf (f
, " called");
1434 /* Print ipa_tree_map data structures of all functions in the
1438 ipa_print_all_params (FILE * f
)
1440 struct cgraph_node
*node
;
1442 fprintf (f
, "\nFunction parameters:\n");
1443 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1444 ipa_print_node_params (f
, node
);
1447 /* Return a heap allocated vector containing formal parameters of FNDECL. */
1450 ipa_get_vector_of_formal_parms (tree fndecl
)
1452 VEC(tree
, heap
) *args
;
1456 count
= count_formal_params_1 (fndecl
);
1457 args
= VEC_alloc (tree
, heap
, count
);
1458 for (parm
= DECL_ARGUMENTS (fndecl
); parm
; parm
= TREE_CHAIN (parm
))
1459 VEC_quick_push (tree
, args
, parm
);
1464 /* Return a heap allocated vector containing types of formal parameters of
1465 function type FNTYPE. */
1467 static inline VEC(tree
, heap
) *
1468 get_vector_of_formal_parm_types (tree fntype
)
1470 VEC(tree
, heap
) *types
;
1474 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1477 types
= VEC_alloc (tree
, heap
, count
);
1478 for (t
= TYPE_ARG_TYPES (fntype
); t
; t
= TREE_CHAIN (t
))
1479 VEC_quick_push (tree
, types
, TREE_VALUE (t
));
1484 /* Modify the function declaration FNDECL and its type according to the plan in
1485 ADJUSTMENTS. It also sets base fields of individual adjustments structures
1486 to reflect the actual parameters being modified which are determined by the
1487 base_index field. */
1490 ipa_modify_formal_parameters (tree fndecl
, ipa_parm_adjustment_vec adjustments
,
1491 const char *synth_parm_prefix
)
1493 VEC(tree
, heap
) *oparms
, *otypes
;
1494 tree orig_type
, new_type
= NULL
;
1495 tree old_arg_types
, t
, new_arg_types
= NULL
;
1496 tree parm
, *link
= &DECL_ARGUMENTS (fndecl
);
1497 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1498 tree new_reversed
= NULL
;
1499 bool care_for_types
, last_parm_void
;
1501 if (!synth_parm_prefix
)
1502 synth_parm_prefix
= "SYNTH";
1504 oparms
= ipa_get_vector_of_formal_parms (fndecl
);
1505 orig_type
= TREE_TYPE (fndecl
);
1506 old_arg_types
= TYPE_ARG_TYPES (orig_type
);
1508 /* The following test is an ugly hack, some functions simply don't have any
1509 arguments in their type. This is probably a bug but well... */
1510 care_for_types
= (old_arg_types
!= NULL_TREE
);
1513 last_parm_void
= (TREE_VALUE (tree_last (old_arg_types
))
1515 otypes
= get_vector_of_formal_parm_types (orig_type
);
1517 gcc_assert (VEC_length (tree
, oparms
) + 1 == VEC_length (tree
, otypes
));
1519 gcc_assert (VEC_length (tree
, oparms
) == VEC_length (tree
, otypes
));
1523 last_parm_void
= false;
1527 for (i
= 0; i
< len
; i
++)
1529 struct ipa_parm_adjustment
*adj
;
1532 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1533 parm
= VEC_index (tree
, oparms
, adj
->base_index
);
1536 if (adj
->copy_param
)
1539 new_arg_types
= tree_cons (NULL_TREE
, VEC_index (tree
, otypes
,
1543 link
= &TREE_CHAIN (parm
);
1545 else if (!adj
->remove_param
)
1551 ptype
= build_pointer_type (adj
->type
);
1556 new_arg_types
= tree_cons (NULL_TREE
, ptype
, new_arg_types
);
1558 new_parm
= build_decl (UNKNOWN_LOCATION
, PARM_DECL
, NULL_TREE
,
1560 DECL_NAME (new_parm
) = create_tmp_var_name (synth_parm_prefix
);
1562 DECL_ARTIFICIAL (new_parm
) = 1;
1563 DECL_ARG_TYPE (new_parm
) = ptype
;
1564 DECL_CONTEXT (new_parm
) = fndecl
;
1565 TREE_USED (new_parm
) = 1;
1566 DECL_IGNORED_P (new_parm
) = 1;
1567 layout_decl (new_parm
, 0);
1569 add_referenced_var (new_parm
);
1570 mark_sym_for_renaming (new_parm
);
1572 adj
->reduction
= new_parm
;
1576 link
= &TREE_CHAIN (new_parm
);
1584 new_reversed
= nreverse (new_arg_types
);
1588 TREE_CHAIN (new_arg_types
) = void_list_node
;
1590 new_reversed
= void_list_node
;
1594 /* Use copy_node to preserve as much as possible from original type
1595 (debug info, attribute lists etc.)
1596 Exception is METHOD_TYPEs must have THIS argument.
1597 When we are asked to remove it, we need to build new FUNCTION_TYPE
1599 if (TREE_CODE (orig_type
) != METHOD_TYPE
1600 || (VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->copy_param
1601 && VEC_index (ipa_parm_adjustment_t
, adjustments
, 0)->base_index
== 0))
1603 new_type
= copy_node (orig_type
);
1604 TYPE_ARG_TYPES (new_type
) = new_reversed
;
1609 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type
),
1611 TYPE_CONTEXT (new_type
) = TYPE_CONTEXT (orig_type
);
1612 DECL_VINDEX (fndecl
) = NULL_TREE
;
1615 /* This is a new type, not a copy of an old type. Need to reassociate
1616 variants. We can handle everything except the main variant lazily. */
1617 t
= TYPE_MAIN_VARIANT (orig_type
);
1620 TYPE_MAIN_VARIANT (new_type
) = t
;
1621 TYPE_NEXT_VARIANT (new_type
) = TYPE_NEXT_VARIANT (t
);
1622 TYPE_NEXT_VARIANT (t
) = new_type
;
1626 TYPE_MAIN_VARIANT (new_type
) = new_type
;
1627 TYPE_NEXT_VARIANT (new_type
) = NULL
;
1630 TREE_TYPE (fndecl
) = new_type
;
1632 VEC_free (tree
, heap
, otypes
);
1633 VEC_free (tree
, heap
, oparms
);
1636 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
1637 If this is a directly recursive call, CS must be NULL. Otherwise it must
1638 contain the corresponding call graph edge. */
1641 ipa_modify_call_arguments (struct cgraph_edge
*cs
, gimple stmt
,
1642 ipa_parm_adjustment_vec adjustments
)
1644 VEC(tree
, heap
) *vargs
;
1646 gimple_stmt_iterator gsi
;
1650 len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1651 vargs
= VEC_alloc (tree
, heap
, len
);
1653 gsi
= gsi_for_stmt (stmt
);
1654 for (i
= 0; i
< len
; i
++)
1656 struct ipa_parm_adjustment
*adj
;
1658 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1660 if (adj
->copy_param
)
1662 tree arg
= gimple_call_arg (stmt
, adj
->base_index
);
1664 VEC_quick_push (tree
, vargs
, arg
);
1666 else if (!adj
->remove_param
)
1668 tree expr
, orig_expr
;
1669 bool allow_ptr
, repl_found
;
1671 orig_expr
= expr
= gimple_call_arg (stmt
, adj
->base_index
);
1672 if (TREE_CODE (expr
) == ADDR_EXPR
)
1675 expr
= TREE_OPERAND (expr
, 0);
1680 repl_found
= build_ref_for_offset (&expr
, TREE_TYPE (expr
),
1681 adj
->offset
, adj
->type
,
1686 expr
= build_fold_addr_expr (expr
);
1690 tree ptrtype
= build_pointer_type (adj
->type
);
1692 if (!POINTER_TYPE_P (TREE_TYPE (expr
)))
1693 expr
= build_fold_addr_expr (expr
);
1694 if (!useless_type_conversion_p (ptrtype
, TREE_TYPE (expr
)))
1695 expr
= fold_convert (ptrtype
, expr
);
1696 expr
= fold_build2 (POINTER_PLUS_EXPR
, ptrtype
, expr
,
1697 build_int_cst (size_type_node
,
1698 adj
->offset
/ BITS_PER_UNIT
));
1700 expr
= fold_build1 (INDIRECT_REF
, adj
->type
, expr
);
1702 expr
= force_gimple_operand_gsi (&gsi
, expr
,
1704 || is_gimple_reg_type (adj
->type
),
1705 NULL
, true, GSI_SAME_STMT
);
1706 VEC_quick_push (tree
, vargs
, expr
);
1710 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1712 fprintf (dump_file
, "replacing stmt:");
1713 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), 0, 0);
1716 callee_decl
= !cs
? gimple_call_fndecl (stmt
) : cs
->callee
->decl
;
1717 new_stmt
= gimple_build_call_vec (callee_decl
, vargs
);
1718 VEC_free (tree
, heap
, vargs
);
1719 if (gimple_call_lhs (stmt
))
1720 gimple_call_set_lhs (new_stmt
, gimple_call_lhs (stmt
));
1722 gimple_set_block (new_stmt
, gimple_block (stmt
));
1723 if (gimple_has_location (stmt
))
1724 gimple_set_location (new_stmt
, gimple_location (stmt
));
1725 gimple_call_copy_flags (new_stmt
, stmt
);
1726 gimple_call_set_chain (new_stmt
, gimple_call_chain (stmt
));
1728 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1730 fprintf (dump_file
, "with stmt:");
1731 print_gimple_stmt (dump_file
, new_stmt
, 0, 0);
1732 fprintf (dump_file
, "\n");
1734 gsi_replace (&gsi
, new_stmt
, true);
1736 cgraph_set_call_stmt (cs
, new_stmt
);
1737 update_ssa (TODO_update_ssa
);
1738 free_dominance_info (CDI_DOMINATORS
);
1741 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
1744 index_in_adjustments_multiple_times_p (int base_index
,
1745 ipa_parm_adjustment_vec adjustments
)
1747 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1750 for (i
= 0; i
< len
; i
++)
1752 struct ipa_parm_adjustment
*adj
;
1753 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1755 if (adj
->base_index
== base_index
)
1767 /* Return adjustments that should have the same effect on function parameters
1768 and call arguments as if they were first changed according to adjustments in
1769 INNER and then by adjustments in OUTER. */
1771 ipa_parm_adjustment_vec
1772 ipa_combine_adjustments (ipa_parm_adjustment_vec inner
,
1773 ipa_parm_adjustment_vec outer
)
1775 int i
, outlen
= VEC_length (ipa_parm_adjustment_t
, outer
);
1776 int inlen
= VEC_length (ipa_parm_adjustment_t
, inner
);
1778 ipa_parm_adjustment_vec adjustments
, tmp
;
1780 tmp
= VEC_alloc (ipa_parm_adjustment_t
, heap
, inlen
);
1781 for (i
= 0; i
< inlen
; i
++)
1783 struct ipa_parm_adjustment
*n
;
1784 n
= VEC_index (ipa_parm_adjustment_t
, inner
, i
);
1786 if (n
->remove_param
)
1789 VEC_quick_push (ipa_parm_adjustment_t
, tmp
, n
);
1792 adjustments
= VEC_alloc (ipa_parm_adjustment_t
, heap
, outlen
+ removals
);
1793 for (i
= 0; i
< outlen
; i
++)
1795 struct ipa_parm_adjustment
*r
;
1796 struct ipa_parm_adjustment
*out
= VEC_index (ipa_parm_adjustment_t
,
1798 struct ipa_parm_adjustment
*in
= VEC_index (ipa_parm_adjustment_t
, tmp
,
1801 gcc_assert (!in
->remove_param
);
1802 if (out
->remove_param
)
1804 if (!index_in_adjustments_multiple_times_p (in
->base_index
, tmp
))
1806 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1807 memset (r
, 0, sizeof (*r
));
1808 r
->remove_param
= true;
1813 r
= VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, NULL
);
1814 memset (r
, 0, sizeof (*r
));
1815 r
->base_index
= in
->base_index
;
1816 r
->type
= out
->type
;
1818 /* FIXME: Create nonlocal value too. */
1820 if (in
->copy_param
&& out
->copy_param
)
1821 r
->copy_param
= true;
1822 else if (in
->copy_param
)
1823 r
->offset
= out
->offset
;
1824 else if (out
->copy_param
)
1825 r
->offset
= in
->offset
;
1827 r
->offset
= in
->offset
+ out
->offset
;
1830 for (i
= 0; i
< inlen
; i
++)
1832 struct ipa_parm_adjustment
*n
= VEC_index (ipa_parm_adjustment_t
,
1835 if (n
->remove_param
)
1836 VEC_quick_push (ipa_parm_adjustment_t
, adjustments
, n
);
1839 VEC_free (ipa_parm_adjustment_t
, heap
, tmp
);
1843 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
1844 friendly way, assuming they are meant to be applied to FNDECL. */
1847 ipa_dump_param_adjustments (FILE *file
, ipa_parm_adjustment_vec adjustments
,
1850 int i
, len
= VEC_length (ipa_parm_adjustment_t
, adjustments
);
1852 VEC(tree
, heap
) *parms
= ipa_get_vector_of_formal_parms (fndecl
);
1854 fprintf (file
, "IPA param adjustments: ");
1855 for (i
= 0; i
< len
; i
++)
1857 struct ipa_parm_adjustment
*adj
;
1858 adj
= VEC_index (ipa_parm_adjustment_t
, adjustments
, i
);
1861 fprintf (file
, " ");
1865 fprintf (file
, "%i. base_index: %i - ", i
, adj
->base_index
);
1866 print_generic_expr (file
, VEC_index (tree
, parms
, adj
->base_index
), 0);
1869 fprintf (file
, ", base: ");
1870 print_generic_expr (file
, adj
->base
, 0);
1874 fprintf (file
, ", reduction: ");
1875 print_generic_expr (file
, adj
->reduction
, 0);
1877 if (adj
->new_ssa_base
)
1879 fprintf (file
, ", new_ssa_base: ");
1880 print_generic_expr (file
, adj
->new_ssa_base
, 0);
1883 if (adj
->copy_param
)
1884 fprintf (file
, ", copy_param");
1885 else if (adj
->remove_param
)
1886 fprintf (file
, ", remove_param");
1888 fprintf (file
, ", offset %li", (long) adj
->offset
);
1890 fprintf (file
, ", by_ref");
1891 print_node_brief (file
, ", type: ", adj
->type
, 0);
1892 fprintf (file
, "\n");
1894 VEC_free (tree
, heap
, parms
);
1897 /* Stream out jump function JUMP_FUNC to OB. */
1900 ipa_write_jump_function (struct output_block
*ob
,
1901 struct ipa_jump_func
*jump_func
)
1903 lto_output_uleb128_stream (ob
->main_stream
,
1906 switch (jump_func
->type
)
1908 case IPA_JF_UNKNOWN
:
1911 lto_output_tree (ob
, jump_func
->value
.constant
, true);
1913 case IPA_JF_PASS_THROUGH
:
1914 lto_output_tree (ob
, jump_func
->value
.pass_through
.operand
, true);
1915 lto_output_uleb128_stream (ob
->main_stream
,
1916 jump_func
->value
.pass_through
.formal_id
);
1917 lto_output_uleb128_stream (ob
->main_stream
,
1918 jump_func
->value
.pass_through
.operation
);
1920 case IPA_JF_ANCESTOR
:
1921 lto_output_uleb128_stream (ob
->main_stream
,
1922 jump_func
->value
.ancestor
.offset
);
1923 lto_output_tree (ob
, jump_func
->value
.ancestor
.type
, true);
1924 lto_output_uleb128_stream (ob
->main_stream
,
1925 jump_func
->value
.ancestor
.formal_id
);
1927 case IPA_JF_CONST_MEMBER_PTR
:
1928 lto_output_tree (ob
, jump_func
->value
.member_cst
.pfn
, true);
1929 lto_output_tree (ob
, jump_func
->value
.member_cst
.delta
, false);
1934 /* Read in jump function JUMP_FUNC from IB. */
1937 ipa_read_jump_function (struct lto_input_block
*ib
,
1938 struct ipa_jump_func
*jump_func
,
1939 struct data_in
*data_in
)
1941 jump_func
->type
= (enum jump_func_type
) lto_input_uleb128 (ib
);
1943 switch (jump_func
->type
)
1945 case IPA_JF_UNKNOWN
:
1948 jump_func
->value
.constant
= lto_input_tree (ib
, data_in
);
1950 case IPA_JF_PASS_THROUGH
:
1951 jump_func
->value
.pass_through
.operand
= lto_input_tree (ib
, data_in
);
1952 jump_func
->value
.pass_through
.formal_id
= lto_input_uleb128 (ib
);
1953 jump_func
->value
.pass_through
.operation
= (enum tree_code
) lto_input_uleb128 (ib
);
1955 case IPA_JF_ANCESTOR
:
1956 jump_func
->value
.ancestor
.offset
= lto_input_uleb128 (ib
);
1957 jump_func
->value
.ancestor
.type
= lto_input_tree (ib
, data_in
);
1958 jump_func
->value
.ancestor
.formal_id
= lto_input_uleb128 (ib
);
1960 case IPA_JF_CONST_MEMBER_PTR
:
1961 jump_func
->value
.member_cst
.pfn
= lto_input_tree (ib
, data_in
);
1962 jump_func
->value
.member_cst
.delta
= lto_input_tree (ib
, data_in
);
1967 /* Stream out a parameter call note. */
1970 ipa_write_param_call_note (struct output_block
*ob
,
1971 struct ipa_param_call_note
*note
)
1973 gcc_assert (!note
->processed
);
1974 lto_output_uleb128_stream (ob
->main_stream
, gimple_uid (note
->stmt
));
1975 lto_output_sleb128_stream (ob
->main_stream
, note
->formal_id
);
1976 lto_output_sleb128_stream (ob
->main_stream
, note
->count
);
1977 lto_output_sleb128_stream (ob
->main_stream
, note
->frequency
);
1978 lto_output_sleb128_stream (ob
->main_stream
, note
->loop_nest
);
1981 /* Read in a parameter call note. */
1984 ipa_read_param_call_note (struct lto_input_block
*ib
,
1985 struct ipa_node_params
*info
)
1988 struct ipa_param_call_note
*note
= XCNEW (struct ipa_param_call_note
);
1990 note
->lto_stmt_uid
= (unsigned int) lto_input_uleb128 (ib
);
1991 note
->formal_id
= (int) lto_input_sleb128 (ib
);
1992 note
->count
= (gcov_type
) lto_input_sleb128 (ib
);
1993 note
->frequency
= (int) lto_input_sleb128 (ib
);
1994 note
->loop_nest
= (int) lto_input_sleb128 (ib
);
1996 note
->next
= info
->param_calls
;
1997 info
->param_calls
= note
;
2001 /* Stream out NODE info to OB. */
2004 ipa_write_node_info (struct output_block
*ob
, struct cgraph_node
*node
)
2007 lto_cgraph_encoder_t encoder
;
2008 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2010 struct cgraph_edge
*e
;
2011 struct bitpack_d
*bp
;
2013 struct ipa_param_call_note
*note
;
2015 encoder
= ob
->decl_state
->cgraph_node_encoder
;
2016 node_ref
= lto_cgraph_encoder_encode (encoder
, node
);
2017 lto_output_uleb128_stream (ob
->main_stream
, node_ref
);
2019 bp
= bitpack_create ();
2020 bp_pack_value (bp
, info
->called_with_var_arguments
, 1);
2021 bp_pack_value (bp
, info
->uses_analysis_done
, 1);
2022 gcc_assert (info
->modification_analysis_done
2023 || ipa_get_param_count (info
) == 0);
2024 gcc_assert (!info
->node_enqueued
);
2025 gcc_assert (!info
->ipcp_orig_node
);
2026 for (j
= 0; j
< ipa_get_param_count (info
); j
++)
2028 bp_pack_value (bp
, info
->params
[j
].modified
, 1);
2029 bp_pack_value (bp
, info
->params
[j
].called
, 1);
2031 lto_output_bitpack (ob
->main_stream
, bp
);
2032 bitpack_delete (bp
);
2033 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2035 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2037 lto_output_uleb128_stream (ob
->main_stream
,
2038 ipa_get_cs_argument_count (args
));
2039 for (j
= 0; j
< ipa_get_cs_argument_count (args
); j
++)
2040 ipa_write_jump_function (ob
, ipa_get_ith_jump_func (args
, j
));
2043 for (note
= info
->param_calls
; note
; note
= note
->next
)
2045 lto_output_uleb128_stream (ob
->main_stream
, note_count
);
2046 for (note
= info
->param_calls
; note
; note
= note
->next
)
2047 ipa_write_param_call_note (ob
, note
);
2050 /* Srtream in NODE info from IB. */
2053 ipa_read_node_info (struct lto_input_block
*ib
, struct cgraph_node
*node
,
2054 struct data_in
*data_in
)
2056 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2058 struct cgraph_edge
*e
;
2059 struct bitpack_d
*bp
;
2062 ipa_initialize_node_params (node
);
2064 bp
= lto_input_bitpack (ib
);
2065 info
->called_with_var_arguments
= bp_unpack_value (bp
, 1);
2066 info
->uses_analysis_done
= bp_unpack_value (bp
, 1);
2067 if (ipa_get_param_count (info
) != 0)
2069 info
->modification_analysis_done
= true;
2070 info
->uses_analysis_done
= true;
2072 info
->node_enqueued
= false;
2073 for (k
= 0; k
< ipa_get_param_count (info
); k
++)
2075 info
->params
[k
].modified
= bp_unpack_value (bp
, 1);
2076 info
->params
[k
].called
= bp_unpack_value (bp
, 1);
2078 bitpack_delete (bp
);
2079 for (e
= node
->callees
; e
; e
= e
->next_callee
)
2081 struct ipa_edge_args
*args
= IPA_EDGE_REF (e
);
2082 int count
= lto_input_uleb128 (ib
);
2084 ipa_set_cs_argument_count (args
, count
);
2088 args
->jump_functions
= GGC_CNEWVEC (struct ipa_jump_func
,
2089 ipa_get_cs_argument_count (args
));
2090 for (k
= 0; k
< ipa_get_cs_argument_count (args
); k
++)
2091 ipa_read_jump_function (ib
, ipa_get_ith_jump_func (args
, k
), data_in
);
2094 note_count
= lto_input_uleb128 (ib
);
2095 for (i
= 0; i
< note_count
; i
++)
2096 ipa_read_param_call_note (ib
, info
);
2099 /* Write jump functions for nodes in SET. */
2102 ipa_prop_write_jump_functions (cgraph_node_set set
)
2104 struct cgraph_node
*node
;
2105 struct output_block
*ob
= create_output_block (LTO_section_jump_functions
);
2106 unsigned int count
= 0;
2107 cgraph_node_set_iterator csi
;
2109 ob
->cgraph_node
= NULL
;
2111 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2113 node
= csi_node (csi
);
2114 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2118 lto_output_uleb128_stream (ob
->main_stream
, count
);
2120 /* Process all of the functions. */
2121 for (csi
= csi_start (set
); !csi_end_p (csi
); csi_next (&csi
))
2123 node
= csi_node (csi
);
2124 if (node
->analyzed
&& IPA_NODE_REF (node
) != NULL
)
2125 ipa_write_node_info (ob
, node
);
2127 lto_output_1_stream (ob
->main_stream
, 0);
2128 produce_asm (ob
, NULL
);
2129 destroy_output_block (ob
);
2132 /* Read section in file FILE_DATA of length LEN with data DATA. */
2135 ipa_prop_read_section (struct lto_file_decl_data
*file_data
, const char *data
,
2138 const struct lto_function_header
*header
=
2139 (const struct lto_function_header
*) data
;
2140 const int32_t cfg_offset
= sizeof (struct lto_function_header
);
2141 const int32_t main_offset
= cfg_offset
+ header
->cfg_size
;
2142 const int32_t string_offset
= main_offset
+ header
->main_size
;
2143 struct data_in
*data_in
;
2144 struct lto_input_block ib_main
;
2148 LTO_INIT_INPUT_BLOCK (ib_main
, (const char *) data
+ main_offset
, 0,
2152 lto_data_in_create (file_data
, (const char *) data
+ string_offset
,
2153 header
->string_size
, NULL
);
2154 count
= lto_input_uleb128 (&ib_main
);
2156 for (i
= 0; i
< count
; i
++)
2159 struct cgraph_node
*node
;
2160 lto_cgraph_encoder_t encoder
;
2162 index
= lto_input_uleb128 (&ib_main
);
2163 encoder
= file_data
->cgraph_node_encoder
;
2164 node
= lto_cgraph_encoder_deref (encoder
, index
);
2165 ipa_read_node_info (&ib_main
, node
, data_in
);
2167 lto_free_section_data (file_data
, LTO_section_jump_functions
, NULL
, data
,
2169 lto_data_in_delete (data_in
);
2172 /* Read ipcp jump functions. */
2175 ipa_prop_read_jump_functions (void)
2177 struct lto_file_decl_data
**file_data_vec
= lto_get_file_decl_data ();
2178 struct lto_file_decl_data
*file_data
;
2181 ipa_check_create_node_params ();
2182 ipa_check_create_edge_args ();
2183 ipa_register_cgraph_hooks ();
2185 while ((file_data
= file_data_vec
[j
++]))
2188 const char *data
= lto_get_section_data (file_data
, LTO_section_jump_functions
, NULL
, &len
);
2191 ipa_prop_read_section (file_data
, data
, len
);
2195 /* After merging units, we can get mismatch in argument counts.
2196 Also decl merging might've rendered parameter lists obsolette.
2197 Also compute called_with_variable_arg info. */
2200 ipa_update_after_lto_read (void)
2202 struct cgraph_node
*node
;
2203 struct cgraph_edge
*cs
;
2205 ipa_check_create_node_params ();
2206 ipa_check_create_edge_args ();
2208 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2210 ipa_initialize_node_params (node
);
2212 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2214 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
2216 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs
))
2217 != ipa_get_param_count (IPA_NODE_REF (cs
->callee
)))
2218 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs
->callee
));
2222 /* Walk param call notes of NODE and set their call statements given the uid
2223 stored in each note and STMTS which is an array of statements indexed by the
2227 lto_ipa_fixup_call_notes (struct cgraph_node
*node
, gimple
*stmts
)
2229 struct ipa_node_params
*info
;
2230 struct ipa_param_call_note
*note
;
2232 ipa_check_create_node_params ();
2233 info
= IPA_NODE_REF (node
);
2234 note
= info
->param_calls
;
2235 /* If there are no notes or they have already been fixed up (the same fixup
2236 is called for both inlining and ipa-cp), there's nothing to do. */
2237 if (!note
|| note
->stmt
)
2242 note
->stmt
= stmts
[note
->lto_stmt_uid
];