| 1 |
/* Support routines for Value Range Propagation (VRP). |
| 2 |
Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Diego Novillo <[email protected]>. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License 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 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "basic-block.h" |
| 25 |
#include "bitmap.h" |
| 26 |
#include "sbitmap.h" |
| 27 |
#include "options.h" |
| 28 |
#include "dominance.h" |
| 29 |
#include "function.h" |
| 30 |
#include "cfg.h" |
| 31 |
#include "tree.h" |
| 32 |
#include "gimple.h" |
| 33 |
#include "tree-pass.h" |
| 34 |
#include "ssa.h" |
| 35 |
#include "gimple-pretty-print.h" |
| 36 |
#include "fold-const.h" |
| 37 |
#include "cfganal.h" |
| 38 |
#include "gimple-iterator.h" |
| 39 |
#include "tree-cfg.h" |
| 40 |
#include "tree-ssa-loop-manip.h" |
| 41 |
#include "tree-ssa-loop-niter.h" |
| 42 |
#include "tree-into-ssa.h" |
| 43 |
#include "cfgloop.h" |
| 44 |
#include "tree-scalar-evolution.h" |
| 45 |
#include "tree-ssa-propagate.h" |
| 46 |
#include "domwalk.h" |
| 47 |
#include "vr-values.h" |
| 48 |
#include "gimple-array-bounds.h" |
| 49 |
#include "gimple-range.h" |
| 50 |
#include "gimple-range-path.h" |
| 51 |
#include "value-pointer-equiv.h" |
| 52 |
#include "gimple-fold.h" |
| 53 |
#include "tree-dfa.h" |
| 54 |
#include "tree-ssa-dce.h" |
| 55 |
#include "alloc-pool.h" |
| 56 |
#include "cgraph.h" |
| 57 |
#include "symbol-summary.h" |
| 58 |
#include "ipa-utils.h" |
| 59 |
#include "sreal.h" |
| 60 |
#include "ipa-cp.h" |
| 61 |
#include "ipa-prop.h" |
| 62 |
#include "attribs.h" |
| 63 |
#include "diagnostic-core.h" |
| 64 |
|
| 65 |
// This class is utilized by VRP and ranger to remove __builtin_unreachable |
| 66 |
// calls, and reflect any resulting global ranges. |
| 67 |
// |
| 68 |
// maybe_register() is called on condition statements , and if that |
| 69 |
// matches the pattern of one branch being a builtin_unreachable, either check |
| 70 |
// for early removal or register the resulting executable edge in a list. |
| 71 |
// |
| 72 |
// During early/non-final processing, we check to see if ALL exports from the |
| 73 |
// block can be safely updated with a new global value. If they can, then |
| 74 |
// we rewrite the condition and update those values immediately. Otherwise |
| 75 |
// the unreachable condition is left in the IL until the final pass. |
| 76 |
// |
| 77 |
// During final processing, after all blocks have been registered, |
| 78 |
// remove_and_update_globals() will |
| 79 |
// - check all exports from registered blocks |
| 80 |
// - ensure the cache entry of each export is set with the appropriate range |
| 81 |
// - rewrite the conditions to take the executable edge |
| 82 |
// - perform DCE on any feeding instructions to those rewritten conditions |
| 83 |
// |
| 84 |
// Then each of the immediate use chain of each export is walked, and a new |
| 85 |
// global range created by unioning the ranges at all remaining use locations. |
| 86 |
|
| 87 |
class remove_unreachable { |
| 88 |
public: |
| 89 |
remove_unreachable (range_query &r, bool all) : m_ranger (r), final_p (all) |
| 90 |
{ m_list.create (30); m_tmp = BITMAP_ALLOC (NULL); } |
| 91 |
~remove_unreachable () { BITMAP_FREE (m_tmp); m_list.release (); } |
| 92 |
void handle_early (gimple *s, edge e); |
| 93 |
void maybe_register (gimple *s); |
| 94 |
bool remove (); |
| 95 |
bool remove_and_update_globals (); |
| 96 |
bool fully_replaceable (tree name, basic_block bb); |
| 97 |
vec<std::pair<int, int> > m_list; |
| 98 |
range_query &m_ranger; |
| 99 |
bool final_p; |
| 100 |
bitmap m_tmp; |
| 101 |
}; |
| 102 |
|
| 103 |
// Check if block BB has a __builtin_unreachable () call on one arm, and |
| 104 |
// register the executable edge if so. |
| 105 |
|
| 106 |
void |
| 107 |
remove_unreachable::maybe_register (gimple *s) |
| 108 |
{ |
| 109 |
gcc_checking_assert (gimple_code (s) == GIMPLE_COND); |
| 110 |
basic_block bb = gimple_bb (s); |
| 111 |
|
| 112 |
edge e0 = EDGE_SUCC (bb, 0); |
| 113 |
basic_block bb0 = e0->dest; |
| 114 |
bool un0 = EDGE_COUNT (bb0->succs) == 0 |
| 115 |
&& gimple_seq_unreachable_p (bb_seq (bb0)); |
| 116 |
edge e1 = EDGE_SUCC (bb, 1); |
| 117 |
basic_block bb1 = e1->dest; |
| 118 |
bool un1 = EDGE_COUNT (bb1->succs) == 0 |
| 119 |
&& gimple_seq_unreachable_p (bb_seq (bb1)); |
| 120 |
|
| 121 |
// If the 2 blocks are not different, ignore. |
| 122 |
if (un0 == un1) |
| 123 |
return; |
| 124 |
|
| 125 |
// Constant expressions are ignored. |
| 126 |
if (TREE_CODE (gimple_cond_lhs (s)) != SSA_NAME |
| 127 |
&& TREE_CODE (gimple_cond_rhs (s)) != SSA_NAME) |
| 128 |
return; |
| 129 |
|
| 130 |
edge e = un0 ? e1 : e0; |
| 131 |
|
| 132 |
if (!final_p) |
| 133 |
handle_early (s, e); |
| 134 |
else |
| 135 |
m_list.safe_push (std::make_pair (e->src->index, e->dest->index)); |
| 136 |
} |
| 137 |
|
| 138 |
// Return true if all uses of NAME are dominated by block BB. 1 use |
| 139 |
// is allowed in block BB, This is one we hope to remove. |
| 140 |
// ie |
| 141 |
// _2 = _1 & 7; |
| 142 |
// if (_2 != 0) |
| 143 |
// goto <bb 3>; [0.00%] |
| 144 |
// Any additional use of _1 or _2 in this block invalidates early replacement. |
| 145 |
|
| 146 |
bool |
| 147 |
remove_unreachable::fully_replaceable (tree name, basic_block bb) |
| 148 |
{ |
| 149 |
use_operand_p use_p; |
| 150 |
imm_use_iterator iter; |
| 151 |
bool saw_in_bb = false; |
| 152 |
|
| 153 |
// If a name loads from memory, we may lose information used in |
| 154 |
// commoning opportunities later. See tree-ssa/ssa-pre-34.c. |
| 155 |
gimple *def_stmt = SSA_NAME_DEF_STMT (name); |
| 156 |
if (gimple_vuse (def_stmt)) |
| 157 |
return false; |
| 158 |
|
| 159 |
FOR_EACH_IMM_USE_FAST (use_p, iter, name) |
| 160 |
{ |
| 161 |
gimple *use_stmt = USE_STMT (use_p); |
| 162 |
// Ignore debug stmts and the branch. |
| 163 |
if (is_gimple_debug (use_stmt)) |
| 164 |
continue; |
| 165 |
basic_block use_bb = gimple_bb (use_stmt); |
| 166 |
// Only one use in the block allowed to avoid complicated cases. |
| 167 |
if (use_bb == bb) |
| 168 |
{ |
| 169 |
if (saw_in_bb) |
| 170 |
return false; |
| 171 |
else |
| 172 |
saw_in_bb = true; |
| 173 |
} |
| 174 |
else if (!dominated_by_p (CDI_DOMINATORS, use_bb, bb)) |
| 175 |
return false; |
| 176 |
} |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
// This routine is called to check builtin_unreachable calls during any |
| 181 |
// time before final removal. The only way we can be sure it does not |
| 182 |
// provide any additional information is to expect that we can update the |
| 183 |
// global values of all exports from a block. This means the branch |
| 184 |
// to the unreachable call must dominate all uses of those ssa-names, with |
| 185 |
// the exception that there can be a single use in the block containing |
| 186 |
// the branch. IF the name used in the branch is defined in the block, it may |
| 187 |
// contain the name of something else that will be an export. And likewise |
| 188 |
// that may also use another name that is an export etc. |
| 189 |
// |
| 190 |
// As long as there is only a single use, we can be sure that there are no other |
| 191 |
// side effects (like being passed to a call, or stored to a global, etc. |
| 192 |
// This means we will miss cases where there are 2 or more uses that have |
| 193 |
// no interveneing statements that may had side effects, but it catches most |
| 194 |
// of the caes we care about, and prevents expensive in depth analysis. |
| 195 |
// |
| 196 |
// Ranger will still reflect the proper ranges at other places in these missed |
| 197 |
// cases, we simply will not remove/set globals early. |
| 198 |
|
| 199 |
void |
| 200 |
remove_unreachable::handle_early (gimple *s, edge e) |
| 201 |
{ |
| 202 |
// If there is no gori_ssa, there is no early processsing. |
| 203 |
if (!m_ranger.gori_ssa ()) |
| 204 |
return ; |
| 205 |
bool lhs_p = TREE_CODE (gimple_cond_lhs (s)) == SSA_NAME; |
| 206 |
bool rhs_p = TREE_CODE (gimple_cond_rhs (s)) == SSA_NAME; |
| 207 |
// Do not remove __builtin_unreachable if it confers a relation, or |
| 208 |
// that relation may be lost in subsequent passes. |
| 209 |
if (lhs_p && rhs_p) |
| 210 |
return; |
| 211 |
// Do not remove addresses early. ie if (x == &y) |
| 212 |
if (lhs_p && TREE_CODE (gimple_cond_rhs (s)) == ADDR_EXPR) |
| 213 |
return; |
| 214 |
|
| 215 |
gcc_checking_assert (gimple_outgoing_range_stmt_p (e->src) == s); |
| 216 |
gcc_checking_assert (!final_p); |
| 217 |
|
| 218 |
// Check if every export and its dependencies are dominated by this branch. |
| 219 |
// Dependencies are required as it needs to dominate potential |
| 220 |
// recalculations. See PR 123300. |
| 221 |
tree name; |
| 222 |
FOR_EACH_GORI_EXPORT_AND_DEP_NAME (m_ranger.gori_ssa (), e->src, name, m_tmp) |
| 223 |
{ |
| 224 |
if (!fully_replaceable (name, e->src)) |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
// Set the global value for each. |
| 229 |
FOR_EACH_GORI_EXPORT_NAME (m_ranger.gori_ssa (), e->src, name) |
| 230 |
{ |
| 231 |
value_range r (TREE_TYPE (name)); |
| 232 |
m_ranger.range_on_entry (r, e->dest, name); |
| 233 |
// Nothing at this late stage we can do if the write fails. |
| 234 |
if (!set_range_info (name, r)) |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
tree ssa = lhs_p ? gimple_cond_lhs (s) : gimple_cond_rhs (s); |
| 239 |
|
| 240 |
// Rewrite the condition. |
| 241 |
if (e->flags & EDGE_TRUE_VALUE) |
| 242 |
gimple_cond_make_true (as_a<gcond *> (s)); |
| 243 |
else |
| 244 |
gimple_cond_make_false (as_a<gcond *> (s)); |
| 245 |
update_stmt (s); |
| 246 |
|
| 247 |
// If the name on S is defined in this block, see if there is DCE work to do. |
| 248 |
if (gimple_bb (SSA_NAME_DEF_STMT (ssa)) == e->src) |
| 249 |
{ |
| 250 |
auto_bitmap dce; |
| 251 |
bitmap_set_bit (dce, SSA_NAME_VERSION (ssa)); |
| 252 |
simple_dce_from_worklist (dce); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
// Process the edges in the list, change the conditions and removing any |
| 257 |
// dead code feeding those conditions. This removes the unreachables, but |
| 258 |
// makes no attempt to set globals values. |
| 259 |
|
| 260 |
bool |
| 261 |
remove_unreachable::remove () |
| 262 |
{ |
| 263 |
if (!final_p || m_list.length () == 0) |
| 264 |
return false; |
| 265 |
|
| 266 |
bool change = false; |
| 267 |
unsigned i; |
| 268 |
for (i = 0; i < m_list.length (); i++) |
| 269 |
{ |
| 270 |
auto eb = m_list[i]; |
| 271 |
basic_block src = BASIC_BLOCK_FOR_FN (cfun, eb.first); |
| 272 |
basic_block dest = BASIC_BLOCK_FOR_FN (cfun, eb.second); |
| 273 |
if (!src || !dest) |
| 274 |
continue; |
| 275 |
edge e = find_edge (src, dest); |
| 276 |
gimple *s = gimple_outgoing_range_stmt_p (e->src); |
| 277 |
gcc_checking_assert (gimple_code (s) == GIMPLE_COND); |
| 278 |
|
| 279 |
tree name = gimple_range_ssa_p (gimple_cond_lhs (s)); |
| 280 |
if (!name) |
| 281 |
name = gimple_range_ssa_p (gimple_cond_rhs (s)); |
| 282 |
// Check if global value can be set for NAME. |
| 283 |
if (name && fully_replaceable (name, src)) |
| 284 |
{ |
| 285 |
value_range r (TREE_TYPE (name)); |
| 286 |
if (gori_name_on_edge (r, name, e, &m_ranger)) |
| 287 |
set_range_info (name, r); |
| 288 |
} |
| 289 |
|
| 290 |
change = true; |
| 291 |
// Rewrite the condition. |
| 292 |
if (e->flags & EDGE_TRUE_VALUE) |
| 293 |
gimple_cond_make_true (as_a<gcond *> (s)); |
| 294 |
else |
| 295 |
gimple_cond_make_false (as_a<gcond *> (s)); |
| 296 |
update_stmt (s); |
| 297 |
} |
| 298 |
|
| 299 |
return change; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
// Process the edges in the list, change the conditions and removing any |
| 304 |
// dead code feeding those conditions. Calculate the range of any |
| 305 |
// names that may have been exported from those blocks, and determine if |
| 306 |
// there is any updates to their global ranges.. |
| 307 |
// Return true if any builtin_unreachables/globals eliminated/updated. |
| 308 |
|
| 309 |
bool |
| 310 |
remove_unreachable::remove_and_update_globals () |
| 311 |
{ |
| 312 |
if (m_list.length () == 0) |
| 313 |
return false; |
| 314 |
|
| 315 |
// If there is no import/export info, Do basic removal. |
| 316 |
if (!m_ranger.gori_ssa ()) |
| 317 |
return remove (); |
| 318 |
|
| 319 |
bool change = false; |
| 320 |
tree name; |
| 321 |
unsigned i; |
| 322 |
bitmap_iterator bi; |
| 323 |
auto_bitmap all_exports; |
| 324 |
for (i = 0; i < m_list.length (); i++) |
| 325 |
{ |
| 326 |
auto eb = m_list[i]; |
| 327 |
basic_block src = BASIC_BLOCK_FOR_FN (cfun, eb.first); |
| 328 |
basic_block dest = BASIC_BLOCK_FOR_FN (cfun, eb.second); |
| 329 |
if (!src || !dest) |
| 330 |
continue; |
| 331 |
edge e = find_edge (src, dest); |
| 332 |
gimple *s = gimple_outgoing_range_stmt_p (e->src); |
| 333 |
gcc_checking_assert (gimple_code (s) == GIMPLE_COND); |
| 334 |
|
| 335 |
bool dominate_exit_p = true; |
| 336 |
FOR_EACH_GORI_EXPORT_NAME (m_ranger.gori_ssa (), e->src, name) |
| 337 |
{ |
| 338 |
// Ensure the cache is set for NAME in the succ block. |
| 339 |
value_range r(TREE_TYPE (name)); |
| 340 |
value_range ex(TREE_TYPE (name)); |
| 341 |
m_ranger.range_on_entry (r, e->dest, name); |
| 342 |
m_ranger.range_on_entry (ex, EXIT_BLOCK_PTR_FOR_FN (cfun), name); |
| 343 |
// If the range produced by this __builtin_unreachacble expression |
| 344 |
// is not fully reflected in the range at exit, then it does not |
| 345 |
// dominate the exit of the function. |
| 346 |
if (ex.intersect (r)) |
| 347 |
dominate_exit_p = false; |
| 348 |
} |
| 349 |
|
| 350 |
// If the exit is dominated, add to the export list. Otherwise if this |
| 351 |
// isn't the final VRP pass, leave the call in the IL. |
| 352 |
if (dominate_exit_p) |
| 353 |
bitmap_ior_into (all_exports, |
| 354 |
m_ranger.gori_ssa ()->exports (e->src)); |
| 355 |
else if (!final_p) |
| 356 |
continue; |
| 357 |
|
| 358 |
change = true; |
| 359 |
// Rewrite the condition. |
| 360 |
if (e->flags & EDGE_TRUE_VALUE) |
| 361 |
gimple_cond_make_true (as_a<gcond *> (s)); |
| 362 |
else |
| 363 |
gimple_cond_make_false (as_a<gcond *> (s)); |
| 364 |
update_stmt (s); |
| 365 |
} |
| 366 |
|
| 367 |
if (bitmap_empty_p (all_exports)) |
| 368 |
return false; |
| 369 |
// Invoke DCE on all exported names to eliminate dead feeding defs. |
| 370 |
auto_bitmap dce; |
| 371 |
bitmap_copy (dce, all_exports); |
| 372 |
// Don't attempt to DCE parameters. |
| 373 |
EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi) |
| 374 |
if (!ssa_name (i) || SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))) |
| 375 |
bitmap_clear_bit (dce, i); |
| 376 |
simple_dce_from_worklist (dce); |
| 377 |
|
| 378 |
// Loop over all uses of each name and find maximal range. This is the |
| 379 |
// new global range. |
| 380 |
use_operand_p use_p; |
| 381 |
imm_use_iterator iter; |
| 382 |
EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi) |
| 383 |
{ |
| 384 |
name = ssa_name (i); |
| 385 |
if (!name || SSA_NAME_IN_FREE_LIST (name)) |
| 386 |
continue; |
| 387 |
value_range r (TREE_TYPE (name)); |
| 388 |
value_range exp_range (TREE_TYPE (name)); |
| 389 |
r.set_undefined (); |
| 390 |
FOR_EACH_IMM_USE_FAST (use_p, iter, name) |
| 391 |
{ |
| 392 |
gimple *use_stmt = USE_STMT (use_p); |
| 393 |
if (is_gimple_debug (use_stmt)) |
| 394 |
continue; |
| 395 |
if (!m_ranger.range_of_expr (exp_range, name, use_stmt)) |
| 396 |
exp_range.set_varying (TREE_TYPE (name)); |
| 397 |
r.union_ (exp_range); |
| 398 |
if (r.varying_p ()) |
| 399 |
break; |
| 400 |
} |
| 401 |
// Include the on-exit range to ensure non-dominated unreachables |
| 402 |
// don't incorrectly impact the global range. |
| 403 |
m_ranger.range_on_entry (exp_range, EXIT_BLOCK_PTR_FOR_FN (cfun), name); |
| 404 |
r.union_ (exp_range); |
| 405 |
if (r.varying_p () || r.undefined_p ()) |
| 406 |
continue; |
| 407 |
if (!set_range_info (name, r)) |
| 408 |
continue; |
| 409 |
change = true; |
| 410 |
} |
| 411 |
return change; |
| 412 |
} |
| 413 |
|
| 414 |
/* VR_TYPE describes a range with minimum value *MIN and maximum |
| 415 |
value *MAX. Restrict the range to the set of values that have |
| 416 |
no bits set outside NONZERO_BITS. Update *MIN and *MAX and |
| 417 |
return the new range type. |
| 418 |
|
| 419 |
SGN gives the sign of the values described by the range. */ |
| 420 |
|
| 421 |
enum value_range_kind |
| 422 |
intersect_range_with_nonzero_bits (enum value_range_kind vr_type, |
| 423 |
wide_int *min, wide_int *max, |
| 424 |
const wide_int &nonzero_bits, |
| 425 |
signop sgn) |
| 426 |
{ |
| 427 |
if (vr_type == VR_ANTI_RANGE) |
| 428 |
{ |
| 429 |
/* The VR_ANTI_RANGE is equivalent to the union of the ranges |
| 430 |
A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS |
| 431 |
to create an inclusive upper bound for A and an inclusive lower |
| 432 |
bound for B. */ |
| 433 |
wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits); |
| 434 |
wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits); |
| 435 |
|
| 436 |
/* If the calculation of A_MAX wrapped, A is effectively empty |
| 437 |
and A_MAX is the highest value that satisfies NONZERO_BITS. |
| 438 |
Likewise if the calculation of B_MIN wrapped, B is effectively |
| 439 |
empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */ |
| 440 |
bool a_empty = wi::ge_p (a_max, *min, sgn); |
| 441 |
bool b_empty = wi::le_p (b_min, *max, sgn); |
| 442 |
|
| 443 |
/* If both A and B are empty, there are no valid values. */ |
| 444 |
if (a_empty && b_empty) |
| 445 |
return VR_UNDEFINED; |
| 446 |
|
| 447 |
/* If exactly one of A or B is empty, return a VR_RANGE for the |
| 448 |
other one. */ |
| 449 |
if (a_empty || b_empty) |
| 450 |
{ |
| 451 |
*min = b_min; |
| 452 |
*max = a_max; |
| 453 |
gcc_checking_assert (wi::le_p (*min, *max, sgn)); |
| 454 |
return VR_RANGE; |
| 455 |
} |
| 456 |
|
| 457 |
/* Update the VR_ANTI_RANGE bounds. */ |
| 458 |
*min = a_max + 1; |
| 459 |
*max = b_min - 1; |
| 460 |
gcc_checking_assert (wi::le_p (*min, *max, sgn)); |
| 461 |
|
| 462 |
/* Now check whether the excluded range includes any values that |
| 463 |
satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */ |
| 464 |
if (wi::round_up_for_mask (*min, nonzero_bits) == b_min) |
| 465 |
{ |
| 466 |
unsigned int precision = min->get_precision (); |
| 467 |
*min = wi::min_value (precision, sgn); |
| 468 |
*max = wi::max_value (precision, sgn); |
| 469 |
vr_type = VR_RANGE; |
| 470 |
} |
| 471 |
} |
| 472 |
if (vr_type == VR_RANGE || vr_type == VR_VARYING) |
| 473 |
{ |
| 474 |
*max = wi::round_down_for_mask (*max, nonzero_bits); |
| 475 |
|
| 476 |
/* Check that the range contains at least one valid value. */ |
| 477 |
if (wi::gt_p (*min, *max, sgn)) |
| 478 |
return VR_UNDEFINED; |
| 479 |
|
| 480 |
*min = wi::round_up_for_mask (*min, nonzero_bits); |
| 481 |
gcc_checking_assert (wi::le_p (*min, *max, sgn)); |
| 482 |
} |
| 483 |
return vr_type; |
| 484 |
} |
| 485 |
|
| 486 |
/* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE |
| 487 |
otherwise. We only handle additive operations and set NEG to true if the |
| 488 |
symbol is negated and INV to the invariant part, if any. */ |
| 489 |
|
| 490 |
static tree |
| 491 |
get_single_symbol (tree t, bool *neg, tree *inv) |
| 492 |
{ |
| 493 |
bool neg_; |
| 494 |
tree inv_; |
| 495 |
|
| 496 |
*inv = NULL_TREE; |
| 497 |
*neg = false; |
| 498 |
|
| 499 |
if (TREE_CODE (t) == PLUS_EXPR |
| 500 |
|| TREE_CODE (t) == POINTER_PLUS_EXPR |
| 501 |
|| TREE_CODE (t) == MINUS_EXPR) |
| 502 |
{ |
| 503 |
if (is_gimple_min_invariant (TREE_OPERAND (t, 0))) |
| 504 |
{ |
| 505 |
neg_ = (TREE_CODE (t) == MINUS_EXPR); |
| 506 |
inv_ = TREE_OPERAND (t, 0); |
| 507 |
t = TREE_OPERAND (t, 1); |
| 508 |
} |
| 509 |
else if (is_gimple_min_invariant (TREE_OPERAND (t, 1))) |
| 510 |
{ |
| 511 |
neg_ = false; |
| 512 |
inv_ = TREE_OPERAND (t, 1); |
| 513 |
t = TREE_OPERAND (t, 0); |
| 514 |
} |
| 515 |
else |
| 516 |
return NULL_TREE; |
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
neg_ = false; |
| 521 |
inv_ = NULL_TREE; |
| 522 |
} |
| 523 |
|
| 524 |
if (TREE_CODE (t) == NEGATE_EXPR) |
| 525 |
{ |
| 526 |
t = TREE_OPERAND (t, 0); |
| 527 |
neg_ = !neg_; |
| 528 |
} |
| 529 |
|
| 530 |
if (TREE_CODE (t) != SSA_NAME) |
| 531 |
return NULL_TREE; |
| 532 |
|
| 533 |
if (inv_ && TREE_OVERFLOW_P (inv_)) |
| 534 |
inv_ = drop_tree_overflow (inv_); |
| 535 |
|
| 536 |
*neg = neg_; |
| 537 |
*inv = inv_; |
| 538 |
return t; |
| 539 |
} |
| 540 |
|
| 541 |
/* Compare two values VAL1 and VAL2. Return |
| 542 |
|
| 543 |
-2 if VAL1 and VAL2 cannot be compared at compile-time, |
| 544 |
-1 if VAL1 < VAL2, |
| 545 |
0 if VAL1 == VAL2, |
| 546 |
+1 if VAL1 > VAL2, and |
| 547 |
+2 if VAL1 != VAL2 |
| 548 |
|
| 549 |
This is similar to tree_int_cst_compare but supports pointer values |
| 550 |
and values that cannot be compared at compile time. |
| 551 |
|
| 552 |
If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to |
| 553 |
true if the return value is only valid if we assume that signed |
| 554 |
overflow is undefined. */ |
| 555 |
|
| 556 |
int |
| 557 |
compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p) |
| 558 |
{ |
| 559 |
if (val1 == val2) |
| 560 |
return 0; |
| 561 |
|
| 562 |
/* Below we rely on the fact that VAL1 and VAL2 are both pointers or |
| 563 |
both integers. */ |
| 564 |
gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1)) |
| 565 |
== POINTER_TYPE_P (TREE_TYPE (val2))); |
| 566 |
|
| 567 |
/* Convert the two values into the same type. This is needed because |
| 568 |
sizetype causes sign extension even for unsigned types. */ |
| 569 |
if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2))) |
| 570 |
val2 = fold_convert (TREE_TYPE (val1), val2); |
| 571 |
|
| 572 |
const bool overflow_undefined |
| 573 |
= INTEGRAL_TYPE_P (TREE_TYPE (val1)) |
| 574 |
&& TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)); |
| 575 |
tree inv1, inv2; |
| 576 |
bool neg1, neg2; |
| 577 |
tree sym1 = get_single_symbol (val1, &neg1, &inv1); |
| 578 |
tree sym2 = get_single_symbol (val2, &neg2, &inv2); |
| 579 |
|
| 580 |
/* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1 |
| 581 |
accordingly. If VAL1 and VAL2 don't use the same name, return -2. */ |
| 582 |
if (sym1 && sym2) |
| 583 |
{ |
| 584 |
/* Both values must use the same name with the same sign. */ |
| 585 |
if (sym1 != sym2 || neg1 != neg2) |
| 586 |
return -2; |
| 587 |
|
| 588 |
/* [-]NAME + CST == [-]NAME + CST. */ |
| 589 |
if (inv1 == inv2) |
| 590 |
return 0; |
| 591 |
|
| 592 |
/* If overflow is defined we cannot simplify more. */ |
| 593 |
if (!overflow_undefined) |
| 594 |
return -2; |
| 595 |
|
| 596 |
if (strict_overflow_p != NULL |
| 597 |
/* Symbolic range building sets the no-warning bit to declare |
| 598 |
that overflow doesn't happen. */ |
| 599 |
&& (!inv1 || !warning_suppressed_p (val1, OPT_Woverflow)) |
| 600 |
&& (!inv2 || !warning_suppressed_p (val2, OPT_Woverflow))) |
| 601 |
*strict_overflow_p = true; |
| 602 |
|
| 603 |
if (!inv1) |
| 604 |
inv1 = build_int_cst (TREE_TYPE (val1), 0); |
| 605 |
if (!inv2) |
| 606 |
inv2 = build_int_cst (TREE_TYPE (val2), 0); |
| 607 |
|
| 608 |
return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2), |
| 609 |
TYPE_SIGN (TREE_TYPE (val1))); |
| 610 |
} |
| 611 |
|
| 612 |
const bool cst1 = is_gimple_min_invariant (val1); |
| 613 |
const bool cst2 = is_gimple_min_invariant (val2); |
| 614 |
|
| 615 |
/* If one is of the form '[-]NAME + CST' and the other is constant, then |
| 616 |
it might be possible to say something depending on the constants. */ |
| 617 |
if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1)) |
| 618 |
{ |
| 619 |
if (!overflow_undefined) |
| 620 |
return -2; |
| 621 |
|
| 622 |
if (strict_overflow_p != NULL |
| 623 |
/* Symbolic range building sets the no-warning bit to declare |
| 624 |
that overflow doesn't happen. */ |
| 625 |
&& (!sym1 || !warning_suppressed_p (val1, OPT_Woverflow)) |
| 626 |
&& (!sym2 || !warning_suppressed_p (val2, OPT_Woverflow))) |
| 627 |
*strict_overflow_p = true; |
| 628 |
|
| 629 |
const signop sgn = TYPE_SIGN (TREE_TYPE (val1)); |
| 630 |
tree cst = cst1 ? val1 : val2; |
| 631 |
tree inv = cst1 ? inv2 : inv1; |
| 632 |
|
| 633 |
/* Compute the difference between the constants. If it overflows or |
| 634 |
underflows, this means that we can trivially compare the NAME with |
| 635 |
it and, consequently, the two values with each other. */ |
| 636 |
wide_int diff = wi::to_wide (cst) - wi::to_wide (inv); |
| 637 |
if (wi::cmp (0, wi::to_wide (inv), sgn) |
| 638 |
!= wi::cmp (diff, wi::to_wide (cst), sgn)) |
| 639 |
{ |
| 640 |
const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn); |
| 641 |
return cst1 ? res : -res; |
| 642 |
} |
| 643 |
|
| 644 |
return -2; |
| 645 |
} |
| 646 |
|
| 647 |
/* We cannot say anything more for non-constants. */ |
| 648 |
if (!cst1 || !cst2) |
| 649 |
return -2; |
| 650 |
|
| 651 |
if (!POINTER_TYPE_P (TREE_TYPE (val1))) |
| 652 |
{ |
| 653 |
/* We cannot compare overflowed values. */ |
| 654 |
if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2)) |
| 655 |
return -2; |
| 656 |
|
| 657 |
if (TREE_CODE (val1) == INTEGER_CST |
| 658 |
&& TREE_CODE (val2) == INTEGER_CST) |
| 659 |
return tree_int_cst_compare (val1, val2); |
| 660 |
|
| 661 |
if (poly_int_tree_p (val1) && poly_int_tree_p (val2)) |
| 662 |
{ |
| 663 |
if (known_eq (wi::to_poly_widest (val1), |
| 664 |
wi::to_poly_widest (val2))) |
| 665 |
return 0; |
| 666 |
if (known_lt (wi::to_poly_widest (val1), |
| 667 |
wi::to_poly_widest (val2))) |
| 668 |
return -1; |
| 669 |
if (known_gt (wi::to_poly_widest (val1), |
| 670 |
wi::to_poly_widest (val2))) |
| 671 |
return 1; |
| 672 |
} |
| 673 |
|
| 674 |
return -2; |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST) |
| 679 |
{ |
| 680 |
/* We cannot compare overflowed values. */ |
| 681 |
if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2)) |
| 682 |
return -2; |
| 683 |
|
| 684 |
return tree_int_cst_compare (val1, val2); |
| 685 |
} |
| 686 |
|
| 687 |
/* First see if VAL1 and VAL2 are not the same. */ |
| 688 |
if (operand_equal_p (val1, val2, 0)) |
| 689 |
return 0; |
| 690 |
|
| 691 |
fold_defer_overflow_warnings (); |
| 692 |
|
| 693 |
/* If VAL1 is a lower address than VAL2, return -1. */ |
| 694 |
tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2); |
| 695 |
if (t && integer_onep (t)) |
| 696 |
{ |
| 697 |
fold_undefer_and_ignore_overflow_warnings (); |
| 698 |
return -1; |
| 699 |
} |
| 700 |
|
| 701 |
/* If VAL1 is a higher address than VAL2, return +1. */ |
| 702 |
t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1); |
| 703 |
if (t && integer_onep (t)) |
| 704 |
{ |
| 705 |
fold_undefer_and_ignore_overflow_warnings (); |
| 706 |
return 1; |
| 707 |
} |
| 708 |
|
| 709 |
/* If VAL1 is different than VAL2, return +2. */ |
| 710 |
t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2); |
| 711 |
fold_undefer_and_ignore_overflow_warnings (); |
| 712 |
if (t && integer_onep (t)) |
| 713 |
return 2; |
| 714 |
|
| 715 |
return -2; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
/* Compare values like compare_values_warnv. */ |
| 720 |
|
| 721 |
int |
| 722 |
compare_values (tree val1, tree val2) |
| 723 |
{ |
| 724 |
bool sop; |
| 725 |
return compare_values_warnv (val1, val2, &sop); |
| 726 |
} |
| 727 |
|
| 728 |
/* Helper for overflow_comparison_p |
| 729 |
|
| 730 |
OP0 CODE OP1 is a comparison. Examine the comparison and potentially |
| 731 |
OP1's defining statement to see if it ultimately has the form |
| 732 |
OP0 CODE (OP0 PLUS INTEGER_CST) |
| 733 |
|
| 734 |
If so, return TRUE indicating this is an overflow test and store into |
| 735 |
*NEW_CST an updated constant that can be used in a narrowed range test. |
| 736 |
|
| 737 |
REVERSED indicates if the comparison was originally: |
| 738 |
|
| 739 |
OP1 CODE' OP0. |
| 740 |
|
| 741 |
This affects how we build the updated constant. */ |
| 742 |
|
| 743 |
static bool |
| 744 |
overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1, |
| 745 |
bool reversed, tree *new_cst) |
| 746 |
{ |
| 747 |
/* See if this is a relational operation between two SSA_NAMES with |
| 748 |
unsigned, overflow wrapping values. If so, check it more deeply. */ |
| 749 |
if ((code == LT_EXPR || code == LE_EXPR |
| 750 |
|| code == GE_EXPR || code == GT_EXPR) |
| 751 |
&& TREE_CODE (op0) == SSA_NAME |
| 752 |
&& TREE_CODE (op1) == SSA_NAME |
| 753 |
&& INTEGRAL_TYPE_P (TREE_TYPE (op0)) |
| 754 |
&& TYPE_UNSIGNED (TREE_TYPE (op0)) |
| 755 |
&& TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))) |
| 756 |
{ |
| 757 |
gimple *op1_def = SSA_NAME_DEF_STMT (op1); |
| 758 |
|
| 759 |
/* Now look at the defining statement of OP1 to see if it adds |
| 760 |
or subtracts a nonzero constant from another operand. */ |
| 761 |
if (op1_def |
| 762 |
&& is_gimple_assign (op1_def) |
| 763 |
&& gimple_assign_rhs_code (op1_def) == PLUS_EXPR |
| 764 |
&& TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST |
| 765 |
&& !integer_zerop (gimple_assign_rhs2 (op1_def))) |
| 766 |
{ |
| 767 |
tree target = gimple_assign_rhs1 (op1_def); |
| 768 |
|
| 769 |
/* If we did not find our target SSA_NAME, then this is not |
| 770 |
an overflow test. */ |
| 771 |
if (op0 != target) |
| 772 |
return false; |
| 773 |
|
| 774 |
tree type = TREE_TYPE (op0); |
| 775 |
wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED); |
| 776 |
tree inc = gimple_assign_rhs2 (op1_def); |
| 777 |
if (reversed) |
| 778 |
*new_cst = wide_int_to_tree (type, max + wi::to_wide (inc)); |
| 779 |
else |
| 780 |
*new_cst = wide_int_to_tree (type, max - wi::to_wide (inc)); |
| 781 |
return true; |
| 782 |
} |
| 783 |
} |
| 784 |
return false; |
| 785 |
} |
| 786 |
|
| 787 |
/* OP0 CODE OP1 is a comparison. Examine the comparison and potentially |
| 788 |
OP1's defining statement to see if it ultimately has the form |
| 789 |
OP0 CODE (OP0 PLUS INTEGER_CST) |
| 790 |
|
| 791 |
If so, return TRUE indicating this is an overflow test and store into |
| 792 |
*NEW_CST an updated constant that can be used in a narrowed range test. |
| 793 |
|
| 794 |
These statements are left as-is in the IL to facilitate discovery of |
| 795 |
{ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But |
| 796 |
the alternate range representation is often useful within VRP. */ |
| 797 |
|
| 798 |
bool |
| 799 |
overflow_comparison_p (tree_code code, tree name, tree val, tree *new_cst) |
| 800 |
{ |
| 801 |
if (overflow_comparison_p_1 (code, name, val, false, new_cst)) |
| 802 |
return true; |
| 803 |
return overflow_comparison_p_1 (swap_tree_comparison (code), val, name, |
| 804 |
true, new_cst); |
| 805 |
} |
| 806 |
|
| 807 |
/* Searches the case label vector VEC for the index *IDX of the CASE_LABEL |
| 808 |
that includes the value VAL. The search is restricted to the range |
| 809 |
[START_IDX, n - 1] where n is the size of VEC. |
| 810 |
|
| 811 |
If there is a CASE_LABEL for VAL, its index is placed in IDX and true is |
| 812 |
returned. |
| 813 |
|
| 814 |
If there is no CASE_LABEL for VAL and there is one that is larger than VAL, |
| 815 |
it is placed in IDX and false is returned. |
| 816 |
|
| 817 |
If VAL is larger than any CASE_LABEL, n is placed on IDX and false is |
| 818 |
returned. */ |
| 819 |
|
| 820 |
bool |
| 821 |
find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx) |
| 822 |
{ |
| 823 |
size_t n = gimple_switch_num_labels (stmt); |
| 824 |
size_t low, high; |
| 825 |
|
| 826 |
/* Find case label for minimum of the value range or the next one. |
| 827 |
At each iteration we are searching in [low, high - 1]. */ |
| 828 |
|
| 829 |
for (low = start_idx, high = n; high != low; ) |
| 830 |
{ |
| 831 |
tree t; |
| 832 |
int cmp; |
| 833 |
/* Note that i != high, so we never ask for n. */ |
| 834 |
size_t i = (high + low) / 2; |
| 835 |
t = gimple_switch_label (stmt, i); |
| 836 |
|
| 837 |
/* Cache the result of comparing CASE_LOW and val. */ |
| 838 |
cmp = tree_int_cst_compare (CASE_LOW (t), val); |
| 839 |
|
| 840 |
if (cmp == 0) |
| 841 |
{ |
| 842 |
/* Ranges cannot be empty. */ |
| 843 |
*idx = i; |
| 844 |
return true; |
| 845 |
} |
| 846 |
else if (cmp > 0) |
| 847 |
high = i; |
| 848 |
else |
| 849 |
{ |
| 850 |
low = i + 1; |
| 851 |
if (CASE_HIGH (t) != NULL |
| 852 |
&& tree_int_cst_compare (CASE_HIGH (t), val) >= 0) |
| 853 |
{ |
| 854 |
*idx = i; |
| 855 |
return true; |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
*idx = high; |
| 861 |
return false; |
| 862 |
} |
| 863 |
|
| 864 |
/* Searches the case label vector VEC for the range of CASE_LABELs that is used |
| 865 |
for values between MIN and MAX. The first index is placed in MIN_IDX. The |
| 866 |
last index is placed in MAX_IDX. If the range of CASE_LABELs is empty |
| 867 |
then MAX_IDX < MIN_IDX. |
| 868 |
Returns true if the default label is not needed. */ |
| 869 |
|
| 870 |
bool |
| 871 |
find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx, |
| 872 |
size_t *max_idx) |
| 873 |
{ |
| 874 |
size_t i, j; |
| 875 |
bool min_take_default = !find_case_label_index (stmt, 1, min, &i); |
| 876 |
bool max_take_default = !find_case_label_index (stmt, i, max, &j); |
| 877 |
|
| 878 |
if (i == j |
| 879 |
&& min_take_default |
| 880 |
&& max_take_default) |
| 881 |
{ |
| 882 |
/* Only the default case label reached. |
| 883 |
Return an empty range. */ |
| 884 |
*min_idx = 1; |
| 885 |
*max_idx = 0; |
| 886 |
return false; |
| 887 |
} |
| 888 |
else |
| 889 |
{ |
| 890 |
bool take_default = min_take_default || max_take_default; |
| 891 |
tree low, high; |
| 892 |
size_t k; |
| 893 |
|
| 894 |
if (max_take_default) |
| 895 |
j--; |
| 896 |
|
| 897 |
/* If the case label range is continuous, we do not need |
| 898 |
the default case label. Verify that. */ |
| 899 |
high = CASE_LOW (gimple_switch_label (stmt, i)); |
| 900 |
if (CASE_HIGH (gimple_switch_label (stmt, i))) |
| 901 |
high = CASE_HIGH (gimple_switch_label (stmt, i)); |
| 902 |
for (k = i + 1; k <= j; ++k) |
| 903 |
{ |
| 904 |
low = CASE_LOW (gimple_switch_label (stmt, k)); |
| 905 |
if (!integer_onep (int_const_binop (MINUS_EXPR, low, high))) |
| 906 |
{ |
| 907 |
take_default = true; |
| 908 |
break; |
| 909 |
} |
| 910 |
high = low; |
| 911 |
if (CASE_HIGH (gimple_switch_label (stmt, k))) |
| 912 |
high = CASE_HIGH (gimple_switch_label (stmt, k)); |
| 913 |
} |
| 914 |
|
| 915 |
*min_idx = i; |
| 916 |
*max_idx = j; |
| 917 |
return !take_default; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
/* Given a SWITCH_STMT, return the case label that encompasses the |
| 922 |
known possible values for the switch operand. RANGE_OF_OP is a |
| 923 |
range for the known values of the switch operand. */ |
| 924 |
|
| 925 |
tree |
| 926 |
find_case_label_range (gswitch *switch_stmt, const irange *range_of_op) |
| 927 |
{ |
| 928 |
if (range_of_op->undefined_p () |
| 929 |
|| range_of_op->varying_p ()) |
| 930 |
return NULL_TREE; |
| 931 |
|
| 932 |
size_t i, j; |
| 933 |
tree op = gimple_switch_index (switch_stmt); |
| 934 |
tree type = TREE_TYPE (op); |
| 935 |
tree tmin = wide_int_to_tree (type, range_of_op->lower_bound ()); |
| 936 |
tree tmax = wide_int_to_tree (type, range_of_op->upper_bound ()); |
| 937 |
find_case_label_range (switch_stmt, tmin, tmax, &i, &j); |
| 938 |
if (i == j) |
| 939 |
{ |
| 940 |
/* Look for exactly one label that encompasses the range of |
| 941 |
the operand. */ |
| 942 |
tree label = gimple_switch_label (switch_stmt, i); |
| 943 |
tree case_high |
| 944 |
= CASE_HIGH (label) ? CASE_HIGH (label) : CASE_LOW (label); |
| 945 |
wide_int wlow = wi::to_wide (CASE_LOW (label)); |
| 946 |
wide_int whigh = wi::to_wide (case_high); |
| 947 |
int_range_max label_range (TREE_TYPE (case_high), wlow, whigh); |
| 948 |
if (!types_compatible_p (label_range.type (), range_of_op->type ())) |
| 949 |
range_cast (label_range, range_of_op->type ()); |
| 950 |
label_range.intersect (*range_of_op); |
| 951 |
if (label_range == *range_of_op) |
| 952 |
return label; |
| 953 |
} |
| 954 |
else if (i > j) |
| 955 |
{ |
| 956 |
/* If there are no labels at all, take the default. */ |
| 957 |
return gimple_switch_label (switch_stmt, 0); |
| 958 |
} |
| 959 |
else |
| 960 |
{ |
| 961 |
/* Otherwise, there are various labels that can encompass |
| 962 |
the range of operand. In which case, see if the range of |
| 963 |
the operand is entirely *outside* the bounds of all the |
| 964 |
(non-default) case labels. If so, take the default. */ |
| 965 |
unsigned n = gimple_switch_num_labels (switch_stmt); |
| 966 |
tree min_label = gimple_switch_label (switch_stmt, 1); |
| 967 |
tree max_label = gimple_switch_label (switch_stmt, n - 1); |
| 968 |
tree case_high = CASE_HIGH (max_label); |
| 969 |
if (!case_high) |
| 970 |
case_high = CASE_LOW (max_label); |
| 971 |
int_range_max label_range (TREE_TYPE (CASE_LOW (min_label)), |
| 972 |
wi::to_wide (CASE_LOW (min_label)), |
| 973 |
wi::to_wide (case_high)); |
| 974 |
if (!types_compatible_p (label_range.type (), range_of_op->type ())) |
| 975 |
range_cast (label_range, range_of_op->type ()); |
| 976 |
label_range.intersect (*range_of_op); |
| 977 |
if (label_range.undefined_p ()) |
| 978 |
return gimple_switch_label (switch_stmt, 0); |
| 979 |
} |
| 980 |
return NULL_TREE; |
| 981 |
} |
| 982 |
|
| 983 |
struct case_info |
| 984 |
{ |
| 985 |
tree expr; |
| 986 |
basic_block bb; |
| 987 |
}; |
| 988 |
|
| 989 |
// This is a ranger based folder which continues to use the dominator |
| 990 |
// walk to access the substitute and fold machinery. Ranges are calculated |
| 991 |
// on demand. |
| 992 |
|
| 993 |
class rvrp_folder : public substitute_and_fold_engine |
| 994 |
{ |
| 995 |
public: |
| 996 |
|
| 997 |
rvrp_folder (gimple_ranger *r, bool all) |
| 998 |
: substitute_and_fold_engine (), |
| 999 |
m_unreachable (*r, all), |
| 1000 |
m_simplifier (r, r->non_executable_edge_flag) |
| 1001 |
{ |
| 1002 |
m_ranger = r; |
| 1003 |
m_pta = new pointer_equiv_analyzer (m_ranger); |
| 1004 |
m_last_bb_stmt = NULL; |
| 1005 |
} |
| 1006 |
|
| 1007 |
~rvrp_folder () |
| 1008 |
{ |
| 1009 |
delete m_pta; |
| 1010 |
} |
| 1011 |
|
| 1012 |
tree value_of_expr (tree name, gimple *s = NULL) override |
| 1013 |
{ |
| 1014 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1015 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1016 |
return NULL; |
| 1017 |
tree ret = m_ranger->value_of_expr (name, s); |
| 1018 |
if (!ret && supported_pointer_equiv_p (name)) |
| 1019 |
ret = m_pta->get_equiv (name); |
| 1020 |
return ret; |
| 1021 |
} |
| 1022 |
|
| 1023 |
tree value_on_edge (edge e, tree name) override |
| 1024 |
{ |
| 1025 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1026 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1027 |
return NULL; |
| 1028 |
tree ret = m_ranger->value_on_edge (e, name); |
| 1029 |
if (!ret && supported_pointer_equiv_p (name)) |
| 1030 |
ret = m_pta->get_equiv (name); |
| 1031 |
return ret; |
| 1032 |
} |
| 1033 |
|
| 1034 |
tree value_of_stmt (gimple *s, tree name = NULL) override |
| 1035 |
{ |
| 1036 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1037 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1038 |
return NULL; |
| 1039 |
return m_ranger->value_of_stmt (s, name); |
| 1040 |
} |
| 1041 |
|
| 1042 |
void pre_fold_bb (basic_block bb) override |
| 1043 |
{ |
| 1044 |
m_pta->enter (bb); |
| 1045 |
for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
| 1046 |
gsi_next (&gsi)) |
| 1047 |
m_ranger->register_inferred_ranges (gsi.phi ()); |
| 1048 |
m_last_bb_stmt = last_nondebug_stmt (bb); |
| 1049 |
} |
| 1050 |
|
| 1051 |
void post_fold_bb (basic_block bb) override |
| 1052 |
{ |
| 1053 |
m_pta->leave (bb); |
| 1054 |
} |
| 1055 |
|
| 1056 |
void pre_fold_stmt (gimple *stmt) override |
| 1057 |
{ |
| 1058 |
m_pta->visit_stmt (stmt); |
| 1059 |
// If this is the last stmt and there are inferred ranges, reparse the |
| 1060 |
// block for transitive inferred ranges that occur earlier in the block. |
| 1061 |
if (stmt == m_last_bb_stmt) |
| 1062 |
{ |
| 1063 |
m_ranger->register_transitive_inferred_ranges (gimple_bb (stmt)); |
| 1064 |
// Also check for builtin_unreachable calls. |
| 1065 |
if (cfun->after_inlining && gimple_code (stmt) == GIMPLE_COND) |
| 1066 |
m_unreachable.maybe_register (stmt); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
bool fold_stmt (gimple_stmt_iterator *gsi) override |
| 1071 |
{ |
| 1072 |
bool ret = m_simplifier.simplify (gsi); |
| 1073 |
if (!ret) |
| 1074 |
ret = m_ranger->fold_stmt (gsi, follow_single_use_edges); |
| 1075 |
m_ranger->register_inferred_ranges (gsi_stmt (*gsi)); |
| 1076 |
return ret; |
| 1077 |
} |
| 1078 |
|
| 1079 |
remove_unreachable m_unreachable; |
| 1080 |
private: |
| 1081 |
DISABLE_COPY_AND_ASSIGN (rvrp_folder); |
| 1082 |
gimple_ranger *m_ranger; |
| 1083 |
simplify_using_ranges m_simplifier; |
| 1084 |
pointer_equiv_analyzer *m_pta; |
| 1085 |
gimple *m_last_bb_stmt; |
| 1086 |
}; |
| 1087 |
|
| 1088 |
/* Main entry point for a VRP pass using just ranger. This can be called |
| 1089 |
from anywhere to perform a VRP pass, including from EVRP. */ |
| 1090 |
|
| 1091 |
unsigned int |
| 1092 |
execute_ranger_vrp (struct function *fun, bool final_p) |
| 1093 |
{ |
| 1094 |
loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS); |
| 1095 |
rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa); |
| 1096 |
scev_initialize (); |
| 1097 |
calculate_dominance_info (CDI_DOMINATORS); |
| 1098 |
|
| 1099 |
set_all_edges_as_executable (fun); |
| 1100 |
gimple_ranger *ranger = enable_ranger (fun, false); |
| 1101 |
rvrp_folder folder (ranger, final_p); |
| 1102 |
phi_analysis_initialize (ranger->const_query ()); |
| 1103 |
folder.substitute_and_fold (); |
| 1104 |
// Ensure the cache in SCEV has been cleared before processing |
| 1105 |
// globals to be removed. |
| 1106 |
scev_reset (); |
| 1107 |
// Remove tagged builtin-unreachable and maybe update globals. |
| 1108 |
folder.m_unreachable.remove_and_update_globals (); |
| 1109 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 1110 |
ranger->dump (dump_file); |
| 1111 |
|
| 1112 |
if (value_range::supports_type_p (TREE_TYPE |
| 1113 |
(TREE_TYPE (current_function_decl))) |
| 1114 |
&& flag_ipa_vrp |
| 1115 |
&& !lookup_attribute ("noipa", DECL_ATTRIBUTES (current_function_decl))) |
| 1116 |
{ |
| 1117 |
edge e; |
| 1118 |
edge_iterator ei; |
| 1119 |
bool found = false; |
| 1120 |
value_range return_range (TREE_TYPE (TREE_TYPE (current_function_decl))); |
| 1121 |
FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) |
| 1122 |
if (greturn *ret = dyn_cast <greturn *> (*gsi_last_bb (e->src))) |
| 1123 |
{ |
| 1124 |
tree retval = gimple_return_retval (ret); |
| 1125 |
if (!retval) |
| 1126 |
{ |
| 1127 |
return_range.set_varying (TREE_TYPE (TREE_TYPE (current_function_decl))); |
| 1128 |
found = true; |
| 1129 |
continue; |
| 1130 |
} |
| 1131 |
value_range r (TREE_TYPE (retval)); |
| 1132 |
if (ranger->range_of_expr (r, retval, ret) |
| 1133 |
&& !r.undefined_p () |
| 1134 |
&& !r.varying_p ()) |
| 1135 |
{ |
| 1136 |
if (!found) |
| 1137 |
return_range = r; |
| 1138 |
else |
| 1139 |
return_range.union_ (r); |
| 1140 |
} |
| 1141 |
else |
| 1142 |
return_range.set_varying (TREE_TYPE (retval)); |
| 1143 |
found = true; |
| 1144 |
} |
| 1145 |
if (found && !return_range.varying_p ()) |
| 1146 |
{ |
| 1147 |
ipa_record_return_value_range (return_range); |
| 1148 |
if (POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))) |
| 1149 |
&& return_range.nonzero_p () |
| 1150 |
&& cgraph_node::get (current_function_decl) |
| 1151 |
->add_detected_attribute ("returns_nonnull")) |
| 1152 |
warn_function_returns_nonnull (current_function_decl); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
phi_analysis_finalize (); |
| 1157 |
disable_ranger (fun); |
| 1158 |
scev_finalize (); |
| 1159 |
loop_optimizer_finalize (); |
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
// Implement a Fast VRP folder. Not quite as effective but faster. |
| 1164 |
|
| 1165 |
class fvrp_folder : public substitute_and_fold_engine |
| 1166 |
{ |
| 1167 |
public: |
| 1168 |
fvrp_folder (dom_ranger *dr, bool final_p) : substitute_and_fold_engine (), |
| 1169 |
m_simplifier (dr) |
| 1170 |
{ |
| 1171 |
m_dom_ranger = dr; |
| 1172 |
if (final_p) |
| 1173 |
m_unreachable = new remove_unreachable (*dr, final_p); |
| 1174 |
else |
| 1175 |
m_unreachable = NULL; |
| 1176 |
} |
| 1177 |
|
| 1178 |
~fvrp_folder () { } |
| 1179 |
|
| 1180 |
tree value_of_expr (tree name, gimple *s = NULL) override |
| 1181 |
{ |
| 1182 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1183 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1184 |
return NULL; |
| 1185 |
return m_dom_ranger->value_of_expr (name, s); |
| 1186 |
} |
| 1187 |
|
| 1188 |
tree value_on_edge (edge e, tree name) override |
| 1189 |
{ |
| 1190 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1191 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1192 |
return NULL; |
| 1193 |
return m_dom_ranger->value_on_edge (e, name); |
| 1194 |
} |
| 1195 |
|
| 1196 |
tree value_of_stmt (gimple *s, tree name = NULL) override |
| 1197 |
{ |
| 1198 |
// Shortcircuit subst_and_fold callbacks for abnormal ssa_names. |
| 1199 |
if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1200 |
return NULL; |
| 1201 |
return m_dom_ranger->value_of_stmt (s, name); |
| 1202 |
} |
| 1203 |
|
| 1204 |
void pre_fold_bb (basic_block bb) override |
| 1205 |
{ |
| 1206 |
m_dom_ranger->pre_bb (bb); |
| 1207 |
// Now process the PHIs in advance. |
| 1208 |
gphi_iterator psi = gsi_start_phis (bb); |
| 1209 |
for ( ; !gsi_end_p (psi); gsi_next (&psi)) |
| 1210 |
{ |
| 1211 |
tree name = gimple_range_ssa_p (PHI_RESULT (psi.phi ())); |
| 1212 |
if (name) |
| 1213 |
{ |
| 1214 |
value_range vr(TREE_TYPE (name)); |
| 1215 |
m_dom_ranger->range_of_stmt (vr, psi.phi (), name); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
void post_fold_bb (basic_block bb) override |
| 1221 |
{ |
| 1222 |
m_dom_ranger->post_bb (bb); |
| 1223 |
} |
| 1224 |
|
| 1225 |
void pre_fold_stmt (gimple *s) override |
| 1226 |
{ |
| 1227 |
// Ensure range_of_stmt has been called. |
| 1228 |
tree type = gimple_range_type (s); |
| 1229 |
if (type) |
| 1230 |
{ |
| 1231 |
value_range vr(type); |
| 1232 |
m_dom_ranger->range_of_stmt (vr, s); |
| 1233 |
} |
| 1234 |
if (m_unreachable && gimple_code (s) == GIMPLE_COND) |
| 1235 |
m_unreachable->maybe_register (s); |
| 1236 |
|
| 1237 |
} |
| 1238 |
|
| 1239 |
bool fold_stmt (gimple_stmt_iterator *gsi) override |
| 1240 |
{ |
| 1241 |
bool ret = m_simplifier.simplify (gsi); |
| 1242 |
if (!ret) |
| 1243 |
ret = ::fold_stmt (gsi, follow_single_use_edges); |
| 1244 |
return ret; |
| 1245 |
} |
| 1246 |
|
| 1247 |
remove_unreachable *m_unreachable; |
| 1248 |
private: |
| 1249 |
DISABLE_COPY_AND_ASSIGN (fvrp_folder); |
| 1250 |
simplify_using_ranges m_simplifier; |
| 1251 |
dom_ranger *m_dom_ranger; |
| 1252 |
}; |
| 1253 |
|
| 1254 |
|
| 1255 |
// Main entry point for a FAST VRP pass using a dom ranger. |
| 1256 |
|
| 1257 |
unsigned int |
| 1258 |
execute_fast_vrp (struct function *fun, bool final_p) |
| 1259 |
{ |
| 1260 |
calculate_dominance_info (CDI_DOMINATORS); |
| 1261 |
dom_ranger dr; |
| 1262 |
fvrp_folder folder (&dr, final_p); |
| 1263 |
|
| 1264 |
gcc_checking_assert (!fun->x_range_query); |
| 1265 |
set_all_edges_as_executable (fun); |
| 1266 |
fun->x_range_query = &dr; |
| 1267 |
// Create a relation oracle without transitives. |
| 1268 |
get_range_query (fun)->create_relation_oracle (false); |
| 1269 |
|
| 1270 |
folder.substitute_and_fold (); |
| 1271 |
if (folder.m_unreachable) |
| 1272 |
folder.m_unreachable->remove (); |
| 1273 |
|
| 1274 |
get_range_query (fun)->destroy_relation_oracle (); |
| 1275 |
fun->x_range_query = NULL; |
| 1276 |
return 0; |
| 1277 |
} |
| 1278 |
|
| 1279 |
namespace { |
| 1280 |
|
| 1281 |
const pass_data pass_data_vrp = |
| 1282 |
{ |
| 1283 |
GIMPLE_PASS, /* type */ |
| 1284 |
"vrp", /* name */ |
| 1285 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 1286 |
TV_TREE_VRP, /* tv_id */ |
| 1287 |
PROP_ssa, /* properties_required */ |
| 1288 |
0, /* properties_provided */ |
| 1289 |
0, /* properties_destroyed */ |
| 1290 |
0, /* todo_flags_start */ |
| 1291 |
( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */ |
| 1292 |
}; |
| 1293 |
|
| 1294 |
const pass_data pass_data_early_vrp = |
| 1295 |
{ |
| 1296 |
GIMPLE_PASS, /* type */ |
| 1297 |
"evrp", /* name */ |
| 1298 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 1299 |
TV_TREE_EARLY_VRP, /* tv_id */ |
| 1300 |
PROP_ssa, /* properties_required */ |
| 1301 |
0, /* properties_provided */ |
| 1302 |
0, /* properties_destroyed */ |
| 1303 |
0, /* todo_flags_start */ |
| 1304 |
( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ), |
| 1305 |
}; |
| 1306 |
|
| 1307 |
const pass_data pass_data_fast_vrp = |
| 1308 |
{ |
| 1309 |
GIMPLE_PASS, /* type */ |
| 1310 |
"fvrp", /* name */ |
| 1311 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 1312 |
TV_TREE_FAST_VRP, /* tv_id */ |
| 1313 |
PROP_ssa, /* properties_required */ |
| 1314 |
0, /* properties_provided */ |
| 1315 |
0, /* properties_destroyed */ |
| 1316 |
0, /* todo_flags_start */ |
| 1317 |
( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ), |
| 1318 |
}; |
| 1319 |
|
| 1320 |
|
| 1321 |
class pass_vrp : public gimple_opt_pass |
| 1322 |
{ |
| 1323 |
public: |
| 1324 |
pass_vrp (gcc::context *ctxt, const pass_data &data_) |
| 1325 |
: gimple_opt_pass (data_, ctxt), data (data_), final_p (false) |
| 1326 |
{ } |
| 1327 |
|
| 1328 |
/* opt_pass methods: */ |
| 1329 |
opt_pass * clone () final override |
| 1330 |
{ return new pass_vrp (m_ctxt, data); } |
| 1331 |
void set_pass_param (unsigned int n, bool param) final override |
| 1332 |
{ |
| 1333 |
gcc_assert (n == 0); |
| 1334 |
final_p = param; |
| 1335 |
} |
| 1336 |
bool gate (function *) final override { return flag_tree_vrp != 0; } |
| 1337 |
unsigned int execute (function *fun) final override |
| 1338 |
{ |
| 1339 |
// Check for fast vrp. |
| 1340 |
bool use_fvrp = (&data == &pass_data_fast_vrp); |
| 1341 |
if (!use_fvrp && last_basic_block_for_fn (fun) > param_vrp_block_limit) |
| 1342 |
{ |
| 1343 |
use_fvrp = true; |
| 1344 |
warning (OPT_Wdisabled_optimization, |
| 1345 |
"using fast VRP algorithm; %d basic blocks" |
| 1346 |
" exceeds %<--param=vrp-block-limit=%d%> limit", |
| 1347 |
n_basic_blocks_for_fn (fun), |
| 1348 |
param_vrp_block_limit); |
| 1349 |
} |
| 1350 |
if (use_fvrp) |
| 1351 |
return execute_fast_vrp (fun, final_p); |
| 1352 |
return execute_ranger_vrp (fun, final_p); |
| 1353 |
} |
| 1354 |
|
| 1355 |
private: |
| 1356 |
const pass_data &data; |
| 1357 |
bool final_p; |
| 1358 |
}; // class pass_vrp |
| 1359 |
} // anon namespace |
| 1360 |
|
| 1361 |
gimple_opt_pass * |
| 1362 |
make_pass_vrp (gcc::context *ctxt) |
| 1363 |
{ |
| 1364 |
return new pass_vrp (ctxt, pass_data_vrp); |
| 1365 |
} |
| 1366 |
|
| 1367 |
gimple_opt_pass * |
| 1368 |
make_pass_early_vrp (gcc::context *ctxt) |
| 1369 |
{ |
| 1370 |
return new pass_vrp (ctxt, pass_data_early_vrp); |
| 1371 |
} |
| 1372 |
|
| 1373 |
gimple_opt_pass * |
| 1374 |
make_pass_fast_vrp (gcc::context *ctxt) |
| 1375 |
{ |
| 1376 |
return new pass_vrp (ctxt, pass_data_fast_vrp); |
| 1377 |
} |