alias.c: Include toplev.h
[gcc.git] / gcc / unroll.c
1 /* Try to unroll loops, and split induction variables.
2 Copyright (C) 1992, 93, 94, 95, 97, 1998 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Try to unroll a loop, and split induction variables.
23
24 Loops for which the number of iterations can be calculated exactly are
25 handled specially. If the number of iterations times the insn_count is
26 less than MAX_UNROLLED_INSNS, then the loop is unrolled completely.
27 Otherwise, we try to unroll the loop a number of times modulo the number
28 of iterations, so that only one exit test will be needed. It is unrolled
29 a number of times approximately equal to MAX_UNROLLED_INSNS divided by
30 the insn count.
31
32 Otherwise, if the number of iterations can be calculated exactly at
33 run time, and the loop is always entered at the top, then we try to
34 precondition the loop. That is, at run time, calculate how many times
35 the loop will execute, and then execute the loop body a few times so
36 that the remaining iterations will be some multiple of 4 (or 2 if the
37 loop is large). Then fall through to a loop unrolled 4 (or 2) times,
38 with only one exit test needed at the end of the loop.
39
40 Otherwise, if the number of iterations can not be calculated exactly,
41 not even at run time, then we still unroll the loop a number of times
42 approximately equal to MAX_UNROLLED_INSNS divided by the insn count,
43 but there must be an exit test after each copy of the loop body.
44
45 For each induction variable, which is dead outside the loop (replaceable)
46 or for which we can easily calculate the final value, if we can easily
47 calculate its value at each place where it is set as a function of the
48 current loop unroll count and the variable's value at loop entry, then
49 the induction variable is split into `N' different variables, one for
50 each copy of the loop body. One variable is live across the backward
51 branch, and the others are all calculated as a function of this variable.
52 This helps eliminate data dependencies, and leads to further opportunities
53 for cse. */
54
55 /* Possible improvements follow: */
56
57 /* ??? Add an extra pass somewhere to determine whether unrolling will
58 give any benefit. E.g. after generating all unrolled insns, compute the
59 cost of all insns and compare against cost of insns in rolled loop.
60
61 - On traditional architectures, unrolling a non-constant bound loop
62 is a win if there is a giv whose only use is in memory addresses, the
63 memory addresses can be split, and hence giv increments can be
64 eliminated.
65 - It is also a win if the loop is executed many times, and preconditioning
66 can be performed for the loop.
67 Add code to check for these and similar cases. */
68
69 /* ??? Improve control of which loops get unrolled. Could use profiling
70 info to only unroll the most commonly executed loops. Perhaps have
71 a user specifyable option to control the amount of code expansion,
72 or the percent of loops to consider for unrolling. Etc. */
73
74 /* ??? Look at the register copies inside the loop to see if they form a
75 simple permutation. If so, iterate the permutation until it gets back to
76 the start state. This is how many times we should unroll the loop, for
77 best results, because then all register copies can be eliminated.
78 For example, the lisp nreverse function should be unrolled 3 times
79 while (this)
80 {
81 next = this->cdr;
82 this->cdr = prev;
83 prev = this;
84 this = next;
85 }
86
87 ??? The number of times to unroll the loop may also be based on data
88 references in the loop. For example, if we have a loop that references
89 x[i-1], x[i], and x[i+1], we should unroll it a multiple of 3 times. */
90
91 /* ??? Add some simple linear equation solving capability so that we can
92 determine the number of loop iterations for more complex loops.
93 For example, consider this loop from gdb
94 #define SWAP_TARGET_AND_HOST(buffer,len)
95 {
96 char tmp;
97 char *p = (char *) buffer;
98 char *q = ((char *) buffer) + len - 1;
99 int iterations = (len + 1) >> 1;
100 int i;
101 for (p; p < q; p++, q--;)
102 {
103 tmp = *q;
104 *q = *p;
105 *p = tmp;
106 }
107 }
108 Note that:
109 start value = p = &buffer + current_iteration
110 end value = q = &buffer + len - 1 - current_iteration
111 Given the loop exit test of "p < q", then there must be "q - p" iterations,
112 set equal to zero and solve for number of iterations:
113 q - p = len - 1 - 2*current_iteration = 0
114 current_iteration = (len - 1) / 2
115 Hence, there are (len - 1) / 2 (rounded up to the nearest integer)
116 iterations of this loop. */
117
118 /* ??? Currently, no labels are marked as loop invariant when doing loop
119 unrolling. This is because an insn inside the loop, that loads the address
120 of a label inside the loop into a register, could be moved outside the loop
121 by the invariant code motion pass if labels were invariant. If the loop
122 is subsequently unrolled, the code will be wrong because each unrolled
123 body of the loop will use the same address, whereas each actually needs a
124 different address. A case where this happens is when a loop containing
125 a switch statement is unrolled.
126
127 It would be better to let labels be considered invariant. When we
128 unroll loops here, check to see if any insns using a label local to the
129 loop were moved before the loop. If so, then correct the problem, by
130 moving the insn back into the loop, or perhaps replicate the insn before
131 the loop, one copy for each time the loop is unrolled. */
132
133 /* The prime factors looked for when trying to unroll a loop by some
134 number which is modulo the total number of iterations. Just checking
135 for these 4 prime factors will find at least one factor for 75% of
136 all numbers theoretically. Practically speaking, this will succeed
137 almost all of the time since loops are generally a multiple of 2
138 and/or 5. */
139
140 #define NUM_FACTORS 4
141
142 struct _factor { int factor, count; } factors[NUM_FACTORS]
143 = { {2, 0}, {3, 0}, {5, 0}, {7, 0}};
144
145 /* Describes the different types of loop unrolling performed. */
146
147 enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
148
149 #include "config.h"
150 #include "system.h"
151 #include "rtl.h"
152 #include "insn-config.h"
153 #include "integrate.h"
154 #include "regs.h"
155 #include "recog.h"
156 #include "flags.h"
157 #include "expr.h"
158 #include "loop.h"
159 #include "toplev.h"
160
161 /* This controls which loops are unrolled, and by how much we unroll
162 them. */
163
164 #ifndef MAX_UNROLLED_INSNS
165 #define MAX_UNROLLED_INSNS 100
166 #endif
167
168 /* Indexed by register number, if non-zero, then it contains a pointer
169 to a struct induction for a DEST_REG giv which has been combined with
170 one of more address givs. This is needed because whenever such a DEST_REG
171 giv is modified, we must modify the value of all split address givs
172 that were combined with this DEST_REG giv. */
173
174 static struct induction **addr_combined_regs;
175
176 /* Indexed by register number, if this is a splittable induction variable,
177 then this will hold the current value of the register, which depends on the
178 iteration number. */
179
180 static rtx *splittable_regs;
181
182 /* Indexed by register number, if this is a splittable induction variable,
183 then this will hold the number of instructions in the loop that modify
184 the induction variable. Used to ensure that only the last insn modifying
185 a split iv will update the original iv of the dest. */
186
187 static int *splittable_regs_updates;
188
189 /* Values describing the current loop's iteration variable. These are set up
190 by loop_iterations, and used by precondition_loop_p. */
191
192 static rtx loop_iteration_var;
193 static rtx loop_initial_value;
194 static rtx loop_increment;
195 static rtx loop_final_value;
196 static enum rtx_code loop_comparison_code;
197
198 /* Forward declarations. */
199
200 static void init_reg_map PROTO((struct inline_remap *, int));
201 static int precondition_loop_p PROTO((rtx *, rtx *, rtx *, rtx, rtx));
202 static rtx calculate_giv_inc PROTO((rtx, rtx, int));
203 static rtx initial_reg_note_copy PROTO((rtx, struct inline_remap *));
204 static void final_reg_note_copy PROTO((rtx, struct inline_remap *));
205 static void copy_loop_body PROTO((rtx, rtx, struct inline_remap *, rtx, int,
206 enum unroll_types, rtx, rtx, rtx, rtx));
207 void iteration_info PROTO((rtx, rtx *, rtx *, rtx, rtx));
208 static rtx approx_final_value PROTO((enum rtx_code, rtx, int *, int *));
209 static int find_splittable_regs PROTO((enum unroll_types, rtx, rtx, rtx, int));
210 static int find_splittable_givs PROTO((struct iv_class *,enum unroll_types,
211 rtx, rtx, rtx, int));
212 static int reg_dead_after_loop PROTO((rtx, rtx, rtx));
213 static rtx fold_rtx_mult_add PROTO((rtx, rtx, rtx, enum machine_mode));
214 static int verify_addresses PROTO((struct induction *, rtx, int));
215 static rtx remap_split_bivs PROTO((rtx));
216
217 /* Try to unroll one loop and split induction variables in the loop.
218
219 The loop is described by the arguments LOOP_END, INSN_COUNT, and
220 LOOP_START. END_INSERT_BEFORE indicates where insns should be added
221 which need to be executed when the loop falls through. STRENGTH_REDUCTION_P
222 indicates whether information generated in the strength reduction pass
223 is available.
224
225 This function is intended to be called from within `strength_reduce'
226 in loop.c. */
227
228 void
229 unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
230 strength_reduce_p)
231 rtx loop_end;
232 int insn_count;
233 rtx loop_start;
234 rtx end_insert_before;
235 int strength_reduce_p;
236 {
237 int i, j, temp;
238 int unroll_number = 1;
239 rtx copy_start, copy_end;
240 rtx insn, sequence, pattern, tem;
241 int max_labelno, max_insnno;
242 rtx insert_before;
243 struct inline_remap *map;
244 char *local_label;
245 char *local_regno;
246 int maxregnum;
247 int new_maxregnum;
248 rtx exit_label = 0;
249 rtx start_label;
250 struct iv_class *bl;
251 int splitting_not_safe = 0;
252 enum unroll_types unroll_type;
253 int loop_preconditioned = 0;
254 rtx safety_label;
255 /* This points to the last real insn in the loop, which should be either
256 a JUMP_INSN (for conditional jumps) or a BARRIER (for unconditional
257 jumps). */
258 rtx last_loop_insn;
259
260 /* Don't bother unrolling huge loops. Since the minimum factor is
261 two, loops greater than one half of MAX_UNROLLED_INSNS will never
262 be unrolled. */
263 if (insn_count > MAX_UNROLLED_INSNS / 2)
264 {
265 if (loop_dump_stream)
266 fprintf (loop_dump_stream, "Unrolling failure: Loop too big.\n");
267 return;
268 }
269
270 /* When emitting debugger info, we can't unroll loops with unequal numbers
271 of block_beg and block_end notes, because that would unbalance the block
272 structure of the function. This can happen as a result of the
273 "if (foo) bar; else break;" optimization in jump.c. */
274 /* ??? Gcc has a general policy that -g is never supposed to change the code
275 that the compiler emits, so we must disable this optimization always,
276 even if debug info is not being output. This is rare, so this should
277 not be a significant performance problem. */
278
279 if (1 /* write_symbols != NO_DEBUG */)
280 {
281 int block_begins = 0;
282 int block_ends = 0;
283
284 for (insn = loop_start; insn != loop_end; insn = NEXT_INSN (insn))
285 {
286 if (GET_CODE (insn) == NOTE)
287 {
288 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
289 block_begins++;
290 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
291 block_ends++;
292 }
293 }
294
295 if (block_begins != block_ends)
296 {
297 if (loop_dump_stream)
298 fprintf (loop_dump_stream,
299 "Unrolling failure: Unbalanced block notes.\n");
300 return;
301 }
302 }
303
304 /* Determine type of unroll to perform. Depends on the number of iterations
305 and the size of the loop. */
306
307 /* If there is no strength reduce info, then set loop_n_iterations to zero.
308 This can happen if strength_reduce can't find any bivs in the loop.
309 A value of zero indicates that the number of iterations could not be
310 calculated. */
311
312 if (! strength_reduce_p)
313 loop_n_iterations = 0;
314
315 if (loop_dump_stream && loop_n_iterations > 0)
316 fprintf (loop_dump_stream,
317 "Loop unrolling: %d iterations.\n", loop_n_iterations);
318
319 /* Find and save a pointer to the last nonnote insn in the loop. */
320
321 last_loop_insn = prev_nonnote_insn (loop_end);
322
323 /* Calculate how many times to unroll the loop. Indicate whether or
324 not the loop is being completely unrolled. */
325
326 if (loop_n_iterations == 1)
327 {
328 /* If number of iterations is exactly 1, then eliminate the compare and
329 branch at the end of the loop since they will never be taken.
330 Then return, since no other action is needed here. */
331
332 /* If the last instruction is not a BARRIER or a JUMP_INSN, then
333 don't do anything. */
334
335 if (GET_CODE (last_loop_insn) == BARRIER)
336 {
337 /* Delete the jump insn. This will delete the barrier also. */
338 delete_insn (PREV_INSN (last_loop_insn));
339 }
340 else if (GET_CODE (last_loop_insn) == JUMP_INSN)
341 {
342 #ifdef HAVE_cc0
343 /* The immediately preceding insn is a compare which must be
344 deleted. */
345 delete_insn (last_loop_insn);
346 delete_insn (PREV_INSN (last_loop_insn));
347 #else
348 /* The immediately preceding insn may not be the compare, so don't
349 delete it. */
350 delete_insn (last_loop_insn);
351 #endif
352 }
353 return;
354 }
355 else if (loop_n_iterations > 0
356 && loop_n_iterations * insn_count < MAX_UNROLLED_INSNS)
357 {
358 unroll_number = loop_n_iterations;
359 unroll_type = UNROLL_COMPLETELY;
360 }
361 else if (loop_n_iterations > 0)
362 {
363 /* Try to factor the number of iterations. Don't bother with the
364 general case, only using 2, 3, 5, and 7 will get 75% of all
365 numbers theoretically, and almost all in practice. */
366
367 for (i = 0; i < NUM_FACTORS; i++)
368 factors[i].count = 0;
369
370 temp = loop_n_iterations;
371 for (i = NUM_FACTORS - 1; i >= 0; i--)
372 while (temp % factors[i].factor == 0)
373 {
374 factors[i].count++;
375 temp = temp / factors[i].factor;
376 }
377
378 /* Start with the larger factors first so that we generally
379 get lots of unrolling. */
380
381 unroll_number = 1;
382 temp = insn_count;
383 for (i = 3; i >= 0; i--)
384 while (factors[i].count--)
385 {
386 if (temp * factors[i].factor < MAX_UNROLLED_INSNS)
387 {
388 unroll_number *= factors[i].factor;
389 temp *= factors[i].factor;
390 }
391 else
392 break;
393 }
394
395 /* If we couldn't find any factors, then unroll as in the normal
396 case. */
397 if (unroll_number == 1)
398 {
399 if (loop_dump_stream)
400 fprintf (loop_dump_stream,
401 "Loop unrolling: No factors found.\n");
402 }
403 else
404 unroll_type = UNROLL_MODULO;
405 }
406
407
408 /* Default case, calculate number of times to unroll loop based on its
409 size. */
410 if (unroll_number == 1)
411 {
412 if (8 * insn_count < MAX_UNROLLED_INSNS)
413 unroll_number = 8;
414 else if (4 * insn_count < MAX_UNROLLED_INSNS)
415 unroll_number = 4;
416 else
417 unroll_number = 2;
418
419 unroll_type = UNROLL_NAIVE;
420 }
421
422 /* Now we know how many times to unroll the loop. */
423
424 if (loop_dump_stream)
425 fprintf (loop_dump_stream,
426 "Unrolling loop %d times.\n", unroll_number);
427
428
429 if (unroll_type == UNROLL_COMPLETELY || unroll_type == UNROLL_MODULO)
430 {
431 /* Loops of these types should never start with a jump down to
432 the exit condition test. For now, check for this case just to
433 be sure. UNROLL_NAIVE loops can be of this form, this case is
434 handled below. */
435 insn = loop_start;
436 while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
437 insn = NEXT_INSN (insn);
438 if (GET_CODE (insn) == JUMP_INSN)
439 abort ();
440 }
441
442 if (unroll_type == UNROLL_COMPLETELY)
443 {
444 /* Completely unrolling the loop: Delete the compare and branch at
445 the end (the last two instructions). This delete must done at the
446 very end of loop unrolling, to avoid problems with calls to
447 back_branch_in_range_p, which is called by find_splittable_regs.
448 All increments of splittable bivs/givs are changed to load constant
449 instructions. */
450
451 copy_start = loop_start;
452
453 /* Set insert_before to the instruction immediately after the JUMP_INSN
454 (or BARRIER), so that any NOTEs between the JUMP_INSN and the end of
455 the loop will be correctly handled by copy_loop_body. */
456 insert_before = NEXT_INSN (last_loop_insn);
457
458 /* Set copy_end to the insn before the jump at the end of the loop. */
459 if (GET_CODE (last_loop_insn) == BARRIER)
460 copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
461 else if (GET_CODE (last_loop_insn) == JUMP_INSN)
462 {
463 #ifdef HAVE_cc0
464 /* The instruction immediately before the JUMP_INSN is a compare
465 instruction which we do not want to copy. */
466 copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
467 #else
468 /* The instruction immediately before the JUMP_INSN may not be the
469 compare, so we must copy it. */
470 copy_end = PREV_INSN (last_loop_insn);
471 #endif
472 }
473 else
474 {
475 /* We currently can't unroll a loop if it doesn't end with a
476 JUMP_INSN. There would need to be a mechanism that recognizes
477 this case, and then inserts a jump after each loop body, which
478 jumps to after the last loop body. */
479 if (loop_dump_stream)
480 fprintf (loop_dump_stream,
481 "Unrolling failure: loop does not end with a JUMP_INSN.\n");
482 return;
483 }
484 }
485 else if (unroll_type == UNROLL_MODULO)
486 {
487 /* Partially unrolling the loop: The compare and branch at the end
488 (the last two instructions) must remain. Don't copy the compare
489 and branch instructions at the end of the loop. Insert the unrolled
490 code immediately before the compare/branch at the end so that the
491 code will fall through to them as before. */
492
493 copy_start = loop_start;
494
495 /* Set insert_before to the jump insn at the end of the loop.
496 Set copy_end to before the jump insn at the end of the loop. */
497 if (GET_CODE (last_loop_insn) == BARRIER)
498 {
499 insert_before = PREV_INSN (last_loop_insn);
500 copy_end = PREV_INSN (insert_before);
501 }
502 else if (GET_CODE (last_loop_insn) == JUMP_INSN)
503 {
504 #ifdef HAVE_cc0
505 /* The instruction immediately before the JUMP_INSN is a compare
506 instruction which we do not want to copy or delete. */
507 insert_before = PREV_INSN (last_loop_insn);
508 copy_end = PREV_INSN (insert_before);
509 #else
510 /* The instruction immediately before the JUMP_INSN may not be the
511 compare, so we must copy it. */
512 insert_before = last_loop_insn;
513 copy_end = PREV_INSN (last_loop_insn);
514 #endif
515 }
516 else
517 {
518 /* We currently can't unroll a loop if it doesn't end with a
519 JUMP_INSN. There would need to be a mechanism that recognizes
520 this case, and then inserts a jump after each loop body, which
521 jumps to after the last loop body. */
522 if (loop_dump_stream)
523 fprintf (loop_dump_stream,
524 "Unrolling failure: loop does not end with a JUMP_INSN.\n");
525 return;
526 }
527 }
528 else
529 {
530 /* Normal case: Must copy the compare and branch instructions at the
531 end of the loop. */
532
533 if (GET_CODE (last_loop_insn) == BARRIER)
534 {
535 /* Loop ends with an unconditional jump and a barrier.
536 Handle this like above, don't copy jump and barrier.
537 This is not strictly necessary, but doing so prevents generating
538 unconditional jumps to an immediately following label.
539
540 This will be corrected below if the target of this jump is
541 not the start_label. */
542
543 insert_before = PREV_INSN (last_loop_insn);
544 copy_end = PREV_INSN (insert_before);
545 }
546 else if (GET_CODE (last_loop_insn) == JUMP_INSN)
547 {
548 /* Set insert_before to immediately after the JUMP_INSN, so that
549 NOTEs at the end of the loop will be correctly handled by
550 copy_loop_body. */
551 insert_before = NEXT_INSN (last_loop_insn);
552 copy_end = last_loop_insn;
553 }
554 else
555 {
556 /* We currently can't unroll a loop if it doesn't end with a
557 JUMP_INSN. There would need to be a mechanism that recognizes
558 this case, and then inserts a jump after each loop body, which
559 jumps to after the last loop body. */
560 if (loop_dump_stream)
561 fprintf (loop_dump_stream,
562 "Unrolling failure: loop does not end with a JUMP_INSN.\n");
563 return;
564 }
565
566 /* If copying exit test branches because they can not be eliminated,
567 then must convert the fall through case of the branch to a jump past
568 the end of the loop. Create a label to emit after the loop and save
569 it for later use. Do not use the label after the loop, if any, since
570 it might be used by insns outside the loop, or there might be insns
571 added before it later by final_[bg]iv_value which must be after
572 the real exit label. */
573 exit_label = gen_label_rtx ();
574
575 insn = loop_start;
576 while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
577 insn = NEXT_INSN (insn);
578
579 if (GET_CODE (insn) == JUMP_INSN)
580 {
581 /* The loop starts with a jump down to the exit condition test.
582 Start copying the loop after the barrier following this
583 jump insn. */
584 copy_start = NEXT_INSN (insn);
585
586 /* Splitting induction variables doesn't work when the loop is
587 entered via a jump to the bottom, because then we end up doing
588 a comparison against a new register for a split variable, but
589 we did not execute the set insn for the new register because
590 it was skipped over. */
591 splitting_not_safe = 1;
592 if (loop_dump_stream)
593 fprintf (loop_dump_stream,
594 "Splitting not safe, because loop not entered at top.\n");
595 }
596 else
597 copy_start = loop_start;
598 }
599
600 /* This should always be the first label in the loop. */
601 start_label = NEXT_INSN (copy_start);
602 /* There may be a line number note and/or a loop continue note here. */
603 while (GET_CODE (start_label) == NOTE)
604 start_label = NEXT_INSN (start_label);
605 if (GET_CODE (start_label) != CODE_LABEL)
606 {
607 /* This can happen as a result of jump threading. If the first insns in
608 the loop test the same condition as the loop's backward jump, or the
609 opposite condition, then the backward jump will be modified to point
610 to elsewhere, and the loop's start label is deleted.
611
612 This case currently can not be handled by the loop unrolling code. */
613
614 if (loop_dump_stream)
615 fprintf (loop_dump_stream,
616 "Unrolling failure: unknown insns between BEG note and loop label.\n");
617 return;
618 }
619 if (LABEL_NAME (start_label))
620 {
621 /* The jump optimization pass must have combined the original start label
622 with a named label for a goto. We can't unroll this case because
623 jumps which go to the named label must be handled differently than
624 jumps to the loop start, and it is impossible to differentiate them
625 in this case. */
626 if (loop_dump_stream)
627 fprintf (loop_dump_stream,
628 "Unrolling failure: loop start label is gone\n");
629 return;
630 }
631
632 if (unroll_type == UNROLL_NAIVE
633 && GET_CODE (last_loop_insn) == BARRIER
634 && start_label != JUMP_LABEL (PREV_INSN (last_loop_insn)))
635 {
636 /* In this case, we must copy the jump and barrier, because they will
637 not be converted to jumps to an immediately following label. */
638
639 insert_before = NEXT_INSN (last_loop_insn);
640 copy_end = last_loop_insn;
641 }
642
643 if (unroll_type == UNROLL_NAIVE
644 && GET_CODE (last_loop_insn) == JUMP_INSN
645 && start_label != JUMP_LABEL (last_loop_insn))
646 {
647 /* ??? The loop ends with a conditional branch that does not branch back
648 to the loop start label. In this case, we must emit an unconditional
649 branch to the loop exit after emitting the final branch.
650 copy_loop_body does not have support for this currently, so we
651 give up. It doesn't seem worthwhile to unroll anyways since
652 unrolling would increase the number of branch instructions
653 executed. */
654 if (loop_dump_stream)
655 fprintf (loop_dump_stream,
656 "Unrolling failure: final conditional branch not to loop start\n");
657 return;
658 }
659
660 /* Allocate a translation table for the labels and insn numbers.
661 They will be filled in as we copy the insns in the loop. */
662
663 max_labelno = max_label_num ();
664 max_insnno = get_max_uid ();
665
666 map = (struct inline_remap *) alloca (sizeof (struct inline_remap));
667
668 map->integrating = 0;
669
670 /* Allocate the label map. */
671
672 if (max_labelno > 0)
673 {
674 map->label_map = (rtx *) alloca (max_labelno * sizeof (rtx));
675
676 local_label = (char *) alloca (max_labelno);
677 bzero (local_label, max_labelno);
678 }
679 else
680 map->label_map = 0;
681
682 /* Search the loop and mark all local labels, i.e. the ones which have to
683 be distinct labels when copied. For all labels which might be
684 non-local, set their label_map entries to point to themselves.
685 If they happen to be local their label_map entries will be overwritten
686 before the loop body is copied. The label_map entries for local labels
687 will be set to a different value each time the loop body is copied. */
688
689 for (insn = copy_start; insn != loop_end; insn = NEXT_INSN (insn))
690 {
691 if (GET_CODE (insn) == CODE_LABEL)
692 local_label[CODE_LABEL_NUMBER (insn)] = 1;
693 else if (GET_CODE (insn) == JUMP_INSN)
694 {
695 if (JUMP_LABEL (insn))
696 set_label_in_map (map,
697 CODE_LABEL_NUMBER (JUMP_LABEL (insn)),
698 JUMP_LABEL (insn));
699 else if (GET_CODE (PATTERN (insn)) == ADDR_VEC
700 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
701 {
702 rtx pat = PATTERN (insn);
703 int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
704 int len = XVECLEN (pat, diff_vec_p);
705 rtx label;
706
707 for (i = 0; i < len; i++)
708 {
709 label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
710 set_label_in_map (map,
711 CODE_LABEL_NUMBER (label),
712 label);
713 }
714 }
715 }
716 }
717
718 /* Allocate space for the insn map. */
719
720 map->insn_map = (rtx *) alloca (max_insnno * sizeof (rtx));
721
722 /* Set this to zero, to indicate that we are doing loop unrolling,
723 not function inlining. */
724 map->inline_target = 0;
725
726 /* The register and constant maps depend on the number of registers
727 present, so the final maps can't be created until after
728 find_splittable_regs is called. However, they are needed for
729 preconditioning, so we create temporary maps when preconditioning
730 is performed. */
731
732 /* The preconditioning code may allocate two new pseudo registers. */
733 maxregnum = max_reg_num ();
734
735 /* Allocate and zero out the splittable_regs and addr_combined_regs
736 arrays. These must be zeroed here because they will be used if
737 loop preconditioning is performed, and must be zero for that case.
738
739 It is safe to do this here, since the extra registers created by the
740 preconditioning code and find_splittable_regs will never be used
741 to access the splittable_regs[] and addr_combined_regs[] arrays. */
742
743 splittable_regs = (rtx *) alloca (maxregnum * sizeof (rtx));
744 bzero ((char *) splittable_regs, maxregnum * sizeof (rtx));
745 splittable_regs_updates = (int *) alloca (maxregnum * sizeof (int));
746 bzero ((char *) splittable_regs_updates, maxregnum * sizeof (int));
747 addr_combined_regs
748 = (struct induction **) alloca (maxregnum * sizeof (struct induction *));
749 bzero ((char *) addr_combined_regs, maxregnum * sizeof (struct induction *));
750 /* We must limit it to max_reg_before_loop, because only these pseudo
751 registers have valid regno_first_uid info. Any register created after
752 that is unlikely to be local to the loop anyways. */
753 local_regno = (char *) alloca (max_reg_before_loop);
754 bzero (local_regno, max_reg_before_loop);
755
756 /* Mark all local registers, i.e. the ones which are referenced only
757 inside the loop. */
758 if (INSN_UID (copy_end) < max_uid_for_loop)
759 {
760 int copy_start_luid = INSN_LUID (copy_start);
761 int copy_end_luid = INSN_LUID (copy_end);
762
763 /* If a register is used in the jump insn, we must not duplicate it
764 since it will also be used outside the loop. */
765 if (GET_CODE (copy_end) == JUMP_INSN)
766 copy_end_luid--;
767 /* If copy_start points to the NOTE that starts the loop, then we must
768 use the next luid, because invariant pseudo-regs moved out of the loop
769 have their lifetimes modified to start here, but they are not safe
770 to duplicate. */
771 if (copy_start == loop_start)
772 copy_start_luid++;
773
774 /* If a pseudo's lifetime is entirely contained within this loop, then we
775 can use a different pseudo in each unrolled copy of the loop. This
776 results in better code. */
777 for (j = FIRST_PSEUDO_REGISTER; j < max_reg_before_loop; ++j)
778 if (REGNO_FIRST_UID (j) > 0 && REGNO_FIRST_UID (j) <= max_uid_for_loop
779 && uid_luid[REGNO_FIRST_UID (j)] >= copy_start_luid
780 && REGNO_LAST_UID (j) > 0 && REGNO_LAST_UID (j) <= max_uid_for_loop
781 && uid_luid[REGNO_LAST_UID (j)] <= copy_end_luid)
782 {
783 /* However, we must also check for loop-carried dependencies.
784 If the value the pseudo has at the end of iteration X is
785 used by iteration X+1, then we can not use a different pseudo
786 for each unrolled copy of the loop. */
787 /* A pseudo is safe if regno_first_uid is a set, and this
788 set dominates all instructions from regno_first_uid to
789 regno_last_uid. */
790 /* ??? This check is simplistic. We would get better code if
791 this check was more sophisticated. */
792 if (set_dominates_use (j, REGNO_FIRST_UID (j), REGNO_LAST_UID (j),
793 copy_start, copy_end))
794 local_regno[j] = 1;
795
796 if (loop_dump_stream)
797 {
798 if (local_regno[j])
799 fprintf (loop_dump_stream, "Marked reg %d as local\n", j);
800 else
801 fprintf (loop_dump_stream, "Did not mark reg %d as local\n",
802 j);
803 }
804 }
805 }
806
807 /* If this loop requires exit tests when unrolled, check to see if we
808 can precondition the loop so as to make the exit tests unnecessary.
809 Just like variable splitting, this is not safe if the loop is entered
810 via a jump to the bottom. Also, can not do this if no strength
811 reduce info, because precondition_loop_p uses this info. */
812
813 /* Must copy the loop body for preconditioning before the following
814 find_splittable_regs call since that will emit insns which need to
815 be after the preconditioned loop copies, but immediately before the
816 unrolled loop copies. */
817
818 /* Also, it is not safe to split induction variables for the preconditioned
819 copies of the loop body. If we split induction variables, then the code
820 assumes that each induction variable can be represented as a function
821 of its initial value and the loop iteration number. This is not true
822 in this case, because the last preconditioned copy of the loop body
823 could be any iteration from the first up to the `unroll_number-1'th,
824 depending on the initial value of the iteration variable. Therefore
825 we can not split induction variables here, because we can not calculate
826 their value. Hence, this code must occur before find_splittable_regs
827 is called. */
828
829 if (unroll_type == UNROLL_NAIVE && ! splitting_not_safe && strength_reduce_p)
830 {
831 rtx initial_value, final_value, increment;
832
833 if (precondition_loop_p (&initial_value, &final_value, &increment,
834 loop_start, loop_end))
835 {
836 register rtx diff ;
837 enum machine_mode mode;
838 rtx *labels;
839 int abs_inc, neg_inc;
840
841 map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
842
843 map->const_equiv_map = (rtx *) alloca (maxregnum * sizeof (rtx));
844 map->const_age_map = (unsigned *) alloca (maxregnum
845 * sizeof (unsigned));
846 map->const_equiv_map_size = maxregnum;
847 global_const_equiv_map = map->const_equiv_map;
848 global_const_equiv_map_size = maxregnum;
849
850 init_reg_map (map, maxregnum);
851
852 /* Limit loop unrolling to 4, since this will make 7 copies of
853 the loop body. */
854 if (unroll_number > 4)
855 unroll_number = 4;
856
857 /* Save the absolute value of the increment, and also whether or
858 not it is negative. */
859 neg_inc = 0;
860 abs_inc = INTVAL (increment);
861 if (abs_inc < 0)
862 {
863 abs_inc = - abs_inc;
864 neg_inc = 1;
865 }
866
867 start_sequence ();
868
869 /* Decide what mode to do these calculations in. Choose the larger
870 of final_value's mode and initial_value's mode, or a full-word if
871 both are constants. */
872 mode = GET_MODE (final_value);
873 if (mode == VOIDmode)
874 {
875 mode = GET_MODE (initial_value);
876 if (mode == VOIDmode)
877 mode = word_mode;
878 }
879 else if (mode != GET_MODE (initial_value)
880 && (GET_MODE_SIZE (mode)
881 < GET_MODE_SIZE (GET_MODE (initial_value))))
882 mode = GET_MODE (initial_value);
883
884 /* Calculate the difference between the final and initial values.
885 Final value may be a (plus (reg x) (const_int 1)) rtx.
886 Let the following cse pass simplify this if initial value is
887 a constant.
888
889 We must copy the final and initial values here to avoid
890 improperly shared rtl. */
891
892 diff = expand_binop (mode, sub_optab, copy_rtx (final_value),
893 copy_rtx (initial_value), NULL_RTX, 0,
894 OPTAB_LIB_WIDEN);
895
896 /* Now calculate (diff % (unroll * abs (increment))) by using an
897 and instruction. */
898 diff = expand_binop (GET_MODE (diff), and_optab, diff,
899 GEN_INT (unroll_number * abs_inc - 1),
900 NULL_RTX, 0, OPTAB_LIB_WIDEN);
901
902 /* Now emit a sequence of branches to jump to the proper precond
903 loop entry point. */
904
905 labels = (rtx *) alloca (sizeof (rtx) * unroll_number);
906 for (i = 0; i < unroll_number; i++)
907 labels[i] = gen_label_rtx ();
908
909 /* Check for the case where the initial value is greater than or
910 equal to the final value. In that case, we want to execute
911 exactly one loop iteration. The code below will fail for this
912 case. This check does not apply if the loop has a NE
913 comparison at the end. */
914
915 if (loop_comparison_code != NE)
916 {
917 emit_cmp_insn (initial_value, final_value, neg_inc ? LE : GE,
918 NULL_RTX, mode, 0, 0);
919 if (neg_inc)
920 emit_jump_insn (gen_ble (labels[1]));
921 else
922 emit_jump_insn (gen_bge (labels[1]));
923 JUMP_LABEL (get_last_insn ()) = labels[1];
924 LABEL_NUSES (labels[1])++;
925 }
926
927 /* Assuming the unroll_number is 4, and the increment is 2, then
928 for a negative increment: for a positive increment:
929 diff = 0,1 precond 0 diff = 0,7 precond 0
930 diff = 2,3 precond 3 diff = 1,2 precond 1
931 diff = 4,5 precond 2 diff = 3,4 precond 2
932 diff = 6,7 precond 1 diff = 5,6 precond 3 */
933
934 /* We only need to emit (unroll_number - 1) branches here, the
935 last case just falls through to the following code. */
936
937 /* ??? This would give better code if we emitted a tree of branches
938 instead of the current linear list of branches. */
939
940 for (i = 0; i < unroll_number - 1; i++)
941 {
942 int cmp_const;
943 enum rtx_code cmp_code;
944
945 /* For negative increments, must invert the constant compared
946 against, except when comparing against zero. */
947 if (i == 0)
948 {
949 cmp_const = 0;
950 cmp_code = EQ;
951 }
952 else if (neg_inc)
953 {
954 cmp_const = unroll_number - i;
955 cmp_code = GE;
956 }
957 else
958 {
959 cmp_const = i;
960 cmp_code = LE;
961 }
962
963 emit_cmp_insn (diff, GEN_INT (abs_inc * cmp_const),
964 cmp_code, NULL_RTX, mode, 0, 0);
965
966 if (i == 0)
967 emit_jump_insn (gen_beq (labels[i]));
968 else if (neg_inc)
969 emit_jump_insn (gen_bge (labels[i]));
970 else
971 emit_jump_insn (gen_ble (labels[i]));
972 JUMP_LABEL (get_last_insn ()) = labels[i];
973 LABEL_NUSES (labels[i])++;
974 }
975
976 /* If the increment is greater than one, then we need another branch,
977 to handle other cases equivalent to 0. */
978
979 /* ??? This should be merged into the code above somehow to help
980 simplify the code here, and reduce the number of branches emitted.
981 For the negative increment case, the branch here could easily
982 be merged with the `0' case branch above. For the positive
983 increment case, it is not clear how this can be simplified. */
984
985 if (abs_inc != 1)
986 {
987 int cmp_const;
988 enum rtx_code cmp_code;
989
990 if (neg_inc)
991 {
992 cmp_const = abs_inc - 1;
993 cmp_code = LE;
994 }
995 else
996 {
997 cmp_const = abs_inc * (unroll_number - 1) + 1;
998 cmp_code = GE;
999 }
1000
1001 emit_cmp_insn (diff, GEN_INT (cmp_const), cmp_code, NULL_RTX,
1002 mode, 0, 0);
1003
1004 if (neg_inc)
1005 emit_jump_insn (gen_ble (labels[0]));
1006 else
1007 emit_jump_insn (gen_bge (labels[0]));
1008 JUMP_LABEL (get_last_insn ()) = labels[0];
1009 LABEL_NUSES (labels[0])++;
1010 }
1011
1012 sequence = gen_sequence ();
1013 end_sequence ();
1014 emit_insn_before (sequence, loop_start);
1015
1016 /* Only the last copy of the loop body here needs the exit
1017 test, so set copy_end to exclude the compare/branch here,
1018 and then reset it inside the loop when get to the last
1019 copy. */
1020
1021 if (GET_CODE (last_loop_insn) == BARRIER)
1022 copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
1023 else if (GET_CODE (last_loop_insn) == JUMP_INSN)
1024 {
1025 #ifdef HAVE_cc0
1026 /* The immediately preceding insn is a compare which we do not
1027 want to copy. */
1028 copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
1029 #else
1030 /* The immediately preceding insn may not be a compare, so we
1031 must copy it. */
1032 copy_end = PREV_INSN (last_loop_insn);
1033 #endif
1034 }
1035 else
1036 abort ();
1037
1038 for (i = 1; i < unroll_number; i++)
1039 {
1040 emit_label_after (labels[unroll_number - i],
1041 PREV_INSN (loop_start));
1042
1043 bzero ((char *) map->insn_map, max_insnno * sizeof (rtx));
1044 bzero ((char *) map->const_equiv_map, maxregnum * sizeof (rtx));
1045 bzero ((char *) map->const_age_map,
1046 maxregnum * sizeof (unsigned));
1047 map->const_age = 0;
1048
1049 for (j = 0; j < max_labelno; j++)
1050 if (local_label[j])
1051 set_label_in_map (map, j, gen_label_rtx ());
1052
1053 for (j = FIRST_PSEUDO_REGISTER; j < max_reg_before_loop; j++)
1054 if (local_regno[j])
1055 {
1056 map->reg_map[j] = gen_reg_rtx (GET_MODE (regno_reg_rtx[j]));
1057 record_base_value (REGNO (map->reg_map[j]),
1058 regno_reg_rtx[j], 0);
1059 }
1060 /* The last copy needs the compare/branch insns at the end,
1061 so reset copy_end here if the loop ends with a conditional
1062 branch. */
1063
1064 if (i == unroll_number - 1)
1065 {
1066 if (GET_CODE (last_loop_insn) == BARRIER)
1067 copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
1068 else
1069 copy_end = last_loop_insn;
1070 }
1071
1072 /* None of the copies are the `last_iteration', so just
1073 pass zero for that parameter. */
1074 copy_loop_body (copy_start, copy_end, map, exit_label, 0,
1075 unroll_type, start_label, loop_end,
1076 loop_start, copy_end);
1077 }
1078 emit_label_after (labels[0], PREV_INSN (loop_start));
1079
1080 if (GET_CODE (last_loop_insn) == BARRIER)
1081 {
1082 insert_before = PREV_INSN (last_loop_insn);
1083 copy_end = PREV_INSN (insert_before);
1084 }
1085 else
1086 {
1087 #ifdef HAVE_cc0
1088 /* The immediately preceding insn is a compare which we do not
1089 want to copy. */
1090 insert_before = PREV_INSN (last_loop_insn);
1091 copy_end = PREV_INSN (insert_before);
1092 #else
1093 /* The immediately preceding insn may not be a compare, so we
1094 must copy it. */
1095 insert_before = last_loop_insn;
1096 copy_end = PREV_INSN (last_loop_insn);
1097 #endif
1098 }
1099
1100 /* Set unroll type to MODULO now. */
1101 unroll_type = UNROLL_MODULO;
1102 loop_preconditioned = 1;
1103
1104 #ifdef HAIFA
1105 /* Fix the initial value for the loop as needed. */
1106 if (loop_n_iterations <= 0)
1107 loop_start_value [uid_loop_num [INSN_UID (loop_start)]]
1108 = initial_value;
1109 #endif
1110 }
1111 }
1112
1113 /* If reach here, and the loop type is UNROLL_NAIVE, then don't unroll
1114 the loop unless all loops are being unrolled. */
1115 if (unroll_type == UNROLL_NAIVE && ! flag_unroll_all_loops)
1116 {
1117 if (loop_dump_stream)
1118 fprintf (loop_dump_stream, "Unrolling failure: Naive unrolling not being done.\n");
1119 return;
1120 }
1121
1122 /* At this point, we are guaranteed to unroll the loop. */
1123
1124 /* Keep track of the unroll factor for each loop. */
1125 if (unroll_type == UNROLL_COMPLETELY)
1126 loop_unroll_factor [uid_loop_num [INSN_UID (loop_start)]] = -1;
1127 else
1128 loop_unroll_factor [uid_loop_num [INSN_UID (loop_start)]] = unroll_number;
1129
1130
1131 /* For each biv and giv, determine whether it can be safely split into
1132 a different variable for each unrolled copy of the loop body.
1133 We precalculate and save this info here, since computing it is
1134 expensive.
1135
1136 Do this before deleting any instructions from the loop, so that
1137 back_branch_in_range_p will work correctly. */
1138
1139 if (splitting_not_safe)
1140 temp = 0;
1141 else
1142 temp = find_splittable_regs (unroll_type, loop_start, loop_end,
1143 end_insert_before, unroll_number);
1144
1145 /* find_splittable_regs may have created some new registers, so must
1146 reallocate the reg_map with the new larger size, and must realloc
1147 the constant maps also. */
1148
1149 maxregnum = max_reg_num ();
1150 map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
1151
1152 init_reg_map (map, maxregnum);
1153
1154 /* Space is needed in some of the map for new registers, so new_maxregnum
1155 is an (over)estimate of how many registers will exist at the end. */
1156 new_maxregnum = maxregnum + (temp * unroll_number * 2);
1157
1158 /* Must realloc space for the constant maps, because the number of registers
1159 may have changed. */
1160
1161 map->const_equiv_map = (rtx *) alloca (new_maxregnum * sizeof (rtx));
1162 map->const_age_map = (unsigned *) alloca (new_maxregnum * sizeof (unsigned));
1163
1164 map->const_equiv_map_size = new_maxregnum;
1165 global_const_equiv_map = map->const_equiv_map;
1166 global_const_equiv_map_size = new_maxregnum;
1167
1168 /* Search the list of bivs and givs to find ones which need to be remapped
1169 when split, and set their reg_map entry appropriately. */
1170
1171 for (bl = loop_iv_list; bl; bl = bl->next)
1172 {
1173 if (REGNO (bl->biv->src_reg) != bl->regno)
1174 map->reg_map[bl->regno] = bl->biv->src_reg;
1175 #if 0
1176 /* Currently, non-reduced/final-value givs are never split. */
1177 for (v = bl->giv; v; v = v->next_iv)
1178 if (REGNO (v->src_reg) != bl->regno)
1179 map->reg_map[REGNO (v->dest_reg)] = v->src_reg;
1180 #endif
1181 }
1182
1183 /* Use our current register alignment and pointer flags. */
1184 map->regno_pointer_flag = regno_pointer_flag;
1185 map->regno_pointer_align = regno_pointer_align;
1186
1187 /* If the loop is being partially unrolled, and the iteration variables
1188 are being split, and are being renamed for the split, then must fix up
1189 the compare/jump instruction at the end of the loop to refer to the new
1190 registers. This compare isn't copied, so the registers used in it
1191 will never be replaced if it isn't done here. */
1192
1193 if (unroll_type == UNROLL_MODULO)
1194 {
1195 insn = NEXT_INSN (copy_end);
1196 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1197 PATTERN (insn) = remap_split_bivs (PATTERN (insn));
1198 }
1199
1200 /* For unroll_number - 1 times, make a copy of each instruction
1201 between copy_start and copy_end, and insert these new instructions
1202 before the end of the loop. */
1203
1204 for (i = 0; i < unroll_number; i++)
1205 {
1206 bzero ((char *) map->insn_map, max_insnno * sizeof (rtx));
1207 bzero ((char *) map->const_equiv_map, new_maxregnum * sizeof (rtx));
1208 bzero ((char *) map->const_age_map, new_maxregnum * sizeof (unsigned));
1209 map->const_age = 0;
1210
1211 for (j = 0; j < max_labelno; j++)
1212 if (local_label[j])
1213 set_label_in_map (map, j, gen_label_rtx ());
1214
1215 for (j = FIRST_PSEUDO_REGISTER; j < max_reg_before_loop; j++)
1216 if (local_regno[j])
1217 {
1218 map->reg_map[j] = gen_reg_rtx (GET_MODE (regno_reg_rtx[j]));
1219 record_base_value (REGNO (map->reg_map[j]),
1220 regno_reg_rtx[j], 0);
1221 }
1222
1223 /* If loop starts with a branch to the test, then fix it so that
1224 it points to the test of the first unrolled copy of the loop. */
1225 if (i == 0 && loop_start != copy_start)
1226 {
1227 insn = PREV_INSN (copy_start);
1228 pattern = PATTERN (insn);
1229
1230 tem = get_label_from_map (map,
1231 CODE_LABEL_NUMBER
1232 (XEXP (SET_SRC (pattern), 0)));
1233 SET_SRC (pattern) = gen_rtx_LABEL_REF (VOIDmode, tem);
1234
1235 /* Set the jump label so that it can be used by later loop unrolling
1236 passes. */
1237 JUMP_LABEL (insn) = tem;
1238 LABEL_NUSES (tem)++;
1239 }
1240
1241 copy_loop_body (copy_start, copy_end, map, exit_label,
1242 i == unroll_number - 1, unroll_type, start_label,
1243 loop_end, insert_before, insert_before);
1244 }
1245
1246 /* Before deleting any insns, emit a CODE_LABEL immediately after the last
1247 insn to be deleted. This prevents any runaway delete_insn call from
1248 more insns that it should, as it always stops at a CODE_LABEL. */
1249
1250 /* Delete the compare and branch at the end of the loop if completely
1251 unrolling the loop. Deleting the backward branch at the end also
1252 deletes the code label at the start of the loop. This is done at
1253 the very end to avoid problems with back_branch_in_range_p. */
1254
1255 if (unroll_type == UNROLL_COMPLETELY)
1256 safety_label = emit_label_after (gen_label_rtx (), last_loop_insn);
1257 else
1258 safety_label = emit_label_after (gen_label_rtx (), copy_end);
1259
1260 /* Delete all of the original loop instructions. Don't delete the
1261 LOOP_BEG note, or the first code label in the loop. */
1262
1263 insn = NEXT_INSN (copy_start);
1264 while (insn != safety_label)
1265 {
1266 if (insn != start_label)
1267 insn = delete_insn (insn);
1268 else
1269 insn = NEXT_INSN (insn);
1270 }
1271
1272 /* Can now delete the 'safety' label emitted to protect us from runaway
1273 delete_insn calls. */
1274 if (INSN_DELETED_P (safety_label))
1275 abort ();
1276 delete_insn (safety_label);
1277
1278 /* If exit_label exists, emit it after the loop. Doing the emit here
1279 forces it to have a higher INSN_UID than any insn in the unrolled loop.
1280 This is needed so that mostly_true_jump in reorg.c will treat jumps
1281 to this loop end label correctly, i.e. predict that they are usually
1282 not taken. */
1283 if (exit_label)
1284 emit_label_after (exit_label, loop_end);
1285 }
1286 \f
1287 /* Return true if the loop can be safely, and profitably, preconditioned
1288 so that the unrolled copies of the loop body don't need exit tests.
1289
1290 This only works if final_value, initial_value and increment can be
1291 determined, and if increment is a constant power of 2.
1292 If increment is not a power of 2, then the preconditioning modulo
1293 operation would require a real modulo instead of a boolean AND, and this
1294 is not considered `profitable'. */
1295
1296 /* ??? If the loop is known to be executed very many times, or the machine
1297 has a very cheap divide instruction, then preconditioning is a win even
1298 when the increment is not a power of 2. Use RTX_COST to compute
1299 whether divide is cheap. */
1300
1301 static int
1302 precondition_loop_p (initial_value, final_value, increment, loop_start,
1303 loop_end)
1304 rtx *initial_value, *final_value, *increment;
1305 rtx loop_start, loop_end;
1306 {
1307
1308 if (loop_n_iterations > 0)
1309 {
1310 *initial_value = const0_rtx;
1311 *increment = const1_rtx;
1312 *final_value = GEN_INT (loop_n_iterations);
1313
1314 if (loop_dump_stream)
1315 fprintf (loop_dump_stream,
1316 "Preconditioning: Success, number of iterations known, %d.\n",
1317 loop_n_iterations);
1318 return 1;
1319 }
1320
1321 if (loop_initial_value == 0)
1322 {
1323 if (loop_dump_stream)
1324 fprintf (loop_dump_stream,
1325 "Preconditioning: Could not find initial value.\n");
1326 return 0;
1327 }
1328 else if (loop_increment == 0)
1329 {
1330 if (loop_dump_stream)
1331 fprintf (loop_dump_stream,
1332 "Preconditioning: Could not find increment value.\n");
1333 return 0;
1334 }
1335 else if (GET_CODE (loop_increment) != CONST_INT)
1336 {
1337 if (loop_dump_stream)
1338 fprintf (loop_dump_stream,
1339 "Preconditioning: Increment not a constant.\n");
1340 return 0;
1341 }
1342 else if ((exact_log2 (INTVAL (loop_increment)) < 0)
1343 && (exact_log2 (- INTVAL (loop_increment)) < 0))
1344 {
1345 if (loop_dump_stream)
1346 fprintf (loop_dump_stream,
1347 "Preconditioning: Increment not a constant power of 2.\n");
1348 return 0;
1349 }
1350
1351 /* Unsigned_compare and compare_dir can be ignored here, since they do
1352 not matter for preconditioning. */
1353
1354 if (loop_final_value == 0)
1355 {
1356 if (loop_dump_stream)
1357 fprintf (loop_dump_stream,
1358 "Preconditioning: EQ comparison loop.\n");
1359 return 0;
1360 }
1361
1362 /* Must ensure that final_value is invariant, so call invariant_p to
1363 check. Before doing so, must check regno against max_reg_before_loop
1364 to make sure that the register is in the range covered by invariant_p.
1365 If it isn't, then it is most likely a biv/giv which by definition are
1366 not invariant. */
1367 if ((GET_CODE (loop_final_value) == REG
1368 && REGNO (loop_final_value) >= max_reg_before_loop)
1369 || (GET_CODE (loop_final_value) == PLUS
1370 && REGNO (XEXP (loop_final_value, 0)) >= max_reg_before_loop)
1371 || ! invariant_p (loop_final_value))
1372 {
1373 if (loop_dump_stream)
1374 fprintf (loop_dump_stream,
1375 "Preconditioning: Final value not invariant.\n");
1376 return 0;
1377 }
1378
1379 /* Fail for floating point values, since the caller of this function
1380 does not have code to deal with them. */
1381 if (GET_MODE_CLASS (GET_MODE (loop_final_value)) == MODE_FLOAT
1382 || GET_MODE_CLASS (GET_MODE (loop_initial_value)) == MODE_FLOAT)
1383 {
1384 if (loop_dump_stream)
1385 fprintf (loop_dump_stream,
1386 "Preconditioning: Floating point final or initial value.\n");
1387 return 0;
1388 }
1389
1390 /* Now set initial_value to be the iteration_var, since that may be a
1391 simpler expression, and is guaranteed to be correct if all of the
1392 above tests succeed.
1393
1394 We can not use the initial_value as calculated, because it will be
1395 one too small for loops of the form "while (i-- > 0)". We can not
1396 emit code before the loop_skip_over insns to fix this problem as this
1397 will then give a number one too large for loops of the form
1398 "while (--i > 0)".
1399
1400 Note that all loops that reach here are entered at the top, because
1401 this function is not called if the loop starts with a jump. */
1402
1403 /* Fail if loop_iteration_var is not live before loop_start, since we need
1404 to test its value in the preconditioning code. */
1405
1406 if (uid_luid[REGNO_FIRST_UID (REGNO (loop_iteration_var))]
1407 > INSN_LUID (loop_start))
1408 {
1409 if (loop_dump_stream)
1410 fprintf (loop_dump_stream,
1411 "Preconditioning: Iteration var not live before loop start.\n");
1412 return 0;
1413 }
1414
1415 *initial_value = loop_iteration_var;
1416 *increment = loop_increment;
1417 *final_value = loop_final_value;
1418
1419 /* Success! */
1420 if (loop_dump_stream)
1421 fprintf (loop_dump_stream, "Preconditioning: Successful.\n");
1422 return 1;
1423 }
1424
1425
1426 /* All pseudo-registers must be mapped to themselves. Two hard registers
1427 must be mapped, VIRTUAL_STACK_VARS_REGNUM and VIRTUAL_INCOMING_ARGS_
1428 REGNUM, to avoid function-inlining specific conversions of these
1429 registers. All other hard regs can not be mapped because they may be
1430 used with different
1431 modes. */
1432
1433 static void
1434 init_reg_map (map, maxregnum)
1435 struct inline_remap *map;
1436 int maxregnum;
1437 {
1438 int i;
1439
1440 for (i = maxregnum - 1; i > LAST_VIRTUAL_REGISTER; i--)
1441 map->reg_map[i] = regno_reg_rtx[i];
1442 /* Just clear the rest of the entries. */
1443 for (i = LAST_VIRTUAL_REGISTER; i >= 0; i--)
1444 map->reg_map[i] = 0;
1445
1446 map->reg_map[VIRTUAL_STACK_VARS_REGNUM]
1447 = regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM];
1448 map->reg_map[VIRTUAL_INCOMING_ARGS_REGNUM]
1449 = regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM];
1450 }
1451 \f
1452 /* Strength-reduction will often emit code for optimized biv/givs which
1453 calculates their value in a temporary register, and then copies the result
1454 to the iv. This procedure reconstructs the pattern computing the iv;
1455 verifying that all operands are of the proper form.
1456
1457 PATTERN must be the result of single_set.
1458 The return value is the amount that the giv is incremented by. */
1459
1460 static rtx
1461 calculate_giv_inc (pattern, src_insn, regno)
1462 rtx pattern, src_insn;
1463 int regno;
1464 {
1465 rtx increment;
1466 rtx increment_total = 0;
1467 int tries = 0;
1468
1469 retry:
1470 /* Verify that we have an increment insn here. First check for a plus
1471 as the set source. */
1472 if (GET_CODE (SET_SRC (pattern)) != PLUS)
1473 {
1474 /* SR sometimes computes the new giv value in a temp, then copies it
1475 to the new_reg. */
1476 src_insn = PREV_INSN (src_insn);
1477 pattern = PATTERN (src_insn);
1478 if (GET_CODE (SET_SRC (pattern)) != PLUS)
1479 abort ();
1480
1481 /* The last insn emitted is not needed, so delete it to avoid confusing
1482 the second cse pass. This insn sets the giv unnecessarily. */
1483 delete_insn (get_last_insn ());
1484 }
1485
1486 /* Verify that we have a constant as the second operand of the plus. */
1487 increment = XEXP (SET_SRC (pattern), 1);
1488 if (GET_CODE (increment) != CONST_INT)
1489 {
1490 /* SR sometimes puts the constant in a register, especially if it is
1491 too big to be an add immed operand. */
1492 src_insn = PREV_INSN (src_insn);
1493 increment = SET_SRC (PATTERN (src_insn));
1494
1495 /* SR may have used LO_SUM to compute the constant if it is too large
1496 for a load immed operand. In this case, the constant is in operand
1497 one of the LO_SUM rtx. */
1498 if (GET_CODE (increment) == LO_SUM)
1499 increment = XEXP (increment, 1);
1500
1501 /* Some ports store large constants in memory and add a REG_EQUAL
1502 note to the store insn. */
1503 else if (GET_CODE (increment) == MEM)
1504 {
1505 rtx note = find_reg_note (src_insn, REG_EQUAL, 0);
1506 if (note)
1507 increment = XEXP (note, 0);
1508 }
1509
1510 else if (GET_CODE (increment) == IOR
1511 || GET_CODE (increment) == ASHIFT
1512 || GET_CODE (increment) == PLUS)
1513 {
1514 /* The rs6000 port loads some constants with IOR.
1515 The alpha port loads some constants with ASHIFT and PLUS. */
1516 rtx second_part = XEXP (increment, 1);
1517 enum rtx_code code = GET_CODE (increment);
1518
1519 src_insn = PREV_INSN (src_insn);
1520 increment = SET_SRC (PATTERN (src_insn));
1521 /* Don't need the last insn anymore. */
1522 delete_insn (get_last_insn ());
1523
1524 if (GET_CODE (second_part) != CONST_INT
1525 || GET_CODE (increment) != CONST_INT)
1526 abort ();
1527
1528 if (code == IOR)
1529 increment = GEN_INT (INTVAL (increment) | INTVAL (second_part));
1530 else if (code == PLUS)
1531 increment = GEN_INT (INTVAL (increment) + INTVAL (second_part));
1532 else
1533 increment = GEN_INT (INTVAL (increment) << INTVAL (second_part));
1534 }
1535
1536 if (GET_CODE (increment) != CONST_INT)
1537 abort ();
1538
1539 /* The insn loading the constant into a register is no longer needed,
1540 so delete it. */
1541 delete_insn (get_last_insn ());
1542 }
1543
1544 if (increment_total)
1545 increment_total = GEN_INT (INTVAL (increment_total) + INTVAL (increment));
1546 else
1547 increment_total = increment;
1548
1549 /* Check that the source register is the same as the register we expected
1550 to see as the source. If not, something is seriously wrong. */
1551 if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
1552 || REGNO (XEXP (SET_SRC (pattern), 0)) != regno)
1553 {
1554 /* Some machines (e.g. the romp), may emit two add instructions for
1555 certain constants, so lets try looking for another add immediately
1556 before this one if we have only seen one add insn so far. */
1557
1558 if (tries == 0)
1559 {
1560 tries++;
1561
1562 src_insn = PREV_INSN (src_insn);
1563 pattern = PATTERN (src_insn);
1564
1565 delete_insn (get_last_insn ());
1566
1567 goto retry;
1568 }
1569
1570 abort ();
1571 }
1572
1573 return increment_total;
1574 }
1575
1576 /* Copy REG_NOTES, except for insn references, because not all insn_map
1577 entries are valid yet. We do need to copy registers now though, because
1578 the reg_map entries can change during copying. */
1579
1580 static rtx
1581 initial_reg_note_copy (notes, map)
1582 rtx notes;
1583 struct inline_remap *map;
1584 {
1585 rtx copy;
1586
1587 if (notes == 0)
1588 return 0;
1589
1590 copy = rtx_alloc (GET_CODE (notes));
1591 PUT_MODE (copy, GET_MODE (notes));
1592
1593 if (GET_CODE (notes) == EXPR_LIST)
1594 XEXP (copy, 0) = copy_rtx_and_substitute (XEXP (notes, 0), map);
1595 else if (GET_CODE (notes) == INSN_LIST)
1596 /* Don't substitute for these yet. */
1597 XEXP (copy, 0) = XEXP (notes, 0);
1598 else
1599 abort ();
1600
1601 XEXP (copy, 1) = initial_reg_note_copy (XEXP (notes, 1), map);
1602
1603 return copy;
1604 }
1605
1606 /* Fixup insn references in copied REG_NOTES. */
1607
1608 static void
1609 final_reg_note_copy (notes, map)
1610 rtx notes;
1611 struct inline_remap *map;
1612 {
1613 rtx note;
1614
1615 for (note = notes; note; note = XEXP (note, 1))
1616 if (GET_CODE (note) == INSN_LIST)
1617 XEXP (note, 0) = map->insn_map[INSN_UID (XEXP (note, 0))];
1618 }
1619
1620 /* Copy each instruction in the loop, substituting from map as appropriate.
1621 This is very similar to a loop in expand_inline_function. */
1622
1623 static void
1624 copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
1625 unroll_type, start_label, loop_end, insert_before,
1626 copy_notes_from)
1627 rtx copy_start, copy_end;
1628 struct inline_remap *map;
1629 rtx exit_label;
1630 int last_iteration;
1631 enum unroll_types unroll_type;
1632 rtx start_label, loop_end, insert_before, copy_notes_from;
1633 {
1634 rtx insn, pattern;
1635 rtx set, tem, copy;
1636 int dest_reg_was_split, i;
1637 #ifdef HAVE_cc0
1638 rtx cc0_insn = 0;
1639 #endif
1640 rtx final_label = 0;
1641 rtx giv_inc, giv_dest_reg, giv_src_reg;
1642
1643 /* If this isn't the last iteration, then map any references to the
1644 start_label to final_label. Final label will then be emitted immediately
1645 after the end of this loop body if it was ever used.
1646
1647 If this is the last iteration, then map references to the start_label
1648 to itself. */
1649 if (! last_iteration)
1650 {
1651 final_label = gen_label_rtx ();
1652 set_label_in_map (map, CODE_LABEL_NUMBER (start_label),
1653 final_label);
1654 }
1655 else
1656 set_label_in_map (map, CODE_LABEL_NUMBER (start_label), start_label);
1657
1658 start_sequence ();
1659
1660 insn = copy_start;
1661 do
1662 {
1663 insn = NEXT_INSN (insn);
1664
1665 map->orig_asm_operands_vector = 0;
1666
1667 switch (GET_CODE (insn))
1668 {
1669 case INSN:
1670 pattern = PATTERN (insn);
1671 copy = 0;
1672 giv_inc = 0;
1673
1674 /* Check to see if this is a giv that has been combined with
1675 some split address givs. (Combined in the sense that
1676 `combine_givs' in loop.c has put two givs in the same register.)
1677 In this case, we must search all givs based on the same biv to
1678 find the address givs. Then split the address givs.
1679 Do this before splitting the giv, since that may map the
1680 SET_DEST to a new register. */
1681
1682 if ((set = single_set (insn))
1683 && GET_CODE (SET_DEST (set)) == REG
1684 && addr_combined_regs[REGNO (SET_DEST (set))])
1685 {
1686 struct iv_class *bl;
1687 struct induction *v, *tv;
1688 int regno = REGNO (SET_DEST (set));
1689
1690 v = addr_combined_regs[REGNO (SET_DEST (set))];
1691 bl = reg_biv_class[REGNO (v->src_reg)];
1692
1693 /* Although the giv_inc amount is not needed here, we must call
1694 calculate_giv_inc here since it might try to delete the
1695 last insn emitted. If we wait until later to call it,
1696 we might accidentally delete insns generated immediately
1697 below by emit_unrolled_add. */
1698
1699 giv_inc = calculate_giv_inc (set, insn, regno);
1700
1701 /* Now find all address giv's that were combined with this
1702 giv 'v'. */
1703 for (tv = bl->giv; tv; tv = tv->next_iv)
1704 if (tv->giv_type == DEST_ADDR && tv->same == v)
1705 {
1706 int this_giv_inc;
1707
1708 /* If this DEST_ADDR giv was not split, then ignore it. */
1709 if (*tv->location != tv->dest_reg)
1710 continue;
1711
1712 /* Scale this_giv_inc if the multiplicative factors of
1713 the two givs are different. */
1714 this_giv_inc = INTVAL (giv_inc);
1715 if (tv->mult_val != v->mult_val)
1716 this_giv_inc = (this_giv_inc / INTVAL (v->mult_val)
1717 * INTVAL (tv->mult_val));
1718
1719 tv->dest_reg = plus_constant (tv->dest_reg, this_giv_inc);
1720 *tv->location = tv->dest_reg;
1721
1722 if (last_iteration && unroll_type != UNROLL_COMPLETELY)
1723 {
1724 /* Must emit an insn to increment the split address
1725 giv. Add in the const_adjust field in case there
1726 was a constant eliminated from the address. */
1727 rtx value, dest_reg;
1728
1729 /* tv->dest_reg will be either a bare register,
1730 or else a register plus a constant. */
1731 if (GET_CODE (tv->dest_reg) == REG)
1732 dest_reg = tv->dest_reg;
1733 else
1734 dest_reg = XEXP (tv->dest_reg, 0);
1735
1736 /* Check for shared address givs, and avoid
1737 incrementing the shared pseudo reg more than
1738 once. */
1739 if (! tv->same_insn && ! tv->shared)
1740 {
1741 /* tv->dest_reg may actually be a (PLUS (REG)
1742 (CONST)) here, so we must call plus_constant
1743 to add the const_adjust amount before calling
1744 emit_unrolled_add below. */
1745 value = plus_constant (tv->dest_reg,
1746 tv->const_adjust);
1747
1748 /* The constant could be too large for an add
1749 immediate, so can't directly emit an insn
1750 here. */
1751 emit_unrolled_add (dest_reg, XEXP (value, 0),
1752 XEXP (value, 1));
1753 }
1754
1755 /* Reset the giv to be just the register again, in case
1756 it is used after the set we have just emitted.
1757 We must subtract the const_adjust factor added in
1758 above. */
1759 tv->dest_reg = plus_constant (dest_reg,
1760 - tv->const_adjust);
1761 *tv->location = tv->dest_reg;
1762 }
1763 }
1764 }
1765
1766 /* If this is a setting of a splittable variable, then determine
1767 how to split the variable, create a new set based on this split,
1768 and set up the reg_map so that later uses of the variable will
1769 use the new split variable. */
1770
1771 dest_reg_was_split = 0;
1772
1773 if ((set = single_set (insn))
1774 && GET_CODE (SET_DEST (set)) == REG
1775 && splittable_regs[REGNO (SET_DEST (set))])
1776 {
1777 int regno = REGNO (SET_DEST (set));
1778
1779 dest_reg_was_split = 1;
1780
1781 /* Compute the increment value for the giv, if it wasn't
1782 already computed above. */
1783
1784 if (giv_inc == 0)
1785 giv_inc = calculate_giv_inc (set, insn, regno);
1786 giv_dest_reg = SET_DEST (set);
1787 giv_src_reg = SET_DEST (set);
1788
1789 if (unroll_type == UNROLL_COMPLETELY)
1790 {
1791 /* Completely unrolling the loop. Set the induction
1792 variable to a known constant value. */
1793
1794 /* The value in splittable_regs may be an invariant
1795 value, so we must use plus_constant here. */
1796 splittable_regs[regno]
1797 = plus_constant (splittable_regs[regno], INTVAL (giv_inc));
1798
1799 if (GET_CODE (splittable_regs[regno]) == PLUS)
1800 {
1801 giv_src_reg = XEXP (splittable_regs[regno], 0);
1802 giv_inc = XEXP (splittable_regs[regno], 1);
1803 }
1804 else
1805 {
1806 /* The splittable_regs value must be a REG or a
1807 CONST_INT, so put the entire value in the giv_src_reg
1808 variable. */
1809 giv_src_reg = splittable_regs[regno];
1810 giv_inc = const0_rtx;
1811 }
1812 }
1813 else
1814 {
1815 /* Partially unrolling loop. Create a new pseudo
1816 register for the iteration variable, and set it to
1817 be a constant plus the original register. Except
1818 on the last iteration, when the result has to
1819 go back into the original iteration var register. */
1820
1821 /* Handle bivs which must be mapped to a new register
1822 when split. This happens for bivs which need their
1823 final value set before loop entry. The new register
1824 for the biv was stored in the biv's first struct
1825 induction entry by find_splittable_regs. */
1826
1827 if (regno < max_reg_before_loop
1828 && reg_iv_type[regno] == BASIC_INDUCT)
1829 {
1830 giv_src_reg = reg_biv_class[regno]->biv->src_reg;
1831 giv_dest_reg = giv_src_reg;
1832 }
1833
1834 #if 0
1835 /* If non-reduced/final-value givs were split, then
1836 this would have to remap those givs also. See
1837 find_splittable_regs. */
1838 #endif
1839
1840 splittable_regs[regno]
1841 = GEN_INT (INTVAL (giv_inc)
1842 + INTVAL (splittable_regs[regno]));
1843 giv_inc = splittable_regs[regno];
1844
1845 /* Now split the induction variable by changing the dest
1846 of this insn to a new register, and setting its
1847 reg_map entry to point to this new register.
1848
1849 If this is the last iteration, and this is the last insn
1850 that will update the iv, then reuse the original dest,
1851 to ensure that the iv will have the proper value when
1852 the loop exits or repeats.
1853
1854 Using splittable_regs_updates here like this is safe,
1855 because it can only be greater than one if all
1856 instructions modifying the iv are always executed in
1857 order. */
1858
1859 if (! last_iteration
1860 || (splittable_regs_updates[regno]-- != 1))
1861 {
1862 tem = gen_reg_rtx (GET_MODE (giv_src_reg));
1863 giv_dest_reg = tem;
1864 map->reg_map[regno] = tem;
1865 record_base_value (REGNO (tem),
1866 giv_inc == const0_rtx
1867 ? giv_src_reg
1868 : gen_rtx_PLUS (GET_MODE (giv_src_reg),
1869 giv_src_reg, giv_inc),
1870 1);
1871 }
1872 else
1873 map->reg_map[regno] = giv_src_reg;
1874 }
1875
1876 /* The constant being added could be too large for an add
1877 immediate, so can't directly emit an insn here. */
1878 emit_unrolled_add (giv_dest_reg, giv_src_reg, giv_inc);
1879 copy = get_last_insn ();
1880 pattern = PATTERN (copy);
1881 }
1882 else
1883 {
1884 pattern = copy_rtx_and_substitute (pattern, map);
1885 copy = emit_insn (pattern);
1886 }
1887 REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
1888
1889 #ifdef HAVE_cc0
1890 /* If this insn is setting CC0, it may need to look at
1891 the insn that uses CC0 to see what type of insn it is.
1892 In that case, the call to recog via validate_change will
1893 fail. So don't substitute constants here. Instead,
1894 do it when we emit the following insn.
1895
1896 For example, see the pyr.md file. That machine has signed and
1897 unsigned compares. The compare patterns must check the
1898 following branch insn to see which what kind of compare to
1899 emit.
1900
1901 If the previous insn set CC0, substitute constants on it as
1902 well. */
1903 if (sets_cc0_p (PATTERN (copy)) != 0)
1904 cc0_insn = copy;
1905 else
1906 {
1907 if (cc0_insn)
1908 try_constants (cc0_insn, map);
1909 cc0_insn = 0;
1910 try_constants (copy, map);
1911 }
1912 #else
1913 try_constants (copy, map);
1914 #endif
1915
1916 /* Make split induction variable constants `permanent' since we
1917 know there are no backward branches across iteration variable
1918 settings which would invalidate this. */
1919 if (dest_reg_was_split)
1920 {
1921 int regno = REGNO (SET_DEST (pattern));
1922
1923 if (regno < map->const_equiv_map_size
1924 && map->const_age_map[regno] == map->const_age)
1925 map->const_age_map[regno] = -1;
1926 }
1927 break;
1928
1929 case JUMP_INSN:
1930 pattern = copy_rtx_and_substitute (PATTERN (insn), map);
1931 copy = emit_jump_insn (pattern);
1932 REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
1933
1934 if (JUMP_LABEL (insn) == start_label && insn == copy_end
1935 && ! last_iteration)
1936 {
1937 /* This is a branch to the beginning of the loop; this is the
1938 last insn being copied; and this is not the last iteration.
1939 In this case, we want to change the original fall through
1940 case to be a branch past the end of the loop, and the
1941 original jump label case to fall_through. */
1942
1943 if (invert_exp (pattern, copy))
1944 {
1945 if (! redirect_exp (&pattern,
1946 get_label_from_map (map,
1947 CODE_LABEL_NUMBER
1948 (JUMP_LABEL (insn))),
1949 exit_label, copy))
1950 abort ();
1951 }
1952 else
1953 {
1954 rtx jmp;
1955 rtx lab = gen_label_rtx ();
1956 /* Can't do it by reversing the jump (probably because we
1957 couldn't reverse the conditions), so emit a new
1958 jump_insn after COPY, and redirect the jump around
1959 that. */
1960 jmp = emit_jump_insn_after (gen_jump (exit_label), copy);
1961 jmp = emit_barrier_after (jmp);
1962 emit_label_after (lab, jmp);
1963 LABEL_NUSES (lab) = 0;
1964 if (! redirect_exp (&pattern,
1965 get_label_from_map (map,
1966 CODE_LABEL_NUMBER
1967 (JUMP_LABEL (insn))),
1968 lab, copy))
1969 abort ();
1970 }
1971 }
1972
1973 #ifdef HAVE_cc0
1974 if (cc0_insn)
1975 try_constants (cc0_insn, map);
1976 cc0_insn = 0;
1977 #endif
1978 try_constants (copy, map);
1979
1980 /* Set the jump label of COPY correctly to avoid problems with
1981 later passes of unroll_loop, if INSN had jump label set. */
1982 if (JUMP_LABEL (insn))
1983 {
1984 rtx label = 0;
1985
1986 /* Can't use the label_map for every insn, since this may be
1987 the backward branch, and hence the label was not mapped. */
1988 if ((set = single_set (copy)))
1989 {
1990 tem = SET_SRC (set);
1991 if (GET_CODE (tem) == LABEL_REF)
1992 label = XEXP (tem, 0);
1993 else if (GET_CODE (tem) == IF_THEN_ELSE)
1994 {
1995 if (XEXP (tem, 1) != pc_rtx)
1996 label = XEXP (XEXP (tem, 1), 0);
1997 else
1998 label = XEXP (XEXP (tem, 2), 0);
1999 }
2000 }
2001
2002 if (label && GET_CODE (label) == CODE_LABEL)
2003 JUMP_LABEL (copy) = label;
2004 else
2005 {
2006 /* An unrecognizable jump insn, probably the entry jump
2007 for a switch statement. This label must have been mapped,
2008 so just use the label_map to get the new jump label. */
2009 JUMP_LABEL (copy)
2010 = get_label_from_map (map,
2011 CODE_LABEL_NUMBER (JUMP_LABEL (insn)));
2012 }
2013
2014 /* If this is a non-local jump, then must increase the label
2015 use count so that the label will not be deleted when the
2016 original jump is deleted. */
2017 LABEL_NUSES (JUMP_LABEL (copy))++;
2018 }
2019 else if (GET_CODE (PATTERN (copy)) == ADDR_VEC
2020 || GET_CODE (PATTERN (copy)) == ADDR_DIFF_VEC)
2021 {
2022 rtx pat = PATTERN (copy);
2023 int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
2024 int len = XVECLEN (pat, diff_vec_p);
2025 int i;
2026
2027 for (i = 0; i < len; i++)
2028 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))++;
2029 }
2030
2031 /* If this used to be a conditional jump insn but whose branch
2032 direction is now known, we must do something special. */
2033 if (condjump_p (insn) && !simplejump_p (insn) && map->last_pc_value)
2034 {
2035 #ifdef HAVE_cc0
2036 /* The previous insn set cc0 for us. So delete it. */
2037 delete_insn (PREV_INSN (copy));
2038 #endif
2039
2040 /* If this is now a no-op, delete it. */
2041 if (map->last_pc_value == pc_rtx)
2042 {
2043 /* Don't let delete_insn delete the label referenced here,
2044 because we might possibly need it later for some other
2045 instruction in the loop. */
2046 if (JUMP_LABEL (copy))
2047 LABEL_NUSES (JUMP_LABEL (copy))++;
2048 delete_insn (copy);
2049 if (JUMP_LABEL (copy))
2050 LABEL_NUSES (JUMP_LABEL (copy))--;
2051 copy = 0;
2052 }
2053 else
2054 /* Otherwise, this is unconditional jump so we must put a
2055 BARRIER after it. We could do some dead code elimination
2056 here, but jump.c will do it just as well. */
2057 emit_barrier ();
2058 }
2059 break;
2060
2061 case CALL_INSN:
2062 pattern = copy_rtx_and_substitute (PATTERN (insn), map);
2063 copy = emit_call_insn (pattern);
2064 REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
2065
2066 /* Because the USAGE information potentially contains objects other
2067 than hard registers, we need to copy it. */
2068 CALL_INSN_FUNCTION_USAGE (copy)
2069 = copy_rtx_and_substitute (CALL_INSN_FUNCTION_USAGE (insn), map);
2070
2071 #ifdef HAVE_cc0
2072 if (cc0_insn)
2073 try_constants (cc0_insn, map);
2074 cc0_insn = 0;
2075 #endif
2076 try_constants (copy, map);
2077
2078 /* Be lazy and assume CALL_INSNs clobber all hard registers. */
2079 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2080 map->const_equiv_map[i] = 0;
2081 break;
2082
2083 case CODE_LABEL:
2084 /* If this is the loop start label, then we don't need to emit a
2085 copy of this label since no one will use it. */
2086
2087 if (insn != start_label)
2088 {
2089 copy = emit_label (get_label_from_map (map,
2090 CODE_LABEL_NUMBER (insn)));
2091 map->const_age++;
2092 }
2093 break;
2094
2095 case BARRIER:
2096 copy = emit_barrier ();
2097 break;
2098
2099 case NOTE:
2100 /* VTOP notes are valid only before the loop exit test. If placed
2101 anywhere else, loop may generate bad code. */
2102
2103 if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
2104 && (NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_VTOP
2105 || (last_iteration && unroll_type != UNROLL_COMPLETELY)))
2106 copy = emit_note (NOTE_SOURCE_FILE (insn),
2107 NOTE_LINE_NUMBER (insn));
2108 else
2109 copy = 0;
2110 break;
2111
2112 default:
2113 abort ();
2114 break;
2115 }
2116
2117 map->insn_map[INSN_UID (insn)] = copy;
2118 }
2119 while (insn != copy_end);
2120
2121 /* Now finish coping the REG_NOTES. */
2122 insn = copy_start;
2123 do
2124 {
2125 insn = NEXT_INSN (insn);
2126 if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2127 || GET_CODE (insn) == CALL_INSN)
2128 && map->insn_map[INSN_UID (insn)])
2129 final_reg_note_copy (REG_NOTES (map->insn_map[INSN_UID (insn)]), map);
2130 }
2131 while (insn != copy_end);
2132
2133 /* There may be notes between copy_notes_from and loop_end. Emit a copy of
2134 each of these notes here, since there may be some important ones, such as
2135 NOTE_INSN_BLOCK_END notes, in this group. We don't do this on the last
2136 iteration, because the original notes won't be deleted.
2137
2138 We can't use insert_before here, because when from preconditioning,
2139 insert_before points before the loop. We can't use copy_end, because
2140 there may be insns already inserted after it (which we don't want to
2141 copy) when not from preconditioning code. */
2142
2143 if (! last_iteration)
2144 {
2145 for (insn = copy_notes_from; insn != loop_end; insn = NEXT_INSN (insn))
2146 {
2147 if (GET_CODE (insn) == NOTE
2148 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED)
2149 emit_note (NOTE_SOURCE_FILE (insn), NOTE_LINE_NUMBER (insn));
2150 }
2151 }
2152
2153 if (final_label && LABEL_NUSES (final_label) > 0)
2154 emit_label (final_label);
2155
2156 tem = gen_sequence ();
2157 end_sequence ();
2158 emit_insn_before (tem, insert_before);
2159 }
2160 \f
2161 /* Emit an insn, using the expand_binop to ensure that a valid insn is
2162 emitted. This will correctly handle the case where the increment value
2163 won't fit in the immediate field of a PLUS insns. */
2164
2165 void
2166 emit_unrolled_add (dest_reg, src_reg, increment)
2167 rtx dest_reg, src_reg, increment;
2168 {
2169 rtx result;
2170
2171 result = expand_binop (GET_MODE (dest_reg), add_optab, src_reg, increment,
2172 dest_reg, 0, OPTAB_LIB_WIDEN);
2173
2174 if (dest_reg != result)
2175 emit_move_insn (dest_reg, result);
2176 }
2177 \f
2178 /* Searches the insns between INSN and LOOP_END. Returns 1 if there
2179 is a backward branch in that range that branches to somewhere between
2180 LOOP_START and INSN. Returns 0 otherwise. */
2181
2182 /* ??? This is quadratic algorithm. Could be rewritten to be linear.
2183 In practice, this is not a problem, because this function is seldom called,
2184 and uses a negligible amount of CPU time on average. */
2185
2186 int
2187 back_branch_in_range_p (insn, loop_start, loop_end)
2188 rtx insn;
2189 rtx loop_start, loop_end;
2190 {
2191 rtx p, q, target_insn;
2192 rtx orig_loop_end = loop_end;
2193
2194 /* Stop before we get to the backward branch at the end of the loop. */
2195 loop_end = prev_nonnote_insn (loop_end);
2196 if (GET_CODE (loop_end) == BARRIER)
2197 loop_end = PREV_INSN (loop_end);
2198
2199 /* Check in case insn has been deleted, search forward for first non
2200 deleted insn following it. */
2201 while (INSN_DELETED_P (insn))
2202 insn = NEXT_INSN (insn);
2203
2204 /* Check for the case where insn is the last insn in the loop. Deal
2205 with the case where INSN was a deleted loop test insn, in which case
2206 it will now be the NOTE_LOOP_END. */
2207 if (insn == loop_end || insn == orig_loop_end)
2208 return 0;
2209
2210 for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
2211 {
2212 if (GET_CODE (p) == JUMP_INSN)
2213 {
2214 target_insn = JUMP_LABEL (p);
2215
2216 /* Search from loop_start to insn, to see if one of them is
2217 the target_insn. We can't use INSN_LUID comparisons here,
2218 since insn may not have an LUID entry. */
2219 for (q = loop_start; q != insn; q = NEXT_INSN (q))
2220 if (q == target_insn)
2221 return 1;
2222 }
2223 }
2224
2225 return 0;
2226 }
2227
2228 /* Try to generate the simplest rtx for the expression
2229 (PLUS (MULT mult1 mult2) add1). This is used to calculate the initial
2230 value of giv's. */
2231
2232 static rtx
2233 fold_rtx_mult_add (mult1, mult2, add1, mode)
2234 rtx mult1, mult2, add1;
2235 enum machine_mode mode;
2236 {
2237 rtx temp, mult_res;
2238 rtx result;
2239
2240 /* The modes must all be the same. This should always be true. For now,
2241 check to make sure. */
2242 if ((GET_MODE (mult1) != mode && GET_MODE (mult1) != VOIDmode)
2243 || (GET_MODE (mult2) != mode && GET_MODE (mult2) != VOIDmode)
2244 || (GET_MODE (add1) != mode && GET_MODE (add1) != VOIDmode))
2245 abort ();
2246
2247 /* Ensure that if at least one of mult1/mult2 are constant, then mult2
2248 will be a constant. */
2249 if (GET_CODE (mult1) == CONST_INT)
2250 {
2251 temp = mult2;
2252 mult2 = mult1;
2253 mult1 = temp;
2254 }
2255
2256 mult_res = simplify_binary_operation (MULT, mode, mult1, mult2);
2257 if (! mult_res)
2258 mult_res = gen_rtx_MULT (mode, mult1, mult2);
2259
2260 /* Again, put the constant second. */
2261 if (GET_CODE (add1) == CONST_INT)
2262 {
2263 temp = add1;
2264 add1 = mult_res;
2265 mult_res = temp;
2266 }
2267
2268 result = simplify_binary_operation (PLUS, mode, add1, mult_res);
2269 if (! result)
2270 result = gen_rtx_PLUS (mode, add1, mult_res);
2271
2272 return result;
2273 }
2274
2275 /* Searches the list of induction struct's for the biv BL, to try to calculate
2276 the total increment value for one iteration of the loop as a constant.
2277
2278 Returns the increment value as an rtx, simplified as much as possible,
2279 if it can be calculated. Otherwise, returns 0. */
2280
2281 rtx
2282 biv_total_increment (bl, loop_start, loop_end)
2283 struct iv_class *bl;
2284 rtx loop_start, loop_end;
2285 {
2286 struct induction *v;
2287 rtx result;
2288
2289 /* For increment, must check every instruction that sets it. Each
2290 instruction must be executed only once each time through the loop.
2291 To verify this, we check that the insn is always executed, and that
2292 there are no backward branches after the insn that branch to before it.
2293 Also, the insn must have a mult_val of one (to make sure it really is
2294 an increment). */
2295
2296 result = const0_rtx;
2297 for (v = bl->biv; v; v = v->next_iv)
2298 {
2299 if (v->always_computable && v->mult_val == const1_rtx
2300 && ! back_branch_in_range_p (v->insn, loop_start, loop_end))
2301 result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
2302 else
2303 return 0;
2304 }
2305
2306 return result;
2307 }
2308
2309 /* Determine the initial value of the iteration variable, and the amount
2310 that it is incremented each loop. Use the tables constructed by
2311 the strength reduction pass to calculate these values.
2312
2313 Initial_value and/or increment are set to zero if their values could not
2314 be calculated. */
2315
2316 void
2317 iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
2318 rtx iteration_var, *initial_value, *increment;
2319 rtx loop_start, loop_end;
2320 {
2321 struct iv_class *bl;
2322 #if 0
2323 struct induction *v;
2324 #endif
2325
2326 /* Clear the result values, in case no answer can be found. */
2327 *initial_value = 0;
2328 *increment = 0;
2329
2330 /* The iteration variable can be either a giv or a biv. Check to see
2331 which it is, and compute the variable's initial value, and increment
2332 value if possible. */
2333
2334 /* If this is a new register, can't handle it since we don't have any
2335 reg_iv_type entry for it. */
2336 if (REGNO (iteration_var) >= max_reg_before_loop)
2337 {
2338 if (loop_dump_stream)
2339 fprintf (loop_dump_stream,
2340 "Loop unrolling: No reg_iv_type entry for iteration var.\n");
2341 return;
2342 }
2343
2344 /* Reject iteration variables larger than the host wide int size, since they
2345 could result in a number of iterations greater than the range of our
2346 `unsigned HOST_WIDE_INT' variable loop_n_iterations. */
2347 else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
2348 > HOST_BITS_PER_WIDE_INT))
2349 {
2350 if (loop_dump_stream)
2351 fprintf (loop_dump_stream,
2352 "Loop unrolling: Iteration var rejected because mode too large.\n");
2353 return;
2354 }
2355 else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
2356 {
2357 if (loop_dump_stream)
2358 fprintf (loop_dump_stream,
2359 "Loop unrolling: Iteration var not an integer.\n");
2360 return;
2361 }
2362 else if (reg_iv_type[REGNO (iteration_var)] == BASIC_INDUCT)
2363 {
2364 /* Grab initial value, only useful if it is a constant. */
2365 bl = reg_biv_class[REGNO (iteration_var)];
2366 *initial_value = bl->initial_value;
2367
2368 *increment = biv_total_increment (bl, loop_start, loop_end);
2369 }
2370 else if (reg_iv_type[REGNO (iteration_var)] == GENERAL_INDUCT)
2371 {
2372 #if 1
2373 /* ??? The code below does not work because the incorrect number of
2374 iterations is calculated when the biv is incremented after the giv
2375 is set (which is the usual case). This can probably be accounted
2376 for by biasing the initial_value by subtracting the amount of the
2377 increment that occurs between the giv set and the giv test. However,
2378 a giv as an iterator is very rare, so it does not seem worthwhile
2379 to handle this. */
2380 /* ??? An example failure is: i = 6; do {;} while (i++ < 9). */
2381 if (loop_dump_stream)
2382 fprintf (loop_dump_stream,
2383 "Loop unrolling: Giv iterators are not handled.\n");
2384 return;
2385 #else
2386 /* Initial value is mult_val times the biv's initial value plus
2387 add_val. Only useful if it is a constant. */
2388 v = reg_iv_info[REGNO (iteration_var)];
2389 bl = reg_biv_class[REGNO (v->src_reg)];
2390 *initial_value = fold_rtx_mult_add (v->mult_val, bl->initial_value,
2391 v->add_val, v->mode);
2392
2393 /* Increment value is mult_val times the increment value of the biv. */
2394
2395 *increment = biv_total_increment (bl, loop_start, loop_end);
2396 if (*increment)
2397 *increment = fold_rtx_mult_add (v->mult_val, *increment, const0_rtx,
2398 v->mode);
2399 #endif
2400 }
2401 else
2402 {
2403 if (loop_dump_stream)
2404 fprintf (loop_dump_stream,
2405 "Loop unrolling: Not basic or general induction var.\n");
2406 return;
2407 }
2408 }
2409
2410 /* Calculate the approximate final value of the iteration variable
2411 which has an loop exit test with code COMPARISON_CODE and comparison value
2412 of COMPARISON_VALUE. Also returns an indication of whether the comparison
2413 was signed or unsigned, and the direction of the comparison. This info is
2414 needed to calculate the number of loop iterations. */
2415
2416 static rtx
2417 approx_final_value (comparison_code, comparison_value, unsigned_p, compare_dir)
2418 enum rtx_code comparison_code;
2419 rtx comparison_value;
2420 int *unsigned_p;
2421 int *compare_dir;
2422 {
2423 /* Calculate the final value of the induction variable.
2424 The exact final value depends on the branch operator, and increment sign.
2425 This is only an approximate value. It will be wrong if the iteration
2426 variable is not incremented by one each time through the loop, and
2427 approx final value - start value % increment != 0. */
2428
2429 *unsigned_p = 0;
2430 switch (comparison_code)
2431 {
2432 case LEU:
2433 *unsigned_p = 1;
2434 case LE:
2435 *compare_dir = 1;
2436 return plus_constant (comparison_value, 1);
2437 case GEU:
2438 *unsigned_p = 1;
2439 case GE:
2440 *compare_dir = -1;
2441 return plus_constant (comparison_value, -1);
2442 case EQ:
2443 /* Can not calculate a final value for this case. */
2444 *compare_dir = 0;
2445 return 0;
2446 case LTU:
2447 *unsigned_p = 1;
2448 case LT:
2449 *compare_dir = 1;
2450 return comparison_value;
2451 break;
2452 case GTU:
2453 *unsigned_p = 1;
2454 case GT:
2455 *compare_dir = -1;
2456 return comparison_value;
2457 case NE:
2458 *compare_dir = 0;
2459 return comparison_value;
2460 default:
2461 abort ();
2462 }
2463 }
2464
2465 /* For each biv and giv, determine whether it can be safely split into
2466 a different variable for each unrolled copy of the loop body. If it
2467 is safe to split, then indicate that by saving some useful info
2468 in the splittable_regs array.
2469
2470 If the loop is being completely unrolled, then splittable_regs will hold
2471 the current value of the induction variable while the loop is unrolled.
2472 It must be set to the initial value of the induction variable here.
2473 Otherwise, splittable_regs will hold the difference between the current
2474 value of the induction variable and the value the induction variable had
2475 at the top of the loop. It must be set to the value 0 here.
2476
2477 Returns the total number of instructions that set registers that are
2478 splittable. */
2479
2480 /* ?? If the loop is only unrolled twice, then most of the restrictions to
2481 constant values are unnecessary, since we can easily calculate increment
2482 values in this case even if nothing is constant. The increment value
2483 should not involve a multiply however. */
2484
2485 /* ?? Even if the biv/giv increment values aren't constant, it may still
2486 be beneficial to split the variable if the loop is only unrolled a few
2487 times, since multiplies by small integers (1,2,3,4) are very cheap. */
2488
2489 static int
2490 find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
2491 unroll_number)
2492 enum unroll_types unroll_type;
2493 rtx loop_start, loop_end;
2494 rtx end_insert_before;
2495 int unroll_number;
2496 {
2497 struct iv_class *bl;
2498 struct induction *v;
2499 rtx increment, tem;
2500 rtx biv_final_value;
2501 int biv_splittable;
2502 int result = 0;
2503
2504 for (bl = loop_iv_list; bl; bl = bl->next)
2505 {
2506 /* Biv_total_increment must return a constant value,
2507 otherwise we can not calculate the split values. */
2508
2509 increment = biv_total_increment (bl, loop_start, loop_end);
2510 if (! increment || GET_CODE (increment) != CONST_INT)
2511 continue;
2512
2513 /* The loop must be unrolled completely, or else have a known number
2514 of iterations and only one exit, or else the biv must be dead
2515 outside the loop, or else the final value must be known. Otherwise,
2516 it is unsafe to split the biv since it may not have the proper
2517 value on loop exit. */
2518
2519 /* loop_number_exit_count is non-zero if the loop has an exit other than
2520 a fall through at the end. */
2521
2522 biv_splittable = 1;
2523 biv_final_value = 0;
2524 if (unroll_type != UNROLL_COMPLETELY
2525 && (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
2526 || unroll_type == UNROLL_NAIVE)
2527 && (uid_luid[REGNO_LAST_UID (bl->regno)] >= INSN_LUID (loop_end)
2528 || ! bl->init_insn
2529 || INSN_UID (bl->init_insn) >= max_uid_for_loop
2530 || (uid_luid[REGNO_FIRST_UID (bl->regno)]
2531 < INSN_LUID (bl->init_insn))
2532 || reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
2533 && ! (biv_final_value = final_biv_value (bl, loop_start, loop_end)))
2534 biv_splittable = 0;
2535
2536 /* If any of the insns setting the BIV don't do so with a simple
2537 PLUS, we don't know how to split it. */
2538 for (v = bl->biv; biv_splittable && v; v = v->next_iv)
2539 if ((tem = single_set (v->insn)) == 0
2540 || GET_CODE (SET_DEST (tem)) != REG
2541 || REGNO (SET_DEST (tem)) != bl->regno
2542 || GET_CODE (SET_SRC (tem)) != PLUS)
2543 biv_splittable = 0;
2544
2545 /* If final value is non-zero, then must emit an instruction which sets
2546 the value of the biv to the proper value. This is done after
2547 handling all of the givs, since some of them may need to use the
2548 biv's value in their initialization code. */
2549
2550 /* This biv is splittable. If completely unrolling the loop, save
2551 the biv's initial value. Otherwise, save the constant zero. */
2552
2553 if (biv_splittable == 1)
2554 {
2555 if (unroll_type == UNROLL_COMPLETELY)
2556 {
2557 /* If the initial value of the biv is itself (i.e. it is too
2558 complicated for strength_reduce to compute), or is a hard
2559 register, or it isn't invariant, then we must create a new
2560 pseudo reg to hold the initial value of the biv. */
2561
2562 if (GET_CODE (bl->initial_value) == REG
2563 && (REGNO (bl->initial_value) == bl->regno
2564 || REGNO (bl->initial_value) < FIRST_PSEUDO_REGISTER
2565 || ! invariant_p (bl->initial_value)))
2566 {
2567 rtx tem = gen_reg_rtx (bl->biv->mode);
2568
2569 record_base_value (REGNO (tem), bl->biv->add_val, 0);
2570 emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2571 loop_start);
2572
2573 if (loop_dump_stream)
2574 fprintf (loop_dump_stream, "Biv %d initial value remapped to %d.\n",
2575 bl->regno, REGNO (tem));
2576
2577 splittable_regs[bl->regno] = tem;
2578 }
2579 else
2580 splittable_regs[bl->regno] = bl->initial_value;
2581 }
2582 else
2583 splittable_regs[bl->regno] = const0_rtx;
2584
2585 /* Save the number of instructions that modify the biv, so that
2586 we can treat the last one specially. */
2587
2588 splittable_regs_updates[bl->regno] = bl->biv_count;
2589 result += bl->biv_count;
2590
2591 if (loop_dump_stream)
2592 fprintf (loop_dump_stream,
2593 "Biv %d safe to split.\n", bl->regno);
2594 }
2595
2596 /* Check every giv that depends on this biv to see whether it is
2597 splittable also. Even if the biv isn't splittable, givs which
2598 depend on it may be splittable if the biv is live outside the
2599 loop, and the givs aren't. */
2600
2601 result += find_splittable_givs (bl, unroll_type, loop_start, loop_end,
2602 increment, unroll_number);
2603
2604 /* If final value is non-zero, then must emit an instruction which sets
2605 the value of the biv to the proper value. This is done after
2606 handling all of the givs, since some of them may need to use the
2607 biv's value in their initialization code. */
2608 if (biv_final_value)
2609 {
2610 /* If the loop has multiple exits, emit the insns before the
2611 loop to ensure that it will always be executed no matter
2612 how the loop exits. Otherwise emit the insn after the loop,
2613 since this is slightly more efficient. */
2614 if (! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
2615 emit_insn_before (gen_move_insn (bl->biv->src_reg,
2616 biv_final_value),
2617 end_insert_before);
2618 else
2619 {
2620 /* Create a new register to hold the value of the biv, and then
2621 set the biv to its final value before the loop start. The biv
2622 is set to its final value before loop start to ensure that
2623 this insn will always be executed, no matter how the loop
2624 exits. */
2625 rtx tem = gen_reg_rtx (bl->biv->mode);
2626 record_base_value (REGNO (tem), bl->biv->add_val, 0);
2627
2628 emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2629 loop_start);
2630 emit_insn_before (gen_move_insn (bl->biv->src_reg,
2631 biv_final_value),
2632 loop_start);
2633
2634 if (loop_dump_stream)
2635 fprintf (loop_dump_stream, "Biv %d mapped to %d for split.\n",
2636 REGNO (bl->biv->src_reg), REGNO (tem));
2637
2638 /* Set up the mapping from the original biv register to the new
2639 register. */
2640 bl->biv->src_reg = tem;
2641 }
2642 }
2643 }
2644 return result;
2645 }
2646
2647 /* Return 1 if the first and last unrolled copy of the address giv V is valid
2648 for the instruction that is using it. Do not make any changes to that
2649 instruction. */
2650
2651 static int
2652 verify_addresses (v, giv_inc, unroll_number)
2653 struct induction *v;
2654 rtx giv_inc;
2655 int unroll_number;
2656 {
2657 int ret = 1;
2658 rtx orig_addr = *v->location;
2659 rtx last_addr = plus_constant (v->dest_reg,
2660 INTVAL (giv_inc) * (unroll_number - 1));
2661
2662 /* First check to see if either address would fail. Handle the fact
2663 that we have may have a match_dup. */
2664 if (! validate_replace_rtx (*v->location, v->dest_reg, v->insn)
2665 || ! validate_replace_rtx (*v->location, last_addr, v->insn))
2666 ret = 0;
2667
2668 /* Now put things back the way they were before. This should always
2669 succeed. */
2670 if (! validate_replace_rtx (*v->location, orig_addr, v->insn))
2671 abort ();
2672
2673 return ret;
2674 }
2675
2676 /* For every giv based on the biv BL, check to determine whether it is
2677 splittable. This is a subroutine to find_splittable_regs ().
2678
2679 Return the number of instructions that set splittable registers. */
2680
2681 static int
2682 find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
2683 unroll_number)
2684 struct iv_class *bl;
2685 enum unroll_types unroll_type;
2686 rtx loop_start, loop_end;
2687 rtx increment;
2688 int unroll_number;
2689 {
2690 struct induction *v, *v2;
2691 rtx final_value;
2692 rtx tem;
2693 int result = 0;
2694
2695 /* Scan the list of givs, and set the same_insn field when there are
2696 multiple identical givs in the same insn. */
2697 for (v = bl->giv; v; v = v->next_iv)
2698 for (v2 = v->next_iv; v2; v2 = v2->next_iv)
2699 if (v->insn == v2->insn && rtx_equal_p (v->new_reg, v2->new_reg)
2700 && ! v2->same_insn)
2701 v2->same_insn = v;
2702
2703 for (v = bl->giv; v; v = v->next_iv)
2704 {
2705 rtx giv_inc, value;
2706
2707 /* Only split the giv if it has already been reduced, or if the loop is
2708 being completely unrolled. */
2709 if (unroll_type != UNROLL_COMPLETELY && v->ignore)
2710 continue;
2711
2712 /* The giv can be split if the insn that sets the giv is executed once
2713 and only once on every iteration of the loop. */
2714 /* An address giv can always be split. v->insn is just a use not a set,
2715 and hence it does not matter whether it is always executed. All that
2716 matters is that all the biv increments are always executed, and we
2717 won't reach here if they aren't. */
2718 if (v->giv_type != DEST_ADDR
2719 && (! v->always_computable
2720 || back_branch_in_range_p (v->insn, loop_start, loop_end)))
2721 continue;
2722
2723 /* The giv increment value must be a constant. */
2724 giv_inc = fold_rtx_mult_add (v->mult_val, increment, const0_rtx,
2725 v->mode);
2726 if (! giv_inc || GET_CODE (giv_inc) != CONST_INT)
2727 continue;
2728
2729 /* The loop must be unrolled completely, or else have a known number of
2730 iterations and only one exit, or else the giv must be dead outside
2731 the loop, or else the final value of the giv must be known.
2732 Otherwise, it is not safe to split the giv since it may not have the
2733 proper value on loop exit. */
2734
2735 /* The used outside loop test will fail for DEST_ADDR givs. They are
2736 never used outside the loop anyways, so it is always safe to split a
2737 DEST_ADDR giv. */
2738
2739 final_value = 0;
2740 if (unroll_type != UNROLL_COMPLETELY
2741 && (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
2742 || unroll_type == UNROLL_NAIVE)
2743 && v->giv_type != DEST_ADDR
2744 /* The next part is true if the pseudo is used outside the loop.
2745 We assume that this is true for any pseudo created after loop
2746 starts, because we don't have a reg_n_info entry for them. */
2747 && (REGNO (v->dest_reg) >= max_reg_before_loop
2748 || (REGNO_FIRST_UID (REGNO (v->dest_reg)) != INSN_UID (v->insn)
2749 /* Check for the case where the pseudo is set by a shift/add
2750 sequence, in which case the first insn setting the pseudo
2751 is the first insn of the shift/add sequence. */
2752 && (! (tem = find_reg_note (v->insn, REG_RETVAL, NULL_RTX))
2753 || (REGNO_FIRST_UID (REGNO (v->dest_reg))
2754 != INSN_UID (XEXP (tem, 0)))))
2755 /* Line above always fails if INSN was moved by loop opt. */
2756 || (uid_luid[REGNO_LAST_UID (REGNO (v->dest_reg))]
2757 >= INSN_LUID (loop_end)))
2758 && ! (final_value = v->final_value))
2759 continue;
2760
2761 #if 0
2762 /* Currently, non-reduced/final-value givs are never split. */
2763 /* Should emit insns after the loop if possible, as the biv final value
2764 code below does. */
2765
2766 /* If the final value is non-zero, and the giv has not been reduced,
2767 then must emit an instruction to set the final value. */
2768 if (final_value && !v->new_reg)
2769 {
2770 /* Create a new register to hold the value of the giv, and then set
2771 the giv to its final value before the loop start. The giv is set
2772 to its final value before loop start to ensure that this insn
2773 will always be executed, no matter how we exit. */
2774 tem = gen_reg_rtx (v->mode);
2775 emit_insn_before (gen_move_insn (tem, v->dest_reg), loop_start);
2776 emit_insn_before (gen_move_insn (v->dest_reg, final_value),
2777 loop_start);
2778
2779 if (loop_dump_stream)
2780 fprintf (loop_dump_stream, "Giv %d mapped to %d for split.\n",
2781 REGNO (v->dest_reg), REGNO (tem));
2782
2783 v->src_reg = tem;
2784 }
2785 #endif
2786
2787 /* This giv is splittable. If completely unrolling the loop, save the
2788 giv's initial value. Otherwise, save the constant zero for it. */
2789
2790 if (unroll_type == UNROLL_COMPLETELY)
2791 {
2792 /* It is not safe to use bl->initial_value here, because it may not
2793 be invariant. It is safe to use the initial value stored in
2794 the splittable_regs array if it is set. In rare cases, it won't
2795 be set, so then we do exactly the same thing as
2796 find_splittable_regs does to get a safe value. */
2797 rtx biv_initial_value;
2798
2799 if (splittable_regs[bl->regno])
2800 biv_initial_value = splittable_regs[bl->regno];
2801 else if (GET_CODE (bl->initial_value) != REG
2802 || (REGNO (bl->initial_value) != bl->regno
2803 && REGNO (bl->initial_value) >= FIRST_PSEUDO_REGISTER))
2804 biv_initial_value = bl->initial_value;
2805 else
2806 {
2807 rtx tem = gen_reg_rtx (bl->biv->mode);
2808
2809 record_base_value (REGNO (tem), bl->biv->add_val, 0);
2810 emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2811 loop_start);
2812 biv_initial_value = tem;
2813 }
2814 value = fold_rtx_mult_add (v->mult_val, biv_initial_value,
2815 v->add_val, v->mode);
2816 }
2817 else
2818 value = const0_rtx;
2819
2820 if (v->new_reg)
2821 {
2822 /* If a giv was combined with another giv, then we can only split
2823 this giv if the giv it was combined with was reduced. This
2824 is because the value of v->new_reg is meaningless in this
2825 case. */
2826 if (v->same && ! v->same->new_reg)
2827 {
2828 if (loop_dump_stream)
2829 fprintf (loop_dump_stream,
2830 "giv combined with unreduced giv not split.\n");
2831 continue;
2832 }
2833 /* If the giv is an address destination, it could be something other
2834 than a simple register, these have to be treated differently. */
2835 else if (v->giv_type == DEST_REG)
2836 {
2837 /* If value is not a constant, register, or register plus
2838 constant, then compute its value into a register before
2839 loop start. This prevents invalid rtx sharing, and should
2840 generate better code. We can use bl->initial_value here
2841 instead of splittable_regs[bl->regno] because this code
2842 is going before the loop start. */
2843 if (unroll_type == UNROLL_COMPLETELY
2844 && GET_CODE (value) != CONST_INT
2845 && GET_CODE (value) != REG
2846 && (GET_CODE (value) != PLUS
2847 || GET_CODE (XEXP (value, 0)) != REG
2848 || GET_CODE (XEXP (value, 1)) != CONST_INT))
2849 {
2850 rtx tem = gen_reg_rtx (v->mode);
2851 record_base_value (REGNO (tem), v->add_val, 0);
2852 emit_iv_add_mult (bl->initial_value, v->mult_val,
2853 v->add_val, tem, loop_start);
2854 value = tem;
2855 }
2856
2857 splittable_regs[REGNO (v->new_reg)] = value;
2858 }
2859 else
2860 {
2861 /* Splitting address givs is useful since it will often allow us
2862 to eliminate some increment insns for the base giv as
2863 unnecessary. */
2864
2865 /* If the addr giv is combined with a dest_reg giv, then all
2866 references to that dest reg will be remapped, which is NOT
2867 what we want for split addr regs. We always create a new
2868 register for the split addr giv, just to be safe. */
2869
2870 /* If we have multiple identical address givs within a
2871 single instruction, then use a single pseudo reg for
2872 both. This is necessary in case one is a match_dup
2873 of the other. */
2874
2875 v->const_adjust = 0;
2876
2877 if (v->same_insn)
2878 {
2879 v->dest_reg = v->same_insn->dest_reg;
2880 if (loop_dump_stream)
2881 fprintf (loop_dump_stream,
2882 "Sharing address givs in insn %d\n",
2883 INSN_UID (v->insn));
2884 }
2885 /* If multiple address GIVs have been combined with the
2886 same dest_reg GIV, do not create a new register for
2887 each. */
2888 else if (unroll_type != UNROLL_COMPLETELY
2889 && v->giv_type == DEST_ADDR
2890 && v->same && v->same->giv_type == DEST_ADDR
2891 && v->same->unrolled
2892 /* combine_givs_p may return true for some cases
2893 where the add and mult values are not equal.
2894 To share a register here, the values must be
2895 equal. */
2896 && rtx_equal_p (v->same->mult_val, v->mult_val)
2897 && rtx_equal_p (v->same->add_val, v->add_val))
2898
2899 {
2900 v->dest_reg = v->same->dest_reg;
2901 v->shared = 1;
2902 }
2903 else if (unroll_type != UNROLL_COMPLETELY)
2904 {
2905 /* If not completely unrolling the loop, then create a new
2906 register to hold the split value of the DEST_ADDR giv.
2907 Emit insn to initialize its value before loop start. */
2908
2909 rtx tem = gen_reg_rtx (v->mode);
2910 record_base_value (REGNO (tem), v->add_val, 0);
2911
2912 /* If the address giv has a constant in its new_reg value,
2913 then this constant can be pulled out and put in value,
2914 instead of being part of the initialization code. */
2915
2916 if (GET_CODE (v->new_reg) == PLUS
2917 && GET_CODE (XEXP (v->new_reg, 1)) == CONST_INT)
2918 {
2919 v->dest_reg
2920 = plus_constant (tem, INTVAL (XEXP (v->new_reg,1)));
2921
2922 /* Only succeed if this will give valid addresses.
2923 Try to validate both the first and the last
2924 address resulting from loop unrolling, if
2925 one fails, then can't do const elim here. */
2926 if (verify_addresses (v, giv_inc, unroll_number))
2927 {
2928 /* Save the negative of the eliminated const, so
2929 that we can calculate the dest_reg's increment
2930 value later. */
2931 v->const_adjust = - INTVAL (XEXP (v->new_reg, 1));
2932
2933 v->new_reg = XEXP (v->new_reg, 0);
2934 if (loop_dump_stream)
2935 fprintf (loop_dump_stream,
2936 "Eliminating constant from giv %d\n",
2937 REGNO (tem));
2938 }
2939 else
2940 v->dest_reg = tem;
2941 }
2942 else
2943 v->dest_reg = tem;
2944
2945 /* If the address hasn't been checked for validity yet, do so
2946 now, and fail completely if either the first or the last
2947 unrolled copy of the address is not a valid address
2948 for the instruction that uses it. */
2949 if (v->dest_reg == tem
2950 && ! verify_addresses (v, giv_inc, unroll_number))
2951 {
2952 for (v2 = v->next_iv; v2; v2 = v2->next_iv)
2953 if (v2->same_insn == v)
2954 v2->same_insn = 0;
2955
2956 if (loop_dump_stream)
2957 fprintf (loop_dump_stream,
2958 "Invalid address for giv at insn %d\n",
2959 INSN_UID (v->insn));
2960 continue;
2961 }
2962
2963 /* We set this after the address check, to guarantee that
2964 the register will be initialized. */
2965 v->unrolled = 1;
2966
2967 /* To initialize the new register, just move the value of
2968 new_reg into it. This is not guaranteed to give a valid
2969 instruction on machines with complex addressing modes.
2970 If we can't recognize it, then delete it and emit insns
2971 to calculate the value from scratch. */
2972 emit_insn_before (gen_rtx_SET (VOIDmode, tem,
2973 copy_rtx (v->new_reg)),
2974 loop_start);
2975 if (recog_memoized (PREV_INSN (loop_start)) < 0)
2976 {
2977 rtx sequence, ret;
2978
2979 /* We can't use bl->initial_value to compute the initial
2980 value, because the loop may have been preconditioned.
2981 We must calculate it from NEW_REG. Try using
2982 force_operand instead of emit_iv_add_mult. */
2983 delete_insn (PREV_INSN (loop_start));
2984
2985 start_sequence ();
2986 ret = force_operand (v->new_reg, tem);
2987 if (ret != tem)
2988 emit_move_insn (tem, ret);
2989 sequence = gen_sequence ();
2990 end_sequence ();
2991 emit_insn_before (sequence, loop_start);
2992
2993 if (loop_dump_stream)
2994 fprintf (loop_dump_stream,
2995 "Invalid init insn, rewritten.\n");
2996 }
2997 }
2998 else
2999 {
3000 v->dest_reg = value;
3001
3002 /* Check the resulting address for validity, and fail
3003 if the resulting address would be invalid. */
3004 if (! verify_addresses (v, giv_inc, unroll_number))
3005 {
3006 for (v2 = v->next_iv; v2; v2 = v2->next_iv)
3007 if (v2->same_insn == v)
3008 v2->same_insn = 0;
3009
3010 if (loop_dump_stream)
3011 fprintf (loop_dump_stream,
3012 "Invalid address for giv at insn %d\n",
3013 INSN_UID (v->insn));
3014 continue;
3015 }
3016 }
3017
3018 /* Store the value of dest_reg into the insn. This sharing
3019 will not be a problem as this insn will always be copied
3020 later. */
3021
3022 *v->location = v->dest_reg;
3023
3024 /* If this address giv is combined with a dest reg giv, then
3025 save the base giv's induction pointer so that we will be
3026 able to handle this address giv properly. The base giv
3027 itself does not have to be splittable. */
3028
3029 if (v->same && v->same->giv_type == DEST_REG)
3030 addr_combined_regs[REGNO (v->same->new_reg)] = v->same;
3031
3032 if (GET_CODE (v->new_reg) == REG)
3033 {
3034 /* This giv maybe hasn't been combined with any others.
3035 Make sure that it's giv is marked as splittable here. */
3036
3037 splittable_regs[REGNO (v->new_reg)] = value;
3038
3039 /* Make it appear to depend upon itself, so that the
3040 giv will be properly split in the main loop above. */
3041 if (! v->same)
3042 {
3043 v->same = v;
3044 addr_combined_regs[REGNO (v->new_reg)] = v;
3045 }
3046 }
3047
3048 if (loop_dump_stream)
3049 fprintf (loop_dump_stream, "DEST_ADDR giv being split.\n");
3050 }
3051 }
3052 else
3053 {
3054 #if 0
3055 /* Currently, unreduced giv's can't be split. This is not too much
3056 of a problem since unreduced giv's are not live across loop
3057 iterations anyways. When unrolling a loop completely though,
3058 it makes sense to reduce&split givs when possible, as this will
3059 result in simpler instructions, and will not require that a reg
3060 be live across loop iterations. */
3061
3062 splittable_regs[REGNO (v->dest_reg)] = value;
3063 fprintf (stderr, "Giv %d at insn %d not reduced\n",
3064 REGNO (v->dest_reg), INSN_UID (v->insn));
3065 #else
3066 continue;
3067 #endif
3068 }
3069
3070 /* Unreduced givs are only updated once by definition. Reduced givs
3071 are updated as many times as their biv is. Mark it so if this is
3072 a splittable register. Don't need to do anything for address givs
3073 where this may not be a register. */
3074
3075 if (GET_CODE (v->new_reg) == REG)
3076 {
3077 int count = 1;
3078 if (! v->ignore)
3079 count = reg_biv_class[REGNO (v->src_reg)]->biv_count;
3080
3081 splittable_regs_updates[REGNO (v->new_reg)] = count;
3082 }
3083
3084 result++;
3085
3086 if (loop_dump_stream)
3087 {
3088 int regnum;
3089
3090 if (GET_CODE (v->dest_reg) == CONST_INT)
3091 regnum = -1;
3092 else if (GET_CODE (v->dest_reg) != REG)
3093 regnum = REGNO (XEXP (v->dest_reg, 0));
3094 else
3095 regnum = REGNO (v->dest_reg);
3096 fprintf (loop_dump_stream, "Giv %d at insn %d safe to split.\n",
3097 regnum, INSN_UID (v->insn));
3098 }
3099 }
3100
3101 return result;
3102 }
3103 \f
3104 /* Try to prove that the register is dead after the loop exits. Trace every
3105 loop exit looking for an insn that will always be executed, which sets
3106 the register to some value, and appears before the first use of the register
3107 is found. If successful, then return 1, otherwise return 0. */
3108
3109 /* ?? Could be made more intelligent in the handling of jumps, so that
3110 it can search past if statements and other similar structures. */
3111
3112 static int
3113 reg_dead_after_loop (reg, loop_start, loop_end)
3114 rtx reg, loop_start, loop_end;
3115 {
3116 rtx insn, label;
3117 enum rtx_code code;
3118 int jump_count = 0;
3119 int label_count = 0;
3120 int this_loop_num = uid_loop_num[INSN_UID (loop_start)];
3121
3122 /* In addition to checking all exits of this loop, we must also check
3123 all exits of inner nested loops that would exit this loop. We don't
3124 have any way to identify those, so we just give up if there are any
3125 such inner loop exits. */
3126
3127 for (label = loop_number_exit_labels[this_loop_num]; label;
3128 label = LABEL_NEXTREF (label))
3129 label_count++;
3130
3131 if (label_count != loop_number_exit_count[this_loop_num])
3132 return 0;
3133
3134 /* HACK: Must also search the loop fall through exit, create a label_ref
3135 here which points to the loop_end, and append the loop_number_exit_labels
3136 list to it. */
3137 label = gen_rtx_LABEL_REF (VOIDmode, loop_end);
3138 LABEL_NEXTREF (label) = loop_number_exit_labels[this_loop_num];
3139
3140 for ( ; label; label = LABEL_NEXTREF (label))
3141 {
3142 /* Succeed if find an insn which sets the biv or if reach end of
3143 function. Fail if find an insn that uses the biv, or if come to
3144 a conditional jump. */
3145
3146 insn = NEXT_INSN (XEXP (label, 0));
3147 while (insn)
3148 {
3149 code = GET_CODE (insn);
3150 if (GET_RTX_CLASS (code) == 'i')
3151 {
3152 rtx set;
3153
3154 if (reg_referenced_p (reg, PATTERN (insn)))
3155 return 0;
3156
3157 set = single_set (insn);
3158 if (set && rtx_equal_p (SET_DEST (set), reg))
3159 break;
3160 }
3161
3162 if (code == JUMP_INSN)
3163 {
3164 if (GET_CODE (PATTERN (insn)) == RETURN)
3165 break;
3166 else if (! simplejump_p (insn)
3167 /* Prevent infinite loop following infinite loops. */
3168 || jump_count++ > 20)
3169 return 0;
3170 else
3171 insn = JUMP_LABEL (insn);
3172 }
3173
3174 insn = NEXT_INSN (insn);
3175 }
3176 }
3177
3178 /* Success, the register is dead on all loop exits. */
3179 return 1;
3180 }
3181
3182 /* Try to calculate the final value of the biv, the value it will have at
3183 the end of the loop. If we can do it, return that value. */
3184
3185 rtx
3186 final_biv_value (bl, loop_start, loop_end)
3187 struct iv_class *bl;
3188 rtx loop_start, loop_end;
3189 {
3190 rtx increment, tem;
3191
3192 /* ??? This only works for MODE_INT biv's. Reject all others for now. */
3193
3194 if (GET_MODE_CLASS (bl->biv->mode) != MODE_INT)
3195 return 0;
3196
3197 /* The final value for reversed bivs must be calculated differently than
3198 for ordinary bivs. In this case, there is already an insn after the
3199 loop which sets this biv's final value (if necessary), and there are
3200 no other loop exits, so we can return any value. */
3201 if (bl->reversed)
3202 {
3203 if (loop_dump_stream)
3204 fprintf (loop_dump_stream,
3205 "Final biv value for %d, reversed biv.\n", bl->regno);
3206
3207 return const0_rtx;
3208 }
3209
3210 /* Try to calculate the final value as initial value + (number of iterations
3211 * increment). For this to work, increment must be invariant, the only
3212 exit from the loop must be the fall through at the bottom (otherwise
3213 it may not have its final value when the loop exits), and the initial
3214 value of the biv must be invariant. */
3215
3216 if (loop_n_iterations != 0
3217 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]]
3218 && invariant_p (bl->initial_value))
3219 {
3220 increment = biv_total_increment (bl, loop_start, loop_end);
3221
3222 if (increment && invariant_p (increment))
3223 {
3224 /* Can calculate the loop exit value, emit insns after loop
3225 end to calculate this value into a temporary register in
3226 case it is needed later. */
3227
3228 tem = gen_reg_rtx (bl->biv->mode);
3229 record_base_value (REGNO (tem), bl->biv->add_val, 0);
3230 /* Make sure loop_end is not the last insn. */
3231 if (NEXT_INSN (loop_end) == 0)
3232 emit_note_after (NOTE_INSN_DELETED, loop_end);
3233 emit_iv_add_mult (increment, GEN_INT (loop_n_iterations),
3234 bl->initial_value, tem, NEXT_INSN (loop_end));
3235
3236 if (loop_dump_stream)
3237 fprintf (loop_dump_stream,
3238 "Final biv value for %d, calculated.\n", bl->regno);
3239
3240 return tem;
3241 }
3242 }
3243
3244 /* Check to see if the biv is dead at all loop exits. */
3245 if (reg_dead_after_loop (bl->biv->src_reg, loop_start, loop_end))
3246 {
3247 if (loop_dump_stream)
3248 fprintf (loop_dump_stream,
3249 "Final biv value for %d, biv dead after loop exit.\n",
3250 bl->regno);
3251
3252 return const0_rtx;
3253 }
3254
3255 return 0;
3256 }
3257
3258 /* Try to calculate the final value of the giv, the value it will have at
3259 the end of the loop. If we can do it, return that value. */
3260
3261 rtx
3262 final_giv_value (v, loop_start, loop_end)
3263 struct induction *v;
3264 rtx loop_start, loop_end;
3265 {
3266 struct iv_class *bl;
3267 rtx insn;
3268 rtx increment, tem;
3269 rtx insert_before, seq;
3270
3271 bl = reg_biv_class[REGNO (v->src_reg)];
3272
3273 /* The final value for givs which depend on reversed bivs must be calculated
3274 differently than for ordinary givs. In this case, there is already an
3275 insn after the loop which sets this giv's final value (if necessary),
3276 and there are no other loop exits, so we can return any value. */
3277 if (bl->reversed)
3278 {
3279 if (loop_dump_stream)
3280 fprintf (loop_dump_stream,
3281 "Final giv value for %d, depends on reversed biv\n",
3282 REGNO (v->dest_reg));
3283 return const0_rtx;
3284 }
3285
3286 /* Try to calculate the final value as a function of the biv it depends
3287 upon. The only exit from the loop must be the fall through at the bottom
3288 (otherwise it may not have its final value when the loop exits). */
3289
3290 /* ??? Can calculate the final giv value by subtracting off the
3291 extra biv increments times the giv's mult_val. The loop must have
3292 only one exit for this to work, but the loop iterations does not need
3293 to be known. */
3294
3295 if (loop_n_iterations != 0
3296 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
3297 {
3298 /* ?? It is tempting to use the biv's value here since these insns will
3299 be put after the loop, and hence the biv will have its final value
3300 then. However, this fails if the biv is subsequently eliminated.
3301 Perhaps determine whether biv's are eliminable before trying to
3302 determine whether giv's are replaceable so that we can use the
3303 biv value here if it is not eliminable. */
3304
3305 /* We are emitting code after the end of the loop, so we must make
3306 sure that bl->initial_value is still valid then. It will still
3307 be valid if it is invariant. */
3308
3309 increment = biv_total_increment (bl, loop_start, loop_end);
3310
3311 if (increment && invariant_p (increment)
3312 && invariant_p (bl->initial_value))
3313 {
3314 /* Can calculate the loop exit value of its biv as
3315 (loop_n_iterations * increment) + initial_value */
3316
3317 /* The loop exit value of the giv is then
3318 (final_biv_value - extra increments) * mult_val + add_val.
3319 The extra increments are any increments to the biv which
3320 occur in the loop after the giv's value is calculated.
3321 We must search from the insn that sets the giv to the end
3322 of the loop to calculate this value. */
3323
3324 insert_before = NEXT_INSN (loop_end);
3325
3326 /* Put the final biv value in tem. */
3327 tem = gen_reg_rtx (bl->biv->mode);
3328 record_base_value (REGNO (tem), bl->biv->add_val, 0);
3329 emit_iv_add_mult (increment, GEN_INT (loop_n_iterations),
3330 bl->initial_value, tem, insert_before);
3331
3332 /* Subtract off extra increments as we find them. */
3333 for (insn = NEXT_INSN (v->insn); insn != loop_end;
3334 insn = NEXT_INSN (insn))
3335 {
3336 struct induction *biv;
3337
3338 for (biv = bl->biv; biv; biv = biv->next_iv)
3339 if (biv->insn == insn)
3340 {
3341 start_sequence ();
3342 tem = expand_binop (GET_MODE (tem), sub_optab, tem,
3343 biv->add_val, NULL_RTX, 0,
3344 OPTAB_LIB_WIDEN);
3345 seq = gen_sequence ();
3346 end_sequence ();
3347 emit_insn_before (seq, insert_before);
3348 }
3349 }
3350
3351 /* Now calculate the giv's final value. */
3352 emit_iv_add_mult (tem, v->mult_val, v->add_val, tem,
3353 insert_before);
3354
3355 if (loop_dump_stream)
3356 fprintf (loop_dump_stream,
3357 "Final giv value for %d, calc from biv's value.\n",
3358 REGNO (v->dest_reg));
3359
3360 return tem;
3361 }
3362 }
3363
3364 /* Replaceable giv's should never reach here. */
3365 if (v->replaceable)
3366 abort ();
3367
3368 /* Check to see if the biv is dead at all loop exits. */
3369 if (reg_dead_after_loop (v->dest_reg, loop_start, loop_end))
3370 {
3371 if (loop_dump_stream)
3372 fprintf (loop_dump_stream,
3373 "Final giv value for %d, giv dead after loop exit.\n",
3374 REGNO (v->dest_reg));
3375
3376 return const0_rtx;
3377 }
3378
3379 return 0;
3380 }
3381
3382
3383 /* Calculate the number of loop iterations. Returns the exact number of loop
3384 iterations if it can be calculated, otherwise returns zero. */
3385
3386 unsigned HOST_WIDE_INT
3387 loop_iterations (loop_start, loop_end)
3388 rtx loop_start, loop_end;
3389 {
3390 rtx comparison, comparison_value;
3391 rtx iteration_var, initial_value, increment, final_value;
3392 enum rtx_code comparison_code;
3393 HOST_WIDE_INT i;
3394 int increment_dir;
3395 int unsigned_compare, compare_dir, final_larger;
3396 unsigned long tempu;
3397 rtx last_loop_insn;
3398
3399 /* First find the iteration variable. If the last insn is a conditional
3400 branch, and the insn before tests a register value, make that the
3401 iteration variable. */
3402
3403 loop_initial_value = 0;
3404 loop_increment = 0;
3405 loop_final_value = 0;
3406 loop_iteration_var = 0;
3407
3408 /* We used to use pren_nonnote_insn here, but that fails because it might
3409 accidentally get the branch for a contained loop if the branch for this
3410 loop was deleted. We can only trust branches immediately before the
3411 loop_end. */
3412 last_loop_insn = PREV_INSN (loop_end);
3413
3414 comparison = get_condition_for_loop (last_loop_insn);
3415 if (comparison == 0)
3416 {
3417 if (loop_dump_stream)
3418 fprintf (loop_dump_stream,
3419 "Loop unrolling: No final conditional branch found.\n");
3420 return 0;
3421 }
3422
3423 /* ??? Get_condition may switch position of induction variable and
3424 invariant register when it canonicalizes the comparison. */
3425
3426 comparison_code = GET_CODE (comparison);
3427 iteration_var = XEXP (comparison, 0);
3428 comparison_value = XEXP (comparison, 1);
3429
3430 if (GET_CODE (iteration_var) != REG)
3431 {
3432 if (loop_dump_stream)
3433 fprintf (loop_dump_stream,
3434 "Loop unrolling: Comparison not against register.\n");
3435 return 0;
3436 }
3437
3438 /* Loop iterations is always called before any new registers are created
3439 now, so this should never occur. */
3440
3441 if (REGNO (iteration_var) >= max_reg_before_loop)
3442 abort ();
3443
3444 iteration_info (iteration_var, &initial_value, &increment,
3445 loop_start, loop_end);
3446 if (initial_value == 0)
3447 /* iteration_info already printed a message. */
3448 return 0;
3449
3450 /* If the comparison value is an invariant register, then try to find
3451 its value from the insns before the start of the loop. */
3452
3453 if (GET_CODE (comparison_value) == REG && invariant_p (comparison_value))
3454 {
3455 rtx insn, set;
3456
3457 for (insn = PREV_INSN (loop_start); insn ; insn = PREV_INSN (insn))
3458 {
3459 if (GET_CODE (insn) == CODE_LABEL)
3460 break;
3461
3462 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
3463 && reg_set_p (comparison_value, insn))
3464 {
3465 /* We found the last insn before the loop that sets the register.
3466 If it sets the entire register, and has a REG_EQUAL note,
3467 then use the value of the REG_EQUAL note. */
3468 if ((set = single_set (insn))
3469 && (SET_DEST (set) == comparison_value))
3470 {
3471 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3472
3473 /* Only use the REG_EQUAL note if it is a constant.
3474 Other things, divide in particular, will cause
3475 problems later if we use them. */
3476 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3477 && CONSTANT_P (XEXP (note, 0)))
3478 comparison_value = XEXP (note, 0);
3479 }
3480 break;
3481 }
3482 }
3483 }
3484
3485 final_value = approx_final_value (comparison_code, comparison_value,
3486 &unsigned_compare, &compare_dir);
3487
3488 /* Save the calculated values describing this loop's bounds, in case
3489 precondition_loop_p will need them later. These values can not be
3490 recalculated inside precondition_loop_p because strength reduction
3491 optimizations may obscure the loop's structure. */
3492
3493 loop_iteration_var = iteration_var;
3494 loop_initial_value = initial_value;
3495 loop_increment = increment;
3496 loop_final_value = final_value;
3497 loop_comparison_code = comparison_code;
3498
3499 if (increment == 0)
3500 {
3501 if (loop_dump_stream)
3502 fprintf (loop_dump_stream,
3503 "Loop unrolling: Increment value can't be calculated.\n");
3504 return 0;
3505 }
3506 else if (GET_CODE (increment) != CONST_INT)
3507 {
3508 if (loop_dump_stream)
3509 fprintf (loop_dump_stream,
3510 "Loop unrolling: Increment value not constant.\n");
3511 return 0;
3512 }
3513 else if (GET_CODE (initial_value) != CONST_INT)
3514 {
3515 if (loop_dump_stream)
3516 fprintf (loop_dump_stream,
3517 "Loop unrolling: Initial value not constant.\n");
3518 return 0;
3519 }
3520 else if (final_value == 0)
3521 {
3522 if (loop_dump_stream)
3523 fprintf (loop_dump_stream,
3524 "Loop unrolling: EQ comparison loop.\n");
3525 return 0;
3526 }
3527 else if (GET_CODE (final_value) != CONST_INT)
3528 {
3529 if (loop_dump_stream)
3530 fprintf (loop_dump_stream,
3531 "Loop unrolling: Final value not constant.\n");
3532 return 0;
3533 }
3534
3535 /* ?? Final value and initial value do not have to be constants.
3536 Only their difference has to be constant. When the iteration variable
3537 is an array address, the final value and initial value might both
3538 be addresses with the same base but different constant offsets.
3539 Final value must be invariant for this to work.
3540
3541 To do this, need some way to find the values of registers which are
3542 invariant. */
3543
3544 /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1. */
3545 if (unsigned_compare)
3546 final_larger
3547 = ((unsigned HOST_WIDE_INT) INTVAL (final_value)
3548 > (unsigned HOST_WIDE_INT) INTVAL (initial_value))
3549 - ((unsigned HOST_WIDE_INT) INTVAL (final_value)
3550 < (unsigned HOST_WIDE_INT) INTVAL (initial_value));
3551 else
3552 final_larger = (INTVAL (final_value) > INTVAL (initial_value))
3553 - (INTVAL (final_value) < INTVAL (initial_value));
3554
3555 if (INTVAL (increment) > 0)
3556 increment_dir = 1;
3557 else if (INTVAL (increment) == 0)
3558 increment_dir = 0;
3559 else
3560 increment_dir = -1;
3561
3562 /* There are 27 different cases: compare_dir = -1, 0, 1;
3563 final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
3564 There are 4 normal cases, 4 reverse cases (where the iteration variable
3565 will overflow before the loop exits), 4 infinite loop cases, and 15
3566 immediate exit (0 or 1 iteration depending on loop type) cases.
3567 Only try to optimize the normal cases. */
3568
3569 /* (compare_dir/final_larger/increment_dir)
3570 Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
3571 Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
3572 Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
3573 Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
3574
3575 /* ?? If the meaning of reverse loops (where the iteration variable
3576 will overflow before the loop exits) is undefined, then could
3577 eliminate all of these special checks, and just always assume
3578 the loops are normal/immediate/infinite. Note that this means
3579 the sign of increment_dir does not have to be known. Also,
3580 since it does not really hurt if immediate exit loops or infinite loops
3581 are optimized, then that case could be ignored also, and hence all
3582 loops can be optimized.
3583
3584 According to ANSI Spec, the reverse loop case result is undefined,
3585 because the action on overflow is undefined.
3586
3587 See also the special test for NE loops below. */
3588
3589 if (final_larger == increment_dir && final_larger != 0
3590 && (final_larger == compare_dir || compare_dir == 0))
3591 /* Normal case. */
3592 ;
3593 else
3594 {
3595 if (loop_dump_stream)
3596 fprintf (loop_dump_stream,
3597 "Loop unrolling: Not normal loop.\n");
3598 return 0;
3599 }
3600
3601 /* Calculate the number of iterations, final_value is only an approximation,
3602 so correct for that. Note that tempu and loop_n_iterations are
3603 unsigned, because they can be as large as 2^n - 1. */
3604
3605 i = INTVAL (increment);
3606 if (i > 0)
3607 tempu = INTVAL (final_value) - INTVAL (initial_value);
3608 else if (i < 0)
3609 {
3610 tempu = INTVAL (initial_value) - INTVAL (final_value);
3611 i = -i;
3612 }
3613 else
3614 abort ();
3615
3616 /* For NE tests, make sure that the iteration variable won't miss the
3617 final value. If tempu mod i is not zero, then the iteration variable
3618 will overflow before the loop exits, and we can not calculate the
3619 number of iterations. */
3620 if (compare_dir == 0 && (tempu % i) != 0)
3621 return 0;
3622
3623 return tempu / i + ((tempu % i) != 0);
3624 }
3625
3626 /* Replace uses of split bivs with their split pseudo register. This is
3627 for original instructions which remain after loop unrolling without
3628 copying. */
3629
3630 static rtx
3631 remap_split_bivs (x)
3632 rtx x;
3633 {
3634 register enum rtx_code code;
3635 register int i;
3636 register char *fmt;
3637
3638 if (x == 0)
3639 return x;
3640
3641 code = GET_CODE (x);
3642 switch (code)
3643 {
3644 case SCRATCH:
3645 case PC:
3646 case CC0:
3647 case CONST_INT:
3648 case CONST_DOUBLE:
3649 case CONST:
3650 case SYMBOL_REF:
3651 case LABEL_REF:
3652 return x;
3653
3654 case REG:
3655 #if 0
3656 /* If non-reduced/final-value givs were split, then this would also
3657 have to remap those givs also. */
3658 #endif
3659 if (REGNO (x) < max_reg_before_loop
3660 && reg_iv_type[REGNO (x)] == BASIC_INDUCT)
3661 return reg_biv_class[REGNO (x)]->biv->src_reg;
3662 break;
3663
3664 default:
3665 break;
3666 }
3667
3668 fmt = GET_RTX_FORMAT (code);
3669 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3670 {
3671 if (fmt[i] == 'e')
3672 XEXP (x, i) = remap_split_bivs (XEXP (x, i));
3673 if (fmt[i] == 'E')
3674 {
3675 register int j;
3676 for (j = 0; j < XVECLEN (x, i); j++)
3677 XVECEXP (x, i, j) = remap_split_bivs (XVECEXP (x, i, j));
3678 }
3679 }
3680 return x;
3681 }
3682
3683 /* If FIRST_UID is a set of REGNO, and FIRST_UID dominates LAST_UID (e.g.
3684 FIST_UID is always executed if LAST_UID is), then return 1. Otherwise
3685 return 0. COPY_START is where we can start looking for the insns
3686 FIRST_UID and LAST_UID. COPY_END is where we stop looking for these
3687 insns.
3688
3689 If there is no JUMP_INSN between LOOP_START and FIRST_UID, then FIRST_UID
3690 must dominate LAST_UID.
3691
3692 If there is a CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
3693 may not dominate LAST_UID.
3694
3695 If there is no CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
3696 must dominate LAST_UID. */
3697
3698 int
3699 set_dominates_use (regno, first_uid, last_uid, copy_start, copy_end)
3700 int regno;
3701 int first_uid;
3702 int last_uid;
3703 rtx copy_start;
3704 rtx copy_end;
3705 {
3706 int passed_jump = 0;
3707 rtx p = NEXT_INSN (copy_start);
3708
3709 while (INSN_UID (p) != first_uid)
3710 {
3711 if (GET_CODE (p) == JUMP_INSN)
3712 passed_jump= 1;
3713 /* Could not find FIRST_UID. */
3714 if (p == copy_end)
3715 return 0;
3716 p = NEXT_INSN (p);
3717 }
3718
3719 /* Verify that FIRST_UID is an insn that entirely sets REGNO. */
3720 if (GET_RTX_CLASS (GET_CODE (p)) != 'i'
3721 || ! dead_or_set_regno_p (p, regno))
3722 return 0;
3723
3724 /* FIRST_UID is always executed. */
3725 if (passed_jump == 0)
3726 return 1;
3727
3728 while (INSN_UID (p) != last_uid)
3729 {
3730 /* If we see a CODE_LABEL between FIRST_UID and LAST_UID, then we
3731 can not be sure that FIRST_UID dominates LAST_UID. */
3732 if (GET_CODE (p) == CODE_LABEL)
3733 return 0;
3734 /* Could not find LAST_UID, but we reached the end of the loop, so
3735 it must be safe. */
3736 else if (p == copy_end)
3737 return 1;
3738 p = NEXT_INSN (p);
3739 }
3740
3741 /* FIRST_UID is always executed if LAST_UID is executed. */
3742 return 1;
3743 }