syscall, runtime: always call XSI strerror_r
[official-gcc.git] / gcc / ipa-cp.cc
blobd5230c7c5e6b150f7ac9e1a1445178239dc67c39
1 /* Interprocedural constant propagation
2 Copyright (C) 2005-2022 Free Software Foundation, Inc.
4 Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
5 <mjambor@suse.cz>
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
12 version.
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
17 for more details.
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
34 is deemed good.
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
46 calls are redirected.
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
61 values:
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
94 third stage.
96 Third phase - materialization of clones, call statement updates.
97 ============================================
99 This stage is currently performed by call graph code (mainly in cgraphunit.cc
100 and tree-inline.cc) according to instructions inserted to the call graph by
101 the second stage. */
103 #define INCLUDE_ALGORITHM
104 #include "config.h"
105 #include "system.h"
106 #include "coretypes.h"
107 #include "backend.h"
108 #include "tree.h"
109 #include "gimple-expr.h"
110 #include "gimple.h"
111 #include "predict.h"
112 #include "alloc-pool.h"
113 #include "tree-pass.h"
114 #include "cgraph.h"
115 #include "diagnostic.h"
116 #include "fold-const.h"
117 #include "gimple-iterator.h"
118 #include "gimple-fold.h"
119 #include "symbol-summary.h"
120 #include "tree-vrp.h"
121 #include "ipa-prop.h"
122 #include "tree-pretty-print.h"
123 #include "tree-inline.h"
124 #include "ipa-fnsummary.h"
125 #include "ipa-utils.h"
126 #include "tree-ssa-ccp.h"
127 #include "stringpool.h"
128 #include "attribs.h"
129 #include "dbgcnt.h"
130 #include "symtab-clones.h"
132 template <typename valtype> class ipcp_value;
134 /* Describes a particular source for an IPA-CP value. */
136 template <typename valtype>
137 struct ipcp_value_source
139 public:
140 /* Aggregate offset of the source, negative if the source is scalar value of
141 the argument itself. */
142 HOST_WIDE_INT offset;
143 /* The incoming edge that brought the value. */
144 cgraph_edge *cs;
145 /* If the jump function that resulted into his value was a pass-through or an
146 ancestor, this is the ipcp_value of the caller from which the described
147 value has been derived. Otherwise it is NULL. */
148 ipcp_value<valtype> *val;
149 /* Next pointer in a linked list of sources of a value. */
150 ipcp_value_source *next;
151 /* If the jump function that resulted into his value was a pass-through or an
152 ancestor, this is the index of the parameter of the caller the jump
153 function references. */
154 int index;
157 /* Common ancestor for all ipcp_value instantiations. */
159 class ipcp_value_base
161 public:
162 /* Time benefit and that specializing the function for this value would bring
163 about in this function alone. */
164 sreal local_time_benefit;
165 /* Time benefit that specializing the function for this value can bring about
166 in it's callees. */
167 sreal prop_time_benefit;
168 /* Size cost that specializing the function for this value would bring about
169 in this function alone. */
170 int local_size_cost;
171 /* Size cost that specializing the function for this value can bring about in
172 it's callees. */
173 int prop_size_cost;
175 ipcp_value_base ()
176 : local_time_benefit (0), prop_time_benefit (0),
177 local_size_cost (0), prop_size_cost (0) {}
180 /* Describes one particular value stored in struct ipcp_lattice. */
182 template <typename valtype>
183 class ipcp_value : public ipcp_value_base
185 public:
186 /* The actual value for the given parameter. */
187 valtype value;
188 /* The list of sources from which this value originates. */
189 ipcp_value_source <valtype> *sources = nullptr;
190 /* Next pointers in a linked list of all values in a lattice. */
191 ipcp_value *next = nullptr;
192 /* Next pointers in a linked list of values in a strongly connected component
193 of values. */
194 ipcp_value *scc_next = nullptr;
195 /* Next pointers in a linked list of SCCs of values sorted topologically
196 according their sources. */
197 ipcp_value *topo_next = nullptr;
198 /* A specialized node created for this value, NULL if none has been (so far)
199 created. */
200 cgraph_node *spec_node = nullptr;
201 /* Depth first search number and low link for topological sorting of
202 values. */
203 int dfs = 0;
204 int low_link = 0;
205 /* SCC number to identify values which recursively feed into each other.
206 Values in the same SCC have the same SCC number. */
207 int scc_no = 0;
208 /* Non zero if the value is generated from another value in the same lattice
209 for a self-recursive call, the actual number is how many times the
210 operation has been performed. In the unlikely event of the value being
211 present in two chains fo self-recursive value generation chains, it is the
212 maximum. */
213 unsigned self_recursion_generated_level = 0;
214 /* True if this value is currently on the topo-sort stack. */
215 bool on_stack = false;
217 void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx,
218 HOST_WIDE_INT offset);
220 /* Return true if both THIS value and O feed into each other. */
222 bool same_scc (const ipcp_value<valtype> *o)
224 return o->scc_no == scc_no;
227 /* Return true, if a this value has been generated for a self-recursive call as
228 a result of an arithmetic pass-through jump-function acting on a value in
229 the same lattice function. */
231 bool self_recursion_generated_p ()
233 return self_recursion_generated_level > 0;
237 /* Lattice describing potential values of a formal parameter of a function, or
238 a part of an aggregate. TOP is represented by a lattice with zero values
239 and with contains_variable and bottom flags cleared. BOTTOM is represented
240 by a lattice with the bottom flag set. In that case, values and
241 contains_variable flag should be disregarded. */
243 template <typename valtype>
244 struct ipcp_lattice
246 public:
247 /* The list of known values and types in this lattice. Note that values are
248 not deallocated if a lattice is set to bottom because there may be value
249 sources referencing them. */
250 ipcp_value<valtype> *values;
251 /* Number of known values and types in this lattice. */
252 int values_count;
253 /* The lattice contains a variable component (in addition to values). */
254 bool contains_variable;
255 /* The value of the lattice is bottom (i.e. variable and unusable for any
256 propagation). */
257 bool bottom;
259 inline bool is_single_const ();
260 inline bool set_to_bottom ();
261 inline bool set_contains_variable ();
262 bool add_value (valtype newval, cgraph_edge *cs,
263 ipcp_value<valtype> *src_val = NULL,
264 int src_idx = 0, HOST_WIDE_INT offset = -1,
265 ipcp_value<valtype> **val_p = NULL,
266 unsigned same_lat_gen_level = 0);
267 void print (FILE * f, bool dump_sources, bool dump_benefits);
270 /* Lattice of tree values with an offset to describe a part of an
271 aggregate. */
273 struct ipcp_agg_lattice : public ipcp_lattice<tree>
275 public:
276 /* Offset that is being described by this lattice. */
277 HOST_WIDE_INT offset;
278 /* Size so that we don't have to re-compute it every time we traverse the
279 list. Must correspond to TYPE_SIZE of all lat values. */
280 HOST_WIDE_INT size;
281 /* Next element of the linked list. */
282 struct ipcp_agg_lattice *next;
285 /* Lattice of known bits, only capable of holding one value.
286 Bitwise constant propagation propagates which bits of a
287 value are constant.
288 For eg:
289 int f(int x)
291 return some_op (x);
294 int f1(int y)
296 if (cond)
297 return f (y & 0xff);
298 else
299 return f (y & 0xf);
302 In the above case, the param 'x' will always have all
303 the bits (except the bits in lsb) set to 0.
304 Hence the mask of 'x' would be 0xff. The mask
305 reflects that the bits in lsb are unknown.
306 The actual propagated value is given by m_value & ~m_mask. */
308 class ipcp_bits_lattice
310 public:
311 bool bottom_p () const { return m_lattice_val == IPA_BITS_VARYING; }
312 bool top_p () const { return m_lattice_val == IPA_BITS_UNDEFINED; }
313 bool constant_p () const { return m_lattice_val == IPA_BITS_CONSTANT; }
314 bool set_to_bottom ();
315 bool set_to_constant (widest_int, widest_int);
316 bool known_nonzero_p () const;
318 widest_int get_value () const { return m_value; }
319 widest_int get_mask () const { return m_mask; }
321 bool meet_with (ipcp_bits_lattice& other, unsigned, signop,
322 enum tree_code, tree, bool);
324 bool meet_with (widest_int, widest_int, unsigned);
326 void print (FILE *);
328 private:
329 enum { IPA_BITS_UNDEFINED, IPA_BITS_CONSTANT, IPA_BITS_VARYING } m_lattice_val;
331 /* Similar to ccp_lattice_t, mask represents which bits of value are constant.
332 If a bit in mask is set to 0, then the corresponding bit in
333 value is known to be constant. */
334 widest_int m_value, m_mask;
336 bool meet_with_1 (widest_int, widest_int, unsigned, bool);
337 void get_value_and_mask (tree, widest_int *, widest_int *);
340 /* Lattice of value ranges. */
342 class ipcp_vr_lattice
344 public:
345 value_range m_vr;
347 inline bool bottom_p () const;
348 inline bool top_p () const;
349 inline bool set_to_bottom ();
350 bool meet_with (const value_range *p_vr);
351 bool meet_with (const ipcp_vr_lattice &other);
352 void init () { gcc_assert (m_vr.undefined_p ()); }
353 void print (FILE * f);
355 private:
356 bool meet_with_1 (const value_range *other_vr);
359 /* Structure containing lattices for a parameter itself and for pieces of
360 aggregates that are passed in the parameter or by a reference in a parameter
361 plus some other useful flags. */
363 class ipcp_param_lattices
365 public:
366 /* Lattice describing the value of the parameter itself. */
367 ipcp_lattice<tree> itself;
368 /* Lattice describing the polymorphic contexts of a parameter. */
369 ipcp_lattice<ipa_polymorphic_call_context> ctxlat;
370 /* Lattices describing aggregate parts. */
371 ipcp_agg_lattice *aggs;
372 /* Lattice describing known bits. */
373 ipcp_bits_lattice bits_lattice;
374 /* Lattice describing value range. */
375 ipcp_vr_lattice m_value_range;
376 /* Number of aggregate lattices */
377 int aggs_count;
378 /* True if aggregate data were passed by reference (as opposed to by
379 value). */
380 bool aggs_by_ref;
381 /* All aggregate lattices contain a variable component (in addition to
382 values). */
383 bool aggs_contain_variable;
384 /* The value of all aggregate lattices is bottom (i.e. variable and unusable
385 for any propagation). */
386 bool aggs_bottom;
388 /* There is a virtual call based on this parameter. */
389 bool virt_call;
392 /* Allocation pools for values and their sources in ipa-cp. */
394 object_allocator<ipcp_value<tree> > ipcp_cst_values_pool
395 ("IPA-CP constant values");
397 object_allocator<ipcp_value<ipa_polymorphic_call_context> >
398 ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
400 object_allocator<ipcp_value_source<tree> > ipcp_sources_pool
401 ("IPA-CP value sources");
403 object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
404 ("IPA_CP aggregate lattices");
406 /* Base count to use in heuristics when using profile feedback. */
408 static profile_count base_count;
410 /* Original overall size of the program. */
412 static long overall_size, orig_overall_size;
414 /* Node name to unique clone suffix number map. */
415 static hash_map<const char *, unsigned> *clone_num_suffixes;
417 /* Return the param lattices structure corresponding to the Ith formal
418 parameter of the function described by INFO. */
419 static inline class ipcp_param_lattices *
420 ipa_get_parm_lattices (class ipa_node_params *info, int i)
422 gcc_assert (i >= 0 && i < ipa_get_param_count (info));
423 gcc_checking_assert (!info->ipcp_orig_node);
424 gcc_checking_assert (info->lattices);
425 return &(info->lattices[i]);
428 /* Return the lattice corresponding to the scalar value of the Ith formal
429 parameter of the function described by INFO. */
430 static inline ipcp_lattice<tree> *
431 ipa_get_scalar_lat (class ipa_node_params *info, int i)
433 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
434 return &plats->itself;
437 /* Return the lattice corresponding to the scalar value of the Ith formal
438 parameter of the function described by INFO. */
439 static inline ipcp_lattice<ipa_polymorphic_call_context> *
440 ipa_get_poly_ctx_lat (class ipa_node_params *info, int i)
442 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
443 return &plats->ctxlat;
446 /* Return whether LAT is a lattice with a single constant and without an
447 undefined value. */
449 template <typename valtype>
450 inline bool
451 ipcp_lattice<valtype>::is_single_const ()
453 if (bottom || contains_variable || values_count != 1)
454 return false;
455 else
456 return true;
459 /* Return true iff X and Y should be considered equal values by IPA-CP. */
461 static bool
462 values_equal_for_ipcp_p (tree x, tree y)
464 gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
466 if (x == y)
467 return true;
469 if (TREE_CODE (x) == ADDR_EXPR
470 && TREE_CODE (y) == ADDR_EXPR
471 && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
472 && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL)
473 return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
474 DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
475 else
476 return operand_equal_p (x, y, 0);
479 /* Print V which is extracted from a value in a lattice to F. */
481 static void
482 print_ipcp_constant_value (FILE * f, tree v)
484 if (TREE_CODE (v) == ADDR_EXPR
485 && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL)
487 fprintf (f, "& ");
488 print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0)));
490 else
491 print_generic_expr (f, v);
494 /* Print V which is extracted from a value in a lattice to F. */
496 static void
497 print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
499 v.dump(f, false);
502 /* Print a lattice LAT to F. */
504 template <typename valtype>
505 void
506 ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
508 ipcp_value<valtype> *val;
509 bool prev = false;
511 if (bottom)
513 fprintf (f, "BOTTOM\n");
514 return;
517 if (!values_count && !contains_variable)
519 fprintf (f, "TOP\n");
520 return;
523 if (contains_variable)
525 fprintf (f, "VARIABLE");
526 prev = true;
527 if (dump_benefits)
528 fprintf (f, "\n");
531 for (val = values; val; val = val->next)
533 if (dump_benefits && prev)
534 fprintf (f, " ");
535 else if (!dump_benefits && prev)
536 fprintf (f, ", ");
537 else
538 prev = true;
540 print_ipcp_constant_value (f, val->value);
542 if (dump_sources)
544 ipcp_value_source<valtype> *s;
546 if (val->self_recursion_generated_p ())
547 fprintf (f, " [self_gen(%i), from:",
548 val->self_recursion_generated_level);
549 else
550 fprintf (f, " [scc: %i, from:", val->scc_no);
551 for (s = val->sources; s; s = s->next)
552 fprintf (f, " %i(%f)", s->cs->caller->order,
553 s->cs->sreal_frequency ().to_double ());
554 fprintf (f, "]");
557 if (dump_benefits)
558 fprintf (f, " [loc_time: %g, loc_size: %i, "
559 "prop_time: %g, prop_size: %i]\n",
560 val->local_time_benefit.to_double (), val->local_size_cost,
561 val->prop_time_benefit.to_double (), val->prop_size_cost);
563 if (!dump_benefits)
564 fprintf (f, "\n");
567 void
568 ipcp_bits_lattice::print (FILE *f)
570 if (top_p ())
571 fprintf (f, " Bits unknown (TOP)\n");
572 else if (bottom_p ())
573 fprintf (f, " Bits unusable (BOTTOM)\n");
574 else
576 fprintf (f, " Bits: value = "); print_hex (get_value (), f);
577 fprintf (f, ", mask = "); print_hex (get_mask (), f);
578 fprintf (f, "\n");
582 /* Print value range lattice to F. */
584 void
585 ipcp_vr_lattice::print (FILE * f)
587 dump_value_range (f, &m_vr);
590 /* Print all ipcp_lattices of all functions to F. */
592 static void
593 print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
595 struct cgraph_node *node;
596 int i, count;
598 fprintf (f, "\nLattices:\n");
599 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
601 class ipa_node_params *info;
603 info = ipa_node_params_sum->get (node);
604 /* Skip unoptimized functions and constprop clones since we don't make
605 lattices for them. */
606 if (!info || info->ipcp_orig_node)
607 continue;
608 fprintf (f, " Node: %s:\n", node->dump_name ());
609 count = ipa_get_param_count (info);
610 for (i = 0; i < count; i++)
612 struct ipcp_agg_lattice *aglat;
613 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
614 fprintf (f, " param [%d]: ", i);
615 plats->itself.print (f, dump_sources, dump_benefits);
616 fprintf (f, " ctxs: ");
617 plats->ctxlat.print (f, dump_sources, dump_benefits);
618 plats->bits_lattice.print (f);
619 fprintf (f, " ");
620 plats->m_value_range.print (f);
621 fprintf (f, "\n");
622 if (plats->virt_call)
623 fprintf (f, " virt_call flag set\n");
625 if (plats->aggs_bottom)
627 fprintf (f, " AGGS BOTTOM\n");
628 continue;
630 if (plats->aggs_contain_variable)
631 fprintf (f, " AGGS VARIABLE\n");
632 for (aglat = plats->aggs; aglat; aglat = aglat->next)
634 fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
635 plats->aggs_by_ref ? "ref " : "", aglat->offset);
636 aglat->print (f, dump_sources, dump_benefits);
642 /* Determine whether it is at all technically possible to create clones of NODE
643 and store this information in the ipa_node_params structure associated
644 with NODE. */
646 static void
647 determine_versionability (struct cgraph_node *node,
648 class ipa_node_params *info)
650 const char *reason = NULL;
652 /* There are a number of generic reasons functions cannot be versioned. We
653 also cannot remove parameters if there are type attributes such as fnspec
654 present. */
655 if (node->alias || node->thunk)
656 reason = "alias or thunk";
657 else if (!node->versionable)
658 reason = "not a tree_versionable_function";
659 else if (node->get_availability () <= AVAIL_INTERPOSABLE)
660 reason = "insufficient body availability";
661 else if (!opt_for_fn (node->decl, optimize)
662 || !opt_for_fn (node->decl, flag_ipa_cp))
663 reason = "non-optimized function";
664 else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
666 /* Ideally we should clone the SIMD clones themselves and create
667 vector copies of them, so IPA-cp and SIMD clones can happily
668 coexist, but that may not be worth the effort. */
669 reason = "function has SIMD clones";
671 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl)))
673 /* Ideally we should clone the target clones themselves and create
674 copies of them, so IPA-cp and target clones can happily
675 coexist, but that may not be worth the effort. */
676 reason = "function target_clones attribute";
678 /* Don't clone decls local to a comdat group; it breaks and for C++
679 decloned constructors, inlining is always better anyway. */
680 else if (node->comdat_local_p ())
681 reason = "comdat-local function";
682 else if (node->calls_comdat_local)
684 /* TODO: call is versionable if we make sure that all
685 callers are inside of a comdat group. */
686 reason = "calls comdat-local function";
689 /* Functions calling BUILT_IN_VA_ARG_PACK and BUILT_IN_VA_ARG_PACK_LEN
690 work only when inlined. Cloning them may still lead to better code
691 because ipa-cp will not give up on cloning further. If the function is
692 external this however leads to wrong code because we may end up producing
693 offline copy of the function. */
694 if (DECL_EXTERNAL (node->decl))
695 for (cgraph_edge *edge = node->callees; !reason && edge;
696 edge = edge->next_callee)
697 if (fndecl_built_in_p (edge->callee->decl, BUILT_IN_NORMAL))
699 if (DECL_FUNCTION_CODE (edge->callee->decl) == BUILT_IN_VA_ARG_PACK)
700 reason = "external function which calls va_arg_pack";
701 if (DECL_FUNCTION_CODE (edge->callee->decl)
702 == BUILT_IN_VA_ARG_PACK_LEN)
703 reason = "external function which calls va_arg_pack_len";
706 if (reason && dump_file && !node->alias && !node->thunk)
707 fprintf (dump_file, "Function %s is not versionable, reason: %s.\n",
708 node->dump_name (), reason);
710 info->versionable = (reason == NULL);
713 /* Return true if it is at all technically possible to create clones of a
714 NODE. */
716 static bool
717 ipcp_versionable_function_p (struct cgraph_node *node)
719 ipa_node_params *info = ipa_node_params_sum->get (node);
720 return info && info->versionable;
723 /* Structure holding accumulated information about callers of a node. */
725 struct caller_statistics
727 /* If requested (see below), self-recursive call counts are summed into this
728 field. */
729 profile_count rec_count_sum;
730 /* The sum of all ipa counts of all the other (non-recursive) calls. */
731 profile_count count_sum;
732 /* Sum of all frequencies for all calls. */
733 sreal freq_sum;
734 /* Number of calls and hot calls respectively. */
735 int n_calls, n_hot_calls;
736 /* If itself is set up, also count the number of non-self-recursive
737 calls. */
738 int n_nonrec_calls;
739 /* If non-NULL, this is the node itself and calls from it should have their
740 counts included in rec_count_sum and not count_sum. */
741 cgraph_node *itself;
744 /* Initialize fields of STAT to zeroes and optionally set it up so that edges
745 from IGNORED_CALLER are not counted. */
747 static inline void
748 init_caller_stats (caller_statistics *stats, cgraph_node *itself = NULL)
750 stats->rec_count_sum = profile_count::zero ();
751 stats->count_sum = profile_count::zero ();
752 stats->n_calls = 0;
753 stats->n_hot_calls = 0;
754 stats->n_nonrec_calls = 0;
755 stats->freq_sum = 0;
756 stats->itself = itself;
759 /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
760 non-thunk incoming edges to NODE. */
762 static bool
763 gather_caller_stats (struct cgraph_node *node, void *data)
765 struct caller_statistics *stats = (struct caller_statistics *) data;
766 struct cgraph_edge *cs;
768 for (cs = node->callers; cs; cs = cs->next_caller)
769 if (!cs->caller->thunk)
771 ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
772 if (info && info->node_dead)
773 continue;
775 if (cs->count.ipa ().initialized_p ())
777 if (stats->itself && stats->itself == cs->caller)
778 stats->rec_count_sum += cs->count.ipa ();
779 else
780 stats->count_sum += cs->count.ipa ();
782 stats->freq_sum += cs->sreal_frequency ();
783 stats->n_calls++;
784 if (stats->itself && stats->itself != cs->caller)
785 stats->n_nonrec_calls++;
787 if (cs->maybe_hot_p ())
788 stats->n_hot_calls ++;
790 return false;
794 /* Return true if this NODE is viable candidate for cloning. */
796 static bool
797 ipcp_cloning_candidate_p (struct cgraph_node *node)
799 struct caller_statistics stats;
801 gcc_checking_assert (node->has_gimple_body_p ());
803 if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
805 if (dump_file)
806 fprintf (dump_file, "Not considering %s for cloning; "
807 "-fipa-cp-clone disabled.\n",
808 node->dump_name ());
809 return false;
812 if (node->optimize_for_size_p ())
814 if (dump_file)
815 fprintf (dump_file, "Not considering %s for cloning; "
816 "optimizing it for size.\n",
817 node->dump_name ());
818 return false;
821 init_caller_stats (&stats);
822 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
824 if (ipa_size_summaries->get (node)->self_size < stats.n_calls)
826 if (dump_file)
827 fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
828 node->dump_name ());
829 return true;
832 /* When profile is available and function is hot, propagate into it even if
833 calls seems cold; constant propagation can improve function's speed
834 significantly. */
835 if (stats.count_sum > profile_count::zero ()
836 && node->count.ipa ().initialized_p ())
838 if (stats.count_sum > node->count.ipa ().apply_scale (90, 100))
840 if (dump_file)
841 fprintf (dump_file, "Considering %s for cloning; "
842 "usually called directly.\n",
843 node->dump_name ());
844 return true;
847 if (!stats.n_hot_calls)
849 if (dump_file)
850 fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n",
851 node->dump_name ());
852 return false;
854 if (dump_file)
855 fprintf (dump_file, "Considering %s for cloning.\n",
856 node->dump_name ());
857 return true;
860 template <typename valtype>
861 class value_topo_info
863 public:
864 /* Head of the linked list of topologically sorted values. */
865 ipcp_value<valtype> *values_topo;
866 /* Stack for creating SCCs, represented by a linked list too. */
867 ipcp_value<valtype> *stack;
868 /* Counter driving the algorithm in add_val_to_toposort. */
869 int dfs_counter;
871 value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
873 void add_val (ipcp_value<valtype> *cur_val);
874 void propagate_effects ();
877 /* Arrays representing a topological ordering of call graph nodes and a stack
878 of nodes used during constant propagation and also data required to perform
879 topological sort of values and propagation of benefits in the determined
880 order. */
882 class ipa_topo_info
884 public:
885 /* Array with obtained topological order of cgraph nodes. */
886 struct cgraph_node **order;
887 /* Stack of cgraph nodes used during propagation within SCC until all values
888 in the SCC stabilize. */
889 struct cgraph_node **stack;
890 int nnodes, stack_top;
892 value_topo_info<tree> constants;
893 value_topo_info<ipa_polymorphic_call_context> contexts;
895 ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
896 constants ()
900 /* Skip edges from and to nodes without ipa_cp enabled.
901 Ignore not available symbols. */
903 static bool
904 ignore_edge_p (cgraph_edge *e)
906 enum availability avail;
907 cgraph_node *ultimate_target
908 = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
910 return (avail <= AVAIL_INTERPOSABLE
911 || !opt_for_fn (ultimate_target->decl, optimize)
912 || !opt_for_fn (ultimate_target->decl, flag_ipa_cp));
915 /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
917 static void
918 build_toporder_info (class ipa_topo_info *topo)
920 topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
921 topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
923 gcc_checking_assert (topo->stack_top == 0);
924 topo->nnodes = ipa_reduced_postorder (topo->order, true,
925 ignore_edge_p);
928 /* Free information about strongly connected components and the arrays in
929 TOPO. */
931 static void
932 free_toporder_info (class ipa_topo_info *topo)
934 ipa_free_postorder_info ();
935 free (topo->order);
936 free (topo->stack);
939 /* Add NODE to the stack in TOPO, unless it is already there. */
941 static inline void
942 push_node_to_stack (class ipa_topo_info *topo, struct cgraph_node *node)
944 ipa_node_params *info = ipa_node_params_sum->get (node);
945 if (info->node_enqueued)
946 return;
947 info->node_enqueued = 1;
948 topo->stack[topo->stack_top++] = node;
951 /* Pop a node from the stack in TOPO and return it or return NULL if the stack
952 is empty. */
954 static struct cgraph_node *
955 pop_node_from_stack (class ipa_topo_info *topo)
957 if (topo->stack_top)
959 struct cgraph_node *node;
960 topo->stack_top--;
961 node = topo->stack[topo->stack_top];
962 ipa_node_params_sum->get (node)->node_enqueued = 0;
963 return node;
965 else
966 return NULL;
969 /* Set lattice LAT to bottom and return true if it previously was not set as
970 such. */
972 template <typename valtype>
973 inline bool
974 ipcp_lattice<valtype>::set_to_bottom ()
976 bool ret = !bottom;
977 bottom = true;
978 return ret;
981 /* Mark lattice as containing an unknown value and return true if it previously
982 was not marked as such. */
984 template <typename valtype>
985 inline bool
986 ipcp_lattice<valtype>::set_contains_variable ()
988 bool ret = !contains_variable;
989 contains_variable = true;
990 return ret;
993 /* Set all aggregate lattices in PLATS to bottom and return true if they were
994 not previously set as such. */
996 static inline bool
997 set_agg_lats_to_bottom (class ipcp_param_lattices *plats)
999 bool ret = !plats->aggs_bottom;
1000 plats->aggs_bottom = true;
1001 return ret;
1004 /* Mark all aggregate lattices in PLATS as containing an unknown value and
1005 return true if they were not previously marked as such. */
1007 static inline bool
1008 set_agg_lats_contain_variable (class ipcp_param_lattices *plats)
1010 bool ret = !plats->aggs_contain_variable;
1011 plats->aggs_contain_variable = true;
1012 return ret;
1015 bool
1016 ipcp_vr_lattice::meet_with (const ipcp_vr_lattice &other)
1018 return meet_with_1 (&other.m_vr);
1021 /* Meet the current value of the lattice with value range described by VR
1022 lattice. */
1024 bool
1025 ipcp_vr_lattice::meet_with (const value_range *p_vr)
1027 return meet_with_1 (p_vr);
1030 /* Meet the current value of the lattice with value range described by
1031 OTHER_VR lattice. Return TRUE if anything changed. */
1033 bool
1034 ipcp_vr_lattice::meet_with_1 (const value_range *other_vr)
1036 if (bottom_p ())
1037 return false;
1039 if (other_vr->varying_p ())
1040 return set_to_bottom ();
1042 value_range save (m_vr);
1043 m_vr.union_ (*other_vr);
1044 return m_vr != save;
1047 /* Return true if value range information in the lattice is yet unknown. */
1049 bool
1050 ipcp_vr_lattice::top_p () const
1052 return m_vr.undefined_p ();
1055 /* Return true if value range information in the lattice is known to be
1056 unusable. */
1058 bool
1059 ipcp_vr_lattice::bottom_p () const
1061 return m_vr.varying_p ();
1064 /* Set value range information in the lattice to bottom. Return true if it
1065 previously was in a different state. */
1067 bool
1068 ipcp_vr_lattice::set_to_bottom ()
1070 if (m_vr.varying_p ())
1071 return false;
1072 /* ?? We create all sorts of VARYING ranges for floats, structures,
1073 and other types which we cannot handle as ranges. We should
1074 probably avoid handling them throughout the pass, but it's easier
1075 to create a sensible VARYING here and let the lattice
1076 propagate. */
1077 m_vr.set_varying (integer_type_node);
1078 return true;
1081 /* Set lattice value to bottom, if it already isn't the case. */
1083 bool
1084 ipcp_bits_lattice::set_to_bottom ()
1086 if (bottom_p ())
1087 return false;
1088 m_lattice_val = IPA_BITS_VARYING;
1089 m_value = 0;
1090 m_mask = -1;
1091 return true;
1094 /* Set to constant if it isn't already. Only meant to be called
1095 when switching state from TOP. */
1097 bool
1098 ipcp_bits_lattice::set_to_constant (widest_int value, widest_int mask)
1100 gcc_assert (top_p ());
1101 m_lattice_val = IPA_BITS_CONSTANT;
1102 m_value = wi::bit_and (wi::bit_not (mask), value);
1103 m_mask = mask;
1104 return true;
1107 /* Return true if any of the known bits are non-zero. */
1109 bool
1110 ipcp_bits_lattice::known_nonzero_p () const
1112 if (!constant_p ())
1113 return false;
1114 return wi::ne_p (wi::bit_and (wi::bit_not (m_mask), m_value), 0);
1117 /* Convert operand to value, mask form. */
1119 void
1120 ipcp_bits_lattice::get_value_and_mask (tree operand, widest_int *valuep, widest_int *maskp)
1122 wide_int get_nonzero_bits (const_tree);
1124 if (TREE_CODE (operand) == INTEGER_CST)
1126 *valuep = wi::to_widest (operand);
1127 *maskp = 0;
1129 else
1131 *valuep = 0;
1132 *maskp = -1;
1136 /* Meet operation, similar to ccp_lattice_meet, we xor values
1137 if this->value, value have different values at same bit positions, we want
1138 to drop that bit to varying. Return true if mask is changed.
1139 This function assumes that the lattice value is in CONSTANT state. If
1140 DROP_ALL_ONES, mask out any known bits with value one afterwards. */
1142 bool
1143 ipcp_bits_lattice::meet_with_1 (widest_int value, widest_int mask,
1144 unsigned precision, bool drop_all_ones)
1146 gcc_assert (constant_p ());
1148 widest_int old_mask = m_mask;
1149 m_mask = (m_mask | mask) | (m_value ^ value);
1150 if (drop_all_ones)
1151 m_mask |= m_value;
1152 m_value &= ~m_mask;
1154 if (wi::sext (m_mask, precision) == -1)
1155 return set_to_bottom ();
1157 return m_mask != old_mask;
1160 /* Meet the bits lattice with operand
1161 described by <value, mask, sgn, precision. */
1163 bool
1164 ipcp_bits_lattice::meet_with (widest_int value, widest_int mask,
1165 unsigned precision)
1167 if (bottom_p ())
1168 return false;
1170 if (top_p ())
1172 if (wi::sext (mask, precision) == -1)
1173 return set_to_bottom ();
1174 return set_to_constant (value, mask);
1177 return meet_with_1 (value, mask, precision, false);
1180 /* Meet bits lattice with the result of bit_value_binop (other, operand)
1181 if code is binary operation or bit_value_unop (other) if code is unary op.
1182 In the case when code is nop_expr, no adjustment is required. If
1183 DROP_ALL_ONES, mask out any known bits with value one afterwards. */
1185 bool
1186 ipcp_bits_lattice::meet_with (ipcp_bits_lattice& other, unsigned precision,
1187 signop sgn, enum tree_code code, tree operand,
1188 bool drop_all_ones)
1190 if (other.bottom_p ())
1191 return set_to_bottom ();
1193 if (bottom_p () || other.top_p ())
1194 return false;
1196 widest_int adjusted_value, adjusted_mask;
1198 if (TREE_CODE_CLASS (code) == tcc_binary)
1200 tree type = TREE_TYPE (operand);
1201 widest_int o_value, o_mask;
1202 get_value_and_mask (operand, &o_value, &o_mask);
1204 bit_value_binop (code, sgn, precision, &adjusted_value, &adjusted_mask,
1205 sgn, precision, other.get_value (), other.get_mask (),
1206 TYPE_SIGN (type), TYPE_PRECISION (type), o_value, o_mask);
1208 if (wi::sext (adjusted_mask, precision) == -1)
1209 return set_to_bottom ();
1212 else if (TREE_CODE_CLASS (code) == tcc_unary)
1214 bit_value_unop (code, sgn, precision, &adjusted_value,
1215 &adjusted_mask, sgn, precision, other.get_value (),
1216 other.get_mask ());
1218 if (wi::sext (adjusted_mask, precision) == -1)
1219 return set_to_bottom ();
1222 else
1223 return set_to_bottom ();
1225 if (top_p ())
1227 if (drop_all_ones)
1229 adjusted_mask |= adjusted_value;
1230 adjusted_value &= ~adjusted_mask;
1232 if (wi::sext (adjusted_mask, precision) == -1)
1233 return set_to_bottom ();
1234 return set_to_constant (adjusted_value, adjusted_mask);
1236 else
1237 return meet_with_1 (adjusted_value, adjusted_mask, precision,
1238 drop_all_ones);
1241 /* Dump the contents of the list to FILE. */
1243 void
1244 ipa_argagg_value_list::dump (FILE *f)
1246 bool comma = false;
1247 for (const ipa_argagg_value &av : m_elts)
1249 fprintf (f, "%s %i[%u]=", comma ? "," : "",
1250 av.index, av.unit_offset);
1251 print_generic_expr (f, av.value);
1252 if (av.by_ref)
1253 fprintf (f, "(by_ref)");
1254 comma = true;
1256 fprintf (f, "\n");
1259 /* Dump the contents of the list to stderr. */
1261 void
1262 ipa_argagg_value_list::debug ()
1264 dump (stderr);
1267 /* Return the item describing a constant stored for INDEX at UNIT_OFFSET or
1268 NULL if there is no such constant. */
1270 const ipa_argagg_value *
1271 ipa_argagg_value_list::get_elt (int index, unsigned unit_offset) const
1273 ipa_argagg_value key;
1274 key.index = index;
1275 key.unit_offset = unit_offset;
1276 const ipa_argagg_value *res
1277 = std::lower_bound (m_elts.begin (), m_elts.end (), key,
1278 [] (const ipa_argagg_value &elt,
1279 const ipa_argagg_value &val)
1281 if (elt.index < val.index)
1282 return true;
1283 if (elt.index > val.index)
1284 return false;
1285 if (elt.unit_offset < val.unit_offset)
1286 return true;
1287 return false;
1290 if (res == m_elts.end ()
1291 || res->index != index
1292 || res->unit_offset != unit_offset)
1293 res = nullptr;
1295 /* TODO: perhaps remove the check (that the underlying array is indeed
1296 sorted) if it turns out it can be too slow? */
1297 if (!flag_checking)
1298 return res;
1300 const ipa_argagg_value *slow_res = NULL;
1301 int prev_index = -1;
1302 unsigned prev_unit_offset = 0;
1303 for (const ipa_argagg_value &av : m_elts)
1305 gcc_assert (prev_index < 0
1306 || prev_index < av.index
1307 || prev_unit_offset < av.unit_offset);
1308 prev_index = av.index;
1309 prev_unit_offset = av.unit_offset;
1310 if (av.index == index
1311 && av.unit_offset == unit_offset)
1312 slow_res = &av;
1314 gcc_assert (res == slow_res);
1316 return res;
1319 /* Return the first item describing a constant stored for parameter with INDEX,
1320 regardless of offset or reference, or NULL if there is no such constant. */
1322 const ipa_argagg_value *
1323 ipa_argagg_value_list::get_elt_for_index (int index) const
1325 const ipa_argagg_value *res
1326 = std::lower_bound (m_elts.begin (), m_elts.end (), index,
1327 [] (const ipa_argagg_value &elt, unsigned idx)
1329 return elt.index < idx;
1331 if (res == m_elts.end ()
1332 || res->index != index)
1333 res = nullptr;
1334 return res;
1337 /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, not
1338 performing any check of whether value is passed by reference, or NULL_TREE
1339 if there is no such constant. */
1341 tree
1342 ipa_argagg_value_list::get_value (int index, unsigned unit_offset) const
1344 const ipa_argagg_value *av = get_elt (index, unit_offset);
1345 return av ? av->value : NULL_TREE;
1348 /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is
1349 passed by reference or not according to BY_REF, or NULL_TREE if there is
1350 no such constant. */
1352 tree
1353 ipa_argagg_value_list::get_value (int index, unsigned unit_offset,
1354 bool by_ref) const
1356 const ipa_argagg_value *av = get_elt (index, unit_offset);
1357 if (av && av->by_ref == by_ref)
1358 return av->value;
1359 return NULL_TREE;
1362 /* Return true if all elements present in OTHER are also present in this
1363 list. */
1365 bool
1366 ipa_argagg_value_list::superset_of_p (const ipa_argagg_value_list &other) const
1368 unsigned j = 0;
1369 for (unsigned i = 0; i < other.m_elts.size (); i++)
1371 unsigned other_index = other.m_elts[i].index;
1372 unsigned other_offset = other.m_elts[i].unit_offset;
1374 while (j < m_elts.size ()
1375 && (m_elts[j].index < other_index
1376 || (m_elts[j].index == other_index
1377 && m_elts[j].unit_offset < other_offset)))
1378 j++;
1380 if (j >= m_elts.size ()
1381 || m_elts[j].index != other_index
1382 || m_elts[j].unit_offset != other_offset
1383 || m_elts[j].by_ref != other.m_elts[i].by_ref
1384 || !m_elts[j].value
1385 || !values_equal_for_ipcp_p (m_elts[j].value, other.m_elts[i].value))
1386 return false;
1388 return true;
1391 /* Push all items in this list that describe parameter SRC_INDEX into RES as
1392 ones describing DST_INDEX while subtracting UNIT_DELTA from their unit
1393 offsets but skip those which would end up with a negative offset. */
1395 void
1396 ipa_argagg_value_list::push_adjusted_values (unsigned src_index,
1397 unsigned dest_index,
1398 unsigned unit_delta,
1399 vec<ipa_argagg_value> *res) const
1401 const ipa_argagg_value *av = get_elt_for_index (src_index);
1402 if (!av)
1403 return;
1404 unsigned prev_unit_offset = 0;
1405 bool first = true;
1406 for (; av < m_elts.end (); ++av)
1408 if (av->index > src_index)
1409 return;
1410 if (av->index == src_index
1411 && (av->unit_offset >= unit_delta)
1412 && av->value)
1414 ipa_argagg_value new_av;
1415 gcc_checking_assert (av->value);
1416 new_av.value = av->value;
1417 new_av.unit_offset = av->unit_offset - unit_delta;
1418 new_av.index = dest_index;
1419 new_av.by_ref = av->by_ref;
1421 /* Quick check that the offsets we push are indeed increasing. */
1422 gcc_assert (first
1423 || new_av.unit_offset > prev_unit_offset);
1424 prev_unit_offset = new_av.unit_offset;
1425 first = false;
1427 res->safe_push (new_av);
1432 /* Push to RES information about single lattices describing aggregate values in
1433 PLATS as those describing parameter DEST_INDEX and the original offset minus
1434 UNIT_DELTA. Return true if any item has been pushed to RES. */
1436 static bool
1437 push_agg_values_from_plats (ipcp_param_lattices *plats, int dest_index,
1438 unsigned unit_delta,
1439 vec<ipa_argagg_value> *res)
1441 if (plats->aggs_contain_variable)
1442 return false;
1444 bool pushed_sth = false;
1445 bool first = true;
1446 unsigned prev_unit_offset = 0;
1447 for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
1448 if (aglat->is_single_const ()
1449 && (aglat->offset / BITS_PER_UNIT - unit_delta) >= 0)
1451 ipa_argagg_value iav;
1452 iav.value = aglat->values->value;
1453 iav.unit_offset = aglat->offset / BITS_PER_UNIT - unit_delta;
1454 iav.index = dest_index;
1455 iav.by_ref = plats->aggs_by_ref;
1457 gcc_assert (first
1458 || iav.unit_offset > prev_unit_offset);
1459 prev_unit_offset = iav.unit_offset;
1460 first = false;
1462 pushed_sth = true;
1463 res->safe_push (iav);
1465 return pushed_sth;
1468 /* Turn all values in LIST that are not present in OTHER into NULL_TREEs.
1469 Return the number of remaining valid entries. */
1471 static unsigned
1472 intersect_argaggs_with (vec<ipa_argagg_value> &elts,
1473 const vec<ipa_argagg_value> &other)
1475 unsigned valid_entries = 0;
1476 unsigned j = 0;
1477 for (unsigned i = 0; i < elts.length (); i++)
1479 if (!elts[i].value)
1480 continue;
1482 unsigned this_index = elts[i].index;
1483 unsigned this_offset = elts[i].unit_offset;
1485 while (j < other.length ()
1486 && (other[j].index < this_index
1487 || (other[j].index == this_index
1488 && other[j].unit_offset < this_offset)))
1489 j++;
1491 if (j >= other.length ())
1493 elts[i].value = NULL_TREE;
1494 continue;
1497 if (other[j].index == this_index
1498 && other[j].unit_offset == this_offset
1499 && other[j].by_ref == elts[i].by_ref
1500 && other[j].value
1501 && values_equal_for_ipcp_p (other[j].value, elts[i].value))
1502 valid_entries++;
1503 else
1504 elts[i].value = NULL_TREE;
1506 return valid_entries;
1509 /* Mark bot aggregate and scalar lattices as containing an unknown variable,
1510 return true is any of them has not been marked as such so far. */
1512 static inline bool
1513 set_all_contains_variable (class ipcp_param_lattices *plats)
1515 bool ret;
1516 ret = plats->itself.set_contains_variable ();
1517 ret |= plats->ctxlat.set_contains_variable ();
1518 ret |= set_agg_lats_contain_variable (plats);
1519 ret |= plats->bits_lattice.set_to_bottom ();
1520 ret |= plats->m_value_range.set_to_bottom ();
1521 return ret;
1524 /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
1525 points to by the number of callers to NODE. */
1527 static bool
1528 count_callers (cgraph_node *node, void *data)
1530 int *caller_count = (int *) data;
1532 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
1533 /* Local thunks can be handled transparently, but if the thunk cannot
1534 be optimized out, count it as a real use. */
1535 if (!cs->caller->thunk || !cs->caller->local)
1536 ++*caller_count;
1537 return false;
1540 /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
1541 the one caller of some other node. Set the caller's corresponding flag. */
1543 static bool
1544 set_single_call_flag (cgraph_node *node, void *)
1546 cgraph_edge *cs = node->callers;
1547 /* Local thunks can be handled transparently, skip them. */
1548 while (cs && cs->caller->thunk && cs->caller->local)
1549 cs = cs->next_caller;
1550 if (cs)
1551 if (ipa_node_params* info = ipa_node_params_sum->get (cs->caller))
1553 info->node_calling_single_call = true;
1554 return true;
1556 return false;
1559 /* Initialize ipcp_lattices. */
1561 static void
1562 initialize_node_lattices (struct cgraph_node *node)
1564 ipa_node_params *info = ipa_node_params_sum->get (node);
1565 struct cgraph_edge *ie;
1566 bool disable = false, variable = false;
1567 int i;
1569 gcc_checking_assert (node->has_gimple_body_p ());
1571 if (!ipa_get_param_count (info))
1572 disable = true;
1573 else if (node->local)
1575 int caller_count = 0;
1576 node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
1577 true);
1578 gcc_checking_assert (caller_count > 0);
1579 if (caller_count == 1)
1580 node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
1581 NULL, true);
1583 else
1585 /* When cloning is allowed, we can assume that externally visible
1586 functions are not called. We will compensate this by cloning
1587 later. */
1588 if (ipcp_versionable_function_p (node)
1589 && ipcp_cloning_candidate_p (node))
1590 variable = true;
1591 else
1592 disable = true;
1595 if (dump_file && (dump_flags & TDF_DETAILS)
1596 && !node->alias && !node->thunk)
1598 fprintf (dump_file, "Initializing lattices of %s\n",
1599 node->dump_name ());
1600 if (disable || variable)
1601 fprintf (dump_file, " Marking all lattices as %s\n",
1602 disable ? "BOTTOM" : "VARIABLE");
1605 auto_vec<bool, 16> surviving_params;
1606 bool pre_modified = false;
1608 clone_info *cinfo = clone_info::get (node);
1610 if (!disable && cinfo && cinfo->param_adjustments)
1612 /* At the moment all IPA optimizations should use the number of
1613 parameters of the prevailing decl as the m_always_copy_start.
1614 Handling any other value would complicate the code below, so for the
1615 time bing let's only assert it is so. */
1616 gcc_assert ((cinfo->param_adjustments->m_always_copy_start
1617 == ipa_get_param_count (info))
1618 || cinfo->param_adjustments->m_always_copy_start < 0);
1620 pre_modified = true;
1621 cinfo->param_adjustments->get_surviving_params (&surviving_params);
1623 if (dump_file && (dump_flags & TDF_DETAILS)
1624 && !node->alias && !node->thunk)
1626 bool first = true;
1627 for (int j = 0; j < ipa_get_param_count (info); j++)
1629 if (j < (int) surviving_params.length ()
1630 && surviving_params[j])
1631 continue;
1632 if (first)
1634 fprintf (dump_file,
1635 " The following parameters are dead on arrival:");
1636 first = false;
1638 fprintf (dump_file, " %u", j);
1640 if (!first)
1641 fprintf (dump_file, "\n");
1645 for (i = 0; i < ipa_get_param_count (info); i++)
1647 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1648 if (disable
1649 || !ipa_get_type (info, i)
1650 || (pre_modified && (surviving_params.length () <= (unsigned) i
1651 || !surviving_params[i])))
1653 plats->itself.set_to_bottom ();
1654 plats->ctxlat.set_to_bottom ();
1655 set_agg_lats_to_bottom (plats);
1656 plats->bits_lattice.set_to_bottom ();
1657 plats->m_value_range.m_vr = value_range ();
1658 plats->m_value_range.set_to_bottom ();
1660 else
1662 plats->m_value_range.init ();
1663 if (variable)
1664 set_all_contains_variable (plats);
1668 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1669 if (ie->indirect_info->polymorphic
1670 && ie->indirect_info->param_index >= 0)
1672 gcc_checking_assert (ie->indirect_info->param_index >= 0);
1673 ipa_get_parm_lattices (info,
1674 ie->indirect_info->param_index)->virt_call = 1;
1678 /* Return true if VALUE can be safely IPA-CP propagated to a parameter of type
1679 PARAM_TYPE. */
1681 static bool
1682 ipacp_value_safe_for_type (tree param_type, tree value)
1684 tree val_type = TREE_TYPE (value);
1685 if (param_type == val_type
1686 || useless_type_conversion_p (param_type, val_type)
1687 || fold_convertible_p (param_type, value))
1688 return true;
1689 else
1690 return false;
1693 /* Return the result of a (possibly arithmetic) operation on the constant
1694 value INPUT. OPERAND is 2nd operand for binary operation. RES_TYPE is
1695 the type of the parameter to which the result is passed. Return
1696 NULL_TREE if that cannot be determined or be considered an
1697 interprocedural invariant. */
1699 static tree
1700 ipa_get_jf_arith_result (enum tree_code opcode, tree input, tree operand,
1701 tree res_type)
1703 tree res;
1705 if (opcode == NOP_EXPR)
1706 return input;
1707 if (!is_gimple_ip_invariant (input))
1708 return NULL_TREE;
1710 if (opcode == ASSERT_EXPR)
1712 if (values_equal_for_ipcp_p (input, operand))
1713 return input;
1714 else
1715 return NULL_TREE;
1718 if (!res_type)
1720 if (TREE_CODE_CLASS (opcode) == tcc_comparison)
1721 res_type = boolean_type_node;
1722 else if (expr_type_first_operand_type_p (opcode))
1723 res_type = TREE_TYPE (input);
1724 else
1725 return NULL_TREE;
1728 if (TREE_CODE_CLASS (opcode) == tcc_unary)
1729 res = fold_unary (opcode, res_type, input);
1730 else
1731 res = fold_binary (opcode, res_type, input, operand);
1733 if (res && !is_gimple_ip_invariant (res))
1734 return NULL_TREE;
1736 return res;
1739 /* Return the result of a (possibly arithmetic) pass through jump function
1740 JFUNC on the constant value INPUT. RES_TYPE is the type of the parameter
1741 to which the result is passed. Return NULL_TREE if that cannot be
1742 determined or be considered an interprocedural invariant. */
1744 static tree
1745 ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input,
1746 tree res_type)
1748 return ipa_get_jf_arith_result (ipa_get_jf_pass_through_operation (jfunc),
1749 input,
1750 ipa_get_jf_pass_through_operand (jfunc),
1751 res_type);
1754 /* Return the result of an ancestor jump function JFUNC on the constant value
1755 INPUT. Return NULL_TREE if that cannot be determined. */
1757 static tree
1758 ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
1760 gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
1761 if (TREE_CODE (input) == ADDR_EXPR)
1763 gcc_checking_assert (is_gimple_ip_invariant_address (input));
1764 poly_int64 off = ipa_get_jf_ancestor_offset (jfunc);
1765 if (known_eq (off, 0))
1766 return input;
1767 poly_int64 byte_offset = exact_div (off, BITS_PER_UNIT);
1768 return build1 (ADDR_EXPR, TREE_TYPE (input),
1769 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (input)), input,
1770 build_int_cst (ptr_type_node, byte_offset)));
1772 else if (ipa_get_jf_ancestor_keep_null (jfunc)
1773 && zerop (input))
1774 return input;
1775 else
1776 return NULL_TREE;
1779 /* Determine whether JFUNC evaluates to a single known constant value and if
1780 so, return it. Otherwise return NULL. INFO describes the caller node or
1781 the one it is inlined to, so that pass-through jump functions can be
1782 evaluated. PARM_TYPE is the type of the parameter to which the result is
1783 passed. */
1785 tree
1786 ipa_value_from_jfunc (class ipa_node_params *info, struct ipa_jump_func *jfunc,
1787 tree parm_type)
1789 if (jfunc->type == IPA_JF_CONST)
1790 return ipa_get_jf_constant (jfunc);
1791 else if (jfunc->type == IPA_JF_PASS_THROUGH
1792 || jfunc->type == IPA_JF_ANCESTOR)
1794 tree input;
1795 int idx;
1797 if (jfunc->type == IPA_JF_PASS_THROUGH)
1798 idx = ipa_get_jf_pass_through_formal_id (jfunc);
1799 else
1800 idx = ipa_get_jf_ancestor_formal_id (jfunc);
1802 if (info->ipcp_orig_node)
1803 input = info->known_csts[idx];
1804 else
1806 ipcp_lattice<tree> *lat;
1808 if (!info->lattices
1809 || idx >= ipa_get_param_count (info))
1810 return NULL_TREE;
1811 lat = ipa_get_scalar_lat (info, idx);
1812 if (!lat->is_single_const ())
1813 return NULL_TREE;
1814 input = lat->values->value;
1817 if (!input)
1818 return NULL_TREE;
1820 if (jfunc->type == IPA_JF_PASS_THROUGH)
1821 return ipa_get_jf_pass_through_result (jfunc, input, parm_type);
1822 else
1823 return ipa_get_jf_ancestor_result (jfunc, input);
1825 else
1826 return NULL_TREE;
1829 /* Determine whether JFUNC evaluates to single known polymorphic context, given
1830 that INFO describes the caller node or the one it is inlined to, CS is the
1831 call graph edge corresponding to JFUNC and CSIDX index of the described
1832 parameter. */
1834 ipa_polymorphic_call_context
1835 ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1836 ipa_jump_func *jfunc)
1838 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
1839 ipa_polymorphic_call_context ctx;
1840 ipa_polymorphic_call_context *edge_ctx
1841 = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1843 if (edge_ctx && !edge_ctx->useless_p ())
1844 ctx = *edge_ctx;
1846 if (jfunc->type == IPA_JF_PASS_THROUGH
1847 || jfunc->type == IPA_JF_ANCESTOR)
1849 ipa_polymorphic_call_context srcctx;
1850 int srcidx;
1851 bool type_preserved = true;
1852 if (jfunc->type == IPA_JF_PASS_THROUGH)
1854 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1855 return ctx;
1856 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1857 srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1859 else
1861 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1862 srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1864 if (info->ipcp_orig_node)
1866 if (info->known_contexts.exists ())
1867 srcctx = info->known_contexts[srcidx];
1869 else
1871 if (!info->lattices
1872 || srcidx >= ipa_get_param_count (info))
1873 return ctx;
1874 ipcp_lattice<ipa_polymorphic_call_context> *lat;
1875 lat = ipa_get_poly_ctx_lat (info, srcidx);
1876 if (!lat->is_single_const ())
1877 return ctx;
1878 srcctx = lat->values->value;
1880 if (srcctx.useless_p ())
1881 return ctx;
1882 if (jfunc->type == IPA_JF_ANCESTOR)
1883 srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1884 if (!type_preserved)
1885 srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1886 srcctx.combine_with (ctx);
1887 return srcctx;
1890 return ctx;
1893 /* Emulate effects of unary OPERATION and/or conversion from SRC_TYPE to
1894 DST_TYPE on value range in SRC_VR and store it to DST_VR. Return true if
1895 the result is a range or an anti-range. */
1897 static bool
1898 ipa_vr_operation_and_type_effects (value_range *dst_vr,
1899 value_range *src_vr,
1900 enum tree_code operation,
1901 tree dst_type, tree src_type)
1903 range_fold_unary_expr (dst_vr, operation, dst_type, src_vr, src_type);
1904 if (dst_vr->varying_p () || dst_vr->undefined_p ())
1905 return false;
1906 return true;
1909 /* Determine value_range of JFUNC given that INFO describes the caller node or
1910 the one it is inlined to, CS is the call graph edge corresponding to JFUNC
1911 and PARM_TYPE of the parameter. */
1913 value_range
1914 ipa_value_range_from_jfunc (ipa_node_params *info, cgraph_edge *cs,
1915 ipa_jump_func *jfunc, tree parm_type)
1917 value_range vr;
1918 if (jfunc->m_vr)
1919 ipa_vr_operation_and_type_effects (&vr,
1920 jfunc->m_vr,
1921 NOP_EXPR, parm_type,
1922 jfunc->m_vr->type ());
1923 if (vr.singleton_p ())
1924 return vr;
1925 if (jfunc->type == IPA_JF_PASS_THROUGH)
1927 int idx;
1928 ipcp_transformation *sum
1929 = ipcp_get_transformation_summary (cs->caller->inlined_to
1930 ? cs->caller->inlined_to
1931 : cs->caller);
1932 if (!sum || !sum->m_vr)
1933 return vr;
1935 idx = ipa_get_jf_pass_through_formal_id (jfunc);
1937 if (!(*sum->m_vr)[idx].known)
1938 return vr;
1939 tree vr_type = ipa_get_type (info, idx);
1940 value_range srcvr (wide_int_to_tree (vr_type, (*sum->m_vr)[idx].min),
1941 wide_int_to_tree (vr_type, (*sum->m_vr)[idx].max),
1942 (*sum->m_vr)[idx].type);
1944 enum tree_code operation = ipa_get_jf_pass_through_operation (jfunc);
1946 if (TREE_CODE_CLASS (operation) == tcc_unary)
1948 value_range res;
1950 if (ipa_vr_operation_and_type_effects (&res,
1951 &srcvr,
1952 operation, parm_type,
1953 vr_type))
1954 vr.intersect (res);
1956 else
1958 value_range op_res, res;
1959 tree op = ipa_get_jf_pass_through_operand (jfunc);
1960 value_range op_vr (op, op);
1962 range_fold_binary_expr (&op_res, operation, vr_type, &srcvr, &op_vr);
1963 if (ipa_vr_operation_and_type_effects (&res,
1964 &op_res,
1965 NOP_EXPR, parm_type,
1966 vr_type))
1967 vr.intersect (res);
1970 return vr;
1973 /* Determine whether ITEM, jump function for an aggregate part, evaluates to a
1974 single known constant value and if so, return it. Otherwise return NULL.
1975 NODE and INFO describes the caller node or the one it is inlined to, and
1976 its related info. */
1978 tree
1979 ipa_agg_value_from_jfunc (ipa_node_params *info, cgraph_node *node,
1980 const ipa_agg_jf_item *item)
1982 tree value = NULL_TREE;
1983 int src_idx;
1985 if (item->offset < 0 || item->jftype == IPA_JF_UNKNOWN)
1986 return NULL_TREE;
1988 if (item->jftype == IPA_JF_CONST)
1989 return item->value.constant;
1991 gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
1992 || item->jftype == IPA_JF_LOAD_AGG);
1994 src_idx = item->value.pass_through.formal_id;
1996 if (info->ipcp_orig_node)
1998 if (item->jftype == IPA_JF_PASS_THROUGH)
1999 value = info->known_csts[src_idx];
2000 else if (ipcp_transformation *ts = ipcp_get_transformation_summary (node))
2002 ipa_argagg_value_list avl (ts);
2003 value = avl.get_value (src_idx,
2004 item->value.load_agg.offset / BITS_PER_UNIT,
2005 item->value.load_agg.by_ref);
2008 else if (info->lattices)
2010 class ipcp_param_lattices *src_plats
2011 = ipa_get_parm_lattices (info, src_idx);
2013 if (item->jftype == IPA_JF_PASS_THROUGH)
2015 struct ipcp_lattice<tree> *lat = &src_plats->itself;
2017 if (!lat->is_single_const ())
2018 return NULL_TREE;
2020 value = lat->values->value;
2022 else if (src_plats->aggs
2023 && !src_plats->aggs_bottom
2024 && !src_plats->aggs_contain_variable
2025 && src_plats->aggs_by_ref == item->value.load_agg.by_ref)
2027 struct ipcp_agg_lattice *aglat;
2029 for (aglat = src_plats->aggs; aglat; aglat = aglat->next)
2031 if (aglat->offset > item->value.load_agg.offset)
2032 break;
2034 if (aglat->offset == item->value.load_agg.offset)
2036 if (aglat->is_single_const ())
2037 value = aglat->values->value;
2038 break;
2044 if (!value)
2045 return NULL_TREE;
2047 if (item->jftype == IPA_JF_LOAD_AGG)
2049 tree load_type = item->value.load_agg.type;
2050 tree value_type = TREE_TYPE (value);
2052 /* Ensure value type is compatible with load type. */
2053 if (!useless_type_conversion_p (load_type, value_type))
2054 return NULL_TREE;
2057 return ipa_get_jf_arith_result (item->value.pass_through.operation,
2058 value,
2059 item->value.pass_through.operand,
2060 item->type);
2063 /* Process all items in AGG_JFUNC relative to caller (or the node the original
2064 caller is inlined to) NODE which described by INFO and push the results to
2065 RES as describing values passed in parameter DST_INDEX. */
2067 void
2068 ipa_push_agg_values_from_jfunc (ipa_node_params *info, cgraph_node *node,
2069 ipa_agg_jump_function *agg_jfunc,
2070 unsigned dst_index,
2071 vec<ipa_argagg_value> *res)
2073 unsigned prev_unit_offset = 0;
2074 bool first = true;
2076 for (const ipa_agg_jf_item &item : agg_jfunc->items)
2078 tree value = ipa_agg_value_from_jfunc (info, node, &item);
2079 if (!value)
2080 continue;
2082 ipa_argagg_value iav;
2083 iav.value = value;
2084 iav.unit_offset = item.offset / BITS_PER_UNIT;
2085 iav.index = dst_index;
2086 iav.by_ref = agg_jfunc->by_ref;
2088 gcc_assert (first
2089 || iav.unit_offset > prev_unit_offset);
2090 prev_unit_offset = iav.unit_offset;
2091 first = false;
2093 res->safe_push (iav);
2097 /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
2098 bottom, not containing a variable component and without any known value at
2099 the same time. */
2101 DEBUG_FUNCTION void
2102 ipcp_verify_propagated_values (void)
2104 struct cgraph_node *node;
2106 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2108 ipa_node_params *info = ipa_node_params_sum->get (node);
2109 if (!opt_for_fn (node->decl, flag_ipa_cp)
2110 || !opt_for_fn (node->decl, optimize))
2111 continue;
2112 int i, count = ipa_get_param_count (info);
2114 for (i = 0; i < count; i++)
2116 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
2118 if (!lat->bottom
2119 && !lat->contains_variable
2120 && lat->values_count == 0)
2122 if (dump_file)
2124 symtab->dump (dump_file);
2125 fprintf (dump_file, "\nIPA lattices after constant "
2126 "propagation, before gcc_unreachable:\n");
2127 print_all_lattices (dump_file, true, false);
2130 gcc_unreachable ();
2136 /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
2138 static bool
2139 values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
2140 ipa_polymorphic_call_context y)
2142 return x.equal_to (y);
2146 /* Add a new value source to the value represented by THIS, marking that a
2147 value comes from edge CS and (if the underlying jump function is a
2148 pass-through or an ancestor one) from a caller value SRC_VAL of a caller
2149 parameter described by SRC_INDEX. OFFSET is negative if the source was the
2150 scalar value of the parameter itself or the offset within an aggregate. */
2152 template <typename valtype>
2153 void
2154 ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
2155 int src_idx, HOST_WIDE_INT offset)
2157 ipcp_value_source<valtype> *src;
2159 src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
2160 src->offset = offset;
2161 src->cs = cs;
2162 src->val = src_val;
2163 src->index = src_idx;
2165 src->next = sources;
2166 sources = src;
2169 /* Allocate a new ipcp_value holding a tree constant, initialize its value to
2170 SOURCE and clear all other fields. */
2172 static ipcp_value<tree> *
2173 allocate_and_init_ipcp_value (tree cst, unsigned same_lat_gen_level)
2175 ipcp_value<tree> *val;
2177 val = new (ipcp_cst_values_pool.allocate ()) ipcp_value<tree>();
2178 val->value = cst;
2179 val->self_recursion_generated_level = same_lat_gen_level;
2180 return val;
2183 /* Allocate a new ipcp_value holding a polymorphic context, initialize its
2184 value to SOURCE and clear all other fields. */
2186 static ipcp_value<ipa_polymorphic_call_context> *
2187 allocate_and_init_ipcp_value (ipa_polymorphic_call_context ctx,
2188 unsigned same_lat_gen_level)
2190 ipcp_value<ipa_polymorphic_call_context> *val;
2192 val = new (ipcp_poly_ctx_values_pool.allocate ())
2193 ipcp_value<ipa_polymorphic_call_context>();
2194 val->value = ctx;
2195 val->self_recursion_generated_level = same_lat_gen_level;
2196 return val;
2199 /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
2200 SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
2201 meaning. OFFSET -1 means the source is scalar and not a part of an
2202 aggregate. If non-NULL, VAL_P records address of existing or newly added
2203 ipcp_value.
2205 If the value is generated for a self-recursive call as a result of an
2206 arithmetic pass-through jump-function acting on a value in the same lattice,
2207 SAME_LAT_GEN_LEVEL must be the length of such chain, otherwise it must be
2208 zero. If it is non-zero, PARAM_IPA_CP_VALUE_LIST_SIZE limit is ignored. */
2210 template <typename valtype>
2211 bool
2212 ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
2213 ipcp_value<valtype> *src_val,
2214 int src_idx, HOST_WIDE_INT offset,
2215 ipcp_value<valtype> **val_p,
2216 unsigned same_lat_gen_level)
2218 ipcp_value<valtype> *val, *last_val = NULL;
2220 if (val_p)
2221 *val_p = NULL;
2223 if (bottom)
2224 return false;
2226 for (val = values; val; last_val = val, val = val->next)
2227 if (values_equal_for_ipcp_p (val->value, newval))
2229 if (val_p)
2230 *val_p = val;
2232 if (val->self_recursion_generated_level < same_lat_gen_level)
2233 val->self_recursion_generated_level = same_lat_gen_level;
2235 if (ipa_edge_within_scc (cs))
2237 ipcp_value_source<valtype> *s;
2238 for (s = val->sources; s; s = s->next)
2239 if (s->cs == cs && s->val == src_val)
2240 break;
2241 if (s)
2242 return false;
2245 val->add_source (cs, src_val, src_idx, offset);
2246 return false;
2249 if (!same_lat_gen_level && values_count == opt_for_fn (cs->caller->decl,
2250 param_ipa_cp_value_list_size))
2252 /* We can only free sources, not the values themselves, because sources
2253 of other values in this SCC might point to them. */
2254 for (val = values; val; val = val->next)
2256 while (val->sources)
2258 ipcp_value_source<valtype> *src = val->sources;
2259 val->sources = src->next;
2260 ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
2263 values = NULL;
2264 return set_to_bottom ();
2267 values_count++;
2268 val = allocate_and_init_ipcp_value (newval, same_lat_gen_level);
2269 val->add_source (cs, src_val, src_idx, offset);
2270 val->next = NULL;
2272 /* Add the new value to end of value list, which can reduce iterations
2273 of propagation stage for recursive function. */
2274 if (last_val)
2275 last_val->next = val;
2276 else
2277 values = val;
2279 if (val_p)
2280 *val_p = val;
2282 return true;
2285 /* A helper function that returns result of operation specified by OPCODE on
2286 the value of SRC_VAL. If non-NULL, OPND1_TYPE is expected type for the
2287 value of SRC_VAL. If the operation is binary, OPND2 is a constant value
2288 acting as its second operand. If non-NULL, RES_TYPE is expected type of
2289 the result. */
2291 static tree
2292 get_val_across_arith_op (enum tree_code opcode,
2293 tree opnd1_type,
2294 tree opnd2,
2295 ipcp_value<tree> *src_val,
2296 tree res_type)
2298 tree opnd1 = src_val->value;
2300 /* Skip source values that is incompatible with specified type. */
2301 if (opnd1_type
2302 && !useless_type_conversion_p (opnd1_type, TREE_TYPE (opnd1)))
2303 return NULL_TREE;
2305 return ipa_get_jf_arith_result (opcode, opnd1, opnd2, res_type);
2308 /* Propagate values through an arithmetic transformation described by a jump
2309 function associated with edge CS, taking values from SRC_LAT and putting
2310 them into DEST_LAT. OPND1_TYPE is expected type for the values in SRC_LAT.
2311 OPND2 is a constant value if transformation is a binary operation.
2312 SRC_OFFSET specifies offset in an aggregate if SRC_LAT describes lattice of
2313 a part of the aggregate. SRC_IDX is the index of the source parameter.
2314 RES_TYPE is the value type of result being propagated into. Return true if
2315 DEST_LAT changed. */
2317 static bool
2318 propagate_vals_across_arith_jfunc (cgraph_edge *cs,
2319 enum tree_code opcode,
2320 tree opnd1_type,
2321 tree opnd2,
2322 ipcp_lattice<tree> *src_lat,
2323 ipcp_lattice<tree> *dest_lat,
2324 HOST_WIDE_INT src_offset,
2325 int src_idx,
2326 tree res_type)
2328 ipcp_value<tree> *src_val;
2329 bool ret = false;
2331 /* Due to circular dependencies, propagating within an SCC through arithmetic
2332 transformation would create infinite number of values. But for
2333 self-feeding recursive function, we could allow propagation in a limited
2334 count, and this can enable a simple kind of recursive function versioning.
2335 For other scenario, we would just make lattices bottom. */
2336 if (opcode != NOP_EXPR && ipa_edge_within_scc (cs))
2338 int i;
2340 int max_recursive_depth = opt_for_fn(cs->caller->decl,
2341 param_ipa_cp_max_recursive_depth);
2342 if (src_lat != dest_lat || max_recursive_depth < 1)
2343 return dest_lat->set_contains_variable ();
2345 /* No benefit if recursive execution is in low probability. */
2346 if (cs->sreal_frequency () * 100
2347 <= ((sreal) 1) * opt_for_fn (cs->caller->decl,
2348 param_ipa_cp_min_recursive_probability))
2349 return dest_lat->set_contains_variable ();
2351 auto_vec<ipcp_value<tree> *, 8> val_seeds;
2353 for (src_val = src_lat->values; src_val; src_val = src_val->next)
2355 /* Now we do not use self-recursively generated value as propagation
2356 source, this is absolutely conservative, but could avoid explosion
2357 of lattice's value space, especially when one recursive function
2358 calls another recursive. */
2359 if (src_val->self_recursion_generated_p ())
2361 ipcp_value_source<tree> *s;
2363 /* If the lattice has already been propagated for the call site,
2364 no need to do that again. */
2365 for (s = src_val->sources; s; s = s->next)
2366 if (s->cs == cs)
2367 return dest_lat->set_contains_variable ();
2369 else
2370 val_seeds.safe_push (src_val);
2373 gcc_assert ((int) val_seeds.length () <= param_ipa_cp_value_list_size);
2375 /* Recursively generate lattice values with a limited count. */
2376 FOR_EACH_VEC_ELT (val_seeds, i, src_val)
2378 for (int j = 1; j < max_recursive_depth; j++)
2380 tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
2381 src_val, res_type);
2382 if (!cstval
2383 || !ipacp_value_safe_for_type (res_type, cstval))
2384 break;
2386 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
2387 src_offset, &src_val, j);
2388 gcc_checking_assert (src_val);
2391 ret |= dest_lat->set_contains_variable ();
2393 else
2394 for (src_val = src_lat->values; src_val; src_val = src_val->next)
2396 /* Now we do not use self-recursively generated value as propagation
2397 source, otherwise it is easy to make value space of normal lattice
2398 overflow. */
2399 if (src_val->self_recursion_generated_p ())
2401 ret |= dest_lat->set_contains_variable ();
2402 continue;
2405 tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
2406 src_val, res_type);
2407 if (cstval
2408 && ipacp_value_safe_for_type (res_type, cstval))
2409 ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
2410 src_offset);
2411 else
2412 ret |= dest_lat->set_contains_variable ();
2415 return ret;
2418 /* Propagate values through a pass-through jump function JFUNC associated with
2419 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
2420 is the index of the source parameter. PARM_TYPE is the type of the
2421 parameter to which the result is passed. */
2423 static bool
2424 propagate_vals_across_pass_through (cgraph_edge *cs, ipa_jump_func *jfunc,
2425 ipcp_lattice<tree> *src_lat,
2426 ipcp_lattice<tree> *dest_lat, int src_idx,
2427 tree parm_type)
2429 return propagate_vals_across_arith_jfunc (cs,
2430 ipa_get_jf_pass_through_operation (jfunc),
2431 NULL_TREE,
2432 ipa_get_jf_pass_through_operand (jfunc),
2433 src_lat, dest_lat, -1, src_idx, parm_type);
2436 /* Propagate values through an ancestor jump function JFUNC associated with
2437 edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
2438 is the index of the source parameter. */
2440 static bool
2441 propagate_vals_across_ancestor (struct cgraph_edge *cs,
2442 struct ipa_jump_func *jfunc,
2443 ipcp_lattice<tree> *src_lat,
2444 ipcp_lattice<tree> *dest_lat, int src_idx,
2445 tree param_type)
2447 ipcp_value<tree> *src_val;
2448 bool ret = false;
2450 if (ipa_edge_within_scc (cs))
2451 return dest_lat->set_contains_variable ();
2453 for (src_val = src_lat->values; src_val; src_val = src_val->next)
2455 tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
2457 if (t && ipacp_value_safe_for_type (param_type, t))
2458 ret |= dest_lat->add_value (t, cs, src_val, src_idx);
2459 else
2460 ret |= dest_lat->set_contains_variable ();
2463 return ret;
2466 /* Propagate scalar values across jump function JFUNC that is associated with
2467 edge CS and put the values into DEST_LAT. PARM_TYPE is the type of the
2468 parameter to which the result is passed. */
2470 static bool
2471 propagate_scalar_across_jump_function (struct cgraph_edge *cs,
2472 struct ipa_jump_func *jfunc,
2473 ipcp_lattice<tree> *dest_lat,
2474 tree param_type)
2476 if (dest_lat->bottom)
2477 return false;
2479 if (jfunc->type == IPA_JF_CONST)
2481 tree val = ipa_get_jf_constant (jfunc);
2482 if (ipacp_value_safe_for_type (param_type, val))
2483 return dest_lat->add_value (val, cs, NULL, 0);
2484 else
2485 return dest_lat->set_contains_variable ();
2487 else if (jfunc->type == IPA_JF_PASS_THROUGH
2488 || jfunc->type == IPA_JF_ANCESTOR)
2490 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2491 ipcp_lattice<tree> *src_lat;
2492 int src_idx;
2493 bool ret;
2495 if (jfunc->type == IPA_JF_PASS_THROUGH)
2496 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2497 else
2498 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2500 src_lat = ipa_get_scalar_lat (caller_info, src_idx);
2501 if (src_lat->bottom)
2502 return dest_lat->set_contains_variable ();
2504 /* If we would need to clone the caller and cannot, do not propagate. */
2505 if (!ipcp_versionable_function_p (cs->caller)
2506 && (src_lat->contains_variable
2507 || (src_lat->values_count > 1)))
2508 return dest_lat->set_contains_variable ();
2510 if (jfunc->type == IPA_JF_PASS_THROUGH)
2511 ret = propagate_vals_across_pass_through (cs, jfunc, src_lat,
2512 dest_lat, src_idx,
2513 param_type);
2514 else
2515 ret = propagate_vals_across_ancestor (cs, jfunc, src_lat, dest_lat,
2516 src_idx, param_type);
2518 if (src_lat->contains_variable)
2519 ret |= dest_lat->set_contains_variable ();
2521 return ret;
2524 /* TODO: We currently do not handle member method pointers in IPA-CP (we only
2525 use it for indirect inlining), we should propagate them too. */
2526 return dest_lat->set_contains_variable ();
2529 /* Propagate scalar values across jump function JFUNC that is associated with
2530 edge CS and describes argument IDX and put the values into DEST_LAT. */
2532 static bool
2533 propagate_context_across_jump_function (cgraph_edge *cs,
2534 ipa_jump_func *jfunc, int idx,
2535 ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
2537 if (dest_lat->bottom)
2538 return false;
2539 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
2540 bool ret = false;
2541 bool added_sth = false;
2542 bool type_preserved = true;
2544 ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
2545 = ipa_get_ith_polymorhic_call_context (args, idx);
2547 if (edge_ctx_ptr)
2548 edge_ctx = *edge_ctx_ptr;
2550 if (jfunc->type == IPA_JF_PASS_THROUGH
2551 || jfunc->type == IPA_JF_ANCESTOR)
2553 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2554 int src_idx;
2555 ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
2557 /* TODO: Once we figure out how to propagate speculations, it will
2558 probably be a good idea to switch to speculation if type_preserved is
2559 not set instead of punting. */
2560 if (jfunc->type == IPA_JF_PASS_THROUGH)
2562 if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
2563 goto prop_fail;
2564 type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
2565 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2567 else
2569 type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
2570 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2573 src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
2574 /* If we would need to clone the caller and cannot, do not propagate. */
2575 if (!ipcp_versionable_function_p (cs->caller)
2576 && (src_lat->contains_variable
2577 || (src_lat->values_count > 1)))
2578 goto prop_fail;
2580 ipcp_value<ipa_polymorphic_call_context> *src_val;
2581 for (src_val = src_lat->values; src_val; src_val = src_val->next)
2583 ipa_polymorphic_call_context cur = src_val->value;
2585 if (!type_preserved)
2586 cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
2587 if (jfunc->type == IPA_JF_ANCESTOR)
2588 cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
2589 /* TODO: In cases we know how the context is going to be used,
2590 we can improve the result by passing proper OTR_TYPE. */
2591 cur.combine_with (edge_ctx);
2592 if (!cur.useless_p ())
2594 if (src_lat->contains_variable
2595 && !edge_ctx.equal_to (cur))
2596 ret |= dest_lat->set_contains_variable ();
2597 ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
2598 added_sth = true;
2603 prop_fail:
2604 if (!added_sth)
2606 if (!edge_ctx.useless_p ())
2607 ret |= dest_lat->add_value (edge_ctx, cs);
2608 else
2609 ret |= dest_lat->set_contains_variable ();
2612 return ret;
2615 /* Propagate bits across jfunc that is associated with
2616 edge cs and update dest_lattice accordingly. */
2618 bool
2619 propagate_bits_across_jump_function (cgraph_edge *cs, int idx,
2620 ipa_jump_func *jfunc,
2621 ipcp_bits_lattice *dest_lattice)
2623 if (dest_lattice->bottom_p ())
2624 return false;
2626 enum availability availability;
2627 cgraph_node *callee = cs->callee->function_symbol (&availability);
2628 ipa_node_params *callee_info = ipa_node_params_sum->get (callee);
2629 tree parm_type = ipa_get_type (callee_info, idx);
2631 /* For K&R C programs, ipa_get_type() could return NULL_TREE. Avoid the
2632 transform for these cases. Similarly, we can have bad type mismatches
2633 with LTO, avoid doing anything with those too. */
2634 if (!parm_type
2635 || (!INTEGRAL_TYPE_P (parm_type) && !POINTER_TYPE_P (parm_type)))
2637 if (dump_file && (dump_flags & TDF_DETAILS))
2638 fprintf (dump_file, "Setting dest_lattice to bottom, because type of "
2639 "param %i of %s is NULL or unsuitable for bits propagation\n",
2640 idx, cs->callee->dump_name ());
2642 return dest_lattice->set_to_bottom ();
2645 unsigned precision = TYPE_PRECISION (parm_type);
2646 signop sgn = TYPE_SIGN (parm_type);
2648 if (jfunc->type == IPA_JF_PASS_THROUGH
2649 || jfunc->type == IPA_JF_ANCESTOR)
2651 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2652 tree operand = NULL_TREE;
2653 enum tree_code code;
2654 unsigned src_idx;
2655 bool keep_null = false;
2657 if (jfunc->type == IPA_JF_PASS_THROUGH)
2659 code = ipa_get_jf_pass_through_operation (jfunc);
2660 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2661 if (code != NOP_EXPR)
2662 operand = ipa_get_jf_pass_through_operand (jfunc);
2664 else
2666 code = POINTER_PLUS_EXPR;
2667 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2668 unsigned HOST_WIDE_INT offset
2669 = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
2670 keep_null = (ipa_get_jf_ancestor_keep_null (jfunc) || !offset);
2671 operand = build_int_cstu (size_type_node, offset);
2674 class ipcp_param_lattices *src_lats
2675 = ipa_get_parm_lattices (caller_info, src_idx);
2677 /* Try to propagate bits if src_lattice is bottom, but jfunc is known.
2678 for eg consider:
2679 int f(int x)
2681 g (x & 0xff);
2683 Assume lattice for x is bottom, however we can still propagate
2684 result of x & 0xff == 0xff, which gets computed during ccp1 pass
2685 and we store it in jump function during analysis stage. */
2687 if (!src_lats->bits_lattice.bottom_p ())
2689 bool drop_all_ones
2690 = keep_null && !src_lats->bits_lattice.known_nonzero_p ();
2692 return dest_lattice->meet_with (src_lats->bits_lattice, precision,
2693 sgn, code, operand, drop_all_ones);
2697 if (jfunc->bits)
2698 return dest_lattice->meet_with (jfunc->bits->value, jfunc->bits->mask,
2699 precision);
2700 else
2701 return dest_lattice->set_to_bottom ();
2704 /* Propagate value range across jump function JFUNC that is associated with
2705 edge CS with param of callee of PARAM_TYPE and update DEST_PLATS
2706 accordingly. */
2708 static bool
2709 propagate_vr_across_jump_function (cgraph_edge *cs, ipa_jump_func *jfunc,
2710 class ipcp_param_lattices *dest_plats,
2711 tree param_type)
2713 ipcp_vr_lattice *dest_lat = &dest_plats->m_value_range;
2715 if (dest_lat->bottom_p ())
2716 return false;
2718 if (!param_type
2719 || (!INTEGRAL_TYPE_P (param_type)
2720 && !POINTER_TYPE_P (param_type)))
2721 return dest_lat->set_to_bottom ();
2723 if (jfunc->type == IPA_JF_PASS_THROUGH)
2725 enum tree_code operation = ipa_get_jf_pass_through_operation (jfunc);
2726 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2727 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2728 class ipcp_param_lattices *src_lats
2729 = ipa_get_parm_lattices (caller_info, src_idx);
2730 tree operand_type = ipa_get_type (caller_info, src_idx);
2732 if (src_lats->m_value_range.bottom_p ())
2733 return dest_lat->set_to_bottom ();
2735 value_range vr;
2736 if (TREE_CODE_CLASS (operation) == tcc_unary)
2737 ipa_vr_operation_and_type_effects (&vr,
2738 &src_lats->m_value_range.m_vr,
2739 operation, param_type,
2740 operand_type);
2741 /* A crude way to prevent unbounded number of value range updates
2742 in SCC components. We should allow limited number of updates within
2743 SCC, too. */
2744 else if (!ipa_edge_within_scc (cs))
2746 tree op = ipa_get_jf_pass_through_operand (jfunc);
2747 value_range op_vr (op, op);
2748 value_range op_res,res;
2750 range_fold_binary_expr (&op_res, operation, operand_type,
2751 &src_lats->m_value_range.m_vr, &op_vr);
2752 ipa_vr_operation_and_type_effects (&vr,
2753 &op_res,
2754 NOP_EXPR, param_type,
2755 operand_type);
2757 if (!vr.undefined_p () && !vr.varying_p ())
2759 if (jfunc->m_vr)
2761 value_range jvr;
2762 if (ipa_vr_operation_and_type_effects (&jvr, jfunc->m_vr,
2763 NOP_EXPR,
2764 param_type,
2765 jfunc->m_vr->type ()))
2766 vr.intersect (jvr);
2768 return dest_lat->meet_with (&vr);
2771 else if (jfunc->type == IPA_JF_CONST)
2773 tree val = ipa_get_jf_constant (jfunc);
2774 if (TREE_CODE (val) == INTEGER_CST)
2776 val = fold_convert (param_type, val);
2777 if (TREE_OVERFLOW_P (val))
2778 val = drop_tree_overflow (val);
2780 value_range tmpvr (val, val);
2781 return dest_lat->meet_with (&tmpvr);
2785 value_range vr;
2786 if (jfunc->m_vr
2787 && ipa_vr_operation_and_type_effects (&vr, jfunc->m_vr, NOP_EXPR,
2788 param_type,
2789 jfunc->m_vr->type ()))
2790 return dest_lat->meet_with (&vr);
2791 else
2792 return dest_lat->set_to_bottom ();
2795 /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
2796 NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
2797 other cases, return false). If there are no aggregate items, set
2798 aggs_by_ref to NEW_AGGS_BY_REF. */
2800 static bool
2801 set_check_aggs_by_ref (class ipcp_param_lattices *dest_plats,
2802 bool new_aggs_by_ref)
2804 if (dest_plats->aggs)
2806 if (dest_plats->aggs_by_ref != new_aggs_by_ref)
2808 set_agg_lats_to_bottom (dest_plats);
2809 return true;
2812 else
2813 dest_plats->aggs_by_ref = new_aggs_by_ref;
2814 return false;
2817 /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
2818 already existing lattice for the given OFFSET and SIZE, marking all skipped
2819 lattices as containing variable and checking for overlaps. If there is no
2820 already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
2821 it with offset, size and contains_variable to PRE_EXISTING, and return true,
2822 unless there are too many already. If there are two many, return false. If
2823 there are overlaps turn whole DEST_PLATS to bottom and return false. If any
2824 skipped lattices were newly marked as containing variable, set *CHANGE to
2825 true. MAX_AGG_ITEMS is the maximum number of lattices. */
2827 static bool
2828 merge_agg_lats_step (class ipcp_param_lattices *dest_plats,
2829 HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
2830 struct ipcp_agg_lattice ***aglat,
2831 bool pre_existing, bool *change, int max_agg_items)
2833 gcc_checking_assert (offset >= 0);
2835 while (**aglat && (**aglat)->offset < offset)
2837 if ((**aglat)->offset + (**aglat)->size > offset)
2839 set_agg_lats_to_bottom (dest_plats);
2840 return false;
2842 *change |= (**aglat)->set_contains_variable ();
2843 *aglat = &(**aglat)->next;
2846 if (**aglat && (**aglat)->offset == offset)
2848 if ((**aglat)->size != val_size)
2850 set_agg_lats_to_bottom (dest_plats);
2851 return false;
2853 gcc_assert (!(**aglat)->next
2854 || (**aglat)->next->offset >= offset + val_size);
2855 return true;
2857 else
2859 struct ipcp_agg_lattice *new_al;
2861 if (**aglat && (**aglat)->offset < offset + val_size)
2863 set_agg_lats_to_bottom (dest_plats);
2864 return false;
2866 if (dest_plats->aggs_count == max_agg_items)
2867 return false;
2868 dest_plats->aggs_count++;
2869 new_al = ipcp_agg_lattice_pool.allocate ();
2870 memset (new_al, 0, sizeof (*new_al));
2872 new_al->offset = offset;
2873 new_al->size = val_size;
2874 new_al->contains_variable = pre_existing;
2876 new_al->next = **aglat;
2877 **aglat = new_al;
2878 return true;
2882 /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
2883 containing an unknown value. */
2885 static bool
2886 set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
2888 bool ret = false;
2889 while (aglat)
2891 ret |= aglat->set_contains_variable ();
2892 aglat = aglat->next;
2894 return ret;
2897 /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
2898 DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
2899 parameter used for lattice value sources. Return true if DEST_PLATS changed
2900 in any way. */
2902 static bool
2903 merge_aggregate_lattices (struct cgraph_edge *cs,
2904 class ipcp_param_lattices *dest_plats,
2905 class ipcp_param_lattices *src_plats,
2906 int src_idx, HOST_WIDE_INT offset_delta)
2908 bool pre_existing = dest_plats->aggs != NULL;
2909 struct ipcp_agg_lattice **dst_aglat;
2910 bool ret = false;
2912 if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
2913 return true;
2914 if (src_plats->aggs_bottom)
2915 return set_agg_lats_contain_variable (dest_plats);
2916 if (src_plats->aggs_contain_variable)
2917 ret |= set_agg_lats_contain_variable (dest_plats);
2918 dst_aglat = &dest_plats->aggs;
2920 int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
2921 param_ipa_max_agg_items);
2922 for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
2923 src_aglat;
2924 src_aglat = src_aglat->next)
2926 HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
2928 if (new_offset < 0)
2929 continue;
2930 if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
2931 &dst_aglat, pre_existing, &ret, max_agg_items))
2933 struct ipcp_agg_lattice *new_al = *dst_aglat;
2935 dst_aglat = &(*dst_aglat)->next;
2936 if (src_aglat->bottom)
2938 ret |= new_al->set_contains_variable ();
2939 continue;
2941 if (src_aglat->contains_variable)
2942 ret |= new_al->set_contains_variable ();
2943 for (ipcp_value<tree> *val = src_aglat->values;
2944 val;
2945 val = val->next)
2946 ret |= new_al->add_value (val->value, cs, val, src_idx,
2947 src_aglat->offset);
2949 else if (dest_plats->aggs_bottom)
2950 return true;
2952 ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
2953 return ret;
2956 /* Determine whether there is anything to propagate FROM SRC_PLATS through a
2957 pass-through JFUNC and if so, whether it has conform and conforms to the
2958 rules about propagating values passed by reference. */
2960 static bool
2961 agg_pass_through_permissible_p (class ipcp_param_lattices *src_plats,
2962 struct ipa_jump_func *jfunc)
2964 return src_plats->aggs
2965 && (!src_plats->aggs_by_ref
2966 || ipa_get_jf_pass_through_agg_preserved (jfunc));
2969 /* Propagate values through ITEM, jump function for a part of an aggregate,
2970 into corresponding aggregate lattice AGLAT. CS is the call graph edge
2971 associated with the jump function. Return true if AGLAT changed in any
2972 way. */
2974 static bool
2975 propagate_aggregate_lattice (struct cgraph_edge *cs,
2976 struct ipa_agg_jf_item *item,
2977 struct ipcp_agg_lattice *aglat)
2979 class ipa_node_params *caller_info;
2980 class ipcp_param_lattices *src_plats;
2981 struct ipcp_lattice<tree> *src_lat;
2982 HOST_WIDE_INT src_offset;
2983 int src_idx;
2984 tree load_type;
2985 bool ret;
2987 if (item->jftype == IPA_JF_CONST)
2989 tree value = item->value.constant;
2991 gcc_checking_assert (is_gimple_ip_invariant (value));
2992 return aglat->add_value (value, cs, NULL, 0);
2995 gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
2996 || item->jftype == IPA_JF_LOAD_AGG);
2998 caller_info = ipa_node_params_sum->get (cs->caller);
2999 src_idx = item->value.pass_through.formal_id;
3000 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3002 if (item->jftype == IPA_JF_PASS_THROUGH)
3004 load_type = NULL_TREE;
3005 src_lat = &src_plats->itself;
3006 src_offset = -1;
3008 else
3010 HOST_WIDE_INT load_offset = item->value.load_agg.offset;
3011 struct ipcp_agg_lattice *src_aglat;
3013 for (src_aglat = src_plats->aggs; src_aglat; src_aglat = src_aglat->next)
3014 if (src_aglat->offset >= load_offset)
3015 break;
3017 load_type = item->value.load_agg.type;
3018 if (!src_aglat
3019 || src_aglat->offset > load_offset
3020 || src_aglat->size != tree_to_shwi (TYPE_SIZE (load_type))
3021 || src_plats->aggs_by_ref != item->value.load_agg.by_ref)
3022 return aglat->set_contains_variable ();
3024 src_lat = src_aglat;
3025 src_offset = load_offset;
3028 if (src_lat->bottom
3029 || (!ipcp_versionable_function_p (cs->caller)
3030 && !src_lat->is_single_const ()))
3031 return aglat->set_contains_variable ();
3033 ret = propagate_vals_across_arith_jfunc (cs,
3034 item->value.pass_through.operation,
3035 load_type,
3036 item->value.pass_through.operand,
3037 src_lat, aglat,
3038 src_offset,
3039 src_idx,
3040 item->type);
3042 if (src_lat->contains_variable)
3043 ret |= aglat->set_contains_variable ();
3045 return ret;
3048 /* Propagate scalar values across jump function JFUNC that is associated with
3049 edge CS and put the values into DEST_LAT. */
3051 static bool
3052 propagate_aggs_across_jump_function (struct cgraph_edge *cs,
3053 struct ipa_jump_func *jfunc,
3054 class ipcp_param_lattices *dest_plats)
3056 bool ret = false;
3058 if (dest_plats->aggs_bottom)
3059 return false;
3061 if (jfunc->type == IPA_JF_PASS_THROUGH
3062 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
3064 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
3065 int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
3066 class ipcp_param_lattices *src_plats;
3068 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3069 if (agg_pass_through_permissible_p (src_plats, jfunc))
3071 /* Currently we do not produce clobber aggregate jump
3072 functions, replace with merging when we do. */
3073 gcc_assert (!jfunc->agg.items);
3074 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
3075 src_idx, 0);
3076 return ret;
3079 else if (jfunc->type == IPA_JF_ANCESTOR
3080 && ipa_get_jf_ancestor_agg_preserved (jfunc))
3082 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
3083 int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
3084 class ipcp_param_lattices *src_plats;
3086 src_plats = ipa_get_parm_lattices (caller_info, src_idx);
3087 if (src_plats->aggs && src_plats->aggs_by_ref)
3089 /* Currently we do not produce clobber aggregate jump
3090 functions, replace with merging when we do. */
3091 gcc_assert (!jfunc->agg.items);
3092 ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
3093 ipa_get_jf_ancestor_offset (jfunc));
3095 else if (!src_plats->aggs_by_ref)
3096 ret |= set_agg_lats_to_bottom (dest_plats);
3097 else
3098 ret |= set_agg_lats_contain_variable (dest_plats);
3099 return ret;
3102 if (jfunc->agg.items)
3104 bool pre_existing = dest_plats->aggs != NULL;
3105 struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
3106 struct ipa_agg_jf_item *item;
3107 int i;
3109 if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
3110 return true;
3112 int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
3113 param_ipa_max_agg_items);
3114 FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
3116 HOST_WIDE_INT val_size;
3118 if (item->offset < 0 || item->jftype == IPA_JF_UNKNOWN)
3119 continue;
3120 val_size = tree_to_shwi (TYPE_SIZE (item->type));
3122 if (merge_agg_lats_step (dest_plats, item->offset, val_size,
3123 &aglat, pre_existing, &ret, max_agg_items))
3125 ret |= propagate_aggregate_lattice (cs, item, *aglat);
3126 aglat = &(*aglat)->next;
3128 else if (dest_plats->aggs_bottom)
3129 return true;
3132 ret |= set_chain_of_aglats_contains_variable (*aglat);
3134 else
3135 ret |= set_agg_lats_contain_variable (dest_plats);
3137 return ret;
3140 /* Return true if on the way cfrom CS->caller to the final (non-alias and
3141 non-thunk) destination, the call passes through a thunk. */
3143 static bool
3144 call_passes_through_thunk (cgraph_edge *cs)
3146 cgraph_node *alias_or_thunk = cs->callee;
3147 while (alias_or_thunk->alias)
3148 alias_or_thunk = alias_or_thunk->get_alias_target ();
3149 return alias_or_thunk->thunk;
3152 /* Propagate constants from the caller to the callee of CS. INFO describes the
3153 caller. */
3155 static bool
3156 propagate_constants_across_call (struct cgraph_edge *cs)
3158 class ipa_node_params *callee_info;
3159 enum availability availability;
3160 cgraph_node *callee;
3161 class ipa_edge_args *args;
3162 bool ret = false;
3163 int i, args_count, parms_count;
3165 callee = cs->callee->function_symbol (&availability);
3166 if (!callee->definition)
3167 return false;
3168 gcc_checking_assert (callee->has_gimple_body_p ());
3169 callee_info = ipa_node_params_sum->get (callee);
3170 if (!callee_info)
3171 return false;
3173 args = ipa_edge_args_sum->get (cs);
3174 parms_count = ipa_get_param_count (callee_info);
3175 if (parms_count == 0)
3176 return false;
3177 if (!args
3178 || !opt_for_fn (cs->caller->decl, flag_ipa_cp)
3179 || !opt_for_fn (cs->caller->decl, optimize))
3181 for (i = 0; i < parms_count; i++)
3182 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
3183 i));
3184 return ret;
3186 args_count = ipa_get_cs_argument_count (args);
3188 /* If this call goes through a thunk we must not propagate to the first (0th)
3189 parameter. However, we might need to uncover a thunk from below a series
3190 of aliases first. */
3191 if (call_passes_through_thunk (cs))
3193 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
3194 0));
3195 i = 1;
3197 else
3198 i = 0;
3200 for (; (i < args_count) && (i < parms_count); i++)
3202 struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
3203 class ipcp_param_lattices *dest_plats;
3204 tree param_type = ipa_get_type (callee_info, i);
3206 dest_plats = ipa_get_parm_lattices (callee_info, i);
3207 if (availability == AVAIL_INTERPOSABLE)
3208 ret |= set_all_contains_variable (dest_plats);
3209 else
3211 ret |= propagate_scalar_across_jump_function (cs, jump_func,
3212 &dest_plats->itself,
3213 param_type);
3214 ret |= propagate_context_across_jump_function (cs, jump_func, i,
3215 &dest_plats->ctxlat);
3217 |= propagate_bits_across_jump_function (cs, i, jump_func,
3218 &dest_plats->bits_lattice);
3219 ret |= propagate_aggs_across_jump_function (cs, jump_func,
3220 dest_plats);
3221 if (opt_for_fn (callee->decl, flag_ipa_vrp))
3222 ret |= propagate_vr_across_jump_function (cs, jump_func,
3223 dest_plats, param_type);
3224 else
3225 ret |= dest_plats->m_value_range.set_to_bottom ();
3228 for (; i < parms_count; i++)
3229 ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
3231 return ret;
3234 /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
3235 KNOWN_CONTEXTS, and known aggregates either in AVS or KNOWN_AGGS return
3236 the destination. The latter three can be NULL. If AGG_REPS is not NULL,
3237 KNOWN_AGGS is ignored. */
3239 static tree
3240 ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
3241 const vec<tree> &known_csts,
3242 const vec<ipa_polymorphic_call_context> &known_contexts,
3243 const ipa_argagg_value_list &avs,
3244 bool *speculative)
3246 int param_index = ie->indirect_info->param_index;
3247 HOST_WIDE_INT anc_offset;
3248 tree t = NULL;
3249 tree target = NULL;
3251 *speculative = false;
3253 if (param_index == -1)
3254 return NULL_TREE;
3256 if (!ie->indirect_info->polymorphic)
3258 tree t = NULL;
3260 if (ie->indirect_info->agg_contents)
3262 t = NULL;
3263 if ((unsigned) param_index < known_csts.length ()
3264 && known_csts[param_index])
3265 t = ipa_find_agg_cst_from_init (known_csts[param_index],
3266 ie->indirect_info->offset,
3267 ie->indirect_info->by_ref);
3269 if (!t && ie->indirect_info->guaranteed_unmodified)
3270 t = avs.get_value (param_index,
3271 ie->indirect_info->offset / BITS_PER_UNIT,
3272 ie->indirect_info->by_ref);
3274 else if ((unsigned) param_index < known_csts.length ())
3275 t = known_csts[param_index];
3277 if (t
3278 && TREE_CODE (t) == ADDR_EXPR
3279 && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
3280 return TREE_OPERAND (t, 0);
3281 else
3282 return NULL_TREE;
3285 if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
3286 return NULL_TREE;
3288 gcc_assert (!ie->indirect_info->agg_contents);
3289 gcc_assert (!ie->indirect_info->by_ref);
3290 anc_offset = ie->indirect_info->offset;
3292 t = NULL;
3294 if ((unsigned) param_index < known_csts.length ()
3295 && known_csts[param_index])
3296 t = ipa_find_agg_cst_from_init (known_csts[param_index],
3297 ie->indirect_info->offset, true);
3299 /* Try to work out value of virtual table pointer value in replacements. */
3300 /* or known aggregate values. */
3301 if (!t)
3302 t = avs.get_value (param_index,
3303 ie->indirect_info->offset / BITS_PER_UNIT,
3304 true);
3306 /* If we found the virtual table pointer, lookup the target. */
3307 if (t)
3309 tree vtable;
3310 unsigned HOST_WIDE_INT offset;
3311 if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
3313 bool can_refer;
3314 target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
3315 vtable, offset, &can_refer);
3316 if (can_refer)
3318 if (!target
3319 || fndecl_built_in_p (target, BUILT_IN_UNREACHABLE)
3320 || !possible_polymorphic_call_target_p
3321 (ie, cgraph_node::get (target)))
3323 /* Do not speculate builtin_unreachable, it is stupid! */
3324 if (ie->indirect_info->vptr_changed)
3325 return NULL;
3326 target = ipa_impossible_devirt_target (ie, target);
3328 *speculative = ie->indirect_info->vptr_changed;
3329 if (!*speculative)
3330 return target;
3335 /* Do we know the constant value of pointer? */
3336 if (!t && (unsigned) param_index < known_csts.length ())
3337 t = known_csts[param_index];
3339 gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
3341 ipa_polymorphic_call_context context;
3342 if (known_contexts.length () > (unsigned int) param_index)
3344 context = known_contexts[param_index];
3345 context.offset_by (anc_offset);
3346 if (ie->indirect_info->vptr_changed)
3347 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
3348 ie->indirect_info->otr_type);
3349 if (t)
3351 ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
3352 (t, ie->indirect_info->otr_type, anc_offset);
3353 if (!ctx2.useless_p ())
3354 context.combine_with (ctx2, ie->indirect_info->otr_type);
3357 else if (t)
3359 context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
3360 anc_offset);
3361 if (ie->indirect_info->vptr_changed)
3362 context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
3363 ie->indirect_info->otr_type);
3365 else
3366 return NULL_TREE;
3368 vec <cgraph_node *>targets;
3369 bool final;
3371 targets = possible_polymorphic_call_targets
3372 (ie->indirect_info->otr_type,
3373 ie->indirect_info->otr_token,
3374 context, &final);
3375 if (!final || targets.length () > 1)
3377 struct cgraph_node *node;
3378 if (*speculative)
3379 return target;
3380 if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
3381 || ie->speculative || !ie->maybe_hot_p ())
3382 return NULL;
3383 node = try_speculative_devirtualization (ie->indirect_info->otr_type,
3384 ie->indirect_info->otr_token,
3385 context);
3386 if (node)
3388 *speculative = true;
3389 target = node->decl;
3391 else
3392 return NULL;
3394 else
3396 *speculative = false;
3397 if (targets.length () == 1)
3398 target = targets[0]->decl;
3399 else
3400 target = ipa_impossible_devirt_target (ie, NULL_TREE);
3403 if (target && !possible_polymorphic_call_target_p (ie,
3404 cgraph_node::get (target)))
3406 if (*speculative)
3407 return NULL;
3408 target = ipa_impossible_devirt_target (ie, target);
3411 return target;
3414 /* If an indirect edge IE can be turned into a direct one based on data in
3415 AVALS, return the destination. Store into *SPECULATIVE a boolean determinig
3416 whether the discovered target is only speculative guess. */
3418 tree
3419 ipa_get_indirect_edge_target (struct cgraph_edge *ie,
3420 ipa_call_arg_values *avals,
3421 bool *speculative)
3423 ipa_argagg_value_list avl (avals);
3424 return ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
3425 avals->m_known_contexts,
3426 avl, speculative);
3429 /* Calculate devirtualization time bonus for NODE, assuming we know information
3430 about arguments stored in AVALS. */
3432 static int
3433 devirtualization_time_bonus (struct cgraph_node *node,
3434 ipa_auto_call_arg_values *avals)
3436 struct cgraph_edge *ie;
3437 int res = 0;
3439 for (ie = node->indirect_calls; ie; ie = ie->next_callee)
3441 struct cgraph_node *callee;
3442 class ipa_fn_summary *isummary;
3443 enum availability avail;
3444 tree target;
3445 bool speculative;
3447 ipa_argagg_value_list avl (avals);
3448 target = ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
3449 avals->m_known_contexts,
3450 avl, &speculative);
3451 if (!target)
3452 continue;
3454 /* Only bare minimum benefit for clearly un-inlineable targets. */
3455 res += 1;
3456 callee = cgraph_node::get (target);
3457 if (!callee || !callee->definition)
3458 continue;
3459 callee = callee->function_symbol (&avail);
3460 if (avail < AVAIL_AVAILABLE)
3461 continue;
3462 isummary = ipa_fn_summaries->get (callee);
3463 if (!isummary || !isummary->inlinable)
3464 continue;
3466 int size = ipa_size_summaries->get (callee)->size;
3467 /* FIXME: The values below need re-considering and perhaps also
3468 integrating into the cost metrics, at lest in some very basic way. */
3469 int max_inline_insns_auto
3470 = opt_for_fn (callee->decl, param_max_inline_insns_auto);
3471 if (size <= max_inline_insns_auto / 4)
3472 res += 31 / ((int)speculative + 1);
3473 else if (size <= max_inline_insns_auto / 2)
3474 res += 15 / ((int)speculative + 1);
3475 else if (size <= max_inline_insns_auto
3476 || DECL_DECLARED_INLINE_P (callee->decl))
3477 res += 7 / ((int)speculative + 1);
3480 return res;
3483 /* Return time bonus incurred because of hints stored in ESTIMATES. */
3485 static int
3486 hint_time_bonus (cgraph_node *node, const ipa_call_estimates &estimates)
3488 int result = 0;
3489 ipa_hints hints = estimates.hints;
3490 if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
3491 result += opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
3493 sreal bonus_for_one = opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
3495 if (hints & INLINE_HINT_loop_iterations)
3496 result += (estimates.loops_with_known_iterations * bonus_for_one).to_int ();
3498 if (hints & INLINE_HINT_loop_stride)
3499 result += (estimates.loops_with_known_strides * bonus_for_one).to_int ();
3501 return result;
3504 /* If there is a reason to penalize the function described by INFO in the
3505 cloning goodness evaluation, do so. */
3507 static inline sreal
3508 incorporate_penalties (cgraph_node *node, ipa_node_params *info,
3509 sreal evaluation)
3511 if (info->node_within_scc && !info->node_is_self_scc)
3512 evaluation = (evaluation
3513 * (100 - opt_for_fn (node->decl,
3514 param_ipa_cp_recursion_penalty))) / 100;
3516 if (info->node_calling_single_call)
3517 evaluation = (evaluation
3518 * (100 - opt_for_fn (node->decl,
3519 param_ipa_cp_single_call_penalty)))
3520 / 100;
3522 return evaluation;
3525 /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
3526 and SIZE_COST and with the sum of frequencies of incoming edges to the
3527 potential new clone in FREQUENCIES. */
3529 static bool
3530 good_cloning_opportunity_p (struct cgraph_node *node, sreal time_benefit,
3531 sreal freq_sum, profile_count count_sum,
3532 int size_cost)
3534 if (time_benefit == 0
3535 || !opt_for_fn (node->decl, flag_ipa_cp_clone)
3536 || node->optimize_for_size_p ())
3537 return false;
3539 gcc_assert (size_cost > 0);
3541 ipa_node_params *info = ipa_node_params_sum->get (node);
3542 int eval_threshold = opt_for_fn (node->decl, param_ipa_cp_eval_threshold);
3543 if (count_sum.nonzero_p ())
3545 gcc_assert (base_count.nonzero_p ());
3546 sreal factor = count_sum.probability_in (base_count).to_sreal ();
3547 sreal evaluation = (time_benefit * factor) / size_cost;
3548 evaluation = incorporate_penalties (node, info, evaluation);
3549 evaluation *= 1000;
3551 if (dump_file && (dump_flags & TDF_DETAILS))
3553 fprintf (dump_file, " good_cloning_opportunity_p (time: %g, "
3554 "size: %i, count_sum: ", time_benefit.to_double (),
3555 size_cost);
3556 count_sum.dump (dump_file);
3557 fprintf (dump_file, "%s%s) -> evaluation: %.2f, threshold: %i\n",
3558 info->node_within_scc
3559 ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
3560 info->node_calling_single_call ? ", single_call" : "",
3561 evaluation.to_double (), eval_threshold);
3564 return evaluation.to_int () >= eval_threshold;
3566 else
3568 sreal evaluation = (time_benefit * freq_sum) / size_cost;
3569 evaluation = incorporate_penalties (node, info, evaluation);
3570 evaluation *= 1000;
3572 if (dump_file && (dump_flags & TDF_DETAILS))
3573 fprintf (dump_file, " good_cloning_opportunity_p (time: %g, "
3574 "size: %i, freq_sum: %g%s%s) -> evaluation: %.2f, "
3575 "threshold: %i\n",
3576 time_benefit.to_double (), size_cost, freq_sum.to_double (),
3577 info->node_within_scc
3578 ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
3579 info->node_calling_single_call ? ", single_call" : "",
3580 evaluation.to_double (), eval_threshold);
3582 return evaluation.to_int () >= eval_threshold;
3586 /* Grow vectors in AVALS and fill them with information about values of
3587 parameters that are known to be independent of the context. Only calculate
3588 m_known_aggs if CALCULATE_AGGS is true. INFO describes the function. If
3589 REMOVABLE_PARAMS_COST is non-NULL, the movement cost of all removable
3590 parameters will be stored in it.
3592 TODO: Also grow context independent value range vectors. */
3594 static bool
3595 gather_context_independent_values (class ipa_node_params *info,
3596 ipa_auto_call_arg_values *avals,
3597 bool calculate_aggs,
3598 int *removable_params_cost)
3600 int i, count = ipa_get_param_count (info);
3601 bool ret = false;
3603 avals->m_known_vals.safe_grow_cleared (count, true);
3604 avals->m_known_contexts.safe_grow_cleared (count, true);
3606 if (removable_params_cost)
3607 *removable_params_cost = 0;
3609 for (i = 0; i < count; i++)
3611 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3612 ipcp_lattice<tree> *lat = &plats->itself;
3614 if (lat->is_single_const ())
3616 ipcp_value<tree> *val = lat->values;
3617 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
3618 avals->m_known_vals[i] = val->value;
3619 if (removable_params_cost)
3620 *removable_params_cost
3621 += estimate_move_cost (TREE_TYPE (val->value), false);
3622 ret = true;
3624 else if (removable_params_cost
3625 && !ipa_is_param_used (info, i))
3626 *removable_params_cost
3627 += ipa_get_param_move_cost (info, i);
3629 if (!ipa_is_param_used (info, i))
3630 continue;
3632 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3633 /* Do not account known context as reason for cloning. We can see
3634 if it permits devirtualization. */
3635 if (ctxlat->is_single_const ())
3636 avals->m_known_contexts[i] = ctxlat->values->value;
3638 if (calculate_aggs)
3639 ret |= push_agg_values_from_plats (plats, i, 0, &avals->m_known_aggs);
3642 return ret;
3645 /* Perform time and size measurement of NODE with the context given in AVALS,
3646 calculate the benefit compared to the node without specialization and store
3647 it into VAL. Take into account REMOVABLE_PARAMS_COST of all
3648 context-independent or unused removable parameters and EST_MOVE_COST, the
3649 estimated movement of the considered parameter. */
3651 static void
3652 perform_estimation_of_a_value (cgraph_node *node,
3653 ipa_auto_call_arg_values *avals,
3654 int removable_params_cost, int est_move_cost,
3655 ipcp_value_base *val)
3657 sreal time_benefit;
3658 ipa_call_estimates estimates;
3660 estimate_ipcp_clone_size_and_time (node, avals, &estimates);
3662 /* Extern inline functions have no cloning local time benefits because they
3663 will be inlined anyway. The only reason to clone them is if it enables
3664 optimization in any of the functions they call. */
3665 if (DECL_EXTERNAL (node->decl) && DECL_DECLARED_INLINE_P (node->decl))
3666 time_benefit = 0;
3667 else
3668 time_benefit = (estimates.nonspecialized_time - estimates.time)
3669 + (devirtualization_time_bonus (node, avals)
3670 + hint_time_bonus (node, estimates)
3671 + removable_params_cost + est_move_cost);
3673 int size = estimates.size;
3674 gcc_checking_assert (size >=0);
3675 /* The inliner-heuristics based estimates may think that in certain
3676 contexts some functions do not have any size at all but we want
3677 all specializations to have at least a tiny cost, not least not to
3678 divide by zero. */
3679 if (size == 0)
3680 size = 1;
3682 val->local_time_benefit = time_benefit;
3683 val->local_size_cost = size;
3686 /* Get the overall limit oof growth based on parameters extracted from growth.
3687 it does not really make sense to mix functions with different overall growth
3688 limits but it is possible and if it happens, we do not want to select one
3689 limit at random. */
3691 static long
3692 get_max_overall_size (cgraph_node *node)
3694 long max_new_size = orig_overall_size;
3695 long large_unit = opt_for_fn (node->decl, param_ipa_cp_large_unit_insns);
3696 if (max_new_size < large_unit)
3697 max_new_size = large_unit;
3698 int unit_growth = opt_for_fn (node->decl, param_ipa_cp_unit_growth);
3699 max_new_size += max_new_size * unit_growth / 100 + 1;
3700 return max_new_size;
3703 /* Iterate over known values of parameters of NODE and estimate the local
3704 effects in terms of time and size they have. */
3706 static void
3707 estimate_local_effects (struct cgraph_node *node)
3709 ipa_node_params *info = ipa_node_params_sum->get (node);
3710 int count = ipa_get_param_count (info);
3711 bool always_const;
3712 int removable_params_cost;
3714 if (!count || !ipcp_versionable_function_p (node))
3715 return;
3717 if (dump_file && (dump_flags & TDF_DETAILS))
3718 fprintf (dump_file, "\nEstimating effects for %s.\n", node->dump_name ());
3720 ipa_auto_call_arg_values avals;
3721 always_const = gather_context_independent_values (info, &avals, true,
3722 &removable_params_cost);
3723 int devirt_bonus = devirtualization_time_bonus (node, &avals);
3724 if (always_const || devirt_bonus
3725 || (removable_params_cost && node->can_change_signature))
3727 struct caller_statistics stats;
3728 ipa_call_estimates estimates;
3730 init_caller_stats (&stats);
3731 node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3732 false);
3733 estimate_ipcp_clone_size_and_time (node, &avals, &estimates);
3734 sreal time = estimates.nonspecialized_time - estimates.time;
3735 time += devirt_bonus;
3736 time += hint_time_bonus (node, estimates);
3737 time += removable_params_cost;
3738 int size = estimates.size - stats.n_calls * removable_params_cost;
3740 if (dump_file)
3741 fprintf (dump_file, " - context independent values, size: %i, "
3742 "time_benefit: %f\n", size, (time).to_double ());
3744 if (size <= 0 || node->local)
3746 info->do_clone_for_all_contexts = true;
3748 if (dump_file)
3749 fprintf (dump_file, " Decided to specialize for all "
3750 "known contexts, code not going to grow.\n");
3752 else if (good_cloning_opportunity_p (node, time, stats.freq_sum,
3753 stats.count_sum, size))
3755 if (size + overall_size <= get_max_overall_size (node))
3757 info->do_clone_for_all_contexts = true;
3758 overall_size += size;
3760 if (dump_file)
3761 fprintf (dump_file, " Decided to specialize for all "
3762 "known contexts, growth (to %li) deemed "
3763 "beneficial.\n", overall_size);
3765 else if (dump_file && (dump_flags & TDF_DETAILS))
3766 fprintf (dump_file, " Not cloning for all contexts because "
3767 "maximum unit size would be reached with %li.\n",
3768 size + overall_size);
3770 else if (dump_file && (dump_flags & TDF_DETAILS))
3771 fprintf (dump_file, " Not cloning for all contexts because "
3772 "!good_cloning_opportunity_p.\n");
3776 for (int i = 0; i < count; i++)
3778 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3779 ipcp_lattice<tree> *lat = &plats->itself;
3780 ipcp_value<tree> *val;
3782 if (lat->bottom
3783 || !lat->values
3784 || avals.m_known_vals[i])
3785 continue;
3787 for (val = lat->values; val; val = val->next)
3789 gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
3790 avals.m_known_vals[i] = val->value;
3792 int emc = estimate_move_cost (TREE_TYPE (val->value), true);
3793 perform_estimation_of_a_value (node, &avals, removable_params_cost,
3794 emc, val);
3796 if (dump_file && (dump_flags & TDF_DETAILS))
3798 fprintf (dump_file, " - estimates for value ");
3799 print_ipcp_constant_value (dump_file, val->value);
3800 fprintf (dump_file, " for ");
3801 ipa_dump_param (dump_file, info, i);
3802 fprintf (dump_file, ": time_benefit: %g, size: %i\n",
3803 val->local_time_benefit.to_double (),
3804 val->local_size_cost);
3807 avals.m_known_vals[i] = NULL_TREE;
3810 for (int i = 0; i < count; i++)
3812 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3814 if (!plats->virt_call)
3815 continue;
3817 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3818 ipcp_value<ipa_polymorphic_call_context> *val;
3820 if (ctxlat->bottom
3821 || !ctxlat->values
3822 || !avals.m_known_contexts[i].useless_p ())
3823 continue;
3825 for (val = ctxlat->values; val; val = val->next)
3827 avals.m_known_contexts[i] = val->value;
3828 perform_estimation_of_a_value (node, &avals, removable_params_cost,
3829 0, val);
3831 if (dump_file && (dump_flags & TDF_DETAILS))
3833 fprintf (dump_file, " - estimates for polymorphic context ");
3834 print_ipcp_constant_value (dump_file, val->value);
3835 fprintf (dump_file, " for ");
3836 ipa_dump_param (dump_file, info, i);
3837 fprintf (dump_file, ": time_benefit: %g, size: %i\n",
3838 val->local_time_benefit.to_double (),
3839 val->local_size_cost);
3842 avals.m_known_contexts[i] = ipa_polymorphic_call_context ();
3845 unsigned all_ctx_len = avals.m_known_aggs.length ();
3846 auto_vec<ipa_argagg_value, 32> all_ctx;
3847 all_ctx.reserve_exact (all_ctx_len);
3848 all_ctx.splice (avals.m_known_aggs);
3849 avals.m_known_aggs.safe_grow_cleared (all_ctx_len + 1);
3851 unsigned j = 0;
3852 for (int index = 0; index < count; index++)
3854 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, index);
3856 if (plats->aggs_bottom || !plats->aggs)
3857 continue;
3859 for (ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3861 ipcp_value<tree> *val;
3862 if (aglat->bottom || !aglat->values
3863 /* If the following is true, the one value is already part of all
3864 context estimations. */
3865 || (!plats->aggs_contain_variable
3866 && aglat->is_single_const ()))
3867 continue;
3869 unsigned unit_offset = aglat->offset / BITS_PER_UNIT;
3870 while (j < all_ctx_len
3871 && (all_ctx[j].index < index
3872 || (all_ctx[j].index == index
3873 && all_ctx[j].unit_offset < unit_offset)))
3875 avals.m_known_aggs[j] = all_ctx[j];
3876 j++;
3879 for (unsigned k = j; k < all_ctx_len; k++)
3880 avals.m_known_aggs[k+1] = all_ctx[k];
3882 for (val = aglat->values; val; val = val->next)
3884 avals.m_known_aggs[j].value = val->value;
3885 avals.m_known_aggs[j].unit_offset = unit_offset;
3886 avals.m_known_aggs[j].index = index;
3887 avals.m_known_aggs[j].by_ref = plats->aggs_by_ref;
3889 perform_estimation_of_a_value (node, &avals,
3890 removable_params_cost, 0, val);
3892 if (dump_file && (dump_flags & TDF_DETAILS))
3894 fprintf (dump_file, " - estimates for value ");
3895 print_ipcp_constant_value (dump_file, val->value);
3896 fprintf (dump_file, " for ");
3897 ipa_dump_param (dump_file, info, index);
3898 fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
3899 "]: time_benefit: %g, size: %i\n",
3900 plats->aggs_by_ref ? "ref " : "",
3901 aglat->offset,
3902 val->local_time_benefit.to_double (),
3903 val->local_size_cost);
3911 /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
3912 topological sort of values. */
3914 template <typename valtype>
3915 void
3916 value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
3918 ipcp_value_source<valtype> *src;
3920 if (cur_val->dfs)
3921 return;
3923 dfs_counter++;
3924 cur_val->dfs = dfs_counter;
3925 cur_val->low_link = dfs_counter;
3927 cur_val->topo_next = stack;
3928 stack = cur_val;
3929 cur_val->on_stack = true;
3931 for (src = cur_val->sources; src; src = src->next)
3932 if (src->val)
3934 if (src->val->dfs == 0)
3936 add_val (src->val);
3937 if (src->val->low_link < cur_val->low_link)
3938 cur_val->low_link = src->val->low_link;
3940 else if (src->val->on_stack
3941 && src->val->dfs < cur_val->low_link)
3942 cur_val->low_link = src->val->dfs;
3945 if (cur_val->dfs == cur_val->low_link)
3947 ipcp_value<valtype> *v, *scc_list = NULL;
3951 v = stack;
3952 stack = v->topo_next;
3953 v->on_stack = false;
3954 v->scc_no = cur_val->dfs;
3956 v->scc_next = scc_list;
3957 scc_list = v;
3959 while (v != cur_val);
3961 cur_val->topo_next = values_topo;
3962 values_topo = cur_val;
3966 /* Add all values in lattices associated with NODE to the topological sort if
3967 they are not there yet. */
3969 static void
3970 add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
3972 ipa_node_params *info = ipa_node_params_sum->get (node);
3973 int i, count = ipa_get_param_count (info);
3975 for (i = 0; i < count; i++)
3977 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3978 ipcp_lattice<tree> *lat = &plats->itself;
3979 struct ipcp_agg_lattice *aglat;
3981 if (!lat->bottom)
3983 ipcp_value<tree> *val;
3984 for (val = lat->values; val; val = val->next)
3985 topo->constants.add_val (val);
3988 if (!plats->aggs_bottom)
3989 for (aglat = plats->aggs; aglat; aglat = aglat->next)
3990 if (!aglat->bottom)
3992 ipcp_value<tree> *val;
3993 for (val = aglat->values; val; val = val->next)
3994 topo->constants.add_val (val);
3997 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3998 if (!ctxlat->bottom)
4000 ipcp_value<ipa_polymorphic_call_context> *ctxval;
4001 for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
4002 topo->contexts.add_val (ctxval);
4007 /* One pass of constants propagation along the call graph edges, from callers
4008 to callees (requires topological ordering in TOPO), iterate over strongly
4009 connected components. */
4011 static void
4012 propagate_constants_topo (class ipa_topo_info *topo)
4014 int i;
4016 for (i = topo->nnodes - 1; i >= 0; i--)
4018 unsigned j;
4019 struct cgraph_node *v, *node = topo->order[i];
4020 vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
4022 /* First, iteratively propagate within the strongly connected component
4023 until all lattices stabilize. */
4024 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
4025 if (v->has_gimple_body_p ())
4027 if (opt_for_fn (v->decl, flag_ipa_cp)
4028 && opt_for_fn (v->decl, optimize))
4029 push_node_to_stack (topo, v);
4030 /* When V is not optimized, we can not push it to stack, but
4031 still we need to set all its callees lattices to bottom. */
4032 else
4034 for (cgraph_edge *cs = v->callees; cs; cs = cs->next_callee)
4035 propagate_constants_across_call (cs);
4039 v = pop_node_from_stack (topo);
4040 while (v)
4042 struct cgraph_edge *cs;
4043 class ipa_node_params *info = NULL;
4044 bool self_scc = true;
4046 for (cs = v->callees; cs; cs = cs->next_callee)
4047 if (ipa_edge_within_scc (cs))
4049 cgraph_node *callee = cs->callee->function_symbol ();
4051 if (v != callee)
4052 self_scc = false;
4054 if (!info)
4056 info = ipa_node_params_sum->get (v);
4057 info->node_within_scc = true;
4060 if (propagate_constants_across_call (cs))
4061 push_node_to_stack (topo, callee);
4064 if (info)
4065 info->node_is_self_scc = self_scc;
4067 v = pop_node_from_stack (topo);
4070 /* Afterwards, propagate along edges leading out of the SCC, calculates
4071 the local effects of the discovered constants and all valid values to
4072 their topological sort. */
4073 FOR_EACH_VEC_ELT (cycle_nodes, j, v)
4074 if (v->has_gimple_body_p ()
4075 && opt_for_fn (v->decl, flag_ipa_cp)
4076 && opt_for_fn (v->decl, optimize))
4078 struct cgraph_edge *cs;
4080 estimate_local_effects (v);
4081 add_all_node_vals_to_toposort (v, topo);
4082 for (cs = v->callees; cs; cs = cs->next_callee)
4083 if (!ipa_edge_within_scc (cs))
4084 propagate_constants_across_call (cs);
4086 cycle_nodes.release ();
4090 /* Propagate the estimated effects of individual values along the topological
4091 from the dependent values to those they depend on. */
4093 template <typename valtype>
4094 void
4095 value_topo_info<valtype>::propagate_effects ()
4097 ipcp_value<valtype> *base;
4098 hash_set<ipcp_value<valtype> *> processed_srcvals;
4100 for (base = values_topo; base; base = base->topo_next)
4102 ipcp_value_source<valtype> *src;
4103 ipcp_value<valtype> *val;
4104 sreal time = 0;
4105 HOST_WIDE_INT size = 0;
4107 for (val = base; val; val = val->scc_next)
4109 time = time + val->local_time_benefit + val->prop_time_benefit;
4110 size = size + val->local_size_cost + val->prop_size_cost;
4113 for (val = base; val; val = val->scc_next)
4115 processed_srcvals.empty ();
4116 for (src = val->sources; src; src = src->next)
4117 if (src->val
4118 && src->cs->maybe_hot_p ())
4120 if (!processed_srcvals.add (src->val))
4122 HOST_WIDE_INT prop_size = size + src->val->prop_size_cost;
4123 if (prop_size < INT_MAX)
4124 src->val->prop_size_cost = prop_size;
4125 else
4126 continue;
4129 int special_factor = 1;
4130 if (val->same_scc (src->val))
4131 special_factor
4132 = opt_for_fn(src->cs->caller->decl,
4133 param_ipa_cp_recursive_freq_factor);
4134 else if (val->self_recursion_generated_p ()
4135 && (src->cs->callee->function_symbol ()
4136 == src->cs->caller))
4138 int max_recur_gen_depth
4139 = opt_for_fn(src->cs->caller->decl,
4140 param_ipa_cp_max_recursive_depth);
4141 special_factor = max_recur_gen_depth
4142 - val->self_recursion_generated_level + 1;
4145 src->val->prop_time_benefit
4146 += time * special_factor * src->cs->sreal_frequency ();
4149 if (size < INT_MAX)
4151 val->prop_time_benefit = time;
4152 val->prop_size_cost = size;
4154 else
4156 val->prop_time_benefit = 0;
4157 val->prop_size_cost = 0;
4163 /* Callback for qsort to sort counts of all edges. */
4165 static int
4166 compare_edge_profile_counts (const void *a, const void *b)
4168 const profile_count *cnt1 = (const profile_count *) a;
4169 const profile_count *cnt2 = (const profile_count *) b;
4171 if (*cnt1 < *cnt2)
4172 return 1;
4173 if (*cnt1 > *cnt2)
4174 return -1;
4175 return 0;
4179 /* Propagate constants, polymorphic contexts and their effects from the
4180 summaries interprocedurally. */
4182 static void
4183 ipcp_propagate_stage (class ipa_topo_info *topo)
4185 struct cgraph_node *node;
4187 if (dump_file)
4188 fprintf (dump_file, "\n Propagating constants:\n\n");
4190 base_count = profile_count::uninitialized ();
4192 bool compute_count_base = false;
4193 unsigned base_count_pos_percent = 0;
4194 FOR_EACH_DEFINED_FUNCTION (node)
4196 if (node->has_gimple_body_p ()
4197 && opt_for_fn (node->decl, flag_ipa_cp)
4198 && opt_for_fn (node->decl, optimize))
4200 ipa_node_params *info = ipa_node_params_sum->get (node);
4201 determine_versionability (node, info);
4203 unsigned nlattices = ipa_get_param_count (info);
4204 void *chunk = XCNEWVEC (class ipcp_param_lattices, nlattices);
4205 info->lattices = new (chunk) ipcp_param_lattices[nlattices];
4206 initialize_node_lattices (node);
4208 ipa_size_summary *s = ipa_size_summaries->get (node);
4209 if (node->definition && !node->alias && s != NULL)
4210 overall_size += s->self_size;
4211 if (node->count.ipa ().initialized_p ())
4213 compute_count_base = true;
4214 unsigned pos_percent = opt_for_fn (node->decl,
4215 param_ipa_cp_profile_count_base);
4216 base_count_pos_percent = MAX (base_count_pos_percent, pos_percent);
4220 if (compute_count_base)
4222 auto_vec<profile_count> all_edge_counts;
4223 all_edge_counts.reserve_exact (symtab->edges_count);
4224 FOR_EACH_DEFINED_FUNCTION (node)
4225 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
4227 profile_count count = cs->count.ipa ();
4228 if (!(count > profile_count::zero ()))
4229 continue;
4231 enum availability avail;
4232 cgraph_node *tgt
4233 = cs->callee->function_or_virtual_thunk_symbol (&avail);
4234 ipa_node_params *info = ipa_node_params_sum->get (tgt);
4235 if (info && info->versionable)
4236 all_edge_counts.quick_push (count);
4239 if (!all_edge_counts.is_empty ())
4241 gcc_assert (base_count_pos_percent <= 100);
4242 all_edge_counts.qsort (compare_edge_profile_counts);
4244 unsigned base_count_pos
4245 = ((all_edge_counts.length () * (base_count_pos_percent)) / 100);
4246 base_count = all_edge_counts[base_count_pos];
4248 if (dump_file)
4250 fprintf (dump_file, "\nSelected base_count from %u edges at "
4251 "position %u, arriving at: ", all_edge_counts.length (),
4252 base_count_pos);
4253 base_count.dump (dump_file);
4254 fprintf (dump_file, "\n");
4257 else if (dump_file)
4258 fprintf (dump_file, "\nNo candidates with non-zero call count found, "
4259 "continuing as if without profile feedback.\n");
4262 orig_overall_size = overall_size;
4264 if (dump_file)
4265 fprintf (dump_file, "\noverall_size: %li\n", overall_size);
4267 propagate_constants_topo (topo);
4268 if (flag_checking)
4269 ipcp_verify_propagated_values ();
4270 topo->constants.propagate_effects ();
4271 topo->contexts.propagate_effects ();
4273 if (dump_file)
4275 fprintf (dump_file, "\nIPA lattices after all propagation:\n");
4276 print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
4280 /* Discover newly direct outgoing edges from NODE which is a new clone with
4281 known KNOWN_CSTS and make them direct. */
4283 static void
4284 ipcp_discover_new_direct_edges (struct cgraph_node *node,
4285 vec<tree> known_csts,
4286 vec<ipa_polymorphic_call_context>
4287 known_contexts,
4288 vec<ipa_argagg_value, va_gc> *aggvals)
4290 struct cgraph_edge *ie, *next_ie;
4291 bool found = false;
4293 for (ie = node->indirect_calls; ie; ie = next_ie)
4295 tree target;
4296 bool speculative;
4298 next_ie = ie->next_callee;
4299 ipa_argagg_value_list avs (aggvals);
4300 target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
4301 avs, &speculative);
4302 if (target)
4304 bool agg_contents = ie->indirect_info->agg_contents;
4305 bool polymorphic = ie->indirect_info->polymorphic;
4306 int param_index = ie->indirect_info->param_index;
4307 struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
4308 speculative);
4309 found = true;
4311 if (cs && !agg_contents && !polymorphic)
4313 ipa_node_params *info = ipa_node_params_sum->get (node);
4314 int c = ipa_get_controlled_uses (info, param_index);
4315 if (c != IPA_UNDESCRIBED_USE
4316 && !ipa_get_param_load_dereferenced (info, param_index))
4318 struct ipa_ref *to_del;
4320 c--;
4321 ipa_set_controlled_uses (info, param_index, c);
4322 if (dump_file && (dump_flags & TDF_DETAILS))
4323 fprintf (dump_file, " controlled uses count of param "
4324 "%i bumped down to %i\n", param_index, c);
4325 if (c == 0
4326 && (to_del = node->find_reference (cs->callee, NULL, 0)))
4328 if (dump_file && (dump_flags & TDF_DETAILS))
4329 fprintf (dump_file, " and even removing its "
4330 "cloning-created reference\n");
4331 to_del->remove_reference ();
4337 /* Turning calls to direct calls will improve overall summary. */
4338 if (found)
4339 ipa_update_overall_fn_summary (node);
4342 class edge_clone_summary;
4343 static call_summary <edge_clone_summary *> *edge_clone_summaries = NULL;
4345 /* Edge clone summary. */
4347 class edge_clone_summary
4349 public:
4350 /* Default constructor. */
4351 edge_clone_summary (): prev_clone (NULL), next_clone (NULL) {}
4353 /* Default destructor. */
4354 ~edge_clone_summary ()
4356 if (prev_clone)
4357 edge_clone_summaries->get (prev_clone)->next_clone = next_clone;
4358 if (next_clone)
4359 edge_clone_summaries->get (next_clone)->prev_clone = prev_clone;
4362 cgraph_edge *prev_clone;
4363 cgraph_edge *next_clone;
4366 class edge_clone_summary_t:
4367 public call_summary <edge_clone_summary *>
4369 public:
4370 edge_clone_summary_t (symbol_table *symtab):
4371 call_summary <edge_clone_summary *> (symtab)
4373 m_initialize_when_cloning = true;
4376 void duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
4377 edge_clone_summary *src_data,
4378 edge_clone_summary *dst_data) final override;
4381 /* Edge duplication hook. */
4383 void
4384 edge_clone_summary_t::duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
4385 edge_clone_summary *src_data,
4386 edge_clone_summary *dst_data)
4388 if (src_data->next_clone)
4389 edge_clone_summaries->get (src_data->next_clone)->prev_clone = dst_edge;
4390 dst_data->prev_clone = src_edge;
4391 dst_data->next_clone = src_data->next_clone;
4392 src_data->next_clone = dst_edge;
4395 /* Return true is CS calls DEST or its clone for all contexts. When
4396 ALLOW_RECURSION_TO_CLONE is false, also return false for self-recursive
4397 edges from/to an all-context clone. */
4399 static bool
4400 calls_same_node_or_its_all_contexts_clone_p (cgraph_edge *cs, cgraph_node *dest,
4401 bool allow_recursion_to_clone)
4403 enum availability availability;
4404 cgraph_node *callee = cs->callee->function_symbol (&availability);
4406 if (availability <= AVAIL_INTERPOSABLE)
4407 return false;
4408 if (callee == dest)
4409 return true;
4410 if (!allow_recursion_to_clone && cs->caller == callee)
4411 return false;
4413 ipa_node_params *info = ipa_node_params_sum->get (callee);
4414 return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
4417 /* Return true if edge CS does bring about the value described by SRC to
4418 DEST_VAL of node DEST or its clone for all contexts. */
4420 static bool
4421 cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
4422 cgraph_node *dest, ipcp_value<tree> *dest_val)
4424 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
4426 if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, !src->val)
4427 || caller_info->node_dead)
4428 return false;
4430 if (!src->val)
4431 return true;
4433 if (caller_info->ipcp_orig_node)
4435 tree t = NULL_TREE;
4436 if (src->offset == -1)
4437 t = caller_info->known_csts[src->index];
4438 else if (ipcp_transformation *ts
4439 = ipcp_get_transformation_summary (cs->caller))
4441 ipa_argagg_value_list avl (ts);
4442 t = avl.get_value (src->index, src->offset / BITS_PER_UNIT);
4444 return (t != NULL_TREE
4445 && values_equal_for_ipcp_p (src->val->value, t));
4447 else
4449 if (src->val == dest_val)
4450 return true;
4452 struct ipcp_agg_lattice *aglat;
4453 class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
4454 src->index);
4455 if (src->offset == -1)
4456 return (plats->itself.is_single_const ()
4457 && values_equal_for_ipcp_p (src->val->value,
4458 plats->itself.values->value));
4459 else
4461 if (plats->aggs_bottom || plats->aggs_contain_variable)
4462 return false;
4463 for (aglat = plats->aggs; aglat; aglat = aglat->next)
4464 if (aglat->offset == src->offset)
4465 return (aglat->is_single_const ()
4466 && values_equal_for_ipcp_p (src->val->value,
4467 aglat->values->value));
4469 return false;
4473 /* Return true if edge CS does bring about the value described by SRC to
4474 DST_VAL of node DEST or its clone for all contexts. */
4476 static bool
4477 cgraph_edge_brings_value_p (cgraph_edge *cs,
4478 ipcp_value_source<ipa_polymorphic_call_context> *src,
4479 cgraph_node *dest,
4480 ipcp_value<ipa_polymorphic_call_context> *)
4482 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
4484 if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, true)
4485 || caller_info->node_dead)
4486 return false;
4487 if (!src->val)
4488 return true;
4490 if (caller_info->ipcp_orig_node)
4491 return (caller_info->known_contexts.length () > (unsigned) src->index)
4492 && values_equal_for_ipcp_p (src->val->value,
4493 caller_info->known_contexts[src->index]);
4495 class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
4496 src->index);
4497 return plats->ctxlat.is_single_const ()
4498 && values_equal_for_ipcp_p (src->val->value,
4499 plats->ctxlat.values->value);
4502 /* Get the next clone in the linked list of clones of an edge. */
4504 static inline struct cgraph_edge *
4505 get_next_cgraph_edge_clone (struct cgraph_edge *cs)
4507 edge_clone_summary *s = edge_clone_summaries->get (cs);
4508 return s != NULL ? s->next_clone : NULL;
4511 /* Given VAL that is intended for DEST, iterate over all its sources and if any
4512 of them is viable and hot, return true. In that case, for those that still
4513 hold, add their edge frequency and their number and cumulative profile
4514 counts of self-ecursive and other edges into *FREQUENCY, *CALLER_COUNT,
4515 REC_COUNT_SUM and NONREC_COUNT_SUM respectively. */
4517 template <typename valtype>
4518 static bool
4519 get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
4520 sreal *freq_sum, int *caller_count,
4521 profile_count *rec_count_sum,
4522 profile_count *nonrec_count_sum)
4524 ipcp_value_source<valtype> *src;
4525 sreal freq = 0;
4526 int count = 0;
4527 profile_count rec_cnt = profile_count::zero ();
4528 profile_count nonrec_cnt = profile_count::zero ();
4529 bool hot = false;
4530 bool non_self_recursive = false;
4532 for (src = val->sources; src; src = src->next)
4534 struct cgraph_edge *cs = src->cs;
4535 while (cs)
4537 if (cgraph_edge_brings_value_p (cs, src, dest, val))
4539 count++;
4540 freq += cs->sreal_frequency ();
4541 hot |= cs->maybe_hot_p ();
4542 if (cs->caller != dest)
4544 non_self_recursive = true;
4545 if (cs->count.ipa ().initialized_p ())
4546 rec_cnt += cs->count.ipa ();
4548 else if (cs->count.ipa ().initialized_p ())
4549 nonrec_cnt += cs->count.ipa ();
4551 cs = get_next_cgraph_edge_clone (cs);
4555 /* If the only edges bringing a value are self-recursive ones, do not bother
4556 evaluating it. */
4557 if (!non_self_recursive)
4558 return false;
4560 *freq_sum = freq;
4561 *caller_count = count;
4562 *rec_count_sum = rec_cnt;
4563 *nonrec_count_sum = nonrec_cnt;
4565 if (!hot && ipa_node_params_sum->get (dest)->node_within_scc)
4567 struct cgraph_edge *cs;
4569 /* Cold non-SCC source edge could trigger hot recursive execution of
4570 function. Consider the case as hot and rely on following cost model
4571 computation to further select right one. */
4572 for (cs = dest->callers; cs; cs = cs->next_caller)
4573 if (cs->caller == dest && cs->maybe_hot_p ())
4574 return true;
4577 return hot;
4580 /* Given a NODE, and a set of its CALLERS, try to adjust order of the callers
4581 to let a non-self-recursive caller be the first element. Thus, we can
4582 simplify intersecting operations on values that arrive from all of these
4583 callers, especially when there exists self-recursive call. Return true if
4584 this kind of adjustment is possible. */
4586 static bool
4587 adjust_callers_for_value_intersection (vec<cgraph_edge *> &callers,
4588 cgraph_node *node)
4590 for (unsigned i = 0; i < callers.length (); i++)
4592 cgraph_edge *cs = callers[i];
4594 if (cs->caller != node)
4596 if (i > 0)
4598 callers[i] = callers[0];
4599 callers[0] = cs;
4601 return true;
4604 return false;
4607 /* Return a vector of incoming edges that do bring value VAL to node DEST. It
4608 is assumed their number is known and equal to CALLER_COUNT. */
4610 template <typename valtype>
4611 static vec<cgraph_edge *>
4612 gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
4613 int caller_count)
4615 ipcp_value_source<valtype> *src;
4616 vec<cgraph_edge *> ret;
4618 ret.create (caller_count);
4619 for (src = val->sources; src; src = src->next)
4621 struct cgraph_edge *cs = src->cs;
4622 while (cs)
4624 if (cgraph_edge_brings_value_p (cs, src, dest, val))
4625 ret.quick_push (cs);
4626 cs = get_next_cgraph_edge_clone (cs);
4630 if (caller_count > 1)
4631 adjust_callers_for_value_intersection (ret, dest);
4633 return ret;
4636 /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
4637 Return it or NULL if for some reason it cannot be created. FORCE_LOAD_REF
4638 should be set to true when the reference created for the constant should be
4639 a load one and not an address one because the corresponding parameter p is
4640 only used as *p. */
4642 static struct ipa_replace_map *
4643 get_replacement_map (class ipa_node_params *info, tree value, int parm_num,
4644 bool force_load_ref)
4646 struct ipa_replace_map *replace_map;
4648 replace_map = ggc_alloc<ipa_replace_map> ();
4649 if (dump_file)
4651 fprintf (dump_file, " replacing ");
4652 ipa_dump_param (dump_file, info, parm_num);
4654 fprintf (dump_file, " with const ");
4655 print_generic_expr (dump_file, value);
4657 if (force_load_ref)
4658 fprintf (dump_file, " - forcing load reference\n");
4659 else
4660 fprintf (dump_file, "\n");
4662 replace_map->parm_num = parm_num;
4663 replace_map->new_tree = value;
4664 replace_map->force_load_ref = force_load_ref;
4665 return replace_map;
4668 /* Dump new profiling counts of NODE. SPEC is true when NODE is a specialzied
4669 one, otherwise it will be referred to as the original node. */
4671 static void
4672 dump_profile_updates (cgraph_node *node, bool spec)
4674 if (spec)
4675 fprintf (dump_file, " setting count of the specialized node %s to ",
4676 node->dump_name ());
4677 else
4678 fprintf (dump_file, " setting count of the original node %s to ",
4679 node->dump_name ());
4681 node->count.dump (dump_file);
4682 fprintf (dump_file, "\n");
4683 for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
4685 fprintf (dump_file, " edge to %s has count ",
4686 cs->callee->dump_name ());
4687 cs->count.dump (dump_file);
4688 fprintf (dump_file, "\n");
4692 /* With partial train run we do not want to assume that original's count is
4693 zero whenever we redurect all executed edges to clone. Simply drop profile
4694 to local one in this case. In eany case, return the new value. ORIG_NODE
4695 is the original node and its count has not been updaed yet. */
4697 profile_count
4698 lenient_count_portion_handling (profile_count remainder, cgraph_node *orig_node)
4700 if (remainder.ipa_p () && !remainder.ipa ().nonzero_p ()
4701 && orig_node->count.ipa_p () && orig_node->count.ipa ().nonzero_p ()
4702 && opt_for_fn (orig_node->decl, flag_profile_partial_training))
4703 remainder = remainder.guessed_local ();
4705 return remainder;
4708 /* Structure to sum counts coming from nodes other than the original node and
4709 its clones. */
4711 struct gather_other_count_struct
4713 cgraph_node *orig;
4714 profile_count other_count;
4717 /* Worker callback of call_for_symbol_thunks_and_aliases summing the number of
4718 counts that come from non-self-recursive calls.. */
4720 static bool
4721 gather_count_of_non_rec_edges (cgraph_node *node, void *data)
4723 gather_other_count_struct *desc = (gather_other_count_struct *) data;
4724 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4725 if (cs->caller != desc->orig && cs->caller->clone_of != desc->orig)
4726 desc->other_count += cs->count.ipa ();
4727 return false;
4730 /* Structure to help analyze if we need to boost counts of some clones of some
4731 non-recursive edges to match the new callee count. */
4733 struct desc_incoming_count_struct
4735 cgraph_node *orig;
4736 hash_set <cgraph_edge *> *processed_edges;
4737 profile_count count;
4738 unsigned unproc_orig_rec_edges;
4741 /* Go over edges calling NODE and its thunks and gather information about
4742 incoming counts so that we know if we need to make any adjustments. */
4744 static void
4745 analyze_clone_icoming_counts (cgraph_node *node,
4746 desc_incoming_count_struct *desc)
4748 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4749 if (cs->caller->thunk)
4751 analyze_clone_icoming_counts (cs->caller, desc);
4752 continue;
4754 else
4756 if (cs->count.initialized_p ())
4757 desc->count += cs->count.ipa ();
4758 if (!desc->processed_edges->contains (cs)
4759 && cs->caller->clone_of == desc->orig)
4760 desc->unproc_orig_rec_edges++;
4764 /* If caller edge counts of a clone created for a self-recursive arithmetic
4765 jump function must be adjusted because it is coming from a the "seed" clone
4766 for the first value and so has been excessively scaled back as if it was not
4767 a recursive call, adjust it so that the incoming counts of NODE match its
4768 count. NODE is the node or its thunk. */
4770 static void
4771 adjust_clone_incoming_counts (cgraph_node *node,
4772 desc_incoming_count_struct *desc)
4774 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4775 if (cs->caller->thunk)
4777 adjust_clone_incoming_counts (cs->caller, desc);
4778 profile_count sum = profile_count::zero ();
4779 for (cgraph_edge *e = cs->caller->callers; e; e = e->next_caller)
4780 if (e->count.initialized_p ())
4781 sum += e->count.ipa ();
4782 cs->count = cs->count.combine_with_ipa_count (sum);
4784 else if (!desc->processed_edges->contains (cs)
4785 && cs->caller->clone_of == desc->orig)
4787 cs->count += desc->count;
4788 if (dump_file)
4790 fprintf (dump_file, " Adjusted count of an incoming edge of "
4791 "a clone %s -> %s to ", cs->caller->dump_name (),
4792 cs->callee->dump_name ());
4793 cs->count.dump (dump_file);
4794 fprintf (dump_file, "\n");
4799 /* When ORIG_NODE has been cloned for values which have been generated fora
4800 self-recursive call as a result of an arithmetic pass-through
4801 jump-functions, adjust its count together with counts of all such clones in
4802 SELF_GEN_CLONES which also at this point contains ORIG_NODE itself.
4804 The function sums the counts of the original node and all its clones that
4805 cannot be attributed to a specific clone because it comes from a
4806 non-recursive edge. This sum is then evenly divided between the clones and
4807 on top of that each one gets all the counts which can be attributed directly
4808 to it. */
4810 static void
4811 update_counts_for_self_gen_clones (cgraph_node *orig_node,
4812 const vec<cgraph_node *> &self_gen_clones)
4814 profile_count redist_sum = orig_node->count.ipa ();
4815 if (!(redist_sum > profile_count::zero ()))
4816 return;
4818 if (dump_file)
4819 fprintf (dump_file, " Updating profile of self recursive clone "
4820 "series\n");
4822 gather_other_count_struct gocs;
4823 gocs.orig = orig_node;
4824 gocs.other_count = profile_count::zero ();
4826 auto_vec <profile_count, 8> other_edges_count;
4827 for (cgraph_node *n : self_gen_clones)
4829 gocs.other_count = profile_count::zero ();
4830 n->call_for_symbol_thunks_and_aliases (gather_count_of_non_rec_edges,
4831 &gocs, false);
4832 other_edges_count.safe_push (gocs.other_count);
4833 redist_sum -= gocs.other_count;
4836 hash_set<cgraph_edge *> processed_edges;
4837 unsigned i = 0;
4838 for (cgraph_node *n : self_gen_clones)
4840 profile_count orig_count = n->count;
4841 profile_count new_count
4842 = (redist_sum / self_gen_clones.length () + other_edges_count[i]);
4843 new_count = lenient_count_portion_handling (new_count, orig_node);
4844 n->count = new_count;
4845 profile_count::adjust_for_ipa_scaling (&new_count, &orig_count);
4846 for (cgraph_edge *cs = n->callees; cs; cs = cs->next_callee)
4848 cs->count = cs->count.apply_scale (new_count, orig_count);
4849 processed_edges.add (cs);
4851 for (cgraph_edge *cs = n->indirect_calls; cs; cs = cs->next_callee)
4852 cs->count = cs->count.apply_scale (new_count, orig_count);
4854 i++;
4857 /* There are still going to be edges to ORIG_NODE that have one or more
4858 clones coming from another node clone in SELF_GEN_CLONES and which we
4859 scaled by the same amount, which means that the total incoming sum of
4860 counts to ORIG_NODE will be too high, scale such edges back. */
4861 for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee)
4863 if (cs->callee->ultimate_alias_target () == orig_node)
4865 unsigned den = 0;
4866 for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
4867 if (e->callee->ultimate_alias_target () == orig_node
4868 && processed_edges.contains (e))
4869 den++;
4870 if (den > 0)
4871 for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
4872 if (e->callee->ultimate_alias_target () == orig_node
4873 && processed_edges.contains (e))
4874 e->count /= den;
4878 /* Edges from the seeds of the valus generated for arithmetic jump-functions
4879 along self-recursive edges are likely to have fairly low count and so
4880 edges from them to nodes in the self_gen_clones do not correspond to the
4881 artificially distributed count of the nodes, the total sum of incoming
4882 edges to some clones might be too low. Detect this situation and correct
4883 it. */
4884 for (cgraph_node *n : self_gen_clones)
4886 if (!(n->count.ipa () > profile_count::zero ()))
4887 continue;
4889 desc_incoming_count_struct desc;
4890 desc.orig = orig_node;
4891 desc.processed_edges = &processed_edges;
4892 desc.count = profile_count::zero ();
4893 desc.unproc_orig_rec_edges = 0;
4894 analyze_clone_icoming_counts (n, &desc);
4896 if (n->count.differs_from_p (desc.count))
4898 if (n->count > desc.count
4899 && desc.unproc_orig_rec_edges > 0)
4901 desc.count = n->count - desc.count;
4902 desc.count = desc.count /= desc.unproc_orig_rec_edges;
4903 adjust_clone_incoming_counts (n, &desc);
4905 else if (dump_file)
4906 fprintf (dump_file,
4907 " Unable to fix up incoming counts for %s.\n",
4908 n->dump_name ());
4912 if (dump_file)
4913 for (cgraph_node *n : self_gen_clones)
4914 dump_profile_updates (n, n != orig_node);
4915 return;
4918 /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
4919 their profile information to reflect this. This function should not be used
4920 for clones generated for arithmetic pass-through jump functions on a
4921 self-recursive call graph edge, that situation is handled by
4922 update_counts_for_self_gen_clones. */
4924 static void
4925 update_profiling_info (struct cgraph_node *orig_node,
4926 struct cgraph_node *new_node)
4928 struct caller_statistics stats;
4929 profile_count new_sum;
4930 profile_count remainder, orig_node_count = orig_node->count.ipa ();
4932 if (!(orig_node_count > profile_count::zero ()))
4933 return;
4935 if (dump_file)
4937 fprintf (dump_file, " Updating profile from original count: ");
4938 orig_node_count.dump (dump_file);
4939 fprintf (dump_file, "\n");
4942 init_caller_stats (&stats, new_node);
4943 new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
4944 false);
4945 new_sum = stats.count_sum;
4947 if (new_sum > orig_node_count)
4949 /* TODO: Perhaps this should be gcc_unreachable ()? */
4950 remainder = profile_count::zero ().guessed_local ();
4952 else if (stats.rec_count_sum.nonzero_p ())
4954 int new_nonrec_calls = stats.n_nonrec_calls;
4955 /* There are self-recursive edges which are likely to bring in the
4956 majority of calls but which we must divide in between the original and
4957 new node. */
4958 init_caller_stats (&stats, orig_node);
4959 orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats,
4960 &stats, false);
4961 int orig_nonrec_calls = stats.n_nonrec_calls;
4962 profile_count orig_nonrec_call_count = stats.count_sum;
4964 if (orig_node->local)
4966 if (!orig_nonrec_call_count.nonzero_p ())
4968 if (dump_file)
4969 fprintf (dump_file, " The original is local and the only "
4970 "incoming edges from non-dead callers with nonzero "
4971 "counts are self-recursive, assuming it is cold.\n");
4972 /* The NEW_NODE count and counts of all its outgoing edges
4973 are still unmodified copies of ORIG_NODE's. Just clear
4974 the latter and bail out. */
4975 profile_count zero;
4976 if (opt_for_fn (orig_node->decl, flag_profile_partial_training))
4977 zero = profile_count::zero ().guessed_local ();
4978 else
4979 zero = profile_count::adjusted_zero ();
4980 orig_node->count = zero;
4981 for (cgraph_edge *cs = orig_node->callees;
4983 cs = cs->next_callee)
4984 cs->count = zero;
4985 for (cgraph_edge *cs = orig_node->indirect_calls;
4987 cs = cs->next_callee)
4988 cs->count = zero;
4989 return;
4992 else
4994 /* Let's behave as if there was another caller that accounts for all
4995 the calls that were either indirect or from other compilation
4996 units. */
4997 orig_nonrec_calls++;
4998 profile_count pretend_caller_count
4999 = (orig_node_count - new_sum - orig_nonrec_call_count
5000 - stats.rec_count_sum);
5001 orig_nonrec_call_count += pretend_caller_count;
5004 /* Divide all "unexplained" counts roughly proportionally to sums of
5005 counts of non-recursive calls.
5007 We put rather arbitrary limits on how many counts we claim because the
5008 number of non-self-recursive incoming count is only a rough guideline
5009 and there are cases (such as mcf) where using it blindly just takes
5010 too many. And if lattices are considered in the opposite order we
5011 could also take too few. */
5012 profile_count unexp = orig_node_count - new_sum - orig_nonrec_call_count;
5014 int limit_den = 2 * (orig_nonrec_calls + new_nonrec_calls);
5015 profile_count new_part
5016 = MAX(MIN (unexp.apply_scale (new_sum,
5017 new_sum + orig_nonrec_call_count),
5018 unexp.apply_scale (limit_den - 1, limit_den)),
5019 unexp.apply_scale (new_nonrec_calls, limit_den));
5020 if (dump_file)
5022 fprintf (dump_file, " Claiming ");
5023 new_part.dump (dump_file);
5024 fprintf (dump_file, " of unexplained ");
5025 unexp.dump (dump_file);
5026 fprintf (dump_file, " counts because of self-recursive "
5027 "calls\n");
5029 new_sum += new_part;
5030 remainder = lenient_count_portion_handling (orig_node_count - new_sum,
5031 orig_node);
5033 else
5034 remainder = lenient_count_portion_handling (orig_node_count - new_sum,
5035 orig_node);
5037 new_sum = orig_node_count.combine_with_ipa_count (new_sum);
5038 new_node->count = new_sum;
5039 orig_node->count = remainder;
5041 profile_count orig_new_node_count = orig_node_count;
5042 profile_count::adjust_for_ipa_scaling (&new_sum, &orig_new_node_count);
5043 for (cgraph_edge *cs = new_node->callees; cs; cs = cs->next_callee)
5044 cs->count = cs->count.apply_scale (new_sum, orig_new_node_count);
5045 for (cgraph_edge *cs = new_node->indirect_calls; cs; cs = cs->next_callee)
5046 cs->count = cs->count.apply_scale (new_sum, orig_new_node_count);
5048 profile_count::adjust_for_ipa_scaling (&remainder, &orig_node_count);
5049 for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee)
5050 cs->count = cs->count.apply_scale (remainder, orig_node_count);
5051 for (cgraph_edge *cs = orig_node->indirect_calls; cs; cs = cs->next_callee)
5052 cs->count = cs->count.apply_scale (remainder, orig_node_count);
5054 if (dump_file)
5056 dump_profile_updates (new_node, true);
5057 dump_profile_updates (orig_node, false);
5061 /* Update the respective profile of specialized NEW_NODE and the original
5062 ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
5063 have been redirected to the specialized version. */
5065 static void
5066 update_specialized_profile (struct cgraph_node *new_node,
5067 struct cgraph_node *orig_node,
5068 profile_count redirected_sum)
5070 struct cgraph_edge *cs;
5071 profile_count new_node_count, orig_node_count = orig_node->count;
5073 if (dump_file)
5075 fprintf (dump_file, " the sum of counts of redirected edges is ");
5076 redirected_sum.dump (dump_file);
5077 fprintf (dump_file, "\n");
5079 if (!(orig_node_count > profile_count::zero ()))
5080 return;
5082 gcc_assert (orig_node_count >= redirected_sum);
5084 new_node_count = new_node->count;
5085 new_node->count += redirected_sum;
5086 orig_node->count -= redirected_sum;
5088 for (cs = new_node->callees; cs; cs = cs->next_callee)
5089 cs->count += cs->count.apply_scale (redirected_sum, new_node_count);
5091 for (cs = orig_node->callees; cs; cs = cs->next_callee)
5093 profile_count dec = cs->count.apply_scale (redirected_sum,
5094 orig_node_count);
5095 cs->count -= dec;
5098 if (dump_file)
5100 dump_profile_updates (new_node, true);
5101 dump_profile_updates (orig_node, false);
5105 static void adjust_references_in_caller (cgraph_edge *cs,
5106 symtab_node *symbol, int index);
5108 /* Simple structure to pass a symbol and index (with same meaning as parameters
5109 of adjust_references_in_caller) through a void* parameter of a
5110 call_for_symbol_thunks_and_aliases callback. */
5111 struct symbol_and_index_together
5113 symtab_node *symbol;
5114 int index;
5117 /* Worker callback of call_for_symbol_thunks_and_aliases to recursively call
5118 adjust_references_in_caller on edges up in the call-graph, if necessary. */
5119 static bool
5120 adjust_refs_in_act_callers (struct cgraph_node *node, void *data)
5122 symbol_and_index_together *pack = (symbol_and_index_together *) data;
5123 for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
5124 if (!cs->caller->thunk)
5125 adjust_references_in_caller (cs, pack->symbol, pack->index);
5126 return false;
5129 /* At INDEX of a function being called by CS there is an ADDR_EXPR of a
5130 variable which is only dereferenced and which is represented by SYMBOL. See
5131 if we can remove ADDR reference in callers assosiated witht the call. */
5133 static void
5134 adjust_references_in_caller (cgraph_edge *cs, symtab_node *symbol, int index)
5136 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5137 ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, index);
5138 if (jfunc->type == IPA_JF_CONST)
5140 ipa_ref *to_del = cs->caller->find_reference (symbol, cs->call_stmt,
5141 cs->lto_stmt_uid);
5142 if (!to_del)
5143 return;
5144 to_del->remove_reference ();
5145 if (dump_file)
5146 fprintf (dump_file, " Removed a reference from %s to %s.\n",
5147 cs->caller->dump_name (), symbol->dump_name ());
5148 return;
5151 if (jfunc->type != IPA_JF_PASS_THROUGH
5152 || ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
5153 return;
5155 int fidx = ipa_get_jf_pass_through_formal_id (jfunc);
5156 cgraph_node *caller = cs->caller;
5157 ipa_node_params *caller_info = ipa_node_params_sum->get (caller);
5158 /* TODO: This consistency check may be too big and not really
5159 that useful. Consider removing it. */
5160 tree cst;
5161 if (caller_info->ipcp_orig_node)
5162 cst = caller_info->known_csts[fidx];
5163 else
5165 ipcp_lattice<tree> *lat = ipa_get_scalar_lat (caller_info, fidx);
5166 gcc_assert (lat->is_single_const ());
5167 cst = lat->values->value;
5169 gcc_assert (TREE_CODE (cst) == ADDR_EXPR
5170 && (symtab_node::get (get_base_address (TREE_OPERAND (cst, 0)))
5171 == symbol));
5173 int cuses = ipa_get_controlled_uses (caller_info, fidx);
5174 if (cuses == IPA_UNDESCRIBED_USE)
5175 return;
5176 gcc_assert (cuses > 0);
5177 cuses--;
5178 ipa_set_controlled_uses (caller_info, fidx, cuses);
5179 if (cuses)
5180 return;
5182 if (caller_info->ipcp_orig_node)
5184 /* Cloning machinery has created a reference here, we need to either
5185 remove it or change it to a read one. */
5186 ipa_ref *to_del = caller->find_reference (symbol, NULL, 0);
5187 if (to_del && to_del->use == IPA_REF_ADDR)
5189 to_del->remove_reference ();
5190 if (dump_file)
5191 fprintf (dump_file, " Removed a reference from %s to %s.\n",
5192 cs->caller->dump_name (), symbol->dump_name ());
5193 if (ipa_get_param_load_dereferenced (caller_info, fidx))
5195 caller->create_reference (symbol, IPA_REF_LOAD, NULL);
5196 if (dump_file)
5197 fprintf (dump_file,
5198 " ...and replaced it with LOAD one.\n");
5203 symbol_and_index_together pack;
5204 pack.symbol = symbol;
5205 pack.index = fidx;
5206 if (caller->can_change_signature)
5207 caller->call_for_symbol_thunks_and_aliases (adjust_refs_in_act_callers,
5208 &pack, true);
5212 /* Return true if we would like to remove a parameter from NODE when cloning it
5213 with KNOWN_CSTS scalar constants. */
5215 static bool
5216 want_remove_some_param_p (cgraph_node *node, vec<tree> known_csts)
5218 auto_vec<bool, 16> surviving;
5219 bool filled_vec = false;
5220 ipa_node_params *info = ipa_node_params_sum->get (node);
5221 int i, count = ipa_get_param_count (info);
5223 for (i = 0; i < count; i++)
5225 if (!known_csts[i] && ipa_is_param_used (info, i))
5226 continue;
5228 if (!filled_vec)
5230 clone_info *info = clone_info::get (node);
5231 if (!info || !info->param_adjustments)
5232 return true;
5233 info->param_adjustments->get_surviving_params (&surviving);
5234 filled_vec = true;
5236 if (surviving.length() < (unsigned) i && surviving[i])
5237 return true;
5239 return false;
5242 /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
5243 known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
5244 redirect all edges in CALLERS to it. */
5246 static struct cgraph_node *
5247 create_specialized_node (struct cgraph_node *node,
5248 vec<tree> known_csts,
5249 vec<ipa_polymorphic_call_context> known_contexts,
5250 vec<ipa_argagg_value, va_gc> *aggvals,
5251 vec<cgraph_edge *> &callers)
5253 ipa_node_params *new_info, *info = ipa_node_params_sum->get (node);
5254 vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
5255 vec<ipa_adjusted_param, va_gc> *new_params = NULL;
5256 struct cgraph_node *new_node;
5257 int i, count = ipa_get_param_count (info);
5258 clone_info *cinfo = clone_info::get (node);
5259 ipa_param_adjustments *old_adjustments = cinfo
5260 ? cinfo->param_adjustments : NULL;
5261 ipa_param_adjustments *new_adjustments;
5262 gcc_assert (!info->ipcp_orig_node);
5263 gcc_assert (node->can_change_signature
5264 || !old_adjustments);
5266 if (old_adjustments)
5268 /* At the moment all IPA optimizations should use the number of
5269 parameters of the prevailing decl as the m_always_copy_start.
5270 Handling any other value would complicate the code below, so for the
5271 time bing let's only assert it is so. */
5272 gcc_assert (old_adjustments->m_always_copy_start == count
5273 || old_adjustments->m_always_copy_start < 0);
5274 int old_adj_count = vec_safe_length (old_adjustments->m_adj_params);
5275 for (i = 0; i < old_adj_count; i++)
5277 ipa_adjusted_param *old_adj = &(*old_adjustments->m_adj_params)[i];
5278 if (!node->can_change_signature
5279 || old_adj->op != IPA_PARAM_OP_COPY
5280 || (!known_csts[old_adj->base_index]
5281 && ipa_is_param_used (info, old_adj->base_index)))
5283 ipa_adjusted_param new_adj = *old_adj;
5285 new_adj.prev_clone_adjustment = true;
5286 new_adj.prev_clone_index = i;
5287 vec_safe_push (new_params, new_adj);
5290 bool skip_return = old_adjustments->m_skip_return;
5291 new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
5292 ipa_param_adjustments (new_params, count,
5293 skip_return));
5295 else if (node->can_change_signature
5296 && want_remove_some_param_p (node, known_csts))
5298 ipa_adjusted_param adj;
5299 memset (&adj, 0, sizeof (adj));
5300 adj.op = IPA_PARAM_OP_COPY;
5301 for (i = 0; i < count; i++)
5302 if (!known_csts[i] && ipa_is_param_used (info, i))
5304 adj.base_index = i;
5305 adj.prev_clone_index = i;
5306 vec_safe_push (new_params, adj);
5308 new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
5309 ipa_param_adjustments (new_params, count, false));
5311 else
5312 new_adjustments = NULL;
5314 auto_vec<cgraph_edge *, 2> self_recursive_calls;
5315 for (i = callers.length () - 1; i >= 0; i--)
5317 cgraph_edge *cs = callers[i];
5318 if (cs->caller == node)
5320 self_recursive_calls.safe_push (cs);
5321 callers.unordered_remove (i);
5324 replace_trees = cinfo ? vec_safe_copy (cinfo->tree_map) : NULL;
5325 for (i = 0; i < count; i++)
5327 tree t = known_csts[i];
5328 if (!t)
5329 continue;
5331 gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
5333 bool load_ref = false;
5334 symtab_node *ref_symbol;
5335 if (TREE_CODE (t) == ADDR_EXPR)
5337 tree base = get_base_address (TREE_OPERAND (t, 0));
5338 if (TREE_CODE (base) == VAR_DECL
5339 && ipa_get_controlled_uses (info, i) == 0
5340 && ipa_get_param_load_dereferenced (info, i)
5341 && (ref_symbol = symtab_node::get (base)))
5343 load_ref = true;
5344 if (node->can_change_signature)
5345 for (cgraph_edge *caller : callers)
5346 adjust_references_in_caller (caller, ref_symbol, i);
5350 ipa_replace_map *replace_map = get_replacement_map (info, t, i, load_ref);
5351 if (replace_map)
5352 vec_safe_push (replace_trees, replace_map);
5355 unsigned &suffix_counter = clone_num_suffixes->get_or_insert (
5356 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (
5357 node->decl)));
5358 new_node = node->create_virtual_clone (callers, replace_trees,
5359 new_adjustments, "constprop",
5360 suffix_counter);
5361 suffix_counter++;
5363 bool have_self_recursive_calls = !self_recursive_calls.is_empty ();
5364 for (unsigned j = 0; j < self_recursive_calls.length (); j++)
5366 cgraph_edge *cs = get_next_cgraph_edge_clone (self_recursive_calls[j]);
5367 /* Cloned edges can disappear during cloning as speculation can be
5368 resolved, check that we have one and that it comes from the last
5369 cloning. */
5370 if (cs && cs->caller == new_node)
5371 cs->redirect_callee_duplicating_thunks (new_node);
5372 /* Any future code that would make more than one clone of an outgoing
5373 edge would confuse this mechanism, so let's check that does not
5374 happen. */
5375 gcc_checking_assert (!cs
5376 || !get_next_cgraph_edge_clone (cs)
5377 || get_next_cgraph_edge_clone (cs)->caller != new_node);
5379 if (have_self_recursive_calls)
5380 new_node->expand_all_artificial_thunks ();
5382 ipa_set_node_agg_value_chain (new_node, aggvals);
5383 for (const ipa_argagg_value &av : aggvals)
5384 new_node->maybe_create_reference (av.value, NULL);
5386 if (dump_file && (dump_flags & TDF_DETAILS))
5388 fprintf (dump_file, " the new node is %s.\n", new_node->dump_name ());
5389 if (known_contexts.exists ())
5391 for (i = 0; i < count; i++)
5392 if (!known_contexts[i].useless_p ())
5394 fprintf (dump_file, " known ctx %i is ", i);
5395 known_contexts[i].dump (dump_file);
5398 if (aggvals)
5400 fprintf (dump_file, " Aggregate replacements:");
5401 ipa_argagg_value_list avs (aggvals);
5402 avs.dump (dump_file);
5406 new_info = ipa_node_params_sum->get (new_node);
5407 new_info->ipcp_orig_node = node;
5408 new_node->ipcp_clone = true;
5409 new_info->known_csts = known_csts;
5410 new_info->known_contexts = known_contexts;
5412 ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts,
5413 aggvals);
5415 return new_node;
5418 /* Return true if JFUNC, which describes a i-th parameter of call CS, is a
5419 pass-through function to itself when the cgraph_node involved is not an
5420 IPA-CP clone. When SIMPLE is true, further check if JFUNC is a simple
5421 no-operation pass-through. */
5423 static bool
5424 self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i,
5425 bool simple = true)
5427 enum availability availability;
5428 if (cs->caller == cs->callee->function_symbol (&availability)
5429 && availability > AVAIL_INTERPOSABLE
5430 && jfunc->type == IPA_JF_PASS_THROUGH
5431 && (!simple || ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
5432 && ipa_get_jf_pass_through_formal_id (jfunc) == i
5433 && ipa_node_params_sum->get (cs->caller)
5434 && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
5435 return true;
5436 return false;
5439 /* Return true if JFUNC, which describes a part of an aggregate represented or
5440 pointed to by the i-th parameter of call CS, is a pass-through function to
5441 itself when the cgraph_node involved is not an IPA-CP clone.. When
5442 SIMPLE is true, further check if JFUNC is a simple no-operation
5443 pass-through. */
5445 static bool
5446 self_recursive_agg_pass_through_p (const cgraph_edge *cs,
5447 const ipa_agg_jf_item *jfunc,
5448 int i, bool simple = true)
5450 enum availability availability;
5451 if (cs->caller == cs->callee->function_symbol (&availability)
5452 && availability > AVAIL_INTERPOSABLE
5453 && jfunc->jftype == IPA_JF_LOAD_AGG
5454 && jfunc->offset == jfunc->value.load_agg.offset
5455 && (!simple || jfunc->value.pass_through.operation == NOP_EXPR)
5456 && jfunc->value.pass_through.formal_id == i
5457 && useless_type_conversion_p (jfunc->value.load_agg.type, jfunc->type)
5458 && ipa_node_params_sum->get (cs->caller)
5459 && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
5460 return true;
5461 return false;
5464 /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
5465 KNOWN_CSTS with constants that are also known for all of the CALLERS. */
5467 static void
5468 find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
5469 vec<tree> &known_csts,
5470 const vec<cgraph_edge *> &callers)
5472 ipa_node_params *info = ipa_node_params_sum->get (node);
5473 int i, count = ipa_get_param_count (info);
5475 for (i = 0; i < count; i++)
5477 struct cgraph_edge *cs;
5478 tree newval = NULL_TREE;
5479 int j;
5480 bool first = true;
5481 tree type = ipa_get_type (info, i);
5483 if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
5484 continue;
5486 FOR_EACH_VEC_ELT (callers, j, cs)
5488 struct ipa_jump_func *jump_func;
5489 tree t;
5491 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5492 if (!args
5493 || i >= ipa_get_cs_argument_count (args)
5494 || (i == 0
5495 && call_passes_through_thunk (cs)))
5497 newval = NULL_TREE;
5498 break;
5500 jump_func = ipa_get_ith_jump_func (args, i);
5502 /* Besides simple pass-through jump function, arithmetic jump
5503 function could also introduce argument-direct-pass-through for
5504 self-feeding recursive call. For example,
5506 fn (int i)
5508 fn (i & 1);
5511 Given that i is 0, recursive propagation via (i & 1) also gets
5512 0. */
5513 if (self_recursive_pass_through_p (cs, jump_func, i, false))
5515 gcc_assert (newval);
5516 t = ipa_get_jf_arith_result (
5517 ipa_get_jf_pass_through_operation (jump_func),
5518 newval,
5519 ipa_get_jf_pass_through_operand (jump_func),
5520 type);
5522 else
5523 t = ipa_value_from_jfunc (ipa_node_params_sum->get (cs->caller),
5524 jump_func, type);
5525 if (!t
5526 || (newval
5527 && !values_equal_for_ipcp_p (t, newval))
5528 || (!first && !newval))
5530 newval = NULL_TREE;
5531 break;
5533 else
5534 newval = t;
5535 first = false;
5538 if (newval)
5540 if (dump_file && (dump_flags & TDF_DETAILS))
5542 fprintf (dump_file, " adding an extra known scalar value ");
5543 print_ipcp_constant_value (dump_file, newval);
5544 fprintf (dump_file, " for ");
5545 ipa_dump_param (dump_file, info, i);
5546 fprintf (dump_file, "\n");
5549 known_csts[i] = newval;
5554 /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
5555 KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
5556 CALLERS. */
5558 static void
5559 find_more_contexts_for_caller_subset (cgraph_node *node,
5560 vec<ipa_polymorphic_call_context>
5561 *known_contexts,
5562 const vec<cgraph_edge *> &callers)
5564 ipa_node_params *info = ipa_node_params_sum->get (node);
5565 int i, count = ipa_get_param_count (info);
5567 for (i = 0; i < count; i++)
5569 cgraph_edge *cs;
5571 if (ipa_get_poly_ctx_lat (info, i)->bottom
5572 || (known_contexts->exists ()
5573 && !(*known_contexts)[i].useless_p ()))
5574 continue;
5576 ipa_polymorphic_call_context newval;
5577 bool first = true;
5578 int j;
5580 FOR_EACH_VEC_ELT (callers, j, cs)
5582 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5583 if (!args
5584 || i >= ipa_get_cs_argument_count (args))
5585 return;
5586 ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
5587 ipa_polymorphic_call_context ctx;
5588 ctx = ipa_context_from_jfunc (ipa_node_params_sum->get (cs->caller),
5589 cs, i, jfunc);
5590 if (first)
5592 newval = ctx;
5593 first = false;
5595 else
5596 newval.meet_with (ctx);
5597 if (newval.useless_p ())
5598 break;
5601 if (!newval.useless_p ())
5603 if (dump_file && (dump_flags & TDF_DETAILS))
5605 fprintf (dump_file, " adding an extra known polymorphic "
5606 "context ");
5607 print_ipcp_constant_value (dump_file, newval);
5608 fprintf (dump_file, " for ");
5609 ipa_dump_param (dump_file, info, i);
5610 fprintf (dump_file, "\n");
5613 if (!known_contexts->exists ())
5614 known_contexts->safe_grow_cleared (ipa_get_param_count (info),
5615 true);
5616 (*known_contexts)[i] = newval;
5622 /* Push all aggregate values coming along edge CS for parameter number INDEX to
5623 RES. If INTERIM is non-NULL, it contains the current interim state of
5624 collected aggregate values which can be used to compute values passed over
5625 self-recursive edges.
5627 This basically one iteration of push_agg_values_from_edge over one
5628 parameter, which allows for simpler early returns. */
5630 static void
5631 push_agg_values_for_index_from_edge (struct cgraph_edge *cs, int index,
5632 vec<ipa_argagg_value> *res,
5633 const ipa_argagg_value_list *interim)
5635 bool agg_values_from_caller = false;
5636 bool agg_jf_preserved = false;
5637 unsigned unit_delta = UINT_MAX;
5638 int src_idx = -1;
5639 ipa_jump_func *jfunc = ipa_get_ith_jump_func (ipa_edge_args_sum->get (cs),
5640 index);
5642 if (jfunc->type == IPA_JF_PASS_THROUGH
5643 && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
5645 agg_values_from_caller = true;
5646 agg_jf_preserved = ipa_get_jf_pass_through_agg_preserved (jfunc);
5647 src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
5648 unit_delta = 0;
5650 else if (jfunc->type == IPA_JF_ANCESTOR
5651 && ipa_get_jf_ancestor_agg_preserved (jfunc))
5653 agg_values_from_caller = true;
5654 agg_jf_preserved = true;
5655 src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
5656 unit_delta = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
5659 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
5660 if (agg_values_from_caller)
5662 if (caller_info->ipcp_orig_node)
5664 struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
5665 ipcp_transformation *ts
5666 = ipcp_get_transformation_summary (cs->caller);
5667 ipa_node_params *orig_info = ipa_node_params_sum->get (orig_node);
5668 ipcp_param_lattices *orig_plats
5669 = ipa_get_parm_lattices (orig_info, src_idx);
5670 if (ts
5671 && orig_plats->aggs
5672 && (agg_jf_preserved || !orig_plats->aggs_by_ref))
5674 ipa_argagg_value_list src (ts);
5675 src.push_adjusted_values (src_idx, index, unit_delta, res);
5676 return;
5679 else
5681 ipcp_param_lattices *src_plats
5682 = ipa_get_parm_lattices (caller_info, src_idx);
5683 if (src_plats->aggs
5684 && !src_plats->aggs_bottom
5685 && (agg_jf_preserved || !src_plats->aggs_by_ref))
5687 if (interim && self_recursive_pass_through_p (cs, jfunc, index))
5689 interim->push_adjusted_values (src_idx, index, unit_delta,
5690 res);
5691 return;
5693 if (!src_plats->aggs_contain_variable)
5695 push_agg_values_from_plats (src_plats, index, unit_delta,
5696 res);
5697 return;
5703 if (!jfunc->agg.items)
5704 return;
5705 bool first = true;
5706 unsigned prev_unit_offset = 0;
5707 for (const ipa_agg_jf_item &agg_jf : *jfunc->agg.items)
5709 tree value, srcvalue;
5710 /* Besides simple pass-through aggregate jump function, arithmetic
5711 aggregate jump function could also bring same aggregate value as
5712 parameter passed-in for self-feeding recursive call. For example,
5714 fn (int *i)
5716 int j = *i & 1;
5717 fn (&j);
5720 Given that *i is 0, recursive propagation via (*i & 1) also gets 0. */
5721 if (interim
5722 && self_recursive_agg_pass_through_p (cs, &agg_jf, index, false)
5723 && (srcvalue = interim->get_value(index,
5724 agg_jf.offset / BITS_PER_UNIT)))
5725 value = ipa_get_jf_arith_result (agg_jf.value.pass_through.operation,
5726 srcvalue,
5727 agg_jf.value.pass_through.operand,
5728 agg_jf.type);
5729 else
5730 value = ipa_agg_value_from_jfunc (caller_info, cs->caller,
5731 &agg_jf);
5732 if (value)
5734 struct ipa_argagg_value iav;
5735 iav.value = value;
5736 iav.unit_offset = agg_jf.offset / BITS_PER_UNIT;
5737 iav.index = index;
5738 iav.by_ref = jfunc->agg.by_ref;
5740 gcc_assert (first
5741 || iav.unit_offset > prev_unit_offset);
5742 prev_unit_offset = iav.unit_offset;
5743 first = false;
5745 res->safe_push (iav);
5748 return;
5751 /* Push all aggregate values coming along edge CS to RES. DEST_INFO is the
5752 description of ultimate callee of CS or the one it was cloned from (the
5753 summary where lattices are). If INTERIM is non-NULL, it contains the
5754 current interim state of collected aggregate values which can be used to
5755 compute values passed over self-recursive edges (if OPTIMIZE_SELF_RECURSION
5756 is true) and to skip values which clearly will not be part of intersection
5757 with INTERIM. */
5759 static void
5760 push_agg_values_from_edge (struct cgraph_edge *cs,
5761 ipa_node_params *dest_info,
5762 vec<ipa_argagg_value> *res,
5763 const ipa_argagg_value_list *interim,
5764 bool optimize_self_recursion)
5766 ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5767 if (!args)
5768 return;
5770 int count = MIN (ipa_get_param_count (dest_info),
5771 ipa_get_cs_argument_count (args));
5773 unsigned interim_index = 0;
5774 for (int index = 0; index < count; index++)
5776 if (interim)
5778 while (interim_index < interim->m_elts.size ()
5779 && interim->m_elts[interim_index].value
5780 && interim->m_elts[interim_index].index < index)
5781 interim_index++;
5782 if (interim_index >= interim->m_elts.size ()
5783 || interim->m_elts[interim_index].index > index)
5784 continue;
5787 ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, index);
5788 if (!ipa_is_param_used (dest_info, index)
5789 || plats->aggs_bottom)
5790 continue;
5791 push_agg_values_for_index_from_edge (cs, index, res,
5792 optimize_self_recursion ? interim
5793 : NULL);
5798 /* Look at edges in CALLERS and collect all known aggregate values that arrive
5799 from all of them. Return nullptr if there are none. */
5801 static struct vec<ipa_argagg_value, va_gc> *
5802 find_aggregate_values_for_callers_subset (struct cgraph_node *node,
5803 const vec<cgraph_edge *> &callers)
5805 ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5806 if (dest_info->ipcp_orig_node)
5807 dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
5809 /* gather_edges_for_value puts a non-recursive call into the first element of
5810 callers if it can. */
5811 auto_vec<ipa_argagg_value, 32> interim;
5812 push_agg_values_from_edge (callers[0], dest_info, &interim, NULL, true);
5814 unsigned valid_entries = interim.length ();
5815 if (!valid_entries)
5816 return nullptr;
5818 unsigned caller_count = callers.length();
5819 for (unsigned i = 1; i < caller_count; i++)
5821 auto_vec<ipa_argagg_value, 32> last;
5822 ipa_argagg_value_list avs (&interim);
5823 push_agg_values_from_edge (callers[i], dest_info, &last, &avs, true);
5825 valid_entries = intersect_argaggs_with (interim, last);
5826 if (!valid_entries)
5827 return nullptr;
5830 vec<ipa_argagg_value, va_gc> *res = NULL;
5831 vec_safe_reserve_exact (res, valid_entries);
5832 for (const ipa_argagg_value &av : interim)
5833 if (av.value)
5834 res->quick_push(av);
5835 gcc_checking_assert (res->length () == valid_entries);
5836 return res;
5839 /* Determine whether CS also brings all scalar values that the NODE is
5840 specialized for. */
5842 static bool
5843 cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
5844 struct cgraph_node *node)
5846 ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5847 int count = ipa_get_param_count (dest_info);
5848 class ipa_node_params *caller_info;
5849 class ipa_edge_args *args;
5850 int i;
5852 caller_info = ipa_node_params_sum->get (cs->caller);
5853 args = ipa_edge_args_sum->get (cs);
5854 for (i = 0; i < count; i++)
5856 struct ipa_jump_func *jump_func;
5857 tree val, t;
5859 val = dest_info->known_csts[i];
5860 if (!val)
5861 continue;
5863 if (i >= ipa_get_cs_argument_count (args))
5864 return false;
5865 jump_func = ipa_get_ith_jump_func (args, i);
5866 t = ipa_value_from_jfunc (caller_info, jump_func,
5867 ipa_get_type (dest_info, i));
5868 if (!t || !values_equal_for_ipcp_p (val, t))
5869 return false;
5871 return true;
5874 /* Determine whether CS also brings all aggregate values that NODE is
5875 specialized for. */
5877 static bool
5878 cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
5879 struct cgraph_node *node)
5881 ipcp_transformation *ts = ipcp_get_transformation_summary (node);
5882 if (!ts || vec_safe_is_empty (ts->m_agg_values))
5883 return true;
5885 const ipa_argagg_value_list existing (ts->m_agg_values);
5886 auto_vec<ipa_argagg_value, 32> edge_values;
5887 ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5888 gcc_checking_assert (dest_info->ipcp_orig_node);
5889 dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
5890 push_agg_values_from_edge (cs, dest_info, &edge_values, &existing, false);
5891 const ipa_argagg_value_list avl (&edge_values);
5892 return avl.superset_of_p (existing);
5895 /* Given an original NODE and a VAL for which we have already created a
5896 specialized clone, look whether there are incoming edges that still lead
5897 into the old node but now also bring the requested value and also conform to
5898 all other criteria such that they can be redirected the special node.
5899 This function can therefore redirect the final edge in a SCC. */
5901 template <typename valtype>
5902 static void
5903 perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
5905 ipcp_value_source<valtype> *src;
5906 profile_count redirected_sum = profile_count::zero ();
5908 for (src = val->sources; src; src = src->next)
5910 struct cgraph_edge *cs = src->cs;
5911 while (cs)
5913 if (cgraph_edge_brings_value_p (cs, src, node, val)
5914 && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
5915 && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
5917 if (dump_file)
5918 fprintf (dump_file, " - adding an extra caller %s of %s\n",
5919 cs->caller->dump_name (),
5920 val->spec_node->dump_name ());
5922 cs->redirect_callee_duplicating_thunks (val->spec_node);
5923 val->spec_node->expand_all_artificial_thunks ();
5924 if (cs->count.ipa ().initialized_p ())
5925 redirected_sum = redirected_sum + cs->count.ipa ();
5927 cs = get_next_cgraph_edge_clone (cs);
5931 if (redirected_sum.nonzero_p ())
5932 update_specialized_profile (val->spec_node, node, redirected_sum);
5935 /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
5937 static bool
5938 known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
5940 ipa_polymorphic_call_context *ctx;
5941 int i;
5943 FOR_EACH_VEC_ELT (known_contexts, i, ctx)
5944 if (!ctx->useless_p ())
5945 return true;
5946 return false;
5949 /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
5951 static vec<ipa_polymorphic_call_context>
5952 copy_useful_known_contexts (const vec<ipa_polymorphic_call_context> &known_contexts)
5954 if (known_contexts_useful_p (known_contexts))
5955 return known_contexts.copy ();
5956 else
5957 return vNULL;
5960 /* Copy known scalar values from AVALS into KNOWN_CSTS and modify the copy
5961 according to VAL and INDEX. If non-empty, replace KNOWN_CONTEXTS with its
5962 copy too. */
5964 static void
5965 copy_known_vectors_add_val (ipa_auto_call_arg_values *avals,
5966 vec<tree> *known_csts,
5967 vec<ipa_polymorphic_call_context> *known_contexts,
5968 ipcp_value<tree> *val, int index)
5970 *known_csts = avals->m_known_vals.copy ();
5971 *known_contexts = copy_useful_known_contexts (avals->m_known_contexts);
5972 (*known_csts)[index] = val->value;
5975 /* Copy known scalar values from AVALS into KNOWN_CSTS. Similarly, copy
5976 contexts to KNOWN_CONTEXTS and modify the copy according to VAL and
5977 INDEX. */
5979 static void
5980 copy_known_vectors_add_val (ipa_auto_call_arg_values *avals,
5981 vec<tree> *known_csts,
5982 vec<ipa_polymorphic_call_context> *known_contexts,
5983 ipcp_value<ipa_polymorphic_call_context> *val,
5984 int index)
5986 *known_csts = avals->m_known_vals.copy ();
5987 *known_contexts = avals->m_known_contexts.copy ();
5988 (*known_contexts)[index] = val->value;
5991 /* Return true if OFFSET indicates this was not an aggregate value or there is
5992 a replacement equivalent to VALUE, INDEX and OFFSET among those in the
5993 AGGVALS list. */
5995 DEBUG_FUNCTION bool
5996 ipcp_val_agg_replacement_ok_p (vec<ipa_argagg_value, va_gc> *aggvals,
5997 int index, HOST_WIDE_INT offset, tree value)
5999 if (offset == -1)
6000 return true;
6002 const ipa_argagg_value_list avl (aggvals);
6003 tree v = avl.get_value (index, offset / BITS_PER_UNIT);
6004 return v && values_equal_for_ipcp_p (v, value);
6007 /* Return true if offset is minus one because source of a polymorphic context
6008 cannot be an aggregate value. */
6010 DEBUG_FUNCTION bool
6011 ipcp_val_agg_replacement_ok_p (vec<ipa_argagg_value, va_gc> *,
6012 int , HOST_WIDE_INT offset,
6013 ipa_polymorphic_call_context)
6015 return offset == -1;
6018 /* Decide whether to create a special version of NODE for value VAL of
6019 parameter at the given INDEX. If OFFSET is -1, the value is for the
6020 parameter itself, otherwise it is stored at the given OFFSET of the
6021 parameter. AVALS describes the other already known values. SELF_GEN_CLONES
6022 is a vector which contains clones created for self-recursive calls with an
6023 arithmetic pass-through jump function. */
6025 template <typename valtype>
6026 static bool
6027 decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
6028 ipcp_value<valtype> *val, ipa_auto_call_arg_values *avals,
6029 vec<cgraph_node *> *self_gen_clones)
6031 int caller_count;
6032 sreal freq_sum;
6033 profile_count count_sum, rec_count_sum;
6034 vec<cgraph_edge *> callers;
6036 if (val->spec_node)
6038 perhaps_add_new_callers (node, val);
6039 return false;
6041 else if (val->local_size_cost + overall_size > get_max_overall_size (node))
6043 if (dump_file && (dump_flags & TDF_DETAILS))
6044 fprintf (dump_file, " Ignoring candidate value because "
6045 "maximum unit size would be reached with %li.\n",
6046 val->local_size_cost + overall_size);
6047 return false;
6049 else if (!get_info_about_necessary_edges (val, node, &freq_sum, &caller_count,
6050 &rec_count_sum, &count_sum))
6051 return false;
6053 if (!dbg_cnt (ipa_cp_values))
6054 return false;
6056 if (val->self_recursion_generated_p ())
6058 /* The edge counts in this case might not have been adjusted yet.
6059 Nevertleless, even if they were it would be only a guesswork which we
6060 can do now. The recursive part of the counts can be derived from the
6061 count of the original node anyway. */
6062 if (node->count.ipa ().nonzero_p ())
6064 unsigned dem = self_gen_clones->length () + 1;
6065 rec_count_sum = node->count.ipa () / dem;
6067 else
6068 rec_count_sum = profile_count::zero ();
6071 /* get_info_about_necessary_edges only sums up ipa counts. */
6072 count_sum += rec_count_sum;
6074 if (dump_file && (dump_flags & TDF_DETAILS))
6076 fprintf (dump_file, " - considering value ");
6077 print_ipcp_constant_value (dump_file, val->value);
6078 fprintf (dump_file, " for ");
6079 ipa_dump_param (dump_file, ipa_node_params_sum->get (node), index);
6080 if (offset != -1)
6081 fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
6082 fprintf (dump_file, " (caller_count: %i)\n", caller_count);
6085 if (!good_cloning_opportunity_p (node, val->local_time_benefit,
6086 freq_sum, count_sum,
6087 val->local_size_cost)
6088 && !good_cloning_opportunity_p (node, val->prop_time_benefit,
6089 freq_sum, count_sum, val->prop_size_cost))
6090 return false;
6092 if (dump_file)
6093 fprintf (dump_file, " Creating a specialized node of %s.\n",
6094 node->dump_name ());
6096 vec<tree> known_csts;
6097 vec<ipa_polymorphic_call_context> known_contexts;
6099 callers = gather_edges_for_value (val, node, caller_count);
6100 if (offset == -1)
6101 copy_known_vectors_add_val (avals, &known_csts, &known_contexts, val, index);
6102 else
6104 known_csts = avals->m_known_vals.copy ();
6105 known_contexts = copy_useful_known_contexts (avals->m_known_contexts);
6107 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
6108 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
6109 vec<ipa_argagg_value, va_gc> *aggvals
6110 = find_aggregate_values_for_callers_subset (node, callers);
6111 gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
6112 offset, val->value));
6113 val->spec_node = create_specialized_node (node, known_csts, known_contexts,
6114 aggvals, callers);
6116 if (val->self_recursion_generated_p ())
6117 self_gen_clones->safe_push (val->spec_node);
6118 else
6119 update_profiling_info (node, val->spec_node);
6121 callers.release ();
6122 overall_size += val->local_size_cost;
6123 if (dump_file && (dump_flags & TDF_DETAILS))
6124 fprintf (dump_file, " overall size reached %li\n",
6125 overall_size);
6127 /* TODO: If for some lattice there is only one other known value
6128 left, make a special node for it too. */
6130 return true;
6133 /* Decide whether and what specialized clones of NODE should be created. */
6135 static bool
6136 decide_whether_version_node (struct cgraph_node *node)
6138 ipa_node_params *info = ipa_node_params_sum->get (node);
6139 int i, count = ipa_get_param_count (info);
6140 bool ret = false;
6142 if (count == 0)
6143 return false;
6145 if (dump_file && (dump_flags & TDF_DETAILS))
6146 fprintf (dump_file, "\nEvaluating opportunities for %s.\n",
6147 node->dump_name ());
6149 auto_vec <cgraph_node *, 9> self_gen_clones;
6150 ipa_auto_call_arg_values avals;
6151 gather_context_independent_values (info, &avals, false, NULL);
6153 for (i = 0; i < count;i++)
6155 if (!ipa_is_param_used (info, i))
6156 continue;
6158 class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6159 ipcp_lattice<tree> *lat = &plats->itself;
6160 ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
6162 if (!lat->bottom
6163 && !avals.m_known_vals[i])
6165 ipcp_value<tree> *val;
6166 for (val = lat->values; val; val = val->next)
6168 /* If some values generated for self-recursive calls with
6169 arithmetic jump functions fall outside of the known
6170 value_range for the parameter, we can skip them. VR interface
6171 supports this only for integers now. */
6172 if (TREE_CODE (val->value) == INTEGER_CST
6173 && !plats->m_value_range.bottom_p ()
6174 && !plats->m_value_range.m_vr.contains_p (val->value))
6176 /* This can happen also if a constant present in the source
6177 code falls outside of the range of parameter's type, so we
6178 cannot assert. */
6179 if (dump_file && (dump_flags & TDF_DETAILS))
6181 fprintf (dump_file, " - skipping%s value ",
6182 val->self_recursion_generated_p ()
6183 ? " self_recursion_generated" : "");
6184 print_ipcp_constant_value (dump_file, val->value);
6185 fprintf (dump_file, " because it is outside known "
6186 "value range.\n");
6188 continue;
6190 ret |= decide_about_value (node, i, -1, val, &avals,
6191 &self_gen_clones);
6195 if (!plats->aggs_bottom)
6197 struct ipcp_agg_lattice *aglat;
6198 ipcp_value<tree> *val;
6199 for (aglat = plats->aggs; aglat; aglat = aglat->next)
6200 if (!aglat->bottom && aglat->values
6201 /* If the following is false, the one value has been considered
6202 for cloning for all contexts. */
6203 && (plats->aggs_contain_variable
6204 || !aglat->is_single_const ()))
6205 for (val = aglat->values; val; val = val->next)
6206 ret |= decide_about_value (node, i, aglat->offset, val, &avals,
6207 &self_gen_clones);
6210 if (!ctxlat->bottom
6211 && avals.m_known_contexts[i].useless_p ())
6213 ipcp_value<ipa_polymorphic_call_context> *val;
6214 for (val = ctxlat->values; val; val = val->next)
6215 ret |= decide_about_value (node, i, -1, val, &avals,
6216 &self_gen_clones);
6220 if (!self_gen_clones.is_empty ())
6222 self_gen_clones.safe_push (node);
6223 update_counts_for_self_gen_clones (node, self_gen_clones);
6226 if (info->do_clone_for_all_contexts)
6228 if (!dbg_cnt (ipa_cp_values))
6230 info->do_clone_for_all_contexts = false;
6231 return ret;
6234 struct cgraph_node *clone;
6235 auto_vec<cgraph_edge *> callers = node->collect_callers ();
6237 for (int i = callers.length () - 1; i >= 0; i--)
6239 cgraph_edge *cs = callers[i];
6240 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
6242 if (caller_info && caller_info->node_dead)
6243 callers.unordered_remove (i);
6246 if (!adjust_callers_for_value_intersection (callers, node))
6248 /* If node is not called by anyone, or all its caller edges are
6249 self-recursive, the node is not really in use, no need to do
6250 cloning. */
6251 info->do_clone_for_all_contexts = false;
6252 return ret;
6255 if (dump_file)
6256 fprintf (dump_file, " - Creating a specialized node of %s "
6257 "for all known contexts.\n", node->dump_name ());
6259 vec<tree> known_csts = avals.m_known_vals.copy ();
6260 vec<ipa_polymorphic_call_context> known_contexts
6261 = copy_useful_known_contexts (avals.m_known_contexts);
6262 find_more_scalar_values_for_callers_subset (node, known_csts, callers);
6263 find_more_contexts_for_caller_subset (node, &known_contexts, callers);
6264 vec<ipa_argagg_value, va_gc> *aggvals
6265 = find_aggregate_values_for_callers_subset (node, callers);
6267 if (!known_contexts_useful_p (known_contexts))
6269 known_contexts.release ();
6270 known_contexts = vNULL;
6272 clone = create_specialized_node (node, known_csts, known_contexts,
6273 aggvals, callers);
6274 info->do_clone_for_all_contexts = false;
6275 ipa_node_params_sum->get (clone)->is_all_contexts_clone = true;
6276 ret = true;
6279 return ret;
6282 /* Transitively mark all callees of NODE within the same SCC as not dead. */
6284 static void
6285 spread_undeadness (struct cgraph_node *node)
6287 struct cgraph_edge *cs;
6289 for (cs = node->callees; cs; cs = cs->next_callee)
6290 if (ipa_edge_within_scc (cs))
6292 struct cgraph_node *callee;
6293 class ipa_node_params *info;
6295 callee = cs->callee->function_symbol (NULL);
6296 info = ipa_node_params_sum->get (callee);
6298 if (info && info->node_dead)
6300 info->node_dead = 0;
6301 spread_undeadness (callee);
6306 /* Return true if NODE has a caller from outside of its SCC that is not
6307 dead. Worker callback for cgraph_for_node_and_aliases. */
6309 static bool
6310 has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
6311 void *data ATTRIBUTE_UNUSED)
6313 struct cgraph_edge *cs;
6315 for (cs = node->callers; cs; cs = cs->next_caller)
6316 if (cs->caller->thunk
6317 && cs->caller->call_for_symbol_thunks_and_aliases
6318 (has_undead_caller_from_outside_scc_p, NULL, true))
6319 return true;
6320 else if (!ipa_edge_within_scc (cs))
6322 ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
6323 if (!caller_info /* Unoptimized caller are like dead ones. */
6324 || !caller_info->node_dead)
6325 return true;
6327 return false;
6331 /* Identify nodes within the same SCC as NODE which are no longer needed
6332 because of new clones and will be removed as unreachable. */
6334 static void
6335 identify_dead_nodes (struct cgraph_node *node)
6337 struct cgraph_node *v;
6338 for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6339 if (v->local)
6341 ipa_node_params *info = ipa_node_params_sum->get (v);
6342 if (info
6343 && !v->call_for_symbol_thunks_and_aliases
6344 (has_undead_caller_from_outside_scc_p, NULL, true))
6345 info->node_dead = 1;
6348 for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6350 ipa_node_params *info = ipa_node_params_sum->get (v);
6351 if (info && !info->node_dead)
6352 spread_undeadness (v);
6355 if (dump_file && (dump_flags & TDF_DETAILS))
6357 for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6358 if (ipa_node_params_sum->get (v)
6359 && ipa_node_params_sum->get (v)->node_dead)
6360 fprintf (dump_file, " Marking node as dead: %s.\n",
6361 v->dump_name ());
6365 /* The decision stage. Iterate over the topological order of call graph nodes
6366 TOPO and make specialized clones if deemed beneficial. */
6368 static void
6369 ipcp_decision_stage (class ipa_topo_info *topo)
6371 int i;
6373 if (dump_file)
6374 fprintf (dump_file, "\nIPA decision stage:\n\n");
6376 for (i = topo->nnodes - 1; i >= 0; i--)
6378 struct cgraph_node *node = topo->order[i];
6379 bool change = false, iterate = true;
6381 while (iterate)
6383 struct cgraph_node *v;
6384 iterate = false;
6385 for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6386 if (v->has_gimple_body_p ()
6387 && ipcp_versionable_function_p (v))
6388 iterate |= decide_whether_version_node (v);
6390 change |= iterate;
6392 if (change)
6393 identify_dead_nodes (node);
6397 /* Look up all the bits information that we have discovered and copy it over
6398 to the transformation summary. */
6400 static void
6401 ipcp_store_bits_results (void)
6403 cgraph_node *node;
6405 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
6407 ipa_node_params *info = ipa_node_params_sum->get (node);
6408 bool dumped_sth = false;
6409 bool found_useful_result = false;
6411 if (!opt_for_fn (node->decl, flag_ipa_bit_cp) || !info)
6413 if (dump_file)
6414 fprintf (dump_file, "Not considering %s for ipa bitwise propagation "
6415 "; -fipa-bit-cp: disabled.\n",
6416 node->dump_name ());
6417 continue;
6420 if (info->ipcp_orig_node)
6421 info = ipa_node_params_sum->get (info->ipcp_orig_node);
6422 if (!info->lattices)
6423 /* Newly expanded artificial thunks do not have lattices. */
6424 continue;
6426 unsigned count = ipa_get_param_count (info);
6427 for (unsigned i = 0; i < count; i++)
6429 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6430 if (plats->bits_lattice.constant_p ())
6432 found_useful_result = true;
6433 break;
6437 if (!found_useful_result)
6438 continue;
6440 ipcp_transformation_initialize ();
6441 ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
6442 vec_safe_reserve_exact (ts->bits, count);
6444 for (unsigned i = 0; i < count; i++)
6446 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6447 ipa_bits *jfbits;
6449 if (plats->bits_lattice.constant_p ())
6451 jfbits
6452 = ipa_get_ipa_bits_for_value (plats->bits_lattice.get_value (),
6453 plats->bits_lattice.get_mask ());
6454 if (!dbg_cnt (ipa_cp_bits))
6455 jfbits = NULL;
6457 else
6458 jfbits = NULL;
6460 ts->bits->quick_push (jfbits);
6461 if (!dump_file || !jfbits)
6462 continue;
6463 if (!dumped_sth)
6465 fprintf (dump_file, "Propagated bits info for function %s:\n",
6466 node->dump_name ());
6467 dumped_sth = true;
6469 fprintf (dump_file, " param %i: value = ", i);
6470 print_hex (jfbits->value, dump_file);
6471 fprintf (dump_file, ", mask = ");
6472 print_hex (jfbits->mask, dump_file);
6473 fprintf (dump_file, "\n");
6478 /* Look up all VR information that we have discovered and copy it over
6479 to the transformation summary. */
6481 static void
6482 ipcp_store_vr_results (void)
6484 cgraph_node *node;
6486 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
6488 ipa_node_params *info = ipa_node_params_sum->get (node);
6489 bool found_useful_result = false;
6491 if (!info || !opt_for_fn (node->decl, flag_ipa_vrp))
6493 if (dump_file)
6494 fprintf (dump_file, "Not considering %s for VR discovery "
6495 "and propagate; -fipa-ipa-vrp: disabled.\n",
6496 node->dump_name ());
6497 continue;
6500 if (info->ipcp_orig_node)
6501 info = ipa_node_params_sum->get (info->ipcp_orig_node);
6502 if (!info->lattices)
6503 /* Newly expanded artificial thunks do not have lattices. */
6504 continue;
6506 unsigned count = ipa_get_param_count (info);
6507 for (unsigned i = 0; i < count; i++)
6509 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6510 if (!plats->m_value_range.bottom_p ()
6511 && !plats->m_value_range.top_p ())
6513 found_useful_result = true;
6514 break;
6517 if (!found_useful_result)
6518 continue;
6520 ipcp_transformation_initialize ();
6521 ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
6522 vec_safe_reserve_exact (ts->m_vr, count);
6524 for (unsigned i = 0; i < count; i++)
6526 ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6527 ipa_vr vr;
6529 if (!plats->m_value_range.bottom_p ()
6530 && !plats->m_value_range.top_p ()
6531 && dbg_cnt (ipa_cp_vr))
6533 vr.known = true;
6534 vr.type = plats->m_value_range.m_vr.kind ();
6535 vr.min = wi::to_wide (plats->m_value_range.m_vr.min ());
6536 vr.max = wi::to_wide (plats->m_value_range.m_vr.max ());
6538 else
6540 vr.known = false;
6541 vr.type = VR_VARYING;
6542 vr.min = vr.max = wi::zero (INT_TYPE_SIZE);
6544 ts->m_vr->quick_push (vr);
6549 /* The IPCP driver. */
6551 static unsigned int
6552 ipcp_driver (void)
6554 class ipa_topo_info topo;
6556 if (edge_clone_summaries == NULL)
6557 edge_clone_summaries = new edge_clone_summary_t (symtab);
6559 ipa_check_create_node_params ();
6560 ipa_check_create_edge_args ();
6561 clone_num_suffixes = new hash_map<const char *, unsigned>;
6563 if (dump_file)
6565 fprintf (dump_file, "\nIPA structures before propagation:\n");
6566 if (dump_flags & TDF_DETAILS)
6567 ipa_print_all_params (dump_file);
6568 ipa_print_all_jump_functions (dump_file);
6571 /* Topological sort. */
6572 build_toporder_info (&topo);
6573 /* Do the interprocedural propagation. */
6574 ipcp_propagate_stage (&topo);
6575 /* Decide what constant propagation and cloning should be performed. */
6576 ipcp_decision_stage (&topo);
6577 /* Store results of bits propagation. */
6578 ipcp_store_bits_results ();
6579 /* Store results of value range propagation. */
6580 ipcp_store_vr_results ();
6582 /* Free all IPCP structures. */
6583 delete clone_num_suffixes;
6584 free_toporder_info (&topo);
6585 delete edge_clone_summaries;
6586 edge_clone_summaries = NULL;
6587 ipa_free_all_structures_after_ipa_cp ();
6588 if (dump_file)
6589 fprintf (dump_file, "\nIPA constant propagation end\n");
6590 return 0;
6593 /* Initialization and computation of IPCP data structures. This is the initial
6594 intraprocedural analysis of functions, which gathers information to be
6595 propagated later on. */
6597 static void
6598 ipcp_generate_summary (void)
6600 struct cgraph_node *node;
6602 if (dump_file)
6603 fprintf (dump_file, "\nIPA constant propagation start:\n");
6604 ipa_register_cgraph_hooks ();
6606 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
6607 ipa_analyze_node (node);
6610 namespace {
6612 const pass_data pass_data_ipa_cp =
6614 IPA_PASS, /* type */
6615 "cp", /* name */
6616 OPTGROUP_NONE, /* optinfo_flags */
6617 TV_IPA_CONSTANT_PROP, /* tv_id */
6618 0, /* properties_required */
6619 0, /* properties_provided */
6620 0, /* properties_destroyed */
6621 0, /* todo_flags_start */
6622 ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
6625 class pass_ipa_cp : public ipa_opt_pass_d
6627 public:
6628 pass_ipa_cp (gcc::context *ctxt)
6629 : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
6630 ipcp_generate_summary, /* generate_summary */
6631 NULL, /* write_summary */
6632 NULL, /* read_summary */
6633 ipcp_write_transformation_summaries, /*
6634 write_optimization_summary */
6635 ipcp_read_transformation_summaries, /*
6636 read_optimization_summary */
6637 NULL, /* stmt_fixup */
6638 0, /* function_transform_todo_flags_start */
6639 ipcp_transform_function, /* function_transform */
6640 NULL) /* variable_transform */
6643 /* opt_pass methods: */
6644 bool gate (function *) final override
6646 /* FIXME: We should remove the optimize check after we ensure we never run
6647 IPA passes when not optimizing. */
6648 return (flag_ipa_cp && optimize) || in_lto_p;
6651 unsigned int execute (function *) final override { return ipcp_driver (); }
6653 }; // class pass_ipa_cp
6655 } // anon namespace
6657 ipa_opt_pass_d *
6658 make_pass_ipa_cp (gcc::context *ctxt)
6660 return new pass_ipa_cp (ctxt);
6663 /* Reset all state within ipa-cp.cc so that we can rerun the compiler
6664 within the same process. For use by toplev::finalize. */
6666 void
6667 ipa_cp_cc_finalize (void)
6669 base_count = profile_count::uninitialized ();
6670 overall_size = 0;
6671 orig_overall_size = 0;
6672 ipcp_free_transformation_sum ();