2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / loop-iv.c
blobc0d6a1d3f3fc85a686f808a6c66f0c51fd4544fd
1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004-2015 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 "tm.h"
54 #include "rtl.h"
55 #include "hard-reg-set.h"
56 #include "obstack.h"
57 #include "predict.h"
58 #include "function.h"
59 #include "dominance.h"
60 #include "cfg.h"
61 #include "basic-block.h"
62 #include "cfgloop.h"
63 #include "symtab.h"
64 #include "flags.h"
65 #include "alias.h"
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"
75 #include "expr.h"
76 #include "intl.h"
77 #include "diagnostic-core.h"
78 #include "df.h"
79 #include "dumpfile.h"
80 #include "rtl-iter.h"
82 /* Possible return values of iv_get_reaching_def. */
84 enum iv_grd_result
86 /* More than one reaching def, or reaching def that does not
87 dominate the use. */
88 GRD_INVALID,
90 /* The use is trivial invariant of the loop, i.e. is not changed
91 inside the loop. */
92 GRD_INVARIANT,
94 /* The use is reached by initial value and a value from the
95 previous iteration. */
96 GRD_MAYBE_BIV,
98 /* The use has single dominating def. */
99 GRD_SINGLE_DOM
102 /* Information about a biv. */
104 struct biv_entry
106 unsigned regno; /* The register of the biv. */
107 struct rtx_iv iv; /* Value of the biv. */
110 static bool clean_slate = true;
112 static unsigned int iv_ref_table_size = 0;
114 /* Table of rtx_ivs indexed by the df_ref uid field. */
115 static struct rtx_iv ** iv_ref_table;
117 /* Induction variable stored at the reference. */
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)
121 /* The current loop. */
123 static struct loop *current_loop;
125 /* Hashtable helper. */
127 struct biv_entry_hasher : typed_free_remove <biv_entry>
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 *);
135 /* Returns hash value for biv B. */
137 inline hashval_t
138 biv_entry_hasher::hash (const biv_entry *b)
140 return b->regno;
143 /* Compares biv B and register R. */
145 inline bool
146 biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
148 return b->regno == REGNO (r);
151 /* Bivs of the current loop. */
153 static hash_table<biv_entry_hasher> *bivs;
155 static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
157 /* Return the RTX code corresponding to the IV extend code EXTEND. */
158 static inline enum rtx_code
159 iv_extend_to_rtx_code (enum iv_extend_code extend)
161 switch (extend)
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;
170 gcc_unreachable ();
173 /* Dumps information about IV to FILE. */
175 extern void dump_iv_info (FILE *, struct rtx_iv *);
176 void
177 dump_iv_info (FILE *file, struct rtx_iv *iv)
179 if (!iv->base)
181 fprintf (file, "not simple");
182 return;
185 if (iv->step == const0_rtx
186 && !iv->first_special)
187 fprintf (file, "invariant ");
189 print_rtl (file, iv->base);
190 if (iv->step != const0_rtx)
192 fprintf (file, " + ");
193 print_rtl (file, iv->step);
194 fprintf (file, " * iteration");
196 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
198 if (iv->mode != iv->extend_mode)
199 fprintf (file, " %s to %s",
200 rtx_name[iv_extend_to_rtx_code (iv->extend)],
201 GET_MODE_NAME (iv->extend_mode));
203 if (iv->mult != const1_rtx)
205 fprintf (file, " * ");
206 print_rtl (file, iv->mult);
208 if (iv->delta != const0_rtx)
210 fprintf (file, " + ");
211 print_rtl (file, iv->delta);
213 if (iv->first_special)
214 fprintf (file, " (first special)");
217 /* Generates a subreg to get the least significant part of EXPR (in mode
218 INNER_MODE) to OUTER_MODE. */
221 lowpart_subreg (machine_mode outer_mode, rtx expr,
222 machine_mode inner_mode)
224 return simplify_gen_subreg (outer_mode, expr, inner_mode,
225 subreg_lowpart_offset (outer_mode, inner_mode));
228 static void
229 check_iv_ref_table_size (void)
231 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
233 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
234 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
235 memset (&iv_ref_table[iv_ref_table_size], 0,
236 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
237 iv_ref_table_size = new_size;
242 /* Checks whether REG is a well-behaved register. */
244 static bool
245 simple_reg_p (rtx reg)
247 unsigned r;
249 if (GET_CODE (reg) == SUBREG)
251 if (!subreg_lowpart_p (reg))
252 return false;
253 reg = SUBREG_REG (reg);
256 if (!REG_P (reg))
257 return false;
259 r = REGNO (reg);
260 if (HARD_REGISTER_NUM_P (r))
261 return false;
263 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
264 return false;
266 return true;
269 /* Clears the information about ivs stored in df. */
271 static void
272 clear_iv_info (void)
274 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
275 struct rtx_iv *iv;
277 check_iv_ref_table_size ();
278 for (i = 0; i < n_defs; i++)
280 iv = iv_ref_table[i];
281 if (iv)
283 free (iv);
284 iv_ref_table[i] = NULL;
288 bivs->empty ();
292 /* Prepare the data for an induction variable analysis of a LOOP. */
294 void
295 iv_analysis_loop_init (struct loop *loop)
297 current_loop = loop;
299 /* Clear the information from the analysis of the previous loop. */
300 if (clean_slate)
302 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
303 bivs = new hash_table<biv_entry_hasher> (10);
304 clean_slate = false;
306 else
307 clear_iv_info ();
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 ();
313 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
314 df_chain_add_problem (DF_UD_CHAIN);
315 df_note_add_problem ();
316 df_analyze_loop (loop);
317 if (dump_file)
318 df_dump_region (dump_file);
320 check_iv_ref_table_size ();
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
325 dominating the latch. If REG has no definition in loop, DEF
326 is set to NULL and true is returned. */
328 static bool
329 latch_dominating_def (rtx reg, df_ref *def)
331 df_ref single_rd = NULL, adef;
332 unsigned regno = REGNO (reg);
333 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
335 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
337 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
338 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
339 continue;
341 /* More than one reaching definition. */
342 if (single_rd)
343 return false;
345 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
346 return false;
348 single_rd = adef;
351 *def = single_rd;
352 return true;
355 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
357 static enum iv_grd_result
358 iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
360 df_ref use, adef;
361 basic_block def_bb, use_bb;
362 rtx_insn *def_insn;
363 bool dom_p;
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));
372 use = df_find_use (insn, reg);
373 gcc_assert (use != NULL);
375 if (!DF_REF_CHAIN (use))
376 return GRD_INVARIANT;
378 /* More than one reaching def. */
379 if (DF_REF_CHAIN (use)->next)
380 return GRD_INVALID;
382 adef = DF_REF_CHAIN (use)->ref;
384 /* We do not handle setting only part of the register. */
385 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
386 return GRD_INVALID;
388 def_insn = DF_REF_INSN (adef);
389 def_bb = DF_REF_BB (adef);
390 use_bb = BLOCK_FOR_INSN (insn);
392 if (use_bb == def_bb)
393 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
394 else
395 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
397 if (dom_p)
399 *def = adef;
400 return GRD_SINGLE_DOM;
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
405 latch. */
406 if (just_once_each_iteration_p (current_loop, def_bb))
407 return GRD_MAYBE_BIV;
409 return GRD_INVALID;
412 /* Sets IV to invariant CST in MODE. Always returns true (just for
413 consistency with other iv manipulation functions that may fail). */
415 static bool
416 iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
418 if (mode == VOIDmode)
419 mode = GET_MODE (cst);
421 iv->mode = mode;
422 iv->base = cst;
423 iv->step = const0_rtx;
424 iv->first_special = false;
425 iv->extend = IV_UNKNOWN_EXTEND;
426 iv->extend_mode = iv->mode;
427 iv->delta = const0_rtx;
428 iv->mult = const1_rtx;
430 return true;
433 /* Evaluates application of subreg to MODE on IV. */
435 static bool
436 iv_subreg (struct rtx_iv *iv, machine_mode mode)
438 /* If iv is invariant, just calculate the new value. */
439 if (iv->step == const0_rtx
440 && !iv->first_special)
442 rtx val = get_iv_value (iv, const0_rtx);
443 val = lowpart_subreg (mode, val,
444 iv->extend == IV_UNKNOWN_EXTEND
445 ? iv->mode : iv->extend_mode);
447 iv->base = val;
448 iv->extend = IV_UNKNOWN_EXTEND;
449 iv->mode = iv->extend_mode = mode;
450 iv->delta = const0_rtx;
451 iv->mult = const1_rtx;
452 return true;
455 if (iv->extend_mode == mode)
456 return true;
458 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
459 return false;
461 iv->extend = IV_UNKNOWN_EXTEND;
462 iv->mode = mode;
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;
472 return true;
475 /* Evaluates application of EXTEND to MODE on IV. */
477 static bool
478 iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
480 /* If iv is invariant, just calculate the new value. */
481 if (iv->step == const0_rtx
482 && !iv->first_special)
484 rtx val = get_iv_value (iv, const0_rtx);
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);
489 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
490 val,
491 iv->extend == extend
492 ? iv->extend_mode : iv->mode);
493 iv->base = val;
494 iv->extend = IV_UNKNOWN_EXTEND;
495 iv->mode = iv->extend_mode = mode;
496 iv->delta = const0_rtx;
497 iv->mult = const1_rtx;
498 return true;
501 if (mode != iv->extend_mode)
502 return false;
504 if (iv->extend != IV_UNKNOWN_EXTEND
505 && iv->extend != extend)
506 return false;
508 iv->extend = extend;
510 return true;
513 /* Evaluates negation of IV. */
515 static bool
516 iv_neg (struct rtx_iv *iv)
518 if (iv->extend == IV_UNKNOWN_EXTEND)
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);
525 else
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);
533 return true;
536 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
538 static bool
539 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
541 machine_mode mode;
542 rtx arg;
544 /* Extend the constant to extend_mode of the other operand if necessary. */
545 if (iv0->extend == IV_UNKNOWN_EXTEND
546 && iv0->mode == iv0->extend_mode
547 && iv0->step == const0_rtx
548 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
550 iv0->extend_mode = iv1->extend_mode;
551 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
552 iv0->base, iv0->mode);
554 if (iv1->extend == IV_UNKNOWN_EXTEND
555 && iv1->mode == iv1->extend_mode
556 && iv1->step == const0_rtx
557 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
559 iv1->extend_mode = iv0->extend_mode;
560 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
561 iv1->base, iv1->mode);
564 mode = iv0->extend_mode;
565 if (mode != iv1->extend_mode)
566 return false;
568 if (iv0->extend == IV_UNKNOWN_EXTEND
569 && iv1->extend == IV_UNKNOWN_EXTEND)
571 if (iv0->mode != iv1->mode)
572 return false;
574 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
575 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
577 return true;
580 /* Handle addition of constant. */
581 if (iv1->extend == IV_UNKNOWN_EXTEND
582 && iv1->mode == mode
583 && iv1->step == const0_rtx)
585 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
586 return true;
589 if (iv0->extend == IV_UNKNOWN_EXTEND
590 && iv0->mode == mode
591 && iv0->step == const0_rtx)
593 arg = iv0->base;
594 *iv0 = *iv1;
595 if (op == MINUS
596 && !iv_neg (iv0))
597 return false;
599 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
600 return true;
603 return false;
606 /* Evaluates multiplication of IV by constant CST. */
608 static bool
609 iv_mult (struct rtx_iv *iv, rtx mby)
611 machine_mode mode = iv->extend_mode;
613 if (GET_MODE (mby) != VOIDmode
614 && GET_MODE (mby) != mode)
615 return false;
617 if (iv->extend == IV_UNKNOWN_EXTEND)
619 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
620 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
622 else
624 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
625 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
628 return true;
631 /* Evaluates shift of IV by constant CST. */
633 static bool
634 iv_shift (struct rtx_iv *iv, rtx mby)
636 machine_mode mode = iv->extend_mode;
638 if (GET_MODE (mby) != VOIDmode
639 && GET_MODE (mby) != mode)
640 return false;
642 if (iv->extend == IV_UNKNOWN_EXTEND)
644 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
645 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
647 else
649 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
650 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
653 return true;
656 /* The recursive part of get_biv_step. Gets the value of the single value
657 defined by DEF wrto initial value of REG inside loop, in shape described
658 at get_biv_step. */
660 static bool
661 get_biv_step_1 (df_ref def, rtx reg,
662 rtx *inner_step, machine_mode *inner_mode,
663 enum iv_extend_code *extend, machine_mode outer_mode,
664 rtx *outer_step)
666 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
667 rtx next, nextr;
668 enum rtx_code code;
669 rtx_insn *insn = DF_REF_INSN (def);
670 df_ref next_def;
671 enum iv_grd_result res;
673 set = single_set (insn);
674 if (!set)
675 return false;
677 rhs = find_reg_equal_equiv_note (insn);
678 if (rhs)
679 rhs = XEXP (rhs, 0);
680 else
681 rhs = SET_SRC (set);
683 code = GET_CODE (rhs);
684 switch (code)
686 case SUBREG:
687 case REG:
688 next = rhs;
689 break;
691 case PLUS:
692 case MINUS:
693 op0 = XEXP (rhs, 0);
694 op1 = XEXP (rhs, 1);
696 if (code == PLUS && CONSTANT_P (op0))
697 std::swap (op0, op1);
699 if (!simple_reg_p (op0)
700 || !CONSTANT_P (op1))
701 return false;
703 if (GET_MODE (rhs) != outer_mode)
705 /* ppc64 uses expressions like
707 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
709 this is equivalent to
711 (set x':DI (plus:DI y:DI 1))
712 (set x:SI (subreg:SI (x':DI)). */
713 if (GET_CODE (op0) != SUBREG)
714 return false;
715 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
716 return false;
719 next = op0;
720 break;
722 case SIGN_EXTEND:
723 case ZERO_EXTEND:
724 if (GET_MODE (rhs) != outer_mode)
725 return false;
727 op0 = XEXP (rhs, 0);
728 if (!simple_reg_p (op0))
729 return false;
731 next = op0;
732 break;
734 default:
735 return false;
738 if (GET_CODE (next) == SUBREG)
740 if (!subreg_lowpart_p (next))
741 return false;
743 nextr = SUBREG_REG (next);
744 if (GET_MODE (nextr) != outer_mode)
745 return false;
747 else
748 nextr = next;
750 res = iv_get_reaching_def (insn, nextr, &next_def);
752 if (res == GRD_INVALID || res == GRD_INVARIANT)
753 return false;
755 if (res == GRD_MAYBE_BIV)
757 if (!rtx_equal_p (nextr, reg))
758 return false;
760 *inner_step = const0_rtx;
761 *extend = IV_UNKNOWN_EXTEND;
762 *inner_mode = outer_mode;
763 *outer_step = const0_rtx;
765 else if (!get_biv_step_1 (next_def, reg,
766 inner_step, inner_mode, extend, outer_mode,
767 outer_step))
768 return false;
770 if (GET_CODE (next) == SUBREG)
772 machine_mode amode = GET_MODE (next);
774 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
775 return false;
777 *inner_mode = amode;
778 *inner_step = simplify_gen_binary (PLUS, outer_mode,
779 *inner_step, *outer_step);
780 *outer_step = const0_rtx;
781 *extend = IV_UNKNOWN_EXTEND;
784 switch (code)
786 case REG:
787 case SUBREG:
788 break;
790 case PLUS:
791 case MINUS:
792 if (*inner_mode == outer_mode
793 /* See comment in previous switch. */
794 || GET_MODE (rhs) != outer_mode)
795 *inner_step = simplify_gen_binary (code, outer_mode,
796 *inner_step, op1);
797 else
798 *outer_step = simplify_gen_binary (code, outer_mode,
799 *outer_step, op1);
800 break;
802 case SIGN_EXTEND:
803 case ZERO_EXTEND:
804 gcc_assert (GET_MODE (op0) == *inner_mode
805 && *extend == IV_UNKNOWN_EXTEND
806 && *outer_step == const0_rtx);
808 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
809 break;
811 default:
812 return false;
815 return true;
818 /* Gets the operation on register REG inside loop, in shape
820 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
822 If the operation cannot be described in this shape, return false.
823 LAST_DEF is the definition of REG that dominates loop latch. */
825 static bool
826 get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
827 machine_mode *inner_mode, enum iv_extend_code *extend,
828 machine_mode *outer_mode, rtx *outer_step)
830 *outer_mode = GET_MODE (reg);
832 if (!get_biv_step_1 (last_def, reg,
833 inner_step, inner_mode, extend, *outer_mode,
834 outer_step))
835 return false;
837 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
838 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
840 return true;
843 /* Records information that DEF is induction variable IV. */
845 static void
846 record_iv (df_ref def, struct rtx_iv *iv)
848 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
850 *recorded_iv = *iv;
851 check_iv_ref_table_size ();
852 DF_REF_IV_SET (def, recorded_iv);
855 /* If DEF was already analyzed for bivness, store the description of the biv to
856 IV and return true. Otherwise return false. */
858 static bool
859 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
861 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
863 if (!biv)
864 return false;
866 *iv = biv->iv;
867 return true;
870 static void
871 record_biv (rtx def, struct rtx_iv *iv)
873 struct biv_entry *biv = XNEW (struct biv_entry);
874 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
876 biv->regno = REGNO (def);
877 biv->iv = *iv;
878 gcc_assert (!*slot);
879 *slot = biv;
882 /* Determines whether DEF is a biv and if so, stores its description
883 to *IV. */
885 static bool
886 iv_analyze_biv (rtx def, struct rtx_iv *iv)
888 rtx inner_step, outer_step;
889 machine_mode inner_mode, outer_mode;
890 enum iv_extend_code extend;
891 df_ref last_def;
893 if (dump_file)
895 fprintf (dump_file, "Analyzing ");
896 print_rtl (dump_file, def);
897 fprintf (dump_file, " for bivness.\n");
900 if (!REG_P (def))
902 if (!CONSTANT_P (def))
903 return false;
905 return iv_constant (iv, def, VOIDmode);
908 if (!latch_dominating_def (def, &last_def))
910 if (dump_file)
911 fprintf (dump_file, " not simple.\n");
912 return false;
915 if (!last_def)
916 return iv_constant (iv, def, VOIDmode);
918 if (analyzed_for_bivness_p (def, iv))
920 if (dump_file)
921 fprintf (dump_file, " already analysed.\n");
922 return iv->base != NULL_RTX;
925 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
926 &outer_mode, &outer_step))
928 iv->base = NULL_RTX;
929 goto end;
932 /* Loop transforms base to es (base + inner_step) + outer_step,
933 where es means extend of subreg between inner_mode and outer_mode.
934 The corresponding induction variable is
936 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
938 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
939 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
940 iv->mode = inner_mode;
941 iv->extend_mode = outer_mode;
942 iv->extend = extend;
943 iv->mult = const1_rtx;
944 iv->delta = outer_step;
945 iv->first_special = inner_mode != outer_mode;
947 end:
948 if (dump_file)
950 fprintf (dump_file, " ");
951 dump_iv_info (dump_file, iv);
952 fprintf (dump_file, "\n");
955 record_biv (def, iv);
956 return iv->base != NULL_RTX;
959 /* Analyzes expression RHS used at INSN and stores the result to *IV.
960 The mode of the induction variable is MODE. */
962 bool
963 iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
964 struct rtx_iv *iv)
966 rtx mby = NULL_RTX;
967 rtx op0 = NULL_RTX, op1 = NULL_RTX;
968 struct rtx_iv iv0, iv1;
969 enum rtx_code code = GET_CODE (rhs);
970 machine_mode omode = mode;
972 iv->mode = VOIDmode;
973 iv->base = NULL_RTX;
974 iv->step = NULL_RTX;
976 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
978 if (CONSTANT_P (rhs)
979 || REG_P (rhs)
980 || code == SUBREG)
982 if (!iv_analyze_op (insn, rhs, iv))
983 return false;
985 if (iv->mode == VOIDmode)
987 iv->mode = mode;
988 iv->extend_mode = mode;
991 return true;
994 switch (code)
996 case REG:
997 op0 = rhs;
998 break;
1000 case SIGN_EXTEND:
1001 case ZERO_EXTEND:
1002 case NEG:
1003 op0 = XEXP (rhs, 0);
1004 omode = GET_MODE (op0);
1005 break;
1007 case PLUS:
1008 case MINUS:
1009 op0 = XEXP (rhs, 0);
1010 op1 = XEXP (rhs, 1);
1011 break;
1013 case MULT:
1014 op0 = XEXP (rhs, 0);
1015 mby = XEXP (rhs, 1);
1016 if (!CONSTANT_P (mby))
1017 std::swap (op0, mby);
1018 if (!CONSTANT_P (mby))
1019 return false;
1020 break;
1022 case ASHIFT:
1023 op0 = XEXP (rhs, 0);
1024 mby = XEXP (rhs, 1);
1025 if (!CONSTANT_P (mby))
1026 return false;
1027 break;
1029 default:
1030 return false;
1033 if (op0
1034 && !iv_analyze_expr (insn, op0, omode, &iv0))
1035 return false;
1037 if (op1
1038 && !iv_analyze_expr (insn, op1, omode, &iv1))
1039 return false;
1041 switch (code)
1043 case SIGN_EXTEND:
1044 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1045 return false;
1046 break;
1048 case ZERO_EXTEND:
1049 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
1050 return false;
1051 break;
1053 case NEG:
1054 if (!iv_neg (&iv0))
1055 return false;
1056 break;
1058 case PLUS:
1059 case MINUS:
1060 if (!iv_add (&iv0, &iv1, code))
1061 return false;
1062 break;
1064 case MULT:
1065 if (!iv_mult (&iv0, mby))
1066 return false;
1067 break;
1069 case ASHIFT:
1070 if (!iv_shift (&iv0, mby))
1071 return false;
1072 break;
1074 default:
1075 break;
1078 *iv = iv0;
1079 return iv->base != NULL_RTX;
1082 /* Analyzes iv DEF and stores the result to *IV. */
1084 static bool
1085 iv_analyze_def (df_ref def, struct rtx_iv *iv)
1087 rtx_insn *insn = DF_REF_INSN (def);
1088 rtx reg = DF_REF_REG (def);
1089 rtx set, rhs;
1091 if (dump_file)
1093 fprintf (dump_file, "Analyzing def of ");
1094 print_rtl (dump_file, reg);
1095 fprintf (dump_file, " in insn ");
1096 print_rtl_single (dump_file, insn);
1099 check_iv_ref_table_size ();
1100 if (DF_REF_IV (def))
1102 if (dump_file)
1103 fprintf (dump_file, " already analysed.\n");
1104 *iv = *DF_REF_IV (def);
1105 return iv->base != NULL_RTX;
1108 iv->mode = VOIDmode;
1109 iv->base = NULL_RTX;
1110 iv->step = NULL_RTX;
1112 if (!REG_P (reg))
1113 return false;
1115 set = single_set (insn);
1116 if (!set)
1117 return false;
1119 if (!REG_P (SET_DEST (set)))
1120 return false;
1122 gcc_assert (SET_DEST (set) == reg);
1123 rhs = find_reg_equal_equiv_note (insn);
1124 if (rhs)
1125 rhs = XEXP (rhs, 0);
1126 else
1127 rhs = SET_SRC (set);
1129 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1130 record_iv (def, iv);
1132 if (dump_file)
1134 print_rtl (dump_file, reg);
1135 fprintf (dump_file, " in insn ");
1136 print_rtl_single (dump_file, insn);
1137 fprintf (dump_file, " is ");
1138 dump_iv_info (dump_file, iv);
1139 fprintf (dump_file, "\n");
1142 return iv->base != NULL_RTX;
1145 /* Analyzes operand OP of INSN and stores the result to *IV. */
1147 static bool
1148 iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
1150 df_ref def = NULL;
1151 enum iv_grd_result res;
1153 if (dump_file)
1155 fprintf (dump_file, "Analyzing operand ");
1156 print_rtl (dump_file, op);
1157 fprintf (dump_file, " of insn ");
1158 print_rtl_single (dump_file, insn);
1161 if (function_invariant_p (op))
1162 res = GRD_INVARIANT;
1163 else if (GET_CODE (op) == SUBREG)
1165 if (!subreg_lowpart_p (op))
1166 return false;
1168 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1169 return false;
1171 return iv_subreg (iv, GET_MODE (op));
1173 else
1175 res = iv_get_reaching_def (insn, op, &def);
1176 if (res == GRD_INVALID)
1178 if (dump_file)
1179 fprintf (dump_file, " not simple.\n");
1180 return false;
1184 if (res == GRD_INVARIANT)
1186 iv_constant (iv, op, VOIDmode);
1188 if (dump_file)
1190 fprintf (dump_file, " ");
1191 dump_iv_info (dump_file, iv);
1192 fprintf (dump_file, "\n");
1194 return true;
1197 if (res == GRD_MAYBE_BIV)
1198 return iv_analyze_biv (op, iv);
1200 return iv_analyze_def (def, iv);
1203 /* Analyzes value VAL at INSN and stores the result to *IV. */
1205 bool
1206 iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
1208 rtx reg;
1210 /* We must find the insn in that val is used, so that we get to UD chains.
1211 Since the function is sometimes called on result of get_condition,
1212 this does not necessarily have to be directly INSN; scan also the
1213 following insns. */
1214 if (simple_reg_p (val))
1216 if (GET_CODE (val) == SUBREG)
1217 reg = SUBREG_REG (val);
1218 else
1219 reg = val;
1221 while (!df_find_use (insn, reg))
1222 insn = NEXT_INSN (insn);
1225 return iv_analyze_op (insn, val, iv);
1228 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1230 bool
1231 iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
1233 df_ref adef;
1235 adef = df_find_def (insn, def);
1236 if (!adef)
1237 return false;
1239 return iv_analyze_def (adef, iv);
1242 /* Checks whether definition of register REG in INSN is a basic induction
1243 variable. IV analysis must have been initialized (via a call to
1244 iv_analysis_loop_init) for this function to produce a result. */
1246 bool
1247 biv_p (rtx_insn *insn, rtx reg)
1249 struct rtx_iv iv;
1250 df_ref def, last_def;
1252 if (!simple_reg_p (reg))
1253 return false;
1255 def = df_find_def (insn, reg);
1256 gcc_assert (def != NULL);
1257 if (!latch_dominating_def (reg, &last_def))
1258 return false;
1259 if (last_def != def)
1260 return false;
1262 if (!iv_analyze_biv (reg, &iv))
1263 return false;
1265 return iv.step != const0_rtx;
1268 /* Calculates value of IV at ITERATION-th iteration. */
1271 get_iv_value (struct rtx_iv *iv, rtx iteration)
1273 rtx val;
1275 /* We would need to generate some if_then_else patterns, and so far
1276 it is not needed anywhere. */
1277 gcc_assert (!iv->first_special);
1279 if (iv->step != const0_rtx && iteration != const0_rtx)
1280 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1281 simplify_gen_binary (MULT, iv->extend_mode,
1282 iv->step, iteration));
1283 else
1284 val = iv->base;
1286 if (iv->extend_mode == iv->mode)
1287 return val;
1289 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1291 if (iv->extend == IV_UNKNOWN_EXTEND)
1292 return val;
1294 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1295 iv->extend_mode, val, iv->mode);
1296 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1297 simplify_gen_binary (MULT, iv->extend_mode,
1298 iv->mult, val));
1300 return val;
1303 /* Free the data for an induction variable analysis. */
1305 void
1306 iv_analysis_done (void)
1308 if (!clean_slate)
1310 clear_iv_info ();
1311 clean_slate = true;
1312 df_finish_pass (true);
1313 delete bivs;
1314 bivs = NULL;
1315 free (iv_ref_table);
1316 iv_ref_table = NULL;
1317 iv_ref_table_size = 0;
1321 /* Computes inverse to X modulo (1 << MOD). */
1323 static uint64_t
1324 inverse (uint64_t x, int mod)
1326 uint64_t mask =
1327 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1328 uint64_t rslt = 1;
1329 int i;
1331 for (i = 0; i < mod - 1; i++)
1333 rslt = (rslt * x) & mask;
1334 x = (x * x) & mask;
1337 return rslt;
1340 /* Checks whether any register in X is in set ALT. */
1342 static bool
1343 altered_reg_used (const_rtx x, bitmap alt)
1345 subrtx_iterator::array_type array;
1346 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1348 const_rtx x = *iter;
1349 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1350 return true;
1352 return false;
1355 /* Marks registers altered by EXPR in set ALT. */
1357 static void
1358 mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
1360 if (GET_CODE (expr) == SUBREG)
1361 expr = SUBREG_REG (expr);
1362 if (!REG_P (expr))
1363 return;
1365 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
1368 /* Checks whether RHS is simple enough to process. */
1370 static bool
1371 simple_rhs_p (rtx rhs)
1373 rtx op0, op1;
1375 if (function_invariant_p (rhs)
1376 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
1377 return true;
1379 switch (GET_CODE (rhs))
1381 case PLUS:
1382 case MINUS:
1383 case AND:
1384 op0 = XEXP (rhs, 0);
1385 op1 = XEXP (rhs, 1);
1386 /* Allow reg OP const and reg OP reg. */
1387 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
1388 && !function_invariant_p (op0))
1389 return false;
1390 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
1391 && !function_invariant_p (op1))
1392 return false;
1394 return true;
1396 case ASHIFT:
1397 case ASHIFTRT:
1398 case LSHIFTRT:
1399 case MULT:
1400 op0 = XEXP (rhs, 0);
1401 op1 = XEXP (rhs, 1);
1402 /* Allow reg OP const. */
1403 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1404 return false;
1405 if (!function_invariant_p (op1))
1406 return false;
1408 return true;
1410 default:
1411 return false;
1415 /* If REGNO has a single definition, return its known value, otherwise return
1416 null. */
1418 static rtx
1419 find_single_def_src (unsigned int regno)
1421 df_ref adef;
1422 rtx set, src;
1424 for (;;)
1426 rtx note;
1427 adef = DF_REG_DEF_CHAIN (regno);
1428 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
1429 || DF_REF_IS_ARTIFICIAL (adef))
1430 return NULL_RTX;
1432 set = single_set (DF_REF_INSN (adef));
1433 if (set == NULL || !REG_P (SET_DEST (set))
1434 || REGNO (SET_DEST (set)) != regno)
1435 return NULL_RTX;
1437 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1439 if (note && function_invariant_p (XEXP (note, 0)))
1441 src = XEXP (note, 0);
1442 break;
1444 src = SET_SRC (set);
1446 if (REG_P (src))
1448 regno = REGNO (src);
1449 continue;
1451 break;
1453 if (!function_invariant_p (src))
1454 return NULL_RTX;
1456 return src;
1459 /* If any registers in *EXPR that have a single definition, try to replace
1460 them with the known-equivalent values. */
1462 static void
1463 replace_single_def_regs (rtx *expr)
1465 subrtx_var_iterator::array_type array;
1466 repeat:
1467 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1469 rtx x = *iter;
1470 if (REG_P (x))
1471 if (rtx new_x = find_single_def_src (REGNO (x)))
1473 *expr = simplify_replace_rtx (*expr, x, new_x);
1474 goto repeat;
1479 /* A subroutine of simplify_using_initial_values, this function examines INSN
1480 to see if it contains a suitable set that we can use to make a replacement.
1481 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1482 the set; return false otherwise. */
1484 static bool
1485 suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
1487 rtx set = single_set (insn);
1488 rtx lhs = NULL_RTX, rhs;
1490 if (!set)
1491 return false;
1493 lhs = SET_DEST (set);
1494 if (!REG_P (lhs))
1495 return false;
1497 rhs = find_reg_equal_equiv_note (insn);
1498 if (rhs)
1499 rhs = XEXP (rhs, 0);
1500 else
1501 rhs = SET_SRC (set);
1503 if (!simple_rhs_p (rhs))
1504 return false;
1506 *dest = lhs;
1507 *src = rhs;
1508 return true;
1511 /* Using the data returned by suitable_set_for_replacement, replace DEST
1512 with SRC in *EXPR and return the new expression. Also call
1513 replace_single_def_regs if the replacement changed something. */
1514 static void
1515 replace_in_expr (rtx *expr, rtx dest, rtx src)
1517 rtx old = *expr;
1518 *expr = simplify_replace_rtx (*expr, dest, src);
1519 if (old == *expr)
1520 return;
1521 replace_single_def_regs (expr);
1524 /* Checks whether A implies B. */
1526 static bool
1527 implies_p (rtx a, rtx b)
1529 rtx op0, op1, opb0, opb1;
1530 machine_mode mode;
1532 if (rtx_equal_p (a, b))
1533 return true;
1535 if (GET_CODE (a) == EQ)
1537 op0 = XEXP (a, 0);
1538 op1 = XEXP (a, 1);
1540 if (REG_P (op0)
1541 || (GET_CODE (op0) == SUBREG
1542 && REG_P (SUBREG_REG (op0))))
1544 rtx r = simplify_replace_rtx (b, op0, op1);
1545 if (r == const_true_rtx)
1546 return true;
1549 if (REG_P (op1)
1550 || (GET_CODE (op1) == SUBREG
1551 && REG_P (SUBREG_REG (op1))))
1553 rtx r = simplify_replace_rtx (b, op1, op0);
1554 if (r == const_true_rtx)
1555 return true;
1559 if (b == const_true_rtx)
1560 return true;
1562 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1563 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1564 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1565 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1566 return false;
1568 op0 = XEXP (a, 0);
1569 op1 = XEXP (a, 1);
1570 opb0 = XEXP (b, 0);
1571 opb1 = XEXP (b, 1);
1573 mode = GET_MODE (op0);
1574 if (mode != GET_MODE (opb0))
1575 mode = VOIDmode;
1576 else if (mode == VOIDmode)
1578 mode = GET_MODE (op1);
1579 if (mode != GET_MODE (opb1))
1580 mode = VOIDmode;
1583 /* A < B implies A + 1 <= B. */
1584 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1585 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1588 if (GET_CODE (a) == GT)
1589 std::swap (op0, op1);
1591 if (GET_CODE (b) == GE)
1592 std::swap (opb0, opb1);
1594 if (SCALAR_INT_MODE_P (mode)
1595 && rtx_equal_p (op1, opb1)
1596 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1597 return true;
1598 return false;
1601 /* A < B or A > B imply A != B. TODO: Likewise
1602 A + n < B implies A != B + n if neither wraps. */
1603 if (GET_CODE (b) == NE
1604 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1605 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1607 if (rtx_equal_p (op0, opb0)
1608 && rtx_equal_p (op1, opb1))
1609 return true;
1612 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1613 if (GET_CODE (a) == NE
1614 && op1 == const0_rtx)
1616 if ((GET_CODE (b) == GTU
1617 && opb1 == const0_rtx)
1618 || (GET_CODE (b) == GEU
1619 && opb1 == const1_rtx))
1620 return rtx_equal_p (op0, opb0);
1623 /* A != N is equivalent to A - (N + 1) <u -1. */
1624 if (GET_CODE (a) == NE
1625 && CONST_INT_P (op1)
1626 && GET_CODE (b) == LTU
1627 && opb1 == constm1_rtx
1628 && GET_CODE (opb0) == PLUS
1629 && CONST_INT_P (XEXP (opb0, 1))
1630 /* Avoid overflows. */
1631 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1632 != ((unsigned HOST_WIDE_INT)1
1633 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1634 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1635 return rtx_equal_p (op0, XEXP (opb0, 0));
1637 /* Likewise, A != N implies A - N > 0. */
1638 if (GET_CODE (a) == NE
1639 && CONST_INT_P (op1))
1641 if (GET_CODE (b) == GTU
1642 && GET_CODE (opb0) == PLUS
1643 && opb1 == const0_rtx
1644 && CONST_INT_P (XEXP (opb0, 1))
1645 /* Avoid overflows. */
1646 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1647 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1648 && rtx_equal_p (XEXP (opb0, 0), op0))
1649 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1650 if (GET_CODE (b) == GEU
1651 && GET_CODE (opb0) == PLUS
1652 && opb1 == const1_rtx
1653 && CONST_INT_P (XEXP (opb0, 1))
1654 /* Avoid overflows. */
1655 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1656 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1657 && rtx_equal_p (XEXP (opb0, 0), op0))
1658 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1661 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1662 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
1663 && CONST_INT_P (op1)
1664 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1665 || INTVAL (op1) >= 0)
1666 && GET_CODE (b) == LTU
1667 && CONST_INT_P (opb1)
1668 && rtx_equal_p (op0, opb0))
1669 return INTVAL (opb1) < 0;
1671 return false;
1674 /* Canonicalizes COND so that
1676 (1) Ensure that operands are ordered according to
1677 swap_commutative_operands_p.
1678 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1679 for GE, GEU, and LEU. */
1682 canon_condition (rtx cond)
1684 rtx op0, op1;
1685 enum rtx_code code;
1686 machine_mode mode;
1688 code = GET_CODE (cond);
1689 op0 = XEXP (cond, 0);
1690 op1 = XEXP (cond, 1);
1692 if (swap_commutative_operands_p (op0, op1))
1694 code = swap_condition (code);
1695 std::swap (op0, op1);
1698 mode = GET_MODE (op0);
1699 if (mode == VOIDmode)
1700 mode = GET_MODE (op1);
1701 gcc_assert (mode != VOIDmode);
1703 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
1705 rtx_mode_t const_val (op1, mode);
1707 switch (code)
1709 case LE:
1710 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1712 code = LT;
1713 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1715 break;
1717 case GE:
1718 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1720 code = GT;
1721 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1723 break;
1725 case LEU:
1726 if (wi::ne_p (const_val, -1))
1728 code = LTU;
1729 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1731 break;
1733 case GEU:
1734 if (wi::ne_p (const_val, 0))
1736 code = GTU;
1737 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1739 break;
1741 default:
1742 break;
1746 if (op0 != XEXP (cond, 0)
1747 || op1 != XEXP (cond, 1)
1748 || code != GET_CODE (cond)
1749 || GET_MODE (cond) != SImode)
1750 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1752 return cond;
1755 /* Reverses CONDition; returns NULL if we cannot. */
1757 static rtx
1758 reversed_condition (rtx cond)
1760 enum rtx_code reversed;
1761 reversed = reversed_comparison_code (cond, NULL);
1762 if (reversed == UNKNOWN)
1763 return NULL_RTX;
1764 else
1765 return gen_rtx_fmt_ee (reversed,
1766 GET_MODE (cond), XEXP (cond, 0),
1767 XEXP (cond, 1));
1770 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1771 set of altered regs. */
1773 void
1774 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1776 rtx rev, reve, exp = *expr;
1778 /* If some register gets altered later, we do not really speak about its
1779 value at the time of comparison. */
1780 if (altered && altered_reg_used (cond, altered))
1781 return;
1783 if (GET_CODE (cond) == EQ
1784 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1786 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1787 return;
1790 if (!COMPARISON_P (exp))
1791 return;
1793 rev = reversed_condition (cond);
1794 reve = reversed_condition (exp);
1796 cond = canon_condition (cond);
1797 exp = canon_condition (exp);
1798 if (rev)
1799 rev = canon_condition (rev);
1800 if (reve)
1801 reve = canon_condition (reve);
1803 if (rtx_equal_p (exp, cond))
1805 *expr = const_true_rtx;
1806 return;
1809 if (rev && rtx_equal_p (exp, rev))
1811 *expr = const0_rtx;
1812 return;
1815 if (implies_p (cond, exp))
1817 *expr = const_true_rtx;
1818 return;
1821 if (reve && implies_p (cond, reve))
1823 *expr = const0_rtx;
1824 return;
1827 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1828 be false. */
1829 if (rev && implies_p (exp, rev))
1831 *expr = const0_rtx;
1832 return;
1835 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1836 if (rev && reve && implies_p (reve, rev))
1838 *expr = const_true_rtx;
1839 return;
1842 /* We would like to have some other tests here. TODO. */
1844 return;
1847 /* Use relationship between A and *B to eventually eliminate *B.
1848 OP is the operation we consider. */
1850 static void
1851 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1853 switch (op)
1855 case AND:
1856 /* If A implies *B, we may replace *B by true. */
1857 if (implies_p (a, *b))
1858 *b = const_true_rtx;
1859 break;
1861 case IOR:
1862 /* If *B implies A, we may replace *B by false. */
1863 if (implies_p (*b, a))
1864 *b = const0_rtx;
1865 break;
1867 default:
1868 gcc_unreachable ();
1872 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1873 operation we consider. */
1875 static void
1876 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1878 rtx elt;
1880 for (elt = tail; elt; elt = XEXP (elt, 1))
1881 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1882 for (elt = tail; elt; elt = XEXP (elt, 1))
1883 eliminate_implied_condition (op, XEXP (elt, 0), head);
1886 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1887 is a list, its elements are assumed to be combined using OP. */
1889 static void
1890 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1892 bool expression_valid;
1893 rtx head, tail, last_valid_expr;
1894 rtx_expr_list *cond_list;
1895 rtx_insn *insn;
1896 rtx neutral, aggr;
1897 regset altered, this_altered;
1898 edge e;
1900 if (!*expr)
1901 return;
1903 if (CONSTANT_P (*expr))
1904 return;
1906 if (GET_CODE (*expr) == EXPR_LIST)
1908 head = XEXP (*expr, 0);
1909 tail = XEXP (*expr, 1);
1911 eliminate_implied_conditions (op, &head, tail);
1913 switch (op)
1915 case AND:
1916 neutral = const_true_rtx;
1917 aggr = const0_rtx;
1918 break;
1920 case IOR:
1921 neutral = const0_rtx;
1922 aggr = const_true_rtx;
1923 break;
1925 default:
1926 gcc_unreachable ();
1929 simplify_using_initial_values (loop, UNKNOWN, &head);
1930 if (head == aggr)
1932 XEXP (*expr, 0) = aggr;
1933 XEXP (*expr, 1) = NULL_RTX;
1934 return;
1936 else if (head == neutral)
1938 *expr = tail;
1939 simplify_using_initial_values (loop, op, expr);
1940 return;
1942 simplify_using_initial_values (loop, op, &tail);
1944 if (tail && XEXP (tail, 0) == aggr)
1946 *expr = tail;
1947 return;
1950 XEXP (*expr, 0) = head;
1951 XEXP (*expr, 1) = tail;
1952 return;
1955 gcc_assert (op == UNKNOWN);
1957 replace_single_def_regs (expr);
1958 if (CONSTANT_P (*expr))
1959 return;
1961 e = loop_preheader_edge (loop);
1962 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1963 return;
1965 altered = ALLOC_REG_SET (&reg_obstack);
1966 this_altered = ALLOC_REG_SET (&reg_obstack);
1968 expression_valid = true;
1969 last_valid_expr = *expr;
1970 cond_list = NULL;
1971 while (1)
1973 insn = BB_END (e->src);
1974 if (any_condjump_p (insn))
1976 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1978 if (cond && (e->flags & EDGE_FALLTHRU))
1979 cond = reversed_condition (cond);
1980 if (cond)
1982 rtx old = *expr;
1983 simplify_using_condition (cond, expr, altered);
1984 if (old != *expr)
1986 rtx note;
1987 if (CONSTANT_P (*expr))
1988 goto out;
1989 for (note = cond_list; note; note = XEXP (note, 1))
1991 simplify_using_condition (XEXP (note, 0), expr, altered);
1992 if (CONSTANT_P (*expr))
1993 goto out;
1996 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
2000 FOR_BB_INSNS_REVERSE (e->src, insn)
2002 rtx src, dest;
2003 rtx old = *expr;
2005 if (!INSN_P (insn))
2006 continue;
2008 CLEAR_REG_SET (this_altered);
2009 note_stores (PATTERN (insn), mark_altered, this_altered);
2010 if (CALL_P (insn))
2012 /* Kill all call clobbered registers. */
2013 unsigned int i;
2014 hard_reg_set_iterator hrsi;
2015 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
2016 0, i, hrsi)
2017 SET_REGNO_REG_SET (this_altered, i);
2020 if (suitable_set_for_replacement (insn, &dest, &src))
2022 rtx_expr_list **pnote, **pnote_next;
2024 replace_in_expr (expr, dest, src);
2025 if (CONSTANT_P (*expr))
2026 goto out;
2028 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2030 rtx_expr_list *note = *pnote;
2031 rtx old_cond = XEXP (note, 0);
2033 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2034 replace_in_expr (&XEXP (note, 0), dest, src);
2036 /* We can no longer use a condition that has been simplified
2037 to a constant, and simplify_using_condition will abort if
2038 we try. */
2039 if (CONSTANT_P (XEXP (note, 0)))
2041 *pnote = *pnote_next;
2042 pnote_next = pnote;
2043 free_EXPR_LIST_node (note);
2045 /* Retry simplifications with this condition if either the
2046 expression or the condition changed. */
2047 else if (old_cond != XEXP (note, 0) || old != *expr)
2048 simplify_using_condition (XEXP (note, 0), expr, altered);
2051 else
2053 rtx_expr_list **pnote, **pnote_next;
2055 /* If we did not use this insn to make a replacement, any overlap
2056 between stores in this insn and our expression will cause the
2057 expression to become invalid. */
2058 if (altered_reg_used (*expr, this_altered))
2059 goto out;
2061 /* Likewise for the conditions. */
2062 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2064 rtx_expr_list *note = *pnote;
2065 rtx old_cond = XEXP (note, 0);
2067 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2068 if (altered_reg_used (old_cond, this_altered))
2070 *pnote = *pnote_next;
2071 pnote_next = pnote;
2072 free_EXPR_LIST_node (note);
2077 if (CONSTANT_P (*expr))
2078 goto out;
2080 IOR_REG_SET (altered, this_altered);
2082 /* If the expression now contains regs that have been altered, we
2083 can't return it to the caller. However, it is still valid for
2084 further simplification, so keep searching to see if we can
2085 eventually turn it into a constant. */
2086 if (altered_reg_used (*expr, altered))
2087 expression_valid = false;
2088 if (expression_valid)
2089 last_valid_expr = *expr;
2092 if (!single_pred_p (e->src)
2093 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2094 break;
2095 e = single_pred_edge (e->src);
2098 out:
2099 free_EXPR_LIST_list (&cond_list);
2100 if (!CONSTANT_P (*expr))
2101 *expr = last_valid_expr;
2102 FREE_REG_SET (altered);
2103 FREE_REG_SET (this_altered);
2106 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2107 that IV occurs as left operands of comparison COND and its signedness
2108 is SIGNED_P to DESC. */
2110 static void
2111 shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
2112 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2114 rtx mmin, mmax, cond_over, cond_under;
2116 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
2117 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2118 iv->base, mmin);
2119 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2120 iv->base, mmax);
2122 switch (cond)
2124 case LE:
2125 case LT:
2126 case LEU:
2127 case LTU:
2128 if (cond_under != const0_rtx)
2129 desc->infinite =
2130 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2131 if (cond_over != const0_rtx)
2132 desc->noloop_assumptions =
2133 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2134 break;
2136 case GE:
2137 case GT:
2138 case GEU:
2139 case GTU:
2140 if (cond_over != const0_rtx)
2141 desc->infinite =
2142 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2143 if (cond_under != const0_rtx)
2144 desc->noloop_assumptions =
2145 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2146 break;
2148 case NE:
2149 if (cond_over != const0_rtx)
2150 desc->infinite =
2151 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2152 if (cond_under != const0_rtx)
2153 desc->infinite =
2154 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2155 break;
2157 default:
2158 gcc_unreachable ();
2161 iv->mode = mode;
2162 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
2165 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2166 subregs of the same mode if possible (sometimes it is necessary to add
2167 some assumptions to DESC). */
2169 static bool
2170 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2171 enum rtx_code cond, struct niter_desc *desc)
2173 machine_mode comp_mode;
2174 bool signed_p;
2176 /* If the ivs behave specially in the first iteration, or are
2177 added/multiplied after extending, we ignore them. */
2178 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2179 return false;
2180 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2181 return false;
2183 /* If there is some extend, it must match signedness of the comparison. */
2184 switch (cond)
2186 case LE:
2187 case LT:
2188 if (iv0->extend == IV_ZERO_EXTEND
2189 || iv1->extend == IV_ZERO_EXTEND)
2190 return false;
2191 signed_p = true;
2192 break;
2194 case LEU:
2195 case LTU:
2196 if (iv0->extend == IV_SIGN_EXTEND
2197 || iv1->extend == IV_SIGN_EXTEND)
2198 return false;
2199 signed_p = false;
2200 break;
2202 case NE:
2203 if (iv0->extend != IV_UNKNOWN_EXTEND
2204 && iv1->extend != IV_UNKNOWN_EXTEND
2205 && iv0->extend != iv1->extend)
2206 return false;
2208 signed_p = false;
2209 if (iv0->extend != IV_UNKNOWN_EXTEND)
2210 signed_p = iv0->extend == IV_SIGN_EXTEND;
2211 if (iv1->extend != IV_UNKNOWN_EXTEND)
2212 signed_p = iv1->extend == IV_SIGN_EXTEND;
2213 break;
2215 default:
2216 gcc_unreachable ();
2219 /* Values of both variables should be computed in the same mode. These
2220 might indeed be different, if we have comparison like
2222 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2224 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2225 in different modes. This does not seem impossible to handle, but
2226 it hardly ever occurs in practice.
2228 The only exception is the case when one of operands is invariant.
2229 For example pentium 3 generates comparisons like
2230 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2231 definitely do not want this prevent the optimization. */
2232 comp_mode = iv0->extend_mode;
2233 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2234 comp_mode = iv1->extend_mode;
2236 if (iv0->extend_mode != comp_mode)
2238 if (iv0->mode != iv0->extend_mode
2239 || iv0->step != const0_rtx)
2240 return false;
2242 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2243 comp_mode, iv0->base, iv0->mode);
2244 iv0->extend_mode = comp_mode;
2247 if (iv1->extend_mode != comp_mode)
2249 if (iv1->mode != iv1->extend_mode
2250 || iv1->step != const0_rtx)
2251 return false;
2253 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2254 comp_mode, iv1->base, iv1->mode);
2255 iv1->extend_mode = comp_mode;
2258 /* Check that both ivs belong to a range of a single mode. If one of the
2259 operands is an invariant, we may need to shorten it into the common
2260 mode. */
2261 if (iv0->mode == iv0->extend_mode
2262 && iv0->step == const0_rtx
2263 && iv0->mode != iv1->mode)
2264 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2266 if (iv1->mode == iv1->extend_mode
2267 && iv1->step == const0_rtx
2268 && iv0->mode != iv1->mode)
2269 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2271 if (iv0->mode != iv1->mode)
2272 return false;
2274 desc->mode = iv0->mode;
2275 desc->signed_p = signed_p;
2277 return true;
2280 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2281 result. This function is called from iv_number_of_iterations with
2282 a number of fields in DESC already filled in. OLD_NITER is the original
2283 expression for the number of iterations, before we tried to simplify it. */
2285 static uint64_t
2286 determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
2288 rtx niter = desc->niter_expr;
2289 rtx mmin, mmax, cmp;
2290 uint64_t nmax, inc;
2291 uint64_t andmax = 0;
2293 /* We used to look for constant operand 0 of AND,
2294 but canonicalization should always make this impossible. */
2295 gcc_checking_assert (GET_CODE (niter) != AND
2296 || !CONST_INT_P (XEXP (niter, 0)));
2298 if (GET_CODE (niter) == AND
2299 && CONST_INT_P (XEXP (niter, 1)))
2301 andmax = UINTVAL (XEXP (niter, 1));
2302 niter = XEXP (niter, 0);
2305 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
2306 nmax = UINTVAL (mmax) - UINTVAL (mmin);
2308 if (GET_CODE (niter) == UDIV)
2310 if (!CONST_INT_P (XEXP (niter, 1)))
2311 return nmax;
2312 inc = INTVAL (XEXP (niter, 1));
2313 niter = XEXP (niter, 0);
2315 else
2316 inc = 1;
2318 /* We could use a binary search here, but for now improving the upper
2319 bound by just one eliminates one important corner case. */
2320 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2321 desc->mode, old_niter, mmax);
2322 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2323 if (cmp == const_true_rtx)
2325 nmax--;
2327 if (dump_file)
2328 fprintf (dump_file, ";; improved upper bound by one.\n");
2330 nmax /= inc;
2331 if (andmax)
2332 nmax = MIN (nmax, andmax);
2333 if (dump_file)
2334 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
2335 nmax);
2336 return nmax;
2339 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2340 the result into DESC. Very similar to determine_number_of_iterations
2341 (basically its rtl version), complicated by things like subregs. */
2343 static void
2344 iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
2345 struct niter_desc *desc)
2347 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
2348 struct rtx_iv iv0, iv1;
2349 rtx assumption, may_not_xform;
2350 enum rtx_code cond;
2351 machine_mode mode, comp_mode;
2352 rtx mmin, mmax, mode_mmin, mode_mmax;
2353 uint64_t s, size, d, inv, max;
2354 int64_t up, down, inc, step_val;
2355 int was_sharp = false;
2356 rtx old_niter;
2357 bool step_is_pow2;
2359 /* The meaning of these assumptions is this:
2360 if !assumptions
2361 then the rest of information does not have to be valid
2362 if noloop_assumptions then the loop does not roll
2363 if infinite then this exit is never used */
2365 desc->assumptions = NULL_RTX;
2366 desc->noloop_assumptions = NULL_RTX;
2367 desc->infinite = NULL_RTX;
2368 desc->simple_p = true;
2370 desc->const_iter = false;
2371 desc->niter_expr = NULL_RTX;
2373 cond = GET_CODE (condition);
2374 gcc_assert (COMPARISON_P (condition));
2376 mode = GET_MODE (XEXP (condition, 0));
2377 if (mode == VOIDmode)
2378 mode = GET_MODE (XEXP (condition, 1));
2379 /* The constant comparisons should be folded. */
2380 gcc_assert (mode != VOIDmode);
2382 /* We only handle integers or pointers. */
2383 if (GET_MODE_CLASS (mode) != MODE_INT
2384 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2385 goto fail;
2387 op0 = XEXP (condition, 0);
2388 if (!iv_analyze (insn, op0, &iv0))
2389 goto fail;
2390 if (iv0.extend_mode == VOIDmode)
2391 iv0.mode = iv0.extend_mode = mode;
2393 op1 = XEXP (condition, 1);
2394 if (!iv_analyze (insn, op1, &iv1))
2395 goto fail;
2396 if (iv1.extend_mode == VOIDmode)
2397 iv1.mode = iv1.extend_mode = mode;
2399 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2400 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2401 goto fail;
2403 /* Check condition and normalize it. */
2405 switch (cond)
2407 case GE:
2408 case GT:
2409 case GEU:
2410 case GTU:
2411 std::swap (iv0, iv1);
2412 cond = swap_condition (cond);
2413 break;
2414 case NE:
2415 case LE:
2416 case LEU:
2417 case LT:
2418 case LTU:
2419 break;
2420 default:
2421 goto fail;
2424 /* Handle extends. This is relatively nontrivial, so we only try in some
2425 easy cases, when we can canonicalize the ivs (possibly by adding some
2426 assumptions) to shape subreg (base + i * step). This function also fills
2427 in desc->mode and desc->signed_p. */
2429 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2430 goto fail;
2432 comp_mode = iv0.extend_mode;
2433 mode = iv0.mode;
2434 size = GET_MODE_PRECISION (mode);
2435 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2436 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2437 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2439 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
2440 goto fail;
2442 /* We can take care of the case of two induction variables chasing each other
2443 if the test is NE. I have never seen a loop using it, but still it is
2444 cool. */
2445 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2447 if (cond != NE)
2448 goto fail;
2450 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2451 iv1.step = const0_rtx;
2454 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2455 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2457 /* This is either infinite loop or the one that ends immediately, depending
2458 on initial values. Unswitching should remove this kind of conditions. */
2459 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2460 goto fail;
2462 if (cond != NE)
2464 if (iv0.step == const0_rtx)
2465 step_val = -INTVAL (iv1.step);
2466 else
2467 step_val = INTVAL (iv0.step);
2469 /* Ignore loops of while (i-- < 10) type. */
2470 if (step_val < 0)
2471 goto fail;
2473 step_is_pow2 = !(step_val & (step_val - 1));
2475 else
2477 /* We do not care about whether the step is power of two in this
2478 case. */
2479 step_is_pow2 = false;
2480 step_val = 0;
2483 /* Some more condition normalization. We must record some assumptions
2484 due to overflows. */
2485 switch (cond)
2487 case LT:
2488 case LTU:
2489 /* We want to take care only of non-sharp relationals; this is easy,
2490 as in cases the overflow would make the transformation unsafe
2491 the loop does not roll. Seemingly it would make more sense to want
2492 to take care of sharp relationals instead, as NE is more similar to
2493 them, but the problem is that here the transformation would be more
2494 difficult due to possibly infinite loops. */
2495 if (iv0.step == const0_rtx)
2497 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2498 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2499 mode_mmax);
2500 if (assumption == const_true_rtx)
2501 goto zero_iter_simplify;
2502 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2503 iv0.base, const1_rtx);
2505 else
2507 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2508 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2509 mode_mmin);
2510 if (assumption == const_true_rtx)
2511 goto zero_iter_simplify;
2512 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2513 iv1.base, constm1_rtx);
2516 if (assumption != const0_rtx)
2517 desc->noloop_assumptions =
2518 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2519 cond = (cond == LT) ? LE : LEU;
2521 /* It will be useful to be able to tell the difference once more in
2522 LE -> NE reduction. */
2523 was_sharp = true;
2524 break;
2525 default: ;
2528 /* Take care of trivially infinite loops. */
2529 if (cond != NE)
2531 if (iv0.step == const0_rtx)
2533 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2534 if (rtx_equal_p (tmp, mode_mmin))
2536 desc->infinite =
2537 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2538 /* Fill in the remaining fields somehow. */
2539 goto zero_iter_simplify;
2542 else
2544 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2545 if (rtx_equal_p (tmp, mode_mmax))
2547 desc->infinite =
2548 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2549 /* Fill in the remaining fields somehow. */
2550 goto zero_iter_simplify;
2555 /* If we can we want to take care of NE conditions instead of size
2556 comparisons, as they are much more friendly (most importantly
2557 this takes care of special handling of loops with step 1). We can
2558 do it if we first check that upper bound is greater or equal to
2559 lower bound, their difference is constant c modulo step and that
2560 there is not an overflow. */
2561 if (cond != NE)
2563 if (iv0.step == const0_rtx)
2564 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2565 else
2566 step = iv0.step;
2567 step = lowpart_subreg (mode, step, comp_mode);
2568 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2569 delta = lowpart_subreg (mode, delta, comp_mode);
2570 delta = simplify_gen_binary (UMOD, mode, delta, step);
2571 may_xform = const0_rtx;
2572 may_not_xform = const_true_rtx;
2574 if (CONST_INT_P (delta))
2576 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2578 /* A special case. We have transformed condition of type
2579 for (i = 0; i < 4; i += 4)
2580 into
2581 for (i = 0; i <= 3; i += 4)
2582 obviously if the test for overflow during that transformation
2583 passed, we cannot overflow here. Most importantly any
2584 loop with sharp end condition and step 1 falls into this
2585 category, so handling this case specially is definitely
2586 worth the troubles. */
2587 may_xform = const_true_rtx;
2589 else if (iv0.step == const0_rtx)
2591 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2592 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2593 bound = lowpart_subreg (mode, bound, comp_mode);
2594 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2595 may_xform = simplify_gen_relational (cond, SImode, mode,
2596 bound, tmp);
2597 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2598 SImode, mode,
2599 bound, tmp);
2601 else
2603 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2604 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2605 bound = lowpart_subreg (mode, bound, comp_mode);
2606 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2607 may_xform = simplify_gen_relational (cond, SImode, mode,
2608 tmp, bound);
2609 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2610 SImode, mode,
2611 tmp, bound);
2615 if (may_xform != const0_rtx)
2617 /* We perform the transformation always provided that it is not
2618 completely senseless. This is OK, as we would need this assumption
2619 to determine the number of iterations anyway. */
2620 if (may_xform != const_true_rtx)
2622 /* If the step is a power of two and the final value we have
2623 computed overflows, the cycle is infinite. Otherwise it
2624 is nontrivial to compute the number of iterations. */
2625 if (step_is_pow2)
2626 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2627 desc->infinite);
2628 else
2629 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2630 desc->assumptions);
2633 /* We are going to lose some information about upper bound on
2634 number of iterations in this step, so record the information
2635 here. */
2636 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2637 if (CONST_INT_P (iv1.base))
2638 up = INTVAL (iv1.base);
2639 else
2640 up = INTVAL (mode_mmax) - inc;
2641 down = INTVAL (CONST_INT_P (iv0.base)
2642 ? iv0.base
2643 : mode_mmin);
2644 max = (uint64_t) (up - down) / inc + 1;
2645 if (!desc->infinite
2646 && !desc->assumptions)
2647 record_niter_bound (loop, max, false, true);
2649 if (iv0.step == const0_rtx)
2651 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2652 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2654 else
2656 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2657 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2660 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2661 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2662 assumption = simplify_gen_relational (reverse_condition (cond),
2663 SImode, mode, tmp0, tmp1);
2664 if (assumption == const_true_rtx)
2665 goto zero_iter_simplify;
2666 else if (assumption != const0_rtx)
2667 desc->noloop_assumptions =
2668 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2669 cond = NE;
2673 /* Count the number of iterations. */
2674 if (cond == NE)
2676 /* Everything we do here is just arithmetics modulo size of mode. This
2677 makes us able to do more involved computations of number of iterations
2678 than in other cases. First transform the condition into shape
2679 s * i <> c, with s positive. */
2680 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2681 iv0.base = const0_rtx;
2682 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2683 iv1.step = const0_rtx;
2684 if (INTVAL (iv0.step) < 0)
2686 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2687 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
2689 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2691 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2692 is infinite. Otherwise, the number of iterations is
2693 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2694 s = INTVAL (iv0.step); d = 1;
2695 while (s % 2 != 1)
2697 s /= 2;
2698 d *= 2;
2699 size--;
2701 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
2703 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2704 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
2705 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2706 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2708 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
2709 inv = inverse (s, size);
2710 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2711 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2713 else
2715 if (iv1.step == const0_rtx)
2716 /* Condition in shape a + s * i <= b
2717 We must know that b + s does not overflow and a <= b + s and then we
2718 can compute number of iterations as (b + s - a) / s. (It might
2719 seem that we in fact could be more clever about testing the b + s
2720 overflow condition using some information about b - a mod s,
2721 but it was already taken into account during LE -> NE transform). */
2723 step = iv0.step;
2724 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2725 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2727 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2728 lowpart_subreg (mode, step,
2729 comp_mode));
2730 if (step_is_pow2)
2732 rtx t0, t1;
2734 /* If s is power of 2, we know that the loop is infinite if
2735 a % s <= b % s and b + s overflows. */
2736 assumption = simplify_gen_relational (reverse_condition (cond),
2737 SImode, mode,
2738 tmp1, bound);
2740 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2741 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2742 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2743 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2744 desc->infinite =
2745 alloc_EXPR_LIST (0, assumption, desc->infinite);
2747 else
2749 assumption = simplify_gen_relational (cond, SImode, mode,
2750 tmp1, bound);
2751 desc->assumptions =
2752 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2755 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2756 tmp = lowpart_subreg (mode, tmp, comp_mode);
2757 assumption = simplify_gen_relational (reverse_condition (cond),
2758 SImode, mode, tmp0, tmp);
2760 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2761 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2763 else
2765 /* Condition in shape a <= b - s * i
2766 We must know that a - s does not overflow and a - s <= b and then
2767 we can again compute number of iterations as (b - (a - s)) / s. */
2768 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2769 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2770 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2772 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2773 lowpart_subreg (mode, step, comp_mode));
2774 if (step_is_pow2)
2776 rtx t0, t1;
2778 /* If s is power of 2, we know that the loop is infinite if
2779 a % s <= b % s and a - s overflows. */
2780 assumption = simplify_gen_relational (reverse_condition (cond),
2781 SImode, mode,
2782 bound, tmp0);
2784 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2785 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2786 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2787 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2788 desc->infinite =
2789 alloc_EXPR_LIST (0, assumption, desc->infinite);
2791 else
2793 assumption = simplify_gen_relational (cond, SImode, mode,
2794 bound, tmp0);
2795 desc->assumptions =
2796 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2799 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2800 tmp = lowpart_subreg (mode, tmp, comp_mode);
2801 assumption = simplify_gen_relational (reverse_condition (cond),
2802 SImode, mode,
2803 tmp, tmp1);
2804 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2805 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2807 if (assumption == const_true_rtx)
2808 goto zero_iter_simplify;
2809 else if (assumption != const0_rtx)
2810 desc->noloop_assumptions =
2811 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2812 delta = simplify_gen_binary (UDIV, mode, delta, step);
2813 desc->niter_expr = delta;
2816 old_niter = desc->niter_expr;
2818 simplify_using_initial_values (loop, AND, &desc->assumptions);
2819 if (desc->assumptions
2820 && XEXP (desc->assumptions, 0) == const0_rtx)
2821 goto fail;
2822 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2823 simplify_using_initial_values (loop, IOR, &desc->infinite);
2824 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2826 /* Rerun the simplification. Consider code (created by copying loop headers)
2828 i = 0;
2830 if (0 < n)
2834 i++;
2835 } while (i < n);
2838 The first pass determines that i = 0, the second pass uses it to eliminate
2839 noloop assumption. */
2841 simplify_using_initial_values (loop, AND, &desc->assumptions);
2842 if (desc->assumptions
2843 && XEXP (desc->assumptions, 0) == const0_rtx)
2844 goto fail;
2845 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2846 simplify_using_initial_values (loop, IOR, &desc->infinite);
2847 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2849 if (desc->noloop_assumptions
2850 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2851 goto zero_iter;
2853 if (CONST_INT_P (desc->niter_expr))
2855 uint64_t val = INTVAL (desc->niter_expr);
2857 desc->const_iter = true;
2858 desc->niter = val & GET_MODE_MASK (desc->mode);
2859 if (!desc->infinite
2860 && !desc->assumptions)
2861 record_niter_bound (loop, desc->niter, false, true);
2863 else
2865 max = determine_max_iter (loop, desc, old_niter);
2866 if (!max)
2867 goto zero_iter_simplify;
2868 if (!desc->infinite
2869 && !desc->assumptions)
2870 record_niter_bound (loop, max, false, true);
2872 /* simplify_using_initial_values does a copy propagation on the registers
2873 in the expression for the number of iterations. This prolongs life
2874 ranges of registers and increases register pressure, and usually
2875 brings no gain (and if it happens to do, the cse pass will take care
2876 of it anyway). So prevent this behavior, unless it enabled us to
2877 derive that the number of iterations is a constant. */
2878 desc->niter_expr = old_niter;
2881 return;
2883 zero_iter_simplify:
2884 /* Simplify the assumptions. */
2885 simplify_using_initial_values (loop, AND, &desc->assumptions);
2886 if (desc->assumptions
2887 && XEXP (desc->assumptions, 0) == const0_rtx)
2888 goto fail;
2889 simplify_using_initial_values (loop, IOR, &desc->infinite);
2891 /* Fallthru. */
2892 zero_iter:
2893 desc->const_iter = true;
2894 desc->niter = 0;
2895 record_niter_bound (loop, 0, true, true);
2896 desc->noloop_assumptions = NULL_RTX;
2897 desc->niter_expr = const0_rtx;
2898 return;
2900 fail:
2901 desc->simple_p = false;
2902 return;
2905 /* Checks whether E is a simple exit from LOOP and stores its description
2906 into DESC. */
2908 static void
2909 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2911 basic_block exit_bb;
2912 rtx condition;
2913 rtx_insn *at;
2914 edge ein;
2916 exit_bb = e->src;
2917 desc->simple_p = false;
2919 /* It must belong directly to the loop. */
2920 if (exit_bb->loop_father != loop)
2921 return;
2923 /* It must be tested (at least) once during any iteration. */
2924 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2925 return;
2927 /* It must end in a simple conditional jump. */
2928 if (!any_condjump_p (BB_END (exit_bb)))
2929 return;
2931 ein = EDGE_SUCC (exit_bb, 0);
2932 if (ein == e)
2933 ein = EDGE_SUCC (exit_bb, 1);
2935 desc->out_edge = e;
2936 desc->in_edge = ein;
2938 /* Test whether the condition is suitable. */
2939 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2940 return;
2942 if (ein->flags & EDGE_FALLTHRU)
2944 condition = reversed_condition (condition);
2945 if (!condition)
2946 return;
2949 /* Check that we are able to determine number of iterations and fill
2950 in information about it. */
2951 iv_number_of_iterations (loop, at, condition, desc);
2954 /* Finds a simple exit of LOOP and stores its description into DESC. */
2956 void
2957 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2959 unsigned i;
2960 basic_block *body;
2961 edge e;
2962 struct niter_desc act;
2963 bool any = false;
2964 edge_iterator ei;
2966 desc->simple_p = false;
2967 body = get_loop_body (loop);
2969 for (i = 0; i < loop->num_nodes; i++)
2971 FOR_EACH_EDGE (e, ei, body[i]->succs)
2973 if (flow_bb_inside_loop_p (loop, e->dest))
2974 continue;
2976 check_simple_exit (loop, e, &act);
2977 if (!act.simple_p)
2978 continue;
2980 if (!any)
2981 any = true;
2982 else
2984 /* Prefer constant iterations; the less the better. */
2985 if (!act.const_iter
2986 || (desc->const_iter && act.niter >= desc->niter))
2987 continue;
2989 /* Also if the actual exit may be infinite, while the old one
2990 not, prefer the old one. */
2991 if (act.infinite && !desc->infinite)
2992 continue;
2995 *desc = act;
2999 if (dump_file)
3001 if (desc->simple_p)
3003 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
3004 fprintf (dump_file, " simple exit %d -> %d\n",
3005 desc->out_edge->src->index,
3006 desc->out_edge->dest->index);
3007 if (desc->assumptions)
3009 fprintf (dump_file, " assumptions: ");
3010 print_rtl (dump_file, desc->assumptions);
3011 fprintf (dump_file, "\n");
3013 if (desc->noloop_assumptions)
3015 fprintf (dump_file, " does not roll if: ");
3016 print_rtl (dump_file, desc->noloop_assumptions);
3017 fprintf (dump_file, "\n");
3019 if (desc->infinite)
3021 fprintf (dump_file, " infinite if: ");
3022 print_rtl (dump_file, desc->infinite);
3023 fprintf (dump_file, "\n");
3026 fprintf (dump_file, " number of iterations: ");
3027 print_rtl (dump_file, desc->niter_expr);
3028 fprintf (dump_file, "\n");
3030 fprintf (dump_file, " upper bound: %li\n",
3031 (long)get_max_loop_iterations_int (loop));
3032 fprintf (dump_file, " realistic bound: %li\n",
3033 (long)get_estimated_loop_iterations_int (loop));
3035 else
3036 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
3039 free (body);
3042 /* Creates a simple loop description of LOOP if it was not computed
3043 already. */
3045 struct niter_desc *
3046 get_simple_loop_desc (struct loop *loop)
3048 struct niter_desc *desc = simple_loop_desc (loop);
3050 if (desc)
3051 return desc;
3053 /* At least desc->infinite is not always initialized by
3054 find_simple_loop_exit. */
3055 desc = ggc_cleared_alloc<niter_desc> ();
3056 iv_analysis_loop_init (loop);
3057 find_simple_exit (loop, desc);
3058 loop->simple_loop_desc = desc;
3060 if (desc->simple_p && (desc->assumptions || desc->infinite))
3062 const char *wording;
3064 /* Assume that no overflow happens and that the loop is finite.
3065 We already warned at the tree level if we ran optimizations there. */
3066 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
3068 if (desc->infinite)
3070 wording =
3071 flag_unsafe_loop_optimizations
3072 ? N_("assuming that the loop is not infinite")
3073 : N_("cannot optimize possibly infinite loops");
3074 warning (OPT_Wunsafe_loop_optimizations, "%s",
3075 gettext (wording));
3077 if (desc->assumptions)
3079 wording =
3080 flag_unsafe_loop_optimizations
3081 ? N_("assuming that the loop counter does not overflow")
3082 : N_("cannot optimize loop, the loop counter may overflow");
3083 warning (OPT_Wunsafe_loop_optimizations, "%s",
3084 gettext (wording));
3088 if (flag_unsafe_loop_optimizations)
3090 desc->assumptions = NULL_RTX;
3091 desc->infinite = NULL_RTX;
3095 return desc;
3098 /* Releases simple loop description for LOOP. */
3100 void
3101 free_simple_loop_desc (struct loop *loop)
3103 struct niter_desc *desc = simple_loop_desc (loop);
3105 if (!desc)
3106 return;
3108 ggc_free (desc);
3109 loop->simple_loop_desc = NULL;