Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / loop.c
blob4d27b8a7ee99b5f591994abe701f71b1a460017a
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 88, 89, 91-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include "system.h"
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
52 /* Vector mapping INSN_UIDs to luids.
53 The luids are like uids but increase monotonically always.
54 We use them to see whether a jump comes from outside a given loop. */
56 int *uid_luid;
58 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
59 number the insn is contained in. */
61 int *uid_loop_num;
63 /* 1 + largest uid of any insn. */
65 int max_uid_for_loop;
67 /* 1 + luid of last insn. */
69 static int max_luid;
71 /* Number of loops detected in current function. Used as index to the
72 next few tables. */
74 static int max_loop_num;
76 /* Indexed by loop number, contains the first and last insn of each loop. */
78 static rtx *loop_number_loop_starts, *loop_number_loop_ends;
80 /* For each loop, gives the containing loop number, -1 if none. */
82 int *loop_outer_loop;
84 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
85 really a loop (an insn outside the loop branches into it). */
87 static char *loop_invalid;
89 /* Indexed by loop number, links together all LABEL_REFs which refer to
90 code labels outside the loop. Used by routines that need to know all
91 loop exits, such as final_biv_value and final_giv_value.
93 This does not include loop exits due to return instructions. This is
94 because all bivs and givs are pseudos, and hence must be dead after a
95 return, so the presense of a return does not affect any of the
96 optimizations that use this info. It is simpler to just not include return
97 instructions on this list. */
99 rtx *loop_number_exit_labels;
101 /* Indexed by loop number, counts the number of LABEL_REFs on
102 loop_number_exit_labels for this loop and all loops nested inside it. */
104 int *loop_number_exit_count;
106 /* Holds the number of loop iterations. It is zero if the number could not be
107 calculated. Must be unsigned since the number of iterations can
108 be as high as 2^wordsize-1. For loops with a wider iterator, this number
109 will will be zero if the number of loop iterations is too large for an
110 unsigned integer to hold. */
112 unsigned HOST_WIDE_INT loop_n_iterations;
114 /* Nonzero if there is a subroutine call in the current loop.
115 (unknown_address_altered is also nonzero in this case.) */
117 static int loop_has_call;
119 /* Nonzero if there is a volatile memory reference in the current
120 loop. */
122 static int loop_has_volatile;
124 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
125 current loop. A continue statement will generate a branch to
126 NEXT_INSN (loop_continue). */
128 static rtx loop_continue;
130 /* Indexed by register number, contains the number of times the reg
131 is set during the loop being scanned.
132 During code motion, a negative value indicates a reg that has been
133 made a candidate; in particular -2 means that it is an candidate that
134 we know is equal to a constant and -1 means that it is an candidate
135 not known equal to a constant.
136 After code motion, regs moved have 0 (which is accurate now)
137 while the failed candidates have the original number of times set.
139 Therefore, at all times, == 0 indicates an invariant register;
140 < 0 a conditionally invariant one. */
142 static int *n_times_set;
144 /* Original value of n_times_set; same except that this value
145 is not set negative for a reg whose sets have been made candidates
146 and not set to 0 for a reg that is moved. */
148 static int *n_times_used;
150 /* Index by register number, 1 indicates that the register
151 cannot be moved or strength reduced. */
153 static char *may_not_optimize;
155 /* Nonzero means reg N has already been moved out of one loop.
156 This reduces the desire to move it out of another. */
158 static char *moved_once;
160 /* Array of MEMs that are stored in this loop. If there are too many to fit
161 here, we just turn on unknown_address_altered. */
163 #define NUM_STORES 20
164 static rtx loop_store_mems[NUM_STORES];
166 /* Index of first available slot in above array. */
167 static int loop_store_mems_idx;
169 /* Nonzero if we don't know what MEMs were changed in the current loop.
170 This happens if the loop contains a call (in which case `loop_has_call'
171 will also be set) or if we store into more than NUM_STORES MEMs. */
173 static int unknown_address_altered;
175 /* Count of movable (i.e. invariant) instructions discovered in the loop. */
176 static int num_movables;
178 /* Count of memory write instructions discovered in the loop. */
179 static int num_mem_sets;
181 /* Number of loops contained within the current one, including itself. */
182 static int loops_enclosed;
184 /* Bound on pseudo register number before loop optimization.
185 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
186 int max_reg_before_loop;
188 /* This obstack is used in product_cheap_p to allocate its rtl. It
189 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
190 If we used the same obstack that it did, we would be deallocating
191 that array. */
193 static struct obstack temp_obstack;
195 /* This is where the pointer to the obstack being used for RTL is stored. */
197 extern struct obstack *rtl_obstack;
199 #define obstack_chunk_alloc xmalloc
200 #define obstack_chunk_free free
202 extern char *oballoc ();
204 /* During the analysis of a loop, a chain of `struct movable's
205 is made to record all the movable insns found.
206 Then the entire chain can be scanned to decide which to move. */
208 struct movable
210 rtx insn; /* A movable insn */
211 rtx set_src; /* The expression this reg is set from. */
212 rtx set_dest; /* The destination of this SET. */
213 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
214 of any registers used within the LIBCALL. */
215 int consec; /* Number of consecutive following insns
216 that must be moved with this one. */
217 int regno; /* The register it sets */
218 short lifetime; /* lifetime of that register;
219 may be adjusted when matching movables
220 that load the same value are found. */
221 short savings; /* Number of insns we can move for this reg,
222 including other movables that force this
223 or match this one. */
224 unsigned int cond : 1; /* 1 if only conditionally movable */
225 unsigned int force : 1; /* 1 means MUST move this insn */
226 unsigned int global : 1; /* 1 means reg is live outside this loop */
227 /* If PARTIAL is 1, GLOBAL means something different:
228 that the reg is live outside the range from where it is set
229 to the following label. */
230 unsigned int done : 1; /* 1 inhibits further processing of this */
232 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
233 In particular, moving it does not make it
234 invariant. */
235 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
236 load SRC, rather than copying INSN. */
237 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
238 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
239 that we should avoid changing when clearing
240 the rest of the reg. */
241 struct movable *match; /* First entry for same value */
242 struct movable *forces; /* An insn that must be moved if this is */
243 struct movable *next;
246 FILE *loop_dump_stream;
248 /* Forward declarations. */
250 static void find_and_verify_loops ();
251 static void mark_loop_jump ();
252 static void prescan_loop ();
253 static int reg_in_basic_block_p ();
254 static int consec_sets_invariant_p ();
255 static rtx libcall_other_reg ();
256 static int labels_in_range_p ();
257 static void count_loop_regs_set ();
258 static void note_addr_stored ();
259 static int loop_reg_used_before_p ();
260 static void scan_loop ();
261 #if 0
262 static void replace_call_address ();
263 #endif
264 static rtx skip_consec_insns ();
265 static int libcall_benefit ();
266 static void ignore_some_movables ();
267 static void force_movables ();
268 static void combine_movables ();
269 static int rtx_equal_for_loop_p ();
270 static void move_movables ();
271 static void strength_reduce ();
272 static int valid_initial_value_p ();
273 static void find_mem_givs ();
274 static void record_biv ();
275 static void check_final_value ();
276 static void record_giv ();
277 static void update_giv_derive ();
278 static int basic_induction_var ();
279 static rtx simplify_giv_expr ();
280 static int general_induction_var ();
281 static int consec_sets_giv ();
282 static int check_dbra_loop ();
283 static rtx express_from ();
284 static int combine_givs_p ();
285 static void combine_givs ();
286 static int product_cheap_p ();
287 static int maybe_eliminate_biv ();
288 static int maybe_eliminate_biv_1 ();
289 static int last_use_this_basic_block ();
290 static void record_initial ();
291 static void update_reg_last_use ();
293 /* Relative gain of eliminating various kinds of operations. */
294 int add_cost;
295 #if 0
296 int shift_cost;
297 int mult_cost;
298 #endif
300 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
301 copy the value of the strength reduced giv to its original register. */
302 int copy_cost;
304 void
305 init_loop ()
307 char *free_point = (char *) oballoc (1);
308 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
310 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
312 /* We multiply by 2 to reconcile the difference in scale between
313 these two ways of computing costs. Otherwise the cost of a copy
314 will be far less than the cost of an add. */
316 copy_cost = 2 * 2;
318 /* Free the objects we just allocated. */
319 obfree (free_point);
321 /* Initialize the obstack used for rtl in product_cheap_p. */
322 gcc_obstack_init (&temp_obstack);
325 /* Entry point of this file. Perform loop optimization
326 on the current function. F is the first insn of the function
327 and DUMPFILE is a stream for output of a trace of actions taken
328 (or 0 if none should be output). */
330 void
331 loop_optimize (f, dumpfile)
332 /* f is the first instruction of a chain of insns for one function */
333 rtx f;
334 FILE *dumpfile;
336 register rtx insn;
337 register int i;
338 rtx last_insn;
340 loop_dump_stream = dumpfile;
342 init_recog_no_volatile ();
343 init_alias_analysis ();
345 max_reg_before_loop = max_reg_num ();
347 moved_once = (char *) alloca (max_reg_before_loop);
348 bzero (moved_once, max_reg_before_loop);
350 regs_may_share = 0;
352 /* Count the number of loops. */
354 max_loop_num = 0;
355 for (insn = f; insn; insn = NEXT_INSN (insn))
357 if (GET_CODE (insn) == NOTE
358 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
359 max_loop_num++;
362 /* Don't waste time if no loops. */
363 if (max_loop_num == 0)
364 return;
366 /* Get size to use for tables indexed by uids.
367 Leave some space for labels allocated by find_and_verify_loops. */
368 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
370 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
371 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
373 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
374 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
376 /* Allocate tables for recording each loop. We set each entry, so they need
377 not be zeroed. */
378 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
379 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
380 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
381 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
382 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
383 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
385 /* Find and process each loop.
386 First, find them, and record them in order of their beginnings. */
387 find_and_verify_loops (f);
389 /* Now find all register lifetimes. This must be done after
390 find_and_verify_loops, because it might reorder the insns in the
391 function. */
392 reg_scan (f, max_reg_num (), 1);
394 /* See if we went too far. */
395 if (get_max_uid () > max_uid_for_loop)
396 abort ();
397 /* Now reset it to the actual size we need. See above. */
398 max_uid_for_loop = get_max_uid () + 1;
400 /* Compute the mapping from uids to luids.
401 LUIDs are numbers assigned to insns, like uids,
402 except that luids increase monotonically through the code.
403 Don't assign luids to line-number NOTEs, so that the distance in luids
404 between two insns is not affected by -g. */
406 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
408 last_insn = insn;
409 if (GET_CODE (insn) != NOTE
410 || NOTE_LINE_NUMBER (insn) <= 0)
411 uid_luid[INSN_UID (insn)] = ++i;
412 else
413 /* Give a line number note the same luid as preceding insn. */
414 uid_luid[INSN_UID (insn)] = i;
417 max_luid = i + 1;
419 /* Don't leave gaps in uid_luid for insns that have been
420 deleted. It is possible that the first or last insn
421 using some register has been deleted by cross-jumping.
422 Make sure that uid_luid for that former insn's uid
423 points to the general area where that insn used to be. */
424 for (i = 0; i < max_uid_for_loop; i++)
426 uid_luid[0] = uid_luid[i];
427 if (uid_luid[0] != 0)
428 break;
430 for (i = 0; i < max_uid_for_loop; i++)
431 if (uid_luid[i] == 0)
432 uid_luid[i] = uid_luid[i - 1];
434 /* Create a mapping from loops to BLOCK tree nodes. */
435 if (flag_unroll_loops && write_symbols != NO_DEBUG)
436 find_loop_tree_blocks ();
438 /* Now scan the loops, last ones first, since this means inner ones are done
439 before outer ones. */
440 for (i = max_loop_num-1; i >= 0; i--)
441 if (! loop_invalid[i] && loop_number_loop_ends[i])
442 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
443 max_reg_num ());
445 /* If debugging and unrolling loops, we must replicate the tree nodes
446 corresponding to the blocks inside the loop, so that the original one
447 to one mapping will remain. */
448 if (flag_unroll_loops && write_symbols != NO_DEBUG)
449 unroll_block_trees ();
452 /* Optimize one loop whose start is LOOP_START and end is END.
453 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
454 NOTE_INSN_LOOP_END. */
456 /* ??? Could also move memory writes out of loops if the destination address
457 is invariant, the source is invariant, the memory write is not volatile,
458 and if we can prove that no read inside the loop can read this address
459 before the write occurs. If there is a read of this address after the
460 write, then we can also mark the memory read as invariant. */
462 static void
463 scan_loop (loop_start, end, nregs)
464 rtx loop_start, end;
465 int nregs;
467 register int i;
468 register rtx p;
469 /* 1 if we are scanning insns that could be executed zero times. */
470 int maybe_never = 0;
471 /* 1 if we are scanning insns that might never be executed
472 due to a subroutine call which might exit before they are reached. */
473 int call_passed = 0;
474 /* For a rotated loop that is entered near the bottom,
475 this is the label at the top. Otherwise it is zero. */
476 rtx loop_top = 0;
477 /* Jump insn that enters the loop, or 0 if control drops in. */
478 rtx loop_entry_jump = 0;
479 /* Place in the loop where control enters. */
480 rtx scan_start;
481 /* Number of insns in the loop. */
482 int insn_count;
483 int in_libcall = 0;
484 int tem;
485 rtx temp;
486 /* The SET from an insn, if it is the only SET in the insn. */
487 rtx set, set1;
488 /* Chain describing insns movable in current loop. */
489 struct movable *movables = 0;
490 /* Last element in `movables' -- so we can add elements at the end. */
491 struct movable *last_movable = 0;
492 /* Ratio of extra register life span we can justify
493 for saving an instruction. More if loop doesn't call subroutines
494 since in that case saving an insn makes more difference
495 and more registers are available. */
496 int threshold;
497 /* If we have calls, contains the insn in which a register was used
498 if it was used exactly once; contains const0_rtx if it was used more
499 than once. */
500 rtx *reg_single_usage = 0;
501 /* Nonzero if we are scanning instructions in a sub-loop. */
502 int loop_depth = 0;
504 n_times_set = (int *) alloca (nregs * sizeof (int));
505 n_times_used = (int *) alloca (nregs * sizeof (int));
506 may_not_optimize = (char *) alloca (nregs);
508 /* Determine whether this loop starts with a jump down to a test at
509 the end. This will occur for a small number of loops with a test
510 that is too complex to duplicate in front of the loop.
512 We search for the first insn or label in the loop, skipping NOTEs.
513 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
514 (because we might have a loop executed only once that contains a
515 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
516 (in case we have a degenerate loop).
518 Note that if we mistakenly think that a loop is entered at the top
519 when, in fact, it is entered at the exit test, the only effect will be
520 slightly poorer optimization. Making the opposite error can generate
521 incorrect code. Since very few loops now start with a jump to the
522 exit test, the code here to detect that case is very conservative. */
524 for (p = NEXT_INSN (loop_start);
525 p != end
526 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
527 && (GET_CODE (p) != NOTE
528 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
529 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
530 p = NEXT_INSN (p))
533 scan_start = p;
535 /* Set up variables describing this loop. */
536 prescan_loop (loop_start, end);
537 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
539 /* If loop has a jump before the first label,
540 the true entry is the target of that jump.
541 Start scan from there.
542 But record in LOOP_TOP the place where the end-test jumps
543 back to so we can scan that after the end of the loop. */
544 if (GET_CODE (p) == JUMP_INSN)
546 loop_entry_jump = p;
548 /* Loop entry must be unconditional jump (and not a RETURN) */
549 if (simplejump_p (p)
550 && JUMP_LABEL (p) != 0
551 /* Check to see whether the jump actually
552 jumps out of the loop (meaning it's no loop).
553 This case can happen for things like
554 do {..} while (0). If this label was generated previously
555 by loop, we can't tell anything about it and have to reject
556 the loop. */
557 && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
558 && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
559 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
561 loop_top = next_label (scan_start);
562 scan_start = JUMP_LABEL (p);
566 /* If SCAN_START was an insn created by loop, we don't know its luid
567 as required by loop_reg_used_before_p. So skip such loops. (This
568 test may never be true, but it's best to play it safe.)
570 Also, skip loops where we do not start scanning at a label. This
571 test also rejects loops starting with a JUMP_INSN that failed the
572 test above. */
574 if (INSN_UID (scan_start) >= max_uid_for_loop
575 || GET_CODE (scan_start) != CODE_LABEL)
577 if (loop_dump_stream)
578 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
579 INSN_UID (loop_start), INSN_UID (end));
580 return;
583 /* Count number of times each reg is set during this loop.
584 Set may_not_optimize[I] if it is not safe to move out
585 the setting of register I. If this loop has calls, set
586 reg_single_usage[I]. */
588 bzero ((char *) n_times_set, nregs * sizeof (int));
589 bzero (may_not_optimize, nregs);
591 if (loop_has_call)
593 reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
594 bzero ((char *) reg_single_usage, nregs * sizeof (rtx));
597 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
598 may_not_optimize, reg_single_usage, &insn_count, nregs);
600 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
601 may_not_optimize[i] = 1, n_times_set[i] = 1;
602 bcopy ((char *) n_times_set, (char *) n_times_used, nregs * sizeof (int));
604 if (loop_dump_stream)
606 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
607 INSN_UID (loop_start), INSN_UID (end), insn_count);
608 if (loop_continue)
609 fprintf (loop_dump_stream, "Continue at insn %d.\n",
610 INSN_UID (loop_continue));
613 /* Scan through the loop finding insns that are safe to move.
614 Set n_times_set negative for the reg being set, so that
615 this reg will be considered invariant for subsequent insns.
616 We consider whether subsequent insns use the reg
617 in deciding whether it is worth actually moving.
619 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
620 and therefore it is possible that the insns we are scanning
621 would never be executed. At such times, we must make sure
622 that it is safe to execute the insn once instead of zero times.
623 When MAYBE_NEVER is 0, all insns will be executed at least once
624 so that is not a problem. */
626 p = scan_start;
627 while (1)
629 p = NEXT_INSN (p);
630 /* At end of a straight-in loop, we are done.
631 At end of a loop entered at the bottom, scan the top. */
632 if (p == scan_start)
633 break;
634 if (p == end)
636 if (loop_top != 0)
637 p = loop_top;
638 else
639 break;
640 if (p == scan_start)
641 break;
644 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
645 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
646 in_libcall = 1;
647 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
648 && find_reg_note (p, REG_RETVAL, NULL_RTX))
649 in_libcall = 0;
651 if (GET_CODE (p) == INSN
652 && (set = single_set (p))
653 && GET_CODE (SET_DEST (set)) == REG
654 && ! may_not_optimize[REGNO (SET_DEST (set))])
656 int tem1 = 0;
657 int tem2 = 0;
658 int move_insn = 0;
659 rtx src = SET_SRC (set);
660 rtx dependencies = 0;
662 /* Figure out what to use as a source of this insn. If a REG_EQUIV
663 note is given or if a REG_EQUAL note with a constant operand is
664 specified, use it as the source and mark that we should move
665 this insn by calling emit_move_insn rather that duplicating the
666 insn.
668 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
669 is present. */
670 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
671 if (temp)
672 src = XEXP (temp, 0), move_insn = 1;
673 else
675 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
676 if (temp && CONSTANT_P (XEXP (temp, 0)))
677 src = XEXP (temp, 0), move_insn = 1;
678 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
680 src = XEXP (temp, 0);
681 /* A libcall block can use regs that don't appear in
682 the equivalent expression. To move the libcall,
683 we must move those regs too. */
684 dependencies = libcall_other_reg (p, src);
688 /* Don't try to optimize a register that was made
689 by loop-optimization for an inner loop.
690 We don't know its life-span, so we can't compute the benefit. */
691 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
693 /* In order to move a register, we need to have one of three cases:
694 (1) it is used only in the same basic block as the set
695 (2) it is not a user variable and it is not used in the
696 exit test (this can cause the variable to be used
697 before it is set just like a user-variable).
698 (3) the set is guaranteed to be executed once the loop starts,
699 and the reg is not used until after that. */
700 else if (! ((! maybe_never
701 && ! loop_reg_used_before_p (set, p, loop_start,
702 scan_start, end))
703 || (! REG_USERVAR_P (SET_DEST (set))
704 && ! REG_LOOP_TEST_P (SET_DEST (set)))
705 || reg_in_basic_block_p (p, SET_DEST (set))))
707 else if ((tem = invariant_p (src))
708 && (dependencies == 0
709 || (tem2 = invariant_p (dependencies)) != 0)
710 && (n_times_set[REGNO (SET_DEST (set))] == 1
711 || (tem1
712 = consec_sets_invariant_p (SET_DEST (set),
713 n_times_set[REGNO (SET_DEST (set))],
714 p)))
715 /* If the insn can cause a trap (such as divide by zero),
716 can't move it unless it's guaranteed to be executed
717 once loop is entered. Even a function call might
718 prevent the trap insn from being reached
719 (since it might exit!) */
720 && ! ((maybe_never || call_passed)
721 && may_trap_p (src)))
723 register struct movable *m;
724 register int regno = REGNO (SET_DEST (set));
726 /* A potential lossage is where we have a case where two insns
727 can be combined as long as they are both in the loop, but
728 we move one of them outside the loop. For large loops,
729 this can lose. The most common case of this is the address
730 of a function being called.
732 Therefore, if this register is marked as being used exactly
733 once if we are in a loop with calls (a "large loop"), see if
734 we can replace the usage of this register with the source
735 of this SET. If we can, delete this insn.
737 Don't do this if P has a REG_RETVAL note or if we have
738 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
740 if (reg_single_usage && reg_single_usage[regno] != 0
741 && reg_single_usage[regno] != const0_rtx
742 && REGNO_FIRST_UID (regno) == INSN_UID (p)
743 && (REGNO_LAST_UID (regno)
744 == INSN_UID (reg_single_usage[regno]))
745 && n_times_set[REGNO (SET_DEST (set))] == 1
746 && ! side_effects_p (SET_SRC (set))
747 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
748 && (! SMALL_REGISTER_CLASSES
749 || (! (GET_CODE (SET_SRC (set)) == REG
750 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
751 /* This test is not redundant; SET_SRC (set) might be
752 a call-clobbered register and the life of REGNO
753 might span a call. */
754 && ! modified_between_p (SET_SRC (set), p,
755 reg_single_usage[regno])
756 && no_labels_between_p (p, reg_single_usage[regno])
757 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
758 reg_single_usage[regno]))
760 /* Replace any usage in a REG_EQUAL note. Must copy the
761 new source, so that we don't get rtx sharing between the
762 SET_SOURCE and REG_NOTES of insn p. */
763 REG_NOTES (reg_single_usage[regno])
764 = replace_rtx (REG_NOTES (reg_single_usage[regno]),
765 SET_DEST (set), copy_rtx (SET_SRC (set)));
767 PUT_CODE (p, NOTE);
768 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
769 NOTE_SOURCE_FILE (p) = 0;
770 n_times_set[regno] = 0;
771 continue;
774 m = (struct movable *) alloca (sizeof (struct movable));
775 m->next = 0;
776 m->insn = p;
777 m->set_src = src;
778 m->dependencies = dependencies;
779 m->set_dest = SET_DEST (set);
780 m->force = 0;
781 m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
782 m->done = 0;
783 m->forces = 0;
784 m->partial = 0;
785 m->move_insn = move_insn;
786 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
787 m->savemode = VOIDmode;
788 m->regno = regno;
789 /* Set M->cond if either invariant_p or consec_sets_invariant_p
790 returned 2 (only conditionally invariant). */
791 m->cond = ((tem | tem1 | tem2) > 1);
792 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
793 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
794 m->match = 0;
795 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
796 - uid_luid[REGNO_FIRST_UID (regno)]);
797 m->savings = n_times_used[regno];
798 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
799 m->savings += libcall_benefit (p);
800 n_times_set[regno] = move_insn ? -2 : -1;
801 /* Add M to the end of the chain MOVABLES. */
802 if (movables == 0)
803 movables = m;
804 else
805 last_movable->next = m;
806 last_movable = m;
808 if (m->consec > 0)
810 /* Skip this insn, not checking REG_LIBCALL notes. */
811 p = next_nonnote_insn (p);
812 /* Skip the consecutive insns, if there are any. */
813 p = skip_consec_insns (p, m->consec);
814 /* Back up to the last insn of the consecutive group. */
815 p = prev_nonnote_insn (p);
817 /* We must now reset m->move_insn, m->is_equiv, and possibly
818 m->set_src to correspond to the effects of all the
819 insns. */
820 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
821 if (temp)
822 m->set_src = XEXP (temp, 0), m->move_insn = 1;
823 else
825 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
826 if (temp && CONSTANT_P (XEXP (temp, 0)))
827 m->set_src = XEXP (temp, 0), m->move_insn = 1;
828 else
829 m->move_insn = 0;
832 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
835 /* If this register is always set within a STRICT_LOW_PART
836 or set to zero, then its high bytes are constant.
837 So clear them outside the loop and within the loop
838 just load the low bytes.
839 We must check that the machine has an instruction to do so.
840 Also, if the value loaded into the register
841 depends on the same register, this cannot be done. */
842 else if (SET_SRC (set) == const0_rtx
843 && GET_CODE (NEXT_INSN (p)) == INSN
844 && (set1 = single_set (NEXT_INSN (p)))
845 && GET_CODE (set1) == SET
846 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
847 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
848 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
849 == SET_DEST (set))
850 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
852 register int regno = REGNO (SET_DEST (set));
853 if (n_times_set[regno] == 2)
855 register struct movable *m;
856 m = (struct movable *) alloca (sizeof (struct movable));
857 m->next = 0;
858 m->insn = p;
859 m->set_dest = SET_DEST (set);
860 m->dependencies = 0;
861 m->force = 0;
862 m->consec = 0;
863 m->done = 0;
864 m->forces = 0;
865 m->move_insn = 0;
866 m->partial = 1;
867 /* If the insn may not be executed on some cycles,
868 we can't clear the whole reg; clear just high part.
869 Not even if the reg is used only within this loop.
870 Consider this:
871 while (1)
872 while (s != t) {
873 if (foo ()) x = *s;
874 use (x);
876 Clearing x before the inner loop could clobber a value
877 being saved from the last time around the outer loop.
878 However, if the reg is not used outside this loop
879 and all uses of the register are in the same
880 basic block as the store, there is no problem.
882 If this insn was made by loop, we don't know its
883 INSN_LUID and hence must make a conservative
884 assumption. */
885 m->global = (INSN_UID (p) >= max_uid_for_loop
886 || (uid_luid[REGNO_LAST_UID (regno)]
887 > INSN_LUID (end))
888 || (uid_luid[REGNO_FIRST_UID (regno)]
889 < INSN_LUID (p))
890 || (labels_in_range_p
891 (p, uid_luid[REGNO_FIRST_UID (regno)])));
892 if (maybe_never && m->global)
893 m->savemode = GET_MODE (SET_SRC (set1));
894 else
895 m->savemode = VOIDmode;
896 m->regno = regno;
897 m->cond = 0;
898 m->match = 0;
899 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
900 - uid_luid[REGNO_FIRST_UID (regno)]);
901 m->savings = 1;
902 n_times_set[regno] = -1;
903 /* Add M to the end of the chain MOVABLES. */
904 if (movables == 0)
905 movables = m;
906 else
907 last_movable->next = m;
908 last_movable = m;
912 /* Past a call insn, we get to insns which might not be executed
913 because the call might exit. This matters for insns that trap.
914 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
915 so they don't count. */
916 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
917 call_passed = 1;
918 /* Past a label or a jump, we get to insns for which we
919 can't count on whether or how many times they will be
920 executed during each iteration. Therefore, we can
921 only move out sets of trivial variables
922 (those not used after the loop). */
923 /* Similar code appears twice in strength_reduce. */
924 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
925 /* If we enter the loop in the middle, and scan around to the
926 beginning, don't set maybe_never for that. This must be an
927 unconditional jump, otherwise the code at the top of the
928 loop might never be executed. Unconditional jumps are
929 followed a by barrier then loop end. */
930 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
931 && NEXT_INSN (NEXT_INSN (p)) == end
932 && simplejump_p (p)))
933 maybe_never = 1;
934 else if (GET_CODE (p) == NOTE)
936 /* At the virtual top of a converted loop, insns are again known to
937 be executed: logically, the loop begins here even though the exit
938 code has been duplicated. */
939 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
940 maybe_never = call_passed = 0;
941 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
942 loop_depth++;
943 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
944 loop_depth--;
948 /* If one movable subsumes another, ignore that other. */
950 ignore_some_movables (movables);
952 /* For each movable insn, see if the reg that it loads
953 leads when it dies right into another conditionally movable insn.
954 If so, record that the second insn "forces" the first one,
955 since the second can be moved only if the first is. */
957 force_movables (movables);
959 /* See if there are multiple movable insns that load the same value.
960 If there are, make all but the first point at the first one
961 through the `match' field, and add the priorities of them
962 all together as the priority of the first. */
964 combine_movables (movables, nregs);
966 /* Now consider each movable insn to decide whether it is worth moving.
967 Store 0 in n_times_set for each reg that is moved. */
969 move_movables (movables, threshold,
970 insn_count, loop_start, end, nregs);
972 /* Now candidates that still are negative are those not moved.
973 Change n_times_set to indicate that those are not actually invariant. */
974 for (i = 0; i < nregs; i++)
975 if (n_times_set[i] < 0)
976 n_times_set[i] = n_times_used[i];
978 if (flag_strength_reduce)
979 strength_reduce (scan_start, end, loop_top,
980 insn_count, loop_start, end);
983 /* Add elements to *OUTPUT to record all the pseudo-regs
984 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
986 void
987 record_excess_regs (in_this, not_in_this, output)
988 rtx in_this, not_in_this;
989 rtx *output;
991 enum rtx_code code;
992 char *fmt;
993 int i;
995 code = GET_CODE (in_this);
997 switch (code)
999 case PC:
1000 case CC0:
1001 case CONST_INT:
1002 case CONST_DOUBLE:
1003 case CONST:
1004 case SYMBOL_REF:
1005 case LABEL_REF:
1006 return;
1008 case REG:
1009 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1010 && ! reg_mentioned_p (in_this, not_in_this))
1011 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1012 return;
1014 default:
1015 break;
1018 fmt = GET_RTX_FORMAT (code);
1019 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1021 int j;
1023 switch (fmt[i])
1025 case 'E':
1026 for (j = 0; j < XVECLEN (in_this, i); j++)
1027 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1028 break;
1030 case 'e':
1031 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1032 break;
1037 /* Check what regs are referred to in the libcall block ending with INSN,
1038 aside from those mentioned in the equivalent value.
1039 If there are none, return 0.
1040 If there are one or more, return an EXPR_LIST containing all of them. */
1042 static rtx
1043 libcall_other_reg (insn, equiv)
1044 rtx insn, equiv;
1046 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1047 rtx p = XEXP (note, 0);
1048 rtx output = 0;
1050 /* First, find all the regs used in the libcall block
1051 that are not mentioned as inputs to the result. */
1053 while (p != insn)
1055 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1056 || GET_CODE (p) == CALL_INSN)
1057 record_excess_regs (PATTERN (p), equiv, &output);
1058 p = NEXT_INSN (p);
1061 return output;
1064 /* Return 1 if all uses of REG
1065 are between INSN and the end of the basic block. */
1067 static int
1068 reg_in_basic_block_p (insn, reg)
1069 rtx insn, reg;
1071 int regno = REGNO (reg);
1072 rtx p;
1074 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1075 return 0;
1077 /* Search this basic block for the already recorded last use of the reg. */
1078 for (p = insn; p; p = NEXT_INSN (p))
1080 switch (GET_CODE (p))
1082 case NOTE:
1083 break;
1085 case INSN:
1086 case CALL_INSN:
1087 /* Ordinary insn: if this is the last use, we win. */
1088 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1089 return 1;
1090 break;
1092 case JUMP_INSN:
1093 /* Jump insn: if this is the last use, we win. */
1094 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1095 return 1;
1096 /* Otherwise, it's the end of the basic block, so we lose. */
1097 return 0;
1099 case CODE_LABEL:
1100 case BARRIER:
1101 /* It's the end of the basic block, so we lose. */
1102 return 0;
1104 default:
1105 break;
1109 /* The "last use" doesn't follow the "first use"?? */
1110 abort ();
1113 /* Compute the benefit of eliminating the insns in the block whose
1114 last insn is LAST. This may be a group of insns used to compute a
1115 value directly or can contain a library call. */
1117 static int
1118 libcall_benefit (last)
1119 rtx last;
1121 rtx insn;
1122 int benefit = 0;
1124 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1125 insn != last; insn = NEXT_INSN (insn))
1127 if (GET_CODE (insn) == CALL_INSN)
1128 benefit += 10; /* Assume at least this many insns in a library
1129 routine. */
1130 else if (GET_CODE (insn) == INSN
1131 && GET_CODE (PATTERN (insn)) != USE
1132 && GET_CODE (PATTERN (insn)) != CLOBBER)
1133 benefit++;
1136 return benefit;
1139 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1141 static rtx
1142 skip_consec_insns (insn, count)
1143 rtx insn;
1144 int count;
1146 for (; count > 0; count--)
1148 rtx temp;
1150 /* If first insn of libcall sequence, skip to end. */
1151 /* Do this at start of loop, since INSN is guaranteed to
1152 be an insn here. */
1153 if (GET_CODE (insn) != NOTE
1154 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1155 insn = XEXP (temp, 0);
1157 do insn = NEXT_INSN (insn);
1158 while (GET_CODE (insn) == NOTE);
1161 return insn;
1164 /* Ignore any movable whose insn falls within a libcall
1165 which is part of another movable.
1166 We make use of the fact that the movable for the libcall value
1167 was made later and so appears later on the chain. */
1169 static void
1170 ignore_some_movables (movables)
1171 struct movable *movables;
1173 register struct movable *m, *m1;
1175 for (m = movables; m; m = m->next)
1177 /* Is this a movable for the value of a libcall? */
1178 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1179 if (note)
1181 rtx insn;
1182 /* Check for earlier movables inside that range,
1183 and mark them invalid. We cannot use LUIDs here because
1184 insns created by loop.c for prior loops don't have LUIDs.
1185 Rather than reject all such insns from movables, we just
1186 explicitly check each insn in the libcall (since invariant
1187 libcalls aren't that common). */
1188 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1189 for (m1 = movables; m1 != m; m1 = m1->next)
1190 if (m1->insn == insn)
1191 m1->done = 1;
1196 /* For each movable insn, see if the reg that it loads
1197 leads when it dies right into another conditionally movable insn.
1198 If so, record that the second insn "forces" the first one,
1199 since the second can be moved only if the first is. */
1201 static void
1202 force_movables (movables)
1203 struct movable *movables;
1205 register struct movable *m, *m1;
1206 for (m1 = movables; m1; m1 = m1->next)
1207 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1208 if (!m1->partial && !m1->done)
1210 int regno = m1->regno;
1211 for (m = m1->next; m; m = m->next)
1212 /* ??? Could this be a bug? What if CSE caused the
1213 register of M1 to be used after this insn?
1214 Since CSE does not update regno_last_uid,
1215 this insn M->insn might not be where it dies.
1216 But very likely this doesn't matter; what matters is
1217 that M's reg is computed from M1's reg. */
1218 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1219 && !m->done)
1220 break;
1221 if (m != 0 && m->set_src == m1->set_dest
1222 /* If m->consec, m->set_src isn't valid. */
1223 && m->consec == 0)
1224 m = 0;
1226 /* Increase the priority of the moving the first insn
1227 since it permits the second to be moved as well. */
1228 if (m != 0)
1230 m->forces = m1;
1231 m1->lifetime += m->lifetime;
1232 m1->savings += m1->savings;
1237 /* Find invariant expressions that are equal and can be combined into
1238 one register. */
1240 static void
1241 combine_movables (movables, nregs)
1242 struct movable *movables;
1243 int nregs;
1245 register struct movable *m;
1246 char *matched_regs = (char *) alloca (nregs);
1247 enum machine_mode mode;
1249 /* Regs that are set more than once are not allowed to match
1250 or be matched. I'm no longer sure why not. */
1251 /* Perhaps testing m->consec_sets would be more appropriate here? */
1253 for (m = movables; m; m = m->next)
1254 if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1256 register struct movable *m1;
1257 int regno = m->regno;
1259 bzero (matched_regs, nregs);
1260 matched_regs[regno] = 1;
1262 /* We want later insns to match the first one. Don't make the first
1263 one match any later ones. So start this loop at m->next. */
1264 for (m1 = m->next; m1; m1 = m1->next)
1265 if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1266 /* A reg used outside the loop mustn't be eliminated. */
1267 && !m1->global
1268 /* A reg used for zero-extending mustn't be eliminated. */
1269 && !m1->partial
1270 && (matched_regs[m1->regno]
1273 /* Can combine regs with different modes loaded from the
1274 same constant only if the modes are the same or
1275 if both are integer modes with M wider or the same
1276 width as M1. The check for integer is redundant, but
1277 safe, since the only case of differing destination
1278 modes with equal sources is when both sources are
1279 VOIDmode, i.e., CONST_INT. */
1280 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1281 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1282 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1283 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1284 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1285 /* See if the source of M1 says it matches M. */
1286 && ((GET_CODE (m1->set_src) == REG
1287 && matched_regs[REGNO (m1->set_src)])
1288 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1289 movables))))
1290 && ((m->dependencies == m1->dependencies)
1291 || rtx_equal_p (m->dependencies, m1->dependencies)))
1293 m->lifetime += m1->lifetime;
1294 m->savings += m1->savings;
1295 m1->done = 1;
1296 m1->match = m;
1297 matched_regs[m1->regno] = 1;
1301 /* Now combine the regs used for zero-extension.
1302 This can be done for those not marked `global'
1303 provided their lives don't overlap. */
1305 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1306 mode = GET_MODE_WIDER_MODE (mode))
1308 register struct movable *m0 = 0;
1310 /* Combine all the registers for extension from mode MODE.
1311 Don't combine any that are used outside this loop. */
1312 for (m = movables; m; m = m->next)
1313 if (m->partial && ! m->global
1314 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1316 register struct movable *m1;
1317 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1318 int last = uid_luid[REGNO_LAST_UID (m->regno)];
1320 if (m0 == 0)
1322 /* First one: don't check for overlap, just record it. */
1323 m0 = m;
1324 continue;
1327 /* Make sure they extend to the same mode.
1328 (Almost always true.) */
1329 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1330 continue;
1332 /* We already have one: check for overlap with those
1333 already combined together. */
1334 for (m1 = movables; m1 != m; m1 = m1->next)
1335 if (m1 == m0 || (m1->partial && m1->match == m0))
1336 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1337 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1338 goto overlap;
1340 /* No overlap: we can combine this with the others. */
1341 m0->lifetime += m->lifetime;
1342 m0->savings += m->savings;
1343 m->done = 1;
1344 m->match = m0;
1346 overlap: ;
1351 /* Return 1 if regs X and Y will become the same if moved. */
1353 static int
1354 regs_match_p (x, y, movables)
1355 rtx x, y;
1356 struct movable *movables;
1358 int xn = REGNO (x);
1359 int yn = REGNO (y);
1360 struct movable *mx, *my;
1362 for (mx = movables; mx; mx = mx->next)
1363 if (mx->regno == xn)
1364 break;
1366 for (my = movables; my; my = my->next)
1367 if (my->regno == yn)
1368 break;
1370 return (mx && my
1371 && ((mx->match == my->match && mx->match != 0)
1372 || mx->match == my
1373 || mx == my->match));
1376 /* Return 1 if X and Y are identical-looking rtx's.
1377 This is the Lisp function EQUAL for rtx arguments.
1379 If two registers are matching movables or a movable register and an
1380 equivalent constant, consider them equal. */
1382 static int
1383 rtx_equal_for_loop_p (x, y, movables)
1384 rtx x, y;
1385 struct movable *movables;
1387 register int i;
1388 register int j;
1389 register struct movable *m;
1390 register enum rtx_code code;
1391 register char *fmt;
1393 if (x == y)
1394 return 1;
1395 if (x == 0 || y == 0)
1396 return 0;
1398 code = GET_CODE (x);
1400 /* If we have a register and a constant, they may sometimes be
1401 equal. */
1402 if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1403 && CONSTANT_P (y))
1404 for (m = movables; m; m = m->next)
1405 if (m->move_insn && m->regno == REGNO (x)
1406 && rtx_equal_p (m->set_src, y))
1407 return 1;
1409 else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1410 && CONSTANT_P (x))
1411 for (m = movables; m; m = m->next)
1412 if (m->move_insn && m->regno == REGNO (y)
1413 && rtx_equal_p (m->set_src, x))
1414 return 1;
1416 /* Otherwise, rtx's of different codes cannot be equal. */
1417 if (code != GET_CODE (y))
1418 return 0;
1420 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1421 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1423 if (GET_MODE (x) != GET_MODE (y))
1424 return 0;
1426 /* These three types of rtx's can be compared nonrecursively. */
1427 if (code == REG)
1428 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1430 if (code == LABEL_REF)
1431 return XEXP (x, 0) == XEXP (y, 0);
1432 if (code == SYMBOL_REF)
1433 return XSTR (x, 0) == XSTR (y, 0);
1435 /* Compare the elements. If any pair of corresponding elements
1436 fail to match, return 0 for the whole things. */
1438 fmt = GET_RTX_FORMAT (code);
1439 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1441 switch (fmt[i])
1443 case 'w':
1444 if (XWINT (x, i) != XWINT (y, i))
1445 return 0;
1446 break;
1448 case 'i':
1449 if (XINT (x, i) != XINT (y, i))
1450 return 0;
1451 break;
1453 case 'E':
1454 /* Two vectors must have the same length. */
1455 if (XVECLEN (x, i) != XVECLEN (y, i))
1456 return 0;
1458 /* And the corresponding elements must match. */
1459 for (j = 0; j < XVECLEN (x, i); j++)
1460 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1461 return 0;
1462 break;
1464 case 'e':
1465 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1466 return 0;
1467 break;
1469 case 's':
1470 if (strcmp (XSTR (x, i), XSTR (y, i)))
1471 return 0;
1472 break;
1474 case 'u':
1475 /* These are just backpointers, so they don't matter. */
1476 break;
1478 case '0':
1479 break;
1481 /* It is believed that rtx's at this level will never
1482 contain anything but integers and other rtx's,
1483 except for within LABEL_REFs and SYMBOL_REFs. */
1484 default:
1485 abort ();
1488 return 1;
1491 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1492 insns in INSNS which use thet reference. */
1494 static void
1495 add_label_notes (x, insns)
1496 rtx x;
1497 rtx insns;
1499 enum rtx_code code = GET_CODE (x);
1500 int i, j;
1501 char *fmt;
1502 rtx insn;
1504 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1506 rtx next = next_real_insn (XEXP (x, 0));
1508 /* Don't record labels that refer to dispatch tables.
1509 This is not necessary, since the tablejump references the same label.
1510 And if we did record them, flow.c would make worse code. */
1511 if (next == 0
1512 || ! (GET_CODE (next) == JUMP_INSN
1513 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1514 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1516 for (insn = insns; insn; insn = NEXT_INSN (insn))
1517 if (reg_mentioned_p (XEXP (x, 0), insn))
1518 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1519 REG_NOTES (insn));
1521 return;
1524 fmt = GET_RTX_FORMAT (code);
1525 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1527 if (fmt[i] == 'e')
1528 add_label_notes (XEXP (x, i), insns);
1529 else if (fmt[i] == 'E')
1530 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1531 add_label_notes (XVECEXP (x, i, j), insns);
1535 /* Scan MOVABLES, and move the insns that deserve to be moved.
1536 If two matching movables are combined, replace one reg with the
1537 other throughout. */
1539 static void
1540 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1541 struct movable *movables;
1542 int threshold;
1543 int insn_count;
1544 rtx loop_start;
1545 rtx end;
1546 int nregs;
1548 rtx new_start = 0;
1549 register struct movable *m;
1550 register rtx p;
1551 /* Map of pseudo-register replacements to handle combining
1552 when we move several insns that load the same value
1553 into different pseudo-registers. */
1554 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1555 char *already_moved = (char *) alloca (nregs);
1557 bzero (already_moved, nregs);
1558 bzero ((char *) reg_map, nregs * sizeof (rtx));
1560 num_movables = 0;
1562 for (m = movables; m; m = m->next)
1564 /* Describe this movable insn. */
1566 if (loop_dump_stream)
1568 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1569 INSN_UID (m->insn), m->regno, m->lifetime);
1570 if (m->consec > 0)
1571 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1572 if (m->cond)
1573 fprintf (loop_dump_stream, "cond ");
1574 if (m->force)
1575 fprintf (loop_dump_stream, "force ");
1576 if (m->global)
1577 fprintf (loop_dump_stream, "global ");
1578 if (m->done)
1579 fprintf (loop_dump_stream, "done ");
1580 if (m->move_insn)
1581 fprintf (loop_dump_stream, "move-insn ");
1582 if (m->match)
1583 fprintf (loop_dump_stream, "matches %d ",
1584 INSN_UID (m->match->insn));
1585 if (m->forces)
1586 fprintf (loop_dump_stream, "forces %d ",
1587 INSN_UID (m->forces->insn));
1590 /* Count movables. Value used in heuristics in strength_reduce. */
1591 num_movables++;
1593 /* Ignore the insn if it's already done (it matched something else).
1594 Otherwise, see if it is now safe to move. */
1596 if (!m->done
1597 && (! m->cond
1598 || (1 == invariant_p (m->set_src)
1599 && (m->dependencies == 0
1600 || 1 == invariant_p (m->dependencies))
1601 && (m->consec == 0
1602 || 1 == consec_sets_invariant_p (m->set_dest,
1603 m->consec + 1,
1604 m->insn))))
1605 && (! m->forces || m->forces->done))
1607 register int regno;
1608 register rtx p;
1609 int savings = m->savings;
1611 /* We have an insn that is safe to move.
1612 Compute its desirability. */
1614 p = m->insn;
1615 regno = m->regno;
1617 if (loop_dump_stream)
1618 fprintf (loop_dump_stream, "savings %d ", savings);
1620 if (moved_once[regno])
1622 insn_count *= 2;
1624 if (loop_dump_stream)
1625 fprintf (loop_dump_stream, "halved since already moved ");
1628 /* An insn MUST be moved if we already moved something else
1629 which is safe only if this one is moved too: that is,
1630 if already_moved[REGNO] is nonzero. */
1632 /* An insn is desirable to move if the new lifetime of the
1633 register is no more than THRESHOLD times the old lifetime.
1634 If it's not desirable, it means the loop is so big
1635 that moving won't speed things up much,
1636 and it is liable to make register usage worse. */
1638 /* It is also desirable to move if it can be moved at no
1639 extra cost because something else was already moved. */
1641 if (already_moved[regno]
1642 || (threshold * savings * m->lifetime) >= insn_count
1643 || (m->forces && m->forces->done
1644 && n_times_used[m->forces->regno] == 1))
1646 int count;
1647 register struct movable *m1;
1648 rtx first;
1650 /* Now move the insns that set the reg. */
1652 if (m->partial && m->match)
1654 rtx newpat, i1;
1655 rtx r1, r2;
1656 /* Find the end of this chain of matching regs.
1657 Thus, we load each reg in the chain from that one reg.
1658 And that reg is loaded with 0 directly,
1659 since it has ->match == 0. */
1660 for (m1 = m; m1->match; m1 = m1->match);
1661 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1662 SET_DEST (PATTERN (m1->insn)));
1663 i1 = emit_insn_before (newpat, loop_start);
1665 /* Mark the moved, invariant reg as being allowed to
1666 share a hard reg with the other matching invariant. */
1667 REG_NOTES (i1) = REG_NOTES (m->insn);
1668 r1 = SET_DEST (PATTERN (m->insn));
1669 r2 = SET_DEST (PATTERN (m1->insn));
1670 regs_may_share
1671 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1672 gen_rtx_EXPR_LIST (VOIDmode, r2,
1673 regs_may_share));
1674 delete_insn (m->insn);
1676 if (new_start == 0)
1677 new_start = i1;
1679 if (loop_dump_stream)
1680 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1682 /* If we are to re-generate the item being moved with a
1683 new move insn, first delete what we have and then emit
1684 the move insn before the loop. */
1685 else if (m->move_insn)
1687 rtx i1, temp;
1689 for (count = m->consec; count >= 0; count--)
1691 /* If this is the first insn of a library call sequence,
1692 skip to the end. */
1693 if (GET_CODE (p) != NOTE
1694 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1695 p = XEXP (temp, 0);
1697 /* If this is the last insn of a libcall sequence, then
1698 delete every insn in the sequence except the last.
1699 The last insn is handled in the normal manner. */
1700 if (GET_CODE (p) != NOTE
1701 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1703 temp = XEXP (temp, 0);
1704 while (temp != p)
1705 temp = delete_insn (temp);
1708 p = delete_insn (p);
1709 while (p && GET_CODE (p) == NOTE)
1710 p = NEXT_INSN (p);
1713 start_sequence ();
1714 emit_move_insn (m->set_dest, m->set_src);
1715 temp = get_insns ();
1716 end_sequence ();
1718 add_label_notes (m->set_src, temp);
1720 i1 = emit_insns_before (temp, loop_start);
1721 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1722 REG_NOTES (i1)
1723 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1724 m->set_src, REG_NOTES (i1));
1726 if (loop_dump_stream)
1727 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1729 /* The more regs we move, the less we like moving them. */
1730 threshold -= 3;
1732 else
1734 for (count = m->consec; count >= 0; count--)
1736 rtx i1, temp;
1738 /* If first insn of libcall sequence, skip to end. */
1739 /* Do this at start of loop, since p is guaranteed to
1740 be an insn here. */
1741 if (GET_CODE (p) != NOTE
1742 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1743 p = XEXP (temp, 0);
1745 /* If last insn of libcall sequence, move all
1746 insns except the last before the loop. The last
1747 insn is handled in the normal manner. */
1748 if (GET_CODE (p) != NOTE
1749 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1751 rtx fn_address = 0;
1752 rtx fn_reg = 0;
1753 rtx fn_address_insn = 0;
1755 first = 0;
1756 for (temp = XEXP (temp, 0); temp != p;
1757 temp = NEXT_INSN (temp))
1759 rtx body;
1760 rtx n;
1761 rtx next;
1763 if (GET_CODE (temp) == NOTE)
1764 continue;
1766 body = PATTERN (temp);
1768 /* Find the next insn after TEMP,
1769 not counting USE or NOTE insns. */
1770 for (next = NEXT_INSN (temp); next != p;
1771 next = NEXT_INSN (next))
1772 if (! (GET_CODE (next) == INSN
1773 && GET_CODE (PATTERN (next)) == USE)
1774 && GET_CODE (next) != NOTE)
1775 break;
1777 /* If that is the call, this may be the insn
1778 that loads the function address.
1780 Extract the function address from the insn
1781 that loads it into a register.
1782 If this insn was cse'd, we get incorrect code.
1784 So emit a new move insn that copies the
1785 function address into the register that the
1786 call insn will use. flow.c will delete any
1787 redundant stores that we have created. */
1788 if (GET_CODE (next) == CALL_INSN
1789 && GET_CODE (body) == SET
1790 && GET_CODE (SET_DEST (body)) == REG
1791 && (n = find_reg_note (temp, REG_EQUAL,
1792 NULL_RTX)))
1794 fn_reg = SET_SRC (body);
1795 if (GET_CODE (fn_reg) != REG)
1796 fn_reg = SET_DEST (body);
1797 fn_address = XEXP (n, 0);
1798 fn_address_insn = temp;
1800 /* We have the call insn.
1801 If it uses the register we suspect it might,
1802 load it with the correct address directly. */
1803 if (GET_CODE (temp) == CALL_INSN
1804 && fn_address != 0
1805 && reg_referenced_p (fn_reg, body))
1806 emit_insn_after (gen_move_insn (fn_reg,
1807 fn_address),
1808 fn_address_insn);
1810 if (GET_CODE (temp) == CALL_INSN)
1812 i1 = emit_call_insn_before (body, loop_start);
1813 /* Because the USAGE information potentially
1814 contains objects other than hard registers
1815 we need to copy it. */
1816 if (CALL_INSN_FUNCTION_USAGE (temp))
1817 CALL_INSN_FUNCTION_USAGE (i1)
1818 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1820 else
1821 i1 = emit_insn_before (body, loop_start);
1822 if (first == 0)
1823 first = i1;
1824 if (temp == fn_address_insn)
1825 fn_address_insn = i1;
1826 REG_NOTES (i1) = REG_NOTES (temp);
1827 delete_insn (temp);
1830 if (m->savemode != VOIDmode)
1832 /* P sets REG to zero; but we should clear only
1833 the bits that are not covered by the mode
1834 m->savemode. */
1835 rtx reg = m->set_dest;
1836 rtx sequence;
1837 rtx tem;
1839 start_sequence ();
1840 tem = expand_binop
1841 (GET_MODE (reg), and_optab, reg,
1842 GEN_INT ((((HOST_WIDE_INT) 1
1843 << GET_MODE_BITSIZE (m->savemode)))
1844 - 1),
1845 reg, 1, OPTAB_LIB_WIDEN);
1846 if (tem == 0)
1847 abort ();
1848 if (tem != reg)
1849 emit_move_insn (reg, tem);
1850 sequence = gen_sequence ();
1851 end_sequence ();
1852 i1 = emit_insn_before (sequence, loop_start);
1854 else if (GET_CODE (p) == CALL_INSN)
1856 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1857 /* Because the USAGE information potentially
1858 contains objects other than hard registers
1859 we need to copy it. */
1860 if (CALL_INSN_FUNCTION_USAGE (p))
1861 CALL_INSN_FUNCTION_USAGE (i1)
1862 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1864 else
1865 i1 = emit_insn_before (PATTERN (p), loop_start);
1867 REG_NOTES (i1) = REG_NOTES (p);
1869 /* If there is a REG_EQUAL note present whose value is
1870 not loop invariant, then delete it, since it may
1871 cause problems with later optimization passes.
1872 It is possible for cse to create such notes
1873 like this as a result of record_jump_cond. */
1875 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1876 && ! invariant_p (XEXP (temp, 0)))
1877 remove_note (i1, temp);
1879 if (new_start == 0)
1880 new_start = i1;
1882 if (loop_dump_stream)
1883 fprintf (loop_dump_stream, " moved to %d",
1884 INSN_UID (i1));
1886 #if 0
1887 /* This isn't needed because REG_NOTES is copied
1888 below and is wrong since P might be a PARALLEL. */
1889 if (REG_NOTES (i1) == 0
1890 && ! m->partial /* But not if it's a zero-extend
1891 clear. */
1892 && ! m->global /* and not if used outside the loop
1893 (since it might get set
1894 outside). */
1895 && CONSTANT_P (SET_SRC (PATTERN (p))))
1896 REG_NOTES (i1)
1897 = gen_rtx_EXPR_LIST (REG_EQUAL,
1898 SET_SRC (PATTERN (p)),
1899 REG_NOTES (i1));
1900 #endif
1902 /* If library call, now fix the REG_NOTES that contain
1903 insn pointers, namely REG_LIBCALL on FIRST
1904 and REG_RETVAL on I1. */
1905 if (temp = find_reg_note (i1, REG_RETVAL, NULL_RTX))
1907 XEXP (temp, 0) = first;
1908 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
1909 XEXP (temp, 0) = i1;
1912 delete_insn (p);
1913 do p = NEXT_INSN (p);
1914 while (p && GET_CODE (p) == NOTE);
1917 /* The more regs we move, the less we like moving them. */
1918 threshold -= 3;
1921 /* Any other movable that loads the same register
1922 MUST be moved. */
1923 already_moved[regno] = 1;
1925 /* This reg has been moved out of one loop. */
1926 moved_once[regno] = 1;
1928 /* The reg set here is now invariant. */
1929 if (! m->partial)
1930 n_times_set[regno] = 0;
1932 m->done = 1;
1934 /* Change the length-of-life info for the register
1935 to say it lives at least the full length of this loop.
1936 This will help guide optimizations in outer loops. */
1938 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
1939 /* This is the old insn before all the moved insns.
1940 We can't use the moved insn because it is out of range
1941 in uid_luid. Only the old insns have luids. */
1942 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
1943 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
1944 REGNO_LAST_UID (regno) = INSN_UID (end);
1946 /* Combine with this moved insn any other matching movables. */
1948 if (! m->partial)
1949 for (m1 = movables; m1; m1 = m1->next)
1950 if (m1->match == m)
1952 rtx temp;
1954 /* Schedule the reg loaded by M1
1955 for replacement so that shares the reg of M.
1956 If the modes differ (only possible in restricted
1957 circumstances, make a SUBREG. */
1958 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
1959 reg_map[m1->regno] = m->set_dest;
1960 else
1961 reg_map[m1->regno]
1962 = gen_lowpart_common (GET_MODE (m1->set_dest),
1963 m->set_dest);
1965 /* Get rid of the matching insn
1966 and prevent further processing of it. */
1967 m1->done = 1;
1969 /* if library call, delete all insn except last, which
1970 is deleted below */
1971 if (temp = find_reg_note (m1->insn, REG_RETVAL,
1972 NULL_RTX))
1974 for (temp = XEXP (temp, 0); temp != m1->insn;
1975 temp = NEXT_INSN (temp))
1976 delete_insn (temp);
1978 delete_insn (m1->insn);
1980 /* Any other movable that loads the same register
1981 MUST be moved. */
1982 already_moved[m1->regno] = 1;
1984 /* The reg merged here is now invariant,
1985 if the reg it matches is invariant. */
1986 if (! m->partial)
1987 n_times_set[m1->regno] = 0;
1990 else if (loop_dump_stream)
1991 fprintf (loop_dump_stream, "not desirable");
1993 else if (loop_dump_stream && !m->match)
1994 fprintf (loop_dump_stream, "not safe");
1996 if (loop_dump_stream)
1997 fprintf (loop_dump_stream, "\n");
2000 if (new_start == 0)
2001 new_start = loop_start;
2003 /* Go through all the instructions in the loop, making
2004 all the register substitutions scheduled in REG_MAP. */
2005 for (p = new_start; p != end; p = NEXT_INSN (p))
2006 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2007 || GET_CODE (p) == CALL_INSN)
2009 replace_regs (PATTERN (p), reg_map, nregs, 0);
2010 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2011 INSN_CODE (p) = -1;
2015 #if 0
2016 /* Scan X and replace the address of any MEM in it with ADDR.
2017 REG is the address that MEM should have before the replacement. */
2019 static void
2020 replace_call_address (x, reg, addr)
2021 rtx x, reg, addr;
2023 register enum rtx_code code;
2024 register int i;
2025 register char *fmt;
2027 if (x == 0)
2028 return;
2029 code = GET_CODE (x);
2030 switch (code)
2032 case PC:
2033 case CC0:
2034 case CONST_INT:
2035 case CONST_DOUBLE:
2036 case CONST:
2037 case SYMBOL_REF:
2038 case LABEL_REF:
2039 case REG:
2040 return;
2042 case SET:
2043 /* Short cut for very common case. */
2044 replace_call_address (XEXP (x, 1), reg, addr);
2045 return;
2047 case CALL:
2048 /* Short cut for very common case. */
2049 replace_call_address (XEXP (x, 0), reg, addr);
2050 return;
2052 case MEM:
2053 /* If this MEM uses a reg other than the one we expected,
2054 something is wrong. */
2055 if (XEXP (x, 0) != reg)
2056 abort ();
2057 XEXP (x, 0) = addr;
2058 return;
2060 default:
2061 break;
2064 fmt = GET_RTX_FORMAT (code);
2065 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2067 if (fmt[i] == 'e')
2068 replace_call_address (XEXP (x, i), reg, addr);
2069 if (fmt[i] == 'E')
2071 register int j;
2072 for (j = 0; j < XVECLEN (x, i); j++)
2073 replace_call_address (XVECEXP (x, i, j), reg, addr);
2077 #endif
2079 /* Return the number of memory refs to addresses that vary
2080 in the rtx X. */
2082 static int
2083 count_nonfixed_reads (x)
2084 rtx x;
2086 register enum rtx_code code;
2087 register int i;
2088 register char *fmt;
2089 int value;
2091 if (x == 0)
2092 return 0;
2094 code = GET_CODE (x);
2095 switch (code)
2097 case PC:
2098 case CC0:
2099 case CONST_INT:
2100 case CONST_DOUBLE:
2101 case CONST:
2102 case SYMBOL_REF:
2103 case LABEL_REF:
2104 case REG:
2105 return 0;
2107 case MEM:
2108 return ((invariant_p (XEXP (x, 0)) != 1)
2109 + count_nonfixed_reads (XEXP (x, 0)));
2111 default:
2112 break;
2115 value = 0;
2116 fmt = GET_RTX_FORMAT (code);
2117 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2119 if (fmt[i] == 'e')
2120 value += count_nonfixed_reads (XEXP (x, i));
2121 if (fmt[i] == 'E')
2123 register int j;
2124 for (j = 0; j < XVECLEN (x, i); j++)
2125 value += count_nonfixed_reads (XVECEXP (x, i, j));
2128 return value;
2132 #if 0
2133 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2134 Replace it with an instruction to load just the low bytes
2135 if the machine supports such an instruction,
2136 and insert above LOOP_START an instruction to clear the register. */
2138 static void
2139 constant_high_bytes (p, loop_start)
2140 rtx p, loop_start;
2142 register rtx new;
2143 register int insn_code_number;
2145 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2146 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2149 = gen_rtx_SET
2150 (VOIDmode,
2151 gen_rtx_STRICT_LOW_PART
2152 (VOIDmode,
2153 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2154 SET_DEST (PATTERN (p)), 0)),
2155 XEXP (SET_SRC (PATTERN (p)), 0));
2157 insn_code_number = recog (new, p);
2159 if (insn_code_number)
2161 register int i;
2163 /* Clear destination register before the loop. */
2164 emit_insn_before (gen_rtx_SET (VOIDmode,
2165 SET_DEST (PATTERN (p)), const0_rtx),
2166 loop_start);
2168 /* Inside the loop, just load the low part. */
2169 PATTERN (p) = new;
2172 #endif
2174 /* Scan a loop setting the variables `unknown_address_altered',
2175 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2176 and `loop_has_volatile'.
2177 Also, fill in the array `loop_store_mems'. */
2179 static void
2180 prescan_loop (start, end)
2181 rtx start, end;
2183 register int level = 1;
2184 register rtx insn;
2186 unknown_address_altered = 0;
2187 loop_has_call = 0;
2188 loop_has_volatile = 0;
2189 loop_store_mems_idx = 0;
2191 num_mem_sets = 0;
2192 loops_enclosed = 1;
2193 loop_continue = 0;
2195 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2196 insn = NEXT_INSN (insn))
2198 if (GET_CODE (insn) == NOTE)
2200 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2202 ++level;
2203 /* Count number of loops contained in this one. */
2204 loops_enclosed++;
2206 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2208 --level;
2209 if (level == 0)
2211 end = insn;
2212 break;
2215 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2217 if (level == 1)
2218 loop_continue = insn;
2221 else if (GET_CODE (insn) == CALL_INSN)
2223 unknown_address_altered = 1;
2224 loop_has_call = 1;
2226 else
2228 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2230 if (volatile_refs_p (PATTERN (insn)))
2231 loop_has_volatile = 1;
2233 note_stores (PATTERN (insn), note_addr_stored);
2239 /* Scan the function looking for loops. Record the start and end of each loop.
2240 Also mark as invalid loops any loops that contain a setjmp or are branched
2241 to from outside the loop. */
2243 static void
2244 find_and_verify_loops (f)
2245 rtx f;
2247 rtx insn, label;
2248 int current_loop = -1;
2249 int next_loop = -1;
2250 int loop;
2252 /* If there are jumps to undefined labels,
2253 treat them as jumps out of any/all loops.
2254 This also avoids writing past end of tables when there are no loops. */
2255 uid_loop_num[0] = -1;
2257 /* Find boundaries of loops, mark which loops are contained within
2258 loops, and invalidate loops that have setjmp. */
2260 for (insn = f; insn; insn = NEXT_INSN (insn))
2262 if (GET_CODE (insn) == NOTE)
2263 switch (NOTE_LINE_NUMBER (insn))
2265 case NOTE_INSN_LOOP_BEG:
2266 loop_number_loop_starts[++next_loop] = insn;
2267 loop_number_loop_ends[next_loop] = 0;
2268 loop_outer_loop[next_loop] = current_loop;
2269 loop_invalid[next_loop] = 0;
2270 loop_number_exit_labels[next_loop] = 0;
2271 loop_number_exit_count[next_loop] = 0;
2272 current_loop = next_loop;
2273 break;
2275 case NOTE_INSN_SETJMP:
2276 /* In this case, we must invalidate our current loop and any
2277 enclosing loop. */
2278 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2280 loop_invalid[loop] = 1;
2281 if (loop_dump_stream)
2282 fprintf (loop_dump_stream,
2283 "\nLoop at %d ignored due to setjmp.\n",
2284 INSN_UID (loop_number_loop_starts[loop]));
2286 break;
2288 case NOTE_INSN_LOOP_END:
2289 if (current_loop == -1)
2290 abort ();
2292 loop_number_loop_ends[current_loop] = insn;
2293 current_loop = loop_outer_loop[current_loop];
2294 break;
2296 default:
2297 break;
2300 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2301 enclosing loop, but this doesn't matter. */
2302 uid_loop_num[INSN_UID (insn)] = current_loop;
2305 /* Any loop containing a label used in an initializer must be invalidated,
2306 because it can be jumped into from anywhere. */
2308 for (label = forced_labels; label; label = XEXP (label, 1))
2310 int loop_num;
2312 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2313 loop_num != -1;
2314 loop_num = loop_outer_loop[loop_num])
2315 loop_invalid[loop_num] = 1;
2318 /* Any loop containing a label used for an exception handler must be
2319 invalidated, because it can be jumped into from anywhere. */
2321 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2323 int loop_num;
2325 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2326 loop_num != -1;
2327 loop_num = loop_outer_loop[loop_num])
2328 loop_invalid[loop_num] = 1;
2331 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2332 loop that it is not contained within, that loop is marked invalid.
2333 If any INSN or CALL_INSN uses a label's address, then the loop containing
2334 that label is marked invalid, because it could be jumped into from
2335 anywhere.
2337 Also look for blocks of code ending in an unconditional branch that
2338 exits the loop. If such a block is surrounded by a conditional
2339 branch around the block, move the block elsewhere (see below) and
2340 invert the jump to point to the code block. This may eliminate a
2341 label in our loop and will simplify processing by both us and a
2342 possible second cse pass. */
2344 for (insn = f; insn; insn = NEXT_INSN (insn))
2345 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2347 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2349 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2351 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2352 if (note)
2354 int loop_num;
2356 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2357 loop_num != -1;
2358 loop_num = loop_outer_loop[loop_num])
2359 loop_invalid[loop_num] = 1;
2363 if (GET_CODE (insn) != JUMP_INSN)
2364 continue;
2366 mark_loop_jump (PATTERN (insn), this_loop_num);
2368 /* See if this is an unconditional branch outside the loop. */
2369 if (this_loop_num != -1
2370 && (GET_CODE (PATTERN (insn)) == RETURN
2371 || (simplejump_p (insn)
2372 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2373 != this_loop_num)))
2374 && get_max_uid () < max_uid_for_loop)
2376 rtx p;
2377 rtx our_next = next_real_insn (insn);
2378 int dest_loop;
2379 int outer_loop = -1;
2381 /* Go backwards until we reach the start of the loop, a label,
2382 or a JUMP_INSN. */
2383 for (p = PREV_INSN (insn);
2384 GET_CODE (p) != CODE_LABEL
2385 && ! (GET_CODE (p) == NOTE
2386 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2387 && GET_CODE (p) != JUMP_INSN;
2388 p = PREV_INSN (p))
2391 /* Check for the case where we have a jump to an inner nested
2392 loop, and do not perform the optimization in that case. */
2394 if (JUMP_LABEL (insn))
2396 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2397 if (dest_loop != -1)
2399 for (outer_loop = dest_loop; outer_loop != -1;
2400 outer_loop = loop_outer_loop[outer_loop])
2401 if (outer_loop == this_loop_num)
2402 break;
2406 /* Make sure that the target of P is within the current loop. */
2408 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2409 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2410 outer_loop = this_loop_num;
2412 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2413 we have a block of code to try to move.
2415 We look backward and then forward from the target of INSN
2416 to find a BARRIER at the same loop depth as the target.
2417 If we find such a BARRIER, we make a new label for the start
2418 of the block, invert the jump in P and point it to that label,
2419 and move the block of code to the spot we found. */
2421 if (outer_loop == -1
2422 && GET_CODE (p) == JUMP_INSN
2423 && JUMP_LABEL (p) != 0
2424 /* Just ignore jumps to labels that were never emitted.
2425 These always indicate compilation errors. */
2426 && INSN_UID (JUMP_LABEL (p)) != 0
2427 && condjump_p (p)
2428 && ! simplejump_p (p)
2429 && next_real_insn (JUMP_LABEL (p)) == our_next)
2431 rtx target
2432 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2433 int target_loop_num = uid_loop_num[INSN_UID (target)];
2434 rtx loc;
2436 for (loc = target; loc; loc = PREV_INSN (loc))
2437 if (GET_CODE (loc) == BARRIER
2438 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2439 break;
2441 if (loc == 0)
2442 for (loc = target; loc; loc = NEXT_INSN (loc))
2443 if (GET_CODE (loc) == BARRIER
2444 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2445 break;
2447 if (loc)
2449 rtx cond_label = JUMP_LABEL (p);
2450 rtx new_label = get_label_after (p);
2452 /* Ensure our label doesn't go away. */
2453 LABEL_NUSES (cond_label)++;
2455 /* Verify that uid_loop_num is large enough and that
2456 we can invert P. */
2457 if (invert_jump (p, new_label))
2459 rtx q, r;
2461 /* Include the BARRIER after INSN and copy the
2462 block after LOC. */
2463 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2464 reorder_insns (new_label, NEXT_INSN (insn), loc);
2466 /* All those insns are now in TARGET_LOOP_NUM. */
2467 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2468 q = NEXT_INSN (q))
2469 uid_loop_num[INSN_UID (q)] = target_loop_num;
2471 /* The label jumped to by INSN is no longer a loop exit.
2472 Unless INSN does not have a label (e.g., it is a
2473 RETURN insn), search loop_number_exit_labels to find
2474 its label_ref, and remove it. Also turn off
2475 LABEL_OUTSIDE_LOOP_P bit. */
2476 if (JUMP_LABEL (insn))
2478 int loop_num;
2480 for (q = 0,
2481 r = loop_number_exit_labels[this_loop_num];
2482 r; q = r, r = LABEL_NEXTREF (r))
2483 if (XEXP (r, 0) == JUMP_LABEL (insn))
2485 LABEL_OUTSIDE_LOOP_P (r) = 0;
2486 if (q)
2487 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2488 else
2489 loop_number_exit_labels[this_loop_num]
2490 = LABEL_NEXTREF (r);
2491 break;
2494 for (loop_num = this_loop_num;
2495 loop_num != -1 && loop_num != target_loop_num;
2496 loop_num = loop_outer_loop[loop_num])
2497 loop_number_exit_count[loop_num]--;
2499 /* If we didn't find it, then something is wrong. */
2500 if (! r)
2501 abort ();
2504 /* P is now a jump outside the loop, so it must be put
2505 in loop_number_exit_labels, and marked as such.
2506 The easiest way to do this is to just call
2507 mark_loop_jump again for P. */
2508 mark_loop_jump (PATTERN (p), this_loop_num);
2510 /* If INSN now jumps to the insn after it,
2511 delete INSN. */
2512 if (JUMP_LABEL (insn) != 0
2513 && (next_real_insn (JUMP_LABEL (insn))
2514 == next_real_insn (insn)))
2515 delete_insn (insn);
2518 /* Continue the loop after where the conditional
2519 branch used to jump, since the only branch insn
2520 in the block (if it still remains) is an inter-loop
2521 branch and hence needs no processing. */
2522 insn = NEXT_INSN (cond_label);
2524 if (--LABEL_NUSES (cond_label) == 0)
2525 delete_insn (cond_label);
2527 /* This loop will be continued with NEXT_INSN (insn). */
2528 insn = PREV_INSN (insn);
2535 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2536 loops it is contained in, mark the target loop invalid.
2538 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2540 static void
2541 mark_loop_jump (x, loop_num)
2542 rtx x;
2543 int loop_num;
2545 int dest_loop;
2546 int outer_loop;
2547 int i;
2549 switch (GET_CODE (x))
2551 case PC:
2552 case USE:
2553 case CLOBBER:
2554 case REG:
2555 case MEM:
2556 case CONST_INT:
2557 case CONST_DOUBLE:
2558 case RETURN:
2559 return;
2561 case CONST:
2562 /* There could be a label reference in here. */
2563 mark_loop_jump (XEXP (x, 0), loop_num);
2564 return;
2566 case PLUS:
2567 case MINUS:
2568 case MULT:
2569 mark_loop_jump (XEXP (x, 0), loop_num);
2570 mark_loop_jump (XEXP (x, 1), loop_num);
2571 return;
2573 case SIGN_EXTEND:
2574 case ZERO_EXTEND:
2575 mark_loop_jump (XEXP (x, 0), loop_num);
2576 return;
2578 case LABEL_REF:
2579 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2581 /* Link together all labels that branch outside the loop. This
2582 is used by final_[bg]iv_value and the loop unrolling code. Also
2583 mark this LABEL_REF so we know that this branch should predict
2584 false. */
2586 /* A check to make sure the label is not in an inner nested loop,
2587 since this does not count as a loop exit. */
2588 if (dest_loop != -1)
2590 for (outer_loop = dest_loop; outer_loop != -1;
2591 outer_loop = loop_outer_loop[outer_loop])
2592 if (outer_loop == loop_num)
2593 break;
2595 else
2596 outer_loop = -1;
2598 if (loop_num != -1 && outer_loop == -1)
2600 LABEL_OUTSIDE_LOOP_P (x) = 1;
2601 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2602 loop_number_exit_labels[loop_num] = x;
2604 for (outer_loop = loop_num;
2605 outer_loop != -1 && outer_loop != dest_loop;
2606 outer_loop = loop_outer_loop[outer_loop])
2607 loop_number_exit_count[outer_loop]++;
2610 /* If this is inside a loop, but not in the current loop or one enclosed
2611 by it, it invalidates at least one loop. */
2613 if (dest_loop == -1)
2614 return;
2616 /* We must invalidate every nested loop containing the target of this
2617 label, except those that also contain the jump insn. */
2619 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2621 /* Stop when we reach a loop that also contains the jump insn. */
2622 for (outer_loop = loop_num; outer_loop != -1;
2623 outer_loop = loop_outer_loop[outer_loop])
2624 if (dest_loop == outer_loop)
2625 return;
2627 /* If we get here, we know we need to invalidate a loop. */
2628 if (loop_dump_stream && ! loop_invalid[dest_loop])
2629 fprintf (loop_dump_stream,
2630 "\nLoop at %d ignored due to multiple entry points.\n",
2631 INSN_UID (loop_number_loop_starts[dest_loop]));
2633 loop_invalid[dest_loop] = 1;
2635 return;
2637 case SET:
2638 /* If this is not setting pc, ignore. */
2639 if (SET_DEST (x) == pc_rtx)
2640 mark_loop_jump (SET_SRC (x), loop_num);
2641 return;
2643 case IF_THEN_ELSE:
2644 mark_loop_jump (XEXP (x, 1), loop_num);
2645 mark_loop_jump (XEXP (x, 2), loop_num);
2646 return;
2648 case PARALLEL:
2649 case ADDR_VEC:
2650 for (i = 0; i < XVECLEN (x, 0); i++)
2651 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2652 return;
2654 case ADDR_DIFF_VEC:
2655 for (i = 0; i < XVECLEN (x, 1); i++)
2656 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2657 return;
2659 default:
2660 /* Treat anything else (such as a symbol_ref)
2661 as a branch out of this loop, but not into any loop. */
2663 if (loop_num != -1)
2665 loop_number_exit_labels[loop_num] = x;
2667 for (outer_loop = loop_num; outer_loop != -1;
2668 outer_loop = loop_outer_loop[outer_loop])
2669 loop_number_exit_count[outer_loop]++;
2671 return;
2675 /* Return nonzero if there is a label in the range from
2676 insn INSN to and including the insn whose luid is END
2677 INSN must have an assigned luid (i.e., it must not have
2678 been previously created by loop.c). */
2680 static int
2681 labels_in_range_p (insn, end)
2682 rtx insn;
2683 int end;
2685 while (insn && INSN_LUID (insn) <= end)
2687 if (GET_CODE (insn) == CODE_LABEL)
2688 return 1;
2689 insn = NEXT_INSN (insn);
2692 return 0;
2695 /* Record that a memory reference X is being set. */
2697 static void
2698 note_addr_stored (x)
2699 rtx x;
2701 register int i;
2703 if (x == 0 || GET_CODE (x) != MEM)
2704 return;
2706 /* Count number of memory writes.
2707 This affects heuristics in strength_reduce. */
2708 num_mem_sets++;
2710 /* BLKmode MEM means all memory is clobbered. */
2711 if (GET_MODE (x) == BLKmode)
2712 unknown_address_altered = 1;
2714 if (unknown_address_altered)
2715 return;
2717 for (i = 0; i < loop_store_mems_idx; i++)
2718 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2719 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2721 /* We are storing at the same address as previously noted. Save the
2722 wider reference. */
2723 if (GET_MODE_SIZE (GET_MODE (x))
2724 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2725 loop_store_mems[i] = x;
2726 break;
2729 if (i == NUM_STORES)
2730 unknown_address_altered = 1;
2732 else if (i == loop_store_mems_idx)
2733 loop_store_mems[loop_store_mems_idx++] = x;
2736 /* Return nonzero if the rtx X is invariant over the current loop.
2738 The value is 2 if we refer to something only conditionally invariant.
2740 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2741 Otherwise, a memory ref is invariant if it does not conflict with
2742 anything stored in `loop_store_mems'. */
2745 invariant_p (x)
2746 register rtx x;
2748 register int i;
2749 register enum rtx_code code;
2750 register char *fmt;
2751 int conditional = 0;
2753 if (x == 0)
2754 return 1;
2755 code = GET_CODE (x);
2756 switch (code)
2758 case CONST_INT:
2759 case CONST_DOUBLE:
2760 case SYMBOL_REF:
2761 case CONST:
2762 return 1;
2764 case LABEL_REF:
2765 /* A LABEL_REF is normally invariant, however, if we are unrolling
2766 loops, and this label is inside the loop, then it isn't invariant.
2767 This is because each unrolled copy of the loop body will have
2768 a copy of this label. If this was invariant, then an insn loading
2769 the address of this label into a register might get moved outside
2770 the loop, and then each loop body would end up using the same label.
2772 We don't know the loop bounds here though, so just fail for all
2773 labels. */
2774 if (flag_unroll_loops)
2775 return 0;
2776 else
2777 return 1;
2779 case PC:
2780 case CC0:
2781 case UNSPEC_VOLATILE:
2782 return 0;
2784 case REG:
2785 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2786 since the reg might be set by initialization within the loop. */
2788 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2789 || x == arg_pointer_rtx)
2790 && ! current_function_has_nonlocal_goto)
2791 return 1;
2793 if (loop_has_call
2794 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2795 return 0;
2797 if (n_times_set[REGNO (x)] < 0)
2798 return 2;
2800 return n_times_set[REGNO (x)] == 0;
2802 case MEM:
2803 /* Volatile memory references must be rejected. Do this before
2804 checking for read-only items, so that volatile read-only items
2805 will be rejected also. */
2806 if (MEM_VOLATILE_P (x))
2807 return 0;
2809 /* Read-only items (such as constants in a constant pool) are
2810 invariant if their address is. */
2811 if (RTX_UNCHANGING_P (x))
2812 break;
2814 /* If we filled the table (or had a subroutine call), any location
2815 in memory could have been clobbered. */
2816 if (unknown_address_altered)
2817 return 0;
2819 /* See if there is any dependence between a store and this load. */
2820 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2821 if (true_dependence (loop_store_mems[i], x))
2822 return 0;
2824 /* It's not invalidated by a store in memory
2825 but we must still verify the address is invariant. */
2826 break;
2828 case ASM_OPERANDS:
2829 /* Don't mess with insns declared volatile. */
2830 if (MEM_VOLATILE_P (x))
2831 return 0;
2832 break;
2834 default:
2835 break;
2838 fmt = GET_RTX_FORMAT (code);
2839 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2841 if (fmt[i] == 'e')
2843 int tem = invariant_p (XEXP (x, i));
2844 if (tem == 0)
2845 return 0;
2846 if (tem == 2)
2847 conditional = 1;
2849 else if (fmt[i] == 'E')
2851 register int j;
2852 for (j = 0; j < XVECLEN (x, i); j++)
2854 int tem = invariant_p (XVECEXP (x, i, j));
2855 if (tem == 0)
2856 return 0;
2857 if (tem == 2)
2858 conditional = 1;
2864 return 1 + conditional;
2868 /* Return nonzero if all the insns in the loop that set REG
2869 are INSN and the immediately following insns,
2870 and if each of those insns sets REG in an invariant way
2871 (not counting uses of REG in them).
2873 The value is 2 if some of these insns are only conditionally invariant.
2875 We assume that INSN itself is the first set of REG
2876 and that its source is invariant. */
2878 static int
2879 consec_sets_invariant_p (reg, n_sets, insn)
2880 int n_sets;
2881 rtx reg, insn;
2883 register rtx p = insn;
2884 register int regno = REGNO (reg);
2885 rtx temp;
2886 /* Number of sets we have to insist on finding after INSN. */
2887 int count = n_sets - 1;
2888 int old = n_times_set[regno];
2889 int value = 0;
2890 int this;
2892 /* If N_SETS hit the limit, we can't rely on its value. */
2893 if (n_sets == 127)
2894 return 0;
2896 n_times_set[regno] = 0;
2898 while (count > 0)
2900 register enum rtx_code code;
2901 rtx set;
2903 p = NEXT_INSN (p);
2904 code = GET_CODE (p);
2906 /* If library call, skip to end of of it. */
2907 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2908 p = XEXP (temp, 0);
2910 this = 0;
2911 if (code == INSN
2912 && (set = single_set (p))
2913 && GET_CODE (SET_DEST (set)) == REG
2914 && REGNO (SET_DEST (set)) == regno)
2916 this = invariant_p (SET_SRC (set));
2917 if (this != 0)
2918 value |= this;
2919 else if (temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
2921 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
2922 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
2923 notes are OK. */
2924 this = (CONSTANT_P (XEXP (temp, 0))
2925 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
2926 && invariant_p (XEXP (temp, 0))));
2927 if (this != 0)
2928 value |= this;
2931 if (this != 0)
2932 count--;
2933 else if (code != NOTE)
2935 n_times_set[regno] = old;
2936 return 0;
2940 n_times_set[regno] = old;
2941 /* If invariant_p ever returned 2, we return 2. */
2942 return 1 + (value & 2);
2945 #if 0
2946 /* I don't think this condition is sufficient to allow INSN
2947 to be moved, so we no longer test it. */
2949 /* Return 1 if all insns in the basic block of INSN and following INSN
2950 that set REG are invariant according to TABLE. */
2952 static int
2953 all_sets_invariant_p (reg, insn, table)
2954 rtx reg, insn;
2955 short *table;
2957 register rtx p = insn;
2958 register int regno = REGNO (reg);
2960 while (1)
2962 register enum rtx_code code;
2963 p = NEXT_INSN (p);
2964 code = GET_CODE (p);
2965 if (code == CODE_LABEL || code == JUMP_INSN)
2966 return 1;
2967 if (code == INSN && GET_CODE (PATTERN (p)) == SET
2968 && GET_CODE (SET_DEST (PATTERN (p))) == REG
2969 && REGNO (SET_DEST (PATTERN (p))) == regno)
2971 if (!invariant_p (SET_SRC (PATTERN (p)), table))
2972 return 0;
2976 #endif /* 0 */
2978 /* Look at all uses (not sets) of registers in X. For each, if it is
2979 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
2980 a different insn, set USAGE[REGNO] to const0_rtx. */
2982 static void
2983 find_single_use_in_loop (insn, x, usage)
2984 rtx insn;
2985 rtx x;
2986 rtx *usage;
2988 enum rtx_code code = GET_CODE (x);
2989 char *fmt = GET_RTX_FORMAT (code);
2990 int i, j;
2992 if (code == REG)
2993 usage[REGNO (x)]
2994 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
2995 ? const0_rtx : insn;
2997 else if (code == SET)
2999 /* Don't count SET_DEST if it is a REG; otherwise count things
3000 in SET_DEST because if a register is partially modified, it won't
3001 show up as a potential movable so we don't care how USAGE is set
3002 for it. */
3003 if (GET_CODE (SET_DEST (x)) != REG)
3004 find_single_use_in_loop (insn, SET_DEST (x), usage);
3005 find_single_use_in_loop (insn, SET_SRC (x), usage);
3007 else
3008 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3010 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3011 find_single_use_in_loop (insn, XEXP (x, i), usage);
3012 else if (fmt[i] == 'E')
3013 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3014 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3018 /* Increment N_TIMES_SET at the index of each register
3019 that is modified by an insn between FROM and TO.
3020 If the value of an element of N_TIMES_SET becomes 127 or more,
3021 stop incrementing it, to avoid overflow.
3023 Store in SINGLE_USAGE[I] the single insn in which register I is
3024 used, if it is only used once. Otherwise, it is set to 0 (for no
3025 uses) or const0_rtx for more than one use. This parameter may be zero,
3026 in which case this processing is not done.
3028 Store in *COUNT_PTR the number of actual instruction
3029 in the loop. We use this to decide what is worth moving out. */
3031 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3032 In that case, it is the insn that last set reg n. */
3034 static void
3035 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3036 register rtx from, to;
3037 char *may_not_move;
3038 rtx *single_usage;
3039 int *count_ptr;
3040 int nregs;
3042 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3043 register rtx insn;
3044 register int count = 0;
3045 register rtx dest;
3047 bzero ((char *) last_set, nregs * sizeof (rtx));
3048 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3050 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3052 ++count;
3054 /* If requested, record registers that have exactly one use. */
3055 if (single_usage)
3057 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3059 /* Include uses in REG_EQUAL notes. */
3060 if (REG_NOTES (insn))
3061 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3064 if (GET_CODE (PATTERN (insn)) == CLOBBER
3065 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3066 /* Don't move a reg that has an explicit clobber.
3067 We might do so sometimes, but it's not worth the pain. */
3068 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3070 if (GET_CODE (PATTERN (insn)) == SET
3071 || GET_CODE (PATTERN (insn)) == CLOBBER)
3073 dest = SET_DEST (PATTERN (insn));
3074 while (GET_CODE (dest) == SUBREG
3075 || GET_CODE (dest) == ZERO_EXTRACT
3076 || GET_CODE (dest) == SIGN_EXTRACT
3077 || GET_CODE (dest) == STRICT_LOW_PART)
3078 dest = XEXP (dest, 0);
3079 if (GET_CODE (dest) == REG)
3081 register int regno = REGNO (dest);
3082 /* If this is the first setting of this reg
3083 in current basic block, and it was set before,
3084 it must be set in two basic blocks, so it cannot
3085 be moved out of the loop. */
3086 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3087 may_not_move[regno] = 1;
3088 /* If this is not first setting in current basic block,
3089 see if reg was used in between previous one and this.
3090 If so, neither one can be moved. */
3091 if (last_set[regno] != 0
3092 && reg_used_between_p (dest, last_set[regno], insn))
3093 may_not_move[regno] = 1;
3094 if (n_times_set[regno] < 127)
3095 ++n_times_set[regno];
3096 last_set[regno] = insn;
3099 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3101 register int i;
3102 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3104 register rtx x = XVECEXP (PATTERN (insn), 0, i);
3105 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3106 /* Don't move a reg that has an explicit clobber.
3107 It's not worth the pain to try to do it correctly. */
3108 may_not_move[REGNO (XEXP (x, 0))] = 1;
3110 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3112 dest = SET_DEST (x);
3113 while (GET_CODE (dest) == SUBREG
3114 || GET_CODE (dest) == ZERO_EXTRACT
3115 || GET_CODE (dest) == SIGN_EXTRACT
3116 || GET_CODE (dest) == STRICT_LOW_PART)
3117 dest = XEXP (dest, 0);
3118 if (GET_CODE (dest) == REG)
3120 register int regno = REGNO (dest);
3121 if (n_times_set[regno] > 0 && last_set[regno] == 0)
3122 may_not_move[regno] = 1;
3123 if (last_set[regno] != 0
3124 && reg_used_between_p (dest, last_set[regno], insn))
3125 may_not_move[regno] = 1;
3126 if (n_times_set[regno] < 127)
3127 ++n_times_set[regno];
3128 last_set[regno] = insn;
3135 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3136 bzero ((char *) last_set, nregs * sizeof (rtx));
3138 *count_ptr = count;
3141 /* Given a loop that is bounded by LOOP_START and LOOP_END
3142 and that is entered at SCAN_START,
3143 return 1 if the register set in SET contained in insn INSN is used by
3144 any insn that precedes INSN in cyclic order starting
3145 from the loop entry point.
3147 We don't want to use INSN_LUID here because if we restrict INSN to those
3148 that have a valid INSN_LUID, it means we cannot move an invariant out
3149 from an inner loop past two loops. */
3151 static int
3152 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3153 rtx set, insn, loop_start, scan_start, loop_end;
3155 rtx reg = SET_DEST (set);
3156 rtx p;
3158 /* Scan forward checking for register usage. If we hit INSN, we
3159 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3160 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3162 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3163 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3164 return 1;
3166 if (p == loop_end)
3167 p = loop_start;
3170 return 0;
3173 /* A "basic induction variable" or biv is a pseudo reg that is set
3174 (within this loop) only by incrementing or decrementing it. */
3175 /* A "general induction variable" or giv is a pseudo reg whose
3176 value is a linear function of a biv. */
3178 /* Bivs are recognized by `basic_induction_var';
3179 Givs by `general_induct_var'. */
3181 /* Indexed by register number, indicates whether or not register is an
3182 induction variable, and if so what type. */
3184 enum iv_mode *reg_iv_type;
3186 /* Indexed by register number, contains pointer to `struct induction'
3187 if register is an induction variable. This holds general info for
3188 all induction variables. */
3190 struct induction **reg_iv_info;
3192 /* Indexed by register number, contains pointer to `struct iv_class'
3193 if register is a basic induction variable. This holds info describing
3194 the class (a related group) of induction variables that the biv belongs
3195 to. */
3197 struct iv_class **reg_biv_class;
3199 /* The head of a list which links together (via the next field)
3200 every iv class for the current loop. */
3202 struct iv_class *loop_iv_list;
3204 /* Communication with routines called via `note_stores'. */
3206 static rtx note_insn;
3208 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3210 static rtx addr_placeholder;
3212 /* ??? Unfinished optimizations, and possible future optimizations,
3213 for the strength reduction code. */
3215 /* ??? There is one more optimization you might be interested in doing: to
3216 allocate pseudo registers for frequently-accessed memory locations.
3217 If the same memory location is referenced each time around, it might
3218 be possible to copy it into a register before and out after.
3219 This is especially useful when the memory location is a variable which
3220 is in a stack slot because somewhere its address is taken. If the
3221 loop doesn't contain a function call and the variable isn't volatile,
3222 it is safe to keep the value in a register for the duration of the
3223 loop. One tricky thing is that the copying of the value back from the
3224 register has to be done on all exits from the loop. You need to check that
3225 all the exits from the loop go to the same place. */
3227 /* ??? The interaction of biv elimination, and recognition of 'constant'
3228 bivs, may cause problems. */
3230 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3231 performance problems.
3233 Perhaps don't eliminate things that can be combined with an addressing
3234 mode. Find all givs that have the same biv, mult_val, and add_val;
3235 then for each giv, check to see if its only use dies in a following
3236 memory address. If so, generate a new memory address and check to see
3237 if it is valid. If it is valid, then store the modified memory address,
3238 otherwise, mark the giv as not done so that it will get its own iv. */
3240 /* ??? Could try to optimize branches when it is known that a biv is always
3241 positive. */
3243 /* ??? When replace a biv in a compare insn, we should replace with closest
3244 giv so that an optimized branch can still be recognized by the combiner,
3245 e.g. the VAX acb insn. */
3247 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3248 was rerun in loop_optimize whenever a register was added or moved.
3249 Also, some of the optimizations could be a little less conservative. */
3251 /* Perform strength reduction and induction variable elimination. */
3253 /* Pseudo registers created during this function will be beyond the last
3254 valid index in several tables including n_times_set and regno_last_uid.
3255 This does not cause a problem here, because the added registers cannot be
3256 givs outside of their loop, and hence will never be reconsidered.
3257 But scan_loop must check regnos to make sure they are in bounds. */
3259 static void
3260 strength_reduce (scan_start, end, loop_top, insn_count,
3261 loop_start, loop_end)
3262 rtx scan_start;
3263 rtx end;
3264 rtx loop_top;
3265 int insn_count;
3266 rtx loop_start;
3267 rtx loop_end;
3269 rtx p;
3270 rtx set;
3271 rtx inc_val;
3272 rtx mult_val;
3273 rtx dest_reg;
3274 /* This is 1 if current insn is not executed at least once for every loop
3275 iteration. */
3276 int not_every_iteration = 0;
3277 /* This is 1 if current insn may be executed more than once for every
3278 loop iteration. */
3279 int maybe_multiple = 0;
3280 /* Temporary list pointers for traversing loop_iv_list. */
3281 struct iv_class *bl, **backbl;
3282 /* Ratio of extra register life span we can justify
3283 for saving an instruction. More if loop doesn't call subroutines
3284 since in that case saving an insn makes more difference
3285 and more registers are available. */
3286 /* ??? could set this to last value of threshold in move_movables */
3287 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3288 /* Map of pseudo-register replacements. */
3289 rtx *reg_map;
3290 int call_seen;
3291 rtx test;
3292 rtx end_insert_before;
3293 int loop_depth = 0;
3295 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3296 * sizeof (enum iv_mode *));
3297 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3298 reg_iv_info = (struct induction **)
3299 alloca (max_reg_before_loop * sizeof (struct induction *));
3300 bzero ((char *) reg_iv_info, (max_reg_before_loop
3301 * sizeof (struct induction *)));
3302 reg_biv_class = (struct iv_class **)
3303 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3304 bzero ((char *) reg_biv_class, (max_reg_before_loop
3305 * sizeof (struct iv_class *)));
3307 loop_iv_list = 0;
3308 addr_placeholder = gen_reg_rtx (Pmode);
3310 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3311 must be put before this insn, so that they will appear in the right
3312 order (i.e. loop order).
3314 If loop_end is the end of the current function, then emit a
3315 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3316 dummy note insn. */
3317 if (NEXT_INSN (loop_end) != 0)
3318 end_insert_before = NEXT_INSN (loop_end);
3319 else
3320 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3322 /* Scan through loop to find all possible bivs. */
3324 p = scan_start;
3325 while (1)
3327 p = NEXT_INSN (p);
3328 /* At end of a straight-in loop, we are done.
3329 At end of a loop entered at the bottom, scan the top. */
3330 if (p == scan_start)
3331 break;
3332 if (p == end)
3334 if (loop_top != 0)
3335 p = loop_top;
3336 else
3337 break;
3338 if (p == scan_start)
3339 break;
3342 if (GET_CODE (p) == INSN
3343 && (set = single_set (p))
3344 && GET_CODE (SET_DEST (set)) == REG)
3346 dest_reg = SET_DEST (set);
3347 if (REGNO (dest_reg) < max_reg_before_loop
3348 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3349 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3351 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3352 dest_reg, p, &inc_val, &mult_val))
3354 /* It is a possible basic induction variable.
3355 Create and initialize an induction structure for it. */
3357 struct induction *v
3358 = (struct induction *) alloca (sizeof (struct induction));
3360 record_biv (v, p, dest_reg, inc_val, mult_val,
3361 not_every_iteration, maybe_multiple);
3362 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3364 else if (REGNO (dest_reg) < max_reg_before_loop)
3365 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3369 /* Past CODE_LABEL, we get to insns that may be executed multiple
3370 times. The only way we can be sure that they can't is if every
3371 every jump insn between here and the end of the loop either
3372 returns, exits the loop, is a forward jump, or is a jump
3373 to the loop start. */
3375 if (GET_CODE (p) == CODE_LABEL)
3377 rtx insn = p;
3379 maybe_multiple = 0;
3381 while (1)
3383 insn = NEXT_INSN (insn);
3384 if (insn == scan_start)
3385 break;
3386 if (insn == end)
3388 if (loop_top != 0)
3389 insn = loop_top;
3390 else
3391 break;
3392 if (insn == scan_start)
3393 break;
3396 if (GET_CODE (insn) == JUMP_INSN
3397 && GET_CODE (PATTERN (insn)) != RETURN
3398 && (! condjump_p (insn)
3399 || (JUMP_LABEL (insn) != 0
3400 && JUMP_LABEL (insn) != scan_start
3401 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3402 || INSN_UID (insn) >= max_uid_for_loop
3403 || (INSN_LUID (JUMP_LABEL (insn))
3404 < INSN_LUID (insn))))))
3406 maybe_multiple = 1;
3407 break;
3412 /* Past a jump, we get to insns for which we can't count
3413 on whether they will be executed during each iteration. */
3414 /* This code appears twice in strength_reduce. There is also similar
3415 code in scan_loop. */
3416 if (GET_CODE (p) == JUMP_INSN
3417 /* If we enter the loop in the middle, and scan around to the
3418 beginning, don't set not_every_iteration for that.
3419 This can be any kind of jump, since we want to know if insns
3420 will be executed if the loop is executed. */
3421 && ! (JUMP_LABEL (p) == loop_top
3422 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3423 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3425 rtx label = 0;
3427 /* If this is a jump outside the loop, then it also doesn't
3428 matter. Check to see if the target of this branch is on the
3429 loop_number_exits_labels list. */
3431 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3432 label;
3433 label = LABEL_NEXTREF (label))
3434 if (XEXP (label, 0) == JUMP_LABEL (p))
3435 break;
3437 if (! label)
3438 not_every_iteration = 1;
3441 else if (GET_CODE (p) == NOTE)
3443 /* At the virtual top of a converted loop, insns are again known to
3444 be executed each iteration: logically, the loop begins here
3445 even though the exit code has been duplicated. */
3446 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3447 not_every_iteration = 0;
3448 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3449 loop_depth++;
3450 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3451 loop_depth--;
3454 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3455 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3456 or not an insn is known to be executed each iteration of the
3457 loop, whether or not any iterations are known to occur.
3459 Therefore, if we have just passed a label and have no more labels
3460 between here and the test insn of the loop, we know these insns
3461 will be executed each iteration. */
3463 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3464 && no_labels_between_p (p, loop_end))
3465 not_every_iteration = 0;
3468 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3469 Make a sanity check against n_times_set. */
3470 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3472 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3473 /* Above happens if register modified by subreg, etc. */
3474 /* Make sure it is not recognized as a basic induction var: */
3475 || n_times_set[bl->regno] != bl->biv_count
3476 /* If never incremented, it is invariant that we decided not to
3477 move. So leave it alone. */
3478 || ! bl->incremented)
3480 if (loop_dump_stream)
3481 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3482 bl->regno,
3483 (reg_iv_type[bl->regno] != BASIC_INDUCT
3484 ? "not induction variable"
3485 : (! bl->incremented ? "never incremented"
3486 : "count error")));
3488 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3489 *backbl = bl->next;
3491 else
3493 backbl = &bl->next;
3495 if (loop_dump_stream)
3496 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3500 /* Exit if there are no bivs. */
3501 if (! loop_iv_list)
3503 /* Can still unroll the loop anyways, but indicate that there is no
3504 strength reduction info available. */
3505 if (flag_unroll_loops)
3506 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3508 return;
3511 /* Find initial value for each biv by searching backwards from loop_start,
3512 halting at first label. Also record any test condition. */
3514 call_seen = 0;
3515 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3517 note_insn = p;
3519 if (GET_CODE (p) == CALL_INSN)
3520 call_seen = 1;
3522 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3523 || GET_CODE (p) == CALL_INSN)
3524 note_stores (PATTERN (p), record_initial);
3526 /* Record any test of a biv that branches around the loop if no store
3527 between it and the start of loop. We only care about tests with
3528 constants and registers and only certain of those. */
3529 if (GET_CODE (p) == JUMP_INSN
3530 && JUMP_LABEL (p) != 0
3531 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3532 && (test = get_condition_for_loop (p)) != 0
3533 && GET_CODE (XEXP (test, 0)) == REG
3534 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3535 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3536 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3537 && bl->init_insn == 0)
3539 /* If an NE test, we have an initial value! */
3540 if (GET_CODE (test) == NE)
3542 bl->init_insn = p;
3543 bl->init_set = gen_rtx_SET (VOIDmode,
3544 XEXP (test, 0), XEXP (test, 1));
3546 else
3547 bl->initial_test = test;
3551 /* Look at the each biv and see if we can say anything better about its
3552 initial value from any initializing insns set up above. (This is done
3553 in two passes to avoid missing SETs in a PARALLEL.) */
3554 for (bl = loop_iv_list; bl; bl = bl->next)
3556 rtx src;
3557 rtx note;
3559 if (! bl->init_insn)
3560 continue;
3562 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3563 is a constant, use the value of that. */
3564 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3565 && CONSTANT_P (XEXP (note, 0)))
3566 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3567 && CONSTANT_P (XEXP (note, 0))))
3568 src = XEXP (note, 0);
3569 else
3570 src = SET_SRC (bl->init_set);
3572 if (loop_dump_stream)
3573 fprintf (loop_dump_stream,
3574 "Biv %d initialized at insn %d: initial value ",
3575 bl->regno, INSN_UID (bl->init_insn));
3577 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3578 || GET_MODE (src) == VOIDmode)
3579 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3581 bl->initial_value = src;
3583 if (loop_dump_stream)
3585 if (GET_CODE (src) == CONST_INT)
3586 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
3587 else
3589 print_rtl (loop_dump_stream, src);
3590 fprintf (loop_dump_stream, "\n");
3594 else
3596 /* Biv initial value is not simple move,
3597 so let it keep initial value of "itself". */
3599 if (loop_dump_stream)
3600 fprintf (loop_dump_stream, "is complex\n");
3604 /* Search the loop for general induction variables. */
3606 /* A register is a giv if: it is only set once, it is a function of a
3607 biv and a constant (or invariant), and it is not a biv. */
3609 not_every_iteration = 0;
3610 loop_depth = 0;
3611 maybe_multiple = 0;
3612 p = scan_start;
3613 while (1)
3615 p = NEXT_INSN (p);
3616 /* At end of a straight-in loop, we are done.
3617 At end of a loop entered at the bottom, scan the top. */
3618 if (p == scan_start)
3619 break;
3620 if (p == end)
3622 if (loop_top != 0)
3623 p = loop_top;
3624 else
3625 break;
3626 if (p == scan_start)
3627 break;
3630 /* Look for a general induction variable in a register. */
3631 if (GET_CODE (p) == INSN
3632 && (set = single_set (p))
3633 && GET_CODE (SET_DEST (set)) == REG
3634 && ! may_not_optimize[REGNO (SET_DEST (set))])
3636 rtx src_reg;
3637 rtx add_val;
3638 rtx mult_val;
3639 int benefit;
3640 rtx regnote = 0;
3642 dest_reg = SET_DEST (set);
3643 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3644 continue;
3646 if (/* SET_SRC is a giv. */
3647 ((benefit = general_induction_var (SET_SRC (set),
3648 &src_reg, &add_val,
3649 &mult_val))
3650 /* Equivalent expression is a giv. */
3651 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3652 && (benefit = general_induction_var (XEXP (regnote, 0),
3653 &src_reg,
3654 &add_val, &mult_val))))
3655 /* Don't try to handle any regs made by loop optimization.
3656 We have nothing on them in regno_first_uid, etc. */
3657 && REGNO (dest_reg) < max_reg_before_loop
3658 /* Don't recognize a BASIC_INDUCT_VAR here. */
3659 && dest_reg != src_reg
3660 /* This must be the only place where the register is set. */
3661 && (n_times_set[REGNO (dest_reg)] == 1
3662 /* or all sets must be consecutive and make a giv. */
3663 || (benefit = consec_sets_giv (benefit, p,
3664 src_reg, dest_reg,
3665 &add_val, &mult_val))))
3667 int count;
3668 struct induction *v
3669 = (struct induction *) alloca (sizeof (struct induction));
3670 rtx temp;
3672 /* If this is a library call, increase benefit. */
3673 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3674 benefit += libcall_benefit (p);
3676 /* Skip the consecutive insns, if there are any. */
3677 for (count = n_times_set[REGNO (dest_reg)] - 1;
3678 count > 0; count--)
3680 /* If first insn of libcall sequence, skip to end.
3681 Do this at start of loop, since INSN is guaranteed to
3682 be an insn here. */
3683 if (GET_CODE (p) != NOTE
3684 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3685 p = XEXP (temp, 0);
3687 do p = NEXT_INSN (p);
3688 while (GET_CODE (p) == NOTE);
3691 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3692 DEST_REG, not_every_iteration, maybe_multiple,
3693 NULL_PTR, loop_start, loop_end);
3698 #ifndef DONT_REDUCE_ADDR
3699 /* Look for givs which are memory addresses. */
3700 /* This resulted in worse code on a VAX 8600. I wonder if it
3701 still does. */
3702 if (GET_CODE (p) == INSN)
3703 find_mem_givs (PATTERN (p), p, not_every_iteration, maybe_multiple,
3704 loop_start, loop_end);
3705 #endif
3707 /* Update the status of whether giv can derive other givs. This can
3708 change when we pass a label or an insn that updates a biv. */
3709 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3710 || GET_CODE (p) == CODE_LABEL)
3711 update_giv_derive (p);
3713 /* Past CODE_LABEL, we get to insns that may be executed multiple
3714 times. The only way we can be sure that they can't is if every
3715 every jump insn between here and the end of the loop either
3716 returns, exits the loop, is a forward jump, or is a jump
3717 to the loop start. */
3719 if (GET_CODE (p) == CODE_LABEL)
3721 rtx insn = p;
3723 maybe_multiple = 0;
3725 while (1)
3727 insn = NEXT_INSN (insn);
3728 if (insn == scan_start)
3729 break;
3730 if (insn == end)
3732 if (loop_top != 0)
3733 insn = loop_top;
3734 else
3735 break;
3736 if (insn == scan_start)
3737 break;
3740 if (GET_CODE (insn) == JUMP_INSN
3741 && GET_CODE (PATTERN (insn)) != RETURN
3742 && (! condjump_p (insn)
3743 || (JUMP_LABEL (insn) != 0
3744 && JUMP_LABEL (insn) != scan_start
3745 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3746 || INSN_UID (insn) >= max_uid_for_loop
3747 || (INSN_LUID (JUMP_LABEL (insn))
3748 < INSN_LUID (insn))))))
3750 maybe_multiple = 1;
3751 break;
3756 /* Past a jump, we get to insns for which we can't count
3757 on whether they will be executed during each iteration. */
3758 /* This code appears twice in strength_reduce. There is also similar
3759 code in scan_loop. */
3760 if (GET_CODE (p) == JUMP_INSN
3761 /* If we enter the loop in the middle, and scan around to the
3762 beginning, don't set not_every_iteration for that.
3763 This can be any kind of jump, since we want to know if insns
3764 will be executed if the loop is executed. */
3765 && ! (JUMP_LABEL (p) == loop_top
3766 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3767 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3769 rtx label = 0;
3771 /* If this is a jump outside the loop, then it also doesn't
3772 matter. Check to see if the target of this branch is on the
3773 loop_number_exits_labels list. */
3775 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3776 label;
3777 label = LABEL_NEXTREF (label))
3778 if (XEXP (label, 0) == JUMP_LABEL (p))
3779 break;
3781 if (! label)
3782 not_every_iteration = 1;
3785 else if (GET_CODE (p) == NOTE)
3787 /* At the virtual top of a converted loop, insns are again known to
3788 be executed each iteration: logically, the loop begins here
3789 even though the exit code has been duplicated. */
3790 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3791 not_every_iteration = 0;
3792 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3793 loop_depth++;
3794 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3795 loop_depth--;
3798 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3799 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3800 or not an insn is known to be executed each iteration of the
3801 loop, whether or not any iterations are known to occur.
3803 Therefore, if we have just passed a label and have no more labels
3804 between here and the test insn of the loop, we know these insns
3805 will be executed each iteration. */
3807 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3808 && no_labels_between_p (p, loop_end))
3809 not_every_iteration = 0;
3812 /* Try to calculate and save the number of loop iterations. This is
3813 set to zero if the actual number can not be calculated. This must
3814 be called after all giv's have been identified, since otherwise it may
3815 fail if the iteration variable is a giv. */
3817 loop_n_iterations = loop_iterations (loop_start, loop_end);
3819 /* Now for each giv for which we still don't know whether or not it is
3820 replaceable, check to see if it is replaceable because its final value
3821 can be calculated. This must be done after loop_iterations is called,
3822 so that final_giv_value will work correctly. */
3824 for (bl = loop_iv_list; bl; bl = bl->next)
3826 struct induction *v;
3828 for (v = bl->giv; v; v = v->next_iv)
3829 if (! v->replaceable && ! v->not_replaceable)
3830 check_final_value (v, loop_start, loop_end);
3833 /* Try to prove that the loop counter variable (if any) is always
3834 nonnegative; if so, record that fact with a REG_NONNEG note
3835 so that "decrement and branch until zero" insn can be used. */
3836 check_dbra_loop (loop_end, insn_count, loop_start);
3838 /* Create reg_map to hold substitutions for replaceable giv regs. */
3839 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3840 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3842 /* Examine each iv class for feasibility of strength reduction/induction
3843 variable elimination. */
3845 for (bl = loop_iv_list; bl; bl = bl->next)
3847 struct induction *v;
3848 int benefit;
3849 int all_reduced;
3850 rtx final_value = 0;
3852 /* Test whether it will be possible to eliminate this biv
3853 provided all givs are reduced. This is possible if either
3854 the reg is not used outside the loop, or we can compute
3855 what its final value will be.
3857 For architectures with a decrement_and_branch_until_zero insn,
3858 don't do this if we put a REG_NONNEG note on the endtest for
3859 this biv. */
3861 /* Compare against bl->init_insn rather than loop_start.
3862 We aren't concerned with any uses of the biv between
3863 init_insn and loop_start since these won't be affected
3864 by the value of the biv elsewhere in the function, so
3865 long as init_insn doesn't use the biv itself.
3866 March 14, 1989 -- self@bayes.arc.nasa.gov */
3868 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3869 && bl->init_insn
3870 && INSN_UID (bl->init_insn) < max_uid_for_loop
3871 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3872 #ifdef HAVE_decrement_and_branch_until_zero
3873 && ! bl->nonneg
3874 #endif
3875 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3876 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3877 #ifdef HAVE_decrement_and_branch_until_zero
3878 && ! bl->nonneg
3879 #endif
3881 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3882 threshold, insn_count);
3883 else
3885 if (loop_dump_stream)
3887 fprintf (loop_dump_stream,
3888 "Cannot eliminate biv %d.\n",
3889 bl->regno);
3890 fprintf (loop_dump_stream,
3891 "First use: insn %d, last use: insn %d.\n",
3892 REGNO_FIRST_UID (bl->regno),
3893 REGNO_LAST_UID (bl->regno));
3897 /* Combine all giv's for this iv_class. */
3898 combine_givs (bl);
3900 /* This will be true at the end, if all givs which depend on this
3901 biv have been strength reduced.
3902 We can't (currently) eliminate the biv unless this is so. */
3903 all_reduced = 1;
3905 /* Check each giv in this class to see if we will benefit by reducing
3906 it. Skip giv's combined with others. */
3907 for (v = bl->giv; v; v = v->next_iv)
3909 struct induction *tv;
3911 if (v->ignore || v->same)
3912 continue;
3914 benefit = v->benefit;
3916 /* Reduce benefit if not replaceable, since we will insert
3917 a move-insn to replace the insn that calculates this giv.
3918 Don't do this unless the giv is a user variable, since it
3919 will often be marked non-replaceable because of the duplication
3920 of the exit code outside the loop. In such a case, the copies
3921 we insert are dead and will be deleted. So they don't have
3922 a cost. Similar situations exist. */
3923 /* ??? The new final_[bg]iv_value code does a much better job
3924 of finding replaceable giv's, and hence this code may no longer
3925 be necessary. */
3926 if (! v->replaceable && ! bl->eliminable
3927 && REG_USERVAR_P (v->dest_reg))
3928 benefit -= copy_cost;
3930 /* Decrease the benefit to count the add-insns that we will
3931 insert to increment the reduced reg for the giv. */
3932 benefit -= add_cost * bl->biv_count;
3934 /* Decide whether to strength-reduce this giv or to leave the code
3935 unchanged (recompute it from the biv each time it is used).
3936 This decision can be made independently for each giv. */
3938 #ifdef AUTO_INC_DEC
3939 /* Attempt to guess whether autoincrement will handle some of the
3940 new add insns; if so, increase BENEFIT (undo the subtraction of
3941 add_cost that was done above). */
3942 if (v->giv_type == DEST_ADDR
3943 && GET_CODE (v->mult_val) == CONST_INT)
3945 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
3946 if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3947 benefit += add_cost * bl->biv_count;
3948 #endif
3949 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
3950 if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3951 benefit += add_cost * bl->biv_count;
3952 #endif
3954 #endif
3956 /* If an insn is not to be strength reduced, then set its ignore
3957 flag, and clear all_reduced. */
3959 /* A giv that depends on a reversed biv must be reduced if it is
3960 used after the loop exit, otherwise, it would have the wrong
3961 value after the loop exit. To make it simple, just reduce all
3962 of such giv's whether or not we know they are used after the loop
3963 exit. */
3965 if (v->lifetime * threshold * benefit < insn_count
3966 && ! bl->reversed)
3968 if (loop_dump_stream)
3969 fprintf (loop_dump_stream,
3970 "giv of insn %d not worth while, %d vs %d.\n",
3971 INSN_UID (v->insn),
3972 v->lifetime * threshold * benefit, insn_count);
3973 v->ignore = 1;
3974 all_reduced = 0;
3976 else
3978 /* Check that we can increment the reduced giv without a
3979 multiply insn. If not, reject it. */
3981 for (tv = bl->biv; tv; tv = tv->next_iv)
3982 if (tv->mult_val == const1_rtx
3983 && ! product_cheap_p (tv->add_val, v->mult_val))
3985 if (loop_dump_stream)
3986 fprintf (loop_dump_stream,
3987 "giv of insn %d: would need a multiply.\n",
3988 INSN_UID (v->insn));
3989 v->ignore = 1;
3990 all_reduced = 0;
3991 break;
3996 /* Reduce each giv that we decided to reduce. */
3998 for (v = bl->giv; v; v = v->next_iv)
4000 struct induction *tv;
4001 if (! v->ignore && v->same == 0)
4003 int auto_inc_opt = 0;
4005 v->new_reg = gen_reg_rtx (v->mode);
4007 #ifdef AUTO_INC_DEC
4008 /* If the target has auto-increment addressing modes, and
4009 this is an address giv, then try to put the increment
4010 immediately after its use, so that flow can create an
4011 auto-increment addressing mode. */
4012 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4013 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4014 /* We don't handle reversed biv's because bl->biv->insn
4015 does not have a valid INSN_LUID. */
4016 && ! bl->reversed
4017 && v->always_executed && ! v->maybe_multiple
4018 && INSN_UID (v->insn) < max_uid_for_loop)
4020 /* If other giv's have been combined with this one, then
4021 this will work only if all uses of the other giv's occur
4022 before this giv's insn. This is difficult to check.
4024 We simplify this by looking for the common case where
4025 there is one DEST_REG giv, and this giv's insn is the
4026 last use of the dest_reg of that DEST_REG giv. If the
4027 the increment occurs after the address giv, then we can
4028 perform the optimization. (Otherwise, the increment
4029 would have to go before other_giv, and we would not be
4030 able to combine it with the address giv to get an
4031 auto-inc address.) */
4032 if (v->combined_with)
4034 struct induction *other_giv = 0;
4036 for (tv = bl->giv; tv; tv = tv->next_iv)
4037 if (tv->same == v)
4039 if (other_giv)
4040 break;
4041 else
4042 other_giv = tv;
4044 if (! tv && other_giv
4045 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4046 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4047 == INSN_UID (v->insn))
4048 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4049 auto_inc_opt = 1;
4051 /* Check for case where increment is before the the address
4052 giv. Do this test in "loop order". */
4053 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4054 && (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4055 || (INSN_LUID (bl->biv->insn)
4056 > INSN_LUID (scan_start))))
4057 || (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4058 && (INSN_LUID (scan_start)
4059 < INSN_LUID (bl->biv->insn))))
4060 auto_inc_opt = -1;
4061 else
4062 auto_inc_opt = 1;
4064 #ifdef HAVE_cc0
4066 rtx prev;
4068 /* We can't put an insn immediately after one setting
4069 cc0, or immediately before one using cc0. */
4070 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4071 || (auto_inc_opt == -1
4072 && (prev = prev_nonnote_insn (v->insn)) != 0
4073 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4074 && sets_cc0_p (PATTERN (prev))))
4075 auto_inc_opt = 0;
4077 #endif
4079 if (auto_inc_opt)
4080 v->auto_inc_opt = 1;
4082 #endif
4084 /* For each place where the biv is incremented, add an insn
4085 to increment the new, reduced reg for the giv. */
4086 for (tv = bl->biv; tv; tv = tv->next_iv)
4088 rtx insert_before;
4090 if (! auto_inc_opt)
4091 insert_before = tv->insn;
4092 else if (auto_inc_opt == 1)
4093 insert_before = NEXT_INSN (v->insn);
4094 else
4095 insert_before = v->insn;
4097 if (tv->mult_val == const1_rtx)
4098 emit_iv_add_mult (tv->add_val, v->mult_val,
4099 v->new_reg, v->new_reg, insert_before);
4100 else /* tv->mult_val == const0_rtx */
4101 /* A multiply is acceptable here
4102 since this is presumed to be seldom executed. */
4103 emit_iv_add_mult (tv->add_val, v->mult_val,
4104 v->add_val, v->new_reg, insert_before);
4107 /* Add code at loop start to initialize giv's reduced reg. */
4109 emit_iv_add_mult (bl->initial_value, v->mult_val,
4110 v->add_val, v->new_reg, loop_start);
4114 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4115 as not reduced.
4117 For each giv register that can be reduced now: if replaceable,
4118 substitute reduced reg wherever the old giv occurs;
4119 else add new move insn "giv_reg = reduced_reg".
4121 Also check for givs whose first use is their definition and whose
4122 last use is the definition of another giv. If so, it is likely
4123 dead and should not be used to eliminate a biv. */
4124 for (v = bl->giv; v; v = v->next_iv)
4126 if (v->same && v->same->ignore)
4127 v->ignore = 1;
4129 if (v->ignore)
4130 continue;
4132 if (v->giv_type == DEST_REG
4133 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4135 struct induction *v1;
4137 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4138 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4139 v->maybe_dead = 1;
4142 /* Update expression if this was combined, in case other giv was
4143 replaced. */
4144 if (v->same)
4145 v->new_reg = replace_rtx (v->new_reg,
4146 v->same->dest_reg, v->same->new_reg);
4148 if (v->giv_type == DEST_ADDR)
4149 /* Store reduced reg as the address in the memref where we found
4150 this giv. */
4151 validate_change (v->insn, v->location, v->new_reg, 0);
4152 else if (v->replaceable)
4154 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4156 #if 0
4157 /* I can no longer duplicate the original problem. Perhaps
4158 this is unnecessary now? */
4160 /* Replaceable; it isn't strictly necessary to delete the old
4161 insn and emit a new one, because v->dest_reg is now dead.
4163 However, especially when unrolling loops, the special
4164 handling for (set REG0 REG1) in the second cse pass may
4165 make v->dest_reg live again. To avoid this problem, emit
4166 an insn to set the original giv reg from the reduced giv.
4167 We can not delete the original insn, since it may be part
4168 of a LIBCALL, and the code in flow that eliminates dead
4169 libcalls will fail if it is deleted. */
4170 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4171 v->insn);
4172 #endif
4174 else
4176 /* Not replaceable; emit an insn to set the original giv reg from
4177 the reduced giv, same as above. */
4178 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4179 v->insn);
4182 /* When a loop is reversed, givs which depend on the reversed
4183 biv, and which are live outside the loop, must be set to their
4184 correct final value. This insn is only needed if the giv is
4185 not replaceable. The correct final value is the same as the
4186 value that the giv starts the reversed loop with. */
4187 if (bl->reversed && ! v->replaceable)
4188 emit_iv_add_mult (bl->initial_value, v->mult_val,
4189 v->add_val, v->dest_reg, end_insert_before);
4190 else if (v->final_value)
4192 rtx insert_before;
4194 /* If the loop has multiple exits, emit the insn before the
4195 loop to ensure that it will always be executed no matter
4196 how the loop exits. Otherwise, emit the insn after the loop,
4197 since this is slightly more efficient. */
4198 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4199 insert_before = loop_start;
4200 else
4201 insert_before = end_insert_before;
4202 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4203 insert_before);
4205 #if 0
4206 /* If the insn to set the final value of the giv was emitted
4207 before the loop, then we must delete the insn inside the loop
4208 that sets it. If this is a LIBCALL, then we must delete
4209 every insn in the libcall. Note, however, that
4210 final_giv_value will only succeed when there are multiple
4211 exits if the giv is dead at each exit, hence it does not
4212 matter that the original insn remains because it is dead
4213 anyways. */
4214 /* Delete the insn inside the loop that sets the giv since
4215 the giv is now set before (or after) the loop. */
4216 delete_insn (v->insn);
4217 #endif
4220 if (loop_dump_stream)
4222 fprintf (loop_dump_stream, "giv at %d reduced to ",
4223 INSN_UID (v->insn));
4224 print_rtl (loop_dump_stream, v->new_reg);
4225 fprintf (loop_dump_stream, "\n");
4229 /* All the givs based on the biv bl have been reduced if they
4230 merit it. */
4232 /* For each giv not marked as maybe dead that has been combined with a
4233 second giv, clear any "maybe dead" mark on that second giv.
4234 v->new_reg will either be or refer to the register of the giv it
4235 combined with.
4237 Doing this clearing avoids problems in biv elimination where a
4238 giv's new_reg is a complex value that can't be put in the insn but
4239 the giv combined with (with a reg as new_reg) is marked maybe_dead.
4240 Since the register will be used in either case, we'd prefer it be
4241 used from the simpler giv. */
4243 for (v = bl->giv; v; v = v->next_iv)
4244 if (! v->maybe_dead && v->same)
4245 v->same->maybe_dead = 0;
4247 /* Try to eliminate the biv, if it is a candidate.
4248 This won't work if ! all_reduced,
4249 since the givs we planned to use might not have been reduced.
4251 We have to be careful that we didn't initially think we could eliminate
4252 this biv because of a giv that we now think may be dead and shouldn't
4253 be used as a biv replacement.
4255 Also, there is the possibility that we may have a giv that looks
4256 like it can be used to eliminate a biv, but the resulting insn
4257 isn't valid. This can happen, for example, on the 88k, where a
4258 JUMP_INSN can compare a register only with zero. Attempts to
4259 replace it with a compare with a constant will fail.
4261 Note that in cases where this call fails, we may have replaced some
4262 of the occurrences of the biv with a giv, but no harm was done in
4263 doing so in the rare cases where it can occur. */
4265 if (all_reduced == 1 && bl->eliminable
4266 && maybe_eliminate_biv (bl, loop_start, end, 1,
4267 threshold, insn_count))
4270 /* ?? If we created a new test to bypass the loop entirely,
4271 or otherwise drop straight in, based on this test, then
4272 we might want to rewrite it also. This way some later
4273 pass has more hope of removing the initialization of this
4274 biv entirely. */
4276 /* If final_value != 0, then the biv may be used after loop end
4277 and we must emit an insn to set it just in case.
4279 Reversed bivs already have an insn after the loop setting their
4280 value, so we don't need another one. We can't calculate the
4281 proper final value for such a biv here anyways. */
4282 if (final_value != 0 && ! bl->reversed)
4284 rtx insert_before;
4286 /* If the loop has multiple exits, emit the insn before the
4287 loop to ensure that it will always be executed no matter
4288 how the loop exits. Otherwise, emit the insn after the
4289 loop, since this is slightly more efficient. */
4290 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4291 insert_before = loop_start;
4292 else
4293 insert_before = end_insert_before;
4295 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4296 end_insert_before);
4299 #if 0
4300 /* Delete all of the instructions inside the loop which set
4301 the biv, as they are all dead. If is safe to delete them,
4302 because an insn setting a biv will never be part of a libcall. */
4303 /* However, deleting them will invalidate the regno_last_uid info,
4304 so keeping them around is more convenient. Final_biv_value
4305 will only succeed when there are multiple exits if the biv
4306 is dead at each exit, hence it does not matter that the original
4307 insn remains, because it is dead anyways. */
4308 for (v = bl->biv; v; v = v->next_iv)
4309 delete_insn (v->insn);
4310 #endif
4312 if (loop_dump_stream)
4313 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4314 bl->regno);
4318 /* Go through all the instructions in the loop, making all the
4319 register substitutions scheduled in REG_MAP. */
4321 for (p = loop_start; p != end; p = NEXT_INSN (p))
4322 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4323 || GET_CODE (p) == CALL_INSN)
4325 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4326 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4327 INSN_CODE (p) = -1;
4330 /* Unroll loops from within strength reduction so that we can use the
4331 induction variable information that strength_reduce has already
4332 collected. */
4334 if (flag_unroll_loops)
4335 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4337 if (loop_dump_stream)
4338 fprintf (loop_dump_stream, "\n");
4341 /* Return 1 if X is a valid source for an initial value (or as value being
4342 compared against in an initial test).
4344 X must be either a register or constant and must not be clobbered between
4345 the current insn and the start of the loop.
4347 INSN is the insn containing X. */
4349 static int
4350 valid_initial_value_p (x, insn, call_seen, loop_start)
4351 rtx x;
4352 rtx insn;
4353 int call_seen;
4354 rtx loop_start;
4356 if (CONSTANT_P (x))
4357 return 1;
4359 /* Only consider pseudos we know about initialized in insns whose luids
4360 we know. */
4361 if (GET_CODE (x) != REG
4362 || REGNO (x) >= max_reg_before_loop)
4363 return 0;
4365 /* Don't use call-clobbered registers across a call which clobbers it. On
4366 some machines, don't use any hard registers at all. */
4367 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4368 && (SMALL_REGISTER_CLASSES
4369 || (call_used_regs[REGNO (x)] && call_seen)))
4370 return 0;
4372 /* Don't use registers that have been clobbered before the start of the
4373 loop. */
4374 if (reg_set_between_p (x, insn, loop_start))
4375 return 0;
4377 return 1;
4380 /* Scan X for memory refs and check each memory address
4381 as a possible giv. INSN is the insn whose pattern X comes from.
4382 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4383 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
4384 more thanonce in each loop iteration. */
4386 static void
4387 find_mem_givs (x, insn, not_every_iteration, maybe_multiple, loop_start,
4388 loop_end)
4389 rtx x;
4390 rtx insn;
4391 int not_every_iteration, maybe_multiple;
4392 rtx loop_start, loop_end;
4394 register int i, j;
4395 register enum rtx_code code;
4396 register char *fmt;
4398 if (x == 0)
4399 return;
4401 code = GET_CODE (x);
4402 switch (code)
4404 case REG:
4405 case CONST_INT:
4406 case CONST:
4407 case CONST_DOUBLE:
4408 case SYMBOL_REF:
4409 case LABEL_REF:
4410 case PC:
4411 case CC0:
4412 case ADDR_VEC:
4413 case ADDR_DIFF_VEC:
4414 case USE:
4415 case CLOBBER:
4416 return;
4418 case MEM:
4420 rtx src_reg;
4421 rtx add_val;
4422 rtx mult_val;
4423 int benefit;
4425 benefit = general_induction_var (XEXP (x, 0),
4426 &src_reg, &add_val, &mult_val);
4428 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4429 Such a giv isn't useful. */
4430 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4432 /* Found one; record it. */
4433 struct induction *v
4434 = (struct induction *) oballoc (sizeof (struct induction));
4436 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4437 add_val, benefit, DEST_ADDR, not_every_iteration,
4438 maybe_multiple, &XEXP (x, 0), loop_start, loop_end);
4440 v->mem_mode = GET_MODE (x);
4443 return;
4445 default:
4446 break;
4449 /* Recursively scan the subexpressions for other mem refs. */
4451 fmt = GET_RTX_FORMAT (code);
4452 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4453 if (fmt[i] == 'e')
4454 find_mem_givs (XEXP (x, i), insn, not_every_iteration, maybe_multiple,
4455 loop_start, loop_end);
4456 else if (fmt[i] == 'E')
4457 for (j = 0; j < XVECLEN (x, i); j++)
4458 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4459 maybe_multiple, loop_start, loop_end);
4462 /* Fill in the data about one biv update.
4463 V is the `struct induction' in which we record the biv. (It is
4464 allocated by the caller, with alloca.)
4465 INSN is the insn that sets it.
4466 DEST_REG is the biv's reg.
4468 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4469 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
4470 being set to INC_VAL.
4472 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4473 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4474 can be executed more than once per iteration. If MAYBE_MULTIPLE
4475 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4476 executed exactly once per iteration. */
4478 static void
4479 record_biv (v, insn, dest_reg, inc_val, mult_val,
4480 not_every_iteration, maybe_multiple)
4481 struct induction *v;
4482 rtx insn;
4483 rtx dest_reg;
4484 rtx inc_val;
4485 rtx mult_val;
4486 int not_every_iteration;
4487 int maybe_multiple;
4489 struct iv_class *bl;
4491 v->insn = insn;
4492 v->src_reg = dest_reg;
4493 v->dest_reg = dest_reg;
4494 v->mult_val = mult_val;
4495 v->add_val = inc_val;
4496 v->mode = GET_MODE (dest_reg);
4497 v->always_computable = ! not_every_iteration;
4498 v->always_executed = ! not_every_iteration;
4499 v->maybe_multiple = maybe_multiple;
4501 /* Add this to the reg's iv_class, creating a class
4502 if this is the first incrementation of the reg. */
4504 bl = reg_biv_class[REGNO (dest_reg)];
4505 if (bl == 0)
4507 /* Create and initialize new iv_class. */
4509 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4511 bl->regno = REGNO (dest_reg);
4512 bl->biv = 0;
4513 bl->giv = 0;
4514 bl->biv_count = 0;
4515 bl->giv_count = 0;
4517 /* Set initial value to the reg itself. */
4518 bl->initial_value = dest_reg;
4519 /* We haven't seen the initializing insn yet */
4520 bl->init_insn = 0;
4521 bl->init_set = 0;
4522 bl->initial_test = 0;
4523 bl->incremented = 0;
4524 bl->eliminable = 0;
4525 bl->nonneg = 0;
4526 bl->reversed = 0;
4527 bl->total_benefit = 0;
4529 /* Add this class to loop_iv_list. */
4530 bl->next = loop_iv_list;
4531 loop_iv_list = bl;
4533 /* Put it in the array of biv register classes. */
4534 reg_biv_class[REGNO (dest_reg)] = bl;
4537 /* Update IV_CLASS entry for this biv. */
4538 v->next_iv = bl->biv;
4539 bl->biv = v;
4540 bl->biv_count++;
4541 if (mult_val == const1_rtx)
4542 bl->incremented = 1;
4544 if (loop_dump_stream)
4546 fprintf (loop_dump_stream,
4547 "Insn %d: possible biv, reg %d,",
4548 INSN_UID (insn), REGNO (dest_reg));
4549 if (GET_CODE (inc_val) == CONST_INT)
4550 fprintf (loop_dump_stream, " const = %d\n",
4551 INTVAL (inc_val));
4552 else
4554 fprintf (loop_dump_stream, " const = ");
4555 print_rtl (loop_dump_stream, inc_val);
4556 fprintf (loop_dump_stream, "\n");
4561 /* Fill in the data about one giv.
4562 V is the `struct induction' in which we record the giv. (It is
4563 allocated by the caller, with alloca.)
4564 INSN is the insn that sets it.
4565 BENEFIT estimates the savings from deleting this insn.
4566 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4567 into a register or is used as a memory address.
4569 SRC_REG is the biv reg which the giv is computed from.
4570 DEST_REG is the giv's reg (if the giv is stored in a reg).
4571 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4572 LOCATION points to the place where this giv's value appears in INSN. */
4574 static void
4575 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4576 type, not_every_iteration, maybe_multiple, location, loop_start,
4577 loop_end)
4578 struct induction *v;
4579 rtx insn;
4580 rtx src_reg;
4581 rtx dest_reg;
4582 rtx mult_val, add_val;
4583 int benefit;
4584 enum g_types type;
4585 int not_every_iteration, maybe_multiple;
4586 rtx *location;
4587 rtx loop_start, loop_end;
4589 struct induction *b;
4590 struct iv_class *bl;
4591 rtx set = single_set (insn);
4592 rtx p;
4594 v->insn = insn;
4595 v->src_reg = src_reg;
4596 v->giv_type = type;
4597 v->dest_reg = dest_reg;
4598 v->mult_val = mult_val;
4599 v->add_val = add_val;
4600 v->benefit = benefit;
4601 v->location = location;
4602 v->cant_derive = 0;
4603 v->combined_with = 0;
4604 v->maybe_multiple = maybe_multiple;
4605 v->maybe_dead = 0;
4606 v->derive_adjustment = 0;
4607 v->same = 0;
4608 v->ignore = 0;
4609 v->new_reg = 0;
4610 v->final_value = 0;
4611 v->same_insn = 0;
4612 v->auto_inc_opt = 0;
4614 /* The v->always_computable field is used in update_giv_derive, to
4615 determine whether a giv can be used to derive another giv. For a
4616 DEST_REG giv, INSN computes a new value for the giv, so its value
4617 isn't computable if INSN insn't executed every iteration.
4618 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4619 it does not compute a new value. Hence the value is always computable
4620 regardless of whether INSN is executed each iteration. */
4622 if (type == DEST_ADDR)
4623 v->always_computable = 1;
4624 else
4625 v->always_computable = ! not_every_iteration;
4627 v->always_executed = ! not_every_iteration;
4629 if (type == DEST_ADDR)
4631 v->mode = GET_MODE (*location);
4632 v->lifetime = 1;
4633 v->times_used = 1;
4635 else /* type == DEST_REG */
4637 v->mode = GET_MODE (SET_DEST (set));
4639 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4640 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4642 v->times_used = n_times_used[REGNO (dest_reg)];
4644 /* If the lifetime is zero, it means that this register is
4645 really a dead store. So mark this as a giv that can be
4646 ignored. This will not prevent the biv from being eliminated. */
4647 if (v->lifetime == 0)
4648 v->ignore = 1;
4650 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4651 reg_iv_info[REGNO (dest_reg)] = v;
4654 /* Add the giv to the class of givs computed from one biv. */
4656 bl = reg_biv_class[REGNO (src_reg)];
4657 if (bl)
4659 v->next_iv = bl->giv;
4660 bl->giv = v;
4661 /* Don't count DEST_ADDR. This is supposed to count the number of
4662 insns that calculate givs. */
4663 if (type == DEST_REG)
4664 bl->giv_count++;
4665 bl->total_benefit += benefit;
4667 else
4668 /* Fatal error, biv missing for this giv? */
4669 abort ();
4671 if (type == DEST_ADDR)
4672 v->replaceable = 1;
4673 else
4675 /* The giv can be replaced outright by the reduced register only if all
4676 of the following conditions are true:
4677 - the insn that sets the giv is always executed on any iteration
4678 on which the giv is used at all
4679 (there are two ways to deduce this:
4680 either the insn is executed on every iteration,
4681 or all uses follow that insn in the same basic block),
4682 - the giv is not used outside the loop
4683 - no assignments to the biv occur during the giv's lifetime. */
4685 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4686 /* Previous line always fails if INSN was moved by loop opt. */
4687 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4688 && (! not_every_iteration
4689 || last_use_this_basic_block (dest_reg, insn)))
4691 /* Now check that there are no assignments to the biv within the
4692 giv's lifetime. This requires two separate checks. */
4694 /* Check each biv update, and fail if any are between the first
4695 and last use of the giv.
4697 If this loop contains an inner loop that was unrolled, then
4698 the insn modifying the biv may have been emitted by the loop
4699 unrolling code, and hence does not have a valid luid. Just
4700 mark the biv as not replaceable in this case. It is not very
4701 useful as a biv, because it is used in two different loops.
4702 It is very unlikely that we would be able to optimize the giv
4703 using this biv anyways. */
4705 v->replaceable = 1;
4706 for (b = bl->biv; b; b = b->next_iv)
4708 if (INSN_UID (b->insn) >= max_uid_for_loop
4709 || ((uid_luid[INSN_UID (b->insn)]
4710 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4711 && (uid_luid[INSN_UID (b->insn)]
4712 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4714 v->replaceable = 0;
4715 v->not_replaceable = 1;
4716 break;
4720 /* If there are any backwards branches that go from after the
4721 biv update to before it, then this giv is not replaceable. */
4722 if (v->replaceable)
4723 for (b = bl->biv; b; b = b->next_iv)
4724 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4726 v->replaceable = 0;
4727 v->not_replaceable = 1;
4728 break;
4731 else
4733 /* May still be replaceable, we don't have enough info here to
4734 decide. */
4735 v->replaceable = 0;
4736 v->not_replaceable = 0;
4740 if (loop_dump_stream)
4742 if (type == DEST_REG)
4743 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4744 INSN_UID (insn), REGNO (dest_reg));
4745 else
4746 fprintf (loop_dump_stream, "Insn %d: dest address",
4747 INSN_UID (insn));
4749 fprintf (loop_dump_stream, " src reg %d benefit %d",
4750 REGNO (src_reg), v->benefit);
4751 fprintf (loop_dump_stream, " used %d lifetime %d",
4752 v->times_used, v->lifetime);
4754 if (v->replaceable)
4755 fprintf (loop_dump_stream, " replaceable");
4757 if (GET_CODE (mult_val) == CONST_INT)
4758 fprintf (loop_dump_stream, " mult %d",
4759 INTVAL (mult_val));
4760 else
4762 fprintf (loop_dump_stream, " mult ");
4763 print_rtl (loop_dump_stream, mult_val);
4766 if (GET_CODE (add_val) == CONST_INT)
4767 fprintf (loop_dump_stream, " add %d",
4768 INTVAL (add_val));
4769 else
4771 fprintf (loop_dump_stream, " add ");
4772 print_rtl (loop_dump_stream, add_val);
4776 if (loop_dump_stream)
4777 fprintf (loop_dump_stream, "\n");
4782 /* All this does is determine whether a giv can be made replaceable because
4783 its final value can be calculated. This code can not be part of record_giv
4784 above, because final_giv_value requires that the number of loop iterations
4785 be known, and that can not be accurately calculated until after all givs
4786 have been identified. */
4788 static void
4789 check_final_value (v, loop_start, loop_end)
4790 struct induction *v;
4791 rtx loop_start, loop_end;
4793 struct iv_class *bl;
4794 rtx final_value = 0;
4796 bl = reg_biv_class[REGNO (v->src_reg)];
4798 /* DEST_ADDR givs will never reach here, because they are always marked
4799 replaceable above in record_giv. */
4801 /* The giv can be replaced outright by the reduced register only if all
4802 of the following conditions are true:
4803 - the insn that sets the giv is always executed on any iteration
4804 on which the giv is used at all
4805 (there are two ways to deduce this:
4806 either the insn is executed on every iteration,
4807 or all uses follow that insn in the same basic block),
4808 - its final value can be calculated (this condition is different
4809 than the one above in record_giv)
4810 - no assignments to the biv occur during the giv's lifetime. */
4812 #if 0
4813 /* This is only called now when replaceable is known to be false. */
4814 /* Clear replaceable, so that it won't confuse final_giv_value. */
4815 v->replaceable = 0;
4816 #endif
4818 if ((final_value = final_giv_value (v, loop_start, loop_end))
4819 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4821 int biv_increment_seen = 0;
4822 rtx p = v->insn;
4823 rtx last_giv_use;
4825 v->replaceable = 1;
4827 /* When trying to determine whether or not a biv increment occurs
4828 during the lifetime of the giv, we can ignore uses of the variable
4829 outside the loop because final_value is true. Hence we can not
4830 use regno_last_uid and regno_first_uid as above in record_giv. */
4832 /* Search the loop to determine whether any assignments to the
4833 biv occur during the giv's lifetime. Start with the insn
4834 that sets the giv, and search around the loop until we come
4835 back to that insn again.
4837 Also fail if there is a jump within the giv's lifetime that jumps
4838 to somewhere outside the lifetime but still within the loop. This
4839 catches spaghetti code where the execution order is not linear, and
4840 hence the above test fails. Here we assume that the giv lifetime
4841 does not extend from one iteration of the loop to the next, so as
4842 to make the test easier. Since the lifetime isn't known yet,
4843 this requires two loops. See also record_giv above. */
4845 last_giv_use = v->insn;
4847 while (1)
4849 p = NEXT_INSN (p);
4850 if (p == loop_end)
4851 p = NEXT_INSN (loop_start);
4852 if (p == v->insn)
4853 break;
4855 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4856 || GET_CODE (p) == CALL_INSN)
4858 if (biv_increment_seen)
4860 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4862 v->replaceable = 0;
4863 v->not_replaceable = 1;
4864 break;
4867 else if (reg_set_p (v->src_reg, PATTERN (p)))
4868 biv_increment_seen = 1;
4869 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4870 last_giv_use = p;
4874 /* Now that the lifetime of the giv is known, check for branches
4875 from within the lifetime to outside the lifetime if it is still
4876 replaceable. */
4878 if (v->replaceable)
4880 p = v->insn;
4881 while (1)
4883 p = NEXT_INSN (p);
4884 if (p == loop_end)
4885 p = NEXT_INSN (loop_start);
4886 if (p == last_giv_use)
4887 break;
4889 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4890 && LABEL_NAME (JUMP_LABEL (p))
4891 && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
4892 || (INSN_UID (v->insn) >= max_uid_for_loop)
4893 || (INSN_UID (last_giv_use) >= max_uid_for_loop)
4894 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
4895 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
4896 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
4897 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
4899 v->replaceable = 0;
4900 v->not_replaceable = 1;
4902 if (loop_dump_stream)
4903 fprintf (loop_dump_stream,
4904 "Found branch outside giv lifetime.\n");
4906 break;
4911 /* If it is replaceable, then save the final value. */
4912 if (v->replaceable)
4913 v->final_value = final_value;
4916 if (loop_dump_stream && v->replaceable)
4917 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
4918 INSN_UID (v->insn), REGNO (v->dest_reg));
4921 /* Update the status of whether a giv can derive other givs.
4923 We need to do something special if there is or may be an update to the biv
4924 between the time the giv is defined and the time it is used to derive
4925 another giv.
4927 In addition, a giv that is only conditionally set is not allowed to
4928 derive another giv once a label has been passed.
4930 The cases we look at are when a label or an update to a biv is passed. */
4932 static void
4933 update_giv_derive (p)
4934 rtx p;
4936 struct iv_class *bl;
4937 struct induction *biv, *giv;
4938 rtx tem;
4939 int dummy;
4941 /* Search all IV classes, then all bivs, and finally all givs.
4943 There are three cases we are concerned with. First we have the situation
4944 of a giv that is only updated conditionally. In that case, it may not
4945 derive any givs after a label is passed.
4947 The second case is when a biv update occurs, or may occur, after the
4948 definition of a giv. For certain biv updates (see below) that are
4949 known to occur between the giv definition and use, we can adjust the
4950 giv definition. For others, or when the biv update is conditional,
4951 we must prevent the giv from deriving any other givs. There are two
4952 sub-cases within this case.
4954 If this is a label, we are concerned with any biv update that is done
4955 conditionally, since it may be done after the giv is defined followed by
4956 a branch here (actually, we need to pass both a jump and a label, but
4957 this extra tracking doesn't seem worth it).
4959 If this is a jump, we are concerned about any biv update that may be
4960 executed multiple times. We are actually only concerned about
4961 backward jumps, but it is probably not worth performing the test
4962 on the jump again here.
4964 If this is a biv update, we must adjust the giv status to show that a
4965 subsequent biv update was performed. If this adjustment cannot be done,
4966 the giv cannot derive further givs. */
4968 for (bl = loop_iv_list; bl; bl = bl->next)
4969 for (biv = bl->biv; biv; biv = biv->next_iv)
4970 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
4971 || biv->insn == p)
4973 for (giv = bl->giv; giv; giv = giv->next_iv)
4975 /* If cant_derive is already true, there is no point in
4976 checking all of these conditions again. */
4977 if (giv->cant_derive)
4978 continue;
4980 /* If this giv is conditionally set and we have passed a label,
4981 it cannot derive anything. */
4982 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
4983 giv->cant_derive = 1;
4985 /* Skip givs that have mult_val == 0, since
4986 they are really invariants. Also skip those that are
4987 replaceable, since we know their lifetime doesn't contain
4988 any biv update. */
4989 else if (giv->mult_val == const0_rtx || giv->replaceable)
4990 continue;
4992 /* The only way we can allow this giv to derive another
4993 is if this is a biv increment and we can form the product
4994 of biv->add_val and giv->mult_val. In this case, we will
4995 be able to compute a compensation. */
4996 else if (biv->insn == p)
4998 tem = 0;
5000 if (biv->mult_val == const1_rtx)
5001 tem = simplify_giv_expr (gen_rtx_MULT (giv->mode,
5002 biv->add_val,
5003 giv->mult_val),
5004 &dummy);
5006 if (tem && giv->derive_adjustment)
5007 tem = simplify_giv_expr
5008 (gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
5009 &dummy);
5011 if (tem)
5012 giv->derive_adjustment = tem;
5013 else
5014 giv->cant_derive = 1;
5016 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5017 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5018 giv->cant_derive = 1;
5023 /* Check whether an insn is an increment legitimate for a basic induction var.
5024 X is the source of insn P, or a part of it.
5025 MODE is the mode in which X should be interpreted.
5027 DEST_REG is the putative biv, also the destination of the insn.
5028 We accept patterns of these forms:
5029 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5030 REG = INVARIANT + REG
5032 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5033 and store the additive term into *INC_VAL.
5035 If X is an assignment of an invariant into DEST_REG, we set
5036 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5038 We also want to detect a BIV when it corresponds to a variable
5039 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5040 of the variable may be a PLUS that adds a SUBREG of that variable to
5041 an invariant and then sign- or zero-extends the result of the PLUS
5042 into the variable.
5044 Most GIVs in such cases will be in the promoted mode, since that is the
5045 probably the natural computation mode (and almost certainly the mode
5046 used for addresses) on the machine. So we view the pseudo-reg containing
5047 the variable as the BIV, as if it were simply incremented.
5049 Note that treating the entire pseudo as a BIV will result in making
5050 simple increments to any GIVs based on it. However, if the variable
5051 overflows in its declared mode but not its promoted mode, the result will
5052 be incorrect. This is acceptable if the variable is signed, since
5053 overflows in such cases are undefined, but not if it is unsigned, since
5054 those overflows are defined. So we only check for SIGN_EXTEND and
5055 not ZERO_EXTEND.
5057 If we cannot find a biv, we return 0. */
5059 static int
5060 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5061 register rtx x;
5062 enum machine_mode mode;
5063 rtx p;
5064 rtx dest_reg;
5065 rtx *inc_val;
5066 rtx *mult_val;
5068 register enum rtx_code code;
5069 rtx arg;
5070 rtx insn, set = 0;
5072 code = GET_CODE (x);
5073 switch (code)
5075 case PLUS:
5076 if (XEXP (x, 0) == dest_reg
5077 || (GET_CODE (XEXP (x, 0)) == SUBREG
5078 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5079 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5080 arg = XEXP (x, 1);
5081 else if (XEXP (x, 1) == dest_reg
5082 || (GET_CODE (XEXP (x, 1)) == SUBREG
5083 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5084 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5085 arg = XEXP (x, 0);
5086 else
5087 return 0;
5089 if (invariant_p (arg) != 1)
5090 return 0;
5092 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5093 *mult_val = const1_rtx;
5094 return 1;
5096 case SUBREG:
5097 /* If this is a SUBREG for a promoted variable, check the inner
5098 value. */
5099 if (SUBREG_PROMOTED_VAR_P (x))
5100 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5101 dest_reg, p, inc_val, mult_val);
5102 return 0;
5104 case REG:
5105 /* If this register is assigned in the previous insn, look at its
5106 source, but don't go outside the loop or past a label. */
5108 for (insn = PREV_INSN (p);
5109 (insn && GET_CODE (insn) == NOTE
5110 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5111 insn = PREV_INSN (insn))
5114 if (insn)
5115 set = single_set (insn);
5117 if (set != 0
5118 && (SET_DEST (set) == x
5119 || (GET_CODE (SET_DEST (set)) == SUBREG
5120 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5121 <= UNITS_PER_WORD)
5122 && SUBREG_REG (SET_DEST (set)) == x)))
5123 return basic_induction_var (SET_SRC (set),
5124 (GET_MODE (SET_SRC (set)) == VOIDmode
5125 ? GET_MODE (x)
5126 : GET_MODE (SET_SRC (set))),
5127 dest_reg, insn,
5128 inc_val, mult_val);
5129 /* ... fall through ... */
5131 /* Can accept constant setting of biv only when inside inner most loop.
5132 Otherwise, a biv of an inner loop may be incorrectly recognized
5133 as a biv of the outer loop,
5134 causing code to be moved INTO the inner loop. */
5135 case MEM:
5136 if (invariant_p (x) != 1)
5137 return 0;
5138 case CONST_INT:
5139 case SYMBOL_REF:
5140 case CONST:
5141 if (loops_enclosed == 1)
5143 /* Possible bug here? Perhaps we don't know the mode of X. */
5144 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5145 *mult_val = const0_rtx;
5146 return 1;
5148 else
5149 return 0;
5151 case SIGN_EXTEND:
5152 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5153 dest_reg, p, inc_val, mult_val);
5154 case ASHIFTRT:
5155 /* Similar, since this can be a sign extension. */
5156 for (insn = PREV_INSN (p);
5157 (insn && GET_CODE (insn) == NOTE
5158 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5159 insn = PREV_INSN (insn))
5162 if (insn)
5163 set = single_set (insn);
5165 if (set && SET_DEST (set) == XEXP (x, 0)
5166 && GET_CODE (XEXP (x, 1)) == CONST_INT
5167 && INTVAL (XEXP (x, 1)) >= 0
5168 && GET_CODE (SET_SRC (set)) == ASHIFT
5169 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5170 return basic_induction_var (XEXP (SET_SRC (set), 0),
5171 GET_MODE (XEXP (x, 0)),
5172 dest_reg, insn, inc_val, mult_val);
5173 return 0;
5175 default:
5176 return 0;
5180 /* A general induction variable (giv) is any quantity that is a linear
5181 function of a basic induction variable,
5182 i.e. giv = biv * mult_val + add_val.
5183 The coefficients can be any loop invariant quantity.
5184 A giv need not be computed directly from the biv;
5185 it can be computed by way of other givs. */
5187 /* Determine whether X computes a giv.
5188 If it does, return a nonzero value
5189 which is the benefit from eliminating the computation of X;
5190 set *SRC_REG to the register of the biv that it is computed from;
5191 set *ADD_VAL and *MULT_VAL to the coefficients,
5192 such that the value of X is biv * mult + add; */
5194 static int
5195 general_induction_var (x, src_reg, add_val, mult_val)
5196 rtx x;
5197 rtx *src_reg;
5198 rtx *add_val;
5199 rtx *mult_val;
5201 rtx orig_x = x;
5202 int benefit = 0;
5203 char *storage;
5205 /* If this is an invariant, forget it, it isn't a giv. */
5206 if (invariant_p (x) == 1)
5207 return 0;
5209 /* See if the expression could be a giv and get its form.
5210 Mark our place on the obstack in case we don't find a giv. */
5211 storage = (char *) oballoc (0);
5212 x = simplify_giv_expr (x, &benefit);
5213 if (x == 0)
5215 obfree (storage);
5216 return 0;
5219 switch (GET_CODE (x))
5221 case USE:
5222 case CONST_INT:
5223 /* Since this is now an invariant and wasn't before, it must be a giv
5224 with MULT_VAL == 0. It doesn't matter which BIV we associate this
5225 with. */
5226 *src_reg = loop_iv_list->biv->dest_reg;
5227 *mult_val = const0_rtx;
5228 *add_val = x;
5229 break;
5231 case REG:
5232 /* This is equivalent to a BIV. */
5233 *src_reg = x;
5234 *mult_val = const1_rtx;
5235 *add_val = const0_rtx;
5236 break;
5238 case PLUS:
5239 /* Either (plus (biv) (invar)) or
5240 (plus (mult (biv) (invar_1)) (invar_2)). */
5241 if (GET_CODE (XEXP (x, 0)) == MULT)
5243 *src_reg = XEXP (XEXP (x, 0), 0);
5244 *mult_val = XEXP (XEXP (x, 0), 1);
5246 else
5248 *src_reg = XEXP (x, 0);
5249 *mult_val = const1_rtx;
5251 *add_val = XEXP (x, 1);
5252 break;
5254 case MULT:
5255 /* ADD_VAL is zero. */
5256 *src_reg = XEXP (x, 0);
5257 *mult_val = XEXP (x, 1);
5258 *add_val = const0_rtx;
5259 break;
5261 default:
5262 abort ();
5265 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5266 unless they are CONST_INT). */
5267 if (GET_CODE (*add_val) == USE)
5268 *add_val = XEXP (*add_val, 0);
5269 if (GET_CODE (*mult_val) == USE)
5270 *mult_val = XEXP (*mult_val, 0);
5272 benefit += rtx_cost (orig_x, SET);
5274 /* Always return some benefit if this is a giv so it will be detected
5275 as such. This allows elimination of bivs that might otherwise
5276 not be eliminated. */
5277 return benefit == 0 ? 1 : benefit;
5280 /* Given an expression, X, try to form it as a linear function of a biv.
5281 We will canonicalize it to be of the form
5282 (plus (mult (BIV) (invar_1))
5283 (invar_2))
5284 with possible degeneracies.
5286 The invariant expressions must each be of a form that can be used as a
5287 machine operand. We surround then with a USE rtx (a hack, but localized
5288 and certainly unambiguous!) if not a CONST_INT for simplicity in this
5289 routine; it is the caller's responsibility to strip them.
5291 If no such canonicalization is possible (i.e., two biv's are used or an
5292 expression that is neither invariant nor a biv or giv), this routine
5293 returns 0.
5295 For a non-zero return, the result will have a code of CONST_INT, USE,
5296 REG (for a BIV), PLUS, or MULT. No other codes will occur.
5298 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
5300 static rtx
5301 simplify_giv_expr (x, benefit)
5302 rtx x;
5303 int *benefit;
5305 enum machine_mode mode = GET_MODE (x);
5306 rtx arg0, arg1;
5307 rtx tem;
5309 /* If this is not an integer mode, or if we cannot do arithmetic in this
5310 mode, this can't be a giv. */
5311 if (mode != VOIDmode
5312 && (GET_MODE_CLASS (mode) != MODE_INT
5313 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5314 return 0;
5316 switch (GET_CODE (x))
5318 case PLUS:
5319 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5320 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5321 if (arg0 == 0 || arg1 == 0)
5322 return 0;
5324 /* Put constant last, CONST_INT last if both constant. */
5325 if ((GET_CODE (arg0) == USE
5326 || GET_CODE (arg0) == CONST_INT)
5327 && GET_CODE (arg1) != CONST_INT)
5328 tem = arg0, arg0 = arg1, arg1 = tem;
5330 /* Handle addition of zero, then addition of an invariant. */
5331 if (arg1 == const0_rtx)
5332 return arg0;
5333 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5334 switch (GET_CODE (arg0))
5336 case CONST_INT:
5337 case USE:
5338 /* Both invariant. Only valid if sum is machine operand.
5339 First strip off possible USE on first operand. */
5340 if (GET_CODE (arg0) == USE)
5341 arg0 = XEXP (arg0, 0);
5343 tem = 0;
5344 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5346 tem = plus_constant (arg0, INTVAL (arg1));
5347 if (GET_CODE (tem) != CONST_INT)
5348 tem = gen_rtx_USE (mode, tem);
5351 return tem;
5353 case REG:
5354 case MULT:
5355 /* biv + invar or mult + invar. Return sum. */
5356 return gen_rtx_PLUS (mode, arg0, arg1);
5358 case PLUS:
5359 /* (a + invar_1) + invar_2. Associate. */
5360 return
5361 simplify_giv_expr (gen_rtx_PLUS (mode,
5362 XEXP (arg0, 0),
5363 gen_rtx_PLUS (mode,
5364 XEXP (arg0, 1),
5365 arg1)),
5366 benefit);
5368 default:
5369 abort ();
5372 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5373 MULT to reduce cases. */
5374 if (GET_CODE (arg0) == REG)
5375 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
5376 if (GET_CODE (arg1) == REG)
5377 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
5379 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5380 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5381 Recurse to associate the second PLUS. */
5382 if (GET_CODE (arg1) == MULT)
5383 tem = arg0, arg0 = arg1, arg1 = tem;
5385 if (GET_CODE (arg1) == PLUS)
5386 return
5387 simplify_giv_expr (gen_rtx_PLUS (mode,
5388 gen_rtx_PLUS (mode, arg0,
5389 XEXP (arg1, 0)),
5390 XEXP (arg1, 1)),
5391 benefit);
5393 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5394 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5395 abort ();
5397 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5398 return 0;
5400 return simplify_giv_expr (gen_rtx_MULT (mode,
5401 XEXP (arg0, 0),
5402 gen_rtx_PLUS (mode,
5403 XEXP (arg0, 1),
5404 XEXP (arg1, 1))),
5405 benefit);
5407 case MINUS:
5408 /* Handle "a - b" as "a + b * (-1)". */
5409 return simplify_giv_expr (gen_rtx_PLUS (mode,
5410 XEXP (x, 0),
5411 gen_rtx_MULT (mode,
5412 XEXP (x, 1),
5413 constm1_rtx)),
5414 benefit);
5416 case MULT:
5417 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5418 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5419 if (arg0 == 0 || arg1 == 0)
5420 return 0;
5422 /* Put constant last, CONST_INT last if both constant. */
5423 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5424 && GET_CODE (arg1) != CONST_INT)
5425 tem = arg0, arg0 = arg1, arg1 = tem;
5427 /* If second argument is not now constant, not giv. */
5428 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5429 return 0;
5431 /* Handle multiply by 0 or 1. */
5432 if (arg1 == const0_rtx)
5433 return const0_rtx;
5435 else if (arg1 == const1_rtx)
5436 return arg0;
5438 switch (GET_CODE (arg0))
5440 case REG:
5441 /* biv * invar. Done. */
5442 return gen_rtx_MULT (mode, arg0, arg1);
5444 case CONST_INT:
5445 /* Product of two constants. */
5446 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5448 case USE:
5449 /* invar * invar. Not giv. */
5450 return 0;
5452 case MULT:
5453 /* (a * invar_1) * invar_2. Associate. */
5454 return simplify_giv_expr (gen_rtx_MULT (mode,
5455 XEXP (arg0, 0),
5456 gen_rtx_MULT (mode,
5457 XEXP (arg0, 1),
5458 arg1)),
5459 benefit);
5461 case PLUS:
5462 /* (a + invar_1) * invar_2. Distribute. */
5463 return simplify_giv_expr (gen_rtx_PLUS (mode,
5464 gen_rtx_MULT (mode,
5465 XEXP (arg0, 0),
5466 arg1),
5467 gen_rtx_MULT (mode,
5468 XEXP (arg0, 1),
5469 arg1)),
5470 benefit);
5472 default:
5473 abort ();
5476 case ASHIFT:
5477 /* Shift by constant is multiply by power of two. */
5478 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5479 return 0;
5481 return
5482 simplify_giv_expr (gen_rtx_MULT (mode,
5483 XEXP (x, 0),
5484 GEN_INT ((HOST_WIDE_INT) 1
5485 << INTVAL (XEXP (x, 1)))),
5486 benefit);
5488 case NEG:
5489 /* "-a" is "a * (-1)" */
5490 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
5491 benefit);
5493 case NOT:
5494 /* "~a" is "-a - 1". Silly, but easy. */
5495 return simplify_giv_expr (gen_rtx_MINUS (mode,
5496 gen_rtx_NEG (mode, XEXP (x, 0)),
5497 const1_rtx),
5498 benefit);
5500 case USE:
5501 /* Already in proper form for invariant. */
5502 return x;
5504 case REG:
5505 /* If this is a new register, we can't deal with it. */
5506 if (REGNO (x) >= max_reg_before_loop)
5507 return 0;
5509 /* Check for biv or giv. */
5510 switch (reg_iv_type[REGNO (x)])
5512 case BASIC_INDUCT:
5513 return x;
5514 case GENERAL_INDUCT:
5516 struct induction *v = reg_iv_info[REGNO (x)];
5518 /* Form expression from giv and add benefit. Ensure this giv
5519 can derive another and subtract any needed adjustment if so. */
5520 *benefit += v->benefit;
5521 if (v->cant_derive)
5522 return 0;
5524 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
5525 v->src_reg, v->mult_val),
5526 v->add_val);
5528 if (v->derive_adjustment)
5529 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
5530 return simplify_giv_expr (tem, benefit);
5533 default:
5534 break;
5537 /* Fall through to general case. */
5538 default:
5539 /* If invariant, return as USE (unless CONST_INT).
5540 Otherwise, not giv. */
5541 if (GET_CODE (x) == USE)
5542 x = XEXP (x, 0);
5544 if (invariant_p (x) == 1)
5546 if (GET_CODE (x) == CONST_INT)
5547 return x;
5548 else
5549 return gen_rtx_USE (mode, x);
5551 else
5552 return 0;
5556 /* Help detect a giv that is calculated by several consecutive insns;
5557 for example,
5558 giv = biv * M
5559 giv = giv + A
5560 The caller has already identified the first insn P as having a giv as dest;
5561 we check that all other insns that set the same register follow
5562 immediately after P, that they alter nothing else,
5563 and that the result of the last is still a giv.
5565 The value is 0 if the reg set in P is not really a giv.
5566 Otherwise, the value is the amount gained by eliminating
5567 all the consecutive insns that compute the value.
5569 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5570 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5572 The coefficients of the ultimate giv value are stored in
5573 *MULT_VAL and *ADD_VAL. */
5575 static int
5576 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5577 add_val, mult_val)
5578 int first_benefit;
5579 rtx p;
5580 rtx src_reg;
5581 rtx dest_reg;
5582 rtx *add_val;
5583 rtx *mult_val;
5585 int count;
5586 enum rtx_code code;
5587 int benefit;
5588 rtx temp;
5589 rtx set;
5591 /* Indicate that this is a giv so that we can update the value produced in
5592 each insn of the multi-insn sequence.
5594 This induction structure will be used only by the call to
5595 general_induction_var below, so we can allocate it on our stack.
5596 If this is a giv, our caller will replace the induct var entry with
5597 a new induction structure. */
5598 struct induction *v
5599 = (struct induction *) alloca (sizeof (struct induction));
5600 v->src_reg = src_reg;
5601 v->mult_val = *mult_val;
5602 v->add_val = *add_val;
5603 v->benefit = first_benefit;
5604 v->cant_derive = 0;
5605 v->derive_adjustment = 0;
5607 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5608 reg_iv_info[REGNO (dest_reg)] = v;
5610 count = n_times_set[REGNO (dest_reg)] - 1;
5612 while (count > 0)
5614 p = NEXT_INSN (p);
5615 code = GET_CODE (p);
5617 /* If libcall, skip to end of call sequence. */
5618 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5619 p = XEXP (temp, 0);
5621 if (code == INSN
5622 && (set = single_set (p))
5623 && GET_CODE (SET_DEST (set)) == REG
5624 && SET_DEST (set) == dest_reg
5625 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5626 add_val, mult_val))
5627 /* Giv created by equivalent expression. */
5628 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5629 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5630 add_val, mult_val))))
5631 && src_reg == v->src_reg)
5633 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5634 benefit += libcall_benefit (p);
5636 count--;
5637 v->mult_val = *mult_val;
5638 v->add_val = *add_val;
5639 v->benefit = benefit;
5641 else if (code != NOTE)
5643 /* Allow insns that set something other than this giv to a
5644 constant. Such insns are needed on machines which cannot
5645 include long constants and should not disqualify a giv. */
5646 if (code == INSN
5647 && (set = single_set (p))
5648 && SET_DEST (set) != dest_reg
5649 && CONSTANT_P (SET_SRC (set)))
5650 continue;
5652 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5653 return 0;
5657 return v->benefit;
5660 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5661 represented by G1. If no such expression can be found, or it is clear that
5662 it cannot possibly be a valid address, 0 is returned.
5664 To perform the computation, we note that
5665 G1 = a * v + b and
5666 G2 = c * v + d
5667 where `v' is the biv.
5669 So G2 = (c/a) * G1 + (d - b*c/a) */
5671 #ifdef ADDRESS_COST
5672 static rtx
5673 express_from (g1, g2)
5674 struct induction *g1, *g2;
5676 rtx mult, add;
5678 /* The value that G1 will be multiplied by must be a constant integer. Also,
5679 the only chance we have of getting a valid address is if b*c/a (see above
5680 for notation) is also an integer. */
5681 if (GET_CODE (g1->mult_val) != CONST_INT
5682 || GET_CODE (g2->mult_val) != CONST_INT
5683 || GET_CODE (g1->add_val) != CONST_INT
5684 || g1->mult_val == const0_rtx
5685 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5686 return 0;
5688 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5689 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5691 /* Form simplified final result. */
5692 if (mult == const0_rtx)
5693 return add;
5694 else if (mult == const1_rtx)
5695 mult = g1->dest_reg;
5696 else
5697 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
5699 if (add == const0_rtx)
5700 return mult;
5701 else
5702 return gen_rtx_PLUS (g2->mode, mult, add);
5704 #endif
5706 /* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5707 (either directly or via an address expression) a register used to represent
5708 G1. Set g2->new_reg to a represtation of G1 (normally just
5709 g1->dest_reg). */
5711 static int
5712 combine_givs_p (g1, g2)
5713 struct induction *g1, *g2;
5715 rtx tem;
5717 /* If these givs are identical, they can be combined. */
5718 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5719 && rtx_equal_p (g1->add_val, g2->add_val))
5721 g2->new_reg = g1->dest_reg;
5722 return 1;
5725 #ifdef ADDRESS_COST
5726 /* If G2 can be expressed as a function of G1 and that function is valid
5727 as an address and no more expensive than using a register for G2,
5728 the expression of G2 in terms of G1 can be used. */
5729 if (g2->giv_type == DEST_ADDR
5730 && (tem = express_from (g1, g2)) != 0
5731 && memory_address_p (g2->mem_mode, tem)
5732 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5734 g2->new_reg = tem;
5735 return 1;
5737 #endif
5739 return 0;
5742 #ifdef GIV_SORT_CRITERION
5743 /* Compare two givs and sort the most desirable one for combinations first.
5744 This is used only in one qsort call below. */
5746 static int
5747 giv_sort (x, y)
5748 struct induction **x, **y;
5750 GIV_SORT_CRITERION (*x, *y);
5752 return 0;
5754 #endif
5756 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5757 any other. If so, point SAME to the giv combined with and set NEW_REG to
5758 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5759 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5761 static void
5762 combine_givs (bl)
5763 struct iv_class *bl;
5765 struct induction *g1, *g2, **giv_array, *temp_iv;
5766 int i, j, giv_count, pass;
5768 /* Count givs, because bl->giv_count is incorrect here. */
5769 giv_count = 0;
5770 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5771 giv_count++;
5773 giv_array
5774 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5775 i = 0;
5776 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5777 giv_array[i++] = g1;
5779 #ifdef GIV_SORT_CRITERION
5780 /* Sort the givs if GIV_SORT_CRITERION is defined.
5781 This is usually defined for processors which lack
5782 negative register offsets so more givs may be combined. */
5784 if (loop_dump_stream)
5785 fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5787 qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5788 #endif
5790 for (i = 0; i < giv_count; i++)
5792 g1 = giv_array[i];
5793 for (pass = 0; pass <= 1; pass++)
5794 for (j = 0; j < giv_count; j++)
5796 g2 = giv_array[j];
5797 if (g1 != g2
5798 /* First try to combine with replaceable givs, then all givs. */
5799 && (g1->replaceable || pass == 1)
5800 /* If either has already been combined or is to be ignored, can't
5801 combine. */
5802 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5803 /* If something has been based on G2, G2 cannot itself be based
5804 on something else. */
5805 && ! g2->combined_with
5806 && combine_givs_p (g1, g2))
5808 /* g2->new_reg set by `combine_givs_p' */
5809 g2->same = g1;
5810 g1->combined_with = 1;
5812 /* If one of these givs is a DEST_REG that was only used
5813 once, by the other giv, this is actually a single use.
5814 The DEST_REG has the correct cost, while the other giv
5815 counts the REG use too often. */
5816 if (g2->giv_type == DEST_REG
5817 && n_times_used[REGNO (g2->dest_reg)] == 1
5818 && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5819 g1->benefit = g2->benefit;
5820 else if (g1->giv_type != DEST_REG
5821 || n_times_used[REGNO (g1->dest_reg)] != 1
5822 || ! reg_mentioned_p (g1->dest_reg,
5823 PATTERN (g2->insn)))
5825 g1->benefit += g2->benefit;
5826 g1->times_used += g2->times_used;
5828 /* ??? The new final_[bg]iv_value code does a much better job
5829 of finding replaceable giv's, and hence this code may no
5830 longer be necessary. */
5831 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5832 g1->benefit -= copy_cost;
5833 g1->lifetime += g2->lifetime;
5835 if (loop_dump_stream)
5836 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5837 INSN_UID (g2->insn), INSN_UID (g1->insn));
5843 /* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5845 void
5846 emit_iv_add_mult (b, m, a, reg, insert_before)
5847 rtx b; /* initial value of basic induction variable */
5848 rtx m; /* multiplicative constant */
5849 rtx a; /* additive constant */
5850 rtx reg; /* destination register */
5851 rtx insert_before;
5853 rtx seq;
5854 rtx result;
5856 /* Prevent unexpected sharing of these rtx. */
5857 a = copy_rtx (a);
5858 b = copy_rtx (b);
5860 /* Increase the lifetime of any invariants moved further in code. */
5861 update_reg_last_use (a, insert_before);
5862 update_reg_last_use (b, insert_before);
5863 update_reg_last_use (m, insert_before);
5865 start_sequence ();
5866 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5867 if (reg != result)
5868 emit_move_insn (reg, result);
5869 seq = gen_sequence ();
5870 end_sequence ();
5872 emit_insn_before (seq, insert_before);
5875 /* Test whether A * B can be computed without
5876 an actual multiply insn. Value is 1 if so. */
5878 static int
5879 product_cheap_p (a, b)
5880 rtx a;
5881 rtx b;
5883 int i;
5884 rtx tmp;
5885 struct obstack *old_rtl_obstack = rtl_obstack;
5886 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
5887 int win = 1;
5889 /* If only one is constant, make it B. */
5890 if (GET_CODE (a) == CONST_INT)
5891 tmp = a, a = b, b = tmp;
5893 /* If first constant, both constant, so don't need multiply. */
5894 if (GET_CODE (a) == CONST_INT)
5895 return 1;
5897 /* If second not constant, neither is constant, so would need multiply. */
5898 if (GET_CODE (b) != CONST_INT)
5899 return 0;
5901 /* One operand is constant, so might not need multiply insn. Generate the
5902 code for the multiply and see if a call or multiply, or long sequence
5903 of insns is generated. */
5905 rtl_obstack = &temp_obstack;
5906 start_sequence ();
5907 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
5908 tmp = gen_sequence ();
5909 end_sequence ();
5911 if (GET_CODE (tmp) == SEQUENCE)
5913 if (XVEC (tmp, 0) == 0)
5914 win = 1;
5915 else if (XVECLEN (tmp, 0) > 3)
5916 win = 0;
5917 else
5918 for (i = 0; i < XVECLEN (tmp, 0); i++)
5920 rtx insn = XVECEXP (tmp, 0, i);
5922 if (GET_CODE (insn) != INSN
5923 || (GET_CODE (PATTERN (insn)) == SET
5924 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
5925 || (GET_CODE (PATTERN (insn)) == PARALLEL
5926 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
5927 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
5929 win = 0;
5930 break;
5934 else if (GET_CODE (tmp) == SET
5935 && GET_CODE (SET_SRC (tmp)) == MULT)
5936 win = 0;
5937 else if (GET_CODE (tmp) == PARALLEL
5938 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
5939 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
5940 win = 0;
5942 /* Free any storage we obtained in generating this multiply and restore rtl
5943 allocation to its normal obstack. */
5944 obstack_free (&temp_obstack, storage);
5945 rtl_obstack = old_rtl_obstack;
5947 return win;
5950 /* Check to see if loop can be terminated by a "decrement and branch until
5951 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
5952 Also try reversing an increment loop to a decrement loop
5953 to see if the optimization can be performed.
5954 Value is nonzero if optimization was performed. */
5956 /* This is useful even if the architecture doesn't have such an insn,
5957 because it might change a loops which increments from 0 to n to a loop
5958 which decrements from n to 0. A loop that decrements to zero is usually
5959 faster than one that increments from zero. */
5961 /* ??? This could be rewritten to use some of the loop unrolling procedures,
5962 such as approx_final_value, biv_total_increment, loop_iterations, and
5963 final_[bg]iv_value. */
5965 static int
5966 check_dbra_loop (loop_end, insn_count, loop_start)
5967 rtx loop_end;
5968 int insn_count;
5969 rtx loop_start;
5971 struct iv_class *bl;
5972 rtx reg;
5973 rtx jump_label;
5974 rtx final_value;
5975 rtx start_value;
5976 rtx new_add_val;
5977 rtx comparison;
5978 rtx before_comparison;
5979 rtx p;
5981 /* If last insn is a conditional branch, and the insn before tests a
5982 register value, try to optimize it. Otherwise, we can't do anything. */
5984 comparison = get_condition_for_loop (PREV_INSN (loop_end));
5985 if (comparison == 0)
5986 return 0;
5988 /* Check all of the bivs to see if the compare uses one of them.
5989 Skip biv's set more than once because we can't guarantee that
5990 it will be zero on the last iteration. Also skip if the biv is
5991 used between its update and the test insn. */
5993 for (bl = loop_iv_list; bl; bl = bl->next)
5995 if (bl->biv_count == 1
5996 && bl->biv->dest_reg == XEXP (comparison, 0)
5997 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
5998 PREV_INSN (PREV_INSN (loop_end))))
5999 break;
6002 if (! bl)
6003 return 0;
6005 /* Look for the case where the basic induction variable is always
6006 nonnegative, and equals zero on the last iteration.
6007 In this case, add a reg_note REG_NONNEG, which allows the
6008 m68k DBRA instruction to be used. */
6010 if (((GET_CODE (comparison) == GT
6011 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6012 && INTVAL (XEXP (comparison, 1)) == -1)
6013 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6014 && GET_CODE (bl->biv->add_val) == CONST_INT
6015 && INTVAL (bl->biv->add_val) < 0)
6017 /* Initial value must be greater than 0,
6018 init_val % -dec_value == 0 to ensure that it equals zero on
6019 the last iteration */
6021 if (GET_CODE (bl->initial_value) == CONST_INT
6022 && INTVAL (bl->initial_value) > 0
6023 && (INTVAL (bl->initial_value)
6024 % (-INTVAL (bl->biv->add_val))) == 0)
6026 /* register always nonnegative, add REG_NOTE to branch */
6027 REG_NOTES (PREV_INSN (loop_end))
6028 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6029 REG_NOTES (PREV_INSN (loop_end)));
6030 bl->nonneg = 1;
6032 return 1;
6035 /* If the decrement is 1 and the value was tested as >= 0 before
6036 the loop, then we can safely optimize. */
6037 for (p = loop_start; p; p = PREV_INSN (p))
6039 if (GET_CODE (p) == CODE_LABEL)
6040 break;
6041 if (GET_CODE (p) != JUMP_INSN)
6042 continue;
6044 before_comparison = get_condition_for_loop (p);
6045 if (before_comparison
6046 && XEXP (before_comparison, 0) == bl->biv->dest_reg
6047 && GET_CODE (before_comparison) == LT
6048 && XEXP (before_comparison, 1) == const0_rtx
6049 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6050 && INTVAL (bl->biv->add_val) == -1)
6052 REG_NOTES (PREV_INSN (loop_end))
6053 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6054 REG_NOTES (PREV_INSN (loop_end)));
6055 bl->nonneg = 1;
6057 return 1;
6061 else if (num_mem_sets <= 1)
6063 /* Try to change inc to dec, so can apply above optimization. */
6064 /* Can do this if:
6065 all registers modified are induction variables or invariant,
6066 all memory references have non-overlapping addresses
6067 (obviously true if only one write)
6068 allow 2 insns for the compare/jump at the end of the loop. */
6069 /* Also, we must avoid any instructions which use both the reversed
6070 biv and another biv. Such instructions will fail if the loop is
6071 reversed. We meet this condition by requiring that either
6072 no_use_except_counting is true, or else that there is only
6073 one biv. */
6074 int num_nonfixed_reads = 0;
6075 /* 1 if the iteration var is used only to count iterations. */
6076 int no_use_except_counting = 0;
6077 /* 1 if the loop has no memory store, or it has a single memory store
6078 which is reversible. */
6079 int reversible_mem_store = 1;
6081 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6082 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6083 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6085 if (bl->giv_count == 0
6086 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6088 rtx bivreg = regno_reg_rtx[bl->regno];
6090 /* If there are no givs for this biv, and the only exit is the
6091 fall through at the end of the the loop, then
6092 see if perhaps there are no uses except to count. */
6093 no_use_except_counting = 1;
6094 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6095 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6097 rtx set = single_set (p);
6099 if (set && GET_CODE (SET_DEST (set)) == REG
6100 && REGNO (SET_DEST (set)) == bl->regno)
6101 /* An insn that sets the biv is okay. */
6103 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6104 || p == prev_nonnote_insn (loop_end))
6105 /* Don't bother about the end test. */
6107 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6108 /* Any other use of the biv is no good. */
6110 no_use_except_counting = 0;
6111 break;
6116 /* If the loop has a single store, and the destination address is
6117 invariant, then we can't reverse the loop, because this address
6118 might then have the wrong value at loop exit.
6119 This would work if the source was invariant also, however, in that
6120 case, the insn should have been moved out of the loop. */
6122 if (num_mem_sets == 1)
6123 reversible_mem_store
6124 = (! unknown_address_altered
6125 && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6127 /* This code only acts for innermost loops. Also it simplifies
6128 the memory address check by only reversing loops with
6129 zero or one memory access.
6130 Two memory accesses could involve parts of the same array,
6131 and that can't be reversed. */
6133 if (num_nonfixed_reads <= 1
6134 && !loop_has_call
6135 && !loop_has_volatile
6136 && reversible_mem_store
6137 && (no_use_except_counting
6138 || ((bl->giv_count + bl->biv_count + num_mem_sets
6139 + num_movables + 2 == insn_count)
6140 && (bl == loop_iv_list && bl->next == 0))))
6142 rtx tem;
6144 /* Loop can be reversed. */
6145 if (loop_dump_stream)
6146 fprintf (loop_dump_stream, "Can reverse loop\n");
6148 /* Now check other conditions:
6150 The increment must be a constant, as must the initial value,
6151 and the comparison code must be LT.
6153 This test can probably be improved since +/- 1 in the constant
6154 can be obtained by changing LT to LE and vice versa; this is
6155 confusing. */
6157 if (comparison
6158 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6159 /* LE gets turned into LT */
6160 && GET_CODE (comparison) == LT
6161 && GET_CODE (bl->initial_value) == CONST_INT)
6163 HOST_WIDE_INT add_val, comparison_val;
6164 rtx initial_value;
6166 add_val = INTVAL (bl->biv->add_val);
6167 comparison_val = INTVAL (XEXP (comparison, 1));
6168 initial_value = bl->initial_value;
6170 /* Normalize the initial value if it has no other use
6171 except as a counter. This will allow a few more loops
6172 to be reversed. */
6173 if (no_use_except_counting)
6175 comparison_val = comparison_val - INTVAL (bl->initial_value);
6176 initial_value = const0_rtx;
6179 /* If the initial value is not zero, or if the comparison
6180 value is not an exact multiple of the increment, then we
6181 can not reverse this loop. */
6182 if (initial_value != const0_rtx
6183 || (comparison_val % add_val) != 0)
6184 return 0;
6186 /* Reset these in case we normalized the initial value
6187 and comparison value above. */
6188 bl->initial_value = initial_value;
6189 XEXP (comparison, 1) = GEN_INT (comparison_val);
6191 /* Register will always be nonnegative, with value
6192 0 on last iteration if loop reversed */
6194 /* Save some info needed to produce the new insns. */
6195 reg = bl->biv->dest_reg;
6196 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6197 if (jump_label == pc_rtx)
6198 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6199 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6201 final_value = XEXP (comparison, 1);
6202 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6203 - INTVAL (bl->biv->add_val));
6205 /* Initialize biv to start_value before loop start.
6206 The old initializing insn will be deleted as a
6207 dead store by flow.c. */
6208 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6210 /* Add insn to decrement register, and delete insn
6211 that incremented the register. */
6212 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6213 bl->biv->insn);
6214 delete_insn (bl->biv->insn);
6216 /* Update biv info to reflect its new status. */
6217 bl->biv->insn = p;
6218 bl->initial_value = start_value;
6219 bl->biv->add_val = new_add_val;
6221 /* Inc LABEL_NUSES so that delete_insn will
6222 not delete the label. */
6223 LABEL_NUSES (XEXP (jump_label, 0)) ++;
6225 /* Emit an insn after the end of the loop to set the biv's
6226 proper exit value if it is used anywhere outside the loop. */
6227 if ((REGNO_LAST_UID (bl->regno)
6228 != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
6229 || ! bl->init_insn
6230 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6231 emit_insn_after (gen_move_insn (reg, final_value),
6232 loop_end);
6234 /* Delete compare/branch at end of loop. */
6235 delete_insn (PREV_INSN (loop_end));
6236 delete_insn (PREV_INSN (loop_end));
6238 /* Add new compare/branch insn at end of loop. */
6239 start_sequence ();
6240 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6241 GET_MODE (reg), 0, 0);
6242 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6243 tem = gen_sequence ();
6244 end_sequence ();
6245 emit_jump_insn_before (tem, loop_end);
6247 for (tem = PREV_INSN (loop_end);
6248 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6250 if (tem)
6252 JUMP_LABEL (tem) = XEXP (jump_label, 0);
6254 /* Increment of LABEL_NUSES done above. */
6255 /* Register is now always nonnegative,
6256 so add REG_NONNEG note to the branch. */
6257 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
6258 REG_NOTES (tem));
6261 bl->nonneg = 1;
6263 /* Mark that this biv has been reversed. Each giv which depends
6264 on this biv, and which is also live past the end of the loop
6265 will have to be fixed up. */
6267 bl->reversed = 1;
6269 if (loop_dump_stream)
6270 fprintf (loop_dump_stream,
6271 "Reversed loop and added reg_nonneg\n");
6273 return 1;
6278 return 0;
6281 /* Verify whether the biv BL appears to be eliminable,
6282 based on the insns in the loop that refer to it.
6283 LOOP_START is the first insn of the loop, and END is the end insn.
6285 If ELIMINATE_P is non-zero, actually do the elimination.
6287 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6288 determine whether invariant insns should be placed inside or at the
6289 start of the loop. */
6291 static int
6292 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6293 struct iv_class *bl;
6294 rtx loop_start;
6295 rtx end;
6296 int eliminate_p;
6297 int threshold, insn_count;
6299 rtx reg = bl->biv->dest_reg;
6300 rtx p;
6302 /* Scan all insns in the loop, stopping if we find one that uses the
6303 biv in a way that we cannot eliminate. */
6305 for (p = loop_start; p != end; p = NEXT_INSN (p))
6307 enum rtx_code code = GET_CODE (p);
6308 rtx where = threshold >= insn_count ? loop_start : p;
6310 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6311 && reg_mentioned_p (reg, PATTERN (p))
6312 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6314 if (loop_dump_stream)
6315 fprintf (loop_dump_stream,
6316 "Cannot eliminate biv %d: biv used in insn %d.\n",
6317 bl->regno, INSN_UID (p));
6318 break;
6322 if (p == end)
6324 if (loop_dump_stream)
6325 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6326 bl->regno, eliminate_p ? "was" : "can be");
6327 return 1;
6330 return 0;
6333 /* If BL appears in X (part of the pattern of INSN), see if we can
6334 eliminate its use. If so, return 1. If not, return 0.
6336 If BIV does not appear in X, return 1.
6338 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
6339 where extra insns should be added. Depending on how many items have been
6340 moved out of the loop, it will either be before INSN or at the start of
6341 the loop. */
6343 static int
6344 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6345 rtx x, insn;
6346 struct iv_class *bl;
6347 int eliminate_p;
6348 rtx where;
6350 enum rtx_code code = GET_CODE (x);
6351 rtx reg = bl->biv->dest_reg;
6352 enum machine_mode mode = GET_MODE (reg);
6353 struct induction *v;
6354 rtx arg, new, tem;
6355 int arg_operand;
6356 char *fmt;
6357 int i, j;
6359 switch (code)
6361 case REG:
6362 /* If we haven't already been able to do something with this BIV,
6363 we can't eliminate it. */
6364 if (x == reg)
6365 return 0;
6366 return 1;
6368 case SET:
6369 /* If this sets the BIV, it is not a problem. */
6370 if (SET_DEST (x) == reg)
6371 return 1;
6373 /* If this is an insn that defines a giv, it is also ok because
6374 it will go away when the giv is reduced. */
6375 for (v = bl->giv; v; v = v->next_iv)
6376 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6377 return 1;
6379 #ifdef HAVE_cc0
6380 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6382 /* Can replace with any giv that was reduced and
6383 that has (MULT_VAL != 0) and (ADD_VAL == 0).
6384 Require a constant for MULT_VAL, so we know it's nonzero.
6385 ??? We disable this optimization to avoid potential
6386 overflows. */
6388 for (v = bl->giv; v; v = v->next_iv)
6389 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6390 && v->add_val == const0_rtx
6391 && ! v->ignore && ! v->maybe_dead && v->always_computable
6392 && v->mode == mode
6393 && 0)
6395 /* If the giv V had the auto-inc address optimization applied
6396 to it, and INSN occurs between the giv insn and the biv
6397 insn, then we must adjust the value used here.
6398 This is rare, so we don't bother to do so. */
6399 if (v->auto_inc_opt
6400 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6401 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6402 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6403 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6404 continue;
6406 if (! eliminate_p)
6407 return 1;
6409 /* If the giv has the opposite direction of change,
6410 then reverse the comparison. */
6411 if (INTVAL (v->mult_val) < 0)
6412 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
6413 const0_rtx, v->new_reg);
6414 else
6415 new = v->new_reg;
6417 /* We can probably test that giv's reduced reg. */
6418 if (validate_change (insn, &SET_SRC (x), new, 0))
6419 return 1;
6422 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6423 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6424 Require a constant for MULT_VAL, so we know it's nonzero.
6425 ??? Do this only if ADD_VAL is a pointer to avoid a potential
6426 overflow problem. */
6428 for (v = bl->giv; v; v = v->next_iv)
6429 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6430 && ! v->ignore && ! v->maybe_dead && v->always_computable
6431 && v->mode == mode
6432 && (GET_CODE (v->add_val) == SYMBOL_REF
6433 || GET_CODE (v->add_val) == LABEL_REF
6434 || GET_CODE (v->add_val) == CONST
6435 || (GET_CODE (v->add_val) == REG
6436 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6438 /* If the giv V had the auto-inc address optimization applied
6439 to it, and INSN occurs between the giv insn and the biv
6440 insn, then we must adjust the value used here.
6441 This is rare, so we don't bother to do so. */
6442 if (v->auto_inc_opt
6443 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6444 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6445 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6446 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6447 continue;
6449 if (! eliminate_p)
6450 return 1;
6452 /* If the giv has the opposite direction of change,
6453 then reverse the comparison. */
6454 if (INTVAL (v->mult_val) < 0)
6455 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
6456 v->new_reg);
6457 else
6458 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
6459 copy_rtx (v->add_val));
6461 /* Replace biv with the giv's reduced register. */
6462 update_reg_last_use (v->add_val, insn);
6463 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6464 return 1;
6466 /* Insn doesn't support that constant or invariant. Copy it
6467 into a register (it will be a loop invariant.) */
6468 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6470 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6471 where);
6473 /* Substitute the new register for its invariant value in
6474 the compare expression. */
6475 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6476 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6477 return 1;
6480 #endif
6481 break;
6483 case COMPARE:
6484 case EQ: case NE:
6485 case GT: case GE: case GTU: case GEU:
6486 case LT: case LE: case LTU: case LEU:
6487 /* See if either argument is the biv. */
6488 if (XEXP (x, 0) == reg)
6489 arg = XEXP (x, 1), arg_operand = 1;
6490 else if (XEXP (x, 1) == reg)
6491 arg = XEXP (x, 0), arg_operand = 0;
6492 else
6493 break;
6495 if (CONSTANT_P (arg))
6497 /* First try to replace with any giv that has constant positive
6498 mult_val and constant add_val. We might be able to support
6499 negative mult_val, but it seems complex to do it in general. */
6501 for (v = bl->giv; v; v = v->next_iv)
6502 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6503 && (GET_CODE (v->add_val) == SYMBOL_REF
6504 || GET_CODE (v->add_val) == LABEL_REF
6505 || GET_CODE (v->add_val) == CONST
6506 || (GET_CODE (v->add_val) == REG
6507 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6508 && ! v->ignore && ! v->maybe_dead && v->always_computable
6509 && v->mode == mode)
6511 /* If the giv V had the auto-inc address optimization applied
6512 to it, and INSN occurs between the giv insn and the biv
6513 insn, then we must adjust the value used here.
6514 This is rare, so we don't bother to do so. */
6515 if (v->auto_inc_opt
6516 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6517 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6518 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6519 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6520 continue;
6522 if (! eliminate_p)
6523 return 1;
6525 /* Replace biv with the giv's reduced reg. */
6526 XEXP (x, 1-arg_operand) = v->new_reg;
6528 /* If all constants are actually constant integers and
6529 the derived constant can be directly placed in the COMPARE,
6530 do so. */
6531 if (GET_CODE (arg) == CONST_INT
6532 && GET_CODE (v->mult_val) == CONST_INT
6533 && GET_CODE (v->add_val) == CONST_INT
6534 && validate_change (insn, &XEXP (x, arg_operand),
6535 GEN_INT (INTVAL (arg)
6536 * INTVAL (v->mult_val)
6537 + INTVAL (v->add_val)), 0))
6538 return 1;
6540 /* Otherwise, load it into a register. */
6541 tem = gen_reg_rtx (mode);
6542 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6543 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6544 return 1;
6546 /* If that failed, put back the change we made above. */
6547 XEXP (x, 1-arg_operand) = reg;
6550 /* Look for giv with positive constant mult_val and nonconst add_val.
6551 Insert insns to calculate new compare value.
6552 ??? Turn this off due to possible overflow. */
6554 for (v = bl->giv; v; v = v->next_iv)
6555 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6556 && ! v->ignore && ! v->maybe_dead && v->always_computable
6557 && v->mode == mode
6558 && 0)
6560 rtx tem;
6562 /* If the giv V had the auto-inc address optimization applied
6563 to it, and INSN occurs between the giv insn and the biv
6564 insn, then we must adjust the value used here.
6565 This is rare, so we don't bother to do so. */
6566 if (v->auto_inc_opt
6567 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6568 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6569 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6570 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6571 continue;
6573 if (! eliminate_p)
6574 return 1;
6576 tem = gen_reg_rtx (mode);
6578 /* Replace biv with giv's reduced register. */
6579 validate_change (insn, &XEXP (x, 1 - arg_operand),
6580 v->new_reg, 1);
6582 /* Compute value to compare against. */
6583 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6584 /* Use it in this insn. */
6585 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6586 if (apply_change_group ())
6587 return 1;
6590 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6592 if (invariant_p (arg) == 1)
6594 /* Look for giv with constant positive mult_val and nonconst
6595 add_val. Insert insns to compute new compare value.
6596 ??? Turn this off due to possible overflow. */
6598 for (v = bl->giv; v; v = v->next_iv)
6599 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6600 && ! v->ignore && ! v->maybe_dead && v->always_computable
6601 && v->mode == mode
6602 && 0)
6604 rtx tem;
6606 /* If the giv V had the auto-inc address optimization applied
6607 to it, and INSN occurs between the giv insn and the biv
6608 insn, then we must adjust the value used here.
6609 This is rare, so we don't bother to do so. */
6610 if (v->auto_inc_opt
6611 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6612 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6613 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6614 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6615 continue;
6617 if (! eliminate_p)
6618 return 1;
6620 tem = gen_reg_rtx (mode);
6622 /* Replace biv with giv's reduced register. */
6623 validate_change (insn, &XEXP (x, 1 - arg_operand),
6624 v->new_reg, 1);
6626 /* Compute value to compare against. */
6627 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6628 tem, where);
6629 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6630 if (apply_change_group ())
6631 return 1;
6635 /* This code has problems. Basically, you can't know when
6636 seeing if we will eliminate BL, whether a particular giv
6637 of ARG will be reduced. If it isn't going to be reduced,
6638 we can't eliminate BL. We can try forcing it to be reduced,
6639 but that can generate poor code.
6641 The problem is that the benefit of reducing TV, below should
6642 be increased if BL can actually be eliminated, but this means
6643 we might have to do a topological sort of the order in which
6644 we try to process biv. It doesn't seem worthwhile to do
6645 this sort of thing now. */
6647 #if 0
6648 /* Otherwise the reg compared with had better be a biv. */
6649 if (GET_CODE (arg) != REG
6650 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6651 return 0;
6653 /* Look for a pair of givs, one for each biv,
6654 with identical coefficients. */
6655 for (v = bl->giv; v; v = v->next_iv)
6657 struct induction *tv;
6659 if (v->ignore || v->maybe_dead || v->mode != mode)
6660 continue;
6662 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6663 if (! tv->ignore && ! tv->maybe_dead
6664 && rtx_equal_p (tv->mult_val, v->mult_val)
6665 && rtx_equal_p (tv->add_val, v->add_val)
6666 && tv->mode == mode)
6668 /* If the giv V had the auto-inc address optimization applied
6669 to it, and INSN occurs between the giv insn and the biv
6670 insn, then we must adjust the value used here.
6671 This is rare, so we don't bother to do so. */
6672 if (v->auto_inc_opt
6673 && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6674 && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6675 || (INSN_LUID (v->insn) > INSN_LUID (insn)
6676 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6677 continue;
6679 if (! eliminate_p)
6680 return 1;
6682 /* Replace biv with its giv's reduced reg. */
6683 XEXP (x, 1-arg_operand) = v->new_reg;
6684 /* Replace other operand with the other giv's
6685 reduced reg. */
6686 XEXP (x, arg_operand) = tv->new_reg;
6687 return 1;
6690 #endif
6693 /* If we get here, the biv can't be eliminated. */
6694 return 0;
6696 case MEM:
6697 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6698 biv is used in it, since it will be replaced. */
6699 for (v = bl->giv; v; v = v->next_iv)
6700 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6701 return 1;
6702 break;
6704 default:
6705 break;
6708 /* See if any subexpression fails elimination. */
6709 fmt = GET_RTX_FORMAT (code);
6710 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6712 switch (fmt[i])
6714 case 'e':
6715 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6716 eliminate_p, where))
6717 return 0;
6718 break;
6720 case 'E':
6721 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6722 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6723 eliminate_p, where))
6724 return 0;
6725 break;
6729 return 1;
6732 /* Return nonzero if the last use of REG
6733 is in an insn following INSN in the same basic block. */
6735 static int
6736 last_use_this_basic_block (reg, insn)
6737 rtx reg;
6738 rtx insn;
6740 rtx n;
6741 for (n = insn;
6742 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6743 n = NEXT_INSN (n))
6745 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6746 return 1;
6748 return 0;
6751 /* Called via `note_stores' to record the initial value of a biv. Here we
6752 just record the location of the set and process it later. */
6754 static void
6755 record_initial (dest, set)
6756 rtx dest;
6757 rtx set;
6759 struct iv_class *bl;
6761 if (GET_CODE (dest) != REG
6762 || REGNO (dest) >= max_reg_before_loop
6763 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6764 return;
6766 bl = reg_biv_class[REGNO (dest)];
6768 /* If this is the first set found, record it. */
6769 if (bl->init_insn == 0)
6771 bl->init_insn = note_insn;
6772 bl->init_set = set;
6776 /* If any of the registers in X are "old" and currently have a last use earlier
6777 than INSN, update them to have a last use of INSN. Their actual last use
6778 will be the previous insn but it will not have a valid uid_luid so we can't
6779 use it. */
6781 static void
6782 update_reg_last_use (x, insn)
6783 rtx x;
6784 rtx insn;
6786 /* Check for the case where INSN does not have a valid luid. In this case,
6787 there is no need to modify the regno_last_uid, as this can only happen
6788 when code is inserted after the loop_end to set a pseudo's final value,
6789 and hence this insn will never be the last use of x. */
6790 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6791 && INSN_UID (insn) < max_uid_for_loop
6792 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6793 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6794 else
6796 register int i, j;
6797 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6798 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6800 if (fmt[i] == 'e')
6801 update_reg_last_use (XEXP (x, i), insn);
6802 else if (fmt[i] == 'E')
6803 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6804 update_reg_last_use (XVECEXP (x, i, j), insn);
6809 /* Given a jump insn JUMP, return the condition that will cause it to branch
6810 to its JUMP_LABEL. If the condition cannot be understood, or is an
6811 inequality floating-point comparison which needs to be reversed, 0 will
6812 be returned.
6814 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6815 insn used in locating the condition was found. If a replacement test
6816 of the condition is desired, it should be placed in front of that
6817 insn and we will be sure that the inputs are still valid.
6819 The condition will be returned in a canonical form to simplify testing by
6820 callers. Specifically:
6822 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6823 (2) Both operands will be machine operands; (cc0) will have been replaced.
6824 (3) If an operand is a constant, it will be the second operand.
6825 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6826 for GE, GEU, and LEU. */
6829 get_condition (jump, earliest)
6830 rtx jump;
6831 rtx *earliest;
6833 enum rtx_code code;
6834 rtx prev = jump;
6835 rtx set;
6836 rtx tem;
6837 rtx op0, op1;
6838 int reverse_code = 0;
6839 int did_reverse_condition = 0;
6841 /* If this is not a standard conditional jump, we can't parse it. */
6842 if (GET_CODE (jump) != JUMP_INSN
6843 || ! condjump_p (jump) || simplejump_p (jump))
6844 return 0;
6846 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6847 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6848 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6850 if (earliest)
6851 *earliest = jump;
6853 /* If this branches to JUMP_LABEL when the condition is false, reverse
6854 the condition. */
6855 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6856 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
6857 code = reverse_condition (code), did_reverse_condition ^= 1;
6859 /* If we are comparing a register with zero, see if the register is set
6860 in the previous insn to a COMPARE or a comparison operation. Perform
6861 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6862 in cse.c */
6864 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
6866 /* Set non-zero when we find something of interest. */
6867 rtx x = 0;
6869 #ifdef HAVE_cc0
6870 /* If comparison with cc0, import actual comparison from compare
6871 insn. */
6872 if (op0 == cc0_rtx)
6874 if ((prev = prev_nonnote_insn (prev)) == 0
6875 || GET_CODE (prev) != INSN
6876 || (set = single_set (prev)) == 0
6877 || SET_DEST (set) != cc0_rtx)
6878 return 0;
6880 op0 = SET_SRC (set);
6881 op1 = CONST0_RTX (GET_MODE (op0));
6882 if (earliest)
6883 *earliest = prev;
6885 #endif
6887 /* If this is a COMPARE, pick up the two things being compared. */
6888 if (GET_CODE (op0) == COMPARE)
6890 op1 = XEXP (op0, 1);
6891 op0 = XEXP (op0, 0);
6892 continue;
6894 else if (GET_CODE (op0) != REG)
6895 break;
6897 /* Go back to the previous insn. Stop if it is not an INSN. We also
6898 stop if it isn't a single set or if it has a REG_INC note because
6899 we don't want to bother dealing with it. */
6901 if ((prev = prev_nonnote_insn (prev)) == 0
6902 || GET_CODE (prev) != INSN
6903 || FIND_REG_INC_NOTE (prev, 0)
6904 || (set = single_set (prev)) == 0)
6905 break;
6907 /* If this is setting OP0, get what it sets it to if it looks
6908 relevant. */
6909 if (rtx_equal_p (SET_DEST (set), op0))
6911 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
6913 if ((GET_CODE (SET_SRC (set)) == COMPARE
6914 || (((code == NE
6915 || (code == LT
6916 && GET_MODE_CLASS (inner_mode) == MODE_INT
6917 && (GET_MODE_BITSIZE (inner_mode)
6918 <= HOST_BITS_PER_WIDE_INT)
6919 && (STORE_FLAG_VALUE
6920 & ((HOST_WIDE_INT) 1
6921 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6922 #ifdef FLOAT_STORE_FLAG_VALUE
6923 || (code == LT
6924 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6925 && FLOAT_STORE_FLAG_VALUE < 0)
6926 #endif
6928 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
6929 x = SET_SRC (set);
6930 else if (((code == EQ
6931 || (code == GE
6932 && (GET_MODE_BITSIZE (inner_mode)
6933 <= HOST_BITS_PER_WIDE_INT)
6934 && GET_MODE_CLASS (inner_mode) == MODE_INT
6935 && (STORE_FLAG_VALUE
6936 & ((HOST_WIDE_INT) 1
6937 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6938 #ifdef FLOAT_STORE_FLAG_VALUE
6939 || (code == GE
6940 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6941 && FLOAT_STORE_FLAG_VALUE < 0)
6942 #endif
6944 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
6946 /* We might have reversed a LT to get a GE here. But this wasn't
6947 actually the comparison of data, so we don't flag that we
6948 have had to reverse the condition. */
6949 did_reverse_condition ^= 1;
6950 reverse_code = 1;
6951 x = SET_SRC (set);
6953 else
6954 break;
6957 else if (reg_set_p (op0, prev))
6958 /* If this sets OP0, but not directly, we have to give up. */
6959 break;
6961 if (x)
6963 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
6964 code = GET_CODE (x);
6965 if (reverse_code)
6967 code = reverse_condition (code);
6968 did_reverse_condition ^= 1;
6969 reverse_code = 0;
6972 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
6973 if (earliest)
6974 *earliest = prev;
6978 /* If constant is first, put it last. */
6979 if (CONSTANT_P (op0))
6980 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
6982 /* If OP0 is the result of a comparison, we weren't able to find what
6983 was really being compared, so fail. */
6984 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6985 return 0;
6987 /* Canonicalize any ordered comparison with integers involving equality
6988 if we can do computations in the relevant mode and we do not
6989 overflow. */
6991 if (GET_CODE (op1) == CONST_INT
6992 && GET_MODE (op0) != VOIDmode
6993 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
6995 HOST_WIDE_INT const_val = INTVAL (op1);
6996 unsigned HOST_WIDE_INT uconst_val = const_val;
6997 unsigned HOST_WIDE_INT max_val
6998 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
7000 switch (code)
7002 case LE:
7003 if (const_val != max_val >> 1)
7004 code = LT, op1 = GEN_INT (const_val + 1);
7005 break;
7007 case GE:
7008 if (const_val
7009 != (((HOST_WIDE_INT) 1
7010 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7011 code = GT, op1 = GEN_INT (const_val - 1);
7012 break;
7014 case LEU:
7015 if (uconst_val != max_val)
7016 code = LTU, op1 = GEN_INT (uconst_val + 1);
7017 break;
7019 case GEU:
7020 if (uconst_val != 0)
7021 code = GTU, op1 = GEN_INT (uconst_val - 1);
7022 break;
7024 default:
7025 break;
7029 /* If this was floating-point and we reversed anything other than an
7030 EQ or NE, return zero. */
7031 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7032 && did_reverse_condition && code != NE && code != EQ
7033 && ! flag_fast_math
7034 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7035 return 0;
7037 #ifdef HAVE_cc0
7038 /* Never return CC0; return zero instead. */
7039 if (op0 == cc0_rtx)
7040 return 0;
7041 #endif
7043 return gen_rtx (code, VOIDmode, op0, op1);
7046 /* Similar to above routine, except that we also put an invariant last
7047 unless both operands are invariants. */
7050 get_condition_for_loop (x)
7051 rtx x;
7053 rtx comparison = get_condition (x, NULL_PTR);
7055 if (comparison == 0
7056 || ! invariant_p (XEXP (comparison, 0))
7057 || invariant_p (XEXP (comparison, 1)))
7058 return comparison;
7060 return gen_rtx (swap_condition (GET_CODE (comparison)), VOIDmode,
7061 XEXP (comparison, 1), XEXP (comparison, 0));