]> gcc.gnu.org Git - gcc.git/blame - gcc/loop.c
loop.c (strength_reduce): When doing biv->giv conversion, update reg note of NEXT...
[gcc.git] / gcc / loop.c
CommitLineData
c8465d70 1/* Perform various loop optimizations, including strength reduction.
ad83f537 2 Copyright (C) 1987, 88, 89, 91-98, 1999 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
a35311b0
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
b4ad7b23
RS
20
21
22/* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
28
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
33
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
36
37#include "config.h"
670ee920 38#include "system.h"
b4ad7b23
RS
39#include "rtl.h"
40#include "obstack.h"
41#include "expr.h"
42#include "insn-config.h"
43#include "insn-flags.h"
44#include "regs.h"
45#include "hard-reg-set.h"
46#include "recog.h"
47#include "flags.h"
48#include "real.h"
b4ad7b23 49#include "loop.h"
6adb4e3a 50#include "except.h"
2e107e9e 51#include "toplev.h"
b4ad7b23
RS
52
53/* Vector mapping INSN_UIDs to luids.
d45cf215 54 The luids are like uids but increase monotonically always.
b4ad7b23
RS
55 We use them to see whether a jump comes from outside a given loop. */
56
57int *uid_luid;
58
59/* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
60 number the insn is contained in. */
61
62int *uid_loop_num;
63
64/* 1 + largest uid of any insn. */
65
66int max_uid_for_loop;
67
68/* 1 + luid of last insn. */
69
70static int max_luid;
71
72/* Number of loops detected in current function. Used as index to the
73 next few tables. */
74
75static int max_loop_num;
76
77/* Indexed by loop number, contains the first and last insn of each loop. */
78
79static rtx *loop_number_loop_starts, *loop_number_loop_ends;
80
3ec2b590
R
81/* Likewise for the continue insn */
82static rtx *loop_number_loop_cont;
83
84/* The first code_label that is reached in every loop iteration.
85 0 when not computed yet, initially const0_rtx if a jump couldn't be
86 followed.
87 Also set to 0 when there is no such label before the NOTE_INSN_LOOP_CONT
88 of this loop, or in verify_dominator, if a jump couldn't be followed. */
89static rtx *loop_number_cont_dominator;
90
b4ad7b23
RS
91/* For each loop, gives the containing loop number, -1 if none. */
92
93int *loop_outer_loop;
94
cac8ce95
DE
95#ifdef HAVE_decrement_and_branch_on_count
96/* Records whether resource in use by inner loop. */
8c660648
JL
97
98int *loop_used_count_register;
cac8ce95 99#endif /* HAVE_decrement_and_branch_on_count */
8c660648 100
b4ad7b23
RS
101/* Indexed by loop number, contains a nonzero value if the "loop" isn't
102 really a loop (an insn outside the loop branches into it). */
103
104static char *loop_invalid;
105
106/* Indexed by loop number, links together all LABEL_REFs which refer to
107 code labels outside the loop. Used by routines that need to know all
108 loop exits, such as final_biv_value and final_giv_value.
109
110 This does not include loop exits due to return instructions. This is
111 because all bivs and givs are pseudos, and hence must be dead after a
112 return, so the presense of a return does not affect any of the
113 optimizations that use this info. It is simpler to just not include return
114 instructions on this list. */
115
116rtx *loop_number_exit_labels;
117
353127c2
RK
118/* Indexed by loop number, counts the number of LABEL_REFs on
119 loop_number_exit_labels for this loop and all loops nested inside it. */
120
121int *loop_number_exit_count;
122
9ae8ffe7 123/* Nonzero if there is a subroutine call in the current loop. */
b4ad7b23
RS
124
125static int loop_has_call;
126
552bc76f
RS
127/* Nonzero if there is a volatile memory reference in the current
128 loop. */
129
130static int loop_has_volatile;
131
8c368ee2
DE
132/* Nonzero if there is a tablejump in the current loop. */
133
134static int loop_has_tablejump;
135
b4ad7b23
RS
136/* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
137 current loop. A continue statement will generate a branch to
138 NEXT_INSN (loop_continue). */
139
140static rtx loop_continue;
141
142/* Indexed by register number, contains the number of times the reg
143 is set during the loop being scanned.
144 During code motion, a negative value indicates a reg that has been
145 made a candidate; in particular -2 means that it is an candidate that
c5b7917e 146 we know is equal to a constant and -1 means that it is an candidate
b4ad7b23
RS
147 not known equal to a constant.
148 After code motion, regs moved have 0 (which is accurate now)
149 while the failed candidates have the original number of times set.
150
151 Therefore, at all times, == 0 indicates an invariant register;
152 < 0 a conditionally invariant one. */
153
4b259e3f 154static varray_type set_in_loop;
b4ad7b23 155
4b259e3f 156/* Original value of set_in_loop; same except that this value
b4ad7b23
RS
157 is not set negative for a reg whose sets have been made candidates
158 and not set to 0 for a reg that is moved. */
159
4b259e3f 160static varray_type n_times_set;
b4ad7b23
RS
161
162/* Index by register number, 1 indicates that the register
163 cannot be moved or strength reduced. */
164
8deb8e2c 165static varray_type may_not_optimize;
b4ad7b23 166
d6b44532
RH
167/* Contains the insn in which a register was used if it was used
168 exactly once; contains const0_rtx if it was used more than once. */
169
170static varray_type reg_single_usage;
171
b4ad7b23
RS
172/* Nonzero means reg N has already been moved out of one loop.
173 This reduces the desire to move it out of another. */
174
175static char *moved_once;
176
5026a502 177/* List of MEMs that are stored in this loop. */
b4ad7b23 178
5026a502 179static rtx loop_store_mems;
b4ad7b23 180
2d4fde68
R
181/* The insn where the first of these was found. */
182static rtx first_loop_store_insn;
183
41a972a9
MM
184typedef struct loop_mem_info {
185 rtx mem; /* The MEM itself. */
186 rtx reg; /* Corresponding pseudo, if any. */
187 int optimize; /* Nonzero if we can optimize access to this MEM. */
188} loop_mem_info;
189
190/* Array of MEMs that are used (read or written) in this loop, but
191 cannot be aliased by anything in this loop, except perhaps
192 themselves. In other words, if loop_mems[i] is altered during the
193 loop, it is altered by an expression that is rtx_equal_p to it. */
194
195static loop_mem_info *loop_mems;
196
197/* The index of the next available slot in LOOP_MEMS. */
198
199static int loop_mems_idx;
200
201/* The number of elements allocated in LOOP_MEMs. */
202
203static int loop_mems_allocated;
204
b4ad7b23 205/* Nonzero if we don't know what MEMs were changed in the current loop.
552bc76f 206 This happens if the loop contains a call (in which case `loop_has_call'
b4ad7b23
RS
207 will also be set) or if we store into more than NUM_STORES MEMs. */
208
209static int unknown_address_altered;
210
211/* Count of movable (i.e. invariant) instructions discovered in the loop. */
212static int num_movables;
213
214/* Count of memory write instructions discovered in the loop. */
215static int num_mem_sets;
216
217/* Number of loops contained within the current one, including itself. */
218static int loops_enclosed;
219
220/* Bound on pseudo register number before loop optimization.
221 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
222int max_reg_before_loop;
223
224/* This obstack is used in product_cheap_p to allocate its rtl. It
225 may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
226 If we used the same obstack that it did, we would be deallocating
227 that array. */
228
229static struct obstack temp_obstack;
230
231/* This is where the pointer to the obstack being used for RTL is stored. */
232
233extern struct obstack *rtl_obstack;
234
235#define obstack_chunk_alloc xmalloc
236#define obstack_chunk_free free
b4ad7b23
RS
237\f
238/* During the analysis of a loop, a chain of `struct movable's
239 is made to record all the movable insns found.
240 Then the entire chain can be scanned to decide which to move. */
241
242struct movable
243{
244 rtx insn; /* A movable insn */
0f41302f
MS
245 rtx set_src; /* The expression this reg is set from. */
246 rtx set_dest; /* The destination of this SET. */
b4ad7b23 247 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
0f41302f 248 of any registers used within the LIBCALL. */
b4ad7b23
RS
249 int consec; /* Number of consecutive following insns
250 that must be moved with this one. */
251 int regno; /* The register it sets */
252 short lifetime; /* lifetime of that register;
253 may be adjusted when matching movables
254 that load the same value are found. */
255 short savings; /* Number of insns we can move for this reg,
256 including other movables that force this
257 or match this one. */
258 unsigned int cond : 1; /* 1 if only conditionally movable */
259 unsigned int force : 1; /* 1 means MUST move this insn */
260 unsigned int global : 1; /* 1 means reg is live outside this loop */
261 /* If PARTIAL is 1, GLOBAL means something different:
262 that the reg is live outside the range from where it is set
263 to the following label. */
264 unsigned int done : 1; /* 1 inhibits further processing of this */
265
266 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
267 In particular, moving it does not make it
268 invariant. */
269 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
270 load SRC, rather than copying INSN. */
1a61c29f
JW
271 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
272 first insn of a consecutive sets group. */
0f41302f 273 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
b4ad7b23
RS
274 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
275 that we should avoid changing when clearing
276 the rest of the reg. */
277 struct movable *match; /* First entry for same value */
278 struct movable *forces; /* An insn that must be moved if this is */
279 struct movable *next;
280};
281
45f97e2e
RH
282static struct movable *the_movables;
283
b4ad7b23
RS
284FILE *loop_dump_stream;
285
286/* Forward declarations. */
287
3ec2b590 288static void verify_dominator PROTO((int));
56c0e996
BS
289static void find_and_verify_loops PROTO((rtx));
290static void mark_loop_jump PROTO((rtx, int));
291static void prescan_loop PROTO((rtx, rtx));
292static int reg_in_basic_block_p PROTO((rtx, rtx));
293static int consec_sets_invariant_p PROTO((rtx, int, rtx));
56c0e996 294static int labels_in_range_p PROTO((rtx, int));
237228c0
BS
295static void count_one_set PROTO((rtx, rtx, varray_type, rtx *));
296
8deb8e2c
MM
297static void count_loop_regs_set PROTO((rtx, rtx, varray_type, varray_type,
298 int *, int));
693e265f 299static void note_addr_stored PROTO((rtx, rtx));
56c0e996 300static int loop_reg_used_before_p PROTO((rtx, rtx, rtx, rtx, rtx));
6dd49eb4 301static void scan_loop PROTO((rtx, rtx, rtx, int, int));
e9a25f70 302#if 0
27fa83c1 303static void replace_call_address PROTO((rtx, rtx, rtx));
e9a25f70 304#endif
56c0e996
BS
305static rtx skip_consec_insns PROTO((rtx, int));
306static int libcall_benefit PROTO((rtx));
307static void ignore_some_movables PROTO((struct movable *));
308static void force_movables PROTO((struct movable *));
309static void combine_movables PROTO((struct movable *, int));
fd4a1ca5 310static int regs_match_p PROTO((rtx, rtx, struct movable *));
56c0e996 311static int rtx_equal_for_loop_p PROTO((rtx, rtx, struct movable *));
e009aaf3 312static void add_label_notes PROTO((rtx, rtx));
56c0e996 313static void move_movables PROTO((struct movable *, int, int, rtx, rtx, int));
e009aaf3 314static int count_nonfixed_reads PROTO((rtx));
6dd49eb4 315static void strength_reduce PROTO((rtx, rtx, rtx, int, rtx, rtx, rtx, int, int));
8deb8e2c 316static void find_single_use_in_loop PROTO((rtx, rtx, varray_type));
56c0e996
BS
317static int valid_initial_value_p PROTO((rtx, rtx, int, rtx));
318static void find_mem_givs PROTO((rtx, rtx, int, rtx, rtx));
3ec2b590 319static void record_biv PROTO((struct induction *, rtx, rtx, rtx, rtx, rtx *, int, int));
302670f3
MH
320static void check_final_value PROTO((struct induction *, rtx, rtx,
321 unsigned HOST_WIDE_INT));
56c0e996
BS
322static void record_giv PROTO((struct induction *, rtx, rtx, rtx, rtx, rtx, int, enum g_types, int, rtx *, rtx, rtx));
323static void update_giv_derive PROTO((rtx));
3ec2b590 324static int basic_induction_var PROTO((rtx, enum machine_mode, rtx, rtx, rtx *, rtx *, rtx **));
56c0e996 325static rtx simplify_giv_expr PROTO((rtx, int *));
45f97e2e 326static int general_induction_var PROTO((rtx, rtx *, rtx *, rtx *, int, int *));
a07516d3 327static int consec_sets_giv PROTO((int, rtx, rtx, rtx, rtx *, rtx *, rtx *));
5629b16c 328static int check_dbra_loop PROTO((rtx, int, rtx, struct loop_info *));
45f97e2e 329static rtx express_from_1 PROTO((rtx, rtx, rtx));
45f97e2e 330static rtx combine_givs_p PROTO((struct induction *, struct induction *));
56c0e996 331static void combine_givs PROTO((struct iv_class *));
3ec2b590 332struct recombine_givs_stats;
96df87b8 333static int find_life_end PROTO((rtx, struct recombine_givs_stats *, rtx, rtx));
53dc05e4 334static void recombine_givs PROTO((struct iv_class *, rtx, rtx, int));
56c0e996
BS
335static int product_cheap_p PROTO((rtx, rtx));
336static int maybe_eliminate_biv PROTO((struct iv_class *, rtx, rtx, int, int, int));
337static int maybe_eliminate_biv_1 PROTO((rtx, rtx, struct iv_class *, int, rtx));
338static int last_use_this_basic_block PROTO((rtx, rtx));
339static void record_initial PROTO((rtx, rtx));
340static void update_reg_last_use PROTO((rtx, rtx));
41a972a9
MM
341static rtx next_insn_in_loop PROTO((rtx, rtx, rtx, rtx));
342static void load_mems_and_recount_loop_regs_set PROTO((rtx, rtx, rtx,
d6b44532 343 rtx, int *));
41a972a9
MM
344static void load_mems PROTO((rtx, rtx, rtx, rtx));
345static int insert_loop_mem PROTO((rtx *, void *));
346static int replace_loop_mem PROTO((rtx *, void *));
347static int replace_label PROTO((rtx *, void *));
348
349typedef struct rtx_and_int {
350 rtx r;
351 int i;
352} rtx_and_int;
353
354typedef struct rtx_pair {
355 rtx r1;
356 rtx r2;
357} rtx_pair;
358
359/* Nonzero iff INSN is between START and END, inclusive. */
360#define INSN_IN_RANGE_P(INSN, START, END) \
361 (INSN_UID (INSN) < max_uid_for_loop \
362 && INSN_LUID (INSN) >= INSN_LUID (START) \
363 && INSN_LUID (INSN) <= INSN_LUID (END))
8c660648 364
51723711 365#ifdef HAVE_decrement_and_branch_on_count
cac8ce95 366/* Test whether BCT applicable and safe. */
302670f3 367static void insert_bct PROTO((rtx, rtx, struct loop_info *));
8c660648 368
cac8ce95 369/* Auxiliary function that inserts the BCT pattern into the loop. */
56c0e996 370static void instrument_loop_bct PROTO((rtx, rtx, rtx));
51723711 371#endif /* HAVE_decrement_and_branch_on_count */
8c660648 372
2a1777af
JL
373/* Indirect_jump_in_function is computed once per function. */
374int indirect_jump_in_function = 0;
56c0e996 375static int indirect_jump_in_function_p PROTO((rtx));
2a1777af 376
a6207a2b
R
377static int compute_luids PROTO((rtx, rtx, int));
378
a6207a2b
R
379static int biv_elimination_giv_has_0_offset PROTO((struct induction *,
380 struct induction *, rtx));
b4ad7b23
RS
381\f
382/* Relative gain of eliminating various kinds of operations. */
45f97e2e 383static int add_cost;
b4ad7b23 384#if 0
45f97e2e
RH
385static int shift_cost;
386static int mult_cost;
b4ad7b23
RS
387#endif
388
389/* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
390 copy the value of the strength reduced giv to its original register. */
45f97e2e
RH
391static int copy_cost;
392
393/* Cost of using a register, to normalize the benefits of a giv. */
394static int reg_address_cost;
395
b4ad7b23
RS
396
397void
398init_loop ()
399{
400 char *free_point = (char *) oballoc (1);
38a448ca 401 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
b4ad7b23 402
38a448ca 403 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
b4ad7b23 404
45f97e2e
RH
405#ifdef ADDRESS_COST
406 reg_address_cost = ADDRESS_COST (reg);
407#else
408 reg_address_cost = rtx_cost (reg, MEM);
409#endif
410
b4ad7b23
RS
411 /* We multiply by 2 to reconcile the difference in scale between
412 these two ways of computing costs. Otherwise the cost of a copy
413 will be far less than the cost of an add. */
5fd8383e 414
b4ad7b23 415 copy_cost = 2 * 2;
b4ad7b23
RS
416
417 /* Free the objects we just allocated. */
418 obfree (free_point);
419
420 /* Initialize the obstack used for rtl in product_cheap_p. */
421 gcc_obstack_init (&temp_obstack);
422}
423\f
3ec2b590
R
424/* Compute the mapping from uids to luids.
425 LUIDs are numbers assigned to insns, like uids,
426 except that luids increase monotonically through the code.
427 Start at insn START and stop just before END. Assign LUIDs
428 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
429static int
430compute_luids (start, end, prev_luid)
431 rtx start, end;
432 int prev_luid;
433{
434 int i;
435 rtx insn;
436
437 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
438 {
439 if (INSN_UID (insn) >= max_uid_for_loop)
440 continue;
441 /* Don't assign luids to line-number NOTEs, so that the distance in
442 luids between two insns is not affected by -g. */
443 if (GET_CODE (insn) != NOTE
444 || NOTE_LINE_NUMBER (insn) <= 0)
445 uid_luid[INSN_UID (insn)] = ++i;
446 else
447 /* Give a line number note the same luid as preceding insn. */
448 uid_luid[INSN_UID (insn)] = i;
449 }
450 return i + 1;
451}
452\f
b4ad7b23
RS
453/* Entry point of this file. Perform loop optimization
454 on the current function. F is the first insn of the function
455 and DUMPFILE is a stream for output of a trace of actions taken
456 (or 0 if none should be output). */
457
458void
5accd822 459loop_optimize (f, dumpfile, unroll_p, bct_p)
b4ad7b23
RS
460 /* f is the first instruction of a chain of insns for one function */
461 rtx f;
462 FILE *dumpfile;
5accd822 463 int unroll_p, bct_p;
b4ad7b23
RS
464{
465 register rtx insn;
466 register int i;
b4ad7b23
RS
467
468 loop_dump_stream = dumpfile;
469
470 init_recog_no_volatile ();
b4ad7b23
RS
471
472 max_reg_before_loop = max_reg_num ();
473
474 moved_once = (char *) alloca (max_reg_before_loop);
475 bzero (moved_once, max_reg_before_loop);
476
477 regs_may_share = 0;
478
0f41302f 479 /* Count the number of loops. */
b4ad7b23
RS
480
481 max_loop_num = 0;
482 for (insn = f; insn; insn = NEXT_INSN (insn))
483 {
484 if (GET_CODE (insn) == NOTE
485 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
486 max_loop_num++;
487 }
488
489 /* Don't waste time if no loops. */
490 if (max_loop_num == 0)
491 return;
492
493 /* Get size to use for tables indexed by uids.
494 Leave some space for labels allocated by find_and_verify_loops. */
1c01e9df 495 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
b4ad7b23
RS
496
497 uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
498 uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
499
4c9a05bc
RK
500 bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
501 bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
b4ad7b23
RS
502
503 /* Allocate tables for recording each loop. We set each entry, so they need
504 not be zeroed. */
505 loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
506 loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
3ec2b590
R
507 loop_number_loop_cont = (rtx *) alloca (max_loop_num * sizeof (rtx));
508 loop_number_cont_dominator = (rtx *) alloca (max_loop_num * sizeof (rtx));
b4ad7b23
RS
509 loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
510 loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
511 loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
353127c2 512 loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
b4ad7b23 513
cac8ce95 514#ifdef HAVE_decrement_and_branch_on_count
8c660648 515 /* Allocate for BCT optimization */
8c660648
JL
516 loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
517 bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
cac8ce95 518#endif /* HAVE_decrement_and_branch_on_count */
8c660648 519
b4ad7b23
RS
520 /* Find and process each loop.
521 First, find them, and record them in order of their beginnings. */
522 find_and_verify_loops (f);
523
524 /* Now find all register lifetimes. This must be done after
525 find_and_verify_loops, because it might reorder the insns in the
526 function. */
527 reg_scan (f, max_reg_num (), 1);
528
7506f491
DE
529 /* This must occur after reg_scan so that registers created by gcse
530 will have entries in the register tables.
531
532 We could have added a call to reg_scan after gcse_main in toplev.c,
533 but moving this call to init_alias_analysis is more efficient. */
534 init_alias_analysis ();
535
e318cec0
R
536 /* See if we went too far. Note that get_max_uid already returns
537 one more that the maximum uid of all insn. */
1c01e9df
TW
538 if (get_max_uid () > max_uid_for_loop)
539 abort ();
f5963e61 540 /* Now reset it to the actual size we need. See above. */
e318cec0 541 max_uid_for_loop = get_max_uid ();
1c01e9df 542
3ec2b590
R
543 /* find_and_verify_loops has already called compute_luids, but it might
544 have rearranged code afterwards, so we need to recompute the luids now. */
545 max_luid = compute_luids (f, NULL_RTX, 0);
b4ad7b23
RS
546
547 /* Don't leave gaps in uid_luid for insns that have been
548 deleted. It is possible that the first or last insn
549 using some register has been deleted by cross-jumping.
550 Make sure that uid_luid for that former insn's uid
551 points to the general area where that insn used to be. */
552 for (i = 0; i < max_uid_for_loop; i++)
553 {
554 uid_luid[0] = uid_luid[i];
555 if (uid_luid[0] != 0)
556 break;
557 }
558 for (i = 0; i < max_uid_for_loop; i++)
559 if (uid_luid[i] == 0)
560 uid_luid[i] = uid_luid[i - 1];
561
562 /* Create a mapping from loops to BLOCK tree nodes. */
81797aba 563 if (unroll_p && write_symbols != NO_DEBUG)
07e857c2 564 find_loop_tree_blocks ();
b4ad7b23 565
2a1777af
JL
566 /* Determine if the function has indirect jump. On some systems
567 this prevents low overhead loop instructions from being used. */
8c660648 568 indirect_jump_in_function = indirect_jump_in_function_p (f);
8c660648 569
b4ad7b23
RS
570 /* Now scan the loops, last ones first, since this means inner ones are done
571 before outer ones. */
572 for (i = max_loop_num-1; i >= 0; i--)
573 if (! loop_invalid[i] && loop_number_loop_ends[i])
574 scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
6dd49eb4 575 loop_number_loop_cont[i], unroll_p, bct_p);
07e857c2
JW
576
577 /* If debugging and unrolling loops, we must replicate the tree nodes
578 corresponding to the blocks inside the loop, so that the original one
579 to one mapping will remain. */
81797aba 580 if (unroll_p && write_symbols != NO_DEBUG)
07e857c2 581 unroll_block_trees ();
45f97e2e
RH
582
583 end_alias_analysis ();
b4ad7b23
RS
584}
585\f
41a972a9
MM
586/* Returns the next insn, in execution order, after INSN. START and
587 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
588 respectively. LOOP_TOP, if non-NULL, is the top of the loop in the
589 insn-stream; it is used with loops that are entered near the
590 bottom. */
591
592static rtx
593next_insn_in_loop (insn, start, end, loop_top)
594 rtx insn;
595 rtx start;
596 rtx end;
597 rtx loop_top;
598{
599 insn = NEXT_INSN (insn);
600
601 if (insn == end)
602 {
603 if (loop_top)
604 /* Go to the top of the loop, and continue there. */
605 insn = loop_top;
606 else
607 /* We're done. */
608 insn = NULL_RTX;
609 }
610
611 if (insn == start)
612 /* We're done. */
613 insn = NULL_RTX;
614
615 return insn;
616}
617
b4ad7b23
RS
618/* Optimize one loop whose start is LOOP_START and end is END.
619 LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
6dd49eb4
R
620 NOTE_INSN_LOOP_END.
621 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
b4ad7b23
RS
622
623/* ??? Could also move memory writes out of loops if the destination address
624 is invariant, the source is invariant, the memory write is not volatile,
625 and if we can prove that no read inside the loop can read this address
626 before the write occurs. If there is a read of this address after the
627 write, then we can also mark the memory read as invariant. */
628
629static void
6dd49eb4
R
630scan_loop (loop_start, end, loop_cont, unroll_p, bct_p)
631 rtx loop_start, end, loop_cont;
5accd822 632 int unroll_p, bct_p;
b4ad7b23
RS
633{
634 register int i;
41a972a9 635 rtx p;
b4ad7b23
RS
636 /* 1 if we are scanning insns that could be executed zero times. */
637 int maybe_never = 0;
638 /* 1 if we are scanning insns that might never be executed
639 due to a subroutine call which might exit before they are reached. */
640 int call_passed = 0;
641 /* For a rotated loop that is entered near the bottom,
642 this is the label at the top. Otherwise it is zero. */
643 rtx loop_top = 0;
644 /* Jump insn that enters the loop, or 0 if control drops in. */
645 rtx loop_entry_jump = 0;
646 /* Place in the loop where control enters. */
647 rtx scan_start;
648 /* Number of insns in the loop. */
649 int insn_count;
650 int in_libcall = 0;
651 int tem;
652 rtx temp;
653 /* The SET from an insn, if it is the only SET in the insn. */
654 rtx set, set1;
655 /* Chain describing insns movable in current loop. */
656 struct movable *movables = 0;
657 /* Last element in `movables' -- so we can add elements at the end. */
658 struct movable *last_movable = 0;
659 /* Ratio of extra register life span we can justify
660 for saving an instruction. More if loop doesn't call subroutines
661 since in that case saving an insn makes more difference
662 and more registers are available. */
663 int threshold;
5ea7a4ae
JW
664 /* Nonzero if we are scanning instructions in a sub-loop. */
665 int loop_depth = 0;
41a972a9 666 int nregs;
b4ad7b23
RS
667
668 /* Determine whether this loop starts with a jump down to a test at
669 the end. This will occur for a small number of loops with a test
670 that is too complex to duplicate in front of the loop.
671
672 We search for the first insn or label in the loop, skipping NOTEs.
673 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
674 (because we might have a loop executed only once that contains a
675 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
676 (in case we have a degenerate loop).
677
678 Note that if we mistakenly think that a loop is entered at the top
679 when, in fact, it is entered at the exit test, the only effect will be
680 slightly poorer optimization. Making the opposite error can generate
681 incorrect code. Since very few loops now start with a jump to the
682 exit test, the code here to detect that case is very conservative. */
683
684 for (p = NEXT_INSN (loop_start);
685 p != end
686 && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
687 && (GET_CODE (p) != NOTE
688 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
689 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
690 p = NEXT_INSN (p))
691 ;
692
693 scan_start = p;
694
695 /* Set up variables describing this loop. */
696 prescan_loop (loop_start, end);
697 threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
698
699 /* If loop has a jump before the first label,
700 the true entry is the target of that jump.
701 Start scan from there.
702 But record in LOOP_TOP the place where the end-test jumps
703 back to so we can scan that after the end of the loop. */
704 if (GET_CODE (p) == JUMP_INSN)
705 {
706 loop_entry_jump = p;
707
708 /* Loop entry must be unconditional jump (and not a RETURN) */
709 if (simplejump_p (p)
710 && JUMP_LABEL (p) != 0
711 /* Check to see whether the jump actually
712 jumps out of the loop (meaning it's no loop).
713 This case can happen for things like
714 do {..} while (0). If this label was generated previously
715 by loop, we can't tell anything about it and have to reject
716 the loop. */
41a972a9 717 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, end))
b4ad7b23
RS
718 {
719 loop_top = next_label (scan_start);
720 scan_start = JUMP_LABEL (p);
721 }
722 }
723
724 /* If SCAN_START was an insn created by loop, we don't know its luid
725 as required by loop_reg_used_before_p. So skip such loops. (This
726 test may never be true, but it's best to play it safe.)
727
728 Also, skip loops where we do not start scanning at a label. This
729 test also rejects loops starting with a JUMP_INSN that failed the
730 test above. */
731
732 if (INSN_UID (scan_start) >= max_uid_for_loop
733 || GET_CODE (scan_start) != CODE_LABEL)
734 {
735 if (loop_dump_stream)
736 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
737 INSN_UID (loop_start), INSN_UID (end));
738 return;
739 }
740
741 /* Count number of times each reg is set during this loop.
8deb8e2c 742 Set VARRAY_CHAR (may_not_optimize, I) if it is not safe to move out
d6b44532 743 the setting of register I. Set VARRAY_RTX (reg_single_usage, I). */
41a972a9
MM
744
745 /* Allocate extra space for REGS that might be created by
8deb8e2c
MM
746 load_mems. We allocate a little extra slop as well, in the hopes
747 that even after the moving of movables creates some new registers
748 we won't have to reallocate these arrays. However, we do grow
749 the arrays, if necessary, in load_mems_recount_loop_regs_set. */
750 nregs = max_reg_num () + loop_mems_idx + 16;
4b259e3f 751 VARRAY_INT_INIT (set_in_loop, nregs, "set_in_loop");
8deb8e2c 752 VARRAY_INT_INIT (n_times_set, nregs, "n_times_set");
8deb8e2c 753 VARRAY_CHAR_INIT (may_not_optimize, nregs, "may_not_optimize");
d6b44532 754 VARRAY_RTX_INIT (reg_single_usage, nregs, "reg_single_usage");
b4ad7b23
RS
755
756 count_loop_regs_set (loop_top ? loop_top : loop_start, end,
757 may_not_optimize, reg_single_usage, &insn_count, nregs);
758
759 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8deb8e2c
MM
760 {
761 VARRAY_CHAR (may_not_optimize, i) = 1;
4b259e3f 762 VARRAY_INT (set_in_loop, i) = 1;
8deb8e2c 763 }
ef9e3c5b
JL
764
765#ifdef AVOID_CCMODE_COPIES
766 /* Don't try to move insns which set CC registers if we should not
767 create CCmode register copies. */
3568fdd2 768 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
ef9e3c5b 769 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
8deb8e2c 770 VARRAY_CHAR (may_not_optimize, i) = 1;
ef9e3c5b
JL
771#endif
772
4b259e3f
R
773 bcopy ((char *) &set_in_loop->data,
774 (char *) &n_times_set->data, nregs * sizeof (int));
b4ad7b23
RS
775
776 if (loop_dump_stream)
777 {
778 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
779 INSN_UID (loop_start), INSN_UID (end), insn_count);
780 if (loop_continue)
781 fprintf (loop_dump_stream, "Continue at insn %d.\n",
782 INSN_UID (loop_continue));
783 }
784
785 /* Scan through the loop finding insns that are safe to move.
4b259e3f 786 Set set_in_loop negative for the reg being set, so that
b4ad7b23
RS
787 this reg will be considered invariant for subsequent insns.
788 We consider whether subsequent insns use the reg
789 in deciding whether it is worth actually moving.
790
791 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
792 and therefore it is possible that the insns we are scanning
793 would never be executed. At such times, we must make sure
794 that it is safe to execute the insn once instead of zero times.
795 When MAYBE_NEVER is 0, all insns will be executed at least once
796 so that is not a problem. */
797
41a972a9
MM
798 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
799 p != NULL_RTX;
800 p = next_insn_in_loop (p, scan_start, end, loop_top))
b4ad7b23 801 {
b4ad7b23 802 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5fd8383e 803 && find_reg_note (p, REG_LIBCALL, NULL_RTX))
b4ad7b23
RS
804 in_libcall = 1;
805 else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5fd8383e 806 && find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
807 in_libcall = 0;
808
809 if (GET_CODE (p) == INSN
810 && (set = single_set (p))
811 && GET_CODE (SET_DEST (set)) == REG
8deb8e2c 812 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
b4ad7b23
RS
813 {
814 int tem1 = 0;
815 int tem2 = 0;
816 int move_insn = 0;
817 rtx src = SET_SRC (set);
818 rtx dependencies = 0;
819
820 /* Figure out what to use as a source of this insn. If a REG_EQUIV
821 note is given or if a REG_EQUAL note with a constant operand is
822 specified, use it as the source and mark that we should move
823 this insn by calling emit_move_insn rather that duplicating the
824 insn.
825
826 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
827 is present. */
5fd8383e 828 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
b4ad7b23
RS
829 if (temp)
830 src = XEXP (temp, 0), move_insn = 1;
831 else
832 {
5fd8383e 833 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
b4ad7b23
RS
834 if (temp && CONSTANT_P (XEXP (temp, 0)))
835 src = XEXP (temp, 0), move_insn = 1;
5fd8383e 836 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
837 {
838 src = XEXP (temp, 0);
839 /* A libcall block can use regs that don't appear in
840 the equivalent expression. To move the libcall,
841 we must move those regs too. */
842 dependencies = libcall_other_reg (p, src);
843 }
844 }
845
846 /* Don't try to optimize a register that was made
847 by loop-optimization for an inner loop.
848 We don't know its life-span, so we can't compute the benefit. */
849 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
850 ;
77854601 851 else if (/* The register is used in basic blocks other
95ca22f4
MM
852 than the one where it is set (meaning that
853 something after this point in the loop might
854 depend on its value before the set). */
77854601
MH
855 ! reg_in_basic_block_p (p, SET_DEST (set))
856 /* And the set is not guaranteed to be executed one
857 the loop starts, or the value before the set is
858 needed before the set occurs...
859
860 ??? Note we have quadratic behaviour here, mitigated
861 by the fact that the previous test will often fail for
862 large loops. Rather than re-scanning the entire loop
863 each time for register usage, we should build tables
864 of the register usage and use them here instead. */
865 && (maybe_never
866 || loop_reg_used_before_p (set, p, loop_start,
867 scan_start, end)))
e1f7435e
JL
868 /* It is unsafe to move the set.
869
870 This code used to consider it OK to move a set of a variable
871 which was not created by the user and not used in an exit test.
872 That behavior is incorrect and was removed. */
b4ad7b23
RS
873 ;
874 else if ((tem = invariant_p (src))
875 && (dependencies == 0
876 || (tem2 = invariant_p (dependencies)) != 0)
4b259e3f 877 && (VARRAY_INT (set_in_loop,
8deb8e2c 878 REGNO (SET_DEST (set))) == 1
b4ad7b23 879 || (tem1
8deb8e2c
MM
880 = consec_sets_invariant_p
881 (SET_DEST (set),
4b259e3f 882 VARRAY_INT (set_in_loop, REGNO (SET_DEST (set))),
8deb8e2c 883 p)))
b4ad7b23
RS
884 /* If the insn can cause a trap (such as divide by zero),
885 can't move it unless it's guaranteed to be executed
886 once loop is entered. Even a function call might
887 prevent the trap insn from being reached
888 (since it might exit!) */
889 && ! ((maybe_never || call_passed)
890 && may_trap_p (src)))
891 {
892 register struct movable *m;
893 register int regno = REGNO (SET_DEST (set));
894
895 /* A potential lossage is where we have a case where two insns
896 can be combined as long as they are both in the loop, but
897 we move one of them outside the loop. For large loops,
898 this can lose. The most common case of this is the address
899 of a function being called.
900
901 Therefore, if this register is marked as being used exactly
902 once if we are in a loop with calls (a "large loop"), see if
903 we can replace the usage of this register with the source
904 of this SET. If we can, delete this insn.
905
906 Don't do this if P has a REG_RETVAL note or if we have
907 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
908
d6b44532
RH
909 if (loop_has_call
910 && VARRAY_RTX (reg_single_usage, regno) != 0
8deb8e2c 911 && VARRAY_RTX (reg_single_usage, regno) != const0_rtx
b1f21e0a
MM
912 && REGNO_FIRST_UID (regno) == INSN_UID (p)
913 && (REGNO_LAST_UID (regno)
8deb8e2c 914 == INSN_UID (VARRAY_RTX (reg_single_usage, regno)))
4b259e3f 915 && VARRAY_INT (set_in_loop, regno) == 1
b4ad7b23 916 && ! side_effects_p (SET_SRC (set))
5fd8383e 917 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
e9a25f70
JL
918 && (! SMALL_REGISTER_CLASSES
919 || (! (GET_CODE (SET_SRC (set)) == REG
920 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
b4ad7b23
RS
921 /* This test is not redundant; SET_SRC (set) might be
922 a call-clobbered register and the life of REGNO
923 might span a call. */
924 && ! modified_between_p (SET_SRC (set), p,
8deb8e2c
MM
925 VARRAY_RTX
926 (reg_single_usage, regno))
927 && no_labels_between_p (p, VARRAY_RTX (reg_single_usage, regno))
b4ad7b23 928 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
8deb8e2c
MM
929 VARRAY_RTX
930 (reg_single_usage, regno)))
b4ad7b23 931 {
5eeedd4d
JW
932 /* Replace any usage in a REG_EQUAL note. Must copy the
933 new source, so that we don't get rtx sharing between the
934 SET_SOURCE and REG_NOTES of insn p. */
8deb8e2c
MM
935 REG_NOTES (VARRAY_RTX (reg_single_usage, regno))
936 = replace_rtx (REG_NOTES (VARRAY_RTX
937 (reg_single_usage, regno)),
5eeedd4d 938 SET_DEST (set), copy_rtx (SET_SRC (set)));
b4ad7b23
RS
939
940 PUT_CODE (p, NOTE);
941 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
942 NOTE_SOURCE_FILE (p) = 0;
4b259e3f 943 VARRAY_INT (set_in_loop, regno) = 0;
b4ad7b23
RS
944 continue;
945 }
946
947 m = (struct movable *) alloca (sizeof (struct movable));
948 m->next = 0;
949 m->insn = p;
950 m->set_src = src;
951 m->dependencies = dependencies;
952 m->set_dest = SET_DEST (set);
953 m->force = 0;
4b259e3f 954 m->consec = VARRAY_INT (set_in_loop,
8deb8e2c 955 REGNO (SET_DEST (set))) - 1;
b4ad7b23
RS
956 m->done = 0;
957 m->forces = 0;
958 m->partial = 0;
959 m->move_insn = move_insn;
1a61c29f 960 m->move_insn_first = 0;
5fd8383e 961 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
b4ad7b23
RS
962 m->savemode = VOIDmode;
963 m->regno = regno;
964 /* Set M->cond if either invariant_p or consec_sets_invariant_p
965 returned 2 (only conditionally invariant). */
966 m->cond = ((tem | tem1 | tem2) > 1);
b1f21e0a
MM
967 m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
968 || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
b4ad7b23 969 m->match = 0;
b1f21e0a
MM
970 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
971 - uid_luid[REGNO_FIRST_UID (regno)]);
4b259e3f 972 m->savings = VARRAY_INT (n_times_set, regno);
5fd8383e 973 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23 974 m->savings += libcall_benefit (p);
4b259e3f 975 VARRAY_INT (set_in_loop, regno) = move_insn ? -2 : -1;
b4ad7b23
RS
976 /* Add M to the end of the chain MOVABLES. */
977 if (movables == 0)
978 movables = m;
979 else
980 last_movable->next = m;
981 last_movable = m;
982
983 if (m->consec > 0)
984 {
1a61c29f
JW
985 /* It is possible for the first instruction to have a
986 REG_EQUAL note but a non-invariant SET_SRC, so we must
987 remember the status of the first instruction in case
988 the last instruction doesn't have a REG_EQUAL note. */
989 m->move_insn_first = m->move_insn;
990
b4ad7b23 991 /* Skip this insn, not checking REG_LIBCALL notes. */
202a34fd 992 p = next_nonnote_insn (p);
b4ad7b23
RS
993 /* Skip the consecutive insns, if there are any. */
994 p = skip_consec_insns (p, m->consec);
995 /* Back up to the last insn of the consecutive group. */
996 p = prev_nonnote_insn (p);
997
998 /* We must now reset m->move_insn, m->is_equiv, and possibly
999 m->set_src to correspond to the effects of all the
1000 insns. */
5fd8383e 1001 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
b4ad7b23
RS
1002 if (temp)
1003 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1004 else
1005 {
5fd8383e 1006 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
b4ad7b23
RS
1007 if (temp && CONSTANT_P (XEXP (temp, 0)))
1008 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1009 else
1010 m->move_insn = 0;
1011
1012 }
5fd8383e 1013 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
b4ad7b23
RS
1014 }
1015 }
1016 /* If this register is always set within a STRICT_LOW_PART
1017 or set to zero, then its high bytes are constant.
1018 So clear them outside the loop and within the loop
1019 just load the low bytes.
1020 We must check that the machine has an instruction to do so.
1021 Also, if the value loaded into the register
1022 depends on the same register, this cannot be done. */
1023 else if (SET_SRC (set) == const0_rtx
1024 && GET_CODE (NEXT_INSN (p)) == INSN
1025 && (set1 = single_set (NEXT_INSN (p)))
1026 && GET_CODE (set1) == SET
1027 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1028 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1029 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1030 == SET_DEST (set))
1031 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1032 {
1033 register int regno = REGNO (SET_DEST (set));
4b259e3f 1034 if (VARRAY_INT (set_in_loop, regno) == 2)
b4ad7b23
RS
1035 {
1036 register struct movable *m;
1037 m = (struct movable *) alloca (sizeof (struct movable));
1038 m->next = 0;
1039 m->insn = p;
1040 m->set_dest = SET_DEST (set);
1041 m->dependencies = 0;
1042 m->force = 0;
1043 m->consec = 0;
1044 m->done = 0;
1045 m->forces = 0;
1046 m->move_insn = 0;
8cf619da 1047 m->move_insn_first = 0;
b4ad7b23
RS
1048 m->partial = 1;
1049 /* If the insn may not be executed on some cycles,
1050 we can't clear the whole reg; clear just high part.
1051 Not even if the reg is used only within this loop.
1052 Consider this:
1053 while (1)
1054 while (s != t) {
1055 if (foo ()) x = *s;
1056 use (x);
1057 }
1058 Clearing x before the inner loop could clobber a value
1059 being saved from the last time around the outer loop.
1060 However, if the reg is not used outside this loop
1061 and all uses of the register are in the same
1062 basic block as the store, there is no problem.
1063
1064 If this insn was made by loop, we don't know its
1065 INSN_LUID and hence must make a conservative
0f41302f 1066 assumption. */
b4ad7b23 1067 m->global = (INSN_UID (p) >= max_uid_for_loop
b1f21e0a 1068 || (uid_luid[REGNO_LAST_UID (regno)]
b4ad7b23 1069 > INSN_LUID (end))
b1f21e0a 1070 || (uid_luid[REGNO_FIRST_UID (regno)]
b4ad7b23
RS
1071 < INSN_LUID (p))
1072 || (labels_in_range_p
b1f21e0a 1073 (p, uid_luid[REGNO_FIRST_UID (regno)])));
b4ad7b23
RS
1074 if (maybe_never && m->global)
1075 m->savemode = GET_MODE (SET_SRC (set1));
1076 else
1077 m->savemode = VOIDmode;
1078 m->regno = regno;
1079 m->cond = 0;
1080 m->match = 0;
b1f21e0a
MM
1081 m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
1082 - uid_luid[REGNO_FIRST_UID (regno)]);
b4ad7b23 1083 m->savings = 1;
4b259e3f 1084 VARRAY_INT (set_in_loop, regno) = -1;
b4ad7b23
RS
1085 /* Add M to the end of the chain MOVABLES. */
1086 if (movables == 0)
1087 movables = m;
1088 else
1089 last_movable->next = m;
1090 last_movable = m;
1091 }
1092 }
1093 }
1094 /* Past a call insn, we get to insns which might not be executed
1095 because the call might exit. This matters for insns that trap.
1096 Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1097 so they don't count. */
1098 else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1099 call_passed = 1;
1100 /* Past a label or a jump, we get to insns for which we
1101 can't count on whether or how many times they will be
1102 executed during each iteration. Therefore, we can
1103 only move out sets of trivial variables
1104 (those not used after the loop). */
8516af93 1105 /* Similar code appears twice in strength_reduce. */
b4ad7b23
RS
1106 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1107 /* If we enter the loop in the middle, and scan around to the
1108 beginning, don't set maybe_never for that. This must be an
1109 unconditional jump, otherwise the code at the top of the
1110 loop might never be executed. Unconditional jumps are
1111 followed a by barrier then loop end. */
1112 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1113 && NEXT_INSN (NEXT_INSN (p)) == end
1114 && simplejump_p (p)))
1115 maybe_never = 1;
5ea7a4ae
JW
1116 else if (GET_CODE (p) == NOTE)
1117 {
1118 /* At the virtual top of a converted loop, insns are again known to
1119 be executed: logically, the loop begins here even though the exit
1120 code has been duplicated. */
1121 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1122 maybe_never = call_passed = 0;
1123 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1124 loop_depth++;
1125 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1126 loop_depth--;
1127 }
b4ad7b23
RS
1128 }
1129
1130 /* If one movable subsumes another, ignore that other. */
1131
1132 ignore_some_movables (movables);
1133
1134 /* For each movable insn, see if the reg that it loads
1135 leads when it dies right into another conditionally movable insn.
1136 If so, record that the second insn "forces" the first one,
1137 since the second can be moved only if the first is. */
1138
1139 force_movables (movables);
1140
1141 /* See if there are multiple movable insns that load the same value.
1142 If there are, make all but the first point at the first one
1143 through the `match' field, and add the priorities of them
1144 all together as the priority of the first. */
1145
1146 combine_movables (movables, nregs);
1147
1148 /* Now consider each movable insn to decide whether it is worth moving.
4b259e3f 1149 Store 0 in set_in_loop for each reg that is moved.
b4ad7b23 1150
9dd07f87
R
1151 Generally this increases code size, so do not move moveables when
1152 optimizing for code size. */
1153
1154 if (! optimize_size)
1155 move_movables (movables, threshold,
1156 insn_count, loop_start, end, nregs);
b4ad7b23
RS
1157
1158 /* Now candidates that still are negative are those not moved.
4b259e3f 1159 Change set_in_loop to indicate that those are not actually invariant. */
b4ad7b23 1160 for (i = 0; i < nregs; i++)
4b259e3f
R
1161 if (VARRAY_INT (set_in_loop, i) < 0)
1162 VARRAY_INT (set_in_loop, i) = VARRAY_INT (n_times_set, i);
b4ad7b23 1163
3ec2b590 1164 /* Now that we've moved some things out of the loop, we might be able to
d6b44532 1165 hoist even more memory references. */
41a972a9 1166 load_mems_and_recount_loop_regs_set (scan_start, end, loop_top,
d6b44532 1167 loop_start, &insn_count);
4b259e3f 1168
b4ad7b23 1169 if (flag_strength_reduce)
45f97e2e
RH
1170 {
1171 the_movables = movables;
1172 strength_reduce (scan_start, end, loop_top,
6dd49eb4 1173 insn_count, loop_start, end, loop_cont, unroll_p, bct_p);
45f97e2e 1174 }
8deb8e2c 1175
d6b44532 1176 VARRAY_FREE (reg_single_usage);
4b259e3f 1177 VARRAY_FREE (set_in_loop);
8deb8e2c 1178 VARRAY_FREE (n_times_set);
8deb8e2c 1179 VARRAY_FREE (may_not_optimize);
b4ad7b23
RS
1180}
1181\f
1182/* Add elements to *OUTPUT to record all the pseudo-regs
1183 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1184
1185void
1186record_excess_regs (in_this, not_in_this, output)
1187 rtx in_this, not_in_this;
1188 rtx *output;
1189{
1190 enum rtx_code code;
1191 char *fmt;
1192 int i;
1193
1194 code = GET_CODE (in_this);
1195
1196 switch (code)
1197 {
1198 case PC:
1199 case CC0:
1200 case CONST_INT:
1201 case CONST_DOUBLE:
1202 case CONST:
1203 case SYMBOL_REF:
1204 case LABEL_REF:
1205 return;
1206
1207 case REG:
1208 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1209 && ! reg_mentioned_p (in_this, not_in_this))
38a448ca 1210 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
b4ad7b23 1211 return;
e9a25f70
JL
1212
1213 default:
1214 break;
b4ad7b23
RS
1215 }
1216
1217 fmt = GET_RTX_FORMAT (code);
1218 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1219 {
1220 int j;
1221
1222 switch (fmt[i])
1223 {
1224 case 'E':
1225 for (j = 0; j < XVECLEN (in_this, i); j++)
1226 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1227 break;
1228
1229 case 'e':
1230 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1231 break;
1232 }
1233 }
1234}
1235\f
1236/* Check what regs are referred to in the libcall block ending with INSN,
1237 aside from those mentioned in the equivalent value.
1238 If there are none, return 0.
1239 If there are one or more, return an EXPR_LIST containing all of them. */
1240
89d3d442 1241rtx
b4ad7b23
RS
1242libcall_other_reg (insn, equiv)
1243 rtx insn, equiv;
1244{
5fd8383e 1245 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
b4ad7b23
RS
1246 rtx p = XEXP (note, 0);
1247 rtx output = 0;
1248
1249 /* First, find all the regs used in the libcall block
1250 that are not mentioned as inputs to the result. */
1251
1252 while (p != insn)
1253 {
1254 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1255 || GET_CODE (p) == CALL_INSN)
1256 record_excess_regs (PATTERN (p), equiv, &output);
1257 p = NEXT_INSN (p);
1258 }
1259
1260 return output;
1261}
1262\f
1263/* Return 1 if all uses of REG
1264 are between INSN and the end of the basic block. */
1265
1266static int
1267reg_in_basic_block_p (insn, reg)
1268 rtx insn, reg;
1269{
1270 int regno = REGNO (reg);
1271 rtx p;
1272
b1f21e0a 1273 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
b4ad7b23
RS
1274 return 0;
1275
1276 /* Search this basic block for the already recorded last use of the reg. */
1277 for (p = insn; p; p = NEXT_INSN (p))
1278 {
1279 switch (GET_CODE (p))
1280 {
1281 case NOTE:
1282 break;
1283
1284 case INSN:
1285 case CALL_INSN:
1286 /* Ordinary insn: if this is the last use, we win. */
b1f21e0a 1287 if (REGNO_LAST_UID (regno) == INSN_UID (p))
b4ad7b23
RS
1288 return 1;
1289 break;
1290
1291 case JUMP_INSN:
1292 /* Jump insn: if this is the last use, we win. */
b1f21e0a 1293 if (REGNO_LAST_UID (regno) == INSN_UID (p))
b4ad7b23
RS
1294 return 1;
1295 /* Otherwise, it's the end of the basic block, so we lose. */
1296 return 0;
1297
1298 case CODE_LABEL:
1299 case BARRIER:
1300 /* It's the end of the basic block, so we lose. */
1301 return 0;
e9a25f70
JL
1302
1303 default:
1304 break;
b4ad7b23
RS
1305 }
1306 }
1307
1308 /* The "last use" doesn't follow the "first use"?? */
1309 abort ();
1310}
1311\f
1312/* Compute the benefit of eliminating the insns in the block whose
1313 last insn is LAST. This may be a group of insns used to compute a
1314 value directly or can contain a library call. */
1315
1316static int
1317libcall_benefit (last)
1318 rtx last;
1319{
1320 rtx insn;
1321 int benefit = 0;
1322
5fd8383e 1323 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
b4ad7b23
RS
1324 insn != last; insn = NEXT_INSN (insn))
1325 {
1326 if (GET_CODE (insn) == CALL_INSN)
1327 benefit += 10; /* Assume at least this many insns in a library
0f41302f 1328 routine. */
b4ad7b23
RS
1329 else if (GET_CODE (insn) == INSN
1330 && GET_CODE (PATTERN (insn)) != USE
1331 && GET_CODE (PATTERN (insn)) != CLOBBER)
1332 benefit++;
1333 }
1334
1335 return benefit;
1336}
1337\f
1338/* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1339
1340static rtx
1341skip_consec_insns (insn, count)
1342 rtx insn;
1343 int count;
1344{
1345 for (; count > 0; count--)
1346 {
1347 rtx temp;
1348
1349 /* If first insn of libcall sequence, skip to end. */
1350 /* Do this at start of loop, since INSN is guaranteed to
1351 be an insn here. */
1352 if (GET_CODE (insn) != NOTE
5fd8383e 1353 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1354 insn = XEXP (temp, 0);
1355
1356 do insn = NEXT_INSN (insn);
1357 while (GET_CODE (insn) == NOTE);
1358 }
1359
1360 return insn;
1361}
1362
1363/* Ignore any movable whose insn falls within a libcall
1364 which is part of another movable.
1365 We make use of the fact that the movable for the libcall value
1366 was made later and so appears later on the chain. */
1367
1368static void
1369ignore_some_movables (movables)
1370 struct movable *movables;
1371{
1372 register struct movable *m, *m1;
1373
1374 for (m = movables; m; m = m->next)
1375 {
1376 /* Is this a movable for the value of a libcall? */
5fd8383e 1377 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
b4ad7b23
RS
1378 if (note)
1379 {
1380 rtx insn;
1381 /* Check for earlier movables inside that range,
1382 and mark them invalid. We cannot use LUIDs here because
1383 insns created by loop.c for prior loops don't have LUIDs.
1384 Rather than reject all such insns from movables, we just
1385 explicitly check each insn in the libcall (since invariant
1386 libcalls aren't that common). */
1387 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1388 for (m1 = movables; m1 != m; m1 = m1->next)
1389 if (m1->insn == insn)
1390 m1->done = 1;
1391 }
1392 }
1393}
1394
1395/* For each movable insn, see if the reg that it loads
1396 leads when it dies right into another conditionally movable insn.
1397 If so, record that the second insn "forces" the first one,
1398 since the second can be moved only if the first is. */
1399
1400static void
1401force_movables (movables)
1402 struct movable *movables;
1403{
1404 register struct movable *m, *m1;
1405 for (m1 = movables; m1; m1 = m1->next)
1406 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1407 if (!m1->partial && !m1->done)
1408 {
1409 int regno = m1->regno;
1410 for (m = m1->next; m; m = m->next)
1411 /* ??? Could this be a bug? What if CSE caused the
1412 register of M1 to be used after this insn?
1413 Since CSE does not update regno_last_uid,
1414 this insn M->insn might not be where it dies.
1415 But very likely this doesn't matter; what matters is
1416 that M's reg is computed from M1's reg. */
b1f21e0a 1417 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
b4ad7b23
RS
1418 && !m->done)
1419 break;
1420 if (m != 0 && m->set_src == m1->set_dest
1421 /* If m->consec, m->set_src isn't valid. */
1422 && m->consec == 0)
1423 m = 0;
1424
1425 /* Increase the priority of the moving the first insn
1426 since it permits the second to be moved as well. */
1427 if (m != 0)
1428 {
1429 m->forces = m1;
1430 m1->lifetime += m->lifetime;
3875b31d 1431 m1->savings += m->savings;
b4ad7b23
RS
1432 }
1433 }
1434}
1435\f
1436/* Find invariant expressions that are equal and can be combined into
1437 one register. */
1438
1439static void
1440combine_movables (movables, nregs)
1441 struct movable *movables;
1442 int nregs;
1443{
1444 register struct movable *m;
1445 char *matched_regs = (char *) alloca (nregs);
1446 enum machine_mode mode;
1447
1448 /* Regs that are set more than once are not allowed to match
1449 or be matched. I'm no longer sure why not. */
1450 /* Perhaps testing m->consec_sets would be more appropriate here? */
1451
1452 for (m = movables; m; m = m->next)
4b259e3f 1453 if (m->match == 0 && VARRAY_INT (n_times_set, m->regno) == 1 && !m->partial)
b4ad7b23
RS
1454 {
1455 register struct movable *m1;
1456 int regno = m->regno;
b4ad7b23
RS
1457
1458 bzero (matched_regs, nregs);
1459 matched_regs[regno] = 1;
1460
88016fb7
DE
1461 /* We want later insns to match the first one. Don't make the first
1462 one match any later ones. So start this loop at m->next. */
1463 for (m1 = m->next; m1; m1 = m1->next)
4b259e3f 1464 if (m != m1 && m1->match == 0 && VARRAY_INT (n_times_set, m1->regno) == 1
b4ad7b23
RS
1465 /* A reg used outside the loop mustn't be eliminated. */
1466 && !m1->global
1467 /* A reg used for zero-extending mustn't be eliminated. */
1468 && !m1->partial
1469 && (matched_regs[m1->regno]
1470 ||
1471 (
1472 /* Can combine regs with different modes loaded from the
1473 same constant only if the modes are the same or
1474 if both are integer modes with M wider or the same
1475 width as M1. The check for integer is redundant, but
1476 safe, since the only case of differing destination
1477 modes with equal sources is when both sources are
1478 VOIDmode, i.e., CONST_INT. */
1479 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1480 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1481 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1482 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1483 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1484 /* See if the source of M1 says it matches M. */
1485 && ((GET_CODE (m1->set_src) == REG
1486 && matched_regs[REGNO (m1->set_src)])
1487 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1488 movables))))
1489 && ((m->dependencies == m1->dependencies)
1490 || rtx_equal_p (m->dependencies, m1->dependencies)))
1491 {
1492 m->lifetime += m1->lifetime;
1493 m->savings += m1->savings;
1494 m1->done = 1;
1495 m1->match = m;
1496 matched_regs[m1->regno] = 1;
1497 }
1498 }
1499
1500 /* Now combine the regs used for zero-extension.
1501 This can be done for those not marked `global'
1502 provided their lives don't overlap. */
1503
1504 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1505 mode = GET_MODE_WIDER_MODE (mode))
1506 {
1507 register struct movable *m0 = 0;
1508
1509 /* Combine all the registers for extension from mode MODE.
1510 Don't combine any that are used outside this loop. */
1511 for (m = movables; m; m = m->next)
1512 if (m->partial && ! m->global
1513 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1514 {
1515 register struct movable *m1;
b1f21e0a
MM
1516 int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1517 int last = uid_luid[REGNO_LAST_UID (m->regno)];
b4ad7b23
RS
1518
1519 if (m0 == 0)
1520 {
1521 /* First one: don't check for overlap, just record it. */
1522 m0 = m;
1523 continue;
1524 }
1525
1526 /* Make sure they extend to the same mode.
1527 (Almost always true.) */
1528 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1529 continue;
1530
1531 /* We already have one: check for overlap with those
1532 already combined together. */
1533 for (m1 = movables; m1 != m; m1 = m1->next)
1534 if (m1 == m0 || (m1->partial && m1->match == m0))
b1f21e0a
MM
1535 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1536 || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
b4ad7b23
RS
1537 goto overlap;
1538
1539 /* No overlap: we can combine this with the others. */
1540 m0->lifetime += m->lifetime;
1541 m0->savings += m->savings;
1542 m->done = 1;
1543 m->match = m0;
1544
1545 overlap: ;
1546 }
1547 }
1548}
1549\f
1550/* Return 1 if regs X and Y will become the same if moved. */
1551
1552static int
1553regs_match_p (x, y, movables)
1554 rtx x, y;
1555 struct movable *movables;
1556{
1557 int xn = REGNO (x);
1558 int yn = REGNO (y);
1559 struct movable *mx, *my;
1560
1561 for (mx = movables; mx; mx = mx->next)
1562 if (mx->regno == xn)
1563 break;
1564
1565 for (my = movables; my; my = my->next)
1566 if (my->regno == yn)
1567 break;
1568
1569 return (mx && my
1570 && ((mx->match == my->match && mx->match != 0)
1571 || mx->match == my
1572 || mx == my->match));
1573}
1574
1575/* Return 1 if X and Y are identical-looking rtx's.
1576 This is the Lisp function EQUAL for rtx arguments.
1577
1578 If two registers are matching movables or a movable register and an
1579 equivalent constant, consider them equal. */
1580
1581static int
1582rtx_equal_for_loop_p (x, y, movables)
1583 rtx x, y;
1584 struct movable *movables;
1585{
1586 register int i;
1587 register int j;
1588 register struct movable *m;
1589 register enum rtx_code code;
1590 register char *fmt;
1591
1592 if (x == y)
1593 return 1;
1594 if (x == 0 || y == 0)
1595 return 0;
1596
1597 code = GET_CODE (x);
1598
1599 /* If we have a register and a constant, they may sometimes be
1600 equal. */
4b259e3f 1601 if (GET_CODE (x) == REG && VARRAY_INT (set_in_loop, REGNO (x)) == -2
b4ad7b23 1602 && CONSTANT_P (y))
b1a0c816
JL
1603 {
1604 for (m = movables; m; m = m->next)
1605 if (m->move_insn && m->regno == REGNO (x)
1606 && rtx_equal_p (m->set_src, y))
1607 return 1;
1608 }
4b259e3f 1609 else if (GET_CODE (y) == REG && VARRAY_INT (set_in_loop, REGNO (y)) == -2
b4ad7b23 1610 && CONSTANT_P (x))
b1a0c816
JL
1611 {
1612 for (m = movables; m; m = m->next)
1613 if (m->move_insn && m->regno == REGNO (y)
1614 && rtx_equal_p (m->set_src, x))
1615 return 1;
1616 }
b4ad7b23
RS
1617
1618 /* Otherwise, rtx's of different codes cannot be equal. */
1619 if (code != GET_CODE (y))
1620 return 0;
1621
1622 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1623 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1624
1625 if (GET_MODE (x) != GET_MODE (y))
1626 return 0;
1627
1628 /* These three types of rtx's can be compared nonrecursively. */
1629 if (code == REG)
1630 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1631
1632 if (code == LABEL_REF)
1633 return XEXP (x, 0) == XEXP (y, 0);
1634 if (code == SYMBOL_REF)
1635 return XSTR (x, 0) == XSTR (y, 0);
1636
1637 /* Compare the elements. If any pair of corresponding elements
1638 fail to match, return 0 for the whole things. */
1639
1640 fmt = GET_RTX_FORMAT (code);
1641 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1642 {
1643 switch (fmt[i])
1644 {
5fd8383e
RK
1645 case 'w':
1646 if (XWINT (x, i) != XWINT (y, i))
1647 return 0;
1648 break;
1649
b4ad7b23
RS
1650 case 'i':
1651 if (XINT (x, i) != XINT (y, i))
1652 return 0;
1653 break;
1654
1655 case 'E':
1656 /* Two vectors must have the same length. */
1657 if (XVECLEN (x, i) != XVECLEN (y, i))
1658 return 0;
1659
1660 /* And the corresponding elements must match. */
1661 for (j = 0; j < XVECLEN (x, i); j++)
1662 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1663 return 0;
1664 break;
1665
1666 case 'e':
1667 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1668 return 0;
1669 break;
1670
1671 case 's':
1672 if (strcmp (XSTR (x, i), XSTR (y, i)))
1673 return 0;
1674 break;
1675
1676 case 'u':
1677 /* These are just backpointers, so they don't matter. */
1678 break;
1679
1680 case '0':
1681 break;
1682
1683 /* It is believed that rtx's at this level will never
1684 contain anything but integers and other rtx's,
1685 except for within LABEL_REFs and SYMBOL_REFs. */
1686 default:
1687 abort ();
1688 }
1689 }
1690 return 1;
1691}
1692\f
c160c628
RK
1693/* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1694 insns in INSNS which use thet reference. */
1695
1696static void
1697add_label_notes (x, insns)
1698 rtx x;
1699 rtx insns;
1700{
1701 enum rtx_code code = GET_CODE (x);
7dcd3836 1702 int i, j;
c160c628
RK
1703 char *fmt;
1704 rtx insn;
1705
82d00367 1706 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
c160c628 1707 {
6b3603c2
JL
1708 /* This code used to ignore labels that referred to dispatch tables to
1709 avoid flow generating (slighly) worse code.
1710
1711 We no longer ignore such label references (see LABEL_REF handling in
1712 mark_jump_label for additional information). */
1713 for (insn = insns; insn; insn = NEXT_INSN (insn))
1714 if (reg_mentioned_p (XEXP (x, 0), insn))
1715 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
1716 REG_NOTES (insn));
c160c628
RK
1717 }
1718
1719 fmt = GET_RTX_FORMAT (code);
1720 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7dcd3836
RK
1721 {
1722 if (fmt[i] == 'e')
1723 add_label_notes (XEXP (x, i), insns);
1724 else if (fmt[i] == 'E')
1725 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1726 add_label_notes (XVECEXP (x, i, j), insns);
1727 }
c160c628
RK
1728}
1729\f
b4ad7b23
RS
1730/* Scan MOVABLES, and move the insns that deserve to be moved.
1731 If two matching movables are combined, replace one reg with the
1732 other throughout. */
1733
1734static void
1735move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1736 struct movable *movables;
1737 int threshold;
1738 int insn_count;
1739 rtx loop_start;
1740 rtx end;
1741 int nregs;
1742{
1743 rtx new_start = 0;
1744 register struct movable *m;
1745 register rtx p;
1746 /* Map of pseudo-register replacements to handle combining
1747 when we move several insns that load the same value
1748 into different pseudo-registers. */
1749 rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1750 char *already_moved = (char *) alloca (nregs);
1751
1752 bzero (already_moved, nregs);
4c9a05bc 1753 bzero ((char *) reg_map, nregs * sizeof (rtx));
b4ad7b23
RS
1754
1755 num_movables = 0;
1756
1757 for (m = movables; m; m = m->next)
1758 {
1759 /* Describe this movable insn. */
1760
1761 if (loop_dump_stream)
1762 {
1763 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1764 INSN_UID (m->insn), m->regno, m->lifetime);
1765 if (m->consec > 0)
1766 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1767 if (m->cond)
1768 fprintf (loop_dump_stream, "cond ");
1769 if (m->force)
1770 fprintf (loop_dump_stream, "force ");
1771 if (m->global)
1772 fprintf (loop_dump_stream, "global ");
1773 if (m->done)
1774 fprintf (loop_dump_stream, "done ");
1775 if (m->move_insn)
1776 fprintf (loop_dump_stream, "move-insn ");
1777 if (m->match)
1778 fprintf (loop_dump_stream, "matches %d ",
1779 INSN_UID (m->match->insn));
1780 if (m->forces)
1781 fprintf (loop_dump_stream, "forces %d ",
1782 INSN_UID (m->forces->insn));
1783 }
1784
1785 /* Count movables. Value used in heuristics in strength_reduce. */
1786 num_movables++;
1787
1788 /* Ignore the insn if it's already done (it matched something else).
1789 Otherwise, see if it is now safe to move. */
1790
1791 if (!m->done
1792 && (! m->cond
1793 || (1 == invariant_p (m->set_src)
1794 && (m->dependencies == 0
1795 || 1 == invariant_p (m->dependencies))
1796 && (m->consec == 0
1797 || 1 == consec_sets_invariant_p (m->set_dest,
1798 m->consec + 1,
1799 m->insn))))
1800 && (! m->forces || m->forces->done))
1801 {
1802 register int regno;
1803 register rtx p;
1804 int savings = m->savings;
1805
1806 /* We have an insn that is safe to move.
1807 Compute its desirability. */
1808
1809 p = m->insn;
1810 regno = m->regno;
1811
1812 if (loop_dump_stream)
1813 fprintf (loop_dump_stream, "savings %d ", savings);
1814
877ca132
HB
1815 if (moved_once[regno] && loop_dump_stream)
1816 fprintf (loop_dump_stream, "halved since already moved ");
b4ad7b23
RS
1817
1818 /* An insn MUST be moved if we already moved something else
1819 which is safe only if this one is moved too: that is,
1820 if already_moved[REGNO] is nonzero. */
1821
1822 /* An insn is desirable to move if the new lifetime of the
1823 register is no more than THRESHOLD times the old lifetime.
1824 If it's not desirable, it means the loop is so big
1825 that moving won't speed things up much,
1826 and it is liable to make register usage worse. */
1827
1828 /* It is also desirable to move if it can be moved at no
1829 extra cost because something else was already moved. */
1830
1831 if (already_moved[regno]
e5eb27e5 1832 || flag_move_all_movables
877ca132
HB
1833 || (threshold * savings * m->lifetime) >=
1834 (moved_once[regno] ? insn_count * 2 : insn_count)
b4ad7b23 1835 || (m->forces && m->forces->done
4b259e3f 1836 && VARRAY_INT (n_times_set, m->forces->regno) == 1))
b4ad7b23
RS
1837 {
1838 int count;
1839 register struct movable *m1;
1840 rtx first;
1841
1842 /* Now move the insns that set the reg. */
1843
1844 if (m->partial && m->match)
1845 {
1846 rtx newpat, i1;
1847 rtx r1, r2;
1848 /* Find the end of this chain of matching regs.
1849 Thus, we load each reg in the chain from that one reg.
1850 And that reg is loaded with 0 directly,
1851 since it has ->match == 0. */
1852 for (m1 = m; m1->match; m1 = m1->match);
1853 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1854 SET_DEST (PATTERN (m1->insn)));
1855 i1 = emit_insn_before (newpat, loop_start);
1856
1857 /* Mark the moved, invariant reg as being allowed to
1858 share a hard reg with the other matching invariant. */
1859 REG_NOTES (i1) = REG_NOTES (m->insn);
1860 r1 = SET_DEST (PATTERN (m->insn));
1861 r2 = SET_DEST (PATTERN (m1->insn));
38a448ca
RH
1862 regs_may_share
1863 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1864 gen_rtx_EXPR_LIST (VOIDmode, r2,
1865 regs_may_share));
b4ad7b23
RS
1866 delete_insn (m->insn);
1867
1868 if (new_start == 0)
1869 new_start = i1;
1870
1871 if (loop_dump_stream)
1872 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1873 }
1874 /* If we are to re-generate the item being moved with a
1875 new move insn, first delete what we have and then emit
1876 the move insn before the loop. */
1877 else if (m->move_insn)
1878 {
1879 rtx i1, temp;
1880
1881 for (count = m->consec; count >= 0; count--)
1882 {
1883 /* If this is the first insn of a library call sequence,
1884 skip to the end. */
1885 if (GET_CODE (p) != NOTE
5fd8383e 1886 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1887 p = XEXP (temp, 0);
1888
1889 /* If this is the last insn of a libcall sequence, then
1890 delete every insn in the sequence except the last.
1891 The last insn is handled in the normal manner. */
1892 if (GET_CODE (p) != NOTE
5fd8383e 1893 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
b4ad7b23
RS
1894 {
1895 temp = XEXP (temp, 0);
1896 while (temp != p)
1897 temp = delete_insn (temp);
1898 }
1899
9655bf95 1900 temp = p;
b4ad7b23 1901 p = delete_insn (p);
9655bf95
DM
1902
1903 /* simplify_giv_expr expects that it can walk the insns
1904 at m->insn forwards and see this old sequence we are
1905 tossing here. delete_insn does preserve the next
1906 pointers, but when we skip over a NOTE we must fix
1907 it up. Otherwise that code walks into the non-deleted
1908 insn stream. */
dd202606 1909 while (p && GET_CODE (p) == NOTE)
9655bf95 1910 p = NEXT_INSN (temp) = NEXT_INSN (p);
b4ad7b23
RS
1911 }
1912
1913 start_sequence ();
1914 emit_move_insn (m->set_dest, m->set_src);
c160c628 1915 temp = get_insns ();
b4ad7b23
RS
1916 end_sequence ();
1917
c160c628
RK
1918 add_label_notes (m->set_src, temp);
1919
1920 i1 = emit_insns_before (temp, loop_start);
5fd8383e 1921 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
b4ad7b23 1922 REG_NOTES (i1)
38a448ca
RH
1923 = gen_rtx_EXPR_LIST (m->is_equiv ? REG_EQUIV : REG_EQUAL,
1924 m->set_src, REG_NOTES (i1));
b4ad7b23
RS
1925
1926 if (loop_dump_stream)
1927 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1928
1929 /* The more regs we move, the less we like moving them. */
1930 threshold -= 3;
1931 }
1932 else
1933 {
1934 for (count = m->consec; count >= 0; count--)
1935 {
1936 rtx i1, temp;
1937
0f41302f 1938 /* If first insn of libcall sequence, skip to end. */
b4ad7b23
RS
1939 /* Do this at start of loop, since p is guaranteed to
1940 be an insn here. */
1941 if (GET_CODE (p) != NOTE
5fd8383e 1942 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
1943 p = XEXP (temp, 0);
1944
1945 /* If last insn of libcall sequence, move all
1946 insns except the last before the loop. The last
1947 insn is handled in the normal manner. */
1948 if (GET_CODE (p) != NOTE
5fd8383e 1949 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
b4ad7b23
RS
1950 {
1951 rtx fn_address = 0;
1952 rtx fn_reg = 0;
1953 rtx fn_address_insn = 0;
1954
1955 first = 0;
1956 for (temp = XEXP (temp, 0); temp != p;
1957 temp = NEXT_INSN (temp))
1958 {
1959 rtx body;
1960 rtx n;
1961 rtx next;
1962
1963 if (GET_CODE (temp) == NOTE)
1964 continue;
1965
1966 body = PATTERN (temp);
1967
1968 /* Find the next insn after TEMP,
1969 not counting USE or NOTE insns. */
1970 for (next = NEXT_INSN (temp); next != p;
1971 next = NEXT_INSN (next))
1972 if (! (GET_CODE (next) == INSN
1973 && GET_CODE (PATTERN (next)) == USE)
1974 && GET_CODE (next) != NOTE)
1975 break;
1976
1977 /* If that is the call, this may be the insn
1978 that loads the function address.
1979
1980 Extract the function address from the insn
1981 that loads it into a register.
1982 If this insn was cse'd, we get incorrect code.
1983
1984 So emit a new move insn that copies the
1985 function address into the register that the
1986 call insn will use. flow.c will delete any
1987 redundant stores that we have created. */
1988 if (GET_CODE (next) == CALL_INSN
1989 && GET_CODE (body) == SET
1990 && GET_CODE (SET_DEST (body)) == REG
5fd8383e
RK
1991 && (n = find_reg_note (temp, REG_EQUAL,
1992 NULL_RTX)))
b4ad7b23
RS
1993 {
1994 fn_reg = SET_SRC (body);
1995 if (GET_CODE (fn_reg) != REG)
1996 fn_reg = SET_DEST (body);
1997 fn_address = XEXP (n, 0);
1998 fn_address_insn = temp;
1999 }
2000 /* We have the call insn.
2001 If it uses the register we suspect it might,
2002 load it with the correct address directly. */
2003 if (GET_CODE (temp) == CALL_INSN
2004 && fn_address != 0
d9f8a199 2005 && reg_referenced_p (fn_reg, body))
b4ad7b23
RS
2006 emit_insn_after (gen_move_insn (fn_reg,
2007 fn_address),
2008 fn_address_insn);
2009
2010 if (GET_CODE (temp) == CALL_INSN)
f97d29ce
JW
2011 {
2012 i1 = emit_call_insn_before (body, loop_start);
2013 /* Because the USAGE information potentially
2014 contains objects other than hard registers
2015 we need to copy it. */
8c4f5c09 2016 if (CALL_INSN_FUNCTION_USAGE (temp))
db3cf6fb
MS
2017 CALL_INSN_FUNCTION_USAGE (i1)
2018 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
f97d29ce 2019 }
b4ad7b23
RS
2020 else
2021 i1 = emit_insn_before (body, loop_start);
2022 if (first == 0)
2023 first = i1;
2024 if (temp == fn_address_insn)
2025 fn_address_insn = i1;
2026 REG_NOTES (i1) = REG_NOTES (temp);
2027 delete_insn (temp);
2028 }
18985c91
R
2029 if (new_start == 0)
2030 new_start = first;
b4ad7b23
RS
2031 }
2032 if (m->savemode != VOIDmode)
2033 {
2034 /* P sets REG to zero; but we should clear only
2035 the bits that are not covered by the mode
2036 m->savemode. */
2037 rtx reg = m->set_dest;
2038 rtx sequence;
2039 rtx tem;
2040
2041 start_sequence ();
2042 tem = expand_binop
2043 (GET_MODE (reg), and_optab, reg,
5fd8383e
RK
2044 GEN_INT ((((HOST_WIDE_INT) 1
2045 << GET_MODE_BITSIZE (m->savemode)))
b4ad7b23
RS
2046 - 1),
2047 reg, 1, OPTAB_LIB_WIDEN);
2048 if (tem == 0)
2049 abort ();
2050 if (tem != reg)
2051 emit_move_insn (reg, tem);
2052 sequence = gen_sequence ();
2053 end_sequence ();
2054 i1 = emit_insn_before (sequence, loop_start);
2055 }
2056 else if (GET_CODE (p) == CALL_INSN)
f97d29ce
JW
2057 {
2058 i1 = emit_call_insn_before (PATTERN (p), loop_start);
2059 /* Because the USAGE information potentially
2060 contains objects other than hard registers
2061 we need to copy it. */
8c4f5c09 2062 if (CALL_INSN_FUNCTION_USAGE (p))
db3cf6fb
MS
2063 CALL_INSN_FUNCTION_USAGE (i1)
2064 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
f97d29ce 2065 }
1a61c29f
JW
2066 else if (count == m->consec && m->move_insn_first)
2067 {
2068 /* The SET_SRC might not be invariant, so we must
2069 use the REG_EQUAL note. */
2070 start_sequence ();
2071 emit_move_insn (m->set_dest, m->set_src);
2072 temp = get_insns ();
2073 end_sequence ();
2074
2075 add_label_notes (m->set_src, temp);
2076
2077 i1 = emit_insns_before (temp, loop_start);
2078 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2079 REG_NOTES (i1)
2080 = gen_rtx_EXPR_LIST ((m->is_equiv ? REG_EQUIV
2081 : REG_EQUAL),
2082 m->set_src, REG_NOTES (i1));
2083 }
b4ad7b23
RS
2084 else
2085 i1 = emit_insn_before (PATTERN (p), loop_start);
2086
1a61c29f
JW
2087 if (REG_NOTES (i1) == 0)
2088 {
2089 REG_NOTES (i1) = REG_NOTES (p);
b4ad7b23 2090
1a61c29f
JW
2091 /* If there is a REG_EQUAL note present whose value
2092 is not loop invariant, then delete it, since it
2093 may cause problems with later optimization passes.
2094 It is possible for cse to create such notes
2095 like this as a result of record_jump_cond. */
e6726b1f 2096
1a61c29f
JW
2097 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2098 && ! invariant_p (XEXP (temp, 0)))
2099 remove_note (i1, temp);
2100 }
e6726b1f 2101
b4ad7b23
RS
2102 if (new_start == 0)
2103 new_start = i1;
2104
2105 if (loop_dump_stream)
2106 fprintf (loop_dump_stream, " moved to %d",
2107 INSN_UID (i1));
2108
b4ad7b23
RS
2109 /* If library call, now fix the REG_NOTES that contain
2110 insn pointers, namely REG_LIBCALL on FIRST
2111 and REG_RETVAL on I1. */
51723711 2112 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
b4ad7b23
RS
2113 {
2114 XEXP (temp, 0) = first;
5fd8383e 2115 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
b4ad7b23
RS
2116 XEXP (temp, 0) = i1;
2117 }
2118
9655bf95 2119 temp = p;
b4ad7b23 2120 delete_insn (p);
9655bf95
DM
2121 p = NEXT_INSN (p);
2122
2123 /* simplify_giv_expr expects that it can walk the insns
2124 at m->insn forwards and see this old sequence we are
2125 tossing here. delete_insn does preserve the next
2126 pointers, but when we skip over a NOTE we must fix
2127 it up. Otherwise that code walks into the non-deleted
2128 insn stream. */
2129 while (p && GET_CODE (p) == NOTE)
2130 p = NEXT_INSN (temp) = NEXT_INSN (p);
b4ad7b23
RS
2131 }
2132
2133 /* The more regs we move, the less we like moving them. */
2134 threshold -= 3;
2135 }
2136
2137 /* Any other movable that loads the same register
2138 MUST be moved. */
2139 already_moved[regno] = 1;
2140
2141 /* This reg has been moved out of one loop. */
2142 moved_once[regno] = 1;
2143
2144 /* The reg set here is now invariant. */
2145 if (! m->partial)
4b259e3f 2146 VARRAY_INT (set_in_loop, regno) = 0;
b4ad7b23
RS
2147
2148 m->done = 1;
2149
2150 /* Change the length-of-life info for the register
2151 to say it lives at least the full length of this loop.
2152 This will help guide optimizations in outer loops. */
2153
b1f21e0a 2154 if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
b4ad7b23
RS
2155 /* This is the old insn before all the moved insns.
2156 We can't use the moved insn because it is out of range
2157 in uid_luid. Only the old insns have luids. */
b1f21e0a
MM
2158 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2159 if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2160 REGNO_LAST_UID (regno) = INSN_UID (end);
b4ad7b23
RS
2161
2162 /* Combine with this moved insn any other matching movables. */
2163
2164 if (! m->partial)
2165 for (m1 = movables; m1; m1 = m1->next)
2166 if (m1->match == m)
2167 {
2168 rtx temp;
2169
2170 /* Schedule the reg loaded by M1
2171 for replacement so that shares the reg of M.
2172 If the modes differ (only possible in restricted
51f0646f
JL
2173 circumstances, make a SUBREG.
2174
2175 Note this assumes that the target dependent files
2176 treat REG and SUBREG equally, including within
2177 GO_IF_LEGITIMATE_ADDRESS and in all the
2178 predicates since we never verify that replacing the
2179 original register with a SUBREG results in a
2180 recognizable insn. */
b4ad7b23
RS
2181 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2182 reg_map[m1->regno] = m->set_dest;
2183 else
2184 reg_map[m1->regno]
2185 = gen_lowpart_common (GET_MODE (m1->set_dest),
2186 m->set_dest);
2187
2188 /* Get rid of the matching insn
2189 and prevent further processing of it. */
2190 m1->done = 1;
2191
2192 /* if library call, delete all insn except last, which
2193 is deleted below */
51723711
KG
2194 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2195 NULL_RTX)))
b4ad7b23
RS
2196 {
2197 for (temp = XEXP (temp, 0); temp != m1->insn;
2198 temp = NEXT_INSN (temp))
2199 delete_insn (temp);
2200 }
2201 delete_insn (m1->insn);
2202
2203 /* Any other movable that loads the same register
2204 MUST be moved. */
2205 already_moved[m1->regno] = 1;
2206
2207 /* The reg merged here is now invariant,
2208 if the reg it matches is invariant. */
2209 if (! m->partial)
4b259e3f 2210 VARRAY_INT (set_in_loop, m1->regno) = 0;
b4ad7b23
RS
2211 }
2212 }
2213 else if (loop_dump_stream)
2214 fprintf (loop_dump_stream, "not desirable");
2215 }
2216 else if (loop_dump_stream && !m->match)
2217 fprintf (loop_dump_stream, "not safe");
2218
2219 if (loop_dump_stream)
2220 fprintf (loop_dump_stream, "\n");
2221 }
2222
2223 if (new_start == 0)
2224 new_start = loop_start;
2225
2226 /* Go through all the instructions in the loop, making
2227 all the register substitutions scheduled in REG_MAP. */
2228 for (p = new_start; p != end; p = NEXT_INSN (p))
2229 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2230 || GET_CODE (p) == CALL_INSN)
2231 {
2232 replace_regs (PATTERN (p), reg_map, nregs, 0);
2233 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
da0c128e 2234 INSN_CODE (p) = -1;
b4ad7b23
RS
2235 }
2236}
2237\f
2238#if 0
2239/* Scan X and replace the address of any MEM in it with ADDR.
2240 REG is the address that MEM should have before the replacement. */
2241
2242static void
2243replace_call_address (x, reg, addr)
2244 rtx x, reg, addr;
2245{
2246 register enum rtx_code code;
2247 register int i;
2248 register char *fmt;
2249
2250 if (x == 0)
2251 return;
2252 code = GET_CODE (x);
2253 switch (code)
2254 {
2255 case PC:
2256 case CC0:
2257 case CONST_INT:
2258 case CONST_DOUBLE:
2259 case CONST:
2260 case SYMBOL_REF:
2261 case LABEL_REF:
2262 case REG:
2263 return;
2264
2265 case SET:
2266 /* Short cut for very common case. */
2267 replace_call_address (XEXP (x, 1), reg, addr);
2268 return;
2269
2270 case CALL:
2271 /* Short cut for very common case. */
2272 replace_call_address (XEXP (x, 0), reg, addr);
2273 return;
2274
2275 case MEM:
2276 /* If this MEM uses a reg other than the one we expected,
2277 something is wrong. */
2278 if (XEXP (x, 0) != reg)
2279 abort ();
2280 XEXP (x, 0) = addr;
2281 return;
e9a25f70
JL
2282
2283 default:
2284 break;
b4ad7b23
RS
2285 }
2286
2287 fmt = GET_RTX_FORMAT (code);
2288 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2289 {
2290 if (fmt[i] == 'e')
2291 replace_call_address (XEXP (x, i), reg, addr);
2292 if (fmt[i] == 'E')
2293 {
2294 register int j;
2295 for (j = 0; j < XVECLEN (x, i); j++)
2296 replace_call_address (XVECEXP (x, i, j), reg, addr);
2297 }
2298 }
2299}
2300#endif
2301\f
2302/* Return the number of memory refs to addresses that vary
2303 in the rtx X. */
2304
2305static int
2306count_nonfixed_reads (x)
2307 rtx x;
2308{
2309 register enum rtx_code code;
2310 register int i;
2311 register char *fmt;
2312 int value;
2313
2314 if (x == 0)
2315 return 0;
2316
2317 code = GET_CODE (x);
2318 switch (code)
2319 {
2320 case PC:
2321 case CC0:
2322 case CONST_INT:
2323 case CONST_DOUBLE:
2324 case CONST:
2325 case SYMBOL_REF:
2326 case LABEL_REF:
2327 case REG:
2328 return 0;
2329
2330 case MEM:
2331 return ((invariant_p (XEXP (x, 0)) != 1)
2332 + count_nonfixed_reads (XEXP (x, 0)));
e9a25f70
JL
2333
2334 default:
2335 break;
b4ad7b23
RS
2336 }
2337
2338 value = 0;
2339 fmt = GET_RTX_FORMAT (code);
2340 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2341 {
2342 if (fmt[i] == 'e')
2343 value += count_nonfixed_reads (XEXP (x, i));
2344 if (fmt[i] == 'E')
2345 {
2346 register int j;
2347 for (j = 0; j < XVECLEN (x, i); j++)
2348 value += count_nonfixed_reads (XVECEXP (x, i, j));
2349 }
2350 }
2351 return value;
2352}
2353
2354\f
2355#if 0
2356/* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2357 Replace it with an instruction to load just the low bytes
2358 if the machine supports such an instruction,
2359 and insert above LOOP_START an instruction to clear the register. */
2360
2361static void
2362constant_high_bytes (p, loop_start)
2363 rtx p, loop_start;
2364{
2365 register rtx new;
2366 register int insn_code_number;
2367
2368 /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2369 to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...). */
2370
38a448ca
RH
2371 new = gen_rtx_SET (VOIDmode,
2372 gen_rtx_STRICT_LOW_PART (VOIDmode,
2373 gen_rtx_SUBREG (GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
b4ad7b23
RS
2374 SET_DEST (PATTERN (p)),
2375 0)),
2376 XEXP (SET_SRC (PATTERN (p)), 0));
2377 insn_code_number = recog (new, p);
2378
2379 if (insn_code_number)
2380 {
2381 register int i;
2382
2383 /* Clear destination register before the loop. */
38a448ca
RH
2384 emit_insn_before (gen_rtx_SET (VOIDmode, SET_DEST (PATTERN (p)),
2385 const0_rtx),
b4ad7b23
RS
2386 loop_start);
2387
2388 /* Inside the loop, just load the low part. */
2389 PATTERN (p) = new;
2390 }
2391}
2392#endif
2393\f
2394/* Scan a loop setting the variables `unknown_address_altered',
8c368ee2
DE
2395 `num_mem_sets', `loop_continue', `loops_enclosed', `loop_has_call',
2396 `loop_has_volatile', and `loop_has_tablejump'.
5026a502 2397 Also, fill in the array `loop_mems' and the list `loop_store_mems'. */
b4ad7b23
RS
2398
2399static void
2400prescan_loop (start, end)
2401 rtx start, end;
2402{
2403 register int level = 1;
41a972a9
MM
2404 rtx insn;
2405 int loop_has_multiple_exit_targets = 0;
2406 /* The label after END. Jumping here is just like falling off the
2407 end of the loop. We use next_nonnote_insn instead of next_label
2408 as a hedge against the (pathological) case where some actual insn
2409 might end up between the two. */
2410 rtx exit_target = next_nonnote_insn (end);
2411 if (exit_target == NULL_RTX || GET_CODE (exit_target) != CODE_LABEL)
2412 loop_has_multiple_exit_targets = 1;
b4ad7b23
RS
2413
2414 unknown_address_altered = 0;
2415 loop_has_call = 0;
552bc76f 2416 loop_has_volatile = 0;
8c368ee2 2417 loop_has_tablejump = 0;
5026a502 2418 loop_store_mems = NULL_RTX;
2d4fde68 2419 first_loop_store_insn = NULL_RTX;
41a972a9 2420 loop_mems_idx = 0;
b4ad7b23
RS
2421
2422 num_mem_sets = 0;
2423 loops_enclosed = 1;
2424 loop_continue = 0;
2425
2426 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2427 insn = NEXT_INSN (insn))
2428 {
2429 if (GET_CODE (insn) == NOTE)
2430 {
2431 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2432 {
2433 ++level;
2434 /* Count number of loops contained in this one. */
2435 loops_enclosed++;
2436 }
2437 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2438 {
2439 --level;
2440 if (level == 0)
2441 {
2442 end = insn;
2443 break;
2444 }
2445 }
2446 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2447 {
2448 if (level == 1)
2449 loop_continue = insn;
2450 }
2451 }
2452 else if (GET_CODE (insn) == CALL_INSN)
2453 {
9ae8ffe7
JL
2454 if (! CONST_CALL_P (insn))
2455 unknown_address_altered = 1;
b4ad7b23
RS
2456 loop_has_call = 1;
2457 }
41a972a9 2458 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
b4ad7b23 2459 {
41a972a9
MM
2460 rtx label1 = NULL_RTX;
2461 rtx label2 = NULL_RTX;
2462
2463 if (volatile_refs_p (PATTERN (insn)))
2464 loop_has_volatile = 1;
8c368ee2
DE
2465
2466 if (GET_CODE (insn) == JUMP_INSN
2467 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2468 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2469 loop_has_tablejump = 1;
41a972a9
MM
2470
2471 note_stores (PATTERN (insn), note_addr_stored);
2d4fde68
R
2472 if (! first_loop_store_insn && loop_store_mems)
2473 first_loop_store_insn = insn;
41a972a9 2474
8c368ee2 2475 if (! loop_has_multiple_exit_targets
41a972a9
MM
2476 && GET_CODE (insn) == JUMP_INSN
2477 && GET_CODE (PATTERN (insn)) == SET
2478 && SET_DEST (PATTERN (insn)) == pc_rtx)
552bc76f 2479 {
41a972a9
MM
2480 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2481 {
2482 label1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2483 label2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2484 }
2485 else
2486 {
2487 label1 = SET_SRC (PATTERN (insn));
2488 }
2489
2490 do {
2491 if (label1 && label1 != pc_rtx)
2492 {
2493 if (GET_CODE (label1) != LABEL_REF)
2494 {
2495 /* Something tricky. */
2496 loop_has_multiple_exit_targets = 1;
2497 break;
2498 }
2499 else if (XEXP (label1, 0) != exit_target
2500 && LABEL_OUTSIDE_LOOP_P (label1))
2501 {
2502 /* A jump outside the current loop. */
2503 loop_has_multiple_exit_targets = 1;
2504 break;
2505 }
2506 }
552bc76f 2507
41a972a9
MM
2508 label1 = label2;
2509 label2 = NULL_RTX;
2510 } while (label1);
552bc76f 2511 }
b4ad7b23 2512 }
41a972a9
MM
2513 else if (GET_CODE (insn) == RETURN)
2514 loop_has_multiple_exit_targets = 1;
b4ad7b23 2515 }
41a972a9
MM
2516
2517 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2518 if (/* We can't tell what MEMs are aliased by what. */
2519 !unknown_address_altered
2520 /* An exception thrown by a called function might land us
2521 anywhere. */
2522 && !loop_has_call
2523 /* We don't want loads for MEMs moved to a location before the
2524 one at which their stack memory becomes allocated. (Note
2525 that this is not a problem for malloc, etc., since those
2526 require actual function calls. */
2527 && !current_function_calls_alloca
2528 /* There are ways to leave the loop other than falling off the
2529 end. */
2530 && !loop_has_multiple_exit_targets)
2531 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2532 insn = NEXT_INSN (insn))
2533 for_each_rtx (&insn, insert_loop_mem, 0);
b4ad7b23
RS
2534}
2535\f
3ec2b590
R
2536/* LOOP_NUMBER_CONT_DOMINATOR is now the last label between the loop start
2537 and the continue note that is a the destination of a (cond)jump after
2538 the continue note. If there is any (cond)jump between the loop start
2539 and what we have so far as LOOP_NUMBER_CONT_DOMINATOR that has a
2540 target between LOOP_DOMINATOR and the continue note, move
2541 LOOP_NUMBER_CONT_DOMINATOR forward to that label; if a jump's
2542 destination cannot be determined, clear LOOP_NUMBER_CONT_DOMINATOR. */
2543
2544static void
2545verify_dominator (loop_number)
2546 int loop_number;
2547{
2548 rtx insn;
2549
2550 if (! loop_number_cont_dominator[loop_number])
2551 /* This can happen for an empty loop, e.g. in
2552 gcc.c-torture/compile/920410-2.c */
2553 return;
2554 if (loop_number_cont_dominator[loop_number] == const0_rtx)
2555 {
2556 loop_number_cont_dominator[loop_number] = 0;
2557 return;
2558 }
2559 for (insn = loop_number_loop_starts[loop_number];
2560 insn != loop_number_cont_dominator[loop_number];
2561 insn = NEXT_INSN (insn))
2562 {
2563 if (GET_CODE (insn) == JUMP_INSN
2564 && GET_CODE (PATTERN (insn)) != RETURN)
2565 {
2566 rtx label = JUMP_LABEL (insn);
2567 int label_luid = INSN_LUID (label);
2568
2569 if (! condjump_p (insn)
2570 && ! condjump_in_parallel_p (insn))
2571 {
2572 loop_number_cont_dominator[loop_number] = NULL_RTX;
2573 return;
2574 }
2575 if (label_luid < INSN_LUID (loop_number_loop_cont[loop_number])
2576 && (label_luid
2577 > INSN_LUID (loop_number_cont_dominator[loop_number])))
2578 loop_number_cont_dominator[loop_number] = label;
2579 }
2580 }
2581}
2582
b4ad7b23
RS
2583/* Scan the function looking for loops. Record the start and end of each loop.
2584 Also mark as invalid loops any loops that contain a setjmp or are branched
2585 to from outside the loop. */
2586
2587static void
2588find_and_verify_loops (f)
2589 rtx f;
2590{
034dabc9 2591 rtx insn, label;
b4ad7b23
RS
2592 int current_loop = -1;
2593 int next_loop = -1;
2594 int loop;
2595
3ec2b590
R
2596 compute_luids (f, NULL_RTX, 0);
2597
b4ad7b23
RS
2598 /* If there are jumps to undefined labels,
2599 treat them as jumps out of any/all loops.
2600 This also avoids writing past end of tables when there are no loops. */
2601 uid_loop_num[0] = -1;
2602
2603 /* Find boundaries of loops, mark which loops are contained within
2604 loops, and invalidate loops that have setjmp. */
2605
2606 for (insn = f; insn; insn = NEXT_INSN (insn))
2607 {
2608 if (GET_CODE (insn) == NOTE)
2609 switch (NOTE_LINE_NUMBER (insn))
2610 {
2611 case NOTE_INSN_LOOP_BEG:
2612 loop_number_loop_starts[++next_loop] = insn;
2613 loop_number_loop_ends[next_loop] = 0;
3ec2b590
R
2614 loop_number_loop_cont[next_loop] = 0;
2615 loop_number_cont_dominator[next_loop] = 0;
b4ad7b23
RS
2616 loop_outer_loop[next_loop] = current_loop;
2617 loop_invalid[next_loop] = 0;
2618 loop_number_exit_labels[next_loop] = 0;
353127c2 2619 loop_number_exit_count[next_loop] = 0;
b4ad7b23
RS
2620 current_loop = next_loop;
2621 break;
2622
2623 case NOTE_INSN_SETJMP:
2624 /* In this case, we must invalidate our current loop and any
2625 enclosing loop. */
2626 for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2627 {
2628 loop_invalid[loop] = 1;
2629 if (loop_dump_stream)
2630 fprintf (loop_dump_stream,
2631 "\nLoop at %d ignored due to setjmp.\n",
2632 INSN_UID (loop_number_loop_starts[loop]));
2633 }
2634 break;
2635
3ec2b590
R
2636 case NOTE_INSN_LOOP_CONT:
2637 loop_number_loop_cont[current_loop] = insn;
2638 break;
b4ad7b23
RS
2639 case NOTE_INSN_LOOP_END:
2640 if (current_loop == -1)
2641 abort ();
2642
2643 loop_number_loop_ends[current_loop] = insn;
3ec2b590 2644 verify_dominator (current_loop);
b4ad7b23
RS
2645 current_loop = loop_outer_loop[current_loop];
2646 break;
2647
e9a25f70
JL
2648 default:
2649 break;
b4ad7b23 2650 }
3ec2b590
R
2651 /* If for any loop, this is a jump insn between the NOTE_INSN_LOOP_CONT
2652 and NOTE_INSN_LOOP_END notes, update loop_number_loop_dominator. */
2653 else if (GET_CODE (insn) == JUMP_INSN
2654 && GET_CODE (PATTERN (insn)) != RETURN
2655 && current_loop >= 0)
2656 {
2657 int this_loop;
2658 rtx label = JUMP_LABEL (insn);
2659
2660 if (! condjump_p (insn) && ! condjump_in_parallel_p (insn))
2661 label = NULL_RTX;
2662
2663 this_loop = current_loop;
2664 do
2665 {
2666 /* First see if we care about this loop. */
2667 if (loop_number_loop_cont[this_loop]
2668 && loop_number_cont_dominator[this_loop] != const0_rtx)
2669 {
2670 /* If the jump destination is not known, invalidate
2671 loop_number_const_dominator. */
2672 if (! label)
2673 loop_number_cont_dominator[this_loop] = const0_rtx;
2674 else
2675 /* Check if the destination is between loop start and
2676 cont. */
2677 if ((INSN_LUID (label)
2678 < INSN_LUID (loop_number_loop_cont[this_loop]))
2679 && (INSN_LUID (label)
2680 > INSN_LUID (loop_number_loop_starts[this_loop]))
2681 /* And if there is no later destination already
2682 recorded. */
2683 && (! loop_number_cont_dominator[this_loop]
2684 || (INSN_LUID (label)
2685 > INSN_LUID (loop_number_cont_dominator
2686 [this_loop]))))
2687 loop_number_cont_dominator[this_loop] = label;
2688 }
2689 this_loop = loop_outer_loop[this_loop];
2690 }
2691 while (this_loop >= 0);
2692 }
b4ad7b23
RS
2693
2694 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2695 enclosing loop, but this doesn't matter. */
2696 uid_loop_num[INSN_UID (insn)] = current_loop;
2697 }
2698
034dabc9
JW
2699 /* Any loop containing a label used in an initializer must be invalidated,
2700 because it can be jumped into from anywhere. */
2701
2702 for (label = forced_labels; label; label = XEXP (label, 1))
2703 {
2704 int loop_num;
2705
2706 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2707 loop_num != -1;
2708 loop_num = loop_outer_loop[loop_num])
2709 loop_invalid[loop_num] = 1;
2710 }
2711
6adb4e3a
MS
2712 /* Any loop containing a label used for an exception handler must be
2713 invalidated, because it can be jumped into from anywhere. */
2714
2715 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2716 {
2717 int loop_num;
2718
2719 for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2720 loop_num != -1;
2721 loop_num = loop_outer_loop[loop_num])
2722 loop_invalid[loop_num] = 1;
2723 }
2724
034dabc9
JW
2725 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2726 loop that it is not contained within, that loop is marked invalid.
2727 If any INSN or CALL_INSN uses a label's address, then the loop containing
2728 that label is marked invalid, because it could be jumped into from
2729 anywhere.
b4ad7b23
RS
2730
2731 Also look for blocks of code ending in an unconditional branch that
2732 exits the loop. If such a block is surrounded by a conditional
2733 branch around the block, move the block elsewhere (see below) and
2734 invert the jump to point to the code block. This may eliminate a
2735 label in our loop and will simplify processing by both us and a
2736 possible second cse pass. */
2737
2738 for (insn = f; insn; insn = NEXT_INSN (insn))
034dabc9 2739 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
b4ad7b23
RS
2740 {
2741 int this_loop_num = uid_loop_num[INSN_UID (insn)];
2742
034dabc9
JW
2743 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2744 {
2745 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2746 if (note)
2747 {
2748 int loop_num;
2749
2750 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2751 loop_num != -1;
2752 loop_num = loop_outer_loop[loop_num])
2753 loop_invalid[loop_num] = 1;
2754 }
2755 }
2756
2757 if (GET_CODE (insn) != JUMP_INSN)
2758 continue;
2759
b4ad7b23
RS
2760 mark_loop_jump (PATTERN (insn), this_loop_num);
2761
2762 /* See if this is an unconditional branch outside the loop. */
2763 if (this_loop_num != -1
2764 && (GET_CODE (PATTERN (insn)) == RETURN
2765 || (simplejump_p (insn)
2766 && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
1c01e9df
TW
2767 != this_loop_num)))
2768 && get_max_uid () < max_uid_for_loop)
b4ad7b23
RS
2769 {
2770 rtx p;
2771 rtx our_next = next_real_insn (insn);
fdccb6df
RK
2772 int dest_loop;
2773 int outer_loop = -1;
b4ad7b23
RS
2774
2775 /* Go backwards until we reach the start of the loop, a label,
2776 or a JUMP_INSN. */
2777 for (p = PREV_INSN (insn);
2778 GET_CODE (p) != CODE_LABEL
2779 && ! (GET_CODE (p) == NOTE
2780 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2781 && GET_CODE (p) != JUMP_INSN;
2782 p = PREV_INSN (p))
2783 ;
2784
edf711a4
RK
2785 /* Check for the case where we have a jump to an inner nested
2786 loop, and do not perform the optimization in that case. */
2787
fdccb6df 2788 if (JUMP_LABEL (insn))
edf711a4 2789 {
fdccb6df
RK
2790 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2791 if (dest_loop != -1)
2792 {
2793 for (outer_loop = dest_loop; outer_loop != -1;
2794 outer_loop = loop_outer_loop[outer_loop])
2795 if (outer_loop == this_loop_num)
2796 break;
2797 }
edf711a4 2798 }
edf711a4 2799
89724a5a
RK
2800 /* Make sure that the target of P is within the current loop. */
2801
9a8e74f0 2802 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
89724a5a
RK
2803 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2804 outer_loop = this_loop_num;
2805
b4ad7b23
RS
2806 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2807 we have a block of code to try to move.
2808
2809 We look backward and then forward from the target of INSN
2810 to find a BARRIER at the same loop depth as the target.
2811 If we find such a BARRIER, we make a new label for the start
2812 of the block, invert the jump in P and point it to that label,
2813 and move the block of code to the spot we found. */
2814
edf711a4
RK
2815 if (outer_loop == -1
2816 && GET_CODE (p) == JUMP_INSN
c6096c5e
RS
2817 && JUMP_LABEL (p) != 0
2818 /* Just ignore jumps to labels that were never emitted.
2819 These always indicate compilation errors. */
2820 && INSN_UID (JUMP_LABEL (p)) != 0
2821 && condjump_p (p)
2822 && ! simplejump_p (p)
2823 && next_real_insn (JUMP_LABEL (p)) == our_next)
b4ad7b23
RS
2824 {
2825 rtx target
2826 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2827 int target_loop_num = uid_loop_num[INSN_UID (target)];
2828 rtx loc;
2829
2830 for (loc = target; loc; loc = PREV_INSN (loc))
2831 if (GET_CODE (loc) == BARRIER
2832 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2833 break;
2834
2835 if (loc == 0)
2836 for (loc = target; loc; loc = NEXT_INSN (loc))
2837 if (GET_CODE (loc) == BARRIER
2838 && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2839 break;
2840
2841 if (loc)
2842 {
2843 rtx cond_label = JUMP_LABEL (p);
2844 rtx new_label = get_label_after (p);
2845
2846 /* Ensure our label doesn't go away. */
2847 LABEL_NUSES (cond_label)++;
2848
2849 /* Verify that uid_loop_num is large enough and that
0f41302f 2850 we can invert P. */
1c01e9df 2851 if (invert_jump (p, new_label))
b4ad7b23
RS
2852 {
2853 rtx q, r;
2854
72ec635f
JL
2855 /* If no suitable BARRIER was found, create a suitable
2856 one before TARGET. Since TARGET is a fall through
2857 path, we'll need to insert an jump around our block
2858 and a add a BARRIER before TARGET.
2859
2860 This creates an extra unconditional jump outside
2861 the loop. However, the benefits of removing rarely
2862 executed instructions from inside the loop usually
2863 outweighs the cost of the extra unconditional jump
2864 outside the loop. */
2865 if (loc == 0)
2866 {
2867 rtx temp;
2868
2869 temp = gen_jump (JUMP_LABEL (insn));
2870 temp = emit_jump_insn_before (temp, target);
2871 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2872 LABEL_NUSES (JUMP_LABEL (insn))++;
2873 loc = emit_barrier_before (target);
2874 }
2875
b4ad7b23
RS
2876 /* Include the BARRIER after INSN and copy the
2877 block after LOC. */
915f619f 2878 new_label = squeeze_notes (new_label, NEXT_INSN (insn));
b4ad7b23
RS
2879 reorder_insns (new_label, NEXT_INSN (insn), loc);
2880
2881 /* All those insns are now in TARGET_LOOP_NUM. */
2882 for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2883 q = NEXT_INSN (q))
2884 uid_loop_num[INSN_UID (q)] = target_loop_num;
2885
2886 /* The label jumped to by INSN is no longer a loop exit.
2887 Unless INSN does not have a label (e.g., it is a
2888 RETURN insn), search loop_number_exit_labels to find
2889 its label_ref, and remove it. Also turn off
2890 LABEL_OUTSIDE_LOOP_P bit. */
2891 if (JUMP_LABEL (insn))
2892 {
353127c2
RK
2893 int loop_num;
2894
b4ad7b23
RS
2895 for (q = 0,
2896 r = loop_number_exit_labels[this_loop_num];
2897 r; q = r, r = LABEL_NEXTREF (r))
2898 if (XEXP (r, 0) == JUMP_LABEL (insn))
2899 {
2900 LABEL_OUTSIDE_LOOP_P (r) = 0;
2901 if (q)
2902 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2903 else
2904 loop_number_exit_labels[this_loop_num]
2905 = LABEL_NEXTREF (r);
2906 break;
2907 }
2908
353127c2
RK
2909 for (loop_num = this_loop_num;
2910 loop_num != -1 && loop_num != target_loop_num;
2911 loop_num = loop_outer_loop[loop_num])
2912 loop_number_exit_count[loop_num]--;
2913
0f41302f 2914 /* If we didn't find it, then something is wrong. */
b4ad7b23
RS
2915 if (! r)
2916 abort ();
2917 }
2918
2919 /* P is now a jump outside the loop, so it must be put
2920 in loop_number_exit_labels, and marked as such.
2921 The easiest way to do this is to just call
2922 mark_loop_jump again for P. */
2923 mark_loop_jump (PATTERN (p), this_loop_num);
2924
2925 /* If INSN now jumps to the insn after it,
2926 delete INSN. */
2927 if (JUMP_LABEL (insn) != 0
2928 && (next_real_insn (JUMP_LABEL (insn))
2929 == next_real_insn (insn)))
2930 delete_insn (insn);
2931 }
2932
2933 /* Continue the loop after where the conditional
2934 branch used to jump, since the only branch insn
2935 in the block (if it still remains) is an inter-loop
2936 branch and hence needs no processing. */
2937 insn = NEXT_INSN (cond_label);
2938
2939 if (--LABEL_NUSES (cond_label) == 0)
2940 delete_insn (cond_label);
3ad0cfaf
RK
2941
2942 /* This loop will be continued with NEXT_INSN (insn). */
2943 insn = PREV_INSN (insn);
b4ad7b23
RS
2944 }
2945 }
2946 }
2947 }
2948}
2949
2950/* If any label in X jumps to a loop different from LOOP_NUM and any of the
2951 loops it is contained in, mark the target loop invalid.
2952
2953 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2954
2955static void
2956mark_loop_jump (x, loop_num)
2957 rtx x;
2958 int loop_num;
2959{
2960 int dest_loop;
2961 int outer_loop;
2962 int i;
2963
2964 switch (GET_CODE (x))
2965 {
2966 case PC:
2967 case USE:
2968 case CLOBBER:
2969 case REG:
2970 case MEM:
2971 case CONST_INT:
2972 case CONST_DOUBLE:
2973 case RETURN:
2974 return;
2975
2976 case CONST:
2977 /* There could be a label reference in here. */
2978 mark_loop_jump (XEXP (x, 0), loop_num);
2979 return;
2980
2981 case PLUS:
2982 case MINUS:
2983 case MULT:
b4ad7b23
RS
2984 mark_loop_jump (XEXP (x, 0), loop_num);
2985 mark_loop_jump (XEXP (x, 1), loop_num);
2986 return;
2987
c4ae2725
JL
2988 case LO_SUM:
2989 /* This may refer to a LABEL_REF or SYMBOL_REF. */
2990 mark_loop_jump (XEXP (x, 1), loop_num);
2991 return;
2992
b4ad7b23
RS
2993 case SIGN_EXTEND:
2994 case ZERO_EXTEND:
2995 mark_loop_jump (XEXP (x, 0), loop_num);
2996 return;
2997
2998 case LABEL_REF:
2999 dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
3000
3001 /* Link together all labels that branch outside the loop. This
3002 is used by final_[bg]iv_value and the loop unrolling code. Also
3003 mark this LABEL_REF so we know that this branch should predict
3004 false. */
3005
edf711a4
RK
3006 /* A check to make sure the label is not in an inner nested loop,
3007 since this does not count as a loop exit. */
3008 if (dest_loop != -1)
3009 {
3010 for (outer_loop = dest_loop; outer_loop != -1;
3011 outer_loop = loop_outer_loop[outer_loop])
3012 if (outer_loop == loop_num)
3013 break;
3014 }
3015 else
3016 outer_loop = -1;
3017
3018 if (loop_num != -1 && outer_loop == -1)
b4ad7b23
RS
3019 {
3020 LABEL_OUTSIDE_LOOP_P (x) = 1;
3021 LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
3022 loop_number_exit_labels[loop_num] = x;
353127c2
RK
3023
3024 for (outer_loop = loop_num;
3025 outer_loop != -1 && outer_loop != dest_loop;
3026 outer_loop = loop_outer_loop[outer_loop])
3027 loop_number_exit_count[outer_loop]++;
b4ad7b23
RS
3028 }
3029
3030 /* If this is inside a loop, but not in the current loop or one enclosed
3031 by it, it invalidates at least one loop. */
3032
3033 if (dest_loop == -1)
3034 return;
3035
3036 /* We must invalidate every nested loop containing the target of this
3037 label, except those that also contain the jump insn. */
3038
3039 for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
3040 {
3041 /* Stop when we reach a loop that also contains the jump insn. */
3042 for (outer_loop = loop_num; outer_loop != -1;
3043 outer_loop = loop_outer_loop[outer_loop])
3044 if (dest_loop == outer_loop)
3045 return;
3046
3047 /* If we get here, we know we need to invalidate a loop. */
3048 if (loop_dump_stream && ! loop_invalid[dest_loop])
3049 fprintf (loop_dump_stream,
3050 "\nLoop at %d ignored due to multiple entry points.\n",
3051 INSN_UID (loop_number_loop_starts[dest_loop]));
3052
3053 loop_invalid[dest_loop] = 1;
3054 }
3055 return;
3056
3057 case SET:
3058 /* If this is not setting pc, ignore. */
3059 if (SET_DEST (x) == pc_rtx)
3060 mark_loop_jump (SET_SRC (x), loop_num);
3061 return;
3062
3063 case IF_THEN_ELSE:
3064 mark_loop_jump (XEXP (x, 1), loop_num);
3065 mark_loop_jump (XEXP (x, 2), loop_num);
3066 return;
3067
3068 case PARALLEL:
3069 case ADDR_VEC:
3070 for (i = 0; i < XVECLEN (x, 0); i++)
3071 mark_loop_jump (XVECEXP (x, 0, i), loop_num);
3072 return;
3073
3074 case ADDR_DIFF_VEC:
3075 for (i = 0; i < XVECLEN (x, 1); i++)
3076 mark_loop_jump (XVECEXP (x, 1, i), loop_num);
3077 return;
3078
3079 default:
c4ae2725
JL
3080 /* Strictly speaking this is not a jump into the loop, only a possible
3081 jump out of the loop. However, we have no way to link the destination
3082 of this jump onto the list of exit labels. To be safe we mark this
3083 loop and any containing loops as invalid. */
b6ccc3fb 3084 if (loop_num != -1)
353127c2 3085 {
353127c2
RK
3086 for (outer_loop = loop_num; outer_loop != -1;
3087 outer_loop = loop_outer_loop[outer_loop])
c4ae2725
JL
3088 {
3089 if (loop_dump_stream && ! loop_invalid[outer_loop])
3090 fprintf (loop_dump_stream,
3091 "\nLoop at %d ignored due to unknown exit jump.\n",
3092 INSN_UID (loop_number_loop_starts[outer_loop]));
3093 loop_invalid[outer_loop] = 1;
3094 }
353127c2 3095 }
b6ccc3fb 3096 return;
b4ad7b23
RS
3097 }
3098}
3099\f
3100/* Return nonzero if there is a label in the range from
3101 insn INSN to and including the insn whose luid is END
3102 INSN must have an assigned luid (i.e., it must not have
3103 been previously created by loop.c). */
3104
3105static int
3106labels_in_range_p (insn, end)
3107 rtx insn;
3108 int end;
3109{
3110 while (insn && INSN_LUID (insn) <= end)
3111 {
3112 if (GET_CODE (insn) == CODE_LABEL)
3113 return 1;
3114 insn = NEXT_INSN (insn);
3115 }
3116
3117 return 0;
3118}
3119
3120/* Record that a memory reference X is being set. */
3121
3122static void
693e265f 3123note_addr_stored (x, y)
b4ad7b23 3124 rtx x;
693e265f 3125 rtx y ATTRIBUTE_UNUSED;
b4ad7b23 3126{
b4ad7b23
RS
3127 if (x == 0 || GET_CODE (x) != MEM)
3128 return;
3129
3130 /* Count number of memory writes.
3131 This affects heuristics in strength_reduce. */
3132 num_mem_sets++;
3133
ca800983
RK
3134 /* BLKmode MEM means all memory is clobbered. */
3135 if (GET_MODE (x) == BLKmode)
3136 unknown_address_altered = 1;
3137
b4ad7b23
RS
3138 if (unknown_address_altered)
3139 return;
3140
5026a502 3141 loop_store_mems = gen_rtx_EXPR_LIST (VOIDmode, x, loop_store_mems);
b4ad7b23
RS
3142}
3143\f
3144/* Return nonzero if the rtx X is invariant over the current loop.
3145
3146 The value is 2 if we refer to something only conditionally invariant.
3147
3148 If `unknown_address_altered' is nonzero, no memory ref is invariant.
3149 Otherwise, a memory ref is invariant if it does not conflict with
3150 anything stored in `loop_store_mems'. */
3151
3152int
3153invariant_p (x)
3154 register rtx x;
3155{
3156 register int i;
3157 register enum rtx_code code;
3158 register char *fmt;
3159 int conditional = 0;
5026a502 3160 rtx mem_list_entry;
b4ad7b23
RS
3161
3162 if (x == 0)
3163 return 1;
3164 code = GET_CODE (x);
3165 switch (code)
3166 {
3167 case CONST_INT:
3168 case CONST_DOUBLE:
3169 case SYMBOL_REF:
3170 case CONST:
3171 return 1;
3172
3173 case LABEL_REF:
3174 /* A LABEL_REF is normally invariant, however, if we are unrolling
3175 loops, and this label is inside the loop, then it isn't invariant.
3176 This is because each unrolled copy of the loop body will have
3177 a copy of this label. If this was invariant, then an insn loading
3178 the address of this label into a register might get moved outside
3179 the loop, and then each loop body would end up using the same label.
3180
3181 We don't know the loop bounds here though, so just fail for all
3182 labels. */
81797aba 3183 if (flag_unroll_loops)
b4ad7b23
RS
3184 return 0;
3185 else
3186 return 1;
3187
3188 case PC:
3189 case CC0:
3190 case UNSPEC_VOLATILE:
3191 return 0;
3192
3193 case REG:
3194 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3195 since the reg might be set by initialization within the loop. */
1f027d54
RK
3196
3197 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3198 || x == arg_pointer_rtx)
3199 && ! current_function_has_nonlocal_goto)
b4ad7b23 3200 return 1;
1f027d54 3201
b4ad7b23
RS
3202 if (loop_has_call
3203 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3204 return 0;
1f027d54 3205
4b259e3f 3206 if (VARRAY_INT (set_in_loop, REGNO (x)) < 0)
b4ad7b23 3207 return 2;
1f027d54 3208
4b259e3f 3209 return VARRAY_INT (set_in_loop, REGNO (x)) == 0;
b4ad7b23
RS
3210
3211 case MEM:
667a4593
JW
3212 /* Volatile memory references must be rejected. Do this before
3213 checking for read-only items, so that volatile read-only items
3214 will be rejected also. */
3215 if (MEM_VOLATILE_P (x))
3216 return 0;
3217
b4ad7b23
RS
3218 /* Read-only items (such as constants in a constant pool) are
3219 invariant if their address is. */
3220 if (RTX_UNCHANGING_P (x))
3221 break;
3222
5026a502
JL
3223 /* If we had a subroutine call, any location in memory could have been
3224 clobbered. */
667a4593 3225 if (unknown_address_altered)
b4ad7b23
RS
3226 return 0;
3227
3228 /* See if there is any dependence between a store and this load. */
5026a502
JL
3229 mem_list_entry = loop_store_mems;
3230 while (mem_list_entry)
3231 {
3232 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3233 x, rtx_varies_p))
3234 return 0;
3235 mem_list_entry = XEXP (mem_list_entry, 1);
3236 }
b4ad7b23
RS
3237
3238 /* It's not invalidated by a store in memory
3239 but we must still verify the address is invariant. */
3240 break;
3241
3242 case ASM_OPERANDS:
3243 /* Don't mess with insns declared volatile. */
3244 if (MEM_VOLATILE_P (x))
3245 return 0;
e9a25f70
JL
3246 break;
3247
3248 default:
3249 break;
b4ad7b23
RS
3250 }
3251
3252 fmt = GET_RTX_FORMAT (code);
3253 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3254 {
3255 if (fmt[i] == 'e')
3256 {
3257 int tem = invariant_p (XEXP (x, i));
3258 if (tem == 0)
3259 return 0;
3260 if (tem == 2)
3261 conditional = 1;
3262 }
3263 else if (fmt[i] == 'E')
3264 {
3265 register int j;
3266 for (j = 0; j < XVECLEN (x, i); j++)
3267 {
3268 int tem = invariant_p (XVECEXP (x, i, j));
3269 if (tem == 0)
3270 return 0;
3271 if (tem == 2)
3272 conditional = 1;
3273 }
3274
3275 }
3276 }
3277
3278 return 1 + conditional;
3279}
3280
b4ad7b23
RS
3281\f
3282/* Return nonzero if all the insns in the loop that set REG
3283 are INSN and the immediately following insns,
3284 and if each of those insns sets REG in an invariant way
3285 (not counting uses of REG in them).
3286
3287 The value is 2 if some of these insns are only conditionally invariant.
3288
3289 We assume that INSN itself is the first set of REG
3290 and that its source is invariant. */
3291
3292static int
3293consec_sets_invariant_p (reg, n_sets, insn)
3294 int n_sets;
3295 rtx reg, insn;
3296{
3297 register rtx p = insn;
3298 register int regno = REGNO (reg);
3299 rtx temp;
3300 /* Number of sets we have to insist on finding after INSN. */
3301 int count = n_sets - 1;
4b259e3f 3302 int old = VARRAY_INT (set_in_loop, regno);
b4ad7b23
RS
3303 int value = 0;
3304 int this;
3305
3306 /* If N_SETS hit the limit, we can't rely on its value. */
3307 if (n_sets == 127)
3308 return 0;
3309
4b259e3f 3310 VARRAY_INT (set_in_loop, regno) = 0;
b4ad7b23
RS
3311
3312 while (count > 0)
3313 {
3314 register enum rtx_code code;
3315 rtx set;
3316
3317 p = NEXT_INSN (p);
3318 code = GET_CODE (p);
3319
38e01259 3320 /* If library call, skip to end of it. */
5fd8383e 3321 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
3322 p = XEXP (temp, 0);
3323
3324 this = 0;
3325 if (code == INSN
3326 && (set = single_set (p))
3327 && GET_CODE (SET_DEST (set)) == REG
3328 && REGNO (SET_DEST (set)) == regno)
3329 {
3330 this = invariant_p (SET_SRC (set));
3331 if (this != 0)
3332 value |= this;
51723711 3333 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
b4ad7b23 3334 {
83d90aac
JW
3335 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3336 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3337 notes are OK. */
3338 this = (CONSTANT_P (XEXP (temp, 0))
3339 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3340 && invariant_p (XEXP (temp, 0))));
b4ad7b23
RS
3341 if (this != 0)
3342 value |= this;
3343 }
3344 }
3345 if (this != 0)
3346 count--;
3347 else if (code != NOTE)
3348 {
4b259e3f 3349 VARRAY_INT (set_in_loop, regno) = old;
b4ad7b23
RS
3350 return 0;
3351 }
3352 }
3353
4b259e3f 3354 VARRAY_INT (set_in_loop, regno) = old;
b4ad7b23
RS
3355 /* If invariant_p ever returned 2, we return 2. */
3356 return 1 + (value & 2);
3357}
3358
3359#if 0
3360/* I don't think this condition is sufficient to allow INSN
3361 to be moved, so we no longer test it. */
3362
3363/* Return 1 if all insns in the basic block of INSN and following INSN
3364 that set REG are invariant according to TABLE. */
3365
3366static int
3367all_sets_invariant_p (reg, insn, table)
3368 rtx reg, insn;
3369 short *table;
3370{
3371 register rtx p = insn;
3372 register int regno = REGNO (reg);
3373
3374 while (1)
3375 {
3376 register enum rtx_code code;
3377 p = NEXT_INSN (p);
3378 code = GET_CODE (p);
3379 if (code == CODE_LABEL || code == JUMP_INSN)
3380 return 1;
3381 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3382 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3383 && REGNO (SET_DEST (PATTERN (p))) == regno)
3384 {
3385 if (!invariant_p (SET_SRC (PATTERN (p)), table))
3386 return 0;
3387 }
3388 }
3389}
3390#endif /* 0 */
3391\f
3392/* Look at all uses (not sets) of registers in X. For each, if it is
3393 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3394 a different insn, set USAGE[REGNO] to const0_rtx. */
3395
3396static void
3397find_single_use_in_loop (insn, x, usage)
3398 rtx insn;
3399 rtx x;
8deb8e2c 3400 varray_type usage;
b4ad7b23
RS
3401{
3402 enum rtx_code code = GET_CODE (x);
3403 char *fmt = GET_RTX_FORMAT (code);
3404 int i, j;
3405
3406 if (code == REG)
8deb8e2c
MM
3407 VARRAY_RTX (usage, REGNO (x))
3408 = (VARRAY_RTX (usage, REGNO (x)) != 0
3409 && VARRAY_RTX (usage, REGNO (x)) != insn)
b4ad7b23
RS
3410 ? const0_rtx : insn;
3411
3412 else if (code == SET)
3413 {
3414 /* Don't count SET_DEST if it is a REG; otherwise count things
3415 in SET_DEST because if a register is partially modified, it won't
3416 show up as a potential movable so we don't care how USAGE is set
3417 for it. */
3418 if (GET_CODE (SET_DEST (x)) != REG)
3419 find_single_use_in_loop (insn, SET_DEST (x), usage);
3420 find_single_use_in_loop (insn, SET_SRC (x), usage);
3421 }
3422 else
3423 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3424 {
3425 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3426 find_single_use_in_loop (insn, XEXP (x, i), usage);
3427 else if (fmt[i] == 'E')
3428 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3429 find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3430 }
3431}
3432\f
a4c3ddd8
BS
3433/* Count and record any set in X which is contained in INSN. Update
3434 MAY_NOT_MOVE and LAST_SET for any register set in X. */
3435
3436static void
3437count_one_set (insn, x, may_not_move, last_set)
3438 rtx insn, x;
3439 varray_type may_not_move;
3440 rtx *last_set;
3441{
3442 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3443 /* Don't move a reg that has an explicit clobber.
3444 It's not worth the pain to try to do it correctly. */
3445 VARRAY_CHAR (may_not_move, REGNO (XEXP (x, 0))) = 1;
3446
3447 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3448 {
3449 rtx dest = SET_DEST (x);
3450 while (GET_CODE (dest) == SUBREG
3451 || GET_CODE (dest) == ZERO_EXTRACT
3452 || GET_CODE (dest) == SIGN_EXTRACT
3453 || GET_CODE (dest) == STRICT_LOW_PART)
3454 dest = XEXP (dest, 0);
3455 if (GET_CODE (dest) == REG)
3456 {
3457 register int regno = REGNO (dest);
3458 /* If this is the first setting of this reg
3459 in current basic block, and it was set before,
3460 it must be set in two basic blocks, so it cannot
3461 be moved out of the loop. */
4b259e3f 3462 if (VARRAY_INT (set_in_loop, regno) > 0
a4c3ddd8
BS
3463 && last_set[regno] == 0)
3464 VARRAY_CHAR (may_not_move, regno) = 1;
3465 /* If this is not first setting in current basic block,
3466 see if reg was used in between previous one and this.
3467 If so, neither one can be moved. */
3468 if (last_set[regno] != 0
3469 && reg_used_between_p (dest, last_set[regno], insn))
3470 VARRAY_CHAR (may_not_move, regno) = 1;
4b259e3f
R
3471 if (VARRAY_INT (set_in_loop, regno) < 127)
3472 ++VARRAY_INT (set_in_loop, regno);
a4c3ddd8
BS
3473 last_set[regno] = insn;
3474 }
3475 }
3476}
3477
4b259e3f 3478/* Increment SET_IN_LOOP at the index of each register
b4ad7b23 3479 that is modified by an insn between FROM and TO.
4b259e3f 3480 If the value of an element of SET_IN_LOOP becomes 127 or more,
b4ad7b23
RS
3481 stop incrementing it, to avoid overflow.
3482
3483 Store in SINGLE_USAGE[I] the single insn in which register I is
3484 used, if it is only used once. Otherwise, it is set to 0 (for no
3485 uses) or const0_rtx for more than one use. This parameter may be zero,
3486 in which case this processing is not done.
3487
3488 Store in *COUNT_PTR the number of actual instruction
3489 in the loop. We use this to decide what is worth moving out. */
3490
3491/* last_set[n] is nonzero iff reg n has been set in the current basic block.
3492 In that case, it is the insn that last set reg n. */
3493
3494static void
3495count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3496 register rtx from, to;
8deb8e2c
MM
3497 varray_type may_not_move;
3498 varray_type single_usage;
b4ad7b23
RS
3499 int *count_ptr;
3500 int nregs;
3501{
3502 register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3503 register rtx insn;
3504 register int count = 0;
b4ad7b23 3505
4c9a05bc 3506 bzero ((char *) last_set, nregs * sizeof (rtx));
b4ad7b23
RS
3507 for (insn = from; insn != to; insn = NEXT_INSN (insn))
3508 {
3509 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3510 {
3511 ++count;
3512
d6b44532
RH
3513 /* Record registers that have exactly one use. */
3514 find_single_use_in_loop (insn, PATTERN (insn), single_usage);
b4ad7b23 3515
d6b44532
RH
3516 /* Include uses in REG_EQUAL notes. */
3517 if (REG_NOTES (insn))
3518 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
b4ad7b23 3519
b4ad7b23
RS
3520 if (GET_CODE (PATTERN (insn)) == SET
3521 || GET_CODE (PATTERN (insn)) == CLOBBER)
a4c3ddd8 3522 count_one_set (insn, PATTERN (insn), may_not_move, last_set);
b4ad7b23
RS
3523 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3524 {
3525 register int i;
3526 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
a4c3ddd8
BS
3527 count_one_set (insn, XVECEXP (PATTERN (insn), 0, i),
3528 may_not_move, last_set);
b4ad7b23
RS
3529 }
3530 }
4c9a05bc 3531
b4ad7b23 3532 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
4c9a05bc 3533 bzero ((char *) last_set, nregs * sizeof (rtx));
b4ad7b23
RS
3534 }
3535 *count_ptr = count;
3536}
3537\f
3538/* Given a loop that is bounded by LOOP_START and LOOP_END
3539 and that is entered at SCAN_START,
3540 return 1 if the register set in SET contained in insn INSN is used by
3541 any insn that precedes INSN in cyclic order starting
3542 from the loop entry point.
3543
3544 We don't want to use INSN_LUID here because if we restrict INSN to those
3545 that have a valid INSN_LUID, it means we cannot move an invariant out
3546 from an inner loop past two loops. */
3547
3548static int
3549loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3550 rtx set, insn, loop_start, scan_start, loop_end;
3551{
3552 rtx reg = SET_DEST (set);
3553 rtx p;
3554
3555 /* Scan forward checking for register usage. If we hit INSN, we
3556 are done. Otherwise, if we hit LOOP_END, wrap around to LOOP_START. */
3557 for (p = scan_start; p != insn; p = NEXT_INSN (p))
3558 {
3559 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3560 && reg_overlap_mentioned_p (reg, PATTERN (p)))
3561 return 1;
3562
3563 if (p == loop_end)
3564 p = loop_start;
3565 }
3566
3567 return 0;
3568}
3569\f
3570/* A "basic induction variable" or biv is a pseudo reg that is set
3571 (within this loop) only by incrementing or decrementing it. */
3572/* A "general induction variable" or giv is a pseudo reg whose
3573 value is a linear function of a biv. */
3574
3575/* Bivs are recognized by `basic_induction_var';
45f97e2e 3576 Givs by `general_induction_var'. */
b4ad7b23
RS
3577
3578/* Indexed by register number, indicates whether or not register is an
3579 induction variable, and if so what type. */
3580
3ec2b590 3581varray_type reg_iv_type;
b4ad7b23
RS
3582
3583/* Indexed by register number, contains pointer to `struct induction'
3584 if register is an induction variable. This holds general info for
3585 all induction variables. */
3586
3ec2b590 3587varray_type reg_iv_info;
b4ad7b23
RS
3588
3589/* Indexed by register number, contains pointer to `struct iv_class'
3590 if register is a basic induction variable. This holds info describing
3591 the class (a related group) of induction variables that the biv belongs
3592 to. */
3593
3594struct iv_class **reg_biv_class;
3595
3596/* The head of a list which links together (via the next field)
3597 every iv class for the current loop. */
3598
3599struct iv_class *loop_iv_list;
3600
3ec2b590
R
3601/* Givs made from biv increments are always splittable for loop unrolling.
3602 Since there is no regscan info for them, we have to keep track of them
3603 separately. */
3604int first_increment_giv, last_increment_giv;
3605
b4ad7b23
RS
3606/* Communication with routines called via `note_stores'. */
3607
3608static rtx note_insn;
3609
3610/* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
3611
3612static rtx addr_placeholder;
3613
3614/* ??? Unfinished optimizations, and possible future optimizations,
3615 for the strength reduction code. */
3616
b4ad7b23 3617/* ??? The interaction of biv elimination, and recognition of 'constant'
0f41302f 3618 bivs, may cause problems. */
b4ad7b23
RS
3619
3620/* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3621 performance problems.
3622
3623 Perhaps don't eliminate things that can be combined with an addressing
3624 mode. Find all givs that have the same biv, mult_val, and add_val;
3625 then for each giv, check to see if its only use dies in a following
3626 memory address. If so, generate a new memory address and check to see
3627 if it is valid. If it is valid, then store the modified memory address,
3628 otherwise, mark the giv as not done so that it will get its own iv. */
3629
3630/* ??? Could try to optimize branches when it is known that a biv is always
3631 positive. */
3632
3633/* ??? When replace a biv in a compare insn, we should replace with closest
3634 giv so that an optimized branch can still be recognized by the combiner,
3635 e.g. the VAX acb insn. */
3636
3637/* ??? Many of the checks involving uid_luid could be simplified if regscan
3638 was rerun in loop_optimize whenever a register was added or moved.
3639 Also, some of the optimizations could be a little less conservative. */
3640\f
41a972a9 3641/* Perform strength reduction and induction variable elimination.
b4ad7b23 3642
41a972a9 3643 Pseudo registers created during this function will be beyond the last
b4ad7b23
RS
3644 valid index in several tables including n_times_set and regno_last_uid.
3645 This does not cause a problem here, because the added registers cannot be
3646 givs outside of their loop, and hence will never be reconsidered.
41a972a9
MM
3647 But scan_loop must check regnos to make sure they are in bounds.
3648
3649 SCAN_START is the first instruction in the loop, as the loop would
3650 actually be executed. END is the NOTE_INSN_LOOP_END. LOOP_TOP is
3651 the first instruction in the loop, as it is layed out in the
6dd49eb4
R
3652 instruction stream. LOOP_START is the NOTE_INSN_LOOP_BEG.
3653 LOOP_CONT is the NOTE_INSN_LOOP_CONT. */
b4ad7b23
RS
3654
3655static void
3656strength_reduce (scan_start, end, loop_top, insn_count,
6dd49eb4 3657 loop_start, loop_end, loop_cont, unroll_p, bct_p)
b4ad7b23
RS
3658 rtx scan_start;
3659 rtx end;
3660 rtx loop_top;
3661 int insn_count;
3662 rtx loop_start;
3663 rtx loop_end;
6dd49eb4 3664 rtx loop_cont;
d46965b9 3665 int unroll_p, bct_p ATTRIBUTE_UNUSED;
b4ad7b23
RS
3666{
3667 rtx p;
3668 rtx set;
3669 rtx inc_val;
3670 rtx mult_val;
3671 rtx dest_reg;
3ec2b590 3672 rtx *location;
b4ad7b23
RS
3673 /* This is 1 if current insn is not executed at least once for every loop
3674 iteration. */
3675 int not_every_iteration = 0;
7dcd3836
RK
3676 /* This is 1 if current insn may be executed more than once for every
3677 loop iteration. */
3678 int maybe_multiple = 0;
ae188a87
JL
3679 /* This is 1 if we have past a branch back to the top of the loop
3680 (aka a loop latch). */
3681 int past_loop_latch = 0;
b4ad7b23
RS
3682 /* Temporary list pointers for traversing loop_iv_list. */
3683 struct iv_class *bl, **backbl;
3684 /* Ratio of extra register life span we can justify
3685 for saving an instruction. More if loop doesn't call subroutines
3686 since in that case saving an insn makes more difference
3687 and more registers are available. */
3688 /* ??? could set this to last value of threshold in move_movables */
3689 int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3690 /* Map of pseudo-register replacements. */
3691 rtx *reg_map;
97ec0ad8 3692 int reg_map_size;
b4ad7b23
RS
3693 int call_seen;
3694 rtx test;
3695 rtx end_insert_before;
5ea7a4ae 3696 int loop_depth = 0;
3ec2b590 3697 int n_extra_increment;
302670f3
MH
3698 struct loop_info loop_iteration_info;
3699 struct loop_info *loop_info = &loop_iteration_info;
b4ad7b23 3700
5353610b
R
3701 /* If scan_start points to the loop exit test, we have to be wary of
3702 subversive use of gotos inside expression statements. */
3703 if (prev_nonnote_insn (scan_start) != prev_nonnote_insn (loop_start))
3704 maybe_multiple = back_branch_in_range_p (scan_start, loop_start, loop_end);
3705
3ec2b590
R
3706 VARRAY_INT_INIT (reg_iv_type, max_reg_before_loop, "reg_iv_type");
3707 VARRAY_GENERIC_PTR_INIT (reg_iv_info, max_reg_before_loop, "reg_iv_info");
b4ad7b23
RS
3708 reg_biv_class = (struct iv_class **)
3709 alloca (max_reg_before_loop * sizeof (struct iv_class *));
3710 bzero ((char *) reg_biv_class, (max_reg_before_loop
3711 * sizeof (struct iv_class *)));
3712
3713 loop_iv_list = 0;
3714 addr_placeholder = gen_reg_rtx (Pmode);
3715
3716 /* Save insn immediately after the loop_end. Insns inserted after loop_end
3717 must be put before this insn, so that they will appear in the right
b2586fe0 3718 order (i.e. loop order).
b4ad7b23 3719
b2586fe0
JL
3720 If loop_end is the end of the current function, then emit a
3721 NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3722 dummy note insn. */
3723 if (NEXT_INSN (loop_end) != 0)
3724 end_insert_before = NEXT_INSN (loop_end);
3725 else
3726 end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
b4ad7b23
RS
3727
3728 /* Scan through loop to find all possible bivs. */
3729
41a972a9
MM
3730 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
3731 p != NULL_RTX;
3732 p = next_insn_in_loop (p, scan_start, end, loop_top))
b4ad7b23 3733 {
b4ad7b23
RS
3734 if (GET_CODE (p) == INSN
3735 && (set = single_set (p))
3736 && GET_CODE (SET_DEST (set)) == REG)
3737 {
3738 dest_reg = SET_DEST (set);
3739 if (REGNO (dest_reg) < max_reg_before_loop
3740 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3ec2b590 3741 && REG_IV_TYPE (REGNO (dest_reg)) != NOT_BASIC_INDUCT)
b4ad7b23 3742 {
7056f7e8 3743 if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3ec2b590
R
3744 dest_reg, p, &inc_val, &mult_val,
3745 &location))
b4ad7b23
RS
3746 {
3747 /* It is a possible basic induction variable.
3748 Create and initialize an induction structure for it. */
3749
3750 struct induction *v
3751 = (struct induction *) alloca (sizeof (struct induction));
3752
3ec2b590 3753 record_biv (v, p, dest_reg, inc_val, mult_val, location,
7dcd3836 3754 not_every_iteration, maybe_multiple);
3ec2b590 3755 REG_IV_TYPE (REGNO (dest_reg)) = BASIC_INDUCT;
b4ad7b23
RS
3756 }
3757 else if (REGNO (dest_reg) < max_reg_before_loop)
3ec2b590 3758 REG_IV_TYPE (REGNO (dest_reg)) = NOT_BASIC_INDUCT;
b4ad7b23
RS
3759 }
3760 }
3761
7dcd3836
RK
3762 /* Past CODE_LABEL, we get to insns that may be executed multiple
3763 times. The only way we can be sure that they can't is if every
38e01259 3764 jump insn between here and the end of the loop either
5353610b
R
3765 returns, exits the loop, is a jump to a location that is still
3766 behind the label, or is a jump to the loop start. */
7dcd3836
RK
3767
3768 if (GET_CODE (p) == CODE_LABEL)
3769 {
3770 rtx insn = p;
3771
3772 maybe_multiple = 0;
3773
3774 while (1)
3775 {
3776 insn = NEXT_INSN (insn);
3777 if (insn == scan_start)
3778 break;
3779 if (insn == end)
3780 {
3781 if (loop_top != 0)
f67ff5de 3782 insn = loop_top;
7dcd3836
RK
3783 else
3784 break;
3785 if (insn == scan_start)
3786 break;
3787 }
3788
3789 if (GET_CODE (insn) == JUMP_INSN
3790 && GET_CODE (PATTERN (insn)) != RETURN
3791 && (! condjump_p (insn)
3792 || (JUMP_LABEL (insn) != 0
8516af93 3793 && JUMP_LABEL (insn) != scan_start
1cb1fe66 3794 && ! loop_insn_first_p (p, JUMP_LABEL (insn)))))
8516af93
JW
3795 {
3796 maybe_multiple = 1;
3797 break;
3798 }
7dcd3836
RK
3799 }
3800 }
3801
8516af93
JW
3802 /* Past a jump, we get to insns for which we can't count
3803 on whether they will be executed during each iteration. */
3804 /* This code appears twice in strength_reduce. There is also similar
3805 code in scan_loop. */
3806 if (GET_CODE (p) == JUMP_INSN
b4ad7b23
RS
3807 /* If we enter the loop in the middle, and scan around to the
3808 beginning, don't set not_every_iteration for that.
3809 This can be any kind of jump, since we want to know if insns
3810 will be executed if the loop is executed. */
8516af93 3811 && ! (JUMP_LABEL (p) == loop_top
b4ad7b23
RS
3812 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3813 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
8516af93
JW
3814 {
3815 rtx label = 0;
3816
3817 /* If this is a jump outside the loop, then it also doesn't
3818 matter. Check to see if the target of this branch is on the
3819 loop_number_exits_labels list. */
3820
3821 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3822 label;
3823 label = LABEL_NEXTREF (label))
3824 if (XEXP (label, 0) == JUMP_LABEL (p))
3825 break;
3826
3827 if (! label)
3828 not_every_iteration = 1;
3829 }
b4ad7b23 3830
5ea7a4ae
JW
3831 else if (GET_CODE (p) == NOTE)
3832 {
3833 /* At the virtual top of a converted loop, insns are again known to
3834 be executed each iteration: logically, the loop begins here
5f3db57e
JL
3835 even though the exit code has been duplicated.
3836
3837 Insns are also again known to be executed each iteration at
3838 the LOOP_CONT note. */
3839 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
3840 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
3841 && loop_depth == 0)
5ea7a4ae
JW
3842 not_every_iteration = 0;
3843 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3844 loop_depth++;
3845 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3846 loop_depth--;
3847 }
b4ad7b23 3848
ae188a87
JL
3849 /* Note if we pass a loop latch. If we do, then we can not clear
3850 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
3851 a loop since a jump before the last CODE_LABEL may have started
3852 a new loop iteration.
3853
3854 Note that LOOP_TOP is only set for rotated loops and we need
3855 this check for all loops, so compare against the CODE_LABEL
3856 which immediately follows LOOP_START. */
3857 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == NEXT_INSN (loop_start))
3858 past_loop_latch = 1;
3859
b4ad7b23
RS
3860 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3861 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3862 or not an insn is known to be executed each iteration of the
3863 loop, whether or not any iterations are known to occur.
3864
3865 Therefore, if we have just passed a label and have no more labels
ae188a87
JL
3866 between here and the test insn of the loop, and we have not passed
3867 a jump to the top of the loop, then we know these insns will be
3868 executed each iteration. */
b4ad7b23 3869
ae188a87
JL
3870 if (not_every_iteration
3871 && ! past_loop_latch
3872 && GET_CODE (p) == CODE_LABEL
6dd49eb4 3873 && no_labels_between_p (p, loop_end)
1cb1fe66 3874 && loop_insn_first_p (p, loop_cont))
b4ad7b23
RS
3875 not_every_iteration = 0;
3876 }
3877
3878 /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3879 Make a sanity check against n_times_set. */
3880 for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3881 {
3ec2b590 3882 if (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
b4ad7b23
RS
3883 /* Above happens if register modified by subreg, etc. */
3884 /* Make sure it is not recognized as a basic induction var: */
8deb8e2c 3885 || VARRAY_INT (n_times_set, bl->regno) != bl->biv_count
b4ad7b23
RS
3886 /* If never incremented, it is invariant that we decided not to
3887 move. So leave it alone. */
3888 || ! bl->incremented)
3889 {
3890 if (loop_dump_stream)
3891 fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3892 bl->regno,
3ec2b590 3893 (REG_IV_TYPE (bl->regno) != BASIC_INDUCT
b4ad7b23
RS
3894 ? "not induction variable"
3895 : (! bl->incremented ? "never incremented"
3896 : "count error")));
3897
3ec2b590 3898 REG_IV_TYPE (bl->regno) = NOT_BASIC_INDUCT;
b4ad7b23
RS
3899 *backbl = bl->next;
3900 }
3901 else
3902 {
3903 backbl = &bl->next;
3904
3905 if (loop_dump_stream)
3906 fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3907 }
3908 }
3909
3910 /* Exit if there are no bivs. */
3911 if (! loop_iv_list)
3912 {
3913 /* Can still unroll the loop anyways, but indicate that there is no
3914 strength reduction info available. */
81797aba 3915 if (unroll_p)
302670f3
MH
3916 unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
3917 loop_info, 0);
b4ad7b23
RS
3918
3919 return;
3920 }
3921
3922 /* Find initial value for each biv by searching backwards from loop_start,
3923 halting at first label. Also record any test condition. */
3924
3925 call_seen = 0;
3926 for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3927 {
3928 note_insn = p;
3929
3930 if (GET_CODE (p) == CALL_INSN)
3931 call_seen = 1;
3932
3933 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3934 || GET_CODE (p) == CALL_INSN)
3935 note_stores (PATTERN (p), record_initial);
3936
3937 /* Record any test of a biv that branches around the loop if no store
3938 between it and the start of loop. We only care about tests with
3939 constants and registers and only certain of those. */
3940 if (GET_CODE (p) == JUMP_INSN
3941 && JUMP_LABEL (p) != 0
3942 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3943 && (test = get_condition_for_loop (p)) != 0
3944 && GET_CODE (XEXP (test, 0)) == REG
3945 && REGNO (XEXP (test, 0)) < max_reg_before_loop
3946 && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3947 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3948 && bl->init_insn == 0)
3949 {
3950 /* If an NE test, we have an initial value! */
3951 if (GET_CODE (test) == NE)
3952 {
3953 bl->init_insn = p;
38a448ca
RH
3954 bl->init_set = gen_rtx_SET (VOIDmode,
3955 XEXP (test, 0), XEXP (test, 1));
b4ad7b23
RS
3956 }
3957 else
3958 bl->initial_test = test;
3959 }
3960 }
3961
3962 /* Look at the each biv and see if we can say anything better about its
3963 initial value from any initializing insns set up above. (This is done
3964 in two passes to avoid missing SETs in a PARALLEL.) */
53dc05e4 3965 for (backbl = &loop_iv_list; (bl = *backbl); backbl = &bl->next)
b4ad7b23
RS
3966 {
3967 rtx src;
956d6950 3968 rtx note;
b4ad7b23
RS
3969
3970 if (! bl->init_insn)
3971 continue;
3972
956d6950
JL
3973 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
3974 is a constant, use the value of that. */
3975 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
3976 && CONSTANT_P (XEXP (note, 0)))
3977 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
3978 && CONSTANT_P (XEXP (note, 0))))
3979 src = XEXP (note, 0);
3980 else
3981 src = SET_SRC (bl->init_set);
b4ad7b23
RS
3982
3983 if (loop_dump_stream)
3984 fprintf (loop_dump_stream,
3985 "Biv %d initialized at insn %d: initial value ",
3986 bl->regno, INSN_UID (bl->init_insn));
3987
43a674af
JW
3988 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3989 || GET_MODE (src) == VOIDmode)
63d59526 3990 && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
b4ad7b23
RS
3991 {
3992 bl->initial_value = src;
3993
3994 if (loop_dump_stream)
3995 {
3996 if (GET_CODE (src) == CONST_INT)
9ba7a303
JC
3997 {
3998 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
3999 fputc ('\n', loop_dump_stream);
4000 }
b4ad7b23
RS
4001 else
4002 {
4003 print_rtl (loop_dump_stream, src);
4004 fprintf (loop_dump_stream, "\n");
4005 }
4006 }
4007 }
4008 else
4009 {
3ec2b590
R
4010 struct iv_class *bl2 = 0;
4011 rtx increment;
4012
4013 /* Biv initial value is not a simple move. If it is the sum of
4014 another biv and a constant, check if both bivs are incremented
4015 in lockstep. Then we are actually looking at a giv.
4016 For simplicity, we only handle the case where there is but a
4017 single increment, and the register is not used elsewhere. */
4018 if (bl->biv_count == 1
4019 && bl->regno < max_reg_before_loop
4020 && uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4021 && GET_CODE (src) == PLUS
4022 && GET_CODE (XEXP (src, 0)) == REG
4023 && CONSTANT_P (XEXP (src, 1))
4024 && ((increment = biv_total_increment (bl, loop_start, loop_end))
4025 != NULL_RTX))
4026 {
4027 int regno = REGNO (XEXP (src, 0));
b4ad7b23 4028
3ec2b590
R
4029 for (bl2 = loop_iv_list; bl2; bl2 = bl2->next)
4030 if (bl2->regno == regno)
4031 break;
4032 }
4033
4034 /* Now, can we transform this biv into a giv? */
4035 if (bl2
4036 && bl2->biv_count == 1
4037 && rtx_equal_p (increment,
4038 biv_total_increment (bl2, loop_start, loop_end))
4039 /* init_insn is only set to insns that are before loop_start
4040 without any intervening labels. */
4041 && ! reg_set_between_p (bl2->biv->src_reg,
4042 PREV_INSN (bl->init_insn), loop_start)
4043 /* The register from BL2 must be set before the register from
4044 BL is set, or we must be able to move the latter set after
4045 the former set. Currently there can't be any labels
4046 in-between when biv_toal_increment returns nonzero both times
4047 but we test it here in case some day some real cfg analysis
4048 gets used to set always_computable. */
1cb1fe66 4049 && ((loop_insn_first_p (bl2->biv->insn, bl->biv->insn)
3ec2b590
R
4050 && no_labels_between_p (bl2->biv->insn, bl->biv->insn))
4051 || (! reg_used_between_p (bl->biv->src_reg, bl->biv->insn,
4052 bl2->biv->insn)
4053 && no_jumps_between_p (bl->biv->insn, bl2->biv->insn)))
4054 && validate_change (bl->biv->insn,
4055 &SET_SRC (single_set (bl->biv->insn)),
4056 copy_rtx (src), 0))
4057 {
4058 int loop_num = uid_loop_num[INSN_UID (loop_start)];
4059 rtx dominator = loop_number_cont_dominator[loop_num];
3ec2b590
R
4060 rtx giv = bl->biv->src_reg;
4061 rtx giv_insn = bl->biv->insn;
4062 rtx after_giv = NEXT_INSN (giv_insn);
4063
4064 if (loop_dump_stream)
4065 fprintf (loop_dump_stream, "is giv of biv %d\n", bl2->regno);
4066 /* Let this giv be discovered by the generic code. */
4067 REG_IV_TYPE (bl->regno) = UNKNOWN_INDUCT;
41c1b007 4068 reg_biv_class[bl->regno] = NULL_PTR;
3ec2b590
R
4069 /* We can get better optimization if we can move the giv setting
4070 before the first giv use. */
4071 if (dominator
079a615d 4072 && ! loop_insn_first_p (dominator, scan_start)
3ec2b590
R
4073 && ! reg_set_between_p (bl2->biv->src_reg, loop_start,
4074 dominator)
4075 && ! reg_used_between_p (giv, loop_start, dominator)
4076 && ! reg_used_between_p (giv, giv_insn, loop_end))
4077 {
4078 rtx p;
22b4cc65 4079 rtx next;
3ec2b590 4080
22b4cc65 4081 for (next = NEXT_INSN (dominator); ; next = NEXT_INSN (next))
3ec2b590 4082 {
3ec2b590
R
4083 if ((GET_RTX_CLASS (GET_CODE (next)) == 'i'
4084 && (reg_mentioned_p (giv, PATTERN (next))
4085 || reg_set_p (bl2->biv->src_reg, next)))
4086 || GET_CODE (next) == JUMP_INSN)
4087 break;
4088#ifdef HAVE_cc0
4089 if (GET_RTX_CLASS (GET_CODE (next)) != 'i'
4090 || ! sets_cc0_p (PATTERN (next)))
4091#endif
4092 dominator = next;
4093 }
4094 if (loop_dump_stream)
4095 fprintf (loop_dump_stream, "move after insn %d\n",
4096 INSN_UID (dominator));
4097 /* Avoid problems with luids by actually moving the insn
4098 and adjusting all luids in the range. */
4099 reorder_insns (giv_insn, giv_insn, dominator);
4100 for (p = dominator; INSN_UID (p) >= max_uid_for_loop; )
4101 p = PREV_INSN (p);
4102 compute_luids (giv_insn, after_giv, INSN_LUID (p));
4103 /* If the only purpose of the init insn is to initialize
4104 this giv, delete it. */
4105 if (single_set (bl->init_insn)
4106 && ! reg_used_between_p (giv, bl->init_insn, loop_start))
4107 delete_insn (bl->init_insn);
4108 }
1cb1fe66 4109 else if (! loop_insn_first_p (bl2->biv->insn, bl->biv->insn))
3ec2b590
R
4110 {
4111 rtx p = PREV_INSN (giv_insn);
4112 while (INSN_UID (p) >= max_uid_for_loop)
4113 p = PREV_INSN (p);
4114 reorder_insns (giv_insn, giv_insn, bl2->biv->insn);
4115 compute_luids (after_giv, NEXT_INSN (giv_insn),
4116 INSN_LUID (p));
4117 }
4118 /* Remove this biv from the chain. */
4119 if (bl->next)
41c1b007
R
4120 {
4121 /* We move the following giv from *bl->next into *bl.
4122 We have to update reg_biv_class for that moved biv
4123 to point to its new address. */
4124 *bl = *bl->next;
4125 reg_biv_class[bl->regno] = bl;
4126 }
3ec2b590
R
4127 else
4128 {
4129 *backbl = 0;
4130 break;
4131 }
4132 }
4133
4134 /* If we can't make it a giv,
4135 let biv keep initial value of "itself". */
4136 else if (loop_dump_stream)
b4ad7b23
RS
4137 fprintf (loop_dump_stream, "is complex\n");
4138 }
4139 }
4140
3ec2b590
R
4141 /* If a biv is unconditionally incremented several times in a row, convert
4142 all but the last increment into a giv. */
4143
4144 /* Get an upper bound for the number of registers
4145 we might have after all bivs have been processed. */
4146 first_increment_giv = max_reg_num ();
4147 for (n_extra_increment = 0, bl = loop_iv_list; bl; bl = bl->next)
4148 n_extra_increment += bl->biv_count - 1;
6449b397
NC
4149
4150 /* If the loop contains volatile memory references do not allow any
4151 replacements to take place, since this could loose the volatile markers. */
d4ca58b3 4152 if (n_extra_increment && ! loop_has_volatile)
3ec2b590
R
4153 {
4154 int nregs = first_increment_giv + n_extra_increment;
4155
4156 /* Reallocate reg_iv_type and reg_iv_info. */
4157 VARRAY_GROW (reg_iv_type, nregs);
4158 VARRAY_GROW (reg_iv_info, nregs);
4159
4160 for (bl = loop_iv_list; bl; bl = bl->next)
4161 {
4162 struct induction **vp, *v, *next;
b72bdd84
R
4163 int biv_dead_after_loop = 0;
4164
3ec2b590
R
4165 /* The biv increments lists are in reverse order. Fix this first. */
4166 for (v = bl->biv, bl->biv = 0; v; v = next)
4167 {
4168 next = v->next_iv;
4169 v->next_iv = bl->biv;
4170 bl->biv = v;
4171 }
b72bdd84
R
4172
4173 /* We must guard against the case that an early exit between v->insn
4174 and next->insn leaves the biv live after the loop, since that
4175 would mean that we'd be missing an increment for the final
4176 value. The following test to set biv_dead_after_loop is like
4177 the first part of the test to set bl->eliminable.
4178 We don't check here if we can calculate the final value, since
4179 this can't succeed if we already know that there is a jump
4180 between v->insn and next->insn, yet next->always_executed is
4181 set and next->maybe_multiple is cleared. Such a combination
cc291433 4182 implies that the jump destination is outside the loop.
b72bdd84
R
4183 If we want to make this check more sophisticated, we should
4184 check each branch between v->insn and next->insn individually
cc291433 4185 to see if the biv is dead at its destination. */
b72bdd84
R
4186
4187 if (uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
4188 && bl->init_insn
4189 && INSN_UID (bl->init_insn) < max_uid_for_loop
4190 && (uid_luid[REGNO_FIRST_UID (bl->regno)]
4191 >= INSN_LUID (bl->init_insn))
4192#ifdef HAVE_decrement_and_branch_until_zero
4193 && ! bl->nonneg
4194#endif
4195 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4196 biv_dead_after_loop = 1;
4197
3ec2b590
R
4198 for (vp = &bl->biv, next = *vp; v = next, next = v->next_iv;)
4199 {
4200 HOST_WIDE_INT offset;
a1c0cf1f 4201 rtx set, add_val, old_reg, dest_reg, last_use_insn, note;
3ec2b590 4202 int old_regno, new_regno;
b72bdd84 4203
3ec2b590
R
4204 if (! v->always_executed
4205 || v->maybe_multiple
4206 || GET_CODE (v->add_val) != CONST_INT
4207 || ! next->always_executed
4208 || next->maybe_multiple
b72bdd84 4209 || ! CONSTANT_P (next->add_val)
2485126f
R
4210 || v->mult_val != const1_rtx
4211 || next->mult_val != const1_rtx
b72bdd84
R
4212 || ! (biv_dead_after_loop
4213 || no_jumps_between_p (v->insn, next->insn)))
3ec2b590
R
4214 {
4215 vp = &v->next_iv;
4216 continue;
4217 }
4218 offset = INTVAL (v->add_val);
4219 set = single_set (v->insn);
4220 add_val = plus_constant (next->add_val, offset);
4221 old_reg = v->dest_reg;
4222 dest_reg = gen_reg_rtx (v->mode);
4223
f56246be
R
4224 /* Unlike reg_iv_type / reg_iv_info, the other three arrays
4225 have been allocated with some slop space, so we may not
4226 actually need to reallocate them. If we do, the following
4227 if statement will be executed just once in this loop. */
3ec2b590
R
4228 if ((unsigned) max_reg_num () > n_times_set->num_elements)
4229 {
f56246be 4230 /* Grow all the remaining arrays. */
3ec2b590
R
4231 VARRAY_GROW (set_in_loop, nregs);
4232 VARRAY_GROW (n_times_set, nregs);
4233 VARRAY_GROW (may_not_optimize, nregs);
a366a40a 4234 VARRAY_GROW (reg_single_usage, nregs);
3ec2b590
R
4235 }
4236
8e9fb571 4237 if (! validate_change (next->insn, next->location, add_val, 0))
3ec2b590
R
4238 {
4239 vp = &v->next_iv;
4240 continue;
4241 }
8e9fb571
R
4242
4243 /* Here we can try to eliminate the increment by combining
4244 it into the uses. */
4245
4246 /* Set last_use_insn so that we can check against it. */
4247
4248 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4249 p != next->insn;
4250 p = next_insn_in_loop (p, scan_start, end, loop_top))
4251 {
8e9fb571
R
4252 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
4253 continue;
4254 if (reg_mentioned_p (old_reg, PATTERN (p)))
4255 {
4256 last_use_insn = p;
4257 }
4258 }
4259
4260 /* If we can't get the LUIDs for the insns, we can't
4261 calculate the lifetime. This is likely from unrolling
4262 of an inner loop, so there is little point in making this
4263 a DEST_REG giv anyways. */
4264 if (INSN_UID (v->insn) >= max_uid_for_loop
4265 || INSN_UID (last_use_insn) >= max_uid_for_loop
4266 || ! validate_change (v->insn, &SET_DEST (set), dest_reg, 0))
4267 {
4268 /* Change the increment at NEXT back to what it was. */
4269 if (! validate_change (next->insn, next->location,
4270 next->add_val, 0))
4271 abort ();
4272 vp = &v->next_iv;
4273 continue;
4274 }
3ec2b590
R
4275 next->add_val = add_val;
4276 v->dest_reg = dest_reg;
4277 v->giv_type = DEST_REG;
4278 v->location = &SET_SRC (set);
4279 v->cant_derive = 0;
4280 v->combined_with = 0;
4281 v->maybe_dead = 0;
4282 v->derive_adjustment = 0;
4283 v->same = 0;
4284 v->ignore = 0;
4285 v->new_reg = 0;
4286 v->final_value = 0;
4287 v->same_insn = 0;
4288 v->auto_inc_opt = 0;
4289 v->unrolled = 0;
4290 v->shared = 0;
4d87f7a7 4291 v->derived_from = 0;
3ec2b590
R
4292 v->always_computable = 1;
4293 v->always_executed = 1;
4294 v->replaceable = 1;
4295 v->no_const_addval = 0;
4296
4297 old_regno = REGNO (old_reg);
4298 new_regno = REGNO (dest_reg);
4299 VARRAY_INT (set_in_loop, old_regno)--;
4300 VARRAY_INT (set_in_loop, new_regno) = 1;
4301 VARRAY_INT (n_times_set, old_regno)--;
4302 VARRAY_INT (n_times_set, new_regno) = 1;
4303 VARRAY_CHAR (may_not_optimize, new_regno) = 0;
4304
4305 REG_IV_TYPE (new_regno) = GENERAL_INDUCT;
4306 REG_IV_INFO (new_regno) = v;
a1c0cf1f
R
4307
4308 /* If next_insn has a REG_EQUAL note that mentiones OLD_REG,
4309 it must be replaced. */
4310 note = find_reg_note (next->insn, REG_EQUAL, NULL_RTX);
4311 if (note && reg_mentioned_p (old_reg, XEXP (note, 0)))
4312 XEXP (note, 0) = copy_rtx (SET_SRC (single_set (next->insn)));
4313
3ec2b590
R
4314 /* Remove the increment from the list of biv increments,
4315 and record it as a giv. */
4316 *vp = next;
4317 bl->biv_count--;
4318 v->next_iv = bl->giv;
4319 bl->giv = v;
4320 bl->giv_count++;
4321 v->benefit = rtx_cost (SET_SRC (set), SET);
4322 bl->total_benefit += v->benefit;
4323
4324 /* Now replace the biv with DEST_REG in all insns between
4325 the replaced increment and the next increment, and
4326 remember the last insn that needed a replacement. */
4327 for (last_use_insn = v->insn, p = NEXT_INSN (v->insn);
4328 p != next->insn;
4329 p = next_insn_in_loop (p, scan_start, end, loop_top))
4330 {
4331 rtx note;
4332
4333 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
4334 continue;
4335 if (reg_mentioned_p (old_reg, PATTERN (p)))
4336 {
4337 last_use_insn = p;
4338 if (! validate_replace_rtx (old_reg, dest_reg, p))
4339 abort ();
4340 }
4341 for (note = REG_NOTES (p); note; note = XEXP (note, 1))
4342 {
4343 if (GET_CODE (note) == EXPR_LIST)
4344 XEXP (note, 0)
4345 = replace_rtx (XEXP (note, 0), old_reg, dest_reg);
4346 }
4347 }
4348
4349 v->last_use = last_use_insn;
4350 v->lifetime = INSN_LUID (v->insn) - INSN_LUID (last_use_insn);
4351 /* If the lifetime is zero, it means that this register is really
4352 a dead store. So mark this as a giv that can be ignored.
4353 This will not prevent the biv from being eliminated. */
4354 if (v->lifetime == 0)
4355 v->ignore = 1;
1ccf8937
R
4356
4357 if (loop_dump_stream)
4358 fprintf (loop_dump_stream,
4359 "Increment %d of biv %d converted to giv %d.\n\n",
4360 INSN_UID (v->insn), old_regno, new_regno);
3ec2b590
R
4361 }
4362 }
4363 }
4364 last_increment_giv = max_reg_num () - 1;
4365
b4ad7b23
RS
4366 /* Search the loop for general induction variables. */
4367
4368 /* A register is a giv if: it is only set once, it is a function of a
4369 biv and a constant (or invariant), and it is not a biv. */
4370
4371 not_every_iteration = 0;
5ea7a4ae 4372 loop_depth = 0;
b4ad7b23
RS
4373 p = scan_start;
4374 while (1)
4375 {
4376 p = NEXT_INSN (p);
4377 /* At end of a straight-in loop, we are done.
4378 At end of a loop entered at the bottom, scan the top. */
4379 if (p == scan_start)
4380 break;
4381 if (p == end)
4382 {
4383 if (loop_top != 0)
f67ff5de 4384 p = loop_top;
b4ad7b23
RS
4385 else
4386 break;
4387 if (p == scan_start)
4388 break;
4389 }
4390
4391 /* Look for a general induction variable in a register. */
4392 if (GET_CODE (p) == INSN
4393 && (set = single_set (p))
4394 && GET_CODE (SET_DEST (set)) == REG
8deb8e2c 4395 && ! VARRAY_CHAR (may_not_optimize, REGNO (SET_DEST (set))))
b4ad7b23
RS
4396 {
4397 rtx src_reg;
4398 rtx add_val;
4399 rtx mult_val;
4400 int benefit;
4401 rtx regnote = 0;
a07516d3 4402 rtx last_consec_insn;
b4ad7b23
RS
4403
4404 dest_reg = SET_DEST (set);
4405 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
4406 continue;
4407
4408 if (/* SET_SRC is a giv. */
45f97e2e
RH
4409 (general_induction_var (SET_SRC (set), &src_reg, &add_val,
4410 &mult_val, 0, &benefit)
0f41302f 4411 /* Equivalent expression is a giv. */
5fd8383e 4412 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
45f97e2e
RH
4413 && general_induction_var (XEXP (regnote, 0), &src_reg,
4414 &add_val, &mult_val, 0,
4415 &benefit)))
b4ad7b23
RS
4416 /* Don't try to handle any regs made by loop optimization.
4417 We have nothing on them in regno_first_uid, etc. */
4418 && REGNO (dest_reg) < max_reg_before_loop
4419 /* Don't recognize a BASIC_INDUCT_VAR here. */
4420 && dest_reg != src_reg
4421 /* This must be the only place where the register is set. */
8deb8e2c 4422 && (VARRAY_INT (n_times_set, REGNO (dest_reg)) == 1
0f41302f 4423 /* or all sets must be consecutive and make a giv. */
b4ad7b23
RS
4424 || (benefit = consec_sets_giv (benefit, p,
4425 src_reg, dest_reg,
a07516d3
R
4426 &add_val, &mult_val,
4427 &last_consec_insn))))
b4ad7b23 4428 {
b4ad7b23
RS
4429 struct induction *v
4430 = (struct induction *) alloca (sizeof (struct induction));
b4ad7b23
RS
4431
4432 /* If this is a library call, increase benefit. */
5fd8383e 4433 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
4434 benefit += libcall_benefit (p);
4435
4436 /* Skip the consecutive insns, if there are any. */
a07516d3
R
4437 if (VARRAY_INT (n_times_set, REGNO (dest_reg)) != 1)
4438 p = last_consec_insn;
b4ad7b23
RS
4439
4440 record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
5fd8383e 4441 DEST_REG, not_every_iteration, NULL_PTR, loop_start,
b4ad7b23
RS
4442 loop_end);
4443
4444 }
4445 }
4446
4447#ifndef DONT_REDUCE_ADDR
4448 /* Look for givs which are memory addresses. */
4449 /* This resulted in worse code on a VAX 8600. I wonder if it
4450 still does. */
4451 if (GET_CODE (p) == INSN)
4452 find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
4453 loop_end);
4454#endif
4455
4456 /* Update the status of whether giv can derive other givs. This can
4457 change when we pass a label or an insn that updates a biv. */
7dcd3836
RK
4458 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4459 || GET_CODE (p) == CODE_LABEL)
b4ad7b23
RS
4460 update_giv_derive (p);
4461
8516af93
JW
4462 /* Past a jump, we get to insns for which we can't count
4463 on whether they will be executed during each iteration. */
4464 /* This code appears twice in strength_reduce. There is also similar
4465 code in scan_loop. */
4466 if (GET_CODE (p) == JUMP_INSN
4467 /* If we enter the loop in the middle, and scan around to the
4468 beginning, don't set not_every_iteration for that.
b4ad7b23
RS
4469 This can be any kind of jump, since we want to know if insns
4470 will be executed if the loop is executed. */
8516af93 4471 && ! (JUMP_LABEL (p) == loop_top
b4ad7b23
RS
4472 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
4473 || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
8516af93
JW
4474 {
4475 rtx label = 0;
4476
4477 /* If this is a jump outside the loop, then it also doesn't
4478 matter. Check to see if the target of this branch is on the
4479 loop_number_exits_labels list. */
4480
4481 for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
4482 label;
4483 label = LABEL_NEXTREF (label))
4484 if (XEXP (label, 0) == JUMP_LABEL (p))
4485 break;
4486
4487 if (! label)
4488 not_every_iteration = 1;
4489 }
b4ad7b23 4490
5ea7a4ae
JW
4491 else if (GET_CODE (p) == NOTE)
4492 {
4493 /* At the virtual top of a converted loop, insns are again known to
4494 be executed each iteration: logically, the loop begins here
5f3db57e
JL
4495 even though the exit code has been duplicated.
4496
4497 Insns are also again known to be executed each iteration at
4498 the LOOP_CONT note. */
4499 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4500 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4501 && loop_depth == 0)
5ea7a4ae
JW
4502 not_every_iteration = 0;
4503 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4504 loop_depth++;
4505 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4506 loop_depth--;
4507 }
b4ad7b23
RS
4508
4509 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4510 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4511 or not an insn is known to be executed each iteration of the
4512 loop, whether or not any iterations are known to occur.
4513
4514 Therefore, if we have just passed a label and have no more labels
4515 between here and the test insn of the loop, we know these insns
4516 will be executed each iteration. */
4517
4518 if (not_every_iteration && GET_CODE (p) == CODE_LABEL
6dd49eb4 4519 && no_labels_between_p (p, loop_end)
1cb1fe66 4520 && loop_insn_first_p (p, loop_cont))
b4ad7b23
RS
4521 not_every_iteration = 0;
4522 }
4523
4524 /* Try to calculate and save the number of loop iterations. This is
4525 set to zero if the actual number can not be calculated. This must
4526 be called after all giv's have been identified, since otherwise it may
4527 fail if the iteration variable is a giv. */
4528
302670f3 4529 loop_iterations (loop_start, loop_end, loop_info);
b4ad7b23
RS
4530
4531 /* Now for each giv for which we still don't know whether or not it is
4532 replaceable, check to see if it is replaceable because its final value
4533 can be calculated. This must be done after loop_iterations is called,
4534 so that final_giv_value will work correctly. */
4535
4536 for (bl = loop_iv_list; bl; bl = bl->next)
4537 {
4538 struct induction *v;
4539
4540 for (v = bl->giv; v; v = v->next_iv)
4541 if (! v->replaceable && ! v->not_replaceable)
302670f3 4542 check_final_value (v, loop_start, loop_end, loop_info->n_iterations);
b4ad7b23
RS
4543 }
4544
4545 /* Try to prove that the loop counter variable (if any) is always
4546 nonnegative; if so, record that fact with a REG_NONNEG note
4547 so that "decrement and branch until zero" insn can be used. */
5629b16c 4548 check_dbra_loop (loop_end, insn_count, loop_start, loop_info);
b4ad7b23 4549
97ec0ad8
R
4550 /* Create reg_map to hold substitutions for replaceable giv regs.
4551 Some givs might have been made from biv increments, so look at
4552 reg_iv_type for a suitable size. */
4553 reg_map_size = reg_iv_type->num_elements;
4554 reg_map = (rtx *) alloca (reg_map_size * sizeof (rtx));
4555 bzero ((char *) reg_map, reg_map_size * sizeof (rtx));
b4ad7b23
RS
4556
4557 /* Examine each iv class for feasibility of strength reduction/induction
4558 variable elimination. */
4559
4560 for (bl = loop_iv_list; bl; bl = bl->next)
4561 {
4562 struct induction *v;
4563 int benefit;
4564 int all_reduced;
4565 rtx final_value = 0;
f56246be 4566 unsigned nregs;
b4ad7b23
RS
4567
4568 /* Test whether it will be possible to eliminate this biv
4569 provided all givs are reduced. This is possible if either
4570 the reg is not used outside the loop, or we can compute
4571 what its final value will be.
4572
4573 For architectures with a decrement_and_branch_until_zero insn,
4574 don't do this if we put a REG_NONNEG note on the endtest for
4575 this biv. */
4576
4577 /* Compare against bl->init_insn rather than loop_start.
4578 We aren't concerned with any uses of the biv between
4579 init_insn and loop_start since these won't be affected
4580 by the value of the biv elsewhere in the function, so
4581 long as init_insn doesn't use the biv itself.
4582 March 14, 1989 -- self@bayes.arc.nasa.gov */
4583
b1f21e0a 4584 if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
b4ad7b23
RS
4585 && bl->init_insn
4586 && INSN_UID (bl->init_insn) < max_uid_for_loop
b1f21e0a 4587 && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
b4ad7b23
RS
4588#ifdef HAVE_decrement_and_branch_until_zero
4589 && ! bl->nonneg
4590#endif
4591 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
302670f3
MH
4592 || ((final_value = final_biv_value (bl, loop_start, loop_end,
4593 loop_info->n_iterations))
b4ad7b23
RS
4594#ifdef HAVE_decrement_and_branch_until_zero
4595 && ! bl->nonneg
4596#endif
4597 ))
4598 bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
4599 threshold, insn_count);
4600 else
4601 {
4602 if (loop_dump_stream)
4603 {
4604 fprintf (loop_dump_stream,
4605 "Cannot eliminate biv %d.\n",
4606 bl->regno);
4607 fprintf (loop_dump_stream,
4608 "First use: insn %d, last use: insn %d.\n",
b1f21e0a
MM
4609 REGNO_FIRST_UID (bl->regno),
4610 REGNO_LAST_UID (bl->regno));
b4ad7b23
RS
4611 }
4612 }
4613
4614 /* Combine all giv's for this iv_class. */
4615 combine_givs (bl);
4616
4617 /* This will be true at the end, if all givs which depend on this
4618 biv have been strength reduced.
4619 We can't (currently) eliminate the biv unless this is so. */
4620 all_reduced = 1;
4621
4622 /* Check each giv in this class to see if we will benefit by reducing
4623 it. Skip giv's combined with others. */
4624 for (v = bl->giv; v; v = v->next_iv)
4625 {
4626 struct induction *tv;
4627
4628 if (v->ignore || v->same)
4629 continue;
4630
4631 benefit = v->benefit;
4632
4633 /* Reduce benefit if not replaceable, since we will insert
4634 a move-insn to replace the insn that calculates this giv.
4635 Don't do this unless the giv is a user variable, since it
4636 will often be marked non-replaceable because of the duplication
4637 of the exit code outside the loop. In such a case, the copies
4638 we insert are dead and will be deleted. So they don't have
4639 a cost. Similar situations exist. */
4640 /* ??? The new final_[bg]iv_value code does a much better job
4641 of finding replaceable giv's, and hence this code may no longer
4642 be necessary. */
4643 if (! v->replaceable && ! bl->eliminable
4644 && REG_USERVAR_P (v->dest_reg))
4645 benefit -= copy_cost;
4646
4647 /* Decrease the benefit to count the add-insns that we will
4648 insert to increment the reduced reg for the giv. */
4649 benefit -= add_cost * bl->biv_count;
4650
4651 /* Decide whether to strength-reduce this giv or to leave the code
4652 unchanged (recompute it from the biv each time it is used).
4653 This decision can be made independently for each giv. */
4654
ab162578
JL
4655#ifdef AUTO_INC_DEC
4656 /* Attempt to guess whether autoincrement will handle some of the
4657 new add insns; if so, increase BENEFIT (undo the subtraction of
4658 add_cost that was done above). */
4659 if (v->giv_type == DEST_ADDR
4660 && GET_CODE (v->mult_val) == CONST_INT)
4661 {
940da324
JL
4662 if (HAVE_POST_INCREMENT
4663 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
ab162578 4664 benefit += add_cost * bl->biv_count;
940da324
JL
4665 else if (HAVE_PRE_INCREMENT
4666 && INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4667 benefit += add_cost * bl->biv_count;
4668 else if (HAVE_POST_DECREMENT
4669 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
4670 benefit += add_cost * bl->biv_count;
4671 else if (HAVE_PRE_DECREMENT
4672 && -INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
ab162578 4673 benefit += add_cost * bl->biv_count;
ab162578
JL
4674 }
4675#endif
b4ad7b23
RS
4676
4677 /* If an insn is not to be strength reduced, then set its ignore
4678 flag, and clear all_reduced. */
4679
e6f6eb29
JW
4680 /* A giv that depends on a reversed biv must be reduced if it is
4681 used after the loop exit, otherwise, it would have the wrong
4682 value after the loop exit. To make it simple, just reduce all
4683 of such giv's whether or not we know they are used after the loop
4684 exit. */
4685
e5eb27e5
JL
4686 if ( ! flag_reduce_all_givs && v->lifetime * threshold * benefit < insn_count
4687 && ! bl->reversed )
b4ad7b23
RS
4688 {
4689 if (loop_dump_stream)
4690 fprintf (loop_dump_stream,
4691 "giv of insn %d not worth while, %d vs %d.\n",
4692 INSN_UID (v->insn),
4693 v->lifetime * threshold * benefit, insn_count);
4694 v->ignore = 1;
4695 all_reduced = 0;
4696 }
4697 else
4698 {
4699 /* Check that we can increment the reduced giv without a
4700 multiply insn. If not, reject it. */
4701
4702 for (tv = bl->biv; tv; tv = tv->next_iv)
4703 if (tv->mult_val == const1_rtx
4704 && ! product_cheap_p (tv->add_val, v->mult_val))
4705 {
4706 if (loop_dump_stream)
4707 fprintf (loop_dump_stream,
4708 "giv of insn %d: would need a multiply.\n",
4709 INSN_UID (v->insn));
4710 v->ignore = 1;
4711 all_reduced = 0;
4712 break;
4713 }
4714 }
4715 }
4716
8c354a41
R
4717 /* Check for givs whose first use is their definition and whose
4718 last use is the definition of another giv. If so, it is likely
4719 dead and should not be used to derive another giv nor to
4720 eliminate a biv. */
4721 for (v = bl->giv; v; v = v->next_iv)
4722 {
4723 if (v->ignore
4724 || (v->same && v->same->ignore))
4725 continue;
4726
4727 if (v->last_use)
4728 {
4729 struct induction *v1;
4730
4731 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4732 if (v->last_use == v1->insn)
4733 v->maybe_dead = 1;
4734 }
4735 else if (v->giv_type == DEST_REG
4736 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4737 {
4738 struct induction *v1;
4739
4740 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4741 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4742 v->maybe_dead = 1;
4743 }
4744 }
4745
3ec2b590 4746 /* Now that we know which givs will be reduced, try to rearrange the
f56246be
R
4747 combinations to reduce register pressure.
4748 recombine_givs calls find_life_end, which needs reg_iv_type and
4749 reg_iv_info to be valid for all pseudos. We do the necessary
4750 reallocation here since it allows to check if there are still
4751 more bivs to process. */
4752 nregs = max_reg_num ();
4753 if (nregs > reg_iv_type->num_elements)
4754 {
4755 /* If there are still more bivs to process, allocate some slack
4756 space so that we're not constantly reallocating these arrays. */
4757 if (bl->next)
4758 nregs += nregs / 4;
4759 /* Reallocate reg_iv_type and reg_iv_info. */
4760 VARRAY_GROW (reg_iv_type, nregs);
4761 VARRAY_GROW (reg_iv_info, nregs);
4762 }
53dc05e4 4763 recombine_givs (bl, loop_start, loop_end, unroll_p);
3ec2b590 4764
b4ad7b23
RS
4765 /* Reduce each giv that we decided to reduce. */
4766
4767 for (v = bl->giv; v; v = v->next_iv)
4768 {
4769 struct induction *tv;
4770 if (! v->ignore && v->same == 0)
4771 {
8516af93
JW
4772 int auto_inc_opt = 0;
4773
743f9f5d
R
4774 /* If the code for derived givs immediately below has already
4775 allocated a new_reg, we must keep it. */
4776 if (! v->new_reg)
4777 v->new_reg = gen_reg_rtx (v->mode);
b4ad7b23 4778
4d87f7a7 4779 if (v->derived_from)
3ec2b590 4780 {
743f9f5d
R
4781 struct induction *d = v->derived_from;
4782
4783 /* In case d->dest_reg is not replaceable, we have
4784 to replace it in v->insn now. */
4785 if (! d->new_reg)
4786 d->new_reg = gen_reg_rtx (d->mode);
4787 PATTERN (v->insn)
4788 = replace_rtx (PATTERN (v->insn), d->dest_reg, d->new_reg);
3ec2b590
R
4789 PATTERN (v->insn)
4790 = replace_rtx (PATTERN (v->insn), v->dest_reg, v->new_reg);
1b786838
R
4791 /* For each place where the biv is incremented, add an
4792 insn to set the new, reduced reg for the giv.
4793 We used to do this only for biv_count != 1, but
4794 this fails when there is a giv after a single biv
4795 increment, e.g. when the last giv was expressed as
4796 pre-decrement. */
4797 for (tv = bl->biv; tv; tv = tv->next_iv)
3ec2b590 4798 {
1b786838
R
4799 /* We always emit reduced giv increments before the
4800 biv increment when bl->biv_count != 1. So by
4801 emitting the add insns for derived givs after the
4802 biv increment, they pick up the updated value of
4803 the reduced giv.
4804 If the reduced giv is processed with
4805 auto_inc_opt == 1, then it is incremented earlier
4806 than the biv, hence we'll still pick up the right
4807 value.
4808 If it's processed with auto_inc_opt == -1,
4809 that implies that the biv increment is before the
4810 first reduced giv's use. The derived giv's lifetime
4811 is after the reduced giv's lifetime, hence in this
4812 case, the biv increment doesn't matter. */
4813 emit_insn_after (copy_rtx (PATTERN (v->insn)), tv->insn);
3ec2b590
R
4814 }
4815 continue;
4816 }
4817
8516af93
JW
4818#ifdef AUTO_INC_DEC
4819 /* If the target has auto-increment addressing modes, and
4820 this is an address giv, then try to put the increment
4821 immediately after its use, so that flow can create an
4822 auto-increment addressing mode. */
4823 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
085daa5a
JW
4824 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4825 /* We don't handle reversed biv's because bl->biv->insn
4826 does not have a valid INSN_LUID. */
4827 && ! bl->reversed
f5963e61
JL
4828 && v->always_executed && ! v->maybe_multiple
4829 && INSN_UID (v->insn) < max_uid_for_loop)
8516af93
JW
4830 {
4831 /* If other giv's have been combined with this one, then
4832 this will work only if all uses of the other giv's occur
4833 before this giv's insn. This is difficult to check.
4834
4835 We simplify this by looking for the common case where
4836 there is one DEST_REG giv, and this giv's insn is the
4837 last use of the dest_reg of that DEST_REG giv. If the
38e01259 4838 increment occurs after the address giv, then we can
8516af93
JW
4839 perform the optimization. (Otherwise, the increment
4840 would have to go before other_giv, and we would not be
4841 able to combine it with the address giv to get an
4842 auto-inc address.) */
4843 if (v->combined_with)
4844 {
4845 struct induction *other_giv = 0;
4846
4847 for (tv = bl->giv; tv; tv = tv->next_iv)
4848 if (tv->same == v)
4849 {
4850 if (other_giv)
4851 break;
4852 else
4853 other_giv = tv;
4854 }
4855 if (! tv && other_giv
43243872 4856 && REGNO (other_giv->dest_reg) < max_reg_before_loop
b1f21e0a 4857 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
8516af93
JW
4858 == INSN_UID (v->insn))
4859 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4860 auto_inc_opt = 1;
4861 }
38e01259 4862 /* Check for case where increment is before the address
72b0c616
RK
4863 giv. Do this test in "loop order". */
4864 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4865 && (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4866 || (INSN_LUID (bl->biv->insn)
4867 > INSN_LUID (scan_start))))
4868 || (INSN_LUID (v->insn) < INSN_LUID (scan_start)
4869 && (INSN_LUID (scan_start)
4870 < INSN_LUID (bl->biv->insn))))
8516af93
JW
4871 auto_inc_opt = -1;
4872 else
4873 auto_inc_opt = 1;
4874
bb91b814 4875#ifdef HAVE_cc0
a7a4457e
DE
4876 {
4877 rtx prev;
4878
4879 /* We can't put an insn immediately after one setting
4880 cc0, or immediately before one using cc0. */
4881 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4882 || (auto_inc_opt == -1
4883 && (prev = prev_nonnote_insn (v->insn)) != 0
4884 && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4885 && sets_cc0_p (PATTERN (prev))))
4886 auto_inc_opt = 0;
4887 }
bb91b814
JW
4888#endif
4889
8516af93
JW
4890 if (auto_inc_opt)
4891 v->auto_inc_opt = 1;
4892 }
4893#endif
4894
4895 /* For each place where the biv is incremented, add an insn
4896 to increment the new, reduced reg for the giv. */
b4ad7b23
RS
4897 for (tv = bl->biv; tv; tv = tv->next_iv)
4898 {
8516af93
JW
4899 rtx insert_before;
4900
4901 if (! auto_inc_opt)
4902 insert_before = tv->insn;
4903 else if (auto_inc_opt == 1)
4904 insert_before = NEXT_INSN (v->insn);
4905 else
4906 insert_before = v->insn;
4907
b4ad7b23
RS
4908 if (tv->mult_val == const1_rtx)
4909 emit_iv_add_mult (tv->add_val, v->mult_val,
8516af93 4910 v->new_reg, v->new_reg, insert_before);
b4ad7b23
RS
4911 else /* tv->mult_val == const0_rtx */
4912 /* A multiply is acceptable here
4913 since this is presumed to be seldom executed. */
4914 emit_iv_add_mult (tv->add_val, v->mult_val,
8516af93 4915 v->add_val, v->new_reg, insert_before);
b4ad7b23
RS
4916 }
4917
4918 /* Add code at loop start to initialize giv's reduced reg. */
4919
4920 emit_iv_add_mult (bl->initial_value, v->mult_val,
4921 v->add_val, v->new_reg, loop_start);
4922 }
4923 }
4924
4925 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
4926 as not reduced.
4927
4928 For each giv register that can be reduced now: if replaceable,
4929 substitute reduced reg wherever the old giv occurs;
8c354a41 4930 else add new move insn "giv_reg = reduced_reg". */
b4ad7b23 4931
b4ad7b23
RS
4932 for (v = bl->giv; v; v = v->next_iv)
4933 {
4934 if (v->same && v->same->ignore)
4935 v->ignore = 1;
4936
4937 if (v->ignore)
4938 continue;
4939
b4ad7b23
RS
4940 /* Update expression if this was combined, in case other giv was
4941 replaced. */
4942 if (v->same)
4943 v->new_reg = replace_rtx (v->new_reg,
4944 v->same->dest_reg, v->same->new_reg);
4945
4946 if (v->giv_type == DEST_ADDR)
4947 /* Store reduced reg as the address in the memref where we found
4948 this giv. */
9abdca9c 4949 validate_change (v->insn, v->location, v->new_reg, 0);
b4ad7b23
RS
4950 else if (v->replaceable)
4951 {
4952 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4953
4954#if 0
4955 /* I can no longer duplicate the original problem. Perhaps
4956 this is unnecessary now? */
4957
4958 /* Replaceable; it isn't strictly necessary to delete the old
4959 insn and emit a new one, because v->dest_reg is now dead.
4960
4961 However, especially when unrolling loops, the special
4962 handling for (set REG0 REG1) in the second cse pass may
4963 make v->dest_reg live again. To avoid this problem, emit
4964 an insn to set the original giv reg from the reduced giv.
4965 We can not delete the original insn, since it may be part
4966 of a LIBCALL, and the code in flow that eliminates dead
4967 libcalls will fail if it is deleted. */
4968 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4969 v->insn);
4970#endif
4971 }
4972 else
4973 {
4974 /* Not replaceable; emit an insn to set the original giv reg from
4975 the reduced giv, same as above. */
4976 emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4977 v->insn);
4978 }
4979
4980 /* When a loop is reversed, givs which depend on the reversed
4981 biv, and which are live outside the loop, must be set to their
4982 correct final value. This insn is only needed if the giv is
4983 not replaceable. The correct final value is the same as the
4984 value that the giv starts the reversed loop with. */
4985 if (bl->reversed && ! v->replaceable)
4986 emit_iv_add_mult (bl->initial_value, v->mult_val,
4987 v->add_val, v->dest_reg, end_insert_before);
4988 else if (v->final_value)
4989 {
4990 rtx insert_before;
4991
4992 /* If the loop has multiple exits, emit the insn before the
4993 loop to ensure that it will always be executed no matter
4994 how the loop exits. Otherwise, emit the insn after the loop,
4995 since this is slightly more efficient. */
353127c2 4996 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
b4ad7b23
RS
4997 insert_before = loop_start;
4998 else
4999 insert_before = end_insert_before;
5000 emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
5001 insert_before);
5002
5003#if 0
5004 /* If the insn to set the final value of the giv was emitted
5005 before the loop, then we must delete the insn inside the loop
5006 that sets it. If this is a LIBCALL, then we must delete
5007 every insn in the libcall. Note, however, that
5008 final_giv_value will only succeed when there are multiple
5009 exits if the giv is dead at each exit, hence it does not
5010 matter that the original insn remains because it is dead
5011 anyways. */
5012 /* Delete the insn inside the loop that sets the giv since
5013 the giv is now set before (or after) the loop. */
5014 delete_insn (v->insn);
5015#endif
5016 }
5017
5018 if (loop_dump_stream)
5019 {
5020 fprintf (loop_dump_stream, "giv at %d reduced to ",
5021 INSN_UID (v->insn));
5022 print_rtl (loop_dump_stream, v->new_reg);
5023 fprintf (loop_dump_stream, "\n");
5024 }
5025 }
5026
5027 /* All the givs based on the biv bl have been reduced if they
5028 merit it. */
5029
5030 /* For each giv not marked as maybe dead that has been combined with a
5031 second giv, clear any "maybe dead" mark on that second giv.
5032 v->new_reg will either be or refer to the register of the giv it
5033 combined with.
5034
5035 Doing this clearing avoids problems in biv elimination where a
5036 giv's new_reg is a complex value that can't be put in the insn but
5037 the giv combined with (with a reg as new_reg) is marked maybe_dead.
5038 Since the register will be used in either case, we'd prefer it be
5039 used from the simpler giv. */
5040
5041 for (v = bl->giv; v; v = v->next_iv)
5042 if (! v->maybe_dead && v->same)
5043 v->same->maybe_dead = 0;
5044
5045 /* Try to eliminate the biv, if it is a candidate.
5046 This won't work if ! all_reduced,
5047 since the givs we planned to use might not have been reduced.
5048
d45cf215 5049 We have to be careful that we didn't initially think we could eliminate
b4ad7b23
RS
5050 this biv because of a giv that we now think may be dead and shouldn't
5051 be used as a biv replacement.
5052
5053 Also, there is the possibility that we may have a giv that looks
5054 like it can be used to eliminate a biv, but the resulting insn
5055 isn't valid. This can happen, for example, on the 88k, where a
5056 JUMP_INSN can compare a register only with zero. Attempts to
c5b7917e 5057 replace it with a compare with a constant will fail.
b4ad7b23
RS
5058
5059 Note that in cases where this call fails, we may have replaced some
5060 of the occurrences of the biv with a giv, but no harm was done in
5061 doing so in the rare cases where it can occur. */
5062
5063 if (all_reduced == 1 && bl->eliminable
5064 && maybe_eliminate_biv (bl, loop_start, end, 1,
5065 threshold, insn_count))
5066
5067 {
5068 /* ?? If we created a new test to bypass the loop entirely,
5069 or otherwise drop straight in, based on this test, then
5070 we might want to rewrite it also. This way some later
5071 pass has more hope of removing the initialization of this
0f41302f 5072 biv entirely. */
b4ad7b23
RS
5073
5074 /* If final_value != 0, then the biv may be used after loop end
5075 and we must emit an insn to set it just in case.
5076
5077 Reversed bivs already have an insn after the loop setting their
5078 value, so we don't need another one. We can't calculate the
0f41302f 5079 proper final value for such a biv here anyways. */
b4ad7b23
RS
5080 if (final_value != 0 && ! bl->reversed)
5081 {
5082 rtx insert_before;
5083
5084 /* If the loop has multiple exits, emit the insn before the
5085 loop to ensure that it will always be executed no matter
5086 how the loop exits. Otherwise, emit the insn after the
5087 loop, since this is slightly more efficient. */
353127c2 5088 if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
b4ad7b23
RS
5089 insert_before = loop_start;
5090 else
5091 insert_before = end_insert_before;
5092
5093 emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
5094 end_insert_before);
5095 }
5096
5097#if 0
5098 /* Delete all of the instructions inside the loop which set
5099 the biv, as they are all dead. If is safe to delete them,
5100 because an insn setting a biv will never be part of a libcall. */
5101 /* However, deleting them will invalidate the regno_last_uid info,
5102 so keeping them around is more convenient. Final_biv_value
5103 will only succeed when there are multiple exits if the biv
5104 is dead at each exit, hence it does not matter that the original
5105 insn remains, because it is dead anyways. */
5106 for (v = bl->biv; v; v = v->next_iv)
5107 delete_insn (v->insn);
5108#endif
5109
5110 if (loop_dump_stream)
5111 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5112 bl->regno);
5113 }
5114 }
5115
5116 /* Go through all the instructions in the loop, making all the
5117 register substitutions scheduled in REG_MAP. */
5118
5119 for (p = loop_start; p != end; p = NEXT_INSN (p))
5120 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5121 || GET_CODE (p) == CALL_INSN)
5122 {
97ec0ad8
R
5123 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5124 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
da0c128e 5125 INSN_CODE (p) = -1;
b4ad7b23
RS
5126 }
5127
5128 /* Unroll loops from within strength reduction so that we can use the
5129 induction variable information that strength_reduce has already
5130 collected. */
5131
81797aba 5132 if (unroll_p)
302670f3
MH
5133 unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
5134 loop_info, 1);
b4ad7b23 5135
8c660648 5136#ifdef HAVE_decrement_and_branch_on_count
cac8ce95
DE
5137 /* Instrument the loop with BCT insn. */
5138 if (HAVE_decrement_and_branch_on_count && bct_p
5139 && flag_branch_on_count_reg)
302670f3 5140 insert_bct (loop_start, loop_end, loop_info);
cac8ce95 5141#endif /* HAVE_decrement_and_branch_on_count */
8c660648 5142
b4ad7b23
RS
5143 if (loop_dump_stream)
5144 fprintf (loop_dump_stream, "\n");
3ec2b590
R
5145 VARRAY_FREE (reg_iv_type);
5146 VARRAY_FREE (reg_iv_info);
b4ad7b23
RS
5147}
5148\f
5149/* Return 1 if X is a valid source for an initial value (or as value being
5150 compared against in an initial test).
5151
5152 X must be either a register or constant and must not be clobbered between
5153 the current insn and the start of the loop.
5154
5155 INSN is the insn containing X. */
5156
5157static int
5158valid_initial_value_p (x, insn, call_seen, loop_start)
5159 rtx x;
5160 rtx insn;
5161 int call_seen;
5162 rtx loop_start;
5163{
5164 if (CONSTANT_P (x))
5165 return 1;
5166
d45cf215 5167 /* Only consider pseudos we know about initialized in insns whose luids
b4ad7b23
RS
5168 we know. */
5169 if (GET_CODE (x) != REG
5170 || REGNO (x) >= max_reg_before_loop)
5171 return 0;
5172
5173 /* Don't use call-clobbered registers across a call which clobbers it. On
5174 some machines, don't use any hard registers at all. */
5175 if (REGNO (x) < FIRST_PSEUDO_REGISTER
e9a25f70
JL
5176 && (SMALL_REGISTER_CLASSES
5177 || (call_used_regs[REGNO (x)] && call_seen)))
b4ad7b23
RS
5178 return 0;
5179
5180 /* Don't use registers that have been clobbered before the start of the
5181 loop. */
5182 if (reg_set_between_p (x, insn, loop_start))
5183 return 0;
5184
5185 return 1;
5186}
5187\f
5188/* Scan X for memory refs and check each memory address
5189 as a possible giv. INSN is the insn whose pattern X comes from.
5190 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5191 every loop iteration. */
5192
5193static void
5194find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
5195 rtx x;
5196 rtx insn;
5197 int not_every_iteration;
5198 rtx loop_start, loop_end;
5199{
5200 register int i, j;
5201 register enum rtx_code code;
5202 register char *fmt;
5203
5204 if (x == 0)
5205 return;
5206
5207 code = GET_CODE (x);
5208 switch (code)
5209 {
5210 case REG:
5211 case CONST_INT:
5212 case CONST:
5213 case CONST_DOUBLE:
5214 case SYMBOL_REF:
5215 case LABEL_REF:
5216 case PC:
5217 case CC0:
5218 case ADDR_VEC:
5219 case ADDR_DIFF_VEC:
5220 case USE:
5221 case CLOBBER:
5222 return;
5223
5224 case MEM:
5225 {
5226 rtx src_reg;
5227 rtx add_val;
5228 rtx mult_val;
5229 int benefit;
5230
45f97e2e
RH
5231 /* This code used to disable creating GIVs with mult_val == 1 and
5232 add_val == 0. However, this leads to lost optimizations when
5233 it comes time to combine a set of related DEST_ADDR GIVs, since
5234 this one would not be seen. */
b4ad7b23 5235
45f97e2e
RH
5236 if (general_induction_var (XEXP (x, 0), &src_reg, &add_val,
5237 &mult_val, 1, &benefit))
b4ad7b23
RS
5238 {
5239 /* Found one; record it. */
5240 struct induction *v
5241 = (struct induction *) oballoc (sizeof (struct induction));
5242
5243 record_giv (v, insn, src_reg, addr_placeholder, mult_val,
5244 add_val, benefit, DEST_ADDR, not_every_iteration,
5245 &XEXP (x, 0), loop_start, loop_end);
5246
5247 v->mem_mode = GET_MODE (x);
5248 }
b4ad7b23 5249 }
e9a25f70
JL
5250 return;
5251
5252 default:
5253 break;
b4ad7b23
RS
5254 }
5255
5256 /* Recursively scan the subexpressions for other mem refs. */
5257
5258 fmt = GET_RTX_FORMAT (code);
5259 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5260 if (fmt[i] == 'e')
5261 find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
5262 loop_end);
5263 else if (fmt[i] == 'E')
5264 for (j = 0; j < XVECLEN (x, i); j++)
5265 find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
5266 loop_start, loop_end);
5267}
5268\f
5269/* Fill in the data about one biv update.
5270 V is the `struct induction' in which we record the biv. (It is
5271 allocated by the caller, with alloca.)
5272 INSN is the insn that sets it.
5273 DEST_REG is the biv's reg.
5274
5275 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5276 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
7dcd3836
RK
5277 being set to INC_VAL.
5278
5279 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5280 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5281 can be executed more than once per iteration. If MAYBE_MULTIPLE
5282 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5283 executed exactly once per iteration. */
b4ad7b23
RS
5284
5285static void
3ec2b590 5286record_biv (v, insn, dest_reg, inc_val, mult_val, location,
7dcd3836 5287 not_every_iteration, maybe_multiple)
b4ad7b23
RS
5288 struct induction *v;
5289 rtx insn;
5290 rtx dest_reg;
5291 rtx inc_val;
5292 rtx mult_val;
3ec2b590 5293 rtx *location;
b4ad7b23 5294 int not_every_iteration;
7dcd3836 5295 int maybe_multiple;
b4ad7b23
RS
5296{
5297 struct iv_class *bl;
5298
5299 v->insn = insn;
5300 v->src_reg = dest_reg;
5301 v->dest_reg = dest_reg;
5302 v->mult_val = mult_val;
5303 v->add_val = inc_val;
3ec2b590 5304 v->location = location;
b4ad7b23
RS
5305 v->mode = GET_MODE (dest_reg);
5306 v->always_computable = ! not_every_iteration;
8516af93 5307 v->always_executed = ! not_every_iteration;
7dcd3836 5308 v->maybe_multiple = maybe_multiple;
b4ad7b23
RS
5309
5310 /* Add this to the reg's iv_class, creating a class
5311 if this is the first incrementation of the reg. */
5312
5313 bl = reg_biv_class[REGNO (dest_reg)];
5314 if (bl == 0)
5315 {
5316 /* Create and initialize new iv_class. */
5317
5318 bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
5319
5320 bl->regno = REGNO (dest_reg);
5321 bl->biv = 0;
5322 bl->giv = 0;
5323 bl->biv_count = 0;
5324 bl->giv_count = 0;
5325
5326 /* Set initial value to the reg itself. */
5327 bl->initial_value = dest_reg;
c5b7917e 5328 /* We haven't seen the initializing insn yet */
b4ad7b23
RS
5329 bl->init_insn = 0;
5330 bl->init_set = 0;
5331 bl->initial_test = 0;
5332 bl->incremented = 0;
5333 bl->eliminable = 0;
5334 bl->nonneg = 0;
5335 bl->reversed = 0;
b5d27be7 5336 bl->total_benefit = 0;
b4ad7b23
RS
5337
5338 /* Add this class to loop_iv_list. */
5339 bl->next = loop_iv_list;
5340 loop_iv_list = bl;
5341
5342 /* Put it in the array of biv register classes. */
5343 reg_biv_class[REGNO (dest_reg)] = bl;
5344 }
5345
5346 /* Update IV_CLASS entry for this biv. */
5347 v->next_iv = bl->biv;
5348 bl->biv = v;
5349 bl->biv_count++;
5350 if (mult_val == const1_rtx)
5351 bl->incremented = 1;
5352
5353 if (loop_dump_stream)
5354 {
5355 fprintf (loop_dump_stream,
5356 "Insn %d: possible biv, reg %d,",
5357 INSN_UID (insn), REGNO (dest_reg));
5358 if (GET_CODE (inc_val) == CONST_INT)
9ba7a303
JC
5359 {
5360 fprintf (loop_dump_stream, " const =");
5361 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (inc_val));
5362 fputc ('\n', loop_dump_stream);
5363 }
b4ad7b23
RS
5364 else
5365 {
5366 fprintf (loop_dump_stream, " const = ");
5367 print_rtl (loop_dump_stream, inc_val);
5368 fprintf (loop_dump_stream, "\n");
5369 }
5370 }
5371}
5372\f
5373/* Fill in the data about one giv.
5374 V is the `struct induction' in which we record the giv. (It is
5375 allocated by the caller, with alloca.)
5376 INSN is the insn that sets it.
5377 BENEFIT estimates the savings from deleting this insn.
5378 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5379 into a register or is used as a memory address.
5380
5381 SRC_REG is the biv reg which the giv is computed from.
5382 DEST_REG is the giv's reg (if the giv is stored in a reg).
5383 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5384 LOCATION points to the place where this giv's value appears in INSN. */
5385
5386static void
5387record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
5388 type, not_every_iteration, location, loop_start, loop_end)
5389 struct induction *v;
5390 rtx insn;
5391 rtx src_reg;
5392 rtx dest_reg;
5393 rtx mult_val, add_val;
5394 int benefit;
5395 enum g_types type;
5396 int not_every_iteration;
5397 rtx *location;
5398 rtx loop_start, loop_end;
5399{
5400 struct induction *b;
5401 struct iv_class *bl;
5402 rtx set = single_set (insn);
b4ad7b23
RS
5403
5404 v->insn = insn;
5405 v->src_reg = src_reg;
5406 v->giv_type = type;
5407 v->dest_reg = dest_reg;
5408 v->mult_val = mult_val;
5409 v->add_val = add_val;
5410 v->benefit = benefit;
5411 v->location = location;
5412 v->cant_derive = 0;
5413 v->combined_with = 0;
7dcd3836 5414 v->maybe_multiple = 0;
b4ad7b23
RS
5415 v->maybe_dead = 0;
5416 v->derive_adjustment = 0;
5417 v->same = 0;
5418 v->ignore = 0;
5419 v->new_reg = 0;
5420 v->final_value = 0;
f415f7be 5421 v->same_insn = 0;
8516af93 5422 v->auto_inc_opt = 0;
9ae8ffe7
JL
5423 v->unrolled = 0;
5424 v->shared = 0;
4d87f7a7 5425 v->derived_from = 0;
3ec2b590 5426 v->last_use = 0;
b4ad7b23
RS
5427
5428 /* The v->always_computable field is used in update_giv_derive, to
5429 determine whether a giv can be used to derive another giv. For a
5430 DEST_REG giv, INSN computes a new value for the giv, so its value
5431 isn't computable if INSN insn't executed every iteration.
5432 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5433 it does not compute a new value. Hence the value is always computable
d45cf215 5434 regardless of whether INSN is executed each iteration. */
b4ad7b23
RS
5435
5436 if (type == DEST_ADDR)
5437 v->always_computable = 1;
5438 else
5439 v->always_computable = ! not_every_iteration;
5440
8516af93
JW
5441 v->always_executed = ! not_every_iteration;
5442
b4ad7b23
RS
5443 if (type == DEST_ADDR)
5444 {
5445 v->mode = GET_MODE (*location);
5446 v->lifetime = 1;
b4ad7b23
RS
5447 }
5448 else /* type == DEST_REG */
5449 {
5450 v->mode = GET_MODE (SET_DEST (set));
5451
b1f21e0a
MM
5452 v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
5453 - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
b4ad7b23 5454
b4ad7b23
RS
5455 /* If the lifetime is zero, it means that this register is
5456 really a dead store. So mark this as a giv that can be
0f41302f 5457 ignored. This will not prevent the biv from being eliminated. */
b4ad7b23
RS
5458 if (v->lifetime == 0)
5459 v->ignore = 1;
5460
3ec2b590
R
5461 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
5462 REG_IV_INFO (REGNO (dest_reg)) = v;
b4ad7b23
RS
5463 }
5464
5465 /* Add the giv to the class of givs computed from one biv. */
5466
5467 bl = reg_biv_class[REGNO (src_reg)];
5468 if (bl)
5469 {
5470 v->next_iv = bl->giv;
5471 bl->giv = v;
5472 /* Don't count DEST_ADDR. This is supposed to count the number of
5473 insns that calculate givs. */
5474 if (type == DEST_REG)
5475 bl->giv_count++;
5476 bl->total_benefit += benefit;
5477 }
5478 else
5479 /* Fatal error, biv missing for this giv? */
5480 abort ();
5481
5482 if (type == DEST_ADDR)
5483 v->replaceable = 1;
5484 else
5485 {
5486 /* The giv can be replaced outright by the reduced register only if all
5487 of the following conditions are true:
5488 - the insn that sets the giv is always executed on any iteration
5489 on which the giv is used at all
5490 (there are two ways to deduce this:
5491 either the insn is executed on every iteration,
5492 or all uses follow that insn in the same basic block),
5493 - the giv is not used outside the loop
5494 - no assignments to the biv occur during the giv's lifetime. */
5495
b1f21e0a 5496 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
b4ad7b23 5497 /* Previous line always fails if INSN was moved by loop opt. */
b1f21e0a 5498 && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
b4ad7b23
RS
5499 && (! not_every_iteration
5500 || last_use_this_basic_block (dest_reg, insn)))
5501 {
5502 /* Now check that there are no assignments to the biv within the
5503 giv's lifetime. This requires two separate checks. */
5504
5505 /* Check each biv update, and fail if any are between the first
5506 and last use of the giv.
5507
5508 If this loop contains an inner loop that was unrolled, then
5509 the insn modifying the biv may have been emitted by the loop
5510 unrolling code, and hence does not have a valid luid. Just
5511 mark the biv as not replaceable in this case. It is not very
5512 useful as a biv, because it is used in two different loops.
5513 It is very unlikely that we would be able to optimize the giv
5514 using this biv anyways. */
5515
5516 v->replaceable = 1;
5517 for (b = bl->biv; b; b = b->next_iv)
5518 {
5519 if (INSN_UID (b->insn) >= max_uid_for_loop
5520 || ((uid_luid[INSN_UID (b->insn)]
b1f21e0a 5521 >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
b4ad7b23 5522 && (uid_luid[INSN_UID (b->insn)]
b1f21e0a 5523 <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
b4ad7b23
RS
5524 {
5525 v->replaceable = 0;
5526 v->not_replaceable = 1;
5527 break;
5528 }
5529 }
5530
5031afa7
JW
5531 /* If there are any backwards branches that go from after the
5532 biv update to before it, then this giv is not replaceable. */
b4ad7b23 5533 if (v->replaceable)
5031afa7
JW
5534 for (b = bl->biv; b; b = b->next_iv)
5535 if (back_branch_in_range_p (b->insn, loop_start, loop_end))
5536 {
5537 v->replaceable = 0;
5538 v->not_replaceable = 1;
5539 break;
5540 }
b4ad7b23
RS
5541 }
5542 else
5543 {
5544 /* May still be replaceable, we don't have enough info here to
5545 decide. */
5546 v->replaceable = 0;
5547 v->not_replaceable = 0;
5548 }
5549 }
5550
45f97e2e
RH
5551 /* Record whether the add_val contains a const_int, for later use by
5552 combine_givs. */
5553 {
5554 rtx tem = add_val;
5555
5556 v->no_const_addval = 1;
5557 if (tem == const0_rtx)
5558 ;
5559 else if (GET_CODE (tem) == CONST_INT)
5560 v->no_const_addval = 0;
5561 else if (GET_CODE (tem) == PLUS)
5562 {
5563 while (1)
5564 {
5565 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5566 tem = XEXP (tem, 0);
5567 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5568 tem = XEXP (tem, 1);
5569 else
5570 break;
5571 }
5572 if (GET_CODE (XEXP (tem, 1)) == CONST_INT)
5573 v->no_const_addval = 0;
5574 }
5575 }
5576
b4ad7b23
RS
5577 if (loop_dump_stream)
5578 {
5579 if (type == DEST_REG)
5580 fprintf (loop_dump_stream, "Insn %d: giv reg %d",
5581 INSN_UID (insn), REGNO (dest_reg));
5582 else
5583 fprintf (loop_dump_stream, "Insn %d: dest address",
5584 INSN_UID (insn));
5585
5586 fprintf (loop_dump_stream, " src reg %d benefit %d",
5587 REGNO (src_reg), v->benefit);
4b259e3f
R
5588 fprintf (loop_dump_stream, " lifetime %d",
5589 v->lifetime);
b4ad7b23
RS
5590
5591 if (v->replaceable)
5592 fprintf (loop_dump_stream, " replaceable");
5593
45f97e2e
RH
5594 if (v->no_const_addval)
5595 fprintf (loop_dump_stream, " ncav");
5596
b4ad7b23 5597 if (GET_CODE (mult_val) == CONST_INT)
9ba7a303
JC
5598 {
5599 fprintf (loop_dump_stream, " mult ");
5600 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (mult_val));
5601 }
b4ad7b23
RS
5602 else
5603 {
5604 fprintf (loop_dump_stream, " mult ");
5605 print_rtl (loop_dump_stream, mult_val);
5606 }
5607
5608 if (GET_CODE (add_val) == CONST_INT)
9ba7a303
JC
5609 {
5610 fprintf (loop_dump_stream, " add ");
5611 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (add_val));
5612 }
b4ad7b23
RS
5613 else
5614 {
5615 fprintf (loop_dump_stream, " add ");
5616 print_rtl (loop_dump_stream, add_val);
5617 }
5618 }
5619
5620 if (loop_dump_stream)
5621 fprintf (loop_dump_stream, "\n");
5622
5623}
5624
5625
5626/* All this does is determine whether a giv can be made replaceable because
5627 its final value can be calculated. This code can not be part of record_giv
5628 above, because final_giv_value requires that the number of loop iterations
5629 be known, and that can not be accurately calculated until after all givs
5630 have been identified. */
5631
5632static void
302670f3 5633check_final_value (v, loop_start, loop_end, n_iterations)
b4ad7b23
RS
5634 struct induction *v;
5635 rtx loop_start, loop_end;
302670f3 5636 unsigned HOST_WIDE_INT n_iterations;
b4ad7b23
RS
5637{
5638 struct iv_class *bl;
5639 rtx final_value = 0;
b4ad7b23
RS
5640
5641 bl = reg_biv_class[REGNO (v->src_reg)];
5642
5643 /* DEST_ADDR givs will never reach here, because they are always marked
5644 replaceable above in record_giv. */
5645
5646 /* The giv can be replaced outright by the reduced register only if all
5647 of the following conditions are true:
5648 - the insn that sets the giv is always executed on any iteration
5649 on which the giv is used at all
5650 (there are two ways to deduce this:
5651 either the insn is executed on every iteration,
5652 or all uses follow that insn in the same basic block),
5653 - its final value can be calculated (this condition is different
5654 than the one above in record_giv)
5655 - no assignments to the biv occur during the giv's lifetime. */
5656
5657#if 0
5658 /* This is only called now when replaceable is known to be false. */
5659 /* Clear replaceable, so that it won't confuse final_giv_value. */
5660 v->replaceable = 0;
5661#endif
5662
302670f3 5663 if ((final_value = final_giv_value (v, loop_start, loop_end, n_iterations))
b4ad7b23
RS
5664 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5665 {
5666 int biv_increment_seen = 0;
5667 rtx p = v->insn;
5668 rtx last_giv_use;
5669
5670 v->replaceable = 1;
5671
5672 /* When trying to determine whether or not a biv increment occurs
5673 during the lifetime of the giv, we can ignore uses of the variable
5674 outside the loop because final_value is true. Hence we can not
5675 use regno_last_uid and regno_first_uid as above in record_giv. */
5676
5677 /* Search the loop to determine whether any assignments to the
5678 biv occur during the giv's lifetime. Start with the insn
5679 that sets the giv, and search around the loop until we come
5680 back to that insn again.
5681
5682 Also fail if there is a jump within the giv's lifetime that jumps
5683 to somewhere outside the lifetime but still within the loop. This
5684 catches spaghetti code where the execution order is not linear, and
5685 hence the above test fails. Here we assume that the giv lifetime
5686 does not extend from one iteration of the loop to the next, so as
5687 to make the test easier. Since the lifetime isn't known yet,
5688 this requires two loops. See also record_giv above. */
5689
5690 last_giv_use = v->insn;
5691
5692 while (1)
5693 {
5694 p = NEXT_INSN (p);
5695 if (p == loop_end)
5696 p = NEXT_INSN (loop_start);
5697 if (p == v->insn)
5698 break;
5699
5700 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5701 || GET_CODE (p) == CALL_INSN)
5702 {
5703 if (biv_increment_seen)
5704 {
5705 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5706 {
5707 v->replaceable = 0;
5708 v->not_replaceable = 1;
5709 break;
5710 }
5711 }
c5da853f 5712 else if (reg_set_p (v->src_reg, PATTERN (p)))
b4ad7b23
RS
5713 biv_increment_seen = 1;
5714 else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5715 last_giv_use = p;
5716 }
5717 }
5718
5719 /* Now that the lifetime of the giv is known, check for branches
5720 from within the lifetime to outside the lifetime if it is still
5721 replaceable. */
5722
5723 if (v->replaceable)
5724 {
5725 p = v->insn;
5726 while (1)
5727 {
5728 p = NEXT_INSN (p);
5729 if (p == loop_end)
5730 p = NEXT_INSN (loop_start);
5731 if (p == last_giv_use)
5732 break;
5733
5734 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5735 && LABEL_NAME (JUMP_LABEL (p))
1cb1fe66
R
5736 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5737 && loop_insn_first_p (loop_start, JUMP_LABEL (p)))
5738 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5739 && loop_insn_first_p (JUMP_LABEL (p), loop_end))))
b4ad7b23
RS
5740 {
5741 v->replaceable = 0;
5742 v->not_replaceable = 1;
5743
5744 if (loop_dump_stream)
5745 fprintf (loop_dump_stream,
5746 "Found branch outside giv lifetime.\n");
5747
5748 break;
5749 }
5750 }
5751 }
5752
5753 /* If it is replaceable, then save the final value. */
5754 if (v->replaceable)
5755 v->final_value = final_value;
5756 }
5757
5758 if (loop_dump_stream && v->replaceable)
5759 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5760 INSN_UID (v->insn), REGNO (v->dest_reg));
5761}
5762\f
5763/* Update the status of whether a giv can derive other givs.
5764
5765 We need to do something special if there is or may be an update to the biv
5766 between the time the giv is defined and the time it is used to derive
5767 another giv.
5768
5769 In addition, a giv that is only conditionally set is not allowed to
5770 derive another giv once a label has been passed.
5771
5772 The cases we look at are when a label or an update to a biv is passed. */
5773
5774static void
5775update_giv_derive (p)
5776 rtx p;
5777{
5778 struct iv_class *bl;
5779 struct induction *biv, *giv;
5780 rtx tem;
5781 int dummy;
5782
5783 /* Search all IV classes, then all bivs, and finally all givs.
5784
7dcd3836 5785 There are three cases we are concerned with. First we have the situation
b4ad7b23
RS
5786 of a giv that is only updated conditionally. In that case, it may not
5787 derive any givs after a label is passed.
5788
5789 The second case is when a biv update occurs, or may occur, after the
5790 definition of a giv. For certain biv updates (see below) that are
5791 known to occur between the giv definition and use, we can adjust the
5792 giv definition. For others, or when the biv update is conditional,
5793 we must prevent the giv from deriving any other givs. There are two
5794 sub-cases within this case.
5795
5796 If this is a label, we are concerned with any biv update that is done
5797 conditionally, since it may be done after the giv is defined followed by
5798 a branch here (actually, we need to pass both a jump and a label, but
5799 this extra tracking doesn't seem worth it).
5800
7dcd3836
RK
5801 If this is a jump, we are concerned about any biv update that may be
5802 executed multiple times. We are actually only concerned about
5803 backward jumps, but it is probably not worth performing the test
5804 on the jump again here.
5805
5806 If this is a biv update, we must adjust the giv status to show that a
b4ad7b23
RS
5807 subsequent biv update was performed. If this adjustment cannot be done,
5808 the giv cannot derive further givs. */
5809
5810 for (bl = loop_iv_list; bl; bl = bl->next)
5811 for (biv = bl->biv; biv; biv = biv->next_iv)
7dcd3836
RK
5812 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5813 || biv->insn == p)
b4ad7b23
RS
5814 {
5815 for (giv = bl->giv; giv; giv = giv->next_iv)
5816 {
5817 /* If cant_derive is already true, there is no point in
5818 checking all of these conditions again. */
5819 if (giv->cant_derive)
5820 continue;
5821
5822 /* If this giv is conditionally set and we have passed a label,
5823 it cannot derive anything. */
5824 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5825 giv->cant_derive = 1;
5826
5827 /* Skip givs that have mult_val == 0, since
5828 they are really invariants. Also skip those that are
5829 replaceable, since we know their lifetime doesn't contain
5830 any biv update. */
5831 else if (giv->mult_val == const0_rtx || giv->replaceable)
5832 continue;
5833
5834 /* The only way we can allow this giv to derive another
5835 is if this is a biv increment and we can form the product
5836 of biv->add_val and giv->mult_val. In this case, we will
5837 be able to compute a compensation. */
5838 else if (biv->insn == p)
5839 {
c160c628
RK
5840 tem = 0;
5841
5842 if (biv->mult_val == const1_rtx)
38a448ca
RH
5843 tem = simplify_giv_expr (gen_rtx_MULT (giv->mode,
5844 biv->add_val,
5845 giv->mult_val),
c160c628
RK
5846 &dummy);
5847
5848 if (tem && giv->derive_adjustment)
38a448ca
RH
5849 tem = simplify_giv_expr (gen_rtx_PLUS (giv->mode, tem,
5850 giv->derive_adjustment),
c160c628
RK
5851 &dummy);
5852 if (tem)
b4ad7b23
RS
5853 giv->derive_adjustment = tem;
5854 else
5855 giv->cant_derive = 1;
5856 }
7dcd3836
RK
5857 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5858 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
b4ad7b23
RS
5859 giv->cant_derive = 1;
5860 }
5861 }
5862}
5863\f
5864/* Check whether an insn is an increment legitimate for a basic induction var.
7056f7e8
RS
5865 X is the source of insn P, or a part of it.
5866 MODE is the mode in which X should be interpreted.
5867
b4ad7b23
RS
5868 DEST_REG is the putative biv, also the destination of the insn.
5869 We accept patterns of these forms:
09d7f5a5 5870 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
b4ad7b23 5871 REG = INVARIANT + REG
b4ad7b23
RS
5872
5873 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
3ec2b590
R
5874 store the additive term into *INC_VAL, and store the place where
5875 we found the additive term into *LOCATION.
b4ad7b23
RS
5876
5877 If X is an assignment of an invariant into DEST_REG, we set
5878 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5879
09d7f5a5
RK
5880 We also want to detect a BIV when it corresponds to a variable
5881 whose mode was promoted via PROMOTED_MODE. In that case, an increment
5882 of the variable may be a PLUS that adds a SUBREG of that variable to
5883 an invariant and then sign- or zero-extends the result of the PLUS
5884 into the variable.
5885
5886 Most GIVs in such cases will be in the promoted mode, since that is the
5887 probably the natural computation mode (and almost certainly the mode
5888 used for addresses) on the machine. So we view the pseudo-reg containing
5889 the variable as the BIV, as if it were simply incremented.
5890
5891 Note that treating the entire pseudo as a BIV will result in making
5892 simple increments to any GIVs based on it. However, if the variable
5893 overflows in its declared mode but not its promoted mode, the result will
5894 be incorrect. This is acceptable if the variable is signed, since
5895 overflows in such cases are undefined, but not if it is unsigned, since
5896 those overflows are defined. So we only check for SIGN_EXTEND and
5897 not ZERO_EXTEND.
5898
5899 If we cannot find a biv, we return 0. */
b4ad7b23
RS
5900
5901static int
3ec2b590 5902basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val, location)
b4ad7b23 5903 register rtx x;
7056f7e8 5904 enum machine_mode mode;
09d7f5a5 5905 rtx p;
b4ad7b23
RS
5906 rtx dest_reg;
5907 rtx *inc_val;
5908 rtx *mult_val;
3ec2b590 5909 rtx **location;
b4ad7b23
RS
5910{
5911 register enum rtx_code code;
3ec2b590 5912 rtx *argp, arg;
09d7f5a5 5913 rtx insn, set = 0;
b4ad7b23
RS
5914
5915 code = GET_CODE (x);
5916 switch (code)
5917 {
5918 case PLUS:
45f97e2e 5919 if (rtx_equal_p (XEXP (x, 0), dest_reg)
09d7f5a5
RK
5920 || (GET_CODE (XEXP (x, 0)) == SUBREG
5921 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5922 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
3ec2b590
R
5923 {
5924 argp = &XEXP (x, 1);
5925 }
45f97e2e 5926 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
09d7f5a5 5927 || (GET_CODE (XEXP (x, 1)) == SUBREG
b81fd0f4
RS
5928 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5929 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
3ec2b590
R
5930 {
5931 argp = &XEXP (x, 0);
5932 }
b4ad7b23
RS
5933 else
5934 return 0;
5935
3ec2b590 5936 arg = *argp;
b4ad7b23
RS
5937 if (invariant_p (arg) != 1)
5938 return 0;
5939
7056f7e8 5940 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
b4ad7b23 5941 *mult_val = const1_rtx;
3ec2b590 5942 *location = argp;
b4ad7b23
RS
5943 return 1;
5944
09d7f5a5
RK
5945 case SUBREG:
5946 /* If this is a SUBREG for a promoted variable, check the inner
5947 value. */
5948 if (SUBREG_PROMOTED_VAR_P (x))
7056f7e8 5949 return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
3ec2b590 5950 dest_reg, p, inc_val, mult_val, location);
fe159061 5951 return 0;
b4ad7b23 5952
09d7f5a5 5953 case REG:
45f97e2e 5954 /* If this register is assigned in a previous insn, look at its
09d7f5a5
RK
5955 source, but don't go outside the loop or past a label. */
5956
45f97e2e
RH
5957 insn = p;
5958 while (1)
5959 {
5960 do {
5961 insn = PREV_INSN (insn);
5962 } while (insn && GET_CODE (insn) == NOTE
5963 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
09d7f5a5 5964
45f97e2e
RH
5965 if (!insn)
5966 break;
5967 set = single_set (insn);
5968 if (set == 0)
5969 break;
09d7f5a5 5970
45f97e2e
RH
5971 if ((SET_DEST (set) == x
5972 || (GET_CODE (SET_DEST (set)) == SUBREG
5973 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5974 <= UNITS_PER_WORD)
5975 && SUBREG_REG (SET_DEST (set)) == x))
5976 && basic_induction_var (SET_SRC (set),
5977 (GET_MODE (SET_SRC (set)) == VOIDmode
5978 ? GET_MODE (x)
5979 : GET_MODE (SET_SRC (set))),
5980 dest_reg, insn,
3ec2b590 5981 inc_val, mult_val, location))
45f97e2e
RH
5982 return 1;
5983 }
0f41302f 5984 /* ... fall through ... */
b4ad7b23
RS
5985
5986 /* Can accept constant setting of biv only when inside inner most loop.
5987 Otherwise, a biv of an inner loop may be incorrectly recognized
5988 as a biv of the outer loop,
5989 causing code to be moved INTO the inner loop. */
5990 case MEM:
b4ad7b23
RS
5991 if (invariant_p (x) != 1)
5992 return 0;
5993 case CONST_INT:
5994 case SYMBOL_REF:
5995 case CONST:
829002bb
BM
5996 /* convert_modes aborts if we try to convert to or from CCmode, so just
5997 exclude that case. It is very unlikely that a condition code value
5998 would be a useful iterator anyways. */
5999 if (loops_enclosed == 1
6000 && GET_MODE_CLASS (mode) != MODE_CC
6001 && GET_MODE_CLASS (GET_MODE (dest_reg)) != MODE_CC)
b4ad7b23 6002 {
7056f7e8
RS
6003 /* Possible bug here? Perhaps we don't know the mode of X. */
6004 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
b4ad7b23
RS
6005 *mult_val = const0_rtx;
6006 return 1;
6007 }
6008 else
6009 return 0;
6010
09d7f5a5 6011 case SIGN_EXTEND:
7056f7e8 6012 return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3ec2b590 6013 dest_reg, p, inc_val, mult_val, location);
45f97e2e 6014
09d7f5a5
RK
6015 case ASHIFTRT:
6016 /* Similar, since this can be a sign extension. */
6017 for (insn = PREV_INSN (p);
6018 (insn && GET_CODE (insn) == NOTE
6019 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6020 insn = PREV_INSN (insn))
6021 ;
6022
6023 if (insn)
6024 set = single_set (insn);
6025
6026 if (set && SET_DEST (set) == XEXP (x, 0)
6027 && GET_CODE (XEXP (x, 1)) == CONST_INT
6028 && INTVAL (XEXP (x, 1)) >= 0
6029 && GET_CODE (SET_SRC (set)) == ASHIFT
6030 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
7056f7e8
RS
6031 return basic_induction_var (XEXP (SET_SRC (set), 0),
6032 GET_MODE (XEXP (x, 0)),
3ec2b590
R
6033 dest_reg, insn, inc_val, mult_val,
6034 location);
09d7f5a5
RK
6035 return 0;
6036
b4ad7b23
RS
6037 default:
6038 return 0;
6039 }
6040}
6041\f
6042/* A general induction variable (giv) is any quantity that is a linear
6043 function of a basic induction variable,
6044 i.e. giv = biv * mult_val + add_val.
6045 The coefficients can be any loop invariant quantity.
6046 A giv need not be computed directly from the biv;
6047 it can be computed by way of other givs. */
6048
6049/* Determine whether X computes a giv.
6050 If it does, return a nonzero value
6051 which is the benefit from eliminating the computation of X;
6052 set *SRC_REG to the register of the biv that it is computed from;
6053 set *ADD_VAL and *MULT_VAL to the coefficients,
6054 such that the value of X is biv * mult + add; */
6055
6056static int
45f97e2e 6057general_induction_var (x, src_reg, add_val, mult_val, is_addr, pbenefit)
b4ad7b23
RS
6058 rtx x;
6059 rtx *src_reg;
6060 rtx *add_val;
6061 rtx *mult_val;
45f97e2e
RH
6062 int is_addr;
6063 int *pbenefit;
b4ad7b23
RS
6064{
6065 rtx orig_x = x;
b4ad7b23
RS
6066 char *storage;
6067
6068 /* If this is an invariant, forget it, it isn't a giv. */
6069 if (invariant_p (x) == 1)
6070 return 0;
6071
6072 /* See if the expression could be a giv and get its form.
6073 Mark our place on the obstack in case we don't find a giv. */
6074 storage = (char *) oballoc (0);
45f97e2e
RH
6075 *pbenefit = 0;
6076 x = simplify_giv_expr (x, pbenefit);
b4ad7b23
RS
6077 if (x == 0)
6078 {
6079 obfree (storage);
6080 return 0;
6081 }
6082
6083 switch (GET_CODE (x))
6084 {
6085 case USE:
6086 case CONST_INT:
6087 /* Since this is now an invariant and wasn't before, it must be a giv
6088 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6089 with. */
6090 *src_reg = loop_iv_list->biv->dest_reg;
6091 *mult_val = const0_rtx;
6092 *add_val = x;
6093 break;
6094
6095 case REG:
6096 /* This is equivalent to a BIV. */
6097 *src_reg = x;
6098 *mult_val = const1_rtx;
6099 *add_val = const0_rtx;
6100 break;
6101
6102 case PLUS:
6103 /* Either (plus (biv) (invar)) or
6104 (plus (mult (biv) (invar_1)) (invar_2)). */
6105 if (GET_CODE (XEXP (x, 0)) == MULT)
6106 {
6107 *src_reg = XEXP (XEXP (x, 0), 0);
6108 *mult_val = XEXP (XEXP (x, 0), 1);
6109 }
6110 else
6111 {
6112 *src_reg = XEXP (x, 0);
6113 *mult_val = const1_rtx;
6114 }
6115 *add_val = XEXP (x, 1);
6116 break;
6117
6118 case MULT:
6119 /* ADD_VAL is zero. */
6120 *src_reg = XEXP (x, 0);
6121 *mult_val = XEXP (x, 1);
6122 *add_val = const0_rtx;
6123 break;
6124
6125 default:
6126 abort ();
6127 }
6128
6129 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6130 unless they are CONST_INT). */
6131 if (GET_CODE (*add_val) == USE)
6132 *add_val = XEXP (*add_val, 0);
6133 if (GET_CODE (*mult_val) == USE)
6134 *mult_val = XEXP (*mult_val, 0);
6135
45f97e2e
RH
6136 if (is_addr)
6137 {
6138#ifdef ADDRESS_COST
6139 *pbenefit += ADDRESS_COST (orig_x) - reg_address_cost;
6140#else
6141 *pbenefit += rtx_cost (orig_x, MEM) - reg_address_cost;
6142#endif
6143 }
6144 else
6145 *pbenefit += rtx_cost (orig_x, SET);
b4ad7b23 6146
45f97e2e
RH
6147 /* Always return true if this is a giv so it will be detected as such,
6148 even if the benefit is zero or negative. This allows elimination
6149 of bivs that might otherwise not be eliminated. */
6150 return 1;
b4ad7b23
RS
6151}
6152\f
6153/* Given an expression, X, try to form it as a linear function of a biv.
6154 We will canonicalize it to be of the form
6155 (plus (mult (BIV) (invar_1))
6156 (invar_2))
c5b7917e 6157 with possible degeneracies.
b4ad7b23
RS
6158
6159 The invariant expressions must each be of a form that can be used as a
6160 machine operand. We surround then with a USE rtx (a hack, but localized
6161 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6162 routine; it is the caller's responsibility to strip them.
6163
6164 If no such canonicalization is possible (i.e., two biv's are used or an
6165 expression that is neither invariant nor a biv or giv), this routine
6166 returns 0.
6167
6168 For a non-zero return, the result will have a code of CONST_INT, USE,
6169 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6170
6171 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6172
45f97e2e
RH
6173static rtx sge_plus PROTO ((enum machine_mode, rtx, rtx));
6174static rtx sge_plus_constant PROTO ((rtx, rtx));
6175
b4ad7b23
RS
6176static rtx
6177simplify_giv_expr (x, benefit)
6178 rtx x;
6179 int *benefit;
6180{
6181 enum machine_mode mode = GET_MODE (x);
6182 rtx arg0, arg1;
6183 rtx tem;
6184
6185 /* If this is not an integer mode, or if we cannot do arithmetic in this
6186 mode, this can't be a giv. */
6187 if (mode != VOIDmode
6188 && (GET_MODE_CLASS (mode) != MODE_INT
5fd8383e 6189 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
45f97e2e 6190 return NULL_RTX;
b4ad7b23
RS
6191
6192 switch (GET_CODE (x))
6193 {
6194 case PLUS:
6195 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
6196 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
6197 if (arg0 == 0 || arg1 == 0)
45f97e2e 6198 return NULL_RTX;
b4ad7b23
RS
6199
6200 /* Put constant last, CONST_INT last if both constant. */
6201 if ((GET_CODE (arg0) == USE
6202 || GET_CODE (arg0) == CONST_INT)
45f97e2e
RH
6203 && ! ((GET_CODE (arg0) == USE
6204 && GET_CODE (arg1) == USE)
6205 || GET_CODE (arg1) == CONST_INT))
b4ad7b23
RS
6206 tem = arg0, arg0 = arg1, arg1 = tem;
6207
6208 /* Handle addition of zero, then addition of an invariant. */
6209 if (arg1 == const0_rtx)
6210 return arg0;
6211 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6212 switch (GET_CODE (arg0))
6213 {
6214 case CONST_INT:
6215 case USE:
45f97e2e
RH
6216 /* Adding two invariants must result in an invariant, so enclose
6217 addition operation inside a USE and return it. */
b4ad7b23
RS
6218 if (GET_CODE (arg0) == USE)
6219 arg0 = XEXP (arg0, 0);
da0af5a5
JL
6220 if (GET_CODE (arg1) == USE)
6221 arg1 = XEXP (arg1, 0);
6222
45f97e2e
RH
6223 if (GET_CODE (arg0) == CONST_INT)
6224 tem = arg0, arg0 = arg1, arg1 = tem;
6225 if (GET_CODE (arg1) == CONST_INT)
6226 tem = sge_plus_constant (arg0, arg1);
da0af5a5 6227 else
45f97e2e 6228 tem = sge_plus (mode, arg0, arg1);
b4ad7b23 6229
45f97e2e
RH
6230 if (GET_CODE (tem) != CONST_INT)
6231 tem = gen_rtx_USE (mode, tem);
b4ad7b23
RS
6232 return tem;
6233
6234 case REG:
6235 case MULT:
6236 /* biv + invar or mult + invar. Return sum. */
38a448ca 6237 return gen_rtx_PLUS (mode, arg0, arg1);
b4ad7b23
RS
6238
6239 case PLUS:
6240 /* (a + invar_1) + invar_2. Associate. */
45f97e2e
RH
6241 return simplify_giv_expr (
6242 gen_rtx_PLUS (mode, XEXP (arg0, 0),
6243 gen_rtx_PLUS (mode, XEXP (arg0, 1), arg1)),
6244 benefit);
b4ad7b23
RS
6245
6246 default:
6247 abort ();
6248 }
6249
6250 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6251 MULT to reduce cases. */
6252 if (GET_CODE (arg0) == REG)
38a448ca 6253 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
b4ad7b23 6254 if (GET_CODE (arg1) == REG)
38a448ca 6255 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
b4ad7b23
RS
6256
6257 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6258 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6259 Recurse to associate the second PLUS. */
6260 if (GET_CODE (arg1) == MULT)
6261 tem = arg0, arg0 = arg1, arg1 = tem;
6262
6263 if (GET_CODE (arg1) == PLUS)
38a448ca
RH
6264 return simplify_giv_expr (gen_rtx_PLUS (mode,
6265 gen_rtx_PLUS (mode, arg0,
6266 XEXP (arg1, 0)),
6267 XEXP (arg1, 1)),
b4ad7b23
RS
6268 benefit);
6269
6270 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6271 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
45f97e2e 6272 return NULL_RTX;
b4ad7b23 6273
45f97e2e
RH
6274 if (!rtx_equal_p (arg0, arg1))
6275 return NULL_RTX;
b4ad7b23 6276
38a448ca
RH
6277 return simplify_giv_expr (gen_rtx_MULT (mode,
6278 XEXP (arg0, 0),
6279 gen_rtx_PLUS (mode,
6280 XEXP (arg0, 1),
6281 XEXP (arg1, 1))),
b4ad7b23
RS
6282 benefit);
6283
6284 case MINUS:
0f41302f 6285 /* Handle "a - b" as "a + b * (-1)". */
38a448ca
RH
6286 return simplify_giv_expr (gen_rtx_PLUS (mode,
6287 XEXP (x, 0),
6288 gen_rtx_MULT (mode, XEXP (x, 1),
6289 constm1_rtx)),
b4ad7b23
RS
6290 benefit);
6291
6292 case MULT:
6293 arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
6294 arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
6295 if (arg0 == 0 || arg1 == 0)
45f97e2e 6296 return NULL_RTX;
b4ad7b23
RS
6297
6298 /* Put constant last, CONST_INT last if both constant. */
6299 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6300 && GET_CODE (arg1) != CONST_INT)
6301 tem = arg0, arg0 = arg1, arg1 = tem;
6302
6303 /* If second argument is not now constant, not giv. */
6304 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
45f97e2e 6305 return NULL_RTX;
b4ad7b23
RS
6306
6307 /* Handle multiply by 0 or 1. */
6308 if (arg1 == const0_rtx)
6309 return const0_rtx;
6310
6311 else if (arg1 == const1_rtx)
6312 return arg0;
6313
6314 switch (GET_CODE (arg0))
6315 {
6316 case REG:
6317 /* biv * invar. Done. */
38a448ca 6318 return gen_rtx_MULT (mode, arg0, arg1);
b4ad7b23
RS
6319
6320 case CONST_INT:
6321 /* Product of two constants. */
5fd8383e 6322 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
b4ad7b23
RS
6323
6324 case USE:
45f97e2e
RH
6325 /* invar * invar. It is a giv, but very few of these will
6326 actually pay off, so limit to simple registers. */
6327 if (GET_CODE (arg1) != CONST_INT)
6328 return NULL_RTX;
6329
6330 arg0 = XEXP (arg0, 0);
6331 if (GET_CODE (arg0) == REG)
6332 tem = gen_rtx_MULT (mode, arg0, arg1);
6333 else if (GET_CODE (arg0) == MULT
6334 && GET_CODE (XEXP (arg0, 0)) == REG
6335 && GET_CODE (XEXP (arg0, 1)) == CONST_INT)
6336 {
6337 tem = gen_rtx_MULT (mode, XEXP (arg0, 0),
6338 GEN_INT (INTVAL (XEXP (arg0, 1))
6339 * INTVAL (arg1)));
6340 }
6341 else
6342 return NULL_RTX;
6343 return gen_rtx_USE (mode, tem);
b4ad7b23
RS
6344
6345 case MULT:
6346 /* (a * invar_1) * invar_2. Associate. */
38a448ca
RH
6347 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (arg0, 0),
6348 gen_rtx_MULT (mode,
6349 XEXP (arg0, 1),
6350 arg1)),
b4ad7b23
RS
6351 benefit);
6352
6353 case PLUS:
6354 /* (a + invar_1) * invar_2. Distribute. */
38a448ca
RH
6355 return simplify_giv_expr (gen_rtx_PLUS (mode,
6356 gen_rtx_MULT (mode,
6357 XEXP (arg0, 0),
6358 arg1),
6359 gen_rtx_MULT (mode,
6360 XEXP (arg0, 1),
6361 arg1)),
b4ad7b23
RS
6362 benefit);
6363
6364 default:
6365 abort ();
6366 }
6367
6368 case ASHIFT:
b4ad7b23
RS
6369 /* Shift by constant is multiply by power of two. */
6370 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6371 return 0;
6372
38a448ca
RH
6373 return simplify_giv_expr (gen_rtx_MULT (mode,
6374 XEXP (x, 0),
6375 GEN_INT ((HOST_WIDE_INT) 1
6376 << INTVAL (XEXP (x, 1)))),
b4ad7b23
RS
6377 benefit);
6378
6379 case NEG:
6380 /* "-a" is "a * (-1)" */
38a448ca 6381 return simplify_giv_expr (gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
b4ad7b23
RS
6382 benefit);
6383
6384 case NOT:
6385 /* "~a" is "-a - 1". Silly, but easy. */
38a448ca
RH
6386 return simplify_giv_expr (gen_rtx_MINUS (mode,
6387 gen_rtx_NEG (mode, XEXP (x, 0)),
6388 const1_rtx),
b4ad7b23
RS
6389 benefit);
6390
6391 case USE:
6392 /* Already in proper form for invariant. */
6393 return x;
6394
6395 case REG:
6396 /* If this is a new register, we can't deal with it. */
6397 if (REGNO (x) >= max_reg_before_loop)
6398 return 0;
6399
6400 /* Check for biv or giv. */
3ec2b590 6401 switch (REG_IV_TYPE (REGNO (x)))
b4ad7b23
RS
6402 {
6403 case BASIC_INDUCT:
6404 return x;
6405 case GENERAL_INDUCT:
6406 {
3ec2b590 6407 struct induction *v = REG_IV_INFO (REGNO (x));
b4ad7b23
RS
6408
6409 /* Form expression from giv and add benefit. Ensure this giv
6410 can derive another and subtract any needed adjustment if so. */
6411 *benefit += v->benefit;
6412 if (v->cant_derive)
6413 return 0;
6414
38a448ca
RH
6415 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode, v->src_reg,
6416 v->mult_val),
b4ad7b23
RS
6417 v->add_val);
6418 if (v->derive_adjustment)
38a448ca 6419 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
b4ad7b23
RS
6420 return simplify_giv_expr (tem, benefit);
6421 }
e9a25f70
JL
6422
6423 default:
45f97e2e
RH
6424 /* If it isn't an induction variable, and it is invariant, we
6425 may be able to simplify things further by looking through
6426 the bits we just moved outside the loop. */
6427 if (invariant_p (x) == 1)
6428 {
6429 struct movable *m;
6430
6431 for (m = the_movables; m ; m = m->next)
6432 if (rtx_equal_p (x, m->set_dest))
6433 {
6434 /* Ok, we found a match. Substitute and simplify. */
6435
6436 /* If we match another movable, we must use that, as
6437 this one is going away. */
6438 if (m->match)
6439 return simplify_giv_expr (m->match->set_dest, benefit);
6440
6441 /* If consec is non-zero, this is a member of a group of
6442 instructions that were moved together. We handle this
6443 case only to the point of seeking to the last insn and
6444 looking for a REG_EQUAL. Fail if we don't find one. */
6445 if (m->consec != 0)
6446 {
6447 int i = m->consec;
6448 tem = m->insn;
6449 do { tem = NEXT_INSN (tem); } while (--i > 0);
6450
6451 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6452 if (tem)
6453 tem = XEXP (tem, 0);
6454 }
6455 else
6456 {
6457 tem = single_set (m->insn);
6458 if (tem)
6459 tem = SET_SRC (tem);
6460 }
6461
6462 if (tem)
6463 {
6464 /* What we are most interested in is pointer
6465 arithmetic on invariants -- only take
6466 patterns we may be able to do something with. */
6467 if (GET_CODE (tem) == PLUS
6468 || GET_CODE (tem) == MULT
6469 || GET_CODE (tem) == ASHIFT
6470 || GET_CODE (tem) == CONST_INT
6471 || GET_CODE (tem) == SYMBOL_REF)
6472 {
6473 tem = simplify_giv_expr (tem, benefit);
6474 if (tem)
6475 return tem;
6476 }
6477 else if (GET_CODE (tem) == CONST
6478 && GET_CODE (XEXP (tem, 0)) == PLUS
6479 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6480 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6481 {
6482 tem = simplify_giv_expr (XEXP (tem, 0), benefit);
6483 if (tem)
6484 return tem;
6485 }
6486 }
6487 break;
6488 }
6489 }
e9a25f70 6490 break;
b4ad7b23
RS
6491 }
6492
6493 /* Fall through to general case. */
6494 default:
6495 /* If invariant, return as USE (unless CONST_INT).
6496 Otherwise, not giv. */
6497 if (GET_CODE (x) == USE)
6498 x = XEXP (x, 0);
6499
6500 if (invariant_p (x) == 1)
6501 {
6502 if (GET_CODE (x) == CONST_INT)
6503 return x;
45f97e2e
RH
6504 if (GET_CODE (x) == CONST
6505 && GET_CODE (XEXP (x, 0)) == PLUS
6506 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6507 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6508 x = XEXP (x, 0);
6509 return gen_rtx_USE (mode, x);
b4ad7b23
RS
6510 }
6511 else
6512 return 0;
6513 }
6514}
45f97e2e
RH
6515
6516/* This routine folds invariants such that there is only ever one
6517 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6518
6519static rtx
6520sge_plus_constant (x, c)
6521 rtx x, c;
6522{
6523 if (GET_CODE (x) == CONST_INT)
6524 return GEN_INT (INTVAL (x) + INTVAL (c));
6525 else if (GET_CODE (x) != PLUS)
6526 return gen_rtx_PLUS (GET_MODE (x), x, c);
6527 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6528 {
6529 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6530 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6531 }
6532 else if (GET_CODE (XEXP (x, 0)) == PLUS
6533 || GET_CODE (XEXP (x, 1)) != PLUS)
6534 {
6535 return gen_rtx_PLUS (GET_MODE (x),
6536 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6537 }
6538 else
6539 {
6540 return gen_rtx_PLUS (GET_MODE (x),
6541 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6542 }
6543}
6544
6545static rtx
6546sge_plus (mode, x, y)
6547 enum machine_mode mode;
6548 rtx x, y;
6549{
6550 while (GET_CODE (y) == PLUS)
6551 {
6552 rtx a = XEXP (y, 0);
6553 if (GET_CODE (a) == CONST_INT)
6554 x = sge_plus_constant (x, a);
6555 else
6556 x = gen_rtx_PLUS (mode, x, a);
6557 y = XEXP (y, 1);
6558 }
6559 if (GET_CODE (y) == CONST_INT)
6560 x = sge_plus_constant (x, y);
6561 else
6562 x = gen_rtx_PLUS (mode, x, y);
6563 return x;
6564}
b4ad7b23
RS
6565\f
6566/* Help detect a giv that is calculated by several consecutive insns;
6567 for example,
6568 giv = biv * M
6569 giv = giv + A
6570 The caller has already identified the first insn P as having a giv as dest;
6571 we check that all other insns that set the same register follow
6572 immediately after P, that they alter nothing else,
6573 and that the result of the last is still a giv.
6574
6575 The value is 0 if the reg set in P is not really a giv.
6576 Otherwise, the value is the amount gained by eliminating
6577 all the consecutive insns that compute the value.
6578
6579 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6580 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6581
6582 The coefficients of the ultimate giv value are stored in
6583 *MULT_VAL and *ADD_VAL. */
6584
6585static int
6586consec_sets_giv (first_benefit, p, src_reg, dest_reg,
a07516d3 6587 add_val, mult_val, last_consec_insn)
b4ad7b23
RS
6588 int first_benefit;
6589 rtx p;
6590 rtx src_reg;
6591 rtx dest_reg;
6592 rtx *add_val;
6593 rtx *mult_val;
a07516d3 6594 rtx *last_consec_insn;
b4ad7b23
RS
6595{
6596 int count;
6597 enum rtx_code code;
6598 int benefit;
6599 rtx temp;
6600 rtx set;
6601
6602 /* Indicate that this is a giv so that we can update the value produced in
6603 each insn of the multi-insn sequence.
6604
6605 This induction structure will be used only by the call to
6606 general_induction_var below, so we can allocate it on our stack.
6607 If this is a giv, our caller will replace the induct var entry with
6608 a new induction structure. */
6609 struct induction *v
6610 = (struct induction *) alloca (sizeof (struct induction));
6611 v->src_reg = src_reg;
6612 v->mult_val = *mult_val;
6613 v->add_val = *add_val;
6614 v->benefit = first_benefit;
6615 v->cant_derive = 0;
6616 v->derive_adjustment = 0;
6617
3ec2b590
R
6618 REG_IV_TYPE (REGNO (dest_reg)) = GENERAL_INDUCT;
6619 REG_IV_INFO (REGNO (dest_reg)) = v;
b4ad7b23 6620
8deb8e2c 6621 count = VARRAY_INT (n_times_set, REGNO (dest_reg)) - 1;
b4ad7b23
RS
6622
6623 while (count > 0)
6624 {
6625 p = NEXT_INSN (p);
6626 code = GET_CODE (p);
6627
6628 /* If libcall, skip to end of call sequence. */
5fd8383e 6629 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
b4ad7b23
RS
6630 p = XEXP (temp, 0);
6631
6632 if (code == INSN
6633 && (set = single_set (p))
6634 && GET_CODE (SET_DEST (set)) == REG
6635 && SET_DEST (set) == dest_reg
45f97e2e
RH
6636 && (general_induction_var (SET_SRC (set), &src_reg,
6637 add_val, mult_val, 0, &benefit)
b4ad7b23 6638 /* Giv created by equivalent expression. */
5fd8383e 6639 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
45f97e2e
RH
6640 && general_induction_var (XEXP (temp, 0), &src_reg,
6641 add_val, mult_val, 0, &benefit)))
b4ad7b23
RS
6642 && src_reg == v->src_reg)
6643 {
5fd8383e 6644 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
b4ad7b23
RS
6645 benefit += libcall_benefit (p);
6646
6647 count--;
6648 v->mult_val = *mult_val;
6649 v->add_val = *add_val;
6650 v->benefit = benefit;
6651 }
6652 else if (code != NOTE)
6653 {
6654 /* Allow insns that set something other than this giv to a
6655 constant. Such insns are needed on machines which cannot
6656 include long constants and should not disqualify a giv. */
6657 if (code == INSN
6658 && (set = single_set (p))
6659 && SET_DEST (set) != dest_reg
6660 && CONSTANT_P (SET_SRC (set)))
6661 continue;
6662
3ec2b590 6663 REG_IV_TYPE (REGNO (dest_reg)) = UNKNOWN_INDUCT;
b4ad7b23
RS
6664 return 0;
6665 }
6666 }
6667
a07516d3 6668 *last_consec_insn = p;
b4ad7b23
RS
6669 return v->benefit;
6670}
6671\f
6672/* Return an rtx, if any, that expresses giv G2 as a function of the register
6673 represented by G1. If no such expression can be found, or it is clear that
6674 it cannot possibly be a valid address, 0 is returned.
6675
6676 To perform the computation, we note that
45f97e2e
RH
6677 G1 = x * v + a and
6678 G2 = y * v + b
b4ad7b23
RS
6679 where `v' is the biv.
6680
45f97e2e
RH
6681 So G2 = (y/b) * G1 + (b - a*y/x).
6682
6683 Note that MULT = y/x.
6684
6685 Update: A and B are now allowed to be additive expressions such that
6686 B contains all variables in A. That is, computing B-A will not require
6687 subtracting variables. */
6688
6689static rtx
6690express_from_1 (a, b, mult)
6691 rtx a, b, mult;
6692{
6693 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
6694
6695 if (mult == const0_rtx)
6696 return b;
6697
6698 /* If MULT is not 1, we cannot handle A with non-constants, since we
6699 would then be required to subtract multiples of the registers in A.
6700 This is theoretically possible, and may even apply to some Fortran
6701 constructs, but it is a lot of work and we do not attempt it here. */
6702
6703 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
6704 return NULL_RTX;
6705
6706 /* In general these structures are sorted top to bottom (down the PLUS
6707 chain), but not left to right across the PLUS. If B is a higher
6708 order giv than A, we can strip one level and recurse. If A is higher
6709 order, we'll eventually bail out, but won't know that until the end.
6710 If they are the same, we'll strip one level around this loop. */
6711
6712 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
6713 {
6714 rtx ra, rb, oa, ob, tmp;
6715
6716 ra = XEXP (a, 0), oa = XEXP (a, 1);
6717 if (GET_CODE (ra) == PLUS)
6718 tmp = ra, ra = oa, oa = tmp;
6719
6720 rb = XEXP (b, 0), ob = XEXP (b, 1);
6721 if (GET_CODE (rb) == PLUS)
6722 tmp = rb, rb = ob, ob = tmp;
6723
6724 if (rtx_equal_p (ra, rb))
6725 /* We matched: remove one reg completely. */
6726 a = oa, b = ob;
6727 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
6728 /* An alternate match. */
6729 a = oa, b = rb;
6730 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
6731 /* An alternate match. */
6732 a = ra, b = ob;
6733 else
6734 {
6735 /* Indicates an extra register in B. Strip one level from B and
6736 recurse, hoping B was the higher order expression. */
6737 ob = express_from_1 (a, ob, mult);
6738 if (ob == NULL_RTX)
6739 return NULL_RTX;
6740 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
6741 }
6742 }
6743
6744 /* Here we are at the last level of A, go through the cases hoping to
6745 get rid of everything but a constant. */
6746
6747 if (GET_CODE (a) == PLUS)
6748 {
efe3eb65 6749 rtx ra, oa;
45f97e2e
RH
6750
6751 ra = XEXP (a, 0), oa = XEXP (a, 1);
6752 if (rtx_equal_p (oa, b))
6753 oa = ra;
6754 else if (!rtx_equal_p (ra, b))
6755 return NULL_RTX;
6756
6757 if (GET_CODE (oa) != CONST_INT)
6758 return NULL_RTX;
6759
6760 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
6761 }
6762 else if (GET_CODE (a) == CONST_INT)
6763 {
6764 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
6765 }
6766 else if (GET_CODE (b) == PLUS)
6767 {
6768 if (rtx_equal_p (a, XEXP (b, 0)))
6769 return XEXP (b, 1);
6770 else if (rtx_equal_p (a, XEXP (b, 1)))
6771 return XEXP (b, 0);
6772 else
6773 return NULL_RTX;
6774 }
6775 else if (rtx_equal_p (a, b))
6776 return const0_rtx;
6777
6778 return NULL_RTX;
6779}
b4ad7b23 6780
4d87f7a7 6781rtx
b4ad7b23
RS
6782express_from (g1, g2)
6783 struct induction *g1, *g2;
6784{
6785 rtx mult, add;
6786
6787 /* The value that G1 will be multiplied by must be a constant integer. Also,
6788 the only chance we have of getting a valid address is if b*c/a (see above
6789 for notation) is also an integer. */
45f97e2e
RH
6790 if (GET_CODE (g1->mult_val) == CONST_INT
6791 && GET_CODE (g2->mult_val) == CONST_INT)
6792 {
6793 if (g1->mult_val == const0_rtx
6794 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
6795 return NULL_RTX;
6796 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
6797 }
6798 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
6799 mult = const1_rtx;
6800 else
6801 {
6802 /* ??? Find out if the one is a multiple of the other? */
6803 return NULL_RTX;
6804 }
b4ad7b23 6805
45f97e2e
RH
6806 add = express_from_1 (g1->add_val, g2->add_val, mult);
6807 if (add == NULL_RTX)
6808 return NULL_RTX;
b4ad7b23
RS
6809
6810 /* Form simplified final result. */
6811 if (mult == const0_rtx)
6812 return add;
6813 else if (mult == const1_rtx)
6814 mult = g1->dest_reg;
6815 else
38a448ca 6816 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
b4ad7b23
RS
6817
6818 if (add == const0_rtx)
6819 return mult;
6820 else
86219cc7
BS
6821 {
6822 if (GET_CODE (add) == PLUS
6823 && CONSTANT_P (XEXP (add, 1)))
6824 {
6825 rtx tem = XEXP (add, 1);
6826 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
6827 add = tem;
6828 }
6829
6830 return gen_rtx_PLUS (g2->mode, mult, add);
6831 }
6832
b4ad7b23 6833}
b4ad7b23 6834\f
da5a44b3
BS
6835/* Return an rtx, if any, that expresses giv G2 as a function of the register
6836 represented by G1. This indicates that G2 should be combined with G1 and
6837 that G2 can use (either directly or via an address expression) a register
6838 used to represent G1. */
b4ad7b23 6839
45f97e2e 6840static rtx
b4ad7b23
RS
6841combine_givs_p (g1, g2)
6842 struct induction *g1, *g2;
6843{
45f97e2e 6844 rtx tem = express_from (g1, g2);
b4ad7b23 6845
45f97e2e
RH
6846 /* If these givs are identical, they can be combined. We use the results
6847 of express_from because the addends are not in a canonical form, so
6848 rtx_equal_p is a weaker test. */
3ec2b590
R
6849 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
6850 combination to be the other way round. */
6851 if (tem == g1->dest_reg
6852 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
b4ad7b23 6853 {
45f97e2e 6854 return g1->dest_reg;
b4ad7b23
RS
6855 }
6856
b4ad7b23
RS
6857 /* If G2 can be expressed as a function of G1 and that function is valid
6858 as an address and no more expensive than using a register for G2,
6859 the expression of G2 in terms of G1 can be used. */
45f97e2e
RH
6860 if (tem != NULL_RTX
6861 && g2->giv_type == DEST_ADDR
b4ad7b23 6862 && memory_address_p (g2->mem_mode, tem)
45f97e2e
RH
6863 /* ??? Looses, especially with -fforce-addr, where *g2->location
6864 will always be a register, and so anything more complicated
6865 gets discarded. */
6866#if 0
6867#ifdef ADDRESS_COST
6868 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
6869#else
6870 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
6871#endif
6872#endif
6873 )
b4ad7b23 6874 {
45f97e2e 6875 return tem;
b4ad7b23 6876 }
b4ad7b23 6877
45f97e2e 6878 return NULL_RTX;
b4ad7b23
RS
6879}
6880\f
45f97e2e
RH
6881struct combine_givs_stats
6882{
6883 int giv_number;
6884 int total_benefit;
6885};
6886
6887static int
6888cmp_combine_givs_stats (x, y)
6889 struct combine_givs_stats *x, *y;
6890{
6891 int d;
6892 d = y->total_benefit - x->total_benefit;
6893 /* Stabilize the sort. */
6894 if (!d)
6895 d = x->giv_number - y->giv_number;
6896 return d;
6897}
6898
b4ad7b23
RS
6899/* Check all pairs of givs for iv_class BL and see if any can be combined with
6900 any other. If so, point SAME to the giv combined with and set NEW_REG to
6901 be an expression (in terms of the other giv's DEST_REG) equivalent to the
6902 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
6903
6904static void
6905combine_givs (bl)
6906 struct iv_class *bl;
6907{
ba12c883
RH
6908 /* Additional benefit to add for being combined multiple times. */
6909 const int extra_benefit = 3;
6910
29a82058 6911 struct induction *g1, *g2, **giv_array;
45f97e2e
RH
6912 int i, j, k, giv_count;
6913 struct combine_givs_stats *stats;
6914 rtx *can_combine;
b4ad7b23 6915
7027f90a
JW
6916 /* Count givs, because bl->giv_count is incorrect here. */
6917 giv_count = 0;
b4ad7b23 6918 for (g1 = bl->giv; g1; g1 = g1->next_iv)
45f97e2e
RH
6919 if (!g1->ignore)
6920 giv_count++;
7027f90a
JW
6921
6922 giv_array
6923 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
6924 i = 0;
6925 for (g1 = bl->giv; g1; g1 = g1->next_iv)
45f97e2e
RH
6926 if (!g1->ignore)
6927 giv_array[i++] = g1;
7027f90a 6928
45f97e2e 6929 stats = (struct combine_givs_stats *) alloca (giv_count * sizeof (*stats));
efe3eb65 6930 bzero ((char *) stats, giv_count * sizeof (*stats));
7027f90a 6931
45f97e2e 6932 can_combine = (rtx *) alloca (giv_count * giv_count * sizeof(rtx));
efe3eb65 6933 bzero ((char *) can_combine, giv_count * giv_count * sizeof(rtx));
7027f90a
JW
6934
6935 for (i = 0; i < giv_count; i++)
6936 {
45f97e2e 6937 int this_benefit;
ba12c883 6938 rtx single_use;
45f97e2e 6939
7027f90a 6940 g1 = giv_array[i];
ba12c883
RH
6941 stats[i].giv_number = i;
6942
6943 /* If a DEST_REG GIV is used only once, do not allow it to combine
6944 with anything, for in doing so we will gain nothing that cannot
6945 be had by simply letting the GIV with which we would have combined
6946 to be reduced on its own. The losage shows up in particular with
6947 DEST_ADDR targets on hosts with reg+reg addressing, though it can
6948 be seen elsewhere as well. */
6949 if (g1->giv_type == DEST_REG
6950 && (single_use = VARRAY_RTX (reg_single_usage, REGNO (g1->dest_reg)))
6951 && single_use != const0_rtx)
6952 continue;
45f97e2e
RH
6953
6954 this_benefit = g1->benefit;
6955 /* Add an additional weight for zero addends. */
6956 if (g1->no_const_addval)
6957 this_benefit += 1;
ba12c883 6958
45f97e2e
RH
6959 for (j = 0; j < giv_count; j++)
6960 {
6961 rtx this_combine;
6962
6963 g2 = giv_array[j];
6964 if (g1 != g2
6965 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
6966 {
6967 can_combine[i*giv_count + j] = this_combine;
ba12c883 6968 this_benefit += g2->benefit + extra_benefit;
45f97e2e
RH
6969 }
6970 }
45f97e2e
RH
6971 stats[i].total_benefit = this_benefit;
6972 }
6973
6974 /* Iterate, combining until we can't. */
6975restart:
6976 qsort (stats, giv_count, sizeof(*stats), cmp_combine_givs_stats);
6977
6978 if (loop_dump_stream)
6979 {
6980 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
6981 for (k = 0; k < giv_count; k++)
6982 {
6983 g1 = giv_array[stats[k].giv_number];
6984 if (!g1->combined_with && !g1->same)
6985 fprintf (loop_dump_stream, " {%d, %d}",
6986 INSN_UID (giv_array[stats[k].giv_number]->insn),
6987 stats[k].total_benefit);
6988 }
6989 putc ('\n', loop_dump_stream);
6990 }
6991
6992 for (k = 0; k < giv_count; k++)
6993 {
6994 int g1_add_benefit = 0;
6995
6996 i = stats[k].giv_number;
6997 g1 = giv_array[i];
6998
6999 /* If it has already been combined, skip. */
7000 if (g1->combined_with || g1->same)
7001 continue;
7002
7003 for (j = 0; j < giv_count; j++)
7004 {
7005 g2 = giv_array[j];
7006 if (g1 != g2 && can_combine[i*giv_count + j]
7007 /* If it has already been combined, skip. */
7008 && ! g2->same && ! g2->combined_with)
7009 {
7010 int l;
7011
7012 g2->new_reg = can_combine[i*giv_count + j];
7013 g2->same = g1;
3ec2b590 7014 g1->combined_with++;
45f97e2e
RH
7015 g1->lifetime += g2->lifetime;
7016
ba12c883 7017 g1_add_benefit += g2->benefit;
45f97e2e
RH
7018
7019 /* ??? The new final_[bg]iv_value code does a much better job
7020 of finding replaceable giv's, and hence this code may no
7021 longer be necessary. */
7022 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7023 g1_add_benefit -= copy_cost;
7027f90a 7024
45f97e2e
RH
7025 /* To help optimize the next set of combinations, remove
7026 this giv from the benefits of other potential mates. */
7027 for (l = 0; l < giv_count; ++l)
7028 {
7029 int m = stats[l].giv_number;
7030 if (can_combine[m*giv_count + j])
ba12c883 7031 stats[l].total_benefit -= g2->benefit + extra_benefit;
45f97e2e
RH
7032 }
7033
7034 if (loop_dump_stream)
7035 fprintf (loop_dump_stream,
7036 "giv at %d combined with giv at %d\n",
7037 INSN_UID (g2->insn), INSN_UID (g1->insn));
7038 }
7039 }
7040
7041 /* To help optimize the next set of combinations, remove
7042 this giv from the benefits of other potential mates. */
7043 if (g1->combined_with)
7044 {
7045 for (j = 0; j < giv_count; ++j)
7046 {
7047 int m = stats[j].giv_number;
0466bdc4 7048 if (can_combine[m*giv_count + i])
ba12c883 7049 stats[j].total_benefit -= g1->benefit + extra_benefit;
45f97e2e
RH
7050 }
7051
7052 g1->benefit += g1_add_benefit;
7053
7054 /* We've finished with this giv, and everything it touched.
7055 Restart the combination so that proper weights for the
7056 rest of the givs are properly taken into account. */
7057 /* ??? Ideally we would compact the arrays at this point, so
7058 as to not cover old ground. But sanely compacting
7059 can_combine is tricky. */
7060 goto restart;
7061 }
7027f90a 7062 }
b4ad7b23
RS
7063}
7064\f
3ec2b590
R
7065struct recombine_givs_stats
7066{
7067 int giv_number;
7068 int start_luid, end_luid;
7069};
7070
7071/* Used below as comparison function for qsort. We want a ascending luid
7072 when scanning the array starting at the end, thus the arguments are
7073 used in reverse. */
7074static int
7075cmp_recombine_givs_stats (x, y)
7076 struct recombine_givs_stats *x, *y;
7077{
7078 int d;
7079 d = y->start_luid - x->start_luid;
7080 /* Stabilize the sort. */
7081 if (!d)
7082 d = y->giv_number - x->giv_number;
7083 return d;
7084}
7085
7086/* Scan X, which is a part of INSN, for the end of life of a giv. Also
7087 look for the start of life of a giv where the start has not been seen
7088 yet to unlock the search for the end of its life.
7089 Only consider givs that belong to BIV.
7090 Return the total number of lifetime ends that have been found. */
7091static int
7092find_life_end (x, stats, insn, biv)
7093 rtx x, insn, biv;
7094 struct recombine_givs_stats *stats;
7095{
7096 enum rtx_code code;
7097 char *fmt;
7098 int i, j;
7099 int retval;
7100
7101 code = GET_CODE (x);
7102 switch (code)
7103 {
7104 case SET:
7105 {
7106 rtx reg = SET_DEST (x);
7107 if (GET_CODE (reg) == REG)
7108 {
7109 int regno = REGNO (reg);
7110 struct induction *v = REG_IV_INFO (regno);
7111
7112 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7113 && ! v->ignore
7114 && v->src_reg == biv
7115 && stats[v->ix].end_luid <= 0)
7116 {
7117 /* If we see a 0 here for end_luid, it means that we have
7118 scanned the entire loop without finding any use at all.
7119 We must not predicate this code on a start_luid match
7120 since that would make the test fail for givs that have
7121 been hoisted out of inner loops. */
7122 if (stats[v->ix].end_luid == 0)
7123 {
7124 stats[v->ix].end_luid = stats[v->ix].start_luid;
7125 return 1 + find_life_end (SET_SRC (x), stats, insn, biv);
7126 }
7127 else if (stats[v->ix].start_luid == INSN_LUID (insn))
7128 stats[v->ix].end_luid = 0;
7129 }
7130 return find_life_end (SET_SRC (x), stats, insn, biv);
7131 }
7132 break;
7133 }
7134 case REG:
7135 {
7136 int regno = REGNO (x);
7137 struct induction *v = REG_IV_INFO (regno);
7138
7139 if (REG_IV_TYPE (regno) == GENERAL_INDUCT
7140 && ! v->ignore
7141 && v->src_reg == biv
7142 && stats[v->ix].end_luid == 0)
7143 {
7144 while (INSN_UID (insn) >= max_uid_for_loop)
7145 insn = NEXT_INSN (insn);
7146 stats[v->ix].end_luid = INSN_LUID (insn);
7147 return 1;
7148 }
7149 return 0;
7150 }
7151 case LABEL_REF:
7152 case CONST_DOUBLE:
7153 case CONST_INT:
7154 case CONST:
7155 return 0;
7156 default:
7157 break;
7158 }
7159 fmt = GET_RTX_FORMAT (code);
7160 retval = 0;
7161 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7162 {
7163 if (fmt[i] == 'e')
7164 retval += find_life_end (XEXP (x, i), stats, insn, biv);
7165
7166 else if (fmt[i] == 'E')
7167 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7168 retval += find_life_end (XVECEXP (x, i, j), stats, insn, biv);
7169 }
7170 return retval;
7171}
7172
7173/* For each giv that has been combined with another, look if
7174 we can combine it with the most recently used one instead.
7175 This tends to shorten giv lifetimes, and helps the next step:
7176 try to derive givs from other givs. */
7177static void
53dc05e4 7178recombine_givs (bl, loop_start, loop_end, unroll_p)
3ec2b590
R
7179 struct iv_class *bl;
7180 rtx loop_start, loop_end;
53dc05e4 7181 int unroll_p;
3ec2b590
R
7182{
7183 struct induction *v, **giv_array, *last_giv;
7184 struct recombine_givs_stats *stats;
7185 int giv_count;
7186 int i, rescan;
7187 int ends_need_computing;
7188
7189 for (giv_count = 0, v = bl->giv; v; v = v->next_iv)
7190 {
7191 if (! v->ignore)
7192 giv_count++;
7193 }
7194 giv_array
7195 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7196 stats = (struct recombine_givs_stats *) alloca (giv_count * sizeof *stats);
7197
7198 /* Initialize stats and set up the ix field for each giv in stats to name
7199 the corresponding index into stats. */
7200 for (i = 0, v = bl->giv; v; v = v->next_iv)
7201 {
7202 rtx p;
7203
7204 if (v->ignore)
7205 continue;
7206 giv_array[i] = v;
7207 stats[i].giv_number = i;
7208 /* If this giv has been hoisted out of an inner loop, use the luid of
7209 the previous insn. */
7210 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7211 p = PREV_INSN (p);
7212 stats[i].start_luid = INSN_LUID (p);
3ec2b590
R
7213 i++;
7214 }
7215
7216 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7217
41c1b007
R
7218 /* Set up the ix field for each giv in stats to name
7219 the corresponding index into stats, and
7220 do the actual most-recently-used recombination. */
3ec2b590
R
7221 for (last_giv = 0, i = giv_count - 1; i >= 0; i--)
7222 {
7223 v = giv_array[stats[i].giv_number];
41c1b007 7224 v->ix = i;
3ec2b590
R
7225 if (v->same)
7226 {
7227 struct induction *old_same = v->same;
7228 rtx new_combine;
7229
7230 /* combine_givs_p actually says if we can make this transformation.
7231 The other tests are here only to avoid keeping a giv alive
7232 that could otherwise be eliminated. */
7233 if (last_giv
7234 && ((old_same->maybe_dead && ! old_same->combined_with)
7235 || ! last_giv->maybe_dead
7236 || last_giv->combined_with)
7237 && (new_combine = combine_givs_p (last_giv, v)))
7238 {
7239 old_same->combined_with--;
7240 v->new_reg = new_combine;
7241 v->same = last_giv;
7242 last_giv->combined_with++;
7243 /* No need to update lifetimes / benefits here since we have
7244 already decided what to reduce. */
516e5fa6
RH
7245
7246 if (loop_dump_stream)
7247 {
7248 fprintf (loop_dump_stream,
7249 "giv at %d recombined with giv at %d as ",
7250 INSN_UID (v->insn), INSN_UID (last_giv->insn));
7251 print_rtl (loop_dump_stream, v->new_reg);
7252 putc ('\n', loop_dump_stream);
7253 }
3ec2b590
R
7254 continue;
7255 }
7256 v = v->same;
7257 }
7258 else if (v->giv_type != DEST_REG)
7259 continue;
7260 if (! last_giv
7261 || (last_giv->maybe_dead && ! last_giv->combined_with)
7262 || ! v->maybe_dead
7263 || v->combined_with)
7264 last_giv = v;
7265 }
7266
7267 ends_need_computing = 0;
7268 /* For each DEST_REG giv, compute lifetime starts, and try to compute
7269 lifetime ends from regscan info. */
7270 for (i = 0, v = bl->giv; v; v = v->next_iv)
7271 {
7272 if (v->ignore)
7273 continue;
7274 if (v->giv_type == DEST_ADDR)
7275 {
7276 /* Loop unrolling of an inner loop can even create new DEST_REG
7277 givs. */
7278 rtx p;
7279 for (p = v->insn; INSN_UID (p) >= max_uid_for_loop; )
7280 p = PREV_INSN (p);
7281 stats[i].start_luid = stats[i].end_luid = INSN_LUID (p);
7282 if (p != v->insn)
7283 stats[i].end_luid++;
7284 }
7285 else /* v->giv_type == DEST_REG */
7286 {
7287 if (v->last_use)
7288 {
7289 stats[i].start_luid = INSN_LUID (v->insn);
7290 stats[i].end_luid = INSN_LUID (v->last_use);
7291 }
7292 else if (INSN_UID (v->insn) >= max_uid_for_loop)
7293 {
7294 rtx p;
7295 /* This insn has been created by loop optimization on an inner
7296 loop. We don't have a proper start_luid that will match
7297 when we see the first set. But we do know that there will
7298 be no use before the set, so we can set end_luid to 0 so that
7299 we'll start looking for the last use right away. */
7300 for (p = PREV_INSN (v->insn); INSN_UID (p) >= max_uid_for_loop; )
7301 p = PREV_INSN (p);
7302 stats[i].start_luid = INSN_LUID (p);
7303 stats[i].end_luid = 0;
7304 ends_need_computing++;
7305 }
7306 else
7307 {
7308 int regno = REGNO (v->dest_reg);
7309 int count = VARRAY_INT (n_times_set, regno) - 1;
7310 rtx p = v->insn;
7311
7312 /* Find the first insn that sets the giv, so that we can verify
7313 if this giv's lifetime wraps around the loop. We also need
7314 the luid of the first setting insn in order to detect the
7315 last use properly. */
7316 while (count)
7317 {
7318 p = prev_nonnote_insn (p);
7319 if (reg_set_p (v->dest_reg, p))
7320 count--;
7321 }
7322
7323 stats[i].start_luid = INSN_LUID (p);
7324 if (stats[i].start_luid > uid_luid[REGNO_FIRST_UID (regno)])
7325 {
7326 stats[i].end_luid = -1;
7327 ends_need_computing++;
7328 }
7329 else
7330 {
7331 stats[i].end_luid = uid_luid[REGNO_LAST_UID (regno)];
7332 if (stats[i].end_luid > INSN_LUID (loop_end))
7333 {
7334 stats[i].end_luid = -1;
7335 ends_need_computing++;
7336 }
7337 }
7338 }
7339 }
7340 i++;
7341 }
7342
7343 /* If the regscan information was unconclusive for one or more DEST_REG
7344 givs, scan the all insn in the loop to find out lifetime ends. */
7345 if (ends_need_computing)
7346 {
7347 rtx biv = bl->biv->src_reg;
7348 rtx p = loop_end;
7349
7350 do
7351 {
7352 if (p == loop_start)
7353 p = loop_end;
7354 p = PREV_INSN (p);
7355 if (GET_RTX_CLASS (GET_CODE (p)) != 'i')
7356 continue;
7357 ends_need_computing -= find_life_end (PATTERN (p), stats, p, biv);
7358 }
7359 while (ends_need_computing);
7360 }
7361
7362 /* Set start_luid back to the last insn that sets the giv. This allows
7363 more combinations. */
7364 for (i = 0, v = bl->giv; v; v = v->next_iv)
7365 {
7366 if (v->ignore)
7367 continue;
7368 if (INSN_UID (v->insn) < max_uid_for_loop)
7369 stats[i].start_luid = INSN_LUID (v->insn);
7370 i++;
7371 }
7372
7373 /* Now adjust lifetime ends by taking combined givs into account. */
7374 for (i = 0, v = bl->giv; v; v = v->next_iv)
7375 {
7376 unsigned luid;
7377 int j;
7378
7379 if (v->ignore)
7380 continue;
7381 if (v->same && ! v->same->ignore)
7382 {
7383 j = v->same->ix;
7384 luid = stats[i].start_luid;
7385 /* Use unsigned arithmetic to model loop wrap-around. */
7386 if (luid - stats[j].start_luid
7387 > (unsigned) stats[j].end_luid - stats[j].start_luid)
7388 stats[j].end_luid = luid;
7389 }
7390 i++;
7391 }
7392
7393 qsort (stats, giv_count, sizeof(*stats), cmp_recombine_givs_stats);
7394
7395 /* Try to derive DEST_REG givs from previous DEST_REG givs with the
7396 same mult_val and non-overlapping lifetime. This reduces register
7397 pressure.
7398 Once we find a DEST_REG giv that is suitable to derive others from,
7399 we set last_giv to this giv, and try to derive as many other DEST_REG
7400 givs from it without joining overlapping lifetimes. If we then
7401 encounter a DEST_REG giv that we can't derive, we set rescan to the
7402 index for this giv (unless rescan is already set).
7403 When we are finished with the current LAST_GIV (i.e. the inner loop
7404 terminates), we start again with rescan, which then becomes the new
7405 LAST_GIV. */
7406 for (i = giv_count - 1; i >= 0; i = rescan)
7407 {
7408 int life_start, life_end;
7409
7410 for (last_giv = 0, rescan = -1; i >= 0; i--)
7411 {
7412 rtx sum;
7413
7414 v = giv_array[stats[i].giv_number];
4d87f7a7 7415 if (v->giv_type != DEST_REG || v->derived_from || v->same)
3ec2b590
R
7416 continue;
7417 if (! last_giv)
7418 {
7221f080
R
7419 /* Don't use a giv that's likely to be dead to derive
7420 others - that would be likely to keep that giv alive. */
7421 if (! v->maybe_dead || v->combined_with)
7422 {
7423 last_giv = v;
7424 life_start = stats[i].start_luid;
7425 life_end = stats[i].end_luid;
7426 }
3ec2b590
R
7427 continue;
7428 }
7429 /* Use unsigned arithmetic to model loop wrap around. */
7430 if (((unsigned) stats[i].start_luid - life_start
7431 >= (unsigned) life_end - life_start)
7432 && ((unsigned) stats[i].end_luid - life_start
7221f080
R
7433 > (unsigned) life_end - life_start)
7434 /* Check that the giv insn we're about to use for deriving
7435 precedes all uses of that giv. Note that initializing the
7436 derived giv would defeat the purpose of reducing register
7437 pressure.
7438 ??? We could arrange to move the insn. */
7439 && ((unsigned) stats[i].end_luid - INSN_LUID (loop_start)
7440 > (unsigned) stats[i].start_luid - INSN_LUID (loop_start))
3ec2b590
R
7441 && rtx_equal_p (last_giv->mult_val, v->mult_val)
7442 /* ??? Could handle libcalls, but would need more logic. */
7443 && ! find_reg_note (v->insn, REG_RETVAL, NULL_RTX)
7444 /* We would really like to know if for any giv that v
7445 is combined with, v->insn or any intervening biv increment
7446 dominates that combined giv. However, we
7447 don't have this detailed control flow information.
7448 N.B. since last_giv will be reduced, it is valid
7449 anywhere in the loop, so we don't need to check the
7221f080
R
7450 validity of last_giv.
7451 We rely here on the fact that v->always_executed implies that
7452 there is no jump to someplace else in the loop before the
7453 giv insn, and hence any insn that is executed before the
7454 giv insn in the loop will have a lower luid. */
3ec2b590
R
7455 && (v->always_executed || ! v->combined_with)
7456 && (sum = express_from (last_giv, v))
53dc05e4
R
7457 /* Make sure we don't make the add more expensive. ADD_COST
7458 doesn't take different costs of registers and constants into
7459 account, so compare the cost of the actual SET_SRCs. */
7460 && (rtx_cost (sum, SET)
7461 <= rtx_cost (SET_SRC (single_set (v->insn)), SET))
7462 /* ??? unroll can't understand anything but reg + const_int
7463 sums. It would be cleaner to fix unroll. */
7464 && ((GET_CODE (sum) == PLUS
7465 && GET_CODE (XEXP (sum, 0)) == REG
7466 && GET_CODE (XEXP (sum, 1)) == CONST_INT)
7467 || ! unroll_p)
3ec2b590 7468 && validate_change (v->insn, &PATTERN (v->insn),
743f9f5d 7469 gen_rtx_SET (VOIDmode, v->dest_reg, sum), 0))
3ec2b590 7470 {
4d87f7a7 7471 v->derived_from = last_giv;
3ec2b590 7472 life_end = stats[i].end_luid;
516e5fa6
RH
7473
7474 if (loop_dump_stream)
7475 {
7476 fprintf (loop_dump_stream,
7477 "giv at %d derived from %d as ",
7478 INSN_UID (v->insn), INSN_UID (last_giv->insn));
743f9f5d 7479 print_rtl (loop_dump_stream, sum);
516e5fa6
RH
7480 putc ('\n', loop_dump_stream);
7481 }
3ec2b590
R
7482 }
7483 else if (rescan < 0)
7484 rescan = i;
7485 }
7486 }
7487}
7488\f
b4ad7b23
RS
7489/* EMIT code before INSERT_BEFORE to set REG = B * M + A. */
7490
7491void
7492emit_iv_add_mult (b, m, a, reg, insert_before)
7493 rtx b; /* initial value of basic induction variable */
7494 rtx m; /* multiplicative constant */
7495 rtx a; /* additive constant */
7496 rtx reg; /* destination register */
7497 rtx insert_before;
7498{
7499 rtx seq;
7500 rtx result;
7501
7502 /* Prevent unexpected sharing of these rtx. */
7503 a = copy_rtx (a);
7504 b = copy_rtx (b);
7505
0f41302f 7506 /* Increase the lifetime of any invariants moved further in code. */
b4ad7b23
RS
7507 update_reg_last_use (a, insert_before);
7508 update_reg_last_use (b, insert_before);
7509 update_reg_last_use (m, insert_before);
7510
7511 start_sequence ();
7512 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
7513 if (reg != result)
7514 emit_move_insn (reg, result);
7515 seq = gen_sequence ();
7516 end_sequence ();
7517
7518 emit_insn_before (seq, insert_before);
9ae8ffe7 7519
00116a7b
RH
7520 /* It is entirely possible that the expansion created lots of new
7521 registers. Iterate over the sequence we just created and
7522 record them all. */
7523
7524 if (GET_CODE (seq) == SEQUENCE)
7525 {
7526 int i;
7527 for (i = 0; i < XVECLEN (seq, 0); ++i)
7528 {
7529 rtx set = single_set (XVECEXP (seq, 0, i));
7530 if (set && GET_CODE (SET_DEST (set)) == REG)
7531 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7532 }
7533 }
7534 else if (GET_CODE (seq) == SET
7535 && GET_CODE (SET_DEST (seq)) == REG)
7536 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
b4ad7b23
RS
7537}
7538\f
7539/* Test whether A * B can be computed without
7540 an actual multiply insn. Value is 1 if so. */
7541
7542static int
7543product_cheap_p (a, b)
7544 rtx a;
7545 rtx b;
7546{
7547 int i;
7548 rtx tmp;
7549 struct obstack *old_rtl_obstack = rtl_obstack;
7550 char *storage = (char *) obstack_alloc (&temp_obstack, 0);
7551 int win = 1;
7552
0f41302f 7553 /* If only one is constant, make it B. */
b4ad7b23
RS
7554 if (GET_CODE (a) == CONST_INT)
7555 tmp = a, a = b, b = tmp;
7556
7557 /* If first constant, both constant, so don't need multiply. */
7558 if (GET_CODE (a) == CONST_INT)
7559 return 1;
7560
7561 /* If second not constant, neither is constant, so would need multiply. */
7562 if (GET_CODE (b) != CONST_INT)
7563 return 0;
7564
7565 /* One operand is constant, so might not need multiply insn. Generate the
7566 code for the multiply and see if a call or multiply, or long sequence
7567 of insns is generated. */
7568
7569 rtl_obstack = &temp_obstack;
7570 start_sequence ();
5fd8383e 7571 expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
b4ad7b23
RS
7572 tmp = gen_sequence ();
7573 end_sequence ();
7574
7575 if (GET_CODE (tmp) == SEQUENCE)
7576 {
7577 if (XVEC (tmp, 0) == 0)
7578 win = 1;
7579 else if (XVECLEN (tmp, 0) > 3)
7580 win = 0;
7581 else
7582 for (i = 0; i < XVECLEN (tmp, 0); i++)
7583 {
7584 rtx insn = XVECEXP (tmp, 0, i);
7585
7586 if (GET_CODE (insn) != INSN
7587 || (GET_CODE (PATTERN (insn)) == SET
7588 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7589 || (GET_CODE (PATTERN (insn)) == PARALLEL
7590 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7591 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7592 {
7593 win = 0;
7594 break;
7595 }
7596 }
7597 }
7598 else if (GET_CODE (tmp) == SET
7599 && GET_CODE (SET_SRC (tmp)) == MULT)
7600 win = 0;
7601 else if (GET_CODE (tmp) == PARALLEL
7602 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7603 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7604 win = 0;
7605
7606 /* Free any storage we obtained in generating this multiply and restore rtl
7607 allocation to its normal obstack. */
7608 obstack_free (&temp_obstack, storage);
7609 rtl_obstack = old_rtl_obstack;
7610
7611 return win;
7612}
7613\f
7614/* Check to see if loop can be terminated by a "decrement and branch until
7615 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7616 Also try reversing an increment loop to a decrement loop
7617 to see if the optimization can be performed.
7618 Value is nonzero if optimization was performed. */
7619
7620/* This is useful even if the architecture doesn't have such an insn,
7621 because it might change a loops which increments from 0 to n to a loop
7622 which decrements from n to 0. A loop that decrements to zero is usually
7623 faster than one that increments from zero. */
7624
7625/* ??? This could be rewritten to use some of the loop unrolling procedures,
7626 such as approx_final_value, biv_total_increment, loop_iterations, and
7627 final_[bg]iv_value. */
7628
7629static int
5629b16c 7630check_dbra_loop (loop_end, insn_count, loop_start, loop_info)
b4ad7b23
RS
7631 rtx loop_end;
7632 int insn_count;
7633 rtx loop_start;
5629b16c 7634 struct loop_info *loop_info;
b4ad7b23
RS
7635{
7636 struct iv_class *bl;
7637 rtx reg;
7638 rtx jump_label;
7639 rtx final_value;
7640 rtx start_value;
b4ad7b23
RS
7641 rtx new_add_val;
7642 rtx comparison;
7643 rtx before_comparison;
7644 rtx p;
0628fde6
JW
7645 rtx jump;
7646 rtx first_compare;
7647 int compare_and_branch;
b4ad7b23
RS
7648
7649 /* If last insn is a conditional branch, and the insn before tests a
7650 register value, try to optimize it. Otherwise, we can't do anything. */
7651
0628fde6
JW
7652 jump = PREV_INSN (loop_end);
7653 comparison = get_condition_for_loop (jump);
b4ad7b23
RS
7654 if (comparison == 0)
7655 return 0;
7656
0628fde6
JW
7657 /* Try to compute whether the compare/branch at the loop end is one or
7658 two instructions. */
7659 get_condition (jump, &first_compare);
7660 if (first_compare == jump)
7661 compare_and_branch = 1;
7662 else if (first_compare == prev_nonnote_insn (jump))
7663 compare_and_branch = 2;
7664 else
7665 return 0;
7666
b4ad7b23
RS
7667 /* Check all of the bivs to see if the compare uses one of them.
7668 Skip biv's set more than once because we can't guarantee that
7669 it will be zero on the last iteration. Also skip if the biv is
7670 used between its update and the test insn. */
7671
7672 for (bl = loop_iv_list; bl; bl = bl->next)
7673 {
7674 if (bl->biv_count == 1
7675 && bl->biv->dest_reg == XEXP (comparison, 0)
7676 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
0628fde6 7677 first_compare))
b4ad7b23
RS
7678 break;
7679 }
7680
7681 if (! bl)
7682 return 0;
7683
7684 /* Look for the case where the basic induction variable is always
7685 nonnegative, and equals zero on the last iteration.
7686 In this case, add a reg_note REG_NONNEG, which allows the
7687 m68k DBRA instruction to be used. */
7688
7689 if (((GET_CODE (comparison) == GT
7690 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7691 && INTVAL (XEXP (comparison, 1)) == -1)
7692 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7693 && GET_CODE (bl->biv->add_val) == CONST_INT
7694 && INTVAL (bl->biv->add_val) < 0)
7695 {
7696 /* Initial value must be greater than 0,
7697 init_val % -dec_value == 0 to ensure that it equals zero on
7698 the last iteration */
7699
7700 if (GET_CODE (bl->initial_value) == CONST_INT
7701 && INTVAL (bl->initial_value) > 0
db3cf6fb
MS
7702 && (INTVAL (bl->initial_value)
7703 % (-INTVAL (bl->biv->add_val))) == 0)
b4ad7b23
RS
7704 {
7705 /* register always nonnegative, add REG_NOTE to branch */
7706 REG_NOTES (PREV_INSN (loop_end))
38a448ca
RH
7707 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
7708 REG_NOTES (PREV_INSN (loop_end)));
b4ad7b23
RS
7709 bl->nonneg = 1;
7710
7711 return 1;
7712 }
7713
7714 /* If the decrement is 1 and the value was tested as >= 0 before
7715 the loop, then we can safely optimize. */
7716 for (p = loop_start; p; p = PREV_INSN (p))
7717 {
7718 if (GET_CODE (p) == CODE_LABEL)
7719 break;
7720 if (GET_CODE (p) != JUMP_INSN)
7721 continue;
7722
7723 before_comparison = get_condition_for_loop (p);
7724 if (before_comparison
7725 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7726 && GET_CODE (before_comparison) == LT
7727 && XEXP (before_comparison, 1) == const0_rtx
7728 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7729 && INTVAL (bl->biv->add_val) == -1)
7730 {
7731 REG_NOTES (PREV_INSN (loop_end))
38a448ca
RH
7732 = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
7733 REG_NOTES (PREV_INSN (loop_end)));
b4ad7b23
RS
7734 bl->nonneg = 1;
7735
7736 return 1;
7737 }
7738 }
7739 }
c48ba252 7740 else if (INTVAL (bl->biv->add_val) > 0)
b4ad7b23
RS
7741 {
7742 /* Try to change inc to dec, so can apply above optimization. */
7743 /* Can do this if:
7744 all registers modified are induction variables or invariant,
7745 all memory references have non-overlapping addresses
7746 (obviously true if only one write)
7747 allow 2 insns for the compare/jump at the end of the loop. */
45cc060e
JW
7748 /* Also, we must avoid any instructions which use both the reversed
7749 biv and another biv. Such instructions will fail if the loop is
7750 reversed. We meet this condition by requiring that either
7751 no_use_except_counting is true, or else that there is only
7752 one biv. */
b4ad7b23
RS
7753 int num_nonfixed_reads = 0;
7754 /* 1 if the iteration var is used only to count iterations. */
7755 int no_use_except_counting = 0;
b418c26e
JW
7756 /* 1 if the loop has no memory store, or it has a single memory store
7757 which is reversible. */
7758 int reversible_mem_store = 1;
b4ad7b23 7759
b4ad7b23 7760 if (bl->giv_count == 0
353127c2 7761 && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
b4ad7b23
RS
7762 {
7763 rtx bivreg = regno_reg_rtx[bl->regno];
7764
7765 /* If there are no givs for this biv, and the only exit is the
38e01259 7766 fall through at the end of the loop, then
b4ad7b23
RS
7767 see if perhaps there are no uses except to count. */
7768 no_use_except_counting = 1;
7769 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7770 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7771 {
7772 rtx set = single_set (p);
7773
7774 if (set && GET_CODE (SET_DEST (set)) == REG
7775 && REGNO (SET_DEST (set)) == bl->regno)
7776 /* An insn that sets the biv is okay. */
7777 ;
7778 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
7779 || p == prev_nonnote_insn (loop_end))
7780 /* Don't bother about the end test. */
7781 ;
7782 else if (reg_mentioned_p (bivreg, PATTERN (p)))
b4ad7b23
RS
7783 {
7784 no_use_except_counting = 0;
7785 break;
7786 }
7787 }
7788 }
7789
c48ba252
R
7790 if (no_use_except_counting)
7791 ; /* no need to worry about MEMs. */
7792 else if (num_mem_sets <= 1)
7793 {
7794 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
7795 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
7796 num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
7797
7798 /* If the loop has a single store, and the destination address is
7799 invariant, then we can't reverse the loop, because this address
7800 might then have the wrong value at loop exit.
7801 This would work if the source was invariant also, however, in that
7802 case, the insn should have been moved out of the loop. */
7803
7804 if (num_mem_sets == 1)
2d4fde68
R
7805 {
7806 struct induction *v;
7807
7808 reversible_mem_store
7809 = (! unknown_address_altered
ef1d4aec 7810 && ! invariant_p (XEXP (XEXP (loop_store_mems, 0), 0)));
2d4fde68
R
7811
7812 /* If the store depends on a register that is set after the
7813 store, it depends on the initial value, and is thus not
7814 reversible. */
7815 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
7816 {
7817 if (v->giv_type == DEST_REG
7818 && reg_mentioned_p (v->dest_reg,
7819 XEXP (loop_store_mems, 0))
1cb1fe66 7820 && loop_insn_first_p (first_loop_store_insn, v->insn))
2d4fde68
R
7821 reversible_mem_store = 0;
7822 }
7823 }
c48ba252
R
7824 }
7825 else
7826 return 0;
b418c26e 7827
b4ad7b23
RS
7828 /* This code only acts for innermost loops. Also it simplifies
7829 the memory address check by only reversing loops with
7830 zero or one memory access.
7831 Two memory accesses could involve parts of the same array,
c48ba252
R
7832 and that can't be reversed.
7833 If the biv is used only for counting, than we don't need to worry
7834 about all these things. */
7835
7836 if ((num_nonfixed_reads <= 1
7837 && !loop_has_call
7838 && !loop_has_volatile
7839 && reversible_mem_store
7840 && (bl->giv_count + bl->biv_count + num_mem_sets
7841 + num_movables + compare_and_branch == insn_count)
7842 && (bl == loop_iv_list && bl->next == 0))
7843 || no_use_except_counting)
b4ad7b23 7844 {
b4ad7b23
RS
7845 rtx tem;
7846
7847 /* Loop can be reversed. */
7848 if (loop_dump_stream)
7849 fprintf (loop_dump_stream, "Can reverse loop\n");
7850
7851 /* Now check other conditions:
e9a25f70 7852
956d6950
JL
7853 The increment must be a constant, as must the initial value,
7854 and the comparison code must be LT.
b4ad7b23
RS
7855
7856 This test can probably be improved since +/- 1 in the constant
7857 can be obtained by changing LT to LE and vice versa; this is
7858 confusing. */
7859
e9a25f70 7860 if (comparison
c48ba252
R
7861 /* for constants, LE gets turned into LT */
7862 && (GET_CODE (comparison) == LT
7863 || (GET_CODE (comparison) == LE
7864 && no_use_except_counting)))
b4ad7b23 7865 {
c48ba252
R
7866 HOST_WIDE_INT add_val, add_adjust, comparison_val;
7867 rtx initial_value, comparison_value;
7868 int nonneg = 0;
7869 enum rtx_code cmp_code;
7870 int comparison_const_width;
7871 unsigned HOST_WIDE_INT comparison_sign_mask;
e9a25f70
JL
7872
7873 add_val = INTVAL (bl->biv->add_val);
c48ba252 7874 comparison_value = XEXP (comparison, 1);
2c74fb2b
AS
7875 if (GET_MODE (comparison_value) == VOIDmode)
7876 comparison_const_width
7877 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
7878 else
7879 comparison_const_width
7880 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
c48ba252
R
7881 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
7882 comparison_const_width = HOST_BITS_PER_WIDE_INT;
7883 comparison_sign_mask
7884 = (unsigned HOST_WIDE_INT)1 << (comparison_const_width - 1);
7885
3aa94dc8
JL
7886 /* If the comparison value is not a loop invariant, then we
7887 can not reverse this loop.
7888
7889 ??? If the insns which initialize the comparison value as
7890 a whole compute an invariant result, then we could move
7891 them out of the loop and proceed with loop reversal. */
9231189b 7892 if (!invariant_p (comparison_value))
3aa94dc8
JL
7893 return 0;
7894
c48ba252
R
7895 if (GET_CODE (comparison_value) == CONST_INT)
7896 comparison_val = INTVAL (comparison_value);
e9a25f70
JL
7897 initial_value = bl->initial_value;
7898
a8decb2c
JL
7899 /* Normalize the initial value if it is an integer and
7900 has no other use except as a counter. This will allow
7901 a few more loops to be reversed. */
7902 if (no_use_except_counting
c48ba252 7903 && GET_CODE (comparison_value) == CONST_INT
a8decb2c 7904 && GET_CODE (initial_value) == CONST_INT)
e9a25f70
JL
7905 {
7906 comparison_val = comparison_val - INTVAL (bl->initial_value);
c48ba252
R
7907 /* The code below requires comparison_val to be a multiple
7908 of add_val in order to do the loop reversal, so
7909 round up comparison_val to a multiple of add_val.
7910 Since comparison_value is constant, we know that the
7911 current comparison code is LT. */
7912 comparison_val = comparison_val + add_val - 1;
7913 comparison_val
7914 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
7915 /* We postpone overflow checks for COMPARISON_VAL here;
7916 even if there is an overflow, we might still be able to
7917 reverse the loop, if converting the loop exit test to
7918 NE is possible. */
7919 initial_value = const0_rtx;
e9a25f70
JL
7920 }
7921
c48ba252
R
7922 /* First check if we can do a vanilla loop reversal. */
7923 if (initial_value == const0_rtx
7924 /* If we have a decrement_and_branch_on_count, prefer
7925 the NE test, since this will allow that instruction to
c5cbf81e
JL
7926 be generated. Note that we must use a vanilla loop
7927 reversal if the biv is used to calculate a giv or has
7928 a non-counting use. */
5accd822 7929#if ! defined (HAVE_decrement_and_branch_until_zero) && defined (HAVE_decrement_and_branch_on_count)
35704c46 7930 && (! (add_val == 1 && loop_info->vtop
c5cbf81e
JL
7931 && (bl->biv_count == 0
7932 || no_use_except_counting)))
c48ba252
R
7933#endif
7934 && GET_CODE (comparison_value) == CONST_INT
7935 /* Now do postponed overflow checks on COMPARISON_VAL. */
7936 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
7937 & comparison_sign_mask))
7938 {
7939 /* Register will always be nonnegative, with value
7940 0 on last iteration */
7941 add_adjust = add_val;
7942 nonneg = 1;
7943 cmp_code = GE;
7944 }
35704c46 7945 else if (add_val == 1 && loop_info->vtop
c5cbf81e
JL
7946 && (bl->biv_count == 0
7947 || no_use_except_counting))
c48ba252
R
7948 {
7949 add_adjust = 0;
7950 cmp_code = NE;
7951 }
7952 else
7953 return 0;
7954
7955 if (GET_CODE (comparison) == LE)
7956 add_adjust -= add_val;
7957
e9a25f70
JL
7958 /* If the initial value is not zero, or if the comparison
7959 value is not an exact multiple of the increment, then we
7960 can not reverse this loop. */
c48ba252
R
7961 if (initial_value == const0_rtx
7962 && GET_CODE (comparison_value) == CONST_INT)
7963 {
7964 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
7965 return 0;
7966 }
7967 else
7968 {
7969 if (! no_use_except_counting || add_val != 1)
7970 return 0;
7971 }
e9a25f70 7972
8ed69d09
R
7973 final_value = comparison_value;
7974
e9a25f70
JL
7975 /* Reset these in case we normalized the initial value
7976 and comparison value above. */
8ed69d09
R
7977 if (GET_CODE (comparison_value) == CONST_INT
7978 && GET_CODE (initial_value) == CONST_INT)
7979 {
7980 comparison_value = GEN_INT (comparison_val);
7981 final_value
7982 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
7983 }
e9a25f70 7984 bl->initial_value = initial_value;
b4ad7b23
RS
7985
7986 /* Save some info needed to produce the new insns. */
7987 reg = bl->biv->dest_reg;
7988 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
3c2f289c
RK
7989 if (jump_label == pc_rtx)
7990 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
5fd8383e 7991 new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
b4ad7b23 7992
c48ba252
R
7993 /* Set start_value; if this is not a CONST_INT, we need
7994 to generate a SUB.
7995 Initialize biv to start_value before loop start.
b4ad7b23
RS
7996 The old initializing insn will be deleted as a
7997 dead store by flow.c. */
c48ba252
R
7998 if (initial_value == const0_rtx
7999 && GET_CODE (comparison_value) == CONST_INT)
8000 {
8001 start_value = GEN_INT (comparison_val - add_adjust);
8002 emit_insn_before (gen_move_insn (reg, start_value),
8003 loop_start);
8004 }
8005 else if (GET_CODE (initial_value) == CONST_INT)
8006 {
8007 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8008 enum machine_mode mode = GET_MODE (reg);
8009 enum insn_code icode
8010 = add_optab->handlers[(int) mode].insn_code;
8011 if (! (*insn_operand_predicate[icode][0]) (reg, mode)
8012 || ! ((*insn_operand_predicate[icode][1])
8013 (comparison_value, mode))
8014 || ! (*insn_operand_predicate[icode][2]) (offset, mode))
8015 return 0;
8016 start_value
8017 = gen_rtx_PLUS (mode, comparison_value, offset);
8018 emit_insn_before ((GEN_FCN (icode)
8019 (reg, comparison_value, offset)),
8020 loop_start);
8021 if (GET_CODE (comparison) == LE)
8022 final_value = gen_rtx_PLUS (mode, comparison_value,
8023 GEN_INT (add_val));
8024 }
8025 else if (! add_adjust)
8026 {
8027 enum machine_mode mode = GET_MODE (reg);
8028 enum insn_code icode
8029 = sub_optab->handlers[(int) mode].insn_code;
8030 if (! (*insn_operand_predicate[icode][0]) (reg, mode)
8031 || ! ((*insn_operand_predicate[icode][1])
8032 (comparison_value, mode))
8033 || ! ((*insn_operand_predicate[icode][2])
8034 (initial_value, mode)))
8035 return 0;
8036 start_value
8037 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8038 emit_insn_before ((GEN_FCN (icode)
8039 (reg, comparison_value, initial_value)),
8040 loop_start);
8041 }
8042 else
8043 /* We could handle the other cases too, but it'll be
8044 better to have a testcase first. */
8045 return 0;
b4ad7b23 8046
225a7e3d
JL
8047 /* We may not have a single insn which can increment a reg, so
8048 create a sequence to hold all the insns from expand_inc. */
8049 start_sequence ();
8050 expand_inc (reg, new_add_val);
8051 tem = gen_sequence ();
8052 end_sequence ();
8053
8054 p = emit_insn_before (tem, bl->biv->insn);
b4ad7b23
RS
8055 delete_insn (bl->biv->insn);
8056
8057 /* Update biv info to reflect its new status. */
8058 bl->biv->insn = p;
8059 bl->initial_value = start_value;
8060 bl->biv->add_val = new_add_val;
8061
5629b16c 8062 /* Update loop info. */
eb6a3bc0
MH
8063 loop_info->initial_value = reg;
8064 loop_info->initial_equiv_value = reg;
5629b16c
MH
8065 loop_info->final_value = const0_rtx;
8066 loop_info->final_equiv_value = const0_rtx;
8067 loop_info->comparison_value = const0_rtx;
8068 loop_info->comparison_code = cmp_code;
8069 loop_info->increment = new_add_val;
8070
b4ad7b23
RS
8071 /* Inc LABEL_NUSES so that delete_insn will
8072 not delete the label. */
8073 LABEL_NUSES (XEXP (jump_label, 0)) ++;
8074
8075 /* Emit an insn after the end of the loop to set the biv's
8076 proper exit value if it is used anywhere outside the loop. */
0628fde6 8077 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
b4ad7b23 8078 || ! bl->init_insn
b1f21e0a 8079 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
b4ad7b23
RS
8080 emit_insn_after (gen_move_insn (reg, final_value),
8081 loop_end);
8082
8083 /* Delete compare/branch at end of loop. */
8084 delete_insn (PREV_INSN (loop_end));
0628fde6
JW
8085 if (compare_and_branch == 2)
8086 delete_insn (first_compare);
b4ad7b23
RS
8087
8088 /* Add new compare/branch insn at end of loop. */
8089 start_sequence ();
362cc3d4
MH
8090 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8091 GET_MODE (reg), 0, 0,
8092 XEXP (jump_label, 0));
b4ad7b23
RS
8093 tem = gen_sequence ();
8094 end_sequence ();
8095 emit_jump_insn_before (tem, loop_end);
8096
a7060368
MH
8097 for (tem = PREV_INSN (loop_end);
8098 tem && GET_CODE (tem) != JUMP_INSN;
8099 tem = PREV_INSN (tem))
8100 ;
8101
8102 if (tem)
8103 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8104
c48ba252 8105 if (nonneg)
b4ad7b23 8106 {
c48ba252
R
8107 if (tem)
8108 {
c48ba252
R
8109 /* Increment of LABEL_NUSES done above. */
8110 /* Register is now always nonnegative,
8111 so add REG_NONNEG note to the branch. */
8112 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, NULL_RTX,
8113 REG_NOTES (tem));
8114 }
8115 bl->nonneg = 1;
b4ad7b23
RS
8116 }
8117
b4ad7b23
RS
8118 /* Mark that this biv has been reversed. Each giv which depends
8119 on this biv, and which is also live past the end of the loop
8120 will have to be fixed up. */
8121
8122 bl->reversed = 1;
8123
8124 if (loop_dump_stream)
b50cb11f
MH
8125 {
8126 fprintf (loop_dump_stream, "Reversed loop");
8127 if (bl->nonneg)
8128 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8129 else
8130 fprintf (loop_dump_stream, "\n");
8131 }
b4ad7b23
RS
8132
8133 return 1;
8134 }
8135 }
8136 }
8137
8138 return 0;
8139}
8140\f
8141/* Verify whether the biv BL appears to be eliminable,
8142 based on the insns in the loop that refer to it.
8143 LOOP_START is the first insn of the loop, and END is the end insn.
8144
8145 If ELIMINATE_P is non-zero, actually do the elimination.
8146
8147 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8148 determine whether invariant insns should be placed inside or at the
8149 start of the loop. */
8150
8151static int
8152maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
8153 struct iv_class *bl;
8154 rtx loop_start;
8155 rtx end;
8156 int eliminate_p;
8157 int threshold, insn_count;
8158{
8159 rtx reg = bl->biv->dest_reg;
bd5a664e 8160 rtx p;
b4ad7b23
RS
8161
8162 /* Scan all insns in the loop, stopping if we find one that uses the
8163 biv in a way that we cannot eliminate. */
8164
8165 for (p = loop_start; p != end; p = NEXT_INSN (p))
8166 {
8167 enum rtx_code code = GET_CODE (p);
8168 rtx where = threshold >= insn_count ? loop_start : p;
8169
fdb1833a
R
8170 /* If this is a libcall that sets a giv, skip ahead to its end. */
8171 if (GET_RTX_CLASS (code) == 'i')
8172 {
8173 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8174
8175 if (note)
8176 {
8177 rtx last = XEXP (note, 0);
8178 rtx set = single_set (last);
8179
8180 if (set && GET_CODE (SET_DEST (set)) == REG)
8181 {
8182 int regno = REGNO (SET_DEST (set));
8183
ab519383
GS
8184 if (regno < max_reg_before_loop
8185 && REG_IV_TYPE (regno) == GENERAL_INDUCT
fdb1833a
R
8186 && REG_IV_INFO (regno)->src_reg == bl->biv->src_reg)
8187 p = last;
8188 }
8189 }
8190 }
b4ad7b23
RS
8191 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8192 && reg_mentioned_p (reg, PATTERN (p))
8193 && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
8194 {
8195 if (loop_dump_stream)
8196 fprintf (loop_dump_stream,
8197 "Cannot eliminate biv %d: biv used in insn %d.\n",
8198 bl->regno, INSN_UID (p));
8199 break;
8200 }
8201 }
8202
8203 if (p == end)
8204 {
8205 if (loop_dump_stream)
8206 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8207 bl->regno, eliminate_p ? "was" : "can be");
8208 return 1;
8209 }
8210
8211 return 0;
8212}
8213\f
a6207a2b 8214/* INSN and REFERENCE are instructions in the same insn chain.
f38cbf0f 8215 Return non-zero if INSN is first. */
a6207a2b 8216
c99f8c2a 8217int
a6207a2b
R
8218loop_insn_first_p (insn, reference)
8219 rtx insn, reference;
8220{
f38cbf0f
R
8221 rtx p, q;
8222
8223 for (p = insn, q = reference; ;)
8224 {
8225 /* Start with test for not first so that INSN == REFERENCE yields not
8226 first. */
8227 if (q == insn || ! p)
8228 return 0;
8229 if (p == reference || ! q)
8230 return 1;
8231
7c2772f1
R
8232 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8233 previous insn, hence the <= comparison below does not work if
8234 P is a note. */
f38cbf0f 8235 if (INSN_UID (p) < max_uid_for_loop
7c2772f1
R
8236 && INSN_UID (q) < max_uid_for_loop
8237 && GET_CODE (p) != NOTE)
8238 return INSN_LUID (p) <= INSN_LUID (q);
f38cbf0f 8239
7c2772f1
R
8240 if (INSN_UID (p) >= max_uid_for_loop
8241 || GET_CODE (p) == NOTE)
f38cbf0f
R
8242 p = NEXT_INSN (p);
8243 if (INSN_UID (q) >= max_uid_for_loop)
8244 q = NEXT_INSN (q);
8245 }
a6207a2b
R
8246}
8247
8248/* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8249 the offset that we have to take into account due to auto-increment /
8250 div derivation is zero. */
8251static int
8252biv_elimination_giv_has_0_offset (biv, giv, insn)
8253 struct induction *biv, *giv;
8254 rtx insn;
8255{
8256 /* If the giv V had the auto-inc address optimization applied
8257 to it, and INSN occurs between the giv insn and the biv
8258 insn, then we'd have to adjust the value used here.
8259 This is rare, so we don't bother to make this possible. */
8260 if (giv->auto_inc_opt
8261 && ((loop_insn_first_p (giv->insn, insn)
8262 && loop_insn_first_p (insn, biv->insn))
8263 || (loop_insn_first_p (biv->insn, insn)
8264 && loop_insn_first_p (insn, giv->insn))))
8265 return 0;
8266
8267 /* If the giv V was derived from another giv, and INSN does
8268 not occur between the giv insn and the biv insn, then we'd
8269 have to adjust the value used here. This is rare, so we don't
8270 bother to make this possible. */
8271 if (giv->derived_from
8272 && ! (giv->always_executed
8273 && loop_insn_first_p (giv->insn, insn)
8274 && loop_insn_first_p (insn, biv->insn)))
8275 return 0;
8276 if (giv->same
8277 && giv->same->derived_from
8278 && ! (giv->same->always_executed
8279 && loop_insn_first_p (giv->same->insn, insn)
8280 && loop_insn_first_p (insn, biv->insn)))
8281 return 0;
8282
8283 return 1;
8284}
8285
b4ad7b23
RS
8286/* If BL appears in X (part of the pattern of INSN), see if we can
8287 eliminate its use. If so, return 1. If not, return 0.
8288
8289 If BIV does not appear in X, return 1.
8290
8291 If ELIMINATE_P is non-zero, actually do the elimination. WHERE indicates
8292 where extra insns should be added. Depending on how many items have been
8293 moved out of the loop, it will either be before INSN or at the start of
8294 the loop. */
8295
8296static int
8297maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
8298 rtx x, insn;
8299 struct iv_class *bl;
8300 int eliminate_p;
8301 rtx where;
8302{
8303 enum rtx_code code = GET_CODE (x);
8304 rtx reg = bl->biv->dest_reg;
8305 enum machine_mode mode = GET_MODE (reg);
8306 struct induction *v;
51723711
KG
8307 rtx arg, tem;
8308#ifdef HAVE_cc0
8309 rtx new;
8310#endif
b4ad7b23
RS
8311 int arg_operand;
8312 char *fmt;
8313 int i, j;
8314
8315 switch (code)
8316 {
8317 case REG:
8318 /* If we haven't already been able to do something with this BIV,
8319 we can't eliminate it. */
8320 if (x == reg)
8321 return 0;
8322 return 1;
8323
8324 case SET:
8325 /* If this sets the BIV, it is not a problem. */
8326 if (SET_DEST (x) == reg)
8327 return 1;
8328
8329 /* If this is an insn that defines a giv, it is also ok because
8330 it will go away when the giv is reduced. */
8331 for (v = bl->giv; v; v = v->next_iv)
8332 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8333 return 1;
8334
8335#ifdef HAVE_cc0
8336 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8337 {
8338 /* Can replace with any giv that was reduced and
8339 that has (MULT_VAL != 0) and (ADD_VAL == 0).
fbdc6da8
RK
8340 Require a constant for MULT_VAL, so we know it's nonzero.
8341 ??? We disable this optimization to avoid potential
8342 overflows. */
b4ad7b23
RS
8343
8344 for (v = bl->giv; v; v = v->next_iv)
8345 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
8346 && v->add_val == const0_rtx
453331a3 8347 && ! v->ignore && ! v->maybe_dead && v->always_computable
fbdc6da8
RK
8348 && v->mode == mode
8349 && 0)
b4ad7b23 8350 {
a6207a2b 8351 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8352 continue;
8353
b4ad7b23
RS
8354 if (! eliminate_p)
8355 return 1;
8356
8357 /* If the giv has the opposite direction of change,
8358 then reverse the comparison. */
8359 if (INTVAL (v->mult_val) < 0)
38a448ca
RH
8360 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8361 const0_rtx, v->new_reg);
b4ad7b23
RS
8362 else
8363 new = v->new_reg;
8364
8365 /* We can probably test that giv's reduced reg. */
8366 if (validate_change (insn, &SET_SRC (x), new, 0))
8367 return 1;
8368 }
8369
8370 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8371 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
fbdc6da8
RK
8372 Require a constant for MULT_VAL, so we know it's nonzero.
8373 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8374 overflow problem. */
b4ad7b23
RS
8375
8376 for (v = bl->giv; v; v = v->next_iv)
8377 if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
453331a3 8378 && ! v->ignore && ! v->maybe_dead && v->always_computable
fbdc6da8
RK
8379 && v->mode == mode
8380 && (GET_CODE (v->add_val) == SYMBOL_REF
8381 || GET_CODE (v->add_val) == LABEL_REF
8382 || GET_CODE (v->add_val) == CONST
8383 || (GET_CODE (v->add_val) == REG
8384 && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
b4ad7b23 8385 {
a6207a2b 8386 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8387 continue;
8388
b4ad7b23
RS
8389 if (! eliminate_p)
8390 return 1;
8391
8392 /* If the giv has the opposite direction of change,
8393 then reverse the comparison. */
8394 if (INTVAL (v->mult_val) < 0)
38a448ca
RH
8395 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8396 v->new_reg);
b4ad7b23 8397 else
38a448ca
RH
8398 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8399 copy_rtx (v->add_val));
b4ad7b23
RS
8400
8401 /* Replace biv with the giv's reduced register. */
8402 update_reg_last_use (v->add_val, insn);
8403 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8404 return 1;
8405
8406 /* Insn doesn't support that constant or invariant. Copy it
8407 into a register (it will be a loop invariant.) */
8408 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8409
8410 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
8411 where);
8412
2ae3dcac
RK
8413 /* Substitute the new register for its invariant value in
8414 the compare expression. */
8415 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8416 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
b4ad7b23
RS
8417 return 1;
8418 }
8419 }
8420#endif
8421 break;
8422
8423 case COMPARE:
8424 case EQ: case NE:
8425 case GT: case GE: case GTU: case GEU:
8426 case LT: case LE: case LTU: case LEU:
8427 /* See if either argument is the biv. */
8428 if (XEXP (x, 0) == reg)
8429 arg = XEXP (x, 1), arg_operand = 1;
8430 else if (XEXP (x, 1) == reg)
8431 arg = XEXP (x, 0), arg_operand = 0;
8432 else
8433 break;
8434
8435 if (CONSTANT_P (arg))
8436 {
8437 /* First try to replace with any giv that has constant positive
8438 mult_val and constant add_val. We might be able to support
8439 negative mult_val, but it seems complex to do it in general. */
8440
8441 for (v = bl->giv; v; v = v->next_iv)
8442 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
fbdc6da8
RK
8443 && (GET_CODE (v->add_val) == SYMBOL_REF
8444 || GET_CODE (v->add_val) == LABEL_REF
8445 || GET_CODE (v->add_val) == CONST
8446 || (GET_CODE (v->add_val) == REG
8447 && REGNO_POINTER_FLAG (REGNO (v->add_val))))
453331a3 8448 && ! v->ignore && ! v->maybe_dead && v->always_computable
b4ad7b23
RS
8449 && v->mode == mode)
8450 {
a6207a2b 8451 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8452 continue;
8453
b4ad7b23
RS
8454 if (! eliminate_p)
8455 return 1;
8456
8457 /* Replace biv with the giv's reduced reg. */
8458 XEXP (x, 1-arg_operand) = v->new_reg;
8459
8460 /* If all constants are actually constant integers and
8461 the derived constant can be directly placed in the COMPARE,
8462 do so. */
8463 if (GET_CODE (arg) == CONST_INT
8464 && GET_CODE (v->mult_val) == CONST_INT
8465 && GET_CODE (v->add_val) == CONST_INT
8466 && validate_change (insn, &XEXP (x, arg_operand),
5fd8383e
RK
8467 GEN_INT (INTVAL (arg)
8468 * INTVAL (v->mult_val)
8469 + INTVAL (v->add_val)), 0))
b4ad7b23
RS
8470 return 1;
8471
8472 /* Otherwise, load it into a register. */
8473 tem = gen_reg_rtx (mode);
8474 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8475 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
8476 return 1;
8477
8478 /* If that failed, put back the change we made above. */
8479 XEXP (x, 1-arg_operand) = reg;
8480 }
8481
8482 /* Look for giv with positive constant mult_val and nonconst add_val.
fbdc6da8
RK
8483 Insert insns to calculate new compare value.
8484 ??? Turn this off due to possible overflow. */
b4ad7b23
RS
8485
8486 for (v = bl->giv; v; v = v->next_iv)
d45cf215 8487 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
453331a3 8488 && ! v->ignore && ! v->maybe_dead && v->always_computable
fbdc6da8
RK
8489 && v->mode == mode
8490 && 0)
b4ad7b23
RS
8491 {
8492 rtx tem;
8493
a6207a2b 8494 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8495 continue;
8496
b4ad7b23
RS
8497 if (! eliminate_p)
8498 return 1;
8499
8500 tem = gen_reg_rtx (mode);
8501
8502 /* Replace biv with giv's reduced register. */
8503 validate_change (insn, &XEXP (x, 1 - arg_operand),
8504 v->new_reg, 1);
8505
8506 /* Compute value to compare against. */
8507 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
8508 /* Use it in this insn. */
8509 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8510 if (apply_change_group ())
8511 return 1;
8512 }
8513 }
8514 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8515 {
8516 if (invariant_p (arg) == 1)
8517 {
8518 /* Look for giv with constant positive mult_val and nonconst
fbdc6da8
RK
8519 add_val. Insert insns to compute new compare value.
8520 ??? Turn this off due to possible overflow. */
b4ad7b23
RS
8521
8522 for (v = bl->giv; v; v = v->next_iv)
8523 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
453331a3 8524 && ! v->ignore && ! v->maybe_dead && v->always_computable
fbdc6da8
RK
8525 && v->mode == mode
8526 && 0)
b4ad7b23
RS
8527 {
8528 rtx tem;
8529
a6207a2b 8530 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8531 continue;
8532
b4ad7b23
RS
8533 if (! eliminate_p)
8534 return 1;
8535
8536 tem = gen_reg_rtx (mode);
8537
8538 /* Replace biv with giv's reduced register. */
8539 validate_change (insn, &XEXP (x, 1 - arg_operand),
8540 v->new_reg, 1);
8541
8542 /* Compute value to compare against. */
8543 emit_iv_add_mult (arg, v->mult_val, v->add_val,
8544 tem, where);
8545 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8546 if (apply_change_group ())
8547 return 1;
8548 }
8549 }
8550
8551 /* This code has problems. Basically, you can't know when
8552 seeing if we will eliminate BL, whether a particular giv
8553 of ARG will be reduced. If it isn't going to be reduced,
8554 we can't eliminate BL. We can try forcing it to be reduced,
8555 but that can generate poor code.
8556
8557 The problem is that the benefit of reducing TV, below should
8558 be increased if BL can actually be eliminated, but this means
8559 we might have to do a topological sort of the order in which
8560 we try to process biv. It doesn't seem worthwhile to do
8561 this sort of thing now. */
8562
8563#if 0
8564 /* Otherwise the reg compared with had better be a biv. */
8565 if (GET_CODE (arg) != REG
3ec2b590 8566 || REG_IV_TYPE (REGNO (arg)) != BASIC_INDUCT)
b4ad7b23
RS
8567 return 0;
8568
8569 /* Look for a pair of givs, one for each biv,
8570 with identical coefficients. */
8571 for (v = bl->giv; v; v = v->next_iv)
8572 {
8573 struct induction *tv;
8574
8575 if (v->ignore || v->maybe_dead || v->mode != mode)
8576 continue;
8577
8578 for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
8579 if (! tv->ignore && ! tv->maybe_dead
8580 && rtx_equal_p (tv->mult_val, v->mult_val)
8581 && rtx_equal_p (tv->add_val, v->add_val)
8582 && tv->mode == mode)
8583 {
a6207a2b 8584 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8516af93
JW
8585 continue;
8586
b4ad7b23
RS
8587 if (! eliminate_p)
8588 return 1;
8589
8590 /* Replace biv with its giv's reduced reg. */
8591 XEXP (x, 1-arg_operand) = v->new_reg;
8592 /* Replace other operand with the other giv's
8593 reduced reg. */
8594 XEXP (x, arg_operand) = tv->new_reg;
8595 return 1;
8596 }
8597 }
8598#endif
8599 }
8600
8601 /* If we get here, the biv can't be eliminated. */
8602 return 0;
8603
8604 case MEM:
8605 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8606 biv is used in it, since it will be replaced. */
8607 for (v = bl->giv; v; v = v->next_iv)
8608 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8609 return 1;
8610 break;
e9a25f70
JL
8611
8612 default:
8613 break;
b4ad7b23
RS
8614 }
8615
8616 /* See if any subexpression fails elimination. */
8617 fmt = GET_RTX_FORMAT (code);
8618 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8619 {
8620 switch (fmt[i])
8621 {
8622 case 'e':
8623 if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl,
8624 eliminate_p, where))
8625 return 0;
8626 break;
8627
8628 case 'E':
8629 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8630 if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
8631 eliminate_p, where))
8632 return 0;
8633 break;
8634 }
8635 }
8636
8637 return 1;
8638}
8639\f
8640/* Return nonzero if the last use of REG
8641 is in an insn following INSN in the same basic block. */
8642
8643static int
8644last_use_this_basic_block (reg, insn)
8645 rtx reg;
8646 rtx insn;
8647{
8648 rtx n;
8649 for (n = insn;
8650 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8651 n = NEXT_INSN (n))
8652 {
b1f21e0a 8653 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
b4ad7b23
RS
8654 return 1;
8655 }
8656 return 0;
8657}
8658\f
8659/* Called via `note_stores' to record the initial value of a biv. Here we
8660 just record the location of the set and process it later. */
8661
8662static void
8663record_initial (dest, set)
8664 rtx dest;
8665 rtx set;
8666{
8667 struct iv_class *bl;
8668
8669 if (GET_CODE (dest) != REG
8670 || REGNO (dest) >= max_reg_before_loop
3ec2b590 8671 || REG_IV_TYPE (REGNO (dest)) != BASIC_INDUCT)
b4ad7b23
RS
8672 return;
8673
8674 bl = reg_biv_class[REGNO (dest)];
8675
8676 /* If this is the first set found, record it. */
8677 if (bl->init_insn == 0)
8678 {
8679 bl->init_insn = note_insn;
8680 bl->init_set = set;
8681 }
8682}
8683\f
8684/* If any of the registers in X are "old" and currently have a last use earlier
8685 than INSN, update them to have a last use of INSN. Their actual last use
8686 will be the previous insn but it will not have a valid uid_luid so we can't
8687 use it. */
8688
8689static void
8690update_reg_last_use (x, insn)
8691 rtx x;
8692 rtx insn;
8693{
8694 /* Check for the case where INSN does not have a valid luid. In this case,
8695 there is no need to modify the regno_last_uid, as this can only happen
8696 when code is inserted after the loop_end to set a pseudo's final value,
8697 and hence this insn will never be the last use of x. */
8698 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
8699 && INSN_UID (insn) < max_uid_for_loop
b1f21e0a
MM
8700 && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
8701 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
b4ad7b23
RS
8702 else
8703 {
8704 register int i, j;
8705 register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
8706 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
8707 {
8708 if (fmt[i] == 'e')
8709 update_reg_last_use (XEXP (x, i), insn);
8710 else if (fmt[i] == 'E')
8711 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8712 update_reg_last_use (XVECEXP (x, i, j), insn);
8713 }
8714 }
8715}
8716\f
8717/* Given a jump insn JUMP, return the condition that will cause it to branch
8718 to its JUMP_LABEL. If the condition cannot be understood, or is an
8719 inequality floating-point comparison which needs to be reversed, 0 will
8720 be returned.
8721
8722 If EARLIEST is non-zero, it is a pointer to a place where the earliest
8723 insn used in locating the condition was found. If a replacement test
8724 of the condition is desired, it should be placed in front of that
8725 insn and we will be sure that the inputs are still valid.
8726
8727 The condition will be returned in a canonical form to simplify testing by
8728 callers. Specifically:
8729
8730 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
8731 (2) Both operands will be machine operands; (cc0) will have been replaced.
8732 (3) If an operand is a constant, it will be the second operand.
8733 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
8734 for GE, GEU, and LEU. */
8735
8736rtx
8737get_condition (jump, earliest)
8738 rtx jump;
8739 rtx *earliest;
8740{
8741 enum rtx_code code;
8742 rtx prev = jump;
8743 rtx set;
8744 rtx tem;
8745 rtx op0, op1;
8746 int reverse_code = 0;
8747 int did_reverse_condition = 0;
f283421d 8748 enum machine_mode mode;
b4ad7b23
RS
8749
8750 /* If this is not a standard conditional jump, we can't parse it. */
8751 if (GET_CODE (jump) != JUMP_INSN
8752 || ! condjump_p (jump) || simplejump_p (jump))
8753 return 0;
8754
8755 code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
f283421d 8756 mode = GET_MODE (XEXP (SET_SRC (PATTERN (jump)), 0));
b4ad7b23
RS
8757 op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
8758 op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
8759
8760 if (earliest)
8761 *earliest = jump;
8762
8763 /* If this branches to JUMP_LABEL when the condition is false, reverse
8764 the condition. */
b5d27be7
RS
8765 if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
8766 && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
b4ad7b23
RS
8767 code = reverse_condition (code), did_reverse_condition ^= 1;
8768
8769 /* If we are comparing a register with zero, see if the register is set
8770 in the previous insn to a COMPARE or a comparison operation. Perform
8771 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
8772 in cse.c */
8773
a18b5d98 8774 while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
b4ad7b23
RS
8775 {
8776 /* Set non-zero when we find something of interest. */
8777 rtx x = 0;
8778
8779#ifdef HAVE_cc0
8780 /* If comparison with cc0, import actual comparison from compare
8781 insn. */
8782 if (op0 == cc0_rtx)
8783 {
8784 if ((prev = prev_nonnote_insn (prev)) == 0
8785 || GET_CODE (prev) != INSN
8786 || (set = single_set (prev)) == 0
8787 || SET_DEST (set) != cc0_rtx)
8788 return 0;
8789
8790 op0 = SET_SRC (set);
8791 op1 = CONST0_RTX (GET_MODE (op0));
8792 if (earliest)
8793 *earliest = prev;
8794 }
8795#endif
8796
8797 /* If this is a COMPARE, pick up the two things being compared. */
8798 if (GET_CODE (op0) == COMPARE)
8799 {
8800 op1 = XEXP (op0, 1);
8801 op0 = XEXP (op0, 0);
8802 continue;
8803 }
8804 else if (GET_CODE (op0) != REG)
8805 break;
8806
8807 /* Go back to the previous insn. Stop if it is not an INSN. We also
8808 stop if it isn't a single set or if it has a REG_INC note because
8809 we don't want to bother dealing with it. */
8810
8811 if ((prev = prev_nonnote_insn (prev)) == 0
8812 || GET_CODE (prev) != INSN
8813 || FIND_REG_INC_NOTE (prev, 0)
8814 || (set = single_set (prev)) == 0)
8815 break;
8816
8817 /* If this is setting OP0, get what it sets it to if it looks
8818 relevant. */
a95c317b 8819 if (rtx_equal_p (SET_DEST (set), op0))
b4ad7b23
RS
8820 {
8821 enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
8822
f283421d
RH
8823 /* ??? We may not combine comparisons done in a CCmode with
8824 comparisons not done in a CCmode. This is to aid targets
8825 like Alpha that have an IEEE compliant EQ instruction, and
8826 a non-IEEE compliant BEQ instruction. The use of CCmode is
8827 actually artificial, simply to prevent the combination, but
12f289ac
JW
8828 should not affect other platforms.
8829
8830 However, we must allow VOIDmode comparisons to match either
8831 CCmode or non-CCmode comparison, because some ports have
8832 modeless comparisons inside branch patterns.
8833
8834 ??? This mode check should perhaps look more like the mode check
8835 in simplify_comparison in combine. */
f283421d 8836
b4ad7b23 8837 if ((GET_CODE (SET_SRC (set)) == COMPARE
b565a316
RK
8838 || (((code == NE
8839 || (code == LT
8840 && GET_MODE_CLASS (inner_mode) == MODE_INT
5fd8383e
RK
8841 && (GET_MODE_BITSIZE (inner_mode)
8842 <= HOST_BITS_PER_WIDE_INT)
b565a316 8843 && (STORE_FLAG_VALUE
5fd8383e
RK
8844 & ((HOST_WIDE_INT) 1
8845 << (GET_MODE_BITSIZE (inner_mode) - 1))))
b565a316
RK
8846#ifdef FLOAT_STORE_FLAG_VALUE
8847 || (code == LT
8848 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8849 && FLOAT_STORE_FLAG_VALUE < 0)
8850#endif
8851 ))
f283421d 8852 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
12f289ac
JW
8853 && (((GET_MODE_CLASS (mode) == MODE_CC)
8854 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8855 || mode == VOIDmode || inner_mode == VOIDmode))
b4ad7b23 8856 x = SET_SRC (set);
b565a316
RK
8857 else if (((code == EQ
8858 || (code == GE
5fd8383e
RK
8859 && (GET_MODE_BITSIZE (inner_mode)
8860 <= HOST_BITS_PER_WIDE_INT)
b565a316
RK
8861 && GET_MODE_CLASS (inner_mode) == MODE_INT
8862 && (STORE_FLAG_VALUE
5fd8383e
RK
8863 & ((HOST_WIDE_INT) 1
8864 << (GET_MODE_BITSIZE (inner_mode) - 1))))
b565a316
RK
8865#ifdef FLOAT_STORE_FLAG_VALUE
8866 || (code == GE
8867 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
8868 && FLOAT_STORE_FLAG_VALUE < 0)
fb8ca0a4 8869#endif
b565a316 8870 ))
f283421d 8871 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
12f289ac
JW
8872 && (((GET_MODE_CLASS (mode) == MODE_CC)
8873 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
8874 || mode == VOIDmode || inner_mode == VOIDmode))
8875
b4ad7b23
RS
8876 {
8877 /* We might have reversed a LT to get a GE here. But this wasn't
8878 actually the comparison of data, so we don't flag that we
8879 have had to reverse the condition. */
8880 did_reverse_condition ^= 1;
8881 reverse_code = 1;
8882 x = SET_SRC (set);
8883 }
71ef37f6
RK
8884 else
8885 break;
b4ad7b23
RS
8886 }
8887
8888 else if (reg_set_p (op0, prev))
8889 /* If this sets OP0, but not directly, we have to give up. */
8890 break;
8891
8892 if (x)
8893 {
8894 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8895 code = GET_CODE (x);
8896 if (reverse_code)
8897 {
8898 code = reverse_condition (code);
8899 did_reverse_condition ^= 1;
8900 reverse_code = 0;
8901 }
8902
8903 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
8904 if (earliest)
8905 *earliest = prev;
8906 }
8907 }
8908
8909 /* If constant is first, put it last. */
8910 if (CONSTANT_P (op0))
8911 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
8912
8913 /* If OP0 is the result of a comparison, we weren't able to find what
8914 was really being compared, so fail. */
8915 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
8916 return 0;
8917
d8cfa4ee
RK
8918 /* Canonicalize any ordered comparison with integers involving equality
8919 if we can do computations in the relevant mode and we do not
8920 overflow. */
8921
8922 if (GET_CODE (op1) == CONST_INT
8923 && GET_MODE (op0) != VOIDmode
8924 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
b4ad7b23 8925 {
5fd8383e
RK
8926 HOST_WIDE_INT const_val = INTVAL (op1);
8927 unsigned HOST_WIDE_INT uconst_val = const_val;
d8cfa4ee
RK
8928 unsigned HOST_WIDE_INT max_val
8929 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
b4ad7b23
RS
8930
8931 switch (code)
d8cfa4ee
RK
8932 {
8933 case LE:
e51712db 8934 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
d8cfa4ee
RK
8935 code = LT, op1 = GEN_INT (const_val + 1);
8936 break;
b4ad7b23 8937
460f50dc
R
8938 /* When cross-compiling, const_val might be sign-extended from
8939 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
d8cfa4ee 8940 case GE:
e51712db 8941 if ((HOST_WIDE_INT) (const_val & max_val)
d8cfa4ee
RK
8942 != (((HOST_WIDE_INT) 1
8943 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
8944 code = GT, op1 = GEN_INT (const_val - 1);
8945 break;
b4ad7b23 8946
d8cfa4ee 8947 case LEU:
460f50dc 8948 if (uconst_val < max_val)
d8cfa4ee
RK
8949 code = LTU, op1 = GEN_INT (uconst_val + 1);
8950 break;
b4ad7b23 8951
d8cfa4ee
RK
8952 case GEU:
8953 if (uconst_val != 0)
8954 code = GTU, op1 = GEN_INT (uconst_val - 1);
8955 break;
e9a25f70
JL
8956
8957 default:
8958 break;
d8cfa4ee 8959 }
b4ad7b23
RS
8960 }
8961
8962 /* If this was floating-point and we reversed anything other than an
8963 EQ or NE, return zero. */
8964 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
8965 && did_reverse_condition && code != NE && code != EQ
1fc3d466 8966 && ! flag_fast_math
b4ad7b23
RS
8967 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8968 return 0;
8969
8970#ifdef HAVE_cc0
8971 /* Never return CC0; return zero instead. */
8972 if (op0 == cc0_rtx)
8973 return 0;
8974#endif
8975
38a448ca 8976 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
b4ad7b23
RS
8977}
8978
8979/* Similar to above routine, except that we also put an invariant last
8980 unless both operands are invariants. */
8981
8982rtx
8983get_condition_for_loop (x)
8984 rtx x;
8985{
5fd8383e 8986 rtx comparison = get_condition (x, NULL_PTR);
b4ad7b23
RS
8987
8988 if (comparison == 0
8989 || ! invariant_p (XEXP (comparison, 0))
8990 || invariant_p (XEXP (comparison, 1)))
8991 return comparison;
8992
38a448ca
RH
8993 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
8994 XEXP (comparison, 1), XEXP (comparison, 0));
b4ad7b23 8995}
8c660648 8996
51723711 8997#ifdef HAVE_decrement_and_branch_on_count
cac8ce95
DE
8998/* Instrument loop for insertion of bct instruction. We distinguish between
8999 loops with compile-time bounds and those with run-time bounds.
9000 Information from loop_iterations() is used to compute compile-time bounds.
9001 Run-time bounds should use loop preconditioning, but currently ignored.
9002 */
9003
45f97e2e 9004static void
302670f3 9005insert_bct (loop_start, loop_end, loop_info)
cac8ce95 9006 rtx loop_start, loop_end;
302670f3 9007 struct loop_info *loop_info;
8c660648 9008{
8c660648 9009 int i;
cac8ce95 9010 unsigned HOST_WIDE_INT n_iterations;
8c660648 9011
cac8ce95 9012 int increment_direction, compare_direction;
8c660648 9013
cac8ce95
DE
9014 /* If the loop condition is <= or >=, the number of iteration
9015 is 1 more than the range of the bounds of the loop. */
9016 int add_iteration = 0;
8c660648 9017
cac8ce95 9018 enum machine_mode loop_var_mode = word_mode;
8c660648 9019
cac8ce95 9020 int loop_num = uid_loop_num [INSN_UID (loop_start)];
8c660648 9021
cac8ce95 9022 /* It's impossible to instrument a competely unrolled loop. */
302670f3 9023 if (loop_info->unroll_number == -1)
8c660648 9024 return;
8c660648 9025
cac8ce95
DE
9026 /* Make sure that the count register is not in use. */
9027 if (loop_used_count_register [loop_num])
8c660648 9028 {
cac8ce95 9029 if (loop_dump_stream)
8c660648 9030 fprintf (loop_dump_stream,
cac8ce95
DE
9031 "insert_bct %d: BCT instrumentation failed: count register already in use\n",
9032 loop_num);
8c660648
JL
9033 return;
9034 }
9035
cac8ce95
DE
9036 /* Make sure that the function has no indirect jumps. */
9037 if (indirect_jump_in_function)
9038 {
8c660648
JL
9039 if (loop_dump_stream)
9040 fprintf (loop_dump_stream,
cac8ce95
DE
9041 "insert_bct %d: BCT instrumentation failed: indirect jump in function\n",
9042 loop_num);
8c660648
JL
9043 return;
9044 }
9045
cac8ce95
DE
9046 /* Make sure that the last loop insn is a conditional jump. */
9047 if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
9048 || ! condjump_p (PREV_INSN (loop_end))
9049 || simplejump_p (PREV_INSN (loop_end)))
9050 {
8c660648
JL
9051 if (loop_dump_stream)
9052 fprintf (loop_dump_stream,
cac8ce95
DE
9053 "insert_bct %d: BCT instrumentation failed: invalid jump at loop end\n",
9054 loop_num);
8c660648
JL
9055 return;
9056 }
8c660648 9057
cac8ce95
DE
9058 /* Make sure that the loop does not contain a function call
9059 (the count register might be altered by the called function). */
9060 if (loop_has_call)
8c660648 9061 {
cac8ce95
DE
9062 if (loop_dump_stream)
9063 fprintf (loop_dump_stream,
9064 "insert_bct %d: BCT instrumentation failed: function call in loop\n",
9065 loop_num);
9066 return;
9067 }
8c660648 9068
cac8ce95
DE
9069 /* Make sure that the loop does not jump via a table.
9070 (the count register might be used to perform the branch on table). */
8c368ee2 9071 if (loop_has_tablejump)
cac8ce95 9072 {
8c368ee2
DE
9073 if (loop_dump_stream)
9074 fprintf (loop_dump_stream,
9075 "insert_bct %d: BCT instrumentation failed: computed branch in the loop\n",
9076 loop_num);
9077 return;
cac8ce95 9078 }
8c660648 9079
cac8ce95 9080 /* Account for loop unrolling in instrumented iteration count. */
302670f3
MH
9081 if (loop_info->unroll_number > 1)
9082 n_iterations = loop_info->n_iterations / loop_info->unroll_number;
cac8ce95 9083 else
302670f3 9084 n_iterations = loop_info->n_iterations;
8c660648 9085
cac8ce95
DE
9086 if (n_iterations != 0 && n_iterations < 3)
9087 {
9088 /* Allow an enclosing outer loop to benefit if possible. */
9089 if (loop_dump_stream)
9090 fprintf (loop_dump_stream,
9091 "insert_bct %d: Too few iterations to benefit from BCT optimization\n",
9092 loop_num);
9093 return;
9094 }
8c660648 9095
cac8ce95 9096 /* Try to instrument the loop. */
8c660648 9097
cac8ce95
DE
9098 /* Handle the simpler case, where the bounds are known at compile time. */
9099 if (n_iterations > 0)
9100 {
9101 /* Mark all enclosing loops that they cannot use count register. */
8c368ee2 9102 for (i = loop_num; i != -1; i = loop_outer_loop[i])
cac8ce95 9103 loop_used_count_register[i] = 1;
8c660648 9104 instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
8c660648
JL
9105 return;
9106 }
9107
cac8ce95
DE
9108 /* Handle the more complex case, that the bounds are NOT known
9109 at compile time. In this case we generate run_time calculation
9110 of the number of iterations. */
9111
302670f3 9112 if (loop_info->iteration_var == 0)
400d6322
DE
9113 {
9114 if (loop_dump_stream)
9115 fprintf (loop_dump_stream,
9116 "insert_bct %d: BCT Runtime Instrumentation failed: no loop iteration variable found\n",
9117 loop_num);
9118 return;
9119 }
9120
302670f3
MH
9121 if (GET_MODE_CLASS (GET_MODE (loop_info->iteration_var)) != MODE_INT
9122 || GET_MODE_SIZE (GET_MODE (loop_info->iteration_var)) != UNITS_PER_WORD)
cac8ce95
DE
9123 {
9124 if (loop_dump_stream)
9125 fprintf (loop_dump_stream,
400d6322 9126 "insert_bct %d: BCT Runtime Instrumentation failed: loop variable not integer\n",
cac8ce95
DE
9127 loop_num);
9128 return;
9129 }
8c660648
JL
9130
9131 /* With runtime bounds, if the compare is of the form '!=' we give up */
302670f3 9132 if (loop_info->comparison_code == NE)
cac8ce95
DE
9133 {
9134 if (loop_dump_stream)
9135 fprintf (loop_dump_stream,
400d6322 9136 "insert_bct %d: BCT Runtime Instrumentation failed: runtime bounds with != comparison\n",
cac8ce95
DE
9137 loop_num);
9138 return;
9139 }
9140/* Use common loop preconditioning code instead. */
9141#if 0
9142 else
9143 {
9144 /* We rely on the existence of run-time guard to ensure that the
9145 loop executes at least once. */
9146 rtx sequence;
9147 rtx iterations_num_reg;
8c660648 9148
cac8ce95
DE
9149 unsigned HOST_WIDE_INT increment_value_abs
9150 = INTVAL (increment) * increment_direction;
8c660648 9151
cac8ce95
DE
9152 /* make sure that the increment is a power of two, otherwise (an
9153 expensive) divide is needed. */
9154 if (exact_log2 (increment_value_abs) == -1)
9155 {
9156 if (loop_dump_stream)
9157 fprintf (loop_dump_stream,
9158 "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
9159 return;
9160 }
8c660648 9161
cac8ce95
DE
9162 /* compute the number of iterations */
9163 start_sequence ();
8c660648 9164 {
cac8ce95 9165 rtx temp_reg;
8c660648 9166
cac8ce95
DE
9167 /* Again, the number of iterations is calculated by:
9168 ;
9169 ; compare-val - initial-val + (increment -1) + additional-iteration
9170 ; num_iterations = -----------------------------------------------------------------
9171 ; increment
8c660648 9172 */
cac8ce95
DE
9173 /* ??? Do we have to call copy_rtx here before passing rtx to
9174 expand_binop? */
9175 if (compare_direction > 0)
9176 {
9177 /* <, <= :the loop variable is increasing */
9178 temp_reg = expand_binop (loop_var_mode, sub_optab,
9179 comparison_value, initial_value,
9180 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9181 }
9182 else
9183 {
9184 temp_reg = expand_binop (loop_var_mode, sub_optab,
9185 initial_value, comparison_value,
9186 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9187 }
8c660648 9188
cac8ce95
DE
9189 if (increment_value_abs - 1 + add_iteration != 0)
9190 temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
9191 GEN_INT (increment_value_abs - 1
9192 + add_iteration),
9193 NULL_RTX, 0, OPTAB_LIB_WIDEN);
8c660648 9194
cac8ce95
DE
9195 if (increment_value_abs != 1)
9196 {
9197 /* ??? This will generate an expensive divide instruction for
9198 most targets. The original authors apparently expected this
9199 to be a shift, since they test for power-of-2 divisors above,
9200 but just naively generating a divide instruction will not give
9201 a shift. It happens to work for the PowerPC target because
9202 the rs6000.md file has a divide pattern that emits shifts.
9203 It will probably not work for any other target. */
9204 iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
9205 temp_reg,
9206 GEN_INT (increment_value_abs),
9207 NULL_RTX, 0, OPTAB_LIB_WIDEN);
9208 }
9209 else
9210 iterations_num_reg = temp_reg;
9211 }
9212 sequence = gen_sequence ();
9213 end_sequence ();
9214 emit_insn_before (sequence, loop_start);
9215 instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
8c660648 9216 }
cac8ce95
DE
9217
9218 return;
9219#endif /* Complex case */
8c660648
JL
9220}
9221
cac8ce95
DE
9222/* Instrument loop by inserting a bct in it as follows:
9223 1. A new counter register is created.
9224 2. In the head of the loop the new variable is initialized to the value
9225 passed in the loop_num_iterations parameter.
8c660648 9226 3. At the end of the loop, comparison of the register with 0 is generated.
cac8ce95
DE
9227 The created comparison follows the pattern defined for the
9228 decrement_and_branch_on_count insn, so this insn will be generated.
9229 4. The branch on the old variable are deleted. The compare must remain
9230 because it might be used elsewhere. If the loop-variable or condition
9231 register are used elsewhere, they will be eliminated by flow. */
8c660648
JL
9232
9233static void
9234instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
9235 rtx loop_start, loop_end;
9236 rtx loop_num_iterations;
9237{
cac8ce95 9238 rtx counter_reg;
8c660648 9239 rtx start_label;
8c660648 9240 rtx sequence;
8c660648 9241
8c660648
JL
9242 if (HAVE_decrement_and_branch_on_count)
9243 {
9244 if (loop_dump_stream)
cac8ce95
DE
9245 {
9246 fputs ("instrument_bct: Inserting BCT (", loop_dump_stream);
9247 if (GET_CODE (loop_num_iterations) == CONST_INT)
9248 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
9249 INTVAL (loop_num_iterations));
9250 else
9251 fputs ("runtime", loop_dump_stream);
9252 fputs (" iterations)", loop_dump_stream);
9253 }
8c660648 9254
5accd822
DE
9255 /* Discard original jump to continue loop. Original compare result
9256 may still be live, so it cannot be discarded explicitly. */
8c660648
JL
9257 delete_insn (PREV_INSN (loop_end));
9258
cac8ce95 9259 /* Insert the label which will delimit the start of the loop. */
8c660648
JL
9260 start_label = gen_label_rtx ();
9261 emit_label_after (start_label, loop_start);
9262
cac8ce95 9263 /* Insert initialization of the count register into the loop header. */
8c660648 9264 start_sequence ();
cac8ce95
DE
9265 counter_reg = gen_reg_rtx (word_mode);
9266 emit_insn (gen_move_insn (counter_reg, loop_num_iterations));
8c660648
JL
9267 sequence = gen_sequence ();
9268 end_sequence ();
5accd822 9269 emit_insn_before (sequence, loop_start);
8c660648 9270
cac8ce95 9271 /* Insert new comparison on the count register instead of the
8c660648
JL
9272 old one, generating the needed BCT pattern (that will be
9273 later recognized by assembly generation phase). */
cac8ce95 9274 emit_jump_insn_before (gen_decrement_and_branch_on_count (counter_reg,
5accd822 9275 start_label),
8c660648
JL
9276 loop_end);
9277 LABEL_NUSES (start_label)++;
9278 }
9279
8c660648 9280}
51723711
KG
9281#endif /* HAVE_decrement_and_branch_on_count */
9282
2a1777af 9283/* Scan the function and determine whether it has indirect (computed) jumps.
8c660648 9284
2a1777af
JL
9285 This is taken mostly from flow.c; similar code exists elsewhere
9286 in the compiler. It may be useful to put this into rtlanal.c. */
8c660648
JL
9287static int
9288indirect_jump_in_function_p (start)
9289 rtx start;
9290{
9291 rtx insn;
8c660648 9292
2a1777af
JL
9293 for (insn = start; insn; insn = NEXT_INSN (insn))
9294 if (computed_jump_p (insn))
9295 return 1;
7019d00e
L
9296
9297 return 0;
8c660648 9298}
41a972a9
MM
9299
9300/* Add MEM to the LOOP_MEMS array, if appropriate. See the
9301 documentation for LOOP_MEMS for the definition of `appropriate'.
9302 This function is called from prescan_loop via for_each_rtx. */
9303
9304static int
9305insert_loop_mem (mem, data)
9306 rtx *mem;
e51712db 9307 void *data ATTRIBUTE_UNUSED;
41a972a9
MM
9308{
9309 int i;
9310 rtx m = *mem;
9311
9312 if (m == NULL_RTX)
9313 return 0;
9314
9315 switch (GET_CODE (m))
9316 {
9317 case MEM:
9318 break;
9319
9320 case CONST_DOUBLE:
9321 /* We're not interested in the MEM associated with a
9322 CONST_DOUBLE, so there's no need to traverse into this. */
9323 return -1;
9324
9325 default:
9326 /* This is not a MEM. */
9327 return 0;
9328 }
9329
9330 /* See if we've already seen this MEM. */
9331 for (i = 0; i < loop_mems_idx; ++i)
9332 if (rtx_equal_p (m, loop_mems[i].mem))
9333 {
9334 if (GET_MODE (m) != GET_MODE (loop_mems[i].mem))
9335 /* The modes of the two memory accesses are different. If
9336 this happens, something tricky is going on, and we just
9337 don't optimize accesses to this MEM. */
9338 loop_mems[i].optimize = 0;
9339
9340 return 0;
9341 }
9342
9343 /* Resize the array, if necessary. */
9344 if (loop_mems_idx == loop_mems_allocated)
9345 {
9346 if (loop_mems_allocated != 0)
9347 loop_mems_allocated *= 2;
9348 else
9349 loop_mems_allocated = 32;
9350
9351 loop_mems = (loop_mem_info*)
9352 xrealloc (loop_mems,
9353 loop_mems_allocated * sizeof (loop_mem_info));
9354 }
9355
9356 /* Actually insert the MEM. */
9357 loop_mems[loop_mems_idx].mem = m;
9358 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9359 because we can't put it in a register. We still store it in the
9360 table, though, so that if we see the same address later, but in a
9361 non-BLK mode, we'll not think we can optimize it at that point. */
9362 loop_mems[loop_mems_idx].optimize = (GET_MODE (m) != BLKmode);
9363 loop_mems[loop_mems_idx].reg = NULL_RTX;
9364 ++loop_mems_idx;
8deb8e2c
MM
9365
9366 return 0;
41a972a9
MM
9367}
9368
4b259e3f 9369/* Like load_mems, but also ensures that SET_IN_LOOP,
41a972a9
MM
9370 MAY_NOT_OPTIMIZE, REG_SINGLE_USAGE, and INSN_COUNT have the correct
9371 values after load_mems. */
9372
9373static void
9374load_mems_and_recount_loop_regs_set (scan_start, end, loop_top, start,
d6b44532 9375 insn_count)
41a972a9
MM
9376 rtx scan_start;
9377 rtx end;
9378 rtx loop_top;
9379 rtx start;
41a972a9
MM
9380 int *insn_count;
9381{
9382 int nregs = max_reg_num ();
9383
9384 load_mems (scan_start, end, loop_top, start);
9385
4b259e3f 9386 /* Recalculate set_in_loop and friends since load_mems may have
41a972a9
MM
9387 created new registers. */
9388 if (max_reg_num () > nregs)
9389 {
9390 int i;
9391 int old_nregs;
9392
9393 old_nregs = nregs;
9394 nregs = max_reg_num ();
9395
4b259e3f 9396 if ((unsigned) nregs > set_in_loop->num_elements)
8deb8e2c
MM
9397 {
9398 /* Grow all the arrays. */
4b259e3f 9399 VARRAY_GROW (set_in_loop, nregs);
8deb8e2c 9400 VARRAY_GROW (n_times_set, nregs);
8deb8e2c 9401 VARRAY_GROW (may_not_optimize, nregs);
d6b44532 9402 VARRAY_GROW (reg_single_usage, nregs);
8deb8e2c
MM
9403 }
9404 /* Clear the arrays */
4b259e3f 9405 bzero ((char *) &set_in_loop->data, nregs * sizeof (int));
8deb8e2c 9406 bzero ((char *) &may_not_optimize->data, nregs * sizeof (char));
d6b44532 9407 bzero ((char *) &reg_single_usage->data, nregs * sizeof (rtx));
41a972a9
MM
9408
9409 count_loop_regs_set (loop_top ? loop_top : start, end,
9410 may_not_optimize, reg_single_usage,
9411 insn_count, nregs);
9412
9413 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
8deb8e2c
MM
9414 {
9415 VARRAY_CHAR (may_not_optimize, i) = 1;
4b259e3f 9416 VARRAY_INT (set_in_loop, i) = 1;
8deb8e2c 9417 }
41a972a9 9418
dd0208b9
DM
9419#ifdef AVOID_CCMODE_COPIES
9420 /* Don't try to move insns which set CC registers if we should not
9421 create CCmode register copies. */
78b87d18 9422 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
dd0208b9 9423 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
8deb8e2c 9424 VARRAY_CHAR (may_not_optimize, i) = 1;
dd0208b9
DM
9425#endif
9426
4b259e3f
R
9427 /* Set n_times_set for the new registers. */
9428 bcopy ((char *) (&set_in_loop->data.i[0] + old_nregs),
9429 (char *) (&n_times_set->data.i[0] + old_nregs),
41a972a9
MM
9430 (nregs - old_nregs) * sizeof (int));
9431 }
9432}
9433
9434/* Move MEMs into registers for the duration of the loop. SCAN_START
9435 is the first instruction in the loop (as it is executed). The
9436 other parameters are as for next_insn_in_loop. */
9437
9438static void
9439load_mems (scan_start, end, loop_top, start)
9440 rtx scan_start;
9441 rtx end;
9442 rtx loop_top;
9443 rtx start;
9444{
9445 int maybe_never = 0;
9446 int i;
9447 rtx p;
9448 rtx label = NULL_RTX;
9449 rtx end_label;
9450
9451 if (loop_mems_idx > 0)
9452 {
9453 /* Nonzero if the next instruction may never be executed. */
9454 int next_maybe_never = 0;
9455
9456 /* Check to see if it's possible that some instructions in the
9457 loop are never executed. */
9458 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
9459 p != NULL_RTX && !maybe_never;
9460 p = next_insn_in_loop (p, scan_start, end, loop_top))
9461 {
9462 if (GET_CODE (p) == CODE_LABEL)
9463 maybe_never = 1;
9464 else if (GET_CODE (p) == JUMP_INSN
9465 /* If we enter the loop in the middle, and scan
9466 around to the beginning, don't set maybe_never
9467 for that. This must be an unconditional jump,
9468 otherwise the code at the top of the loop might
9469 never be executed. Unconditional jumps are
9470 followed a by barrier then loop end. */
9471 && ! (GET_CODE (p) == JUMP_INSN
9472 && JUMP_LABEL (p) == loop_top
9473 && NEXT_INSN (NEXT_INSN (p)) == end
9474 && simplejump_p (p)))
9475 {
9476 if (!condjump_p (p))
9477 /* Something complicated. */
9478 maybe_never = 1;
9479 else
9480 /* If there are any more instructions in the loop, they
9481 might not be reached. */
9482 next_maybe_never = 1;
9483 }
9484 else if (next_maybe_never)
9485 maybe_never = 1;
9486 }
9487
9488 /* Actually move the MEMs. */
9489 for (i = 0; i < loop_mems_idx; ++i)
9490 {
41a972a9
MM
9491 int written = 0;
9492 rtx reg;
9493 rtx mem = loop_mems[i].mem;
5026a502 9494 rtx mem_list_entry;
41a972a9
MM
9495
9496 if (MEM_VOLATILE_P (mem)
9497 || invariant_p (XEXP (mem, 0)) != 1)
9498 /* There's no telling whether or not MEM is modified. */
9499 loop_mems[i].optimize = 0;
9500
9501 /* Go through the MEMs written to in the loop to see if this
9502 one is aliased by one of them. */
5026a502
JL
9503 mem_list_entry = loop_store_mems;
9504 while (mem_list_entry)
41a972a9 9505 {
5026a502 9506 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
41a972a9 9507 written = 1;
5026a502 9508 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
41a972a9
MM
9509 mem, rtx_varies_p))
9510 {
9511 /* MEM is indeed aliased by this store. */
9512 loop_mems[i].optimize = 0;
9513 break;
9514 }
5026a502 9515 mem_list_entry = XEXP (mem_list_entry, 1);
41a972a9
MM
9516 }
9517
9518 /* If this MEM is written to, we must be sure that there
9519 are no reads from another MEM that aliases this one. */
9520 if (loop_mems[i].optimize && written)
9521 {
9522 int j;
9523
9524 for (j = 0; j < loop_mems_idx; ++j)
9525 {
9526 if (j == i)
9527 continue;
9528 else if (true_dependence (mem,
9529 VOIDmode,
9530 loop_mems[j].mem,
9531 rtx_varies_p))
9532 {
9533 /* It's not safe to hoist loop_mems[i] out of
9534 the loop because writes to it might not be
9535 seen by reads from loop_mems[j]. */
9536 loop_mems[i].optimize = 0;
9537 break;
9538 }
9539 }
9540 }
9541
9542 if (maybe_never && may_trap_p (mem))
9543 /* We can't access the MEM outside the loop; it might
9544 cause a trap that wouldn't have happened otherwise. */
9545 loop_mems[i].optimize = 0;
9546
9547 if (!loop_mems[i].optimize)
9548 /* We thought we were going to lift this MEM out of the
9549 loop, but later discovered that we could not. */
9550 continue;
9551
9552 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9553 order to keep scan_loop from moving stores to this MEM
9554 out of the loop just because this REG is neither a
9555 user-variable nor used in the loop test. */
9556 reg = gen_reg_rtx (GET_MODE (mem));
9557 REG_USERVAR_P (reg) = 1;
9558 loop_mems[i].reg = reg;
9559
9560 /* Now, replace all references to the MEM with the
9561 corresponding pesudos. */
9562 for (p = next_insn_in_loop (scan_start, scan_start, end, loop_top);
9563 p != NULL_RTX;
9564 p = next_insn_in_loop (p, scan_start, end, loop_top))
9565 {
59d4e481
KGA
9566 rtx_and_int ri;
9567 ri.r = p;
9568 ri.i = i;
41a972a9
MM
9569 for_each_rtx (&p, replace_loop_mem, &ri);
9570 }
9571
9572 if (!apply_change_group ())
9573 /* We couldn't replace all occurrences of the MEM. */
9574 loop_mems[i].optimize = 0;
9575 else
9576 {
9577 rtx set;
9578
9579 /* Load the memory immediately before START, which is
9580 the NOTE_LOOP_BEG. */
826947c5 9581 set = gen_move_insn (reg, mem);
41a972a9
MM
9582 emit_insn_before (set, start);
9583
9584 if (written)
9585 {
9586 if (label == NULL_RTX)
9587 {
9588 /* We must compute the former
9589 right-after-the-end label before we insert
9590 the new one. */
9591 end_label = next_label (end);
9592 label = gen_label_rtx ();
9593 emit_label_after (label, end);
9594 }
9595
9596 /* Store the memory immediately after END, which is
9597 the NOTE_LOOP_END. */
826947c5 9598 set = gen_move_insn (copy_rtx (mem), reg);
41a972a9
MM
9599 emit_insn_after (set, label);
9600 }
9601
9602 if (loop_dump_stream)
9603 {
9604 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9605 REGNO (reg), (written ? "r/w" : "r/o"));
9606 print_rtl (loop_dump_stream, mem);
9607 fputc ('\n', loop_dump_stream);
9608 }
9609 }
9610 }
9611 }
9612
9613 if (label != NULL_RTX)
9614 {
9615 /* Now, we need to replace all references to the previous exit
9616 label with the new one. */
59d4e481
KGA
9617 rtx_pair rr;
9618 rr.r1 = end_label;
9619 rr.r2 = label;
41a972a9
MM
9620
9621 for (p = start; p != end; p = NEXT_INSN (p))
7940acc4
JW
9622 {
9623 for_each_rtx (&p, replace_label, &rr);
9624
9625 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9626 field. This is not handled by for_each_rtx because it doesn't
9627 handle unprinted ('0') fields. We need to update JUMP_LABEL
9628 because the immediately following unroll pass will use it.
9629 replace_label would not work anyways, because that only handles
9630 LABEL_REFs. */
9631 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9632 JUMP_LABEL (p) = label;
9633 }
41a972a9
MM
9634 }
9635}
9636
9637/* Replace MEM with its associated pseudo register. This function is
9638 called from load_mems via for_each_rtx. DATA is actually an
9639 rtx_and_int * describing the instruction currently being scanned
9640 and the MEM we are currently replacing. */
9641
9642static int
9643replace_loop_mem (mem, data)
9644 rtx *mem;
9645 void *data;
9646{
9647 rtx_and_int *ri;
9648 rtx insn;
9649 int i;
9650 rtx m = *mem;
9651
9652 if (m == NULL_RTX)
9653 return 0;
9654
9655 switch (GET_CODE (m))
9656 {
9657 case MEM:
9658 break;
9659
9660 case CONST_DOUBLE:
9661 /* We're not interested in the MEM associated with a
9662 CONST_DOUBLE, so there's no need to traverse into one. */
9663 return -1;
9664
9665 default:
9666 /* This is not a MEM. */
9667 return 0;
9668 }
9669
9670 ri = (rtx_and_int*) data;
9671 i = ri->i;
9672
9673 if (!rtx_equal_p (loop_mems[i].mem, m))
9674 /* This is not the MEM we are currently replacing. */
9675 return 0;
9676
9677 insn = ri->r;
9678
9679 /* Actually replace the MEM. */
9680 validate_change (insn, mem, loop_mems[i].reg, 1);
9681
9682 return 0;
9683}
9684
9685/* Replace occurrences of the old exit label for the loop with the new
9686 one. DATA is an rtx_pair containing the old and new labels,
9687 respectively. */
9688
9689static int
9690replace_label (x, data)
9691 rtx *x;
9692 void *data;
9693{
9694 rtx l = *x;
9695 rtx old_label = ((rtx_pair*) data)->r1;
9696 rtx new_label = ((rtx_pair*) data)->r2;
9697
9698 if (l == NULL_RTX)
9699 return 0;
9700
9701 if (GET_CODE (l) != LABEL_REF)
9702 return 0;
9703
9704 if (XEXP (l, 0) != old_label)
9705 return 0;
9706
9707 XEXP (l, 0) = new_label;
9708 ++LABEL_NUSES (new_label);
9709 --LABEL_NUSES (old_label);
9710
9711 return 0;
9712}
9713
This page took 2.063043 seconds and 5 git commands to generate.