]> gcc.gnu.org Git - gcc.git/blame - gcc/loop.c
(call_insn_operand): New funcion.
[gcc.git] / gcc / loop.c
CommitLineData
b4ad7b23 1/* Move constant computations out of loops.
5fd8383e 2 Copyright (C) 1987, 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
b4ad7b23
RS
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* This is the loop optimization pass of the compiler.
22 It finds invariant computations within loops and moves them
23 to the beginning of the loop. Then it identifies basic and
24 general induction variables. Strength reduction is applied to the general
25 induction variables, and induction variable elimination is applied to
26 the basic induction variables.
27
28 It also finds cases where
29 a register is set within the loop by zero-extending a narrower value
30 and changes these to zero the entire register once before the loop
31 and merely copy the low part within the loop.
32
33 Most of the complexity is in heuristics to decide when it is worth
34 while to do these things. */
35
ff2da9fc 36#include <stdio.h>
b4ad7b23
RS
37#include "config.h"
38#include "rtl.h"
39#include "obstack.h"
40#include "expr.h"
41#include "insn-config.h"
42#include "insn-flags.h"
43#include "regs.h"
44#include "hard-reg-set.h"
45#include "recog.h"
46#include "flags.h"
47#include "real.h"
b4ad7b23
RS
48#include "loop.h"
49
50/* Vector mapping INSN_UIDs to luids.
d45cf215 51 The luids are like uids but increase monotonically always.
b4ad7b23
RS
52 We use them to see whether a jump comes from outside a given loop. */
53
54int *uid_luid;
55
56/* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
57 number the insn is contained in. */
58
59int *uid_loop_num;
60
61/* 1 + largest uid of any insn. */
62
63int max_uid_for_loop;
64
65/* 1 + luid of last insn. */
66
67static int max_luid;
68
69/* Number of loops detected in current function. Used as index to the
70 next few tables. */
71
72static int max_loop_num;
73
74/* Indexed by loop number, contains the first and last insn of each loop. */
75
76static rtx *loop_number_loop_starts, *loop_number_loop_ends;
77
78/* For each loop, gives the containing loop number, -1 if none. */
79
80int *loop_outer_loop;
81
82/* Indexed by loop number, contains a nonzero value if the "loop" isn't
83 really a loop (an insn outside the loop branches into it). */
84
85static char *loop_invalid;
86
87/* Indexed by loop number, links together all LABEL_REFs which refer to
88 code labels outside the loop. Used by routines that need to know all
89 loop exits, such as final_biv_value and final_giv_value.
90
91 This does not include loop exits due to return instructions. This is
92 because all bivs and givs are pseudos, and hence must be dead after a
93 return, so the presense of a return does not affect any of the
94 optimizations that use this info. It is simpler to just not include return
95 instructions on this list. */
96
97rtx *loop_number_exit_labels;
98
99/* Holds the number of loop iterations. It is zero if the number could not be
5fd8383e
RK
100 calculated. Must be unsigned since the number of iterations can
101 be as high as 2^wordsize-1. For loops with a wider iterator, this number
102 will will be zero if the number of loop iterations is too large for an
103 unsigned integer to hold. */
b4ad7b23 104
5fd8383e 105unsigned HOST_WIDE_INT loop_n_iterations;
b4ad7b23
RS
106
107/* Nonzero if there is a subroutine call in the current loop.
108 (unknown_address_altered is also nonzero in this case.) */
109
110static int loop_has_call;
111
552bc76f
RS
112/* Nonzero if there is a volatile memory reference in the current
113 loop. */
114
115static int loop_has_volatile;
116
b4ad7b23
RS
117/* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
118 current loop. A continue statement will generate a branch to
119 NEXT_INSN (loop_continue). */
120
121static rtx loop_continue;
122
123/* Indexed by register number, contains the number of times the reg
124 is set during the loop being scanned.
125 During code motion, a negative value indicates a reg that has been
126 made a candidate; in particular -2 means that it is an candidate that
c5b7917e 127 we know is equal to a constant and -1 means that it is an candidate
b4ad7b23
RS
128 not known equal to a constant.
129 After code motion, regs moved have 0 (which is accurate now)
130 while the failed candidates have the original number of times set.
131
132 Therefore, at all times, == 0 indicates an invariant register;
133 < 0 a conditionally invariant one. */
134
135static short *n_times_set;
136
137/* Original value of n_times_set; same except that this value
138 is not set negative for a reg whose sets have been made candidates
139 and not set to 0 for a reg that is moved. */
140
141static short *n_times_used;
142
143/* Index by register number, 1 indicates that the register
144 cannot be moved or strength reduced. */
145
146static char *may_not_optimize;
147
148/* Nonzero means reg N has already been moved out of one loop.
149 This reduces the desire to move it out of another. */
150
151static char *moved_once;
152
153/* Array of MEMs that are stored in this loop. If there are too many to fit
154 here, we just turn on unknown_address_altered. */
155
156#define NUM_STORES 20
157static rtx loop_store_mems[NUM_STORES];
158
159/* Index of first available slot in above array. */
160static int loop_store_mems_idx;
161
162/* Nonzero if we don't know what MEMs were changed in the current loop.
552bc76f 163 This happens if the loop contains a call (in which case `loop_has_call'
b4ad7b23
RS
164 will also be set) or if we store into more than NUM_STORES MEMs. */
165
166static int unknown_address_altered;
167
168/* Count of movable (i.e. invariant) instructions discovered in the loop. */
169static int num_movables;
170
171/* Count of memory write instructions discovered in the loop. */
172static int num_mem_sets;
173
174/* Number of loops contained within the current one, including itself. */
175static int loops_enclosed;
176
177/* Bound on pseudo register number before loop optimization.
178 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
179int max_reg_before_loop;
180
181/* This obstack is used in product_cheap_p to allocate its rtl. It
182 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
183 If we used the same obstack that it did, we would be deallocating
184 that array. */
185
186static struct obstack temp_obstack;
187
188/* This is where the pointer to the obstack being used for RTL is stored. */
189
190extern struct obstack *rtl_obstack;
191
192#define obstack_chunk_alloc xmalloc
193#define obstack_chunk_free free
194
195extern char *oballoc ();
b4ad7b23
RS
196\f
197/* During the analysis of a loop, a chain of `struct movable's
198 is made to record all the movable insns found.
199 Then the entire chain can be scanned to decide which to move. */
200
201struct movable
202{
203 rtx insn; /* A movable insn */
204 rtx set_src; /* The expression this reg is set from. */
205 rtx set_dest; /* The destination of this SET. */
206 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
207 of any registers used within the LIBCALL. */
208 int consec; /* Number of consecutive following insns
209 that must be moved with this one. */
210 int regno; /* The register it sets */
211 short lifetime; /* lifetime of that register;
212 may be adjusted when matching movables
213 that load the same value are found. */
214 short savings; /* Number of insns we can move for this reg,
215 including other movables that force this
216 or match this one. */
217 unsigned int cond : 1; /* 1 if only conditionally movable */
218 unsigned int force : 1; /* 1 means MUST move this insn */
219 unsigned int global : 1; /* 1 means reg is live outside this loop */
220 /* If PARTIAL is 1, GLOBAL means something different:
221 that the reg is live outside the range from where it is set
222 to the following label. */
223 unsigned int done : 1; /* 1 inhibits further processing of this */
224
225 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
226 In particular, moving it does not make it
227 invariant. */
228 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
229 load SRC, rather than copying INSN. */
230 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
231 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
232 that we should avoid changing when clearing
233 the rest of the reg. */
234 struct movable *match; /* First entry for same value */
235 struct movable *forces; /* An insn that must be moved if this is */
236 struct movable *next;
237};
238
239FILE *loop_dump_stream;
240
241/* Forward declarations. */
242
243static void find_and_verify_loops ();
244static void mark_loop_jump ();
245static void prescan_loop ();
246static int reg_in_basic_block_p ();
247static int consec_sets_invariant_p ();
248static rtx libcall_other_reg ();
249static int labels_in_range_p ();
250static void count_loop_regs_set ();
251static void note_addr_stored ();
252static int loop_reg_used_before_p ();
253static void scan_loop ();
254static void replace_call_address ();
255static rtx skip_consec_insns ();
256static int libcall_benefit ();
257static void ignore_some_movables ();
258static void force_movables ();
259static void combine_movables ();
260static int rtx_equal_for_loop_p ();
261static void move_movables ();
262static void strength_reduce ();
263static int valid_initial_value_p ();
264static void find_mem_givs ();
265static void record_biv ();
266static void check_final_value ();
267static void record_giv ();
268static void update_giv_derive ();
b4ad7b23
RS
269static int basic_induction_var ();
270static rtx simplify_giv_expr ();
271static int general_induction_var ();
272static int consec_sets_giv ();
273static int check_dbra_loop ();
274static rtx express_from ();
275static int combine_givs_p ();
276static void combine_givs ();
277static int product_cheap_p ();
278static int maybe_eliminate_biv ();
279static int maybe_eliminate_biv_1 ();
280static int last_use_this_basic_block ();
281static void record_initial ();
282static void update_reg_last_use ();
283\f
284/* Relative gain of eliminating various kinds of operations. */
285int add_cost;
286#if 0
287int shift_cost;
288int mult_cost;
289#endif
290
291/* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
292 copy the value of the strength reduced giv to its original register. */
293int copy_cost;
294
295void
296init_loop ()
297{
298 char *free_point = (char *) oballoc (1);
6e1b9d9f 299 rtx reg = gen_rtx (REG, word_mode, 0);
5fd8383e 300 rtx pow2 = GEN_INT (32);
b4ad7b23
RS
301 rtx lea;
302 int i;
303
6e1b9d9f 304 add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
b4ad7b23
RS
305
306 /* We multiply by 2 to reconcile the difference in scale between
307 these two ways of computing costs. Otherwise the cost of a copy
308 will be far less than the cost of an add. */
5fd8383e 309
b4ad7b23 310 copy_cost = 2 * 2;
b4ad7b23
RS
311
312 /* Free the objects we just allocated. */
313 obfree (free_point);
314
315 /* Initialize the obstack used for rtl in product_cheap_p. */
316 gcc_obstack_init (&temp_obstack);
317}
318\f
319/* Entry point of this file. Perform loop optimization
320 on the current function. F is the first insn of the function
321 and DUMPFILE is a stream for output of a trace of actions taken
322 (or 0 if none should be output). */
323
324void
325loop_optimize (f, dumpfile)
326 /* f is the first instruction of a chain of insns for one function */
327 rtx f;
328 FILE *dumpfile;
329{
330 register rtx insn;
331 register int i;
332 rtx end;
333 rtx last_insn;
334
335 loop_dump_stream = dumpfile;
336
337 init_recog_no_volatile ();
338 init_alias_analysis ();
339
340 max_reg_before_loop = max_reg_num ();
341
342 moved_once = (char *) alloca (max_reg_before_loop);
343 bzero (moved_once, max_reg_before_loop);
344
345 regs_may_share = 0;
346
347 /* Count the number of loops. */
348
349 max_loop_num = 0;
350 for (insn = f; insn; insn = NEXT_INSN (insn))
351 {
352 if (GET_CODE (insn) == NOTE
353 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
354 max_loop_num++;
355 }
356
357 /* Don't waste time if no loops. */
358 if (max_loop_num == 0)
359 return;
360
361 /* Get size to use for tables indexed by uids.
362 Leave some space for labels allocated by find_and_verify_loops. */
1c01e9df 363 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
b4ad7b23
RS
364
365 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
366 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
367
368 bzero (uid_luid, max_uid_for_loop * sizeof (int));
369 bzero (uid_loop_num, max_uid_for_loop * sizeof (int));
370
371 /* Allocate tables for recording each loop. We set each entry, so they need
372 not be zeroed. */
373 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
374 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
375 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
376 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
377 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
378
b4ad7b23
RS
379 /* Find and process each loop.
380 First, find them, and record them in order of their beginnings. */
381 find_and_verify_loops (f);
382
383 /* Now find all register lifetimes. This must be done after
384 find_and_verify_loops, because it might reorder the insns in the
385 function. */
386 reg_scan (f, max_reg_num (), 1);
387
1c01e9df
TW
388 /* See if we went too far. */
389 if (get_max_uid () > max_uid_for_loop)
390 abort ();
391
b4ad7b23
RS
392 /* Compute the mapping from uids to luids.
393 LUIDs are numbers assigned to insns, like uids,
394 except that luids increase monotonically through the code.
395 Don't assign luids to line-number NOTEs, so that the distance in luids
396 between two insns is not affected by -g. */
397
398 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
399 {
400 last_insn = insn;
401 if (GET_CODE (insn) != NOTE
402 || NOTE_LINE_NUMBER (insn) <= 0)
403 uid_luid[INSN_UID (insn)] = ++i;
404 else
405 /* Give a line number note the same luid as preceding insn. */
406 uid_luid[INSN_UID (insn)] = i;
407 }
408
409 max_luid = i + 1;
410
411 /* Don't leave gaps in uid_luid for insns that have been
412 deleted. It is possible that the first or last insn
413 using some register has been deleted by cross-jumping.
414 Make sure that uid_luid for that former insn's uid
415 points to the general area where that insn used to be. */
416 for (i = 0; i < max_uid_for_loop; i++)
417 {
418 uid_luid[0] = uid_luid[i];
419 if (uid_luid[0] != 0)
420 break;
421 }
422 for (i = 0; i < max_uid_for_loop; i++)
423 if (uid_luid[i] == 0)
424 uid_luid[i] = uid_luid[i - 1];
425
426 /* Create a mapping from loops to BLOCK tree nodes. */
427 if (flag_unroll_loops && write_symbols != NO_DEBUG)
07e857c2 428 find_loop_tree_blocks ();
b4ad7b23
RS
429
430 /* Now scan the loops, last ones first, since this means inner ones are done
431 before outer ones. */
432 for (i = max_loop_num-1; i >= 0; i--)
433 if (! loop_invalid[i] && loop_number_loop_ends[i])
434 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
435 max_reg_num ());
07e857c2
JW
436
437 /* If debugging and unrolling loops, we must replicate the tree nodes
438 corresponding to the blocks inside the loop, so that the original one
439 to one mapping will remain. */
440 if (flag_unroll_loops && write_symbols != NO_DEBUG)
441 unroll_block_trees ();
b4ad7b23
RS
442}
443\f
444/* Optimize one loop whose start is LOOP_START and end is END.
445 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
446 NOTE_INSN_LOOP_END. */
447
448/* ??? Could also move memory writes out of loops if the destination address
449 is invariant, the source is invariant, the memory write is not volatile,
450 and if we can prove that no read inside the loop can read this address
451 before the write occurs. If there is a read of this address after the
452 write, then we can also mark the memory read as invariant. */
453
454static void
455scan_loop (loop_start, end, nregs)
456 rtx loop_start, end;
457 int nregs;
458{
459 register int i;
460 register rtx p;
461 /* 1 if we are scanning insns that could be executed zero times. */
462 int maybe_never = 0;
463 /* 1 if we are scanning insns that might never be executed
464 due to a subroutine call which might exit before they are reached. */
465 int call_passed = 0;
466 /* For a rotated loop that is entered near the bottom,
467 this is the label at the top. Otherwise it is zero. */
468 rtx loop_top = 0;
469 /* Jump insn that enters the loop, or 0 if control drops in. */
470 rtx loop_entry_jump = 0;
471 /* Place in the loop where control enters. */
472 rtx scan_start;
473 /* Number of insns in the loop. */
474 int insn_count;
475 int in_libcall = 0;
476 int tem;
477 rtx temp;
478 /* The SET from an insn, if it is the only SET in the insn. */
479 rtx set, set1;
480 /* Chain describing insns movable in current loop. */
481 struct movable *movables = 0;
482 /* Last element in `movables' -- so we can add elements at the end. */
483 struct movable *last_movable = 0;
484 /* Ratio of extra register life span we can justify
485 for saving an instruction. More if loop doesn't call subroutines
486 since in that case saving an insn makes more difference
487 and more registers are available. */
488 int threshold;
489 /* If we have calls, contains the insn in which a register was used
490 if it was used exactly once; contains const0_rtx if it was used more
491 than once. */
492 rtx *reg_single_usage = 0;
493
494 n_times_set = (short *) alloca (nregs * sizeof (short));
495 n_times_used = (short *) alloca (nregs * sizeof (short));
496 may_not_optimize = (char *) alloca (nregs);
497
498 /* Determine whether this loop starts with a jump down to a test at
499 the end. This will occur for a small number of loops with a test
500 that is too complex to duplicate in front of the loop.
501
502 We search for the first insn or label in the loop, skipping NOTEs.
503 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
504 (because we might have a loop executed only once that contains a
505 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
506 (in case we have a degenerate loop).
507
508 Note that if we mistakenly think that a loop is entered at the top
509 when, in fact, it is entered at the exit test, the only effect will be
510 slightly poorer optimization. Making the opposite error can generate
511 incorrect code. Since very few loops now start with a jump to the
512 exit test, the code here to detect that case is very conservative. */
513
514 for (p = NEXT_INSN (loop_start);
515 p != end
516 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
517 && (GET_CODE (p) != NOTE
518 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
519 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
520 p = NEXT_INSN (p))
521 ;
522
523 scan_start = p;
524
525 /* Set up variables describing this loop. */
526 prescan_loop (loop_start, end);
527 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
528
529 /* If loop has a jump before the first label,
530 the true entry is the target of that jump.
531 Start scan from there.
532 But record in LOOP_TOP the place where the end-test jumps
533 back to so we can scan that after the end of the loop. */
534 if (GET_CODE (p) == JUMP_INSN)
535 {
536 loop_entry_jump = p;
537
538 /* Loop entry must be unconditional jump (and not a RETURN) */
539 if (simplejump_p (p)
540 && JUMP_LABEL (p) != 0
541 /* Check to see whether the jump actually
542 jumps out of the loop (meaning it's no loop).
543 This case can happen for things like
544 do {..} while (0). If this label was generated previously
545 by loop, we can't tell anything about it and have to reject
546 the loop. */
547 && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
548 && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
549 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
550 {
551 loop_top = next_label (scan_start);
552 scan_start = JUMP_LABEL (p);
553 }
554 }
555
556 /* If SCAN_START was an insn created by loop, we don't know its luid
557 as required by loop_reg_used_before_p. So skip such loops. (This
558 test may never be true, but it's best to play it safe.)
559
560 Also, skip loops where we do not start scanning at a label. This
561 test also rejects loops starting with a JUMP_INSN that failed the
562 test above. */
563
564 if (INSN_UID (scan_start) >= max_uid_for_loop
565 || GET_CODE (scan_start) != CODE_LABEL)
566 {
567 if (loop_dump_stream)
568 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
569 INSN_UID (loop_start), INSN_UID (end));
570 return;
571 }
572
573 /* Count number of times each reg is set during this loop.
574 Set may_not_optimize[I] if it is not safe to move out
575 the setting of register I. If this loop has calls, set
576 reg_single_usage[I]. */
577
578 bzero (n_times_set, nregs * sizeof (short));
579 bzero (may_not_optimize, nregs);
580
581 if (loop_has_call)
582 {
583 reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
584 bzero (reg_single_usage, nregs * sizeof (rtx));
585 }
586
587 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
588 may_not_optimize, reg_single_usage, &insn_count, nregs);
589
590 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
591 may_not_optimize[i] = 1, n_times_set[i] = 1;
592 bcopy (n_times_set, n_times_used, nregs * sizeof (short));
593
594 if (loop_dump_stream)
595 {
596 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
597 INSN_UID (loop_start), INSN_UID (end), insn_count);
598 if (loop_continue)
599 fprintf (loop_dump_stream, "Continue at insn %d.\n",
600 INSN_UID (loop_continue));
601 }
602
603 /* Scan through the loop finding insns that are safe to move.
d45cf215 604 Set n_times_set negative for the reg being set, so that
b4ad7b23
RS
605 this reg will be considered invariant for subsequent insns.
606 We consider whether subsequent insns use the reg
607 in deciding whether it is worth actually moving.
608
609 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
610 and therefore it is possible that the insns we are scanning
611 would never be executed. At such times, we must make sure
612 that it is safe to execute the insn once instead of zero times.
613 When MAYBE_NEVER is 0, all insns will be executed at least once
614 so that is not a problem. */
615
616 p = scan_start;
617 while (1)
618 {
619 p = NEXT_INSN (p);
620 /* At end of a straight-in loop, we are done.
621 At end of a loop entered at the bottom, scan the top. */
622 if (p == scan_start)
623 break;
624 if (p == end)
625 {
626 if (loop_top != 0)
627 p = NEXT_INSN (loop_top);
628 else
629 break;
630 if (p == scan_start)
631 break;
632 }
633
634 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5fd8383e 635 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
b4ad7b23
RS
636 in_libcall = 1;
637 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5fd8383e 638 && find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
639 in_libcall = 0;
640
641 if (GET_CODE (p) == INSN
642 && (set = single_set (p))
643 && GET_CODE (SET_DEST (set)) == REG
644 && ! may_not_optimize[REGNO (SET_DEST (set))])
645 {
646 int tem1 = 0;
647 int tem2 = 0;
648 int move_insn = 0;
649 rtx src = SET_SRC (set);
650 rtx dependencies = 0;
651
652 /* Figure out what to use as a source of this insn. If a REG_EQUIV
653 note is given or if a REG_EQUAL note with a constant operand is
654 specified, use it as the source and mark that we should move
655 this insn by calling emit_move_insn rather that duplicating the
656 insn.
657
658 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
659 is present. */
5fd8383e 660 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
b4ad7b23
RS
661 if (temp)
662 src = XEXP (temp, 0), move_insn = 1;
663 else
664 {
5fd8383e 665 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
b4ad7b23
RS
666 if (temp && CONSTANT_P (XEXP (temp, 0)))
667 src = XEXP (temp, 0), move_insn = 1;
5fd8383e 668 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
669 {
670 src = XEXP (temp, 0);
671 /* A libcall block can use regs that don't appear in
672 the equivalent expression. To move the libcall,
673 we must move those regs too. */
674 dependencies = libcall_other_reg (p, src);
675 }
676 }
677
678 /* Don't try to optimize a register that was made
679 by loop-optimization for an inner loop.
680 We don't know its life-span, so we can't compute the benefit. */
681 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
682 ;
683 /* In order to move a register, we need to have one of three cases:
684 (1) it is used only in the same basic block as the set
685 (2) it is not a user variable.
686 (3) the set is guaranteed to be executed once the loop starts,
687 and the reg is not used until after that. */
688 else if (! ((! maybe_never
689 && ! loop_reg_used_before_p (set, p, loop_start,
690 scan_start, end))
691 || ! REG_USERVAR_P (SET_DEST (PATTERN (p)))
692 || reg_in_basic_block_p (p, SET_DEST (PATTERN (p)))))
693 ;
694 else if ((tem = invariant_p (src))
695 && (dependencies == 0
696 || (tem2 = invariant_p (dependencies)) != 0)
697 && (n_times_set[REGNO (SET_DEST (set))] == 1
698 || (tem1
699 = consec_sets_invariant_p (SET_DEST (set),
700 n_times_set[REGNO (SET_DEST (set))],
701 p)))
702 /* If the insn can cause a trap (such as divide by zero),
703 can't move it unless it's guaranteed to be executed
704 once loop is entered. Even a function call might
705 prevent the trap insn from being reached
706 (since it might exit!) */
707 && ! ((maybe_never || call_passed)
708 && may_trap_p (src)))
709 {
710 register struct movable *m;
711 register int regno = REGNO (SET_DEST (set));
712
713 /* A potential lossage is where we have a case where two insns
714 can be combined as long as they are both in the loop, but
715 we move one of them outside the loop. For large loops,
716 this can lose. The most common case of this is the address
717 of a function being called.
718
719 Therefore, if this register is marked as being used exactly
720 once if we are in a loop with calls (a "large loop"), see if
721 we can replace the usage of this register with the source
722 of this SET. If we can, delete this insn.
723
724 Don't do this if P has a REG_RETVAL note or if we have
725 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
726
727 if (reg_single_usage && reg_single_usage[regno] != 0
728 && reg_single_usage[regno] != const0_rtx
729 && regno_first_uid[regno] == INSN_UID (p)
730 && (regno_last_uid[regno]
731 == INSN_UID (reg_single_usage[regno]))
732 && n_times_set[REGNO (SET_DEST (set))] == 1
733 && ! side_effects_p (SET_SRC (set))
5fd8383e 734 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
b4ad7b23
RS
735#ifdef SMALL_REGISTER_CLASSES
736 && ! (GET_CODE (SET_SRC (set)) == REG
737 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)
738#endif
739 /* This test is not redundant; SET_SRC (set) might be
740 a call-clobbered register and the life of REGNO
741 might span a call. */
742 && ! modified_between_p (SET_SRC (set), p,
743 reg_single_usage[regno])
744 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
745 reg_single_usage[regno]))
746 {
747 /* Replace any usage in a REG_EQUAL note. */
748 REG_NOTES (reg_single_usage[regno])
749 = replace_rtx (REG_NOTES (reg_single_usage[regno]),
750 SET_DEST (set), SET_SRC (set));
751
752 PUT_CODE (p, NOTE);
753 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
754 NOTE_SOURCE_FILE (p) = 0;
755 n_times_set[regno] = 0;
756 continue;
757 }
758
759 m = (struct movable *) alloca (sizeof (struct movable));
760 m->next = 0;
761 m->insn = p;
762 m->set_src = src;
763 m->dependencies = dependencies;
764 m->set_dest = SET_DEST (set);
765 m->force = 0;
766 m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
767 m->done = 0;
768 m->forces = 0;
769 m->partial = 0;
770 m->move_insn = move_insn;
5fd8383e 771 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
b4ad7b23
RS
772 m->savemode = VOIDmode;
773 m->regno = regno;
774 /* Set M->cond if either invariant_p or consec_sets_invariant_p
775 returned 2 (only conditionally invariant). */
776 m->cond = ((tem | tem1 | tem2) > 1);
777 m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
778 || uid_luid[regno_first_uid[regno]] < INSN_LUID (loop_start));
779 m->match = 0;
780 m->lifetime = (uid_luid[regno_last_uid[regno]]
781 - uid_luid[regno_first_uid[regno]]);
782 m->savings = n_times_used[regno];
5fd8383e 783 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
784 m->savings += libcall_benefit (p);
785 n_times_set[regno] = move_insn ? -2 : -1;
786 /* Add M to the end of the chain MOVABLES. */
787 if (movables == 0)
788 movables = m;
789 else
790 last_movable->next = m;
791 last_movable = m;
792
793 if (m->consec > 0)
794 {
795 /* Skip this insn, not checking REG_LIBCALL notes. */
796 p = NEXT_INSN (p);
797 /* Skip the consecutive insns, if there are any. */
798 p = skip_consec_insns (p, m->consec);
799 /* Back up to the last insn of the consecutive group. */
800 p = prev_nonnote_insn (p);
801
802 /* We must now reset m->move_insn, m->is_equiv, and possibly
803 m->set_src to correspond to the effects of all the
804 insns. */
5fd8383e 805 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
b4ad7b23
RS
806 if (temp)
807 m->set_src = XEXP (temp, 0), m->move_insn = 1;
808 else
809 {
5fd8383e 810 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
b4ad7b23
RS
811 if (temp && CONSTANT_P (XEXP (temp, 0)))
812 m->set_src = XEXP (temp, 0), m->move_insn = 1;
813 else
814 m->move_insn = 0;
815
816 }
5fd8383e 817 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
b4ad7b23
RS
818 }
819 }
820 /* If this register is always set within a STRICT_LOW_PART
821 or set to zero, then its high bytes are constant.
822 So clear them outside the loop and within the loop
823 just load the low bytes.
824 We must check that the machine has an instruction to do so.
825 Also, if the value loaded into the register
826 depends on the same register, this cannot be done. */
827 else if (SET_SRC (set) == const0_rtx
828 && GET_CODE (NEXT_INSN (p)) == INSN
829 && (set1 = single_set (NEXT_INSN (p)))
830 && GET_CODE (set1) == SET
831 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
832 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
833 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
834 == SET_DEST (set))
835 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
836 {
837 register int regno = REGNO (SET_DEST (set));
838 if (n_times_set[regno] == 2)
839 {
840 register struct movable *m;
841 m = (struct movable *) alloca (sizeof (struct movable));
842 m->next = 0;
843 m->insn = p;
844 m->set_dest = SET_DEST (set);
845 m->dependencies = 0;
846 m->force = 0;
847 m->consec = 0;
848 m->done = 0;
849 m->forces = 0;
850 m->move_insn = 0;
851 m->partial = 1;
852 /* If the insn may not be executed on some cycles,
853 we can't clear the whole reg; clear just high part.
854 Not even if the reg is used only within this loop.
855 Consider this:
856 while (1)
857 while (s != t) {
858 if (foo ()) x = *s;
859 use (x);
860 }
861 Clearing x before the inner loop could clobber a value
862 being saved from the last time around the outer loop.
863 However, if the reg is not used outside this loop
864 and all uses of the register are in the same
865 basic block as the store, there is no problem.
866
867 If this insn was made by loop, we don't know its
868 INSN_LUID and hence must make a conservative
869 assumption. */
870 m->global = (INSN_UID (p) >= max_uid_for_loop
871 || (uid_luid[regno_last_uid[regno]]
872 > INSN_LUID (end))
873 || (uid_luid[regno_first_uid[regno]]
874 < INSN_LUID (p))
875 || (labels_in_range_p
876 (p, uid_luid[regno_first_uid[regno]])));
877 if (maybe_never && m->global)
878 m->savemode = GET_MODE (SET_SRC (set1));
879 else
880 m->savemode = VOIDmode;
881 m->regno = regno;
882 m->cond = 0;
883 m->match = 0;
884 m->lifetime = (uid_luid[regno_last_uid[regno]]
885 - uid_luid[regno_first_uid[regno]]);
886 m->savings = 1;
887 n_times_set[regno] = -1;
888 /* Add M to the end of the chain MOVABLES. */
889 if (movables == 0)
890 movables = m;
891 else
892 last_movable->next = m;
893 last_movable = m;
894 }
895 }
896 }
897 /* Past a call insn, we get to insns which might not be executed
898 because the call might exit. This matters for insns that trap.
899 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
900 so they don't count. */
901 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
902 call_passed = 1;
903 /* Past a label or a jump, we get to insns for which we
904 can't count on whether or how many times they will be
905 executed during each iteration. Therefore, we can
906 only move out sets of trivial variables
907 (those not used after the loop). */
908 /* This code appears in three places, once in scan_loop, and twice
909 in strength_reduce. */
910 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
911 /* If we enter the loop in the middle, and scan around to the
912 beginning, don't set maybe_never for that. This must be an
913 unconditional jump, otherwise the code at the top of the
914 loop might never be executed. Unconditional jumps are
915 followed a by barrier then loop end. */
916 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
917 && NEXT_INSN (NEXT_INSN (p)) == end
918 && simplejump_p (p)))
919 maybe_never = 1;
920 /* At the virtual top of a converted loop, insns are again known to
921 be executed: logically, the loop begins here even though the exit
922 code has been duplicated. */
923 else if (GET_CODE (p) == NOTE
924 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP)
925 maybe_never = call_passed = 0;
926 }
927
928 /* If one movable subsumes another, ignore that other. */
929
930 ignore_some_movables (movables);
931
932 /* For each movable insn, see if the reg that it loads
933 leads when it dies right into another conditionally movable insn.
934 If so, record that the second insn "forces" the first one,
935 since the second can be moved only if the first is. */
936
937 force_movables (movables);
938
939 /* See if there are multiple movable insns that load the same value.
940 If there are, make all but the first point at the first one
941 through the `match' field, and add the priorities of them
942 all together as the priority of the first. */
943
944 combine_movables (movables, nregs);
945
946 /* Now consider each movable insn to decide whether it is worth moving.
947 Store 0 in n_times_set for each reg that is moved. */
948
949 move_movables (movables, threshold,
950 insn_count, loop_start, end, nregs);
951
952 /* Now candidates that still are negative are those not moved.
953 Change n_times_set to indicate that those are not actually invariant. */
954 for (i = 0; i < nregs; i++)
955 if (n_times_set[i] < 0)
956 n_times_set[i] = n_times_used[i];
957
958 if (flag_strength_reduce)
959 strength_reduce (scan_start, end, loop_top,
960 insn_count, loop_start, end);
961}
962\f
963/* Add elements to *OUTPUT to record all the pseudo-regs
964 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
965
966void
967record_excess_regs (in_this, not_in_this, output)
968 rtx in_this, not_in_this;
969 rtx *output;
970{
971 enum rtx_code code;
972 char *fmt;
973 int i;
974
975 code = GET_CODE (in_this);
976
977 switch (code)
978 {
979 case PC:
980 case CC0:
981 case CONST_INT:
982 case CONST_DOUBLE:
983 case CONST:
984 case SYMBOL_REF:
985 case LABEL_REF:
986 return;
987
988 case REG:
989 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
990 && ! reg_mentioned_p (in_this, not_in_this))
991 *output = gen_rtx (EXPR_LIST, VOIDmode, in_this, *output);
992 return;
993 }
994
995 fmt = GET_RTX_FORMAT (code);
996 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
997 {
998 int j;
999
1000 switch (fmt[i])
1001 {
1002 case 'E':
1003 for (j = 0; j < XVECLEN (in_this, i); j++)
1004 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1005 break;
1006
1007 case 'e':
1008 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1009 break;
1010 }
1011 }
1012}
1013\f
1014/* Check what regs are referred to in the libcall block ending with INSN,
1015 aside from those mentioned in the equivalent value.
1016 If there are none, return 0.
1017 If there are one or more, return an EXPR_LIST containing all of them. */
1018
1019static rtx
1020libcall_other_reg (insn, equiv)
1021 rtx insn, equiv;
1022{
5fd8383e 1023 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
b4ad7b23
RS
1024 rtx p = XEXP (note, 0);
1025 rtx output = 0;
1026
1027 /* First, find all the regs used in the libcall block
1028 that are not mentioned as inputs to the result. */
1029
1030 while (p != insn)
1031 {
1032 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1033 || GET_CODE (p) == CALL_INSN)
1034 record_excess_regs (PATTERN (p), equiv, &output);
1035 p = NEXT_INSN (p);
1036 }
1037
1038 return output;
1039}
1040\f
1041/* Return 1 if all uses of REG
1042 are between INSN and the end of the basic block. */
1043
1044static int
1045reg_in_basic_block_p (insn, reg)
1046 rtx insn, reg;
1047{
1048 int regno = REGNO (reg);
1049 rtx p;
1050
1051 if (regno_first_uid[regno] != INSN_UID (insn))
1052 return 0;
1053
1054 /* Search this basic block for the already recorded last use of the reg. */
1055 for (p = insn; p; p = NEXT_INSN (p))
1056 {
1057 switch (GET_CODE (p))
1058 {
1059 case NOTE:
1060 break;
1061
1062 case INSN:
1063 case CALL_INSN:
1064 /* Ordinary insn: if this is the last use, we win. */
1065 if (regno_last_uid[regno] == INSN_UID (p))
1066 return 1;
1067 break;
1068
1069 case JUMP_INSN:
1070 /* Jump insn: if this is the last use, we win. */
1071 if (regno_last_uid[regno] == INSN_UID (p))
1072 return 1;
1073 /* Otherwise, it's the end of the basic block, so we lose. */
1074 return 0;
1075
1076 case CODE_LABEL:
1077 case BARRIER:
1078 /* It's the end of the basic block, so we lose. */
1079 return 0;
1080 }
1081 }
1082
1083 /* The "last use" doesn't follow the "first use"?? */
1084 abort ();
1085}
1086\f
1087/* Compute the benefit of eliminating the insns in the block whose
1088 last insn is LAST. This may be a group of insns used to compute a
1089 value directly or can contain a library call. */
1090
1091static int
1092libcall_benefit (last)
1093 rtx last;
1094{
1095 rtx insn;
1096 int benefit = 0;
1097
5fd8383e 1098 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
b4ad7b23
RS
1099 insn != last; insn = NEXT_INSN (insn))
1100 {
1101 if (GET_CODE (insn) == CALL_INSN)
1102 benefit += 10; /* Assume at least this many insns in a library
1103 routine. */
1104 else if (GET_CODE (insn) == INSN
1105 && GET_CODE (PATTERN (insn)) != USE
1106 && GET_CODE (PATTERN (insn)) != CLOBBER)
1107 benefit++;
1108 }
1109
1110 return benefit;
1111}
1112\f
1113/* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1114
1115static rtx
1116skip_consec_insns (insn, count)
1117 rtx insn;
1118 int count;
1119{
1120 for (; count > 0; count--)
1121 {
1122 rtx temp;
1123
1124 /* If first insn of libcall sequence, skip to end. */
1125 /* Do this at start of loop, since INSN is guaranteed to
1126 be an insn here. */
1127 if (GET_CODE (insn) != NOTE
5fd8383e 1128 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1129 insn = XEXP (temp, 0);
1130
1131 do insn = NEXT_INSN (insn);
1132 while (GET_CODE (insn) == NOTE);
1133 }
1134
1135 return insn;
1136}
1137
1138/* Ignore any movable whose insn falls within a libcall
1139 which is part of another movable.
1140 We make use of the fact that the movable for the libcall value
1141 was made later and so appears later on the chain. */
1142
1143static void
1144ignore_some_movables (movables)
1145 struct movable *movables;
1146{
1147 register struct movable *m, *m1;
1148
1149 for (m = movables; m; m = m->next)
1150 {
1151 /* Is this a movable for the value of a libcall? */
5fd8383e 1152 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
b4ad7b23
RS
1153 if (note)
1154 {
1155 rtx insn;
1156 /* Check for earlier movables inside that range,
1157 and mark them invalid. We cannot use LUIDs here because
1158 insns created by loop.c for prior loops don't have LUIDs.
1159 Rather than reject all such insns from movables, we just
1160 explicitly check each insn in the libcall (since invariant
1161 libcalls aren't that common). */
1162 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1163 for (m1 = movables; m1 != m; m1 = m1->next)
1164 if (m1->insn == insn)
1165 m1->done = 1;
1166 }
1167 }
1168}
1169
1170/* For each movable insn, see if the reg that it loads
1171 leads when it dies right into another conditionally movable insn.
1172 If so, record that the second insn "forces" the first one,
1173 since the second can be moved only if the first is. */
1174
1175static void
1176force_movables (movables)
1177 struct movable *movables;
1178{
1179 register struct movable *m, *m1;
1180 for (m1 = movables; m1; m1 = m1->next)
1181 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1182 if (!m1->partial && !m1->done)
1183 {
1184 int regno = m1->regno;
1185 for (m = m1->next; m; m = m->next)
1186 /* ??? Could this be a bug? What if CSE caused the
1187 register of M1 to be used after this insn?
1188 Since CSE does not update regno_last_uid,
1189 this insn M->insn might not be where it dies.
1190 But very likely this doesn't matter; what matters is
1191 that M's reg is computed from M1's reg. */
1192 if (INSN_UID (m->insn) == regno_last_uid[regno]
1193 && !m->done)
1194 break;
1195 if (m != 0 && m->set_src == m1->set_dest
1196 /* If m->consec, m->set_src isn't valid. */
1197 && m->consec == 0)
1198 m = 0;
1199
1200 /* Increase the priority of the moving the first insn
1201 since it permits the second to be moved as well. */
1202 if (m != 0)
1203 {
1204 m->forces = m1;
1205 m1->lifetime += m->lifetime;
1206 m1->savings += m1->savings;
1207 }
1208 }
1209}
1210\f
1211/* Find invariant expressions that are equal and can be combined into
1212 one register. */
1213
1214static void
1215combine_movables (movables, nregs)
1216 struct movable *movables;
1217 int nregs;
1218{
1219 register struct movable *m;
1220 char *matched_regs = (char *) alloca (nregs);
1221 enum machine_mode mode;
1222
1223 /* Regs that are set more than once are not allowed to match
1224 or be matched. I'm no longer sure why not. */
1225 /* Perhaps testing m->consec_sets would be more appropriate here? */
1226
1227 for (m = movables; m; m = m->next)
1228 if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1229 {
1230 register struct movable *m1;
1231 int regno = m->regno;
1232 rtx reg_note, reg_note1;
1233
1234 bzero (matched_regs, nregs);
1235 matched_regs[regno] = 1;
1236
1237 for (m1 = movables; m1; m1 = m1->next)
1238 if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1239 /* A reg used outside the loop mustn't be eliminated. */
1240 && !m1->global
1241 /* A reg used for zero-extending mustn't be eliminated. */
1242 && !m1->partial
1243 && (matched_regs[m1->regno]
1244 ||
1245 (
1246 /* Can combine regs with different modes loaded from the
1247 same constant only if the modes are the same or
1248 if both are integer modes with M wider or the same
1249 width as M1. The check for integer is redundant, but
1250 safe, since the only case of differing destination
1251 modes with equal sources is when both sources are
1252 VOIDmode, i.e., CONST_INT. */
1253 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1254 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1255 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1256 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1257 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1258 /* See if the source of M1 says it matches M. */
1259 && ((GET_CODE (m1->set_src) == REG
1260 && matched_regs[REGNO (m1->set_src)])
1261 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1262 movables))))
1263 && ((m->dependencies == m1->dependencies)
1264 || rtx_equal_p (m->dependencies, m1->dependencies)))
1265 {
1266 m->lifetime += m1->lifetime;
1267 m->savings += m1->savings;
1268 m1->done = 1;
1269 m1->match = m;
1270 matched_regs[m1->regno] = 1;
1271 }
1272 }
1273
1274 /* Now combine the regs used for zero-extension.
1275 This can be done for those not marked `global'
1276 provided their lives don't overlap. */
1277
1278 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1279 mode = GET_MODE_WIDER_MODE (mode))
1280 {
1281 register struct movable *m0 = 0;
1282
1283 /* Combine all the registers for extension from mode MODE.
1284 Don't combine any that are used outside this loop. */
1285 for (m = movables; m; m = m->next)
1286 if (m->partial && ! m->global
1287 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1288 {
1289 register struct movable *m1;
1290 int first = uid_luid[regno_first_uid[m->regno]];
1291 int last = uid_luid[regno_last_uid[m->regno]];
1292
1293 if (m0 == 0)
1294 {
1295 /* First one: don't check for overlap, just record it. */
1296 m0 = m;
1297 continue;
1298 }
1299
1300 /* Make sure they extend to the same mode.
1301 (Almost always true.) */
1302 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1303 continue;
1304
1305 /* We already have one: check for overlap with those
1306 already combined together. */
1307 for (m1 = movables; m1 != m; m1 = m1->next)
1308 if (m1 == m0 || (m1->partial && m1->match == m0))
1309 if (! (uid_luid[regno_first_uid[m1->regno]] > last
1310 || uid_luid[regno_last_uid[m1->regno]] < first))
1311 goto overlap;
1312
1313 /* No overlap: we can combine this with the others. */
1314 m0->lifetime += m->lifetime;
1315 m0->savings += m->savings;
1316 m->done = 1;
1317 m->match = m0;
1318
1319 overlap: ;
1320 }
1321 }
1322}
1323\f
1324/* Return 1 if regs X and Y will become the same if moved. */
1325
1326static int
1327regs_match_p (x, y, movables)
1328 rtx x, y;
1329 struct movable *movables;
1330{
1331 int xn = REGNO (x);
1332 int yn = REGNO (y);
1333 struct movable *mx, *my;
1334
1335 for (mx = movables; mx; mx = mx->next)
1336 if (mx->regno == xn)
1337 break;
1338
1339 for (my = movables; my; my = my->next)
1340 if (my->regno == yn)
1341 break;
1342
1343 return (mx && my
1344 && ((mx->match == my->match && mx->match != 0)
1345 || mx->match == my
1346 || mx == my->match));
1347}
1348
1349/* Return 1 if X and Y are identical-looking rtx's.
1350 This is the Lisp function EQUAL for rtx arguments.
1351
1352 If two registers are matching movables or a movable register and an
1353 equivalent constant, consider them equal. */
1354
1355static int
1356rtx_equal_for_loop_p (x, y, movables)
1357 rtx x, y;
1358 struct movable *movables;
1359{
1360 register int i;
1361 register int j;
1362 register struct movable *m;
1363 register enum rtx_code code;
1364 register char *fmt;
1365
1366 if (x == y)
1367 return 1;
1368 if (x == 0 || y == 0)
1369 return 0;
1370
1371 code = GET_CODE (x);
1372
1373 /* If we have a register and a constant, they may sometimes be
1374 equal. */
1375 if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1376 && CONSTANT_P (y))
1377 for (m = movables; m; m = m->next)
1378 if (m->move_insn && m->regno == REGNO (x)
1379 && rtx_equal_p (m->set_src, y))
1380 return 1;
1381
1382 else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1383 && CONSTANT_P (x))
1384 for (m = movables; m; m = m->next)
1385 if (m->move_insn && m->regno == REGNO (y)
1386 && rtx_equal_p (m->set_src, x))
1387 return 1;
1388
1389 /* Otherwise, rtx's of different codes cannot be equal. */
1390 if (code != GET_CODE (y))
1391 return 0;
1392
1393 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1394 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1395
1396 if (GET_MODE (x) != GET_MODE (y))
1397 return 0;
1398
1399 /* These three types of rtx's can be compared nonrecursively. */
1400 if (code == REG)
1401 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1402
1403 if (code == LABEL_REF)
1404 return XEXP (x, 0) == XEXP (y, 0);
1405 if (code == SYMBOL_REF)
1406 return XSTR (x, 0) == XSTR (y, 0);
1407
1408 /* Compare the elements. If any pair of corresponding elements
1409 fail to match, return 0 for the whole things. */
1410
1411 fmt = GET_RTX_FORMAT (code);
1412 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1413 {
1414 switch (fmt[i])
1415 {
5fd8383e
RK
1416 case 'w':
1417 if (XWINT (x, i) != XWINT (y, i))
1418 return 0;
1419 break;
1420
b4ad7b23
RS
1421 case 'i':
1422 if (XINT (x, i) != XINT (y, i))
1423 return 0;
1424 break;
1425
1426 case 'E':
1427 /* Two vectors must have the same length. */
1428 if (XVECLEN (x, i) != XVECLEN (y, i))
1429 return 0;
1430
1431 /* And the corresponding elements must match. */
1432 for (j = 0; j < XVECLEN (x, i); j++)
1433 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1434 return 0;
1435 break;
1436
1437 case 'e':
1438 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1439 return 0;
1440 break;
1441
1442 case 's':
1443 if (strcmp (XSTR (x, i), XSTR (y, i)))
1444 return 0;
1445 break;
1446
1447 case 'u':
1448 /* These are just backpointers, so they don't matter. */
1449 break;
1450
1451 case '0':
1452 break;
1453
1454 /* It is believed that rtx's at this level will never
1455 contain anything but integers and other rtx's,
1456 except for within LABEL_REFs and SYMBOL_REFs. */
1457 default:
1458 abort ();
1459 }
1460 }
1461 return 1;
1462}
1463\f
c160c628
RK
1464/* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1465 insns in INSNS which use thet reference. */
1466
1467static void
1468add_label_notes (x, insns)
1469 rtx x;
1470 rtx insns;
1471{
1472 enum rtx_code code = GET_CODE (x);
7dcd3836 1473 int i, j;
c160c628
RK
1474 char *fmt;
1475 rtx insn;
1476
82d00367 1477 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
c160c628 1478 {
034dabc9
JW
1479 rtx next = next_real_insn (XEXP (x, 0));
1480
1481 /* Don't record labels that refer to dispatch tables.
1482 This is not necessary, since the tablejump references the same label.
1483 And if we did record them, flow.c would make worse code. */
1484 if (next == 0
1485 || ! (GET_CODE (next) == JUMP_INSN
1486 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1487 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1488 {
1489 for (insn = insns; insn; insn = NEXT_INSN (insn))
1490 if (reg_mentioned_p (XEXP (x, 0), insn))
1491 REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, XEXP (x, 0),
1492 REG_NOTES (insn));
1493 }
c160c628
RK
1494 return;
1495 }
1496
1497 fmt = GET_RTX_FORMAT (code);
1498 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7dcd3836
RK
1499 {
1500 if (fmt[i] == 'e')
1501 add_label_notes (XEXP (x, i), insns);
1502 else if (fmt[i] == 'E')
1503 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1504 add_label_notes (XVECEXP (x, i, j), insns);
1505 }
c160c628
RK
1506}
1507\f
b4ad7b23
RS
1508/* Scan MOVABLES, and move the insns that deserve to be moved.
1509 If two matching movables are combined, replace one reg with the
1510 other throughout. */
1511
1512static void
1513move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1514 struct movable *movables;
1515 int threshold;
1516 int insn_count;
1517 rtx loop_start;
1518 rtx end;
1519 int nregs;
1520{
1521 rtx new_start = 0;
1522 register struct movable *m;
1523 register rtx p;
1524 /* Map of pseudo-register replacements to handle combining
1525 when we move several insns that load the same value
1526 into different pseudo-registers. */
1527 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1528 char *already_moved = (char *) alloca (nregs);
1529
1530 bzero (already_moved, nregs);
1531 bzero (reg_map, nregs * sizeof (rtx));
1532
1533 num_movables = 0;
1534
1535 for (m = movables; m; m = m->next)
1536 {
1537 /* Describe this movable insn. */
1538
1539 if (loop_dump_stream)
1540 {
1541 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1542 INSN_UID (m->insn), m->regno, m->lifetime);
1543 if (m->consec > 0)
1544 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1545 if (m->cond)
1546 fprintf (loop_dump_stream, "cond ");
1547 if (m->force)
1548 fprintf (loop_dump_stream, "force ");
1549 if (m->global)
1550 fprintf (loop_dump_stream, "global ");
1551 if (m->done)
1552 fprintf (loop_dump_stream, "done ");
1553 if (m->move_insn)
1554 fprintf (loop_dump_stream, "move-insn ");
1555 if (m->match)
1556 fprintf (loop_dump_stream, "matches %d ",
1557 INSN_UID (m->match->insn));
1558 if (m->forces)
1559 fprintf (loop_dump_stream, "forces %d ",
1560 INSN_UID (m->forces->insn));
1561 }
1562
1563 /* Count movables. Value used in heuristics in strength_reduce. */
1564 num_movables++;
1565
1566 /* Ignore the insn if it's already done (it matched something else).
1567 Otherwise, see if it is now safe to move. */
1568
1569 if (!m->done
1570 && (! m->cond
1571 || (1 == invariant_p (m->set_src)
1572 && (m->dependencies == 0
1573 || 1 == invariant_p (m->dependencies))
1574 && (m->consec == 0
1575 || 1 == consec_sets_invariant_p (m->set_dest,
1576 m->consec + 1,
1577 m->insn))))
1578 && (! m->forces || m->forces->done))
1579 {
1580 register int regno;
1581 register rtx p;
1582 int savings = m->savings;
1583
1584 /* We have an insn that is safe to move.
1585 Compute its desirability. */
1586
1587 p = m->insn;
1588 regno = m->regno;
1589
1590 if (loop_dump_stream)
1591 fprintf (loop_dump_stream, "savings %d ", savings);
1592
1593 if (moved_once[regno])
1594 {
1595 insn_count *= 2;
1596
1597 if (loop_dump_stream)
1598 fprintf (loop_dump_stream, "halved since already moved ");
1599 }
1600
1601 /* An insn MUST be moved if we already moved something else
1602 which is safe only if this one is moved too: that is,
1603 if already_moved[REGNO] is nonzero. */
1604
1605 /* An insn is desirable to move if the new lifetime of the
1606 register is no more than THRESHOLD times the old lifetime.
1607 If it's not desirable, it means the loop is so big
1608 that moving won't speed things up much,
1609 and it is liable to make register usage worse. */
1610
1611 /* It is also desirable to move if it can be moved at no
1612 extra cost because something else was already moved. */
1613
1614 if (already_moved[regno]
1615 || (threshold * savings * m->lifetime) >= insn_count
1616 || (m->forces && m->forces->done
1617 && n_times_used[m->forces->regno] == 1))
1618 {
1619 int count;
1620 register struct movable *m1;
1621 rtx first;
1622
1623 /* Now move the insns that set the reg. */
1624
1625 if (m->partial && m->match)
1626 {
1627 rtx newpat, i1;
1628 rtx r1, r2;
1629 /* Find the end of this chain of matching regs.
1630 Thus, we load each reg in the chain from that one reg.
1631 And that reg is loaded with 0 directly,
1632 since it has ->match == 0. */
1633 for (m1 = m; m1->match; m1 = m1->match);
1634 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1635 SET_DEST (PATTERN (m1->insn)));
1636 i1 = emit_insn_before (newpat, loop_start);
1637
1638 /* Mark the moved, invariant reg as being allowed to
1639 share a hard reg with the other matching invariant. */
1640 REG_NOTES (i1) = REG_NOTES (m->insn);
1641 r1 = SET_DEST (PATTERN (m->insn));
1642 r2 = SET_DEST (PATTERN (m1->insn));
1643 regs_may_share = gen_rtx (EXPR_LIST, VOIDmode, r1,
1644 gen_rtx (EXPR_LIST, VOIDmode, r2,
1645 regs_may_share));
1646 delete_insn (m->insn);
1647
1648 if (new_start == 0)
1649 new_start = i1;
1650
1651 if (loop_dump_stream)
1652 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1653 }
1654 /* If we are to re-generate the item being moved with a
1655 new move insn, first delete what we have and then emit
1656 the move insn before the loop. */
1657 else if (m->move_insn)
1658 {
1659 rtx i1, temp;
1660
1661 for (count = m->consec; count >= 0; count--)
1662 {
1663 /* If this is the first insn of a library call sequence,
1664 skip to the end. */
1665 if (GET_CODE (p) != NOTE
5fd8383e 1666 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1667 p = XEXP (temp, 0);
1668
1669 /* If this is the last insn of a libcall sequence, then
1670 delete every insn in the sequence except the last.
1671 The last insn is handled in the normal manner. */
1672 if (GET_CODE (p) != NOTE
5fd8383e 1673 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
b4ad7b23
RS
1674 {
1675 temp = XEXP (temp, 0);
1676 while (temp != p)
1677 temp = delete_insn (temp);
1678 }
1679
1680 p = delete_insn (p);
1681 }
1682
1683 start_sequence ();
1684 emit_move_insn (m->set_dest, m->set_src);
c160c628 1685 temp = get_insns ();
b4ad7b23
RS
1686 end_sequence ();
1687
c160c628
RK
1688 add_label_notes (m->set_src, temp);
1689
1690 i1 = emit_insns_before (temp, loop_start);
5fd8383e 1691 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
b4ad7b23
RS
1692 REG_NOTES (i1)
1693 = gen_rtx (EXPR_LIST,
1694 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1695 m->set_src, REG_NOTES (i1));
1696
1697 if (loop_dump_stream)
1698 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1699
1700 /* The more regs we move, the less we like moving them. */
1701 threshold -= 3;
1702 }
1703 else
1704 {
1705 for (count = m->consec; count >= 0; count--)
1706 {
1707 rtx i1, temp;
1708
1709 /* If first insn of libcall sequence, skip to end. */
1710 /* Do this at start of loop, since p is guaranteed to
1711 be an insn here. */
1712 if (GET_CODE (p) != NOTE
5fd8383e 1713 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1714 p = XEXP (temp, 0);
1715
1716 /* If last insn of libcall sequence, move all
1717 insns except the last before the loop. The last
1718 insn is handled in the normal manner. */
1719 if (GET_CODE (p) != NOTE
5fd8383e 1720 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
b4ad7b23
RS
1721 {
1722 rtx fn_address = 0;
1723 rtx fn_reg = 0;
1724 rtx fn_address_insn = 0;
1725
1726 first = 0;
1727 for (temp = XEXP (temp, 0); temp != p;
1728 temp = NEXT_INSN (temp))
1729 {
1730 rtx body;
1731 rtx n;
1732 rtx next;
1733
1734 if (GET_CODE (temp) == NOTE)
1735 continue;
1736
1737 body = PATTERN (temp);
1738
1739 /* Find the next insn after TEMP,
1740 not counting USE or NOTE insns. */
1741 for (next = NEXT_INSN (temp); next != p;
1742 next = NEXT_INSN (next))
1743 if (! (GET_CODE (next) == INSN
1744 && GET_CODE (PATTERN (next)) == USE)
1745 && GET_CODE (next) != NOTE)
1746 break;
1747
1748 /* If that is the call, this may be the insn
1749 that loads the function address.
1750
1751 Extract the function address from the insn
1752 that loads it into a register.
1753 If this insn was cse'd, we get incorrect code.
1754
1755 So emit a new move insn that copies the
1756 function address into the register that the
1757 call insn will use. flow.c will delete any
1758 redundant stores that we have created. */
1759 if (GET_CODE (next) == CALL_INSN
1760 && GET_CODE (body) == SET
1761 && GET_CODE (SET_DEST (body)) == REG
5fd8383e
RK
1762 && (n = find_reg_note (temp, REG_EQUAL,
1763 NULL_RTX)))
b4ad7b23
RS
1764 {
1765 fn_reg = SET_SRC (body);
1766 if (GET_CODE (fn_reg) != REG)
1767 fn_reg = SET_DEST (body);
1768 fn_address = XEXP (n, 0);
1769 fn_address_insn = temp;
1770 }
1771 /* We have the call insn.
1772 If it uses the register we suspect it might,
1773 load it with the correct address directly. */
1774 if (GET_CODE (temp) == CALL_INSN
1775 && fn_address != 0
d9f8a199 1776 && reg_referenced_p (fn_reg, body))
b4ad7b23
RS
1777 emit_insn_after (gen_move_insn (fn_reg,
1778 fn_address),
1779 fn_address_insn);
1780
1781 if (GET_CODE (temp) == CALL_INSN)
1782 i1 = emit_call_insn_before (body, loop_start);
1783 else
1784 i1 = emit_insn_before (body, loop_start);
1785 if (first == 0)
1786 first = i1;
1787 if (temp == fn_address_insn)
1788 fn_address_insn = i1;
1789 REG_NOTES (i1) = REG_NOTES (temp);
1790 delete_insn (temp);
1791 }
1792 }
1793 if (m->savemode != VOIDmode)
1794 {
1795 /* P sets REG to zero; but we should clear only
1796 the bits that are not covered by the mode
1797 m->savemode. */
1798 rtx reg = m->set_dest;
1799 rtx sequence;
1800 rtx tem;
1801
1802 start_sequence ();
1803 tem = expand_binop
1804 (GET_MODE (reg), and_optab, reg,
5fd8383e
RK
1805 GEN_INT ((((HOST_WIDE_INT) 1
1806 << GET_MODE_BITSIZE (m->savemode)))
b4ad7b23
RS
1807 - 1),
1808 reg, 1, OPTAB_LIB_WIDEN);
1809 if (tem == 0)
1810 abort ();
1811 if (tem != reg)
1812 emit_move_insn (reg, tem);
1813 sequence = gen_sequence ();
1814 end_sequence ();
1815 i1 = emit_insn_before (sequence, loop_start);
1816 }
1817 else if (GET_CODE (p) == CALL_INSN)
1818 i1 = emit_call_insn_before (PATTERN (p), loop_start);
1819 else
1820 i1 = emit_insn_before (PATTERN (p), loop_start);
1821
1822 REG_NOTES (i1) = REG_NOTES (p);
1823
1824 if (new_start == 0)
1825 new_start = i1;
1826
1827 if (loop_dump_stream)
1828 fprintf (loop_dump_stream, " moved to %d",
1829 INSN_UID (i1));
1830
1831#if 0
1832 /* This isn't needed because REG_NOTES is copied
1833 below and is wrong since P might be a PARALLEL. */
1834 if (REG_NOTES (i1) == 0
1835 && ! m->partial /* But not if it's a zero-extend clr. */
1836 && ! m->global /* and not if used outside the loop
1837 (since it might get set outside). */
1838 && CONSTANT_P (SET_SRC (PATTERN (p))))
1839 REG_NOTES (i1)
1840 = gen_rtx (EXPR_LIST, REG_EQUAL,
1841 SET_SRC (PATTERN (p)), REG_NOTES (i1));
1842#endif
1843
1844 /* If library call, now fix the REG_NOTES that contain
1845 insn pointers, namely REG_LIBCALL on FIRST
1846 and REG_RETVAL on I1. */
5fd8383e 1847 if (temp = find_reg_note (i1, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
1848 {
1849 XEXP (temp, 0) = first;
5fd8383e 1850 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
b4ad7b23
RS
1851 XEXP (temp, 0) = i1;
1852 }
1853
1854 delete_insn (p);
1855 do p = NEXT_INSN (p);
1856 while (p && GET_CODE (p) == NOTE);
1857 }
1858
1859 /* The more regs we move, the less we like moving them. */
1860 threshold -= 3;
1861 }
1862
1863 /* Any other movable that loads the same register
1864 MUST be moved. */
1865 already_moved[regno] = 1;
1866
1867 /* This reg has been moved out of one loop. */
1868 moved_once[regno] = 1;
1869
1870 /* The reg set here is now invariant. */
1871 if (! m->partial)
1872 n_times_set[regno] = 0;
1873
1874 m->done = 1;
1875
1876 /* Change the length-of-life info for the register
1877 to say it lives at least the full length of this loop.
1878 This will help guide optimizations in outer loops. */
1879
1880 if (uid_luid[regno_first_uid[regno]] > INSN_LUID (loop_start))
1881 /* This is the old insn before all the moved insns.
1882 We can't use the moved insn because it is out of range
1883 in uid_luid. Only the old insns have luids. */
1884 regno_first_uid[regno] = INSN_UID (loop_start);
1885 if (uid_luid[regno_last_uid[regno]] < INSN_LUID (end))
1886 regno_last_uid[regno] = INSN_UID (end);
1887
1888 /* Combine with this moved insn any other matching movables. */
1889
1890 if (! m->partial)
1891 for (m1 = movables; m1; m1 = m1->next)
1892 if (m1->match == m)
1893 {
1894 rtx temp;
1895
1896 /* Schedule the reg loaded by M1
1897 for replacement so that shares the reg of M.
1898 If the modes differ (only possible in restricted
1899 circumstances, make a SUBREG. */
1900 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
1901 reg_map[m1->regno] = m->set_dest;
1902 else
1903 reg_map[m1->regno]
1904 = gen_lowpart_common (GET_MODE (m1->set_dest),
1905 m->set_dest);
1906
1907 /* Get rid of the matching insn
1908 and prevent further processing of it. */
1909 m1->done = 1;
1910
1911 /* if library call, delete all insn except last, which
1912 is deleted below */
5fd8383e
RK
1913 if (temp = find_reg_note (m1->insn, REG_RETVAL,
1914 NULL_RTX))
b4ad7b23
RS
1915 {
1916 for (temp = XEXP (temp, 0); temp != m1->insn;
1917 temp = NEXT_INSN (temp))
1918 delete_insn (temp);
1919 }
1920 delete_insn (m1->insn);
1921
1922 /* Any other movable that loads the same register
1923 MUST be moved. */
1924 already_moved[m1->regno] = 1;
1925
1926 /* The reg merged here is now invariant,
1927 if the reg it matches is invariant. */
1928 if (! m->partial)
1929 n_times_set[m1->regno] = 0;
1930 }
1931 }
1932 else if (loop_dump_stream)
1933 fprintf (loop_dump_stream, "not desirable");
1934 }
1935 else if (loop_dump_stream && !m->match)
1936 fprintf (loop_dump_stream, "not safe");
1937
1938 if (loop_dump_stream)
1939 fprintf (loop_dump_stream, "\n");
1940 }
1941
1942 if (new_start == 0)
1943 new_start = loop_start;
1944
1945 /* Go through all the instructions in the loop, making
1946 all the register substitutions scheduled in REG_MAP. */
1947 for (p = new_start; p != end; p = NEXT_INSN (p))
1948 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1949 || GET_CODE (p) == CALL_INSN)
1950 {
1951 replace_regs (PATTERN (p), reg_map, nregs, 0);
1952 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
da0c128e 1953 INSN_CODE (p) = -1;
b4ad7b23
RS
1954 }
1955}
1956\f
1957#if 0
1958/* Scan X and replace the address of any MEM in it with ADDR.
1959 REG is the address that MEM should have before the replacement. */
1960
1961static void
1962replace_call_address (x, reg, addr)
1963 rtx x, reg, addr;
1964{
1965 register enum rtx_code code;
1966 register int i;
1967 register char *fmt;
1968
1969 if (x == 0)
1970 return;
1971 code = GET_CODE (x);
1972 switch (code)
1973 {
1974 case PC:
1975 case CC0:
1976 case CONST_INT:
1977 case CONST_DOUBLE:
1978 case CONST:
1979 case SYMBOL_REF:
1980 case LABEL_REF:
1981 case REG:
1982 return;
1983
1984 case SET:
1985 /* Short cut for very common case. */
1986 replace_call_address (XEXP (x, 1), reg, addr);
1987 return;
1988
1989 case CALL:
1990 /* Short cut for very common case. */
1991 replace_call_address (XEXP (x, 0), reg, addr);
1992 return;
1993
1994 case MEM:
1995 /* If this MEM uses a reg other than the one we expected,
1996 something is wrong. */
1997 if (XEXP (x, 0) != reg)
1998 abort ();
1999 XEXP (x, 0) = addr;
2000 return;
2001 }
2002
2003 fmt = GET_RTX_FORMAT (code);
2004 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2005 {
2006 if (fmt[i] == 'e')
2007 replace_call_address (XEXP (x, i), reg, addr);
2008 if (fmt[i] == 'E')
2009 {
2010 register int j;
2011 for (j = 0; j < XVECLEN (x, i); j++)
2012 replace_call_address (XVECEXP (x, i, j), reg, addr);
2013 }
2014 }
2015}
2016#endif
2017\f
2018/* Return the number of memory refs to addresses that vary
2019 in the rtx X. */
2020
2021static int
2022count_nonfixed_reads (x)
2023 rtx x;
2024{
2025 register enum rtx_code code;
2026 register int i;
2027 register char *fmt;
2028 int value;
2029
2030 if (x == 0)
2031 return 0;
2032
2033 code = GET_CODE (x);
2034 switch (code)
2035 {
2036 case PC:
2037 case CC0:
2038 case CONST_INT:
2039 case CONST_DOUBLE:
2040 case CONST:
2041 case SYMBOL_REF:
2042 case LABEL_REF:
2043 case REG:
2044 return 0;
2045
2046 case MEM:
2047 return ((invariant_p (XEXP (x, 0)) != 1)
2048 + count_nonfixed_reads (XEXP (x, 0)));
2049 }
2050
2051 value = 0;
2052 fmt = GET_RTX_FORMAT (code);
2053 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2054 {
2055 if (fmt[i] == 'e')
2056 value += count_nonfixed_reads (XEXP (x, i));
2057 if (fmt[i] == 'E')
2058 {
2059 register int j;
2060 for (j = 0; j < XVECLEN (x, i); j++)
2061 value += count_nonfixed_reads (XVECEXP (x, i, j));
2062 }
2063 }
2064 return value;
2065}
2066
2067\f
2068#if 0
2069/* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2070 Replace it with an instruction to load just the low bytes
2071 if the machine supports such an instruction,
2072 and insert above LOOP_START an instruction to clear the register. */
2073
2074static void
2075constant_high_bytes (p, loop_start)
2076 rtx p, loop_start;
2077{
2078 register rtx new;
2079 register int insn_code_number;
2080
2081 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2082 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2083
2084 new = gen_rtx (SET, VOIDmode,
2085 gen_rtx (STRICT_LOW_PART, VOIDmode,
2086 gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2087 SET_DEST (PATTERN (p)),
2088 0)),
2089 XEXP (SET_SRC (PATTERN (p)), 0));
2090 insn_code_number = recog (new, p);
2091
2092 if (insn_code_number)
2093 {
2094 register int i;
2095
2096 /* Clear destination register before the loop. */
2097 emit_insn_before (gen_rtx (SET, VOIDmode,
2098 SET_DEST (PATTERN (p)),
2099 const0_rtx),
2100 loop_start);
2101
2102 /* Inside the loop, just load the low part. */
2103 PATTERN (p) = new;
2104 }
2105}
2106#endif
2107\f
2108/* Scan a loop setting the variables `unknown_address_altered',
552bc76f
RS
2109 `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2110 and `loop_has_volatile'.
b4ad7b23
RS
2111 Also, fill in the array `loop_store_mems'. */
2112
2113static void
2114prescan_loop (start, end)
2115 rtx start, end;
2116{
2117 register int level = 1;
2118 register rtx insn;
2119
2120 unknown_address_altered = 0;
2121 loop_has_call = 0;
552bc76f 2122 loop_has_volatile = 0;
b4ad7b23
RS
2123 loop_store_mems_idx = 0;
2124
2125 num_mem_sets = 0;
2126 loops_enclosed = 1;
2127 loop_continue = 0;
2128
2129 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2130 insn = NEXT_INSN (insn))
2131 {
2132 if (GET_CODE (insn) == NOTE)
2133 {
2134 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2135 {
2136 ++level;
2137 /* Count number of loops contained in this one. */
2138 loops_enclosed++;
2139 }
2140 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2141 {
2142 --level;
2143 if (level == 0)
2144 {
2145 end = insn;
2146 break;
2147 }
2148 }
2149 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2150 {
2151 if (level == 1)
2152 loop_continue = insn;
2153 }
2154 }
2155 else if (GET_CODE (insn) == CALL_INSN)
2156 {
2157 unknown_address_altered = 1;
2158 loop_has_call = 1;
2159 }
2160 else
2161 {
2162 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
552bc76f
RS
2163 {
2164 if (volatile_refs_p (PATTERN (insn)))
2165 loop_has_volatile = 1;
2166
2167 note_stores (PATTERN (insn), note_addr_stored);
2168 }
b4ad7b23
RS
2169 }
2170 }
2171}
2172\f
2173/* Scan the function looking for loops. Record the start and end of each loop.
2174 Also mark as invalid loops any loops that contain a setjmp or are branched
2175 to from outside the loop. */
2176
2177static void
2178find_and_verify_loops (f)
2179 rtx f;
2180{
034dabc9 2181 rtx insn, label;
b4ad7b23
RS
2182 int current_loop = -1;
2183 int next_loop = -1;
2184 int loop;
2185
2186 /* If there are jumps to undefined labels,
2187 treat them as jumps out of any/all loops.
2188 This also avoids writing past end of tables when there are no loops. */
2189 uid_loop_num[0] = -1;
2190
2191 /* Find boundaries of loops, mark which loops are contained within
2192 loops, and invalidate loops that have setjmp. */
2193
2194 for (insn = f; insn; insn = NEXT_INSN (insn))
2195 {
2196 if (GET_CODE (insn) == NOTE)
2197 switch (NOTE_LINE_NUMBER (insn))
2198 {
2199 case NOTE_INSN_LOOP_BEG:
2200 loop_number_loop_starts[++next_loop] = insn;
2201 loop_number_loop_ends[next_loop] = 0;
2202 loop_outer_loop[next_loop] = current_loop;
2203 loop_invalid[next_loop] = 0;
2204 loop_number_exit_labels[next_loop] = 0;
2205 current_loop = next_loop;
2206 break;
2207
2208 case NOTE_INSN_SETJMP:
2209 /* In this case, we must invalidate our current loop and any
2210 enclosing loop. */
2211 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2212 {
2213 loop_invalid[loop] = 1;
2214 if (loop_dump_stream)
2215 fprintf (loop_dump_stream,
2216 "\nLoop at %d ignored due to setjmp.\n",
2217 INSN_UID (loop_number_loop_starts[loop]));
2218 }
2219 break;
2220
2221 case NOTE_INSN_LOOP_END:
2222 if (current_loop == -1)
2223 abort ();
2224
2225 loop_number_loop_ends[current_loop] = insn;
2226 current_loop = loop_outer_loop[current_loop];
2227 break;
2228
2229 }
2230
2231 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2232 enclosing loop, but this doesn't matter. */
2233 uid_loop_num[INSN_UID (insn)] = current_loop;
2234 }
2235
034dabc9
JW
2236 /* Any loop containing a label used in an initializer must be invalidated,
2237 because it can be jumped into from anywhere. */
2238
2239 for (label = forced_labels; label; label = XEXP (label, 1))
2240 {
2241 int loop_num;
2242
2243 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2244 loop_num != -1;
2245 loop_num = loop_outer_loop[loop_num])
2246 loop_invalid[loop_num] = 1;
2247 }
2248
2249 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2250 loop that it is not contained within, that loop is marked invalid.
2251 If any INSN or CALL_INSN uses a label's address, then the loop containing
2252 that label is marked invalid, because it could be jumped into from
2253 anywhere.
b4ad7b23
RS
2254
2255 Also look for blocks of code ending in an unconditional branch that
2256 exits the loop. If such a block is surrounded by a conditional
2257 branch around the block, move the block elsewhere (see below) and
2258 invert the jump to point to the code block. This may eliminate a
2259 label in our loop and will simplify processing by both us and a
2260 possible second cse pass. */
2261
2262 for (insn = f; insn; insn = NEXT_INSN (insn))
034dabc9 2263 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
b4ad7b23
RS
2264 {
2265 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2266
034dabc9
JW
2267 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2268 {
2269 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2270 if (note)
2271 {
2272 int loop_num;
2273
2274 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2275 loop_num != -1;
2276 loop_num = loop_outer_loop[loop_num])
2277 loop_invalid[loop_num] = 1;
2278 }
2279 }
2280
2281 if (GET_CODE (insn) != JUMP_INSN)
2282 continue;
2283
b4ad7b23
RS
2284 mark_loop_jump (PATTERN (insn), this_loop_num);
2285
2286 /* See if this is an unconditional branch outside the loop. */
2287 if (this_loop_num != -1
2288 && (GET_CODE (PATTERN (insn)) == RETURN
2289 || (simplejump_p (insn)
2290 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
1c01e9df
TW
2291 != this_loop_num)))
2292 && get_max_uid () < max_uid_for_loop)
b4ad7b23
RS
2293 {
2294 rtx p;
2295 rtx our_next = next_real_insn (insn);
2296
2297 /* Go backwards until we reach the start of the loop, a label,
2298 or a JUMP_INSN. */
2299 for (p = PREV_INSN (insn);
2300 GET_CODE (p) != CODE_LABEL
2301 && ! (GET_CODE (p) == NOTE
2302 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2303 && GET_CODE (p) != JUMP_INSN;
2304 p = PREV_INSN (p))
2305 ;
2306
2307 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2308 we have a block of code to try to move.
2309
2310 We look backward and then forward from the target of INSN
2311 to find a BARRIER at the same loop depth as the target.
2312 If we find such a BARRIER, we make a new label for the start
2313 of the block, invert the jump in P and point it to that label,
2314 and move the block of code to the spot we found. */
2315
2316 if (GET_CODE (p) == JUMP_INSN
c6096c5e
RS
2317 && JUMP_LABEL (p) != 0
2318 /* Just ignore jumps to labels that were never emitted.
2319 These always indicate compilation errors. */
2320 && INSN_UID (JUMP_LABEL (p)) != 0
2321 && condjump_p (p)
2322 && ! simplejump_p (p)
2323 && next_real_insn (JUMP_LABEL (p)) == our_next)
b4ad7b23
RS
2324 {
2325 rtx target
2326 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2327 int target_loop_num = uid_loop_num[INSN_UID (target)];
2328 rtx loc;
2329
2330 for (loc = target; loc; loc = PREV_INSN (loc))
2331 if (GET_CODE (loc) == BARRIER
2332 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2333 break;
2334
2335 if (loc == 0)
2336 for (loc = target; loc; loc = NEXT_INSN (loc))
2337 if (GET_CODE (loc) == BARRIER
2338 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2339 break;
2340
2341 if (loc)
2342 {
2343 rtx cond_label = JUMP_LABEL (p);
2344 rtx new_label = get_label_after (p);
2345
2346 /* Ensure our label doesn't go away. */
2347 LABEL_NUSES (cond_label)++;
2348
2349 /* Verify that uid_loop_num is large enough and that
2350 we can invert P. */
1c01e9df 2351 if (invert_jump (p, new_label))
b4ad7b23
RS
2352 {
2353 rtx q, r;
2354
2355 /* Include the BARRIER after INSN and copy the
2356 block after LOC. */
915f619f 2357 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
b4ad7b23
RS
2358 reorder_insns (new_label, NEXT_INSN (insn), loc);
2359
2360 /* All those insns are now in TARGET_LOOP_NUM. */
2361 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2362 q = NEXT_INSN (q))
2363 uid_loop_num[INSN_UID (q)] = target_loop_num;
2364
2365 /* The label jumped to by INSN is no longer a loop exit.
2366 Unless INSN does not have a label (e.g., it is a
2367 RETURN insn), search loop_number_exit_labels to find
2368 its label_ref, and remove it. Also turn off
2369 LABEL_OUTSIDE_LOOP_P bit. */
2370 if (JUMP_LABEL (insn))
2371 {
2372 for (q = 0,
2373 r = loop_number_exit_labels[this_loop_num];
2374 r; q = r, r = LABEL_NEXTREF (r))
2375 if (XEXP (r, 0) == JUMP_LABEL (insn))
2376 {
2377 LABEL_OUTSIDE_LOOP_P (r) = 0;
2378 if (q)
2379 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2380 else
2381 loop_number_exit_labels[this_loop_num]
2382 = LABEL_NEXTREF (r);
2383 break;
2384 }
2385
2386 /* If we didn't find it, then something is wrong. */
2387 if (! r)
2388 abort ();
2389 }
2390
2391 /* P is now a jump outside the loop, so it must be put
2392 in loop_number_exit_labels, and marked as such.
2393 The easiest way to do this is to just call
2394 mark_loop_jump again for P. */
2395 mark_loop_jump (PATTERN (p), this_loop_num);
2396
2397 /* If INSN now jumps to the insn after it,
2398 delete INSN. */
2399 if (JUMP_LABEL (insn) != 0
2400 && (next_real_insn (JUMP_LABEL (insn))
2401 == next_real_insn (insn)))
2402 delete_insn (insn);
2403 }
2404
2405 /* Continue the loop after where the conditional
2406 branch used to jump, since the only branch insn
2407 in the block (if it still remains) is an inter-loop
2408 branch and hence needs no processing. */
2409 insn = NEXT_INSN (cond_label);
2410
2411 if (--LABEL_NUSES (cond_label) == 0)
2412 delete_insn (cond_label);
2413 }
2414 }
2415 }
2416 }
2417}
2418
2419/* If any label in X jumps to a loop different from LOOP_NUM and any of the
2420 loops it is contained in, mark the target loop invalid.
2421
2422 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2423
2424static void
2425mark_loop_jump (x, loop_num)
2426 rtx x;
2427 int loop_num;
2428{
2429 int dest_loop;
2430 int outer_loop;
2431 int i;
2432
2433 switch (GET_CODE (x))
2434 {
2435 case PC:
2436 case USE:
2437 case CLOBBER:
2438 case REG:
2439 case MEM:
2440 case CONST_INT:
2441 case CONST_DOUBLE:
2442 case RETURN:
2443 return;
2444
2445 case CONST:
2446 /* There could be a label reference in here. */
2447 mark_loop_jump (XEXP (x, 0), loop_num);
2448 return;
2449
2450 case PLUS:
2451 case MINUS:
2452 case MULT:
2453 case LSHIFT:
2454 mark_loop_jump (XEXP (x, 0), loop_num);
2455 mark_loop_jump (XEXP (x, 1), loop_num);
2456 return;
2457
2458 case SIGN_EXTEND:
2459 case ZERO_EXTEND:
2460 mark_loop_jump (XEXP (x, 0), loop_num);
2461 return;
2462
2463 case LABEL_REF:
2464 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2465
2466 /* Link together all labels that branch outside the loop. This
2467 is used by final_[bg]iv_value and the loop unrolling code. Also
2468 mark this LABEL_REF so we know that this branch should predict
2469 false. */
2470
2471 if (dest_loop != loop_num && loop_num != -1)
2472 {
2473 LABEL_OUTSIDE_LOOP_P (x) = 1;
2474 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2475 loop_number_exit_labels[loop_num] = x;
2476 }
2477
2478 /* If this is inside a loop, but not in the current loop or one enclosed
2479 by it, it invalidates at least one loop. */
2480
2481 if (dest_loop == -1)
2482 return;
2483
2484 /* We must invalidate every nested loop containing the target of this
2485 label, except those that also contain the jump insn. */
2486
2487 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2488 {
2489 /* Stop when we reach a loop that also contains the jump insn. */
2490 for (outer_loop = loop_num; outer_loop != -1;
2491 outer_loop = loop_outer_loop[outer_loop])
2492 if (dest_loop == outer_loop)
2493 return;
2494
2495 /* If we get here, we know we need to invalidate a loop. */
2496 if (loop_dump_stream && ! loop_invalid[dest_loop])
2497 fprintf (loop_dump_stream,
2498 "\nLoop at %d ignored due to multiple entry points.\n",
2499 INSN_UID (loop_number_loop_starts[dest_loop]));
2500
2501 loop_invalid[dest_loop] = 1;
2502 }
2503 return;
2504
2505 case SET:
2506 /* If this is not setting pc, ignore. */
2507 if (SET_DEST (x) == pc_rtx)
2508 mark_loop_jump (SET_SRC (x), loop_num);
2509 return;
2510
2511 case IF_THEN_ELSE:
2512 mark_loop_jump (XEXP (x, 1), loop_num);
2513 mark_loop_jump (XEXP (x, 2), loop_num);
2514 return;
2515
2516 case PARALLEL:
2517 case ADDR_VEC:
2518 for (i = 0; i < XVECLEN (x, 0); i++)
2519 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2520 return;
2521
2522 case ADDR_DIFF_VEC:
2523 for (i = 0; i < XVECLEN (x, 1); i++)
2524 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2525 return;
2526
2527 default:
2528 /* Nothing else should occur in a JUMP_INSN. */
2529 abort ();
2530 }
2531}
2532\f
2533/* Return nonzero if there is a label in the range from
2534 insn INSN to and including the insn whose luid is END
2535 INSN must have an assigned luid (i.e., it must not have
2536 been previously created by loop.c). */
2537
2538static int
2539labels_in_range_p (insn, end)
2540 rtx insn;
2541 int end;
2542{
2543 while (insn && INSN_LUID (insn) <= end)
2544 {
2545 if (GET_CODE (insn) == CODE_LABEL)
2546 return 1;
2547 insn = NEXT_INSN (insn);
2548 }
2549
2550 return 0;
2551}
2552
2553/* Record that a memory reference X is being set. */
2554
2555static void
2556note_addr_stored (x)
2557 rtx x;
2558{
2559 register int i;
2560
2561 if (x == 0 || GET_CODE (x) != MEM)
2562 return;
2563
2564 /* Count number of memory writes.
2565 This affects heuristics in strength_reduce. */
2566 num_mem_sets++;
2567
2568 if (unknown_address_altered)
2569 return;
2570
2571 for (i = 0; i < loop_store_mems_idx; i++)
2572 if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2573 && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2574 {
2575 /* We are storing at the same address as previously noted. Save the
2576 wider reference, treating BLKmode as wider. */
2577 if (GET_MODE (x) == BLKmode
2578 || (GET_MODE_SIZE (GET_MODE (x))
2579 > GET_MODE_SIZE (GET_MODE (loop_store_mems[i]))))
2580 loop_store_mems[i] = x;
2581 break;
2582 }
2583
2584 if (i == NUM_STORES)
2585 unknown_address_altered = 1;
2586
2587 else if (i == loop_store_mems_idx)
2588 loop_store_mems[loop_store_mems_idx++] = x;
2589}
2590\f
2591/* Return nonzero if the rtx X is invariant over the current loop.
2592
2593 The value is 2 if we refer to something only conditionally invariant.
2594
2595 If `unknown_address_altered' is nonzero, no memory ref is invariant.
2596 Otherwise, a memory ref is invariant if it does not conflict with
2597 anything stored in `loop_store_mems'. */
2598
2599int
2600invariant_p (x)
2601 register rtx x;
2602{
2603 register int i;
2604 register enum rtx_code code;
2605 register char *fmt;
2606 int conditional = 0;
2607
2608 if (x == 0)
2609 return 1;
2610 code = GET_CODE (x);
2611 switch (code)
2612 {
2613 case CONST_INT:
2614 case CONST_DOUBLE:
2615 case SYMBOL_REF:
2616 case CONST:
2617 return 1;
2618
2619 case LABEL_REF:
2620 /* A LABEL_REF is normally invariant, however, if we are unrolling
2621 loops, and this label is inside the loop, then it isn't invariant.
2622 This is because each unrolled copy of the loop body will have
2623 a copy of this label. If this was invariant, then an insn loading
2624 the address of this label into a register might get moved outside
2625 the loop, and then each loop body would end up using the same label.
2626
2627 We don't know the loop bounds here though, so just fail for all
2628 labels. */
2629 if (flag_unroll_loops)
2630 return 0;
2631 else
2632 return 1;
2633
2634 case PC:
2635 case CC0:
2636 case UNSPEC_VOLATILE:
2637 return 0;
2638
2639 case REG:
2640 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2641 since the reg might be set by initialization within the loop. */
2642 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
2643 return 1;
2644 if (loop_has_call
2645 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2646 return 0;
2647 if (n_times_set[REGNO (x)] < 0)
2648 return 2;
2649 return n_times_set[REGNO (x)] == 0;
2650
2651 case MEM:
2652 /* Read-only items (such as constants in a constant pool) are
2653 invariant if their address is. */
2654 if (RTX_UNCHANGING_P (x))
2655 break;
2656
2657 /* If we filled the table (or had a subroutine call), any location
2658 in memory could have been clobbered. */
2659 if (unknown_address_altered
2660 /* Don't mess with volatile memory references. */
2661 || MEM_VOLATILE_P (x))
2662 return 0;
2663
2664 /* See if there is any dependence between a store and this load. */
2665 for (i = loop_store_mems_idx - 1; i >= 0; i--)
2666 if (true_dependence (loop_store_mems[i], x))
2667 return 0;
2668
2669 /* It's not invalidated by a store in memory
2670 but we must still verify the address is invariant. */
2671 break;
2672
2673 case ASM_OPERANDS:
2674 /* Don't mess with insns declared volatile. */
2675 if (MEM_VOLATILE_P (x))
2676 return 0;
2677 }
2678
2679 fmt = GET_RTX_FORMAT (code);
2680 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2681 {
2682 if (fmt[i] == 'e')
2683 {
2684 int tem = invariant_p (XEXP (x, i));
2685 if (tem == 0)
2686 return 0;
2687 if (tem == 2)
2688 conditional = 1;
2689 }
2690 else if (fmt[i] == 'E')
2691 {
2692 register int j;
2693 for (j = 0; j < XVECLEN (x, i); j++)
2694 {
2695 int tem = invariant_p (XVECEXP (x, i, j));
2696 if (tem == 0)
2697 return 0;
2698 if (tem == 2)
2699 conditional = 1;
2700 }
2701
2702 }
2703 }
2704
2705 return 1 + conditional;
2706}
2707
b4ad7b23
RS
2708\f
2709/* Return nonzero if all the insns in the loop that set REG
2710 are INSN and the immediately following insns,
2711 and if each of those insns sets REG in an invariant way
2712 (not counting uses of REG in them).
2713
2714 The value is 2 if some of these insns are only conditionally invariant.
2715
2716 We assume that INSN itself is the first set of REG
2717 and that its source is invariant. */
2718
2719static int
2720consec_sets_invariant_p (reg, n_sets, insn)
2721 int n_sets;
2722 rtx reg, insn;
2723{
2724 register rtx p = insn;
2725 register int regno = REGNO (reg);
2726 rtx temp;
2727 /* Number of sets we have to insist on finding after INSN. */
2728 int count = n_sets - 1;
2729 int old = n_times_set[regno];
2730 int value = 0;
2731 int this;
2732
2733 /* If N_SETS hit the limit, we can't rely on its value. */
2734 if (n_sets == 127)
2735 return 0;
2736
2737 n_times_set[regno] = 0;
2738
2739 while (count > 0)
2740 {
2741 register enum rtx_code code;
2742 rtx set;
2743
2744 p = NEXT_INSN (p);
2745 code = GET_CODE (p);
2746
2747 /* If library call, skip to end of of it. */
5fd8383e 2748 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
2749 p = XEXP (temp, 0);
2750
2751 this = 0;
2752 if (code == INSN
2753 && (set = single_set (p))
2754 && GET_CODE (SET_DEST (set)) == REG
2755 && REGNO (SET_DEST (set)) == regno)
2756 {
2757 this = invariant_p (SET_SRC (set));
2758 if (this != 0)
2759 value |= this;
5fd8383e 2760 else if (temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
b4ad7b23 2761 {
83d90aac
JW
2762 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
2763 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
2764 notes are OK. */
2765 this = (CONSTANT_P (XEXP (temp, 0))
2766 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
2767 && invariant_p (XEXP (temp, 0))));
b4ad7b23
RS
2768 if (this != 0)
2769 value |= this;
2770 }
2771 }
2772 if (this != 0)
2773 count--;
2774 else if (code != NOTE)
2775 {
2776 n_times_set[regno] = old;
2777 return 0;
2778 }
2779 }
2780
2781 n_times_set[regno] = old;
2782 /* If invariant_p ever returned 2, we return 2. */
2783 return 1 + (value & 2);
2784}
2785
2786#if 0
2787/* I don't think this condition is sufficient to allow INSN
2788 to be moved, so we no longer test it. */
2789
2790/* Return 1 if all insns in the basic block of INSN and following INSN
2791 that set REG are invariant according to TABLE. */
2792
2793static int
2794all_sets_invariant_p (reg, insn, table)
2795 rtx reg, insn;
2796 short *table;
2797{
2798 register rtx p = insn;
2799 register int regno = REGNO (reg);
2800
2801 while (1)
2802 {
2803 register enum rtx_code code;
2804 p = NEXT_INSN (p);
2805 code = GET_CODE (p);
2806 if (code == CODE_LABEL || code == JUMP_INSN)
2807 return 1;
2808 if (code == INSN && GET_CODE (PATTERN (p)) == SET
2809 && GET_CODE (SET_DEST (PATTERN (p))) == REG
2810 && REGNO (SET_DEST (PATTERN (p))) == regno)
2811 {
2812 if (!invariant_p (SET_SRC (PATTERN (p)), table))
2813 return 0;
2814 }
2815 }
2816}
2817#endif /* 0 */
2818\f
2819/* Look at all uses (not sets) of registers in X. For each, if it is
2820 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
2821 a different insn, set USAGE[REGNO] to const0_rtx. */
2822
2823static void
2824find_single_use_in_loop (insn, x, usage)
2825 rtx insn;
2826 rtx x;
2827 rtx *usage;
2828{
2829 enum rtx_code code = GET_CODE (x);
2830 char *fmt = GET_RTX_FORMAT (code);
2831 int i, j;
2832
2833 if (code == REG)
2834 usage[REGNO (x)]
2835 = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
2836 ? const0_rtx : insn;
2837
2838 else if (code == SET)
2839 {
2840 /* Don't count SET_DEST if it is a REG; otherwise count things
2841 in SET_DEST because if a register is partially modified, it won't
2842 show up as a potential movable so we don't care how USAGE is set
2843 for it. */
2844 if (GET_CODE (SET_DEST (x)) != REG)
2845 find_single_use_in_loop (insn, SET_DEST (x), usage);
2846 find_single_use_in_loop (insn, SET_SRC (x), usage);
2847 }
2848 else
2849 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2850 {
2851 if (fmt[i] == 'e' && XEXP (x, i) != 0)
2852 find_single_use_in_loop (insn, XEXP (x, i), usage);
2853 else if (fmt[i] == 'E')
2854 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2855 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
2856 }
2857}
2858\f
2859/* Increment N_TIMES_SET at the index of each register
2860 that is modified by an insn between FROM and TO.
2861 If the value of an element of N_TIMES_SET becomes 127 or more,
2862 stop incrementing it, to avoid overflow.
2863
2864 Store in SINGLE_USAGE[I] the single insn in which register I is
2865 used, if it is only used once. Otherwise, it is set to 0 (for no
2866 uses) or const0_rtx for more than one use. This parameter may be zero,
2867 in which case this processing is not done.
2868
2869 Store in *COUNT_PTR the number of actual instruction
2870 in the loop. We use this to decide what is worth moving out. */
2871
2872/* last_set[n] is nonzero iff reg n has been set in the current basic block.
2873 In that case, it is the insn that last set reg n. */
2874
2875static void
2876count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
2877 register rtx from, to;
2878 char *may_not_move;
2879 rtx *single_usage;
2880 int *count_ptr;
2881 int nregs;
2882{
2883 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
2884 register rtx insn;
2885 register int count = 0;
2886 register rtx dest;
2887
2888 bzero (last_set, nregs * sizeof (rtx));
2889 for (insn = from; insn != to; insn = NEXT_INSN (insn))
2890 {
2891 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2892 {
2893 ++count;
2894
2895 /* If requested, record registers that have exactly one use. */
2896 if (single_usage)
2897 {
2898 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
2899
2900 /* Include uses in REG_EQUAL notes. */
2901 if (REG_NOTES (insn))
2902 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
2903 }
2904
2905 if (GET_CODE (PATTERN (insn)) == CLOBBER
2906 && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
2907 /* Don't move a reg that has an explicit clobber.
2908 We might do so sometimes, but it's not worth the pain. */
2909 may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
2910
2911 if (GET_CODE (PATTERN (insn)) == SET
2912 || GET_CODE (PATTERN (insn)) == CLOBBER)
2913 {
2914 dest = SET_DEST (PATTERN (insn));
2915 while (GET_CODE (dest) == SUBREG
2916 || GET_CODE (dest) == ZERO_EXTRACT
2917 || GET_CODE (dest) == SIGN_EXTRACT
2918 || GET_CODE (dest) == STRICT_LOW_PART)
2919 dest = XEXP (dest, 0);
2920 if (GET_CODE (dest) == REG)
2921 {
2922 register int regno = REGNO (dest);
2923 /* If this is the first setting of this reg
2924 in current basic block, and it was set before,
2925 it must be set in two basic blocks, so it cannot
2926 be moved out of the loop. */
2927 if (n_times_set[regno] > 0 && last_set[regno] == 0)
2928 may_not_move[regno] = 1;
2929 /* If this is not first setting in current basic block,
2930 see if reg was used in between previous one and this.
2931 If so, neither one can be moved. */
2932 if (last_set[regno] != 0
2933 && reg_used_between_p (dest, last_set[regno], insn))
2934 may_not_move[regno] = 1;
2935 if (n_times_set[regno] < 127)
2936 ++n_times_set[regno];
2937 last_set[regno] = insn;
2938 }
2939 }
2940 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2941 {
2942 register int i;
2943 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
2944 {
2945 register rtx x = XVECEXP (PATTERN (insn), 0, i);
2946 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
2947 /* Don't move a reg that has an explicit clobber.
2948 It's not worth the pain to try to do it correctly. */
2949 may_not_move[REGNO (XEXP (x, 0))] = 1;
2950
2951 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2952 {
2953 dest = SET_DEST (x);
2954 while (GET_CODE (dest) == SUBREG
2955 || GET_CODE (dest) == ZERO_EXTRACT
2956 || GET_CODE (dest) == SIGN_EXTRACT
2957 || GET_CODE (dest) == STRICT_LOW_PART)
2958 dest = XEXP (dest, 0);
2959 if (GET_CODE (dest) == REG)
2960 {
2961 register int regno = REGNO (dest);
2962 if (n_times_set[regno] > 0 && last_set[regno] == 0)
2963 may_not_move[regno] = 1;
2964 if (last_set[regno] != 0
2965 && reg_used_between_p (dest, last_set[regno], insn))
2966 may_not_move[regno] = 1;
2967 if (n_times_set[regno] < 127)
2968 ++n_times_set[regno];
2969 last_set[regno] = insn;
2970 }
2971 }
2972 }
2973 }
2974 }
2975 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
2976 bzero (last_set, nregs * sizeof (rtx));
2977 }
2978 *count_ptr = count;
2979}
2980\f
2981/* Given a loop that is bounded by LOOP_START and LOOP_END
2982 and that is entered at SCAN_START,
2983 return 1 if the register set in SET contained in insn INSN is used by
2984 any insn that precedes INSN in cyclic order starting
2985 from the loop entry point.
2986
2987 We don't want to use INSN_LUID here because if we restrict INSN to those
2988 that have a valid INSN_LUID, it means we cannot move an invariant out
2989 from an inner loop past two loops. */
2990
2991static int
2992loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
2993 rtx set, insn, loop_start, scan_start, loop_end;
2994{
2995 rtx reg = SET_DEST (set);
2996 rtx p;
2997
2998 /* Scan forward checking for register usage. If we hit INSN, we
2999 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3000 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3001 {
3002 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3003 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3004 return 1;
3005
3006 if (p == loop_end)
3007 p = loop_start;
3008 }
3009
3010 return 0;
3011}
3012\f
3013/* A "basic induction variable" or biv is a pseudo reg that is set
3014 (within this loop) only by incrementing or decrementing it. */
3015/* A "general induction variable" or giv is a pseudo reg whose
3016 value is a linear function of a biv. */
3017
3018/* Bivs are recognized by `basic_induction_var';
3019 Givs by `general_induct_var'. */
3020
3021/* Indexed by register number, indicates whether or not register is an
3022 induction variable, and if so what type. */
3023
3024enum iv_mode *reg_iv_type;
3025
3026/* Indexed by register number, contains pointer to `struct induction'
3027 if register is an induction variable. This holds general info for
3028 all induction variables. */
3029
3030struct induction **reg_iv_info;
3031
3032/* Indexed by register number, contains pointer to `struct iv_class'
3033 if register is a basic induction variable. This holds info describing
3034 the class (a related group) of induction variables that the biv belongs
3035 to. */
3036
3037struct iv_class **reg_biv_class;
3038
3039/* The head of a list which links together (via the next field)
3040 every iv class for the current loop. */
3041
3042struct iv_class *loop_iv_list;
3043
3044/* Communication with routines called via `note_stores'. */
3045
3046static rtx note_insn;
3047
3048/* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3049
3050static rtx addr_placeholder;
3051
3052/* ??? Unfinished optimizations, and possible future optimizations,
3053 for the strength reduction code. */
3054
3055/* ??? There is one more optimization you might be interested in doing: to
3056 allocate pseudo registers for frequently-accessed memory locations.
3057 If the same memory location is referenced each time around, it might
3058 be possible to copy it into a register before and out after.
3059 This is especially useful when the memory location is a variable which
3060 is in a stack slot because somewhere its address is taken. If the
3061 loop doesn't contain a function call and the variable isn't volatile,
3062 it is safe to keep the value in a register for the duration of the
3063 loop. One tricky thing is that the copying of the value back from the
3064 register has to be done on all exits from the loop. You need to check that
3065 all the exits from the loop go to the same place. */
3066
3067/* ??? The interaction of biv elimination, and recognition of 'constant'
3068 bivs, may cause problems. */
3069
3070/* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3071 performance problems.
3072
3073 Perhaps don't eliminate things that can be combined with an addressing
3074 mode. Find all givs that have the same biv, mult_val, and add_val;
3075 then for each giv, check to see if its only use dies in a following
3076 memory address. If so, generate a new memory address and check to see
3077 if it is valid. If it is valid, then store the modified memory address,
3078 otherwise, mark the giv as not done so that it will get its own iv. */
3079
3080/* ??? Could try to optimize branches when it is known that a biv is always
3081 positive. */
3082
3083/* ??? When replace a biv in a compare insn, we should replace with closest
3084 giv so that an optimized branch can still be recognized by the combiner,
3085 e.g. the VAX acb insn. */
3086
3087/* ??? Many of the checks involving uid_luid could be simplified if regscan
3088 was rerun in loop_optimize whenever a register was added or moved.
3089 Also, some of the optimizations could be a little less conservative. */
3090\f
3091/* Perform strength reduction and induction variable elimination. */
3092
3093/* Pseudo registers created during this function will be beyond the last
3094 valid index in several tables including n_times_set and regno_last_uid.
3095 This does not cause a problem here, because the added registers cannot be
3096 givs outside of their loop, and hence will never be reconsidered.
3097 But scan_loop must check regnos to make sure they are in bounds. */
3098
3099static void
3100strength_reduce (scan_start, end, loop_top, insn_count,
3101 loop_start, loop_end)
3102 rtx scan_start;
3103 rtx end;
3104 rtx loop_top;
3105 int insn_count;
3106 rtx loop_start;
3107 rtx loop_end;
3108{
3109 rtx p;
3110 rtx set;
3111 rtx inc_val;
3112 rtx mult_val;
3113 rtx dest_reg;
3114 /* This is 1 if current insn is not executed at least once for every loop
3115 iteration. */
3116 int not_every_iteration = 0;
7dcd3836
RK
3117 /* This is 1 if current insn may be executed more than once for every
3118 loop iteration. */
3119 int maybe_multiple = 0;
b4ad7b23
RS
3120 /* Temporary list pointers for traversing loop_iv_list. */
3121 struct iv_class *bl, **backbl;
3122 /* Ratio of extra register life span we can justify
3123 for saving an instruction. More if loop doesn't call subroutines
3124 since in that case saving an insn makes more difference
3125 and more registers are available. */
3126 /* ??? could set this to last value of threshold in move_movables */
3127 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3128 /* Map of pseudo-register replacements. */
3129 rtx *reg_map;
3130 int call_seen;
3131 rtx test;
3132 rtx end_insert_before;
3133
3134 reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3135 * sizeof (enum iv_mode *));
3136 bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3137 reg_iv_info = (struct induction **)
3138 alloca (max_reg_before_loop * sizeof (struct induction *));
3139 bzero ((char *) reg_iv_info, (max_reg_before_loop
3140 * sizeof (struct induction *)));
3141 reg_biv_class = (struct iv_class **)
3142 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3143 bzero ((char *) reg_biv_class, (max_reg_before_loop
3144 * sizeof (struct iv_class *)));
3145
3146 loop_iv_list = 0;
3147 addr_placeholder = gen_reg_rtx (Pmode);
3148
3149 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3150 must be put before this insn, so that they will appear in the right
b2586fe0 3151 order (i.e. loop order).
b4ad7b23 3152
b2586fe0
JL
3153 If loop_end is the end of the current function, then emit a
3154 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3155 dummy note insn. */
3156 if (NEXT_INSN (loop_end) != 0)
3157 end_insert_before = NEXT_INSN (loop_end);
3158 else
3159 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
b4ad7b23
RS
3160
3161 /* Scan through loop to find all possible bivs. */
3162
3163 p = scan_start;
3164 while (1)
3165 {
3166 p = NEXT_INSN (p);
3167 /* At end of a straight-in loop, we are done.
3168 At end of a loop entered at the bottom, scan the top. */
3169 if (p == scan_start)
3170 break;
3171 if (p == end)
3172 {
3173 if (loop_top != 0)
3174 p = NEXT_INSN (loop_top);
3175 else
3176 break;
3177 if (p == scan_start)
3178 break;
3179 }
3180
3181 if (GET_CODE (p) == INSN
3182 && (set = single_set (p))
3183 && GET_CODE (SET_DEST (set)) == REG)
3184 {
3185 dest_reg = SET_DEST (set);
3186 if (REGNO (dest_reg) < max_reg_before_loop
3187 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3188 && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3189 {
09d7f5a5 3190 if (basic_induction_var (SET_SRC (set), dest_reg, p,
b4ad7b23
RS
3191 &inc_val, &mult_val))
3192 {
3193 /* It is a possible basic induction variable.
3194 Create and initialize an induction structure for it. */
3195
3196 struct induction *v
3197 = (struct induction *) alloca (sizeof (struct induction));
3198
3199 record_biv (v, p, dest_reg, inc_val, mult_val,
7dcd3836 3200 not_every_iteration, maybe_multiple);
b4ad7b23
RS
3201 reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3202 }
3203 else if (REGNO (dest_reg) < max_reg_before_loop)
3204 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3205 }
3206 }
3207
7dcd3836
RK
3208 /* Past CODE_LABEL, we get to insns that may be executed multiple
3209 times. The only way we can be sure that they can't is if every
3210 every jump insn between here and the end of the loop either
3211 returns, exits the loop, or is a forward jump. */
3212
3213 if (GET_CODE (p) == CODE_LABEL)
3214 {
3215 rtx insn = p;
3216
3217 maybe_multiple = 0;
3218
3219 while (1)
3220 {
3221 insn = NEXT_INSN (insn);
3222 if (insn == scan_start)
3223 break;
3224 if (insn == end)
3225 {
3226 if (loop_top != 0)
3227 insn = NEXT_INSN (loop_top);
3228 else
3229 break;
3230 if (insn == scan_start)
3231 break;
3232 }
3233
3234 if (GET_CODE (insn) == JUMP_INSN
3235 && GET_CODE (PATTERN (insn)) != RETURN
3236 && (! condjump_p (insn)
3237 || (JUMP_LABEL (insn) != 0
cdc54cc9
TW
3238 && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3239 || INSN_UID (insn) >= max_uid_for_loop
7dcd3836
RK
3240 || (INSN_LUID (JUMP_LABEL (insn))
3241 < INSN_LUID (insn))))))
3242 {
3243 maybe_multiple = 1;
3244 break;
3245 }
3246 }
3247 }
3248
b4ad7b23
RS
3249 /* Past a label or a jump, we get to insns for which we can't count
3250 on whether or how many times they will be executed during each
3251 iteration. */
3252 /* This code appears in three places, once in scan_loop, and twice
3253 in strength_reduce. */
3254 if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
3255 /* If we enter the loop in the middle, and scan around to the
3256 beginning, don't set not_every_iteration for that.
3257 This can be any kind of jump, since we want to know if insns
3258 will be executed if the loop is executed. */
3259 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
3260 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3261 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3262 not_every_iteration = 1;
3263
3264 /* At the virtual top of a converted loop, insns are again known to
3265 be executed each iteration: logically, the loop begins here
3266 even though the exit code has been duplicated. */
3267
3268 else if (GET_CODE (p) == NOTE
3269 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP)
3270 not_every_iteration = 0;
3271
3272 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3273 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3274 or not an insn is known to be executed each iteration of the
3275 loop, whether or not any iterations are known to occur.
3276
3277 Therefore, if we have just passed a label and have no more labels
3278 between here and the test insn of the loop, we know these insns
3279 will be executed each iteration. This can also happen if we
3280 have just passed a jump, for example, when there are nested loops. */
3281
3282 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3283 && no_labels_between_p (p, loop_end))
3284 not_every_iteration = 0;
3285 }
3286
3287 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3288 Make a sanity check against n_times_set. */
3289 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3290 {
3291 if (reg_iv_type[bl->regno] != BASIC_INDUCT
3292 /* Above happens if register modified by subreg, etc. */
3293 /* Make sure it is not recognized as a basic induction var: */
3294 || n_times_set[bl->regno] != bl->biv_count
3295 /* If never incremented, it is invariant that we decided not to
3296 move. So leave it alone. */
3297 || ! bl->incremented)
3298 {
3299 if (loop_dump_stream)
3300 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3301 bl->regno,
3302 (reg_iv_type[bl->regno] != BASIC_INDUCT
3303 ? "not induction variable"
3304 : (! bl->incremented ? "never incremented"
3305 : "count error")));
3306
3307 reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3308 *backbl = bl->next;
3309 }
3310 else
3311 {
3312 backbl = &bl->next;
3313
3314 if (loop_dump_stream)
3315 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3316 }
3317 }
3318
3319 /* Exit if there are no bivs. */
3320 if (! loop_iv_list)
3321 {
3322 /* Can still unroll the loop anyways, but indicate that there is no
3323 strength reduction info available. */
3324 if (flag_unroll_loops)
3325 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3326
3327 return;
3328 }
3329
3330 /* Find initial value for each biv by searching backwards from loop_start,
3331 halting at first label. Also record any test condition. */
3332
3333 call_seen = 0;
3334 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3335 {
3336 note_insn = p;
3337
3338 if (GET_CODE (p) == CALL_INSN)
3339 call_seen = 1;
3340
3341 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3342 || GET_CODE (p) == CALL_INSN)
3343 note_stores (PATTERN (p), record_initial);
3344
3345 /* Record any test of a biv that branches around the loop if no store
3346 between it and the start of loop. We only care about tests with
3347 constants and registers and only certain of those. */
3348 if (GET_CODE (p) == JUMP_INSN
3349 && JUMP_LABEL (p) != 0
3350 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3351 && (test = get_condition_for_loop (p)) != 0
3352 && GET_CODE (XEXP (test, 0)) == REG
3353 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3354 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3355 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3356 && bl->init_insn == 0)
3357 {
3358 /* If an NE test, we have an initial value! */
3359 if (GET_CODE (test) == NE)
3360 {
3361 bl->init_insn = p;
3362 bl->init_set = gen_rtx (SET, VOIDmode,
3363 XEXP (test, 0), XEXP (test, 1));
3364 }
3365 else
3366 bl->initial_test = test;
3367 }
3368 }
3369
3370 /* Look at the each biv and see if we can say anything better about its
3371 initial value from any initializing insns set up above. (This is done
3372 in two passes to avoid missing SETs in a PARALLEL.) */
3373 for (bl = loop_iv_list; bl; bl = bl->next)
3374 {
3375 rtx src;
3376
3377 if (! bl->init_insn)
3378 continue;
3379
3380 src = SET_SRC (bl->init_set);
3381
3382 if (loop_dump_stream)
3383 fprintf (loop_dump_stream,
3384 "Biv %d initialized at insn %d: initial value ",
3385 bl->regno, INSN_UID (bl->init_insn));
3386
3387 if (valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3388 {
3389 bl->initial_value = src;
3390
3391 if (loop_dump_stream)
3392 {
3393 if (GET_CODE (src) == CONST_INT)
3394 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
3395 else
3396 {
3397 print_rtl (loop_dump_stream, src);
3398 fprintf (loop_dump_stream, "\n");
3399 }
3400 }
3401 }
3402 else
3403 {
3404 /* Biv initial value is not simple move,
d45cf215 3405 so let it keep initial value of "itself". */
b4ad7b23
RS
3406
3407 if (loop_dump_stream)
3408 fprintf (loop_dump_stream, "is complex\n");
3409 }
3410 }
3411
3412 /* Search the loop for general induction variables. */
3413
3414 /* A register is a giv if: it is only set once, it is a function of a
3415 biv and a constant (or invariant), and it is not a biv. */
3416
3417 not_every_iteration = 0;
3418 p = scan_start;
3419 while (1)
3420 {
3421 p = NEXT_INSN (p);
3422 /* At end of a straight-in loop, we are done.
3423 At end of a loop entered at the bottom, scan the top. */
3424 if (p == scan_start)
3425 break;
3426 if (p == end)
3427 {
3428 if (loop_top != 0)
3429 p = NEXT_INSN (loop_top);
3430 else
3431 break;
3432 if (p == scan_start)
3433 break;
3434 }
3435
3436 /* Look for a general induction variable in a register. */
3437 if (GET_CODE (p) == INSN
3438 && (set = single_set (p))
3439 && GET_CODE (SET_DEST (set)) == REG
3440 && ! may_not_optimize[REGNO (SET_DEST (set))])
3441 {
3442 rtx src_reg;
3443 rtx add_val;
3444 rtx mult_val;
3445 int benefit;
3446 rtx regnote = 0;
3447
3448 dest_reg = SET_DEST (set);
3449 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3450 continue;
3451
3452 if (/* SET_SRC is a giv. */
3453 ((benefit = general_induction_var (SET_SRC (set),
3454 &src_reg, &add_val,
3455 &mult_val))
3456 /* Equivalent expression is a giv. */
5fd8383e 3457 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
b4ad7b23
RS
3458 && (benefit = general_induction_var (XEXP (regnote, 0),
3459 &src_reg,
3460 &add_val, &mult_val))))
3461 /* Don't try to handle any regs made by loop optimization.
3462 We have nothing on them in regno_first_uid, etc. */
3463 && REGNO (dest_reg) < max_reg_before_loop
3464 /* Don't recognize a BASIC_INDUCT_VAR here. */
3465 && dest_reg != src_reg
3466 /* This must be the only place where the register is set. */
3467 && (n_times_set[REGNO (dest_reg)] == 1
3468 /* or all sets must be consecutive and make a giv. */
3469 || (benefit = consec_sets_giv (benefit, p,
3470 src_reg, dest_reg,
3471 &add_val, &mult_val))))
3472 {
3473 int count;
3474 struct induction *v
3475 = (struct induction *) alloca (sizeof (struct induction));
3476 rtx temp;
3477
3478 /* If this is a library call, increase benefit. */
5fd8383e 3479 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
3480 benefit += libcall_benefit (p);
3481
3482 /* Skip the consecutive insns, if there are any. */
3483 for (count = n_times_set[REGNO (dest_reg)] - 1;
3484 count > 0; count--)
3485 {
3486 /* If first insn of libcall sequence, skip to end.
3487 Do this at start of loop, since INSN is guaranteed to
3488 be an insn here. */
3489 if (GET_CODE (p) != NOTE
5fd8383e 3490 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
3491 p = XEXP (temp, 0);
3492
3493 do p = NEXT_INSN (p);
3494 while (GET_CODE (p) == NOTE);
3495 }
3496
3497 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
5fd8383e 3498 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
b4ad7b23
RS
3499 loop_end);
3500
3501 }
3502 }
3503
3504#ifndef DONT_REDUCE_ADDR
3505 /* Look for givs which are memory addresses. */
3506 /* This resulted in worse code on a VAX 8600. I wonder if it
3507 still does. */
3508 if (GET_CODE (p) == INSN)
3509 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3510 loop_end);
3511#endif
3512
3513 /* Update the status of whether giv can derive other givs. This can
3514 change when we pass a label or an insn that updates a biv. */
7dcd3836
RK
3515 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3516 || GET_CODE (p) == CODE_LABEL)
b4ad7b23
RS
3517 update_giv_derive (p);
3518
3519 /* Past a label or a jump, we get to insns for which we can't count
3520 on whether or how many times they will be executed during each
3521 iteration. */
3522 /* This code appears in three places, once in scan_loop, and twice
3523 in strength_reduce. */
3524 if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
3525 /* If we enter the loop in the middle, and scan around
3526 to the beginning, don't set not_every_iteration for that.
3527 This can be any kind of jump, since we want to know if insns
3528 will be executed if the loop is executed. */
3529 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
3530 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3531 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3532 not_every_iteration = 1;
3533
3534 /* At the virtual top of a converted loop, insns are again known to
3535 be executed each iteration: logically, the loop begins here
3536 even though the exit code has been duplicated. */
3537
3538 else if (GET_CODE (p) == NOTE
3539 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP)
3540 not_every_iteration = 0;
3541
3542 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3543 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3544 or not an insn is known to be executed each iteration of the
3545 loop, whether or not any iterations are known to occur.
3546
3547 Therefore, if we have just passed a label and have no more labels
3548 between here and the test insn of the loop, we know these insns
3549 will be executed each iteration. */
3550
3551 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3552 && no_labels_between_p (p, loop_end))
3553 not_every_iteration = 0;
3554 }
3555
3556 /* Try to calculate and save the number of loop iterations. This is
3557 set to zero if the actual number can not be calculated. This must
3558 be called after all giv's have been identified, since otherwise it may
3559 fail if the iteration variable is a giv. */
3560
3561 loop_n_iterations = loop_iterations (loop_start, loop_end);
3562
3563 /* Now for each giv for which we still don't know whether or not it is
3564 replaceable, check to see if it is replaceable because its final value
3565 can be calculated. This must be done after loop_iterations is called,
3566 so that final_giv_value will work correctly. */
3567
3568 for (bl = loop_iv_list; bl; bl = bl->next)
3569 {
3570 struct induction *v;
3571
3572 for (v = bl->giv; v; v = v->next_iv)
3573 if (! v->replaceable && ! v->not_replaceable)
3574 check_final_value (v, loop_start, loop_end);
3575 }
3576
3577 /* Try to prove that the loop counter variable (if any) is always
3578 nonnegative; if so, record that fact with a REG_NONNEG note
3579 so that "decrement and branch until zero" insn can be used. */
3580 check_dbra_loop (loop_end, insn_count, loop_start);
3581
3582 /* Create reg_map to hold substitutions for replaceable giv regs. */
3583 reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3584 bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3585
3586 /* Examine each iv class for feasibility of strength reduction/induction
3587 variable elimination. */
3588
3589 for (bl = loop_iv_list; bl; bl = bl->next)
3590 {
3591 struct induction *v;
3592 int benefit;
3593 int all_reduced;
3594 rtx final_value = 0;
3595
3596 /* Test whether it will be possible to eliminate this biv
3597 provided all givs are reduced. This is possible if either
3598 the reg is not used outside the loop, or we can compute
3599 what its final value will be.
3600
3601 For architectures with a decrement_and_branch_until_zero insn,
3602 don't do this if we put a REG_NONNEG note on the endtest for
3603 this biv. */
3604
3605 /* Compare against bl->init_insn rather than loop_start.
3606 We aren't concerned with any uses of the biv between
3607 init_insn and loop_start since these won't be affected
3608 by the value of the biv elsewhere in the function, so
3609 long as init_insn doesn't use the biv itself.
3610 March 14, 1989 -- self@bayes.arc.nasa.gov */
3611
3612 if ((uid_luid[regno_last_uid[bl->regno]] < INSN_LUID (loop_end)
3613 && bl->init_insn
3614 && INSN_UID (bl->init_insn) < max_uid_for_loop
3615 && uid_luid[regno_first_uid[bl->regno]] >= INSN_LUID (bl->init_insn)
3616#ifdef HAVE_decrement_and_branch_until_zero
3617 && ! bl->nonneg
3618#endif
3619 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3620 || ((final_value = final_biv_value (bl, loop_start, loop_end))
3621#ifdef HAVE_decrement_and_branch_until_zero
3622 && ! bl->nonneg
3623#endif
3624 ))
3625 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3626 threshold, insn_count);
3627 else
3628 {
3629 if (loop_dump_stream)
3630 {
3631 fprintf (loop_dump_stream,
3632 "Cannot eliminate biv %d.\n",
3633 bl->regno);
3634 fprintf (loop_dump_stream,
3635 "First use: insn %d, last use: insn %d.\n",
3636 regno_first_uid[bl->regno],
3637 regno_last_uid[bl->regno]);
3638 }
3639 }
3640
3641 /* Combine all giv's for this iv_class. */
3642 combine_givs (bl);
3643
3644 /* This will be true at the end, if all givs which depend on this
3645 biv have been strength reduced.
3646 We can't (currently) eliminate the biv unless this is so. */
3647 all_reduced = 1;
3648
3649 /* Check each giv in this class to see if we will benefit by reducing
3650 it. Skip giv's combined with others. */
3651 for (v = bl->giv; v; v = v->next_iv)
3652 {
3653 struct induction *tv;
3654
3655 if (v->ignore || v->same)
3656 continue;
3657
3658 benefit = v->benefit;
3659
3660 /* Reduce benefit if not replaceable, since we will insert
3661 a move-insn to replace the insn that calculates this giv.
3662 Don't do this unless the giv is a user variable, since it
3663 will often be marked non-replaceable because of the duplication
3664 of the exit code outside the loop. In such a case, the copies
3665 we insert are dead and will be deleted. So they don't have
3666 a cost. Similar situations exist. */
3667 /* ??? The new final_[bg]iv_value code does a much better job
3668 of finding replaceable giv's, and hence this code may no longer
3669 be necessary. */
3670 if (! v->replaceable && ! bl->eliminable
3671 && REG_USERVAR_P (v->dest_reg))
3672 benefit -= copy_cost;
3673
3674 /* Decrease the benefit to count the add-insns that we will
3675 insert to increment the reduced reg for the giv. */
3676 benefit -= add_cost * bl->biv_count;
3677
3678 /* Decide whether to strength-reduce this giv or to leave the code
3679 unchanged (recompute it from the biv each time it is used).
3680 This decision can be made independently for each giv. */
3681
3682 /* ??? Perhaps attempt to guess whether autoincrement will handle
3683 some of the new add insns; if so, can increase BENEFIT
3684 (undo the subtraction of add_cost that was done above). */
3685
3686 /* If an insn is not to be strength reduced, then set its ignore
3687 flag, and clear all_reduced. */
3688
3689 if (v->lifetime * threshold * benefit < insn_count)
3690 {
3691 if (loop_dump_stream)
3692 fprintf (loop_dump_stream,
3693 "giv of insn %d not worth while, %d vs %d.\n",
3694 INSN_UID (v->insn),
3695 v->lifetime * threshold * benefit, insn_count);
3696 v->ignore = 1;
3697 all_reduced = 0;
3698 }
3699 else
3700 {
3701 /* Check that we can increment the reduced giv without a
3702 multiply insn. If not, reject it. */
3703
3704 for (tv = bl->biv; tv; tv = tv->next_iv)
3705 if (tv->mult_val == const1_rtx
3706 && ! product_cheap_p (tv->add_val, v->mult_val))
3707 {
3708 if (loop_dump_stream)
3709 fprintf (loop_dump_stream,
3710 "giv of insn %d: would need a multiply.\n",
3711 INSN_UID (v->insn));
3712 v->ignore = 1;
3713 all_reduced = 0;
3714 break;
3715 }
3716 }
3717 }
3718
3719 /* Reduce each giv that we decided to reduce. */
3720
3721 for (v = bl->giv; v; v = v->next_iv)
3722 {
3723 struct induction *tv;
3724 if (! v->ignore && v->same == 0)
3725 {
3726 v->new_reg = gen_reg_rtx (v->mode);
3727
3728 /* For each place where the biv is incremented,
3729 add an insn to increment the new, reduced reg for the giv. */
3730 for (tv = bl->biv; tv; tv = tv->next_iv)
3731 {
3732 if (tv->mult_val == const1_rtx)
3733 emit_iv_add_mult (tv->add_val, v->mult_val,
3734 v->new_reg, v->new_reg, tv->insn);
3735 else /* tv->mult_val == const0_rtx */
3736 /* A multiply is acceptable here
3737 since this is presumed to be seldom executed. */
3738 emit_iv_add_mult (tv->add_val, v->mult_val,
3739 v->add_val, v->new_reg, tv->insn);
3740 }
3741
3742 /* Add code at loop start to initialize giv's reduced reg. */
3743
3744 emit_iv_add_mult (bl->initial_value, v->mult_val,
3745 v->add_val, v->new_reg, loop_start);
3746 }
3747 }
3748
3749 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
3750 as not reduced.
3751
3752 For each giv register that can be reduced now: if replaceable,
3753 substitute reduced reg wherever the old giv occurs;
3754 else add new move insn "giv_reg = reduced_reg".
3755
3756 Also check for givs whose first use is their definition and whose
3757 last use is the definition of another giv. If so, it is likely
3758 dead and should not be used to eliminate a biv. */
3759 for (v = bl->giv; v; v = v->next_iv)
3760 {
3761 if (v->same && v->same->ignore)
3762 v->ignore = 1;
3763
3764 if (v->ignore)
3765 continue;
3766
3767 if (v->giv_type == DEST_REG
3768 && regno_first_uid[REGNO (v->dest_reg)] == INSN_UID (v->insn))
3769 {
3770 struct induction *v1;
3771
3772 for (v1 = bl->giv; v1; v1 = v1->next_iv)
3773 if (regno_last_uid[REGNO (v->dest_reg)] == INSN_UID (v1->insn))
3774 v->maybe_dead = 1;
3775 }
3776
3777 /* Update expression if this was combined, in case other giv was
3778 replaced. */
3779 if (v->same)
3780 v->new_reg = replace_rtx (v->new_reg,
3781 v->same->dest_reg, v->same->new_reg);
3782
3783 if (v->giv_type == DEST_ADDR)
3784 /* Store reduced reg as the address in the memref where we found
3785 this giv. */
3786 *v->location = v->new_reg;
3787 else if (v->replaceable)
3788 {
3789 reg_map[REGNO (v->dest_reg)] = v->new_reg;
3790
3791#if 0
3792 /* I can no longer duplicate the original problem. Perhaps
3793 this is unnecessary now? */
3794
3795 /* Replaceable; it isn't strictly necessary to delete the old
3796 insn and emit a new one, because v->dest_reg is now dead.
3797
3798 However, especially when unrolling loops, the special
3799 handling for (set REG0 REG1) in the second cse pass may
3800 make v->dest_reg live again. To avoid this problem, emit
3801 an insn to set the original giv reg from the reduced giv.
3802 We can not delete the original insn, since it may be part
3803 of a LIBCALL, and the code in flow that eliminates dead
3804 libcalls will fail if it is deleted. */
3805 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
3806 v->insn);
3807#endif
3808 }
3809 else
3810 {
3811 /* Not replaceable; emit an insn to set the original giv reg from
3812 the reduced giv, same as above. */
3813 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
3814 v->insn);
3815 }
3816
3817 /* When a loop is reversed, givs which depend on the reversed
3818 biv, and which are live outside the loop, must be set to their
3819 correct final value. This insn is only needed if the giv is
3820 not replaceable. The correct final value is the same as the
3821 value that the giv starts the reversed loop with. */
3822 if (bl->reversed && ! v->replaceable)
3823 emit_iv_add_mult (bl->initial_value, v->mult_val,
3824 v->add_val, v->dest_reg, end_insert_before);
3825 else if (v->final_value)
3826 {
3827 rtx insert_before;
3828
3829 /* If the loop has multiple exits, emit the insn before the
3830 loop to ensure that it will always be executed no matter
3831 how the loop exits. Otherwise, emit the insn after the loop,
3832 since this is slightly more efficient. */
3833 if (loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
3834 insert_before = loop_start;
3835 else
3836 insert_before = end_insert_before;
3837 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
3838 insert_before);
3839
3840#if 0
3841 /* If the insn to set the final value of the giv was emitted
3842 before the loop, then we must delete the insn inside the loop
3843 that sets it. If this is a LIBCALL, then we must delete
3844 every insn in the libcall. Note, however, that
3845 final_giv_value will only succeed when there are multiple
3846 exits if the giv is dead at each exit, hence it does not
3847 matter that the original insn remains because it is dead
3848 anyways. */
3849 /* Delete the insn inside the loop that sets the giv since
3850 the giv is now set before (or after) the loop. */
3851 delete_insn (v->insn);
3852#endif
3853 }
3854
3855 if (loop_dump_stream)
3856 {
3857 fprintf (loop_dump_stream, "giv at %d reduced to ",
3858 INSN_UID (v->insn));
3859 print_rtl (loop_dump_stream, v->new_reg);
3860 fprintf (loop_dump_stream, "\n");
3861 }
3862 }
3863
3864 /* All the givs based on the biv bl have been reduced if they
3865 merit it. */
3866
3867 /* For each giv not marked as maybe dead that has been combined with a
3868 second giv, clear any "maybe dead" mark on that second giv.
3869 v->new_reg will either be or refer to the register of the giv it
3870 combined with.
3871
3872 Doing this clearing avoids problems in biv elimination where a
3873 giv's new_reg is a complex value that can't be put in the insn but
3874 the giv combined with (with a reg as new_reg) is marked maybe_dead.
3875 Since the register will be used in either case, we'd prefer it be
3876 used from the simpler giv. */
3877
3878 for (v = bl->giv; v; v = v->next_iv)
3879 if (! v->maybe_dead && v->same)
3880 v->same->maybe_dead = 0;
3881
3882 /* Try to eliminate the biv, if it is a candidate.
3883 This won't work if ! all_reduced,
3884 since the givs we planned to use might not have been reduced.
3885
d45cf215 3886 We have to be careful that we didn't initially think we could eliminate
b4ad7b23
RS
3887 this biv because of a giv that we now think may be dead and shouldn't
3888 be used as a biv replacement.
3889
3890 Also, there is the possibility that we may have a giv that looks
3891 like it can be used to eliminate a biv, but the resulting insn
3892 isn't valid. This can happen, for example, on the 88k, where a
3893 JUMP_INSN can compare a register only with zero. Attempts to
c5b7917e 3894 replace it with a compare with a constant will fail.
b4ad7b23
RS
3895
3896 Note that in cases where this call fails, we may have replaced some
3897 of the occurrences of the biv with a giv, but no harm was done in
3898 doing so in the rare cases where it can occur. */
3899
3900 if (all_reduced == 1 && bl->eliminable
3901 && maybe_eliminate_biv (bl, loop_start, end, 1,
3902 threshold, insn_count))
3903
3904 {
3905 /* ?? If we created a new test to bypass the loop entirely,
3906 or otherwise drop straight in, based on this test, then
3907 we might want to rewrite it also. This way some later
3908 pass has more hope of removing the initialization of this
3909 biv entirely. */
3910
3911 /* If final_value != 0, then the biv may be used after loop end
3912 and we must emit an insn to set it just in case.
3913
3914 Reversed bivs already have an insn after the loop setting their
3915 value, so we don't need another one. We can't calculate the
3916 proper final value for such a biv here anyways. */
3917 if (final_value != 0 && ! bl->reversed)
3918 {
3919 rtx insert_before;
3920
3921 /* If the loop has multiple exits, emit the insn before the
3922 loop to ensure that it will always be executed no matter
3923 how the loop exits. Otherwise, emit the insn after the
3924 loop, since this is slightly more efficient. */
3925 if (loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
3926 insert_before = loop_start;
3927 else
3928 insert_before = end_insert_before;
3929
3930 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
3931 end_insert_before);
3932 }
3933
3934#if 0
3935 /* Delete all of the instructions inside the loop which set
3936 the biv, as they are all dead. If is safe to delete them,
3937 because an insn setting a biv will never be part of a libcall. */
3938 /* However, deleting them will invalidate the regno_last_uid info,
3939 so keeping them around is more convenient. Final_biv_value
3940 will only succeed when there are multiple exits if the biv
3941 is dead at each exit, hence it does not matter that the original
3942 insn remains, because it is dead anyways. */
3943 for (v = bl->biv; v; v = v->next_iv)
3944 delete_insn (v->insn);
3945#endif
3946
3947 if (loop_dump_stream)
3948 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
3949 bl->regno);
3950 }
3951 }
3952
3953 /* Go through all the instructions in the loop, making all the
3954 register substitutions scheduled in REG_MAP. */
3955
3956 for (p = loop_start; p != end; p = NEXT_INSN (p))
3957 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3958 || GET_CODE (p) == CALL_INSN)
3959 {
3960 replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
3961 replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
da0c128e 3962 INSN_CODE (p) = -1;
b4ad7b23
RS
3963 }
3964
3965 /* Unroll loops from within strength reduction so that we can use the
3966 induction variable information that strength_reduce has already
3967 collected. */
3968
3969 if (flag_unroll_loops)
3970 unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
3971
3972 if (loop_dump_stream)
3973 fprintf (loop_dump_stream, "\n");
3974}
3975\f
3976/* Return 1 if X is a valid source for an initial value (or as value being
3977 compared against in an initial test).
3978
3979 X must be either a register or constant and must not be clobbered between
3980 the current insn and the start of the loop.
3981
3982 INSN is the insn containing X. */
3983
3984static int
3985valid_initial_value_p (x, insn, call_seen, loop_start)
3986 rtx x;
3987 rtx insn;
3988 int call_seen;
3989 rtx loop_start;
3990{
3991 if (CONSTANT_P (x))
3992 return 1;
3993
d45cf215 3994 /* Only consider pseudos we know about initialized in insns whose luids
b4ad7b23
RS
3995 we know. */
3996 if (GET_CODE (x) != REG
3997 || REGNO (x) >= max_reg_before_loop)
3998 return 0;
3999
4000 /* Don't use call-clobbered registers across a call which clobbers it. On
4001 some machines, don't use any hard registers at all. */
4002 if (REGNO (x) < FIRST_PSEUDO_REGISTER
4003#ifndef SMALL_REGISTER_CLASSES
4004 && call_used_regs[REGNO (x)] && call_seen
4005#endif
4006 )
4007 return 0;
4008
4009 /* Don't use registers that have been clobbered before the start of the
4010 loop. */
4011 if (reg_set_between_p (x, insn, loop_start))
4012 return 0;
4013
4014 return 1;
4015}
4016\f
4017/* Scan X for memory refs and check each memory address
4018 as a possible giv. INSN is the insn whose pattern X comes from.
4019 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4020 every loop iteration. */
4021
4022static void
4023find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4024 rtx x;
4025 rtx insn;
4026 int not_every_iteration;
4027 rtx loop_start, loop_end;
4028{
4029 register int i, j;
4030 register enum rtx_code code;
4031 register char *fmt;
4032
4033 if (x == 0)
4034 return;
4035
4036 code = GET_CODE (x);
4037 switch (code)
4038 {
4039 case REG:
4040 case CONST_INT:
4041 case CONST:
4042 case CONST_DOUBLE:
4043 case SYMBOL_REF:
4044 case LABEL_REF:
4045 case PC:
4046 case CC0:
4047 case ADDR_VEC:
4048 case ADDR_DIFF_VEC:
4049 case USE:
4050 case CLOBBER:
4051 return;
4052
4053 case MEM:
4054 {
4055 rtx src_reg;
4056 rtx add_val;
4057 rtx mult_val;
4058 int benefit;
4059
4060 benefit = general_induction_var (XEXP (x, 0),
4061 &src_reg, &add_val, &mult_val);
4062
4063 /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4064 Such a giv isn't useful. */
4065 if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4066 {
4067 /* Found one; record it. */
4068 struct induction *v
4069 = (struct induction *) oballoc (sizeof (struct induction));
4070
4071 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4072 add_val, benefit, DEST_ADDR, not_every_iteration,
4073 &XEXP (x, 0), loop_start, loop_end);
4074
4075 v->mem_mode = GET_MODE (x);
4076 }
4077 return;
4078 }
4079 }
4080
4081 /* Recursively scan the subexpressions for other mem refs. */
4082
4083 fmt = GET_RTX_FORMAT (code);
4084 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4085 if (fmt[i] == 'e')
4086 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4087 loop_end);
4088 else if (fmt[i] == 'E')
4089 for (j = 0; j < XVECLEN (x, i); j++)
4090 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4091 loop_start, loop_end);
4092}
4093\f
4094/* Fill in the data about one biv update.
4095 V is the `struct induction' in which we record the biv. (It is
4096 allocated by the caller, with alloca.)
4097 INSN is the insn that sets it.
4098 DEST_REG is the biv's reg.
4099
4100 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4101 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
7dcd3836
RK
4102 being set to INC_VAL.
4103
4104 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4105 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4106 can be executed more than once per iteration. If MAYBE_MULTIPLE
4107 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4108 executed exactly once per iteration. */
b4ad7b23
RS
4109
4110static void
7dcd3836
RK
4111record_biv (v, insn, dest_reg, inc_val, mult_val,
4112 not_every_iteration, maybe_multiple)
b4ad7b23
RS
4113 struct induction *v;
4114 rtx insn;
4115 rtx dest_reg;
4116 rtx inc_val;
4117 rtx mult_val;
4118 int not_every_iteration;
7dcd3836 4119 int maybe_multiple;
b4ad7b23
RS
4120{
4121 struct iv_class *bl;
4122
4123 v->insn = insn;
4124 v->src_reg = dest_reg;
4125 v->dest_reg = dest_reg;
4126 v->mult_val = mult_val;
4127 v->add_val = inc_val;
4128 v->mode = GET_MODE (dest_reg);
4129 v->always_computable = ! not_every_iteration;
7dcd3836 4130 v->maybe_multiple = maybe_multiple;
b4ad7b23
RS
4131
4132 /* Add this to the reg's iv_class, creating a class
4133 if this is the first incrementation of the reg. */
4134
4135 bl = reg_biv_class[REGNO (dest_reg)];
4136 if (bl == 0)
4137 {
4138 /* Create and initialize new iv_class. */
4139
4140 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4141
4142 bl->regno = REGNO (dest_reg);
4143 bl->biv = 0;
4144 bl->giv = 0;
4145 bl->biv_count = 0;
4146 bl->giv_count = 0;
4147
4148 /* Set initial value to the reg itself. */
4149 bl->initial_value = dest_reg;
c5b7917e 4150 /* We haven't seen the initializing insn yet */
b4ad7b23
RS
4151 bl->init_insn = 0;
4152 bl->init_set = 0;
4153 bl->initial_test = 0;
4154 bl->incremented = 0;
4155 bl->eliminable = 0;
4156 bl->nonneg = 0;
4157 bl->reversed = 0;
b5d27be7 4158 bl->total_benefit = 0;
b4ad7b23
RS
4159
4160 /* Add this class to loop_iv_list. */
4161 bl->next = loop_iv_list;
4162 loop_iv_list = bl;
4163
4164 /* Put it in the array of biv register classes. */
4165 reg_biv_class[REGNO (dest_reg)] = bl;
4166 }
4167
4168 /* Update IV_CLASS entry for this biv. */
4169 v->next_iv = bl->biv;
4170 bl->biv = v;
4171 bl->biv_count++;
4172 if (mult_val == const1_rtx)
4173 bl->incremented = 1;
4174
4175 if (loop_dump_stream)
4176 {
4177 fprintf (loop_dump_stream,
4178 "Insn %d: possible biv, reg %d,",
4179 INSN_UID (insn), REGNO (dest_reg));
4180 if (GET_CODE (inc_val) == CONST_INT)
4181 fprintf (loop_dump_stream, " const = %d\n",
4182 INTVAL (inc_val));
4183 else
4184 {
4185 fprintf (loop_dump_stream, " const = ");
4186 print_rtl (loop_dump_stream, inc_val);
4187 fprintf (loop_dump_stream, "\n");
4188 }
4189 }
4190}
4191\f
4192/* Fill in the data about one giv.
4193 V is the `struct induction' in which we record the giv. (It is
4194 allocated by the caller, with alloca.)
4195 INSN is the insn that sets it.
4196 BENEFIT estimates the savings from deleting this insn.
4197 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4198 into a register or is used as a memory address.
4199
4200 SRC_REG is the biv reg which the giv is computed from.
4201 DEST_REG is the giv's reg (if the giv is stored in a reg).
4202 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4203 LOCATION points to the place where this giv's value appears in INSN. */
4204
4205static void
4206record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4207 type, not_every_iteration, location, loop_start, loop_end)
4208 struct induction *v;
4209 rtx insn;
4210 rtx src_reg;
4211 rtx dest_reg;
4212 rtx mult_val, add_val;
4213 int benefit;
4214 enum g_types type;
4215 int not_every_iteration;
4216 rtx *location;
4217 rtx loop_start, loop_end;
4218{
4219 struct induction *b;
4220 struct iv_class *bl;
4221 rtx set = single_set (insn);
4222 rtx p;
4223
4224 v->insn = insn;
4225 v->src_reg = src_reg;
4226 v->giv_type = type;
4227 v->dest_reg = dest_reg;
4228 v->mult_val = mult_val;
4229 v->add_val = add_val;
4230 v->benefit = benefit;
4231 v->location = location;
4232 v->cant_derive = 0;
4233 v->combined_with = 0;
7dcd3836 4234 v->maybe_multiple = 0;
b4ad7b23
RS
4235 v->maybe_dead = 0;
4236 v->derive_adjustment = 0;
4237 v->same = 0;
4238 v->ignore = 0;
4239 v->new_reg = 0;
4240 v->final_value = 0;
4241
4242 /* The v->always_computable field is used in update_giv_derive, to
4243 determine whether a giv can be used to derive another giv. For a
4244 DEST_REG giv, INSN computes a new value for the giv, so its value
4245 isn't computable if INSN insn't executed every iteration.
4246 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4247 it does not compute a new value. Hence the value is always computable
d45cf215 4248 regardless of whether INSN is executed each iteration. */
b4ad7b23
RS
4249
4250 if (type == DEST_ADDR)
4251 v->always_computable = 1;
4252 else
4253 v->always_computable = ! not_every_iteration;
4254
4255 if (type == DEST_ADDR)
4256 {
4257 v->mode = GET_MODE (*location);
4258 v->lifetime = 1;
4259 v->times_used = 1;
4260 }
4261 else /* type == DEST_REG */
4262 {
4263 v->mode = GET_MODE (SET_DEST (set));
4264
4265 v->lifetime = (uid_luid[regno_last_uid[REGNO (dest_reg)]]
4266 - uid_luid[regno_first_uid[REGNO (dest_reg)]]);
4267
4268 v->times_used = n_times_used[REGNO (dest_reg)];
4269
4270 /* If the lifetime is zero, it means that this register is
4271 really a dead store. So mark this as a giv that can be
4272 ignored. This will not prevent the biv from being eliminated. */
4273 if (v->lifetime == 0)
4274 v->ignore = 1;
4275
4276 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4277 reg_iv_info[REGNO (dest_reg)] = v;
4278 }
4279
4280 /* Add the giv to the class of givs computed from one biv. */
4281
4282 bl = reg_biv_class[REGNO (src_reg)];
4283 if (bl)
4284 {
4285 v->next_iv = bl->giv;
4286 bl->giv = v;
4287 /* Don't count DEST_ADDR. This is supposed to count the number of
4288 insns that calculate givs. */
4289 if (type == DEST_REG)
4290 bl->giv_count++;
4291 bl->total_benefit += benefit;
4292 }
4293 else
4294 /* Fatal error, biv missing for this giv? */
4295 abort ();
4296
4297 if (type == DEST_ADDR)
4298 v->replaceable = 1;
4299 else
4300 {
4301 /* The giv can be replaced outright by the reduced register only if all
4302 of the following conditions are true:
4303 - the insn that sets the giv is always executed on any iteration
4304 on which the giv is used at all
4305 (there are two ways to deduce this:
4306 either the insn is executed on every iteration,
4307 or all uses follow that insn in the same basic block),
4308 - the giv is not used outside the loop
4309 - no assignments to the biv occur during the giv's lifetime. */
4310
4311 if (regno_first_uid[REGNO (dest_reg)] == INSN_UID (insn)
4312 /* Previous line always fails if INSN was moved by loop opt. */
4313 && uid_luid[regno_last_uid[REGNO (dest_reg)]] < INSN_LUID (loop_end)
4314 && (! not_every_iteration
4315 || last_use_this_basic_block (dest_reg, insn)))
4316 {
4317 /* Now check that there are no assignments to the biv within the
4318 giv's lifetime. This requires two separate checks. */
4319
4320 /* Check each biv update, and fail if any are between the first
4321 and last use of the giv.
4322
4323 If this loop contains an inner loop that was unrolled, then
4324 the insn modifying the biv may have been emitted by the loop
4325 unrolling code, and hence does not have a valid luid. Just
4326 mark the biv as not replaceable in this case. It is not very
4327 useful as a biv, because it is used in two different loops.
4328 It is very unlikely that we would be able to optimize the giv
4329 using this biv anyways. */
4330
4331 v->replaceable = 1;
4332 for (b = bl->biv; b; b = b->next_iv)
4333 {
4334 if (INSN_UID (b->insn) >= max_uid_for_loop
4335 || ((uid_luid[INSN_UID (b->insn)]
4336 >= uid_luid[regno_first_uid[REGNO (dest_reg)]])
4337 && (uid_luid[INSN_UID (b->insn)]
4338 <= uid_luid[regno_last_uid[REGNO (dest_reg)]])))
4339 {
4340 v->replaceable = 0;
4341 v->not_replaceable = 1;
4342 break;
4343 }
4344 }
4345
4346 /* Check each insn between the first and last use of the giv,
4347 and fail if any of them are branches that jump to a named label
4348 outside this range, but still inside the loop. This catches
4349 cases of spaghetti code where the execution order of insns
4350 is not linear, and hence the above test fails. For example,
4351 in the following code, j is not replaceable:
4352 for (i = 0; i < 100; ) {
4353 L0: j = 4*i; goto L1;
4354 L2: k = j; goto L3;
4355 L1: i++; goto L2;
4356 L3: ; }
4357 printf ("k = %d\n", k); }
4358 This test is conservative, but this test succeeds rarely enough
4359 that it isn't a problem. See also check_final_value below. */
4360
4361 if (v->replaceable)
4362 for (p = insn;
4363 INSN_UID (p) >= max_uid_for_loop
4364 || INSN_LUID (p) < uid_luid[regno_last_uid[REGNO (dest_reg)]];
4365 p = NEXT_INSN (p))
4366 {
4367 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4368 && LABEL_NAME (JUMP_LABEL (p))
4369 && ((INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start)
4370 && (INSN_LUID (JUMP_LABEL (p))
4371 < uid_luid[regno_first_uid[REGNO (dest_reg)]]))
4372 || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end)
4373 && (INSN_LUID (JUMP_LABEL (p))
4374 > uid_luid[regno_last_uid[REGNO (dest_reg)]]))))
4375 {
4376 v->replaceable = 0;
4377 v->not_replaceable = 1;
4378
4379 if (loop_dump_stream)
4380 fprintf (loop_dump_stream,
4381 "Found branch outside giv lifetime.\n");
4382
4383 break;
4384 }
4385 }
4386 }
4387 else
4388 {
4389 /* May still be replaceable, we don't have enough info here to
4390 decide. */
4391 v->replaceable = 0;
4392 v->not_replaceable = 0;
4393 }
4394 }
4395
4396 if (loop_dump_stream)
4397 {
4398 if (type == DEST_REG)
4399 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4400 INSN_UID (insn), REGNO (dest_reg));
4401 else
4402 fprintf (loop_dump_stream, "Insn %d: dest address",
4403 INSN_UID (insn));
4404
4405 fprintf (loop_dump_stream, " src reg %d benefit %d",
4406 REGNO (src_reg), v->benefit);
4407 fprintf (loop_dump_stream, " used %d lifetime %d",
4408 v->times_used, v->lifetime);
4409
4410 if (v->replaceable)
4411 fprintf (loop_dump_stream, " replaceable");
4412
4413 if (GET_CODE (mult_val) == CONST_INT)
4414 fprintf (loop_dump_stream, " mult %d",
4415 INTVAL (mult_val));
4416 else
4417 {
4418 fprintf (loop_dump_stream, " mult ");
4419 print_rtl (loop_dump_stream, mult_val);
4420 }
4421
4422 if (GET_CODE (add_val) == CONST_INT)
4423 fprintf (loop_dump_stream, " add %d",
4424 INTVAL (add_val));
4425 else
4426 {
4427 fprintf (loop_dump_stream, " add ");
4428 print_rtl (loop_dump_stream, add_val);
4429 }
4430 }
4431
4432 if (loop_dump_stream)
4433 fprintf (loop_dump_stream, "\n");
4434
4435}
4436
4437
4438/* All this does is determine whether a giv can be made replaceable because
4439 its final value can be calculated. This code can not be part of record_giv
4440 above, because final_giv_value requires that the number of loop iterations
4441 be known, and that can not be accurately calculated until after all givs
4442 have been identified. */
4443
4444static void
4445check_final_value (v, loop_start, loop_end)
4446 struct induction *v;
4447 rtx loop_start, loop_end;
4448{
4449 struct iv_class *bl;
4450 rtx final_value = 0;
4451 rtx tem;
4452
4453 bl = reg_biv_class[REGNO (v->src_reg)];
4454
4455 /* DEST_ADDR givs will never reach here, because they are always marked
4456 replaceable above in record_giv. */
4457
4458 /* The giv can be replaced outright by the reduced register only if all
4459 of the following conditions are true:
4460 - the insn that sets the giv is always executed on any iteration
4461 on which the giv is used at all
4462 (there are two ways to deduce this:
4463 either the insn is executed on every iteration,
4464 or all uses follow that insn in the same basic block),
4465 - its final value can be calculated (this condition is different
4466 than the one above in record_giv)
4467 - no assignments to the biv occur during the giv's lifetime. */
4468
4469#if 0
4470 /* This is only called now when replaceable is known to be false. */
4471 /* Clear replaceable, so that it won't confuse final_giv_value. */
4472 v->replaceable = 0;
4473#endif
4474
4475 if ((final_value = final_giv_value (v, loop_start, loop_end))
4476 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4477 {
4478 int biv_increment_seen = 0;
4479 rtx p = v->insn;
4480 rtx last_giv_use;
4481
4482 v->replaceable = 1;
4483
4484 /* When trying to determine whether or not a biv increment occurs
4485 during the lifetime of the giv, we can ignore uses of the variable
4486 outside the loop because final_value is true. Hence we can not
4487 use regno_last_uid and regno_first_uid as above in record_giv. */
4488
4489 /* Search the loop to determine whether any assignments to the
4490 biv occur during the giv's lifetime. Start with the insn
4491 that sets the giv, and search around the loop until we come
4492 back to that insn again.
4493
4494 Also fail if there is a jump within the giv's lifetime that jumps
4495 to somewhere outside the lifetime but still within the loop. This
4496 catches spaghetti code where the execution order is not linear, and
4497 hence the above test fails. Here we assume that the giv lifetime
4498 does not extend from one iteration of the loop to the next, so as
4499 to make the test easier. Since the lifetime isn't known yet,
4500 this requires two loops. See also record_giv above. */
4501
4502 last_giv_use = v->insn;
4503
4504 while (1)
4505 {
4506 p = NEXT_INSN (p);
4507 if (p == loop_end)
4508 p = NEXT_INSN (loop_start);
4509 if (p == v->insn)
4510 break;
4511
4512 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4513 || GET_CODE (p) == CALL_INSN)
4514 {
4515 if (biv_increment_seen)
4516 {
4517 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4518 {
4519 v->replaceable = 0;
4520 v->not_replaceable = 1;
4521 break;
4522 }
4523 }
4524 else if (GET_CODE (PATTERN (p)) == SET
4525 && SET_DEST (PATTERN (p)) == v->src_reg)
4526 biv_increment_seen = 1;
4527 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4528 last_giv_use = p;
4529 }
4530 }
4531
4532 /* Now that the lifetime of the giv is known, check for branches
4533 from within the lifetime to outside the lifetime if it is still
4534 replaceable. */
4535
4536 if (v->replaceable)
4537 {
4538 p = v->insn;
4539 while (1)
4540 {
4541 p = NEXT_INSN (p);
4542 if (p == loop_end)
4543 p = NEXT_INSN (loop_start);
4544 if (p == last_giv_use)
4545 break;
4546
4547 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4548 && LABEL_NAME (JUMP_LABEL (p))
4549 && ((INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
4550 && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
4551 || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
4552 && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
4553 {
4554 v->replaceable = 0;
4555 v->not_replaceable = 1;
4556
4557 if (loop_dump_stream)
4558 fprintf (loop_dump_stream,
4559 "Found branch outside giv lifetime.\n");
4560
4561 break;
4562 }
4563 }
4564 }
4565
4566 /* If it is replaceable, then save the final value. */
4567 if (v->replaceable)
4568 v->final_value = final_value;
4569 }
4570
4571 if (loop_dump_stream && v->replaceable)
4572 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
4573 INSN_UID (v->insn), REGNO (v->dest_reg));
4574}
4575\f
4576/* Update the status of whether a giv can derive other givs.
4577
4578 We need to do something special if there is or may be an update to the biv
4579 between the time the giv is defined and the time it is used to derive
4580 another giv.
4581
4582 In addition, a giv that is only conditionally set is not allowed to
4583 derive another giv once a label has been passed.
4584
4585 The cases we look at are when a label or an update to a biv is passed. */
4586
4587static void
4588update_giv_derive (p)
4589 rtx p;
4590{
4591 struct iv_class *bl;
4592 struct induction *biv, *giv;
4593 rtx tem;
4594 int dummy;
4595
4596 /* Search all IV classes, then all bivs, and finally all givs.
4597
7dcd3836 4598 There are three cases we are concerned with. First we have the situation
b4ad7b23
RS
4599 of a giv that is only updated conditionally. In that case, it may not
4600 derive any givs after a label is passed.
4601
4602 The second case is when a biv update occurs, or may occur, after the
4603 definition of a giv. For certain biv updates (see below) that are
4604 known to occur between the giv definition and use, we can adjust the
4605 giv definition. For others, or when the biv update is conditional,
4606 we must prevent the giv from deriving any other givs. There are two
4607 sub-cases within this case.
4608
4609 If this is a label, we are concerned with any biv update that is done
4610 conditionally, since it may be done after the giv is defined followed by
4611 a branch here (actually, we need to pass both a jump and a label, but
4612 this extra tracking doesn't seem worth it).
4613
7dcd3836
RK
4614 If this is a jump, we are concerned about any biv update that may be
4615 executed multiple times. We are actually only concerned about
4616 backward jumps, but it is probably not worth performing the test
4617 on the jump again here.
4618
4619 If this is a biv update, we must adjust the giv status to show that a
b4ad7b23
RS
4620 subsequent biv update was performed. If this adjustment cannot be done,
4621 the giv cannot derive further givs. */
4622
4623 for (bl = loop_iv_list; bl; bl = bl->next)
4624 for (biv = bl->biv; biv; biv = biv->next_iv)
7dcd3836
RK
4625 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
4626 || biv->insn == p)
b4ad7b23
RS
4627 {
4628 for (giv = bl->giv; giv; giv = giv->next_iv)
4629 {
4630 /* If cant_derive is already true, there is no point in
4631 checking all of these conditions again. */
4632 if (giv->cant_derive)
4633 continue;
4634
4635 /* If this giv is conditionally set and we have passed a label,
4636 it cannot derive anything. */
4637 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
4638 giv->cant_derive = 1;
4639
4640 /* Skip givs that have mult_val == 0, since
4641 they are really invariants. Also skip those that are
4642 replaceable, since we know their lifetime doesn't contain
4643 any biv update. */
4644 else if (giv->mult_val == const0_rtx || giv->replaceable)
4645 continue;
4646
4647 /* The only way we can allow this giv to derive another
4648 is if this is a biv increment and we can form the product
4649 of biv->add_val and giv->mult_val. In this case, we will
4650 be able to compute a compensation. */
4651 else if (biv->insn == p)
4652 {
c160c628
RK
4653 tem = 0;
4654
4655 if (biv->mult_val == const1_rtx)
4656 tem = simplify_giv_expr (gen_rtx (MULT, giv->mode,
4657 biv->add_val,
4658 giv->mult_val),
4659 &dummy);
4660
4661 if (tem && giv->derive_adjustment)
4662 tem = simplify_giv_expr (gen_rtx (PLUS, giv->mode, tem,
4663 giv->derive_adjustment),
4664 &dummy);
4665 if (tem)
b4ad7b23
RS
4666 giv->derive_adjustment = tem;
4667 else
4668 giv->cant_derive = 1;
4669 }
7dcd3836
RK
4670 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
4671 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
b4ad7b23
RS
4672 giv->cant_derive = 1;
4673 }
4674 }
4675}
4676\f
4677/* Check whether an insn is an increment legitimate for a basic induction var.
09d7f5a5 4678 X is the source of insn P.
b4ad7b23
RS
4679 DEST_REG is the putative biv, also the destination of the insn.
4680 We accept patterns of these forms:
09d7f5a5 4681 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
b4ad7b23 4682 REG = INVARIANT + REG
b4ad7b23
RS
4683
4684 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
4685 and store the additive term into *INC_VAL.
4686
4687 If X is an assignment of an invariant into DEST_REG, we set
4688 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
4689
09d7f5a5
RK
4690 We also want to detect a BIV when it corresponds to a variable
4691 whose mode was promoted via PROMOTED_MODE. In that case, an increment
4692 of the variable may be a PLUS that adds a SUBREG of that variable to
4693 an invariant and then sign- or zero-extends the result of the PLUS
4694 into the variable.
4695
4696 Most GIVs in such cases will be in the promoted mode, since that is the
4697 probably the natural computation mode (and almost certainly the mode
4698 used for addresses) on the machine. So we view the pseudo-reg containing
4699 the variable as the BIV, as if it were simply incremented.
4700
4701 Note that treating the entire pseudo as a BIV will result in making
4702 simple increments to any GIVs based on it. However, if the variable
4703 overflows in its declared mode but not its promoted mode, the result will
4704 be incorrect. This is acceptable if the variable is signed, since
4705 overflows in such cases are undefined, but not if it is unsigned, since
4706 those overflows are defined. So we only check for SIGN_EXTEND and
4707 not ZERO_EXTEND.
4708
4709 If we cannot find a biv, we return 0. */
b4ad7b23
RS
4710
4711static int
09d7f5a5 4712basic_induction_var (x, dest_reg, p, inc_val, mult_val)
b4ad7b23 4713 register rtx x;
09d7f5a5 4714 rtx p;
b4ad7b23
RS
4715 rtx dest_reg;
4716 rtx *inc_val;
4717 rtx *mult_val;
4718{
4719 register enum rtx_code code;
4720 rtx arg;
09d7f5a5 4721 rtx insn, set = 0;
b4ad7b23
RS
4722
4723 code = GET_CODE (x);
4724 switch (code)
4725 {
4726 case PLUS:
09d7f5a5
RK
4727 if (XEXP (x, 0) == dest_reg
4728 || (GET_CODE (XEXP (x, 0)) == SUBREG
4729 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
4730 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
b4ad7b23 4731 arg = XEXP (x, 1);
09d7f5a5
RK
4732 else if (XEXP (x, 1) == dest_reg
4733 || (GET_CODE (XEXP (x, 1)) == SUBREG
b81fd0f4
RS
4734 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
4735 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
b4ad7b23
RS
4736 arg = XEXP (x, 0);
4737 else
4738 return 0;
4739
4740 if (invariant_p (arg) != 1)
4741 return 0;
4742
09d7f5a5 4743 *inc_val = convert_to_mode (GET_MODE (dest_reg), arg, 0);;
b4ad7b23
RS
4744 *mult_val = const1_rtx;
4745 return 1;
4746
09d7f5a5
RK
4747 case SUBREG:
4748 /* If this is a SUBREG for a promoted variable, check the inner
4749 value. */
4750 if (SUBREG_PROMOTED_VAR_P (x))
4751 return basic_induction_var (SUBREG_REG (x), dest_reg, p,
4752 inc_val, mult_val);
b4ad7b23 4753
09d7f5a5
RK
4754 case REG:
4755 /* If this register is assigned in the previous insn, look at its
4756 source, but don't go outside the loop or past a label. */
4757
4758 for (insn = PREV_INSN (p);
4759 (insn && GET_CODE (insn) == NOTE
4760 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
4761 insn = PREV_INSN (insn))
4762 ;
4763
4764 if (insn)
4765 set = single_set (insn);
4766
4767 if (set != 0 && SET_DEST (set) == x)
4768 return basic_induction_var (SET_SRC (set), dest_reg, insn,
4769 inc_val, mult_val);
4770 /* ... fall through ... */
b4ad7b23
RS
4771
4772 /* Can accept constant setting of biv only when inside inner most loop.
4773 Otherwise, a biv of an inner loop may be incorrectly recognized
4774 as a biv of the outer loop,
4775 causing code to be moved INTO the inner loop. */
4776 case MEM:
b4ad7b23
RS
4777 if (invariant_p (x) != 1)
4778 return 0;
4779 case CONST_INT:
4780 case SYMBOL_REF:
4781 case CONST:
4782 if (loops_enclosed == 1)
4783 {
09d7f5a5 4784 *inc_val = convert_to_mode (GET_MODE (dest_reg), x, 0);;
b4ad7b23
RS
4785 *mult_val = const0_rtx;
4786 return 1;
4787 }
4788 else
4789 return 0;
4790
09d7f5a5
RK
4791 case SIGN_EXTEND:
4792 return basic_induction_var (XEXP (x, 0), dest_reg, p,
4793 inc_val, mult_val);
4794 case ASHIFTRT:
4795 /* Similar, since this can be a sign extension. */
4796 for (insn = PREV_INSN (p);
4797 (insn && GET_CODE (insn) == NOTE
4798 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
4799 insn = PREV_INSN (insn))
4800 ;
4801
4802 if (insn)
4803 set = single_set (insn);
4804
4805 if (set && SET_DEST (set) == XEXP (x, 0)
4806 && GET_CODE (XEXP (x, 1)) == CONST_INT
4807 && INTVAL (XEXP (x, 1)) >= 0
4808 && GET_CODE (SET_SRC (set)) == ASHIFT
4809 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
4810 return basic_induction_var (XEXP (SET_SRC (set), 0), dest_reg, insn,
4811 inc_val, mult_val);
4812 return 0;
4813
b4ad7b23
RS
4814 default:
4815 return 0;
4816 }
4817}
4818\f
4819/* A general induction variable (giv) is any quantity that is a linear
4820 function of a basic induction variable,
4821 i.e. giv = biv * mult_val + add_val.
4822 The coefficients can be any loop invariant quantity.
4823 A giv need not be computed directly from the biv;
4824 it can be computed by way of other givs. */
4825
4826/* Determine whether X computes a giv.
4827 If it does, return a nonzero value
4828 which is the benefit from eliminating the computation of X;
4829 set *SRC_REG to the register of the biv that it is computed from;
4830 set *ADD_VAL and *MULT_VAL to the coefficients,
4831 such that the value of X is biv * mult + add; */
4832
4833static int
4834general_induction_var (x, src_reg, add_val, mult_val)
4835 rtx x;
4836 rtx *src_reg;
4837 rtx *add_val;
4838 rtx *mult_val;
4839{
4840 rtx orig_x = x;
4841 int benefit = 0;
4842 char *storage;
4843
4844 /* If this is an invariant, forget it, it isn't a giv. */
4845 if (invariant_p (x) == 1)
4846 return 0;
4847
4848 /* See if the expression could be a giv and get its form.
4849 Mark our place on the obstack in case we don't find a giv. */
4850 storage = (char *) oballoc (0);
4851 x = simplify_giv_expr (x, &benefit);
4852 if (x == 0)
4853 {
4854 obfree (storage);
4855 return 0;
4856 }
4857
4858 switch (GET_CODE (x))
4859 {
4860 case USE:
4861 case CONST_INT:
4862 /* Since this is now an invariant and wasn't before, it must be a giv
4863 with MULT_VAL == 0. It doesn't matter which BIV we associate this
4864 with. */
4865 *src_reg = loop_iv_list->biv->dest_reg;
4866 *mult_val = const0_rtx;
4867 *add_val = x;
4868 break;
4869
4870 case REG:
4871 /* This is equivalent to a BIV. */
4872 *src_reg = x;
4873 *mult_val = const1_rtx;
4874 *add_val = const0_rtx;
4875 break;
4876
4877 case PLUS:
4878 /* Either (plus (biv) (invar)) or
4879 (plus (mult (biv) (invar_1)) (invar_2)). */
4880 if (GET_CODE (XEXP (x, 0)) == MULT)
4881 {
4882 *src_reg = XEXP (XEXP (x, 0), 0);
4883 *mult_val = XEXP (XEXP (x, 0), 1);
4884 }
4885 else
4886 {
4887 *src_reg = XEXP (x, 0);
4888 *mult_val = const1_rtx;
4889 }
4890 *add_val = XEXP (x, 1);
4891 break;
4892
4893 case MULT:
4894 /* ADD_VAL is zero. */
4895 *src_reg = XEXP (x, 0);
4896 *mult_val = XEXP (x, 1);
4897 *add_val = const0_rtx;
4898 break;
4899
4900 default:
4901 abort ();
4902 }
4903
4904 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
4905 unless they are CONST_INT). */
4906 if (GET_CODE (*add_val) == USE)
4907 *add_val = XEXP (*add_val, 0);
4908 if (GET_CODE (*mult_val) == USE)
4909 *mult_val = XEXP (*mult_val, 0);
4910
3bb22aee 4911 benefit += rtx_cost (orig_x, SET);
b4ad7b23
RS
4912
4913 /* Always return some benefit if this is a giv so it will be detected
4914 as such. This allows elimination of bivs that might otherwise
4915 not be eliminated. */
4916 return benefit == 0 ? 1 : benefit;
4917}
4918\f
4919/* Given an expression, X, try to form it as a linear function of a biv.
4920 We will canonicalize it to be of the form
4921 (plus (mult (BIV) (invar_1))
4922 (invar_2))
c5b7917e 4923 with possible degeneracies.
b4ad7b23
RS
4924
4925 The invariant expressions must each be of a form that can be used as a
4926 machine operand. We surround then with a USE rtx (a hack, but localized
4927 and certainly unambiguous!) if not a CONST_INT for simplicity in this
4928 routine; it is the caller's responsibility to strip them.
4929
4930 If no such canonicalization is possible (i.e., two biv's are used or an
4931 expression that is neither invariant nor a biv or giv), this routine
4932 returns 0.
4933
4934 For a non-zero return, the result will have a code of CONST_INT, USE,
4935 REG (for a BIV), PLUS, or MULT. No other codes will occur.
4936
4937 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
4938
4939static rtx
4940simplify_giv_expr (x, benefit)
4941 rtx x;
4942 int *benefit;
4943{
4944 enum machine_mode mode = GET_MODE (x);
4945 rtx arg0, arg1;
4946 rtx tem;
4947
4948 /* If this is not an integer mode, or if we cannot do arithmetic in this
4949 mode, this can't be a giv. */
4950 if (mode != VOIDmode
4951 && (GET_MODE_CLASS (mode) != MODE_INT
5fd8383e 4952 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
b4ad7b23
RS
4953 return 0;
4954
4955 switch (GET_CODE (x))
4956 {
4957 case PLUS:
4958 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
4959 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
4960 if (arg0 == 0 || arg1 == 0)
4961 return 0;
4962
4963 /* Put constant last, CONST_INT last if both constant. */
4964 if ((GET_CODE (arg0) == USE
4965 || GET_CODE (arg0) == CONST_INT)
4966 && GET_CODE (arg1) != CONST_INT)
4967 tem = arg0, arg0 = arg1, arg1 = tem;
4968
4969 /* Handle addition of zero, then addition of an invariant. */
4970 if (arg1 == const0_rtx)
4971 return arg0;
4972 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
4973 switch (GET_CODE (arg0))
4974 {
4975 case CONST_INT:
4976 case USE:
4977 /* Both invariant. Only valid if sum is machine operand.
4978 First strip off possible USE on first operand. */
4979 if (GET_CODE (arg0) == USE)
4980 arg0 = XEXP (arg0, 0);
4981
4982 tem = 0;
4983 if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
4984 {
4985 tem = plus_constant (arg0, INTVAL (arg1));
4986 if (GET_CODE (tem) != CONST_INT)
4987 tem = gen_rtx (USE, mode, tem);
4988 }
4989
4990 return tem;
4991
4992 case REG:
4993 case MULT:
4994 /* biv + invar or mult + invar. Return sum. */
4995 return gen_rtx (PLUS, mode, arg0, arg1);
4996
4997 case PLUS:
4998 /* (a + invar_1) + invar_2. Associate. */
4999 return simplify_giv_expr (gen_rtx (PLUS, mode,
5000 XEXP (arg0, 0),
5001 gen_rtx (PLUS, mode,
5002 XEXP (arg0, 1), arg1)),
5003 benefit);
5004
5005 default:
5006 abort ();
5007 }
5008
5009 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
5010 MULT to reduce cases. */
5011 if (GET_CODE (arg0) == REG)
5012 arg0 = gen_rtx (MULT, mode, arg0, const1_rtx);
5013 if (GET_CODE (arg1) == REG)
5014 arg1 = gen_rtx (MULT, mode, arg1, const1_rtx);
5015
5016 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5017 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5018 Recurse to associate the second PLUS. */
5019 if (GET_CODE (arg1) == MULT)
5020 tem = arg0, arg0 = arg1, arg1 = tem;
5021
5022 if (GET_CODE (arg1) == PLUS)
5023 return simplify_giv_expr (gen_rtx (PLUS, mode,
5024 gen_rtx (PLUS, mode,
5025 arg0, XEXP (arg1, 0)),
5026 XEXP (arg1, 1)),
5027 benefit);
5028
5029 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
5030 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5031 abort ();
5032
5033 if (XEXP (arg0, 0) != XEXP (arg1, 0))
5034 return 0;
5035
5036 return simplify_giv_expr (gen_rtx (MULT, mode,
5037 XEXP (arg0, 0),
5038 gen_rtx (PLUS, mode,
5039 XEXP (arg0, 1),
5040 XEXP (arg1, 1))),
5041 benefit);
5042
5043 case MINUS:
5044 /* Handle "a - b" as "a + b * (-1)". */
5045 return simplify_giv_expr (gen_rtx (PLUS, mode,
5046 XEXP (x, 0),
5047 gen_rtx (MULT, mode,
5fd8383e 5048 XEXP (x, 1), constm1_rtx)),
b4ad7b23
RS
5049 benefit);
5050
5051 case MULT:
5052 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5053 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5054 if (arg0 == 0 || arg1 == 0)
5055 return 0;
5056
5057 /* Put constant last, CONST_INT last if both constant. */
5058 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5059 && GET_CODE (arg1) != CONST_INT)
5060 tem = arg0, arg0 = arg1, arg1 = tem;
5061
5062 /* If second argument is not now constant, not giv. */
5063 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5064 return 0;
5065
5066 /* Handle multiply by 0 or 1. */
5067 if (arg1 == const0_rtx)
5068 return const0_rtx;
5069
5070 else if (arg1 == const1_rtx)
5071 return arg0;
5072
5073 switch (GET_CODE (arg0))
5074 {
5075 case REG:
5076 /* biv * invar. Done. */
5077 return gen_rtx (MULT, mode, arg0, arg1);
5078
5079 case CONST_INT:
5080 /* Product of two constants. */
5fd8383e 5081 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
b4ad7b23
RS
5082
5083 case USE:
5084 /* invar * invar. Not giv. */
5085 return 0;
5086
5087 case MULT:
5088 /* (a * invar_1) * invar_2. Associate. */
5089 return simplify_giv_expr (gen_rtx (MULT, mode,
5090 XEXP (arg0, 0),
5091 gen_rtx (MULT, mode,
5092 XEXP (arg0, 1), arg1)),
5093 benefit);
5094
5095 case PLUS:
5096 /* (a + invar_1) * invar_2. Distribute. */
5097 return simplify_giv_expr (gen_rtx (PLUS, mode,
5098 gen_rtx (MULT, mode,
5099 XEXP (arg0, 0), arg1),
5100 gen_rtx (MULT, mode,
5101 XEXP (arg0, 1), arg1)),
5102 benefit);
5103
5104 default:
5105 abort ();
5106 }
5107
5108 case ASHIFT:
5109 case LSHIFT:
5110 /* Shift by constant is multiply by power of two. */
5111 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5112 return 0;
5113
5114 return simplify_giv_expr (gen_rtx (MULT, mode,
5115 XEXP (x, 0),
5fd8383e
RK
5116 GEN_INT ((HOST_WIDE_INT) 1
5117 << INTVAL (XEXP (x, 1)))),
b4ad7b23
RS
5118 benefit);
5119
5120 case NEG:
5121 /* "-a" is "a * (-1)" */
5fd8383e 5122 return simplify_giv_expr (gen_rtx (MULT, mode, XEXP (x, 0), constm1_rtx),
b4ad7b23
RS
5123 benefit);
5124
5125 case NOT:
5126 /* "~a" is "-a - 1". Silly, but easy. */
5127 return simplify_giv_expr (gen_rtx (MINUS, mode,
5128 gen_rtx (NEG, mode, XEXP (x, 0)),
5129 const1_rtx),
5130 benefit);
5131
5132 case USE:
5133 /* Already in proper form for invariant. */
5134 return x;
5135
5136 case REG:
5137 /* If this is a new register, we can't deal with it. */
5138 if (REGNO (x) >= max_reg_before_loop)
5139 return 0;
5140
5141 /* Check for biv or giv. */
5142 switch (reg_iv_type[REGNO (x)])
5143 {
5144 case BASIC_INDUCT:
5145 return x;
5146 case GENERAL_INDUCT:
5147 {
5148 struct induction *v = reg_iv_info[REGNO (x)];
5149
5150 /* Form expression from giv and add benefit. Ensure this giv
5151 can derive another and subtract any needed adjustment if so. */
5152 *benefit += v->benefit;
5153 if (v->cant_derive)
5154 return 0;
5155
5156 tem = gen_rtx (PLUS, mode, gen_rtx (MULT, mode,
5157 v->src_reg, v->mult_val),
5158 v->add_val);
5159 if (v->derive_adjustment)
5160 tem = gen_rtx (MINUS, mode, tem, v->derive_adjustment);
5161 return simplify_giv_expr (tem, benefit);
5162 }
5163 }
5164
5165 /* Fall through to general case. */
5166 default:
5167 /* If invariant, return as USE (unless CONST_INT).
5168 Otherwise, not giv. */
5169 if (GET_CODE (x) == USE)
5170 x = XEXP (x, 0);
5171
5172 if (invariant_p (x) == 1)
5173 {
5174 if (GET_CODE (x) == CONST_INT)
5175 return x;
5176 else
5177 return gen_rtx (USE, mode, x);
5178 }
5179 else
5180 return 0;
5181 }
5182}
5183\f
5184/* Help detect a giv that is calculated by several consecutive insns;
5185 for example,
5186 giv = biv * M
5187 giv = giv + A
5188 The caller has already identified the first insn P as having a giv as dest;
5189 we check that all other insns that set the same register follow
5190 immediately after P, that they alter nothing else,
5191 and that the result of the last is still a giv.
5192
5193 The value is 0 if the reg set in P is not really a giv.
5194 Otherwise, the value is the amount gained by eliminating
5195 all the consecutive insns that compute the value.
5196
5197 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5198 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5199
5200 The coefficients of the ultimate giv value are stored in
5201 *MULT_VAL and *ADD_VAL. */
5202
5203static int
5204consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5205 add_val, mult_val)
5206 int first_benefit;
5207 rtx p;
5208 rtx src_reg;
5209 rtx dest_reg;
5210 rtx *add_val;
5211 rtx *mult_val;
5212{
5213 int count;
5214 enum rtx_code code;
5215 int benefit;
5216 rtx temp;
5217 rtx set;
5218
5219 /* Indicate that this is a giv so that we can update the value produced in
5220 each insn of the multi-insn sequence.
5221
5222 This induction structure will be used only by the call to
5223 general_induction_var below, so we can allocate it on our stack.
5224 If this is a giv, our caller will replace the induct var entry with
5225 a new induction structure. */
5226 struct induction *v
5227 = (struct induction *) alloca (sizeof (struct induction));
5228 v->src_reg = src_reg;
5229 v->mult_val = *mult_val;
5230 v->add_val = *add_val;
5231 v->benefit = first_benefit;
5232 v->cant_derive = 0;
5233 v->derive_adjustment = 0;
5234
5235 reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5236 reg_iv_info[REGNO (dest_reg)] = v;
5237
5238 count = n_times_set[REGNO (dest_reg)] - 1;
5239
5240 while (count > 0)
5241 {
5242 p = NEXT_INSN (p);
5243 code = GET_CODE (p);
5244
5245 /* If libcall, skip to end of call sequence. */
5fd8383e 5246 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
5247 p = XEXP (temp, 0);
5248
5249 if (code == INSN
5250 && (set = single_set (p))
5251 && GET_CODE (SET_DEST (set)) == REG
5252 && SET_DEST (set) == dest_reg
5253 && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5254 add_val, mult_val))
5255 /* Giv created by equivalent expression. */
5fd8383e 5256 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
b4ad7b23
RS
5257 && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5258 add_val, mult_val))))
5259 && src_reg == v->src_reg)
5260 {
5fd8383e 5261 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
5262 benefit += libcall_benefit (p);
5263
5264 count--;
5265 v->mult_val = *mult_val;
5266 v->add_val = *add_val;
5267 v->benefit = benefit;
5268 }
5269 else if (code != NOTE)
5270 {
5271 /* Allow insns that set something other than this giv to a
5272 constant. Such insns are needed on machines which cannot
5273 include long constants and should not disqualify a giv. */
5274 if (code == INSN
5275 && (set = single_set (p))
5276 && SET_DEST (set) != dest_reg
5277 && CONSTANT_P (SET_SRC (set)))
5278 continue;
5279
5280 reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5281 return 0;
5282 }
5283 }
5284
5285 return v->benefit;
5286}
5287\f
5288/* Return an rtx, if any, that expresses giv G2 as a function of the register
5289 represented by G1. If no such expression can be found, or it is clear that
5290 it cannot possibly be a valid address, 0 is returned.
5291
5292 To perform the computation, we note that
5293 G1 = a * v + b and
5294 G2 = c * v + d
5295 where `v' is the biv.
5296
5297 So G2 = (c/a) * G1 + (d - b*c/a) */
5298
5299#ifdef ADDRESS_COST
5300static rtx
5301express_from (g1, g2)
5302 struct induction *g1, *g2;
5303{
5304 rtx mult, add;
5305
5306 /* The value that G1 will be multiplied by must be a constant integer. Also,
5307 the only chance we have of getting a valid address is if b*c/a (see above
5308 for notation) is also an integer. */
5309 if (GET_CODE (g1->mult_val) != CONST_INT
5310 || GET_CODE (g2->mult_val) != CONST_INT
5311 || GET_CODE (g1->add_val) != CONST_INT
5312 || g1->mult_val == const0_rtx
5313 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5314 return 0;
5315
5fd8383e 5316 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
b4ad7b23
RS
5317 add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5318
5319 /* Form simplified final result. */
5320 if (mult == const0_rtx)
5321 return add;
5322 else if (mult == const1_rtx)
5323 mult = g1->dest_reg;
5324 else
5325 mult = gen_rtx (MULT, g2->mode, g1->dest_reg, mult);
5326
5327 if (add == const0_rtx)
5328 return mult;
5329 else
5330 return gen_rtx (PLUS, g2->mode, mult, add);
5331}
5332#endif
5333\f
5334/* Return 1 if giv G2 can be combined with G1. This means that G2 can use
5335 (either directly or via an address expression) a register used to represent
5336 G1. Set g2->new_reg to a represtation of G1 (normally just
5337 g1->dest_reg). */
5338
5339static int
5340combine_givs_p (g1, g2)
5341 struct induction *g1, *g2;
5342{
5343 rtx tem;
5344
5345 /* If these givs are identical, they can be combined. */
5346 if (rtx_equal_p (g1->mult_val, g2->mult_val)
5347 && rtx_equal_p (g1->add_val, g2->add_val))
5348 {
5349 g2->new_reg = g1->dest_reg;
5350 return 1;
5351 }
5352
5353#ifdef ADDRESS_COST
5354 /* If G2 can be expressed as a function of G1 and that function is valid
5355 as an address and no more expensive than using a register for G2,
5356 the expression of G2 in terms of G1 can be used. */
5357 if (g2->giv_type == DEST_ADDR
5358 && (tem = express_from (g1, g2)) != 0
5359 && memory_address_p (g2->mem_mode, tem)
5360 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5361 {
5362 g2->new_reg = tem;
5363 return 1;
5364 }
5365#endif
5366
5367 return 0;
5368}
5369\f
5370/* Check all pairs of givs for iv_class BL and see if any can be combined with
5371 any other. If so, point SAME to the giv combined with and set NEW_REG to
5372 be an expression (in terms of the other giv's DEST_REG) equivalent to the
5373 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
5374
5375static void
5376combine_givs (bl)
5377 struct iv_class *bl;
5378{
5379 struct induction *g1, *g2;
5380 int pass;
5381
5382 for (g1 = bl->giv; g1; g1 = g1->next_iv)
5383 for (pass = 0; pass <= 1; pass++)
5384 for (g2 = bl->giv; g2; g2 = g2->next_iv)
5385 if (g1 != g2
5386 /* First try to combine with replaceable givs, then all givs. */
5387 && (g1->replaceable || pass == 1)
5388 /* If either has already been combined or is to be ignored, can't
5389 combine. */
5390 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5391 /* If something has been based on G2, G2 cannot itself be based
5392 on something else. */
5393 && ! g2->combined_with
5394 && combine_givs_p (g1, g2))
5395 {
5396 /* g2->new_reg set by `combine_givs_p' */
5397 g2->same = g1;
5398 g1->combined_with = 1;
5399 g1->benefit += g2->benefit;
5400 /* ??? The new final_[bg]iv_value code does a much better job
5401 of finding replaceable giv's, and hence this code may no
5402 longer be necessary. */
5403 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5404 g1->benefit -= copy_cost;
5405 g1->lifetime += g2->lifetime;
5406 g1->times_used += g2->times_used;
5407
5408 if (loop_dump_stream)
5409 fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5410 INSN_UID (g2->insn), INSN_UID (g1->insn));
5411 }
5412}
5413\f
5414/* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
5415
5416void
5417emit_iv_add_mult (b, m, a, reg, insert_before)
5418 rtx b; /* initial value of basic induction variable */
5419 rtx m; /* multiplicative constant */
5420 rtx a; /* additive constant */
5421 rtx reg; /* destination register */
5422 rtx insert_before;
5423{
5424 rtx seq;
5425 rtx result;
5426
5427 /* Prevent unexpected sharing of these rtx. */
5428 a = copy_rtx (a);
5429 b = copy_rtx (b);
5430
5431 /* Increase the lifetime of any invariants moved further in code. */
5432 update_reg_last_use (a, insert_before);
5433 update_reg_last_use (b, insert_before);
5434 update_reg_last_use (m, insert_before);
5435
5436 start_sequence ();
5437 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5438 if (reg != result)
5439 emit_move_insn (reg, result);
5440 seq = gen_sequence ();
5441 end_sequence ();
5442
5443 emit_insn_before (seq, insert_before);
5444}
5445\f
5446/* Test whether A * B can be computed without
5447 an actual multiply insn. Value is 1 if so. */
5448
5449static int
5450product_cheap_p (a, b)
5451 rtx a;
5452 rtx b;
5453{
5454 int i;
5455 rtx tmp;
5456 struct obstack *old_rtl_obstack = rtl_obstack;
5457 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
5458 int win = 1;
5459
5460 /* If only one is constant, make it B. */
5461 if (GET_CODE (a) == CONST_INT)
5462 tmp = a, a = b, b = tmp;
5463
5464 /* If first constant, both constant, so don't need multiply. */
5465 if (GET_CODE (a) == CONST_INT)
5466 return 1;
5467
5468 /* If second not constant, neither is constant, so would need multiply. */
5469 if (GET_CODE (b) != CONST_INT)
5470 return 0;
5471
5472 /* One operand is constant, so might not need multiply insn. Generate the
5473 code for the multiply and see if a call or multiply, or long sequence
5474 of insns is generated. */
5475
5476 rtl_obstack = &temp_obstack;
5477 start_sequence ();
5fd8383e 5478 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
b4ad7b23
RS
5479 tmp = gen_sequence ();
5480 end_sequence ();
5481
5482 if (GET_CODE (tmp) == SEQUENCE)
5483 {
5484 if (XVEC (tmp, 0) == 0)
5485 win = 1;
5486 else if (XVECLEN (tmp, 0) > 3)
5487 win = 0;
5488 else
5489 for (i = 0; i < XVECLEN (tmp, 0); i++)
5490 {
5491 rtx insn = XVECEXP (tmp, 0, i);
5492
5493 if (GET_CODE (insn) != INSN
5494 || (GET_CODE (PATTERN (insn)) == SET
5495 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
5496 || (GET_CODE (PATTERN (insn)) == PARALLEL
5497 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
5498 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
5499 {
5500 win = 0;
5501 break;
5502 }
5503 }
5504 }
5505 else if (GET_CODE (tmp) == SET
5506 && GET_CODE (SET_SRC (tmp)) == MULT)
5507 win = 0;
5508 else if (GET_CODE (tmp) == PARALLEL
5509 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
5510 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
5511 win = 0;
5512
5513 /* Free any storage we obtained in generating this multiply and restore rtl
5514 allocation to its normal obstack. */
5515 obstack_free (&temp_obstack, storage);
5516 rtl_obstack = old_rtl_obstack;
5517
5518 return win;
5519}
5520\f
5521/* Check to see if loop can be terminated by a "decrement and branch until
5522 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
5523 Also try reversing an increment loop to a decrement loop
5524 to see if the optimization can be performed.
5525 Value is nonzero if optimization was performed. */
5526
5527/* This is useful even if the architecture doesn't have such an insn,
5528 because it might change a loops which increments from 0 to n to a loop
5529 which decrements from n to 0. A loop that decrements to zero is usually
5530 faster than one that increments from zero. */
5531
5532/* ??? This could be rewritten to use some of the loop unrolling procedures,
5533 such as approx_final_value, biv_total_increment, loop_iterations, and
5534 final_[bg]iv_value. */
5535
5536static int
5537check_dbra_loop (loop_end, insn_count, loop_start)
5538 rtx loop_end;
5539 int insn_count;
5540 rtx loop_start;
5541{
5542 struct iv_class *bl;
5543 rtx reg;
5544 rtx jump_label;
5545 rtx final_value;
5546 rtx start_value;
5547 enum rtx_code branch_code;
5548 rtx new_add_val;
5549 rtx comparison;
5550 rtx before_comparison;
5551 rtx p;
5552
5553 /* If last insn is a conditional branch, and the insn before tests a
5554 register value, try to optimize it. Otherwise, we can't do anything. */
5555
5556 comparison = get_condition_for_loop (PREV_INSN (loop_end));
5557 if (comparison == 0)
5558 return 0;
5559
5560 /* Check all of the bivs to see if the compare uses one of them.
5561 Skip biv's set more than once because we can't guarantee that
5562 it will be zero on the last iteration. Also skip if the biv is
5563 used between its update and the test insn. */
5564
5565 for (bl = loop_iv_list; bl; bl = bl->next)
5566 {
5567 if (bl->biv_count == 1
5568 && bl->biv->dest_reg == XEXP (comparison, 0)
5569 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
5570 PREV_INSN (PREV_INSN (loop_end))))
5571 break;
5572 }
5573
5574 if (! bl)
5575 return 0;
5576
5577 /* Look for the case where the basic induction variable is always
5578 nonnegative, and equals zero on the last iteration.
5579 In this case, add a reg_note REG_NONNEG, which allows the
5580 m68k DBRA instruction to be used. */
5581
5582 if (((GET_CODE (comparison) == GT
5583 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
5584 && INTVAL (XEXP (comparison, 1)) == -1)
5585 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
5586 && GET_CODE (bl->biv->add_val) == CONST_INT
5587 && INTVAL (bl->biv->add_val) < 0)
5588 {
5589 /* Initial value must be greater than 0,
5590 init_val % -dec_value == 0 to ensure that it equals zero on
5591 the last iteration */
5592
5593 if (GET_CODE (bl->initial_value) == CONST_INT
5594 && INTVAL (bl->initial_value) > 0
5595 && (INTVAL (bl->initial_value) %
5596 (-INTVAL (bl->biv->add_val))) == 0)
5597 {
5598 /* register always nonnegative, add REG_NOTE to branch */
5599 REG_NOTES (PREV_INSN (loop_end))
5fd8383e 5600 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
b4ad7b23
RS
5601 REG_NOTES (PREV_INSN (loop_end)));
5602 bl->nonneg = 1;
5603
5604 return 1;
5605 }
5606
5607 /* If the decrement is 1 and the value was tested as >= 0 before
5608 the loop, then we can safely optimize. */
5609 for (p = loop_start; p; p = PREV_INSN (p))
5610 {
5611 if (GET_CODE (p) == CODE_LABEL)
5612 break;
5613 if (GET_CODE (p) != JUMP_INSN)
5614 continue;
5615
5616 before_comparison = get_condition_for_loop (p);
5617 if (before_comparison
5618 && XEXP (before_comparison, 0) == bl->biv->dest_reg
5619 && GET_CODE (before_comparison) == LT
5620 && XEXP (before_comparison, 1) == const0_rtx
5621 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
5622 && INTVAL (bl->biv->add_val) == -1)
5623 {
5624 REG_NOTES (PREV_INSN (loop_end))
5fd8383e 5625 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
b4ad7b23
RS
5626 REG_NOTES (PREV_INSN (loop_end)));
5627 bl->nonneg = 1;
5628
5629 return 1;
5630 }
5631 }
5632 }
5633 else if (num_mem_sets <= 1)
5634 {
5635 /* Try to change inc to dec, so can apply above optimization. */
5636 /* Can do this if:
5637 all registers modified are induction variables or invariant,
5638 all memory references have non-overlapping addresses
5639 (obviously true if only one write)
5640 allow 2 insns for the compare/jump at the end of the loop. */
5641 int num_nonfixed_reads = 0;
5642 /* 1 if the iteration var is used only to count iterations. */
5643 int no_use_except_counting = 0;
5644
5645 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
5646 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5647 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
5648
5649 if (bl->giv_count == 0
5650 && ! loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
5651 {
5652 rtx bivreg = regno_reg_rtx[bl->regno];
5653
5654 /* If there are no givs for this biv, and the only exit is the
5655 fall through at the end of the the loop, then
5656 see if perhaps there are no uses except to count. */
5657 no_use_except_counting = 1;
5658 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
5659 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5660 {
5661 rtx set = single_set (p);
5662
5663 if (set && GET_CODE (SET_DEST (set)) == REG
5664 && REGNO (SET_DEST (set)) == bl->regno)
5665 /* An insn that sets the biv is okay. */
5666 ;
5667 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
5668 || p == prev_nonnote_insn (loop_end))
5669 /* Don't bother about the end test. */
5670 ;
5671 else if (reg_mentioned_p (bivreg, PATTERN (p)))
5672 /* Any other use of the biv is no good. */
5673 {
5674 no_use_except_counting = 0;
5675 break;
5676 }
5677 }
5678 }
5679
5680 /* This code only acts for innermost loops. Also it simplifies
5681 the memory address check by only reversing loops with
5682 zero or one memory access.
5683 Two memory accesses could involve parts of the same array,
5684 and that can't be reversed. */
5685
5686 if (num_nonfixed_reads <= 1
5687 && !loop_has_call
552bc76f 5688 && !loop_has_volatile
b4ad7b23
RS
5689 && (no_use_except_counting
5690 || (bl->giv_count + bl->biv_count + num_mem_sets
5691 + num_movables + 2 == insn_count)))
5692 {
5693 rtx condition = get_condition_for_loop (PREV_INSN (loop_end));
5694 int win;
5695 rtx tem;
5696
5697 /* Loop can be reversed. */
5698 if (loop_dump_stream)
5699 fprintf (loop_dump_stream, "Can reverse loop\n");
5700
5701 /* Now check other conditions:
5702 initial_value must be zero,
5703 final_value % add_val == 0, so that when reversed, the
5704 biv will be zero on the last iteration.
5705
5706 This test can probably be improved since +/- 1 in the constant
5707 can be obtained by changing LT to LE and vice versa; this is
5708 confusing. */
5709
5710 if (comparison && bl->initial_value == const0_rtx
5711 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
5712 /* LE gets turned into LT */
5713 && GET_CODE (comparison) == LT
5714 && (INTVAL (XEXP (comparison, 1))
5715 % INTVAL (bl->biv->add_val)) == 0)
5716 {
5717 /* Register will always be nonnegative, with value
5718 0 on last iteration if loop reversed */
5719
5720 /* Save some info needed to produce the new insns. */
5721 reg = bl->biv->dest_reg;
5722 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
5fd8383e 5723 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
b4ad7b23
RS
5724
5725 final_value = XEXP (comparison, 1);
5fd8383e
RK
5726 start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
5727 - INTVAL (bl->biv->add_val));
b4ad7b23
RS
5728
5729 /* Initialize biv to start_value before loop start.
5730 The old initializing insn will be deleted as a
5731 dead store by flow.c. */
5732 emit_insn_before (gen_move_insn (reg, start_value), loop_start);
5733
5734 /* Add insn to decrement register, and delete insn
5735 that incremented the register. */
5736 p = emit_insn_before (gen_add2_insn (reg, new_add_val),
5737 bl->biv->insn);
5738 delete_insn (bl->biv->insn);
5739
5740 /* Update biv info to reflect its new status. */
5741 bl->biv->insn = p;
5742 bl->initial_value = start_value;
5743 bl->biv->add_val = new_add_val;
5744
5745 /* Inc LABEL_NUSES so that delete_insn will
5746 not delete the label. */
5747 LABEL_NUSES (XEXP (jump_label, 0)) ++;
5748
5749 /* Emit an insn after the end of the loop to set the biv's
5750 proper exit value if it is used anywhere outside the loop. */
5751 if ((regno_last_uid[bl->regno]
5752 != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
5753 || ! bl->init_insn
5754 || regno_first_uid[bl->regno] != INSN_UID (bl->init_insn))
5755 emit_insn_after (gen_move_insn (reg, final_value),
5756 loop_end);
5757
5758 /* Delete compare/branch at end of loop. */
5759 delete_insn (PREV_INSN (loop_end));
5760 delete_insn (PREV_INSN (loop_end));
5761
5762 /* Add new compare/branch insn at end of loop. */
5763 start_sequence ();
5fd8383e
RK
5764 emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
5765 GET_MODE (reg), 0, 0);
b4ad7b23
RS
5766 emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
5767 tem = gen_sequence ();
5768 end_sequence ();
5769 emit_jump_insn_before (tem, loop_end);
5770
5771 for (tem = PREV_INSN (loop_end);
5772 tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
5773 ;
5774 if (tem)
5775 {
5776 JUMP_LABEL (tem) = XEXP (jump_label, 0);
5777
5778 /* Increment of LABEL_NUSES done above. */
5779 /* Register is now always nonnegative,
5780 so add REG_NONNEG note to the branch. */
5fd8383e 5781 REG_NOTES (tem) = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
b4ad7b23
RS
5782 REG_NOTES (tem));
5783 }
5784
5785 bl->nonneg = 1;
5786
5787 /* Mark that this biv has been reversed. Each giv which depends
5788 on this biv, and which is also live past the end of the loop
5789 will have to be fixed up. */
5790
5791 bl->reversed = 1;
5792
5793 if (loop_dump_stream)
5794 fprintf (loop_dump_stream,
5795 "Reversed loop and added reg_nonneg\n");
5796
5797 return 1;
5798 }
5799 }
5800 }
5801
5802 return 0;
5803}
5804\f
5805/* Verify whether the biv BL appears to be eliminable,
5806 based on the insns in the loop that refer to it.
5807 LOOP_START is the first insn of the loop, and END is the end insn.
5808
5809 If ELIMINATE_P is non-zero, actually do the elimination.
5810
5811 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
5812 determine whether invariant insns should be placed inside or at the
5813 start of the loop. */
5814
5815static int
5816maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
5817 struct iv_class *bl;
5818 rtx loop_start;
5819 rtx end;
5820 int eliminate_p;
5821 int threshold, insn_count;
5822{
5823 rtx reg = bl->biv->dest_reg;
5824 rtx p, set;
5825 struct induction *v;
5826
5827 /* Scan all insns in the loop, stopping if we find one that uses the
5828 biv in a way that we cannot eliminate. */
5829
5830 for (p = loop_start; p != end; p = NEXT_INSN (p))
5831 {
5832 enum rtx_code code = GET_CODE (p);
5833 rtx where = threshold >= insn_count ? loop_start : p;
5834
5835 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
5836 && reg_mentioned_p (reg, PATTERN (p))
5837 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
5838 {
5839 if (loop_dump_stream)
5840 fprintf (loop_dump_stream,
5841 "Cannot eliminate biv %d: biv used in insn %d.\n",
5842 bl->regno, INSN_UID (p));
5843 break;
5844 }
5845 }
5846
5847 if (p == end)
5848 {
5849 if (loop_dump_stream)
5850 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
5851 bl->regno, eliminate_p ? "was" : "can be");
5852 return 1;
5853 }
5854
5855 return 0;
5856}
5857\f
5858/* If BL appears in X (part of the pattern of INSN), see if we can
5859 eliminate its use. If so, return 1. If not, return 0.
5860
5861 If BIV does not appear in X, return 1.
5862
5863 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
5864 where extra insns should be added. Depending on how many items have been
5865 moved out of the loop, it will either be before INSN or at the start of
5866 the loop. */
5867
5868static int
5869maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
5870 rtx x, insn;
5871 struct iv_class *bl;
5872 int eliminate_p;
5873 rtx where;
5874{
5875 enum rtx_code code = GET_CODE (x);
5876 rtx reg = bl->biv->dest_reg;
5877 enum machine_mode mode = GET_MODE (reg);
5878 struct induction *v;
5879 rtx arg, new, tem;
5880 int arg_operand;
5881 char *fmt;
5882 int i, j;
5883
5884 switch (code)
5885 {
5886 case REG:
5887 /* If we haven't already been able to do something with this BIV,
5888 we can't eliminate it. */
5889 if (x == reg)
5890 return 0;
5891 return 1;
5892
5893 case SET:
5894 /* If this sets the BIV, it is not a problem. */
5895 if (SET_DEST (x) == reg)
5896 return 1;
5897
5898 /* If this is an insn that defines a giv, it is also ok because
5899 it will go away when the giv is reduced. */
5900 for (v = bl->giv; v; v = v->next_iv)
5901 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
5902 return 1;
5903
5904#ifdef HAVE_cc0
5905 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
5906 {
5907 /* Can replace with any giv that was reduced and
5908 that has (MULT_VAL != 0) and (ADD_VAL == 0).
5909 Require a constant for MULT_VAL, so we know it's nonzero. */
5910
5911 for (v = bl->giv; v; v = v->next_iv)
5912 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
5913 && v->add_val == const0_rtx
5914 && ! v->ignore && ! v->maybe_dead
5915 && v->mode == mode)
5916 {
5917 if (! eliminate_p)
5918 return 1;
5919
5920 /* If the giv has the opposite direction of change,
5921 then reverse the comparison. */
5922 if (INTVAL (v->mult_val) < 0)
5923 new = gen_rtx (COMPARE, GET_MODE (v->new_reg),
5924 const0_rtx, v->new_reg);
5925 else
5926 new = v->new_reg;
5927
5928 /* We can probably test that giv's reduced reg. */
5929 if (validate_change (insn, &SET_SRC (x), new, 0))
5930 return 1;
5931 }
5932
5933 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
5934 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
5935 Require a constant for MULT_VAL, so we know it's nonzero. */
5936
5937 for (v = bl->giv; v; v = v->next_iv)
5938 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
5939 && ! v->ignore && ! v->maybe_dead
5940 && v->mode == mode)
5941 {
5942 if (! eliminate_p)
5943 return 1;
5944
5945 /* If the giv has the opposite direction of change,
5946 then reverse the comparison. */
5947 if (INTVAL (v->mult_val) < 0)
5948 new = gen_rtx (COMPARE, VOIDmode, copy_rtx (v->add_val),
5949 v->new_reg);
5950 else
5951 new = gen_rtx (COMPARE, VOIDmode, v->new_reg,
5952 copy_rtx (v->add_val));
5953
5954 /* Replace biv with the giv's reduced register. */
5955 update_reg_last_use (v->add_val, insn);
5956 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
5957 return 1;
5958
5959 /* Insn doesn't support that constant or invariant. Copy it
5960 into a register (it will be a loop invariant.) */
5961 tem = gen_reg_rtx (GET_MODE (v->new_reg));
5962
5963 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
5964 where);
5965
5966 if (validate_change (insn, &SET_SRC (PATTERN (insn)),
5967 gen_rtx (COMPARE, VOIDmode,
5968 v->new_reg, tem), 0))
5969 return 1;
5970 }
5971 }
5972#endif
5973 break;
5974
5975 case COMPARE:
5976 case EQ: case NE:
5977 case GT: case GE: case GTU: case GEU:
5978 case LT: case LE: case LTU: case LEU:
5979 /* See if either argument is the biv. */
5980 if (XEXP (x, 0) == reg)
5981 arg = XEXP (x, 1), arg_operand = 1;
5982 else if (XEXP (x, 1) == reg)
5983 arg = XEXP (x, 0), arg_operand = 0;
5984 else
5985 break;
5986
5987 if (CONSTANT_P (arg))
5988 {
5989 /* First try to replace with any giv that has constant positive
5990 mult_val and constant add_val. We might be able to support
5991 negative mult_val, but it seems complex to do it in general. */
5992
5993 for (v = bl->giv; v; v = v->next_iv)
5994 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
5995 && CONSTANT_P (v->add_val)
5996 && ! v->ignore && ! v->maybe_dead
5997 && v->mode == mode)
5998 {
5999 if (! eliminate_p)
6000 return 1;
6001
6002 /* Replace biv with the giv's reduced reg. */
6003 XEXP (x, 1-arg_operand) = v->new_reg;
6004
6005 /* If all constants are actually constant integers and
6006 the derived constant can be directly placed in the COMPARE,
6007 do so. */
6008 if (GET_CODE (arg) == CONST_INT
6009 && GET_CODE (v->mult_val) == CONST_INT
6010 && GET_CODE (v->add_val) == CONST_INT
6011 && validate_change (insn, &XEXP (x, arg_operand),
5fd8383e
RK
6012 GEN_INT (INTVAL (arg)
6013 * INTVAL (v->mult_val)
6014 + INTVAL (v->add_val)), 0))
b4ad7b23
RS
6015 return 1;
6016
6017 /* Otherwise, load it into a register. */
6018 tem = gen_reg_rtx (mode);
6019 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6020 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6021 return 1;
6022
6023 /* If that failed, put back the change we made above. */
6024 XEXP (x, 1-arg_operand) = reg;
6025 }
6026
6027 /* Look for giv with positive constant mult_val and nonconst add_val.
6028 Insert insns to calculate new compare value. */
6029
6030 for (v = bl->giv; v; v = v->next_iv)
d45cf215 6031 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
b4ad7b23
RS
6032 && ! v->ignore && ! v->maybe_dead
6033 && v->mode == mode)
6034 {
6035 rtx tem;
6036
6037 if (! eliminate_p)
6038 return 1;
6039
6040 tem = gen_reg_rtx (mode);
6041
6042 /* Replace biv with giv's reduced register. */
6043 validate_change (insn, &XEXP (x, 1 - arg_operand),
6044 v->new_reg, 1);
6045
6046 /* Compute value to compare against. */
6047 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6048 /* Use it in this insn. */
6049 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6050 if (apply_change_group ())
6051 return 1;
6052 }
6053 }
6054 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6055 {
6056 if (invariant_p (arg) == 1)
6057 {
6058 /* Look for giv with constant positive mult_val and nonconst
6059 add_val. Insert insns to compute new compare value. */
6060
6061 for (v = bl->giv; v; v = v->next_iv)
6062 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6063 && ! v->ignore && ! v->maybe_dead
6064 && v->mode == mode)
6065 {
6066 rtx tem;
6067
6068 if (! eliminate_p)
6069 return 1;
6070
6071 tem = gen_reg_rtx (mode);
6072
6073 /* Replace biv with giv's reduced register. */
6074 validate_change (insn, &XEXP (x, 1 - arg_operand),
6075 v->new_reg, 1);
6076
6077 /* Compute value to compare against. */
6078 emit_iv_add_mult (arg, v->mult_val, v->add_val,
6079 tem, where);
6080 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6081 if (apply_change_group ())
6082 return 1;
6083 }
6084 }
6085
6086 /* This code has problems. Basically, you can't know when
6087 seeing if we will eliminate BL, whether a particular giv
6088 of ARG will be reduced. If it isn't going to be reduced,
6089 we can't eliminate BL. We can try forcing it to be reduced,
6090 but that can generate poor code.
6091
6092 The problem is that the benefit of reducing TV, below should
6093 be increased if BL can actually be eliminated, but this means
6094 we might have to do a topological sort of the order in which
6095 we try to process biv. It doesn't seem worthwhile to do
6096 this sort of thing now. */
6097
6098#if 0
6099 /* Otherwise the reg compared with had better be a biv. */
6100 if (GET_CODE (arg) != REG
6101 || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6102 return 0;
6103
6104 /* Look for a pair of givs, one for each biv,
6105 with identical coefficients. */
6106 for (v = bl->giv; v; v = v->next_iv)
6107 {
6108 struct induction *tv;
6109
6110 if (v->ignore || v->maybe_dead || v->mode != mode)
6111 continue;
6112
6113 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6114 if (! tv->ignore && ! tv->maybe_dead
6115 && rtx_equal_p (tv->mult_val, v->mult_val)
6116 && rtx_equal_p (tv->add_val, v->add_val)
6117 && tv->mode == mode)
6118 {
6119 if (! eliminate_p)
6120 return 1;
6121
6122 /* Replace biv with its giv's reduced reg. */
6123 XEXP (x, 1-arg_operand) = v->new_reg;
6124 /* Replace other operand with the other giv's
6125 reduced reg. */
6126 XEXP (x, arg_operand) = tv->new_reg;
6127 return 1;
6128 }
6129 }
6130#endif
6131 }
6132
6133 /* If we get here, the biv can't be eliminated. */
6134 return 0;
6135
6136 case MEM:
6137 /* If this address is a DEST_ADDR giv, it doesn't matter if the
6138 biv is used in it, since it will be replaced. */
6139 for (v = bl->giv; v; v = v->next_iv)
6140 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6141 return 1;
6142 break;
6143 }
6144
6145 /* See if any subexpression fails elimination. */
6146 fmt = GET_RTX_FORMAT (code);
6147 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6148 {
6149 switch (fmt[i])
6150 {
6151 case 'e':
6152 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
6153 eliminate_p, where))
6154 return 0;
6155 break;
6156
6157 case 'E':
6158 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6159 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6160 eliminate_p, where))
6161 return 0;
6162 break;
6163 }
6164 }
6165
6166 return 1;
6167}
6168\f
6169/* Return nonzero if the last use of REG
6170 is in an insn following INSN in the same basic block. */
6171
6172static int
6173last_use_this_basic_block (reg, insn)
6174 rtx reg;
6175 rtx insn;
6176{
6177 rtx n;
6178 for (n = insn;
6179 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6180 n = NEXT_INSN (n))
6181 {
6182 if (regno_last_uid[REGNO (reg)] == INSN_UID (n))
6183 return 1;
6184 }
6185 return 0;
6186}
6187\f
6188/* Called via `note_stores' to record the initial value of a biv. Here we
6189 just record the location of the set and process it later. */
6190
6191static void
6192record_initial (dest, set)
6193 rtx dest;
6194 rtx set;
6195{
6196 struct iv_class *bl;
6197
6198 if (GET_CODE (dest) != REG
6199 || REGNO (dest) >= max_reg_before_loop
6200 || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6201 return;
6202
6203 bl = reg_biv_class[REGNO (dest)];
6204
6205 /* If this is the first set found, record it. */
6206 if (bl->init_insn == 0)
6207 {
6208 bl->init_insn = note_insn;
6209 bl->init_set = set;
6210 }
6211}
6212\f
6213/* If any of the registers in X are "old" and currently have a last use earlier
6214 than INSN, update them to have a last use of INSN. Their actual last use
6215 will be the previous insn but it will not have a valid uid_luid so we can't
6216 use it. */
6217
6218static void
6219update_reg_last_use (x, insn)
6220 rtx x;
6221 rtx insn;
6222{
6223 /* Check for the case where INSN does not have a valid luid. In this case,
6224 there is no need to modify the regno_last_uid, as this can only happen
6225 when code is inserted after the loop_end to set a pseudo's final value,
6226 and hence this insn will never be the last use of x. */
6227 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6228 && INSN_UID (insn) < max_uid_for_loop
6229 && uid_luid[regno_last_uid[REGNO (x)]] < uid_luid[INSN_UID (insn)])
6230 regno_last_uid[REGNO (x)] = INSN_UID (insn);
6231 else
6232 {
6233 register int i, j;
6234 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6235 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6236 {
6237 if (fmt[i] == 'e')
6238 update_reg_last_use (XEXP (x, i), insn);
6239 else if (fmt[i] == 'E')
6240 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6241 update_reg_last_use (XVECEXP (x, i, j), insn);
6242 }
6243 }
6244}
6245\f
6246/* Given a jump insn JUMP, return the condition that will cause it to branch
6247 to its JUMP_LABEL. If the condition cannot be understood, or is an
6248 inequality floating-point comparison which needs to be reversed, 0 will
6249 be returned.
6250
6251 If EARLIEST is non-zero, it is a pointer to a place where the earliest
6252 insn used in locating the condition was found. If a replacement test
6253 of the condition is desired, it should be placed in front of that
6254 insn and we will be sure that the inputs are still valid.
6255
6256 The condition will be returned in a canonical form to simplify testing by
6257 callers. Specifically:
6258
6259 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6260 (2) Both operands will be machine operands; (cc0) will have been replaced.
6261 (3) If an operand is a constant, it will be the second operand.
6262 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6263 for GE, GEU, and LEU. */
6264
6265rtx
6266get_condition (jump, earliest)
6267 rtx jump;
6268 rtx *earliest;
6269{
6270 enum rtx_code code;
6271 rtx prev = jump;
6272 rtx set;
6273 rtx tem;
6274 rtx op0, op1;
6275 int reverse_code = 0;
6276 int did_reverse_condition = 0;
6277
6278 /* If this is not a standard conditional jump, we can't parse it. */
6279 if (GET_CODE (jump) != JUMP_INSN
6280 || ! condjump_p (jump) || simplejump_p (jump))
6281 return 0;
6282
6283 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6284 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6285 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6286
6287 if (earliest)
6288 *earliest = jump;
6289
6290 /* If this branches to JUMP_LABEL when the condition is false, reverse
6291 the condition. */
b5d27be7
RS
6292 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6293 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
b4ad7b23
RS
6294 code = reverse_condition (code), did_reverse_condition ^= 1;
6295
6296 /* If we are comparing a register with zero, see if the register is set
6297 in the previous insn to a COMPARE or a comparison operation. Perform
6298 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6299 in cse.c */
6300
6301 while (GET_RTX_CLASS (code) == '<' && op1 == const0_rtx)
6302 {
6303 /* Set non-zero when we find something of interest. */
6304 rtx x = 0;
6305
6306#ifdef HAVE_cc0
6307 /* If comparison with cc0, import actual comparison from compare
6308 insn. */
6309 if (op0 == cc0_rtx)
6310 {
6311 if ((prev = prev_nonnote_insn (prev)) == 0
6312 || GET_CODE (prev) != INSN
6313 || (set = single_set (prev)) == 0
6314 || SET_DEST (set) != cc0_rtx)
6315 return 0;
6316
6317 op0 = SET_SRC (set);
6318 op1 = CONST0_RTX (GET_MODE (op0));
6319 if (earliest)
6320 *earliest = prev;
6321 }
6322#endif
6323
6324 /* If this is a COMPARE, pick up the two things being compared. */
6325 if (GET_CODE (op0) == COMPARE)
6326 {
6327 op1 = XEXP (op0, 1);
6328 op0 = XEXP (op0, 0);
6329 continue;
6330 }
6331 else if (GET_CODE (op0) != REG)
6332 break;
6333
6334 /* Go back to the previous insn. Stop if it is not an INSN. We also
6335 stop if it isn't a single set or if it has a REG_INC note because
6336 we don't want to bother dealing with it. */
6337
6338 if ((prev = prev_nonnote_insn (prev)) == 0
6339 || GET_CODE (prev) != INSN
6340 || FIND_REG_INC_NOTE (prev, 0)
6341 || (set = single_set (prev)) == 0)
6342 break;
6343
6344 /* If this is setting OP0, get what it sets it to if it looks
6345 relevant. */
6346 if (SET_DEST (set) == op0)
6347 {
6348 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
6349
6350 if ((GET_CODE (SET_SRC (set)) == COMPARE
b565a316
RK
6351 || (((code == NE
6352 || (code == LT
6353 && GET_MODE_CLASS (inner_mode) == MODE_INT
5fd8383e
RK
6354 && (GET_MODE_BITSIZE (inner_mode)
6355 <= HOST_BITS_PER_WIDE_INT)
b565a316 6356 && (STORE_FLAG_VALUE
5fd8383e
RK
6357 & ((HOST_WIDE_INT) 1
6358 << (GET_MODE_BITSIZE (inner_mode) - 1))))
b565a316
RK
6359#ifdef FLOAT_STORE_FLAG_VALUE
6360 || (code == LT
6361 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6362 && FLOAT_STORE_FLAG_VALUE < 0)
6363#endif
6364 ))
b4ad7b23
RS
6365 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
6366 x = SET_SRC (set);
b565a316
RK
6367 else if (((code == EQ
6368 || (code == GE
5fd8383e
RK
6369 && (GET_MODE_BITSIZE (inner_mode)
6370 <= HOST_BITS_PER_WIDE_INT)
b565a316
RK
6371 && GET_MODE_CLASS (inner_mode) == MODE_INT
6372 && (STORE_FLAG_VALUE
5fd8383e
RK
6373 & ((HOST_WIDE_INT) 1
6374 << (GET_MODE_BITSIZE (inner_mode) - 1))))
b565a316
RK
6375#ifdef FLOAT_STORE_FLAG_VALUE
6376 || (code == GE
6377 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6378 && FLOAT_STORE_FLAG_VALUE < 0)
fb8ca0a4 6379#endif
b565a316 6380 ))
b4ad7b23
RS
6381 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
6382 {
6383 /* We might have reversed a LT to get a GE here. But this wasn't
6384 actually the comparison of data, so we don't flag that we
6385 have had to reverse the condition. */
6386 did_reverse_condition ^= 1;
6387 reverse_code = 1;
6388 x = SET_SRC (set);
6389 }
6390 }
6391
6392 else if (reg_set_p (op0, prev))
6393 /* If this sets OP0, but not directly, we have to give up. */
6394 break;
6395
6396 if (x)
6397 {
6398 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
6399 code = GET_CODE (x);
6400 if (reverse_code)
6401 {
6402 code = reverse_condition (code);
6403 did_reverse_condition ^= 1;
6404 reverse_code = 0;
6405 }
6406
6407 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
6408 if (earliest)
6409 *earliest = prev;
6410 }
6411 }
6412
6413 /* If constant is first, put it last. */
6414 if (CONSTANT_P (op0))
6415 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
6416
6417 /* If OP0 is the result of a comparison, we weren't able to find what
6418 was really being compared, so fail. */
6419 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6420 return 0;
6421
d8cfa4ee
RK
6422 /* Canonicalize any ordered comparison with integers involving equality
6423 if we can do computations in the relevant mode and we do not
6424 overflow. */
6425
6426 if (GET_CODE (op1) == CONST_INT
6427 && GET_MODE (op0) != VOIDmode
6428 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
b4ad7b23 6429 {
5fd8383e
RK
6430 HOST_WIDE_INT const_val = INTVAL (op1);
6431 unsigned HOST_WIDE_INT uconst_val = const_val;
d8cfa4ee
RK
6432 unsigned HOST_WIDE_INT max_val
6433 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
b4ad7b23
RS
6434
6435 switch (code)
d8cfa4ee
RK
6436 {
6437 case LE:
6438 if (const_val != max_val >> 1)
6439 code = LT, op1 = GEN_INT (const_val + 1);
6440 break;
b4ad7b23 6441
d8cfa4ee
RK
6442 case GE:
6443 if (const_val
6444 != (((HOST_WIDE_INT) 1
6445 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
6446 code = GT, op1 = GEN_INT (const_val - 1);
6447 break;
b4ad7b23 6448
d8cfa4ee
RK
6449 case LEU:
6450 if (uconst_val != max_val)
6451 code = LTU, op1 = GEN_INT (uconst_val + 1);
6452 break;
b4ad7b23 6453
d8cfa4ee
RK
6454 case GEU:
6455 if (uconst_val != 0)
6456 code = GTU, op1 = GEN_INT (uconst_val - 1);
6457 break;
6458 }
b4ad7b23
RS
6459 }
6460
6461 /* If this was floating-point and we reversed anything other than an
6462 EQ or NE, return zero. */
6463 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6464 && did_reverse_condition && code != NE && code != EQ
6465 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
6466 return 0;
6467
6468#ifdef HAVE_cc0
6469 /* Never return CC0; return zero instead. */
6470 if (op0 == cc0_rtx)
6471 return 0;
6472#endif
6473
6474 return gen_rtx (code, VOIDmode, op0, op1);
6475}
6476
6477/* Similar to above routine, except that we also put an invariant last
6478 unless both operands are invariants. */
6479
6480rtx
6481get_condition_for_loop (x)
6482 rtx x;
6483{
5fd8383e 6484 rtx comparison = get_condition (x, NULL_PTR);
b4ad7b23
RS
6485
6486 if (comparison == 0
6487 || ! invariant_p (XEXP (comparison, 0))
6488 || invariant_p (XEXP (comparison, 1)))
6489 return comparison;
6490
6491 return gen_rtx (swap_condition (GET_CODE (comparison)), VOIDmode,
6492 XEXP (comparison, 1), XEXP (comparison, 0));
6493}
This page took 0.712584 seconds and 5 git commands to generate.