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
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"
56 #include "basic-block.h"
61 /* The insn information. */
68 /* The previous definition of the register defined by the single
72 /* The description of the iv. */
76 static struct insn_info
*insn_info
;
78 /* The last definition of register. */
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
93 static unsigned max_reg_no
;
95 /* Dumps information about IV to FILE. */
97 extern void dump_iv_info (FILE *, struct rtx_iv
*);
99 dump_iv_info (FILE *file
, struct rtx_iv
*iv
)
103 fprintf (file
, "not simple");
107 if (iv
->step
== const0_rtx
)
109 fprintf (file
, "invariant ");
110 print_rtl (file
, iv
->base
);
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. */
142 assign_luids (basic_block bb
)
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. */
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. */
170 simple_reg_p (rtx reg
)
174 if (GET_CODE (reg
) == SUBREG
)
176 if (!subreg_lowpart_p (reg
))
178 reg
= SUBREG_REG (reg
);
185 if (HARD_REGISTER_NUM_P (r
))
188 if (GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
191 if (last_def
[r
] == const0_rtx
)
197 /* Checks whether assignment LHS = RHS is simple enough for us to process. */
200 simple_set_p (rtx lhs
, rtx rhs
)
205 || !simple_reg_p (lhs
))
208 if (CONSTANT_P (rhs
))
211 switch (GET_CODE (rhs
))
215 return simple_reg_p (rhs
);
220 return simple_reg_p (XEXP (rhs
, 0));
228 if (!simple_reg_p (op0
)
229 && !CONSTANT_P (op0
))
232 if (!simple_reg_p (op1
)
233 && !CONSTANT_P (op1
))
236 if (GET_CODE (rhs
) == MULT
238 && !CONSTANT_P (op1
))
248 /* Mark single SET in INSN. */
251 mark_single_set (rtx insn
, rtx set
)
253 rtx def
= SET_DEST (set
), src
;
256 src
= find_reg_equal_equiv_note (insn
);
262 if (!simple_set_p (SET_DEST (set
), src
))
266 uid
= INSN_UID (insn
);
268 bivs
[regno
].analysed
= false;
269 insn_info
[uid
].prev_def
= last_def
[regno
];
270 last_def
[regno
] = insn
;
275 /* Invalidate register REG unless it is equal to EXCEPT. */
278 kill_sets (rtx reg
, rtx by ATTRIBUTE_UNUSED
, void *except
)
280 if (GET_CODE (reg
) == SUBREG
)
281 reg
= SUBREG_REG (reg
);
287 last_def
[REGNO (reg
)] = const0_rtx
;
290 /* Marks sets in basic block BB. If DOM is true, BB dominates the loop
294 mark_sets (basic_block bb
, bool dom
)
298 FOR_BB_INSNS (bb
, insn
)
304 && (set
= single_set (insn
)))
305 def
= mark_single_set (insn
, set
);
309 note_stores (PATTERN (insn
), kill_sets
, def
);
313 /* Prepare the data for an induction variable analysis of a LOOP. */
316 iv_analysis_loop_init (struct loop
*loop
)
318 basic_block
*body
= get_loop_body_in_dom_order (loop
);
321 if ((unsigned) get_max_uid () >= max_insn_no
)
323 /* Add some reserve for insns and registers produced in optimizations. */
324 max_insn_no
= get_max_uid () + 100;
327 insn_info
= xmalloc (max_insn_no
* sizeof (struct insn_info
));
330 if ((unsigned) max_reg_num () >= max_reg_no
)
332 max_reg_no
= max_reg_num () + 100;
335 last_def
= xmalloc (max_reg_no
* sizeof (rtx
));
338 bivs
= xmalloc (max_reg_no
* sizeof (struct rtx_iv
));
341 memset (last_def
, 0, max_reg_num () * sizeof (rtx
));
343 for (b
= 0; b
< loop
->num_nodes
; b
++)
345 assign_luids (body
[b
]);
346 mark_sets (body
[b
], just_once_each_iteration_p (loop
, body
[b
]));
352 /* Gets definition of REG reaching the INSN. If REG is not simple, const0_rtx
353 is returned. If INSN is before the first def in the loop, NULL_RTX is
357 iv_get_reaching_def (rtx insn
, rtx reg
)
359 unsigned regno
, luid
, auid
;
363 if (GET_CODE (reg
) == SUBREG
)
365 if (!subreg_lowpart_p (reg
))
367 reg
= SUBREG_REG (reg
);
374 || last_def
[regno
] == const0_rtx
)
375 return last_def
[regno
];
377 bb
= BLOCK_FOR_INSN (insn
);
378 luid
= insn_info
[INSN_UID (insn
)].luid
;
380 ainsn
= last_def
[regno
];
383 abb
= BLOCK_FOR_INSN (ainsn
);
385 if (dominated_by_p (CDI_DOMINATORS
, bb
, abb
))
388 auid
= INSN_UID (ainsn
);
389 ainsn
= insn_info
[auid
].prev_def
;
397 abb
= BLOCK_FOR_INSN (ainsn
);
401 auid
= INSN_UID (ainsn
);
402 if (luid
> insn_info
[auid
].luid
)
405 ainsn
= insn_info
[auid
].prev_def
;
411 /* Sets IV to invariant CST in MODE. Always returns true (just for
412 consistency with other iv manipulation functions that may fail). */
415 iv_constant (struct rtx_iv
*iv
, rtx cst
, enum machine_mode mode
)
417 if (mode
== VOIDmode
)
418 mode
= GET_MODE (cst
);
423 iv
->step
= const0_rtx
;
424 iv
->first_special
= false;
426 iv
->extend_mode
= iv
->mode
;
427 iv
->delta
= const0_rtx
;
428 iv
->mult
= const1_rtx
;
433 /* Evaluates application of subreg to MODE on IV. */
436 iv_subreg (struct rtx_iv
*iv
, enum machine_mode mode
)
438 if (iv
->extend_mode
== mode
)
441 if (GET_MODE_BITSIZE (mode
) > GET_MODE_BITSIZE (iv
->mode
))
447 iv
->base
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
448 simplify_gen_binary (MULT
, iv
->extend_mode
,
449 iv
->base
, iv
->mult
));
450 iv
->step
= simplify_gen_binary (MULT
, iv
->extend_mode
, iv
->step
, iv
->mult
);
451 iv
->mult
= const1_rtx
;
452 iv
->delta
= const0_rtx
;
453 iv
->first_special
= false;
458 /* Evaluates application of EXTEND to MODE on IV. */
461 iv_extend (struct rtx_iv
*iv
, enum rtx_code extend
, enum machine_mode mode
)
463 if (mode
!= iv
->extend_mode
)
466 if (iv
->extend
!= NIL
467 && iv
->extend
!= extend
)
475 /* Evaluates negation of IV. */
478 iv_neg (struct rtx_iv
*iv
)
480 if (iv
->extend
== NIL
)
482 iv
->base
= simplify_gen_unary (NEG
, iv
->extend_mode
,
483 iv
->base
, iv
->extend_mode
);
484 iv
->step
= simplify_gen_unary (NEG
, iv
->extend_mode
,
485 iv
->step
, iv
->extend_mode
);
489 iv
->delta
= simplify_gen_unary (NEG
, iv
->extend_mode
,
490 iv
->delta
, iv
->extend_mode
);
491 iv
->mult
= simplify_gen_unary (NEG
, iv
->extend_mode
,
492 iv
->mult
, iv
->extend_mode
);
498 /* Evaluates addition or subtraction (according to OP) of IV1 to IV0. */
501 iv_add (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
, enum rtx_code op
)
503 enum machine_mode mode
;
506 /* Extend the constant to extend_mode of the other operand if necessary. */
507 if (iv0
->extend
== NIL
508 && iv0
->mode
== iv0
->extend_mode
509 && iv0
->step
== const0_rtx
510 && GET_MODE_SIZE (iv0
->extend_mode
) < GET_MODE_SIZE (iv1
->extend_mode
))
512 iv0
->extend_mode
= iv1
->extend_mode
;
513 iv0
->base
= simplify_gen_unary (ZERO_EXTEND
, iv0
->extend_mode
,
514 iv0
->base
, iv0
->mode
);
516 if (iv1
->extend
== NIL
517 && iv1
->mode
== iv1
->extend_mode
518 && iv1
->step
== const0_rtx
519 && GET_MODE_SIZE (iv1
->extend_mode
) < GET_MODE_SIZE (iv0
->extend_mode
))
521 iv1
->extend_mode
= iv0
->extend_mode
;
522 iv1
->base
= simplify_gen_unary (ZERO_EXTEND
, iv1
->extend_mode
,
523 iv1
->base
, iv1
->mode
);
526 mode
= iv0
->extend_mode
;
527 if (mode
!= iv1
->extend_mode
)
530 if (iv0
->extend
== NIL
&& iv1
->extend
== NIL
)
532 if (iv0
->mode
!= iv1
->mode
)
535 iv0
->base
= simplify_gen_binary (op
, mode
, iv0
->base
, iv1
->base
);
536 iv0
->step
= simplify_gen_binary (op
, mode
, iv0
->step
, iv1
->step
);
541 /* Handle addition of constant. */
542 if (iv1
->extend
== NIL
544 && iv1
->step
== const0_rtx
)
546 iv0
->delta
= simplify_gen_binary (op
, mode
, iv0
->delta
, iv1
->base
);
550 if (iv0
->extend
== NIL
552 && iv0
->step
== const0_rtx
)
560 iv0
->delta
= simplify_gen_binary (PLUS
, mode
, iv0
->delta
, arg
);
567 /* Evaluates multiplication of IV by constant CST. */
570 iv_mult (struct rtx_iv
*iv
, rtx mby
)
572 enum machine_mode mode
= iv
->extend_mode
;
574 if (GET_MODE (mby
) != VOIDmode
575 && GET_MODE (mby
) != mode
)
578 if (iv
->extend
== NIL
)
580 iv
->base
= simplify_gen_binary (MULT
, mode
, iv
->base
, mby
);
581 iv
->step
= simplify_gen_binary (MULT
, mode
, iv
->step
, mby
);
585 iv
->delta
= simplify_gen_binary (MULT
, mode
, iv
->delta
, mby
);
586 iv
->mult
= simplify_gen_binary (MULT
, mode
, iv
->mult
, mby
);
592 /* The recursive part of get_biv_step. Gets the value of the single value
593 defined in INSN wrto initial value of REG inside loop, in shape described
597 get_biv_step_1 (rtx insn
, rtx reg
,
598 rtx
*inner_step
, enum machine_mode
*inner_mode
,
599 enum rtx_code
*extend
, enum machine_mode outer_mode
,
602 rtx set
, lhs
, rhs
, op0
= NULL_RTX
, op1
= NULL_RTX
;
603 rtx next
, nextr
, def_insn
, tmp
;
606 set
= single_set (insn
);
607 rhs
= find_reg_equal_equiv_note (insn
);
612 lhs
= SET_DEST (set
);
614 code
= GET_CODE (rhs
);
627 if (code
== PLUS
&& CONSTANT_P (op0
))
629 tmp
= op0
; op0
= op1
; op1
= tmp
;
632 if (!simple_reg_p (op0
)
633 || !CONSTANT_P (op1
))
636 if (GET_MODE (rhs
) != outer_mode
)
638 /* ppc64 uses expressions like
640 (set x:SI (plus:SI (subreg:SI y:DI) 1)).
642 this is equivalent to
644 (set x':DI (plus:DI y:DI 1))
645 (set x:SI (subreg:SI (x':DI)). */
646 if (GET_CODE (op0
) != SUBREG
)
648 if (GET_MODE (SUBREG_REG (op0
)) != outer_mode
)
657 if (GET_MODE (rhs
) != outer_mode
)
661 if (!simple_reg_p (op0
))
671 if (GET_CODE (next
) == SUBREG
)
673 if (!subreg_lowpart_p (next
))
676 nextr
= SUBREG_REG (next
);
677 if (GET_MODE (nextr
) != outer_mode
)
683 def_insn
= iv_get_reaching_def (insn
, nextr
);
684 if (def_insn
== const0_rtx
)
689 if (!rtx_equal_p (nextr
, reg
))
692 *inner_step
= const0_rtx
;
694 *inner_mode
= outer_mode
;
695 *outer_step
= const0_rtx
;
697 else if (!get_biv_step_1 (def_insn
, reg
,
698 inner_step
, inner_mode
, extend
, outer_mode
,
702 if (GET_CODE (next
) == SUBREG
)
704 enum machine_mode amode
= GET_MODE (next
);
706 if (GET_MODE_SIZE (amode
) > GET_MODE_SIZE (*inner_mode
))
710 *inner_step
= simplify_gen_binary (PLUS
, outer_mode
,
711 *inner_step
, *outer_step
);
712 *outer_step
= const0_rtx
;
724 if (*inner_mode
== outer_mode
725 /* See comment in previous switch. */
726 || GET_MODE (rhs
) != outer_mode
)
727 *inner_step
= simplify_gen_binary (code
, outer_mode
,
730 *outer_step
= simplify_gen_binary (code
, outer_mode
,
736 if (GET_MODE (op0
) != *inner_mode
738 || *outer_step
!= const0_rtx
)
751 /* Gets the operation on register REG inside loop, in shape
753 OUTER_STEP + EXTEND_{OUTER_MODE} (SUBREG_{INNER_MODE} (REG + INNER_STEP))
755 If the operation cannot be described in this shape, return false. */
758 get_biv_step (rtx reg
, rtx
*inner_step
, enum machine_mode
*inner_mode
,
759 enum rtx_code
*extend
, enum machine_mode
*outer_mode
,
762 *outer_mode
= GET_MODE (reg
);
764 if (!get_biv_step_1 (last_def
[REGNO (reg
)], reg
,
765 inner_step
, inner_mode
, extend
, *outer_mode
,
769 if (*inner_mode
!= *outer_mode
773 if (*inner_mode
== *outer_mode
777 if (*inner_mode
== *outer_mode
778 && *outer_step
!= const0_rtx
)
784 /* Determines whether DEF is a biv and if so, stores its description
788 iv_analyze_biv (rtx def
, struct rtx_iv
*iv
)
791 rtx inner_step
, outer_step
;
792 enum machine_mode inner_mode
, outer_mode
;
793 enum rtx_code extend
;
797 fprintf (dump_file
, "Analysing ");
798 print_rtl (dump_file
, def
);
799 fprintf (dump_file
, " for bivness.\n");
804 if (!CONSTANT_P (def
))
807 return iv_constant (iv
, def
, VOIDmode
);
811 if (last_def
[regno
] == const0_rtx
)
814 fprintf (dump_file
, " not simple.\n");
818 if (last_def
[regno
] && bivs
[regno
].analysed
)
821 fprintf (dump_file
, " already analysed.\n");
824 return iv
->base
!= NULL_RTX
;
827 if (!last_def
[regno
])
829 iv_constant (iv
, def
, VOIDmode
);
834 if (!get_biv_step (def
, &inner_step
, &inner_mode
, &extend
,
835 &outer_mode
, &outer_step
))
841 /* Loop transforms base to es (base + inner_step) + outer_step,
842 where es means extend of subreg between inner_mode and outer_mode.
843 The corresponding induction variable is
845 es ((base - outer_step) + i * (inner_step + outer_step)) + outer_step */
847 iv
->base
= simplify_gen_binary (MINUS
, outer_mode
, def
, outer_step
);
848 iv
->step
= simplify_gen_binary (PLUS
, outer_mode
, inner_step
, outer_step
);
849 iv
->mode
= inner_mode
;
850 iv
->extend_mode
= outer_mode
;
852 iv
->mult
= const1_rtx
;
853 iv
->delta
= outer_step
;
854 iv
->first_special
= inner_mode
!= outer_mode
;
859 fprintf (dump_file
, " ");
860 dump_iv_info (dump_file
, iv
);
861 fprintf (dump_file
, "\n");
866 return iv
->base
!= NULL_RTX
;
869 /* Analyzes operand OP of INSN and stores the result to *IV. */
872 iv_analyze_op (rtx insn
, rtx op
, struct rtx_iv
*iv
)
876 bool inv
= CONSTANT_P (op
);
880 fprintf (dump_file
, "Analysing operand ");
881 print_rtl (dump_file
, op
);
882 fprintf (dump_file
, " of insn ");
883 print_rtl_single (dump_file
, insn
);
886 if (GET_CODE (op
) == SUBREG
)
888 if (!subreg_lowpart_p (op
))
891 if (!iv_analyze_op (insn
, SUBREG_REG (op
), iv
))
894 return iv_subreg (iv
, GET_MODE (op
));
900 if (!last_def
[regno
])
902 else if (last_def
[regno
] == const0_rtx
)
905 fprintf (dump_file
, " not simple.\n");
912 iv_constant (iv
, op
, VOIDmode
);
916 fprintf (dump_file
, " ");
917 dump_iv_info (dump_file
, iv
);
918 fprintf (dump_file
, "\n");
923 def_insn
= iv_get_reaching_def (insn
, op
);
924 if (def_insn
== const0_rtx
)
927 fprintf (dump_file
, " not simple.\n");
931 return iv_analyze (def_insn
, op
, iv
);
934 /* Analyzes iv DEF defined in INSN and stores the result to *IV. */
937 iv_analyze (rtx insn
, rtx def
, struct rtx_iv
*iv
)
940 rtx set
, rhs
, mby
= NULL_RTX
, tmp
;
941 rtx op0
= NULL_RTX
, op1
= NULL_RTX
;
942 struct rtx_iv iv0
, iv1
;
943 enum machine_mode amode
;
946 if (insn
== const0_rtx
)
949 if (GET_CODE (def
) == SUBREG
)
951 if (!subreg_lowpart_p (def
))
954 if (!iv_analyze (insn
, SUBREG_REG (def
), iv
))
957 return iv_subreg (iv
, GET_MODE (def
));
961 return iv_analyze_biv (def
, iv
);
965 fprintf (dump_file
, "Analysing def of ");
966 print_rtl (dump_file
, def
);
967 fprintf (dump_file
, " in insn ");
968 print_rtl_single (dump_file
, insn
);
971 uid
= INSN_UID (insn
);
972 if (insn_info
[uid
].iv
.analysed
)
975 fprintf (dump_file
, " already analysed.\n");
976 *iv
= insn_info
[uid
].iv
;
977 return iv
->base
!= NULL_RTX
;
984 set
= single_set (insn
);
985 rhs
= find_reg_equal_equiv_note (insn
);
990 code
= GET_CODE (rhs
);
992 if (CONSTANT_P (rhs
))
995 amode
= GET_MODE (def
);
1002 if (!subreg_lowpart_p (rhs
))
1014 op0
= XEXP (rhs
, 0);
1019 op0
= XEXP (rhs
, 0);
1020 op1
= XEXP (rhs
, 1);
1024 op0
= XEXP (rhs
, 0);
1025 mby
= XEXP (rhs
, 1);
1026 if (!CONSTANT_P (mby
))
1028 if (!CONSTANT_P (op0
))
1040 amode
= GET_MODE (rhs
);
1045 if (!iv_analyze_op (insn
, op0
, &iv0
))
1048 if (iv0
.mode
== VOIDmode
)
1051 iv0
.extend_mode
= amode
;
1057 if (!iv_analyze_op (insn
, op1
, &iv1
))
1060 if (iv1
.mode
== VOIDmode
)
1063 iv1
.extend_mode
= amode
;
1071 if (!iv_extend (&iv0
, code
, amode
))
1082 if (!iv_add (&iv0
, &iv1
, code
))
1087 if (!iv_mult (&iv0
, mby
))
1098 iv
->analysed
= true;
1099 insn_info
[uid
].iv
= *iv
;
1103 print_rtl (dump_file
, def
);
1104 fprintf (dump_file
, " in insn ");
1105 print_rtl_single (dump_file
, insn
);
1106 fprintf (dump_file
, " is ");
1107 dump_iv_info (dump_file
, iv
);
1108 fprintf (dump_file
, "\n");
1111 return iv
->base
!= NULL_RTX
;
1114 /* Calculates value of IV at ITERATION-th iteration. */
1117 get_iv_value (struct rtx_iv
*iv
, rtx iteration
)
1121 /* We would need to generate some if_then_else patterns, and so far
1122 it is not needed anywhere. */
1123 if (iv
->first_special
)
1126 if (iv
->step
!= const0_rtx
&& iteration
!= const0_rtx
)
1127 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->base
,
1128 simplify_gen_binary (MULT
, iv
->extend_mode
,
1129 iv
->step
, iteration
));
1133 if (iv
->extend_mode
== iv
->mode
)
1136 val
= lowpart_subreg (iv
->mode
, val
, iv
->extend_mode
);
1138 if (iv
->extend
== NIL
)
1141 val
= simplify_gen_unary (iv
->extend
, iv
->extend_mode
, val
, iv
->mode
);
1142 val
= simplify_gen_binary (PLUS
, iv
->extend_mode
, iv
->delta
,
1143 simplify_gen_binary (MULT
, iv
->extend_mode
,
1149 /* Free the data for an induction variable analysis. */
1152 iv_analysis_done (void)
1173 /* Computes inverse to X modulo (1 << MOD). */
1175 static unsigned HOST_WIDEST_INT
1176 inverse (unsigned HOST_WIDEST_INT x
, int mod
)
1178 unsigned HOST_WIDEST_INT mask
=
1179 ((unsigned HOST_WIDEST_INT
) 1 << (mod
- 1) << 1) - 1;
1180 unsigned HOST_WIDEST_INT rslt
= 1;
1183 for (i
= 0; i
< mod
- 1; i
++)
1185 rslt
= (rslt
* x
) & mask
;
1192 /* Tries to estimate the maximum number of iterations. */
1194 static unsigned HOST_WIDEST_INT
1195 determine_max_iter (struct niter_desc
*desc
)
1197 rtx niter
= desc
->niter_expr
;
1198 rtx mmin
, mmax
, left
, right
;
1199 unsigned HOST_WIDEST_INT nmax
, inc
;
1201 if (GET_CODE (niter
) == AND
1202 && GET_CODE (XEXP (niter
, 0)) == CONST_INT
)
1204 nmax
= INTVAL (XEXP (niter
, 0));
1205 if (!(nmax
& (nmax
+ 1)))
1207 desc
->niter_max
= nmax
;
1212 get_mode_bounds (desc
->mode
, desc
->signed_p
, desc
->mode
, &mmin
, &mmax
);
1213 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
1215 if (GET_CODE (niter
) == UDIV
)
1217 if (GET_CODE (XEXP (niter
, 1)) != CONST_INT
)
1219 desc
->niter_max
= nmax
;
1222 inc
= INTVAL (XEXP (niter
, 1));
1223 niter
= XEXP (niter
, 0);
1228 if (GET_CODE (niter
) == PLUS
)
1230 left
= XEXP (niter
, 0);
1231 right
= XEXP (niter
, 0);
1233 if (GET_CODE (right
) == CONST_INT
)
1234 right
= GEN_INT (-INTVAL (right
));
1236 else if (GET_CODE (niter
) == MINUS
)
1238 left
= XEXP (niter
, 0);
1239 right
= XEXP (niter
, 0);
1247 if (GET_CODE (left
) == CONST_INT
)
1249 if (GET_CODE (right
) == CONST_INT
)
1251 nmax
= INTVAL (mmax
) - INTVAL (mmin
);
1253 desc
->niter_max
= nmax
/ inc
;
1257 /* Checks whether register *REG is in set ALT. Callback for for_each_rtx. */
1260 altered_reg_used (rtx
*reg
, void *alt
)
1265 return REGNO_REG_SET_P (alt
, REGNO (*reg
));
1268 /* Marks registers altered by EXPR in set ALT. */
1271 mark_altered (rtx expr
, rtx by ATTRIBUTE_UNUSED
, void *alt
)
1273 if (GET_CODE (expr
) == SUBREG
)
1274 expr
= SUBREG_REG (expr
);
1278 SET_REGNO_REG_SET (alt
, REGNO (expr
));
1281 /* Checks whether RHS is simple enough to process. */
1284 simple_rhs_p (rtx rhs
)
1288 if (CONSTANT_P (rhs
)
1292 switch (GET_CODE (rhs
))
1296 op0
= XEXP (rhs
, 0);
1297 op1
= XEXP (rhs
, 1);
1298 /* Allow reg + const sets only. */
1299 if (REG_P (op0
) && CONSTANT_P (op1
))
1301 if (REG_P (op1
) && CONSTANT_P (op0
))
1311 /* Simplifies *EXPR using assignment in INSN. ALTERED is the set of registers
1315 simplify_using_assignment (rtx insn
, rtx
*expr
, regset altered
)
1317 rtx set
= single_set (insn
);
1323 lhs
= SET_DEST (set
);
1325 || altered_reg_used (&lhs
, altered
))
1331 note_stores (PATTERN (insn
), mark_altered
, altered
);
1332 if (GET_CODE (insn
) == CALL_INSN
)
1336 /* Kill all call clobbered registers. */
1337 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1338 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1339 SET_REGNO_REG_SET (altered
, i
);
1345 rhs
= find_reg_equal_equiv_note (insn
);
1347 rhs
= XEXP (rhs
, 0);
1349 rhs
= SET_SRC (set
);
1351 if (!simple_rhs_p (rhs
))
1354 if (for_each_rtx (&rhs
, altered_reg_used
, altered
))
1357 *expr
= simplify_replace_rtx (*expr
, lhs
, rhs
);
1360 /* Checks whether A implies B. */
1363 implies_p (rtx a
, rtx b
)
1365 rtx op0
, op1
, opb0
, opb1
, r
;
1366 enum machine_mode mode
;
1368 if (GET_CODE (a
) == EQ
)
1375 r
= simplify_replace_rtx (b
, op0
, op1
);
1376 if (r
== const_true_rtx
)
1382 r
= simplify_replace_rtx (b
, op1
, op0
);
1383 if (r
== const_true_rtx
)
1388 /* A < B implies A + 1 <= B. */
1389 if ((GET_CODE (a
) == GT
|| GET_CODE (a
) == LT
)
1390 && (GET_CODE (b
) == GE
|| GET_CODE (b
) == LE
))
1397 if (GET_CODE (a
) == GT
)
1404 if (GET_CODE (b
) == GE
)
1411 mode
= GET_MODE (op0
);
1412 if (mode
!= GET_MODE (opb0
))
1414 else if (mode
== VOIDmode
)
1416 mode
= GET_MODE (op1
);
1417 if (mode
!= GET_MODE (opb1
))
1421 if (mode
!= VOIDmode
1422 && rtx_equal_p (op1
, opb1
)
1423 && simplify_gen_binary (MINUS
, mode
, opb0
, op0
) == const1_rtx
)
1430 /* Canonicalizes COND so that
1432 (1) Ensure that operands are ordered according to
1433 swap_commutative_operands_p.
1434 (2) (LE x const) will be replaced with (LT x <const+1>) and similarly
1435 for GE, GEU, and LEU. */
1438 canon_condition (rtx cond
)
1443 enum machine_mode mode
;
1445 code
= GET_CODE (cond
);
1446 op0
= XEXP (cond
, 0);
1447 op1
= XEXP (cond
, 1);
1449 if (swap_commutative_operands_p (op0
, op1
))
1451 code
= swap_condition (code
);
1457 mode
= GET_MODE (op0
);
1458 if (mode
== VOIDmode
)
1459 mode
= GET_MODE (op1
);
1460 if (mode
== VOIDmode
)
1463 if (GET_CODE (op1
) == CONST_INT
1464 && GET_MODE_CLASS (mode
) != MODE_CC
1465 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
1467 HOST_WIDE_INT const_val
= INTVAL (op1
);
1468 unsigned HOST_WIDE_INT uconst_val
= const_val
;
1469 unsigned HOST_WIDE_INT max_val
1470 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (mode
);
1475 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
1476 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
1479 /* When cross-compiling, const_val might be sign-extended from
1480 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
1482 if ((HOST_WIDE_INT
) (const_val
& max_val
)
1483 != (((HOST_WIDE_INT
) 1
1484 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
1485 code
= GT
, op1
= gen_int_mode (const_val
- 1, mode
);
1489 if (uconst_val
< max_val
)
1490 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, mode
);
1494 if (uconst_val
!= 0)
1495 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, mode
);
1503 if (op0
!= XEXP (cond
, 0)
1504 || op1
!= XEXP (cond
, 1)
1505 || code
!= GET_CODE (cond
)
1506 || GET_MODE (cond
) != SImode
)
1507 cond
= gen_rtx_fmt_ee (code
, SImode
, op0
, op1
);
1512 /* Tries to use the fact that COND holds to simplify EXPR. ALTERED is the
1513 set of altered regs. */
1516 simplify_using_condition (rtx cond
, rtx
*expr
, regset altered
)
1518 rtx rev
, reve
, exp
= *expr
;
1520 if (!COMPARISON_P (exp
))
1523 /* If some register gets altered later, we do not really speak about its
1524 value at the time of comparison. */
1526 && for_each_rtx (&cond
, altered_reg_used
, altered
))
1529 rev
= reversed_condition (cond
);
1530 reve
= reversed_condition (exp
);
1532 cond
= canon_condition (cond
);
1533 exp
= canon_condition (exp
);
1535 rev
= canon_condition (rev
);
1537 reve
= canon_condition (reve
);
1539 if (rtx_equal_p (exp
, cond
))
1541 *expr
= const_true_rtx
;
1546 if (rev
&& rtx_equal_p (exp
, rev
))
1552 if (implies_p (cond
, exp
))
1554 *expr
= const_true_rtx
;
1558 if (reve
&& implies_p (cond
, reve
))
1564 /* A proof by contradiction. If *EXPR implies (not cond), *EXPR must
1566 if (rev
&& implies_p (exp
, rev
))
1572 /* Similarly, If (not *EXPR) implies (not cond), *EXPR must be true. */
1573 if (rev
&& reve
&& implies_p (reve
, rev
))
1575 *expr
= const_true_rtx
;
1579 /* We would like to have some other tests here. TODO. */
1584 /* Use relationship between A and *B to eventually eliminate *B.
1585 OP is the operation we consider. */
1588 eliminate_implied_condition (enum rtx_code op
, rtx a
, rtx
*b
)
1592 /* If A implies *B, we may replace *B by true. */
1593 if (implies_p (a
, *b
))
1594 *b
= const_true_rtx
;
1598 /* If *B implies A, we may replace *B by false. */
1599 if (implies_p (*b
, a
))
1606 /* Eliminates the conditions in TAIL that are implied by HEAD. OP is the
1607 operation we consider. */
1610 eliminate_implied_conditions (enum rtx_code op
, rtx
*head
, rtx tail
)
1614 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1615 eliminate_implied_condition (op
, *head
, &XEXP (elt
, 0));
1616 for (elt
= tail
; elt
; elt
= XEXP (elt
, 1))
1617 eliminate_implied_condition (op
, XEXP (elt
, 0), head
);
1620 /* Simplifies *EXPR using initial values at the start of the LOOP. If *EXPR
1621 is a list, its elements are assumed to be combined using OP. */
1624 simplify_using_initial_values (struct loop
*loop
, enum rtx_code op
, rtx
*expr
)
1626 rtx head
, tail
, insn
;
1629 regset_head altered_head
;
1635 if (CONSTANT_P (*expr
))
1638 if (GET_CODE (*expr
) == EXPR_LIST
)
1640 head
= XEXP (*expr
, 0);
1641 tail
= XEXP (*expr
, 1);
1643 eliminate_implied_conditions (op
, &head
, tail
);
1647 neutral
= const_true_rtx
;
1652 neutral
= const0_rtx
;
1653 aggr
= const_true_rtx
;
1658 simplify_using_initial_values (loop
, NIL
, &head
);
1661 XEXP (*expr
, 0) = aggr
;
1662 XEXP (*expr
, 1) = NULL_RTX
;
1665 else if (head
== neutral
)
1668 simplify_using_initial_values (loop
, op
, expr
);
1671 simplify_using_initial_values (loop
, op
, &tail
);
1673 if (tail
&& XEXP (tail
, 0) == aggr
)
1679 XEXP (*expr
, 0) = head
;
1680 XEXP (*expr
, 1) = tail
;
1687 e
= loop_preheader_edge (loop
);
1688 if (e
->src
== ENTRY_BLOCK_PTR
)
1691 altered
= INITIALIZE_REG_SET (altered_head
);
1695 insn
= BB_END (e
->src
);
1696 if (any_condjump_p (insn
))
1698 /* FIXME -- slightly wrong -- what if compared register
1699 gets altered between start of the condition and insn? */
1700 rtx cond
= get_condition (BB_END (e
->src
), NULL
, false);
1702 if (cond
&& (e
->flags
& EDGE_FALLTHRU
))
1703 cond
= reversed_condition (cond
);
1706 simplify_using_condition (cond
, expr
, altered
);
1707 if (CONSTANT_P (*expr
))
1709 FREE_REG_SET (altered
);
1715 FOR_BB_INSNS_REVERSE (e
->src
, insn
)
1720 simplify_using_assignment (insn
, expr
, altered
);
1721 if (CONSTANT_P (*expr
))
1723 FREE_REG_SET (altered
);
1730 || e
->src
== ENTRY_BLOCK_PTR
)
1734 FREE_REG_SET (altered
);
1737 /* Transforms invariant IV into MODE. Adds assumptions based on the fact
1738 that IV occurs as left operands of comparison COND and its signedness
1739 is SIGNED_P to DESC. */
1742 shorten_into_mode (struct rtx_iv
*iv
, enum machine_mode mode
,
1743 enum rtx_code cond
, bool signed_p
, struct niter_desc
*desc
)
1745 rtx mmin
, mmax
, cond_over
, cond_under
;
1747 get_mode_bounds (mode
, signed_p
, iv
->extend_mode
, &mmin
, &mmax
);
1748 cond_under
= simplify_gen_relational (LT
, SImode
, iv
->extend_mode
,
1750 cond_over
= simplify_gen_relational (GT
, SImode
, iv
->extend_mode
,
1759 if (cond_under
!= const0_rtx
)
1761 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1762 if (cond_over
!= const0_rtx
)
1763 desc
->noloop_assumptions
=
1764 alloc_EXPR_LIST (0, cond_over
, desc
->noloop_assumptions
);
1771 if (cond_over
!= const0_rtx
)
1773 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1774 if (cond_under
!= const0_rtx
)
1775 desc
->noloop_assumptions
=
1776 alloc_EXPR_LIST (0, cond_under
, desc
->noloop_assumptions
);
1780 if (cond_over
!= const0_rtx
)
1782 alloc_EXPR_LIST (0, cond_over
, desc
->infinite
);
1783 if (cond_under
!= const0_rtx
)
1785 alloc_EXPR_LIST (0, cond_under
, desc
->infinite
);
1793 iv
->extend
= signed_p
? SIGN_EXTEND
: ZERO_EXTEND
;
1796 /* Transforms IV0 and IV1 compared by COND so that they are both compared as
1797 subregs of the same mode if possible (sometimes it is necessary to add
1798 some assumptions to DESC). */
1801 canonicalize_iv_subregs (struct rtx_iv
*iv0
, struct rtx_iv
*iv1
,
1802 enum rtx_code cond
, struct niter_desc
*desc
)
1804 enum machine_mode comp_mode
;
1807 /* If the ivs behave specially in the first iteration, or are
1808 added/multiplied after extending, we ignore them. */
1809 if (iv0
->first_special
|| iv0
->mult
!= const1_rtx
|| iv0
->delta
!= const0_rtx
)
1811 if (iv1
->first_special
|| iv1
->mult
!= const1_rtx
|| iv1
->delta
!= const0_rtx
)
1814 /* If there is some extend, it must match signedness of the comparison. */
1819 if (iv0
->extend
== ZERO_EXTEND
1820 || iv1
->extend
== ZERO_EXTEND
)
1827 if (iv0
->extend
== SIGN_EXTEND
1828 || iv1
->extend
== SIGN_EXTEND
)
1834 if (iv0
->extend
!= NIL
1835 && iv1
->extend
!= NIL
1836 && iv0
->extend
!= iv1
->extend
)
1840 if (iv0
->extend
!= NIL
)
1841 signed_p
= iv0
->extend
== SIGN_EXTEND
;
1842 if (iv1
->extend
!= NIL
)
1843 signed_p
= iv1
->extend
== SIGN_EXTEND
;
1850 /* Values of both variables should be computed in the same mode. These
1851 might indeed be different, if we have comparison like
1853 (compare (subreg:SI (iv0)) (subreg:SI (iv1)))
1855 and iv0 and iv1 are both ivs iterating in SI mode, but calculated
1856 in different modes. This does not seem impossible to handle, but
1857 it hardly ever occurs in practice.
1859 The only exception is the case when one of operands is invariant.
1860 For example pentium 3 generates comparisons like
1861 (lt (subreg:HI (reg:SI)) 100). Here we assign HImode to 100, but we
1862 definitely do not want this prevent the optimization. */
1863 comp_mode
= iv0
->extend_mode
;
1864 if (GET_MODE_BITSIZE (comp_mode
) < GET_MODE_BITSIZE (iv1
->extend_mode
))
1865 comp_mode
= iv1
->extend_mode
;
1867 if (iv0
->extend_mode
!= comp_mode
)
1869 if (iv0
->mode
!= iv0
->extend_mode
1870 || iv0
->step
!= const0_rtx
)
1873 iv0
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
1874 comp_mode
, iv0
->base
, iv0
->mode
);
1875 iv0
->extend_mode
= comp_mode
;
1878 if (iv1
->extend_mode
!= comp_mode
)
1880 if (iv1
->mode
!= iv1
->extend_mode
1881 || iv1
->step
!= const0_rtx
)
1884 iv1
->base
= simplify_gen_unary (signed_p
? SIGN_EXTEND
: ZERO_EXTEND
,
1885 comp_mode
, iv1
->base
, iv1
->mode
);
1886 iv1
->extend_mode
= comp_mode
;
1889 /* Check that both ivs belong to a range of a single mode. If one of the
1890 operands is an invariant, we may need to shorten it into the common
1892 if (iv0
->mode
== iv0
->extend_mode
1893 && iv0
->step
== const0_rtx
1894 && iv0
->mode
!= iv1
->mode
)
1895 shorten_into_mode (iv0
, iv1
->mode
, cond
, signed_p
, desc
);
1897 if (iv1
->mode
== iv1
->extend_mode
1898 && iv1
->step
== const0_rtx
1899 && iv0
->mode
!= iv1
->mode
)
1900 shorten_into_mode (iv1
, iv0
->mode
, swap_condition (cond
), signed_p
, desc
);
1902 if (iv0
->mode
!= iv1
->mode
)
1905 desc
->mode
= iv0
->mode
;
1906 desc
->signed_p
= signed_p
;
1911 /* Computes number of iterations of the CONDITION in INSN in LOOP and stores
1912 the result into DESC. Very similar to determine_number_of_iterations
1913 (basically its rtl version), complicated by things like subregs. */
1916 iv_number_of_iterations (struct loop
*loop
, rtx insn
, rtx condition
,
1917 struct niter_desc
*desc
)
1919 rtx op0
, op1
, delta
, step
, bound
, may_xform
, def_insn
, tmp
, tmp0
, tmp1
;
1920 struct rtx_iv iv0
, iv1
, tmp_iv
;
1921 rtx assumption
, may_not_xform
;
1923 enum machine_mode mode
, comp_mode
;
1924 rtx mmin
, mmax
, mode_mmin
, mode_mmax
;
1925 unsigned HOST_WIDEST_INT s
, size
, d
, inv
;
1926 HOST_WIDEST_INT up
, down
, inc
;
1927 int was_sharp
= false;
1929 /* The meaning of these assumptions is this:
1931 then the rest of information does not have to be valid
1932 if noloop_assumptions then the loop does not roll
1933 if infinite then this exit is never used */
1935 desc
->assumptions
= NULL_RTX
;
1936 desc
->noloop_assumptions
= NULL_RTX
;
1937 desc
->infinite
= NULL_RTX
;
1938 desc
->simple_p
= true;
1940 desc
->const_iter
= false;
1941 desc
->niter_expr
= NULL_RTX
;
1942 desc
->niter_max
= 0;
1944 cond
= GET_CODE (condition
);
1945 if (!COMPARISON_P (condition
))
1948 mode
= GET_MODE (XEXP (condition
, 0));
1949 if (mode
== VOIDmode
)
1950 mode
= GET_MODE (XEXP (condition
, 1));
1951 /* The constant comparisons should be folded. */
1952 if (mode
== VOIDmode
)
1955 /* We only handle integers or pointers. */
1956 if (GET_MODE_CLASS (mode
) != MODE_INT
1957 && GET_MODE_CLASS (mode
) != MODE_PARTIAL_INT
)
1960 op0
= XEXP (condition
, 0);
1961 def_insn
= iv_get_reaching_def (insn
, op0
);
1962 if (!iv_analyze (def_insn
, op0
, &iv0
))
1964 if (iv0
.extend_mode
== VOIDmode
)
1965 iv0
.mode
= iv0
.extend_mode
= mode
;
1967 op1
= XEXP (condition
, 1);
1968 def_insn
= iv_get_reaching_def (insn
, op1
);
1969 if (!iv_analyze (def_insn
, op1
, &iv1
))
1971 if (iv1
.extend_mode
== VOIDmode
)
1972 iv1
.mode
= iv1
.extend_mode
= mode
;
1974 if (GET_MODE_BITSIZE (iv0
.extend_mode
) > HOST_BITS_PER_WIDE_INT
1975 || GET_MODE_BITSIZE (iv1
.extend_mode
) > HOST_BITS_PER_WIDE_INT
)
1978 /* Check condition and normalize it. */
1986 tmp_iv
= iv0
; iv0
= iv1
; iv1
= tmp_iv
;
1987 cond
= swap_condition (cond
);
1999 /* Handle extends. This is relatively nontrivial, so we only try in some
2000 easy cases, when we can canonicalize the ivs (possibly by adding some
2001 assumptions) to shape subreg (base + i * step). This function also fills
2002 in desc->mode and desc->signed_p. */
2004 if (!canonicalize_iv_subregs (&iv0
, &iv1
, cond
, desc
))
2007 comp_mode
= iv0
.extend_mode
;
2009 size
= GET_MODE_BITSIZE (mode
);
2010 get_mode_bounds (mode
, (cond
== LE
|| cond
== LT
), comp_mode
, &mmin
, &mmax
);
2011 mode_mmin
= lowpart_subreg (mode
, mmin
, comp_mode
);
2012 mode_mmax
= lowpart_subreg (mode
, mmax
, comp_mode
);
2014 if (GET_CODE (iv0
.step
) != CONST_INT
|| GET_CODE (iv1
.step
) != CONST_INT
)
2017 /* We can take care of the case of two induction variables chasing each other
2018 if the test is NE. I have never seen a loop using it, but still it is
2020 if (iv0
.step
!= const0_rtx
&& iv1
.step
!= const0_rtx
)
2025 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2026 iv1
.step
= const0_rtx
;
2029 /* This is either infinite loop or the one that ends immediately, depending
2030 on initial values. Unswitching should remove this kind of conditions. */
2031 if (iv0
.step
== const0_rtx
&& iv1
.step
== const0_rtx
)
2034 /* Ignore loops of while (i-- < 10) type. */
2036 && (INTVAL (iv0
.step
) < 0 || INTVAL (iv1
.step
) > 0))
2039 /* Some more condition normalization. We must record some assumptions
2040 due to overflows. */
2045 /* We want to take care only of non-sharp relationals; this is easy,
2046 as in cases the overflow would make the transformation unsafe
2047 the loop does not roll. Seemingly it would make more sense to want
2048 to take care of sharp relationals instead, as NE is more similar to
2049 them, but the problem is that here the transformation would be more
2050 difficult due to possibly infinite loops. */
2051 if (iv0
.step
== const0_rtx
)
2053 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2054 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2056 if (assumption
== const_true_rtx
)
2058 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2059 iv0
.base
, const1_rtx
);
2063 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2064 assumption
= simplify_gen_relational (EQ
, SImode
, mode
, tmp
,
2066 if (assumption
== const_true_rtx
)
2068 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
,
2069 iv1
.base
, constm1_rtx
);
2072 if (assumption
!= const0_rtx
)
2073 desc
->noloop_assumptions
=
2074 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2075 cond
= (cond
== LT
) ? LE
: LEU
;
2077 /* It will be useful to be able to tell the difference once more in
2078 LE -> NE reduction. */
2084 /* Take care of trivially infinite loops. */
2087 if (iv0
.step
== const0_rtx
)
2089 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2090 if (rtx_equal_p (tmp
, mode_mmin
))
2093 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2099 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2100 if (rtx_equal_p (tmp
, mode_mmax
))
2103 alloc_EXPR_LIST (0, const_true_rtx
, NULL_RTX
);
2109 /* If we can we want to take care of NE conditions instead of size
2110 comparisons, as they are much more friendly (most importantly
2111 this takes care of special handling of loops with step 1). We can
2112 do it if we first check that upper bound is greater or equal to
2113 lower bound, their difference is constant c modulo step and that
2114 there is not an overflow. */
2117 if (iv0
.step
== const0_rtx
)
2118 step
= simplify_gen_unary (NEG
, comp_mode
, iv1
.step
, comp_mode
);
2121 delta
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2122 delta
= lowpart_subreg (mode
, delta
, comp_mode
);
2123 delta
= simplify_gen_binary (UMOD
, mode
, delta
, step
);
2124 may_xform
= const0_rtx
;
2125 may_not_xform
= const_true_rtx
;
2127 if (GET_CODE (delta
) == CONST_INT
)
2129 if (was_sharp
&& INTVAL (delta
) == INTVAL (step
) - 1)
2131 /* A special case. We have transformed condition of type
2132 for (i = 0; i < 4; i += 4)
2134 for (i = 0; i <= 3; i += 4)
2135 obviously if the test for overflow during that transformation
2136 passed, we cannot overflow here. Most importantly any
2137 loop with sharp end condition and step 1 falls into this
2138 category, so handling this case specially is definitely
2139 worth the troubles. */
2140 may_xform
= const_true_rtx
;
2142 else if (iv0
.step
== const0_rtx
)
2144 bound
= simplify_gen_binary (PLUS
, comp_mode
, mmin
, step
);
2145 bound
= simplify_gen_binary (MINUS
, comp_mode
, bound
, delta
);
2146 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2147 tmp
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2148 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2150 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2156 bound
= simplify_gen_binary (MINUS
, comp_mode
, mmax
, step
);
2157 bound
= simplify_gen_binary (PLUS
, comp_mode
, bound
, delta
);
2158 bound
= lowpart_subreg (mode
, bound
, comp_mode
);
2159 tmp
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2160 may_xform
= simplify_gen_relational (cond
, SImode
, mode
,
2162 may_not_xform
= simplify_gen_relational (reverse_condition (cond
),
2168 if (may_xform
!= const0_rtx
)
2170 /* We perform the transformation always provided that it is not
2171 completely senseless. This is OK, as we would need this assumption
2172 to determine the number of iterations anyway. */
2173 if (may_xform
!= const_true_rtx
)
2175 /* If the step is a power of two and the final value we have
2176 computed overflows, the cycle is infinite. Otherwise it
2177 is nontrivial to compute the number of iterations. */
2179 if ((s
& (s
- 1)) == 0)
2180 desc
->infinite
= alloc_EXPR_LIST (0, may_not_xform
,
2183 desc
->assumptions
= alloc_EXPR_LIST (0, may_xform
,
2187 /* We are going to lose some information about upper bound on
2188 number of iterations in this step, so record the information
2190 inc
= INTVAL (iv0
.step
) - INTVAL (iv1
.step
);
2191 if (GET_CODE (iv1
.base
) == CONST_INT
)
2192 up
= INTVAL (iv1
.base
);
2194 up
= INTVAL (mode_mmax
) - inc
;
2195 down
= INTVAL (GET_CODE (iv0
.base
) == CONST_INT
2198 desc
->niter_max
= (up
- down
) / inc
+ 1;
2200 if (iv0
.step
== const0_rtx
)
2202 iv0
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, delta
);
2203 iv0
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.base
, step
);
2207 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, delta
);
2208 iv1
.base
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, step
);
2211 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2212 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2213 assumption
= simplify_gen_relational (reverse_condition (cond
),
2214 SImode
, mode
, tmp0
, tmp1
);
2215 if (assumption
== const_true_rtx
)
2217 else if (assumption
!= const0_rtx
)
2218 desc
->noloop_assumptions
=
2219 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2224 /* Count the number of iterations. */
2227 /* Everything we do here is just arithmetics modulo size of mode. This
2228 makes us able to do more involved computations of number of iterations
2229 than in other cases. First transform the condition into shape
2230 s * i <> c, with s positive. */
2231 iv1
.base
= simplify_gen_binary (MINUS
, comp_mode
, iv1
.base
, iv0
.base
);
2232 iv0
.base
= const0_rtx
;
2233 iv0
.step
= simplify_gen_binary (MINUS
, comp_mode
, iv0
.step
, iv1
.step
);
2234 iv1
.step
= const0_rtx
;
2235 if (INTVAL (iv0
.step
) < 0)
2237 iv0
.step
= simplify_gen_unary (NEG
, comp_mode
, iv0
.step
, mode
);
2238 iv1
.base
= simplify_gen_unary (NEG
, comp_mode
, iv1
.base
, mode
);
2240 iv0
.step
= lowpart_subreg (mode
, iv0
.step
, comp_mode
);
2242 /* Let nsd (s, size of mode) = d. If d does not divide c, the loop
2243 is infinite. Otherwise, the number of iterations is
2244 (inverse(s/d) * (c/d)) mod (size of mode/d). */
2245 s
= INTVAL (iv0
.step
); d
= 1;
2252 bound
= GEN_INT (((unsigned HOST_WIDEST_INT
) 1 << (size
- 1 ) << 1) - 1);
2254 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2255 tmp
= simplify_gen_binary (UMOD
, mode
, tmp1
, GEN_INT (d
));
2256 assumption
= simplify_gen_relational (NE
, SImode
, mode
, tmp
, const0_rtx
);
2257 desc
->infinite
= alloc_EXPR_LIST (0, assumption
, desc
->infinite
);
2259 tmp
= simplify_gen_binary (UDIV
, mode
, tmp1
, GEN_INT (d
));
2260 inv
= inverse (s
, size
);
2261 inv
= trunc_int_for_mode (inv
, mode
);
2262 tmp
= simplify_gen_binary (MULT
, mode
, tmp
, GEN_INT (inv
));
2263 desc
->niter_expr
= simplify_gen_binary (AND
, mode
, tmp
, bound
);
2267 if (iv1
.step
== const0_rtx
)
2268 /* Condition in shape a + s * i <= b
2269 We must know that b + s does not overflow and a <= b + s and then we
2270 can compute number of iterations as (b + s - a) / s. (It might
2271 seem that we in fact could be more clever about testing the b + s
2272 overflow condition using some information about b - a mod s,
2273 but it was already taken into account during LE -> NE transform). */
2276 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2277 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2279 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmax
,
2280 lowpart_subreg (mode
, step
, comp_mode
));
2281 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2284 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2286 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv1
.base
, iv0
.step
);
2287 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2288 assumption
= simplify_gen_relational (reverse_condition (cond
),
2289 SImode
, mode
, tmp0
, tmp
);
2291 delta
= simplify_gen_binary (PLUS
, mode
, tmp1
, step
);
2292 delta
= simplify_gen_binary (MINUS
, mode
, delta
, tmp0
);
2296 /* Condition in shape a <= b - s * i
2297 We must know that a - s does not overflow and a - s <= b and then
2298 we can again compute number of iterations as (b - (a - s)) / s. */
2299 step
= simplify_gen_unary (NEG
, mode
, iv1
.step
, mode
);
2300 tmp0
= lowpart_subreg (mode
, iv0
.base
, comp_mode
);
2301 tmp1
= lowpart_subreg (mode
, iv1
.base
, comp_mode
);
2303 bound
= simplify_gen_binary (MINUS
, mode
, mode_mmin
,
2304 lowpart_subreg (mode
, step
, comp_mode
));
2305 assumption
= simplify_gen_relational (cond
, SImode
, mode
,
2308 alloc_EXPR_LIST (0, assumption
, desc
->assumptions
);
2310 tmp
= simplify_gen_binary (PLUS
, comp_mode
, iv0
.base
, iv1
.step
);
2311 tmp
= lowpart_subreg (mode
, tmp
, comp_mode
);
2312 assumption
= simplify_gen_relational (reverse_condition (cond
),
2315 delta
= simplify_gen_binary (MINUS
, mode
, tmp0
, step
);
2316 delta
= simplify_gen_binary (MINUS
, mode
, tmp1
, delta
);
2318 if (assumption
== const_true_rtx
)
2320 else if (assumption
!= const0_rtx
)
2321 desc
->noloop_assumptions
=
2322 alloc_EXPR_LIST (0, assumption
, desc
->noloop_assumptions
);
2323 delta
= simplify_gen_binary (UDIV
, mode
, delta
, step
);
2324 desc
->niter_expr
= delta
;
2327 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2328 if (desc
->assumptions
2329 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2331 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2332 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2333 simplify_using_initial_values (loop
, NIL
, &desc
->niter_expr
);
2335 /* Rerun the simplification. Consider code (created by copying loop headers)
2347 The first pass determines that i = 0, the second pass uses it to eliminate
2348 noloop assumption. */
2350 simplify_using_initial_values (loop
, AND
, &desc
->assumptions
);
2351 if (desc
->assumptions
2352 && XEXP (desc
->assumptions
, 0) == const0_rtx
)
2354 simplify_using_initial_values (loop
, IOR
, &desc
->noloop_assumptions
);
2355 simplify_using_initial_values (loop
, IOR
, &desc
->infinite
);
2356 simplify_using_initial_values (loop
, NIL
, &desc
->niter_expr
);
2358 if (desc
->noloop_assumptions
2359 && XEXP (desc
->noloop_assumptions
, 0) == const_true_rtx
)
2362 if (GET_CODE (desc
->niter_expr
) == CONST_INT
)
2364 unsigned HOST_WIDEST_INT val
= INTVAL (desc
->niter_expr
);
2366 desc
->const_iter
= true;
2367 desc
->niter_max
= desc
->niter
= val
& GET_MODE_MASK (desc
->mode
);
2369 else if (!desc
->niter_max
)
2370 desc
->niter_max
= determine_max_iter (desc
);
2375 desc
->simple_p
= false;
2379 desc
->const_iter
= true;
2381 desc
->niter_max
= 0;
2382 desc
->niter_expr
= const0_rtx
;
2386 /* Checks whether E is a simple exit from LOOP and stores its description
2390 check_simple_exit (struct loop
*loop
, edge e
, struct niter_desc
*desc
)
2392 basic_block exit_bb
;
2397 desc
->simple_p
= false;
2399 /* It must belong directly to the loop. */
2400 if (exit_bb
->loop_father
!= loop
)
2403 /* It must be tested (at least) once during any iteration. */
2404 if (!dominated_by_p (CDI_DOMINATORS
, loop
->latch
, exit_bb
))
2407 /* It must end in a simple conditional jump. */
2408 if (!any_condjump_p (BB_END (exit_bb
)))
2418 /* Test whether the condition is suitable. */
2419 if (!(condition
= get_condition (BB_END (ei
->src
), &at
, false)))
2422 if (ei
->flags
& EDGE_FALLTHRU
)
2424 condition
= reversed_condition (condition
);
2429 /* Check that we are able to determine number of iterations and fill
2430 in information about it. */
2431 iv_number_of_iterations (loop
, at
, condition
, desc
);
2434 /* Finds a simple exit of LOOP and stores its description into DESC. */
2437 find_simple_exit (struct loop
*loop
, struct niter_desc
*desc
)
2442 struct niter_desc act
;
2445 desc
->simple_p
= false;
2446 body
= get_loop_body (loop
);
2448 for (i
= 0; i
< loop
->num_nodes
; i
++)
2450 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
2452 if (flow_bb_inside_loop_p (loop
, e
->dest
))
2455 check_simple_exit (loop
, e
, &act
);
2459 /* Prefer constant iterations; the less the better. */
2462 else if (!act
.const_iter
2463 || (desc
->const_iter
&& act
.niter
>= desc
->niter
))
2473 fprintf (dump_file
, "Loop %d is simple:\n", loop
->num
);
2474 fprintf (dump_file
, " simple exit %d -> %d\n",
2475 desc
->out_edge
->src
->index
,
2476 desc
->out_edge
->dest
->index
);
2477 if (desc
->assumptions
)
2479 fprintf (dump_file
, " assumptions: ");
2480 print_rtl (dump_file
, desc
->assumptions
);
2481 fprintf (dump_file
, "\n");
2483 if (desc
->noloop_assumptions
)
2485 fprintf (dump_file
, " does not roll if: ");
2486 print_rtl (dump_file
, desc
->noloop_assumptions
);
2487 fprintf (dump_file
, "\n");
2491 fprintf (dump_file
, " infinite if: ");
2492 print_rtl (dump_file
, desc
->infinite
);
2493 fprintf (dump_file
, "\n");
2496 fprintf (dump_file
, " number of iterations: ");
2497 print_rtl (dump_file
, desc
->niter_expr
);
2498 fprintf (dump_file
, "\n");
2500 fprintf (dump_file
, " upper bound: ");
2501 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter_max
);
2502 fprintf (dump_file
, "\n");
2505 fprintf (dump_file
, "Loop %d is not simple.\n", loop
->num
);
2511 /* Creates a simple loop description of LOOP if it was not computed
2515 get_simple_loop_desc (struct loop
*loop
)
2517 struct niter_desc
*desc
= simple_loop_desc (loop
);
2522 desc
= xmalloc (sizeof (struct niter_desc
));
2523 iv_analysis_loop_init (loop
);
2524 find_simple_exit (loop
, desc
);
2530 /* Releases simple loop description for LOOP. */
2533 free_simple_loop_desc (struct loop
*loop
)
2535 struct niter_desc
*desc
= simple_loop_desc (loop
);