1 1.1 mrg /* Functions to determine/estimate number of iterations of a loop. 2 1.1 mrg Copyright (C) 2004-2022 Free Software Foundation, Inc. 3 1.1 mrg 4 1.1 mrg This file is part of GCC. 5 1.1 mrg 6 1.1 mrg GCC is free software; you can redistribute it and/or modify it 7 1.1 mrg under the terms of the GNU General Public License as published by the 8 1.1 mrg Free Software Foundation; either version 3, or (at your option) any 9 1.1 mrg later version. 10 1.1 mrg 11 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT 12 1.1 mrg ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 1.1 mrg for more details. 15 1.1 mrg 16 1.1 mrg You should have received a copy of the GNU General Public License 17 1.1 mrg along with GCC; see the file COPYING3. If not see 18 1.1 mrg <http://www.gnu.org/licenses/>. */ 19 1.1 mrg 20 1.1 mrg #include "config.h" 21 1.1 mrg #include "system.h" 22 1.1 mrg #include "coretypes.h" 23 1.1 mrg #include "backend.h" 24 1.1 mrg #include "rtl.h" 25 1.1 mrg #include "tree.h" 26 1.1 mrg #include "gimple.h" 27 1.1 mrg #include "tree-pass.h" 28 1.1 mrg #include "ssa.h" 29 1.1 mrg #include "gimple-pretty-print.h" 30 1.1 mrg #include "diagnostic-core.h" 31 1.1 mrg #include "stor-layout.h" 32 1.1 mrg #include "fold-const.h" 33 1.1 mrg #include "calls.h" 34 1.1 mrg #include "intl.h" 35 1.1 mrg #include "gimplify.h" 36 1.1 mrg #include "gimple-iterator.h" 37 1.1 mrg #include "tree-cfg.h" 38 1.1 mrg #include "tree-ssa-loop-ivopts.h" 39 1.1 mrg #include "tree-ssa-loop-niter.h" 40 1.1 mrg #include "tree-ssa-loop.h" 41 1.1 mrg #include "cfgloop.h" 42 1.1 mrg #include "tree-chrec.h" 43 1.1 mrg #include "tree-scalar-evolution.h" 44 1.1 mrg #include "tree-dfa.h" 45 1.1 mrg #include "gimple-range.h" 46 1.1 mrg 47 1.1 mrg 48 1.1 mrg /* The maximum number of dominator BBs we search for conditions 49 1.1 mrg of loop header copies we use for simplifying a conditional 50 1.1 mrg expression. */ 51 1.1 mrg #define MAX_DOMINATORS_TO_WALK 8 52 1.1 mrg 53 1.1 mrg /* 54 1.1 mrg 55 1.1 mrg Analysis of number of iterations of an affine exit test. 56 1.1 mrg 57 1.1 mrg */ 58 1.1 mrg 59 1.1 mrg /* Bounds on some value, BELOW <= X <= UP. */ 60 1.1 mrg 61 1.1 mrg struct bounds 62 1.1 mrg { 63 1.1 mrg mpz_t below, up; 64 1.1 mrg }; 65 1.1 mrg 66 1.1 mrg static bool number_of_iterations_popcount (loop_p loop, edge exit, 67 1.1 mrg enum tree_code code, 68 1.1 mrg class tree_niter_desc *niter); 69 1.1 mrg 70 1.1 mrg 71 1.1 mrg /* Splits expression EXPR to a variable part VAR and constant OFFSET. */ 72 1.1 mrg 73 1.1 mrg static void 74 1.1 mrg split_to_var_and_offset (tree expr, tree *var, mpz_t offset) 75 1.1 mrg { 76 1.1 mrg tree type = TREE_TYPE (expr); 77 1.1 mrg tree op0, op1; 78 1.1 mrg bool negate = false; 79 1.1 mrg 80 1.1 mrg *var = expr; 81 1.1 mrg mpz_set_ui (offset, 0); 82 1.1 mrg 83 1.1 mrg switch (TREE_CODE (expr)) 84 1.1 mrg { 85 1.1 mrg case MINUS_EXPR: 86 1.1 mrg negate = true; 87 1.1 mrg /* Fallthru. */ 88 1.1 mrg 89 1.1 mrg case PLUS_EXPR: 90 1.1 mrg case POINTER_PLUS_EXPR: 91 1.1 mrg op0 = TREE_OPERAND (expr, 0); 92 1.1 mrg op1 = TREE_OPERAND (expr, 1); 93 1.1 mrg 94 1.1 mrg if (TREE_CODE (op1) != INTEGER_CST) 95 1.1 mrg break; 96 1.1 mrg 97 1.1 mrg *var = op0; 98 1.1 mrg /* Always sign extend the offset. */ 99 1.1 mrg wi::to_mpz (wi::to_wide (op1), offset, SIGNED); 100 1.1 mrg if (negate) 101 1.1 mrg mpz_neg (offset, offset); 102 1.1 mrg break; 103 1.1 mrg 104 1.1 mrg case INTEGER_CST: 105 1.1 mrg *var = build_int_cst_type (type, 0); 106 1.1 mrg wi::to_mpz (wi::to_wide (expr), offset, TYPE_SIGN (type)); 107 1.1 mrg break; 108 1.1 mrg 109 1.1 mrg default: 110 1.1 mrg break; 111 1.1 mrg } 112 1.1 mrg } 113 1.1 mrg 114 1.1 mrg /* From condition C0 CMP C1 derives information regarding the value range 115 1.1 mrg of VAR, which is of TYPE. Results are stored in to BELOW and UP. */ 116 1.1 mrg 117 1.1 mrg static void 118 1.1 mrg refine_value_range_using_guard (tree type, tree var, 119 1.1 mrg tree c0, enum tree_code cmp, tree c1, 120 1.1 mrg mpz_t below, mpz_t up) 121 1.1 mrg { 122 1.1 mrg tree varc0, varc1, ctype; 123 1.1 mrg mpz_t offc0, offc1; 124 1.1 mrg mpz_t mint, maxt, minc1, maxc1; 125 1.1 mrg bool no_wrap = nowrap_type_p (type); 126 1.1 mrg bool c0_ok, c1_ok; 127 1.1 mrg signop sgn = TYPE_SIGN (type); 128 1.1 mrg 129 1.1 mrg switch (cmp) 130 1.1 mrg { 131 1.1 mrg case LT_EXPR: 132 1.1 mrg case LE_EXPR: 133 1.1 mrg case GT_EXPR: 134 1.1 mrg case GE_EXPR: 135 1.1 mrg STRIP_SIGN_NOPS (c0); 136 1.1 mrg STRIP_SIGN_NOPS (c1); 137 1.1 mrg ctype = TREE_TYPE (c0); 138 1.1 mrg if (!useless_type_conversion_p (ctype, type)) 139 1.1 mrg return; 140 1.1 mrg 141 1.1 mrg break; 142 1.1 mrg 143 1.1 mrg case EQ_EXPR: 144 1.1 mrg /* We could derive quite precise information from EQ_EXPR, however, 145 1.1 mrg such a guard is unlikely to appear, so we do not bother with 146 1.1 mrg handling it. */ 147 1.1 mrg return; 148 1.1 mrg 149 1.1 mrg case NE_EXPR: 150 1.1 mrg /* NE_EXPR comparisons do not contain much of useful information, 151 1.1 mrg except for cases of comparing with bounds. */ 152 1.1 mrg if (TREE_CODE (c1) != INTEGER_CST 153 1.1 mrg || !INTEGRAL_TYPE_P (type)) 154 1.1 mrg return; 155 1.1 mrg 156 1.1 mrg /* Ensure that the condition speaks about an expression in the same 157 1.1 mrg type as X and Y. */ 158 1.1 mrg ctype = TREE_TYPE (c0); 159 1.1 mrg if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type)) 160 1.1 mrg return; 161 1.1 mrg c0 = fold_convert (type, c0); 162 1.1 mrg c1 = fold_convert (type, c1); 163 1.1 mrg 164 1.1 mrg if (operand_equal_p (var, c0, 0)) 165 1.1 mrg { 166 1.1 mrg mpz_t valc1; 167 1.1 mrg 168 1.1 mrg /* Case of comparing VAR with its below/up bounds. */ 169 1.1 mrg mpz_init (valc1); 170 1.1 mrg wi::to_mpz (wi::to_wide (c1), valc1, TYPE_SIGN (type)); 171 1.1 mrg if (mpz_cmp (valc1, below) == 0) 172 1.1 mrg cmp = GT_EXPR; 173 1.1 mrg if (mpz_cmp (valc1, up) == 0) 174 1.1 mrg cmp = LT_EXPR; 175 1.1 mrg 176 1.1 mrg mpz_clear (valc1); 177 1.1 mrg } 178 1.1 mrg else 179 1.1 mrg { 180 1.1 mrg /* Case of comparing with the bounds of the type. */ 181 1.1 mrg wide_int min = wi::min_value (type); 182 1.1 mrg wide_int max = wi::max_value (type); 183 1.1 mrg 184 1.1 mrg if (wi::to_wide (c1) == min) 185 1.1 mrg cmp = GT_EXPR; 186 1.1 mrg if (wi::to_wide (c1) == max) 187 1.1 mrg cmp = LT_EXPR; 188 1.1 mrg } 189 1.1 mrg 190 1.1 mrg /* Quick return if no useful information. */ 191 1.1 mrg if (cmp == NE_EXPR) 192 1.1 mrg return; 193 1.1 mrg 194 1.1 mrg break; 195 1.1 mrg 196 1.1 mrg default: 197 1.1 mrg return; 198 1.1 mrg } 199 1.1 mrg 200 1.1 mrg mpz_init (offc0); 201 1.1 mrg mpz_init (offc1); 202 1.1 mrg split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0); 203 1.1 mrg split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1); 204 1.1 mrg 205 1.1 mrg /* We are only interested in comparisons of expressions based on VAR. */ 206 1.1 mrg if (operand_equal_p (var, varc1, 0)) 207 1.1 mrg { 208 1.1 mrg std::swap (varc0, varc1); 209 1.1 mrg mpz_swap (offc0, offc1); 210 1.1 mrg cmp = swap_tree_comparison (cmp); 211 1.1 mrg } 212 1.1 mrg else if (!operand_equal_p (var, varc0, 0)) 213 1.1 mrg { 214 1.1 mrg mpz_clear (offc0); 215 1.1 mrg mpz_clear (offc1); 216 1.1 mrg return; 217 1.1 mrg } 218 1.1 mrg 219 1.1 mrg mpz_init (mint); 220 1.1 mrg mpz_init (maxt); 221 1.1 mrg get_type_static_bounds (type, mint, maxt); 222 1.1 mrg mpz_init (minc1); 223 1.1 mrg mpz_init (maxc1); 224 1.1 mrg value_range r; 225 1.1 mrg /* Setup range information for varc1. */ 226 1.1 mrg if (integer_zerop (varc1)) 227 1.1 mrg { 228 1.1 mrg wi::to_mpz (0, minc1, TYPE_SIGN (type)); 229 1.1 mrg wi::to_mpz (0, maxc1, TYPE_SIGN (type)); 230 1.1 mrg } 231 1.1 mrg else if (TREE_CODE (varc1) == SSA_NAME 232 1.1 mrg && INTEGRAL_TYPE_P (type) 233 1.1 mrg && get_range_query (cfun)->range_of_expr (r, varc1) 234 1.1 mrg && r.kind () == VR_RANGE) 235 1.1 mrg { 236 1.1 mrg gcc_assert (wi::le_p (r.lower_bound (), r.upper_bound (), sgn)); 237 1.1 mrg wi::to_mpz (r.lower_bound (), minc1, sgn); 238 1.1 mrg wi::to_mpz (r.upper_bound (), maxc1, sgn); 239 1.1 mrg } 240 1.1 mrg else 241 1.1 mrg { 242 1.1 mrg mpz_set (minc1, mint); 243 1.1 mrg mpz_set (maxc1, maxt); 244 1.1 mrg } 245 1.1 mrg 246 1.1 mrg /* Compute valid range information for varc1 + offc1. Note nothing 247 1.1 mrg useful can be derived if it overflows or underflows. Overflow or 248 1.1 mrg underflow could happen when: 249 1.1 mrg 250 1.1 mrg offc1 > 0 && varc1 + offc1 > MAX_VAL (type) 251 1.1 mrg offc1 < 0 && varc1 + offc1 < MIN_VAL (type). */ 252 1.1 mrg mpz_add (minc1, minc1, offc1); 253 1.1 mrg mpz_add (maxc1, maxc1, offc1); 254 1.1 mrg c1_ok = (no_wrap 255 1.1 mrg || mpz_sgn (offc1) == 0 256 1.1 mrg || (mpz_sgn (offc1) < 0 && mpz_cmp (minc1, mint) >= 0) 257 1.1 mrg || (mpz_sgn (offc1) > 0 && mpz_cmp (maxc1, maxt) <= 0)); 258 1.1 mrg if (!c1_ok) 259 1.1 mrg goto end; 260 1.1 mrg 261 1.1 mrg if (mpz_cmp (minc1, mint) < 0) 262 1.1 mrg mpz_set (minc1, mint); 263 1.1 mrg if (mpz_cmp (maxc1, maxt) > 0) 264 1.1 mrg mpz_set (maxc1, maxt); 265 1.1 mrg 266 1.1 mrg if (cmp == LT_EXPR) 267 1.1 mrg { 268 1.1 mrg cmp = LE_EXPR; 269 1.1 mrg mpz_sub_ui (maxc1, maxc1, 1); 270 1.1 mrg } 271 1.1 mrg if (cmp == GT_EXPR) 272 1.1 mrg { 273 1.1 mrg cmp = GE_EXPR; 274 1.1 mrg mpz_add_ui (minc1, minc1, 1); 275 1.1 mrg } 276 1.1 mrg 277 1.1 mrg /* Compute range information for varc0. If there is no overflow, 278 1.1 mrg the condition implied that 279 1.1 mrg 280 1.1 mrg (varc0) cmp (varc1 + offc1 - offc0) 281 1.1 mrg 282 1.1 mrg We can possibly improve the upper bound of varc0 if cmp is LE_EXPR, 283 1.1 mrg or the below bound if cmp is GE_EXPR. 284 1.1 mrg 285 1.1 mrg To prove there is no overflow/underflow, we need to check below 286 1.1 mrg four cases: 287 1.1 mrg 1) cmp == LE_EXPR && offc0 > 0 288 1.1 mrg 289 1.1 mrg (varc0 + offc0) doesn't overflow 290 1.1 mrg && (varc1 + offc1 - offc0) doesn't underflow 291 1.1 mrg 292 1.1 mrg 2) cmp == LE_EXPR && offc0 < 0 293 1.1 mrg 294 1.1 mrg (varc0 + offc0) doesn't underflow 295 1.1 mrg && (varc1 + offc1 - offc0) doesn't overfloe 296 1.1 mrg 297 1.1 mrg In this case, (varc0 + offc0) will never underflow if we can 298 1.1 mrg prove (varc1 + offc1 - offc0) doesn't overflow. 299 1.1 mrg 300 1.1 mrg 3) cmp == GE_EXPR && offc0 < 0 301 1.1 mrg 302 1.1 mrg (varc0 + offc0) doesn't underflow 303 1.1 mrg && (varc1 + offc1 - offc0) doesn't overflow 304 1.1 mrg 305 1.1 mrg 4) cmp == GE_EXPR && offc0 > 0 306 1.1 mrg 307 1.1 mrg (varc0 + offc0) doesn't overflow 308 1.1 mrg && (varc1 + offc1 - offc0) doesn't underflow 309 1.1 mrg 310 1.1 mrg In this case, (varc0 + offc0) will never overflow if we can 311 1.1 mrg prove (varc1 + offc1 - offc0) doesn't underflow. 312 1.1 mrg 313 1.1 mrg Note we only handle case 2 and 4 in below code. */ 314 1.1 mrg 315 1.1 mrg mpz_sub (minc1, minc1, offc0); 316 1.1 mrg mpz_sub (maxc1, maxc1, offc0); 317 1.1 mrg c0_ok = (no_wrap 318 1.1 mrg || mpz_sgn (offc0) == 0 319 1.1 mrg || (cmp == LE_EXPR 320 1.1 mrg && mpz_sgn (offc0) < 0 && mpz_cmp (maxc1, maxt) <= 0) 321 1.1 mrg || (cmp == GE_EXPR 322 1.1 mrg && mpz_sgn (offc0) > 0 && mpz_cmp (minc1, mint) >= 0)); 323 1.1 mrg if (!c0_ok) 324 1.1 mrg goto end; 325 1.1 mrg 326 1.1 mrg if (cmp == LE_EXPR) 327 1.1 mrg { 328 1.1 mrg if (mpz_cmp (up, maxc1) > 0) 329 1.1 mrg mpz_set (up, maxc1); 330 1.1 mrg } 331 1.1 mrg else 332 1.1 mrg { 333 1.1 mrg if (mpz_cmp (below, minc1) < 0) 334 1.1 mrg mpz_set (below, minc1); 335 1.1 mrg } 336 1.1 mrg 337 1.1 mrg end: 338 1.1 mrg mpz_clear (mint); 339 1.1 mrg mpz_clear (maxt); 340 1.1 mrg mpz_clear (minc1); 341 1.1 mrg mpz_clear (maxc1); 342 1.1 mrg mpz_clear (offc0); 343 1.1 mrg mpz_clear (offc1); 344 1.1 mrg } 345 1.1 mrg 346 1.1 mrg /* Stores estimate on the minimum/maximum value of the expression VAR + OFF 347 1.1 mrg in TYPE to MIN and MAX. */ 348 1.1 mrg 349 1.1 mrg static void 350 1.1 mrg determine_value_range (class loop *loop, tree type, tree var, mpz_t off, 351 1.1 mrg mpz_t min, mpz_t max) 352 1.1 mrg { 353 1.1 mrg int cnt = 0; 354 1.1 mrg mpz_t minm, maxm; 355 1.1 mrg basic_block bb; 356 1.1 mrg wide_int minv, maxv; 357 1.1 mrg enum value_range_kind rtype = VR_VARYING; 358 1.1 mrg 359 1.1 mrg /* If the expression is a constant, we know its value exactly. */ 360 1.1 mrg if (integer_zerop (var)) 361 1.1 mrg { 362 1.1 mrg mpz_set (min, off); 363 1.1 mrg mpz_set (max, off); 364 1.1 mrg return; 365 1.1 mrg } 366 1.1 mrg 367 1.1 mrg get_type_static_bounds (type, min, max); 368 1.1 mrg 369 1.1 mrg /* See if we have some range info from VRP. */ 370 1.1 mrg if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type)) 371 1.1 mrg { 372 1.1 mrg edge e = loop_preheader_edge (loop); 373 1.1 mrg signop sgn = TYPE_SIGN (type); 374 1.1 mrg gphi_iterator gsi; 375 1.1 mrg 376 1.1 mrg /* Either for VAR itself... */ 377 1.1 mrg value_range var_range; 378 1.1 mrg get_range_query (cfun)->range_of_expr (var_range, var); 379 1.1 mrg rtype = var_range.kind (); 380 1.1 mrg if (!var_range.undefined_p ()) 381 1.1 mrg { 382 1.1 mrg minv = var_range.lower_bound (); 383 1.1 mrg maxv = var_range.upper_bound (); 384 1.1 mrg } 385 1.1 mrg 386 1.1 mrg /* Or for PHI results in loop->header where VAR is used as 387 1.1 mrg PHI argument from the loop preheader edge. */ 388 1.1 mrg for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi)) 389 1.1 mrg { 390 1.1 mrg gphi *phi = gsi.phi (); 391 1.1 mrg value_range phi_range; 392 1.1 mrg if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var 393 1.1 mrg && get_range_query (cfun)->range_of_expr (phi_range, 394 1.1 mrg gimple_phi_result (phi)) 395 1.1 mrg && phi_range.kind () == VR_RANGE) 396 1.1 mrg { 397 1.1 mrg if (rtype != VR_RANGE) 398 1.1 mrg { 399 1.1 mrg rtype = VR_RANGE; 400 1.1 mrg minv = phi_range.lower_bound (); 401 1.1 mrg maxv = phi_range.upper_bound (); 402 1.1 mrg } 403 1.1 mrg else 404 1.1 mrg { 405 1.1 mrg minv = wi::max (minv, phi_range.lower_bound (), sgn); 406 1.1 mrg maxv = wi::min (maxv, phi_range.upper_bound (), sgn); 407 1.1 mrg /* If the PHI result range are inconsistent with 408 1.1 mrg the VAR range, give up on looking at the PHI 409 1.1 mrg results. This can happen if VR_UNDEFINED is 410 1.1 mrg involved. */ 411 1.1 mrg if (wi::gt_p (minv, maxv, sgn)) 412 1.1 mrg { 413 1.1 mrg value_range vr; 414 1.1 mrg get_range_query (cfun)->range_of_expr (vr, var); 415 1.1 mrg rtype = vr.kind (); 416 1.1 mrg if (!vr.undefined_p ()) 417 1.1 mrg { 418 1.1 mrg minv = vr.lower_bound (); 419 1.1 mrg maxv = vr.upper_bound (); 420 1.1 mrg } 421 1.1 mrg break; 422 1.1 mrg } 423 1.1 mrg } 424 1.1 mrg } 425 1.1 mrg } 426 1.1 mrg mpz_init (minm); 427 1.1 mrg mpz_init (maxm); 428 1.1 mrg if (rtype != VR_RANGE) 429 1.1 mrg { 430 1.1 mrg mpz_set (minm, min); 431 1.1 mrg mpz_set (maxm, max); 432 1.1 mrg } 433 1.1 mrg else 434 1.1 mrg { 435 1.1 mrg gcc_assert (wi::le_p (minv, maxv, sgn)); 436 1.1 mrg wi::to_mpz (minv, minm, sgn); 437 1.1 mrg wi::to_mpz (maxv, maxm, sgn); 438 1.1 mrg } 439 1.1 mrg /* Now walk the dominators of the loop header and use the entry 440 1.1 mrg guards to refine the estimates. */ 441 1.1 mrg for (bb = loop->header; 442 1.1 mrg bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK; 443 1.1 mrg bb = get_immediate_dominator (CDI_DOMINATORS, bb)) 444 1.1 mrg { 445 1.1 mrg edge e; 446 1.1 mrg tree c0, c1; 447 1.1 mrg gimple *cond; 448 1.1 mrg enum tree_code cmp; 449 1.1 mrg 450 1.1 mrg if (!single_pred_p (bb)) 451 1.1 mrg continue; 452 1.1 mrg e = single_pred_edge (bb); 453 1.1 mrg 454 1.1 mrg if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) 455 1.1 mrg continue; 456 1.1 mrg 457 1.1 mrg cond = last_stmt (e->src); 458 1.1 mrg c0 = gimple_cond_lhs (cond); 459 1.1 mrg cmp = gimple_cond_code (cond); 460 1.1 mrg c1 = gimple_cond_rhs (cond); 461 1.1 mrg 462 1.1 mrg if (e->flags & EDGE_FALSE_VALUE) 463 1.1 mrg cmp = invert_tree_comparison (cmp, false); 464 1.1 mrg 465 1.1 mrg refine_value_range_using_guard (type, var, c0, cmp, c1, minm, maxm); 466 1.1 mrg ++cnt; 467 1.1 mrg } 468 1.1 mrg 469 1.1 mrg mpz_add (minm, minm, off); 470 1.1 mrg mpz_add (maxm, maxm, off); 471 1.1 mrg /* If the computation may not wrap or off is zero, then this 472 1.1 mrg is always fine. If off is negative and minv + off isn't 473 1.1 mrg smaller than type's minimum, or off is positive and 474 1.1 mrg maxv + off isn't bigger than type's maximum, use the more 475 1.1 mrg precise range too. */ 476 1.1 mrg if (nowrap_type_p (type) 477 1.1 mrg || mpz_sgn (off) == 0 478 1.1 mrg || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0) 479 1.1 mrg || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0)) 480 1.1 mrg { 481 1.1 mrg mpz_set (min, minm); 482 1.1 mrg mpz_set (max, maxm); 483 1.1 mrg mpz_clear (minm); 484 1.1 mrg mpz_clear (maxm); 485 1.1 mrg return; 486 1.1 mrg } 487 1.1 mrg mpz_clear (minm); 488 1.1 mrg mpz_clear (maxm); 489 1.1 mrg } 490 1.1 mrg 491 1.1 mrg /* If the computation may wrap, we know nothing about the value, except for 492 1.1 mrg the range of the type. */ 493 1.1 mrg if (!nowrap_type_p (type)) 494 1.1 mrg return; 495 1.1 mrg 496 1.1 mrg /* Since the addition of OFF does not wrap, if OFF is positive, then we may 497 1.1 mrg add it to MIN, otherwise to MAX. */ 498 1.1 mrg if (mpz_sgn (off) < 0) 499 1.1 mrg mpz_add (max, max, off); 500 1.1 mrg else 501 1.1 mrg mpz_add (min, min, off); 502 1.1 mrg } 503 1.1 mrg 504 1.1 mrg /* Stores the bounds on the difference of the values of the expressions 505 1.1 mrg (var + X) and (var + Y), computed in TYPE, to BNDS. */ 506 1.1 mrg 507 1.1 mrg static void 508 1.1 mrg bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y, 509 1.1 mrg bounds *bnds) 510 1.1 mrg { 511 1.1 mrg int rel = mpz_cmp (x, y); 512 1.1 mrg bool may_wrap = !nowrap_type_p (type); 513 1.1 mrg mpz_t m; 514 1.1 mrg 515 1.1 mrg /* If X == Y, then the expressions are always equal. 516 1.1 mrg If X > Y, there are the following possibilities: 517 1.1 mrg a) neither of var + X and var + Y overflow or underflow, or both of 518 1.1 mrg them do. Then their difference is X - Y. 519 1.1 mrg b) var + X overflows, and var + Y does not. Then the values of the 520 1.1 mrg expressions are var + X - M and var + Y, where M is the range of 521 1.1 mrg the type, and their difference is X - Y - M. 522 1.1 mrg c) var + Y underflows and var + X does not. Their difference again 523 1.1 mrg is M - X + Y. 524 1.1 mrg Therefore, if the arithmetics in type does not overflow, then the 525 1.1 mrg bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y) 526 1.1 mrg Similarly, if X < Y, the bounds are either (X - Y, X - Y) or 527 1.1 mrg (X - Y, X - Y + M). */ 528 1.1 mrg 529 1.1 mrg if (rel == 0) 530 1.1 mrg { 531 1.1 mrg mpz_set_ui (bnds->below, 0); 532 1.1 mrg mpz_set_ui (bnds->up, 0); 533 1.1 mrg return; 534 1.1 mrg } 535 1.1 mrg 536 1.1 mrg mpz_init (m); 537 1.1 mrg wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), m, UNSIGNED); 538 1.1 mrg mpz_add_ui (m, m, 1); 539 1.1 mrg mpz_sub (bnds->up, x, y); 540 1.1 mrg mpz_set (bnds->below, bnds->up); 541 1.1 mrg 542 1.1 mrg if (may_wrap) 543 1.1 mrg { 544 1.1 mrg if (rel > 0) 545 1.1 mrg mpz_sub (bnds->below, bnds->below, m); 546 1.1 mrg else 547 1.1 mrg mpz_add (bnds->up, bnds->up, m); 548 1.1 mrg } 549 1.1 mrg 550 1.1 mrg mpz_clear (m); 551 1.1 mrg } 552 1.1 mrg 553 1.1 mrg /* From condition C0 CMP C1 derives information regarding the 554 1.1 mrg difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE, 555 1.1 mrg and stores it to BNDS. */ 556 1.1 mrg 557 1.1 mrg static void 558 1.1 mrg refine_bounds_using_guard (tree type, tree varx, mpz_t offx, 559 1.1 mrg tree vary, mpz_t offy, 560 1.1 mrg tree c0, enum tree_code cmp, tree c1, 561 1.1 mrg bounds *bnds) 562 1.1 mrg { 563 1.1 mrg tree varc0, varc1, ctype; 564 1.1 mrg mpz_t offc0, offc1, loffx, loffy, bnd; 565 1.1 mrg bool lbound = false; 566 1.1 mrg bool no_wrap = nowrap_type_p (type); 567 1.1 mrg bool x_ok, y_ok; 568 1.1 mrg 569 1.1 mrg switch (cmp) 570 1.1 mrg { 571 1.1 mrg case LT_EXPR: 572 1.1 mrg case LE_EXPR: 573 1.1 mrg case GT_EXPR: 574 1.1 mrg case GE_EXPR: 575 1.1 mrg STRIP_SIGN_NOPS (c0); 576 1.1 mrg STRIP_SIGN_NOPS (c1); 577 1.1 mrg ctype = TREE_TYPE (c0); 578 1.1 mrg if (!useless_type_conversion_p (ctype, type)) 579 1.1 mrg return; 580 1.1 mrg 581 1.1 mrg break; 582 1.1 mrg 583 1.1 mrg case EQ_EXPR: 584 1.1 mrg /* We could derive quite precise information from EQ_EXPR, however, such 585 1.1 mrg a guard is unlikely to appear, so we do not bother with handling 586 1.1 mrg it. */ 587 1.1 mrg return; 588 1.1 mrg 589 1.1 mrg case NE_EXPR: 590 1.1 mrg /* NE_EXPR comparisons do not contain much of useful information, except for 591 1.1 mrg special case of comparing with the bounds of the type. */ 592 1.1 mrg if (TREE_CODE (c1) != INTEGER_CST 593 1.1 mrg || !INTEGRAL_TYPE_P (type)) 594 1.1 mrg return; 595 1.1 mrg 596 1.1 mrg /* Ensure that the condition speaks about an expression in the same type 597 1.1 mrg as X and Y. */ 598 1.1 mrg ctype = TREE_TYPE (c0); 599 1.1 mrg if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type)) 600 1.1 mrg return; 601 1.1 mrg c0 = fold_convert (type, c0); 602 1.1 mrg c1 = fold_convert (type, c1); 603 1.1 mrg 604 1.1 mrg if (TYPE_MIN_VALUE (type) 605 1.1 mrg && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0)) 606 1.1 mrg { 607 1.1 mrg cmp = GT_EXPR; 608 1.1 mrg break; 609 1.1 mrg } 610 1.1 mrg if (TYPE_MAX_VALUE (type) 611 1.1 mrg && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0)) 612 1.1 mrg { 613 1.1 mrg cmp = LT_EXPR; 614 1.1 mrg break; 615 1.1 mrg } 616 1.1 mrg 617 1.1 mrg return; 618 1.1 mrg default: 619 1.1 mrg return; 620 1.1 mrg } 621 1.1 mrg 622 1.1 mrg mpz_init (offc0); 623 1.1 mrg mpz_init (offc1); 624 1.1 mrg split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0); 625 1.1 mrg split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1); 626 1.1 mrg 627 1.1 mrg /* We are only interested in comparisons of expressions based on VARX and 628 1.1 mrg VARY. TODO -- we might also be able to derive some bounds from 629 1.1 mrg expressions containing just one of the variables. */ 630 1.1 mrg 631 1.1 mrg if (operand_equal_p (varx, varc1, 0)) 632 1.1 mrg { 633 1.1 mrg std::swap (varc0, varc1); 634 1.1 mrg mpz_swap (offc0, offc1); 635 1.1 mrg cmp = swap_tree_comparison (cmp); 636 1.1 mrg } 637 1.1 mrg 638 1.1 mrg if (!operand_equal_p (varx, varc0, 0) 639 1.1 mrg || !operand_equal_p (vary, varc1, 0)) 640 1.1 mrg goto end; 641 1.1 mrg 642 1.1 mrg mpz_init_set (loffx, offx); 643 1.1 mrg mpz_init_set (loffy, offy); 644 1.1 mrg 645 1.1 mrg if (cmp == GT_EXPR || cmp == GE_EXPR) 646 1.1 mrg { 647 1.1 mrg std::swap (varx, vary); 648 1.1 mrg mpz_swap (offc0, offc1); 649 1.1 mrg mpz_swap (loffx, loffy); 650 1.1 mrg cmp = swap_tree_comparison (cmp); 651 1.1 mrg lbound = true; 652 1.1 mrg } 653 1.1 mrg 654 1.1 mrg /* If there is no overflow, the condition implies that 655 1.1 mrg 656 1.1 mrg (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0). 657 1.1 mrg 658 1.1 mrg The overflows and underflows may complicate things a bit; each 659 1.1 mrg overflow decreases the appropriate offset by M, and underflow 660 1.1 mrg increases it by M. The above inequality would not necessarily be 661 1.1 mrg true if 662 1.1 mrg 663 1.1 mrg -- VARX + OFFX underflows and VARX + OFFC0 does not, or 664 1.1 mrg VARX + OFFC0 overflows, but VARX + OFFX does not. 665 1.1 mrg This may only happen if OFFX < OFFC0. 666 1.1 mrg -- VARY + OFFY overflows and VARY + OFFC1 does not, or 667 1.1 mrg VARY + OFFC1 underflows and VARY + OFFY does not. 668 1.1 mrg This may only happen if OFFY > OFFC1. */ 669 1.1 mrg 670 1.1 mrg if (no_wrap) 671 1.1 mrg { 672 1.1 mrg x_ok = true; 673 1.1 mrg y_ok = true; 674 1.1 mrg } 675 1.1 mrg else 676 1.1 mrg { 677 1.1 mrg x_ok = (integer_zerop (varx) 678 1.1 mrg || mpz_cmp (loffx, offc0) >= 0); 679 1.1 mrg y_ok = (integer_zerop (vary) 680 1.1 mrg || mpz_cmp (loffy, offc1) <= 0); 681 1.1 mrg } 682 1.1 mrg 683 1.1 mrg if (x_ok && y_ok) 684 1.1 mrg { 685 1.1 mrg mpz_init (bnd); 686 1.1 mrg mpz_sub (bnd, loffx, loffy); 687 1.1 mrg mpz_add (bnd, bnd, offc1); 688 1.1 mrg mpz_sub (bnd, bnd, offc0); 689 1.1 mrg 690 1.1 mrg if (cmp == LT_EXPR) 691 1.1 mrg mpz_sub_ui (bnd, bnd, 1); 692 1.1 mrg 693 1.1 mrg if (lbound) 694 1.1 mrg { 695 1.1 mrg mpz_neg (bnd, bnd); 696 1.1 mrg if (mpz_cmp (bnds->below, bnd) < 0) 697 1.1 mrg mpz_set (bnds->below, bnd); 698 1.1 mrg } 699 1.1 mrg else 700 1.1 mrg { 701 1.1 mrg if (mpz_cmp (bnd, bnds->up) < 0) 702 1.1 mrg mpz_set (bnds->up, bnd); 703 1.1 mrg } 704 1.1 mrg mpz_clear (bnd); 705 1.1 mrg } 706 1.1 mrg 707 1.1 mrg mpz_clear (loffx); 708 1.1 mrg mpz_clear (loffy); 709 1.1 mrg end: 710 1.1 mrg mpz_clear (offc0); 711 1.1 mrg mpz_clear (offc1); 712 1.1 mrg } 713 1.1 mrg 714 1.1 mrg /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS. 715 1.1 mrg The subtraction is considered to be performed in arbitrary precision, 716 1.1 mrg without overflows. 717 1.1 mrg 718 1.1 mrg We do not attempt to be too clever regarding the value ranges of X and 719 1.1 mrg Y; most of the time, they are just integers or ssa names offsetted by 720 1.1 mrg integer. However, we try to use the information contained in the 721 1.1 mrg comparisons before the loop (usually created by loop header copying). */ 722 1.1 mrg 723 1.1 mrg static void 724 1.1 mrg bound_difference (class loop *loop, tree x, tree y, bounds *bnds) 725 1.1 mrg { 726 1.1 mrg tree type = TREE_TYPE (x); 727 1.1 mrg tree varx, vary; 728 1.1 mrg mpz_t offx, offy; 729 1.1 mrg mpz_t minx, maxx, miny, maxy; 730 1.1 mrg int cnt = 0; 731 1.1 mrg edge e; 732 1.1 mrg basic_block bb; 733 1.1 mrg tree c0, c1; 734 1.1 mrg gimple *cond; 735 1.1 mrg enum tree_code cmp; 736 1.1 mrg 737 1.1 mrg /* Get rid of unnecessary casts, but preserve the value of 738 1.1 mrg the expressions. */ 739 1.1 mrg STRIP_SIGN_NOPS (x); 740 1.1 mrg STRIP_SIGN_NOPS (y); 741 1.1 mrg 742 1.1 mrg mpz_init (bnds->below); 743 1.1 mrg mpz_init (bnds->up); 744 1.1 mrg mpz_init (offx); 745 1.1 mrg mpz_init (offy); 746 1.1 mrg split_to_var_and_offset (x, &varx, offx); 747 1.1 mrg split_to_var_and_offset (y, &vary, offy); 748 1.1 mrg 749 1.1 mrg if (!integer_zerop (varx) 750 1.1 mrg && operand_equal_p (varx, vary, 0)) 751 1.1 mrg { 752 1.1 mrg /* Special case VARX == VARY -- we just need to compare the 753 1.1 mrg offsets. The matters are a bit more complicated in the 754 1.1 mrg case addition of offsets may wrap. */ 755 1.1 mrg bound_difference_of_offsetted_base (type, offx, offy, bnds); 756 1.1 mrg } 757 1.1 mrg else 758 1.1 mrg { 759 1.1 mrg /* Otherwise, use the value ranges to determine the initial 760 1.1 mrg estimates on below and up. */ 761 1.1 mrg mpz_init (minx); 762 1.1 mrg mpz_init (maxx); 763 1.1 mrg mpz_init (miny); 764 1.1 mrg mpz_init (maxy); 765 1.1 mrg determine_value_range (loop, type, varx, offx, minx, maxx); 766 1.1 mrg determine_value_range (loop, type, vary, offy, miny, maxy); 767 1.1 mrg 768 1.1 mrg mpz_sub (bnds->below, minx, maxy); 769 1.1 mrg mpz_sub (bnds->up, maxx, miny); 770 1.1 mrg mpz_clear (minx); 771 1.1 mrg mpz_clear (maxx); 772 1.1 mrg mpz_clear (miny); 773 1.1 mrg mpz_clear (maxy); 774 1.1 mrg } 775 1.1 mrg 776 1.1 mrg /* If both X and Y are constants, we cannot get any more precise. */ 777 1.1 mrg if (integer_zerop (varx) && integer_zerop (vary)) 778 1.1 mrg goto end; 779 1.1 mrg 780 1.1 mrg /* Now walk the dominators of the loop header and use the entry 781 1.1 mrg guards to refine the estimates. */ 782 1.1 mrg for (bb = loop->header; 783 1.1 mrg bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK; 784 1.1 mrg bb = get_immediate_dominator (CDI_DOMINATORS, bb)) 785 1.1 mrg { 786 1.1 mrg if (!single_pred_p (bb)) 787 1.1 mrg continue; 788 1.1 mrg e = single_pred_edge (bb); 789 1.1 mrg 790 1.1 mrg if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) 791 1.1 mrg continue; 792 1.1 mrg 793 1.1 mrg cond = last_stmt (e->src); 794 1.1 mrg c0 = gimple_cond_lhs (cond); 795 1.1 mrg cmp = gimple_cond_code (cond); 796 1.1 mrg c1 = gimple_cond_rhs (cond); 797 1.1 mrg 798 1.1 mrg if (e->flags & EDGE_FALSE_VALUE) 799 1.1 mrg cmp = invert_tree_comparison (cmp, false); 800 1.1 mrg 801 1.1 mrg refine_bounds_using_guard (type, varx, offx, vary, offy, 802 1.1 mrg c0, cmp, c1, bnds); 803 1.1 mrg ++cnt; 804 1.1 mrg } 805 1.1 mrg 806 1.1 mrg end: 807 1.1 mrg mpz_clear (offx); 808 1.1 mrg mpz_clear (offy); 809 1.1 mrg } 810 1.1 mrg 811 1.1 mrg /* Update the bounds in BNDS that restrict the value of X to the bounds 812 1.1 mrg that restrict the value of X + DELTA. X can be obtained as a 813 1.1 mrg difference of two values in TYPE. */ 814 1.1 mrg 815 1.1 mrg static void 816 1.1 mrg bounds_add (bounds *bnds, const widest_int &delta, tree type) 817 1.1 mrg { 818 1.1 mrg mpz_t mdelta, max; 819 1.1 mrg 820 1.1 mrg mpz_init (mdelta); 821 1.1 mrg wi::to_mpz (delta, mdelta, SIGNED); 822 1.1 mrg 823 1.1 mrg mpz_init (max); 824 1.1 mrg wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), max, UNSIGNED); 825 1.1 mrg 826 1.1 mrg mpz_add (bnds->up, bnds->up, mdelta); 827 1.1 mrg mpz_add (bnds->below, bnds->below, mdelta); 828 1.1 mrg 829 1.1 mrg if (mpz_cmp (bnds->up, max) > 0) 830 1.1 mrg mpz_set (bnds->up, max); 831 1.1 mrg 832 1.1 mrg mpz_neg (max, max); 833 1.1 mrg if (mpz_cmp (bnds->below, max) < 0) 834 1.1 mrg mpz_set (bnds->below, max); 835 1.1 mrg 836 1.1 mrg mpz_clear (mdelta); 837 1.1 mrg mpz_clear (max); 838 1.1 mrg } 839 1.1 mrg 840 1.1 mrg /* Update the bounds in BNDS that restrict the value of X to the bounds 841 1.1 mrg that restrict the value of -X. */ 842 1.1 mrg 843 1.1 mrg static void 844 1.1 mrg bounds_negate (bounds *bnds) 845 1.1 mrg { 846 1.1 mrg mpz_t tmp; 847 1.1 mrg 848 1.1 mrg mpz_init_set (tmp, bnds->up); 849 1.1 mrg mpz_neg (bnds->up, bnds->below); 850 1.1 mrg mpz_neg (bnds->below, tmp); 851 1.1 mrg mpz_clear (tmp); 852 1.1 mrg } 853 1.1 mrg 854 1.1 mrg /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */ 855 1.1 mrg 856 1.1 mrg static tree 857 1.1 mrg inverse (tree x, tree mask) 858 1.1 mrg { 859 1.1 mrg tree type = TREE_TYPE (x); 860 1.1 mrg tree rslt; 861 1.1 mrg unsigned ctr = tree_floor_log2 (mask); 862 1.1 mrg 863 1.1 mrg if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT) 864 1.1 mrg { 865 1.1 mrg unsigned HOST_WIDE_INT ix; 866 1.1 mrg unsigned HOST_WIDE_INT imask; 867 1.1 mrg unsigned HOST_WIDE_INT irslt = 1; 868 1.1 mrg 869 1.1 mrg gcc_assert (cst_and_fits_in_hwi (x)); 870 1.1 mrg gcc_assert (cst_and_fits_in_hwi (mask)); 871 1.1 mrg 872 1.1 mrg ix = int_cst_value (x); 873 1.1 mrg imask = int_cst_value (mask); 874 1.1 mrg 875 1.1 mrg for (; ctr; ctr--) 876 1.1 mrg { 877 1.1 mrg irslt *= ix; 878 1.1 mrg ix *= ix; 879 1.1 mrg } 880 1.1 mrg irslt &= imask; 881 1.1 mrg 882 1.1 mrg rslt = build_int_cst_type (type, irslt); 883 1.1 mrg } 884 1.1 mrg else 885 1.1 mrg { 886 1.1 mrg rslt = build_int_cst (type, 1); 887 1.1 mrg for (; ctr; ctr--) 888 1.1 mrg { 889 1.1 mrg rslt = int_const_binop (MULT_EXPR, rslt, x); 890 1.1 mrg x = int_const_binop (MULT_EXPR, x, x); 891 1.1 mrg } 892 1.1 mrg rslt = int_const_binop (BIT_AND_EXPR, rslt, mask); 893 1.1 mrg } 894 1.1 mrg 895 1.1 mrg return rslt; 896 1.1 mrg } 897 1.1 mrg 898 1.1 mrg /* Derives the upper bound BND on the number of executions of loop with exit 899 1.1 mrg condition S * i <> C. If NO_OVERFLOW is true, then the control variable of 900 1.1 mrg the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed 901 1.1 mrg that the loop ends through this exit, i.e., the induction variable ever 902 1.1 mrg reaches the value of C. 903 1.1 mrg 904 1.1 mrg The value C is equal to final - base, where final and base are the final and 905 1.1 mrg initial value of the actual induction variable in the analysed loop. BNDS 906 1.1 mrg bounds the value of this difference when computed in signed type with 907 1.1 mrg unbounded range, while the computation of C is performed in an unsigned 908 1.1 mrg type with the range matching the range of the type of the induction variable. 909 1.1 mrg In particular, BNDS.up contains an upper bound on C in the following cases: 910 1.1 mrg -- if the iv must reach its final value without overflow, i.e., if 911 1.1 mrg NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or 912 1.1 mrg -- if final >= base, which we know to hold when BNDS.below >= 0. */ 913 1.1 mrg 914 1.1 mrg static void 915 1.1 mrg number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s, 916 1.1 mrg bounds *bnds, bool exit_must_be_taken) 917 1.1 mrg { 918 1.1 mrg widest_int max; 919 1.1 mrg mpz_t d; 920 1.1 mrg tree type = TREE_TYPE (c); 921 1.1 mrg bool bnds_u_valid = ((no_overflow && exit_must_be_taken) 922 1.1 mrg || mpz_sgn (bnds->below) >= 0); 923 1.1 mrg 924 1.1 mrg if (integer_onep (s) 925 1.1 mrg || (TREE_CODE (c) == INTEGER_CST 926 1.1 mrg && TREE_CODE (s) == INTEGER_CST 927 1.1 mrg && wi::mod_trunc (wi::to_wide (c), wi::to_wide (s), 928 1.1 mrg TYPE_SIGN (type)) == 0) 929 1.1 mrg || (TYPE_OVERFLOW_UNDEFINED (type) 930 1.1 mrg && multiple_of_p (type, c, s))) 931 1.1 mrg { 932 1.1 mrg /* If C is an exact multiple of S, then its value will be reached before 933 1.1 mrg the induction variable overflows (unless the loop is exited in some 934 1.1 mrg other way before). Note that the actual induction variable in the 935 1.1 mrg loop (which ranges from base to final instead of from 0 to C) may 936 1.1 mrg overflow, in which case BNDS.up will not be giving a correct upper 937 1.1 mrg bound on C; thus, BNDS_U_VALID had to be computed in advance. */ 938 1.1 mrg no_overflow = true; 939 1.1 mrg exit_must_be_taken = true; 940 1.1 mrg } 941 1.1 mrg 942 1.1 mrg /* If the induction variable can overflow, the number of iterations is at 943 1.1 mrg most the period of the control variable (or infinite, but in that case 944 1.1 mrg the whole # of iterations analysis will fail). */ 945 1.1 mrg if (!no_overflow) 946 1.1 mrg { 947 1.1 mrg max = wi::mask <widest_int> (TYPE_PRECISION (type) 948 1.1 mrg - wi::ctz (wi::to_wide (s)), false); 949 1.1 mrg wi::to_mpz (max, bnd, UNSIGNED); 950 1.1 mrg return; 951 1.1 mrg } 952 1.1 mrg 953 1.1 mrg /* Now we know that the induction variable does not overflow, so the loop 954 1.1 mrg iterates at most (range of type / S) times. */ 955 1.1 mrg wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), bnd, UNSIGNED); 956 1.1 mrg 957 1.1 mrg /* If the induction variable is guaranteed to reach the value of C before 958 1.1 mrg overflow, ... */ 959 1.1 mrg if (exit_must_be_taken) 960 1.1 mrg { 961 1.1 mrg /* ... then we can strengthen this to C / S, and possibly we can use 962 1.1 mrg the upper bound on C given by BNDS. */ 963 1.1 mrg if (TREE_CODE (c) == INTEGER_CST) 964 1.1 mrg wi::to_mpz (wi::to_wide (c), bnd, UNSIGNED); 965 1.1 mrg else if (bnds_u_valid) 966 1.1 mrg mpz_set (bnd, bnds->up); 967 1.1 mrg } 968 1.1 mrg 969 1.1 mrg mpz_init (d); 970 1.1 mrg wi::to_mpz (wi::to_wide (s), d, UNSIGNED); 971 1.1 mrg mpz_fdiv_q (bnd, bnd, d); 972 1.1 mrg mpz_clear (d); 973 1.1 mrg } 974 1.1 mrg 975 1.1 mrg /* Determines number of iterations of loop whose ending condition 976 1.1 mrg is IV <> FINAL. TYPE is the type of the iv. The number of 977 1.1 mrg iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if 978 1.1 mrg we know that the exit must be taken eventually, i.e., that the IV 979 1.1 mrg ever reaches the value FINAL (we derived this earlier, and possibly set 980 1.1 mrg NITER->assumptions to make sure this is the case). BNDS contains the 981 1.1 mrg bounds on the difference FINAL - IV->base. */ 982 1.1 mrg 983 1.1 mrg static bool 984 1.1 mrg number_of_iterations_ne (class loop *loop, tree type, affine_iv *iv, 985 1.1 mrg tree final, class tree_niter_desc *niter, 986 1.1 mrg bool exit_must_be_taken, bounds *bnds) 987 1.1 mrg { 988 1.1 mrg tree niter_type = unsigned_type_for (type); 989 1.1 mrg tree s, c, d, bits, assumption, tmp, bound; 990 1.1 mrg mpz_t max; 991 1.1 mrg 992 1.1 mrg niter->control = *iv; 993 1.1 mrg niter->bound = final; 994 1.1 mrg niter->cmp = NE_EXPR; 995 1.1 mrg 996 1.1 mrg /* Rearrange the terms so that we get inequality S * i <> C, with S 997 1.1 mrg positive. Also cast everything to the unsigned type. If IV does 998 1.1 mrg not overflow, BNDS bounds the value of C. Also, this is the 999 1.1 mrg case if the computation |FINAL - IV->base| does not overflow, i.e., 1000 1.1 mrg if BNDS->below in the result is nonnegative. */ 1001 1.1 mrg if (tree_int_cst_sign_bit (iv->step)) 1002 1.1 mrg { 1003 1.1 mrg s = fold_convert (niter_type, 1004 1.1 mrg fold_build1 (NEGATE_EXPR, type, iv->step)); 1005 1.1 mrg c = fold_build2 (MINUS_EXPR, niter_type, 1006 1.1 mrg fold_convert (niter_type, iv->base), 1007 1.1 mrg fold_convert (niter_type, final)); 1008 1.1 mrg bounds_negate (bnds); 1009 1.1 mrg } 1010 1.1 mrg else 1011 1.1 mrg { 1012 1.1 mrg s = fold_convert (niter_type, iv->step); 1013 1.1 mrg c = fold_build2 (MINUS_EXPR, niter_type, 1014 1.1 mrg fold_convert (niter_type, final), 1015 1.1 mrg fold_convert (niter_type, iv->base)); 1016 1.1 mrg } 1017 1.1 mrg 1018 1.1 mrg mpz_init (max); 1019 1.1 mrg number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds, 1020 1.1 mrg exit_must_be_taken); 1021 1.1 mrg niter->max = widest_int::from (wi::from_mpz (niter_type, max, false), 1022 1.1 mrg TYPE_SIGN (niter_type)); 1023 1.1 mrg mpz_clear (max); 1024 1.1 mrg 1025 1.1 mrg /* Compute no-overflow information for the control iv. This can be 1026 1.1 mrg proven when below two conditions are satisfied: 1027 1.1 mrg 1028 1.1 mrg 1) IV evaluates toward FINAL at beginning, i.e: 1029 1.1 mrg base <= FINAL ; step > 0 1030 1.1 mrg base >= FINAL ; step < 0 1031 1.1 mrg 1032 1.1 mrg 2) |FINAL - base| is an exact multiple of step. 1033 1.1 mrg 1034 1.1 mrg Unfortunately, it's hard to prove above conditions after pass loop-ch 1035 1.1 mrg because loop with exit condition (IV != FINAL) usually will be guarded 1036 1.1 mrg by initial-condition (IV.base - IV.step != FINAL). In this case, we 1037 1.1 mrg can alternatively try to prove below conditions: 1038 1.1 mrg 1039 1.1 mrg 1') IV evaluates toward FINAL at beginning, i.e: 1040 1.1 mrg new_base = base - step < FINAL ; step > 0 1041 1.1 mrg && base - step doesn't underflow 1042 1.1 mrg new_base = base - step > FINAL ; step < 0 1043 1.1 mrg && base - step doesn't overflow 1044 1.1 mrg 1045 1.1 mrg Please refer to PR34114 as an example of loop-ch's impact. 1046 1.1 mrg 1047 1.1 mrg Note, for NE_EXPR, base equals to FINAL is a special case, in 1048 1.1 mrg which the loop exits immediately, and the iv does not overflow. 1049 1.1 mrg 1050 1.1 mrg Also note, we prove condition 2) by checking base and final seperately 1051 1.1 mrg along with condition 1) or 1'). Since we ensure the difference 1052 1.1 mrg computation of c does not wrap with cond below and the adjusted s 1053 1.1 mrg will fit a signed type as well as an unsigned we can safely do 1054 1.1 mrg this using the type of the IV if it is not pointer typed. */ 1055 1.1 mrg tree mtype = type; 1056 1.1 mrg if (POINTER_TYPE_P (type)) 1057 1.1 mrg mtype = niter_type; 1058 1.1 mrg if (!niter->control.no_overflow 1059 1.1 mrg && (integer_onep (s) 1060 1.1 mrg || (multiple_of_p (mtype, fold_convert (mtype, iv->base), 1061 1.1 mrg fold_convert (mtype, s), false) 1062 1.1 mrg && multiple_of_p (mtype, fold_convert (mtype, final), 1063 1.1 mrg fold_convert (mtype, s), false)))) 1064 1.1 mrg { 1065 1.1 mrg tree t, cond, relaxed_cond = boolean_false_node; 1066 1.1 mrg 1067 1.1 mrg if (tree_int_cst_sign_bit (iv->step)) 1068 1.1 mrg { 1069 1.1 mrg cond = fold_build2 (GE_EXPR, boolean_type_node, iv->base, final); 1070 1.1 mrg if (TREE_CODE (type) == INTEGER_TYPE) 1071 1.1 mrg { 1072 1.1 mrg /* Only when base - step doesn't overflow. */ 1073 1.1 mrg t = TYPE_MAX_VALUE (type); 1074 1.1 mrg t = fold_build2 (PLUS_EXPR, type, t, iv->step); 1075 1.1 mrg t = fold_build2 (GE_EXPR, boolean_type_node, t, iv->base); 1076 1.1 mrg if (integer_nonzerop (t)) 1077 1.1 mrg { 1078 1.1 mrg t = fold_build2 (MINUS_EXPR, type, iv->base, iv->step); 1079 1.1 mrg relaxed_cond = fold_build2 (GT_EXPR, boolean_type_node, t, 1080 1.1 mrg final); 1081 1.1 mrg } 1082 1.1 mrg } 1083 1.1 mrg } 1084 1.1 mrg else 1085 1.1 mrg { 1086 1.1 mrg cond = fold_build2 (LE_EXPR, boolean_type_node, iv->base, final); 1087 1.1 mrg if (TREE_CODE (type) == INTEGER_TYPE) 1088 1.1 mrg { 1089 1.1 mrg /* Only when base - step doesn't underflow. */ 1090 1.1 mrg t = TYPE_MIN_VALUE (type); 1091 1.1 mrg t = fold_build2 (PLUS_EXPR, type, t, iv->step); 1092 1.1 mrg t = fold_build2 (LE_EXPR, boolean_type_node, t, iv->base); 1093 1.1 mrg if (integer_nonzerop (t)) 1094 1.1 mrg { 1095 1.1 mrg t = fold_build2 (MINUS_EXPR, type, iv->base, iv->step); 1096 1.1 mrg relaxed_cond = fold_build2 (LT_EXPR, boolean_type_node, t, 1097 1.1 mrg final); 1098 1.1 mrg } 1099 1.1 mrg } 1100 1.1 mrg } 1101 1.1 mrg 1102 1.1 mrg t = simplify_using_initial_conditions (loop, cond); 1103 1.1 mrg if (!t || !integer_onep (t)) 1104 1.1 mrg t = simplify_using_initial_conditions (loop, relaxed_cond); 1105 1.1 mrg 1106 1.1 mrg if (t && integer_onep (t)) 1107 1.1 mrg { 1108 1.1 mrg niter->control.no_overflow = true; 1109 1.1 mrg niter->niter = fold_build2 (EXACT_DIV_EXPR, niter_type, c, s); 1110 1.1 mrg return true; 1111 1.1 mrg } 1112 1.1 mrg } 1113 1.1 mrg 1114 1.1 mrg /* Let nsd (step, size of mode) = d. If d does not divide c, the loop 1115 1.1 mrg is infinite. Otherwise, the number of iterations is 1116 1.1 mrg (inverse(s/d) * (c/d)) mod (size of mode/d). */ 1117 1.1 mrg bits = num_ending_zeros (s); 1118 1.1 mrg bound = build_low_bits_mask (niter_type, 1119 1.1 mrg (TYPE_PRECISION (niter_type) 1120 1.1 mrg - tree_to_uhwi (bits))); 1121 1.1 mrg 1122 1.1 mrg d = fold_binary_to_constant (LSHIFT_EXPR, niter_type, 1123 1.1 mrg build_int_cst (niter_type, 1), bits); 1124 1.1 mrg s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits); 1125 1.1 mrg 1126 1.1 mrg if (!exit_must_be_taken) 1127 1.1 mrg { 1128 1.1 mrg /* If we cannot assume that the exit is taken eventually, record the 1129 1.1 mrg assumptions for divisibility of c. */ 1130 1.1 mrg assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d); 1131 1.1 mrg assumption = fold_build2 (EQ_EXPR, boolean_type_node, 1132 1.1 mrg assumption, build_int_cst (niter_type, 0)); 1133 1.1 mrg if (!integer_nonzerop (assumption)) 1134 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1135 1.1 mrg niter->assumptions, assumption); 1136 1.1 mrg } 1137 1.1 mrg 1138 1.1 mrg c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d); 1139 1.1 mrg if (integer_onep (s)) 1140 1.1 mrg { 1141 1.1 mrg niter->niter = c; 1142 1.1 mrg } 1143 1.1 mrg else 1144 1.1 mrg { 1145 1.1 mrg tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound)); 1146 1.1 mrg niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound); 1147 1.1 mrg } 1148 1.1 mrg return true; 1149 1.1 mrg } 1150 1.1 mrg 1151 1.1 mrg /* Checks whether we can determine the final value of the control variable 1152 1.1 mrg of the loop with ending condition IV0 < IV1 (computed in TYPE). 1153 1.1 mrg DELTA is the difference IV1->base - IV0->base, STEP is the absolute value 1154 1.1 mrg of the step. The assumptions necessary to ensure that the computation 1155 1.1 mrg of the final value does not overflow are recorded in NITER. If we 1156 1.1 mrg find the final value, we adjust DELTA and return TRUE. Otherwise 1157 1.1 mrg we return false. BNDS bounds the value of IV1->base - IV0->base, 1158 1.1 mrg and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is 1159 1.1 mrg true if we know that the exit must be taken eventually. */ 1160 1.1 mrg 1161 1.1 mrg static bool 1162 1.1 mrg number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, 1163 1.1 mrg class tree_niter_desc *niter, 1164 1.1 mrg tree *delta, tree step, 1165 1.1 mrg bool exit_must_be_taken, bounds *bnds) 1166 1.1 mrg { 1167 1.1 mrg tree niter_type = TREE_TYPE (step); 1168 1.1 mrg tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step); 1169 1.1 mrg tree tmod; 1170 1.1 mrg mpz_t mmod; 1171 1.1 mrg tree assumption = boolean_true_node, bound, noloop; 1172 1.1 mrg bool ret = false, fv_comp_no_overflow; 1173 1.1 mrg tree type1 = type; 1174 1.1 mrg if (POINTER_TYPE_P (type)) 1175 1.1 mrg type1 = sizetype; 1176 1.1 mrg 1177 1.1 mrg if (TREE_CODE (mod) != INTEGER_CST) 1178 1.1 mrg return false; 1179 1.1 mrg if (integer_nonzerop (mod)) 1180 1.1 mrg mod = fold_build2 (MINUS_EXPR, niter_type, step, mod); 1181 1.1 mrg tmod = fold_convert (type1, mod); 1182 1.1 mrg 1183 1.1 mrg mpz_init (mmod); 1184 1.1 mrg wi::to_mpz (wi::to_wide (mod), mmod, UNSIGNED); 1185 1.1 mrg mpz_neg (mmod, mmod); 1186 1.1 mrg 1187 1.1 mrg /* If the induction variable does not overflow and the exit is taken, 1188 1.1 mrg then the computation of the final value does not overflow. This is 1189 1.1 mrg also obviously the case if the new final value is equal to the 1190 1.1 mrg current one. Finally, we postulate this for pointer type variables, 1191 1.1 mrg as the code cannot rely on the object to that the pointer points being 1192 1.1 mrg placed at the end of the address space (and more pragmatically, 1193 1.1 mrg TYPE_{MIN,MAX}_VALUE is not defined for pointers). */ 1194 1.1 mrg if (integer_zerop (mod) || POINTER_TYPE_P (type)) 1195 1.1 mrg fv_comp_no_overflow = true; 1196 1.1 mrg else if (!exit_must_be_taken) 1197 1.1 mrg fv_comp_no_overflow = false; 1198 1.1 mrg else 1199 1.1 mrg fv_comp_no_overflow = 1200 1.1 mrg (iv0->no_overflow && integer_nonzerop (iv0->step)) 1201 1.1 mrg || (iv1->no_overflow && integer_nonzerop (iv1->step)); 1202 1.1 mrg 1203 1.1 mrg if (integer_nonzerop (iv0->step)) 1204 1.1 mrg { 1205 1.1 mrg /* The final value of the iv is iv1->base + MOD, assuming that this 1206 1.1 mrg computation does not overflow, and that 1207 1.1 mrg iv0->base <= iv1->base + MOD. */ 1208 1.1 mrg if (!fv_comp_no_overflow) 1209 1.1 mrg { 1210 1.1 mrg bound = fold_build2 (MINUS_EXPR, type1, 1211 1.1 mrg TYPE_MAX_VALUE (type1), tmod); 1212 1.1 mrg assumption = fold_build2 (LE_EXPR, boolean_type_node, 1213 1.1 mrg iv1->base, bound); 1214 1.1 mrg if (integer_zerop (assumption)) 1215 1.1 mrg goto end; 1216 1.1 mrg } 1217 1.1 mrg } 1218 1.1 mrg else 1219 1.1 mrg { 1220 1.1 mrg /* The final value of the iv is iv0->base - MOD, assuming that this 1221 1.1 mrg computation does not overflow, and that 1222 1.1 mrg iv0->base - MOD <= iv1->base. */ 1223 1.1 mrg if (!fv_comp_no_overflow) 1224 1.1 mrg { 1225 1.1 mrg bound = fold_build2 (PLUS_EXPR, type1, 1226 1.1 mrg TYPE_MIN_VALUE (type1), tmod); 1227 1.1 mrg assumption = fold_build2 (GE_EXPR, boolean_type_node, 1228 1.1 mrg iv0->base, bound); 1229 1.1 mrg if (integer_zerop (assumption)) 1230 1.1 mrg goto end; 1231 1.1 mrg } 1232 1.1 mrg } 1233 1.1 mrg 1234 1.1 mrg /* IV0 < IV1 does not loop if IV0->base >= IV1->base. */ 1235 1.1 mrg if (mpz_cmp (mmod, bnds->below) < 0) 1236 1.1 mrg noloop = boolean_false_node; 1237 1.1 mrg else 1238 1.1 mrg noloop = fold_build2 (GE_EXPR, boolean_type_node, 1239 1.1 mrg iv0->base, iv1->base); 1240 1.1 mrg 1241 1.1 mrg if (!integer_nonzerop (assumption)) 1242 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1243 1.1 mrg niter->assumptions, 1244 1.1 mrg assumption); 1245 1.1 mrg if (!integer_zerop (noloop)) 1246 1.1 mrg niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, 1247 1.1 mrg niter->may_be_zero, 1248 1.1 mrg noloop); 1249 1.1 mrg bounds_add (bnds, wi::to_widest (mod), type); 1250 1.1 mrg *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod); 1251 1.1 mrg 1252 1.1 mrg ret = true; 1253 1.1 mrg end: 1254 1.1 mrg mpz_clear (mmod); 1255 1.1 mrg return ret; 1256 1.1 mrg } 1257 1.1 mrg 1258 1.1 mrg /* Add assertions to NITER that ensure that the control variable of the loop 1259 1.1 mrg with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1 1260 1.1 mrg are TYPE. Returns false if we can prove that there is an overflow, true 1261 1.1 mrg otherwise. STEP is the absolute value of the step. */ 1262 1.1 mrg 1263 1.1 mrg static bool 1264 1.1 mrg assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1, 1265 1.1 mrg class tree_niter_desc *niter, tree step) 1266 1.1 mrg { 1267 1.1 mrg tree bound, d, assumption, diff; 1268 1.1 mrg tree niter_type = TREE_TYPE (step); 1269 1.1 mrg 1270 1.1 mrg if (integer_nonzerop (iv0->step)) 1271 1.1 mrg { 1272 1.1 mrg /* for (i = iv0->base; i < iv1->base; i += iv0->step) */ 1273 1.1 mrg if (iv0->no_overflow) 1274 1.1 mrg return true; 1275 1.1 mrg 1276 1.1 mrg /* If iv0->base is a constant, we can determine the last value before 1277 1.1 mrg overflow precisely; otherwise we conservatively assume 1278 1.1 mrg MAX - STEP + 1. */ 1279 1.1 mrg 1280 1.1 mrg if (TREE_CODE (iv0->base) == INTEGER_CST) 1281 1.1 mrg { 1282 1.1 mrg d = fold_build2 (MINUS_EXPR, niter_type, 1283 1.1 mrg fold_convert (niter_type, TYPE_MAX_VALUE (type)), 1284 1.1 mrg fold_convert (niter_type, iv0->base)); 1285 1.1 mrg diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step); 1286 1.1 mrg } 1287 1.1 mrg else 1288 1.1 mrg diff = fold_build2 (MINUS_EXPR, niter_type, step, 1289 1.1 mrg build_int_cst (niter_type, 1)); 1290 1.1 mrg bound = fold_build2 (MINUS_EXPR, type, 1291 1.1 mrg TYPE_MAX_VALUE (type), fold_convert (type, diff)); 1292 1.1 mrg assumption = fold_build2 (LE_EXPR, boolean_type_node, 1293 1.1 mrg iv1->base, bound); 1294 1.1 mrg } 1295 1.1 mrg else 1296 1.1 mrg { 1297 1.1 mrg /* for (i = iv1->base; i > iv0->base; i += iv1->step) */ 1298 1.1 mrg if (iv1->no_overflow) 1299 1.1 mrg return true; 1300 1.1 mrg 1301 1.1 mrg if (TREE_CODE (iv1->base) == INTEGER_CST) 1302 1.1 mrg { 1303 1.1 mrg d = fold_build2 (MINUS_EXPR, niter_type, 1304 1.1 mrg fold_convert (niter_type, iv1->base), 1305 1.1 mrg fold_convert (niter_type, TYPE_MIN_VALUE (type))); 1306 1.1 mrg diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step); 1307 1.1 mrg } 1308 1.1 mrg else 1309 1.1 mrg diff = fold_build2 (MINUS_EXPR, niter_type, step, 1310 1.1 mrg build_int_cst (niter_type, 1)); 1311 1.1 mrg bound = fold_build2 (PLUS_EXPR, type, 1312 1.1 mrg TYPE_MIN_VALUE (type), fold_convert (type, diff)); 1313 1.1 mrg assumption = fold_build2 (GE_EXPR, boolean_type_node, 1314 1.1 mrg iv0->base, bound); 1315 1.1 mrg } 1316 1.1 mrg 1317 1.1 mrg if (integer_zerop (assumption)) 1318 1.1 mrg return false; 1319 1.1 mrg if (!integer_nonzerop (assumption)) 1320 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1321 1.1 mrg niter->assumptions, assumption); 1322 1.1 mrg 1323 1.1 mrg iv0->no_overflow = true; 1324 1.1 mrg iv1->no_overflow = true; 1325 1.1 mrg return true; 1326 1.1 mrg } 1327 1.1 mrg 1328 1.1 mrg /* Add an assumption to NITER that a loop whose ending condition 1329 1.1 mrg is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS 1330 1.1 mrg bounds the value of IV1->base - IV0->base. */ 1331 1.1 mrg 1332 1.1 mrg static void 1333 1.1 mrg assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1, 1334 1.1 mrg class tree_niter_desc *niter, bounds *bnds) 1335 1.1 mrg { 1336 1.1 mrg tree assumption = boolean_true_node, bound, diff; 1337 1.1 mrg tree mbz, mbzl, mbzr, type1; 1338 1.1 mrg bool rolls_p, no_overflow_p; 1339 1.1 mrg widest_int dstep; 1340 1.1 mrg mpz_t mstep, max; 1341 1.1 mrg 1342 1.1 mrg /* We are going to compute the number of iterations as 1343 1.1 mrg (iv1->base - iv0->base + step - 1) / step, computed in the unsigned 1344 1.1 mrg variant of TYPE. This formula only works if 1345 1.1 mrg 1346 1.1 mrg -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1 1347 1.1 mrg 1348 1.1 mrg (where MAX is the maximum value of the unsigned variant of TYPE, and 1349 1.1 mrg the computations in this formula are performed in full precision, 1350 1.1 mrg i.e., without overflows). 1351 1.1 mrg 1352 1.1 mrg Usually, for loops with exit condition iv0->base + step * i < iv1->base, 1353 1.1 mrg we have a condition of the form iv0->base - step < iv1->base before the loop, 1354 1.1 mrg and for loops iv0->base < iv1->base - step * i the condition 1355 1.1 mrg iv0->base < iv1->base + step, due to loop header copying, which enable us 1356 1.1 mrg to prove the lower bound. 1357 1.1 mrg 1358 1.1 mrg The upper bound is more complicated. Unless the expressions for initial 1359 1.1 mrg and final value themselves contain enough information, we usually cannot 1360 1.1 mrg derive it from the context. */ 1361 1.1 mrg 1362 1.1 mrg /* First check whether the answer does not follow from the bounds we gathered 1363 1.1 mrg before. */ 1364 1.1 mrg if (integer_nonzerop (iv0->step)) 1365 1.1 mrg dstep = wi::to_widest (iv0->step); 1366 1.1 mrg else 1367 1.1 mrg { 1368 1.1 mrg dstep = wi::sext (wi::to_widest (iv1->step), TYPE_PRECISION (type)); 1369 1.1 mrg dstep = -dstep; 1370 1.1 mrg } 1371 1.1 mrg 1372 1.1 mrg mpz_init (mstep); 1373 1.1 mrg wi::to_mpz (dstep, mstep, UNSIGNED); 1374 1.1 mrg mpz_neg (mstep, mstep); 1375 1.1 mrg mpz_add_ui (mstep, mstep, 1); 1376 1.1 mrg 1377 1.1 mrg rolls_p = mpz_cmp (mstep, bnds->below) <= 0; 1378 1.1 mrg 1379 1.1 mrg mpz_init (max); 1380 1.1 mrg wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), max, UNSIGNED); 1381 1.1 mrg mpz_add (max, max, mstep); 1382 1.1 mrg no_overflow_p = (mpz_cmp (bnds->up, max) <= 0 1383 1.1 mrg /* For pointers, only values lying inside a single object 1384 1.1 mrg can be compared or manipulated by pointer arithmetics. 1385 1.1 mrg Gcc in general does not allow or handle objects larger 1386 1.1 mrg than half of the address space, hence the upper bound 1387 1.1 mrg is satisfied for pointers. */ 1388 1.1 mrg || POINTER_TYPE_P (type)); 1389 1.1 mrg mpz_clear (mstep); 1390 1.1 mrg mpz_clear (max); 1391 1.1 mrg 1392 1.1 mrg if (rolls_p && no_overflow_p) 1393 1.1 mrg return; 1394 1.1 mrg 1395 1.1 mrg type1 = type; 1396 1.1 mrg if (POINTER_TYPE_P (type)) 1397 1.1 mrg type1 = sizetype; 1398 1.1 mrg 1399 1.1 mrg /* Now the hard part; we must formulate the assumption(s) as expressions, and 1400 1.1 mrg we must be careful not to introduce overflow. */ 1401 1.1 mrg 1402 1.1 mrg if (integer_nonzerop (iv0->step)) 1403 1.1 mrg { 1404 1.1 mrg diff = fold_build2 (MINUS_EXPR, type1, 1405 1.1 mrg iv0->step, build_int_cst (type1, 1)); 1406 1.1 mrg 1407 1.1 mrg /* We need to know that iv0->base >= MIN + iv0->step - 1. Since 1408 1.1 mrg 0 address never belongs to any object, we can assume this for 1409 1.1 mrg pointers. */ 1410 1.1 mrg if (!POINTER_TYPE_P (type)) 1411 1.1 mrg { 1412 1.1 mrg bound = fold_build2 (PLUS_EXPR, type1, 1413 1.1 mrg TYPE_MIN_VALUE (type), diff); 1414 1.1 mrg assumption = fold_build2 (GE_EXPR, boolean_type_node, 1415 1.1 mrg iv0->base, bound); 1416 1.1 mrg } 1417 1.1 mrg 1418 1.1 mrg /* And then we can compute iv0->base - diff, and compare it with 1419 1.1 mrg iv1->base. */ 1420 1.1 mrg mbzl = fold_build2 (MINUS_EXPR, type1, 1421 1.1 mrg fold_convert (type1, iv0->base), diff); 1422 1.1 mrg mbzr = fold_convert (type1, iv1->base); 1423 1.1 mrg } 1424 1.1 mrg else 1425 1.1 mrg { 1426 1.1 mrg diff = fold_build2 (PLUS_EXPR, type1, 1427 1.1 mrg iv1->step, build_int_cst (type1, 1)); 1428 1.1 mrg 1429 1.1 mrg if (!POINTER_TYPE_P (type)) 1430 1.1 mrg { 1431 1.1 mrg bound = fold_build2 (PLUS_EXPR, type1, 1432 1.1 mrg TYPE_MAX_VALUE (type), diff); 1433 1.1 mrg assumption = fold_build2 (LE_EXPR, boolean_type_node, 1434 1.1 mrg iv1->base, bound); 1435 1.1 mrg } 1436 1.1 mrg 1437 1.1 mrg mbzl = fold_convert (type1, iv0->base); 1438 1.1 mrg mbzr = fold_build2 (MINUS_EXPR, type1, 1439 1.1 mrg fold_convert (type1, iv1->base), diff); 1440 1.1 mrg } 1441 1.1 mrg 1442 1.1 mrg if (!integer_nonzerop (assumption)) 1443 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1444 1.1 mrg niter->assumptions, assumption); 1445 1.1 mrg if (!rolls_p) 1446 1.1 mrg { 1447 1.1 mrg mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr); 1448 1.1 mrg niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, 1449 1.1 mrg niter->may_be_zero, mbz); 1450 1.1 mrg } 1451 1.1 mrg } 1452 1.1 mrg 1453 1.1 mrg /* Determines number of iterations of loop whose ending condition 1454 1.1 mrg is IV0 < IV1 which likes: {base, -C} < n, or n < {base, C}. 1455 1.1 mrg The number of iterations is stored to NITER. */ 1456 1.1 mrg 1457 1.1 mrg static bool 1458 1.1 mrg number_of_iterations_until_wrap (class loop *loop, tree type, affine_iv *iv0, 1459 1.1 mrg affine_iv *iv1, class tree_niter_desc *niter) 1460 1.1 mrg { 1461 1.1 mrg tree niter_type = unsigned_type_for (type); 1462 1.1 mrg tree step, num, assumptions, may_be_zero, span; 1463 1.1 mrg wide_int high, low, max, min; 1464 1.1 mrg 1465 1.1 mrg may_be_zero = fold_build2 (LE_EXPR, boolean_type_node, iv1->base, iv0->base); 1466 1.1 mrg if (integer_onep (may_be_zero)) 1467 1.1 mrg return false; 1468 1.1 mrg 1469 1.1 mrg int prec = TYPE_PRECISION (type); 1470 1.1 mrg signop sgn = TYPE_SIGN (type); 1471 1.1 mrg min = wi::min_value (prec, sgn); 1472 1.1 mrg max = wi::max_value (prec, sgn); 1473 1.1 mrg 1474 1.1 mrg /* n < {base, C}. */ 1475 1.1 mrg if (integer_zerop (iv0->step) && !tree_int_cst_sign_bit (iv1->step)) 1476 1.1 mrg { 1477 1.1 mrg step = iv1->step; 1478 1.1 mrg /* MIN + C - 1 <= n. */ 1479 1.1 mrg tree last = wide_int_to_tree (type, min + wi::to_wide (step) - 1); 1480 1.1 mrg assumptions = fold_build2 (LE_EXPR, boolean_type_node, last, iv0->base); 1481 1.1 mrg if (integer_zerop (assumptions)) 1482 1.1 mrg return false; 1483 1.1 mrg 1484 1.1 mrg num = fold_build2 (MINUS_EXPR, niter_type, 1485 1.1 mrg wide_int_to_tree (niter_type, max), 1486 1.1 mrg fold_convert (niter_type, iv1->base)); 1487 1.1 mrg 1488 1.1 mrg /* When base has the form iv + 1, if we know iv >= n, then iv + 1 < n 1489 1.1 mrg only when iv + 1 overflows, i.e. when iv == TYPE_VALUE_MAX. */ 1490 1.1 mrg if (sgn == UNSIGNED 1491 1.1 mrg && integer_onep (step) 1492 1.1 mrg && TREE_CODE (iv1->base) == PLUS_EXPR 1493 1.1 mrg && integer_onep (TREE_OPERAND (iv1->base, 1))) 1494 1.1 mrg { 1495 1.1 mrg tree cond = fold_build2 (GE_EXPR, boolean_type_node, 1496 1.1 mrg TREE_OPERAND (iv1->base, 0), iv0->base); 1497 1.1 mrg cond = simplify_using_initial_conditions (loop, cond); 1498 1.1 mrg if (integer_onep (cond)) 1499 1.1 mrg may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node, 1500 1.1 mrg TREE_OPERAND (iv1->base, 0), 1501 1.1 mrg TYPE_MAX_VALUE (type)); 1502 1.1 mrg } 1503 1.1 mrg 1504 1.1 mrg high = max; 1505 1.1 mrg if (TREE_CODE (iv1->base) == INTEGER_CST) 1506 1.1 mrg low = wi::to_wide (iv1->base) - 1; 1507 1.1 mrg else if (TREE_CODE (iv0->base) == INTEGER_CST) 1508 1.1 mrg low = wi::to_wide (iv0->base); 1509 1.1 mrg else 1510 1.1 mrg low = min; 1511 1.1 mrg } 1512 1.1 mrg /* {base, -C} < n. */ 1513 1.1 mrg else if (tree_int_cst_sign_bit (iv0->step) && integer_zerop (iv1->step)) 1514 1.1 mrg { 1515 1.1 mrg step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv0->step), iv0->step); 1516 1.1 mrg /* MAX - C + 1 >= n. */ 1517 1.1 mrg tree last = wide_int_to_tree (type, max - wi::to_wide (step) + 1); 1518 1.1 mrg assumptions = fold_build2 (GE_EXPR, boolean_type_node, last, iv1->base); 1519 1.1 mrg if (integer_zerop (assumptions)) 1520 1.1 mrg return false; 1521 1.1 mrg 1522 1.1 mrg num = fold_build2 (MINUS_EXPR, niter_type, 1523 1.1 mrg fold_convert (niter_type, iv0->base), 1524 1.1 mrg wide_int_to_tree (niter_type, min)); 1525 1.1 mrg low = min; 1526 1.1 mrg if (TREE_CODE (iv0->base) == INTEGER_CST) 1527 1.1 mrg high = wi::to_wide (iv0->base) + 1; 1528 1.1 mrg else if (TREE_CODE (iv1->base) == INTEGER_CST) 1529 1.1 mrg high = wi::to_wide (iv1->base); 1530 1.1 mrg else 1531 1.1 mrg high = max; 1532 1.1 mrg } 1533 1.1 mrg else 1534 1.1 mrg return false; 1535 1.1 mrg 1536 1.1 mrg /* (delta + step - 1) / step */ 1537 1.1 mrg step = fold_convert (niter_type, step); 1538 1.1 mrg num = fold_build2 (PLUS_EXPR, niter_type, num, step); 1539 1.1 mrg niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, num, step); 1540 1.1 mrg 1541 1.1 mrg widest_int delta, s; 1542 1.1 mrg delta = widest_int::from (high, sgn) - widest_int::from (low, sgn); 1543 1.1 mrg s = wi::to_widest (step); 1544 1.1 mrg delta = delta + s - 1; 1545 1.1 mrg niter->max = wi::udiv_floor (delta, s); 1546 1.1 mrg 1547 1.1 mrg niter->may_be_zero = may_be_zero; 1548 1.1 mrg 1549 1.1 mrg if (!integer_nonzerop (assumptions)) 1550 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1551 1.1 mrg niter->assumptions, assumptions); 1552 1.1 mrg 1553 1.1 mrg niter->control.no_overflow = false; 1554 1.1 mrg 1555 1.1 mrg /* Update bound and exit condition as: 1556 1.1 mrg bound = niter * STEP + (IVbase - STEP). 1557 1.1 mrg { IVbase - STEP, +, STEP } != bound 1558 1.1 mrg Here, biasing IVbase by 1 step makes 'bound' be the value before wrap. 1559 1.1 mrg */ 1560 1.1 mrg tree base_type = TREE_TYPE (niter->control.base); 1561 1.1 mrg if (POINTER_TYPE_P (base_type)) 1562 1.1 mrg { 1563 1.1 mrg tree utype = unsigned_type_for (base_type); 1564 1.1 mrg niter->control.base 1565 1.1 mrg = fold_build2 (MINUS_EXPR, utype, 1566 1.1 mrg fold_convert (utype, niter->control.base), 1567 1.1 mrg fold_convert (utype, niter->control.step)); 1568 1.1 mrg niter->control.base = fold_convert (base_type, niter->control.base); 1569 1.1 mrg } 1570 1.1 mrg else 1571 1.1 mrg niter->control.base 1572 1.1 mrg = fold_build2 (MINUS_EXPR, base_type, niter->control.base, 1573 1.1 mrg niter->control.step); 1574 1.1 mrg 1575 1.1 mrg span = fold_build2 (MULT_EXPR, niter_type, niter->niter, 1576 1.1 mrg fold_convert (niter_type, niter->control.step)); 1577 1.1 mrg niter->bound = fold_build2 (PLUS_EXPR, niter_type, span, 1578 1.1 mrg fold_convert (niter_type, niter->control.base)); 1579 1.1 mrg niter->bound = fold_convert (type, niter->bound); 1580 1.1 mrg niter->cmp = NE_EXPR; 1581 1.1 mrg 1582 1.1 mrg return true; 1583 1.1 mrg } 1584 1.1 mrg 1585 1.1 mrg /* Determines number of iterations of loop whose ending condition 1586 1.1 mrg is IV0 < IV1. TYPE is the type of the iv. The number of 1587 1.1 mrg iterations is stored to NITER. BNDS bounds the difference 1588 1.1 mrg IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know 1589 1.1 mrg that the exit must be taken eventually. */ 1590 1.1 mrg 1591 1.1 mrg static bool 1592 1.1 mrg number_of_iterations_lt (class loop *loop, tree type, affine_iv *iv0, 1593 1.1 mrg affine_iv *iv1, class tree_niter_desc *niter, 1594 1.1 mrg bool exit_must_be_taken, bounds *bnds) 1595 1.1 mrg { 1596 1.1 mrg tree niter_type = unsigned_type_for (type); 1597 1.1 mrg tree delta, step, s; 1598 1.1 mrg mpz_t mstep, tmp; 1599 1.1 mrg 1600 1.1 mrg if (integer_nonzerop (iv0->step)) 1601 1.1 mrg { 1602 1.1 mrg niter->control = *iv0; 1603 1.1 mrg niter->cmp = LT_EXPR; 1604 1.1 mrg niter->bound = iv1->base; 1605 1.1 mrg } 1606 1.1 mrg else 1607 1.1 mrg { 1608 1.1 mrg niter->control = *iv1; 1609 1.1 mrg niter->cmp = GT_EXPR; 1610 1.1 mrg niter->bound = iv0->base; 1611 1.1 mrg } 1612 1.1 mrg 1613 1.1 mrg /* {base, -C} < n, or n < {base, C} */ 1614 1.1 mrg if (tree_int_cst_sign_bit (iv0->step) 1615 1.1 mrg || (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step))) 1616 1.1 mrg return number_of_iterations_until_wrap (loop, type, iv0, iv1, niter); 1617 1.1 mrg 1618 1.1 mrg delta = fold_build2 (MINUS_EXPR, niter_type, 1619 1.1 mrg fold_convert (niter_type, iv1->base), 1620 1.1 mrg fold_convert (niter_type, iv0->base)); 1621 1.1 mrg 1622 1.1 mrg /* First handle the special case that the step is +-1. */ 1623 1.1 mrg if ((integer_onep (iv0->step) && integer_zerop (iv1->step)) 1624 1.1 mrg || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step))) 1625 1.1 mrg { 1626 1.1 mrg /* for (i = iv0->base; i < iv1->base; i++) 1627 1.1 mrg 1628 1.1 mrg or 1629 1.1 mrg 1630 1.1 mrg for (i = iv1->base; i > iv0->base; i--). 1631 1.1 mrg 1632 1.1 mrg In both cases # of iterations is iv1->base - iv0->base, assuming that 1633 1.1 mrg iv1->base >= iv0->base. 1634 1.1 mrg 1635 1.1 mrg First try to derive a lower bound on the value of 1636 1.1 mrg iv1->base - iv0->base, computed in full precision. If the difference 1637 1.1 mrg is nonnegative, we are done, otherwise we must record the 1638 1.1 mrg condition. */ 1639 1.1 mrg 1640 1.1 mrg if (mpz_sgn (bnds->below) < 0) 1641 1.1 mrg niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node, 1642 1.1 mrg iv1->base, iv0->base); 1643 1.1 mrg niter->niter = delta; 1644 1.1 mrg niter->max = widest_int::from (wi::from_mpz (niter_type, bnds->up, false), 1645 1.1 mrg TYPE_SIGN (niter_type)); 1646 1.1 mrg niter->control.no_overflow = true; 1647 1.1 mrg return true; 1648 1.1 mrg } 1649 1.1 mrg 1650 1.1 mrg if (integer_nonzerop (iv0->step)) 1651 1.1 mrg step = fold_convert (niter_type, iv0->step); 1652 1.1 mrg else 1653 1.1 mrg step = fold_convert (niter_type, 1654 1.1 mrg fold_build1 (NEGATE_EXPR, type, iv1->step)); 1655 1.1 mrg 1656 1.1 mrg /* If we can determine the final value of the control iv exactly, we can 1657 1.1 mrg transform the condition to != comparison. In particular, this will be 1658 1.1 mrg the case if DELTA is constant. */ 1659 1.1 mrg if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step, 1660 1.1 mrg exit_must_be_taken, bnds)) 1661 1.1 mrg { 1662 1.1 mrg affine_iv zps; 1663 1.1 mrg 1664 1.1 mrg zps.base = build_int_cst (niter_type, 0); 1665 1.1 mrg zps.step = step; 1666 1.1 mrg /* number_of_iterations_lt_to_ne will add assumptions that ensure that 1667 1.1 mrg zps does not overflow. */ 1668 1.1 mrg zps.no_overflow = true; 1669 1.1 mrg 1670 1.1 mrg return number_of_iterations_ne (loop, type, &zps, 1671 1.1 mrg delta, niter, true, bnds); 1672 1.1 mrg } 1673 1.1 mrg 1674 1.1 mrg /* Make sure that the control iv does not overflow. */ 1675 1.1 mrg if (!assert_no_overflow_lt (type, iv0, iv1, niter, step)) 1676 1.1 mrg return false; 1677 1.1 mrg 1678 1.1 mrg /* We determine the number of iterations as (delta + step - 1) / step. For 1679 1.1 mrg this to work, we must know that iv1->base >= iv0->base - step + 1, 1680 1.1 mrg otherwise the loop does not roll. */ 1681 1.1 mrg assert_loop_rolls_lt (type, iv0, iv1, niter, bnds); 1682 1.1 mrg 1683 1.1 mrg s = fold_build2 (MINUS_EXPR, niter_type, 1684 1.1 mrg step, build_int_cst (niter_type, 1)); 1685 1.1 mrg delta = fold_build2 (PLUS_EXPR, niter_type, delta, s); 1686 1.1 mrg niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step); 1687 1.1 mrg 1688 1.1 mrg mpz_init (mstep); 1689 1.1 mrg mpz_init (tmp); 1690 1.1 mrg wi::to_mpz (wi::to_wide (step), mstep, UNSIGNED); 1691 1.1 mrg mpz_add (tmp, bnds->up, mstep); 1692 1.1 mrg mpz_sub_ui (tmp, tmp, 1); 1693 1.1 mrg mpz_fdiv_q (tmp, tmp, mstep); 1694 1.1 mrg niter->max = widest_int::from (wi::from_mpz (niter_type, tmp, false), 1695 1.1 mrg TYPE_SIGN (niter_type)); 1696 1.1 mrg mpz_clear (mstep); 1697 1.1 mrg mpz_clear (tmp); 1698 1.1 mrg 1699 1.1 mrg return true; 1700 1.1 mrg } 1701 1.1 mrg 1702 1.1 mrg /* Determines number of iterations of loop whose ending condition 1703 1.1 mrg is IV0 <= IV1. TYPE is the type of the iv. The number of 1704 1.1 mrg iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if 1705 1.1 mrg we know that this condition must eventually become false (we derived this 1706 1.1 mrg earlier, and possibly set NITER->assumptions to make sure this 1707 1.1 mrg is the case). BNDS bounds the difference IV1->base - IV0->base. */ 1708 1.1 mrg 1709 1.1 mrg static bool 1710 1.1 mrg number_of_iterations_le (class loop *loop, tree type, affine_iv *iv0, 1711 1.1 mrg affine_iv *iv1, class tree_niter_desc *niter, 1712 1.1 mrg bool exit_must_be_taken, bounds *bnds) 1713 1.1 mrg { 1714 1.1 mrg tree assumption; 1715 1.1 mrg tree type1 = type; 1716 1.1 mrg if (POINTER_TYPE_P (type)) 1717 1.1 mrg type1 = sizetype; 1718 1.1 mrg 1719 1.1 mrg /* Say that IV0 is the control variable. Then IV0 <= IV1 iff 1720 1.1 mrg IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest 1721 1.1 mrg value of the type. This we must know anyway, since if it is 1722 1.1 mrg equal to this value, the loop rolls forever. We do not check 1723 1.1 mrg this condition for pointer type ivs, as the code cannot rely on 1724 1.1 mrg the object to that the pointer points being placed at the end of 1725 1.1 mrg the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is 1726 1.1 mrg not defined for pointers). */ 1727 1.1 mrg 1728 1.1 mrg if (!exit_must_be_taken && !POINTER_TYPE_P (type)) 1729 1.1 mrg { 1730 1.1 mrg if (integer_nonzerop (iv0->step)) 1731 1.1 mrg assumption = fold_build2 (NE_EXPR, boolean_type_node, 1732 1.1 mrg iv1->base, TYPE_MAX_VALUE (type)); 1733 1.1 mrg else 1734 1.1 mrg assumption = fold_build2 (NE_EXPR, boolean_type_node, 1735 1.1 mrg iv0->base, TYPE_MIN_VALUE (type)); 1736 1.1 mrg 1737 1.1 mrg if (integer_zerop (assumption)) 1738 1.1 mrg return false; 1739 1.1 mrg if (!integer_nonzerop (assumption)) 1740 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 1741 1.1 mrg niter->assumptions, assumption); 1742 1.1 mrg } 1743 1.1 mrg 1744 1.1 mrg if (integer_nonzerop (iv0->step)) 1745 1.1 mrg { 1746 1.1 mrg if (POINTER_TYPE_P (type)) 1747 1.1 mrg iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1); 1748 1.1 mrg else 1749 1.1 mrg iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base, 1750 1.1 mrg build_int_cst (type1, 1)); 1751 1.1 mrg } 1752 1.1 mrg else if (POINTER_TYPE_P (type)) 1753 1.1 mrg iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1); 1754 1.1 mrg else 1755 1.1 mrg iv0->base = fold_build2 (MINUS_EXPR, type1, 1756 1.1 mrg iv0->base, build_int_cst (type1, 1)); 1757 1.1 mrg 1758 1.1 mrg bounds_add (bnds, 1, type1); 1759 1.1 mrg 1760 1.1 mrg return number_of_iterations_lt (loop, type, iv0, iv1, niter, exit_must_be_taken, 1761 1.1 mrg bnds); 1762 1.1 mrg } 1763 1.1 mrg 1764 1.1 mrg /* Dumps description of affine induction variable IV to FILE. */ 1765 1.1 mrg 1766 1.1 mrg static void 1767 1.1 mrg dump_affine_iv (FILE *file, affine_iv *iv) 1768 1.1 mrg { 1769 1.1 mrg if (!integer_zerop (iv->step)) 1770 1.1 mrg fprintf (file, "["); 1771 1.1 mrg 1772 1.1 mrg print_generic_expr (dump_file, iv->base, TDF_SLIM); 1773 1.1 mrg 1774 1.1 mrg if (!integer_zerop (iv->step)) 1775 1.1 mrg { 1776 1.1 mrg fprintf (file, ", + , "); 1777 1.1 mrg print_generic_expr (dump_file, iv->step, TDF_SLIM); 1778 1.1 mrg fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : ""); 1779 1.1 mrg } 1780 1.1 mrg } 1781 1.1 mrg 1782 1.1 mrg /* Determine the number of iterations according to condition (for staying 1783 1.1 mrg inside loop) which compares two induction variables using comparison 1784 1.1 mrg operator CODE. The induction variable on left side of the comparison 1785 1.1 mrg is IV0, the right-hand side is IV1. Both induction variables must have 1786 1.1 mrg type TYPE, which must be an integer or pointer type. The steps of the 1787 1.1 mrg ivs must be constants (or NULL_TREE, which is interpreted as constant zero). 1788 1.1 mrg 1789 1.1 mrg LOOP is the loop whose number of iterations we are determining. 1790 1.1 mrg 1791 1.1 mrg ONLY_EXIT is true if we are sure this is the only way the loop could be 1792 1.1 mrg exited (including possibly non-returning function calls, exceptions, etc.) 1793 1.1 mrg -- in this case we can use the information whether the control induction 1794 1.1 mrg variables can overflow or not in a more efficient way. 1795 1.1 mrg 1796 1.1 mrg if EVERY_ITERATION is true, we know the test is executed on every iteration. 1797 1.1 mrg 1798 1.1 mrg The results (number of iterations and assumptions as described in 1799 1.1 mrg comments at class tree_niter_desc in tree-ssa-loop.h) are stored to NITER. 1800 1.1 mrg Returns false if it fails to determine number of iterations, true if it 1801 1.1 mrg was determined (possibly with some assumptions). */ 1802 1.1 mrg 1803 1.1 mrg static bool 1804 1.1 mrg number_of_iterations_cond (class loop *loop, 1805 1.1 mrg tree type, affine_iv *iv0, enum tree_code code, 1806 1.1 mrg affine_iv *iv1, class tree_niter_desc *niter, 1807 1.1 mrg bool only_exit, bool every_iteration) 1808 1.1 mrg { 1809 1.1 mrg bool exit_must_be_taken = false, ret; 1810 1.1 mrg bounds bnds; 1811 1.1 mrg 1812 1.1 mrg /* If the test is not executed every iteration, wrapping may make the test 1813 1.1 mrg to pass again. 1814 1.1 mrg TODO: the overflow case can be still used as unreliable estimate of upper 1815 1.1 mrg bound. But we have no API to pass it down to number of iterations code 1816 1.1 mrg and, at present, it will not use it anyway. */ 1817 1.1 mrg if (!every_iteration 1818 1.1 mrg && (!iv0->no_overflow || !iv1->no_overflow 1819 1.1 mrg || code == NE_EXPR || code == EQ_EXPR)) 1820 1.1 mrg return false; 1821 1.1 mrg 1822 1.1 mrg /* The meaning of these assumptions is this: 1823 1.1 mrg if !assumptions 1824 1.1 mrg then the rest of information does not have to be valid 1825 1.1 mrg if may_be_zero then the loop does not roll, even if 1826 1.1 mrg niter != 0. */ 1827 1.1 mrg niter->assumptions = boolean_true_node; 1828 1.1 mrg niter->may_be_zero = boolean_false_node; 1829 1.1 mrg niter->niter = NULL_TREE; 1830 1.1 mrg niter->max = 0; 1831 1.1 mrg niter->bound = NULL_TREE; 1832 1.1 mrg niter->cmp = ERROR_MARK; 1833 1.1 mrg 1834 1.1 mrg /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that 1835 1.1 mrg the control variable is on lhs. */ 1836 1.1 mrg if (code == GE_EXPR || code == GT_EXPR 1837 1.1 mrg || (code == NE_EXPR && integer_zerop (iv0->step))) 1838 1.1 mrg { 1839 1.1 mrg std::swap (iv0, iv1); 1840 1.1 mrg code = swap_tree_comparison (code); 1841 1.1 mrg } 1842 1.1 mrg 1843 1.1 mrg if (POINTER_TYPE_P (type)) 1844 1.1 mrg { 1845 1.1 mrg /* Comparison of pointers is undefined unless both iv0 and iv1 point 1846 1.1 mrg to the same object. If they do, the control variable cannot wrap 1847 1.1 mrg (as wrap around the bounds of memory will never return a pointer 1848 1.1 mrg that would be guaranteed to point to the same object, even if we 1849 1.1 mrg avoid undefined behavior by casting to size_t and back). */ 1850 1.1 mrg iv0->no_overflow = true; 1851 1.1 mrg iv1->no_overflow = true; 1852 1.1 mrg } 1853 1.1 mrg 1854 1.1 mrg /* If the control induction variable does not overflow and the only exit 1855 1.1 mrg from the loop is the one that we analyze, we know it must be taken 1856 1.1 mrg eventually. */ 1857 1.1 mrg if (only_exit) 1858 1.1 mrg { 1859 1.1 mrg if (!integer_zerop (iv0->step) && iv0->no_overflow) 1860 1.1 mrg exit_must_be_taken = true; 1861 1.1 mrg else if (!integer_zerop (iv1->step) && iv1->no_overflow) 1862 1.1 mrg exit_must_be_taken = true; 1863 1.1 mrg } 1864 1.1 mrg 1865 1.1 mrg /* We can handle cases which neither of the sides of the comparison is 1866 1.1 mrg invariant: 1867 1.1 mrg 1868 1.1 mrg {iv0.base, iv0.step} cmp_code {iv1.base, iv1.step} 1869 1.1 mrg as if: 1870 1.1 mrg {iv0.base, iv0.step - iv1.step} cmp_code {iv1.base, 0} 1871 1.1 mrg 1872 1.1 mrg provided that either below condition is satisfied: 1873 1.1 mrg 1874 1.1 mrg a) the test is NE_EXPR; 1875 1.1 mrg b) iv0 and iv1 do not overflow and iv0.step - iv1.step is of 1876 1.1 mrg the same sign and of less or equal magnitude than iv0.step 1877 1.1 mrg 1878 1.1 mrg This rarely occurs in practice, but it is simple enough to manage. */ 1879 1.1 mrg if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step)) 1880 1.1 mrg { 1881 1.1 mrg tree step_type = POINTER_TYPE_P (type) ? sizetype : type; 1882 1.1 mrg tree step = fold_binary_to_constant (MINUS_EXPR, step_type, 1883 1.1 mrg iv0->step, iv1->step); 1884 1.1 mrg 1885 1.1 mrg /* For code other than NE_EXPR we have to ensure moving the evolution 1886 1.1 mrg of IV1 to that of IV0 does not introduce overflow. */ 1887 1.1 mrg if (TREE_CODE (step) != INTEGER_CST 1888 1.1 mrg || !iv0->no_overflow || !iv1->no_overflow) 1889 1.1 mrg { 1890 1.1 mrg if (code != NE_EXPR) 1891 1.1 mrg return false; 1892 1.1 mrg iv0->no_overflow = false; 1893 1.1 mrg } 1894 1.1 mrg /* If the new step of IV0 has changed sign or is of greater 1895 1.1 mrg magnitude then we do not know whether IV0 does overflow 1896 1.1 mrg and thus the transform is not valid for code other than NE_EXPR. */ 1897 1.1 mrg else if (tree_int_cst_sign_bit (step) != tree_int_cst_sign_bit (iv0->step) 1898 1.1 mrg || wi::gtu_p (wi::abs (wi::to_widest (step)), 1899 1.1 mrg wi::abs (wi::to_widest (iv0->step)))) 1900 1.1 mrg { 1901 1.1 mrg if (POINTER_TYPE_P (type) && code != NE_EXPR) 1902 1.1 mrg /* For relational pointer compares we have further guarantees 1903 1.1 mrg that the pointers always point to the same object (or one 1904 1.1 mrg after it) and that objects do not cross the zero page. So 1905 1.1 mrg not only is the transform always valid for relational 1906 1.1 mrg pointer compares, we also know the resulting IV does not 1907 1.1 mrg overflow. */ 1908 1.1 mrg ; 1909 1.1 mrg else if (code != NE_EXPR) 1910 1.1 mrg return false; 1911 1.1 mrg else 1912 1.1 mrg iv0->no_overflow = false; 1913 1.1 mrg } 1914 1.1 mrg 1915 1.1 mrg iv0->step = step; 1916 1.1 mrg iv1->step = build_int_cst (step_type, 0); 1917 1.1 mrg iv1->no_overflow = true; 1918 1.1 mrg } 1919 1.1 mrg 1920 1.1 mrg /* If the result of the comparison is a constant, the loop is weird. More 1921 1.1 mrg precise handling would be possible, but the situation is not common enough 1922 1.1 mrg to waste time on it. */ 1923 1.1 mrg if (integer_zerop (iv0->step) && integer_zerop (iv1->step)) 1924 1.1 mrg return false; 1925 1.1 mrg 1926 1.1 mrg /* If the loop exits immediately, there is nothing to do. */ 1927 1.1 mrg tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base); 1928 1.1 mrg if (tem && integer_zerop (tem)) 1929 1.1 mrg { 1930 1.1 mrg if (!every_iteration) 1931 1.1 mrg return false; 1932 1.1 mrg niter->niter = build_int_cst (unsigned_type_for (type), 0); 1933 1.1 mrg niter->max = 0; 1934 1.1 mrg return true; 1935 1.1 mrg } 1936 1.1 mrg 1937 1.1 mrg /* OK, now we know we have a senseful loop. Handle several cases, depending 1938 1.1 mrg on what comparison operator is used. */ 1939 1.1 mrg bound_difference (loop, iv1->base, iv0->base, &bnds); 1940 1.1 mrg 1941 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 1942 1.1 mrg { 1943 1.1 mrg fprintf (dump_file, 1944 1.1 mrg "Analyzing # of iterations of loop %d\n", loop->num); 1945 1.1 mrg 1946 1.1 mrg fprintf (dump_file, " exit condition "); 1947 1.1 mrg dump_affine_iv (dump_file, iv0); 1948 1.1 mrg fprintf (dump_file, " %s ", 1949 1.1 mrg code == NE_EXPR ? "!=" 1950 1.1 mrg : code == LT_EXPR ? "<" 1951 1.1 mrg : "<="); 1952 1.1 mrg dump_affine_iv (dump_file, iv1); 1953 1.1 mrg fprintf (dump_file, "\n"); 1954 1.1 mrg 1955 1.1 mrg fprintf (dump_file, " bounds on difference of bases: "); 1956 1.1 mrg mpz_out_str (dump_file, 10, bnds.below); 1957 1.1 mrg fprintf (dump_file, " ... "); 1958 1.1 mrg mpz_out_str (dump_file, 10, bnds.up); 1959 1.1 mrg fprintf (dump_file, "\n"); 1960 1.1 mrg } 1961 1.1 mrg 1962 1.1 mrg switch (code) 1963 1.1 mrg { 1964 1.1 mrg case NE_EXPR: 1965 1.1 mrg gcc_assert (integer_zerop (iv1->step)); 1966 1.1 mrg ret = number_of_iterations_ne (loop, type, iv0, iv1->base, niter, 1967 1.1 mrg exit_must_be_taken, &bnds); 1968 1.1 mrg break; 1969 1.1 mrg 1970 1.1 mrg case LT_EXPR: 1971 1.1 mrg ret = number_of_iterations_lt (loop, type, iv0, iv1, niter, 1972 1.1 mrg exit_must_be_taken, &bnds); 1973 1.1 mrg break; 1974 1.1 mrg 1975 1.1 mrg case LE_EXPR: 1976 1.1 mrg ret = number_of_iterations_le (loop, type, iv0, iv1, niter, 1977 1.1 mrg exit_must_be_taken, &bnds); 1978 1.1 mrg break; 1979 1.1 mrg 1980 1.1 mrg default: 1981 1.1 mrg gcc_unreachable (); 1982 1.1 mrg } 1983 1.1 mrg 1984 1.1 mrg mpz_clear (bnds.up); 1985 1.1 mrg mpz_clear (bnds.below); 1986 1.1 mrg 1987 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 1988 1.1 mrg { 1989 1.1 mrg if (ret) 1990 1.1 mrg { 1991 1.1 mrg fprintf (dump_file, " result:\n"); 1992 1.1 mrg if (!integer_nonzerop (niter->assumptions)) 1993 1.1 mrg { 1994 1.1 mrg fprintf (dump_file, " under assumptions "); 1995 1.1 mrg print_generic_expr (dump_file, niter->assumptions, TDF_SLIM); 1996 1.1 mrg fprintf (dump_file, "\n"); 1997 1.1 mrg } 1998 1.1 mrg 1999 1.1 mrg if (!integer_zerop (niter->may_be_zero)) 2000 1.1 mrg { 2001 1.1 mrg fprintf (dump_file, " zero if "); 2002 1.1 mrg print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM); 2003 1.1 mrg fprintf (dump_file, "\n"); 2004 1.1 mrg } 2005 1.1 mrg 2006 1.1 mrg fprintf (dump_file, " # of iterations "); 2007 1.1 mrg print_generic_expr (dump_file, niter->niter, TDF_SLIM); 2008 1.1 mrg fprintf (dump_file, ", bounded by "); 2009 1.1 mrg print_decu (niter->max, dump_file); 2010 1.1 mrg fprintf (dump_file, "\n"); 2011 1.1 mrg } 2012 1.1 mrg else 2013 1.1 mrg fprintf (dump_file, " failed\n\n"); 2014 1.1 mrg } 2015 1.1 mrg return ret; 2016 1.1 mrg } 2017 1.1 mrg 2018 1.1 mrg /* Substitute NEW_TREE for OLD in EXPR and fold the result. 2019 1.1 mrg If VALUEIZE is non-NULL then OLD and NEW_TREE are ignored and instead 2020 1.1 mrg all SSA names are replaced with the result of calling the VALUEIZE 2021 1.1 mrg function with the SSA name as argument. */ 2022 1.1 mrg 2023 1.1 mrg tree 2024 1.1 mrg simplify_replace_tree (tree expr, tree old, tree new_tree, 2025 1.1 mrg tree (*valueize) (tree, void*), void *context, 2026 1.1 mrg bool do_fold) 2027 1.1 mrg { 2028 1.1 mrg unsigned i, n; 2029 1.1 mrg tree ret = NULL_TREE, e, se; 2030 1.1 mrg 2031 1.1 mrg if (!expr) 2032 1.1 mrg return NULL_TREE; 2033 1.1 mrg 2034 1.1 mrg /* Do not bother to replace constants. */ 2035 1.1 mrg if (CONSTANT_CLASS_P (expr)) 2036 1.1 mrg return expr; 2037 1.1 mrg 2038 1.1 mrg if (valueize) 2039 1.1 mrg { 2040 1.1 mrg if (TREE_CODE (expr) == SSA_NAME) 2041 1.1 mrg { 2042 1.1 mrg new_tree = valueize (expr, context); 2043 1.1 mrg if (new_tree != expr) 2044 1.1 mrg return new_tree; 2045 1.1 mrg } 2046 1.1 mrg } 2047 1.1 mrg else if (expr == old 2048 1.1 mrg || operand_equal_p (expr, old, 0)) 2049 1.1 mrg return unshare_expr (new_tree); 2050 1.1 mrg 2051 1.1 mrg if (!EXPR_P (expr)) 2052 1.1 mrg return expr; 2053 1.1 mrg 2054 1.1 mrg n = TREE_OPERAND_LENGTH (expr); 2055 1.1 mrg for (i = 0; i < n; i++) 2056 1.1 mrg { 2057 1.1 mrg e = TREE_OPERAND (expr, i); 2058 1.1 mrg se = simplify_replace_tree (e, old, new_tree, valueize, context, do_fold); 2059 1.1 mrg if (e == se) 2060 1.1 mrg continue; 2061 1.1 mrg 2062 1.1 mrg if (!ret) 2063 1.1 mrg ret = copy_node (expr); 2064 1.1 mrg 2065 1.1 mrg TREE_OPERAND (ret, i) = se; 2066 1.1 mrg } 2067 1.1 mrg 2068 1.1 mrg return (ret ? (do_fold ? fold (ret) : ret) : expr); 2069 1.1 mrg } 2070 1.1 mrg 2071 1.1 mrg /* Expand definitions of ssa names in EXPR as long as they are simple 2072 1.1 mrg enough, and return the new expression. If STOP is specified, stop 2073 1.1 mrg expanding if EXPR equals to it. */ 2074 1.1 mrg 2075 1.1 mrg static tree 2076 1.1 mrg expand_simple_operations (tree expr, tree stop, hash_map<tree, tree> &cache) 2077 1.1 mrg { 2078 1.1 mrg unsigned i, n; 2079 1.1 mrg tree ret = NULL_TREE, e, ee, e1; 2080 1.1 mrg enum tree_code code; 2081 1.1 mrg gimple *stmt; 2082 1.1 mrg 2083 1.1 mrg if (expr == NULL_TREE) 2084 1.1 mrg return expr; 2085 1.1 mrg 2086 1.1 mrg if (is_gimple_min_invariant (expr)) 2087 1.1 mrg return expr; 2088 1.1 mrg 2089 1.1 mrg code = TREE_CODE (expr); 2090 1.1 mrg if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))) 2091 1.1 mrg { 2092 1.1 mrg n = TREE_OPERAND_LENGTH (expr); 2093 1.1 mrg for (i = 0; i < n; i++) 2094 1.1 mrg { 2095 1.1 mrg e = TREE_OPERAND (expr, i); 2096 1.1 mrg /* SCEV analysis feeds us with a proper expression 2097 1.1 mrg graph matching the SSA graph. Avoid turning it 2098 1.1 mrg into a tree here, thus handle tree sharing 2099 1.1 mrg properly. 2100 1.1 mrg ??? The SSA walk below still turns the SSA graph 2101 1.1 mrg into a tree but until we find a testcase do not 2102 1.1 mrg introduce additional tree sharing here. */ 2103 1.1 mrg bool existed_p; 2104 1.1 mrg tree &cee = cache.get_or_insert (e, &existed_p); 2105 1.1 mrg if (existed_p) 2106 1.1 mrg ee = cee; 2107 1.1 mrg else 2108 1.1 mrg { 2109 1.1 mrg cee = e; 2110 1.1 mrg ee = expand_simple_operations (e, stop, cache); 2111 1.1 mrg if (ee != e) 2112 1.1 mrg *cache.get (e) = ee; 2113 1.1 mrg } 2114 1.1 mrg if (e == ee) 2115 1.1 mrg continue; 2116 1.1 mrg 2117 1.1 mrg if (!ret) 2118 1.1 mrg ret = copy_node (expr); 2119 1.1 mrg 2120 1.1 mrg TREE_OPERAND (ret, i) = ee; 2121 1.1 mrg } 2122 1.1 mrg 2123 1.1 mrg if (!ret) 2124 1.1 mrg return expr; 2125 1.1 mrg 2126 1.1 mrg fold_defer_overflow_warnings (); 2127 1.1 mrg ret = fold (ret); 2128 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 2129 1.1 mrg return ret; 2130 1.1 mrg } 2131 1.1 mrg 2132 1.1 mrg /* Stop if it's not ssa name or the one we don't want to expand. */ 2133 1.1 mrg if (TREE_CODE (expr) != SSA_NAME || expr == stop) 2134 1.1 mrg return expr; 2135 1.1 mrg 2136 1.1 mrg stmt = SSA_NAME_DEF_STMT (expr); 2137 1.1 mrg if (gimple_code (stmt) == GIMPLE_PHI) 2138 1.1 mrg { 2139 1.1 mrg basic_block src, dest; 2140 1.1 mrg 2141 1.1 mrg if (gimple_phi_num_args (stmt) != 1) 2142 1.1 mrg return expr; 2143 1.1 mrg e = PHI_ARG_DEF (stmt, 0); 2144 1.1 mrg 2145 1.1 mrg /* Avoid propagating through loop exit phi nodes, which 2146 1.1 mrg could break loop-closed SSA form restrictions. */ 2147 1.1 mrg dest = gimple_bb (stmt); 2148 1.1 mrg src = single_pred (dest); 2149 1.1 mrg if (TREE_CODE (e) == SSA_NAME 2150 1.1 mrg && src->loop_father != dest->loop_father) 2151 1.1 mrg return expr; 2152 1.1 mrg 2153 1.1 mrg return expand_simple_operations (e, stop, cache); 2154 1.1 mrg } 2155 1.1 mrg if (gimple_code (stmt) != GIMPLE_ASSIGN) 2156 1.1 mrg return expr; 2157 1.1 mrg 2158 1.1 mrg /* Avoid expanding to expressions that contain SSA names that need 2159 1.1 mrg to take part in abnormal coalescing. */ 2160 1.1 mrg ssa_op_iter iter; 2161 1.1 mrg FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE) 2162 1.1 mrg if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e)) 2163 1.1 mrg return expr; 2164 1.1 mrg 2165 1.1 mrg e = gimple_assign_rhs1 (stmt); 2166 1.1 mrg code = gimple_assign_rhs_code (stmt); 2167 1.1 mrg if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) 2168 1.1 mrg { 2169 1.1 mrg if (is_gimple_min_invariant (e)) 2170 1.1 mrg return e; 2171 1.1 mrg 2172 1.1 mrg if (code == SSA_NAME) 2173 1.1 mrg return expand_simple_operations (e, stop, cache); 2174 1.1 mrg else if (code == ADDR_EXPR) 2175 1.1 mrg { 2176 1.1 mrg poly_int64 offset; 2177 1.1 mrg tree base = get_addr_base_and_unit_offset (TREE_OPERAND (e, 0), 2178 1.1 mrg &offset); 2179 1.1 mrg if (base 2180 1.1 mrg && TREE_CODE (base) == MEM_REF) 2181 1.1 mrg { 2182 1.1 mrg ee = expand_simple_operations (TREE_OPERAND (base, 0), stop, 2183 1.1 mrg cache); 2184 1.1 mrg return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (expr), ee, 2185 1.1 mrg wide_int_to_tree (sizetype, 2186 1.1 mrg mem_ref_offset (base) 2187 1.1 mrg + offset)); 2188 1.1 mrg } 2189 1.1 mrg } 2190 1.1 mrg 2191 1.1 mrg return expr; 2192 1.1 mrg } 2193 1.1 mrg 2194 1.1 mrg switch (code) 2195 1.1 mrg { 2196 1.1 mrg CASE_CONVERT: 2197 1.1 mrg /* Casts are simple. */ 2198 1.1 mrg ee = expand_simple_operations (e, stop, cache); 2199 1.1 mrg return fold_build1 (code, TREE_TYPE (expr), ee); 2200 1.1 mrg 2201 1.1 mrg case PLUS_EXPR: 2202 1.1 mrg case MINUS_EXPR: 2203 1.1 mrg if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (expr)) 2204 1.1 mrg && TYPE_OVERFLOW_TRAPS (TREE_TYPE (expr))) 2205 1.1 mrg return expr; 2206 1.1 mrg /* Fallthru. */ 2207 1.1 mrg case POINTER_PLUS_EXPR: 2208 1.1 mrg /* And increments and decrements by a constant are simple. */ 2209 1.1 mrg e1 = gimple_assign_rhs2 (stmt); 2210 1.1 mrg if (!is_gimple_min_invariant (e1)) 2211 1.1 mrg return expr; 2212 1.1 mrg 2213 1.1 mrg ee = expand_simple_operations (e, stop, cache); 2214 1.1 mrg return fold_build2 (code, TREE_TYPE (expr), ee, e1); 2215 1.1 mrg 2216 1.1 mrg default: 2217 1.1 mrg return expr; 2218 1.1 mrg } 2219 1.1 mrg } 2220 1.1 mrg 2221 1.1 mrg tree 2222 1.1 mrg expand_simple_operations (tree expr, tree stop) 2223 1.1 mrg { 2224 1.1 mrg hash_map<tree, tree> cache; 2225 1.1 mrg return expand_simple_operations (expr, stop, cache); 2226 1.1 mrg } 2227 1.1 mrg 2228 1.1 mrg /* Tries to simplify EXPR using the condition COND. Returns the simplified 2229 1.1 mrg expression (or EXPR unchanged, if no simplification was possible). */ 2230 1.1 mrg 2231 1.1 mrg static tree 2232 1.1 mrg tree_simplify_using_condition_1 (tree cond, tree expr) 2233 1.1 mrg { 2234 1.1 mrg bool changed; 2235 1.1 mrg tree e, e0, e1, e2, notcond; 2236 1.1 mrg enum tree_code code = TREE_CODE (expr); 2237 1.1 mrg 2238 1.1 mrg if (code == INTEGER_CST) 2239 1.1 mrg return expr; 2240 1.1 mrg 2241 1.1 mrg if (code == TRUTH_OR_EXPR 2242 1.1 mrg || code == TRUTH_AND_EXPR 2243 1.1 mrg || code == COND_EXPR) 2244 1.1 mrg { 2245 1.1 mrg changed = false; 2246 1.1 mrg 2247 1.1 mrg e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0)); 2248 1.1 mrg if (TREE_OPERAND (expr, 0) != e0) 2249 1.1 mrg changed = true; 2250 1.1 mrg 2251 1.1 mrg e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1)); 2252 1.1 mrg if (TREE_OPERAND (expr, 1) != e1) 2253 1.1 mrg changed = true; 2254 1.1 mrg 2255 1.1 mrg if (code == COND_EXPR) 2256 1.1 mrg { 2257 1.1 mrg e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2)); 2258 1.1 mrg if (TREE_OPERAND (expr, 2) != e2) 2259 1.1 mrg changed = true; 2260 1.1 mrg } 2261 1.1 mrg else 2262 1.1 mrg e2 = NULL_TREE; 2263 1.1 mrg 2264 1.1 mrg if (changed) 2265 1.1 mrg { 2266 1.1 mrg if (code == COND_EXPR) 2267 1.1 mrg expr = fold_build3 (code, boolean_type_node, e0, e1, e2); 2268 1.1 mrg else 2269 1.1 mrg expr = fold_build2 (code, boolean_type_node, e0, e1); 2270 1.1 mrg } 2271 1.1 mrg 2272 1.1 mrg return expr; 2273 1.1 mrg } 2274 1.1 mrg 2275 1.1 mrg /* In case COND is equality, we may be able to simplify EXPR by copy/constant 2276 1.1 mrg propagation, and vice versa. Fold does not handle this, since it is 2277 1.1 mrg considered too expensive. */ 2278 1.1 mrg if (TREE_CODE (cond) == EQ_EXPR) 2279 1.1 mrg { 2280 1.1 mrg e0 = TREE_OPERAND (cond, 0); 2281 1.1 mrg e1 = TREE_OPERAND (cond, 1); 2282 1.1 mrg 2283 1.1 mrg /* We know that e0 == e1. Check whether we cannot simplify expr 2284 1.1 mrg using this fact. */ 2285 1.1 mrg e = simplify_replace_tree (expr, e0, e1); 2286 1.1 mrg if (integer_zerop (e) || integer_nonzerop (e)) 2287 1.1 mrg return e; 2288 1.1 mrg 2289 1.1 mrg e = simplify_replace_tree (expr, e1, e0); 2290 1.1 mrg if (integer_zerop (e) || integer_nonzerop (e)) 2291 1.1 mrg return e; 2292 1.1 mrg } 2293 1.1 mrg if (TREE_CODE (expr) == EQ_EXPR) 2294 1.1 mrg { 2295 1.1 mrg e0 = TREE_OPERAND (expr, 0); 2296 1.1 mrg e1 = TREE_OPERAND (expr, 1); 2297 1.1 mrg 2298 1.1 mrg /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */ 2299 1.1 mrg e = simplify_replace_tree (cond, e0, e1); 2300 1.1 mrg if (integer_zerop (e)) 2301 1.1 mrg return e; 2302 1.1 mrg e = simplify_replace_tree (cond, e1, e0); 2303 1.1 mrg if (integer_zerop (e)) 2304 1.1 mrg return e; 2305 1.1 mrg } 2306 1.1 mrg if (TREE_CODE (expr) == NE_EXPR) 2307 1.1 mrg { 2308 1.1 mrg e0 = TREE_OPERAND (expr, 0); 2309 1.1 mrg e1 = TREE_OPERAND (expr, 1); 2310 1.1 mrg 2311 1.1 mrg /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */ 2312 1.1 mrg e = simplify_replace_tree (cond, e0, e1); 2313 1.1 mrg if (integer_zerop (e)) 2314 1.1 mrg return boolean_true_node; 2315 1.1 mrg e = simplify_replace_tree (cond, e1, e0); 2316 1.1 mrg if (integer_zerop (e)) 2317 1.1 mrg return boolean_true_node; 2318 1.1 mrg } 2319 1.1 mrg 2320 1.1 mrg /* Check whether COND ==> EXPR. */ 2321 1.1 mrg notcond = invert_truthvalue (cond); 2322 1.1 mrg e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, expr); 2323 1.1 mrg if (e && integer_nonzerop (e)) 2324 1.1 mrg return e; 2325 1.1 mrg 2326 1.1 mrg /* Check whether COND ==> not EXPR. */ 2327 1.1 mrg e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, expr); 2328 1.1 mrg if (e && integer_zerop (e)) 2329 1.1 mrg return e; 2330 1.1 mrg 2331 1.1 mrg return expr; 2332 1.1 mrg } 2333 1.1 mrg 2334 1.1 mrg /* Tries to simplify EXPR using the condition COND. Returns the simplified 2335 1.1 mrg expression (or EXPR unchanged, if no simplification was possible). 2336 1.1 mrg Wrapper around tree_simplify_using_condition_1 that ensures that chains 2337 1.1 mrg of simple operations in definitions of ssa names in COND are expanded, 2338 1.1 mrg so that things like casts or incrementing the value of the bound before 2339 1.1 mrg the loop do not cause us to fail. */ 2340 1.1 mrg 2341 1.1 mrg static tree 2342 1.1 mrg tree_simplify_using_condition (tree cond, tree expr) 2343 1.1 mrg { 2344 1.1 mrg cond = expand_simple_operations (cond); 2345 1.1 mrg 2346 1.1 mrg return tree_simplify_using_condition_1 (cond, expr); 2347 1.1 mrg } 2348 1.1 mrg 2349 1.1 mrg /* Tries to simplify EXPR using the conditions on entry to LOOP. 2350 1.1 mrg Returns the simplified expression (or EXPR unchanged, if no 2351 1.1 mrg simplification was possible). */ 2352 1.1 mrg 2353 1.1 mrg tree 2354 1.1 mrg simplify_using_initial_conditions (class loop *loop, tree expr) 2355 1.1 mrg { 2356 1.1 mrg edge e; 2357 1.1 mrg basic_block bb; 2358 1.1 mrg gimple *stmt; 2359 1.1 mrg tree cond, expanded, backup; 2360 1.1 mrg int cnt = 0; 2361 1.1 mrg 2362 1.1 mrg if (TREE_CODE (expr) == INTEGER_CST) 2363 1.1 mrg return expr; 2364 1.1 mrg 2365 1.1 mrg backup = expanded = expand_simple_operations (expr); 2366 1.1 mrg 2367 1.1 mrg /* Limit walking the dominators to avoid quadraticness in 2368 1.1 mrg the number of BBs times the number of loops in degenerate 2369 1.1 mrg cases. */ 2370 1.1 mrg for (bb = loop->header; 2371 1.1 mrg bb != ENTRY_BLOCK_PTR_FOR_FN (cfun) && cnt < MAX_DOMINATORS_TO_WALK; 2372 1.1 mrg bb = get_immediate_dominator (CDI_DOMINATORS, bb)) 2373 1.1 mrg { 2374 1.1 mrg if (!single_pred_p (bb)) 2375 1.1 mrg continue; 2376 1.1 mrg e = single_pred_edge (bb); 2377 1.1 mrg 2378 1.1 mrg if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) 2379 1.1 mrg continue; 2380 1.1 mrg 2381 1.1 mrg stmt = last_stmt (e->src); 2382 1.1 mrg cond = fold_build2 (gimple_cond_code (stmt), 2383 1.1 mrg boolean_type_node, 2384 1.1 mrg gimple_cond_lhs (stmt), 2385 1.1 mrg gimple_cond_rhs (stmt)); 2386 1.1 mrg if (e->flags & EDGE_FALSE_VALUE) 2387 1.1 mrg cond = invert_truthvalue (cond); 2388 1.1 mrg expanded = tree_simplify_using_condition (cond, expanded); 2389 1.1 mrg /* Break if EXPR is simplified to const values. */ 2390 1.1 mrg if (expanded 2391 1.1 mrg && (integer_zerop (expanded) || integer_nonzerop (expanded))) 2392 1.1 mrg return expanded; 2393 1.1 mrg 2394 1.1 mrg ++cnt; 2395 1.1 mrg } 2396 1.1 mrg 2397 1.1 mrg /* Return the original expression if no simplification is done. */ 2398 1.1 mrg return operand_equal_p (backup, expanded, 0) ? expr : expanded; 2399 1.1 mrg } 2400 1.1 mrg 2401 1.1 mrg /* Tries to simplify EXPR using the evolutions of the loop invariants 2402 1.1 mrg in the superloops of LOOP. Returns the simplified expression 2403 1.1 mrg (or EXPR unchanged, if no simplification was possible). */ 2404 1.1 mrg 2405 1.1 mrg static tree 2406 1.1 mrg simplify_using_outer_evolutions (class loop *loop, tree expr) 2407 1.1 mrg { 2408 1.1 mrg enum tree_code code = TREE_CODE (expr); 2409 1.1 mrg bool changed; 2410 1.1 mrg tree e, e0, e1, e2; 2411 1.1 mrg 2412 1.1 mrg if (is_gimple_min_invariant (expr)) 2413 1.1 mrg return expr; 2414 1.1 mrg 2415 1.1 mrg if (code == TRUTH_OR_EXPR 2416 1.1 mrg || code == TRUTH_AND_EXPR 2417 1.1 mrg || code == COND_EXPR) 2418 1.1 mrg { 2419 1.1 mrg changed = false; 2420 1.1 mrg 2421 1.1 mrg e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0)); 2422 1.1 mrg if (TREE_OPERAND (expr, 0) != e0) 2423 1.1 mrg changed = true; 2424 1.1 mrg 2425 1.1 mrg e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1)); 2426 1.1 mrg if (TREE_OPERAND (expr, 1) != e1) 2427 1.1 mrg changed = true; 2428 1.1 mrg 2429 1.1 mrg if (code == COND_EXPR) 2430 1.1 mrg { 2431 1.1 mrg e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2)); 2432 1.1 mrg if (TREE_OPERAND (expr, 2) != e2) 2433 1.1 mrg changed = true; 2434 1.1 mrg } 2435 1.1 mrg else 2436 1.1 mrg e2 = NULL_TREE; 2437 1.1 mrg 2438 1.1 mrg if (changed) 2439 1.1 mrg { 2440 1.1 mrg if (code == COND_EXPR) 2441 1.1 mrg expr = fold_build3 (code, boolean_type_node, e0, e1, e2); 2442 1.1 mrg else 2443 1.1 mrg expr = fold_build2 (code, boolean_type_node, e0, e1); 2444 1.1 mrg } 2445 1.1 mrg 2446 1.1 mrg return expr; 2447 1.1 mrg } 2448 1.1 mrg 2449 1.1 mrg e = instantiate_parameters (loop, expr); 2450 1.1 mrg if (is_gimple_min_invariant (e)) 2451 1.1 mrg return e; 2452 1.1 mrg 2453 1.1 mrg return expr; 2454 1.1 mrg } 2455 1.1 mrg 2456 1.1 mrg /* Returns true if EXIT is the only possible exit from LOOP. */ 2457 1.1 mrg 2458 1.1 mrg bool 2459 1.1 mrg loop_only_exit_p (const class loop *loop, basic_block *body, const_edge exit) 2460 1.1 mrg { 2461 1.1 mrg gimple_stmt_iterator bsi; 2462 1.1 mrg unsigned i; 2463 1.1 mrg 2464 1.1 mrg if (exit != single_exit (loop)) 2465 1.1 mrg return false; 2466 1.1 mrg 2467 1.1 mrg for (i = 0; i < loop->num_nodes; i++) 2468 1.1 mrg for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi)) 2469 1.1 mrg if (stmt_can_terminate_bb_p (gsi_stmt (bsi))) 2470 1.1 mrg return false; 2471 1.1 mrg 2472 1.1 mrg return true; 2473 1.1 mrg } 2474 1.1 mrg 2475 1.1 mrg /* Stores description of number of iterations of LOOP derived from 2476 1.1 mrg EXIT (an exit edge of the LOOP) in NITER. Returns true if some useful 2477 1.1 mrg information could be derived (and fields of NITER have meaning described 2478 1.1 mrg in comments at class tree_niter_desc declaration), false otherwise. 2479 1.1 mrg When EVERY_ITERATION is true, only tests that are known to be executed 2480 1.1 mrg every iteration are considered (i.e. only test that alone bounds the loop). 2481 1.1 mrg If AT_STMT is not NULL, this function stores LOOP's condition statement in 2482 1.1 mrg it when returning true. */ 2483 1.1 mrg 2484 1.1 mrg bool 2485 1.1 mrg number_of_iterations_exit_assumptions (class loop *loop, edge exit, 2486 1.1 mrg class tree_niter_desc *niter, 2487 1.1 mrg gcond **at_stmt, bool every_iteration, 2488 1.1 mrg basic_block *body) 2489 1.1 mrg { 2490 1.1 mrg gimple *last; 2491 1.1 mrg gcond *stmt; 2492 1.1 mrg tree type; 2493 1.1 mrg tree op0, op1; 2494 1.1 mrg enum tree_code code; 2495 1.1 mrg affine_iv iv0, iv1; 2496 1.1 mrg bool safe; 2497 1.1 mrg 2498 1.1 mrg /* The condition at a fake exit (if it exists) does not control its 2499 1.1 mrg execution. */ 2500 1.1 mrg if (exit->flags & EDGE_FAKE) 2501 1.1 mrg return false; 2502 1.1 mrg 2503 1.1 mrg /* Nothing to analyze if the loop is known to be infinite. */ 2504 1.1 mrg if (loop_constraint_set_p (loop, LOOP_C_INFINITE)) 2505 1.1 mrg return false; 2506 1.1 mrg 2507 1.1 mrg safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src); 2508 1.1 mrg 2509 1.1 mrg if (every_iteration && !safe) 2510 1.1 mrg return false; 2511 1.1 mrg 2512 1.1 mrg niter->assumptions = boolean_false_node; 2513 1.1 mrg niter->control.base = NULL_TREE; 2514 1.1 mrg niter->control.step = NULL_TREE; 2515 1.1 mrg niter->control.no_overflow = false; 2516 1.1 mrg last = last_stmt (exit->src); 2517 1.1 mrg if (!last) 2518 1.1 mrg return false; 2519 1.1 mrg stmt = dyn_cast <gcond *> (last); 2520 1.1 mrg if (!stmt) 2521 1.1 mrg return false; 2522 1.1 mrg 2523 1.1 mrg /* We want the condition for staying inside loop. */ 2524 1.1 mrg code = gimple_cond_code (stmt); 2525 1.1 mrg if (exit->flags & EDGE_TRUE_VALUE) 2526 1.1 mrg code = invert_tree_comparison (code, false); 2527 1.1 mrg 2528 1.1 mrg switch (code) 2529 1.1 mrg { 2530 1.1 mrg case GT_EXPR: 2531 1.1 mrg case GE_EXPR: 2532 1.1 mrg case LT_EXPR: 2533 1.1 mrg case LE_EXPR: 2534 1.1 mrg case NE_EXPR: 2535 1.1 mrg break; 2536 1.1 mrg 2537 1.1 mrg default: 2538 1.1 mrg return false; 2539 1.1 mrg } 2540 1.1 mrg 2541 1.1 mrg op0 = gimple_cond_lhs (stmt); 2542 1.1 mrg op1 = gimple_cond_rhs (stmt); 2543 1.1 mrg type = TREE_TYPE (op0); 2544 1.1 mrg 2545 1.1 mrg if (TREE_CODE (type) != INTEGER_TYPE 2546 1.1 mrg && !POINTER_TYPE_P (type)) 2547 1.1 mrg return false; 2548 1.1 mrg 2549 1.1 mrg tree iv0_niters = NULL_TREE; 2550 1.1 mrg if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), 2551 1.1 mrg op0, &iv0, safe ? &iv0_niters : NULL, false)) 2552 1.1 mrg return number_of_iterations_popcount (loop, exit, code, niter); 2553 1.1 mrg tree iv1_niters = NULL_TREE; 2554 1.1 mrg if (!simple_iv_with_niters (loop, loop_containing_stmt (stmt), 2555 1.1 mrg op1, &iv1, safe ? &iv1_niters : NULL, false)) 2556 1.1 mrg return false; 2557 1.1 mrg /* Give up on complicated case. */ 2558 1.1 mrg if (iv0_niters && iv1_niters) 2559 1.1 mrg return false; 2560 1.1 mrg 2561 1.1 mrg /* We don't want to see undefined signed overflow warnings while 2562 1.1 mrg computing the number of iterations. */ 2563 1.1 mrg fold_defer_overflow_warnings (); 2564 1.1 mrg 2565 1.1 mrg iv0.base = expand_simple_operations (iv0.base); 2566 1.1 mrg iv1.base = expand_simple_operations (iv1.base); 2567 1.1 mrg bool body_from_caller = true; 2568 1.1 mrg if (!body) 2569 1.1 mrg { 2570 1.1 mrg body = get_loop_body (loop); 2571 1.1 mrg body_from_caller = false; 2572 1.1 mrg } 2573 1.1 mrg bool only_exit_p = loop_only_exit_p (loop, body, exit); 2574 1.1 mrg if (!body_from_caller) 2575 1.1 mrg free (body); 2576 1.1 mrg if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter, 2577 1.1 mrg only_exit_p, safe)) 2578 1.1 mrg { 2579 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 2580 1.1 mrg return false; 2581 1.1 mrg } 2582 1.1 mrg 2583 1.1 mrg /* Incorporate additional assumption implied by control iv. */ 2584 1.1 mrg tree iv_niters = iv0_niters ? iv0_niters : iv1_niters; 2585 1.1 mrg if (iv_niters) 2586 1.1 mrg { 2587 1.1 mrg tree assumption = fold_build2 (LE_EXPR, boolean_type_node, niter->niter, 2588 1.1 mrg fold_convert (TREE_TYPE (niter->niter), 2589 1.1 mrg iv_niters)); 2590 1.1 mrg 2591 1.1 mrg if (!integer_nonzerop (assumption)) 2592 1.1 mrg niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, 2593 1.1 mrg niter->assumptions, assumption); 2594 1.1 mrg 2595 1.1 mrg /* Refine upper bound if possible. */ 2596 1.1 mrg if (TREE_CODE (iv_niters) == INTEGER_CST 2597 1.1 mrg && niter->max > wi::to_widest (iv_niters)) 2598 1.1 mrg niter->max = wi::to_widest (iv_niters); 2599 1.1 mrg } 2600 1.1 mrg 2601 1.1 mrg /* There is no assumptions if the loop is known to be finite. */ 2602 1.1 mrg if (!integer_zerop (niter->assumptions) 2603 1.1 mrg && loop_constraint_set_p (loop, LOOP_C_FINITE)) 2604 1.1 mrg niter->assumptions = boolean_true_node; 2605 1.1 mrg 2606 1.1 mrg if (optimize >= 3) 2607 1.1 mrg { 2608 1.1 mrg niter->assumptions = simplify_using_outer_evolutions (loop, 2609 1.1 mrg niter->assumptions); 2610 1.1 mrg niter->may_be_zero = simplify_using_outer_evolutions (loop, 2611 1.1 mrg niter->may_be_zero); 2612 1.1 mrg niter->niter = simplify_using_outer_evolutions (loop, niter->niter); 2613 1.1 mrg } 2614 1.1 mrg 2615 1.1 mrg niter->assumptions 2616 1.1 mrg = simplify_using_initial_conditions (loop, 2617 1.1 mrg niter->assumptions); 2618 1.1 mrg niter->may_be_zero 2619 1.1 mrg = simplify_using_initial_conditions (loop, 2620 1.1 mrg niter->may_be_zero); 2621 1.1 mrg 2622 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 2623 1.1 mrg 2624 1.1 mrg /* If NITER has simplified into a constant, update MAX. */ 2625 1.1 mrg if (TREE_CODE (niter->niter) == INTEGER_CST) 2626 1.1 mrg niter->max = wi::to_widest (niter->niter); 2627 1.1 mrg 2628 1.1 mrg if (at_stmt) 2629 1.1 mrg *at_stmt = stmt; 2630 1.1 mrg 2631 1.1 mrg return (!integer_zerop (niter->assumptions)); 2632 1.1 mrg } 2633 1.1 mrg 2634 1.1 mrg 2635 1.1 mrg /* Utility function to check if OP is defined by a stmt 2636 1.1 mrg that is a val - 1. */ 2637 1.1 mrg 2638 1.1 mrg static bool 2639 1.1 mrg ssa_defined_by_minus_one_stmt_p (tree op, tree val) 2640 1.1 mrg { 2641 1.1 mrg gimple *stmt; 2642 1.1 mrg return (TREE_CODE (op) == SSA_NAME 2643 1.1 mrg && (stmt = SSA_NAME_DEF_STMT (op)) 2644 1.1 mrg && is_gimple_assign (stmt) 2645 1.1 mrg && (gimple_assign_rhs_code (stmt) == PLUS_EXPR) 2646 1.1 mrg && val == gimple_assign_rhs1 (stmt) 2647 1.1 mrg && integer_minus_onep (gimple_assign_rhs2 (stmt))); 2648 1.1 mrg } 2649 1.1 mrg 2650 1.1 mrg 2651 1.1 mrg /* See if LOOP is a popcout implementation, determine NITER for the loop 2652 1.1 mrg 2653 1.1 mrg We match: 2654 1.1 mrg <bb 2> 2655 1.1 mrg goto <bb 4> 2656 1.1 mrg 2657 1.1 mrg <bb 3> 2658 1.1 mrg _1 = b_11 + -1 2659 1.1 mrg b_6 = _1 & b_11 2660 1.1 mrg 2661 1.1 mrg <bb 4> 2662 1.1 mrg b_11 = PHI <b_5(D)(2), b_6(3)> 2663 1.1 mrg 2664 1.1 mrg exit block 2665 1.1 mrg if (b_11 != 0) 2666 1.1 mrg goto <bb 3> 2667 1.1 mrg else 2668 1.1 mrg goto <bb 5> 2669 1.1 mrg 2670 1.1 mrg OR we match copy-header version: 2671 1.1 mrg if (b_5 != 0) 2672 1.1 mrg goto <bb 3> 2673 1.1 mrg else 2674 1.1 mrg goto <bb 4> 2675 1.1 mrg 2676 1.1 mrg <bb 3> 2677 1.1 mrg b_11 = PHI <b_5(2), b_6(3)> 2678 1.1 mrg _1 = b_11 + -1 2679 1.1 mrg b_6 = _1 & b_11 2680 1.1 mrg 2681 1.1 mrg exit block 2682 1.1 mrg if (b_6 != 0) 2683 1.1 mrg goto <bb 3> 2684 1.1 mrg else 2685 1.1 mrg goto <bb 4> 2686 1.1 mrg 2687 1.1 mrg If popcount pattern, update NITER accordingly. 2688 1.1 mrg i.e., set NITER to __builtin_popcount (b) 2689 1.1 mrg return true if we did, false otherwise. 2690 1.1 mrg 2691 1.1 mrg */ 2692 1.1 mrg 2693 1.1 mrg static bool 2694 1.1 mrg number_of_iterations_popcount (loop_p loop, edge exit, 2695 1.1 mrg enum tree_code code, 2696 1.1 mrg class tree_niter_desc *niter) 2697 1.1 mrg { 2698 1.1 mrg bool adjust = true; 2699 1.1 mrg tree iter; 2700 1.1 mrg HOST_WIDE_INT max; 2701 1.1 mrg adjust = true; 2702 1.1 mrg tree fn = NULL_TREE; 2703 1.1 mrg 2704 1.1 mrg /* Check loop terminating branch is like 2705 1.1 mrg if (b != 0). */ 2706 1.1 mrg gimple *stmt = last_stmt (exit->src); 2707 1.1 mrg if (!stmt 2708 1.1 mrg || gimple_code (stmt) != GIMPLE_COND 2709 1.1 mrg || code != NE_EXPR 2710 1.1 mrg || !integer_zerop (gimple_cond_rhs (stmt)) 2711 1.1 mrg || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME) 2712 1.1 mrg return false; 2713 1.1 mrg 2714 1.1 mrg gimple *and_stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt)); 2715 1.1 mrg 2716 1.1 mrg /* Depending on copy-header is performed, feeding PHI stmts might be in 2717 1.1 mrg the loop header or loop latch, handle this. */ 2718 1.1 mrg if (gimple_code (and_stmt) == GIMPLE_PHI 2719 1.1 mrg && gimple_bb (and_stmt) == loop->header 2720 1.1 mrg && gimple_phi_num_args (and_stmt) == 2 2721 1.1 mrg && (TREE_CODE (gimple_phi_arg_def (and_stmt, 2722 1.1 mrg loop_latch_edge (loop)->dest_idx)) 2723 1.1 mrg == SSA_NAME)) 2724 1.1 mrg { 2725 1.1 mrg /* SSA used in exit condition is defined by PHI stmt 2726 1.1 mrg b_11 = PHI <b_5(D)(2), b_6(3)> 2727 1.1 mrg from the PHI stmt, get the and_stmt 2728 1.1 mrg b_6 = _1 & b_11. */ 2729 1.1 mrg tree t = gimple_phi_arg_def (and_stmt, loop_latch_edge (loop)->dest_idx); 2730 1.1 mrg and_stmt = SSA_NAME_DEF_STMT (t); 2731 1.1 mrg adjust = false; 2732 1.1 mrg } 2733 1.1 mrg 2734 1.1 mrg /* Make sure it is indeed an and stmt (b_6 = _1 & b_11). */ 2735 1.1 mrg if (!is_gimple_assign (and_stmt) 2736 1.1 mrg || gimple_assign_rhs_code (and_stmt) != BIT_AND_EXPR) 2737 1.1 mrg return false; 2738 1.1 mrg 2739 1.1 mrg tree b_11 = gimple_assign_rhs1 (and_stmt); 2740 1.1 mrg tree _1 = gimple_assign_rhs2 (and_stmt); 2741 1.1 mrg 2742 1.1 mrg /* Check that _1 is defined by _b11 + -1 (_1 = b_11 + -1). 2743 1.1 mrg Also make sure that b_11 is the same in and_stmt and _1 defining stmt. 2744 1.1 mrg Also canonicalize if _1 and _b11 are revrsed. */ 2745 1.1 mrg if (ssa_defined_by_minus_one_stmt_p (b_11, _1)) 2746 1.1 mrg std::swap (b_11, _1); 2747 1.1 mrg else if (ssa_defined_by_minus_one_stmt_p (_1, b_11)) 2748 1.1 mrg ; 2749 1.1 mrg else 2750 1.1 mrg return false; 2751 1.1 mrg /* Check the recurrence: 2752 1.1 mrg ... = PHI <b_5(2), b_6(3)>. */ 2753 1.1 mrg gimple *phi = SSA_NAME_DEF_STMT (b_11); 2754 1.1 mrg if (gimple_code (phi) != GIMPLE_PHI 2755 1.1 mrg || (gimple_bb (phi) != loop_latch_edge (loop)->dest) 2756 1.1 mrg || (gimple_assign_lhs (and_stmt) 2757 1.1 mrg != gimple_phi_arg_def (phi, loop_latch_edge (loop)->dest_idx))) 2758 1.1 mrg return false; 2759 1.1 mrg 2760 1.1 mrg /* We found a match. Get the corresponding popcount builtin. */ 2761 1.1 mrg tree src = gimple_phi_arg_def (phi, loop_preheader_edge (loop)->dest_idx); 2762 1.1 mrg if (TYPE_PRECISION (TREE_TYPE (src)) <= TYPE_PRECISION (integer_type_node)) 2763 1.1 mrg fn = builtin_decl_implicit (BUILT_IN_POPCOUNT); 2764 1.1 mrg else if (TYPE_PRECISION (TREE_TYPE (src)) 2765 1.1 mrg == TYPE_PRECISION (long_integer_type_node)) 2766 1.1 mrg fn = builtin_decl_implicit (BUILT_IN_POPCOUNTL); 2767 1.1 mrg else if (TYPE_PRECISION (TREE_TYPE (src)) 2768 1.1 mrg == TYPE_PRECISION (long_long_integer_type_node) 2769 1.1 mrg || (TYPE_PRECISION (TREE_TYPE (src)) 2770 1.1 mrg == 2 * TYPE_PRECISION (long_long_integer_type_node))) 2771 1.1 mrg fn = builtin_decl_implicit (BUILT_IN_POPCOUNTLL); 2772 1.1 mrg 2773 1.1 mrg if (!fn) 2774 1.1 mrg return false; 2775 1.1 mrg 2776 1.1 mrg /* Update NITER params accordingly */ 2777 1.1 mrg tree utype = unsigned_type_for (TREE_TYPE (src)); 2778 1.1 mrg src = fold_convert (utype, src); 2779 1.1 mrg if (TYPE_PRECISION (TREE_TYPE (src)) < TYPE_PRECISION (integer_type_node)) 2780 1.1 mrg src = fold_convert (unsigned_type_node, src); 2781 1.1 mrg tree call; 2782 1.1 mrg if (TYPE_PRECISION (TREE_TYPE (src)) 2783 1.1 mrg == 2 * TYPE_PRECISION (long_long_integer_type_node)) 2784 1.1 mrg { 2785 1.1 mrg int prec = TYPE_PRECISION (long_long_integer_type_node); 2786 1.1 mrg tree src1 = fold_convert (long_long_unsigned_type_node, 2787 1.1 mrg fold_build2 (RSHIFT_EXPR, TREE_TYPE (src), 2788 1.1 mrg unshare_expr (src), 2789 1.1 mrg build_int_cst (integer_type_node, 2790 1.1 mrg prec))); 2791 1.1 mrg tree src2 = fold_convert (long_long_unsigned_type_node, src); 2792 1.1 mrg call = build_call_expr (fn, 1, src1); 2793 1.1 mrg call = fold_build2 (PLUS_EXPR, TREE_TYPE (call), call, 2794 1.1 mrg build_call_expr (fn, 1, src2)); 2795 1.1 mrg call = fold_convert (utype, call); 2796 1.1 mrg } 2797 1.1 mrg else 2798 1.1 mrg call = fold_convert (utype, build_call_expr (fn, 1, src)); 2799 1.1 mrg if (adjust) 2800 1.1 mrg iter = fold_build2 (MINUS_EXPR, utype, call, build_int_cst (utype, 1)); 2801 1.1 mrg else 2802 1.1 mrg iter = call; 2803 1.1 mrg 2804 1.1 mrg if (TREE_CODE (call) == INTEGER_CST) 2805 1.1 mrg max = tree_to_uhwi (call); 2806 1.1 mrg else 2807 1.1 mrg max = TYPE_PRECISION (TREE_TYPE (src)); 2808 1.1 mrg if (adjust) 2809 1.1 mrg max = max - 1; 2810 1.1 mrg 2811 1.1 mrg niter->niter = iter; 2812 1.1 mrg niter->assumptions = boolean_true_node; 2813 1.1 mrg 2814 1.1 mrg if (adjust) 2815 1.1 mrg { 2816 1.1 mrg tree may_be_zero = fold_build2 (EQ_EXPR, boolean_type_node, src, 2817 1.1 mrg build_zero_cst (TREE_TYPE (src))); 2818 1.1 mrg niter->may_be_zero 2819 1.1 mrg = simplify_using_initial_conditions (loop, may_be_zero); 2820 1.1 mrg } 2821 1.1 mrg else 2822 1.1 mrg niter->may_be_zero = boolean_false_node; 2823 1.1 mrg 2824 1.1 mrg niter->max = max; 2825 1.1 mrg niter->bound = NULL_TREE; 2826 1.1 mrg niter->cmp = ERROR_MARK; 2827 1.1 mrg return true; 2828 1.1 mrg } 2829 1.1 mrg 2830 1.1 mrg 2831 1.1 mrg /* Like number_of_iterations_exit_assumptions, but return TRUE only if 2832 1.1 mrg the niter information holds unconditionally. */ 2833 1.1 mrg 2834 1.1 mrg bool 2835 1.1 mrg number_of_iterations_exit (class loop *loop, edge exit, 2836 1.1 mrg class tree_niter_desc *niter, 2837 1.1 mrg bool warn, bool every_iteration, 2838 1.1 mrg basic_block *body) 2839 1.1 mrg { 2840 1.1 mrg gcond *stmt; 2841 1.1 mrg if (!number_of_iterations_exit_assumptions (loop, exit, niter, 2842 1.1 mrg &stmt, every_iteration, body)) 2843 1.1 mrg return false; 2844 1.1 mrg 2845 1.1 mrg if (integer_nonzerop (niter->assumptions)) 2846 1.1 mrg return true; 2847 1.1 mrg 2848 1.1 mrg if (warn && dump_enabled_p ()) 2849 1.1 mrg dump_printf_loc (MSG_MISSED_OPTIMIZATION, stmt, 2850 1.1 mrg "missed loop optimization: niters analysis ends up " 2851 1.1 mrg "with assumptions.\n"); 2852 1.1 mrg 2853 1.1 mrg return false; 2854 1.1 mrg } 2855 1.1 mrg 2856 1.1 mrg /* Try to determine the number of iterations of LOOP. If we succeed, 2857 1.1 mrg expression giving number of iterations is returned and *EXIT is 2858 1.1 mrg set to the edge from that the information is obtained. Otherwise 2859 1.1 mrg chrec_dont_know is returned. */ 2860 1.1 mrg 2861 1.1 mrg tree 2862 1.1 mrg find_loop_niter (class loop *loop, edge *exit) 2863 1.1 mrg { 2864 1.1 mrg unsigned i; 2865 1.1 mrg auto_vec<edge> exits = get_loop_exit_edges (loop); 2866 1.1 mrg edge ex; 2867 1.1 mrg tree niter = NULL_TREE, aniter; 2868 1.1 mrg class tree_niter_desc desc; 2869 1.1 mrg 2870 1.1 mrg *exit = NULL; 2871 1.1 mrg FOR_EACH_VEC_ELT (exits, i, ex) 2872 1.1 mrg { 2873 1.1 mrg if (!number_of_iterations_exit (loop, ex, &desc, false)) 2874 1.1 mrg continue; 2875 1.1 mrg 2876 1.1 mrg if (integer_nonzerop (desc.may_be_zero)) 2877 1.1 mrg { 2878 1.1 mrg /* We exit in the first iteration through this exit. 2879 1.1 mrg We won't find anything better. */ 2880 1.1 mrg niter = build_int_cst (unsigned_type_node, 0); 2881 1.1 mrg *exit = ex; 2882 1.1 mrg break; 2883 1.1 mrg } 2884 1.1 mrg 2885 1.1 mrg if (!integer_zerop (desc.may_be_zero)) 2886 1.1 mrg continue; 2887 1.1 mrg 2888 1.1 mrg aniter = desc.niter; 2889 1.1 mrg 2890 1.1 mrg if (!niter) 2891 1.1 mrg { 2892 1.1 mrg /* Nothing recorded yet. */ 2893 1.1 mrg niter = aniter; 2894 1.1 mrg *exit = ex; 2895 1.1 mrg continue; 2896 1.1 mrg } 2897 1.1 mrg 2898 1.1 mrg /* Prefer constants, the lower the better. */ 2899 1.1 mrg if (TREE_CODE (aniter) != INTEGER_CST) 2900 1.1 mrg continue; 2901 1.1 mrg 2902 1.1 mrg if (TREE_CODE (niter) != INTEGER_CST) 2903 1.1 mrg { 2904 1.1 mrg niter = aniter; 2905 1.1 mrg *exit = ex; 2906 1.1 mrg continue; 2907 1.1 mrg } 2908 1.1 mrg 2909 1.1 mrg if (tree_int_cst_lt (aniter, niter)) 2910 1.1 mrg { 2911 1.1 mrg niter = aniter; 2912 1.1 mrg *exit = ex; 2913 1.1 mrg continue; 2914 1.1 mrg } 2915 1.1 mrg } 2916 1.1 mrg 2917 1.1 mrg return niter ? niter : chrec_dont_know; 2918 1.1 mrg } 2919 1.1 mrg 2920 1.1 mrg /* Return true if loop is known to have bounded number of iterations. */ 2921 1.1 mrg 2922 1.1 mrg bool 2923 1.1 mrg finite_loop_p (class loop *loop) 2924 1.1 mrg { 2925 1.1 mrg widest_int nit; 2926 1.1 mrg int flags; 2927 1.1 mrg 2928 1.1 mrg flags = flags_from_decl_or_type (current_function_decl); 2929 1.1 mrg if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE)) 2930 1.1 mrg { 2931 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 2932 1.1 mrg fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n", 2933 1.1 mrg loop->num); 2934 1.1 mrg return true; 2935 1.1 mrg } 2936 1.1 mrg 2937 1.1 mrg if (loop->any_upper_bound 2938 1.1 mrg || max_loop_iterations (loop, &nit)) 2939 1.1 mrg { 2940 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 2941 1.1 mrg fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n", 2942 1.1 mrg loop->num); 2943 1.1 mrg return true; 2944 1.1 mrg } 2945 1.1 mrg 2946 1.1 mrg if (loop->finite_p) 2947 1.1 mrg { 2948 1.1 mrg unsigned i; 2949 1.1 mrg auto_vec<edge> exits = get_loop_exit_edges (loop); 2950 1.1 mrg edge ex; 2951 1.1 mrg 2952 1.1 mrg /* If the loop has a normal exit, we can assume it will terminate. */ 2953 1.1 mrg FOR_EACH_VEC_ELT (exits, i, ex) 2954 1.1 mrg if (!(ex->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_FAKE))) 2955 1.1 mrg { 2956 1.1 mrg if (dump_file) 2957 1.1 mrg fprintf (dump_file, "Assume loop %i to be finite: it has an exit " 2958 1.1 mrg "and -ffinite-loops is on.\n", loop->num); 2959 1.1 mrg return true; 2960 1.1 mrg } 2961 1.1 mrg } 2962 1.1 mrg 2963 1.1 mrg return false; 2964 1.1 mrg } 2965 1.1 mrg 2966 1.1 mrg /* 2967 1.1 mrg 2968 1.1 mrg Analysis of a number of iterations of a loop by a brute-force evaluation. 2969 1.1 mrg 2970 1.1 mrg */ 2971 1.1 mrg 2972 1.1 mrg /* Bound on the number of iterations we try to evaluate. */ 2973 1.1 mrg 2974 1.1 mrg #define MAX_ITERATIONS_TO_TRACK \ 2975 1.1 mrg ((unsigned) param_max_iterations_to_track) 2976 1.1 mrg 2977 1.1 mrg /* Returns the loop phi node of LOOP such that ssa name X is derived from its 2978 1.1 mrg result by a chain of operations such that all but exactly one of their 2979 1.1 mrg operands are constants. */ 2980 1.1 mrg 2981 1.1 mrg static gphi * 2982 1.1 mrg chain_of_csts_start (class loop *loop, tree x) 2983 1.1 mrg { 2984 1.1 mrg gimple *stmt = SSA_NAME_DEF_STMT (x); 2985 1.1 mrg tree use; 2986 1.1 mrg basic_block bb = gimple_bb (stmt); 2987 1.1 mrg enum tree_code code; 2988 1.1 mrg 2989 1.1 mrg if (!bb 2990 1.1 mrg || !flow_bb_inside_loop_p (loop, bb)) 2991 1.1 mrg return NULL; 2992 1.1 mrg 2993 1.1 mrg if (gimple_code (stmt) == GIMPLE_PHI) 2994 1.1 mrg { 2995 1.1 mrg if (bb == loop->header) 2996 1.1 mrg return as_a <gphi *> (stmt); 2997 1.1 mrg 2998 1.1 mrg return NULL; 2999 1.1 mrg } 3000 1.1 mrg 3001 1.1 mrg if (gimple_code (stmt) != GIMPLE_ASSIGN 3002 1.1 mrg || gimple_assign_rhs_class (stmt) == GIMPLE_TERNARY_RHS) 3003 1.1 mrg return NULL; 3004 1.1 mrg 3005 1.1 mrg code = gimple_assign_rhs_code (stmt); 3006 1.1 mrg if (gimple_references_memory_p (stmt) 3007 1.1 mrg || TREE_CODE_CLASS (code) == tcc_reference 3008 1.1 mrg || (code == ADDR_EXPR 3009 1.1 mrg && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))) 3010 1.1 mrg return NULL; 3011 1.1 mrg 3012 1.1 mrg use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE); 3013 1.1 mrg if (use == NULL_TREE) 3014 1.1 mrg return NULL; 3015 1.1 mrg 3016 1.1 mrg return chain_of_csts_start (loop, use); 3017 1.1 mrg } 3018 1.1 mrg 3019 1.1 mrg /* Determines whether the expression X is derived from a result of a phi node 3020 1.1 mrg in header of LOOP such that 3021 1.1 mrg 3022 1.1 mrg * the derivation of X consists only from operations with constants 3023 1.1 mrg * the initial value of the phi node is constant 3024 1.1 mrg * the value of the phi node in the next iteration can be derived from the 3025 1.1 mrg value in the current iteration by a chain of operations with constants, 3026 1.1 mrg or is also a constant 3027 1.1 mrg 3028 1.1 mrg If such phi node exists, it is returned, otherwise NULL is returned. */ 3029 1.1 mrg 3030 1.1 mrg static gphi * 3031 1.1 mrg get_base_for (class loop *loop, tree x) 3032 1.1 mrg { 3033 1.1 mrg gphi *phi; 3034 1.1 mrg tree init, next; 3035 1.1 mrg 3036 1.1 mrg if (is_gimple_min_invariant (x)) 3037 1.1 mrg return NULL; 3038 1.1 mrg 3039 1.1 mrg phi = chain_of_csts_start (loop, x); 3040 1.1 mrg if (!phi) 3041 1.1 mrg return NULL; 3042 1.1 mrg 3043 1.1 mrg init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); 3044 1.1 mrg next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); 3045 1.1 mrg 3046 1.1 mrg if (!is_gimple_min_invariant (init)) 3047 1.1 mrg return NULL; 3048 1.1 mrg 3049 1.1 mrg if (TREE_CODE (next) == SSA_NAME 3050 1.1 mrg && chain_of_csts_start (loop, next) != phi) 3051 1.1 mrg return NULL; 3052 1.1 mrg 3053 1.1 mrg return phi; 3054 1.1 mrg } 3055 1.1 mrg 3056 1.1 mrg /* Given an expression X, then 3057 1.1 mrg 3058 1.1 mrg * if X is NULL_TREE, we return the constant BASE. 3059 1.1 mrg * if X is a constant, we return the constant X. 3060 1.1 mrg * otherwise X is a SSA name, whose value in the considered loop is derived 3061 1.1 mrg by a chain of operations with constant from a result of a phi node in 3062 1.1 mrg the header of the loop. Then we return value of X when the value of the 3063 1.1 mrg result of this phi node is given by the constant BASE. */ 3064 1.1 mrg 3065 1.1 mrg static tree 3066 1.1 mrg get_val_for (tree x, tree base) 3067 1.1 mrg { 3068 1.1 mrg gimple *stmt; 3069 1.1 mrg 3070 1.1 mrg gcc_checking_assert (is_gimple_min_invariant (base)); 3071 1.1 mrg 3072 1.1 mrg if (!x) 3073 1.1 mrg return base; 3074 1.1 mrg else if (is_gimple_min_invariant (x)) 3075 1.1 mrg return x; 3076 1.1 mrg 3077 1.1 mrg stmt = SSA_NAME_DEF_STMT (x); 3078 1.1 mrg if (gimple_code (stmt) == GIMPLE_PHI) 3079 1.1 mrg return base; 3080 1.1 mrg 3081 1.1 mrg gcc_checking_assert (is_gimple_assign (stmt)); 3082 1.1 mrg 3083 1.1 mrg /* STMT must be either an assignment of a single SSA name or an 3084 1.1 mrg expression involving an SSA name and a constant. Try to fold that 3085 1.1 mrg expression using the value for the SSA name. */ 3086 1.1 mrg if (gimple_assign_ssa_name_copy_p (stmt)) 3087 1.1 mrg return get_val_for (gimple_assign_rhs1 (stmt), base); 3088 1.1 mrg else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS 3089 1.1 mrg && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME) 3090 1.1 mrg return fold_build1 (gimple_assign_rhs_code (stmt), 3091 1.1 mrg TREE_TYPE (gimple_assign_lhs (stmt)), 3092 1.1 mrg get_val_for (gimple_assign_rhs1 (stmt), base)); 3093 1.1 mrg else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS) 3094 1.1 mrg { 3095 1.1 mrg tree rhs1 = gimple_assign_rhs1 (stmt); 3096 1.1 mrg tree rhs2 = gimple_assign_rhs2 (stmt); 3097 1.1 mrg if (TREE_CODE (rhs1) == SSA_NAME) 3098 1.1 mrg rhs1 = get_val_for (rhs1, base); 3099 1.1 mrg else if (TREE_CODE (rhs2) == SSA_NAME) 3100 1.1 mrg rhs2 = get_val_for (rhs2, base); 3101 1.1 mrg else 3102 1.1 mrg gcc_unreachable (); 3103 1.1 mrg return fold_build2 (gimple_assign_rhs_code (stmt), 3104 1.1 mrg TREE_TYPE (gimple_assign_lhs (stmt)), rhs1, rhs2); 3105 1.1 mrg } 3106 1.1 mrg else 3107 1.1 mrg gcc_unreachable (); 3108 1.1 mrg } 3109 1.1 mrg 3110 1.1 mrg 3111 1.1 mrg /* Tries to count the number of iterations of LOOP till it exits by EXIT 3112 1.1 mrg by brute force -- i.e. by determining the value of the operands of the 3113 1.1 mrg condition at EXIT in first few iterations of the loop (assuming that 3114 1.1 mrg these values are constant) and determining the first one in that the 3115 1.1 mrg condition is not satisfied. Returns the constant giving the number 3116 1.1 mrg of the iterations of LOOP if successful, chrec_dont_know otherwise. */ 3117 1.1 mrg 3118 1.1 mrg tree 3119 1.1 mrg loop_niter_by_eval (class loop *loop, edge exit) 3120 1.1 mrg { 3121 1.1 mrg tree acnd; 3122 1.1 mrg tree op[2], val[2], next[2], aval[2]; 3123 1.1 mrg gphi *phi; 3124 1.1 mrg gimple *cond; 3125 1.1 mrg unsigned i, j; 3126 1.1 mrg enum tree_code cmp; 3127 1.1 mrg 3128 1.1 mrg cond = last_stmt (exit->src); 3129 1.1 mrg if (!cond || gimple_code (cond) != GIMPLE_COND) 3130 1.1 mrg return chrec_dont_know; 3131 1.1 mrg 3132 1.1 mrg cmp = gimple_cond_code (cond); 3133 1.1 mrg if (exit->flags & EDGE_TRUE_VALUE) 3134 1.1 mrg cmp = invert_tree_comparison (cmp, false); 3135 1.1 mrg 3136 1.1 mrg switch (cmp) 3137 1.1 mrg { 3138 1.1 mrg case EQ_EXPR: 3139 1.1 mrg case NE_EXPR: 3140 1.1 mrg case GT_EXPR: 3141 1.1 mrg case GE_EXPR: 3142 1.1 mrg case LT_EXPR: 3143 1.1 mrg case LE_EXPR: 3144 1.1 mrg op[0] = gimple_cond_lhs (cond); 3145 1.1 mrg op[1] = gimple_cond_rhs (cond); 3146 1.1 mrg break; 3147 1.1 mrg 3148 1.1 mrg default: 3149 1.1 mrg return chrec_dont_know; 3150 1.1 mrg } 3151 1.1 mrg 3152 1.1 mrg for (j = 0; j < 2; j++) 3153 1.1 mrg { 3154 1.1 mrg if (is_gimple_min_invariant (op[j])) 3155 1.1 mrg { 3156 1.1 mrg val[j] = op[j]; 3157 1.1 mrg next[j] = NULL_TREE; 3158 1.1 mrg op[j] = NULL_TREE; 3159 1.1 mrg } 3160 1.1 mrg else 3161 1.1 mrg { 3162 1.1 mrg phi = get_base_for (loop, op[j]); 3163 1.1 mrg if (!phi) 3164 1.1 mrg return chrec_dont_know; 3165 1.1 mrg val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); 3166 1.1 mrg next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); 3167 1.1 mrg } 3168 1.1 mrg } 3169 1.1 mrg 3170 1.1 mrg /* Don't issue signed overflow warnings. */ 3171 1.1 mrg fold_defer_overflow_warnings (); 3172 1.1 mrg 3173 1.1 mrg for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++) 3174 1.1 mrg { 3175 1.1 mrg for (j = 0; j < 2; j++) 3176 1.1 mrg aval[j] = get_val_for (op[j], val[j]); 3177 1.1 mrg 3178 1.1 mrg acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]); 3179 1.1 mrg if (acnd && integer_zerop (acnd)) 3180 1.1 mrg { 3181 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 3182 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 3183 1.1 mrg fprintf (dump_file, 3184 1.1 mrg "Proved that loop %d iterates %d times using brute force.\n", 3185 1.1 mrg loop->num, i); 3186 1.1 mrg return build_int_cst (unsigned_type_node, i); 3187 1.1 mrg } 3188 1.1 mrg 3189 1.1 mrg for (j = 0; j < 2; j++) 3190 1.1 mrg { 3191 1.1 mrg aval[j] = val[j]; 3192 1.1 mrg val[j] = get_val_for (next[j], val[j]); 3193 1.1 mrg if (!is_gimple_min_invariant (val[j])) 3194 1.1 mrg { 3195 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 3196 1.1 mrg return chrec_dont_know; 3197 1.1 mrg } 3198 1.1 mrg } 3199 1.1 mrg 3200 1.1 mrg /* If the next iteration would use the same base values 3201 1.1 mrg as the current one, there is no point looping further, 3202 1.1 mrg all following iterations will be the same as this one. */ 3203 1.1 mrg if (val[0] == aval[0] && val[1] == aval[1]) 3204 1.1 mrg break; 3205 1.1 mrg } 3206 1.1 mrg 3207 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 3208 1.1 mrg 3209 1.1 mrg return chrec_dont_know; 3210 1.1 mrg } 3211 1.1 mrg 3212 1.1 mrg /* Finds the exit of the LOOP by that the loop exits after a constant 3213 1.1 mrg number of iterations and stores the exit edge to *EXIT. The constant 3214 1.1 mrg giving the number of iterations of LOOP is returned. The number of 3215 1.1 mrg iterations is determined using loop_niter_by_eval (i.e. by brute force 3216 1.1 mrg evaluation). If we are unable to find the exit for that loop_niter_by_eval 3217 1.1 mrg determines the number of iterations, chrec_dont_know is returned. */ 3218 1.1 mrg 3219 1.1 mrg tree 3220 1.1 mrg find_loop_niter_by_eval (class loop *loop, edge *exit) 3221 1.1 mrg { 3222 1.1 mrg unsigned i; 3223 1.1 mrg auto_vec<edge> exits = get_loop_exit_edges (loop); 3224 1.1 mrg edge ex; 3225 1.1 mrg tree niter = NULL_TREE, aniter; 3226 1.1 mrg 3227 1.1 mrg *exit = NULL; 3228 1.1 mrg 3229 1.1 mrg /* Loops with multiple exits are expensive to handle and less important. */ 3230 1.1 mrg if (!flag_expensive_optimizations 3231 1.1 mrg && exits.length () > 1) 3232 1.1 mrg return chrec_dont_know; 3233 1.1 mrg 3234 1.1 mrg FOR_EACH_VEC_ELT (exits, i, ex) 3235 1.1 mrg { 3236 1.1 mrg if (!just_once_each_iteration_p (loop, ex->src)) 3237 1.1 mrg continue; 3238 1.1 mrg 3239 1.1 mrg aniter = loop_niter_by_eval (loop, ex); 3240 1.1 mrg if (chrec_contains_undetermined (aniter)) 3241 1.1 mrg continue; 3242 1.1 mrg 3243 1.1 mrg if (niter 3244 1.1 mrg && !tree_int_cst_lt (aniter, niter)) 3245 1.1 mrg continue; 3246 1.1 mrg 3247 1.1 mrg niter = aniter; 3248 1.1 mrg *exit = ex; 3249 1.1 mrg } 3250 1.1 mrg 3251 1.1 mrg return niter ? niter : chrec_dont_know; 3252 1.1 mrg } 3253 1.1 mrg 3254 1.1 mrg /* 3255 1.1 mrg 3256 1.1 mrg Analysis of upper bounds on number of iterations of a loop. 3257 1.1 mrg 3258 1.1 mrg */ 3259 1.1 mrg 3260 1.1 mrg static widest_int derive_constant_upper_bound_ops (tree, tree, 3261 1.1 mrg enum tree_code, tree); 3262 1.1 mrg 3263 1.1 mrg /* Returns a constant upper bound on the value of the right-hand side of 3264 1.1 mrg an assignment statement STMT. */ 3265 1.1 mrg 3266 1.1 mrg static widest_int 3267 1.1 mrg derive_constant_upper_bound_assign (gimple *stmt) 3268 1.1 mrg { 3269 1.1 mrg enum tree_code code = gimple_assign_rhs_code (stmt); 3270 1.1 mrg tree op0 = gimple_assign_rhs1 (stmt); 3271 1.1 mrg tree op1 = gimple_assign_rhs2 (stmt); 3272 1.1 mrg 3273 1.1 mrg return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)), 3274 1.1 mrg op0, code, op1); 3275 1.1 mrg } 3276 1.1 mrg 3277 1.1 mrg /* Returns a constant upper bound on the value of expression VAL. VAL 3278 1.1 mrg is considered to be unsigned. If its type is signed, its value must 3279 1.1 mrg be nonnegative. */ 3280 1.1 mrg 3281 1.1 mrg static widest_int 3282 1.1 mrg derive_constant_upper_bound (tree val) 3283 1.1 mrg { 3284 1.1 mrg enum tree_code code; 3285 1.1 mrg tree op0, op1, op2; 3286 1.1 mrg 3287 1.1 mrg extract_ops_from_tree (val, &code, &op0, &op1, &op2); 3288 1.1 mrg return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1); 3289 1.1 mrg } 3290 1.1 mrg 3291 1.1 mrg /* Returns a constant upper bound on the value of expression OP0 CODE OP1, 3292 1.1 mrg whose type is TYPE. The expression is considered to be unsigned. If 3293 1.1 mrg its type is signed, its value must be nonnegative. */ 3294 1.1 mrg 3295 1.1 mrg static widest_int 3296 1.1 mrg derive_constant_upper_bound_ops (tree type, tree op0, 3297 1.1 mrg enum tree_code code, tree op1) 3298 1.1 mrg { 3299 1.1 mrg tree subtype, maxt; 3300 1.1 mrg widest_int bnd, max, cst; 3301 1.1 mrg gimple *stmt; 3302 1.1 mrg 3303 1.1 mrg if (INTEGRAL_TYPE_P (type)) 3304 1.1 mrg maxt = TYPE_MAX_VALUE (type); 3305 1.1 mrg else 3306 1.1 mrg maxt = upper_bound_in_type (type, type); 3307 1.1 mrg 3308 1.1 mrg max = wi::to_widest (maxt); 3309 1.1 mrg 3310 1.1 mrg switch (code) 3311 1.1 mrg { 3312 1.1 mrg case INTEGER_CST: 3313 1.1 mrg return wi::to_widest (op0); 3314 1.1 mrg 3315 1.1 mrg CASE_CONVERT: 3316 1.1 mrg subtype = TREE_TYPE (op0); 3317 1.1 mrg if (!TYPE_UNSIGNED (subtype) 3318 1.1 mrg /* If TYPE is also signed, the fact that VAL is nonnegative implies 3319 1.1 mrg that OP0 is nonnegative. */ 3320 1.1 mrg && TYPE_UNSIGNED (type) 3321 1.1 mrg && !tree_expr_nonnegative_p (op0)) 3322 1.1 mrg { 3323 1.1 mrg /* If we cannot prove that the casted expression is nonnegative, 3324 1.1 mrg we cannot establish more useful upper bound than the precision 3325 1.1 mrg of the type gives us. */ 3326 1.1 mrg return max; 3327 1.1 mrg } 3328 1.1 mrg 3329 1.1 mrg /* We now know that op0 is an nonnegative value. Try deriving an upper 3330 1.1 mrg bound for it. */ 3331 1.1 mrg bnd = derive_constant_upper_bound (op0); 3332 1.1 mrg 3333 1.1 mrg /* If the bound does not fit in TYPE, max. value of TYPE could be 3334 1.1 mrg attained. */ 3335 1.1 mrg if (wi::ltu_p (max, bnd)) 3336 1.1 mrg return max; 3337 1.1 mrg 3338 1.1 mrg return bnd; 3339 1.1 mrg 3340 1.1 mrg case PLUS_EXPR: 3341 1.1 mrg case POINTER_PLUS_EXPR: 3342 1.1 mrg case MINUS_EXPR: 3343 1.1 mrg if (TREE_CODE (op1) != INTEGER_CST 3344 1.1 mrg || !tree_expr_nonnegative_p (op0)) 3345 1.1 mrg return max; 3346 1.1 mrg 3347 1.1 mrg /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to 3348 1.1 mrg choose the most logical way how to treat this constant regardless 3349 1.1 mrg of the signedness of the type. */ 3350 1.1 mrg cst = wi::sext (wi::to_widest (op1), TYPE_PRECISION (type)); 3351 1.1 mrg if (code != MINUS_EXPR) 3352 1.1 mrg cst = -cst; 3353 1.1 mrg 3354 1.1 mrg bnd = derive_constant_upper_bound (op0); 3355 1.1 mrg 3356 1.1 mrg if (wi::neg_p (cst)) 3357 1.1 mrg { 3358 1.1 mrg cst = -cst; 3359 1.1 mrg /* Avoid CST == 0x80000... */ 3360 1.1 mrg if (wi::neg_p (cst)) 3361 1.1 mrg return max; 3362 1.1 mrg 3363 1.1 mrg /* OP0 + CST. We need to check that 3364 1.1 mrg BND <= MAX (type) - CST. */ 3365 1.1 mrg 3366 1.1 mrg widest_int mmax = max - cst; 3367 1.1 mrg if (wi::leu_p (bnd, mmax)) 3368 1.1 mrg return max; 3369 1.1 mrg 3370 1.1 mrg return bnd + cst; 3371 1.1 mrg } 3372 1.1 mrg else 3373 1.1 mrg { 3374 1.1 mrg /* OP0 - CST, where CST >= 0. 3375 1.1 mrg 3376 1.1 mrg If TYPE is signed, we have already verified that OP0 >= 0, and we 3377 1.1 mrg know that the result is nonnegative. This implies that 3378 1.1 mrg VAL <= BND - CST. 3379 1.1 mrg 3380 1.1 mrg If TYPE is unsigned, we must additionally know that OP0 >= CST, 3381 1.1 mrg otherwise the operation underflows. 3382 1.1 mrg */ 3383 1.1 mrg 3384 1.1 mrg /* This should only happen if the type is unsigned; however, for 3385 1.1 mrg buggy programs that use overflowing signed arithmetics even with 3386 1.1 mrg -fno-wrapv, this condition may also be true for signed values. */ 3387 1.1 mrg if (wi::ltu_p (bnd, cst)) 3388 1.1 mrg return max; 3389 1.1 mrg 3390 1.1 mrg if (TYPE_UNSIGNED (type)) 3391 1.1 mrg { 3392 1.1 mrg tree tem = fold_binary (GE_EXPR, boolean_type_node, op0, 3393 1.1 mrg wide_int_to_tree (type, cst)); 3394 1.1 mrg if (!tem || integer_nonzerop (tem)) 3395 1.1 mrg return max; 3396 1.1 mrg } 3397 1.1 mrg 3398 1.1 mrg bnd -= cst; 3399 1.1 mrg } 3400 1.1 mrg 3401 1.1 mrg return bnd; 3402 1.1 mrg 3403 1.1 mrg case FLOOR_DIV_EXPR: 3404 1.1 mrg case EXACT_DIV_EXPR: 3405 1.1 mrg if (TREE_CODE (op1) != INTEGER_CST 3406 1.1 mrg || tree_int_cst_sign_bit (op1)) 3407 1.1 mrg return max; 3408 1.1 mrg 3409 1.1 mrg bnd = derive_constant_upper_bound (op0); 3410 1.1 mrg return wi::udiv_floor (bnd, wi::to_widest (op1)); 3411 1.1 mrg 3412 1.1 mrg case BIT_AND_EXPR: 3413 1.1 mrg if (TREE_CODE (op1) != INTEGER_CST 3414 1.1 mrg || tree_int_cst_sign_bit (op1)) 3415 1.1 mrg return max; 3416 1.1 mrg return wi::to_widest (op1); 3417 1.1 mrg 3418 1.1 mrg case SSA_NAME: 3419 1.1 mrg stmt = SSA_NAME_DEF_STMT (op0); 3420 1.1 mrg if (gimple_code (stmt) != GIMPLE_ASSIGN 3421 1.1 mrg || gimple_assign_lhs (stmt) != op0) 3422 1.1 mrg return max; 3423 1.1 mrg return derive_constant_upper_bound_assign (stmt); 3424 1.1 mrg 3425 1.1 mrg default: 3426 1.1 mrg return max; 3427 1.1 mrg } 3428 1.1 mrg } 3429 1.1 mrg 3430 1.1 mrg /* Emit a -Waggressive-loop-optimizations warning if needed. */ 3431 1.1 mrg 3432 1.1 mrg static void 3433 1.1 mrg do_warn_aggressive_loop_optimizations (class loop *loop, 3434 1.1 mrg widest_int i_bound, gimple *stmt) 3435 1.1 mrg { 3436 1.1 mrg /* Don't warn if the loop doesn't have known constant bound. */ 3437 1.1 mrg if (!loop->nb_iterations 3438 1.1 mrg || TREE_CODE (loop->nb_iterations) != INTEGER_CST 3439 1.1 mrg || !warn_aggressive_loop_optimizations 3440 1.1 mrg /* To avoid warning multiple times for the same loop, 3441 1.1 mrg only start warning when we preserve loops. */ 3442 1.1 mrg || (cfun->curr_properties & PROP_loops) == 0 3443 1.1 mrg /* Only warn once per loop. */ 3444 1.1 mrg || loop->warned_aggressive_loop_optimizations 3445 1.1 mrg /* Only warn if undefined behavior gives us lower estimate than the 3446 1.1 mrg known constant bound. */ 3447 1.1 mrg || wi::cmpu (i_bound, wi::to_widest (loop->nb_iterations)) >= 0 3448 1.1 mrg /* And undefined behavior happens unconditionally. */ 3449 1.1 mrg || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt))) 3450 1.1 mrg return; 3451 1.1 mrg 3452 1.1 mrg edge e = single_exit (loop); 3453 1.1 mrg if (e == NULL) 3454 1.1 mrg return; 3455 1.1 mrg 3456 1.1 mrg gimple *estmt = last_stmt (e->src); 3457 1.1 mrg char buf[WIDE_INT_PRINT_BUFFER_SIZE]; 3458 1.1 mrg print_dec (i_bound, buf, TYPE_UNSIGNED (TREE_TYPE (loop->nb_iterations)) 3459 1.1 mrg ? UNSIGNED : SIGNED); 3460 1.1 mrg auto_diagnostic_group d; 3461 1.1 mrg if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations, 3462 1.1 mrg "iteration %s invokes undefined behavior", buf)) 3463 1.1 mrg inform (gimple_location (estmt), "within this loop"); 3464 1.1 mrg loop->warned_aggressive_loop_optimizations = true; 3465 1.1 mrg } 3466 1.1 mrg 3467 1.1 mrg /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT 3468 1.1 mrg is true if the loop is exited immediately after STMT, and this exit 3469 1.1 mrg is taken at last when the STMT is executed BOUND + 1 times. 3470 1.1 mrg REALISTIC is true if BOUND is expected to be close to the real number 3471 1.1 mrg of iterations. UPPER is true if we are sure the loop iterates at most 3472 1.1 mrg BOUND times. I_BOUND is a widest_int upper estimate on BOUND. */ 3473 1.1 mrg 3474 1.1 mrg static void 3475 1.1 mrg record_estimate (class loop *loop, tree bound, const widest_int &i_bound, 3476 1.1 mrg gimple *at_stmt, bool is_exit, bool realistic, bool upper) 3477 1.1 mrg { 3478 1.1 mrg widest_int delta; 3479 1.1 mrg 3480 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 3481 1.1 mrg { 3482 1.1 mrg fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : ""); 3483 1.1 mrg print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM); 3484 1.1 mrg fprintf (dump_file, " is %sexecuted at most ", 3485 1.1 mrg upper ? "" : "probably "); 3486 1.1 mrg print_generic_expr (dump_file, bound, TDF_SLIM); 3487 1.1 mrg fprintf (dump_file, " (bounded by "); 3488 1.1 mrg print_decu (i_bound, dump_file); 3489 1.1 mrg fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num); 3490 1.1 mrg } 3491 1.1 mrg 3492 1.1 mrg /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the 3493 1.1 mrg real number of iterations. */ 3494 1.1 mrg if (TREE_CODE (bound) != INTEGER_CST) 3495 1.1 mrg realistic = false; 3496 1.1 mrg else 3497 1.1 mrg gcc_checking_assert (i_bound == wi::to_widest (bound)); 3498 1.1 mrg 3499 1.1 mrg /* If we have a guaranteed upper bound, record it in the appropriate 3500 1.1 mrg list, unless this is an !is_exit bound (i.e. undefined behavior in 3501 1.1 mrg at_stmt) in a loop with known constant number of iterations. */ 3502 1.1 mrg if (upper 3503 1.1 mrg && (is_exit 3504 1.1 mrg || loop->nb_iterations == NULL_TREE 3505 1.1 mrg || TREE_CODE (loop->nb_iterations) != INTEGER_CST)) 3506 1.1 mrg { 3507 1.1 mrg class nb_iter_bound *elt = ggc_alloc<nb_iter_bound> (); 3508 1.1 mrg 3509 1.1 mrg elt->bound = i_bound; 3510 1.1 mrg elt->stmt = at_stmt; 3511 1.1 mrg elt->is_exit = is_exit; 3512 1.1 mrg elt->next = loop->bounds; 3513 1.1 mrg loop->bounds = elt; 3514 1.1 mrg } 3515 1.1 mrg 3516 1.1 mrg /* If statement is executed on every path to the loop latch, we can directly 3517 1.1 mrg infer the upper bound on the # of iterations of the loop. */ 3518 1.1 mrg if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt))) 3519 1.1 mrg upper = false; 3520 1.1 mrg 3521 1.1 mrg /* Update the number of iteration estimates according to the bound. 3522 1.1 mrg If at_stmt is an exit then the loop latch is executed at most BOUND times, 3523 1.1 mrg otherwise it can be executed BOUND + 1 times. We will lower the estimate 3524 1.1 mrg later if such statement must be executed on last iteration */ 3525 1.1 mrg if (is_exit) 3526 1.1 mrg delta = 0; 3527 1.1 mrg else 3528 1.1 mrg delta = 1; 3529 1.1 mrg widest_int new_i_bound = i_bound + delta; 3530 1.1 mrg 3531 1.1 mrg /* If an overflow occurred, ignore the result. */ 3532 1.1 mrg if (wi::ltu_p (new_i_bound, delta)) 3533 1.1 mrg return; 3534 1.1 mrg 3535 1.1 mrg if (upper && !is_exit) 3536 1.1 mrg do_warn_aggressive_loop_optimizations (loop, new_i_bound, at_stmt); 3537 1.1 mrg record_niter_bound (loop, new_i_bound, realistic, upper); 3538 1.1 mrg } 3539 1.1 mrg 3540 1.1 mrg /* Records the control iv analyzed in NITER for LOOP if the iv is valid 3541 1.1 mrg and doesn't overflow. */ 3542 1.1 mrg 3543 1.1 mrg static void 3544 1.1 mrg record_control_iv (class loop *loop, class tree_niter_desc *niter) 3545 1.1 mrg { 3546 1.1 mrg struct control_iv *iv; 3547 1.1 mrg 3548 1.1 mrg if (!niter->control.base || !niter->control.step) 3549 1.1 mrg return; 3550 1.1 mrg 3551 1.1 mrg if (!integer_onep (niter->assumptions) || !niter->control.no_overflow) 3552 1.1 mrg return; 3553 1.1 mrg 3554 1.1 mrg iv = ggc_alloc<control_iv> (); 3555 1.1 mrg iv->base = niter->control.base; 3556 1.1 mrg iv->step = niter->control.step; 3557 1.1 mrg iv->next = loop->control_ivs; 3558 1.1 mrg loop->control_ivs = iv; 3559 1.1 mrg 3560 1.1 mrg return; 3561 1.1 mrg } 3562 1.1 mrg 3563 1.1 mrg /* This function returns TRUE if below conditions are satisfied: 3564 1.1 mrg 1) VAR is SSA variable. 3565 1.1 mrg 2) VAR is an IV:{base, step} in its defining loop. 3566 1.1 mrg 3) IV doesn't overflow. 3567 1.1 mrg 4) Both base and step are integer constants. 3568 1.1 mrg 5) Base is the MIN/MAX value depends on IS_MIN. 3569 1.1 mrg Store value of base to INIT correspondingly. */ 3570 1.1 mrg 3571 1.1 mrg static bool 3572 1.1 mrg get_cst_init_from_scev (tree var, wide_int *init, bool is_min) 3573 1.1 mrg { 3574 1.1 mrg if (TREE_CODE (var) != SSA_NAME) 3575 1.1 mrg return false; 3576 1.1 mrg 3577 1.1 mrg gimple *def_stmt = SSA_NAME_DEF_STMT (var); 3578 1.1 mrg class loop *loop = loop_containing_stmt (def_stmt); 3579 1.1 mrg 3580 1.1 mrg if (loop == NULL) 3581 1.1 mrg return false; 3582 1.1 mrg 3583 1.1 mrg affine_iv iv; 3584 1.1 mrg if (!simple_iv (loop, loop, var, &iv, false)) 3585 1.1 mrg return false; 3586 1.1 mrg 3587 1.1 mrg if (!iv.no_overflow) 3588 1.1 mrg return false; 3589 1.1 mrg 3590 1.1 mrg if (TREE_CODE (iv.base) != INTEGER_CST || TREE_CODE (iv.step) != INTEGER_CST) 3591 1.1 mrg return false; 3592 1.1 mrg 3593 1.1 mrg if (is_min == tree_int_cst_sign_bit (iv.step)) 3594 1.1 mrg return false; 3595 1.1 mrg 3596 1.1 mrg *init = wi::to_wide (iv.base); 3597 1.1 mrg return true; 3598 1.1 mrg } 3599 1.1 mrg 3600 1.1 mrg /* Record the estimate on number of iterations of LOOP based on the fact that 3601 1.1 mrg the induction variable BASE + STEP * i evaluated in STMT does not wrap and 3602 1.1 mrg its values belong to the range <LOW, HIGH>. REALISTIC is true if the 3603 1.1 mrg estimated number of iterations is expected to be close to the real one. 3604 1.1 mrg UPPER is true if we are sure the induction variable does not wrap. */ 3605 1.1 mrg 3606 1.1 mrg static void 3607 1.1 mrg record_nonwrapping_iv (class loop *loop, tree base, tree step, gimple *stmt, 3608 1.1 mrg tree low, tree high, bool realistic, bool upper) 3609 1.1 mrg { 3610 1.1 mrg tree niter_bound, extreme, delta; 3611 1.1 mrg tree type = TREE_TYPE (base), unsigned_type; 3612 1.1 mrg tree orig_base = base; 3613 1.1 mrg 3614 1.1 mrg if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step)) 3615 1.1 mrg return; 3616 1.1 mrg 3617 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 3618 1.1 mrg { 3619 1.1 mrg fprintf (dump_file, "Induction variable ("); 3620 1.1 mrg print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM); 3621 1.1 mrg fprintf (dump_file, ") "); 3622 1.1 mrg print_generic_expr (dump_file, base, TDF_SLIM); 3623 1.1 mrg fprintf (dump_file, " + "); 3624 1.1 mrg print_generic_expr (dump_file, step, TDF_SLIM); 3625 1.1 mrg fprintf (dump_file, " * iteration does not wrap in statement "); 3626 1.1 mrg print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); 3627 1.1 mrg fprintf (dump_file, " in loop %d.\n", loop->num); 3628 1.1 mrg } 3629 1.1 mrg 3630 1.1 mrg unsigned_type = unsigned_type_for (type); 3631 1.1 mrg base = fold_convert (unsigned_type, base); 3632 1.1 mrg step = fold_convert (unsigned_type, step); 3633 1.1 mrg 3634 1.1 mrg if (tree_int_cst_sign_bit (step)) 3635 1.1 mrg { 3636 1.1 mrg wide_int max; 3637 1.1 mrg value_range base_range; 3638 1.1 mrg if (get_range_query (cfun)->range_of_expr (base_range, orig_base) 3639 1.1 mrg && !base_range.undefined_p ()) 3640 1.1 mrg max = base_range.upper_bound (); 3641 1.1 mrg extreme = fold_convert (unsigned_type, low); 3642 1.1 mrg if (TREE_CODE (orig_base) == SSA_NAME 3643 1.1 mrg && TREE_CODE (high) == INTEGER_CST 3644 1.1 mrg && INTEGRAL_TYPE_P (TREE_TYPE (orig_base)) 3645 1.1 mrg && (base_range.kind () == VR_RANGE 3646 1.1 mrg || get_cst_init_from_scev (orig_base, &max, false)) 3647 1.1 mrg && wi::gts_p (wi::to_wide (high), max)) 3648 1.1 mrg base = wide_int_to_tree (unsigned_type, max); 3649 1.1 mrg else if (TREE_CODE (base) != INTEGER_CST 3650 1.1 mrg && dominated_by_p (CDI_DOMINATORS, 3651 1.1 mrg loop->latch, gimple_bb (stmt))) 3652 1.1 mrg base = fold_convert (unsigned_type, high); 3653 1.1 mrg delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme); 3654 1.1 mrg step = fold_build1 (NEGATE_EXPR, unsigned_type, step); 3655 1.1 mrg } 3656 1.1 mrg else 3657 1.1 mrg { 3658 1.1 mrg wide_int min; 3659 1.1 mrg value_range base_range; 3660 1.1 mrg if (get_range_query (cfun)->range_of_expr (base_range, orig_base) 3661 1.1 mrg && !base_range.undefined_p ()) 3662 1.1 mrg min = base_range.lower_bound (); 3663 1.1 mrg extreme = fold_convert (unsigned_type, high); 3664 1.1 mrg if (TREE_CODE (orig_base) == SSA_NAME 3665 1.1 mrg && TREE_CODE (low) == INTEGER_CST 3666 1.1 mrg && INTEGRAL_TYPE_P (TREE_TYPE (orig_base)) 3667 1.1 mrg && (base_range.kind () == VR_RANGE 3668 1.1 mrg || get_cst_init_from_scev (orig_base, &min, true)) 3669 1.1 mrg && wi::gts_p (min, wi::to_wide (low))) 3670 1.1 mrg base = wide_int_to_tree (unsigned_type, min); 3671 1.1 mrg else if (TREE_CODE (base) != INTEGER_CST 3672 1.1 mrg && dominated_by_p (CDI_DOMINATORS, 3673 1.1 mrg loop->latch, gimple_bb (stmt))) 3674 1.1 mrg base = fold_convert (unsigned_type, low); 3675 1.1 mrg delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base); 3676 1.1 mrg } 3677 1.1 mrg 3678 1.1 mrg /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value 3679 1.1 mrg would get out of the range. */ 3680 1.1 mrg niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step); 3681 1.1 mrg widest_int max = derive_constant_upper_bound (niter_bound); 3682 1.1 mrg record_estimate (loop, niter_bound, max, stmt, false, realistic, upper); 3683 1.1 mrg } 3684 1.1 mrg 3685 1.1 mrg /* Determine information about number of iterations a LOOP from the index 3686 1.1 mrg IDX of a data reference accessed in STMT. RELIABLE is true if STMT is 3687 1.1 mrg guaranteed to be executed in every iteration of LOOP. Callback for 3688 1.1 mrg for_each_index. */ 3689 1.1 mrg 3690 1.1 mrg struct ilb_data 3691 1.1 mrg { 3692 1.1 mrg class loop *loop; 3693 1.1 mrg gimple *stmt; 3694 1.1 mrg }; 3695 1.1 mrg 3696 1.1 mrg static bool 3697 1.1 mrg idx_infer_loop_bounds (tree base, tree *idx, void *dta) 3698 1.1 mrg { 3699 1.1 mrg struct ilb_data *data = (struct ilb_data *) dta; 3700 1.1 mrg tree ev, init, step; 3701 1.1 mrg tree low, high, type, next; 3702 1.1 mrg bool sign, upper = true, at_end = false; 3703 1.1 mrg class loop *loop = data->loop; 3704 1.1 mrg 3705 1.1 mrg if (TREE_CODE (base) != ARRAY_REF) 3706 1.1 mrg return true; 3707 1.1 mrg 3708 1.1 mrg /* For arrays at the end of the structure, we are not guaranteed that they 3709 1.1 mrg do not really extend over their declared size. However, for arrays of 3710 1.1 mrg size greater than one, this is unlikely to be intended. */ 3711 1.1 mrg if (array_at_struct_end_p (base)) 3712 1.1 mrg { 3713 1.1 mrg at_end = true; 3714 1.1 mrg upper = false; 3715 1.1 mrg } 3716 1.1 mrg 3717 1.1 mrg class loop *dloop = loop_containing_stmt (data->stmt); 3718 1.1 mrg if (!dloop) 3719 1.1 mrg return true; 3720 1.1 mrg 3721 1.1 mrg ev = analyze_scalar_evolution (dloop, *idx); 3722 1.1 mrg ev = instantiate_parameters (loop, ev); 3723 1.1 mrg init = initial_condition (ev); 3724 1.1 mrg step = evolution_part_in_loop_num (ev, loop->num); 3725 1.1 mrg 3726 1.1 mrg if (!init 3727 1.1 mrg || !step 3728 1.1 mrg || TREE_CODE (step) != INTEGER_CST 3729 1.1 mrg || integer_zerop (step) 3730 1.1 mrg || tree_contains_chrecs (init, NULL) 3731 1.1 mrg || chrec_contains_symbols_defined_in_loop (init, loop->num)) 3732 1.1 mrg return true; 3733 1.1 mrg 3734 1.1 mrg low = array_ref_low_bound (base); 3735 1.1 mrg high = array_ref_up_bound (base); 3736 1.1 mrg 3737 1.1 mrg /* The case of nonconstant bounds could be handled, but it would be 3738 1.1 mrg complicated. */ 3739 1.1 mrg if (TREE_CODE (low) != INTEGER_CST 3740 1.1 mrg || !high 3741 1.1 mrg || TREE_CODE (high) != INTEGER_CST) 3742 1.1 mrg return true; 3743 1.1 mrg sign = tree_int_cst_sign_bit (step); 3744 1.1 mrg type = TREE_TYPE (step); 3745 1.1 mrg 3746 1.1 mrg /* The array of length 1 at the end of a structure most likely extends 3747 1.1 mrg beyond its bounds. */ 3748 1.1 mrg if (at_end 3749 1.1 mrg && operand_equal_p (low, high, 0)) 3750 1.1 mrg return true; 3751 1.1 mrg 3752 1.1 mrg /* In case the relevant bound of the array does not fit in type, or 3753 1.1 mrg it does, but bound + step (in type) still belongs into the range of the 3754 1.1 mrg array, the index may wrap and still stay within the range of the array 3755 1.1 mrg (consider e.g. if the array is indexed by the full range of 3756 1.1 mrg unsigned char). 3757 1.1 mrg 3758 1.1 mrg To make things simpler, we require both bounds to fit into type, although 3759 1.1 mrg there are cases where this would not be strictly necessary. */ 3760 1.1 mrg if (!int_fits_type_p (high, type) 3761 1.1 mrg || !int_fits_type_p (low, type)) 3762 1.1 mrg return true; 3763 1.1 mrg low = fold_convert (type, low); 3764 1.1 mrg high = fold_convert (type, high); 3765 1.1 mrg 3766 1.1 mrg if (sign) 3767 1.1 mrg next = fold_binary (PLUS_EXPR, type, low, step); 3768 1.1 mrg else 3769 1.1 mrg next = fold_binary (PLUS_EXPR, type, high, step); 3770 1.1 mrg 3771 1.1 mrg if (tree_int_cst_compare (low, next) <= 0 3772 1.1 mrg && tree_int_cst_compare (next, high) <= 0) 3773 1.1 mrg return true; 3774 1.1 mrg 3775 1.1 mrg /* If access is not executed on every iteration, we must ensure that overlow 3776 1.1 mrg may not make the access valid later. */ 3777 1.1 mrg if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt)) 3778 1.1 mrg && scev_probably_wraps_p (NULL_TREE, 3779 1.1 mrg initial_condition_in_loop_num (ev, loop->num), 3780 1.1 mrg step, data->stmt, loop, true)) 3781 1.1 mrg upper = false; 3782 1.1 mrg 3783 1.1 mrg record_nonwrapping_iv (loop, init, step, data->stmt, low, high, false, upper); 3784 1.1 mrg return true; 3785 1.1 mrg } 3786 1.1 mrg 3787 1.1 mrg /* Determine information about number of iterations a LOOP from the bounds 3788 1.1 mrg of arrays in the data reference REF accessed in STMT. RELIABLE is true if 3789 1.1 mrg STMT is guaranteed to be executed in every iteration of LOOP.*/ 3790 1.1 mrg 3791 1.1 mrg static void 3792 1.1 mrg infer_loop_bounds_from_ref (class loop *loop, gimple *stmt, tree ref) 3793 1.1 mrg { 3794 1.1 mrg struct ilb_data data; 3795 1.1 mrg 3796 1.1 mrg data.loop = loop; 3797 1.1 mrg data.stmt = stmt; 3798 1.1 mrg for_each_index (&ref, idx_infer_loop_bounds, &data); 3799 1.1 mrg } 3800 1.1 mrg 3801 1.1 mrg /* Determine information about number of iterations of a LOOP from the way 3802 1.1 mrg arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be 3803 1.1 mrg executed in every iteration of LOOP. */ 3804 1.1 mrg 3805 1.1 mrg static void 3806 1.1 mrg infer_loop_bounds_from_array (class loop *loop, gimple *stmt) 3807 1.1 mrg { 3808 1.1 mrg if (is_gimple_assign (stmt)) 3809 1.1 mrg { 3810 1.1 mrg tree op0 = gimple_assign_lhs (stmt); 3811 1.1 mrg tree op1 = gimple_assign_rhs1 (stmt); 3812 1.1 mrg 3813 1.1 mrg /* For each memory access, analyze its access function 3814 1.1 mrg and record a bound on the loop iteration domain. */ 3815 1.1 mrg if (REFERENCE_CLASS_P (op0)) 3816 1.1 mrg infer_loop_bounds_from_ref (loop, stmt, op0); 3817 1.1 mrg 3818 1.1 mrg if (REFERENCE_CLASS_P (op1)) 3819 1.1 mrg infer_loop_bounds_from_ref (loop, stmt, op1); 3820 1.1 mrg } 3821 1.1 mrg else if (is_gimple_call (stmt)) 3822 1.1 mrg { 3823 1.1 mrg tree arg, lhs; 3824 1.1 mrg unsigned i, n = gimple_call_num_args (stmt); 3825 1.1 mrg 3826 1.1 mrg lhs = gimple_call_lhs (stmt); 3827 1.1 mrg if (lhs && REFERENCE_CLASS_P (lhs)) 3828 1.1 mrg infer_loop_bounds_from_ref (loop, stmt, lhs); 3829 1.1 mrg 3830 1.1 mrg for (i = 0; i < n; i++) 3831 1.1 mrg { 3832 1.1 mrg arg = gimple_call_arg (stmt, i); 3833 1.1 mrg if (REFERENCE_CLASS_P (arg)) 3834 1.1 mrg infer_loop_bounds_from_ref (loop, stmt, arg); 3835 1.1 mrg } 3836 1.1 mrg } 3837 1.1 mrg } 3838 1.1 mrg 3839 1.1 mrg /* Determine information about number of iterations of a LOOP from the fact 3840 1.1 mrg that pointer arithmetics in STMT does not overflow. */ 3841 1.1 mrg 3842 1.1 mrg static void 3843 1.1 mrg infer_loop_bounds_from_pointer_arith (class loop *loop, gimple *stmt) 3844 1.1 mrg { 3845 1.1 mrg tree def, base, step, scev, type, low, high; 3846 1.1 mrg tree var, ptr; 3847 1.1 mrg 3848 1.1 mrg if (!is_gimple_assign (stmt) 3849 1.1 mrg || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR) 3850 1.1 mrg return; 3851 1.1 mrg 3852 1.1 mrg def = gimple_assign_lhs (stmt); 3853 1.1 mrg if (TREE_CODE (def) != SSA_NAME) 3854 1.1 mrg return; 3855 1.1 mrg 3856 1.1 mrg type = TREE_TYPE (def); 3857 1.1 mrg if (!nowrap_type_p (type)) 3858 1.1 mrg return; 3859 1.1 mrg 3860 1.1 mrg ptr = gimple_assign_rhs1 (stmt); 3861 1.1 mrg if (!expr_invariant_in_loop_p (loop, ptr)) 3862 1.1 mrg return; 3863 1.1 mrg 3864 1.1 mrg var = gimple_assign_rhs2 (stmt); 3865 1.1 mrg if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var))) 3866 1.1 mrg return; 3867 1.1 mrg 3868 1.1 mrg class loop *uloop = loop_containing_stmt (stmt); 3869 1.1 mrg scev = instantiate_parameters (loop, analyze_scalar_evolution (uloop, def)); 3870 1.1 mrg if (chrec_contains_undetermined (scev)) 3871 1.1 mrg return; 3872 1.1 mrg 3873 1.1 mrg base = initial_condition_in_loop_num (scev, loop->num); 3874 1.1 mrg step = evolution_part_in_loop_num (scev, loop->num); 3875 1.1 mrg 3876 1.1 mrg if (!base || !step 3877 1.1 mrg || TREE_CODE (step) != INTEGER_CST 3878 1.1 mrg || tree_contains_chrecs (base, NULL) 3879 1.1 mrg || chrec_contains_symbols_defined_in_loop (base, loop->num)) 3880 1.1 mrg return; 3881 1.1 mrg 3882 1.1 mrg low = lower_bound_in_type (type, type); 3883 1.1 mrg high = upper_bound_in_type (type, type); 3884 1.1 mrg 3885 1.1 mrg /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot 3886 1.1 mrg produce a NULL pointer. The contrary would mean NULL points to an object, 3887 1.1 mrg while NULL is supposed to compare unequal with the address of all objects. 3888 1.1 mrg Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a 3889 1.1 mrg NULL pointer since that would mean wrapping, which we assume here not to 3890 1.1 mrg happen. So, we can exclude NULL from the valid range of pointer 3891 1.1 mrg arithmetic. */ 3892 1.1 mrg if (flag_delete_null_pointer_checks && int_cst_value (low) == 0) 3893 1.1 mrg low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type))); 3894 1.1 mrg 3895 1.1 mrg record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); 3896 1.1 mrg } 3897 1.1 mrg 3898 1.1 mrg /* Determine information about number of iterations of a LOOP from the fact 3899 1.1 mrg that signed arithmetics in STMT does not overflow. */ 3900 1.1 mrg 3901 1.1 mrg static void 3902 1.1 mrg infer_loop_bounds_from_signedness (class loop *loop, gimple *stmt) 3903 1.1 mrg { 3904 1.1 mrg tree def, base, step, scev, type, low, high; 3905 1.1 mrg 3906 1.1 mrg if (gimple_code (stmt) != GIMPLE_ASSIGN) 3907 1.1 mrg return; 3908 1.1 mrg 3909 1.1 mrg def = gimple_assign_lhs (stmt); 3910 1.1 mrg 3911 1.1 mrg if (TREE_CODE (def) != SSA_NAME) 3912 1.1 mrg return; 3913 1.1 mrg 3914 1.1 mrg type = TREE_TYPE (def); 3915 1.1 mrg if (!INTEGRAL_TYPE_P (type) 3916 1.1 mrg || !TYPE_OVERFLOW_UNDEFINED (type)) 3917 1.1 mrg return; 3918 1.1 mrg 3919 1.1 mrg scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def)); 3920 1.1 mrg if (chrec_contains_undetermined (scev)) 3921 1.1 mrg return; 3922 1.1 mrg 3923 1.1 mrg base = initial_condition_in_loop_num (scev, loop->num); 3924 1.1 mrg step = evolution_part_in_loop_num (scev, loop->num); 3925 1.1 mrg 3926 1.1 mrg if (!base || !step 3927 1.1 mrg || TREE_CODE (step) != INTEGER_CST 3928 1.1 mrg || tree_contains_chrecs (base, NULL) 3929 1.1 mrg || chrec_contains_symbols_defined_in_loop (base, loop->num)) 3930 1.1 mrg return; 3931 1.1 mrg 3932 1.1 mrg low = lower_bound_in_type (type, type); 3933 1.1 mrg high = upper_bound_in_type (type, type); 3934 1.1 mrg value_range r; 3935 1.1 mrg get_range_query (cfun)->range_of_expr (r, def); 3936 1.1 mrg if (r.kind () == VR_RANGE) 3937 1.1 mrg { 3938 1.1 mrg low = wide_int_to_tree (type, r.lower_bound ()); 3939 1.1 mrg high = wide_int_to_tree (type, r.upper_bound ()); 3940 1.1 mrg } 3941 1.1 mrg 3942 1.1 mrg record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); 3943 1.1 mrg } 3944 1.1 mrg 3945 1.1 mrg /* The following analyzers are extracting informations on the bounds 3946 1.1 mrg of LOOP from the following undefined behaviors: 3947 1.1 mrg 3948 1.1 mrg - data references should not access elements over the statically 3949 1.1 mrg allocated size, 3950 1.1 mrg 3951 1.1 mrg - signed variables should not overflow when flag_wrapv is not set. 3952 1.1 mrg */ 3953 1.1 mrg 3954 1.1 mrg static void 3955 1.1 mrg infer_loop_bounds_from_undefined (class loop *loop, basic_block *bbs) 3956 1.1 mrg { 3957 1.1 mrg unsigned i; 3958 1.1 mrg gimple_stmt_iterator bsi; 3959 1.1 mrg basic_block bb; 3960 1.1 mrg bool reliable; 3961 1.1 mrg 3962 1.1 mrg for (i = 0; i < loop->num_nodes; i++) 3963 1.1 mrg { 3964 1.1 mrg bb = bbs[i]; 3965 1.1 mrg 3966 1.1 mrg /* If BB is not executed in each iteration of the loop, we cannot 3967 1.1 mrg use the operations in it to infer reliable upper bound on the 3968 1.1 mrg # of iterations of the loop. However, we can use it as a guess. 3969 1.1 mrg Reliable guesses come only from array bounds. */ 3970 1.1 mrg reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb); 3971 1.1 mrg 3972 1.1 mrg for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) 3973 1.1 mrg { 3974 1.1 mrg gimple *stmt = gsi_stmt (bsi); 3975 1.1 mrg 3976 1.1 mrg infer_loop_bounds_from_array (loop, stmt); 3977 1.1 mrg 3978 1.1 mrg if (reliable) 3979 1.1 mrg { 3980 1.1 mrg infer_loop_bounds_from_signedness (loop, stmt); 3981 1.1 mrg infer_loop_bounds_from_pointer_arith (loop, stmt); 3982 1.1 mrg } 3983 1.1 mrg } 3984 1.1 mrg 3985 1.1 mrg } 3986 1.1 mrg } 3987 1.1 mrg 3988 1.1 mrg /* Compare wide ints, callback for qsort. */ 3989 1.1 mrg 3990 1.1 mrg static int 3991 1.1 mrg wide_int_cmp (const void *p1, const void *p2) 3992 1.1 mrg { 3993 1.1 mrg const widest_int *d1 = (const widest_int *) p1; 3994 1.1 mrg const widest_int *d2 = (const widest_int *) p2; 3995 1.1 mrg return wi::cmpu (*d1, *d2); 3996 1.1 mrg } 3997 1.1 mrg 3998 1.1 mrg /* Return index of BOUND in BOUNDS array sorted in increasing order. 3999 1.1 mrg Lookup by binary search. */ 4000 1.1 mrg 4001 1.1 mrg static int 4002 1.1 mrg bound_index (const vec<widest_int> &bounds, const widest_int &bound) 4003 1.1 mrg { 4004 1.1 mrg unsigned int end = bounds.length (); 4005 1.1 mrg unsigned int begin = 0; 4006 1.1 mrg 4007 1.1 mrg /* Find a matching index by means of a binary search. */ 4008 1.1 mrg while (begin != end) 4009 1.1 mrg { 4010 1.1 mrg unsigned int middle = (begin + end) / 2; 4011 1.1 mrg widest_int index = bounds[middle]; 4012 1.1 mrg 4013 1.1 mrg if (index == bound) 4014 1.1 mrg return middle; 4015 1.1 mrg else if (wi::ltu_p (index, bound)) 4016 1.1 mrg begin = middle + 1; 4017 1.1 mrg else 4018 1.1 mrg end = middle; 4019 1.1 mrg } 4020 1.1 mrg gcc_unreachable (); 4021 1.1 mrg } 4022 1.1 mrg 4023 1.1 mrg /* We recorded loop bounds only for statements dominating loop latch (and thus 4024 1.1 mrg executed each loop iteration). If there are any bounds on statements not 4025 1.1 mrg dominating the loop latch we can improve the estimate by walking the loop 4026 1.1 mrg body and seeing if every path from loop header to loop latch contains 4027 1.1 mrg some bounded statement. */ 4028 1.1 mrg 4029 1.1 mrg static void 4030 1.1 mrg discover_iteration_bound_by_body_walk (class loop *loop) 4031 1.1 mrg { 4032 1.1 mrg class nb_iter_bound *elt; 4033 1.1 mrg auto_vec<widest_int> bounds; 4034 1.1 mrg vec<vec<basic_block> > queues = vNULL; 4035 1.1 mrg vec<basic_block> queue = vNULL; 4036 1.1 mrg ptrdiff_t queue_index; 4037 1.1 mrg ptrdiff_t latch_index = 0; 4038 1.1 mrg 4039 1.1 mrg /* Discover what bounds may interest us. */ 4040 1.1 mrg for (elt = loop->bounds; elt; elt = elt->next) 4041 1.1 mrg { 4042 1.1 mrg widest_int bound = elt->bound; 4043 1.1 mrg 4044 1.1 mrg /* Exit terminates loop at given iteration, while non-exits produce undefined 4045 1.1 mrg effect on the next iteration. */ 4046 1.1 mrg if (!elt->is_exit) 4047 1.1 mrg { 4048 1.1 mrg bound += 1; 4049 1.1 mrg /* If an overflow occurred, ignore the result. */ 4050 1.1 mrg if (bound == 0) 4051 1.1 mrg continue; 4052 1.1 mrg } 4053 1.1 mrg 4054 1.1 mrg if (!loop->any_upper_bound 4055 1.1 mrg || wi::ltu_p (bound, loop->nb_iterations_upper_bound)) 4056 1.1 mrg bounds.safe_push (bound); 4057 1.1 mrg } 4058 1.1 mrg 4059 1.1 mrg /* Exit early if there is nothing to do. */ 4060 1.1 mrg if (!bounds.exists ()) 4061 1.1 mrg return; 4062 1.1 mrg 4063 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 4064 1.1 mrg fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n"); 4065 1.1 mrg 4066 1.1 mrg /* Sort the bounds in decreasing order. */ 4067 1.1 mrg bounds.qsort (wide_int_cmp); 4068 1.1 mrg 4069 1.1 mrg /* For every basic block record the lowest bound that is guaranteed to 4070 1.1 mrg terminate the loop. */ 4071 1.1 mrg 4072 1.1 mrg hash_map<basic_block, ptrdiff_t> bb_bounds; 4073 1.1 mrg for (elt = loop->bounds; elt; elt = elt->next) 4074 1.1 mrg { 4075 1.1 mrg widest_int bound = elt->bound; 4076 1.1 mrg if (!elt->is_exit) 4077 1.1 mrg { 4078 1.1 mrg bound += 1; 4079 1.1 mrg /* If an overflow occurred, ignore the result. */ 4080 1.1 mrg if (bound == 0) 4081 1.1 mrg continue; 4082 1.1 mrg } 4083 1.1 mrg 4084 1.1 mrg if (!loop->any_upper_bound 4085 1.1 mrg || wi::ltu_p (bound, loop->nb_iterations_upper_bound)) 4086 1.1 mrg { 4087 1.1 mrg ptrdiff_t index = bound_index (bounds, bound); 4088 1.1 mrg ptrdiff_t *entry = bb_bounds.get (gimple_bb (elt->stmt)); 4089 1.1 mrg if (!entry) 4090 1.1 mrg bb_bounds.put (gimple_bb (elt->stmt), index); 4091 1.1 mrg else if ((ptrdiff_t)*entry > index) 4092 1.1 mrg *entry = index; 4093 1.1 mrg } 4094 1.1 mrg } 4095 1.1 mrg 4096 1.1 mrg hash_map<basic_block, ptrdiff_t> block_priority; 4097 1.1 mrg 4098 1.1 mrg /* Perform shortest path discovery loop->header ... loop->latch. 4099 1.1 mrg 4100 1.1 mrg The "distance" is given by the smallest loop bound of basic block 4101 1.1 mrg present in the path and we look for path with largest smallest bound 4102 1.1 mrg on it. 4103 1.1 mrg 4104 1.1 mrg To avoid the need for fibonacci heap on double ints we simply compress 4105 1.1 mrg double ints into indexes to BOUNDS array and then represent the queue 4106 1.1 mrg as arrays of queues for every index. 4107 1.1 mrg Index of BOUNDS.length() means that the execution of given BB has 4108 1.1 mrg no bounds determined. 4109 1.1 mrg 4110 1.1 mrg VISITED is a pointer map translating basic block into smallest index 4111 1.1 mrg it was inserted into the priority queue with. */ 4112 1.1 mrg latch_index = -1; 4113 1.1 mrg 4114 1.1 mrg /* Start walk in loop header with index set to infinite bound. */ 4115 1.1 mrg queue_index = bounds.length (); 4116 1.1 mrg queues.safe_grow_cleared (queue_index + 1, true); 4117 1.1 mrg queue.safe_push (loop->header); 4118 1.1 mrg queues[queue_index] = queue; 4119 1.1 mrg block_priority.put (loop->header, queue_index); 4120 1.1 mrg 4121 1.1 mrg for (; queue_index >= 0; queue_index--) 4122 1.1 mrg { 4123 1.1 mrg if (latch_index < queue_index) 4124 1.1 mrg { 4125 1.1 mrg while (queues[queue_index].length ()) 4126 1.1 mrg { 4127 1.1 mrg basic_block bb; 4128 1.1 mrg ptrdiff_t bound_index = queue_index; 4129 1.1 mrg edge e; 4130 1.1 mrg edge_iterator ei; 4131 1.1 mrg 4132 1.1 mrg queue = queues[queue_index]; 4133 1.1 mrg bb = queue.pop (); 4134 1.1 mrg 4135 1.1 mrg /* OK, we later inserted the BB with lower priority, skip it. */ 4136 1.1 mrg if (*block_priority.get (bb) > queue_index) 4137 1.1 mrg continue; 4138 1.1 mrg 4139 1.1 mrg /* See if we can improve the bound. */ 4140 1.1 mrg ptrdiff_t *entry = bb_bounds.get (bb); 4141 1.1 mrg if (entry && *entry < bound_index) 4142 1.1 mrg bound_index = *entry; 4143 1.1 mrg 4144 1.1 mrg /* Insert succesors into the queue, watch for latch edge 4145 1.1 mrg and record greatest index we saw. */ 4146 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 4147 1.1 mrg { 4148 1.1 mrg bool insert = false; 4149 1.1 mrg 4150 1.1 mrg if (loop_exit_edge_p (loop, e)) 4151 1.1 mrg continue; 4152 1.1 mrg 4153 1.1 mrg if (e == loop_latch_edge (loop) 4154 1.1 mrg && latch_index < bound_index) 4155 1.1 mrg latch_index = bound_index; 4156 1.1 mrg else if (!(entry = block_priority.get (e->dest))) 4157 1.1 mrg { 4158 1.1 mrg insert = true; 4159 1.1 mrg block_priority.put (e->dest, bound_index); 4160 1.1 mrg } 4161 1.1 mrg else if (*entry < bound_index) 4162 1.1 mrg { 4163 1.1 mrg insert = true; 4164 1.1 mrg *entry = bound_index; 4165 1.1 mrg } 4166 1.1 mrg 4167 1.1 mrg if (insert) 4168 1.1 mrg queues[bound_index].safe_push (e->dest); 4169 1.1 mrg } 4170 1.1 mrg } 4171 1.1 mrg } 4172 1.1 mrg queues[queue_index].release (); 4173 1.1 mrg } 4174 1.1 mrg 4175 1.1 mrg gcc_assert (latch_index >= 0); 4176 1.1 mrg if ((unsigned)latch_index < bounds.length ()) 4177 1.1 mrg { 4178 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 4179 1.1 mrg { 4180 1.1 mrg fprintf (dump_file, "Found better loop bound "); 4181 1.1 mrg print_decu (bounds[latch_index], dump_file); 4182 1.1 mrg fprintf (dump_file, "\n"); 4183 1.1 mrg } 4184 1.1 mrg record_niter_bound (loop, bounds[latch_index], false, true); 4185 1.1 mrg } 4186 1.1 mrg 4187 1.1 mrg queues.release (); 4188 1.1 mrg } 4189 1.1 mrg 4190 1.1 mrg /* See if every path cross the loop goes through a statement that is known 4191 1.1 mrg to not execute at the last iteration. In that case we can decrese iteration 4192 1.1 mrg count by 1. */ 4193 1.1 mrg 4194 1.1 mrg static void 4195 1.1 mrg maybe_lower_iteration_bound (class loop *loop) 4196 1.1 mrg { 4197 1.1 mrg hash_set<gimple *> *not_executed_last_iteration = NULL; 4198 1.1 mrg class nb_iter_bound *elt; 4199 1.1 mrg bool found_exit = false; 4200 1.1 mrg auto_vec<basic_block> queue; 4201 1.1 mrg bitmap visited; 4202 1.1 mrg 4203 1.1 mrg /* Collect all statements with interesting (i.e. lower than 4204 1.1 mrg nb_iterations_upper_bound) bound on them. 4205 1.1 mrg 4206 1.1 mrg TODO: Due to the way record_estimate choose estimates to store, the bounds 4207 1.1 mrg will be always nb_iterations_upper_bound-1. We can change this to record 4208 1.1 mrg also statements not dominating the loop latch and update the walk bellow 4209 1.1 mrg to the shortest path algorithm. */ 4210 1.1 mrg for (elt = loop->bounds; elt; elt = elt->next) 4211 1.1 mrg { 4212 1.1 mrg if (!elt->is_exit 4213 1.1 mrg && wi::ltu_p (elt->bound, loop->nb_iterations_upper_bound)) 4214 1.1 mrg { 4215 1.1 mrg if (!not_executed_last_iteration) 4216 1.1 mrg not_executed_last_iteration = new hash_set<gimple *>; 4217 1.1 mrg not_executed_last_iteration->add (elt->stmt); 4218 1.1 mrg } 4219 1.1 mrg } 4220 1.1 mrg if (!not_executed_last_iteration) 4221 1.1 mrg return; 4222 1.1 mrg 4223 1.1 mrg /* Start DFS walk in the loop header and see if we can reach the 4224 1.1 mrg loop latch or any of the exits (including statements with side 4225 1.1 mrg effects that may terminate the loop otherwise) without visiting 4226 1.1 mrg any of the statements known to have undefined effect on the last 4227 1.1 mrg iteration. */ 4228 1.1 mrg queue.safe_push (loop->header); 4229 1.1 mrg visited = BITMAP_ALLOC (NULL); 4230 1.1 mrg bitmap_set_bit (visited, loop->header->index); 4231 1.1 mrg found_exit = false; 4232 1.1 mrg 4233 1.1 mrg do 4234 1.1 mrg { 4235 1.1 mrg basic_block bb = queue.pop (); 4236 1.1 mrg gimple_stmt_iterator gsi; 4237 1.1 mrg bool stmt_found = false; 4238 1.1 mrg 4239 1.1 mrg /* Loop for possible exits and statements bounding the execution. */ 4240 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 4241 1.1 mrg { 4242 1.1 mrg gimple *stmt = gsi_stmt (gsi); 4243 1.1 mrg if (not_executed_last_iteration->contains (stmt)) 4244 1.1 mrg { 4245 1.1 mrg stmt_found = true; 4246 1.1 mrg break; 4247 1.1 mrg } 4248 1.1 mrg if (gimple_has_side_effects (stmt)) 4249 1.1 mrg { 4250 1.1 mrg found_exit = true; 4251 1.1 mrg break; 4252 1.1 mrg } 4253 1.1 mrg } 4254 1.1 mrg if (found_exit) 4255 1.1 mrg break; 4256 1.1 mrg 4257 1.1 mrg /* If no bounding statement is found, continue the walk. */ 4258 1.1 mrg if (!stmt_found) 4259 1.1 mrg { 4260 1.1 mrg edge e; 4261 1.1 mrg edge_iterator ei; 4262 1.1 mrg 4263 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 4264 1.1 mrg { 4265 1.1 mrg if (loop_exit_edge_p (loop, e) 4266 1.1 mrg || e == loop_latch_edge (loop)) 4267 1.1 mrg { 4268 1.1 mrg found_exit = true; 4269 1.1 mrg break; 4270 1.1 mrg } 4271 1.1 mrg if (bitmap_set_bit (visited, e->dest->index)) 4272 1.1 mrg queue.safe_push (e->dest); 4273 1.1 mrg } 4274 1.1 mrg } 4275 1.1 mrg } 4276 1.1 mrg while (queue.length () && !found_exit); 4277 1.1 mrg 4278 1.1 mrg /* If every path through the loop reach bounding statement before exit, 4279 1.1 mrg then we know the last iteration of the loop will have undefined effect 4280 1.1 mrg and we can decrease number of iterations. */ 4281 1.1 mrg 4282 1.1 mrg if (!found_exit) 4283 1.1 mrg { 4284 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 4285 1.1 mrg fprintf (dump_file, "Reducing loop iteration estimate by 1; " 4286 1.1 mrg "undefined statement must be executed at the last iteration.\n"); 4287 1.1 mrg record_niter_bound (loop, loop->nb_iterations_upper_bound - 1, 4288 1.1 mrg false, true); 4289 1.1 mrg } 4290 1.1 mrg 4291 1.1 mrg BITMAP_FREE (visited); 4292 1.1 mrg delete not_executed_last_iteration; 4293 1.1 mrg } 4294 1.1 mrg 4295 1.1 mrg /* Get expected upper bound for number of loop iterations for 4296 1.1 mrg BUILT_IN_EXPECT_WITH_PROBABILITY for a condition COND. */ 4297 1.1 mrg 4298 1.1 mrg static tree 4299 1.1 mrg get_upper_bound_based_on_builtin_expr_with_prob (gcond *cond) 4300 1.1 mrg { 4301 1.1 mrg if (cond == NULL) 4302 1.1 mrg return NULL_TREE; 4303 1.1 mrg 4304 1.1 mrg tree lhs = gimple_cond_lhs (cond); 4305 1.1 mrg if (TREE_CODE (lhs) != SSA_NAME) 4306 1.1 mrg return NULL_TREE; 4307 1.1 mrg 4308 1.1 mrg gimple *stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)); 4309 1.1 mrg gcall *def = dyn_cast<gcall *> (stmt); 4310 1.1 mrg if (def == NULL) 4311 1.1 mrg return NULL_TREE; 4312 1.1 mrg 4313 1.1 mrg tree decl = gimple_call_fndecl (def); 4314 1.1 mrg if (!decl 4315 1.1 mrg || !fndecl_built_in_p (decl, BUILT_IN_EXPECT_WITH_PROBABILITY) 4316 1.1 mrg || gimple_call_num_args (stmt) != 3) 4317 1.1 mrg return NULL_TREE; 4318 1.1 mrg 4319 1.1 mrg tree c = gimple_call_arg (def, 1); 4320 1.1 mrg tree condt = TREE_TYPE (lhs); 4321 1.1 mrg tree res = fold_build2 (gimple_cond_code (cond), 4322 1.1 mrg condt, c, 4323 1.1 mrg gimple_cond_rhs (cond)); 4324 1.1 mrg if (TREE_CODE (res) != INTEGER_CST) 4325 1.1 mrg return NULL_TREE; 4326 1.1 mrg 4327 1.1 mrg 4328 1.1 mrg tree prob = gimple_call_arg (def, 2); 4329 1.1 mrg tree t = TREE_TYPE (prob); 4330 1.1 mrg tree one 4331 1.1 mrg = build_real_from_int_cst (t, 4332 1.1 mrg integer_one_node); 4333 1.1 mrg if (integer_zerop (res)) 4334 1.1 mrg prob = fold_build2 (MINUS_EXPR, t, one, prob); 4335 1.1 mrg tree r = fold_build2 (RDIV_EXPR, t, one, prob); 4336 1.1 mrg if (TREE_CODE (r) != REAL_CST) 4337 1.1 mrg return NULL_TREE; 4338 1.1 mrg 4339 1.1 mrg HOST_WIDE_INT probi 4340 1.1 mrg = real_to_integer (TREE_REAL_CST_PTR (r)); 4341 1.1 mrg return build_int_cst (condt, probi); 4342 1.1 mrg } 4343 1.1 mrg 4344 1.1 mrg /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P 4345 1.1 mrg is true also use estimates derived from undefined behavior. */ 4346 1.1 mrg 4347 1.1 mrg void 4348 1.1 mrg estimate_numbers_of_iterations (class loop *loop) 4349 1.1 mrg { 4350 1.1 mrg tree niter, type; 4351 1.1 mrg unsigned i; 4352 1.1 mrg class tree_niter_desc niter_desc; 4353 1.1 mrg edge ex; 4354 1.1 mrg widest_int bound; 4355 1.1 mrg edge likely_exit; 4356 1.1 mrg 4357 1.1 mrg /* Give up if we already have tried to compute an estimation. */ 4358 1.1 mrg if (loop->estimate_state != EST_NOT_COMPUTED) 4359 1.1 mrg return; 4360 1.1 mrg 4361 1.1 mrg if (dump_file && (dump_flags & TDF_DETAILS)) 4362 1.1 mrg fprintf (dump_file, "Estimating # of iterations of loop %d\n", loop->num); 4363 1.1 mrg 4364 1.1 mrg loop->estimate_state = EST_AVAILABLE; 4365 1.1 mrg 4366 1.1 mrg /* If we have a measured profile, use it to estimate the number of 4367 1.1 mrg iterations. Normally this is recorded by branch_prob right after 4368 1.1 mrg reading the profile. In case we however found a new loop, record the 4369 1.1 mrg information here. 4370 1.1 mrg 4371 1.1 mrg Explicitly check for profile status so we do not report 4372 1.1 mrg wrong prediction hitrates for guessed loop iterations heuristics. 4373 1.1 mrg Do not recompute already recorded bounds - we ought to be better on 4374 1.1 mrg updating iteration bounds than updating profile in general and thus 4375 1.1 mrg recomputing iteration bounds later in the compilation process will just 4376 1.1 mrg introduce random roundoff errors. */ 4377 1.1 mrg if (!loop->any_estimate 4378 1.1 mrg && loop->header->count.reliable_p ()) 4379 1.1 mrg { 4380 1.1 mrg gcov_type nit = expected_loop_iterations_unbounded (loop); 4381 1.1 mrg bound = gcov_type_to_wide_int (nit); 4382 1.1 mrg record_niter_bound (loop, bound, true, false); 4383 1.1 mrg } 4384 1.1 mrg 4385 1.1 mrg /* Ensure that loop->nb_iterations is computed if possible. If it turns out 4386 1.1 mrg to be constant, we avoid undefined behavior implied bounds and instead 4387 1.1 mrg diagnose those loops with -Waggressive-loop-optimizations. */ 4388 1.1 mrg number_of_latch_executions (loop); 4389 1.1 mrg 4390 1.1 mrg basic_block *body = get_loop_body (loop); 4391 1.1 mrg auto_vec<edge> exits = get_loop_exit_edges (loop, body); 4392 1.1 mrg likely_exit = single_likely_exit (loop, exits); 4393 1.1 mrg FOR_EACH_VEC_ELT (exits, i, ex) 4394 1.1 mrg { 4395 1.1 mrg if (ex == likely_exit) 4396 1.1 mrg { 4397 1.1 mrg gimple *stmt = last_stmt (ex->src); 4398 1.1 mrg if (stmt != NULL) 4399 1.1 mrg { 4400 1.1 mrg gcond *cond = dyn_cast<gcond *> (stmt); 4401 1.1 mrg tree niter_bound 4402 1.1 mrg = get_upper_bound_based_on_builtin_expr_with_prob (cond); 4403 1.1 mrg if (niter_bound != NULL_TREE) 4404 1.1 mrg { 4405 1.1 mrg widest_int max = derive_constant_upper_bound (niter_bound); 4406 1.1 mrg record_estimate (loop, niter_bound, max, cond, 4407 1.1 mrg true, true, false); 4408 1.1 mrg } 4409 1.1 mrg } 4410 1.1 mrg } 4411 1.1 mrg 4412 1.1 mrg if (!number_of_iterations_exit (loop, ex, &niter_desc, 4413 1.1 mrg false, false, body)) 4414 1.1 mrg continue; 4415 1.1 mrg 4416 1.1 mrg niter = niter_desc.niter; 4417 1.1 mrg type = TREE_TYPE (niter); 4418 1.1 mrg if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST) 4419 1.1 mrg niter = build3 (COND_EXPR, type, niter_desc.may_be_zero, 4420 1.1 mrg build_int_cst (type, 0), 4421 1.1 mrg niter); 4422 1.1 mrg record_estimate (loop, niter, niter_desc.max, 4423 1.1 mrg last_stmt (ex->src), 4424 1.1 mrg true, ex == likely_exit, true); 4425 1.1 mrg record_control_iv (loop, &niter_desc); 4426 1.1 mrg } 4427 1.1 mrg 4428 1.1 mrg if (flag_aggressive_loop_optimizations) 4429 1.1 mrg infer_loop_bounds_from_undefined (loop, body); 4430 1.1 mrg free (body); 4431 1.1 mrg 4432 1.1 mrg discover_iteration_bound_by_body_walk (loop); 4433 1.1 mrg 4434 1.1 mrg maybe_lower_iteration_bound (loop); 4435 1.1 mrg 4436 1.1 mrg /* If we know the exact number of iterations of this loop, try to 4437 1.1 mrg not break code with undefined behavior by not recording smaller 4438 1.1 mrg maximum number of iterations. */ 4439 1.1 mrg if (loop->nb_iterations 4440 1.1 mrg && TREE_CODE (loop->nb_iterations) == INTEGER_CST) 4441 1.1 mrg { 4442 1.1 mrg loop->any_upper_bound = true; 4443 1.1 mrg loop->nb_iterations_upper_bound = wi::to_widest (loop->nb_iterations); 4444 1.1 mrg } 4445 1.1 mrg } 4446 1.1 mrg 4447 1.1 mrg /* Sets NIT to the estimated number of executions of the latch of the 4448 1.1 mrg LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as 4449 1.1 mrg large as the number of iterations. If we have no reliable estimate, 4450 1.1 mrg the function returns false, otherwise returns true. */ 4451 1.1 mrg 4452 1.1 mrg bool 4453 1.1 mrg estimated_loop_iterations (class loop *loop, widest_int *nit) 4454 1.1 mrg { 4455 1.1 mrg /* When SCEV information is available, try to update loop iterations 4456 1.1 mrg estimate. Otherwise just return whatever we recorded earlier. */ 4457 1.1 mrg if (scev_initialized_p ()) 4458 1.1 mrg estimate_numbers_of_iterations (loop); 4459 1.1 mrg 4460 1.1 mrg return (get_estimated_loop_iterations (loop, nit)); 4461 1.1 mrg } 4462 1.1 mrg 4463 1.1 mrg /* Similar to estimated_loop_iterations, but returns the estimate only 4464 1.1 mrg if it fits to HOST_WIDE_INT. If this is not the case, or the estimate 4465 1.1 mrg on the number of iterations of LOOP could not be derived, returns -1. */ 4466 1.1 mrg 4467 1.1 mrg HOST_WIDE_INT 4468 1.1 mrg estimated_loop_iterations_int (class loop *loop) 4469 1.1 mrg { 4470 1.1 mrg widest_int nit; 4471 1.1 mrg HOST_WIDE_INT hwi_nit; 4472 1.1 mrg 4473 1.1 mrg if (!estimated_loop_iterations (loop, &nit)) 4474 1.1 mrg return -1; 4475 1.1 mrg 4476 1.1 mrg if (!wi::fits_shwi_p (nit)) 4477 1.1 mrg return -1; 4478 1.1 mrg hwi_nit = nit.to_shwi (); 4479 1.1 mrg 4480 1.1 mrg return hwi_nit < 0 ? -1 : hwi_nit; 4481 1.1 mrg } 4482 1.1 mrg 4483 1.1 mrg 4484 1.1 mrg /* Sets NIT to an upper bound for the maximum number of executions of the 4485 1.1 mrg latch of the LOOP. If we have no reliable estimate, the function returns 4486 1.1 mrg false, otherwise returns true. */ 4487 1.1 mrg 4488 1.1 mrg bool 4489 1.1 mrg max_loop_iterations (class loop *loop, widest_int *nit) 4490 1.1 mrg { 4491 1.1 mrg /* When SCEV information is available, try to update loop iterations 4492 1.1 mrg estimate. Otherwise just return whatever we recorded earlier. */ 4493 1.1 mrg if (scev_initialized_p ()) 4494 1.1 mrg estimate_numbers_of_iterations (loop); 4495 1.1 mrg 4496 1.1 mrg return get_max_loop_iterations (loop, nit); 4497 1.1 mrg } 4498 1.1 mrg 4499 1.1 mrg /* Similar to max_loop_iterations, but returns the estimate only 4500 1.1 mrg if it fits to HOST_WIDE_INT. If this is not the case, or the estimate 4501 1.1 mrg on the number of iterations of LOOP could not be derived, returns -1. */ 4502 1.1 mrg 4503 1.1 mrg HOST_WIDE_INT 4504 1.1 mrg max_loop_iterations_int (class loop *loop) 4505 1.1 mrg { 4506 1.1 mrg widest_int nit; 4507 1.1 mrg HOST_WIDE_INT hwi_nit; 4508 1.1 mrg 4509 1.1 mrg if (!max_loop_iterations (loop, &nit)) 4510 1.1 mrg return -1; 4511 1.1 mrg 4512 1.1 mrg if (!wi::fits_shwi_p (nit)) 4513 1.1 mrg return -1; 4514 1.1 mrg hwi_nit = nit.to_shwi (); 4515 1.1 mrg 4516 1.1 mrg return hwi_nit < 0 ? -1 : hwi_nit; 4517 1.1 mrg } 4518 1.1 mrg 4519 1.1 mrg /* Sets NIT to an likely upper bound for the maximum number of executions of the 4520 1.1 mrg latch of the LOOP. If we have no reliable estimate, the function returns 4521 1.1 mrg false, otherwise returns true. */ 4522 1.1 mrg 4523 1.1 mrg bool 4524 1.1 mrg likely_max_loop_iterations (class loop *loop, widest_int *nit) 4525 1.1 mrg { 4526 1.1 mrg /* When SCEV information is available, try to update loop iterations 4527 1.1 mrg estimate. Otherwise just return whatever we recorded earlier. */ 4528 1.1 mrg if (scev_initialized_p ()) 4529 1.1 mrg estimate_numbers_of_iterations (loop); 4530 1.1 mrg 4531 1.1 mrg return get_likely_max_loop_iterations (loop, nit); 4532 1.1 mrg } 4533 1.1 mrg 4534 1.1 mrg /* Similar to max_loop_iterations, but returns the estimate only 4535 1.1 mrg if it fits to HOST_WIDE_INT. If this is not the case, or the estimate 4536 1.1 mrg on the number of iterations of LOOP could not be derived, returns -1. */ 4537 1.1 mrg 4538 1.1 mrg HOST_WIDE_INT 4539 1.1 mrg likely_max_loop_iterations_int (class loop *loop) 4540 1.1 mrg { 4541 1.1 mrg widest_int nit; 4542 1.1 mrg HOST_WIDE_INT hwi_nit; 4543 1.1 mrg 4544 1.1 mrg if (!likely_max_loop_iterations (loop, &nit)) 4545 1.1 mrg return -1; 4546 1.1 mrg 4547 1.1 mrg if (!wi::fits_shwi_p (nit)) 4548 1.1 mrg return -1; 4549 1.1 mrg hwi_nit = nit.to_shwi (); 4550 1.1 mrg 4551 1.1 mrg return hwi_nit < 0 ? -1 : hwi_nit; 4552 1.1 mrg } 4553 1.1 mrg 4554 1.1 mrg /* Returns an estimate for the number of executions of statements 4555 1.1 mrg in the LOOP. For statements before the loop exit, this exceeds 4556 1.1 mrg the number of execution of the latch by one. */ 4557 1.1 mrg 4558 1.1 mrg HOST_WIDE_INT 4559 1.1 mrg estimated_stmt_executions_int (class loop *loop) 4560 1.1 mrg { 4561 1.1 mrg HOST_WIDE_INT nit = estimated_loop_iterations_int (loop); 4562 1.1 mrg HOST_WIDE_INT snit; 4563 1.1 mrg 4564 1.1 mrg if (nit == -1) 4565 1.1 mrg return -1; 4566 1.1 mrg 4567 1.1 mrg snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1); 4568 1.1 mrg 4569 1.1 mrg /* If the computation overflows, return -1. */ 4570 1.1 mrg return snit < 0 ? -1 : snit; 4571 1.1 mrg } 4572 1.1 mrg 4573 1.1 mrg /* Sets NIT to the maximum number of executions of the latch of the 4574 1.1 mrg LOOP, plus one. If we have no reliable estimate, the function returns 4575 1.1 mrg false, otherwise returns true. */ 4576 1.1 mrg 4577 1.1 mrg bool 4578 1.1 mrg max_stmt_executions (class loop *loop, widest_int *nit) 4579 1.1 mrg { 4580 1.1 mrg widest_int nit_minus_one; 4581 1.1 mrg 4582 1.1 mrg if (!max_loop_iterations (loop, nit)) 4583 1.1 mrg return false; 4584 1.1 mrg 4585 1.1 mrg nit_minus_one = *nit; 4586 1.1 mrg 4587 1.1 mrg *nit += 1; 4588 1.1 mrg 4589 1.1 mrg return wi::gtu_p (*nit, nit_minus_one); 4590 1.1 mrg } 4591 1.1 mrg 4592 1.1 mrg /* Sets NIT to the estimated maximum number of executions of the latch of the 4593 1.1 mrg LOOP, plus one. If we have no likely estimate, the function returns 4594 1.1 mrg false, otherwise returns true. */ 4595 1.1 mrg 4596 1.1 mrg bool 4597 1.1 mrg likely_max_stmt_executions (class loop *loop, widest_int *nit) 4598 1.1 mrg { 4599 1.1 mrg widest_int nit_minus_one; 4600 1.1 mrg 4601 1.1 mrg if (!likely_max_loop_iterations (loop, nit)) 4602 1.1 mrg return false; 4603 1.1 mrg 4604 1.1 mrg nit_minus_one = *nit; 4605 1.1 mrg 4606 1.1 mrg *nit += 1; 4607 1.1 mrg 4608 1.1 mrg return wi::gtu_p (*nit, nit_minus_one); 4609 1.1 mrg } 4610 1.1 mrg 4611 1.1 mrg /* Sets NIT to the estimated number of executions of the latch of the 4612 1.1 mrg LOOP, plus one. If we have no reliable estimate, the function returns 4613 1.1 mrg false, otherwise returns true. */ 4614 1.1 mrg 4615 1.1 mrg bool 4616 1.1 mrg estimated_stmt_executions (class loop *loop, widest_int *nit) 4617 1.1 mrg { 4618 1.1 mrg widest_int nit_minus_one; 4619 1.1 mrg 4620 1.1 mrg if (!estimated_loop_iterations (loop, nit)) 4621 1.1 mrg return false; 4622 1.1 mrg 4623 1.1 mrg nit_minus_one = *nit; 4624 1.1 mrg 4625 1.1 mrg *nit += 1; 4626 1.1 mrg 4627 1.1 mrg return wi::gtu_p (*nit, nit_minus_one); 4628 1.1 mrg } 4629 1.1 mrg 4630 1.1 mrg /* Records estimates on numbers of iterations of loops. */ 4631 1.1 mrg 4632 1.1 mrg void 4633 1.1 mrg estimate_numbers_of_iterations (function *fn) 4634 1.1 mrg { 4635 1.1 mrg /* We don't want to issue signed overflow warnings while getting 4636 1.1 mrg loop iteration estimates. */ 4637 1.1 mrg fold_defer_overflow_warnings (); 4638 1.1 mrg 4639 1.1 mrg for (auto loop : loops_list (fn, 0)) 4640 1.1 mrg estimate_numbers_of_iterations (loop); 4641 1.1 mrg 4642 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 4643 1.1 mrg } 4644 1.1 mrg 4645 1.1 mrg /* Returns true if statement S1 dominates statement S2. */ 4646 1.1 mrg 4647 1.1 mrg bool 4648 1.1 mrg stmt_dominates_stmt_p (gimple *s1, gimple *s2) 4649 1.1 mrg { 4650 1.1 mrg basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2); 4651 1.1 mrg 4652 1.1 mrg if (!bb1 4653 1.1 mrg || s1 == s2) 4654 1.1 mrg return true; 4655 1.1 mrg 4656 1.1 mrg if (bb1 == bb2) 4657 1.1 mrg { 4658 1.1 mrg gimple_stmt_iterator bsi; 4659 1.1 mrg 4660 1.1 mrg if (gimple_code (s2) == GIMPLE_PHI) 4661 1.1 mrg return false; 4662 1.1 mrg 4663 1.1 mrg if (gimple_code (s1) == GIMPLE_PHI) 4664 1.1 mrg return true; 4665 1.1 mrg 4666 1.1 mrg for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi)) 4667 1.1 mrg if (gsi_stmt (bsi) == s1) 4668 1.1 mrg return true; 4669 1.1 mrg 4670 1.1 mrg return false; 4671 1.1 mrg } 4672 1.1 mrg 4673 1.1 mrg return dominated_by_p (CDI_DOMINATORS, bb2, bb1); 4674 1.1 mrg } 4675 1.1 mrg 4676 1.1 mrg /* Returns true when we can prove that the number of executions of 4677 1.1 mrg STMT in the loop is at most NITER, according to the bound on 4678 1.1 mrg the number of executions of the statement NITER_BOUND->stmt recorded in 4679 1.1 mrg NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT. 4680 1.1 mrg 4681 1.1 mrg ??? This code can become quite a CPU hog - we can have many bounds, 4682 1.1 mrg and large basic block forcing stmt_dominates_stmt_p to be queried 4683 1.1 mrg many times on a large basic blocks, so the whole thing is O(n^2) 4684 1.1 mrg for scev_probably_wraps_p invocation (that can be done n times). 4685 1.1 mrg 4686 1.1 mrg It would make more sense (and give better answers) to remember BB 4687 1.1 mrg bounds computed by discover_iteration_bound_by_body_walk. */ 4688 1.1 mrg 4689 1.1 mrg static bool 4690 1.1 mrg n_of_executions_at_most (gimple *stmt, 4691 1.1 mrg class nb_iter_bound *niter_bound, 4692 1.1 mrg tree niter) 4693 1.1 mrg { 4694 1.1 mrg widest_int bound = niter_bound->bound; 4695 1.1 mrg tree nit_type = TREE_TYPE (niter), e; 4696 1.1 mrg enum tree_code cmp; 4697 1.1 mrg 4698 1.1 mrg gcc_assert (TYPE_UNSIGNED (nit_type)); 4699 1.1 mrg 4700 1.1 mrg /* If the bound does not even fit into NIT_TYPE, it cannot tell us that 4701 1.1 mrg the number of iterations is small. */ 4702 1.1 mrg if (!wi::fits_to_tree_p (bound, nit_type)) 4703 1.1 mrg return false; 4704 1.1 mrg 4705 1.1 mrg /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1 4706 1.1 mrg times. This means that: 4707 1.1 mrg 4708 1.1 mrg -- if NITER_BOUND->is_exit is true, then everything after 4709 1.1 mrg it at most NITER_BOUND->bound times. 4710 1.1 mrg 4711 1.1 mrg -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT 4712 1.1 mrg is executed, then NITER_BOUND->stmt is executed as well in the same 4713 1.1 mrg iteration then STMT is executed at most NITER_BOUND->bound + 1 times. 4714 1.1 mrg 4715 1.1 mrg If we can determine that NITER_BOUND->stmt is always executed 4716 1.1 mrg after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times. 4717 1.1 mrg We conclude that if both statements belong to the same 4718 1.1 mrg basic block and STMT is before NITER_BOUND->stmt and there are no 4719 1.1 mrg statements with side effects in between. */ 4720 1.1 mrg 4721 1.1 mrg if (niter_bound->is_exit) 4722 1.1 mrg { 4723 1.1 mrg if (stmt == niter_bound->stmt 4724 1.1 mrg || !stmt_dominates_stmt_p (niter_bound->stmt, stmt)) 4725 1.1 mrg return false; 4726 1.1 mrg cmp = GE_EXPR; 4727 1.1 mrg } 4728 1.1 mrg else 4729 1.1 mrg { 4730 1.1 mrg if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt)) 4731 1.1 mrg { 4732 1.1 mrg gimple_stmt_iterator bsi; 4733 1.1 mrg if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt) 4734 1.1 mrg || gimple_code (stmt) == GIMPLE_PHI 4735 1.1 mrg || gimple_code (niter_bound->stmt) == GIMPLE_PHI) 4736 1.1 mrg return false; 4737 1.1 mrg 4738 1.1 mrg /* By stmt_dominates_stmt_p we already know that STMT appears 4739 1.1 mrg before NITER_BOUND->STMT. Still need to test that the loop 4740 1.1 mrg cannot be terinated by a side effect in between. */ 4741 1.1 mrg for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt; 4742 1.1 mrg gsi_next (&bsi)) 4743 1.1 mrg if (gimple_has_side_effects (gsi_stmt (bsi))) 4744 1.1 mrg return false; 4745 1.1 mrg bound += 1; 4746 1.1 mrg if (bound == 0 4747 1.1 mrg || !wi::fits_to_tree_p (bound, nit_type)) 4748 1.1 mrg return false; 4749 1.1 mrg } 4750 1.1 mrg cmp = GT_EXPR; 4751 1.1 mrg } 4752 1.1 mrg 4753 1.1 mrg e = fold_binary (cmp, boolean_type_node, 4754 1.1 mrg niter, wide_int_to_tree (nit_type, bound)); 4755 1.1 mrg return e && integer_nonzerop (e); 4756 1.1 mrg } 4757 1.1 mrg 4758 1.1 mrg /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */ 4759 1.1 mrg 4760 1.1 mrg bool 4761 1.1 mrg nowrap_type_p (tree type) 4762 1.1 mrg { 4763 1.1 mrg if (ANY_INTEGRAL_TYPE_P (type) 4764 1.1 mrg && TYPE_OVERFLOW_UNDEFINED (type)) 4765 1.1 mrg return true; 4766 1.1 mrg 4767 1.1 mrg if (POINTER_TYPE_P (type)) 4768 1.1 mrg return true; 4769 1.1 mrg 4770 1.1 mrg return false; 4771 1.1 mrg } 4772 1.1 mrg 4773 1.1 mrg /* Return true if we can prove LOOP is exited before evolution of induction 4774 1.1 mrg variable {BASE, STEP} overflows with respect to its type bound. */ 4775 1.1 mrg 4776 1.1 mrg static bool 4777 1.1 mrg loop_exits_before_overflow (tree base, tree step, 4778 1.1 mrg gimple *at_stmt, class loop *loop) 4779 1.1 mrg { 4780 1.1 mrg widest_int niter; 4781 1.1 mrg struct control_iv *civ; 4782 1.1 mrg class nb_iter_bound *bound; 4783 1.1 mrg tree e, delta, step_abs, unsigned_base; 4784 1.1 mrg tree type = TREE_TYPE (step); 4785 1.1 mrg tree unsigned_type, valid_niter; 4786 1.1 mrg 4787 1.1 mrg /* Don't issue signed overflow warnings. */ 4788 1.1 mrg fold_defer_overflow_warnings (); 4789 1.1 mrg 4790 1.1 mrg /* Compute the number of iterations before we reach the bound of the 4791 1.1 mrg type, and verify that the loop is exited before this occurs. */ 4792 1.1 mrg unsigned_type = unsigned_type_for (type); 4793 1.1 mrg unsigned_base = fold_convert (unsigned_type, base); 4794 1.1 mrg 4795 1.1 mrg if (tree_int_cst_sign_bit (step)) 4796 1.1 mrg { 4797 1.1 mrg tree extreme = fold_convert (unsigned_type, 4798 1.1 mrg lower_bound_in_type (type, type)); 4799 1.1 mrg delta = fold_build2 (MINUS_EXPR, unsigned_type, unsigned_base, extreme); 4800 1.1 mrg step_abs = fold_build1 (NEGATE_EXPR, unsigned_type, 4801 1.1 mrg fold_convert (unsigned_type, step)); 4802 1.1 mrg } 4803 1.1 mrg else 4804 1.1 mrg { 4805 1.1 mrg tree extreme = fold_convert (unsigned_type, 4806 1.1 mrg upper_bound_in_type (type, type)); 4807 1.1 mrg delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, unsigned_base); 4808 1.1 mrg step_abs = fold_convert (unsigned_type, step); 4809 1.1 mrg } 4810 1.1 mrg 4811 1.1 mrg valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs); 4812 1.1 mrg 4813 1.1 mrg estimate_numbers_of_iterations (loop); 4814 1.1 mrg 4815 1.1 mrg if (max_loop_iterations (loop, &niter) 4816 1.1 mrg && wi::fits_to_tree_p (niter, TREE_TYPE (valid_niter)) 4817 1.1 mrg && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter, 4818 1.1 mrg wide_int_to_tree (TREE_TYPE (valid_niter), 4819 1.1 mrg niter))) != NULL 4820 1.1 mrg && integer_nonzerop (e)) 4821 1.1 mrg { 4822 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 4823 1.1 mrg return true; 4824 1.1 mrg } 4825 1.1 mrg if (at_stmt) 4826 1.1 mrg for (bound = loop->bounds; bound; bound = bound->next) 4827 1.1 mrg { 4828 1.1 mrg if (n_of_executions_at_most (at_stmt, bound, valid_niter)) 4829 1.1 mrg { 4830 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 4831 1.1 mrg return true; 4832 1.1 mrg } 4833 1.1 mrg } 4834 1.1 mrg fold_undefer_and_ignore_overflow_warnings (); 4835 1.1 mrg 4836 1.1 mrg /* Try to prove loop is exited before {base, step} overflows with the 4837 1.1 mrg help of analyzed loop control IV. This is done only for IVs with 4838 1.1 mrg constant step because otherwise we don't have the information. */ 4839 1.1 mrg if (TREE_CODE (step) == INTEGER_CST) 4840 1.1 mrg { 4841 1.1 mrg for (civ = loop->control_ivs; civ; civ = civ->next) 4842 1.1 mrg { 4843 1.1 mrg enum tree_code code; 4844 1.1 mrg tree civ_type = TREE_TYPE (civ->step); 4845 1.1 mrg 4846 1.1 mrg /* Have to consider type difference because operand_equal_p ignores 4847 1.1 mrg that for constants. */ 4848 1.1 mrg if (TYPE_UNSIGNED (type) != TYPE_UNSIGNED (civ_type) 4849 1.1 mrg || element_precision (type) != element_precision (civ_type)) 4850 1.1 mrg continue; 4851 1.1 mrg 4852 1.1 mrg /* Only consider control IV with same step. */ 4853 1.1 mrg if (!operand_equal_p (step, civ->step, 0)) 4854 1.1 mrg continue; 4855 1.1 mrg 4856 1.1 mrg /* Done proving if this is a no-overflow control IV. */ 4857 1.1 mrg if (operand_equal_p (base, civ->base, 0)) 4858 1.1 mrg return true; 4859 1.1 mrg 4860 1.1 mrg /* Control IV is recorded after expanding simple operations, 4861 1.1 mrg Here we expand base and compare it too. */ 4862 1.1 mrg tree expanded_base = expand_simple_operations (base); 4863 1.1 mrg if (operand_equal_p (expanded_base, civ->base, 0)) 4864 1.1 mrg return true; 4865 1.1 mrg 4866 1.1 mrg /* If this is a before stepping control IV, in other words, we have 4867 1.1 mrg 4868 1.1 mrg {civ_base, step} = {base + step, step} 4869 1.1 mrg 4870 1.1 mrg Because civ {base + step, step} doesn't overflow during loop 4871 1.1 mrg iterations, {base, step} will not overflow if we can prove the 4872 1.1 mrg operation "base + step" does not overflow. Specifically, we try 4873 1.1 mrg to prove below conditions are satisfied: 4874 1.1 mrg 4875 1.1 mrg base <= UPPER_BOUND (type) - step ;;step > 0 4876 1.1 mrg base >= LOWER_BOUND (type) - step ;;step < 0 4877 1.1 mrg 4878 1.1 mrg by proving the reverse conditions are false using loop's initial 4879 1.1 mrg condition. */ 4880 1.1 mrg if (POINTER_TYPE_P (TREE_TYPE (base))) 4881 1.1 mrg code = POINTER_PLUS_EXPR; 4882 1.1 mrg else 4883 1.1 mrg code = PLUS_EXPR; 4884 1.1 mrg 4885 1.1 mrg tree stepped = fold_build2 (code, TREE_TYPE (base), base, step); 4886 1.1 mrg tree expanded_stepped = fold_build2 (code, TREE_TYPE (base), 4887 1.1 mrg expanded_base, step); 4888 1.1 mrg if (operand_equal_p (stepped, civ->base, 0) 4889 1.1 mrg || operand_equal_p (expanded_stepped, civ->base, 0)) 4890 1.1 mrg { 4891 1.1 mrg tree extreme; 4892 1.1 mrg 4893 1.1 mrg if (tree_int_cst_sign_bit (step)) 4894 1.1 mrg { 4895 1.1 mrg code = LT_EXPR; 4896 1.1 mrg extreme = lower_bound_in_type (type, type); 4897 1.1 mrg } 4898 1.1 mrg else 4899 1.1 mrg { 4900 1.1 mrg code = GT_EXPR; 4901 1.1 mrg extreme = upper_bound_in_type (type, type); 4902 1.1 mrg } 4903 1.1 mrg extreme = fold_build2 (MINUS_EXPR, type, extreme, step); 4904 1.1 mrg e = fold_build2 (code, boolean_type_node, base, extreme); 4905 1.1 mrg e = simplify_using_initial_conditions (loop, e); 4906 1.1 mrg if (integer_zerop (e)) 4907 1.1 mrg return true; 4908 1.1 mrg } 4909 1.1 mrg } 4910 1.1 mrg } 4911 1.1 mrg 4912 1.1 mrg return false; 4913 1.1 mrg } 4914 1.1 mrg 4915 1.1 mrg /* VAR is scev variable whose evolution part is constant STEP, this function 4916 1.1 mrg proves that VAR can't overflow by using value range info. If VAR's value 4917 1.1 mrg range is [MIN, MAX], it can be proven by: 4918 1.1 mrg MAX + step doesn't overflow ; if step > 0 4919 1.1 mrg or 4920 1.1 mrg MIN + step doesn't underflow ; if step < 0. 4921 1.1 mrg 4922 1.1 mrg We can only do this if var is computed in every loop iteration, i.e, var's 4923 1.1 mrg definition has to dominate loop latch. Consider below example: 4924 1.1 mrg 4925 1.1 mrg { 4926 1.1 mrg unsigned int i; 4927 1.1 mrg 4928 1.1 mrg <bb 3>: 4929 1.1 mrg 4930 1.1 mrg <bb 4>: 4931 1.1 mrg # RANGE [0, 4294967294] NONZERO 65535 4932 1.1 mrg # i_21 = PHI <0(3), i_18(9)> 4933 1.1 mrg if (i_21 != 0) 4934 1.1 mrg goto <bb 6>; 4935 1.1 mrg else 4936 1.1 mrg goto <bb 8>; 4937 1.1 mrg 4938 1.1 mrg <bb 6>: 4939 1.1 mrg # RANGE [0, 65533] NONZERO 65535 4940 1.1 mrg _6 = i_21 + 4294967295; 4941 1.1 mrg # RANGE [0, 65533] NONZERO 65535 4942 1.1 mrg _7 = (long unsigned int) _6; 4943 1.1 mrg # RANGE [0, 524264] NONZERO 524280 4944 1.1 mrg _8 = _7 * 8; 4945 1.1 mrg # PT = nonlocal escaped 4946 1.1 mrg _9 = a_14 + _8; 4947 1.1 mrg *_9 = 0; 4948 1.1 mrg 4949 1.1 mrg <bb 8>: 4950 1.1 mrg # RANGE [1, 65535] NONZERO 65535 4951 1.1 mrg i_18 = i_21 + 1; 4952 1.1 mrg if (i_18 >= 65535) 4953 1.1 mrg goto <bb 10>; 4954 1.1 mrg else 4955 1.1 mrg goto <bb 9>; 4956 1.1 mrg 4957 1.1 mrg <bb 9>: 4958 1.1 mrg goto <bb 4>; 4959 1.1 mrg 4960 1.1 mrg <bb 10>: 4961 1.1 mrg return; 4962 1.1 mrg } 4963 1.1 mrg 4964 1.1 mrg VAR _6 doesn't overflow only with pre-condition (i_21 != 0), here we 4965 1.1 mrg can't use _6 to prove no-overlfow for _7. In fact, var _7 takes value 4966 1.1 mrg sequence (4294967295, 0, 1, ..., 65533) in loop life time, rather than 4967 1.1 mrg (4294967295, 4294967296, ...). */ 4968 1.1 mrg 4969 1.1 mrg static bool 4970 1.1 mrg scev_var_range_cant_overflow (tree var, tree step, class loop *loop) 4971 1.1 mrg { 4972 1.1 mrg tree type; 4973 1.1 mrg wide_int minv, maxv, diff, step_wi; 4974 1.1 mrg 4975 1.1 mrg if (TREE_CODE (step) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (var))) 4976 1.1 mrg return false; 4977 1.1 mrg 4978 1.1 mrg /* Check if VAR evaluates in every loop iteration. It's not the case 4979 1.1 mrg if VAR is default definition or does not dominate loop's latch. */ 4980 1.1 mrg basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (var)); 4981 1.1 mrg if (!def_bb || !dominated_by_p (CDI_DOMINATORS, loop->latch, def_bb)) 4982 1.1 mrg return false; 4983 1.1 mrg 4984 1.1 mrg value_range r; 4985 1.1 mrg get_range_query (cfun)->range_of_expr (r, var); 4986 1.1 mrg if (r.kind () != VR_RANGE) 4987 1.1 mrg return false; 4988 1.1 mrg 4989 1.1 mrg /* VAR is a scev whose evolution part is STEP and value range info 4990 1.1 mrg is [MIN, MAX], we can prove its no-overflowness by conditions: 4991 1.1 mrg 4992 1.1 mrg type_MAX - MAX >= step ; if step > 0 4993 1.1 mrg MIN - type_MIN >= |step| ; if step < 0. 4994 1.1 mrg 4995 1.1 mrg Or VAR must take value outside of value range, which is not true. */ 4996 1.1 mrg step_wi = wi::to_wide (step); 4997 1.1 mrg type = TREE_TYPE (var); 4998 1.1 mrg if (tree_int_cst_sign_bit (step)) 4999 1.1 mrg { 5000 1.1 mrg diff = r.lower_bound () - wi::to_wide (lower_bound_in_type (type, type)); 5001 1.1 mrg step_wi = - step_wi; 5002 1.1 mrg } 5003 1.1 mrg else 5004 1.1 mrg diff = wi::to_wide (upper_bound_in_type (type, type)) - r.upper_bound (); 5005 1.1 mrg 5006 1.1 mrg return (wi::geu_p (diff, step_wi)); 5007 1.1 mrg } 5008 1.1 mrg 5009 1.1 mrg /* Return false only when the induction variable BASE + STEP * I is 5010 1.1 mrg known to not overflow: i.e. when the number of iterations is small 5011 1.1 mrg enough with respect to the step and initial condition in order to 5012 1.1 mrg keep the evolution confined in TYPEs bounds. Return true when the 5013 1.1 mrg iv is known to overflow or when the property is not computable. 5014 1.1 mrg 5015 1.1 mrg USE_OVERFLOW_SEMANTICS is true if this function should assume that 5016 1.1 mrg the rules for overflow of the given language apply (e.g., that signed 5017 1.1 mrg arithmetics in C does not overflow). 5018 1.1 mrg 5019 1.1 mrg If VAR is a ssa variable, this function also returns false if VAR can 5020 1.1 mrg be proven not overflow with value range info. */ 5021 1.1 mrg 5022 1.1 mrg bool 5023 1.1 mrg scev_probably_wraps_p (tree var, tree base, tree step, 5024 1.1 mrg gimple *at_stmt, class loop *loop, 5025 1.1 mrg bool use_overflow_semantics) 5026 1.1 mrg { 5027 1.1 mrg /* FIXME: We really need something like 5028 1.1 mrg http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html. 5029 1.1 mrg 5030 1.1 mrg We used to test for the following situation that frequently appears 5031 1.1 mrg during address arithmetics: 5032 1.1 mrg 5033 1.1 mrg D.1621_13 = (long unsigned intD.4) D.1620_12; 5034 1.1 mrg D.1622_14 = D.1621_13 * 8; 5035 1.1 mrg D.1623_15 = (doubleD.29 *) D.1622_14; 5036 1.1 mrg 5037 1.1 mrg And derived that the sequence corresponding to D_14 5038 1.1 mrg can be proved to not wrap because it is used for computing a 5039 1.1 mrg memory access; however, this is not really the case -- for example, 5040 1.1 mrg if D_12 = (unsigned char) [254,+,1], then D_14 has values 5041 1.1 mrg 2032, 2040, 0, 8, ..., but the code is still legal. */ 5042 1.1 mrg 5043 1.1 mrg if (chrec_contains_undetermined (base) 5044 1.1 mrg || chrec_contains_undetermined (step)) 5045 1.1 mrg return true; 5046 1.1 mrg 5047 1.1 mrg if (integer_zerop (step)) 5048 1.1 mrg return false; 5049 1.1 mrg 5050 1.1 mrg /* If we can use the fact that signed and pointer arithmetics does not 5051 1.1 mrg wrap, we are done. */ 5052 1.1 mrg if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base))) 5053 1.1 mrg return false; 5054 1.1 mrg 5055 1.1 mrg /* To be able to use estimates on number of iterations of the loop, 5056 1.1 mrg we must have an upper bound on the absolute value of the step. */ 5057 1.1 mrg if (TREE_CODE (step) != INTEGER_CST) 5058 1.1 mrg return true; 5059 1.1 mrg 5060 1.1 mrg /* Check if var can be proven not overflow with value range info. */ 5061 1.1 mrg if (var && TREE_CODE (var) == SSA_NAME 5062 1.1 mrg && scev_var_range_cant_overflow (var, step, loop)) 5063 1.1 mrg return false; 5064 1.1 mrg 5065 1.1 mrg if (loop_exits_before_overflow (base, step, at_stmt, loop)) 5066 1.1 mrg return false; 5067 1.1 mrg 5068 1.1 mrg /* At this point we still don't have a proof that the iv does not 5069 1.1 mrg overflow: give up. */ 5070 1.1 mrg return true; 5071 1.1 mrg } 5072 1.1 mrg 5073 1.1 mrg /* Frees the information on upper bounds on numbers of iterations of LOOP. */ 5074 1.1 mrg 5075 1.1 mrg void 5076 1.1 mrg free_numbers_of_iterations_estimates (class loop *loop) 5077 1.1 mrg { 5078 1.1 mrg struct control_iv *civ; 5079 1.1 mrg class nb_iter_bound *bound; 5080 1.1 mrg 5081 1.1 mrg loop->nb_iterations = NULL; 5082 1.1 mrg loop->estimate_state = EST_NOT_COMPUTED; 5083 1.1 mrg for (bound = loop->bounds; bound;) 5084 1.1 mrg { 5085 1.1 mrg class nb_iter_bound *next = bound->next; 5086 1.1 mrg ggc_free (bound); 5087 1.1 mrg bound = next; 5088 1.1 mrg } 5089 1.1 mrg loop->bounds = NULL; 5090 1.1 mrg 5091 1.1 mrg for (civ = loop->control_ivs; civ;) 5092 1.1 mrg { 5093 1.1 mrg struct control_iv *next = civ->next; 5094 1.1 mrg ggc_free (civ); 5095 1.1 mrg civ = next; 5096 1.1 mrg } 5097 1.1 mrg loop->control_ivs = NULL; 5098 1.1 mrg } 5099 1.1 mrg 5100 1.1 mrg /* Frees the information on upper bounds on numbers of iterations of loops. */ 5101 1.1 mrg 5102 1.1 mrg void 5103 1.1 mrg free_numbers_of_iterations_estimates (function *fn) 5104 1.1 mrg { 5105 1.1 mrg for (auto loop : loops_list (fn, 0)) 5106 1.1 mrg free_numbers_of_iterations_estimates (loop); 5107 1.1 mrg } 5108 1.1 mrg 5109 1.1 mrg /* Substitute value VAL for ssa name NAME inside expressions held 5110 1.1 mrg at LOOP. */ 5111 1.1 mrg 5112 1.1 mrg void 5113 1.1 mrg substitute_in_loop_info (class loop *loop, tree name, tree val) 5114 1.1 mrg { 5115 1.1 mrg loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val); 5116 1.1 mrg } 5117