Nuke USB_DO_ATTACH and remove device_t dv, since it is no longer needed.
[dragonfly.git] / contrib / gcc-3.4 / gcc / doloop.c
bloba82fb16a35cb966f2ca5be26cfe573b7ab9ff8da
1 /* Perform doloop optimizations
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Michael P. Hayes (m.hayes@elec.canterbury.ac.nz)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "loop.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "toplev.h"
34 #include "tm_p.h"
35 #include "cfgloop.h"
38 /* This module is used to modify loops with a determinable number of
39 iterations to use special low-overhead looping instructions.
41 It first validates whether the loop is well behaved and has a
42 determinable number of iterations (either at compile or run-time).
43 It then modifies the loop to use a low-overhead looping pattern as
44 follows:
46 1. A pseudo register is allocated as the loop iteration counter.
48 2. The number of loop iterations is calculated and is stored
49 in the loop counter.
51 3. At the end of the loop, the jump insn is replaced by the
52 doloop_end pattern. The compare must remain because it might be
53 used elsewhere. If the loop-variable or condition register are
54 used elsewhere, they will be eliminated by flow.
56 4. An optional doloop_begin pattern is inserted at the top of the
57 loop.
61 #ifdef HAVE_doloop_end
63 static unsigned HOST_WIDE_INT doloop_iterations_max (const struct loop_info *,
64 enum machine_mode, int);
65 static int doloop_valid_p (const struct loop *, rtx);
66 static int doloop_modify (const struct loop *, rtx, rtx, rtx, rtx, rtx);
67 static int doloop_modify_runtime (const struct loop *, rtx, rtx, rtx,
68 enum machine_mode, rtx);
71 /* Return the loop termination condition for PATTERN or zero
72 if it is not a decrement and branch jump insn. */
73 rtx
74 doloop_condition_get (rtx pattern)
76 rtx cmp;
77 rtx inc;
78 rtx reg;
79 rtx condition;
81 /* The canonical doloop pattern we expect is:
83 (parallel [(set (pc) (if_then_else (condition)
84 (label_ref (label))
85 (pc)))
86 (set (reg) (plus (reg) (const_int -1)))
87 (additional clobbers and uses)])
89 Some machines (IA-64) make the decrement conditional on
90 the condition as well, so we don't bother verifying the
91 actual decrement. In summary, the branch must be the
92 first entry of the parallel (also required by jump.c),
93 and the second entry of the parallel must be a set of
94 the loop counter register. */
96 if (GET_CODE (pattern) != PARALLEL)
97 return 0;
99 cmp = XVECEXP (pattern, 0, 0);
100 inc = XVECEXP (pattern, 0, 1);
102 /* Check for (set (reg) (something)). */
103 if (GET_CODE (inc) != SET || ! REG_P (SET_DEST (inc)))
104 return 0;
106 /* Extract loop counter register. */
107 reg = SET_DEST (inc);
109 /* Check for (set (pc) (if_then_else (condition)
110 (label_ref (label))
111 (pc))). */
112 if (GET_CODE (cmp) != SET
113 || SET_DEST (cmp) != pc_rtx
114 || GET_CODE (SET_SRC (cmp)) != IF_THEN_ELSE
115 || GET_CODE (XEXP (SET_SRC (cmp), 1)) != LABEL_REF
116 || XEXP (SET_SRC (cmp), 2) != pc_rtx)
117 return 0;
119 /* Extract loop termination condition. */
120 condition = XEXP (SET_SRC (cmp), 0);
122 if ((GET_CODE (condition) != GE && GET_CODE (condition) != NE)
123 || GET_CODE (XEXP (condition, 1)) != CONST_INT)
124 return 0;
126 if (XEXP (condition, 0) == reg)
127 return condition;
129 if (GET_CODE (XEXP (condition, 0)) == PLUS
130 && XEXP (XEXP (condition, 0), 0) == reg)
131 return condition;
133 /* ??? If a machine uses a funny comparison, we could return a
134 canonicalised form here. */
136 return 0;
140 /* Return an estimate of the maximum number of loop iterations for the
141 loop specified by LOOP or zero if the loop is not normal.
142 MODE is the mode of the iteration count and NONNEG is nonzero if
143 the iteration count has been proved to be non-negative. */
144 static unsigned HOST_WIDE_INT
145 doloop_iterations_max (const struct loop_info *loop_info,
146 enum machine_mode mode, int nonneg)
148 unsigned HOST_WIDE_INT n_iterations_max;
149 enum rtx_code code;
150 rtx min_value;
151 rtx max_value;
152 HOST_WIDE_INT abs_inc;
153 int neg_inc;
155 neg_inc = 0;
156 abs_inc = INTVAL (loop_info->increment);
157 if (abs_inc < 0)
159 abs_inc = -abs_inc;
160 neg_inc = 1;
163 if (neg_inc)
165 code = swap_condition (loop_info->comparison_code);
166 min_value = loop_info->final_equiv_value;
167 max_value = loop_info->initial_equiv_value;
169 else
171 code = loop_info->comparison_code;
172 min_value = loop_info->initial_equiv_value;
173 max_value = loop_info->final_equiv_value;
176 /* Since the loop has a VTOP, we know that the initial test will be
177 true and thus the value of max_value should be greater than the
178 value of min_value. Thus the difference should always be positive
179 and the code must be LT, LE, LTU, LEU, or NE. Otherwise the loop is
180 not normal, e.g., `for (i = 0; i < 10; i--)'. */
181 switch (code)
183 case LTU:
184 case LEU:
186 unsigned HOST_WIDE_INT umax;
187 unsigned HOST_WIDE_INT umin;
189 if (GET_CODE (min_value) == CONST_INT)
190 umin = INTVAL (min_value);
191 else
192 umin = 0;
194 if (GET_CODE (max_value) == CONST_INT)
195 umax = INTVAL (max_value);
196 else
197 umax = ((unsigned) 2 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
199 n_iterations_max = umax - umin;
200 break;
203 case LT:
204 case LE:
206 HOST_WIDE_INT smax;
207 HOST_WIDE_INT smin;
209 if (GET_CODE (min_value) == CONST_INT)
210 smin = INTVAL (min_value);
211 else
212 smin = -((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1));
214 if (GET_CODE (max_value) == CONST_INT)
215 smax = INTVAL (max_value);
216 else
217 smax = ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
219 n_iterations_max = smax - smin;
220 break;
223 case NE:
224 if (GET_CODE (min_value) == CONST_INT
225 && GET_CODE (max_value) == CONST_INT)
226 n_iterations_max = INTVAL (max_value) - INTVAL (min_value);
227 else
228 /* We need to conservatively assume that we might have the maximum
229 number of iterations without any additional knowledge. */
230 n_iterations_max = ((unsigned) 2 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
231 break;
233 default:
234 return 0;
237 n_iterations_max /= abs_inc;
239 /* If we know that the iteration count is non-negative then adjust
240 n_iterations_max if it is so large that it appears negative. */
241 if (nonneg
242 && n_iterations_max > ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)))
243 n_iterations_max = ((unsigned) 1 << (GET_MODE_BITSIZE (mode) - 1)) - 1;
245 return n_iterations_max;
249 /* Return nonzero if the loop specified by LOOP is suitable for
250 the use of special low-overhead looping instructions. */
251 static int
252 doloop_valid_p (const struct loop *loop, rtx jump_insn)
254 const struct loop_info *loop_info = LOOP_INFO (loop);
256 /* The loop must have a conditional jump at the end. */
257 if (! any_condjump_p (jump_insn)
258 || ! onlyjump_p (jump_insn))
260 if (loop_dump_stream)
261 fprintf (loop_dump_stream,
262 "Doloop: Invalid jump at loop end.\n");
263 return 0;
266 /* Give up if a loop has been completely unrolled. */
267 if (loop_info->n_iterations == loop_info->unroll_number)
269 if (loop_dump_stream)
270 fprintf (loop_dump_stream,
271 "Doloop: Loop completely unrolled.\n");
272 return 0;
275 /* The loop must have a single exit target. A break or return
276 statement within a loop will generate multiple loop exits.
277 Another example of a loop that currently generates multiple exit
278 targets is for (i = 0; i < (foo ? 8 : 4); i++) { }. */
279 if (loop_info->has_multiple_exit_targets || loop->exit_count)
281 if (loop_dump_stream)
282 fprintf (loop_dump_stream,
283 "Doloop: Loop has multiple exit targets.\n");
284 return 0;
287 /* An indirect jump may jump out of the loop. */
288 if (loop_info->has_indirect_jump)
290 if (loop_dump_stream)
291 fprintf (loop_dump_stream,
292 "Doloop: Indirect jump in function.\n");
293 return 0;
296 /* A called function may clobber any special registers required for
297 low-overhead looping. */
298 if (loop_info->has_call)
300 if (loop_dump_stream)
301 fprintf (loop_dump_stream,
302 "Doloop: Function call in loop.\n");
303 return 0;
306 /* Some targets (eg, PPC) use the count register for branch on table
307 instructions. ??? This should be a target specific check. */
308 if (loop_info->has_tablejump)
310 if (loop_dump_stream)
311 fprintf (loop_dump_stream,
312 "Doloop: Computed branch in the loop.\n");
313 return 0;
316 if (! loop_info->increment)
318 if (loop_dump_stream)
319 fprintf (loop_dump_stream,
320 "Doloop: Could not determine iteration info.\n");
321 return 0;
324 if (GET_CODE (loop_info->increment) != CONST_INT)
326 if (loop_dump_stream)
327 fprintf (loop_dump_stream,
328 "Doloop: Increment not an integer constant.\n");
329 return 0;
332 /* There is no guarantee that a NE loop will terminate if the
333 absolute increment is not unity. ??? We could compute this
334 condition at run-time and have an additional jump around the loop
335 to ensure an infinite loop. */
336 if (loop_info->comparison_code == NE
337 && !loop_info->preconditioned
338 && INTVAL (loop_info->increment) != -1
339 && INTVAL (loop_info->increment) != 1)
341 if (loop_dump_stream)
342 fprintf (loop_dump_stream,
343 "Doloop: NE loop with non-unity increment.\n");
344 return 0;
347 /* Check for loops that may not terminate under special conditions. */
348 if (! loop_info->n_iterations
349 && ((loop_info->comparison_code == LEU
350 && INTVAL (loop_info->increment) > 0)
351 || (loop_info->comparison_code == GEU
352 && INTVAL (loop_info->increment) < 0)
353 || (loop_info->comparison_code == LTU
354 && INTVAL (loop_info->increment) > 1)
355 || (loop_info->comparison_code == GTU
356 && INTVAL (loop_info->increment) < -1)))
358 /* If the comparison is LEU and the comparison value is UINT_MAX
359 then the loop will not terminate. Similarly, if the
360 comparison code is GEU and the comparison value is 0, the
361 loop will not terminate.
363 If the absolute increment is not 1, the loop can be infinite
364 even with LTU/GTU, e.g. for (i = 3; i > 0; i -= 2)
366 Note that with LE and GE, the loop behavior is undefined
367 (C++ standard section 5 clause 5) if an overflow occurs, say
368 between INT_MAX and INT_MAX + 1. We thus don't have to worry
369 about these two cases.
371 ??? We could compute these conditions at run-time and have a
372 additional jump around the loop to ensure an infinite loop.
373 However, it is very unlikely that this is the intended
374 behavior of the loop and checking for these rare boundary
375 conditions would pessimize all other code.
377 If the loop is executed only a few times an extra check to
378 restart the loop could use up most of the benefits of using a
379 count register loop. Note however, that normally, this
380 restart branch would never execute, so it could be predicted
381 well by the CPU. We should generate the pessimistic code by
382 default, and have an option, e.g. -funsafe-loops that would
383 enable count-register loops in this case. */
384 if (loop_dump_stream)
385 fprintf (loop_dump_stream,
386 "Doloop: Possible infinite iteration case ignored.\n");
389 return 1;
393 /* Modify the loop to use the low-overhead looping insn where LOOP
394 describes the loop, ITERATIONS is an RTX containing the desired
395 number of loop iterations, ITERATIONS_MAX is a CONST_INT specifying
396 the maximum number of loop iterations, and DOLOOP_INSN is the
397 low-overhead looping insn to emit at the end of the loop. This
398 returns nonzero if it was successful. */
399 static int
400 doloop_modify (const struct loop *loop, rtx iterations, rtx iterations_max,
401 rtx doloop_seq, rtx start_label, rtx condition)
403 rtx counter_reg;
404 rtx count;
405 rtx sequence;
406 rtx jump_insn;
407 int nonneg = 0;
408 int decrement_count;
410 jump_insn = prev_nonnote_insn (loop->end);
412 if (loop_dump_stream)
414 fprintf (loop_dump_stream, "Doloop: Inserting doloop pattern (");
415 if (GET_CODE (iterations) == CONST_INT)
416 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
417 INTVAL (iterations));
418 else
419 fputs ("runtime", loop_dump_stream);
420 fputs (" iterations).", loop_dump_stream);
423 /* Emit the label that will delimit the top of the loop.
424 This has to be done before the delete_insn call below, to prevent
425 delete_insn from deleting too much. */
426 emit_label_after (start_label, loop->top ? loop->top : loop->start);
427 LABEL_NUSES (start_label)++;
429 /* Discard original jump to continue loop. The original compare
430 result may still be live, so it cannot be discarded explicitly. */
431 delete_related_insns (jump_insn);
433 counter_reg = XEXP (condition, 0);
434 if (GET_CODE (counter_reg) == PLUS)
435 counter_reg = XEXP (counter_reg, 0);
437 start_sequence ();
439 count = iterations;
440 decrement_count = 0;
441 switch (GET_CODE (condition))
443 case NE:
444 /* Currently only NE tests against zero and one are supported. */
445 if (XEXP (condition, 1) == const0_rtx)
446 decrement_count = 1;
447 else if (XEXP (condition, 1) != const1_rtx)
448 abort ();
449 break;
451 case GE:
452 /* Currently only GE tests against zero are supported. */
453 if (XEXP (condition, 1) != const0_rtx)
454 abort ();
456 /* The iteration count needs decrementing for a GE test. */
457 decrement_count = 1;
459 /* Determine if the iteration counter will be non-negative.
460 Note that the maximum value loaded is iterations_max - 1. */
461 if ((unsigned HOST_WIDE_INT) INTVAL (iterations_max)
462 <= ((unsigned) 1 << (GET_MODE_BITSIZE (GET_MODE (counter_reg)) - 1)))
463 nonneg = 1;
464 break;
466 /* Abort if an invalid doloop pattern has been generated. */
467 default:
468 abort ();
471 if (decrement_count)
473 if (GET_CODE (count) == CONST_INT)
474 count = GEN_INT (INTVAL (count) - 1);
475 else
476 count = expand_simple_binop (GET_MODE (counter_reg), MINUS,
477 count, const1_rtx,
478 0, 0, OPTAB_LIB_WIDEN);
481 /* Insert initialization of the count register into the loop header. */
482 convert_move (counter_reg, count, 1);
483 sequence = get_insns ();
484 end_sequence ();
485 emit_insn_before (sequence, loop->start);
487 /* Some targets (eg, C4x) need to initialize special looping
488 registers. */
489 #ifdef HAVE_doloop_begin
491 rtx init;
493 init = gen_doloop_begin (counter_reg,
494 GET_CODE (iterations) == CONST_INT
495 ? iterations : const0_rtx, iterations_max,
496 GEN_INT (loop->level));
497 if (init)
499 start_sequence ();
500 emit_insn (init);
501 sequence = get_insns ();
502 end_sequence ();
503 emit_insn_after (sequence, loop->start);
506 #endif
508 /* Insert the new low-overhead looping insn. */
509 emit_jump_insn_before (doloop_seq, loop->end);
510 jump_insn = prev_nonnote_insn (loop->end);
511 JUMP_LABEL (jump_insn) = start_label;
513 /* Add a REG_NONNEG note if the actual or estimated maximum number
514 of iterations is non-negative. */
515 if (nonneg)
517 REG_NOTES (jump_insn)
518 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX, REG_NOTES (jump_insn));
520 return 1;
524 /* Handle the more complex case, where the bounds are not known at
525 compile time. In this case we generate a run_time calculation of
526 the number of iterations. We rely on the existence of a run-time
527 guard to ensure that the loop executes at least once, i.e.,
528 initial_value obeys the loop comparison condition. If a guard is
529 not present, we emit one. The loop to modify is described by LOOP.
530 ITERATIONS_MAX is a CONST_INT specifying the estimated maximum
531 number of loop iterations. DOLOOP_INSN is the low-overhead looping
532 insn to insert. Returns nonzero if loop successfully modified. */
533 static int
534 doloop_modify_runtime (const struct loop *loop, rtx iterations_max,
535 rtx doloop_seq, rtx start_label,
536 enum machine_mode mode, rtx condition)
538 const struct loop_info *loop_info = LOOP_INFO (loop);
539 HOST_WIDE_INT abs_inc;
540 HOST_WIDE_INT abs_loop_inc;
541 int neg_inc;
542 rtx diff;
543 rtx sequence;
544 rtx iterations;
545 rtx initial_value;
546 rtx final_value;
547 rtx increment;
548 int unsigned_p;
549 enum rtx_code comparison_code;
551 increment = loop_info->increment;
552 initial_value = loop_info->initial_value;
553 final_value = loop_info->final_value;
555 neg_inc = 0;
556 abs_inc = INTVAL (increment);
557 if (abs_inc < 0)
559 abs_inc = -abs_inc;
560 neg_inc = 1;
563 comparison_code = loop_info->comparison_code;
564 unsigned_p = (comparison_code == LTU
565 || comparison_code == LEU
566 || comparison_code == GTU
567 || comparison_code == GEU
568 || comparison_code == NE);
570 /* The number of iterations (prior to any loop unrolling) is given by:
572 n = (abs (final - initial) + abs_inc - 1) / abs_inc.
574 However, it is possible for the summation to overflow, and a
575 safer method is:
577 n = abs (final - initial) / abs_inc;
578 n += (abs (final - initial) % abs_inc) != 0;
580 But when abs_inc is a power of two, the summation won't overflow
581 except in cases where the loop never terminates. So we don't
582 need to use this more costly calculation.
584 If the loop has been unrolled, the full calculation is
586 t1 = abs_inc * unroll_number; increment per loop
587 n = (abs (final - initial) + abs_inc - 1) / t1; full loops
588 n += (abs (final - initial) + abs_inc - 1) % t1) >= abs_inc;
589 partial loop
590 which works out to be equivalent to
592 n = (abs (final - initial) + t1 - 1) / t1;
594 In the case where the loop was preconditioned, a few iterations
595 may have been executed earlier; but 'initial' was adjusted as they
596 were executed, so we don't need anything special for that case here.
597 As above, when t1 is a power of two we don't need to worry about
598 overflow.
600 The division and modulo operations can be avoided by requiring
601 that the increment is a power of 2 (precondition_loop_p enforces
602 this requirement). Nevertheless, the RTX_COSTS should be checked
603 to see if a fast divmod is available. */
605 start_sequence ();
606 /* abs (final - initial) */
607 diff = expand_simple_binop (mode, MINUS,
608 copy_rtx (neg_inc ? initial_value : final_value),
609 copy_rtx (neg_inc ? final_value : initial_value),
610 NULL_RTX, unsigned_p, OPTAB_LIB_WIDEN);
612 /* Some code transformations can result in code akin to
614 tmp = i + 1;
616 goto scan_start;
617 top:
618 tmp = tmp + 1;
619 scan_start:
620 i = tmp;
621 if (i < n) goto top;
623 We'll have already detected this form of loop in scan_loop,
624 and set loop->top and loop->scan_start appropriately.
626 In this situation, we skip the increment the first time through
627 the loop, which results in an incorrect estimate of the number
628 of iterations. Adjust the difference to compensate. */
629 /* ??? Logically, it would seem this belongs in loop_iterations.
630 However, this causes regressions e.g. on x86 execute/20011008-3.c,
631 so I do not believe we've properly characterized the exact nature
632 of the problem. In the meantime, this fixes execute/20011126-2.c
633 on ia64 and some Ada front end miscompilation on ppc. */
635 if (loop->scan_start)
637 rtx iteration_var = loop_info->iteration_var;
638 struct loop_ivs *ivs = LOOP_IVS (loop);
639 struct iv_class *bl;
641 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
642 bl = REG_IV_CLASS (ivs, REGNO (iteration_var));
643 else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
645 struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
646 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
648 else
649 /* Iteration var must be an induction variable to get here. */
650 abort ();
652 if (INSN_UID (bl->biv->insn) < max_uid_for_loop
653 && INSN_LUID (bl->biv->insn) < INSN_LUID (loop->scan_start))
655 if (loop_dump_stream)
656 fprintf (loop_dump_stream,
657 "Doloop: Basic induction var skips initial incr.\n");
659 diff = expand_simple_binop (mode, PLUS, diff, GEN_INT (abs_inc),
660 diff, unsigned_p, OPTAB_LIB_WIDEN);
664 abs_loop_inc = abs_inc * loop_info->unroll_number;
665 if (abs_loop_inc != 1)
667 int shift_count;
669 shift_count = exact_log2 (abs_loop_inc);
670 if (shift_count < 0)
671 abort ();
673 /* (abs (final - initial) + abs_inc * unroll_number - 1) */
674 diff = expand_simple_binop (GET_MODE (diff), PLUS,
675 diff, GEN_INT (abs_loop_inc - 1),
676 diff, 1, OPTAB_LIB_WIDEN);
678 /* (abs (final - initial) + abs_inc * unroll_number - 1)
679 / (abs_inc * unroll_number) */
680 diff = expand_simple_binop (GET_MODE (diff), LSHIFTRT,
681 diff, GEN_INT (shift_count),
682 diff, 1, OPTAB_LIB_WIDEN);
684 iterations = diff;
686 /* If there is a NOTE_INSN_LOOP_VTOP, we have a `for' or `while'
687 style loop, with a loop exit test at the start. Thus, we can
688 assume that the loop condition was true when the loop was
689 entered.
691 `do-while' loops require special treatment since the exit test is
692 not executed before the start of the loop. We need to determine
693 if the loop will terminate after the first pass and to limit the
694 iteration count to one if necessary. */
695 if (! loop->vtop)
697 if (loop_dump_stream)
698 fprintf (loop_dump_stream, "Doloop: Do-while loop.\n");
700 /* A `do-while' loop must iterate at least once. For code like
701 i = initial; do { ... } while (++i < final);
702 we will calculate a bogus iteration count if initial > final.
703 So detect this and set the iteration count to 1.
704 Note that if the loop has been unrolled, then the loop body
705 is guaranteed to execute at least once. Also, when the
706 comparison is NE, our calculated count will be OK. */
707 if (loop_info->unroll_number == 1 && comparison_code != NE)
709 rtx label;
711 /* Emit insns to test if the loop will immediately
712 terminate and to set the iteration count to 1 if true. */
713 label = gen_label_rtx();
714 emit_cmp_and_jump_insns (copy_rtx (initial_value),
715 copy_rtx (loop_info->comparison_value),
716 comparison_code, NULL_RTX, mode, 0,
717 label);
718 JUMP_LABEL (get_last_insn ()) = label;
719 LABEL_NUSES (label)++;
720 emit_move_insn (iterations, const1_rtx);
721 emit_label (label);
725 sequence = get_insns ();
726 end_sequence ();
727 emit_insn_before (sequence, loop->start);
729 return doloop_modify (loop, iterations, iterations_max, doloop_seq,
730 start_label, condition);
734 /* This is the main entry point. Process loop described by LOOP
735 validating that the loop is suitable for conversion to use a low
736 overhead looping instruction, replacing the jump insn where
737 suitable. We distinguish between loops with compile-time bounds
738 and those with run-time bounds. Information from LOOP is used to
739 compute the number of iterations and to determine whether the loop
740 is a candidate for this optimization. Returns nonzero if loop
741 successfully modified. */
743 doloop_optimize (const struct loop *loop)
745 struct loop_info *loop_info = LOOP_INFO (loop);
746 rtx initial_value;
747 rtx final_value;
748 rtx increment;
749 rtx jump_insn;
750 enum machine_mode mode;
751 unsigned HOST_WIDE_INT n_iterations;
752 unsigned HOST_WIDE_INT n_iterations_max;
753 rtx doloop_seq, doloop_pat, doloop_reg;
754 rtx iterations;
755 rtx iterations_max;
756 rtx start_label;
757 rtx condition;
759 if (loop_dump_stream)
760 fprintf (loop_dump_stream,
761 "Doloop: Processing loop %d, enclosed levels %d.\n",
762 loop->num, loop->level);
764 jump_insn = prev_nonnote_insn (loop->end);
766 /* Check that loop is a candidate for a low-overhead looping insn. */
767 if (! doloop_valid_p (loop, jump_insn))
768 return 0;
770 /* Determine if the loop can be safely, and profitably,
771 preconditioned. While we don't precondition the loop in a loop
772 unrolling sense, this test ensures that the loop is well behaved
773 and that the increment is a constant integer. */
774 if (! precondition_loop_p (loop, &initial_value, &final_value,
775 &increment, &mode))
777 if (loop_dump_stream)
778 fprintf (loop_dump_stream,
779 "Doloop: Cannot precondition loop.\n");
780 return 0;
783 /* Determine or estimate the maximum number of loop iterations. */
784 n_iterations = loop_info->n_iterations;
785 if (n_iterations)
787 /* This is the simple case where the initial and final loop
788 values are constants. */
789 n_iterations_max = n_iterations;
791 else
793 int nonneg = find_reg_note (jump_insn, REG_NONNEG, 0) != 0;
795 /* This is the harder case where the initial and final loop
796 values may not be constants. */
797 n_iterations_max = doloop_iterations_max (loop_info, mode, nonneg);
799 if (! n_iterations_max)
801 /* We have something like `for (i = 0; i < 10; i--)'. */
802 if (loop_dump_stream)
803 fprintf (loop_dump_stream,
804 "Doloop: Not normal loop.\n");
805 return 0;
809 /* Account for loop unrolling in the iteration count. This will
810 have no effect if loop_iterations could not determine the number
811 of iterations. */
812 n_iterations /= loop_info->unroll_number;
813 n_iterations_max /= loop_info->unroll_number;
815 if (n_iterations && n_iterations < 3)
817 if (loop_dump_stream)
818 fprintf (loop_dump_stream,
819 "Doloop: Too few iterations (%ld) to be profitable.\n",
820 (long int) n_iterations);
821 return 0;
824 iterations = GEN_INT (n_iterations);
825 iterations_max = GEN_INT (n_iterations_max);
827 /* Generate looping insn. If the pattern FAILs then give up trying
828 to modify the loop since there is some aspect the back-end does
829 not like. */
830 start_label = gen_label_rtx ();
831 doloop_reg = gen_reg_rtx (mode);
832 doloop_seq = gen_doloop_end (doloop_reg, iterations, iterations_max,
833 GEN_INT (loop->level), start_label);
834 if (! doloop_seq && mode != word_mode)
836 PUT_MODE (doloop_reg, word_mode);
837 doloop_seq = gen_doloop_end (doloop_reg, iterations, iterations_max,
838 GEN_INT (loop->level), start_label);
840 if (! doloop_seq)
842 if (loop_dump_stream)
843 fprintf (loop_dump_stream,
844 "Doloop: Target unwilling to use doloop pattern!\n");
845 return 0;
848 /* If multiple instructions were created, the last must be the
849 jump instruction. Also, a raw define_insn may yield a plain
850 pattern. */
851 doloop_pat = doloop_seq;
852 if (INSN_P (doloop_pat))
854 while (NEXT_INSN (doloop_pat) != NULL_RTX)
855 doloop_pat = NEXT_INSN (doloop_pat);
856 if (GET_CODE (doloop_pat) == JUMP_INSN)
857 doloop_pat = PATTERN (doloop_pat);
858 else
859 doloop_pat = NULL_RTX;
862 if (! doloop_pat
863 || ! (condition = doloop_condition_get (doloop_pat)))
865 if (loop_dump_stream)
866 fprintf (loop_dump_stream,
867 "Doloop: Unrecognizable doloop pattern!\n");
868 return 0;
871 if (n_iterations != 0)
872 /* Handle the simpler case, where we know the iteration count at
873 compile time. */
874 return doloop_modify (loop, iterations, iterations_max, doloop_seq,
875 start_label, condition);
876 else
877 /* Handle the harder case, where we must add additional runtime tests. */
878 return doloop_modify_runtime (loop, iterations_max, doloop_seq,
879 start_label, mode, condition);
882 #endif /* HAVE_doloop_end */