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