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