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