Branch data Line data Source code
1 : : /* Interprocedural constant propagation
2 : : Copyright (C) 2005-2025 Free Software Foundation, Inc.
3 : :
4 : : Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor
5 : : <mjambor@suse.cz>
6 : :
7 : : This file is part of GCC.
8 : :
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.
13 : :
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.
18 : :
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/>. */
22 : :
23 : : /* Interprocedural constant propagation (IPA-CP).
24 : :
25 : : The goal of this transformation is to
26 : :
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
30 : :
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.
35 : :
36 : : The algorithm also propagates types and attempts to perform type based
37 : : devirtualization. Types are propagated much like constants.
38 : :
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.
47 : :
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.
52 : :
53 : :
54 : : First stage - intraprocedural analysis
55 : : =======================================
56 : :
57 : : This phase computes jump_function and modification flags.
58 : :
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:
62 : :
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.
67 : :
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.
70 : :
71 : : ipcp_generate_summary() is the main function of the first stage.
72 : :
73 : : Second stage - interprocedural analysis
74 : : ========================================
75 : :
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.
79 : :
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.
86 : :
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.
92 : :
93 : : ipcp_propagate_stage() and ipcp_decision_stage() together constitute the
94 : : third stage.
95 : :
96 : : Third phase - materialization of clones, call statement updates.
97 : : ============================================
98 : :
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. */
102 : :
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 "sreal.h"
113 : : #include "alloc-pool.h"
114 : : #include "tree-pass.h"
115 : : #include "cgraph.h"
116 : : #include "diagnostic.h"
117 : : #include "fold-const.h"
118 : : #include "gimple-iterator.h"
119 : : #include "gimple-fold.h"
120 : : #include "symbol-summary.h"
121 : : #include "tree-vrp.h"
122 : : #include "ipa-cp.h"
123 : : #include "ipa-prop.h"
124 : : #include "tree-pretty-print.h"
125 : : #include "tree-inline.h"
126 : : #include "ipa-fnsummary.h"
127 : : #include "ipa-utils.h"
128 : : #include "tree-ssa-ccp.h"
129 : : #include "stringpool.h"
130 : : #include "attribs.h"
131 : : #include "dbgcnt.h"
132 : : #include "symtab-clones.h"
133 : : #include "gimple-range.h"
134 : :
135 : :
136 : : /* Allocation pools for values and their sources in ipa-cp. */
137 : :
138 : : object_allocator<ipcp_value<tree> > ipcp_cst_values_pool
139 : : ("IPA-CP constant values");
140 : :
141 : : object_allocator<ipcp_value<ipa_polymorphic_call_context> >
142 : : ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts");
143 : :
144 : : object_allocator<ipcp_value_source<tree> > ipcp_sources_pool
145 : : ("IPA-CP value sources");
146 : :
147 : : object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool
148 : : ("IPA_CP aggregate lattices");
149 : :
150 : : /* Original overall size of the program. */
151 : :
152 : : static long overall_size, orig_overall_size;
153 : :
154 : : /* Node name to unique clone suffix number map. */
155 : : static hash_map<const char *, unsigned> *clone_num_suffixes;
156 : :
157 : : /* Return the param lattices structure corresponding to the Ith formal
158 : : parameter of the function described by INFO. */
159 : : static inline class ipcp_param_lattices *
160 : 29518996 : ipa_get_parm_lattices (class ipa_node_params *info, int i)
161 : : {
162 : 59037992 : gcc_assert (i >= 0 && i < ipa_get_param_count (info));
163 : 29518996 : gcc_checking_assert (!info->ipcp_orig_node);
164 : 29518996 : return &(info->lattices[i]);
165 : : }
166 : :
167 : : /* Return the lattice corresponding to the scalar value of the Ith formal
168 : : parameter of the function described by INFO. */
169 : : static inline ipcp_lattice<tree> *
170 : 5571549 : ipa_get_scalar_lat (class ipa_node_params *info, int i)
171 : : {
172 : 5748191 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
173 : 5571549 : return &plats->itself;
174 : : }
175 : :
176 : : /* Return the lattice corresponding to the scalar value of the Ith formal
177 : : parameter of the function described by INFO. */
178 : : static inline ipcp_lattice<ipa_polymorphic_call_context> *
179 : 263232 : ipa_get_poly_ctx_lat (class ipa_node_params *info, int i)
180 : : {
181 : 263232 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
182 : 263232 : return &plats->ctxlat;
183 : : }
184 : :
185 : : /* Return whether LAT is a lattice with a single constant and without an
186 : : undefined value. */
187 : :
188 : : template <typename valtype>
189 : : inline bool
190 : 11066361 : ipcp_lattice<valtype>::is_single_const ()
191 : : {
192 : 1248717 : if (bottom || contains_variable || values_count != 1)
193 : : return false;
194 : : else
195 : : return true;
196 : : }
197 : :
198 : : /* Return true iff X and Y should be considered equal values by IPA-CP. */
199 : :
200 : : bool
201 : 861671 : values_equal_for_ipcp_p (tree x, tree y)
202 : : {
203 : 861671 : gcc_checking_assert (x != NULL_TREE && y != NULL_TREE);
204 : :
205 : 861671 : if (x == y)
206 : : return true;
207 : :
208 : 524818 : if (TREE_CODE (x) == ADDR_EXPR
209 : 156821 : && TREE_CODE (y) == ADDR_EXPR
210 : 156517 : && (TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL
211 : 136605 : || (TREE_CODE (TREE_OPERAND (x, 0)) == VAR_DECL
212 : 81043 : && DECL_IN_CONSTANT_POOL (TREE_OPERAND (x, 0))))
213 : 544730 : && (TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL
214 : 10 : || (TREE_CODE (TREE_OPERAND (y, 0)) == VAR_DECL
215 : 5 : && DECL_IN_CONSTANT_POOL (TREE_OPERAND (y, 0)))))
216 : 19902 : return TREE_OPERAND (x, 0) == TREE_OPERAND (y, 0)
217 : 39761 : || operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)),
218 : 19859 : DECL_INITIAL (TREE_OPERAND (y, 0)), 0);
219 : : else
220 : 504916 : return operand_equal_p (x, y, 0);
221 : : }
222 : :
223 : : /* Print V which is extracted from a value in a lattice to F. This overloaded
224 : : function is used to print tree constants. */
225 : :
226 : : static void
227 : 584 : print_ipcp_constant_value (FILE * f, tree v)
228 : : {
229 : 0 : ipa_print_constant_value (f, v);
230 : 0 : }
231 : :
232 : : /* Print V which is extracted from a value in a lattice to F. This overloaded
233 : : function is used to print constant polymorphic call contexts. */
234 : :
235 : : static void
236 : 193 : print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v)
237 : : {
238 : 193 : v.dump(f, false);
239 : 0 : }
240 : :
241 : : /* Print a lattice LAT to F. */
242 : :
243 : : template <typename valtype>
244 : : void
245 : 1914 : ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits)
246 : : {
247 : : ipcp_value<valtype> *val;
248 : 1914 : bool prev = false;
249 : :
250 : 1914 : if (bottom)
251 : : {
252 : 922 : fprintf (f, "BOTTOM\n");
253 : 922 : return;
254 : : }
255 : :
256 : 992 : if (!values_count && !contains_variable)
257 : : {
258 : 0 : fprintf (f, "TOP\n");
259 : 0 : return;
260 : : }
261 : :
262 : 992 : if (contains_variable)
263 : : {
264 : 738 : fprintf (f, "VARIABLE");
265 : 738 : prev = true;
266 : 738 : if (dump_benefits)
267 : 738 : fprintf (f, "\n");
268 : : }
269 : :
270 : 1548 : for (val = values; val; val = val->next)
271 : : {
272 : 556 : if (dump_benefits && prev)
273 : 302 : fprintf (f, " ");
274 : 254 : else if (!dump_benefits && prev)
275 : 0 : fprintf (f, ", ");
276 : : else
277 : : prev = true;
278 : :
279 : 556 : print_ipcp_constant_value (f, val->value);
280 : :
281 : 556 : if (dump_sources)
282 : : {
283 : : ipcp_value_source<valtype> *s;
284 : :
285 : 170 : if (val->self_recursion_generated_p ())
286 : 27 : fprintf (f, " [self_gen(%i), from:",
287 : : val->self_recursion_generated_level);
288 : : else
289 : 143 : fprintf (f, " [scc: %i, from:", val->scc_no);
290 : 358 : for (s = val->sources; s; s = s->next)
291 : 188 : fprintf (f, " %i(%f)", s->cs->caller->get_uid (),
292 : 376 : s->cs->sreal_frequency ().to_double ());
293 : 170 : fprintf (f, "]");
294 : : }
295 : :
296 : 556 : if (dump_benefits)
297 : 556 : fprintf (f, " [loc_time: %g, loc_size: %i, "
298 : : "prop_time: %g, prop_size: %i]\n",
299 : : val->local_time_benefit.to_double (), val->local_size_cost,
300 : : val->prop_time_benefit.to_double (), val->prop_size_cost);
301 : : }
302 : 992 : if (!dump_benefits)
303 : 0 : fprintf (f, "\n");
304 : : }
305 : :
306 : : /* Print VALUE to F in a form which in usual cases does not take thousands of
307 : : characters. */
308 : :
309 : : static void
310 : 1028 : ipcp_print_widest_int (FILE *f, const widest_int &value)
311 : : {
312 : 1028 : if (value == -1)
313 : 0 : fprintf (f, "-1");
314 : 1028 : else if (wi::arshift (value, 128) == -1)
315 : : {
316 : 229 : char buf[35], *p = buf + 2;
317 : 229 : widest_int v = wi::zext (value, 128);
318 : 229 : size_t len;
319 : 229 : print_hex (v, buf);
320 : 229 : len = strlen (p);
321 : 229 : if (len == 32)
322 : : {
323 : 229 : fprintf (f, "0xf..f");
324 : 6792 : while (*p == 'f')
325 : 6334 : ++p;
326 : : }
327 : : else
328 : 0 : fprintf (f, "0xf..f%0*d", (int) (32 - len), 0);
329 : 229 : fputs (p, f);
330 : 229 : }
331 : : else
332 : 799 : print_hex (value, f);
333 : 1028 : }
334 : :
335 : : void
336 : 895 : ipcp_bits_lattice::print (FILE *f)
337 : : {
338 : 895 : if (top_p ())
339 : 0 : fprintf (f, " Bits unknown (TOP)\n");
340 : 895 : else if (bottom_p ())
341 : 721 : fprintf (f, " Bits unusable (BOTTOM)\n");
342 : : else
343 : : {
344 : 174 : fprintf (f, " Bits: value = ");
345 : 174 : ipcp_print_widest_int (f, get_value ());
346 : 174 : fprintf (f, ", mask = ");
347 : 174 : ipcp_print_widest_int (f, get_mask ());
348 : 174 : fprintf (f, "\n");
349 : : }
350 : 895 : }
351 : :
352 : : /* Print value range lattice to F. */
353 : :
354 : : void
355 : 895 : ipcp_vr_lattice::print (FILE * f)
356 : : {
357 : 895 : m_vr.dump (f);
358 : 895 : }
359 : :
360 : : /* Print all ipcp_lattices of all functions to F. */
361 : :
362 : : static void
363 : 156 : print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits)
364 : : {
365 : 156 : struct cgraph_node *node;
366 : 156 : int i, count;
367 : :
368 : 156 : fprintf (f, "\nLattices:\n");
369 : 867 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
370 : : {
371 : 711 : class ipa_node_params *info;
372 : :
373 : 711 : info = ipa_node_params_sum->get (node);
374 : : /* Skip unoptimized functions and constprop clones since we don't make
375 : : lattices for them. */
376 : 711 : if (!info || info->ipcp_orig_node)
377 : 0 : continue;
378 : 711 : fprintf (f, " Node: %s:\n", node->dump_name ());
379 : 711 : count = ipa_get_param_count (info);
380 : 1606 : for (i = 0; i < count; i++)
381 : : {
382 : 895 : struct ipcp_agg_lattice *aglat;
383 : 895 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
384 : 895 : fprintf (f, " param [%d]: ", i);
385 : 895 : plats->itself.print (f, dump_sources, dump_benefits);
386 : 895 : fprintf (f, " ctxs: ");
387 : 895 : plats->ctxlat.print (f, dump_sources, dump_benefits);
388 : 895 : plats->bits_lattice.print (f);
389 : 895 : fprintf (f, " ");
390 : 895 : plats->m_value_range.print (f);
391 : 895 : fprintf (f, "\n");
392 : 895 : if (plats->virt_call)
393 : 75 : fprintf (f, " virt_call flag set\n");
394 : :
395 : 895 : if (plats->aggs_bottom)
396 : : {
397 : 478 : fprintf (f, " AGGS BOTTOM\n");
398 : 478 : continue;
399 : : }
400 : 417 : if (plats->aggs_contain_variable)
401 : 379 : fprintf (f, " AGGS VARIABLE\n");
402 : 541 : for (aglat = plats->aggs; aglat; aglat = aglat->next)
403 : : {
404 : 124 : fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ",
405 : 124 : plats->aggs_by_ref ? "ref " : "", aglat->offset);
406 : 124 : aglat->print (f, dump_sources, dump_benefits);
407 : : }
408 : : }
409 : : }
410 : 156 : }
411 : :
412 : : /* Determine whether it is at all technically possible to create clones of NODE
413 : : and store this information in the ipa_node_params structure associated
414 : : with NODE. */
415 : :
416 : : static void
417 : 1256761 : determine_versionability (struct cgraph_node *node,
418 : : class ipa_node_params *info)
419 : : {
420 : 1256761 : const char *reason = NULL;
421 : :
422 : : /* There are a number of generic reasons functions cannot be versioned. We
423 : : also cannot remove parameters if there are type attributes such as fnspec
424 : : present. */
425 : 1256761 : if (node->alias || node->thunk)
426 : : reason = "alias or thunk";
427 : 1256761 : else if (!node->versionable)
428 : : reason = "not a tree_versionable_function";
429 : 1129934 : else if (node->get_availability () <= AVAIL_INTERPOSABLE)
430 : : reason = "insufficient body availability";
431 : 1064093 : else if (!opt_for_fn (node->decl, optimize)
432 : 1064093 : || !opt_for_fn (node->decl, flag_ipa_cp))
433 : : reason = "non-optimized function";
434 : 1064093 : else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl)))
435 : : {
436 : : /* Ideally we should clone the SIMD clones themselves and create
437 : : vector copies of them, so IPA-cp and SIMD clones can happily
438 : : coexist, but that may not be worth the effort. */
439 : : reason = "function has SIMD clones";
440 : : }
441 : 1063740 : else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl)))
442 : : {
443 : : /* Ideally we should clone the target clones themselves and create
444 : : copies of them, so IPA-cp and target clones can happily
445 : : coexist, but that may not be worth the effort. */
446 : : reason = "function target_clones attribute";
447 : : }
448 : : /* Don't clone decls local to a comdat group; it breaks and for C++
449 : : decloned constructors, inlining is always better anyway. */
450 : 1063681 : else if (node->comdat_local_p ())
451 : : reason = "comdat-local function";
452 : 1061492 : else if (node->calls_comdat_local)
453 : : {
454 : : /* TODO: call is versionable if we make sure that all
455 : : callers are inside of a comdat group. */
456 : 2291 : reason = "calls comdat-local function";
457 : : }
458 : :
459 : : /* Functions calling BUILT_IN_VA_ARG_PACK and BUILT_IN_VA_ARG_PACK_LEN
460 : : work only when inlined. Cloning them may still lead to better code
461 : : because ipa-cp will not give up on cloning further. If the function is
462 : : external this however leads to wrong code because we may end up producing
463 : : offline copy of the function. */
464 : 1256761 : if (DECL_EXTERNAL (node->decl))
465 : 117882 : for (cgraph_edge *edge = node->callees; !reason && edge;
466 : 81922 : edge = edge->next_callee)
467 : 81922 : if (fndecl_built_in_p (edge->callee->decl, BUILT_IN_NORMAL))
468 : : {
469 : 16237 : if (DECL_FUNCTION_CODE (edge->callee->decl) == BUILT_IN_VA_ARG_PACK)
470 : 0 : reason = "external function which calls va_arg_pack";
471 : 16237 : if (DECL_FUNCTION_CODE (edge->callee->decl)
472 : : == BUILT_IN_VA_ARG_PACK_LEN)
473 : 0 : reason = "external function which calls va_arg_pack_len";
474 : : }
475 : :
476 : 1256761 : if (reason && dump_file && !node->alias && !node->thunk)
477 : 56 : fprintf (dump_file, "Function %s is not versionable, reason: %s.\n",
478 : : node->dump_name (), reason);
479 : :
480 : 1256761 : info->versionable = (reason == NULL);
481 : 1256761 : }
482 : :
483 : : /* Return true if it is at all technically possible to create clones of a
484 : : NODE. */
485 : :
486 : : static bool
487 : 3509624 : ipcp_versionable_function_p (struct cgraph_node *node)
488 : : {
489 : 3509624 : ipa_node_params *info = ipa_node_params_sum->get (node);
490 : 3509624 : return info && info->versionable;
491 : : }
492 : :
493 : : /* Structure holding accumulated information about callers of a node. */
494 : :
495 : 890482 : struct caller_statistics
496 : : {
497 : : /* If requested (see below), self-recursive call counts are summed into this
498 : : field. */
499 : : profile_count rec_count_sum;
500 : : /* The sum of all ipa counts of all the other (non-recursive) calls. */
501 : : profile_count count_sum;
502 : : /* Sum of all frequencies for all calls. */
503 : : sreal freq_sum;
504 : : /* Number of calls and calls considered interesting respectively. */
505 : : int n_calls, n_interesting_calls;
506 : : /* If itself is set up, also count the number of non-self-recursive
507 : : calls. */
508 : : int n_nonrec_calls;
509 : : /* If non-NULL, this is the node itself and calls from it should have their
510 : : counts included in rec_count_sum and not count_sum. */
511 : : cgraph_node *itself;
512 : : /* True if there is a caller that has no IPA profile. */
513 : : bool called_without_ipa_profile;
514 : : };
515 : :
516 : : /* Initialize fields of STAT to zeroes and optionally set it up so that edges
517 : : from IGNORED_CALLER are not counted. */
518 : :
519 : : static inline void
520 : 147999 : init_caller_stats (caller_statistics *stats, cgraph_node *itself = NULL)
521 : : {
522 : 147999 : stats->rec_count_sum = profile_count::zero ();
523 : 147999 : stats->count_sum = profile_count::zero ();
524 : 147999 : stats->n_calls = 0;
525 : 147999 : stats->n_interesting_calls = 0;
526 : 147999 : stats->n_nonrec_calls = 0;
527 : 147999 : stats->freq_sum = 0;
528 : 147999 : stats->itself = itself;
529 : 147999 : stats->called_without_ipa_profile = false;
530 : 147999 : }
531 : :
532 : : /* We want to propagate across edges that may be executed, however
533 : : we do not want to check maybe_hot, since call itself may be cold
534 : : while calee contains some heavy loop which makes propagation still
535 : : relevant.
536 : :
537 : : In particular, even edge called once may lead to significant
538 : : improvement. */
539 : :
540 : : static bool
541 : 460333 : cs_interesting_for_ipcp_p (cgraph_edge *e)
542 : : {
543 : : /* If profile says the edge is executed, we want to optimize. */
544 : 460333 : if (e->count.ipa ().nonzero_p ())
545 : 297 : return true;
546 : : /* If local (possibly guseed or adjusted 0 profile) claims edge is
547 : : not executed, do not propagate.
548 : : Do not trust AFDO since branch needs to be executed multiple
549 : : time to count while we want to propagate even call called
550 : : once during the train run if callee is important. */
551 : 460036 : if (e->count.initialized_p () && !e->count.nonzero_p ()
552 : 595321 : && e->count.quality () != AFDO)
553 : : return false;
554 : : /* If we have zero IPA profile, still consider edge for cloning
555 : : in case we do partial training. */
556 : 324751 : if (e->count.ipa ().initialized_p ()
557 : 324751 : && e->count.ipa ().quality () != AFDO
558 : 324754 : && !opt_for_fn (e->callee->decl,flag_profile_partial_training))
559 : 3 : return false;
560 : : return true;
561 : : }
562 : :
563 : : /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of
564 : : non-thunk incoming edges to NODE. */
565 : :
566 : : static bool
567 : 160199 : gather_caller_stats (struct cgraph_node *node, void *data)
568 : : {
569 : 160199 : struct caller_statistics *stats = (struct caller_statistics *) data;
570 : 160199 : struct cgraph_edge *cs;
571 : :
572 : 252797 : for (cs = node->callers; cs; cs = cs->next_caller)
573 : 92598 : if (!cs->caller->thunk)
574 : : {
575 : 92246 : ipa_node_params *info = ipa_node_params_sum->get (cs->caller);
576 : 92246 : if (info && info->node_dead)
577 : 0 : continue;
578 : :
579 : 92246 : if (cs->count.ipa ().initialized_p ())
580 : : {
581 : 3328 : if (stats->itself && stats->itself == cs->caller)
582 : 0 : stats->rec_count_sum += cs->count.ipa ();
583 : : else
584 : 3328 : stats->count_sum += cs->count.ipa ();
585 : : }
586 : : else
587 : 88918 : stats->called_without_ipa_profile = true;
588 : 92246 : stats->freq_sum += cs->sreal_frequency ();
589 : 92246 : stats->n_calls++;
590 : 92246 : if (stats->itself && stats->itself != cs->caller)
591 : 11 : stats->n_nonrec_calls++;
592 : :
593 : : /* If profile known to be zero, we do not want to clone for performance.
594 : : However if call is cold, the called function may still contain
595 : : important hot loops. */
596 : 92246 : if (cs_interesting_for_ipcp_p (cs))
597 : 88836 : stats->n_interesting_calls++;
598 : : }
599 : 160199 : return false;
600 : :
601 : : }
602 : :
603 : : /* Return true if this NODE is viable candidate for cloning. */
604 : :
605 : : static bool
606 : 786968 : ipcp_cloning_candidate_p (struct cgraph_node *node)
607 : : {
608 : 786968 : struct caller_statistics stats;
609 : :
610 : 786968 : gcc_checking_assert (node->has_gimple_body_p ());
611 : :
612 : 786968 : if (!opt_for_fn (node->decl, flag_ipa_cp_clone))
613 : : {
614 : 739916 : if (dump_file)
615 : 31 : fprintf (dump_file, "Not considering %s for cloning; "
616 : : "-fipa-cp-clone disabled.\n",
617 : : node->dump_name ());
618 : 739916 : return false;
619 : : }
620 : :
621 : : /* Do not use profile here since cold wrapper wrap
622 : : hot function. */
623 : 47052 : if (opt_for_fn (node->decl, optimize_size))
624 : : {
625 : 10 : if (dump_file)
626 : 0 : fprintf (dump_file, "Not considering %s for cloning; "
627 : : "optimizing it for size.\n",
628 : : node->dump_name ());
629 : 10 : return false;
630 : : }
631 : :
632 : 47042 : init_caller_stats (&stats);
633 : 47042 : node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false);
634 : :
635 : 47042 : if (ipa_size_summaries->get (node)->self_size < stats.n_calls)
636 : : {
637 : 219 : if (dump_file)
638 : 0 : fprintf (dump_file, "Considering %s for cloning; code might shrink.\n",
639 : : node->dump_name ());
640 : 219 : return true;
641 : : }
642 : 46823 : if (!stats.n_interesting_calls)
643 : : {
644 : 37478 : if (dump_file)
645 : 220 : fprintf (dump_file, "Not considering %s for cloning; "
646 : : "no calls considered interesting by profile.\n",
647 : : node->dump_name ());
648 : 37478 : return false;
649 : : }
650 : 9345 : if (dump_file)
651 : 153 : fprintf (dump_file, "Considering %s for cloning.\n",
652 : : node->dump_name ());
653 : : return true;
654 : : }
655 : :
656 : : template <typename valtype>
657 : : class value_topo_info
658 : : {
659 : : public:
660 : : /* Head of the linked list of topologically sorted values. */
661 : : ipcp_value<valtype> *values_topo;
662 : : /* Stack for creating SCCs, represented by a linked list too. */
663 : : ipcp_value<valtype> *stack;
664 : : /* Counter driving the algorithm in add_val_to_toposort. */
665 : : int dfs_counter;
666 : :
667 : 126818 : value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0)
668 : : {}
669 : : void add_val (ipcp_value<valtype> *cur_val);
670 : : void propagate_effects ();
671 : : };
672 : :
673 : : /* Arrays representing a topological ordering of call graph nodes and a stack
674 : : of nodes used during constant propagation and also data required to perform
675 : : topological sort of values and propagation of benefits in the determined
676 : : order. */
677 : :
678 : : class ipa_topo_info
679 : : {
680 : : public:
681 : : /* Array with obtained topological order of cgraph nodes. */
682 : : struct cgraph_node **order;
683 : : /* Stack of cgraph nodes used during propagation within SCC until all values
684 : : in the SCC stabilize. */
685 : : struct cgraph_node **stack;
686 : : int nnodes, stack_top;
687 : :
688 : : value_topo_info<tree> constants;
689 : : value_topo_info<ipa_polymorphic_call_context> contexts;
690 : :
691 : 126818 : ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0),
692 : 126818 : constants ()
693 : : {}
694 : : };
695 : :
696 : : /* Skip edges from and to nodes without ipa_cp enabled.
697 : : Ignore not available symbols. */
698 : :
699 : : static bool
700 : 5182302 : ignore_edge_p (cgraph_edge *e)
701 : : {
702 : 5182302 : enum availability avail;
703 : 5182302 : cgraph_node *ultimate_target
704 : 5182302 : = e->callee->function_or_virtual_thunk_symbol (&avail, e->caller);
705 : :
706 : 5182302 : return (avail <= AVAIL_INTERPOSABLE
707 : 1833548 : || !opt_for_fn (ultimate_target->decl, optimize)
708 : 7006927 : || !opt_for_fn (ultimate_target->decl, flag_ipa_cp));
709 : : }
710 : :
711 : : /* Allocate the arrays in TOPO and topologically sort the nodes into order. */
712 : :
713 : : static void
714 : 126818 : build_toporder_info (class ipa_topo_info *topo)
715 : : {
716 : 126818 : topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
717 : 126818 : topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
718 : :
719 : 126818 : gcc_checking_assert (topo->stack_top == 0);
720 : 126818 : topo->nnodes = ipa_reduced_postorder (topo->order, true,
721 : : ignore_edge_p);
722 : 126818 : }
723 : :
724 : : /* Free information about strongly connected components and the arrays in
725 : : TOPO. */
726 : :
727 : : static void
728 : 126818 : free_toporder_info (class ipa_topo_info *topo)
729 : : {
730 : 126818 : ipa_free_postorder_info ();
731 : 126818 : free (topo->order);
732 : 126818 : free (topo->stack);
733 : 126818 : }
734 : :
735 : : /* Add NODE to the stack in TOPO, unless it is already there. */
736 : :
737 : : static inline void
738 : 1260518 : push_node_to_stack (class ipa_topo_info *topo, struct cgraph_node *node)
739 : : {
740 : 1260518 : ipa_node_params *info = ipa_node_params_sum->get (node);
741 : 1260518 : if (info->node_enqueued)
742 : : return;
743 : 1259615 : info->node_enqueued = 1;
744 : 1259615 : topo->stack[topo->stack_top++] = node;
745 : : }
746 : :
747 : : /* Pop a node from the stack in TOPO and return it or return NULL if the stack
748 : : is empty. */
749 : :
750 : : static struct cgraph_node *
751 : 2596498 : pop_node_from_stack (class ipa_topo_info *topo)
752 : : {
753 : 2596498 : if (topo->stack_top)
754 : : {
755 : 1259615 : struct cgraph_node *node;
756 : 1259615 : topo->stack_top--;
757 : 1259615 : node = topo->stack[topo->stack_top];
758 : 1259615 : ipa_node_params_sum->get (node)->node_enqueued = 0;
759 : 1259615 : return node;
760 : : }
761 : : else
762 : : return NULL;
763 : : }
764 : :
765 : : /* Set lattice LAT to bottom and return true if it previously was not set as
766 : : such. */
767 : :
768 : : template <typename valtype>
769 : : inline bool
770 : 2095764 : ipcp_lattice<valtype>::set_to_bottom ()
771 : : {
772 : 2095764 : bool ret = !bottom;
773 : 2095764 : bottom = true;
774 : : return ret;
775 : : }
776 : :
777 : : /* Mark lattice as containing an unknown value and return true if it previously
778 : : was not marked as such. */
779 : :
780 : : template <typename valtype>
781 : : inline bool
782 : 1461691 : ipcp_lattice<valtype>::set_contains_variable ()
783 : : {
784 : 1461691 : bool ret = !contains_variable;
785 : 1461691 : contains_variable = true;
786 : : return ret;
787 : : }
788 : :
789 : : /* Set all aggregate lattices in PLATS to bottom and return true if they were
790 : : not previously set as such. */
791 : :
792 : : static inline bool
793 : 2095582 : set_agg_lats_to_bottom (class ipcp_param_lattices *plats)
794 : : {
795 : 2095582 : bool ret = !plats->aggs_bottom;
796 : 2095582 : plats->aggs_bottom = true;
797 : 2095582 : return ret;
798 : : }
799 : :
800 : : /* Mark all aggregate lattices in PLATS as containing an unknown value and
801 : : return true if they were not previously marked as such. */
802 : :
803 : : static inline bool
804 : 1000587 : set_agg_lats_contain_variable (class ipcp_param_lattices *plats)
805 : : {
806 : 1000587 : bool ret = !plats->aggs_contain_variable;
807 : 1000587 : plats->aggs_contain_variable = true;
808 : 1000587 : return ret;
809 : : }
810 : :
811 : : bool
812 : 0 : ipcp_vr_lattice::meet_with (const ipcp_vr_lattice &other)
813 : : {
814 : 0 : return meet_with_1 (other.m_vr);
815 : : }
816 : :
817 : : /* Meet the current value of the lattice with the range described by
818 : : P_VR. */
819 : :
820 : : bool
821 : 458099 : ipcp_vr_lattice::meet_with (const vrange &p_vr)
822 : : {
823 : 458099 : return meet_with_1 (p_vr);
824 : : }
825 : :
826 : : /* Meet the current value of the lattice with the range described by
827 : : OTHER_VR. Return TRUE if anything changed. */
828 : :
829 : : bool
830 : 458099 : ipcp_vr_lattice::meet_with_1 (const vrange &other_vr)
831 : : {
832 : 458099 : if (bottom_p ())
833 : : return false;
834 : :
835 : 458099 : if (other_vr.varying_p ())
836 : 0 : return set_to_bottom ();
837 : :
838 : 458099 : bool res;
839 : 458099 : if (flag_checking)
840 : : {
841 : 458099 : value_range save (m_vr);
842 : 458099 : res = m_vr.union_ (other_vr);
843 : 458099 : gcc_assert (res == (m_vr != save));
844 : 458099 : }
845 : : else
846 : 0 : res = m_vr.union_ (other_vr);
847 : : return res;
848 : : }
849 : :
850 : : /* Return true if value range information in the lattice is yet unknown. */
851 : :
852 : : bool
853 : : ipcp_vr_lattice::top_p () const
854 : : {
855 : 161470 : return m_vr.undefined_p ();
856 : : }
857 : :
858 : : /* Return true if value range information in the lattice is known to be
859 : : unusable. */
860 : :
861 : : bool
862 : 6826595 : ipcp_vr_lattice::bottom_p () const
863 : : {
864 : 458099 : return m_vr.varying_p ();
865 : : }
866 : :
867 : : /* Set value range information in the lattice to bottom. Return true if it
868 : : previously was in a different state. */
869 : :
870 : : bool
871 : 2372176 : ipcp_vr_lattice::set_to_bottom ()
872 : : {
873 : 2372176 : if (m_vr.varying_p ())
874 : : return false;
875 : :
876 : : /* Setting an unsupported type here forces the temporary to default
877 : : to unsupported_range, which can handle VARYING/DEFINED ranges,
878 : : but nothing else (union, intersect, etc). This allows us to set
879 : : bottoms on any ranges, and is safe as all users of the lattice
880 : : check for bottom first. */
881 : 2234198 : m_vr.set_type (void_type_node);
882 : 2234198 : m_vr.set_varying (void_type_node);
883 : :
884 : 2234198 : return true;
885 : : }
886 : :
887 : : /* Set lattice value to bottom, if it already isn't the case. */
888 : :
889 : : bool
890 : 2388802 : ipcp_bits_lattice::set_to_bottom ()
891 : : {
892 : 2388802 : if (bottom_p ())
893 : : return false;
894 : 2251589 : m_lattice_val = IPA_BITS_VARYING;
895 : 2251589 : m_value = 0;
896 : 2251589 : m_mask = -1;
897 : 2251589 : return true;
898 : : }
899 : :
900 : : /* Set to constant if it isn't already. Only meant to be called
901 : : when switching state from TOP. */
902 : :
903 : : bool
904 : 65237 : ipcp_bits_lattice::set_to_constant (widest_int value, widest_int mask)
905 : : {
906 : 65237 : gcc_assert (top_p ());
907 : 65237 : m_lattice_val = IPA_BITS_CONSTANT;
908 : 65237 : m_value = wi::bit_and (wi::bit_not (mask), value);
909 : 65237 : m_mask = mask;
910 : 65237 : return true;
911 : : }
912 : :
913 : : /* Return true if any of the known bits are non-zero. */
914 : :
915 : : bool
916 : 601 : ipcp_bits_lattice::known_nonzero_p () const
917 : : {
918 : 601 : if (!constant_p ())
919 : : return false;
920 : 601 : return wi::ne_p (wi::bit_and (wi::bit_not (m_mask), m_value), 0);
921 : : }
922 : :
923 : : /* Convert operand to value, mask form. */
924 : :
925 : : void
926 : 2226 : ipcp_bits_lattice::get_value_and_mask (tree operand, widest_int *valuep, widest_int *maskp)
927 : : {
928 : 2226 : wide_int get_nonzero_bits (const_tree);
929 : :
930 : 2226 : if (TREE_CODE (operand) == INTEGER_CST)
931 : : {
932 : 2226 : *valuep = wi::to_widest (operand);
933 : 2226 : *maskp = 0;
934 : : }
935 : : else
936 : : {
937 : 0 : *valuep = 0;
938 : 0 : *maskp = -1;
939 : : }
940 : 2226 : }
941 : :
942 : : /* Meet operation, similar to ccp_lattice_meet, we xor values
943 : : if this->value, value have different values at same bit positions, we want
944 : : to drop that bit to varying. Return true if mask is changed.
945 : : This function assumes that the lattice value is in CONSTANT state. If
946 : : DROP_ALL_ONES, mask out any known bits with value one afterwards. */
947 : :
948 : : bool
949 : 282771 : ipcp_bits_lattice::meet_with_1 (widest_int value, widest_int mask,
950 : : unsigned precision, bool drop_all_ones)
951 : : {
952 : 282771 : gcc_assert (constant_p ());
953 : :
954 : 282771 : widest_int old_mask = m_mask;
955 : 282771 : m_mask = (m_mask | mask) | (m_value ^ value);
956 : 282771 : if (drop_all_ones)
957 : 226 : m_mask |= m_value;
958 : :
959 : 282771 : widest_int cap_mask = wi::shifted_mask <widest_int> (0, precision, true);
960 : 282771 : m_mask |= cap_mask;
961 : 282771 : if (wi::sext (m_mask, precision) == -1)
962 : 3105 : return set_to_bottom ();
963 : :
964 : 279666 : m_value &= ~m_mask;
965 : 279666 : return m_mask != old_mask;
966 : 282771 : }
967 : :
968 : : /* Meet the bits lattice with operand
969 : : described by <value, mask, sgn, precision. */
970 : :
971 : : bool
972 : 375074 : ipcp_bits_lattice::meet_with (widest_int value, widest_int mask,
973 : : unsigned precision)
974 : : {
975 : 375074 : if (bottom_p ())
976 : : return false;
977 : :
978 : 375074 : if (top_p ())
979 : : {
980 : 105200 : if (wi::sext (mask, precision) == -1)
981 : 45522 : return set_to_bottom ();
982 : 59678 : return set_to_constant (value, mask);
983 : : }
984 : :
985 : 269874 : return meet_with_1 (value, mask, precision, false);
986 : : }
987 : :
988 : : /* Meet bits lattice with the result of bit_value_binop (other, operand)
989 : : if code is binary operation or bit_value_unop (other) if code is unary op.
990 : : In the case when code is nop_expr, no adjustment is required. If
991 : : DROP_ALL_ONES, mask out any known bits with value one afterwards. */
992 : :
993 : : bool
994 : 21954 : ipcp_bits_lattice::meet_with (ipcp_bits_lattice& other, unsigned precision,
995 : : signop sgn, enum tree_code code, tree operand,
996 : : bool drop_all_ones)
997 : : {
998 : 21954 : if (other.bottom_p ())
999 : 0 : return set_to_bottom ();
1000 : :
1001 : 21954 : if (bottom_p () || other.top_p ())
1002 : : return false;
1003 : :
1004 : 18565 : widest_int adjusted_value, adjusted_mask;
1005 : :
1006 : 18565 : if (TREE_CODE_CLASS (code) == tcc_binary)
1007 : : {
1008 : 2226 : tree type = TREE_TYPE (operand);
1009 : 2226 : widest_int o_value, o_mask;
1010 : 2226 : get_value_and_mask (operand, &o_value, &o_mask);
1011 : :
1012 : 2226 : bit_value_binop (code, sgn, precision, &adjusted_value, &adjusted_mask,
1013 : 4452 : sgn, precision, other.get_value (), other.get_mask (),
1014 : 2226 : TYPE_SIGN (type), TYPE_PRECISION (type), o_value, o_mask);
1015 : :
1016 : 2226 : if (wi::sext (adjusted_mask, precision) == -1)
1017 : 80 : return set_to_bottom ();
1018 : 2226 : }
1019 : :
1020 : 16339 : else if (TREE_CODE_CLASS (code) == tcc_unary)
1021 : : {
1022 : 32628 : bit_value_unop (code, sgn, precision, &adjusted_value,
1023 : 32628 : &adjusted_mask, sgn, precision, other.get_value (),
1024 : 16314 : other.get_mask ());
1025 : :
1026 : 16314 : if (wi::sext (adjusted_mask, precision) == -1)
1027 : 4 : return set_to_bottom ();
1028 : : }
1029 : :
1030 : : else
1031 : 25 : return set_to_bottom ();
1032 : :
1033 : 18456 : if (top_p ())
1034 : : {
1035 : 5559 : if (drop_all_ones)
1036 : : {
1037 : 375 : adjusted_mask |= adjusted_value;
1038 : 375 : adjusted_value &= ~adjusted_mask;
1039 : : }
1040 : 5559 : widest_int cap_mask = wi::shifted_mask <widest_int> (0, precision, true);
1041 : 5559 : adjusted_mask |= cap_mask;
1042 : 5559 : if (wi::sext (adjusted_mask, precision) == -1)
1043 : 0 : return set_to_bottom ();
1044 : 5559 : return set_to_constant (adjusted_value, adjusted_mask);
1045 : 5559 : }
1046 : : else
1047 : 12897 : return meet_with_1 (adjusted_value, adjusted_mask, precision,
1048 : : drop_all_ones);
1049 : 18565 : }
1050 : :
1051 : : /* Dump the contents of the list to FILE. */
1052 : :
1053 : : void
1054 : 94 : ipa_argagg_value_list::dump (FILE *f)
1055 : : {
1056 : 94 : bool comma = false;
1057 : 274 : for (const ipa_argagg_value &av : m_elts)
1058 : : {
1059 : 180 : fprintf (f, "%s %i[%u]=", comma ? "," : "",
1060 : 180 : av.index, av.unit_offset);
1061 : 180 : print_generic_expr (f, av.value);
1062 : 180 : if (av.by_ref)
1063 : 159 : fprintf (f, "(by_ref)");
1064 : 180 : if (av.killed)
1065 : 1 : fprintf (f, "(killed)");
1066 : 180 : comma = true;
1067 : : }
1068 : 94 : fprintf (f, "\n");
1069 : 94 : }
1070 : :
1071 : : /* Dump the contents of the list to stderr. */
1072 : :
1073 : : void
1074 : 0 : ipa_argagg_value_list::debug ()
1075 : : {
1076 : 0 : dump (stderr);
1077 : 0 : }
1078 : :
1079 : : /* Return the item describing a constant stored for INDEX at UNIT_OFFSET or
1080 : : NULL if there is no such constant. */
1081 : :
1082 : : const ipa_argagg_value *
1083 : 27006485 : ipa_argagg_value_list::get_elt (int index, unsigned unit_offset) const
1084 : : {
1085 : 27006485 : ipa_argagg_value key;
1086 : 27006485 : key.index = index;
1087 : 27006485 : key.unit_offset = unit_offset;
1088 : 27006485 : const ipa_argagg_value *res
1089 : 27006485 : = std::lower_bound (m_elts.begin (), m_elts.end (), key,
1090 : 4575757 : [] (const ipa_argagg_value &elt,
1091 : : const ipa_argagg_value &val)
1092 : : {
1093 : 4575757 : if (elt.index < val.index)
1094 : : return true;
1095 : 3924885 : if (elt.index > val.index)
1096 : : return false;
1097 : 3024649 : if (elt.unit_offset < val.unit_offset)
1098 : : return true;
1099 : : return false;
1100 : : });
1101 : :
1102 : 27006485 : if (res == m_elts.end ()
1103 : 2138138 : || res->index != index
1104 : 28629787 : || res->unit_offset != unit_offset)
1105 : : res = nullptr;
1106 : :
1107 : : /* TODO: perhaps remove the check (that the underlying array is indeed
1108 : : sorted) if it turns out it can be too slow? */
1109 : 27006485 : if (!flag_checking)
1110 : : return res;
1111 : :
1112 : : const ipa_argagg_value *slow_res = NULL;
1113 : : int prev_index = -1;
1114 : : unsigned prev_unit_offset = 0;
1115 : 35610392 : for (const ipa_argagg_value &av : m_elts)
1116 : : {
1117 : 8603907 : gcc_assert (prev_index < 0
1118 : : || prev_index < av.index
1119 : : || prev_unit_offset < av.unit_offset);
1120 : 8603907 : prev_index = av.index;
1121 : 8603907 : prev_unit_offset = av.unit_offset;
1122 : 8603907 : if (av.index == index
1123 : 4477579 : && av.unit_offset == unit_offset)
1124 : 8603907 : slow_res = &av;
1125 : : }
1126 : 27006485 : gcc_assert (res == slow_res);
1127 : :
1128 : : return res;
1129 : : }
1130 : :
1131 : : /* Return the first item describing a constant stored for parameter with INDEX,
1132 : : regardless of offset or reference, or NULL if there is no such constant. */
1133 : :
1134 : : const ipa_argagg_value *
1135 : 130391 : ipa_argagg_value_list::get_elt_for_index (int index) const
1136 : : {
1137 : 130391 : const ipa_argagg_value *res
1138 : 130391 : = std::lower_bound (m_elts.begin (), m_elts.end (), index,
1139 : 13192 : [] (const ipa_argagg_value &elt, unsigned idx)
1140 : : {
1141 : 13192 : return elt.index < idx;
1142 : : });
1143 : 130391 : if (res == m_elts.end ()
1144 : 130391 : || res->index != index)
1145 : : res = nullptr;
1146 : 130391 : return res;
1147 : : }
1148 : :
1149 : : /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, not
1150 : : performing any check of whether value is passed by reference, or NULL_TREE
1151 : : if there is no such constant. */
1152 : :
1153 : : tree
1154 : 32458 : ipa_argagg_value_list::get_value (int index, unsigned unit_offset) const
1155 : : {
1156 : 32458 : const ipa_argagg_value *av = get_elt (index, unit_offset);
1157 : 32458 : return av ? av->value : NULL_TREE;
1158 : : }
1159 : :
1160 : : /* Return the aggregate constant stored for INDEX at UNIT_OFFSET, if it is
1161 : : passed by reference or not according to BY_REF, or NULL_TREE if there is
1162 : : no such constant. */
1163 : :
1164 : : tree
1165 : 26965066 : ipa_argagg_value_list::get_value (int index, unsigned unit_offset,
1166 : : bool by_ref) const
1167 : : {
1168 : 26965066 : const ipa_argagg_value *av = get_elt (index, unit_offset);
1169 : 26965066 : if (av && av->by_ref == by_ref)
1170 : 1255178 : return av->value;
1171 : : return NULL_TREE;
1172 : : }
1173 : :
1174 : : /* Return true if all elements present in OTHER are also present in this
1175 : : list. */
1176 : :
1177 : : bool
1178 : 15 : ipa_argagg_value_list::superset_of_p (const ipa_argagg_value_list &other) const
1179 : : {
1180 : 15 : unsigned j = 0;
1181 : 20 : for (unsigned i = 0; i < other.m_elts.size (); i++)
1182 : : {
1183 : 15 : unsigned other_index = other.m_elts[i].index;
1184 : 15 : unsigned other_offset = other.m_elts[i].unit_offset;
1185 : :
1186 : 15 : while (j < m_elts.size ()
1187 : 15 : && (m_elts[j].index < other_index
1188 : 5 : || (m_elts[j].index == other_index
1189 : 5 : && m_elts[j].unit_offset < other_offset)))
1190 : 0 : j++;
1191 : :
1192 : 15 : if (j >= m_elts.size ()
1193 : 5 : || m_elts[j].index != other_index
1194 : 5 : || m_elts[j].unit_offset != other_offset
1195 : 5 : || m_elts[j].by_ref != other.m_elts[i].by_ref
1196 : 5 : || !m_elts[j].value
1197 : 20 : || !values_equal_for_ipcp_p (m_elts[j].value, other.m_elts[i].value))
1198 : 10 : return false;
1199 : : }
1200 : : return true;
1201 : : }
1202 : :
1203 : : /* Push all items in this list that describe parameter SRC_INDEX into RES as
1204 : : ones describing DST_INDEX while subtracting UNIT_DELTA from their unit
1205 : : offsets but skip those which would end up with a negative offset. */
1206 : :
1207 : : void
1208 : 727 : ipa_argagg_value_list::push_adjusted_values (unsigned src_index,
1209 : : unsigned dest_index,
1210 : : unsigned unit_delta,
1211 : : vec<ipa_argagg_value> *res) const
1212 : : {
1213 : 727 : const ipa_argagg_value *av = get_elt_for_index (src_index);
1214 : 727 : if (!av)
1215 : : return;
1216 : : unsigned prev_unit_offset = 0;
1217 : : bool first = true;
1218 : 1446 : for (; av < m_elts.end (); ++av)
1219 : : {
1220 : 1045 : if (av->index > src_index)
1221 : : return;
1222 : 982 : if (av->index == src_index
1223 : 982 : && (av->unit_offset >= unit_delta)
1224 : 982 : && av->value)
1225 : : {
1226 : 980 : ipa_argagg_value new_av;
1227 : 980 : gcc_checking_assert (av->value);
1228 : 980 : new_av.value = av->value;
1229 : 980 : new_av.unit_offset = av->unit_offset - unit_delta;
1230 : 980 : new_av.index = dest_index;
1231 : 980 : new_av.by_ref = av->by_ref;
1232 : 980 : gcc_assert (!av->killed);
1233 : 980 : new_av.killed = false;
1234 : :
1235 : : /* Quick check that the offsets we push are indeed increasing. */
1236 : 980 : gcc_assert (first
1237 : : || new_av.unit_offset > prev_unit_offset);
1238 : 980 : prev_unit_offset = new_av.unit_offset;
1239 : 980 : first = false;
1240 : :
1241 : 980 : res->safe_push (new_av);
1242 : : }
1243 : : }
1244 : : }
1245 : :
1246 : : /* Push to RES information about single lattices describing aggregate values in
1247 : : PLATS as those describing parameter DEST_INDEX and the original offset minus
1248 : : UNIT_DELTA. Return true if any item has been pushed to RES. */
1249 : :
1250 : : static bool
1251 : 1801767 : push_agg_values_from_plats (ipcp_param_lattices *plats, int dest_index,
1252 : : unsigned unit_delta,
1253 : : vec<ipa_argagg_value> *res)
1254 : : {
1255 : 1801767 : if (plats->aggs_contain_variable)
1256 : : return false;
1257 : :
1258 : 1621258 : bool pushed_sth = false;
1259 : 1621258 : bool first = true;
1260 : 1621258 : unsigned prev_unit_offset = 0;
1261 : 1652306 : for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
1262 : 61797 : if (aglat->is_single_const ()
1263 : 22945 : && (aglat->offset / BITS_PER_UNIT - unit_delta) >= 0)
1264 : : {
1265 : 22945 : ipa_argagg_value iav;
1266 : 22945 : iav.value = aglat->values->value;
1267 : 22945 : iav.unit_offset = aglat->offset / BITS_PER_UNIT - unit_delta;
1268 : 22945 : iav.index = dest_index;
1269 : 22945 : iav.by_ref = plats->aggs_by_ref;
1270 : 22945 : iav.killed = false;
1271 : :
1272 : 22945 : gcc_assert (first
1273 : : || iav.unit_offset > prev_unit_offset);
1274 : 22945 : prev_unit_offset = iav.unit_offset;
1275 : 22945 : first = false;
1276 : :
1277 : 22945 : pushed_sth = true;
1278 : 22945 : res->safe_push (iav);
1279 : : }
1280 : : return pushed_sth;
1281 : : }
1282 : :
1283 : : /* Turn all values in LIST that are not present in OTHER into NULL_TREEs.
1284 : : Return the number of remaining valid entries. */
1285 : :
1286 : : static unsigned
1287 : 3830 : intersect_argaggs_with (vec<ipa_argagg_value> &elts,
1288 : : const vec<ipa_argagg_value> &other)
1289 : : {
1290 : 3830 : unsigned valid_entries = 0;
1291 : 3830 : unsigned j = 0;
1292 : 24121 : for (unsigned i = 0; i < elts.length (); i++)
1293 : : {
1294 : 20291 : if (!elts[i].value)
1295 : 3047 : continue;
1296 : :
1297 : 17244 : unsigned this_index = elts[i].index;
1298 : 17244 : unsigned this_offset = elts[i].unit_offset;
1299 : :
1300 : 17244 : while (j < other.length ()
1301 : 63762 : && (other[j].index < this_index
1302 : 30138 : || (other[j].index == this_index
1303 : 29935 : && other[j].unit_offset < this_offset)))
1304 : 14829 : j++;
1305 : :
1306 : 17244 : if (j >= other.length ())
1307 : : {
1308 : 384 : elts[i].value = NULL_TREE;
1309 : 384 : continue;
1310 : : }
1311 : :
1312 : 16860 : if (other[j].index == this_index
1313 : 16657 : && other[j].unit_offset == this_offset
1314 : 16502 : && other[j].by_ref == elts[i].by_ref
1315 : 16502 : && other[j].value
1316 : 33362 : && values_equal_for_ipcp_p (other[j].value, elts[i].value))
1317 : 15214 : valid_entries++;
1318 : : else
1319 : 1646 : elts[i].value = NULL_TREE;
1320 : : }
1321 : 3830 : return valid_entries;
1322 : : }
1323 : :
1324 : : /* Mark bot aggregate and scalar lattices as containing an unknown variable,
1325 : : return true is any of them has not been marked as such so far. */
1326 : :
1327 : : static inline bool
1328 : 161730 : set_all_contains_variable (class ipcp_param_lattices *plats)
1329 : : {
1330 : 161730 : bool ret;
1331 : 161730 : ret = plats->itself.set_contains_variable ();
1332 : 161730 : ret |= plats->ctxlat.set_contains_variable ();
1333 : 161730 : ret |= set_agg_lats_contain_variable (plats);
1334 : 161730 : ret |= plats->bits_lattice.set_to_bottom ();
1335 : 161730 : ret |= plats->m_value_range.set_to_bottom ();
1336 : 161730 : return ret;
1337 : : }
1338 : :
1339 : : /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA
1340 : : points to by the number of callers to NODE. */
1341 : :
1342 : : static bool
1343 : 98566 : count_callers (cgraph_node *node, void *data)
1344 : : {
1345 : 98566 : int *caller_count = (int *) data;
1346 : :
1347 : 395378 : for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
1348 : : /* Local thunks can be handled transparently, but if the thunk cannot
1349 : : be optimized out, count it as a real use. */
1350 : 296812 : if (!cs->caller->thunk || !cs->caller->local)
1351 : 296812 : ++*caller_count;
1352 : 98566 : return false;
1353 : : }
1354 : :
1355 : : /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on
1356 : : the one caller of some other node. Set the caller's corresponding flag. */
1357 : :
1358 : : static bool
1359 : 56739 : set_single_call_flag (cgraph_node *node, void *)
1360 : : {
1361 : 56739 : cgraph_edge *cs = node->callers;
1362 : : /* Local thunks can be handled transparently, skip them. */
1363 : 56739 : while (cs && cs->caller->thunk && cs->caller->local)
1364 : 0 : cs = cs->next_caller;
1365 : 56739 : if (cs)
1366 : 56044 : if (ipa_node_params* info = ipa_node_params_sum->get (cs->caller))
1367 : : {
1368 : 56043 : info->node_calling_single_call = true;
1369 : 56043 : return true;
1370 : : }
1371 : : return false;
1372 : : }
1373 : :
1374 : : /* Initialize ipcp_lattices. */
1375 : :
1376 : : static void
1377 : 1256761 : initialize_node_lattices (struct cgraph_node *node)
1378 : : {
1379 : 1256761 : ipa_node_params *info = ipa_node_params_sum->get (node);
1380 : 1256761 : struct cgraph_edge *ie;
1381 : 1256761 : bool disable = false, variable = false;
1382 : 1256761 : int i;
1383 : :
1384 : 1256761 : gcc_checking_assert (node->has_gimple_body_p ());
1385 : :
1386 : 1256761 : if (!ipa_get_param_count (info))
1387 : : disable = true;
1388 : 1031513 : else if (node->local)
1389 : : {
1390 : 87120 : int caller_count = 0;
1391 : 87120 : node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count,
1392 : : true);
1393 : 87120 : gcc_checking_assert (caller_count > 0);
1394 : 87120 : if (caller_count == 1)
1395 : 56044 : node->call_for_symbol_thunks_and_aliases (set_single_call_flag,
1396 : : NULL, true);
1397 : : }
1398 : : else
1399 : : {
1400 : : /* When cloning is allowed, we can assume that externally visible
1401 : : functions are not called. We will compensate this by cloning
1402 : : later. */
1403 : 944393 : if (ipcp_versionable_function_p (node)
1404 : 944393 : && ipcp_cloning_candidate_p (node))
1405 : : variable = true;
1406 : : else
1407 : : disable = true;
1408 : : }
1409 : :
1410 : 711 : if (dump_file && (dump_flags & TDF_DETAILS)
1411 : 1256914 : && !node->alias && !node->thunk)
1412 : : {
1413 : 153 : fprintf (dump_file, "Initializing lattices of %s\n",
1414 : : node->dump_name ());
1415 : 153 : if (disable || variable)
1416 : 110 : fprintf (dump_file, " Marking all lattices as %s\n",
1417 : : disable ? "BOTTOM" : "VARIABLE");
1418 : : }
1419 : :
1420 : 1256761 : auto_vec<bool, 16> surviving_params;
1421 : 1256761 : bool pre_modified = false;
1422 : :
1423 : 1256761 : clone_info *cinfo = clone_info::get (node);
1424 : :
1425 : 1256761 : if (!disable && cinfo && cinfo->param_adjustments)
1426 : : {
1427 : : /* At the moment all IPA optimizations should use the number of
1428 : : parameters of the prevailing decl as the m_always_copy_start.
1429 : : Handling any other value would complicate the code below, so for the
1430 : : time bing let's only assert it is so. */
1431 : 0 : gcc_assert ((cinfo->param_adjustments->m_always_copy_start
1432 : : == ipa_get_param_count (info))
1433 : : || cinfo->param_adjustments->m_always_copy_start < 0);
1434 : :
1435 : 0 : pre_modified = true;
1436 : 0 : cinfo->param_adjustments->get_surviving_params (&surviving_params);
1437 : :
1438 : 0 : if (dump_file && (dump_flags & TDF_DETAILS)
1439 : 0 : && !node->alias && !node->thunk)
1440 : : {
1441 : : bool first = true;
1442 : 0 : for (int j = 0; j < ipa_get_param_count (info); j++)
1443 : : {
1444 : 0 : if (j < (int) surviving_params.length ()
1445 : 0 : && surviving_params[j])
1446 : 0 : continue;
1447 : 0 : if (first)
1448 : : {
1449 : 0 : fprintf (dump_file,
1450 : : " The following parameters are dead on arrival:");
1451 : 0 : first = false;
1452 : : }
1453 : 0 : fprintf (dump_file, " %u", j);
1454 : : }
1455 : 0 : if (!first)
1456 : 0 : fprintf (dump_file, "\n");
1457 : : }
1458 : : }
1459 : :
1460 : 6910154 : for (i = 0; i < ipa_get_param_count (info); i++)
1461 : : {
1462 : 2310940 : ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
1463 : 2310940 : tree type = ipa_get_type (info, i);
1464 : 2310940 : if (disable
1465 : 216506 : || !ipa_get_type (info, i)
1466 : 2527446 : || (pre_modified && (surviving_params.length () <= (unsigned) i
1467 : 0 : || !surviving_params[i])))
1468 : : {
1469 : 2094434 : plats->itself.set_to_bottom ();
1470 : 2094434 : plats->ctxlat.set_to_bottom ();
1471 : 2094434 : set_agg_lats_to_bottom (plats);
1472 : 2094434 : plats->bits_lattice.set_to_bottom ();
1473 : 2094434 : plats->m_value_range.init (type);
1474 : 2094434 : plats->m_value_range.set_to_bottom ();
1475 : : }
1476 : : else
1477 : : {
1478 : 216506 : plats->m_value_range.init (type);
1479 : 216506 : if (variable)
1480 : 24455 : set_all_contains_variable (plats);
1481 : : }
1482 : : }
1483 : :
1484 : 1393854 : for (ie = node->indirect_calls; ie; ie = ie->next_callee)
1485 : 137093 : if (ie->indirect_info->polymorphic
1486 : 10044 : && ie->indirect_info->param_index >= 0)
1487 : : {
1488 : 10019 : gcc_checking_assert (ie->indirect_info->param_index >= 0);
1489 : 10019 : ipa_get_parm_lattices (info,
1490 : 10019 : ie->indirect_info->param_index)->virt_call = 1;
1491 : : }
1492 : 1256761 : }
1493 : :
1494 : : /* Return VALUE if it is NULL_TREE or if it can be directly safely IPA-CP
1495 : : propagated to a parameter of type PARAM_TYPE, or return a fold-converted
1496 : : VALUE to PARAM_TYPE if that is possible. Return NULL_TREE otherwise. */
1497 : :
1498 : : static tree
1499 : 401726 : ipacp_value_safe_for_type (tree param_type, tree value)
1500 : : {
1501 : 401726 : if (!value)
1502 : : return NULL_TREE;
1503 : 401462 : tree val_type = TREE_TYPE (value);
1504 : 401462 : if (param_type == val_type
1505 : 401462 : || useless_type_conversion_p (param_type, val_type))
1506 : 401237 : return value;
1507 : 225 : if (fold_convertible_p (param_type, value))
1508 : 208 : return fold_convert (param_type, value);
1509 : : else
1510 : : return NULL_TREE;
1511 : : }
1512 : :
1513 : : /* Return the result of a (possibly arithmetic) operation determined by OPCODE
1514 : : on the constant value INPUT. OPERAND is 2nd operand for binary operation
1515 : : and is required for binary operations. RES_TYPE, required when opcode is
1516 : : not NOP_EXPR, is the type in which any operation is to be performed. Return
1517 : : NULL_TREE if that cannot be determined or be considered an interprocedural
1518 : : invariant. */
1519 : :
1520 : : static tree
1521 : 36459 : ipa_get_jf_arith_result (enum tree_code opcode, tree input, tree operand,
1522 : : tree res_type)
1523 : : {
1524 : 36459 : tree res;
1525 : :
1526 : 36459 : if (opcode == NOP_EXPR)
1527 : : return input;
1528 : 3473 : if (!is_gimple_ip_invariant (input))
1529 : : return NULL_TREE;
1530 : :
1531 : 3473 : if (opcode == ASSERT_EXPR)
1532 : : {
1533 : 932 : if (values_equal_for_ipcp_p (input, operand))
1534 : : return input;
1535 : : else
1536 : : return NULL_TREE;
1537 : : }
1538 : :
1539 : 2541 : if (TREE_CODE_CLASS (opcode) == tcc_unary)
1540 : 71 : res = fold_unary (opcode, res_type, input);
1541 : : else
1542 : 2470 : res = fold_binary (opcode, res_type, input, operand);
1543 : :
1544 : 2541 : if (res && !is_gimple_ip_invariant (res))
1545 : : return NULL_TREE;
1546 : :
1547 : : return res;
1548 : : }
1549 : :
1550 : : /* Return the result of an ancestor jump function JFUNC on the constant value
1551 : : INPUT. Return NULL_TREE if that cannot be determined. */
1552 : :
1553 : : static tree
1554 : 1001 : ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input)
1555 : : {
1556 : 1001 : gcc_checking_assert (TREE_CODE (input) != TREE_BINFO);
1557 : 1001 : if (TREE_CODE (input) == ADDR_EXPR)
1558 : : {
1559 : 913 : gcc_checking_assert (is_gimple_ip_invariant_address (input));
1560 : 913 : poly_int64 off = ipa_get_jf_ancestor_offset (jfunc);
1561 : 913 : if (known_eq (off, 0))
1562 : : return input;
1563 : 841 : poly_int64 byte_offset = exact_div (off, BITS_PER_UNIT);
1564 : 1682 : return build1 (ADDR_EXPR, TREE_TYPE (input),
1565 : 841 : fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (input)), input,
1566 : 841 : build_int_cst (ptr_type_node, byte_offset)));
1567 : : }
1568 : 88 : else if (ipa_get_jf_ancestor_keep_null (jfunc)
1569 : 88 : && zerop (input))
1570 : : return input;
1571 : : else
1572 : 84 : return NULL_TREE;
1573 : : }
1574 : :
1575 : : /* Determine whether JFUNC evaluates to a single known constant value and if
1576 : : so, return it. Otherwise return NULL. INFO describes the caller node or
1577 : : the one it is inlined to, so that pass-through jump functions can be
1578 : : evaluated. PARM_TYPE is the type of the parameter to which the result is
1579 : : passed. */
1580 : :
1581 : : tree
1582 : 16762260 : ipa_value_from_jfunc (class ipa_node_params *info, struct ipa_jump_func *jfunc,
1583 : : tree parm_type)
1584 : : {
1585 : 16762260 : if (jfunc->type == IPA_JF_CONST)
1586 : 4186291 : return ipa_get_jf_constant (jfunc);
1587 : 12575969 : else if (jfunc->type == IPA_JF_PASS_THROUGH
1588 : 9707106 : || jfunc->type == IPA_JF_ANCESTOR)
1589 : : {
1590 : 3632086 : tree input;
1591 : 3632086 : int idx;
1592 : :
1593 : 3632086 : if (jfunc->type == IPA_JF_PASS_THROUGH)
1594 : 2868863 : idx = ipa_get_jf_pass_through_formal_id (jfunc);
1595 : : else
1596 : 763223 : idx = ipa_get_jf_ancestor_formal_id (jfunc);
1597 : :
1598 : 3632086 : if (info->ipcp_orig_node)
1599 : 46550 : input = info->known_csts[idx];
1600 : : else
1601 : : {
1602 : 3585536 : ipcp_lattice<tree> *lat;
1603 : :
1604 : 6613002 : if (info->lattices.is_empty ()
1605 : 3027466 : || idx >= ipa_get_param_count (info))
1606 : : return NULL_TREE;
1607 : 3027466 : lat = ipa_get_scalar_lat (info, idx);
1608 : 3027466 : if (!lat->is_single_const ())
1609 : : return NULL_TREE;
1610 : 144 : input = lat->values->value;
1611 : : }
1612 : :
1613 : 46694 : if (!input)
1614 : : return NULL_TREE;
1615 : :
1616 : 13779 : if (jfunc->type == IPA_JF_PASS_THROUGH)
1617 : : {
1618 : 13104 : if (!parm_type)
1619 : : return NULL_TREE;
1620 : 13093 : enum tree_code opcode = ipa_get_jf_pass_through_operation (jfunc);
1621 : 13093 : tree op2 = ipa_get_jf_pass_through_operand (jfunc);
1622 : 13093 : tree op_type
1623 : 13093 : = (opcode == NOP_EXPR) ? NULL_TREE
1624 : 638 : : ipa_get_jf_pass_through_op_type (jfunc);
1625 : 13093 : tree cstval = ipa_get_jf_arith_result (opcode, input, op2, op_type);
1626 : 13093 : return ipacp_value_safe_for_type (parm_type, cstval);
1627 : : }
1628 : : else
1629 : 675 : return ipa_get_jf_ancestor_result (jfunc, input);
1630 : : }
1631 : : else
1632 : : return NULL_TREE;
1633 : : }
1634 : :
1635 : : /* Determine whether JFUNC evaluates to single known polymorphic context, given
1636 : : that INFO describes the caller node or the one it is inlined to, CS is the
1637 : : call graph edge corresponding to JFUNC and CSIDX index of the described
1638 : : parameter. */
1639 : :
1640 : : ipa_polymorphic_call_context
1641 : 254535 : ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx,
1642 : : ipa_jump_func *jfunc)
1643 : : {
1644 : 254535 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
1645 : 254535 : ipa_polymorphic_call_context ctx;
1646 : 254535 : ipa_polymorphic_call_context *edge_ctx
1647 : 254535 : = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL;
1648 : :
1649 : 203098 : if (edge_ctx && !edge_ctx->useless_p ())
1650 : 201399 : ctx = *edge_ctx;
1651 : :
1652 : 254535 : if (jfunc->type == IPA_JF_PASS_THROUGH
1653 : 216939 : || jfunc->type == IPA_JF_ANCESTOR)
1654 : : {
1655 : 42636 : ipa_polymorphic_call_context srcctx;
1656 : 42636 : int srcidx;
1657 : 42636 : bool type_preserved = true;
1658 : 42636 : if (jfunc->type == IPA_JF_PASS_THROUGH)
1659 : : {
1660 : 37596 : if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
1661 : 286 : return ctx;
1662 : 37310 : type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
1663 : 37310 : srcidx = ipa_get_jf_pass_through_formal_id (jfunc);
1664 : : }
1665 : : else
1666 : : {
1667 : 5040 : type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
1668 : 5040 : srcidx = ipa_get_jf_ancestor_formal_id (jfunc);
1669 : : }
1670 : 42350 : if (info->ipcp_orig_node)
1671 : : {
1672 : 4040 : if (info->known_contexts.exists ())
1673 : 571 : srcctx = info->known_contexts[srcidx];
1674 : : }
1675 : : else
1676 : : {
1677 : 74554 : if (info->lattices.is_empty ()
1678 : 36244 : || srcidx >= ipa_get_param_count (info))
1679 : 2066 : return ctx;
1680 : 36244 : ipcp_lattice<ipa_polymorphic_call_context> *lat;
1681 : 36244 : lat = ipa_get_poly_ctx_lat (info, srcidx);
1682 : 36244 : if (!lat->is_single_const ())
1683 : 35367 : return ctx;
1684 : 877 : srcctx = lat->values->value;
1685 : : }
1686 : 4917 : if (srcctx.useless_p ())
1687 : 3587 : return ctx;
1688 : 1330 : if (jfunc->type == IPA_JF_ANCESTOR)
1689 : 100 : srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc));
1690 : 1330 : if (!type_preserved)
1691 : 521 : srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
1692 : 1330 : srcctx.combine_with (ctx);
1693 : 1330 : return srcctx;
1694 : : }
1695 : :
1696 : 211899 : return ctx;
1697 : : }
1698 : :
1699 : : /* Emulate effects of unary OPERATION and/or conversion from SRC_TYPE to
1700 : : DST_TYPE on value range in SRC_VR and store it to DST_VR. Return true if
1701 : : the result is a range that is not VARYING nor UNDEFINED. */
1702 : :
1703 : : bool
1704 : 8784673 : ipa_vr_operation_and_type_effects (vrange &dst_vr,
1705 : : const vrange &src_vr,
1706 : : enum tree_code operation,
1707 : : tree dst_type, tree src_type)
1708 : : {
1709 : 16636814 : if (!ipa_vr_supported_type_p (dst_type)
1710 : 0 : || !ipa_vr_supported_type_p (src_type))
1711 : : return false;
1712 : :
1713 : 8784673 : range_op_handler handler (operation);
1714 : 8784673 : if (!handler)
1715 : : return false;
1716 : :
1717 : 8784673 : value_range varying (dst_type);
1718 : 8784673 : varying.set_varying (dst_type);
1719 : :
1720 : 8784673 : return (handler.operand_check_p (dst_type, src_type, dst_type)
1721 : 8784673 : && handler.fold_range (dst_vr, dst_type, src_vr, varying)
1722 : 8784671 : && !dst_vr.varying_p ()
1723 : 17569266 : && !dst_vr.undefined_p ());
1724 : 8784673 : }
1725 : :
1726 : : /* Same as above, but the SRC_VR argument is an IPA_VR which must
1727 : : first be extracted onto a vrange. */
1728 : :
1729 : : bool
1730 : 8690855 : ipa_vr_operation_and_type_effects (vrange &dst_vr,
1731 : : const ipa_vr &src_vr,
1732 : : enum tree_code operation,
1733 : : tree dst_type, tree src_type)
1734 : : {
1735 : 8690855 : value_range tmp;
1736 : 8690855 : src_vr.get_vrange (tmp);
1737 : 8690855 : return ipa_vr_operation_and_type_effects (dst_vr, tmp, operation,
1738 : 8690855 : dst_type, src_type);
1739 : 8690855 : }
1740 : :
1741 : : /* Given a PASS_THROUGH jump function JFUNC that takes as its source SRC_VR of
1742 : : SRC_TYPE and the result needs to be DST_TYPE, if any value range information
1743 : : can be deduced at all, intersect VR with it. CONTEXT_NODE is the call graph
1744 : : node representing the function for which optimization flags should be
1745 : : evaluated. */
1746 : :
1747 : : static void
1748 : 94190 : ipa_vr_intersect_with_arith_jfunc (vrange &vr,
1749 : : ipa_jump_func *jfunc,
1750 : : cgraph_node *context_node,
1751 : : const value_range &src_vr,
1752 : : tree src_type,
1753 : : tree dst_type)
1754 : : {
1755 : 94190 : if (src_vr.undefined_p () || src_vr.varying_p ())
1756 : 92818 : return;
1757 : :
1758 : 93759 : enum tree_code operation = ipa_get_jf_pass_through_operation (jfunc);
1759 : 93759 : if (TREE_CODE_CLASS (operation) == tcc_unary)
1760 : : {
1761 : 92387 : value_range op_res;
1762 : 92387 : const value_range *inter_vr;
1763 : 92387 : if (operation != NOP_EXPR)
1764 : : {
1765 : 89 : tree operation_type = ipa_get_jf_pass_through_op_type (jfunc);
1766 : 89 : op_res.set_varying (operation_type);
1767 : 89 : if (!ipa_vr_operation_and_type_effects (op_res, src_vr, operation,
1768 : : operation_type, src_type))
1769 : : return;
1770 : 89 : if (src_type == dst_type)
1771 : : {
1772 : 30 : vr.intersect (op_res);
1773 : 30 : return;
1774 : : }
1775 : : inter_vr = &op_res;
1776 : : src_type = operation_type;
1777 : : }
1778 : : else
1779 : : inter_vr = &src_vr;
1780 : :
1781 : 92357 : value_range tmp_res (dst_type);
1782 : 92357 : if (ipa_vr_operation_and_type_effects (tmp_res, *inter_vr, NOP_EXPR,
1783 : : dst_type, src_type))
1784 : 92356 : vr.intersect (tmp_res);
1785 : 92357 : return;
1786 : 92387 : }
1787 : :
1788 : 1372 : tree operand = ipa_get_jf_pass_through_operand (jfunc);
1789 : 1372 : range_op_handler handler (operation);
1790 : 1372 : if (!handler)
1791 : : return;
1792 : 1372 : value_range op_vr (TREE_TYPE (operand));
1793 : 1372 : ipa_get_range_from_ip_invariant (op_vr, operand, context_node);
1794 : :
1795 : 1372 : tree operation_type = ipa_get_jf_pass_through_op_type (jfunc);
1796 : 1372 : value_range op_res (operation_type);
1797 : 1989 : if (!ipa_vr_supported_type_p (operation_type)
1798 : 1372 : || !handler.operand_check_p (operation_type, src_type, op_vr.type ())
1799 : 1372 : || !handler.fold_range (op_res, operation_type, src_vr, op_vr))
1800 : 0 : return;
1801 : :
1802 : 1372 : value_range tmp_res (dst_type);
1803 : 1372 : if (ipa_vr_operation_and_type_effects (tmp_res, op_res, NOP_EXPR, dst_type,
1804 : : operation_type))
1805 : 1308 : vr.intersect (tmp_res);
1806 : 1372 : }
1807 : :
1808 : : /* Determine range of JFUNC given that INFO describes the caller node or
1809 : : the one it is inlined to, CS is the call graph edge corresponding to JFUNC
1810 : : and PARM_TYPE of the parameter. */
1811 : :
1812 : : void
1813 : 11419598 : ipa_value_range_from_jfunc (vrange &vr,
1814 : : ipa_node_params *info, cgraph_edge *cs,
1815 : : ipa_jump_func *jfunc, tree parm_type)
1816 : : {
1817 : 11419598 : vr.set_varying (parm_type);
1818 : :
1819 : 11419598 : if (jfunc->m_vr && jfunc->m_vr->known_p ())
1820 : 7936025 : ipa_vr_operation_and_type_effects (vr,
1821 : : *jfunc->m_vr,
1822 : : NOP_EXPR, parm_type,
1823 : 7936025 : jfunc->m_vr->type ());
1824 : 11419598 : if (vr.singleton_p ())
1825 : : return;
1826 : :
1827 : 11419544 : if (jfunc->type == IPA_JF_PASS_THROUGH)
1828 : : {
1829 : 2241253 : ipcp_transformation *sum
1830 : 2241253 : = ipcp_get_transformation_summary (cs->caller->inlined_to
1831 : : ? cs->caller->inlined_to
1832 : : : cs->caller);
1833 : 2241253 : if (!sum || !sum->m_vr)
1834 : 2162558 : return;
1835 : :
1836 : 121132 : int idx = ipa_get_jf_pass_through_formal_id (jfunc);
1837 : :
1838 : 121132 : if (!(*sum->m_vr)[idx].known_p ())
1839 : : return;
1840 : 78695 : tree src_type = ipa_get_type (info, idx);
1841 : 78695 : value_range srcvr;
1842 : 78695 : (*sum->m_vr)[idx].get_vrange (srcvr);
1843 : :
1844 : 78695 : ipa_vr_intersect_with_arith_jfunc (vr, jfunc, cs->caller, srcvr, src_type,
1845 : : parm_type);
1846 : 78695 : }
1847 : : }
1848 : :
1849 : : /* Determine whether ITEM, jump function for an aggregate part, evaluates to a
1850 : : single known constant value and if so, return it. Otherwise return NULL.
1851 : : NODE and INFO describes the caller node or the one it is inlined to, and
1852 : : its related info. */
1853 : :
1854 : : tree
1855 : 2156671 : ipa_agg_value_from_jfunc (ipa_node_params *info, cgraph_node *node,
1856 : : const ipa_agg_jf_item *item)
1857 : : {
1858 : 2156671 : tree value = NULL_TREE;
1859 : 2156671 : int src_idx;
1860 : :
1861 : 2156671 : if (item->offset < 0
1862 : 2102998 : || item->jftype == IPA_JF_UNKNOWN
1863 : 1979351 : || item->offset >= (HOST_WIDE_INT) UINT_MAX * BITS_PER_UNIT)
1864 : : return NULL_TREE;
1865 : :
1866 : 1979351 : if (item->jftype == IPA_JF_CONST)
1867 : 1670289 : return item->value.constant;
1868 : :
1869 : 309062 : gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
1870 : : || item->jftype == IPA_JF_LOAD_AGG);
1871 : :
1872 : 309062 : src_idx = item->value.pass_through.formal_id;
1873 : :
1874 : 309062 : if (info->ipcp_orig_node)
1875 : : {
1876 : 7584 : if (item->jftype == IPA_JF_PASS_THROUGH)
1877 : 1645 : value = info->known_csts[src_idx];
1878 : 5939 : else if (ipcp_transformation *ts = ipcp_get_transformation_summary (node))
1879 : : {
1880 : 5939 : ipa_argagg_value_list avl (ts);
1881 : 5939 : value = avl.get_value (src_idx,
1882 : 5939 : item->value.load_agg.offset / BITS_PER_UNIT,
1883 : 5939 : item->value.load_agg.by_ref);
1884 : : }
1885 : : }
1886 : 301478 : else if (!info->lattices.is_empty ())
1887 : : {
1888 : 211235 : class ipcp_param_lattices *src_plats
1889 : 211235 : = ipa_get_parm_lattices (info, src_idx);
1890 : :
1891 : 211235 : if (item->jftype == IPA_JF_PASS_THROUGH)
1892 : : {
1893 : 134580 : struct ipcp_lattice<tree> *lat = &src_plats->itself;
1894 : :
1895 : 489273 : if (!lat->is_single_const ())
1896 : : return NULL_TREE;
1897 : :
1898 : 0 : value = lat->values->value;
1899 : : }
1900 : 76655 : else if (src_plats->aggs
1901 : 7247 : && !src_plats->aggs_bottom
1902 : 7247 : && !src_plats->aggs_contain_variable
1903 : 1263 : && src_plats->aggs_by_ref == item->value.load_agg.by_ref)
1904 : : {
1905 : : struct ipcp_agg_lattice *aglat;
1906 : :
1907 : 2038 : for (aglat = src_plats->aggs; aglat; aglat = aglat->next)
1908 : : {
1909 : 2038 : if (aglat->offset > item->value.load_agg.offset)
1910 : : break;
1911 : :
1912 : 2034 : if (aglat->offset == item->value.load_agg.offset)
1913 : : {
1914 : 1259 : if (aglat->is_single_const ())
1915 : 3 : value = aglat->values->value;
1916 : : break;
1917 : : }
1918 : : }
1919 : : }
1920 : : }
1921 : :
1922 : 7591 : if (!value)
1923 : 171613 : return NULL_TREE;
1924 : :
1925 : 2869 : if (item->jftype == IPA_JF_LOAD_AGG)
1926 : : {
1927 : 2146 : tree load_type = item->value.load_agg.type;
1928 : 2146 : tree value_type = TREE_TYPE (value);
1929 : :
1930 : : /* Ensure value type is compatible with load type. */
1931 : 2146 : if (!useless_type_conversion_p (load_type, value_type))
1932 : : return NULL_TREE;
1933 : : }
1934 : :
1935 : 5738 : tree cstval = ipa_get_jf_arith_result (item->value.pass_through.operation,
1936 : : value,
1937 : 2869 : item->value.pass_through.operand,
1938 : 2869 : item->value.pass_through.op_type);
1939 : 2869 : return ipacp_value_safe_for_type (item->type, cstval);
1940 : : }
1941 : :
1942 : : /* Process all items in AGG_JFUNC relative to caller (or the node the original
1943 : : caller is inlined to) NODE which described by INFO and push the results to
1944 : : RES as describing values passed in parameter DST_INDEX. */
1945 : :
1946 : : void
1947 : 13914399 : ipa_push_agg_values_from_jfunc (ipa_node_params *info, cgraph_node *node,
1948 : : ipa_agg_jump_function *agg_jfunc,
1949 : : unsigned dst_index,
1950 : : vec<ipa_argagg_value> *res)
1951 : : {
1952 : 13914399 : unsigned prev_unit_offset = 0;
1953 : 13914399 : bool first = true;
1954 : :
1955 : 17997808 : for (const ipa_agg_jf_item &item : agg_jfunc->items)
1956 : : {
1957 : 2102521 : tree value = ipa_agg_value_from_jfunc (info, node, &item);
1958 : 2102521 : if (!value)
1959 : 482007 : continue;
1960 : :
1961 : 1620514 : ipa_argagg_value iav;
1962 : 1620514 : iav.value = value;
1963 : 1620514 : iav.unit_offset = item.offset / BITS_PER_UNIT;
1964 : 1620514 : iav.index = dst_index;
1965 : 1620514 : iav.by_ref = agg_jfunc->by_ref;
1966 : 1620514 : iav.killed = 0;
1967 : :
1968 : 1620514 : gcc_assert (first
1969 : : || iav.unit_offset > prev_unit_offset);
1970 : 1620514 : prev_unit_offset = iav.unit_offset;
1971 : 1620514 : first = false;
1972 : :
1973 : 1620514 : res->safe_push (iav);
1974 : : }
1975 : 13914399 : }
1976 : :
1977 : : /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not
1978 : : bottom, not containing a variable component and without any known value at
1979 : : the same time. */
1980 : :
1981 : : DEBUG_FUNCTION void
1982 : 126810 : ipcp_verify_propagated_values (void)
1983 : : {
1984 : 126810 : struct cgraph_node *node;
1985 : :
1986 : 1393402 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1987 : : {
1988 : 1266592 : ipa_node_params *info = ipa_node_params_sum->get (node);
1989 : 1266592 : if (!opt_for_fn (node->decl, flag_ipa_cp)
1990 : 1266592 : || !opt_for_fn (node->decl, optimize))
1991 : 9848 : continue;
1992 : 1256744 : int i, count = ipa_get_param_count (info);
1993 : :
1994 : 3567670 : for (i = 0; i < count; i++)
1995 : : {
1996 : 2310926 : ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i);
1997 : :
1998 : 2310926 : if (!lat->bottom
1999 : 215520 : && !lat->contains_variable
2000 : 31775 : && lat->values_count == 0)
2001 : : {
2002 : 0 : if (dump_file)
2003 : : {
2004 : 0 : symtab->dump (dump_file);
2005 : 0 : fprintf (dump_file, "\nIPA lattices after constant "
2006 : : "propagation, before gcc_unreachable:\n");
2007 : 0 : print_all_lattices (dump_file, true, false);
2008 : : }
2009 : :
2010 : 0 : gcc_unreachable ();
2011 : : }
2012 : : }
2013 : : }
2014 : 126810 : }
2015 : :
2016 : : /* Return true iff X and Y should be considered equal contexts by IPA-CP. */
2017 : :
2018 : : static bool
2019 : 2566 : values_equal_for_ipcp_p (ipa_polymorphic_call_context x,
2020 : : ipa_polymorphic_call_context y)
2021 : : {
2022 : 2416 : return x.equal_to (y);
2023 : : }
2024 : :
2025 : :
2026 : : /* Add a new value source to the value represented by THIS, marking that a
2027 : : value comes from edge CS and (if the underlying jump function is a
2028 : : pass-through or an ancestor one) from a caller value SRC_VAL of a caller
2029 : : parameter described by SRC_INDEX. OFFSET is negative if the source was the
2030 : : scalar value of the parameter itself or the offset within an aggregate. */
2031 : :
2032 : : template <typename valtype>
2033 : : void
2034 : 333766 : ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val,
2035 : : int src_idx, HOST_WIDE_INT offset)
2036 : : {
2037 : : ipcp_value_source<valtype> *src;
2038 : :
2039 : 476782 : src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>;
2040 : 476782 : src->offset = offset;
2041 : 476782 : src->cs = cs;
2042 : 476782 : src->val = src_val;
2043 : 476782 : src->index = src_idx;
2044 : :
2045 : 476782 : src->next = sources;
2046 : 476782 : sources = src;
2047 : : }
2048 : :
2049 : : /* Allocate a new ipcp_value holding a tree constant, initialize its value to
2050 : : SOURCE and clear all other fields. */
2051 : :
2052 : : static ipcp_value<tree> *
2053 : 134669 : allocate_and_init_ipcp_value (tree cst, unsigned same_lat_gen_level)
2054 : : {
2055 : 134669 : ipcp_value<tree> *val;
2056 : :
2057 : 269338 : val = new (ipcp_cst_values_pool.allocate ()) ipcp_value<tree>();
2058 : 134669 : val->value = cst;
2059 : 134669 : val->self_recursion_generated_level = same_lat_gen_level;
2060 : 134669 : return val;
2061 : : }
2062 : :
2063 : : /* Allocate a new ipcp_value holding a polymorphic context, initialize its
2064 : : value to SOURCE and clear all other fields. */
2065 : :
2066 : : static ipcp_value<ipa_polymorphic_call_context> *
2067 : 8347 : allocate_and_init_ipcp_value (ipa_polymorphic_call_context ctx,
2068 : : unsigned same_lat_gen_level)
2069 : : {
2070 : 8347 : ipcp_value<ipa_polymorphic_call_context> *val;
2071 : :
2072 : 8347 : val = new (ipcp_poly_ctx_values_pool.allocate ())
2073 : 8347 : ipcp_value<ipa_polymorphic_call_context>();
2074 : 8347 : val->value = ctx;
2075 : 8347 : val->self_recursion_generated_level = same_lat_gen_level;
2076 : 8347 : return val;
2077 : : }
2078 : :
2079 : : /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS,
2080 : : SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same
2081 : : meaning. OFFSET -1 means the source is scalar and not a part of an
2082 : : aggregate. If non-NULL, VAL_P records address of existing or newly added
2083 : : ipcp_value.
2084 : :
2085 : : If the value is generated for a self-recursive call as a result of an
2086 : : arithmetic pass-through jump-function acting on a value in the same lattice,
2087 : : SAME_LAT_GEN_LEVEL must be the length of such chain, otherwise it must be
2088 : : zero. If it is non-zero, PARAM_IPA_CP_VALUE_LIST_SIZE limit is ignored. */
2089 : :
2090 : : template <typename valtype>
2091 : : bool
2092 : 488189 : ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs,
2093 : : ipcp_value<valtype> *src_val,
2094 : : int src_idx, HOST_WIDE_INT offset,
2095 : : ipcp_value<valtype> **val_p,
2096 : : unsigned same_lat_gen_level)
2097 : : {
2098 : 488189 : ipcp_value<valtype> *val, *last_val = NULL;
2099 : :
2100 : 488189 : if (val_p)
2101 : 1124 : *val_p = NULL;
2102 : :
2103 : 488189 : if (bottom)
2104 : : return false;
2105 : :
2106 : 951596 : for (val = values; val; last_val = val, val = val->next)
2107 : 807250 : if (values_equal_for_ipcp_p (val->value, newval))
2108 : : {
2109 : 340699 : if (val_p)
2110 : 395 : *val_p = val;
2111 : :
2112 : 340699 : if (val->self_recursion_generated_level < same_lat_gen_level)
2113 : 158 : val->self_recursion_generated_level = same_lat_gen_level;
2114 : :
2115 : 340699 : if (ipa_edge_within_scc (cs))
2116 : : {
2117 : : ipcp_value_source<valtype> *s;
2118 : 43656 : for (s = val->sources; s; s = s->next)
2119 : 40113 : if (s->cs == cs && s->val == src_val)
2120 : : break;
2121 : 10476 : if (s)
2122 : : return false;
2123 : : }
2124 : :
2125 : 333766 : val->add_source (cs, src_val, src_idx, offset);
2126 : 333766 : return false;
2127 : : }
2128 : :
2129 : 144346 : if (!same_lat_gen_level && values_count >= opt_for_fn (cs->callee->decl,
2130 : : param_ipa_cp_value_list_size))
2131 : : {
2132 : : /* We can only free sources, not the values themselves, because sources
2133 : : of other values in this SCC might point to them. */
2134 : 11952 : for (val = values; val; val = val->next)
2135 : : {
2136 : 39797 : while (val->sources)
2137 : : {
2138 : 29175 : ipcp_value_source<valtype> *src = val->sources;
2139 : 29175 : val->sources = src->next;
2140 : 29175 : ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src);
2141 : : }
2142 : : }
2143 : 1330 : values = NULL;
2144 : 1330 : return set_to_bottom ();
2145 : : }
2146 : :
2147 : 143016 : values_count++;
2148 : 143016 : val = allocate_and_init_ipcp_value (newval, same_lat_gen_level);
2149 : 143016 : val->add_source (cs, src_val, src_idx, offset);
2150 : 143016 : val->next = NULL;
2151 : :
2152 : : /* Add the new value to end of value list, which can reduce iterations
2153 : : of propagation stage for recursive function. */
2154 : 143016 : if (last_val)
2155 : 43492 : last_val->next = val;
2156 : : else
2157 : 99524 : values = val;
2158 : :
2159 : 143016 : if (val_p)
2160 : 729 : *val_p = val;
2161 : :
2162 : : return true;
2163 : : }
2164 : :
2165 : : /* A helper function that returns result of operation specified by OPCODE on
2166 : : the value of SRC_VAL. If non-NULL, OPND1_TYPE is expected type for the
2167 : : value of SRC_VAL. If the operation is binary, OPND2 is a constant value
2168 : : acting as its second operand. OP_TYPE is the type in which the operation is
2169 : : performed. */
2170 : :
2171 : : static tree
2172 : 20483 : get_val_across_arith_op (enum tree_code opcode,
2173 : : tree opnd1_type,
2174 : : tree opnd2,
2175 : : ipcp_value<tree> *src_val,
2176 : : tree op_type)
2177 : : {
2178 : 20483 : tree opnd1 = src_val->value;
2179 : :
2180 : : /* Skip source values that is incompatible with specified type. */
2181 : 20483 : if (opnd1_type
2182 : 20483 : && !useless_type_conversion_p (opnd1_type, TREE_TYPE (opnd1)))
2183 : : return NULL_TREE;
2184 : :
2185 : 20483 : return ipa_get_jf_arith_result (opcode, opnd1, opnd2, op_type);
2186 : : }
2187 : :
2188 : : /* Propagate values through an arithmetic transformation described by a jump
2189 : : function associated with edge CS, taking values from SRC_LAT and putting
2190 : : them into DEST_LAT. OPND1_TYPE, if non-NULL, is the expected type for the
2191 : : values in SRC_LAT. OPND2 is a constant value if transformation is a binary
2192 : : operation. SRC_OFFSET specifies offset in an aggregate if SRC_LAT describes
2193 : : lattice of a part of an aggregate, otherwise it should be -1. SRC_IDX is
2194 : : the index of the source parameter. OP_TYPE is the type in which the
2195 : : operation is performed and can be NULL when OPCODE is NOP_EXPR. RES_TYPE is
2196 : : the value type of result being propagated into. Return true if DEST_LAT
2197 : : changed. */
2198 : :
2199 : : static bool
2200 : 75055 : propagate_vals_across_arith_jfunc (cgraph_edge *cs,
2201 : : enum tree_code opcode,
2202 : : tree opnd1_type,
2203 : : tree opnd2,
2204 : : ipcp_lattice<tree> *src_lat,
2205 : : ipcp_lattice<tree> *dest_lat,
2206 : : HOST_WIDE_INT src_offset,
2207 : : int src_idx,
2208 : : tree op_type,
2209 : : tree res_type)
2210 : : {
2211 : 75055 : ipcp_value<tree> *src_val;
2212 : 75055 : bool ret = false;
2213 : :
2214 : : /* Due to circular dependencies, propagating within an SCC through arithmetic
2215 : : transformation would create infinite number of values. But for
2216 : : self-feeding recursive function, we could allow propagation in a limited
2217 : : count, and this can enable a simple kind of recursive function versioning.
2218 : : For other scenario, we would just make lattices bottom. */
2219 : 75055 : if (opcode != NOP_EXPR && ipa_edge_within_scc (cs))
2220 : : {
2221 : 2138 : int i;
2222 : :
2223 : 2138 : int max_recursive_depth = opt_for_fn(cs->caller->decl,
2224 : : param_ipa_cp_max_recursive_depth);
2225 : 2138 : if (src_lat != dest_lat || max_recursive_depth < 1)
2226 : 1647 : return dest_lat->set_contains_variable ();
2227 : :
2228 : : /* No benefit if recursive execution is in low probability. */
2229 : 1254 : if (cs->sreal_frequency () * 100
2230 : 2508 : <= ((sreal) 1) * opt_for_fn (cs->caller->decl,
2231 : : param_ipa_cp_min_recursive_probability))
2232 : 89 : return dest_lat->set_contains_variable ();
2233 : :
2234 : 1165 : auto_vec<ipcp_value<tree> *, 8> val_seeds;
2235 : :
2236 : 2175 : for (src_val = src_lat->values; src_val; src_val = src_val->next)
2237 : : {
2238 : : /* Now we do not use self-recursively generated value as propagation
2239 : : source, this is absolutely conservative, but could avoid explosion
2240 : : of lattice's value space, especially when one recursive function
2241 : : calls another recursive. */
2242 : 1684 : if (src_val->self_recursion_generated_p ())
2243 : : {
2244 : 890 : ipcp_value_source<tree> *s;
2245 : :
2246 : : /* If the lattice has already been propagated for the call site,
2247 : : no need to do that again. */
2248 : 1403 : for (s = src_val->sources; s; s = s->next)
2249 : 1187 : if (s->cs == cs)
2250 : 674 : return dest_lat->set_contains_variable ();
2251 : : }
2252 : : else
2253 : 794 : val_seeds.safe_push (src_val);
2254 : : }
2255 : :
2256 : 982 : gcc_assert ((int) val_seeds.length () <= param_ipa_cp_value_list_size);
2257 : :
2258 : : /* Recursively generate lattice values with a limited count. */
2259 : 767 : FOR_EACH_VEC_ELT (val_seeds, i, src_val)
2260 : : {
2261 : 1262 : for (int j = 1; j < max_recursive_depth; j++)
2262 : : {
2263 : 1126 : tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
2264 : : src_val, op_type);
2265 : 1126 : cstval = ipacp_value_safe_for_type (res_type, cstval);
2266 : 1126 : if (!cstval)
2267 : : break;
2268 : :
2269 : 1124 : ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
2270 : : src_offset, &src_val, j);
2271 : 1124 : gcc_checking_assert (src_val);
2272 : : }
2273 : : }
2274 : 491 : ret |= dest_lat->set_contains_variable ();
2275 : 1165 : }
2276 : : else
2277 : 92399 : for (src_val = src_lat->values; src_val; src_val = src_val->next)
2278 : : {
2279 : : /* Now we do not use self-recursively generated value as propagation
2280 : : source, otherwise it is easy to make value space of normal lattice
2281 : : overflow. */
2282 : 19482 : if (src_val->self_recursion_generated_p ())
2283 : : {
2284 : 125 : ret |= dest_lat->set_contains_variable ();
2285 : 125 : continue;
2286 : : }
2287 : :
2288 : 19357 : tree cstval = get_val_across_arith_op (opcode, opnd1_type, opnd2,
2289 : : src_val, op_type);
2290 : 19357 : cstval = ipacp_value_safe_for_type (res_type, cstval);
2291 : 19357 : if (cstval)
2292 : 19170 : ret |= dest_lat->add_value (cstval, cs, src_val, src_idx,
2293 : : src_offset);
2294 : : else
2295 : 187 : ret |= dest_lat->set_contains_variable ();
2296 : : }
2297 : :
2298 : : return ret;
2299 : : }
2300 : :
2301 : : /* Propagate values through a pass-through jump function JFUNC associated with
2302 : : edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
2303 : : is the index of the source parameter. PARM_TYPE is the type of the
2304 : : parameter to which the result is passed. */
2305 : :
2306 : : static bool
2307 : 70882 : propagate_vals_across_pass_through (cgraph_edge *cs, ipa_jump_func *jfunc,
2308 : : ipcp_lattice<tree> *src_lat,
2309 : : ipcp_lattice<tree> *dest_lat, int src_idx,
2310 : : tree parm_type)
2311 : : {
2312 : 70882 : gcc_checking_assert (parm_type);
2313 : 70882 : enum tree_code opcode = ipa_get_jf_pass_through_operation (jfunc);
2314 : 70882 : tree op_type = (opcode == NOP_EXPR) ? NULL_TREE
2315 : 2293 : : ipa_get_jf_pass_through_op_type (jfunc);
2316 : 70882 : return propagate_vals_across_arith_jfunc (cs, opcode, NULL_TREE,
2317 : : ipa_get_jf_pass_through_operand (jfunc),
2318 : : src_lat, dest_lat, -1, src_idx, op_type,
2319 : 70882 : parm_type);
2320 : : }
2321 : :
2322 : : /* Propagate values through an ancestor jump function JFUNC associated with
2323 : : edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX
2324 : : is the index of the source parameter. */
2325 : :
2326 : : static bool
2327 : 2310 : propagate_vals_across_ancestor (struct cgraph_edge *cs,
2328 : : struct ipa_jump_func *jfunc,
2329 : : ipcp_lattice<tree> *src_lat,
2330 : : ipcp_lattice<tree> *dest_lat, int src_idx,
2331 : : tree param_type)
2332 : : {
2333 : 2310 : ipcp_value<tree> *src_val;
2334 : 2310 : bool ret = false;
2335 : :
2336 : 2310 : if (ipa_edge_within_scc (cs))
2337 : 8 : return dest_lat->set_contains_variable ();
2338 : :
2339 : 2628 : for (src_val = src_lat->values; src_val; src_val = src_val->next)
2340 : : {
2341 : 326 : tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value);
2342 : 326 : t = ipacp_value_safe_for_type (param_type, t);
2343 : 326 : if (t)
2344 : 256 : ret |= dest_lat->add_value (t, cs, src_val, src_idx);
2345 : : else
2346 : 70 : ret |= dest_lat->set_contains_variable ();
2347 : : }
2348 : :
2349 : : return ret;
2350 : : }
2351 : :
2352 : : /* Propagate scalar values across jump function JFUNC that is associated with
2353 : : edge CS and put the values into DEST_LAT. PARM_TYPE is the type of the
2354 : : parameter to which the result is passed. */
2355 : :
2356 : : static bool
2357 : 3815562 : propagate_scalar_across_jump_function (struct cgraph_edge *cs,
2358 : : struct ipa_jump_func *jfunc,
2359 : : ipcp_lattice<tree> *dest_lat,
2360 : : tree param_type)
2361 : : {
2362 : 3815562 : if (dest_lat->bottom)
2363 : : return false;
2364 : :
2365 : 783594 : if (jfunc->type == IPA_JF_CONST)
2366 : : {
2367 : 364941 : tree val = ipa_get_jf_constant (jfunc);
2368 : 364941 : val = ipacp_value_safe_for_type (param_type, val);
2369 : 364941 : if (val)
2370 : 364924 : return dest_lat->add_value (val, cs, NULL, 0);
2371 : : else
2372 : 17 : return dest_lat->set_contains_variable ();
2373 : : }
2374 : 418653 : else if (jfunc->type == IPA_JF_PASS_THROUGH
2375 : 246684 : || jfunc->type == IPA_JF_ANCESTOR)
2376 : : {
2377 : 176642 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2378 : 176642 : ipcp_lattice<tree> *src_lat;
2379 : 176642 : int src_idx;
2380 : 176642 : bool ret;
2381 : :
2382 : 176642 : if (jfunc->type == IPA_JF_PASS_THROUGH)
2383 : 171969 : src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2384 : : else
2385 : 4673 : src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2386 : :
2387 : 176642 : src_lat = ipa_get_scalar_lat (caller_info, src_idx);
2388 : 176642 : if (src_lat->bottom)
2389 : 103278 : return dest_lat->set_contains_variable ();
2390 : :
2391 : : /* If we would need to clone the caller and cannot, do not propagate. */
2392 : 73364 : if (!ipcp_versionable_function_p (cs->caller)
2393 : 73364 : && (src_lat->contains_variable
2394 : 133 : || (src_lat->values_count > 1)))
2395 : 172 : return dest_lat->set_contains_variable ();
2396 : :
2397 : 73192 : if (jfunc->type == IPA_JF_PASS_THROUGH)
2398 : 70882 : ret = propagate_vals_across_pass_through (cs, jfunc, src_lat,
2399 : : dest_lat, src_idx,
2400 : : param_type);
2401 : : else
2402 : 2310 : ret = propagate_vals_across_ancestor (cs, jfunc, src_lat, dest_lat,
2403 : : src_idx, param_type);
2404 : :
2405 : 73192 : if (src_lat->contains_variable)
2406 : 63235 : ret |= dest_lat->set_contains_variable ();
2407 : :
2408 : 73192 : return ret;
2409 : : }
2410 : :
2411 : : /* TODO: We currently do not handle member method pointers in IPA-CP (we only
2412 : : use it for indirect inlining), we should propagate them too. */
2413 : 242011 : return dest_lat->set_contains_variable ();
2414 : : }
2415 : :
2416 : : /* Propagate scalar values across jump function JFUNC that is associated with
2417 : : edge CS and describes argument IDX and put the values into DEST_LAT. */
2418 : :
2419 : : static bool
2420 : 3815562 : propagate_context_across_jump_function (cgraph_edge *cs,
2421 : : ipa_jump_func *jfunc, int idx,
2422 : : ipcp_lattice<ipa_polymorphic_call_context> *dest_lat)
2423 : : {
2424 : 3815562 : if (dest_lat->bottom)
2425 : : return false;
2426 : 878852 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
2427 : 878852 : bool ret = false;
2428 : 878852 : bool added_sth = false;
2429 : 878852 : bool type_preserved = true;
2430 : :
2431 : 878852 : ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr
2432 : 893429 : = ipa_get_ith_polymorhic_call_context (args, idx);
2433 : :
2434 : 14577 : if (edge_ctx_ptr)
2435 : 14577 : edge_ctx = *edge_ctx_ptr;
2436 : :
2437 : 878852 : if (jfunc->type == IPA_JF_PASS_THROUGH
2438 : 706414 : || jfunc->type == IPA_JF_ANCESTOR)
2439 : : {
2440 : 177203 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2441 : 177203 : int src_idx;
2442 : 177203 : ipcp_lattice<ipa_polymorphic_call_context> *src_lat;
2443 : :
2444 : : /* TODO: Once we figure out how to propagate speculations, it will
2445 : : probably be a good idea to switch to speculation if type_preserved is
2446 : : not set instead of punting. */
2447 : 177203 : if (jfunc->type == IPA_JF_PASS_THROUGH)
2448 : : {
2449 : 172438 : if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR)
2450 : 6729 : goto prop_fail;
2451 : 165709 : type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc);
2452 : 165709 : src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2453 : : }
2454 : : else
2455 : : {
2456 : 4765 : type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc);
2457 : 4765 : src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2458 : : }
2459 : :
2460 : 170474 : src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx);
2461 : : /* If we would need to clone the caller and cannot, do not propagate. */
2462 : 170474 : if (!ipcp_versionable_function_p (cs->caller)
2463 : 170474 : && (src_lat->contains_variable
2464 : 13665 : || (src_lat->values_count > 1)))
2465 : 2420 : goto prop_fail;
2466 : :
2467 : 168054 : ipcp_value<ipa_polymorphic_call_context> *src_val;
2468 : 169328 : for (src_val = src_lat->values; src_val; src_val = src_val->next)
2469 : : {
2470 : 1274 : ipa_polymorphic_call_context cur = src_val->value;
2471 : :
2472 : 1274 : if (!type_preserved)
2473 : 902 : cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor);
2474 : 1274 : if (jfunc->type == IPA_JF_ANCESTOR)
2475 : 342 : cur.offset_by (ipa_get_jf_ancestor_offset (jfunc));
2476 : : /* TODO: In cases we know how the context is going to be used,
2477 : : we can improve the result by passing proper OTR_TYPE. */
2478 : 1274 : cur.combine_with (edge_ctx);
2479 : 2548 : if (!cur.useless_p ())
2480 : : {
2481 : 1091 : if (src_lat->contains_variable
2482 : 1091 : && !edge_ctx.equal_to (cur))
2483 : 273 : ret |= dest_lat->set_contains_variable ();
2484 : 1091 : ret |= dest_lat->add_value (cur, cs, src_val, src_idx);
2485 : 1091 : added_sth = true;
2486 : : }
2487 : : }
2488 : : }
2489 : :
2490 : 701649 : prop_fail:
2491 : 177203 : if (!added_sth)
2492 : : {
2493 : 877821 : if (!edge_ctx.useless_p ())
2494 : 9006 : ret |= dest_lat->add_value (edge_ctx, cs);
2495 : : else
2496 : 868815 : ret |= dest_lat->set_contains_variable ();
2497 : : }
2498 : :
2499 : : return ret;
2500 : : }
2501 : :
2502 : : /* Propagate bits across jfunc that is associated with
2503 : : edge cs and update dest_lattice accordingly. */
2504 : :
2505 : : bool
2506 : 3815562 : propagate_bits_across_jump_function (cgraph_edge *cs, int idx,
2507 : : ipa_jump_func *jfunc,
2508 : : ipcp_bits_lattice *dest_lattice)
2509 : : {
2510 : 3815562 : if (dest_lattice->bottom_p ())
2511 : : return false;
2512 : :
2513 : 480930 : enum availability availability;
2514 : 480930 : cgraph_node *callee = cs->callee->function_symbol (&availability);
2515 : 480930 : ipa_node_params *callee_info = ipa_node_params_sum->get (callee);
2516 : 480930 : tree parm_type = ipa_get_type (callee_info, idx);
2517 : :
2518 : : /* For K&R C programs, ipa_get_type() could return NULL_TREE. Avoid the
2519 : : transform for these cases. Similarly, we can have bad type mismatches
2520 : : with LTO, avoid doing anything with those too. */
2521 : 480930 : if (!parm_type
2522 : 480930 : || (!INTEGRAL_TYPE_P (parm_type) && !POINTER_TYPE_P (parm_type)))
2523 : : {
2524 : 28991 : if (dump_file && (dump_flags & TDF_DETAILS))
2525 : 7 : fprintf (dump_file, "Setting dest_lattice to bottom, because type of "
2526 : : "param %i of %s is NULL or unsuitable for bits propagation\n",
2527 : 7 : idx, cs->callee->dump_name ());
2528 : :
2529 : 28991 : return dest_lattice->set_to_bottom ();
2530 : : }
2531 : :
2532 : 451939 : if (jfunc->type == IPA_JF_PASS_THROUGH
2533 : 364439 : || jfunc->type == IPA_JF_ANCESTOR)
2534 : : {
2535 : 89857 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2536 : 89857 : tree operand = NULL_TREE;
2537 : 89857 : tree op_type = NULL_TREE;
2538 : 89857 : enum tree_code code;
2539 : 89857 : unsigned src_idx;
2540 : 89857 : bool keep_null = false;
2541 : :
2542 : 89857 : if (jfunc->type == IPA_JF_PASS_THROUGH)
2543 : : {
2544 : 87500 : code = ipa_get_jf_pass_through_operation (jfunc);
2545 : 87500 : src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2546 : 87500 : if (code != NOP_EXPR)
2547 : : {
2548 : 1631 : operand = ipa_get_jf_pass_through_operand (jfunc);
2549 : 1631 : op_type = ipa_get_jf_pass_through_op_type (jfunc);
2550 : : }
2551 : : }
2552 : : else
2553 : : {
2554 : 2357 : code = POINTER_PLUS_EXPR;
2555 : 2357 : src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2556 : 2357 : unsigned HOST_WIDE_INT offset
2557 : 2357 : = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
2558 : 2357 : keep_null = (ipa_get_jf_ancestor_keep_null (jfunc) || !offset);
2559 : 2357 : operand = build_int_cstu (size_type_node, offset);
2560 : : }
2561 : :
2562 : 89857 : class ipcp_param_lattices *src_lats
2563 : 89857 : = ipa_get_parm_lattices (caller_info, src_idx);
2564 : :
2565 : : /* Try to propagate bits if src_lattice is bottom, but jfunc is known.
2566 : : for eg consider:
2567 : : int f(int x)
2568 : : {
2569 : : g (x & 0xff);
2570 : : }
2571 : : Assume lattice for x is bottom, however we can still propagate
2572 : : result of x & 0xff == 0xff, which gets computed during ccp1 pass
2573 : : and we store it in jump function during analysis stage. */
2574 : :
2575 : 89857 : if (!src_lats->bits_lattice.bottom_p ())
2576 : : {
2577 : 21954 : if (!op_type)
2578 : 20764 : op_type = ipa_get_type (caller_info, src_idx);
2579 : :
2580 : 21954 : unsigned precision = TYPE_PRECISION (op_type);
2581 : 21954 : signop sgn = TYPE_SIGN (op_type);
2582 : 21954 : bool drop_all_ones
2583 : 21954 : = keep_null && !src_lats->bits_lattice.known_nonzero_p ();
2584 : :
2585 : 21954 : return dest_lattice->meet_with (src_lats->bits_lattice, precision,
2586 : 21954 : sgn, code, operand, drop_all_ones);
2587 : : }
2588 : : }
2589 : :
2590 : 429985 : value_range vr (parm_type);
2591 : 429985 : if (jfunc->m_vr)
2592 : : {
2593 : 375074 : jfunc->m_vr->get_vrange (vr);
2594 : 375074 : if (!vr.undefined_p () && !vr.varying_p ())
2595 : : {
2596 : 375074 : irange_bitmask bm = vr.get_bitmask ();
2597 : 375074 : widest_int mask
2598 : 375074 : = widest_int::from (bm.mask (), TYPE_SIGN (parm_type));
2599 : 375074 : widest_int value
2600 : 375074 : = widest_int::from (bm.value (), TYPE_SIGN (parm_type));
2601 : 375074 : return dest_lattice->meet_with (value, mask,
2602 : 375074 : TYPE_PRECISION (parm_type));
2603 : 375074 : }
2604 : : }
2605 : 54911 : return dest_lattice->set_to_bottom ();
2606 : 429985 : }
2607 : :
2608 : : /* Propagate value range across jump function JFUNC that is associated with
2609 : : edge CS with param of callee of PARAM_TYPE and update DEST_PLATS
2610 : : accordingly. */
2611 : :
2612 : : static bool
2613 : 3814725 : propagate_vr_across_jump_function (cgraph_edge *cs, ipa_jump_func *jfunc,
2614 : : class ipcp_param_lattices *dest_plats,
2615 : : tree param_type)
2616 : : {
2617 : 3814725 : ipcp_vr_lattice *dest_lat = &dest_plats->m_value_range;
2618 : :
2619 : 3814725 : if (dest_lat->bottom_p ())
2620 : : return false;
2621 : :
2622 : 573274 : if (!param_type
2623 : 573274 : || !ipa_vr_supported_type_p (param_type))
2624 : 28981 : return dest_lat->set_to_bottom ();
2625 : :
2626 : 544293 : value_range vr (param_type);
2627 : 544293 : vr.set_varying (param_type);
2628 : 544293 : if (jfunc->m_vr)
2629 : 478299 : ipa_vr_operation_and_type_effects (vr, *jfunc->m_vr, NOP_EXPR,
2630 : : param_type,
2631 : 478299 : jfunc->m_vr->type ());
2632 : :
2633 : 544293 : if (jfunc->type == IPA_JF_PASS_THROUGH)
2634 : : {
2635 : 81193 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2636 : 81193 : int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2637 : 81193 : class ipcp_param_lattices *src_lats
2638 : 81193 : = ipa_get_parm_lattices (caller_info, src_idx);
2639 : 81193 : tree operand_type = ipa_get_type (caller_info, src_idx);
2640 : :
2641 : 81193 : if (src_lats->m_value_range.bottom_p ())
2642 : 65204 : return dest_lat->set_to_bottom ();
2643 : :
2644 : 15989 : if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR
2645 : 15989 : || !ipa_edge_within_scc (cs))
2646 : 15495 : ipa_vr_intersect_with_arith_jfunc (vr, jfunc, cs->caller,
2647 : 15495 : src_lats->m_value_range.m_vr,
2648 : : operand_type, param_type);
2649 : : }
2650 : :
2651 : 479089 : if (!vr.undefined_p () && !vr.varying_p ())
2652 : 458099 : return dest_lat->meet_with (vr);
2653 : : else
2654 : 20990 : return dest_lat->set_to_bottom ();
2655 : 544293 : }
2656 : :
2657 : : /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches
2658 : : NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all
2659 : : other cases, return false). If there are no aggregate items, set
2660 : : aggs_by_ref to NEW_AGGS_BY_REF. */
2661 : :
2662 : : static bool
2663 : 40347 : set_check_aggs_by_ref (class ipcp_param_lattices *dest_plats,
2664 : : bool new_aggs_by_ref)
2665 : : {
2666 : 0 : if (dest_plats->aggs)
2667 : : {
2668 : 21240 : if (dest_plats->aggs_by_ref != new_aggs_by_ref)
2669 : : {
2670 : 0 : set_agg_lats_to_bottom (dest_plats);
2671 : 0 : return true;
2672 : : }
2673 : : }
2674 : : else
2675 : 19107 : dest_plats->aggs_by_ref = new_aggs_by_ref;
2676 : : return false;
2677 : : }
2678 : :
2679 : : /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an
2680 : : already existing lattice for the given OFFSET and SIZE, marking all skipped
2681 : : lattices as containing variable and checking for overlaps. If there is no
2682 : : already existing lattice for the OFFSET and VAL_SIZE, create one, initialize
2683 : : it with offset, size and contains_variable to PRE_EXISTING, and return true,
2684 : : unless there are too many already. If there are two many, return false. If
2685 : : there are overlaps turn whole DEST_PLATS to bottom and return false. If any
2686 : : skipped lattices were newly marked as containing variable, set *CHANGE to
2687 : : true. MAX_AGG_ITEMS is the maximum number of lattices. */
2688 : :
2689 : : static bool
2690 : 107047 : merge_agg_lats_step (class ipcp_param_lattices *dest_plats,
2691 : : HOST_WIDE_INT offset, HOST_WIDE_INT val_size,
2692 : : struct ipcp_agg_lattice ***aglat,
2693 : : bool pre_existing, bool *change, int max_agg_items)
2694 : : {
2695 : 107047 : gcc_checking_assert (offset >= 0);
2696 : :
2697 : 110734 : while (**aglat && (**aglat)->offset < offset)
2698 : : {
2699 : 3687 : if ((**aglat)->offset + (**aglat)->size > offset)
2700 : : {
2701 : 0 : set_agg_lats_to_bottom (dest_plats);
2702 : 0 : return false;
2703 : : }
2704 : 3687 : *change |= (**aglat)->set_contains_variable ();
2705 : 3687 : *aglat = &(**aglat)->next;
2706 : : }
2707 : :
2708 : 107047 : if (**aglat && (**aglat)->offset == offset)
2709 : : {
2710 : 52910 : if ((**aglat)->size != val_size)
2711 : : {
2712 : 13 : set_agg_lats_to_bottom (dest_plats);
2713 : 13 : return false;
2714 : : }
2715 : 52897 : gcc_assert (!(**aglat)->next
2716 : : || (**aglat)->next->offset >= offset + val_size);
2717 : : return true;
2718 : : }
2719 : : else
2720 : : {
2721 : 54137 : struct ipcp_agg_lattice *new_al;
2722 : :
2723 : 54137 : if (**aglat && (**aglat)->offset < offset + val_size)
2724 : : {
2725 : 3 : set_agg_lats_to_bottom (dest_plats);
2726 : 3 : return false;
2727 : : }
2728 : 54134 : if (dest_plats->aggs_count == max_agg_items)
2729 : : return false;
2730 : 54095 : dest_plats->aggs_count++;
2731 : 54095 : new_al = ipcp_agg_lattice_pool.allocate ();
2732 : :
2733 : 54095 : new_al->offset = offset;
2734 : 54095 : new_al->size = val_size;
2735 : 54095 : new_al->contains_variable = pre_existing;
2736 : :
2737 : 54095 : new_al->next = **aglat;
2738 : 54095 : **aglat = new_al;
2739 : 54095 : return true;
2740 : : }
2741 : : }
2742 : :
2743 : : /* Set all AGLAT and all other aggregate lattices reachable by next pointers as
2744 : : containing an unknown value. */
2745 : :
2746 : : static bool
2747 : 40327 : set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat)
2748 : : {
2749 : 40327 : bool ret = false;
2750 : 42667 : while (aglat)
2751 : : {
2752 : 2340 : ret |= aglat->set_contains_variable ();
2753 : 2340 : aglat = aglat->next;
2754 : : }
2755 : 40327 : return ret;
2756 : : }
2757 : :
2758 : : /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting
2759 : : DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source
2760 : : parameter used for lattice value sources. Return true if DEST_PLATS changed
2761 : : in any way. */
2762 : :
2763 : : static bool
2764 : 4437 : merge_aggregate_lattices (struct cgraph_edge *cs,
2765 : : class ipcp_param_lattices *dest_plats,
2766 : : class ipcp_param_lattices *src_plats,
2767 : : int src_idx, HOST_WIDE_INT offset_delta)
2768 : : {
2769 : 4437 : bool pre_existing = dest_plats->aggs != NULL;
2770 : 4437 : struct ipcp_agg_lattice **dst_aglat;
2771 : 4437 : bool ret = false;
2772 : :
2773 : 4437 : if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref))
2774 : 0 : return true;
2775 : 4437 : if (src_plats->aggs_bottom)
2776 : 4 : return set_agg_lats_contain_variable (dest_plats);
2777 : 4433 : if (src_plats->aggs_contain_variable)
2778 : 2686 : ret |= set_agg_lats_contain_variable (dest_plats);
2779 : 4433 : dst_aglat = &dest_plats->aggs;
2780 : :
2781 : 4433 : int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
2782 : : param_ipa_max_agg_items);
2783 : 4433 : for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs;
2784 : 12850 : src_aglat;
2785 : 8417 : src_aglat = src_aglat->next)
2786 : : {
2787 : 8417 : HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta;
2788 : :
2789 : 8417 : if (new_offset < 0)
2790 : 11 : continue;
2791 : 8406 : if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size,
2792 : : &dst_aglat, pre_existing, &ret, max_agg_items))
2793 : : {
2794 : 8402 : struct ipcp_agg_lattice *new_al = *dst_aglat;
2795 : :
2796 : 8402 : dst_aglat = &(*dst_aglat)->next;
2797 : 8402 : if (src_aglat->bottom)
2798 : : {
2799 : 0 : ret |= new_al->set_contains_variable ();
2800 : 0 : continue;
2801 : : }
2802 : 8402 : if (src_aglat->contains_variable)
2803 : 5156 : ret |= new_al->set_contains_variable ();
2804 : 8402 : for (ipcp_value<tree> *val = src_aglat->values;
2805 : 12595 : val;
2806 : 4193 : val = val->next)
2807 : 4193 : ret |= new_al->add_value (val->value, cs, val, src_idx,
2808 : : src_aglat->offset);
2809 : : }
2810 : 4 : else if (dest_plats->aggs_bottom)
2811 : : return true;
2812 : : }
2813 : 4433 : ret |= set_chain_of_aglats_contains_variable (*dst_aglat);
2814 : 4433 : return ret;
2815 : : }
2816 : :
2817 : : /* Determine whether there is anything to propagate FROM SRC_PLATS through a
2818 : : pass-through JFUNC and if so, whether it has conform and conforms to the
2819 : : rules about propagating values passed by reference. */
2820 : :
2821 : : static bool
2822 : 165530 : agg_pass_through_permissible_p (class ipcp_param_lattices *src_plats,
2823 : : struct ipa_jump_func *jfunc)
2824 : : {
2825 : 165530 : return src_plats->aggs
2826 : 165530 : && (!src_plats->aggs_by_ref
2827 : 4898 : || ipa_get_jf_pass_through_agg_preserved (jfunc));
2828 : : }
2829 : :
2830 : : /* Propagate values through ITEM, jump function for a part of an aggregate,
2831 : : into corresponding aggregate lattice AGLAT. CS is the call graph edge
2832 : : associated with the jump function. Return true if AGLAT changed in any
2833 : : way. */
2834 : :
2835 : : static bool
2836 : 98590 : propagate_aggregate_lattice (struct cgraph_edge *cs,
2837 : : struct ipa_agg_jf_item *item,
2838 : : struct ipcp_agg_lattice *aglat)
2839 : : {
2840 : 98590 : class ipa_node_params *caller_info;
2841 : 98590 : class ipcp_param_lattices *src_plats;
2842 : 98590 : struct ipcp_lattice<tree> *src_lat;
2843 : 98590 : HOST_WIDE_INT src_offset;
2844 : 98590 : int src_idx;
2845 : 98590 : tree load_type;
2846 : 98590 : bool ret;
2847 : :
2848 : 98590 : if (item->jftype == IPA_JF_CONST)
2849 : : {
2850 : 88425 : tree value = item->value.constant;
2851 : :
2852 : 88425 : gcc_checking_assert (is_gimple_ip_invariant (value));
2853 : 88425 : return aglat->add_value (value, cs, NULL, 0);
2854 : : }
2855 : :
2856 : 10165 : gcc_checking_assert (item->jftype == IPA_JF_PASS_THROUGH
2857 : : || item->jftype == IPA_JF_LOAD_AGG);
2858 : :
2859 : 10165 : caller_info = ipa_node_params_sum->get (cs->caller);
2860 : 10165 : src_idx = item->value.pass_through.formal_id;
2861 : 10165 : src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2862 : :
2863 : 10165 : if (item->jftype == IPA_JF_PASS_THROUGH)
2864 : : {
2865 : 2822 : load_type = NULL_TREE;
2866 : 2822 : src_lat = &src_plats->itself;
2867 : 2822 : src_offset = -1;
2868 : : }
2869 : : else
2870 : : {
2871 : 7343 : HOST_WIDE_INT load_offset = item->value.load_agg.offset;
2872 : 7343 : struct ipcp_agg_lattice *src_aglat;
2873 : :
2874 : 10000 : for (src_aglat = src_plats->aggs; src_aglat; src_aglat = src_aglat->next)
2875 : 6048 : if (src_aglat->offset >= load_offset)
2876 : : break;
2877 : :
2878 : 7343 : load_type = item->value.load_agg.type;
2879 : 7343 : if (!src_aglat
2880 : 3391 : || src_aglat->offset > load_offset
2881 : 3195 : || src_aglat->size != tree_to_shwi (TYPE_SIZE (load_type))
2882 : 10538 : || src_plats->aggs_by_ref != item->value.load_agg.by_ref)
2883 : 4148 : return aglat->set_contains_variable ();
2884 : :
2885 : : src_lat = src_aglat;
2886 : : src_offset = load_offset;
2887 : : }
2888 : :
2889 : 6017 : if (src_lat->bottom
2890 : 6017 : || (!ipcp_versionable_function_p (cs->caller)
2891 : 6017 : && !src_lat->is_single_const ()))
2892 : 1844 : return aglat->set_contains_variable ();
2893 : :
2894 : 4173 : ret = propagate_vals_across_arith_jfunc (cs,
2895 : : item->value.pass_through.operation,
2896 : : load_type,
2897 : : item->value.pass_through.operand,
2898 : : src_lat, aglat,
2899 : : src_offset,
2900 : : src_idx,
2901 : : item->value.pass_through.op_type,
2902 : : item->type);
2903 : :
2904 : 4173 : if (src_lat->contains_variable)
2905 : 2457 : ret |= aglat->set_contains_variable ();
2906 : :
2907 : : return ret;
2908 : : }
2909 : :
2910 : : /* Propagate scalar values across jump function JFUNC that is associated with
2911 : : edge CS and put the values into DEST_LAT. */
2912 : :
2913 : : static bool
2914 : 3815562 : propagate_aggs_across_jump_function (struct cgraph_edge *cs,
2915 : : struct ipa_jump_func *jfunc,
2916 : : class ipcp_param_lattices *dest_plats)
2917 : : {
2918 : 3815562 : bool ret = false;
2919 : :
2920 : 3815562 : if (dest_plats->aggs_bottom)
2921 : : return false;
2922 : :
2923 : 877646 : if (jfunc->type == IPA_JF_PASS_THROUGH
2924 : 877646 : && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
2925 : : {
2926 : 165530 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2927 : 165530 : int src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
2928 : 165530 : class ipcp_param_lattices *src_plats;
2929 : :
2930 : 165530 : src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2931 : 165530 : if (agg_pass_through_permissible_p (src_plats, jfunc))
2932 : : {
2933 : : /* Currently we do not produce clobber aggregate jump
2934 : : functions, replace with merging when we do. */
2935 : 4342 : gcc_assert (!jfunc->agg.items);
2936 : 4342 : ret |= merge_aggregate_lattices (cs, dest_plats, src_plats,
2937 : : src_idx, 0);
2938 : 4342 : return ret;
2939 : : }
2940 : : }
2941 : 712116 : else if (jfunc->type == IPA_JF_ANCESTOR
2942 : 712116 : && ipa_get_jf_ancestor_agg_preserved (jfunc))
2943 : : {
2944 : 1235 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
2945 : 1235 : int src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
2946 : 1235 : class ipcp_param_lattices *src_plats;
2947 : :
2948 : 1235 : src_plats = ipa_get_parm_lattices (caller_info, src_idx);
2949 : 1235 : if (src_plats->aggs && src_plats->aggs_by_ref)
2950 : : {
2951 : : /* Currently we do not produce clobber aggregate jump
2952 : : functions, replace with merging when we do. */
2953 : 95 : gcc_assert (!jfunc->agg.items);
2954 : 95 : ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx,
2955 : : ipa_get_jf_ancestor_offset (jfunc));
2956 : : }
2957 : 1140 : else if (!src_plats->aggs_by_ref)
2958 : 1132 : ret |= set_agg_lats_to_bottom (dest_plats);
2959 : : else
2960 : 8 : ret |= set_agg_lats_contain_variable (dest_plats);
2961 : 1235 : return ret;
2962 : : }
2963 : :
2964 : 872069 : if (jfunc->agg.items)
2965 : : {
2966 : 35910 : bool pre_existing = dest_plats->aggs != NULL;
2967 : 35910 : struct ipcp_agg_lattice **aglat = &dest_plats->aggs;
2968 : 35910 : struct ipa_agg_jf_item *item;
2969 : 35910 : int i;
2970 : :
2971 : 35910 : if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref))
2972 : 16 : return true;
2973 : :
2974 : 35910 : int max_agg_items = opt_for_fn (cs->callee->function_symbol ()->decl,
2975 : : param_ipa_max_agg_items);
2976 : 134879 : FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item)
2977 : : {
2978 : 98985 : HOST_WIDE_INT val_size;
2979 : :
2980 : 98985 : if (item->offset < 0 || item->jftype == IPA_JF_UNKNOWN)
2981 : 344 : continue;
2982 : 98641 : val_size = tree_to_shwi (TYPE_SIZE (item->type));
2983 : :
2984 : 98641 : if (merge_agg_lats_step (dest_plats, item->offset, val_size,
2985 : : &aglat, pre_existing, &ret, max_agg_items))
2986 : : {
2987 : 98590 : ret |= propagate_aggregate_lattice (cs, item, *aglat);
2988 : 98590 : aglat = &(*aglat)->next;
2989 : : }
2990 : 51 : else if (dest_plats->aggs_bottom)
2991 : : return true;
2992 : : }
2993 : :
2994 : 71788 : ret |= set_chain_of_aglats_contains_variable (*aglat);
2995 : : }
2996 : : else
2997 : 836159 : ret |= set_agg_lats_contain_variable (dest_plats);
2998 : :
2999 : 872053 : return ret;
3000 : : }
3001 : :
3002 : : /* Return true if on the way cfrom CS->caller to the final (non-alias and
3003 : : non-thunk) destination, the call passes through a thunk. */
3004 : :
3005 : : static bool
3006 : 1723233 : call_passes_through_thunk (cgraph_edge *cs)
3007 : : {
3008 : 1723233 : cgraph_node *alias_or_thunk = cs->callee;
3009 : 1853021 : while (alias_or_thunk->alias)
3010 : 129788 : alias_or_thunk = alias_or_thunk->get_alias_target ();
3011 : 1723233 : return alias_or_thunk->thunk;
3012 : : }
3013 : :
3014 : : /* Propagate constants from the caller to the callee of CS. INFO describes the
3015 : : caller. */
3016 : :
3017 : : static bool
3018 : 5188051 : propagate_constants_across_call (struct cgraph_edge *cs)
3019 : : {
3020 : 5188051 : class ipa_node_params *callee_info;
3021 : 5188051 : enum availability availability;
3022 : 5188051 : cgraph_node *callee;
3023 : 5188051 : class ipa_edge_args *args;
3024 : 5188051 : bool ret = false;
3025 : 5188051 : int i, args_count, parms_count;
3026 : :
3027 : 5188051 : callee = cs->callee->function_symbol (&availability);
3028 : 5188051 : if (!callee->definition)
3029 : : return false;
3030 : 1911920 : gcc_checking_assert (callee->has_gimple_body_p ());
3031 : 1911920 : callee_info = ipa_node_params_sum->get (callee);
3032 : 1911920 : if (!callee_info)
3033 : : return false;
3034 : :
3035 : 1903181 : args = ipa_edge_args_sum->get (cs);
3036 : 1903181 : parms_count = ipa_get_param_count (callee_info);
3037 : 1710908 : if (parms_count == 0)
3038 : : return false;
3039 : 1710908 : if (!args
3040 : 1710664 : || !opt_for_fn (cs->caller->decl, flag_ipa_cp)
3041 : 3421572 : || !opt_for_fn (cs->caller->decl, optimize))
3042 : : {
3043 : 752 : for (i = 0; i < parms_count; i++)
3044 : 508 : ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
3045 : : i));
3046 : : return ret;
3047 : : }
3048 : 1710664 : args_count = ipa_get_cs_argument_count (args);
3049 : :
3050 : : /* If this call goes through a thunk we must not propagate to the first (0th)
3051 : : parameter. However, we might need to uncover a thunk from below a series
3052 : : of aliases first. */
3053 : 1710664 : if (call_passes_through_thunk (cs))
3054 : : {
3055 : 172 : ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info,
3056 : : 0));
3057 : 172 : i = 1;
3058 : : }
3059 : : else
3060 : : i = 0;
3061 : :
3062 : 5662654 : for (; (i < args_count) && (i < parms_count); i++)
3063 : : {
3064 : 3951990 : struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i);
3065 : 3951990 : class ipcp_param_lattices *dest_plats;
3066 : 3951990 : tree param_type = ipa_get_type (callee_info, i);
3067 : :
3068 : 3951990 : dest_plats = ipa_get_parm_lattices (callee_info, i);
3069 : 3951990 : if (availability == AVAIL_INTERPOSABLE)
3070 : 136428 : ret |= set_all_contains_variable (dest_plats);
3071 : : else
3072 : : {
3073 : 3815562 : ret |= propagate_scalar_across_jump_function (cs, jump_func,
3074 : : &dest_plats->itself,
3075 : : param_type);
3076 : 3815562 : ret |= propagate_context_across_jump_function (cs, jump_func, i,
3077 : : &dest_plats->ctxlat);
3078 : 3815562 : ret
3079 : 3815562 : |= propagate_bits_across_jump_function (cs, i, jump_func,
3080 : : &dest_plats->bits_lattice);
3081 : 3815562 : ret |= propagate_aggs_across_jump_function (cs, jump_func,
3082 : : dest_plats);
3083 : 3815562 : if (opt_for_fn (callee->decl, flag_ipa_vrp))
3084 : 3814725 : ret |= propagate_vr_across_jump_function (cs, jump_func,
3085 : : dest_plats, param_type);
3086 : : else
3087 : 837 : ret |= dest_plats->m_value_range.set_to_bottom ();
3088 : : }
3089 : : }
3090 : 1710831 : for (; i < parms_count; i++)
3091 : 167 : ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i));
3092 : :
3093 : : return ret;
3094 : : }
3095 : :
3096 : : /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS
3097 : : KNOWN_CONTEXTS, and known aggregates either in AVS or KNOWN_AGGS return
3098 : : the destination. The latter three can be NULL. If AGG_REPS is not NULL,
3099 : : KNOWN_AGGS is ignored. */
3100 : :
3101 : : static tree
3102 : 637549 : ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
3103 : : const vec<tree> &known_csts,
3104 : : const vec<ipa_polymorphic_call_context> &known_contexts,
3105 : : const ipa_argagg_value_list &avs,
3106 : : bool *speculative)
3107 : : {
3108 : 637549 : int param_index = ie->indirect_info->param_index;
3109 : 637549 : HOST_WIDE_INT anc_offset;
3110 : 637549 : tree t = NULL;
3111 : 637549 : tree target = NULL;
3112 : :
3113 : 637549 : *speculative = false;
3114 : :
3115 : 637549 : if (param_index == -1)
3116 : : return NULL_TREE;
3117 : :
3118 : 375833 : if (!ie->indirect_info->polymorphic)
3119 : : {
3120 : 248662 : tree t = NULL;
3121 : :
3122 : 248662 : if (ie->indirect_info->agg_contents)
3123 : : {
3124 : 54147 : t = NULL;
3125 : 54147 : if ((unsigned) param_index < known_csts.length ()
3126 : 54147 : && known_csts[param_index])
3127 : 50081 : t = ipa_find_agg_cst_from_init (known_csts[param_index],
3128 : : ie->indirect_info->offset,
3129 : : ie->indirect_info->by_ref);
3130 : :
3131 : 54147 : if (!t && ie->indirect_info->guaranteed_unmodified)
3132 : 51038 : t = avs.get_value (param_index,
3133 : 51038 : ie->indirect_info->offset / BITS_PER_UNIT,
3134 : : ie->indirect_info->by_ref);
3135 : : }
3136 : 194515 : else if ((unsigned) param_index < known_csts.length ())
3137 : 194515 : t = known_csts[param_index];
3138 : :
3139 : 248603 : if (t
3140 : 188800 : && TREE_CODE (t) == ADDR_EXPR
3141 : 437328 : && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL)
3142 : 188725 : return TREE_OPERAND (t, 0);
3143 : : else
3144 : 59937 : return NULL_TREE;
3145 : : }
3146 : :
3147 : 127171 : if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
3148 : : return NULL_TREE;
3149 : :
3150 : 127171 : gcc_assert (!ie->indirect_info->agg_contents);
3151 : 127171 : gcc_assert (!ie->indirect_info->by_ref);
3152 : 127171 : anc_offset = ie->indirect_info->offset;
3153 : :
3154 : 127171 : t = NULL;
3155 : :
3156 : 127171 : if ((unsigned) param_index < known_csts.length ()
3157 : 127171 : && known_csts[param_index])
3158 : 26261 : t = ipa_find_agg_cst_from_init (known_csts[param_index],
3159 : : ie->indirect_info->offset, true);
3160 : :
3161 : : /* Try to work out value of virtual table pointer value in replacements. */
3162 : : /* or known aggregate values. */
3163 : 26261 : if (!t)
3164 : 127165 : t = avs.get_value (param_index,
3165 : 127165 : ie->indirect_info->offset / BITS_PER_UNIT,
3166 : : true);
3167 : :
3168 : : /* If we found the virtual table pointer, lookup the target. */
3169 : 127165 : if (t)
3170 : : {
3171 : 18534 : tree vtable;
3172 : 18534 : unsigned HOST_WIDE_INT offset;
3173 : 18534 : if (vtable_pointer_value_to_vtable (t, &vtable, &offset))
3174 : : {
3175 : 18534 : bool can_refer;
3176 : 18534 : target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token,
3177 : : vtable, offset, &can_refer);
3178 : 18534 : if (can_refer)
3179 : : {
3180 : 18496 : if (!target
3181 : 18496 : || fndecl_built_in_p (target, BUILT_IN_UNREACHABLE)
3182 : 36872 : || !possible_polymorphic_call_target_p
3183 : 18376 : (ie, cgraph_node::get (target)))
3184 : : {
3185 : : /* Do not speculate builtin_unreachable, it is stupid! */
3186 : 235 : if (ie->indirect_info->vptr_changed)
3187 : 14050 : return NULL;
3188 : 235 : target = ipa_impossible_devirt_target (ie, target);
3189 : : }
3190 : 18496 : *speculative = ie->indirect_info->vptr_changed;
3191 : 18496 : if (!*speculative)
3192 : : return target;
3193 : : }
3194 : : }
3195 : : }
3196 : :
3197 : : /* Do we know the constant value of pointer? */
3198 : 113121 : if (!t && (unsigned) param_index < known_csts.length ())
3199 : 35598 : t = known_csts[param_index];
3200 : :
3201 : 113121 : gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO);
3202 : :
3203 : 113121 : ipa_polymorphic_call_context context;
3204 : 113121 : if (known_contexts.length () > (unsigned int) param_index)
3205 : : {
3206 : 113112 : context = known_contexts[param_index];
3207 : 113112 : context.offset_by (anc_offset);
3208 : 113112 : if (ie->indirect_info->vptr_changed)
3209 : 47988 : context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
3210 : : ie->indirect_info->otr_type);
3211 : 113112 : if (t)
3212 : : {
3213 : 12528 : ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context
3214 : 12528 : (t, ie->indirect_info->otr_type, anc_offset);
3215 : 25056 : if (!ctx2.useless_p ())
3216 : 11048 : context.combine_with (ctx2, ie->indirect_info->otr_type);
3217 : : }
3218 : : }
3219 : 9 : else if (t)
3220 : : {
3221 : 12 : context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type,
3222 : 6 : anc_offset);
3223 : 6 : if (ie->indirect_info->vptr_changed)
3224 : 0 : context.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
3225 : : ie->indirect_info->otr_type);
3226 : : }
3227 : : else
3228 : : return NULL_TREE;
3229 : :
3230 : 113118 : vec <cgraph_node *>targets;
3231 : 113118 : bool final;
3232 : :
3233 : 113118 : targets = possible_polymorphic_call_targets
3234 : 226236 : (ie->indirect_info->otr_type,
3235 : 113118 : ie->indirect_info->otr_token,
3236 : : context, &final);
3237 : 124709 : if (!final || targets.length () > 1)
3238 : : {
3239 : 101855 : struct cgraph_node *node;
3240 : 101855 : if (*speculative)
3241 : : return target;
3242 : 101833 : if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively)
3243 : 101833 : || ie->speculative || !ie->maybe_hot_p ())
3244 : 22056 : return NULL;
3245 : 159554 : node = try_speculative_devirtualization (ie->indirect_info->otr_type,
3246 : 79777 : ie->indirect_info->otr_token,
3247 : : context);
3248 : 79777 : if (node)
3249 : : {
3250 : 965 : *speculative = true;
3251 : 965 : target = node->decl;
3252 : : }
3253 : : else
3254 : : return NULL;
3255 : : }
3256 : : else
3257 : : {
3258 : 11263 : *speculative = false;
3259 : 11263 : if (targets.length () == 1)
3260 : 11248 : target = targets[0]->decl;
3261 : : else
3262 : 15 : target = ipa_impossible_devirt_target (ie, NULL_TREE);
3263 : : }
3264 : :
3265 : 12228 : if (target && !possible_polymorphic_call_target_p (ie,
3266 : : cgraph_node::get (target)))
3267 : : {
3268 : 55 : if (*speculative)
3269 : : return NULL;
3270 : 37 : target = ipa_impossible_devirt_target (ie, target);
3271 : : }
3272 : :
3273 : : return target;
3274 : : }
3275 : :
3276 : : /* If an indirect edge IE can be turned into a direct one based on data in
3277 : : AVALS, return the destination. Store into *SPECULATIVE a boolean determinig
3278 : : whether the discovered target is only speculative guess. */
3279 : :
3280 : : tree
3281 : 521511 : ipa_get_indirect_edge_target (struct cgraph_edge *ie,
3282 : : ipa_call_arg_values *avals,
3283 : : bool *speculative)
3284 : : {
3285 : 521511 : ipa_argagg_value_list avl (avals);
3286 : 521511 : return ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
3287 : 521511 : avals->m_known_contexts,
3288 : 521511 : avl, speculative);
3289 : : }
3290 : :
3291 : : /* Calculate devirtualization time bonus for NODE, assuming we know information
3292 : : about arguments stored in AVALS. */
3293 : :
3294 : : static int
3295 : 938588 : devirtualization_time_bonus (struct cgraph_node *node,
3296 : : ipa_auto_call_arg_values *avals)
3297 : : {
3298 : 938588 : struct cgraph_edge *ie;
3299 : 938588 : int res = 0;
3300 : :
3301 : 1052809 : for (ie = node->indirect_calls; ie; ie = ie->next_callee)
3302 : : {
3303 : 114221 : struct cgraph_node *callee;
3304 : 114221 : class ipa_fn_summary *isummary;
3305 : 114221 : enum availability avail;
3306 : 114221 : tree target;
3307 : 114221 : bool speculative;
3308 : :
3309 : 114221 : ipa_argagg_value_list avl (avals);
3310 : 114221 : target = ipa_get_indirect_edge_target_1 (ie, avals->m_known_vals,
3311 : : avals->m_known_contexts,
3312 : : avl, &speculative);
3313 : 114221 : if (!target)
3314 : 113301 : continue;
3315 : :
3316 : : /* Only bare minimum benefit for clearly un-inlineable targets. */
3317 : 1104 : res += 1;
3318 : 1104 : callee = cgraph_node::get (target);
3319 : 1104 : if (!callee || !callee->definition)
3320 : 134 : continue;
3321 : 970 : callee = callee->function_symbol (&avail);
3322 : 970 : if (avail < AVAIL_AVAILABLE)
3323 : 0 : continue;
3324 : 970 : isummary = ipa_fn_summaries->get (callee);
3325 : 970 : if (!isummary || !isummary->inlinable)
3326 : 50 : continue;
3327 : :
3328 : 920 : int size = ipa_size_summaries->get (callee)->size;
3329 : : /* FIXME: The values below need re-considering and perhaps also
3330 : : integrating into the cost metrics, at lest in some very basic way. */
3331 : 920 : int max_inline_insns_auto
3332 : 920 : = opt_for_fn (callee->decl, param_max_inline_insns_auto);
3333 : 920 : if (size <= max_inline_insns_auto / 4)
3334 : 210 : res += 31 / ((int)speculative + 1);
3335 : 710 : else if (size <= max_inline_insns_auto / 2)
3336 : 226 : res += 15 / ((int)speculative + 1);
3337 : 484 : else if (size <= max_inline_insns_auto
3338 : 484 : || DECL_DECLARED_INLINE_P (callee->decl))
3339 : 58 : res += 7 / ((int)speculative + 1);
3340 : : }
3341 : :
3342 : 938588 : return res;
3343 : : }
3344 : :
3345 : : /* Return time bonus incurred because of hints stored in ESTIMATES. */
3346 : :
3347 : : static sreal
3348 : 172517 : hint_time_bonus (cgraph_node *node, const ipa_call_estimates &estimates)
3349 : : {
3350 : 172517 : sreal result = 0;
3351 : 172517 : ipa_hints hints = estimates.hints;
3352 : 172517 : if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride))
3353 : 11210 : result += opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
3354 : :
3355 : 172517 : sreal bonus_for_one = opt_for_fn (node->decl, param_ipa_cp_loop_hint_bonus);
3356 : :
3357 : 172517 : if (hints & INLINE_HINT_loop_iterations)
3358 : 7246 : result += estimates.loops_with_known_iterations * bonus_for_one;
3359 : :
3360 : 172517 : if (hints & INLINE_HINT_loop_stride)
3361 : 5126 : result += estimates.loops_with_known_strides * bonus_for_one;
3362 : :
3363 : 172517 : return result;
3364 : : }
3365 : :
3366 : : /* If there is a reason to penalize the function described by INFO in the
3367 : : cloning goodness evaluation, do so. */
3368 : :
3369 : : static inline sreal
3370 : 37734 : incorporate_penalties (cgraph_node *node, ipa_node_params *info,
3371 : : sreal evaluation)
3372 : : {
3373 : 37734 : if (info->node_within_scc && !info->node_is_self_scc)
3374 : 846 : evaluation = (evaluation
3375 : 846 : * (100 - opt_for_fn (node->decl,
3376 : 1692 : param_ipa_cp_recursion_penalty))) / 100;
3377 : :
3378 : 37734 : if (info->node_calling_single_call)
3379 : 2534 : evaluation = (evaluation
3380 : 2534 : * (100 - opt_for_fn (node->decl,
3381 : 2534 : param_ipa_cp_single_call_penalty)))
3382 : 5068 : / 100;
3383 : :
3384 : 37734 : return evaluation;
3385 : : }
3386 : :
3387 : : /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT
3388 : : and SIZE_COST and with the sum of frequencies of incoming edges to the
3389 : : potential new clone in FREQUENCIES. */
3390 : :
3391 : : static bool
3392 : 214228 : good_cloning_opportunity_p (struct cgraph_node *node, sreal time_benefit,
3393 : : sreal freq_sum, profile_count count_sum,
3394 : : int size_cost, bool called_without_ipa_profile)
3395 : : {
3396 : 214228 : gcc_assert (count_sum.ipa () == count_sum);
3397 : 214228 : if (count_sum.quality () == AFDO)
3398 : 0 : count_sum = count_sum.force_nonzero ();
3399 : 214228 : if (time_benefit == 0
3400 : 190911 : || !opt_for_fn (node->decl, flag_ipa_cp_clone)
3401 : : /* If there is no call which was executed in profiling or where
3402 : : profile is missing, we do not want to clone. */
3403 : 47658 : || (!called_without_ipa_profile && !count_sum.nonzero_p ()))
3404 : 176494 : return false;
3405 : :
3406 : 37734 : gcc_assert (size_cost > 0);
3407 : :
3408 : 37734 : ipa_node_params *info = ipa_node_params_sum->get (node);
3409 : 37734 : int eval_threshold = opt_for_fn (node->decl, param_ipa_cp_eval_threshold);
3410 : : /* If we know the execution IPA execution counts, we can estimate overall
3411 : : speedup of the program. */
3412 : 37734 : if (count_sum.nonzero_p ())
3413 : : {
3414 : 164 : profile_count saved_time = count_sum * time_benefit;
3415 : 164 : sreal evaluation = saved_time.to_sreal_scale (profile_count::one ())
3416 : 328 : / size_cost;
3417 : 164 : evaluation = incorporate_penalties (node, info, evaluation);
3418 : :
3419 : 164 : if (dump_file && (dump_flags & TDF_DETAILS))
3420 : : {
3421 : 0 : fprintf (dump_file, " good_cloning_opportunity_p (time: %g, "
3422 : : "size: %i, count_sum: ", time_benefit.to_double (),
3423 : : size_cost);
3424 : 0 : count_sum.dump (dump_file);
3425 : 0 : fprintf (dump_file, ", overall time saved: ");
3426 : 0 : saved_time.dump (dump_file);
3427 : 0 : fprintf (dump_file, "%s%s) -> evaluation: %.2f, threshold: %i\n",
3428 : 0 : info->node_within_scc
3429 : 0 : ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
3430 : 0 : info->node_calling_single_call ? ", single_call" : "",
3431 : : evaluation.to_double (), eval_threshold);
3432 : : }
3433 : 164 : gcc_checking_assert (saved_time == saved_time.ipa ());
3434 : 164 : if (!maybe_hot_count_p (NULL, saved_time))
3435 : : {
3436 : 24 : if (dump_file && (dump_flags & TDF_DETAILS))
3437 : 0 : fprintf (dump_file, " not cloning: time saved is not hot\n");
3438 : : }
3439 : : /* Evaulation approximately corresponds to time saved per instruction
3440 : : introduced. This is likely almost always going to be true, since we
3441 : : already checked that time saved is large enough to be considered
3442 : : hot. */
3443 : 140 : else if (evaluation >= (sreal)eval_threshold)
3444 : 164 : return true;
3445 : : /* If all call sites have profile known; we know we do not want t clone.
3446 : : If there are calls with unknown profile; try local heuristics. */
3447 : 133 : if (!called_without_ipa_profile)
3448 : : return false;
3449 : : }
3450 : 37570 : sreal evaluation = (time_benefit * freq_sum) / size_cost;
3451 : 37570 : evaluation = incorporate_penalties (node, info, evaluation);
3452 : 37570 : evaluation *= 1000;
3453 : :
3454 : 37570 : if (dump_file && (dump_flags & TDF_DETAILS))
3455 : 226 : fprintf (dump_file, " good_cloning_opportunity_p (time: %g, "
3456 : : "size: %i, freq_sum: %g%s%s) -> evaluation: %.2f, "
3457 : : "threshold: %i\n",
3458 : : time_benefit.to_double (), size_cost, freq_sum.to_double (),
3459 : 113 : info->node_within_scc
3460 : 26 : ? (info->node_is_self_scc ? ", self_scc" : ", scc") : "",
3461 : 113 : info->node_calling_single_call ? ", single_call" : "",
3462 : : evaluation.to_double (), eval_threshold);
3463 : :
3464 : 37570 : return evaluation >= eval_threshold;
3465 : : }
3466 : :
3467 : : /* Grow vectors in AVALS and fill them with information about values of
3468 : : parameters that are known to be independent of the context. Only calculate
3469 : : m_known_aggs if CALCULATE_AGGS is true. INFO describes the function. If
3470 : : REMOVABLE_PARAMS_COST is non-NULL, the movement cost of all removable
3471 : : parameters will be stored in it.
3472 : :
3473 : : TODO: Also grow context independent value range vectors. */
3474 : :
3475 : : static bool
3476 : 1753093 : gather_context_independent_values (class ipa_node_params *info,
3477 : : ipa_auto_call_arg_values *avals,
3478 : : bool calculate_aggs,
3479 : : int *removable_params_cost)
3480 : : {
3481 : 1753093 : int i, count = ipa_get_param_count (info);
3482 : 1753093 : bool ret = false;
3483 : :
3484 : 1753093 : avals->m_known_vals.safe_grow_cleared (count, true);
3485 : 1753093 : avals->m_known_contexts.safe_grow_cleared (count, true);
3486 : :
3487 : 1753093 : if (removable_params_cost)
3488 : 867018 : *removable_params_cost = 0;
3489 : :
3490 : 5852782 : for (i = 0; i < count; i++)
3491 : : {
3492 : 4099689 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3493 : 4099689 : ipcp_lattice<tree> *lat = &plats->itself;
3494 : :
3495 : 4099689 : if (lat->is_single_const ())
3496 : : {
3497 : 52832 : ipcp_value<tree> *val = lat->values;
3498 : 52832 : gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
3499 : 52832 : avals->m_known_vals[i] = val->value;
3500 : 52832 : if (removable_params_cost)
3501 : 35230 : *removable_params_cost
3502 : 17615 : += estimate_move_cost (TREE_TYPE (val->value), false);
3503 : : ret = true;
3504 : : }
3505 : 4046857 : else if (removable_params_cost
3506 : 4046857 : && !ipa_is_param_used (info, i))
3507 : 436854 : *removable_params_cost
3508 : 218427 : += ipa_get_param_move_cost (info, i);
3509 : :
3510 : 4099689 : if (!ipa_is_param_used (info, i))
3511 : 454322 : continue;
3512 : :
3513 : 3645367 : ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3514 : : /* Do not account known context as reason for cloning. We can see
3515 : : if it permits devirtualization. */
3516 : 3645367 : if (ctxlat->is_single_const ())
3517 : 13884 : avals->m_known_contexts[i] = ctxlat->values->value;
3518 : :
3519 : 3645367 : if (calculate_aggs)
3520 : 1801733 : ret |= push_agg_values_from_plats (plats, i, 0, &avals->m_known_aggs);
3521 : : }
3522 : :
3523 : 1753093 : return ret;
3524 : : }
3525 : :
3526 : : /* Perform time and size measurement of NODE with the context given in AVALS,
3527 : : calculate the benefit compared to the node without specialization and store
3528 : : it into VAL. Take into account REMOVABLE_PARAMS_COST of all
3529 : : context-independent or unused removable parameters and EST_MOVE_COST, the
3530 : : estimated movement of the considered parameter. */
3531 : :
3532 : : static void
3533 : 71631 : perform_estimation_of_a_value (cgraph_node *node,
3534 : : ipa_auto_call_arg_values *avals,
3535 : : int removable_params_cost, int est_move_cost,
3536 : : ipcp_value_base *val)
3537 : : {
3538 : 71631 : sreal time_benefit;
3539 : 71631 : ipa_call_estimates estimates;
3540 : :
3541 : 71631 : estimate_ipcp_clone_size_and_time (node, avals, &estimates);
3542 : :
3543 : : /* Extern inline functions have no cloning local time benefits because they
3544 : : will be inlined anyway. The only reason to clone them is if it enables
3545 : : optimization in any of the functions they call. */
3546 : 71631 : if (DECL_EXTERNAL (node->decl) && DECL_DECLARED_INLINE_P (node->decl))
3547 : 61 : time_benefit = 0;
3548 : : else
3549 : 71570 : time_benefit = (estimates.nonspecialized_time - estimates.time)
3550 : 71570 : + hint_time_bonus (node, estimates)
3551 : 71570 : + (devirtualization_time_bonus (node, avals)
3552 : 143140 : + removable_params_cost + est_move_cost);
3553 : :
3554 : 71631 : int size = estimates.size;
3555 : 71631 : gcc_checking_assert (size >=0);
3556 : : /* The inliner-heuristics based estimates may think that in certain
3557 : : contexts some functions do not have any size at all but we want
3558 : : all specializations to have at least a tiny cost, not least not to
3559 : : divide by zero. */
3560 : 71631 : if (size == 0)
3561 : 0 : size = 1;
3562 : :
3563 : 71631 : val->local_time_benefit = time_benefit;
3564 : 71631 : val->local_size_cost = size;
3565 : 71631 : }
3566 : :
3567 : : /* Get the overall limit oof growth based on parameters extracted from growth.
3568 : : it does not really make sense to mix functions with different overall growth
3569 : : limits but it is possible and if it happens, we do not want to select one
3570 : : limit at random. */
3571 : :
3572 : : static long
3573 : 87178 : get_max_overall_size (cgraph_node *node)
3574 : : {
3575 : 87178 : long max_new_size = orig_overall_size;
3576 : 87178 : long large_unit = opt_for_fn (node->decl, param_ipa_cp_large_unit_insns);
3577 : 87178 : if (max_new_size < large_unit)
3578 : : max_new_size = large_unit;
3579 : 87178 : int unit_growth = opt_for_fn (node->decl, param_ipa_cp_unit_growth);
3580 : 87178 : max_new_size += max_new_size * unit_growth / 100 + 1;
3581 : 87178 : return max_new_size;
3582 : : }
3583 : :
3584 : : /* Return true if NODE should be cloned just for a parameter removal, possibly
3585 : : dumping a reason if not. */
3586 : :
3587 : : static bool
3588 : 131753 : clone_for_param_removal_p (cgraph_node *node)
3589 : : {
3590 : 131753 : if (!node->can_change_signature)
3591 : : {
3592 : 2733 : if (dump_file && (dump_flags & TDF_DETAILS))
3593 : 0 : fprintf (dump_file, " Not considering cloning to remove parameters, "
3594 : : "function cannot change signature.\n");
3595 : 2733 : return false;
3596 : : }
3597 : 129020 : if (node->can_be_local_p ())
3598 : : {
3599 : 43120 : if (dump_file && (dump_flags & TDF_DETAILS))
3600 : 1 : fprintf (dump_file, " Not considering cloning to remove parameters, "
3601 : : "IPA-SRA can do it potentially better.\n");
3602 : 43120 : return false;
3603 : : }
3604 : : return true;
3605 : : }
3606 : :
3607 : : /* Iterate over known values of parameters of NODE and estimate the local
3608 : : effects in terms of time and size they have. */
3609 : :
3610 : : static void
3611 : 1256761 : estimate_local_effects (struct cgraph_node *node)
3612 : : {
3613 : 1256761 : ipa_node_params *info = ipa_node_params_sum->get (node);
3614 : 1256761 : int count = ipa_get_param_count (info);
3615 : 1031513 : bool always_const;
3616 : 1031513 : int removable_params_cost;
3617 : :
3618 : 1031513 : if (!count || !ipcp_versionable_function_p (node))
3619 : 389743 : return;
3620 : :
3621 : 867018 : if (dump_file && (dump_flags & TDF_DETAILS))
3622 : 106 : fprintf (dump_file, "\nEstimating effects for %s.\n", node->dump_name ());
3623 : :
3624 : 867018 : ipa_auto_call_arg_values avals;
3625 : 867018 : always_const = gather_context_independent_values (info, &avals, true,
3626 : : &removable_params_cost);
3627 : 867018 : int devirt_bonus = devirtualization_time_bonus (node, &avals);
3628 : 867018 : if (always_const || devirt_bonus
3629 : 867018 : || (removable_params_cost && clone_for_param_removal_p (node)))
3630 : : {
3631 : 100947 : struct caller_statistics stats;
3632 : 100947 : ipa_call_estimates estimates;
3633 : :
3634 : 100947 : init_caller_stats (&stats);
3635 : 100947 : node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
3636 : : false);
3637 : 100947 : estimate_ipcp_clone_size_and_time (node, &avals, &estimates);
3638 : 100947 : sreal time = estimates.nonspecialized_time - estimates.time;
3639 : 100947 : time += devirt_bonus;
3640 : 100947 : time += hint_time_bonus (node, estimates);
3641 : 100947 : time += removable_params_cost;
3642 : 100947 : int size = estimates.size - stats.n_calls * removable_params_cost;
3643 : :
3644 : 100947 : if (dump_file)
3645 : 260 : fprintf (dump_file, " - context independent values, size: %i, "
3646 : : "time_benefit: %f\n", size, (time).to_double ());
3647 : :
3648 : 100947 : if (size <= 0 || node->local)
3649 : : {
3650 : 17092 : info->do_clone_for_all_contexts = true;
3651 : :
3652 : 17092 : if (dump_file)
3653 : 95 : fprintf (dump_file, " Decided to specialize for all "
3654 : : "known contexts, code not going to grow.\n");
3655 : : }
3656 : 83855 : else if (good_cloning_opportunity_p (node, time, stats.freq_sum,
3657 : : stats.count_sum, size,
3658 : 83855 : stats.called_without_ipa_profile))
3659 : : {
3660 : 245 : if (size + overall_size <= get_max_overall_size (node))
3661 : : {
3662 : 245 : info->do_clone_for_all_contexts = true;
3663 : 245 : overall_size += size;
3664 : :
3665 : 245 : if (dump_file)
3666 : 9 : fprintf (dump_file, " Decided to specialize for all "
3667 : : "known contexts, growth (to %li) deemed "
3668 : : "beneficial.\n", overall_size);
3669 : : }
3670 : 0 : else if (dump_file && (dump_flags & TDF_DETAILS))
3671 : 0 : fprintf (dump_file, " Not cloning for all contexts because "
3672 : : "maximum unit size would be reached with %li.\n",
3673 : : size + overall_size);
3674 : : }
3675 : 83610 : else if (dump_file && (dump_flags & TDF_DETAILS))
3676 : 4 : fprintf (dump_file, " Not cloning for all contexts because "
3677 : : "!good_cloning_opportunity_p.\n");
3678 : :
3679 : : }
3680 : :
3681 : 2889925 : for (int i = 0; i < count; i++)
3682 : : {
3683 : 2022907 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3684 : 2022907 : ipcp_lattice<tree> *lat = &plats->itself;
3685 : 2022907 : ipcp_value<tree> *val;
3686 : :
3687 : 4025486 : if (lat->bottom
3688 : 205049 : || !lat->values
3689 : 2060850 : || avals.m_known_vals[i])
3690 : 2002579 : continue;
3691 : :
3692 : 62204 : for (val = lat->values; val; val = val->next)
3693 : : {
3694 : 41876 : gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO);
3695 : 41876 : avals.m_known_vals[i] = val->value;
3696 : :
3697 : 41876 : int emc = estimate_move_cost (TREE_TYPE (val->value), true);
3698 : 41876 : perform_estimation_of_a_value (node, &avals, removable_params_cost,
3699 : : emc, val);
3700 : :
3701 : 41876 : if (dump_file && (dump_flags & TDF_DETAILS))
3702 : : {
3703 : 43 : fprintf (dump_file, " - estimates for value ");
3704 : 43 : print_ipcp_constant_value (dump_file, val->value);
3705 : 43 : fprintf (dump_file, " for ");
3706 : 43 : ipa_dump_param (dump_file, info, i);
3707 : 43 : fprintf (dump_file, ": time_benefit: %g, size: %i\n",
3708 : : val->local_time_benefit.to_double (),
3709 : : val->local_size_cost);
3710 : : }
3711 : : }
3712 : 20328 : avals.m_known_vals[i] = NULL_TREE;
3713 : : }
3714 : :
3715 : 2889925 : for (int i = 0; i < count; i++)
3716 : : {
3717 : 2022907 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3718 : :
3719 : 2022907 : if (!plats->virt_call)
3720 : 2014686 : continue;
3721 : :
3722 : 8221 : ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3723 : 8221 : ipcp_value<ipa_polymorphic_call_context> *val;
3724 : :
3725 : 16239 : if (ctxlat->bottom
3726 : 2873 : || !ctxlat->values
3727 : 11090 : || !avals.m_known_contexts[i].useless_p ())
3728 : 8018 : continue;
3729 : :
3730 : 531 : for (val = ctxlat->values; val; val = val->next)
3731 : : {
3732 : 328 : avals.m_known_contexts[i] = val->value;
3733 : 328 : perform_estimation_of_a_value (node, &avals, removable_params_cost,
3734 : : 0, val);
3735 : :
3736 : 328 : if (dump_file && (dump_flags & TDF_DETAILS))
3737 : : {
3738 : 0 : fprintf (dump_file, " - estimates for polymorphic context ");
3739 : 0 : print_ipcp_constant_value (dump_file, val->value);
3740 : 0 : fprintf (dump_file, " for ");
3741 : 0 : ipa_dump_param (dump_file, info, i);
3742 : 0 : fprintf (dump_file, ": time_benefit: %g, size: %i\n",
3743 : : val->local_time_benefit.to_double (),
3744 : : val->local_size_cost);
3745 : : }
3746 : : }
3747 : 203 : avals.m_known_contexts[i] = ipa_polymorphic_call_context ();
3748 : : }
3749 : :
3750 : 867018 : unsigned all_ctx_len = avals.m_known_aggs.length ();
3751 : 867018 : auto_vec<ipa_argagg_value, 32> all_ctx;
3752 : 867018 : all_ctx.reserve_exact (all_ctx_len);
3753 : 867018 : all_ctx.splice (avals.m_known_aggs);
3754 : 867018 : avals.m_known_aggs.safe_grow_cleared (all_ctx_len + 1);
3755 : :
3756 : 867018 : unsigned j = 0;
3757 : 2889925 : for (int index = 0; index < count; index++)
3758 : : {
3759 : 2022907 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, index);
3760 : :
3761 : 2022907 : if (plats->aggs_bottom || !plats->aggs)
3762 : 2004276 : continue;
3763 : :
3764 : 70650 : for (ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next)
3765 : : {
3766 : 52019 : ipcp_value<tree> *val;
3767 : 51679 : if (aglat->bottom || !aglat->values
3768 : : /* If the following is true, the one value is already part of all
3769 : : context estimations. */
3770 : 96444 : || (!plats->aggs_contain_variable
3771 : 27154 : && aglat->is_single_const ()))
3772 : 30557 : continue;
3773 : :
3774 : 21462 : unsigned unit_offset = aglat->offset / BITS_PER_UNIT;
3775 : 21462 : while (j < all_ctx_len
3776 : 29618 : && (all_ctx[j].index < index
3777 : 3217 : || (all_ctx[j].index == index
3778 : 2290 : && all_ctx[j].unit_offset < unit_offset)))
3779 : : {
3780 : 3140 : avals.m_known_aggs[j] = all_ctx[j];
3781 : 3140 : j++;
3782 : : }
3783 : :
3784 : 30454 : for (unsigned k = j; k < all_ctx_len; k++)
3785 : 8992 : avals.m_known_aggs[k+1] = all_ctx[k];
3786 : :
3787 : 50889 : for (val = aglat->values; val; val = val->next)
3788 : : {
3789 : 29427 : avals.m_known_aggs[j].value = val->value;
3790 : 29427 : avals.m_known_aggs[j].unit_offset = unit_offset;
3791 : 29427 : avals.m_known_aggs[j].index = index;
3792 : 29427 : avals.m_known_aggs[j].by_ref = plats->aggs_by_ref;
3793 : 29427 : avals.m_known_aggs[j].killed = false;
3794 : :
3795 : 29427 : perform_estimation_of_a_value (node, &avals,
3796 : : removable_params_cost, 0, val);
3797 : :
3798 : 29427 : if (dump_file && (dump_flags & TDF_DETAILS))
3799 : : {
3800 : 76 : fprintf (dump_file, " - estimates for value ");
3801 : 76 : print_ipcp_constant_value (dump_file, val->value);
3802 : 76 : fprintf (dump_file, " for ");
3803 : 76 : ipa_dump_param (dump_file, info, index);
3804 : 152 : fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
3805 : : "]: time_benefit: %g, size: %i\n",
3806 : 76 : plats->aggs_by_ref ? "ref " : "",
3807 : : aglat->offset,
3808 : : val->local_time_benefit.to_double (),
3809 : : val->local_size_cost);
3810 : : }
3811 : : }
3812 : : }
3813 : : }
3814 : 867018 : }
3815 : :
3816 : :
3817 : : /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the
3818 : : topological sort of values. */
3819 : :
3820 : : template <typename valtype>
3821 : : void
3822 : 132529 : value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val)
3823 : : {
3824 : : ipcp_value_source<valtype> *src;
3825 : :
3826 : 132529 : if (cur_val->dfs)
3827 : : return;
3828 : :
3829 : 132368 : dfs_counter++;
3830 : 132368 : cur_val->dfs = dfs_counter;
3831 : 132368 : cur_val->low_link = dfs_counter;
3832 : :
3833 : 132368 : cur_val->topo_next = stack;
3834 : 132368 : stack = cur_val;
3835 : 132368 : cur_val->on_stack = true;
3836 : :
3837 : 579925 : for (src = cur_val->sources; src; src = src->next)
3838 : 447557 : if (src->val)
3839 : : {
3840 : 20637 : if (src->val->dfs == 0)
3841 : : {
3842 : 185 : add_val (src->val);
3843 : 185 : if (src->val->low_link < cur_val->low_link)
3844 : 19 : cur_val->low_link = src->val->low_link;
3845 : : }
3846 : 20452 : else if (src->val->on_stack
3847 : 1437 : && src->val->dfs < cur_val->low_link)
3848 : 72 : cur_val->low_link = src->val->dfs;
3849 : : }
3850 : :
3851 : 132368 : if (cur_val->dfs == cur_val->low_link)
3852 : : {
3853 : : ipcp_value<valtype> *v, *scc_list = NULL;
3854 : :
3855 : : do
3856 : : {
3857 : 132368 : v = stack;
3858 : 132368 : stack = v->topo_next;
3859 : 132368 : v->on_stack = false;
3860 : 132368 : v->scc_no = cur_val->dfs;
3861 : :
3862 : 132368 : v->scc_next = scc_list;
3863 : 132368 : scc_list = v;
3864 : : }
3865 : 132368 : while (v != cur_val);
3866 : :
3867 : 132281 : cur_val->topo_next = values_topo;
3868 : 132281 : values_topo = cur_val;
3869 : : }
3870 : : }
3871 : :
3872 : : /* Add all values in lattices associated with NODE to the topological sort if
3873 : : they are not there yet. */
3874 : :
3875 : : static void
3876 : 1256761 : add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo)
3877 : : {
3878 : 1256761 : ipa_node_params *info = ipa_node_params_sum->get (node);
3879 : 1256761 : int i, count = ipa_get_param_count (info);
3880 : :
3881 : 3567701 : for (i = 0; i < count; i++)
3882 : : {
3883 : 2310940 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
3884 : 2310940 : ipcp_lattice<tree> *lat = &plats->itself;
3885 : 2310940 : struct ipcp_agg_lattice *aglat;
3886 : :
3887 : 2310940 : if (!lat->bottom)
3888 : : {
3889 : 215520 : ipcp_value<tree> *val;
3890 : 285781 : for (val = lat->values; val; val = val->next)
3891 : 70261 : topo->constants.add_val (val);
3892 : : }
3893 : :
3894 : 2310940 : if (!plats->aggs_bottom)
3895 : 269402 : for (aglat = plats->aggs; aglat; aglat = aglat->next)
3896 : 54044 : if (!aglat->bottom)
3897 : : {
3898 : 53704 : ipcp_value<tree> *val;
3899 : 107472 : for (val = aglat->values; val; val = val->next)
3900 : 53768 : topo->constants.add_val (val);
3901 : : }
3902 : :
3903 : 2310940 : ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
3904 : 2310940 : if (!ctxlat->bottom)
3905 : : {
3906 : 216502 : ipcp_value<ipa_polymorphic_call_context> *ctxval;
3907 : 224817 : for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next)
3908 : 8315 : topo->contexts.add_val (ctxval);
3909 : : }
3910 : : }
3911 : 1256761 : }
3912 : :
3913 : : /* One pass of constants propagation along the call graph edges, from callers
3914 : : to callees (requires topological ordering in TOPO), iterate over strongly
3915 : : connected components. */
3916 : :
3917 : : static void
3918 : 126818 : propagate_constants_topo (class ipa_topo_info *topo)
3919 : : {
3920 : 126818 : int i;
3921 : :
3922 : 1463701 : for (i = topo->nnodes - 1; i >= 0; i--)
3923 : : {
3924 : 1336883 : unsigned j;
3925 : 1336883 : struct cgraph_node *v, *node = topo->order[i];
3926 : 1336883 : vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node);
3927 : :
3928 : : /* First, iteratively propagate within the strongly connected component
3929 : : until all lattices stabilize. */
3930 : 2679579 : FOR_EACH_VEC_ELT (cycle_nodes, j, v)
3931 : 1342696 : if (v->has_gimple_body_p ())
3932 : : {
3933 : 1266609 : if (opt_for_fn (v->decl, flag_ipa_cp)
3934 : 1266609 : && opt_for_fn (v->decl, optimize))
3935 : 1256761 : push_node_to_stack (topo, v);
3936 : : /* When V is not optimized, we can not push it to stack, but
3937 : : still we need to set all its callees lattices to bottom. */
3938 : : else
3939 : : {
3940 : 23003 : for (cgraph_edge *cs = v->callees; cs; cs = cs->next_callee)
3941 : 13155 : propagate_constants_across_call (cs);
3942 : : }
3943 : : }
3944 : :
3945 : 1336883 : v = pop_node_from_stack (topo);
3946 : 3933381 : while (v)
3947 : : {
3948 : 1259615 : struct cgraph_edge *cs;
3949 : 1259615 : class ipa_node_params *info = NULL;
3950 : 1259615 : bool self_scc = true;
3951 : :
3952 : 6459715 : for (cs = v->callees; cs; cs = cs->next_callee)
3953 : 5200100 : if (ipa_edge_within_scc (cs))
3954 : : {
3955 : 31847 : cgraph_node *callee = cs->callee->function_symbol ();
3956 : :
3957 : 31847 : if (v != callee)
3958 : 21037 : self_scc = false;
3959 : :
3960 : 31847 : if (!info)
3961 : : {
3962 : 13995 : info = ipa_node_params_sum->get (v);
3963 : 13995 : info->node_within_scc = true;
3964 : : }
3965 : :
3966 : 31847 : if (propagate_constants_across_call (cs))
3967 : 3757 : push_node_to_stack (topo, callee);
3968 : : }
3969 : :
3970 : 1259615 : if (info)
3971 : 13995 : info->node_is_self_scc = self_scc;
3972 : :
3973 : 1259615 : v = pop_node_from_stack (topo);
3974 : : }
3975 : :
3976 : : /* Afterwards, propagate along edges leading out of the SCC, calculates
3977 : : the local effects of the discovered constants and all valid values to
3978 : : their topological sort. */
3979 : 2679579 : FOR_EACH_VEC_ELT (cycle_nodes, j, v)
3980 : 1342696 : if (v->has_gimple_body_p ()
3981 : 1266609 : && opt_for_fn (v->decl, flag_ipa_cp)
3982 : 2599457 : && opt_for_fn (v->decl, optimize))
3983 : : {
3984 : 1256761 : struct cgraph_edge *cs;
3985 : :
3986 : 1256761 : estimate_local_effects (v);
3987 : 1256761 : add_all_node_vals_to_toposort (v, topo);
3988 : 6424740 : for (cs = v->callees; cs; cs = cs->next_callee)
3989 : 5167979 : if (!ipa_edge_within_scc (cs))
3990 : 5143049 : propagate_constants_across_call (cs);
3991 : : }
3992 : 1336883 : cycle_nodes.release ();
3993 : : }
3994 : 126818 : }
3995 : :
3996 : : /* Propagate the estimated effects of individual values along the topological
3997 : : from the dependent values to those they depend on. */
3998 : :
3999 : : template <typename valtype>
4000 : : void
4001 : 253636 : value_topo_info<valtype>::propagate_effects ()
4002 : : {
4003 : : ipcp_value<valtype> *base;
4004 : 253636 : hash_set<ipcp_value<valtype> *> processed_srcvals;
4005 : :
4006 : 385917 : for (base = values_topo; base; base = base->topo_next)
4007 : : {
4008 : : ipcp_value_source<valtype> *src;
4009 : : ipcp_value<valtype> *val;
4010 : 132281 : sreal time = 0;
4011 : 132281 : HOST_WIDE_INT size = 0;
4012 : :
4013 : 264649 : for (val = base; val; val = val->scc_next)
4014 : : {
4015 : 132368 : time = time + val->local_time_benefit + val->prop_time_benefit;
4016 : 132368 : size = size + val->local_size_cost + val->prop_size_cost;
4017 : : }
4018 : :
4019 : 264649 : for (val = base; val; val = val->scc_next)
4020 : : {
4021 : 132368 : processed_srcvals.empty ();
4022 : 579925 : for (src = val->sources; src; src = src->next)
4023 : 447557 : if (src->val
4024 : 447557 : && cs_interesting_for_ipcp_p (src->cs))
4025 : : {
4026 : 20595 : if (!processed_srcvals.add (src->val))
4027 : : {
4028 : 16241 : HOST_WIDE_INT prop_size = size + src->val->prop_size_cost;
4029 : 16241 : if (prop_size < INT_MAX)
4030 : 16241 : src->val->prop_size_cost = prop_size;
4031 : : else
4032 : 0 : continue;
4033 : : }
4034 : :
4035 : 20595 : int special_factor = 1;
4036 : 20595 : if (val->same_scc (src->val))
4037 : : special_factor
4038 : 1524 : = opt_for_fn(src->cs->caller->decl,
4039 : : param_ipa_cp_recursive_freq_factor);
4040 : 19071 : else if (val->self_recursion_generated_p ()
4041 : 19071 : && (src->cs->callee->function_symbol ()
4042 : 710 : == src->cs->caller))
4043 : : {
4044 : 710 : int max_recur_gen_depth
4045 : 710 : = opt_for_fn(src->cs->caller->decl,
4046 : : param_ipa_cp_max_recursive_depth);
4047 : 710 : special_factor = max_recur_gen_depth
4048 : 710 : - val->self_recursion_generated_level + 1;
4049 : : }
4050 : :
4051 : 20595 : src->val->prop_time_benefit
4052 : 41190 : += time * special_factor * src->cs->sreal_frequency ();
4053 : : }
4054 : :
4055 : 132368 : if (size < INT_MAX)
4056 : : {
4057 : 132368 : val->prop_time_benefit = time;
4058 : 132368 : val->prop_size_cost = size;
4059 : : }
4060 : : else
4061 : : {
4062 : 0 : val->prop_time_benefit = 0;
4063 : 0 : val->prop_size_cost = 0;
4064 : : }
4065 : : }
4066 : : }
4067 : 253636 : }
4068 : :
4069 : :
4070 : : /* Propagate constants, polymorphic contexts and their effects from the
4071 : : summaries interprocedurally. */
4072 : :
4073 : : static void
4074 : 126818 : ipcp_propagate_stage (class ipa_topo_info *topo)
4075 : : {
4076 : 126818 : struct cgraph_node *node;
4077 : :
4078 : 126818 : if (dump_file)
4079 : 156 : fprintf (dump_file, "\n Propagating constants:\n\n");
4080 : :
4081 : 1469518 : FOR_EACH_DEFINED_FUNCTION (node)
4082 : : {
4083 : 1342700 : if (node->has_gimple_body_p ()
4084 : 1266609 : && opt_for_fn (node->decl, flag_ipa_cp)
4085 : 2599461 : && opt_for_fn (node->decl, optimize))
4086 : : {
4087 : 1256761 : ipa_node_params *info = ipa_node_params_sum->get (node);
4088 : 1256761 : determine_versionability (node, info);
4089 : :
4090 : 1256761 : unsigned nlattices = ipa_get_param_count (info);
4091 : 1256761 : info->lattices.safe_grow_cleared (nlattices, true);
4092 : 1256761 : initialize_node_lattices (node);
4093 : : }
4094 : 1342700 : ipa_size_summary *s = ipa_size_summaries->get (node);
4095 : 1342700 : if (node->definition && !node->alias && s != NULL)
4096 : 1267595 : overall_size += s->self_size;
4097 : : }
4098 : :
4099 : 126818 : orig_overall_size = overall_size;
4100 : :
4101 : 126818 : if (dump_file)
4102 : 156 : fprintf (dump_file, "\noverall_size: %li\n", overall_size);
4103 : :
4104 : 126818 : propagate_constants_topo (topo);
4105 : 126818 : if (flag_checking)
4106 : 126810 : ipcp_verify_propagated_values ();
4107 : 126818 : topo->constants.propagate_effects ();
4108 : 126818 : topo->contexts.propagate_effects ();
4109 : :
4110 : 126818 : if (dump_file)
4111 : : {
4112 : 156 : fprintf (dump_file, "\nIPA lattices after all propagation:\n");
4113 : 156 : print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true);
4114 : : }
4115 : 126818 : }
4116 : :
4117 : : /* Discover newly direct outgoing edges from NODE which is a new clone with
4118 : : known KNOWN_CSTS and make them direct. */
4119 : :
4120 : : static void
4121 : 19849 : ipcp_discover_new_direct_edges (struct cgraph_node *node,
4122 : : vec<tree> known_csts,
4123 : : vec<ipa_polymorphic_call_context>
4124 : : known_contexts,
4125 : : vec<ipa_argagg_value, va_gc> *aggvals)
4126 : : {
4127 : 19849 : struct cgraph_edge *ie, *next_ie;
4128 : 19849 : bool found = false;
4129 : :
4130 : 21666 : for (ie = node->indirect_calls; ie; ie = next_ie)
4131 : : {
4132 : 1817 : tree target;
4133 : 1817 : bool speculative;
4134 : :
4135 : 1817 : next_ie = ie->next_callee;
4136 : 1817 : ipa_argagg_value_list avs (aggvals);
4137 : 1817 : target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts,
4138 : : avs, &speculative);
4139 : 1817 : if (target)
4140 : : {
4141 : 579 : bool agg_contents = ie->indirect_info->agg_contents;
4142 : 579 : bool polymorphic = ie->indirect_info->polymorphic;
4143 : 579 : int param_index = ie->indirect_info->param_index;
4144 : 579 : struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target,
4145 : : speculative);
4146 : 579 : found = true;
4147 : :
4148 : 579 : if (cs && !agg_contents && !polymorphic)
4149 : : {
4150 : 373 : ipa_node_params *info = ipa_node_params_sum->get (node);
4151 : 373 : int c = ipa_get_controlled_uses (info, param_index);
4152 : 373 : if (c != IPA_UNDESCRIBED_USE
4153 : 373 : && !ipa_get_param_load_dereferenced (info, param_index))
4154 : : {
4155 : 369 : struct ipa_ref *to_del;
4156 : :
4157 : 369 : c--;
4158 : 369 : ipa_set_controlled_uses (info, param_index, c);
4159 : 369 : if (dump_file && (dump_flags & TDF_DETAILS))
4160 : 3 : fprintf (dump_file, " controlled uses count of param "
4161 : : "%i bumped down to %i\n", param_index, c);
4162 : 369 : if (c == 0
4163 : 369 : && (to_del = node->find_reference (cs->callee, NULL, 0,
4164 : : IPA_REF_ADDR)))
4165 : : {
4166 : 285 : if (dump_file && (dump_flags & TDF_DETAILS))
4167 : 3 : fprintf (dump_file, " and even removing its "
4168 : : "cloning-created reference\n");
4169 : 285 : to_del->remove_reference ();
4170 : : }
4171 : : }
4172 : : }
4173 : : }
4174 : : }
4175 : : /* Turning calls to direct calls will improve overall summary. */
4176 : 19849 : if (found)
4177 : 481 : ipa_update_overall_fn_summary (node);
4178 : 19849 : }
4179 : :
4180 : : class edge_clone_summary;
4181 : : static call_summary <edge_clone_summary *> *edge_clone_summaries = NULL;
4182 : :
4183 : : /* Edge clone summary. */
4184 : :
4185 : : class edge_clone_summary
4186 : : {
4187 : : public:
4188 : : /* Default constructor. */
4189 : 383936 : edge_clone_summary (): prev_clone (NULL), next_clone (NULL) {}
4190 : :
4191 : : /* Default destructor. */
4192 : 383936 : ~edge_clone_summary ()
4193 : : {
4194 : 383936 : if (prev_clone)
4195 : 34807 : edge_clone_summaries->get (prev_clone)->next_clone = next_clone;
4196 : 383936 : if (next_clone)
4197 : 161291 : edge_clone_summaries->get (next_clone)->prev_clone = prev_clone;
4198 : 383936 : }
4199 : :
4200 : : cgraph_edge *prev_clone;
4201 : : cgraph_edge *next_clone;
4202 : : };
4203 : :
4204 : : class edge_clone_summary_t:
4205 : : public call_summary <edge_clone_summary *>
4206 : : {
4207 : : public:
4208 : 126818 : edge_clone_summary_t (symbol_table *symtab):
4209 : 253636 : call_summary <edge_clone_summary *> (symtab)
4210 : : {
4211 : 126818 : m_initialize_when_cloning = true;
4212 : : }
4213 : :
4214 : : void duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
4215 : : edge_clone_summary *src_data,
4216 : : edge_clone_summary *dst_data) final override;
4217 : : };
4218 : :
4219 : : /* Edge duplication hook. */
4220 : :
4221 : : void
4222 : 194843 : edge_clone_summary_t::duplicate (cgraph_edge *src_edge, cgraph_edge *dst_edge,
4223 : : edge_clone_summary *src_data,
4224 : : edge_clone_summary *dst_data)
4225 : : {
4226 : 194843 : if (src_data->next_clone)
4227 : 5738 : edge_clone_summaries->get (src_data->next_clone)->prev_clone = dst_edge;
4228 : 194843 : dst_data->prev_clone = src_edge;
4229 : 194843 : dst_data->next_clone = src_data->next_clone;
4230 : 194843 : src_data->next_clone = dst_edge;
4231 : 194843 : }
4232 : :
4233 : : /* Return true is CS calls DEST or its clone for all contexts. When
4234 : : ALLOW_RECURSION_TO_CLONE is false, also return false for self-recursive
4235 : : edges from/to an all-context clone. */
4236 : :
4237 : : static bool
4238 : 637726 : calls_same_node_or_its_all_contexts_clone_p (cgraph_edge *cs, cgraph_node *dest,
4239 : : bool allow_recursion_to_clone)
4240 : : {
4241 : 637726 : enum availability availability;
4242 : 637726 : cgraph_node *callee = cs->callee->function_symbol (&availability);
4243 : :
4244 : 637726 : if (availability <= AVAIL_INTERPOSABLE)
4245 : : return false;
4246 : 637584 : if (callee == dest)
4247 : : return true;
4248 : 86275 : if (!allow_recursion_to_clone && cs->caller == callee)
4249 : : return false;
4250 : :
4251 : 86129 : ipa_node_params *info = ipa_node_params_sum->get (callee);
4252 : 86129 : return info->is_all_contexts_clone && info->ipcp_orig_node == dest;
4253 : : }
4254 : :
4255 : : /* Return true if edge CS does bring about the value described by SRC to
4256 : : DEST_VAL of node DEST or its clone for all contexts. */
4257 : :
4258 : : static bool
4259 : 635306 : cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src,
4260 : : cgraph_node *dest, ipcp_value<tree> *dest_val)
4261 : : {
4262 : 635306 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
4263 : :
4264 : 635306 : if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, !src->val)
4265 : 635306 : || caller_info->node_dead)
4266 : : return false;
4267 : :
4268 : 370136 : if (!src->val)
4269 : : return true;
4270 : :
4271 : 18797 : if (caller_info->ipcp_orig_node)
4272 : : {
4273 : 4810 : tree t = NULL_TREE;
4274 : 4810 : if (src->offset == -1)
4275 : 3034 : t = caller_info->known_csts[src->index];
4276 : 1776 : else if (ipcp_transformation *ts
4277 : 1776 : = ipcp_get_transformation_summary (cs->caller))
4278 : : {
4279 : 1776 : ipa_argagg_value_list avl (ts);
4280 : 1776 : t = avl.get_value (src->index, src->offset / BITS_PER_UNIT);
4281 : : }
4282 : 4810 : return (t != NULL_TREE
4283 : 4810 : && values_equal_for_ipcp_p (src->val->value, t));
4284 : : }
4285 : : else
4286 : : {
4287 : 13987 : if (src->val == dest_val)
4288 : : return true;
4289 : :
4290 : 13125 : struct ipcp_agg_lattice *aglat;
4291 : 13125 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
4292 : : src->index);
4293 : 13125 : if (src->offset == -1)
4294 : 9997 : return (plats->itself.is_single_const ()
4295 : 6 : && values_equal_for_ipcp_p (src->val->value,
4296 : 6 : plats->itself.values->value));
4297 : : else
4298 : : {
4299 : 3128 : if (plats->aggs_bottom || plats->aggs_contain_variable)
4300 : : return false;
4301 : 1393 : for (aglat = plats->aggs; aglat; aglat = aglat->next)
4302 : 1393 : if (aglat->offset == src->offset)
4303 : 670 : return (aglat->is_single_const ()
4304 : 4 : && values_equal_for_ipcp_p (src->val->value,
4305 : 4 : aglat->values->value));
4306 : : }
4307 : : return false;
4308 : : }
4309 : : }
4310 : :
4311 : : /* Return true if edge CS does bring about the value described by SRC to
4312 : : DST_VAL of node DEST or its clone for all contexts. */
4313 : :
4314 : : static bool
4315 : 2420 : cgraph_edge_brings_value_p (cgraph_edge *cs,
4316 : : ipcp_value_source<ipa_polymorphic_call_context> *src,
4317 : : cgraph_node *dest,
4318 : : ipcp_value<ipa_polymorphic_call_context> *)
4319 : : {
4320 : 2420 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
4321 : :
4322 : 2420 : if (!calls_same_node_or_its_all_contexts_clone_p (cs, dest, true)
4323 : 2420 : || caller_info->node_dead)
4324 : : return false;
4325 : 2084 : if (!src->val)
4326 : : return true;
4327 : :
4328 : 598 : if (caller_info->ipcp_orig_node)
4329 : 905 : return (caller_info->known_contexts.length () > (unsigned) src->index)
4330 : 206 : && values_equal_for_ipcp_p (src->val->value,
4331 : 103 : caller_info->known_contexts[src->index]);
4332 : :
4333 : 489 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info,
4334 : : src->index);
4335 : 489 : return plats->ctxlat.is_single_const ()
4336 : 47 : && values_equal_for_ipcp_p (src->val->value,
4337 : 47 : plats->ctxlat.values->value);
4338 : : }
4339 : :
4340 : : /* Get the next clone in the linked list of clones of an edge. */
4341 : :
4342 : : static inline struct cgraph_edge *
4343 : 638305 : get_next_cgraph_edge_clone (struct cgraph_edge *cs)
4344 : : {
4345 : 638305 : edge_clone_summary *s = edge_clone_summaries->get (cs);
4346 : 638305 : return s != NULL ? s->next_clone : NULL;
4347 : : }
4348 : :
4349 : : /* Given VAL that is intended for DEST, iterate over all its sources and if any
4350 : : of them is viable and hot, return true. In that case, for those that still
4351 : : hold, add their edge frequency and their number and cumulative profile
4352 : : counts of self-ecursive and other edges into *FREQUENCY, *CALLER_COUNT,
4353 : : REC_COUNT_SUM and NONREC_COUNT_SUM respectively. */
4354 : :
4355 : : template <typename valtype>
4356 : : static bool
4357 : 86933 : get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest,
4358 : : sreal *freq_sum, int *caller_count,
4359 : : profile_count *rec_count_sum,
4360 : : profile_count *nonrec_count_sum,
4361 : : bool *called_without_ipa_profile)
4362 : : {
4363 : : ipcp_value_source<valtype> *src;
4364 : 86933 : sreal freq = 0;
4365 : 86933 : int count = 0;
4366 : 86933 : profile_count rec_cnt = profile_count::zero ();
4367 : 86933 : profile_count nonrec_cnt = profile_count::zero ();
4368 : 86933 : bool interesting = false;
4369 : 86933 : bool non_self_recursive = false;
4370 : 86933 : *called_without_ipa_profile = false;
4371 : :
4372 : 473786 : for (src = val->sources; src; src = src->next)
4373 : : {
4374 : 386853 : struct cgraph_edge *cs = src->cs;
4375 : 981984 : while (cs)
4376 : : {
4377 : 595131 : if (cgraph_edge_brings_value_p (cs, src, dest, val))
4378 : : {
4379 : 347450 : count++;
4380 : 347450 : freq += cs->sreal_frequency ();
4381 : 347450 : interesting |= cs_interesting_for_ipcp_p (cs);
4382 : 347450 : if (cs->caller != dest)
4383 : : {
4384 : 345879 : non_self_recursive = true;
4385 : 345879 : if (cs->count.ipa ().initialized_p ())
4386 : 398 : rec_cnt += cs->count.ipa ();
4387 : : else
4388 : 345481 : *called_without_ipa_profile = true;
4389 : : }
4390 : 1571 : else if (cs->count.ipa ().initialized_p ())
4391 : 0 : nonrec_cnt += cs->count.ipa ();
4392 : : else
4393 : 1571 : *called_without_ipa_profile = true;
4394 : : }
4395 : 595131 : cs = get_next_cgraph_edge_clone (cs);
4396 : : }
4397 : : }
4398 : :
4399 : : /* If the only edges bringing a value are self-recursive ones, do not bother
4400 : : evaluating it. */
4401 : 86933 : if (!non_self_recursive)
4402 : : return false;
4403 : :
4404 : 67496 : *freq_sum = freq;
4405 : 67496 : *caller_count = count;
4406 : 67496 : *rec_count_sum = rec_cnt;
4407 : 67496 : *nonrec_count_sum = nonrec_cnt;
4408 : :
4409 : 67496 : return interesting;
4410 : : }
4411 : :
4412 : : /* Given a NODE, and a set of its CALLERS, try to adjust order of the callers
4413 : : to let a non-self-recursive caller be the first element. Thus, we can
4414 : : simplify intersecting operations on values that arrive from all of these
4415 : : callers, especially when there exists self-recursive call. Return true if
4416 : : this kind of adjustment is possible. */
4417 : :
4418 : : static bool
4419 : 18339 : adjust_callers_for_value_intersection (vec<cgraph_edge *> &callers,
4420 : : cgraph_node *node)
4421 : : {
4422 : 18386 : for (unsigned i = 0; i < callers.length (); i++)
4423 : : {
4424 : 18161 : cgraph_edge *cs = callers[i];
4425 : :
4426 : 18161 : if (cs->caller != node)
4427 : : {
4428 : 18114 : if (i > 0)
4429 : : {
4430 : 37 : callers[i] = callers[0];
4431 : 37 : callers[0] = cs;
4432 : : }
4433 : 18114 : return true;
4434 : : }
4435 : : }
4436 : : return false;
4437 : : }
4438 : :
4439 : : /* Return a vector of incoming edges that do bring value VAL to node DEST. It
4440 : : is assumed their number is known and equal to CALLER_COUNT. */
4441 : :
4442 : : template <typename valtype>
4443 : : static vec<cgraph_edge *>
4444 : 2737 : gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest,
4445 : : int caller_count)
4446 : : {
4447 : : ipcp_value_source<valtype> *src;
4448 : : vec<cgraph_edge *> ret;
4449 : :
4450 : 2737 : ret.create (caller_count);
4451 : 12203 : for (src = val->sources; src; src = src->next)
4452 : : {
4453 : 9466 : struct cgraph_edge *cs = src->cs;
4454 : 26995 : while (cs)
4455 : : {
4456 : 17529 : if (cgraph_edge_brings_value_p (cs, src, dest, val))
4457 : 8798 : ret.quick_push (cs);
4458 : 17529 : cs = get_next_cgraph_edge_clone (cs);
4459 : : }
4460 : : }
4461 : :
4462 : 2737 : if (caller_count > 1)
4463 : 1002 : adjust_callers_for_value_intersection (ret, dest);
4464 : :
4465 : 2737 : return ret;
4466 : : }
4467 : :
4468 : : /* Construct a replacement map for a know VALUE for a formal parameter PARAM.
4469 : : Return it or NULL if for some reason it cannot be created. FORCE_LOAD_REF
4470 : : should be set to true when the reference created for the constant should be
4471 : : a load one and not an address one because the corresponding parameter p is
4472 : : only used as *p. */
4473 : :
4474 : : static struct ipa_replace_map *
4475 : 21153 : get_replacement_map (class ipa_node_params *info, tree value, int parm_num,
4476 : : bool force_load_ref)
4477 : : {
4478 : 21153 : struct ipa_replace_map *replace_map;
4479 : :
4480 : 21153 : replace_map = ggc_alloc<ipa_replace_map> ();
4481 : 21153 : if (dump_file)
4482 : : {
4483 : 171 : fprintf (dump_file, " replacing ");
4484 : 171 : ipa_dump_param (dump_file, info, parm_num);
4485 : :
4486 : 171 : fprintf (dump_file, " with const ");
4487 : 171 : print_generic_expr (dump_file, value);
4488 : :
4489 : 171 : if (force_load_ref)
4490 : 11 : fprintf (dump_file, " - forcing load reference\n");
4491 : : else
4492 : 160 : fprintf (dump_file, "\n");
4493 : : }
4494 : 21153 : replace_map->parm_num = parm_num;
4495 : 21153 : replace_map->new_tree = value;
4496 : 21153 : replace_map->force_load_ref = force_load_ref;
4497 : 21153 : return replace_map;
4498 : : }
4499 : :
4500 : : /* Dump new profiling counts of NODE. SPEC is true when NODE is a specialzied
4501 : : one, otherwise it will be referred to as the original node. */
4502 : :
4503 : : static void
4504 : 4 : dump_profile_updates (cgraph_node *node, bool spec)
4505 : : {
4506 : 4 : if (spec)
4507 : 2 : fprintf (dump_file, " setting count of the specialized node %s to ",
4508 : : node->dump_name ());
4509 : : else
4510 : 2 : fprintf (dump_file, " setting count of the original node %s to ",
4511 : : node->dump_name ());
4512 : :
4513 : 4 : node->count.dump (dump_file);
4514 : 4 : fprintf (dump_file, "\n");
4515 : 6 : for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee)
4516 : : {
4517 : 2 : fprintf (dump_file, " edge to %s has count ",
4518 : 2 : cs->callee->dump_name ());
4519 : 2 : cs->count.dump (dump_file);
4520 : 2 : fprintf (dump_file, "\n");
4521 : : }
4522 : 4 : }
4523 : :
4524 : : /* With partial train run we do not want to assume that original's count is
4525 : : zero whenever we redurect all executed edges to clone. Simply drop profile
4526 : : to local one in this case. In eany case, return the new value. ORIG_NODE
4527 : : is the original node and its count has not been updaed yet. */
4528 : :
4529 : : profile_count
4530 : 20 : lenient_count_portion_handling (profile_count remainder, cgraph_node *orig_node)
4531 : : {
4532 : 40 : if (remainder.ipa_p () && !remainder.ipa ().nonzero_p ()
4533 : 30 : && orig_node->count.ipa_p () && orig_node->count.ipa ().nonzero_p ()
4534 : 5 : && opt_for_fn (orig_node->decl, flag_profile_partial_training))
4535 : 0 : remainder = orig_node->count.guessed_local ();
4536 : :
4537 : 20 : return remainder;
4538 : : }
4539 : :
4540 : : /* Structure to sum counts coming from nodes other than the original node and
4541 : : its clones. */
4542 : :
4543 : : struct gather_other_count_struct
4544 : : {
4545 : : cgraph_node *orig;
4546 : : profile_count other_count;
4547 : : };
4548 : :
4549 : : /* Worker callback of call_for_symbol_thunks_and_aliases summing the number of
4550 : : counts that come from non-self-recursive calls.. */
4551 : :
4552 : : static bool
4553 : 10 : gather_count_of_non_rec_edges (cgraph_node *node, void *data)
4554 : : {
4555 : 10 : gather_other_count_struct *desc = (gather_other_count_struct *) data;
4556 : 24 : for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4557 : 14 : if (cs->caller != desc->orig && cs->caller->clone_of != desc->orig)
4558 : 0 : desc->other_count += cs->count.ipa ();
4559 : 10 : return false;
4560 : : }
4561 : :
4562 : : /* Structure to help analyze if we need to boost counts of some clones of some
4563 : : non-recursive edges to match the new callee count. */
4564 : :
4565 : : struct desc_incoming_count_struct
4566 : : {
4567 : : cgraph_node *orig;
4568 : : hash_set <cgraph_edge *> *processed_edges;
4569 : : profile_count count;
4570 : : unsigned unproc_orig_rec_edges;
4571 : : };
4572 : :
4573 : : /* Go over edges calling NODE and its thunks and gather information about
4574 : : incoming counts so that we know if we need to make any adjustments. */
4575 : :
4576 : : static void
4577 : 10 : analyze_clone_icoming_counts (cgraph_node *node,
4578 : : desc_incoming_count_struct *desc)
4579 : : {
4580 : 24 : for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4581 : 14 : if (cs->caller->thunk)
4582 : : {
4583 : 0 : analyze_clone_icoming_counts (cs->caller, desc);
4584 : 0 : continue;
4585 : : }
4586 : : else
4587 : : {
4588 : 14 : if (cs->count.initialized_p ())
4589 : 14 : desc->count += cs->count.ipa ();
4590 : 14 : if (!desc->processed_edges->contains (cs)
4591 : 14 : && cs->caller->clone_of == desc->orig)
4592 : 4 : desc->unproc_orig_rec_edges++;
4593 : : }
4594 : 10 : }
4595 : :
4596 : : /* If caller edge counts of a clone created for a self-recursive arithmetic
4597 : : jump function must be adjusted because it is coming from a the "seed" clone
4598 : : for the first value and so has been excessively scaled back as if it was not
4599 : : a recursive call, adjust it so that the incoming counts of NODE match its
4600 : : count. NODE is the node or its thunk. */
4601 : :
4602 : : static void
4603 : 0 : adjust_clone_incoming_counts (cgraph_node *node,
4604 : : desc_incoming_count_struct *desc)
4605 : : {
4606 : 0 : for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4607 : 0 : if (cs->caller->thunk)
4608 : : {
4609 : 0 : adjust_clone_incoming_counts (cs->caller, desc);
4610 : 0 : profile_count sum = profile_count::zero ();
4611 : 0 : for (cgraph_edge *e = cs->caller->callers; e; e = e->next_caller)
4612 : 0 : if (e->count.initialized_p ())
4613 : 0 : sum += e->count.ipa ();
4614 : 0 : cs->count = cs->count.combine_with_ipa_count (sum);
4615 : : }
4616 : 0 : else if (!desc->processed_edges->contains (cs)
4617 : 0 : && cs->caller->clone_of == desc->orig
4618 : 0 : && cs->count.compatible_p (desc->count))
4619 : : {
4620 : 0 : cs->count += desc->count;
4621 : 0 : if (dump_file)
4622 : : {
4623 : 0 : fprintf (dump_file, " Adjusted count of an incoming edge of "
4624 : 0 : "a clone %s -> %s to ", cs->caller->dump_name (),
4625 : 0 : cs->callee->dump_name ());
4626 : 0 : cs->count.dump (dump_file);
4627 : 0 : fprintf (dump_file, "\n");
4628 : : }
4629 : : }
4630 : 0 : }
4631 : :
4632 : : /* When ORIG_NODE has been cloned for values which have been generated fora
4633 : : self-recursive call as a result of an arithmetic pass-through
4634 : : jump-functions, adjust its count together with counts of all such clones in
4635 : : SELF_GEN_CLONES which also at this point contains ORIG_NODE itself.
4636 : :
4637 : : The function sums the counts of the original node and all its clones that
4638 : : cannot be attributed to a specific clone because it comes from a
4639 : : non-recursive edge. This sum is then evenly divided between the clones and
4640 : : on top of that each one gets all the counts which can be attributed directly
4641 : : to it. */
4642 : :
4643 : : static void
4644 : 26 : update_counts_for_self_gen_clones (cgraph_node *orig_node,
4645 : : const vec<cgraph_node *> &self_gen_clones)
4646 : : {
4647 : 26 : profile_count redist_sum = orig_node->count.ipa ();
4648 : 26 : if (!redist_sum.nonzero_p ())
4649 : : return;
4650 : :
4651 : 4 : if (dump_file)
4652 : 0 : fprintf (dump_file, " Updating profile of self recursive clone "
4653 : : "series\n");
4654 : :
4655 : 4 : gather_other_count_struct gocs;
4656 : 4 : gocs.orig = orig_node;
4657 : 4 : gocs.other_count = profile_count::zero ();
4658 : :
4659 : 4 : auto_vec <profile_count, 8> other_edges_count;
4660 : 22 : for (cgraph_node *n : self_gen_clones)
4661 : : {
4662 : 10 : gocs.other_count = profile_count::zero ();
4663 : 10 : n->call_for_symbol_thunks_and_aliases (gather_count_of_non_rec_edges,
4664 : : &gocs, false);
4665 : 10 : other_edges_count.safe_push (gocs.other_count);
4666 : 10 : redist_sum -= gocs.other_count;
4667 : : }
4668 : :
4669 : 4 : hash_set<cgraph_edge *> processed_edges;
4670 : 4 : unsigned i = 0;
4671 : 22 : for (cgraph_node *n : self_gen_clones)
4672 : : {
4673 : 10 : profile_count new_count
4674 : 20 : = (redist_sum / self_gen_clones.length () + other_edges_count[i]);
4675 : 10 : new_count = lenient_count_portion_handling (new_count, orig_node);
4676 : 10 : n->scale_profile_to (new_count);
4677 : 20 : for (cgraph_edge *cs = n->callees; cs; cs = cs->next_callee)
4678 : 10 : processed_edges.add (cs);
4679 : :
4680 : 10 : i++;
4681 : : }
4682 : :
4683 : : /* There are still going to be edges to ORIG_NODE that have one or more
4684 : : clones coming from another node clone in SELF_GEN_CLONES and which we
4685 : : scaled by the same amount, which means that the total incoming sum of
4686 : : counts to ORIG_NODE will be too high, scale such edges back. */
4687 : 8 : for (cgraph_edge *cs = orig_node->callees; cs; cs = cs->next_callee)
4688 : : {
4689 : 4 : if (cs->callee->ultimate_alias_target () == orig_node)
4690 : : {
4691 : 4 : unsigned den = 0;
4692 : 22 : for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
4693 : 18 : if (e->callee->ultimate_alias_target () == orig_node
4694 : 18 : && processed_edges.contains (e))
4695 : 8 : den++;
4696 : 4 : if (den > 0)
4697 : 22 : for (cgraph_edge *e = cs; e; e = get_next_cgraph_edge_clone (e))
4698 : 18 : if (e->callee->ultimate_alias_target () == orig_node
4699 : 18 : && processed_edges.contains (e))
4700 : 8 : e->count /= den;
4701 : : }
4702 : : }
4703 : :
4704 : : /* Edges from the seeds of the valus generated for arithmetic jump-functions
4705 : : along self-recursive edges are likely to have fairly low count and so
4706 : : edges from them to nodes in the self_gen_clones do not correspond to the
4707 : : artificially distributed count of the nodes, the total sum of incoming
4708 : : edges to some clones might be too low. Detect this situation and correct
4709 : : it. */
4710 : 22 : for (cgraph_node *n : self_gen_clones)
4711 : : {
4712 : 10 : if (!n->count.ipa ().nonzero_p ())
4713 : 0 : continue;
4714 : :
4715 : 10 : desc_incoming_count_struct desc;
4716 : 10 : desc.orig = orig_node;
4717 : 10 : desc.processed_edges = &processed_edges;
4718 : 10 : desc.count = profile_count::zero ();
4719 : 10 : desc.unproc_orig_rec_edges = 0;
4720 : 10 : analyze_clone_icoming_counts (n, &desc);
4721 : :
4722 : 10 : if (n->count.differs_from_p (desc.count))
4723 : : {
4724 : 0 : if (n->count > desc.count
4725 : 0 : && desc.unproc_orig_rec_edges > 0)
4726 : : {
4727 : 0 : desc.count = n->count - desc.count;
4728 : 0 : desc.count = desc.count /= desc.unproc_orig_rec_edges;
4729 : 0 : adjust_clone_incoming_counts (n, &desc);
4730 : : }
4731 : 0 : else if (dump_file)
4732 : 0 : fprintf (dump_file,
4733 : : " Unable to fix up incoming counts for %s.\n",
4734 : : n->dump_name ());
4735 : : }
4736 : : }
4737 : :
4738 : 4 : if (dump_file)
4739 : 0 : for (cgraph_node *n : self_gen_clones)
4740 : 0 : dump_profile_updates (n, n != orig_node);
4741 : 4 : return;
4742 : 4 : }
4743 : :
4744 : : /* After a specialized NEW_NODE version of ORIG_NODE has been created, update
4745 : : their profile information to reflect this. This function should not be used
4746 : : for clones generated for arithmetic pass-through jump functions on a
4747 : : self-recursive call graph edge, that situation is handled by
4748 : : update_counts_for_self_gen_clones. */
4749 : :
4750 : : static void
4751 : 2567 : update_profiling_info (struct cgraph_node *orig_node,
4752 : : struct cgraph_node *new_node)
4753 : : {
4754 : 2567 : struct caller_statistics stats;
4755 : 2567 : profile_count new_sum;
4756 : 2567 : profile_count remainder, orig_node_count = orig_node->count.ipa ();
4757 : :
4758 : 2567 : if (!orig_node_count.nonzero_p ())
4759 : 2557 : return;
4760 : :
4761 : 10 : if (dump_file)
4762 : : {
4763 : 2 : fprintf (dump_file, " Updating profile from original count: ");
4764 : 2 : orig_node_count.dump (dump_file);
4765 : 2 : fprintf (dump_file, "\n");
4766 : : }
4767 : :
4768 : 10 : init_caller_stats (&stats, new_node);
4769 : 10 : new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats,
4770 : : false);
4771 : 10 : new_sum = stats.count_sum;
4772 : :
4773 : 10 : bool orig_edges_processed = false;
4774 : 10 : if (new_sum > orig_node_count)
4775 : : {
4776 : : /* Profile has alreay gone astray, keep what we have but lower it
4777 : : to global0adjusted or to local if we have partial training. */
4778 : 0 : if (opt_for_fn (orig_node->decl, flag_profile_partial_training))
4779 : 0 : orig_node->make_profile_local ();
4780 : 0 : if (new_sum.quality () == AFDO)
4781 : 0 : orig_node->make_profile_global0 (GUESSED_GLOBAL0_AFDO);
4782 : : else
4783 : 0 : orig_node->make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
4784 : : orig_edges_processed = true;
4785 : : }
4786 : 10 : else if (stats.rec_count_sum.nonzero_p ())
4787 : : {
4788 : 0 : int new_nonrec_calls = stats.n_nonrec_calls;
4789 : : /* There are self-recursive edges which are likely to bring in the
4790 : : majority of calls but which we must divide in between the original and
4791 : : new node. */
4792 : 0 : init_caller_stats (&stats, orig_node);
4793 : 0 : orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats,
4794 : : &stats, false);
4795 : 0 : int orig_nonrec_calls = stats.n_nonrec_calls;
4796 : 0 : profile_count orig_nonrec_call_count = stats.count_sum;
4797 : :
4798 : 0 : if (orig_node->local)
4799 : : {
4800 : 0 : if (!orig_nonrec_call_count.nonzero_p ())
4801 : : {
4802 : 0 : if (dump_file)
4803 : 0 : fprintf (dump_file, " The original is local and the only "
4804 : : "incoming edges from non-dead callers with nonzero "
4805 : : "counts are self-recursive, assuming it is cold.\n");
4806 : : /* The NEW_NODE count and counts of all its outgoing edges
4807 : : are still unmodified copies of ORIG_NODE's. Just clear
4808 : : the latter and bail out. */
4809 : 0 : if (opt_for_fn (orig_node->decl, flag_profile_partial_training))
4810 : 0 : orig_node->make_profile_local ();
4811 : 0 : else if (orig_nonrec_call_count.quality () == AFDO)
4812 : 0 : orig_node->make_profile_global0 (GUESSED_GLOBAL0_AFDO);
4813 : : else
4814 : 0 : orig_node->make_profile_global0 (GUESSED_GLOBAL0_ADJUSTED);
4815 : 0 : return;
4816 : : }
4817 : : }
4818 : : else
4819 : : {
4820 : : /* Let's behave as if there was another caller that accounts for all
4821 : : the calls that were either indirect or from other compilation
4822 : : units. */
4823 : 0 : orig_nonrec_calls++;
4824 : 0 : profile_count pretend_caller_count
4825 : 0 : = (orig_node_count - new_sum - orig_nonrec_call_count
4826 : 0 : - stats.rec_count_sum);
4827 : 0 : orig_nonrec_call_count += pretend_caller_count;
4828 : : }
4829 : :
4830 : : /* Divide all "unexplained" counts roughly proportionally to sums of
4831 : : counts of non-recursive calls.
4832 : :
4833 : : We put rather arbitrary limits on how many counts we claim because the
4834 : : number of non-self-recursive incoming count is only a rough guideline
4835 : : and there are cases (such as mcf) where using it blindly just takes
4836 : : too many. And if lattices are considered in the opposite order we
4837 : : could also take too few. */
4838 : 0 : profile_count unexp = orig_node_count - new_sum - orig_nonrec_call_count;
4839 : :
4840 : 0 : int limit_den = 2 * (orig_nonrec_calls + new_nonrec_calls);
4841 : 0 : profile_count new_part = unexp.apply_scale (limit_den - 1, limit_den);
4842 : 0 : profile_count den = new_sum + orig_nonrec_call_count;
4843 : 0 : if (den.nonzero_p ())
4844 : 0 : new_part = MIN (unexp.apply_scale (new_sum, den), new_part);
4845 : 0 : new_part = MAX (new_part,
4846 : : unexp.apply_scale (new_nonrec_calls, limit_den));
4847 : 0 : if (dump_file)
4848 : : {
4849 : 0 : fprintf (dump_file, " Claiming ");
4850 : 0 : new_part.dump (dump_file);
4851 : 0 : fprintf (dump_file, " of unexplained ");
4852 : 0 : unexp.dump (dump_file);
4853 : 0 : fprintf (dump_file, " counts because of self-recursive "
4854 : : "calls\n");
4855 : : }
4856 : 0 : new_sum += new_part;
4857 : 0 : remainder = lenient_count_portion_handling (orig_node_count - new_sum,
4858 : : orig_node);
4859 : : }
4860 : : else
4861 : 10 : remainder = lenient_count_portion_handling (orig_node_count - new_sum,
4862 : : orig_node);
4863 : :
4864 : 10 : new_node->scale_profile_to (new_sum);
4865 : :
4866 : 10 : if (!orig_edges_processed)
4867 : 10 : orig_node->scale_profile_to (remainder);
4868 : :
4869 : 10 : if (dump_file)
4870 : : {
4871 : 2 : dump_profile_updates (new_node, true);
4872 : 2 : dump_profile_updates (orig_node, false);
4873 : : }
4874 : : }
4875 : :
4876 : : /* Update the respective profile of specialized NEW_NODE and the original
4877 : : ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM
4878 : : have been redirected to the specialized version. */
4879 : :
4880 : : static void
4881 : 0 : update_specialized_profile (struct cgraph_node *new_node,
4882 : : struct cgraph_node *orig_node,
4883 : : profile_count redirected_sum)
4884 : : {
4885 : 0 : if (dump_file)
4886 : : {
4887 : 0 : fprintf (dump_file, " the sum of counts of redirected edges is ");
4888 : 0 : redirected_sum.dump (dump_file);
4889 : 0 : fprintf (dump_file, "\n old ipa count of the original node is ");
4890 : 0 : orig_node->count.dump (dump_file);
4891 : 0 : fprintf (dump_file, "\n");
4892 : : }
4893 : 0 : if (!orig_node->count.ipa ().nonzero_p ()
4894 : 0 : || !redirected_sum.nonzero_p ())
4895 : 0 : return;
4896 : :
4897 : 0 : orig_node->scale_profile_to
4898 : 0 : (lenient_count_portion_handling (orig_node->count.ipa () - redirected_sum,
4899 : : orig_node));
4900 : :
4901 : 0 : new_node->scale_profile_to (new_node->count.ipa () + redirected_sum);
4902 : :
4903 : 0 : if (dump_file)
4904 : : {
4905 : 0 : dump_profile_updates (new_node, true);
4906 : 0 : dump_profile_updates (orig_node, false);
4907 : : }
4908 : : }
4909 : :
4910 : : static void adjust_references_in_caller (cgraph_edge *cs,
4911 : : symtab_node *symbol, int index);
4912 : :
4913 : : /* Simple structure to pass a symbol and index (with same meaning as parameters
4914 : : of adjust_references_in_caller) through a void* parameter of a
4915 : : call_for_symbol_thunks_and_aliases callback. */
4916 : : struct symbol_and_index_together
4917 : : {
4918 : : symtab_node *symbol;
4919 : : int index;
4920 : : };
4921 : :
4922 : : /* Worker callback of call_for_symbol_thunks_and_aliases to recursively call
4923 : : adjust_references_in_caller on edges up in the call-graph, if necessary. */
4924 : : static bool
4925 : 8 : adjust_refs_in_act_callers (struct cgraph_node *node, void *data)
4926 : : {
4927 : 8 : symbol_and_index_together *pack = (symbol_and_index_together *) data;
4928 : 38 : for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller)
4929 : 30 : if (!cs->caller->thunk)
4930 : 30 : adjust_references_in_caller (cs, pack->symbol, pack->index);
4931 : 8 : return false;
4932 : : }
4933 : :
4934 : : /* At INDEX of a function being called by CS there is an ADDR_EXPR of a
4935 : : variable which is only dereferenced and which is represented by SYMBOL. See
4936 : : if we can remove ADDR reference in callers assosiated witht the call. */
4937 : :
4938 : : static void
4939 : 379 : adjust_references_in_caller (cgraph_edge *cs, symtab_node *symbol, int index)
4940 : : {
4941 : 379 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
4942 : 379 : ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, index);
4943 : 379 : if (jfunc->type == IPA_JF_CONST)
4944 : : {
4945 : 363 : ipa_ref *to_del = cs->caller->find_reference (symbol, cs->call_stmt,
4946 : : cs->lto_stmt_uid,
4947 : : IPA_REF_ADDR);
4948 : 363 : if (!to_del)
4949 : 371 : return;
4950 : 363 : to_del->remove_reference ();
4951 : 363 : ipa_zap_jf_refdesc (jfunc);
4952 : 363 : if (dump_file)
4953 : 22 : fprintf (dump_file, " Removed a reference from %s to %s.\n",
4954 : 11 : cs->caller->dump_name (), symbol->dump_name ());
4955 : 363 : return;
4956 : : }
4957 : :
4958 : 16 : if (jfunc->type != IPA_JF_PASS_THROUGH
4959 : 16 : || ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR
4960 : 32 : || ipa_get_jf_pass_through_refdesc_decremented (jfunc))
4961 : : return;
4962 : :
4963 : 16 : int fidx = ipa_get_jf_pass_through_formal_id (jfunc);
4964 : 16 : cgraph_node *caller = cs->caller;
4965 : 16 : ipa_node_params *caller_info = ipa_node_params_sum->get (caller);
4966 : : /* TODO: This consistency check may be too big and not really
4967 : : that useful. Consider removing it. */
4968 : 16 : tree cst;
4969 : 16 : if (caller_info->ipcp_orig_node)
4970 : 15 : cst = caller_info->known_csts[fidx];
4971 : : else
4972 : : {
4973 : 1 : ipcp_lattice<tree> *lat = ipa_get_scalar_lat (caller_info, fidx);
4974 : 1 : gcc_assert (lat->is_single_const ());
4975 : 1 : cst = lat->values->value;
4976 : : }
4977 : 16 : gcc_assert (TREE_CODE (cst) == ADDR_EXPR
4978 : : && (symtab_node::get (get_base_address (TREE_OPERAND (cst, 0)))
4979 : : == symbol));
4980 : :
4981 : 16 : int cuses = ipa_get_controlled_uses (caller_info, fidx);
4982 : 16 : if (cuses == IPA_UNDESCRIBED_USE)
4983 : : return;
4984 : 16 : gcc_assert (cuses > 0);
4985 : 16 : cuses--;
4986 : 16 : ipa_set_controlled_uses (caller_info, fidx, cuses);
4987 : 16 : ipa_set_jf_pass_through_refdesc_decremented (jfunc, true);
4988 : 16 : if (dump_file && (dump_flags & TDF_DETAILS))
4989 : 3 : fprintf (dump_file, " Controlled uses of parameter %i of %s dropped "
4990 : : "to %i.\n", fidx, caller->dump_name (), cuses);
4991 : 16 : if (cuses)
4992 : : return;
4993 : :
4994 : 8 : if (caller_info->ipcp_orig_node)
4995 : : {
4996 : : /* Cloning machinery has created a reference here, we need to either
4997 : : remove it or change it to a read one. */
4998 : 7 : ipa_ref *to_del = caller->find_reference (symbol, NULL, 0, IPA_REF_ADDR);
4999 : 7 : if (to_del)
5000 : : {
5001 : 7 : to_del->remove_reference ();
5002 : 7 : if (dump_file)
5003 : 6 : fprintf (dump_file, " Removed a reference from %s to %s.\n",
5004 : 3 : cs->caller->dump_name (), symbol->dump_name ());
5005 : 7 : if (ipa_get_param_load_dereferenced (caller_info, fidx))
5006 : : {
5007 : 5 : caller->create_reference (symbol, IPA_REF_LOAD, NULL);
5008 : 5 : if (dump_file)
5009 : 2 : fprintf (dump_file,
5010 : : " ...and replaced it with LOAD one.\n");
5011 : : }
5012 : : }
5013 : : }
5014 : :
5015 : 8 : symbol_and_index_together pack;
5016 : 8 : pack.symbol = symbol;
5017 : 8 : pack.index = fidx;
5018 : 8 : if (caller->can_change_signature)
5019 : 8 : caller->call_for_symbol_thunks_and_aliases (adjust_refs_in_act_callers,
5020 : : &pack, true);
5021 : : }
5022 : :
5023 : :
5024 : : /* Return true if we would like to remove a parameter from NODE when cloning it
5025 : : with KNOWN_CSTS scalar constants. */
5026 : :
5027 : : static bool
5028 : 18411 : want_remove_some_param_p (cgraph_node *node, vec<tree> known_csts)
5029 : : {
5030 : 18411 : auto_vec<bool, 16> surviving;
5031 : 18411 : bool filled_vec = false;
5032 : 18411 : ipa_node_params *info = ipa_node_params_sum->get (node);
5033 : 18411 : int i, count = ipa_get_param_count (info);
5034 : :
5035 : 36722 : for (i = 0; i < count; i++)
5036 : : {
5037 : 32190 : if (!known_csts[i] && ipa_is_param_used (info, i))
5038 : 18311 : continue;
5039 : :
5040 : 13879 : if (!filled_vec)
5041 : : {
5042 : 13879 : clone_info *info = clone_info::get (node);
5043 : 13879 : if (!info || !info->param_adjustments)
5044 : : return true;
5045 : 0 : info->param_adjustments->get_surviving_params (&surviving);
5046 : 0 : filled_vec = true;
5047 : : }
5048 : 0 : if (surviving.length() < (unsigned) i && surviving[i])
5049 : : return true;
5050 : : }
5051 : : return false;
5052 : 18411 : }
5053 : :
5054 : : /* Create a specialized version of NODE with known constants in KNOWN_CSTS,
5055 : : known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and
5056 : : redirect all edges in CALLERS to it. */
5057 : :
5058 : : static struct cgraph_node *
5059 : 19849 : create_specialized_node (struct cgraph_node *node,
5060 : : vec<tree> known_csts,
5061 : : vec<ipa_polymorphic_call_context> known_contexts,
5062 : : vec<ipa_argagg_value, va_gc> *aggvals,
5063 : : vec<cgraph_edge *> &callers)
5064 : : {
5065 : 19849 : ipa_node_params *new_info, *info = ipa_node_params_sum->get (node);
5066 : 19849 : vec<ipa_replace_map *, va_gc> *replace_trees = NULL;
5067 : 19849 : vec<ipa_adjusted_param, va_gc> *new_params = NULL;
5068 : 19849 : struct cgraph_node *new_node;
5069 : 19849 : int i, count = ipa_get_param_count (info);
5070 : 19849 : clone_info *cinfo = clone_info::get (node);
5071 : 0 : ipa_param_adjustments *old_adjustments = cinfo
5072 : 19849 : ? cinfo->param_adjustments : NULL;
5073 : 19849 : ipa_param_adjustments *new_adjustments;
5074 : 19849 : gcc_assert (!info->ipcp_orig_node);
5075 : 19849 : gcc_assert (node->can_change_signature
5076 : : || !old_adjustments);
5077 : :
5078 : 18411 : if (old_adjustments)
5079 : : {
5080 : : /* At the moment all IPA optimizations should use the number of
5081 : : parameters of the prevailing decl as the m_always_copy_start.
5082 : : Handling any other value would complicate the code below, so for the
5083 : : time bing let's only assert it is so. */
5084 : 0 : gcc_assert (old_adjustments->m_always_copy_start == count
5085 : : || old_adjustments->m_always_copy_start < 0);
5086 : 0 : int old_adj_count = vec_safe_length (old_adjustments->m_adj_params);
5087 : 0 : for (i = 0; i < old_adj_count; i++)
5088 : : {
5089 : 0 : ipa_adjusted_param *old_adj = &(*old_adjustments->m_adj_params)[i];
5090 : 0 : if (!node->can_change_signature
5091 : 0 : || old_adj->op != IPA_PARAM_OP_COPY
5092 : 0 : || (!known_csts[old_adj->base_index]
5093 : 0 : && ipa_is_param_used (info, old_adj->base_index)))
5094 : : {
5095 : 0 : ipa_adjusted_param new_adj = *old_adj;
5096 : :
5097 : 0 : new_adj.prev_clone_adjustment = true;
5098 : 0 : new_adj.prev_clone_index = i;
5099 : 0 : vec_safe_push (new_params, new_adj);
5100 : : }
5101 : : }
5102 : 0 : bool skip_return = old_adjustments->m_skip_return;
5103 : 0 : new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
5104 : : ipa_param_adjustments (new_params, count,
5105 : 0 : skip_return));
5106 : : }
5107 : 19849 : else if (node->can_change_signature
5108 : 19849 : && want_remove_some_param_p (node, known_csts))
5109 : : {
5110 : 13879 : ipa_adjusted_param adj;
5111 : 13879 : memset (&adj, 0, sizeof (adj));
5112 : 13879 : adj.op = IPA_PARAM_OP_COPY;
5113 : 54769 : for (i = 0; i < count; i++)
5114 : 40890 : if (!known_csts[i] && ipa_is_param_used (info, i))
5115 : : {
5116 : 15553 : adj.base_index = i;
5117 : 15553 : adj.prev_clone_index = i;
5118 : 15553 : vec_safe_push (new_params, adj);
5119 : : }
5120 : 13879 : new_adjustments = (new (ggc_alloc <ipa_param_adjustments> ())
5121 : 13879 : ipa_param_adjustments (new_params, count, false));
5122 : : }
5123 : : else
5124 : : new_adjustments = NULL;
5125 : :
5126 : 19849 : auto_vec<cgraph_edge *, 2> self_recursive_calls;
5127 : 94282 : for (i = callers.length () - 1; i >= 0; i--)
5128 : : {
5129 : 54584 : cgraph_edge *cs = callers[i];
5130 : 54584 : if (cs->caller == node)
5131 : : {
5132 : 272 : self_recursive_calls.safe_push (cs);
5133 : 272 : callers.unordered_remove (i);
5134 : : }
5135 : : }
5136 : 19849 : replace_trees = cinfo ? vec_safe_copy (cinfo->tree_map) : NULL;
5137 : 76363 : for (i = 0; i < count; i++)
5138 : : {
5139 : 56514 : tree t = known_csts[i];
5140 : 56514 : if (!t)
5141 : 35361 : continue;
5142 : :
5143 : 21153 : gcc_checking_assert (TREE_CODE (t) != TREE_BINFO);
5144 : :
5145 : 21153 : bool load_ref = false;
5146 : 21153 : symtab_node *ref_symbol;
5147 : 21153 : if (TREE_CODE (t) == ADDR_EXPR)
5148 : : {
5149 : 6570 : tree base = get_base_address (TREE_OPERAND (t, 0));
5150 : 6570 : if (TREE_CODE (base) == VAR_DECL
5151 : 3075 : && ipa_get_controlled_uses (info, i) == 0
5152 : 1102 : && ipa_get_param_load_dereferenced (info, i)
5153 : 6934 : && (ref_symbol = symtab_node::get (base)))
5154 : : {
5155 : 364 : load_ref = true;
5156 : 364 : if (node->can_change_signature)
5157 : 1315 : for (cgraph_edge *caller : callers)
5158 : 349 : adjust_references_in_caller (caller, ref_symbol, i);
5159 : : }
5160 : : }
5161 : :
5162 : 21153 : ipa_replace_map *replace_map = get_replacement_map (info, t, i, load_ref);
5163 : 21153 : if (replace_map)
5164 : 21153 : vec_safe_push (replace_trees, replace_map);
5165 : : }
5166 : :
5167 : 59547 : unsigned &suffix_counter = clone_num_suffixes->get_or_insert (
5168 : 19849 : IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (
5169 : : node->decl)));
5170 : 19849 : new_node = node->create_virtual_clone (callers, replace_trees,
5171 : : new_adjustments, "constprop",
5172 : : suffix_counter);
5173 : 19849 : suffix_counter++;
5174 : :
5175 : 19849 : bool have_self_recursive_calls = !self_recursive_calls.is_empty ();
5176 : 20121 : for (unsigned j = 0; j < self_recursive_calls.length (); j++)
5177 : : {
5178 : 272 : cgraph_edge *cs = get_next_cgraph_edge_clone (self_recursive_calls[j]);
5179 : : /* Cloned edges can disappear during cloning as speculation can be
5180 : : resolved, check that we have one and that it comes from the last
5181 : : cloning. */
5182 : 272 : if (cs && cs->caller == new_node)
5183 : 271 : cs->redirect_callee_duplicating_thunks (new_node);
5184 : : /* Any future code that would make more than one clone of an outgoing
5185 : : edge would confuse this mechanism, so let's check that does not
5186 : : happen. */
5187 : 271 : gcc_checking_assert (!cs
5188 : : || !get_next_cgraph_edge_clone (cs)
5189 : : || get_next_cgraph_edge_clone (cs)->caller != new_node);
5190 : : }
5191 : 19849 : if (have_self_recursive_calls)
5192 : 184 : new_node->expand_all_artificial_thunks ();
5193 : :
5194 : 19849 : ipa_set_node_agg_value_chain (new_node, aggvals);
5195 : 50918 : for (const ipa_argagg_value &av : aggvals)
5196 : 31069 : new_node->maybe_create_reference (av.value, NULL);
5197 : :
5198 : 19849 : if (dump_file && (dump_flags & TDF_DETAILS))
5199 : : {
5200 : 88 : fprintf (dump_file, " the new node is %s.\n", new_node->dump_name ());
5201 : 88 : if (known_contexts.exists ())
5202 : : {
5203 : 0 : for (i = 0; i < count; i++)
5204 : 0 : if (!known_contexts[i].useless_p ())
5205 : : {
5206 : 0 : fprintf (dump_file, " known ctx %i is ", i);
5207 : 0 : known_contexts[i].dump (dump_file);
5208 : : }
5209 : : }
5210 : 88 : if (aggvals)
5211 : : {
5212 : 47 : fprintf (dump_file, " Aggregate replacements:");
5213 : 47 : ipa_argagg_value_list avs (aggvals);
5214 : 47 : avs.dump (dump_file);
5215 : : }
5216 : : }
5217 : :
5218 : 19849 : new_info = ipa_node_params_sum->get (new_node);
5219 : 19849 : new_info->ipcp_orig_node = node;
5220 : 19849 : new_node->ipcp_clone = true;
5221 : 19849 : new_info->known_csts = known_csts;
5222 : 19849 : new_info->known_contexts = known_contexts;
5223 : :
5224 : 19849 : ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts,
5225 : : aggvals);
5226 : :
5227 : 19849 : return new_node;
5228 : 19849 : }
5229 : :
5230 : : /* Return true if JFUNC, which describes a i-th parameter of call CS, is a
5231 : : pass-through function to itself when the cgraph_node involved is not an
5232 : : IPA-CP clone. When SIMPLE is true, further check if JFUNC is a simple
5233 : : no-operation pass-through. */
5234 : :
5235 : : static bool
5236 : 38392 : self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i,
5237 : : bool simple = true)
5238 : : {
5239 : 38392 : enum availability availability;
5240 : 38392 : if (cs->caller == cs->callee->function_symbol (&availability)
5241 : 109 : && availability > AVAIL_INTERPOSABLE
5242 : 109 : && jfunc->type == IPA_JF_PASS_THROUGH
5243 : 54 : && (!simple || ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
5244 : 54 : && ipa_get_jf_pass_through_formal_id (jfunc) == i
5245 : 54 : && ipa_node_params_sum->get (cs->caller)
5246 : 38446 : && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
5247 : 50 : return true;
5248 : : return false;
5249 : : }
5250 : :
5251 : : /* Return true if JFUNC, which describes a part of an aggregate represented or
5252 : : pointed to by the i-th parameter of call CS, is a pass-through function to
5253 : : itself when the cgraph_node involved is not an IPA-CP clone.. When
5254 : : SIMPLE is true, further check if JFUNC is a simple no-operation
5255 : : pass-through. */
5256 : :
5257 : : static bool
5258 : 19701 : self_recursive_agg_pass_through_p (const cgraph_edge *cs,
5259 : : const ipa_agg_jf_item *jfunc,
5260 : : int i, bool simple = true)
5261 : : {
5262 : 19701 : enum availability availability;
5263 : 19701 : if (cs->caller == cs->callee->function_symbol (&availability)
5264 : 73 : && availability > AVAIL_INTERPOSABLE
5265 : 73 : && jfunc->jftype == IPA_JF_LOAD_AGG
5266 : 5 : && jfunc->offset == jfunc->value.load_agg.offset
5267 : 5 : && (!simple || jfunc->value.pass_through.operation == NOP_EXPR)
5268 : 5 : && jfunc->value.pass_through.formal_id == i
5269 : 3 : && useless_type_conversion_p (jfunc->value.load_agg.type, jfunc->type)
5270 : 3 : && ipa_node_params_sum->get (cs->caller)
5271 : 19704 : && !ipa_node_params_sum->get (cs->caller)->ipcp_orig_node)
5272 : : return true;
5273 : : return false;
5274 : : }
5275 : :
5276 : : /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in
5277 : : KNOWN_CSTS with constants that are also known for all of the CALLERS. */
5278 : :
5279 : : static void
5280 : 19849 : find_more_scalar_values_for_callers_subset (struct cgraph_node *node,
5281 : : vec<tree> &known_csts,
5282 : : const vec<cgraph_edge *> &callers)
5283 : : {
5284 : 19849 : ipa_node_params *info = ipa_node_params_sum->get (node);
5285 : 19849 : int i, count = ipa_get_param_count (info);
5286 : :
5287 : 76363 : for (i = 0; i < count; i++)
5288 : : {
5289 : 56514 : struct cgraph_edge *cs;
5290 : 56514 : tree newval = NULL_TREE;
5291 : 56514 : int j;
5292 : 56514 : bool first = true;
5293 : 56514 : tree type = ipa_get_type (info, i);
5294 : :
5295 : 56514 : if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i])
5296 : 23864 : continue;
5297 : :
5298 : 40091 : FOR_EACH_VEC_ELT (callers, j, cs)
5299 : : {
5300 : 38400 : struct ipa_jump_func *jump_func;
5301 : 38400 : tree t;
5302 : :
5303 : 38400 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5304 : 38400 : if (!args
5305 : 38400 : || i >= ipa_get_cs_argument_count (args)
5306 : 76791 : || (i == 0
5307 : 12569 : && call_passes_through_thunk (cs)))
5308 : : {
5309 : : newval = NULL_TREE;
5310 : : break;
5311 : : }
5312 : 38351 : jump_func = ipa_get_ith_jump_func (args, i);
5313 : :
5314 : : /* Besides simple pass-through jump function, arithmetic jump
5315 : : function could also introduce argument-direct-pass-through for
5316 : : self-feeding recursive call. For example,
5317 : :
5318 : : fn (int i)
5319 : : {
5320 : : fn (i & 1);
5321 : : }
5322 : :
5323 : : Given that i is 0, recursive propagation via (i & 1) also gets
5324 : : 0. */
5325 : 38351 : if (self_recursive_pass_through_p (cs, jump_func, i, false))
5326 : : {
5327 : 13 : gcc_assert (newval);
5328 : 13 : enum tree_code opcode
5329 : 13 : = ipa_get_jf_pass_through_operation (jump_func);
5330 : 13 : tree op_type = (opcode == NOP_EXPR) ? NULL_TREE
5331 : 11 : : ipa_get_jf_pass_through_op_type (jump_func);
5332 : 13 : t = ipa_get_jf_arith_result (opcode, newval,
5333 : : ipa_get_jf_pass_through_operand (jump_func),
5334 : : op_type);
5335 : 13 : t = ipacp_value_safe_for_type (type, t);
5336 : : }
5337 : : else
5338 : 38338 : t = ipa_value_from_jfunc (ipa_node_params_sum->get (cs->caller),
5339 : : jump_func, type);
5340 : 38351 : if (!t
5341 : 9265 : || (newval
5342 : 5499 : && !values_equal_for_ipcp_p (t, newval))
5343 : 45792 : || (!first && !newval))
5344 : : {
5345 : : newval = NULL_TREE;
5346 : : break;
5347 : : }
5348 : : else
5349 : 7441 : newval = t;
5350 : 7441 : first = false;
5351 : : }
5352 : :
5353 : 32650 : if (newval)
5354 : : {
5355 : 1691 : if (dump_file && (dump_flags & TDF_DETAILS))
5356 : : {
5357 : 1 : fprintf (dump_file, " adding an extra known scalar value ");
5358 : 1 : print_ipcp_constant_value (dump_file, newval);
5359 : 1 : fprintf (dump_file, " for ");
5360 : 1 : ipa_dump_param (dump_file, info, i);
5361 : 1 : fprintf (dump_file, "\n");
5362 : : }
5363 : :
5364 : 1691 : known_csts[i] = newval;
5365 : : }
5366 : : }
5367 : 19849 : }
5368 : :
5369 : : /* Given a NODE and a subset of its CALLERS, try to populate plank slots in
5370 : : KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the
5371 : : CALLERS. */
5372 : :
5373 : : static void
5374 : 19849 : find_more_contexts_for_caller_subset (cgraph_node *node,
5375 : : vec<ipa_polymorphic_call_context>
5376 : : *known_contexts,
5377 : : const vec<cgraph_edge *> &callers)
5378 : : {
5379 : 19849 : ipa_node_params *info = ipa_node_params_sum->get (node);
5380 : 19849 : int i, count = ipa_get_param_count (info);
5381 : :
5382 : 76354 : for (i = 0; i < count; i++)
5383 : : {
5384 : 56514 : cgraph_edge *cs;
5385 : :
5386 : 56514 : if (ipa_get_poly_ctx_lat (info, i)->bottom
5387 : 56514 : || (known_contexts->exists ()
5388 : 1413 : && !(*known_contexts)[i].useless_p ()))
5389 : 4530 : continue;
5390 : :
5391 : 51984 : ipa_polymorphic_call_context newval;
5392 : 51984 : bool first = true;
5393 : 51984 : int j;
5394 : :
5395 : 52179 : FOR_EACH_VEC_ELT (callers, j, cs)
5396 : : {
5397 : 52058 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5398 : 52058 : if (!args
5399 : 104116 : || i >= ipa_get_cs_argument_count (args))
5400 : 9 : return;
5401 : 52049 : ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i);
5402 : 52049 : ipa_polymorphic_call_context ctx;
5403 : 52049 : ctx = ipa_context_from_jfunc (ipa_node_params_sum->get (cs->caller),
5404 : : cs, i, jfunc);
5405 : 52049 : if (first)
5406 : : {
5407 : 51975 : newval = ctx;
5408 : 51975 : first = false;
5409 : : }
5410 : : else
5411 : 74 : newval.meet_with (ctx);
5412 : 103976 : if (newval.useless_p ())
5413 : : break;
5414 : : }
5415 : :
5416 : 103950 : if (!newval.useless_p ())
5417 : : {
5418 : 121 : if (dump_file && (dump_flags & TDF_DETAILS))
5419 : : {
5420 : 0 : fprintf (dump_file, " adding an extra known polymorphic "
5421 : : "context ");
5422 : 0 : print_ipcp_constant_value (dump_file, newval);
5423 : 0 : fprintf (dump_file, " for ");
5424 : 0 : ipa_dump_param (dump_file, info, i);
5425 : 0 : fprintf (dump_file, "\n");
5426 : : }
5427 : :
5428 : 121 : if (!known_contexts->exists ())
5429 : 212 : known_contexts->safe_grow_cleared (ipa_get_param_count (info),
5430 : : true);
5431 : 121 : (*known_contexts)[i] = newval;
5432 : : }
5433 : :
5434 : : }
5435 : : }
5436 : :
5437 : : /* Push all aggregate values coming along edge CS for parameter number INDEX to
5438 : : RES. If INTERIM is non-NULL, it contains the current interim state of
5439 : : collected aggregate values which can be used to compute values passed over
5440 : : self-recursive edges.
5441 : :
5442 : : This basically one iteration of push_agg_values_from_edge over one
5443 : : parameter, which allows for simpler early returns. */
5444 : :
5445 : : static void
5446 : 50026 : push_agg_values_for_index_from_edge (struct cgraph_edge *cs, int index,
5447 : : vec<ipa_argagg_value> *res,
5448 : : const ipa_argagg_value_list *interim)
5449 : : {
5450 : 50026 : bool agg_values_from_caller = false;
5451 : 50026 : bool agg_jf_preserved = false;
5452 : 50026 : unsigned unit_delta = UINT_MAX;
5453 : 50026 : int src_idx = -1;
5454 : 50026 : ipa_jump_func *jfunc = ipa_get_ith_jump_func (ipa_edge_args_sum->get (cs),
5455 : : index);
5456 : :
5457 : 50026 : if (jfunc->type == IPA_JF_PASS_THROUGH
5458 : 50026 : && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR)
5459 : : {
5460 : 5396 : agg_values_from_caller = true;
5461 : 5396 : agg_jf_preserved = ipa_get_jf_pass_through_agg_preserved (jfunc);
5462 : 5396 : src_idx = ipa_get_jf_pass_through_formal_id (jfunc);
5463 : 5396 : unit_delta = 0;
5464 : : }
5465 : 44630 : else if (jfunc->type == IPA_JF_ANCESTOR
5466 : 44630 : && ipa_get_jf_ancestor_agg_preserved (jfunc))
5467 : : {
5468 : 34 : agg_values_from_caller = true;
5469 : 34 : agg_jf_preserved = true;
5470 : 34 : src_idx = ipa_get_jf_ancestor_formal_id (jfunc);
5471 : 34 : unit_delta = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT;
5472 : : }
5473 : :
5474 : 50026 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
5475 : 50026 : if (agg_values_from_caller)
5476 : : {
5477 : 5430 : if (caller_info->ipcp_orig_node)
5478 : : {
5479 : 3157 : struct cgraph_node *orig_node = caller_info->ipcp_orig_node;
5480 : 3157 : ipcp_transformation *ts
5481 : 3157 : = ipcp_get_transformation_summary (cs->caller);
5482 : 3157 : ipa_node_params *orig_info = ipa_node_params_sum->get (orig_node);
5483 : 3157 : ipcp_param_lattices *orig_plats
5484 : 3157 : = ipa_get_parm_lattices (orig_info, src_idx);
5485 : 3157 : if (ts
5486 : 3157 : && orig_plats->aggs
5487 : 776 : && (agg_jf_preserved || !orig_plats->aggs_by_ref))
5488 : : {
5489 : 690 : ipa_argagg_value_list src (ts);
5490 : 690 : src.push_adjusted_values (src_idx, index, unit_delta, res);
5491 : 690 : return;
5492 : : }
5493 : : }
5494 : : else
5495 : : {
5496 : 2273 : ipcp_param_lattices *src_plats
5497 : 2273 : = ipa_get_parm_lattices (caller_info, src_idx);
5498 : 2273 : if (src_plats->aggs
5499 : 266 : && !src_plats->aggs_bottom
5500 : 266 : && (agg_jf_preserved || !src_plats->aggs_by_ref))
5501 : : {
5502 : 252 : if (interim && self_recursive_pass_through_p (cs, jfunc, index))
5503 : : {
5504 : 37 : interim->push_adjusted_values (src_idx, index, unit_delta,
5505 : : res);
5506 : 37 : return;
5507 : : }
5508 : 215 : if (!src_plats->aggs_contain_variable)
5509 : : {
5510 : 34 : push_agg_values_from_plats (src_plats, index, unit_delta,
5511 : : res);
5512 : 34 : return;
5513 : : }
5514 : : }
5515 : : }
5516 : : }
5517 : :
5518 : 49265 : if (!jfunc->agg.items)
5519 : : return;
5520 : 14208 : bool first = true;
5521 : 14208 : unsigned prev_unit_offset = 0;
5522 : 67548 : for (const ipa_agg_jf_item &agg_jf : *jfunc->agg.items)
5523 : : {
5524 : 53340 : tree value, srcvalue;
5525 : : /* Besides simple pass-through aggregate jump function, arithmetic
5526 : : aggregate jump function could also bring same aggregate value as
5527 : : parameter passed-in for self-feeding recursive call. For example,
5528 : :
5529 : : fn (int *i)
5530 : : {
5531 : : int j = *i & 1;
5532 : : fn (&j);
5533 : : }
5534 : :
5535 : : Given that *i is 0, recursive propagation via (*i & 1) also gets 0. */
5536 : 53340 : if (interim
5537 : 19701 : && self_recursive_agg_pass_through_p (cs, &agg_jf, index, false)
5538 : 53343 : && (srcvalue = interim->get_value(index,
5539 : 3 : agg_jf.offset / BITS_PER_UNIT)))
5540 : : {
5541 : 2 : value = ipa_get_jf_arith_result (agg_jf.value.pass_through.operation,
5542 : : srcvalue,
5543 : 1 : agg_jf.value.pass_through.operand,
5544 : 1 : agg_jf.value.pass_through.op_type);
5545 : 1 : value = ipacp_value_safe_for_type (agg_jf.type, value);
5546 : : }
5547 : : else
5548 : 53339 : value = ipa_agg_value_from_jfunc (caller_info, cs->caller,
5549 : : &agg_jf);
5550 : 53340 : if (value)
5551 : : {
5552 : 51874 : struct ipa_argagg_value iav;
5553 : 51874 : iav.value = value;
5554 : 51874 : iav.unit_offset = agg_jf.offset / BITS_PER_UNIT;
5555 : 51874 : iav.index = index;
5556 : 51874 : iav.by_ref = jfunc->agg.by_ref;
5557 : 51874 : iav.killed = false;
5558 : :
5559 : 51874 : gcc_assert (first
5560 : : || iav.unit_offset > prev_unit_offset);
5561 : 51874 : prev_unit_offset = iav.unit_offset;
5562 : 51874 : first = false;
5563 : :
5564 : 51874 : res->safe_push (iav);
5565 : : }
5566 : : }
5567 : : return;
5568 : : }
5569 : :
5570 : : /* Push all aggregate values coming along edge CS to RES. DEST_INFO is the
5571 : : description of ultimate callee of CS or the one it was cloned from (the
5572 : : summary where lattices are). If INTERIM is non-NULL, it contains the
5573 : : current interim state of collected aggregate values which can be used to
5574 : : compute values passed over self-recursive edges (if OPTIMIZE_SELF_RECURSION
5575 : : is true) and to skip values which clearly will not be part of intersection
5576 : : with INTERIM. */
5577 : :
5578 : : static void
5579 : 23694 : push_agg_values_from_edge (struct cgraph_edge *cs,
5580 : : ipa_node_params *dest_info,
5581 : : vec<ipa_argagg_value> *res,
5582 : : const ipa_argagg_value_list *interim,
5583 : : bool optimize_self_recursion)
5584 : : {
5585 : 23694 : ipa_edge_args *args = ipa_edge_args_sum->get (cs);
5586 : 23694 : if (!args)
5587 : : return;
5588 : :
5589 : 47388 : int count = MIN (ipa_get_param_count (dest_info),
5590 : : ipa_get_cs_argument_count (args));
5591 : :
5592 : 23694 : unsigned interim_index = 0;
5593 : 91455 : for (int index = 0; index < count; index++)
5594 : : {
5595 : 67761 : if (interim)
5596 : : {
5597 : 20892 : while (interim_index < interim->m_elts.size ()
5598 : 18941 : && interim->m_elts[interim_index].value
5599 : 36808 : && interim->m_elts[interim_index].index < index)
5600 : 9636 : interim_index++;
5601 : 15927 : if (interim_index >= interim->m_elts.size ()
5602 : 11256 : || interim->m_elts[interim_index].index > index)
5603 : 4671 : continue;
5604 : : }
5605 : :
5606 : 63090 : ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, index);
5607 : 63090 : if (!ipa_is_param_used (dest_info, index)
5608 : 63090 : || plats->aggs_bottom)
5609 : 13064 : continue;
5610 : 50041 : push_agg_values_for_index_from_edge (cs, index, res,
5611 : : optimize_self_recursion ? interim
5612 : : : NULL);
5613 : : }
5614 : : }
5615 : :
5616 : :
5617 : : /* Look at edges in CALLERS and collect all known aggregate values that arrive
5618 : : from all of them. Return nullptr if there are none. */
5619 : :
5620 : : static struct vec<ipa_argagg_value, va_gc> *
5621 : 19849 : find_aggregate_values_for_callers_subset (struct cgraph_node *node,
5622 : : const vec<cgraph_edge *> &callers)
5623 : : {
5624 : 19849 : ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5625 : 19849 : if (dest_info->ipcp_orig_node)
5626 : 0 : dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
5627 : :
5628 : : /* gather_edges_for_value puts a non-recursive call into the first element of
5629 : : callers if it can. */
5630 : 19849 : auto_vec<ipa_argagg_value, 32> interim;
5631 : 19849 : push_agg_values_from_edge (callers[0], dest_info, &interim, NULL, true);
5632 : :
5633 : 32926 : unsigned valid_entries = interim.length ();
5634 : 19849 : if (!valid_entries)
5635 : : return nullptr;
5636 : :
5637 : 6903 : unsigned caller_count = callers.length();
5638 : 10602 : for (unsigned i = 1; i < caller_count; i++)
5639 : : {
5640 : 3830 : auto_vec<ipa_argagg_value, 32> last;
5641 : 3830 : ipa_argagg_value_list avs (&interim);
5642 : 3830 : push_agg_values_from_edge (callers[i], dest_info, &last, &avs, true);
5643 : :
5644 : 3830 : valid_entries = intersect_argaggs_with (interim, last);
5645 : 3830 : if (!valid_entries)
5646 : 131 : return nullptr;
5647 : 3830 : }
5648 : :
5649 : 6772 : vec<ipa_argagg_value, va_gc> *res = NULL;
5650 : 6772 : vec_safe_reserve_exact (res, valid_entries);
5651 : 53147 : for (const ipa_argagg_value &av : interim)
5652 : 32831 : if (av.value)
5653 : 31069 : res->quick_push(av);
5654 : 6772 : gcc_checking_assert (res->length () == valid_entries);
5655 : : return res;
5656 : 19849 : }
5657 : :
5658 : : /* Determine whether CS also brings all scalar values that the NODE is
5659 : : specialized for. */
5660 : :
5661 : : static bool
5662 : 41 : cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs,
5663 : : struct cgraph_node *node)
5664 : : {
5665 : 41 : ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5666 : 41 : int count = ipa_get_param_count (dest_info);
5667 : 41 : class ipa_node_params *caller_info;
5668 : 41 : class ipa_edge_args *args;
5669 : 41 : int i;
5670 : :
5671 : 41 : caller_info = ipa_node_params_sum->get (cs->caller);
5672 : 41 : args = ipa_edge_args_sum->get (cs);
5673 : 123 : for (i = 0; i < count; i++)
5674 : : {
5675 : 94 : struct ipa_jump_func *jump_func;
5676 : 94 : tree val, t;
5677 : :
5678 : 94 : val = dest_info->known_csts[i];
5679 : 94 : if (!val)
5680 : 49 : continue;
5681 : :
5682 : 90 : if (i >= ipa_get_cs_argument_count (args))
5683 : : return false;
5684 : 45 : jump_func = ipa_get_ith_jump_func (args, i);
5685 : 45 : t = ipa_value_from_jfunc (caller_info, jump_func,
5686 : : ipa_get_type (dest_info, i));
5687 : 45 : if (!t || !values_equal_for_ipcp_p (val, t))
5688 : 12 : return false;
5689 : : }
5690 : : return true;
5691 : : }
5692 : :
5693 : : /* Determine whether CS also brings all aggregate values that NODE is
5694 : : specialized for. */
5695 : :
5696 : : static bool
5697 : 29 : cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs,
5698 : : struct cgraph_node *node)
5699 : : {
5700 : 29 : ipcp_transformation *ts = ipcp_get_transformation_summary (node);
5701 : 29 : if (!ts || vec_safe_is_empty (ts->m_agg_values))
5702 : : return true;
5703 : :
5704 : 15 : const ipa_argagg_value_list existing (ts->m_agg_values);
5705 : 15 : auto_vec<ipa_argagg_value, 32> edge_values;
5706 : 15 : ipa_node_params *dest_info = ipa_node_params_sum->get (node);
5707 : 15 : gcc_checking_assert (dest_info->ipcp_orig_node);
5708 : 15 : dest_info = ipa_node_params_sum->get (dest_info->ipcp_orig_node);
5709 : 15 : push_agg_values_from_edge (cs, dest_info, &edge_values, &existing, false);
5710 : 15 : const ipa_argagg_value_list avl (&edge_values);
5711 : 15 : return avl.superset_of_p (existing);
5712 : 15 : }
5713 : :
5714 : : /* Given an original NODE and a VAL for which we have already created a
5715 : : specialized clone, look whether there are incoming edges that still lead
5716 : : into the old node but now also bring the requested value and also conform to
5717 : : all other criteria such that they can be redirected the special node.
5718 : : This function can therefore redirect the final edge in a SCC. */
5719 : :
5720 : : template <typename valtype>
5721 : : static void
5722 : 2748 : perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val)
5723 : : {
5724 : : ipcp_value_source<valtype> *src;
5725 : 2748 : profile_count redirected_sum = profile_count::zero ();
5726 : :
5727 : 12227 : for (src = val->sources; src; src = src->next)
5728 : : {
5729 : 9479 : struct cgraph_edge *cs = src->cs;
5730 : 34545 : while (cs)
5731 : : {
5732 : 25066 : if (cgraph_edge_brings_value_p (cs, src, node, val)
5733 : 41 : && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node)
5734 : 25095 : && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node))
5735 : : {
5736 : 19 : if (dump_file)
5737 : 4 : fprintf (dump_file, " - adding an extra caller %s of %s\n",
5738 : 4 : cs->caller->dump_name (),
5739 : 4 : val->spec_node->dump_name ());
5740 : :
5741 : 19 : cs->redirect_callee_duplicating_thunks (val->spec_node);
5742 : 19 : val->spec_node->expand_all_artificial_thunks ();
5743 : 19 : if (cs->count.ipa ().initialized_p ())
5744 : 0 : redirected_sum = redirected_sum + cs->count.ipa ();
5745 : : }
5746 : 25066 : cs = get_next_cgraph_edge_clone (cs);
5747 : : }
5748 : : }
5749 : :
5750 : 2748 : if (redirected_sum.nonzero_p ())
5751 : 0 : update_specialized_profile (val->spec_node, node, redirected_sum);
5752 : 2748 : }
5753 : :
5754 : : /* Return true if KNOWN_CONTEXTS contain at least one useful context. */
5755 : :
5756 : : static bool
5757 : 36912 : known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts)
5758 : : {
5759 : 36912 : ipa_polymorphic_call_context *ctx;
5760 : 36912 : int i;
5761 : :
5762 : 92393 : FOR_EACH_VEC_ELT (known_contexts, i, ctx)
5763 : 111934 : if (!ctx->useless_p ())
5764 : : return true;
5765 : : return false;
5766 : : }
5767 : :
5768 : : /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */
5769 : :
5770 : : static vec<ipa_polymorphic_call_context>
5771 : 19800 : copy_useful_known_contexts (const vec<ipa_polymorphic_call_context> &known_contexts)
5772 : : {
5773 : 19800 : if (known_contexts_useful_p (known_contexts))
5774 : 464 : return known_contexts.copy ();
5775 : : else
5776 : 19336 : return vNULL;
5777 : : }
5778 : :
5779 : : /* Copy known scalar values from AVALS into KNOWN_CSTS and modify the copy
5780 : : according to VAL and INDEX. If non-empty, replace KNOWN_CONTEXTS with its
5781 : : copy too. */
5782 : :
5783 : : static void
5784 : 1567 : copy_known_vectors_add_val (ipa_auto_call_arg_values *avals,
5785 : : vec<tree> *known_csts,
5786 : : vec<ipa_polymorphic_call_context> *known_contexts,
5787 : : ipcp_value<tree> *val, int index)
5788 : : {
5789 : 1567 : *known_csts = avals->m_known_vals.copy ();
5790 : 1567 : *known_contexts = copy_useful_known_contexts (avals->m_known_contexts);
5791 : 1567 : (*known_csts)[index] = val->value;
5792 : 1567 : }
5793 : :
5794 : : /* Copy known scalar values from AVALS into KNOWN_CSTS. Similarly, copy
5795 : : contexts to KNOWN_CONTEXTS and modify the copy according to VAL and
5796 : : INDEX. */
5797 : :
5798 : : static void
5799 : 49 : copy_known_vectors_add_val (ipa_auto_call_arg_values *avals,
5800 : : vec<tree> *known_csts,
5801 : : vec<ipa_polymorphic_call_context> *known_contexts,
5802 : : ipcp_value<ipa_polymorphic_call_context> *val,
5803 : : int index)
5804 : : {
5805 : 49 : *known_csts = avals->m_known_vals.copy ();
5806 : 49 : *known_contexts = avals->m_known_contexts.copy ();
5807 : 49 : (*known_contexts)[index] = val->value;
5808 : 49 : }
5809 : :
5810 : : /* Return true if OFFSET indicates this was not an aggregate value or there is
5811 : : a replacement equivalent to VALUE, INDEX and OFFSET among those in the
5812 : : AGGVALS list. */
5813 : :
5814 : : DEBUG_FUNCTION bool
5815 : 2688 : ipcp_val_agg_replacement_ok_p (vec<ipa_argagg_value, va_gc> *aggvals,
5816 : : int index, HOST_WIDE_INT offset, tree value)
5817 : : {
5818 : 2688 : if (offset == -1)
5819 : : return true;
5820 : :
5821 : 1121 : const ipa_argagg_value_list avl (aggvals);
5822 : 1121 : tree v = avl.get_value (index, offset / BITS_PER_UNIT);
5823 : 1121 : return v && values_equal_for_ipcp_p (v, value);
5824 : : }
5825 : :
5826 : : /* Return true if offset is minus one because source of a polymorphic context
5827 : : cannot be an aggregate value. */
5828 : :
5829 : : DEBUG_FUNCTION bool
5830 : 49 : ipcp_val_agg_replacement_ok_p (vec<ipa_argagg_value, va_gc> *,
5831 : : int , HOST_WIDE_INT offset,
5832 : : ipa_polymorphic_call_context)
5833 : : {
5834 : 49 : return offset == -1;
5835 : : }
5836 : :
5837 : : /* Decide whether to create a special version of NODE for value VAL of
5838 : : parameter at the given INDEX. If OFFSET is -1, the value is for the
5839 : : parameter itself, otherwise it is stored at the given OFFSET of the
5840 : : parameter. AVALS describes the other already known values. SELF_GEN_CLONES
5841 : : is a vector which contains clones created for self-recursive calls with an
5842 : : arithmetic pass-through jump function. */
5843 : :
5844 : : template <typename valtype>
5845 : : static bool
5846 : 89681 : decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset,
5847 : : ipcp_value<valtype> *val, ipa_auto_call_arg_values *avals,
5848 : : vec<cgraph_node *> *self_gen_clones)
5849 : : {
5850 : : int caller_count;
5851 : 89681 : sreal freq_sum;
5852 : : profile_count count_sum, rec_count_sum;
5853 : : vec<cgraph_edge *> callers;
5854 : : bool called_without_ipa_profile;
5855 : :
5856 : 89681 : if (val->spec_node)
5857 : : {
5858 : 2748 : perhaps_add_new_callers (node, val);
5859 : 2748 : return false;
5860 : : }
5861 : 86933 : else if (val->local_size_cost + overall_size > get_max_overall_size (node))
5862 : : {
5863 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5864 : 0 : fprintf (dump_file, " Ignoring candidate value because "
5865 : : "maximum unit size would be reached with %li.\n",
5866 : : val->local_size_cost + overall_size);
5867 : 0 : return false;
5868 : : }
5869 : 86933 : else if (!get_info_about_necessary_edges (val, node, &freq_sum, &caller_count,
5870 : : &rec_count_sum, &count_sum,
5871 : : &called_without_ipa_profile))
5872 : : return false;
5873 : :
5874 : 66450 : if (!dbg_cnt (ipa_cp_values))
5875 : : return false;
5876 : :
5877 : 66450 : if (val->self_recursion_generated_p ())
5878 : : {
5879 : : /* The edge counts in this case might not have been adjusted yet.
5880 : : Nevertleless, even if they were it would be only a guesswork which we
5881 : : can do now. The recursive part of the counts can be derived from the
5882 : : count of the original node anyway. */
5883 : 227 : if (node->count.ipa ().nonzero_p ())
5884 : : {
5885 : 12 : unsigned dem = self_gen_clones->length () + 1;
5886 : 12 : rec_count_sum = node->count.ipa () / dem;
5887 : : }
5888 : : else
5889 : 203 : rec_count_sum = profile_count::zero ();
5890 : : }
5891 : :
5892 : : /* get_info_about_necessary_edges only sums up ipa counts. */
5893 : 66450 : count_sum += rec_count_sum;
5894 : :
5895 : 66450 : if (dump_file && (dump_flags & TDF_DETAILS))
5896 : : {
5897 : 101 : fprintf (dump_file, " - considering value ");
5898 : 101 : print_ipcp_constant_value (dump_file, val->value);
5899 : 101 : fprintf (dump_file, " for ");
5900 : 101 : ipa_dump_param (dump_file, ipa_node_params_sum->get (node), index);
5901 : 101 : if (offset != -1)
5902 : 57 : fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset);
5903 : 101 : fprintf (dump_file, " (caller_count: %i)\n", caller_count);
5904 : : }
5905 : :
5906 : 66450 : if (!good_cloning_opportunity_p (node, val->local_time_benefit,
5907 : : freq_sum, count_sum,
5908 : : val->local_size_cost,
5909 : : called_without_ipa_profile)
5910 : 66450 : && !good_cloning_opportunity_p (node, val->prop_time_benefit,
5911 : : freq_sum, count_sum, val->prop_size_cost,
5912 : : called_without_ipa_profile))
5913 : : return false;
5914 : :
5915 : 2737 : if (dump_file)
5916 : 137 : fprintf (dump_file, " Creating a specialized node of %s.\n",
5917 : : node->dump_name ());
5918 : :
5919 : : vec<tree> known_csts;
5920 : : vec<ipa_polymorphic_call_context> known_contexts;
5921 : :
5922 : 2737 : callers = gather_edges_for_value (val, node, caller_count);
5923 : 2737 : if (offset == -1)
5924 : 1616 : copy_known_vectors_add_val (avals, &known_csts, &known_contexts, val, index);
5925 : : else
5926 : : {
5927 : 1121 : known_csts = avals->m_known_vals.copy ();
5928 : 1121 : known_contexts = copy_useful_known_contexts (avals->m_known_contexts);
5929 : : }
5930 : 2737 : find_more_scalar_values_for_callers_subset (node, known_csts, callers);
5931 : 2737 : find_more_contexts_for_caller_subset (node, &known_contexts, callers);
5932 : : vec<ipa_argagg_value, va_gc> *aggvals
5933 : 2737 : = find_aggregate_values_for_callers_subset (node, callers);
5934 : 2737 : gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index,
5935 : : offset, val->value));
5936 : 2737 : val->spec_node = create_specialized_node (node, known_csts, known_contexts,
5937 : : aggvals, callers);
5938 : :
5939 : 2737 : if (val->self_recursion_generated_p ())
5940 : 170 : self_gen_clones->safe_push (val->spec_node);
5941 : : else
5942 : 2567 : update_profiling_info (node, val->spec_node);
5943 : :
5944 : 2737 : callers.release ();
5945 : 2737 : overall_size += val->local_size_cost;
5946 : 2737 : if (dump_file && (dump_flags & TDF_DETAILS))
5947 : 65 : fprintf (dump_file, " overall size reached %li\n",
5948 : : overall_size);
5949 : :
5950 : : /* TODO: If for some lattice there is only one other known value
5951 : : left, make a special node for it too. */
5952 : :
5953 : : return true;
5954 : : }
5955 : :
5956 : : /* Like irange::contains_p(), but convert VAL to the range of R if
5957 : : necessary. */
5958 : :
5959 : : static inline bool
5960 : 15751 : ipa_range_contains_p (const vrange &r, tree val)
5961 : : {
5962 : 15751 : if (r.undefined_p ())
5963 : : return false;
5964 : :
5965 : 15751 : tree type = r.type ();
5966 : 15751 : if (!wi::fits_to_tree_p (wi::to_wide (val), type))
5967 : : return false;
5968 : :
5969 : 15751 : val = fold_convert (type, val);
5970 : 15751 : return r.contains_p (val);
5971 : : }
5972 : :
5973 : : /* Decide whether and what specialized clones of NODE should be created. */
5974 : :
5975 : : static bool
5976 : 1078298 : decide_whether_version_node (struct cgraph_node *node)
5977 : : {
5978 : 1078298 : ipa_node_params *info = ipa_node_params_sum->get (node);
5979 : 1078298 : int i, count = ipa_get_param_count (info);
5980 : 1964373 : bool ret = false;
5981 : :
5982 : 886075 : if (count == 0)
5983 : : return false;
5984 : :
5985 : 886075 : if (dump_file && (dump_flags & TDF_DETAILS))
5986 : 161 : fprintf (dump_file, "\nEvaluating opportunities for %s.\n",
5987 : : node->dump_name ());
5988 : :
5989 : 886075 : auto_vec <cgraph_node *, 9> self_gen_clones;
5990 : 886075 : ipa_auto_call_arg_values avals;
5991 : 886075 : gather_context_independent_values (info, &avals, false, NULL);
5992 : :
5993 : 2962857 : for (i = 0; i < count;i++)
5994 : : {
5995 : 2076782 : if (!ipa_is_param_used (info, i))
5996 : 233148 : continue;
5997 : :
5998 : 1843634 : class ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
5999 : 1843634 : ipcp_lattice<tree> *lat = &plats->itself;
6000 : 1843634 : ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat;
6001 : :
6002 : 1843634 : if (!lat->bottom
6003 : 1843634 : && !avals.m_known_vals[i])
6004 : : {
6005 : 201877 : ipcp_value<tree> *val;
6006 : 248769 : for (val = lat->values; val; val = val->next)
6007 : : {
6008 : : /* If some values generated for self-recursive calls with
6009 : : arithmetic jump functions fall outside of the known
6010 : : range for the parameter, we can skip them. */
6011 : 46919 : if (TREE_CODE (val->value) == INTEGER_CST
6012 : 27552 : && !plats->m_value_range.bottom_p ()
6013 : 62643 : && !ipa_range_contains_p (plats->m_value_range.m_vr,
6014 : : val->value))
6015 : : {
6016 : : /* This can happen also if a constant present in the source
6017 : : code falls outside of the range of parameter's type, so we
6018 : : cannot assert. */
6019 : 27 : if (dump_file && (dump_flags & TDF_DETAILS))
6020 : : {
6021 : 0 : fprintf (dump_file, " - skipping%s value ",
6022 : 0 : val->self_recursion_generated_p ()
6023 : : ? " self_recursion_generated" : "");
6024 : 0 : print_ipcp_constant_value (dump_file, val->value);
6025 : 0 : fprintf (dump_file, " because it is outside known "
6026 : : "value range.\n");
6027 : : }
6028 : 27 : continue;
6029 : : }
6030 : 46865 : ret |= decide_about_value (node, i, -1, val, &avals,
6031 : : &self_gen_clones);
6032 : : }
6033 : : }
6034 : :
6035 : 1843634 : if (!plats->aggs_bottom)
6036 : : {
6037 : 231620 : struct ipcp_agg_lattice *aglat;
6038 : 231620 : ipcp_value<tree> *val;
6039 : 316732 : for (aglat = plats->aggs; aglat; aglat = aglat->next)
6040 : 84683 : if (!aglat->bottom && aglat->values
6041 : : /* If the following is false, the one value has been considered
6042 : : for cloning for all contexts. */
6043 : 160904 : && (plats->aggs_contain_variable
6044 : 137508 : || !aglat->is_single_const ()))
6045 : 71297 : for (val = aglat->values; val; val = val->next)
6046 : 41307 : ret |= decide_about_value (node, i, aglat->offset, val, &avals,
6047 : : &self_gen_clones);
6048 : : }
6049 : :
6050 : 1843634 : if (!ctxlat->bottom
6051 : 2303744 : && avals.m_known_contexts[i].useless_p ())
6052 : : {
6053 : 225531 : ipcp_value<ipa_polymorphic_call_context> *val;
6054 : 227040 : for (val = ctxlat->values; val; val = val->next)
6055 : 1509 : ret |= decide_about_value (node, i, -1, val, &avals,
6056 : : &self_gen_clones);
6057 : : }
6058 : : }
6059 : :
6060 : 886075 : if (!self_gen_clones.is_empty ())
6061 : : {
6062 : 26 : self_gen_clones.safe_push (node);
6063 : 26 : update_counts_for_self_gen_clones (node, self_gen_clones);
6064 : : }
6065 : :
6066 : 886075 : if (info->do_clone_for_all_contexts)
6067 : : {
6068 : 17337 : if (!dbg_cnt (ipa_cp_values))
6069 : : {
6070 : 0 : info->do_clone_for_all_contexts = false;
6071 : 225 : return ret;
6072 : : }
6073 : :
6074 : 17337 : struct cgraph_node *clone;
6075 : 17337 : auto_vec<cgraph_edge *> callers = node->collect_callers ();
6076 : :
6077 : 83406 : for (int i = callers.length () - 1; i >= 0; i--)
6078 : : {
6079 : 48918 : cgraph_edge *cs = callers[i];
6080 : 48918 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
6081 : :
6082 : 48918 : if (caller_info && caller_info->node_dead)
6083 : 3124 : callers.unordered_remove (i);
6084 : : }
6085 : :
6086 : 17337 : if (!adjust_callers_for_value_intersection (callers, node))
6087 : : {
6088 : : /* If node is not called by anyone, or all its caller edges are
6089 : : self-recursive, the node is not really in use, no need to do
6090 : : cloning. */
6091 : 225 : info->do_clone_for_all_contexts = false;
6092 : 225 : return ret;
6093 : : }
6094 : :
6095 : 17112 : if (dump_file)
6096 : 100 : fprintf (dump_file, " - Creating a specialized node of %s "
6097 : : "for all known contexts.\n", node->dump_name ());
6098 : :
6099 : 17112 : vec<tree> known_csts = avals.m_known_vals.copy ();
6100 : 17112 : vec<ipa_polymorphic_call_context> known_contexts
6101 : 17112 : = copy_useful_known_contexts (avals.m_known_contexts);
6102 : 17112 : find_more_scalar_values_for_callers_subset (node, known_csts, callers);
6103 : 17112 : find_more_contexts_for_caller_subset (node, &known_contexts, callers);
6104 : 17112 : vec<ipa_argagg_value, va_gc> *aggvals
6105 : 17112 : = find_aggregate_values_for_callers_subset (node, callers);
6106 : :
6107 : 17112 : if (!known_contexts_useful_p (known_contexts))
6108 : : {
6109 : 16604 : known_contexts.release ();
6110 : 16604 : known_contexts = vNULL;
6111 : : }
6112 : 17112 : clone = create_specialized_node (node, known_csts, known_contexts,
6113 : : aggvals, callers);
6114 : 17112 : info->do_clone_for_all_contexts = false;
6115 : 17112 : ipa_node_params_sum->get (clone)->is_all_contexts_clone = true;
6116 : 17112 : ret = true;
6117 : 17337 : }
6118 : :
6119 : : return ret;
6120 : 886075 : }
6121 : :
6122 : : /* Transitively mark all callees of NODE within the same SCC as not dead. */
6123 : :
6124 : : static void
6125 : 4053 : spread_undeadness (struct cgraph_node *node)
6126 : : {
6127 : 4053 : struct cgraph_edge *cs;
6128 : :
6129 : 12647 : for (cs = node->callees; cs; cs = cs->next_callee)
6130 : 8594 : if (ipa_edge_within_scc (cs))
6131 : : {
6132 : 792 : struct cgraph_node *callee;
6133 : 792 : class ipa_node_params *info;
6134 : :
6135 : 792 : callee = cs->callee->function_symbol (NULL);
6136 : 792 : info = ipa_node_params_sum->get (callee);
6137 : :
6138 : 792 : if (info && info->node_dead)
6139 : : {
6140 : 66 : info->node_dead = 0;
6141 : 66 : spread_undeadness (callee);
6142 : : }
6143 : : }
6144 : 4053 : }
6145 : :
6146 : : /* Return true if NODE has a caller from outside of its SCC that is not
6147 : : dead. Worker callback for cgraph_for_node_and_aliases. */
6148 : :
6149 : : static bool
6150 : 15766 : has_undead_caller_from_outside_scc_p (struct cgraph_node *node,
6151 : : void *data ATTRIBUTE_UNUSED)
6152 : : {
6153 : 15766 : struct cgraph_edge *cs;
6154 : :
6155 : 19279 : for (cs = node->callers; cs; cs = cs->next_caller)
6156 : 3826 : if (cs->caller->thunk
6157 : 3826 : && cs->caller->call_for_symbol_thunks_and_aliases
6158 : 0 : (has_undead_caller_from_outside_scc_p, NULL, true))
6159 : : return true;
6160 : 3826 : else if (!ipa_edge_within_scc (cs))
6161 : : {
6162 : 3419 : ipa_node_params *caller_info = ipa_node_params_sum->get (cs->caller);
6163 : 3419 : if (!caller_info /* Unoptimized caller are like dead ones. */
6164 : 3417 : || !caller_info->node_dead)
6165 : : return true;
6166 : : }
6167 : : return false;
6168 : : }
6169 : :
6170 : :
6171 : : /* Identify nodes within the same SCC as NODE which are no longer needed
6172 : : because of new clones and will be removed as unreachable. */
6173 : :
6174 : : static void
6175 : 18826 : identify_dead_nodes (struct cgraph_node *node)
6176 : : {
6177 : 18826 : struct cgraph_node *v;
6178 : 37914 : for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6179 : 19088 : if (v->local)
6180 : : {
6181 : 15471 : ipa_node_params *info = ipa_node_params_sum->get (v);
6182 : 15471 : if (info
6183 : 30942 : && !v->call_for_symbol_thunks_and_aliases
6184 : 15471 : (has_undead_caller_from_outside_scc_p, NULL, true))
6185 : 15158 : info->node_dead = 1;
6186 : : }
6187 : :
6188 : 37914 : for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6189 : : {
6190 : 19088 : ipa_node_params *info = ipa_node_params_sum->get (v);
6191 : 19088 : if (info && !info->node_dead)
6192 : 3987 : spread_undeadness (v);
6193 : : }
6194 : :
6195 : 18826 : if (dump_file && (dump_flags & TDF_DETAILS))
6196 : : {
6197 : 101 : for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6198 : 52 : if (ipa_node_params_sum->get (v)
6199 : 52 : && ipa_node_params_sum->get (v)->node_dead)
6200 : 32 : fprintf (dump_file, " Marking node as dead: %s.\n",
6201 : : v->dump_name ());
6202 : : }
6203 : 18826 : }
6204 : :
6205 : : /* The decision stage. Iterate over the topological order of call graph nodes
6206 : : TOPO and make specialized clones if deemed beneficial. */
6207 : :
6208 : : static void
6209 : 126818 : ipcp_decision_stage (class ipa_topo_info *topo)
6210 : : {
6211 : 126818 : int i;
6212 : :
6213 : 126818 : if (dump_file)
6214 : 156 : fprintf (dump_file, "\nIPA decision stage:\n\n");
6215 : :
6216 : 1463701 : for (i = topo->nnodes - 1; i >= 0; i--)
6217 : : {
6218 : 1336883 : struct cgraph_node *node = topo->order[i];
6219 : 1336883 : bool change = false, iterate = true;
6220 : :
6221 : 2692599 : while (iterate)
6222 : : {
6223 : : struct cgraph_node *v;
6224 : : iterate = false;
6225 : 2717515 : for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle)
6226 : 1361799 : if (v->has_gimple_body_p ()
6227 : 1361799 : && ipcp_versionable_function_p (v))
6228 : 1078298 : iterate |= decide_whether_version_node (v);
6229 : :
6230 : 1355716 : change |= iterate;
6231 : : }
6232 : 1336883 : if (change)
6233 : 18826 : identify_dead_nodes (node);
6234 : : }
6235 : 126818 : }
6236 : :
6237 : : /* Look up all VR and bits information that we have discovered and copy it
6238 : : over to the transformation summary. */
6239 : :
6240 : : static void
6241 : 126818 : ipcp_store_vr_results (void)
6242 : : {
6243 : 126818 : cgraph_node *node;
6244 : :
6245 : 1413276 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
6246 : : {
6247 : 1286458 : ipa_node_params *info = ipa_node_params_sum->get (node);
6248 : 1286458 : bool dumped_sth = false;
6249 : 1286458 : bool found_useful_result = false;
6250 : 1286458 : bool do_vr = true;
6251 : 1286458 : bool do_bits = true;
6252 : :
6253 : 1286458 : if (!info || !opt_for_fn (node->decl, flag_ipa_vrp))
6254 : : {
6255 : 10476 : if (dump_file)
6256 : 33 : fprintf (dump_file, "Not considering %s for VR discovery "
6257 : : "and propagate; -fipa-ipa-vrp: disabled.\n",
6258 : : node->dump_name ());
6259 : : do_vr = false;
6260 : : }
6261 : 1286458 : if (!info || !opt_for_fn (node->decl, flag_ipa_bit_cp))
6262 : : {
6263 : 10253 : if (dump_file)
6264 : 4 : fprintf (dump_file, "Not considering %s for ipa bitwise "
6265 : : "propagation ; -fipa-bit-cp: disabled.\n",
6266 : : node->dump_name ());
6267 : 10253 : do_bits = false;
6268 : : }
6269 : 10253 : if (!do_bits && !do_vr)
6270 : 10244 : continue;
6271 : :
6272 : 1276214 : if (info->ipcp_orig_node)
6273 : 19710 : info = ipa_node_params_sum->get (info->ipcp_orig_node);
6274 : 1276214 : if (info->lattices.is_empty ())
6275 : : /* Newly expanded artificial thunks do not have lattices. */
6276 : 225180 : continue;
6277 : :
6278 : 1051034 : unsigned count = ipa_get_param_count (info);
6279 : 3284673 : for (unsigned i = 0; i < count; i++)
6280 : : {
6281 : 2291211 : ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6282 : 2291211 : if (do_vr
6283 : 2291064 : && !plats->m_value_range.bottom_p ()
6284 : 2346595 : && !plats->m_value_range.top_p ())
6285 : : {
6286 : : found_useful_result = true;
6287 : : break;
6288 : : }
6289 : 2235827 : if (do_bits && plats->bits_lattice.constant_p ())
6290 : : {
6291 : : found_useful_result = true;
6292 : : break;
6293 : : }
6294 : : }
6295 : 1051034 : if (!found_useful_result)
6296 : 993462 : continue;
6297 : :
6298 : 57572 : ipcp_transformation_initialize ();
6299 : 57572 : ipcp_transformation *ts = ipcp_transformation_sum->get_create (node);
6300 : 57572 : vec_safe_reserve_exact (ts->m_vr, count);
6301 : :
6302 : 211552 : for (unsigned i = 0; i < count; i++)
6303 : : {
6304 : 153980 : ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i);
6305 : 153980 : ipcp_bits_lattice *bits = NULL;
6306 : :
6307 : 153980 : if (do_bits
6308 : 153976 : && plats->bits_lattice.constant_p ()
6309 : 242378 : && dbg_cnt (ipa_cp_bits))
6310 : 88398 : bits = &plats->bits_lattice;
6311 : :
6312 : 153980 : if (do_vr
6313 : 153962 : && !plats->m_value_range.bottom_p ()
6314 : 106086 : && !plats->m_value_range.top_p ()
6315 : 260066 : && dbg_cnt (ipa_cp_vr))
6316 : : {
6317 : 106086 : if (bits)
6318 : : {
6319 : 83345 : value_range tmp = plats->m_value_range.m_vr;
6320 : 83345 : tree type = ipa_get_type (info, i);
6321 : 166690 : irange_bitmask bm (wide_int::from (bits->get_value (),
6322 : 83345 : TYPE_PRECISION (type),
6323 : 83345 : TYPE_SIGN (type)),
6324 : 166690 : wide_int::from (bits->get_mask (),
6325 : 83345 : TYPE_PRECISION (type),
6326 : 166690 : TYPE_SIGN (type)));
6327 : 83345 : tmp.update_bitmask (bm);
6328 : : // Reflecting the bitmask on the ranges can sometime
6329 : : // produce an UNDEFINED value if the the bitmask update
6330 : : // was previously deferred. See PR 120048.
6331 : 83345 : if (tmp.undefined_p ())
6332 : 1 : tmp.set_varying (type);
6333 : 83345 : ipa_vr vr (tmp);
6334 : 83345 : ts->m_vr->quick_push (vr);
6335 : 83345 : }
6336 : : else
6337 : : {
6338 : 22741 : ipa_vr vr (plats->m_value_range.m_vr);
6339 : 22741 : ts->m_vr->quick_push (vr);
6340 : : }
6341 : : }
6342 : 47894 : else if (bits)
6343 : : {
6344 : 5053 : tree type = ipa_get_type (info, i);
6345 : 5053 : value_range tmp;
6346 : 5053 : tmp.set_varying (type);
6347 : 10106 : irange_bitmask bm (wide_int::from (bits->get_value (),
6348 : 5053 : TYPE_PRECISION (type),
6349 : 5053 : TYPE_SIGN (type)),
6350 : 10106 : wide_int::from (bits->get_mask (),
6351 : 5053 : TYPE_PRECISION (type),
6352 : 10106 : TYPE_SIGN (type)));
6353 : 5053 : tmp.update_bitmask (bm);
6354 : : // Reflecting the bitmask on the ranges can sometime
6355 : : // produce an UNDEFINED value if the the bitmask update
6356 : : // was previously deferred. See PR 120048.
6357 : 5053 : if (tmp.undefined_p ())
6358 : 0 : tmp.set_varying (type);
6359 : 5053 : ipa_vr vr (tmp);
6360 : 5053 : ts->m_vr->quick_push (vr);
6361 : 5053 : }
6362 : : else
6363 : : {
6364 : 42841 : ipa_vr vr;
6365 : 42841 : ts->m_vr->quick_push (vr);
6366 : : }
6367 : :
6368 : 153980 : if (!dump_file || !bits)
6369 : 153640 : continue;
6370 : :
6371 : 340 : if (!dumped_sth)
6372 : : {
6373 : 238 : fprintf (dump_file, "Propagated bits info for function %s:\n",
6374 : : node->dump_name ());
6375 : 238 : dumped_sth = true;
6376 : : }
6377 : 340 : fprintf (dump_file, " param %i: value = ", i);
6378 : 340 : ipcp_print_widest_int (dump_file, bits->get_value ());
6379 : 340 : fprintf (dump_file, ", mask = ");
6380 : 340 : ipcp_print_widest_int (dump_file, bits->get_mask ());
6381 : 340 : fprintf (dump_file, "\n");
6382 : : }
6383 : : }
6384 : 126818 : }
6385 : :
6386 : : /* The IPCP driver. */
6387 : :
6388 : : static unsigned int
6389 : 126818 : ipcp_driver (void)
6390 : : {
6391 : 126818 : class ipa_topo_info topo;
6392 : :
6393 : 126818 : if (edge_clone_summaries == NULL)
6394 : 126818 : edge_clone_summaries = new edge_clone_summary_t (symtab);
6395 : :
6396 : 126818 : ipa_check_create_node_params ();
6397 : 126818 : ipa_check_create_edge_args ();
6398 : 126818 : clone_num_suffixes = new hash_map<const char *, unsigned>;
6399 : :
6400 : 126818 : if (dump_file)
6401 : : {
6402 : 156 : fprintf (dump_file, "\nIPA structures before propagation:\n");
6403 : 156 : if (dump_flags & TDF_DETAILS)
6404 : 43 : ipa_print_all_params (dump_file);
6405 : 156 : ipa_print_all_jump_functions (dump_file);
6406 : : }
6407 : :
6408 : : /* Topological sort. */
6409 : 126818 : build_toporder_info (&topo);
6410 : : /* Do the interprocedural propagation. */
6411 : 126818 : ipcp_propagate_stage (&topo);
6412 : : /* Decide what constant propagation and cloning should be performed. */
6413 : 126818 : ipcp_decision_stage (&topo);
6414 : : /* Store results of value range and bits propagation. */
6415 : 126818 : ipcp_store_vr_results ();
6416 : :
6417 : : /* Free all IPCP structures. */
6418 : 253636 : delete clone_num_suffixes;
6419 : 126818 : free_toporder_info (&topo);
6420 : 126818 : delete edge_clone_summaries;
6421 : 126818 : edge_clone_summaries = NULL;
6422 : 126818 : ipa_free_all_structures_after_ipa_cp ();
6423 : 126818 : if (dump_file)
6424 : 156 : fprintf (dump_file, "\nIPA constant propagation end\n");
6425 : 126818 : return 0;
6426 : : }
6427 : :
6428 : : /* Initialization and computation of IPCP data structures. This is the initial
6429 : : intraprocedural analysis of functions, which gathers information to be
6430 : : propagated later on. */
6431 : :
6432 : : static void
6433 : 122764 : ipcp_generate_summary (void)
6434 : : {
6435 : 122764 : struct cgraph_node *node;
6436 : :
6437 : 122764 : if (dump_file)
6438 : 158 : fprintf (dump_file, "\nIPA constant propagation start:\n");
6439 : 122764 : ipa_register_cgraph_hooks ();
6440 : :
6441 : 1365624 : FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
6442 : 1242860 : ipa_analyze_node (node);
6443 : 122764 : }
6444 : :
6445 : : namespace {
6446 : :
6447 : : const pass_data pass_data_ipa_cp =
6448 : : {
6449 : : IPA_PASS, /* type */
6450 : : "cp", /* name */
6451 : : OPTGROUP_NONE, /* optinfo_flags */
6452 : : TV_IPA_CONSTANT_PROP, /* tv_id */
6453 : : 0, /* properties_required */
6454 : : 0, /* properties_provided */
6455 : : 0, /* properties_destroyed */
6456 : : 0, /* todo_flags_start */
6457 : : ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */
6458 : : };
6459 : :
6460 : : class pass_ipa_cp : public ipa_opt_pass_d
6461 : : {
6462 : : public:
6463 : 284673 : pass_ipa_cp (gcc::context *ctxt)
6464 : : : ipa_opt_pass_d (pass_data_ipa_cp, ctxt,
6465 : : ipcp_generate_summary, /* generate_summary */
6466 : : NULL, /* write_summary */
6467 : : NULL, /* read_summary */
6468 : : ipcp_write_transformation_summaries, /*
6469 : : write_optimization_summary */
6470 : : ipcp_read_transformation_summaries, /*
6471 : : read_optimization_summary */
6472 : : NULL, /* stmt_fixup */
6473 : : 0, /* function_transform_todo_flags_start */
6474 : : ipcp_transform_function, /* function_transform */
6475 : 284673 : NULL) /* variable_transform */
6476 : 284673 : {}
6477 : :
6478 : : /* opt_pass methods: */
6479 : 570219 : bool gate (function *) final override
6480 : : {
6481 : : /* FIXME: We should remove the optimize check after we ensure we never run
6482 : : IPA passes when not optimizing. */
6483 : 570219 : return (flag_ipa_cp && optimize) || in_lto_p;
6484 : : }
6485 : :
6486 : 126818 : unsigned int execute (function *) final override { return ipcp_driver (); }
6487 : :
6488 : : }; // class pass_ipa_cp
6489 : :
6490 : : } // anon namespace
6491 : :
6492 : : ipa_opt_pass_d *
6493 : 284673 : make_pass_ipa_cp (gcc::context *ctxt)
6494 : : {
6495 : 284673 : return new pass_ipa_cp (ctxt);
6496 : : }
6497 : :
6498 : : /* Reset all state within ipa-cp.cc so that we can rerun the compiler
6499 : : within the same process. For use by toplev::finalize. */
6500 : :
6501 : : void
6502 : 255947 : ipa_cp_cc_finalize (void)
6503 : : {
6504 : 255947 : overall_size = 0;
6505 : 255947 : orig_overall_size = 0;
6506 : 255947 : ipcp_free_transformation_sum ();
6507 : 255947 : }
6508 : :
6509 : : /* Given PARAM which must be a parameter of function FNDECL described by THIS,
6510 : : return its index in the DECL_ARGUMENTS chain, using a pre-computed
6511 : : DECL_UID-sorted vector if available (which is pre-computed only if there are
6512 : : many parameters). Can return -1 if param is static chain not represented
6513 : : among DECL_ARGUMENTS. */
6514 : :
6515 : : int
6516 : 117504 : ipcp_transformation::get_param_index (const_tree fndecl, const_tree param) const
6517 : : {
6518 : 117504 : gcc_assert (TREE_CODE (param) == PARM_DECL);
6519 : 117504 : if (m_uid_to_idx)
6520 : : {
6521 : 0 : unsigned puid = DECL_UID (param);
6522 : 0 : const ipa_uid_to_idx_map_elt *res
6523 : 0 : = std::lower_bound (m_uid_to_idx->begin(), m_uid_to_idx->end (), puid,
6524 : 0 : [] (const ipa_uid_to_idx_map_elt &elt, unsigned uid)
6525 : : {
6526 : 0 : return elt.uid < uid;
6527 : : });
6528 : 0 : if (res == m_uid_to_idx->end ()
6529 : 0 : || res->uid != puid)
6530 : : {
6531 : 0 : gcc_assert (DECL_STATIC_CHAIN (fndecl));
6532 : : return -1;
6533 : : }
6534 : 0 : return res->index;
6535 : : }
6536 : :
6537 : 117504 : unsigned index = 0;
6538 : 268424 : for (tree p = DECL_ARGUMENTS (fndecl); p; p = DECL_CHAIN (p), index++)
6539 : 267037 : if (p == param)
6540 : 116117 : return (int) index;
6541 : :
6542 : 1387 : gcc_assert (DECL_STATIC_CHAIN (fndecl));
6543 : : return -1;
6544 : : }
6545 : :
6546 : : /* Helper function to qsort a vector of ipa_uid_to_idx_map_elt elements
6547 : : according to the uid. */
6548 : :
6549 : : static int
6550 : 0 : compare_uids (const void *a, const void *b)
6551 : : {
6552 : 0 : const ipa_uid_to_idx_map_elt *e1 = (const ipa_uid_to_idx_map_elt *) a;
6553 : 0 : const ipa_uid_to_idx_map_elt *e2 = (const ipa_uid_to_idx_map_elt *) b;
6554 : 0 : if (e1->uid < e2->uid)
6555 : : return -1;
6556 : 0 : if (e1->uid > e2->uid)
6557 : : return 1;
6558 : 0 : gcc_unreachable ();
6559 : : }
6560 : :
6561 : : /* Assuming THIS describes FNDECL and it has sufficiently many parameters to
6562 : : justify the overhead, create a DECL_UID-sorted vector to speed up mapping
6563 : : from parameters to their indices in DECL_ARGUMENTS chain. */
6564 : :
6565 : : void
6566 : 20755 : ipcp_transformation::maybe_create_parm_idx_map (tree fndecl)
6567 : : {
6568 : 20755 : int c = count_formal_params (fndecl);
6569 : 20755 : if (c < 32)
6570 : : return;
6571 : :
6572 : 0 : m_uid_to_idx = NULL;
6573 : 0 : vec_safe_reserve (m_uid_to_idx, c, true);
6574 : 0 : unsigned index = 0;
6575 : 0 : for (tree p = DECL_ARGUMENTS (fndecl); p; p = DECL_CHAIN (p), index++)
6576 : : {
6577 : 0 : ipa_uid_to_idx_map_elt elt;
6578 : 0 : elt.uid = DECL_UID (p);
6579 : 0 : elt.index = index;
6580 : 0 : m_uid_to_idx->quick_push (elt);
6581 : : }
6582 : 0 : m_uid_to_idx->qsort (compare_uids);
6583 : : }
|