1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Interprocedural constant propagation (IPA-CP).
25 The goal of this transformation is to
27 1) discover functions which are always invoked with some arguments with the
28 same known constant values and modify the functions so that the
29 subsequent optimizations can take advantage of the knowledge, and
31 2) partial specialization - create specialized versions of functions
32 transformed in this way if some parameters are known constants only in
33 certain contexts but the estimated tradeoff between speedup and cost size
36 The algorithm also propagates types and attempts to perform type based
37 devirtualization. Types are propagated much like constants.
39 The algorithm basically consists of three stages. In the first, functions
40 are analyzed one at a time and jump functions are constructed for all known
41 call-sites. In the second phase, the pass propagates information from the
42 jump functions across the call to reveal what values are available at what
43 call sites, performs estimations of effects of known values on functions and
44 their callees, and finally decides what specialized extra versions should be
45 created. In the third, the special versions materialize and appropriate
48 The algorithm used is to a certain extent based on "Interprocedural Constant
49 Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon,
50 Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D
51 Cooper, Mary W. Hall, and Ken Kennedy.
54 First stage - intraprocedural analysis
55 =======================================
57 This phase computes jump_function and modification flags.
59 A jump function for a call-site represents the values passed as an actual
60 arguments of a given call-site. In principle, there are three types of
63 Pass through - the caller's formal parameter is passed as an actual
64 argument, plus an operation on it can be performed.
65 Constant - a constant is passed as an actual argument.
66 Unknown - neither of the above.
68 All jump function types are described in detail in ipa-prop.h, together with
69 the data structures that represent them and methods of accessing them.
71 ipcp_generate_summary() is the main function of the first stage.
73 Second stage - interprocedural analysis
74 ========================================
76 This stage is itself divided into two phases. In the first, we propagate
77 known values over the call graph, in the second, we make cloning decisions.
78 It uses a different algorithm than the original Callahan's paper.
80 First, we traverse the functions topologically from callers to callees and,
81 for each strongly connected component (SCC), we propagate constants
82 according to previously computed jump functions. We also record what known
83 values depend on other known values and estimate local effects. Finally, we
84 propagate cumulative information about these effects from dependent values
85 to those on which they depend.
87 Second, we again traverse the call graph in the same topological order and
88 make clones for functions which we know are called with the same values in
89 all contexts and decide about extra specialized clones of functions just for
90 some contexts - these decisions are based on both local estimates and
91 cumulative estimates propagated from callees.
93 ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
96 Third phase - materialization of clones, call statement updates.
97 ============================================
99 This stage is currently performed by call graph code (mainly in cgraphunit.c
100 and tree-inline.c) according to instructions inserted to the call graph by
105 #include "coretypes.h"
107 #include "gimple-fold.h"
108 #include "gimple-expr.h"
111 #include "basic-block.h"
113 #include "hash-map.h"
115 #include "plugin-api.h"
117 #include "hash-set.h"
118 #include "machmode.h"
120 #include "hard-reg-set.h"
122 #include "function.h"
125 #include "alloc-pool.h"
126 #include "ipa-prop.h"
128 #include "tree-pass.h"
130 #include "diagnostic.h"
131 #include "tree-pretty-print.h"
132 #include "tree-inline.h"
134 #include "ipa-inline.h"
135 #include "ipa-utils.h"
139 /* Describes a particular source for an IPA-CP value. */
141 struct ipcp_value_source
143 /* Aggregate offset of the source, negative if the source is scalar value of
144 the argument itself. */
145 HOST_WIDE_INT offset
;
146 /* The incoming edge that brought the value. */
147 struct cgraph_edge
*cs
;
148 /* If the jump function that resulted into his value was a pass-through or an
149 ancestor, this is the ipcp_value of the caller from which the described
150 value has been derived. Otherwise it is NULL. */
151 struct ipcp_value
*val
;
152 /* Next pointer in a linked list of sources of a value. */
153 struct ipcp_value_source
*next
;
154 /* If the jump function that resulted into his value was a pass-through or an
155 ancestor, this is the index of the parameter of the caller the jump
156 function references. */
160 /* Describes one particular value stored in struct ipcp_lattice. */
164 /* The actual value for the given parameter. This is either an IPA invariant
165 or a TREE_BINFO describing a type that can be used for
168 /* The list of sources from which this value originates. */
169 struct ipcp_value_source
*sources
;
170 /* Next pointers in a linked list of all values in a lattice. */
171 struct ipcp_value
*next
;
172 /* Next pointers in a linked list of values in a strongly connected component
174 struct ipcp_value
*scc_next
;
175 /* Next pointers in a linked list of SCCs of values sorted topologically
176 according their sources. */
177 struct ipcp_value
*topo_next
;
178 /* A specialized node created for this value, NULL if none has been (so far)
180 struct cgraph_node
*spec_node
;
181 /* Depth first search number and low link for topological sorting of
184 /* Time benefit and size cost that specializing the function for this value
185 would bring about in this function alone. */
186 int local_time_benefit
, local_size_cost
;
187 /* Time benefit and size cost that specializing the function for this value
188 can bring about in it's callees (transitively). */
189 int prop_time_benefit
, prop_size_cost
;
190 /* True if this valye is currently on the topo-sort stack. */
194 /* Lattice describing potential values of a formal parameter of a function, or
195 a part of an aggreagate. TOP is represented by a lattice with zero values
196 and with contains_variable and bottom flags cleared. BOTTOM is represented
197 by a lattice with the bottom flag set. In that case, values and
198 contains_variable flag should be disregarded. */
202 /* The list of known values and types in this lattice. Note that values are
203 not deallocated if a lattice is set to bottom because there may be value
204 sources referencing them. */
205 struct ipcp_value
*values
;
206 /* Number of known values and types in this lattice. */
208 /* The lattice contains a variable component (in addition to values). */
209 bool contains_variable
;
210 /* The value of the lattice is bottom (i.e. variable and unusable for any
215 /* Lattice with an offset to describe a part of an aggregate. */
217 struct ipcp_agg_lattice
: public ipcp_lattice
219 /* Offset that is being described by this lattice. */
220 HOST_WIDE_INT offset
;
221 /* Size so that we don't have to re-compute it every time we traverse the
222 list. Must correspond to TYPE_SIZE of all lat values. */
224 /* Next element of the linked list. */
225 struct ipcp_agg_lattice
*next
;
228 /* Structure containing lattices for a parameter itself and for pieces of
229 aggregates that are passed in the parameter or by a reference in a parameter
230 plus some other useful flags. */
232 struct ipcp_param_lattices
234 /* Lattice describing the value of the parameter itself. */
235 struct ipcp_lattice itself
;
236 /* Lattices describing aggregate parts. */
237 struct ipcp_agg_lattice
*aggs
;
238 /* Number of aggregate lattices */
240 /* True if aggregate data were passed by reference (as opposed to by
243 /* All aggregate lattices contain a variable component (in addition to
245 bool aggs_contain_variable
;
246 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
247 for any propagation). */
250 /* There is a virtual call based on this parameter. */
254 /* Allocation pools for values and their sources in ipa-cp. */
256 alloc_pool ipcp_values_pool
;
257 alloc_pool ipcp_sources_pool
;
258 alloc_pool ipcp_agg_lattice_pool
;
260 /* Maximal count found in program. */
262 static gcov_type max_count
;
264 /* Original overall size of the program. */
266 static long overall_size
, max_new_size
;
268 /* Head of the linked list of topologically sorted values. */
270 static struct ipcp_value
*values_topo
;
272 /* Return the param lattices structure corresponding to the Ith formal
273 parameter of the function described by INFO. */
274 static inline struct ipcp_param_lattices
*
275 ipa_get_parm_lattices (struct ipa_node_params
*info
, int i
)
277 gcc_assert (i
>= 0 && i
< ipa_get_param_count (info
));
278 gcc_checking_assert (!info
->ipcp_orig_node
);
279 gcc_checking_assert (info
->lattices
);
280 return &(info
->lattices
[i
]);
283 /* Return the lattice corresponding to the scalar value of the Ith formal
284 parameter of the function described by INFO. */
285 static inline struct ipcp_lattice
*
286 ipa_get_scalar_lat (struct ipa_node_params
*info
, int i
)
288 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
289 return &plats
->itself
;
292 /* Return whether LAT is a lattice with a single constant and without an
296 ipa_lat_is_single_const (struct ipcp_lattice
*lat
)
299 || lat
->contains_variable
300 || lat
->values_count
!= 1)
306 /* Print V which is extracted from a value in a lattice to F. */
309 print_ipcp_constant_value (FILE * f
, tree v
)
311 if (TREE_CODE (v
) == TREE_BINFO
)
313 fprintf (f
, "BINFO ");
314 print_generic_expr (f
, BINFO_TYPE (v
), 0);
316 else if (TREE_CODE (v
) == ADDR_EXPR
317 && TREE_CODE (TREE_OPERAND (v
, 0)) == CONST_DECL
)
320 print_generic_expr (f
, DECL_INITIAL (TREE_OPERAND (v
, 0)), 0);
323 print_generic_expr (f
, v
, 0);
326 /* Print a lattice LAT to F. */
329 print_lattice (FILE * f
, struct ipcp_lattice
*lat
,
330 bool dump_sources
, bool dump_benefits
)
332 struct ipcp_value
*val
;
337 fprintf (f
, "BOTTOM\n");
341 if (!lat
->values_count
&& !lat
->contains_variable
)
343 fprintf (f
, "TOP\n");
347 if (lat
->contains_variable
)
349 fprintf (f
, "VARIABLE");
355 for (val
= lat
->values
; val
; val
= val
->next
)
357 if (dump_benefits
&& prev
)
359 else if (!dump_benefits
&& prev
)
364 print_ipcp_constant_value (f
, val
->value
);
368 struct ipcp_value_source
*s
;
370 fprintf (f
, " [from:");
371 for (s
= val
->sources
; s
; s
= s
->next
)
372 fprintf (f
, " %i(%i)", s
->cs
->caller
->order
,
378 fprintf (f
, " [loc_time: %i, loc_size: %i, "
379 "prop_time: %i, prop_size: %i]\n",
380 val
->local_time_benefit
, val
->local_size_cost
,
381 val
->prop_time_benefit
, val
->prop_size_cost
);
387 /* Print all ipcp_lattices of all functions to F. */
390 print_all_lattices (FILE * f
, bool dump_sources
, bool dump_benefits
)
392 struct cgraph_node
*node
;
395 fprintf (f
, "\nLattices:\n");
396 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
398 struct ipa_node_params
*info
;
400 info
= IPA_NODE_REF (node
);
401 fprintf (f
, " Node: %s/%i:\n", node
->name (),
403 count
= ipa_get_param_count (info
);
404 for (i
= 0; i
< count
; i
++)
406 struct ipcp_agg_lattice
*aglat
;
407 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
408 fprintf (f
, " param [%d]: ", i
);
409 print_lattice (f
, &plats
->itself
, dump_sources
, dump_benefits
);
411 if (plats
->virt_call
)
412 fprintf (f
, " virt_call flag set\n");
414 if (plats
->aggs_bottom
)
416 fprintf (f
, " AGGS BOTTOM\n");
419 if (plats
->aggs_contain_variable
)
420 fprintf (f
, " AGGS VARIABLE\n");
421 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
423 fprintf (f
, " %soffset " HOST_WIDE_INT_PRINT_DEC
": ",
424 plats
->aggs_by_ref
? "ref " : "", aglat
->offset
);
425 print_lattice (f
, aglat
, dump_sources
, dump_benefits
);
431 /* Determine whether it is at all technically possible to create clones of NODE
432 and store this information in the ipa_node_params structure associated
436 determine_versionability (struct cgraph_node
*node
)
438 const char *reason
= NULL
;
440 /* There are a number of generic reasons functions cannot be versioned. We
441 also cannot remove parameters if there are type attributes such as fnspec
443 if (node
->alias
|| node
->thunk
.thunk_p
)
444 reason
= "alias or thunk";
445 else if (!node
->local
.versionable
)
446 reason
= "not a tree_versionable_function";
447 else if (node
->get_availability () <= AVAIL_INTERPOSABLE
)
448 reason
= "insufficient body availability";
449 else if (!opt_for_fn (node
->decl
, optimize
)
450 || !opt_for_fn (node
->decl
, flag_ipa_cp
))
451 reason
= "non-optimized function";
452 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node
->decl
)))
454 /* Ideally we should clone the SIMD clones themselves and create
455 vector copies of them, so IPA-cp and SIMD clones can happily
456 coexist, but that may not be worth the effort. */
457 reason
= "function has SIMD clones";
459 /* Don't clone decls local to a comdat group; it breaks and for C++
460 decloned constructors, inlining is always better anyway. */
461 else if (node
->comdat_local_p ())
462 reason
= "comdat-local function";
464 if (reason
&& dump_file
&& !node
->alias
&& !node
->thunk
.thunk_p
)
465 fprintf (dump_file
, "Function %s/%i is not versionable, reason: %s.\n",
466 node
->name (), node
->order
, reason
);
468 node
->local
.versionable
= (reason
== NULL
);
471 /* Return true if it is at all technically possible to create clones of a
475 ipcp_versionable_function_p (struct cgraph_node
*node
)
477 return node
->local
.versionable
;
480 /* Structure holding accumulated information about callers of a node. */
482 struct caller_statistics
485 int n_calls
, n_hot_calls
, freq_sum
;
488 /* Initialize fields of STAT to zeroes. */
491 init_caller_stats (struct caller_statistics
*stats
)
493 stats
->count_sum
= 0;
495 stats
->n_hot_calls
= 0;
499 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
500 non-thunk incoming edges to NODE. */
503 gather_caller_stats (struct cgraph_node
*node
, void *data
)
505 struct caller_statistics
*stats
= (struct caller_statistics
*) data
;
506 struct cgraph_edge
*cs
;
508 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
509 if (cs
->caller
->thunk
.thunk_p
)
510 cs
->caller
->call_for_symbol_thunks_and_aliases (gather_caller_stats
,
514 stats
->count_sum
+= cs
->count
;
515 stats
->freq_sum
+= cs
->frequency
;
517 if (cs
->maybe_hot_p ())
518 stats
->n_hot_calls
++;
524 /* Return true if this NODE is viable candidate for cloning. */
527 ipcp_cloning_candidate_p (struct cgraph_node
*node
)
529 struct caller_statistics stats
;
531 gcc_checking_assert (node
->has_gimple_body_p ());
533 if (!flag_ipa_cp_clone
)
536 fprintf (dump_file
, "Not considering %s for cloning; "
537 "-fipa-cp-clone disabled.\n",
542 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node
->decl
)))
545 fprintf (dump_file
, "Not considering %s for cloning; "
546 "optimizing it for size.\n",
551 init_caller_stats (&stats
);
552 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
, false);
554 if (inline_summary (node
)->self_size
< stats
.n_calls
)
557 fprintf (dump_file
, "Considering %s for cloning; code might shrink.\n",
562 /* When profile is available and function is hot, propagate into it even if
563 calls seems cold; constant propagation can improve function's speed
567 if (stats
.count_sum
> node
->count
* 90 / 100)
570 fprintf (dump_file
, "Considering %s for cloning; "
571 "usually called directly.\n",
576 if (!stats
.n_hot_calls
)
579 fprintf (dump_file
, "Not considering %s for cloning; no hot calls.\n",
584 fprintf (dump_file
, "Considering %s for cloning.\n",
589 /* Arrays representing a topological ordering of call graph nodes and a stack
590 of noes used during constant propagation. */
594 struct cgraph_node
**order
;
595 struct cgraph_node
**stack
;
596 int nnodes
, stack_top
;
599 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
602 build_toporder_info (struct ipa_topo_info
*topo
)
604 topo
->order
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
605 topo
->stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
608 topo
->nnodes
= ipa_reduced_postorder (topo
->order
, true, true, NULL
);
611 /* Free information about strongly connected components and the arrays in
615 free_toporder_info (struct ipa_topo_info
*topo
)
617 ipa_free_postorder_info ();
622 /* Add NODE to the stack in TOPO, unless it is already there. */
625 push_node_to_stack (struct ipa_topo_info
*topo
, struct cgraph_node
*node
)
627 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
628 if (info
->node_enqueued
)
630 info
->node_enqueued
= 1;
631 topo
->stack
[topo
->stack_top
++] = node
;
634 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
637 static struct cgraph_node
*
638 pop_node_from_stack (struct ipa_topo_info
*topo
)
642 struct cgraph_node
*node
;
644 node
= topo
->stack
[topo
->stack_top
];
645 IPA_NODE_REF (node
)->node_enqueued
= 0;
652 /* Set lattice LAT to bottom and return true if it previously was not set as
656 set_lattice_to_bottom (struct ipcp_lattice
*lat
)
658 bool ret
= !lat
->bottom
;
663 /* Mark lattice as containing an unknown value and return true if it previously
664 was not marked as such. */
667 set_lattice_contains_variable (struct ipcp_lattice
*lat
)
669 bool ret
= !lat
->contains_variable
;
670 lat
->contains_variable
= true;
674 /* Set all aggegate lattices in PLATS to bottom and return true if they were
675 not previously set as such. */
678 set_agg_lats_to_bottom (struct ipcp_param_lattices
*plats
)
680 bool ret
= !plats
->aggs_bottom
;
681 plats
->aggs_bottom
= true;
685 /* Mark all aggegate lattices in PLATS as containing an unknown value and
686 return true if they were not previously marked as such. */
689 set_agg_lats_contain_variable (struct ipcp_param_lattices
*plats
)
691 bool ret
= !plats
->aggs_contain_variable
;
692 plats
->aggs_contain_variable
= true;
696 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
697 return true is any of them has not been marked as such so far. */
700 set_all_contains_variable (struct ipcp_param_lattices
*plats
)
702 bool ret
= !plats
->itself
.contains_variable
|| !plats
->aggs_contain_variable
;
703 plats
->itself
.contains_variable
= true;
704 plats
->aggs_contain_variable
= true;
708 /* Initialize ipcp_lattices. */
711 initialize_node_lattices (struct cgraph_node
*node
)
713 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
714 struct cgraph_edge
*ie
;
715 bool disable
= false, variable
= false;
718 gcc_checking_assert (node
->has_gimple_body_p ());
719 if (!node
->local
.local
)
721 /* When cloning is allowed, we can assume that externally visible
722 functions are not called. We will compensate this by cloning
724 if (ipcp_versionable_function_p (node
)
725 && ipcp_cloning_candidate_p (node
))
731 if (disable
|| variable
)
733 for (i
= 0; i
< ipa_get_param_count (info
) ; i
++)
735 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
738 set_lattice_to_bottom (&plats
->itself
);
739 set_agg_lats_to_bottom (plats
);
742 set_all_contains_variable (plats
);
744 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
745 && !node
->alias
&& !node
->thunk
.thunk_p
)
746 fprintf (dump_file
, "Marking all lattices of %s/%i as %s\n",
747 node
->name (), node
->order
,
748 disable
? "BOTTOM" : "VARIABLE");
751 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
752 if (ie
->indirect_info
->polymorphic
753 && ie
->indirect_info
->param_index
>= 0)
755 gcc_checking_assert (ie
->indirect_info
->param_index
>= 0);
756 ipa_get_parm_lattices (info
,
757 ie
->indirect_info
->param_index
)->virt_call
= 1;
761 /* Return the result of a (possibly arithmetic) pass through jump function
762 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
763 determined or be considered an interprocedural invariant. */
766 ipa_get_jf_pass_through_result (struct ipa_jump_func
*jfunc
, tree input
)
770 if (TREE_CODE (input
) == TREE_BINFO
)
772 if (ipa_get_jf_pass_through_type_preserved (jfunc
))
774 gcc_checking_assert (ipa_get_jf_pass_through_operation (jfunc
)
781 if (ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
784 gcc_checking_assert (is_gimple_ip_invariant (input
));
785 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc
))
787 restype
= boolean_type_node
;
789 restype
= TREE_TYPE (input
);
790 res
= fold_binary (ipa_get_jf_pass_through_operation (jfunc
), restype
,
791 input
, ipa_get_jf_pass_through_operand (jfunc
));
793 if (res
&& !is_gimple_ip_invariant (res
))
799 /* Return the result of an ancestor jump function JFUNC on the constant value
800 INPUT. Return NULL_TREE if that cannot be determined. */
803 ipa_get_jf_ancestor_result (struct ipa_jump_func
*jfunc
, tree input
)
805 if (TREE_CODE (input
) == TREE_BINFO
)
807 if (!ipa_get_jf_ancestor_type_preserved (jfunc
))
809 /* FIXME: At LTO we can't propagate to non-polymorphic type, because
810 we have no ODR equivalency on those. This should be fixed by
811 propagating on types rather than binfos that would make type
812 matching here unnecesary. */
814 && (TREE_CODE (ipa_get_jf_ancestor_type (jfunc
)) != RECORD_TYPE
815 || !TYPE_BINFO (ipa_get_jf_ancestor_type (jfunc
))
816 || !BINFO_VTABLE (TYPE_BINFO (ipa_get_jf_ancestor_type (jfunc
)))))
818 if (!ipa_get_jf_ancestor_offset (jfunc
))
822 return get_binfo_at_offset (input
,
823 ipa_get_jf_ancestor_offset (jfunc
),
824 ipa_get_jf_ancestor_type (jfunc
));
826 else if (TREE_CODE (input
) == ADDR_EXPR
)
828 tree t
= TREE_OPERAND (input
, 0);
829 t
= build_ref_for_offset (EXPR_LOCATION (t
), t
,
830 ipa_get_jf_ancestor_offset (jfunc
),
831 ipa_get_jf_ancestor_type (jfunc
)
832 ? ipa_get_jf_ancestor_type (jfunc
)
833 : ptr_type_node
, NULL
, false);
834 return build_fold_addr_expr (t
);
840 /* Determine whether JFUNC evaluates to a known value (that is either a
841 constant or a binfo) and if so, return it. Otherwise return NULL. INFO
842 describes the caller node so that pass-through jump functions can be
846 ipa_value_from_jfunc (struct ipa_node_params
*info
, struct ipa_jump_func
*jfunc
)
848 if (jfunc
->type
== IPA_JF_CONST
)
849 return ipa_get_jf_constant (jfunc
);
850 else if (jfunc
->type
== IPA_JF_KNOWN_TYPE
)
851 return ipa_binfo_from_known_type_jfunc (jfunc
);
852 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
853 || jfunc
->type
== IPA_JF_ANCESTOR
)
858 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
859 idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
861 idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
863 if (info
->ipcp_orig_node
)
864 input
= info
->known_vals
[idx
];
867 struct ipcp_lattice
*lat
;
871 gcc_checking_assert (!flag_ipa_cp
);
874 lat
= ipa_get_scalar_lat (info
, idx
);
875 if (!ipa_lat_is_single_const (lat
))
877 input
= lat
->values
->value
;
883 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
884 return ipa_get_jf_pass_through_result (jfunc
, input
);
886 return ipa_get_jf_ancestor_result (jfunc
, input
);
893 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
894 bottom, not containing a variable component and without any known value at
898 ipcp_verify_propagated_values (void)
900 struct cgraph_node
*node
;
902 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
904 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
905 int i
, count
= ipa_get_param_count (info
);
907 for (i
= 0; i
< count
; i
++)
909 struct ipcp_lattice
*lat
= ipa_get_scalar_lat (info
, i
);
912 && !lat
->contains_variable
913 && lat
->values_count
== 0)
917 symtab_node::dump_table (dump_file
);
918 fprintf (dump_file
, "\nIPA lattices after constant "
919 "propagation, before gcc_unreachable:\n");
920 print_all_lattices (dump_file
, true, false);
929 /* Return true iff X and Y should be considered equal values by IPA-CP. */
932 values_equal_for_ipcp_p (tree x
, tree y
)
934 gcc_checking_assert (x
!= NULL_TREE
&& y
!= NULL_TREE
);
939 if (TREE_CODE (x
) == TREE_BINFO
|| TREE_CODE (y
) == TREE_BINFO
)
942 if (TREE_CODE (x
) == ADDR_EXPR
943 && TREE_CODE (y
) == ADDR_EXPR
944 && TREE_CODE (TREE_OPERAND (x
, 0)) == CONST_DECL
945 && TREE_CODE (TREE_OPERAND (y
, 0)) == CONST_DECL
)
946 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x
, 0)),
947 DECL_INITIAL (TREE_OPERAND (y
, 0)), 0);
949 return operand_equal_p (x
, y
, 0);
952 /* Add a new value source to VAL, marking that a value comes from edge CS and
953 (if the underlying jump function is a pass-through or an ancestor one) from
954 a caller value SRC_VAL of a caller parameter described by SRC_INDEX. OFFSET
955 is negative if the source was the scalar value of the parameter itself or
956 the offset within an aggregate. */
959 add_value_source (struct ipcp_value
*val
, struct cgraph_edge
*cs
,
960 struct ipcp_value
*src_val
, int src_idx
, HOST_WIDE_INT offset
)
962 struct ipcp_value_source
*src
;
964 src
= (struct ipcp_value_source
*) pool_alloc (ipcp_sources_pool
);
965 src
->offset
= offset
;
968 src
->index
= src_idx
;
970 src
->next
= val
->sources
;
974 /* Try to add NEWVAL to LAT, potentially creating a new struct ipcp_value for
975 it. CS, SRC_VAL SRC_INDEX and OFFSET are meant for add_value_source and
976 have the same meaning. */
979 add_value_to_lattice (struct ipcp_lattice
*lat
, tree newval
,
980 struct cgraph_edge
*cs
, struct ipcp_value
*src_val
,
981 int src_idx
, HOST_WIDE_INT offset
)
983 struct ipcp_value
*val
;
988 for (val
= lat
->values
; val
; val
= val
->next
)
989 if (values_equal_for_ipcp_p (val
->value
, newval
))
991 if (ipa_edge_within_scc (cs
))
993 struct ipcp_value_source
*s
;
994 for (s
= val
->sources
; s
; s
= s
->next
)
1001 add_value_source (val
, cs
, src_val
, src_idx
, offset
);
1005 if (lat
->values_count
== PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE
))
1007 /* We can only free sources, not the values themselves, because sources
1008 of other values in this this SCC might point to them. */
1009 for (val
= lat
->values
; val
; val
= val
->next
)
1011 while (val
->sources
)
1013 struct ipcp_value_source
*src
= val
->sources
;
1014 val
->sources
= src
->next
;
1015 pool_free (ipcp_sources_pool
, src
);
1020 return set_lattice_to_bottom (lat
);
1023 lat
->values_count
++;
1024 val
= (struct ipcp_value
*) pool_alloc (ipcp_values_pool
);
1025 memset (val
, 0, sizeof (*val
));
1027 add_value_source (val
, cs
, src_val
, src_idx
, offset
);
1028 val
->value
= newval
;
1029 val
->next
= lat
->values
;
1034 /* Like above but passes a special value of offset to distinguish that the
1035 origin is the scalar value of the parameter rather than a part of an
1039 add_scalar_value_to_lattice (struct ipcp_lattice
*lat
, tree newval
,
1040 struct cgraph_edge
*cs
,
1041 struct ipcp_value
*src_val
, int src_idx
)
1043 return add_value_to_lattice (lat
, newval
, cs
, src_val
, src_idx
, -1);
1046 /* Propagate values through a pass-through jump function JFUNC associated with
1047 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1048 is the index of the source parameter. */
1051 propagate_vals_accross_pass_through (struct cgraph_edge
*cs
,
1052 struct ipa_jump_func
*jfunc
,
1053 struct ipcp_lattice
*src_lat
,
1054 struct ipcp_lattice
*dest_lat
,
1057 struct ipcp_value
*src_val
;
1060 /* Do not create new values when propagating within an SCC because if there
1061 are arithmetic functions with circular dependencies, there is infinite
1062 number of them and we would just make lattices bottom. */
1063 if ((ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1064 && ipa_edge_within_scc (cs
))
1065 ret
= set_lattice_contains_variable (dest_lat
);
1067 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1069 tree cstval
= ipa_get_jf_pass_through_result (jfunc
, src_val
->value
);
1072 ret
|= add_scalar_value_to_lattice (dest_lat
, cstval
, cs
, src_val
,
1075 ret
|= set_lattice_contains_variable (dest_lat
);
1081 /* Propagate values through an ancestor jump function JFUNC associated with
1082 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1083 is the index of the source parameter. */
1086 propagate_vals_accross_ancestor (struct cgraph_edge
*cs
,
1087 struct ipa_jump_func
*jfunc
,
1088 struct ipcp_lattice
*src_lat
,
1089 struct ipcp_lattice
*dest_lat
,
1092 struct ipcp_value
*src_val
;
1095 if (ipa_edge_within_scc (cs
))
1096 return set_lattice_contains_variable (dest_lat
);
1098 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1100 tree t
= ipa_get_jf_ancestor_result (jfunc
, src_val
->value
);
1103 ret
|= add_scalar_value_to_lattice (dest_lat
, t
, cs
, src_val
, src_idx
);
1105 ret
|= set_lattice_contains_variable (dest_lat
);
1111 /* Propagate scalar values across jump function JFUNC that is associated with
1112 edge CS and put the values into DEST_LAT. */
1115 propagate_scalar_accross_jump_function (struct cgraph_edge
*cs
,
1116 struct ipa_jump_func
*jfunc
,
1117 struct ipcp_lattice
*dest_lat
)
1119 if (dest_lat
->bottom
)
1122 if (jfunc
->type
== IPA_JF_CONST
1123 || jfunc
->type
== IPA_JF_KNOWN_TYPE
)
1127 if (jfunc
->type
== IPA_JF_KNOWN_TYPE
)
1129 val
= ipa_binfo_from_known_type_jfunc (jfunc
);
1131 return set_lattice_contains_variable (dest_lat
);
1134 val
= ipa_get_jf_constant (jfunc
);
1135 return add_scalar_value_to_lattice (dest_lat
, val
, cs
, NULL
, 0);
1137 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
1138 || jfunc
->type
== IPA_JF_ANCESTOR
)
1140 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1141 struct ipcp_lattice
*src_lat
;
1145 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1146 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1148 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1150 src_lat
= ipa_get_scalar_lat (caller_info
, src_idx
);
1151 if (src_lat
->bottom
)
1152 return set_lattice_contains_variable (dest_lat
);
1154 /* If we would need to clone the caller and cannot, do not propagate. */
1155 if (!ipcp_versionable_function_p (cs
->caller
)
1156 && (src_lat
->contains_variable
1157 || (src_lat
->values_count
> 1)))
1158 return set_lattice_contains_variable (dest_lat
);
1160 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1161 ret
= propagate_vals_accross_pass_through (cs
, jfunc
, src_lat
,
1164 ret
= propagate_vals_accross_ancestor (cs
, jfunc
, src_lat
, dest_lat
,
1167 if (src_lat
->contains_variable
)
1168 ret
|= set_lattice_contains_variable (dest_lat
);
1173 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1174 use it for indirect inlining), we should propagate them too. */
1175 return set_lattice_contains_variable (dest_lat
);
1178 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1179 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1180 other cases, return false). If there are no aggregate items, set
1181 aggs_by_ref to NEW_AGGS_BY_REF. */
1184 set_check_aggs_by_ref (struct ipcp_param_lattices
*dest_plats
,
1185 bool new_aggs_by_ref
)
1187 if (dest_plats
->aggs
)
1189 if (dest_plats
->aggs_by_ref
!= new_aggs_by_ref
)
1191 set_agg_lats_to_bottom (dest_plats
);
1196 dest_plats
->aggs_by_ref
= new_aggs_by_ref
;
1200 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1201 already existing lattice for the given OFFSET and SIZE, marking all skipped
1202 lattices as containing variable and checking for overlaps. If there is no
1203 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1204 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1205 unless there are too many already. If there are two many, return false. If
1206 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1207 skipped lattices were newly marked as containing variable, set *CHANGE to
1211 merge_agg_lats_step (struct ipcp_param_lattices
*dest_plats
,
1212 HOST_WIDE_INT offset
, HOST_WIDE_INT val_size
,
1213 struct ipcp_agg_lattice
***aglat
,
1214 bool pre_existing
, bool *change
)
1216 gcc_checking_assert (offset
>= 0);
1218 while (**aglat
&& (**aglat
)->offset
< offset
)
1220 if ((**aglat
)->offset
+ (**aglat
)->size
> offset
)
1222 set_agg_lats_to_bottom (dest_plats
);
1225 *change
|= set_lattice_contains_variable (**aglat
);
1226 *aglat
= &(**aglat
)->next
;
1229 if (**aglat
&& (**aglat
)->offset
== offset
)
1231 if ((**aglat
)->size
!= val_size
1233 && (**aglat
)->next
->offset
< offset
+ val_size
))
1235 set_agg_lats_to_bottom (dest_plats
);
1238 gcc_checking_assert (!(**aglat
)->next
1239 || (**aglat
)->next
->offset
>= offset
+ val_size
);
1244 struct ipcp_agg_lattice
*new_al
;
1246 if (**aglat
&& (**aglat
)->offset
< offset
+ val_size
)
1248 set_agg_lats_to_bottom (dest_plats
);
1251 if (dest_plats
->aggs_count
== PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS
))
1253 dest_plats
->aggs_count
++;
1254 new_al
= (struct ipcp_agg_lattice
*) pool_alloc (ipcp_agg_lattice_pool
);
1255 memset (new_al
, 0, sizeof (*new_al
));
1257 new_al
->offset
= offset
;
1258 new_al
->size
= val_size
;
1259 new_al
->contains_variable
= pre_existing
;
1261 new_al
->next
= **aglat
;
1267 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1268 containing an unknown value. */
1271 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice
*aglat
)
1276 ret
|= set_lattice_contains_variable (aglat
);
1277 aglat
= aglat
->next
;
1282 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1283 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1284 parameter used for lattice value sources. Return true if DEST_PLATS changed
1288 merge_aggregate_lattices (struct cgraph_edge
*cs
,
1289 struct ipcp_param_lattices
*dest_plats
,
1290 struct ipcp_param_lattices
*src_plats
,
1291 int src_idx
, HOST_WIDE_INT offset_delta
)
1293 bool pre_existing
= dest_plats
->aggs
!= NULL
;
1294 struct ipcp_agg_lattice
**dst_aglat
;
1297 if (set_check_aggs_by_ref (dest_plats
, src_plats
->aggs_by_ref
))
1299 if (src_plats
->aggs_bottom
)
1300 return set_agg_lats_contain_variable (dest_plats
);
1301 if (src_plats
->aggs_contain_variable
)
1302 ret
|= set_agg_lats_contain_variable (dest_plats
);
1303 dst_aglat
= &dest_plats
->aggs
;
1305 for (struct ipcp_agg_lattice
*src_aglat
= src_plats
->aggs
;
1307 src_aglat
= src_aglat
->next
)
1309 HOST_WIDE_INT new_offset
= src_aglat
->offset
- offset_delta
;
1313 if (merge_agg_lats_step (dest_plats
, new_offset
, src_aglat
->size
,
1314 &dst_aglat
, pre_existing
, &ret
))
1316 struct ipcp_agg_lattice
*new_al
= *dst_aglat
;
1318 dst_aglat
= &(*dst_aglat
)->next
;
1319 if (src_aglat
->bottom
)
1321 ret
|= set_lattice_contains_variable (new_al
);
1324 if (src_aglat
->contains_variable
)
1325 ret
|= set_lattice_contains_variable (new_al
);
1326 for (struct ipcp_value
*val
= src_aglat
->values
;
1329 ret
|= add_value_to_lattice (new_al
, val
->value
, cs
, val
, src_idx
,
1332 else if (dest_plats
->aggs_bottom
)
1335 ret
|= set_chain_of_aglats_contains_variable (*dst_aglat
);
1339 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1340 pass-through JFUNC and if so, whether it has conform and conforms to the
1341 rules about propagating values passed by reference. */
1344 agg_pass_through_permissible_p (struct ipcp_param_lattices
*src_plats
,
1345 struct ipa_jump_func
*jfunc
)
1347 return src_plats
->aggs
1348 && (!src_plats
->aggs_by_ref
1349 || ipa_get_jf_pass_through_agg_preserved (jfunc
));
1352 /* Propagate scalar values across jump function JFUNC that is associated with
1353 edge CS and put the values into DEST_LAT. */
1356 propagate_aggs_accross_jump_function (struct cgraph_edge
*cs
,
1357 struct ipa_jump_func
*jfunc
,
1358 struct ipcp_param_lattices
*dest_plats
)
1362 if (dest_plats
->aggs_bottom
)
1365 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1366 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
1368 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1369 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1370 struct ipcp_param_lattices
*src_plats
;
1372 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
1373 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
1375 /* Currently we do not produce clobber aggregate jump
1376 functions, replace with merging when we do. */
1377 gcc_assert (!jfunc
->agg
.items
);
1378 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
,
1382 ret
|= set_agg_lats_contain_variable (dest_plats
);
1384 else if (jfunc
->type
== IPA_JF_ANCESTOR
1385 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
1387 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1388 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1389 struct ipcp_param_lattices
*src_plats
;
1391 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
1392 if (src_plats
->aggs
&& src_plats
->aggs_by_ref
)
1394 /* Currently we do not produce clobber aggregate jump
1395 functions, replace with merging when we do. */
1396 gcc_assert (!jfunc
->agg
.items
);
1397 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
, src_idx
,
1398 ipa_get_jf_ancestor_offset (jfunc
));
1400 else if (!src_plats
->aggs_by_ref
)
1401 ret
|= set_agg_lats_to_bottom (dest_plats
);
1403 ret
|= set_agg_lats_contain_variable (dest_plats
);
1405 else if (jfunc
->agg
.items
)
1407 bool pre_existing
= dest_plats
->aggs
!= NULL
;
1408 struct ipcp_agg_lattice
**aglat
= &dest_plats
->aggs
;
1409 struct ipa_agg_jf_item
*item
;
1412 if (set_check_aggs_by_ref (dest_plats
, jfunc
->agg
.by_ref
))
1415 FOR_EACH_VEC_ELT (*jfunc
->agg
.items
, i
, item
)
1417 HOST_WIDE_INT val_size
;
1419 if (item
->offset
< 0)
1421 gcc_checking_assert (is_gimple_ip_invariant (item
->value
));
1422 val_size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item
->value
)));
1424 if (merge_agg_lats_step (dest_plats
, item
->offset
, val_size
,
1425 &aglat
, pre_existing
, &ret
))
1427 ret
|= add_value_to_lattice (*aglat
, item
->value
, cs
, NULL
, 0, 0);
1428 aglat
= &(*aglat
)->next
;
1430 else if (dest_plats
->aggs_bottom
)
1434 ret
|= set_chain_of_aglats_contains_variable (*aglat
);
1437 ret
|= set_agg_lats_contain_variable (dest_plats
);
1442 /* Propagate constants from the caller to the callee of CS. INFO describes the
1446 propagate_constants_accross_call (struct cgraph_edge
*cs
)
1448 struct ipa_node_params
*callee_info
;
1449 enum availability availability
;
1450 struct cgraph_node
*callee
, *alias_or_thunk
;
1451 struct ipa_edge_args
*args
;
1453 int i
, args_count
, parms_count
;
1455 callee
= cs
->callee
->function_symbol (&availability
);
1456 if (!callee
->definition
)
1458 gcc_checking_assert (callee
->has_gimple_body_p ());
1459 callee_info
= IPA_NODE_REF (callee
);
1461 args
= IPA_EDGE_REF (cs
);
1462 args_count
= ipa_get_cs_argument_count (args
);
1463 parms_count
= ipa_get_param_count (callee_info
);
1464 if (parms_count
== 0)
1467 /* If this call goes through a thunk we must not propagate to the first (0th)
1468 parameter. However, we might need to uncover a thunk from below a series
1469 of aliases first. */
1470 alias_or_thunk
= cs
->callee
;
1471 while (alias_or_thunk
->alias
)
1472 alias_or_thunk
= alias_or_thunk
->get_alias_target ();
1473 if (alias_or_thunk
->thunk
.thunk_p
)
1475 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
,
1482 for (; (i
< args_count
) && (i
< parms_count
); i
++)
1484 struct ipa_jump_func
*jump_func
= ipa_get_ith_jump_func (args
, i
);
1485 struct ipcp_param_lattices
*dest_plats
;
1487 dest_plats
= ipa_get_parm_lattices (callee_info
, i
);
1488 if (availability
== AVAIL_INTERPOSABLE
)
1489 ret
|= set_all_contains_variable (dest_plats
);
1492 ret
|= propagate_scalar_accross_jump_function (cs
, jump_func
,
1493 &dest_plats
->itself
);
1494 ret
|= propagate_aggs_accross_jump_function (cs
, jump_func
,
1498 for (; i
< parms_count
; i
++)
1499 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
, i
));
1504 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1505 (which can contain both constants and binfos), KNOWN_BINFOS, KNOWN_AGGS or
1506 AGG_REPS return the destination. The latter three can be NULL. If AGG_REPS
1507 is not NULL, KNOWN_AGGS is ignored. */
1510 ipa_get_indirect_edge_target_1 (struct cgraph_edge
*ie
,
1511 vec
<tree
> known_vals
,
1512 vec
<tree
> known_binfos
,
1513 vec
<ipa_agg_jump_function_p
> known_aggs
,
1514 struct ipa_agg_replacement_value
*agg_reps
)
1516 int param_index
= ie
->indirect_info
->param_index
;
1517 HOST_WIDE_INT token
, anc_offset
;
1522 if (param_index
== -1
1523 || known_vals
.length () <= (unsigned int) param_index
)
1526 if (!ie
->indirect_info
->polymorphic
)
1530 if (ie
->indirect_info
->agg_contents
)
1537 if (agg_reps
->index
== param_index
1538 && agg_reps
->offset
== ie
->indirect_info
->offset
1539 && agg_reps
->by_ref
== ie
->indirect_info
->by_ref
)
1541 t
= agg_reps
->value
;
1544 agg_reps
= agg_reps
->next
;
1547 else if (known_aggs
.length () > (unsigned int) param_index
)
1549 struct ipa_agg_jump_function
*agg
;
1550 agg
= known_aggs
[param_index
];
1551 t
= ipa_find_agg_cst_for_param (agg
, ie
->indirect_info
->offset
,
1552 ie
->indirect_info
->by_ref
);
1558 t
= known_vals
[param_index
];
1561 TREE_CODE (t
) == ADDR_EXPR
1562 && TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
)
1563 return TREE_OPERAND (t
, 0);
1568 if (!flag_devirtualize
)
1571 gcc_assert (!ie
->indirect_info
->agg_contents
);
1572 token
= ie
->indirect_info
->otr_token
;
1573 anc_offset
= ie
->indirect_info
->offset
;
1574 otr_type
= ie
->indirect_info
->otr_type
;
1578 /* Try to work out value of virtual table pointer value in replacemnets. */
1579 if (!t
&& agg_reps
&& !ie
->indirect_info
->by_ref
1580 && !ie
->indirect_info
->vptr_changed
)
1584 if (agg_reps
->index
== param_index
1585 && agg_reps
->offset
== ie
->indirect_info
->offset
1586 && agg_reps
->by_ref
)
1588 t
= agg_reps
->value
;
1591 agg_reps
= agg_reps
->next
;
1595 /* Try to work out value of virtual table pointer value in known
1596 aggregate values. */
1597 if (!t
&& known_aggs
.length () > (unsigned int) param_index
1598 && !ie
->indirect_info
->by_ref
1599 && !ie
->indirect_info
->vptr_changed
)
1601 struct ipa_agg_jump_function
*agg
;
1602 agg
= known_aggs
[param_index
];
1603 t
= ipa_find_agg_cst_for_param (agg
, ie
->indirect_info
->offset
,
1607 /* If we found the virtual table pointer, lookup the target. */
1611 unsigned HOST_WIDE_INT offset
;
1612 if (vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
1614 target
= gimple_get_virt_method_for_vtable (ie
->indirect_info
->otr_token
,
1618 if ((TREE_CODE (TREE_TYPE (target
)) == FUNCTION_TYPE
1619 && DECL_FUNCTION_CODE (target
) == BUILT_IN_UNREACHABLE
)
1620 || !possible_polymorphic_call_target_p
1621 (ie
, cgraph_node::get (target
)))
1622 target
= ipa_impossible_devirt_target (ie
, target
);
1628 /* Did we work out BINFO via type propagation? */
1629 if (!t
&& known_binfos
.length () > (unsigned int) param_index
)
1630 t
= known_binfos
[param_index
];
1631 /* Or do we know the constant value of pointer? */
1633 t
= known_vals
[param_index
];
1637 if (TREE_CODE (t
) != TREE_BINFO
)
1639 ipa_polymorphic_call_context
context (t
, ie
->indirect_info
->otr_type
,
1641 vec
<cgraph_node
*>targets
;
1644 targets
= possible_polymorphic_call_targets
1645 (ie
->indirect_info
->otr_type
,
1646 ie
->indirect_info
->otr_token
,
1648 if (!final
|| targets
.length () > 1)
1650 if (targets
.length () == 1)
1651 target
= targets
[0]->decl
;
1653 target
= ipa_impossible_devirt_target (ie
, NULL_TREE
);
1659 binfo
= get_binfo_at_offset (t
, anc_offset
, otr_type
);
1662 target
= gimple_get_virt_method_for_binfo (token
, binfo
);
1665 if (target
&& !possible_polymorphic_call_target_p (ie
,
1666 cgraph_node::get (target
)))
1667 target
= ipa_impossible_devirt_target (ie
, target
);
1673 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1674 (which can contain both constants and binfos), KNOWN_BINFOS (which can be
1675 NULL) or KNOWN_AGGS (which also can be NULL) return the destination. */
1678 ipa_get_indirect_edge_target (struct cgraph_edge
*ie
,
1679 vec
<tree
> known_vals
,
1680 vec
<tree
> known_binfos
,
1681 vec
<ipa_agg_jump_function_p
> known_aggs
)
1683 return ipa_get_indirect_edge_target_1 (ie
, known_vals
, known_binfos
,
1687 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
1688 and KNOWN_BINFOS. */
1691 devirtualization_time_bonus (struct cgraph_node
*node
,
1692 vec
<tree
> known_csts
,
1693 vec
<tree
> known_binfos
,
1694 vec
<ipa_agg_jump_function_p
> known_aggs
)
1696 struct cgraph_edge
*ie
;
1699 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
1701 struct cgraph_node
*callee
;
1702 struct inline_summary
*isummary
;
1703 enum availability avail
;
1706 target
= ipa_get_indirect_edge_target (ie
, known_csts
, known_binfos
,
1711 /* Only bare minimum benefit for clearly un-inlineable targets. */
1713 callee
= cgraph_node::get (target
);
1714 if (!callee
|| !callee
->definition
)
1716 callee
= callee
->function_symbol (&avail
);
1717 if (avail
< AVAIL_AVAILABLE
)
1719 isummary
= inline_summary (callee
);
1720 if (!isummary
->inlinable
)
1723 /* FIXME: The values below need re-considering and perhaps also
1724 integrating into the cost metrics, at lest in some very basic way. */
1725 if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 4)
1727 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 2)
1729 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
1730 || DECL_DECLARED_INLINE_P (callee
->decl
))
1737 /* Return time bonus incurred because of HINTS. */
1740 hint_time_bonus (inline_hints hints
)
1743 if (hints
& (INLINE_HINT_loop_iterations
| INLINE_HINT_loop_stride
))
1744 result
+= PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS
);
1745 if (hints
& INLINE_HINT_array_index
)
1746 result
+= PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS
);
1750 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
1751 and SIZE_COST and with the sum of frequencies of incoming edges to the
1752 potential new clone in FREQUENCIES. */
1755 good_cloning_opportunity_p (struct cgraph_node
*node
, int time_benefit
,
1756 int freq_sum
, gcov_type count_sum
, int size_cost
)
1758 if (time_benefit
== 0
1759 || !flag_ipa_cp_clone
1760 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node
->decl
)))
1763 gcc_assert (size_cost
> 0);
1767 int factor
= (count_sum
* 1000) / max_count
;
1768 int64_t evaluation
= (((int64_t) time_benefit
* factor
)
1771 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1772 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
1773 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
1774 ") -> evaluation: " "%"PRId64
1775 ", threshold: %i\n",
1776 time_benefit
, size_cost
, (HOST_WIDE_INT
) count_sum
,
1777 evaluation
, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
1779 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
1783 int64_t evaluation
= (((int64_t) time_benefit
* freq_sum
)
1786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1787 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
1788 "size: %i, freq_sum: %i) -> evaluation: "
1789 "%"PRId64
", threshold: %i\n",
1790 time_benefit
, size_cost
, freq_sum
, evaluation
,
1791 PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
1793 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
1797 /* Return all context independent values from aggregate lattices in PLATS in a
1798 vector. Return NULL if there are none. */
1800 static vec
<ipa_agg_jf_item
, va_gc
> *
1801 context_independent_aggregate_values (struct ipcp_param_lattices
*plats
)
1803 vec
<ipa_agg_jf_item
, va_gc
> *res
= NULL
;
1805 if (plats
->aggs_bottom
1806 || plats
->aggs_contain_variable
1807 || plats
->aggs_count
== 0)
1810 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
;
1812 aglat
= aglat
->next
)
1813 if (ipa_lat_is_single_const (aglat
))
1815 struct ipa_agg_jf_item item
;
1816 item
.offset
= aglat
->offset
;
1817 item
.value
= aglat
->values
->value
;
1818 vec_safe_push (res
, item
);
1823 /* Allocate KNOWN_CSTS, KNOWN_BINFOS and, if non-NULL, KNOWN_AGGS and populate
1824 them with values of parameters that are known independent of the context.
1825 INFO describes the function. If REMOVABLE_PARAMS_COST is non-NULL, the
1826 movement cost of all removable parameters will be stored in it. */
1829 gather_context_independent_values (struct ipa_node_params
*info
,
1830 vec
<tree
> *known_csts
,
1831 vec
<tree
> *known_binfos
,
1832 vec
<ipa_agg_jump_function
> *known_aggs
,
1833 int *removable_params_cost
)
1835 int i
, count
= ipa_get_param_count (info
);
1838 known_csts
->create (0);
1839 known_binfos
->create (0);
1840 known_csts
->safe_grow_cleared (count
);
1841 known_binfos
->safe_grow_cleared (count
);
1844 known_aggs
->create (0);
1845 known_aggs
->safe_grow_cleared (count
);
1848 if (removable_params_cost
)
1849 *removable_params_cost
= 0;
1851 for (i
= 0; i
< count
; i
++)
1853 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
1854 struct ipcp_lattice
*lat
= &plats
->itself
;
1856 if (ipa_lat_is_single_const (lat
))
1858 struct ipcp_value
*val
= lat
->values
;
1859 if (TREE_CODE (val
->value
) != TREE_BINFO
)
1861 (*known_csts
)[i
] = val
->value
;
1862 if (removable_params_cost
)
1863 *removable_params_cost
1864 += estimate_move_cost (TREE_TYPE (val
->value
), false);
1867 else if (plats
->virt_call
)
1869 (*known_binfos
)[i
] = val
->value
;
1872 else if (removable_params_cost
1873 && !ipa_is_param_used (info
, i
))
1874 *removable_params_cost
+= ipa_get_param_move_cost (info
, i
);
1876 else if (removable_params_cost
1877 && !ipa_is_param_used (info
, i
))
1878 *removable_params_cost
1879 += ipa_get_param_move_cost (info
, i
);
1883 vec
<ipa_agg_jf_item
, va_gc
> *agg_items
;
1884 struct ipa_agg_jump_function
*ajf
;
1886 agg_items
= context_independent_aggregate_values (plats
);
1887 ajf
= &(*known_aggs
)[i
];
1888 ajf
->items
= agg_items
;
1889 ajf
->by_ref
= plats
->aggs_by_ref
;
1890 ret
|= agg_items
!= NULL
;
1897 /* The current interface in ipa-inline-analysis requires a pointer vector.
1900 FIXME: That interface should be re-worked, this is slightly silly. Still,
1901 I'd like to discuss how to change it first and this demonstrates the
1904 static vec
<ipa_agg_jump_function_p
>
1905 agg_jmp_p_vec_for_t_vec (vec
<ipa_agg_jump_function
> known_aggs
)
1907 vec
<ipa_agg_jump_function_p
> ret
;
1908 struct ipa_agg_jump_function
*ajf
;
1911 ret
.create (known_aggs
.length ());
1912 FOR_EACH_VEC_ELT (known_aggs
, i
, ajf
)
1913 ret
.quick_push (ajf
);
1917 /* Iterate over known values of parameters of NODE and estimate the local
1918 effects in terms of time and size they have. */
1921 estimate_local_effects (struct cgraph_node
*node
)
1923 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
1924 int i
, count
= ipa_get_param_count (info
);
1925 vec
<tree
> known_csts
, known_binfos
;
1926 vec
<ipa_agg_jump_function
> known_aggs
;
1927 vec
<ipa_agg_jump_function_p
> known_aggs_ptrs
;
1929 int base_time
= inline_summary (node
)->time
;
1930 int removable_params_cost
;
1932 if (!count
|| !ipcp_versionable_function_p (node
))
1935 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1936 fprintf (dump_file
, "\nEstimating effects for %s/%i, base_time: %i.\n",
1937 node
->name (), node
->order
, base_time
);
1939 always_const
= gather_context_independent_values (info
, &known_csts
,
1940 &known_binfos
, &known_aggs
,
1941 &removable_params_cost
);
1942 known_aggs_ptrs
= agg_jmp_p_vec_for_t_vec (known_aggs
);
1945 struct caller_statistics stats
;
1949 init_caller_stats (&stats
);
1950 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
1952 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_binfos
,
1953 known_aggs_ptrs
, &size
, &time
, &hints
);
1954 time
-= devirtualization_time_bonus (node
, known_csts
, known_binfos
,
1956 time
-= hint_time_bonus (hints
);
1957 time
-= removable_params_cost
;
1958 size
-= stats
.n_calls
* removable_params_cost
;
1961 fprintf (dump_file
, " - context independent values, size: %i, "
1962 "time_benefit: %i\n", size
, base_time
- time
);
1965 || node
->will_be_removed_from_program_if_no_direct_calls_p ())
1967 info
->do_clone_for_all_contexts
= true;
1971 fprintf (dump_file
, " Decided to specialize for all "
1972 "known contexts, code not going to grow.\n");
1974 else if (good_cloning_opportunity_p (node
, base_time
- time
,
1975 stats
.freq_sum
, stats
.count_sum
,
1978 if (size
+ overall_size
<= max_new_size
)
1980 info
->do_clone_for_all_contexts
= true;
1982 overall_size
+= size
;
1985 fprintf (dump_file
, " Decided to specialize for all "
1986 "known contexts, growth deemed beneficial.\n");
1988 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1989 fprintf (dump_file
, " Not cloning for all contexts because "
1990 "max_new_size would be reached with %li.\n",
1991 size
+ overall_size
);
1995 for (i
= 0; i
< count
; i
++)
1997 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
1998 struct ipcp_lattice
*lat
= &plats
->itself
;
1999 struct ipcp_value
*val
;
2008 for (val
= lat
->values
; val
; val
= val
->next
)
2010 int time
, size
, time_benefit
;
2013 if (TREE_CODE (val
->value
) != TREE_BINFO
)
2015 known_csts
[i
] = val
->value
;
2016 known_binfos
[i
] = NULL_TREE
;
2017 emc
= estimate_move_cost (TREE_TYPE (val
->value
), true);
2019 else if (plats
->virt_call
)
2021 known_csts
[i
] = NULL_TREE
;
2022 known_binfos
[i
] = val
->value
;
2028 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_binfos
,
2029 known_aggs_ptrs
, &size
, &time
,
2031 time_benefit
= base_time
- time
2032 + devirtualization_time_bonus (node
, known_csts
, known_binfos
,
2034 + hint_time_bonus (hints
)
2035 + removable_params_cost
+ emc
;
2037 gcc_checking_assert (size
>=0);
2038 /* The inliner-heuristics based estimates may think that in certain
2039 contexts some functions do not have any size at all but we want
2040 all specializations to have at least a tiny cost, not least not to
2045 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2047 fprintf (dump_file
, " - estimates for value ");
2048 print_ipcp_constant_value (dump_file
, val
->value
);
2049 fprintf (dump_file
, " for ");
2050 ipa_dump_param (dump_file
, info
, i
);
2051 fprintf (dump_file
, ": time_benefit: %i, size: %i\n",
2052 time_benefit
, size
);
2055 val
->local_time_benefit
= time_benefit
;
2056 val
->local_size_cost
= size
;
2058 known_binfos
[i
] = NULL_TREE
;
2059 known_csts
[i
] = NULL_TREE
;
2062 for (i
= 0; i
< count
; i
++)
2064 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2065 struct ipa_agg_jump_function
*ajf
;
2066 struct ipcp_agg_lattice
*aglat
;
2068 if (plats
->aggs_bottom
|| !plats
->aggs
)
2071 ajf
= &known_aggs
[i
];
2072 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2074 struct ipcp_value
*val
;
2075 if (aglat
->bottom
|| !aglat
->values
2076 /* If the following is true, the one value is in known_aggs. */
2077 || (!plats
->aggs_contain_variable
2078 && ipa_lat_is_single_const (aglat
)))
2081 for (val
= aglat
->values
; val
; val
= val
->next
)
2083 int time
, size
, time_benefit
;
2084 struct ipa_agg_jf_item item
;
2087 item
.offset
= aglat
->offset
;
2088 item
.value
= val
->value
;
2089 vec_safe_push (ajf
->items
, item
);
2091 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_binfos
,
2092 known_aggs_ptrs
, &size
, &time
,
2094 time_benefit
= base_time
- time
2095 + devirtualization_time_bonus (node
, known_csts
, known_binfos
,
2097 + hint_time_bonus (hints
);
2098 gcc_checking_assert (size
>=0);
2102 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2104 fprintf (dump_file
, " - estimates for value ");
2105 print_ipcp_constant_value (dump_file
, val
->value
);
2106 fprintf (dump_file
, " for ");
2107 ipa_dump_param (dump_file
, info
, i
);
2108 fprintf (dump_file
, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2109 "]: time_benefit: %i, size: %i\n",
2110 plats
->aggs_by_ref
? "ref " : "",
2111 aglat
->offset
, time_benefit
, size
);
2114 val
->local_time_benefit
= time_benefit
;
2115 val
->local_size_cost
= size
;
2121 for (i
= 0; i
< count
; i
++)
2122 vec_free (known_aggs
[i
].items
);
2124 known_csts
.release ();
2125 known_binfos
.release ();
2126 known_aggs
.release ();
2127 known_aggs_ptrs
.release ();
2131 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2132 topological sort of values. */
2135 add_val_to_toposort (struct ipcp_value
*cur_val
)
2137 static int dfs_counter
= 0;
2138 static struct ipcp_value
*stack
;
2139 struct ipcp_value_source
*src
;
2145 cur_val
->dfs
= dfs_counter
;
2146 cur_val
->low_link
= dfs_counter
;
2148 cur_val
->topo_next
= stack
;
2150 cur_val
->on_stack
= true;
2152 for (src
= cur_val
->sources
; src
; src
= src
->next
)
2155 if (src
->val
->dfs
== 0)
2157 add_val_to_toposort (src
->val
);
2158 if (src
->val
->low_link
< cur_val
->low_link
)
2159 cur_val
->low_link
= src
->val
->low_link
;
2161 else if (src
->val
->on_stack
2162 && src
->val
->dfs
< cur_val
->low_link
)
2163 cur_val
->low_link
= src
->val
->dfs
;
2166 if (cur_val
->dfs
== cur_val
->low_link
)
2168 struct ipcp_value
*v
, *scc_list
= NULL
;
2173 stack
= v
->topo_next
;
2174 v
->on_stack
= false;
2176 v
->scc_next
= scc_list
;
2179 while (v
!= cur_val
);
2181 cur_val
->topo_next
= values_topo
;
2182 values_topo
= cur_val
;
2186 /* Add all values in lattices associated with NODE to the topological sort if
2187 they are not there yet. */
2190 add_all_node_vals_to_toposort (struct cgraph_node
*node
)
2192 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2193 int i
, count
= ipa_get_param_count (info
);
2195 for (i
= 0; i
< count
; i
++)
2197 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2198 struct ipcp_lattice
*lat
= &plats
->itself
;
2199 struct ipcp_agg_lattice
*aglat
;
2200 struct ipcp_value
*val
;
2203 for (val
= lat
->values
; val
; val
= val
->next
)
2204 add_val_to_toposort (val
);
2206 if (!plats
->aggs_bottom
)
2207 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2209 for (val
= aglat
->values
; val
; val
= val
->next
)
2210 add_val_to_toposort (val
);
2214 /* One pass of constants propagation along the call graph edges, from callers
2215 to callees (requires topological ordering in TOPO), iterate over strongly
2216 connected components. */
2219 propagate_constants_topo (struct ipa_topo_info
*topo
)
2223 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
2226 struct cgraph_node
*v
, *node
= topo
->order
[i
];
2227 vec
<cgraph_node
*> cycle_nodes
= ipa_get_nodes_in_cycle (node
);
2229 /* First, iteratively propagate within the strongly connected component
2230 until all lattices stabilize. */
2231 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
2232 if (v
->has_gimple_body_p ())
2233 push_node_to_stack (topo
, v
);
2235 v
= pop_node_from_stack (topo
);
2238 struct cgraph_edge
*cs
;
2240 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
2241 if (ipa_edge_within_scc (cs
)
2242 && propagate_constants_accross_call (cs
))
2243 push_node_to_stack (topo
, cs
->callee
);
2244 v
= pop_node_from_stack (topo
);
2247 /* Afterwards, propagate along edges leading out of the SCC, calculates
2248 the local effects of the discovered constants and all valid values to
2249 their topological sort. */
2250 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
2251 if (v
->has_gimple_body_p ())
2253 struct cgraph_edge
*cs
;
2255 estimate_local_effects (v
);
2256 add_all_node_vals_to_toposort (v
);
2257 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
2258 if (!ipa_edge_within_scc (cs
))
2259 propagate_constants_accross_call (cs
);
2261 cycle_nodes
.release ();
2266 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2267 the bigger one if otherwise. */
2270 safe_add (int a
, int b
)
2272 if (a
> INT_MAX
/2 || b
> INT_MAX
/2)
2273 return a
> b
? a
: b
;
2279 /* Propagate the estimated effects of individual values along the topological
2280 from the dependent values to those they depend on. */
2283 propagate_effects (void)
2285 struct ipcp_value
*base
;
2287 for (base
= values_topo
; base
; base
= base
->topo_next
)
2289 struct ipcp_value_source
*src
;
2290 struct ipcp_value
*val
;
2291 int time
= 0, size
= 0;
2293 for (val
= base
; val
; val
= val
->scc_next
)
2295 time
= safe_add (time
,
2296 val
->local_time_benefit
+ val
->prop_time_benefit
);
2297 size
= safe_add (size
, val
->local_size_cost
+ val
->prop_size_cost
);
2300 for (val
= base
; val
; val
= val
->scc_next
)
2301 for (src
= val
->sources
; src
; src
= src
->next
)
2303 && src
->cs
->maybe_hot_p ())
2305 src
->val
->prop_time_benefit
= safe_add (time
,
2306 src
->val
->prop_time_benefit
);
2307 src
->val
->prop_size_cost
= safe_add (size
,
2308 src
->val
->prop_size_cost
);
2314 /* Propagate constants, binfos and their effects from the summaries
2315 interprocedurally. */
2318 ipcp_propagate_stage (struct ipa_topo_info
*topo
)
2320 struct cgraph_node
*node
;
2323 fprintf (dump_file
, "\n Propagating constants:\n\n");
2326 ipa_update_after_lto_read ();
2329 FOR_EACH_DEFINED_FUNCTION (node
)
2331 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2333 determine_versionability (node
);
2334 if (node
->has_gimple_body_p ())
2336 info
->lattices
= XCNEWVEC (struct ipcp_param_lattices
,
2337 ipa_get_param_count (info
));
2338 initialize_node_lattices (node
);
2340 if (node
->definition
&& !node
->alias
)
2341 overall_size
+= inline_summary (node
)->self_size
;
2342 if (node
->count
> max_count
)
2343 max_count
= node
->count
;
2346 max_new_size
= overall_size
;
2347 if (max_new_size
< PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
))
2348 max_new_size
= PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
);
2349 max_new_size
+= max_new_size
* PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH
) / 100 + 1;
2352 fprintf (dump_file
, "\noverall_size: %li, max_new_size: %li\n",
2353 overall_size
, max_new_size
);
2355 propagate_constants_topo (topo
);
2356 #ifdef ENABLE_CHECKING
2357 ipcp_verify_propagated_values ();
2359 propagate_effects ();
2363 fprintf (dump_file
, "\nIPA lattices after all propagation:\n");
2364 print_all_lattices (dump_file
, (dump_flags
& TDF_DETAILS
), true);
2368 /* Discover newly direct outgoing edges from NODE which is a new clone with
2369 known KNOWN_VALS and make them direct. */
2372 ipcp_discover_new_direct_edges (struct cgraph_node
*node
,
2373 vec
<tree
> known_vals
,
2374 struct ipa_agg_replacement_value
*aggvals
)
2376 struct cgraph_edge
*ie
, *next_ie
;
2379 for (ie
= node
->indirect_calls
; ie
; ie
= next_ie
)
2383 next_ie
= ie
->next_callee
;
2384 target
= ipa_get_indirect_edge_target_1 (ie
, known_vals
, vNULL
, vNULL
,
2388 bool agg_contents
= ie
->indirect_info
->agg_contents
;
2389 bool polymorphic
= ie
->indirect_info
->polymorphic
;
2390 int param_index
= ie
->indirect_info
->param_index
;
2391 struct cgraph_edge
*cs
= ipa_make_edge_direct_to_target (ie
, target
);
2394 if (cs
&& !agg_contents
&& !polymorphic
)
2396 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2397 int c
= ipa_get_controlled_uses (info
, param_index
);
2398 if (c
!= IPA_UNDESCRIBED_USE
)
2400 struct ipa_ref
*to_del
;
2403 ipa_set_controlled_uses (info
, param_index
, c
);
2404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2405 fprintf (dump_file
, " controlled uses count of param "
2406 "%i bumped down to %i\n", param_index
, c
);
2408 && (to_del
= node
->find_reference (cs
->callee
, NULL
, 0)))
2410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2411 fprintf (dump_file
, " and even removing its "
2412 "cloning-created reference\n");
2413 to_del
->remove_reference ();
2419 /* Turning calls to direct calls will improve overall summary. */
2421 inline_update_overall_summary (node
);
2424 /* Vector of pointers which for linked lists of clones of an original crgaph
2427 static vec
<cgraph_edge
*> next_edge_clone
;
2428 static vec
<cgraph_edge
*> prev_edge_clone
;
2431 grow_edge_clone_vectors (void)
2433 if (next_edge_clone
.length ()
2434 <= (unsigned) symtab
->edges_max_uid
)
2435 next_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
2436 if (prev_edge_clone
.length ()
2437 <= (unsigned) symtab
->edges_max_uid
)
2438 prev_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
2441 /* Edge duplication hook to grow the appropriate linked list in
2445 ipcp_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
2448 grow_edge_clone_vectors ();
2450 struct cgraph_edge
*old_next
= next_edge_clone
[src
->uid
];
2452 prev_edge_clone
[old_next
->uid
] = dst
;
2453 prev_edge_clone
[dst
->uid
] = src
;
2455 next_edge_clone
[dst
->uid
] = old_next
;
2456 next_edge_clone
[src
->uid
] = dst
;
2459 /* Hook that is called by cgraph.c when an edge is removed. */
2462 ipcp_edge_removal_hook (struct cgraph_edge
*cs
, void *)
2464 grow_edge_clone_vectors ();
2466 struct cgraph_edge
*prev
= prev_edge_clone
[cs
->uid
];
2467 struct cgraph_edge
*next
= next_edge_clone
[cs
->uid
];
2469 next_edge_clone
[prev
->uid
] = next
;
2471 prev_edge_clone
[next
->uid
] = prev
;
2474 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
2475 parameter with the given INDEX. */
2478 get_clone_agg_value (struct cgraph_node
*node
, HOST_WIDE_INT offset
,
2481 struct ipa_agg_replacement_value
*aggval
;
2483 aggval
= ipa_get_agg_replacements_for_node (node
);
2486 if (aggval
->offset
== offset
2487 && aggval
->index
== index
)
2488 return aggval
->value
;
2489 aggval
= aggval
->next
;
2494 /* Return true if edge CS does bring about the value described by SRC. */
2497 cgraph_edge_brings_value_p (struct cgraph_edge
*cs
,
2498 struct ipcp_value_source
*src
)
2500 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
2501 cgraph_node
*real_dest
= cs
->callee
->function_symbol ();
2502 struct ipa_node_params
*dst_info
= IPA_NODE_REF (real_dest
);
2504 if ((dst_info
->ipcp_orig_node
&& !dst_info
->is_all_contexts_clone
)
2505 || caller_info
->node_dead
)
2510 if (caller_info
->ipcp_orig_node
)
2513 if (src
->offset
== -1)
2514 t
= caller_info
->known_vals
[src
->index
];
2516 t
= get_clone_agg_value (cs
->caller
, src
->offset
, src
->index
);
2517 return (t
!= NULL_TREE
2518 && values_equal_for_ipcp_p (src
->val
->value
, t
));
2522 struct ipcp_agg_lattice
*aglat
;
2523 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (caller_info
,
2525 if (src
->offset
== -1)
2526 return (ipa_lat_is_single_const (&plats
->itself
)
2527 && values_equal_for_ipcp_p (src
->val
->value
,
2528 plats
->itself
.values
->value
));
2531 if (plats
->aggs_bottom
|| plats
->aggs_contain_variable
)
2533 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2534 if (aglat
->offset
== src
->offset
)
2535 return (ipa_lat_is_single_const (aglat
)
2536 && values_equal_for_ipcp_p (src
->val
->value
,
2537 aglat
->values
->value
));
2543 /* Get the next clone in the linked list of clones of an edge. */
2545 static inline struct cgraph_edge
*
2546 get_next_cgraph_edge_clone (struct cgraph_edge
*cs
)
2548 return next_edge_clone
[cs
->uid
];
2551 /* Given VAL, iterate over all its sources and if they still hold, add their
2552 edge frequency and their number into *FREQUENCY and *CALLER_COUNT
2556 get_info_about_necessary_edges (struct ipcp_value
*val
, int *freq_sum
,
2557 gcov_type
*count_sum
, int *caller_count
)
2559 struct ipcp_value_source
*src
;
2560 int freq
= 0, count
= 0;
2564 for (src
= val
->sources
; src
; src
= src
->next
)
2566 struct cgraph_edge
*cs
= src
->cs
;
2569 if (cgraph_edge_brings_value_p (cs
, src
))
2572 freq
+= cs
->frequency
;
2574 hot
|= cs
->maybe_hot_p ();
2576 cs
= get_next_cgraph_edge_clone (cs
);
2582 *caller_count
= count
;
2586 /* Return a vector of incoming edges that do bring value VAL. It is assumed
2587 their number is known and equal to CALLER_COUNT. */
2589 static vec
<cgraph_edge
*>
2590 gather_edges_for_value (struct ipcp_value
*val
, int caller_count
)
2592 struct ipcp_value_source
*src
;
2593 vec
<cgraph_edge
*> ret
;
2595 ret
.create (caller_count
);
2596 for (src
= val
->sources
; src
; src
= src
->next
)
2598 struct cgraph_edge
*cs
= src
->cs
;
2601 if (cgraph_edge_brings_value_p (cs
, src
))
2602 ret
.quick_push (cs
);
2603 cs
= get_next_cgraph_edge_clone (cs
);
2610 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
2611 Return it or NULL if for some reason it cannot be created. */
2613 static struct ipa_replace_map
*
2614 get_replacement_map (struct ipa_node_params
*info
, tree value
, int parm_num
)
2616 struct ipa_replace_map
*replace_map
;
2619 replace_map
= ggc_alloc
<ipa_replace_map
> ();
2622 fprintf (dump_file
, " replacing ");
2623 ipa_dump_param (dump_file
, info
, parm_num
);
2625 fprintf (dump_file
, " with const ");
2626 print_generic_expr (dump_file
, value
, 0);
2627 fprintf (dump_file
, "\n");
2629 replace_map
->old_tree
= NULL
;
2630 replace_map
->parm_num
= parm_num
;
2631 replace_map
->new_tree
= value
;
2632 replace_map
->replace_p
= true;
2633 replace_map
->ref_p
= false;
2638 /* Dump new profiling counts */
2641 dump_profile_updates (struct cgraph_node
*orig_node
,
2642 struct cgraph_node
*new_node
)
2644 struct cgraph_edge
*cs
;
2646 fprintf (dump_file
, " setting count of the specialized node to "
2647 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) new_node
->count
);
2648 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
2649 fprintf (dump_file
, " edge to %s has count "
2650 HOST_WIDE_INT_PRINT_DEC
"\n",
2651 cs
->callee
->name (), (HOST_WIDE_INT
) cs
->count
);
2653 fprintf (dump_file
, " setting count of the original node to "
2654 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) orig_node
->count
);
2655 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
2656 fprintf (dump_file
, " edge to %s is left with "
2657 HOST_WIDE_INT_PRINT_DEC
"\n",
2658 cs
->callee
->name (), (HOST_WIDE_INT
) cs
->count
);
2661 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
2662 their profile information to reflect this. */
2665 update_profiling_info (struct cgraph_node
*orig_node
,
2666 struct cgraph_node
*new_node
)
2668 struct cgraph_edge
*cs
;
2669 struct caller_statistics stats
;
2670 gcov_type new_sum
, orig_sum
;
2671 gcov_type remainder
, orig_node_count
= orig_node
->count
;
2673 if (orig_node_count
== 0)
2676 init_caller_stats (&stats
);
2677 orig_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
2679 orig_sum
= stats
.count_sum
;
2680 init_caller_stats (&stats
);
2681 new_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
2683 new_sum
= stats
.count_sum
;
2685 if (orig_node_count
< orig_sum
+ new_sum
)
2688 fprintf (dump_file
, " Problem: node %s/%i has too low count "
2689 HOST_WIDE_INT_PRINT_DEC
" while the sum of incoming "
2690 "counts is " HOST_WIDE_INT_PRINT_DEC
"\n",
2691 orig_node
->name (), orig_node
->order
,
2692 (HOST_WIDE_INT
) orig_node_count
,
2693 (HOST_WIDE_INT
) (orig_sum
+ new_sum
));
2695 orig_node_count
= (orig_sum
+ new_sum
) * 12 / 10;
2697 fprintf (dump_file
, " proceeding by pretending it was "
2698 HOST_WIDE_INT_PRINT_DEC
"\n",
2699 (HOST_WIDE_INT
) orig_node_count
);
2702 new_node
->count
= new_sum
;
2703 remainder
= orig_node_count
- new_sum
;
2704 orig_node
->count
= remainder
;
2706 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
2708 cs
->count
= apply_probability (cs
->count
,
2709 GCOV_COMPUTE_SCALE (new_sum
,
2714 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
2715 cs
->count
= apply_probability (cs
->count
,
2716 GCOV_COMPUTE_SCALE (remainder
,
2720 dump_profile_updates (orig_node
, new_node
);
2723 /* Update the respective profile of specialized NEW_NODE and the original
2724 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
2725 have been redirected to the specialized version. */
2728 update_specialized_profile (struct cgraph_node
*new_node
,
2729 struct cgraph_node
*orig_node
,
2730 gcov_type redirected_sum
)
2732 struct cgraph_edge
*cs
;
2733 gcov_type new_node_count
, orig_node_count
= orig_node
->count
;
2736 fprintf (dump_file
, " the sum of counts of redirected edges is "
2737 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) redirected_sum
);
2738 if (orig_node_count
== 0)
2741 gcc_assert (orig_node_count
>= redirected_sum
);
2743 new_node_count
= new_node
->count
;
2744 new_node
->count
+= redirected_sum
;
2745 orig_node
->count
-= redirected_sum
;
2747 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
2749 cs
->count
+= apply_probability (cs
->count
,
2750 GCOV_COMPUTE_SCALE (redirected_sum
,
2755 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
2757 gcov_type dec
= apply_probability (cs
->count
,
2758 GCOV_COMPUTE_SCALE (redirected_sum
,
2760 if (dec
< cs
->count
)
2767 dump_profile_updates (orig_node
, new_node
);
2770 /* Create a specialized version of NODE with known constants and types of
2771 parameters in KNOWN_VALS and redirect all edges in CALLERS to it. */
2773 static struct cgraph_node
*
2774 create_specialized_node (struct cgraph_node
*node
,
2775 vec
<tree
> known_vals
,
2776 struct ipa_agg_replacement_value
*aggvals
,
2777 vec
<cgraph_edge
*> callers
)
2779 struct ipa_node_params
*new_info
, *info
= IPA_NODE_REF (node
);
2780 vec
<ipa_replace_map
*, va_gc
> *replace_trees
= NULL
;
2781 struct ipa_agg_replacement_value
*av
;
2782 struct cgraph_node
*new_node
;
2783 int i
, count
= ipa_get_param_count (info
);
2784 bitmap args_to_skip
;
2786 gcc_assert (!info
->ipcp_orig_node
);
2788 if (node
->local
.can_change_signature
)
2790 args_to_skip
= BITMAP_GGC_ALLOC ();
2791 for (i
= 0; i
< count
; i
++)
2793 tree t
= known_vals
[i
];
2795 if ((t
&& TREE_CODE (t
) != TREE_BINFO
)
2796 || !ipa_is_param_used (info
, i
))
2797 bitmap_set_bit (args_to_skip
, i
);
2802 args_to_skip
= NULL
;
2803 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2804 fprintf (dump_file
, " cannot change function signature\n");
2807 for (i
= 0; i
< count
; i
++)
2809 tree t
= known_vals
[i
];
2810 if (t
&& TREE_CODE (t
) != TREE_BINFO
)
2812 struct ipa_replace_map
*replace_map
;
2814 replace_map
= get_replacement_map (info
, t
, i
);
2816 vec_safe_push (replace_trees
, replace_map
);
2820 new_node
= node
->create_virtual_clone (callers
, replace_trees
,
2821 args_to_skip
, "constprop");
2822 ipa_set_node_agg_value_chain (new_node
, aggvals
);
2823 for (av
= aggvals
; av
; av
= av
->next
)
2824 new_node
->maybe_create_reference (av
->value
, IPA_REF_ADDR
, NULL
);
2826 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2828 fprintf (dump_file
, " the new node is %s/%i.\n",
2829 new_node
->name (), new_node
->order
);
2831 ipa_dump_agg_replacement_values (dump_file
, aggvals
);
2833 ipa_check_create_node_params ();
2834 update_profiling_info (node
, new_node
);
2835 new_info
= IPA_NODE_REF (new_node
);
2836 new_info
->ipcp_orig_node
= node
;
2837 new_info
->known_vals
= known_vals
;
2839 ipcp_discover_new_direct_edges (new_node
, known_vals
, aggvals
);
2845 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
2846 KNOWN_VALS with constants and types that are also known for all of the
2850 find_more_scalar_values_for_callers_subset (struct cgraph_node
*node
,
2851 vec
<tree
> known_vals
,
2852 vec
<cgraph_edge
*> callers
)
2854 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2855 int i
, count
= ipa_get_param_count (info
);
2857 for (i
= 0; i
< count
; i
++)
2859 struct cgraph_edge
*cs
;
2860 tree newval
= NULL_TREE
;
2863 if (ipa_get_scalar_lat (info
, i
)->bottom
|| known_vals
[i
])
2866 FOR_EACH_VEC_ELT (callers
, j
, cs
)
2868 struct ipa_jump_func
*jump_func
;
2871 if (i
>= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
)))
2876 jump_func
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), i
);
2877 t
= ipa_value_from_jfunc (IPA_NODE_REF (cs
->caller
), jump_func
);
2880 && !values_equal_for_ipcp_p (t
, newval
)))
2891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2893 fprintf (dump_file
, " adding an extra known scalar value ");
2894 print_ipcp_constant_value (dump_file
, newval
);
2895 fprintf (dump_file
, " for ");
2896 ipa_dump_param (dump_file
, info
, i
);
2897 fprintf (dump_file
, "\n");
2900 known_vals
[i
] = newval
;
2905 /* Go through PLATS and create a vector of values consisting of values and
2906 offsets (minus OFFSET) of lattices that contain only a single value. */
2908 static vec
<ipa_agg_jf_item
>
2909 copy_plats_to_inter (struct ipcp_param_lattices
*plats
, HOST_WIDE_INT offset
)
2911 vec
<ipa_agg_jf_item
> res
= vNULL
;
2913 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
2916 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2917 if (ipa_lat_is_single_const (aglat
))
2919 struct ipa_agg_jf_item ti
;
2920 ti
.offset
= aglat
->offset
- offset
;
2921 ti
.value
= aglat
->values
->value
;
2927 /* Intersect all values in INTER with single value lattices in PLATS (while
2928 subtracting OFFSET). */
2931 intersect_with_plats (struct ipcp_param_lattices
*plats
,
2932 vec
<ipa_agg_jf_item
> *inter
,
2933 HOST_WIDE_INT offset
)
2935 struct ipcp_agg_lattice
*aglat
;
2936 struct ipa_agg_jf_item
*item
;
2939 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
2945 aglat
= plats
->aggs
;
2946 FOR_EACH_VEC_ELT (*inter
, k
, item
)
2953 if (aglat
->offset
- offset
> item
->offset
)
2955 if (aglat
->offset
- offset
== item
->offset
)
2957 gcc_checking_assert (item
->value
);
2958 if (values_equal_for_ipcp_p (item
->value
, aglat
->values
->value
))
2962 aglat
= aglat
->next
;
2965 item
->value
= NULL_TREE
;
2969 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
2970 vector result while subtracting OFFSET from the individual value offsets. */
2972 static vec
<ipa_agg_jf_item
>
2973 agg_replacements_to_vector (struct cgraph_node
*node
, int index
,
2974 HOST_WIDE_INT offset
)
2976 struct ipa_agg_replacement_value
*av
;
2977 vec
<ipa_agg_jf_item
> res
= vNULL
;
2979 for (av
= ipa_get_agg_replacements_for_node (node
); av
; av
= av
->next
)
2980 if (av
->index
== index
2981 && (av
->offset
- offset
) >= 0)
2983 struct ipa_agg_jf_item item
;
2984 gcc_checking_assert (av
->value
);
2985 item
.offset
= av
->offset
- offset
;
2986 item
.value
= av
->value
;
2987 res
.safe_push (item
);
2993 /* Intersect all values in INTER with those that we have already scheduled to
2994 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
2995 (while subtracting OFFSET). */
2998 intersect_with_agg_replacements (struct cgraph_node
*node
, int index
,
2999 vec
<ipa_agg_jf_item
> *inter
,
3000 HOST_WIDE_INT offset
)
3002 struct ipa_agg_replacement_value
*srcvals
;
3003 struct ipa_agg_jf_item
*item
;
3006 srcvals
= ipa_get_agg_replacements_for_node (node
);
3013 FOR_EACH_VEC_ELT (*inter
, i
, item
)
3015 struct ipa_agg_replacement_value
*av
;
3019 for (av
= srcvals
; av
; av
= av
->next
)
3021 gcc_checking_assert (av
->value
);
3022 if (av
->index
== index
3023 && av
->offset
- offset
== item
->offset
)
3025 if (values_equal_for_ipcp_p (item
->value
, av
->value
))
3031 item
->value
= NULL_TREE
;
3035 /* Intersect values in INTER with aggregate values that come along edge CS to
3036 parameter number INDEX and return it. If INTER does not actually exist yet,
3037 copy all incoming values to it. If we determine we ended up with no values
3038 whatsoever, return a released vector. */
3040 static vec
<ipa_agg_jf_item
>
3041 intersect_aggregates_with_edge (struct cgraph_edge
*cs
, int index
,
3042 vec
<ipa_agg_jf_item
> inter
)
3044 struct ipa_jump_func
*jfunc
;
3045 jfunc
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), index
);
3046 if (jfunc
->type
== IPA_JF_PASS_THROUGH
3047 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
3049 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3050 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
3052 if (caller_info
->ipcp_orig_node
)
3054 struct cgraph_node
*orig_node
= caller_info
->ipcp_orig_node
;
3055 struct ipcp_param_lattices
*orig_plats
;
3056 orig_plats
= ipa_get_parm_lattices (IPA_NODE_REF (orig_node
),
3058 if (agg_pass_through_permissible_p (orig_plats
, jfunc
))
3060 if (!inter
.exists ())
3061 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, 0);
3063 intersect_with_agg_replacements (cs
->caller
, src_idx
,
3074 struct ipcp_param_lattices
*src_plats
;
3075 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
3076 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
3078 /* Currently we do not produce clobber aggregate jump
3079 functions, adjust when we do. */
3080 gcc_checking_assert (!jfunc
->agg
.items
);
3081 if (!inter
.exists ())
3082 inter
= copy_plats_to_inter (src_plats
, 0);
3084 intersect_with_plats (src_plats
, &inter
, 0);
3093 else if (jfunc
->type
== IPA_JF_ANCESTOR
3094 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
3096 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3097 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
3098 struct ipcp_param_lattices
*src_plats
;
3099 HOST_WIDE_INT delta
= ipa_get_jf_ancestor_offset (jfunc
);
3101 if (caller_info
->ipcp_orig_node
)
3103 if (!inter
.exists ())
3104 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, delta
);
3106 intersect_with_agg_replacements (cs
->caller
, src_idx
, &inter
,
3111 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);;
3112 /* Currently we do not produce clobber aggregate jump
3113 functions, adjust when we do. */
3114 gcc_checking_assert (!src_plats
->aggs
|| !jfunc
->agg
.items
);
3115 if (!inter
.exists ())
3116 inter
= copy_plats_to_inter (src_plats
, delta
);
3118 intersect_with_plats (src_plats
, &inter
, delta
);
3121 else if (jfunc
->agg
.items
)
3123 struct ipa_agg_jf_item
*item
;
3126 if (!inter
.exists ())
3127 for (unsigned i
= 0; i
< jfunc
->agg
.items
->length (); i
++)
3128 inter
.safe_push ((*jfunc
->agg
.items
)[i
]);
3130 FOR_EACH_VEC_ELT (inter
, k
, item
)
3133 bool found
= false;;
3138 while ((unsigned) l
< jfunc
->agg
.items
->length ())
3140 struct ipa_agg_jf_item
*ti
;
3141 ti
= &(*jfunc
->agg
.items
)[l
];
3142 if (ti
->offset
> item
->offset
)
3144 if (ti
->offset
== item
->offset
)
3146 gcc_checking_assert (ti
->value
);
3147 if (values_equal_for_ipcp_p (item
->value
,
3161 return vec
<ipa_agg_jf_item
>();
3166 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3167 from all of them. */
3169 static struct ipa_agg_replacement_value
*
3170 find_aggregate_values_for_callers_subset (struct cgraph_node
*node
,
3171 vec
<cgraph_edge
*> callers
)
3173 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
3174 struct ipa_agg_replacement_value
*res
;
3175 struct ipa_agg_replacement_value
**tail
= &res
;
3176 struct cgraph_edge
*cs
;
3177 int i
, j
, count
= ipa_get_param_count (dest_info
);
3179 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3181 int c
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
3186 for (i
= 0; i
< count
; i
++)
3188 struct cgraph_edge
*cs
;
3189 vec
<ipa_agg_jf_item
> inter
= vNULL
;
3190 struct ipa_agg_jf_item
*item
;
3191 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (dest_info
, i
);
3194 /* Among other things, the following check should deal with all by_ref
3196 if (plats
->aggs_bottom
)
3199 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3201 inter
= intersect_aggregates_with_edge (cs
, i
, inter
);
3203 if (!inter
.exists ())
3207 FOR_EACH_VEC_ELT (inter
, j
, item
)
3209 struct ipa_agg_replacement_value
*v
;
3214 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
3216 v
->offset
= item
->offset
;
3217 v
->value
= item
->value
;
3218 v
->by_ref
= plats
->aggs_by_ref
;
3224 if (inter
.exists ())
3231 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3233 static struct ipa_agg_replacement_value
*
3234 known_aggs_to_agg_replacement_list (vec
<ipa_agg_jump_function
> known_aggs
)
3236 struct ipa_agg_replacement_value
*res
;
3237 struct ipa_agg_replacement_value
**tail
= &res
;
3238 struct ipa_agg_jump_function
*aggjf
;
3239 struct ipa_agg_jf_item
*item
;
3242 FOR_EACH_VEC_ELT (known_aggs
, i
, aggjf
)
3243 FOR_EACH_VEC_SAFE_ELT (aggjf
->items
, j
, item
)
3245 struct ipa_agg_replacement_value
*v
;
3246 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
3248 v
->offset
= item
->offset
;
3249 v
->value
= item
->value
;
3250 v
->by_ref
= aggjf
->by_ref
;
3258 /* Determine whether CS also brings all scalar values that the NODE is
3262 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge
*cs
,
3263 struct cgraph_node
*node
)
3265 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
3266 int count
= ipa_get_param_count (dest_info
);
3267 struct ipa_node_params
*caller_info
;
3268 struct ipa_edge_args
*args
;
3271 caller_info
= IPA_NODE_REF (cs
->caller
);
3272 args
= IPA_EDGE_REF (cs
);
3273 for (i
= 0; i
< count
; i
++)
3275 struct ipa_jump_func
*jump_func
;
3278 val
= dest_info
->known_vals
[i
];
3282 if (i
>= ipa_get_cs_argument_count (args
))
3284 jump_func
= ipa_get_ith_jump_func (args
, i
);
3285 t
= ipa_value_from_jfunc (caller_info
, jump_func
);
3286 if (!t
|| !values_equal_for_ipcp_p (val
, t
))
3292 /* Determine whether CS also brings all aggregate values that NODE is
3295 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge
*cs
,
3296 struct cgraph_node
*node
)
3298 struct ipa_node_params
*orig_caller_info
= IPA_NODE_REF (cs
->caller
);
3299 struct ipa_node_params
*orig_node_info
;
3300 struct ipa_agg_replacement_value
*aggval
;
3303 aggval
= ipa_get_agg_replacements_for_node (node
);
3307 count
= ipa_get_param_count (IPA_NODE_REF (node
));
3308 ec
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
3310 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
3311 if (aggval
->index
>= ec
)
3314 orig_node_info
= IPA_NODE_REF (IPA_NODE_REF (node
)->ipcp_orig_node
);
3315 if (orig_caller_info
->ipcp_orig_node
)
3316 orig_caller_info
= IPA_NODE_REF (orig_caller_info
->ipcp_orig_node
);
3318 for (i
= 0; i
< count
; i
++)
3320 static vec
<ipa_agg_jf_item
> values
= vec
<ipa_agg_jf_item
>();
3321 struct ipcp_param_lattices
*plats
;
3322 bool interesting
= false;
3323 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
3324 if (aggval
->index
== i
)
3332 plats
= ipa_get_parm_lattices (orig_node_info
, aggval
->index
);
3333 if (plats
->aggs_bottom
)
3336 values
= intersect_aggregates_with_edge (cs
, i
, values
);
3337 if (!values
.exists ())
3340 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
3341 if (aggval
->index
== i
)
3343 struct ipa_agg_jf_item
*item
;
3346 FOR_EACH_VEC_ELT (values
, j
, item
)
3348 && item
->offset
== av
->offset
3349 && values_equal_for_ipcp_p (item
->value
, av
->value
))
3364 /* Given an original NODE and a VAL for which we have already created a
3365 specialized clone, look whether there are incoming edges that still lead
3366 into the old node but now also bring the requested value and also conform to
3367 all other criteria such that they can be redirected the the special node.
3368 This function can therefore redirect the final edge in a SCC. */
3371 perhaps_add_new_callers (struct cgraph_node
*node
, struct ipcp_value
*val
)
3373 struct ipcp_value_source
*src
;
3374 gcov_type redirected_sum
= 0;
3376 for (src
= val
->sources
; src
; src
= src
->next
)
3378 struct cgraph_edge
*cs
= src
->cs
;
3381 enum availability availability
;
3382 struct cgraph_node
*dst
= cs
->callee
->function_symbol (&availability
);
3383 if ((dst
== node
|| IPA_NODE_REF (dst
)->is_all_contexts_clone
)
3384 && availability
> AVAIL_INTERPOSABLE
3385 && cgraph_edge_brings_value_p (cs
, src
))
3387 if (cgraph_edge_brings_all_scalars_for_node (cs
, val
->spec_node
)
3388 && cgraph_edge_brings_all_agg_vals_for_node (cs
,
3392 fprintf (dump_file
, " - adding an extra caller %s/%i"
3394 xstrdup (cs
->caller
->name ()),
3396 xstrdup (val
->spec_node
->name ()),
3397 val
->spec_node
->order
);
3399 cs
->redirect_callee (val
->spec_node
);
3400 redirected_sum
+= cs
->count
;
3403 cs
= get_next_cgraph_edge_clone (cs
);
3408 update_specialized_profile (val
->spec_node
, node
, redirected_sum
);
3412 /* Copy KNOWN_BINFOS to KNOWN_VALS. */
3415 move_binfos_to_values (vec
<tree
> known_vals
,
3416 vec
<tree
> known_binfos
)
3421 for (i
= 0; known_binfos
.iterate (i
, &t
); i
++)
3426 /* Return true if there is a replacement equivalent to VALUE, INDEX and OFFSET
3427 among those in the AGGVALS list. */
3430 ipcp_val_in_agg_replacements_p (struct ipa_agg_replacement_value
*aggvals
,
3431 int index
, HOST_WIDE_INT offset
, tree value
)
3435 if (aggvals
->index
== index
3436 && aggvals
->offset
== offset
3437 && values_equal_for_ipcp_p (aggvals
->value
, value
))
3439 aggvals
= aggvals
->next
;
3444 /* Decide wheter to create a special version of NODE for value VAL of parameter
3445 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
3446 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
3447 KNOWN_BINFOS and KNOWN_AGGS describe the other already known values. */
3450 decide_about_value (struct cgraph_node
*node
, int index
, HOST_WIDE_INT offset
,
3451 struct ipcp_value
*val
, vec
<tree
> known_csts
,
3452 vec
<tree
> known_binfos
)
3454 struct ipa_agg_replacement_value
*aggvals
;
3455 int freq_sum
, caller_count
;
3456 gcov_type count_sum
;
3457 vec
<cgraph_edge
*> callers
;
3462 perhaps_add_new_callers (node
, val
);
3465 else if (val
->local_size_cost
+ overall_size
> max_new_size
)
3467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3468 fprintf (dump_file
, " Ignoring candidate value because "
3469 "max_new_size would be reached with %li.\n",
3470 val
->local_size_cost
+ overall_size
);
3473 else if (!get_info_about_necessary_edges (val
, &freq_sum
, &count_sum
,
3477 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3479 fprintf (dump_file
, " - considering value ");
3480 print_ipcp_constant_value (dump_file
, val
->value
);
3481 fprintf (dump_file
, " for ");
3482 ipa_dump_param (dump_file
, IPA_NODE_REF (node
), index
);
3484 fprintf (dump_file
, ", offset: " HOST_WIDE_INT_PRINT_DEC
, offset
);
3485 fprintf (dump_file
, " (caller_count: %i)\n", caller_count
);
3488 if (!good_cloning_opportunity_p (node
, val
->local_time_benefit
,
3489 freq_sum
, count_sum
,
3490 val
->local_size_cost
)
3491 && !good_cloning_opportunity_p (node
,
3492 val
->local_time_benefit
3493 + val
->prop_time_benefit
,
3494 freq_sum
, count_sum
,
3495 val
->local_size_cost
3496 + val
->prop_size_cost
))
3500 fprintf (dump_file
, " Creating a specialized node of %s/%i.\n",
3501 node
->name (), node
->order
);
3503 callers
= gather_edges_for_value (val
, caller_count
);
3504 kv
= known_csts
.copy ();
3505 move_binfos_to_values (kv
, known_binfos
);
3507 kv
[index
] = val
->value
;
3508 find_more_scalar_values_for_callers_subset (node
, kv
, callers
);
3509 aggvals
= find_aggregate_values_for_callers_subset (node
, callers
);
3510 gcc_checking_assert (offset
== -1
3511 || ipcp_val_in_agg_replacements_p (aggvals
, index
,
3512 offset
, val
->value
));
3513 val
->spec_node
= create_specialized_node (node
, kv
, aggvals
, callers
);
3514 overall_size
+= val
->local_size_cost
;
3516 /* TODO: If for some lattice there is only one other known value
3517 left, make a special node for it too. */
3522 /* Decide whether and what specialized clones of NODE should be created. */
3525 decide_whether_version_node (struct cgraph_node
*node
)
3527 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3528 int i
, count
= ipa_get_param_count (info
);
3529 vec
<tree
> known_csts
, known_binfos
;
3530 vec
<ipa_agg_jump_function
> known_aggs
= vNULL
;
3536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3537 fprintf (dump_file
, "\nEvaluating opportunities for %s/%i.\n",
3538 node
->name (), node
->order
);
3540 gather_context_independent_values (info
, &known_csts
, &known_binfos
,
3541 info
->do_clone_for_all_contexts
? &known_aggs
3544 for (i
= 0; i
< count
;i
++)
3546 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
3547 struct ipcp_lattice
*lat
= &plats
->itself
;
3548 struct ipcp_value
*val
;
3552 && !known_binfos
[i
])
3553 for (val
= lat
->values
; val
; val
= val
->next
)
3554 ret
|= decide_about_value (node
, i
, -1, val
, known_csts
,
3557 if (!plats
->aggs_bottom
)
3559 struct ipcp_agg_lattice
*aglat
;
3560 struct ipcp_value
*val
;
3561 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3562 if (!aglat
->bottom
&& aglat
->values
3563 /* If the following is false, the one value is in
3565 && (plats
->aggs_contain_variable
3566 || !ipa_lat_is_single_const (aglat
)))
3567 for (val
= aglat
->values
; val
; val
= val
->next
)
3568 ret
|= decide_about_value (node
, i
, aglat
->offset
, val
,
3569 known_csts
, known_binfos
);
3571 info
= IPA_NODE_REF (node
);
3574 if (info
->do_clone_for_all_contexts
)
3576 struct cgraph_node
*clone
;
3577 vec
<cgraph_edge
*> callers
;
3580 fprintf (dump_file
, " - Creating a specialized node of %s/%i "
3581 "for all known contexts.\n", node
->name (),
3584 callers
= node
->collect_callers ();
3585 move_binfos_to_values (known_csts
, known_binfos
);
3586 clone
= create_specialized_node (node
, known_csts
,
3587 known_aggs_to_agg_replacement_list (known_aggs
),
3589 info
= IPA_NODE_REF (node
);
3590 info
->do_clone_for_all_contexts
= false;
3591 IPA_NODE_REF (clone
)->is_all_contexts_clone
= true;
3592 for (i
= 0; i
< count
; i
++)
3593 vec_free (known_aggs
[i
].items
);
3594 known_aggs
.release ();
3598 known_csts
.release ();
3600 known_binfos
.release ();
3604 /* Transitively mark all callees of NODE within the same SCC as not dead. */
3607 spread_undeadness (struct cgraph_node
*node
)
3609 struct cgraph_edge
*cs
;
3611 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
3612 if (ipa_edge_within_scc (cs
))
3614 struct cgraph_node
*callee
;
3615 struct ipa_node_params
*info
;
3617 callee
= cs
->callee
->function_symbol (NULL
);
3618 info
= IPA_NODE_REF (callee
);
3620 if (info
->node_dead
)
3622 info
->node_dead
= 0;
3623 spread_undeadness (callee
);
3628 /* Return true if NODE has a caller from outside of its SCC that is not
3629 dead. Worker callback for cgraph_for_node_and_aliases. */
3632 has_undead_caller_from_outside_scc_p (struct cgraph_node
*node
,
3633 void *data ATTRIBUTE_UNUSED
)
3635 struct cgraph_edge
*cs
;
3637 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
3638 if (cs
->caller
->thunk
.thunk_p
3639 && cs
->caller
->call_for_symbol_thunks_and_aliases
3640 (has_undead_caller_from_outside_scc_p
, NULL
, true))
3642 else if (!ipa_edge_within_scc (cs
)
3643 && !IPA_NODE_REF (cs
->caller
)->node_dead
)
3649 /* Identify nodes within the same SCC as NODE which are no longer needed
3650 because of new clones and will be removed as unreachable. */
3653 identify_dead_nodes (struct cgraph_node
*node
)
3655 struct cgraph_node
*v
;
3656 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
3657 if (v
->will_be_removed_from_program_if_no_direct_calls_p ()
3658 && !v
->call_for_symbol_thunks_and_aliases
3659 (has_undead_caller_from_outside_scc_p
, NULL
, true))
3660 IPA_NODE_REF (v
)->node_dead
= 1;
3662 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
3663 if (!IPA_NODE_REF (v
)->node_dead
)
3664 spread_undeadness (v
);
3666 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3668 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
3669 if (IPA_NODE_REF (v
)->node_dead
)
3670 fprintf (dump_file
, " Marking node as dead: %s/%i.\n",
3671 v
->name (), v
->order
);
3675 /* The decision stage. Iterate over the topological order of call graph nodes
3676 TOPO and make specialized clones if deemed beneficial. */
3679 ipcp_decision_stage (struct ipa_topo_info
*topo
)
3684 fprintf (dump_file
, "\nIPA decision stage:\n\n");
3686 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
3688 struct cgraph_node
*node
= topo
->order
[i
];
3689 bool change
= false, iterate
= true;
3693 struct cgraph_node
*v
;
3695 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
3696 if (v
->has_gimple_body_p ()
3697 && ipcp_versionable_function_p (v
))
3698 iterate
|= decide_whether_version_node (v
);
3703 identify_dead_nodes (node
);
3707 /* The IPCP driver. */
3712 struct cgraph_2edge_hook_list
*edge_duplication_hook_holder
;
3713 struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
3714 struct ipa_topo_info topo
;
3716 ipa_check_create_node_params ();
3717 ipa_check_create_edge_args ();
3718 grow_edge_clone_vectors ();
3719 edge_duplication_hook_holder
=
3720 symtab
->add_edge_duplication_hook (&ipcp_edge_duplication_hook
, NULL
);
3721 edge_removal_hook_holder
=
3722 symtab
->add_edge_removal_hook (&ipcp_edge_removal_hook
, NULL
);
3724 ipcp_values_pool
= create_alloc_pool ("IPA-CP values",
3725 sizeof (struct ipcp_value
), 32);
3726 ipcp_sources_pool
= create_alloc_pool ("IPA-CP value sources",
3727 sizeof (struct ipcp_value_source
), 64);
3728 ipcp_agg_lattice_pool
= create_alloc_pool ("IPA_CP aggregate lattices",
3729 sizeof (struct ipcp_agg_lattice
),
3733 fprintf (dump_file
, "\nIPA structures before propagation:\n");
3734 if (dump_flags
& TDF_DETAILS
)
3735 ipa_print_all_params (dump_file
);
3736 ipa_print_all_jump_functions (dump_file
);
3739 /* Topological sort. */
3740 build_toporder_info (&topo
);
3741 /* Do the interprocedural propagation. */
3742 ipcp_propagate_stage (&topo
);
3743 /* Decide what constant propagation and cloning should be performed. */
3744 ipcp_decision_stage (&topo
);
3746 /* Free all IPCP structures. */
3747 free_toporder_info (&topo
);
3748 next_edge_clone
.release ();
3749 symtab
->remove_edge_removal_hook (edge_removal_hook_holder
);
3750 symtab
->remove_edge_duplication_hook (edge_duplication_hook_holder
);
3751 ipa_free_all_structures_after_ipa_cp ();
3753 fprintf (dump_file
, "\nIPA constant propagation end\n");
3757 /* Initialization and computation of IPCP data structures. This is the initial
3758 intraprocedural analysis of functions, which gathers information to be
3759 propagated later on. */
3762 ipcp_generate_summary (void)
3764 struct cgraph_node
*node
;
3767 fprintf (dump_file
, "\nIPA constant propagation start:\n");
3768 ipa_register_cgraph_hooks ();
3770 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
3772 node
->local
.versionable
3773 = tree_versionable_function_p (node
->decl
);
3774 ipa_analyze_node (node
);
3778 /* Write ipcp summary for nodes in SET. */
3781 ipcp_write_summary (void)
3783 ipa_prop_write_jump_functions ();
3786 /* Read ipcp summary. */
3789 ipcp_read_summary (void)
3791 ipa_prop_read_jump_functions ();
3796 const pass_data pass_data_ipa_cp
=
3798 IPA_PASS
, /* type */
3800 OPTGROUP_NONE
, /* optinfo_flags */
3801 TV_IPA_CONSTANT_PROP
, /* tv_id */
3802 0, /* properties_required */
3803 0, /* properties_provided */
3804 0, /* properties_destroyed */
3805 0, /* todo_flags_start */
3806 ( TODO_dump_symtab
| TODO_remove_functions
), /* todo_flags_finish */
3809 class pass_ipa_cp
: public ipa_opt_pass_d
3812 pass_ipa_cp (gcc::context
*ctxt
)
3813 : ipa_opt_pass_d (pass_data_ipa_cp
, ctxt
,
3814 ipcp_generate_summary
, /* generate_summary */
3815 ipcp_write_summary
, /* write_summary */
3816 ipcp_read_summary
, /* read_summary */
3817 ipa_prop_write_all_agg_replacement
, /*
3818 write_optimization_summary */
3819 ipa_prop_read_all_agg_replacement
, /*
3820 read_optimization_summary */
3821 NULL
, /* stmt_fixup */
3822 0, /* function_transform_todo_flags_start */
3823 ipcp_transform_function
, /* function_transform */
3824 NULL
) /* variable_transform */
3827 /* opt_pass methods: */
3828 virtual bool gate (function
*)
3830 /* FIXME: We should remove the optimize check after we ensure we never run
3831 IPA passes when not optimizing. */
3832 return flag_ipa_cp
&& optimize
;
3835 virtual unsigned int execute (function
*) { return ipcp_driver (); }
3837 }; // class pass_ipa_cp
3842 make_pass_ipa_cp (gcc::context
*ctxt
)
3844 return new pass_ipa_cp (ctxt
);
3847 /* Reset all state within ipa-cp.c so that we can rerun the compiler
3848 within the same process. For use by toplev::finalize. */
3851 ipa_cp_c_finalize (void)