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