| 1 |
/* Callgraph based analysis of static variables. |
| 2 |
Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Kenneth Zadeck <[email protected]> |
| 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 |
/* This file marks functions as being either const (TREE_READONLY) or |
| 22 |
pure (DECL_PURE_P). It can also set a variant of these that |
| 23 |
are allowed to loop indefinitely (DECL_LOOPING_CONST_PURE_P). |
| 24 |
|
| 25 |
This must be run after inlining decisions have been made since |
| 26 |
otherwise, the local sets will not contain information that is |
| 27 |
consistent with post inlined state. The global sets are not prone |
| 28 |
to this problem since they are by definition transitive. */ |
| 29 |
|
| 30 |
/* The code in this module is called by the ipa pass manager. It |
| 31 |
should be one of the later passes since it's information is used by |
| 32 |
the rest of the compilation. */ |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
#include "system.h" |
| 36 |
#include "coretypes.h" |
| 37 |
#include "backend.h" |
| 38 |
#include "target.h" |
| 39 |
#include "tree.h" |
| 40 |
#include "gimple.h" |
| 41 |
#include "tree-pass.h" |
| 42 |
#include "tree-streamer.h" |
| 43 |
#include "cgraph.h" |
| 44 |
#include "diagnostic.h" |
| 45 |
#include "calls.h" |
| 46 |
#include "cfganal.h" |
| 47 |
#include "tree-eh.h" |
| 48 |
#include "gimple-iterator.h" |
| 49 |
#include "gimple-walk.h" |
| 50 |
#include "tree-cfg.h" |
| 51 |
#include "tree-ssa-loop-niter.h" |
| 52 |
#include "langhooks.h" |
| 53 |
#include "ipa-utils.h" |
| 54 |
#include "gimple-pretty-print.h" |
| 55 |
#include "cfgloop.h" |
| 56 |
#include "tree-scalar-evolution.h" |
| 57 |
#include "intl.h" |
| 58 |
#include "opts.h" |
| 59 |
#include "ssa.h" |
| 60 |
#include "alloc-pool.h" |
| 61 |
#include "symbol-summary.h" |
| 62 |
#include "sreal.h" |
| 63 |
#include "ipa-cp.h" |
| 64 |
#include "ipa-prop.h" |
| 65 |
#include "ipa-fnsummary.h" |
| 66 |
#include "symtab-thunks.h" |
| 67 |
#include "dbgcnt.h" |
| 68 |
#include "gcc-urlifier.h" |
| 69 |
|
| 70 |
/* Lattice values for const and pure functions. Everything starts out |
| 71 |
being const, then may drop to pure and then neither depending on |
| 72 |
what is found. */ |
| 73 |
enum pure_const_state_e |
| 74 |
{ |
| 75 |
IPA_CONST, |
| 76 |
IPA_PURE, |
| 77 |
IPA_NEITHER |
| 78 |
}; |
| 79 |
|
| 80 |
static const char *pure_const_names[3] = {"const", "pure", "neither"}; |
| 81 |
|
| 82 |
enum malloc_state_e |
| 83 |
{ |
| 84 |
STATE_MALLOC_TOP, |
| 85 |
STATE_MALLOC, |
| 86 |
STATE_MALLOC_BOTTOM |
| 87 |
}; |
| 88 |
|
| 89 |
static const char *malloc_state_names[] = {"malloc_top", "malloc", "malloc_bottom"}; |
| 90 |
|
| 91 |
/* Holder for the const_state. There is one of these per function |
| 92 |
decl. */ |
| 93 |
class funct_state_d |
| 94 |
{ |
| 95 |
public: |
| 96 |
funct_state_d (): pure_const_state (IPA_NEITHER), |
| 97 |
state_previously_known (IPA_NEITHER), looping_previously_known (true), |
| 98 |
looping (true), can_throw (true), can_free (true), |
| 99 |
malloc_state (STATE_MALLOC_BOTTOM) {} |
| 100 |
|
| 101 |
funct_state_d (const funct_state_d &s): pure_const_state (s.pure_const_state), |
| 102 |
state_previously_known (s.state_previously_known), |
| 103 |
looping_previously_known (s.looping_previously_known), |
| 104 |
looping (s.looping), can_throw (s.can_throw), can_free (s.can_free), |
| 105 |
malloc_state (s.malloc_state) {} |
| 106 |
|
| 107 |
/* See above. */ |
| 108 |
enum pure_const_state_e pure_const_state; |
| 109 |
/* What user set here; we can be always sure about this. */ |
| 110 |
enum pure_const_state_e state_previously_known; |
| 111 |
bool looping_previously_known; |
| 112 |
|
| 113 |
/* True if the function could possibly infinite loop. There are a |
| 114 |
lot of ways that this could be determined. We are pretty |
| 115 |
conservative here. While it is possible to cse pure and const |
| 116 |
calls, it is not legal to have dce get rid of the call if there |
| 117 |
is a possibility that the call could infinite loop since this is |
| 118 |
a behavioral change. */ |
| 119 |
bool looping; |
| 120 |
|
| 121 |
bool can_throw; |
| 122 |
|
| 123 |
/* If function can call free, munmap or otherwise make previously |
| 124 |
non-trapping memory accesses trapping. */ |
| 125 |
bool can_free; |
| 126 |
|
| 127 |
enum malloc_state_e malloc_state; |
| 128 |
}; |
| 129 |
|
| 130 |
typedef class funct_state_d * funct_state; |
| 131 |
|
| 132 |
/* The storage of the funct_state is abstracted because there is the |
| 133 |
possibility that it may be desirable to move this to the cgraph |
| 134 |
local info. */ |
| 135 |
|
| 136 |
class funct_state_summary_t: |
| 137 |
public fast_function_summary <funct_state_d *, va_heap> |
| 138 |
{ |
| 139 |
public: |
| 140 |
funct_state_summary_t (symbol_table *symtab): |
| 141 |
fast_function_summary <funct_state_d *, va_heap> (symtab) {} |
| 142 |
|
| 143 |
void insert (cgraph_node *, funct_state_d *state) final override; |
| 144 |
void duplicate (cgraph_node *src_node, cgraph_node *dst_node, |
| 145 |
funct_state_d *src_data, |
| 146 |
funct_state_d *dst_data) final override; |
| 147 |
}; |
| 148 |
|
| 149 |
static funct_state_summary_t *funct_state_summaries = NULL; |
| 150 |
|
| 151 |
static bool gate_pure_const (void); |
| 152 |
|
| 153 |
namespace { |
| 154 |
|
| 155 |
const pass_data pass_data_ipa_pure_const = |
| 156 |
{ |
| 157 |
IPA_PASS, /* type */ |
| 158 |
"pure-const", /* name */ |
| 159 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 160 |
TV_IPA_PURE_CONST, /* tv_id */ |
| 161 |
0, /* properties_required */ |
| 162 |
0, /* properties_provided */ |
| 163 |
0, /* properties_destroyed */ |
| 164 |
0, /* todo_flags_start */ |
| 165 |
0, /* todo_flags_finish */ |
| 166 |
}; |
| 167 |
|
| 168 |
class pass_ipa_pure_const : public ipa_opt_pass_d |
| 169 |
{ |
| 170 |
public: |
| 171 |
pass_ipa_pure_const(gcc::context *ctxt); |
| 172 |
|
| 173 |
/* opt_pass methods: */ |
| 174 |
bool gate (function *) final override { return gate_pure_const (); } |
| 175 |
unsigned int execute (function *fun) final override; |
| 176 |
|
| 177 |
void register_hooks (void); |
| 178 |
|
| 179 |
private: |
| 180 |
bool init_p; |
| 181 |
}; // class pass_ipa_pure_const |
| 182 |
|
| 183 |
} // anon namespace |
| 184 |
|
| 185 |
/* Try to guess if function body will always be visible to compiler |
| 186 |
when compiling the call and whether compiler will be able |
| 187 |
to propagate the information by itself. */ |
| 188 |
|
| 189 |
static bool |
| 190 |
function_always_visible_to_compiler_p (tree decl) |
| 191 |
{ |
| 192 |
return (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl) |
| 193 |
|| DECL_COMDAT (decl)); |
| 194 |
} |
| 195 |
|
| 196 |
/* Emit suggestion about attribute ATTRIB_NAME for DECL. KNOWN_FINITE |
| 197 |
is true if the function is known to be finite. The diagnostic is |
| 198 |
controlled by OPTION. WARNED_ABOUT is a hash_set<tree> unique for |
| 199 |
OPTION, this function may initialize it and it is always returned |
| 200 |
by the function. */ |
| 201 |
|
| 202 |
static hash_set<tree> * |
| 203 |
suggest_attribute (diagnostic_option_id option, tree decl, bool known_finite, |
| 204 |
hash_set<tree> *warned_about, |
| 205 |
const char * attrib_name) |
| 206 |
{ |
| 207 |
if (!option_enabled (option.m_idx, lang_hooks.option_lang_mask (), |
| 208 |
&global_options)) |
| 209 |
return warned_about; |
| 210 |
if (TREE_THIS_VOLATILE (decl) |
| 211 |
|| (known_finite && function_always_visible_to_compiler_p (decl))) |
| 212 |
return warned_about; |
| 213 |
|
| 214 |
if (!warned_about) |
| 215 |
warned_about = new hash_set<tree>; |
| 216 |
if (warned_about->contains (decl)) |
| 217 |
return warned_about; |
| 218 |
warned_about->add (decl); |
| 219 |
auto_urlify_attributes sentinel; |
| 220 |
warning_at (DECL_SOURCE_LOCATION (decl), |
| 221 |
option, |
| 222 |
known_finite |
| 223 |
? G_("function might be candidate for attribute %qs") |
| 224 |
: G_("function might be candidate for attribute %qs" |
| 225 |
" if it is known to return normally"), attrib_name); |
| 226 |
return warned_about; |
| 227 |
} |
| 228 |
|
| 229 |
/* Emit suggestion about __attribute_((pure)) for DECL. KNOWN_FINITE |
| 230 |
is true if the function is known to be finite. */ |
| 231 |
|
| 232 |
static void |
| 233 |
warn_function_pure (tree decl, bool known_finite) |
| 234 |
{ |
| 235 |
/* Declaring a void function pure makes no sense and is diagnosed |
| 236 |
by -Wattributes because calling it would have no effect. */ |
| 237 |
if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) |
| 238 |
return; |
| 239 |
|
| 240 |
static hash_set<tree> *warned_about; |
| 241 |
warned_about |
| 242 |
= suggest_attribute (OPT_Wsuggest_attribute_pure, decl, |
| 243 |
known_finite, warned_about, "pure"); |
| 244 |
} |
| 245 |
|
| 246 |
/* Emit suggestion about __attribute_((const)) for DECL. KNOWN_FINITE |
| 247 |
is true if the function is known to be finite. */ |
| 248 |
|
| 249 |
static void |
| 250 |
warn_function_const (tree decl, bool known_finite) |
| 251 |
{ |
| 252 |
/* Declaring a void function const makes no sense is diagnosed |
| 253 |
by -Wattributes because calling it would have no effect. */ |
| 254 |
if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) |
| 255 |
return; |
| 256 |
|
| 257 |
static hash_set<tree> *warned_about; |
| 258 |
warned_about |
| 259 |
= suggest_attribute (OPT_Wsuggest_attribute_const, decl, |
| 260 |
known_finite, warned_about, "const"); |
| 261 |
} |
| 262 |
|
| 263 |
/* Emit suggestion about __attribute__((malloc)) for DECL. */ |
| 264 |
|
| 265 |
static void |
| 266 |
warn_function_malloc (tree decl) |
| 267 |
{ |
| 268 |
static hash_set<tree> *warned_about; |
| 269 |
warned_about |
| 270 |
= suggest_attribute (OPT_Wsuggest_attribute_malloc, decl, |
| 271 |
true, warned_about, "malloc"); |
| 272 |
} |
| 273 |
|
| 274 |
/* Emit suggestion about __attribute__((noreturn)) for DECL. */ |
| 275 |
|
| 276 |
static void |
| 277 |
warn_function_noreturn (tree decl) |
| 278 |
{ |
| 279 |
tree original_decl = decl; |
| 280 |
|
| 281 |
static hash_set<tree> *warned_about; |
| 282 |
if (!lang_hooks.missing_noreturn_ok_p (decl) |
| 283 |
&& targetm.warn_func_return (decl)) |
| 284 |
warned_about |
| 285 |
= suggest_attribute (OPT_Wsuggest_attribute_noreturn, original_decl, |
| 286 |
true, warned_about, "noreturn"); |
| 287 |
} |
| 288 |
|
| 289 |
void |
| 290 |
warn_function_cold (tree decl) |
| 291 |
{ |
| 292 |
tree original_decl = decl; |
| 293 |
|
| 294 |
static hash_set<tree> *warned_about; |
| 295 |
warned_about |
| 296 |
= suggest_attribute (OPT_Wsuggest_attribute_cold, original_decl, |
| 297 |
true, warned_about, "cold"); |
| 298 |
} |
| 299 |
|
| 300 |
void |
| 301 |
warn_function_returns_nonnull (tree decl) |
| 302 |
{ |
| 303 |
static hash_set<tree> *warned_about; |
| 304 |
warned_about |
| 305 |
= suggest_attribute (OPT_Wsuggest_attribute_returns_nonnull, decl, |
| 306 |
true, warned_about, "returns_nonnull"); |
| 307 |
} |
| 308 |
|
| 309 |
/* Check to see if the use (or definition when CHECKING_WRITE is true) |
| 310 |
variable T is legal in a function that is either pure or const. */ |
| 311 |
|
| 312 |
static inline void |
| 313 |
check_decl (funct_state local, |
| 314 |
tree t, bool checking_write, bool ipa) |
| 315 |
{ |
| 316 |
/* Do not want to do anything with volatile except mark any |
| 317 |
function that uses one to be not const or pure. */ |
| 318 |
if (TREE_THIS_VOLATILE (t)) |
| 319 |
{ |
| 320 |
local->pure_const_state = IPA_NEITHER; |
| 321 |
if (dump_file) |
| 322 |
fprintf (dump_file, " Volatile operand is not const/pure\n"); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
/* Do not care about a local automatic that is not static. */ |
| 327 |
if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) |
| 328 |
return; |
| 329 |
|
| 330 |
/* If the variable has the "used" attribute, treat it as if it had a |
| 331 |
been touched by the devil. */ |
| 332 |
if (DECL_PRESERVE_P (t)) |
| 333 |
{ |
| 334 |
local->pure_const_state = IPA_NEITHER; |
| 335 |
if (dump_file) |
| 336 |
fprintf (dump_file, " Used static/global variable is not const/pure\n"); |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
/* In IPA mode we are not interested in checking actual loads and stores; |
| 341 |
they will be processed at propagation time using ipa_ref. */ |
| 342 |
if (ipa) |
| 343 |
return; |
| 344 |
|
| 345 |
/* Since we have dealt with the locals and params cases above, if we |
| 346 |
are CHECKING_WRITE, this cannot be a pure or constant |
| 347 |
function. */ |
| 348 |
if (checking_write) |
| 349 |
{ |
| 350 |
local->pure_const_state = IPA_NEITHER; |
| 351 |
if (dump_file) |
| 352 |
fprintf (dump_file, " static/global memory write is not const/pure\n"); |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
if (DECL_EXTERNAL (t) || TREE_PUBLIC (t)) |
| 357 |
{ |
| 358 |
/* Readonly reads are safe. */ |
| 359 |
if (TREE_READONLY (t)) |
| 360 |
return; /* Read of a constant, do not change the function state. */ |
| 361 |
else |
| 362 |
{ |
| 363 |
if (dump_file) |
| 364 |
fprintf (dump_file, " global memory read is not const\n"); |
| 365 |
/* Just a regular read. */ |
| 366 |
if (local->pure_const_state == IPA_CONST) |
| 367 |
local->pure_const_state = IPA_PURE; |
| 368 |
} |
| 369 |
} |
| 370 |
else |
| 371 |
{ |
| 372 |
/* Compilation level statics can be read if they are readonly |
| 373 |
variables. */ |
| 374 |
if (TREE_READONLY (t)) |
| 375 |
return; |
| 376 |
|
| 377 |
if (dump_file) |
| 378 |
fprintf (dump_file, " static memory read is not const\n"); |
| 379 |
/* Just a regular read. */ |
| 380 |
if (local->pure_const_state == IPA_CONST) |
| 381 |
local->pure_const_state = IPA_PURE; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/* Check to see if the use (or definition when CHECKING_WRITE is true) |
| 387 |
variable T is legal in a function that is either pure or const. */ |
| 388 |
|
| 389 |
static inline void |
| 390 |
check_op (funct_state local, tree t, bool checking_write) |
| 391 |
{ |
| 392 |
t = get_base_address (t); |
| 393 |
if (t && TREE_THIS_VOLATILE (t)) |
| 394 |
{ |
| 395 |
local->pure_const_state = IPA_NEITHER; |
| 396 |
if (dump_file) |
| 397 |
fprintf (dump_file, " Volatile indirect ref is not const/pure\n"); |
| 398 |
return; |
| 399 |
} |
| 400 |
else if (refs_local_or_readonly_memory_p (t)) |
| 401 |
{ |
| 402 |
if (dump_file) |
| 403 |
fprintf (dump_file, " Indirect ref to local or readonly " |
| 404 |
"memory is OK\n"); |
| 405 |
return; |
| 406 |
} |
| 407 |
else if (checking_write) |
| 408 |
{ |
| 409 |
local->pure_const_state = IPA_NEITHER; |
| 410 |
if (dump_file) |
| 411 |
fprintf (dump_file, " Indirect ref write is not const/pure\n"); |
| 412 |
return; |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
if (dump_file) |
| 417 |
fprintf (dump_file, " Indirect ref read is not const\n"); |
| 418 |
if (local->pure_const_state == IPA_CONST) |
| 419 |
local->pure_const_state = IPA_PURE; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/* compute state based on ECF FLAGS and store to STATE and LOOPING. */ |
| 424 |
|
| 425 |
static void |
| 426 |
state_from_flags (enum pure_const_state_e *state, bool *looping, |
| 427 |
int flags, bool cannot_lead_to_return) |
| 428 |
{ |
| 429 |
*looping = false; |
| 430 |
if (flags & ECF_LOOPING_CONST_OR_PURE) |
| 431 |
{ |
| 432 |
*looping = true; |
| 433 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 434 |
fprintf (dump_file, " looping\n"); |
| 435 |
} |
| 436 |
if (flags & ECF_CONST) |
| 437 |
{ |
| 438 |
*state = IPA_CONST; |
| 439 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 440 |
fprintf (dump_file, " const\n"); |
| 441 |
} |
| 442 |
else if (flags & ECF_PURE) |
| 443 |
{ |
| 444 |
*state = IPA_PURE; |
| 445 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 446 |
fprintf (dump_file, " pure\n"); |
| 447 |
} |
| 448 |
else if (cannot_lead_to_return) |
| 449 |
{ |
| 450 |
*state = IPA_PURE; |
| 451 |
*looping = true; |
| 452 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 453 |
fprintf (dump_file, " ignoring side effects->pure looping\n"); |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 458 |
fprintf (dump_file, " neither\n"); |
| 459 |
*state = IPA_NEITHER; |
| 460 |
*looping = true; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
/* Merge STATE and STATE2 and LOOPING and LOOPING2 and store |
| 465 |
into STATE and LOOPING better of the two variants. |
| 466 |
Be sure to merge looping correctly. IPA_NEITHER functions |
| 467 |
have looping 0 even if they don't have to return. */ |
| 468 |
|
| 469 |
static inline void |
| 470 |
better_state (enum pure_const_state_e *state, bool *looping, |
| 471 |
enum pure_const_state_e state2, bool looping2) |
| 472 |
{ |
| 473 |
if (state2 < *state) |
| 474 |
{ |
| 475 |
if (*state == IPA_NEITHER) |
| 476 |
*looping = looping2; |
| 477 |
else |
| 478 |
*looping = MIN (*looping, looping2); |
| 479 |
*state = state2; |
| 480 |
} |
| 481 |
else if (state2 != IPA_NEITHER) |
| 482 |
*looping = MIN (*looping, looping2); |
| 483 |
} |
| 484 |
|
| 485 |
/* Merge STATE and STATE2 and LOOPING and LOOPING2 and store |
| 486 |
into STATE and LOOPING worse of the two variants. |
| 487 |
N is the actual node called. */ |
| 488 |
|
| 489 |
static inline void |
| 490 |
worse_state (enum pure_const_state_e *state, bool *looping, |
| 491 |
enum pure_const_state_e state2, bool looping2, |
| 492 |
struct symtab_node *from, |
| 493 |
struct symtab_node *to) |
| 494 |
{ |
| 495 |
/* Consider function: |
| 496 |
|
| 497 |
bool a(int *p) |
| 498 |
{ |
| 499 |
return *p==*p; |
| 500 |
} |
| 501 |
|
| 502 |
During early optimization we will turn this into: |
| 503 |
|
| 504 |
bool a(int *p) |
| 505 |
{ |
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
Now if this function will be detected as CONST however when interposed it |
| 510 |
may end up being just pure. We always must assume the worst scenario here. |
| 511 |
*/ |
| 512 |
if (*state == IPA_CONST && state2 == IPA_CONST |
| 513 |
&& to && !TREE_READONLY (to->decl) && !to->binds_to_current_def_p (from)) |
| 514 |
{ |
| 515 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 516 |
fprintf (dump_file, "Dropping state to PURE because call to %s may not " |
| 517 |
"bind to current def.\n", to->dump_name ()); |
| 518 |
state2 = IPA_PURE; |
| 519 |
} |
| 520 |
*state = MAX (*state, state2); |
| 521 |
*looping = MAX (*looping, looping2); |
| 522 |
} |
| 523 |
|
| 524 |
/* Recognize special cases of builtins that are by themselves not const |
| 525 |
but function using them is. */ |
| 526 |
bool |
| 527 |
builtin_safe_for_const_function_p (bool *looping, tree callee) |
| 528 |
{ |
| 529 |
if (DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) |
| 530 |
switch (DECL_FUNCTION_CODE (callee)) |
| 531 |
{ |
| 532 |
case BUILT_IN_RETURN: |
| 533 |
case BUILT_IN_UNREACHABLE: |
| 534 |
CASE_BUILT_IN_ALLOCA: |
| 535 |
case BUILT_IN_STACK_SAVE: |
| 536 |
case BUILT_IN_STACK_RESTORE: |
| 537 |
case BUILT_IN_EH_POINTER: |
| 538 |
case BUILT_IN_EH_FILTER: |
| 539 |
case BUILT_IN_UNWIND_RESUME: |
| 540 |
case BUILT_IN_CXA_END_CLEANUP: |
| 541 |
case BUILT_IN_EH_COPY_VALUES: |
| 542 |
case BUILT_IN_FRAME_ADDRESS: |
| 543 |
case BUILT_IN_APPLY_ARGS: |
| 544 |
case BUILT_IN_ASAN_BEFORE_DYNAMIC_INIT: |
| 545 |
case BUILT_IN_ASAN_AFTER_DYNAMIC_INIT: |
| 546 |
case BUILT_IN_DWARF_CFA: |
| 547 |
case BUILT_IN_RETURN_ADDRESS: |
| 548 |
*looping = false; |
| 549 |
return true; |
| 550 |
case BUILT_IN_PREFETCH: |
| 551 |
*looping = true; |
| 552 |
return true; |
| 553 |
default: |
| 554 |
break; |
| 555 |
} |
| 556 |
return false; |
| 557 |
} |
| 558 |
|
| 559 |
/* Check the parameters of a function call to CALL_EXPR to see if |
| 560 |
there are any references in the parameters that are not allowed for |
| 561 |
pure or const functions. Also check to see if this is either an |
| 562 |
indirect call, a call outside the compilation unit, or has special |
| 563 |
attributes that may also effect the purity. The CALL_EXPR node for |
| 564 |
the entire call expression. */ |
| 565 |
|
| 566 |
static void |
| 567 |
check_call (funct_state local, gcall *call, bool ipa) |
| 568 |
{ |
| 569 |
int flags = gimple_call_flags (call); |
| 570 |
tree callee_t = gimple_call_fndecl (call); |
| 571 |
bool possibly_throws = stmt_could_throw_p (cfun, call); |
| 572 |
bool possibly_throws_externally = (possibly_throws |
| 573 |
&& stmt_can_throw_external (cfun, call)); |
| 574 |
|
| 575 |
if (possibly_throws) |
| 576 |
{ |
| 577 |
unsigned int i; |
| 578 |
for (i = 0; i < gimple_num_ops (call); i++) |
| 579 |
if (gimple_op (call, i) |
| 580 |
&& tree_could_throw_p (gimple_op (call, i))) |
| 581 |
{ |
| 582 |
if (possibly_throws && cfun->can_throw_non_call_exceptions) |
| 583 |
{ |
| 584 |
if (dump_file) |
| 585 |
fprintf (dump_file, " operand can throw; looping\n"); |
| 586 |
local->looping = true; |
| 587 |
} |
| 588 |
if (possibly_throws_externally) |
| 589 |
{ |
| 590 |
if (dump_file) |
| 591 |
fprintf (dump_file, " operand can throw externally\n"); |
| 592 |
local->can_throw = true; |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/* The const and pure flags are set by a variety of places in the |
| 598 |
compiler (including here). If someone has already set the flags |
| 599 |
for the callee, (such as for some of the builtins) we will use |
| 600 |
them, otherwise we will compute our own information. |
| 601 |
|
| 602 |
Const and pure functions have less clobber effects than other |
| 603 |
functions so we process these first. Otherwise if it is a call |
| 604 |
outside the compilation unit or an indirect call we punt. This |
| 605 |
leaves local calls which will be processed by following the call |
| 606 |
graph. */ |
| 607 |
if (callee_t) |
| 608 |
{ |
| 609 |
bool call_looping; |
| 610 |
|
| 611 |
if (gimple_call_builtin_p (call, BUILT_IN_NORMAL) |
| 612 |
&& !nonfreeing_call_p (call)) |
| 613 |
local->can_free = true; |
| 614 |
|
| 615 |
if (builtin_safe_for_const_function_p (&call_looping, callee_t)) |
| 616 |
{ |
| 617 |
worse_state (&local->pure_const_state, &local->looping, |
| 618 |
IPA_CONST, call_looping, |
| 619 |
NULL, NULL); |
| 620 |
return; |
| 621 |
} |
| 622 |
/* When bad things happen to bad functions, they cannot be const |
| 623 |
or pure. */ |
| 624 |
if (setjmp_call_p (callee_t)) |
| 625 |
{ |
| 626 |
if (dump_file) |
| 627 |
fprintf (dump_file, " setjmp is not const/pure\n"); |
| 628 |
local->looping = true; |
| 629 |
local->pure_const_state = IPA_NEITHER; |
| 630 |
} |
| 631 |
|
| 632 |
if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL) |
| 633 |
switch (DECL_FUNCTION_CODE (callee_t)) |
| 634 |
{ |
| 635 |
case BUILT_IN_LONGJMP: |
| 636 |
case BUILT_IN_NONLOCAL_GOTO: |
| 637 |
if (dump_file) |
| 638 |
fprintf (dump_file, |
| 639 |
" longjmp and nonlocal goto is not const/pure\n"); |
| 640 |
local->pure_const_state = IPA_NEITHER; |
| 641 |
local->looping = true; |
| 642 |
break; |
| 643 |
default: |
| 644 |
break; |
| 645 |
} |
| 646 |
} |
| 647 |
else if (gimple_call_internal_p (call) && !nonfreeing_call_p (call)) |
| 648 |
local->can_free = true; |
| 649 |
|
| 650 |
/* When not in IPA mode, we can still handle self recursion. */ |
| 651 |
if (!ipa && callee_t |
| 652 |
&& recursive_call_p (current_function_decl, callee_t)) |
| 653 |
{ |
| 654 |
if (dump_file) |
| 655 |
fprintf (dump_file, " Recursive call can loop.\n"); |
| 656 |
local->looping = true; |
| 657 |
} |
| 658 |
/* Either callee is unknown or we are doing local analysis. |
| 659 |
Look to see if there are any bits available for the callee (such as by |
| 660 |
declaration or because it is builtin) and process solely on the basis of |
| 661 |
those bits. Handle internal calls always, those calls don't have |
| 662 |
corresponding cgraph edges and thus aren't processed during |
| 663 |
the propagation. */ |
| 664 |
else if (!ipa || gimple_call_internal_p (call)) |
| 665 |
{ |
| 666 |
enum pure_const_state_e call_state; |
| 667 |
bool call_looping; |
| 668 |
if (possibly_throws && cfun->can_throw_non_call_exceptions) |
| 669 |
{ |
| 670 |
if (dump_file) |
| 671 |
fprintf (dump_file, " can throw; looping\n"); |
| 672 |
local->looping = true; |
| 673 |
} |
| 674 |
if (possibly_throws_externally) |
| 675 |
{ |
| 676 |
if (dump_file) |
| 677 |
{ |
| 678 |
fprintf (dump_file, " can throw externally to lp %i\n", |
| 679 |
lookup_stmt_eh_lp (call)); |
| 680 |
if (callee_t) |
| 681 |
fprintf (dump_file, " callee:%s\n", |
| 682 |
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (callee_t))); |
| 683 |
} |
| 684 |
local->can_throw = true; |
| 685 |
} |
| 686 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 687 |
fprintf (dump_file, " checking flags for call:"); |
| 688 |
state_from_flags (&call_state, &call_looping, flags, |
| 689 |
((flags & (ECF_NORETURN | ECF_NOTHROW)) |
| 690 |
== (ECF_NORETURN | ECF_NOTHROW)) |
| 691 |
|| (!flag_exceptions && (flags & ECF_NORETURN))); |
| 692 |
worse_state (&local->pure_const_state, &local->looping, |
| 693 |
call_state, call_looping, NULL, NULL); |
| 694 |
} |
| 695 |
/* Direct functions calls are handled by IPA propagation. */ |
| 696 |
} |
| 697 |
|
| 698 |
/* Wrapper around check_decl for loads in local more. */ |
| 699 |
|
| 700 |
static bool |
| 701 |
check_load (gimple *, tree op, tree, void *data) |
| 702 |
{ |
| 703 |
if (DECL_P (op)) |
| 704 |
check_decl ((funct_state)data, op, false, false); |
| 705 |
else |
| 706 |
check_op ((funct_state)data, op, false); |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
/* Wrapper around check_decl for stores in local more. */ |
| 711 |
|
| 712 |
static bool |
| 713 |
check_store (gimple *, tree op, tree, void *data) |
| 714 |
{ |
| 715 |
if (DECL_P (op)) |
| 716 |
check_decl ((funct_state)data, op, true, false); |
| 717 |
else |
| 718 |
check_op ((funct_state)data, op, true); |
| 719 |
return false; |
| 720 |
} |
| 721 |
|
| 722 |
/* Wrapper around check_decl for loads in ipa mode. */ |
| 723 |
|
| 724 |
static bool |
| 725 |
check_ipa_load (gimple *, tree op, tree, void *data) |
| 726 |
{ |
| 727 |
if (DECL_P (op)) |
| 728 |
check_decl ((funct_state)data, op, false, true); |
| 729 |
else |
| 730 |
check_op ((funct_state)data, op, false); |
| 731 |
return false; |
| 732 |
} |
| 733 |
|
| 734 |
/* Wrapper around check_decl for stores in ipa mode. */ |
| 735 |
|
| 736 |
static bool |
| 737 |
check_ipa_store (gimple *, tree op, tree, void *data) |
| 738 |
{ |
| 739 |
if (DECL_P (op)) |
| 740 |
check_decl ((funct_state)data, op, true, true); |
| 741 |
else |
| 742 |
check_op ((funct_state)data, op, true); |
| 743 |
return false; |
| 744 |
} |
| 745 |
|
| 746 |
/* Look into pointer pointed to by GSIP and figure out what interesting side |
| 747 |
effects it has. */ |
| 748 |
static void |
| 749 |
check_stmt (gimple_stmt_iterator *gsip, funct_state local, bool ipa) |
| 750 |
{ |
| 751 |
gimple *stmt = gsi_stmt (*gsip); |
| 752 |
|
| 753 |
if (is_gimple_debug (stmt)) |
| 754 |
return; |
| 755 |
|
| 756 |
/* Do consider clobber as side effects before IPA, so we rather inline |
| 757 |
C++ destructors and keep clobber semantics than eliminate them. |
| 758 |
|
| 759 |
Similar logic is in ipa-modref. |
| 760 |
|
| 761 |
TODO: We may get smarter during early optimizations on these and let |
| 762 |
functions containing only clobbers to be optimized more. This is a common |
| 763 |
case of C++ destructors. */ |
| 764 |
|
| 765 |
if ((ipa || cfun->after_inlining) && gimple_clobber_p (stmt)) |
| 766 |
return; |
| 767 |
|
| 768 |
if (dump_file) |
| 769 |
{ |
| 770 |
fprintf (dump_file, " scanning: "); |
| 771 |
print_gimple_stmt (dump_file, stmt, 0); |
| 772 |
} |
| 773 |
|
| 774 |
if (gimple_has_volatile_ops (stmt) |
| 775 |
&& !gimple_clobber_p (stmt)) |
| 776 |
{ |
| 777 |
local->pure_const_state = IPA_NEITHER; |
| 778 |
if (dump_file) |
| 779 |
fprintf (dump_file, " Volatile stmt is not const/pure\n"); |
| 780 |
} |
| 781 |
|
| 782 |
/* Look for loads and stores. */ |
| 783 |
walk_stmt_load_store_ops (stmt, local, |
| 784 |
ipa ? check_ipa_load : check_load, |
| 785 |
ipa ? check_ipa_store : check_store); |
| 786 |
|
| 787 |
if (gimple_code (stmt) != GIMPLE_CALL |
| 788 |
&& stmt_could_throw_p (cfun, stmt)) |
| 789 |
{ |
| 790 |
if (cfun->can_throw_non_call_exceptions) |
| 791 |
{ |
| 792 |
if (dump_file) |
| 793 |
fprintf (dump_file, " can throw; looping\n"); |
| 794 |
local->looping = true; |
| 795 |
} |
| 796 |
if (stmt_can_throw_external (cfun, stmt)) |
| 797 |
{ |
| 798 |
if (dump_file) |
| 799 |
fprintf (dump_file, " can throw externally\n"); |
| 800 |
local->can_throw = true; |
| 801 |
} |
| 802 |
else |
| 803 |
if (dump_file) |
| 804 |
fprintf (dump_file, " can throw\n"); |
| 805 |
} |
| 806 |
switch (gimple_code (stmt)) |
| 807 |
{ |
| 808 |
case GIMPLE_CALL: |
| 809 |
check_call (local, as_a <gcall *> (stmt), ipa); |
| 810 |
break; |
| 811 |
case GIMPLE_LABEL: |
| 812 |
if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt)))) |
| 813 |
/* Target of long jump. */ |
| 814 |
{ |
| 815 |
if (dump_file) |
| 816 |
fprintf (dump_file, " nonlocal label is not const/pure\n"); |
| 817 |
local->pure_const_state = IPA_NEITHER; |
| 818 |
} |
| 819 |
break; |
| 820 |
case GIMPLE_ASM: |
| 821 |
if (gimple_asm_clobbers_memory_p (as_a <gasm *> (stmt))) |
| 822 |
{ |
| 823 |
if (dump_file) |
| 824 |
fprintf (dump_file, " memory asm clobber is not const/pure\n"); |
| 825 |
/* Abandon all hope, ye who enter here. */ |
| 826 |
local->pure_const_state = IPA_NEITHER; |
| 827 |
local->can_free = true; |
| 828 |
} |
| 829 |
if (gimple_asm_volatile_p (as_a <gasm *> (stmt))) |
| 830 |
{ |
| 831 |
if (dump_file) |
| 832 |
fprintf (dump_file, " volatile is not const/pure\n"); |
| 833 |
/* Abandon all hope, ye who enter here. */ |
| 834 |
local->pure_const_state = IPA_NEITHER; |
| 835 |
local->looping = true; |
| 836 |
local->can_free = true; |
| 837 |
} |
| 838 |
return; |
| 839 |
default: |
| 840 |
break; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
/* Check that RETVAL is used only in STMT and in comparisons against 0. |
| 845 |
RETVAL is return value of the function and STMT is return stmt. */ |
| 846 |
|
| 847 |
static bool |
| 848 |
check_retval_uses (tree retval, gimple *stmt) |
| 849 |
{ |
| 850 |
imm_use_iterator use_iter; |
| 851 |
gimple *use_stmt; |
| 852 |
|
| 853 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, retval) |
| 854 |
if (gcond *cond = dyn_cast<gcond *> (use_stmt)) |
| 855 |
{ |
| 856 |
tree op2 = gimple_cond_rhs (cond); |
| 857 |
if (!integer_zerop (op2)) |
| 858 |
return false; |
| 859 |
} |
| 860 |
else if (gassign *ga = dyn_cast<gassign *> (use_stmt)) |
| 861 |
{ |
| 862 |
enum tree_code code = gimple_assign_rhs_code (ga); |
| 863 |
if (TREE_CODE_CLASS (code) != tcc_comparison) |
| 864 |
return false; |
| 865 |
if (!integer_zerop (gimple_assign_rhs2 (ga))) |
| 866 |
return false; |
| 867 |
} |
| 868 |
else if (is_gimple_debug (use_stmt)) |
| 869 |
; |
| 870 |
else if (use_stmt != stmt) |
| 871 |
return false; |
| 872 |
|
| 873 |
return true; |
| 874 |
} |
| 875 |
|
| 876 |
/* malloc_candidate_p() checks if FUN can possibly be annotated with malloc |
| 877 |
attribute. Currently this function does a very conservative analysis. |
| 878 |
FUN is considered to be a candidate if |
| 879 |
1) It returns a value of pointer type. |
| 880 |
2) SSA_NAME_DEF_STMT (return_value) is either a function call or |
| 881 |
a phi, and element of phi is either NULL or |
| 882 |
SSA_NAME_DEF_STMT(element) is function call. |
| 883 |
3) The return-value has immediate uses only within comparisons (gcond or gassign) |
| 884 |
and return_stmt (and likewise a phi arg has immediate use only within comparison |
| 885 |
or the phi stmt). */ |
| 886 |
|
| 887 |
#define DUMP_AND_RETURN(reason) \ |
| 888 |
{ \ |
| 889 |
if (dump_file && (dump_flags & TDF_DETAILS)) \ |
| 890 |
fprintf (dump_file, "\n%s is not a malloc candidate, reason: %s\n", \ |
| 891 |
(node->dump_name ()), (reason)); \ |
| 892 |
return false; \ |
| 893 |
} |
| 894 |
|
| 895 |
static bool |
| 896 |
malloc_candidate_p_1 (function *fun, tree retval, gimple *ret_stmt, bool ipa, |
| 897 |
bitmap visited) |
| 898 |
{ |
| 899 |
cgraph_node *node = cgraph_node::get_create (fun->decl); |
| 900 |
if (!bitmap_set_bit (visited, SSA_NAME_VERSION (retval))) |
| 901 |
return true; |
| 902 |
|
| 903 |
if (!check_retval_uses (retval, ret_stmt)) |
| 904 |
DUMP_AND_RETURN("Return value has uses outside return stmt" |
| 905 |
" and comparisons against 0.") |
| 906 |
|
| 907 |
gimple *def = SSA_NAME_DEF_STMT (retval); |
| 908 |
|
| 909 |
if (gcall *call_stmt = dyn_cast<gcall *> (def)) |
| 910 |
{ |
| 911 |
tree callee_decl = gimple_call_fndecl (call_stmt); |
| 912 |
if (!callee_decl) |
| 913 |
return false; |
| 914 |
|
| 915 |
if (!ipa && !DECL_IS_MALLOC (callee_decl)) |
| 916 |
DUMP_AND_RETURN("callee_decl does not have malloc attribute for" |
| 917 |
" non-ipa mode.") |
| 918 |
|
| 919 |
cgraph_edge *cs = node->get_edge (call_stmt); |
| 920 |
if (cs) |
| 921 |
{ |
| 922 |
ipa_call_summary *es = ipa_call_summaries->get_create (cs); |
| 923 |
es->is_return_callee_uncaptured = true; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
else if (gphi *phi = dyn_cast<gphi *> (def)) |
| 928 |
{ |
| 929 |
bool all_args_zero = true; |
| 930 |
for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) |
| 931 |
{ |
| 932 |
tree arg = gimple_phi_arg_def (phi, i); |
| 933 |
if (integer_zerop (arg)) |
| 934 |
continue; |
| 935 |
|
| 936 |
all_args_zero = false; |
| 937 |
if (TREE_CODE (arg) != SSA_NAME) |
| 938 |
DUMP_AND_RETURN ("phi arg is not SSA_NAME."); |
| 939 |
if (!check_retval_uses (arg, phi)) |
| 940 |
DUMP_AND_RETURN ("phi arg has uses outside phi" |
| 941 |
" and comparisons against 0.") |
| 942 |
|
| 943 |
gimple *arg_def = SSA_NAME_DEF_STMT (arg); |
| 944 |
if (is_a<gphi *> (arg_def)) |
| 945 |
{ |
| 946 |
if (!malloc_candidate_p_1 (fun, arg, phi, ipa, visited)) |
| 947 |
DUMP_AND_RETURN ("nested phi fail") |
| 948 |
continue; |
| 949 |
} |
| 950 |
|
| 951 |
gcall *call_stmt = dyn_cast<gcall *> (arg_def); |
| 952 |
if (!call_stmt) |
| 953 |
DUMP_AND_RETURN ("phi arg is a not a call_stmt.") |
| 954 |
|
| 955 |
tree callee_decl = gimple_call_fndecl (call_stmt); |
| 956 |
if (!callee_decl) |
| 957 |
return false; |
| 958 |
if (!ipa && !DECL_IS_MALLOC (callee_decl)) |
| 959 |
DUMP_AND_RETURN("callee_decl does not have malloc attribute" |
| 960 |
" for non-ipa mode.") |
| 961 |
|
| 962 |
cgraph_edge *cs = node->get_edge (call_stmt); |
| 963 |
if (cs) |
| 964 |
{ |
| 965 |
ipa_call_summary *es = ipa_call_summaries->get_create (cs); |
| 966 |
es->is_return_callee_uncaptured = true; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
if (all_args_zero) |
| 971 |
DUMP_AND_RETURN ("Return value is a phi with all args equal to 0.") |
| 972 |
} |
| 973 |
|
| 974 |
else |
| 975 |
DUMP_AND_RETURN("def_stmt of return value is not a call or phi-stmt.") |
| 976 |
|
| 977 |
return true; |
| 978 |
} |
| 979 |
|
| 980 |
static bool |
| 981 |
malloc_candidate_p (function *fun, bool ipa) |
| 982 |
{ |
| 983 |
basic_block exit_block = EXIT_BLOCK_PTR_FOR_FN (fun); |
| 984 |
edge e; |
| 985 |
edge_iterator ei; |
| 986 |
cgraph_node *node = cgraph_node::get_create (fun->decl); |
| 987 |
|
| 988 |
if (EDGE_COUNT (exit_block->preds) == 0 |
| 989 |
|| !flag_delete_null_pointer_checks) |
| 990 |
return false; |
| 991 |
|
| 992 |
auto_bitmap visited; |
| 993 |
FOR_EACH_EDGE (e, ei, exit_block->preds) |
| 994 |
{ |
| 995 |
gimple_stmt_iterator gsi = gsi_last_bb (e->src); |
| 996 |
greturn *ret_stmt = dyn_cast<greturn *> (gsi_stmt (gsi)); |
| 997 |
|
| 998 |
if (!ret_stmt) |
| 999 |
return false; |
| 1000 |
|
| 1001 |
tree retval = gimple_return_retval (ret_stmt); |
| 1002 |
if (!retval) |
| 1003 |
DUMP_AND_RETURN("No return value.") |
| 1004 |
|
| 1005 |
if (TREE_CODE (retval) != SSA_NAME |
| 1006 |
|| TREE_CODE (TREE_TYPE (retval)) != POINTER_TYPE) |
| 1007 |
DUMP_AND_RETURN("Return value is not SSA_NAME or not a pointer type.") |
| 1008 |
|
| 1009 |
if (!malloc_candidate_p_1 (fun, retval, ret_stmt, ipa, visited)) |
| 1010 |
return false; |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1014 |
fprintf (dump_file, "\nFound %s to be candidate for malloc attribute\n", |
| 1015 |
IDENTIFIER_POINTER (DECL_NAME (fun->decl))); |
| 1016 |
return true; |
| 1017 |
} |
| 1018 |
|
| 1019 |
#undef DUMP_AND_RETURN |
| 1020 |
|
| 1021 |
/* Return true if function is known to be finite. */ |
| 1022 |
|
| 1023 |
bool |
| 1024 |
finite_function_p () |
| 1025 |
{ |
| 1026 |
/* Const functions cannot have back edges (an |
| 1027 |
indication of possible infinite loop side |
| 1028 |
effect. */ |
| 1029 |
bool finite = true; |
| 1030 |
if (mark_dfs_back_edges ()) |
| 1031 |
{ |
| 1032 |
/* Preheaders are needed for SCEV to work. |
| 1033 |
Simple latches and recorded exits improve chances that loop will |
| 1034 |
proved to be finite in testcases such as in loop-15.c |
| 1035 |
and loop-24.c */ |
| 1036 |
loop_optimizer_init (LOOPS_HAVE_PREHEADERS |
| 1037 |
| LOOPS_HAVE_SIMPLE_LATCHES |
| 1038 |
| LOOPS_HAVE_RECORDED_EXITS); |
| 1039 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1040 |
flow_loops_dump (dump_file, NULL, 0); |
| 1041 |
if (mark_irreducible_loops ()) |
| 1042 |
{ |
| 1043 |
if (dump_file) |
| 1044 |
fprintf (dump_file, " has irreducible loops\n"); |
| 1045 |
finite = false; |
| 1046 |
} |
| 1047 |
else |
| 1048 |
{ |
| 1049 |
scev_initialize (); |
| 1050 |
for (auto loop : loops_list (cfun, 0)) |
| 1051 |
if (!finite_loop_p (loop)) |
| 1052 |
{ |
| 1053 |
if (dump_file) |
| 1054 |
fprintf (dump_file, " cannot prove finiteness of " |
| 1055 |
"loop %i\n", loop->num); |
| 1056 |
finite =false; |
| 1057 |
break; |
| 1058 |
} |
| 1059 |
scev_finalize (); |
| 1060 |
} |
| 1061 |
loop_optimizer_finalize (); |
| 1062 |
} |
| 1063 |
return finite; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* This is the main routine for finding the reference patterns for |
| 1067 |
global variables within a function FN. */ |
| 1068 |
|
| 1069 |
static funct_state |
| 1070 |
analyze_function (struct cgraph_node *fn, bool ipa) |
| 1071 |
{ |
| 1072 |
tree decl = fn->decl; |
| 1073 |
funct_state l; |
| 1074 |
basic_block this_block; |
| 1075 |
|
| 1076 |
l = XCNEW (class funct_state_d); |
| 1077 |
l->pure_const_state = IPA_CONST; |
| 1078 |
l->state_previously_known = IPA_NEITHER; |
| 1079 |
l->looping_previously_known = true; |
| 1080 |
l->looping = false; |
| 1081 |
l->can_throw = false; |
| 1082 |
l->can_free = false; |
| 1083 |
state_from_flags (&l->state_previously_known, &l->looping_previously_known, |
| 1084 |
flags_from_decl_or_type (fn->decl), |
| 1085 |
fn->cannot_return_p ()); |
| 1086 |
|
| 1087 |
if (fn->thunk || fn->alias) |
| 1088 |
{ |
| 1089 |
/* Thunk gets propagated through, so nothing interesting happens. */ |
| 1090 |
gcc_assert (ipa); |
| 1091 |
if (fn->thunk && thunk_info::get (fn)->virtual_offset_p) |
| 1092 |
l->pure_const_state = IPA_NEITHER; |
| 1093 |
return l; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (dump_file) |
| 1097 |
{ |
| 1098 |
fprintf (dump_file, "\n\n local analysis of %s\n ", |
| 1099 |
fn->dump_name ()); |
| 1100 |
} |
| 1101 |
|
| 1102 |
push_cfun (DECL_STRUCT_FUNCTION (decl)); |
| 1103 |
|
| 1104 |
FOR_EACH_BB_FN (this_block, cfun) |
| 1105 |
{ |
| 1106 |
gimple_stmt_iterator gsi; |
| 1107 |
struct walk_stmt_info wi; |
| 1108 |
|
| 1109 |
memset (&wi, 0, sizeof (wi)); |
| 1110 |
for (gsi = gsi_start_bb (this_block); |
| 1111 |
!gsi_end_p (gsi); |
| 1112 |
gsi_next (&gsi)) |
| 1113 |
{ |
| 1114 |
/* NULL memory accesses terminates BB. These accesses are known |
| 1115 |
to trip undefined behaviour. gimple-ssa-isolate-paths turns them |
| 1116 |
to volatile accesses and adds builtin_trap call which would |
| 1117 |
confuse us otherwise. */ |
| 1118 |
if (infer_nonnull_range_by_dereference (gsi_stmt (gsi), |
| 1119 |
null_pointer_node)) |
| 1120 |
{ |
| 1121 |
if (dump_file) |
| 1122 |
fprintf (dump_file, " NULL memory access; terminating BB%s\n", |
| 1123 |
flag_non_call_exceptions ? "; looping" : ""); |
| 1124 |
if (flag_non_call_exceptions) |
| 1125 |
{ |
| 1126 |
l->looping = true; |
| 1127 |
if (stmt_can_throw_external (cfun, gsi_stmt (gsi))) |
| 1128 |
{ |
| 1129 |
if (dump_file) |
| 1130 |
fprintf (dump_file, " can throw externally\n"); |
| 1131 |
l->can_throw = true; |
| 1132 |
} |
| 1133 |
} |
| 1134 |
break; |
| 1135 |
} |
| 1136 |
check_stmt (&gsi, l, ipa); |
| 1137 |
if (l->pure_const_state == IPA_NEITHER |
| 1138 |
&& l->looping |
| 1139 |
&& l->can_throw |
| 1140 |
&& l->can_free) |
| 1141 |
goto end; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
end: |
| 1146 |
if (l->pure_const_state != IPA_NEITHER |
| 1147 |
&& !l->looping |
| 1148 |
&& !finite_function_p ()) |
| 1149 |
l->looping = true; |
| 1150 |
|
| 1151 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1152 |
fprintf (dump_file, " checking previously known:"); |
| 1153 |
|
| 1154 |
better_state (&l->pure_const_state, &l->looping, |
| 1155 |
l->state_previously_known, |
| 1156 |
l->looping_previously_known); |
| 1157 |
if (TREE_NOTHROW (decl)) |
| 1158 |
l->can_throw = false; |
| 1159 |
|
| 1160 |
l->malloc_state = STATE_MALLOC_BOTTOM; |
| 1161 |
if (DECL_IS_MALLOC (decl)) |
| 1162 |
l->malloc_state = STATE_MALLOC; |
| 1163 |
else if (ipa && malloc_candidate_p (DECL_STRUCT_FUNCTION (decl), true)) |
| 1164 |
l->malloc_state = STATE_MALLOC_TOP; |
| 1165 |
else if (malloc_candidate_p (DECL_STRUCT_FUNCTION (decl), false)) |
| 1166 |
l->malloc_state = STATE_MALLOC; |
| 1167 |
|
| 1168 |
pop_cfun (); |
| 1169 |
if (dump_file) |
| 1170 |
{ |
| 1171 |
if (l->looping) |
| 1172 |
fprintf (dump_file, "Function is locally looping.\n"); |
| 1173 |
if (l->can_throw) |
| 1174 |
fprintf (dump_file, "Function is locally throwing.\n"); |
| 1175 |
if (l->pure_const_state == IPA_CONST) |
| 1176 |
fprintf (dump_file, "Function is locally const.\n"); |
| 1177 |
if (l->pure_const_state == IPA_PURE) |
| 1178 |
fprintf (dump_file, "Function is locally pure.\n"); |
| 1179 |
if (l->can_free) |
| 1180 |
fprintf (dump_file, "Function can locally free.\n"); |
| 1181 |
if (l->malloc_state == STATE_MALLOC) |
| 1182 |
fprintf (dump_file, "Function is locally malloc.\n"); |
| 1183 |
} |
| 1184 |
return l; |
| 1185 |
} |
| 1186 |
|
| 1187 |
void |
| 1188 |
funct_state_summary_t::insert (cgraph_node *node, funct_state_d *state) |
| 1189 |
{ |
| 1190 |
/* There are some shared nodes, in particular the initializers on |
| 1191 |
static declarations. We do not need to scan them more than once |
| 1192 |
since all we would be interested in are the addressof |
| 1193 |
operations. */ |
| 1194 |
if (opt_for_fn (node->decl, flag_ipa_pure_const)) |
| 1195 |
{ |
| 1196 |
funct_state_d *a = analyze_function (node, true); |
| 1197 |
new (state) funct_state_d (*a); |
| 1198 |
free (a); |
| 1199 |
} |
| 1200 |
else |
| 1201 |
/* Do not keep stale summaries. */ |
| 1202 |
funct_state_summaries->remove (node); |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* Called when new clone is inserted to callgraph late. */ |
| 1206 |
|
| 1207 |
void |
| 1208 |
funct_state_summary_t::duplicate (cgraph_node *, cgraph_node *dst, |
| 1209 |
funct_state_d *src_data, |
| 1210 |
funct_state_d *dst_data) |
| 1211 |
{ |
| 1212 |
new (dst_data) funct_state_d (*src_data); |
| 1213 |
if (dst_data->malloc_state == STATE_MALLOC |
| 1214 |
&& VOID_TYPE_P (TREE_TYPE (TREE_TYPE (dst->decl)))) |
| 1215 |
dst_data->malloc_state = STATE_MALLOC_BOTTOM; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
void |
| 1220 |
pass_ipa_pure_const:: |
| 1221 |
register_hooks (void) |
| 1222 |
{ |
| 1223 |
if (init_p) |
| 1224 |
return; |
| 1225 |
|
| 1226 |
init_p = true; |
| 1227 |
|
| 1228 |
funct_state_summaries = new funct_state_summary_t (symtab); |
| 1229 |
} |
| 1230 |
|
| 1231 |
|
| 1232 |
/* Analyze each function in the cgraph to see if it is locally PURE or |
| 1233 |
CONST. */ |
| 1234 |
|
| 1235 |
static void |
| 1236 |
pure_const_generate_summary (void) |
| 1237 |
{ |
| 1238 |
struct cgraph_node *node; |
| 1239 |
|
| 1240 |
pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass); |
| 1241 |
pass->register_hooks (); |
| 1242 |
|
| 1243 |
/* Process all of the functions. |
| 1244 |
|
| 1245 |
We process AVAIL_INTERPOSABLE functions. We cannot use the results |
| 1246 |
by default, but the info can be used at LTO with -fwhole-program or |
| 1247 |
when function got cloned and the clone is AVAILABLE. */ |
| 1248 |
|
| 1249 |
FOR_EACH_DEFINED_FUNCTION (node) |
| 1250 |
if (opt_for_fn (node->decl, flag_ipa_pure_const)) |
| 1251 |
{ |
| 1252 |
funct_state_d *a = analyze_function (node, true); |
| 1253 |
new (funct_state_summaries->get_create (node)) funct_state_d (*a); |
| 1254 |
free (a); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
/* Serialize the ipa info for lto. */ |
| 1260 |
|
| 1261 |
static void |
| 1262 |
pure_const_write_summary (void) |
| 1263 |
{ |
| 1264 |
struct cgraph_node *node; |
| 1265 |
struct lto_simple_output_block *ob |
| 1266 |
= lto_create_simple_output_block (LTO_section_ipa_pure_const); |
| 1267 |
unsigned int count = 0; |
| 1268 |
lto_symtab_encoder_iterator lsei; |
| 1269 |
lto_symtab_encoder_t encoder; |
| 1270 |
|
| 1271 |
encoder = lto_get_out_decl_state ()->symtab_node_encoder; |
| 1272 |
|
| 1273 |
for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
| 1274 |
lsei_next_function_in_partition (&lsei)) |
| 1275 |
{ |
| 1276 |
node = lsei_cgraph_node (lsei); |
| 1277 |
if (node->definition && funct_state_summaries->exists (node)) |
| 1278 |
count++; |
| 1279 |
} |
| 1280 |
|
| 1281 |
streamer_write_uhwi_stream (ob->main_stream, count); |
| 1282 |
|
| 1283 |
/* Process all of the functions. */ |
| 1284 |
for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei); |
| 1285 |
lsei_next_function_in_partition (&lsei)) |
| 1286 |
{ |
| 1287 |
node = lsei_cgraph_node (lsei); |
| 1288 |
funct_state_d *fs = funct_state_summaries->get (node); |
| 1289 |
if (node->definition && fs != NULL) |
| 1290 |
{ |
| 1291 |
struct bitpack_d bp; |
| 1292 |
int node_ref; |
| 1293 |
lto_symtab_encoder_t encoder; |
| 1294 |
|
| 1295 |
encoder = ob->decl_state->symtab_node_encoder; |
| 1296 |
node_ref = lto_symtab_encoder_encode (encoder, node); |
| 1297 |
streamer_write_uhwi_stream (ob->main_stream, node_ref); |
| 1298 |
|
| 1299 |
/* Note that flags will need to be read in the opposite |
| 1300 |
order as we are pushing the bitflags into FLAGS. */ |
| 1301 |
bp = bitpack_create (ob->main_stream); |
| 1302 |
bp_pack_value (&bp, fs->pure_const_state, 2); |
| 1303 |
bp_pack_value (&bp, fs->state_previously_known, 2); |
| 1304 |
bp_pack_value (&bp, fs->looping_previously_known, 1); |
| 1305 |
bp_pack_value (&bp, fs->looping, 1); |
| 1306 |
bp_pack_value (&bp, fs->can_throw, 1); |
| 1307 |
bp_pack_value (&bp, fs->can_free, 1); |
| 1308 |
bp_pack_value (&bp, fs->malloc_state, 2); |
| 1309 |
streamer_write_bitpack (&bp); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
lto_destroy_simple_output_block (ob); |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
/* Deserialize the ipa info for lto. */ |
| 1318 |
|
| 1319 |
static void |
| 1320 |
pure_const_read_summary (void) |
| 1321 |
{ |
| 1322 |
struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data (); |
| 1323 |
struct lto_file_decl_data *file_data; |
| 1324 |
unsigned int j = 0; |
| 1325 |
|
| 1326 |
pass_ipa_pure_const *pass = static_cast <pass_ipa_pure_const *> (current_pass); |
| 1327 |
pass->register_hooks (); |
| 1328 |
|
| 1329 |
while ((file_data = file_data_vec[j++])) |
| 1330 |
{ |
| 1331 |
const char *data; |
| 1332 |
size_t len; |
| 1333 |
class lto_input_block *ib |
| 1334 |
= lto_create_simple_input_block (file_data, |
| 1335 |
LTO_section_ipa_pure_const, |
| 1336 |
&data, &len); |
| 1337 |
if (ib) |
| 1338 |
{ |
| 1339 |
unsigned int i; |
| 1340 |
unsigned int count = streamer_read_uhwi (ib); |
| 1341 |
|
| 1342 |
for (i = 0; i < count; i++) |
| 1343 |
{ |
| 1344 |
unsigned int index; |
| 1345 |
struct cgraph_node *node; |
| 1346 |
struct bitpack_d bp; |
| 1347 |
funct_state fs; |
| 1348 |
lto_symtab_encoder_t encoder; |
| 1349 |
|
| 1350 |
index = streamer_read_uhwi (ib); |
| 1351 |
encoder = file_data->symtab_node_encoder; |
| 1352 |
node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder, |
| 1353 |
index)); |
| 1354 |
|
| 1355 |
fs = funct_state_summaries->get_create (node); |
| 1356 |
/* Note that the flags must be read in the opposite |
| 1357 |
order in which they were written (the bitflags were |
| 1358 |
pushed into FLAGS). */ |
| 1359 |
bp = streamer_read_bitpack (ib); |
| 1360 |
fs->pure_const_state |
| 1361 |
= (enum pure_const_state_e) bp_unpack_value (&bp, 2); |
| 1362 |
fs->state_previously_known |
| 1363 |
= (enum pure_const_state_e) bp_unpack_value (&bp, 2); |
| 1364 |
fs->looping_previously_known = bp_unpack_value (&bp, 1); |
| 1365 |
fs->looping = bp_unpack_value (&bp, 1); |
| 1366 |
fs->can_throw = bp_unpack_value (&bp, 1); |
| 1367 |
fs->can_free = bp_unpack_value (&bp, 1); |
| 1368 |
fs->malloc_state |
| 1369 |
= (enum malloc_state_e) bp_unpack_value (&bp, 2); |
| 1370 |
|
| 1371 |
if (dump_file) |
| 1372 |
{ |
| 1373 |
int flags = flags_from_decl_or_type (node->decl); |
| 1374 |
fprintf (dump_file, "Read info for %s ", node->dump_name ()); |
| 1375 |
if (flags & ECF_CONST) |
| 1376 |
fprintf (dump_file, " const"); |
| 1377 |
if (flags & ECF_PURE) |
| 1378 |
fprintf (dump_file, " pure"); |
| 1379 |
if (flags & ECF_NOTHROW) |
| 1380 |
fprintf (dump_file, " nothrow"); |
| 1381 |
fprintf (dump_file, "\n pure const state: %s\n", |
| 1382 |
pure_const_names[fs->pure_const_state]); |
| 1383 |
fprintf (dump_file, " previously known state: %s\n", |
| 1384 |
pure_const_names[fs->state_previously_known]); |
| 1385 |
if (fs->looping) |
| 1386 |
fprintf (dump_file," function is locally looping\n"); |
| 1387 |
if (fs->looping_previously_known) |
| 1388 |
fprintf (dump_file," function is previously known looping\n"); |
| 1389 |
if (fs->can_throw) |
| 1390 |
fprintf (dump_file," function is locally throwing\n"); |
| 1391 |
if (fs->can_free) |
| 1392 |
fprintf (dump_file," function can locally free\n"); |
| 1393 |
fprintf (dump_file, "\n malloc state: %s\n", |
| 1394 |
malloc_state_names[fs->malloc_state]); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
lto_destroy_simple_input_block (file_data, |
| 1399 |
LTO_section_ipa_pure_const, |
| 1400 |
ib, data, len); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
/* We only propagate across edges that can throw externally and their callee |
| 1406 |
is not interposable. */ |
| 1407 |
|
| 1408 |
static bool |
| 1409 |
ignore_edge_for_nothrow (struct cgraph_edge *e) |
| 1410 |
{ |
| 1411 |
if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl)) |
| 1412 |
return true; |
| 1413 |
|
| 1414 |
enum availability avail; |
| 1415 |
cgraph_node *ultimate_target |
| 1416 |
= e->callee->function_or_virtual_thunk_symbol (&avail, e->caller); |
| 1417 |
if (avail <= AVAIL_INTERPOSABLE || TREE_NOTHROW (ultimate_target->decl)) |
| 1418 |
return true; |
| 1419 |
return ((opt_for_fn (e->callee->decl, flag_non_call_exceptions) |
| 1420 |
&& !e->callee->binds_to_current_def_p (e->caller)) |
| 1421 |
|| !opt_for_fn (e->caller->decl, flag_ipa_pure_const) |
| 1422 |
|| !opt_for_fn (ultimate_target->decl, flag_ipa_pure_const)); |
| 1423 |
} |
| 1424 |
|
| 1425 |
/* Return true if NODE is self recursive function. |
| 1426 |
Indirectly recursive functions appears as non-trivial strongly |
| 1427 |
connected components, so we need to care about self recursion |
| 1428 |
only. */ |
| 1429 |
|
| 1430 |
static bool |
| 1431 |
self_recursive_p (struct cgraph_node *node) |
| 1432 |
{ |
| 1433 |
struct cgraph_edge *e; |
| 1434 |
for (e = node->callees; e; e = e->next_callee) |
| 1435 |
if (e->callee->function_symbol () == node) |
| 1436 |
return true; |
| 1437 |
return false; |
| 1438 |
} |
| 1439 |
|
| 1440 |
/* Return true if N is cdtor that is not const or pure. In this case we may |
| 1441 |
need to remove unreachable function if it is marked const/pure. */ |
| 1442 |
|
| 1443 |
static bool |
| 1444 |
cdtor_p (cgraph_node *n, void *) |
| 1445 |
{ |
| 1446 |
if (DECL_STATIC_CONSTRUCTOR (n->decl) || DECL_STATIC_DESTRUCTOR (n->decl)) |
| 1447 |
return ((!TREE_READONLY (n->decl) && !DECL_PURE_P (n->decl)) |
| 1448 |
|| DECL_LOOPING_CONST_OR_PURE_P (n->decl)); |
| 1449 |
return false; |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* Skip edges from and to nodes without ipa_pure_const enabled. |
| 1453 |
Ignore not available symbols. */ |
| 1454 |
|
| 1455 |
static bool |
| 1456 |
ignore_edge_for_pure_const (struct cgraph_edge *e) |
| 1457 |
{ |
| 1458 |
enum availability avail; |
| 1459 |
cgraph_node *ultimate_target |
| 1460 |
= e->callee->function_or_virtual_thunk_symbol (&avail, e->caller); |
| 1461 |
|
| 1462 |
return (avail <= AVAIL_INTERPOSABLE |
| 1463 |
|| !opt_for_fn (e->caller->decl, flag_ipa_pure_const) |
| 1464 |
|| !opt_for_fn (ultimate_target->decl, |
| 1465 |
flag_ipa_pure_const)); |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* Return true if function should be skipped for local pure const analysis. */ |
| 1469 |
|
| 1470 |
static bool |
| 1471 |
skip_function_for_local_pure_const (struct cgraph_node *node) |
| 1472 |
{ |
| 1473 |
/* Because we do not schedule pass_fixup_cfg over whole program after early |
| 1474 |
optimizations we must not promote functions that are called by already |
| 1475 |
processed functions. */ |
| 1476 |
|
| 1477 |
if (function_called_by_processed_nodes_p ()) |
| 1478 |
{ |
| 1479 |
if (dump_file) |
| 1480 |
fprintf (dump_file, "Function called in recursive cycle; ignoring\n"); |
| 1481 |
return true; |
| 1482 |
} |
| 1483 |
/* Save some work and do not analyze functions which are interposable and |
| 1484 |
do not have any non-interposable aliases. */ |
| 1485 |
if (node->get_availability () <= AVAIL_INTERPOSABLE |
| 1486 |
&& !flag_lto |
| 1487 |
&& !node->has_aliases_p ()) |
| 1488 |
{ |
| 1489 |
if (dump_file) |
| 1490 |
fprintf (dump_file, |
| 1491 |
"Function is interposable; not analyzing.\n"); |
| 1492 |
return true; |
| 1493 |
} |
| 1494 |
return false; |
| 1495 |
} |
| 1496 |
|
| 1497 |
/* Make function const and output warning. If LOCAL is true, |
| 1498 |
return true if anything changed. Otherwise return true if |
| 1499 |
we may have introduced removale ctors. */ |
| 1500 |
|
| 1501 |
bool |
| 1502 |
ipa_make_function_const (struct cgraph_node *node, bool looping, bool local) |
| 1503 |
{ |
| 1504 |
bool cdtor = false; |
| 1505 |
|
| 1506 |
if (TREE_READONLY (node->decl) |
| 1507 |
&& (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl))) |
| 1508 |
return false; |
| 1509 |
warn_function_const (node->decl, !looping); |
| 1510 |
if (local && skip_function_for_local_pure_const (node)) |
| 1511 |
return false; |
| 1512 |
if (dump_file) |
| 1513 |
fprintf (dump_file, "Function found to be %sconst: %s\n", |
| 1514 |
looping ? "looping " : "", |
| 1515 |
node->dump_name ()); |
| 1516 |
if (!local && !looping) |
| 1517 |
cdtor = node->call_for_symbol_and_aliases (cdtor_p, NULL, true); |
| 1518 |
if (!dbg_cnt (ipa_attr)) |
| 1519 |
return false; |
| 1520 |
if (node->set_const_flag (true, looping)) |
| 1521 |
{ |
| 1522 |
if (dump_file) |
| 1523 |
fprintf (dump_file, |
| 1524 |
"Declaration updated to be %sconst: %s\n", |
| 1525 |
looping ? "looping " : "", |
| 1526 |
node->dump_name ()); |
| 1527 |
if (local) |
| 1528 |
return true; |
| 1529 |
return cdtor; |
| 1530 |
} |
| 1531 |
return false; |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* Make function const and output warning. If LOCAL is true, |
| 1535 |
return true if anything changed. Otherwise return true if |
| 1536 |
we may have introduced removale ctors. */ |
| 1537 |
|
| 1538 |
bool |
| 1539 |
ipa_make_function_pure (struct cgraph_node *node, bool looping, bool local) |
| 1540 |
{ |
| 1541 |
bool cdtor = false; |
| 1542 |
|
| 1543 |
if (TREE_READONLY (node->decl) |
| 1544 |
|| (DECL_PURE_P (node->decl) |
| 1545 |
&& (looping || !DECL_LOOPING_CONST_OR_PURE_P (node->decl)))) |
| 1546 |
return false; |
| 1547 |
warn_function_pure (node->decl, !looping); |
| 1548 |
if (local && skip_function_for_local_pure_const (node)) |
| 1549 |
return false; |
| 1550 |
if (dump_file) |
| 1551 |
fprintf (dump_file, "Function found to be %spure: %s\n", |
| 1552 |
looping ? "looping " : "", |
| 1553 |
node->dump_name ()); |
| 1554 |
if (!local && !looping) |
| 1555 |
cdtor = node->call_for_symbol_and_aliases (cdtor_p, NULL, true); |
| 1556 |
if (!dbg_cnt (ipa_attr)) |
| 1557 |
return false; |
| 1558 |
if (node->set_pure_flag (true, looping)) |
| 1559 |
{ |
| 1560 |
if (dump_file) |
| 1561 |
fprintf (dump_file, |
| 1562 |
"Declaration updated to be %spure: %s\n", |
| 1563 |
looping ? "looping " : "", |
| 1564 |
node->dump_name ()); |
| 1565 |
if (local) |
| 1566 |
return true; |
| 1567 |
return cdtor; |
| 1568 |
} |
| 1569 |
return false; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/* Produce transitive closure over the callgraph and compute pure/const |
| 1573 |
attributes. */ |
| 1574 |
|
| 1575 |
static bool |
| 1576 |
propagate_pure_const (void) |
| 1577 |
{ |
| 1578 |
struct cgraph_node *node; |
| 1579 |
struct cgraph_node *w; |
| 1580 |
struct cgraph_node **order = |
| 1581 |
XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
| 1582 |
int order_pos; |
| 1583 |
int i; |
| 1584 |
struct ipa_dfs_info * w_info; |
| 1585 |
bool remove_p = false; |
| 1586 |
|
| 1587 |
order_pos = ipa_reduced_postorder (order, true, |
| 1588 |
ignore_edge_for_pure_const); |
| 1589 |
if (dump_file) |
| 1590 |
{ |
| 1591 |
cgraph_node::dump_cgraph (dump_file); |
| 1592 |
ipa_print_order (dump_file, "reduced", order, order_pos); |
| 1593 |
} |
| 1594 |
|
| 1595 |
/* Propagate the local information through the call graph to produce |
| 1596 |
the global information. All the nodes within a cycle will have |
| 1597 |
the same info so we collapse cycles first. Then we can do the |
| 1598 |
propagation in one pass from the leaves to the roots. */ |
| 1599 |
for (i = 0; i < order_pos; i++ ) |
| 1600 |
{ |
| 1601 |
enum pure_const_state_e pure_const_state = IPA_CONST; |
| 1602 |
bool looping = false; |
| 1603 |
int count = 0; |
| 1604 |
node = order[i]; |
| 1605 |
|
| 1606 |
if (node->alias) |
| 1607 |
continue; |
| 1608 |
|
| 1609 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1610 |
fprintf (dump_file, "Starting cycle\n"); |
| 1611 |
|
| 1612 |
/* Find the worst state for any node in the cycle. */ |
| 1613 |
w = node; |
| 1614 |
while (w && pure_const_state != IPA_NEITHER) |
| 1615 |
{ |
| 1616 |
struct cgraph_edge *e; |
| 1617 |
struct cgraph_edge *ie; |
| 1618 |
int i; |
| 1619 |
struct ipa_ref *ref = NULL; |
| 1620 |
|
| 1621 |
funct_state w_l = funct_state_summaries->get_create (w); |
| 1622 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1623 |
fprintf (dump_file, " Visiting %s state:%s looping %i\n", |
| 1624 |
w->dump_name (), |
| 1625 |
pure_const_names[w_l->pure_const_state], |
| 1626 |
w_l->looping); |
| 1627 |
|
| 1628 |
/* First merge in function body properties. |
| 1629 |
We are safe to pass NULL as FROM and TO because we will take care |
| 1630 |
of possible interposition when walking callees. */ |
| 1631 |
worse_state (&pure_const_state, &looping, |
| 1632 |
w_l->pure_const_state, w_l->looping, |
| 1633 |
NULL, NULL); |
| 1634 |
if (pure_const_state == IPA_NEITHER) |
| 1635 |
break; |
| 1636 |
|
| 1637 |
count++; |
| 1638 |
|
| 1639 |
/* We consider recursive cycles as possibly infinite. |
| 1640 |
This might be relaxed since infinite recursion leads to stack |
| 1641 |
overflow. */ |
| 1642 |
if (count > 1) |
| 1643 |
looping = true; |
| 1644 |
|
| 1645 |
/* Now walk the edges and merge in callee properties. */ |
| 1646 |
for (e = w->callees; e && pure_const_state != IPA_NEITHER; |
| 1647 |
e = e->next_callee) |
| 1648 |
{ |
| 1649 |
enum availability avail; |
| 1650 |
struct cgraph_node *y = e->callee-> |
| 1651 |
function_or_virtual_thunk_symbol (&avail, |
| 1652 |
e->caller); |
| 1653 |
enum pure_const_state_e edge_state = IPA_CONST; |
| 1654 |
bool edge_looping = false; |
| 1655 |
|
| 1656 |
if (e->recursive_p ()) |
| 1657 |
looping = true; |
| 1658 |
|
| 1659 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1660 |
{ |
| 1661 |
fprintf (dump_file, " Call to %s", |
| 1662 |
e->callee->dump_name ()); |
| 1663 |
} |
| 1664 |
if (avail > AVAIL_INTERPOSABLE) |
| 1665 |
{ |
| 1666 |
funct_state y_l = funct_state_summaries->get_create (y); |
| 1667 |
|
| 1668 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1669 |
{ |
| 1670 |
fprintf (dump_file, |
| 1671 |
" state:%s looping:%i\n", |
| 1672 |
pure_const_names[y_l->pure_const_state], |
| 1673 |
y_l->looping); |
| 1674 |
} |
| 1675 |
if (y_l->pure_const_state > IPA_PURE |
| 1676 |
&& e->cannot_lead_to_return_p ()) |
| 1677 |
{ |
| 1678 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1679 |
fprintf (dump_file, |
| 1680 |
" Ignoring side effects" |
| 1681 |
" -> pure, looping\n"); |
| 1682 |
edge_state = IPA_PURE; |
| 1683 |
edge_looping = true; |
| 1684 |
} |
| 1685 |
else |
| 1686 |
{ |
| 1687 |
edge_state = y_l->pure_const_state; |
| 1688 |
edge_looping = y_l->looping; |
| 1689 |
} |
| 1690 |
} |
| 1691 |
else if (builtin_safe_for_const_function_p (&edge_looping, |
| 1692 |
y->decl)) |
| 1693 |
edge_state = IPA_CONST; |
| 1694 |
else |
| 1695 |
state_from_flags (&edge_state, &edge_looping, |
| 1696 |
flags_from_decl_or_type (y->decl), |
| 1697 |
e->cannot_lead_to_return_p ()); |
| 1698 |
|
| 1699 |
/* Merge the results with what we already know. */ |
| 1700 |
better_state (&edge_state, &edge_looping, |
| 1701 |
w_l->state_previously_known, |
| 1702 |
w_l->looping_previously_known); |
| 1703 |
worse_state (&pure_const_state, &looping, |
| 1704 |
edge_state, edge_looping, e->caller, e->callee); |
| 1705 |
if (pure_const_state == IPA_NEITHER) |
| 1706 |
break; |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* Now process the indirect call. */ |
| 1710 |
for (ie = w->indirect_calls; |
| 1711 |
ie && pure_const_state != IPA_NEITHER; ie = ie->next_callee) |
| 1712 |
{ |
| 1713 |
enum pure_const_state_e edge_state = IPA_CONST; |
| 1714 |
bool edge_looping = false; |
| 1715 |
|
| 1716 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1717 |
fprintf (dump_file, " Indirect call"); |
| 1718 |
state_from_flags (&edge_state, &edge_looping, |
| 1719 |
ie->indirect_info->ecf_flags, |
| 1720 |
ie->cannot_lead_to_return_p ()); |
| 1721 |
/* Merge the results with what we already know. */ |
| 1722 |
better_state (&edge_state, &edge_looping, |
| 1723 |
w_l->state_previously_known, |
| 1724 |
w_l->looping_previously_known); |
| 1725 |
worse_state (&pure_const_state, &looping, |
| 1726 |
edge_state, edge_looping, NULL, NULL); |
| 1727 |
if (pure_const_state == IPA_NEITHER) |
| 1728 |
break; |
| 1729 |
} |
| 1730 |
|
| 1731 |
/* And finally all loads and stores. */ |
| 1732 |
for (i = 0; w->iterate_reference (i, ref) |
| 1733 |
&& pure_const_state != IPA_NEITHER; i++) |
| 1734 |
{ |
| 1735 |
enum pure_const_state_e ref_state = IPA_CONST; |
| 1736 |
bool ref_looping = false; |
| 1737 |
switch (ref->use) |
| 1738 |
{ |
| 1739 |
case IPA_REF_LOAD: |
| 1740 |
/* readonly reads are safe. */ |
| 1741 |
if (TREE_READONLY (ref->referred->decl)) |
| 1742 |
break; |
| 1743 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1744 |
fprintf (dump_file, " nonreadonly global var read\n"); |
| 1745 |
ref_state = IPA_PURE; |
| 1746 |
break; |
| 1747 |
case IPA_REF_STORE: |
| 1748 |
if (ref->cannot_lead_to_return ()) |
| 1749 |
break; |
| 1750 |
ref_state = IPA_NEITHER; |
| 1751 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1752 |
fprintf (dump_file, " global var write\n"); |
| 1753 |
break; |
| 1754 |
case IPA_REF_ADDR: |
| 1755 |
break; |
| 1756 |
default: |
| 1757 |
gcc_unreachable (); |
| 1758 |
} |
| 1759 |
better_state (&ref_state, &ref_looping, |
| 1760 |
w_l->state_previously_known, |
| 1761 |
w_l->looping_previously_known); |
| 1762 |
worse_state (&pure_const_state, &looping, |
| 1763 |
ref_state, ref_looping, NULL, NULL); |
| 1764 |
if (pure_const_state == IPA_NEITHER) |
| 1765 |
break; |
| 1766 |
} |
| 1767 |
w_info = (struct ipa_dfs_info *) w->aux; |
| 1768 |
w = w_info->next_cycle; |
| 1769 |
} |
| 1770 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1771 |
fprintf (dump_file, "Result %s looping %i\n", |
| 1772 |
pure_const_names [pure_const_state], |
| 1773 |
looping); |
| 1774 |
|
| 1775 |
/* Find the worst state of can_free for any node in the cycle. */ |
| 1776 |
bool can_free = false; |
| 1777 |
w = node; |
| 1778 |
while (w && !can_free) |
| 1779 |
{ |
| 1780 |
struct cgraph_edge *e; |
| 1781 |
funct_state w_l = funct_state_summaries->get (w); |
| 1782 |
|
| 1783 |
if (w_l->can_free |
| 1784 |
|| w->get_availability () == AVAIL_INTERPOSABLE |
| 1785 |
|| w->indirect_calls) |
| 1786 |
can_free = true; |
| 1787 |
|
| 1788 |
for (e = w->callees; e && !can_free; e = e->next_callee) |
| 1789 |
{ |
| 1790 |
enum availability avail; |
| 1791 |
struct cgraph_node *y = e->callee-> |
| 1792 |
function_or_virtual_thunk_symbol (&avail, |
| 1793 |
e->caller); |
| 1794 |
|
| 1795 |
if (avail > AVAIL_INTERPOSABLE) |
| 1796 |
can_free = funct_state_summaries->get (y)->can_free; |
| 1797 |
else |
| 1798 |
can_free = true; |
| 1799 |
} |
| 1800 |
w_info = (struct ipa_dfs_info *) w->aux; |
| 1801 |
w = w_info->next_cycle; |
| 1802 |
} |
| 1803 |
|
| 1804 |
/* Copy back the region's pure_const_state which is shared by |
| 1805 |
all nodes in the region. */ |
| 1806 |
w = node; |
| 1807 |
while (w) |
| 1808 |
{ |
| 1809 |
funct_state w_l = funct_state_summaries->get (w); |
| 1810 |
enum pure_const_state_e this_state = pure_const_state; |
| 1811 |
bool this_looping = looping; |
| 1812 |
|
| 1813 |
w_l->can_free = can_free; |
| 1814 |
w->nonfreeing_fn = !can_free; |
| 1815 |
if (!can_free && dump_file) |
| 1816 |
fprintf (dump_file, "Function found not to call free: %s\n", |
| 1817 |
w->dump_name ()); |
| 1818 |
|
| 1819 |
if (w_l->state_previously_known != IPA_NEITHER |
| 1820 |
&& this_state > w_l->state_previously_known) |
| 1821 |
{ |
| 1822 |
if (this_state == IPA_NEITHER) |
| 1823 |
this_looping = w_l->looping_previously_known; |
| 1824 |
this_state = w_l->state_previously_known; |
| 1825 |
} |
| 1826 |
if (!this_looping && self_recursive_p (w)) |
| 1827 |
this_looping = true; |
| 1828 |
if (!w_l->looping_previously_known) |
| 1829 |
this_looping = false; |
| 1830 |
|
| 1831 |
/* All nodes within a cycle share the same info. */ |
| 1832 |
w_l->pure_const_state = this_state; |
| 1833 |
w_l->looping = this_looping; |
| 1834 |
|
| 1835 |
/* Inline clones share declaration with their offline copies; |
| 1836 |
do not modify their declarations since the offline copy may |
| 1837 |
be different. */ |
| 1838 |
if (!w->inlined_to) |
| 1839 |
switch (this_state) |
| 1840 |
{ |
| 1841 |
case IPA_CONST: |
| 1842 |
remove_p |= ipa_make_function_const (w, this_looping, false); |
| 1843 |
break; |
| 1844 |
|
| 1845 |
case IPA_PURE: |
| 1846 |
remove_p |= ipa_make_function_pure (w, this_looping, false); |
| 1847 |
break; |
| 1848 |
|
| 1849 |
default: |
| 1850 |
break; |
| 1851 |
} |
| 1852 |
w_info = (struct ipa_dfs_info *) w->aux; |
| 1853 |
w = w_info->next_cycle; |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
ipa_free_postorder_info (); |
| 1858 |
free (order); |
| 1859 |
return remove_p; |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* Produce transitive closure over the callgraph and compute nothrow |
| 1863 |
attributes. */ |
| 1864 |
|
| 1865 |
static void |
| 1866 |
propagate_nothrow (void) |
| 1867 |
{ |
| 1868 |
struct cgraph_node *node; |
| 1869 |
struct cgraph_node *w; |
| 1870 |
struct cgraph_node **order = |
| 1871 |
XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
| 1872 |
int order_pos; |
| 1873 |
int i; |
| 1874 |
struct ipa_dfs_info * w_info; |
| 1875 |
|
| 1876 |
order_pos = ipa_reduced_postorder (order, true, |
| 1877 |
ignore_edge_for_nothrow); |
| 1878 |
if (dump_file) |
| 1879 |
{ |
| 1880 |
cgraph_node::dump_cgraph (dump_file); |
| 1881 |
ipa_print_order (dump_file, "reduced for nothrow", order, order_pos); |
| 1882 |
} |
| 1883 |
|
| 1884 |
/* Propagate the local information through the call graph to produce |
| 1885 |
the global information. All the nodes within a cycle will have |
| 1886 |
the same info so we collapse cycles first. Then we can do the |
| 1887 |
propagation in one pass from the leaves to the roots. */ |
| 1888 |
for (i = 0; i < order_pos; i++ ) |
| 1889 |
{ |
| 1890 |
bool can_throw = false; |
| 1891 |
node = order[i]; |
| 1892 |
|
| 1893 |
if (node->alias) |
| 1894 |
continue; |
| 1895 |
|
| 1896 |
/* Find the worst state for any node in the cycle. */ |
| 1897 |
w = node; |
| 1898 |
while (w && !can_throw) |
| 1899 |
{ |
| 1900 |
struct cgraph_edge *e, *ie; |
| 1901 |
|
| 1902 |
if (!TREE_NOTHROW (w->decl)) |
| 1903 |
{ |
| 1904 |
funct_state w_l = funct_state_summaries->get_create (w); |
| 1905 |
|
| 1906 |
if (w_l->can_throw |
| 1907 |
|| w->get_availability () == AVAIL_INTERPOSABLE) |
| 1908 |
can_throw = true; |
| 1909 |
|
| 1910 |
for (e = w->callees; e && !can_throw; e = e->next_callee) |
| 1911 |
{ |
| 1912 |
enum availability avail; |
| 1913 |
|
| 1914 |
if (!e->can_throw_external || TREE_NOTHROW (e->callee->decl)) |
| 1915 |
continue; |
| 1916 |
|
| 1917 |
struct cgraph_node *y = e->callee-> |
| 1918 |
function_or_virtual_thunk_symbol (&avail, |
| 1919 |
e->caller); |
| 1920 |
|
| 1921 |
/* We can use info about the callee only if we know it |
| 1922 |
cannot be interposed. |
| 1923 |
When callee is compiled with non-call exceptions we also |
| 1924 |
must check that the declaration is bound to current |
| 1925 |
body as other semantically equivalent body may still |
| 1926 |
throw. */ |
| 1927 |
if (avail <= AVAIL_INTERPOSABLE |
| 1928 |
|| (!TREE_NOTHROW (y->decl) |
| 1929 |
&& (funct_state_summaries->get_create (y)->can_throw |
| 1930 |
|| (opt_for_fn (y->decl, flag_non_call_exceptions) |
| 1931 |
&& !e->callee->binds_to_current_def_p (w))))) |
| 1932 |
can_throw = true; |
| 1933 |
} |
| 1934 |
for (ie = w->indirect_calls; ie && !can_throw; |
| 1935 |
ie = ie->next_callee) |
| 1936 |
if (ie->can_throw_external |
| 1937 |
&& !(ie->indirect_info->ecf_flags & ECF_NOTHROW)) |
| 1938 |
can_throw = true; |
| 1939 |
} |
| 1940 |
w_info = (struct ipa_dfs_info *) w->aux; |
| 1941 |
w = w_info->next_cycle; |
| 1942 |
} |
| 1943 |
|
| 1944 |
/* Copy back the region's pure_const_state which is shared by |
| 1945 |
all nodes in the region. */ |
| 1946 |
w = node; |
| 1947 |
while (w) |
| 1948 |
{ |
| 1949 |
funct_state w_l = funct_state_summaries->get_create (w); |
| 1950 |
if (!can_throw && !TREE_NOTHROW (w->decl)) |
| 1951 |
{ |
| 1952 |
/* Inline clones share declaration with their offline copies; |
| 1953 |
do not modify their declarations since the offline copy may |
| 1954 |
be different. */ |
| 1955 |
if (!w->inlined_to) |
| 1956 |
{ |
| 1957 |
w->set_nothrow_flag (true); |
| 1958 |
if (dump_file) |
| 1959 |
fprintf (dump_file, "Function found to be nothrow: %s\n", |
| 1960 |
w->dump_name ()); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
else if (can_throw && !TREE_NOTHROW (w->decl)) |
| 1964 |
w_l->can_throw = true; |
| 1965 |
w_info = (struct ipa_dfs_info *) w->aux; |
| 1966 |
w = w_info->next_cycle; |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
ipa_free_postorder_info (); |
| 1971 |
free (order); |
| 1972 |
} |
| 1973 |
|
| 1974 |
/* Debugging function to dump state of malloc lattice. */ |
| 1975 |
|
| 1976 |
DEBUG_FUNCTION |
| 1977 |
static void |
| 1978 |
dump_malloc_lattice (FILE *dump_file, const char *s) |
| 1979 |
{ |
| 1980 |
if (!dump_file) |
| 1981 |
return; |
| 1982 |
|
| 1983 |
fprintf (dump_file, "\n\nMALLOC LATTICE %s:\n", s); |
| 1984 |
cgraph_node *node; |
| 1985 |
FOR_EACH_FUNCTION (node) |
| 1986 |
{ |
| 1987 |
funct_state fs = funct_state_summaries->get (node); |
| 1988 |
if (fs) |
| 1989 |
fprintf (dump_file, "%s: %s\n", node->dump_name (), |
| 1990 |
malloc_state_names[fs->malloc_state]); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* Propagate malloc attribute across the callgraph. */ |
| 1995 |
|
| 1996 |
static void |
| 1997 |
propagate_malloc (void) |
| 1998 |
{ |
| 1999 |
cgraph_node *node; |
| 2000 |
FOR_EACH_FUNCTION (node) |
| 2001 |
{ |
| 2002 |
if (DECL_IS_MALLOC (node->decl)) |
| 2003 |
if (!funct_state_summaries->exists (node)) |
| 2004 |
{ |
| 2005 |
funct_state fs = funct_state_summaries->get_create (node); |
| 2006 |
fs->malloc_state = STATE_MALLOC; |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
dump_malloc_lattice (dump_file, "Initial"); |
| 2011 |
struct cgraph_node **order |
| 2012 |
= XNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
| 2013 |
int order_pos = ipa_reverse_postorder (order); |
| 2014 |
bool changed = true; |
| 2015 |
|
| 2016 |
while (changed) |
| 2017 |
{ |
| 2018 |
changed = false; |
| 2019 |
/* Walk in postorder. */ |
| 2020 |
for (int i = order_pos - 1; i >= 0; --i) |
| 2021 |
{ |
| 2022 |
cgraph_node *node = order[i]; |
| 2023 |
if (node->alias |
| 2024 |
|| !node->definition |
| 2025 |
|| !funct_state_summaries->exists (node)) |
| 2026 |
continue; |
| 2027 |
|
| 2028 |
funct_state l = funct_state_summaries->get (node); |
| 2029 |
|
| 2030 |
/* FIXME: add support for indirect-calls. */ |
| 2031 |
if (node->indirect_calls) |
| 2032 |
{ |
| 2033 |
l->malloc_state = STATE_MALLOC_BOTTOM; |
| 2034 |
continue; |
| 2035 |
} |
| 2036 |
|
| 2037 |
if (node->get_availability () <= AVAIL_INTERPOSABLE) |
| 2038 |
{ |
| 2039 |
l->malloc_state = STATE_MALLOC_BOTTOM; |
| 2040 |
continue; |
| 2041 |
} |
| 2042 |
|
| 2043 |
if (l->malloc_state == STATE_MALLOC_BOTTOM) |
| 2044 |
continue; |
| 2045 |
|
| 2046 |
auto_vec<cgraph_node *, 16> callees; |
| 2047 |
for (cgraph_edge *cs = node->callees; cs; cs = cs->next_callee) |
| 2048 |
{ |
| 2049 |
ipa_call_summary *es = ipa_call_summaries->get_create (cs); |
| 2050 |
if (es && es->is_return_callee_uncaptured) |
| 2051 |
callees.safe_push (cs->callee); |
| 2052 |
} |
| 2053 |
|
| 2054 |
malloc_state_e new_state = l->malloc_state; |
| 2055 |
for (unsigned j = 0; j < callees.length (); j++) |
| 2056 |
{ |
| 2057 |
cgraph_node *callee = callees[j]; |
| 2058 |
if (!funct_state_summaries->exists (node)) |
| 2059 |
{ |
| 2060 |
new_state = STATE_MALLOC_BOTTOM; |
| 2061 |
break; |
| 2062 |
} |
| 2063 |
malloc_state_e callee_state |
| 2064 |
= funct_state_summaries->get_create (callee)->malloc_state; |
| 2065 |
if (new_state < callee_state) |
| 2066 |
new_state = callee_state; |
| 2067 |
} |
| 2068 |
if (new_state != l->malloc_state) |
| 2069 |
{ |
| 2070 |
changed = true; |
| 2071 |
l->malloc_state = new_state; |
| 2072 |
} |
| 2073 |
} |
| 2074 |
} |
| 2075 |
|
| 2076 |
FOR_EACH_DEFINED_FUNCTION (node) |
| 2077 |
if (funct_state_summaries->exists (node)) |
| 2078 |
{ |
| 2079 |
funct_state l = funct_state_summaries->get (node); |
| 2080 |
if (!node->alias |
| 2081 |
&& l->malloc_state == STATE_MALLOC |
| 2082 |
&& !node->inlined_to |
| 2083 |
&& !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (node->decl)))) |
| 2084 |
{ |
| 2085 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 2086 |
fprintf (dump_file, "Function %s found to be malloc\n", |
| 2087 |
node->dump_name ()); |
| 2088 |
|
| 2089 |
bool malloc_decl_p = DECL_IS_MALLOC (node->decl); |
| 2090 |
node->set_malloc_flag (true); |
| 2091 |
if (!malloc_decl_p && warn_suggest_attribute_malloc) |
| 2092 |
warn_function_malloc (node->decl); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
dump_malloc_lattice (dump_file, "after propagation"); |
| 2097 |
ipa_free_postorder_info (); |
| 2098 |
free (order); |
| 2099 |
} |
| 2100 |
|
| 2101 |
/* Produce the global information by preforming a transitive closure |
| 2102 |
on the local information that was produced by generate_summary. */ |
| 2103 |
|
| 2104 |
unsigned int |
| 2105 |
pass_ipa_pure_const:: |
| 2106 |
execute (function *) |
| 2107 |
{ |
| 2108 |
bool remove_p; |
| 2109 |
|
| 2110 |
/* Nothrow makes more function to not lead to return and improve |
| 2111 |
later analysis. */ |
| 2112 |
propagate_nothrow (); |
| 2113 |
propagate_malloc (); |
| 2114 |
remove_p = propagate_pure_const (); |
| 2115 |
|
| 2116 |
delete funct_state_summaries; |
| 2117 |
return remove_p ? TODO_remove_functions : 0; |
| 2118 |
} |
| 2119 |
|
| 2120 |
static bool |
| 2121 |
gate_pure_const (void) |
| 2122 |
{ |
| 2123 |
return flag_ipa_pure_const || in_lto_p; |
| 2124 |
} |
| 2125 |
|
| 2126 |
pass_ipa_pure_const::pass_ipa_pure_const(gcc::context *ctxt) |
| 2127 |
: ipa_opt_pass_d(pass_data_ipa_pure_const, ctxt, |
| 2128 |
pure_const_generate_summary, /* generate_summary */ |
| 2129 |
pure_const_write_summary, /* write_summary */ |
| 2130 |
pure_const_read_summary, /* read_summary */ |
| 2131 |
NULL, /* write_optimization_summary */ |
| 2132 |
NULL, /* read_optimization_summary */ |
| 2133 |
NULL, /* stmt_fixup */ |
| 2134 |
0, /* function_transform_todo_flags_start */ |
| 2135 |
NULL, /* function_transform */ |
| 2136 |
NULL), /* variable_transform */ |
| 2137 |
init_p (false) {} |
| 2138 |
|
| 2139 |
ipa_opt_pass_d * |
| 2140 |
make_pass_ipa_pure_const (gcc::context *ctxt) |
| 2141 |
{ |
| 2142 |
return new pass_ipa_pure_const (ctxt); |
| 2143 |
} |
| 2144 |
|
| 2145 |
/* Simple local pass for pure const discovery reusing the analysis from |
| 2146 |
ipa_pure_const. This pass is effective when executed together with |
| 2147 |
other optimization passes in early optimization pass queue. */ |
| 2148 |
|
| 2149 |
namespace { |
| 2150 |
|
| 2151 |
const pass_data pass_data_local_pure_const = |
| 2152 |
{ |
| 2153 |
GIMPLE_PASS, /* type */ |
| 2154 |
"local-pure-const", /* name */ |
| 2155 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 2156 |
TV_IPA_PURE_CONST, /* tv_id */ |
| 2157 |
0, /* properties_required */ |
| 2158 |
0, /* properties_provided */ |
| 2159 |
0, /* properties_destroyed */ |
| 2160 |
0, /* todo_flags_start */ |
| 2161 |
0, /* todo_flags_finish */ |
| 2162 |
}; |
| 2163 |
|
| 2164 |
class pass_local_pure_const : public gimple_opt_pass |
| 2165 |
{ |
| 2166 |
public: |
| 2167 |
pass_local_pure_const (gcc::context *ctxt) |
| 2168 |
: gimple_opt_pass (pass_data_local_pure_const, ctxt) |
| 2169 |
{} |
| 2170 |
|
| 2171 |
/* opt_pass methods: */ |
| 2172 |
opt_pass * clone () final override |
| 2173 |
{ |
| 2174 |
return new pass_local_pure_const (m_ctxt); |
| 2175 |
} |
| 2176 |
bool gate (function *) final override { return gate_pure_const (); } |
| 2177 |
unsigned int execute (function *) final override; |
| 2178 |
|
| 2179 |
}; // class pass_local_pure_const |
| 2180 |
|
| 2181 |
unsigned int |
| 2182 |
pass_local_pure_const::execute (function *fun) |
| 2183 |
{ |
| 2184 |
bool changed = false; |
| 2185 |
funct_state l; |
| 2186 |
bool skip; |
| 2187 |
struct cgraph_node *node; |
| 2188 |
|
| 2189 |
node = cgraph_node::get (current_function_decl); |
| 2190 |
skip = skip_function_for_local_pure_const (node); |
| 2191 |
|
| 2192 |
if (!warn_suggest_attribute_const |
| 2193 |
&& !warn_suggest_attribute_pure |
| 2194 |
&& skip) |
| 2195 |
return 0; |
| 2196 |
|
| 2197 |
l = analyze_function (node, false); |
| 2198 |
|
| 2199 |
/* Do NORETURN discovery. */ |
| 2200 |
if (!skip && !TREE_THIS_VOLATILE (current_function_decl) |
| 2201 |
&& EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0) |
| 2202 |
{ |
| 2203 |
warn_function_noreturn (fun->decl); |
| 2204 |
if (dump_file) |
| 2205 |
fprintf (dump_file, "Function found to be noreturn: %s\n", |
| 2206 |
current_function_name ()); |
| 2207 |
|
| 2208 |
/* Update declaration and reduce profile to executed once. */ |
| 2209 |
if (cgraph_node::get (current_function_decl)->set_noreturn_flag (true)) |
| 2210 |
changed = true; |
| 2211 |
if (node->frequency > NODE_FREQUENCY_EXECUTED_ONCE) |
| 2212 |
node->frequency = NODE_FREQUENCY_EXECUTED_ONCE; |
| 2213 |
} |
| 2214 |
|
| 2215 |
switch (l->pure_const_state) |
| 2216 |
{ |
| 2217 |
case IPA_CONST: |
| 2218 |
changed |= ipa_make_function_const |
| 2219 |
(cgraph_node::get (current_function_decl), l->looping, true); |
| 2220 |
break; |
| 2221 |
|
| 2222 |
case IPA_PURE: |
| 2223 |
changed |= ipa_make_function_pure |
| 2224 |
(cgraph_node::get (current_function_decl), l->looping, true); |
| 2225 |
break; |
| 2226 |
|
| 2227 |
default: |
| 2228 |
break; |
| 2229 |
} |
| 2230 |
if (!l->can_throw && !TREE_NOTHROW (current_function_decl)) |
| 2231 |
{ |
| 2232 |
node->set_nothrow_flag (true); |
| 2233 |
changed = true; |
| 2234 |
if (dump_file) |
| 2235 |
fprintf (dump_file, "Function found to be nothrow: %s\n", |
| 2236 |
current_function_name ()); |
| 2237 |
} |
| 2238 |
|
| 2239 |
if (l->malloc_state == STATE_MALLOC |
| 2240 |
&& !DECL_IS_MALLOC (current_function_decl)) |
| 2241 |
{ |
| 2242 |
node->set_malloc_flag (true); |
| 2243 |
if (warn_suggest_attribute_malloc) |
| 2244 |
warn_function_malloc (node->decl); |
| 2245 |
changed = true; |
| 2246 |
if (dump_file) |
| 2247 |
fprintf (dump_file, "Function found to be malloc: %s\n", |
| 2248 |
node->dump_name ()); |
| 2249 |
} |
| 2250 |
|
| 2251 |
free (l); |
| 2252 |
if (changed) |
| 2253 |
return execute_fixup_cfg (); |
| 2254 |
else |
| 2255 |
return 0; |
| 2256 |
} |
| 2257 |
|
| 2258 |
} // anon namespace |
| 2259 |
|
| 2260 |
gimple_opt_pass * |
| 2261 |
make_pass_local_pure_const (gcc::context *ctxt) |
| 2262 |
{ |
| 2263 |
return new pass_local_pure_const (ctxt); |
| 2264 |
} |
| 2265 |
|
| 2266 |
/* Emit noreturn warnings. */ |
| 2267 |
|
| 2268 |
namespace { |
| 2269 |
|
| 2270 |
const pass_data pass_data_warn_function_noreturn = |
| 2271 |
{ |
| 2272 |
GIMPLE_PASS, /* type */ |
| 2273 |
"*warn_function_noreturn", /* name */ |
| 2274 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 2275 |
TV_NONE, /* tv_id */ |
| 2276 |
PROP_cfg, /* properties_required */ |
| 2277 |
0, /* properties_provided */ |
| 2278 |
0, /* properties_destroyed */ |
| 2279 |
0, /* todo_flags_start */ |
| 2280 |
0, /* todo_flags_finish */ |
| 2281 |
}; |
| 2282 |
|
| 2283 |
class pass_warn_function_noreturn : public gimple_opt_pass |
| 2284 |
{ |
| 2285 |
public: |
| 2286 |
pass_warn_function_noreturn (gcc::context *ctxt) |
| 2287 |
: gimple_opt_pass (pass_data_warn_function_noreturn, ctxt) |
| 2288 |
{} |
| 2289 |
|
| 2290 |
/* opt_pass methods: */ |
| 2291 |
bool gate (function *) final override |
| 2292 |
{ |
| 2293 |
return warn_suggest_attribute_noreturn; |
| 2294 |
} |
| 2295 |
unsigned int execute (function *fun) final override |
| 2296 |
{ |
| 2297 |
if (!TREE_THIS_VOLATILE (current_function_decl) |
| 2298 |
&& EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) == 0) |
| 2299 |
warn_function_noreturn (current_function_decl); |
| 2300 |
return 0; |
| 2301 |
} |
| 2302 |
|
| 2303 |
}; // class pass_warn_function_noreturn |
| 2304 |
|
| 2305 |
} // anon namespace |
| 2306 |
|
| 2307 |
gimple_opt_pass * |
| 2308 |
make_pass_warn_function_noreturn (gcc::context *ctxt) |
| 2309 |
{ |
| 2310 |
return new pass_warn_function_noreturn (ctxt); |
| 2311 |
} |
| 2312 |
|
| 2313 |
/* Simple local pass for pure const discovery reusing the analysis from |
| 2314 |
ipa_pure_const. This pass is effective when executed together with |
| 2315 |
other optimization passes in early optimization pass queue. */ |
| 2316 |
|
| 2317 |
namespace { |
| 2318 |
|
| 2319 |
const pass_data pass_data_nothrow = |
| 2320 |
{ |
| 2321 |
GIMPLE_PASS, /* type */ |
| 2322 |
"nothrow", /* name */ |
| 2323 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 2324 |
TV_IPA_PURE_CONST, /* tv_id */ |
| 2325 |
0, /* properties_required */ |
| 2326 |
0, /* properties_provided */ |
| 2327 |
0, /* properties_destroyed */ |
| 2328 |
0, /* todo_flags_start */ |
| 2329 |
0, /* todo_flags_finish */ |
| 2330 |
}; |
| 2331 |
|
| 2332 |
class pass_nothrow : public gimple_opt_pass |
| 2333 |
{ |
| 2334 |
public: |
| 2335 |
pass_nothrow (gcc::context *ctxt) |
| 2336 |
: gimple_opt_pass (pass_data_nothrow, ctxt) |
| 2337 |
{} |
| 2338 |
|
| 2339 |
/* opt_pass methods: */ |
| 2340 |
opt_pass * clone () final override { return new pass_nothrow (m_ctxt); } |
| 2341 |
bool gate (function *) final override { return optimize; } |
| 2342 |
unsigned int execute (function *) final override; |
| 2343 |
|
| 2344 |
}; // class pass_nothrow |
| 2345 |
|
| 2346 |
unsigned int |
| 2347 |
pass_nothrow::execute (function *) |
| 2348 |
{ |
| 2349 |
struct cgraph_node *node; |
| 2350 |
basic_block this_block; |
| 2351 |
|
| 2352 |
if (TREE_NOTHROW (current_function_decl)) |
| 2353 |
return 0; |
| 2354 |
|
| 2355 |
node = cgraph_node::get (current_function_decl); |
| 2356 |
|
| 2357 |
/* We run during lowering, we cannot really use availability yet. */ |
| 2358 |
if (cgraph_node::get (current_function_decl)->get_availability () |
| 2359 |
<= AVAIL_INTERPOSABLE) |
| 2360 |
{ |
| 2361 |
if (dump_file) |
| 2362 |
fprintf (dump_file, "Function is interposable;" |
| 2363 |
" not analyzing.\n"); |
| 2364 |
return true; |
| 2365 |
} |
| 2366 |
|
| 2367 |
FOR_EACH_BB_FN (this_block, cfun) |
| 2368 |
{ |
| 2369 |
for (gimple_stmt_iterator gsi = gsi_start_bb (this_block); |
| 2370 |
!gsi_end_p (gsi); |
| 2371 |
gsi_next (&gsi)) |
| 2372 |
if (stmt_can_throw_external (cfun, gsi_stmt (gsi))) |
| 2373 |
{ |
| 2374 |
if (is_gimple_call (gsi_stmt (gsi))) |
| 2375 |
{ |
| 2376 |
tree callee_t = gimple_call_fndecl (gsi_stmt (gsi)); |
| 2377 |
if (callee_t && recursive_call_p (current_function_decl, |
| 2378 |
callee_t)) |
| 2379 |
continue; |
| 2380 |
} |
| 2381 |
|
| 2382 |
if (dump_file) |
| 2383 |
{ |
| 2384 |
fprintf (dump_file, "Statement can throw: "); |
| 2385 |
print_gimple_stmt (dump_file, gsi_stmt (gsi), 0); |
| 2386 |
} |
| 2387 |
return 0; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
|
| 2391 |
node->set_nothrow_flag (true); |
| 2392 |
|
| 2393 |
bool cfg_changed = false; |
| 2394 |
if (self_recursive_p (node)) |
| 2395 |
FOR_EACH_BB_FN (this_block, cfun) |
| 2396 |
if (gcall *g = safe_dyn_cast <gcall *> (*gsi_last_bb (this_block))) |
| 2397 |
{ |
| 2398 |
tree callee_t = gimple_call_fndecl (g); |
| 2399 |
if (callee_t |
| 2400 |
&& recursive_call_p (current_function_decl, callee_t) |
| 2401 |
&& maybe_clean_eh_stmt (g) |
| 2402 |
&& gimple_purge_dead_eh_edges (this_block)) |
| 2403 |
cfg_changed = true; |
| 2404 |
} |
| 2405 |
|
| 2406 |
if (dump_file) |
| 2407 |
fprintf (dump_file, "Function found to be nothrow: %s\n", |
| 2408 |
current_function_name ()); |
| 2409 |
return cfg_changed ? TODO_cleanup_cfg : 0; |
| 2410 |
} |
| 2411 |
|
| 2412 |
} // anon namespace |
| 2413 |
|
| 2414 |
gimple_opt_pass * |
| 2415 |
make_pass_nothrow (gcc::context *ctxt) |
| 2416 |
{ |
| 2417 |
return new pass_nothrow (ctxt); |
| 2418 |
} |