* config/h8300/h8300.md (pushqi_h8300): Don't push the stack
[official-gcc.git] / gcc / doloop.c
blob4f9eaff9cc20d5cae23661d3f087b7c2e811c1a7
1 /* Perform doloop optimizations
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "flags.h"
28 #include "expr.h"
29 #include "loop.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "toplev.h"
33 #include "tm_p.h"
36 /* This module is used to modify loops with a determinable number of
37 iterations to use special low-overhead looping instructions.
39 It first validates whether the loop is well behaved and has a
40 determinable number of iterations (either at compile or run-time).
41 It then modifies the loop to use a low-overhead looping pattern as
42 follows:
44 1. A pseudo register is allocated as the loop iteration counter.
46 2. The number of loop iterations is calculated and is stored
47 in the loop counter.
49 3. At the end of the loop, the jump insn is replaced by the
50 doloop_end pattern. The compare must remain because it might be
51 used elsewhere. If the loop-variable or condition register are
52 used elsewhere, they will be eliminated by flow.
54 4. An optional doloop_begin pattern is inserted at the top of the
55 loop.
59 #ifdef HAVE_doloop_end
61 static rtx doloop_condition_get
62 PARAMS ((rtx));
63 static unsigned HOST_WIDE_INT doloop_iterations_max
64 PARAMS ((const struct loop_info *, enum machine_mode, int));
65 static int doloop_valid_p
66 PARAMS ((const struct loop *, rtx));
67 static int doloop_modify
68 PARAMS ((const struct loop *, rtx, rtx, rtx, rtx, rtx));
69 static int doloop_modify_runtime
70 PARAMS ((const struct loop *, rtx, rtx, rtx, enum machine_mode, rtx));
73 /* Return the loop termination condition for PATTERN or zero
74 if it is not a decrement and branch jump insn. */
75 static rtx
76 doloop_condition_get (pattern)
77 rtx pattern;
79 rtx cmp;
80 rtx inc;
81 rtx reg;
82 rtx condition;
84 /* The canonical doloop pattern we expect is:
86 (parallel [(set (pc) (if_then_else (condition)
87 (label_ref (label))
88 (pc)))
89 (set (reg) (plus (reg) (const_int -1)))
90 (additional clobbers and uses)])
92 Some machines (IA-64) make the decrement conditional on
93 the condition as well, so we don't bother verifying the
94 actual decrement. In summary, the branch must be the
95 first entry of the parallel (also required by jump.c),
96 and the second entry of the parallel must be a set of
97 the loop counter register. */
99 if (GET_CODE (pattern) != PARALLEL)
100 return 0;
102 cmp = XVECEXP (pattern, 0, 0);
103 inc = XVECEXP (pattern, 0, 1);
105 /* Check for (set (reg) (something)). */
106 if (GET_CODE (inc) != SET || ! REG_P (SET_DEST (inc)))
107 return 0;
109 /* Extract loop counter register. */
110 reg = SET_DEST (inc);
112 /* Check for (set (pc) (if_then_else (condition)
113 (label_ref (label))
114 (pc))). */
115 if (GET_CODE (cmp) != SET
116 || SET_DEST (cmp) != pc_rtx
117 || GET_CODE (SET_SRC (cmp)) != IF_THEN_ELSE
118 || GET_CODE (XEXP (SET_SRC (cmp), 1)) != LABEL_REF
119 || XEXP (SET_SRC (cmp), 2) != pc_rtx)
120 return 0;
122 /* Extract loop termination condition. */
123 condition = XEXP (SET_SRC (cmp), 0);
125 if ((GET_CODE (condition) != GE && GET_CODE (condition) != NE)
126 || GET_CODE (XEXP (condition, 1)) != CONST_INT)
127 return 0;
129 if (XEXP (condition, 0) == reg)
130 return condition;
132 if (GET_CODE (XEXP (condition, 0)) == PLUS
133 && XEXP (XEXP (condition, 0), 0) == reg)
134 return condition;
136 /* ??? If a machine uses a funny comparison, we could return a
137 canonicalised form here. */
139 return 0;
143 /* Return an estimate of the maximum number of loop iterations for the
144 loop specified by LOOP or zero if the loop is not normal.
145 MODE is the mode of the iteration count and NONNEG is nonzero if
146 the iteration count has been proved to be non-negative. */
147 static unsigned HOST_WIDE_INT
148 doloop_iterations_max (loop_info, mode, nonneg)
149 const struct loop_info *loop_info;
150 enum machine_mode mode;
151 int nonneg;
153 unsigned HOST_WIDE_INT n_iterations_max;
154 enum rtx_code code;
155 rtx min_value;
156 rtx max_value;
157 HOST_WIDE_INT abs_inc;
158 int neg_inc;
160 neg_inc = 0;
161 abs_inc = INTVAL (loop_info->increment);
162 if (abs_inc < 0)
164 abs_inc = -abs_inc;
165 neg_inc = 1;
168 if (neg_inc)
170 code = swap_condition (loop_info->comparison_code);
171 min_value = loop_info->final_equiv_value;
172 max_value = loop_info->initial_equiv_value;
174 else
176 code = loop_info->comparison_code;
177 min_value = loop_info->initial_equiv_value;
178 max_value = loop_info->final_equiv_value;
181 /* Since the loop has a VTOP, we know that the initial test will be
182 true and thus the value of max_value should be greater than the
183 value of min_value. Thus the difference should always be positive
184 and the code must be LT, LE, LTU, LEU, or NE. Otherwise the loop is
185 not normal, e.g., `for (i = 0; i < 10; i--)'. */
186 switch (code)
188 case LTU:
189 case LEU:
191 unsigned HOST_WIDE_INT umax;
192 unsigned HOST_WIDE_INT umin;
194 if (GET_CODE (min_value) == CONST_INT)
195 umin = INTVAL (min_value);
196 else
197 umin = 0;
199 if (GET_CODE (max_value) == CONST_INT)
200 umax = INTVAL (max_value);
201 else
202 umax = ((unsigned) 2 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
204 n_iterations_max = umax - umin;
205 break;
208 case LT:
209 case LE:
211 HOST_WIDE_INT smax;
212 HOST_WIDE_INT smin;
214 if (GET_CODE (min_value) == CONST_INT)
215 smin = INTVAL (min_value);
216 else
217 smin = -((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1));
219 if (GET_CODE (max_value) == CONST_INT)
220 smax = INTVAL (max_value);
221 else
222 smax = ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
224 n_iterations_max = smax - smin;
225 break;
228 case NE:
229 if (GET_CODE (min_value) == CONST_INT
230 && GET_CODE (max_value) == CONST_INT)
231 n_iterations_max = INTVAL (max_value) - INTVAL (min_value);
232 else
233 /* We need to conservatively assume that we might have the maximum
234 number of iterations without any additional knowledge. */
235 n_iterations_max = ((unsigned) 2 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
236 break;
238 default:
239 return 0;
242 n_iterations_max /= abs_inc;
244 /* If we know that the iteration count is non-negative then adjust
245 n_iterations_max if it is so large that it appears negative. */
246 if (nonneg
247 && n_iterations_max > ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)))
248 n_iterations_max = ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
250 return n_iterations_max;
254 /* Return nonzero if the loop specified by LOOP is suitable for
255 the use of special low-overhead looping instructions. */
256 static int
257 doloop_valid_p (loop, jump_insn)
258 const struct loop *loop;
259 rtx jump_insn;
261 const struct loop_info *loop_info = LOOP_INFO (loop);
263 /* The loop must have a conditional jump at the end. */
264 if (! any_condjump_p (jump_insn)
265 || ! onlyjump_p (jump_insn))
267 if (loop_dump_stream)
268 fprintf (loop_dump_stream,
269 "Doloop: Invalid jump at loop end.\n");
270 return 0;
273 /* Give up if a loop has been completely unrolled. */
274 if (loop_info->n_iterations == loop_info->unroll_number)
276 if (loop_dump_stream)
277 fprintf (loop_dump_stream,
278 "Doloop: Loop completely unrolled.\n");
279 return 0;
282 /* The loop must have a single exit target. A break or return
283 statement within a loop will generate multiple loop exits.
284 Another example of a loop that currently generates multiple exit
285 targets is for (i = 0; i < (foo ? 8 : 4); i++) { }. */
286 if (loop_info->has_multiple_exit_targets || loop->exit_count)
288 if (loop_dump_stream)
289 fprintf (loop_dump_stream,
290 "Doloop: Loop has multiple exit targets.\n");
291 return 0;
294 /* An indirect jump may jump out of the loop. */
295 if (loop_info->has_indirect_jump)
297 if (loop_dump_stream)
298 fprintf (loop_dump_stream,
299 "Doloop: Indirect jump in function.\n");
300 return 0;
303 /* A called function may clobber any special registers required for
304 low-overhead looping. */
305 if (loop_info->has_call)
307 if (loop_dump_stream)
308 fprintf (loop_dump_stream,
309 "Doloop: Function call in loop.\n");
310 return 0;
313 /* Some targets (eg, PPC) use the count register for branch on table
314 instructions. ??? This should be a target specific check. */
315 if (loop_info->has_tablejump)
317 if (loop_dump_stream)
318 fprintf (loop_dump_stream,
319 "Doloop: Computed branch in the loop.\n");
320 return 0;
323 if (! loop_info->increment)
325 if (loop_dump_stream)
326 fprintf (loop_dump_stream,
327 "Doloop: Could not determine iteration info.\n");
328 return 0;
331 if (GET_CODE (loop_info->increment) != CONST_INT)
333 if (loop_dump_stream)
334 fprintf (loop_dump_stream,
335 "Doloop: Increment not an integer constant.\n");
336 return 0;
339 /* There is no guarantee that a NE loop will terminate if the
340 absolute increment is not unity. ??? We could compute this
341 condition at run-time and have an additional jump around the loop
342 to ensure an infinite loop. */
343 if (loop_info->comparison_code == NE
344 && !loop_info->preconditioned
345 && INTVAL (loop_info->increment) != -1
346 && INTVAL (loop_info->increment) != 1)
348 if (loop_dump_stream)
349 fprintf (loop_dump_stream,
350 "Doloop: NE loop with non-unity increment.\n");
351 return 0;
354 /* Check for loops that may not terminate under special conditions. */
355 if (! loop_info->n_iterations
356 && ((loop_info->comparison_code == LEU
357 && INTVAL (loop_info->increment) > 0)
358 || (loop_info->comparison_code == GEU
359 && INTVAL (loop_info->increment) < 0)
360 || (loop_info->comparison_code == LTU
361 && INTVAL (loop_info->increment) > 1)
362 || (loop_info->comparison_code == GTU
363 && INTVAL (loop_info->increment) < -1)))
365 /* If the comparison is LEU and the comparison value is UINT_MAX
366 then the loop will not terminate. Similarly, if the
367 comparison code is GEU and the comparison value is 0, the
368 loop will not terminate.
370 If the absolute increment is not 1, the loop can be infinite
371 even with LTU/GTU, e.g. for (i = 3; i > 0; i -= 2)
373 Note that with LE and GE, the loop behavior is undefined
374 (C++ standard section 5 clause 5) if an overflow occurs, say
375 between INT_MAX and INT_MAX + 1. We thus don't have to worry
376 about these two cases.
378 ??? We could compute these conditions at run-time and have a
379 additional jump around the loop to ensure an infinite loop.
380 However, it is very unlikely that this is the intended
381 behavior of the loop and checking for these rare boundary
382 conditions would pessimize all other code.
384 If the loop is executed only a few times an extra check to
385 restart the loop could use up most of the benefits of using a
386 count register loop. Note however, that normally, this
387 restart branch would never execute, so it could be predicted
388 well by the CPU. We should generate the pessimistic code by
389 default, and have an option, e.g. -funsafe-loops that would
390 enable count-register loops in this case. */
391 if (loop_dump_stream)
392 fprintf (loop_dump_stream,
393 "Doloop: Possible infinite iteration case ignored.\n");
396 return 1;
400 /* Modify the loop to use the low-overhead looping insn where LOOP
401 describes the loop, ITERATIONS is an RTX containing the desired
402 number of loop iterations, ITERATIONS_MAX is a CONST_INT specifying
403 the maximum number of loop iterations, and DOLOOP_INSN is the
404 low-overhead looping insn to emit at the end of the loop. This
405 returns nonzero if it was successful. */
406 static int
407 doloop_modify (loop, iterations, iterations_max,
408 doloop_seq, start_label, condition)
409 const struct loop *loop;
410 rtx iterations;
411 rtx iterations_max;
412 rtx doloop_seq;
413 rtx start_label;
414 rtx condition;
416 rtx counter_reg;
417 rtx count;
418 rtx sequence;
419 rtx jump_insn;
420 int nonneg = 0;
421 int decrement_count;
423 jump_insn = prev_nonnote_insn (loop->end);
425 if (loop_dump_stream)
427 fprintf (loop_dump_stream, "Doloop: Inserting doloop pattern (");
428 if (GET_CODE (iterations) == CONST_INT)
429 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
430 INTVAL (iterations));
431 else
432 fputs ("runtime", loop_dump_stream);
433 fputs (" iterations).", loop_dump_stream);
436 /* Emit the label that will delimit the top of the loop.
437 This has to be done before the delete_insn call below, to prevent
438 delete_insn from deleting too much. */
439 emit_label_after (start_label, loop->top ? loop->top : loop->start);
440 LABEL_NUSES (start_label)++;
442 /* Discard original jump to continue loop. The original compare
443 result may still be live, so it cannot be discarded explicitly. */
444 delete_related_insns (jump_insn);
446 counter_reg = XEXP (condition, 0);
447 if (GET_CODE (counter_reg) == PLUS)
448 counter_reg = XEXP (counter_reg, 0);
450 start_sequence ();
452 count = iterations;
453 decrement_count = 0;
454 switch (GET_CODE (condition))
456 case NE:
457 /* Currently only NE tests against zero and one are supported. */
458 if (XEXP (condition, 1) == const0_rtx)
459 decrement_count = 1;
460 else if (XEXP (condition, 1) != const1_rtx)
461 abort ();
462 break;
464 case GE:
465 /* Currently only GE tests against zero are supported. */
466 if (XEXP (condition, 1) != const0_rtx)
467 abort ();
469 /* The iteration count needs decrementing for a GE test. */
470 decrement_count = 1;
472 /* Determine if the iteration counter will be non-negative.
473 Note that the maximum value loaded is iterations_max - 1. */
474 if ((unsigned HOST_WIDE_INT) INTVAL (iterations_max)
475 <= ((unsigned) 1 << (GET_MODE_BITSIZE (GET_MODE (counter_reg)) - 1)))
476 nonneg = 1;
477 break;
479 /* Abort if an invalid doloop pattern has been generated. */
480 default:
481 abort ();
484 if (decrement_count)
486 if (GET_CODE (count) == CONST_INT)
487 count = GEN_INT (INTVAL (count) - 1);
488 else
489 count = expand_simple_binop (GET_MODE (counter_reg), MINUS,
490 count, GEN_INT (1),
491 0, 0, OPTAB_LIB_WIDEN);
494 /* Insert initialization of the count register into the loop header. */
495 convert_move (counter_reg, count, 1);
496 sequence = get_insns ();
497 end_sequence ();
498 emit_insn_before (sequence, loop->start);
500 /* Some targets (eg, C4x) need to initialize special looping
501 registers. */
502 #ifdef HAVE_doloop_begin
504 rtx init;
506 init = gen_doloop_begin (counter_reg,
507 GET_CODE (iterations) == CONST_INT
508 ? iterations : const0_rtx, iterations_max,
509 GEN_INT (loop->level));
510 if (init)
512 start_sequence ();
513 emit_insn (init);
514 sequence = get_insns ();
515 end_sequence ();
516 emit_insn_after (sequence, loop->start);
519 #endif
521 /* Insert the new low-overhead looping insn. */
522 emit_jump_insn_before (doloop_seq, loop->end);
523 jump_insn = prev_nonnote_insn (loop->end);
524 JUMP_LABEL (jump_insn) = start_label;
526 /* Add a REG_NONNEG note if the actual or estimated maximum number
527 of iterations is non-negative. */
528 if (nonneg)
530 REG_NOTES (jump_insn)
531 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX, REG_NOTES (jump_insn));
533 return 1;
537 /* Handle the more complex case, where the bounds are not known at
538 compile time. In this case we generate a run_time calculation of
539 the number of iterations. We rely on the existence of a run-time
540 guard to ensure that the loop executes at least once, i.e.,
541 initial_value obeys the loop comparison condition. If a guard is
542 not present, we emit one. The loop to modify is described by LOOP.
543 ITERATIONS_MAX is a CONST_INT specifying the estimated maximum
544 number of loop iterations. DOLOOP_INSN is the low-overhead looping
545 insn to insert. Returns nonzero if loop successfully modified. */
546 static int
547 doloop_modify_runtime (loop, iterations_max,
548 doloop_seq, start_label, mode, condition)
549 const struct loop *loop;
550 rtx iterations_max;
551 rtx doloop_seq;
552 rtx start_label;
553 enum machine_mode mode;
554 rtx condition;
556 const struct loop_info *loop_info = LOOP_INFO (loop);
557 HOST_WIDE_INT abs_inc;
558 HOST_WIDE_INT abs_loop_inc;
559 int neg_inc;
560 rtx diff;
561 rtx sequence;
562 rtx iterations;
563 rtx initial_value;
564 rtx final_value;
565 rtx increment;
566 int unsigned_p;
567 enum rtx_code comparison_code;
569 increment = loop_info->increment;
570 initial_value = loop_info->initial_value;
571 final_value = loop_info->final_value;
573 neg_inc = 0;
574 abs_inc = INTVAL (increment);
575 if (abs_inc < 0)
577 abs_inc = -abs_inc;
578 neg_inc = 1;
581 comparison_code = loop_info->comparison_code;
582 unsigned_p = (comparison_code == LTU
583 || comparison_code == LEU
584 || comparison_code == GTU
585 || comparison_code == GEU
586 || comparison_code == NE);
588 /* The number of iterations (prior to any loop unrolling) is given by:
590 n = (abs (final - initial) + abs_inc - 1) / abs_inc.
592 However, it is possible for the summation to overflow, and a
593 safer method is:
595 n = abs (final - initial) / abs_inc;
596 n += (abs (final - initial) % abs_inc) != 0;
598 But when abs_inc is a power of two, the summation won't overflow
599 except in cases where the loop never terminates. So we don't
600 need to use this more costly calculation.
602 If the loop has been unrolled, the full calculation is
604 t1 = abs_inc * unroll_number; increment per loop
605 n = (abs (final - initial) + abs_inc - 1) / t1; full loops
606 n += (abs (final - initial) + abs_inc - 1) % t1) >= abs_inc;
607 partial loop
608 which works out to be equivalent to
610 n = (abs (final - initial) + t1 - 1) / t1;
612 In the case where the loop was preconditioned, a few iterations
613 may have been executed earlier; but 'initial' was adjusted as they
614 were executed, so we don't need anything special for that case here.
615 As above, when t1 is a power of two we don't need to worry about
616 overflow.
618 The division and modulo operations can be avoided by requiring
619 that the increment is a power of 2 (precondition_loop_p enforces
620 this requirement). Nevertheless, the RTX_COSTS should be checked
621 to see if a fast divmod is available. */
623 start_sequence ();
624 /* abs (final - initial) */
625 diff = expand_simple_binop (mode, MINUS,
626 copy_rtx (neg_inc ? initial_value : final_value),
627 copy_rtx (neg_inc ? final_value : initial_value),
628 NULL_RTX, unsigned_p, OPTAB_LIB_WIDEN);
630 /* Some code transformations can result in code akin to
632 tmp = i + 1;
634 goto scan_start;
635 top:
636 tmp = tmp + 1;
637 scan_start:
638 i = tmp;
639 if (i < n) goto top;
641 We'll have already detected this form of loop in scan_loop,
642 and set loop->top and loop->scan_start appropriately.
644 In this situation, we skip the increment the first time through
645 the loop, which results in an incorrect estimate of the number
646 of iterations. Adjust the difference to compensate. */
647 /* ??? Logically, it would seem this belongs in loop_iterations.
648 However, this causes regressions e.g. on x86 execute/20011008-3.c,
649 so I do not believe we've properly characterized the exact nature
650 of the problem. In the meantime, this fixes execute/20011126-2.c
651 on ia64 and some Ada front end miscompilation on ppc. */
653 if (loop->scan_start)
655 rtx iteration_var = loop_info->iteration_var;
656 struct loop_ivs *ivs = LOOP_IVS (loop);
657 struct iv_class *bl;
659 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
660 bl = REG_IV_CLASS (ivs, REGNO (iteration_var));
661 else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
663 struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
664 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
666 else
667 /* Iteration var must be an induction variable to get here. */
668 abort ();
670 if (INSN_UID (bl->biv->insn) < max_uid_for_loop
671 && INSN_LUID (bl->biv->insn) < INSN_LUID (loop->scan_start))
673 if (loop_dump_stream)
674 fprintf (loop_dump_stream,
675 "Doloop: Basic induction var skips initial incr.\n");
677 diff = expand_simple_binop (mode, PLUS, diff, GEN_INT (abs_inc),
678 diff, unsigned_p, OPTAB_LIB_WIDEN);
682 abs_loop_inc = abs_inc * loop_info->unroll_number;
683 if (abs_loop_inc != 1)
685 int shift_count;
687 shift_count = exact_log2 (abs_loop_inc);
688 if (shift_count < 0)
689 abort ();
691 /* (abs (final - initial) + abs_inc * unroll_number - 1) */
692 diff = expand_simple_binop (GET_MODE (diff), PLUS,
693 diff, GEN_INT (abs_loop_inc - 1),
694 diff, 1, OPTAB_LIB_WIDEN);
696 /* (abs (final - initial) + abs_inc * unroll_number - 1)
697 / (abs_inc * unroll_number) */
698 diff = expand_simple_binop (GET_MODE (diff), LSHIFTRT,
699 diff, GEN_INT (shift_count),
700 diff, 1, OPTAB_LIB_WIDEN);
702 iterations = diff;
704 /* If there is a NOTE_INSN_LOOP_VTOP, we have a `for' or `while'
705 style loop, with a loop exit test at the start. Thus, we can
706 assume that the loop condition was true when the loop was
707 entered.
709 `do-while' loops require special treatment since the exit test is
710 not executed before the start of the loop. We need to determine
711 if the loop will terminate after the first pass and to limit the
712 iteration count to one if necessary. */
713 if (! loop->vtop)
715 if (loop_dump_stream)
716 fprintf (loop_dump_stream, "Doloop: Do-while loop.\n");
718 /* A `do-while' loop must iterate at least once. For code like
719 i = initial; do { ... } while (++i < final);
720 we will calculate a bogus iteration count if initial > final.
721 So detect this and set the iteration count to 1.
722 Note that if the loop has been unrolled, then the loop body
723 is guaranteed to execute at least once. Also, when the
724 comparison is NE, our calculated count will be OK. */
725 if (loop_info->unroll_number == 1 && comparison_code != NE)
727 rtx label;
729 /* Emit insns to test if the loop will immediately
730 terminate and to set the iteration count to 1 if true. */
731 label = gen_label_rtx();
732 emit_cmp_and_jump_insns (copy_rtx (initial_value),
733 copy_rtx (loop_info->comparison_value),
734 comparison_code, NULL_RTX, mode, 0,
735 label);
736 JUMP_LABEL (get_last_insn ()) = label;
737 LABEL_NUSES (label)++;
738 emit_move_insn (iterations, const1_rtx);
739 emit_label (label);
743 sequence = get_insns ();
744 end_sequence ();
745 emit_insn_before (sequence, loop->start);
747 return doloop_modify (loop, iterations, iterations_max, doloop_seq,
748 start_label, condition);
752 /* This is the main entry point. Process loop described by LOOP
753 validating that the loop is suitable for conversion to use a low
754 overhead looping instruction, replacing the jump insn where
755 suitable. We distinguish between loops with compile-time bounds
756 and those with run-time bounds. Information from LOOP is used to
757 compute the number of iterations and to determine whether the loop
758 is a candidate for this optimization. Returns nonzero if loop
759 successfully modified. */
761 doloop_optimize (loop)
762 const struct loop *loop;
764 struct loop_info *loop_info = LOOP_INFO (loop);
765 rtx initial_value;
766 rtx final_value;
767 rtx increment;
768 rtx jump_insn;
769 enum machine_mode mode;
770 unsigned HOST_WIDE_INT n_iterations;
771 unsigned HOST_WIDE_INT n_iterations_max;
772 rtx doloop_seq, doloop_pat, doloop_reg;
773 rtx iterations;
774 rtx iterations_max;
775 rtx start_label;
776 rtx condition;
778 if (loop_dump_stream)
779 fprintf (loop_dump_stream,
780 "Doloop: Processing loop %d, enclosed levels %d.\n",
781 loop->num, loop->level);
783 jump_insn = prev_nonnote_insn (loop->end);
785 /* Check that loop is a candidate for a low-overhead looping insn. */
786 if (! doloop_valid_p (loop, jump_insn))
787 return 0;
789 /* Determine if the loop can be safely, and profitably,
790 preconditioned. While we don't precondition the loop in a loop
791 unrolling sense, this test ensures that the loop is well behaved
792 and that the increment is a constant integer. */
793 if (! precondition_loop_p (loop, &initial_value, &final_value,
794 &increment, &mode))
796 if (loop_dump_stream)
797 fprintf (loop_dump_stream,
798 "Doloop: Cannot precondition loop.\n");
799 return 0;
802 /* Determine or estimate the maximum number of loop iterations. */
803 n_iterations = loop_info->n_iterations;
804 if (n_iterations)
806 /* This is the simple case where the initial and final loop
807 values are constants. */
808 n_iterations_max = n_iterations;
810 else
812 int nonneg = find_reg_note (jump_insn, REG_NONNEG, 0) != 0;
814 /* This is the harder case where the initial and final loop
815 values may not be constants. */
816 n_iterations_max = doloop_iterations_max (loop_info, mode, nonneg);
818 if (! n_iterations_max)
820 /* We have something like `for (i = 0; i < 10; i--)'. */
821 if (loop_dump_stream)
822 fprintf (loop_dump_stream,
823 "Doloop: Not normal loop.\n");
824 return 0;
828 /* Account for loop unrolling in the iteration count. This will
829 have no effect if loop_iterations could not determine the number
830 of iterations. */
831 n_iterations /= loop_info->unroll_number;
832 n_iterations_max /= loop_info->unroll_number;
834 if (n_iterations && n_iterations < 3)
836 if (loop_dump_stream)
837 fprintf (loop_dump_stream,
838 "Doloop: Too few iterations (%ld) to be profitable.\n",
839 (long int) n_iterations);
840 return 0;
843 iterations = GEN_INT (n_iterations);
844 iterations_max = GEN_INT (n_iterations_max);
846 /* Generate looping insn. If the pattern FAILs then give up trying
847 to modify the loop since there is some aspect the back-end does
848 not like. */
849 start_label = gen_label_rtx ();
850 doloop_reg = gen_reg_rtx (mode);
851 doloop_seq = gen_doloop_end (doloop_reg, iterations, iterations_max,
852 GEN_INT (loop->level), start_label);
853 if (! doloop_seq && mode != word_mode)
855 PUT_MODE (doloop_reg, word_mode);
856 doloop_seq = gen_doloop_end (doloop_reg, iterations, iterations_max,
857 GEN_INT (loop->level), start_label);
859 if (! doloop_seq)
861 if (loop_dump_stream)
862 fprintf (loop_dump_stream,
863 "Doloop: Target unwilling to use doloop pattern!\n");
864 return 0;
867 /* If multiple instructions were created, the last must be the
868 jump instruction. Also, a raw define_insn may yield a plain
869 pattern. */
870 doloop_pat = doloop_seq;
871 if (INSN_P (doloop_pat))
873 while (NEXT_INSN (doloop_pat) != NULL_RTX)
874 doloop_pat = NEXT_INSN (doloop_pat);
875 if (GET_CODE (doloop_pat) == JUMP_INSN)
876 doloop_pat = PATTERN (doloop_pat);
877 else
878 doloop_pat = NULL_RTX;
881 if (! doloop_pat
882 || ! (condition = doloop_condition_get (doloop_pat)))
884 if (loop_dump_stream)
885 fprintf (loop_dump_stream,
886 "Doloop: Unrecognizable doloop pattern!\n");
887 return 0;
890 if (n_iterations != 0)
891 /* Handle the simpler case, where we know the iteration count at
892 compile time. */
893 return doloop_modify (loop, iterations, iterations_max, doloop_seq,
894 start_label, condition);
895 else
896 /* Handle the harder case, where we must add additional runtime tests. */
897 return doloop_modify_runtime (loop, iterations_max, doloop_seq,
898 start_label, mode, condition);
901 #endif /* HAVE_doloop_end */