PR c++/77539
[official-gcc.git] / gcc / loop-iv.c
blob78bec9e85217480ab01b5d4631af72e5f0810452
1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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.
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.
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.
36 The available functions are:
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.
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "backend.h"
54 #include "rtl.h"
55 #include "df.h"
56 #include "emit-rtl.h"
57 #include "diagnostic-core.h"
58 #include "cfgloop.h"
59 #include "intl.h"
60 #include "dumpfile.h"
61 #include "rtl-iter.h"
63 /* Possible return values of iv_get_reaching_def. */
65 enum iv_grd_result
67 /* More than one reaching def, or reaching def that does not
68 dominate the use. */
69 GRD_INVALID,
71 /* The use is trivial invariant of the loop, i.e. is not changed
72 inside the loop. */
73 GRD_INVARIANT,
75 /* The use is reached by initial value and a value from the
76 previous iteration. */
77 GRD_MAYBE_BIV,
79 /* The use has single dominating def. */
80 GRD_SINGLE_DOM
83 /* Information about a biv. */
85 struct biv_entry
87 unsigned regno; /* The register of the biv. */
88 struct rtx_iv iv; /* Value of the biv. */
91 static bool clean_slate = true;
93 static unsigned int iv_ref_table_size = 0;
95 /* Table of rtx_ivs indexed by the df_ref uid field. */
96 static struct rtx_iv ** iv_ref_table;
98 /* Induction variable stored at the reference. */
99 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
100 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
102 /* The current loop. */
104 static struct loop *current_loop;
106 /* Hashtable helper. */
108 struct biv_entry_hasher : free_ptr_hash <biv_entry>
110 typedef rtx_def *compare_type;
111 static inline hashval_t hash (const biv_entry *);
112 static inline bool equal (const biv_entry *, const rtx_def *);
115 /* Returns hash value for biv B. */
117 inline hashval_t
118 biv_entry_hasher::hash (const biv_entry *b)
120 return b->regno;
123 /* Compares biv B and register R. */
125 inline bool
126 biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
128 return b->regno == REGNO (r);
131 /* Bivs of the current loop. */
133 static hash_table<biv_entry_hasher> *bivs;
135 static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
137 /* Return the RTX code corresponding to the IV extend code EXTEND. */
138 static inline enum rtx_code
139 iv_extend_to_rtx_code (enum iv_extend_code extend)
141 switch (extend)
143 case IV_SIGN_EXTEND:
144 return SIGN_EXTEND;
145 case IV_ZERO_EXTEND:
146 return ZERO_EXTEND;
147 case IV_UNKNOWN_EXTEND:
148 return UNKNOWN;
150 gcc_unreachable ();
153 /* Dumps information about IV to FILE. */
155 extern void dump_iv_info (FILE *, struct rtx_iv *);
156 void
157 dump_iv_info (FILE *file, struct rtx_iv *iv)
159 if (!iv->base)
161 fprintf (file, "not simple");
162 return;
165 if (iv->step == const0_rtx
166 && !iv->first_special)
167 fprintf (file, "invariant ");
169 print_rtl (file, iv->base);
170 if (iv->step != const0_rtx)
172 fprintf (file, " + ");
173 print_rtl (file, iv->step);
174 fprintf (file, " * iteration");
176 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
178 if (iv->mode != iv->extend_mode)
179 fprintf (file, " %s to %s",
180 rtx_name[iv_extend_to_rtx_code (iv->extend)],
181 GET_MODE_NAME (iv->extend_mode));
183 if (iv->mult != const1_rtx)
185 fprintf (file, " * ");
186 print_rtl (file, iv->mult);
188 if (iv->delta != const0_rtx)
190 fprintf (file, " + ");
191 print_rtl (file, iv->delta);
193 if (iv->first_special)
194 fprintf (file, " (first special)");
197 static void
198 check_iv_ref_table_size (void)
200 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
202 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
203 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
204 memset (&iv_ref_table[iv_ref_table_size], 0,
205 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
206 iv_ref_table_size = new_size;
211 /* Checks whether REG is a well-behaved register. */
213 static bool
214 simple_reg_p (rtx reg)
216 unsigned r;
218 if (GET_CODE (reg) == SUBREG)
220 if (!subreg_lowpart_p (reg))
221 return false;
222 reg = SUBREG_REG (reg);
225 if (!REG_P (reg))
226 return false;
228 r = REGNO (reg);
229 if (HARD_REGISTER_NUM_P (r))
230 return false;
232 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
233 return false;
235 return true;
238 /* Clears the information about ivs stored in df. */
240 static void
241 clear_iv_info (void)
243 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
244 struct rtx_iv *iv;
246 check_iv_ref_table_size ();
247 for (i = 0; i < n_defs; i++)
249 iv = iv_ref_table[i];
250 if (iv)
252 free (iv);
253 iv_ref_table[i] = NULL;
257 bivs->empty ();
261 /* Prepare the data for an induction variable analysis of a LOOP. */
263 void
264 iv_analysis_loop_init (struct loop *loop)
266 current_loop = loop;
268 /* Clear the information from the analysis of the previous loop. */
269 if (clean_slate)
271 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
272 bivs = new hash_table<biv_entry_hasher> (10);
273 clean_slate = false;
275 else
276 clear_iv_info ();
278 /* Get rid of the ud chains before processing the rescans. Then add
279 the problem back. */
280 df_remove_problem (df_chain);
281 df_process_deferred_rescans ();
282 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
283 df_chain_add_problem (DF_UD_CHAIN);
284 df_note_add_problem ();
285 df_analyze_loop (loop);
286 if (dump_file)
287 df_dump_region (dump_file);
289 check_iv_ref_table_size ();
292 /* Finds the definition of REG that dominates loop latch and stores
293 it to DEF. Returns false if there is not a single definition
294 dominating the latch. If REG has no definition in loop, DEF
295 is set to NULL and true is returned. */
297 static bool
298 latch_dominating_def (rtx reg, df_ref *def)
300 df_ref single_rd = NULL, adef;
301 unsigned regno = REGNO (reg);
302 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
304 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
306 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
307 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
308 continue;
310 /* More than one reaching definition. */
311 if (single_rd)
312 return false;
314 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
315 return false;
317 single_rd = adef;
320 *def = single_rd;
321 return true;
324 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
326 static enum iv_grd_result
327 iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
329 df_ref use, adef;
330 basic_block def_bb, use_bb;
331 rtx_insn *def_insn;
332 bool dom_p;
334 *def = NULL;
335 if (!simple_reg_p (reg))
336 return GRD_INVALID;
337 if (GET_CODE (reg) == SUBREG)
338 reg = SUBREG_REG (reg);
339 gcc_assert (REG_P (reg));
341 use = df_find_use (insn, reg);
342 gcc_assert (use != NULL);
344 if (!DF_REF_CHAIN (use))
345 return GRD_INVARIANT;
347 /* More than one reaching def. */
348 if (DF_REF_CHAIN (use)->next)
349 return GRD_INVALID;
351 adef = DF_REF_CHAIN (use)->ref;
353 /* We do not handle setting only part of the register. */
354 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
355 return GRD_INVALID;
357 def_insn = DF_REF_INSN (adef);
358 def_bb = DF_REF_BB (adef);
359 use_bb = BLOCK_FOR_INSN (insn);
361 if (use_bb == def_bb)
362 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
363 else
364 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
366 if (dom_p)
368 *def = adef;
369 return GRD_SINGLE_DOM;
372 /* The definition does not dominate the use. This is still OK if
373 this may be a use of a biv, i.e. if the def_bb dominates loop
374 latch. */
375 if (just_once_each_iteration_p (current_loop, def_bb))
376 return GRD_MAYBE_BIV;
378 return GRD_INVALID;
381 /* Sets IV to invariant CST in MODE. Always returns true (just for
382 consistency with other iv manipulation functions that may fail). */
384 static bool
385 iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
387 if (mode == VOIDmode)
388 mode = GET_MODE (cst);
390 iv->mode = mode;
391 iv->base = cst;
392 iv->step = const0_rtx;
393 iv->first_special = false;
394 iv->extend = IV_UNKNOWN_EXTEND;
395 iv->extend_mode = iv->mode;
396 iv->delta = const0_rtx;
397 iv->mult = const1_rtx;
399 return true;
402 /* Evaluates application of subreg to MODE on IV. */
404 static bool
405 iv_subreg (struct rtx_iv *iv, machine_mode mode)
407 /* If iv is invariant, just calculate the new value. */
408 if (iv->step == const0_rtx
409 && !iv->first_special)
411 rtx val = get_iv_value (iv, const0_rtx);
412 val = lowpart_subreg (mode, val,
413 iv->extend == IV_UNKNOWN_EXTEND
414 ? iv->mode : iv->extend_mode);
416 iv->base = val;
417 iv->extend = IV_UNKNOWN_EXTEND;
418 iv->mode = iv->extend_mode = mode;
419 iv->delta = const0_rtx;
420 iv->mult = const1_rtx;
421 return true;
424 if (iv->extend_mode == mode)
425 return true;
427 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
428 return false;
430 iv->extend = IV_UNKNOWN_EXTEND;
431 iv->mode = mode;
433 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
434 simplify_gen_binary (MULT, iv->extend_mode,
435 iv->base, iv->mult));
436 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
437 iv->mult = const1_rtx;
438 iv->delta = const0_rtx;
439 iv->first_special = false;
441 return true;
444 /* Evaluates application of EXTEND to MODE on IV. */
446 static bool
447 iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
449 /* If iv is invariant, just calculate the new value. */
450 if (iv->step == const0_rtx
451 && !iv->first_special)
453 rtx val = get_iv_value (iv, const0_rtx);
454 if (iv->extend_mode != iv->mode
455 && iv->extend != IV_UNKNOWN_EXTEND
456 && iv->extend != extend)
457 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
458 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
459 val,
460 iv->extend == extend
461 ? iv->extend_mode : iv->mode);
462 iv->base = val;
463 iv->extend = IV_UNKNOWN_EXTEND;
464 iv->mode = iv->extend_mode = mode;
465 iv->delta = const0_rtx;
466 iv->mult = const1_rtx;
467 return true;
470 if (mode != iv->extend_mode)
471 return false;
473 if (iv->extend != IV_UNKNOWN_EXTEND
474 && iv->extend != extend)
475 return false;
477 iv->extend = extend;
479 return true;
482 /* Evaluates negation of IV. */
484 static bool
485 iv_neg (struct rtx_iv *iv)
487 if (iv->extend == IV_UNKNOWN_EXTEND)
489 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
490 iv->base, iv->extend_mode);
491 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
492 iv->step, iv->extend_mode);
494 else
496 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
497 iv->delta, iv->extend_mode);
498 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
499 iv->mult, iv->extend_mode);
502 return true;
505 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
507 static bool
508 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
510 machine_mode mode;
511 rtx arg;
513 /* Extend the constant to extend_mode of the other operand if necessary. */
514 if (iv0->extend == IV_UNKNOWN_EXTEND
515 && iv0->mode == iv0->extend_mode
516 && iv0->step == const0_rtx
517 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
519 iv0->extend_mode = iv1->extend_mode;
520 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
521 iv0->base, iv0->mode);
523 if (iv1->extend == IV_UNKNOWN_EXTEND
524 && iv1->mode == iv1->extend_mode
525 && iv1->step == const0_rtx
526 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
528 iv1->extend_mode = iv0->extend_mode;
529 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
530 iv1->base, iv1->mode);
533 mode = iv0->extend_mode;
534 if (mode != iv1->extend_mode)
535 return false;
537 if (iv0->extend == IV_UNKNOWN_EXTEND
538 && iv1->extend == IV_UNKNOWN_EXTEND)
540 if (iv0->mode != iv1->mode)
541 return false;
543 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
544 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
546 return true;
549 /* Handle addition of constant. */
550 if (iv1->extend == IV_UNKNOWN_EXTEND
551 && iv1->mode == mode
552 && iv1->step == const0_rtx)
554 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
555 return true;
558 if (iv0->extend == IV_UNKNOWN_EXTEND
559 && iv0->mode == mode
560 && iv0->step == const0_rtx)
562 arg = iv0->base;
563 *iv0 = *iv1;
564 if (op == MINUS
565 && !iv_neg (iv0))
566 return false;
568 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
569 return true;
572 return false;
575 /* Evaluates multiplication of IV by constant CST. */
577 static bool
578 iv_mult (struct rtx_iv *iv, rtx mby)
580 machine_mode mode = iv->extend_mode;
582 if (GET_MODE (mby) != VOIDmode
583 && GET_MODE (mby) != mode)
584 return false;
586 if (iv->extend == IV_UNKNOWN_EXTEND)
588 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
589 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
591 else
593 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
594 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
597 return true;
600 /* Evaluates shift of IV by constant CST. */
602 static bool
603 iv_shift (struct rtx_iv *iv, rtx mby)
605 machine_mode mode = iv->extend_mode;
607 if (GET_MODE (mby) != VOIDmode
608 && GET_MODE (mby) != mode)
609 return false;
611 if (iv->extend == IV_UNKNOWN_EXTEND)
613 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
614 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
616 else
618 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
619 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
622 return true;
625 /* The recursive part of get_biv_step. Gets the value of the single value
626 defined by DEF wrto initial value of REG inside loop, in shape described
627 at get_biv_step. */
629 static bool
630 get_biv_step_1 (df_ref def, rtx reg,
631 rtx *inner_step, machine_mode *inner_mode,
632 enum iv_extend_code *extend, machine_mode outer_mode,
633 rtx *outer_step)
635 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
636 rtx next, nextr;
637 enum rtx_code code;
638 rtx_insn *insn = DF_REF_INSN (def);
639 df_ref next_def;
640 enum iv_grd_result res;
642 set = single_set (insn);
643 if (!set)
644 return false;
646 rhs = find_reg_equal_equiv_note (insn);
647 if (rhs)
648 rhs = XEXP (rhs, 0);
649 else
650 rhs = SET_SRC (set);
652 code = GET_CODE (rhs);
653 switch (code)
655 case SUBREG:
656 case REG:
657 next = rhs;
658 break;
660 case PLUS:
661 case MINUS:
662 op0 = XEXP (rhs, 0);
663 op1 = XEXP (rhs, 1);
665 if (code == PLUS && CONSTANT_P (op0))
666 std::swap (op0, op1);
668 if (!simple_reg_p (op0)
669 || !CONSTANT_P (op1))
670 return false;
672 if (GET_MODE (rhs) != outer_mode)
674 /* ppc64 uses expressions like
676 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
678 this is equivalent to
680 (set x':DI (plus:DI y:DI 1))
681 (set x:SI (subreg:SI (x':DI)). */
682 if (GET_CODE (op0) != SUBREG)
683 return false;
684 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
685 return false;
688 next = op0;
689 break;
691 case SIGN_EXTEND:
692 case ZERO_EXTEND:
693 if (GET_MODE (rhs) != outer_mode)
694 return false;
696 op0 = XEXP (rhs, 0);
697 if (!simple_reg_p (op0))
698 return false;
700 next = op0;
701 break;
703 default:
704 return false;
707 if (GET_CODE (next) == SUBREG)
709 if (!subreg_lowpart_p (next))
710 return false;
712 nextr = SUBREG_REG (next);
713 if (GET_MODE (nextr) != outer_mode)
714 return false;
716 else
717 nextr = next;
719 res = iv_get_reaching_def (insn, nextr, &next_def);
721 if (res == GRD_INVALID || res == GRD_INVARIANT)
722 return false;
724 if (res == GRD_MAYBE_BIV)
726 if (!rtx_equal_p (nextr, reg))
727 return false;
729 *inner_step = const0_rtx;
730 *extend = IV_UNKNOWN_EXTEND;
731 *inner_mode = outer_mode;
732 *outer_step = const0_rtx;
734 else if (!get_biv_step_1 (next_def, reg,
735 inner_step, inner_mode, extend, outer_mode,
736 outer_step))
737 return false;
739 if (GET_CODE (next) == SUBREG)
741 machine_mode amode = GET_MODE (next);
743 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
744 return false;
746 *inner_mode = amode;
747 *inner_step = simplify_gen_binary (PLUS, outer_mode,
748 *inner_step, *outer_step);
749 *outer_step = const0_rtx;
750 *extend = IV_UNKNOWN_EXTEND;
753 switch (code)
755 case REG:
756 case SUBREG:
757 break;
759 case PLUS:
760 case MINUS:
761 if (*inner_mode == outer_mode
762 /* See comment in previous switch. */
763 || GET_MODE (rhs) != outer_mode)
764 *inner_step = simplify_gen_binary (code, outer_mode,
765 *inner_step, op1);
766 else
767 *outer_step = simplify_gen_binary (code, outer_mode,
768 *outer_step, op1);
769 break;
771 case SIGN_EXTEND:
772 case ZERO_EXTEND:
773 gcc_assert (GET_MODE (op0) == *inner_mode
774 && *extend == IV_UNKNOWN_EXTEND
775 && *outer_step == const0_rtx);
777 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
778 break;
780 default:
781 return false;
784 return true;
787 /* Gets the operation on register REG inside loop, in shape
789 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
791 If the operation cannot be described in this shape, return false.
792 LAST_DEF is the definition of REG that dominates loop latch. */
794 static bool
795 get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
796 machine_mode *inner_mode, enum iv_extend_code *extend,
797 machine_mode *outer_mode, rtx *outer_step)
799 *outer_mode = GET_MODE (reg);
801 if (!get_biv_step_1 (last_def, reg,
802 inner_step, inner_mode, extend, *outer_mode,
803 outer_step))
804 return false;
806 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
807 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
809 return true;
812 /* Records information that DEF is induction variable IV. */
814 static void
815 record_iv (df_ref def, struct rtx_iv *iv)
817 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
819 *recorded_iv = *iv;
820 check_iv_ref_table_size ();
821 DF_REF_IV_SET (def, recorded_iv);
824 /* If DEF was already analyzed for bivness, store the description of the biv to
825 IV and return true. Otherwise return false. */
827 static bool
828 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
830 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
832 if (!biv)
833 return false;
835 *iv = biv->iv;
836 return true;
839 static void
840 record_biv (rtx def, struct rtx_iv *iv)
842 struct biv_entry *biv = XNEW (struct biv_entry);
843 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
845 biv->regno = REGNO (def);
846 biv->iv = *iv;
847 gcc_assert (!*slot);
848 *slot = biv;
851 /* Determines whether DEF is a biv and if so, stores its description
852 to *IV. */
854 static bool
855 iv_analyze_biv (rtx def, struct rtx_iv *iv)
857 rtx inner_step, outer_step;
858 machine_mode inner_mode, outer_mode;
859 enum iv_extend_code extend;
860 df_ref last_def;
862 if (dump_file)
864 fprintf (dump_file, "Analyzing ");
865 print_rtl (dump_file, def);
866 fprintf (dump_file, " for bivness.\n");
869 if (!REG_P (def))
871 if (!CONSTANT_P (def))
872 return false;
874 return iv_constant (iv, def, VOIDmode);
877 if (!latch_dominating_def (def, &last_def))
879 if (dump_file)
880 fprintf (dump_file, " not simple.\n");
881 return false;
884 if (!last_def)
885 return iv_constant (iv, def, VOIDmode);
887 if (analyzed_for_bivness_p (def, iv))
889 if (dump_file)
890 fprintf (dump_file, " already analysed.\n");
891 return iv->base != NULL_RTX;
894 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
895 &outer_mode, &outer_step))
897 iv->base = NULL_RTX;
898 goto end;
901 /* Loop transforms base to es (base + inner_step) + outer_step,
902 where es means extend of subreg between inner_mode and outer_mode.
903 The corresponding induction variable is
905 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
907 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
908 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
909 iv->mode = inner_mode;
910 iv->extend_mode = outer_mode;
911 iv->extend = extend;
912 iv->mult = const1_rtx;
913 iv->delta = outer_step;
914 iv->first_special = inner_mode != outer_mode;
916 end:
917 if (dump_file)
919 fprintf (dump_file, " ");
920 dump_iv_info (dump_file, iv);
921 fprintf (dump_file, "\n");
924 record_biv (def, iv);
925 return iv->base != NULL_RTX;
928 /* Analyzes expression RHS used at INSN and stores the result to *IV.
929 The mode of the induction variable is MODE. */
931 bool
932 iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
933 struct rtx_iv *iv)
935 rtx mby = NULL_RTX;
936 rtx op0 = NULL_RTX, op1 = NULL_RTX;
937 struct rtx_iv iv0, iv1;
938 enum rtx_code code = GET_CODE (rhs);
939 machine_mode omode = mode;
941 iv->mode = VOIDmode;
942 iv->base = NULL_RTX;
943 iv->step = NULL_RTX;
945 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
947 if (CONSTANT_P (rhs)
948 || REG_P (rhs)
949 || code == SUBREG)
951 if (!iv_analyze_op (insn, rhs, iv))
952 return false;
954 if (iv->mode == VOIDmode)
956 iv->mode = mode;
957 iv->extend_mode = mode;
960 return true;
963 switch (code)
965 case REG:
966 op0 = rhs;
967 break;
969 case SIGN_EXTEND:
970 case ZERO_EXTEND:
971 case NEG:
972 op0 = XEXP (rhs, 0);
973 omode = GET_MODE (op0);
974 break;
976 case PLUS:
977 case MINUS:
978 op0 = XEXP (rhs, 0);
979 op1 = XEXP (rhs, 1);
980 break;
982 case MULT:
983 op0 = XEXP (rhs, 0);
984 mby = XEXP (rhs, 1);
985 if (!CONSTANT_P (mby))
986 std::swap (op0, mby);
987 if (!CONSTANT_P (mby))
988 return false;
989 break;
991 case ASHIFT:
992 op0 = XEXP (rhs, 0);
993 mby = XEXP (rhs, 1);
994 if (!CONSTANT_P (mby))
995 return false;
996 break;
998 default:
999 return false;
1002 if (op0
1003 && !iv_analyze_expr (insn, op0, omode, &iv0))
1004 return false;
1006 if (op1
1007 && !iv_analyze_expr (insn, op1, omode, &iv1))
1008 return false;
1010 switch (code)
1012 case SIGN_EXTEND:
1013 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1014 return false;
1015 break;
1017 case ZERO_EXTEND:
1018 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
1019 return false;
1020 break;
1022 case NEG:
1023 if (!iv_neg (&iv0))
1024 return false;
1025 break;
1027 case PLUS:
1028 case MINUS:
1029 if (!iv_add (&iv0, &iv1, code))
1030 return false;
1031 break;
1033 case MULT:
1034 if (!iv_mult (&iv0, mby))
1035 return false;
1036 break;
1038 case ASHIFT:
1039 if (!iv_shift (&iv0, mby))
1040 return false;
1041 break;
1043 default:
1044 break;
1047 *iv = iv0;
1048 return iv->base != NULL_RTX;
1051 /* Analyzes iv DEF and stores the result to *IV. */
1053 static bool
1054 iv_analyze_def (df_ref def, struct rtx_iv *iv)
1056 rtx_insn *insn = DF_REF_INSN (def);
1057 rtx reg = DF_REF_REG (def);
1058 rtx set, rhs;
1060 if (dump_file)
1062 fprintf (dump_file, "Analyzing def of ");
1063 print_rtl (dump_file, reg);
1064 fprintf (dump_file, " in insn ");
1065 print_rtl_single (dump_file, insn);
1068 check_iv_ref_table_size ();
1069 if (DF_REF_IV (def))
1071 if (dump_file)
1072 fprintf (dump_file, " already analysed.\n");
1073 *iv = *DF_REF_IV (def);
1074 return iv->base != NULL_RTX;
1077 iv->mode = VOIDmode;
1078 iv->base = NULL_RTX;
1079 iv->step = NULL_RTX;
1081 if (!REG_P (reg))
1082 return false;
1084 set = single_set (insn);
1085 if (!set)
1086 return false;
1088 if (!REG_P (SET_DEST (set)))
1089 return false;
1091 gcc_assert (SET_DEST (set) == reg);
1092 rhs = find_reg_equal_equiv_note (insn);
1093 if (rhs)
1094 rhs = XEXP (rhs, 0);
1095 else
1096 rhs = SET_SRC (set);
1098 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1099 record_iv (def, iv);
1101 if (dump_file)
1103 print_rtl (dump_file, reg);
1104 fprintf (dump_file, " in insn ");
1105 print_rtl_single (dump_file, insn);
1106 fprintf (dump_file, " is ");
1107 dump_iv_info (dump_file, iv);
1108 fprintf (dump_file, "\n");
1111 return iv->base != NULL_RTX;
1114 /* Analyzes operand OP of INSN and stores the result to *IV. */
1116 static bool
1117 iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
1119 df_ref def = NULL;
1120 enum iv_grd_result res;
1122 if (dump_file)
1124 fprintf (dump_file, "Analyzing operand ");
1125 print_rtl (dump_file, op);
1126 fprintf (dump_file, " of insn ");
1127 print_rtl_single (dump_file, insn);
1130 if (function_invariant_p (op))
1131 res = GRD_INVARIANT;
1132 else if (GET_CODE (op) == SUBREG)
1134 if (!subreg_lowpart_p (op))
1135 return false;
1137 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1138 return false;
1140 return iv_subreg (iv, GET_MODE (op));
1142 else
1144 res = iv_get_reaching_def (insn, op, &def);
1145 if (res == GRD_INVALID)
1147 if (dump_file)
1148 fprintf (dump_file, " not simple.\n");
1149 return false;
1153 if (res == GRD_INVARIANT)
1155 iv_constant (iv, op, VOIDmode);
1157 if (dump_file)
1159 fprintf (dump_file, " ");
1160 dump_iv_info (dump_file, iv);
1161 fprintf (dump_file, "\n");
1163 return true;
1166 if (res == GRD_MAYBE_BIV)
1167 return iv_analyze_biv (op, iv);
1169 return iv_analyze_def (def, iv);
1172 /* Analyzes value VAL at INSN and stores the result to *IV. */
1174 bool
1175 iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
1177 rtx reg;
1179 /* We must find the insn in that val is used, so that we get to UD chains.
1180 Since the function is sometimes called on result of get_condition,
1181 this does not necessarily have to be directly INSN; scan also the
1182 following insns. */
1183 if (simple_reg_p (val))
1185 if (GET_CODE (val) == SUBREG)
1186 reg = SUBREG_REG (val);
1187 else
1188 reg = val;
1190 while (!df_find_use (insn, reg))
1191 insn = NEXT_INSN (insn);
1194 return iv_analyze_op (insn, val, iv);
1197 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1199 bool
1200 iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
1202 df_ref adef;
1204 adef = df_find_def (insn, def);
1205 if (!adef)
1206 return false;
1208 return iv_analyze_def (adef, iv);
1211 /* Checks whether definition of register REG in INSN is a basic induction
1212 variable. IV analysis must have been initialized (via a call to
1213 iv_analysis_loop_init) for this function to produce a result. */
1215 bool
1216 biv_p (rtx_insn *insn, rtx reg)
1218 struct rtx_iv iv;
1219 df_ref def, last_def;
1221 if (!simple_reg_p (reg))
1222 return false;
1224 def = df_find_def (insn, reg);
1225 gcc_assert (def != NULL);
1226 if (!latch_dominating_def (reg, &last_def))
1227 return false;
1228 if (last_def != def)
1229 return false;
1231 if (!iv_analyze_biv (reg, &iv))
1232 return false;
1234 return iv.step != const0_rtx;
1237 /* Calculates value of IV at ITERATION-th iteration. */
1240 get_iv_value (struct rtx_iv *iv, rtx iteration)
1242 rtx val;
1244 /* We would need to generate some if_then_else patterns, and so far
1245 it is not needed anywhere. */
1246 gcc_assert (!iv->first_special);
1248 if (iv->step != const0_rtx && iteration != const0_rtx)
1249 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1250 simplify_gen_binary (MULT, iv->extend_mode,
1251 iv->step, iteration));
1252 else
1253 val = iv->base;
1255 if (iv->extend_mode == iv->mode)
1256 return val;
1258 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1260 if (iv->extend == IV_UNKNOWN_EXTEND)
1261 return val;
1263 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1264 iv->extend_mode, val, iv->mode);
1265 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1266 simplify_gen_binary (MULT, iv->extend_mode,
1267 iv->mult, val));
1269 return val;
1272 /* Free the data for an induction variable analysis. */
1274 void
1275 iv_analysis_done (void)
1277 if (!clean_slate)
1279 clear_iv_info ();
1280 clean_slate = true;
1281 df_finish_pass (true);
1282 delete bivs;
1283 bivs = NULL;
1284 free (iv_ref_table);
1285 iv_ref_table = NULL;
1286 iv_ref_table_size = 0;
1290 /* Computes inverse to X modulo (1 << MOD). */
1292 static uint64_t
1293 inverse (uint64_t x, int mod)
1295 uint64_t mask =
1296 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1297 uint64_t rslt = 1;
1298 int i;
1300 for (i = 0; i < mod - 1; i++)
1302 rslt = (rslt * x) & mask;
1303 x = (x * x) & mask;
1306 return rslt;
1309 /* Checks whether any register in X is in set ALT. */
1311 static bool
1312 altered_reg_used (const_rtx x, bitmap alt)
1314 subrtx_iterator::array_type array;
1315 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1317 const_rtx x = *iter;
1318 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1319 return true;
1321 return false;
1324 /* Marks registers altered by EXPR in set ALT. */
1326 static void
1327 mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
1329 if (GET_CODE (expr) == SUBREG)
1330 expr = SUBREG_REG (expr);
1331 if (!REG_P (expr))
1332 return;
1334 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
1337 /* Checks whether RHS is simple enough to process. */
1339 static bool
1340 simple_rhs_p (rtx rhs)
1342 rtx op0, op1;
1344 if (function_invariant_p (rhs)
1345 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
1346 return true;
1348 switch (GET_CODE (rhs))
1350 case PLUS:
1351 case MINUS:
1352 case AND:
1353 op0 = XEXP (rhs, 0);
1354 op1 = XEXP (rhs, 1);
1355 /* Allow reg OP const and reg OP reg. */
1356 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
1357 && !function_invariant_p (op0))
1358 return false;
1359 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
1360 && !function_invariant_p (op1))
1361 return false;
1363 return true;
1365 case ASHIFT:
1366 case ASHIFTRT:
1367 case LSHIFTRT:
1368 case MULT:
1369 op0 = XEXP (rhs, 0);
1370 op1 = XEXP (rhs, 1);
1371 /* Allow reg OP const. */
1372 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1373 return false;
1374 if (!function_invariant_p (op1))
1375 return false;
1377 return true;
1379 default:
1380 return false;
1384 /* If REGNO has a single definition, return its known value, otherwise return
1385 null. */
1387 static rtx
1388 find_single_def_src (unsigned int regno)
1390 df_ref adef;
1391 rtx set, src;
1393 for (;;)
1395 rtx note;
1396 adef = DF_REG_DEF_CHAIN (regno);
1397 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
1398 || DF_REF_IS_ARTIFICIAL (adef))
1399 return NULL_RTX;
1401 set = single_set (DF_REF_INSN (adef));
1402 if (set == NULL || !REG_P (SET_DEST (set))
1403 || REGNO (SET_DEST (set)) != regno)
1404 return NULL_RTX;
1406 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1408 if (note && function_invariant_p (XEXP (note, 0)))
1410 src = XEXP (note, 0);
1411 break;
1413 src = SET_SRC (set);
1415 if (REG_P (src))
1417 regno = REGNO (src);
1418 continue;
1420 break;
1422 if (!function_invariant_p (src))
1423 return NULL_RTX;
1425 return src;
1428 /* If any registers in *EXPR that have a single definition, try to replace
1429 them with the known-equivalent values. */
1431 static void
1432 replace_single_def_regs (rtx *expr)
1434 subrtx_var_iterator::array_type array;
1435 repeat:
1436 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1438 rtx x = *iter;
1439 if (REG_P (x))
1440 if (rtx new_x = find_single_def_src (REGNO (x)))
1442 *expr = simplify_replace_rtx (*expr, x, new_x);
1443 goto repeat;
1448 /* A subroutine of simplify_using_initial_values, this function examines INSN
1449 to see if it contains a suitable set that we can use to make a replacement.
1450 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1451 the set; return false otherwise. */
1453 static bool
1454 suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
1456 rtx set = single_set (insn);
1457 rtx lhs = NULL_RTX, rhs;
1459 if (!set)
1460 return false;
1462 lhs = SET_DEST (set);
1463 if (!REG_P (lhs))
1464 return false;
1466 rhs = find_reg_equal_equiv_note (insn);
1467 if (rhs)
1468 rhs = XEXP (rhs, 0);
1469 else
1470 rhs = SET_SRC (set);
1472 if (!simple_rhs_p (rhs))
1473 return false;
1475 *dest = lhs;
1476 *src = rhs;
1477 return true;
1480 /* Using the data returned by suitable_set_for_replacement, replace DEST
1481 with SRC in *EXPR and return the new expression. Also call
1482 replace_single_def_regs if the replacement changed something. */
1483 static void
1484 replace_in_expr (rtx *expr, rtx dest, rtx src)
1486 rtx old = *expr;
1487 *expr = simplify_replace_rtx (*expr, dest, src);
1488 if (old == *expr)
1489 return;
1490 replace_single_def_regs (expr);
1493 /* Checks whether A implies B. */
1495 static bool
1496 implies_p (rtx a, rtx b)
1498 rtx op0, op1, opb0, opb1;
1499 machine_mode mode;
1501 if (rtx_equal_p (a, b))
1502 return true;
1504 if (GET_CODE (a) == EQ)
1506 op0 = XEXP (a, 0);
1507 op1 = XEXP (a, 1);
1509 if (REG_P (op0)
1510 || (GET_CODE (op0) == SUBREG
1511 && REG_P (SUBREG_REG (op0))))
1513 rtx r = simplify_replace_rtx (b, op0, op1);
1514 if (r == const_true_rtx)
1515 return true;
1518 if (REG_P (op1)
1519 || (GET_CODE (op1) == SUBREG
1520 && REG_P (SUBREG_REG (op1))))
1522 rtx r = simplify_replace_rtx (b, op1, op0);
1523 if (r == const_true_rtx)
1524 return true;
1528 if (b == const_true_rtx)
1529 return true;
1531 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1532 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1533 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1534 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1535 return false;
1537 op0 = XEXP (a, 0);
1538 op1 = XEXP (a, 1);
1539 opb0 = XEXP (b, 0);
1540 opb1 = XEXP (b, 1);
1542 mode = GET_MODE (op0);
1543 if (mode != GET_MODE (opb0))
1544 mode = VOIDmode;
1545 else if (mode == VOIDmode)
1547 mode = GET_MODE (op1);
1548 if (mode != GET_MODE (opb1))
1549 mode = VOIDmode;
1552 /* A < B implies A + 1 <= B. */
1553 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1554 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1557 if (GET_CODE (a) == GT)
1558 std::swap (op0, op1);
1560 if (GET_CODE (b) == GE)
1561 std::swap (opb0, opb1);
1563 if (SCALAR_INT_MODE_P (mode)
1564 && rtx_equal_p (op1, opb1)
1565 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1566 return true;
1567 return false;
1570 /* A < B or A > B imply A != B. TODO: Likewise
1571 A + n < B implies A != B + n if neither wraps. */
1572 if (GET_CODE (b) == NE
1573 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1574 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1576 if (rtx_equal_p (op0, opb0)
1577 && rtx_equal_p (op1, opb1))
1578 return true;
1581 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1582 if (GET_CODE (a) == NE
1583 && op1 == const0_rtx)
1585 if ((GET_CODE (b) == GTU
1586 && opb1 == const0_rtx)
1587 || (GET_CODE (b) == GEU
1588 && opb1 == const1_rtx))
1589 return rtx_equal_p (op0, opb0);
1592 /* A != N is equivalent to A - (N + 1) <u -1. */
1593 if (GET_CODE (a) == NE
1594 && CONST_INT_P (op1)
1595 && GET_CODE (b) == LTU
1596 && opb1 == constm1_rtx
1597 && GET_CODE (opb0) == PLUS
1598 && CONST_INT_P (XEXP (opb0, 1))
1599 /* Avoid overflows. */
1600 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1601 != ((unsigned HOST_WIDE_INT)1
1602 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1603 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1604 return rtx_equal_p (op0, XEXP (opb0, 0));
1606 /* Likewise, A != N implies A - N > 0. */
1607 if (GET_CODE (a) == NE
1608 && CONST_INT_P (op1))
1610 if (GET_CODE (b) == GTU
1611 && GET_CODE (opb0) == PLUS
1612 && opb1 == const0_rtx
1613 && CONST_INT_P (XEXP (opb0, 1))
1614 /* Avoid overflows. */
1615 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1616 != (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
1617 && rtx_equal_p (XEXP (opb0, 0), op0))
1618 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1619 if (GET_CODE (b) == GEU
1620 && GET_CODE (opb0) == PLUS
1621 && opb1 == const1_rtx
1622 && CONST_INT_P (XEXP (opb0, 1))
1623 /* Avoid overflows. */
1624 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1625 != (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
1626 && rtx_equal_p (XEXP (opb0, 0), op0))
1627 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1630 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1631 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
1632 && CONST_INT_P (op1)
1633 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1634 || INTVAL (op1) >= 0)
1635 && GET_CODE (b) == LTU
1636 && CONST_INT_P (opb1)
1637 && rtx_equal_p (op0, opb0))
1638 return INTVAL (opb1) < 0;
1640 return false;
1643 /* Canonicalizes COND so that
1645 (1) Ensure that operands are ordered according to
1646 swap_commutative_operands_p.
1647 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1648 for GE, GEU, and LEU. */
1651 canon_condition (rtx cond)
1653 rtx op0, op1;
1654 enum rtx_code code;
1655 machine_mode mode;
1657 code = GET_CODE (cond);
1658 op0 = XEXP (cond, 0);
1659 op1 = XEXP (cond, 1);
1661 if (swap_commutative_operands_p (op0, op1))
1663 code = swap_condition (code);
1664 std::swap (op0, op1);
1667 mode = GET_MODE (op0);
1668 if (mode == VOIDmode)
1669 mode = GET_MODE (op1);
1670 gcc_assert (mode != VOIDmode);
1672 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
1674 rtx_mode_t const_val (op1, mode);
1676 switch (code)
1678 case LE:
1679 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1681 code = LT;
1682 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1684 break;
1686 case GE:
1687 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1689 code = GT;
1690 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1692 break;
1694 case LEU:
1695 if (wi::ne_p (const_val, -1))
1697 code = LTU;
1698 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1700 break;
1702 case GEU:
1703 if (wi::ne_p (const_val, 0))
1705 code = GTU;
1706 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1708 break;
1710 default:
1711 break;
1715 if (op0 != XEXP (cond, 0)
1716 || op1 != XEXP (cond, 1)
1717 || code != GET_CODE (cond)
1718 || GET_MODE (cond) != SImode)
1719 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1721 return cond;
1724 /* Reverses CONDition; returns NULL if we cannot. */
1726 static rtx
1727 reversed_condition (rtx cond)
1729 enum rtx_code reversed;
1730 reversed = reversed_comparison_code (cond, NULL);
1731 if (reversed == UNKNOWN)
1732 return NULL_RTX;
1733 else
1734 return gen_rtx_fmt_ee (reversed,
1735 GET_MODE (cond), XEXP (cond, 0),
1736 XEXP (cond, 1));
1739 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1740 set of altered regs. */
1742 void
1743 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1745 rtx rev, reve, exp = *expr;
1747 /* If some register gets altered later, we do not really speak about its
1748 value at the time of comparison. */
1749 if (altered && altered_reg_used (cond, altered))
1750 return;
1752 if (GET_CODE (cond) == EQ
1753 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1755 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1756 return;
1759 if (!COMPARISON_P (exp))
1760 return;
1762 rev = reversed_condition (cond);
1763 reve = reversed_condition (exp);
1765 cond = canon_condition (cond);
1766 exp = canon_condition (exp);
1767 if (rev)
1768 rev = canon_condition (rev);
1769 if (reve)
1770 reve = canon_condition (reve);
1772 if (rtx_equal_p (exp, cond))
1774 *expr = const_true_rtx;
1775 return;
1778 if (rev && rtx_equal_p (exp, rev))
1780 *expr = const0_rtx;
1781 return;
1784 if (implies_p (cond, exp))
1786 *expr = const_true_rtx;
1787 return;
1790 if (reve && implies_p (cond, reve))
1792 *expr = const0_rtx;
1793 return;
1796 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1797 be false. */
1798 if (rev && implies_p (exp, rev))
1800 *expr = const0_rtx;
1801 return;
1804 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1805 if (rev && reve && implies_p (reve, rev))
1807 *expr = const_true_rtx;
1808 return;
1811 /* We would like to have some other tests here. TODO. */
1813 return;
1816 /* Use relationship between A and *B to eventually eliminate *B.
1817 OP is the operation we consider. */
1819 static void
1820 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1822 switch (op)
1824 case AND:
1825 /* If A implies *B, we may replace *B by true. */
1826 if (implies_p (a, *b))
1827 *b = const_true_rtx;
1828 break;
1830 case IOR:
1831 /* If *B implies A, we may replace *B by false. */
1832 if (implies_p (*b, a))
1833 *b = const0_rtx;
1834 break;
1836 default:
1837 gcc_unreachable ();
1841 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1842 operation we consider. */
1844 static void
1845 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1847 rtx elt;
1849 for (elt = tail; elt; elt = XEXP (elt, 1))
1850 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1851 for (elt = tail; elt; elt = XEXP (elt, 1))
1852 eliminate_implied_condition (op, XEXP (elt, 0), head);
1855 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1856 is a list, its elements are assumed to be combined using OP. */
1858 static void
1859 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1861 bool expression_valid;
1862 rtx head, tail, last_valid_expr;
1863 rtx_expr_list *cond_list;
1864 rtx_insn *insn;
1865 rtx neutral, aggr;
1866 regset altered, this_altered;
1867 edge e;
1869 if (!*expr)
1870 return;
1872 if (CONSTANT_P (*expr))
1873 return;
1875 if (GET_CODE (*expr) == EXPR_LIST)
1877 head = XEXP (*expr, 0);
1878 tail = XEXP (*expr, 1);
1880 eliminate_implied_conditions (op, &head, tail);
1882 switch (op)
1884 case AND:
1885 neutral = const_true_rtx;
1886 aggr = const0_rtx;
1887 break;
1889 case IOR:
1890 neutral = const0_rtx;
1891 aggr = const_true_rtx;
1892 break;
1894 default:
1895 gcc_unreachable ();
1898 simplify_using_initial_values (loop, UNKNOWN, &head);
1899 if (head == aggr)
1901 XEXP (*expr, 0) = aggr;
1902 XEXP (*expr, 1) = NULL_RTX;
1903 return;
1905 else if (head == neutral)
1907 *expr = tail;
1908 simplify_using_initial_values (loop, op, expr);
1909 return;
1911 simplify_using_initial_values (loop, op, &tail);
1913 if (tail && XEXP (tail, 0) == aggr)
1915 *expr = tail;
1916 return;
1919 XEXP (*expr, 0) = head;
1920 XEXP (*expr, 1) = tail;
1921 return;
1924 gcc_assert (op == UNKNOWN);
1926 replace_single_def_regs (expr);
1927 if (CONSTANT_P (*expr))
1928 return;
1930 e = loop_preheader_edge (loop);
1931 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1932 return;
1934 altered = ALLOC_REG_SET (&reg_obstack);
1935 this_altered = ALLOC_REG_SET (&reg_obstack);
1937 expression_valid = true;
1938 last_valid_expr = *expr;
1939 cond_list = NULL;
1940 while (1)
1942 insn = BB_END (e->src);
1943 if (any_condjump_p (insn))
1945 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1947 if (cond && (e->flags & EDGE_FALLTHRU))
1948 cond = reversed_condition (cond);
1949 if (cond)
1951 rtx old = *expr;
1952 simplify_using_condition (cond, expr, altered);
1953 if (old != *expr)
1955 rtx note;
1956 if (CONSTANT_P (*expr))
1957 goto out;
1958 for (note = cond_list; note; note = XEXP (note, 1))
1960 simplify_using_condition (XEXP (note, 0), expr, altered);
1961 if (CONSTANT_P (*expr))
1962 goto out;
1965 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
1969 FOR_BB_INSNS_REVERSE (e->src, insn)
1971 rtx src, dest;
1972 rtx old = *expr;
1974 if (!INSN_P (insn))
1975 continue;
1977 CLEAR_REG_SET (this_altered);
1978 note_stores (PATTERN (insn), mark_altered, this_altered);
1979 if (CALL_P (insn))
1981 /* Kill all call clobbered registers. */
1982 unsigned int i;
1983 hard_reg_set_iterator hrsi;
1984 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
1985 0, i, hrsi)
1986 SET_REGNO_REG_SET (this_altered, i);
1989 if (suitable_set_for_replacement (insn, &dest, &src))
1991 rtx_expr_list **pnote, **pnote_next;
1993 replace_in_expr (expr, dest, src);
1994 if (CONSTANT_P (*expr))
1995 goto out;
1997 for (pnote = &cond_list; *pnote; pnote = pnote_next)
1999 rtx_expr_list *note = *pnote;
2000 rtx old_cond = XEXP (note, 0);
2002 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2003 replace_in_expr (&XEXP (note, 0), dest, src);
2005 /* We can no longer use a condition that has been simplified
2006 to a constant, and simplify_using_condition will abort if
2007 we try. */
2008 if (CONSTANT_P (XEXP (note, 0)))
2010 *pnote = *pnote_next;
2011 pnote_next = pnote;
2012 free_EXPR_LIST_node (note);
2014 /* Retry simplifications with this condition if either the
2015 expression or the condition changed. */
2016 else if (old_cond != XEXP (note, 0) || old != *expr)
2017 simplify_using_condition (XEXP (note, 0), expr, altered);
2020 else
2022 rtx_expr_list **pnote, **pnote_next;
2024 /* If we did not use this insn to make a replacement, any overlap
2025 between stores in this insn and our expression will cause the
2026 expression to become invalid. */
2027 if (altered_reg_used (*expr, this_altered))
2028 goto out;
2030 /* Likewise for the conditions. */
2031 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2033 rtx_expr_list *note = *pnote;
2034 rtx old_cond = XEXP (note, 0);
2036 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2037 if (altered_reg_used (old_cond, this_altered))
2039 *pnote = *pnote_next;
2040 pnote_next = pnote;
2041 free_EXPR_LIST_node (note);
2046 if (CONSTANT_P (*expr))
2047 goto out;
2049 IOR_REG_SET (altered, this_altered);
2051 /* If the expression now contains regs that have been altered, we
2052 can't return it to the caller. However, it is still valid for
2053 further simplification, so keep searching to see if we can
2054 eventually turn it into a constant. */
2055 if (altered_reg_used (*expr, altered))
2056 expression_valid = false;
2057 if (expression_valid)
2058 last_valid_expr = *expr;
2061 if (!single_pred_p (e->src)
2062 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2063 break;
2064 e = single_pred_edge (e->src);
2067 out:
2068 free_EXPR_LIST_list (&cond_list);
2069 if (!CONSTANT_P (*expr))
2070 *expr = last_valid_expr;
2071 FREE_REG_SET (altered);
2072 FREE_REG_SET (this_altered);
2075 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2076 that IV occurs as left operands of comparison COND and its signedness
2077 is SIGNED_P to DESC. */
2079 static void
2080 shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
2081 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2083 rtx mmin, mmax, cond_over, cond_under;
2085 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
2086 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2087 iv->base, mmin);
2088 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2089 iv->base, mmax);
2091 switch (cond)
2093 case LE:
2094 case LT:
2095 case LEU:
2096 case LTU:
2097 if (cond_under != const0_rtx)
2098 desc->infinite =
2099 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2100 if (cond_over != const0_rtx)
2101 desc->noloop_assumptions =
2102 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2103 break;
2105 case GE:
2106 case GT:
2107 case GEU:
2108 case GTU:
2109 if (cond_over != const0_rtx)
2110 desc->infinite =
2111 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2112 if (cond_under != const0_rtx)
2113 desc->noloop_assumptions =
2114 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2115 break;
2117 case NE:
2118 if (cond_over != const0_rtx)
2119 desc->infinite =
2120 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2121 if (cond_under != const0_rtx)
2122 desc->infinite =
2123 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2124 break;
2126 default:
2127 gcc_unreachable ();
2130 iv->mode = mode;
2131 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
2134 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2135 subregs of the same mode if possible (sometimes it is necessary to add
2136 some assumptions to DESC). */
2138 static bool
2139 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2140 enum rtx_code cond, struct niter_desc *desc)
2142 machine_mode comp_mode;
2143 bool signed_p;
2145 /* If the ivs behave specially in the first iteration, or are
2146 added/multiplied after extending, we ignore them. */
2147 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2148 return false;
2149 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2150 return false;
2152 /* If there is some extend, it must match signedness of the comparison. */
2153 switch (cond)
2155 case LE:
2156 case LT:
2157 if (iv0->extend == IV_ZERO_EXTEND
2158 || iv1->extend == IV_ZERO_EXTEND)
2159 return false;
2160 signed_p = true;
2161 break;
2163 case LEU:
2164 case LTU:
2165 if (iv0->extend == IV_SIGN_EXTEND
2166 || iv1->extend == IV_SIGN_EXTEND)
2167 return false;
2168 signed_p = false;
2169 break;
2171 case NE:
2172 if (iv0->extend != IV_UNKNOWN_EXTEND
2173 && iv1->extend != IV_UNKNOWN_EXTEND
2174 && iv0->extend != iv1->extend)
2175 return false;
2177 signed_p = false;
2178 if (iv0->extend != IV_UNKNOWN_EXTEND)
2179 signed_p = iv0->extend == IV_SIGN_EXTEND;
2180 if (iv1->extend != IV_UNKNOWN_EXTEND)
2181 signed_p = iv1->extend == IV_SIGN_EXTEND;
2182 break;
2184 default:
2185 gcc_unreachable ();
2188 /* Values of both variables should be computed in the same mode. These
2189 might indeed be different, if we have comparison like
2191 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2193 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2194 in different modes. This does not seem impossible to handle, but
2195 it hardly ever occurs in practice.
2197 The only exception is the case when one of operands is invariant.
2198 For example pentium 3 generates comparisons like
2199 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2200 definitely do not want this prevent the optimization. */
2201 comp_mode = iv0->extend_mode;
2202 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2203 comp_mode = iv1->extend_mode;
2205 if (iv0->extend_mode != comp_mode)
2207 if (iv0->mode != iv0->extend_mode
2208 || iv0->step != const0_rtx)
2209 return false;
2211 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2212 comp_mode, iv0->base, iv0->mode);
2213 iv0->extend_mode = comp_mode;
2216 if (iv1->extend_mode != comp_mode)
2218 if (iv1->mode != iv1->extend_mode
2219 || iv1->step != const0_rtx)
2220 return false;
2222 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2223 comp_mode, iv1->base, iv1->mode);
2224 iv1->extend_mode = comp_mode;
2227 /* Check that both ivs belong to a range of a single mode. If one of the
2228 operands is an invariant, we may need to shorten it into the common
2229 mode. */
2230 if (iv0->mode == iv0->extend_mode
2231 && iv0->step == const0_rtx
2232 && iv0->mode != iv1->mode)
2233 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2235 if (iv1->mode == iv1->extend_mode
2236 && iv1->step == const0_rtx
2237 && iv0->mode != iv1->mode)
2238 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2240 if (iv0->mode != iv1->mode)
2241 return false;
2243 desc->mode = iv0->mode;
2244 desc->signed_p = signed_p;
2246 return true;
2249 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2250 result. This function is called from iv_number_of_iterations with
2251 a number of fields in DESC already filled in. OLD_NITER is the original
2252 expression for the number of iterations, before we tried to simplify it. */
2254 static uint64_t
2255 determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
2257 rtx niter = desc->niter_expr;
2258 rtx mmin, mmax, cmp;
2259 uint64_t nmax, inc;
2260 uint64_t andmax = 0;
2262 /* We used to look for constant operand 0 of AND,
2263 but canonicalization should always make this impossible. */
2264 gcc_checking_assert (GET_CODE (niter) != AND
2265 || !CONST_INT_P (XEXP (niter, 0)));
2267 if (GET_CODE (niter) == AND
2268 && CONST_INT_P (XEXP (niter, 1)))
2270 andmax = UINTVAL (XEXP (niter, 1));
2271 niter = XEXP (niter, 0);
2274 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
2275 nmax = UINTVAL (mmax) - UINTVAL (mmin);
2277 if (GET_CODE (niter) == UDIV)
2279 if (!CONST_INT_P (XEXP (niter, 1)))
2280 return nmax;
2281 inc = INTVAL (XEXP (niter, 1));
2282 niter = XEXP (niter, 0);
2284 else
2285 inc = 1;
2287 /* We could use a binary search here, but for now improving the upper
2288 bound by just one eliminates one important corner case. */
2289 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2290 desc->mode, old_niter, mmax);
2291 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2292 if (cmp == const_true_rtx)
2294 nmax--;
2296 if (dump_file)
2297 fprintf (dump_file, ";; improved upper bound by one.\n");
2299 nmax /= inc;
2300 if (andmax)
2301 nmax = MIN (nmax, andmax);
2302 if (dump_file)
2303 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
2304 nmax);
2305 return nmax;
2308 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2309 the result into DESC. Very similar to determine_number_of_iterations
2310 (basically its rtl version), complicated by things like subregs. */
2312 static void
2313 iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
2314 struct niter_desc *desc)
2316 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
2317 struct rtx_iv iv0, iv1;
2318 rtx assumption, may_not_xform;
2319 enum rtx_code cond;
2320 machine_mode mode, comp_mode;
2321 rtx mmin, mmax, mode_mmin, mode_mmax;
2322 uint64_t s, size, d, inv, max, up, down;
2323 int64_t inc, step_val;
2324 int was_sharp = false;
2325 rtx old_niter;
2326 bool step_is_pow2;
2328 /* The meaning of these assumptions is this:
2329 if !assumptions
2330 then the rest of information does not have to be valid
2331 if noloop_assumptions then the loop does not roll
2332 if infinite then this exit is never used */
2334 desc->assumptions = NULL_RTX;
2335 desc->noloop_assumptions = NULL_RTX;
2336 desc->infinite = NULL_RTX;
2337 desc->simple_p = true;
2339 desc->const_iter = false;
2340 desc->niter_expr = NULL_RTX;
2342 cond = GET_CODE (condition);
2343 gcc_assert (COMPARISON_P (condition));
2345 mode = GET_MODE (XEXP (condition, 0));
2346 if (mode == VOIDmode)
2347 mode = GET_MODE (XEXP (condition, 1));
2348 /* The constant comparisons should be folded. */
2349 gcc_assert (mode != VOIDmode);
2351 /* We only handle integers or pointers. */
2352 if (GET_MODE_CLASS (mode) != MODE_INT
2353 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2354 goto fail;
2356 op0 = XEXP (condition, 0);
2357 if (!iv_analyze (insn, op0, &iv0))
2358 goto fail;
2359 if (iv0.extend_mode == VOIDmode)
2360 iv0.mode = iv0.extend_mode = mode;
2362 op1 = XEXP (condition, 1);
2363 if (!iv_analyze (insn, op1, &iv1))
2364 goto fail;
2365 if (iv1.extend_mode == VOIDmode)
2366 iv1.mode = iv1.extend_mode = mode;
2368 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2369 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2370 goto fail;
2372 /* Check condition and normalize it. */
2374 switch (cond)
2376 case GE:
2377 case GT:
2378 case GEU:
2379 case GTU:
2380 std::swap (iv0, iv1);
2381 cond = swap_condition (cond);
2382 break;
2383 case NE:
2384 case LE:
2385 case LEU:
2386 case LT:
2387 case LTU:
2388 break;
2389 default:
2390 goto fail;
2393 /* Handle extends. This is relatively nontrivial, so we only try in some
2394 easy cases, when we can canonicalize the ivs (possibly by adding some
2395 assumptions) to shape subreg (base + i * step). This function also fills
2396 in desc->mode and desc->signed_p. */
2398 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2399 goto fail;
2401 comp_mode = iv0.extend_mode;
2402 mode = iv0.mode;
2403 size = GET_MODE_PRECISION (mode);
2404 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2405 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2406 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2408 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
2409 goto fail;
2411 /* We can take care of the case of two induction variables chasing each other
2412 if the test is NE. I have never seen a loop using it, but still it is
2413 cool. */
2414 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2416 if (cond != NE)
2417 goto fail;
2419 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2420 iv1.step = const0_rtx;
2423 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2424 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2426 /* This is either infinite loop or the one that ends immediately, depending
2427 on initial values. Unswitching should remove this kind of conditions. */
2428 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2429 goto fail;
2431 if (cond != NE)
2433 if (iv0.step == const0_rtx)
2434 step_val = -INTVAL (iv1.step);
2435 else
2436 step_val = INTVAL (iv0.step);
2438 /* Ignore loops of while (i-- < 10) type. */
2439 if (step_val < 0)
2440 goto fail;
2442 step_is_pow2 = !(step_val & (step_val - 1));
2444 else
2446 /* We do not care about whether the step is power of two in this
2447 case. */
2448 step_is_pow2 = false;
2449 step_val = 0;
2452 /* Some more condition normalization. We must record some assumptions
2453 due to overflows. */
2454 switch (cond)
2456 case LT:
2457 case LTU:
2458 /* We want to take care only of non-sharp relationals; this is easy,
2459 as in cases the overflow would make the transformation unsafe
2460 the loop does not roll. Seemingly it would make more sense to want
2461 to take care of sharp relationals instead, as NE is more similar to
2462 them, but the problem is that here the transformation would be more
2463 difficult due to possibly infinite loops. */
2464 if (iv0.step == const0_rtx)
2466 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2467 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2468 mode_mmax);
2469 if (assumption == const_true_rtx)
2470 goto zero_iter_simplify;
2471 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2472 iv0.base, const1_rtx);
2474 else
2476 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2477 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2478 mode_mmin);
2479 if (assumption == const_true_rtx)
2480 goto zero_iter_simplify;
2481 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2482 iv1.base, constm1_rtx);
2485 if (assumption != const0_rtx)
2486 desc->noloop_assumptions =
2487 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2488 cond = (cond == LT) ? LE : LEU;
2490 /* It will be useful to be able to tell the difference once more in
2491 LE -> NE reduction. */
2492 was_sharp = true;
2493 break;
2494 default: ;
2497 /* Take care of trivially infinite loops. */
2498 if (cond != NE)
2500 if (iv0.step == const0_rtx)
2502 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2503 if (rtx_equal_p (tmp, mode_mmin))
2505 desc->infinite =
2506 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2507 /* Fill in the remaining fields somehow. */
2508 goto zero_iter_simplify;
2511 else
2513 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2514 if (rtx_equal_p (tmp, mode_mmax))
2516 desc->infinite =
2517 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2518 /* Fill in the remaining fields somehow. */
2519 goto zero_iter_simplify;
2524 /* If we can we want to take care of NE conditions instead of size
2525 comparisons, as they are much more friendly (most importantly
2526 this takes care of special handling of loops with step 1). We can
2527 do it if we first check that upper bound is greater or equal to
2528 lower bound, their difference is constant c modulo step and that
2529 there is not an overflow. */
2530 if (cond != NE)
2532 if (iv0.step == const0_rtx)
2533 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2534 else
2535 step = iv0.step;
2536 step = lowpart_subreg (mode, step, comp_mode);
2537 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2538 delta = lowpart_subreg (mode, delta, comp_mode);
2539 delta = simplify_gen_binary (UMOD, mode, delta, step);
2540 may_xform = const0_rtx;
2541 may_not_xform = const_true_rtx;
2543 if (CONST_INT_P (delta))
2545 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2547 /* A special case. We have transformed condition of type
2548 for (i = 0; i < 4; i += 4)
2549 into
2550 for (i = 0; i <= 3; i += 4)
2551 obviously if the test for overflow during that transformation
2552 passed, we cannot overflow here. Most importantly any
2553 loop with sharp end condition and step 1 falls into this
2554 category, so handling this case specially is definitely
2555 worth the troubles. */
2556 may_xform = const_true_rtx;
2558 else if (iv0.step == const0_rtx)
2560 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2561 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2562 bound = lowpart_subreg (mode, bound, comp_mode);
2563 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2564 may_xform = simplify_gen_relational (cond, SImode, mode,
2565 bound, tmp);
2566 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2567 SImode, mode,
2568 bound, tmp);
2570 else
2572 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2573 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2574 bound = lowpart_subreg (mode, bound, comp_mode);
2575 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2576 may_xform = simplify_gen_relational (cond, SImode, mode,
2577 tmp, bound);
2578 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2579 SImode, mode,
2580 tmp, bound);
2584 if (may_xform != const0_rtx)
2586 /* We perform the transformation always provided that it is not
2587 completely senseless. This is OK, as we would need this assumption
2588 to determine the number of iterations anyway. */
2589 if (may_xform != const_true_rtx)
2591 /* If the step is a power of two and the final value we have
2592 computed overflows, the cycle is infinite. Otherwise it
2593 is nontrivial to compute the number of iterations. */
2594 if (step_is_pow2)
2595 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2596 desc->infinite);
2597 else
2598 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2599 desc->assumptions);
2602 /* We are going to lose some information about upper bound on
2603 number of iterations in this step, so record the information
2604 here. */
2605 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2606 if (CONST_INT_P (iv1.base))
2607 up = INTVAL (iv1.base);
2608 else
2609 up = INTVAL (mode_mmax) - inc;
2610 down = INTVAL (CONST_INT_P (iv0.base)
2611 ? iv0.base
2612 : mode_mmin);
2613 max = (up - down) / inc + 1;
2614 if (!desc->infinite
2615 && !desc->assumptions)
2616 record_niter_bound (loop, max, false, true);
2618 if (iv0.step == const0_rtx)
2620 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2621 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2623 else
2625 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2626 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2629 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2630 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2631 assumption = simplify_gen_relational (reverse_condition (cond),
2632 SImode, mode, tmp0, tmp1);
2633 if (assumption == const_true_rtx)
2634 goto zero_iter_simplify;
2635 else if (assumption != const0_rtx)
2636 desc->noloop_assumptions =
2637 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2638 cond = NE;
2642 /* Count the number of iterations. */
2643 if (cond == NE)
2645 /* Everything we do here is just arithmetics modulo size of mode. This
2646 makes us able to do more involved computations of number of iterations
2647 than in other cases. First transform the condition into shape
2648 s * i <> c, with s positive. */
2649 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2650 iv0.base = const0_rtx;
2651 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2652 iv1.step = const0_rtx;
2653 if (INTVAL (iv0.step) < 0)
2655 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2656 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
2658 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2660 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2661 is infinite. Otherwise, the number of iterations is
2662 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2663 s = INTVAL (iv0.step); d = 1;
2664 while (s % 2 != 1)
2666 s /= 2;
2667 d *= 2;
2668 size--;
2670 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
2672 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2673 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
2674 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2675 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2677 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
2678 inv = inverse (s, size);
2679 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2680 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2682 else
2684 if (iv1.step == const0_rtx)
2685 /* Condition in shape a + s * i <= b
2686 We must know that b + s does not overflow and a <= b + s and then we
2687 can compute number of iterations as (b + s - a) / s. (It might
2688 seem that we in fact could be more clever about testing the b + s
2689 overflow condition using some information about b - a mod s,
2690 but it was already taken into account during LE -> NE transform). */
2692 step = iv0.step;
2693 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2694 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2696 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2697 lowpart_subreg (mode, step,
2698 comp_mode));
2699 if (step_is_pow2)
2701 rtx t0, t1;
2703 /* If s is power of 2, we know that the loop is infinite if
2704 a % s <= b % s and b + s overflows. */
2705 assumption = simplify_gen_relational (reverse_condition (cond),
2706 SImode, mode,
2707 tmp1, bound);
2709 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2710 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2711 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2712 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2713 desc->infinite =
2714 alloc_EXPR_LIST (0, assumption, desc->infinite);
2716 else
2718 assumption = simplify_gen_relational (cond, SImode, mode,
2719 tmp1, bound);
2720 desc->assumptions =
2721 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2724 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2725 tmp = lowpart_subreg (mode, tmp, comp_mode);
2726 assumption = simplify_gen_relational (reverse_condition (cond),
2727 SImode, mode, tmp0, tmp);
2729 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2730 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2732 else
2734 /* Condition in shape a <= b - s * i
2735 We must know that a - s does not overflow and a - s <= b and then
2736 we can again compute number of iterations as (b - (a - s)) / s. */
2737 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2738 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2739 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2741 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2742 lowpart_subreg (mode, step, comp_mode));
2743 if (step_is_pow2)
2745 rtx t0, t1;
2747 /* If s is power of 2, we know that the loop is infinite if
2748 a % s <= b % s and a - s overflows. */
2749 assumption = simplify_gen_relational (reverse_condition (cond),
2750 SImode, mode,
2751 bound, tmp0);
2753 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2754 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2755 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2756 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2757 desc->infinite =
2758 alloc_EXPR_LIST (0, assumption, desc->infinite);
2760 else
2762 assumption = simplify_gen_relational (cond, SImode, mode,
2763 bound, tmp0);
2764 desc->assumptions =
2765 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2768 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2769 tmp = lowpart_subreg (mode, tmp, comp_mode);
2770 assumption = simplify_gen_relational (reverse_condition (cond),
2771 SImode, mode,
2772 tmp, tmp1);
2773 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2774 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2776 if (assumption == const_true_rtx)
2777 goto zero_iter_simplify;
2778 else if (assumption != const0_rtx)
2779 desc->noloop_assumptions =
2780 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2781 delta = simplify_gen_binary (UDIV, mode, delta, step);
2782 desc->niter_expr = delta;
2785 old_niter = desc->niter_expr;
2787 simplify_using_initial_values (loop, AND, &desc->assumptions);
2788 if (desc->assumptions
2789 && XEXP (desc->assumptions, 0) == const0_rtx)
2790 goto fail;
2791 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2792 simplify_using_initial_values (loop, IOR, &desc->infinite);
2793 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2795 /* Rerun the simplification. Consider code (created by copying loop headers)
2797 i = 0;
2799 if (0 < n)
2803 i++;
2804 } while (i < n);
2807 The first pass determines that i = 0, the second pass uses it to eliminate
2808 noloop assumption. */
2810 simplify_using_initial_values (loop, AND, &desc->assumptions);
2811 if (desc->assumptions
2812 && XEXP (desc->assumptions, 0) == const0_rtx)
2813 goto fail;
2814 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2815 simplify_using_initial_values (loop, IOR, &desc->infinite);
2816 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2818 if (desc->noloop_assumptions
2819 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2820 goto zero_iter;
2822 if (CONST_INT_P (desc->niter_expr))
2824 uint64_t val = INTVAL (desc->niter_expr);
2826 desc->const_iter = true;
2827 desc->niter = val & GET_MODE_MASK (desc->mode);
2828 if (!desc->infinite
2829 && !desc->assumptions)
2830 record_niter_bound (loop, desc->niter, false, true);
2832 else
2834 max = determine_max_iter (loop, desc, old_niter);
2835 if (!max)
2836 goto zero_iter_simplify;
2837 if (!desc->infinite
2838 && !desc->assumptions)
2839 record_niter_bound (loop, max, false, true);
2841 /* simplify_using_initial_values does a copy propagation on the registers
2842 in the expression for the number of iterations. This prolongs life
2843 ranges of registers and increases register pressure, and usually
2844 brings no gain (and if it happens to do, the cse pass will take care
2845 of it anyway). So prevent this behavior, unless it enabled us to
2846 derive that the number of iterations is a constant. */
2847 desc->niter_expr = old_niter;
2850 return;
2852 zero_iter_simplify:
2853 /* Simplify the assumptions. */
2854 simplify_using_initial_values (loop, AND, &desc->assumptions);
2855 if (desc->assumptions
2856 && XEXP (desc->assumptions, 0) == const0_rtx)
2857 goto fail;
2858 simplify_using_initial_values (loop, IOR, &desc->infinite);
2860 /* Fallthru. */
2861 zero_iter:
2862 desc->const_iter = true;
2863 desc->niter = 0;
2864 record_niter_bound (loop, 0, true, true);
2865 desc->noloop_assumptions = NULL_RTX;
2866 desc->niter_expr = const0_rtx;
2867 return;
2869 fail:
2870 desc->simple_p = false;
2871 return;
2874 /* Checks whether E is a simple exit from LOOP and stores its description
2875 into DESC. */
2877 static void
2878 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2880 basic_block exit_bb;
2881 rtx condition;
2882 rtx_insn *at;
2883 edge ein;
2885 exit_bb = e->src;
2886 desc->simple_p = false;
2888 /* It must belong directly to the loop. */
2889 if (exit_bb->loop_father != loop)
2890 return;
2892 /* It must be tested (at least) once during any iteration. */
2893 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2894 return;
2896 /* It must end in a simple conditional jump. */
2897 if (!any_condjump_p (BB_END (exit_bb)))
2898 return;
2900 ein = EDGE_SUCC (exit_bb, 0);
2901 if (ein == e)
2902 ein = EDGE_SUCC (exit_bb, 1);
2904 desc->out_edge = e;
2905 desc->in_edge = ein;
2907 /* Test whether the condition is suitable. */
2908 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2909 return;
2911 if (ein->flags & EDGE_FALLTHRU)
2913 condition = reversed_condition (condition);
2914 if (!condition)
2915 return;
2918 /* Check that we are able to determine number of iterations and fill
2919 in information about it. */
2920 iv_number_of_iterations (loop, at, condition, desc);
2923 /* Finds a simple exit of LOOP and stores its description into DESC. */
2925 void
2926 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2928 unsigned i;
2929 basic_block *body;
2930 edge e;
2931 struct niter_desc act;
2932 bool any = false;
2933 edge_iterator ei;
2935 desc->simple_p = false;
2936 body = get_loop_body (loop);
2938 for (i = 0; i < loop->num_nodes; i++)
2940 FOR_EACH_EDGE (e, ei, body[i]->succs)
2942 if (flow_bb_inside_loop_p (loop, e->dest))
2943 continue;
2945 check_simple_exit (loop, e, &act);
2946 if (!act.simple_p)
2947 continue;
2949 if (!any)
2950 any = true;
2951 else
2953 /* Prefer constant iterations; the less the better. */
2954 if (!act.const_iter
2955 || (desc->const_iter && act.niter >= desc->niter))
2956 continue;
2958 /* Also if the actual exit may be infinite, while the old one
2959 not, prefer the old one. */
2960 if (act.infinite && !desc->infinite)
2961 continue;
2964 *desc = act;
2968 if (dump_file)
2970 if (desc->simple_p)
2972 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
2973 fprintf (dump_file, " simple exit %d -> %d\n",
2974 desc->out_edge->src->index,
2975 desc->out_edge->dest->index);
2976 if (desc->assumptions)
2978 fprintf (dump_file, " assumptions: ");
2979 print_rtl (dump_file, desc->assumptions);
2980 fprintf (dump_file, "\n");
2982 if (desc->noloop_assumptions)
2984 fprintf (dump_file, " does not roll if: ");
2985 print_rtl (dump_file, desc->noloop_assumptions);
2986 fprintf (dump_file, "\n");
2988 if (desc->infinite)
2990 fprintf (dump_file, " infinite if: ");
2991 print_rtl (dump_file, desc->infinite);
2992 fprintf (dump_file, "\n");
2995 fprintf (dump_file, " number of iterations: ");
2996 print_rtl (dump_file, desc->niter_expr);
2997 fprintf (dump_file, "\n");
2999 fprintf (dump_file, " upper bound: %li\n",
3000 (long)get_max_loop_iterations_int (loop));
3001 fprintf (dump_file, " likely upper bound: %li\n",
3002 (long)get_likely_max_loop_iterations_int (loop));
3003 fprintf (dump_file, " realistic bound: %li\n",
3004 (long)get_estimated_loop_iterations_int (loop));
3006 else
3007 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
3010 free (body);
3013 /* Creates a simple loop description of LOOP if it was not computed
3014 already. */
3016 struct niter_desc *
3017 get_simple_loop_desc (struct loop *loop)
3019 struct niter_desc *desc = simple_loop_desc (loop);
3021 if (desc)
3022 return desc;
3024 /* At least desc->infinite is not always initialized by
3025 find_simple_loop_exit. */
3026 desc = ggc_cleared_alloc<niter_desc> ();
3027 iv_analysis_loop_init (loop);
3028 find_simple_exit (loop, desc);
3029 loop->simple_loop_desc = desc;
3030 return desc;
3033 /* Releases simple loop description for LOOP. */
3035 void
3036 free_simple_loop_desc (struct loop *loop)
3038 struct niter_desc *desc = simple_loop_desc (loop);
3040 if (!desc)
3041 return;
3043 ggc_free (desc);
3044 loop->simple_loop_desc = NULL;