2004-02-19 David Daney <ddaney@avtrex.com>
[official-gcc.git] / gcc / loop-iv.c
blobefdcc7395985ceb295560d82b6dca4185b8f4468
1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 /* This is just a very simplistic analysis of induction variables of the loop.
22 The major use is for determining the number of iterations of a loop for
23 loop unrolling, doloop optimization and branch prediction. For this we
24 are only interested in bivs and a fairly limited set of givs that are
25 needed in the exit condition. We also only compute the iv information on
26 demand.
28 The interesting registers are determined. A register is interesting if
30 -- it is set only in the blocks that dominate the latch of the current loop
31 -- all its sets are simple -- i.e. in the form we understand
33 We also number the insns sequentially in each basic block. For a use of the
34 interesting reg, it is now easy to find a reaching definition (there may be
35 only one).
37 Induction variable is then simply analysed by walking the use-def
38 chains.
40 Usage:
42 iv_analysis_loop_init (loop);
43 insn = iv_get_reaching_def (where, reg);
44 if (iv_analyse (insn, reg, &iv))
46 ...
48 iv_analysis_done (); */
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 "basic-block.h"
57 #include "cfgloop.h"
58 #include "expr.h"
59 #include "output.h"
61 /* The insn information. */
63 struct insn_info
65 /* Id of the insn. */
66 unsigned luid;
68 /* The previous definition of the register defined by the single
69 set in the insn. */
70 rtx prev_def;
72 /* The description of the iv. */
73 struct rtx_iv iv;
76 static struct insn_info *insn_info;
78 /* The last definition of register. */
80 static rtx *last_def;
82 /* The bivs. */
84 static struct rtx_iv *bivs;
86 /* Maximal insn number for that there is place in insn_info array. */
88 static unsigned max_insn_no;
90 /* Maximal register number for that there is place in bivs and last_def
91 arrays. */
93 static unsigned max_reg_no;
95 /* Dumps information about IV to FILE. */
97 extern void dump_iv_info (FILE *, struct rtx_iv *);
98 void
99 dump_iv_info (FILE *file, struct rtx_iv *iv)
101 if (!iv->base)
103 fprintf (file, "not simple");
104 return;
107 if (iv->step == const0_rtx)
109 fprintf (file, "invariant ");
110 print_rtl (file, iv->base);
111 return;
114 print_rtl (file, iv->base);
115 fprintf (file, " + ");
116 print_rtl (file, iv->step);
117 fprintf (file, " * iteration");
118 fprintf (file, " (in %s)", GET_MODE_NAME (iv->mode));
120 if (iv->mode != iv->extend_mode)
121 fprintf (file, " %s to %s",
122 rtx_name[iv->extend],
123 GET_MODE_NAME (iv->extend_mode));
125 if (iv->mult != const1_rtx)
127 fprintf (file, " * ");
128 print_rtl (file, iv->mult);
130 if (iv->delta != const0_rtx)
132 fprintf (file, " + ");
133 print_rtl (file, iv->delta);
135 if (iv->first_special)
136 fprintf (file, " (first special)");
139 /* Assigns luids to insns in basic block BB. */
141 static void
142 assign_luids (basic_block bb)
144 unsigned i = 0, uid;
145 rtx insn;
147 FOR_BB_INSNS (bb, insn)
149 uid = INSN_UID (insn);
150 insn_info[uid].luid = i++;
151 insn_info[uid].prev_def = NULL_RTX;
152 insn_info[uid].iv.analysed = false;
156 /* Generates a subreg to get the least significant part of EXPR (in mode
157 INNER_MODE) to OUTER_MODE. */
159 static rtx
160 lowpart_subreg (enum machine_mode outer_mode, rtx expr,
161 enum machine_mode inner_mode)
163 return simplify_gen_subreg (outer_mode, expr, inner_mode,
164 subreg_lowpart_offset (outer_mode, inner_mode));
167 /* Checks whether REG is a well-behaved register. */
169 static bool
170 simple_reg_p (rtx reg)
172 unsigned r;
174 if (GET_CODE (reg) == SUBREG)
176 if (!subreg_lowpart_p (reg))
177 return false;
178 reg = SUBREG_REG (reg);
181 if (!REG_P (reg))
182 return false;
184 r = REGNO (reg);
185 if (HARD_REGISTER_NUM_P (r))
186 return false;
188 if (GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
189 return false;
191 if (last_def[r] == const0_rtx)
192 return false;
194 return true;
197 /* Checks whether assignment LHS = RHS is simple enough for us to process. */
199 static bool
200 simple_set_p (rtx lhs, rtx rhs)
202 rtx op0, op1;
204 if (!REG_P (lhs)
205 || !simple_reg_p (lhs))
206 return false;
208 if (CONSTANT_P (rhs))
209 return true;
211 switch (GET_CODE (rhs))
213 case SUBREG:
214 case REG:
215 return simple_reg_p (rhs);
217 case SIGN_EXTEND:
218 case ZERO_EXTEND:
219 case NEG:
220 return simple_reg_p (XEXP (rhs, 0));
222 case PLUS:
223 case MINUS:
224 case MULT:
225 op0 = XEXP (rhs, 0);
226 op1 = XEXP (rhs, 1);
228 if (!simple_reg_p (op0)
229 && !CONSTANT_P (op0))
230 return false;
232 if (!simple_reg_p (op1)
233 && !CONSTANT_P (op1))
234 return false;
236 if (GET_CODE (rhs) == MULT
237 && !CONSTANT_P (op0)
238 && !CONSTANT_P (op1))
239 return false;
241 return true;
243 default:
244 return false;
248 /* Mark single SET in INSN. */
250 static rtx
251 mark_single_set (rtx insn, rtx set)
253 rtx def = SET_DEST (set), src;
254 unsigned regno, uid;
256 src = find_reg_equal_equiv_note (insn);
257 if (!src)
258 src = SET_SRC (set);
260 if (!simple_set_p (SET_DEST (set), src))
261 return NULL_RTX;
263 regno = REGNO (def);
264 uid = INSN_UID (insn);
266 bivs[regno].analysed = false;
267 insn_info[uid].prev_def = last_def[regno];
268 last_def[regno] = insn;
270 return def;
273 /* Invalidate register REG unless it is equal to EXCEPT. */
275 static void
276 kill_sets (rtx reg, rtx by ATTRIBUTE_UNUSED, void *except)
278 if (GET_CODE (reg) == SUBREG)
279 reg = SUBREG_REG (reg);
280 if (!REG_P (reg))
281 return;
282 if (reg == except)
283 return;
285 last_def[REGNO (reg)] = const0_rtx;
288 /* Marks sets in basic block BB. If DOM is true, BB dominates the loop
289 latch. */
291 static void
292 mark_sets (basic_block bb, bool dom)
294 rtx insn, set, def;
296 FOR_BB_INSNS (bb, insn)
298 if (!INSN_P (insn))
299 continue;
301 if (dom
302 && (set = single_set (insn)))
303 def = mark_single_set (insn, set);
304 else
305 def = NULL_RTX;
307 note_stores (PATTERN (insn), kill_sets, def);
311 /* Prepare the data for an induction variable analysis of a LOOP. */
313 void
314 iv_analysis_loop_init (struct loop *loop)
316 basic_block *body = get_loop_body_in_dom_order (loop);
317 unsigned b;
319 if ((unsigned) get_max_uid () >= max_insn_no)
321 /* Add some reserve for insns and registers produced in optimizations. */
322 max_insn_no = get_max_uid () + 100;
323 if (insn_info)
324 free (insn_info);
325 insn_info = xmalloc (max_insn_no * sizeof (struct insn_info));
328 if ((unsigned) max_reg_num () >= max_reg_no)
330 max_reg_no = max_reg_num () + 100;
331 if (last_def)
332 free (last_def);
333 last_def = xmalloc (max_reg_no * sizeof (rtx));
334 if (bivs)
335 free (bivs);
336 bivs = xmalloc (max_reg_no * sizeof (struct rtx_iv));
339 memset (last_def, 0, max_reg_num () * sizeof (rtx));
341 for (b = 0; b < loop->num_nodes; b++)
343 assign_luids (body[b]);
344 mark_sets (body[b], just_once_each_iteration_p (loop, body[b]));
347 free (body);
350 /* Gets definition of REG reaching the INSN. If REG is not simple, const0_rtx
351 is returned. If INSN is before the first def in the loop, NULL_RTX is
352 returned. */
355 iv_get_reaching_def (rtx insn, rtx reg)
357 unsigned regno, luid, auid;
358 rtx ainsn;
359 basic_block bb, abb;
361 if (GET_CODE (reg) == SUBREG)
363 if (!subreg_lowpart_p (reg))
364 return const0_rtx;
365 reg = SUBREG_REG (reg);
367 if (!REG_P (reg))
368 return NULL_RTX;
370 regno = REGNO (reg);
371 if (!last_def[regno]
372 || last_def[regno] == const0_rtx)
373 return last_def[regno];
375 bb = BLOCK_FOR_INSN (insn);
376 luid = insn_info[INSN_UID (insn)].luid;
378 ainsn = last_def[regno];
379 while (1)
381 abb = BLOCK_FOR_INSN (ainsn);
383 if (dominated_by_p (CDI_DOMINATORS, bb, abb))
384 break;
386 auid = INSN_UID (ainsn);
387 ainsn = insn_info[auid].prev_def;
389 if (!ainsn)
390 return NULL_RTX;
393 while (1)
395 abb = BLOCK_FOR_INSN (ainsn);
396 if (abb != bb)
397 return ainsn;
399 auid = INSN_UID (ainsn);
400 if (luid > insn_info[auid].luid)
401 return ainsn;
403 ainsn = insn_info[auid].prev_def;
404 if (!ainsn)
405 return NULL_RTX;
409 /* Sets IV to invariant CST in MODE. Always returns true (just for
410 consistency with other iv manipulation functions that may fail). */
412 static bool
413 iv_constant (struct rtx_iv *iv, rtx cst, enum machine_mode mode)
415 if (mode == VOIDmode)
416 mode = GET_MODE (cst);
418 iv->analysed = true;
419 iv->mode = mode;
420 iv->base = cst;
421 iv->step = const0_rtx;
422 iv->first_special = false;
423 iv->extend = NIL;
424 iv->extend_mode = iv->mode;
425 iv->delta = const0_rtx;
426 iv->mult = const1_rtx;
428 return true;
431 /* Evaluates application of subreg to MODE on IV. */
433 static bool
434 iv_subreg (struct rtx_iv *iv, enum machine_mode mode)
436 if (iv->extend_mode == mode)
437 return true;
439 if (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (iv->mode))
440 return false;
442 iv->extend = NIL;
443 iv->mode = mode;
445 iv->base = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
446 simplify_gen_binary (MULT, iv->extend_mode,
447 iv->base, iv->mult));
448 iv->step = simplify_gen_binary (MULT, iv->extend_mode, iv->step, iv->mult);
449 iv->mult = const1_rtx;
450 iv->delta = const0_rtx;
451 iv->first_special = false;
453 return true;
456 /* Evaluates application of EXTEND to MODE on IV. */
458 static bool
459 iv_extend (struct rtx_iv *iv, enum rtx_code extend, enum machine_mode mode)
461 if (mode != iv->extend_mode)
462 return false;
464 if (iv->extend != NIL
465 && iv->extend != extend)
466 return false;
468 iv->extend = extend;
470 return true;
473 /* Evaluates negation of IV. */
475 static bool
476 iv_neg (struct rtx_iv *iv)
478 if (iv->extend == NIL)
480 iv->base = simplify_gen_unary (NEG, iv->extend_mode,
481 iv->base, iv->extend_mode);
482 iv->step = simplify_gen_unary (NEG, iv->extend_mode,
483 iv->step, iv->extend_mode);
485 else
487 iv->delta = simplify_gen_unary (NEG, iv->extend_mode,
488 iv->delta, iv->extend_mode);
489 iv->mult = simplify_gen_unary (NEG, iv->extend_mode,
490 iv->mult, iv->extend_mode);
493 return true;
496 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
498 static bool
499 iv_add (struct rtx_iv *iv0, struct rtx_iv *iv1, enum rtx_code op)
501 enum machine_mode mode;
502 rtx arg;
504 /* Extend the constant to extend_mode of the other operand if neccesary. */
505 if (iv0->extend == NIL
506 && iv0->mode == iv0->extend_mode
507 && iv0->step == const0_rtx
508 && GET_MODE_SIZE (iv0->extend_mode) < GET_MODE_SIZE (iv1->extend_mode))
510 iv0->extend_mode = iv1->extend_mode;
511 iv0->base = simplify_gen_unary (ZERO_EXTEND, iv0->extend_mode,
512 iv0->base, iv0->mode);
514 if (iv1->extend == NIL
515 && iv1->mode == iv1->extend_mode
516 && iv1->step == const0_rtx
517 && GET_MODE_SIZE (iv1->extend_mode) < GET_MODE_SIZE (iv0->extend_mode))
519 iv1->extend_mode = iv0->extend_mode;
520 iv1->base = simplify_gen_unary (ZERO_EXTEND, iv1->extend_mode,
521 iv1->base, iv1->mode);
524 mode = iv0->extend_mode;
525 if (mode != iv1->extend_mode)
526 return false;
528 if (iv0->extend == NIL && iv1->extend == NIL)
530 if (iv0->mode != iv1->mode)
531 return false;
533 iv0->base = simplify_gen_binary (op, mode, iv0->base, iv1->base);
534 iv0->step = simplify_gen_binary (op, mode, iv0->step, iv1->step);
536 return true;
539 /* Handle addition of constant. */
540 if (iv1->extend == NIL
541 && iv1->mode == mode
542 && iv1->step == const0_rtx)
544 iv0->delta = simplify_gen_binary (op, mode, iv0->delta, iv1->base);
545 return true;
548 if (iv0->extend == NIL
549 && iv0->mode == mode
550 && iv0->step == const0_rtx)
552 arg = iv0->base;
553 *iv0 = *iv1;
554 if (op == MINUS
555 && !iv_neg (iv0))
556 return false;
558 iv0->delta = simplify_gen_binary (PLUS, mode, iv0->delta, arg);
559 return true;
562 return false;
565 /* Evaluates multiplication of IV by constant CST. */
567 static bool
568 iv_mult (struct rtx_iv *iv, rtx mby)
570 enum machine_mode mode = iv->extend_mode;
572 if (GET_MODE (mby) != VOIDmode
573 && GET_MODE (mby) != mode)
574 return false;
576 if (iv->extend == NIL)
578 iv->base = simplify_gen_binary (MULT, mode, iv->base, mby);
579 iv->step = simplify_gen_binary (MULT, mode, iv->step, mby);
581 else
583 iv->delta = simplify_gen_binary (MULT, mode, iv->delta, mby);
584 iv->mult = simplify_gen_binary (MULT, mode, iv->mult, mby);
587 return true;
590 /* The recursive part of get_biv_step. Gets the value of the single value
591 defined in INSN wrto initial value of REG inside loop, in shape described
592 at get_biv_step. */
594 static bool
595 get_biv_step_1 (rtx insn, rtx reg,
596 rtx *inner_step, enum machine_mode *inner_mode,
597 enum rtx_code *extend, enum machine_mode outer_mode,
598 rtx *outer_step)
600 rtx set, lhs, rhs, op0 = NULL_RTX, op1 = NULL_RTX;
601 rtx next, nextr, def_insn, tmp;
602 enum rtx_code code;
604 set = single_set (insn);
605 rhs = find_reg_equal_equiv_note (insn);
606 if (!rhs)
607 rhs = SET_SRC (set);
608 lhs = SET_DEST (set);
610 code = GET_CODE (rhs);
611 switch (code)
613 case SUBREG:
614 case REG:
615 next = rhs;
616 break;
618 case PLUS:
619 case MINUS:
620 op0 = XEXP (rhs, 0);
621 op1 = XEXP (rhs, 1);
623 if (code == PLUS && CONSTANT_P (op0))
625 tmp = op0; op0 = op1; op1 = tmp;
628 if (!simple_reg_p (op0)
629 || !CONSTANT_P (op1))
630 return false;
632 if (GET_MODE (rhs) != outer_mode)
634 /* ppc64 uses expressions like
636 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
638 this is equivalent to
640 (set x':DI (plus:DI y:DI 1))
641 (set x:SI (subreg:SI (x':DI)). */
642 if (GET_CODE (op0) != SUBREG)
643 return false;
644 if (GET_MODE (SUBREG_REG (op0)) != outer_mode)
645 return false;
648 next = op0;
649 break;
651 case SIGN_EXTEND:
652 case ZERO_EXTEND:
653 if (GET_MODE (rhs) != outer_mode)
654 return false;
656 op0 = XEXP (rhs, 0);
657 if (!simple_reg_p (op0))
658 return false;
660 next = op0;
661 break;
663 default:
664 return false;
667 if (GET_CODE (next) == SUBREG)
669 if (!subreg_lowpart_p (next))
670 return false;
672 nextr = SUBREG_REG (next);
673 if (GET_MODE (nextr) != outer_mode)
674 return false;
676 else
677 nextr = next;
679 def_insn = iv_get_reaching_def (insn, nextr);
680 if (def_insn == const0_rtx)
681 return false;
683 if (!def_insn)
685 if (!rtx_equal_p (nextr, reg))
686 return false;
688 *inner_step = const0_rtx;
689 *extend = NIL;
690 *inner_mode = outer_mode;
691 *outer_step = const0_rtx;
693 else if (!get_biv_step_1 (def_insn, reg,
694 inner_step, inner_mode, extend, outer_mode,
695 outer_step))
696 return false;
698 if (GET_CODE (next) == SUBREG)
700 enum machine_mode amode = GET_MODE (next);
702 if (GET_MODE_SIZE (amode) > GET_MODE_SIZE (*inner_mode))
703 return false;
705 *inner_mode = amode;
706 *inner_step = simplify_gen_binary (PLUS, outer_mode,
707 *inner_step, *outer_step);
708 *outer_step = const0_rtx;
709 *extend = NIL;
712 switch (code)
714 case REG:
715 case SUBREG:
716 break;
718 case PLUS:
719 case MINUS:
720 if (*inner_mode == outer_mode
721 /* See comment in previous switch. */
722 || GET_MODE (rhs) != outer_mode)
723 *inner_step = simplify_gen_binary (code, outer_mode,
724 *inner_step, op1);
725 else
726 *outer_step = simplify_gen_binary (code, outer_mode,
727 *outer_step, op1);
728 break;
730 case SIGN_EXTEND:
731 case ZERO_EXTEND:
732 if (GET_MODE (op0) != *inner_mode
733 || *extend != NIL
734 || *outer_step != const0_rtx)
735 abort ();
737 *extend = code;
738 break;
740 default:
741 abort ();
744 return true;
747 /* Gets the operation on register REG inside loop, in shape
749 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
751 If the operation cannot be described in this shape, return false. */
753 static bool
754 get_biv_step (rtx reg, rtx *inner_step, enum machine_mode *inner_mode,
755 enum rtx_code *extend, enum machine_mode *outer_mode,
756 rtx *outer_step)
758 *outer_mode = GET_MODE (reg);
760 if (!get_biv_step_1 (last_def[REGNO (reg)], reg,
761 inner_step, inner_mode, extend, *outer_mode,
762 outer_step))
763 return false;
765 if (*inner_mode != *outer_mode
766 && *extend == NIL)
767 abort ();
769 if (*inner_mode == *outer_mode
770 && *extend != NIL)
771 abort ();
773 if (*inner_mode == *outer_mode
774 && *outer_step != const0_rtx)
775 abort ();
777 return true;
780 /* Determines whether DEF is a biv and if so, stores its description
781 to *IV. */
783 static bool
784 iv_analyse_biv (rtx def, struct rtx_iv *iv)
786 unsigned regno;
787 rtx inner_step, outer_step;
788 enum machine_mode inner_mode, outer_mode;
789 enum rtx_code extend;
791 if (rtl_dump_file)
793 fprintf (rtl_dump_file, "Analysing ");
794 print_rtl (rtl_dump_file, def);
795 fprintf (rtl_dump_file, " for bivness.\n");
798 if (!REG_P (def))
800 if (!CONSTANT_P (def))
801 return false;
803 return iv_constant (iv, def, VOIDmode);
806 regno = REGNO (def);
807 if (last_def[regno] == const0_rtx)
809 if (rtl_dump_file)
810 fprintf (rtl_dump_file, " not simple.\n");
811 return false;
814 if (last_def[regno] && bivs[regno].analysed)
816 if (rtl_dump_file)
817 fprintf (rtl_dump_file, " already analysed.\n");
819 *iv = bivs[regno];
820 return iv->base != NULL_RTX;
823 if (!last_def[regno])
825 iv_constant (iv, def, VOIDmode);
826 goto end;
829 iv->analysed = true;
830 if (!get_biv_step (def, &inner_step, &inner_mode, &extend,
831 &outer_mode, &outer_step))
833 iv->base = NULL_RTX;
834 goto end;
837 /* Loop transforms base to es (base + inner_step) + outer_step,
838 where es means extend of subreg between inner_mode and outer_mode.
839 The corresponding induction variable is
841 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
843 iv->base = simplify_gen_binary (MINUS, outer_mode, def, outer_step);
844 iv->step = simplify_gen_binary (PLUS, outer_mode, inner_step, outer_step);
845 iv->mode = inner_mode;
846 iv->extend_mode = outer_mode;
847 iv->extend = extend;
848 iv->mult = const1_rtx;
849 iv->delta = outer_step;
850 iv->first_special = inner_mode != outer_mode;
852 end:
853 if (rtl_dump_file)
855 fprintf (rtl_dump_file, " ");
856 dump_iv_info (rtl_dump_file, iv);
857 fprintf (rtl_dump_file, "\n");
860 bivs[regno] = *iv;
862 return iv->base != NULL_RTX;
865 /* Analyses operand OP of INSN and stores the result to *IV. */
867 static bool
868 iv_analyse_op (rtx insn, rtx op, struct rtx_iv *iv)
870 rtx def_insn;
871 unsigned regno;
872 bool inv = CONSTANT_P (op);
874 if (rtl_dump_file)
876 fprintf (rtl_dump_file, "Analysing operand ");
877 print_rtl (rtl_dump_file, op);
878 fprintf (rtl_dump_file, " of insn ");
879 print_rtl_single (rtl_dump_file, insn);
882 if (GET_CODE (op) == SUBREG)
884 if (!subreg_lowpart_p (op))
885 return false;
887 if (!iv_analyse_op (insn, SUBREG_REG (op), iv))
888 return false;
890 return iv_subreg (iv, GET_MODE (op));
893 if (!inv)
895 regno = REGNO (op);
896 if (!last_def[regno])
897 inv = true;
898 else if (last_def[regno] == const0_rtx)
900 if (rtl_dump_file)
901 fprintf (rtl_dump_file, " not simple.\n");
902 return false;
906 if (inv)
908 iv_constant (iv, op, VOIDmode);
910 if (rtl_dump_file)
912 fprintf (rtl_dump_file, " ");
913 dump_iv_info (rtl_dump_file, iv);
914 fprintf (rtl_dump_file, "\n");
916 return true;
919 def_insn = iv_get_reaching_def (insn, op);
920 if (def_insn == const0_rtx)
922 if (rtl_dump_file)
923 fprintf (rtl_dump_file, " not simple.\n");
924 return false;
927 return iv_analyse (def_insn, op, iv);
930 /* Analyses iv DEF defined in INSN and stores the result to *IV. */
932 bool
933 iv_analyse (rtx insn, rtx def, struct rtx_iv *iv)
935 unsigned uid;
936 rtx set, rhs, mby = NULL_RTX, tmp;
937 rtx op0 = NULL_RTX, op1 = NULL_RTX;
938 struct rtx_iv iv0, iv1;
939 enum machine_mode amode;
940 enum rtx_code code;
942 if (insn == const0_rtx)
943 return false;
945 if (GET_CODE (def) == SUBREG)
947 if (!subreg_lowpart_p (def))
948 return false;
950 if (!iv_analyse (insn, SUBREG_REG (def), iv))
951 return false;
953 return iv_subreg (iv, GET_MODE (def));
956 if (!insn)
957 return iv_analyse_biv (def, iv);
959 if (rtl_dump_file)
961 fprintf (rtl_dump_file, "Analysing def of ");
962 print_rtl (rtl_dump_file, def);
963 fprintf (rtl_dump_file, " in insn ");
964 print_rtl_single (rtl_dump_file, insn);
967 uid = INSN_UID (insn);
968 if (insn_info[uid].iv.analysed)
970 if (rtl_dump_file)
971 fprintf (rtl_dump_file, " already analysed.\n");
972 *iv = insn_info[uid].iv;
973 return iv->base != NULL_RTX;
976 iv->mode = VOIDmode;
977 iv->base = NULL_RTX;
978 iv->step = NULL_RTX;
980 set = single_set (insn);
981 rhs = find_reg_equal_equiv_note (insn);
982 if (!rhs)
983 rhs = SET_SRC (set);
984 code = GET_CODE (rhs);
986 if (CONSTANT_P (rhs))
988 op0 = rhs;
989 amode = GET_MODE (def);
991 else
993 switch (code)
995 case SUBREG:
996 if (!subreg_lowpart_p (rhs))
997 goto end;
998 op0 = rhs;
999 break;
1001 case REG:
1002 op0 = rhs;
1003 break;
1005 case SIGN_EXTEND:
1006 case ZERO_EXTEND:
1007 case NEG:
1008 op0 = XEXP (rhs, 0);
1009 break;
1011 case PLUS:
1012 case MINUS:
1013 op0 = XEXP (rhs, 0);
1014 op1 = XEXP (rhs, 1);
1015 break;
1017 case MULT:
1018 op0 = XEXP (rhs, 0);
1019 mby = XEXP (rhs, 1);
1020 if (!CONSTANT_P (mby))
1022 if (!CONSTANT_P (op0))
1023 abort ();
1024 tmp = op0;
1025 op0 = mby;
1026 mby = tmp;
1028 break;
1030 default:
1031 abort ();
1034 amode = GET_MODE (rhs);
1037 if (op0)
1039 if (!iv_analyse_op (insn, op0, &iv0))
1040 goto end;
1042 if (iv0.mode == VOIDmode)
1044 iv0.mode = amode;
1045 iv0.extend_mode = amode;
1049 if (op1)
1051 if (!iv_analyse_op (insn, op1, &iv1))
1052 goto end;
1054 if (iv1.mode == VOIDmode)
1056 iv1.mode = amode;
1057 iv1.extend_mode = amode;
1061 switch (code)
1063 case SIGN_EXTEND:
1064 case ZERO_EXTEND:
1065 if (!iv_extend (&iv0, code, amode))
1066 goto end;
1067 break;
1069 case NEG:
1070 if (!iv_neg (&iv0))
1071 goto end;
1072 break;
1074 case PLUS:
1075 case MINUS:
1076 if (!iv_add (&iv0, &iv1, code))
1077 goto end;
1078 break;
1080 case MULT:
1081 if (!iv_mult (&iv0, mby))
1082 goto end;
1083 break;
1085 default:
1086 break;
1089 *iv = iv0;
1091 end:
1092 iv->analysed = true;
1093 insn_info[uid].iv = *iv;
1095 if (rtl_dump_file)
1097 print_rtl (rtl_dump_file, def);
1098 fprintf (rtl_dump_file, " in insn ");
1099 print_rtl_single (rtl_dump_file, insn);
1100 fprintf (rtl_dump_file, " is ");
1101 dump_iv_info (rtl_dump_file, iv);
1102 fprintf (rtl_dump_file, "\n");
1105 return iv->base != NULL_RTX;
1108 /* Calculates value of IV at ITERATION-th iteration. */
1111 get_iv_value (struct rtx_iv *iv, rtx iteration)
1113 rtx val;
1115 /* We would need to generate some if_then_else patterns, and so far
1116 it is not needed anywhere. */
1117 if (iv->first_special)
1118 abort ();
1120 if (iv->step != const0_rtx && iteration != const0_rtx)
1121 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->base,
1122 simplify_gen_binary (MULT, iv->extend_mode,
1123 iv->step, iteration));
1124 else
1125 val = iv->base;
1127 if (iv->extend_mode == iv->mode)
1128 return val;
1130 val = lowpart_subreg (iv->mode, val, iv->extend_mode);
1132 if (iv->extend == NIL)
1133 return val;
1135 val = simplify_gen_unary (iv->extend, iv->extend_mode, val, iv->mode);
1136 val = simplify_gen_binary (PLUS, iv->extend_mode, iv->delta,
1137 simplify_gen_binary (MULT, iv->extend_mode,
1138 iv->mult, val));
1140 return val;
1143 /* Free the data for an induction variable analysis. */
1145 void
1146 iv_analysis_done (void)
1148 max_insn_no = 0;
1149 max_reg_no = 0;
1150 if (insn_info)
1152 free (insn_info);
1153 insn_info = NULL;
1155 if (last_def)
1157 free (last_def);
1158 last_def = NULL;
1160 if (bivs)
1162 free (bivs);
1163 bivs = NULL;
1167 /* Computes inverse to X modulo (1 << MOD). */
1169 static unsigned HOST_WIDEST_INT
1170 inverse (unsigned HOST_WIDEST_INT x, int mod)
1172 unsigned HOST_WIDEST_INT mask =
1173 ((unsigned HOST_WIDEST_INT) 1 << (mod - 1) << 1) - 1;
1174 unsigned HOST_WIDEST_INT rslt = 1;
1175 int i;
1177 for (i = 0; i < mod - 1; i++)
1179 rslt = (rslt * x) & mask;
1180 x = (x * x) & mask;
1183 return rslt;
1186 /* Tries to estimate the maximum number of iterations. */
1188 static unsigned HOST_WIDEST_INT
1189 determine_max_iter (struct niter_desc *desc)
1191 rtx niter = desc->niter_expr;
1192 rtx mmin, mmax, left, right;
1193 unsigned HOST_WIDEST_INT nmax, inc;
1195 if (GET_CODE (niter) == AND
1196 && GET_CODE (XEXP (niter, 0)) == CONST_INT)
1198 nmax = INTVAL (XEXP (niter, 0));
1199 if (!(nmax & (nmax + 1)))
1201 desc->niter_max = nmax;
1202 return nmax;
1206 get_mode_bounds (desc->mode, desc->signed_p, &mmin, &mmax);
1207 nmax = INTVAL (mmax) - INTVAL (mmin);
1209 if (GET_CODE (niter) == UDIV)
1211 if (GET_CODE (XEXP (niter, 1)) != CONST_INT)
1213 desc->niter_max = nmax;
1214 return nmax;
1216 inc = INTVAL (XEXP (niter, 1));
1217 niter = XEXP (niter, 0);
1219 else
1220 inc = 1;
1222 if (GET_CODE (niter) == PLUS)
1224 left = XEXP (niter, 0);
1225 right = XEXP (niter, 0);
1227 if (GET_CODE (right) == CONST_INT)
1228 right = GEN_INT (-INTVAL (right));
1230 else if (GET_CODE (niter) == MINUS)
1232 left = XEXP (niter, 0);
1233 right = XEXP (niter, 0);
1235 else
1237 left = niter;
1238 right = mmin;
1241 if (GET_CODE (left) == CONST_INT)
1242 mmax = left;
1243 if (GET_CODE (right) == CONST_INT)
1244 mmin = right;
1245 nmax = INTVAL (mmax) - INTVAL (mmin);
1247 desc->niter_max = nmax / inc;
1248 return nmax / inc;
1251 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1253 static int
1254 altered_reg_used (rtx *reg, void *alt)
1256 if (!REG_P (*reg))
1257 return 0;
1259 return REGNO_REG_SET_P (alt, REGNO (*reg));
1262 /* Marks registers altered by EXPR in set ALT. */
1264 static void
1265 mark_altered (rtx expr, rtx by ATTRIBUTE_UNUSED, void *alt)
1267 if (GET_CODE (expr) == SUBREG)
1268 expr = SUBREG_REG (expr);
1269 if (!REG_P (expr))
1270 return;
1272 SET_REGNO_REG_SET (alt, REGNO (expr));
1275 /* Checks whether RHS is simple enough to process. */
1277 static bool
1278 simple_rhs_p (rtx rhs)
1280 rtx op0, op1;
1282 if (CONSTANT_P (rhs)
1283 || REG_P (rhs))
1284 return true;
1286 switch (GET_CODE (rhs))
1288 case PLUS:
1289 case MINUS:
1290 op0 = XEXP (rhs, 0);
1291 op1 = XEXP (rhs, 1);
1292 /* Allow reg + const sets only. */
1293 if (REG_P (op0) && CONSTANT_P (op1))
1294 return true;
1295 if (REG_P (op1) && CONSTANT_P (op0))
1296 return true;
1298 return false;
1300 default:
1301 return false;
1305 /* Simplifies *EXPR using assignment in INSN. ALTERED is the set of registers
1306 altered so far. */
1308 static void
1309 simplify_using_assignment (rtx insn, rtx *expr, regset altered)
1311 rtx set = single_set (insn);
1312 rtx lhs, rhs;
1313 bool ret = false;
1315 if (set)
1317 lhs = SET_DEST (set);
1318 if (GET_CODE (lhs) != REG
1319 || altered_reg_used (&lhs, altered))
1320 ret = true;
1322 else
1323 ret = true;
1325 note_stores (PATTERN (insn), mark_altered, altered);
1326 if (GET_CODE (insn) == CALL_INSN)
1328 int i;
1330 /* Kill all call clobbered registers. */
1331 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1332 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1333 SET_REGNO_REG_SET (altered, i);
1336 if (ret)
1337 return;
1339 rhs = find_reg_equal_equiv_note (insn);
1340 if (!rhs)
1341 rhs = SET_SRC (set);
1343 if (!simple_rhs_p (rhs))
1344 return;
1346 if (for_each_rtx (&rhs, altered_reg_used, altered))
1347 return;
1349 *expr = simplify_replace_rtx (*expr, lhs, rhs);
1352 /* Checks whether A implies B. */
1354 static bool
1355 implies_p (rtx a, rtx b)
1357 rtx op0, op1, r;
1359 if (GET_CODE (a) == EQ)
1361 op0 = XEXP (a, 0);
1362 op1 = XEXP (a, 1);
1364 if (REG_P (op0))
1366 r = simplify_replace_rtx (b, op0, op1);
1367 if (r == const_true_rtx)
1368 return true;
1371 if (REG_P (op1))
1373 r = simplify_replace_rtx (b, op1, op0);
1374 if (r == const_true_rtx)
1375 return true;
1379 return false;
1382 /* Canonicalizes COND so that
1384 (1) Ensure that operands are ordered according to
1385 swap_commutative_operands_p.
1386 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1387 for GE, GEU, and LEU. */
1390 canon_condition (rtx cond)
1392 rtx tem;
1393 rtx op0, op1;
1394 enum rtx_code code;
1395 enum machine_mode mode;
1397 code = GET_CODE (cond);
1398 op0 = XEXP (cond, 0);
1399 op1 = XEXP (cond, 1);
1401 if (swap_commutative_operands_p (op0, op1))
1403 code = swap_condition (code);
1404 tem = op0;
1405 op0 = op1;
1406 op1 = tem;
1409 mode = GET_MODE (op0);
1410 if (mode == VOIDmode)
1411 mode = GET_MODE (op1);
1412 if (mode == VOIDmode)
1413 abort ();
1415 if (GET_CODE (op1) == CONST_INT
1416 && GET_MODE_CLASS (mode) != MODE_CC
1417 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
1419 HOST_WIDE_INT const_val = INTVAL (op1);
1420 unsigned HOST_WIDE_INT uconst_val = const_val;
1421 unsigned HOST_WIDE_INT max_val
1422 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (mode);
1424 switch (code)
1426 case LE:
1427 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
1428 code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
1429 break;
1431 /* When cross-compiling, const_val might be sign-extended from
1432 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1433 case GE:
1434 if ((HOST_WIDE_INT) (const_val & max_val)
1435 != (((HOST_WIDE_INT) 1
1436 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
1437 code = GT, op1 = gen_int_mode (const_val - 1, mode);
1438 break;
1440 case LEU:
1441 if (uconst_val < max_val)
1442 code = LTU, op1 = gen_int_mode (uconst_val + 1, mode);
1443 break;
1445 case GEU:
1446 if (uconst_val != 0)
1447 code = GTU, op1 = gen_int_mode (uconst_val - 1, mode);
1448 break;
1450 default:
1451 break;
1455 if (op0 != XEXP (cond, 0)
1456 || op1 != XEXP (cond, 1)
1457 || code != GET_CODE (cond)
1458 || GET_MODE (cond) != SImode)
1459 cond = gen_rtx_fmt_ee (code, SImode, op0, op1);
1461 return cond;
1464 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1465 set of altered regs. */
1467 void
1468 simplify_using_condition (rtx cond, rtx *expr, regset altered)
1470 rtx rev, reve, exp = *expr;
1472 if (GET_RTX_CLASS (GET_CODE (*expr)) != '<')
1473 return;
1475 /* If some register gets altered later, we do not really speak about its
1476 value at the time of comparison. */
1477 if (altered
1478 && for_each_rtx (&cond, altered_reg_used, altered))
1479 return;
1481 rev = reversed_condition (cond);
1482 reve = reversed_condition (exp);
1484 cond = canon_condition (cond);
1485 exp = canon_condition (exp);
1486 if (rev)
1487 rev = canon_condition (rev);
1488 if (reve)
1489 reve = canon_condition (reve);
1491 if (rtx_equal_p (exp, cond))
1493 *expr = const_true_rtx;
1494 return;
1498 if (rev && rtx_equal_p (exp, rev))
1500 *expr = const0_rtx;
1501 return;
1504 if (implies_p (cond, exp))
1506 *expr = const_true_rtx;
1507 return;
1510 if (reve && implies_p (cond, reve))
1512 *expr = const0_rtx;
1513 return;
1516 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1517 be false. */
1518 if (rev && implies_p (exp, rev))
1520 *expr = const0_rtx;
1521 return;
1524 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1525 if (rev && reve && implies_p (reve, rev))
1527 *expr = const_true_rtx;
1528 return;
1531 /* We would like to have some other tests here. TODO. */
1533 return;
1536 /* Use relationship between A and *B to eventually eliminate *B.
1537 OP is the operation we consider. */
1539 static void
1540 eliminate_implied_condition (enum rtx_code op, rtx a, rtx *b)
1542 if (op == AND)
1544 /* If A implies *B, we may replace *B by true. */
1545 if (implies_p (a, *b))
1546 *b = const_true_rtx;
1548 else if (op == IOR)
1550 /* If *B implies A, we may replace *B by false. */
1551 if (implies_p (*b, a))
1552 *b = const0_rtx;
1554 else
1555 abort ();
1558 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1559 operation we consider. */
1561 static void
1562 eliminate_implied_conditions (enum rtx_code op, rtx *head, rtx tail)
1564 rtx elt;
1566 for (elt = tail; elt; elt = XEXP (elt, 1))
1567 eliminate_implied_condition (op, *head, &XEXP (elt, 0));
1568 for (elt = tail; elt; elt = XEXP (elt, 1))
1569 eliminate_implied_condition (op, XEXP (elt, 0), head);
1572 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1573 is a list, its elements are assumed to be combined using OP. */
1575 static void
1576 simplify_using_initial_values (struct loop *loop, enum rtx_code op, rtx *expr)
1578 rtx head, tail, insn;
1579 rtx neutral, aggr;
1580 regset altered;
1581 regset_head altered_head;
1582 edge e;
1584 if (!*expr)
1585 return;
1587 if (CONSTANT_P (*expr))
1588 return;
1590 if (GET_CODE (*expr) == EXPR_LIST)
1592 head = XEXP (*expr, 0);
1593 tail = XEXP (*expr, 1);
1595 eliminate_implied_conditions (op, &head, tail);
1597 if (op == AND)
1599 neutral = const_true_rtx;
1600 aggr = const0_rtx;
1602 else if (op == IOR)
1604 neutral = const0_rtx;
1605 aggr = const_true_rtx;
1607 else
1608 abort ();
1610 simplify_using_initial_values (loop, NIL, &head);
1611 if (head == aggr)
1613 XEXP (*expr, 0) = aggr;
1614 XEXP (*expr, 1) = NULL_RTX;
1615 return;
1617 else if (head == neutral)
1619 *expr = tail;
1620 simplify_using_initial_values (loop, op, expr);
1621 return;
1623 simplify_using_initial_values (loop, op, &tail);
1625 if (tail && XEXP (tail, 0) == aggr)
1627 *expr = tail;
1628 return;
1631 XEXP (*expr, 0) = head;
1632 XEXP (*expr, 1) = tail;
1633 return;
1636 if (op != NIL)
1637 abort ();
1639 e = loop_preheader_edge (loop);
1640 if (e->src == ENTRY_BLOCK_PTR)
1641 return;
1643 altered = INITIALIZE_REG_SET (altered_head);
1645 while (1)
1647 insn = BB_END (e->src);
1648 if (any_condjump_p (insn))
1650 /* FIXME -- slightly wrong -- what if compared register
1651 gets altered between start of the condition and insn? */
1652 rtx cond = get_condition (BB_END (e->src), NULL, false);
1654 if (cond && (e->flags & EDGE_FALLTHRU))
1655 cond = reversed_condition (cond);
1656 if (cond)
1658 simplify_using_condition (cond, expr, altered);
1659 if (CONSTANT_P (*expr))
1661 FREE_REG_SET (altered);
1662 return;
1667 FOR_BB_INSNS_REVERSE (e->src, insn)
1669 if (!INSN_P (insn))
1670 continue;
1672 simplify_using_assignment (insn, expr, altered);
1673 if (CONSTANT_P (*expr))
1675 FREE_REG_SET (altered);
1676 return;
1680 e = e->src->pred;
1681 if (e->pred_next
1682 || e->src == ENTRY_BLOCK_PTR)
1683 break;
1686 FREE_REG_SET (altered);
1689 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
1690 that IV occurs as left operands of comparison COND and its signedness
1691 is SIGNED_P to DESC. */
1693 static void
1694 shorten_into_mode (struct rtx_iv *iv, enum machine_mode mode,
1695 enum rtx_code cond, bool signed_p, struct niter_desc *desc)
1697 rtx mmin, mmax, cond_over, cond_under;
1699 get_mode_bounds (mode, signed_p, &mmin, &mmax);
1700 cond_under = simplify_gen_relational (LT, SImode, iv->extend_mode,
1701 iv->base, mmin);
1702 cond_over = simplify_gen_relational (GT, SImode, iv->extend_mode,
1703 iv->base, mmax);
1705 switch (cond)
1707 case LE:
1708 case LT:
1709 case LEU:
1710 case LTU:
1711 if (cond_under != const0_rtx)
1712 desc->infinite =
1713 alloc_EXPR_LIST (0, cond_under, desc->infinite);
1714 if (cond_over != const0_rtx)
1715 desc->noloop_assumptions =
1716 alloc_EXPR_LIST (0, cond_over, desc->noloop_assumptions);
1717 break;
1719 case GE:
1720 case GT:
1721 case GEU:
1722 case GTU:
1723 if (cond_over != const0_rtx)
1724 desc->infinite =
1725 alloc_EXPR_LIST (0, cond_over, desc->infinite);
1726 if (cond_under != const0_rtx)
1727 desc->noloop_assumptions =
1728 alloc_EXPR_LIST (0, cond_under, desc->noloop_assumptions);
1729 break;
1731 case NE:
1732 if (cond_over != const0_rtx)
1733 desc->infinite =
1734 alloc_EXPR_LIST (0, cond_over, desc->infinite);
1735 if (cond_under != const0_rtx)
1736 desc->infinite =
1737 alloc_EXPR_LIST (0, cond_under, desc->infinite);
1738 break;
1740 default:
1741 abort ();
1744 iv->mode = mode;
1745 iv->extend = signed_p ? SIGN_EXTEND : ZERO_EXTEND;
1748 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
1749 subregs of the same mode if possible (sometimes it is neccesary to add
1750 some assumptions to DESC). */
1752 static bool
1753 canonicalize_iv_subregs (struct rtx_iv *iv0, struct rtx_iv *iv1,
1754 enum rtx_code cond, struct niter_desc *desc)
1756 enum machine_mode comp_mode;
1757 bool signed_p;
1759 /* If the ivs behave specially in the first iteration, or are
1760 added/multiplied after extending, we ignore them. */
1761 if (iv0->first_special || iv0->mult != const1_rtx || iv0->delta != const0_rtx)
1762 return false;
1763 if (iv1->first_special || iv1->mult != const1_rtx || iv1->delta != const0_rtx)
1764 return false;
1766 /* If there is some extend, it must match signedness of the comparison. */
1767 switch (cond)
1769 case LE:
1770 case LT:
1771 if (iv0->extend == ZERO_EXTEND
1772 || iv1->extend == ZERO_EXTEND)
1773 return false;
1774 signed_p = true;
1775 break;
1777 case LEU:
1778 case LTU:
1779 if (iv0->extend == SIGN_EXTEND
1780 || iv1->extend == SIGN_EXTEND)
1781 return false;
1782 signed_p = false;
1783 break;
1785 case NE:
1786 if (iv0->extend != NIL
1787 && iv1->extend != NIL
1788 && iv0->extend != iv1->extend)
1789 return false;
1791 signed_p = false;
1792 if (iv0->extend != NIL)
1793 signed_p = iv0->extend == SIGN_EXTEND;
1794 if (iv1->extend != NIL)
1795 signed_p = iv1->extend == SIGN_EXTEND;
1796 break;
1798 default:
1799 abort ();
1802 /* Values of both variables should be computed in the same mode. These
1803 might indeed be different, if we have comparison like
1805 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
1807 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
1808 in different modes. This does not seem impossible to handle, but
1809 it hardly ever occurs in practice.
1811 The only exception is the case when one of operands is invariant.
1812 For example pentium 3 generates comparisons like
1813 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
1814 definitely do not want this prevent the optimization. */
1815 comp_mode = iv0->extend_mode;
1816 if (GET_MODE_BITSIZE (comp_mode) < GET_MODE_BITSIZE (iv1->extend_mode))
1817 comp_mode = iv1->extend_mode;
1819 if (iv0->extend_mode != comp_mode)
1821 if (iv0->mode != iv0->extend_mode
1822 || iv0->step != const0_rtx)
1823 return false;
1825 iv0->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
1826 comp_mode, iv0->base, iv0->mode);
1827 iv0->extend_mode = comp_mode;
1830 if (iv1->extend_mode != comp_mode)
1832 if (iv1->mode != iv1->extend_mode
1833 || iv1->step != const0_rtx)
1834 return false;
1836 iv1->base = simplify_gen_unary (signed_p ? SIGN_EXTEND : ZERO_EXTEND,
1837 comp_mode, iv1->base, iv1->mode);
1838 iv1->extend_mode = comp_mode;
1841 /* Check that both ivs belong to a range of a single mode. If one of the
1842 operands is an invariant, we may need to shorten it into the common
1843 mode. */
1844 if (iv0->mode == iv0->extend_mode
1845 && iv0->step == const0_rtx
1846 && iv0->mode != iv1->mode)
1847 shorten_into_mode (iv0, iv1->mode, cond, signed_p, desc);
1849 if (iv1->mode == iv1->extend_mode
1850 && iv1->step == const0_rtx
1851 && iv0->mode != iv1->mode)
1852 shorten_into_mode (iv1, iv0->mode, swap_condition (cond), signed_p, desc);
1854 if (iv0->mode != iv1->mode)
1855 return false;
1857 desc->mode = iv0->mode;
1858 desc->signed_p = signed_p;
1860 return true;
1863 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
1864 the result into DESC. Very similar to determine_number_of_iterations
1865 (basically its rtl version), complicated by things like subregs. */
1867 void
1868 iv_number_of_iterations (struct loop *loop, rtx insn, rtx condition,
1869 struct niter_desc *desc)
1871 rtx op0, op1, delta, step, bound, may_xform, def_insn, tmp, tmp0, tmp1;
1872 struct rtx_iv iv0, iv1, tmp_iv;
1873 rtx assumption;
1874 enum rtx_code cond;
1875 enum machine_mode mode, comp_mode;
1876 rtx mmin, mmax;
1877 unsigned HOST_WIDEST_INT s, size, d;
1878 HOST_WIDEST_INT up, down, inc;
1879 int was_sharp = false;
1881 /* The meaning of these assumptions is this:
1882 if !assumptions
1883 then the rest of information does not have to be valid
1884 if noloop_assumptions then the loop does not roll
1885 if infinite then this exit is never used */
1887 desc->assumptions = NULL_RTX;
1888 desc->noloop_assumptions = NULL_RTX;
1889 desc->infinite = NULL_RTX;
1890 desc->simple_p = true;
1892 desc->const_iter = false;
1893 desc->niter_expr = NULL_RTX;
1894 desc->niter_max = 0;
1896 cond = GET_CODE (condition);
1897 if (GET_RTX_CLASS (cond) != '<')
1898 abort ();
1900 mode = GET_MODE (XEXP (condition, 0));
1901 if (mode == VOIDmode)
1902 mode = GET_MODE (XEXP (condition, 1));
1903 /* The constant comparisons should be folded. */
1904 if (mode == VOIDmode)
1905 abort ();
1907 /* We only handle integers or pointers. */
1908 if (GET_MODE_CLASS (mode) != MODE_INT
1909 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
1910 goto fail;
1912 op0 = XEXP (condition, 0);
1913 def_insn = iv_get_reaching_def (insn, op0);
1914 if (!iv_analyse (def_insn, op0, &iv0))
1915 goto fail;
1916 if (iv0.extend_mode == VOIDmode)
1917 iv0.mode = iv0.extend_mode = mode;
1919 op1 = XEXP (condition, 1);
1920 def_insn = iv_get_reaching_def (insn, op1);
1921 if (!iv_analyse (def_insn, op1, &iv1))
1922 goto fail;
1923 if (iv1.extend_mode == VOIDmode)
1924 iv1.mode = iv1.extend_mode = mode;
1926 if (GET_MODE_BITSIZE (iv0.extend_mode) > HOST_BITS_PER_WIDE_INT
1927 || GET_MODE_BITSIZE (iv1.extend_mode) > HOST_BITS_PER_WIDE_INT)
1928 goto fail;
1930 /* Check condition and normalize it. */
1932 switch (cond)
1934 case GE:
1935 case GT:
1936 case GEU:
1937 case GTU:
1938 tmp_iv = iv0; iv0 = iv1; iv1 = tmp_iv;
1939 cond = swap_condition (cond);
1940 break;
1941 case NE:
1942 case LE:
1943 case LEU:
1944 case LT:
1945 case LTU:
1946 break;
1947 default:
1948 goto fail;
1951 /* Handle extends. This is relatively nontrivial, so we only try in some
1952 easy cases, when we can canonicalize the ivs (possibly by adding some
1953 assumptions) to shape subreg (base + i * step). This function also fills
1954 in desc->mode and desc->signed_p. */
1956 if (!canonicalize_iv_subregs (&iv0, &iv1, cond, desc))
1957 goto fail;
1959 comp_mode = iv0.extend_mode;
1960 mode = iv0.mode;
1961 size = GET_MODE_BITSIZE (mode);
1962 get_mode_bounds (mode, (cond == LE || cond == LT), &mmin, &mmax);
1964 if (GET_CODE (iv0.step) != CONST_INT || GET_CODE (iv1.step) != CONST_INT)
1965 goto fail;
1967 /* We can take care of the case of two induction variables chasing each other
1968 if the test is NE. I have never seen a loop using it, but still it is
1969 cool. */
1970 if (iv0.step != const0_rtx && iv1.step != const0_rtx)
1972 if (cond != NE)
1973 goto fail;
1975 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
1976 iv1.step = const0_rtx;
1979 /* This is either infinite loop or the one that ends immediately, depending
1980 on initial values. Unswitching should remove this kind of conditions. */
1981 if (iv0.step == const0_rtx && iv1.step == const0_rtx)
1982 goto fail;
1984 /* Ignore loops of while (i-- < 10) type. */
1985 if (cond != NE
1986 && (INTVAL (iv0.step) < 0 || INTVAL (iv1.step) > 0))
1987 goto fail;
1989 /* Some more condition normalization. We must record some assumptions
1990 due to overflows. */
1991 switch (cond)
1993 case LT:
1994 case LTU:
1995 /* We want to take care only of non-sharp relationals; this is easy,
1996 as in cases the overflow would make the transformation unsafe
1997 the loop does not roll. Seemingly it would make more sense to want
1998 to take care of sharp relationals instead, as NE is more similar to
1999 them, but the problem is that here the transformation would be more
2000 difficult due to possibly infinite loops. */
2001 if (iv0.step == const0_rtx)
2003 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2004 assumption = simplify_gen_relational (EQ, SImode, mode, tmp, mmax);
2005 if (assumption == const_true_rtx)
2006 goto zero_iter;
2007 iv0.base = simplify_gen_binary (PLUS, comp_mode,
2008 iv0.base, const1_rtx);
2010 else
2012 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2013 assumption = simplify_gen_relational (EQ, SImode, mode, tmp, mmin);
2014 if (assumption == const_true_rtx)
2015 goto zero_iter;
2016 iv1.base = simplify_gen_binary (PLUS, comp_mode,
2017 iv1.base, constm1_rtx);
2020 if (assumption != const0_rtx)
2021 desc->noloop_assumptions =
2022 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2023 cond = (cond == LT) ? LE : LEU;
2025 /* It will be useful to be able to tell the difference once more in
2026 LE -> NE reduction. */
2027 was_sharp = true;
2028 break;
2029 default: ;
2032 /* Take care of trivially infinite loops. */
2033 if (cond != NE)
2035 if (iv0.step == const0_rtx)
2037 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2038 if (rtx_equal_p (tmp, mmin))
2040 desc->infinite =
2041 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2042 return;
2045 else
2047 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2048 if (rtx_equal_p (tmp, mmax))
2050 desc->infinite =
2051 alloc_EXPR_LIST (0, const_true_rtx, NULL_RTX);
2052 return;
2057 /* If we can we want to take care of NE conditions instead of size
2058 comparisons, as they are much more friendly (most importantly
2059 this takes care of special handling of loops with step 1). We can
2060 do it if we first check that upper bound is greater or equal to
2061 lower bound, their difference is constant c modulo step and that
2062 there is not an overflow. */
2063 if (cond != NE)
2065 if (iv0.step == const0_rtx)
2066 step = simplify_gen_unary (NEG, comp_mode, iv1.step, comp_mode);
2067 else
2068 step = iv0.step;
2069 delta = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2070 delta = lowpart_subreg (mode, delta, comp_mode);
2071 delta = simplify_gen_binary (UMOD, mode, delta, step);
2072 may_xform = const0_rtx;
2074 if (GET_CODE (delta) == CONST_INT)
2076 if (was_sharp && INTVAL (delta) == INTVAL (step) - 1)
2078 /* A special case. We have transformed condition of type
2079 for (i = 0; i < 4; i += 4)
2080 into
2081 for (i = 0; i <= 3; i += 4)
2082 obviously if the test for overflow during that transformation
2083 passed, we cannot overflow here. Most importantly any
2084 loop with sharp end condition and step 1 falls into this
2085 cathegory, so handling this case specially is definitely
2086 worth the troubles. */
2087 may_xform = const_true_rtx;
2089 else if (iv0.step == const0_rtx)
2091 bound = simplify_gen_binary (PLUS, comp_mode, mmin, step);
2092 bound = simplify_gen_binary (MINUS, comp_mode, bound, delta);
2093 bound = lowpart_subreg (mode, bound, comp_mode);
2094 tmp = lowpart_subreg (mode, iv0.base, comp_mode);
2095 may_xform = simplify_gen_relational (cond, SImode, mode,
2096 bound, tmp);
2098 else
2100 bound = simplify_gen_binary (MINUS, comp_mode, mmax, step);
2101 bound = simplify_gen_binary (PLUS, comp_mode, bound, delta);
2102 bound = lowpart_subreg (mode, bound, comp_mode);
2103 tmp = lowpart_subreg (mode, iv1.base, comp_mode);
2104 may_xform = simplify_gen_relational (cond, SImode, mode,
2105 tmp, bound);
2109 if (may_xform != const0_rtx)
2111 /* We perform the transformation always provided that it is not
2112 completely senseless. This is OK, as we would need this assumption
2113 to determine the number of iterations anyway. */
2114 if (may_xform != const_true_rtx)
2115 desc->assumptions = alloc_EXPR_LIST (0, may_xform,
2116 desc->assumptions);
2118 /* We are going to lose some information about upper bound on
2119 number of iterations in this step, so record the information
2120 here. */
2121 inc = INTVAL (iv0.step) - INTVAL (iv1.step);
2122 if (GET_CODE (iv1.base) == CONST_INT)
2123 up = INTVAL (iv1.base);
2124 else
2125 up = INTVAL (mmax) - inc;
2126 down = INTVAL (GET_CODE (iv0.base) == CONST_INT ? iv0.base : mmin);
2127 desc->niter_max = (up - down) / inc + 1;
2129 if (iv0.step == const0_rtx)
2131 iv0.base = simplify_gen_binary (PLUS, comp_mode, iv0.base, delta);
2132 iv0.base = simplify_gen_binary (MINUS, comp_mode, iv0.base, step);
2134 else
2136 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, delta);
2137 iv1.base = simplify_gen_binary (PLUS, comp_mode, iv1.base, step);
2140 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2141 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2142 assumption = simplify_gen_relational (reverse_condition (cond),
2143 SImode, mode, tmp0, tmp1);
2144 if (assumption == const_true_rtx)
2145 goto zero_iter;
2146 else if (assumption != const0_rtx)
2147 desc->noloop_assumptions =
2148 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2149 cond = NE;
2153 /* Count the number of iterations. */
2154 if (cond == NE)
2156 /* Everything we do here is just arithmetics modulo size of mode. This
2157 makes us able to do more involved computations of number of iterations
2158 than in other cases. First transform the condition into shape
2159 s * i <> c, with s positive. */
2160 iv1.base = simplify_gen_binary (MINUS, comp_mode, iv1.base, iv0.base);
2161 iv0.base = const0_rtx;
2162 iv0.step = simplify_gen_binary (MINUS, comp_mode, iv0.step, iv1.step);
2163 iv1.step = const0_rtx;
2164 if (INTVAL (iv0.step) < 0)
2166 iv0.step = simplify_gen_unary (NEG, comp_mode, iv0.step, mode);
2167 iv1.base = simplify_gen_unary (NEG, comp_mode, iv1.base, mode);
2169 iv0.step = lowpart_subreg (mode, iv0.step, comp_mode);
2171 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2172 is infinite. Otherwise, the number of iterations is
2173 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2174 s = INTVAL (iv0.step); d = 1;
2175 while (s % 2 != 1)
2177 s /= 2;
2178 d *= 2;
2179 size--;
2181 bound = GEN_INT (((unsigned HOST_WIDEST_INT) 1 << (size - 1 ) << 1) - 1);
2183 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2184 tmp = simplify_gen_binary (UMOD, mode, tmp1, GEN_INT (d));
2185 assumption = simplify_gen_relational (NE, SImode, mode, tmp, const0_rtx);
2186 desc->infinite = alloc_EXPR_LIST (0, assumption, desc->infinite);
2188 tmp = simplify_gen_binary (UDIV, mode, tmp1, GEN_INT (d));
2189 tmp = simplify_gen_binary (MULT, mode,
2190 tmp, GEN_INT (inverse (s, size)));
2191 desc->niter_expr = simplify_gen_binary (AND, mode, tmp, bound);
2193 else
2195 if (iv1.step == const0_rtx)
2196 /* Condition in shape a + s * i <= b
2197 We must know that b + s does not overflow and a <= b + s and then we
2198 can compute number of iterations as (b + s - a) / s. (It might
2199 seem that we in fact could be more clever about testing the b + s
2200 overflow condition using some information about b - a mod s,
2201 but it was already taken into account during LE -> NE transform). */
2203 step = iv0.step;
2204 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2205 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2207 bound = simplify_gen_binary (MINUS, mode, mmax, step);
2208 assumption = simplify_gen_relational (cond, SImode, mode,
2209 tmp1, bound);
2210 desc->assumptions =
2211 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2213 tmp = simplify_gen_binary (PLUS, comp_mode, iv1.base, iv0.step);
2214 tmp = lowpart_subreg (mode, tmp, comp_mode);
2215 assumption = simplify_gen_relational (reverse_condition (cond),
2216 SImode, mode, tmp0, tmp);
2218 delta = simplify_gen_binary (PLUS, mode, tmp1, step);
2219 delta = simplify_gen_binary (MINUS, mode, delta, tmp0);
2221 else
2223 /* Condition in shape a <= b - s * i
2224 We must know that a - s does not overflow and a - s <= b and then
2225 we can again compute number of iterations as (b - (a - s)) / s. */
2226 step = simplify_gen_unary (NEG, mode, iv1.step, mode);
2227 tmp0 = lowpart_subreg (mode, iv0.base, comp_mode);
2228 tmp1 = lowpart_subreg (mode, iv1.base, comp_mode);
2230 bound = simplify_gen_binary (MINUS, mode, mmin, step);
2231 assumption = simplify_gen_relational (cond, SImode, mode,
2232 bound, tmp0);
2233 desc->assumptions =
2234 alloc_EXPR_LIST (0, assumption, desc->assumptions);
2236 tmp = simplify_gen_binary (PLUS, comp_mode, iv0.base, iv1.step);
2237 tmp = lowpart_subreg (mode, tmp, comp_mode);
2238 assumption = simplify_gen_relational (reverse_condition (cond),
2239 SImode, mode,
2240 tmp, tmp1);
2241 delta = simplify_gen_binary (MINUS, mode, tmp0, step);
2242 delta = simplify_gen_binary (MINUS, mode, tmp1, delta);
2244 if (assumption == const_true_rtx)
2245 goto zero_iter;
2246 else if (assumption != const0_rtx)
2247 desc->noloop_assumptions =
2248 alloc_EXPR_LIST (0, assumption, desc->noloop_assumptions);
2249 delta = simplify_gen_binary (UDIV, mode, delta, step);
2250 desc->niter_expr = delta;
2253 simplify_using_initial_values (loop, AND, &desc->assumptions);
2254 if (desc->assumptions
2255 && XEXP (desc->assumptions, 0) == const0_rtx)
2256 goto fail;
2257 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2258 simplify_using_initial_values (loop, IOR, &desc->infinite);
2259 simplify_using_initial_values (loop, NIL, &desc->niter_expr);
2261 /* Rerun the simplification. Consider code (created by copying loop headers)
2263 i = 0;
2265 if (0 < n)
2269 i++;
2270 } while (i < n);
2273 The first pass determines that i = 0, the second pass uses it to eliminate
2274 noloop assumption. */
2276 simplify_using_initial_values (loop, AND, &desc->assumptions);
2277 if (desc->assumptions
2278 && XEXP (desc->assumptions, 0) == const0_rtx)
2279 goto fail;
2280 simplify_using_initial_values (loop, IOR, &desc->noloop_assumptions);
2281 simplify_using_initial_values (loop, IOR, &desc->infinite);
2282 simplify_using_initial_values (loop, NIL, &desc->niter_expr);
2284 if (GET_CODE (desc->niter_expr) == CONST_INT)
2286 unsigned HOST_WIDEST_INT val = INTVAL (desc->niter_expr);
2288 desc->const_iter = true;
2289 desc->niter_max = desc->niter = val & GET_MODE_MASK (desc->mode);
2291 else if (!desc->niter_max)
2292 desc->niter_max = determine_max_iter (desc);
2294 return;
2296 fail:
2297 desc->simple_p = false;
2298 return;
2300 zero_iter:
2301 desc->const_iter = true;
2302 desc->niter = 0;
2303 desc->niter_max = 0;
2304 desc->niter_expr = const0_rtx;
2305 return;
2308 /* Checks whether E is a simple exit from LOOP and stores its description
2309 into DESC. TODO Should replace cfgloopanal.c:simple_loop_exit_p. */
2311 static void
2312 check_simple_exit (struct loop *loop, edge e, struct niter_desc *desc)
2314 basic_block exit_bb;
2315 rtx condition, at;
2316 edge ei;
2318 exit_bb = e->src;
2319 desc->simple_p = false;
2321 /* It must belong directly to the loop. */
2322 if (exit_bb->loop_father != loop)
2323 return;
2325 /* It must be tested (at least) once during any iteration. */
2326 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit_bb))
2327 return;
2329 /* It must end in a simple conditional jump. */
2330 if (!any_condjump_p (BB_END (exit_bb)))
2331 return;
2333 ei = exit_bb->succ;
2334 if (ei == e)
2335 ei = ei->succ_next;
2337 desc->out_edge = e;
2338 desc->in_edge = ei;
2340 /* Test whether the condition is suitable. */
2341 if (!(condition = get_condition (BB_END (ei->src), &at, false)))
2342 return;
2344 if (ei->flags & EDGE_FALLTHRU)
2346 condition = reversed_condition (condition);
2347 if (!condition)
2348 return;
2351 /* Check that we are able to determine number of iterations and fill
2352 in information about it. */
2353 iv_number_of_iterations (loop, at, condition, desc);
2356 /* Finds a simple exit of LOOP and stores its description into DESC.
2357 TODO Should replace cfgloopanal.c:simple_loop_p. */
2359 void
2360 find_simple_exit (struct loop *loop, struct niter_desc *desc)
2362 unsigned i;
2363 basic_block *body;
2364 edge e;
2365 struct niter_desc act;
2366 bool any = false;
2368 desc->simple_p = false;
2369 body = get_loop_body (loop);
2371 for (i = 0; i < loop->num_nodes; i++)
2373 for (e = body[i]->succ; e; e = e->succ_next)
2375 if (flow_bb_inside_loop_p (loop, e->dest))
2376 continue;
2378 check_simple_exit (loop, e, &act);
2379 if (!act.simple_p)
2380 continue;
2382 /* Prefer constant iterations; the less the better. */
2383 if (!any)
2384 any = true;
2385 else if (!act.const_iter
2386 || (desc->const_iter && act.niter >= desc->niter))
2387 continue;
2388 *desc = act;
2392 if (rtl_dump_file)
2394 if (desc->simple_p)
2396 fprintf (rtl_dump_file, "Loop %d is simple:\n", loop->num);
2397 fprintf (rtl_dump_file, " simple exit %d -> %d\n",
2398 desc->out_edge->src->index,
2399 desc->out_edge->dest->index);
2400 if (desc->assumptions)
2402 fprintf (rtl_dump_file, " assumptions: ");
2403 print_rtl (rtl_dump_file, desc->assumptions);
2404 fprintf (rtl_dump_file, "\n");
2406 if (desc->noloop_assumptions)
2408 fprintf (rtl_dump_file, " does not roll if: ");
2409 print_rtl (rtl_dump_file, desc->noloop_assumptions);
2410 fprintf (rtl_dump_file, "\n");
2412 if (desc->infinite)
2414 fprintf (rtl_dump_file, " infinite if: ");
2415 print_rtl (rtl_dump_file, desc->infinite);
2416 fprintf (rtl_dump_file, "\n");
2419 fprintf (rtl_dump_file, " number of iterations: ");
2420 print_rtl (rtl_dump_file, desc->niter_expr);
2421 fprintf (rtl_dump_file, "\n");
2423 fprintf (rtl_dump_file, " upper bound: ");
2424 fprintf (rtl_dump_file, HOST_WIDEST_INT_PRINT_DEC, desc->niter_max);
2425 fprintf (rtl_dump_file, "\n");
2427 else
2428 fprintf (rtl_dump_file, "Loop %d is not simple.\n", loop->num);
2431 free (body);
2434 /* Creates a simple loop description of LOOP if it was not computed
2435 already. */
2437 struct niter_desc *
2438 get_simple_loop_desc (struct loop *loop)
2440 struct niter_desc *desc = simple_loop_desc (loop);
2442 if (desc)
2443 return desc;
2445 desc = xmalloc (sizeof (struct niter_desc));
2446 iv_analysis_loop_init (loop);
2447 find_simple_exit (loop, desc);
2448 loop->aux = desc;
2450 return desc;
2453 /* Releases simple loop description for LOOP. */
2455 void
2456 free_simple_loop_desc (struct loop *loop)
2458 struct niter_desc *desc = simple_loop_desc (loop);
2460 if (!desc)
2461 return;
2463 free (desc);
2464 loop->aux = NULL;