]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/loop-iv.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / loop-iv.c
CommitLineData
f9cce2dc 1/* Rtl-level induction variable analysis.
d353bf18 2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
48e1416a 3
f9cce2dc 4This file is part of GCC.
48e1416a 5
f9cce2dc 6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8c4c00c1 8Free Software Foundation; either version 3, or (at your option) any
f9cce2dc 9later version.
48e1416a 10
f9cce2dc 11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
48e1416a 15
f9cce2dc 16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
f9cce2dc 19
3f37d465 20/* This is a simple analysis of induction variables of the loop. The major use
21 is for determining the number of iterations of a loop for loop unrolling,
22 doloop optimization and branch prediction. The iv information is computed
23 on demand.
24
2e49e3bc 25 Induction variables are analyzed by walking the use-def chains. When
26 a basic induction variable (biv) is found, it is cached in the bivs
27 hash table. When register is proved to be a biv, its description
28 is stored to DF_REF_DATA of the def reference.
3f37d465 29
30 The analysis works always with one loop -- you must call
31 iv_analysis_loop_init (loop) for it. All the other functions then work with
32 this loop. When you need to work with another loop, just call
33 iv_analysis_loop_init for it. When you no longer need iv analysis, call
34 iv_analysis_done () to clean up the memory.
35
36 The available functions are:
48e1416a 37
3f37d465 38 iv_analyze (insn, reg, iv): Stores the description of the induction variable
39 corresponding to the use of register REG in INSN to IV. Returns true if
40 REG is an induction variable in INSN. false otherwise.
41 If use of REG is not found in INSN, following insns are scanned (so that
42 we may call this function on insn returned by get_condition).
43 iv_analyze_result (insn, def, iv): Stores to IV the description of the iv
44 corresponding to DEF, which is a register defined in INSN.
45 iv_analyze_expr (insn, rhs, mode, iv): Stores to IV the description of iv
46 corresponding to expression EXPR evaluated at INSN. All registers used bu
47 EXPR must also be used in INSN.
3072d30e 48*/
f9cce2dc 49
50#include "config.h"
51#include "system.h"
52#include "coretypes.h"
53#include "tm.h"
54#include "rtl.h"
55#include "hard-reg-set.h"
42fe97ed 56#include "obstack.h"
94ea8568 57#include "predict.h"
94ea8568 58#include "function.h"
59#include "dominance.h"
60#include "cfg.h"
f9cce2dc 61#include "basic-block.h"
62#include "cfgloop.h"
b20a8bb4 63#include "symtab.h"
d53441c8 64#include "flags.h"
d53441c8 65#include "alias.h"
d53441c8 66#include "tree.h"
67#include "insn-config.h"
68#include "expmed.h"
69#include "dojump.h"
70#include "explow.h"
71#include "calls.h"
72#include "emit-rtl.h"
73#include "varasm.h"
74#include "stmt.h"
f9cce2dc 75#include "expr.h"
7e3cc681 76#include "intl.h"
0b205f4c 77#include "diagnostic-core.h"
3f37d465 78#include "df.h"
b9ed1410 79#include "dumpfile.h"
b25bc675 80#include "rtl-iter.h"
f9cce2dc 81
3f37d465 82/* Possible return values of iv_get_reaching_def. */
f9cce2dc 83
3f37d465 84enum iv_grd_result
f9cce2dc 85{
3f37d465 86 /* More than one reaching def, or reaching def that does not
87 dominate the use. */
88 GRD_INVALID,
f9cce2dc 89
3f37d465 90 /* The use is trivial invariant of the loop, i.e. is not changed
91 inside the loop. */
92 GRD_INVARIANT,
f9cce2dc 93
3f37d465 94 /* The use is reached by initial value and a value from the
95 previous iteration. */
96 GRD_MAYBE_BIV,
97
98 /* The use has single dominating def. */
99 GRD_SINGLE_DOM
100};
101
102/* Information about a biv. */
103
104struct biv_entry
105{
106 unsigned regno; /* The register of the biv. */
107 struct rtx_iv iv; /* Value of the biv. */
f9cce2dc 108};
109
3072d30e 110static bool clean_slate = true;
111
112static unsigned int iv_ref_table_size = 0;
113
114/* Table of rtx_ivs indexed by the df_ref uid field. */
115static struct rtx_iv ** iv_ref_table;
116
3f37d465 117/* Induction variable stored at the reference. */
9af5ce0c 118#define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
119#define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
f9cce2dc 120
3f37d465 121/* The current loop. */
f9cce2dc 122
3f37d465 123static struct loop *current_loop;
f9cce2dc 124
d9dd21a8 125/* Hashtable helper. */
126
127struct biv_entry_hasher : typed_free_remove <biv_entry>
128{
9969c043 129 typedef biv_entry *value_type;
130 typedef rtx_def *compare_type;
131 static inline hashval_t hash (const biv_entry *);
132 static inline bool equal (const biv_entry *, const rtx_def *);
d9dd21a8 133};
134
135/* Returns hash value for biv B. */
136
137inline hashval_t
9969c043 138biv_entry_hasher::hash (const biv_entry *b)
d9dd21a8 139{
140 return b->regno;
141}
142
143/* Compares biv B and register R. */
144
145inline bool
9969c043 146biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
d9dd21a8 147{
148 return b->regno == REGNO (r);
149}
150
3f37d465 151/* Bivs of the current loop. */
f9cce2dc 152
c1f445d2 153static hash_table<biv_entry_hasher> *bivs;
f9cce2dc 154
3eeb4f9a 155static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
f9cce2dc 156
aedb7bf8 157/* Return the RTX code corresponding to the IV extend code EXTEND. */
158static inline enum rtx_code
159iv_extend_to_rtx_code (enum iv_extend_code extend)
160{
161 switch (extend)
162 {
163 case IV_SIGN_EXTEND:
164 return SIGN_EXTEND;
165 case IV_ZERO_EXTEND:
166 return ZERO_EXTEND;
167 case IV_UNKNOWN_EXTEND:
168 return UNKNOWN;
169 }
170 gcc_unreachable ();
171}
172
f9cce2dc 173/* Dumps information about IV to FILE. */
174
175extern void dump_iv_info (FILE *, struct rtx_iv *);
176void
177dump_iv_info (FILE *file, struct rtx_iv *iv)
178{
179 if (!iv->base)
180 {
181 fprintf (file, "not simple");
182 return;
183 }
184
bf9fa951 185 if (iv->step == const0_rtx
186 && !iv->first_special)
187 fprintf (file, "invariant ");
f9cce2dc 188
189 print_rtl (file, iv->base);
bf9fa951 190 if (iv->step != const0_rtx)
191 {
192 fprintf (file, " + ");
193 print_rtl (file, iv->step);
194 fprintf (file, " * iteration");
195 }
f9cce2dc 196 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
197
198 if (iv->mode != iv->extend_mode)
199 fprintf (file, " %s to %s",
aedb7bf8 200 rtx_name[iv_extend_to_rtx_code (iv->extend)],
f9cce2dc 201 GET_MODE_NAME (iv->extend_mode));
202
203 if (iv->mult != const1_rtx)
204 {
205 fprintf (file, " * ");
206 print_rtl (file, iv->mult);
207 }
208 if (iv->delta != const0_rtx)
209 {
210 fprintf (file, " + ");
211 print_rtl (file, iv->delta);
212 }
213 if (iv->first_special)
214 fprintf (file, " (first special)");
215}
216
c0efe3c3 217/* Generates a subreg to get the least significant part of EXPR (in mode
218 INNER_MODE) to OUTER_MODE. */
f9cce2dc 219
c0efe3c3 220rtx
3754d046 221lowpart_subreg (machine_mode outer_mode, rtx expr,
222 machine_mode inner_mode)
f9cce2dc 223{
c0efe3c3 224 return simplify_gen_subreg (outer_mode, expr, inner_mode,
225 subreg_lowpart_offset (outer_mode, inner_mode));
f9cce2dc 226}
227
48e1416a 228static void
3072d30e 229check_iv_ref_table_size (void)
230{
9af5ce0c 231 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
3072d30e 232 {
233 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
4077bf7a 234 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
48e1416a 235 memset (&iv_ref_table[iv_ref_table_size], 0,
3072d30e 236 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
237 iv_ref_table_size = new_size;
238 }
239}
240
241
f9cce2dc 242/* Checks whether REG is a well-behaved register. */
243
244static bool
245simple_reg_p (rtx reg)
246{
247 unsigned r;
248
249 if (GET_CODE (reg) == SUBREG)
250 {
251 if (!subreg_lowpart_p (reg))
252 return false;
253 reg = SUBREG_REG (reg);
254 }
255
256 if (!REG_P (reg))
257 return false;
258
259 r = REGNO (reg);
260 if (HARD_REGISTER_NUM_P (r))
261 return false;
262
263 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
264 return false;
265
f9cce2dc 266 return true;
267}
268
3f37d465 269/* Clears the information about ivs stored in df. */
f9cce2dc 270
3f37d465 271static void
272clear_iv_info (void)
f9cce2dc 273{
3072d30e 274 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
3f37d465 275 struct rtx_iv *iv;
f9cce2dc 276
3072d30e 277 check_iv_ref_table_size ();
3f37d465 278 for (i = 0; i < n_defs; i++)
f9cce2dc 279 {
3072d30e 280 iv = iv_ref_table[i];
281 if (iv)
282 {
283 free (iv);
284 iv_ref_table[i] = NULL;
285 }
f9cce2dc 286 }
f9cce2dc 287
c1f445d2 288 bivs->empty ();
f9cce2dc 289}
290
f9cce2dc 291
292/* Prepare the data for an induction variable analysis of a LOOP. */
293
294void
295iv_analysis_loop_init (struct loop *loop)
296{
3f37d465 297 current_loop = loop;
f9cce2dc 298
3f37d465 299 /* Clear the information from the analysis of the previous loop. */
3072d30e 300 if (clean_slate)
b818d1a9 301 {
3072d30e 302 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
c1f445d2 303 bivs = new hash_table<biv_entry_hasher> (10);
3072d30e 304 clean_slate = false;
b818d1a9 305 }
306 else
307 clear_iv_info ();
f9cce2dc 308
3072d30e 309 /* Get rid of the ud chains before processing the rescans. Then add
310 the problem back. */
311 df_remove_problem (df_chain);
312 df_process_deferred_rescans ();
ea9538fb 313 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
3072d30e 314 df_chain_add_problem (DF_UD_CHAIN);
c56a6318 315 df_note_add_problem ();
67e23d6f 316 df_analyze_loop (loop);
3072d30e 317 if (dump_file)
774f8797 318 df_dump_region (dump_file);
3072d30e 319
320 check_iv_ref_table_size ();
f9cce2dc 321}
322
3f37d465 323/* Finds the definition of REG that dominates loop latch and stores
324 it to DEF. Returns false if there is not a single definition
c102b5b9 325 dominating the latch. If REG has no definition in loop, DEF
3f37d465 326 is set to NULL and true is returned. */
f9cce2dc 327
3f37d465 328static bool
ed6e85ae 329latch_dominating_def (rtx reg, df_ref *def)
f9cce2dc 330{
ed6e85ae 331 df_ref single_rd = NULL, adef;
c102b5b9 332 unsigned regno = REGNO (reg);
3072d30e 333 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
f9cce2dc 334
ed6e85ae 335 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
f9cce2dc 336 {
ed6e85ae 337 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
f53d14b1 338 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
3f37d465 339 continue;
340
c102b5b9 341 /* More than one reaching definition. */
3f37d465 342 if (single_rd)
c102b5b9 343 return false;
344
345 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
346 return false;
3f37d465 347
348 single_rd = adef;
f9cce2dc 349 }
f9cce2dc 350
3f37d465 351 *def = single_rd;
352 return true;
353}
f9cce2dc 354
3f37d465 355/* Gets definition of REG reaching its use in INSN and stores it to DEF. */
f9cce2dc 356
3f37d465 357static enum iv_grd_result
3eeb4f9a 358iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
3f37d465 359{
c102b5b9 360 df_ref use, adef;
3f37d465 361 basic_block def_bb, use_bb;
3eeb4f9a 362 rtx_insn *def_insn;
c102b5b9 363 bool dom_p;
48e1416a 364
3f37d465 365 *def = NULL;
366 if (!simple_reg_p (reg))
367 return GRD_INVALID;
368 if (GET_CODE (reg) == SUBREG)
369 reg = SUBREG_REG (reg);
370 gcc_assert (REG_P (reg));
f9cce2dc 371
3072d30e 372 use = df_find_use (insn, reg);
3f37d465 373 gcc_assert (use != NULL);
f9cce2dc 374
3f37d465 375 if (!DF_REF_CHAIN (use))
376 return GRD_INVARIANT;
f9cce2dc 377
c102b5b9 378 /* More than one reaching def. */
3f37d465 379 if (DF_REF_CHAIN (use)->next)
c102b5b9 380 return GRD_INVALID;
86709046 381
c102b5b9 382 adef = DF_REF_CHAIN (use)->ref;
605dec4e 383
384 /* We do not handle setting only part of the register. */
ed6e85ae 385 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
605dec4e 386 return GRD_INVALID;
387
3f37d465 388 def_insn = DF_REF_INSN (adef);
389 def_bb = DF_REF_BB (adef);
390 use_bb = BLOCK_FOR_INSN (insn);
f9cce2dc 391
3f37d465 392 if (use_bb == def_bb)
3072d30e 393 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
3f37d465 394 else
395 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
f9cce2dc 396
3f37d465 397 if (dom_p)
398 {
399 *def = adef;
400 return GRD_SINGLE_DOM;
f9cce2dc 401 }
3f37d465 402
403 /* The definition does not dominate the use. This is still OK if
404 this may be a use of a biv, i.e. if the def_bb dominates loop
c102b5b9 405 latch. */
406 if (just_once_each_iteration_p (current_loop, def_bb))
3f37d465 407 return GRD_MAYBE_BIV;
408
409 return GRD_INVALID;
f9cce2dc 410}
411
412/* Sets IV to invariant CST in MODE. Always returns true (just for
413 consistency with other iv manipulation functions that may fail). */
414
415static bool
3754d046 416iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
f9cce2dc 417{
418 if (mode == VOIDmode)
419 mode = GET_MODE (cst);
420
f9cce2dc 421 iv->mode = mode;
422 iv->base = cst;
423 iv->step = const0_rtx;
424 iv->first_special = false;
aedb7bf8 425 iv->extend = IV_UNKNOWN_EXTEND;
f9cce2dc 426 iv->extend_mode = iv->mode;
427 iv->delta = const0_rtx;
428 iv->mult = const1_rtx;
429
430 return true;
431}
432
433/* Evaluates application of subreg to MODE on IV. */
434
435static bool
3754d046 436iv_subreg (struct rtx_iv *iv, machine_mode mode)
f9cce2dc 437{
bf9fa951 438 /* If iv is invariant, just calculate the new value. */
439 if (iv->step == const0_rtx
440 && !iv->first_special)
441 {
442 rtx val = get_iv_value (iv, const0_rtx);
28573ae7 443 val = lowpart_subreg (mode, val,
444 iv->extend == IV_UNKNOWN_EXTEND
445 ? iv->mode : iv->extend_mode);
bf9fa951 446
447 iv->base = val;
aedb7bf8 448 iv->extend = IV_UNKNOWN_EXTEND;
bf9fa951 449 iv->mode = iv->extend_mode = mode;
450 iv->delta = const0_rtx;
451 iv->mult = const1_rtx;
452 return true;
453 }
454
f9cce2dc 455 if (iv->extend_mode == mode)
456 return true;
457
458 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
459 return false;
460
aedb7bf8 461 iv->extend = IV_UNKNOWN_EXTEND;
f9cce2dc 462 iv->mode = mode;
463
464 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
465 simplify_gen_binary (MULT, iv->extend_mode,
466 iv->base, iv->mult));
467 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
468 iv->mult = const1_rtx;
469 iv->delta = const0_rtx;
470 iv->first_special = false;
471
472 return true;
473}
474
475/* Evaluates application of EXTEND to MODE on IV. */
476
477static bool
3754d046 478iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
f9cce2dc 479{
bf9fa951 480 /* If iv is invariant, just calculate the new value. */
481 if (iv->step == const0_rtx
482 && !iv->first_special)
483 {
484 rtx val = get_iv_value (iv, const0_rtx);
28573ae7 485 if (iv->extend_mode != iv->mode
486 && iv->extend != IV_UNKNOWN_EXTEND
487 && iv->extend != extend)
488 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
aedb7bf8 489 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
28573ae7 490 val,
491 iv->extend == extend
492 ? iv->extend_mode : iv->mode);
bf9fa951 493 iv->base = val;
aedb7bf8 494 iv->extend = IV_UNKNOWN_EXTEND;
bf9fa951 495 iv->mode = iv->extend_mode = mode;
496 iv->delta = const0_rtx;
497 iv->mult = const1_rtx;
498 return true;
499 }
500
f9cce2dc 501 if (mode != iv->extend_mode)
502 return false;
503
aedb7bf8 504 if (iv->extend != IV_UNKNOWN_EXTEND
f9cce2dc 505 && iv->extend != extend)
506 return false;
507
508 iv->extend = extend;
509
510 return true;
511}
512
513/* Evaluates negation of IV. */
514
515static bool
516iv_neg (struct rtx_iv *iv)
517{
aedb7bf8 518 if (iv->extend == IV_UNKNOWN_EXTEND)
f9cce2dc 519 {
520 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
521 iv->base, iv->extend_mode);
522 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
523 iv->step, iv->extend_mode);
524 }
525 else
526 {
527 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
528 iv->delta, iv->extend_mode);
529 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
530 iv->mult, iv->extend_mode);
531 }
532
533 return true;
534}
535
536/* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
537
538static bool
539iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
540{
3754d046 541 machine_mode mode;
f9cce2dc 542 rtx arg;
543
aab2cf92 544 /* Extend the constant to extend_mode of the other operand if necessary. */
aedb7bf8 545 if (iv0->extend == IV_UNKNOWN_EXTEND
f9cce2dc 546 && iv0->mode == iv0->extend_mode
547 && iv0->step == const0_rtx
548 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
549 {
550 iv0->extend_mode = iv1->extend_mode;
551 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
552 iv0->base, iv0->mode);
553 }
aedb7bf8 554 if (iv1->extend == IV_UNKNOWN_EXTEND
f9cce2dc 555 && iv1->mode == iv1->extend_mode
556 && iv1->step == const0_rtx
557 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
558 {
559 iv1->extend_mode = iv0->extend_mode;
560 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
561 iv1->base, iv1->mode);
562 }
563
564 mode = iv0->extend_mode;
565 if (mode != iv1->extend_mode)
566 return false;
567
aedb7bf8 568 if (iv0->extend == IV_UNKNOWN_EXTEND
569 && iv1->extend == IV_UNKNOWN_EXTEND)
f9cce2dc 570 {
571 if (iv0->mode != iv1->mode)
572 return false;
573
574 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
575 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
576
577 return true;
578 }
579
580 /* Handle addition of constant. */
aedb7bf8 581 if (iv1->extend == IV_UNKNOWN_EXTEND
f9cce2dc 582 && iv1->mode == mode
583 && iv1->step == const0_rtx)
584 {
585 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
586 return true;
587 }
588
aedb7bf8 589 if (iv0->extend == IV_UNKNOWN_EXTEND
f9cce2dc 590 && iv0->mode == mode
591 && iv0->step == const0_rtx)
592 {
593 arg = iv0->base;
594 *iv0 = *iv1;
595 if (op == MINUS
596 && !iv_neg (iv0))
597 return false;
598
599 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
600 return true;
601 }
602
603 return false;
604}
605
606/* Evaluates multiplication of IV by constant CST. */
607
608static bool
609iv_mult (struct rtx_iv *iv, rtx mby)
610{
3754d046 611 machine_mode mode = iv->extend_mode;
f9cce2dc 612
613 if (GET_MODE (mby) != VOIDmode
614 && GET_MODE (mby) != mode)
615 return false;
616
aedb7bf8 617 if (iv->extend == IV_UNKNOWN_EXTEND)
f9cce2dc 618 {
619 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
620 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
621 }
622 else
623 {
624 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
625 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
626 }
627
628 return true;
629}
630
567c3006 631/* Evaluates shift of IV by constant CST. */
632
633static bool
634iv_shift (struct rtx_iv *iv, rtx mby)
635{
3754d046 636 machine_mode mode = iv->extend_mode;
567c3006 637
638 if (GET_MODE (mby) != VOIDmode
639 && GET_MODE (mby) != mode)
640 return false;
641
aedb7bf8 642 if (iv->extend == IV_UNKNOWN_EXTEND)
567c3006 643 {
644 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
645 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
646 }
647 else
648 {
649 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
650 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
651 }
652
653 return true;
654}
655
f9cce2dc 656/* The recursive part of get_biv_step. Gets the value of the single value
3f37d465 657 defined by DEF wrto initial value of REG inside loop, in shape described
f9cce2dc 658 at get_biv_step. */
659
660static bool
ed6e85ae 661get_biv_step_1 (df_ref def, rtx reg,
3754d046 662 rtx *inner_step, machine_mode *inner_mode,
663 enum iv_extend_code *extend, machine_mode outer_mode,
f9cce2dc 664 rtx *outer_step)
665{
a6ed3419 666 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
3f37d465 667 rtx next, nextr, tmp;
f9cce2dc 668 enum rtx_code code;
3eeb4f9a 669 rtx_insn *insn = DF_REF_INSN (def);
ed6e85ae 670 df_ref next_def;
3f37d465 671 enum iv_grd_result res;
f9cce2dc 672
673 set = single_set (insn);
3f37d465 674 if (!set)
675 return false;
676
f9cce2dc 677 rhs = find_reg_equal_equiv_note (insn);
a6629703 678 if (rhs)
679 rhs = XEXP (rhs, 0);
680 else
f9cce2dc 681 rhs = SET_SRC (set);
f9cce2dc 682
683 code = GET_CODE (rhs);
684 switch (code)
685 {
686 case SUBREG:
687 case REG:
688 next = rhs;
689 break;
690
691 case PLUS:
692 case MINUS:
693 op0 = XEXP (rhs, 0);
694 op1 = XEXP (rhs, 1);
695
696 if (code == PLUS && CONSTANT_P (op0))
697 {
698 tmp = op0; op0 = op1; op1 = tmp;
699 }
700
701 if (!simple_reg_p (op0)
702 || !CONSTANT_P (op1))
703 return false;
704
705 if (GET_MODE (rhs) != outer_mode)
706 {
707 /* ppc64 uses expressions like
708
709 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
710
711 this is equivalent to
712
713 (set x':DI (plus:DI y:DI 1))
714 (set x:SI (subreg:SI (x':DI)). */
715 if (GET_CODE (op0) != SUBREG)
716 return false;
717 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
718 return false;
719 }
720
721 next = op0;
722 break;
723
724 case SIGN_EXTEND:
725 case ZERO_EXTEND:
726 if (GET_MODE (rhs) != outer_mode)
727 return false;
728
729 op0 = XEXP (rhs, 0);
730 if (!simple_reg_p (op0))
731 return false;
732
733 next = op0;
734 break;
735
736 default:
737 return false;
738 }
739
740 if (GET_CODE (next) == SUBREG)
741 {
742 if (!subreg_lowpart_p (next))
743 return false;
744
745 nextr = SUBREG_REG (next);
746 if (GET_MODE (nextr) != outer_mode)
747 return false;
748 }
749 else
750 nextr = next;
751
3f37d465 752 res = iv_get_reaching_def (insn, nextr, &next_def);
753
754 if (res == GRD_INVALID || res == GRD_INVARIANT)
f9cce2dc 755 return false;
756
3f37d465 757 if (res == GRD_MAYBE_BIV)
f9cce2dc 758 {
759 if (!rtx_equal_p (nextr, reg))
760 return false;
761
762 *inner_step = const0_rtx;
aedb7bf8 763 *extend = IV_UNKNOWN_EXTEND;
f9cce2dc 764 *inner_mode = outer_mode;
765 *outer_step = const0_rtx;
766 }
3f37d465 767 else if (!get_biv_step_1 (next_def, reg,
f9cce2dc 768 inner_step, inner_mode, extend, outer_mode,
769 outer_step))
770 return false;
771
772 if (GET_CODE (next) == SUBREG)
773 {
3754d046 774 machine_mode amode = GET_MODE (next);
f9cce2dc 775
776 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
777 return false;
778
779 *inner_mode = amode;
780 *inner_step = simplify_gen_binary (PLUS, outer_mode,
781 *inner_step, *outer_step);
782 *outer_step = const0_rtx;
aedb7bf8 783 *extend = IV_UNKNOWN_EXTEND;
f9cce2dc 784 }
785
786 switch (code)
787 {
788 case REG:
789 case SUBREG:
790 break;
791
792 case PLUS:
793 case MINUS:
794 if (*inner_mode == outer_mode
795 /* See comment in previous switch. */
796 || GET_MODE (rhs) != outer_mode)
797 *inner_step = simplify_gen_binary (code, outer_mode,
798 *inner_step, op1);
799 else
800 *outer_step = simplify_gen_binary (code, outer_mode,
801 *outer_step, op1);
802 break;
803
804 case SIGN_EXTEND:
805 case ZERO_EXTEND:
972e95af 806 gcc_assert (GET_MODE (op0) == *inner_mode
aedb7bf8 807 && *extend == IV_UNKNOWN_EXTEND
972e95af 808 && *outer_step == const0_rtx);
f9cce2dc 809
aedb7bf8 810 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
f9cce2dc 811 break;
812
813 default:
3f37d465 814 return false;
f9cce2dc 815 }
816
817 return true;
818}
819
820/* Gets the operation on register REG inside loop, in shape
821
822 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
823
3f37d465 824 If the operation cannot be described in this shape, return false.
825 LAST_DEF is the definition of REG that dominates loop latch. */
f9cce2dc 826
827static bool
ed6e85ae 828get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
3754d046 829 machine_mode *inner_mode, enum iv_extend_code *extend,
830 machine_mode *outer_mode, rtx *outer_step)
f9cce2dc 831{
832 *outer_mode = GET_MODE (reg);
833
3f37d465 834 if (!get_biv_step_1 (last_def, reg,
f9cce2dc 835 inner_step, inner_mode, extend, *outer_mode,
836 outer_step))
837 return false;
838
aedb7bf8 839 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
972e95af 840 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
f9cce2dc 841
842 return true;
843}
844
3f37d465 845/* Records information that DEF is induction variable IV. */
846
847static void
ed6e85ae 848record_iv (df_ref def, struct rtx_iv *iv)
3f37d465 849{
4c36ffe6 850 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
3f37d465 851
852 *recorded_iv = *iv;
3072d30e 853 check_iv_ref_table_size ();
3f37d465 854 DF_REF_IV_SET (def, recorded_iv);
855}
856
857/* If DEF was already analyzed for bivness, store the description of the biv to
858 IV and return true. Otherwise return false. */
859
860static bool
861analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
862{
c1f445d2 863 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
3f37d465 864
865 if (!biv)
866 return false;
867
868 *iv = biv->iv;
869 return true;
870}
871
872static void
873record_biv (rtx def, struct rtx_iv *iv)
874{
4c36ffe6 875 struct biv_entry *biv = XNEW (struct biv_entry);
c1f445d2 876 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
3f37d465 877
878 biv->regno = REGNO (def);
879 biv->iv = *iv;
880 gcc_assert (!*slot);
881 *slot = biv;
882}
883
f9cce2dc 884/* Determines whether DEF is a biv and if so, stores its description
885 to *IV. */
886
887static bool
f08e8669 888iv_analyze_biv (rtx def, struct rtx_iv *iv)
f9cce2dc 889{
f9cce2dc 890 rtx inner_step, outer_step;
3754d046 891 machine_mode inner_mode, outer_mode;
aedb7bf8 892 enum iv_extend_code extend;
ed6e85ae 893 df_ref last_def;
f9cce2dc 894
450d042a 895 if (dump_file)
f9cce2dc 896 {
4133d091 897 fprintf (dump_file, "Analyzing ");
450d042a 898 print_rtl (dump_file, def);
899 fprintf (dump_file, " for bivness.\n");
f9cce2dc 900 }
48e1416a 901
f9cce2dc 902 if (!REG_P (def))
903 {
904 if (!CONSTANT_P (def))
905 return false;
906
907 return iv_constant (iv, def, VOIDmode);
908 }
909
3f37d465 910 if (!latch_dominating_def (def, &last_def))
f9cce2dc 911 {
450d042a 912 if (dump_file)
913 fprintf (dump_file, " not simple.\n");
f9cce2dc 914 return false;
915 }
916
3f37d465 917 if (!last_def)
918 return iv_constant (iv, def, VOIDmode);
919
920 if (analyzed_for_bivness_p (def, iv))
f9cce2dc 921 {
450d042a 922 if (dump_file)
923 fprintf (dump_file, " already analysed.\n");
f9cce2dc 924 return iv->base != NULL_RTX;
925 }
926
3f37d465 927 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
f9cce2dc 928 &outer_mode, &outer_step))
929 {
930 iv->base = NULL_RTX;
931 goto end;
932 }
933
934 /* Loop transforms base to es (base + inner_step) + outer_step,
935 where es means extend of subreg between inner_mode and outer_mode.
936 The corresponding induction variable is
937
938 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
939
940 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
941 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
942 iv->mode = inner_mode;
943 iv->extend_mode = outer_mode;
944 iv->extend = extend;
945 iv->mult = const1_rtx;
946 iv->delta = outer_step;
947 iv->first_special = inner_mode != outer_mode;
948
450d042a 949 end:
950 if (dump_file)
f9cce2dc 951 {
450d042a 952 fprintf (dump_file, " ");
953 dump_iv_info (dump_file, iv);
954 fprintf (dump_file, "\n");
f9cce2dc 955 }
956
3f37d465 957 record_biv (def, iv);
f9cce2dc 958 return iv->base != NULL_RTX;
959}
960
48e1416a 961/* Analyzes expression RHS used at INSN and stores the result to *IV.
3f37d465 962 The mode of the induction variable is MODE. */
f9cce2dc 963
3f37d465 964bool
3754d046 965iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
3eeb4f9a 966 struct rtx_iv *iv)
f9cce2dc 967{
dfcf26a5 968 rtx mby = NULL_RTX;
3f37d465 969 rtx op0 = NULL_RTX, op1 = NULL_RTX;
970 struct rtx_iv iv0, iv1;
971 enum rtx_code code = GET_CODE (rhs);
3754d046 972 machine_mode omode = mode;
f9cce2dc 973
3f37d465 974 iv->mode = VOIDmode;
975 iv->base = NULL_RTX;
976 iv->step = NULL_RTX;
f9cce2dc 977
3f37d465 978 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
f9cce2dc 979
3f37d465 980 if (CONSTANT_P (rhs)
981 || REG_P (rhs)
982 || code == SUBREG)
f9cce2dc 983 {
3f37d465 984 if (!iv_analyze_op (insn, rhs, iv))
985 return false;
48e1416a 986
3f37d465 987 if (iv->mode == VOIDmode)
f9cce2dc 988 {
3f37d465 989 iv->mode = mode;
990 iv->extend_mode = mode;
f9cce2dc 991 }
3f37d465 992
993 return true;
f9cce2dc 994 }
995
3f37d465 996 switch (code)
f9cce2dc 997 {
3f37d465 998 case REG:
999 op0 = rhs;
1000 break;
f9cce2dc 1001
3f37d465 1002 case SIGN_EXTEND:
1003 case ZERO_EXTEND:
1004 case NEG:
1005 op0 = XEXP (rhs, 0);
1006 omode = GET_MODE (op0);
1007 break;
1008
1009 case PLUS:
1010 case MINUS:
1011 op0 = XEXP (rhs, 0);
1012 op1 = XEXP (rhs, 1);
1013 break;
1014
1015 case MULT:
1016 op0 = XEXP (rhs, 0);
1017 mby = XEXP (rhs, 1);
1018 if (!CONSTANT_P (mby))
dfcf26a5 1019 std::swap (op0, mby);
3f37d465 1020 if (!CONSTANT_P (mby))
1021 return false;
1022 break;
f9cce2dc 1023
3f37d465 1024 case ASHIFT:
1025 op0 = XEXP (rhs, 0);
1026 mby = XEXP (rhs, 1);
1027 if (!CONSTANT_P (mby))
1028 return false;
1029 break;
1030
1031 default:
f9cce2dc 1032 return false;
1033 }
1034
3f37d465 1035 if (op0
1036 && !iv_analyze_expr (insn, op0, omode, &iv0))
1037 return false;
f9cce2dc 1038
3f37d465 1039 if (op1
1040 && !iv_analyze_expr (insn, op1, omode, &iv1))
f9cce2dc 1041 return false;
1042
3f37d465 1043 switch (code)
f9cce2dc 1044 {
3f37d465 1045 case SIGN_EXTEND:
aedb7bf8 1046 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1047 return false;
1048 break;
1049
3f37d465 1050 case ZERO_EXTEND:
aedb7bf8 1051 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
3f37d465 1052 return false;
1053 break;
1054
1055 case NEG:
1056 if (!iv_neg (&iv0))
f9cce2dc 1057 return false;
3f37d465 1058 break;
f9cce2dc 1059
3f37d465 1060 case PLUS:
1061 case MINUS:
1062 if (!iv_add (&iv0, &iv1, code))
f9cce2dc 1063 return false;
3f37d465 1064 break;
f9cce2dc 1065
3f37d465 1066 case MULT:
1067 if (!iv_mult (&iv0, mby))
1068 return false;
1069 break;
1070
1071 case ASHIFT:
1072 if (!iv_shift (&iv0, mby))
1073 return false;
1074 break;
1075
1076 default:
1077 break;
f9cce2dc 1078 }
1079
3f37d465 1080 *iv = iv0;
1081 return iv->base != NULL_RTX;
1082}
1083
1084/* Analyzes iv DEF and stores the result to *IV. */
1085
1086static bool
ed6e85ae 1087iv_analyze_def (df_ref def, struct rtx_iv *iv)
3f37d465 1088{
3eeb4f9a 1089 rtx_insn *insn = DF_REF_INSN (def);
3f37d465 1090 rtx reg = DF_REF_REG (def);
1091 rtx set, rhs;
f9cce2dc 1092
450d042a 1093 if (dump_file)
f9cce2dc 1094 {
666137bc 1095 fprintf (dump_file, "Analyzing def of ");
3f37d465 1096 print_rtl (dump_file, reg);
450d042a 1097 fprintf (dump_file, " in insn ");
1098 print_rtl_single (dump_file, insn);
f9cce2dc 1099 }
48e1416a 1100
3072d30e 1101 check_iv_ref_table_size ();
3f37d465 1102 if (DF_REF_IV (def))
f9cce2dc 1103 {
450d042a 1104 if (dump_file)
1105 fprintf (dump_file, " already analysed.\n");
3f37d465 1106 *iv = *DF_REF_IV (def);
f9cce2dc 1107 return iv->base != NULL_RTX;
1108 }
1109
1110 iv->mode = VOIDmode;
1111 iv->base = NULL_RTX;
1112 iv->step = NULL_RTX;
1113
3072d30e 1114 if (!REG_P (reg))
1115 return false;
1116
f9cce2dc 1117 set = single_set (insn);
3072d30e 1118 if (!set)
1119 return false;
1120
1121 if (!REG_P (SET_DEST (set)))
3f37d465 1122 return false;
1123
3072d30e 1124 gcc_assert (SET_DEST (set) == reg);
f9cce2dc 1125 rhs = find_reg_equal_equiv_note (insn);
a6629703 1126 if (rhs)
1127 rhs = XEXP (rhs, 0);
1128 else
f9cce2dc 1129 rhs = SET_SRC (set);
f9cce2dc 1130
3f37d465 1131 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1132 record_iv (def, iv);
1133
1134 if (dump_file)
f9cce2dc 1135 {
3f37d465 1136 print_rtl (dump_file, reg);
1137 fprintf (dump_file, " in insn ");
1138 print_rtl_single (dump_file, insn);
1139 fprintf (dump_file, " is ");
1140 dump_iv_info (dump_file, iv);
1141 fprintf (dump_file, "\n");
f9cce2dc 1142 }
f9cce2dc 1143
3f37d465 1144 return iv->base != NULL_RTX;
1145}
f9cce2dc 1146
3f37d465 1147/* Analyzes operand OP of INSN and stores the result to *IV. */
f9cce2dc 1148
3f37d465 1149static bool
3eeb4f9a 1150iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
3f37d465 1151{
ed6e85ae 1152 df_ref def = NULL;
3f37d465 1153 enum iv_grd_result res;
567c3006 1154
3f37d465 1155 if (dump_file)
1156 {
666137bc 1157 fprintf (dump_file, "Analyzing operand ");
3f37d465 1158 print_rtl (dump_file, op);
1159 fprintf (dump_file, " of insn ");
1160 print_rtl_single (dump_file, insn);
1161 }
567c3006 1162
e54e3d9f 1163 if (function_invariant_p (op))
3f37d465 1164 res = GRD_INVARIANT;
1165 else if (GET_CODE (op) == SUBREG)
1166 {
1167 if (!subreg_lowpart_p (op))
1168 return false;
f9cce2dc 1169
3f37d465 1170 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1171 return false;
f9cce2dc 1172
3f37d465 1173 return iv_subreg (iv, GET_MODE (op));
1174 }
1175 else
f9cce2dc 1176 {
3f37d465 1177 res = iv_get_reaching_def (insn, op, &def);
1178 if (res == GRD_INVALID)
f9cce2dc 1179 {
3f37d465 1180 if (dump_file)
1181 fprintf (dump_file, " not simple.\n");
1182 return false;
f9cce2dc 1183 }
1184 }
1185
3f37d465 1186 if (res == GRD_INVARIANT)
f9cce2dc 1187 {
3f37d465 1188 iv_constant (iv, op, VOIDmode);
f9cce2dc 1189
3f37d465 1190 if (dump_file)
f9cce2dc 1191 {
3f37d465 1192 fprintf (dump_file, " ");
1193 dump_iv_info (dump_file, iv);
1194 fprintf (dump_file, "\n");
f9cce2dc 1195 }
3f37d465 1196 return true;
f9cce2dc 1197 }
1198
3f37d465 1199 if (res == GRD_MAYBE_BIV)
1200 return iv_analyze_biv (op, iv);
f9cce2dc 1201
3f37d465 1202 return iv_analyze_def (def, iv);
1203}
f9cce2dc 1204
3f37d465 1205/* Analyzes value VAL at INSN and stores the result to *IV. */
f9cce2dc 1206
3f37d465 1207bool
3eeb4f9a 1208iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
3f37d465 1209{
1210 rtx reg;
f9cce2dc 1211
3f37d465 1212 /* We must find the insn in that val is used, so that we get to UD chains.
1213 Since the function is sometimes called on result of get_condition,
1214 this does not necessarily have to be directly INSN; scan also the
1215 following insns. */
1216 if (simple_reg_p (val))
1217 {
1218 if (GET_CODE (val) == SUBREG)
1219 reg = SUBREG_REG (val);
1220 else
1221 reg = val;
567c3006 1222
3072d30e 1223 while (!df_find_use (insn, reg))
3f37d465 1224 insn = NEXT_INSN (insn);
f9cce2dc 1225 }
1226
3f37d465 1227 return iv_analyze_op (insn, val, iv);
1228}
f9cce2dc 1229
3f37d465 1230/* Analyzes definition of DEF in INSN and stores the result to IV. */
f9cce2dc 1231
3f37d465 1232bool
3eeb4f9a 1233iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
3f37d465 1234{
ed6e85ae 1235 df_ref adef;
f9cce2dc 1236
3072d30e 1237 adef = df_find_def (insn, def);
3f37d465 1238 if (!adef)
1239 return false;
1240
1241 return iv_analyze_def (adef, iv);
f9cce2dc 1242}
1243
3f37d465 1244/* Checks whether definition of register REG in INSN is a basic induction
a9989fb4 1245 variable. IV analysis must have been initialized (via a call to
1246 iv_analysis_loop_init) for this function to produce a result. */
1247
1248bool
3eeb4f9a 1249biv_p (rtx_insn *insn, rtx reg)
a9989fb4 1250{
1251 struct rtx_iv iv;
ed6e85ae 1252 df_ref def, last_def;
a9989fb4 1253
3f37d465 1254 if (!simple_reg_p (reg))
a9989fb4 1255 return false;
1256
3072d30e 1257 def = df_find_def (insn, reg);
3f37d465 1258 gcc_assert (def != NULL);
1259 if (!latch_dominating_def (reg, &last_def))
1260 return false;
1261 if (last_def != def)
a9989fb4 1262 return false;
1263
3f37d465 1264 if (!iv_analyze_biv (reg, &iv))
1265 return false;
1266
1267 return iv.step != const0_rtx;
a9989fb4 1268}
1269
f9cce2dc 1270/* Calculates value of IV at ITERATION-th iteration. */
1271
1272rtx
1273get_iv_value (struct rtx_iv *iv, rtx iteration)
1274{
1275 rtx val;
1276
1277 /* We would need to generate some if_then_else patterns, and so far
1278 it is not needed anywhere. */
972e95af 1279 gcc_assert (!iv->first_special);
f9cce2dc 1280
1281 if (iv->step != const0_rtx && iteration != const0_rtx)
1282 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1283 simplify_gen_binary (MULT, iv->extend_mode,
1284 iv->step, iteration));
1285 else
1286 val = iv->base;
1287
1288 if (iv->extend_mode == iv->mode)
1289 return val;
1290
c0efe3c3 1291 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
f9cce2dc 1292
aedb7bf8 1293 if (iv->extend == IV_UNKNOWN_EXTEND)
f9cce2dc 1294 return val;
1295
aedb7bf8 1296 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1297 iv->extend_mode, val, iv->mode);
f9cce2dc 1298 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1299 simplify_gen_binary (MULT, iv->extend_mode,
1300 iv->mult, val));
1301
1302 return val;
1303}
1304
1305/* Free the data for an induction variable analysis. */
1306
1307void
1308iv_analysis_done (void)
1309{
3072d30e 1310 if (!clean_slate)
f9cce2dc 1311 {
3f37d465 1312 clear_iv_info ();
3072d30e 1313 clean_slate = true;
314966f4 1314 df_finish_pass (true);
c1f445d2 1315 delete bivs;
1316 bivs = NULL;
3072d30e 1317 free (iv_ref_table);
1318 iv_ref_table = NULL;
1319 iv_ref_table_size = 0;
f9cce2dc 1320 }
1321}
1322
1323/* Computes inverse to X modulo (1 << MOD). */
1324
3a4303e7 1325static uint64_t
1326inverse (uint64_t x, int mod)
f9cce2dc 1327{
3a4303e7 1328 uint64_t mask =
1329 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1330 uint64_t rslt = 1;
f9cce2dc 1331 int i;
1332
1333 for (i = 0; i < mod - 1; i++)
1334 {
1335 rslt = (rslt * x) & mask;
1336 x = (x * x) & mask;
1337 }
1338
1339 return rslt;
1340}
1341
13033283 1342/* Checks whether any register in X is in set ALT. */
f9cce2dc 1343
13033283 1344static bool
1345altered_reg_used (const_rtx x, bitmap alt)
f9cce2dc 1346{
13033283 1347 subrtx_iterator::array_type array;
1348 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1349 {
1350 const_rtx x = *iter;
1351 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1352 return true;
1353 }
1354 return false;
f9cce2dc 1355}
1356
1357/* Marks registers altered by EXPR in set ALT. */
1358
1359static void
81a410b1 1360mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
f9cce2dc 1361{
1362 if (GET_CODE (expr) == SUBREG)
1363 expr = SUBREG_REG (expr);
1364 if (!REG_P (expr))
1365 return;
1366
4077bf7a 1367 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
f9cce2dc 1368}
1369
1370/* Checks whether RHS is simple enough to process. */
1371
1372static bool
1373simple_rhs_p (rtx rhs)
1374{
1375 rtx op0, op1;
1376
e54e3d9f 1377 if (function_invariant_p (rhs)
b06b27dd 1378 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
f9cce2dc 1379 return true;
1380
1381 switch (GET_CODE (rhs))
1382 {
1383 case PLUS:
1384 case MINUS:
b908751e 1385 case AND:
f9cce2dc 1386 op0 = XEXP (rhs, 0);
1387 op1 = XEXP (rhs, 1);
b908751e 1388 /* Allow reg OP const and reg OP reg. */
b718fe63 1389 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
cf36214f 1390 && !function_invariant_p (op0))
b718fe63 1391 return false;
1392 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
cf36214f 1393 && !function_invariant_p (op1))
b718fe63 1394 return false;
f9cce2dc 1395
b718fe63 1396 return true;
1397
1398 case ASHIFT:
b908751e 1399 case ASHIFTRT:
1400 case LSHIFTRT:
1401 case MULT:
b718fe63 1402 op0 = XEXP (rhs, 0);
1403 op1 = XEXP (rhs, 1);
b908751e 1404 /* Allow reg OP const. */
b718fe63 1405 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1406 return false;
cf36214f 1407 if (!function_invariant_p (op1))
b718fe63 1408 return false;
1409
1410 return true;
f9cce2dc 1411
1412 default:
1413 return false;
1414 }
1415}
1416
b25bc675 1417/* If REGNO has a single definition, return its known value, otherwise return
1418 null. */
767cb310 1419
b25bc675 1420static rtx
1421find_single_def_src (unsigned int regno)
767cb310 1422{
767cb310 1423 df_ref adef;
3a40262c 1424 rtx set, src;
767cb310 1425
3a40262c 1426 for (;;)
1427 {
1428 rtx note;
1429 adef = DF_REG_DEF_CHAIN (regno);
1430 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
b25bc675 1431 || DF_REF_IS_ARTIFICIAL (adef))
1432 return NULL_RTX;
3a40262c 1433
1434 set = single_set (DF_REF_INSN (adef));
1435 if (set == NULL || !REG_P (SET_DEST (set))
1436 || REGNO (SET_DEST (set)) != regno)
b25bc675 1437 return NULL_RTX;
767cb310 1438
3a40262c 1439 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1440
1441 if (note && function_invariant_p (XEXP (note, 0)))
1442 {
1443 src = XEXP (note, 0);
1444 break;
1445 }
1446 src = SET_SRC (set);
1447
1448 if (REG_P (src))
1449 {
1450 regno = REGNO (src);
1451 continue;
1452 }
1453 break;
1454 }
1455 if (!function_invariant_p (src))
b25bc675 1456 return NULL_RTX;
1457
1458 return src;
1459}
1460
1461/* If any registers in *EXPR that have a single definition, try to replace
1462 them with the known-equivalent values. */
767cb310 1463
b25bc675 1464static void
1465replace_single_def_regs (rtx *expr)
1466{
1467 subrtx_var_iterator::array_type array;
1468 repeat:
1469 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1470 {
1471 rtx x = *iter;
1472 if (REG_P (x))
1473 if (rtx new_x = find_single_def_src (REGNO (x)))
1474 {
1475 *expr = simplify_replace_rtx (*expr, x, new_x);
1476 goto repeat;
1477 }
1478 }
767cb310 1479}
1480
9bb5f693 1481/* A subroutine of simplify_using_initial_values, this function examines INSN
1482 to see if it contains a suitable set that we can use to make a replacement.
1483 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1484 the set; return false otherwise. */
f9cce2dc 1485
9bb5f693 1486static bool
3eeb4f9a 1487suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
f9cce2dc 1488{
1489 rtx set = single_set (insn);
1b425b8e 1490 rtx lhs = NULL_RTX, rhs;
f9cce2dc 1491
9bb5f693 1492 if (!set)
1493 return false;
f9cce2dc 1494
9bb5f693 1495 lhs = SET_DEST (set);
1496 if (!REG_P (lhs))
1497 return false;
f9cce2dc 1498
1499 rhs = find_reg_equal_equiv_note (insn);
a6629703 1500 if (rhs)
1501 rhs = XEXP (rhs, 0);
1502 else
f9cce2dc 1503 rhs = SET_SRC (set);
1504
1505 if (!simple_rhs_p (rhs))
9bb5f693 1506 return false;
f9cce2dc 1507
9bb5f693 1508 *dest = lhs;
1509 *src = rhs;
1510 return true;
f9cce2dc 1511}
1512
767cb310 1513/* Using the data returned by suitable_set_for_replacement, replace DEST
1514 with SRC in *EXPR and return the new expression. Also call
1515 replace_single_def_regs if the replacement changed something. */
1516static void
1517replace_in_expr (rtx *expr, rtx dest, rtx src)
1518{
1519 rtx old = *expr;
1520 *expr = simplify_replace_rtx (*expr, dest, src);
1521 if (old == *expr)
1522 return;
b25bc675 1523 replace_single_def_regs (expr);
767cb310 1524}
1525
f9cce2dc 1526/* Checks whether A implies B. */
1527
1528static bool
1529implies_p (rtx a, rtx b)
1530{
dfcf26a5 1531 rtx op0, op1, opb0, opb1;
3754d046 1532 machine_mode mode;
f9cce2dc 1533
a6d46489 1534 if (rtx_equal_p (a, b))
1535 return true;
1536
f9cce2dc 1537 if (GET_CODE (a) == EQ)
1538 {
1539 op0 = XEXP (a, 0);
1540 op1 = XEXP (a, 1);
1541
a6d46489 1542 if (REG_P (op0)
1543 || (GET_CODE (op0) == SUBREG
1544 && REG_P (SUBREG_REG (op0))))
f9cce2dc 1545 {
dfcf26a5 1546 rtx r = simplify_replace_rtx (b, op0, op1);
f9cce2dc 1547 if (r == const_true_rtx)
1548 return true;
1549 }
1550
a6d46489 1551 if (REG_P (op1)
1552 || (GET_CODE (op1) == SUBREG
1553 && REG_P (SUBREG_REG (op1))))
f9cce2dc 1554 {
dfcf26a5 1555 rtx r = simplify_replace_rtx (b, op1, op0);
f9cce2dc 1556 if (r == const_true_rtx)
1557 return true;
1558 }
1559 }
1560
731d95c8 1561 if (b == const_true_rtx)
1562 return true;
1563
1564 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1565 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1566 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1567 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1568 return false;
1569
1570 op0 = XEXP (a, 0);
1571 op1 = XEXP (a, 1);
1572 opb0 = XEXP (b, 0);
1573 opb1 = XEXP (b, 1);
1574
1575 mode = GET_MODE (op0);
1576 if (mode != GET_MODE (opb0))
1577 mode = VOIDmode;
1578 else if (mode == VOIDmode)
1579 {
1580 mode = GET_MODE (op1);
1581 if (mode != GET_MODE (opb1))
1582 mode = VOIDmode;
1583 }
1584
a6629703 1585 /* A < B implies A + 1 <= B. */
1586 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1587 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1588 {
a6629703 1589
1590 if (GET_CODE (a) == GT)
dfcf26a5 1591 std::swap (op0, op1);
a6629703 1592
1593 if (GET_CODE (b) == GE)
dfcf26a5 1594 std::swap (opb0, opb1);
a6629703 1595
873983a1 1596 if (SCALAR_INT_MODE_P (mode)
a6629703 1597 && rtx_equal_p (op1, opb1)
1598 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1599 return true;
731d95c8 1600 return false;
1601 }
1602
1603 /* A < B or A > B imply A != B. TODO: Likewise
1604 A + n < B implies A != B + n if neither wraps. */
1605 if (GET_CODE (b) == NE
1606 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1607 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1608 {
1609 if (rtx_equal_p (op0, opb0)
1610 && rtx_equal_p (op1, opb1))
1611 return true;
a6629703 1612 }
1613
731d95c8 1614 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1615 if (GET_CODE (a) == NE
1616 && op1 == const0_rtx)
1617 {
1618 if ((GET_CODE (b) == GTU
1619 && opb1 == const0_rtx)
1620 || (GET_CODE (b) == GEU
1621 && opb1 == const1_rtx))
1622 return rtx_equal_p (op0, opb0);
1623 }
1624
1625 /* A != N is equivalent to A - (N + 1) <u -1. */
1626 if (GET_CODE (a) == NE
971ba038 1627 && CONST_INT_P (op1)
731d95c8 1628 && GET_CODE (b) == LTU
1629 && opb1 == constm1_rtx
1630 && GET_CODE (opb0) == PLUS
971ba038 1631 && CONST_INT_P (XEXP (opb0, 1))
731d95c8 1632 /* Avoid overflows. */
1633 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1634 != ((unsigned HOST_WIDE_INT)1
1635 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1636 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1637 return rtx_equal_p (op0, XEXP (opb0, 0));
1638
1639 /* Likewise, A != N implies A - N > 0. */
1640 if (GET_CODE (a) == NE
971ba038 1641 && CONST_INT_P (op1))
731d95c8 1642 {
1643 if (GET_CODE (b) == GTU
1644 && GET_CODE (opb0) == PLUS
1645 && opb1 == const0_rtx
971ba038 1646 && CONST_INT_P (XEXP (opb0, 1))
731d95c8 1647 /* Avoid overflows. */
1648 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1649 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1650 && rtx_equal_p (XEXP (opb0, 0), op0))
1651 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1652 if (GET_CODE (b) == GEU
1653 && GET_CODE (opb0) == PLUS
1654 && opb1 == const1_rtx
971ba038 1655 && CONST_INT_P (XEXP (opb0, 1))
731d95c8 1656 /* Avoid overflows. */
1657 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1658 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1659 && rtx_equal_p (XEXP (opb0, 0), op0))
1660 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1661 }
1662
1663 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1664 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
971ba038 1665 && CONST_INT_P (op1)
731d95c8 1666 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1667 || INTVAL (op1) >= 0)
1668 && GET_CODE (b) == LTU
971ba038 1669 && CONST_INT_P (opb1)
27440859 1670 && rtx_equal_p (op0, opb0))
731d95c8 1671 return INTVAL (opb1) < 0;
1672
f9cce2dc 1673 return false;
1674}
1675
1676/* Canonicalizes COND so that
1677
1678 (1) Ensure that operands are ordered according to
1679 swap_commutative_operands_p.
1680 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1681 for GE, GEU, and LEU. */
1682
1683rtx
1684canon_condition (rtx cond)
1685{
f9cce2dc 1686 rtx op0, op1;
1687 enum rtx_code code;
3754d046 1688 machine_mode mode;
f9cce2dc 1689
1690 code = GET_CODE (cond);
1691 op0 = XEXP (cond, 0);
1692 op1 = XEXP (cond, 1);
1693
1694 if (swap_commutative_operands_p (op0, op1))
1695 {
1696 code = swap_condition (code);
dfcf26a5 1697 std::swap (op0, op1);
f9cce2dc 1698 }
1699
1700 mode = GET_MODE (op0);
1701 if (mode == VOIDmode)
1702 mode = GET_MODE (op1);
972e95af 1703 gcc_assert (mode != VOIDmode);
f9cce2dc 1704
63401591 1705 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
f9cce2dc 1706 {
63401591 1707 rtx_mode_t const_val (op1, mode);
f9cce2dc 1708
1709 switch (code)
1710 {
1711 case LE:
63401591 1712 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1713 {
1714 code = LT;
1715 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1716 }
f9cce2dc 1717 break;
1718
f9cce2dc 1719 case GE:
63401591 1720 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1721 {
1722 code = GT;
1723 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1724 }
f9cce2dc 1725 break;
1726
1727 case LEU:
63401591 1728 if (wi::ne_p (const_val, -1))
1729 {
1730 code = LTU;
1731 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1732 }
f9cce2dc 1733 break;
1734
1735 case GEU:
63401591 1736 if (wi::ne_p (const_val, 0))
1737 {
1738 code = GTU;
1739 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1740 }
f9cce2dc 1741 break;
1742
1743 default:
1744 break;
1745 }
1746 }
1747
1748 if (op0 != XEXP (cond, 0)
1749 || op1 != XEXP (cond, 1)
1750 || code != GET_CODE (cond)
1751 || GET_MODE (cond) != SImode)
1752 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1753
1754 return cond;
1755}
1756
b99ab275 1757/* Reverses CONDition; returns NULL if we cannot. */
1758
1759static rtx
1760reversed_condition (rtx cond)
1761{
1762 enum rtx_code reversed;
1763 reversed = reversed_comparison_code (cond, NULL);
1764 if (reversed == UNKNOWN)
1765 return NULL_RTX;
1766 else
1767 return gen_rtx_fmt_ee (reversed,
1768 GET_MODE (cond), XEXP (cond, 0),
1769 XEXP (cond, 1));
1770}
1771
f9cce2dc 1772/* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1773 set of altered regs. */
1774
1775void
1776simplify_using_condition (rtx cond, rtx *expr, regset altered)
1777{
1778 rtx rev, reve, exp = *expr;
1779
f9cce2dc 1780 /* If some register gets altered later, we do not really speak about its
1781 value at the time of comparison. */
13033283 1782 if (altered && altered_reg_used (cond, altered))
f9cce2dc 1783 return;
1784
7f7a6f4a 1785 if (GET_CODE (cond) == EQ
1786 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1787 {
1788 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1789 return;
1790 }
1791
1792 if (!COMPARISON_P (exp))
1793 return;
1794
f9cce2dc 1795 rev = reversed_condition (cond);
1796 reve = reversed_condition (exp);
1797
1798 cond = canon_condition (cond);
1799 exp = canon_condition (exp);
1800 if (rev)
1801 rev = canon_condition (rev);
1802 if (reve)
1803 reve = canon_condition (reve);
1804
1805 if (rtx_equal_p (exp, cond))
1806 {
1807 *expr = const_true_rtx;
1808 return;
1809 }
1810
f9cce2dc 1811 if (rev && rtx_equal_p (exp, rev))
1812 {
1813 *expr = const0_rtx;
1814 return;
1815 }
1816
1817 if (implies_p (cond, exp))
1818 {
1819 *expr = const_true_rtx;
1820 return;
1821 }
48e1416a 1822
f9cce2dc 1823 if (reve && implies_p (cond, reve))
1824 {
1825 *expr = const0_rtx;
1826 return;
1827 }
1828
1829 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1830 be false. */
1831 if (rev && implies_p (exp, rev))
1832 {
1833 *expr = const0_rtx;
1834 return;
1835 }
1836
1837 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1838 if (rev && reve && implies_p (reve, rev))
1839 {
1840 *expr = const_true_rtx;
1841 return;
1842 }
1843
1844 /* We would like to have some other tests here. TODO. */
1845
1846 return;
1847}
1848
1849/* Use relationship between A and *B to eventually eliminate *B.
1850 OP is the operation we consider. */
1851
1852static void
1853eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1854{
972e95af 1855 switch (op)
f9cce2dc 1856 {
972e95af 1857 case AND:
f9cce2dc 1858 /* If A implies *B, we may replace *B by true. */
1859 if (implies_p (a, *b))
1860 *b = const_true_rtx;
972e95af 1861 break;
1862
1863 case IOR:
f9cce2dc 1864 /* If *B implies A, we may replace *B by false. */
1865 if (implies_p (*b, a))
1866 *b = const0_rtx;
972e95af 1867 break;
1868
1869 default:
1870 gcc_unreachable ();
f9cce2dc 1871 }
f9cce2dc 1872}
1873
1874/* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1875 operation we consider. */
1876
1877static void
1878eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1879{
1880 rtx elt;
1881
1882 for (elt = tail; elt; elt = XEXP (elt, 1))
1883 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1884 for (elt = tail; elt; elt = XEXP (elt, 1))
1885 eliminate_implied_condition (op, XEXP (elt, 0), head);
1886}
1887
1888/* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1889 is a list, its elements are assumed to be combined using OP. */
1890
1891static void
1892simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1893{
9bb5f693 1894 bool expression_valid;
8e56831f 1895 rtx head, tail, last_valid_expr;
1896 rtx_expr_list *cond_list;
3eeb4f9a 1897 rtx_insn *insn;
f9cce2dc 1898 rtx neutral, aggr;
9bb5f693 1899 regset altered, this_altered;
f9cce2dc 1900 edge e;
1901
1902 if (!*expr)
1903 return;
1904
1905 if (CONSTANT_P (*expr))
1906 return;
1907
1908 if (GET_CODE (*expr) == EXPR_LIST)
1909 {
1910 head = XEXP (*expr, 0);
1911 tail = XEXP (*expr, 1);
1912
1913 eliminate_implied_conditions (op, &head, tail);
1914
972e95af 1915 switch (op)
f9cce2dc 1916 {
972e95af 1917 case AND:
f9cce2dc 1918 neutral = const_true_rtx;
1919 aggr = const0_rtx;
972e95af 1920 break;
1921
1922 case IOR:
f9cce2dc 1923 neutral = const0_rtx;
1924 aggr = const_true_rtx;
972e95af 1925 break;
f9cce2dc 1926
972e95af 1927 default:
1928 gcc_unreachable ();
1929 }
48e1416a 1930
21f1e711 1931 simplify_using_initial_values (loop, UNKNOWN, &head);
f9cce2dc 1932 if (head == aggr)
1933 {
1934 XEXP (*expr, 0) = aggr;
1935 XEXP (*expr, 1) = NULL_RTX;
1936 return;
1937 }
1938 else if (head == neutral)
1939 {
1940 *expr = tail;
1941 simplify_using_initial_values (loop, op, expr);
1942 return;
1943 }
1944 simplify_using_initial_values (loop, op, &tail);
1945
1946 if (tail && XEXP (tail, 0) == aggr)
1947 {
1948 *expr = tail;
1949 return;
1950 }
48e1416a 1951
f9cce2dc 1952 XEXP (*expr, 0) = head;
1953 XEXP (*expr, 1) = tail;
1954 return;
1955 }
1956
972e95af 1957 gcc_assert (op == UNKNOWN);
f9cce2dc 1958
b25bc675 1959 replace_single_def_regs (expr);
767cb310 1960 if (CONSTANT_P (*expr))
1961 return;
1962
f9cce2dc 1963 e = loop_preheader_edge (loop);
34154e27 1964 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
f9cce2dc 1965 return;
1966
ae85a37a 1967 altered = ALLOC_REG_SET (&reg_obstack);
9bb5f693 1968 this_altered = ALLOC_REG_SET (&reg_obstack);
f9cce2dc 1969
9bb5f693 1970 expression_valid = true;
1971 last_valid_expr = *expr;
8e56831f 1972 cond_list = NULL;
f9cce2dc 1973 while (1)
1974 {
1975 insn = BB_END (e->src);
1976 if (any_condjump_p (insn))
1977 {
a255f65a 1978 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
7f7a6f4a 1979
f9cce2dc 1980 if (cond && (e->flags & EDGE_FALLTHRU))
1981 cond = reversed_condition (cond);
1982 if (cond)
1983 {
7f7a6f4a 1984 rtx old = *expr;
f9cce2dc 1985 simplify_using_condition (cond, expr, altered);
7f7a6f4a 1986 if (old != *expr)
1987 {
1988 rtx note;
1989 if (CONSTANT_P (*expr))
1990 goto out;
1991 for (note = cond_list; note; note = XEXP (note, 1))
1992 {
1993 simplify_using_condition (XEXP (note, 0), expr, altered);
1994 if (CONSTANT_P (*expr))
1995 goto out;
1996 }
1997 }
1998 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
f9cce2dc 1999 }
2000 }
2001
2002 FOR_BB_INSNS_REVERSE (e->src, insn)
2003 {
9bb5f693 2004 rtx src, dest;
7f7a6f4a 2005 rtx old = *expr;
9bb5f693 2006
f9cce2dc 2007 if (!INSN_P (insn))
2008 continue;
9bb5f693 2009
2010 CLEAR_REG_SET (this_altered);
2011 note_stores (PATTERN (insn), mark_altered, this_altered);
2012 if (CALL_P (insn))
f9cce2dc 2013 {
9bb5f693 2014 /* Kill all call clobbered registers. */
24ec6636 2015 unsigned int i;
2016 hard_reg_set_iterator hrsi;
2017 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
2018 0, i, hrsi)
2019 SET_REGNO_REG_SET (this_altered, i);
f9cce2dc 2020 }
9bb5f693 2021
2022 if (suitable_set_for_replacement (insn, &dest, &src))
9676249d 2023 {
8e56831f 2024 rtx_expr_list **pnote, **pnote_next;
7f7a6f4a 2025
767cb310 2026 replace_in_expr (expr, dest, src);
9bb5f693 2027 if (CONSTANT_P (*expr))
2028 goto out;
7f7a6f4a 2029
2030 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2031 {
9ed997be 2032 rtx_expr_list *note = *pnote;
7f7a6f4a 2033 rtx old_cond = XEXP (note, 0);
2034
8e56831f 2035 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
767cb310 2036 replace_in_expr (&XEXP (note, 0), dest, src);
2037
7f7a6f4a 2038 /* We can no longer use a condition that has been simplified
2039 to a constant, and simplify_using_condition will abort if
2040 we try. */
2041 if (CONSTANT_P (XEXP (note, 0)))
2042 {
2043 *pnote = *pnote_next;
2044 pnote_next = pnote;
2045 free_EXPR_LIST_node (note);
2046 }
2047 /* Retry simplifications with this condition if either the
2048 expression or the condition changed. */
2049 else if (old_cond != XEXP (note, 0) || old != *expr)
2050 simplify_using_condition (XEXP (note, 0), expr, altered);
2051 }
9676249d 2052 }
9bb5f693 2053 else
1c18ed19 2054 {
8e56831f 2055 rtx_expr_list **pnote, **pnote_next;
1c18ed19 2056
2057 /* If we did not use this insn to make a replacement, any overlap
2058 between stores in this insn and our expression will cause the
2059 expression to become invalid. */
13033283 2060 if (altered_reg_used (*expr, this_altered))
1c18ed19 2061 goto out;
2062
2063 /* Likewise for the conditions. */
2064 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2065 {
9ed997be 2066 rtx_expr_list *note = *pnote;
1c18ed19 2067 rtx old_cond = XEXP (note, 0);
2068
8e56831f 2069 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
13033283 2070 if (altered_reg_used (old_cond, this_altered))
1c18ed19 2071 {
2072 *pnote = *pnote_next;
2073 pnote_next = pnote;
2074 free_EXPR_LIST_node (note);
2075 }
2076 }
2077 }
9bb5f693 2078
7f7a6f4a 2079 if (CONSTANT_P (*expr))
2080 goto out;
2081
9bb5f693 2082 IOR_REG_SET (altered, this_altered);
2083
2084 /* If the expression now contains regs that have been altered, we
2085 can't return it to the caller. However, it is still valid for
2086 further simplification, so keep searching to see if we can
2087 eventually turn it into a constant. */
13033283 2088 if (altered_reg_used (*expr, altered))
9bb5f693 2089 expression_valid = false;
2090 if (expression_valid)
2091 last_valid_expr = *expr;
f9cce2dc 2092 }
2093
ea091dfd 2094 if (!single_pred_p (e->src)
34154e27 2095 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
f9cce2dc 2096 break;
ea091dfd 2097 e = single_pred_edge (e->src);
f9cce2dc 2098 }
2099
9bb5f693 2100 out:
7f7a6f4a 2101 free_EXPR_LIST_list (&cond_list);
9bb5f693 2102 if (!CONSTANT_P (*expr))
2103 *expr = last_valid_expr;
f9cce2dc 2104 FREE_REG_SET (altered);
9bb5f693 2105 FREE_REG_SET (this_altered);
f9cce2dc 2106}
2107
2108/* Transforms invariant IV into MODE. Adds assumptions based on the fact
2109 that IV occurs as left operands of comparison COND and its signedness
2110 is SIGNED_P to DESC. */
2111
2112static void
3754d046 2113shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
f9cce2dc 2114 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2115{
2116 rtx mmin, mmax, cond_over, cond_under;
2117
a6629703 2118 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
f9cce2dc 2119 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2120 iv->base, mmin);
2121 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2122 iv->base, mmax);
2123
2124 switch (cond)
2125 {
2126 case LE:
2127 case LT:
2128 case LEU:
2129 case LTU:
2130 if (cond_under != const0_rtx)
2131 desc->infinite =
2132 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2133 if (cond_over != const0_rtx)
2134 desc->noloop_assumptions =
2135 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2136 break;
2137
2138 case GE:
2139 case GT:
2140 case GEU:
2141 case GTU:
2142 if (cond_over != const0_rtx)
2143 desc->infinite =
2144 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2145 if (cond_under != const0_rtx)
2146 desc->noloop_assumptions =
2147 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2148 break;
2149
2150 case NE:
2151 if (cond_over != const0_rtx)
2152 desc->infinite =
2153 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2154 if (cond_under != const0_rtx)
2155 desc->infinite =
2156 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2157 break;
2158
2159 default:
972e95af 2160 gcc_unreachable ();
f9cce2dc 2161 }
2162
2163 iv->mode = mode;
aedb7bf8 2164 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
f9cce2dc 2165}
2166
2167/* Transforms IV0 and IV1 compared by COND so that they are both compared as
aab2cf92 2168 subregs of the same mode if possible (sometimes it is necessary to add
f9cce2dc 2169 some assumptions to DESC). */
2170
2171static bool
2172canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2173 enum rtx_code cond, struct niter_desc *desc)
2174{
3754d046 2175 machine_mode comp_mode;
f9cce2dc 2176 bool signed_p;
2177
2178 /* If the ivs behave specially in the first iteration, or are
2179 added/multiplied after extending, we ignore them. */
2180 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2181 return false;
2182 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2183 return false;
2184
2185 /* If there is some extend, it must match signedness of the comparison. */
2186 switch (cond)
2187 {
2188 case LE:
2189 case LT:
aedb7bf8 2190 if (iv0->extend == IV_ZERO_EXTEND
2191 || iv1->extend == IV_ZERO_EXTEND)
f9cce2dc 2192 return false;
2193 signed_p = true;
2194 break;
2195
2196 case LEU:
2197 case LTU:
aedb7bf8 2198 if (iv0->extend == IV_SIGN_EXTEND
2199 || iv1->extend == IV_SIGN_EXTEND)
f9cce2dc 2200 return false;
2201 signed_p = false;
2202 break;
2203
2204 case NE:
aedb7bf8 2205 if (iv0->extend != IV_UNKNOWN_EXTEND
2206 && iv1->extend != IV_UNKNOWN_EXTEND
f9cce2dc 2207 && iv0->extend != iv1->extend)
2208 return false;
2209
2210 signed_p = false;
aedb7bf8 2211 if (iv0->extend != IV_UNKNOWN_EXTEND)
2212 signed_p = iv0->extend == IV_SIGN_EXTEND;
2213 if (iv1->extend != IV_UNKNOWN_EXTEND)
2214 signed_p = iv1->extend == IV_SIGN_EXTEND;
f9cce2dc 2215 break;
2216
2217 default:
972e95af 2218 gcc_unreachable ();
f9cce2dc 2219 }
2220
2221 /* Values of both variables should be computed in the same mode. These
2222 might indeed be different, if we have comparison like
2223
2224 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2225
2226 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2227 in different modes. This does not seem impossible to handle, but
2228 it hardly ever occurs in practice.
48e1416a 2229
f9cce2dc 2230 The only exception is the case when one of operands is invariant.
2231 For example pentium 3 generates comparisons like
2232 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2233 definitely do not want this prevent the optimization. */
2234 comp_mode = iv0->extend_mode;
2235 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2236 comp_mode = iv1->extend_mode;
2237
2238 if (iv0->extend_mode != comp_mode)
2239 {
2240 if (iv0->mode != iv0->extend_mode
2241 || iv0->step != const0_rtx)
2242 return false;
2243
2244 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2245 comp_mode, iv0->base, iv0->mode);
2246 iv0->extend_mode = comp_mode;
2247 }
2248
2249 if (iv1->extend_mode != comp_mode)
2250 {
2251 if (iv1->mode != iv1->extend_mode
2252 || iv1->step != const0_rtx)
2253 return false;
2254
2255 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2256 comp_mode, iv1->base, iv1->mode);
2257 iv1->extend_mode = comp_mode;
2258 }
2259
2260 /* Check that both ivs belong to a range of a single mode. If one of the
2261 operands is an invariant, we may need to shorten it into the common
2262 mode. */
2263 if (iv0->mode == iv0->extend_mode
2264 && iv0->step == const0_rtx
2265 && iv0->mode != iv1->mode)
2266 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2267
2268 if (iv1->mode == iv1->extend_mode
2269 && iv1->step == const0_rtx
2270 && iv0->mode != iv1->mode)
2271 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2272
2273 if (iv0->mode != iv1->mode)
2274 return false;
2275
2276 desc->mode = iv0->mode;
2277 desc->signed_p = signed_p;
2278
2279 return true;
2280}
2281
8fb72e03 2282/* Tries to estimate the maximum number of iterations in LOOP, and return the
2283 result. This function is called from iv_number_of_iterations with
f77c1bb0 2284 a number of fields in DESC already filled in. OLD_NITER is the original
2285 expression for the number of iterations, before we tried to simplify it. */
c9d59418 2286
3a4303e7 2287static uint64_t
f77c1bb0 2288determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
c9d59418 2289{
2290 rtx niter = desc->niter_expr;
731d95c8 2291 rtx mmin, mmax, cmp;
3a4303e7 2292 uint64_t nmax, inc;
2293 uint64_t andmax = 0;
4678223d 2294
2295 /* We used to look for constant operand 0 of AND,
2296 but canonicalization should always make this impossible. */
2297 gcc_checking_assert (GET_CODE (niter) != AND
2298 || !CONST_INT_P (XEXP (niter, 0)));
c9d59418 2299
2300 if (GET_CODE (niter) == AND
4678223d 2301 && CONST_INT_P (XEXP (niter, 1)))
c9d59418 2302 {
4678223d 2303 andmax = UINTVAL (XEXP (niter, 1));
2304 niter = XEXP (niter, 0);
c9d59418 2305 }
2306
2307 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
06b8401d 2308 nmax = UINTVAL (mmax) - UINTVAL (mmin);
c9d59418 2309
2310 if (GET_CODE (niter) == UDIV)
2311 {
971ba038 2312 if (!CONST_INT_P (XEXP (niter, 1)))
8fb72e03 2313 return nmax;
c9d59418 2314 inc = INTVAL (XEXP (niter, 1));
2315 niter = XEXP (niter, 0);
2316 }
2317 else
2318 inc = 1;
2319
731d95c8 2320 /* We could use a binary search here, but for now improving the upper
2321 bound by just one eliminates one important corner case. */
f77c1bb0 2322 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2323 desc->mode, old_niter, mmax);
731d95c8 2324 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2325 if (cmp == const_true_rtx)
c9d59418 2326 {
731d95c8 2327 nmax--;
c9d59418 2328
731d95c8 2329 if (dump_file)
2330 fprintf (dump_file, ";; improved upper bound by one.\n");
c9d59418 2331 }
4678223d 2332 nmax /= inc;
2333 if (andmax)
2334 nmax = MIN (nmax, andmax);
2335 if (dump_file)
f03df321 2336 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
4678223d 2337 nmax);
2338 return nmax;
c9d59418 2339}
2340
f9cce2dc 2341/* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2342 the result into DESC. Very similar to determine_number_of_iterations
2343 (basically its rtl version), complicated by things like subregs. */
2344
d9918998 2345static void
3eeb4f9a 2346iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
f9cce2dc 2347 struct niter_desc *desc)
2348{
3f37d465 2349 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
f9cce2dc 2350 struct rtx_iv iv0, iv1, tmp_iv;
a6629703 2351 rtx assumption, may_not_xform;
f9cce2dc 2352 enum rtx_code cond;
3754d046 2353 machine_mode mode, comp_mode;
a6629703 2354 rtx mmin, mmax, mode_mmin, mode_mmax;
3a4303e7 2355 uint64_t s, size, d, inv, max;
2356 int64_t up, down, inc, step_val;
f9cce2dc 2357 int was_sharp = false;
1e8e4a1d 2358 rtx old_niter;
f8f90b50 2359 bool step_is_pow2;
f9cce2dc 2360
2361 /* The meaning of these assumptions is this:
2362 if !assumptions
2363 then the rest of information does not have to be valid
2364 if noloop_assumptions then the loop does not roll
2365 if infinite then this exit is never used */
2366
2367 desc->assumptions = NULL_RTX;
2368 desc->noloop_assumptions = NULL_RTX;
2369 desc->infinite = NULL_RTX;
2370 desc->simple_p = true;
2371
2372 desc->const_iter = false;
2373 desc->niter_expr = NULL_RTX;
f9cce2dc 2374
2375 cond = GET_CODE (condition);
972e95af 2376 gcc_assert (COMPARISON_P (condition));
f9cce2dc 2377
2378 mode = GET_MODE (XEXP (condition, 0));
2379 if (mode == VOIDmode)
2380 mode = GET_MODE (XEXP (condition, 1));
2381 /* The constant comparisons should be folded. */
972e95af 2382 gcc_assert (mode != VOIDmode);
f9cce2dc 2383
2384 /* We only handle integers or pointers. */
2385 if (GET_MODE_CLASS (mode) != MODE_INT
2386 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2387 goto fail;
2388
2389 op0 = XEXP (condition, 0);
3f37d465 2390 if (!iv_analyze (insn, op0, &iv0))
f9cce2dc 2391 goto fail;
2392 if (iv0.extend_mode == VOIDmode)
2393 iv0.mode = iv0.extend_mode = mode;
48e1416a 2394
f9cce2dc 2395 op1 = XEXP (condition, 1);
3f37d465 2396 if (!iv_analyze (insn, op1, &iv1))
f9cce2dc 2397 goto fail;
2398 if (iv1.extend_mode == VOIDmode)
2399 iv1.mode = iv1.extend_mode = mode;
2400
2401 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2402 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2403 goto fail;
2404
2405 /* Check condition and normalize it. */
2406
2407 switch (cond)
2408 {
2409 case GE:
2410 case GT:
2411 case GEU:
2412 case GTU:
2413 tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
2414 cond = swap_condition (cond);
2415 break;
2416 case NE:
2417 case LE:
2418 case LEU:
2419 case LT:
2420 case LTU:
2421 break;
2422 default:
2423 goto fail;
2424 }
2425
2426 /* Handle extends. This is relatively nontrivial, so we only try in some
2427 easy cases, when we can canonicalize the ivs (possibly by adding some
2428 assumptions) to shape subreg (base + i * step). This function also fills
2429 in desc->mode and desc->signed_p. */
2430
2431 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2432 goto fail;
2433
2434 comp_mode = iv0.extend_mode;
2435 mode = iv0.mode;
4765975c 2436 size = GET_MODE_PRECISION (mode);
a6629703 2437 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
c0efe3c3 2438 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2439 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
f9cce2dc 2440
971ba038 2441 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
f9cce2dc 2442 goto fail;
2443
2444 /* We can take care of the case of two induction variables chasing each other
2445 if the test is NE. I have never seen a loop using it, but still it is
2446 cool. */
2447 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2448 {
2449 if (cond != NE)
2450 goto fail;
2451
2452 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2453 iv1.step = const0_rtx;
2454 }
2455
e4bef271 2456 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2457 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2458
f9cce2dc 2459 /* This is either infinite loop or the one that ends immediately, depending
2460 on initial values. Unswitching should remove this kind of conditions. */
2461 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2462 goto fail;
2463
f8f90b50 2464 if (cond != NE)
2465 {
2466 if (iv0.step == const0_rtx)
2467 step_val = -INTVAL (iv1.step);
2468 else
15943825 2469 step_val = INTVAL (iv0.step);
f8f90b50 2470
2471 /* Ignore loops of while (i-- < 10) type. */
2472 if (step_val < 0)
2473 goto fail;
2474
2475 step_is_pow2 = !(step_val & (step_val - 1));
2476 }
2477 else
2478 {
2479 /* We do not care about whether the step is power of two in this
2480 case. */
2481 step_is_pow2 = false;
2482 step_val = 0;
2483 }
f9cce2dc 2484
2485 /* Some more condition normalization. We must record some assumptions
2486 due to overflows. */
2487 switch (cond)
2488 {
2489 case LT:
2490 case LTU:
2491 /* We want to take care only of non-sharp relationals; this is easy,
2492 as in cases the overflow would make the transformation unsafe
2493 the loop does not roll. Seemingly it would make more sense to want
2494 to take care of sharp relationals instead, as NE is more similar to
2495 them, but the problem is that here the transformation would be more
2496 difficult due to possibly infinite loops. */
2497 if (iv0.step == const0_rtx)
2498 {
c0efe3c3 2499 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
a6629703 2500 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2501 mode_mmax);
f9cce2dc 2502 if (assumption == const_true_rtx)
f30c7901 2503 goto zero_iter_simplify;
f9cce2dc 2504 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2505 iv0.base, const1_rtx);
2506 }
2507 else
2508 {
c0efe3c3 2509 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
a6629703 2510 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2511 mode_mmin);
f9cce2dc 2512 if (assumption == const_true_rtx)
f30c7901 2513 goto zero_iter_simplify;
f9cce2dc 2514 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2515 iv1.base, constm1_rtx);
2516 }
2517
2518 if (assumption != const0_rtx)
2519 desc->noloop_assumptions =
2520 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2521 cond = (cond == LT) ? LE : LEU;
2522
2523 /* It will be useful to be able to tell the difference once more in
2524 LE -> NE reduction. */
2525 was_sharp = true;
2526 break;
2527 default: ;
2528 }
2529
2530 /* Take care of trivially infinite loops. */
2531 if (cond != NE)
2532 {
2533 if (iv0.step == const0_rtx)
2534 {
c0efe3c3 2535 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
a6629703 2536 if (rtx_equal_p (tmp, mode_mmin))
f9cce2dc 2537 {
2538 desc->infinite =
2539 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
f30c7901 2540 /* Fill in the remaining fields somehow. */
2541 goto zero_iter_simplify;
f9cce2dc 2542 }
2543 }
2544 else
2545 {
c0efe3c3 2546 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
a6629703 2547 if (rtx_equal_p (tmp, mode_mmax))
f9cce2dc 2548 {
2549 desc->infinite =
2550 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
f30c7901 2551 /* Fill in the remaining fields somehow. */
2552 goto zero_iter_simplify;
f9cce2dc 2553 }
2554 }
2555 }
2556
2557 /* If we can we want to take care of NE conditions instead of size
2558 comparisons, as they are much more friendly (most importantly
2559 this takes care of special handling of loops with step 1). We can
2560 do it if we first check that upper bound is greater or equal to
2561 lower bound, their difference is constant c modulo step and that
2562 there is not an overflow. */
2563 if (cond != NE)
2564 {
2565 if (iv0.step == const0_rtx)
2566 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2567 else
2568 step = iv0.step;
e4bef271 2569 step = lowpart_subreg (mode, step, comp_mode);
f9cce2dc 2570 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
c0efe3c3 2571 delta = lowpart_subreg (mode, delta, comp_mode);
f9cce2dc 2572 delta = simplify_gen_binary (UMOD, mode, delta, step);
2573 may_xform = const0_rtx;
a6629703 2574 may_not_xform = const_true_rtx;
f9cce2dc 2575
971ba038 2576 if (CONST_INT_P (delta))
f9cce2dc 2577 {
2578 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2579 {
2580 /* A special case. We have transformed condition of type
2581 for (i = 0; i < 4; i += 4)
2582 into
2583 for (i = 0; i <= 3; i += 4)
2584 obviously if the test for overflow during that transformation
2585 passed, we cannot overflow here. Most importantly any
2586 loop with sharp end condition and step 1 falls into this
aab2cf92 2587 category, so handling this case specially is definitely
f9cce2dc 2588 worth the troubles. */
2589 may_xform = const_true_rtx;
2590 }
2591 else if (iv0.step == const0_rtx)
2592 {
2593 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2594 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
c0efe3c3 2595 bound = lowpart_subreg (mode, bound, comp_mode);
2596 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
f9cce2dc 2597 may_xform = simplify_gen_relational (cond, SImode, mode,
2598 bound, tmp);
a6629703 2599 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2600 SImode, mode,
2601 bound, tmp);
f9cce2dc 2602 }
2603 else
2604 {
2605 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2606 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
c0efe3c3 2607 bound = lowpart_subreg (mode, bound, comp_mode);
2608 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
f9cce2dc 2609 may_xform = simplify_gen_relational (cond, SImode, mode,
2610 tmp, bound);
a6629703 2611 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2612 SImode, mode,
2613 tmp, bound);
f9cce2dc 2614 }
2615 }
2616
2617 if (may_xform != const0_rtx)
2618 {
2619 /* We perform the transformation always provided that it is not
2620 completely senseless. This is OK, as we would need this assumption
2621 to determine the number of iterations anyway. */
2622 if (may_xform != const_true_rtx)
a6629703 2623 {
2624 /* If the step is a power of two and the final value we have
2625 computed overflows, the cycle is infinite. Otherwise it
2626 is nontrivial to compute the number of iterations. */
f8f90b50 2627 if (step_is_pow2)
a6629703 2628 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2629 desc->infinite);
2630 else
2631 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2632 desc->assumptions);
2633 }
f9cce2dc 2634
2635 /* We are going to lose some information about upper bound on
2636 number of iterations in this step, so record the information
2637 here. */
2638 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
971ba038 2639 if (CONST_INT_P (iv1.base))
f9cce2dc 2640 up = INTVAL (iv1.base);
2641 else
a6629703 2642 up = INTVAL (mode_mmax) - inc;
971ba038 2643 down = INTVAL (CONST_INT_P (iv0.base)
a6629703 2644 ? iv0.base
2645 : mode_mmin);
06b8401d 2646 max = (uint64_t) (up - down) / inc + 1;
43780738 2647 if (!desc->infinite
2648 && !desc->assumptions)
e913b5cd 2649 record_niter_bound (loop, max, false, true);
f9cce2dc 2650
2651 if (iv0.step == const0_rtx)
2652 {
2653 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2654 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2655 }
2656 else
2657 {
2658 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2659 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2660 }
2661
c0efe3c3 2662 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2663 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
f9cce2dc 2664 assumption = simplify_gen_relational (reverse_condition (cond),
2665 SImode, mode, tmp0, tmp1);
2666 if (assumption == const_true_rtx)
f30c7901 2667 goto zero_iter_simplify;
f9cce2dc 2668 else if (assumption != const0_rtx)
2669 desc->noloop_assumptions =
2670 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2671 cond = NE;
2672 }
2673 }
2674
2675 /* Count the number of iterations. */
2676 if (cond == NE)
2677 {
2678 /* Everything we do here is just arithmetics modulo size of mode. This
2679 makes us able to do more involved computations of number of iterations
2680 than in other cases. First transform the condition into shape
2681 s * i <> c, with s positive. */
2682 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2683 iv0.base = const0_rtx;
2684 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2685 iv1.step = const0_rtx;
2686 if (INTVAL (iv0.step) < 0)
2687 {
e913b5cd 2688 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2689 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
f9cce2dc 2690 }
c0efe3c3 2691 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
f9cce2dc 2692
2693 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2694 is infinite. Otherwise, the number of iterations is
2695 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2696 s = INTVAL (iv0.step); d = 1;
2697 while (s % 2 != 1)
2698 {
2699 s /= 2;
2700 d *= 2;
2701 size--;
2702 }
3a4303e7 2703 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
f9cce2dc 2704
c0efe3c3 2705 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
5d5ee71f 2706 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
f9cce2dc 2707 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2708 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2709
5d5ee71f 2710 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
a6629703 2711 inv = inverse (s, size);
69e41517 2712 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
f9cce2dc 2713 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2714 }
2715 else
2716 {
2717 if (iv1.step == const0_rtx)
2718 /* Condition in shape a + s * i <= b
2719 We must know that b + s does not overflow and a <= b + s and then we
2720 can compute number of iterations as (b + s - a) / s. (It might
2721 seem that we in fact could be more clever about testing the b + s
2722 overflow condition using some information about b - a mod s,
2723 but it was already taken into account during LE -> NE transform). */
2724 {
2725 step = iv0.step;
c0efe3c3 2726 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2727 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
f9cce2dc 2728
a6629703 2729 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
c0efe3c3 2730 lowpart_subreg (mode, step,
2731 comp_mode));
f8f90b50 2732 if (step_is_pow2)
2733 {
2734 rtx t0, t1;
2735
2736 /* If s is power of 2, we know that the loop is infinite if
2737 a % s <= b % s and b + s overflows. */
2738 assumption = simplify_gen_relational (reverse_condition (cond),
2739 SImode, mode,
2740 tmp1, bound);
2741
2742 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2743 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2744 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2745 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2746 desc->infinite =
2747 alloc_EXPR_LIST (0, assumption, desc->infinite);
2748 }
2749 else
2750 {
2751 assumption = simplify_gen_relational (cond, SImode, mode,
2752 tmp1, bound);
2753 desc->assumptions =
2754 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2755 }
f9cce2dc 2756
2757 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
c0efe3c3 2758 tmp = lowpart_subreg (mode, tmp, comp_mode);
f9cce2dc 2759 assumption = simplify_gen_relational (reverse_condition (cond),
2760 SImode, mode, tmp0, tmp);
2761
2762 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2763 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2764 }
2765 else
2766 {
2767 /* Condition in shape a <= b - s * i
2768 We must know that a - s does not overflow and a - s <= b and then
2769 we can again compute number of iterations as (b - (a - s)) / s. */
2770 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
c0efe3c3 2771 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2772 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
f9cce2dc 2773
58379c86 2774 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
c0efe3c3 2775 lowpart_subreg (mode, step, comp_mode));
f8f90b50 2776 if (step_is_pow2)
2777 {
2778 rtx t0, t1;
2779
2780 /* If s is power of 2, we know that the loop is infinite if
2781 a % s <= b % s and a - s overflows. */
2782 assumption = simplify_gen_relational (reverse_condition (cond),
2783 SImode, mode,
2784 bound, tmp0);
2785
2786 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2787 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2788 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2789 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2790 desc->infinite =
2791 alloc_EXPR_LIST (0, assumption, desc->infinite);
2792 }
2793 else
2794 {
2795 assumption = simplify_gen_relational (cond, SImode, mode,
2796 bound, tmp0);
2797 desc->assumptions =
2798 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2799 }
f9cce2dc 2800
2801 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
c0efe3c3 2802 tmp = lowpart_subreg (mode, tmp, comp_mode);
f9cce2dc 2803 assumption = simplify_gen_relational (reverse_condition (cond),
2804 SImode, mode,
2805 tmp, tmp1);
2806 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2807 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2808 }
2809 if (assumption == const_true_rtx)
f30c7901 2810 goto zero_iter_simplify;
f9cce2dc 2811 else if (assumption != const0_rtx)
2812 desc->noloop_assumptions =
2813 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2814 delta = simplify_gen_binary (UDIV, mode, delta, step);
2815 desc->niter_expr = delta;
2816 }
2817
1e8e4a1d 2818 old_niter = desc->niter_expr;
2819
f9cce2dc 2820 simplify_using_initial_values (loop, AND, &desc->assumptions);
2821 if (desc->assumptions
2822 && XEXP (desc->assumptions, 0) == const0_rtx)
2823 goto fail;
2824 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2825 simplify_using_initial_values (loop, IOR, &desc->infinite);
21f1e711 2826 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
f9cce2dc 2827
2828 /* Rerun the simplification. Consider code (created by copying loop headers)
2829
2830 i = 0;
2831
2832 if (0 < n)
2833 {
2834 do
2835 {
2836 i++;
2837 } while (i < n);
2838 }
2839
2840 The first pass determines that i = 0, the second pass uses it to eliminate
2841 noloop assumption. */
2842
2843 simplify_using_initial_values (loop, AND, &desc->assumptions);
2844 if (desc->assumptions
2845 && XEXP (desc->assumptions, 0) == const0_rtx)
2846 goto fail;
2847 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2848 simplify_using_initial_values (loop, IOR, &desc->infinite);
21f1e711 2849 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
f9cce2dc 2850
2d49f824 2851 if (desc->noloop_assumptions
2852 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2853 goto zero_iter;
2854
971ba038 2855 if (CONST_INT_P (desc->niter_expr))
f9cce2dc 2856 {
3a4303e7 2857 uint64_t val = INTVAL (desc->niter_expr);
f9cce2dc 2858
2859 desc->const_iter = true;
a9ef9877 2860 desc->niter = val & GET_MODE_MASK (desc->mode);
43780738 2861 if (!desc->infinite
2862 && !desc->assumptions)
e913b5cd 2863 record_niter_bound (loop, desc->niter, false, true);
f9cce2dc 2864 }
1e8e4a1d 2865 else
2866 {
8fb72e03 2867 max = determine_max_iter (loop, desc, old_niter);
9ce07db4 2868 if (!max)
2869 goto zero_iter_simplify;
43780738 2870 if (!desc->infinite
2871 && !desc->assumptions)
e913b5cd 2872 record_niter_bound (loop, max, false, true);
1e8e4a1d 2873
2874 /* simplify_using_initial_values does a copy propagation on the registers
2875 in the expression for the number of iterations. This prolongs life
2876 ranges of registers and increases register pressure, and usually
2877 brings no gain (and if it happens to do, the cse pass will take care
2878 of it anyway). So prevent this behavior, unless it enabled us to
2879 derive that the number of iterations is a constant. */
2880 desc->niter_expr = old_niter;
2881 }
f9cce2dc 2882
2883 return;
2884
f30c7901 2885zero_iter_simplify:
2886 /* Simplify the assumptions. */
2887 simplify_using_initial_values (loop, AND, &desc->assumptions);
2888 if (desc->assumptions
2889 && XEXP (desc->assumptions, 0) == const0_rtx)
2890 goto fail;
2891 simplify_using_initial_values (loop, IOR, &desc->infinite);
f9cce2dc 2892
f30c7901 2893 /* Fallthru. */
f9cce2dc 2894zero_iter:
2895 desc->const_iter = true;
2896 desc->niter = 0;
e913b5cd 2897 record_niter_bound (loop, 0, true, true);
f30c7901 2898 desc->noloop_assumptions = NULL_RTX;
f9cce2dc 2899 desc->niter_expr = const0_rtx;
2900 return;
f30c7901 2901
2902fail:
2903 desc->simple_p = false;
2904 return;
f9cce2dc 2905}
2906
2907/* Checks whether E is a simple exit from LOOP and stores its description
9c1ccc0f 2908 into DESC. */
f9cce2dc 2909
2910static void
2911check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2912{
2913 basic_block exit_bb;
2d650f54 2914 rtx condition;
2915 rtx_insn *at;
cd665a06 2916 edge ein;
f9cce2dc 2917
2918 exit_bb = e->src;
2919 desc->simple_p = false;
2920
2921 /* It must belong directly to the loop. */
2922 if (exit_bb->loop_father != loop)
2923 return;
2924
2925 /* It must be tested (at least) once during any iteration. */
c102b5b9 2926 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
f9cce2dc 2927 return;
2928
2929 /* It must end in a simple conditional jump. */
2930 if (!any_condjump_p (BB_END (exit_bb)))
2931 return;
2932
cd665a06 2933 ein = EDGE_SUCC (exit_bb, 0);
2934 if (ein == e)
2935 ein = EDGE_SUCC (exit_bb, 1);
f9cce2dc 2936
2937 desc->out_edge = e;
cd665a06 2938 desc->in_edge = ein;
f9cce2dc 2939
2940 /* Test whether the condition is suitable. */
cd665a06 2941 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
f9cce2dc 2942 return;
2943
cd665a06 2944 if (ein->flags & EDGE_FALLTHRU)
f9cce2dc 2945 {
2946 condition = reversed_condition (condition);
2947 if (!condition)
2948 return;
2949 }
2950
2951 /* Check that we are able to determine number of iterations and fill
2952 in information about it. */
2d650f54 2953 iv_number_of_iterations (loop, at, condition, desc);
f9cce2dc 2954}
2955
9c1ccc0f 2956/* Finds a simple exit of LOOP and stores its description into DESC. */
f9cce2dc 2957
2958void
2959find_simple_exit (struct loop *loop, struct niter_desc *desc)
2960{
2961 unsigned i;
2962 basic_block *body;
2963 edge e;
2964 struct niter_desc act;
2965 bool any = false;
cd665a06 2966 edge_iterator ei;
f9cce2dc 2967
2968 desc->simple_p = false;
2969 body = get_loop_body (loop);
2970
2971 for (i = 0; i < loop->num_nodes; i++)
2972 {
cd665a06 2973 FOR_EACH_EDGE (e, ei, body[i]->succs)
f9cce2dc 2974 {
2975 if (flow_bb_inside_loop_p (loop, e->dest))
2976 continue;
48e1416a 2977
f9cce2dc 2978 check_simple_exit (loop, e, &act);
2979 if (!act.simple_p)
2980 continue;
2981
f9cce2dc 2982 if (!any)
2983 any = true;
f30c7901 2984 else
2985 {
2986 /* Prefer constant iterations; the less the better. */
2987 if (!act.const_iter
2988 || (desc->const_iter && act.niter >= desc->niter))
2989 continue;
2990
2991 /* Also if the actual exit may be infinite, while the old one
2992 not, prefer the old one. */
2993 if (act.infinite && !desc->infinite)
2994 continue;
2995 }
48e1416a 2996
f9cce2dc 2997 *desc = act;
2998 }
2999 }
3000
450d042a 3001 if (dump_file)
f9cce2dc 3002 {
3003 if (desc->simple_p)
3004 {
450d042a 3005 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
3006 fprintf (dump_file, " simple exit %d -> %d\n",
f9cce2dc 3007 desc->out_edge->src->index,
3008 desc->out_edge->dest->index);
3009 if (desc->assumptions)
3010 {
450d042a 3011 fprintf (dump_file, " assumptions: ");
3012 print_rtl (dump_file, desc->assumptions);
3013 fprintf (dump_file, "\n");
f9cce2dc 3014 }
3015 if (desc->noloop_assumptions)
3016 {
450d042a 3017 fprintf (dump_file, " does not roll if: ");
3018 print_rtl (dump_file, desc->noloop_assumptions);
3019 fprintf (dump_file, "\n");
f9cce2dc 3020 }
3021 if (desc->infinite)
3022 {
450d042a 3023 fprintf (dump_file, " infinite if: ");
3024 print_rtl (dump_file, desc->infinite);
3025 fprintf (dump_file, "\n");
f9cce2dc 3026 }
3027
450d042a 3028 fprintf (dump_file, " number of iterations: ");
3029 print_rtl (dump_file, desc->niter_expr);
3030 fprintf (dump_file, "\n");
f9cce2dc 3031
a9ef9877 3032 fprintf (dump_file, " upper bound: %li\n",
4e948f5a 3033 (long)get_max_loop_iterations_int (loop));
a9ef9877 3034 fprintf (dump_file, " realistic bound: %li\n",
4e948f5a 3035 (long)get_estimated_loop_iterations_int (loop));
f9cce2dc 3036 }
3037 else
450d042a 3038 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
f9cce2dc 3039 }
3040
3041 free (body);
3042}
3043
3044/* Creates a simple loop description of LOOP if it was not computed
3045 already. */
3046
3047struct niter_desc *
3048get_simple_loop_desc (struct loop *loop)
3049{
3050 struct niter_desc *desc = simple_loop_desc (loop);
3051
3052 if (desc)
3053 return desc;
3054
242753ba 3055 /* At least desc->infinite is not always initialized by
3056 find_simple_loop_exit. */
25a27413 3057 desc = ggc_cleared_alloc<niter_desc> ();
f9cce2dc 3058 iv_analysis_loop_init (loop);
3059 find_simple_exit (loop, desc);
427286b3 3060 loop->simple_loop_desc = desc;
f9cce2dc 3061
7e3cc681 3062 if (desc->simple_p && (desc->assumptions || desc->infinite))
3063 {
48e1416a 3064 const char *wording;
7e3cc681 3065
48e1416a 3066 /* Assume that no overflow happens and that the loop is finite.
7e3cc681 3067 We already warned at the tree level if we ran optimizations there. */
3068 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
3069 {
3070 if (desc->infinite)
3071 {
48e1416a 3072 wording =
7e3cc681 3073 flag_unsafe_loop_optimizations
3074 ? N_("assuming that the loop is not infinite")
3075 : N_("cannot optimize possibly infinite loops");
3076 warning (OPT_Wunsafe_loop_optimizations, "%s",
3077 gettext (wording));
3078 }
3079 if (desc->assumptions)
3080 {
48e1416a 3081 wording =
7e3cc681 3082 flag_unsafe_loop_optimizations
3083 ? N_("assuming that the loop counter does not overflow")
3084 : N_("cannot optimize loop, the loop counter may overflow");
3085 warning (OPT_Wunsafe_loop_optimizations, "%s",
3086 gettext (wording));
3087 }
3088 }
3089
3090 if (flag_unsafe_loop_optimizations)
3091 {
3092 desc->assumptions = NULL_RTX;
3093 desc->infinite = NULL_RTX;
3094 }
3095 }
3096
f9cce2dc 3097 return desc;
3098}
3099
3100/* Releases simple loop description for LOOP. */
3101
3102void
3103free_simple_loop_desc (struct loop *loop)
3104{
3105 struct niter_desc *desc = simple_loop_desc (loop);
3106
3107 if (!desc)
3108 return;
3109
427286b3 3110 ggc_free (desc);
3111 loop->simple_loop_desc = NULL;
f9cce2dc 3112}