| 1 |
/* Function summary pass. |
| 2 |
Copyright (C) 2003-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Jan Hubicka |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
/* Analysis of function bodies used by inter-procedural passes |
| 22 |
|
| 23 |
We estimate for each function |
| 24 |
- function body size and size after specializing into given context |
| 25 |
- average function execution time in a given context |
| 26 |
- function frame size |
| 27 |
For each call |
| 28 |
- call statement size, time and how often the parameters change |
| 29 |
|
| 30 |
ipa_fn_summary data structures store above information locally (i.e. |
| 31 |
parameters of the function itself) and globally (i.e. parameters of |
| 32 |
the function created by applying all the inline decisions already |
| 33 |
present in the callgraph). |
| 34 |
|
| 35 |
We provide access to the ipa_fn_summary data structure and |
| 36 |
basic logic updating the parameters when inlining is performed. |
| 37 |
|
| 38 |
The summaries are context sensitive. Context means |
| 39 |
1) partial assignment of known constant values of operands |
| 40 |
2) whether function is inlined into the call or not. |
| 41 |
It is easy to add more variants. To represent function size and time |
| 42 |
that depends on context (i.e. it is known to be optimized away when |
| 43 |
context is known either by inlining or from IP-CP and cloning), |
| 44 |
we use predicates. |
| 45 |
|
| 46 |
estimate_edge_size_and_time can be used to query |
| 47 |
function size/time in the given context. ipa_merge_fn_summary_after_inlining merges |
| 48 |
properties of caller and callee after inlining. |
| 49 |
|
| 50 |
Finally pass_inline_parameters is exported. This is used to drive |
| 51 |
computation of function parameters used by the early inliner. IPA |
| 52 |
inlined performs analysis via its analyze_function method. */ |
| 53 |
|
| 54 |
#include "config.h" |
| 55 |
#define INCLUDE_VECTOR |
| 56 |
#include "system.h" |
| 57 |
#include "coretypes.h" |
| 58 |
#include "backend.h" |
| 59 |
#include "target.h" |
| 60 |
#include "tree.h" |
| 61 |
#include "gimple.h" |
| 62 |
#include "alloc-pool.h" |
| 63 |
#include "tree-pass.h" |
| 64 |
#include "ssa.h" |
| 65 |
#include "tree-streamer.h" |
| 66 |
#include "cgraph.h" |
| 67 |
#include "diagnostic.h" |
| 68 |
#include "fold-const.h" |
| 69 |
#include "print-tree.h" |
| 70 |
#include "tree-inline.h" |
| 71 |
#include "gimple-pretty-print.h" |
| 72 |
#include "cfganal.h" |
| 73 |
#include "gimple-iterator.h" |
| 74 |
#include "tree-cfg.h" |
| 75 |
#include "tree-ssa-loop-niter.h" |
| 76 |
#include "tree-ssa-loop.h" |
| 77 |
#include "symbol-summary.h" |
| 78 |
#include "sreal.h" |
| 79 |
#include "ipa-cp.h" |
| 80 |
#include "ipa-prop.h" |
| 81 |
#include "ipa-fnsummary.h" |
| 82 |
#include "cfgloop.h" |
| 83 |
#include "tree-scalar-evolution.h" |
| 84 |
#include "ipa-utils.h" |
| 85 |
#include "cfgexpand.h" |
| 86 |
#include "gimplify.h" |
| 87 |
#include "stringpool.h" |
| 88 |
#include "attribs.h" |
| 89 |
#include "tree-into-ssa.h" |
| 90 |
#include "symtab-clones.h" |
| 91 |
#include "gimple-range.h" |
| 92 |
#include "tree-dfa.h" |
| 93 |
|
| 94 |
/* Summaries. */ |
| 95 |
fast_function_summary <ipa_fn_summary *, va_gc> *ipa_fn_summaries; |
| 96 |
fast_function_summary <ipa_size_summary *, va_heap> *ipa_size_summaries; |
| 97 |
fast_call_summary <ipa_call_summary *, va_heap> *ipa_call_summaries; |
| 98 |
|
| 99 |
/* Edge predicates goes here. */ |
| 100 |
static object_allocator<ipa_predicate> edge_predicate_pool ("edge predicates"); |
| 101 |
|
| 102 |
|
| 103 |
/* Dump IPA hints. */ |
| 104 |
void |
| 105 |
ipa_dump_hints (FILE *f, ipa_hints hints) |
| 106 |
{ |
| 107 |
if (!hints) |
| 108 |
return; |
| 109 |
fprintf (f, "IPA hints:"); |
| 110 |
if (hints & INLINE_HINT_indirect_call) |
| 111 |
{ |
| 112 |
hints &= ~INLINE_HINT_indirect_call; |
| 113 |
fprintf (f, " indirect_call"); |
| 114 |
} |
| 115 |
if (hints & INLINE_HINT_loop_iterations) |
| 116 |
{ |
| 117 |
hints &= ~INLINE_HINT_loop_iterations; |
| 118 |
fprintf (f, " loop_iterations"); |
| 119 |
} |
| 120 |
if (hints & INLINE_HINT_loop_stride) |
| 121 |
{ |
| 122 |
hints &= ~INLINE_HINT_loop_stride; |
| 123 |
fprintf (f, " loop_stride"); |
| 124 |
} |
| 125 |
if (hints & INLINE_HINT_same_scc) |
| 126 |
{ |
| 127 |
hints &= ~INLINE_HINT_same_scc; |
| 128 |
fprintf (f, " same_scc"); |
| 129 |
} |
| 130 |
if (hints & INLINE_HINT_in_scc) |
| 131 |
{ |
| 132 |
hints &= ~INLINE_HINT_in_scc; |
| 133 |
fprintf (f, " in_scc"); |
| 134 |
} |
| 135 |
if (hints & INLINE_HINT_cross_module) |
| 136 |
{ |
| 137 |
hints &= ~INLINE_HINT_cross_module; |
| 138 |
fprintf (f, " cross_module"); |
| 139 |
} |
| 140 |
if (hints & INLINE_HINT_declared_inline) |
| 141 |
{ |
| 142 |
hints &= ~INLINE_HINT_declared_inline; |
| 143 |
fprintf (f, " declared_inline"); |
| 144 |
} |
| 145 |
if (hints & INLINE_HINT_known_hot) |
| 146 |
{ |
| 147 |
hints &= ~INLINE_HINT_known_hot; |
| 148 |
fprintf (f, " known_hot"); |
| 149 |
} |
| 150 |
if (hints & INLINE_HINT_builtin_constant_p) |
| 151 |
{ |
| 152 |
hints &= ~INLINE_HINT_builtin_constant_p; |
| 153 |
fprintf (f, " builtin_constant_p"); |
| 154 |
} |
| 155 |
gcc_assert (!hints); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* Record SIZE and TIME to SUMMARY. |
| 160 |
The accounted code will be executed when EXEC_PRED is true. |
| 161 |
When NONCONST_PRED is false the code will evaluate to constant and |
| 162 |
will get optimized out in specialized clones of the function. |
| 163 |
If CALL is true account to call_size_time_table rather than |
| 164 |
size_time_table. */ |
| 165 |
|
| 166 |
void |
| 167 |
ipa_fn_summary::account_size_time (int size, sreal time, |
| 168 |
const ipa_predicate &exec_pred, |
| 169 |
const ipa_predicate &nonconst_pred_in, |
| 170 |
bool call) |
| 171 |
{ |
| 172 |
size_time_entry *e; |
| 173 |
bool found = false; |
| 174 |
int i; |
| 175 |
ipa_predicate nonconst_pred; |
| 176 |
vec<size_time_entry> *table = call ? &call_size_time_table : &size_time_table; |
| 177 |
|
| 178 |
if (exec_pred == false) |
| 179 |
return; |
| 180 |
|
| 181 |
nonconst_pred = nonconst_pred_in & exec_pred; |
| 182 |
|
| 183 |
if (nonconst_pred == false) |
| 184 |
return; |
| 185 |
|
| 186 |
/* We need to create initial empty unconditional clause, but otherwise |
| 187 |
we don't need to account empty times and sizes. */ |
| 188 |
if (!size && time == 0 && table->length ()) |
| 189 |
return; |
| 190 |
|
| 191 |
/* Only for calls we are unaccounting what we previously recorded. */ |
| 192 |
gcc_checking_assert (time >= 0 || call); |
| 193 |
|
| 194 |
for (i = 0; table->iterate (i, &e); i++) |
| 195 |
if (e->exec_predicate == exec_pred |
| 196 |
&& e->nonconst_predicate == nonconst_pred) |
| 197 |
{ |
| 198 |
found = true; |
| 199 |
break; |
| 200 |
} |
| 201 |
if (i == max_size_time_table_size) |
| 202 |
{ |
| 203 |
i = 0; |
| 204 |
found = true; |
| 205 |
e = &(*table)[0]; |
| 206 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 207 |
fprintf (dump_file, |
| 208 |
"\t\tReached limit on number of entries, " |
| 209 |
"ignoring the predicate."); |
| 210 |
} |
| 211 |
if (dump_file && (dump_flags & TDF_DETAILS) && (time != 0 || size)) |
| 212 |
{ |
| 213 |
fprintf (dump_file, |
| 214 |
"\t\tAccounting size:%3.2f, time:%3.2f on %spredicate exec:", |
| 215 |
((double) size) / ipa_fn_summary::size_scale, |
| 216 |
(time.to_double ()), found ? "" : "new "); |
| 217 |
exec_pred.dump (dump_file, conds, 0); |
| 218 |
if (exec_pred != nonconst_pred) |
| 219 |
{ |
| 220 |
fprintf (dump_file, " nonconst:"); |
| 221 |
nonconst_pred.dump (dump_file, conds); |
| 222 |
} |
| 223 |
else |
| 224 |
fprintf (dump_file, "\n"); |
| 225 |
} |
| 226 |
if (!found) |
| 227 |
{ |
| 228 |
class size_time_entry new_entry; |
| 229 |
new_entry.size = size; |
| 230 |
new_entry.time = time; |
| 231 |
new_entry.exec_predicate = exec_pred; |
| 232 |
new_entry.nonconst_predicate = nonconst_pred; |
| 233 |
if (call) |
| 234 |
call_size_time_table.safe_push (new_entry); |
| 235 |
else |
| 236 |
size_time_table.safe_push (new_entry); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
e->size += size; |
| 241 |
e->time += time; |
| 242 |
/* FIXME: PR bootstrap/92653 gcc_checking_assert (e->time >= -1); */ |
| 243 |
/* Tolerate small roundoff issues. */ |
| 244 |
if (e->time < 0) |
| 245 |
e->time = 0; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* We proved E to be unreachable, redirect it to __builtin_unreachable. */ |
| 250 |
|
| 251 |
static struct cgraph_edge * |
| 252 |
redirect_to_unreachable (struct cgraph_edge *e) |
| 253 |
{ |
| 254 |
struct cgraph_node *callee = !e->inline_failed ? e->callee : NULL; |
| 255 |
struct cgraph_node *target |
| 256 |
= cgraph_node::get_create (builtin_decl_unreachable ()); |
| 257 |
|
| 258 |
gcc_checking_assert (lookup_attribute ("cold", |
| 259 |
DECL_ATTRIBUTES (target->decl))); |
| 260 |
|
| 261 |
if (e->speculative) |
| 262 |
e = cgraph_edge::resolve_speculation (e, target->decl); |
| 263 |
else if (!e->callee) |
| 264 |
e = cgraph_edge::make_direct (e, target); |
| 265 |
else |
| 266 |
e->redirect_callee (target); |
| 267 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 268 |
e->inline_failed = CIF_UNREACHABLE; |
| 269 |
e->count = profile_count::zero (); |
| 270 |
es->call_stmt_size = 0; |
| 271 |
es->call_stmt_time = 0; |
| 272 |
if (callee) |
| 273 |
callee->remove_symbol_and_inline_clones (); |
| 274 |
return e; |
| 275 |
} |
| 276 |
|
| 277 |
/* Set predicate for edge E. */ |
| 278 |
|
| 279 |
static void |
| 280 |
edge_set_predicate (struct cgraph_edge *e, ipa_predicate *predicate) |
| 281 |
{ |
| 282 |
/* If the edge is determined to be never executed, redirect it |
| 283 |
to BUILTIN_UNREACHABLE to make it clear to IPA passes the call will |
| 284 |
be optimized out. */ |
| 285 |
if (predicate && *predicate == false |
| 286 |
/* When handling speculative edges, we need to do the redirection |
| 287 |
just once. Do it always on the direct edge, so we do not |
| 288 |
attempt to resolve speculation while duplicating the edge. */ |
| 289 |
&& (!e->speculative || e->callee)) |
| 290 |
e = redirect_to_unreachable (e); |
| 291 |
|
| 292 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 293 |
if (predicate && *predicate != true) |
| 294 |
{ |
| 295 |
if (!es->predicate) |
| 296 |
es->predicate = edge_predicate_pool.allocate (); |
| 297 |
*es->predicate = *predicate; |
| 298 |
} |
| 299 |
else |
| 300 |
{ |
| 301 |
if (es->predicate) |
| 302 |
edge_predicate_pool.remove (es->predicate); |
| 303 |
es->predicate = NULL; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/* Set predicate for hint *P. */ |
| 308 |
|
| 309 |
static void |
| 310 |
set_hint_predicate (ipa_predicate **p, ipa_predicate new_predicate) |
| 311 |
{ |
| 312 |
if (new_predicate == false || new_predicate == true) |
| 313 |
{ |
| 314 |
if (*p) |
| 315 |
edge_predicate_pool.remove (*p); |
| 316 |
*p = NULL; |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
if (!*p) |
| 321 |
*p = edge_predicate_pool.allocate (); |
| 322 |
**p = new_predicate; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/* Find if NEW_PREDICATE is already in V and if so, increment its freq. |
| 327 |
Otherwise add a new item to the vector with this predicate and frerq equal |
| 328 |
to add_freq, unless the number of predicates would exceed MAX_NUM_PREDICATES |
| 329 |
in which case the function does nothing. */ |
| 330 |
|
| 331 |
static void |
| 332 |
add_freqcounting_predicate (vec<ipa_freqcounting_predicate, va_gc> **v, |
| 333 |
const ipa_predicate &new_predicate, sreal add_freq, |
| 334 |
unsigned max_num_predicates) |
| 335 |
{ |
| 336 |
if (new_predicate == false || new_predicate == true) |
| 337 |
return; |
| 338 |
ipa_freqcounting_predicate *f; |
| 339 |
for (int i = 0; vec_safe_iterate (*v, i, &f); i++) |
| 340 |
if (new_predicate == f->predicate) |
| 341 |
{ |
| 342 |
f->freq += add_freq; |
| 343 |
return; |
| 344 |
} |
| 345 |
if (vec_safe_length (*v) >= max_num_predicates) |
| 346 |
/* Too many different predicates to account for. */ |
| 347 |
return; |
| 348 |
|
| 349 |
ipa_freqcounting_predicate fcp; |
| 350 |
fcp.predicate = NULL; |
| 351 |
set_hint_predicate (&fcp.predicate, new_predicate); |
| 352 |
fcp.freq = add_freq; |
| 353 |
vec_safe_push (*v, fcp); |
| 354 |
return; |
| 355 |
} |
| 356 |
|
| 357 |
/* Compute what conditions may or may not hold given information about |
| 358 |
parameters. RET_CLAUSE returns truths that may hold in a specialized copy, |
| 359 |
while RET_NONSPEC_CLAUSE returns truths that may hold in an nonspecialized |
| 360 |
copy when called in a given context. It is a bitmask of conditions. Bit |
| 361 |
0 means that condition is known to be false, while bit 1 means that condition |
| 362 |
may or may not be true. These differs - for example NOT_INLINED condition |
| 363 |
is always false in the second and also builtin_constant_p tests cannot use |
| 364 |
the fact that parameter is indeed a constant. |
| 365 |
|
| 366 |
When INLINE_P is true, assume that we are inlining. AVAL contains known |
| 367 |
information about argument values. The function does not modify its content |
| 368 |
and so AVALs could also be of type ipa_call_arg_values but so far all |
| 369 |
callers work with the auto version and so we avoid the conversion for |
| 370 |
convenience. |
| 371 |
|
| 372 |
ERROR_MARK value of an argument means compile time invariant. */ |
| 373 |
|
| 374 |
static void |
| 375 |
evaluate_conditions_for_known_args (struct cgraph_node *node, |
| 376 |
bool inline_p, |
| 377 |
ipa_auto_call_arg_values *avals, |
| 378 |
clause_t *ret_clause, |
| 379 |
clause_t *ret_nonspec_clause, |
| 380 |
ipa_call_summary *es) |
| 381 |
{ |
| 382 |
clause_t clause = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition; |
| 383 |
clause_t nonspec_clause = 1 << ipa_predicate::not_inlined_condition; |
| 384 |
class ipa_fn_summary *info = ipa_fn_summaries->get (node); |
| 385 |
int i; |
| 386 |
struct condition *c; |
| 387 |
|
| 388 |
for (i = 0; vec_safe_iterate (info->conds, i, &c); i++) |
| 389 |
{ |
| 390 |
tree val = NULL; |
| 391 |
tree res; |
| 392 |
int j; |
| 393 |
struct expr_eval_op *op; |
| 394 |
|
| 395 |
if (c->code == ipa_predicate::not_sra_candidate) |
| 396 |
{ |
| 397 |
if (!inline_p |
| 398 |
|| !es |
| 399 |
|| (int)es->param.length () <= c->operand_num |
| 400 |
|| !es->param[c->operand_num].points_to_possible_sra_candidate) |
| 401 |
clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 402 |
nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
if (c->agg_contents) |
| 407 |
{ |
| 408 |
if (c->code == ipa_predicate::changed |
| 409 |
&& !c->by_ref |
| 410 |
&& (avals->safe_sval_at(c->operand_num) == error_mark_node)) |
| 411 |
continue; |
| 412 |
|
| 413 |
if (tree sval = avals->safe_sval_at (c->operand_num)) |
| 414 |
val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref); |
| 415 |
if (!val) |
| 416 |
{ |
| 417 |
ipa_argagg_value_list avs (avals); |
| 418 |
val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT, |
| 419 |
c->by_ref); |
| 420 |
} |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
val = avals->safe_sval_at (c->operand_num); |
| 425 |
if (val && val == error_mark_node |
| 426 |
&& c->code != ipa_predicate::changed) |
| 427 |
val = NULL_TREE; |
| 428 |
} |
| 429 |
|
| 430 |
if (!val |
| 431 |
&& (c->code == ipa_predicate::changed |
| 432 |
|| c->code == ipa_predicate::is_not_constant)) |
| 433 |
{ |
| 434 |
clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 435 |
nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 436 |
continue; |
| 437 |
} |
| 438 |
if (c->code == ipa_predicate::changed) |
| 439 |
{ |
| 440 |
nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 441 |
continue; |
| 442 |
} |
| 443 |
|
| 444 |
if (c->code == ipa_predicate::is_not_constant) |
| 445 |
{ |
| 446 |
nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 447 |
continue; |
| 448 |
} |
| 449 |
|
| 450 |
if (val && TYPE_SIZE (c->type) == TYPE_SIZE (TREE_TYPE (val))) |
| 451 |
{ |
| 452 |
if (c->type != TREE_TYPE (val)) |
| 453 |
val = fold_unary (VIEW_CONVERT_EXPR, c->type, val); |
| 454 |
for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++) |
| 455 |
{ |
| 456 |
if (!val) |
| 457 |
break; |
| 458 |
if (!op->val[0]) |
| 459 |
val = fold_unary (op->code, op->type, val); |
| 460 |
else if (!op->val[1]) |
| 461 |
val = fold_binary (op->code, op->type, |
| 462 |
op->index ? op->val[0] : val, |
| 463 |
op->index ? val : op->val[0]); |
| 464 |
else if (op->index == 0) |
| 465 |
val = fold_ternary (op->code, op->type, |
| 466 |
val, op->val[0], op->val[1]); |
| 467 |
else if (op->index == 1) |
| 468 |
val = fold_ternary (op->code, op->type, |
| 469 |
op->val[0], val, op->val[1]); |
| 470 |
else if (op->index == 2) |
| 471 |
val = fold_ternary (op->code, op->type, |
| 472 |
op->val[0], op->val[1], val); |
| 473 |
else |
| 474 |
val = NULL_TREE; |
| 475 |
} |
| 476 |
|
| 477 |
res = val |
| 478 |
? fold_binary_to_constant (c->code, boolean_type_node, val, c->val) |
| 479 |
: NULL; |
| 480 |
|
| 481 |
if (res && integer_zerop (res)) |
| 482 |
continue; |
| 483 |
if (res && integer_onep (res)) |
| 484 |
{ |
| 485 |
clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 486 |
nonspec_clause |
| 487 |
|= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 488 |
continue; |
| 489 |
} |
| 490 |
} |
| 491 |
if (c->operand_num < (int) avals->m_known_value_ranges.length () |
| 492 |
&& !c->agg_contents |
| 493 |
&& (!val || TREE_CODE (val) != INTEGER_CST)) |
| 494 |
{ |
| 495 |
value_range vr (avals->m_known_value_ranges[c->operand_num]); |
| 496 |
if (!vr.undefined_p () |
| 497 |
&& !vr.varying_p () |
| 498 |
&& (TYPE_SIZE (c->type) == TYPE_SIZE (vr.type ()))) |
| 499 |
{ |
| 500 |
if (!useless_type_conversion_p (c->type, vr.type ())) |
| 501 |
range_cast (vr, c->type); |
| 502 |
|
| 503 |
for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++) |
| 504 |
{ |
| 505 |
if (vr.varying_p () || vr.undefined_p ()) |
| 506 |
break; |
| 507 |
|
| 508 |
value_range res (op->type); |
| 509 |
if (!op->val[0]) |
| 510 |
{ |
| 511 |
value_range varying (op->type); |
| 512 |
varying.set_varying (op->type); |
| 513 |
range_op_handler handler (op->code); |
| 514 |
if (!handler |
| 515 |
|| !res.supports_type_p (op->type) |
| 516 |
|| !handler.fold_range (res, op->type, vr, varying)) |
| 517 |
res.set_varying (op->type); |
| 518 |
} |
| 519 |
else if (!op->val[1]) |
| 520 |
{ |
| 521 |
value_range op0 (TREE_TYPE (op->val[0])); |
| 522 |
range_op_handler handler (op->code); |
| 523 |
|
| 524 |
ipa_get_range_from_ip_invariant (op0, op->val[0], node); |
| 525 |
|
| 526 |
if (!handler |
| 527 |
|| !res.supports_type_p (op->type) |
| 528 |
|| !handler.fold_range (res, op->type, |
| 529 |
op->index ? op0 : vr, |
| 530 |
op->index ? vr : op0)) |
| 531 |
res.set_varying (op->type); |
| 532 |
} |
| 533 |
else |
| 534 |
res.set_varying (op->type); |
| 535 |
vr = res; |
| 536 |
} |
| 537 |
if (!vr.varying_p () && !vr.undefined_p ()) |
| 538 |
{ |
| 539 |
int_range<2> res; |
| 540 |
value_range val_vr (TREE_TYPE (c->val)); |
| 541 |
range_op_handler handler (c->code); |
| 542 |
|
| 543 |
ipa_get_range_from_ip_invariant (val_vr, c->val, node); |
| 544 |
|
| 545 |
if (!handler |
| 546 |
|| !val_vr.supports_type_p (TREE_TYPE (c->val)) |
| 547 |
|| !handler.fold_range (res, boolean_type_node, vr, val_vr)) |
| 548 |
res.set_varying (boolean_type_node); |
| 549 |
|
| 550 |
if (res.zero_p ()) |
| 551 |
continue; |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 557 |
nonspec_clause |= 1 << (i + ipa_predicate::first_dynamic_condition); |
| 558 |
} |
| 559 |
*ret_clause = clause; |
| 560 |
if (ret_nonspec_clause) |
| 561 |
*ret_nonspec_clause = nonspec_clause; |
| 562 |
} |
| 563 |
|
| 564 |
/* Return true if VRP will be exectued on the function. |
| 565 |
We do not want to anticipate optimizations that will not happen. |
| 566 |
|
| 567 |
FIXME: This can be confused with -fdisable and debug counters and thus |
| 568 |
it should not be used for correctness (only to make heuristics work). |
| 569 |
This means that inliner should do its own optimizations of expressions |
| 570 |
that it predicts to be constant so wrong code can not be triggered by |
| 571 |
builtin_constant_p. */ |
| 572 |
|
| 573 |
static bool |
| 574 |
vrp_will_run_p (struct cgraph_node *node) |
| 575 |
{ |
| 576 |
return (opt_for_fn (node->decl, optimize) |
| 577 |
&& !opt_for_fn (node->decl, optimize_debug) |
| 578 |
&& opt_for_fn (node->decl, flag_tree_vrp)); |
| 579 |
} |
| 580 |
|
| 581 |
/* Similarly about FRE. */ |
| 582 |
|
| 583 |
static bool |
| 584 |
fre_will_run_p (struct cgraph_node *node) |
| 585 |
{ |
| 586 |
return (opt_for_fn (node->decl, optimize) |
| 587 |
&& !opt_for_fn (node->decl, optimize_debug) |
| 588 |
&& opt_for_fn (node->decl, flag_tree_fre)); |
| 589 |
} |
| 590 |
|
| 591 |
/* Work out what conditions might be true at invocation of E. |
| 592 |
Compute costs for inlined edge if INLINE_P is true. |
| 593 |
|
| 594 |
Return in CLAUSE_PTR the evaluated conditions and in NONSPEC_CLAUSE_PTR |
| 595 |
(if non-NULL) conditions evaluated for nonspecialized clone called |
| 596 |
in a given context. |
| 597 |
|
| 598 |
Vectors in AVALS will be populated with useful known information about |
| 599 |
argument values - information not known to have any uses will be omitted - |
| 600 |
except for m_known_contexts which will only be calculated if |
| 601 |
COMPUTE_CONTEXTS is true. */ |
| 602 |
|
| 603 |
void |
| 604 |
evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p, |
| 605 |
clause_t *clause_ptr, |
| 606 |
clause_t *nonspec_clause_ptr, |
| 607 |
ipa_auto_call_arg_values *avals, |
| 608 |
bool compute_contexts) |
| 609 |
{ |
| 610 |
struct cgraph_node *callee = e->callee->ultimate_alias_target (); |
| 611 |
class ipa_fn_summary *info = ipa_fn_summaries->get (callee); |
| 612 |
class ipa_edge_args *args; |
| 613 |
class ipa_call_summary *es = NULL; |
| 614 |
|
| 615 |
if (clause_ptr) |
| 616 |
*clause_ptr = inline_p ? 0 : 1 << ipa_predicate::not_inlined_condition; |
| 617 |
|
| 618 |
if (ipa_node_params_sum |
| 619 |
&& !e->call_stmt_cannot_inline_p |
| 620 |
&& (info->conds || compute_contexts) |
| 621 |
&& (args = ipa_edge_args_sum->get (e)) != NULL) |
| 622 |
{ |
| 623 |
struct cgraph_node *caller; |
| 624 |
class ipa_node_params *caller_parms_info, *callee_pi = NULL; |
| 625 |
int i, count = ipa_get_cs_argument_count (args); |
| 626 |
es = ipa_call_summaries->get (e); |
| 627 |
|
| 628 |
if (count) |
| 629 |
{ |
| 630 |
if (e->caller->inlined_to) |
| 631 |
caller = e->caller->inlined_to; |
| 632 |
else |
| 633 |
caller = e->caller; |
| 634 |
caller_parms_info = ipa_node_params_sum->get (caller); |
| 635 |
callee_pi = ipa_node_params_sum->get (callee); |
| 636 |
|
| 637 |
/* Watch for thunks. */ |
| 638 |
if (callee_pi) |
| 639 |
/* Watch for variadic functions. */ |
| 640 |
count = MIN (count, ipa_get_param_count (callee_pi)); |
| 641 |
} |
| 642 |
|
| 643 |
if (callee_pi) |
| 644 |
for (i = 0; i < count; i++) |
| 645 |
{ |
| 646 |
struct ipa_jump_func *jf = ipa_get_ith_jump_func (args, i); |
| 647 |
|
| 648 |
if (ipa_is_param_used_by_indirect_call (callee_pi, i) |
| 649 |
|| ipa_is_param_used_by_ipa_predicates (callee_pi, i)) |
| 650 |
{ |
| 651 |
/* Determine if we know constant value of the parameter. */ |
| 652 |
tree type = ipa_get_type (callee_pi, i); |
| 653 |
tree cst = ipa_value_from_jfunc (caller_parms_info, jf, type); |
| 654 |
|
| 655 |
if (!cst && e->call_stmt |
| 656 |
&& i < (int)gimple_call_num_args (e->call_stmt)) |
| 657 |
{ |
| 658 |
cst = gimple_call_arg (e->call_stmt, i); |
| 659 |
if (!is_gimple_min_invariant (cst)) |
| 660 |
cst = NULL; |
| 661 |
} |
| 662 |
if (cst) |
| 663 |
{ |
| 664 |
gcc_checking_assert (TREE_CODE (cst) != TREE_BINFO); |
| 665 |
if (!avals->m_known_vals.length ()) |
| 666 |
avals->m_known_vals.safe_grow_cleared (count, true); |
| 667 |
avals->m_known_vals[i] = cst; |
| 668 |
} |
| 669 |
else if (inline_p && !es->param[i].change_prob) |
| 670 |
{ |
| 671 |
if (!avals->m_known_vals.length ()) |
| 672 |
avals->m_known_vals.safe_grow_cleared (count, true); |
| 673 |
avals->m_known_vals[i] = error_mark_node; |
| 674 |
} |
| 675 |
|
| 676 |
/* If we failed to get simple constant, try value range. */ |
| 677 |
if ((!cst || TREE_CODE (cst) != INTEGER_CST) |
| 678 |
&& vrp_will_run_p (caller) |
| 679 |
&& ipa_is_param_used_by_ipa_predicates (callee_pi, i)) |
| 680 |
{ |
| 681 |
value_range vr (type); |
| 682 |
|
| 683 |
ipa_value_range_from_jfunc (vr, caller_parms_info, e, jf, type); |
| 684 |
if (!vr.undefined_p () && !vr.varying_p ()) |
| 685 |
{ |
| 686 |
if (!avals->m_known_value_ranges.length ()) |
| 687 |
{ |
| 688 |
avals->m_known_value_ranges.safe_grow_cleared (count, |
| 689 |
true); |
| 690 |
for (int i = 0; i < count; ++i) |
| 691 |
avals->m_known_value_ranges[i].set_type (void_type_node); |
| 692 |
} |
| 693 |
avals->m_known_value_ranges[i] = vr; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/* Determine known aggregate values. */ |
| 698 |
if (fre_will_run_p (caller)) |
| 699 |
ipa_push_agg_values_from_jfunc (caller_parms_info, |
| 700 |
caller, &jf->agg, i, |
| 701 |
&avals->m_known_aggs); |
| 702 |
} |
| 703 |
|
| 704 |
/* For calls used in polymorphic calls we further determine |
| 705 |
polymorphic call context. */ |
| 706 |
if (compute_contexts |
| 707 |
&& ipa_is_param_used_by_polymorphic_call (callee_pi, i)) |
| 708 |
{ |
| 709 |
ipa_polymorphic_call_context |
| 710 |
ctx = ipa_context_from_jfunc (caller_parms_info, e, i, jf); |
| 711 |
if (!ctx.useless_p ()) |
| 712 |
{ |
| 713 |
if (!avals->m_known_contexts.length ()) |
| 714 |
avals->m_known_contexts.safe_grow_cleared (count, true); |
| 715 |
avals->m_known_contexts[i] |
| 716 |
= ipa_context_from_jfunc (caller_parms_info, e, i, jf); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
else |
| 721 |
gcc_assert (!count || callee->thunk); |
| 722 |
} |
| 723 |
else if (e->call_stmt && !e->call_stmt_cannot_inline_p && info->conds) |
| 724 |
{ |
| 725 |
int i, count = (int)gimple_call_num_args (e->call_stmt); |
| 726 |
|
| 727 |
for (i = 0; i < count; i++) |
| 728 |
{ |
| 729 |
tree cst = gimple_call_arg (e->call_stmt, i); |
| 730 |
if (!is_gimple_min_invariant (cst)) |
| 731 |
cst = NULL; |
| 732 |
if (cst) |
| 733 |
{ |
| 734 |
if (!avals->m_known_vals.length ()) |
| 735 |
avals->m_known_vals.safe_grow_cleared (count, true); |
| 736 |
avals->m_known_vals[i] = cst; |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
evaluate_conditions_for_known_args (callee, inline_p, avals, clause_ptr, |
| 742 |
nonspec_clause_ptr, es); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/* Allocate the function summary. */ |
| 747 |
|
| 748 |
static void |
| 749 |
ipa_fn_summary_alloc (void) |
| 750 |
{ |
| 751 |
gcc_checking_assert (!ipa_fn_summaries); |
| 752 |
ipa_size_summaries = new ipa_size_summary_t (symtab); |
| 753 |
ipa_fn_summaries = ipa_fn_summary_t::create_ggc (symtab); |
| 754 |
ipa_call_summaries = new ipa_call_summary_t (symtab); |
| 755 |
} |
| 756 |
|
| 757 |
ipa_call_summary::~ipa_call_summary () |
| 758 |
{ |
| 759 |
if (predicate) |
| 760 |
edge_predicate_pool.remove (predicate); |
| 761 |
|
| 762 |
param.release (); |
| 763 |
} |
| 764 |
|
| 765 |
ipa_fn_summary::~ipa_fn_summary () |
| 766 |
{ |
| 767 |
unsigned len = vec_safe_length (loop_iterations); |
| 768 |
for (unsigned i = 0; i < len; i++) |
| 769 |
edge_predicate_pool.remove ((*loop_iterations)[i].predicate); |
| 770 |
len = vec_safe_length (loop_strides); |
| 771 |
for (unsigned i = 0; i < len; i++) |
| 772 |
edge_predicate_pool.remove ((*loop_strides)[i].predicate); |
| 773 |
vec_free (conds); |
| 774 |
call_size_time_table.release (); |
| 775 |
vec_free (loop_iterations); |
| 776 |
vec_free (loop_strides); |
| 777 |
builtin_constant_p_parms.release (); |
| 778 |
} |
| 779 |
|
| 780 |
void |
| 781 |
ipa_fn_summary_t::remove_callees (cgraph_node *node) |
| 782 |
{ |
| 783 |
cgraph_edge *e; |
| 784 |
for (e = node->callees; e; e = e->next_callee) |
| 785 |
ipa_call_summaries->remove (e); |
| 786 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 787 |
ipa_call_summaries->remove (e); |
| 788 |
} |
| 789 |
|
| 790 |
/* Duplicate predicates in loop hint vector, allocating memory for them and |
| 791 |
remove and deallocate any uninteresting (true or false) ones. Return the |
| 792 |
result. */ |
| 793 |
|
| 794 |
static vec<ipa_freqcounting_predicate, va_gc> * |
| 795 |
remap_freqcounting_preds_after_dup (vec<ipa_freqcounting_predicate, va_gc> *v, |
| 796 |
clause_t possible_truths) |
| 797 |
{ |
| 798 |
if (vec_safe_length (v) == 0) |
| 799 |
return NULL; |
| 800 |
|
| 801 |
vec<ipa_freqcounting_predicate, va_gc> *res = v->copy (); |
| 802 |
int len = res->length(); |
| 803 |
for (int i = len - 1; i >= 0; i--) |
| 804 |
{ |
| 805 |
ipa_predicate new_predicate |
| 806 |
= (*res)[i].predicate->remap_after_duplication (possible_truths); |
| 807 |
/* We do not want to free previous predicate; it is used by node |
| 808 |
origin. */ |
| 809 |
(*res)[i].predicate = NULL; |
| 810 |
set_hint_predicate (&(*res)[i].predicate, new_predicate); |
| 811 |
|
| 812 |
if (!(*res)[i].predicate) |
| 813 |
res->unordered_remove (i); |
| 814 |
} |
| 815 |
|
| 816 |
return res; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
/* Hook that is called by cgraph.cc when a node is duplicated. */ |
| 821 |
void |
| 822 |
ipa_fn_summary_t::duplicate (cgraph_node *src, |
| 823 |
cgraph_node *dst, |
| 824 |
ipa_fn_summary *src_info, |
| 825 |
ipa_fn_summary *info) |
| 826 |
{ |
| 827 |
new (info) ipa_fn_summary (*src_info); |
| 828 |
/* TODO: as an optimization, we may avoid copying conditions |
| 829 |
that are known to be false or true. */ |
| 830 |
info->conds = vec_safe_copy (info->conds); |
| 831 |
|
| 832 |
clone_info *cinfo = clone_info::get (dst); |
| 833 |
/* When there are any replacements in the function body, see if we can figure |
| 834 |
out that something was optimized out. */ |
| 835 |
if (ipa_node_params_sum && cinfo && cinfo->tree_map) |
| 836 |
{ |
| 837 |
/* Use SRC parm info since it may not be copied yet. */ |
| 838 |
ipa_node_params *parms_info = ipa_node_params_sum->get (src); |
| 839 |
ipa_auto_call_arg_values avals; |
| 840 |
int count = ipa_get_param_count (parms_info); |
| 841 |
int i, j; |
| 842 |
clause_t possible_truths; |
| 843 |
ipa_predicate true_pred = true; |
| 844 |
size_time_entry *e; |
| 845 |
int optimized_out_size = 0; |
| 846 |
bool inlined_to_p = false; |
| 847 |
struct cgraph_edge *edge, *next; |
| 848 |
|
| 849 |
info->size_time_table.release (); |
| 850 |
avals.m_known_vals.safe_grow_cleared (count, true); |
| 851 |
for (i = 0; i < count; i++) |
| 852 |
{ |
| 853 |
struct ipa_replace_map *r; |
| 854 |
|
| 855 |
for (j = 0; vec_safe_iterate (cinfo->tree_map, j, &r); j++) |
| 856 |
{ |
| 857 |
if (r->parm_num == i) |
| 858 |
{ |
| 859 |
avals.m_known_vals[i] = r->new_tree; |
| 860 |
break; |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
evaluate_conditions_for_known_args (dst, false, |
| 865 |
&avals, |
| 866 |
&possible_truths, |
| 867 |
/* We are going to specialize, |
| 868 |
so ignore nonspec truths. */ |
| 869 |
NULL, |
| 870 |
NULL); |
| 871 |
|
| 872 |
info->account_size_time (0, 0, true_pred, true_pred); |
| 873 |
|
| 874 |
/* Remap size_time vectors. |
| 875 |
Simplify the predicate by pruning out alternatives that are known |
| 876 |
to be false. |
| 877 |
TODO: as on optimization, we can also eliminate conditions known |
| 878 |
to be true. */ |
| 879 |
for (i = 0; src_info->size_time_table.iterate (i, &e); i++) |
| 880 |
{ |
| 881 |
ipa_predicate new_exec_pred; |
| 882 |
ipa_predicate new_nonconst_pred; |
| 883 |
new_exec_pred = e->exec_predicate.remap_after_duplication |
| 884 |
(possible_truths); |
| 885 |
new_nonconst_pred = e->nonconst_predicate.remap_after_duplication |
| 886 |
(possible_truths); |
| 887 |
if (new_exec_pred == false || new_nonconst_pred == false) |
| 888 |
optimized_out_size += e->size; |
| 889 |
else |
| 890 |
info->account_size_time (e->size, e->time, new_exec_pred, |
| 891 |
new_nonconst_pred); |
| 892 |
} |
| 893 |
|
| 894 |
/* Remap edge predicates with the same simplification as above. |
| 895 |
Also copy constantness arrays. */ |
| 896 |
for (edge = dst->callees; edge; edge = next) |
| 897 |
{ |
| 898 |
ipa_predicate new_predicate; |
| 899 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 900 |
next = edge->next_callee; |
| 901 |
|
| 902 |
if (!edge->inline_failed) |
| 903 |
inlined_to_p = true; |
| 904 |
if (!es->predicate) |
| 905 |
continue; |
| 906 |
new_predicate = es->predicate->remap_after_duplication |
| 907 |
(possible_truths); |
| 908 |
if (new_predicate == false && *es->predicate != false) |
| 909 |
optimized_out_size += es->call_stmt_size * ipa_fn_summary::size_scale; |
| 910 |
edge_set_predicate (edge, &new_predicate); |
| 911 |
} |
| 912 |
|
| 913 |
/* Remap indirect edge predicates with the same simplification as above. |
| 914 |
Also copy constantness arrays. */ |
| 915 |
for (edge = dst->indirect_calls; edge; edge = next) |
| 916 |
{ |
| 917 |
ipa_predicate new_predicate; |
| 918 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 919 |
next = edge->next_callee; |
| 920 |
|
| 921 |
gcc_checking_assert (edge->inline_failed); |
| 922 |
if (!es->predicate) |
| 923 |
continue; |
| 924 |
new_predicate = es->predicate->remap_after_duplication |
| 925 |
(possible_truths); |
| 926 |
if (new_predicate == false && *es->predicate != false) |
| 927 |
optimized_out_size |
| 928 |
+= es->call_stmt_size * ipa_fn_summary::size_scale; |
| 929 |
edge_set_predicate (edge, &new_predicate); |
| 930 |
} |
| 931 |
info->loop_iterations |
| 932 |
= remap_freqcounting_preds_after_dup (info->loop_iterations, |
| 933 |
possible_truths); |
| 934 |
info->loop_strides |
| 935 |
= remap_freqcounting_preds_after_dup (info->loop_strides, |
| 936 |
possible_truths); |
| 937 |
if (info->builtin_constant_p_parms.length()) |
| 938 |
{ |
| 939 |
vec <int, va_heap, vl_ptr> parms = info->builtin_constant_p_parms; |
| 940 |
int ip; |
| 941 |
info->builtin_constant_p_parms = vNULL; |
| 942 |
for (i = 0; parms.iterate (i, &ip); i++) |
| 943 |
if (!avals.m_known_vals[ip]) |
| 944 |
info->builtin_constant_p_parms.safe_push (ip); |
| 945 |
} |
| 946 |
|
| 947 |
/* If inliner or someone after inliner will ever start producing |
| 948 |
non-trivial clones, we will get trouble with lack of information |
| 949 |
about updating self sizes, because size vectors already contains |
| 950 |
sizes of the callees. */ |
| 951 |
gcc_assert (!inlined_to_p || !optimized_out_size); |
| 952 |
} |
| 953 |
else |
| 954 |
{ |
| 955 |
info->size_time_table = src_info->size_time_table.copy (); |
| 956 |
info->loop_iterations = vec_safe_copy (src_info->loop_iterations); |
| 957 |
info->loop_strides = vec_safe_copy (info->loop_strides); |
| 958 |
|
| 959 |
info->builtin_constant_p_parms |
| 960 |
= info->builtin_constant_p_parms.copy (); |
| 961 |
|
| 962 |
ipa_freqcounting_predicate *f; |
| 963 |
for (int i = 0; vec_safe_iterate (info->loop_iterations, i, &f); i++) |
| 964 |
{ |
| 965 |
ipa_predicate p = *f->predicate; |
| 966 |
f->predicate = NULL; |
| 967 |
set_hint_predicate (&f->predicate, p); |
| 968 |
} |
| 969 |
for (int i = 0; vec_safe_iterate (info->loop_strides, i, &f); i++) |
| 970 |
{ |
| 971 |
ipa_predicate p = *f->predicate; |
| 972 |
f->predicate = NULL; |
| 973 |
set_hint_predicate (&f->predicate, p); |
| 974 |
} |
| 975 |
} |
| 976 |
if (!dst->inlined_to) |
| 977 |
ipa_update_overall_fn_summary (dst); |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
/* Hook that is called by cgraph.cc when a node is duplicated. */ |
| 982 |
|
| 983 |
void |
| 984 |
ipa_call_summary_t::duplicate (struct cgraph_edge *src, |
| 985 |
struct cgraph_edge *dst, |
| 986 |
class ipa_call_summary *srcinfo, |
| 987 |
class ipa_call_summary *info) |
| 988 |
{ |
| 989 |
new (info) ipa_call_summary (*srcinfo); |
| 990 |
info->predicate = NULL; |
| 991 |
edge_set_predicate (dst, srcinfo->predicate); |
| 992 |
info->param = srcinfo->param.copy (); |
| 993 |
if (!dst->indirect_unknown_callee && src->indirect_unknown_callee) |
| 994 |
{ |
| 995 |
info->call_stmt_size -= (eni_size_weights.indirect_call_cost |
| 996 |
- eni_size_weights.call_cost); |
| 997 |
info->call_stmt_time -= (eni_time_weights.indirect_call_cost |
| 998 |
- eni_time_weights.call_cost); |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* Dump edge summaries associated to NODE and recursively to all clones. |
| 1003 |
Indent by INDENT. */ |
| 1004 |
|
| 1005 |
static void |
| 1006 |
dump_ipa_call_summary (FILE *f, int indent, struct cgraph_node *node, |
| 1007 |
class ipa_fn_summary *info) |
| 1008 |
{ |
| 1009 |
struct cgraph_edge *edge; |
| 1010 |
for (edge = node->callees; edge; edge = edge->next_callee) |
| 1011 |
{ |
| 1012 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 1013 |
struct cgraph_node *callee = edge->callee->ultimate_alias_target (); |
| 1014 |
int i; |
| 1015 |
|
| 1016 |
fprintf (f, |
| 1017 |
"%*s%s %s\n%*s freq:%4.2f", |
| 1018 |
indent, "", callee->dump_name (), |
| 1019 |
!edge->inline_failed |
| 1020 |
? "inlined" : cgraph_inline_failed_string (edge-> inline_failed), |
| 1021 |
indent, "", edge->sreal_frequency ().to_double ()); |
| 1022 |
|
| 1023 |
if (cross_module_call_p (edge)) |
| 1024 |
fprintf (f, " cross module"); |
| 1025 |
|
| 1026 |
if (es) |
| 1027 |
fprintf (f, " loop depth:%2i size:%2i time: %2i", |
| 1028 |
es->loop_depth, es->call_stmt_size, es->call_stmt_time); |
| 1029 |
|
| 1030 |
ipa_fn_summary *s = ipa_fn_summaries->get (callee); |
| 1031 |
ipa_size_summary *ss = ipa_size_summaries->get (callee); |
| 1032 |
if (s != NULL) |
| 1033 |
fprintf (f, " callee size:%2i stack:%2i", |
| 1034 |
(int) (ss->size / ipa_fn_summary::size_scale), |
| 1035 |
(int) s->estimated_stack_size); |
| 1036 |
|
| 1037 |
if (es && es->predicate) |
| 1038 |
{ |
| 1039 |
fprintf (f, " predicate: "); |
| 1040 |
es->predicate->dump (f, info->conds); |
| 1041 |
} |
| 1042 |
else |
| 1043 |
fprintf (f, "\n"); |
| 1044 |
if (es && es->param.exists ()) |
| 1045 |
for (i = 0; i < (int) es->param.length (); i++) |
| 1046 |
{ |
| 1047 |
int prob = es->param[i].change_prob; |
| 1048 |
|
| 1049 |
if (!prob) |
| 1050 |
fprintf (f, "%*s op%i is compile time invariant\n", |
| 1051 |
indent + 2, "", i); |
| 1052 |
else if (prob != REG_BR_PROB_BASE) |
| 1053 |
fprintf (f, "%*s op%i change %f%% of time\n", indent + 2, "", i, |
| 1054 |
prob * 100.0 / REG_BR_PROB_BASE); |
| 1055 |
if (es->param[i].points_to_local_or_readonly_memory) |
| 1056 |
fprintf (f, "%*s op%i points to local or readonly memory\n", |
| 1057 |
indent + 2, "", i); |
| 1058 |
if (es->param[i].points_to_possible_sra_candidate) |
| 1059 |
fprintf (f, "%*s op%i points to possible sra candidate\n", |
| 1060 |
indent + 2, "", i); |
| 1061 |
} |
| 1062 |
if (!edge->inline_failed) |
| 1063 |
{ |
| 1064 |
ipa_size_summary *ss = ipa_size_summaries->get (callee); |
| 1065 |
fprintf (f, "%*sStack frame offset %i, callee self size %i\n", |
| 1066 |
indent + 2, "", |
| 1067 |
(int) ipa_get_stack_frame_offset (callee), |
| 1068 |
(int) ss->estimated_self_stack_size); |
| 1069 |
dump_ipa_call_summary (f, indent + 2, callee, info); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
for (edge = node->indirect_calls; edge; edge = edge->next_callee) |
| 1073 |
{ |
| 1074 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 1075 |
fprintf (f, "%*sindirect call loop depth:%2i freq:%4.2f size:%2i" |
| 1076 |
" time: %2i", |
| 1077 |
indent, "", |
| 1078 |
es->loop_depth, |
| 1079 |
edge->sreal_frequency ().to_double (), es->call_stmt_size, |
| 1080 |
es->call_stmt_time); |
| 1081 |
if (es->predicate) |
| 1082 |
{ |
| 1083 |
fprintf (f, "predicate: "); |
| 1084 |
es->predicate->dump (f, info->conds); |
| 1085 |
} |
| 1086 |
else |
| 1087 |
fprintf (f, "\n"); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
void |
| 1093 |
ipa_dump_fn_summary (FILE *f, struct cgraph_node *node) |
| 1094 |
{ |
| 1095 |
if (node->definition) |
| 1096 |
{ |
| 1097 |
class ipa_fn_summary *s = ipa_fn_summaries->get (node); |
| 1098 |
class ipa_size_summary *ss = ipa_size_summaries->get (node); |
| 1099 |
if (s != NULL) |
| 1100 |
{ |
| 1101 |
size_time_entry *e; |
| 1102 |
int i; |
| 1103 |
fprintf (f, "IPA function summary for %s", node->dump_name ()); |
| 1104 |
if (DECL_DISREGARD_INLINE_LIMITS (node->decl)) |
| 1105 |
fprintf (f, " always_inline"); |
| 1106 |
if (s->inlinable) |
| 1107 |
fprintf (f, " inlinable"); |
| 1108 |
if (s->fp_expressions) |
| 1109 |
fprintf (f, " fp_expression"); |
| 1110 |
if (s->builtin_constant_p_parms.length ()) |
| 1111 |
{ |
| 1112 |
fprintf (f, " builtin_constant_p_parms"); |
| 1113 |
for (unsigned int i = 0; |
| 1114 |
i < s->builtin_constant_p_parms.length (); i++) |
| 1115 |
fprintf (f, " %i", s->builtin_constant_p_parms[i]); |
| 1116 |
} |
| 1117 |
fprintf (f, "\n global time: %f\n", s->time.to_double ()); |
| 1118 |
fprintf (f, " self size: %i\n", ss->self_size); |
| 1119 |
fprintf (f, " global size: %i\n", ss->size); |
| 1120 |
fprintf (f, " min size: %i\n", s->min_size); |
| 1121 |
fprintf (f, " self stack: %i\n", |
| 1122 |
(int) ss->estimated_self_stack_size); |
| 1123 |
fprintf (f, " global stack: %i\n", (int) s->estimated_stack_size); |
| 1124 |
if (s->growth) |
| 1125 |
fprintf (f, " estimated growth:%i\n", (int) s->growth); |
| 1126 |
if (s->scc_no) |
| 1127 |
fprintf (f, " In SCC: %i\n", (int) s->scc_no); |
| 1128 |
for (i = 0; s->size_time_table.iterate (i, &e); i++) |
| 1129 |
{ |
| 1130 |
fprintf (f, " size:%f, time:%f", |
| 1131 |
(double) e->size / ipa_fn_summary::size_scale, |
| 1132 |
e->time.to_double ()); |
| 1133 |
if (e->exec_predicate != true) |
| 1134 |
{ |
| 1135 |
fprintf (f, ", executed if:"); |
| 1136 |
e->exec_predicate.dump (f, s->conds, 0); |
| 1137 |
} |
| 1138 |
if (e->exec_predicate != e->nonconst_predicate) |
| 1139 |
{ |
| 1140 |
fprintf (f, ", nonconst if:"); |
| 1141 |
e->nonconst_predicate.dump (f, s->conds, 0); |
| 1142 |
} |
| 1143 |
fprintf (f, "\n"); |
| 1144 |
} |
| 1145 |
ipa_freqcounting_predicate *fcp; |
| 1146 |
bool first_fcp = true; |
| 1147 |
for (int i = 0; vec_safe_iterate (s->loop_iterations, i, &fcp); i++) |
| 1148 |
{ |
| 1149 |
if (first_fcp) |
| 1150 |
{ |
| 1151 |
fprintf (f, " loop iterations:"); |
| 1152 |
first_fcp = false; |
| 1153 |
} |
| 1154 |
fprintf (f, " %3.2f for ", fcp->freq.to_double ()); |
| 1155 |
fcp->predicate->dump (f, s->conds); |
| 1156 |
} |
| 1157 |
first_fcp = true; |
| 1158 |
for (int i = 0; vec_safe_iterate (s->loop_strides, i, &fcp); i++) |
| 1159 |
{ |
| 1160 |
if (first_fcp) |
| 1161 |
{ |
| 1162 |
fprintf (f, " loop strides:"); |
| 1163 |
first_fcp = false; |
| 1164 |
} |
| 1165 |
fprintf (f, " %3.2f for :", fcp->freq.to_double ()); |
| 1166 |
fcp->predicate->dump (f, s->conds); |
| 1167 |
} |
| 1168 |
fprintf (f, " calls:\n"); |
| 1169 |
dump_ipa_call_summary (f, 4, node, s); |
| 1170 |
fprintf (f, "\n"); |
| 1171 |
if (s->target_info) |
| 1172 |
fprintf (f, " target_info: %x\n", s->target_info); |
| 1173 |
} |
| 1174 |
else |
| 1175 |
fprintf (f, "IPA summary for %s is missing.\n", node->dump_name ()); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
DEBUG_FUNCTION void |
| 1180 |
ipa_debug_fn_summary (struct cgraph_node *node) |
| 1181 |
{ |
| 1182 |
ipa_dump_fn_summary (stderr, node); |
| 1183 |
} |
| 1184 |
|
| 1185 |
void |
| 1186 |
ipa_dump_fn_summaries (FILE *f) |
| 1187 |
{ |
| 1188 |
struct cgraph_node *node; |
| 1189 |
|
| 1190 |
FOR_EACH_DEFINED_FUNCTION (node) |
| 1191 |
if (!node->inlined_to) |
| 1192 |
ipa_dump_fn_summary (f, node); |
| 1193 |
} |
| 1194 |
|
| 1195 |
/* Callback of walk_aliased_vdefs. Flags that it has been invoked to the |
| 1196 |
boolean variable pointed to by DATA. */ |
| 1197 |
|
| 1198 |
static bool |
| 1199 |
mark_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef ATTRIBUTE_UNUSED, |
| 1200 |
void *data) |
| 1201 |
{ |
| 1202 |
bool *b = (bool *) data; |
| 1203 |
*b = true; |
| 1204 |
return true; |
| 1205 |
} |
| 1206 |
|
| 1207 |
/* If OP refers to value of function parameter, return the corresponding |
| 1208 |
parameter. If non-NULL, the size of the memory load (or the SSA_NAME of the |
| 1209 |
PARM_DECL) will be stored to *SIZE_P in that case too. */ |
| 1210 |
|
| 1211 |
static tree |
| 1212 |
unmodified_parm_1 (ipa_func_body_info *fbi, gimple *stmt, tree op, |
| 1213 |
poly_int64 *size_p) |
| 1214 |
{ |
| 1215 |
/* SSA_NAME referring to parm default def? */ |
| 1216 |
if (TREE_CODE (op) == SSA_NAME |
| 1217 |
&& SSA_NAME_IS_DEFAULT_DEF (op) |
| 1218 |
&& TREE_CODE (SSA_NAME_VAR (op)) == PARM_DECL) |
| 1219 |
{ |
| 1220 |
if (size_p) |
| 1221 |
*size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op))); |
| 1222 |
return SSA_NAME_VAR (op); |
| 1223 |
} |
| 1224 |
/* Non-SSA parm reference? */ |
| 1225 |
if (TREE_CODE (op) == PARM_DECL |
| 1226 |
&& fbi->aa_walk_budget > 0) |
| 1227 |
{ |
| 1228 |
bool modified = false; |
| 1229 |
|
| 1230 |
ao_ref refd; |
| 1231 |
ao_ref_init (&refd, op); |
| 1232 |
int walked = walk_aliased_vdefs (&refd, gimple_vuse (stmt), |
| 1233 |
mark_modified, &modified, NULL, NULL, |
| 1234 |
fbi->aa_walk_budget); |
| 1235 |
if (walked < 0) |
| 1236 |
{ |
| 1237 |
fbi->aa_walk_budget = 0; |
| 1238 |
return NULL_TREE; |
| 1239 |
} |
| 1240 |
fbi->aa_walk_budget -= walked; |
| 1241 |
if (!modified) |
| 1242 |
{ |
| 1243 |
if (size_p) |
| 1244 |
*size_p = tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (op))); |
| 1245 |
return op; |
| 1246 |
} |
| 1247 |
} |
| 1248 |
return NULL_TREE; |
| 1249 |
} |
| 1250 |
|
| 1251 |
/* If OP refers to value of function parameter, return the corresponding |
| 1252 |
parameter. Also traverse chains of SSA register assignments. If non-NULL, |
| 1253 |
the size of the memory load (or the SSA_NAME of the PARM_DECL) will be |
| 1254 |
stored to *SIZE_P in that case too. */ |
| 1255 |
|
| 1256 |
static tree |
| 1257 |
unmodified_parm (ipa_func_body_info *fbi, gimple *stmt, tree op, |
| 1258 |
poly_int64 *size_p) |
| 1259 |
{ |
| 1260 |
tree res = unmodified_parm_1 (fbi, stmt, op, size_p); |
| 1261 |
if (res) |
| 1262 |
return res; |
| 1263 |
|
| 1264 |
if (TREE_CODE (op) == SSA_NAME |
| 1265 |
&& !SSA_NAME_IS_DEFAULT_DEF (op) |
| 1266 |
&& gimple_assign_single_p (SSA_NAME_DEF_STMT (op))) |
| 1267 |
return unmodified_parm (fbi, SSA_NAME_DEF_STMT (op), |
| 1268 |
gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op)), |
| 1269 |
size_p); |
| 1270 |
return NULL_TREE; |
| 1271 |
} |
| 1272 |
|
| 1273 |
/* If OP refers to a value of a function parameter or value loaded from an |
| 1274 |
aggregate passed to a parameter (either by value or reference), return TRUE |
| 1275 |
and store the number of the parameter to *INDEX_P, the access size into |
| 1276 |
*SIZE_P, and information whether and how it has been loaded from an |
| 1277 |
aggregate into *AGGPOS. INFO describes the function parameters, STMT is the |
| 1278 |
statement in which OP is used or loaded. */ |
| 1279 |
|
| 1280 |
static bool |
| 1281 |
unmodified_parm_or_parm_agg_item (struct ipa_func_body_info *fbi, |
| 1282 |
gimple *stmt, tree op, int *index_p, |
| 1283 |
poly_int64 *size_p, |
| 1284 |
struct agg_position_info *aggpos) |
| 1285 |
{ |
| 1286 |
tree res = unmodified_parm_1 (fbi, stmt, op, size_p); |
| 1287 |
|
| 1288 |
gcc_checking_assert (aggpos); |
| 1289 |
if (res) |
| 1290 |
{ |
| 1291 |
*index_p = ipa_get_param_decl_index (fbi->info, res); |
| 1292 |
if (*index_p < 0) |
| 1293 |
return false; |
| 1294 |
aggpos->agg_contents = false; |
| 1295 |
aggpos->by_ref = false; |
| 1296 |
return true; |
| 1297 |
} |
| 1298 |
|
| 1299 |
if (TREE_CODE (op) == SSA_NAME) |
| 1300 |
{ |
| 1301 |
if (SSA_NAME_IS_DEFAULT_DEF (op) |
| 1302 |
|| !gimple_assign_single_p (SSA_NAME_DEF_STMT (op))) |
| 1303 |
return false; |
| 1304 |
stmt = SSA_NAME_DEF_STMT (op); |
| 1305 |
op = gimple_assign_rhs1 (stmt); |
| 1306 |
if (!REFERENCE_CLASS_P (op)) |
| 1307 |
return unmodified_parm_or_parm_agg_item (fbi, stmt, op, index_p, size_p, |
| 1308 |
aggpos); |
| 1309 |
} |
| 1310 |
|
| 1311 |
aggpos->agg_contents = true; |
| 1312 |
return ipa_load_from_parm_agg (fbi, fbi->info->descriptors, |
| 1313 |
stmt, op, index_p, &aggpos->offset, |
| 1314 |
size_p, &aggpos->by_ref); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/* If stmt is simple load or store of value pointed to by a function parmaeter, |
| 1318 |
return its index. */ |
| 1319 |
|
| 1320 |
static int |
| 1321 |
load_or_store_of_ptr_parameter (ipa_func_body_info *fbi, gimple *stmt) |
| 1322 |
{ |
| 1323 |
if (!optimize) |
| 1324 |
return -1; |
| 1325 |
gassign *assign = dyn_cast <gassign *> (stmt); |
| 1326 |
if (!assign) |
| 1327 |
return -1; |
| 1328 |
tree param; |
| 1329 |
if (gimple_assign_load_p (stmt)) |
| 1330 |
param = gimple_assign_rhs1 (stmt); |
| 1331 |
else if (gimple_store_p (stmt)) |
| 1332 |
param = gimple_assign_lhs (stmt); |
| 1333 |
else |
| 1334 |
return -1; |
| 1335 |
tree base = get_base_address (param); |
| 1336 |
if (TREE_CODE (base) != MEM_REF |
| 1337 |
|| TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME |
| 1338 |
|| !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))) |
| 1339 |
return -1; |
| 1340 |
tree p = SSA_NAME_VAR (TREE_OPERAND (base, 0)); |
| 1341 |
if (TREE_CODE (p) != PARM_DECL) |
| 1342 |
return -1; |
| 1343 |
return ipa_get_param_decl_index (fbi->info, p); |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* See if statement might disappear after inlining. |
| 1347 |
0 - means not eliminated |
| 1348 |
1 - half of statements goes away |
| 1349 |
2 - for sure it is eliminated. |
| 1350 |
We are not terribly sophisticated, basically looking for simple abstraction |
| 1351 |
penalty wrappers. */ |
| 1352 |
|
| 1353 |
static int |
| 1354 |
eliminated_by_inlining_prob (ipa_func_body_info *fbi, gimple *stmt) |
| 1355 |
{ |
| 1356 |
enum gimple_code code = gimple_code (stmt); |
| 1357 |
enum tree_code rhs_code; |
| 1358 |
|
| 1359 |
if (!optimize) |
| 1360 |
return 0; |
| 1361 |
|
| 1362 |
switch (code) |
| 1363 |
{ |
| 1364 |
case GIMPLE_RETURN: |
| 1365 |
return 2; |
| 1366 |
case GIMPLE_ASSIGN: |
| 1367 |
if (gimple_num_ops (stmt) != 2) |
| 1368 |
return 0; |
| 1369 |
|
| 1370 |
rhs_code = gimple_assign_rhs_code (stmt); |
| 1371 |
|
| 1372 |
/* Casts of parameters, loads from parameters passed by reference |
| 1373 |
and stores to return value or parameters are often free after |
| 1374 |
inlining due to SRA and further combining. |
| 1375 |
Assume that half of statements goes away. */ |
| 1376 |
if (CONVERT_EXPR_CODE_P (rhs_code) |
| 1377 |
|| rhs_code == VIEW_CONVERT_EXPR |
| 1378 |
|| rhs_code == ADDR_EXPR |
| 1379 |
|| gimple_assign_rhs_class (stmt) == GIMPLE_SINGLE_RHS) |
| 1380 |
{ |
| 1381 |
tree rhs = gimple_assign_rhs1 (stmt); |
| 1382 |
tree lhs = gimple_assign_lhs (stmt); |
| 1383 |
tree inner_rhs = get_base_address (rhs); |
| 1384 |
tree inner_lhs = get_base_address (lhs); |
| 1385 |
bool rhs_free = false; |
| 1386 |
bool lhs_free = false; |
| 1387 |
|
| 1388 |
if (!inner_rhs) |
| 1389 |
inner_rhs = rhs; |
| 1390 |
if (!inner_lhs) |
| 1391 |
inner_lhs = lhs; |
| 1392 |
|
| 1393 |
/* Reads of parameter are expected to be free. */ |
| 1394 |
if (unmodified_parm (fbi, stmt, inner_rhs, NULL)) |
| 1395 |
rhs_free = true; |
| 1396 |
/* Match expressions of form &this->field. Those will most likely |
| 1397 |
combine with something upstream after inlining. */ |
| 1398 |
else if (TREE_CODE (inner_rhs) == ADDR_EXPR) |
| 1399 |
{ |
| 1400 |
tree op = get_base_address (TREE_OPERAND (inner_rhs, 0)); |
| 1401 |
if (TREE_CODE (op) == PARM_DECL) |
| 1402 |
rhs_free = true; |
| 1403 |
else if (TREE_CODE (op) == MEM_REF |
| 1404 |
&& unmodified_parm (fbi, stmt, TREE_OPERAND (op, 0), |
| 1405 |
NULL)) |
| 1406 |
rhs_free = true; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* When parameter is not SSA register because its address is taken |
| 1410 |
and it is just copied into one, the statement will be completely |
| 1411 |
free after inlining (we will copy propagate backward). */ |
| 1412 |
if (rhs_free && is_gimple_reg (lhs)) |
| 1413 |
return 2; |
| 1414 |
|
| 1415 |
/* Reads of parameters passed by reference |
| 1416 |
expected to be free (i.e. optimized out after inlining). */ |
| 1417 |
if (TREE_CODE (inner_rhs) == MEM_REF |
| 1418 |
&& unmodified_parm (fbi, stmt, TREE_OPERAND (inner_rhs, 0), NULL)) |
| 1419 |
rhs_free = true; |
| 1420 |
|
| 1421 |
/* Copying parameter passed by reference into gimple register is |
| 1422 |
probably also going to copy propagate, but we can't be quite |
| 1423 |
sure. */ |
| 1424 |
if (rhs_free && is_gimple_reg (lhs)) |
| 1425 |
lhs_free = true; |
| 1426 |
|
| 1427 |
/* Writes to parameters, parameters passed by value and return value |
| 1428 |
(either directly or passed via invisible reference) are free. |
| 1429 |
|
| 1430 |
TODO: We ought to handle testcase like |
| 1431 |
struct a {int a,b;}; |
| 1432 |
struct a |
| 1433 |
returnstruct (void) |
| 1434 |
{ |
| 1435 |
struct a a ={1,2}; |
| 1436 |
return a; |
| 1437 |
} |
| 1438 |
|
| 1439 |
This translate into: |
| 1440 |
|
| 1441 |
returnstruct () |
| 1442 |
{ |
| 1443 |
int a$b; |
| 1444 |
int a$a; |
| 1445 |
struct a a; |
| 1446 |
struct a D.2739; |
| 1447 |
|
| 1448 |
<bb 2>: |
| 1449 |
D.2739.a = 1; |
| 1450 |
D.2739.b = 2; |
| 1451 |
return D.2739; |
| 1452 |
|
| 1453 |
} |
| 1454 |
For that we either need to copy ipa-split logic detecting writes |
| 1455 |
to return value. */ |
| 1456 |
if (TREE_CODE (inner_lhs) == PARM_DECL |
| 1457 |
|| TREE_CODE (inner_lhs) == RESULT_DECL |
| 1458 |
|| (TREE_CODE (inner_lhs) == MEM_REF |
| 1459 |
&& (unmodified_parm (fbi, stmt, TREE_OPERAND (inner_lhs, 0), |
| 1460 |
NULL) |
| 1461 |
|| (TREE_CODE (TREE_OPERAND (inner_lhs, 0)) == SSA_NAME |
| 1462 |
&& SSA_NAME_VAR (TREE_OPERAND (inner_lhs, 0)) |
| 1463 |
&& TREE_CODE (SSA_NAME_VAR (TREE_OPERAND |
| 1464 |
(inner_lhs, |
| 1465 |
0))) == RESULT_DECL)))) |
| 1466 |
lhs_free = true; |
| 1467 |
if (lhs_free |
| 1468 |
&& (is_gimple_reg (rhs) || is_gimple_min_invariant (rhs))) |
| 1469 |
rhs_free = true; |
| 1470 |
if (lhs_free && rhs_free) |
| 1471 |
return 1; |
| 1472 |
} |
| 1473 |
return 0; |
| 1474 |
default: |
| 1475 |
return 0; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* Analyze EXPR if it represents a series of simple operations performed on |
| 1480 |
a function parameter and return true if so. FBI, STMT, EXPR, INDEX_P and |
| 1481 |
AGGPOS have the same meaning like in unmodified_parm_or_parm_agg_item. |
| 1482 |
Type of the parameter or load from an aggregate via the parameter is |
| 1483 |
stored in *TYPE_P. Operations on the parameter are recorded to |
| 1484 |
PARAM_OPS_P if it is not NULL. */ |
| 1485 |
|
| 1486 |
static bool |
| 1487 |
decompose_param_expr (struct ipa_func_body_info *fbi, |
| 1488 |
gimple *stmt, tree expr, |
| 1489 |
int *index_p, tree *type_p, |
| 1490 |
struct agg_position_info *aggpos, |
| 1491 |
expr_eval_ops *param_ops_p = NULL) |
| 1492 |
{ |
| 1493 |
int op_limit = opt_for_fn (fbi->node->decl, param_ipa_max_param_expr_ops); |
| 1494 |
int op_count = 0; |
| 1495 |
|
| 1496 |
if (param_ops_p) |
| 1497 |
*param_ops_p = NULL; |
| 1498 |
|
| 1499 |
while (true) |
| 1500 |
{ |
| 1501 |
expr_eval_op eval_op; |
| 1502 |
unsigned rhs_count; |
| 1503 |
unsigned cst_count = 0; |
| 1504 |
|
| 1505 |
if (unmodified_parm_or_parm_agg_item (fbi, stmt, expr, index_p, NULL, |
| 1506 |
aggpos)) |
| 1507 |
{ |
| 1508 |
tree type = TREE_TYPE (expr); |
| 1509 |
|
| 1510 |
if (aggpos->agg_contents) |
| 1511 |
{ |
| 1512 |
/* Stop if containing bit-field. */ |
| 1513 |
if (TREE_CODE (expr) == BIT_FIELD_REF |
| 1514 |
|| contains_bitfld_component_ref_p (expr)) |
| 1515 |
break; |
| 1516 |
} |
| 1517 |
|
| 1518 |
*type_p = type; |
| 1519 |
return true; |
| 1520 |
} |
| 1521 |
|
| 1522 |
if (TREE_CODE (expr) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (expr)) |
| 1523 |
break; |
| 1524 |
stmt = SSA_NAME_DEF_STMT (expr); |
| 1525 |
|
| 1526 |
if (gcall *call = dyn_cast <gcall *> (stmt)) |
| 1527 |
{ |
| 1528 |
int flags = gimple_call_return_flags (call); |
| 1529 |
if (!(flags & ERF_RETURNS_ARG)) |
| 1530 |
goto fail; |
| 1531 |
int arg = flags & ERF_RETURN_ARG_MASK; |
| 1532 |
if (arg >= (int)gimple_call_num_args (call)) |
| 1533 |
goto fail; |
| 1534 |
expr = gimple_call_arg (stmt, arg); |
| 1535 |
continue; |
| 1536 |
} |
| 1537 |
|
| 1538 |
if (!is_gimple_assign (stmt = SSA_NAME_DEF_STMT (expr))) |
| 1539 |
break; |
| 1540 |
|
| 1541 |
switch (gimple_assign_rhs_class (stmt)) |
| 1542 |
{ |
| 1543 |
case GIMPLE_SINGLE_RHS: |
| 1544 |
expr = gimple_assign_rhs1 (stmt); |
| 1545 |
continue; |
| 1546 |
|
| 1547 |
case GIMPLE_UNARY_RHS: |
| 1548 |
rhs_count = 1; |
| 1549 |
break; |
| 1550 |
|
| 1551 |
case GIMPLE_BINARY_RHS: |
| 1552 |
rhs_count = 2; |
| 1553 |
break; |
| 1554 |
|
| 1555 |
case GIMPLE_TERNARY_RHS: |
| 1556 |
rhs_count = 3; |
| 1557 |
break; |
| 1558 |
|
| 1559 |
default: |
| 1560 |
goto fail; |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* Stop if expression is too complex. */ |
| 1564 |
if (op_count++ == op_limit) |
| 1565 |
break; |
| 1566 |
|
| 1567 |
if (param_ops_p) |
| 1568 |
{ |
| 1569 |
eval_op.code = gimple_assign_rhs_code (stmt); |
| 1570 |
eval_op.type = TREE_TYPE (gimple_assign_lhs (stmt)); |
| 1571 |
eval_op.val[0] = NULL_TREE; |
| 1572 |
eval_op.val[1] = NULL_TREE; |
| 1573 |
} |
| 1574 |
|
| 1575 |
expr = NULL_TREE; |
| 1576 |
for (unsigned i = 0; i < rhs_count; i++) |
| 1577 |
{ |
| 1578 |
tree op = gimple_op (stmt, i + 1); |
| 1579 |
|
| 1580 |
gcc_assert (op && !TYPE_P (op)); |
| 1581 |
if (is_gimple_ip_invariant (op)) |
| 1582 |
{ |
| 1583 |
if (++cst_count == rhs_count) |
| 1584 |
goto fail; |
| 1585 |
|
| 1586 |
eval_op.val[cst_count - 1] = op; |
| 1587 |
} |
| 1588 |
else if (!expr) |
| 1589 |
{ |
| 1590 |
/* Found a non-constant operand, and record its index in rhs |
| 1591 |
operands. */ |
| 1592 |
eval_op.index = i; |
| 1593 |
expr = op; |
| 1594 |
} |
| 1595 |
else |
| 1596 |
{ |
| 1597 |
/* Found more than one non-constant operands. */ |
| 1598 |
goto fail; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
|
| 1602 |
if (param_ops_p) |
| 1603 |
vec_safe_insert (*param_ops_p, 0, eval_op); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* Failed to decompose, free resource and return. */ |
| 1607 |
fail: |
| 1608 |
if (param_ops_p) |
| 1609 |
vec_free (*param_ops_p); |
| 1610 |
|
| 1611 |
return false; |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* Record to SUMMARY that PARM is used by builtin_constant_p. */ |
| 1615 |
|
| 1616 |
static void |
| 1617 |
add_builtin_constant_p_parm (class ipa_fn_summary *summary, int parm) |
| 1618 |
{ |
| 1619 |
int ip; |
| 1620 |
|
| 1621 |
/* Avoid duplicates. */ |
| 1622 |
for (unsigned int i = 0; |
| 1623 |
summary->builtin_constant_p_parms.iterate (i, &ip); i++) |
| 1624 |
if (ip == parm) |
| 1625 |
return; |
| 1626 |
summary->builtin_constant_p_parms.safe_push (parm); |
| 1627 |
} |
| 1628 |
|
| 1629 |
/* If BB ends by a conditional we can turn into predicates, attach corresponding |
| 1630 |
predicates to the CFG edges. */ |
| 1631 |
|
| 1632 |
static void |
| 1633 |
set_cond_stmt_execution_predicate (struct ipa_func_body_info *fbi, |
| 1634 |
class ipa_fn_summary *summary, |
| 1635 |
class ipa_node_params *params_summary, |
| 1636 |
basic_block bb) |
| 1637 |
{ |
| 1638 |
tree op, op2; |
| 1639 |
int index; |
| 1640 |
struct agg_position_info aggpos; |
| 1641 |
enum tree_code code, inverted_code; |
| 1642 |
edge e; |
| 1643 |
edge_iterator ei; |
| 1644 |
gimple *set_stmt; |
| 1645 |
tree param_type; |
| 1646 |
expr_eval_ops param_ops; |
| 1647 |
|
| 1648 |
gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb)); |
| 1649 |
if (!last) |
| 1650 |
return; |
| 1651 |
if (!is_gimple_ip_invariant (gimple_cond_rhs (last))) |
| 1652 |
return; |
| 1653 |
op = gimple_cond_lhs (last); |
| 1654 |
|
| 1655 |
if (decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos, |
| 1656 |
¶m_ops)) |
| 1657 |
{ |
| 1658 |
code = gimple_cond_code (last); |
| 1659 |
inverted_code = invert_tree_comparison (code, HONOR_NANS (op)); |
| 1660 |
|
| 1661 |
FOR_EACH_EDGE (e, ei, bb->succs) |
| 1662 |
{ |
| 1663 |
enum tree_code this_code = (e->flags & EDGE_TRUE_VALUE |
| 1664 |
? code : inverted_code); |
| 1665 |
/* invert_tree_comparison will return ERROR_MARK on FP |
| 1666 |
comparisons that are not EQ/NE instead of returning proper |
| 1667 |
unordered one. Be sure it is not confused with NON_CONSTANT. |
| 1668 |
|
| 1669 |
And if the edge's target is the final block of diamond CFG graph |
| 1670 |
of this conditional statement, we do not need to compute |
| 1671 |
predicate for the edge because the final block's predicate must |
| 1672 |
be at least as that of the first block of the statement. */ |
| 1673 |
if (this_code != ERROR_MARK |
| 1674 |
&& !dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) |
| 1675 |
{ |
| 1676 |
ipa_predicate p |
| 1677 |
= add_condition (summary, params_summary, index, |
| 1678 |
param_type, &aggpos, |
| 1679 |
this_code, gimple_cond_rhs (last), param_ops); |
| 1680 |
e->aux = edge_predicate_pool.allocate (); |
| 1681 |
*(ipa_predicate *) e->aux = p; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
vec_free (param_ops); |
| 1685 |
return; |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (TREE_CODE (op) != SSA_NAME) |
| 1689 |
return; |
| 1690 |
/* Special case |
| 1691 |
if (builtin_constant_p (op)) |
| 1692 |
constant_code |
| 1693 |
else |
| 1694 |
nonconstant_code. |
| 1695 |
Here we can predicate nonconstant_code. We can't |
| 1696 |
really handle constant_code since we have no predicate |
| 1697 |
for this and also the constant code is not known to be |
| 1698 |
optimized away when inliner doesn't see operand is constant. |
| 1699 |
Other optimizers might think otherwise. */ |
| 1700 |
if (gimple_cond_code (last) != NE_EXPR |
| 1701 |
|| !integer_zerop (gimple_cond_rhs (last))) |
| 1702 |
return; |
| 1703 |
set_stmt = SSA_NAME_DEF_STMT (op); |
| 1704 |
if (!gimple_call_builtin_p (set_stmt, BUILT_IN_CONSTANT_P) |
| 1705 |
|| gimple_call_num_args (set_stmt) != 1) |
| 1706 |
return; |
| 1707 |
op2 = gimple_call_arg (set_stmt, 0); |
| 1708 |
if (!decompose_param_expr (fbi, set_stmt, op2, &index, ¶m_type, &aggpos)) |
| 1709 |
return; |
| 1710 |
if (!aggpos.by_ref) |
| 1711 |
add_builtin_constant_p_parm (summary, index); |
| 1712 |
FOR_EACH_EDGE (e, ei, bb->succs) if (e->flags & EDGE_FALSE_VALUE) |
| 1713 |
{ |
| 1714 |
ipa_predicate p = add_condition (summary, params_summary, index, |
| 1715 |
param_type, &aggpos, |
| 1716 |
ipa_predicate::is_not_constant, NULL_TREE); |
| 1717 |
e->aux = edge_predicate_pool.allocate (); |
| 1718 |
*(ipa_predicate *) e->aux = p; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
|
| 1723 |
/* If BB ends by a switch we can turn into predicates, attach corresponding |
| 1724 |
predicates to the CFG edges. */ |
| 1725 |
|
| 1726 |
static void |
| 1727 |
set_switch_stmt_execution_predicate (struct ipa_func_body_info *fbi, |
| 1728 |
class ipa_fn_summary *summary, |
| 1729 |
class ipa_node_params *params_summary, |
| 1730 |
basic_block bb) |
| 1731 |
{ |
| 1732 |
tree op; |
| 1733 |
int index; |
| 1734 |
struct agg_position_info aggpos; |
| 1735 |
edge e; |
| 1736 |
edge_iterator ei; |
| 1737 |
size_t n; |
| 1738 |
size_t case_idx; |
| 1739 |
tree param_type; |
| 1740 |
expr_eval_ops param_ops; |
| 1741 |
|
| 1742 |
gswitch *last = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)); |
| 1743 |
if (!last) |
| 1744 |
return; |
| 1745 |
op = gimple_switch_index (last); |
| 1746 |
if (!decompose_param_expr (fbi, last, op, &index, ¶m_type, &aggpos, |
| 1747 |
¶m_ops)) |
| 1748 |
return; |
| 1749 |
|
| 1750 |
auto_vec<std::pair<tree, tree> > ranges; |
| 1751 |
tree type = TREE_TYPE (op); |
| 1752 |
int bound_limit = opt_for_fn (fbi->node->decl, |
| 1753 |
param_ipa_max_switch_predicate_bounds); |
| 1754 |
int bound_count = 0; |
| 1755 |
// This can safely be an integer range, as switches can only hold |
| 1756 |
// integers. |
| 1757 |
int_range<2> vr; |
| 1758 |
|
| 1759 |
get_range_query (cfun)->range_of_expr (vr, op); |
| 1760 |
if (vr.undefined_p ()) |
| 1761 |
vr.set_varying (TREE_TYPE (op)); |
| 1762 |
tree vr_min, vr_max; |
| 1763 |
// TODO: This entire function could use a rewrite to use the irange |
| 1764 |
// API, instead of trying to recreate its intersection/union logic. |
| 1765 |
// Any use of get_legacy_range() is a serious code smell. |
| 1766 |
value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max); |
| 1767 |
wide_int vr_wmin = wi::to_wide (vr_min); |
| 1768 |
wide_int vr_wmax = wi::to_wide (vr_max); |
| 1769 |
|
| 1770 |
FOR_EACH_EDGE (e, ei, bb->succs) |
| 1771 |
{ |
| 1772 |
e->aux = edge_predicate_pool.allocate (); |
| 1773 |
*(ipa_predicate *) e->aux = false; |
| 1774 |
} |
| 1775 |
|
| 1776 |
e = gimple_switch_edge (cfun, last, 0); |
| 1777 |
/* Set BOUND_COUNT to maximum count to bypass computing predicate for |
| 1778 |
default case if its target basic block is in convergence point of all |
| 1779 |
switch cases, which can be determined by checking whether it |
| 1780 |
post-dominates the switch statement. */ |
| 1781 |
if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) |
| 1782 |
bound_count = INT_MAX; |
| 1783 |
|
| 1784 |
n = gimple_switch_num_labels (last); |
| 1785 |
for (case_idx = 1; case_idx < n; ++case_idx) |
| 1786 |
{ |
| 1787 |
tree cl = gimple_switch_label (last, case_idx); |
| 1788 |
tree min = CASE_LOW (cl); |
| 1789 |
tree max = CASE_HIGH (cl); |
| 1790 |
ipa_predicate p; |
| 1791 |
|
| 1792 |
e = gimple_switch_edge (cfun, last, case_idx); |
| 1793 |
|
| 1794 |
/* The case value might not have same type as switch expression, |
| 1795 |
extend the value based on the expression type. */ |
| 1796 |
if (TREE_TYPE (min) != type) |
| 1797 |
min = wide_int_to_tree (type, wi::to_wide (min)); |
| 1798 |
|
| 1799 |
if (!max) |
| 1800 |
max = min; |
| 1801 |
else if (TREE_TYPE (max) != type) |
| 1802 |
max = wide_int_to_tree (type, wi::to_wide (max)); |
| 1803 |
|
| 1804 |
/* The case's target basic block is in convergence point of all switch |
| 1805 |
cases, its predicate should be at least as that of the switch |
| 1806 |
statement. */ |
| 1807 |
if (dominated_by_p (CDI_POST_DOMINATORS, bb, e->dest)) |
| 1808 |
p = true; |
| 1809 |
else if (min == max) |
| 1810 |
p = add_condition (summary, params_summary, index, param_type, |
| 1811 |
&aggpos, EQ_EXPR, min, param_ops); |
| 1812 |
else |
| 1813 |
{ |
| 1814 |
ipa_predicate p1, p2; |
| 1815 |
p1 = add_condition (summary, params_summary, index, param_type, |
| 1816 |
&aggpos, GE_EXPR, min, param_ops); |
| 1817 |
p2 = add_condition (summary, params_summary,index, param_type, |
| 1818 |
&aggpos, LE_EXPR, max, param_ops); |
| 1819 |
p = p1 & p2; |
| 1820 |
} |
| 1821 |
*(ipa_predicate *) e->aux |
| 1822 |
= p.or_with (summary->conds, *(ipa_predicate *) e->aux); |
| 1823 |
|
| 1824 |
/* If there are too many disjoint case ranges, predicate for default |
| 1825 |
case might become too complicated. So add a limit here. */ |
| 1826 |
if (bound_count > bound_limit) |
| 1827 |
continue; |
| 1828 |
|
| 1829 |
bool new_range = true; |
| 1830 |
|
| 1831 |
if (!ranges.is_empty ()) |
| 1832 |
{ |
| 1833 |
wide_int curr_wmin = wi::to_wide (min); |
| 1834 |
wide_int last_wmax = wi::to_wide (ranges.last ().second); |
| 1835 |
|
| 1836 |
/* Merge case ranges if they are continuous. */ |
| 1837 |
if (curr_wmin == last_wmax + 1) |
| 1838 |
new_range = false; |
| 1839 |
else if (vr_type == VR_ANTI_RANGE) |
| 1840 |
{ |
| 1841 |
/* If two disjoint case ranges can be connected by anti-range |
| 1842 |
of switch index, combine them to one range. */ |
| 1843 |
if (wi::lt_p (vr_wmax, curr_wmin - 1, TYPE_SIGN (type))) |
| 1844 |
vr_type = VR_UNDEFINED; |
| 1845 |
else if (wi::le_p (vr_wmin, last_wmax + 1, TYPE_SIGN (type))) |
| 1846 |
new_range = false; |
| 1847 |
} |
| 1848 |
} |
| 1849 |
|
| 1850 |
/* Create/extend a case range. And we count endpoints of range set, |
| 1851 |
this number nearly equals to number of conditions that we will create |
| 1852 |
for predicate of default case. */ |
| 1853 |
if (new_range) |
| 1854 |
{ |
| 1855 |
bound_count += (min == max) ? 1 : 2; |
| 1856 |
ranges.safe_push (std::make_pair (min, max)); |
| 1857 |
} |
| 1858 |
else |
| 1859 |
{ |
| 1860 |
bound_count += (ranges.last ().first == ranges.last ().second); |
| 1861 |
ranges.last ().second = max; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
e = gimple_switch_edge (cfun, last, 0); |
| 1866 |
if (bound_count > bound_limit) |
| 1867 |
{ |
| 1868 |
*(ipa_predicate *) e->aux = true; |
| 1869 |
vec_free (param_ops); |
| 1870 |
return; |
| 1871 |
} |
| 1872 |
|
| 1873 |
ipa_predicate p_seg = true; |
| 1874 |
ipa_predicate p_all = false; |
| 1875 |
|
| 1876 |
if (vr_type != VR_RANGE) |
| 1877 |
{ |
| 1878 |
vr_wmin = wi::to_wide (TYPE_MIN_VALUE (type)); |
| 1879 |
vr_wmax = wi::to_wide (TYPE_MAX_VALUE (type)); |
| 1880 |
} |
| 1881 |
|
| 1882 |
/* Construct predicate to represent default range set that is negation of |
| 1883 |
all case ranges. Case range is classified as containing single/non-single |
| 1884 |
values. Suppose a piece of case ranges in the following. |
| 1885 |
|
| 1886 |
[D1...D2] [S1] ... [Sn] [D3...D4] |
| 1887 |
|
| 1888 |
To represent default case's range sets between two non-single value |
| 1889 |
case ranges (From D2 to D3), we construct predicate as: |
| 1890 |
|
| 1891 |
D2 < x < D3 && x != S1 && ... && x != Sn |
| 1892 |
*/ |
| 1893 |
for (size_t i = 0; i < ranges.length (); i++) |
| 1894 |
{ |
| 1895 |
tree min = ranges[i].first; |
| 1896 |
tree max = ranges[i].second; |
| 1897 |
|
| 1898 |
if (min == max) |
| 1899 |
p_seg &= add_condition (summary, params_summary, index, |
| 1900 |
param_type, &aggpos, NE_EXPR, |
| 1901 |
min, param_ops); |
| 1902 |
else |
| 1903 |
{ |
| 1904 |
/* Do not create sub-predicate for range that is beyond low bound |
| 1905 |
of switch index. */ |
| 1906 |
if (wi::lt_p (vr_wmin, wi::to_wide (min), TYPE_SIGN (type))) |
| 1907 |
{ |
| 1908 |
p_seg &= add_condition (summary, params_summary, index, |
| 1909 |
param_type, &aggpos, |
| 1910 |
LT_EXPR, min, param_ops); |
| 1911 |
p_all = p_all.or_with (summary->conds, p_seg); |
| 1912 |
} |
| 1913 |
|
| 1914 |
/* Do not create sub-predicate for range that is beyond up bound |
| 1915 |
of switch index. */ |
| 1916 |
if (wi::le_p (vr_wmax, wi::to_wide (max), TYPE_SIGN (type))) |
| 1917 |
{ |
| 1918 |
p_seg = false; |
| 1919 |
break; |
| 1920 |
} |
| 1921 |
|
| 1922 |
p_seg = add_condition (summary, params_summary, index, |
| 1923 |
param_type, &aggpos, GT_EXPR, |
| 1924 |
max, param_ops); |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
p_all = p_all.or_with (summary->conds, p_seg); |
| 1929 |
*(ipa_predicate *) e->aux |
| 1930 |
= p_all.or_with (summary->conds, *(ipa_predicate *) e->aux); |
| 1931 |
|
| 1932 |
vec_free (param_ops); |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
/* For each BB in NODE attach to its AUX pointer predicate under |
| 1937 |
which it is executable. */ |
| 1938 |
|
| 1939 |
static void |
| 1940 |
compute_bb_predicates (struct ipa_func_body_info *fbi, |
| 1941 |
struct cgraph_node *node, |
| 1942 |
class ipa_fn_summary *summary, |
| 1943 |
class ipa_node_params *params_summary) |
| 1944 |
{ |
| 1945 |
struct function *my_function = DECL_STRUCT_FUNCTION (node->decl); |
| 1946 |
bool done = false; |
| 1947 |
basic_block bb; |
| 1948 |
|
| 1949 |
FOR_EACH_BB_FN (bb, my_function) |
| 1950 |
{ |
| 1951 |
set_cond_stmt_execution_predicate (fbi, summary, params_summary, bb); |
| 1952 |
set_switch_stmt_execution_predicate (fbi, summary, params_summary, bb); |
| 1953 |
} |
| 1954 |
|
| 1955 |
/* Entry block is always executable. */ |
| 1956 |
ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux |
| 1957 |
= edge_predicate_pool.allocate (); |
| 1958 |
*(ipa_predicate *) ENTRY_BLOCK_PTR_FOR_FN (my_function)->aux = true; |
| 1959 |
|
| 1960 |
/* A simple dataflow propagation of predicates forward in the CFG. |
| 1961 |
TODO: work in reverse postorder. */ |
| 1962 |
while (!done) |
| 1963 |
{ |
| 1964 |
done = true; |
| 1965 |
FOR_EACH_BB_FN (bb, my_function) |
| 1966 |
{ |
| 1967 |
ipa_predicate p = false; |
| 1968 |
edge e; |
| 1969 |
edge_iterator ei; |
| 1970 |
FOR_EACH_EDGE (e, ei, bb->preds) |
| 1971 |
{ |
| 1972 |
if (e->src->aux) |
| 1973 |
{ |
| 1974 |
ipa_predicate this_bb_predicate |
| 1975 |
= *(ipa_predicate *) e->src->aux; |
| 1976 |
if (e->aux) |
| 1977 |
this_bb_predicate &= (*(ipa_predicate *) e->aux); |
| 1978 |
p = p.or_with (summary->conds, this_bb_predicate); |
| 1979 |
if (p == true) |
| 1980 |
break; |
| 1981 |
} |
| 1982 |
} |
| 1983 |
if (p != false) |
| 1984 |
{ |
| 1985 |
basic_block pdom_bb; |
| 1986 |
|
| 1987 |
if (!bb->aux) |
| 1988 |
{ |
| 1989 |
done = false; |
| 1990 |
bb->aux = edge_predicate_pool.allocate (); |
| 1991 |
*((ipa_predicate *) bb->aux) = p; |
| 1992 |
} |
| 1993 |
else if (p != *(ipa_predicate *) bb->aux) |
| 1994 |
{ |
| 1995 |
/* This OR operation is needed to ensure monotonous data flow |
| 1996 |
in the case we hit the limit on number of clauses and the |
| 1997 |
and/or operations above give approximate answers. */ |
| 1998 |
p = p.or_with (summary->conds, *(ipa_predicate *)bb->aux); |
| 1999 |
if (p != *(ipa_predicate *)bb->aux) |
| 2000 |
{ |
| 2001 |
done = false; |
| 2002 |
*((ipa_predicate *)bb->aux) = p; |
| 2003 |
} |
| 2004 |
} |
| 2005 |
|
| 2006 |
/* For switch/if statement, we can OR-combine predicates of all |
| 2007 |
its cases/branches to get predicate for basic block in their |
| 2008 |
convergence point, but sometimes this will generate very |
| 2009 |
complicated predicate. Actually, we can get simplified |
| 2010 |
predicate in another way by using the fact that predicate |
| 2011 |
for a basic block must also hold true for its post dominators. |
| 2012 |
To be specific, basic block in convergence point of |
| 2013 |
conditional statement should include predicate of the |
| 2014 |
statement. */ |
| 2015 |
pdom_bb = get_immediate_dominator (CDI_POST_DOMINATORS, bb); |
| 2016 |
if (pdom_bb == EXIT_BLOCK_PTR_FOR_FN (my_function) || !pdom_bb) |
| 2017 |
; |
| 2018 |
else if (!pdom_bb->aux) |
| 2019 |
{ |
| 2020 |
done = false; |
| 2021 |
pdom_bb->aux = edge_predicate_pool.allocate (); |
| 2022 |
*((ipa_predicate *)pdom_bb->aux) = p; |
| 2023 |
} |
| 2024 |
else if (p != *(ipa_predicate *)pdom_bb->aux) |
| 2025 |
{ |
| 2026 |
p = p.or_with (summary->conds, |
| 2027 |
*(ipa_predicate *)pdom_bb->aux); |
| 2028 |
if (p != *(ipa_predicate *)pdom_bb->aux) |
| 2029 |
{ |
| 2030 |
done = false; |
| 2031 |
*((ipa_predicate *)pdom_bb->aux) = p; |
| 2032 |
} |
| 2033 |
} |
| 2034 |
} |
| 2035 |
} |
| 2036 |
} |
| 2037 |
} |
| 2038 |
|
| 2039 |
|
| 2040 |
/* Return predicate specifying when the STMT might have result that is not |
| 2041 |
a compile time constant. */ |
| 2042 |
|
| 2043 |
static ipa_predicate |
| 2044 |
will_be_nonconstant_expr_predicate (ipa_func_body_info *fbi, |
| 2045 |
class ipa_fn_summary *summary, |
| 2046 |
class ipa_node_params *params_summary, |
| 2047 |
tree expr, |
| 2048 |
vec<ipa_predicate> nonconstant_names) |
| 2049 |
{ |
| 2050 |
tree parm; |
| 2051 |
int index; |
| 2052 |
|
| 2053 |
while (UNARY_CLASS_P (expr)) |
| 2054 |
expr = TREE_OPERAND (expr, 0); |
| 2055 |
|
| 2056 |
parm = unmodified_parm (fbi, NULL, expr, NULL); |
| 2057 |
if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0) |
| 2058 |
return add_condition (summary, params_summary, index, TREE_TYPE (parm), NULL, |
| 2059 |
ipa_predicate::changed, NULL_TREE); |
| 2060 |
if (is_gimple_min_invariant (expr)) |
| 2061 |
return false; |
| 2062 |
if (TREE_CODE (expr) == SSA_NAME) |
| 2063 |
return nonconstant_names[SSA_NAME_VERSION (expr)]; |
| 2064 |
if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr)) |
| 2065 |
{ |
| 2066 |
ipa_predicate p1 |
| 2067 |
= will_be_nonconstant_expr_predicate (fbi, summary, |
| 2068 |
params_summary, |
| 2069 |
TREE_OPERAND (expr, 0), |
| 2070 |
nonconstant_names); |
| 2071 |
if (p1 == true) |
| 2072 |
return p1; |
| 2073 |
|
| 2074 |
ipa_predicate p2 |
| 2075 |
= will_be_nonconstant_expr_predicate (fbi, summary, |
| 2076 |
params_summary, |
| 2077 |
TREE_OPERAND (expr, 1), |
| 2078 |
nonconstant_names); |
| 2079 |
return p1.or_with (summary->conds, p2); |
| 2080 |
} |
| 2081 |
else if (TREE_CODE (expr) == COND_EXPR) |
| 2082 |
{ |
| 2083 |
ipa_predicate p1 |
| 2084 |
= will_be_nonconstant_expr_predicate (fbi, summary, |
| 2085 |
params_summary, |
| 2086 |
TREE_OPERAND (expr, 0), |
| 2087 |
nonconstant_names); |
| 2088 |
if (p1 == true) |
| 2089 |
return p1; |
| 2090 |
|
| 2091 |
ipa_predicate p2 |
| 2092 |
= will_be_nonconstant_expr_predicate (fbi, summary, |
| 2093 |
params_summary, |
| 2094 |
TREE_OPERAND (expr, 1), |
| 2095 |
nonconstant_names); |
| 2096 |
if (p2 == true) |
| 2097 |
return p2; |
| 2098 |
p1 = p1.or_with (summary->conds, p2); |
| 2099 |
p2 = will_be_nonconstant_expr_predicate (fbi, summary, |
| 2100 |
params_summary, |
| 2101 |
TREE_OPERAND (expr, 2), |
| 2102 |
nonconstant_names); |
| 2103 |
return p2.or_with (summary->conds, p1); |
| 2104 |
} |
| 2105 |
else if (TREE_CODE (expr) == CALL_EXPR) |
| 2106 |
return true; |
| 2107 |
else |
| 2108 |
{ |
| 2109 |
debug_tree (expr); |
| 2110 |
gcc_unreachable (); |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
/* Return predicate specifying when the STMT might have result that is not |
| 2116 |
a compile time constant. */ |
| 2117 |
|
| 2118 |
static ipa_predicate |
| 2119 |
will_be_nonconstant_predicate (struct ipa_func_body_info *fbi, |
| 2120 |
class ipa_fn_summary *summary, |
| 2121 |
class ipa_node_params *params_summary, |
| 2122 |
gimple *stmt, |
| 2123 |
vec<ipa_predicate> nonconstant_names) |
| 2124 |
{ |
| 2125 |
ipa_predicate p = true; |
| 2126 |
ssa_op_iter iter; |
| 2127 |
tree use; |
| 2128 |
tree param_type = NULL_TREE; |
| 2129 |
ipa_predicate op_non_const; |
| 2130 |
bool is_load; |
| 2131 |
int base_index; |
| 2132 |
struct agg_position_info aggpos; |
| 2133 |
|
| 2134 |
/* What statements might be optimized away |
| 2135 |
when their arguments are constant. */ |
| 2136 |
if (gimple_code (stmt) != GIMPLE_ASSIGN |
| 2137 |
&& gimple_code (stmt) != GIMPLE_COND |
| 2138 |
&& gimple_code (stmt) != GIMPLE_SWITCH |
| 2139 |
&& (gimple_code (stmt) != GIMPLE_CALL |
| 2140 |
|| !(gimple_call_flags (stmt) & ECF_CONST))) |
| 2141 |
return p; |
| 2142 |
|
| 2143 |
/* Stores will stay anyway. */ |
| 2144 |
if (gimple_store_p (stmt)) |
| 2145 |
return p; |
| 2146 |
|
| 2147 |
is_load = gimple_assign_load_p (stmt); |
| 2148 |
|
| 2149 |
/* Loads can be optimized when the value is known. */ |
| 2150 |
if (is_load) |
| 2151 |
{ |
| 2152 |
tree op = gimple_assign_rhs1 (stmt); |
| 2153 |
if (!decompose_param_expr (fbi, stmt, op, &base_index, ¶m_type, |
| 2154 |
&aggpos)) |
| 2155 |
return p; |
| 2156 |
} |
| 2157 |
else |
| 2158 |
base_index = -1; |
| 2159 |
|
| 2160 |
/* See if we understand all operands before we start |
| 2161 |
adding conditionals. */ |
| 2162 |
FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE) |
| 2163 |
{ |
| 2164 |
tree parm = unmodified_parm (fbi, stmt, use, NULL); |
| 2165 |
/* For arguments we can build a condition. */ |
| 2166 |
if (parm && ipa_get_param_decl_index (fbi->info, parm) >= 0) |
| 2167 |
continue; |
| 2168 |
if (TREE_CODE (use) != SSA_NAME) |
| 2169 |
return p; |
| 2170 |
/* If we know when operand is constant, |
| 2171 |
we still can say something useful. */ |
| 2172 |
if (nonconstant_names[SSA_NAME_VERSION (use)] != true) |
| 2173 |
continue; |
| 2174 |
return p; |
| 2175 |
} |
| 2176 |
|
| 2177 |
if (is_load) |
| 2178 |
op_non_const = |
| 2179 |
add_condition (summary, params_summary, |
| 2180 |
base_index, param_type, &aggpos, |
| 2181 |
ipa_predicate::changed, NULL_TREE); |
| 2182 |
else |
| 2183 |
op_non_const = false; |
| 2184 |
FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE) |
| 2185 |
{ |
| 2186 |
tree parm = unmodified_parm (fbi, stmt, use, NULL); |
| 2187 |
int index; |
| 2188 |
|
| 2189 |
if (parm && (index = ipa_get_param_decl_index (fbi->info, parm)) >= 0) |
| 2190 |
{ |
| 2191 |
if (index != base_index) |
| 2192 |
p = add_condition (summary, params_summary, index, |
| 2193 |
TREE_TYPE (parm), NULL, |
| 2194 |
ipa_predicate::changed, NULL_TREE); |
| 2195 |
else |
| 2196 |
continue; |
| 2197 |
} |
| 2198 |
else |
| 2199 |
p = nonconstant_names[SSA_NAME_VERSION (use)]; |
| 2200 |
op_non_const = p.or_with (summary->conds, op_non_const); |
| 2201 |
} |
| 2202 |
if ((gimple_code (stmt) == GIMPLE_ASSIGN || gimple_code (stmt) == GIMPLE_CALL) |
| 2203 |
&& gimple_op (stmt, 0) |
| 2204 |
&& TREE_CODE (gimple_op (stmt, 0)) == SSA_NAME) |
| 2205 |
nonconstant_names[SSA_NAME_VERSION (gimple_op (stmt, 0))] |
| 2206 |
= op_non_const; |
| 2207 |
return op_non_const; |
| 2208 |
} |
| 2209 |
|
| 2210 |
struct record_modified_bb_info |
| 2211 |
{ |
| 2212 |
tree op; |
| 2213 |
bitmap bb_set; |
| 2214 |
gimple *stmt; |
| 2215 |
}; |
| 2216 |
|
| 2217 |
/* Value is initialized in INIT_BB and used in USE_BB. We want to compute |
| 2218 |
probability how often it changes between USE_BB. |
| 2219 |
INIT_BB->count/USE_BB->count is an estimate, but if INIT_BB |
| 2220 |
is in different loop nest, we can do better. |
| 2221 |
This is all just estimate. In theory we look for minimal cut separating |
| 2222 |
INIT_BB and USE_BB, but we only want to anticipate loop invariant motion |
| 2223 |
anyway. */ |
| 2224 |
|
| 2225 |
static basic_block |
| 2226 |
get_minimal_bb (basic_block init_bb, basic_block use_bb) |
| 2227 |
{ |
| 2228 |
class loop *l = find_common_loop (init_bb->loop_father, use_bb->loop_father); |
| 2229 |
if (l && l->header->count < init_bb->count) |
| 2230 |
return l->header; |
| 2231 |
return init_bb; |
| 2232 |
} |
| 2233 |
|
| 2234 |
/* Callback of walk_aliased_vdefs. Records basic blocks where the value may be |
| 2235 |
set except for info->stmt. */ |
| 2236 |
|
| 2237 |
static bool |
| 2238 |
record_modified (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data) |
| 2239 |
{ |
| 2240 |
struct record_modified_bb_info *info = |
| 2241 |
(struct record_modified_bb_info *) data; |
| 2242 |
if (SSA_NAME_DEF_STMT (vdef) == info->stmt) |
| 2243 |
return false; |
| 2244 |
if (gimple_clobber_p (SSA_NAME_DEF_STMT (vdef))) |
| 2245 |
return false; |
| 2246 |
bitmap_set_bit (info->bb_set, |
| 2247 |
SSA_NAME_IS_DEFAULT_DEF (vdef) |
| 2248 |
? ENTRY_BLOCK_PTR_FOR_FN (cfun)->index |
| 2249 |
: get_minimal_bb |
| 2250 |
(gimple_bb (SSA_NAME_DEF_STMT (vdef)), |
| 2251 |
gimple_bb (info->stmt))->index); |
| 2252 |
if (dump_file) |
| 2253 |
{ |
| 2254 |
fprintf (dump_file, " Param "); |
| 2255 |
print_generic_expr (dump_file, info->op, TDF_SLIM); |
| 2256 |
fprintf (dump_file, " changed at bb %i, minimal: %i stmt: ", |
| 2257 |
gimple_bb (SSA_NAME_DEF_STMT (vdef))->index, |
| 2258 |
get_minimal_bb |
| 2259 |
(gimple_bb (SSA_NAME_DEF_STMT (vdef)), |
| 2260 |
gimple_bb (info->stmt))->index); |
| 2261 |
print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (vdef), 0); |
| 2262 |
} |
| 2263 |
return false; |
| 2264 |
} |
| 2265 |
|
| 2266 |
/* Return probability (based on REG_BR_PROB_BASE) that I-th parameter of STMT |
| 2267 |
will change since last invocation of STMT. |
| 2268 |
|
| 2269 |
Value 0 is reserved for compile time invariants. |
| 2270 |
For common parameters it is REG_BR_PROB_BASE. For loop invariants it |
| 2271 |
ought to be REG_BR_PROB_BASE / estimated_iters. */ |
| 2272 |
|
| 2273 |
static int |
| 2274 |
param_change_prob (ipa_func_body_info *fbi, gimple *stmt, int i) |
| 2275 |
{ |
| 2276 |
tree op = gimple_call_arg (stmt, i); |
| 2277 |
basic_block bb = gimple_bb (stmt); |
| 2278 |
|
| 2279 |
if (TREE_CODE (op) == WITH_SIZE_EXPR) |
| 2280 |
op = TREE_OPERAND (op, 0); |
| 2281 |
|
| 2282 |
tree base = get_base_address (op); |
| 2283 |
|
| 2284 |
/* Global invariants never change. */ |
| 2285 |
if (is_gimple_min_invariant (base)) |
| 2286 |
return 0; |
| 2287 |
|
| 2288 |
/* We would have to do non-trivial analysis to really work out what |
| 2289 |
is the probability of value to change (i.e. when init statement |
| 2290 |
is in a sibling loop of the call). |
| 2291 |
|
| 2292 |
We do an conservative estimate: when call is executed N times more often |
| 2293 |
than the statement defining value, we take the frequency 1/N. */ |
| 2294 |
if (TREE_CODE (base) == SSA_NAME) |
| 2295 |
{ |
| 2296 |
profile_count init_count; |
| 2297 |
|
| 2298 |
if (!bb->count.nonzero_p ()) |
| 2299 |
return REG_BR_PROB_BASE; |
| 2300 |
|
| 2301 |
if (SSA_NAME_IS_DEFAULT_DEF (base)) |
| 2302 |
init_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count; |
| 2303 |
else |
| 2304 |
init_count = get_minimal_bb |
| 2305 |
(gimple_bb (SSA_NAME_DEF_STMT (base)), |
| 2306 |
gimple_bb (stmt))->count; |
| 2307 |
|
| 2308 |
if (init_count < bb->count) |
| 2309 |
return MAX ((init_count.to_sreal_scale (bb->count) |
| 2310 |
* REG_BR_PROB_BASE).to_int (), 1); |
| 2311 |
return REG_BR_PROB_BASE; |
| 2312 |
} |
| 2313 |
else |
| 2314 |
{ |
| 2315 |
ao_ref refd; |
| 2316 |
profile_count max = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count; |
| 2317 |
struct record_modified_bb_info info; |
| 2318 |
tree init = ctor_for_folding (base); |
| 2319 |
|
| 2320 |
if (init != error_mark_node) |
| 2321 |
return 0; |
| 2322 |
if (!bb->count.nonzero_p () || fbi->aa_walk_budget == 0) |
| 2323 |
return REG_BR_PROB_BASE; |
| 2324 |
if (dump_file) |
| 2325 |
{ |
| 2326 |
fprintf (dump_file, " Analyzing param change probability of "); |
| 2327 |
print_generic_expr (dump_file, op, TDF_SLIM); |
| 2328 |
fprintf (dump_file, "\n"); |
| 2329 |
} |
| 2330 |
ao_ref_init (&refd, op); |
| 2331 |
info.op = op; |
| 2332 |
info.stmt = stmt; |
| 2333 |
info.bb_set = BITMAP_ALLOC (NULL); |
| 2334 |
int walked |
| 2335 |
= walk_aliased_vdefs (&refd, gimple_vuse (stmt), record_modified, &info, |
| 2336 |
NULL, NULL, fbi->aa_walk_budget); |
| 2337 |
if (walked > 0) |
| 2338 |
fbi->aa_walk_budget -= walked; |
| 2339 |
if (walked < 0 || bitmap_bit_p (info.bb_set, bb->index)) |
| 2340 |
{ |
| 2341 |
if (walked < 0) |
| 2342 |
fbi->aa_walk_budget = 0; |
| 2343 |
if (dump_file) |
| 2344 |
{ |
| 2345 |
if (walked < 0) |
| 2346 |
fprintf (dump_file, " Ran out of AA walking budget.\n"); |
| 2347 |
else |
| 2348 |
fprintf (dump_file, " Set in same BB as used.\n"); |
| 2349 |
} |
| 2350 |
BITMAP_FREE (info.bb_set); |
| 2351 |
return REG_BR_PROB_BASE; |
| 2352 |
} |
| 2353 |
|
| 2354 |
bitmap_iterator bi; |
| 2355 |
unsigned index; |
| 2356 |
/* Lookup the most frequent update of the value and believe that |
| 2357 |
it dominates all the other; precise analysis here is difficult. */ |
| 2358 |
EXECUTE_IF_SET_IN_BITMAP (info.bb_set, 0, index, bi) |
| 2359 |
max = max.max (BASIC_BLOCK_FOR_FN (cfun, index)->count); |
| 2360 |
if (dump_file) |
| 2361 |
{ |
| 2362 |
fprintf (dump_file, " Set with count "); |
| 2363 |
max.dump (dump_file); |
| 2364 |
fprintf (dump_file, " and used with count "); |
| 2365 |
bb->count.dump (dump_file); |
| 2366 |
fprintf (dump_file, " freq %f\n", |
| 2367 |
max.to_sreal_scale (bb->count).to_double ()); |
| 2368 |
} |
| 2369 |
|
| 2370 |
BITMAP_FREE (info.bb_set); |
| 2371 |
if (max < bb->count) |
| 2372 |
return MAX ((max.to_sreal_scale (bb->count) |
| 2373 |
* REG_BR_PROB_BASE).to_int (), 1); |
| 2374 |
return REG_BR_PROB_BASE; |
| 2375 |
} |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* Find whether a basic block BB is the final block of a (half) diamond CFG |
| 2379 |
sub-graph and if the predicate the condition depends on is known. If so, |
| 2380 |
return true and store the pointer the predicate in *P. */ |
| 2381 |
|
| 2382 |
static bool |
| 2383 |
phi_result_unknown_predicate (ipa_func_body_info *fbi, |
| 2384 |
ipa_fn_summary *summary, |
| 2385 |
class ipa_node_params *params_summary, |
| 2386 |
basic_block bb, |
| 2387 |
ipa_predicate *p, |
| 2388 |
vec<ipa_predicate> nonconstant_names) |
| 2389 |
{ |
| 2390 |
edge e; |
| 2391 |
edge_iterator ei; |
| 2392 |
basic_block first_bb = NULL; |
| 2393 |
|
| 2394 |
if (single_pred_p (bb)) |
| 2395 |
{ |
| 2396 |
*p = false; |
| 2397 |
return true; |
| 2398 |
} |
| 2399 |
|
| 2400 |
FOR_EACH_EDGE (e, ei, bb->preds) |
| 2401 |
{ |
| 2402 |
if (single_succ_p (e->src)) |
| 2403 |
{ |
| 2404 |
if (!single_pred_p (e->src)) |
| 2405 |
return false; |
| 2406 |
if (!first_bb) |
| 2407 |
first_bb = single_pred (e->src); |
| 2408 |
else if (single_pred (e->src) != first_bb) |
| 2409 |
return false; |
| 2410 |
} |
| 2411 |
else |
| 2412 |
{ |
| 2413 |
if (!first_bb) |
| 2414 |
first_bb = e->src; |
| 2415 |
else if (e->src != first_bb) |
| 2416 |
return false; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
if (!first_bb) |
| 2421 |
return false; |
| 2422 |
|
| 2423 |
gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (first_bb)); |
| 2424 |
if (!stmt |
| 2425 |
|| !is_gimple_ip_invariant (gimple_cond_rhs (stmt))) |
| 2426 |
return false; |
| 2427 |
|
| 2428 |
*p = will_be_nonconstant_expr_predicate (fbi, summary, params_summary, |
| 2429 |
gimple_cond_lhs (stmt), |
| 2430 |
nonconstant_names); |
| 2431 |
if (*p == true) |
| 2432 |
return false; |
| 2433 |
else |
| 2434 |
return true; |
| 2435 |
} |
| 2436 |
|
| 2437 |
/* Given a PHI statement in a function described by inline properties SUMMARY |
| 2438 |
and *P being the predicate describing whether the selected PHI argument is |
| 2439 |
known, store a predicate for the result of the PHI statement into |
| 2440 |
NONCONSTANT_NAMES, if possible. */ |
| 2441 |
|
| 2442 |
static void |
| 2443 |
predicate_for_phi_result (class ipa_fn_summary *summary, gphi *phi, |
| 2444 |
ipa_predicate *p, |
| 2445 |
vec<ipa_predicate> nonconstant_names) |
| 2446 |
{ |
| 2447 |
unsigned i; |
| 2448 |
|
| 2449 |
for (i = 0; i < gimple_phi_num_args (phi); i++) |
| 2450 |
{ |
| 2451 |
tree arg = gimple_phi_arg (phi, i)->def; |
| 2452 |
if (!is_gimple_min_invariant (arg)) |
| 2453 |
{ |
| 2454 |
gcc_assert (TREE_CODE (arg) == SSA_NAME); |
| 2455 |
*p = p->or_with (summary->conds, |
| 2456 |
nonconstant_names[SSA_NAME_VERSION (arg)]); |
| 2457 |
if (*p == true) |
| 2458 |
return; |
| 2459 |
} |
| 2460 |
} |
| 2461 |
|
| 2462 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2463 |
{ |
| 2464 |
fprintf (dump_file, "\t\tphi predicate: "); |
| 2465 |
p->dump (dump_file, summary->conds); |
| 2466 |
} |
| 2467 |
nonconstant_names[SSA_NAME_VERSION (gimple_phi_result (phi))] = *p; |
| 2468 |
} |
| 2469 |
|
| 2470 |
/* For a typical usage of __builtin_expect (a<b, 1), we |
| 2471 |
may introduce an extra relation stmt: |
| 2472 |
With the builtin, we have |
| 2473 |
t1 = a <= b; |
| 2474 |
t2 = (long int) t1; |
| 2475 |
t3 = __builtin_expect (t2, 1); |
| 2476 |
if (t3 != 0) |
| 2477 |
goto ... |
| 2478 |
Without the builtin, we have |
| 2479 |
if (a<=b) |
| 2480 |
goto... |
| 2481 |
This affects the size/time estimation and may have |
| 2482 |
an impact on the earlier inlining. |
| 2483 |
Here find this pattern and fix it up later. */ |
| 2484 |
|
| 2485 |
static gimple * |
| 2486 |
find_foldable_builtin_expect (basic_block bb) |
| 2487 |
{ |
| 2488 |
gimple_stmt_iterator bsi; |
| 2489 |
|
| 2490 |
for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
| 2491 |
{ |
| 2492 |
gimple *stmt = gsi_stmt (bsi); |
| 2493 |
if (gimple_call_builtin_p (stmt, BUILT_IN_EXPECT) |
| 2494 |
|| gimple_call_builtin_p (stmt, BUILT_IN_EXPECT_WITH_PROBABILITY) |
| 2495 |
|| gimple_call_internal_p (stmt, IFN_BUILTIN_EXPECT)) |
| 2496 |
{ |
| 2497 |
tree var = gimple_call_lhs (stmt); |
| 2498 |
tree arg = gimple_call_arg (stmt, 0); |
| 2499 |
use_operand_p use_p; |
| 2500 |
gimple *use_stmt; |
| 2501 |
bool match = false; |
| 2502 |
bool done = false; |
| 2503 |
|
| 2504 |
if (!var || !arg) |
| 2505 |
continue; |
| 2506 |
gcc_assert (TREE_CODE (var) == SSA_NAME); |
| 2507 |
|
| 2508 |
while (TREE_CODE (arg) == SSA_NAME) |
| 2509 |
{ |
| 2510 |
gimple *stmt_tmp = SSA_NAME_DEF_STMT (arg); |
| 2511 |
if (!is_gimple_assign (stmt_tmp)) |
| 2512 |
break; |
| 2513 |
switch (gimple_assign_rhs_code (stmt_tmp)) |
| 2514 |
{ |
| 2515 |
case LT_EXPR: |
| 2516 |
case LE_EXPR: |
| 2517 |
case GT_EXPR: |
| 2518 |
case GE_EXPR: |
| 2519 |
case EQ_EXPR: |
| 2520 |
case NE_EXPR: |
| 2521 |
match = true; |
| 2522 |
done = true; |
| 2523 |
break; |
| 2524 |
CASE_CONVERT: |
| 2525 |
break; |
| 2526 |
default: |
| 2527 |
done = true; |
| 2528 |
break; |
| 2529 |
} |
| 2530 |
if (done) |
| 2531 |
break; |
| 2532 |
arg = gimple_assign_rhs1 (stmt_tmp); |
| 2533 |
} |
| 2534 |
|
| 2535 |
if (match && single_imm_use (var, &use_p, &use_stmt) |
| 2536 |
&& gimple_code (use_stmt) == GIMPLE_COND) |
| 2537 |
return use_stmt; |
| 2538 |
} |
| 2539 |
} |
| 2540 |
return NULL; |
| 2541 |
} |
| 2542 |
|
| 2543 |
/* Return true when the basic blocks contains only clobbers followed by RESX. |
| 2544 |
Such BBs are kept around to make removal of dead stores possible with |
| 2545 |
presence of EH and will be optimized out by optimize_clobbers later in the |
| 2546 |
game. |
| 2547 |
|
| 2548 |
NEED_EH is used to recurse in case the clobber has non-EH predecessors |
| 2549 |
that can be clobber only, too.. When it is false, the RESX is not necessary |
| 2550 |
on the end of basic block. */ |
| 2551 |
|
| 2552 |
static bool |
| 2553 |
clobber_only_eh_bb_p (basic_block bb, bool need_eh = true) |
| 2554 |
{ |
| 2555 |
gimple_stmt_iterator gsi = gsi_last_bb (bb); |
| 2556 |
edge_iterator ei; |
| 2557 |
edge e; |
| 2558 |
|
| 2559 |
if (need_eh) |
| 2560 |
{ |
| 2561 |
if (gsi_end_p (gsi)) |
| 2562 |
return false; |
| 2563 |
if (gimple_code (gsi_stmt (gsi)) != GIMPLE_RESX) |
| 2564 |
return false; |
| 2565 |
gsi_prev (&gsi); |
| 2566 |
} |
| 2567 |
else if (!single_succ_p (bb)) |
| 2568 |
return false; |
| 2569 |
|
| 2570 |
for (; !gsi_end_p (gsi); gsi_prev (&gsi)) |
| 2571 |
{ |
| 2572 |
gimple *stmt = gsi_stmt (gsi); |
| 2573 |
if (is_gimple_debug (stmt)) |
| 2574 |
continue; |
| 2575 |
if (gimple_clobber_p (stmt)) |
| 2576 |
continue; |
| 2577 |
if (gimple_code (stmt) == GIMPLE_LABEL) |
| 2578 |
break; |
| 2579 |
return false; |
| 2580 |
} |
| 2581 |
|
| 2582 |
/* See if all predecessors are either throws or clobber only BBs. */ |
| 2583 |
FOR_EACH_EDGE (e, ei, bb->preds) |
| 2584 |
if (!(e->flags & EDGE_EH) |
| 2585 |
&& !clobber_only_eh_bb_p (e->src, false)) |
| 2586 |
return false; |
| 2587 |
|
| 2588 |
return true; |
| 2589 |
} |
| 2590 |
|
| 2591 |
/* Return true if STMT compute a floating point expression that may be affected |
| 2592 |
by -ffast-math and similar flags. */ |
| 2593 |
|
| 2594 |
static bool |
| 2595 |
fp_expression_p (gimple *stmt) |
| 2596 |
{ |
| 2597 |
ssa_op_iter i; |
| 2598 |
tree op; |
| 2599 |
|
| 2600 |
FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF|SSA_OP_USE) |
| 2601 |
if (FLOAT_TYPE_P (TREE_TYPE (op))) |
| 2602 |
return true; |
| 2603 |
return false; |
| 2604 |
} |
| 2605 |
|
| 2606 |
/* Return true if T references memory location that is local |
| 2607 |
for the function (that means, dead after return) or read-only. */ |
| 2608 |
|
| 2609 |
bool |
| 2610 |
refs_local_or_readonly_memory_p (tree t) |
| 2611 |
{ |
| 2612 |
/* Non-escaping memory is fine. */ |
| 2613 |
t = get_base_address (t); |
| 2614 |
if ((TREE_CODE (t) == MEM_REF |
| 2615 |
|| TREE_CODE (t) == TARGET_MEM_REF)) |
| 2616 |
return points_to_local_or_readonly_memory_p (TREE_OPERAND (t, 0)); |
| 2617 |
|
| 2618 |
/* Automatic variables are fine. */ |
| 2619 |
if (DECL_P (t) |
| 2620 |
&& auto_var_in_fn_p (t, current_function_decl)) |
| 2621 |
return true; |
| 2622 |
|
| 2623 |
/* Read-only variables are fine. */ |
| 2624 |
if (DECL_P (t) && TREE_READONLY (t)) |
| 2625 |
return true; |
| 2626 |
|
| 2627 |
return false; |
| 2628 |
} |
| 2629 |
|
| 2630 |
/* Return true if T is a pointer pointing to memory location that is local |
| 2631 |
for the function (that means, dead after return) or read-only. */ |
| 2632 |
|
| 2633 |
bool |
| 2634 |
points_to_local_or_readonly_memory_p (tree t) |
| 2635 |
{ |
| 2636 |
/* See if memory location is clearly invalid. */ |
| 2637 |
if (integer_zerop (t)) |
| 2638 |
return flag_delete_null_pointer_checks; |
| 2639 |
if (TREE_CODE (t) == SSA_NAME) |
| 2640 |
{ |
| 2641 |
/* For IPA passes we can consinder accesses to return slot local |
| 2642 |
even if it is not local in the sense that memory is dead by |
| 2643 |
the end of founction. |
| 2644 |
The outer function will see a store in the call assignment |
| 2645 |
and thus this will do right thing for all uses of this |
| 2646 |
function in the current IPA passes (modref, pure/const discovery |
| 2647 |
and inlining heuristics). */ |
| 2648 |
if (DECL_RESULT (current_function_decl) |
| 2649 |
&& DECL_BY_REFERENCE (DECL_RESULT (current_function_decl)) |
| 2650 |
&& t == ssa_default_def (cfun, DECL_RESULT (current_function_decl))) |
| 2651 |
return true; |
| 2652 |
return !ptr_deref_may_alias_global_p (t, false); |
| 2653 |
} |
| 2654 |
if (TREE_CODE (t) == ADDR_EXPR |
| 2655 |
&& (TREE_CODE (TREE_OPERAND (t, 0)) != TARGET_MEM_REF |
| 2656 |
|| TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) != INTEGER_CST)) |
| 2657 |
return refs_local_or_readonly_memory_p (TREE_OPERAND (t, 0)); |
| 2658 |
return false; |
| 2659 |
} |
| 2660 |
|
| 2661 |
/* Return true if T is a pointer pointing to memory location that is possible |
| 2662 |
sra candidate if all functions it is passed to are inlined. */ |
| 2663 |
|
| 2664 |
static bool |
| 2665 |
points_to_possible_sra_candidate_p (tree t) |
| 2666 |
{ |
| 2667 |
if (TREE_CODE (t) != ADDR_EXPR) |
| 2668 |
return false; |
| 2669 |
|
| 2670 |
t = get_base_address (TREE_OPERAND (t, 0)); |
| 2671 |
|
| 2672 |
/* Automatic variables are fine. */ |
| 2673 |
if (DECL_P (t) |
| 2674 |
&& auto_var_in_fn_p (t, current_function_decl)) |
| 2675 |
return true; |
| 2676 |
return false; |
| 2677 |
} |
| 2678 |
|
| 2679 |
/* Return true if BB only calls builtin_unreachable. |
| 2680 |
We skip empty basic blocks, debug statements, clobbers and predicts. |
| 2681 |
CACHE is used to memoize already analyzed blocks. */ |
| 2682 |
|
| 2683 |
static bool |
| 2684 |
builtin_unreachable_bb_p (basic_block bb, vec<unsigned char> &cache) |
| 2685 |
{ |
| 2686 |
if (cache[bb->index]) |
| 2687 |
return cache[bb->index] - 1; |
| 2688 |
gimple_stmt_iterator si; |
| 2689 |
auto_vec <basic_block, 4> visited_bbs; |
| 2690 |
bool ret = false; |
| 2691 |
while (true) |
| 2692 |
{ |
| 2693 |
bool empty_bb = true; |
| 2694 |
visited_bbs.safe_push (bb); |
| 2695 |
cache[bb->index] = 3; |
| 2696 |
for (si = gsi_start_nondebug_bb (bb); |
| 2697 |
!gsi_end_p (si) && empty_bb; |
| 2698 |
gsi_next_nondebug (&si)) |
| 2699 |
{ |
| 2700 |
if (gimple_code (gsi_stmt (si)) != GIMPLE_PREDICT |
| 2701 |
&& !gimple_clobber_p (gsi_stmt (si)) |
| 2702 |
&& !gimple_nop_p (gsi_stmt (si))) |
| 2703 |
{ |
| 2704 |
empty_bb = false; |
| 2705 |
break; |
| 2706 |
} |
| 2707 |
} |
| 2708 |
if (!empty_bb) |
| 2709 |
break; |
| 2710 |
else |
| 2711 |
bb = single_succ_edge (bb)->dest; |
| 2712 |
if (cache[bb->index]) |
| 2713 |
{ |
| 2714 |
ret = cache[bb->index] == 3 ? false : cache[bb->index] - 1; |
| 2715 |
goto done; |
| 2716 |
} |
| 2717 |
} |
| 2718 |
if (gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE) |
| 2719 |
|| gimple_call_builtin_p (gsi_stmt (si), BUILT_IN_UNREACHABLE_TRAP)) |
| 2720 |
ret = true; |
| 2721 |
done: |
| 2722 |
for (basic_block vbb:visited_bbs) |
| 2723 |
cache[vbb->index] = (unsigned char)ret + 1; |
| 2724 |
return ret; |
| 2725 |
} |
| 2726 |
|
| 2727 |
static bool |
| 2728 |
guards_builtin_unreachable (basic_block bb, vec<unsigned char> &cache) |
| 2729 |
{ |
| 2730 |
edge_iterator ei; |
| 2731 |
edge e; |
| 2732 |
FOR_EACH_EDGE (e, ei, bb->succs) |
| 2733 |
if (builtin_unreachable_bb_p (e->dest, cache)) |
| 2734 |
{ |
| 2735 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2736 |
fprintf (dump_file, |
| 2737 |
"BB %i ends with conditional guarding __builtin_unreachable;" |
| 2738 |
" conditinal is unnecesary\n", bb->index); |
| 2739 |
return true; |
| 2740 |
} |
| 2741 |
return false; |
| 2742 |
} |
| 2743 |
|
| 2744 |
#define STMT_NECESSARY GF_PLF_1 |
| 2745 |
|
| 2746 |
/* If STMT is not already marked necessary, mark it, and add it to the |
| 2747 |
worklist if ADD_TO_WORKLIST is true. */ |
| 2748 |
|
| 2749 |
static inline void |
| 2750 |
mark_stmt_necessary (gimple *stmt, auto_vec<gimple *> &worklist) |
| 2751 |
{ |
| 2752 |
gcc_assert (stmt); |
| 2753 |
|
| 2754 |
if (gimple_plf (stmt, STMT_NECESSARY)) |
| 2755 |
return; |
| 2756 |
|
| 2757 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2758 |
{ |
| 2759 |
fprintf (dump_file, "Marking useful stmt: "); |
| 2760 |
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
| 2761 |
fprintf (dump_file, "\n"); |
| 2762 |
} |
| 2763 |
|
| 2764 |
gimple_set_plf (stmt, STMT_NECESSARY, true); |
| 2765 |
worklist.safe_push (stmt); |
| 2766 |
} |
| 2767 |
|
| 2768 |
/* Mark the statement defining operand OP as necessary. */ |
| 2769 |
|
| 2770 |
static inline void |
| 2771 |
mark_operand_necessary (tree op, auto_vec<gimple *> &worklist) |
| 2772 |
{ |
| 2773 |
gimple *stmt = SSA_NAME_DEF_STMT (op); |
| 2774 |
if (gimple_nop_p (stmt)) |
| 2775 |
return; |
| 2776 |
mark_stmt_necessary (stmt, worklist); |
| 2777 |
} |
| 2778 |
|
| 2779 |
/* Mark all statements that will remain in the body after optimizing out |
| 2780 |
conditionals guarding __builtin_unreachable which we keep to preserve |
| 2781 |
value ranges. */ |
| 2782 |
|
| 2783 |
static void |
| 2784 |
find_necessary_statements (struct cgraph_node *node) |
| 2785 |
{ |
| 2786 |
struct function *my_function = DECL_STRUCT_FUNCTION (node->decl); |
| 2787 |
auto_vec<unsigned char, 10> cache; |
| 2788 |
basic_block bb; |
| 2789 |
auto_vec<gimple *> worklist; |
| 2790 |
|
| 2791 |
cache.safe_grow_cleared (last_basic_block_for_fn (cfun)); |
| 2792 |
/* Mark all obviously necessary statements. */ |
| 2793 |
FOR_EACH_BB_FN (bb, my_function) |
| 2794 |
{ |
| 2795 |
for (gimple_stmt_iterator gsi = gsi_start_phis (bb); |
| 2796 |
!gsi_end_p (gsi); gsi_next (&gsi)) |
| 2797 |
gimple_set_plf (gsi_stmt (gsi), STMT_NECESSARY, false); |
| 2798 |
|
| 2799 |
for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); |
| 2800 |
gsi_next_nondebug (&bsi)) |
| 2801 |
{ |
| 2802 |
gimple *stmt = gsi_stmt (bsi); |
| 2803 |
|
| 2804 |
gimple_set_plf (stmt, STMT_NECESSARY, false); |
| 2805 |
if (gimple_has_side_effects (stmt) |
| 2806 |
|| (is_ctrl_stmt (stmt) |
| 2807 |
&& (gimple_code (stmt) != GIMPLE_COND |
| 2808 |
|| !guards_builtin_unreachable (bb, cache))) |
| 2809 |
|| gimple_store_p (stmt) |
| 2810 |
|| gimple_code (stmt) == GIMPLE_ASM) |
| 2811 |
mark_stmt_necessary (stmt, worklist); |
| 2812 |
} |
| 2813 |
} |
| 2814 |
while (worklist.length () > 0) |
| 2815 |
{ |
| 2816 |
gimple *stmt = worklist.pop (); |
| 2817 |
|
| 2818 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2819 |
{ |
| 2820 |
fprintf (dump_file, "processing: "); |
| 2821 |
print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
| 2822 |
fprintf (dump_file, "\n"); |
| 2823 |
} |
| 2824 |
if (gimple_code (stmt) == GIMPLE_PHI) |
| 2825 |
for (unsigned int k = 0; k < gimple_phi_num_args (stmt); k++) |
| 2826 |
{ |
| 2827 |
tree arg = PHI_ARG_DEF (stmt, k); |
| 2828 |
|
| 2829 |
if (TREE_CODE (arg) == SSA_NAME) |
| 2830 |
mark_operand_necessary (arg, worklist); |
| 2831 |
} |
| 2832 |
else |
| 2833 |
{ |
| 2834 |
ssa_op_iter iter; |
| 2835 |
tree use; |
| 2836 |
|
| 2837 |
FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE) |
| 2838 |
mark_operand_necessary (use, worklist); |
| 2839 |
} |
| 2840 |
} |
| 2841 |
} |
| 2842 |
|
| 2843 |
/* Analyze function body for NODE. |
| 2844 |
EARLY indicates run from early optimization pipeline. */ |
| 2845 |
|
| 2846 |
static void |
| 2847 |
analyze_function_body (struct cgraph_node *node, bool early) |
| 2848 |
{ |
| 2849 |
sreal time = opt_for_fn (node->decl, param_uninlined_function_time); |
| 2850 |
/* Estimate static overhead for function prologue/epilogue and alignment. */ |
| 2851 |
int size = opt_for_fn (node->decl, param_uninlined_function_insns); |
| 2852 |
/* Benefits are scaled by probability of elimination that is in range |
| 2853 |
<0,2>. */ |
| 2854 |
basic_block bb; |
| 2855 |
struct function *my_function = DECL_STRUCT_FUNCTION (node->decl); |
| 2856 |
sreal freq; |
| 2857 |
class ipa_fn_summary *info = ipa_fn_summaries->get_create (node); |
| 2858 |
ipa_node_params *params_summary |
| 2859 |
= early ? NULL : ipa_node_params_sum->get (node); |
| 2860 |
ipa_predicate bb_predicate; |
| 2861 |
struct ipa_func_body_info fbi; |
| 2862 |
vec<ipa_predicate> nonconstant_names = vNULL; |
| 2863 |
int nblocks, n; |
| 2864 |
int *order; |
| 2865 |
gimple *fix_builtin_expect_stmt; |
| 2866 |
|
| 2867 |
gcc_assert (my_function && my_function->cfg); |
| 2868 |
gcc_assert (cfun == my_function); |
| 2869 |
|
| 2870 |
memset(&fbi, 0, sizeof(fbi)); |
| 2871 |
vec_free (info->conds); |
| 2872 |
info->conds = NULL; |
| 2873 |
info->size_time_table.release (); |
| 2874 |
info->call_size_time_table.release (); |
| 2875 |
|
| 2876 |
/* When optimizing and analyzing for IPA inliner, initialize loop optimizer |
| 2877 |
so we can produce proper inline hints. |
| 2878 |
|
| 2879 |
When optimizing and analyzing for early inliner, initialize node params |
| 2880 |
so we can produce correct BB predicates. */ |
| 2881 |
|
| 2882 |
if (opt_for_fn (node->decl, optimize)) |
| 2883 |
{ |
| 2884 |
calculate_dominance_info (CDI_DOMINATORS); |
| 2885 |
calculate_dominance_info (CDI_POST_DOMINATORS); |
| 2886 |
if (!early) |
| 2887 |
loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); |
| 2888 |
else |
| 2889 |
{ |
| 2890 |
ipa_check_create_node_params (); |
| 2891 |
ipa_initialize_node_params (node); |
| 2892 |
} |
| 2893 |
|
| 2894 |
if (ipa_node_params_sum) |
| 2895 |
{ |
| 2896 |
fbi.node = node; |
| 2897 |
fbi.info = ipa_node_params_sum->get (node); |
| 2898 |
fbi.bb_infos = vNULL; |
| 2899 |
fbi.bb_infos.safe_grow_cleared (last_basic_block_for_fn (cfun), true); |
| 2900 |
fbi.param_count = count_formal_params (node->decl); |
| 2901 |
fbi.aa_walk_budget = opt_for_fn (node->decl, param_ipa_max_aa_steps); |
| 2902 |
|
| 2903 |
nonconstant_names.safe_grow_cleared |
| 2904 |
(SSANAMES (my_function)->length (), true); |
| 2905 |
} |
| 2906 |
} |
| 2907 |
|
| 2908 |
if (dump_file) |
| 2909 |
fprintf (dump_file, "\nAnalyzing function body size: %s\n", |
| 2910 |
node->dump_name ()); |
| 2911 |
|
| 2912 |
/* When we run into maximal number of entries, we assign everything to the |
| 2913 |
constant truth case. Be sure to have it in list. */ |
| 2914 |
bb_predicate = true; |
| 2915 |
info->account_size_time (0, 0, bb_predicate, bb_predicate); |
| 2916 |
|
| 2917 |
bb_predicate = ipa_predicate::not_inlined (); |
| 2918 |
info->account_size_time (opt_for_fn (node->decl, |
| 2919 |
param_uninlined_function_insns) |
| 2920 |
* ipa_fn_summary::size_scale, |
| 2921 |
opt_for_fn (node->decl, |
| 2922 |
param_uninlined_function_time), |
| 2923 |
bb_predicate, |
| 2924 |
bb_predicate); |
| 2925 |
|
| 2926 |
/* Only look for target information for inlinable functions. */ |
| 2927 |
bool scan_for_target_info = |
| 2928 |
info->inlinable |
| 2929 |
&& targetm.target_option.need_ipa_fn_target_info (node->decl, |
| 2930 |
info->target_info); |
| 2931 |
|
| 2932 |
if (fbi.info) |
| 2933 |
compute_bb_predicates (&fbi, node, info, params_summary); |
| 2934 |
find_necessary_statements (node); |
| 2935 |
const profile_count entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count; |
| 2936 |
order = XNEWVEC (int, n_basic_blocks_for_fn (cfun)); |
| 2937 |
nblocks = pre_and_rev_post_order_compute (NULL, order, false); |
| 2938 |
for (n = 0; n < nblocks; n++) |
| 2939 |
{ |
| 2940 |
bb = BASIC_BLOCK_FOR_FN (cfun, order[n]); |
| 2941 |
freq = bb->count.to_sreal_scale (entry_count); |
| 2942 |
if (clobber_only_eh_bb_p (bb)) |
| 2943 |
{ |
| 2944 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2945 |
fprintf (dump_file, "\n Ignoring BB %i;" |
| 2946 |
" it will be optimized away by cleanup_clobbers\n", |
| 2947 |
bb->index); |
| 2948 |
continue; |
| 2949 |
} |
| 2950 |
|
| 2951 |
/* TODO: Obviously predicates can be propagated down across CFG. */ |
| 2952 |
if (fbi.info) |
| 2953 |
{ |
| 2954 |
if (bb->aux) |
| 2955 |
bb_predicate = *(ipa_predicate *)bb->aux; |
| 2956 |
else |
| 2957 |
bb_predicate = false; |
| 2958 |
} |
| 2959 |
else |
| 2960 |
bb_predicate = true; |
| 2961 |
|
| 2962 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2963 |
{ |
| 2964 |
fprintf (dump_file, "\n BB %i predicate:", bb->index); |
| 2965 |
bb_predicate.dump (dump_file, info->conds); |
| 2966 |
} |
| 2967 |
|
| 2968 |
if (fbi.info && nonconstant_names.exists ()) |
| 2969 |
{ |
| 2970 |
ipa_predicate phi_predicate; |
| 2971 |
bool first_phi = true; |
| 2972 |
|
| 2973 |
for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); |
| 2974 |
gsi_next (&bsi)) |
| 2975 |
{ |
| 2976 |
if (first_phi |
| 2977 |
&& !phi_result_unknown_predicate (&fbi, info, |
| 2978 |
params_summary, |
| 2979 |
bb, |
| 2980 |
&phi_predicate, |
| 2981 |
nonconstant_names)) |
| 2982 |
break; |
| 2983 |
first_phi = false; |
| 2984 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2985 |
{ |
| 2986 |
fprintf (dump_file, " "); |
| 2987 |
print_gimple_stmt (dump_file, gsi_stmt (bsi), 0); |
| 2988 |
} |
| 2989 |
predicate_for_phi_result (info, bsi.phi (), &phi_predicate, |
| 2990 |
nonconstant_names); |
| 2991 |
} |
| 2992 |
} |
| 2993 |
|
| 2994 |
fix_builtin_expect_stmt = find_foldable_builtin_expect (bb); |
| 2995 |
|
| 2996 |
for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb); |
| 2997 |
!gsi_end_p (bsi); gsi_next_nondebug (&bsi)) |
| 2998 |
{ |
| 2999 |
gimple *stmt = gsi_stmt (bsi); |
| 3000 |
if (!gimple_plf (stmt, STMT_NECESSARY)) |
| 3001 |
{ |
| 3002 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3003 |
{ |
| 3004 |
fprintf (dump_file, " skipping unnecesary stmt "); |
| 3005 |
print_gimple_stmt (dump_file, stmt, 0); |
| 3006 |
} |
| 3007 |
/* TODO: const calls used only to produce values for |
| 3008 |
builtion_unreachable guards should not be accounted. However |
| 3009 |
we still want to inline them and this does does not work well |
| 3010 |
with the cost model. For now account them as usual. */ |
| 3011 |
if (!is_gimple_call (stmt) |
| 3012 |
|| gimple_call_internal_p (stmt)) |
| 3013 |
continue; |
| 3014 |
} |
| 3015 |
int this_size = estimate_num_insns (stmt, &eni_size_weights); |
| 3016 |
int this_time = estimate_num_insns (stmt, &eni_time_weights); |
| 3017 |
int prob; |
| 3018 |
ipa_predicate will_be_nonconstant; |
| 3019 |
|
| 3020 |
/* This relation stmt should be folded after we remove |
| 3021 |
__builtin_expect call. Adjust the cost here. */ |
| 3022 |
if (stmt == fix_builtin_expect_stmt) |
| 3023 |
{ |
| 3024 |
this_size--; |
| 3025 |
this_time--; |
| 3026 |
} |
| 3027 |
|
| 3028 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3029 |
{ |
| 3030 |
fprintf (dump_file, " "); |
| 3031 |
print_gimple_stmt (dump_file, stmt, 0); |
| 3032 |
fprintf (dump_file, "\t\tfreq:%3.2f size:%3i time:%3i\n", |
| 3033 |
freq.to_double (), this_size, |
| 3034 |
this_time); |
| 3035 |
} |
| 3036 |
|
| 3037 |
if (is_gimple_call (stmt) |
| 3038 |
&& !gimple_call_internal_p (stmt)) |
| 3039 |
{ |
| 3040 |
struct cgraph_edge *edge = node->get_edge (stmt); |
| 3041 |
ipa_call_summary *es = ipa_call_summaries->get_create (edge); |
| 3042 |
|
| 3043 |
/* Special case: results of BUILT_IN_CONSTANT_P will be always |
| 3044 |
resolved as constant. We however don't want to optimize |
| 3045 |
out the cgraph edges. */ |
| 3046 |
if (nonconstant_names.exists () |
| 3047 |
&& gimple_call_builtin_p (stmt, BUILT_IN_CONSTANT_P) |
| 3048 |
&& gimple_call_lhs (stmt) |
| 3049 |
&& TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME) |
| 3050 |
{ |
| 3051 |
ipa_predicate false_p = false; |
| 3052 |
nonconstant_names[SSA_NAME_VERSION (gimple_call_lhs (stmt))] |
| 3053 |
= false_p; |
| 3054 |
} |
| 3055 |
if (ipa_node_params_sum) |
| 3056 |
{ |
| 3057 |
int count = gimple_call_num_args (stmt); |
| 3058 |
int i; |
| 3059 |
|
| 3060 |
if (count) |
| 3061 |
es->param.safe_grow_cleared (count, true); |
| 3062 |
for (i = 0; i < count; i++) |
| 3063 |
{ |
| 3064 |
int prob = param_change_prob (&fbi, stmt, i); |
| 3065 |
gcc_assert (prob >= 0 && prob <= REG_BR_PROB_BASE); |
| 3066 |
es->param[i].change_prob = prob; |
| 3067 |
es->param[i].points_to_local_or_readonly_memory |
| 3068 |
= points_to_local_or_readonly_memory_p |
| 3069 |
(gimple_call_arg (stmt, i)); |
| 3070 |
es->param[i].points_to_possible_sra_candidate |
| 3071 |
= points_to_possible_sra_candidate_p |
| 3072 |
(gimple_call_arg (stmt, i)); |
| 3073 |
} |
| 3074 |
} |
| 3075 |
/* We cannot setup VLA parameters during inlining. */ |
| 3076 |
for (unsigned int i = 0; i < gimple_call_num_args (stmt); ++i) |
| 3077 |
if (TREE_CODE (gimple_call_arg (stmt, i)) == WITH_SIZE_EXPR) |
| 3078 |
{ |
| 3079 |
edge->inline_failed = CIF_FUNCTION_NOT_INLINABLE; |
| 3080 |
break; |
| 3081 |
} |
| 3082 |
es->call_stmt_size = this_size; |
| 3083 |
es->call_stmt_time = this_time; |
| 3084 |
es->loop_depth = bb_loop_depth (bb); |
| 3085 |
edge_set_predicate (edge, &bb_predicate); |
| 3086 |
if (edge->speculative) |
| 3087 |
{ |
| 3088 |
cgraph_edge *indirect |
| 3089 |
= edge->speculative_call_indirect_edge (); |
| 3090 |
ipa_call_summary *es2 |
| 3091 |
= ipa_call_summaries->get_create (indirect); |
| 3092 |
ipa_call_summaries->duplicate (edge, indirect, |
| 3093 |
es, es2); |
| 3094 |
|
| 3095 |
/* Edge is the first direct call. |
| 3096 |
create and duplicate call summaries for multiple |
| 3097 |
speculative call targets. */ |
| 3098 |
for (cgraph_edge *direct |
| 3099 |
= edge->next_speculative_call_target (); |
| 3100 |
direct; |
| 3101 |
direct = direct->next_speculative_call_target ()) |
| 3102 |
{ |
| 3103 |
ipa_call_summary *es3 |
| 3104 |
= ipa_call_summaries->get_create (direct); |
| 3105 |
ipa_call_summaries->duplicate (edge, direct, |
| 3106 |
es, es3); |
| 3107 |
} |
| 3108 |
} |
| 3109 |
} |
| 3110 |
|
| 3111 |
/* TODO: When conditional jump or switch is known to be constant, but |
| 3112 |
we did not translate it into the predicates, we really can account |
| 3113 |
just maximum of the possible paths. */ |
| 3114 |
if (fbi.info) |
| 3115 |
will_be_nonconstant |
| 3116 |
= will_be_nonconstant_predicate (&fbi, info, params_summary, |
| 3117 |
stmt, nonconstant_names); |
| 3118 |
else |
| 3119 |
will_be_nonconstant = true; |
| 3120 |
if (this_time || this_size) |
| 3121 |
{ |
| 3122 |
sreal final_time = (sreal)this_time * freq; |
| 3123 |
prob = eliminated_by_inlining_prob (&fbi, stmt); |
| 3124 |
if (prob == 1 && dump_file && (dump_flags & TDF_DETAILS)) |
| 3125 |
fprintf (dump_file, |
| 3126 |
"\t\t50%% will be eliminated by inlining\n"); |
| 3127 |
if (prob == 2 && dump_file && (dump_flags & TDF_DETAILS)) |
| 3128 |
fprintf (dump_file, "\t\tWill be eliminated by inlining\n"); |
| 3129 |
|
| 3130 |
ipa_predicate p = bb_predicate & will_be_nonconstant; |
| 3131 |
int parm = load_or_store_of_ptr_parameter (&fbi, stmt); |
| 3132 |
ipa_predicate sra_predicate = true; |
| 3133 |
if (parm != -1) |
| 3134 |
sra_predicate &= add_condition (info, params_summary, parm, |
| 3135 |
ptr_type_node, NULL, |
| 3136 |
ipa_predicate::not_sra_candidate, NULL, 0); |
| 3137 |
|
| 3138 |
/* We can ignore statement when we proved it is never going |
| 3139 |
to happen, but we cannot do that for call statements |
| 3140 |
because edges are accounted specially. */ |
| 3141 |
|
| 3142 |
if (*(is_gimple_call (stmt) ? &bb_predicate : &p) != false) |
| 3143 |
{ |
| 3144 |
time += final_time; |
| 3145 |
size += this_size; |
| 3146 |
} |
| 3147 |
|
| 3148 |
/* We account everything but the calls. Calls have their own |
| 3149 |
size/time info attached to cgraph edges. This is necessary |
| 3150 |
in order to make the cost disappear after inlining. */ |
| 3151 |
if (!is_gimple_call (stmt)) |
| 3152 |
{ |
| 3153 |
if (prob) |
| 3154 |
{ |
| 3155 |
ipa_predicate ip |
| 3156 |
= bb_predicate & ipa_predicate::not_inlined () & sra_predicate; |
| 3157 |
info->account_size_time (this_size * prob, |
| 3158 |
(final_time * prob) / 2, ip, |
| 3159 |
p); |
| 3160 |
} |
| 3161 |
if (prob != 2) |
| 3162 |
info->account_size_time (this_size * (2 - prob), |
| 3163 |
(final_time * (2 - prob) / 2), |
| 3164 |
bb_predicate & sra_predicate, |
| 3165 |
p); |
| 3166 |
} |
| 3167 |
|
| 3168 |
if (!info->fp_expressions && fp_expression_p (stmt)) |
| 3169 |
{ |
| 3170 |
info->fp_expressions = true; |
| 3171 |
if (dump_file) |
| 3172 |
fprintf (dump_file, " fp_expression set\n"); |
| 3173 |
} |
| 3174 |
} |
| 3175 |
|
| 3176 |
/* For target specific information, we want to scan all statements |
| 3177 |
rather than those statements with non-zero weights, to avoid |
| 3178 |
missing to scan something interesting for target information, |
| 3179 |
such as: internal function calls. */ |
| 3180 |
if (scan_for_target_info) |
| 3181 |
scan_for_target_info = |
| 3182 |
targetm.target_option.update_ipa_fn_target_info |
| 3183 |
(info->target_info, stmt); |
| 3184 |
|
| 3185 |
/* Account cost of address calculations in the statements. */ |
| 3186 |
for (unsigned int i = 0; i < gimple_num_ops (stmt); i++) |
| 3187 |
{ |
| 3188 |
for (tree op = gimple_op (stmt, i); |
| 3189 |
op && handled_component_p (op); |
| 3190 |
op = TREE_OPERAND (op, 0)) |
| 3191 |
if ((TREE_CODE (op) == ARRAY_REF |
| 3192 |
|| TREE_CODE (op) == ARRAY_RANGE_REF) |
| 3193 |
&& TREE_CODE (TREE_OPERAND (op, 1)) == SSA_NAME) |
| 3194 |
{ |
| 3195 |
ipa_predicate p = bb_predicate; |
| 3196 |
if (fbi.info) |
| 3197 |
p = p & will_be_nonconstant_expr_predicate |
| 3198 |
(&fbi, info, params_summary, |
| 3199 |
TREE_OPERAND (op, 1), |
| 3200 |
nonconstant_names); |
| 3201 |
if (p != false) |
| 3202 |
{ |
| 3203 |
time += freq; |
| 3204 |
size += 1; |
| 3205 |
if (dump_file) |
| 3206 |
fprintf (dump_file, |
| 3207 |
"\t\tAccounting address calculation.\n"); |
| 3208 |
info->account_size_time (ipa_fn_summary::size_scale, |
| 3209 |
freq, |
| 3210 |
bb_predicate, |
| 3211 |
p); |
| 3212 |
} |
| 3213 |
} |
| 3214 |
} |
| 3215 |
|
| 3216 |
} |
| 3217 |
} |
| 3218 |
free (order); |
| 3219 |
|
| 3220 |
if (nonconstant_names.exists () && !early) |
| 3221 |
{ |
| 3222 |
ipa_fn_summary *s = ipa_fn_summaries->get (node); |
| 3223 |
unsigned max_loop_predicates = opt_for_fn (node->decl, |
| 3224 |
param_ipa_max_loop_predicates); |
| 3225 |
|
| 3226 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3227 |
flow_loops_dump (dump_file, NULL, 0); |
| 3228 |
scev_initialize (); |
| 3229 |
for (auto loop : loops_list (cfun, 0)) |
| 3230 |
{ |
| 3231 |
ipa_predicate loop_iterations = true; |
| 3232 |
sreal header_freq; |
| 3233 |
edge ex; |
| 3234 |
unsigned int j; |
| 3235 |
class tree_niter_desc niter_desc; |
| 3236 |
if (!loop->header->aux) |
| 3237 |
continue; |
| 3238 |
|
| 3239 |
profile_count phdr_count = loop_preheader_edge (loop)->count (); |
| 3240 |
sreal phdr_freq = phdr_count.to_sreal_scale (entry_count); |
| 3241 |
|
| 3242 |
bb_predicate = *(ipa_predicate *)loop->header->aux; |
| 3243 |
auto_vec<edge> exits = get_loop_exit_edges (loop); |
| 3244 |
FOR_EACH_VEC_ELT (exits, j, ex) |
| 3245 |
if (number_of_iterations_exit (loop, ex, &niter_desc, false) |
| 3246 |
&& !is_gimple_min_invariant (niter_desc.niter)) |
| 3247 |
{ |
| 3248 |
ipa_predicate will_be_nonconstant |
| 3249 |
= will_be_nonconstant_expr_predicate (&fbi, info, |
| 3250 |
params_summary, |
| 3251 |
niter_desc.niter, |
| 3252 |
nonconstant_names); |
| 3253 |
if (will_be_nonconstant != true) |
| 3254 |
will_be_nonconstant = bb_predicate & will_be_nonconstant; |
| 3255 |
if (will_be_nonconstant != true |
| 3256 |
&& will_be_nonconstant != false) |
| 3257 |
loop_iterations &= will_be_nonconstant; |
| 3258 |
} |
| 3259 |
add_freqcounting_predicate (&s->loop_iterations, loop_iterations, |
| 3260 |
phdr_freq, max_loop_predicates); |
| 3261 |
} |
| 3262 |
|
| 3263 |
/* To avoid quadratic behavior we analyze stride predicates only |
| 3264 |
with respect to the containing loop. Thus we simply iterate |
| 3265 |
over all defs in the outermost loop body. */ |
| 3266 |
for (class loop *loop = loops_for_fn (cfun)->tree_root->inner; |
| 3267 |
loop != NULL; loop = loop->next) |
| 3268 |
{ |
| 3269 |
ipa_predicate loop_stride = true; |
| 3270 |
basic_block *body = get_loop_body (loop); |
| 3271 |
profile_count phdr_count = loop_preheader_edge (loop)->count (); |
| 3272 |
sreal phdr_freq = phdr_count.to_sreal_scale (entry_count); |
| 3273 |
for (unsigned i = 0; i < loop->num_nodes; i++) |
| 3274 |
{ |
| 3275 |
gimple_stmt_iterator gsi; |
| 3276 |
if (!body[i]->aux) |
| 3277 |
continue; |
| 3278 |
|
| 3279 |
bb_predicate = *(ipa_predicate *)body[i]->aux; |
| 3280 |
for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); |
| 3281 |
gsi_next (&gsi)) |
| 3282 |
{ |
| 3283 |
gimple *stmt = gsi_stmt (gsi); |
| 3284 |
|
| 3285 |
if (!is_gimple_assign (stmt)) |
| 3286 |
continue; |
| 3287 |
|
| 3288 |
tree def = gimple_assign_lhs (stmt); |
| 3289 |
if (TREE_CODE (def) != SSA_NAME) |
| 3290 |
continue; |
| 3291 |
|
| 3292 |
affine_iv iv; |
| 3293 |
if (!simple_iv (loop_containing_stmt (stmt), |
| 3294 |
loop_containing_stmt (stmt), |
| 3295 |
def, &iv, true) |
| 3296 |
|| is_gimple_min_invariant (iv.step)) |
| 3297 |
continue; |
| 3298 |
|
| 3299 |
ipa_predicate will_be_nonconstant |
| 3300 |
= will_be_nonconstant_expr_predicate (&fbi, info, |
| 3301 |
params_summary, |
| 3302 |
iv.step, |
| 3303 |
nonconstant_names); |
| 3304 |
if (will_be_nonconstant != true) |
| 3305 |
will_be_nonconstant = bb_predicate & will_be_nonconstant; |
| 3306 |
if (will_be_nonconstant != true |
| 3307 |
&& will_be_nonconstant != false) |
| 3308 |
loop_stride = loop_stride & will_be_nonconstant; |
| 3309 |
} |
| 3310 |
} |
| 3311 |
add_freqcounting_predicate (&s->loop_strides, loop_stride, |
| 3312 |
phdr_freq, max_loop_predicates); |
| 3313 |
free (body); |
| 3314 |
} |
| 3315 |
scev_finalize (); |
| 3316 |
} |
| 3317 |
FOR_ALL_BB_FN (bb, my_function) |
| 3318 |
{ |
| 3319 |
edge e; |
| 3320 |
edge_iterator ei; |
| 3321 |
|
| 3322 |
if (bb->aux) |
| 3323 |
edge_predicate_pool.remove ((ipa_predicate *)bb->aux); |
| 3324 |
bb->aux = NULL; |
| 3325 |
FOR_EACH_EDGE (e, ei, bb->succs) |
| 3326 |
{ |
| 3327 |
if (e->aux) |
| 3328 |
edge_predicate_pool.remove ((ipa_predicate *)e->aux); |
| 3329 |
e->aux = NULL; |
| 3330 |
} |
| 3331 |
} |
| 3332 |
ipa_fn_summary *s = ipa_fn_summaries->get (node); |
| 3333 |
ipa_size_summary *ss = ipa_size_summaries->get (node); |
| 3334 |
s->time = time; |
| 3335 |
ss->self_size = size; |
| 3336 |
nonconstant_names.release (); |
| 3337 |
ipa_release_body_info (&fbi); |
| 3338 |
if (opt_for_fn (node->decl, optimize)) |
| 3339 |
{ |
| 3340 |
if (!early) |
| 3341 |
loop_optimizer_finalize (); |
| 3342 |
else if (!ipa_edge_args_sum) |
| 3343 |
ipa_free_all_node_params (); |
| 3344 |
free_dominance_info (CDI_DOMINATORS); |
| 3345 |
free_dominance_info (CDI_POST_DOMINATORS); |
| 3346 |
} |
| 3347 |
if (dump_file) |
| 3348 |
{ |
| 3349 |
fprintf (dump_file, "\n"); |
| 3350 |
ipa_dump_fn_summary (dump_file, node); |
| 3351 |
} |
| 3352 |
} |
| 3353 |
|
| 3354 |
|
| 3355 |
/* Compute function summary. |
| 3356 |
EARLY is true when we compute parameters during early opts. */ |
| 3357 |
|
| 3358 |
void |
| 3359 |
compute_fn_summary (struct cgraph_node *node, bool early) |
| 3360 |
{ |
| 3361 |
HOST_WIDE_INT self_stack_size; |
| 3362 |
struct cgraph_edge *e; |
| 3363 |
|
| 3364 |
gcc_assert (!node->inlined_to); |
| 3365 |
|
| 3366 |
if (!ipa_fn_summaries) |
| 3367 |
ipa_fn_summary_alloc (); |
| 3368 |
|
| 3369 |
/* Create a new ipa_fn_summary. */ |
| 3370 |
((ipa_fn_summary_t *)ipa_fn_summaries)->remove_callees (node); |
| 3371 |
ipa_fn_summaries->remove (node); |
| 3372 |
class ipa_fn_summary *info = ipa_fn_summaries->get_create (node); |
| 3373 |
class ipa_size_summary *size_info = ipa_size_summaries->get_create (node); |
| 3374 |
|
| 3375 |
/* Estimate the stack size for the function if we're optimizing. */ |
| 3376 |
self_stack_size = optimize && !node->thunk |
| 3377 |
? estimated_stack_frame_size (node) : 0; |
| 3378 |
size_info->estimated_self_stack_size = self_stack_size; |
| 3379 |
info->estimated_stack_size = self_stack_size; |
| 3380 |
|
| 3381 |
if (node->thunk) |
| 3382 |
{ |
| 3383 |
ipa_call_summary *es = ipa_call_summaries->get_create (node->callees); |
| 3384 |
ipa_predicate t = true; |
| 3385 |
|
| 3386 |
node->can_change_signature = false; |
| 3387 |
es->call_stmt_size = eni_size_weights.call_cost; |
| 3388 |
es->call_stmt_time = eni_time_weights.call_cost; |
| 3389 |
info->account_size_time (ipa_fn_summary::size_scale |
| 3390 |
* opt_for_fn (node->decl, |
| 3391 |
param_uninlined_function_thunk_insns), |
| 3392 |
opt_for_fn (node->decl, |
| 3393 |
param_uninlined_function_thunk_time), t, t); |
| 3394 |
t = ipa_predicate::not_inlined (); |
| 3395 |
info->account_size_time (2 * ipa_fn_summary::size_scale, 0, t, t); |
| 3396 |
ipa_update_overall_fn_summary (node); |
| 3397 |
size_info->self_size = size_info->size; |
| 3398 |
if (stdarg_p (TREE_TYPE (node->decl))) |
| 3399 |
{ |
| 3400 |
info->inlinable = false; |
| 3401 |
node->callees->inline_failed = CIF_VARIADIC_THUNK; |
| 3402 |
} |
| 3403 |
else |
| 3404 |
info->inlinable = true; |
| 3405 |
} |
| 3406 |
else |
| 3407 |
{ |
| 3408 |
/* Even is_gimple_min_invariant rely on current_function_decl. */ |
| 3409 |
push_cfun (DECL_STRUCT_FUNCTION (node->decl)); |
| 3410 |
|
| 3411 |
/* During IPA profile merging we may be called w/o virtual SSA form |
| 3412 |
built. */ |
| 3413 |
update_ssa (TODO_update_ssa_only_virtuals); |
| 3414 |
|
| 3415 |
/* Can this function be inlined at all? */ |
| 3416 |
if (!opt_for_fn (node->decl, optimize) |
| 3417 |
&& !lookup_attribute ("always_inline", |
| 3418 |
DECL_ATTRIBUTES (node->decl))) |
| 3419 |
info->inlinable = false; |
| 3420 |
else |
| 3421 |
info->inlinable = tree_inlinable_function_p (node->decl); |
| 3422 |
|
| 3423 |
bool no_signature = false; |
| 3424 |
|
| 3425 |
/* Don't allow signature changes for functions which have |
| 3426 |
[[gnu::musttail]] or [[clang::musttail]] calls. Sometimes |
| 3427 |
(more often on targets which pass everything on the stack) |
| 3428 |
signature changes can result in tail calls being impossible |
| 3429 |
even when without the signature changes they would be ok. |
| 3430 |
See PR121023. */ |
| 3431 |
if (cfun->has_musttail) |
| 3432 |
{ |
| 3433 |
if (dump_file) |
| 3434 |
fprintf (dump_file, "No signature change:" |
| 3435 |
" function has calls with musttail attribute.\n"); |
| 3436 |
no_signature = true; |
| 3437 |
} |
| 3438 |
|
| 3439 |
/* Type attributes can use parameter indices to describe them. |
| 3440 |
Special case fn spec since we can safely preserve them in |
| 3441 |
modref summaries. */ |
| 3442 |
for (tree list = TYPE_ATTRIBUTES (TREE_TYPE (node->decl)); |
| 3443 |
list && !no_signature; list = TREE_CHAIN (list)) |
| 3444 |
if (!ipa_param_adjustments::type_attribute_allowed_p |
| 3445 |
(get_attribute_name (list))) |
| 3446 |
{ |
| 3447 |
if (dump_file) |
| 3448 |
{ |
| 3449 |
fprintf (dump_file, "No signature change:" |
| 3450 |
" function type has unhandled attribute %s.\n", |
| 3451 |
IDENTIFIER_POINTER (get_attribute_name (list))); |
| 3452 |
} |
| 3453 |
no_signature = true; |
| 3454 |
} |
| 3455 |
for (tree parm = DECL_ARGUMENTS (node->decl); |
| 3456 |
parm && !no_signature; parm = DECL_CHAIN (parm)) |
| 3457 |
if (variably_modified_type_p (TREE_TYPE (parm), node->decl)) |
| 3458 |
{ |
| 3459 |
if (dump_file) |
| 3460 |
{ |
| 3461 |
fprintf (dump_file, "No signature change:" |
| 3462 |
" has parameter with variably modified type.\n"); |
| 3463 |
} |
| 3464 |
no_signature = true; |
| 3465 |
} |
| 3466 |
|
| 3467 |
/* Likewise for #pragma omp declare simd functions or functions |
| 3468 |
with simd attribute. */ |
| 3469 |
if (no_signature |
| 3470 |
|| lookup_attribute ("omp declare simd", |
| 3471 |
DECL_ATTRIBUTES (node->decl))) |
| 3472 |
node->can_change_signature = false; |
| 3473 |
else |
| 3474 |
{ |
| 3475 |
/* Otherwise, inlinable functions always can change signature. */ |
| 3476 |
if (info->inlinable) |
| 3477 |
node->can_change_signature = true; |
| 3478 |
else |
| 3479 |
{ |
| 3480 |
/* Functions calling builtin_apply cannot change signature. */ |
| 3481 |
for (e = node->callees; e; e = e->next_callee) |
| 3482 |
{ |
| 3483 |
tree cdecl = e->callee->decl; |
| 3484 |
if (fndecl_built_in_p (cdecl, BUILT_IN_APPLY_ARGS, |
| 3485 |
BUILT_IN_VA_START)) |
| 3486 |
break; |
| 3487 |
} |
| 3488 |
node->can_change_signature = !e; |
| 3489 |
} |
| 3490 |
} |
| 3491 |
analyze_function_body (node, early); |
| 3492 |
pop_cfun (); |
| 3493 |
} |
| 3494 |
|
| 3495 |
/* Inlining characteristics are maintained by the cgraph_mark_inline. */ |
| 3496 |
size_info->size = size_info->self_size; |
| 3497 |
info->estimated_stack_size = size_info->estimated_self_stack_size; |
| 3498 |
|
| 3499 |
/* Code above should compute exactly the same result as |
| 3500 |
ipa_update_overall_fn_summary except for case when speculative |
| 3501 |
edges are present since these are accounted to size but not |
| 3502 |
self_size. Do not compare time since different order the roundoff |
| 3503 |
errors result in slight changes. */ |
| 3504 |
ipa_update_overall_fn_summary (node); |
| 3505 |
if (flag_checking) |
| 3506 |
{ |
| 3507 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 3508 |
if (e->speculative) |
| 3509 |
break; |
| 3510 |
gcc_assert (e || size_info->size == size_info->self_size); |
| 3511 |
} |
| 3512 |
} |
| 3513 |
|
| 3514 |
|
| 3515 |
/* Compute parameters of functions used by inliner using |
| 3516 |
current_function_decl. */ |
| 3517 |
|
| 3518 |
static unsigned int |
| 3519 |
compute_fn_summary_for_current (void) |
| 3520 |
{ |
| 3521 |
compute_fn_summary (cgraph_node::get (current_function_decl), true); |
| 3522 |
return 0; |
| 3523 |
} |
| 3524 |
|
| 3525 |
/* Estimate benefit devirtualizing indirect edge IE and return true if it can |
| 3526 |
be devirtualized and inlined, provided m_known_vals, m_known_contexts and |
| 3527 |
m_known_aggs in AVALS. Return false straight away if AVALS is NULL. */ |
| 3528 |
|
| 3529 |
static bool |
| 3530 |
estimate_edge_devirt_benefit (struct cgraph_edge *ie, |
| 3531 |
int *size, int *time, |
| 3532 |
ipa_call_arg_values *avals) |
| 3533 |
{ |
| 3534 |
tree target; |
| 3535 |
struct cgraph_node *callee; |
| 3536 |
class ipa_fn_summary *isummary; |
| 3537 |
enum availability avail; |
| 3538 |
bool speculative; |
| 3539 |
|
| 3540 |
if (!avals |
| 3541 |
|| (!avals->m_known_vals.length() && !avals->m_known_contexts.length ())) |
| 3542 |
return false; |
| 3543 |
if (!opt_for_fn (ie->caller->decl, flag_indirect_inlining)) |
| 3544 |
return false; |
| 3545 |
|
| 3546 |
target = ipa_get_indirect_edge_target (ie, avals, &speculative); |
| 3547 |
if (!target || speculative) |
| 3548 |
return false; |
| 3549 |
|
| 3550 |
/* Account for difference in cost between indirect and direct calls. */ |
| 3551 |
*size -= (eni_size_weights.indirect_call_cost - eni_size_weights.call_cost); |
| 3552 |
*time -= (eni_time_weights.indirect_call_cost - eni_time_weights.call_cost); |
| 3553 |
gcc_checking_assert (*time >= 0); |
| 3554 |
gcc_checking_assert (*size >= 0); |
| 3555 |
|
| 3556 |
callee = cgraph_node::get (target); |
| 3557 |
if (!callee || !callee->definition) |
| 3558 |
return false; |
| 3559 |
callee = callee->function_symbol (&avail); |
| 3560 |
if (avail < AVAIL_AVAILABLE) |
| 3561 |
return false; |
| 3562 |
isummary = ipa_fn_summaries->get (callee); |
| 3563 |
if (isummary == NULL) |
| 3564 |
return false; |
| 3565 |
|
| 3566 |
return isummary->inlinable; |
| 3567 |
} |
| 3568 |
|
| 3569 |
/* Increase SIZE, MIN_SIZE (if non-NULL) and TIME for size and time needed to |
| 3570 |
handle edge E with probability PROB. Set HINTS accordingly if edge may be |
| 3571 |
devirtualized. AVALS, if non-NULL, describes the context of the call site |
| 3572 |
as far as values of parameters are concerened. */ |
| 3573 |
|
| 3574 |
static inline void |
| 3575 |
estimate_edge_size_and_time (struct cgraph_edge *e, int *size, int *min_size, |
| 3576 |
sreal *time, ipa_call_arg_values *avals, |
| 3577 |
ipa_hints *hints) |
| 3578 |
{ |
| 3579 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 3580 |
int call_size = es->call_stmt_size; |
| 3581 |
int call_time = es->call_stmt_time; |
| 3582 |
int cur_size; |
| 3583 |
|
| 3584 |
if (!e->callee && hints && e->maybe_hot_p () |
| 3585 |
&& estimate_edge_devirt_benefit (e, &call_size, &call_time, avals)) |
| 3586 |
*hints |= INLINE_HINT_indirect_call; |
| 3587 |
cur_size = call_size * ipa_fn_summary::size_scale; |
| 3588 |
*size += cur_size; |
| 3589 |
if (min_size) |
| 3590 |
*min_size += cur_size; |
| 3591 |
if (time) |
| 3592 |
*time += ((sreal)call_time) * e->sreal_frequency (); |
| 3593 |
} |
| 3594 |
|
| 3595 |
|
| 3596 |
/* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all |
| 3597 |
calls in NODE. POSSIBLE_TRUTHS and AVALS describe the context of the call |
| 3598 |
site. |
| 3599 |
|
| 3600 |
Helper for estimate_calls_size_and_time which does the same but |
| 3601 |
(in most cases) faster. */ |
| 3602 |
|
| 3603 |
static void |
| 3604 |
estimate_calls_size_and_time_1 (struct cgraph_node *node, int *size, |
| 3605 |
int *min_size, sreal *time, |
| 3606 |
ipa_hints *hints, |
| 3607 |
clause_t possible_truths, |
| 3608 |
ipa_call_arg_values *avals) |
| 3609 |
{ |
| 3610 |
struct cgraph_edge *e; |
| 3611 |
for (e = node->callees; e; e = e->next_callee) |
| 3612 |
{ |
| 3613 |
if (!e->inline_failed) |
| 3614 |
{ |
| 3615 |
gcc_checking_assert (!ipa_call_summaries->get (e)); |
| 3616 |
estimate_calls_size_and_time_1 (e->callee, size, min_size, time, |
| 3617 |
hints, possible_truths, avals); |
| 3618 |
|
| 3619 |
continue; |
| 3620 |
} |
| 3621 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 3622 |
|
| 3623 |
/* Do not care about zero sized builtins. */ |
| 3624 |
if (!es->call_stmt_size) |
| 3625 |
{ |
| 3626 |
gcc_checking_assert (!es->call_stmt_time); |
| 3627 |
continue; |
| 3628 |
} |
| 3629 |
if (!es->predicate |
| 3630 |
|| es->predicate->evaluate (possible_truths)) |
| 3631 |
{ |
| 3632 |
/* Predicates of calls shall not use NOT_CHANGED codes, |
| 3633 |
so we do not need to compute probabilities. */ |
| 3634 |
estimate_edge_size_and_time (e, size, |
| 3635 |
es->predicate ? NULL : min_size, |
| 3636 |
time, avals, hints); |
| 3637 |
} |
| 3638 |
} |
| 3639 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 3640 |
{ |
| 3641 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 3642 |
if (!es->predicate |
| 3643 |
|| es->predicate->evaluate (possible_truths)) |
| 3644 |
estimate_edge_size_and_time (e, size, |
| 3645 |
es->predicate ? NULL : min_size, |
| 3646 |
time, avals, hints); |
| 3647 |
} |
| 3648 |
} |
| 3649 |
|
| 3650 |
/* Populate sum->call_size_time_table for edges from NODE. */ |
| 3651 |
|
| 3652 |
static void |
| 3653 |
summarize_calls_size_and_time (struct cgraph_node *node, |
| 3654 |
ipa_fn_summary *sum) |
| 3655 |
{ |
| 3656 |
struct cgraph_edge *e; |
| 3657 |
for (e = node->callees; e; e = e->next_callee) |
| 3658 |
{ |
| 3659 |
if (!e->inline_failed) |
| 3660 |
{ |
| 3661 |
gcc_checking_assert (!ipa_call_summaries->get (e)); |
| 3662 |
summarize_calls_size_and_time (e->callee, sum); |
| 3663 |
continue; |
| 3664 |
} |
| 3665 |
int size = 0; |
| 3666 |
sreal time = 0; |
| 3667 |
|
| 3668 |
estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL); |
| 3669 |
|
| 3670 |
ipa_predicate pred = true; |
| 3671 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 3672 |
|
| 3673 |
if (es->predicate) |
| 3674 |
pred = *es->predicate; |
| 3675 |
sum->account_size_time (size, time, pred, pred, true); |
| 3676 |
} |
| 3677 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 3678 |
{ |
| 3679 |
int size = 0; |
| 3680 |
sreal time = 0; |
| 3681 |
|
| 3682 |
estimate_edge_size_and_time (e, &size, NULL, &time, NULL, NULL); |
| 3683 |
ipa_predicate pred = true; |
| 3684 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 3685 |
|
| 3686 |
if (es->predicate) |
| 3687 |
pred = *es->predicate; |
| 3688 |
sum->account_size_time (size, time, pred, pred, true); |
| 3689 |
} |
| 3690 |
} |
| 3691 |
|
| 3692 |
/* Increase SIZE, MIN_SIZE and TIME for size and time needed to handle all |
| 3693 |
calls in NODE. POSSIBLE_TRUTHS and AVALS (the latter if non-NULL) describe |
| 3694 |
context of the call site. */ |
| 3695 |
|
| 3696 |
static void |
| 3697 |
estimate_calls_size_and_time (struct cgraph_node *node, int *size, |
| 3698 |
int *min_size, sreal *time, |
| 3699 |
ipa_hints *hints, |
| 3700 |
clause_t possible_truths, |
| 3701 |
ipa_call_arg_values *avals) |
| 3702 |
{ |
| 3703 |
class ipa_fn_summary *sum = ipa_fn_summaries->get (node); |
| 3704 |
bool use_table = true; |
| 3705 |
|
| 3706 |
gcc_assert (node->callees || node->indirect_calls); |
| 3707 |
|
| 3708 |
/* During early inlining we do not calculate info for very |
| 3709 |
large functions and thus there is no need for producing |
| 3710 |
summaries. */ |
| 3711 |
if (!ipa_node_params_sum) |
| 3712 |
use_table = false; |
| 3713 |
/* Do not calculate summaries for simple wrappers; it is waste |
| 3714 |
of memory. */ |
| 3715 |
else if (node->callees && node->indirect_calls |
| 3716 |
&& node->callees->inline_failed && !node->callees->next_callee) |
| 3717 |
use_table = false; |
| 3718 |
/* If there is an indirect edge that may be optimized, we need |
| 3719 |
to go the slow way. */ |
| 3720 |
else if (avals && hints |
| 3721 |
&& (avals->m_known_vals.length () |
| 3722 |
|| avals->m_known_contexts.length () |
| 3723 |
|| avals->m_known_aggs.length ())) |
| 3724 |
{ |
| 3725 |
ipa_node_params *params_summary = ipa_node_params_sum->get (node); |
| 3726 |
unsigned int nargs = params_summary |
| 3727 |
? ipa_get_param_count (params_summary) : 0; |
| 3728 |
|
| 3729 |
for (unsigned int i = 0; i < nargs && use_table; i++) |
| 3730 |
{ |
| 3731 |
if (ipa_is_param_used_by_indirect_call (params_summary, i) |
| 3732 |
&& (avals->safe_sval_at (i) |
| 3733 |
|| (ipa_argagg_value_list (avals).value_for_index_p (i)))) |
| 3734 |
use_table = false; |
| 3735 |
else if (ipa_is_param_used_by_polymorphic_call (params_summary, i) |
| 3736 |
&& (avals->m_known_contexts.length () > i |
| 3737 |
&& !avals->m_known_contexts[i].useless_p ())) |
| 3738 |
use_table = false; |
| 3739 |
} |
| 3740 |
} |
| 3741 |
|
| 3742 |
/* Fast path is via the call size time table. */ |
| 3743 |
if (use_table) |
| 3744 |
{ |
| 3745 |
/* Build summary if it is absent. */ |
| 3746 |
if (!sum->call_size_time_table.length ()) |
| 3747 |
{ |
| 3748 |
ipa_predicate true_pred = true; |
| 3749 |
sum->account_size_time (0, 0, true_pred, true_pred, true); |
| 3750 |
summarize_calls_size_and_time (node, sum); |
| 3751 |
} |
| 3752 |
|
| 3753 |
int old_size = *size; |
| 3754 |
sreal old_time = time ? *time : 0; |
| 3755 |
|
| 3756 |
if (min_size) |
| 3757 |
*min_size += sum->call_size_time_table[0].size; |
| 3758 |
|
| 3759 |
unsigned int i; |
| 3760 |
size_time_entry *e; |
| 3761 |
|
| 3762 |
/* Walk the table and account sizes and times. */ |
| 3763 |
for (i = 0; sum->call_size_time_table.iterate (i, &e); |
| 3764 |
i++) |
| 3765 |
if (e->exec_predicate.evaluate (possible_truths)) |
| 3766 |
{ |
| 3767 |
*size += e->size; |
| 3768 |
if (time) |
| 3769 |
*time += e->time; |
| 3770 |
} |
| 3771 |
|
| 3772 |
/* Be careful and see if both methods agree. */ |
| 3773 |
if ((flag_checking || dump_file) |
| 3774 |
/* Do not try to sanity check when we know we lost some |
| 3775 |
precision. */ |
| 3776 |
&& sum->call_size_time_table.length () |
| 3777 |
< ipa_fn_summary::max_size_time_table_size) |
| 3778 |
{ |
| 3779 |
estimate_calls_size_and_time_1 (node, &old_size, NULL, &old_time, NULL, |
| 3780 |
possible_truths, avals); |
| 3781 |
gcc_assert (*size == old_size); |
| 3782 |
if (time && (*time - old_time > 1 || *time - old_time < -1) |
| 3783 |
&& dump_file) |
| 3784 |
fprintf (dump_file, "Time mismatch in call summary %f!=%f\n", |
| 3785 |
old_time.to_double (), |
| 3786 |
time->to_double ()); |
| 3787 |
} |
| 3788 |
} |
| 3789 |
/* Slow path by walking all edges. */ |
| 3790 |
else |
| 3791 |
estimate_calls_size_and_time_1 (node, size, min_size, time, hints, |
| 3792 |
possible_truths, avals); |
| 3793 |
} |
| 3794 |
|
| 3795 |
/* Main constructor for ipa call context. Memory allocation of ARG_VALUES |
| 3796 |
is owned by the caller. INLINE_PARAM_SUMMARY is also owned by the |
| 3797 |
caller. */ |
| 3798 |
|
| 3799 |
ipa_call_context::ipa_call_context (cgraph_node *node, clause_t possible_truths, |
| 3800 |
clause_t nonspec_possible_truths, |
| 3801 |
vec<inline_param_summary> |
| 3802 |
inline_param_summary, |
| 3803 |
ipa_auto_call_arg_values *arg_values) |
| 3804 |
: m_node (node), m_possible_truths (possible_truths), |
| 3805 |
m_nonspec_possible_truths (nonspec_possible_truths), |
| 3806 |
m_inline_param_summary (inline_param_summary), |
| 3807 |
m_avals (arg_values) |
| 3808 |
{ |
| 3809 |
} |
| 3810 |
|
| 3811 |
/* Set THIS to be a duplicate of CTX. Copy all relevant info. */ |
| 3812 |
|
| 3813 |
void |
| 3814 |
ipa_cached_call_context::duplicate_from (const ipa_call_context &ctx) |
| 3815 |
{ |
| 3816 |
m_node = ctx.m_node; |
| 3817 |
m_possible_truths = ctx.m_possible_truths; |
| 3818 |
m_nonspec_possible_truths = ctx.m_nonspec_possible_truths; |
| 3819 |
ipa_node_params *params_summary = ipa_node_params_sum->get (m_node); |
| 3820 |
unsigned int nargs = params_summary |
| 3821 |
? ipa_get_param_count (params_summary) : 0; |
| 3822 |
|
| 3823 |
m_inline_param_summary = vNULL; |
| 3824 |
/* Copy the info only if there is at least one useful entry. */ |
| 3825 |
if (ctx.m_inline_param_summary.exists ()) |
| 3826 |
{ |
| 3827 |
unsigned int n = MIN (ctx.m_inline_param_summary.length (), nargs); |
| 3828 |
|
| 3829 |
for (unsigned int i = 0; i < n; i++) |
| 3830 |
if (ipa_is_param_used_by_ipa_predicates (params_summary, i) |
| 3831 |
&& !ctx.m_inline_param_summary[i].useless_p ()) |
| 3832 |
{ |
| 3833 |
m_inline_param_summary |
| 3834 |
= ctx.m_inline_param_summary.copy (); |
| 3835 |
break; |
| 3836 |
} |
| 3837 |
} |
| 3838 |
m_avals.m_known_vals = vNULL; |
| 3839 |
if (ctx.m_avals.m_known_vals.exists ()) |
| 3840 |
{ |
| 3841 |
unsigned int n = MIN (ctx.m_avals.m_known_vals.length (), nargs); |
| 3842 |
|
| 3843 |
for (unsigned int i = 0; i < n; i++) |
| 3844 |
if (ipa_is_param_used_by_indirect_call (params_summary, i) |
| 3845 |
&& ctx.m_avals.m_known_vals[i]) |
| 3846 |
{ |
| 3847 |
m_avals.m_known_vals = ctx.m_avals.m_known_vals.copy (); |
| 3848 |
break; |
| 3849 |
} |
| 3850 |
} |
| 3851 |
|
| 3852 |
m_avals.m_known_contexts = vNULL; |
| 3853 |
if (ctx.m_avals.m_known_contexts.exists ()) |
| 3854 |
{ |
| 3855 |
unsigned int n = MIN (ctx.m_avals.m_known_contexts.length (), nargs); |
| 3856 |
|
| 3857 |
for (unsigned int i = 0; i < n; i++) |
| 3858 |
if (ipa_is_param_used_by_polymorphic_call (params_summary, i) |
| 3859 |
&& !ctx.m_avals.m_known_contexts[i].useless_p ()) |
| 3860 |
{ |
| 3861 |
m_avals.m_known_contexts = ctx.m_avals.m_known_contexts.copy (); |
| 3862 |
break; |
| 3863 |
} |
| 3864 |
} |
| 3865 |
|
| 3866 |
m_avals.m_known_aggs = vNULL; |
| 3867 |
if (ctx.m_avals.m_known_aggs.exists ()) |
| 3868 |
{ |
| 3869 |
const ipa_argagg_value_list avl (&ctx.m_avals); |
| 3870 |
for (unsigned int i = 0; i < nargs; i++) |
| 3871 |
if (ipa_is_param_used_by_indirect_call (params_summary, i) |
| 3872 |
&& avl.value_for_index_p (i)) |
| 3873 |
{ |
| 3874 |
m_avals.m_known_aggs = ctx.m_avals.m_known_aggs.copy (); |
| 3875 |
break; |
| 3876 |
} |
| 3877 |
} |
| 3878 |
|
| 3879 |
m_avals.m_known_value_ranges = vNULL; |
| 3880 |
} |
| 3881 |
|
| 3882 |
/* Release memory used by known_vals/contexts/aggs vectors. and |
| 3883 |
inline_param_summary. */ |
| 3884 |
|
| 3885 |
void |
| 3886 |
ipa_cached_call_context::release () |
| 3887 |
{ |
| 3888 |
/* See if context is initialized at first place. */ |
| 3889 |
if (!m_node) |
| 3890 |
return; |
| 3891 |
m_avals.m_known_aggs.release (); |
| 3892 |
m_avals.m_known_vals.release (); |
| 3893 |
m_avals.m_known_contexts.release (); |
| 3894 |
m_inline_param_summary.release (); |
| 3895 |
} |
| 3896 |
|
| 3897 |
/* Return true if CTX describes the same call context as THIS. */ |
| 3898 |
|
| 3899 |
bool |
| 3900 |
ipa_call_context::equal_to (const ipa_call_context &ctx) |
| 3901 |
{ |
| 3902 |
if (m_node != ctx.m_node |
| 3903 |
|| m_possible_truths != ctx.m_possible_truths |
| 3904 |
|| m_nonspec_possible_truths != ctx.m_nonspec_possible_truths) |
| 3905 |
return false; |
| 3906 |
|
| 3907 |
ipa_node_params *params_summary = ipa_node_params_sum->get (m_node); |
| 3908 |
unsigned int nargs = params_summary |
| 3909 |
? ipa_get_param_count (params_summary) : 0; |
| 3910 |
|
| 3911 |
if (m_inline_param_summary.exists () || ctx.m_inline_param_summary.exists ()) |
| 3912 |
{ |
| 3913 |
for (unsigned int i = 0; i < nargs; i++) |
| 3914 |
{ |
| 3915 |
if (!ipa_is_param_used_by_ipa_predicates (params_summary, i)) |
| 3916 |
continue; |
| 3917 |
if (i >= m_inline_param_summary.length () |
| 3918 |
|| m_inline_param_summary[i].useless_p ()) |
| 3919 |
{ |
| 3920 |
if (i < ctx.m_inline_param_summary.length () |
| 3921 |
&& !ctx.m_inline_param_summary[i].useless_p ()) |
| 3922 |
return false; |
| 3923 |
continue; |
| 3924 |
} |
| 3925 |
if (i >= ctx.m_inline_param_summary.length () |
| 3926 |
|| ctx.m_inline_param_summary[i].useless_p ()) |
| 3927 |
{ |
| 3928 |
if (i < m_inline_param_summary.length () |
| 3929 |
&& !m_inline_param_summary[i].useless_p ()) |
| 3930 |
return false; |
| 3931 |
continue; |
| 3932 |
} |
| 3933 |
if (!m_inline_param_summary[i].equal_to |
| 3934 |
(ctx.m_inline_param_summary[i])) |
| 3935 |
return false; |
| 3936 |
} |
| 3937 |
} |
| 3938 |
if (m_avals.m_known_vals.exists () || ctx.m_avals.m_known_vals.exists ()) |
| 3939 |
{ |
| 3940 |
for (unsigned int i = 0; i < nargs; i++) |
| 3941 |
{ |
| 3942 |
if (!ipa_is_param_used_by_indirect_call (params_summary, i)) |
| 3943 |
continue; |
| 3944 |
if (i >= m_avals.m_known_vals.length () || !m_avals.m_known_vals[i]) |
| 3945 |
{ |
| 3946 |
if (i < ctx.m_avals.m_known_vals.length () |
| 3947 |
&& ctx.m_avals.m_known_vals[i]) |
| 3948 |
return false; |
| 3949 |
continue; |
| 3950 |
} |
| 3951 |
if (i >= ctx.m_avals.m_known_vals.length () |
| 3952 |
|| !ctx.m_avals.m_known_vals[i]) |
| 3953 |
{ |
| 3954 |
if (i < m_avals.m_known_vals.length () && m_avals.m_known_vals[i]) |
| 3955 |
return false; |
| 3956 |
continue; |
| 3957 |
} |
| 3958 |
if (m_avals.m_known_vals[i] != ctx.m_avals.m_known_vals[i]) |
| 3959 |
return false; |
| 3960 |
} |
| 3961 |
} |
| 3962 |
if (m_avals.m_known_contexts.exists () |
| 3963 |
|| ctx.m_avals.m_known_contexts.exists ()) |
| 3964 |
{ |
| 3965 |
for (unsigned int i = 0; i < nargs; i++) |
| 3966 |
{ |
| 3967 |
if (!ipa_is_param_used_by_polymorphic_call (params_summary, i)) |
| 3968 |
continue; |
| 3969 |
if (i >= m_avals.m_known_contexts.length () |
| 3970 |
|| m_avals.m_known_contexts[i].useless_p ()) |
| 3971 |
{ |
| 3972 |
if (i < ctx.m_avals.m_known_contexts.length () |
| 3973 |
&& !ctx.m_avals.m_known_contexts[i].useless_p ()) |
| 3974 |
return false; |
| 3975 |
continue; |
| 3976 |
} |
| 3977 |
if (i >= ctx.m_avals.m_known_contexts.length () |
| 3978 |
|| ctx.m_avals.m_known_contexts[i].useless_p ()) |
| 3979 |
{ |
| 3980 |
if (i < m_avals.m_known_contexts.length () |
| 3981 |
&& !m_avals.m_known_contexts[i].useless_p ()) |
| 3982 |
return false; |
| 3983 |
continue; |
| 3984 |
} |
| 3985 |
if (!m_avals.m_known_contexts[i].equal_to |
| 3986 |
(ctx.m_avals.m_known_contexts[i])) |
| 3987 |
return false; |
| 3988 |
} |
| 3989 |
} |
| 3990 |
if (m_avals.m_known_aggs.exists () || ctx.m_avals.m_known_aggs.exists ()) |
| 3991 |
{ |
| 3992 |
unsigned i = 0, j = 0; |
| 3993 |
while (i < m_avals.m_known_aggs.length () |
| 3994 |
|| j < ctx.m_avals.m_known_aggs.length ()) |
| 3995 |
{ |
| 3996 |
if (i >= m_avals.m_known_aggs.length ()) |
| 3997 |
{ |
| 3998 |
int idx2 = ctx.m_avals.m_known_aggs[j].index; |
| 3999 |
if (ipa_is_param_used_by_indirect_call (params_summary, idx2)) |
| 4000 |
return false; |
| 4001 |
j++; |
| 4002 |
continue; |
| 4003 |
} |
| 4004 |
if (j >= ctx.m_avals.m_known_aggs.length ()) |
| 4005 |
{ |
| 4006 |
int idx1 = m_avals.m_known_aggs[i].index; |
| 4007 |
if (ipa_is_param_used_by_indirect_call (params_summary, idx1)) |
| 4008 |
return false; |
| 4009 |
i++; |
| 4010 |
continue; |
| 4011 |
} |
| 4012 |
|
| 4013 |
int idx1 = m_avals.m_known_aggs[i].index; |
| 4014 |
int idx2 = ctx.m_avals.m_known_aggs[j].index; |
| 4015 |
if (idx1 < idx2) |
| 4016 |
{ |
| 4017 |
if (ipa_is_param_used_by_indirect_call (params_summary, idx1)) |
| 4018 |
return false; |
| 4019 |
i++; |
| 4020 |
continue; |
| 4021 |
} |
| 4022 |
if (idx1 > idx2) |
| 4023 |
{ |
| 4024 |
if (ipa_is_param_used_by_indirect_call (params_summary, idx2)) |
| 4025 |
return false; |
| 4026 |
j++; |
| 4027 |
continue; |
| 4028 |
} |
| 4029 |
if (!ipa_is_param_used_by_indirect_call (params_summary, idx1)) |
| 4030 |
{ |
| 4031 |
i++; |
| 4032 |
j++; |
| 4033 |
continue; |
| 4034 |
} |
| 4035 |
|
| 4036 |
if ((m_avals.m_known_aggs[i].unit_offset |
| 4037 |
!= ctx.m_avals.m_known_aggs[j].unit_offset) |
| 4038 |
|| (m_avals.m_known_aggs[i].by_ref |
| 4039 |
!= ctx.m_avals.m_known_aggs[j].by_ref) |
| 4040 |
|| !operand_equal_p (m_avals.m_known_aggs[i].value, |
| 4041 |
ctx.m_avals.m_known_aggs[j].value)) |
| 4042 |
return false; |
| 4043 |
i++; |
| 4044 |
j++; |
| 4045 |
} |
| 4046 |
} |
| 4047 |
return true; |
| 4048 |
} |
| 4049 |
|
| 4050 |
/* Fill in the selected fields in ESTIMATES with value estimated for call in |
| 4051 |
this context. Always compute size and min_size. Only compute time and |
| 4052 |
nonspecialized_time if EST_TIMES is true. Only compute hints if EST_HINTS |
| 4053 |
is true. */ |
| 4054 |
|
| 4055 |
void |
| 4056 |
ipa_call_context::estimate_size_and_time (ipa_call_estimates *estimates, |
| 4057 |
bool est_times, bool est_hints) |
| 4058 |
{ |
| 4059 |
class ipa_fn_summary *info = ipa_fn_summaries->get (m_node); |
| 4060 |
size_time_entry *e; |
| 4061 |
int size = 0; |
| 4062 |
sreal time = 0; |
| 4063 |
int min_size = 0; |
| 4064 |
ipa_hints hints = 0; |
| 4065 |
sreal loops_with_known_iterations = 0; |
| 4066 |
sreal loops_with_known_strides = 0; |
| 4067 |
int i; |
| 4068 |
|
| 4069 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4070 |
{ |
| 4071 |
bool found = false; |
| 4072 |
fprintf (dump_file, " Estimating body: %s\n" |
| 4073 |
" Known to be false: ", m_node->dump_name ()); |
| 4074 |
|
| 4075 |
for (i = ipa_predicate::not_inlined_condition; |
| 4076 |
i < (ipa_predicate::first_dynamic_condition |
| 4077 |
+ (int) vec_safe_length (info->conds)); i++) |
| 4078 |
if (!(m_possible_truths & (1 << i))) |
| 4079 |
{ |
| 4080 |
if (found) |
| 4081 |
fprintf (dump_file, ", "); |
| 4082 |
found = true; |
| 4083 |
dump_condition (dump_file, info->conds, i); |
| 4084 |
} |
| 4085 |
} |
| 4086 |
|
| 4087 |
if (m_node->callees || m_node->indirect_calls) |
| 4088 |
estimate_calls_size_and_time (m_node, &size, &min_size, |
| 4089 |
est_times ? &time : NULL, |
| 4090 |
est_hints ? &hints : NULL, m_possible_truths, |
| 4091 |
&m_avals); |
| 4092 |
|
| 4093 |
sreal nonspecialized_time = time; |
| 4094 |
|
| 4095 |
min_size += info->size_time_table[0].size; |
| 4096 |
for (i = 0; info->size_time_table.iterate (i, &e); i++) |
| 4097 |
{ |
| 4098 |
bool exec = e->exec_predicate.evaluate (m_nonspec_possible_truths); |
| 4099 |
|
| 4100 |
/* Because predicates are conservative, it can happen that nonconst is 1 |
| 4101 |
but exec is 0. */ |
| 4102 |
if (exec) |
| 4103 |
{ |
| 4104 |
bool nonconst = e->nonconst_predicate.evaluate (m_possible_truths); |
| 4105 |
|
| 4106 |
gcc_checking_assert (e->time >= 0); |
| 4107 |
gcc_checking_assert (time >= 0); |
| 4108 |
|
| 4109 |
/* We compute specialized size only because size of nonspecialized |
| 4110 |
copy is context independent. |
| 4111 |
|
| 4112 |
The difference between nonspecialized execution and specialized is |
| 4113 |
that nonspecialized is not going to have optimized out computations |
| 4114 |
known to be constant in a specialized setting. */ |
| 4115 |
if (nonconst) |
| 4116 |
size += e->size; |
| 4117 |
if (!est_times) |
| 4118 |
continue; |
| 4119 |
nonspecialized_time += e->time; |
| 4120 |
if (!nonconst) |
| 4121 |
; |
| 4122 |
else if (!m_inline_param_summary.exists ()) |
| 4123 |
{ |
| 4124 |
if (nonconst) |
| 4125 |
time += e->time; |
| 4126 |
} |
| 4127 |
else |
| 4128 |
{ |
| 4129 |
int prob = e->nonconst_predicate.probability |
| 4130 |
(info->conds, m_possible_truths, |
| 4131 |
m_inline_param_summary); |
| 4132 |
gcc_checking_assert (prob >= 0); |
| 4133 |
gcc_checking_assert (prob <= REG_BR_PROB_BASE); |
| 4134 |
if (prob == REG_BR_PROB_BASE) |
| 4135 |
time += e->time; |
| 4136 |
else |
| 4137 |
time += e->time * prob / REG_BR_PROB_BASE; |
| 4138 |
} |
| 4139 |
gcc_checking_assert (time >= 0); |
| 4140 |
} |
| 4141 |
} |
| 4142 |
gcc_checking_assert (info->size_time_table[0].exec_predicate == true); |
| 4143 |
gcc_checking_assert (info->size_time_table[0].nonconst_predicate == true); |
| 4144 |
gcc_checking_assert (min_size >= 0); |
| 4145 |
gcc_checking_assert (size >= 0); |
| 4146 |
gcc_checking_assert (time >= 0); |
| 4147 |
/* nonspecialized_time should be always bigger than specialized time. |
| 4148 |
Roundoff issues however may get into the way. */ |
| 4149 |
gcc_checking_assert ((nonspecialized_time - time * 99 / 100) >= -1); |
| 4150 |
|
| 4151 |
/* Roundoff issues may make specialized time bigger than nonspecialized |
| 4152 |
time. We do not really want that to happen because some heuristics |
| 4153 |
may get confused by seeing negative speedups. */ |
| 4154 |
if (time > nonspecialized_time) |
| 4155 |
time = nonspecialized_time; |
| 4156 |
|
| 4157 |
if (est_hints) |
| 4158 |
{ |
| 4159 |
if (info->scc_no) |
| 4160 |
hints |= INLINE_HINT_in_scc; |
| 4161 |
if (DECL_DECLARED_INLINE_P (m_node->decl)) |
| 4162 |
hints |= INLINE_HINT_declared_inline; |
| 4163 |
if (info->builtin_constant_p_parms.length () |
| 4164 |
&& DECL_DECLARED_INLINE_P (m_node->decl)) |
| 4165 |
hints |= INLINE_HINT_builtin_constant_p; |
| 4166 |
|
| 4167 |
ipa_freqcounting_predicate *fcp; |
| 4168 |
for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++) |
| 4169 |
if (!fcp->predicate->evaluate (m_possible_truths)) |
| 4170 |
{ |
| 4171 |
hints |= INLINE_HINT_loop_iterations; |
| 4172 |
loops_with_known_iterations += fcp->freq; |
| 4173 |
} |
| 4174 |
estimates->loops_with_known_iterations = loops_with_known_iterations; |
| 4175 |
|
| 4176 |
for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++) |
| 4177 |
if (!fcp->predicate->evaluate (m_possible_truths)) |
| 4178 |
{ |
| 4179 |
hints |= INLINE_HINT_loop_stride; |
| 4180 |
loops_with_known_strides += fcp->freq; |
| 4181 |
} |
| 4182 |
estimates->loops_with_known_strides = loops_with_known_strides; |
| 4183 |
} |
| 4184 |
|
| 4185 |
size = RDIV (size, ipa_fn_summary::size_scale); |
| 4186 |
min_size = RDIV (min_size, ipa_fn_summary::size_scale); |
| 4187 |
|
| 4188 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4189 |
{ |
| 4190 |
fprintf (dump_file, "\n size:%i", (int) size); |
| 4191 |
if (est_times) |
| 4192 |
fprintf (dump_file, " time:%f nonspec time:%f", |
| 4193 |
time.to_double (), nonspecialized_time.to_double ()); |
| 4194 |
if (est_hints) |
| 4195 |
fprintf (dump_file, " loops with known iterations:%f " |
| 4196 |
"known strides:%f", loops_with_known_iterations.to_double (), |
| 4197 |
loops_with_known_strides.to_double ()); |
| 4198 |
fprintf (dump_file, "\n"); |
| 4199 |
} |
| 4200 |
if (est_times) |
| 4201 |
{ |
| 4202 |
estimates->time = time; |
| 4203 |
estimates->nonspecialized_time = nonspecialized_time; |
| 4204 |
} |
| 4205 |
estimates->size = size; |
| 4206 |
estimates->min_size = min_size; |
| 4207 |
if (est_hints) |
| 4208 |
estimates->hints = hints; |
| 4209 |
return; |
| 4210 |
} |
| 4211 |
|
| 4212 |
|
| 4213 |
/* Estimate size and time needed to execute callee of EDGE assuming that |
| 4214 |
parameters known to be constant at caller of EDGE are propagated. |
| 4215 |
KNOWN_VALS and KNOWN_CONTEXTS are vectors of assumed known constant values |
| 4216 |
and types for parameters. */ |
| 4217 |
|
| 4218 |
void |
| 4219 |
estimate_ipcp_clone_size_and_time (struct cgraph_node *node, |
| 4220 |
ipa_auto_call_arg_values *avals, |
| 4221 |
ipa_call_estimates *estimates) |
| 4222 |
{ |
| 4223 |
clause_t clause, nonspec_clause; |
| 4224 |
|
| 4225 |
evaluate_conditions_for_known_args (node, false, avals, &clause, |
| 4226 |
&nonspec_clause, NULL); |
| 4227 |
ipa_call_context ctx (node, clause, nonspec_clause, vNULL, avals); |
| 4228 |
ctx.estimate_size_and_time (estimates); |
| 4229 |
} |
| 4230 |
|
| 4231 |
/* Return stack frame offset where frame of NODE is supposed to start inside |
| 4232 |
of the function it is inlined to. |
| 4233 |
Return 0 for functions that are not inlined. */ |
| 4234 |
|
| 4235 |
HOST_WIDE_INT |
| 4236 |
ipa_get_stack_frame_offset (struct cgraph_node *node) |
| 4237 |
{ |
| 4238 |
HOST_WIDE_INT offset = 0; |
| 4239 |
if (!node->inlined_to) |
| 4240 |
return 0; |
| 4241 |
node = node->callers->caller; |
| 4242 |
while (true) |
| 4243 |
{ |
| 4244 |
offset += ipa_size_summaries->get (node)->estimated_self_stack_size; |
| 4245 |
if (!node->inlined_to) |
| 4246 |
return offset; |
| 4247 |
node = node->callers->caller; |
| 4248 |
} |
| 4249 |
} |
| 4250 |
|
| 4251 |
|
| 4252 |
/* Update summary information of inline clones after inlining. |
| 4253 |
Compute peak stack usage. */ |
| 4254 |
|
| 4255 |
static void |
| 4256 |
inline_update_callee_summaries (struct cgraph_node *node, int depth) |
| 4257 |
{ |
| 4258 |
struct cgraph_edge *e; |
| 4259 |
|
| 4260 |
ipa_propagate_frequency (node); |
| 4261 |
for (e = node->callees; e; e = e->next_callee) |
| 4262 |
{ |
| 4263 |
if (!e->inline_failed) |
| 4264 |
inline_update_callee_summaries (e->callee, depth); |
| 4265 |
else |
| 4266 |
ipa_call_summaries->get (e)->loop_depth += depth; |
| 4267 |
} |
| 4268 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 4269 |
ipa_call_summaries->get (e)->loop_depth += depth; |
| 4270 |
} |
| 4271 |
|
| 4272 |
/* Update change_prob and points_to_local_or_readonly_memory of EDGE after |
| 4273 |
INLINED_EDGE has been inlined. |
| 4274 |
|
| 4275 |
When function A is inlined in B and A calls C with parameter that |
| 4276 |
changes with probability PROB1 and C is known to be passthrough |
| 4277 |
of argument if B that change with probability PROB2, the probability |
| 4278 |
of change is now PROB1*PROB2. */ |
| 4279 |
|
| 4280 |
static void |
| 4281 |
remap_edge_params (struct cgraph_edge *inlined_edge, |
| 4282 |
struct cgraph_edge *edge) |
| 4283 |
{ |
| 4284 |
if (ipa_node_params_sum) |
| 4285 |
{ |
| 4286 |
int i; |
| 4287 |
ipa_edge_args *args = ipa_edge_args_sum->get (edge); |
| 4288 |
if (!args) |
| 4289 |
return; |
| 4290 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 4291 |
class ipa_call_summary *inlined_es |
| 4292 |
= ipa_call_summaries->get (inlined_edge); |
| 4293 |
|
| 4294 |
if (es->param.length () == 0) |
| 4295 |
return; |
| 4296 |
|
| 4297 |
for (i = 0; i < ipa_get_cs_argument_count (args); i++) |
| 4298 |
{ |
| 4299 |
struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i); |
| 4300 |
if (jfunc->type == IPA_JF_PASS_THROUGH |
| 4301 |
|| jfunc->type == IPA_JF_ANCESTOR) |
| 4302 |
{ |
| 4303 |
int id = jfunc->type == IPA_JF_PASS_THROUGH |
| 4304 |
? ipa_get_jf_pass_through_formal_id (jfunc) |
| 4305 |
: ipa_get_jf_ancestor_formal_id (jfunc); |
| 4306 |
if (id < (int) inlined_es->param.length ()) |
| 4307 |
{ |
| 4308 |
int prob1 = es->param[i].change_prob; |
| 4309 |
int prob2 = inlined_es->param[id].change_prob; |
| 4310 |
int prob = combine_probabilities (prob1, prob2); |
| 4311 |
|
| 4312 |
if (prob1 && prob2 && !prob) |
| 4313 |
prob = 1; |
| 4314 |
|
| 4315 |
es->param[i].change_prob = prob; |
| 4316 |
|
| 4317 |
if (inlined_es |
| 4318 |
->param[id].points_to_local_or_readonly_memory) |
| 4319 |
es->param[i].points_to_local_or_readonly_memory = true; |
| 4320 |
if (inlined_es |
| 4321 |
->param[id].points_to_possible_sra_candidate) |
| 4322 |
es->param[i].points_to_possible_sra_candidate = true; |
| 4323 |
} |
| 4324 |
if (!es->param[i].points_to_local_or_readonly_memory |
| 4325 |
&& jfunc->type == IPA_JF_CONST |
| 4326 |
&& points_to_local_or_readonly_memory_p |
| 4327 |
(ipa_get_jf_constant (jfunc))) |
| 4328 |
es->param[i].points_to_local_or_readonly_memory = true; |
| 4329 |
} |
| 4330 |
} |
| 4331 |
} |
| 4332 |
} |
| 4333 |
|
| 4334 |
/* Update edge summaries of NODE after INLINED_EDGE has been inlined. |
| 4335 |
|
| 4336 |
Remap predicates of callees of NODE. Rest of arguments match |
| 4337 |
remap_predicate. |
| 4338 |
|
| 4339 |
Also update change probabilities. */ |
| 4340 |
|
| 4341 |
static void |
| 4342 |
remap_edge_summaries (struct cgraph_edge *inlined_edge, |
| 4343 |
struct cgraph_node *node, |
| 4344 |
class ipa_fn_summary *info, |
| 4345 |
class ipa_node_params *params_summary, |
| 4346 |
class ipa_fn_summary *callee_info, |
| 4347 |
const vec<int> &operand_map, |
| 4348 |
const vec<HOST_WIDE_INT> &offset_map, |
| 4349 |
clause_t possible_truths, |
| 4350 |
ipa_predicate *toplev_predicate) |
| 4351 |
{ |
| 4352 |
struct cgraph_edge *e, *next; |
| 4353 |
for (e = node->callees; e; e = next) |
| 4354 |
{ |
| 4355 |
ipa_predicate p; |
| 4356 |
next = e->next_callee; |
| 4357 |
|
| 4358 |
if (e->inline_failed) |
| 4359 |
{ |
| 4360 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 4361 |
remap_edge_params (inlined_edge, e); |
| 4362 |
|
| 4363 |
if (es->predicate) |
| 4364 |
{ |
| 4365 |
p = es->predicate->remap_after_inlining |
| 4366 |
(info, params_summary, |
| 4367 |
callee_info, operand_map, |
| 4368 |
offset_map, possible_truths, |
| 4369 |
*toplev_predicate); |
| 4370 |
edge_set_predicate (e, &p); |
| 4371 |
} |
| 4372 |
else |
| 4373 |
edge_set_predicate (e, toplev_predicate); |
| 4374 |
} |
| 4375 |
else |
| 4376 |
remap_edge_summaries (inlined_edge, e->callee, info, |
| 4377 |
params_summary, callee_info, |
| 4378 |
operand_map, offset_map, possible_truths, |
| 4379 |
toplev_predicate); |
| 4380 |
} |
| 4381 |
for (e = node->indirect_calls; e; e = next) |
| 4382 |
{ |
| 4383 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 4384 |
ipa_predicate p; |
| 4385 |
next = e->next_callee; |
| 4386 |
|
| 4387 |
remap_edge_params (inlined_edge, e); |
| 4388 |
if (es->predicate) |
| 4389 |
{ |
| 4390 |
p = es->predicate->remap_after_inlining |
| 4391 |
(info, params_summary, |
| 4392 |
callee_info, operand_map, offset_map, |
| 4393 |
possible_truths, *toplev_predicate); |
| 4394 |
edge_set_predicate (e, &p); |
| 4395 |
} |
| 4396 |
else |
| 4397 |
edge_set_predicate (e, toplev_predicate); |
| 4398 |
} |
| 4399 |
} |
| 4400 |
|
| 4401 |
/* Run remap_after_inlining on each predicate in V. */ |
| 4402 |
|
| 4403 |
static void |
| 4404 |
remap_freqcounting_predicate (class ipa_fn_summary *info, |
| 4405 |
class ipa_node_params *params_summary, |
| 4406 |
class ipa_fn_summary *callee_info, |
| 4407 |
vec<ipa_freqcounting_predicate, va_gc> *v, |
| 4408 |
const vec<int> &operand_map, |
| 4409 |
const vec<HOST_WIDE_INT> &offset_map, |
| 4410 |
clause_t possible_truths, |
| 4411 |
ipa_predicate *toplev_predicate) |
| 4412 |
|
| 4413 |
{ |
| 4414 |
ipa_freqcounting_predicate *fcp; |
| 4415 |
for (int i = 0; vec_safe_iterate (v, i, &fcp); i++) |
| 4416 |
{ |
| 4417 |
ipa_predicate p |
| 4418 |
= fcp->predicate->remap_after_inlining (info, params_summary, |
| 4419 |
callee_info, operand_map, |
| 4420 |
offset_map, possible_truths, |
| 4421 |
*toplev_predicate); |
| 4422 |
if (p != false && p != true) |
| 4423 |
*fcp->predicate &= p; |
| 4424 |
} |
| 4425 |
} |
| 4426 |
|
| 4427 |
/* We inlined EDGE. Update summary of the function we inlined into. */ |
| 4428 |
|
| 4429 |
void |
| 4430 |
ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge) |
| 4431 |
{ |
| 4432 |
ipa_fn_summary *callee_info = ipa_fn_summaries->get (edge->callee); |
| 4433 |
struct cgraph_node *to = (edge->caller->inlined_to |
| 4434 |
? edge->caller->inlined_to : edge->caller); |
| 4435 |
class ipa_fn_summary *info = ipa_fn_summaries->get (to); |
| 4436 |
clause_t clause = 0; /* not_inline is known to be false. */ |
| 4437 |
size_time_entry *e; |
| 4438 |
auto_vec<int, 8> operand_map; |
| 4439 |
auto_vec<HOST_WIDE_INT, 8> offset_map; |
| 4440 |
int i; |
| 4441 |
ipa_predicate toplev_predicate; |
| 4442 |
class ipa_call_summary *es = ipa_call_summaries->get (edge); |
| 4443 |
ipa_node_params *params_summary = (ipa_node_params_sum |
| 4444 |
? ipa_node_params_sum->get (to) : NULL); |
| 4445 |
|
| 4446 |
if (es->predicate) |
| 4447 |
toplev_predicate = *es->predicate; |
| 4448 |
else |
| 4449 |
toplev_predicate = true; |
| 4450 |
|
| 4451 |
info->fp_expressions |= callee_info->fp_expressions; |
| 4452 |
info->target_info |= callee_info->target_info; |
| 4453 |
|
| 4454 |
if (callee_info->conds) |
| 4455 |
{ |
| 4456 |
ipa_auto_call_arg_values avals; |
| 4457 |
evaluate_properties_for_edge (edge, true, &clause, NULL, &avals, false); |
| 4458 |
} |
| 4459 |
if (ipa_node_params_sum && callee_info->conds) |
| 4460 |
{ |
| 4461 |
ipa_edge_args *args = ipa_edge_args_sum->get (edge); |
| 4462 |
int count = args ? ipa_get_cs_argument_count (args) : 0; |
| 4463 |
int i; |
| 4464 |
|
| 4465 |
if (count) |
| 4466 |
{ |
| 4467 |
operand_map.safe_grow_cleared (count, true); |
| 4468 |
offset_map.safe_grow_cleared (count, true); |
| 4469 |
} |
| 4470 |
for (i = 0; i < count; i++) |
| 4471 |
{ |
| 4472 |
struct ipa_jump_func *jfunc = ipa_get_ith_jump_func (args, i); |
| 4473 |
int map = -1; |
| 4474 |
|
| 4475 |
/* TODO: handle non-NOPs when merging. */ |
| 4476 |
if (jfunc->type == IPA_JF_PASS_THROUGH) |
| 4477 |
{ |
| 4478 |
if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) |
| 4479 |
map = ipa_get_jf_pass_through_formal_id (jfunc); |
| 4480 |
if (!ipa_get_jf_pass_through_agg_preserved (jfunc)) |
| 4481 |
offset_map[i] = -1; |
| 4482 |
} |
| 4483 |
else if (jfunc->type == IPA_JF_ANCESTOR) |
| 4484 |
{ |
| 4485 |
HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc); |
| 4486 |
if (offset >= 0 && offset < INT_MAX) |
| 4487 |
{ |
| 4488 |
map = ipa_get_jf_ancestor_formal_id (jfunc); |
| 4489 |
if (!ipa_get_jf_ancestor_agg_preserved (jfunc)) |
| 4490 |
offset = -1; |
| 4491 |
offset_map[i] = offset; |
| 4492 |
} |
| 4493 |
} |
| 4494 |
operand_map[i] = map; |
| 4495 |
gcc_assert (map < ipa_get_param_count (params_summary)); |
| 4496 |
} |
| 4497 |
|
| 4498 |
int ip; |
| 4499 |
for (i = 0; callee_info->builtin_constant_p_parms.iterate (i, &ip); i++) |
| 4500 |
if (ip < count && operand_map[ip] >= 0) |
| 4501 |
add_builtin_constant_p_parm (info, operand_map[ip]); |
| 4502 |
} |
| 4503 |
sreal freq = edge->sreal_frequency (); |
| 4504 |
for (i = 0; callee_info->size_time_table.iterate (i, &e); i++) |
| 4505 |
{ |
| 4506 |
ipa_predicate p; |
| 4507 |
p = e->exec_predicate.remap_after_inlining |
| 4508 |
(info, params_summary, |
| 4509 |
callee_info, operand_map, |
| 4510 |
offset_map, clause, |
| 4511 |
toplev_predicate); |
| 4512 |
ipa_predicate nonconstp; |
| 4513 |
nonconstp = e->nonconst_predicate.remap_after_inlining |
| 4514 |
(info, params_summary, |
| 4515 |
callee_info, operand_map, |
| 4516 |
offset_map, clause, |
| 4517 |
toplev_predicate); |
| 4518 |
if (p != false && nonconstp != false) |
| 4519 |
{ |
| 4520 |
sreal add_time = ((sreal)e->time * freq); |
| 4521 |
int prob = e->nonconst_predicate.probability (callee_info->conds, |
| 4522 |
clause, es->param); |
| 4523 |
if (prob != REG_BR_PROB_BASE) |
| 4524 |
add_time = add_time * prob / REG_BR_PROB_BASE; |
| 4525 |
if (prob != REG_BR_PROB_BASE |
| 4526 |
&& dump_file && (dump_flags & TDF_DETAILS)) |
| 4527 |
{ |
| 4528 |
fprintf (dump_file, "\t\tScaling time by probability:%f\n", |
| 4529 |
(double) prob / REG_BR_PROB_BASE); |
| 4530 |
} |
| 4531 |
info->account_size_time (e->size, add_time, p, nonconstp); |
| 4532 |
} |
| 4533 |
} |
| 4534 |
remap_edge_summaries (edge, edge->callee, info, params_summary, |
| 4535 |
callee_info, operand_map, |
| 4536 |
offset_map, clause, &toplev_predicate); |
| 4537 |
remap_freqcounting_predicate (info, params_summary, callee_info, |
| 4538 |
info->loop_iterations, operand_map, |
| 4539 |
offset_map, clause, &toplev_predicate); |
| 4540 |
remap_freqcounting_predicate (info, params_summary, callee_info, |
| 4541 |
info->loop_strides, operand_map, |
| 4542 |
offset_map, clause, &toplev_predicate); |
| 4543 |
|
| 4544 |
HOST_WIDE_INT stack_frame_offset = ipa_get_stack_frame_offset (edge->callee); |
| 4545 |
HOST_WIDE_INT peak = stack_frame_offset + callee_info->estimated_stack_size; |
| 4546 |
|
| 4547 |
if (info->estimated_stack_size < peak) |
| 4548 |
info->estimated_stack_size = peak; |
| 4549 |
|
| 4550 |
inline_update_callee_summaries (edge->callee, es->loop_depth); |
| 4551 |
if (info->call_size_time_table.length ()) |
| 4552 |
{ |
| 4553 |
int edge_size = 0; |
| 4554 |
sreal edge_time = 0; |
| 4555 |
|
| 4556 |
estimate_edge_size_and_time (edge, &edge_size, NULL, &edge_time, NULL, 0); |
| 4557 |
/* Unaccount size and time of the optimized out call. */ |
| 4558 |
info->account_size_time (-edge_size, -edge_time, |
| 4559 |
es->predicate ? *es->predicate : true, |
| 4560 |
es->predicate ? *es->predicate : true, |
| 4561 |
true); |
| 4562 |
/* Account new calls. */ |
| 4563 |
summarize_calls_size_and_time (edge->callee, info); |
| 4564 |
} |
| 4565 |
|
| 4566 |
/* Free summaries that are not maintained for inline clones/edges. */ |
| 4567 |
ipa_call_summaries->remove (edge); |
| 4568 |
ipa_fn_summaries->remove (edge->callee); |
| 4569 |
ipa_remove_from_growth_caches (edge); |
| 4570 |
} |
| 4571 |
|
| 4572 |
/* For performance reasons ipa_merge_fn_summary_after_inlining is not updating |
| 4573 |
overall size and time. Recompute it. |
| 4574 |
If RESET is true also recompute call_time_size_table. */ |
| 4575 |
|
| 4576 |
void |
| 4577 |
ipa_update_overall_fn_summary (struct cgraph_node *node, bool reset) |
| 4578 |
{ |
| 4579 |
class ipa_fn_summary *info = ipa_fn_summaries->get (node); |
| 4580 |
class ipa_size_summary *size_info = ipa_size_summaries->get (node); |
| 4581 |
size_time_entry *e; |
| 4582 |
int i; |
| 4583 |
|
| 4584 |
size_info->size = 0; |
| 4585 |
info->time = 0; |
| 4586 |
for (i = 0; info->size_time_table.iterate (i, &e); i++) |
| 4587 |
{ |
| 4588 |
size_info->size += e->size; |
| 4589 |
info->time += e->time; |
| 4590 |
} |
| 4591 |
info->min_size = info->size_time_table[0].size; |
| 4592 |
if (reset) |
| 4593 |
info->call_size_time_table.release (); |
| 4594 |
if (node->callees || node->indirect_calls) |
| 4595 |
estimate_calls_size_and_time (node, &size_info->size, &info->min_size, |
| 4596 |
&info->time, NULL, |
| 4597 |
~(clause_t) (1 << ipa_predicate::false_condition), |
| 4598 |
NULL); |
| 4599 |
size_info->size = RDIV (size_info->size, ipa_fn_summary::size_scale); |
| 4600 |
info->min_size = RDIV (info->min_size, ipa_fn_summary::size_scale); |
| 4601 |
} |
| 4602 |
|
| 4603 |
|
| 4604 |
/* This function performs intraprocedural analysis in NODE that is required to |
| 4605 |
inline indirect calls. */ |
| 4606 |
|
| 4607 |
static void |
| 4608 |
inline_indirect_intraprocedural_analysis (struct cgraph_node *node) |
| 4609 |
{ |
| 4610 |
ipa_analyze_node (node); |
| 4611 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 4612 |
{ |
| 4613 |
ipa_print_node_params (dump_file, node); |
| 4614 |
ipa_print_node_jump_functions (dump_file, node); |
| 4615 |
} |
| 4616 |
} |
| 4617 |
|
| 4618 |
|
| 4619 |
/* Note function body size. */ |
| 4620 |
|
| 4621 |
void |
| 4622 |
inline_analyze_function (struct cgraph_node *node) |
| 4623 |
{ |
| 4624 |
push_cfun (DECL_STRUCT_FUNCTION (node->decl)); |
| 4625 |
|
| 4626 |
if (dump_file) |
| 4627 |
fprintf (dump_file, "\nAnalyzing function: %s\n", node->dump_name ()); |
| 4628 |
if (opt_for_fn (node->decl, optimize) && !node->thunk) |
| 4629 |
inline_indirect_intraprocedural_analysis (node); |
| 4630 |
compute_fn_summary (node, false); |
| 4631 |
if (!optimize) |
| 4632 |
{ |
| 4633 |
struct cgraph_edge *e; |
| 4634 |
for (e = node->callees; e; e = e->next_callee) |
| 4635 |
e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED; |
| 4636 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 4637 |
e->inline_failed = CIF_FUNCTION_NOT_OPTIMIZED; |
| 4638 |
} |
| 4639 |
|
| 4640 |
pop_cfun (); |
| 4641 |
} |
| 4642 |
|
| 4643 |
|
| 4644 |
/* Called when new function is inserted to callgraph late. */ |
| 4645 |
|
| 4646 |
void |
| 4647 |
ipa_fn_summary_t::insert (struct cgraph_node *node, ipa_fn_summary *) |
| 4648 |
{ |
| 4649 |
inline_analyze_function (node); |
| 4650 |
} |
| 4651 |
|
| 4652 |
/* Note function body size. */ |
| 4653 |
|
| 4654 |
static void |
| 4655 |
ipa_fn_summary_generate (void) |
| 4656 |
{ |
| 4657 |
struct cgraph_node *node; |
| 4658 |
|
| 4659 |
FOR_EACH_DEFINED_FUNCTION (node) |
| 4660 |
if (DECL_STRUCT_FUNCTION (node->decl)) |
| 4661 |
node->versionable = tree_versionable_function_p (node->decl); |
| 4662 |
|
| 4663 |
ipa_fn_summary_alloc (); |
| 4664 |
|
| 4665 |
ipa_fn_summaries->enable_insertion_hook (); |
| 4666 |
|
| 4667 |
ipa_register_cgraph_hooks (); |
| 4668 |
|
| 4669 |
FOR_EACH_DEFINED_FUNCTION (node) |
| 4670 |
if (!node->alias |
| 4671 |
&& (flag_generate_lto || flag_generate_offload|| flag_wpa |
| 4672 |
|| opt_for_fn (node->decl, optimize))) |
| 4673 |
inline_analyze_function (node); |
| 4674 |
} |
| 4675 |
|
| 4676 |
|
| 4677 |
/* Write inline summary for edge E to OB. */ |
| 4678 |
|
| 4679 |
static void |
| 4680 |
read_ipa_call_summary (class lto_input_block *ib, struct cgraph_edge *e, |
| 4681 |
bool prevails) |
| 4682 |
{ |
| 4683 |
class ipa_call_summary *es = prevails |
| 4684 |
? ipa_call_summaries->get_create (e) : NULL; |
| 4685 |
ipa_predicate p; |
| 4686 |
int length, i; |
| 4687 |
|
| 4688 |
int size = streamer_read_uhwi (ib); |
| 4689 |
int time = streamer_read_uhwi (ib); |
| 4690 |
int depth = streamer_read_uhwi (ib); |
| 4691 |
|
| 4692 |
if (es) |
| 4693 |
{ |
| 4694 |
es->call_stmt_size = size; |
| 4695 |
es->call_stmt_time = time; |
| 4696 |
es->loop_depth = depth; |
| 4697 |
} |
| 4698 |
|
| 4699 |
bitpack_d bp = streamer_read_bitpack (ib); |
| 4700 |
if (es) |
| 4701 |
es->is_return_callee_uncaptured = bp_unpack_value (&bp, 1); |
| 4702 |
else |
| 4703 |
bp_unpack_value (&bp, 1); |
| 4704 |
|
| 4705 |
p.stream_in (ib); |
| 4706 |
if (es) |
| 4707 |
edge_set_predicate (e, &p); |
| 4708 |
length = streamer_read_uhwi (ib); |
| 4709 |
if (length && es |
| 4710 |
&& (e->possibly_call_in_translation_unit_p () |
| 4711 |
/* Also stream in jump functions to builtins in hope that they |
| 4712 |
will get fnspecs. */ |
| 4713 |
|| fndecl_built_in_p (e->callee->decl, BUILT_IN_NORMAL))) |
| 4714 |
{ |
| 4715 |
es->param.safe_grow_cleared (length, true); |
| 4716 |
for (i = 0; i < length; i++) |
| 4717 |
{ |
| 4718 |
es->param[i].change_prob = streamer_read_uhwi (ib); |
| 4719 |
bitpack_d bp = streamer_read_bitpack (ib); |
| 4720 |
es->param[i].points_to_local_or_readonly_memory |
| 4721 |
= bp_unpack_value (&bp, 1); |
| 4722 |
es->param[i].points_to_possible_sra_candidate |
| 4723 |
= bp_unpack_value (&bp, 1); |
| 4724 |
} |
| 4725 |
} |
| 4726 |
else |
| 4727 |
{ |
| 4728 |
for (i = 0; i < length; i++) |
| 4729 |
{ |
| 4730 |
streamer_read_uhwi (ib); |
| 4731 |
streamer_read_uhwi (ib); |
| 4732 |
} |
| 4733 |
} |
| 4734 |
} |
| 4735 |
|
| 4736 |
|
| 4737 |
/* Stream in inline summaries from the section. */ |
| 4738 |
|
| 4739 |
static void |
| 4740 |
inline_read_section (struct lto_file_decl_data *file_data, const char *data, |
| 4741 |
size_t len) |
| 4742 |
{ |
| 4743 |
const struct lto_function_header *header = |
| 4744 |
(const struct lto_function_header *) data; |
| 4745 |
const int cfg_offset = sizeof (struct lto_function_header); |
| 4746 |
const int main_offset = cfg_offset + header->cfg_size; |
| 4747 |
const int string_offset = main_offset + header->main_size; |
| 4748 |
class data_in *data_in; |
| 4749 |
unsigned int i, count2, j; |
| 4750 |
unsigned int f_count; |
| 4751 |
|
| 4752 |
lto_input_block ib ((const char *) data + main_offset, header->main_size, |
| 4753 |
file_data); |
| 4754 |
|
| 4755 |
data_in = |
| 4756 |
lto_data_in_create (file_data, (const char *) data + string_offset, |
| 4757 |
header->string_size, vNULL); |
| 4758 |
f_count = streamer_read_uhwi (&ib); |
| 4759 |
for (i = 0; i < f_count; i++) |
| 4760 |
{ |
| 4761 |
unsigned int index; |
| 4762 |
struct cgraph_node *node; |
| 4763 |
class ipa_fn_summary *info; |
| 4764 |
class ipa_node_params *params_summary; |
| 4765 |
class ipa_size_summary *size_info; |
| 4766 |
lto_symtab_encoder_t encoder; |
| 4767 |
struct bitpack_d bp; |
| 4768 |
struct cgraph_edge *e; |
| 4769 |
ipa_predicate p; |
| 4770 |
|
| 4771 |
index = streamer_read_uhwi (&ib); |
| 4772 |
encoder = file_data->symtab_node_encoder; |
| 4773 |
node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder, |
| 4774 |
index)); |
| 4775 |
info = node->prevailing_p () ? ipa_fn_summaries->get_create (node) : NULL; |
| 4776 |
params_summary = node->prevailing_p () |
| 4777 |
? ipa_node_params_sum->get (node) : NULL; |
| 4778 |
size_info = node->prevailing_p () |
| 4779 |
? ipa_size_summaries->get_create (node) : NULL; |
| 4780 |
|
| 4781 |
int stack_size = streamer_read_uhwi (&ib); |
| 4782 |
int size = streamer_read_uhwi (&ib); |
| 4783 |
sreal time = sreal::stream_in (&ib); |
| 4784 |
|
| 4785 |
if (info) |
| 4786 |
{ |
| 4787 |
info->estimated_stack_size |
| 4788 |
= size_info->estimated_self_stack_size = stack_size; |
| 4789 |
size_info->size = size_info->self_size = size; |
| 4790 |
info->time = time; |
| 4791 |
} |
| 4792 |
|
| 4793 |
bp = streamer_read_bitpack (&ib); |
| 4794 |
if (info) |
| 4795 |
{ |
| 4796 |
info->inlinable = bp_unpack_value (&bp, 1); |
| 4797 |
info->fp_expressions = bp_unpack_value (&bp, 1); |
| 4798 |
if (!lto_stream_offload_p) |
| 4799 |
info->target_info = streamer_read_uhwi (&ib); |
| 4800 |
} |
| 4801 |
else |
| 4802 |
{ |
| 4803 |
bp_unpack_value (&bp, 1); |
| 4804 |
bp_unpack_value (&bp, 1); |
| 4805 |
if (!lto_stream_offload_p) |
| 4806 |
streamer_read_uhwi (&ib); |
| 4807 |
} |
| 4808 |
|
| 4809 |
count2 = streamer_read_uhwi (&ib); |
| 4810 |
gcc_assert (!info || !info->conds); |
| 4811 |
if (info) |
| 4812 |
vec_safe_reserve_exact (info->conds, count2); |
| 4813 |
for (j = 0; j < count2; j++) |
| 4814 |
{ |
| 4815 |
struct condition c; |
| 4816 |
unsigned int k, count3; |
| 4817 |
c.operand_num = streamer_read_uhwi (&ib); |
| 4818 |
c.code = (enum tree_code) streamer_read_uhwi (&ib); |
| 4819 |
c.type = stream_read_tree (&ib, data_in); |
| 4820 |
c.val = stream_read_tree (&ib, data_in); |
| 4821 |
bp = streamer_read_bitpack (&ib); |
| 4822 |
c.agg_contents = bp_unpack_value (&bp, 1); |
| 4823 |
c.by_ref = bp_unpack_value (&bp, 1); |
| 4824 |
if (c.agg_contents) |
| 4825 |
c.offset = streamer_read_uhwi (&ib); |
| 4826 |
count3 = streamer_read_uhwi (&ib); |
| 4827 |
c.param_ops = NULL; |
| 4828 |
if (info) |
| 4829 |
vec_safe_reserve_exact (c.param_ops, count3); |
| 4830 |
if (params_summary) |
| 4831 |
ipa_set_param_used_by_ipa_predicates |
| 4832 |
(params_summary, c.operand_num, true); |
| 4833 |
for (k = 0; k < count3; k++) |
| 4834 |
{ |
| 4835 |
struct expr_eval_op op; |
| 4836 |
enum gimple_rhs_class rhs_class; |
| 4837 |
op.code = (enum tree_code) streamer_read_uhwi (&ib); |
| 4838 |
op.type = stream_read_tree (&ib, data_in); |
| 4839 |
switch (rhs_class = get_gimple_rhs_class (op.code)) |
| 4840 |
{ |
| 4841 |
case GIMPLE_UNARY_RHS: |
| 4842 |
op.index = 0; |
| 4843 |
op.val[0] = NULL_TREE; |
| 4844 |
op.val[1] = NULL_TREE; |
| 4845 |
break; |
| 4846 |
|
| 4847 |
case GIMPLE_BINARY_RHS: |
| 4848 |
case GIMPLE_TERNARY_RHS: |
| 4849 |
bp = streamer_read_bitpack (&ib); |
| 4850 |
op.index = bp_unpack_value (&bp, 2); |
| 4851 |
op.val[0] = stream_read_tree (&ib, data_in); |
| 4852 |
if (rhs_class == GIMPLE_BINARY_RHS) |
| 4853 |
op.val[1] = NULL_TREE; |
| 4854 |
else |
| 4855 |
op.val[1] = stream_read_tree (&ib, data_in); |
| 4856 |
break; |
| 4857 |
|
| 4858 |
default: |
| 4859 |
fatal_error (UNKNOWN_LOCATION, |
| 4860 |
"invalid fnsummary in LTO stream"); |
| 4861 |
} |
| 4862 |
if (info) |
| 4863 |
c.param_ops->quick_push (op); |
| 4864 |
} |
| 4865 |
if (info) |
| 4866 |
info->conds->quick_push (c); |
| 4867 |
} |
| 4868 |
count2 = streamer_read_uhwi (&ib); |
| 4869 |
gcc_assert (!info || !info->size_time_table.length ()); |
| 4870 |
if (info && count2) |
| 4871 |
info->size_time_table.reserve_exact (count2); |
| 4872 |
for (j = 0; j < count2; j++) |
| 4873 |
{ |
| 4874 |
class size_time_entry e; |
| 4875 |
|
| 4876 |
e.size = streamer_read_uhwi (&ib); |
| 4877 |
e.time = sreal::stream_in (&ib); |
| 4878 |
e.exec_predicate.stream_in (&ib); |
| 4879 |
e.nonconst_predicate.stream_in (&ib); |
| 4880 |
|
| 4881 |
if (info) |
| 4882 |
info->size_time_table.quick_push (e); |
| 4883 |
} |
| 4884 |
|
| 4885 |
count2 = streamer_read_uhwi (&ib); |
| 4886 |
for (j = 0; j < count2; j++) |
| 4887 |
{ |
| 4888 |
p.stream_in (&ib); |
| 4889 |
sreal fcp_freq = sreal::stream_in (&ib); |
| 4890 |
if (info) |
| 4891 |
{ |
| 4892 |
ipa_freqcounting_predicate fcp; |
| 4893 |
fcp.predicate = NULL; |
| 4894 |
set_hint_predicate (&fcp.predicate, p); |
| 4895 |
fcp.freq = fcp_freq; |
| 4896 |
vec_safe_push (info->loop_iterations, fcp); |
| 4897 |
} |
| 4898 |
} |
| 4899 |
count2 = streamer_read_uhwi (&ib); |
| 4900 |
for (j = 0; j < count2; j++) |
| 4901 |
{ |
| 4902 |
p.stream_in (&ib); |
| 4903 |
sreal fcp_freq = sreal::stream_in (&ib); |
| 4904 |
if (info) |
| 4905 |
{ |
| 4906 |
ipa_freqcounting_predicate fcp; |
| 4907 |
fcp.predicate = NULL; |
| 4908 |
set_hint_predicate (&fcp.predicate, p); |
| 4909 |
fcp.freq = fcp_freq; |
| 4910 |
vec_safe_push (info->loop_strides, fcp); |
| 4911 |
} |
| 4912 |
} |
| 4913 |
count2 = streamer_read_uhwi (&ib); |
| 4914 |
if (info && count2) |
| 4915 |
info->builtin_constant_p_parms.reserve_exact (count2); |
| 4916 |
for (j = 0; j < count2; j++) |
| 4917 |
{ |
| 4918 |
int parm = streamer_read_uhwi (&ib); |
| 4919 |
if (info) |
| 4920 |
info->builtin_constant_p_parms.quick_push (parm); |
| 4921 |
} |
| 4922 |
for (e = node->callees; e; e = e->next_callee) |
| 4923 |
read_ipa_call_summary (&ib, e, info != NULL); |
| 4924 |
for (e = node->indirect_calls; e; e = e->next_callee) |
| 4925 |
read_ipa_call_summary (&ib, e, info != NULL); |
| 4926 |
} |
| 4927 |
|
| 4928 |
lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data, |
| 4929 |
len); |
| 4930 |
lto_data_in_delete (data_in); |
| 4931 |
} |
| 4932 |
|
| 4933 |
|
| 4934 |
/* Read inline summary. Jump functions are shared among ipa-cp |
| 4935 |
and inliner, so when ipa-cp is active, we don't need to write them |
| 4936 |
twice. */ |
| 4937 |
|
| 4938 |
static void |
| 4939 |
ipa_fn_summary_read (void) |
| 4940 |
{ |
| 4941 |
struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); |
| 4942 |
struct lto_file_decl_data *file_data; |
| 4943 |
unsigned int j = 0; |
| 4944 |
|
| 4945 |
ipa_prop_read_jump_functions (); |
| 4946 |
ipa_fn_summary_alloc (); |
| 4947 |
|
| 4948 |
while ((file_data = file_data_vec[j++])) |
| 4949 |
{ |
| 4950 |
size_t len; |
| 4951 |
const char *data |
| 4952 |
= lto_get_summary_section_data (file_data, LTO_section_ipa_fn_summary, |
| 4953 |
&len); |
| 4954 |
if (data) |
| 4955 |
inline_read_section (file_data, data, len); |
| 4956 |
else |
| 4957 |
/* Fatal error here. We do not want to support compiling ltrans units |
| 4958 |
with different version of compiler or different flags than the WPA |
| 4959 |
unit, so this should never happen. */ |
| 4960 |
fatal_error (input_location, |
| 4961 |
"ipa inline summary is missing in input file"); |
| 4962 |
} |
| 4963 |
ipa_register_cgraph_hooks (); |
| 4964 |
|
| 4965 |
gcc_assert (ipa_fn_summaries); |
| 4966 |
ipa_fn_summaries->enable_insertion_hook (); |
| 4967 |
} |
| 4968 |
|
| 4969 |
|
| 4970 |
/* Write inline summary for edge E to OB. */ |
| 4971 |
|
| 4972 |
static void |
| 4973 |
write_ipa_call_summary (struct output_block *ob, struct cgraph_edge *e) |
| 4974 |
{ |
| 4975 |
class ipa_call_summary *es = ipa_call_summaries->get (e); |
| 4976 |
int i; |
| 4977 |
|
| 4978 |
streamer_write_uhwi (ob, es->call_stmt_size); |
| 4979 |
streamer_write_uhwi (ob, es->call_stmt_time); |
| 4980 |
streamer_write_uhwi (ob, es->loop_depth); |
| 4981 |
|
| 4982 |
bitpack_d bp = bitpack_create (ob->main_stream); |
| 4983 |
bp_pack_value (&bp, es->is_return_callee_uncaptured, 1); |
| 4984 |
streamer_write_bitpack (&bp); |
| 4985 |
|
| 4986 |
if (es->predicate) |
| 4987 |
es->predicate->stream_out (ob); |
| 4988 |
else |
| 4989 |
streamer_write_uhwi (ob, 0); |
| 4990 |
streamer_write_uhwi (ob, es->param.length ()); |
| 4991 |
for (i = 0; i < (int) es->param.length (); i++) |
| 4992 |
{ |
| 4993 |
streamer_write_uhwi (ob, es->param[i].change_prob); |
| 4994 |
bp = bitpack_create (ob->main_stream); |
| 4995 |
bp_pack_value (&bp, es->param[i].points_to_local_or_readonly_memory, 1); |
| 4996 |
bp_pack_value (&bp, es->param[i].points_to_possible_sra_candidate, 1); |
| 4997 |
streamer_write_bitpack (&bp); |
| 4998 |
} |
| 4999 |
} |
| 5000 |
|
| 5001 |
|
| 5002 |
/* Write inline summary for node in SET. |
| 5003 |
Jump functions are shared among ipa-cp and inliner, so when ipa-cp is |
| 5004 |
active, we don't need to write them twice. */ |
| 5005 |
|
| 5006 |
static void |
| 5007 |
ipa_fn_summary_write (void) |
| 5008 |
{ |
| 5009 |
struct output_block *ob = create_output_block (LTO_section_ipa_fn_summary); |
| 5010 |
lto_symtab_encoder_iterator lsei; |
| 5011 |
lto_symtab_encoder_t encoder = ob->decl_state->symtab_node_encoder; |
| 5012 |
unsigned int count = 0; |
| 5013 |
|
| 5014 |
for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
| 5015 |
lsei_next_function_in_partition (&lsei)) |
| 5016 |
{ |
| 5017 |
cgraph_node *cnode = lsei_cgraph_node (lsei); |
| 5018 |
if (cnode->definition && !cnode->alias) |
| 5019 |
count++; |
| 5020 |
} |
| 5021 |
streamer_write_uhwi (ob, count); |
| 5022 |
|
| 5023 |
for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
| 5024 |
lsei_next_function_in_partition (&lsei)) |
| 5025 |
{ |
| 5026 |
cgraph_node *cnode = lsei_cgraph_node (lsei); |
| 5027 |
if (cnode->definition && !cnode->alias) |
| 5028 |
{ |
| 5029 |
class ipa_fn_summary *info = ipa_fn_summaries->get (cnode); |
| 5030 |
class ipa_size_summary *size_info = ipa_size_summaries->get (cnode); |
| 5031 |
struct bitpack_d bp; |
| 5032 |
struct cgraph_edge *edge; |
| 5033 |
int i; |
| 5034 |
size_time_entry *e; |
| 5035 |
struct condition *c; |
| 5036 |
|
| 5037 |
streamer_write_uhwi (ob, lto_symtab_encoder_encode (encoder, cnode)); |
| 5038 |
streamer_write_hwi (ob, size_info->estimated_self_stack_size); |
| 5039 |
streamer_write_hwi (ob, size_info->self_size); |
| 5040 |
info->time.stream_out (ob); |
| 5041 |
bp = bitpack_create (ob->main_stream); |
| 5042 |
bp_pack_value (&bp, info->inlinable, 1); |
| 5043 |
bp_pack_value (&bp, info->fp_expressions, 1); |
| 5044 |
streamer_write_bitpack (&bp); |
| 5045 |
if (!lto_stream_offload_p) |
| 5046 |
streamer_write_uhwi (ob, info->target_info); |
| 5047 |
streamer_write_uhwi (ob, vec_safe_length (info->conds)); |
| 5048 |
for (i = 0; vec_safe_iterate (info->conds, i, &c); i++) |
| 5049 |
{ |
| 5050 |
int j; |
| 5051 |
struct expr_eval_op *op; |
| 5052 |
|
| 5053 |
streamer_write_uhwi (ob, c->operand_num); |
| 5054 |
streamer_write_uhwi (ob, c->code); |
| 5055 |
stream_write_tree (ob, c->type, true); |
| 5056 |
stream_write_tree (ob, c->val, true); |
| 5057 |
bp = bitpack_create (ob->main_stream); |
| 5058 |
bp_pack_value (&bp, c->agg_contents, 1); |
| 5059 |
bp_pack_value (&bp, c->by_ref, 1); |
| 5060 |
streamer_write_bitpack (&bp); |
| 5061 |
if (c->agg_contents) |
| 5062 |
streamer_write_uhwi (ob, c->offset); |
| 5063 |
streamer_write_uhwi (ob, vec_safe_length (c->param_ops)); |
| 5064 |
for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++) |
| 5065 |
{ |
| 5066 |
streamer_write_uhwi (ob, op->code); |
| 5067 |
stream_write_tree (ob, op->type, true); |
| 5068 |
if (op->val[0]) |
| 5069 |
{ |
| 5070 |
bp = bitpack_create (ob->main_stream); |
| 5071 |
bp_pack_value (&bp, op->index, 2); |
| 5072 |
streamer_write_bitpack (&bp); |
| 5073 |
stream_write_tree (ob, op->val[0], true); |
| 5074 |
if (op->val[1]) |
| 5075 |
stream_write_tree (ob, op->val[1], true); |
| 5076 |
} |
| 5077 |
} |
| 5078 |
} |
| 5079 |
streamer_write_uhwi (ob, info->size_time_table.length ()); |
| 5080 |
for (i = 0; info->size_time_table.iterate (i, &e); i++) |
| 5081 |
{ |
| 5082 |
streamer_write_uhwi (ob, e->size); |
| 5083 |
e->time.stream_out (ob); |
| 5084 |
e->exec_predicate.stream_out (ob); |
| 5085 |
e->nonconst_predicate.stream_out (ob); |
| 5086 |
} |
| 5087 |
ipa_freqcounting_predicate *fcp; |
| 5088 |
streamer_write_uhwi (ob, vec_safe_length (info->loop_iterations)); |
| 5089 |
for (i = 0; vec_safe_iterate (info->loop_iterations, i, &fcp); i++) |
| 5090 |
{ |
| 5091 |
fcp->predicate->stream_out (ob); |
| 5092 |
fcp->freq.stream_out (ob); |
| 5093 |
} |
| 5094 |
streamer_write_uhwi (ob, vec_safe_length (info->loop_strides)); |
| 5095 |
for (i = 0; vec_safe_iterate (info->loop_strides, i, &fcp); i++) |
| 5096 |
{ |
| 5097 |
fcp->predicate->stream_out (ob); |
| 5098 |
fcp->freq.stream_out (ob); |
| 5099 |
} |
| 5100 |
streamer_write_uhwi (ob, info->builtin_constant_p_parms.length ()); |
| 5101 |
int ip; |
| 5102 |
for (i = 0; info->builtin_constant_p_parms.iterate (i, &ip); |
| 5103 |
i++) |
| 5104 |
streamer_write_uhwi (ob, ip); |
| 5105 |
for (edge = cnode->callees; edge; edge = edge->next_callee) |
| 5106 |
write_ipa_call_summary (ob, edge); |
| 5107 |
for (edge = cnode->indirect_calls; edge; edge = edge->next_callee) |
| 5108 |
write_ipa_call_summary (ob, edge); |
| 5109 |
} |
| 5110 |
} |
| 5111 |
streamer_write_char_stream (ob->main_stream, 0); |
| 5112 |
produce_asm (ob); |
| 5113 |
destroy_output_block (ob); |
| 5114 |
|
| 5115 |
ipa_prop_write_jump_functions (); |
| 5116 |
} |
| 5117 |
|
| 5118 |
|
| 5119 |
/* Release function summary. */ |
| 5120 |
|
| 5121 |
void |
| 5122 |
ipa_free_fn_summary (void) |
| 5123 |
{ |
| 5124 |
if (!ipa_call_summaries) |
| 5125 |
return; |
| 5126 |
ggc_delete (ipa_fn_summaries); |
| 5127 |
ipa_fn_summaries = NULL; |
| 5128 |
delete ipa_call_summaries; |
| 5129 |
ipa_call_summaries = NULL; |
| 5130 |
edge_predicate_pool.release (); |
| 5131 |
/* During IPA this is one of largest datastructures to release. */ |
| 5132 |
if (flag_wpa) |
| 5133 |
ggc_trim (); |
| 5134 |
} |
| 5135 |
|
| 5136 |
/* Release function summary. */ |
| 5137 |
|
| 5138 |
void |
| 5139 |
ipa_free_size_summary (void) |
| 5140 |
{ |
| 5141 |
if (!ipa_size_summaries) |
| 5142 |
return; |
| 5143 |
delete ipa_size_summaries; |
| 5144 |
ipa_size_summaries = NULL; |
| 5145 |
} |
| 5146 |
|
| 5147 |
namespace { |
| 5148 |
|
| 5149 |
const pass_data pass_data_local_fn_summary = |
| 5150 |
{ |
| 5151 |
GIMPLE_PASS, /* type */ |
| 5152 |
"local-fnsummary", /* name */ |
| 5153 |
OPTGROUP_INLINE, /* optinfo_flags */ |
| 5154 |
TV_INLINE_PARAMETERS, /* tv_id */ |
| 5155 |
0, /* properties_required */ |
| 5156 |
0, /* properties_provided */ |
| 5157 |
0, /* properties_destroyed */ |
| 5158 |
0, /* todo_flags_start */ |
| 5159 |
0, /* todo_flags_finish */ |
| 5160 |
}; |
| 5161 |
|
| 5162 |
class pass_local_fn_summary : public gimple_opt_pass |
| 5163 |
{ |
| 5164 |
public: |
| 5165 |
pass_local_fn_summary (gcc::context *ctxt) |
| 5166 |
: gimple_opt_pass (pass_data_local_fn_summary, ctxt) |
| 5167 |
{} |
| 5168 |
|
| 5169 |
/* opt_pass methods: */ |
| 5170 |
opt_pass * clone () final override |
| 5171 |
{ |
| 5172 |
return new pass_local_fn_summary (m_ctxt); |
| 5173 |
} |
| 5174 |
unsigned int execute (function *) final override |
| 5175 |
{ |
| 5176 |
return compute_fn_summary_for_current (); |
| 5177 |
} |
| 5178 |
|
| 5179 |
}; // class pass_local_fn_summary |
| 5180 |
|
| 5181 |
} // anon namespace |
| 5182 |
|
| 5183 |
gimple_opt_pass * |
| 5184 |
make_pass_local_fn_summary (gcc::context *ctxt) |
| 5185 |
{ |
| 5186 |
return new pass_local_fn_summary (ctxt); |
| 5187 |
} |
| 5188 |
|
| 5189 |
|
| 5190 |
/* Free inline summary. */ |
| 5191 |
|
| 5192 |
namespace { |
| 5193 |
|
| 5194 |
const pass_data pass_data_ipa_free_fn_summary = |
| 5195 |
{ |
| 5196 |
SIMPLE_IPA_PASS, /* type */ |
| 5197 |
"free-fnsummary", /* name */ |
| 5198 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 5199 |
TV_IPA_FREE_INLINE_SUMMARY, /* tv_id */ |
| 5200 |
0, /* properties_required */ |
| 5201 |
0, /* properties_provided */ |
| 5202 |
0, /* properties_destroyed */ |
| 5203 |
0, /* todo_flags_start */ |
| 5204 |
0, /* todo_flags_finish */ |
| 5205 |
}; |
| 5206 |
|
| 5207 |
class pass_ipa_free_fn_summary : public simple_ipa_opt_pass |
| 5208 |
{ |
| 5209 |
public: |
| 5210 |
pass_ipa_free_fn_summary (gcc::context *ctxt) |
| 5211 |
: simple_ipa_opt_pass (pass_data_ipa_free_fn_summary, ctxt), |
| 5212 |
small_p (false) |
| 5213 |
{} |
| 5214 |
|
| 5215 |
/* opt_pass methods: */ |
| 5216 |
opt_pass *clone () final override |
| 5217 |
{ |
| 5218 |
return new pass_ipa_free_fn_summary (m_ctxt); |
| 5219 |
} |
| 5220 |
void set_pass_param (unsigned int n, bool param) final override |
| 5221 |
{ |
| 5222 |
gcc_assert (n == 0); |
| 5223 |
small_p = param; |
| 5224 |
} |
| 5225 |
bool gate (function *) final override { return true; } |
| 5226 |
unsigned int execute (function *) final override |
| 5227 |
{ |
| 5228 |
ipa_free_fn_summary (); |
| 5229 |
/* Free ipa-prop structures if they are no longer needed. */ |
| 5230 |
ipa_free_all_structures_after_iinln (); |
| 5231 |
if (!flag_wpa) |
| 5232 |
ipa_free_size_summary (); |
| 5233 |
return 0; |
| 5234 |
} |
| 5235 |
|
| 5236 |
private: |
| 5237 |
bool small_p; |
| 5238 |
}; // class pass_ipa_free_fn_summary |
| 5239 |
|
| 5240 |
} // anon namespace |
| 5241 |
|
| 5242 |
simple_ipa_opt_pass * |
| 5243 |
make_pass_ipa_free_fn_summary (gcc::context *ctxt) |
| 5244 |
{ |
| 5245 |
return new pass_ipa_free_fn_summary (ctxt); |
| 5246 |
} |
| 5247 |
|
| 5248 |
namespace { |
| 5249 |
|
| 5250 |
const pass_data pass_data_ipa_fn_summary = |
| 5251 |
{ |
| 5252 |
IPA_PASS, /* type */ |
| 5253 |
"fnsummary", /* name */ |
| 5254 |
OPTGROUP_INLINE, /* optinfo_flags */ |
| 5255 |
TV_IPA_FNSUMMARY, /* tv_id */ |
| 5256 |
0, /* properties_required */ |
| 5257 |
0, /* properties_provided */ |
| 5258 |
0, /* properties_destroyed */ |
| 5259 |
0, /* todo_flags_start */ |
| 5260 |
( TODO_dump_symtab ), /* todo_flags_finish */ |
| 5261 |
}; |
| 5262 |
|
| 5263 |
class pass_ipa_fn_summary : public ipa_opt_pass_d |
| 5264 |
{ |
| 5265 |
public: |
| 5266 |
pass_ipa_fn_summary (gcc::context *ctxt) |
| 5267 |
: ipa_opt_pass_d (pass_data_ipa_fn_summary, ctxt, |
| 5268 |
ipa_fn_summary_generate, /* generate_summary */ |
| 5269 |
ipa_fn_summary_write, /* write_summary */ |
| 5270 |
ipa_fn_summary_read, /* read_summary */ |
| 5271 |
NULL, /* write_optimization_summary */ |
| 5272 |
NULL, /* read_optimization_summary */ |
| 5273 |
NULL, /* stmt_fixup */ |
| 5274 |
0, /* function_transform_todo_flags_start */ |
| 5275 |
NULL, /* function_transform */ |
| 5276 |
NULL) /* variable_transform */ |
| 5277 |
{} |
| 5278 |
|
| 5279 |
/* opt_pass methods: */ |
| 5280 |
unsigned int execute (function *) final override { return 0; } |
| 5281 |
|
| 5282 |
}; // class pass_ipa_fn_summary |
| 5283 |
|
| 5284 |
} // anon namespace |
| 5285 |
|
| 5286 |
ipa_opt_pass_d * |
| 5287 |
make_pass_ipa_fn_summary (gcc::context *ctxt) |
| 5288 |
{ |
| 5289 |
return new pass_ipa_fn_summary (ctxt); |
| 5290 |
} |
| 5291 |
|
| 5292 |
/* Reset all state within ipa-fnsummary.cc so that we can rerun the compiler |
| 5293 |
within the same process. For use by toplev::finalize. */ |
| 5294 |
|
| 5295 |
void |
| 5296 |
ipa_fnsummary_cc_finalize (void) |
| 5297 |
{ |
| 5298 |
ipa_free_fn_summary (); |
| 5299 |
ipa_free_size_summary (); |
| 5300 |
} |