cp/
[official-gcc.git] / gcc / loop-iv.c
blob8c3ef7784e6b277e63dd34086bc67c84f575a780
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 "vec.h"
59 #include "hashtab.h"
60 #include "hash-set.h"
61 #include "machmode.h"
62 #include "input.h"
63 #include "function.h"
64 #include "dominance.h"
65 #include "cfg.h"
66 #include "basic-block.h"
67 #include "cfgloop.h"
68 #include "symtab.h"
69 #include "flags.h"
70 #include "statistics.h"
71 #include "double-int.h"
72 #include "real.h"
73 #include "fixed-value.h"
74 #include "alias.h"
75 #include "wide-int.h"
76 #include "inchash.h"
77 #include "tree.h"
78 #include "insn-config.h"
79 #include "expmed.h"
80 #include "dojump.h"
81 #include "explow.h"
82 #include "calls.h"
83 #include "emit-rtl.h"
84 #include "varasm.h"
85 #include "stmt.h"
86 #include "expr.h"
87 #include "intl.h"
88 #include "diagnostic-core.h"
89 #include "df.h"
90 #include "hash-table.h"
91 #include "dumpfile.h"
92 #include "rtl-iter.h"
94 /* Possible return values of iv_get_reaching_def. */
96 enum iv_grd_result
98 /* More than one reaching def, or reaching def that does not
99 dominate the use. */
100 GRD_INVALID,
102 /* The use is trivial invariant of the loop, i.e. is not changed
103 inside the loop. */
104 GRD_INVARIANT,
106 /* The use is reached by initial value and a value from the
107 previous iteration. */
108 GRD_MAYBE_BIV,
110 /* The use has single dominating def. */
111 GRD_SINGLE_DOM
114 /* Information about a biv. */
116 struct biv_entry
118 unsigned regno; /* The register of the biv. */
119 struct rtx_iv iv; /* Value of the biv. */
122 static bool clean_slate = true;
124 static unsigned int iv_ref_table_size = 0;
126 /* Table of rtx_ivs indexed by the df_ref uid field. */
127 static struct rtx_iv ** iv_ref_table;
129 /* Induction variable stored at the reference. */
130 #define DF_REF_IV(REF) iv_ref_table[DF_REF_ID (REF)]
131 #define DF_REF_IV_SET(REF, IV) iv_ref_table[DF_REF_ID (REF)] = (IV)
133 /* The current loop. */
135 static struct loop *current_loop;
137 /* Hashtable helper. */
139 struct biv_entry_hasher : typed_free_remove <biv_entry>
141 typedef biv_entry *value_type;
142 typedef rtx_def *compare_type;
143 static inline hashval_t hash (const biv_entry *);
144 static inline bool equal (const biv_entry *, const rtx_def *);
147 /* Returns hash value for biv B. */
149 inline hashval_t
150 biv_entry_hasher::hash (const biv_entry *b)
152 return b->regno;
155 /* Compares biv B and register R. */
157 inline bool
158 biv_entry_hasher::equal (const biv_entry *b, const rtx_def *r)
160 return b->regno == REGNO (r);
163 /* Bivs of the current loop. */
165 static hash_table<biv_entry_hasher> *bivs;
167 static bool iv_analyze_op (rtx_insn *, rtx, struct rtx_iv *);
169 /* Return the RTX code corresponding to the IV extend code EXTEND. */
170 static inline enum rtx_code
171 iv_extend_to_rtx_code (enum iv_extend_code extend)
173 switch (extend)
175 case IV_SIGN_EXTEND:
176 return SIGN_EXTEND;
177 case IV_ZERO_EXTEND:
178 return ZERO_EXTEND;
179 case IV_UNKNOWN_EXTEND:
180 return UNKNOWN;
182 gcc_unreachable ();
185 /* Dumps information about IV to FILE. */
187 extern void dump_iv_info (FILE *, struct rtx_iv *);
188 void
189 dump_iv_info (FILE *file, struct rtx_iv *iv)
191 if (!iv->base)
193 fprintf (file, "not simple");
194 return;
197 if (iv->step == const0_rtx
198 && !iv->first_special)
199 fprintf (file, "invariant ");
201 print_rtl (file, iv->base);
202 if (iv->step != const0_rtx)
204 fprintf (file, " + ");
205 print_rtl (file, iv->step);
206 fprintf (file, " * iteration");
208 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
210 if (iv->mode != iv->extend_mode)
211 fprintf (file, " %s to %s",
212 rtx_name[iv_extend_to_rtx_code (iv->extend)],
213 GET_MODE_NAME (iv->extend_mode));
215 if (iv->mult != const1_rtx)
217 fprintf (file, " * ");
218 print_rtl (file, iv->mult);
220 if (iv->delta != const0_rtx)
222 fprintf (file, " + ");
223 print_rtl (file, iv->delta);
225 if (iv->first_special)
226 fprintf (file, " (first special)");
229 /* Generates a subreg to get the least significant part of EXPR (in mode
230 INNER_MODE) to OUTER_MODE. */
233 lowpart_subreg (machine_mode outer_mode, rtx expr,
234 machine_mode inner_mode)
236 return simplify_gen_subreg (outer_mode, expr, inner_mode,
237 subreg_lowpart_offset (outer_mode, inner_mode));
240 static void
241 check_iv_ref_table_size (void)
243 if (iv_ref_table_size < DF_DEFS_TABLE_SIZE ())
245 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
246 iv_ref_table = XRESIZEVEC (struct rtx_iv *, iv_ref_table, new_size);
247 memset (&iv_ref_table[iv_ref_table_size], 0,
248 (new_size - iv_ref_table_size) * sizeof (struct rtx_iv *));
249 iv_ref_table_size = new_size;
254 /* Checks whether REG is a well-behaved register. */
256 static bool
257 simple_reg_p (rtx reg)
259 unsigned r;
261 if (GET_CODE (reg) == SUBREG)
263 if (!subreg_lowpart_p (reg))
264 return false;
265 reg = SUBREG_REG (reg);
268 if (!REG_P (reg))
269 return false;
271 r = REGNO (reg);
272 if (HARD_REGISTER_NUM_P (r))
273 return false;
275 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
276 return false;
278 return true;
281 /* Clears the information about ivs stored in df. */
283 static void
284 clear_iv_info (void)
286 unsigned i, n_defs = DF_DEFS_TABLE_SIZE ();
287 struct rtx_iv *iv;
289 check_iv_ref_table_size ();
290 for (i = 0; i < n_defs; i++)
292 iv = iv_ref_table[i];
293 if (iv)
295 free (iv);
296 iv_ref_table[i] = NULL;
300 bivs->empty ();
304 /* Prepare the data for an induction variable analysis of a LOOP. */
306 void
307 iv_analysis_loop_init (struct loop *loop)
309 current_loop = loop;
311 /* Clear the information from the analysis of the previous loop. */
312 if (clean_slate)
314 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
315 bivs = new hash_table<biv_entry_hasher> (10);
316 clean_slate = false;
318 else
319 clear_iv_info ();
321 /* Get rid of the ud chains before processing the rescans. Then add
322 the problem back. */
323 df_remove_problem (df_chain);
324 df_process_deferred_rescans ();
325 df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
326 df_chain_add_problem (DF_UD_CHAIN);
327 df_note_add_problem ();
328 df_analyze_loop (loop);
329 if (dump_file)
330 df_dump_region (dump_file);
332 check_iv_ref_table_size ();
335 /* Finds the definition of REG that dominates loop latch and stores
336 it to DEF. Returns false if there is not a single definition
337 dominating the latch. If REG has no definition in loop, DEF
338 is set to NULL and true is returned. */
340 static bool
341 latch_dominating_def (rtx reg, df_ref *def)
343 df_ref single_rd = NULL, adef;
344 unsigned regno = REGNO (reg);
345 struct df_rd_bb_info *bb_info = DF_RD_BB_INFO (current_loop->latch);
347 for (adef = DF_REG_DEF_CHAIN (regno); adef; adef = DF_REF_NEXT_REG (adef))
349 if (!bitmap_bit_p (df->blocks_to_analyze, DF_REF_BBNO (adef))
350 || !bitmap_bit_p (&bb_info->out, DF_REF_ID (adef)))
351 continue;
353 /* More than one reaching definition. */
354 if (single_rd)
355 return false;
357 if (!just_once_each_iteration_p (current_loop, DF_REF_BB (adef)))
358 return false;
360 single_rd = adef;
363 *def = single_rd;
364 return true;
367 /* Gets definition of REG reaching its use in INSN and stores it to DEF. */
369 static enum iv_grd_result
370 iv_get_reaching_def (rtx_insn *insn, rtx reg, df_ref *def)
372 df_ref use, adef;
373 basic_block def_bb, use_bb;
374 rtx_insn *def_insn;
375 bool dom_p;
377 *def = NULL;
378 if (!simple_reg_p (reg))
379 return GRD_INVALID;
380 if (GET_CODE (reg) == SUBREG)
381 reg = SUBREG_REG (reg);
382 gcc_assert (REG_P (reg));
384 use = df_find_use (insn, reg);
385 gcc_assert (use != NULL);
387 if (!DF_REF_CHAIN (use))
388 return GRD_INVARIANT;
390 /* More than one reaching def. */
391 if (DF_REF_CHAIN (use)->next)
392 return GRD_INVALID;
394 adef = DF_REF_CHAIN (use)->ref;
396 /* We do not handle setting only part of the register. */
397 if (DF_REF_FLAGS (adef) & DF_REF_READ_WRITE)
398 return GRD_INVALID;
400 def_insn = DF_REF_INSN (adef);
401 def_bb = DF_REF_BB (adef);
402 use_bb = BLOCK_FOR_INSN (insn);
404 if (use_bb == def_bb)
405 dom_p = (DF_INSN_LUID (def_insn) < DF_INSN_LUID (insn));
406 else
407 dom_p = dominated_by_p (CDI_DOMINATORS, use_bb, def_bb);
409 if (dom_p)
411 *def = adef;
412 return GRD_SINGLE_DOM;
415 /* The definition does not dominate the use. This is still OK if
416 this may be a use of a biv, i.e. if the def_bb dominates loop
417 latch. */
418 if (just_once_each_iteration_p (current_loop, def_bb))
419 return GRD_MAYBE_BIV;
421 return GRD_INVALID;
424 /* Sets IV to invariant CST in MODE. Always returns true (just for
425 consistency with other iv manipulation functions that may fail). */
427 static bool
428 iv_constant (struct rtx_iv *iv, rtx cst, machine_mode mode)
430 if (mode == VOIDmode)
431 mode = GET_MODE (cst);
433 iv->mode = mode;
434 iv->base = cst;
435 iv->step = const0_rtx;
436 iv->first_special = false;
437 iv->extend = IV_UNKNOWN_EXTEND;
438 iv->extend_mode = iv->mode;
439 iv->delta = const0_rtx;
440 iv->mult = const1_rtx;
442 return true;
445 /* Evaluates application of subreg to MODE on IV. */
447 static bool
448 iv_subreg (struct rtx_iv *iv, machine_mode mode)
450 /* If iv is invariant, just calculate the new value. */
451 if (iv->step == const0_rtx
452 && !iv->first_special)
454 rtx val = get_iv_value (iv, const0_rtx);
455 val = lowpart_subreg (mode, val,
456 iv->extend == IV_UNKNOWN_EXTEND
457 ? iv->mode : iv->extend_mode);
459 iv->base = val;
460 iv->extend = IV_UNKNOWN_EXTEND;
461 iv->mode = iv->extend_mode = mode;
462 iv->delta = const0_rtx;
463 iv->mult = const1_rtx;
464 return true;
467 if (iv->extend_mode == mode)
468 return true;
470 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
471 return false;
473 iv->extend = IV_UNKNOWN_EXTEND;
474 iv->mode = mode;
476 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
477 simplify_gen_binary (MULT, iv->extend_mode,
478 iv->base, iv->mult));
479 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
480 iv->mult = const1_rtx;
481 iv->delta = const0_rtx;
482 iv->first_special = false;
484 return true;
487 /* Evaluates application of EXTEND to MODE on IV. */
489 static bool
490 iv_extend (struct rtx_iv *iv, enum iv_extend_code extend, machine_mode mode)
492 /* If iv is invariant, just calculate the new value. */
493 if (iv->step == const0_rtx
494 && !iv->first_special)
496 rtx val = get_iv_value (iv, const0_rtx);
497 if (iv->extend_mode != iv->mode
498 && iv->extend != IV_UNKNOWN_EXTEND
499 && iv->extend != extend)
500 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
501 val = simplify_gen_unary (iv_extend_to_rtx_code (extend), mode,
502 val,
503 iv->extend == extend
504 ? iv->extend_mode : iv->mode);
505 iv->base = val;
506 iv->extend = IV_UNKNOWN_EXTEND;
507 iv->mode = iv->extend_mode = mode;
508 iv->delta = const0_rtx;
509 iv->mult = const1_rtx;
510 return true;
513 if (mode != iv->extend_mode)
514 return false;
516 if (iv->extend != IV_UNKNOWN_EXTEND
517 && iv->extend != extend)
518 return false;
520 iv->extend = extend;
522 return true;
525 /* Evaluates negation of IV. */
527 static bool
528 iv_neg (struct rtx_iv *iv)
530 if (iv->extend == IV_UNKNOWN_EXTEND)
532 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
533 iv->base, iv->extend_mode);
534 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
535 iv->step, iv->extend_mode);
537 else
539 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
540 iv->delta, iv->extend_mode);
541 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
542 iv->mult, iv->extend_mode);
545 return true;
548 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
550 static bool
551 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
553 machine_mode mode;
554 rtx arg;
556 /* Extend the constant to extend_mode of the other operand if necessary. */
557 if (iv0->extend == IV_UNKNOWN_EXTEND
558 && iv0->mode == iv0->extend_mode
559 && iv0->step == const0_rtx
560 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
562 iv0->extend_mode = iv1->extend_mode;
563 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
564 iv0->base, iv0->mode);
566 if (iv1->extend == IV_UNKNOWN_EXTEND
567 && iv1->mode == iv1->extend_mode
568 && iv1->step == const0_rtx
569 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
571 iv1->extend_mode = iv0->extend_mode;
572 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
573 iv1->base, iv1->mode);
576 mode = iv0->extend_mode;
577 if (mode != iv1->extend_mode)
578 return false;
580 if (iv0->extend == IV_UNKNOWN_EXTEND
581 && iv1->extend == IV_UNKNOWN_EXTEND)
583 if (iv0->mode != iv1->mode)
584 return false;
586 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
587 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
589 return true;
592 /* Handle addition of constant. */
593 if (iv1->extend == IV_UNKNOWN_EXTEND
594 && iv1->mode == mode
595 && iv1->step == const0_rtx)
597 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
598 return true;
601 if (iv0->extend == IV_UNKNOWN_EXTEND
602 && iv0->mode == mode
603 && iv0->step == const0_rtx)
605 arg = iv0->base;
606 *iv0 = *iv1;
607 if (op == MINUS
608 && !iv_neg (iv0))
609 return false;
611 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
612 return true;
615 return false;
618 /* Evaluates multiplication of IV by constant CST. */
620 static bool
621 iv_mult (struct rtx_iv *iv, rtx mby)
623 machine_mode mode = iv->extend_mode;
625 if (GET_MODE (mby) != VOIDmode
626 && GET_MODE (mby) != mode)
627 return false;
629 if (iv->extend == IV_UNKNOWN_EXTEND)
631 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
632 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
634 else
636 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
637 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
640 return true;
643 /* Evaluates shift of IV by constant CST. */
645 static bool
646 iv_shift (struct rtx_iv *iv, rtx mby)
648 machine_mode mode = iv->extend_mode;
650 if (GET_MODE (mby) != VOIDmode
651 && GET_MODE (mby) != mode)
652 return false;
654 if (iv->extend == IV_UNKNOWN_EXTEND)
656 iv->base = simplify_gen_binary (ASHIFT, mode, iv->base, mby);
657 iv->step = simplify_gen_binary (ASHIFT, mode, iv->step, mby);
659 else
661 iv->delta = simplify_gen_binary (ASHIFT, mode, iv->delta, mby);
662 iv->mult = simplify_gen_binary (ASHIFT, mode, iv->mult, mby);
665 return true;
668 /* The recursive part of get_biv_step. Gets the value of the single value
669 defined by DEF wrto initial value of REG inside loop, in shape described
670 at get_biv_step. */
672 static bool
673 get_biv_step_1 (df_ref def, rtx reg,
674 rtx *inner_step, machine_mode *inner_mode,
675 enum iv_extend_code *extend, machine_mode outer_mode,
676 rtx *outer_step)
678 rtx set, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
679 rtx next, nextr, tmp;
680 enum rtx_code code;
681 rtx_insn *insn = DF_REF_INSN (def);
682 df_ref next_def;
683 enum iv_grd_result res;
685 set = single_set (insn);
686 if (!set)
687 return false;
689 rhs = find_reg_equal_equiv_note (insn);
690 if (rhs)
691 rhs = XEXP (rhs, 0);
692 else
693 rhs = SET_SRC (set);
695 code = GET_CODE (rhs);
696 switch (code)
698 case SUBREG:
699 case REG:
700 next = rhs;
701 break;
703 case PLUS:
704 case MINUS:
705 op0 = XEXP (rhs, 0);
706 op1 = XEXP (rhs, 1);
708 if (code == PLUS && CONSTANT_P (op0))
710 tmp = op0; op0 = op1; op1 = tmp;
713 if (!simple_reg_p (op0)
714 || !CONSTANT_P (op1))
715 return false;
717 if (GET_MODE (rhs) != outer_mode)
719 /* ppc64 uses expressions like
721 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
723 this is equivalent to
725 (set x':DI (plus:DI y:DI 1))
726 (set x:SI (subreg:SI (x':DI)). */
727 if (GET_CODE (op0) != SUBREG)
728 return false;
729 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
730 return false;
733 next = op0;
734 break;
736 case SIGN_EXTEND:
737 case ZERO_EXTEND:
738 if (GET_MODE (rhs) != outer_mode)
739 return false;
741 op0 = XEXP (rhs, 0);
742 if (!simple_reg_p (op0))
743 return false;
745 next = op0;
746 break;
748 default:
749 return false;
752 if (GET_CODE (next) == SUBREG)
754 if (!subreg_lowpart_p (next))
755 return false;
757 nextr = SUBREG_REG (next);
758 if (GET_MODE (nextr) != outer_mode)
759 return false;
761 else
762 nextr = next;
764 res = iv_get_reaching_def (insn, nextr, &next_def);
766 if (res == GRD_INVALID || res == GRD_INVARIANT)
767 return false;
769 if (res == GRD_MAYBE_BIV)
771 if (!rtx_equal_p (nextr, reg))
772 return false;
774 *inner_step = const0_rtx;
775 *extend = IV_UNKNOWN_EXTEND;
776 *inner_mode = outer_mode;
777 *outer_step = const0_rtx;
779 else if (!get_biv_step_1 (next_def, reg,
780 inner_step, inner_mode, extend, outer_mode,
781 outer_step))
782 return false;
784 if (GET_CODE (next) == SUBREG)
786 machine_mode amode = GET_MODE (next);
788 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
789 return false;
791 *inner_mode = amode;
792 *inner_step = simplify_gen_binary (PLUS, outer_mode,
793 *inner_step, *outer_step);
794 *outer_step = const0_rtx;
795 *extend = IV_UNKNOWN_EXTEND;
798 switch (code)
800 case REG:
801 case SUBREG:
802 break;
804 case PLUS:
805 case MINUS:
806 if (*inner_mode == outer_mode
807 /* See comment in previous switch. */
808 || GET_MODE (rhs) != outer_mode)
809 *inner_step = simplify_gen_binary (code, outer_mode,
810 *inner_step, op1);
811 else
812 *outer_step = simplify_gen_binary (code, outer_mode,
813 *outer_step, op1);
814 break;
816 case SIGN_EXTEND:
817 case ZERO_EXTEND:
818 gcc_assert (GET_MODE (op0) == *inner_mode
819 && *extend == IV_UNKNOWN_EXTEND
820 && *outer_step == const0_rtx);
822 *extend = (code == SIGN_EXTEND) ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
823 break;
825 default:
826 return false;
829 return true;
832 /* Gets the operation on register REG inside loop, in shape
834 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
836 If the operation cannot be described in this shape, return false.
837 LAST_DEF is the definition of REG that dominates loop latch. */
839 static bool
840 get_biv_step (df_ref last_def, rtx reg, rtx *inner_step,
841 machine_mode *inner_mode, enum iv_extend_code *extend,
842 machine_mode *outer_mode, rtx *outer_step)
844 *outer_mode = GET_MODE (reg);
846 if (!get_biv_step_1 (last_def, reg,
847 inner_step, inner_mode, extend, *outer_mode,
848 outer_step))
849 return false;
851 gcc_assert ((*inner_mode == *outer_mode) != (*extend != IV_UNKNOWN_EXTEND));
852 gcc_assert (*inner_mode != *outer_mode || *outer_step == const0_rtx);
854 return true;
857 /* Records information that DEF is induction variable IV. */
859 static void
860 record_iv (df_ref def, struct rtx_iv *iv)
862 struct rtx_iv *recorded_iv = XNEW (struct rtx_iv);
864 *recorded_iv = *iv;
865 check_iv_ref_table_size ();
866 DF_REF_IV_SET (def, recorded_iv);
869 /* If DEF was already analyzed for bivness, store the description of the biv to
870 IV and return true. Otherwise return false. */
872 static bool
873 analyzed_for_bivness_p (rtx def, struct rtx_iv *iv)
875 struct biv_entry *biv = bivs->find_with_hash (def, REGNO (def));
877 if (!biv)
878 return false;
880 *iv = biv->iv;
881 return true;
884 static void
885 record_biv (rtx def, struct rtx_iv *iv)
887 struct biv_entry *biv = XNEW (struct biv_entry);
888 biv_entry **slot = bivs->find_slot_with_hash (def, REGNO (def), INSERT);
890 biv->regno = REGNO (def);
891 biv->iv = *iv;
892 gcc_assert (!*slot);
893 *slot = biv;
896 /* Determines whether DEF is a biv and if so, stores its description
897 to *IV. */
899 static bool
900 iv_analyze_biv (rtx def, struct rtx_iv *iv)
902 rtx inner_step, outer_step;
903 machine_mode inner_mode, outer_mode;
904 enum iv_extend_code extend;
905 df_ref last_def;
907 if (dump_file)
909 fprintf (dump_file, "Analyzing ");
910 print_rtl (dump_file, def);
911 fprintf (dump_file, " for bivness.\n");
914 if (!REG_P (def))
916 if (!CONSTANT_P (def))
917 return false;
919 return iv_constant (iv, def, VOIDmode);
922 if (!latch_dominating_def (def, &last_def))
924 if (dump_file)
925 fprintf (dump_file, " not simple.\n");
926 return false;
929 if (!last_def)
930 return iv_constant (iv, def, VOIDmode);
932 if (analyzed_for_bivness_p (def, iv))
934 if (dump_file)
935 fprintf (dump_file, " already analysed.\n");
936 return iv->base != NULL_RTX;
939 if (!get_biv_step (last_def, def, &inner_step, &inner_mode, &extend,
940 &outer_mode, &outer_step))
942 iv->base = NULL_RTX;
943 goto end;
946 /* Loop transforms base to es (base + inner_step) + outer_step,
947 where es means extend of subreg between inner_mode and outer_mode.
948 The corresponding induction variable is
950 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
952 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
953 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
954 iv->mode = inner_mode;
955 iv->extend_mode = outer_mode;
956 iv->extend = extend;
957 iv->mult = const1_rtx;
958 iv->delta = outer_step;
959 iv->first_special = inner_mode != outer_mode;
961 end:
962 if (dump_file)
964 fprintf (dump_file, " ");
965 dump_iv_info (dump_file, iv);
966 fprintf (dump_file, "\n");
969 record_biv (def, iv);
970 return iv->base != NULL_RTX;
973 /* Analyzes expression RHS used at INSN and stores the result to *IV.
974 The mode of the induction variable is MODE. */
976 bool
977 iv_analyze_expr (rtx_insn *insn, rtx rhs, machine_mode mode,
978 struct rtx_iv *iv)
980 rtx mby = NULL_RTX;
981 rtx op0 = NULL_RTX, op1 = NULL_RTX;
982 struct rtx_iv iv0, iv1;
983 enum rtx_code code = GET_CODE (rhs);
984 machine_mode omode = mode;
986 iv->mode = VOIDmode;
987 iv->base = NULL_RTX;
988 iv->step = NULL_RTX;
990 gcc_assert (GET_MODE (rhs) == mode || GET_MODE (rhs) == VOIDmode);
992 if (CONSTANT_P (rhs)
993 || REG_P (rhs)
994 || code == SUBREG)
996 if (!iv_analyze_op (insn, rhs, iv))
997 return false;
999 if (iv->mode == VOIDmode)
1001 iv->mode = mode;
1002 iv->extend_mode = mode;
1005 return true;
1008 switch (code)
1010 case REG:
1011 op0 = rhs;
1012 break;
1014 case SIGN_EXTEND:
1015 case ZERO_EXTEND:
1016 case NEG:
1017 op0 = XEXP (rhs, 0);
1018 omode = GET_MODE (op0);
1019 break;
1021 case PLUS:
1022 case MINUS:
1023 op0 = XEXP (rhs, 0);
1024 op1 = XEXP (rhs, 1);
1025 break;
1027 case MULT:
1028 op0 = XEXP (rhs, 0);
1029 mby = XEXP (rhs, 1);
1030 if (!CONSTANT_P (mby))
1031 std::swap (op0, mby);
1032 if (!CONSTANT_P (mby))
1033 return false;
1034 break;
1036 case ASHIFT:
1037 op0 = XEXP (rhs, 0);
1038 mby = XEXP (rhs, 1);
1039 if (!CONSTANT_P (mby))
1040 return false;
1041 break;
1043 default:
1044 return false;
1047 if (op0
1048 && !iv_analyze_expr (insn, op0, omode, &iv0))
1049 return false;
1051 if (op1
1052 && !iv_analyze_expr (insn, op1, omode, &iv1))
1053 return false;
1055 switch (code)
1057 case SIGN_EXTEND:
1058 if (!iv_extend (&iv0, IV_SIGN_EXTEND, mode))
1059 return false;
1060 break;
1062 case ZERO_EXTEND:
1063 if (!iv_extend (&iv0, IV_ZERO_EXTEND, mode))
1064 return false;
1065 break;
1067 case NEG:
1068 if (!iv_neg (&iv0))
1069 return false;
1070 break;
1072 case PLUS:
1073 case MINUS:
1074 if (!iv_add (&iv0, &iv1, code))
1075 return false;
1076 break;
1078 case MULT:
1079 if (!iv_mult (&iv0, mby))
1080 return false;
1081 break;
1083 case ASHIFT:
1084 if (!iv_shift (&iv0, mby))
1085 return false;
1086 break;
1088 default:
1089 break;
1092 *iv = iv0;
1093 return iv->base != NULL_RTX;
1096 /* Analyzes iv DEF and stores the result to *IV. */
1098 static bool
1099 iv_analyze_def (df_ref def, struct rtx_iv *iv)
1101 rtx_insn *insn = DF_REF_INSN (def);
1102 rtx reg = DF_REF_REG (def);
1103 rtx set, rhs;
1105 if (dump_file)
1107 fprintf (dump_file, "Analyzing def of ");
1108 print_rtl (dump_file, reg);
1109 fprintf (dump_file, " in insn ");
1110 print_rtl_single (dump_file, insn);
1113 check_iv_ref_table_size ();
1114 if (DF_REF_IV (def))
1116 if (dump_file)
1117 fprintf (dump_file, " already analysed.\n");
1118 *iv = *DF_REF_IV (def);
1119 return iv->base != NULL_RTX;
1122 iv->mode = VOIDmode;
1123 iv->base = NULL_RTX;
1124 iv->step = NULL_RTX;
1126 if (!REG_P (reg))
1127 return false;
1129 set = single_set (insn);
1130 if (!set)
1131 return false;
1133 if (!REG_P (SET_DEST (set)))
1134 return false;
1136 gcc_assert (SET_DEST (set) == reg);
1137 rhs = find_reg_equal_equiv_note (insn);
1138 if (rhs)
1139 rhs = XEXP (rhs, 0);
1140 else
1141 rhs = SET_SRC (set);
1143 iv_analyze_expr (insn, rhs, GET_MODE (reg), iv);
1144 record_iv (def, iv);
1146 if (dump_file)
1148 print_rtl (dump_file, reg);
1149 fprintf (dump_file, " in insn ");
1150 print_rtl_single (dump_file, insn);
1151 fprintf (dump_file, " is ");
1152 dump_iv_info (dump_file, iv);
1153 fprintf (dump_file, "\n");
1156 return iv->base != NULL_RTX;
1159 /* Analyzes operand OP of INSN and stores the result to *IV. */
1161 static bool
1162 iv_analyze_op (rtx_insn *insn, rtx op, struct rtx_iv *iv)
1164 df_ref def = NULL;
1165 enum iv_grd_result res;
1167 if (dump_file)
1169 fprintf (dump_file, "Analyzing operand ");
1170 print_rtl (dump_file, op);
1171 fprintf (dump_file, " of insn ");
1172 print_rtl_single (dump_file, insn);
1175 if (function_invariant_p (op))
1176 res = GRD_INVARIANT;
1177 else if (GET_CODE (op) == SUBREG)
1179 if (!subreg_lowpart_p (op))
1180 return false;
1182 if (!iv_analyze_op (insn, SUBREG_REG (op), iv))
1183 return false;
1185 return iv_subreg (iv, GET_MODE (op));
1187 else
1189 res = iv_get_reaching_def (insn, op, &def);
1190 if (res == GRD_INVALID)
1192 if (dump_file)
1193 fprintf (dump_file, " not simple.\n");
1194 return false;
1198 if (res == GRD_INVARIANT)
1200 iv_constant (iv, op, VOIDmode);
1202 if (dump_file)
1204 fprintf (dump_file, " ");
1205 dump_iv_info (dump_file, iv);
1206 fprintf (dump_file, "\n");
1208 return true;
1211 if (res == GRD_MAYBE_BIV)
1212 return iv_analyze_biv (op, iv);
1214 return iv_analyze_def (def, iv);
1217 /* Analyzes value VAL at INSN and stores the result to *IV. */
1219 bool
1220 iv_analyze (rtx_insn *insn, rtx val, struct rtx_iv *iv)
1222 rtx reg;
1224 /* We must find the insn in that val is used, so that we get to UD chains.
1225 Since the function is sometimes called on result of get_condition,
1226 this does not necessarily have to be directly INSN; scan also the
1227 following insns. */
1228 if (simple_reg_p (val))
1230 if (GET_CODE (val) == SUBREG)
1231 reg = SUBREG_REG (val);
1232 else
1233 reg = val;
1235 while (!df_find_use (insn, reg))
1236 insn = NEXT_INSN (insn);
1239 return iv_analyze_op (insn, val, iv);
1242 /* Analyzes definition of DEF in INSN and stores the result to IV. */
1244 bool
1245 iv_analyze_result (rtx_insn *insn, rtx def, struct rtx_iv *iv)
1247 df_ref adef;
1249 adef = df_find_def (insn, def);
1250 if (!adef)
1251 return false;
1253 return iv_analyze_def (adef, iv);
1256 /* Checks whether definition of register REG in INSN is a basic induction
1257 variable. IV analysis must have been initialized (via a call to
1258 iv_analysis_loop_init) for this function to produce a result. */
1260 bool
1261 biv_p (rtx_insn *insn, rtx reg)
1263 struct rtx_iv iv;
1264 df_ref def, last_def;
1266 if (!simple_reg_p (reg))
1267 return false;
1269 def = df_find_def (insn, reg);
1270 gcc_assert (def != NULL);
1271 if (!latch_dominating_def (reg, &last_def))
1272 return false;
1273 if (last_def != def)
1274 return false;
1276 if (!iv_analyze_biv (reg, &iv))
1277 return false;
1279 return iv.step != const0_rtx;
1282 /* Calculates value of IV at ITERATION-th iteration. */
1285 get_iv_value (struct rtx_iv *iv, rtx iteration)
1287 rtx val;
1289 /* We would need to generate some if_then_else patterns, and so far
1290 it is not needed anywhere. */
1291 gcc_assert (!iv->first_special);
1293 if (iv->step != const0_rtx && iteration != const0_rtx)
1294 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1295 simplify_gen_binary (MULT, iv->extend_mode,
1296 iv->step, iteration));
1297 else
1298 val = iv->base;
1300 if (iv->extend_mode == iv->mode)
1301 return val;
1303 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1305 if (iv->extend == IV_UNKNOWN_EXTEND)
1306 return val;
1308 val = simplify_gen_unary (iv_extend_to_rtx_code (iv->extend),
1309 iv->extend_mode, val, iv->mode);
1310 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1311 simplify_gen_binary (MULT, iv->extend_mode,
1312 iv->mult, val));
1314 return val;
1317 /* Free the data for an induction variable analysis. */
1319 void
1320 iv_analysis_done (void)
1322 if (!clean_slate)
1324 clear_iv_info ();
1325 clean_slate = true;
1326 df_finish_pass (true);
1327 delete bivs;
1328 bivs = NULL;
1329 free (iv_ref_table);
1330 iv_ref_table = NULL;
1331 iv_ref_table_size = 0;
1335 /* Computes inverse to X modulo (1 << MOD). */
1337 static uint64_t
1338 inverse (uint64_t x, int mod)
1340 uint64_t mask =
1341 ((uint64_t) 1 << (mod - 1) << 1) - 1;
1342 uint64_t rslt = 1;
1343 int i;
1345 for (i = 0; i < mod - 1; i++)
1347 rslt = (rslt * x) & mask;
1348 x = (x * x) & mask;
1351 return rslt;
1354 /* Checks whether any register in X is in set ALT. */
1356 static bool
1357 altered_reg_used (const_rtx x, bitmap alt)
1359 subrtx_iterator::array_type array;
1360 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
1362 const_rtx x = *iter;
1363 if (REG_P (x) && REGNO_REG_SET_P (alt, REGNO (x)))
1364 return true;
1366 return false;
1369 /* Marks registers altered by EXPR in set ALT. */
1371 static void
1372 mark_altered (rtx expr, const_rtx by ATTRIBUTE_UNUSED, void *alt)
1374 if (GET_CODE (expr) == SUBREG)
1375 expr = SUBREG_REG (expr);
1376 if (!REG_P (expr))
1377 return;
1379 SET_REGNO_REG_SET ((bitmap) alt, REGNO (expr));
1382 /* Checks whether RHS is simple enough to process. */
1384 static bool
1385 simple_rhs_p (rtx rhs)
1387 rtx op0, op1;
1389 if (function_invariant_p (rhs)
1390 || (REG_P (rhs) && !HARD_REGISTER_P (rhs)))
1391 return true;
1393 switch (GET_CODE (rhs))
1395 case PLUS:
1396 case MINUS:
1397 case AND:
1398 op0 = XEXP (rhs, 0);
1399 op1 = XEXP (rhs, 1);
1400 /* Allow reg OP const and reg OP reg. */
1401 if (!(REG_P (op0) && !HARD_REGISTER_P (op0))
1402 && !function_invariant_p (op0))
1403 return false;
1404 if (!(REG_P (op1) && !HARD_REGISTER_P (op1))
1405 && !function_invariant_p (op1))
1406 return false;
1408 return true;
1410 case ASHIFT:
1411 case ASHIFTRT:
1412 case LSHIFTRT:
1413 case MULT:
1414 op0 = XEXP (rhs, 0);
1415 op1 = XEXP (rhs, 1);
1416 /* Allow reg OP const. */
1417 if (!(REG_P (op0) && !HARD_REGISTER_P (op0)))
1418 return false;
1419 if (!function_invariant_p (op1))
1420 return false;
1422 return true;
1424 default:
1425 return false;
1429 /* If REGNO has a single definition, return its known value, otherwise return
1430 null. */
1432 static rtx
1433 find_single_def_src (unsigned int regno)
1435 df_ref adef;
1436 rtx set, src;
1438 for (;;)
1440 rtx note;
1441 adef = DF_REG_DEF_CHAIN (regno);
1442 if (adef == NULL || DF_REF_NEXT_REG (adef) != NULL
1443 || DF_REF_IS_ARTIFICIAL (adef))
1444 return NULL_RTX;
1446 set = single_set (DF_REF_INSN (adef));
1447 if (set == NULL || !REG_P (SET_DEST (set))
1448 || REGNO (SET_DEST (set)) != regno)
1449 return NULL_RTX;
1451 note = find_reg_equal_equiv_note (DF_REF_INSN (adef));
1453 if (note && function_invariant_p (XEXP (note, 0)))
1455 src = XEXP (note, 0);
1456 break;
1458 src = SET_SRC (set);
1460 if (REG_P (src))
1462 regno = REGNO (src);
1463 continue;
1465 break;
1467 if (!function_invariant_p (src))
1468 return NULL_RTX;
1470 return src;
1473 /* If any registers in *EXPR that have a single definition, try to replace
1474 them with the known-equivalent values. */
1476 static void
1477 replace_single_def_regs (rtx *expr)
1479 subrtx_var_iterator::array_type array;
1480 repeat:
1481 FOR_EACH_SUBRTX_VAR (iter, array, *expr, NONCONST)
1483 rtx x = *iter;
1484 if (REG_P (x))
1485 if (rtx new_x = find_single_def_src (REGNO (x)))
1487 *expr = simplify_replace_rtx (*expr, x, new_x);
1488 goto repeat;
1493 /* A subroutine of simplify_using_initial_values, this function examines INSN
1494 to see if it contains a suitable set that we can use to make a replacement.
1495 If it is suitable, return true and set DEST and SRC to the lhs and rhs of
1496 the set; return false otherwise. */
1498 static bool
1499 suitable_set_for_replacement (rtx_insn *insn, rtx *dest, rtx *src)
1501 rtx set = single_set (insn);
1502 rtx lhs = NULL_RTX, rhs;
1504 if (!set)
1505 return false;
1507 lhs = SET_DEST (set);
1508 if (!REG_P (lhs))
1509 return false;
1511 rhs = find_reg_equal_equiv_note (insn);
1512 if (rhs)
1513 rhs = XEXP (rhs, 0);
1514 else
1515 rhs = SET_SRC (set);
1517 if (!simple_rhs_p (rhs))
1518 return false;
1520 *dest = lhs;
1521 *src = rhs;
1522 return true;
1525 /* Using the data returned by suitable_set_for_replacement, replace DEST
1526 with SRC in *EXPR and return the new expression. Also call
1527 replace_single_def_regs if the replacement changed something. */
1528 static void
1529 replace_in_expr (rtx *expr, rtx dest, rtx src)
1531 rtx old = *expr;
1532 *expr = simplify_replace_rtx (*expr, dest, src);
1533 if (old == *expr)
1534 return;
1535 replace_single_def_regs (expr);
1538 /* Checks whether A implies B. */
1540 static bool
1541 implies_p (rtx a, rtx b)
1543 rtx op0, op1, opb0, opb1;
1544 machine_mode mode;
1546 if (rtx_equal_p (a, b))
1547 return true;
1549 if (GET_CODE (a) == EQ)
1551 op0 = XEXP (a, 0);
1552 op1 = XEXP (a, 1);
1554 if (REG_P (op0)
1555 || (GET_CODE (op0) == SUBREG
1556 && REG_P (SUBREG_REG (op0))))
1558 rtx r = simplify_replace_rtx (b, op0, op1);
1559 if (r == const_true_rtx)
1560 return true;
1563 if (REG_P (op1)
1564 || (GET_CODE (op1) == SUBREG
1565 && REG_P (SUBREG_REG (op1))))
1567 rtx r = simplify_replace_rtx (b, op1, op0);
1568 if (r == const_true_rtx)
1569 return true;
1573 if (b == const_true_rtx)
1574 return true;
1576 if ((GET_RTX_CLASS (GET_CODE (a)) != RTX_COMM_COMPARE
1577 && GET_RTX_CLASS (GET_CODE (a)) != RTX_COMPARE)
1578 || (GET_RTX_CLASS (GET_CODE (b)) != RTX_COMM_COMPARE
1579 && GET_RTX_CLASS (GET_CODE (b)) != RTX_COMPARE))
1580 return false;
1582 op0 = XEXP (a, 0);
1583 op1 = XEXP (a, 1);
1584 opb0 = XEXP (b, 0);
1585 opb1 = XEXP (b, 1);
1587 mode = GET_MODE (op0);
1588 if (mode != GET_MODE (opb0))
1589 mode = VOIDmode;
1590 else if (mode == VOIDmode)
1592 mode = GET_MODE (op1);
1593 if (mode != GET_MODE (opb1))
1594 mode = VOIDmode;
1597 /* A < B implies A + 1 <= B. */
1598 if ((GET_CODE (a) == GT || GET_CODE (a) == LT)
1599 && (GET_CODE (b) == GE || GET_CODE (b) == LE))
1602 if (GET_CODE (a) == GT)
1603 std::swap (op0, op1);
1605 if (GET_CODE (b) == GE)
1606 std::swap (opb0, opb1);
1608 if (SCALAR_INT_MODE_P (mode)
1609 && rtx_equal_p (op1, opb1)
1610 && simplify_gen_binary (MINUS, mode, opb0, op0) == const1_rtx)
1611 return true;
1612 return false;
1615 /* A < B or A > B imply A != B. TODO: Likewise
1616 A + n < B implies A != B + n if neither wraps. */
1617 if (GET_CODE (b) == NE
1618 && (GET_CODE (a) == GT || GET_CODE (a) == GTU
1619 || GET_CODE (a) == LT || GET_CODE (a) == LTU))
1621 if (rtx_equal_p (op0, opb0)
1622 && rtx_equal_p (op1, opb1))
1623 return true;
1626 /* For unsigned comparisons, A != 0 implies A > 0 and A >= 1. */
1627 if (GET_CODE (a) == NE
1628 && op1 == const0_rtx)
1630 if ((GET_CODE (b) == GTU
1631 && opb1 == const0_rtx)
1632 || (GET_CODE (b) == GEU
1633 && opb1 == const1_rtx))
1634 return rtx_equal_p (op0, opb0);
1637 /* A != N is equivalent to A - (N + 1) <u -1. */
1638 if (GET_CODE (a) == NE
1639 && CONST_INT_P (op1)
1640 && GET_CODE (b) == LTU
1641 && opb1 == constm1_rtx
1642 && GET_CODE (opb0) == PLUS
1643 && CONST_INT_P (XEXP (opb0, 1))
1644 /* Avoid overflows. */
1645 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1646 != ((unsigned HOST_WIDE_INT)1
1647 << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
1648 && INTVAL (XEXP (opb0, 1)) + 1 == -INTVAL (op1))
1649 return rtx_equal_p (op0, XEXP (opb0, 0));
1651 /* Likewise, A != N implies A - N > 0. */
1652 if (GET_CODE (a) == NE
1653 && CONST_INT_P (op1))
1655 if (GET_CODE (b) == GTU
1656 && GET_CODE (opb0) == PLUS
1657 && opb1 == const0_rtx
1658 && CONST_INT_P (XEXP (opb0, 1))
1659 /* Avoid overflows. */
1660 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1661 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1662 && rtx_equal_p (XEXP (opb0, 0), op0))
1663 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1664 if (GET_CODE (b) == GEU
1665 && GET_CODE (opb0) == PLUS
1666 && opb1 == const1_rtx
1667 && CONST_INT_P (XEXP (opb0, 1))
1668 /* Avoid overflows. */
1669 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (opb0, 1))
1670 != ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
1671 && rtx_equal_p (XEXP (opb0, 0), op0))
1672 return INTVAL (op1) == -INTVAL (XEXP (opb0, 1));
1675 /* A >s X, where X is positive, implies A <u Y, if Y is negative. */
1676 if ((GET_CODE (a) == GT || GET_CODE (a) == GE)
1677 && CONST_INT_P (op1)
1678 && ((GET_CODE (a) == GT && op1 == constm1_rtx)
1679 || INTVAL (op1) >= 0)
1680 && GET_CODE (b) == LTU
1681 && CONST_INT_P (opb1)
1682 && rtx_equal_p (op0, opb0))
1683 return INTVAL (opb1) < 0;
1685 return false;
1688 /* Canonicalizes COND so that
1690 (1) Ensure that operands are ordered according to
1691 swap_commutative_operands_p.
1692 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1693 for GE, GEU, and LEU. */
1696 canon_condition (rtx cond)
1698 rtx op0, op1;
1699 enum rtx_code code;
1700 machine_mode mode;
1702 code = GET_CODE (cond);
1703 op0 = XEXP (cond, 0);
1704 op1 = XEXP (cond, 1);
1706 if (swap_commutative_operands_p (op0, op1))
1708 code = swap_condition (code);
1709 std::swap (op0, op1);
1712 mode = GET_MODE (op0);
1713 if (mode == VOIDmode)
1714 mode = GET_MODE (op1);
1715 gcc_assert (mode != VOIDmode);
1717 if (CONST_SCALAR_INT_P (op1) && GET_MODE_CLASS (mode) != MODE_CC)
1719 rtx_mode_t const_val (op1, mode);
1721 switch (code)
1723 case LE:
1724 if (wi::ne_p (const_val, wi::max_value (mode, SIGNED)))
1726 code = LT;
1727 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1729 break;
1731 case GE:
1732 if (wi::ne_p (const_val, wi::min_value (mode, SIGNED)))
1734 code = GT;
1735 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1737 break;
1739 case LEU:
1740 if (wi::ne_p (const_val, -1))
1742 code = LTU;
1743 op1 = immed_wide_int_const (wi::add (const_val, 1), mode);
1745 break;
1747 case GEU:
1748 if (wi::ne_p (const_val, 0))
1750 code = GTU;
1751 op1 = immed_wide_int_const (wi::sub (const_val, 1), mode);
1753 break;
1755 default:
1756 break;
1760 if (op0 != XEXP (cond, 0)
1761 || op1 != XEXP (cond, 1)
1762 || code != GET_CODE (cond)
1763 || GET_MODE (cond) != SImode)
1764 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1766 return cond;
1769 /* Reverses CONDition; returns NULL if we cannot. */
1771 static rtx
1772 reversed_condition (rtx cond)
1774 enum rtx_code reversed;
1775 reversed = reversed_comparison_code (cond, NULL);
1776 if (reversed == UNKNOWN)
1777 return NULL_RTX;
1778 else
1779 return gen_rtx_fmt_ee (reversed,
1780 GET_MODE (cond), XEXP (cond, 0),
1781 XEXP (cond, 1));
1784 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1785 set of altered regs. */
1787 void
1788 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1790 rtx rev, reve, exp = *expr;
1792 /* If some register gets altered later, we do not really speak about its
1793 value at the time of comparison. */
1794 if (altered && altered_reg_used (cond, altered))
1795 return;
1797 if (GET_CODE (cond) == EQ
1798 && REG_P (XEXP (cond, 0)) && CONSTANT_P (XEXP (cond, 1)))
1800 *expr = simplify_replace_rtx (*expr, XEXP (cond, 0), XEXP (cond, 1));
1801 return;
1804 if (!COMPARISON_P (exp))
1805 return;
1807 rev = reversed_condition (cond);
1808 reve = reversed_condition (exp);
1810 cond = canon_condition (cond);
1811 exp = canon_condition (exp);
1812 if (rev)
1813 rev = canon_condition (rev);
1814 if (reve)
1815 reve = canon_condition (reve);
1817 if (rtx_equal_p (exp, cond))
1819 *expr = const_true_rtx;
1820 return;
1823 if (rev && rtx_equal_p (exp, rev))
1825 *expr = const0_rtx;
1826 return;
1829 if (implies_p (cond, exp))
1831 *expr = const_true_rtx;
1832 return;
1835 if (reve && implies_p (cond, reve))
1837 *expr = const0_rtx;
1838 return;
1841 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1842 be false. */
1843 if (rev && implies_p (exp, rev))
1845 *expr = const0_rtx;
1846 return;
1849 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1850 if (rev && reve && implies_p (reve, rev))
1852 *expr = const_true_rtx;
1853 return;
1856 /* We would like to have some other tests here. TODO. */
1858 return;
1861 /* Use relationship between A and *B to eventually eliminate *B.
1862 OP is the operation we consider. */
1864 static void
1865 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1867 switch (op)
1869 case AND:
1870 /* If A implies *B, we may replace *B by true. */
1871 if (implies_p (a, *b))
1872 *b = const_true_rtx;
1873 break;
1875 case IOR:
1876 /* If *B implies A, we may replace *B by false. */
1877 if (implies_p (*b, a))
1878 *b = const0_rtx;
1879 break;
1881 default:
1882 gcc_unreachable ();
1886 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1887 operation we consider. */
1889 static void
1890 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1892 rtx elt;
1894 for (elt = tail; elt; elt = XEXP (elt, 1))
1895 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1896 for (elt = tail; elt; elt = XEXP (elt, 1))
1897 eliminate_implied_condition (op, XEXP (elt, 0), head);
1900 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1901 is a list, its elements are assumed to be combined using OP. */
1903 static void
1904 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1906 bool expression_valid;
1907 rtx head, tail, last_valid_expr;
1908 rtx_expr_list *cond_list;
1909 rtx_insn *insn;
1910 rtx neutral, aggr;
1911 regset altered, this_altered;
1912 edge e;
1914 if (!*expr)
1915 return;
1917 if (CONSTANT_P (*expr))
1918 return;
1920 if (GET_CODE (*expr) == EXPR_LIST)
1922 head = XEXP (*expr, 0);
1923 tail = XEXP (*expr, 1);
1925 eliminate_implied_conditions (op, &head, tail);
1927 switch (op)
1929 case AND:
1930 neutral = const_true_rtx;
1931 aggr = const0_rtx;
1932 break;
1934 case IOR:
1935 neutral = const0_rtx;
1936 aggr = const_true_rtx;
1937 break;
1939 default:
1940 gcc_unreachable ();
1943 simplify_using_initial_values (loop, UNKNOWN, &head);
1944 if (head == aggr)
1946 XEXP (*expr, 0) = aggr;
1947 XEXP (*expr, 1) = NULL_RTX;
1948 return;
1950 else if (head == neutral)
1952 *expr = tail;
1953 simplify_using_initial_values (loop, op, expr);
1954 return;
1956 simplify_using_initial_values (loop, op, &tail);
1958 if (tail && XEXP (tail, 0) == aggr)
1960 *expr = tail;
1961 return;
1964 XEXP (*expr, 0) = head;
1965 XEXP (*expr, 1) = tail;
1966 return;
1969 gcc_assert (op == UNKNOWN);
1971 replace_single_def_regs (expr);
1972 if (CONSTANT_P (*expr))
1973 return;
1975 e = loop_preheader_edge (loop);
1976 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1977 return;
1979 altered = ALLOC_REG_SET (&reg_obstack);
1980 this_altered = ALLOC_REG_SET (&reg_obstack);
1982 expression_valid = true;
1983 last_valid_expr = *expr;
1984 cond_list = NULL;
1985 while (1)
1987 insn = BB_END (e->src);
1988 if (any_condjump_p (insn))
1990 rtx cond = get_condition (BB_END (e->src), NULL, false, true);
1992 if (cond && (e->flags & EDGE_FALLTHRU))
1993 cond = reversed_condition (cond);
1994 if (cond)
1996 rtx old = *expr;
1997 simplify_using_condition (cond, expr, altered);
1998 if (old != *expr)
2000 rtx note;
2001 if (CONSTANT_P (*expr))
2002 goto out;
2003 for (note = cond_list; note; note = XEXP (note, 1))
2005 simplify_using_condition (XEXP (note, 0), expr, altered);
2006 if (CONSTANT_P (*expr))
2007 goto out;
2010 cond_list = alloc_EXPR_LIST (0, cond, cond_list);
2014 FOR_BB_INSNS_REVERSE (e->src, insn)
2016 rtx src, dest;
2017 rtx old = *expr;
2019 if (!INSN_P (insn))
2020 continue;
2022 CLEAR_REG_SET (this_altered);
2023 note_stores (PATTERN (insn), mark_altered, this_altered);
2024 if (CALL_P (insn))
2026 /* Kill all call clobbered registers. */
2027 unsigned int i;
2028 hard_reg_set_iterator hrsi;
2029 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call,
2030 0, i, hrsi)
2031 SET_REGNO_REG_SET (this_altered, i);
2034 if (suitable_set_for_replacement (insn, &dest, &src))
2036 rtx_expr_list **pnote, **pnote_next;
2038 replace_in_expr (expr, dest, src);
2039 if (CONSTANT_P (*expr))
2040 goto out;
2042 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2044 rtx note = *pnote;
2045 rtx old_cond = XEXP (note, 0);
2047 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2048 replace_in_expr (&XEXP (note, 0), dest, src);
2050 /* We can no longer use a condition that has been simplified
2051 to a constant, and simplify_using_condition will abort if
2052 we try. */
2053 if (CONSTANT_P (XEXP (note, 0)))
2055 *pnote = *pnote_next;
2056 pnote_next = pnote;
2057 free_EXPR_LIST_node (note);
2059 /* Retry simplifications with this condition if either the
2060 expression or the condition changed. */
2061 else if (old_cond != XEXP (note, 0) || old != *expr)
2062 simplify_using_condition (XEXP (note, 0), expr, altered);
2065 else
2067 rtx_expr_list **pnote, **pnote_next;
2069 /* If we did not use this insn to make a replacement, any overlap
2070 between stores in this insn and our expression will cause the
2071 expression to become invalid. */
2072 if (altered_reg_used (*expr, this_altered))
2073 goto out;
2075 /* Likewise for the conditions. */
2076 for (pnote = &cond_list; *pnote; pnote = pnote_next)
2078 rtx note = *pnote;
2079 rtx old_cond = XEXP (note, 0);
2081 pnote_next = (rtx_expr_list **)&XEXP (note, 1);
2082 if (altered_reg_used (old_cond, this_altered))
2084 *pnote = *pnote_next;
2085 pnote_next = pnote;
2086 free_EXPR_LIST_node (note);
2091 if (CONSTANT_P (*expr))
2092 goto out;
2094 IOR_REG_SET (altered, this_altered);
2096 /* If the expression now contains regs that have been altered, we
2097 can't return it to the caller. However, it is still valid for
2098 further simplification, so keep searching to see if we can
2099 eventually turn it into a constant. */
2100 if (altered_reg_used (*expr, altered))
2101 expression_valid = false;
2102 if (expression_valid)
2103 last_valid_expr = *expr;
2106 if (!single_pred_p (e->src)
2107 || single_pred (e->src) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
2108 break;
2109 e = single_pred_edge (e->src);
2112 out:
2113 free_EXPR_LIST_list (&cond_list);
2114 if (!CONSTANT_P (*expr))
2115 *expr = last_valid_expr;
2116 FREE_REG_SET (altered);
2117 FREE_REG_SET (this_altered);
2120 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
2121 that IV occurs as left operands of comparison COND and its signedness
2122 is SIGNED_P to DESC. */
2124 static void
2125 shorten_into_mode (struct rtx_iv *iv, machine_mode mode,
2126 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
2128 rtx mmin, mmax, cond_over, cond_under;
2130 get_mode_bounds (mode, signed_p, iv->extend_mode, &mmin, &mmax);
2131 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
2132 iv->base, mmin);
2133 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
2134 iv->base, mmax);
2136 switch (cond)
2138 case LE:
2139 case LT:
2140 case LEU:
2141 case LTU:
2142 if (cond_under != const0_rtx)
2143 desc->infinite =
2144 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2145 if (cond_over != const0_rtx)
2146 desc->noloop_assumptions =
2147 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
2148 break;
2150 case GE:
2151 case GT:
2152 case GEU:
2153 case GTU:
2154 if (cond_over != const0_rtx)
2155 desc->infinite =
2156 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2157 if (cond_under != const0_rtx)
2158 desc->noloop_assumptions =
2159 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
2160 break;
2162 case NE:
2163 if (cond_over != const0_rtx)
2164 desc->infinite =
2165 alloc_EXPR_LIST (0, cond_over, desc->infinite);
2166 if (cond_under != const0_rtx)
2167 desc->infinite =
2168 alloc_EXPR_LIST (0, cond_under, desc->infinite);
2169 break;
2171 default:
2172 gcc_unreachable ();
2175 iv->mode = mode;
2176 iv->extend = signed_p ? IV_SIGN_EXTEND : IV_ZERO_EXTEND;
2179 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
2180 subregs of the same mode if possible (sometimes it is necessary to add
2181 some assumptions to DESC). */
2183 static bool
2184 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
2185 enum rtx_code cond, struct niter_desc *desc)
2187 machine_mode comp_mode;
2188 bool signed_p;
2190 /* If the ivs behave specially in the first iteration, or are
2191 added/multiplied after extending, we ignore them. */
2192 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
2193 return false;
2194 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
2195 return false;
2197 /* If there is some extend, it must match signedness of the comparison. */
2198 switch (cond)
2200 case LE:
2201 case LT:
2202 if (iv0->extend == IV_ZERO_EXTEND
2203 || iv1->extend == IV_ZERO_EXTEND)
2204 return false;
2205 signed_p = true;
2206 break;
2208 case LEU:
2209 case LTU:
2210 if (iv0->extend == IV_SIGN_EXTEND
2211 || iv1->extend == IV_SIGN_EXTEND)
2212 return false;
2213 signed_p = false;
2214 break;
2216 case NE:
2217 if (iv0->extend != IV_UNKNOWN_EXTEND
2218 && iv1->extend != IV_UNKNOWN_EXTEND
2219 && iv0->extend != iv1->extend)
2220 return false;
2222 signed_p = false;
2223 if (iv0->extend != IV_UNKNOWN_EXTEND)
2224 signed_p = iv0->extend == IV_SIGN_EXTEND;
2225 if (iv1->extend != IV_UNKNOWN_EXTEND)
2226 signed_p = iv1->extend == IV_SIGN_EXTEND;
2227 break;
2229 default:
2230 gcc_unreachable ();
2233 /* Values of both variables should be computed in the same mode. These
2234 might indeed be different, if we have comparison like
2236 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
2238 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
2239 in different modes. This does not seem impossible to handle, but
2240 it hardly ever occurs in practice.
2242 The only exception is the case when one of operands is invariant.
2243 For example pentium 3 generates comparisons like
2244 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
2245 definitely do not want this prevent the optimization. */
2246 comp_mode = iv0->extend_mode;
2247 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
2248 comp_mode = iv1->extend_mode;
2250 if (iv0->extend_mode != comp_mode)
2252 if (iv0->mode != iv0->extend_mode
2253 || iv0->step != const0_rtx)
2254 return false;
2256 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2257 comp_mode, iv0->base, iv0->mode);
2258 iv0->extend_mode = comp_mode;
2261 if (iv1->extend_mode != comp_mode)
2263 if (iv1->mode != iv1->extend_mode
2264 || iv1->step != const0_rtx)
2265 return false;
2267 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
2268 comp_mode, iv1->base, iv1->mode);
2269 iv1->extend_mode = comp_mode;
2272 /* Check that both ivs belong to a range of a single mode. If one of the
2273 operands is an invariant, we may need to shorten it into the common
2274 mode. */
2275 if (iv0->mode == iv0->extend_mode
2276 && iv0->step == const0_rtx
2277 && iv0->mode != iv1->mode)
2278 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
2280 if (iv1->mode == iv1->extend_mode
2281 && iv1->step == const0_rtx
2282 && iv0->mode != iv1->mode)
2283 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
2285 if (iv0->mode != iv1->mode)
2286 return false;
2288 desc->mode = iv0->mode;
2289 desc->signed_p = signed_p;
2291 return true;
2294 /* Tries to estimate the maximum number of iterations in LOOP, and return the
2295 result. This function is called from iv_number_of_iterations with
2296 a number of fields in DESC already filled in. OLD_NITER is the original
2297 expression for the number of iterations, before we tried to simplify it. */
2299 static uint64_t
2300 determine_max_iter (struct loop *loop, struct niter_desc *desc, rtx old_niter)
2302 rtx niter = desc->niter_expr;
2303 rtx mmin, mmax, cmp;
2304 uint64_t nmax, inc;
2305 uint64_t andmax = 0;
2307 /* We used to look for constant operand 0 of AND,
2308 but canonicalization should always make this impossible. */
2309 gcc_checking_assert (GET_CODE (niter) != AND
2310 || !CONST_INT_P (XEXP (niter, 0)));
2312 if (GET_CODE (niter) == AND
2313 && CONST_INT_P (XEXP (niter, 1)))
2315 andmax = UINTVAL (XEXP (niter, 1));
2316 niter = XEXP (niter, 0);
2319 get_mode_bounds (desc->mode, desc->signed_p, desc->mode, &mmin, &mmax);
2320 nmax = UINTVAL (mmax) - UINTVAL (mmin);
2322 if (GET_CODE (niter) == UDIV)
2324 if (!CONST_INT_P (XEXP (niter, 1)))
2325 return nmax;
2326 inc = INTVAL (XEXP (niter, 1));
2327 niter = XEXP (niter, 0);
2329 else
2330 inc = 1;
2332 /* We could use a binary search here, but for now improving the upper
2333 bound by just one eliminates one important corner case. */
2334 cmp = simplify_gen_relational (desc->signed_p ? LT : LTU, VOIDmode,
2335 desc->mode, old_niter, mmax);
2336 simplify_using_initial_values (loop, UNKNOWN, &cmp);
2337 if (cmp == const_true_rtx)
2339 nmax--;
2341 if (dump_file)
2342 fprintf (dump_file, ";; improved upper bound by one.\n");
2344 nmax /= inc;
2345 if (andmax)
2346 nmax = MIN (nmax, andmax);
2347 if (dump_file)
2348 fprintf (dump_file, ";; Determined upper bound %" PRId64".\n",
2349 nmax);
2350 return nmax;
2353 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
2354 the result into DESC. Very similar to determine_number_of_iterations
2355 (basically its rtl version), complicated by things like subregs. */
2357 static void
2358 iv_number_of_iterations (struct loop *loop, rtx_insn *insn, rtx condition,
2359 struct niter_desc *desc)
2361 rtx op0, op1, delta, step, bound, may_xform, tmp, tmp0, tmp1;
2362 struct rtx_iv iv0, iv1, tmp_iv;
2363 rtx assumption, may_not_xform;
2364 enum rtx_code cond;
2365 machine_mode mode, comp_mode;
2366 rtx mmin, mmax, mode_mmin, mode_mmax;
2367 uint64_t s, size, d, inv, max;
2368 int64_t up, down, inc, step_val;
2369 int was_sharp = false;
2370 rtx old_niter;
2371 bool step_is_pow2;
2373 /* The meaning of these assumptions is this:
2374 if !assumptions
2375 then the rest of information does not have to be valid
2376 if noloop_assumptions then the loop does not roll
2377 if infinite then this exit is never used */
2379 desc->assumptions = NULL_RTX;
2380 desc->noloop_assumptions = NULL_RTX;
2381 desc->infinite = NULL_RTX;
2382 desc->simple_p = true;
2384 desc->const_iter = false;
2385 desc->niter_expr = NULL_RTX;
2387 cond = GET_CODE (condition);
2388 gcc_assert (COMPARISON_P (condition));
2390 mode = GET_MODE (XEXP (condition, 0));
2391 if (mode == VOIDmode)
2392 mode = GET_MODE (XEXP (condition, 1));
2393 /* The constant comparisons should be folded. */
2394 gcc_assert (mode != VOIDmode);
2396 /* We only handle integers or pointers. */
2397 if (GET_MODE_CLASS (mode) != MODE_INT
2398 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
2399 goto fail;
2401 op0 = XEXP (condition, 0);
2402 if (!iv_analyze (insn, op0, &iv0))
2403 goto fail;
2404 if (iv0.extend_mode == VOIDmode)
2405 iv0.mode = iv0.extend_mode = mode;
2407 op1 = XEXP (condition, 1);
2408 if (!iv_analyze (insn, op1, &iv1))
2409 goto fail;
2410 if (iv1.extend_mode == VOIDmode)
2411 iv1.mode = iv1.extend_mode = mode;
2413 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
2414 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
2415 goto fail;
2417 /* Check condition and normalize it. */
2419 switch (cond)
2421 case GE:
2422 case GT:
2423 case GEU:
2424 case GTU:
2425 tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
2426 cond = swap_condition (cond);
2427 break;
2428 case NE:
2429 case LE:
2430 case LEU:
2431 case LT:
2432 case LTU:
2433 break;
2434 default:
2435 goto fail;
2438 /* Handle extends. This is relatively nontrivial, so we only try in some
2439 easy cases, when we can canonicalize the ivs (possibly by adding some
2440 assumptions) to shape subreg (base + i * step). This function also fills
2441 in desc->mode and desc->signed_p. */
2443 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
2444 goto fail;
2446 comp_mode = iv0.extend_mode;
2447 mode = iv0.mode;
2448 size = GET_MODE_PRECISION (mode);
2449 get_mode_bounds (mode, (cond == LE || cond == LT), comp_mode, &mmin, &mmax);
2450 mode_mmin = lowpart_subreg (mode, mmin, comp_mode);
2451 mode_mmax = lowpart_subreg (mode, mmax, comp_mode);
2453 if (!CONST_INT_P (iv0.step) || !CONST_INT_P (iv1.step))
2454 goto fail;
2456 /* We can take care of the case of two induction variables chasing each other
2457 if the test is NE. I have never seen a loop using it, but still it is
2458 cool. */
2459 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
2461 if (cond != NE)
2462 goto fail;
2464 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2465 iv1.step = const0_rtx;
2468 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2469 iv1.step = lowpart_subreg (mode, iv1.step, comp_mode);
2471 /* This is either infinite loop or the one that ends immediately, depending
2472 on initial values. Unswitching should remove this kind of conditions. */
2473 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
2474 goto fail;
2476 if (cond != NE)
2478 if (iv0.step == const0_rtx)
2479 step_val = -INTVAL (iv1.step);
2480 else
2481 step_val = INTVAL (iv0.step);
2483 /* Ignore loops of while (i-- < 10) type. */
2484 if (step_val < 0)
2485 goto fail;
2487 step_is_pow2 = !(step_val & (step_val - 1));
2489 else
2491 /* We do not care about whether the step is power of two in this
2492 case. */
2493 step_is_pow2 = false;
2494 step_val = 0;
2497 /* Some more condition normalization. We must record some assumptions
2498 due to overflows. */
2499 switch (cond)
2501 case LT:
2502 case LTU:
2503 /* We want to take care only of non-sharp relationals; this is easy,
2504 as in cases the overflow would make the transformation unsafe
2505 the loop does not roll. Seemingly it would make more sense to want
2506 to take care of sharp relationals instead, as NE is more similar to
2507 them, but the problem is that here the transformation would be more
2508 difficult due to possibly infinite loops. */
2509 if (iv0.step == const0_rtx)
2511 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2512 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2513 mode_mmax);
2514 if (assumption == const_true_rtx)
2515 goto zero_iter_simplify;
2516 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2517 iv0.base, const1_rtx);
2519 else
2521 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2522 assumption = simplify_gen_relational (EQ, SImode, mode, tmp,
2523 mode_mmin);
2524 if (assumption == const_true_rtx)
2525 goto zero_iter_simplify;
2526 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2527 iv1.base, constm1_rtx);
2530 if (assumption != const0_rtx)
2531 desc->noloop_assumptions =
2532 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2533 cond = (cond == LT) ? LE : LEU;
2535 /* It will be useful to be able to tell the difference once more in
2536 LE -> NE reduction. */
2537 was_sharp = true;
2538 break;
2539 default: ;
2542 /* Take care of trivially infinite loops. */
2543 if (cond != NE)
2545 if (iv0.step == const0_rtx)
2547 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2548 if (rtx_equal_p (tmp, mode_mmin))
2550 desc->infinite =
2551 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2552 /* Fill in the remaining fields somehow. */
2553 goto zero_iter_simplify;
2556 else
2558 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2559 if (rtx_equal_p (tmp, mode_mmax))
2561 desc->infinite =
2562 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2563 /* Fill in the remaining fields somehow. */
2564 goto zero_iter_simplify;
2569 /* If we can we want to take care of NE conditions instead of size
2570 comparisons, as they are much more friendly (most importantly
2571 this takes care of special handling of loops with step 1). We can
2572 do it if we first check that upper bound is greater or equal to
2573 lower bound, their difference is constant c modulo step and that
2574 there is not an overflow. */
2575 if (cond != NE)
2577 if (iv0.step == const0_rtx)
2578 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2579 else
2580 step = iv0.step;
2581 step = lowpart_subreg (mode, step, comp_mode);
2582 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2583 delta = lowpart_subreg (mode, delta, comp_mode);
2584 delta = simplify_gen_binary (UMOD, mode, delta, step);
2585 may_xform = const0_rtx;
2586 may_not_xform = const_true_rtx;
2588 if (CONST_INT_P (delta))
2590 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2592 /* A special case. We have transformed condition of type
2593 for (i = 0; i < 4; i += 4)
2594 into
2595 for (i = 0; i <= 3; i += 4)
2596 obviously if the test for overflow during that transformation
2597 passed, we cannot overflow here. Most importantly any
2598 loop with sharp end condition and step 1 falls into this
2599 category, so handling this case specially is definitely
2600 worth the troubles. */
2601 may_xform = const_true_rtx;
2603 else if (iv0.step == const0_rtx)
2605 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2606 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2607 bound = lowpart_subreg (mode, bound, comp_mode);
2608 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2609 may_xform = simplify_gen_relational (cond, SImode, mode,
2610 bound, tmp);
2611 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2612 SImode, mode,
2613 bound, tmp);
2615 else
2617 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2618 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2619 bound = lowpart_subreg (mode, bound, comp_mode);
2620 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2621 may_xform = simplify_gen_relational (cond, SImode, mode,
2622 tmp, bound);
2623 may_not_xform = simplify_gen_relational (reverse_condition (cond),
2624 SImode, mode,
2625 tmp, bound);
2629 if (may_xform != const0_rtx)
2631 /* We perform the transformation always provided that it is not
2632 completely senseless. This is OK, as we would need this assumption
2633 to determine the number of iterations anyway. */
2634 if (may_xform != const_true_rtx)
2636 /* If the step is a power of two and the final value we have
2637 computed overflows, the cycle is infinite. Otherwise it
2638 is nontrivial to compute the number of iterations. */
2639 if (step_is_pow2)
2640 desc->infinite = alloc_EXPR_LIST (0, may_not_xform,
2641 desc->infinite);
2642 else
2643 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2644 desc->assumptions);
2647 /* We are going to lose some information about upper bound on
2648 number of iterations in this step, so record the information
2649 here. */
2650 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2651 if (CONST_INT_P (iv1.base))
2652 up = INTVAL (iv1.base);
2653 else
2654 up = INTVAL (mode_mmax) - inc;
2655 down = INTVAL (CONST_INT_P (iv0.base)
2656 ? iv0.base
2657 : mode_mmin);
2658 max = (uint64_t) (up - down) / inc + 1;
2659 if (!desc->infinite
2660 && !desc->assumptions)
2661 record_niter_bound (loop, max, false, true);
2663 if (iv0.step == const0_rtx)
2665 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2666 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2668 else
2670 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2671 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2674 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2675 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2676 assumption = simplify_gen_relational (reverse_condition (cond),
2677 SImode, mode, tmp0, tmp1);
2678 if (assumption == const_true_rtx)
2679 goto zero_iter_simplify;
2680 else if (assumption != const0_rtx)
2681 desc->noloop_assumptions =
2682 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2683 cond = NE;
2687 /* Count the number of iterations. */
2688 if (cond == NE)
2690 /* Everything we do here is just arithmetics modulo size of mode. This
2691 makes us able to do more involved computations of number of iterations
2692 than in other cases. First transform the condition into shape
2693 s * i <> c, with s positive. */
2694 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2695 iv0.base = const0_rtx;
2696 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2697 iv1.step = const0_rtx;
2698 if (INTVAL (iv0.step) < 0)
2700 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, comp_mode);
2701 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, comp_mode);
2703 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2705 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2706 is infinite. Otherwise, the number of iterations is
2707 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2708 s = INTVAL (iv0.step); d = 1;
2709 while (s % 2 != 1)
2711 s /= 2;
2712 d *= 2;
2713 size--;
2715 bound = GEN_INT (((uint64_t) 1 << (size - 1 ) << 1) - 1);
2717 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2718 tmp = simplify_gen_binary (UMOD, mode, tmp1, gen_int_mode (d, mode));
2719 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2720 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2722 tmp = simplify_gen_binary (UDIV, mode, tmp1, gen_int_mode (d, mode));
2723 inv = inverse (s, size);
2724 tmp = simplify_gen_binary (MULT, mode, tmp, gen_int_mode (inv, mode));
2725 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2727 else
2729 if (iv1.step == const0_rtx)
2730 /* Condition in shape a + s * i <= b
2731 We must know that b + s does not overflow and a <= b + s and then we
2732 can compute number of iterations as (b + s - a) / s. (It might
2733 seem that we in fact could be more clever about testing the b + s
2734 overflow condition using some information about b - a mod s,
2735 but it was already taken into account during LE -> NE transform). */
2737 step = iv0.step;
2738 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2739 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2741 bound = simplify_gen_binary (MINUS, mode, mode_mmax,
2742 lowpart_subreg (mode, step,
2743 comp_mode));
2744 if (step_is_pow2)
2746 rtx t0, t1;
2748 /* If s is power of 2, we know that the loop is infinite if
2749 a % s <= b % s and b + s overflows. */
2750 assumption = simplify_gen_relational (reverse_condition (cond),
2751 SImode, mode,
2752 tmp1, bound);
2754 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2755 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2756 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2757 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2758 desc->infinite =
2759 alloc_EXPR_LIST (0, assumption, desc->infinite);
2761 else
2763 assumption = simplify_gen_relational (cond, SImode, mode,
2764 tmp1, bound);
2765 desc->assumptions =
2766 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2769 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2770 tmp = lowpart_subreg (mode, tmp, comp_mode);
2771 assumption = simplify_gen_relational (reverse_condition (cond),
2772 SImode, mode, tmp0, tmp);
2774 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2775 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2777 else
2779 /* Condition in shape a <= b - s * i
2780 We must know that a - s does not overflow and a - s <= b and then
2781 we can again compute number of iterations as (b - (a - s)) / s. */
2782 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2783 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2784 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2786 bound = simplify_gen_binary (PLUS, mode, mode_mmin,
2787 lowpart_subreg (mode, step, comp_mode));
2788 if (step_is_pow2)
2790 rtx t0, t1;
2792 /* If s is power of 2, we know that the loop is infinite if
2793 a % s <= b % s and a - s overflows. */
2794 assumption = simplify_gen_relational (reverse_condition (cond),
2795 SImode, mode,
2796 bound, tmp0);
2798 t0 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp0), step);
2799 t1 = simplify_gen_binary (UMOD, mode, copy_rtx (tmp1), step);
2800 tmp = simplify_gen_relational (cond, SImode, mode, t0, t1);
2801 assumption = simplify_gen_binary (AND, SImode, assumption, tmp);
2802 desc->infinite =
2803 alloc_EXPR_LIST (0, assumption, desc->infinite);
2805 else
2807 assumption = simplify_gen_relational (cond, SImode, mode,
2808 bound, tmp0);
2809 desc->assumptions =
2810 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2813 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2814 tmp = lowpart_subreg (mode, tmp, comp_mode);
2815 assumption = simplify_gen_relational (reverse_condition (cond),
2816 SImode, mode,
2817 tmp, tmp1);
2818 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2819 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2821 if (assumption == const_true_rtx)
2822 goto zero_iter_simplify;
2823 else if (assumption != const0_rtx)
2824 desc->noloop_assumptions =
2825 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2826 delta = simplify_gen_binary (UDIV, mode, delta, step);
2827 desc->niter_expr = delta;
2830 old_niter = desc->niter_expr;
2832 simplify_using_initial_values (loop, AND, &desc->assumptions);
2833 if (desc->assumptions
2834 && XEXP (desc->assumptions, 0) == const0_rtx)
2835 goto fail;
2836 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2837 simplify_using_initial_values (loop, IOR, &desc->infinite);
2838 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2840 /* Rerun the simplification. Consider code (created by copying loop headers)
2842 i = 0;
2844 if (0 < n)
2848 i++;
2849 } while (i < n);
2852 The first pass determines that i = 0, the second pass uses it to eliminate
2853 noloop assumption. */
2855 simplify_using_initial_values (loop, AND, &desc->assumptions);
2856 if (desc->assumptions
2857 && XEXP (desc->assumptions, 0) == const0_rtx)
2858 goto fail;
2859 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2860 simplify_using_initial_values (loop, IOR, &desc->infinite);
2861 simplify_using_initial_values (loop, UNKNOWN, &desc->niter_expr);
2863 if (desc->noloop_assumptions
2864 && XEXP (desc->noloop_assumptions, 0) == const_true_rtx)
2865 goto zero_iter;
2867 if (CONST_INT_P (desc->niter_expr))
2869 uint64_t val = INTVAL (desc->niter_expr);
2871 desc->const_iter = true;
2872 desc->niter = val & GET_MODE_MASK (desc->mode);
2873 if (!desc->infinite
2874 && !desc->assumptions)
2875 record_niter_bound (loop, desc->niter, false, true);
2877 else
2879 max = determine_max_iter (loop, desc, old_niter);
2880 if (!max)
2881 goto zero_iter_simplify;
2882 if (!desc->infinite
2883 && !desc->assumptions)
2884 record_niter_bound (loop, max, false, true);
2886 /* simplify_using_initial_values does a copy propagation on the registers
2887 in the expression for the number of iterations. This prolongs life
2888 ranges of registers and increases register pressure, and usually
2889 brings no gain (and if it happens to do, the cse pass will take care
2890 of it anyway). So prevent this behavior, unless it enabled us to
2891 derive that the number of iterations is a constant. */
2892 desc->niter_expr = old_niter;
2895 return;
2897 zero_iter_simplify:
2898 /* Simplify the assumptions. */
2899 simplify_using_initial_values (loop, AND, &desc->assumptions);
2900 if (desc->assumptions
2901 && XEXP (desc->assumptions, 0) == const0_rtx)
2902 goto fail;
2903 simplify_using_initial_values (loop, IOR, &desc->infinite);
2905 /* Fallthru. */
2906 zero_iter:
2907 desc->const_iter = true;
2908 desc->niter = 0;
2909 record_niter_bound (loop, 0, true, true);
2910 desc->noloop_assumptions = NULL_RTX;
2911 desc->niter_expr = const0_rtx;
2912 return;
2914 fail:
2915 desc->simple_p = false;
2916 return;
2919 /* Checks whether E is a simple exit from LOOP and stores its description
2920 into DESC. */
2922 static void
2923 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2925 basic_block exit_bb;
2926 rtx condition;
2927 rtx_insn *at;
2928 edge ein;
2930 exit_bb = e->src;
2931 desc->simple_p = false;
2933 /* It must belong directly to the loop. */
2934 if (exit_bb->loop_father != loop)
2935 return;
2937 /* It must be tested (at least) once during any iteration. */
2938 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2939 return;
2941 /* It must end in a simple conditional jump. */
2942 if (!any_condjump_p (BB_END (exit_bb)))
2943 return;
2945 ein = EDGE_SUCC (exit_bb, 0);
2946 if (ein == e)
2947 ein = EDGE_SUCC (exit_bb, 1);
2949 desc->out_edge = e;
2950 desc->in_edge = ein;
2952 /* Test whether the condition is suitable. */
2953 if (!(condition = get_condition (BB_END (ein->src), &at, false, false)))
2954 return;
2956 if (ein->flags & EDGE_FALLTHRU)
2958 condition = reversed_condition (condition);
2959 if (!condition)
2960 return;
2963 /* Check that we are able to determine number of iterations and fill
2964 in information about it. */
2965 iv_number_of_iterations (loop, at, condition, desc);
2968 /* Finds a simple exit of LOOP and stores its description into DESC. */
2970 void
2971 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2973 unsigned i;
2974 basic_block *body;
2975 edge e;
2976 struct niter_desc act;
2977 bool any = false;
2978 edge_iterator ei;
2980 desc->simple_p = false;
2981 body = get_loop_body (loop);
2983 for (i = 0; i < loop->num_nodes; i++)
2985 FOR_EACH_EDGE (e, ei, body[i]->succs)
2987 if (flow_bb_inside_loop_p (loop, e->dest))
2988 continue;
2990 check_simple_exit (loop, e, &act);
2991 if (!act.simple_p)
2992 continue;
2994 if (!any)
2995 any = true;
2996 else
2998 /* Prefer constant iterations; the less the better. */
2999 if (!act.const_iter
3000 || (desc->const_iter && act.niter >= desc->niter))
3001 continue;
3003 /* Also if the actual exit may be infinite, while the old one
3004 not, prefer the old one. */
3005 if (act.infinite && !desc->infinite)
3006 continue;
3009 *desc = act;
3013 if (dump_file)
3015 if (desc->simple_p)
3017 fprintf (dump_file, "Loop %d is simple:\n", loop->num);
3018 fprintf (dump_file, " simple exit %d -> %d\n",
3019 desc->out_edge->src->index,
3020 desc->out_edge->dest->index);
3021 if (desc->assumptions)
3023 fprintf (dump_file, " assumptions: ");
3024 print_rtl (dump_file, desc->assumptions);
3025 fprintf (dump_file, "\n");
3027 if (desc->noloop_assumptions)
3029 fprintf (dump_file, " does not roll if: ");
3030 print_rtl (dump_file, desc->noloop_assumptions);
3031 fprintf (dump_file, "\n");
3033 if (desc->infinite)
3035 fprintf (dump_file, " infinite if: ");
3036 print_rtl (dump_file, desc->infinite);
3037 fprintf (dump_file, "\n");
3040 fprintf (dump_file, " number of iterations: ");
3041 print_rtl (dump_file, desc->niter_expr);
3042 fprintf (dump_file, "\n");
3044 fprintf (dump_file, " upper bound: %li\n",
3045 (long)get_max_loop_iterations_int (loop));
3046 fprintf (dump_file, " realistic bound: %li\n",
3047 (long)get_estimated_loop_iterations_int (loop));
3049 else
3050 fprintf (dump_file, "Loop %d is not simple.\n", loop->num);
3053 free (body);
3056 /* Creates a simple loop description of LOOP if it was not computed
3057 already. */
3059 struct niter_desc *
3060 get_simple_loop_desc (struct loop *loop)
3062 struct niter_desc *desc = simple_loop_desc (loop);
3064 if (desc)
3065 return desc;
3067 /* At least desc->infinite is not always initialized by
3068 find_simple_loop_exit. */
3069 desc = ggc_cleared_alloc<niter_desc> ();
3070 iv_analysis_loop_init (loop);
3071 find_simple_exit (loop, desc);
3072 loop->simple_loop_desc = desc;
3074 if (desc->simple_p && (desc->assumptions || desc->infinite))
3076 const char *wording;
3078 /* Assume that no overflow happens and that the loop is finite.
3079 We already warned at the tree level if we ran optimizations there. */
3080 if (!flag_tree_loop_optimize && warn_unsafe_loop_optimizations)
3082 if (desc->infinite)
3084 wording =
3085 flag_unsafe_loop_optimizations
3086 ? N_("assuming that the loop is not infinite")
3087 : N_("cannot optimize possibly infinite loops");
3088 warning (OPT_Wunsafe_loop_optimizations, "%s",
3089 gettext (wording));
3091 if (desc->assumptions)
3093 wording =
3094 flag_unsafe_loop_optimizations
3095 ? N_("assuming that the loop counter does not overflow")
3096 : N_("cannot optimize loop, the loop counter may overflow");
3097 warning (OPT_Wunsafe_loop_optimizations, "%s",
3098 gettext (wording));
3102 if (flag_unsafe_loop_optimizations)
3104 desc->assumptions = NULL_RTX;
3105 desc->infinite = NULL_RTX;
3109 return desc;
3112 /* Releases simple loop description for LOOP. */
3114 void
3115 free_simple_loop_desc (struct loop *loop)
3117 struct niter_desc *desc = simple_loop_desc (loop);
3119 if (!desc)
3120 return;
3122 ggc_free (desc);
3123 loop->simple_loop_desc = NULL;