1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2018 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"
108 #include "gimple-expr.h"
110 #include "alloc-pool.h"
111 #include "tree-pass.h"
113 #include "diagnostic.h"
114 #include "fold-const.h"
115 #include "gimple-fold.h"
116 #include "symbol-summary.h"
117 #include "tree-vrp.h"
118 #include "ipa-prop.h"
119 #include "tree-pretty-print.h"
120 #include "tree-inline.h"
122 #include "ipa-fnsummary.h"
123 #include "ipa-utils.h"
124 #include "tree-ssa-ccp.h"
125 #include "stringpool.h"
128 template <typename valtype
> class ipcp_value
;
130 /* Describes a particular source for an IPA-CP value. */
132 template <typename valtype
>
133 class ipcp_value_source
136 /* Aggregate offset of the source, negative if the source is scalar value of
137 the argument itself. */
138 HOST_WIDE_INT offset
;
139 /* The incoming edge that brought the value. */
141 /* If the jump function that resulted into his value was a pass-through or an
142 ancestor, this is the ipcp_value of the caller from which the described
143 value has been derived. Otherwise it is NULL. */
144 ipcp_value
<valtype
> *val
;
145 /* Next pointer in a linked list of sources of a value. */
146 ipcp_value_source
*next
;
147 /* If the jump function that resulted into his value was a pass-through or an
148 ancestor, this is the index of the parameter of the caller the jump
149 function references. */
153 /* Common ancestor for all ipcp_value instantiations. */
155 class ipcp_value_base
158 /* Time benefit and size cost that specializing the function for this value
159 would bring about in this function alone. */
160 int local_time_benefit
, local_size_cost
;
161 /* Time benefit and size cost that specializing the function for this value
162 can bring about in it's callees (transitively). */
163 int prop_time_benefit
, prop_size_cost
;
166 : local_time_benefit (0), local_size_cost (0),
167 prop_time_benefit (0), prop_size_cost (0) {}
170 /* Describes one particular value stored in struct ipcp_lattice. */
172 template <typename valtype
>
173 class ipcp_value
: public ipcp_value_base
176 /* The actual value for the given parameter. */
178 /* The list of sources from which this value originates. */
179 ipcp_value_source
<valtype
> *sources
;
180 /* Next pointers in a linked list of all values in a lattice. */
182 /* Next pointers in a linked list of values in a strongly connected component
184 ipcp_value
*scc_next
;
185 /* Next pointers in a linked list of SCCs of values sorted topologically
186 according their sources. */
187 ipcp_value
*topo_next
;
188 /* A specialized node created for this value, NULL if none has been (so far)
190 cgraph_node
*spec_node
;
191 /* Depth first search number and low link for topological sorting of
194 /* True if this valye is currently on the topo-sort stack. */
198 : sources (0), next (0), scc_next (0), topo_next (0),
199 spec_node (0), dfs (0), low_link (0), on_stack (false) {}
201 void add_source (cgraph_edge
*cs
, ipcp_value
*src_val
, int src_idx
,
202 HOST_WIDE_INT offset
);
205 /* Lattice describing potential values of a formal parameter of a function, or
206 a part of an aggregate. TOP is represented by a lattice with zero values
207 and with contains_variable and bottom flags cleared. BOTTOM is represented
208 by a lattice with the bottom flag set. In that case, values and
209 contains_variable flag should be disregarded. */
211 template <typename valtype
>
215 /* The list of known values and types in this lattice. Note that values are
216 not deallocated if a lattice is set to bottom because there may be value
217 sources referencing them. */
218 ipcp_value
<valtype
> *values
;
219 /* Number of known values and types in this lattice. */
221 /* The lattice contains a variable component (in addition to values). */
222 bool contains_variable
;
223 /* The value of the lattice is bottom (i.e. variable and unusable for any
227 inline bool is_single_const ();
228 inline bool set_to_bottom ();
229 inline bool set_contains_variable ();
230 bool add_value (valtype newval
, cgraph_edge
*cs
,
231 ipcp_value
<valtype
> *src_val
= NULL
,
232 int src_idx
= 0, HOST_WIDE_INT offset
= -1);
233 void print (FILE * f
, bool dump_sources
, bool dump_benefits
);
236 /* Lattice of tree values with an offset to describe a part of an
239 class ipcp_agg_lattice
: public ipcp_lattice
<tree
>
242 /* Offset that is being described by this lattice. */
243 HOST_WIDE_INT offset
;
244 /* Size so that we don't have to re-compute it every time we traverse the
245 list. Must correspond to TYPE_SIZE of all lat values. */
247 /* Next element of the linked list. */
248 struct ipcp_agg_lattice
*next
;
251 /* Lattice of known bits, only capable of holding one value.
252 Bitwise constant propagation propagates which bits of a
268 In the above case, the param 'x' will always have all
269 the bits (except the bits in lsb) set to 0.
270 Hence the mask of 'x' would be 0xff. The mask
271 reflects that the bits in lsb are unknown.
272 The actual propagated value is given by m_value & ~m_mask. */
274 class ipcp_bits_lattice
277 bool bottom_p () { return m_lattice_val
== IPA_BITS_VARYING
; }
278 bool top_p () { return m_lattice_val
== IPA_BITS_UNDEFINED
; }
279 bool constant_p () { return m_lattice_val
== IPA_BITS_CONSTANT
; }
280 bool set_to_bottom ();
281 bool set_to_constant (widest_int
, widest_int
);
283 widest_int
get_value () { return m_value
; }
284 widest_int
get_mask () { return m_mask
; }
286 bool meet_with (ipcp_bits_lattice
& other
, unsigned, signop
,
287 enum tree_code
, tree
);
289 bool meet_with (widest_int
, widest_int
, unsigned);
294 enum { IPA_BITS_UNDEFINED
, IPA_BITS_CONSTANT
, IPA_BITS_VARYING
} m_lattice_val
;
296 /* Similar to ccp_lattice_t, mask represents which bits of value are constant.
297 If a bit in mask is set to 0, then the corresponding bit in
298 value is known to be constant. */
299 widest_int m_value
, m_mask
;
301 bool meet_with_1 (widest_int
, widest_int
, unsigned);
302 void get_value_and_mask (tree
, widest_int
*, widest_int
*);
305 /* Lattice of value ranges. */
307 class ipcp_vr_lattice
312 inline bool bottom_p () const;
313 inline bool top_p () const;
314 inline bool set_to_bottom ();
315 bool meet_with (const value_range
*p_vr
);
316 bool meet_with (const ipcp_vr_lattice
&other
);
317 void init () { m_vr
.type
= VR_UNDEFINED
; }
318 void print (FILE * f
);
321 bool meet_with_1 (const value_range
*other_vr
);
324 /* Structure containing lattices for a parameter itself and for pieces of
325 aggregates that are passed in the parameter or by a reference in a parameter
326 plus some other useful flags. */
328 class ipcp_param_lattices
331 /* Lattice describing the value of the parameter itself. */
332 ipcp_lattice
<tree
> itself
;
333 /* Lattice describing the polymorphic contexts of a parameter. */
334 ipcp_lattice
<ipa_polymorphic_call_context
> ctxlat
;
335 /* Lattices describing aggregate parts. */
336 ipcp_agg_lattice
*aggs
;
337 /* Lattice describing known bits. */
338 ipcp_bits_lattice bits_lattice
;
339 /* Lattice describing value range. */
340 ipcp_vr_lattice m_value_range
;
341 /* Number of aggregate lattices */
343 /* True if aggregate data were passed by reference (as opposed to by
346 /* All aggregate lattices contain a variable component (in addition to
348 bool aggs_contain_variable
;
349 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
350 for any propagation). */
353 /* There is a virtual call based on this parameter. */
357 /* Allocation pools for values and their sources in ipa-cp. */
359 object_allocator
<ipcp_value
<tree
> > ipcp_cst_values_pool
360 ("IPA-CP constant values");
362 object_allocator
<ipcp_value
<ipa_polymorphic_call_context
> >
363 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
365 object_allocator
<ipcp_value_source
<tree
> > ipcp_sources_pool
366 ("IPA-CP value sources");
368 object_allocator
<ipcp_agg_lattice
> ipcp_agg_lattice_pool
369 ("IPA_CP aggregate lattices");
371 /* Maximal count found in program. */
373 static profile_count max_count
;
375 /* Original overall size of the program. */
377 static long overall_size
, max_new_size
;
379 /* Return the param lattices structure corresponding to the Ith formal
380 parameter of the function described by INFO. */
381 static inline struct ipcp_param_lattices
*
382 ipa_get_parm_lattices (struct ipa_node_params
*info
, int i
)
384 gcc_assert (i
>= 0 && i
< ipa_get_param_count (info
));
385 gcc_checking_assert (!info
->ipcp_orig_node
);
386 gcc_checking_assert (info
->lattices
);
387 return &(info
->lattices
[i
]);
390 /* Return the lattice corresponding to the scalar value of the Ith formal
391 parameter of the function described by INFO. */
392 static inline ipcp_lattice
<tree
> *
393 ipa_get_scalar_lat (struct ipa_node_params
*info
, int i
)
395 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
396 return &plats
->itself
;
399 /* Return the lattice corresponding to the scalar value of the Ith formal
400 parameter of the function described by INFO. */
401 static inline ipcp_lattice
<ipa_polymorphic_call_context
> *
402 ipa_get_poly_ctx_lat (struct ipa_node_params
*info
, int i
)
404 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
405 return &plats
->ctxlat
;
408 /* Return the lattice corresponding to the value range of the Ith formal
409 parameter of the function described by INFO. */
411 static inline ipcp_vr_lattice
*
412 ipa_get_vr_lat (struct ipa_node_params
*info
, int i
)
414 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
415 return &plats
->m_value_range
;
418 /* Return whether LAT is a lattice with a single constant and without an
421 template <typename valtype
>
423 ipcp_lattice
<valtype
>::is_single_const ()
425 if (bottom
|| contains_variable
|| values_count
!= 1)
431 /* Print V which is extracted from a value in a lattice to F. */
434 print_ipcp_constant_value (FILE * f
, tree v
)
436 if (TREE_CODE (v
) == ADDR_EXPR
437 && TREE_CODE (TREE_OPERAND (v
, 0)) == CONST_DECL
)
440 print_generic_expr (f
, DECL_INITIAL (TREE_OPERAND (v
, 0)));
443 print_generic_expr (f
, v
);
446 /* Print V which is extracted from a value in a lattice to F. */
449 print_ipcp_constant_value (FILE * f
, ipa_polymorphic_call_context v
)
454 /* Print a lattice LAT to F. */
456 template <typename valtype
>
458 ipcp_lattice
<valtype
>::print (FILE * f
, bool dump_sources
, bool dump_benefits
)
460 ipcp_value
<valtype
> *val
;
465 fprintf (f
, "BOTTOM\n");
469 if (!values_count
&& !contains_variable
)
471 fprintf (f
, "TOP\n");
475 if (contains_variable
)
477 fprintf (f
, "VARIABLE");
483 for (val
= values
; val
; val
= val
->next
)
485 if (dump_benefits
&& prev
)
487 else if (!dump_benefits
&& prev
)
492 print_ipcp_constant_value (f
, val
->value
);
496 ipcp_value_source
<valtype
> *s
;
498 fprintf (f
, " [from:");
499 for (s
= val
->sources
; s
; s
= s
->next
)
500 fprintf (f
, " %i(%f)", s
->cs
->caller
->order
,
501 s
->cs
->sreal_frequency ().to_double ());
506 fprintf (f
, " [loc_time: %i, loc_size: %i, "
507 "prop_time: %i, prop_size: %i]\n",
508 val
->local_time_benefit
, val
->local_size_cost
,
509 val
->prop_time_benefit
, val
->prop_size_cost
);
516 ipcp_bits_lattice::print (FILE *f
)
519 fprintf (f
, " Bits unknown (TOP)\n");
520 else if (bottom_p ())
521 fprintf (f
, " Bits unusable (BOTTOM)\n");
524 fprintf (f
, " Bits: value = "); print_hex (get_value (), f
);
525 fprintf (f
, ", mask = "); print_hex (get_mask (), f
);
530 /* Print value range lattice to F. */
533 ipcp_vr_lattice::print (FILE * f
)
535 dump_value_range (f
, &m_vr
);
538 /* Print all ipcp_lattices of all functions to F. */
541 print_all_lattices (FILE * f
, bool dump_sources
, bool dump_benefits
)
543 struct cgraph_node
*node
;
546 fprintf (f
, "\nLattices:\n");
547 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
549 struct ipa_node_params
*info
;
551 info
= IPA_NODE_REF (node
);
552 fprintf (f
, " Node: %s:\n", node
->dump_name ());
553 count
= ipa_get_param_count (info
);
554 for (i
= 0; i
< count
; i
++)
556 struct ipcp_agg_lattice
*aglat
;
557 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
558 fprintf (f
, " param [%d]: ", i
);
559 plats
->itself
.print (f
, dump_sources
, dump_benefits
);
560 fprintf (f
, " ctxs: ");
561 plats
->ctxlat
.print (f
, dump_sources
, dump_benefits
);
562 plats
->bits_lattice
.print (f
);
564 plats
->m_value_range
.print (f
);
566 if (plats
->virt_call
)
567 fprintf (f
, " virt_call flag set\n");
569 if (plats
->aggs_bottom
)
571 fprintf (f
, " AGGS BOTTOM\n");
574 if (plats
->aggs_contain_variable
)
575 fprintf (f
, " AGGS VARIABLE\n");
576 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
578 fprintf (f
, " %soffset " HOST_WIDE_INT_PRINT_DEC
": ",
579 plats
->aggs_by_ref
? "ref " : "", aglat
->offset
);
580 aglat
->print (f
, dump_sources
, dump_benefits
);
586 /* Determine whether it is at all technically possible to create clones of NODE
587 and store this information in the ipa_node_params structure associated
591 determine_versionability (struct cgraph_node
*node
,
592 struct ipa_node_params
*info
)
594 const char *reason
= NULL
;
596 /* There are a number of generic reasons functions cannot be versioned. We
597 also cannot remove parameters if there are type attributes such as fnspec
599 if (node
->alias
|| node
->thunk
.thunk_p
)
600 reason
= "alias or thunk";
601 else if (!node
->local
.versionable
)
602 reason
= "not a tree_versionable_function";
603 else if (node
->get_availability () <= AVAIL_INTERPOSABLE
)
604 reason
= "insufficient body availability";
605 else if (!opt_for_fn (node
->decl
, optimize
)
606 || !opt_for_fn (node
->decl
, flag_ipa_cp
))
607 reason
= "non-optimized function";
608 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node
->decl
)))
610 /* Ideally we should clone the SIMD clones themselves and create
611 vector copies of them, so IPA-cp and SIMD clones can happily
612 coexist, but that may not be worth the effort. */
613 reason
= "function has SIMD clones";
615 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node
->decl
)))
617 /* Ideally we should clone the target clones themselves and create
618 copies of them, so IPA-cp and target clones can happily
619 coexist, but that may not be worth the effort. */
620 reason
= "function target_clones attribute";
622 /* Don't clone decls local to a comdat group; it breaks and for C++
623 decloned constructors, inlining is always better anyway. */
624 else if (node
->comdat_local_p ())
625 reason
= "comdat-local function";
626 else if (node
->calls_comdat_local
)
628 /* TODO: call is versionable if we make sure that all
629 callers are inside of a comdat group. */
630 reason
= "calls comdat-local function";
633 /* Functions calling BUILT_IN_VA_ARG_PACK and BUILT_IN_VA_ARG_PACK_LEN
634 work only when inlined. Cloning them may still lead to better code
635 because ipa-cp will not give up on cloning further. If the function is
636 external this however leads to wrong code because we may end up producing
637 offline copy of the function. */
638 if (DECL_EXTERNAL (node
->decl
))
639 for (cgraph_edge
*edge
= node
->callees
; !reason
&& edge
;
640 edge
= edge
->next_callee
)
641 if (DECL_BUILT_IN (edge
->callee
->decl
)
642 && DECL_BUILT_IN_CLASS (edge
->callee
->decl
) == BUILT_IN_NORMAL
)
644 if (DECL_FUNCTION_CODE (edge
->callee
->decl
) == BUILT_IN_VA_ARG_PACK
)
645 reason
= "external function which calls va_arg_pack";
646 if (DECL_FUNCTION_CODE (edge
->callee
->decl
)
647 == BUILT_IN_VA_ARG_PACK_LEN
)
648 reason
= "external function which calls va_arg_pack_len";
651 if (reason
&& dump_file
&& !node
->alias
&& !node
->thunk
.thunk_p
)
652 fprintf (dump_file
, "Function %s is not versionable, reason: %s.\n",
653 node
->dump_name (), reason
);
655 info
->versionable
= (reason
== NULL
);
658 /* Return true if it is at all technically possible to create clones of a
662 ipcp_versionable_function_p (struct cgraph_node
*node
)
664 return IPA_NODE_REF (node
)->versionable
;
667 /* Structure holding accumulated information about callers of a node. */
669 struct caller_statistics
671 profile_count count_sum
;
672 int n_calls
, n_hot_calls
, freq_sum
;
675 /* Initialize fields of STAT to zeroes. */
678 init_caller_stats (struct caller_statistics
*stats
)
680 stats
->count_sum
= profile_count::zero ();
682 stats
->n_hot_calls
= 0;
686 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
687 non-thunk incoming edges to NODE. */
690 gather_caller_stats (struct cgraph_node
*node
, void *data
)
692 struct caller_statistics
*stats
= (struct caller_statistics
*) data
;
693 struct cgraph_edge
*cs
;
695 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
696 if (!cs
->caller
->thunk
.thunk_p
)
698 if (cs
->count
.ipa ().initialized_p ())
699 stats
->count_sum
+= cs
->count
.ipa ();
700 stats
->freq_sum
+= cs
->frequency ();
702 if (cs
->maybe_hot_p ())
703 stats
->n_hot_calls
++;
709 /* Return true if this NODE is viable candidate for cloning. */
712 ipcp_cloning_candidate_p (struct cgraph_node
*node
)
714 struct caller_statistics stats
;
716 gcc_checking_assert (node
->has_gimple_body_p ());
718 if (!opt_for_fn (node
->decl
, flag_ipa_cp_clone
))
721 fprintf (dump_file
, "Not considering %s for cloning; "
722 "-fipa-cp-clone disabled.\n",
727 if (node
->optimize_for_size_p ())
730 fprintf (dump_file
, "Not considering %s for cloning; "
731 "optimizing it for size.\n",
736 init_caller_stats (&stats
);
737 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
, false);
739 if (ipa_fn_summaries
->get (node
)->self_size
< stats
.n_calls
)
742 fprintf (dump_file
, "Considering %s for cloning; code might shrink.\n",
747 /* When profile is available and function is hot, propagate into it even if
748 calls seems cold; constant propagation can improve function's speed
750 if (max_count
> profile_count::zero ())
752 if (stats
.count_sum
> node
->count
.ipa ().apply_scale (90, 100))
755 fprintf (dump_file
, "Considering %s for cloning; "
756 "usually called directly.\n",
761 if (!stats
.n_hot_calls
)
764 fprintf (dump_file
, "Not considering %s for cloning; no hot calls.\n",
769 fprintf (dump_file
, "Considering %s for cloning.\n",
774 template <typename valtype
>
775 class value_topo_info
778 /* Head of the linked list of topologically sorted values. */
779 ipcp_value
<valtype
> *values_topo
;
780 /* Stack for creating SCCs, represented by a linked list too. */
781 ipcp_value
<valtype
> *stack
;
782 /* Counter driving the algorithm in add_val_to_toposort. */
785 value_topo_info () : values_topo (NULL
), stack (NULL
), dfs_counter (0)
787 void add_val (ipcp_value
<valtype
> *cur_val
);
788 void propagate_effects ();
791 /* Arrays representing a topological ordering of call graph nodes and a stack
792 of nodes used during constant propagation and also data required to perform
793 topological sort of values and propagation of benefits in the determined
799 /* Array with obtained topological order of cgraph nodes. */
800 struct cgraph_node
**order
;
801 /* Stack of cgraph nodes used during propagation within SCC until all values
802 in the SCC stabilize. */
803 struct cgraph_node
**stack
;
804 int nnodes
, stack_top
;
806 value_topo_info
<tree
> constants
;
807 value_topo_info
<ipa_polymorphic_call_context
> contexts
;
809 ipa_topo_info () : order(NULL
), stack(NULL
), nnodes(0), stack_top(0),
814 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
817 build_toporder_info (struct ipa_topo_info
*topo
)
819 topo
->order
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
820 topo
->stack
= XCNEWVEC (struct cgraph_node
*, symtab
->cgraph_count
);
822 gcc_checking_assert (topo
->stack_top
== 0);
823 topo
->nnodes
= ipa_reduced_postorder (topo
->order
, true, true, NULL
);
826 /* Free information about strongly connected components and the arrays in
830 free_toporder_info (struct ipa_topo_info
*topo
)
832 ipa_free_postorder_info ();
837 /* Add NODE to the stack in TOPO, unless it is already there. */
840 push_node_to_stack (struct ipa_topo_info
*topo
, struct cgraph_node
*node
)
842 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
843 if (info
->node_enqueued
)
845 info
->node_enqueued
= 1;
846 topo
->stack
[topo
->stack_top
++] = node
;
849 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
852 static struct cgraph_node
*
853 pop_node_from_stack (struct ipa_topo_info
*topo
)
857 struct cgraph_node
*node
;
859 node
= topo
->stack
[topo
->stack_top
];
860 IPA_NODE_REF (node
)->node_enqueued
= 0;
867 /* Set lattice LAT to bottom and return true if it previously was not set as
870 template <typename valtype
>
872 ipcp_lattice
<valtype
>::set_to_bottom ()
879 /* Mark lattice as containing an unknown value and return true if it previously
880 was not marked as such. */
882 template <typename valtype
>
884 ipcp_lattice
<valtype
>::set_contains_variable ()
886 bool ret
= !contains_variable
;
887 contains_variable
= true;
891 /* Set all aggegate lattices in PLATS to bottom and return true if they were
892 not previously set as such. */
895 set_agg_lats_to_bottom (struct ipcp_param_lattices
*plats
)
897 bool ret
= !plats
->aggs_bottom
;
898 plats
->aggs_bottom
= true;
902 /* Mark all aggegate lattices in PLATS as containing an unknown value and
903 return true if they were not previously marked as such. */
906 set_agg_lats_contain_variable (struct ipcp_param_lattices
*plats
)
908 bool ret
= !plats
->aggs_contain_variable
;
909 plats
->aggs_contain_variable
= true;
914 ipcp_vr_lattice::meet_with (const ipcp_vr_lattice
&other
)
916 return meet_with_1 (&other
.m_vr
);
919 /* Meet the current value of the lattice with value ranfge described by VR
923 ipcp_vr_lattice::meet_with (const value_range
*p_vr
)
925 return meet_with_1 (p_vr
);
928 /* Meet the current value of the lattice with value ranfge described by
932 ipcp_vr_lattice::meet_with_1 (const value_range
*other_vr
)
934 tree min
= m_vr
.min
, max
= m_vr
.max
;
935 value_range_type type
= m_vr
.type
;
940 if (other_vr
->type
== VR_VARYING
)
941 return set_to_bottom ();
943 vrp_meet (&m_vr
, other_vr
);
944 if (type
!= m_vr
.type
952 /* Return true if value range information in the lattice is yet unknown. */
955 ipcp_vr_lattice::top_p () const
957 return m_vr
.type
== VR_UNDEFINED
;
960 /* Return true if value range information in the lattice is known to be
964 ipcp_vr_lattice::bottom_p () const
966 return m_vr
.type
== VR_VARYING
;
969 /* Set value range information in the lattice to bottom. Return true if it
970 previously was in a different state. */
973 ipcp_vr_lattice::set_to_bottom ()
975 if (m_vr
.type
== VR_VARYING
)
977 m_vr
.type
= VR_VARYING
;
981 /* Set lattice value to bottom, if it already isn't the case. */
984 ipcp_bits_lattice::set_to_bottom ()
988 m_lattice_val
= IPA_BITS_VARYING
;
994 /* Set to constant if it isn't already. Only meant to be called
995 when switching state from TOP. */
998 ipcp_bits_lattice::set_to_constant (widest_int value
, widest_int mask
)
1000 gcc_assert (top_p ());
1001 m_lattice_val
= IPA_BITS_CONSTANT
;
1007 /* Convert operand to value, mask form. */
1010 ipcp_bits_lattice::get_value_and_mask (tree operand
, widest_int
*valuep
, widest_int
*maskp
)
1012 wide_int
get_nonzero_bits (const_tree
);
1014 if (TREE_CODE (operand
) == INTEGER_CST
)
1016 *valuep
= wi::to_widest (operand
);
1026 /* Meet operation, similar to ccp_lattice_meet, we xor values
1027 if this->value, value have different values at same bit positions, we want
1028 to drop that bit to varying. Return true if mask is changed.
1029 This function assumes that the lattice value is in CONSTANT state */
1032 ipcp_bits_lattice::meet_with_1 (widest_int value
, widest_int mask
,
1035 gcc_assert (constant_p ());
1037 widest_int old_mask
= m_mask
;
1038 m_mask
= (m_mask
| mask
) | (m_value
^ value
);
1040 if (wi::sext (m_mask
, precision
) == -1)
1041 return set_to_bottom ();
1043 return m_mask
!= old_mask
;
1046 /* Meet the bits lattice with operand
1047 described by <value, mask, sgn, precision. */
1050 ipcp_bits_lattice::meet_with (widest_int value
, widest_int mask
,
1058 if (wi::sext (mask
, precision
) == -1)
1059 return set_to_bottom ();
1060 return set_to_constant (value
, mask
);
1063 return meet_with_1 (value
, mask
, precision
);
1066 /* Meet bits lattice with the result of bit_value_binop (other, operand)
1067 if code is binary operation or bit_value_unop (other) if code is unary op.
1068 In the case when code is nop_expr, no adjustment is required. */
1071 ipcp_bits_lattice::meet_with (ipcp_bits_lattice
& other
, unsigned precision
,
1072 signop sgn
, enum tree_code code
, tree operand
)
1074 if (other
.bottom_p ())
1075 return set_to_bottom ();
1077 if (bottom_p () || other
.top_p ())
1080 widest_int adjusted_value
, adjusted_mask
;
1082 if (TREE_CODE_CLASS (code
) == tcc_binary
)
1084 tree type
= TREE_TYPE (operand
);
1085 gcc_assert (INTEGRAL_TYPE_P (type
));
1086 widest_int o_value
, o_mask
;
1087 get_value_and_mask (operand
, &o_value
, &o_mask
);
1089 bit_value_binop (code
, sgn
, precision
, &adjusted_value
, &adjusted_mask
,
1090 sgn
, precision
, other
.get_value (), other
.get_mask (),
1091 TYPE_SIGN (type
), TYPE_PRECISION (type
), o_value
, o_mask
);
1093 if (wi::sext (adjusted_mask
, precision
) == -1)
1094 return set_to_bottom ();
1097 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
1099 bit_value_unop (code
, sgn
, precision
, &adjusted_value
,
1100 &adjusted_mask
, sgn
, precision
, other
.get_value (),
1103 if (wi::sext (adjusted_mask
, precision
) == -1)
1104 return set_to_bottom ();
1108 return set_to_bottom ();
1112 if (wi::sext (adjusted_mask
, precision
) == -1)
1113 return set_to_bottom ();
1114 return set_to_constant (adjusted_value
, adjusted_mask
);
1117 return meet_with_1 (adjusted_value
, adjusted_mask
, precision
);
1120 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
1121 return true is any of them has not been marked as such so far. */
1124 set_all_contains_variable (struct ipcp_param_lattices
*plats
)
1127 ret
= plats
->itself
.set_contains_variable ();
1128 ret
|= plats
->ctxlat
.set_contains_variable ();
1129 ret
|= set_agg_lats_contain_variable (plats
);
1130 ret
|= plats
->bits_lattice
.set_to_bottom ();
1131 ret
|= plats
->m_value_range
.set_to_bottom ();
1135 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
1136 points to by the number of callers to NODE. */
1139 count_callers (cgraph_node
*node
, void *data
)
1141 int *caller_count
= (int *) data
;
1143 for (cgraph_edge
*cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
1144 /* Local thunks can be handled transparently, but if the thunk can not
1145 be optimized out, count it as a real use. */
1146 if (!cs
->caller
->thunk
.thunk_p
|| !cs
->caller
->local
.local
)
1151 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
1152 the one caller of some other node. Set the caller's corresponding flag. */
1155 set_single_call_flag (cgraph_node
*node
, void *)
1157 cgraph_edge
*cs
= node
->callers
;
1158 /* Local thunks can be handled transparently, skip them. */
1159 while (cs
&& cs
->caller
->thunk
.thunk_p
&& cs
->caller
->local
.local
)
1160 cs
= cs
->next_caller
;
1163 IPA_NODE_REF (cs
->caller
)->node_calling_single_call
= true;
1169 /* Initialize ipcp_lattices. */
1172 initialize_node_lattices (struct cgraph_node
*node
)
1174 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
1175 struct cgraph_edge
*ie
;
1176 bool disable
= false, variable
= false;
1179 gcc_checking_assert (node
->has_gimple_body_p ());
1180 if (cgraph_local_p (node
))
1182 int caller_count
= 0;
1183 node
->call_for_symbol_thunks_and_aliases (count_callers
, &caller_count
,
1185 gcc_checking_assert (caller_count
> 0);
1186 if (caller_count
== 1)
1187 node
->call_for_symbol_thunks_and_aliases (set_single_call_flag
,
1192 /* When cloning is allowed, we can assume that externally visible
1193 functions are not called. We will compensate this by cloning
1195 if (ipcp_versionable_function_p (node
)
1196 && ipcp_cloning_candidate_p (node
))
1202 for (i
= 0; i
< ipa_get_param_count (info
); i
++)
1204 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
1205 plats
->m_value_range
.init ();
1208 if (disable
|| variable
)
1210 for (i
= 0; i
< ipa_get_param_count (info
); i
++)
1212 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
1215 plats
->itself
.set_to_bottom ();
1216 plats
->ctxlat
.set_to_bottom ();
1217 set_agg_lats_to_bottom (plats
);
1218 plats
->bits_lattice
.set_to_bottom ();
1219 plats
->m_value_range
.set_to_bottom ();
1222 set_all_contains_variable (plats
);
1224 if (dump_file
&& (dump_flags
& TDF_DETAILS
)
1225 && !node
->alias
&& !node
->thunk
.thunk_p
)
1226 fprintf (dump_file
, "Marking all lattices of %s as %s\n",
1227 node
->dump_name (), disable
? "BOTTOM" : "VARIABLE");
1230 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
1231 if (ie
->indirect_info
->polymorphic
1232 && ie
->indirect_info
->param_index
>= 0)
1234 gcc_checking_assert (ie
->indirect_info
->param_index
>= 0);
1235 ipa_get_parm_lattices (info
,
1236 ie
->indirect_info
->param_index
)->virt_call
= 1;
1240 /* Return the result of a (possibly arithmetic) pass through jump function
1241 JFUNC on the constant value INPUT. RES_TYPE is the type of the parameter
1242 to which the result is passed. Return NULL_TREE if that cannot be
1243 determined or be considered an interprocedural invariant. */
1246 ipa_get_jf_pass_through_result (struct ipa_jump_func
*jfunc
, tree input
,
1251 if (ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
1253 if (!is_gimple_ip_invariant (input
))
1256 tree_code opcode
= ipa_get_jf_pass_through_operation (jfunc
);
1259 if (TREE_CODE_CLASS (opcode
) == tcc_comparison
)
1260 res_type
= boolean_type_node
;
1261 else if (expr_type_first_operand_type_p (opcode
))
1262 res_type
= TREE_TYPE (input
);
1267 if (TREE_CODE_CLASS (opcode
) == tcc_unary
)
1268 res
= fold_unary (opcode
, res_type
, input
);
1270 res
= fold_binary (opcode
, res_type
, input
,
1271 ipa_get_jf_pass_through_operand (jfunc
));
1273 if (res
&& !is_gimple_ip_invariant (res
))
1279 /* Return the result of an ancestor jump function JFUNC on the constant value
1280 INPUT. Return NULL_TREE if that cannot be determined. */
1283 ipa_get_jf_ancestor_result (struct ipa_jump_func
*jfunc
, tree input
)
1285 gcc_checking_assert (TREE_CODE (input
) != TREE_BINFO
);
1286 if (TREE_CODE (input
) == ADDR_EXPR
)
1288 tree t
= TREE_OPERAND (input
, 0);
1289 t
= build_ref_for_offset (EXPR_LOCATION (t
), t
,
1290 ipa_get_jf_ancestor_offset (jfunc
), false,
1291 ptr_type_node
, NULL
, false);
1292 return build_fold_addr_expr (t
);
1298 /* Determine whether JFUNC evaluates to a single known constant value and if
1299 so, return it. Otherwise return NULL. INFO describes the caller node or
1300 the one it is inlined to, so that pass-through jump functions can be
1301 evaluated. PARM_TYPE is the type of the parameter to which the result is
1305 ipa_value_from_jfunc (struct ipa_node_params
*info
, struct ipa_jump_func
*jfunc
,
1308 if (jfunc
->type
== IPA_JF_CONST
)
1309 return ipa_get_jf_constant (jfunc
);
1310 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
1311 || jfunc
->type
== IPA_JF_ANCESTOR
)
1316 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1317 idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1319 idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1321 if (info
->ipcp_orig_node
)
1322 input
= info
->known_csts
[idx
];
1325 ipcp_lattice
<tree
> *lat
;
1328 || idx
>= ipa_get_param_count (info
))
1330 lat
= ipa_get_scalar_lat (info
, idx
);
1331 if (!lat
->is_single_const ())
1333 input
= lat
->values
->value
;
1339 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1340 return ipa_get_jf_pass_through_result (jfunc
, input
, parm_type
);
1342 return ipa_get_jf_ancestor_result (jfunc
, input
);
1348 /* Determie whether JFUNC evaluates to single known polymorphic context, given
1349 that INFO describes the caller node or the one it is inlined to, CS is the
1350 call graph edge corresponding to JFUNC and CSIDX index of the described
1353 ipa_polymorphic_call_context
1354 ipa_context_from_jfunc (ipa_node_params
*info
, cgraph_edge
*cs
, int csidx
,
1355 ipa_jump_func
*jfunc
)
1357 ipa_edge_args
*args
= IPA_EDGE_REF (cs
);
1358 ipa_polymorphic_call_context ctx
;
1359 ipa_polymorphic_call_context
*edge_ctx
1360 = cs
? ipa_get_ith_polymorhic_call_context (args
, csidx
) : NULL
;
1362 if (edge_ctx
&& !edge_ctx
->useless_p ())
1365 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1366 || jfunc
->type
== IPA_JF_ANCESTOR
)
1368 ipa_polymorphic_call_context srcctx
;
1370 bool type_preserved
= true;
1371 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1373 if (ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1375 type_preserved
= ipa_get_jf_pass_through_type_preserved (jfunc
);
1376 srcidx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1380 type_preserved
= ipa_get_jf_ancestor_type_preserved (jfunc
);
1381 srcidx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1383 if (info
->ipcp_orig_node
)
1385 if (info
->known_contexts
.exists ())
1386 srcctx
= info
->known_contexts
[srcidx
];
1391 || srcidx
>= ipa_get_param_count (info
))
1393 ipcp_lattice
<ipa_polymorphic_call_context
> *lat
;
1394 lat
= ipa_get_poly_ctx_lat (info
, srcidx
);
1395 if (!lat
->is_single_const ())
1397 srcctx
= lat
->values
->value
;
1399 if (srcctx
.useless_p ())
1401 if (jfunc
->type
== IPA_JF_ANCESTOR
)
1402 srcctx
.offset_by (ipa_get_jf_ancestor_offset (jfunc
));
1403 if (!type_preserved
)
1404 srcctx
.possible_dynamic_type_change (cs
->in_polymorphic_cdtor
);
1405 srcctx
.combine_with (ctx
);
1412 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1413 bottom, not containing a variable component and without any known value at
1417 ipcp_verify_propagated_values (void)
1419 struct cgraph_node
*node
;
1421 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
1423 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
1424 int i
, count
= ipa_get_param_count (info
);
1426 for (i
= 0; i
< count
; i
++)
1428 ipcp_lattice
<tree
> *lat
= ipa_get_scalar_lat (info
, i
);
1431 && !lat
->contains_variable
1432 && lat
->values_count
== 0)
1436 symtab
->dump (dump_file
);
1437 fprintf (dump_file
, "\nIPA lattices after constant "
1438 "propagation, before gcc_unreachable:\n");
1439 print_all_lattices (dump_file
, true, false);
1448 /* Return true iff X and Y should be considered equal values by IPA-CP. */
1451 values_equal_for_ipcp_p (tree x
, tree y
)
1453 gcc_checking_assert (x
!= NULL_TREE
&& y
!= NULL_TREE
);
1458 if (TREE_CODE (x
) == ADDR_EXPR
1459 && TREE_CODE (y
) == ADDR_EXPR
1460 && TREE_CODE (TREE_OPERAND (x
, 0)) == CONST_DECL
1461 && TREE_CODE (TREE_OPERAND (y
, 0)) == CONST_DECL
)
1462 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x
, 0)),
1463 DECL_INITIAL (TREE_OPERAND (y
, 0)), 0);
1465 return operand_equal_p (x
, y
, 0);
1468 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
1471 values_equal_for_ipcp_p (ipa_polymorphic_call_context x
,
1472 ipa_polymorphic_call_context y
)
1474 return x
.equal_to (y
);
1478 /* Add a new value source to the value represented by THIS, marking that a
1479 value comes from edge CS and (if the underlying jump function is a
1480 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
1481 parameter described by SRC_INDEX. OFFSET is negative if the source was the
1482 scalar value of the parameter itself or the offset within an aggregate. */
1484 template <typename valtype
>
1486 ipcp_value
<valtype
>::add_source (cgraph_edge
*cs
, ipcp_value
*src_val
,
1487 int src_idx
, HOST_WIDE_INT offset
)
1489 ipcp_value_source
<valtype
> *src
;
1491 src
= new (ipcp_sources_pool
.allocate ()) ipcp_value_source
<valtype
>;
1492 src
->offset
= offset
;
1495 src
->index
= src_idx
;
1497 src
->next
= sources
;
1501 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
1502 SOURCE and clear all other fields. */
1504 static ipcp_value
<tree
> *
1505 allocate_and_init_ipcp_value (tree source
)
1507 ipcp_value
<tree
> *val
;
1509 val
= new (ipcp_cst_values_pool
.allocate ()) ipcp_value
<tree
>();
1510 val
->value
= source
;
1514 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
1515 value to SOURCE and clear all other fields. */
1517 static ipcp_value
<ipa_polymorphic_call_context
> *
1518 allocate_and_init_ipcp_value (ipa_polymorphic_call_context source
)
1520 ipcp_value
<ipa_polymorphic_call_context
> *val
;
1523 val
= new (ipcp_poly_ctx_values_pool
.allocate ())
1524 ipcp_value
<ipa_polymorphic_call_context
>();
1525 val
->value
= source
;
1529 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
1530 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
1531 meaning. OFFSET -1 means the source is scalar and not a part of an
1534 template <typename valtype
>
1536 ipcp_lattice
<valtype
>::add_value (valtype newval
, cgraph_edge
*cs
,
1537 ipcp_value
<valtype
> *src_val
,
1538 int src_idx
, HOST_WIDE_INT offset
)
1540 ipcp_value
<valtype
> *val
;
1545 for (val
= values
; val
; val
= val
->next
)
1546 if (values_equal_for_ipcp_p (val
->value
, newval
))
1548 if (ipa_edge_within_scc (cs
))
1550 ipcp_value_source
<valtype
> *s
;
1551 for (s
= val
->sources
; s
; s
= s
->next
)
1558 val
->add_source (cs
, src_val
, src_idx
, offset
);
1562 if (values_count
== PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE
))
1564 /* We can only free sources, not the values themselves, because sources
1565 of other values in this SCC might point to them. */
1566 for (val
= values
; val
; val
= val
->next
)
1568 while (val
->sources
)
1570 ipcp_value_source
<valtype
> *src
= val
->sources
;
1571 val
->sources
= src
->next
;
1572 ipcp_sources_pool
.remove ((ipcp_value_source
<tree
>*)src
);
1577 return set_to_bottom ();
1581 val
= allocate_and_init_ipcp_value (newval
);
1582 val
->add_source (cs
, src_val
, src_idx
, offset
);
1588 /* Propagate values through a pass-through jump function JFUNC associated with
1589 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1590 is the index of the source parameter. PARM_TYPE is the type of the
1591 parameter to which the result is passed. */
1594 propagate_vals_across_pass_through (cgraph_edge
*cs
, ipa_jump_func
*jfunc
,
1595 ipcp_lattice
<tree
> *src_lat
,
1596 ipcp_lattice
<tree
> *dest_lat
, int src_idx
,
1599 ipcp_value
<tree
> *src_val
;
1602 /* Do not create new values when propagating within an SCC because if there
1603 are arithmetic functions with circular dependencies, there is infinite
1604 number of them and we would just make lattices bottom. */
1605 if ((ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1606 && ipa_edge_within_scc (cs
))
1607 ret
= dest_lat
->set_contains_variable ();
1609 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1611 tree cstval
= ipa_get_jf_pass_through_result (jfunc
, src_val
->value
,
1615 ret
|= dest_lat
->add_value (cstval
, cs
, src_val
, src_idx
);
1617 ret
|= dest_lat
->set_contains_variable ();
1623 /* Propagate values through an ancestor jump function JFUNC associated with
1624 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
1625 is the index of the source parameter. */
1628 propagate_vals_across_ancestor (struct cgraph_edge
*cs
,
1629 struct ipa_jump_func
*jfunc
,
1630 ipcp_lattice
<tree
> *src_lat
,
1631 ipcp_lattice
<tree
> *dest_lat
, int src_idx
)
1633 ipcp_value
<tree
> *src_val
;
1636 if (ipa_edge_within_scc (cs
))
1637 return dest_lat
->set_contains_variable ();
1639 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1641 tree t
= ipa_get_jf_ancestor_result (jfunc
, src_val
->value
);
1644 ret
|= dest_lat
->add_value (t
, cs
, src_val
, src_idx
);
1646 ret
|= dest_lat
->set_contains_variable ();
1652 /* Propagate scalar values across jump function JFUNC that is associated with
1653 edge CS and put the values into DEST_LAT. PARM_TYPE is the type of the
1654 parameter to which the result is passed. */
1657 propagate_scalar_across_jump_function (struct cgraph_edge
*cs
,
1658 struct ipa_jump_func
*jfunc
,
1659 ipcp_lattice
<tree
> *dest_lat
,
1662 if (dest_lat
->bottom
)
1665 if (jfunc
->type
== IPA_JF_CONST
)
1667 tree val
= ipa_get_jf_constant (jfunc
);
1668 return dest_lat
->add_value (val
, cs
, NULL
, 0);
1670 else if (jfunc
->type
== IPA_JF_PASS_THROUGH
1671 || jfunc
->type
== IPA_JF_ANCESTOR
)
1673 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1674 ipcp_lattice
<tree
> *src_lat
;
1678 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1679 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1681 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1683 src_lat
= ipa_get_scalar_lat (caller_info
, src_idx
);
1684 if (src_lat
->bottom
)
1685 return dest_lat
->set_contains_variable ();
1687 /* If we would need to clone the caller and cannot, do not propagate. */
1688 if (!ipcp_versionable_function_p (cs
->caller
)
1689 && (src_lat
->contains_variable
1690 || (src_lat
->values_count
> 1)))
1691 return dest_lat
->set_contains_variable ();
1693 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1694 ret
= propagate_vals_across_pass_through (cs
, jfunc
, src_lat
,
1695 dest_lat
, src_idx
, param_type
);
1697 ret
= propagate_vals_across_ancestor (cs
, jfunc
, src_lat
, dest_lat
,
1700 if (src_lat
->contains_variable
)
1701 ret
|= dest_lat
->set_contains_variable ();
1706 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
1707 use it for indirect inlining), we should propagate them too. */
1708 return dest_lat
->set_contains_variable ();
1711 /* Propagate scalar values across jump function JFUNC that is associated with
1712 edge CS and describes argument IDX and put the values into DEST_LAT. */
1715 propagate_context_across_jump_function (cgraph_edge
*cs
,
1716 ipa_jump_func
*jfunc
, int idx
,
1717 ipcp_lattice
<ipa_polymorphic_call_context
> *dest_lat
)
1719 ipa_edge_args
*args
= IPA_EDGE_REF (cs
);
1720 if (dest_lat
->bottom
)
1723 bool added_sth
= false;
1724 bool type_preserved
= true;
1726 ipa_polymorphic_call_context edge_ctx
, *edge_ctx_ptr
1727 = ipa_get_ith_polymorhic_call_context (args
, idx
);
1730 edge_ctx
= *edge_ctx_ptr
;
1732 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1733 || jfunc
->type
== IPA_JF_ANCESTOR
)
1735 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1737 ipcp_lattice
<ipa_polymorphic_call_context
> *src_lat
;
1739 /* TODO: Once we figure out how to propagate speculations, it will
1740 probably be a good idea to switch to speculation if type_preserved is
1741 not set instead of punting. */
1742 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1744 if (ipa_get_jf_pass_through_operation (jfunc
) != NOP_EXPR
)
1746 type_preserved
= ipa_get_jf_pass_through_type_preserved (jfunc
);
1747 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1751 type_preserved
= ipa_get_jf_ancestor_type_preserved (jfunc
);
1752 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1755 src_lat
= ipa_get_poly_ctx_lat (caller_info
, src_idx
);
1756 /* If we would need to clone the caller and cannot, do not propagate. */
1757 if (!ipcp_versionable_function_p (cs
->caller
)
1758 && (src_lat
->contains_variable
1759 || (src_lat
->values_count
> 1)))
1762 ipcp_value
<ipa_polymorphic_call_context
> *src_val
;
1763 for (src_val
= src_lat
->values
; src_val
; src_val
= src_val
->next
)
1765 ipa_polymorphic_call_context cur
= src_val
->value
;
1767 if (!type_preserved
)
1768 cur
.possible_dynamic_type_change (cs
->in_polymorphic_cdtor
);
1769 if (jfunc
->type
== IPA_JF_ANCESTOR
)
1770 cur
.offset_by (ipa_get_jf_ancestor_offset (jfunc
));
1771 /* TODO: In cases we know how the context is going to be used,
1772 we can improve the result by passing proper OTR_TYPE. */
1773 cur
.combine_with (edge_ctx
);
1774 if (!cur
.useless_p ())
1776 if (src_lat
->contains_variable
1777 && !edge_ctx
.equal_to (cur
))
1778 ret
|= dest_lat
->set_contains_variable ();
1779 ret
|= dest_lat
->add_value (cur
, cs
, src_val
, src_idx
);
1789 if (!edge_ctx
.useless_p ())
1790 ret
|= dest_lat
->add_value (edge_ctx
, cs
);
1792 ret
|= dest_lat
->set_contains_variable ();
1798 /* Propagate bits across jfunc that is associated with
1799 edge cs and update dest_lattice accordingly. */
1802 propagate_bits_across_jump_function (cgraph_edge
*cs
, int idx
,
1803 ipa_jump_func
*jfunc
,
1804 ipcp_bits_lattice
*dest_lattice
)
1806 if (dest_lattice
->bottom_p ())
1809 enum availability availability
;
1810 cgraph_node
*callee
= cs
->callee
->function_symbol (&availability
);
1811 struct ipa_node_params
*callee_info
= IPA_NODE_REF (callee
);
1812 tree parm_type
= ipa_get_type (callee_info
, idx
);
1814 /* For K&R C programs, ipa_get_type() could return NULL_TREE. Avoid the
1815 transform for these cases. Similarly, we can have bad type mismatches
1816 with LTO, avoid doing anything with those too. */
1818 || (!INTEGRAL_TYPE_P (parm_type
) && !POINTER_TYPE_P (parm_type
)))
1820 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1821 fprintf (dump_file
, "Setting dest_lattice to bottom, because type of "
1822 "param %i of %s is NULL or unsuitable for bits propagation\n",
1823 idx
, cs
->callee
->name ());
1825 return dest_lattice
->set_to_bottom ();
1828 unsigned precision
= TYPE_PRECISION (parm_type
);
1829 signop sgn
= TYPE_SIGN (parm_type
);
1831 if (jfunc
->type
== IPA_JF_PASS_THROUGH
1832 || jfunc
->type
== IPA_JF_ANCESTOR
)
1834 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1835 tree operand
= NULL_TREE
;
1836 enum tree_code code
;
1839 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1841 code
= ipa_get_jf_pass_through_operation (jfunc
);
1842 src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1843 if (code
!= NOP_EXPR
)
1844 operand
= ipa_get_jf_pass_through_operand (jfunc
);
1848 code
= POINTER_PLUS_EXPR
;
1849 src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
1850 unsigned HOST_WIDE_INT offset
= ipa_get_jf_ancestor_offset (jfunc
) / BITS_PER_UNIT
;
1851 operand
= build_int_cstu (size_type_node
, offset
);
1854 struct ipcp_param_lattices
*src_lats
1855 = ipa_get_parm_lattices (caller_info
, src_idx
);
1857 /* Try to propagate bits if src_lattice is bottom, but jfunc is known.
1863 Assume lattice for x is bottom, however we can still propagate
1864 result of x & 0xff == 0xff, which gets computed during ccp1 pass
1865 and we store it in jump function during analysis stage. */
1867 if (src_lats
->bits_lattice
.bottom_p ()
1869 return dest_lattice
->meet_with (jfunc
->bits
->value
, jfunc
->bits
->mask
,
1872 return dest_lattice
->meet_with (src_lats
->bits_lattice
, precision
, sgn
,
1876 else if (jfunc
->type
== IPA_JF_ANCESTOR
)
1877 return dest_lattice
->set_to_bottom ();
1878 else if (jfunc
->bits
)
1879 return dest_lattice
->meet_with (jfunc
->bits
->value
, jfunc
->bits
->mask
,
1882 return dest_lattice
->set_to_bottom ();
1885 /* Emulate effects of unary OPERATION and/or conversion from SRC_TYPE to
1886 DST_TYPE on value range in SRC_VR and store it to DST_VR. Return true if
1887 the result is a range or an anti-range. */
1890 ipa_vr_operation_and_type_effects (value_range
*dst_vr
, value_range
*src_vr
,
1891 enum tree_code operation
,
1892 tree dst_type
, tree src_type
)
1894 memset (dst_vr
, 0, sizeof (*dst_vr
));
1895 extract_range_from_unary_expr (dst_vr
, operation
, dst_type
, src_vr
, src_type
);
1896 if (dst_vr
->type
== VR_RANGE
|| dst_vr
->type
== VR_ANTI_RANGE
)
1902 /* Propagate value range across jump function JFUNC that is associated with
1903 edge CS with param of callee of PARAM_TYPE and update DEST_PLATS
1907 propagate_vr_across_jump_function (cgraph_edge
*cs
, ipa_jump_func
*jfunc
,
1908 struct ipcp_param_lattices
*dest_plats
,
1911 ipcp_vr_lattice
*dest_lat
= &dest_plats
->m_value_range
;
1913 if (dest_lat
->bottom_p ())
1917 || (!INTEGRAL_TYPE_P (param_type
)
1918 && !POINTER_TYPE_P (param_type
)))
1919 return dest_lat
->set_to_bottom ();
1921 if (jfunc
->type
== IPA_JF_PASS_THROUGH
)
1923 enum tree_code operation
= ipa_get_jf_pass_through_operation (jfunc
);
1925 if (TREE_CODE_CLASS (operation
) == tcc_unary
)
1927 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
1928 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
1929 tree operand_type
= ipa_get_type (caller_info
, src_idx
);
1930 struct ipcp_param_lattices
*src_lats
1931 = ipa_get_parm_lattices (caller_info
, src_idx
);
1933 if (src_lats
->m_value_range
.bottom_p ())
1934 return dest_lat
->set_to_bottom ();
1936 if (ipa_vr_operation_and_type_effects (&vr
,
1937 &src_lats
->m_value_range
.m_vr
,
1938 operation
, param_type
,
1940 return dest_lat
->meet_with (&vr
);
1943 else if (jfunc
->type
== IPA_JF_CONST
)
1945 tree val
= ipa_get_jf_constant (jfunc
);
1946 if (TREE_CODE (val
) == INTEGER_CST
)
1948 val
= fold_convert (param_type
, val
);
1949 if (TREE_OVERFLOW_P (val
))
1950 val
= drop_tree_overflow (val
);
1953 memset (&tmpvr
, 0, sizeof (tmpvr
));
1954 tmpvr
.type
= VR_RANGE
;
1957 return dest_lat
->meet_with (&tmpvr
);
1963 && ipa_vr_operation_and_type_effects (&vr
, jfunc
->m_vr
, NOP_EXPR
,
1965 TREE_TYPE (jfunc
->m_vr
->min
)))
1966 return dest_lat
->meet_with (&vr
);
1968 return dest_lat
->set_to_bottom ();
1971 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
1972 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
1973 other cases, return false). If there are no aggregate items, set
1974 aggs_by_ref to NEW_AGGS_BY_REF. */
1977 set_check_aggs_by_ref (struct ipcp_param_lattices
*dest_plats
,
1978 bool new_aggs_by_ref
)
1980 if (dest_plats
->aggs
)
1982 if (dest_plats
->aggs_by_ref
!= new_aggs_by_ref
)
1984 set_agg_lats_to_bottom (dest_plats
);
1989 dest_plats
->aggs_by_ref
= new_aggs_by_ref
;
1993 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
1994 already existing lattice for the given OFFSET and SIZE, marking all skipped
1995 lattices as containing variable and checking for overlaps. If there is no
1996 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
1997 it with offset, size and contains_variable to PRE_EXISTING, and return true,
1998 unless there are too many already. If there are two many, return false. If
1999 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
2000 skipped lattices were newly marked as containing variable, set *CHANGE to
2004 merge_agg_lats_step (struct ipcp_param_lattices
*dest_plats
,
2005 HOST_WIDE_INT offset
, HOST_WIDE_INT val_size
,
2006 struct ipcp_agg_lattice
***aglat
,
2007 bool pre_existing
, bool *change
)
2009 gcc_checking_assert (offset
>= 0);
2011 while (**aglat
&& (**aglat
)->offset
< offset
)
2013 if ((**aglat
)->offset
+ (**aglat
)->size
> offset
)
2015 set_agg_lats_to_bottom (dest_plats
);
2018 *change
|= (**aglat
)->set_contains_variable ();
2019 *aglat
= &(**aglat
)->next
;
2022 if (**aglat
&& (**aglat
)->offset
== offset
)
2024 if ((**aglat
)->size
!= val_size
2026 && (**aglat
)->next
->offset
< offset
+ val_size
))
2028 set_agg_lats_to_bottom (dest_plats
);
2031 gcc_checking_assert (!(**aglat
)->next
2032 || (**aglat
)->next
->offset
>= offset
+ val_size
);
2037 struct ipcp_agg_lattice
*new_al
;
2039 if (**aglat
&& (**aglat
)->offset
< offset
+ val_size
)
2041 set_agg_lats_to_bottom (dest_plats
);
2044 if (dest_plats
->aggs_count
== PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS
))
2046 dest_plats
->aggs_count
++;
2047 new_al
= ipcp_agg_lattice_pool
.allocate ();
2048 memset (new_al
, 0, sizeof (*new_al
));
2050 new_al
->offset
= offset
;
2051 new_al
->size
= val_size
;
2052 new_al
->contains_variable
= pre_existing
;
2054 new_al
->next
= **aglat
;
2060 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
2061 containing an unknown value. */
2064 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice
*aglat
)
2069 ret
|= aglat
->set_contains_variable ();
2070 aglat
= aglat
->next
;
2075 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
2076 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
2077 parameter used for lattice value sources. Return true if DEST_PLATS changed
2081 merge_aggregate_lattices (struct cgraph_edge
*cs
,
2082 struct ipcp_param_lattices
*dest_plats
,
2083 struct ipcp_param_lattices
*src_plats
,
2084 int src_idx
, HOST_WIDE_INT offset_delta
)
2086 bool pre_existing
= dest_plats
->aggs
!= NULL
;
2087 struct ipcp_agg_lattice
**dst_aglat
;
2090 if (set_check_aggs_by_ref (dest_plats
, src_plats
->aggs_by_ref
))
2092 if (src_plats
->aggs_bottom
)
2093 return set_agg_lats_contain_variable (dest_plats
);
2094 if (src_plats
->aggs_contain_variable
)
2095 ret
|= set_agg_lats_contain_variable (dest_plats
);
2096 dst_aglat
= &dest_plats
->aggs
;
2098 for (struct ipcp_agg_lattice
*src_aglat
= src_plats
->aggs
;
2100 src_aglat
= src_aglat
->next
)
2102 HOST_WIDE_INT new_offset
= src_aglat
->offset
- offset_delta
;
2106 if (merge_agg_lats_step (dest_plats
, new_offset
, src_aglat
->size
,
2107 &dst_aglat
, pre_existing
, &ret
))
2109 struct ipcp_agg_lattice
*new_al
= *dst_aglat
;
2111 dst_aglat
= &(*dst_aglat
)->next
;
2112 if (src_aglat
->bottom
)
2114 ret
|= new_al
->set_contains_variable ();
2117 if (src_aglat
->contains_variable
)
2118 ret
|= new_al
->set_contains_variable ();
2119 for (ipcp_value
<tree
> *val
= src_aglat
->values
;
2122 ret
|= new_al
->add_value (val
->value
, cs
, val
, src_idx
,
2125 else if (dest_plats
->aggs_bottom
)
2128 ret
|= set_chain_of_aglats_contains_variable (*dst_aglat
);
2132 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
2133 pass-through JFUNC and if so, whether it has conform and conforms to the
2134 rules about propagating values passed by reference. */
2137 agg_pass_through_permissible_p (struct ipcp_param_lattices
*src_plats
,
2138 struct ipa_jump_func
*jfunc
)
2140 return src_plats
->aggs
2141 && (!src_plats
->aggs_by_ref
2142 || ipa_get_jf_pass_through_agg_preserved (jfunc
));
2145 /* Propagate scalar values across jump function JFUNC that is associated with
2146 edge CS and put the values into DEST_LAT. */
2149 propagate_aggs_across_jump_function (struct cgraph_edge
*cs
,
2150 struct ipa_jump_func
*jfunc
,
2151 struct ipcp_param_lattices
*dest_plats
)
2155 if (dest_plats
->aggs_bottom
)
2158 if (jfunc
->type
== IPA_JF_PASS_THROUGH
2159 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
2161 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
2162 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
2163 struct ipcp_param_lattices
*src_plats
;
2165 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
2166 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
2168 /* Currently we do not produce clobber aggregate jump
2169 functions, replace with merging when we do. */
2170 gcc_assert (!jfunc
->agg
.items
);
2171 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
,
2175 ret
|= set_agg_lats_contain_variable (dest_plats
);
2177 else if (jfunc
->type
== IPA_JF_ANCESTOR
2178 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
2180 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
2181 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
2182 struct ipcp_param_lattices
*src_plats
;
2184 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
2185 if (src_plats
->aggs
&& src_plats
->aggs_by_ref
)
2187 /* Currently we do not produce clobber aggregate jump
2188 functions, replace with merging when we do. */
2189 gcc_assert (!jfunc
->agg
.items
);
2190 ret
|= merge_aggregate_lattices (cs
, dest_plats
, src_plats
, src_idx
,
2191 ipa_get_jf_ancestor_offset (jfunc
));
2193 else if (!src_plats
->aggs_by_ref
)
2194 ret
|= set_agg_lats_to_bottom (dest_plats
);
2196 ret
|= set_agg_lats_contain_variable (dest_plats
);
2198 else if (jfunc
->agg
.items
)
2200 bool pre_existing
= dest_plats
->aggs
!= NULL
;
2201 struct ipcp_agg_lattice
**aglat
= &dest_plats
->aggs
;
2202 struct ipa_agg_jf_item
*item
;
2205 if (set_check_aggs_by_ref (dest_plats
, jfunc
->agg
.by_ref
))
2208 FOR_EACH_VEC_ELT (*jfunc
->agg
.items
, i
, item
)
2210 HOST_WIDE_INT val_size
;
2212 if (item
->offset
< 0)
2214 gcc_checking_assert (is_gimple_ip_invariant (item
->value
));
2215 val_size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item
->value
)));
2217 if (merge_agg_lats_step (dest_plats
, item
->offset
, val_size
,
2218 &aglat
, pre_existing
, &ret
))
2220 ret
|= (*aglat
)->add_value (item
->value
, cs
, NULL
, 0, 0);
2221 aglat
= &(*aglat
)->next
;
2223 else if (dest_plats
->aggs_bottom
)
2227 ret
|= set_chain_of_aglats_contains_variable (*aglat
);
2230 ret
|= set_agg_lats_contain_variable (dest_plats
);
2235 /* Return true if on the way cfrom CS->caller to the final (non-alias and
2236 non-thunk) destination, the call passes through a thunk. */
2239 call_passes_through_thunk_p (cgraph_edge
*cs
)
2241 cgraph_node
*alias_or_thunk
= cs
->callee
;
2242 while (alias_or_thunk
->alias
)
2243 alias_or_thunk
= alias_or_thunk
->get_alias_target ();
2244 return alias_or_thunk
->thunk
.thunk_p
;
2247 /* Propagate constants from the caller to the callee of CS. INFO describes the
2251 propagate_constants_across_call (struct cgraph_edge
*cs
)
2253 struct ipa_node_params
*callee_info
;
2254 enum availability availability
;
2255 cgraph_node
*callee
;
2256 struct ipa_edge_args
*args
;
2258 int i
, args_count
, parms_count
;
2260 callee
= cs
->callee
->function_symbol (&availability
);
2261 if (!callee
->definition
)
2263 gcc_checking_assert (callee
->has_gimple_body_p ());
2264 callee_info
= IPA_NODE_REF (callee
);
2266 args
= IPA_EDGE_REF (cs
);
2267 args_count
= ipa_get_cs_argument_count (args
);
2268 parms_count
= ipa_get_param_count (callee_info
);
2269 if (parms_count
== 0)
2272 /* No propagation through instrumentation thunks is available yet.
2273 It should be possible with proper mapping of call args and
2274 instrumented callee params in the propagation loop below. But
2275 this case mostly occurs when legacy code calls instrumented code
2276 and it is not a primary target for optimizations.
2277 We detect instrumentation thunks in aliases and thunks chain by
2278 checking instrumentation_clone flag for chain source and target.
2279 Going through instrumentation thunks we always have it changed
2280 from 0 to 1 and all other nodes do not change it. */
2281 if (!cs
->callee
->instrumentation_clone
2282 && callee
->instrumentation_clone
)
2284 for (i
= 0; i
< parms_count
; i
++)
2285 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
,
2290 /* If this call goes through a thunk we must not propagate to the first (0th)
2291 parameter. However, we might need to uncover a thunk from below a series
2292 of aliases first. */
2293 if (call_passes_through_thunk_p (cs
))
2295 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
,
2302 for (; (i
< args_count
) && (i
< parms_count
); i
++)
2304 struct ipa_jump_func
*jump_func
= ipa_get_ith_jump_func (args
, i
);
2305 struct ipcp_param_lattices
*dest_plats
;
2306 tree param_type
= ipa_get_type (callee_info
, i
);
2308 dest_plats
= ipa_get_parm_lattices (callee_info
, i
);
2309 if (availability
== AVAIL_INTERPOSABLE
)
2310 ret
|= set_all_contains_variable (dest_plats
);
2313 ret
|= propagate_scalar_across_jump_function (cs
, jump_func
,
2314 &dest_plats
->itself
,
2316 ret
|= propagate_context_across_jump_function (cs
, jump_func
, i
,
2317 &dest_plats
->ctxlat
);
2319 |= propagate_bits_across_jump_function (cs
, i
, jump_func
,
2320 &dest_plats
->bits_lattice
);
2321 ret
|= propagate_aggs_across_jump_function (cs
, jump_func
,
2323 if (opt_for_fn (callee
->decl
, flag_ipa_vrp
))
2324 ret
|= propagate_vr_across_jump_function (cs
, jump_func
,
2325 dest_plats
, param_type
);
2327 ret
|= dest_plats
->m_value_range
.set_to_bottom ();
2330 for (; i
< parms_count
; i
++)
2331 ret
|= set_all_contains_variable (ipa_get_parm_lattices (callee_info
, i
));
2336 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
2337 KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter
2338 three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */
2341 ipa_get_indirect_edge_target_1 (struct cgraph_edge
*ie
,
2342 vec
<tree
> known_csts
,
2343 vec
<ipa_polymorphic_call_context
> known_contexts
,
2344 vec
<ipa_agg_jump_function_p
> known_aggs
,
2345 struct ipa_agg_replacement_value
*agg_reps
,
2348 int param_index
= ie
->indirect_info
->param_index
;
2349 HOST_WIDE_INT anc_offset
;
2353 *speculative
= false;
2355 if (param_index
== -1
2356 || known_csts
.length () <= (unsigned int) param_index
)
2359 if (!ie
->indirect_info
->polymorphic
)
2363 if (ie
->indirect_info
->agg_contents
)
2366 if (agg_reps
&& ie
->indirect_info
->guaranteed_unmodified
)
2370 if (agg_reps
->index
== param_index
2371 && agg_reps
->offset
== ie
->indirect_info
->offset
2372 && agg_reps
->by_ref
== ie
->indirect_info
->by_ref
)
2374 t
= agg_reps
->value
;
2377 agg_reps
= agg_reps
->next
;
2382 struct ipa_agg_jump_function
*agg
;
2383 if (known_aggs
.length () > (unsigned int) param_index
)
2384 agg
= known_aggs
[param_index
];
2387 bool from_global_constant
;
2388 t
= ipa_find_agg_cst_for_param (agg
, known_csts
[param_index
],
2389 ie
->indirect_info
->offset
,
2390 ie
->indirect_info
->by_ref
,
2391 &from_global_constant
);
2393 && !from_global_constant
2394 && !ie
->indirect_info
->guaranteed_unmodified
)
2399 t
= known_csts
[param_index
];
2402 && TREE_CODE (t
) == ADDR_EXPR
2403 && TREE_CODE (TREE_OPERAND (t
, 0)) == FUNCTION_DECL
)
2404 return TREE_OPERAND (t
, 0);
2409 if (!opt_for_fn (ie
->caller
->decl
, flag_devirtualize
))
2412 gcc_assert (!ie
->indirect_info
->agg_contents
);
2413 anc_offset
= ie
->indirect_info
->offset
;
2417 /* Try to work out value of virtual table pointer value in replacemnets. */
2418 if (!t
&& agg_reps
&& !ie
->indirect_info
->by_ref
)
2422 if (agg_reps
->index
== param_index
2423 && agg_reps
->offset
== ie
->indirect_info
->offset
2424 && agg_reps
->by_ref
)
2426 t
= agg_reps
->value
;
2429 agg_reps
= agg_reps
->next
;
2433 /* Try to work out value of virtual table pointer value in known
2434 aggregate values. */
2435 if (!t
&& known_aggs
.length () > (unsigned int) param_index
2436 && !ie
->indirect_info
->by_ref
)
2438 struct ipa_agg_jump_function
*agg
;
2439 agg
= known_aggs
[param_index
];
2440 t
= ipa_find_agg_cst_for_param (agg
, known_csts
[param_index
],
2441 ie
->indirect_info
->offset
, true);
2444 /* If we found the virtual table pointer, lookup the target. */
2448 unsigned HOST_WIDE_INT offset
;
2449 if (vtable_pointer_value_to_vtable (t
, &vtable
, &offset
))
2452 target
= gimple_get_virt_method_for_vtable (ie
->indirect_info
->otr_token
,
2453 vtable
, offset
, &can_refer
);
2457 || (TREE_CODE (TREE_TYPE (target
)) == FUNCTION_TYPE
2458 && DECL_FUNCTION_CODE (target
) == BUILT_IN_UNREACHABLE
)
2459 || !possible_polymorphic_call_target_p
2460 (ie
, cgraph_node::get (target
)))
2462 /* Do not speculate builtin_unreachable, it is stupid! */
2463 if (ie
->indirect_info
->vptr_changed
)
2465 target
= ipa_impossible_devirt_target (ie
, target
);
2467 *speculative
= ie
->indirect_info
->vptr_changed
;
2474 /* Do we know the constant value of pointer? */
2476 t
= known_csts
[param_index
];
2478 gcc_checking_assert (!t
|| TREE_CODE (t
) != TREE_BINFO
);
2480 ipa_polymorphic_call_context context
;
2481 if (known_contexts
.length () > (unsigned int) param_index
)
2483 context
= known_contexts
[param_index
];
2484 context
.offset_by (anc_offset
);
2485 if (ie
->indirect_info
->vptr_changed
)
2486 context
.possible_dynamic_type_change (ie
->in_polymorphic_cdtor
,
2487 ie
->indirect_info
->otr_type
);
2490 ipa_polymorphic_call_context ctx2
= ipa_polymorphic_call_context
2491 (t
, ie
->indirect_info
->otr_type
, anc_offset
);
2492 if (!ctx2
.useless_p ())
2493 context
.combine_with (ctx2
, ie
->indirect_info
->otr_type
);
2498 context
= ipa_polymorphic_call_context (t
, ie
->indirect_info
->otr_type
,
2500 if (ie
->indirect_info
->vptr_changed
)
2501 context
.possible_dynamic_type_change (ie
->in_polymorphic_cdtor
,
2502 ie
->indirect_info
->otr_type
);
2507 vec
<cgraph_node
*>targets
;
2510 targets
= possible_polymorphic_call_targets
2511 (ie
->indirect_info
->otr_type
,
2512 ie
->indirect_info
->otr_token
,
2514 if (!final
|| targets
.length () > 1)
2516 struct cgraph_node
*node
;
2519 if (!opt_for_fn (ie
->caller
->decl
, flag_devirtualize_speculatively
)
2520 || ie
->speculative
|| !ie
->maybe_hot_p ())
2522 node
= try_speculative_devirtualization (ie
->indirect_info
->otr_type
,
2523 ie
->indirect_info
->otr_token
,
2527 *speculative
= true;
2528 target
= node
->decl
;
2535 *speculative
= false;
2536 if (targets
.length () == 1)
2537 target
= targets
[0]->decl
;
2539 target
= ipa_impossible_devirt_target (ie
, NULL_TREE
);
2542 if (target
&& !possible_polymorphic_call_target_p (ie
,
2543 cgraph_node::get (target
)))
2547 target
= ipa_impossible_devirt_target (ie
, target
);
2554 /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS,
2555 KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL)
2556 return the destination. */
2559 ipa_get_indirect_edge_target (struct cgraph_edge
*ie
,
2560 vec
<tree
> known_csts
,
2561 vec
<ipa_polymorphic_call_context
> known_contexts
,
2562 vec
<ipa_agg_jump_function_p
> known_aggs
,
2565 return ipa_get_indirect_edge_target_1 (ie
, known_csts
, known_contexts
,
2566 known_aggs
, NULL
, speculative
);
2569 /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS
2570 and KNOWN_CONTEXTS. */
2573 devirtualization_time_bonus (struct cgraph_node
*node
,
2574 vec
<tree
> known_csts
,
2575 vec
<ipa_polymorphic_call_context
> known_contexts
,
2576 vec
<ipa_agg_jump_function_p
> known_aggs
)
2578 struct cgraph_edge
*ie
;
2581 for (ie
= node
->indirect_calls
; ie
; ie
= ie
->next_callee
)
2583 struct cgraph_node
*callee
;
2584 struct ipa_fn_summary
*isummary
;
2585 enum availability avail
;
2589 target
= ipa_get_indirect_edge_target (ie
, known_csts
, known_contexts
,
2590 known_aggs
, &speculative
);
2594 /* Only bare minimum benefit for clearly un-inlineable targets. */
2596 callee
= cgraph_node::get (target
);
2597 if (!callee
|| !callee
->definition
)
2599 callee
= callee
->function_symbol (&avail
);
2600 if (avail
< AVAIL_AVAILABLE
)
2602 isummary
= ipa_fn_summaries
->get (callee
);
2603 if (!isummary
->inlinable
)
2606 /* FIXME: The values below need re-considering and perhaps also
2607 integrating into the cost metrics, at lest in some very basic way. */
2608 if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 4)
2609 res
+= 31 / ((int)speculative
+ 1);
2610 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
/ 2)
2611 res
+= 15 / ((int)speculative
+ 1);
2612 else if (isummary
->size
<= MAX_INLINE_INSNS_AUTO
2613 || DECL_DECLARED_INLINE_P (callee
->decl
))
2614 res
+= 7 / ((int)speculative
+ 1);
2620 /* Return time bonus incurred because of HINTS. */
2623 hint_time_bonus (ipa_hints hints
)
2626 if (hints
& (INLINE_HINT_loop_iterations
| INLINE_HINT_loop_stride
))
2627 result
+= PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS
);
2628 if (hints
& INLINE_HINT_array_index
)
2629 result
+= PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS
);
2633 /* If there is a reason to penalize the function described by INFO in the
2634 cloning goodness evaluation, do so. */
2636 static inline int64_t
2637 incorporate_penalties (ipa_node_params
*info
, int64_t evaluation
)
2639 if (info
->node_within_scc
)
2640 evaluation
= (evaluation
2641 * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY
))) / 100;
2643 if (info
->node_calling_single_call
)
2644 evaluation
= (evaluation
2645 * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY
)))
2651 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
2652 and SIZE_COST and with the sum of frequencies of incoming edges to the
2653 potential new clone in FREQUENCIES. */
2656 good_cloning_opportunity_p (struct cgraph_node
*node
, int time_benefit
,
2657 int freq_sum
, profile_count count_sum
, int size_cost
)
2659 if (time_benefit
== 0
2660 || !opt_for_fn (node
->decl
, flag_ipa_cp_clone
)
2661 || node
->optimize_for_size_p ())
2664 gcc_assert (size_cost
> 0);
2666 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2667 if (max_count
> profile_count::zero ())
2669 int factor
= RDIV (count_sum
.probability_in
2670 (max_count
).to_reg_br_prob_base ()
2671 * 1000, REG_BR_PROB_BASE
);
2672 int64_t evaluation
= (((int64_t) time_benefit
* factor
)
2674 evaluation
= incorporate_penalties (info
, evaluation
);
2676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2678 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
2679 "size: %i, count_sum: ", time_benefit
, size_cost
);
2680 count_sum
.dump (dump_file
);
2681 fprintf (dump_file
, "%s%s) -> evaluation: " "%" PRId64
2682 ", threshold: %i\n",
2683 info
->node_within_scc
? ", scc" : "",
2684 info
->node_calling_single_call
? ", single_call" : "",
2685 evaluation
, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
2688 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
2692 int64_t evaluation
= (((int64_t) time_benefit
* freq_sum
)
2694 evaluation
= incorporate_penalties (info
, evaluation
);
2696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2697 fprintf (dump_file
, " good_cloning_opportunity_p (time: %i, "
2698 "size: %i, freq_sum: %i%s%s) -> evaluation: "
2699 "%" PRId64
", threshold: %i\n",
2700 time_benefit
, size_cost
, freq_sum
,
2701 info
->node_within_scc
? ", scc" : "",
2702 info
->node_calling_single_call
? ", single_call" : "",
2703 evaluation
, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
));
2705 return evaluation
>= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD
);
2709 /* Return all context independent values from aggregate lattices in PLATS in a
2710 vector. Return NULL if there are none. */
2712 static vec
<ipa_agg_jf_item
, va_gc
> *
2713 context_independent_aggregate_values (struct ipcp_param_lattices
*plats
)
2715 vec
<ipa_agg_jf_item
, va_gc
> *res
= NULL
;
2717 if (plats
->aggs_bottom
2718 || plats
->aggs_contain_variable
2719 || plats
->aggs_count
== 0)
2722 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
;
2724 aglat
= aglat
->next
)
2725 if (aglat
->is_single_const ())
2727 struct ipa_agg_jf_item item
;
2728 item
.offset
= aglat
->offset
;
2729 item
.value
= aglat
->values
->value
;
2730 vec_safe_push (res
, item
);
2735 /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and
2736 populate them with values of parameters that are known independent of the
2737 context. INFO describes the function. If REMOVABLE_PARAMS_COST is
2738 non-NULL, the movement cost of all removable parameters will be stored in
2742 gather_context_independent_values (struct ipa_node_params
*info
,
2743 vec
<tree
> *known_csts
,
2744 vec
<ipa_polymorphic_call_context
>
2746 vec
<ipa_agg_jump_function
> *known_aggs
,
2747 int *removable_params_cost
)
2749 int i
, count
= ipa_get_param_count (info
);
2752 known_csts
->create (0);
2753 known_contexts
->create (0);
2754 known_csts
->safe_grow_cleared (count
);
2755 known_contexts
->safe_grow_cleared (count
);
2758 known_aggs
->create (0);
2759 known_aggs
->safe_grow_cleared (count
);
2762 if (removable_params_cost
)
2763 *removable_params_cost
= 0;
2765 for (i
= 0; i
< count
; i
++)
2767 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2768 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
2770 if (lat
->is_single_const ())
2772 ipcp_value
<tree
> *val
= lat
->values
;
2773 gcc_checking_assert (TREE_CODE (val
->value
) != TREE_BINFO
);
2774 (*known_csts
)[i
] = val
->value
;
2775 if (removable_params_cost
)
2776 *removable_params_cost
2777 += estimate_move_cost (TREE_TYPE (val
->value
), false);
2780 else if (removable_params_cost
2781 && !ipa_is_param_used (info
, i
))
2782 *removable_params_cost
2783 += ipa_get_param_move_cost (info
, i
);
2785 if (!ipa_is_param_used (info
, i
))
2788 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
2789 /* Do not account known context as reason for cloning. We can see
2790 if it permits devirtualization. */
2791 if (ctxlat
->is_single_const ())
2792 (*known_contexts
)[i
] = ctxlat
->values
->value
;
2796 vec
<ipa_agg_jf_item
, va_gc
> *agg_items
;
2797 struct ipa_agg_jump_function
*ajf
;
2799 agg_items
= context_independent_aggregate_values (plats
);
2800 ajf
= &(*known_aggs
)[i
];
2801 ajf
->items
= agg_items
;
2802 ajf
->by_ref
= plats
->aggs_by_ref
;
2803 ret
|= agg_items
!= NULL
;
2810 /* The current interface in ipa-inline-analysis requires a pointer vector.
2813 FIXME: That interface should be re-worked, this is slightly silly. Still,
2814 I'd like to discuss how to change it first and this demonstrates the
2817 static vec
<ipa_agg_jump_function_p
>
2818 agg_jmp_p_vec_for_t_vec (vec
<ipa_agg_jump_function
> known_aggs
)
2820 vec
<ipa_agg_jump_function_p
> ret
;
2821 struct ipa_agg_jump_function
*ajf
;
2824 ret
.create (known_aggs
.length ());
2825 FOR_EACH_VEC_ELT (known_aggs
, i
, ajf
)
2826 ret
.quick_push (ajf
);
2830 /* Perform time and size measurement of NODE with the context given in
2831 KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost
2832 given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of
2833 all context-independent removable parameters and EST_MOVE_COST of estimated
2834 movement of the considered parameter and store it into VAL. */
2837 perform_estimation_of_a_value (cgraph_node
*node
, vec
<tree
> known_csts
,
2838 vec
<ipa_polymorphic_call_context
> known_contexts
,
2839 vec
<ipa_agg_jump_function_p
> known_aggs_ptrs
,
2840 int removable_params_cost
,
2841 int est_move_cost
, ipcp_value_base
*val
)
2843 int size
, time_benefit
;
2844 sreal time
, base_time
;
2847 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_contexts
,
2848 known_aggs_ptrs
, &size
, &time
,
2849 &base_time
, &hints
);
2851 if (base_time
> 65535)
2853 time_benefit
= base_time
.to_int ()
2854 + devirtualization_time_bonus (node
, known_csts
, known_contexts
,
2856 + hint_time_bonus (hints
)
2857 + removable_params_cost
+ est_move_cost
;
2859 gcc_checking_assert (size
>=0);
2860 /* The inliner-heuristics based estimates may think that in certain
2861 contexts some functions do not have any size at all but we want
2862 all specializations to have at least a tiny cost, not least not to
2867 val
->local_time_benefit
= time_benefit
;
2868 val
->local_size_cost
= size
;
2871 /* Iterate over known values of parameters of NODE and estimate the local
2872 effects in terms of time and size they have. */
2875 estimate_local_effects (struct cgraph_node
*node
)
2877 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
2878 int i
, count
= ipa_get_param_count (info
);
2879 vec
<tree
> known_csts
;
2880 vec
<ipa_polymorphic_call_context
> known_contexts
;
2881 vec
<ipa_agg_jump_function
> known_aggs
;
2882 vec
<ipa_agg_jump_function_p
> known_aggs_ptrs
;
2884 int removable_params_cost
;
2886 if (!count
|| !ipcp_versionable_function_p (node
))
2889 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2890 fprintf (dump_file
, "\nEstimating effects for %s.\n", node
->dump_name ());
2892 always_const
= gather_context_independent_values (info
, &known_csts
,
2893 &known_contexts
, &known_aggs
,
2894 &removable_params_cost
);
2895 known_aggs_ptrs
= agg_jmp_p_vec_for_t_vec (known_aggs
);
2896 int devirt_bonus
= devirtualization_time_bonus (node
, known_csts
,
2897 known_contexts
, known_aggs_ptrs
);
2898 if (always_const
|| devirt_bonus
2899 || (removable_params_cost
&& node
->local
.can_change_signature
))
2901 struct caller_statistics stats
;
2903 sreal time
, base_time
;
2906 init_caller_stats (&stats
);
2907 node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
2909 estimate_ipcp_clone_size_and_time (node
, known_csts
, known_contexts
,
2910 known_aggs_ptrs
, &size
, &time
,
2911 &base_time
, &hints
);
2912 time
-= devirt_bonus
;
2913 time
-= hint_time_bonus (hints
);
2914 time
-= removable_params_cost
;
2915 size
-= stats
.n_calls
* removable_params_cost
;
2918 fprintf (dump_file
, " - context independent values, size: %i, "
2919 "time_benefit: %f\n", size
, (base_time
- time
).to_double ());
2921 if (size
<= 0 || node
->local
.local
)
2923 info
->do_clone_for_all_contexts
= true;
2926 fprintf (dump_file
, " Decided to specialize for all "
2927 "known contexts, code not going to grow.\n");
2929 else if (good_cloning_opportunity_p (node
,
2930 MAX ((base_time
- time
).to_int (),
2932 stats
.freq_sum
, stats
.count_sum
,
2935 if (size
+ overall_size
<= max_new_size
)
2937 info
->do_clone_for_all_contexts
= true;
2938 overall_size
+= size
;
2941 fprintf (dump_file
, " Decided to specialize for all "
2942 "known contexts, growth deemed beneficial.\n");
2944 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2945 fprintf (dump_file
, " Not cloning for all contexts because "
2946 "max_new_size would be reached with %li.\n",
2947 size
+ overall_size
);
2949 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2950 fprintf (dump_file
, " Not cloning for all contexts because "
2951 "!good_cloning_opportunity_p.\n");
2955 for (i
= 0; i
< count
; i
++)
2957 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2958 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
2959 ipcp_value
<tree
> *val
;
2966 for (val
= lat
->values
; val
; val
= val
->next
)
2968 gcc_checking_assert (TREE_CODE (val
->value
) != TREE_BINFO
);
2969 known_csts
[i
] = val
->value
;
2971 int emc
= estimate_move_cost (TREE_TYPE (val
->value
), true);
2972 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
2974 removable_params_cost
, emc
, val
);
2976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2978 fprintf (dump_file
, " - estimates for value ");
2979 print_ipcp_constant_value (dump_file
, val
->value
);
2980 fprintf (dump_file
, " for ");
2981 ipa_dump_param (dump_file
, info
, i
);
2982 fprintf (dump_file
, ": time_benefit: %i, size: %i\n",
2983 val
->local_time_benefit
, val
->local_size_cost
);
2986 known_csts
[i
] = NULL_TREE
;
2989 for (i
= 0; i
< count
; i
++)
2991 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
2993 if (!plats
->virt_call
)
2996 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
2997 ipcp_value
<ipa_polymorphic_call_context
> *val
;
3001 || !known_contexts
[i
].useless_p ())
3004 for (val
= ctxlat
->values
; val
; val
= val
->next
)
3006 known_contexts
[i
] = val
->value
;
3007 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
3009 removable_params_cost
, 0, val
);
3011 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3013 fprintf (dump_file
, " - estimates for polymorphic context ");
3014 print_ipcp_constant_value (dump_file
, val
->value
);
3015 fprintf (dump_file
, " for ");
3016 ipa_dump_param (dump_file
, info
, i
);
3017 fprintf (dump_file
, ": time_benefit: %i, size: %i\n",
3018 val
->local_time_benefit
, val
->local_size_cost
);
3021 known_contexts
[i
] = ipa_polymorphic_call_context ();
3024 for (i
= 0; i
< count
; i
++)
3026 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
3027 struct ipa_agg_jump_function
*ajf
;
3028 struct ipcp_agg_lattice
*aglat
;
3030 if (plats
->aggs_bottom
|| !plats
->aggs
)
3033 ajf
= &known_aggs
[i
];
3034 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3036 ipcp_value
<tree
> *val
;
3037 if (aglat
->bottom
|| !aglat
->values
3038 /* If the following is true, the one value is in known_aggs. */
3039 || (!plats
->aggs_contain_variable
3040 && aglat
->is_single_const ()))
3043 for (val
= aglat
->values
; val
; val
= val
->next
)
3045 struct ipa_agg_jf_item item
;
3047 item
.offset
= aglat
->offset
;
3048 item
.value
= val
->value
;
3049 vec_safe_push (ajf
->items
, item
);
3051 perform_estimation_of_a_value (node
, known_csts
, known_contexts
,
3053 removable_params_cost
, 0, val
);
3055 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3057 fprintf (dump_file
, " - estimates for value ");
3058 print_ipcp_constant_value (dump_file
, val
->value
);
3059 fprintf (dump_file
, " for ");
3060 ipa_dump_param (dump_file
, info
, i
);
3061 fprintf (dump_file
, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
3062 "]: time_benefit: %i, size: %i\n",
3063 plats
->aggs_by_ref
? "ref " : "",
3065 val
->local_time_benefit
, val
->local_size_cost
);
3073 for (i
= 0; i
< count
; i
++)
3074 vec_free (known_aggs
[i
].items
);
3076 known_csts
.release ();
3077 known_contexts
.release ();
3078 known_aggs
.release ();
3079 known_aggs_ptrs
.release ();
3083 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
3084 topological sort of values. */
3086 template <typename valtype
>
3088 value_topo_info
<valtype
>::add_val (ipcp_value
<valtype
> *cur_val
)
3090 ipcp_value_source
<valtype
> *src
;
3096 cur_val
->dfs
= dfs_counter
;
3097 cur_val
->low_link
= dfs_counter
;
3099 cur_val
->topo_next
= stack
;
3101 cur_val
->on_stack
= true;
3103 for (src
= cur_val
->sources
; src
; src
= src
->next
)
3106 if (src
->val
->dfs
== 0)
3109 if (src
->val
->low_link
< cur_val
->low_link
)
3110 cur_val
->low_link
= src
->val
->low_link
;
3112 else if (src
->val
->on_stack
3113 && src
->val
->dfs
< cur_val
->low_link
)
3114 cur_val
->low_link
= src
->val
->dfs
;
3117 if (cur_val
->dfs
== cur_val
->low_link
)
3119 ipcp_value
<valtype
> *v
, *scc_list
= NULL
;
3124 stack
= v
->topo_next
;
3125 v
->on_stack
= false;
3127 v
->scc_next
= scc_list
;
3130 while (v
!= cur_val
);
3132 cur_val
->topo_next
= values_topo
;
3133 values_topo
= cur_val
;
3137 /* Add all values in lattices associated with NODE to the topological sort if
3138 they are not there yet. */
3141 add_all_node_vals_to_toposort (cgraph_node
*node
, ipa_topo_info
*topo
)
3143 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3144 int i
, count
= ipa_get_param_count (info
);
3146 for (i
= 0; i
< count
; i
++)
3148 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
3149 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
3150 struct ipcp_agg_lattice
*aglat
;
3154 ipcp_value
<tree
> *val
;
3155 for (val
= lat
->values
; val
; val
= val
->next
)
3156 topo
->constants
.add_val (val
);
3159 if (!plats
->aggs_bottom
)
3160 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3163 ipcp_value
<tree
> *val
;
3164 for (val
= aglat
->values
; val
; val
= val
->next
)
3165 topo
->constants
.add_val (val
);
3168 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
3169 if (!ctxlat
->bottom
)
3171 ipcp_value
<ipa_polymorphic_call_context
> *ctxval
;
3172 for (ctxval
= ctxlat
->values
; ctxval
; ctxval
= ctxval
->next
)
3173 topo
->contexts
.add_val (ctxval
);
3178 /* One pass of constants propagation along the call graph edges, from callers
3179 to callees (requires topological ordering in TOPO), iterate over strongly
3180 connected components. */
3183 propagate_constants_topo (struct ipa_topo_info
*topo
)
3187 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
3190 struct cgraph_node
*v
, *node
= topo
->order
[i
];
3191 vec
<cgraph_node
*> cycle_nodes
= ipa_get_nodes_in_cycle (node
);
3193 /* First, iteratively propagate within the strongly connected component
3194 until all lattices stabilize. */
3195 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
3196 if (v
->has_gimple_body_p ())
3197 push_node_to_stack (topo
, v
);
3199 v
= pop_node_from_stack (topo
);
3202 struct cgraph_edge
*cs
;
3204 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
3205 if (ipa_edge_within_scc (cs
))
3207 IPA_NODE_REF (v
)->node_within_scc
= true;
3208 if (propagate_constants_across_call (cs
))
3209 push_node_to_stack (topo
, cs
->callee
->function_symbol ());
3211 v
= pop_node_from_stack (topo
);
3214 /* Afterwards, propagate along edges leading out of the SCC, calculates
3215 the local effects of the discovered constants and all valid values to
3216 their topological sort. */
3217 FOR_EACH_VEC_ELT (cycle_nodes
, j
, v
)
3218 if (v
->has_gimple_body_p ())
3220 struct cgraph_edge
*cs
;
3222 estimate_local_effects (v
);
3223 add_all_node_vals_to_toposort (v
, topo
);
3224 for (cs
= v
->callees
; cs
; cs
= cs
->next_callee
)
3225 if (!ipa_edge_within_scc (cs
))
3226 propagate_constants_across_call (cs
);
3228 cycle_nodes
.release ();
3233 /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return
3234 the bigger one if otherwise. */
3237 safe_add (int a
, int b
)
3239 if (a
> INT_MAX
/2 || b
> INT_MAX
/2)
3240 return a
> b
? a
: b
;
3246 /* Propagate the estimated effects of individual values along the topological
3247 from the dependent values to those they depend on. */
3249 template <typename valtype
>
3251 value_topo_info
<valtype
>::propagate_effects ()
3253 ipcp_value
<valtype
> *base
;
3255 for (base
= values_topo
; base
; base
= base
->topo_next
)
3257 ipcp_value_source
<valtype
> *src
;
3258 ipcp_value
<valtype
> *val
;
3259 int time
= 0, size
= 0;
3261 for (val
= base
; val
; val
= val
->scc_next
)
3263 time
= safe_add (time
,
3264 val
->local_time_benefit
+ val
->prop_time_benefit
);
3265 size
= safe_add (size
, val
->local_size_cost
+ val
->prop_size_cost
);
3268 for (val
= base
; val
; val
= val
->scc_next
)
3269 for (src
= val
->sources
; src
; src
= src
->next
)
3271 && src
->cs
->maybe_hot_p ())
3273 src
->val
->prop_time_benefit
= safe_add (time
,
3274 src
->val
->prop_time_benefit
);
3275 src
->val
->prop_size_cost
= safe_add (size
,
3276 src
->val
->prop_size_cost
);
3282 /* Propagate constants, polymorphic contexts and their effects from the
3283 summaries interprocedurally. */
3286 ipcp_propagate_stage (struct ipa_topo_info
*topo
)
3288 struct cgraph_node
*node
;
3291 fprintf (dump_file
, "\n Propagating constants:\n\n");
3293 max_count
= profile_count::uninitialized ();
3295 FOR_EACH_DEFINED_FUNCTION (node
)
3297 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3299 determine_versionability (node
, info
);
3300 if (node
->has_gimple_body_p ())
3302 info
->lattices
= XCNEWVEC (struct ipcp_param_lattices
,
3303 ipa_get_param_count (info
));
3304 initialize_node_lattices (node
);
3306 if (node
->definition
&& !node
->alias
)
3307 overall_size
+= ipa_fn_summaries
->get (node
)->self_size
;
3308 max_count
= max_count
.max (node
->count
.ipa ());
3311 max_new_size
= overall_size
;
3312 if (max_new_size
< PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
))
3313 max_new_size
= PARAM_VALUE (PARAM_LARGE_UNIT_INSNS
);
3314 max_new_size
+= max_new_size
* PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH
) / 100 + 1;
3317 fprintf (dump_file
, "\noverall_size: %li, max_new_size: %li\n",
3318 overall_size
, max_new_size
);
3320 propagate_constants_topo (topo
);
3322 ipcp_verify_propagated_values ();
3323 topo
->constants
.propagate_effects ();
3324 topo
->contexts
.propagate_effects ();
3328 fprintf (dump_file
, "\nIPA lattices after all propagation:\n");
3329 print_all_lattices (dump_file
, (dump_flags
& TDF_DETAILS
), true);
3333 /* Discover newly direct outgoing edges from NODE which is a new clone with
3334 known KNOWN_CSTS and make them direct. */
3337 ipcp_discover_new_direct_edges (struct cgraph_node
*node
,
3338 vec
<tree
> known_csts
,
3339 vec
<ipa_polymorphic_call_context
>
3341 struct ipa_agg_replacement_value
*aggvals
)
3343 struct cgraph_edge
*ie
, *next_ie
;
3346 for (ie
= node
->indirect_calls
; ie
; ie
= next_ie
)
3351 next_ie
= ie
->next_callee
;
3352 target
= ipa_get_indirect_edge_target_1 (ie
, known_csts
, known_contexts
,
3353 vNULL
, aggvals
, &speculative
);
3356 bool agg_contents
= ie
->indirect_info
->agg_contents
;
3357 bool polymorphic
= ie
->indirect_info
->polymorphic
;
3358 int param_index
= ie
->indirect_info
->param_index
;
3359 struct cgraph_edge
*cs
= ipa_make_edge_direct_to_target (ie
, target
,
3363 if (cs
&& !agg_contents
&& !polymorphic
)
3365 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3366 int c
= ipa_get_controlled_uses (info
, param_index
);
3367 if (c
!= IPA_UNDESCRIBED_USE
)
3369 struct ipa_ref
*to_del
;
3372 ipa_set_controlled_uses (info
, param_index
, c
);
3373 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3374 fprintf (dump_file
, " controlled uses count of param "
3375 "%i bumped down to %i\n", param_index
, c
);
3377 && (to_del
= node
->find_reference (cs
->callee
, NULL
, 0)))
3379 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3380 fprintf (dump_file
, " and even removing its "
3381 "cloning-created reference\n");
3382 to_del
->remove_reference ();
3388 /* Turning calls to direct calls will improve overall summary. */
3390 ipa_update_overall_fn_summary (node
);
3393 /* Vector of pointers which for linked lists of clones of an original crgaph
3396 static vec
<cgraph_edge
*> next_edge_clone
;
3397 static vec
<cgraph_edge
*> prev_edge_clone
;
3400 grow_edge_clone_vectors (void)
3402 if (next_edge_clone
.length ()
3403 <= (unsigned) symtab
->edges_max_uid
)
3404 next_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
3405 if (prev_edge_clone
.length ()
3406 <= (unsigned) symtab
->edges_max_uid
)
3407 prev_edge_clone
.safe_grow_cleared (symtab
->edges_max_uid
+ 1);
3410 /* Edge duplication hook to grow the appropriate linked list in
3414 ipcp_edge_duplication_hook (struct cgraph_edge
*src
, struct cgraph_edge
*dst
,
3417 grow_edge_clone_vectors ();
3419 struct cgraph_edge
*old_next
= next_edge_clone
[src
->uid
];
3421 prev_edge_clone
[old_next
->uid
] = dst
;
3422 prev_edge_clone
[dst
->uid
] = src
;
3424 next_edge_clone
[dst
->uid
] = old_next
;
3425 next_edge_clone
[src
->uid
] = dst
;
3428 /* Hook that is called by cgraph.c when an edge is removed. */
3431 ipcp_edge_removal_hook (struct cgraph_edge
*cs
, void *)
3433 grow_edge_clone_vectors ();
3435 struct cgraph_edge
*prev
= prev_edge_clone
[cs
->uid
];
3436 struct cgraph_edge
*next
= next_edge_clone
[cs
->uid
];
3438 next_edge_clone
[prev
->uid
] = next
;
3440 prev_edge_clone
[next
->uid
] = prev
;
3443 /* See if NODE is a clone with a known aggregate value at a given OFFSET of a
3444 parameter with the given INDEX. */
3447 get_clone_agg_value (struct cgraph_node
*node
, HOST_WIDE_INT offset
,
3450 struct ipa_agg_replacement_value
*aggval
;
3452 aggval
= ipa_get_agg_replacements_for_node (node
);
3455 if (aggval
->offset
== offset
3456 && aggval
->index
== index
)
3457 return aggval
->value
;
3458 aggval
= aggval
->next
;
3463 /* Return true is NODE is DEST or its clone for all contexts. */
3466 same_node_or_its_all_contexts_clone_p (cgraph_node
*node
, cgraph_node
*dest
)
3471 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3472 return info
->is_all_contexts_clone
&& info
->ipcp_orig_node
== dest
;
3475 /* Return true if edge CS does bring about the value described by SRC to node
3476 DEST or its clone for all contexts. */
3479 cgraph_edge_brings_value_p (cgraph_edge
*cs
, ipcp_value_source
<tree
> *src
,
3482 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3483 enum availability availability
;
3484 cgraph_node
*real_dest
= cs
->callee
->function_symbol (&availability
);
3486 if (!same_node_or_its_all_contexts_clone_p (real_dest
, dest
)
3487 || availability
<= AVAIL_INTERPOSABLE
3488 || caller_info
->node_dead
)
3493 if (caller_info
->ipcp_orig_node
)
3496 if (src
->offset
== -1)
3497 t
= caller_info
->known_csts
[src
->index
];
3499 t
= get_clone_agg_value (cs
->caller
, src
->offset
, src
->index
);
3500 return (t
!= NULL_TREE
3501 && values_equal_for_ipcp_p (src
->val
->value
, t
));
3505 struct ipcp_agg_lattice
*aglat
;
3506 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (caller_info
,
3508 if (src
->offset
== -1)
3509 return (plats
->itself
.is_single_const ()
3510 && values_equal_for_ipcp_p (src
->val
->value
,
3511 plats
->itself
.values
->value
));
3514 if (plats
->aggs_bottom
|| plats
->aggs_contain_variable
)
3516 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
3517 if (aglat
->offset
== src
->offset
)
3518 return (aglat
->is_single_const ()
3519 && values_equal_for_ipcp_p (src
->val
->value
,
3520 aglat
->values
->value
));
3526 /* Return true if edge CS does bring about the value described by SRC to node
3527 DEST or its clone for all contexts. */
3530 cgraph_edge_brings_value_p (cgraph_edge
*cs
,
3531 ipcp_value_source
<ipa_polymorphic_call_context
> *src
,
3534 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
3535 cgraph_node
*real_dest
= cs
->callee
->function_symbol ();
3537 if (!same_node_or_its_all_contexts_clone_p (real_dest
, dest
)
3538 || caller_info
->node_dead
)
3543 if (caller_info
->ipcp_orig_node
)
3544 return (caller_info
->known_contexts
.length () > (unsigned) src
->index
)
3545 && values_equal_for_ipcp_p (src
->val
->value
,
3546 caller_info
->known_contexts
[src
->index
]);
3548 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (caller_info
,
3550 return plats
->ctxlat
.is_single_const ()
3551 && values_equal_for_ipcp_p (src
->val
->value
,
3552 plats
->ctxlat
.values
->value
);
3555 /* Get the next clone in the linked list of clones of an edge. */
3557 static inline struct cgraph_edge
*
3558 get_next_cgraph_edge_clone (struct cgraph_edge
*cs
)
3560 return next_edge_clone
[cs
->uid
];
3563 /* Given VAL that is intended for DEST, iterate over all its sources and if
3564 they still hold, add their edge frequency and their number into *FREQUENCY
3565 and *CALLER_COUNT respectively. */
3567 template <typename valtype
>
3569 get_info_about_necessary_edges (ipcp_value
<valtype
> *val
, cgraph_node
*dest
,
3571 profile_count
*count_sum
, int *caller_count
)
3573 ipcp_value_source
<valtype
> *src
;
3574 int freq
= 0, count
= 0;
3575 profile_count cnt
= profile_count::zero ();
3578 for (src
= val
->sources
; src
; src
= src
->next
)
3580 struct cgraph_edge
*cs
= src
->cs
;
3583 if (cgraph_edge_brings_value_p (cs
, src
, dest
))
3586 freq
+= cs
->frequency ();
3587 if (cs
->count
.ipa ().initialized_p ())
3588 cnt
+= cs
->count
.ipa ();
3589 hot
|= cs
->maybe_hot_p ();
3591 cs
= get_next_cgraph_edge_clone (cs
);
3597 *caller_count
= count
;
3601 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
3602 is assumed their number is known and equal to CALLER_COUNT. */
3604 template <typename valtype
>
3605 static vec
<cgraph_edge
*>
3606 gather_edges_for_value (ipcp_value
<valtype
> *val
, cgraph_node
*dest
,
3609 ipcp_value_source
<valtype
> *src
;
3610 vec
<cgraph_edge
*> ret
;
3612 ret
.create (caller_count
);
3613 for (src
= val
->sources
; src
; src
= src
->next
)
3615 struct cgraph_edge
*cs
= src
->cs
;
3618 if (cgraph_edge_brings_value_p (cs
, src
, dest
))
3619 ret
.quick_push (cs
);
3620 cs
= get_next_cgraph_edge_clone (cs
);
3627 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
3628 Return it or NULL if for some reason it cannot be created. */
3630 static struct ipa_replace_map
*
3631 get_replacement_map (struct ipa_node_params
*info
, tree value
, int parm_num
)
3633 struct ipa_replace_map
*replace_map
;
3636 replace_map
= ggc_alloc
<ipa_replace_map
> ();
3639 fprintf (dump_file
, " replacing ");
3640 ipa_dump_param (dump_file
, info
, parm_num
);
3642 fprintf (dump_file
, " with const ");
3643 print_generic_expr (dump_file
, value
);
3644 fprintf (dump_file
, "\n");
3646 replace_map
->old_tree
= NULL
;
3647 replace_map
->parm_num
= parm_num
;
3648 replace_map
->new_tree
= value
;
3649 replace_map
->replace_p
= true;
3650 replace_map
->ref_p
= false;
3655 /* Dump new profiling counts */
3658 dump_profile_updates (struct cgraph_node
*orig_node
,
3659 struct cgraph_node
*new_node
)
3661 struct cgraph_edge
*cs
;
3663 fprintf (dump_file
, " setting count of the specialized node to ");
3664 new_node
->count
.dump (dump_file
);
3665 fprintf (dump_file
, "\n");
3666 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3668 fprintf (dump_file
, " edge to %s has count ",
3669 cs
->callee
->name ());
3670 cs
->count
.dump (dump_file
);
3671 fprintf (dump_file
, "\n");
3674 fprintf (dump_file
, " setting count of the original node to ");
3675 orig_node
->count
.dump (dump_file
);
3676 fprintf (dump_file
, "\n");
3677 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3679 fprintf (dump_file
, " edge to %s is left with ",
3680 cs
->callee
->name ());
3681 cs
->count
.dump (dump_file
);
3682 fprintf (dump_file
, "\n");
3686 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
3687 their profile information to reflect this. */
3690 update_profiling_info (struct cgraph_node
*orig_node
,
3691 struct cgraph_node
*new_node
)
3693 struct cgraph_edge
*cs
;
3694 struct caller_statistics stats
;
3695 profile_count new_sum
, orig_sum
;
3696 profile_count remainder
, orig_node_count
= orig_node
->count
;
3698 if (!(orig_node_count
.ipa () > profile_count::zero ()))
3701 init_caller_stats (&stats
);
3702 orig_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
3704 orig_sum
= stats
.count_sum
;
3705 init_caller_stats (&stats
);
3706 new_node
->call_for_symbol_thunks_and_aliases (gather_caller_stats
, &stats
,
3708 new_sum
= stats
.count_sum
;
3710 if (orig_node_count
< orig_sum
+ new_sum
)
3714 fprintf (dump_file
, " Problem: node %s has too low count ",
3715 orig_node
->dump_name ());
3716 orig_node_count
.dump (dump_file
);
3717 fprintf (dump_file
, "while the sum of incoming count is ");
3718 (orig_sum
+ new_sum
).dump (dump_file
);
3719 fprintf (dump_file
, "\n");
3722 orig_node_count
= (orig_sum
+ new_sum
).apply_scale (12, 10);
3725 fprintf (dump_file
, " proceeding by pretending it was ");
3726 orig_node_count
.dump (dump_file
);
3727 fprintf (dump_file
, "\n");
3731 remainder
= orig_node_count
.combine_with_ipa_count (orig_node_count
.ipa ()
3733 new_sum
= orig_node_count
.combine_with_ipa_count (new_sum
);
3734 orig_node
->count
= remainder
;
3736 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3737 cs
->count
= cs
->count
.apply_scale (new_sum
, orig_node_count
);
3739 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3740 cs
->count
= cs
->count
.apply_scale (remainder
, orig_node_count
);
3743 dump_profile_updates (orig_node
, new_node
);
3746 /* Update the respective profile of specialized NEW_NODE and the original
3747 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
3748 have been redirected to the specialized version. */
3751 update_specialized_profile (struct cgraph_node
*new_node
,
3752 struct cgraph_node
*orig_node
,
3753 profile_count redirected_sum
)
3755 struct cgraph_edge
*cs
;
3756 profile_count new_node_count
, orig_node_count
= orig_node
->count
;
3760 fprintf (dump_file
, " the sum of counts of redirected edges is ");
3761 redirected_sum
.dump (dump_file
);
3762 fprintf (dump_file
, "\n");
3764 if (!(orig_node_count
> profile_count::zero ()))
3767 gcc_assert (orig_node_count
>= redirected_sum
);
3769 new_node_count
= new_node
->count
;
3770 new_node
->count
+= redirected_sum
;
3771 orig_node
->count
-= redirected_sum
;
3773 for (cs
= new_node
->callees
; cs
; cs
= cs
->next_callee
)
3774 cs
->count
+= cs
->count
.apply_scale (redirected_sum
, new_node_count
);
3776 for (cs
= orig_node
->callees
; cs
; cs
= cs
->next_callee
)
3778 profile_count dec
= cs
->count
.apply_scale (redirected_sum
,
3784 dump_profile_updates (orig_node
, new_node
);
3787 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
3788 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
3789 redirect all edges in CALLERS to it. */
3791 static struct cgraph_node
*
3792 create_specialized_node (struct cgraph_node
*node
,
3793 vec
<tree
> known_csts
,
3794 vec
<ipa_polymorphic_call_context
> known_contexts
,
3795 struct ipa_agg_replacement_value
*aggvals
,
3796 vec
<cgraph_edge
*> callers
)
3798 struct ipa_node_params
*new_info
, *info
= IPA_NODE_REF (node
);
3799 vec
<ipa_replace_map
*, va_gc
> *replace_trees
= NULL
;
3800 struct ipa_agg_replacement_value
*av
;
3801 struct cgraph_node
*new_node
;
3802 int i
, count
= ipa_get_param_count (info
);
3803 bitmap args_to_skip
;
3805 gcc_assert (!info
->ipcp_orig_node
);
3807 if (node
->local
.can_change_signature
)
3809 args_to_skip
= BITMAP_GGC_ALLOC ();
3810 for (i
= 0; i
< count
; i
++)
3812 tree t
= known_csts
[i
];
3814 if (t
|| !ipa_is_param_used (info
, i
))
3815 bitmap_set_bit (args_to_skip
, i
);
3820 args_to_skip
= NULL
;
3821 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3822 fprintf (dump_file
, " cannot change function signature\n");
3825 for (i
= 0; i
< count
; i
++)
3827 tree t
= known_csts
[i
];
3830 struct ipa_replace_map
*replace_map
;
3832 gcc_checking_assert (TREE_CODE (t
) != TREE_BINFO
);
3833 replace_map
= get_replacement_map (info
, t
, i
);
3835 vec_safe_push (replace_trees
, replace_map
);
3839 new_node
= node
->create_virtual_clone (callers
, replace_trees
,
3840 args_to_skip
, "constprop");
3841 ipa_set_node_agg_value_chain (new_node
, aggvals
);
3842 for (av
= aggvals
; av
; av
= av
->next
)
3843 new_node
->maybe_create_reference (av
->value
, NULL
);
3845 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3847 fprintf (dump_file
, " the new node is %s.\n", new_node
->dump_name ());
3848 if (known_contexts
.exists ())
3850 for (i
= 0; i
< count
; i
++)
3851 if (!known_contexts
[i
].useless_p ())
3853 fprintf (dump_file
, " known ctx %i is ", i
);
3854 known_contexts
[i
].dump (dump_file
);
3858 ipa_dump_agg_replacement_values (dump_file
, aggvals
);
3860 ipa_check_create_node_params ();
3861 update_profiling_info (node
, new_node
);
3862 new_info
= IPA_NODE_REF (new_node
);
3863 new_info
->ipcp_orig_node
= node
;
3864 new_info
->known_csts
= known_csts
;
3865 new_info
->known_contexts
= known_contexts
;
3867 ipcp_discover_new_direct_edges (new_node
, known_csts
, known_contexts
, aggvals
);
3873 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
3874 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
3877 find_more_scalar_values_for_callers_subset (struct cgraph_node
*node
,
3878 vec
<tree
> known_csts
,
3879 vec
<cgraph_edge
*> callers
)
3881 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
3882 int i
, count
= ipa_get_param_count (info
);
3884 for (i
= 0; i
< count
; i
++)
3886 struct cgraph_edge
*cs
;
3887 tree newval
= NULL_TREE
;
3890 tree type
= ipa_get_type (info
, i
);
3892 if (ipa_get_scalar_lat (info
, i
)->bottom
|| known_csts
[i
])
3895 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3897 struct ipa_jump_func
*jump_func
;
3900 if (i
>= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
))
3902 && call_passes_through_thunk_p (cs
))
3903 || (!cs
->callee
->instrumentation_clone
3904 && cs
->callee
->function_symbol ()->instrumentation_clone
))
3909 jump_func
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), i
);
3910 t
= ipa_value_from_jfunc (IPA_NODE_REF (cs
->caller
), jump_func
, type
);
3913 && !values_equal_for_ipcp_p (t
, newval
))
3914 || (!first
&& !newval
))
3926 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3928 fprintf (dump_file
, " adding an extra known scalar value ");
3929 print_ipcp_constant_value (dump_file
, newval
);
3930 fprintf (dump_file
, " for ");
3931 ipa_dump_param (dump_file
, info
, i
);
3932 fprintf (dump_file
, "\n");
3935 known_csts
[i
] = newval
;
3940 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
3941 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
3945 find_more_contexts_for_caller_subset (cgraph_node
*node
,
3946 vec
<ipa_polymorphic_call_context
>
3948 vec
<cgraph_edge
*> callers
)
3950 ipa_node_params
*info
= IPA_NODE_REF (node
);
3951 int i
, count
= ipa_get_param_count (info
);
3953 for (i
= 0; i
< count
; i
++)
3957 if (ipa_get_poly_ctx_lat (info
, i
)->bottom
3958 || (known_contexts
->exists ()
3959 && !(*known_contexts
)[i
].useless_p ()))
3962 ipa_polymorphic_call_context newval
;
3966 FOR_EACH_VEC_ELT (callers
, j
, cs
)
3968 if (i
>= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
)))
3970 ipa_jump_func
*jfunc
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
),
3972 ipa_polymorphic_call_context ctx
;
3973 ctx
= ipa_context_from_jfunc (IPA_NODE_REF (cs
->caller
), cs
, i
,
3981 newval
.meet_with (ctx
);
3982 if (newval
.useless_p ())
3986 if (!newval
.useless_p ())
3988 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3990 fprintf (dump_file
, " adding an extra known polymorphic "
3992 print_ipcp_constant_value (dump_file
, newval
);
3993 fprintf (dump_file
, " for ");
3994 ipa_dump_param (dump_file
, info
, i
);
3995 fprintf (dump_file
, "\n");
3998 if (!known_contexts
->exists ())
3999 known_contexts
->safe_grow_cleared (ipa_get_param_count (info
));
4000 (*known_contexts
)[i
] = newval
;
4006 /* Go through PLATS and create a vector of values consisting of values and
4007 offsets (minus OFFSET) of lattices that contain only a single value. */
4009 static vec
<ipa_agg_jf_item
>
4010 copy_plats_to_inter (struct ipcp_param_lattices
*plats
, HOST_WIDE_INT offset
)
4012 vec
<ipa_agg_jf_item
> res
= vNULL
;
4014 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
4017 for (struct ipcp_agg_lattice
*aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
4018 if (aglat
->is_single_const ())
4020 struct ipa_agg_jf_item ti
;
4021 ti
.offset
= aglat
->offset
- offset
;
4022 ti
.value
= aglat
->values
->value
;
4028 /* Intersect all values in INTER with single value lattices in PLATS (while
4029 subtracting OFFSET). */
4032 intersect_with_plats (struct ipcp_param_lattices
*plats
,
4033 vec
<ipa_agg_jf_item
> *inter
,
4034 HOST_WIDE_INT offset
)
4036 struct ipcp_agg_lattice
*aglat
;
4037 struct ipa_agg_jf_item
*item
;
4040 if (!plats
->aggs
|| plats
->aggs_contain_variable
|| plats
->aggs_bottom
)
4046 aglat
= plats
->aggs
;
4047 FOR_EACH_VEC_ELT (*inter
, k
, item
)
4054 if (aglat
->offset
- offset
> item
->offset
)
4056 if (aglat
->offset
- offset
== item
->offset
)
4058 gcc_checking_assert (item
->value
);
4059 if (values_equal_for_ipcp_p (item
->value
, aglat
->values
->value
))
4063 aglat
= aglat
->next
;
4066 item
->value
= NULL_TREE
;
4070 /* Copy aggregate replacement values of NODE (which is an IPA-CP clone) to the
4071 vector result while subtracting OFFSET from the individual value offsets. */
4073 static vec
<ipa_agg_jf_item
>
4074 agg_replacements_to_vector (struct cgraph_node
*node
, int index
,
4075 HOST_WIDE_INT offset
)
4077 struct ipa_agg_replacement_value
*av
;
4078 vec
<ipa_agg_jf_item
> res
= vNULL
;
4080 for (av
= ipa_get_agg_replacements_for_node (node
); av
; av
= av
->next
)
4081 if (av
->index
== index
4082 && (av
->offset
- offset
) >= 0)
4084 struct ipa_agg_jf_item item
;
4085 gcc_checking_assert (av
->value
);
4086 item
.offset
= av
->offset
- offset
;
4087 item
.value
= av
->value
;
4088 res
.safe_push (item
);
4094 /* Intersect all values in INTER with those that we have already scheduled to
4095 be replaced in parameter number INDEX of NODE, which is an IPA-CP clone
4096 (while subtracting OFFSET). */
4099 intersect_with_agg_replacements (struct cgraph_node
*node
, int index
,
4100 vec
<ipa_agg_jf_item
> *inter
,
4101 HOST_WIDE_INT offset
)
4103 struct ipa_agg_replacement_value
*srcvals
;
4104 struct ipa_agg_jf_item
*item
;
4107 srcvals
= ipa_get_agg_replacements_for_node (node
);
4114 FOR_EACH_VEC_ELT (*inter
, i
, item
)
4116 struct ipa_agg_replacement_value
*av
;
4120 for (av
= srcvals
; av
; av
= av
->next
)
4122 gcc_checking_assert (av
->value
);
4123 if (av
->index
== index
4124 && av
->offset
- offset
== item
->offset
)
4126 if (values_equal_for_ipcp_p (item
->value
, av
->value
))
4132 item
->value
= NULL_TREE
;
4136 /* Intersect values in INTER with aggregate values that come along edge CS to
4137 parameter number INDEX and return it. If INTER does not actually exist yet,
4138 copy all incoming values to it. If we determine we ended up with no values
4139 whatsoever, return a released vector. */
4141 static vec
<ipa_agg_jf_item
>
4142 intersect_aggregates_with_edge (struct cgraph_edge
*cs
, int index
,
4143 vec
<ipa_agg_jf_item
> inter
)
4145 struct ipa_jump_func
*jfunc
;
4146 jfunc
= ipa_get_ith_jump_func (IPA_EDGE_REF (cs
), index
);
4147 if (jfunc
->type
== IPA_JF_PASS_THROUGH
4148 && ipa_get_jf_pass_through_operation (jfunc
) == NOP_EXPR
)
4150 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
4151 int src_idx
= ipa_get_jf_pass_through_formal_id (jfunc
);
4153 if (caller_info
->ipcp_orig_node
)
4155 struct cgraph_node
*orig_node
= caller_info
->ipcp_orig_node
;
4156 struct ipcp_param_lattices
*orig_plats
;
4157 orig_plats
= ipa_get_parm_lattices (IPA_NODE_REF (orig_node
),
4159 if (agg_pass_through_permissible_p (orig_plats
, jfunc
))
4161 if (!inter
.exists ())
4162 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, 0);
4164 intersect_with_agg_replacements (cs
->caller
, src_idx
,
4175 struct ipcp_param_lattices
*src_plats
;
4176 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
4177 if (agg_pass_through_permissible_p (src_plats
, jfunc
))
4179 /* Currently we do not produce clobber aggregate jump
4180 functions, adjust when we do. */
4181 gcc_checking_assert (!jfunc
->agg
.items
);
4182 if (!inter
.exists ())
4183 inter
= copy_plats_to_inter (src_plats
, 0);
4185 intersect_with_plats (src_plats
, &inter
, 0);
4194 else if (jfunc
->type
== IPA_JF_ANCESTOR
4195 && ipa_get_jf_ancestor_agg_preserved (jfunc
))
4197 struct ipa_node_params
*caller_info
= IPA_NODE_REF (cs
->caller
);
4198 int src_idx
= ipa_get_jf_ancestor_formal_id (jfunc
);
4199 struct ipcp_param_lattices
*src_plats
;
4200 HOST_WIDE_INT delta
= ipa_get_jf_ancestor_offset (jfunc
);
4202 if (caller_info
->ipcp_orig_node
)
4204 if (!inter
.exists ())
4205 inter
= agg_replacements_to_vector (cs
->caller
, src_idx
, delta
);
4207 intersect_with_agg_replacements (cs
->caller
, src_idx
, &inter
,
4212 src_plats
= ipa_get_parm_lattices (caller_info
, src_idx
);
4213 /* Currently we do not produce clobber aggregate jump
4214 functions, adjust when we do. */
4215 gcc_checking_assert (!src_plats
->aggs
|| !jfunc
->agg
.items
);
4216 if (!inter
.exists ())
4217 inter
= copy_plats_to_inter (src_plats
, delta
);
4219 intersect_with_plats (src_plats
, &inter
, delta
);
4222 else if (jfunc
->agg
.items
)
4224 struct ipa_agg_jf_item
*item
;
4227 if (!inter
.exists ())
4228 for (unsigned i
= 0; i
< jfunc
->agg
.items
->length (); i
++)
4229 inter
.safe_push ((*jfunc
->agg
.items
)[i
]);
4231 FOR_EACH_VEC_ELT (inter
, k
, item
)
4239 while ((unsigned) l
< jfunc
->agg
.items
->length ())
4241 struct ipa_agg_jf_item
*ti
;
4242 ti
= &(*jfunc
->agg
.items
)[l
];
4243 if (ti
->offset
> item
->offset
)
4245 if (ti
->offset
== item
->offset
)
4247 gcc_checking_assert (ti
->value
);
4248 if (values_equal_for_ipcp_p (item
->value
,
4262 return vec
<ipa_agg_jf_item
>();
4267 /* Look at edges in CALLERS and collect all known aggregate values that arrive
4268 from all of them. */
4270 static struct ipa_agg_replacement_value
*
4271 find_aggregate_values_for_callers_subset (struct cgraph_node
*node
,
4272 vec
<cgraph_edge
*> callers
)
4274 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
4275 struct ipa_agg_replacement_value
*res
;
4276 struct ipa_agg_replacement_value
**tail
= &res
;
4277 struct cgraph_edge
*cs
;
4278 int i
, j
, count
= ipa_get_param_count (dest_info
);
4280 FOR_EACH_VEC_ELT (callers
, j
, cs
)
4282 int c
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
4287 for (i
= 0; i
< count
; i
++)
4289 struct cgraph_edge
*cs
;
4290 vec
<ipa_agg_jf_item
> inter
= vNULL
;
4291 struct ipa_agg_jf_item
*item
;
4292 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (dest_info
, i
);
4295 /* Among other things, the following check should deal with all by_ref
4297 if (plats
->aggs_bottom
)
4300 FOR_EACH_VEC_ELT (callers
, j
, cs
)
4302 inter
= intersect_aggregates_with_edge (cs
, i
, inter
);
4304 if (!inter
.exists ())
4308 FOR_EACH_VEC_ELT (inter
, j
, item
)
4310 struct ipa_agg_replacement_value
*v
;
4315 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
4317 v
->offset
= item
->offset
;
4318 v
->value
= item
->value
;
4319 v
->by_ref
= plats
->aggs_by_ref
;
4325 if (inter
.exists ())
4332 /* Turn KNOWN_AGGS into a list of aggregate replacement values. */
4334 static struct ipa_agg_replacement_value
*
4335 known_aggs_to_agg_replacement_list (vec
<ipa_agg_jump_function
> known_aggs
)
4337 struct ipa_agg_replacement_value
*res
;
4338 struct ipa_agg_replacement_value
**tail
= &res
;
4339 struct ipa_agg_jump_function
*aggjf
;
4340 struct ipa_agg_jf_item
*item
;
4343 FOR_EACH_VEC_ELT (known_aggs
, i
, aggjf
)
4344 FOR_EACH_VEC_SAFE_ELT (aggjf
->items
, j
, item
)
4346 struct ipa_agg_replacement_value
*v
;
4347 v
= ggc_alloc
<ipa_agg_replacement_value
> ();
4349 v
->offset
= item
->offset
;
4350 v
->value
= item
->value
;
4351 v
->by_ref
= aggjf
->by_ref
;
4359 /* Determine whether CS also brings all scalar values that the NODE is
4363 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge
*cs
,
4364 struct cgraph_node
*node
)
4366 struct ipa_node_params
*dest_info
= IPA_NODE_REF (node
);
4367 int count
= ipa_get_param_count (dest_info
);
4368 struct ipa_node_params
*caller_info
;
4369 struct ipa_edge_args
*args
;
4372 caller_info
= IPA_NODE_REF (cs
->caller
);
4373 args
= IPA_EDGE_REF (cs
);
4374 for (i
= 0; i
< count
; i
++)
4376 struct ipa_jump_func
*jump_func
;
4379 val
= dest_info
->known_csts
[i
];
4383 if (i
>= ipa_get_cs_argument_count (args
))
4385 jump_func
= ipa_get_ith_jump_func (args
, i
);
4386 t
= ipa_value_from_jfunc (caller_info
, jump_func
,
4387 ipa_get_type (dest_info
, i
));
4388 if (!t
|| !values_equal_for_ipcp_p (val
, t
))
4394 /* Determine whether CS also brings all aggregate values that NODE is
4397 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge
*cs
,
4398 struct cgraph_node
*node
)
4400 struct ipa_node_params
*orig_caller_info
= IPA_NODE_REF (cs
->caller
);
4401 struct ipa_node_params
*orig_node_info
;
4402 struct ipa_agg_replacement_value
*aggval
;
4405 aggval
= ipa_get_agg_replacements_for_node (node
);
4409 count
= ipa_get_param_count (IPA_NODE_REF (node
));
4410 ec
= ipa_get_cs_argument_count (IPA_EDGE_REF (cs
));
4412 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4413 if (aggval
->index
>= ec
)
4416 orig_node_info
= IPA_NODE_REF (IPA_NODE_REF (node
)->ipcp_orig_node
);
4417 if (orig_caller_info
->ipcp_orig_node
)
4418 orig_caller_info
= IPA_NODE_REF (orig_caller_info
->ipcp_orig_node
);
4420 for (i
= 0; i
< count
; i
++)
4422 static vec
<ipa_agg_jf_item
> values
= vec
<ipa_agg_jf_item
>();
4423 struct ipcp_param_lattices
*plats
;
4424 bool interesting
= false;
4425 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4426 if (aggval
->index
== i
)
4434 plats
= ipa_get_parm_lattices (orig_node_info
, aggval
->index
);
4435 if (plats
->aggs_bottom
)
4438 values
= intersect_aggregates_with_edge (cs
, i
, values
);
4439 if (!values
.exists ())
4442 for (struct ipa_agg_replacement_value
*av
= aggval
; av
; av
= av
->next
)
4443 if (aggval
->index
== i
)
4445 struct ipa_agg_jf_item
*item
;
4448 FOR_EACH_VEC_ELT (values
, j
, item
)
4450 && item
->offset
== av
->offset
4451 && values_equal_for_ipcp_p (item
->value
, av
->value
))
4466 /* Given an original NODE and a VAL for which we have already created a
4467 specialized clone, look whether there are incoming edges that still lead
4468 into the old node but now also bring the requested value and also conform to
4469 all other criteria such that they can be redirected the special node.
4470 This function can therefore redirect the final edge in a SCC. */
4472 template <typename valtype
>
4474 perhaps_add_new_callers (cgraph_node
*node
, ipcp_value
<valtype
> *val
)
4476 ipcp_value_source
<valtype
> *src
;
4477 profile_count redirected_sum
= profile_count::zero ();
4479 for (src
= val
->sources
; src
; src
= src
->next
)
4481 struct cgraph_edge
*cs
= src
->cs
;
4484 if (cgraph_edge_brings_value_p (cs
, src
, node
)
4485 && cgraph_edge_brings_all_scalars_for_node (cs
, val
->spec_node
)
4486 && cgraph_edge_brings_all_agg_vals_for_node (cs
, val
->spec_node
))
4489 fprintf (dump_file
, " - adding an extra caller %s of %s\n",
4490 cs
->caller
->dump_name (),
4491 val
->spec_node
->dump_name ());
4493 cs
->redirect_callee_duplicating_thunks (val
->spec_node
);
4494 val
->spec_node
->expand_all_artificial_thunks ();
4495 if (cs
->count
.ipa ().initialized_p ())
4496 redirected_sum
= redirected_sum
+ cs
->count
.ipa ();
4498 cs
= get_next_cgraph_edge_clone (cs
);
4502 if (redirected_sum
.nonzero_p ())
4503 update_specialized_profile (val
->spec_node
, node
, redirected_sum
);
4506 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
4509 known_contexts_useful_p (vec
<ipa_polymorphic_call_context
> known_contexts
)
4511 ipa_polymorphic_call_context
*ctx
;
4514 FOR_EACH_VEC_ELT (known_contexts
, i
, ctx
)
4515 if (!ctx
->useless_p ())
4520 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
4522 static vec
<ipa_polymorphic_call_context
>
4523 copy_useful_known_contexts (vec
<ipa_polymorphic_call_context
> known_contexts
)
4525 if (known_contexts_useful_p (known_contexts
))
4526 return known_contexts
.copy ();
4531 /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If
4532 non-empty, replace KNOWN_CONTEXTS with its copy too. */
4535 modify_known_vectors_with_val (vec
<tree
> *known_csts
,
4536 vec
<ipa_polymorphic_call_context
> *known_contexts
,
4537 ipcp_value
<tree
> *val
,
4540 *known_csts
= known_csts
->copy ();
4541 *known_contexts
= copy_useful_known_contexts (*known_contexts
);
4542 (*known_csts
)[index
] = val
->value
;
4545 /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the
4546 copy according to VAL and INDEX. */
4549 modify_known_vectors_with_val (vec
<tree
> *known_csts
,
4550 vec
<ipa_polymorphic_call_context
> *known_contexts
,
4551 ipcp_value
<ipa_polymorphic_call_context
> *val
,
4554 *known_csts
= known_csts
->copy ();
4555 *known_contexts
= known_contexts
->copy ();
4556 (*known_contexts
)[index
] = val
->value
;
4559 /* Return true if OFFSET indicates this was not an aggregate value or there is
4560 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
4564 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value
*aggvals
,
4565 int index
, HOST_WIDE_INT offset
, tree value
)
4572 if (aggvals
->index
== index
4573 && aggvals
->offset
== offset
4574 && values_equal_for_ipcp_p (aggvals
->value
, value
))
4576 aggvals
= aggvals
->next
;
4581 /* Return true if offset is minus one because source of a polymorphic contect
4582 cannot be an aggregate value. */
4585 ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value
*,
4586 int , HOST_WIDE_INT offset
,
4587 ipa_polymorphic_call_context
)
4589 return offset
== -1;
4592 /* Decide wheter to create a special version of NODE for value VAL of parameter
4593 at the given INDEX. If OFFSET is -1, the value is for the parameter itself,
4594 otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS,
4595 KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */
4597 template <typename valtype
>
4599 decide_about_value (struct cgraph_node
*node
, int index
, HOST_WIDE_INT offset
,
4600 ipcp_value
<valtype
> *val
, vec
<tree
> known_csts
,
4601 vec
<ipa_polymorphic_call_context
> known_contexts
)
4603 struct ipa_agg_replacement_value
*aggvals
;
4604 int freq_sum
, caller_count
;
4605 profile_count count_sum
;
4606 vec
<cgraph_edge
*> callers
;
4610 perhaps_add_new_callers (node
, val
);
4613 else if (val
->local_size_cost
+ overall_size
> max_new_size
)
4615 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4616 fprintf (dump_file
, " Ignoring candidate value because "
4617 "max_new_size would be reached with %li.\n",
4618 val
->local_size_cost
+ overall_size
);
4621 else if (!get_info_about_necessary_edges (val
, node
, &freq_sum
, &count_sum
,
4625 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4627 fprintf (dump_file
, " - considering value ");
4628 print_ipcp_constant_value (dump_file
, val
->value
);
4629 fprintf (dump_file
, " for ");
4630 ipa_dump_param (dump_file
, IPA_NODE_REF (node
), index
);
4632 fprintf (dump_file
, ", offset: " HOST_WIDE_INT_PRINT_DEC
, offset
);
4633 fprintf (dump_file
, " (caller_count: %i)\n", caller_count
);
4636 if (!good_cloning_opportunity_p (node
, val
->local_time_benefit
,
4637 freq_sum
, count_sum
,
4638 val
->local_size_cost
)
4639 && !good_cloning_opportunity_p (node
,
4640 val
->local_time_benefit
4641 + val
->prop_time_benefit
,
4642 freq_sum
, count_sum
,
4643 val
->local_size_cost
4644 + val
->prop_size_cost
))
4648 fprintf (dump_file
, " Creating a specialized node of %s.\n",
4649 node
->dump_name ());
4651 callers
= gather_edges_for_value (val
, node
, caller_count
);
4653 modify_known_vectors_with_val (&known_csts
, &known_contexts
, val
, index
);
4656 known_csts
= known_csts
.copy ();
4657 known_contexts
= copy_useful_known_contexts (known_contexts
);
4659 find_more_scalar_values_for_callers_subset (node
, known_csts
, callers
);
4660 find_more_contexts_for_caller_subset (node
, &known_contexts
, callers
);
4661 aggvals
= find_aggregate_values_for_callers_subset (node
, callers
);
4662 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals
, index
,
4663 offset
, val
->value
));
4664 val
->spec_node
= create_specialized_node (node
, known_csts
, known_contexts
,
4666 overall_size
+= val
->local_size_cost
;
4668 /* TODO: If for some lattice there is only one other known value
4669 left, make a special node for it too. */
4674 /* Decide whether and what specialized clones of NODE should be created. */
4677 decide_whether_version_node (struct cgraph_node
*node
)
4679 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
4680 int i
, count
= ipa_get_param_count (info
);
4681 vec
<tree
> known_csts
;
4682 vec
<ipa_polymorphic_call_context
> known_contexts
;
4683 vec
<ipa_agg_jump_function
> known_aggs
= vNULL
;
4689 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4690 fprintf (dump_file
, "\nEvaluating opportunities for %s.\n",
4691 node
->dump_name ());
4693 gather_context_independent_values (info
, &known_csts
, &known_contexts
,
4694 info
->do_clone_for_all_contexts
? &known_aggs
4697 for (i
= 0; i
< count
;i
++)
4699 struct ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4700 ipcp_lattice
<tree
> *lat
= &plats
->itself
;
4701 ipcp_lattice
<ipa_polymorphic_call_context
> *ctxlat
= &plats
->ctxlat
;
4706 ipcp_value
<tree
> *val
;
4707 for (val
= lat
->values
; val
; val
= val
->next
)
4708 ret
|= decide_about_value (node
, i
, -1, val
, known_csts
,
4712 if (!plats
->aggs_bottom
)
4714 struct ipcp_agg_lattice
*aglat
;
4715 ipcp_value
<tree
> *val
;
4716 for (aglat
= plats
->aggs
; aglat
; aglat
= aglat
->next
)
4717 if (!aglat
->bottom
&& aglat
->values
4718 /* If the following is false, the one value is in
4720 && (plats
->aggs_contain_variable
4721 || !aglat
->is_single_const ()))
4722 for (val
= aglat
->values
; val
; val
= val
->next
)
4723 ret
|= decide_about_value (node
, i
, aglat
->offset
, val
,
4724 known_csts
, known_contexts
);
4728 && known_contexts
[i
].useless_p ())
4730 ipcp_value
<ipa_polymorphic_call_context
> *val
;
4731 for (val
= ctxlat
->values
; val
; val
= val
->next
)
4732 ret
|= decide_about_value (node
, i
, -1, val
, known_csts
,
4736 info
= IPA_NODE_REF (node
);
4739 if (info
->do_clone_for_all_contexts
)
4741 struct cgraph_node
*clone
;
4742 vec
<cgraph_edge
*> callers
;
4745 fprintf (dump_file
, " - Creating a specialized node of %s "
4746 "for all known contexts.\n", node
->dump_name ());
4748 callers
= node
->collect_callers ();
4750 if (!known_contexts_useful_p (known_contexts
))
4752 known_contexts
.release ();
4753 known_contexts
= vNULL
;
4755 clone
= create_specialized_node (node
, known_csts
, known_contexts
,
4756 known_aggs_to_agg_replacement_list (known_aggs
),
4758 info
= IPA_NODE_REF (node
);
4759 info
->do_clone_for_all_contexts
= false;
4760 IPA_NODE_REF (clone
)->is_all_contexts_clone
= true;
4761 for (i
= 0; i
< count
; i
++)
4762 vec_free (known_aggs
[i
].items
);
4763 known_aggs
.release ();
4768 known_csts
.release ();
4769 known_contexts
.release ();
4775 /* Transitively mark all callees of NODE within the same SCC as not dead. */
4778 spread_undeadness (struct cgraph_node
*node
)
4780 struct cgraph_edge
*cs
;
4782 for (cs
= node
->callees
; cs
; cs
= cs
->next_callee
)
4783 if (ipa_edge_within_scc (cs
))
4785 struct cgraph_node
*callee
;
4786 struct ipa_node_params
*info
;
4788 callee
= cs
->callee
->function_symbol (NULL
);
4789 info
= IPA_NODE_REF (callee
);
4791 if (info
->node_dead
)
4793 info
->node_dead
= 0;
4794 spread_undeadness (callee
);
4799 /* Return true if NODE has a caller from outside of its SCC that is not
4800 dead. Worker callback for cgraph_for_node_and_aliases. */
4803 has_undead_caller_from_outside_scc_p (struct cgraph_node
*node
,
4804 void *data ATTRIBUTE_UNUSED
)
4806 struct cgraph_edge
*cs
;
4808 for (cs
= node
->callers
; cs
; cs
= cs
->next_caller
)
4809 if (cs
->caller
->thunk
.thunk_p
4810 && cs
->caller
->call_for_symbol_thunks_and_aliases
4811 (has_undead_caller_from_outside_scc_p
, NULL
, true))
4813 else if (!ipa_edge_within_scc (cs
)
4814 && !IPA_NODE_REF (cs
->caller
)->node_dead
)
4820 /* Identify nodes within the same SCC as NODE which are no longer needed
4821 because of new clones and will be removed as unreachable. */
4824 identify_dead_nodes (struct cgraph_node
*node
)
4826 struct cgraph_node
*v
;
4827 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4829 && !v
->call_for_symbol_thunks_and_aliases
4830 (has_undead_caller_from_outside_scc_p
, NULL
, true))
4831 IPA_NODE_REF (v
)->node_dead
= 1;
4833 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4834 if (!IPA_NODE_REF (v
)->node_dead
)
4835 spread_undeadness (v
);
4837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4839 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4840 if (IPA_NODE_REF (v
)->node_dead
)
4841 fprintf (dump_file
, " Marking node as dead: %s.\n", v
->dump_name ());
4845 /* The decision stage. Iterate over the topological order of call graph nodes
4846 TOPO and make specialized clones if deemed beneficial. */
4849 ipcp_decision_stage (struct ipa_topo_info
*topo
)
4854 fprintf (dump_file
, "\nIPA decision stage:\n\n");
4856 for (i
= topo
->nnodes
- 1; i
>= 0; i
--)
4858 struct cgraph_node
*node
= topo
->order
[i
];
4859 bool change
= false, iterate
= true;
4863 struct cgraph_node
*v
;
4865 for (v
= node
; v
; v
= ((struct ipa_dfs_info
*) v
->aux
)->next_cycle
)
4866 if (v
->has_gimple_body_p ()
4867 && ipcp_versionable_function_p (v
))
4868 iterate
|= decide_whether_version_node (v
);
4873 identify_dead_nodes (node
);
4877 /* Look up all the bits information that we have discovered and copy it over
4878 to the transformation summary. */
4881 ipcp_store_bits_results (void)
4885 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
4887 ipa_node_params
*info
= IPA_NODE_REF (node
);
4888 bool dumped_sth
= false;
4889 bool found_useful_result
= false;
4891 if (!opt_for_fn (node
->decl
, flag_ipa_bit_cp
))
4894 fprintf (dump_file
, "Not considering %s for ipa bitwise propagation "
4895 "; -fipa-bit-cp: disabled.\n",
4900 if (info
->ipcp_orig_node
)
4901 info
= IPA_NODE_REF (info
->ipcp_orig_node
);
4903 unsigned count
= ipa_get_param_count (info
);
4904 for (unsigned i
= 0; i
< count
; i
++)
4906 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4907 if (plats
->bits_lattice
.constant_p ())
4909 found_useful_result
= true;
4914 if (!found_useful_result
)
4917 ipcp_grow_transformations_if_necessary ();
4918 ipcp_transformation_summary
*ts
= ipcp_get_transformation_summary (node
);
4919 vec_safe_reserve_exact (ts
->bits
, count
);
4921 for (unsigned i
= 0; i
< count
; i
++)
4923 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4926 if (plats
->bits_lattice
.constant_p ())
4928 = ipa_get_ipa_bits_for_value (plats
->bits_lattice
.get_value (),
4929 plats
->bits_lattice
.get_mask ());
4933 ts
->bits
->quick_push (jfbits
);
4934 if (!dump_file
|| !jfbits
)
4938 fprintf (dump_file
, "Propagated bits info for function %s:\n",
4939 node
->dump_name ());
4942 fprintf (dump_file
, " param %i: value = ", i
);
4943 print_hex (jfbits
->value
, dump_file
);
4944 fprintf (dump_file
, ", mask = ");
4945 print_hex (jfbits
->mask
, dump_file
);
4946 fprintf (dump_file
, "\n");
4951 /* Look up all VR information that we have discovered and copy it over
4952 to the transformation summary. */
4955 ipcp_store_vr_results (void)
4959 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
4961 ipa_node_params
*info
= IPA_NODE_REF (node
);
4962 bool found_useful_result
= false;
4964 if (!opt_for_fn (node
->decl
, flag_ipa_vrp
))
4967 fprintf (dump_file
, "Not considering %s for VR discovery "
4968 "and propagate; -fipa-ipa-vrp: disabled.\n",
4973 if (info
->ipcp_orig_node
)
4974 info
= IPA_NODE_REF (info
->ipcp_orig_node
);
4976 unsigned count
= ipa_get_param_count (info
);
4977 for (unsigned i
= 0; i
< count
; i
++)
4979 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4980 if (!plats
->m_value_range
.bottom_p ()
4981 && !plats
->m_value_range
.top_p ())
4983 found_useful_result
= true;
4987 if (!found_useful_result
)
4990 ipcp_grow_transformations_if_necessary ();
4991 ipcp_transformation_summary
*ts
= ipcp_get_transformation_summary (node
);
4992 vec_safe_reserve_exact (ts
->m_vr
, count
);
4994 for (unsigned i
= 0; i
< count
; i
++)
4996 ipcp_param_lattices
*plats
= ipa_get_parm_lattices (info
, i
);
4999 if (!plats
->m_value_range
.bottom_p ()
5000 && !plats
->m_value_range
.top_p ())
5003 vr
.type
= plats
->m_value_range
.m_vr
.type
;
5004 vr
.min
= wi::to_wide (plats
->m_value_range
.m_vr
.min
);
5005 vr
.max
= wi::to_wide (plats
->m_value_range
.m_vr
.max
);
5010 vr
.type
= VR_VARYING
;
5011 vr
.min
= vr
.max
= wi::zero (INT_TYPE_SIZE
);
5013 ts
->m_vr
->quick_push (vr
);
5018 /* The IPCP driver. */
5023 struct cgraph_2edge_hook_list
*edge_duplication_hook_holder
;
5024 struct cgraph_edge_hook_list
*edge_removal_hook_holder
;
5025 struct ipa_topo_info topo
;
5027 ipa_check_create_node_params ();
5028 ipa_check_create_edge_args ();
5029 grow_edge_clone_vectors ();
5030 edge_duplication_hook_holder
5031 = symtab
->add_edge_duplication_hook (&ipcp_edge_duplication_hook
, NULL
);
5032 edge_removal_hook_holder
5033 = symtab
->add_edge_removal_hook (&ipcp_edge_removal_hook
, NULL
);
5037 fprintf (dump_file
, "\nIPA structures before propagation:\n");
5038 if (dump_flags
& TDF_DETAILS
)
5039 ipa_print_all_params (dump_file
);
5040 ipa_print_all_jump_functions (dump_file
);
5043 /* Topological sort. */
5044 build_toporder_info (&topo
);
5045 /* Do the interprocedural propagation. */
5046 ipcp_propagate_stage (&topo
);
5047 /* Decide what constant propagation and cloning should be performed. */
5048 ipcp_decision_stage (&topo
);
5049 /* Store results of bits propagation. */
5050 ipcp_store_bits_results ();
5051 /* Store results of value range propagation. */
5052 ipcp_store_vr_results ();
5054 /* Free all IPCP structures. */
5055 free_toporder_info (&topo
);
5056 next_edge_clone
.release ();
5057 prev_edge_clone
.release ();
5058 symtab
->remove_edge_removal_hook (edge_removal_hook_holder
);
5059 symtab
->remove_edge_duplication_hook (edge_duplication_hook_holder
);
5060 ipa_free_all_structures_after_ipa_cp ();
5062 fprintf (dump_file
, "\nIPA constant propagation end\n");
5066 /* Initialization and computation of IPCP data structures. This is the initial
5067 intraprocedural analysis of functions, which gathers information to be
5068 propagated later on. */
5071 ipcp_generate_summary (void)
5073 struct cgraph_node
*node
;
5076 fprintf (dump_file
, "\nIPA constant propagation start:\n");
5077 ipa_register_cgraph_hooks ();
5079 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node
)
5080 ipa_analyze_node (node
);
5083 /* Write ipcp summary for nodes in SET. */
5086 ipcp_write_summary (void)
5088 ipa_prop_write_jump_functions ();
5091 /* Read ipcp summary. */
5094 ipcp_read_summary (void)
5096 ipa_prop_read_jump_functions ();
5101 const pass_data pass_data_ipa_cp
=
5103 IPA_PASS
, /* type */
5105 OPTGROUP_NONE
, /* optinfo_flags */
5106 TV_IPA_CONSTANT_PROP
, /* tv_id */
5107 0, /* properties_required */
5108 0, /* properties_provided */
5109 0, /* properties_destroyed */
5110 0, /* todo_flags_start */
5111 ( TODO_dump_symtab
| TODO_remove_functions
), /* todo_flags_finish */
5114 class pass_ipa_cp
: public ipa_opt_pass_d
5117 pass_ipa_cp (gcc::context
*ctxt
)
5118 : ipa_opt_pass_d (pass_data_ipa_cp
, ctxt
,
5119 ipcp_generate_summary
, /* generate_summary */
5120 ipcp_write_summary
, /* write_summary */
5121 ipcp_read_summary
, /* read_summary */
5122 ipcp_write_transformation_summaries
, /*
5123 write_optimization_summary */
5124 ipcp_read_transformation_summaries
, /*
5125 read_optimization_summary */
5126 NULL
, /* stmt_fixup */
5127 0, /* function_transform_todo_flags_start */
5128 ipcp_transform_function
, /* function_transform */
5129 NULL
) /* variable_transform */
5132 /* opt_pass methods: */
5133 virtual bool gate (function
*)
5135 /* FIXME: We should remove the optimize check after we ensure we never run
5136 IPA passes when not optimizing. */
5137 return (flag_ipa_cp
&& optimize
) || in_lto_p
;
5140 virtual unsigned int execute (function
*) { return ipcp_driver (); }
5142 }; // class pass_ipa_cp
5147 make_pass_ipa_cp (gcc::context
*ctxt
)
5149 return new pass_ipa_cp (ctxt
);
5152 /* Reset all state within ipa-cp.c so that we can rerun the compiler
5153 within the same process. For use by toplev::finalize. */
5156 ipa_cp_c_finalize (void)
5158 max_count
= profile_count::uninitialized ();