1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2015 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"
109 #include "fold-const.h"
110 #include "gimple-fold.h"
111 #include "gimple-expr.h"
115 #include "hard-reg-set.h"
117 #include "alloc-pool.h"
118 #include "symbol-summary.h"
119 #include "ipa-prop.h"
120 #include "tree-pass.h"
122 #include "diagnostic.h"
123 #include "tree-pretty-print.h"
124 #include "tree-inline.h"
126 #include "ipa-inline.h"
127 #include "ipa-utils.h"
129 template <typename valtype
> class ipcp_value
;
131 /* Describes a particular source for an IPA-CP value. */
133 template <typename valtype
>
134 class ipcp_value_source
137 /* Aggregate offset of the source, negative if the source is scalar value of
138 the argument itself. */
139 HOST_WIDE_INT offset
;
140 /* The incoming edge that brought the value. */
142 /* If the jump function that resulted into his value was a pass-through or an
143 ancestor, this is the ipcp_value of the caller from which the described
144 value has been derived. Otherwise it is NULL. */
145 ipcp_value
<valtype
> *val
;
146 /* Next pointer in a linked list of sources of a value. */
147 ipcp_value_source
*next
;
148 /* If the jump function that resulted into his value was a pass-through or an
149 ancestor, this is the index of the parameter of the caller the jump
150 function references. */
154 /* Common ancestor for all ipcp_value instantiations. */
156 class ipcp_value_base
159 /* Time benefit and size cost that specializing the function for this value
160 would bring about in this function alone. */
161 int local_time_benefit
, local_size_cost
;
162 /* Time benefit and size cost that specializing the function for this value
163 can bring about in it's callees (transitively). */
164 int prop_time_benefit
, prop_size_cost
;
167 /* Describes one particular value stored in struct ipcp_lattice. */
169 template <typename valtype
>
170 class ipcp_value
: public ipcp_value_base
173 /* The actual value for the given parameter. */
175 /* The list of sources from which this value originates. */
176 ipcp_value_source
<valtype
> *sources
;
177 /* Next pointers in a linked list of all values in a lattice. */
179 /* Next pointers in a linked list of values in a strongly connected component
181 ipcp_value
*scc_next
;
182 /* Next pointers in a linked list of SCCs of values sorted topologically
183 according their sources. */
184 ipcp_value
*topo_next
;
185 /* A specialized node created for this value, NULL if none has been (so far)
187 cgraph_node
*spec_node
;
188 /* Depth first search number and low link for topological sorting of
191 /* True if this valye is currently on the topo-sort stack. */
194 void add_source (cgraph_edge
*cs
, ipcp_value
*src_val
, int src_idx
,
195 HOST_WIDE_INT offset
);
198 /* Lattice describing potential values of a formal parameter of a function, or
199 a part of an aggreagate. TOP is represented by a lattice with zero values
200 and with contains_variable and bottom flags cleared. BOTTOM is represented
201 by a lattice with the bottom flag set. In that case, values and
202 contains_variable flag should be disregarded. */
204 template <typename valtype
>
208 /* The list of known values and types in this lattice. Note that values are
209 not deallocated if a lattice is set to bottom because there may be value
210 sources referencing them. */
211 ipcp_value
<valtype
> *values
;
212 /* Number of known values and types in this lattice. */
214 /* The lattice contains a variable component (in addition to values). */
215 bool contains_variable
;
216 /* The value of the lattice is bottom (i.e. variable and unusable for any
220 inline bool is_single_const ();
221 inline bool set_to_bottom ();
222 inline bool set_contains_variable ();
223 bool add_value (valtype newval
, cgraph_edge
*cs
,
224 ipcp_value
<valtype
> *src_val
= NULL
,
225 int src_idx
= 0, HOST_WIDE_INT offset
= -1);
226 void print (FILE * f
, bool dump_sources
, bool dump_benefits
);
229 /* Lattice of tree values with an offset to describe a part of an
232 class ipcp_agg_lattice
: public ipcp_lattice
<tree
>
235 /* Offset that is being described by this lattice. */
236 HOST_WIDE_INT offset
;
237 /* Size so that we don't have to re-compute it every time we traverse the
238 list. Must correspond to TYPE_SIZE of all lat values. */
240 /* Next element of the linked list. */
241 struct ipcp_agg_lattice
*next
;
244 /* Lattice of pointer alignment. Unlike the previous types of lattices, this
245 one is only capable of holding one value. */
247 class ipcp_alignment_lattice
250 /* If bottom and top are both false, these two fields hold values as given by
251 ptr_info_def and get_pointer_alignment_1. */
255 inline bool bottom_p () const;
256 inline bool top_p () const;
257 inline bool set_to_bottom ();
258 bool meet_with (unsigned new_align
, unsigned new_misalign
);
259 bool meet_with (const ipcp_alignment_lattice
&other
, HOST_WIDE_INT offset
);
260 void print (FILE * f
);
262 /* If set, this lattice is bottom and all other fields should be
265 /* If bottom and not_top are false, the lattice is TOP. If not_top is true,
266 the known alignment is stored in the fields align and misalign. The field
267 is negated so that memset to zero initializes the lattice to TOP
271 bool meet_with_1 (unsigned new_align
, unsigned new_misalign
);
274 /* Structure containing lattices for a parameter itself and for pieces of
275 aggregates that are passed in the parameter or by a reference in a parameter
276 plus some other useful flags. */
278 class ipcp_param_lattices
281 /* Lattice describing the value of the parameter itself. */
282 ipcp_lattice
<tree
> itself
;
283 /* Lattice describing the polymorphic contexts of a parameter. */
284 ipcp_lattice
<ipa_polymorphic_call_context
> ctxlat
;
285 /* Lattices describing aggregate parts. */
286 ipcp_agg_lattice
*aggs
;
287 /* Lattice describing known alignment. */
288 ipcp_alignment_lattice alignment
;
289 /* Number of aggregate lattices */
291 /* True if aggregate data were passed by reference (as opposed to by
294 /* All aggregate lattices contain a variable component (in addition to
296 bool aggs_contain_variable
;
297 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
298 for any propagation). */
301 /* There is a virtual call based on this parameter. */
305 /* Allocation pools for values and their sources in ipa-cp. */
307 object_allocator
<ipcp_value
<tree
> > ipcp_cst_values_pool
308 ("IPA-CP constant values");
310 object_allocator
<ipcp_value
<ipa_polymorphic_call_context
> >
311 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
313 object_allocator
<ipcp_value_source
<tree
> > ipcp_sources_pool
314 ("IPA-CP value sources");
316 object_allocator
<ipcp_agg_lattice
> ipcp_agg_lattice_pool
317 ("IPA_CP aggregate lattices");
319 /* Maximal count found in program. */
321 static gcov_type max_count
;
323 /* Original overall size of the program. */
325 static long overall_size
, max_new_size
;
327 /* Return the param lattices structure corresponding to the Ith formal
328 parameter of the function described by INFO. */
329 static inline struct ipcp_param_lattices
*
330 ipa_get_parm_lattices (struct ipa_node_params
*info
, int i
)
332 gcc_assert (i
>= 0 && i
< ipa_get_param_count (info
));
333 gcc_checking_assert (!info
->ipcp_orig_node
);
334 gcc_checking_assert (info
->lattices
);
335 return &(info
->lattices
[i
]);
338 /* Return the lattice corresponding to the scalar value of the Ith formal
339 parameter of the function described by INFO. */
340 static inline ipcp_lattice
<tree
> *
341 ipa_get_scalar_lat (struct ipa_node_params
*info
, int i
)
343 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
344 return &plats
->itself
;
347 /* Return the lattice corresponding to the scalar value of the Ith formal
348 parameter of the function described by INFO. */
349 static inline ipcp_lattice
<ipa_polymorphic_call_context
> *
350 ipa_get_poly_ctx_lat (struct ipa_node_params
*info
, int i
)
352 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
353 return &plats
->ctxlat
;
356 /* Return whether LAT is a lattice with a single constant and without an
359 template <typename valtype
>
361 ipcp_lattice
<valtype
>::is_single_const ()
363 if (bottom
|| contains_variable
|| values_count
!= 1)
369 /* Print V which is extracted from a value in a lattice to F. */
372 print_ipcp_constant_value (FILE * f
, tree v
)
374 if (TREE_CODE (v
) == ADDR_EXPR
375 && TREE_CODE (TREE_OPERAND (v
, 0)) == CONST_DECL
)
378 print_generic_expr (f
, DECL_INITIAL (TREE_OPERAND (v
, 0)), 0);
381 print_generic_expr (f
, v
, 0);
384 /* Print V which is extracted from a value in a lattice to F. */
387 print_ipcp_constant_value (FILE * f
, ipa_polymorphic_call_context v
)
392 /* Print a lattice LAT to F. */
394 template <typename valtype
>
396 ipcp_lattice
<valtype
>::print (FILE * f
, bool dump_sources
, bool dump_benefits
)
398 ipcp_value
<valtype
> *val
;
403 fprintf (f
, "BOTTOM\n");
407 if (!values_count
&& !contains_variable
)
409 fprintf (f
, "TOP\n");
413 if (contains_variable
)
415 fprintf (f
, "VARIABLE");
421 for (val
= values
; val
; val
= val
->next
)
423 if (dump_benefits
&& prev
)
425 else if (!dump_benefits
&& prev
)
430 print_ipcp_constant_value (f
, val
->value
);
434 ipcp_value_source
<valtype
> *s
;
436 fprintf (f
, " [from:");
437 for (s
= val
->sources
; s
; s
= s
->next
)
438 fprintf (f
, " %i(%i)", s
->cs
->caller
->order
,
444 fprintf (f
, " [loc_time: %i, loc_size: %i, "
445 "prop_time: %i, prop_size: %i]\n",
446 val
->local_time_benefit
, val
->local_size_cost
,
447 val
->prop_time_benefit
, val
->prop_size_cost
);
453 /* Print alignment lattice to F. */
456 ipcp_alignment_lattice::print (FILE * f
)
459 fprintf (f
, " Alignment unknown (TOP)\n");
460 else if (bottom_p ())
461 fprintf (f
, " Alignment unusable (BOTTOM)\n");
463 fprintf (f
, " Alignment %u, misalignment %u\n", align
, misalign
);
466 /* Print all ipcp_lattices of all functions to F. */
469 print_all_lattices (FILE * f
, bool dump_sources
, bool dump_benefits
)
471 struct cgraph_node
*node
;
474 fprintf (f
, "\nLattices:\n");
475 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
477 struct ipa_node_params
*info
;
479 info
= IPA_NODE_REF (node
);
480 fprintf (f
, " Node: %s/%i:\n", node
->name (),
482 count
= ipa_get_param_count (info
);
483 for (i
= 0; i
< count
; i
++)
485 struct ipcp_agg_lattice
*aglat
;
486 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
487 fprintf (f
, " param [%d]: ", i
);
488 plats
->itself
.print (f
, dump_sources
, dump_benefits
);
489 fprintf (f
, " ctxs: ");
490 plats
->ctxlat
.print (f
, dump_sources
, dump_benefits
);
491 plats
->alignment
.print (f
);
492 if (plats
->virt_call
)
493 fprintf (f
, " virt_call flag set\n");
495 if (plats
->aggs_bottom
)
497 fprintf (f
, " AGGS BOTTOM\n");
500 if (plats
->aggs_contain_variable
)
501 fprintf (f
, " AGGS VARIABLE\n");
502 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
504 fprintf (f
, " %soffset " HOST_WIDE_INT_PRINT_DEC
": ",
505 plats
->aggs_by_ref
? "ref " : "", aglat
->offset
);
506 aglat
->print (f
, dump_sources
, dump_benefits
);
512 /* Determine whether it is at all technically possible to create clones of NODE
513 and store this information in the ipa_node_params structure associated
517 determine_versionability (struct cgraph_node
*node
,
518 struct ipa_node_params
*info
)
520 const char *reason
= NULL
;
522 /* There are a number of generic reasons functions cannot be versioned. We
523 also cannot remove parameters if there are type attributes such as fnspec
525 if (node
->alias
|| node
->thunk
.thunk_p
)
526 reason
= "alias or thunk";
527 else if (!node
->local
.versionable
)
528 reason
= "not a tree_versionable_function";
529 else if (node
->get_availability () <= AVAIL_INTERPOSABLE
)
530 reason
= "insufficient body availability";
531 else if (!opt_for_fn (node
->decl
, optimize
)
532 || !opt_for_fn (node
->decl
, flag_ipa_cp
))
533 reason
= "non-optimized function";
534 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node
->decl
)))
536 /* Ideally we should clone the SIMD clones themselves and create
537 vector copies of them, so IPA-cp and SIMD clones can happily
538 coexist, but that may not be worth the effort. */
539 reason
= "function has SIMD clones";
541 /* Don't clone decls local to a comdat group; it breaks and for C++
542 decloned constructors, inlining is always better anyway. */
543 else if (node
->comdat_local_p ())
544 reason
= "comdat-local function";
546 if (reason
&& dump_file
&& !node
->alias
&& !node
->thunk
.thunk_p
)
547 fprintf (dump_file
, "Function %s/%i is not versionable, reason: %s.\n",
548 node
->name (), node
->order
, reason
);
550 info
->versionable
= (reason
== NULL
);
553 /* Return true if it is at all technically possible to create clones of a
557 ipcp_versionable_function_p (struct cgraph_node
*node
)
559 return IPA_NODE_REF (node
)->versionable
;
562 /* Structure holding accumulated information about callers of a node. */
564 struct caller_statistics
567 int n_calls
, n_hot_calls
, freq_sum
;
570 /* Initialize fields of STAT to zeroes. */
573 init_caller_stats (struct caller_statistics
*stats
)
575 stats
->count_sum
= 0;
577 stats
->n_hot_calls
= 0;
581 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
582 non-thunk incoming edges to NODE. */
585 gather_caller_stats (struct cgraph_node
*node
, void *data
)
587 struct caller_statistics
*stats
= (struct caller_statistics
*) data
;
588 struct cgraph_edge
*cs
;
590 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
591 if (!cs
->caller
->thunk
.thunk_p
)
593 stats
->count_sum
+= cs
->count
;
594 stats
->freq_sum
+= cs
->frequency
;
596 if (cs
->maybe_hot_p ())
597 stats
->n_hot_calls
++;
603 /* Return true if this NODE is viable candidate for cloning. */
606 ipcp_cloning_candidate_p (struct cgraph_node
*node
)
608 struct caller_statistics stats
;
610 gcc_checking_assert (node
->has_gimple_body_p ());
612 if (!opt_for_fn (node
->decl
, flag_ipa_cp_clone
))
615 fprintf (dump_file
, "Not considering %s for cloning; "
616 "-fipa-cp-clone disabled.\n",
621 if (!optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node
->decl
)))
624 fprintf (dump_file
, "Not considering %s for cloning; "
625 "optimizing it for size.\n",
630 init_caller_stats (&stats
);
631 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
, false);
633 if (inline_summaries
->get (node
)->self_size
< stats
.n_calls
)
636 fprintf (dump_file
, "Considering %s for cloning; code might shrink.\n",
641 /* When profile is available and function is hot, propagate into it even if
642 calls seems cold; constant propagation can improve function's speed
646 if (stats
.count_sum
> node
->count
* 90 / 100)
649 fprintf (dump_file
, "Considering %s for cloning; "
650 "usually called directly.\n",
655 if (!stats
.n_hot_calls
)
658 fprintf (dump_file
, "Not considering %s for cloning; no hot calls.\n",
663 fprintf (dump_file
, "Considering %s for cloning.\n",
668 template <typename valtype
>
669 class value_topo_info
672 /* Head of the linked list of topologically sorted values. */
673 ipcp_value
<valtype
> *values_topo
;
674 /* Stack for creating SCCs, represented by a linked list too. */
675 ipcp_value
<valtype
> *stack
;
676 /* Counter driving the algorithm in add_val_to_toposort. */
679 value_topo_info () : values_topo (NULL
), stack (NULL
), dfs_counter (0)
681 void add_val (ipcp_value
<valtype
> *cur_val
);
682 void propagate_effects ();
685 /* Arrays representing a topological ordering of call graph nodes and a stack
686 of nodes used during constant propagation and also data required to perform
687 topological sort of values and propagation of benefits in the determined
693 /* Array with obtained topological order of cgraph nodes. */
694 struct cgraph_node
**order
;
695 /* Stack of cgraph nodes used during propagation within SCC until all values
696 in the SCC stabilize. */
697 struct cgraph_node
**stack
;
698 int nnodes
, stack_top
;
700 value_topo_info
<tree
> constants
;
701 value_topo_info
<ipa_polymorphic_call_context
> contexts
;
703 ipa_topo_info () : order(NULL
), stack(NULL
), nnodes(0), stack_top(0),
708 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
711 build_toporder_info (struct ipa_topo_info
*topo
)
713 topo
->order
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
714 topo
->stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
716 gcc_checking_assert (topo
->stack_top
== 0);
717 topo
->nnodes
= ipa_reduced_postorder (topo
->order
, true, true, NULL
);
720 /* Free information about strongly connected components and the arrays in
724 free_toporder_info (struct ipa_topo_info
*topo
)
726 ipa_free_postorder_info ();
731 /* Add NODE to the stack in TOPO, unless it is already there. */
734 push_node_to_stack (struct ipa_topo_info
*topo
, struct cgraph_node
*node
)
736 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
737 if (info
->node_enqueued
)
739 info
->node_enqueued
= 1;
740 topo
->stack
[topo
->stack_top
++] = node
;
743 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
746 static struct cgraph_node
*
747 pop_node_from_stack (struct ipa_topo_info
*topo
)
751 struct cgraph_node
*node
;
753 node
= topo
->stack
[topo
->stack_top
];
754 IPA_NODE_REF (node
)->node_enqueued
= 0;
761 /* Set lattice LAT to bottom and return true if it previously was not set as
764 template <typename valtype
>
766 ipcp_lattice
<valtype
>::set_to_bottom ()
773 /* Mark lattice as containing an unknown value and return true if it previously
774 was not marked as such. */
776 template <typename valtype
>
778 ipcp_lattice
<valtype
>::set_contains_variable ()
780 bool ret
= !contains_variable
;
781 contains_variable
= true;
785 /* Set all aggegate lattices in PLATS to bottom and return true if they were
786 not previously set as such. */
789 set_agg_lats_to_bottom (struct ipcp_param_lattices
*plats
)
791 bool ret
= !plats
->aggs_bottom
;
792 plats
->aggs_bottom
= true;
796 /* Mark all aggegate lattices in PLATS as containing an unknown value and
797 return true if they were not previously marked as such. */
800 set_agg_lats_contain_variable (struct ipcp_param_lattices
*plats
)
802 bool ret
= !plats
->aggs_contain_variable
;
803 plats
->aggs_contain_variable
= true;
807 /* Return true if alignment information in the lattice is yet unknown. */
810 ipcp_alignment_lattice::top_p () const
812 return !bottom
&& !not_top
;
815 /* Return true if alignment information in the lattice is known to be
819 ipcp_alignment_lattice::bottom_p () const
824 /* Set alignment information in the lattice to bottom. Return true if it
825 previously was in a different state. */
828 ipcp_alignment_lattice::set_to_bottom ()
836 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
837 and NEW_MISALIGN, assuming that we know the current value is neither TOP nor
838 BOTTOM. Return true if the value of lattice has changed. */
841 ipcp_alignment_lattice::meet_with_1 (unsigned new_align
, unsigned new_misalign
)
843 gcc_checking_assert (new_align
!= 0);
844 if (align
== new_align
&& misalign
== new_misalign
)
847 bool changed
= false;
848 if (align
> new_align
)
851 misalign
= misalign
% new_align
;
854 if (misalign
!= (new_misalign
% align
))
856 int diff
= abs ((int) misalign
- (int) (new_misalign
% align
));
857 align
= (unsigned) diff
& -diff
;
859 misalign
= misalign
% align
;
864 gcc_checking_assert (bottom_p () || align
!= 0);
868 /* Meet the current value of the lattice with alignment described by NEW_ALIGN
869 and NEW_MISALIGN. Return true if the value of lattice has changed. */
872 ipcp_alignment_lattice::meet_with (unsigned new_align
, unsigned new_misalign
)
874 gcc_assert (new_align
!= 0);
881 misalign
= new_misalign
;
884 return meet_with_1 (new_align
, new_misalign
);
887 /* Meet the current value of the lattice with OTHER, taking into account that
888 OFFSET has been added to the pointer value. Return true if the value of
889 lattice has changed. */
892 ipcp_alignment_lattice::meet_with (const ipcp_alignment_lattice
&other
,
893 HOST_WIDE_INT offset
)
895 if (other
.bottom_p ())
896 return set_to_bottom ();
897 if (bottom_p () || other
.top_p ())
900 unsigned adjusted_misalign
= (other
.misalign
+ offset
) % other
.align
;
905 misalign
= adjusted_misalign
;
909 return meet_with_1 (other
.align
, adjusted_misalign
);
912 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
913 return true is any of them has not been marked as such so far. */
916 set_all_contains_variable (struct ipcp_param_lattices
*plats
)
919 ret
= plats
->itself
.set_contains_variable ();
920 ret
|= plats
->ctxlat
.set_contains_variable ();
921 ret
|= set_agg_lats_contain_variable (plats
);
922 ret
|= plats
->alignment
.set_to_bottom ();
926 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
927 points to by the number of callers to NODE. */
930 count_callers (cgraph_node
*node
, void *data
)
932 int *caller_count
= (int *) data
;
934 for (cgraph_edge
*cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
935 /* Local thunks can be handled transparently, but if the thunk can not
936 be optimized out, count it as a real use. */
937 if (!cs
->caller
->thunk
.thunk_p
|| !cs
->caller
->local
.local
)
942 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
943 the one caller of some other node. Set the caller's corresponding flag. */
946 set_single_call_flag (cgraph_node
*node
, void *)
948 cgraph_edge
*cs
= node
->callers
;
949 /* Local thunks can be handled transparently, skip them. */
950 while (cs
&& cs
->caller
->thunk
.thunk_p
&& cs
->caller
->local
.local
)
951 cs
= cs
->next_caller
;
954 IPA_NODE_REF (cs
->caller
)->node_calling_single_call
= true;
960 /* Initialize ipcp_lattices. */
963 initialize_node_lattices (struct cgraph_node
*node
)
965 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
966 struct cgraph_edge
*ie
;
967 bool disable
= false, variable
= false;
970 gcc_checking_assert (node
->has_gimple_body_p ());
971 if (cgraph_local_p (node
))
973 int caller_count
= 0;
974 node
->call_for_symbol_thunks_and_aliases (count_callers
, &caller_count
,
976 gcc_checking_assert (caller_count
> 0);
977 if (caller_count
== 1)
978 node
->call_for_symbol_thunks_and_aliases (set_single_call_flag
,
983 /* When cloning is allowed, we can assume that externally visible
984 functions are not called. We will compensate this by cloning
986 if (ipcp_versionable_function_p (node
)
987 && ipcp_cloning_candidate_p (node
))
993 if (disable
|| variable
)
995 for (i
= 0; i
< ipa_get_param_count (info
) ; i
++)
997 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
1000 plats
->itself
.set_to_bottom ();
1001 plats
->ctxlat
.set_to_bottom ();
1002 set_agg_lats_to_bottom (plats
);
1003 plats
->alignment
.set_to_bottom ();
1006 set_all_contains_variable (plats
);
1008 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1009 && !node
->alias
&& !node
->thunk
.thunk_p
)
1010 fprintf (dump_file
, "Marking all lattices of %s/%i as %s\n",
1011 node
->name (), node
->order
,
1012 disable
? "BOTTOM" : "VARIABLE");
1015 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
1016 if (ie
->indirect_info
->polymorphic
1017 && ie
->indirect_info
->param_index
>= 0)
1019 gcc_checking_assert (ie
->indirect_info
->param_index
>= 0);
1020 ipa_get_parm_lattices (info
,
1021 ie
->indirect_info
->param_index
)->virt_call
= 1;
1025 /* Return the result of a (possibly arithmetic) pass through jump function
1026 JFUNC on the constant value INPUT. Return NULL_TREE if that cannot be
1027 determined or be considered an interprocedural invariant. */
1030 ipa_get_jf_pass_through_result (struct ipa_jump_func
*jfunc
, tree input
)
1034 gcc_checking_assert (is_gimple_ip_invariant (input
));
1035 if (ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
1038 if (TREE_CODE_CLASS (ipa_get_jf_pass_through_operation (jfunc
))
1040 restype
= boolean_type_node
;
1042 restype
= TREE_TYPE (input
);
1043 res
= fold_binary (ipa_get_jf_pass_through_operation (jfunc
), restype
,
1044 input
, ipa_get_jf_pass_through_operand (jfunc
));
1046 if (res
&& !is_gimple_ip_invariant (res
))
1052 /* Return the result of an ancestor jump function JFUNC on the constant value
1053 INPUT. Return NULL_TREE if that cannot be determined. */
1056 ipa_get_jf_ancestor_result (struct ipa_jump_func
*jfunc
, tree input
)
1058 gcc_checking_assert (TREE_CODE (input
) != TREE_BINFO
);
1059 if (TREE_CODE (input
) == ADDR_EXPR
)
1061 tree t
= TREE_OPERAND (input
, 0);
1062 t
= build_ref_for_offset (EXPR_LOCATION (t
), t
,
1063 ipa_get_jf_ancestor_offset (jfunc
),
1064 ptr_type_node
, NULL
, false);
1065 return build_fold_addr_expr (t
);
1071 /* Determine whether JFUNC evaluates to a single known constant value and if
1072 so, return it. Otherwise return NULL. INFO describes the caller node or
1073 the one it is inlined to, so that pass-through jump functions can be
1077 ipa_value_from_jfunc (struct ipa_node_params
*info
, struct ipa_jump_func
*jfunc
)
1079 if (jfunc
->type
== IPA_JF_CONST
)
1080 return ipa_get_jf_constant (jfunc
);
1081 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
1082 || jfunc
->type
== IPA_JF_ANCESTOR
)
1087 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1088 idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1090 idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1092 if (info
->ipcp_orig_node
)
1093 input
= info
->known_csts
[idx
];
1096 ipcp_lattice
<tree
> *lat
;
1099 || idx
>= ipa_get_param_count (info
))
1101 lat
= ipa_get_scalar_lat (info
, idx
);
1102 if (!lat
->is_single_const ())
1104 input
= lat
->values
->value
;
1110 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1111 return ipa_get_jf_pass_through_result (jfunc
, input
);
1113 return ipa_get_jf_ancestor_result (jfunc
, input
);
1119 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1120 that INFO describes the caller node or the one it is inlined to, CS is the
1121 call graph edge corresponding to JFUNC and CSIDX index of the described
1124 ipa_polymorphic_call_context
1125 ipa_context_from_jfunc (ipa_node_params
*info
, cgraph_edge
*cs
, int csidx
,
1126 ipa_jump_func
*jfunc
)
1128 ipa_edge_args
*args
= IPA_EDGE_REF (cs
);
1129 ipa_polymorphic_call_context ctx
;
1130 ipa_polymorphic_call_context
*edge_ctx
1131 = cs
? ipa_get_ith_polymorhic_call_context (args
, csidx
) : NULL
;
1133 if (edge_ctx
&& !edge_ctx
->useless_p ())
1136 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1137 || jfunc
->type
== IPA_JF_ANCESTOR
)
1139 ipa_polymorphic_call_context srcctx
;
1141 bool type_preserved
= true;
1142 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1144 if (ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1146 type_preserved
= ipa_get_jf_pass_through_type_preserved (jfunc
);
1147 srcidx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1151 type_preserved
= ipa_get_jf_ancestor_type_preserved (jfunc
);
1152 srcidx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1154 if (info
->ipcp_orig_node
)
1156 if (info
->known_contexts
.exists ())
1157 srcctx
= info
->known_contexts
[srcidx
];
1162 || srcidx
>= ipa_get_param_count (info
))
1164 ipcp_lattice
<ipa_polymorphic_call_context
> *lat
;
1165 lat
= ipa_get_poly_ctx_lat (info
, srcidx
);
1166 if (!lat
->is_single_const ())
1168 srcctx
= lat
->values
->value
;
1170 if (srcctx
.useless_p ())
1172 if (jfunc
->type
== IPA_JF_ANCESTOR
)
1173 srcctx
.offset_by (ipa_get_jf_ancestor_offset (jfunc
));
1174 if (!type_preserved
)
1175 srcctx
.possible_dynamic_type_change (cs
->in_polymorphic_cdtor
);
1176 srcctx
.combine_with (ctx
);
1183 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1184 bottom, not containing a variable component and without any known value at
1188 ipcp_verify_propagated_values (void)
1190 struct cgraph_node
*node
;
1192 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
1194 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
1195 int i
, count
= ipa_get_param_count (info
);
1197 for (i
= 0; i
< count
; i
++)
1199 ipcp_lattice
<tree
> *lat
= ipa_get_scalar_lat (info
, i
);
1202 && !lat
->contains_variable
1203 && lat
->values_count
== 0)
1207 symtab_node::dump_table (dump_file
);
1208 fprintf (dump_file
, "\nIPA lattices after constant "
1209 "propagation, before gcc_unreachable:\n");
1210 print_all_lattices (dump_file
, true, false);
1219 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1222 values_equal_for_ipcp_p (tree x
, tree y
)
1224 gcc_checking_assert (x
!= NULL_TREE
&& y
!= NULL_TREE
);
1229 if (TREE_CODE (x
) == ADDR_EXPR
1230 && TREE_CODE (y
) == ADDR_EXPR
1231 && TREE_CODE (TREE_OPERAND (x
, 0)) == CONST_DECL
1232 && TREE_CODE (TREE_OPERAND (y
, 0)) == CONST_DECL
)
1233 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x
, 0)),
1234 DECL_INITIAL (TREE_OPERAND (y
, 0)), 0);
1236 return operand_equal_p (x
, y
, 0);
1239 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1242 values_equal_for_ipcp_p (ipa_polymorphic_call_context x
,
1243 ipa_polymorphic_call_context y
)
1245 return x
.equal_to (y
);
1249 /* Add a new value source to the value represented by THIS, marking that a
1250 value comes from edge CS and (if the underlying jump function is a
1251 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1252 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1253 scalar value of the parameter itself or the offset within an aggregate. */
1255 template <typename valtype
>
1257 ipcp_value
<valtype
>::add_source (cgraph_edge
*cs
, ipcp_value
*src_val
,
1258 int src_idx
, HOST_WIDE_INT offset
)
1260 ipcp_value_source
<valtype
> *src
;
1262 src
= new (ipcp_sources_pool
.allocate ()) ipcp_value_source
<valtype
>;
1263 src
->offset
= offset
;
1266 src
->index
= src_idx
;
1268 src
->next
= sources
;
1272 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1273 SOURCE and clear all other fields. */
1275 static ipcp_value
<tree
> *
1276 allocate_and_init_ipcp_value (tree source
)
1278 ipcp_value
<tree
> *val
;
1280 val
= ipcp_cst_values_pool
.allocate ();
1281 memset (val
, 0, sizeof (*val
));
1282 val
->value
= source
;
1286 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1287 value to SOURCE and clear all other fields. */
1289 static ipcp_value
<ipa_polymorphic_call_context
> *
1290 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source
)
1292 ipcp_value
<ipa_polymorphic_call_context
> *val
;
1295 val
= ipcp_poly_ctx_values_pool
.allocate ();
1296 memset (val
, 0, sizeof (*val
));
1297 val
->value
= source
;
1301 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1302 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1303 meaning. OFFSET -1 means the source is scalar and not a part of an
1306 template <typename valtype
>
1308 ipcp_lattice
<valtype
>::add_value (valtype newval
, cgraph_edge
*cs
,
1309 ipcp_value
<valtype
> *src_val
,
1310 int src_idx
, HOST_WIDE_INT offset
)
1312 ipcp_value
<valtype
> *val
;
1317 for (val
= values
; val
; val
= val
->next
)
1318 if (values_equal_for_ipcp_p (val
->value
, newval
))
1320 if (ipa_edge_within_scc (cs
))
1322 ipcp_value_source
<valtype
> *s
;
1323 for (s
= val
->sources
; s
; s
= s
->next
)
1330 val
->add_source (cs
, src_val
, src_idx
, offset
);
1334 if (values_count
== PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE
))
1336 /* We can only free sources, not the values themselves, because sources
1337 of other values in this SCC might point to them. */
1338 for (val
= values
; val
; val
= val
->next
)
1340 while (val
->sources
)
1342 ipcp_value_source
<valtype
> *src
= val
->sources
;
1343 val
->sources
= src
->next
;
1344 ipcp_sources_pool
.remove ((ipcp_value_source
<tree
>*)src
);
1349 return set_to_bottom ();
1353 val
= allocate_and_init_ipcp_value (newval
);
1354 val
->add_source (cs
, src_val
, src_idx
, offset
);
1360 /* Propagate values through a pass-through jump function JFUNC associated with
1361 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1362 is the index of the source parameter. */
1365 propagate_vals_accross_pass_through (cgraph_edge
*cs
,
1366 ipa_jump_func
*jfunc
,
1367 ipcp_lattice
<tree
> *src_lat
,
1368 ipcp_lattice
<tree
> *dest_lat
,
1371 ipcp_value
<tree
> *src_val
;
1374 /* Do not create new values when propagating within an SCC because if there
1375 are arithmetic functions with circular dependencies, there is infinite
1376 number of them and we would just make lattices bottom. */
1377 if ((ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1378 && ipa_edge_within_scc (cs
))
1379 ret
= dest_lat
->set_contains_variable ();
1381 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1383 tree cstval
= ipa_get_jf_pass_through_result (jfunc
, src_val
->value
);
1386 ret
|= dest_lat
->add_value (cstval
, cs
, src_val
, src_idx
);
1388 ret
|= dest_lat
->set_contains_variable ();
1394 /* Propagate values through an ancestor jump function JFUNC associated with
1395 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1396 is the index of the source parameter. */
1399 propagate_vals_accross_ancestor (struct cgraph_edge
*cs
,
1400 struct ipa_jump_func
*jfunc
,
1401 ipcp_lattice
<tree
> *src_lat
,
1402 ipcp_lattice
<tree
> *dest_lat
,
1405 ipcp_value
<tree
> *src_val
;
1408 if (ipa_edge_within_scc (cs
))
1409 return dest_lat
->set_contains_variable ();
1411 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1413 tree t
= ipa_get_jf_ancestor_result (jfunc
, src_val
->value
);
1416 ret
|= dest_lat
->add_value (t
, cs
, src_val
, src_idx
);
1418 ret
|= dest_lat
->set_contains_variable ();
1424 /* Propagate scalar values across jump function JFUNC that is associated with
1425 edge CS and put the values into DEST_LAT. */
1428 propagate_scalar_accross_jump_function (struct cgraph_edge
*cs
,
1429 struct ipa_jump_func
*jfunc
,
1430 ipcp_lattice
<tree
> *dest_lat
)
1432 if (dest_lat
->bottom
)
1435 if (jfunc
->type
== IPA_JF_CONST
)
1437 tree val
= ipa_get_jf_constant (jfunc
);
1438 return dest_lat
->add_value (val
, cs
, NULL
, 0);
1440 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
1441 || jfunc
->type
== IPA_JF_ANCESTOR
)
1443 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1444 ipcp_lattice
<tree
> *src_lat
;
1448 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1449 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1451 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1453 src_lat
= ipa_get_scalar_lat (caller_info
, src_idx
);
1454 if (src_lat
->bottom
)
1455 return dest_lat
->set_contains_variable ();
1457 /* If we would need to clone the caller and cannot, do not propagate. */
1458 if (!ipcp_versionable_function_p (cs
->caller
)
1459 && (src_lat
->contains_variable
1460 || (src_lat
->values_count
> 1)))
1461 return dest_lat
->set_contains_variable ();
1463 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1464 ret
= propagate_vals_accross_pass_through (cs
, jfunc
, src_lat
,
1467 ret
= propagate_vals_accross_ancestor (cs
, jfunc
, src_lat
, dest_lat
,
1470 if (src_lat
->contains_variable
)
1471 ret
|= dest_lat
->set_contains_variable ();
1476 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1477 use it for indirect inlining), we should propagate them too. */
1478 return dest_lat
->set_contains_variable ();
1481 /* Propagate scalar values across jump function JFUNC that is associated with
1482 edge CS and describes argument IDX and put the values into DEST_LAT. */
1485 propagate_context_accross_jump_function (cgraph_edge
*cs
,
1486 ipa_jump_func
*jfunc
, int idx
,
1487 ipcp_lattice
<ipa_polymorphic_call_context
> *dest_lat
)
1489 ipa_edge_args
*args
= IPA_EDGE_REF (cs
);
1490 if (dest_lat
->bottom
)
1493 bool added_sth
= false;
1494 bool type_preserved
= true;
1496 ipa_polymorphic_call_context edge_ctx
, *edge_ctx_ptr
1497 = ipa_get_ith_polymorhic_call_context (args
, idx
);
1500 edge_ctx
= *edge_ctx_ptr
;
1502 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1503 || jfunc
->type
== IPA_JF_ANCESTOR
)
1505 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1507 ipcp_lattice
<ipa_polymorphic_call_context
> *src_lat
;
1509 /* TODO: Once we figure out how to propagate speculations, it will
1510 probably be a good idea to switch to speculation if type_preserved is
1511 not set instead of punting. */
1512 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1514 if (ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1516 type_preserved
= ipa_get_jf_pass_through_type_preserved (jfunc
);
1517 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1521 type_preserved
= ipa_get_jf_ancestor_type_preserved (jfunc
);
1522 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1525 src_lat
= ipa_get_poly_ctx_lat (caller_info
, src_idx
);
1526 /* If we would need to clone the caller and cannot, do not propagate. */
1527 if (!ipcp_versionable_function_p (cs
->caller
)
1528 && (src_lat
->contains_variable
1529 || (src_lat
->values_count
> 1)))
1532 ipcp_value
<ipa_polymorphic_call_context
> *src_val
;
1533 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1535 ipa_polymorphic_call_context cur
= src_val
->value
;
1537 if (!type_preserved
)
1538 cur
.possible_dynamic_type_change (cs
->in_polymorphic_cdtor
);
1539 if (jfunc
->type
== IPA_JF_ANCESTOR
)
1540 cur
.offset_by (ipa_get_jf_ancestor_offset (jfunc
));
1541 /* TODO: In cases we know how the context is going to be used,
1542 we can improve the result by passing proper OTR_TYPE. */
1543 cur
.combine_with (edge_ctx
);
1544 if (!cur
.useless_p ())
1546 if (src_lat
->contains_variable
1547 && !edge_ctx
.equal_to (cur
))
1548 ret
|= dest_lat
->set_contains_variable ();
1549 ret
|= dest_lat
->add_value (cur
, cs
, src_val
, src_idx
);
1559 if (!edge_ctx
.useless_p ())
1560 ret
|= dest_lat
->add_value (edge_ctx
, cs
);
1562 ret
|= dest_lat
->set_contains_variable ();
1568 /* Propagate alignments across jump function JFUNC that is associated with
1569 edge CS and update DEST_LAT accordingly. */
1572 propagate_alignment_accross_jump_function (cgraph_edge
*cs
,
1573 ipa_jump_func
*jfunc
,
1574 ipcp_alignment_lattice
*dest_lat
)
1576 if (dest_lat
->bottom_p ())
1579 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1580 || jfunc
->type
== IPA_JF_ANCESTOR
)
1582 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1583 HOST_WIDE_INT offset
= 0;
1586 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1588 enum tree_code op
= ipa_get_jf_pass_through_operation (jfunc
);
1591 if (op
!= POINTER_PLUS_EXPR
1593 return dest_lat
->set_to_bottom ();
1594 tree operand
= ipa_get_jf_pass_through_operand (jfunc
);
1595 if (!tree_fits_shwi_p (operand
))
1596 return dest_lat
->set_to_bottom ();
1597 offset
= tree_to_shwi (operand
);
1599 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1603 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1604 offset
= ipa_get_jf_ancestor_offset (jfunc
) / BITS_PER_UNIT
;
1607 struct ipcp_param_lattices
*src_lats
;
1608 src_lats
= ipa_get_parm_lattices (caller_info
, src_idx
);
1609 return dest_lat
->meet_with (src_lats
->alignment
, offset
);
1613 if (jfunc
->alignment
.known
)
1614 return dest_lat
->meet_with (jfunc
->alignment
.align
,
1615 jfunc
->alignment
.misalign
);
1617 return dest_lat
->set_to_bottom ();
1621 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1622 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1623 other cases, return false). If there are no aggregate items, set
1624 aggs_by_ref to NEW_AGGS_BY_REF. */
1627 set_check_aggs_by_ref (struct ipcp_param_lattices
*dest_plats
,
1628 bool new_aggs_by_ref
)
1630 if (dest_plats
->aggs
)
1632 if (dest_plats
->aggs_by_ref
!= new_aggs_by_ref
)
1634 set_agg_lats_to_bottom (dest_plats
);
1639 dest_plats
->aggs_by_ref
= new_aggs_by_ref
;
1643 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1644 already existing lattice for the given OFFSET and SIZE, marking all skipped
1645 lattices as containing variable and checking for overlaps. If there is no
1646 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1647 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1648 unless there are too many already. If there are two many, return false. If
1649 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
1650 skipped lattices were newly marked as containing variable, set *CHANGE to
1654 merge_agg_lats_step (struct ipcp_param_lattices
*dest_plats
,
1655 HOST_WIDE_INT offset
, HOST_WIDE_INT val_size
,
1656 struct ipcp_agg_lattice
***aglat
,
1657 bool pre_existing
, bool *change
)
1659 gcc_checking_assert (offset
>= 0);
1661 while (**aglat
&& (**aglat
)->offset
< offset
)
1663 if ((**aglat
)->offset
+ (**aglat
)->size
> offset
)
1665 set_agg_lats_to_bottom (dest_plats
);
1668 *change
|= (**aglat
)->set_contains_variable ();
1669 *aglat
= &(**aglat
)->next
;
1672 if (**aglat
&& (**aglat
)->offset
== offset
)
1674 if ((**aglat
)->size
!= val_size
1676 && (**aglat
)->next
->offset
< offset
+ val_size
))
1678 set_agg_lats_to_bottom (dest_plats
);
1681 gcc_checking_assert (!(**aglat
)->next
1682 || (**aglat
)->next
->offset
>= offset
+ val_size
);
1687 struct ipcp_agg_lattice
*new_al
;
1689 if (**aglat
&& (**aglat
)->offset
< offset
+ val_size
)
1691 set_agg_lats_to_bottom (dest_plats
);
1694 if (dest_plats
->aggs_count
== PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS
))
1696 dest_plats
->aggs_count
++;
1697 new_al
= ipcp_agg_lattice_pool
.allocate ();
1698 memset (new_al
, 0, sizeof (*new_al
));
1700 new_al
->offset
= offset
;
1701 new_al
->size
= val_size
;
1702 new_al
->contains_variable
= pre_existing
;
1704 new_al
->next
= **aglat
;
1710 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
1711 containing an unknown value. */
1714 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice
*aglat
)
1719 ret
|= aglat
->set_contains_variable ();
1720 aglat
= aglat
->next
;
1725 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
1726 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
1727 parameter used for lattice value sources. Return true if DEST_PLATS changed
1731 merge_aggregate_lattices (struct cgraph_edge
*cs
,
1732 struct ipcp_param_lattices
*dest_plats
,
1733 struct ipcp_param_lattices
*src_plats
,
1734 int src_idx
, HOST_WIDE_INT offset_delta
)
1736 bool pre_existing
= dest_plats
->aggs
!= NULL
;
1737 struct ipcp_agg_lattice
**dst_aglat
;
1740 if (set_check_aggs_by_ref (dest_plats
, src_plats
->aggs_by_ref
))
1742 if (src_plats
->aggs_bottom
)
1743 return set_agg_lats_contain_variable (dest_plats
);
1744 if (src_plats
->aggs_contain_variable
)
1745 ret
|= set_agg_lats_contain_variable (dest_plats
);
1746 dst_aglat
= &dest_plats
->aggs
;
1748 for (struct ipcp_agg_lattice
*src_aglat
= src_plats
->aggs
;
1750 src_aglat
= src_aglat
->next
)
1752 HOST_WIDE_INT new_offset
= src_aglat
->offset
- offset_delta
;
1756 if (merge_agg_lats_step (dest_plats
, new_offset
, src_aglat
->size
,
1757 &dst_aglat
, pre_existing
, &ret
))
1759 struct ipcp_agg_lattice
*new_al
= *dst_aglat
;
1761 dst_aglat
= &(*dst_aglat
)->next
;
1762 if (src_aglat
->bottom
)
1764 ret
|= new_al
->set_contains_variable ();
1767 if (src_aglat
->contains_variable
)
1768 ret
|= new_al
->set_contains_variable ();
1769 for (ipcp_value
<tree
> *val
= src_aglat
->values
;
1772 ret
|= new_al
->add_value (val
->value
, cs
, val
, src_idx
,
1775 else if (dest_plats
->aggs_bottom
)
1778 ret
|= set_chain_of_aglats_contains_variable (*dst_aglat
);
1782 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
1783 pass-through JFUNC and if so, whether it has conform and conforms to the
1784 rules about propagating values passed by reference. */
1787 agg_pass_through_permissible_p (struct ipcp_param_lattices
*src_plats
,
1788 struct ipa_jump_func
*jfunc
)
1790 return src_plats
->aggs
1791 && (!src_plats
->aggs_by_ref
1792 || ipa_get_jf_pass_through_agg_preserved (jfunc
));
1795 /* Propagate scalar values across jump function JFUNC that is associated with
1796 edge CS and put the values into DEST_LAT. */
1799 propagate_aggs_accross_jump_function (struct cgraph_edge
*cs
,
1800 struct ipa_jump_func
*jfunc
,
1801 struct ipcp_param_lattices
*dest_plats
)
1805 if (dest_plats
->aggs_bottom
)
1808 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1809 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
1811 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1812 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1813 struct ipcp_param_lattices
*src_plats
;
1815 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
1816 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
1818 /* Currently we do not produce clobber aggregate jump
1819 functions, replace with merging when we do. */
1820 gcc_assert (!jfunc
->agg
.items
);
1821 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
,
1825 ret
|= set_agg_lats_contain_variable (dest_plats
);
1827 else if (jfunc
->type
== IPA_JF_ANCESTOR
1828 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
1830 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1831 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1832 struct ipcp_param_lattices
*src_plats
;
1834 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
1835 if (src_plats
->aggs
&& src_plats
->aggs_by_ref
)
1837 /* Currently we do not produce clobber aggregate jump
1838 functions, replace with merging when we do. */
1839 gcc_assert (!jfunc
->agg
.items
);
1840 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
, src_idx
,
1841 ipa_get_jf_ancestor_offset (jfunc
));
1843 else if (!src_plats
->aggs_by_ref
)
1844 ret
|= set_agg_lats_to_bottom (dest_plats
);
1846 ret
|= set_agg_lats_contain_variable (dest_plats
);
1848 else if (jfunc
->agg
.items
)
1850 bool pre_existing
= dest_plats
->aggs
!= NULL
;
1851 struct ipcp_agg_lattice
**aglat
= &dest_plats
->aggs
;
1852 struct ipa_agg_jf_item
*item
;
1855 if (set_check_aggs_by_ref (dest_plats
, jfunc
->agg
.by_ref
))
1858 FOR_EACH_VEC_ELT (*jfunc
->agg
.items
, i
, item
)
1860 HOST_WIDE_INT val_size
;
1862 if (item
->offset
< 0)
1864 gcc_checking_assert (is_gimple_ip_invariant (item
->value
));
1865 val_size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item
->value
)));
1867 if (merge_agg_lats_step (dest_plats
, item
->offset
, val_size
,
1868 &aglat
, pre_existing
, &ret
))
1870 ret
|= (*aglat
)->add_value (item
->value
, cs
, NULL
, 0, 0);
1871 aglat
= &(*aglat
)->next
;
1873 else if (dest_plats
->aggs_bottom
)
1877 ret
|= set_chain_of_aglats_contains_variable (*aglat
);
1880 ret
|= set_agg_lats_contain_variable (dest_plats
);
1885 /* Propagate constants from the caller to the callee of CS. INFO describes the
1889 propagate_constants_accross_call (struct cgraph_edge
*cs
)
1891 struct ipa_node_params
*callee_info
;
1892 enum availability availability
;
1893 struct cgraph_node
*callee
, *alias_or_thunk
;
1894 struct ipa_edge_args
*args
;
1896 int i
, args_count
, parms_count
;
1898 callee
= cs
->callee
->function_symbol (&availability
);
1899 if (!callee
->definition
)
1901 gcc_checking_assert (callee
->has_gimple_body_p ());
1902 callee_info
= IPA_NODE_REF (callee
);
1904 args
= IPA_EDGE_REF (cs
);
1905 args_count
= ipa_get_cs_argument_count (args
);
1906 parms_count
= ipa_get_param_count (callee_info
);
1907 if (parms_count
== 0)
1910 /* No propagation through instrumentation thunks is available yet.
1911 It should be possible with proper mapping of call args and
1912 instrumented callee params in the propagation loop below. But
1913 this case mostly occurs when legacy code calls instrumented code
1914 and it is not a primary target for optimizations.
1915 We detect instrumentation thunks in aliases and thunks chain by
1916 checking instrumentation_clone flag for chain source and target.
1917 Going through instrumentation thunks we always have it changed
1918 from 0 to 1 and all other nodes do not change it. */
1919 if (!cs
->callee
->instrumentation_clone
1920 && callee
->instrumentation_clone
)
1922 for (i
= 0; i
< parms_count
; i
++)
1923 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
,
1928 /* If this call goes through a thunk we must not propagate to the first (0th)
1929 parameter. However, we might need to uncover a thunk from below a series
1930 of aliases first. */
1931 alias_or_thunk
= cs
->callee
;
1932 while (alias_or_thunk
->alias
)
1933 alias_or_thunk
= alias_or_thunk
->get_alias_target ();
1934 if (alias_or_thunk
->thunk
.thunk_p
)
1936 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
,
1943 for (; (i
< args_count
) && (i
< parms_count
); i
++)
1945 struct ipa_jump_func
*jump_func
= ipa_get_ith_jump_func (args
, i
);
1946 struct ipcp_param_lattices
*dest_plats
;
1948 dest_plats
= ipa_get_parm_lattices (callee_info
, i
);
1949 if (availability
== AVAIL_INTERPOSABLE
)
1950 ret
|= set_all_contains_variable (dest_plats
);
1953 ret
|= propagate_scalar_accross_jump_function (cs
, jump_func
,
1954 &dest_plats
->itself
);
1955 ret
|= propagate_context_accross_jump_function (cs
, jump_func
, i
,
1956 &dest_plats
->ctxlat
);
1957 ret
|= propagate_alignment_accross_jump_function (cs
, jump_func
,
1958 &dest_plats
->alignment
);
1959 ret
|= propagate_aggs_accross_jump_function (cs
, jump_func
,
1963 for (; i
< parms_count
; i
++)
1964 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
, i
));
1969 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
1970 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
1971 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
1974 ipa_get_indirect_edge_target_1 (struct cgraph_edge
*ie
,
1975 vec
<tree
> known_csts
,
1976 vec
<ipa_polymorphic_call_context
> known_contexts
,
1977 vec
<ipa_agg_jump_function_p
> known_aggs
,
1978 struct ipa_agg_replacement_value
*agg_reps
,
1981 int param_index
= ie
->indirect_info
->param_index
;
1982 HOST_WIDE_INT anc_offset
;
1986 *speculative
= false;
1988 if (param_index
== -1
1989 || known_csts
.length () <= (unsigned int) param_index
)
1992 if (!ie
->indirect_info
->polymorphic
)
1996 if (ie
->indirect_info
->agg_contents
)
2003 if (agg_reps
->index
== param_index
2004 && agg_reps
->offset
== ie
->indirect_info
->offset
2005 && agg_reps
->by_ref
== ie
->indirect_info
->by_ref
)
2007 t
= agg_reps
->value
;
2010 agg_reps
= agg_reps
->next
;
2013 else if (known_aggs
.length () > (unsigned int) param_index
)
2015 struct ipa_agg_jump_function
*agg
;
2016 agg
= known_aggs
[param_index
];
2017 t
= ipa_find_agg_cst_for_param (agg
, ie
->indirect_info
->offset
,
2018 ie
->indirect_info
->by_ref
);
2024 t
= known_csts
[param_index
];
2027 TREE_CODE (t
) == ADDR_EXPR
2028 && TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
)
2029 return TREE_OPERAND (t
, 0);
2034 if (!opt_for_fn (ie
->caller
->decl
, flag_devirtualize
))
2037 gcc_assert (!ie
->indirect_info
->agg_contents
);
2038 anc_offset
= ie
->indirect_info
->offset
;
2042 /* Try to work out value of virtual table pointer value in replacemnets. */
2043 if (!t
&& agg_reps
&& !ie
->indirect_info
->by_ref
)
2047 if (agg_reps
->index
== param_index
2048 && agg_reps
->offset
== ie
->indirect_info
->offset
2049 && agg_reps
->by_ref
)
2051 t
= agg_reps
->value
;
2054 agg_reps
= agg_reps
->next
;
2058 /* Try to work out value of virtual table pointer value in known
2059 aggregate values. */
2060 if (!t
&& known_aggs
.length () > (unsigned int) param_index
2061 && !ie
->indirect_info
->by_ref
)
2063 struct ipa_agg_jump_function
*agg
;
2064 agg
= known_aggs
[param_index
];
2065 t
= ipa_find_agg_cst_for_param (agg
, ie
->indirect_info
->offset
,
2069 /* If we found the virtual table pointer, lookup the target. */
2073 unsigned HOST_WIDE_INT offset
;
2074 if (vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2076 target
= gimple_get_virt_method_for_vtable (ie
->indirect_info
->otr_token
,
2080 if ((TREE_CODE (TREE_TYPE (target
)) == FUNCTION_TYPE
2081 && DECL_FUNCTION_CODE (target
) == BUILT_IN_UNREACHABLE
)
2082 || !possible_polymorphic_call_target_p
2083 (ie
, cgraph_node::get (target
)))
2084 target
= ipa_impossible_devirt_target (ie
, target
);
2085 *speculative
= ie
->indirect_info
->vptr_changed
;
2092 /* Do we know the constant value of pointer? */
2094 t
= known_csts
[param_index
];
2096 gcc_checking_assert (!t
|| TREE_CODE (t
) != TREE_BINFO
);
2098 ipa_polymorphic_call_context context
;
2099 if (known_contexts
.length () > (unsigned int) param_index
)
2101 context
= known_contexts
[param_index
];
2102 context
.offset_by (anc_offset
);
2103 if (ie
->indirect_info
->vptr_changed
)
2104 context
.possible_dynamic_type_change (ie
->in_polymorphic_cdtor
,
2105 ie
->indirect_info
->otr_type
);
2108 ipa_polymorphic_call_context ctx2
= ipa_polymorphic_call_context
2109 (t
, ie
->indirect_info
->otr_type
, anc_offset
);
2110 if (!ctx2
.useless_p ())
2111 context
.combine_with (ctx2
, ie
->indirect_info
->otr_type
);
2116 context
= ipa_polymorphic_call_context (t
, ie
->indirect_info
->otr_type
,
2118 if (ie
->indirect_info
->vptr_changed
)
2119 context
.possible_dynamic_type_change (ie
->in_polymorphic_cdtor
,
2120 ie
->indirect_info
->otr_type
);
2125 vec
<cgraph_node
*>targets
;
2128 targets
= possible_polymorphic_call_targets
2129 (ie
->indirect_info
->otr_type
,
2130 ie
->indirect_info
->otr_token
,
2132 if (!final
|| targets
.length () > 1)
2134 struct cgraph_node
*node
;
2137 if (!opt_for_fn (ie
->caller
->decl
, flag_devirtualize_speculatively
)
2138 || ie
->speculative
|| !ie
->maybe_hot_p ())
2140 node
= try_speculative_devirtualization (ie
->indirect_info
->otr_type
,
2141 ie
->indirect_info
->otr_token
,
2145 *speculative
= true;
2146 target
= node
->decl
;
2153 *speculative
= false;
2154 if (targets
.length () == 1)
2155 target
= targets
[0]->decl
;
2157 target
= ipa_impossible_devirt_target (ie
, NULL_TREE
);
2160 if (target
&& !possible_polymorphic_call_target_p (ie
,
2161 cgraph_node::get (target
)))
2162 target
= ipa_impossible_devirt_target (ie
, target
);
2168 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2169 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2170 return the destination. */
2173 ipa_get_indirect_edge_target (struct cgraph_edge
*ie
,
2174 vec
<tree
> known_csts
,
2175 vec
<ipa_polymorphic_call_context
> known_contexts
,
2176 vec
<ipa_agg_jump_function_p
> known_aggs
,
2179 return ipa_get_indirect_edge_target_1 (ie
, known_csts
, known_contexts
,
2180 known_aggs
, NULL
, speculative
);
2183 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2184 and KNOWN_CONTEXTS. */
2187 devirtualization_time_bonus (struct cgraph_node
*node
,
2188 vec
<tree
> known_csts
,
2189 vec
<ipa_polymorphic_call_context
> known_contexts
,
2190 vec
<ipa_agg_jump_function_p
> known_aggs
)
2192 struct cgraph_edge
*ie
;
2195 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
2197 struct cgraph_node
*callee
;
2198 struct inline_summary
*isummary
;
2199 enum availability avail
;
2203 target
= ipa_get_indirect_edge_target (ie
, known_csts
, known_contexts
,
2204 known_aggs
, &speculative
);
2208 /* Only bare minimum benefit for clearly un-inlineable targets. */
2210 callee
= cgraph_node::get (target
);
2211 if (!callee
|| !callee
->definition
)
2213 callee
= callee
->function_symbol (&avail
);
2214 if (avail
< AVAIL_AVAILABLE
)
2216 isummary
= inline_summaries
->get (callee
);
2217 if (!isummary
->inlinable
)
2220 /* FIXME: The values below need re-considering and perhaps also
2221 integrating into the cost metrics, at lest in some very basic way. */
2222 if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 4)
2223 res
+= 31 / ((int)speculative
+ 1);
2224 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 2)
2225 res
+= 15 / ((int)speculative
+ 1);
2226 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
2227 || DECL_DECLARED_INLINE_P (callee
->decl
))
2228 res
+= 7 / ((int)speculative
+ 1);
2234 /* Return time bonus incurred because of HINTS. */
2237 hint_time_bonus (inline_hints hints
)
2240 if (hints
& (INLINE_HINT_loop_iterations
| INLINE_HINT_loop_stride
))
2241 result
+= PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS
);
2242 if (hints
& INLINE_HINT_array_index
)
2243 result
+= PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS
);
2247 /* If there is a reason to penalize the function described by INFO in the
2248 cloning goodness evaluation, do so. */
2250 static inline int64_t
2251 incorporate_penalties (ipa_node_params
*info
, int64_t evaluation
)
2253 if (info
->node_within_scc
)
2254 evaluation
= (evaluation
2255 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY
))) / 100;
2257 if (info
->node_calling_single_call
)
2258 evaluation
= (evaluation
2259 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY
)))
2265 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2266 and SIZE_COST and with the sum of frequencies of incoming edges to the
2267 potential new clone in FREQUENCIES. */
2270 good_cloning_opportunity_p (struct cgraph_node
*node
, int time_benefit
,
2271 int freq_sum
, gcov_type count_sum
, int size_cost
)
2273 if (time_benefit
== 0
2274 || !opt_for_fn (node
->decl
, flag_ipa_cp_clone
)
2275 || !optimize_function_for_speed_p (DECL_STRUCT_FUNCTION (node
->decl
)))
2278 gcc_assert (size_cost
> 0);
2280 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2283 int factor
= (count_sum
* 1000) / max_count
;
2284 int64_t evaluation
= (((int64_t) time_benefit
* factor
)
2286 evaluation
= incorporate_penalties (info
, evaluation
);
2288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2289 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
2290 "size: %i, count_sum: " HOST_WIDE_INT_PRINT_DEC
2291 "%s%s) -> evaluation: " "%" PRId64
2292 ", threshold: %i\n",
2293 time_benefit
, size_cost
, (HOST_WIDE_INT
) count_sum
,
2294 info
->node_within_scc
? ", scc" : "",
2295 info
->node_calling_single_call
? ", single_call" : "",
2296 evaluation
, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
2298 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
2302 int64_t evaluation
= (((int64_t) time_benefit
* freq_sum
)
2304 evaluation
= incorporate_penalties (info
, evaluation
);
2306 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2307 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
2308 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2309 "%" PRId64
", threshold: %i\n",
2310 time_benefit
, size_cost
, freq_sum
,
2311 info
->node_within_scc
? ", scc" : "",
2312 info
->node_calling_single_call
? ", single_call" : "",
2313 evaluation
, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
2315 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
2319 /* Return all context independent values from aggregate lattices in PLATS in a
2320 vector. Return NULL if there are none. */
2322 static vec
<ipa_agg_jf_item
, va_gc
> *
2323 context_independent_aggregate_values (struct ipcp_param_lattices
*plats
)
2325 vec
<ipa_agg_jf_item
, va_gc
> *res
= NULL
;
2327 if (plats
->aggs_bottom
2328 || plats
->aggs_contain_variable
2329 || plats
->aggs_count
== 0)
2332 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
;
2334 aglat
= aglat
->next
)
2335 if (aglat
->is_single_const ())
2337 struct ipa_agg_jf_item item
;
2338 item
.offset
= aglat
->offset
;
2339 item
.value
= aglat
->values
->value
;
2340 vec_safe_push (res
, item
);
2345 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2346 populate them with values of parameters that are known independent of the
2347 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2348 non-NULL, the movement cost of all removable parameters will be stored in
2352 gather_context_independent_values (struct ipa_node_params
*info
,
2353 vec
<tree
> *known_csts
,
2354 vec
<ipa_polymorphic_call_context
>
2356 vec
<ipa_agg_jump_function
> *known_aggs
,
2357 int *removable_params_cost
)
2359 int i
, count
= ipa_get_param_count (info
);
2362 known_csts
->create (0);
2363 known_contexts
->create (0);
2364 known_csts
->safe_grow_cleared (count
);
2365 known_contexts
->safe_grow_cleared (count
);
2368 known_aggs
->create (0);
2369 known_aggs
->safe_grow_cleared (count
);
2372 if (removable_params_cost
)
2373 *removable_params_cost
= 0;
2375 for (i
= 0; i
< count
; i
++)
2377 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2378 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
2380 if (lat
->is_single_const ())
2382 ipcp_value
<tree
> *val
= lat
->values
;
2383 gcc_checking_assert (TREE_CODE (val
->value
) != TREE_BINFO
);
2384 (*known_csts
)[i
] = val
->value
;
2385 if (removable_params_cost
)
2386 *removable_params_cost
2387 += estimate_move_cost (TREE_TYPE (val
->value
), false);
2390 else if (removable_params_cost
2391 && !ipa_is_param_used (info
, i
))
2392 *removable_params_cost
2393 += ipa_get_param_move_cost (info
, i
);
2395 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
2396 if (ctxlat
->is_single_const ())
2398 (*known_contexts
)[i
] = ctxlat
->values
->value
;
2404 vec
<ipa_agg_jf_item
, va_gc
> *agg_items
;
2405 struct ipa_agg_jump_function
*ajf
;
2407 agg_items
= context_independent_aggregate_values (plats
);
2408 ajf
= &(*known_aggs
)[i
];
2409 ajf
->items
= agg_items
;
2410 ajf
->by_ref
= plats
->aggs_by_ref
;
2411 ret
|= agg_items
!= NULL
;
2418 /* The current interface in ipa-inline-analysis requires a pointer vector.
2421 FIXME: That interface should be re-worked, this is slightly silly. Still,
2422 I'd like to discuss how to change it first and this demonstrates the
2425 static vec
<ipa_agg_jump_function_p
>
2426 agg_jmp_p_vec_for_t_vec (vec
<ipa_agg_jump_function
> known_aggs
)
2428 vec
<ipa_agg_jump_function_p
> ret
;
2429 struct ipa_agg_jump_function
*ajf
;
2432 ret
.create (known_aggs
.length ());
2433 FOR_EACH_VEC_ELT (known_aggs
, i
, ajf
)
2434 ret
.quick_push (ajf
);
2438 /* Perform time and size measurement of NODE with the context given in
2439 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2440 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2441 all context-independent removable parameters and EST_MOVE_COST of estimated
2442 movement of the considered parameter and store it into VAL. */
2445 perform_estimation_of_a_value (cgraph_node
*node
, vec
<tree
> known_csts
,
2446 vec
<ipa_polymorphic_call_context
> known_contexts
,
2447 vec
<ipa_agg_jump_function_p
> known_aggs_ptrs
,
2448 int base_time
, int removable_params_cost
,
2449 int est_move_cost
, ipcp_value_base
*val
)
2451 int time
, size
, time_benefit
;
2454 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_contexts
,
2455 known_aggs_ptrs
, &size
, &time
,
2457 time_benefit
= base_time
- time
2458 + devirtualization_time_bonus (node
, known_csts
, known_contexts
,
2460 + hint_time_bonus (hints
)
2461 + removable_params_cost
+ est_move_cost
;
2463 gcc_checking_assert (size
>=0);
2464 /* The inliner-heuristics based estimates may think that in certain
2465 contexts some functions do not have any size at all but we want
2466 all specializations to have at least a tiny cost, not least not to
2471 val
->local_time_benefit
= time_benefit
;
2472 val
->local_size_cost
= size
;
2475 /* Iterate over known values of parameters of NODE and estimate the local
2476 effects in terms of time and size they have. */
2479 estimate_local_effects (struct cgraph_node
*node
)
2481 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2482 int i
, count
= ipa_get_param_count (info
);
2483 vec
<tree
> known_csts
;
2484 vec
<ipa_polymorphic_call_context
> known_contexts
;
2485 vec
<ipa_agg_jump_function
> known_aggs
;
2486 vec
<ipa_agg_jump_function_p
> known_aggs_ptrs
;
2488 int base_time
= inline_summaries
->get (node
)->time
;
2489 int removable_params_cost
;
2491 if (!count
|| !ipcp_versionable_function_p (node
))
2494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2495 fprintf (dump_file
, "\nEstimating effects for %s/%i, base_time: %i.\n",
2496 node
->name (), node
->order
, base_time
);
2498 always_const
= gather_context_independent_values (info
, &known_csts
,
2499 &known_contexts
, &known_aggs
,
2500 &removable_params_cost
);
2501 known_aggs_ptrs
= agg_jmp_p_vec_for_t_vec (known_aggs
);
2504 struct caller_statistics stats
;
2508 init_caller_stats (&stats
);
2509 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
2511 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_contexts
,
2512 known_aggs_ptrs
, &size
, &time
, &hints
);
2513 time
-= devirtualization_time_bonus (node
, known_csts
, known_contexts
,
2515 time
-= hint_time_bonus (hints
);
2516 time
-= removable_params_cost
;
2517 size
-= stats
.n_calls
* removable_params_cost
;
2520 fprintf (dump_file
, " - context independent values, size: %i, "
2521 "time_benefit: %i\n", size
, base_time
- time
);
2524 || node
->will_be_removed_from_program_if_no_direct_calls_p ())
2526 info
->do_clone_for_all_contexts
= true;
2530 fprintf (dump_file
, " Decided to specialize for all "
2531 "known contexts, code not going to grow.\n");
2533 else if (good_cloning_opportunity_p (node
, base_time
- time
,
2534 stats
.freq_sum
, stats
.count_sum
,
2537 if (size
+ overall_size
<= max_new_size
)
2539 info
->do_clone_for_all_contexts
= true;
2541 overall_size
+= size
;
2544 fprintf (dump_file
, " Decided to specialize for all "
2545 "known contexts, growth deemed beneficial.\n");
2547 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2548 fprintf (dump_file
, " Not cloning for all contexts because "
2549 "max_new_size would be reached with %li.\n",
2550 size
+ overall_size
);
2554 for (i
= 0; i
< count
; i
++)
2556 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2557 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
2558 ipcp_value
<tree
> *val
;
2565 for (val
= lat
->values
; val
; val
= val
->next
)
2567 gcc_checking_assert (TREE_CODE (val
->value
) != TREE_BINFO
);
2568 known_csts
[i
] = val
->value
;
2570 int emc
= estimate_move_cost (TREE_TYPE (val
->value
), true);
2571 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
2572 known_aggs_ptrs
, base_time
,
2573 removable_params_cost
, emc
, val
);
2575 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2577 fprintf (dump_file
, " - estimates for value ");
2578 print_ipcp_constant_value (dump_file
, val
->value
);
2579 fprintf (dump_file
, " for ");
2580 ipa_dump_param (dump_file
, info
, i
);
2581 fprintf (dump_file
, ": time_benefit: %i, size: %i\n",
2582 val
->local_time_benefit
, val
->local_size_cost
);
2585 known_csts
[i
] = NULL_TREE
;
2588 for (i
= 0; i
< count
; i
++)
2590 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2592 if (!plats
->virt_call
)
2595 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
2596 ipcp_value
<ipa_polymorphic_call_context
> *val
;
2600 || !known_contexts
[i
].useless_p ())
2603 for (val
= ctxlat
->values
; val
; val
= val
->next
)
2605 known_contexts
[i
] = val
->value
;
2606 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
2607 known_aggs_ptrs
, base_time
,
2608 removable_params_cost
, 0, val
);
2610 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2612 fprintf (dump_file
, " - estimates for polymorphic context ");
2613 print_ipcp_constant_value (dump_file
, val
->value
);
2614 fprintf (dump_file
, " for ");
2615 ipa_dump_param (dump_file
, info
, i
);
2616 fprintf (dump_file
, ": time_benefit: %i, size: %i\n",
2617 val
->local_time_benefit
, val
->local_size_cost
);
2620 known_contexts
[i
] = ipa_polymorphic_call_context ();
2623 for (i
= 0; i
< count
; i
++)
2625 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2626 struct ipa_agg_jump_function
*ajf
;
2627 struct ipcp_agg_lattice
*aglat
;
2629 if (plats
->aggs_bottom
|| !plats
->aggs
)
2632 ajf
= &known_aggs
[i
];
2633 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2635 ipcp_value
<tree
> *val
;
2636 if (aglat
->bottom
|| !aglat
->values
2637 /* If the following is true, the one value is in known_aggs. */
2638 || (!plats
->aggs_contain_variable
2639 && aglat
->is_single_const ()))
2642 for (val
= aglat
->values
; val
; val
= val
->next
)
2644 struct ipa_agg_jf_item item
;
2646 item
.offset
= aglat
->offset
;
2647 item
.value
= val
->value
;
2648 vec_safe_push (ajf
->items
, item
);
2650 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
2651 known_aggs_ptrs
, base_time
,
2652 removable_params_cost
, 0, val
);
2654 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2656 fprintf (dump_file
, " - estimates for value ");
2657 print_ipcp_constant_value (dump_file
, val
->value
);
2658 fprintf (dump_file
, " for ");
2659 ipa_dump_param (dump_file
, info
, i
);
2660 fprintf (dump_file
, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
2661 "]: time_benefit: %i, size: %i\n",
2662 plats
->aggs_by_ref
? "ref " : "",
2664 val
->local_time_benefit
, val
->local_size_cost
);
2672 for (i
= 0; i
< count
; i
++)
2673 vec_free (known_aggs
[i
].items
);
2675 known_csts
.release ();
2676 known_contexts
.release ();
2677 known_aggs
.release ();
2678 known_aggs_ptrs
.release ();
2682 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
2683 topological sort of values. */
2685 template <typename valtype
>
2687 value_topo_info
<valtype
>::add_val (ipcp_value
<valtype
> *cur_val
)
2689 ipcp_value_source
<valtype
> *src
;
2695 cur_val
->dfs
= dfs_counter
;
2696 cur_val
->low_link
= dfs_counter
;
2698 cur_val
->topo_next
= stack
;
2700 cur_val
->on_stack
= true;
2702 for (src
= cur_val
->sources
; src
; src
= src
->next
)
2705 if (src
->val
->dfs
== 0)
2708 if (src
->val
->low_link
< cur_val
->low_link
)
2709 cur_val
->low_link
= src
->val
->low_link
;
2711 else if (src
->val
->on_stack
2712 && src
->val
->dfs
< cur_val
->low_link
)
2713 cur_val
->low_link
= src
->val
->dfs
;
2716 if (cur_val
->dfs
== cur_val
->low_link
)
2718 ipcp_value
<valtype
> *v
, *scc_list
= NULL
;
2723 stack
= v
->topo_next
;
2724 v
->on_stack
= false;
2726 v
->scc_next
= scc_list
;
2729 while (v
!= cur_val
);
2731 cur_val
->topo_next
= values_topo
;
2732 values_topo
= cur_val
;
2736 /* Add all values in lattices associated with NODE to the topological sort if
2737 they are not there yet. */
2740 add_all_node_vals_to_toposort (cgraph_node
*node
, ipa_topo_info
*topo
)
2742 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2743 int i
, count
= ipa_get_param_count (info
);
2745 for (i
= 0; i
< count
; i
++)
2747 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2748 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
2749 struct ipcp_agg_lattice
*aglat
;
2753 ipcp_value
<tree
> *val
;
2754 for (val
= lat
->values
; val
; val
= val
->next
)
2755 topo
->constants
.add_val (val
);
2758 if (!plats
->aggs_bottom
)
2759 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
2762 ipcp_value
<tree
> *val
;
2763 for (val
= aglat
->values
; val
; val
= val
->next
)
2764 topo
->constants
.add_val (val
);
2767 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
2768 if (!ctxlat
->bottom
)
2770 ipcp_value
<ipa_polymorphic_call_context
> *ctxval
;
2771 for (ctxval
= ctxlat
->values
; ctxval
; ctxval
= ctxval
->next
)
2772 topo
->contexts
.add_val (ctxval
);
2777 /* One pass of constants propagation along the call graph edges, from callers
2778 to callees (requires topological ordering in TOPO), iterate over strongly
2779 connected components. */
2782 propagate_constants_topo (struct ipa_topo_info
*topo
)
2786 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
2789 struct cgraph_node
*v
, *node
= topo
->order
[i
];
2790 vec
<cgraph_node
*> cycle_nodes
= ipa_get_nodes_in_cycle (node
);
2792 /* First, iteratively propagate within the strongly connected component
2793 until all lattices stabilize. */
2794 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
2795 if (v
->has_gimple_body_p ())
2796 push_node_to_stack (topo
, v
);
2798 v
= pop_node_from_stack (topo
);
2801 struct cgraph_edge
*cs
;
2803 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
2804 if (ipa_edge_within_scc (cs
))
2806 IPA_NODE_REF (v
)->node_within_scc
= true;
2807 if (propagate_constants_accross_call (cs
))
2808 push_node_to_stack (topo
, cs
->callee
->function_symbol ());
2810 v
= pop_node_from_stack (topo
);
2813 /* Afterwards, propagate along edges leading out of the SCC, calculates
2814 the local effects of the discovered constants and all valid values to
2815 their topological sort. */
2816 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
2817 if (v
->has_gimple_body_p ())
2819 struct cgraph_edge
*cs
;
2821 estimate_local_effects (v
);
2822 add_all_node_vals_to_toposort (v
, topo
);
2823 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
2824 if (!ipa_edge_within_scc (cs
))
2825 propagate_constants_accross_call (cs
);
2827 cycle_nodes
.release ();
2832 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
2833 the bigger one if otherwise. */
2836 safe_add (int a
, int b
)
2838 if (a
> INT_MAX
/2 || b
> INT_MAX
/2)
2839 return a
> b
? a
: b
;
2845 /* Propagate the estimated effects of individual values along the topological
2846 from the dependent values to those they depend on. */
2848 template <typename valtype
>
2850 value_topo_info
<valtype
>::propagate_effects ()
2852 ipcp_value
<valtype
> *base
;
2854 for (base
= values_topo
; base
; base
= base
->topo_next
)
2856 ipcp_value_source
<valtype
> *src
;
2857 ipcp_value
<valtype
> *val
;
2858 int time
= 0, size
= 0;
2860 for (val
= base
; val
; val
= val
->scc_next
)
2862 time
= safe_add (time
,
2863 val
->local_time_benefit
+ val
->prop_time_benefit
);
2864 size
= safe_add (size
, val
->local_size_cost
+ val
->prop_size_cost
);
2867 for (val
= base
; val
; val
= val
->scc_next
)
2868 for (src
= val
->sources
; src
; src
= src
->next
)
2870 && src
->cs
->maybe_hot_p ())
2872 src
->val
->prop_time_benefit
= safe_add (time
,
2873 src
->val
->prop_time_benefit
);
2874 src
->val
->prop_size_cost
= safe_add (size
,
2875 src
->val
->prop_size_cost
);
2881 /* Propagate constants, polymorphic contexts and their effects from the
2882 summaries interprocedurally. */
2885 ipcp_propagate_stage (struct ipa_topo_info
*topo
)
2887 struct cgraph_node
*node
;
2890 fprintf (dump_file
, "\n Propagating constants:\n\n");
2893 ipa_update_after_lto_read ();
2896 FOR_EACH_DEFINED_FUNCTION (node
)
2898 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2900 determine_versionability (node
, info
);
2901 if (node
->has_gimple_body_p ())
2903 info
->lattices
= XCNEWVEC (struct ipcp_param_lattices
,
2904 ipa_get_param_count (info
));
2905 initialize_node_lattices (node
);
2907 if (node
->definition
&& !node
->alias
)
2908 overall_size
+= inline_summaries
->get (node
)->self_size
;
2909 if (node
->count
> max_count
)
2910 max_count
= node
->count
;
2913 max_new_size
= overall_size
;
2914 if (max_new_size
< PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
))
2915 max_new_size
= PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
);
2916 max_new_size
+= max_new_size
* PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH
) / 100 + 1;
2919 fprintf (dump_file
, "\noverall_size: %li, max_new_size: %li\n",
2920 overall_size
, max_new_size
);
2922 propagate_constants_topo (topo
);
2923 #ifdef ENABLE_CHECKING
2924 ipcp_verify_propagated_values ();
2926 topo
->constants
.propagate_effects ();
2927 topo
->contexts
.propagate_effects ();
2931 fprintf (dump_file
, "\nIPA lattices after all propagation:\n");
2932 print_all_lattices (dump_file
, (dump_flags
& TDF_DETAILS
), true);
2936 /* Discover newly direct outgoing edges from NODE which is a new clone with
2937 known KNOWN_CSTS and make them direct. */
2940 ipcp_discover_new_direct_edges (struct cgraph_node
*node
,
2941 vec
<tree
> known_csts
,
2942 vec
<ipa_polymorphic_call_context
>
2944 struct ipa_agg_replacement_value
*aggvals
)
2946 struct cgraph_edge
*ie
, *next_ie
;
2949 for (ie
= node
->indirect_calls
; ie
; ie
= next_ie
)
2954 next_ie
= ie
->next_callee
;
2955 target
= ipa_get_indirect_edge_target_1 (ie
, known_csts
, known_contexts
,
2956 vNULL
, aggvals
, &speculative
);
2959 bool agg_contents
= ie
->indirect_info
->agg_contents
;
2960 bool polymorphic
= ie
->indirect_info
->polymorphic
;
2961 int param_index
= ie
->indirect_info
->param_index
;
2962 struct cgraph_edge
*cs
= ipa_make_edge_direct_to_target (ie
, target
,
2966 if (cs
&& !agg_contents
&& !polymorphic
)
2968 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2969 int c
= ipa_get_controlled_uses (info
, param_index
);
2970 if (c
!= IPA_UNDESCRIBED_USE
)
2972 struct ipa_ref
*to_del
;
2975 ipa_set_controlled_uses (info
, param_index
, c
);
2976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2977 fprintf (dump_file
, " controlled uses count of param "
2978 "%i bumped down to %i\n", param_index
, c
);
2980 && (to_del
= node
->find_reference (cs
->callee
, NULL
, 0)))
2982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2983 fprintf (dump_file
, " and even removing its "
2984 "cloning-created reference\n");
2985 to_del
->remove_reference ();
2991 /* Turning calls to direct calls will improve overall summary. */
2993 inline_update_overall_summary (node
);
2996 /* Vector of pointers which for linked lists of clones of an original crgaph
2999 static vec
<cgraph_edge
*> next_edge_clone
;
3000 static vec
<cgraph_edge
*> prev_edge_clone
;
3003 grow_edge_clone_vectors (void)
3005 if (next_edge_clone
.length ()
3006 <= (unsigned) symtab
->edges_max_uid
)
3007 next_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
3008 if (prev_edge_clone
.length ()
3009 <= (unsigned) symtab
->edges_max_uid
)
3010 prev_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
3013 /* Edge duplication hook to grow the appropriate linked list in
3017 ipcp_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
3020 grow_edge_clone_vectors ();
3022 struct cgraph_edge
*old_next
= next_edge_clone
[src
->uid
];
3024 prev_edge_clone
[old_next
->uid
] = dst
;
3025 prev_edge_clone
[dst
->uid
] = src
;
3027 next_edge_clone
[dst
->uid
] = old_next
;
3028 next_edge_clone
[src
->uid
] = dst
;
3031 /* Hook that is called by cgraph.c when an edge is removed. */
3034 ipcp_edge_removal_hook (struct cgraph_edge
*cs
, void *)
3036 grow_edge_clone_vectors ();
3038 struct cgraph_edge
*prev
= prev_edge_clone
[cs
->uid
];
3039 struct cgraph_edge
*next
= next_edge_clone
[cs
->uid
];
3041 next_edge_clone
[prev
->uid
] = next
;
3043 prev_edge_clone
[next
->uid
] = prev
;
3046 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
3047 parameter with the given INDEX. */
3050 get_clone_agg_value (struct cgraph_node
*node
, HOST_WIDE_INT offset
,
3053 struct ipa_agg_replacement_value
*aggval
;
3055 aggval
= ipa_get_agg_replacements_for_node (node
);
3058 if (aggval
->offset
== offset
3059 && aggval
->index
== index
)
3060 return aggval
->value
;
3061 aggval
= aggval
->next
;
3066 /* Return true is NODE is DEST or its clone for all contexts. */
3069 same_node_or_its_all_contexts_clone_p (cgraph_node
*node
, cgraph_node
*dest
)
3074 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3075 return info
->is_all_contexts_clone
&& info
->ipcp_orig_node
== dest
;
3078 /* Return true if edge CS does bring about the value described by SRC to node
3079 DEST or its clone for all contexts. */
3082 cgraph_edge_brings_value_p (cgraph_edge
*cs
, ipcp_value_source
<tree
> *src
,
3085 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3086 enum availability availability
;
3087 cgraph_node
*real_dest
= cs
->callee
->function_symbol (&availability
);
3089 if (!same_node_or_its_all_contexts_clone_p (real_dest
, dest
)
3090 || availability
<= AVAIL_INTERPOSABLE
3091 || caller_info
->node_dead
)
3096 if (caller_info
->ipcp_orig_node
)
3099 if (src
->offset
== -1)
3100 t
= caller_info
->known_csts
[src
->index
];
3102 t
= get_clone_agg_value (cs
->caller
, src
->offset
, src
->index
);
3103 return (t
!= NULL_TREE
3104 && values_equal_for_ipcp_p (src
->val
->value
, t
));
3108 struct ipcp_agg_lattice
*aglat
;
3109 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (caller_info
,
3111 if (src
->offset
== -1)
3112 return (plats
->itself
.is_single_const ()
3113 && values_equal_for_ipcp_p (src
->val
->value
,
3114 plats
->itself
.values
->value
));
3117 if (plats
->aggs_bottom
|| plats
->aggs_contain_variable
)
3119 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3120 if (aglat
->offset
== src
->offset
)
3121 return (aglat
->is_single_const ()
3122 && values_equal_for_ipcp_p (src
->val
->value
,
3123 aglat
->values
->value
));
3129 /* Return true if edge CS does bring about the value described by SRC to node
3130 DEST or its clone for all contexts. */
3133 cgraph_edge_brings_value_p (cgraph_edge
*cs
,
3134 ipcp_value_source
<ipa_polymorphic_call_context
> *src
,
3137 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3138 cgraph_node
*real_dest
= cs
->callee
->function_symbol ();
3140 if (!same_node_or_its_all_contexts_clone_p (real_dest
, dest
)
3141 || caller_info
->node_dead
)
3146 if (caller_info
->ipcp_orig_node
)
3147 return (caller_info
->known_contexts
.length () > (unsigned) src
->index
)
3148 && values_equal_for_ipcp_p (src
->val
->value
,
3149 caller_info
->known_contexts
[src
->index
]);
3151 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (caller_info
,
3153 return plats
->ctxlat
.is_single_const ()
3154 && values_equal_for_ipcp_p (src
->val
->value
,
3155 plats
->ctxlat
.values
->value
);
3158 /* Get the next clone in the linked list of clones of an edge. */
3160 static inline struct cgraph_edge
*
3161 get_next_cgraph_edge_clone (struct cgraph_edge
*cs
)
3163 return next_edge_clone
[cs
->uid
];
3166 /* Given VAL that is intended for DEST, iterate over all its sources and if
3167 they still hold, add their edge frequency and their number into *FREQUENCY
3168 and *CALLER_COUNT respectively. */
3170 template <typename valtype
>
3172 get_info_about_necessary_edges (ipcp_value
<valtype
> *val
, cgraph_node
*dest
,
3174 gcov_type
*count_sum
, int *caller_count
)
3176 ipcp_value_source
<valtype
> *src
;
3177 int freq
= 0, count
= 0;
3181 for (src
= val
->sources
; src
; src
= src
->next
)
3183 struct cgraph_edge
*cs
= src
->cs
;
3186 if (cgraph_edge_brings_value_p (cs
, src
, dest
))
3189 freq
+= cs
->frequency
;
3191 hot
|= cs
->maybe_hot_p ();
3193 cs
= get_next_cgraph_edge_clone (cs
);
3199 *caller_count
= count
;
3203 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3204 is assumed their number is known and equal to CALLER_COUNT. */
3206 template <typename valtype
>
3207 static vec
<cgraph_edge
*>
3208 gather_edges_for_value (ipcp_value
<valtype
> *val
, cgraph_node
*dest
,
3211 ipcp_value_source
<valtype
> *src
;
3212 vec
<cgraph_edge
*> ret
;
3214 ret
.create (caller_count
);
3215 for (src
= val
->sources
; src
; src
= src
->next
)
3217 struct cgraph_edge
*cs
= src
->cs
;
3220 if (cgraph_edge_brings_value_p (cs
, src
, dest
))
3221 ret
.quick_push (cs
);
3222 cs
= get_next_cgraph_edge_clone (cs
);
3229 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3230 Return it or NULL if for some reason it cannot be created. */
3232 static struct ipa_replace_map
*
3233 get_replacement_map (struct ipa_node_params
*info
, tree value
, int parm_num
)
3235 struct ipa_replace_map
*replace_map
;
3238 replace_map
= ggc_alloc
<ipa_replace_map
> ();
3241 fprintf (dump_file
, " replacing ");
3242 ipa_dump_param (dump_file
, info
, parm_num
);
3244 fprintf (dump_file
, " with const ");
3245 print_generic_expr (dump_file
, value
, 0);
3246 fprintf (dump_file
, "\n");
3248 replace_map
->old_tree
= NULL
;
3249 replace_map
->parm_num
= parm_num
;
3250 replace_map
->new_tree
= value
;
3251 replace_map
->replace_p
= true;
3252 replace_map
->ref_p
= false;
3257 /* Dump new profiling counts */
3260 dump_profile_updates (struct cgraph_node
*orig_node
,
3261 struct cgraph_node
*new_node
)
3263 struct cgraph_edge
*cs
;
3265 fprintf (dump_file
, " setting count of the specialized node to "
3266 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) new_node
->count
);
3267 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3268 fprintf (dump_file
, " edge to %s has count "
3269 HOST_WIDE_INT_PRINT_DEC
"\n",
3270 cs
->callee
->name (), (HOST_WIDE_INT
) cs
->count
);
3272 fprintf (dump_file
, " setting count of the original node to "
3273 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) orig_node
->count
);
3274 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3275 fprintf (dump_file
, " edge to %s is left with "
3276 HOST_WIDE_INT_PRINT_DEC
"\n",
3277 cs
->callee
->name (), (HOST_WIDE_INT
) cs
->count
);
3280 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3281 their profile information to reflect this. */
3284 update_profiling_info (struct cgraph_node
*orig_node
,
3285 struct cgraph_node
*new_node
)
3287 struct cgraph_edge
*cs
;
3288 struct caller_statistics stats
;
3289 gcov_type new_sum
, orig_sum
;
3290 gcov_type remainder
, orig_node_count
= orig_node
->count
;
3292 if (orig_node_count
== 0)
3295 init_caller_stats (&stats
);
3296 orig_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
3298 orig_sum
= stats
.count_sum
;
3299 init_caller_stats (&stats
);
3300 new_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
3302 new_sum
= stats
.count_sum
;
3304 if (orig_node_count
< orig_sum
+ new_sum
)
3307 fprintf (dump_file
, " Problem: node %s/%i has too low count "
3308 HOST_WIDE_INT_PRINT_DEC
" while the sum of incoming "
3309 "counts is " HOST_WIDE_INT_PRINT_DEC
"\n",
3310 orig_node
->name (), orig_node
->order
,
3311 (HOST_WIDE_INT
) orig_node_count
,
3312 (HOST_WIDE_INT
) (orig_sum
+ new_sum
));
3314 orig_node_count
= (orig_sum
+ new_sum
) * 12 / 10;
3316 fprintf (dump_file
, " proceeding by pretending it was "
3317 HOST_WIDE_INT_PRINT_DEC
"\n",
3318 (HOST_WIDE_INT
) orig_node_count
);
3321 new_node
->count
= new_sum
;
3322 remainder
= orig_node_count
- new_sum
;
3323 orig_node
->count
= remainder
;
3325 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3327 cs
->count
= apply_probability (cs
->count
,
3328 GCOV_COMPUTE_SCALE (new_sum
,
3333 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3334 cs
->count
= apply_probability (cs
->count
,
3335 GCOV_COMPUTE_SCALE (remainder
,
3339 dump_profile_updates (orig_node
, new_node
);
3342 /* Update the respective profile of specialized NEW_NODE and the original
3343 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3344 have been redirected to the specialized version. */
3347 update_specialized_profile (struct cgraph_node
*new_node
,
3348 struct cgraph_node
*orig_node
,
3349 gcov_type redirected_sum
)
3351 struct cgraph_edge
*cs
;
3352 gcov_type new_node_count
, orig_node_count
= orig_node
->count
;
3355 fprintf (dump_file
, " the sum of counts of redirected edges is "
3356 HOST_WIDE_INT_PRINT_DEC
"\n", (HOST_WIDE_INT
) redirected_sum
);
3357 if (orig_node_count
== 0)
3360 gcc_assert (orig_node_count
>= redirected_sum
);
3362 new_node_count
= new_node
->count
;
3363 new_node
->count
+= redirected_sum
;
3364 orig_node
->count
-= redirected_sum
;
3366 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3368 cs
->count
+= apply_probability (cs
->count
,
3369 GCOV_COMPUTE_SCALE (redirected_sum
,
3374 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3376 gcov_type dec
= apply_probability (cs
->count
,
3377 GCOV_COMPUTE_SCALE (redirected_sum
,
3379 if (dec
< cs
->count
)
3386 dump_profile_updates (orig_node
, new_node
);
3389 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3390 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3391 redirect all edges in CALLERS to it. */
3393 static struct cgraph_node
*
3394 create_specialized_node (struct cgraph_node
*node
,
3395 vec
<tree
> known_csts
,
3396 vec
<ipa_polymorphic_call_context
> known_contexts
,
3397 struct ipa_agg_replacement_value
*aggvals
,
3398 vec
<cgraph_edge
*> callers
)
3400 struct ipa_node_params
*new_info
, *info
= IPA_NODE_REF (node
);
3401 vec
<ipa_replace_map
*, va_gc
> *replace_trees
= NULL
;
3402 struct ipa_agg_replacement_value
*av
;
3403 struct cgraph_node
*new_node
;
3404 int i
, count
= ipa_get_param_count (info
);
3405 bitmap args_to_skip
;
3407 gcc_assert (!info
->ipcp_orig_node
);
3409 if (node
->local
.can_change_signature
)
3411 args_to_skip
= BITMAP_GGC_ALLOC ();
3412 for (i
= 0; i
< count
; i
++)
3414 tree t
= known_csts
[i
];
3416 if (t
|| !ipa_is_param_used (info
, i
))
3417 bitmap_set_bit (args_to_skip
, i
);
3422 args_to_skip
= NULL
;
3423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3424 fprintf (dump_file
, " cannot change function signature\n");
3427 for (i
= 0; i
< count
; i
++)
3429 tree t
= known_csts
[i
];
3432 struct ipa_replace_map
*replace_map
;
3434 gcc_checking_assert (TREE_CODE (t
) != TREE_BINFO
);
3435 replace_map
= get_replacement_map (info
, t
, i
);
3437 vec_safe_push (replace_trees
, replace_map
);
3441 new_node
= node
->create_virtual_clone (callers
, replace_trees
,
3442 args_to_skip
, "constprop");
3443 ipa_set_node_agg_value_chain (new_node
, aggvals
);
3444 for (av
= aggvals
; av
; av
= av
->next
)
3445 new_node
->maybe_create_reference (av
->value
, IPA_REF_ADDR
, NULL
);
3447 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3449 fprintf (dump_file
, " the new node is %s/%i.\n",
3450 new_node
->name (), new_node
->order
);
3451 if (known_contexts
.exists ())
3453 for (i
= 0; i
< count
; i
++)
3454 if (!known_contexts
[i
].useless_p ())
3456 fprintf (dump_file
, " known ctx %i is ", i
);
3457 known_contexts
[i
].dump (dump_file
);
3461 ipa_dump_agg_replacement_values (dump_file
, aggvals
);
3463 ipa_check_create_node_params ();
3464 update_profiling_info (node
, new_node
);
3465 new_info
= IPA_NODE_REF (new_node
);
3466 new_info
->ipcp_orig_node
= node
;
3467 new_info
->known_csts
= known_csts
;
3468 new_info
->known_contexts
= known_contexts
;
3470 ipcp_discover_new_direct_edges (new_node
, known_csts
, known_contexts
, aggvals
);
3476 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3477 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3480 find_more_scalar_values_for_callers_subset (struct cgraph_node
*node
,
3481 vec
<tree
> known_csts
,
3482 vec
<cgraph_edge
*> callers
)
3484 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3485 int i
, count
= ipa_get_param_count (info
);
3487 for (i
= 0; i
< count
; i
++)
3489 struct cgraph_edge
*cs
;
3490 tree newval
= NULL_TREE
;
3494 if (ipa_get_scalar_lat (info
, i
)->bottom
|| known_csts
[i
])
3497 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3499 struct ipa_jump_func
*jump_func
;
3502 if (i
>= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
)))
3507 jump_func
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), i
);
3508 t
= ipa_value_from_jfunc (IPA_NODE_REF (cs
->caller
), jump_func
);
3511 && !values_equal_for_ipcp_p (t
, newval
))
3512 || (!first
&& !newval
))
3524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3526 fprintf (dump_file
, " adding an extra known scalar value ");
3527 print_ipcp_constant_value (dump_file
, newval
);
3528 fprintf (dump_file
, " for ");
3529 ipa_dump_param (dump_file
, info
, i
);
3530 fprintf (dump_file
, "\n");
3533 known_csts
[i
] = newval
;
3538 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3539 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3543 find_more_contexts_for_caller_subset (cgraph_node
*node
,
3544 vec
<ipa_polymorphic_call_context
>
3546 vec
<cgraph_edge
*> callers
)
3548 ipa_node_params
*info
= IPA_NODE_REF (node
);
3549 int i
, count
= ipa_get_param_count (info
);
3551 for (i
= 0; i
< count
; i
++)
3555 if (ipa_get_poly_ctx_lat (info
, i
)->bottom
3556 || (known_contexts
->exists ()
3557 && !(*known_contexts
)[i
].useless_p ()))
3560 ipa_polymorphic_call_context newval
;
3564 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3566 if (i
>= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
)))
3568 ipa_jump_func
*jfunc
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
),
3570 ipa_polymorphic_call_context ctx
;
3571 ctx
= ipa_context_from_jfunc (IPA_NODE_REF (cs
->caller
), cs
, i
,
3579 newval
.meet_with (ctx
);
3580 if (newval
.useless_p ())
3584 if (!newval
.useless_p ())
3586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3588 fprintf (dump_file
, " adding an extra known polymorphic "
3590 print_ipcp_constant_value (dump_file
, newval
);
3591 fprintf (dump_file
, " for ");
3592 ipa_dump_param (dump_file
, info
, i
);
3593 fprintf (dump_file
, "\n");
3596 if (!known_contexts
->exists ())
3597 known_contexts
->safe_grow_cleared (ipa_get_param_count (info
));
3598 (*known_contexts
)[i
] = newval
;
3604 /* Go through PLATS and create a vector of values consisting of values and
3605 offsets (minus OFFSET) of lattices that contain only a single value. */
3607 static vec
<ipa_agg_jf_item
>
3608 copy_plats_to_inter (struct ipcp_param_lattices
*plats
, HOST_WIDE_INT offset
)
3610 vec
<ipa_agg_jf_item
> res
= vNULL
;
3612 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
3615 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3616 if (aglat
->is_single_const ())
3618 struct ipa_agg_jf_item ti
;
3619 ti
.offset
= aglat
->offset
- offset
;
3620 ti
.value
= aglat
->values
->value
;
3626 /* Intersect all values in INTER with single value lattices in PLATS (while
3627 subtracting OFFSET). */
3630 intersect_with_plats (struct ipcp_param_lattices
*plats
,
3631 vec
<ipa_agg_jf_item
> *inter
,
3632 HOST_WIDE_INT offset
)
3634 struct ipcp_agg_lattice
*aglat
;
3635 struct ipa_agg_jf_item
*item
;
3638 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
3644 aglat
= plats
->aggs
;
3645 FOR_EACH_VEC_ELT (*inter
, k
, item
)
3652 if (aglat
->offset
- offset
> item
->offset
)
3654 if (aglat
->offset
- offset
== item
->offset
)
3656 gcc_checking_assert (item
->value
);
3657 if (values_equal_for_ipcp_p (item
->value
, aglat
->values
->value
))
3661 aglat
= aglat
->next
;
3664 item
->value
= NULL_TREE
;
3668 /* Copy agggregate replacement values of NODE (which is an IPA-CP clone) to the
3669 vector result while subtracting OFFSET from the individual value offsets. */
3671 static vec
<ipa_agg_jf_item
>
3672 agg_replacements_to_vector (struct cgraph_node
*node
, int index
,
3673 HOST_WIDE_INT offset
)
3675 struct ipa_agg_replacement_value
*av
;
3676 vec
<ipa_agg_jf_item
> res
= vNULL
;
3678 for (av
= ipa_get_agg_replacements_for_node (node
); av
; av
= av
->next
)
3679 if (av
->index
== index
3680 && (av
->offset
- offset
) >= 0)
3682 struct ipa_agg_jf_item item
;
3683 gcc_checking_assert (av
->value
);
3684 item
.offset
= av
->offset
- offset
;
3685 item
.value
= av
->value
;
3686 res
.safe_push (item
);
3692 /* Intersect all values in INTER with those that we have already scheduled to
3693 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
3694 (while subtracting OFFSET). */
3697 intersect_with_agg_replacements (struct cgraph_node
*node
, int index
,
3698 vec
<ipa_agg_jf_item
> *inter
,
3699 HOST_WIDE_INT offset
)
3701 struct ipa_agg_replacement_value
*srcvals
;
3702 struct ipa_agg_jf_item
*item
;
3705 srcvals
= ipa_get_agg_replacements_for_node (node
);
3712 FOR_EACH_VEC_ELT (*inter
, i
, item
)
3714 struct ipa_agg_replacement_value
*av
;
3718 for (av
= srcvals
; av
; av
= av
->next
)
3720 gcc_checking_assert (av
->value
);
3721 if (av
->index
== index
3722 && av
->offset
- offset
== item
->offset
)
3724 if (values_equal_for_ipcp_p (item
->value
, av
->value
))
3730 item
->value
= NULL_TREE
;
3734 /* Intersect values in INTER with aggregate values that come along edge CS to
3735 parameter number INDEX and return it. If INTER does not actually exist yet,
3736 copy all incoming values to it. If we determine we ended up with no values
3737 whatsoever, return a released vector. */
3739 static vec
<ipa_agg_jf_item
>
3740 intersect_aggregates_with_edge (struct cgraph_edge
*cs
, int index
,
3741 vec
<ipa_agg_jf_item
> inter
)
3743 struct ipa_jump_func
*jfunc
;
3744 jfunc
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), index
);
3745 if (jfunc
->type
== IPA_JF_PASS_THROUGH
3746 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
3748 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3749 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
3751 if (caller_info
->ipcp_orig_node
)
3753 struct cgraph_node
*orig_node
= caller_info
->ipcp_orig_node
;
3754 struct ipcp_param_lattices
*orig_plats
;
3755 orig_plats
= ipa_get_parm_lattices (IPA_NODE_REF (orig_node
),
3757 if (agg_pass_through_permissible_p (orig_plats
, jfunc
))
3759 if (!inter
.exists ())
3760 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, 0);
3762 intersect_with_agg_replacements (cs
->caller
, src_idx
,
3773 struct ipcp_param_lattices
*src_plats
;
3774 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
3775 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
3777 /* Currently we do not produce clobber aggregate jump
3778 functions, adjust when we do. */
3779 gcc_checking_assert (!jfunc
->agg
.items
);
3780 if (!inter
.exists ())
3781 inter
= copy_plats_to_inter (src_plats
, 0);
3783 intersect_with_plats (src_plats
, &inter
, 0);
3792 else if (jfunc
->type
== IPA_JF_ANCESTOR
3793 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
3795 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3796 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
3797 struct ipcp_param_lattices
*src_plats
;
3798 HOST_WIDE_INT delta
= ipa_get_jf_ancestor_offset (jfunc
);
3800 if (caller_info
->ipcp_orig_node
)
3802 if (!inter
.exists ())
3803 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, delta
);
3805 intersect_with_agg_replacements (cs
->caller
, src_idx
, &inter
,
3810 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);;
3811 /* Currently we do not produce clobber aggregate jump
3812 functions, adjust when we do. */
3813 gcc_checking_assert (!src_plats
->aggs
|| !jfunc
->agg
.items
);
3814 if (!inter
.exists ())
3815 inter
= copy_plats_to_inter (src_plats
, delta
);
3817 intersect_with_plats (src_plats
, &inter
, delta
);
3820 else if (jfunc
->agg
.items
)
3822 struct ipa_agg_jf_item
*item
;
3825 if (!inter
.exists ())
3826 for (unsigned i
= 0; i
< jfunc
->agg
.items
->length (); i
++)
3827 inter
.safe_push ((*jfunc
->agg
.items
)[i
]);
3829 FOR_EACH_VEC_ELT (inter
, k
, item
)
3832 bool found
= false;;
3837 while ((unsigned) l
< jfunc
->agg
.items
->length ())
3839 struct ipa_agg_jf_item
*ti
;
3840 ti
= &(*jfunc
->agg
.items
)[l
];
3841 if (ti
->offset
> item
->offset
)
3843 if (ti
->offset
== item
->offset
)
3845 gcc_checking_assert (ti
->value
);
3846 if (values_equal_for_ipcp_p (item
->value
,
3860 return vec
<ipa_agg_jf_item
>();
3865 /* Look at edges in CALLERS and collect all known aggregate values that arrive
3866 from all of them. */
3868 static struct ipa_agg_replacement_value
*
3869 find_aggregate_values_for_callers_subset (struct cgraph_node
*node
,
3870 vec
<cgraph_edge
*> callers
)
3872 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
3873 struct ipa_agg_replacement_value
*res
;
3874 struct ipa_agg_replacement_value
**tail
= &res
;
3875 struct cgraph_edge
*cs
;
3876 int i
, j
, count
= ipa_get_param_count (dest_info
);
3878 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3880 int c
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
3885 for (i
= 0; i
< count
; i
++)
3887 struct cgraph_edge
*cs
;
3888 vec
<ipa_agg_jf_item
> inter
= vNULL
;
3889 struct ipa_agg_jf_item
*item
;
3890 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (dest_info
, i
);
3893 /* Among other things, the following check should deal with all by_ref
3895 if (plats
->aggs_bottom
)
3898 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3900 inter
= intersect_aggregates_with_edge (cs
, i
, inter
);
3902 if (!inter
.exists ())
3906 FOR_EACH_VEC_ELT (inter
, j
, item
)
3908 struct ipa_agg_replacement_value
*v
;
3913 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
3915 v
->offset
= item
->offset
;
3916 v
->value
= item
->value
;
3917 v
->by_ref
= plats
->aggs_by_ref
;
3923 if (inter
.exists ())
3930 /* Turn KNOWN_AGGS into a list of aggreate replacement values. */
3932 static struct ipa_agg_replacement_value
*
3933 known_aggs_to_agg_replacement_list (vec
<ipa_agg_jump_function
> known_aggs
)
3935 struct ipa_agg_replacement_value
*res
;
3936 struct ipa_agg_replacement_value
**tail
= &res
;
3937 struct ipa_agg_jump_function
*aggjf
;
3938 struct ipa_agg_jf_item
*item
;
3941 FOR_EACH_VEC_ELT (known_aggs
, i
, aggjf
)
3942 FOR_EACH_VEC_SAFE_ELT (aggjf
->items
, j
, item
)
3944 struct ipa_agg_replacement_value
*v
;
3945 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
3947 v
->offset
= item
->offset
;
3948 v
->value
= item
->value
;
3949 v
->by_ref
= aggjf
->by_ref
;
3957 /* Determine whether CS also brings all scalar values that the NODE is
3961 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge
*cs
,
3962 struct cgraph_node
*node
)
3964 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
3965 int count
= ipa_get_param_count (dest_info
);
3966 struct ipa_node_params
*caller_info
;
3967 struct ipa_edge_args
*args
;
3970 caller_info
= IPA_NODE_REF (cs
->caller
);
3971 args
= IPA_EDGE_REF (cs
);
3972 for (i
= 0; i
< count
; i
++)
3974 struct ipa_jump_func
*jump_func
;
3977 val
= dest_info
->known_csts
[i
];
3981 if (i
>= ipa_get_cs_argument_count (args
))
3983 jump_func
= ipa_get_ith_jump_func (args
, i
);
3984 t
= ipa_value_from_jfunc (caller_info
, jump_func
);
3985 if (!t
|| !values_equal_for_ipcp_p (val
, t
))
3991 /* Determine whether CS also brings all aggregate values that NODE is
3994 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge
*cs
,
3995 struct cgraph_node
*node
)
3997 struct ipa_node_params
*orig_caller_info
= IPA_NODE_REF (cs
->caller
);
3998 struct ipa_node_params
*orig_node_info
;
3999 struct ipa_agg_replacement_value
*aggval
;
4002 aggval
= ipa_get_agg_replacements_for_node (node
);
4006 count
= ipa_get_param_count (IPA_NODE_REF (node
));
4007 ec
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
4009 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4010 if (aggval
->index
>= ec
)
4013 orig_node_info
= IPA_NODE_REF (IPA_NODE_REF (node
)->ipcp_orig_node
);
4014 if (orig_caller_info
->ipcp_orig_node
)
4015 orig_caller_info
= IPA_NODE_REF (orig_caller_info
->ipcp_orig_node
);
4017 for (i
= 0; i
< count
; i
++)
4019 static vec
<ipa_agg_jf_item
> values
= vec
<ipa_agg_jf_item
>();
4020 struct ipcp_param_lattices
*plats
;
4021 bool interesting
= false;
4022 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4023 if (aggval
->index
== i
)
4031 plats
= ipa_get_parm_lattices (orig_node_info
, aggval
->index
);
4032 if (plats
->aggs_bottom
)
4035 values
= intersect_aggregates_with_edge (cs
, i
, values
);
4036 if (!values
.exists ())
4039 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4040 if (aggval
->index
== i
)
4042 struct ipa_agg_jf_item
*item
;
4045 FOR_EACH_VEC_ELT (values
, j
, item
)
4047 && item
->offset
== av
->offset
4048 && values_equal_for_ipcp_p (item
->value
, av
->value
))
4063 /* Given an original NODE and a VAL for which we have already created a
4064 specialized clone, look whether there are incoming edges that still lead
4065 into the old node but now also bring the requested value and also conform to
4066 all other criteria such that they can be redirected the special node.
4067 This function can therefore redirect the final edge in a SCC. */
4069 template <typename valtype
>
4071 perhaps_add_new_callers (cgraph_node
*node
, ipcp_value
<valtype
> *val
)
4073 ipcp_value_source
<valtype
> *src
;
4074 gcov_type redirected_sum
= 0;
4076 for (src
= val
->sources
; src
; src
= src
->next
)
4078 struct cgraph_edge
*cs
= src
->cs
;
4081 if (cgraph_edge_brings_value_p (cs
, src
, node
)
4082 && cgraph_edge_brings_all_scalars_for_node (cs
, val
->spec_node
)
4083 && cgraph_edge_brings_all_agg_vals_for_node (cs
, val
->spec_node
))
4086 fprintf (dump_file
, " - adding an extra caller %s/%i"
4088 xstrdup_for_dump (cs
->caller
->name ()),
4090 xstrdup_for_dump (val
->spec_node
->name ()),
4091 val
->spec_node
->order
);
4093 cs
->redirect_callee_duplicating_thunks (val
->spec_node
);
4094 val
->spec_node
->expand_all_artificial_thunks ();
4095 redirected_sum
+= cs
->count
;
4097 cs
= get_next_cgraph_edge_clone (cs
);
4102 update_specialized_profile (val
->spec_node
, node
, redirected_sum
);
4105 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4108 known_contexts_useful_p (vec
<ipa_polymorphic_call_context
> known_contexts
)
4110 ipa_polymorphic_call_context
*ctx
;
4113 FOR_EACH_VEC_ELT (known_contexts
, i
, ctx
)
4114 if (!ctx
->useless_p ())
4119 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4121 static vec
<ipa_polymorphic_call_context
>
4122 copy_useful_known_contexts (vec
<ipa_polymorphic_call_context
> known_contexts
)
4124 if (known_contexts_useful_p (known_contexts
))
4125 return known_contexts
.copy ();
4130 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4131 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4134 modify_known_vectors_with_val (vec
<tree
> *known_csts
,
4135 vec
<ipa_polymorphic_call_context
> *known_contexts
,
4136 ipcp_value
<tree
> *val
,
4139 *known_csts
= known_csts
->copy ();
4140 *known_contexts
= copy_useful_known_contexts (*known_contexts
);
4141 (*known_csts
)[index
] = val
->value
;
4144 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4145 copy according to VAL and INDEX. */
4148 modify_known_vectors_with_val (vec
<tree
> *known_csts
,
4149 vec
<ipa_polymorphic_call_context
> *known_contexts
,
4150 ipcp_value
<ipa_polymorphic_call_context
> *val
,
4153 *known_csts
= known_csts
->copy ();
4154 *known_contexts
= known_contexts
->copy ();
4155 (*known_contexts
)[index
] = val
->value
;
4158 /* Return true if OFFSET indicates this was not an aggregate value or there is
4159 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4163 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value
*aggvals
,
4164 int index
, HOST_WIDE_INT offset
, tree value
)
4171 if (aggvals
->index
== index
4172 && aggvals
->offset
== offset
4173 && values_equal_for_ipcp_p (aggvals
->value
, value
))
4175 aggvals
= aggvals
->next
;
4180 /* Return true if offset is minus one because source of a polymorphic contect
4181 cannot be an aggregate value. */
4184 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value
*,
4185 int , HOST_WIDE_INT offset
,
4186 ipa_polymorphic_call_context
)
4188 return offset
== -1;
4191 /* Decide wheter to create a special version of NODE for value VAL of parameter
4192 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4193 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4194 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4196 template <typename valtype
>
4198 decide_about_value (struct cgraph_node
*node
, int index
, HOST_WIDE_INT offset
,
4199 ipcp_value
<valtype
> *val
, vec
<tree
> known_csts
,
4200 vec
<ipa_polymorphic_call_context
> known_contexts
)
4202 struct ipa_agg_replacement_value
*aggvals
;
4203 int freq_sum
, caller_count
;
4204 gcov_type count_sum
;
4205 vec
<cgraph_edge
*> callers
;
4209 perhaps_add_new_callers (node
, val
);
4212 else if (val
->local_size_cost
+ overall_size
> max_new_size
)
4214 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4215 fprintf (dump_file
, " Ignoring candidate value because "
4216 "max_new_size would be reached with %li.\n",
4217 val
->local_size_cost
+ overall_size
);
4220 else if (!get_info_about_necessary_edges (val
, node
, &freq_sum
, &count_sum
,
4224 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4226 fprintf (dump_file
, " - considering value ");
4227 print_ipcp_constant_value (dump_file
, val
->value
);
4228 fprintf (dump_file
, " for ");
4229 ipa_dump_param (dump_file
, IPA_NODE_REF (node
), index
);
4231 fprintf (dump_file
, ", offset: " HOST_WIDE_INT_PRINT_DEC
, offset
);
4232 fprintf (dump_file
, " (caller_count: %i)\n", caller_count
);
4235 if (!good_cloning_opportunity_p (node
, val
->local_time_benefit
,
4236 freq_sum
, count_sum
,
4237 val
->local_size_cost
)
4238 && !good_cloning_opportunity_p (node
,
4239 val
->local_time_benefit
4240 + val
->prop_time_benefit
,
4241 freq_sum
, count_sum
,
4242 val
->local_size_cost
4243 + val
->prop_size_cost
))
4247 fprintf (dump_file
, " Creating a specialized node of %s/%i.\n",
4248 node
->name (), node
->order
);
4250 callers
= gather_edges_for_value (val
, node
, caller_count
);
4252 modify_known_vectors_with_val (&known_csts
, &known_contexts
, val
, index
);
4255 known_csts
= known_csts
.copy ();
4256 known_contexts
= copy_useful_known_contexts (known_contexts
);
4258 find_more_scalar_values_for_callers_subset (node
, known_csts
, callers
);
4259 find_more_contexts_for_caller_subset (node
, &known_contexts
, callers
);
4260 aggvals
= find_aggregate_values_for_callers_subset (node
, callers
);
4261 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals
, index
,
4262 offset
, val
->value
));
4263 val
->spec_node
= create_specialized_node (node
, known_csts
, known_contexts
,
4265 overall_size
+= val
->local_size_cost
;
4267 /* TODO: If for some lattice there is only one other known value
4268 left, make a special node for it too. */
4273 /* Decide whether and what specialized clones of NODE should be created. */
4276 decide_whether_version_node (struct cgraph_node
*node
)
4278 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
4279 int i
, count
= ipa_get_param_count (info
);
4280 vec
<tree
> known_csts
;
4281 vec
<ipa_polymorphic_call_context
> known_contexts
;
4282 vec
<ipa_agg_jump_function
> known_aggs
= vNULL
;
4288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4289 fprintf (dump_file
, "\nEvaluating opportunities for %s/%i.\n",
4290 node
->name (), node
->order
);
4292 gather_context_independent_values (info
, &known_csts
, &known_contexts
,
4293 info
->do_clone_for_all_contexts
? &known_aggs
4296 for (i
= 0; i
< count
;i
++)
4298 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4299 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
4300 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
4305 ipcp_value
<tree
> *val
;
4306 for (val
= lat
->values
; val
; val
= val
->next
)
4307 ret
|= decide_about_value (node
, i
, -1, val
, known_csts
,
4311 if (!plats
->aggs_bottom
)
4313 struct ipcp_agg_lattice
*aglat
;
4314 ipcp_value
<tree
> *val
;
4315 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
4316 if (!aglat
->bottom
&& aglat
->values
4317 /* If the following is false, the one value is in
4319 && (plats
->aggs_contain_variable
4320 || !aglat
->is_single_const ()))
4321 for (val
= aglat
->values
; val
; val
= val
->next
)
4322 ret
|= decide_about_value (node
, i
, aglat
->offset
, val
,
4323 known_csts
, known_contexts
);
4327 && known_contexts
[i
].useless_p ())
4329 ipcp_value
<ipa_polymorphic_call_context
> *val
;
4330 for (val
= ctxlat
->values
; val
; val
= val
->next
)
4331 ret
|= decide_about_value (node
, i
, -1, val
, known_csts
,
4335 info
= IPA_NODE_REF (node
);
4338 if (info
->do_clone_for_all_contexts
)
4340 struct cgraph_node
*clone
;
4341 vec
<cgraph_edge
*> callers
;
4344 fprintf (dump_file
, " - Creating a specialized node of %s/%i "
4345 "for all known contexts.\n", node
->name (),
4348 callers
= node
->collect_callers ();
4350 if (!known_contexts_useful_p (known_contexts
))
4352 known_contexts
.release ();
4353 known_contexts
= vNULL
;
4355 clone
= create_specialized_node (node
, known_csts
, known_contexts
,
4356 known_aggs_to_agg_replacement_list (known_aggs
),
4358 info
= IPA_NODE_REF (node
);
4359 info
->do_clone_for_all_contexts
= false;
4360 IPA_NODE_REF (clone
)->is_all_contexts_clone
= true;
4361 for (i
= 0; i
< count
; i
++)
4362 vec_free (known_aggs
[i
].items
);
4363 known_aggs
.release ();
4368 known_csts
.release ();
4369 known_contexts
.release ();
4375 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4378 spread_undeadness (struct cgraph_node
*node
)
4380 struct cgraph_edge
*cs
;
4382 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
4383 if (ipa_edge_within_scc (cs
))
4385 struct cgraph_node
*callee
;
4386 struct ipa_node_params
*info
;
4388 callee
= cs
->callee
->function_symbol (NULL
);
4389 info
= IPA_NODE_REF (callee
);
4391 if (info
->node_dead
)
4393 info
->node_dead
= 0;
4394 spread_undeadness (callee
);
4399 /* Return true if NODE has a caller from outside of its SCC that is not
4400 dead. Worker callback for cgraph_for_node_and_aliases. */
4403 has_undead_caller_from_outside_scc_p (struct cgraph_node
*node
,
4404 void *data ATTRIBUTE_UNUSED
)
4406 struct cgraph_edge
*cs
;
4408 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
4409 if (cs
->caller
->thunk
.thunk_p
4410 && cs
->caller
->call_for_symbol_thunks_and_aliases
4411 (has_undead_caller_from_outside_scc_p
, NULL
, true))
4413 else if (!ipa_edge_within_scc (cs
)
4414 && !IPA_NODE_REF (cs
->caller
)->node_dead
)
4420 /* Identify nodes within the same SCC as NODE which are no longer needed
4421 because of new clones and will be removed as unreachable. */
4424 identify_dead_nodes (struct cgraph_node
*node
)
4426 struct cgraph_node
*v
;
4427 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4428 if (v
->will_be_removed_from_program_if_no_direct_calls_p ()
4429 && !v
->call_for_symbol_thunks_and_aliases
4430 (has_undead_caller_from_outside_scc_p
, NULL
, true))
4431 IPA_NODE_REF (v
)->node_dead
= 1;
4433 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4434 if (!IPA_NODE_REF (v
)->node_dead
)
4435 spread_undeadness (v
);
4437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4439 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4440 if (IPA_NODE_REF (v
)->node_dead
)
4441 fprintf (dump_file
, " Marking node as dead: %s/%i.\n",
4442 v
->name (), v
->order
);
4446 /* The decision stage. Iterate over the topological order of call graph nodes
4447 TOPO and make specialized clones if deemed beneficial. */
4450 ipcp_decision_stage (struct ipa_topo_info
*topo
)
4455 fprintf (dump_file
, "\nIPA decision stage:\n\n");
4457 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
4459 struct cgraph_node
*node
= topo
->order
[i
];
4460 bool change
= false, iterate
= true;
4464 struct cgraph_node
*v
;
4466 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4467 if (v
->has_gimple_body_p ()
4468 && ipcp_versionable_function_p (v
))
4469 iterate
|= decide_whether_version_node (v
);
4474 identify_dead_nodes (node
);
4478 /* Look up all alignment information that we have discovered and copy it over
4479 to the transformation summary. */
4482 ipcp_store_alignment_results (void)
4486 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
4488 ipa_node_params
*info
= IPA_NODE_REF (node
);
4489 bool dumped_sth
= false;
4490 bool found_useful_result
= false;
4492 if (!opt_for_fn (node
->decl
, flag_ipa_cp_alignment
))
4495 fprintf (dump_file
, "Not considering %s for alignment discovery "
4496 "and propagate; -fipa-cp-alignment: disabled.\n",
4501 if (info
->ipcp_orig_node
)
4502 info
= IPA_NODE_REF (info
->ipcp_orig_node
);
4504 unsigned count
= ipa_get_param_count (info
);
4505 for (unsigned i
= 0; i
< count
; i
++)
4507 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4508 if (!plats
->alignment
.bottom_p ()
4509 && !plats
->alignment
.top_p ())
4511 gcc_checking_assert (plats
->alignment
.align
> 0);
4512 found_useful_result
= true;
4516 if (!found_useful_result
)
4519 ipcp_grow_transformations_if_necessary ();
4520 ipcp_transformation_summary
*ts
= ipcp_get_transformation_summary (node
);
4521 vec_safe_reserve_exact (ts
->alignments
, count
);
4523 for (unsigned i
= 0; i
< count
; i
++)
4525 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4528 if (!plats
->alignment
.bottom_p ()
4529 && !plats
->alignment
.top_p ())
4532 al
.align
= plats
->alignment
.align
;
4533 al
.misalign
= plats
->alignment
.misalign
;
4538 ts
->alignments
->quick_push (al
);
4539 if (!dump_file
|| !al
.known
)
4543 fprintf (dump_file
, "Propagated alignment info for function %s/%i:\n",
4544 node
->name (), node
->order
);
4547 fprintf (dump_file
, " param %i: align: %u, misalign: %u\n",
4548 i
, al
.align
, al
.misalign
);
4553 /* The IPCP driver. */
4558 struct cgraph_2edge_hook_list
*edge_duplication_hook_holder
;
4559 struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
4560 struct ipa_topo_info topo
;
4562 ipa_check_create_node_params ();
4563 ipa_check_create_edge_args ();
4564 grow_edge_clone_vectors ();
4565 edge_duplication_hook_holder
=
4566 symtab
->add_edge_duplication_hook (&ipcp_edge_duplication_hook
, NULL
);
4567 edge_removal_hook_holder
=
4568 symtab
->add_edge_removal_hook (&ipcp_edge_removal_hook
, NULL
);
4572 fprintf (dump_file
, "\nIPA structures before propagation:\n");
4573 if (dump_flags
& TDF_DETAILS
)
4574 ipa_print_all_params (dump_file
);
4575 ipa_print_all_jump_functions (dump_file
);
4578 /* Topological sort. */
4579 build_toporder_info (&topo
);
4580 /* Do the interprocedural propagation. */
4581 ipcp_propagate_stage (&topo
);
4582 /* Decide what constant propagation and cloning should be performed. */
4583 ipcp_decision_stage (&topo
);
4584 /* Store results of alignment propagation. */
4585 ipcp_store_alignment_results ();
4587 /* Free all IPCP structures. */
4588 free_toporder_info (&topo
);
4589 next_edge_clone
.release ();
4590 prev_edge_clone
.release ();
4591 symtab
->remove_edge_removal_hook (edge_removal_hook_holder
);
4592 symtab
->remove_edge_duplication_hook (edge_duplication_hook_holder
);
4593 ipa_free_all_structures_after_ipa_cp ();
4595 fprintf (dump_file
, "\nIPA constant propagation end\n");
4599 /* Initialization and computation of IPCP data structures. This is the initial
4600 intraprocedural analysis of functions, which gathers information to be
4601 propagated later on. */
4604 ipcp_generate_summary (void)
4606 struct cgraph_node
*node
;
4609 fprintf (dump_file
, "\nIPA constant propagation start:\n");
4610 ipa_register_cgraph_hooks ();
4612 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
4613 ipa_analyze_node (node
);
4616 /* Write ipcp summary for nodes in SET. */
4619 ipcp_write_summary (void)
4621 ipa_prop_write_jump_functions ();
4624 /* Read ipcp summary. */
4627 ipcp_read_summary (void)
4629 ipa_prop_read_jump_functions ();
4634 const pass_data pass_data_ipa_cp
=
4636 IPA_PASS
, /* type */
4638 OPTGROUP_NONE
, /* optinfo_flags */
4639 TV_IPA_CONSTANT_PROP
, /* tv_id */
4640 0, /* properties_required */
4641 0, /* properties_provided */
4642 0, /* properties_destroyed */
4643 0, /* todo_flags_start */
4644 ( TODO_dump_symtab
| TODO_remove_functions
), /* todo_flags_finish */
4647 class pass_ipa_cp
: public ipa_opt_pass_d
4650 pass_ipa_cp (gcc::context
*ctxt
)
4651 : ipa_opt_pass_d (pass_data_ipa_cp
, ctxt
,
4652 ipcp_generate_summary
, /* generate_summary */
4653 ipcp_write_summary
, /* write_summary */
4654 ipcp_read_summary
, /* read_summary */
4655 ipcp_write_transformation_summaries
, /*
4656 write_optimization_summary */
4657 ipcp_read_transformation_summaries
, /*
4658 read_optimization_summary */
4659 NULL
, /* stmt_fixup */
4660 0, /* function_transform_todo_flags_start */
4661 ipcp_transform_function
, /* function_transform */
4662 NULL
) /* variable_transform */
4665 /* opt_pass methods: */
4666 virtual bool gate (function
*)
4668 /* FIXME: We should remove the optimize check after we ensure we never run
4669 IPA passes when not optimizing. */
4670 return (flag_ipa_cp
&& optimize
) || in_lto_p
;
4673 virtual unsigned int execute (function
*) { return ipcp_driver (); }
4675 }; // class pass_ipa_cp
4680 make_pass_ipa_cp (gcc::context
*ctxt
)
4682 return new pass_ipa_cp (ctxt
);
4685 /* Reset all state within ipa-cp.c so that we can rerun the compiler
4686 within the same process. For use by toplev::finalize. */
4689 ipa_cp_c_finalize (void)