1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
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"
37 #include "diagnostic.h"
38 #include "tree-pretty-print.h"
39 #include "gimple-pretty-print.h"
40 #include "lto-streamer.h"
41 #include "data-streamer.h"
42 #include "tree-streamer.h"
45 /* Intermediate information about a parameter that is only useful during the
46 run of ipa_analyze_node and is not kept afterwards. */
48 struct param_analysis_info
51 bitmap visited_statements;
54 /* Vector where the parameter infos are actually stored. */
55 VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
56 /* Vector where the parameter infos are actually stored. */
57 VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
59 /* Holders of ipa cgraph hooks: */
60 static struct cgraph_edge_hook_list *edge_removal_hook_holder;
61 static struct cgraph_node_hook_list *node_removal_hook_holder;
62 static struct cgraph_2edge_hook_list *edge_duplication_hook_holder;
63 static struct cgraph_2node_hook_list *node_duplication_hook_holder;
64 static struct cgraph_node_hook_list *function_insertion_hook_holder;
66 /* Return index of the formal whose tree is PTREE in function which corresponds
70 ipa_get_param_decl_index (struct ipa_node_params *info, tree ptree)
74 count = ipa_get_param_count (info);
75 for (i = 0; i < count; i++)
76 if (ipa_get_param (info, i) == ptree)
82 /* Populate the param_decl field in parameter descriptors of INFO that
83 corresponds to NODE. */
86 ipa_populate_param_decls (struct cgraph_node *node,
87 struct ipa_node_params *info)
95 fnargs = DECL_ARGUMENTS (fndecl);
97 for (parm = fnargs; parm; parm = DECL_CHAIN (parm))
99 VEC_index (ipa_param_descriptor_t,
100 info->descriptors, param_num)->decl = parm;
105 /* Return how many formal parameters FNDECL has. */
108 count_formal_params (tree fndecl)
113 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
119 /* Initialize the ipa_node_params structure associated with NODE by counting
120 the function parameters, creating the descriptors and populating their
124 ipa_initialize_node_params (struct cgraph_node *node)
126 struct ipa_node_params *info = IPA_NODE_REF (node);
128 if (!info->descriptors)
132 param_count = count_formal_params (node->decl);
135 VEC_safe_grow_cleared (ipa_param_descriptor_t, heap,
136 info->descriptors, param_count);
137 ipa_populate_param_decls (node, info);
142 /* Print the jump functions associated with call graph edge CS to file F. */
145 ipa_print_node_jump_functions_for_edge (FILE *f, struct cgraph_edge *cs)
149 count = ipa_get_cs_argument_count (IPA_EDGE_REF (cs));
150 for (i = 0; i < count; i++)
152 struct ipa_jump_func *jump_func;
153 enum jump_func_type type;
155 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
156 type = jump_func->type;
158 fprintf (f, " param %d: ", i);
159 if (type == IPA_JF_UNKNOWN)
160 fprintf (f, "UNKNOWN\n");
161 else if (type == IPA_JF_KNOWN_TYPE)
163 fprintf (f, "KNOWN TYPE: base ");
164 print_generic_expr (f, jump_func->value.known_type.base_type, 0);
165 fprintf (f, ", offset "HOST_WIDE_INT_PRINT_DEC", component ",
166 jump_func->value.known_type.offset);
167 print_generic_expr (f, jump_func->value.known_type.component_type, 0);
170 else if (type == IPA_JF_CONST)
172 tree val = jump_func->value.constant;
173 fprintf (f, "CONST: ");
174 print_generic_expr (f, val, 0);
175 if (TREE_CODE (val) == ADDR_EXPR
176 && TREE_CODE (TREE_OPERAND (val, 0)) == CONST_DECL)
179 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (val, 0)),
184 else if (type == IPA_JF_CONST_MEMBER_PTR)
186 fprintf (f, "CONST MEMBER PTR: ");
187 print_generic_expr (f, jump_func->value.member_cst.pfn, 0);
189 print_generic_expr (f, jump_func->value.member_cst.delta, 0);
192 else if (type == IPA_JF_PASS_THROUGH)
194 fprintf (f, "PASS THROUGH: ");
195 fprintf (f, "%d, op %s ",
196 jump_func->value.pass_through.formal_id,
198 jump_func->value.pass_through.operation]);
199 if (jump_func->value.pass_through.operation != NOP_EXPR)
200 print_generic_expr (f,
201 jump_func->value.pass_through.operand, 0);
204 else if (type == IPA_JF_ANCESTOR)
206 fprintf (f, "ANCESTOR: ");
207 fprintf (f, "%d, offset "HOST_WIDE_INT_PRINT_DEC", ",
208 jump_func->value.ancestor.formal_id,
209 jump_func->value.ancestor.offset);
210 print_generic_expr (f, jump_func->value.ancestor.type, 0);
217 /* Print the jump functions of all arguments on all call graph edges going from
221 ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node)
223 struct cgraph_edge *cs;
226 fprintf (f, " Jump functions of caller %s:\n", cgraph_node_name (node));
227 for (cs = node->callees; cs; cs = cs->next_callee)
229 if (!ipa_edge_args_info_available_for_edge_p (cs))
232 fprintf (f, " callsite %s/%i -> %s/%i : \n",
233 cgraph_node_name (node), node->uid,
234 cgraph_node_name (cs->callee), cs->callee->uid);
235 ipa_print_node_jump_functions_for_edge (f, cs);
238 for (cs = node->indirect_calls, i = 0; cs; cs = cs->next_callee, i++)
240 if (!ipa_edge_args_info_available_for_edge_p (cs))
245 fprintf (f, " indirect callsite %d for stmt ", i);
246 print_gimple_stmt (f, cs->call_stmt, 0, TDF_SLIM);
249 fprintf (f, " indirect callsite %d :\n", i);
250 ipa_print_node_jump_functions_for_edge (f, cs);
255 /* Print ipa_jump_func data structures of all nodes in the call graph to F. */
258 ipa_print_all_jump_functions (FILE *f)
260 struct cgraph_node *node;
262 fprintf (f, "\nJump functions:\n");
263 for (node = cgraph_nodes; node; node = node->next)
265 ipa_print_node_jump_functions (f, node);
269 /* Structure to be passed in between detect_type_change and
270 check_stmt_for_type_change. */
272 struct type_change_info
274 /* Set to true if dynamic type change has been detected. */
275 bool type_maybe_changed;
278 /* Return true if STMT can modify a virtual method table pointer.
280 This function makes special assumptions about both constructors and
281 destructors which are all the functions that are allowed to alter the VMT
282 pointers. It assumes that destructors begin with assignment into all VMT
283 pointers and that constructors essentially look in the following way:
285 1) The very first thing they do is that they call constructors of ancestor
286 sub-objects that have them.
288 2) Then VMT pointers of this and all its ancestors is set to new values
289 corresponding to the type corresponding to the constructor.
291 3) Only afterwards, other stuff such as constructor of member sub-objects
292 and the code written by the user is run. Only this may include calling
293 virtual functions, directly or indirectly.
295 There is no way to call a constructor of an ancestor sub-object in any
298 This means that we do not have to care whether constructors get the correct
299 type information because they will always change it (in fact, if we define
300 the type to be given by the VMT pointer, it is undefined).
302 The most important fact to derive from the above is that if, for some
303 statement in the section 3, we try to detect whether the dynamic type has
304 changed, we can safely ignore all calls as we examine the function body
305 backwards until we reach statements in section 2 because these calls cannot
306 be ancestor constructors or destructors (if the input is not bogus) and so
307 do not change the dynamic type (this holds true only for automatically
308 allocated objects but at the moment we devirtualize only these). We then
309 must detect that statements in section 2 change the dynamic type and can try
310 to derive the new type. That is enough and we can stop, we will never see
311 the calls into constructors of sub-objects in this code. Therefore we can
312 safely ignore all call statements that we traverse.
316 stmt_may_be_vtbl_ptr_store (gimple stmt)
318 if (is_gimple_call (stmt))
320 else if (is_gimple_assign (stmt))
322 tree lhs = gimple_assign_lhs (stmt);
324 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
326 if (flag_strict_aliasing
327 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
330 if (TREE_CODE (lhs) == COMPONENT_REF
331 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
333 /* In the future we might want to use get_base_ref_and_offset to find
334 if there is a field corresponding to the offset and if so, proceed
335 almost like if it was a component ref. */
341 /* Callback of walk_aliased_vdefs and a helper function for
342 detect_type_change to check whether a particular statement may modify
343 the virtual table pointer, and if possible also determine the new type of
344 the (sub-)object. It stores its result into DATA, which points to a
345 type_change_info structure. */
348 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
350 gimple stmt = SSA_NAME_DEF_STMT (vdef);
351 struct type_change_info *tci = (struct type_change_info *) data;
353 if (stmt_may_be_vtbl_ptr_store (stmt))
355 tci->type_maybe_changed = true;
362 /* Detect whether the dynamic type of ARG has changed (before callsite CALL) by
363 looking for assignments to its virtual table pointer. If it is, return true
364 and fill in the jump function JFUNC with relevant type information or set it
365 to unknown. ARG is the object itself (not a pointer to it, unless
366 dereferenced). BASE is the base of the memory access as returned by
367 get_ref_base_and_extent, as is the offset. */
370 detect_type_change (tree arg, tree base, gimple call,
371 struct ipa_jump_func *jfunc, HOST_WIDE_INT offset)
373 struct type_change_info tci;
376 gcc_checking_assert (DECL_P (arg)
377 || TREE_CODE (arg) == MEM_REF
378 || handled_component_p (arg));
379 /* Const calls cannot call virtual methods through VMT and so type changes do
381 if (!flag_devirtualize || !gimple_vuse (call))
384 tci.type_maybe_changed = false;
389 ao.size = POINTER_SIZE;
390 ao.max_size = ao.size;
391 ao.ref_alias_set = -1;
392 ao.base_alias_set = -1;
394 walk_aliased_vdefs (&ao, gimple_vuse (call), check_stmt_for_type_change,
396 if (!tci.type_maybe_changed)
399 jfunc->type = IPA_JF_UNKNOWN;
403 /* Like detect_type_change but ARG is supposed to be a non-dereferenced pointer
404 SSA name (its dereference will become the base and the offset is assumed to
408 detect_type_change_ssa (tree arg, gimple call, struct ipa_jump_func *jfunc)
410 gcc_checking_assert (TREE_CODE (arg) == SSA_NAME);
411 if (!flag_devirtualize
412 || !POINTER_TYPE_P (TREE_TYPE (arg))
413 || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) != RECORD_TYPE)
416 arg = build2 (MEM_REF, ptr_type_node, arg,
417 build_int_cst (ptr_type_node, 0));
419 return detect_type_change (arg, arg, call, jfunc, 0);
422 /* Callback of walk_aliased_vdefs. Flags that it has been invoked to the
423 boolean variable pointed to by DATA. */
426 mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED,
429 bool *b = (bool *) data;
434 /* Return true if the formal parameter PARM might have been modified in this
435 function before reaching the statement STMT. PARM_AINFO is a pointer to a
436 structure containing temporary information about PARM. */
439 is_parm_modified_before_stmt (struct param_analysis_info *parm_ainfo,
440 gimple stmt, tree parm)
442 bool modified = false;
445 if (parm_ainfo->modified)
448 gcc_checking_assert (gimple_vuse (stmt) != NULL_TREE);
449 ao_ref_init (&refd, parm);
450 walk_aliased_vdefs (&refd, gimple_vuse (stmt), mark_modified,
451 &modified, &parm_ainfo->visited_statements);
454 parm_ainfo->modified = true;
460 /* If STMT is an assignment that loads a value from an parameter declaration,
461 return the index of the parameter in ipa_node_params which has not been
462 modified. Otherwise return -1. */
465 load_from_unmodified_param (struct ipa_node_params *info,
466 struct param_analysis_info *parms_ainfo,
472 if (!gimple_assign_single_p (stmt))
475 op1 = gimple_assign_rhs1 (stmt);
476 if (TREE_CODE (op1) != PARM_DECL)
479 index = ipa_get_param_decl_index (info, op1);
481 || is_parm_modified_before_stmt (&parms_ainfo[index], stmt, op1))
487 /* Given that an actual argument is an SSA_NAME (given in NAME) and is a result
488 of an assignment statement STMT, try to determine whether we are actually
489 handling any of the following cases and construct an appropriate jump
490 function into JFUNC if so:
492 1) The passed value is loaded from a formal parameter which is not a gimple
493 register (most probably because it is addressable, the value has to be
494 scalar) and we can guarantee the value has not changed. This case can
495 therefore be described by a simple pass-through jump function. For example:
504 2) The passed value can be described by a simple arithmetic pass-through
511 D.2064_4 = a.1(D) + 4;
514 This case can also occur in combination of the previous one, e.g.:
522 D.2064_4 = a.0_3 + 4;
525 3) The passed value is an address of an object within another one (which
526 also passed by reference). Such situations are described by an ancestor
527 jump function and describe situations such as:
529 B::foo() (struct B * const this)
533 D.1845_2 = &this_1(D)->D.1748;
536 INFO is the structure describing individual parameters access different
537 stages of IPA optimizations. PARMS_AINFO contains the information that is
538 only needed for intraprocedural analysis. */
541 compute_complex_assign_jump_func (struct ipa_node_params *info,
542 struct param_analysis_info *parms_ainfo,
543 struct ipa_jump_func *jfunc,
544 gimple call, gimple stmt, tree name)
546 HOST_WIDE_INT offset, size, max_size;
547 tree op1, tc_ssa, base, ssa;
550 op1 = gimple_assign_rhs1 (stmt);
552 if (TREE_CODE (op1) == SSA_NAME)
554 if (SSA_NAME_IS_DEFAULT_DEF (op1))
555 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (op1));
557 index = load_from_unmodified_param (info, parms_ainfo,
558 SSA_NAME_DEF_STMT (op1));
563 index = load_from_unmodified_param (info, parms_ainfo, stmt);
564 tc_ssa = gimple_assign_lhs (stmt);
569 tree op2 = gimple_assign_rhs2 (stmt);
573 if (!is_gimple_ip_invariant (op2)
574 || (TREE_CODE_CLASS (gimple_expr_code (stmt)) != tcc_comparison
575 && !useless_type_conversion_p (TREE_TYPE (name),
579 jfunc->type = IPA_JF_PASS_THROUGH;
580 jfunc->value.pass_through.formal_id = index;
581 jfunc->value.pass_through.operation = gimple_assign_rhs_code (stmt);
582 jfunc->value.pass_through.operand = op2;
584 else if (gimple_assign_single_p (stmt)
585 && !detect_type_change_ssa (tc_ssa, call, jfunc))
587 jfunc->type = IPA_JF_PASS_THROUGH;
588 jfunc->value.pass_through.formal_id = index;
589 jfunc->value.pass_through.operation = NOP_EXPR;
594 if (TREE_CODE (op1) != ADDR_EXPR)
596 op1 = TREE_OPERAND (op1, 0);
597 if (TREE_CODE (TREE_TYPE (op1)) != RECORD_TYPE)
599 base = get_ref_base_and_extent (op1, &offset, &size, &max_size);
600 if (TREE_CODE (base) != MEM_REF
601 /* If this is a varying address, punt. */
605 offset += mem_ref_offset (base).low * BITS_PER_UNIT;
606 ssa = TREE_OPERAND (base, 0);
607 if (TREE_CODE (ssa) != SSA_NAME
608 || !SSA_NAME_IS_DEFAULT_DEF (ssa)
612 /* Dynamic types are changed only in constructors and destructors and */
613 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (ssa));
615 && !detect_type_change (op1, base, call, jfunc, offset))
617 jfunc->type = IPA_JF_ANCESTOR;
618 jfunc->value.ancestor.formal_id = index;
619 jfunc->value.ancestor.offset = offset;
620 jfunc->value.ancestor.type = TREE_TYPE (op1);
624 /* Extract the base, offset and MEM_REF expression from a statement ASSIGN if
627 iftmp.1_3 = &obj_2(D)->D.1762;
629 The base of the MEM_REF must be a default definition SSA NAME of a
630 parameter. Return NULL_TREE if it looks otherwise. If case of success, the
631 whole MEM_REF expression is returned and the offset calculated from any
632 handled components and the MEM_REF itself is stored into *OFFSET. The whole
633 RHS stripped off the ADDR_EXPR is stored into *OBJ_P. */
636 get_ancestor_addr_info (gimple assign, tree *obj_p, HOST_WIDE_INT *offset)
638 HOST_WIDE_INT size, max_size;
639 tree expr, parm, obj;
641 if (!gimple_assign_single_p (assign))
643 expr = gimple_assign_rhs1 (assign);
645 if (TREE_CODE (expr) != ADDR_EXPR)
647 expr = TREE_OPERAND (expr, 0);
649 expr = get_ref_base_and_extent (expr, offset, &size, &max_size);
651 if (TREE_CODE (expr) != MEM_REF
652 /* If this is a varying address, punt. */
657 parm = TREE_OPERAND (expr, 0);
658 if (TREE_CODE (parm) != SSA_NAME
659 || !SSA_NAME_IS_DEFAULT_DEF (parm)
660 || TREE_CODE (SSA_NAME_VAR (parm)) != PARM_DECL)
663 *offset += mem_ref_offset (expr).low * BITS_PER_UNIT;
669 /* Given that an actual argument is an SSA_NAME that is a result of a phi
670 statement PHI, try to find out whether NAME is in fact a
671 multiple-inheritance typecast from a descendant into an ancestor of a formal
672 parameter and thus can be described by an ancestor jump function and if so,
673 write the appropriate function into JFUNC.
675 Essentially we want to match the following pattern:
683 iftmp.1_3 = &obj_2(D)->D.1762;
686 # iftmp.1_1 = PHI <iftmp.1_3(3), 0B(2)>
687 D.1879_6 = middleman_1 (iftmp.1_1, i_5(D));
691 compute_complex_ancestor_jump_func (struct ipa_node_params *info,
692 struct ipa_jump_func *jfunc,
693 gimple call, gimple phi)
695 HOST_WIDE_INT offset;
697 basic_block phi_bb, assign_bb, cond_bb;
698 tree tmp, parm, expr, obj;
701 if (gimple_phi_num_args (phi) != 2)
704 if (integer_zerop (PHI_ARG_DEF (phi, 1)))
705 tmp = PHI_ARG_DEF (phi, 0);
706 else if (integer_zerop (PHI_ARG_DEF (phi, 0)))
707 tmp = PHI_ARG_DEF (phi, 1);
710 if (TREE_CODE (tmp) != SSA_NAME
711 || SSA_NAME_IS_DEFAULT_DEF (tmp)
712 || !POINTER_TYPE_P (TREE_TYPE (tmp))
713 || TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) != RECORD_TYPE)
716 assign = SSA_NAME_DEF_STMT (tmp);
717 assign_bb = gimple_bb (assign);
718 if (!single_pred_p (assign_bb))
720 expr = get_ancestor_addr_info (assign, &obj, &offset);
723 parm = TREE_OPERAND (expr, 0);
724 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (parm));
725 gcc_assert (index >= 0);
727 cond_bb = single_pred (assign_bb);
728 cond = last_stmt (cond_bb);
730 || gimple_code (cond) != GIMPLE_COND
731 || gimple_cond_code (cond) != NE_EXPR
732 || gimple_cond_lhs (cond) != parm
733 || !integer_zerop (gimple_cond_rhs (cond)))
736 phi_bb = gimple_bb (phi);
737 for (i = 0; i < 2; i++)
739 basic_block pred = EDGE_PRED (phi_bb, i)->src;
740 if (pred != assign_bb && pred != cond_bb)
744 if (!detect_type_change (obj, expr, call, jfunc, offset))
746 jfunc->type = IPA_JF_ANCESTOR;
747 jfunc->value.ancestor.formal_id = index;
748 jfunc->value.ancestor.offset = offset;
749 jfunc->value.ancestor.type = TREE_TYPE (obj);
753 /* Given OP which is passed as an actual argument to a called function,
754 determine if it is possible to construct a KNOWN_TYPE jump function for it
755 and if so, create one and store it to JFUNC. */
758 compute_known_type_jump_func (tree op, struct ipa_jump_func *jfunc,
761 HOST_WIDE_INT offset, size, max_size;
764 if (!flag_devirtualize
765 || TREE_CODE (op) != ADDR_EXPR
766 || TREE_CODE (TREE_TYPE (TREE_TYPE (op))) != RECORD_TYPE)
769 op = TREE_OPERAND (op, 0);
770 base = get_ref_base_and_extent (op, &offset, &size, &max_size);
774 || TREE_CODE (TREE_TYPE (base)) != RECORD_TYPE
775 || is_global_var (base))
778 if (detect_type_change (op, base, call, jfunc, offset)
779 || !TYPE_BINFO (TREE_TYPE (base)))
782 jfunc->type = IPA_JF_KNOWN_TYPE;
783 jfunc->value.known_type.base_type = TREE_TYPE (base);
784 jfunc->value.known_type.offset = offset;
785 jfunc->value.known_type.component_type = TREE_TYPE (op);
789 /* Determine the jump functions of scalar arguments. Scalar means SSA names
790 and constants of a number of selected types. INFO is the ipa_node_params
791 structure associated with the caller, PARMS_AINFO describes state of
792 analysis with respect to individual formal parameters. ARGS is the
793 ipa_edge_args structure describing the callsite CALL which is the call
794 statement being examined.*/
797 compute_scalar_jump_functions (struct ipa_node_params *info,
798 struct param_analysis_info *parms_ainfo,
799 struct ipa_edge_args *args,
805 for (num = 0; num < gimple_call_num_args (call); num++)
807 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
808 arg = gimple_call_arg (call, num);
810 if (is_gimple_ip_invariant (arg))
812 jfunc->type = IPA_JF_CONST;
813 jfunc->value.constant = arg;
815 else if (TREE_CODE (arg) == SSA_NAME)
817 if (SSA_NAME_IS_DEFAULT_DEF (arg))
819 int index = ipa_get_param_decl_index (info, SSA_NAME_VAR (arg));
822 && !detect_type_change_ssa (arg, call, jfunc))
824 jfunc->type = IPA_JF_PASS_THROUGH;
825 jfunc->value.pass_through.formal_id = index;
826 jfunc->value.pass_through.operation = NOP_EXPR;
831 gimple stmt = SSA_NAME_DEF_STMT (arg);
832 if (is_gimple_assign (stmt))
833 compute_complex_assign_jump_func (info, parms_ainfo, jfunc,
835 else if (gimple_code (stmt) == GIMPLE_PHI)
836 compute_complex_ancestor_jump_func (info, jfunc, call, stmt);
840 compute_known_type_jump_func (arg, jfunc, call);
844 /* Inspect the given TYPE and return true iff it has the same structure (the
845 same number of fields of the same types) as a C++ member pointer. If
846 METHOD_PTR and DELTA are non-NULL, store the trees representing the
847 corresponding fields there. */
850 type_like_member_ptr_p (tree type, tree *method_ptr, tree *delta)
854 if (TREE_CODE (type) != RECORD_TYPE)
857 fld = TYPE_FIELDS (type);
858 if (!fld || !POINTER_TYPE_P (TREE_TYPE (fld))
859 || TREE_CODE (TREE_TYPE (TREE_TYPE (fld))) != METHOD_TYPE)
865 fld = DECL_CHAIN (fld);
866 if (!fld || INTEGRAL_TYPE_P (fld))
871 if (DECL_CHAIN (fld))
877 /* Go through arguments of the CALL and for every one that looks like a member
878 pointer, check whether it can be safely declared pass-through and if so,
879 mark that to the corresponding item of jump FUNCTIONS. Return true iff
880 there are non-pass-through member pointers within the arguments. INFO
881 describes formal parameters of the caller. PARMS_INFO is a pointer to a
882 vector containing intermediate information about each formal parameter. */
885 compute_pass_through_member_ptrs (struct ipa_node_params *info,
886 struct param_analysis_info *parms_ainfo,
887 struct ipa_edge_args *args,
890 bool undecided_members = false;
894 for (num = 0; num < gimple_call_num_args (call); num++)
896 arg = gimple_call_arg (call, num);
898 if (type_like_member_ptr_p (TREE_TYPE (arg), NULL, NULL))
900 if (TREE_CODE (arg) == PARM_DECL)
902 int index = ipa_get_param_decl_index (info, arg);
904 gcc_assert (index >=0);
905 if (!is_parm_modified_before_stmt (&parms_ainfo[index], call,
908 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args,
910 jfunc->type = IPA_JF_PASS_THROUGH;
911 jfunc->value.pass_through.formal_id = index;
912 jfunc->value.pass_through.operation = NOP_EXPR;
915 undecided_members = true;
918 undecided_members = true;
922 return undecided_members;
925 /* Simple function filling in a member pointer constant jump function (with PFN
926 and DELTA as the constant value) into JFUNC. */
929 fill_member_ptr_cst_jump_function (struct ipa_jump_func *jfunc,
930 tree pfn, tree delta)
932 jfunc->type = IPA_JF_CONST_MEMBER_PTR;
933 jfunc->value.member_cst.pfn = pfn;
934 jfunc->value.member_cst.delta = delta;
937 /* If RHS is an SSA_NAME and it is defined by a simple copy assign statement,
938 return the rhs of its defining statement. */
941 get_ssa_def_if_simple_copy (tree rhs)
943 while (TREE_CODE (rhs) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (rhs))
945 gimple def_stmt = SSA_NAME_DEF_STMT (rhs);
947 if (gimple_assign_single_p (def_stmt))
948 rhs = gimple_assign_rhs1 (def_stmt);
955 /* Traverse statements from CALL backwards, scanning whether the argument ARG
956 which is a member pointer is filled in with constant values. If it is, fill
957 the jump function JFUNC in appropriately. METHOD_FIELD and DELTA_FIELD are
958 fields of the record type of the member pointer. To give an example, we
959 look for a pattern looking like the following:
961 D.2515.__pfn ={v} printStuff;
962 D.2515.__delta ={v} 0;
963 i_1 = doprinting (D.2515); */
966 determine_cst_member_ptr (gimple call, tree arg, tree method_field,
967 tree delta_field, struct ipa_jump_func *jfunc)
969 gimple_stmt_iterator gsi;
970 tree method = NULL_TREE;
971 tree delta = NULL_TREE;
973 gsi = gsi_for_stmt (call);
976 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
978 gimple stmt = gsi_stmt (gsi);
981 if (!stmt_may_clobber_ref_p (stmt, arg))
983 if (!gimple_assign_single_p (stmt))
986 lhs = gimple_assign_lhs (stmt);
987 rhs = gimple_assign_rhs1 (stmt);
989 if (TREE_CODE (lhs) != COMPONENT_REF
990 || TREE_OPERAND (lhs, 0) != arg)
993 fld = TREE_OPERAND (lhs, 1);
994 if (!method && fld == method_field)
996 rhs = get_ssa_def_if_simple_copy (rhs);
997 if (TREE_CODE (rhs) == ADDR_EXPR
998 && TREE_CODE (TREE_OPERAND (rhs, 0)) == FUNCTION_DECL
999 && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) == METHOD_TYPE)
1001 method = TREE_OPERAND (rhs, 0);
1004 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
1012 if (!delta && fld == delta_field)
1014 rhs = get_ssa_def_if_simple_copy (rhs);
1015 if (TREE_CODE (rhs) == INTEGER_CST)
1020 fill_member_ptr_cst_jump_function (jfunc, rhs, delta);
1032 /* Go through the arguments of the CALL and for every member pointer within
1033 tries determine whether it is a constant. If it is, create a corresponding
1034 constant jump function in FUNCTIONS which is an array of jump functions
1035 associated with the call. */
1038 compute_cst_member_ptr_arguments (struct ipa_edge_args *args,
1042 tree arg, method_field, delta_field;
1044 for (num = 0; num < gimple_call_num_args (call); num++)
1046 struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, num);
1047 arg = gimple_call_arg (call, num);
1049 if (jfunc->type == IPA_JF_UNKNOWN
1050 && type_like_member_ptr_p (TREE_TYPE (arg), &method_field,
1052 determine_cst_member_ptr (call, arg, method_field, delta_field, jfunc);
1056 /* Compute jump function for all arguments of callsite CS and insert the
1057 information in the jump_functions array in the ipa_edge_args corresponding
1058 to this callsite. */
1061 ipa_compute_jump_functions_for_edge (struct param_analysis_info *parms_ainfo,
1062 struct cgraph_edge *cs)
1064 struct ipa_node_params *info = IPA_NODE_REF (cs->caller);
1065 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
1066 gimple call = cs->call_stmt;
1067 int arg_num = gimple_call_num_args (call);
1069 if (arg_num == 0 || args->jump_functions)
1071 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, arg_num);
1073 /* We will deal with constants and SSA scalars first: */
1074 compute_scalar_jump_functions (info, parms_ainfo, args, call);
1076 /* Let's check whether there are any potential member pointers and if so,
1077 whether we can determine their functions as pass_through. */
1078 if (!compute_pass_through_member_ptrs (info, parms_ainfo, args, call))
1081 /* Finally, let's check whether we actually pass a new constant member
1083 compute_cst_member_ptr_arguments (args, call);
1086 /* Compute jump functions for all edges - both direct and indirect - outgoing
1087 from NODE. Also count the actual arguments in the process. */
1090 ipa_compute_jump_functions (struct cgraph_node *node,
1091 struct param_analysis_info *parms_ainfo)
1093 struct cgraph_edge *cs;
1095 for (cs = node->callees; cs; cs = cs->next_callee)
1097 struct cgraph_node *callee = cgraph_function_or_thunk_node (cs->callee,
1099 /* We do not need to bother analyzing calls to unknown
1100 functions unless they may become known during lto/whopr. */
1101 if (!callee->analyzed && !flag_lto)
1103 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1106 for (cs = node->indirect_calls; cs; cs = cs->next_callee)
1107 ipa_compute_jump_functions_for_edge (parms_ainfo, cs);
1110 /* If RHS looks like a rhs of a statement loading pfn from a member
1111 pointer formal parameter, return the parameter, otherwise return
1112 NULL. If USE_DELTA, then we look for a use of the delta field
1113 rather than the pfn. */
1116 ipa_get_member_ptr_load_param (tree rhs, bool use_delta)
1118 tree rec, ref_field, ref_offset, fld, fld_offset, ptr_field, delta_field;
1120 if (TREE_CODE (rhs) == COMPONENT_REF)
1122 ref_field = TREE_OPERAND (rhs, 1);
1123 rhs = TREE_OPERAND (rhs, 0);
1126 ref_field = NULL_TREE;
1127 if (TREE_CODE (rhs) != MEM_REF)
1129 rec = TREE_OPERAND (rhs, 0);
1130 if (TREE_CODE (rec) != ADDR_EXPR)
1132 rec = TREE_OPERAND (rec, 0);
1133 if (TREE_CODE (rec) != PARM_DECL
1134 || !type_like_member_ptr_p (TREE_TYPE (rec), &ptr_field, &delta_field))
1137 ref_offset = TREE_OPERAND (rhs, 1);
1141 if (integer_nonzerop (ref_offset))
1149 return ref_field == fld ? rec : NULL_TREE;
1153 fld_offset = byte_position (delta_field);
1155 fld_offset = byte_position (ptr_field);
1157 return tree_int_cst_equal (ref_offset, fld_offset) ? rec : NULL_TREE;
1160 /* If STMT looks like a statement loading a value from a member pointer formal
1161 parameter, this function returns that parameter. */
1164 ipa_get_stmt_member_ptr_load_param (gimple stmt, bool use_delta)
1168 if (!gimple_assign_single_p (stmt))
1171 rhs = gimple_assign_rhs1 (stmt);
1172 return ipa_get_member_ptr_load_param (rhs, use_delta);
1175 /* Returns true iff T is an SSA_NAME defined by a statement. */
1178 ipa_is_ssa_with_stmt_def (tree t)
1180 if (TREE_CODE (t) == SSA_NAME
1181 && !SSA_NAME_IS_DEFAULT_DEF (t))
1187 /* Find the indirect call graph edge corresponding to STMT and mark it as a
1188 call to a parameter number PARAM_INDEX. NODE is the caller. Return the
1189 indirect call graph edge. */
1191 static struct cgraph_edge *
1192 ipa_note_param_call (struct cgraph_node *node, int param_index, gimple stmt)
1194 struct cgraph_edge *cs;
1196 cs = cgraph_edge (node, stmt);
1197 cs->indirect_info->param_index = param_index;
1198 cs->indirect_info->anc_offset = 0;
1199 cs->indirect_info->polymorphic = 0;
1203 /* Analyze the CALL and examine uses of formal parameters of the caller NODE
1204 (described by INFO). PARMS_AINFO is a pointer to a vector containing
1205 intermediate information about each formal parameter. Currently it checks
1206 whether the call calls a pointer that is a formal parameter and if so, the
1207 parameter is marked with the called flag and an indirect call graph edge
1208 describing the call is created. This is very simple for ordinary pointers
1209 represented in SSA but not-so-nice when it comes to member pointers. The
1210 ugly part of this function does nothing more than trying to match the
1211 pattern of such a call. An example of such a pattern is the gimple dump
1212 below, the call is on the last line:
1215 f$__delta_5 = f.__delta;
1216 f$__pfn_24 = f.__pfn;
1220 f$__delta_5 = MEM[(struct *)&f];
1221 f$__pfn_24 = MEM[(struct *)&f + 4B];
1223 and a few lines below:
1226 D.2496_3 = (int) f$__pfn_24;
1227 D.2497_4 = D.2496_3 & 1;
1234 D.2500_7 = (unsigned int) f$__delta_5;
1235 D.2501_8 = &S + D.2500_7;
1236 D.2502_9 = (int (*__vtbl_ptr_type) (void) * *) D.2501_8;
1237 D.2503_10 = *D.2502_9;
1238 D.2504_12 = f$__pfn_24 + -1;
1239 D.2505_13 = (unsigned int) D.2504_12;
1240 D.2506_14 = D.2503_10 + D.2505_13;
1241 D.2507_15 = *D.2506_14;
1242 iftmp.11_16 = (String:: *) D.2507_15;
1245 # iftmp.11_1 = PHI <iftmp.11_16(3), f$__pfn_24(2)>
1246 D.2500_19 = (unsigned int) f$__delta_5;
1247 D.2508_20 = &S + D.2500_19;
1248 D.2493_21 = iftmp.11_1 (D.2508_20, 4);
1250 Such patterns are results of simple calls to a member pointer:
1252 int doprinting (int (MyString::* f)(int) const)
1254 MyString S ("somestring");
1261 ipa_analyze_indirect_call_uses (struct cgraph_node *node,
1262 struct ipa_node_params *info,
1263 struct param_analysis_info *parms_ainfo,
1264 gimple call, tree target)
1269 tree rec, rec2, cond;
1272 basic_block bb, virt_bb, join;
1274 if (SSA_NAME_IS_DEFAULT_DEF (target))
1276 tree var = SSA_NAME_VAR (target);
1277 index = ipa_get_param_decl_index (info, var);
1279 ipa_note_param_call (node, index, call);
1283 /* Now we need to try to match the complex pattern of calling a member
1286 if (!POINTER_TYPE_P (TREE_TYPE (target))
1287 || TREE_CODE (TREE_TYPE (TREE_TYPE (target))) != METHOD_TYPE)
1290 def = SSA_NAME_DEF_STMT (target);
1291 if (gimple_code (def) != GIMPLE_PHI)
1294 if (gimple_phi_num_args (def) != 2)
1297 /* First, we need to check whether one of these is a load from a member
1298 pointer that is a parameter to this function. */
1299 n1 = PHI_ARG_DEF (def, 0);
1300 n2 = PHI_ARG_DEF (def, 1);
1301 if (!ipa_is_ssa_with_stmt_def (n1) || !ipa_is_ssa_with_stmt_def (n2))
1303 d1 = SSA_NAME_DEF_STMT (n1);
1304 d2 = SSA_NAME_DEF_STMT (n2);
1306 join = gimple_bb (def);
1307 if ((rec = ipa_get_stmt_member_ptr_load_param (d1, false)))
1309 if (ipa_get_stmt_member_ptr_load_param (d2, false))
1312 bb = EDGE_PRED (join, 0)->src;
1313 virt_bb = gimple_bb (d2);
1315 else if ((rec = ipa_get_stmt_member_ptr_load_param (d2, false)))
1317 bb = EDGE_PRED (join, 1)->src;
1318 virt_bb = gimple_bb (d1);
1323 /* Second, we need to check that the basic blocks are laid out in the way
1324 corresponding to the pattern. */
1326 if (!single_pred_p (virt_bb) || !single_succ_p (virt_bb)
1327 || single_pred (virt_bb) != bb
1328 || single_succ (virt_bb) != join)
1331 /* Third, let's see that the branching is done depending on the least
1332 significant bit of the pfn. */
1334 branch = last_stmt (bb);
1335 if (!branch || gimple_code (branch) != GIMPLE_COND)
1338 if ((gimple_cond_code (branch) != NE_EXPR
1339 && gimple_cond_code (branch) != EQ_EXPR)
1340 || !integer_zerop (gimple_cond_rhs (branch)))
1343 cond = gimple_cond_lhs (branch);
1344 if (!ipa_is_ssa_with_stmt_def (cond))
1347 def = SSA_NAME_DEF_STMT (cond);
1348 if (!is_gimple_assign (def)
1349 || gimple_assign_rhs_code (def) != BIT_AND_EXPR
1350 || !integer_onep (gimple_assign_rhs2 (def)))
1353 cond = gimple_assign_rhs1 (def);
1354 if (!ipa_is_ssa_with_stmt_def (cond))
1357 def = SSA_NAME_DEF_STMT (cond);
1359 if (is_gimple_assign (def)
1360 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
1362 cond = gimple_assign_rhs1 (def);
1363 if (!ipa_is_ssa_with_stmt_def (cond))
1365 def = SSA_NAME_DEF_STMT (cond);
1368 rec2 = ipa_get_stmt_member_ptr_load_param (def,
1369 (TARGET_PTRMEMFUNC_VBIT_LOCATION
1370 == ptrmemfunc_vbit_in_delta));
1375 index = ipa_get_param_decl_index (info, rec);
1376 if (index >= 0 && !is_parm_modified_before_stmt (&parms_ainfo[index],
1378 ipa_note_param_call (node, index, call);
1383 /* Analyze a CALL to an OBJ_TYPE_REF which is passed in TARGET and if the
1384 object referenced in the expression is a formal parameter of the caller
1385 (described by INFO), create a call note for the statement. */
1388 ipa_analyze_virtual_call_uses (struct cgraph_node *node,
1389 struct ipa_node_params *info, gimple call,
1392 struct cgraph_edge *cs;
1393 struct cgraph_indirect_call_info *ii;
1394 struct ipa_jump_func jfunc;
1395 tree obj = OBJ_TYPE_REF_OBJECT (target);
1397 HOST_WIDE_INT anc_offset;
1399 if (!flag_devirtualize)
1402 if (TREE_CODE (obj) != SSA_NAME)
1405 if (SSA_NAME_IS_DEFAULT_DEF (obj))
1407 if (TREE_CODE (SSA_NAME_VAR (obj)) != PARM_DECL)
1411 index = ipa_get_param_decl_index (info, SSA_NAME_VAR (obj));
1412 gcc_assert (index >= 0);
1413 if (detect_type_change_ssa (obj, call, &jfunc))
1418 gimple stmt = SSA_NAME_DEF_STMT (obj);
1421 expr = get_ancestor_addr_info (stmt, &obj, &anc_offset);
1424 index = ipa_get_param_decl_index (info,
1425 SSA_NAME_VAR (TREE_OPERAND (expr, 0)));
1426 gcc_assert (index >= 0);
1427 if (detect_type_change (obj, expr, call, &jfunc, anc_offset))
1431 cs = ipa_note_param_call (node, index, call);
1432 ii = cs->indirect_info;
1433 ii->anc_offset = anc_offset;
1434 ii->otr_token = tree_low_cst (OBJ_TYPE_REF_TOKEN (target), 1);
1435 ii->otr_type = TREE_TYPE (TREE_TYPE (OBJ_TYPE_REF_OBJECT (target)));
1436 ii->polymorphic = 1;
1439 /* Analyze a call statement CALL whether and how it utilizes formal parameters
1440 of the caller (described by INFO). PARMS_AINFO is a pointer to a vector
1441 containing intermediate information about each formal parameter. */
1444 ipa_analyze_call_uses (struct cgraph_node *node,
1445 struct ipa_node_params *info,
1446 struct param_analysis_info *parms_ainfo, gimple call)
1448 tree target = gimple_call_fn (call);
1452 if (TREE_CODE (target) == SSA_NAME)
1453 ipa_analyze_indirect_call_uses (node, info, parms_ainfo, call, target);
1454 else if (TREE_CODE (target) == OBJ_TYPE_REF)
1455 ipa_analyze_virtual_call_uses (node, info, call, target);
1459 /* Analyze the call statement STMT with respect to formal parameters (described
1460 in INFO) of caller given by NODE. Currently it only checks whether formal
1461 parameters are called. PARMS_AINFO is a pointer to a vector containing
1462 intermediate information about each formal parameter. */
1465 ipa_analyze_stmt_uses (struct cgraph_node *node, struct ipa_node_params *info,
1466 struct param_analysis_info *parms_ainfo, gimple stmt)
1468 if (is_gimple_call (stmt))
1469 ipa_analyze_call_uses (node, info, parms_ainfo, stmt);
1472 /* Callback of walk_stmt_load_store_addr_ops for the visit_load.
1473 If OP is a parameter declaration, mark it as used in the info structure
1477 visit_ref_for_mod_analysis (gimple stmt ATTRIBUTE_UNUSED,
1478 tree op, void *data)
1480 struct ipa_node_params *info = (struct ipa_node_params *) data;
1482 op = get_base_address (op);
1484 && TREE_CODE (op) == PARM_DECL)
1486 int index = ipa_get_param_decl_index (info, op);
1487 gcc_assert (index >= 0);
1488 ipa_set_param_used (info, index, true);
1494 /* Scan the function body of NODE and inspect the uses of formal parameters.
1495 Store the findings in various structures of the associated ipa_node_params
1496 structure, such as parameter flags, notes etc. PARMS_AINFO is a pointer to a
1497 vector containing intermediate information about each formal parameter. */
1500 ipa_analyze_params_uses (struct cgraph_node *node,
1501 struct param_analysis_info *parms_ainfo)
1503 tree decl = node->decl;
1505 struct function *func;
1506 gimple_stmt_iterator gsi;
1507 struct ipa_node_params *info = IPA_NODE_REF (node);
1510 if (ipa_get_param_count (info) == 0 || info->uses_analysis_done)
1513 for (i = 0; i < ipa_get_param_count (info); i++)
1515 tree parm = ipa_get_param (info, i);
1516 /* For SSA regs see if parameter is used. For non-SSA we compute
1517 the flag during modification analysis. */
1518 if (is_gimple_reg (parm)
1519 && gimple_default_def (DECL_STRUCT_FUNCTION (node->decl), parm))
1520 ipa_set_param_used (info, i, true);
1523 func = DECL_STRUCT_FUNCTION (decl);
1524 FOR_EACH_BB_FN (bb, func)
1526 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1528 gimple stmt = gsi_stmt (gsi);
1530 if (is_gimple_debug (stmt))
1533 ipa_analyze_stmt_uses (node, info, parms_ainfo, stmt);
1534 walk_stmt_load_store_addr_ops (stmt, info,
1535 visit_ref_for_mod_analysis,
1536 visit_ref_for_mod_analysis,
1537 visit_ref_for_mod_analysis);
1539 for (gsi = gsi_start (phi_nodes (bb)); !gsi_end_p (gsi); gsi_next (&gsi))
1540 walk_stmt_load_store_addr_ops (gsi_stmt (gsi), info,
1541 visit_ref_for_mod_analysis,
1542 visit_ref_for_mod_analysis,
1543 visit_ref_for_mod_analysis);
1546 info->uses_analysis_done = 1;
1549 /* Initialize the array describing properties of of formal parameters
1550 of NODE, analyze their uses and compute jump functions associated
1551 with actual arguments of calls from within NODE. */
1554 ipa_analyze_node (struct cgraph_node *node)
1556 struct ipa_node_params *info;
1557 struct param_analysis_info *parms_ainfo;
1560 ipa_check_create_node_params ();
1561 ipa_check_create_edge_args ();
1562 info = IPA_NODE_REF (node);
1563 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1564 current_function_decl = node->decl;
1565 ipa_initialize_node_params (node);
1567 param_count = ipa_get_param_count (info);
1568 parms_ainfo = XALLOCAVEC (struct param_analysis_info, param_count);
1569 memset (parms_ainfo, 0, sizeof (struct param_analysis_info) * param_count);
1571 ipa_analyze_params_uses (node, parms_ainfo);
1572 ipa_compute_jump_functions (node, parms_ainfo);
1574 for (i = 0; i < param_count; i++)
1575 if (parms_ainfo[i].visited_statements)
1576 BITMAP_FREE (parms_ainfo[i].visited_statements);
1578 current_function_decl = NULL;
1583 /* Update the jump function DST when the call graph edge corresponding to SRC is
1584 is being inlined, knowing that DST is of type ancestor and src of known
1588 combine_known_type_and_ancestor_jfs (struct ipa_jump_func *src,
1589 struct ipa_jump_func *dst)
1591 HOST_WIDE_INT combined_offset;
1594 combined_offset = src->value.known_type.offset + dst->value.ancestor.offset;
1595 combined_type = dst->value.ancestor.type;
1597 dst->type = IPA_JF_KNOWN_TYPE;
1598 dst->value.known_type.base_type = src->value.known_type.base_type;
1599 dst->value.known_type.offset = combined_offset;
1600 dst->value.known_type.component_type = combined_type;
1603 /* Update the jump functions associated with call graph edge E when the call
1604 graph edge CS is being inlined, assuming that E->caller is already (possibly
1605 indirectly) inlined into CS->callee and that E has not been inlined. */
1608 update_jump_functions_after_inlining (struct cgraph_edge *cs,
1609 struct cgraph_edge *e)
1611 struct ipa_edge_args *top = IPA_EDGE_REF (cs);
1612 struct ipa_edge_args *args = IPA_EDGE_REF (e);
1613 int count = ipa_get_cs_argument_count (args);
1616 for (i = 0; i < count; i++)
1618 struct ipa_jump_func *dst = ipa_get_ith_jump_func (args, i);
1620 if (dst->type == IPA_JF_ANCESTOR)
1622 struct ipa_jump_func *src;
1624 /* Variable number of arguments can cause havoc if we try to access
1625 one that does not exist in the inlined edge. So make sure we
1627 if (dst->value.ancestor.formal_id >= ipa_get_cs_argument_count (top))
1629 dst->type = IPA_JF_UNKNOWN;
1633 src = ipa_get_ith_jump_func (top, dst->value.ancestor.formal_id);
1634 if (src->type == IPA_JF_KNOWN_TYPE)
1635 combine_known_type_and_ancestor_jfs (src, dst);
1636 else if (src->type == IPA_JF_PASS_THROUGH
1637 && src->value.pass_through.operation == NOP_EXPR)
1638 dst->value.ancestor.formal_id = src->value.pass_through.formal_id;
1639 else if (src->type == IPA_JF_ANCESTOR)
1641 dst->value.ancestor.formal_id = src->value.ancestor.formal_id;
1642 dst->value.ancestor.offset += src->value.ancestor.offset;
1645 dst->type = IPA_JF_UNKNOWN;
1647 else if (dst->type == IPA_JF_PASS_THROUGH)
1649 struct ipa_jump_func *src;
1650 /* We must check range due to calls with variable number of arguments
1651 and we cannot combine jump functions with operations. */
1652 if (dst->value.pass_through.operation == NOP_EXPR
1653 && (dst->value.pass_through.formal_id
1654 < ipa_get_cs_argument_count (top)))
1656 src = ipa_get_ith_jump_func (top,
1657 dst->value.pass_through.formal_id);
1661 dst->type = IPA_JF_UNKNOWN;
1666 /* If TARGET is an addr_expr of a function declaration, make it the destination
1667 of an indirect edge IE and return the edge. Otherwise, return NULL. */
1669 struct cgraph_edge *
1670 ipa_make_edge_direct_to_target (struct cgraph_edge *ie, tree target)
1672 struct cgraph_node *callee;
1674 if (TREE_CODE (target) == ADDR_EXPR)
1675 target = TREE_OPERAND (target, 0);
1676 if (TREE_CODE (target) != FUNCTION_DECL)
1678 callee = cgraph_get_node (target);
1681 ipa_check_create_node_params ();
1683 /* We can not make edges to inline clones. It is bug that someone removed
1684 the cgraph node too early. */
1685 gcc_assert (!callee->global.inlined_to);
1687 cgraph_make_edge_direct (ie, callee);
1690 fprintf (dump_file, "ipa-prop: Discovered %s call to a known target "
1691 "(%s/%i -> %s/%i), for stmt ",
1692 ie->indirect_info->polymorphic ? "a virtual" : "an indirect",
1693 cgraph_node_name (ie->caller), ie->caller->uid,
1694 cgraph_node_name (ie->callee), ie->callee->uid);
1696 print_gimple_stmt (dump_file, ie->call_stmt, 2, TDF_SLIM);
1698 fprintf (dump_file, "with uid %i\n", ie->lto_stmt_uid);
1700 callee = cgraph_function_or_thunk_node (callee, NULL);
1705 /* Try to find a destination for indirect edge IE that corresponds to a simple
1706 call or a call of a member function pointer and where the destination is a
1707 pointer formal parameter described by jump function JFUNC. If it can be
1708 determined, return the newly direct edge, otherwise return NULL. */
1710 static struct cgraph_edge *
1711 try_make_edge_direct_simple_call (struct cgraph_edge *ie,
1712 struct ipa_jump_func *jfunc)
1716 if (jfunc->type == IPA_JF_CONST)
1717 target = jfunc->value.constant;
1718 else if (jfunc->type == IPA_JF_CONST_MEMBER_PTR)
1719 target = jfunc->value.member_cst.pfn;
1723 return ipa_make_edge_direct_to_target (ie, target);
1726 /* Try to find a destination for indirect edge IE that corresponds to a
1727 virtual call based on a formal parameter which is described by jump
1728 function JFUNC and if it can be determined, make it direct and return the
1729 direct edge. Otherwise, return NULL. */
1731 static struct cgraph_edge *
1732 try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
1733 struct ipa_jump_func *jfunc)
1737 if (jfunc->type != IPA_JF_KNOWN_TYPE)
1740 binfo = TYPE_BINFO (jfunc->value.known_type.base_type);
1741 gcc_checking_assert (binfo);
1742 binfo = get_binfo_at_offset (binfo, jfunc->value.known_type.offset
1743 + ie->indirect_info->anc_offset,
1744 ie->indirect_info->otr_type);
1746 target = gimple_get_virt_method_for_binfo (ie->indirect_info->otr_token,
1752 return ipa_make_edge_direct_to_target (ie, target);
1757 /* Update the param called notes associated with NODE when CS is being inlined,
1758 assuming NODE is (potentially indirectly) inlined into CS->callee.
1759 Moreover, if the callee is discovered to be constant, create a new cgraph
1760 edge for it. Newly discovered indirect edges will be added to *NEW_EDGES,
1761 unless NEW_EDGES is NULL. Return true iff a new edge(s) were created. */
1764 update_indirect_edges_after_inlining (struct cgraph_edge *cs,
1765 struct cgraph_node *node,
1766 VEC (cgraph_edge_p, heap) **new_edges)
1768 struct ipa_edge_args *top;
1769 struct cgraph_edge *ie, *next_ie, *new_direct_edge;
1772 ipa_check_create_edge_args ();
1773 top = IPA_EDGE_REF (cs);
1775 for (ie = node->indirect_calls; ie; ie = next_ie)
1777 struct cgraph_indirect_call_info *ici = ie->indirect_info;
1778 struct ipa_jump_func *jfunc;
1780 next_ie = ie->next_callee;
1782 if (ici->param_index == -1)
1785 /* We must check range due to calls with variable number of arguments: */
1786 if (ici->param_index >= ipa_get_cs_argument_count (top))
1788 ici->param_index = -1;
1792 jfunc = ipa_get_ith_jump_func (top, ici->param_index);
1793 if (jfunc->type == IPA_JF_PASS_THROUGH
1794 && jfunc->value.pass_through.operation == NOP_EXPR)
1795 ici->param_index = jfunc->value.pass_through.formal_id;
1796 else if (jfunc->type == IPA_JF_ANCESTOR)
1798 ici->param_index = jfunc->value.ancestor.formal_id;
1799 ici->anc_offset += jfunc->value.ancestor.offset;
1802 /* Either we can find a destination for this edge now or never. */
1803 ici->param_index = -1;
1805 if (!flag_indirect_inlining)
1808 if (ici->polymorphic)
1809 new_direct_edge = try_make_edge_direct_virtual_call (ie, jfunc);
1811 new_direct_edge = try_make_edge_direct_simple_call (ie, jfunc);
1813 if (new_direct_edge)
1815 new_direct_edge->indirect_inlining_edge = 1;
1818 VEC_safe_push (cgraph_edge_p, heap, *new_edges,
1820 top = IPA_EDGE_REF (cs);
1829 /* Recursively traverse subtree of NODE (including node) made of inlined
1830 cgraph_edges when CS has been inlined and invoke
1831 update_indirect_edges_after_inlining on all nodes and
1832 update_jump_functions_after_inlining on all non-inlined edges that lead out
1833 of this subtree. Newly discovered indirect edges will be added to
1834 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were
1838 propagate_info_to_inlined_callees (struct cgraph_edge *cs,
1839 struct cgraph_node *node,
1840 VEC (cgraph_edge_p, heap) **new_edges)
1842 struct cgraph_edge *e;
1845 res = update_indirect_edges_after_inlining (cs, node, new_edges);
1847 for (e = node->callees; e; e = e->next_callee)
1848 if (!e->inline_failed)
1849 res |= propagate_info_to_inlined_callees (cs, e->callee, new_edges);
1851 update_jump_functions_after_inlining (cs, e);
1852 for (e = node->indirect_calls; e; e = e->next_callee)
1853 update_jump_functions_after_inlining (cs, e);
1858 /* Update jump functions and call note functions on inlining the call site CS.
1859 CS is expected to lead to a node already cloned by
1860 cgraph_clone_inline_nodes. Newly discovered indirect edges will be added to
1861 *NEW_EDGES, unless NEW_EDGES is NULL. Return true iff a new edge(s) were +
1865 ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1866 VEC (cgraph_edge_p, heap) **new_edges)
1869 /* Do nothing if the preparation phase has not been carried out yet
1870 (i.e. during early inlining). */
1871 if (!ipa_node_params_vector)
1873 gcc_assert (ipa_edge_args_vector);
1875 changed = propagate_info_to_inlined_callees (cs, cs->callee, new_edges);
1877 /* We do not keep jump functions of inlined edges up to date. Better to free
1878 them so we do not access them accidentally. */
1879 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1883 /* Frees all dynamically allocated structures that the argument info points
1887 ipa_free_edge_args_substructures (struct ipa_edge_args *args)
1889 if (args->jump_functions)
1890 ggc_free (args->jump_functions);
1892 memset (args, 0, sizeof (*args));
1895 /* Free all ipa_edge structures. */
1898 ipa_free_all_edge_args (void)
1901 struct ipa_edge_args *args;
1903 FOR_EACH_VEC_ELT (ipa_edge_args_t, ipa_edge_args_vector, i, args)
1904 ipa_free_edge_args_substructures (args);
1906 VEC_free (ipa_edge_args_t, gc, ipa_edge_args_vector);
1907 ipa_edge_args_vector = NULL;
1910 /* Frees all dynamically allocated structures that the param info points
1914 ipa_free_node_params_substructures (struct ipa_node_params *info)
1916 VEC_free (ipa_param_descriptor_t, heap, info->descriptors);
1917 free (info->lattices);
1918 /* Lattice values and their sources are deallocated with their alocation
1920 VEC_free (tree, heap, info->known_vals);
1921 memset (info, 0, sizeof (*info));
1924 /* Free all ipa_node_params structures. */
1927 ipa_free_all_node_params (void)
1930 struct ipa_node_params *info;
1932 FOR_EACH_VEC_ELT (ipa_node_params_t, ipa_node_params_vector, i, info)
1933 ipa_free_node_params_substructures (info);
1935 VEC_free (ipa_node_params_t, heap, ipa_node_params_vector);
1936 ipa_node_params_vector = NULL;
1939 /* Hook that is called by cgraph.c when an edge is removed. */
1942 ipa_edge_removal_hook (struct cgraph_edge *cs, void *data ATTRIBUTE_UNUSED)
1944 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1945 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
1946 <= (unsigned)cs->uid)
1948 ipa_free_edge_args_substructures (IPA_EDGE_REF (cs));
1951 /* Hook that is called by cgraph.c when a node is removed. */
1954 ipa_node_removal_hook (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
1956 /* During IPA-CP updating we can be called on not-yet analyze clones. */
1957 if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
1958 <= (unsigned)node->uid)
1960 ipa_free_node_params_substructures (IPA_NODE_REF (node));
1963 /* Hook that is called by cgraph.c when a node is duplicated. */
1966 ipa_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst,
1967 __attribute__((unused)) void *data)
1969 struct ipa_edge_args *old_args, *new_args;
1971 ipa_check_create_edge_args ();
1973 old_args = IPA_EDGE_REF (src);
1974 new_args = IPA_EDGE_REF (dst);
1976 new_args->jump_functions = VEC_copy (ipa_jump_func_t, gc,
1977 old_args->jump_functions);
1980 /* Hook that is called by cgraph.c when a node is duplicated. */
1983 ipa_node_duplication_hook (struct cgraph_node *src, struct cgraph_node *dst,
1984 ATTRIBUTE_UNUSED void *data)
1986 struct ipa_node_params *old_info, *new_info;
1988 ipa_check_create_node_params ();
1989 old_info = IPA_NODE_REF (src);
1990 new_info = IPA_NODE_REF (dst);
1992 new_info->descriptors = VEC_copy (ipa_param_descriptor_t, heap,
1993 old_info->descriptors);
1994 new_info->lattices = NULL;
1995 new_info->ipcp_orig_node = old_info->ipcp_orig_node;
1997 new_info->uses_analysis_done = old_info->uses_analysis_done;
1998 new_info->node_enqueued = old_info->node_enqueued;
2002 /* Analyze newly added function into callgraph. */
2005 ipa_add_new_function (struct cgraph_node *node, void *data ATTRIBUTE_UNUSED)
2007 ipa_analyze_node (node);
2010 /* Register our cgraph hooks if they are not already there. */
2013 ipa_register_cgraph_hooks (void)
2015 if (!edge_removal_hook_holder)
2016 edge_removal_hook_holder =
2017 cgraph_add_edge_removal_hook (&ipa_edge_removal_hook, NULL);
2018 if (!node_removal_hook_holder)
2019 node_removal_hook_holder =
2020 cgraph_add_node_removal_hook (&ipa_node_removal_hook, NULL);
2021 if (!edge_duplication_hook_holder)
2022 edge_duplication_hook_holder =
2023 cgraph_add_edge_duplication_hook (&ipa_edge_duplication_hook, NULL);
2024 if (!node_duplication_hook_holder)
2025 node_duplication_hook_holder =
2026 cgraph_add_node_duplication_hook (&ipa_node_duplication_hook, NULL);
2027 function_insertion_hook_holder =
2028 cgraph_add_function_insertion_hook (&ipa_add_new_function, NULL);
2031 /* Unregister our cgraph hooks if they are not already there. */
2034 ipa_unregister_cgraph_hooks (void)
2036 cgraph_remove_edge_removal_hook (edge_removal_hook_holder);
2037 edge_removal_hook_holder = NULL;
2038 cgraph_remove_node_removal_hook (node_removal_hook_holder);
2039 node_removal_hook_holder = NULL;
2040 cgraph_remove_edge_duplication_hook (edge_duplication_hook_holder);
2041 edge_duplication_hook_holder = NULL;
2042 cgraph_remove_node_duplication_hook (node_duplication_hook_holder);
2043 node_duplication_hook_holder = NULL;
2044 cgraph_remove_function_insertion_hook (function_insertion_hook_holder);
2045 function_insertion_hook_holder = NULL;
2048 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2049 longer needed after ipa-cp. */
2052 ipa_free_all_structures_after_ipa_cp (void)
2056 ipa_free_all_edge_args ();
2057 ipa_free_all_node_params ();
2058 free_alloc_pool (ipcp_sources_pool);
2059 free_alloc_pool (ipcp_values_pool);
2060 ipa_unregister_cgraph_hooks ();
2064 /* Free all ipa_node_params and all ipa_edge_args structures if they are no
2065 longer needed after indirect inlining. */
2068 ipa_free_all_structures_after_iinln (void)
2070 ipa_free_all_edge_args ();
2071 ipa_free_all_node_params ();
2072 ipa_unregister_cgraph_hooks ();
2073 if (ipcp_sources_pool)
2074 free_alloc_pool (ipcp_sources_pool);
2075 if (ipcp_values_pool)
2076 free_alloc_pool (ipcp_values_pool);
2079 /* Print ipa_tree_map data structures of all functions in the
2083 ipa_print_node_params (FILE * f, struct cgraph_node *node)
2087 struct ipa_node_params *info;
2089 if (!node->analyzed)
2091 info = IPA_NODE_REF (node);
2092 fprintf (f, " function %s parameter descriptors:\n",
2093 cgraph_node_name (node));
2094 count = ipa_get_param_count (info);
2095 for (i = 0; i < count; i++)
2097 temp = ipa_get_param (info, i);
2098 if (TREE_CODE (temp) == PARM_DECL)
2099 fprintf (f, " param %d : %s", i,
2101 ? (*lang_hooks.decl_printable_name) (temp, 2)
2103 if (ipa_is_param_used (info, i))
2104 fprintf (f, " used");
2109 /* Print ipa_tree_map data structures of all functions in the
2113 ipa_print_all_params (FILE * f)
2115 struct cgraph_node *node;
2117 fprintf (f, "\nFunction parameters:\n");
2118 for (node = cgraph_nodes; node; node = node->next)
2119 ipa_print_node_params (f, node);
2122 /* Return a heap allocated vector containing formal parameters of FNDECL. */
2125 ipa_get_vector_of_formal_parms (tree fndecl)
2127 VEC(tree, heap) *args;
2131 count = count_formal_params (fndecl);
2132 args = VEC_alloc (tree, heap, count);
2133 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
2134 VEC_quick_push (tree, args, parm);
2139 /* Return a heap allocated vector containing types of formal parameters of
2140 function type FNTYPE. */
2142 static inline VEC(tree, heap) *
2143 get_vector_of_formal_parm_types (tree fntype)
2145 VEC(tree, heap) *types;
2149 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2152 types = VEC_alloc (tree, heap, count);
2153 for (t = TYPE_ARG_TYPES (fntype); t; t = TREE_CHAIN (t))
2154 VEC_quick_push (tree, types, TREE_VALUE (t));
2159 /* Modify the function declaration FNDECL and its type according to the plan in
2160 ADJUSTMENTS. It also sets base fields of individual adjustments structures
2161 to reflect the actual parameters being modified which are determined by the
2162 base_index field. */
2165 ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec adjustments,
2166 const char *synth_parm_prefix)
2168 VEC(tree, heap) *oparms, *otypes;
2169 tree orig_type, new_type = NULL;
2170 tree old_arg_types, t, new_arg_types = NULL;
2171 tree parm, *link = &DECL_ARGUMENTS (fndecl);
2172 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2173 tree new_reversed = NULL;
2174 bool care_for_types, last_parm_void;
2176 if (!synth_parm_prefix)
2177 synth_parm_prefix = "SYNTH";
2179 oparms = ipa_get_vector_of_formal_parms (fndecl);
2180 orig_type = TREE_TYPE (fndecl);
2181 old_arg_types = TYPE_ARG_TYPES (orig_type);
2183 /* The following test is an ugly hack, some functions simply don't have any
2184 arguments in their type. This is probably a bug but well... */
2185 care_for_types = (old_arg_types != NULL_TREE);
2188 last_parm_void = (TREE_VALUE (tree_last (old_arg_types))
2190 otypes = get_vector_of_formal_parm_types (orig_type);
2192 gcc_assert (VEC_length (tree, oparms) + 1 == VEC_length (tree, otypes));
2194 gcc_assert (VEC_length (tree, oparms) == VEC_length (tree, otypes));
2198 last_parm_void = false;
2202 for (i = 0; i < len; i++)
2204 struct ipa_parm_adjustment *adj;
2207 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2208 parm = VEC_index (tree, oparms, adj->base_index);
2211 if (adj->copy_param)
2214 new_arg_types = tree_cons (NULL_TREE, VEC_index (tree, otypes,
2218 link = &DECL_CHAIN (parm);
2220 else if (!adj->remove_param)
2226 ptype = build_pointer_type (adj->type);
2231 new_arg_types = tree_cons (NULL_TREE, ptype, new_arg_types);
2233 new_parm = build_decl (UNKNOWN_LOCATION, PARM_DECL, NULL_TREE,
2235 DECL_NAME (new_parm) = create_tmp_var_name (synth_parm_prefix);
2237 DECL_ARTIFICIAL (new_parm) = 1;
2238 DECL_ARG_TYPE (new_parm) = ptype;
2239 DECL_CONTEXT (new_parm) = fndecl;
2240 TREE_USED (new_parm) = 1;
2241 DECL_IGNORED_P (new_parm) = 1;
2242 layout_decl (new_parm, 0);
2244 add_referenced_var (new_parm);
2245 mark_sym_for_renaming (new_parm);
2247 adj->reduction = new_parm;
2251 link = &DECL_CHAIN (new_parm);
2259 new_reversed = nreverse (new_arg_types);
2263 TREE_CHAIN (new_arg_types) = void_list_node;
2265 new_reversed = void_list_node;
2269 /* Use copy_node to preserve as much as possible from original type
2270 (debug info, attribute lists etc.)
2271 Exception is METHOD_TYPEs must have THIS argument.
2272 When we are asked to remove it, we need to build new FUNCTION_TYPE
2274 if (TREE_CODE (orig_type) != METHOD_TYPE
2275 || (VEC_index (ipa_parm_adjustment_t, adjustments, 0)->copy_param
2276 && VEC_index (ipa_parm_adjustment_t, adjustments, 0)->base_index == 0))
2278 new_type = build_distinct_type_copy (orig_type);
2279 TYPE_ARG_TYPES (new_type) = new_reversed;
2284 = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
2286 TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
2287 DECL_VINDEX (fndecl) = NULL_TREE;
2290 /* When signature changes, we need to clear builtin info. */
2291 if (DECL_BUILT_IN (fndecl))
2293 DECL_BUILT_IN_CLASS (fndecl) = NOT_BUILT_IN;
2294 DECL_FUNCTION_CODE (fndecl) = (enum built_in_function) 0;
2297 /* This is a new type, not a copy of an old type. Need to reassociate
2298 variants. We can handle everything except the main variant lazily. */
2299 t = TYPE_MAIN_VARIANT (orig_type);
2302 TYPE_MAIN_VARIANT (new_type) = t;
2303 TYPE_NEXT_VARIANT (new_type) = TYPE_NEXT_VARIANT (t);
2304 TYPE_NEXT_VARIANT (t) = new_type;
2308 TYPE_MAIN_VARIANT (new_type) = new_type;
2309 TYPE_NEXT_VARIANT (new_type) = NULL;
2312 TREE_TYPE (fndecl) = new_type;
2313 DECL_VIRTUAL_P (fndecl) = 0;
2315 VEC_free (tree, heap, otypes);
2316 VEC_free (tree, heap, oparms);
2319 /* Modify actual arguments of a function call CS as indicated in ADJUSTMENTS.
2320 If this is a directly recursive call, CS must be NULL. Otherwise it must
2321 contain the corresponding call graph edge. */
2324 ipa_modify_call_arguments (struct cgraph_edge *cs, gimple stmt,
2325 ipa_parm_adjustment_vec adjustments)
2327 VEC(tree, heap) *vargs;
2328 VEC(tree, gc) **debug_args = NULL;
2330 gimple_stmt_iterator gsi;
2334 len = VEC_length (ipa_parm_adjustment_t, adjustments);
2335 vargs = VEC_alloc (tree, heap, len);
2336 callee_decl = !cs ? gimple_call_fndecl (stmt) : cs->callee->decl;
2338 gsi = gsi_for_stmt (stmt);
2339 for (i = 0; i < len; i++)
2341 struct ipa_parm_adjustment *adj;
2343 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2345 if (adj->copy_param)
2347 tree arg = gimple_call_arg (stmt, adj->base_index);
2349 VEC_quick_push (tree, vargs, arg);
2351 else if (!adj->remove_param)
2353 tree expr, base, off;
2356 /* We create a new parameter out of the value of the old one, we can
2357 do the following kind of transformations:
2359 - A scalar passed by reference is converted to a scalar passed by
2360 value. (adj->by_ref is false and the type of the original
2361 actual argument is a pointer to a scalar).
2363 - A part of an aggregate is passed instead of the whole aggregate.
2364 The part can be passed either by value or by reference, this is
2365 determined by value of adj->by_ref. Moreover, the code below
2366 handles both situations when the original aggregate is passed by
2367 value (its type is not a pointer) and when it is passed by
2368 reference (it is a pointer to an aggregate).
2370 When the new argument is passed by reference (adj->by_ref is true)
2371 it must be a part of an aggregate and therefore we form it by
2372 simply taking the address of a reference inside the original
2375 gcc_checking_assert (adj->offset % BITS_PER_UNIT == 0);
2376 base = gimple_call_arg (stmt, adj->base_index);
2377 loc = EXPR_LOCATION (base);
2379 if (TREE_CODE (base) != ADDR_EXPR
2380 && POINTER_TYPE_P (TREE_TYPE (base)))
2381 off = build_int_cst (adj->alias_ptr_type,
2382 adj->offset / BITS_PER_UNIT);
2385 HOST_WIDE_INT base_offset;
2388 if (TREE_CODE (base) == ADDR_EXPR)
2389 base = TREE_OPERAND (base, 0);
2391 base = get_addr_base_and_unit_offset (base, &base_offset);
2392 /* Aggregate arguments can have non-invariant addresses. */
2395 base = build_fold_addr_expr (prev_base);
2396 off = build_int_cst (adj->alias_ptr_type,
2397 adj->offset / BITS_PER_UNIT);
2399 else if (TREE_CODE (base) == MEM_REF)
2401 off = build_int_cst (adj->alias_ptr_type,
2403 + adj->offset / BITS_PER_UNIT);
2404 off = int_const_binop (PLUS_EXPR, TREE_OPERAND (base, 1),
2406 base = TREE_OPERAND (base, 0);
2410 off = build_int_cst (adj->alias_ptr_type,
2412 + adj->offset / BITS_PER_UNIT);
2413 base = build_fold_addr_expr (base);
2417 expr = fold_build2_loc (loc, MEM_REF, adj->type, base, off);
2419 expr = build_fold_addr_expr (expr);
2421 expr = force_gimple_operand_gsi (&gsi, expr,
2423 || is_gimple_reg_type (adj->type),
2424 NULL, true, GSI_SAME_STMT);
2425 VEC_quick_push (tree, vargs, expr);
2427 if (!adj->copy_param && MAY_HAVE_DEBUG_STMTS)
2430 tree ddecl = NULL_TREE, origin = DECL_ORIGIN (adj->base), arg;
2433 arg = gimple_call_arg (stmt, adj->base_index);
2434 if (!useless_type_conversion_p (TREE_TYPE (origin), TREE_TYPE (arg)))
2436 if (!fold_convertible_p (TREE_TYPE (origin), arg))
2438 arg = fold_convert_loc (gimple_location (stmt),
2439 TREE_TYPE (origin), arg);
2441 if (debug_args == NULL)
2442 debug_args = decl_debug_args_insert (callee_decl);
2443 for (ix = 0; VEC_iterate (tree, *debug_args, ix, ddecl); ix += 2)
2444 if (ddecl == origin)
2446 ddecl = VEC_index (tree, *debug_args, ix + 1);
2451 ddecl = make_node (DEBUG_EXPR_DECL);
2452 DECL_ARTIFICIAL (ddecl) = 1;
2453 TREE_TYPE (ddecl) = TREE_TYPE (origin);
2454 DECL_MODE (ddecl) = DECL_MODE (origin);
2456 VEC_safe_push (tree, gc, *debug_args, origin);
2457 VEC_safe_push (tree, gc, *debug_args, ddecl);
2459 def_temp = gimple_build_debug_bind (ddecl, unshare_expr (arg),
2461 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
2465 if (dump_file && (dump_flags & TDF_DETAILS))
2467 fprintf (dump_file, "replacing stmt:");
2468 print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, 0);
2471 new_stmt = gimple_build_call_vec (callee_decl, vargs);
2472 VEC_free (tree, heap, vargs);
2473 if (gimple_call_lhs (stmt))
2474 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2476 gimple_set_block (new_stmt, gimple_block (stmt));
2477 if (gimple_has_location (stmt))
2478 gimple_set_location (new_stmt, gimple_location (stmt));
2479 gimple_call_copy_flags (new_stmt, stmt);
2480 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2482 if (dump_file && (dump_flags & TDF_DETAILS))
2484 fprintf (dump_file, "with stmt:");
2485 print_gimple_stmt (dump_file, new_stmt, 0, 0);
2486 fprintf (dump_file, "\n");
2488 gsi_replace (&gsi, new_stmt, true);
2490 cgraph_set_call_stmt (cs, new_stmt);
2491 update_ssa (TODO_update_ssa);
2492 free_dominance_info (CDI_DOMINATORS);
2495 /* Return true iff BASE_INDEX is in ADJUSTMENTS more than once. */
2498 index_in_adjustments_multiple_times_p (int base_index,
2499 ipa_parm_adjustment_vec adjustments)
2501 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2504 for (i = 0; i < len; i++)
2506 struct ipa_parm_adjustment *adj;
2507 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2509 if (adj->base_index == base_index)
2521 /* Return adjustments that should have the same effect on function parameters
2522 and call arguments as if they were first changed according to adjustments in
2523 INNER and then by adjustments in OUTER. */
2525 ipa_parm_adjustment_vec
2526 ipa_combine_adjustments (ipa_parm_adjustment_vec inner,
2527 ipa_parm_adjustment_vec outer)
2529 int i, outlen = VEC_length (ipa_parm_adjustment_t, outer);
2530 int inlen = VEC_length (ipa_parm_adjustment_t, inner);
2532 ipa_parm_adjustment_vec adjustments, tmp;
2534 tmp = VEC_alloc (ipa_parm_adjustment_t, heap, inlen);
2535 for (i = 0; i < inlen; i++)
2537 struct ipa_parm_adjustment *n;
2538 n = VEC_index (ipa_parm_adjustment_t, inner, i);
2540 if (n->remove_param)
2543 VEC_quick_push (ipa_parm_adjustment_t, tmp, n);
2546 adjustments = VEC_alloc (ipa_parm_adjustment_t, heap, outlen + removals);
2547 for (i = 0; i < outlen; i++)
2549 struct ipa_parm_adjustment *r;
2550 struct ipa_parm_adjustment *out = VEC_index (ipa_parm_adjustment_t,
2552 struct ipa_parm_adjustment *in = VEC_index (ipa_parm_adjustment_t, tmp,
2555 gcc_assert (!in->remove_param);
2556 if (out->remove_param)
2558 if (!index_in_adjustments_multiple_times_p (in->base_index, tmp))
2560 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2561 memset (r, 0, sizeof (*r));
2562 r->remove_param = true;
2567 r = VEC_quick_push (ipa_parm_adjustment_t, adjustments, NULL);
2568 memset (r, 0, sizeof (*r));
2569 r->base_index = in->base_index;
2570 r->type = out->type;
2572 /* FIXME: Create nonlocal value too. */
2574 if (in->copy_param && out->copy_param)
2575 r->copy_param = true;
2576 else if (in->copy_param)
2577 r->offset = out->offset;
2578 else if (out->copy_param)
2579 r->offset = in->offset;
2581 r->offset = in->offset + out->offset;
2584 for (i = 0; i < inlen; i++)
2586 struct ipa_parm_adjustment *n = VEC_index (ipa_parm_adjustment_t,
2589 if (n->remove_param)
2590 VEC_quick_push (ipa_parm_adjustment_t, adjustments, n);
2593 VEC_free (ipa_parm_adjustment_t, heap, tmp);
2597 /* Dump the adjustments in the vector ADJUSTMENTS to dump_file in a human
2598 friendly way, assuming they are meant to be applied to FNDECL. */
2601 ipa_dump_param_adjustments (FILE *file, ipa_parm_adjustment_vec adjustments,
2604 int i, len = VEC_length (ipa_parm_adjustment_t, adjustments);
2606 VEC(tree, heap) *parms = ipa_get_vector_of_formal_parms (fndecl);
2608 fprintf (file, "IPA param adjustments: ");
2609 for (i = 0; i < len; i++)
2611 struct ipa_parm_adjustment *adj;
2612 adj = VEC_index (ipa_parm_adjustment_t, adjustments, i);
2615 fprintf (file, " ");
2619 fprintf (file, "%i. base_index: %i - ", i, adj->base_index);
2620 print_generic_expr (file, VEC_index (tree, parms, adj->base_index), 0);
2623 fprintf (file, ", base: ");
2624 print_generic_expr (file, adj->base, 0);
2628 fprintf (file, ", reduction: ");
2629 print_generic_expr (file, adj->reduction, 0);
2631 if (adj->new_ssa_base)
2633 fprintf (file, ", new_ssa_base: ");
2634 print_generic_expr (file, adj->new_ssa_base, 0);
2637 if (adj->copy_param)
2638 fprintf (file, ", copy_param");
2639 else if (adj->remove_param)
2640 fprintf (file, ", remove_param");
2642 fprintf (file, ", offset %li", (long) adj->offset);
2644 fprintf (file, ", by_ref");
2645 print_node_brief (file, ", type: ", adj->type, 0);
2646 fprintf (file, "\n");
2648 VEC_free (tree, heap, parms);
2651 /* Stream out jump function JUMP_FUNC to OB. */
2654 ipa_write_jump_function (struct output_block *ob,
2655 struct ipa_jump_func *jump_func)
2657 streamer_write_uhwi (ob, jump_func->type);
2659 switch (jump_func->type)
2661 case IPA_JF_UNKNOWN:
2663 case IPA_JF_KNOWN_TYPE:
2664 streamer_write_uhwi (ob, jump_func->value.known_type.offset);
2665 stream_write_tree (ob, jump_func->value.known_type.base_type, true);
2666 stream_write_tree (ob, jump_func->value.known_type.component_type, true);
2669 stream_write_tree (ob, jump_func->value.constant, true);
2671 case IPA_JF_PASS_THROUGH:
2672 stream_write_tree (ob, jump_func->value.pass_through.operand, true);
2673 streamer_write_uhwi (ob, jump_func->value.pass_through.formal_id);
2674 streamer_write_uhwi (ob, jump_func->value.pass_through.operation);
2676 case IPA_JF_ANCESTOR:
2677 streamer_write_uhwi (ob, jump_func->value.ancestor.offset);
2678 stream_write_tree (ob, jump_func->value.ancestor.type, true);
2679 streamer_write_uhwi (ob, jump_func->value.ancestor.formal_id);
2681 case IPA_JF_CONST_MEMBER_PTR:
2682 stream_write_tree (ob, jump_func->value.member_cst.pfn, true);
2683 stream_write_tree (ob, jump_func->value.member_cst.delta, false);
2688 /* Read in jump function JUMP_FUNC from IB. */
2691 ipa_read_jump_function (struct lto_input_block *ib,
2692 struct ipa_jump_func *jump_func,
2693 struct data_in *data_in)
2695 jump_func->type = (enum jump_func_type) streamer_read_uhwi (ib);
2697 switch (jump_func->type)
2699 case IPA_JF_UNKNOWN:
2701 case IPA_JF_KNOWN_TYPE:
2702 jump_func->value.known_type.offset = streamer_read_uhwi (ib);
2703 jump_func->value.known_type.base_type = stream_read_tree (ib, data_in);
2704 jump_func->value.known_type.component_type = stream_read_tree (ib,
2708 jump_func->value.constant = stream_read_tree (ib, data_in);
2710 case IPA_JF_PASS_THROUGH:
2711 jump_func->value.pass_through.operand = stream_read_tree (ib, data_in);
2712 jump_func->value.pass_through.formal_id = streamer_read_uhwi (ib);
2713 jump_func->value.pass_through.operation
2714 = (enum tree_code) streamer_read_uhwi (ib);
2716 case IPA_JF_ANCESTOR:
2717 jump_func->value.ancestor.offset = streamer_read_uhwi (ib);
2718 jump_func->value.ancestor.type = stream_read_tree (ib, data_in);
2719 jump_func->value.ancestor.formal_id = streamer_read_uhwi (ib);
2721 case IPA_JF_CONST_MEMBER_PTR:
2722 jump_func->value.member_cst.pfn = stream_read_tree (ib, data_in);
2723 jump_func->value.member_cst.delta = stream_read_tree (ib, data_in);
2728 /* Stream out parts of cgraph_indirect_call_info corresponding to CS that are
2729 relevant to indirect inlining to OB. */
2732 ipa_write_indirect_edge_info (struct output_block *ob,
2733 struct cgraph_edge *cs)
2735 struct cgraph_indirect_call_info *ii = cs->indirect_info;
2736 struct bitpack_d bp;
2738 streamer_write_hwi (ob, ii->param_index);
2739 streamer_write_hwi (ob, ii->anc_offset);
2740 bp = bitpack_create (ob->main_stream);
2741 bp_pack_value (&bp, ii->polymorphic, 1);
2742 streamer_write_bitpack (&bp);
2744 if (ii->polymorphic)
2746 streamer_write_hwi (ob, ii->otr_token);
2747 stream_write_tree (ob, ii->otr_type, true);
2751 /* Read in parts of cgraph_indirect_call_info corresponding to CS that are
2752 relevant to indirect inlining from IB. */
2755 ipa_read_indirect_edge_info (struct lto_input_block *ib,
2756 struct data_in *data_in ATTRIBUTE_UNUSED,
2757 struct cgraph_edge *cs)
2759 struct cgraph_indirect_call_info *ii = cs->indirect_info;
2760 struct bitpack_d bp;
2762 ii->param_index = (int) streamer_read_hwi (ib);
2763 ii->anc_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
2764 bp = streamer_read_bitpack (ib);
2765 ii->polymorphic = bp_unpack_value (&bp, 1);
2766 if (ii->polymorphic)
2768 ii->otr_token = (HOST_WIDE_INT) streamer_read_hwi (ib);
2769 ii->otr_type = stream_read_tree (ib, data_in);
2773 /* Stream out NODE info to OB. */
2776 ipa_write_node_info (struct output_block *ob, struct cgraph_node *node)
2779 lto_cgraph_encoder_t encoder;
2780 struct ipa_node_params *info = IPA_NODE_REF (node);
2782 struct cgraph_edge *e;
2783 struct bitpack_d bp;
2785 encoder = ob->decl_state->cgraph_node_encoder;
2786 node_ref = lto_cgraph_encoder_encode (encoder, node);
2787 streamer_write_uhwi (ob, node_ref);
2789 bp = bitpack_create (ob->main_stream);
2790 gcc_assert (info->uses_analysis_done
2791 || ipa_get_param_count (info) == 0);
2792 gcc_assert (!info->node_enqueued);
2793 gcc_assert (!info->ipcp_orig_node);
2794 for (j = 0; j < ipa_get_param_count (info); j++)
2795 bp_pack_value (&bp, ipa_is_param_used (info, j), 1);
2796 streamer_write_bitpack (&bp);
2797 for (e = node->callees; e; e = e->next_callee)
2799 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2801 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2802 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2803 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2805 for (e = node->indirect_calls; e; e = e->next_callee)
2807 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2809 streamer_write_uhwi (ob, ipa_get_cs_argument_count (args));
2810 for (j = 0; j < ipa_get_cs_argument_count (args); j++)
2811 ipa_write_jump_function (ob, ipa_get_ith_jump_func (args, j));
2812 ipa_write_indirect_edge_info (ob, e);
2816 /* Stream in NODE info from IB. */
2819 ipa_read_node_info (struct lto_input_block *ib, struct cgraph_node *node,
2820 struct data_in *data_in)
2822 struct ipa_node_params *info = IPA_NODE_REF (node);
2824 struct cgraph_edge *e;
2825 struct bitpack_d bp;
2827 ipa_initialize_node_params (node);
2829 bp = streamer_read_bitpack (ib);
2830 if (ipa_get_param_count (info) != 0)
2831 info->uses_analysis_done = true;
2832 info->node_enqueued = false;
2833 for (k = 0; k < ipa_get_param_count (info); k++)
2834 ipa_set_param_used (info, k, bp_unpack_value (&bp, 1));
2835 for (e = node->callees; e; e = e->next_callee)
2837 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2838 int count = streamer_read_uhwi (ib);
2842 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions, count);
2844 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2845 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k), data_in);
2847 for (e = node->indirect_calls; e; e = e->next_callee)
2849 struct ipa_edge_args *args = IPA_EDGE_REF (e);
2850 int count = streamer_read_uhwi (ib);
2854 VEC_safe_grow_cleared (ipa_jump_func_t, gc, args->jump_functions,
2856 for (k = 0; k < ipa_get_cs_argument_count (args); k++)
2857 ipa_read_jump_function (ib, ipa_get_ith_jump_func (args, k),
2860 ipa_read_indirect_edge_info (ib, data_in, e);
2864 /* Write jump functions for nodes in SET. */
2867 ipa_prop_write_jump_functions (cgraph_node_set set)
2869 struct cgraph_node *node;
2870 struct output_block *ob;
2871 unsigned int count = 0;
2872 cgraph_node_set_iterator csi;
2874 if (!ipa_node_params_vector)
2877 ob = create_output_block (LTO_section_jump_functions);
2878 ob->cgraph_node = NULL;
2879 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2881 node = csi_node (csi);
2882 if (cgraph_function_with_gimple_body_p (node)
2883 && IPA_NODE_REF (node) != NULL)
2887 streamer_write_uhwi (ob, count);
2889 /* Process all of the functions. */
2890 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2892 node = csi_node (csi);
2893 if (cgraph_function_with_gimple_body_p (node)
2894 && IPA_NODE_REF (node) != NULL)
2895 ipa_write_node_info (ob, node);
2897 streamer_write_char_stream (ob->main_stream, 0);
2898 produce_asm (ob, NULL);
2899 destroy_output_block (ob);
2902 /* Read section in file FILE_DATA of length LEN with data DATA. */
2905 ipa_prop_read_section (struct lto_file_decl_data *file_data, const char *data,
2908 const struct lto_function_header *header =
2909 (const struct lto_function_header *) data;
2910 const int32_t cfg_offset = sizeof (struct lto_function_header);
2911 const int32_t main_offset = cfg_offset + header->cfg_size;
2912 const int32_t string_offset = main_offset + header->main_size;
2913 struct data_in *data_in;
2914 struct lto_input_block ib_main;
2918 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2922 lto_data_in_create (file_data, (const char *) data + string_offset,
2923 header->string_size, NULL);
2924 count = streamer_read_uhwi (&ib_main);
2926 for (i = 0; i < count; i++)
2929 struct cgraph_node *node;
2930 lto_cgraph_encoder_t encoder;
2932 index = streamer_read_uhwi (&ib_main);
2933 encoder = file_data->cgraph_node_encoder;
2934 node = lto_cgraph_encoder_deref (encoder, index);
2935 gcc_assert (node->analyzed);
2936 ipa_read_node_info (&ib_main, node, data_in);
2938 lto_free_section_data (file_data, LTO_section_jump_functions, NULL, data,
2940 lto_data_in_delete (data_in);
2943 /* Read ipcp jump functions. */
2946 ipa_prop_read_jump_functions (void)
2948 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
2949 struct lto_file_decl_data *file_data;
2952 ipa_check_create_node_params ();
2953 ipa_check_create_edge_args ();
2954 ipa_register_cgraph_hooks ();
2956 while ((file_data = file_data_vec[j++]))
2959 const char *data = lto_get_section_data (file_data, LTO_section_jump_functions, NULL, &len);
2962 ipa_prop_read_section (file_data, data, len);
2966 /* After merging units, we can get mismatch in argument counts.
2967 Also decl merging might've rendered parameter lists obsolete.
2968 Also compute called_with_variable_arg info. */
2971 ipa_update_after_lto_read (void)
2973 struct cgraph_node *node;
2975 ipa_check_create_node_params ();
2976 ipa_check_create_edge_args ();
2978 for (node = cgraph_nodes; node; node = node->next)
2980 ipa_initialize_node_params (node);