1 /* Rtl-level induction variable analysis.
2 Copyright (C) 2004, 2005 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
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
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
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
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
37 Induction variable is then simply analyzed by walking the use-def
42 iv_analysis_loop_init (loop);
43 insn = iv_get_reaching_def (where, reg);
44 if (iv_analyze (insn, reg, &iv))
48 iv_analysis_done (); */
52 #include "coretypes.h"
55 #include "hard-reg-set.h"
57 #include "basic-block.h"
62 /* The insn information. */
69 /* The previous definition of the register defined by the single
73 /* The description of the iv. */
77 static struct insn_info
*insn_info
;
79 /* The last definition of register. */
85 static struct rtx_iv
*bivs
;
87 /* Maximal insn number for that there is place in insn_info array. */
89 static unsigned max_insn_no
;
91 /* Maximal register number for that there is place in bivs and last_def
94 static unsigned max_reg_no
;
96 /* Dumps information about IV to FILE. */
98 extern void dump_iv_info (FILE *, struct rtx_iv
*);
100 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
104 fprintf (file
, "not simple");
108 if (iv
->step
== const0_rtx
109 && !iv
->first_special
)
110 fprintf (file
, "invariant ");
112 print_rtl (file
, iv
->base
);
113 if (iv
->step
!= const0_rtx
)
115 fprintf (file
, " + ");
116 print_rtl (file
, iv
->step
);
117 fprintf (file
, " * iteration");
119 fprintf (file
, " (in %s)", GET_MODE_NAME (iv
->mode
));
121 if (iv
->mode
!= iv
->extend_mode
)
122 fprintf (file
, " %s to %s",
123 rtx_name
[iv
->extend
],
124 GET_MODE_NAME (iv
->extend_mode
));
126 if (iv
->mult
!= const1_rtx
)
128 fprintf (file
, " * ");
129 print_rtl (file
, iv
->mult
);
131 if (iv
->delta
!= const0_rtx
)
133 fprintf (file
, " + ");
134 print_rtl (file
, iv
->delta
);
136 if (iv
->first_special
)
137 fprintf (file
, " (first special)");
140 /* Assigns luids to insns in basic block BB. */
143 assign_luids (basic_block bb
)
148 FOR_BB_INSNS (bb
, insn
)
150 uid
= INSN_UID (insn
);
151 insn_info
[uid
].luid
= i
++;
152 insn_info
[uid
].prev_def
= NULL_RTX
;
153 insn_info
[uid
].iv
.analysed
= false;
157 /* Generates a subreg to get the least significant part of EXPR (in mode
158 INNER_MODE) to OUTER_MODE. */
161 lowpart_subreg (enum machine_mode outer_mode
, rtx expr
,
162 enum machine_mode inner_mode
)
164 return simplify_gen_subreg (outer_mode
, expr
, inner_mode
,
165 subreg_lowpart_offset (outer_mode
, inner_mode
));
168 /* Checks whether REG is a well-behaved register. */
171 simple_reg_p (rtx reg
)
175 if (GET_CODE (reg
) == SUBREG
)
177 if (!subreg_lowpart_p (reg
))
179 reg
= SUBREG_REG (reg
);
186 if (HARD_REGISTER_NUM_P (r
))
189 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
192 if (last_def
[r
] == const0_rtx
)
198 /* Checks whether assignment LHS = RHS is simple enough for us to process. */
201 simple_set_p (rtx lhs
, rtx rhs
)
206 || !simple_reg_p (lhs
))
209 if (CONSTANT_P (rhs
))
212 switch (GET_CODE (rhs
))
216 return simple_reg_p (rhs
);
221 return simple_reg_p (XEXP (rhs
, 0));
230 if (!simple_reg_p (op0
)
231 && !CONSTANT_P (op0
))
234 if (!simple_reg_p (op1
)
235 && !CONSTANT_P (op1
))
238 if (GET_CODE (rhs
) == MULT
240 && !CONSTANT_P (op1
))
243 if (GET_CODE (rhs
) == ASHIFT
254 /* Mark single SET in INSN. */
257 mark_single_set (rtx insn
, rtx set
)
259 rtx def
= SET_DEST (set
), src
;
262 src
= find_reg_equal_equiv_note (insn
);
268 if (!simple_set_p (SET_DEST (set
), src
))
272 uid
= INSN_UID (insn
);
274 bivs
[regno
].analysed
= false;
275 insn_info
[uid
].prev_def
= last_def
[regno
];
276 last_def
[regno
] = insn
;
281 /* Invalidate register REG unless it is equal to EXCEPT. */
284 kill_sets (rtx reg
, rtx by ATTRIBUTE_UNUSED
, void *except
)
286 if (GET_CODE (reg
) == SUBREG
)
287 reg
= SUBREG_REG (reg
);
293 last_def
[REGNO (reg
)] = const0_rtx
;
296 /* Marks sets in basic block BB. If DOM is true, BB dominates the loop
300 mark_sets (basic_block bb
, bool dom
)
304 FOR_BB_INSNS (bb
, insn
)
310 && (set
= single_set (insn
)))
311 def
= mark_single_set (insn
, set
);
315 note_stores (PATTERN (insn
), kill_sets
, def
);
319 /* Prepare the data for an induction variable analysis of a LOOP. */
322 iv_analysis_loop_init (struct loop
*loop
)
324 basic_block
*body
= get_loop_body_in_dom_order (loop
);
327 if ((unsigned) get_max_uid () >= max_insn_no
)
329 /* Add some reserve for insns and registers produced in optimizations. */
330 max_insn_no
= get_max_uid () + 100;
333 insn_info
= xmalloc (max_insn_no
* sizeof (struct insn_info
));
336 if ((unsigned) max_reg_num () >= max_reg_no
)
338 max_reg_no
= max_reg_num () + 100;
341 last_def
= xmalloc (max_reg_no
* sizeof (rtx
));
344 bivs
= xmalloc (max_reg_no
* sizeof (struct rtx_iv
));
347 memset (last_def
, 0, max_reg_num () * sizeof (rtx
));
349 for (b
= 0; b
< loop
->num_nodes
; b
++)
351 assign_luids (body
[b
]);
352 mark_sets (body
[b
], just_once_each_iteration_p (loop
, body
[b
]));
358 /* Gets definition of REG reaching the INSN. If REG is not simple, const0_rtx
359 is returned. If INSN is before the first def in the loop, NULL_RTX is
363 iv_get_reaching_def (rtx insn
, rtx reg
)
365 unsigned regno
, luid
, auid
;
369 if (GET_CODE (reg
) == SUBREG
)
371 if (!subreg_lowpart_p (reg
))
373 reg
= SUBREG_REG (reg
);
380 || last_def
[regno
] == const0_rtx
)
381 return last_def
[regno
];
383 bb
= BLOCK_FOR_INSN (insn
);
384 luid
= insn_info
[INSN_UID (insn
)].luid
;
386 ainsn
= last_def
[regno
];
389 abb
= BLOCK_FOR_INSN (ainsn
);
391 if (dominated_by_p (CDI_DOMINATORS
, bb
, abb
))
394 auid
= INSN_UID (ainsn
);
395 ainsn
= insn_info
[auid
].prev_def
;
403 abb
= BLOCK_FOR_INSN (ainsn
);
407 auid
= INSN_UID (ainsn
);
408 if (luid
> insn_info
[auid
].luid
)
411 ainsn
= insn_info
[auid
].prev_def
;
417 /* Sets IV to invariant CST in MODE. Always returns true (just for
418 consistency with other iv manipulation functions that may fail). */
421 iv_constant (struct rtx_iv
*iv
, rtx cst
, enum machine_mode mode
)
423 if (mode
== VOIDmode
)
424 mode
= GET_MODE (cst
);
429 iv
->step
= const0_rtx
;
430 iv
->first_special
= false;
431 iv
->extend
= UNKNOWN
;
432 iv
->extend_mode
= iv
->mode
;
433 iv
->delta
= const0_rtx
;
434 iv
->mult
= const1_rtx
;
439 /* Evaluates application of subreg to MODE on IV. */
442 iv_subreg (struct rtx_iv
*iv
, enum machine_mode mode
)
444 /* If iv is invariant, just calculate the new value. */
445 if (iv
->step
== const0_rtx
446 && !iv
->first_special
)
448 rtx val
= get_iv_value (iv
, const0_rtx
);
449 val
= lowpart_subreg (mode
, val
, iv
->extend_mode
);
452 iv
->extend
= UNKNOWN
;
453 iv
->mode
= iv
->extend_mode
= mode
;
454 iv
->delta
= const0_rtx
;
455 iv
->mult
= const1_rtx
;
459 if (iv
->extend_mode
== mode
)
462 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
465 iv
->extend
= UNKNOWN
;
468 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
469 simplify_gen_binary (MULT
, iv
->extend_mode
,
470 iv
->base
, iv
->mult
));
471 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
472 iv
->mult
= const1_rtx
;
473 iv
->delta
= const0_rtx
;
474 iv
->first_special
= false;
479 /* Evaluates application of EXTEND to MODE on IV. */
482 iv_extend (struct rtx_iv
*iv
, enum rtx_code extend
, enum machine_mode mode
)
484 /* If iv is invariant, just calculate the new value. */
485 if (iv
->step
== const0_rtx
486 && !iv
->first_special
)
488 rtx val
= get_iv_value (iv
, const0_rtx
);
489 val
= simplify_gen_unary (extend
, mode
, val
, iv
->extend_mode
);
492 iv
->extend
= UNKNOWN
;
493 iv
->mode
= iv
->extend_mode
= mode
;
494 iv
->delta
= const0_rtx
;
495 iv
->mult
= const1_rtx
;
499 if (mode
!= iv
->extend_mode
)
502 if (iv
->extend
!= UNKNOWN
503 && iv
->extend
!= extend
)
511 /* Evaluates negation of IV. */
514 iv_neg (struct rtx_iv
*iv
)
516 if (iv
->extend
== UNKNOWN
)
518 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
519 iv
->base
, iv
->extend_mode
);
520 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
521 iv
->step
, iv
->extend_mode
);
525 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
526 iv
->delta
, iv
->extend_mode
);
527 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
528 iv
->mult
, iv
->extend_mode
);
534 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
537 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
539 enum machine_mode mode
;
542 /* Extend the constant to extend_mode of the other operand if necessary. */
543 if (iv0
->extend
== UNKNOWN
544 && iv0
->mode
== iv0
->extend_mode
545 && iv0
->step
== const0_rtx
546 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
548 iv0
->extend_mode
= iv1
->extend_mode
;
549 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
550 iv0
->base
, iv0
->mode
);
552 if (iv1
->extend
== UNKNOWN
553 && iv1
->mode
== iv1
->extend_mode
554 && iv1
->step
== const0_rtx
555 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
557 iv1
->extend_mode
= iv0
->extend_mode
;
558 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
559 iv1
->base
, iv1
->mode
);
562 mode
= iv0
->extend_mode
;
563 if (mode
!= iv1
->extend_mode
)
566 if (iv0
->extend
== UNKNOWN
&& iv1
->extend
== UNKNOWN
)
568 if (iv0
->mode
!= iv1
->mode
)
571 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
572 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
577 /* Handle addition of constant. */
578 if (iv1
->extend
== UNKNOWN
580 && iv1
->step
== const0_rtx
)
582 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
586 if (iv0
->extend
== UNKNOWN
588 && iv0
->step
== const0_rtx
)
596 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
603 /* Evaluates multiplication of IV by constant CST. */
606 iv_mult (struct rtx_iv
*iv
, rtx mby
)
608 enum machine_mode mode
= iv
->extend_mode
;
610 if (GET_MODE (mby
) != VOIDmode
611 && GET_MODE (mby
) != mode
)
614 if (iv
->extend
== UNKNOWN
)
616 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
617 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
621 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
622 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
628 /* Evaluates shift of IV by constant CST. */
631 iv_shift (struct rtx_iv
*iv
, rtx mby
)
633 enum machine_mode mode
= iv
->extend_mode
;
635 if (GET_MODE (mby
) != VOIDmode
636 && GET_MODE (mby
) != mode
)
639 if (iv
->extend
== UNKNOWN
)
641 iv
->base
= simplify_gen_binary (ASHIFT
, mode
, iv
->base
, mby
);
642 iv
->step
= simplify_gen_binary (ASHIFT
, mode
, iv
->step
, mby
);
646 iv
->delta
= simplify_gen_binary (ASHIFT
, mode
, iv
->delta
, mby
);
647 iv
->mult
= simplify_gen_binary (ASHIFT
, mode
, iv
->mult
, mby
);
653 /* The recursive part of get_biv_step. Gets the value of the single value
654 defined in INSN wrto initial value of REG inside loop, in shape described
658 get_biv_step_1 (rtx insn
, rtx reg
,
659 rtx
*inner_step
, enum machine_mode
*inner_mode
,
660 enum rtx_code
*extend
, enum machine_mode outer_mode
,
663 rtx set
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
664 rtx next
, nextr
, def_insn
, tmp
;
667 set
= single_set (insn
);
668 rhs
= find_reg_equal_equiv_note (insn
);
674 code
= GET_CODE (rhs
);
687 if (code
== PLUS
&& CONSTANT_P (op0
))
689 tmp
= op0
; op0
= op1
; op1
= tmp
;
692 if (!simple_reg_p (op0
)
693 || !CONSTANT_P (op1
))
696 if (GET_MODE (rhs
) != outer_mode
)
698 /* ppc64 uses expressions like
700 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
702 this is equivalent to
704 (set x':DI (plus:DI y:DI 1))
705 (set x:SI (subreg:SI (x':DI)). */
706 if (GET_CODE (op0
) != SUBREG
)
708 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
717 if (GET_MODE (rhs
) != outer_mode
)
721 if (!simple_reg_p (op0
))
731 if (GET_CODE (next
) == SUBREG
)
733 if (!subreg_lowpart_p (next
))
736 nextr
= SUBREG_REG (next
);
737 if (GET_MODE (nextr
) != outer_mode
)
743 def_insn
= iv_get_reaching_def (insn
, nextr
);
744 if (def_insn
== const0_rtx
)
749 if (!rtx_equal_p (nextr
, reg
))
752 *inner_step
= const0_rtx
;
754 *inner_mode
= outer_mode
;
755 *outer_step
= const0_rtx
;
757 else if (!get_biv_step_1 (def_insn
, reg
,
758 inner_step
, inner_mode
, extend
, outer_mode
,
762 if (GET_CODE (next
) == SUBREG
)
764 enum machine_mode amode
= GET_MODE (next
);
766 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
770 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
771 *inner_step
, *outer_step
);
772 *outer_step
= const0_rtx
;
784 if (*inner_mode
== outer_mode
785 /* See comment in previous switch. */
786 || GET_MODE (rhs
) != outer_mode
)
787 *inner_step
= simplify_gen_binary (code
, outer_mode
,
790 *outer_step
= simplify_gen_binary (code
, outer_mode
,
796 gcc_assert (GET_MODE (op0
) == *inner_mode
797 && *extend
== UNKNOWN
798 && *outer_step
== const0_rtx
);
810 /* Gets the operation on register REG inside loop, in shape
812 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
814 If the operation cannot be described in this shape, return false. */
817 get_biv_step (rtx reg
, rtx
*inner_step
, enum machine_mode
*inner_mode
,
818 enum rtx_code
*extend
, enum machine_mode
*outer_mode
,
821 *outer_mode
= GET_MODE (reg
);
823 if (!get_biv_step_1 (last_def
[REGNO (reg
)], reg
,
824 inner_step
, inner_mode
, extend
, *outer_mode
,
828 gcc_assert ((*inner_mode
== *outer_mode
) != (*extend
!= UNKNOWN
));
829 gcc_assert (*inner_mode
!= *outer_mode
|| *outer_step
== const0_rtx
);
834 /* Determines whether DEF is a biv and if so, stores its description
838 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
841 rtx inner_step
, outer_step
;
842 enum machine_mode inner_mode
, outer_mode
;
843 enum rtx_code extend
;
847 fprintf (dump_file
, "Analysing ");
848 print_rtl (dump_file
, def
);
849 fprintf (dump_file
, " for bivness.\n");
854 if (!CONSTANT_P (def
))
857 return iv_constant (iv
, def
, VOIDmode
);
861 if (last_def
[regno
] == const0_rtx
)
864 fprintf (dump_file
, " not simple.\n");
868 if (last_def
[regno
] && bivs
[regno
].analysed
)
871 fprintf (dump_file
, " already analysed.\n");
874 return iv
->base
!= NULL_RTX
;
877 if (!last_def
[regno
])
879 iv_constant (iv
, def
, VOIDmode
);
884 if (!get_biv_step (def
, &inner_step
, &inner_mode
, &extend
,
885 &outer_mode
, &outer_step
))
891 /* Loop transforms base to es (base + inner_step) + outer_step,
892 where es means extend of subreg between inner_mode and outer_mode.
893 The corresponding induction variable is
895 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
897 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
898 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
899 iv
->mode
= inner_mode
;
900 iv
->extend_mode
= outer_mode
;
902 iv
->mult
= const1_rtx
;
903 iv
->delta
= outer_step
;
904 iv
->first_special
= inner_mode
!= outer_mode
;
909 fprintf (dump_file
, " ");
910 dump_iv_info (dump_file
, iv
);
911 fprintf (dump_file
, "\n");
916 return iv
->base
!= NULL_RTX
;
919 /* Analyzes operand OP of INSN and stores the result to *IV. */
922 iv_analyze_op (rtx insn
, rtx op
, struct rtx_iv
*iv
)
926 bool inv
= CONSTANT_P (op
);
930 fprintf (dump_file
, "Analysing operand ");
931 print_rtl (dump_file
, op
);
932 fprintf (dump_file
, " of insn ");
933 print_rtl_single (dump_file
, insn
);
936 if (GET_CODE (op
) == SUBREG
)
938 if (!subreg_lowpart_p (op
))
941 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
944 return iv_subreg (iv
, GET_MODE (op
));
950 if (!last_def
[regno
])
952 else if (last_def
[regno
] == const0_rtx
)
955 fprintf (dump_file
, " not simple.\n");
962 iv_constant (iv
, op
, VOIDmode
);
966 fprintf (dump_file
, " ");
967 dump_iv_info (dump_file
, iv
);
968 fprintf (dump_file
, "\n");
973 def_insn
= iv_get_reaching_def (insn
, op
);
974 if (def_insn
== const0_rtx
)
977 fprintf (dump_file
, " not simple.\n");
981 return iv_analyze (def_insn
, op
, iv
);
984 /* Analyzes iv DEF defined in INSN and stores the result to *IV. */
987 iv_analyze (rtx insn
, rtx def
, struct rtx_iv
*iv
)
990 rtx set
, rhs
, mby
= NULL_RTX
, tmp
;
991 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
992 struct rtx_iv iv0
, iv1
;
993 enum machine_mode amode
;
996 if (insn
== const0_rtx
)
999 if (GET_CODE (def
) == SUBREG
)
1001 if (!subreg_lowpart_p (def
))
1004 if (!iv_analyze (insn
, SUBREG_REG (def
), iv
))
1007 return iv_subreg (iv
, GET_MODE (def
));
1011 return iv_analyze_biv (def
, iv
);
1015 fprintf (dump_file
, "Analysing def of ");
1016 print_rtl (dump_file
, def
);
1017 fprintf (dump_file
, " in insn ");
1018 print_rtl_single (dump_file
, insn
);
1021 uid
= INSN_UID (insn
);
1022 if (insn_info
[uid
].iv
.analysed
)
1025 fprintf (dump_file
, " already analysed.\n");
1026 *iv
= insn_info
[uid
].iv
;
1027 return iv
->base
!= NULL_RTX
;
1030 iv
->mode
= VOIDmode
;
1031 iv
->base
= NULL_RTX
;
1032 iv
->step
= NULL_RTX
;
1034 set
= single_set (insn
);
1035 rhs
= find_reg_equal_equiv_note (insn
);
1037 rhs
= XEXP (rhs
, 0);
1039 rhs
= SET_SRC (set
);
1040 code
= GET_CODE (rhs
);
1042 if (CONSTANT_P (rhs
))
1045 amode
= GET_MODE (def
);
1052 if (!subreg_lowpart_p (rhs
))
1064 op0
= XEXP (rhs
, 0);
1069 op0
= XEXP (rhs
, 0);
1070 op1
= XEXP (rhs
, 1);
1074 op0
= XEXP (rhs
, 0);
1075 mby
= XEXP (rhs
, 1);
1076 if (!CONSTANT_P (mby
))
1078 gcc_assert (CONSTANT_P (op0
));
1086 gcc_assert (!CONSTANT_P (XEXP (rhs
, 0)));
1087 op0
= XEXP (rhs
, 0);
1088 mby
= XEXP (rhs
, 1);
1095 amode
= GET_MODE (rhs
);
1100 if (!iv_analyze_op (insn
, op0
, &iv0
))
1103 if (iv0
.mode
== VOIDmode
)
1106 iv0
.extend_mode
= amode
;
1112 if (!iv_analyze_op (insn
, op1
, &iv1
))
1115 if (iv1
.mode
== VOIDmode
)
1118 iv1
.extend_mode
= amode
;
1126 if (!iv_extend (&iv0
, code
, amode
))
1137 if (!iv_add (&iv0
, &iv1
, code
))
1142 if (!iv_mult (&iv0
, mby
))
1147 if (!iv_shift (&iv0
, mby
))
1158 iv
->analysed
= true;
1159 insn_info
[uid
].iv
= *iv
;
1163 print_rtl (dump_file
, def
);
1164 fprintf (dump_file
, " in insn ");
1165 print_rtl_single (dump_file
, insn
);
1166 fprintf (dump_file
, " is ");
1167 dump_iv_info (dump_file
, iv
);
1168 fprintf (dump_file
, "\n");
1171 return iv
->base
!= NULL_RTX
;
1174 /* Checks whether definition of register REG in INSN a basic induction
1175 variable. IV analysis must have been initialized (via a call to
1176 iv_analysis_loop_init) for this function to produce a result. */
1179 biv_p (rtx insn
, rtx reg
)
1186 if (last_def
[REGNO (reg
)] != insn
)
1189 return iv_analyze_biv (reg
, &iv
);
1192 /* Calculates value of IV at ITERATION-th iteration. */
1195 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1199 /* We would need to generate some if_then_else patterns, and so far
1200 it is not needed anywhere. */
1201 gcc_assert (!iv
->first_special
);
1203 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1204 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1205 simplify_gen_binary (MULT
, iv
->extend_mode
,
1206 iv
->step
, iteration
));
1210 if (iv
->extend_mode
== iv
->mode
)
1213 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1215 if (iv
->extend
== UNKNOWN
)
1218 val
= simplify_gen_unary (iv
->extend
, iv
->extend_mode
, val
, iv
->mode
);
1219 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1220 simplify_gen_binary (MULT
, iv
->extend_mode
,
1226 /* Free the data for an induction variable analysis. */
1229 iv_analysis_done (void)
1250 /* Computes inverse to X modulo (1 << MOD). */
1252 static unsigned HOST_WIDEST_INT
1253 inverse (unsigned HOST_WIDEST_INT x
, int mod
)
1255 unsigned HOST_WIDEST_INT mask
=
1256 ((unsigned HOST_WIDEST_INT
) 1 << (mod
- 1) << 1) - 1;
1257 unsigned HOST_WIDEST_INT rslt
= 1;
1260 for (i
= 0; i
< mod
- 1; i
++)
1262 rslt
= (rslt
* x
) & mask
;
1269 /* Tries to estimate the maximum number of iterations. */
1271 static unsigned HOST_WIDEST_INT
1272 determine_max_iter (struct niter_desc
*desc
)
1274 rtx niter
= desc
->niter_expr
;
1275 rtx mmin
, mmax
, left
, right
;
1276 unsigned HOST_WIDEST_INT nmax
, inc
;
1278 if (GET_CODE (niter
) == AND
1279 && GET_CODE (XEXP (niter
, 0)) == CONST_INT
)
1281 nmax
= INTVAL (XEXP (niter
, 0));
1282 if (!(nmax
& (nmax
+ 1)))
1284 desc
->niter_max
= nmax
;
1289 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
1290 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
1292 if (GET_CODE (niter
) == UDIV
)
1294 if (GET_CODE (XEXP (niter
, 1)) != CONST_INT
)
1296 desc
->niter_max
= nmax
;
1299 inc
= INTVAL (XEXP (niter
, 1));
1300 niter
= XEXP (niter
, 0);
1305 if (GET_CODE (niter
) == PLUS
)
1307 left
= XEXP (niter
, 0);
1308 right
= XEXP (niter
, 0);
1310 if (GET_CODE (right
) == CONST_INT
)
1311 right
= GEN_INT (-INTVAL (right
));
1313 else if (GET_CODE (niter
) == MINUS
)
1315 left
= XEXP (niter
, 0);
1316 right
= XEXP (niter
, 0);
1324 if (GET_CODE (left
) == CONST_INT
)
1326 if (GET_CODE (right
) == CONST_INT
)
1328 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
1330 desc
->niter_max
= nmax
/ inc
;
1334 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1337 altered_reg_used (rtx
*reg
, void *alt
)
1342 return REGNO_REG_SET_P (alt
, REGNO (*reg
));
1345 /* Marks registers altered by EXPR in set ALT. */
1348 mark_altered (rtx expr
, rtx by ATTRIBUTE_UNUSED
, void *alt
)
1350 if (GET_CODE (expr
) == SUBREG
)
1351 expr
= SUBREG_REG (expr
);
1355 SET_REGNO_REG_SET (alt
, REGNO (expr
));
1358 /* Checks whether RHS is simple enough to process. */
1361 simple_rhs_p (rtx rhs
)
1365 if (CONSTANT_P (rhs
)
1369 switch (GET_CODE (rhs
))
1373 op0
= XEXP (rhs
, 0);
1374 op1
= XEXP (rhs
, 1);
1375 /* Allow reg + const sets only. */
1376 if (REG_P (op0
) && CONSTANT_P (op1
))
1378 if (REG_P (op1
) && CONSTANT_P (op0
))
1388 /* Simplifies *EXPR using assignment in INSN. ALTERED is the set of registers
1392 simplify_using_assignment (rtx insn
, rtx
*expr
, regset altered
)
1394 rtx set
= single_set (insn
);
1395 rtx lhs
= NULL_RTX
, rhs
;
1400 lhs
= SET_DEST (set
);
1402 || altered_reg_used (&lhs
, altered
))
1408 note_stores (PATTERN (insn
), mark_altered
, altered
);
1413 /* Kill all call clobbered registers. */
1414 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1415 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1416 SET_REGNO_REG_SET (altered
, i
);
1422 rhs
= find_reg_equal_equiv_note (insn
);
1424 rhs
= XEXP (rhs
, 0);
1426 rhs
= SET_SRC (set
);
1428 if (!simple_rhs_p (rhs
))
1431 if (for_each_rtx (&rhs
, altered_reg_used
, altered
))
1434 *expr
= simplify_replace_rtx (*expr
, lhs
, rhs
);
1437 /* Checks whether A implies B. */
1440 implies_p (rtx a
, rtx b
)
1442 rtx op0
, op1
, opb0
, opb1
, r
;
1443 enum machine_mode mode
;
1445 if (GET_CODE (a
) == EQ
)
1452 r
= simplify_replace_rtx (b
, op0
, op1
);
1453 if (r
== const_true_rtx
)
1459 r
= simplify_replace_rtx (b
, op1
, op0
);
1460 if (r
== const_true_rtx
)
1465 /* A < B implies A + 1 <= B. */
1466 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1467 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1474 if (GET_CODE (a
) == GT
)
1481 if (GET_CODE (b
) == GE
)
1488 mode
= GET_MODE (op0
);
1489 if (mode
!= GET_MODE (opb0
))
1491 else if (mode
== VOIDmode
)
1493 mode
= GET_MODE (op1
);
1494 if (mode
!= GET_MODE (opb1
))
1498 if (mode
!= VOIDmode
1499 && rtx_equal_p (op1
, opb1
)
1500 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1507 /* Canonicalizes COND so that
1509 (1) Ensure that operands are ordered according to
1510 swap_commutative_operands_p.
1511 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1512 for GE, GEU, and LEU. */
1515 canon_condition (rtx cond
)
1520 enum machine_mode mode
;
1522 code
= GET_CODE (cond
);
1523 op0
= XEXP (cond
, 0);
1524 op1
= XEXP (cond
, 1);
1526 if (swap_commutative_operands_p (op0
, op1
))
1528 code
= swap_condition (code
);
1534 mode
= GET_MODE (op0
);
1535 if (mode
== VOIDmode
)
1536 mode
= GET_MODE (op1
);
1537 gcc_assert (mode
!= VOIDmode
);
1539 if (GET_CODE (op1
) == CONST_INT
1540 && GET_MODE_CLASS (mode
) != MODE_CC
1541 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
1543 HOST_WIDE_INT const_val
= INTVAL (op1
);
1544 unsigned HOST_WIDE_INT uconst_val
= const_val
;
1545 unsigned HOST_WIDE_INT max_val
1546 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
);
1551 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
1552 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
1555 /* When cross-compiling, const_val might be sign-extended from
1556 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1558 if ((HOST_WIDE_INT
) (const_val
& max_val
)
1559 != (((HOST_WIDE_INT
) 1
1560 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
1561 code
= GT
, op1
= gen_int_mode (const_val
- 1, mode
);
1565 if (uconst_val
< max_val
)
1566 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, mode
);
1570 if (uconst_val
!= 0)
1571 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, mode
);
1579 if (op0
!= XEXP (cond
, 0)
1580 || op1
!= XEXP (cond
, 1)
1581 || code
!= GET_CODE (cond
)
1582 || GET_MODE (cond
) != SImode
)
1583 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1588 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1589 set of altered regs. */
1592 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1594 rtx rev
, reve
, exp
= *expr
;
1596 if (!COMPARISON_P (exp
))
1599 /* If some register gets altered later, we do not really speak about its
1600 value at the time of comparison. */
1602 && for_each_rtx (&cond
, altered_reg_used
, altered
))
1605 rev
= reversed_condition (cond
);
1606 reve
= reversed_condition (exp
);
1608 cond
= canon_condition (cond
);
1609 exp
= canon_condition (exp
);
1611 rev
= canon_condition (rev
);
1613 reve
= canon_condition (reve
);
1615 if (rtx_equal_p (exp
, cond
))
1617 *expr
= const_true_rtx
;
1622 if (rev
&& rtx_equal_p (exp
, rev
))
1628 if (implies_p (cond
, exp
))
1630 *expr
= const_true_rtx
;
1634 if (reve
&& implies_p (cond
, reve
))
1640 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1642 if (rev
&& implies_p (exp
, rev
))
1648 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1649 if (rev
&& reve
&& implies_p (reve
, rev
))
1651 *expr
= const_true_rtx
;
1655 /* We would like to have some other tests here. TODO. */
1660 /* Use relationship between A and *B to eventually eliminate *B.
1661 OP is the operation we consider. */
1664 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1669 /* If A implies *B, we may replace *B by true. */
1670 if (implies_p (a
, *b
))
1671 *b
= const_true_rtx
;
1675 /* If *B implies A, we may replace *B by false. */
1676 if (implies_p (*b
, a
))
1685 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1686 operation we consider. */
1689 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1693 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1694 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1695 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1696 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1699 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1700 is a list, its elements are assumed to be combined using OP. */
1703 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1705 rtx head
, tail
, insn
;
1713 if (CONSTANT_P (*expr
))
1716 if (GET_CODE (*expr
) == EXPR_LIST
)
1718 head
= XEXP (*expr
, 0);
1719 tail
= XEXP (*expr
, 1);
1721 eliminate_implied_conditions (op
, &head
, tail
);
1726 neutral
= const_true_rtx
;
1731 neutral
= const0_rtx
;
1732 aggr
= const_true_rtx
;
1739 simplify_using_initial_values (loop
, UNKNOWN
, &head
);
1742 XEXP (*expr
, 0) = aggr
;
1743 XEXP (*expr
, 1) = NULL_RTX
;
1746 else if (head
== neutral
)
1749 simplify_using_initial_values (loop
, op
, expr
);
1752 simplify_using_initial_values (loop
, op
, &tail
);
1754 if (tail
&& XEXP (tail
, 0) == aggr
)
1760 XEXP (*expr
, 0) = head
;
1761 XEXP (*expr
, 1) = tail
;
1765 gcc_assert (op
== UNKNOWN
);
1767 e
= loop_preheader_edge (loop
);
1768 if (e
->src
== ENTRY_BLOCK_PTR
)
1771 altered
= ALLOC_REG_SET (®_obstack
);
1775 insn
= BB_END (e
->src
);
1776 if (any_condjump_p (insn
))
1778 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false, true);
1780 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1781 cond
= reversed_condition (cond
);
1784 simplify_using_condition (cond
, expr
, altered
);
1785 if (CONSTANT_P (*expr
))
1787 FREE_REG_SET (altered
);
1793 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1798 simplify_using_assignment (insn
, expr
, altered
);
1799 if (CONSTANT_P (*expr
))
1801 FREE_REG_SET (altered
);
1806 if (!single_pred_p (e
->src
)
1807 || single_pred (e
->src
) == ENTRY_BLOCK_PTR
)
1809 e
= single_pred_edge (e
->src
);
1812 FREE_REG_SET (altered
);
1815 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
1816 that IV occurs as left operands of comparison COND and its signedness
1817 is SIGNED_P to DESC. */
1820 shorten_into_mode (struct rtx_iv
*iv
, enum machine_mode mode
,
1821 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
1823 rtx mmin
, mmax
, cond_over
, cond_under
;
1825 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
1826 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
1828 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
1837 if (cond_under
!= const0_rtx
)
1839 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1840 if (cond_over
!= const0_rtx
)
1841 desc
->noloop_assumptions
=
1842 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
1849 if (cond_over
!= const0_rtx
)
1851 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1852 if (cond_under
!= const0_rtx
)
1853 desc
->noloop_assumptions
=
1854 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
1858 if (cond_over
!= const0_rtx
)
1860 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1861 if (cond_under
!= const0_rtx
)
1863 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1871 iv
->extend
= signed_p
? SIGN_EXTEND
: ZERO_EXTEND
;
1874 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
1875 subregs of the same mode if possible (sometimes it is necessary to add
1876 some assumptions to DESC). */
1879 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
1880 enum rtx_code cond
, struct niter_desc
*desc
)
1882 enum machine_mode comp_mode
;
1885 /* If the ivs behave specially in the first iteration, or are
1886 added/multiplied after extending, we ignore them. */
1887 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
1889 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
1892 /* If there is some extend, it must match signedness of the comparison. */
1897 if (iv0
->extend
== ZERO_EXTEND
1898 || iv1
->extend
== ZERO_EXTEND
)
1905 if (iv0
->extend
== SIGN_EXTEND
1906 || iv1
->extend
== SIGN_EXTEND
)
1912 if (iv0
->extend
!= UNKNOWN
1913 && iv1
->extend
!= UNKNOWN
1914 && iv0
->extend
!= iv1
->extend
)
1918 if (iv0
->extend
!= UNKNOWN
)
1919 signed_p
= iv0
->extend
== SIGN_EXTEND
;
1920 if (iv1
->extend
!= UNKNOWN
)
1921 signed_p
= iv1
->extend
== SIGN_EXTEND
;
1928 /* Values of both variables should be computed in the same mode. These
1929 might indeed be different, if we have comparison like
1931 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
1933 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
1934 in different modes. This does not seem impossible to handle, but
1935 it hardly ever occurs in practice.
1937 The only exception is the case when one of operands is invariant.
1938 For example pentium 3 generates comparisons like
1939 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
1940 definitely do not want this prevent the optimization. */
1941 comp_mode
= iv0
->extend_mode
;
1942 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
1943 comp_mode
= iv1
->extend_mode
;
1945 if (iv0
->extend_mode
!= comp_mode
)
1947 if (iv0
->mode
!= iv0
->extend_mode
1948 || iv0
->step
!= const0_rtx
)
1951 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
1952 comp_mode
, iv0
->base
, iv0
->mode
);
1953 iv0
->extend_mode
= comp_mode
;
1956 if (iv1
->extend_mode
!= comp_mode
)
1958 if (iv1
->mode
!= iv1
->extend_mode
1959 || iv1
->step
!= const0_rtx
)
1962 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
1963 comp_mode
, iv1
->base
, iv1
->mode
);
1964 iv1
->extend_mode
= comp_mode
;
1967 /* Check that both ivs belong to a range of a single mode. If one of the
1968 operands is an invariant, we may need to shorten it into the common
1970 if (iv0
->mode
== iv0
->extend_mode
1971 && iv0
->step
== const0_rtx
1972 && iv0
->mode
!= iv1
->mode
)
1973 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
1975 if (iv1
->mode
== iv1
->extend_mode
1976 && iv1
->step
== const0_rtx
1977 && iv0
->mode
!= iv1
->mode
)
1978 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
1980 if (iv0
->mode
!= iv1
->mode
)
1983 desc
->mode
= iv0
->mode
;
1984 desc
->signed_p
= signed_p
;
1989 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
1990 the result into DESC. Very similar to determine_number_of_iterations
1991 (basically its rtl version), complicated by things like subregs. */
1994 iv_number_of_iterations (struct loop
*loop
, rtx insn
, rtx condition
,
1995 struct niter_desc
*desc
)
1997 rtx op0
, op1
, delta
, step
, bound
, may_xform
, def_insn
, tmp
, tmp0
, tmp1
;
1998 struct rtx_iv iv0
, iv1
, tmp_iv
;
1999 rtx assumption
, may_not_xform
;
2001 enum machine_mode mode
, comp_mode
;
2002 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
2003 unsigned HOST_WIDEST_INT s
, size
, d
, inv
;
2004 HOST_WIDEST_INT up
, down
, inc
, step_val
;
2005 int was_sharp
= false;
2009 /* The meaning of these assumptions is this:
2011 then the rest of information does not have to be valid
2012 if noloop_assumptions then the loop does not roll
2013 if infinite then this exit is never used */
2015 desc
->assumptions
= NULL_RTX
;
2016 desc
->noloop_assumptions
= NULL_RTX
;
2017 desc
->infinite
= NULL_RTX
;
2018 desc
->simple_p
= true;
2020 desc
->const_iter
= false;
2021 desc
->niter_expr
= NULL_RTX
;
2022 desc
->niter_max
= 0;
2024 cond
= GET_CODE (condition
);
2025 gcc_assert (COMPARISON_P (condition
));
2027 mode
= GET_MODE (XEXP (condition
, 0));
2028 if (mode
== VOIDmode
)
2029 mode
= GET_MODE (XEXP (condition
, 1));
2030 /* The constant comparisons should be folded. */
2031 gcc_assert (mode
!= VOIDmode
);
2033 /* We only handle integers or pointers. */
2034 if (GET_MODE_CLASS (mode
) != MODE_INT
2035 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
2038 op0
= XEXP (condition
, 0);
2039 def_insn
= iv_get_reaching_def (insn
, op0
);
2040 if (!iv_analyze (def_insn
, op0
, &iv0
))
2042 if (iv0
.extend_mode
== VOIDmode
)
2043 iv0
.mode
= iv0
.extend_mode
= mode
;
2045 op1
= XEXP (condition
, 1);
2046 def_insn
= iv_get_reaching_def (insn
, op1
);
2047 if (!iv_analyze (def_insn
, op1
, &iv1
))
2049 if (iv1
.extend_mode
== VOIDmode
)
2050 iv1
.mode
= iv1
.extend_mode
= mode
;
2052 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
2053 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
2056 /* Check condition and normalize it. */
2064 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
2065 cond
= swap_condition (cond
);
2077 /* Handle extends. This is relatively nontrivial, so we only try in some
2078 easy cases, when we can canonicalize the ivs (possibly by adding some
2079 assumptions) to shape subreg (base + i * step). This function also fills
2080 in desc->mode and desc->signed_p. */
2082 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2085 comp_mode
= iv0
.extend_mode
;
2087 size
= GET_MODE_BITSIZE (mode
);
2088 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2089 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2090 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2092 if (GET_CODE (iv0
.step
) != CONST_INT
|| GET_CODE (iv1
.step
) != CONST_INT
)
2095 /* We can take care of the case of two induction variables chasing each other
2096 if the test is NE. I have never seen a loop using it, but still it is
2098 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2103 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2104 iv1
.step
= const0_rtx
;
2107 /* This is either infinite loop or the one that ends immediately, depending
2108 on initial values. Unswitching should remove this kind of conditions. */
2109 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2114 if (iv0
.step
== const0_rtx
)
2115 step_val
= -INTVAL (iv1
.step
);
2117 step_val
= INTVAL (iv0
.step
);
2119 /* Ignore loops of while (i-- < 10) type. */
2123 step_is_pow2
= !(step_val
& (step_val
- 1));
2127 /* We do not care about whether the step is power of two in this
2129 step_is_pow2
= false;
2133 /* Some more condition normalization. We must record some assumptions
2134 due to overflows. */
2139 /* We want to take care only of non-sharp relationals; this is easy,
2140 as in cases the overflow would make the transformation unsafe
2141 the loop does not roll. Seemingly it would make more sense to want
2142 to take care of sharp relationals instead, as NE is more similar to
2143 them, but the problem is that here the transformation would be more
2144 difficult due to possibly infinite loops. */
2145 if (iv0
.step
== const0_rtx
)
2147 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2148 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2150 if (assumption
== const_true_rtx
)
2152 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2153 iv0
.base
, const1_rtx
);
2157 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2158 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2160 if (assumption
== const_true_rtx
)
2162 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2163 iv1
.base
, constm1_rtx
);
2166 if (assumption
!= const0_rtx
)
2167 desc
->noloop_assumptions
=
2168 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2169 cond
= (cond
== LT
) ? LE
: LEU
;
2171 /* It will be useful to be able to tell the difference once more in
2172 LE -> NE reduction. */
2178 /* Take care of trivially infinite loops. */
2181 if (iv0
.step
== const0_rtx
)
2183 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2184 if (rtx_equal_p (tmp
, mode_mmin
))
2187 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2193 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2194 if (rtx_equal_p (tmp
, mode_mmax
))
2197 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2203 /* If we can we want to take care of NE conditions instead of size
2204 comparisons, as they are much more friendly (most importantly
2205 this takes care of special handling of loops with step 1). We can
2206 do it if we first check that upper bound is greater or equal to
2207 lower bound, their difference is constant c modulo step and that
2208 there is not an overflow. */
2211 if (iv0
.step
== const0_rtx
)
2212 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2215 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2216 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2217 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2218 may_xform
= const0_rtx
;
2219 may_not_xform
= const_true_rtx
;
2221 if (GET_CODE (delta
) == CONST_INT
)
2223 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2225 /* A special case. We have transformed condition of type
2226 for (i = 0; i < 4; i += 4)
2228 for (i = 0; i <= 3; i += 4)
2229 obviously if the test for overflow during that transformation
2230 passed, we cannot overflow here. Most importantly any
2231 loop with sharp end condition and step 1 falls into this
2232 category, so handling this case specially is definitely
2233 worth the troubles. */
2234 may_xform
= const_true_rtx
;
2236 else if (iv0
.step
== const0_rtx
)
2238 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2239 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2240 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2241 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2242 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2244 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2250 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2251 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2252 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2253 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2254 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2256 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2262 if (may_xform
!= const0_rtx
)
2264 /* We perform the transformation always provided that it is not
2265 completely senseless. This is OK, as we would need this assumption
2266 to determine the number of iterations anyway. */
2267 if (may_xform
!= const_true_rtx
)
2269 /* If the step is a power of two and the final value we have
2270 computed overflows, the cycle is infinite. Otherwise it
2271 is nontrivial to compute the number of iterations. */
2273 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2276 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2280 /* We are going to lose some information about upper bound on
2281 number of iterations in this step, so record the information
2283 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2284 if (GET_CODE (iv1
.base
) == CONST_INT
)
2285 up
= INTVAL (iv1
.base
);
2287 up
= INTVAL (mode_mmax
) - inc
;
2288 down
= INTVAL (GET_CODE (iv0
.base
) == CONST_INT
2291 desc
->niter_max
= (up
- down
) / inc
+ 1;
2293 if (iv0
.step
== const0_rtx
)
2295 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2296 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2300 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2301 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2304 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2305 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2306 assumption
= simplify_gen_relational (reverse_condition (cond
),
2307 SImode
, mode
, tmp0
, tmp1
);
2308 if (assumption
== const_true_rtx
)
2310 else if (assumption
!= const0_rtx
)
2311 desc
->noloop_assumptions
=
2312 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2317 /* Count the number of iterations. */
2320 /* Everything we do here is just arithmetics modulo size of mode. This
2321 makes us able to do more involved computations of number of iterations
2322 than in other cases. First transform the condition into shape
2323 s * i <> c, with s positive. */
2324 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2325 iv0
.base
= const0_rtx
;
2326 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2327 iv1
.step
= const0_rtx
;
2328 if (INTVAL (iv0
.step
) < 0)
2330 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, mode
);
2331 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, mode
);
2333 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2335 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2336 is infinite. Otherwise, the number of iterations is
2337 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2338 s
= INTVAL (iv0
.step
); d
= 1;
2345 bound
= GEN_INT (((unsigned HOST_WIDEST_INT
) 1 << (size
- 1 ) << 1) - 1);
2347 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2348 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, GEN_INT (d
));
2349 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2350 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2352 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, GEN_INT (d
));
2353 inv
= inverse (s
, size
);
2354 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, gen_int_mode (inv
, mode
));
2355 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2359 if (iv1
.step
== const0_rtx
)
2360 /* Condition in shape a + s * i <= b
2361 We must know that b + s does not overflow and a <= b + s and then we
2362 can compute number of iterations as (b + s - a) / s. (It might
2363 seem that we in fact could be more clever about testing the b + s
2364 overflow condition using some information about b - a mod s,
2365 but it was already taken into account during LE -> NE transform). */
2368 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2369 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2371 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2372 lowpart_subreg (mode
, step
,
2378 /* If s is power of 2, we know that the loop is infinite if
2379 a % s <= b % s and b + s overflows. */
2380 assumption
= simplify_gen_relational (reverse_condition (cond
),
2384 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2385 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2386 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2387 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2389 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2393 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2396 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2399 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2400 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2401 assumption
= simplify_gen_relational (reverse_condition (cond
),
2402 SImode
, mode
, tmp0
, tmp
);
2404 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2405 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2409 /* Condition in shape a <= b - s * i
2410 We must know that a - s does not overflow and a - s <= b and then
2411 we can again compute number of iterations as (b - (a - s)) / s. */
2412 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2413 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2414 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2416 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmin
,
2417 lowpart_subreg (mode
, step
, comp_mode
));
2422 /* If s is power of 2, we know that the loop is infinite if
2423 a % s <= b % s and a - s overflows. */
2424 assumption
= simplify_gen_relational (reverse_condition (cond
),
2428 t0
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp0
), step
);
2429 t1
= simplify_gen_binary (UMOD
, mode
, copy_rtx (tmp1
), step
);
2430 tmp
= simplify_gen_relational (cond
, SImode
, mode
, t0
, t1
);
2431 assumption
= simplify_gen_binary (AND
, SImode
, assumption
, tmp
);
2433 alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2437 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2440 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2443 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2444 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2445 assumption
= simplify_gen_relational (reverse_condition (cond
),
2448 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2449 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2451 if (assumption
== const_true_rtx
)
2453 else if (assumption
!= const0_rtx
)
2454 desc
->noloop_assumptions
=
2455 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2456 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2457 desc
->niter_expr
= delta
;
2460 old_niter
= desc
->niter_expr
;
2462 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2463 if (desc
->assumptions
2464 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2466 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2467 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2468 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2470 /* Rerun the simplification. Consider code (created by copying loop headers)
2482 The first pass determines that i = 0, the second pass uses it to eliminate
2483 noloop assumption. */
2485 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2486 if (desc
->assumptions
2487 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2489 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2490 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2491 simplify_using_initial_values (loop
, UNKNOWN
, &desc
->niter_expr
);
2493 if (desc
->noloop_assumptions
2494 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2497 if (GET_CODE (desc
->niter_expr
) == CONST_INT
)
2499 unsigned HOST_WIDEST_INT val
= INTVAL (desc
->niter_expr
);
2501 desc
->const_iter
= true;
2502 desc
->niter_max
= desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2506 if (!desc
->niter_max
)
2507 desc
->niter_max
= determine_max_iter (desc
);
2509 /* simplify_using_initial_values does a copy propagation on the registers
2510 in the expression for the number of iterations. This prolongs life
2511 ranges of registers and increases register pressure, and usually
2512 brings no gain (and if it happens to do, the cse pass will take care
2513 of it anyway). So prevent this behavior, unless it enabled us to
2514 derive that the number of iterations is a constant. */
2515 desc
->niter_expr
= old_niter
;
2521 desc
->simple_p
= false;
2525 desc
->const_iter
= true;
2527 desc
->niter_max
= 0;
2528 desc
->niter_expr
= const0_rtx
;
2532 /* Checks whether E is a simple exit from LOOP and stores its description
2536 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2538 basic_block exit_bb
;
2543 desc
->simple_p
= false;
2545 /* It must belong directly to the loop. */
2546 if (exit_bb
->loop_father
!= loop
)
2549 /* It must be tested (at least) once during any iteration. */
2550 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2553 /* It must end in a simple conditional jump. */
2554 if (!any_condjump_p (BB_END (exit_bb
)))
2557 ein
= EDGE_SUCC (exit_bb
, 0);
2559 ein
= EDGE_SUCC (exit_bb
, 1);
2562 desc
->in_edge
= ein
;
2564 /* Test whether the condition is suitable. */
2565 if (!(condition
= get_condition (BB_END (ein
->src
), &at
, false, false)))
2568 if (ein
->flags
& EDGE_FALLTHRU
)
2570 condition
= reversed_condition (condition
);
2575 /* Check that we are able to determine number of iterations and fill
2576 in information about it. */
2577 iv_number_of_iterations (loop
, at
, condition
, desc
);
2580 /* Finds a simple exit of LOOP and stores its description into DESC. */
2583 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2588 struct niter_desc act
;
2592 desc
->simple_p
= false;
2593 body
= get_loop_body (loop
);
2595 for (i
= 0; i
< loop
->num_nodes
; i
++)
2597 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
2599 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2602 check_simple_exit (loop
, e
, &act
);
2606 /* Prefer constant iterations; the less the better. */
2609 else if (!act
.const_iter
2610 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2620 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2621 fprintf (dump_file
, " simple exit %d -> %d\n",
2622 desc
->out_edge
->src
->index
,
2623 desc
->out_edge
->dest
->index
);
2624 if (desc
->assumptions
)
2626 fprintf (dump_file
, " assumptions: ");
2627 print_rtl (dump_file
, desc
->assumptions
);
2628 fprintf (dump_file
, "\n");
2630 if (desc
->noloop_assumptions
)
2632 fprintf (dump_file
, " does not roll if: ");
2633 print_rtl (dump_file
, desc
->noloop_assumptions
);
2634 fprintf (dump_file
, "\n");
2638 fprintf (dump_file
, " infinite if: ");
2639 print_rtl (dump_file
, desc
->infinite
);
2640 fprintf (dump_file
, "\n");
2643 fprintf (dump_file
, " number of iterations: ");
2644 print_rtl (dump_file
, desc
->niter_expr
);
2645 fprintf (dump_file
, "\n");
2647 fprintf (dump_file
, " upper bound: ");
2648 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter_max
);
2649 fprintf (dump_file
, "\n");
2652 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
2658 /* Creates a simple loop description of LOOP if it was not computed
2662 get_simple_loop_desc (struct loop
*loop
)
2664 struct niter_desc
*desc
= simple_loop_desc (loop
);
2669 desc
= xmalloc (sizeof (struct niter_desc
));
2670 iv_analysis_loop_init (loop
);
2671 find_simple_exit (loop
, desc
);
2677 /* Releases simple loop description for LOOP. */
2680 free_simple_loop_desc (struct loop
*loop
)
2682 struct niter_desc
*desc
= simple_loop_desc (loop
);