1 /* Perform doloop optimizations
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Based on code 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
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
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
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 /* This module is used to modify loops with a determinable number of
38 iterations to use special low-overhead looping instructions.
40 It first validates whether the loop is well behaved and has a
41 determinable number of iterations (either at compile or run-time).
42 It then modifies the loop to use a low-overhead looping pattern as
45 1. A pseudo register is allocated as the loop iteration counter.
47 2. The number of loop iterations is calculated and is stored
50 3. At the end of the loop, the jump insn is replaced by the
51 doloop_end pattern. The compare must remain because it might be
52 used elsewhere. If the loop-variable or condition register are
53 used elsewhere, they will be eliminated by flow.
55 4. An optional doloop_begin pattern is inserted at the top of the
58 TODO The optimization should only performed when either the biv used for exit
59 condition is unused at all except for the exit test, or if we do not have to
60 change its value, since otherwise we have to add a new induction variable,
61 which usually will not pay up (unless the cost of the doloop pattern is
62 somehow extremely lower than the cost of compare & jump, or unless the bct
63 register cannot be used for anything else but doloop -- ??? detect these
66 #ifdef HAVE_doloop_end
68 /* Return the loop termination condition for PATTERN or zero
69 if it is not a decrement and branch jump insn. */
72 doloop_condition_get (rtx pattern
)
79 /* The canonical doloop pattern we expect is:
81 (parallel [(set (pc) (if_then_else (condition)
84 (set (reg) (plus (reg) (const_int -1)))
85 (additional clobbers and uses)])
87 Some machines (IA-64) make the decrement conditional on
88 the condition as well, so we don't bother verifying the
89 actual decrement. In summary, the branch must be the
90 first entry of the parallel (also required by jump.c),
91 and the second entry of the parallel must be a set of
92 the loop counter register. */
94 if (GET_CODE (pattern
) != PARALLEL
)
97 cmp
= XVECEXP (pattern
, 0, 0);
98 inc
= XVECEXP (pattern
, 0, 1);
100 /* Check for (set (reg) (something)). */
101 if (GET_CODE (inc
) != SET
|| ! REG_P (SET_DEST (inc
)))
104 /* Extract loop counter register. */
105 reg
= SET_DEST (inc
);
107 /* Check for (set (pc) (if_then_else (condition)
110 if (GET_CODE (cmp
) != SET
111 || SET_DEST (cmp
) != pc_rtx
112 || GET_CODE (SET_SRC (cmp
)) != IF_THEN_ELSE
113 || GET_CODE (XEXP (SET_SRC (cmp
), 1)) != LABEL_REF
114 || XEXP (SET_SRC (cmp
), 2) != pc_rtx
)
117 /* Extract loop termination condition. */
118 condition
= XEXP (SET_SRC (cmp
), 0);
120 if ((GET_CODE (condition
) != GE
&& GET_CODE (condition
) != NE
)
121 || GET_CODE (XEXP (condition
, 1)) != CONST_INT
)
124 if (XEXP (condition
, 0) == reg
)
127 if (GET_CODE (XEXP (condition
, 0)) == PLUS
128 && XEXP (XEXP (condition
, 0), 0) == reg
)
131 /* ??? If a machine uses a funny comparison, we could return a
132 canonicalized form here. */
137 /* Return nonzero if the loop specified by LOOP is suitable for
138 the use of special low-overhead looping instructions. DESC
139 describes the number of iterations of the loop. */
142 doloop_valid_p (struct loop
*loop
, struct niter_desc
*desc
)
144 basic_block
*body
= get_loop_body (loop
), bb
;
149 /* Check for loops that may not terminate under special conditions. */
154 /* There are some cases that would require a special attention.
155 For example if the comparison is LEU and the comparison value
156 is UINT_MAX then the loop will not terminate. Similarly, if the
157 comparison code is GEU and the comparison value is 0, the
158 loop will not terminate.
160 If the absolute increment is not 1, the loop can be infinite
161 even with LTU/GTU, e.g. for (i = 3; i > 0; i -= 2)
163 ??? We could compute these conditions at run-time and have a
164 additional jump around the loop to ensure an infinite loop.
165 However, it is very unlikely that this is the intended
166 behavior of the loop and checking for these rare boundary
167 conditions would pessimize all other code.
169 If the loop is executed only a few times an extra check to
170 restart the loop could use up most of the benefits of using a
171 count register loop. Note however, that normally, this
172 restart branch would never execute, so it could be predicted
173 well by the CPU. We should generate the pessimistic code by
174 default, and have an option, e.g. -funsafe-loops that would
175 enable count-register loops in this case. */
177 fprintf (dump_file
, "Doloop: Possible infinite iteration case.\n");
182 for (i
= 0; i
< loop
->num_nodes
; i
++)
186 for (insn
= BB_HEAD (bb
);
187 insn
!= NEXT_INSN (BB_END (bb
));
188 insn
= NEXT_INSN (insn
))
190 /* A called function may clobber any special registers required for
191 low-overhead looping. */
192 if (GET_CODE (insn
) == CALL_INSN
)
195 fprintf (dump_file
, "Doloop: Function call in loop.\n");
200 /* Some targets (eg, PPC) use the count register for branch on table
201 instructions. ??? This should be a target specific check. */
202 if (GET_CODE (insn
) == JUMP_INSN
203 && (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
204 || GET_CODE (PATTERN (insn
)) == ADDR_VEC
))
207 fprintf (dump_file
, "Doloop: Computed branch in the loop.\n");
221 /* Adds test of COND jumping to DEST to the end of BB. */
224 add_test (rtx cond
, basic_block bb
, basic_block dest
)
226 rtx seq
, jump
, label
;
227 enum machine_mode mode
;
228 rtx op0
= XEXP (cond
, 0), op1
= XEXP (cond
, 1);
229 enum rtx_code code
= GET_CODE (cond
);
231 mode
= GET_MODE (XEXP (cond
, 0));
232 if (mode
== VOIDmode
)
233 mode
= GET_MODE (XEXP (cond
, 1));
236 op0
= force_operand (op0
, NULL_RTX
);
237 op1
= force_operand (op1
, NULL_RTX
);
238 label
= block_label (dest
);
239 do_compare_rtx_and_jump (op0
, op1
, code
, 0, mode
, NULL_RTX
, NULL_RTX
, label
);
241 jump
= get_last_insn ();
242 JUMP_LABEL (jump
) = label
;
244 /* The jump is supposed to handle an unlikely special case. */
246 = gen_rtx_EXPR_LIST (REG_BR_PROB
,
247 const0_rtx
, REG_NOTES (jump
));
249 LABEL_NUSES (label
)++;
253 emit_insn_after (seq
, BB_END (bb
));
256 /* Modify the loop to use the low-overhead looping insn where LOOP
257 describes the loop, DESC describes the number of iterations of the
258 loop, and DOLOOP_INSN is the low-overhead looping insn to emit at the
259 end of the loop. CONDITION is the condition separated from the
263 doloop_modify (struct loop
*loop
, struct niter_desc
*desc
,
264 rtx doloop_seq
, rtx condition
)
267 rtx count
, tmp
, noloop
= NULL_RTX
;
272 bool increment_count
;
273 basic_block loop_end
= desc
->out_edge
->src
;
275 jump_insn
= BB_END (loop_end
);
279 fprintf (dump_file
, "Doloop: Inserting doloop pattern (");
280 if (desc
->const_iter
)
281 fprintf (dump_file
, HOST_WIDEST_INT_PRINT_DEC
, desc
->niter
);
283 fputs ("runtime", dump_file
);
284 fputs (" iterations).\n", dump_file
);
287 /* Discard original jump to continue loop. The original compare
288 result may still be live, so it cannot be discarded explicitly. */
289 delete_insn (jump_insn
);
291 counter_reg
= XEXP (condition
, 0);
292 if (GET_CODE (counter_reg
) == PLUS
)
293 counter_reg
= XEXP (counter_reg
, 0);
295 count
= desc
->niter_expr
;
296 increment_count
= false;
297 switch (GET_CODE (condition
))
300 /* Currently only NE tests against zero and one are supported. */
301 if (XEXP (condition
, 1) == const1_rtx
)
303 increment_count
= true;
306 else if (XEXP (condition
, 1) == const0_rtx
)
313 /* Currently only GE tests against zero are supported. */
314 if (XEXP (condition
, 1) != const0_rtx
)
317 noloop
= constm1_rtx
;
319 /* The iteration count does not need incrementing for a GE test. */
320 increment_count
= false;
322 /* Determine if the iteration counter will be non-negative.
323 Note that the maximum value loaded is iterations_max - 1. */
325 <= ((unsigned HOST_WIDEST_INT
) 1
326 << (GET_MODE_BITSIZE (GET_MODE (counter_reg
)) - 1)))
330 /* Abort if an invalid doloop pattern has been generated. */
336 count
= simplify_gen_binary (PLUS
, desc
->mode
, count
, const1_rtx
);
338 /* Insert initialization of the count register into the loop header. */
340 tmp
= force_operand (count
, counter_reg
);
341 convert_move (counter_reg
, tmp
, 1);
342 sequence
= get_insns ();
344 emit_insn_after (sequence
, BB_END (loop_preheader_edge (loop
)->src
));
346 if (desc
->noloop_assumptions
)
348 rtx ass
= desc
->noloop_assumptions
;
349 basic_block preheader
= loop_preheader_edge (loop
)->src
;
351 = loop_split_edge_with (loop_preheader_edge (loop
), NULL_RTX
);
352 basic_block new_preheader
353 = loop_split_edge_with (loop_preheader_edge (loop
), NULL_RTX
);
358 /* Expand the condition testing the assumptions and if it does not pass,
359 reset the count register to 0. */
360 add_test (XEXP (ass
, 0), preheader
, set_zero
);
361 preheader
->succ
->flags
&= ~EDGE_FALLTHRU
;
362 cnt
= preheader
->succ
->count
;
363 preheader
->succ
->probability
= 0;
364 preheader
->succ
->count
= 0;
365 irr
= preheader
->succ
->flags
& EDGE_IRREDUCIBLE_LOOP
;
366 te
= make_edge (preheader
, new_preheader
, EDGE_FALLTHRU
| irr
);
367 te
->probability
= REG_BR_PROB_BASE
;
369 set_immediate_dominator (CDI_DOMINATORS
, new_preheader
, preheader
);
372 set_zero
->frequency
= 0;
374 for (ass
= XEXP (ass
, 1); ass
; ass
= XEXP (ass
, 1))
376 bb
= loop_split_edge_with (te
, NULL_RTX
);
378 add_test (XEXP (ass
, 0), bb
, set_zero
);
379 make_edge (bb
, set_zero
, irr
);
383 convert_move (counter_reg
, noloop
, 0);
384 sequence
= get_insns ();
386 emit_insn_after (sequence
, BB_END (set_zero
));
389 /* Some targets (eg, C4x) need to initialize special looping
391 #ifdef HAVE_doloop_begin
394 unsigned level
= get_loop_level (loop
) + 1;
395 init
= gen_doloop_begin (counter_reg
,
396 desc
->const_iter
? desc
->niter_expr
: const0_rtx
,
403 sequence
= get_insns ();
405 emit_insn_after (sequence
, BB_END (loop_preheader_edge (loop
)->src
));
410 /* Insert the new low-overhead looping insn. */
411 emit_jump_insn_after (doloop_seq
, BB_END (loop_end
));
412 jump_insn
= BB_END (loop_end
);
413 jump_label
= block_label (desc
->in_edge
->dest
);
414 JUMP_LABEL (jump_insn
) = jump_label
;
415 LABEL_NUSES (jump_label
)++;
417 /* Ensure the right fallthru edge is marked, for case we have reversed
419 desc
->in_edge
->flags
&= ~EDGE_FALLTHRU
;
420 desc
->out_edge
->flags
|= EDGE_FALLTHRU
;
422 /* Add a REG_NONNEG note if the actual or estimated maximum number
423 of iterations is non-negative. */
426 REG_NOTES (jump_insn
)
427 = gen_rtx_EXPR_LIST (REG_NONNEG
, NULL_RTX
, REG_NOTES (jump_insn
));
431 /* Process loop described by LOOP validating that the loop is suitable for
432 conversion to use a low overhead looping instruction, replacing the jump
433 insn where suitable. Returns true if the loop was successfully
437 doloop_optimize (struct loop
*loop
)
439 enum machine_mode mode
;
440 rtx doloop_seq
, doloop_pat
, doloop_reg
;
445 unsigned level
, est_niter
;
446 struct niter_desc
*desc
;
449 fprintf (dump_file
, "Doloop: Processing loop %d.\n", loop
->num
);
451 iv_analysis_loop_init (loop
);
453 /* Find the simple exit of a LOOP. */
454 desc
= get_simple_loop_desc (loop
);
456 /* Check that loop is a candidate for a low-overhead looping insn. */
457 if (!doloop_valid_p (loop
, desc
))
461 "Doloop: The loop is not suitable.\n");
467 if (desc
->const_iter
)
468 est_niter
= desc
->niter
;
469 /* If the estimate on number of iterations is reliable (comes from profile
470 feedback), use it. Do not use it normally, since the expected number
471 of iterations of an unrolled loop is 2. */
472 if (loop
->header
->count
)
473 est_niter
= expected_loop_iterations (loop
);
479 "Doloop: Too few iterations (%u) to be profitable.\n",
484 iterations
= desc
->const_iter
? desc
->niter_expr
: const0_rtx
;
485 iterations_max
= GEN_INT (desc
->niter_max
);
486 level
= get_loop_level (loop
) + 1;
488 /* Generate looping insn. If the pattern FAILs then give up trying
489 to modify the loop since there is some aspect the back-end does
491 start_label
= block_label (desc
->in_edge
->dest
);
492 doloop_reg
= gen_reg_rtx (mode
);
493 doloop_seq
= gen_doloop_end (doloop_reg
, iterations
, iterations_max
,
494 GEN_INT (level
), start_label
);
495 if (! doloop_seq
&& mode
!= word_mode
)
497 PUT_MODE (doloop_reg
, word_mode
);
498 doloop_seq
= gen_doloop_end (doloop_reg
, iterations
, iterations_max
,
499 GEN_INT (level
), start_label
);
505 "Doloop: Target unwilling to use doloop pattern!\n");
509 /* If multiple instructions were created, the last must be the
510 jump instruction. Also, a raw define_insn may yield a plain
512 doloop_pat
= doloop_seq
;
513 if (INSN_P (doloop_pat
))
515 while (NEXT_INSN (doloop_pat
) != NULL_RTX
)
516 doloop_pat
= NEXT_INSN (doloop_pat
);
517 if (GET_CODE (doloop_pat
) == JUMP_INSN
)
518 doloop_pat
= PATTERN (doloop_pat
);
520 doloop_pat
= NULL_RTX
;
524 || ! (condition
= doloop_condition_get (doloop_pat
)))
527 fprintf (dump_file
, "Doloop: Unrecognizable doloop pattern!\n");
531 doloop_modify (loop
, desc
, doloop_seq
, condition
);
535 /* This is the main entry point. Process all LOOPS using doloop_optimize. */
538 doloop_optimize_loops (struct loops
*loops
)
543 for (i
= 1; i
< loops
->num
; i
++)
545 loop
= loops
->parray
[i
];
549 doloop_optimize (loop
);
554 #ifdef ENABLE_CHECKING
555 verify_dominators (CDI_DOMINATORS
);
556 verify_loop_structure (loops
);
559 #endif /* HAVE_doloop_end */